Rescaling of units from a list of preferred units · Issue #172 · python-quantities/python-quantities · GitHub
Skip to content

Rescaling of units from a list of preferred units #172

Open

Description

@rgerkin

I am interested in having the ability to rescale to a preferred set of units (e.g. MKS), without specifying those units in each call. For example, say that I prefer mV, pA, and fC as my units of voltage, current, and charge. I might define a preferred set of units in a downstream package:

import quantities as pq
pq.UnitQuantity('femtocoulomb', 1e-15*pq.C, 'fC')         
pq.preferred = {'V': pq.mV,
                'A': pq.pA,
                'C': pq.UnitQuantity('femtocoulomb', 1e-15*pq.C, 'fC')}

I would then like to change Quantity.rescale() to make the desired units optional, like:

def rescale(self, units=None):
    if units is None:
        unit_str = str(self.simplified.dimensionality)
        if unit_str in pq.preferred and unit_str is not None:
            new_units = pq.preferred[unit_str]
            return self.rescale(new_units)

such that, if not specified, my desired units are looked up in pq.preferred, where I can keep my preferred set of units project-wide. I think it is simplest do this in rescale but if there are serious concerns about mucking with the signature of rescale then I could implement it in a new method rescale_preferred.

Thoughts?

Activity

  1. rgerkin commented on Jan 21, 2020

    @rgerkin
    ContributorAuthor

    I just discovered set_default_units in unit_quantity.py. This acts on the simplified property, but that might be a good enough.

  2. rgerkin commented on Jan 21, 2020

    @rgerkin
    ContributorAuthor

    OK, now I see that there are only a limited set of fundamental dimensions to pick from (currency, current, temperature, etc.) so I cannot set default voltage or charge units, for example. And I don't think I can extend unit_quantity.py to include those (UnitVoltage, etc.) and update set_default_units to include them as keyword arguments, because then there could be conflicts for units which are derivable from each other (e.g. the preferred units for time and charge may be different than for current, which would cause a conflict). So I think there is still a need for a default rescaling option that can support arbitrary units including compound units.

  3. rgerkin commented on Jan 28, 2020

    @rgerkin
    ContributorAuthor

    Implemented in #173. See demo here. Note that errors in the notebook are raised deliberately to illustrate expected failures.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions