def test_multiplier(self): # Test :meth:`Unit.multiplier()` and :meth:`Unit.multiplier_log_10`. d = myokit.Unit() d *= myokit.units.meter d *= 1000 self.assertEqual(d.multiplier(), 1000) self.assertEqual(d.multiplier_log_10(), 3)
def test_operators(self): # Test overloaded unit operators. # Test div d = myokit.Unit() self.assertEqual(d.exponents(), [0] * 7) d = d / myokit.units.m self.assertEqual(d.exponents(), [0, -1, 0, 0, 0, 0, 0]) d = d / myokit.units.m self.assertEqual(d.exponents(), [0, -2, 0, 0, 0, 0, 0]) d = d / d self.assertEqual(d.exponents(), [0, 0, 0, 0, 0, 0, 0]) # Test mul d = myokit.Unit() self.assertEqual(d.exponents(), [0] * 7) d = d * myokit.units.s self.assertEqual(d.exponents(), [0, 0, 1, 0, 0, 0, 0]) d = d * myokit.units.m self.assertEqual(d.exponents(), [0, 1, 1, 0, 0, 0, 0]) d = d * d self.assertEqual(d.exponents(), [0, 2, 2, 0, 0, 0, 0]) # Test pow d = myokit.Unit() self.assertEqual(d.exponents(), [0] * 7) d *= myokit.units.s d *= myokit.units.m self.assertEqual(d.exponents(), [0, 1, 1, 0, 0, 0, 0]) d = d**3 self.assertEqual(d.exponents(), [0, 3, 3, 0, 0, 0, 0]) # Test rdiv and rmul (i.e. with non-units) d = myokit.Unit() d *= myokit.units.meter self.assertEqual(d._m, 0) self.assertEqual(d.exponents(), [0, 1, 0, 0, 0, 0, 0]) d = 1000 * d self.assertEqual(d._m, 3) d = 1 / d self.assertEqual(d._m, -3) self.assertEqual(d.exponents(), [0, -1, 0, 0, 0, 0, 0]) d = 100 * d self.assertEqual(d._m, -1) d = 10 * d self.assertEqual(d._m, 0)
def test_exponents(self): # Test Unit.exponents x = myokit.Unit() self.assertEqual(x.exponents(), [0] * 7) x = myokit.units.m**-2 self.assertEqual(x.exponents(), [0, -2, 0, 0, 0, 0, 0])
def test_float(self): # Test :meth:`Unit.__float__()`. x = myokit.Unit() x *= 123 self.assertAlmostEqual(float(x), 123) # Can't convert unless dimensionless (but with any multiplier) x *= myokit.units.V self.assertRaises(TypeError, float, x)
def test_create(self): # Test basic unit creation. myokit.Unit.parse_simple('mV') myokit.Unit.parse_simple('g') myokit.Unit.parse_simple('kg') myokit.Unit([0, 0, 0, 0, 0, 0, 0]) self.assertRaises(ValueError, myokit.Unit, [0, 0, 0, 0, 0, 0]) self.assertRaises(ValueError, myokit.Unit, [0, 0, 0, 0, 0, 0, 0, 0])
def test_creation_and_str(self): # Test Quanity creation and :meth:`Quantity.__str__()`. from myokit import Quantity as Q # Creation and string representation a = Q('10 [mV]') self.assertEqual(float(a), 10) self.assertEqual(str(a), '10.0 [mV]') a = Q('2', myokit.units.uA) self.assertEqual(float(a), 2) self.assertEqual(str(a), '2.0 [uA]') a = Q(-2, 'uA/cm^2') self.assertEqual(float(a), -2) self.assertEqual(str(a), '-2.0 [uA/cm^2]') a = Q(3.0e1, 'km/N/g') self.assertEqual(float(a), 30) self.assertEqual(str(a), '30.0 [s^2/g^2]') a = Q(4) self.assertEqual(float(a), 4) self.assertEqual(str(a), '4.0 [1]') # Repr self.assertEqual(repr(a), '4.0 [1]') # Creation from a myokit Number x = myokit.Number(2) a = Q(x) self.assertRaisesRegex(ValueError, 'Cannot specify', Q, x, myokit.Unit()) # Creation with string value x = myokit.Quantity('2') x = myokit.Quantity('2 [mV]') self.assertRaisesRegex(ValueError, 'could not be converted', myokit.Quantity, int) self.assertRaisesRegex(ValueError, 'Failed to parse', myokit.Quantity, 'wolf') self.assertRaisesRegex(ValueError, 'Failed to parse', myokit.Quantity, 'a [mV]') x = myokit.Quantity('2', 'mV') self.assertRaisesRegex(ValueError, 'Two units', myokit.Quantity, '2 [mV]', 'mV') # Test hash self.assertEqual(str(x), x.__hash__())
def test_str(self): # Test :meth:`Unit.str()` # Unit with representation in alternative base km_per_s = myokit.Unit([0, 1, -1, 0, 0, 0, 0], 3) # Myokit doesn't know km/s, it does know m/s so this should become: self.assertEqual(str(km_per_s), '[m/s (1000)]') # Myokit doesn't know MA/m^2 mam2 = myokit.parse_unit('MA/m^2') self.assertEqual(str(mam2), '[A/m^2 (1e+06)]') # Simple units m = myokit.parse_unit('m') self.assertEqual(str(m), '[m]') im = 1 / m self.assertEqual(str(im), '[1/m]') # Predefined complex unit self.assertEqual(str(myokit.units.N), '[N]') # Low mulipliers um = m * 1e-6 self.assertEqual(str(um), '[um]') um3 = myokit.parse_unit('um^3') self.assertEqual(str(um3), '[um^3]') attomol = myokit.parse_unit('amol') self.assertEqual(str(attomol), '[mol (1e-18)]') # High multipliers nn = myokit.units.N * 1e-2 self.assertEqual(str(nn), '[N (0.01)]') nn = myokit.units.N * 1e2 self.assertEqual(str(nn), '[N (100)]') nn = myokit.units.N * 1e7 self.assertEqual(str(nn), '[N (1e+07)]') # Unit very similar to a known unit c = myokit.units.V d = c * 1.000000001 self.assertFalse(c == d) self.assertTrue(myokit.Unit.close(c, d)) self.assertEqual(str(c), '[V]') self.assertEqual(str(d), '[V]')
unit system. """ globals()[name] = unit myokit.Unit.register(name, unit, quantifiable, output) def _rep(name): unit = myokit.parse_unit(name) myokit.Unit.register_preferred_representation(name, unit) # # SI myokit.Units # # Dimensionless _add('dimensionless', myokit.Unit()) # Angles _add('rad', dimensionless) _add('radian', dimensionless) _add('sr', dimensionless) _add('steradian', dimensionless) # Basic SI units _add('kg', myokit.Unit([1, 0, 0, 0, 0, 0, 0], 3), quantifiable=False, output=True) _add('m', myokit.Unit([0, 1, 0, 0, 0, 0, 0]), quantifiable=True, output=True) _add('s', myokit.Unit([0, 0, 1, 0, 0, 0, 0]), quantifiable=True, output=True) _add('A', myokit.Unit([0, 0, 0, 1, 0, 0, 0]), quantifiable=True, output=True) _add('K', myokit.Unit([0, 0, 0, 0, 1, 0, 0]), quantifiable=True, output=True) _add('cd', myokit.Unit([0, 0, 0, 0, 0, 1, 0]), quantifiable=True, output=True)
def get_myokit_unit(self): return myokit.Unit( exponents=[self.g, self.m, self.s, self.A, self.K, self.cd, self.mol], multiplier=self.multiplier )
def load_units(apps, schema_editor): Unit = apps.get_model("pkpdapp", "Unit") m = myokit.Unit.parse_simple('m') L = myokit.Unit.parse_simple('L') cL = myokit.Unit.parse_simple('cL') h = myokit.Unit.parse_simple('h') g = myokit.Unit.parse_simple('g') dimensionless = myokit.Unit() units = [ { 'symbol': 'h', 'unit': h, }, { 'symbol': 'mg', 'unit': 1e-3 * g, }, { 'symbol': 'd', 'unit': 24 * h, }, { 'symbol': '1/d', 'unit': 1 / (24 * h), }, { 'symbol': '1/h', 'unit': 1 / h, }, { 'symbol': 'L/mg/d', 'unit': L / (1e-3 * g * 24 * h), }, { 'symbol': 'L', 'unit': L }, { 'symbol': 'L/h', 'unit': L / h }, { 'symbol': '1/L', 'unit': 1 / L }, { 'symbol': 'cm^3', 'unit': (1e-2 * m)**3, }, { 'symbol': 'cm^3/d', 'unit': (1e-2 * m)**3 / (24 * h), }, { 'symbol': 'g', 'unit': g, }, { 'symbol': 'ng', 'unit': 1e-9 * g, }, { 'symbol': 'ng/mL', 'unit': 1e-9 * g / (1e-3 * L), }, { 'symbol': 'mg/L', 'unit': 1e-3 * g / L, }, { 'symbol': 'ng/L', 'unit': 1e-9 * g / L, }, { 'symbol': 'g/L', 'unit': g / L, }, { 'symbol': '10^6/mcL', 'unit': 1e6 / (1e-3 * cL), }, { 'symbol': '10^3/mcL', 'unit': 1e3 / (1e-3 * cL), }, { 'symbol': 'g/dL', 'unit': g / (10 * cL), }, { 'symbol': '', 'unit': dimensionless, }, ] for u in units: Unit.objects.create( symbol=u['symbol'], g=u['unit'].exponents()[0], m=u['unit'].exponents()[1], s=u['unit'].exponents()[2], A=u['unit'].exponents()[3], K=u['unit'].exponents()[4], cd=u['unit'].exponents()[5], mol=u['unit'].exponents()[6], multiplier=u['unit'].multiplier_log_10(), )
def test_register_errors(self): # Test errors for Unit.register (rest is already used a lot). self.assertRaises(TypeError, myokit.Unit.register, 4, myokit.Unit()) self.assertRaises(TypeError, myokit.Unit.register, 'hi', 4)