Skip to content

Negative UncertainQuantity messes up dimensionality #115

Closed

Description

@bjodah

Consider this:

In [1]: import quantities as pq

In [2]: s = pq.UncertainQuantity(3, pq.m, 2)

In [3]: s.rescale(pq.km)
Out[3]: UncertainQuantity(array(0.003), km, array(0.002))

In [4]: s2 = pq.UncertainQuantity(-3, pq.m, 2)

In [5]: s2.rescale(pq.km)
Out[5]: UncertainQuantity(array(-0.003), km, array(0.002))

In [6]: (-s2).rescale(pq.km)
---------------------------------------------------------------------------
AssertionError                            Traceback (most recent call last)
/home/bjorn/.local/lib/python3.4/site-packages/quantities/quantity.py in rescale(self, units)
    191         try:
--> 192             cf = get_conversion_factor(from_u, to_u)
    193         except AssertionError:

/home/bjorn/.local/lib/python3.4/site-packages/quantities/quantity.py in get_conversion_factor(from_u, to_u)
     52     to_u = to_u._reference
---> 53     assert from_u.dimensionality == to_u.dimensionality
     54     return from_u.magnitude / to_u.magnitude

AssertionError: 

During handling of the above exception, another exception occurred:

ValueError                                Traceback (most recent call last)
<ipython-input-6-cbe519da7162> in <module>()
----> 1 (-s2).rescale(pq.km)

/home/bjorn/.local/lib/python3.4/site-packages/quantities/uncertainquantity.py in rescale(self, units)
     72         cls = UncertainQuantity
     73         ret = super(cls, self).rescale(units).view(cls)
---> 74         ret.uncertainty = self.uncertainty.rescale(units)
     75         return ret
     76 

/home/bjorn/.local/lib/python3.4/site-packages/quantities/quantity.py in rescale(self, units)
    194             raise ValueError(
    195                 'Unable to convert between units of "%s" and "%s"'
--> 196                 %(from_u._dimensionality, to_u._dimensionality)
    197             )
    198         return Quantity(cf*self.magnitude, to_u)

ValueError: Unable to convert between units of "dimensionless" and "km"

Activity

  1. bjodah commented on May 27, 2016

    @bjodah
    ContributorAuthor

    Seems to be related to __neg__:

    In [9]: -s
    Out[9]: UncertainQuantity(array(-3.0), m, array(0.0))
    
    In [10]: s*-1
    Out[10]: UncertainQuantity(array(-3.0), m, array(2.0))
    
  2. added a commit that references this issue on May 27, 2016
    dcbb872
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