Giter Site home page Giter Site logo

Unitless scales about quantiphy HOT 4 CLOSED

SeanDS avatar SeanDS commented on May 26, 2024
Unitless scales

from quantiphy.

Comments (4)

KenKundert avatar KenKundert commented on May 26, 2024

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.

KenKundert avatar KenKundert commented on May 26, 2024

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.

SeanDS avatar SeanDS commented on May 26, 2024

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.

KenKundert avatar KenKundert commented on May 26, 2024

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}')
   100100 V/V, 40 dB
100V/V100 V/V, 40 dB
  40dB100 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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.