Comments (4)
Thank you.
The way I would handle this situation would be to first apply units to the value, then do the conversion. So, something like:
UnitConversion('V/V', 'dB', from_dB, to_dB)
gain = Quantity(10, 'V/V')
print(gain.scale('dB'))
or
UnitConversion(('V/V', 'A/A'), 'dB', from_dB, to_dB)
Vgain = Quantity(10, 'V/V', scale='dB')
Igain = Quantity('1M', 'A/A', scale='dB')
print(Vgain, Igain)
from quantiphy.
Alternatively, you can avoid using UnitConversion
like:
def to_dB(value, units=''):
return 20*math.log10(value), 'dB'
gain = Quantity(10, scale=to_dB)
print(gain.scale('dB'))
(In answering this question I discovered that the scaling functions used by Quantity are different than those used by UnitConversion. That was a surprise and does not seem ideal, but I have not decided how to make them consistent. Thus, the to_dB function used in this answer is different from the one used in the previous answer.)
from quantiphy.
Your first approach of applying units to the quantity seems like the best approach for my use-case. Thanks!
BTW, does quantiphy
detect scales from parsed strings? Ideally I'd like to be able to be able to parse strings with or without dB
without first looking at the string to see if it has a scale, i.e. the following condition should be true:
Quantity("120 dB") == Quantity("1e6")
Is this possible or planned?
from quantiphy.
QuantiPhy will not scale unless you tell it to, but I think you can get what you want with something like this:
>>> UnitConversion('V/V', 'dB', from_dB, to_dB)
>>> for gain in '100 100V/V 40dB'.split():
... lgain = Quantity(gain, 'V/V', scale='V/V')
... print(f'{gain:>6} → {lgain}, {lgain:pdB}')
100 → 100 V/V, 40 dB
100V/V → 100 V/V, 40 dB
40dB → 100 V/V, 40 dB
In this example gain
is a string that may or may not have units. If it does not have units, Quantity
will use the second argument as the default units. Finally the third argument says to convert the given value to 'V/V'. The first two values will not be affected because they already are 'V/V', but the third value is 'dB', so Quantity
will use from_dB
to convert it to 'V/V'.
from quantiphy.
Related Issues (20)
- Quantity returns the wrong value HOT 10
- DeprecationWarning when running py.test HOT 2
- Adding new unicode character for micro: "µ" as an alternative to μ HOT 2
- Custom radix HOT 8
- Scaling Problem when entering Quantity HOT 2
- Upload wheels to PyPI HOT 1
- Consider removing setup_requires from setup.py HOT 3
- Consider using mu (µ) character in render method HOT 8
- Consider permissive license? HOT 1
- Converting nanoseconds to picoseconds? HOT 2
- prec='full' fails HOT 3
- translate to Russian Cyrillic HOT 5
- setup.py no longer lists test requirements HOT 2
- Question on use HOT 3
- add type hints to allow static code analysis using mypy HOT 4
- Added to conda-forge HOT 4
- meter is not recognized HOT 2
- Doc is not working HOT 2
- Adding Packages to Spack
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from quantiphy.