def testCollectionQuantityOperations(self): """ Tests that Quantity collections behave correctly """ # Tests that __getitem__ returns a unit s = [1, 2, 3, 4] * u.angstroms self.assertTrue(u.is_quantity(s[0])) for i, val in enumerate(s): self.assertTrue(u.is_quantity(val)) self.assertEqual(val, (i+1) * u.angstroms) # Tests that __setitem__ fails when an incompatible type is added def fail(s): s[0] = 5 self.assertRaises(AttributeError, lambda: fail(s)) def fail(s): s[0] = 5 * u.joules self.assertRaises(TypeError, lambda: fail(s)) def fail(s): s[0] /= 10 * u.meters self.assertRaises(AttributeError, lambda: fail(s)) # Tests that __setitem__ converts to the unit of the container s[0] = 1 * u.nanometers self.assertEqual(s[0]._value, 10) # Tests standard unit conversions x = [1, 2, 3] * u.centimeters self.assertEqual(x / u.millimeters, [10, 20, 30]) # Test the construction of a container in which each element is a # Quantity, passed to the Quantity constructor x = u.Quantity([1*u.angstrom, 2*u.nanometer, 3*u.angstrom]) self.assertEqual(x._value, [1, 20, 3]) self.assertEqual(x.unit, u.angstrom) x = u.Quantity((1, 2, 3)) self.assertTrue(u.is_quantity(x)) self.assertTrue(x.unit.is_dimensionless()) x = u.Quantity(([1*u.angstrom, 2*u.nanometer, 3*u.angstrom], [1*u.angstrom, 4*u.nanometer, 3*u.angstrom])) self.assertEqual(x._value, ([1, 20, 3], [1, 40, 3])) self.assertEqual(x.unit, u.angstrom) self.assertTrue(u.is_quantity(u.Quantity([])))
def testScalarQuantityConstructor(self): """ Tests creating a Quantity using the Quantity constructor """ self.assertTrue(u.is_quantity(u.Quantity(5, u.centimeters))) self.assertTrue(u.is_quantity(u.Quantity(5, u.centimeters**-1))) x = u.Quantity(value=5.0, unit=100.0*u.meters) self.assertTrue(u.is_quantity(x)) self.assertEqual(x, 500*u.meters)
def testNumpyDivision(self): """ Tests that division of numpy Quantities works correctly """ x = u.Quantity(np.asarray([1., 2.]), u.nanometers) y = u.Quantity(np.asarray([3., 4.]), u.picoseconds) xy = x / y self.assertTrue(u.is_quantity(xy)) self.assertEqual(xy.unit, u.nanometers/u.picoseconds) self.assertEqual(xy[0].value_in_unit(u.nanometers/u.picoseconds), 1/3) self.assertEqual(xy[1].value_in_unit(u.nanometers/u.picoseconds), 0.5)
def testNumpyDeepCopy(self): """ Check that deepcopy on numpy array does not strip units """ x = u.Quantity(np.zeros((2, 3)), u.nanometer) y = copy.deepcopy(x) self.assertTrue(np.all(x == y)) self.assertTrue(u.is_quantity(x)) self.assertTrue(u.is_quantity(y))
def testDimensionless(self): """ Tests the properties of unit.dimensionless """ x = 5 * u.dimensionless y = u.Quantity(5, u.dimensionless) self.assertTrue(u.is_quantity(x)) self.assertTrue(u.is_quantity(y)) self.assertNotEqual(x, 5) self.assertNotEqual(y, 5) self.assertEqual(x, y) self.assertEqual(x.value_in_unit_system(u.si_unit_system), 5) self.assertEqual(x.value_in_unit_system(u.cgs_unit_system), 5) self.assertEqual(x.value_in_unit_system(u.md_unit_system), 5) x = u.Quantity(1.0, u.dimensionless) y = u.Quantity(1.0, u.dimensionless) self.assertIsNot(x, y) self.assertEqual(x, y)
def testReduceUnit(self): """ Tests the reduce_unit functionality """ x = u.nanometer**2 / u.angstrom**2 self.assertEqual(str(x), 'nanometer**2/(angstrom**2)') self.assertTrue(x.is_dimensionless()) q = u.Quantity(2.0, x) self.assertEqual(str(q), '2.0 nm**2/(A**2)') self.assertEqual(q.reduce_unit(), 200)
def testMutableQuantityOperations(self): " Tests that mutable Quantity objects do not get unexpectedly changed " # This used to be a bug -- t and s._value were the same object, so # changing t would also change s silently s = [1, 2, 3, 4] * u.angstroms t = s / u.angstroms self.assertEqual(t, [1, 2, 3, 4]) self.assertEqual(s, u.Quantity([1, 2, 3, 4], u.angstroms)) t[0] = 2 self.assertEqual(t, [2, 2, 3, 4]) if not utils.openmm_version or utils.openmm_version > (6, 2): self.assertEqual(s, u.Quantity([1, 2, 3, 4], u.angstroms)) else: t[0] = 1 t = s.value_in_unit(u.angstroms) self.assertEqual(t, [1, 2, 3, 4]) self.assertEqual(s, u.Quantity([1, 2, 3, 4], u.angstroms)) t[0] = 2 self.assertEqual(t, [2, 2, 3, 4]) self.assertEqual(s, u.Quantity([1, 2, 3, 4], u.angstroms)) s = [1, 2, 3, 4] * u.nanometers self.assertEqual(s, u.Quantity([1, 2, 3, 4], u.nanometers)) t = s.in_units_of(u.nanometers) self.assertEqual(s, t) t[0] = 1 * u.meters self.assertAlmostEqualQuantities(t, u.Quantity([1e9, 2, 3, 4], u.nanometers)) self.assertEqual(s, u.Quantity([1, 2, 3, 4], u.nanometers))
def testNumpyFunctions(self): """ Tests various numpy attributes that they result in Quantities """ a = u.Quantity(np.arange(10), u.seconds) self.assertEqual(a.max(), 9*u.seconds) self.assertEqual(a.min(), 0*u.seconds) self.assertEqual(a.mean(), 4.5*u.seconds) self.assertAlmostEqualQuantities(a.std(), 2.8722813232690143*u.seconds) b = a.reshape((5, 2)) self.assertTrue(u.is_quantity(b))
def testQuantityMaths(self): """ Tests dimensional analysis & maths on and b/w Quantity objects """ x = 1.3 * u.meters y = 75.2 * u.centimeters self.assertEqual((x + y) / u.meters, 2.052) self.assertEqual((x - y) / u.meters, 0.548) self.assertEqual(x / y, 1.3 / 0.752) self.assertEqual(x * y, 1.3*0.752*u.meters**2) d1 = 2.0*u.meters d2 = 2.0*u.nanometers self.assertEqual(d1 + d2, (2+2e-9)*u.meters) self.assertAlmostEqual((d2+d1-(2e9+2)*u.nanometers)._value, 0, places=6) self.assertEqual(d1 + d1, 4.0*u.meters) self.assertEqual(d1 - d1, 0.0*u.meters) self.assertEqual(d1 / d1, 1.0) self.assertEqual(d1 * u.meters, 2.0*u.meters**2) self.assertEqual(u.kilograms*(d1/u.seconds)*(d1/u.seconds), 4*u.kilograms*u.meters**2/u.seconds**2) self.assertEqual(u.kilograms*(d1/u.seconds)**2, 4*u.kilograms*u.meters**2/u.seconds**2) self.assertEqual(d1**3, 8.0*u.meters**3) x = d1**(3/2) self.assertAlmostEqual(x._value, math.sqrt(2)**3) self.assertEqual(x.unit, u.meters**(3/2)) self.assertAlmostEqual((d1**0.5)._value, math.sqrt(2)) self.assertEqual((d1**0.5).unit, u.meters**0.5) comp = (3.0 + 4.0j) * u.meters self.assertTrue(u.is_quantity(comp)) self.assertEqual(comp.unit, u.meters) self.assertEqual(str(comp), '(3+4j) m') self.assertEqual(comp + comp, (6.0 + 8.0j)*u.meters) self.assertEqual(comp - comp, 0*u.meters) self.assertEqual(comp * comp, (3.0 + 4.0j)**2 * u.meters**2) self.assertAlmostEqual(abs(comp / comp), 1) self.assertAlmostEqual(1.5*u.nanometers / u.meters, 1.5e-9, places=15) self.assertEqual((2.3*u.meters)**2, 2.3**2*u.meters**2) x = 4.3 * u.meters self.assertEqual(x / u.centimeters, 430) self.assertEqual(str(x / u.seconds), '4.3 m/s') self.assertEqual(str(8.4 / (4.2*u.centimeters)), '2.0 /cm') x = 1.2 * u.meters self.assertEqual(x * 5, u.Quantity(6.0, u.meters))
def testNumpyQuantity(self): """ Tests that numpy arrays can form Quantity values """ q = u.Quantity(np.array([1, 2, 3]), u.centimeters) self.assertTrue(u.is_quantity(q)) self.assertIsInstance(q._value, np.ndarray) self.assertTrue(np.all(q / u.millimeters == np.array([1, 2, 3])*10))
def testString(self): """ Tests unit handling with strings, which should be dimensionless """ s = u.Quantity("string") self.assertEqual(s.value_in_unit_system(u.md_unit_system), "string")
def testUnaryOperators(self): """ Tests unary operators on units """ self.assertEqual(-(2.3*u.meters), u.Quantity(-2.3, u.meters)) self.assertEqual(-(2.3*u.meters), -u.Quantity(2.3, u.meters)) self.assertEqual(+(2.3*u.meters), u.Quantity(2.3, u.meters)) self.assertEqual(2.3*u.meters, +u.Quantity(2.3, u.meters))