Skip to content

Commit

Permalink
Revamp the way ScalarQuantities store and process uncertainties.
Browse files Browse the repository at this point in the history
In commit 851b2ba it was changed so that
Quantitiy.value and Quantity.uncertainty were now in customizable units,
and Quantitiy.value_si was the SI value. However, there was no uncertainty_si.

Now the uncertainty is also stored in SI units, like the value, and converted
to output units on demand. This change should be transparent, i.e. not change 
anything that was correct before, but it does create a new Quantity.uncertainty_si 
property, which is what we should have been using instead of .uncertainty in 
various places. i.e. we now need to go around using this to fix other bugs,
such as @GreenGroup/RMG-databaseReactionMechanismGenerator#21 ReactionMechanismGenerator/RMG-database#21

Also to do: do the same thing for the ArrayQuantity class.
  • Loading branch information
rwest committed Aug 29, 2013
1 parent edefda3 commit f7beb20
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 4 deletions.
7 changes: 5 additions & 2 deletions rmgpy/quantity.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,11 @@ cdef class Units(object):
cdef class ScalarQuantity(Units):

cdef public double value_si
cdef public str uncertaintyType
cdef public double uncertainty
cdef str _uncertaintyType
cdef public double uncertainty_si

cpdef str getUncertaintyType(self)
cpdef setUncertaintyType(self, str v)

cpdef bint isUncertaintyAdditive(self) except -2

Expand Down
42 changes: 40 additions & 2 deletions rmgpy/quantity.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,11 +177,49 @@ def copy(self):
return ScalarQuantity(self.value, self.units, self.uncertainty, self.uncertaintyType)

def getValue(self):
"""
The numeric value of the quantity, in the given units
"""
return self.value_si * self.getConversionFactorFromSI()
def setValue(self, v):
self.value_si = float(v) * self.getConversionFactorToSI()
value = property(getValue, setValue)

def getUncertainty(self):
"""
The numeric value of the uncertainty, in the given units if additive, or no units if multiplicative.
"""
if self.isUncertaintyAdditive():
return self.uncertainty_si * self.getConversionFactorFromSI()
else:
return self.uncertainty_si
def setUncertainty(self, v):
if self.isUncertaintyAdditive():
self.uncertainty_si = float(v) * self.getConversionFactorToSI()
else:
self.uncertainty_si = float(v)
uncertainty = property(getUncertainty, setUncertainty)

def getUncertaintyType(self):
"""
The type of uncertainty: ``'+|-'`` for additive, ``'*|/'`` for multiplicative
"""
return self._uncertaintyType
def setUncertaintyType(self, v):
"""
Check the uncertainty type is valid, then set it, and set the uncertainty to -1.
If you set the uncertainty then change the type, we have no idea what to do with
the units. This ensures you set the type first.
"""
if v not in ['+|-','*|/']:
raise QuantityError("Invalid uncertainty type")
self._uncertaintyType = v
self.uncertainty_si = -1
uncertaintyType = property(getUncertaintyType, setUncertaintyType)



def equals(self, quantity):
"""
Return ``True`` if the everything in a quantity object matches
Expand Down Expand Up @@ -224,14 +262,14 @@ def isUncertaintyAdditive(self):
Return ``True`` if the uncertainty is specified in additive format
and ``False`` otherwise.
"""
return self.uncertaintyType == '+|-'
return self._uncertaintyType == '+|-'

def isUncertaintyMultiplicative(self):
"""
Return ``True`` if the uncertainty is specified in multiplicative
format and ``False`` otherwise.
"""
return self.uncertaintyType == '*|/'
return self._uncertaintyType == '*|/'

################################################################################

Expand Down

0 comments on commit f7beb20

Please sign in to comment.