Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge pull request #3 from enthought/offset_units_patch
patch from user John Lund - include offset functionality in all units by default. deprecate use of scimath.units.smart_unit.OffsetUnit
  • Loading branch information
timdiller committed Mar 28, 2013
2 parents 22deb4e + 860880c commit d163cc3
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 18 deletions.
4 changes: 2 additions & 2 deletions scimath/units/has_units.py
Expand Up @@ -145,9 +145,9 @@ def has_units(func=None, summary='', doc='', inputs=None, outputs=None):
>>> from scimath.units.length import m
>>> a = UnitArray((1,2,3), units=m)
>>> add(a,a) # (Converts m -> ft -> m)
UnitArray([ 2., 4., 6.], units='1.0*m')
UnitArray([ 2., 4., 6.], units='1.0*m+0.0')
>>> add(a,a).units
1.0*m
1.0*m+0.0
Alternatively, parameter information can be specified in the decorator:
Expand Down
4 changes: 2 additions & 2 deletions scimath/units/pressure.py
Expand Up @@ -14,7 +14,7 @@
from SI import pascal, kilo, mega, giga
from scimath.units.force import lbf
from scimath.units.length import inch

from scimath.units.unit import unit
#
# Definitions of common pressure units
#
Expand Down Expand Up @@ -70,7 +70,7 @@
pounds_per_square_inch.label = 'psi'
psi = pounds_per_square_inch
apsi = psi
psig = psi
psig = unit(psi.value, psi.derivation, 14.6959494)


# version
Expand Down
5 changes: 4 additions & 1 deletion scimath/units/smart_unit.py
Expand Up @@ -19,7 +19,7 @@
#############################################################################
# Imports:
#############################################################################

import warnings
import numpy

# Local imports.
Expand All @@ -32,6 +32,9 @@ class OffsetUnit(unit):
""" Special unit to handle temperatures as absolutes--including offsets """

def __init__(self,factor,derivation,offset=0.0):
warnings.warn("Using the OffsetUnit class is not recommended as its offset"
" attribute is now available on the more general parent "
"class: scimath.units.unit.unit.")
unit.__init__(self,factor,derivation)
self.offset = offset

Expand Down
10 changes: 5 additions & 5 deletions scimath/units/temperature.py
Expand Up @@ -12,19 +12,19 @@
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#

from scimath.units.smart_unit import OffsetUnit
from scimath.units.unit import unit

# Tk = Tk
# Tk = 1 * (Tc+273.15)
# Tk = 5/9 * (Tr)
# Tk = 5/9 * (Tf + 459.67)

kelvin = OffsetUnit(1.0, (0, 0, 0, 0, 1, 0, 0), 0.0)
kelvin = unit(1.0, (0, 0, 0, 0, 1, 0, 0), 0.0)
kelvin.label = 'kelvin'
celsius = OffsetUnit(1.0, (0, 0, 0, 0, 1, 0, 0), 273.15)
celsius = unit(1.0, (0, 0, 0, 0, 1, 0, 0), 273.15)
celsius.label = 'celsius'
rankine = OffsetUnit(5.0/9.0, (0, 0, 0, 0, 1, 0, 0), 0.0)
fahrenheit = OffsetUnit(5.0/9.0,(0, 0, 0, 0, 1, 0, 0), 459.67)
rankine = unit(5.0/9.0, (0, 0, 0, 0, 1, 0, 0), 0.0)
fahrenheit = unit(5.0/9.0,(0, 0, 0, 0, 1, 0, 0), 459.67)
fahrenheit.label = 'fahrenheit'

# aliases
Expand Down
20 changes: 19 additions & 1 deletion scimath/units/tests/unit_scalar_test_case.py
@@ -1,11 +1,29 @@
import unittest

from traits.testing.api import doctest_for_module

import scimath.units.unit_scalar as unit_scalar
from scimath.units.unit_scalar import UnitScalar
from scimath.units.length import m
from scimath.units.unit import unit


from nose.tools import assert_equal, assert_is_instance

class UnitScalarDocTestCase(doctest_for_module(unit_scalar)):
pass


def test_offset_unit_computations():
""" Executing some basic computations with a basic custom unit with offset.
"""
my_u = unit(12, m.derivation, 14)
s1 = UnitScalar(3, units=my_u)
s2 = UnitScalar(5, units=my_u)
s3 = s1+s2
assert_equal(s3,UnitScalar(8, units=my_u))



if __name__ == '__main__':
import sys
unittest.main(argv=sys.argv)
15 changes: 10 additions & 5 deletions scimath/units/unit.py
Expand Up @@ -28,10 +28,11 @@ class unit(object):
_negativeOne = (-1, ) * len(_labels)


def __init__(self, value, derivation):
def __init__(self, value, derivation, offset=0.0):
self.value = value
self.derivation = derivation
self.label = None
self.offset = offset
return

def __eq__(self, other):
Expand All @@ -43,7 +44,9 @@ def __eq__(self, other):
if not isinstance(other, unit):
return False

return self.value == other.value and self.derivation == other.derivation
return self.value == other.value and \
self.derivation == other.derivation and \
self.offset == other.offset


def __ne__(self, other):
Expand All @@ -54,7 +57,9 @@ def __ne__(self, other):
if not isinstance(other, unit):
return True

return self.value != other.value or self.derivation != other.derivation
return self.value != other.value or \
self.derivation != other.derivation or\
self.offset != other.offset


def __add__(self, other):
Expand Down Expand Up @@ -155,7 +160,7 @@ def __str__(self):
if not derivation:
return st

return st + "*" + derivation
return st + "*" + derivation + "+" + str(self.offset)

# TODO: something's broken here, I think...perhaps a rename is needed for
# _strDerivation either here or in the helper function.
Expand All @@ -170,7 +175,7 @@ def __repr__(self):
if not derivation:
return st

return st + "*" + derivation
return st + "*" + derivation + "+" + repr(self.offset)

def _strDerivation(self):
return _strDerivation(self._labels, self.derivation)
Expand Down
4 changes: 2 additions & 2 deletions scimath/units/unit_scalar.py
Expand Up @@ -12,9 +12,9 @@ class UnitScalar(UnitArray):
>>> from scimath.units.length import cm
>>> x = UnitScalar(5, units=cm)
>>> x, x.units
(UnitScalar(5, units='0.01*m'), 0.01*m)
(UnitScalar(5, units='0.01*m+0.0'), 0.01*m+0.0)
>>> x**2, (x**2).units
(UnitScalar(25, units='0.0001*m**2'), 0.0001*m**2)
(UnitScalar(25, units='0.0001*m**2+0.0'), 0.0001*m**2+0.0)
'''
def __repr__(self):
return ("UnitScalar(%s, units='%s')"
Expand Down

0 comments on commit d163cc3

Please sign in to comment.