def test_textile(self): ureg = self.ureg qty_direct = 1.331 * ureg.tex with self.assertRaises(DimensionalityError): qty_indirect = qty_direct.to("Nm") with ureg.context("textile"): from pint.util import find_shortest_path qty_indirect = qty_direct.to("Nm") a = qty_direct.to_base_units() b = qty_indirect.to_base_units() da, db = Context.__keytransform__(a.dimensionality, b.dimensionality) p = find_shortest_path(ureg._active_ctx.graph, da, db) self.assertTrue(p) msg = "{} <-> {}".format(a, b) self.assertQuantityAlmostEqual(b, a, rtol=0.01, msg=msg) # Check RKM <-> cN/tex conversion self.assertQuantityAlmostEqual(1 * ureg.RKM, 0.980665 * ureg.cN / ureg.tex) self.assertQuantityAlmostEqual((1 / 0.980665) * ureg.RKM, 1 * ureg.cN / ureg.tex) self.assertAlmostEqual((1 * ureg.RKM).to(ureg.cN / ureg.tex).m, 0.980665) self.assertAlmostEqual((1 * ureg.cN / ureg.tex).to(ureg.RKM).m, 1 / 0.980665)
def test_textile(self): ureg = self.ureg qty_direct = 1.331 * ureg.tex with pytest.raises(DimensionalityError): qty_indirect = qty_direct.to("Nm") with ureg.context("textile"): from pint.util import find_shortest_path qty_indirect = qty_direct.to("Nm") a = qty_direct.to_base_units() b = qty_indirect.to_base_units() da, db = Context.__keytransform__(a.dimensionality, b.dimensionality) p = find_shortest_path(ureg._active_ctx.graph, da, db) assert p msg = "{} <-> {}".format(a, b) helpers.assert_quantity_almost_equal(b, a, rtol=0.01, msg=msg) # Check RKM <-> cN/tex conversion helpers.assert_quantity_almost_equal(1 * ureg.RKM, 0.980665 * ureg.cN / ureg.tex) helpers.assert_quantity_almost_equal((1 / 0.980665) * ureg.RKM, 1 * ureg.cN / ureg.tex) assert (round( abs((1 * ureg.RKM).to(ureg.cN / ureg.tex).m - 0.980665), 7) == 0) assert (round( abs((1 * ureg.cN / ureg.tex).to(ureg.RKM).m - 1 / 0.980665), 7) == 0)
def test_spectroscopy(self): ureg = self.ureg eq = (532.0 * ureg.nm, 563.5 * ureg.terahertz, 2.33053 * ureg.eV) with ureg.context("sp"): from pint.util import find_shortest_path for a, b in itertools.product(eq, eq): for x in range(2): if x == 1: a = a.to_base_units() b = b.to_base_units() da, db = Context.__keytransform__(a.dimensionality, b.dimensionality) p = find_shortest_path(ureg._active_ctx.graph, da, db) assert p msg = "{} <-> {}".format(a, b) # assertAlmostEqualRelError converts second to first helpers.assert_quantity_almost_equal(b, a, rtol=0.01, msg=msg) for a, b in itertools.product(eq, eq): helpers.assert_quantity_almost_equal(a.to(b.units, "sp"), b, rtol=0.01)
def test_shortest_path(self): g = collections.defaultdict(list) g[1] = { 2, } g[2] = { 3, } p = find_shortest_path(g, 1, 2) self.assertEqual(p, [1, 2]) p = find_shortest_path(g, 1, 3) self.assertEqual(p, [1, 2, 3]) p = find_shortest_path(g, 3, 1) self.assertIs(p, None) g = collections.defaultdict(list) g[1] = { 2, } g[2] = {3, 1} g[3] = { 2, } p = find_shortest_path(g, 1, 2) self.assertEqual(p, [1, 2]) p = find_shortest_path(g, 1, 3) self.assertEqual(p, [1, 2, 3]) p = find_shortest_path(g, 3, 1) self.assertEqual(p, [3, 2, 1]) p = find_shortest_path(g, 2, 1) self.assertEqual(p, [2, 1])
def test_textile(self): ureg = self.ureg qty_direct = 1.331 * ureg.tex with self.assertRaises(errors.DimensionalityError): qty_indirect = qty_direct.to('Nm') with ureg.context('textile'): from pint.util import find_shortest_path qty_indirect = qty_direct.to('Nm') a = qty_direct.to_base_units() b = qty_indirect.to_base_units() da, db = Context.__keytransform__(a.dimensionality, b.dimensionality) p = find_shortest_path(ureg._active_ctx.graph, da, db) self.assertTrue(p) msg = '{} <-> {}'.format(a, b) self.assertQuantityAlmostEqual(b, a, rtol=0.01, msg=msg)
def test_spectroscopy(self): ureg = self.ureg eq = (532.0 * ureg.nm, 563.5 * ureg.terahertz, 2.33053 * ureg.eV) with ureg.context("sp"): from pint.util import find_shortest_path for a, b in itertools.product(eq, eq): for x in range(2): if x == 1: a = a.to_base_units() b = b.to_base_units() da, db = Context.__keytransform__(a.dimensionality, b.dimensionality) p = find_shortest_path(ureg._active_ctx.graph, da, db) self.assertTrue(p) msg = "{0} <-> {1}".format(a, b) # assertAlmostEqualRelError converts second to first self.assertQuantityAlmostEqual(b, a, rtol=0.01, msg=msg) for a, b in itertools.product(eq, eq): self.assertQuantityAlmostEqual(a.to(b.units, "sp"), b, rtol=0.01)
def test_spectroscopy(self): ureg = self.ureg eq = (532. * ureg.nm, 563.5 * ureg.terahertz, 2.33053 * ureg.eV) with ureg.context('sp'): from pint.util import find_shortest_path for a, b in itertools.product(eq, eq): for x in range(2): if x == 1: a = a.to_base_units() b = b.to_base_units() da, db = Context.__keytransform__(a.dimensionality, b.dimensionality) p = find_shortest_path(ureg._active_ctx.graph, da, db) self.assertTrue(p) msg = '{} <-> {}'.format(a, b) # assertAlmostEqualRelError converts second to first self.assertAlmostEqualRelError(b, a, rel=.01, msg=msg) for a, b in itertools.product(eq, eq): self.assertAlmostEqualRelError(a.to(b.units, 'sp'), b, rel=.01, msg=msg)
def test_shortest_path(self): g = collections.defaultdict(list) g[1] = {2,} g[2] = {3,} p = find_shortest_path(g, 1, 2) self.assertEqual(p, [1, 2]) p = find_shortest_path(g, 1, 3) self.assertEqual(p, [1, 2, 3]) p = find_shortest_path(g, 3, 1) self.assertIs(p, None) g = collections.defaultdict(list) g[1] = {2,} g[2] = {3, 1} g[3] = {2, } p = find_shortest_path(g, 1, 2) self.assertEqual(p, [1, 2]) p = find_shortest_path(g, 1, 3) self.assertEqual(p, [1, 2, 3]) p = find_shortest_path(g, 3, 1) self.assertEqual(p, [3, 2, 1]) p = find_shortest_path(g, 2, 1) self.assertEqual(p, [2, 1])
def test_shortest_path(self): g = collections.defaultdict(set) g[1] = {2} g[2] = {3} p = find_shortest_path(g, 1, 2) assert p == [1, 2] p = find_shortest_path(g, 1, 3) assert p == [1, 2, 3] p = find_shortest_path(g, 3, 1) assert p is None g = collections.defaultdict(set) g[1] = {2} g[2] = {3, 1} g[3] = {2} p = find_shortest_path(g, 1, 2) assert p == [1, 2] p = find_shortest_path(g, 1, 3) assert p == [1, 2, 3] p = find_shortest_path(g, 3, 1) assert p == [3, 2, 1] p = find_shortest_path(g, 2, 1) assert p == [2, 1]