[yt-svn] commit/yt: 2 new changesets
commits-noreply at bitbucket.org
commits-noreply at bitbucket.org
Fri Apr 21 04:36:47 PDT 2017
2 new commits in yt:
https://bitbucket.org/yt_analysis/yt/commits/8b2147a1a06c/
Changeset: 8b2147a1a06c
Branch: yt
User: ngoldbaum
Date: 2017-03-27 23:58:18+00:00
Summary: Make it possible to create a Unit object from a YTQuantity
Affected #: 3 files
diff -r e265191afc164152ef482e861826d6dc877e6893 -r 8b2147a1a06c44370028aadb6f026a675389ebd6 doc/source/reference/api/api.rst
--- a/doc/source/reference/api/api.rst
+++ b/doc/source/reference/api/api.rst
@@ -149,6 +149,7 @@
~yt.units.unit_object.define_unit
~yt.units.unit_object.Unit
~yt.units.unit_registry.UnitRegistry
+ ~yt.units.unit_systems.UnitSystem
~yt.units.yt_array.YTArray
~yt.units.yt_array.YTQuantity
~yt.units.yt_array.uconcatenate
diff -r e265191afc164152ef482e861826d6dc877e6893 -r 8b2147a1a06c44370028aadb6f026a675389ebd6 yt/units/tests/test_units.py
--- a/yt/units/tests/test_units.py
+++ b/yt/units/tests/test_units.py
@@ -26,6 +26,7 @@
fake_random_ds, assert_allclose_units, \
assert_almost_equal
from yt.units.unit_registry import UnitRegistry
+from yt.units import electrostatic_unit, elementary_charge
# dimensions
from yt.units.dimensions import \
@@ -510,3 +511,18 @@
unserialized_reg = UnitRegistry.from_json(json_reg)
assert_equal(reg.lut, unserialized_reg.lut)
+
+def test_creation_from_ytarray():
+ u1 = Unit(electrostatic_unit)
+ assert_equal(str(u1), 'esu')
+ assert_equal(u1, Unit('esu'))
+ assert_equal(u1, electrostatic_unit.units)
+
+ u2 = Unit(elementary_charge)
+ assert_equal(str(u2), '4.8032056e-10*esu')
+ assert_equal(u2, Unit('4.8032056e-10*esu'))
+ assert_equal(u1, elementary_charge.units)
+
+ assert_equal((u1/u2).base_value, electrostatic_unit/elementary_charge)
+
+ assert_raises(UnitParseError, Unit, [1, 2, 3]*elementary_charge)
diff -r e265191afc164152ef482e861826d6dc877e6893 -r 8b2147a1a06c44370028aadb6f026a675389ebd6 yt/units/unit_object.py
--- a/yt/units/unit_object.py
+++ b/yt/units/unit_object.py
@@ -216,6 +216,17 @@
elif isinstance(unit_expr, Unit):
# grab the unit object's sympy expression.
unit_expr = unit_expr.expr
+ elif hasattr(unit_expr, 'units') and hasattr(unit_expr, 'value'):
+ # something that looks like a YTArray, grab the unit and value
+ if unit_expr.shape != ():
+ raise UnitParseError(
+ 'Cannot create a unit from a non-scalar YTArray, received: '
+ '%s' % (unit_expr, ))
+ value = unit_expr.value
+ if value == 1:
+ unit_expr = unit_expr.units.expr
+ else:
+ unit_expr = unit_expr.value*unit_expr.units.expr
# Make sure we have an Expr at this point.
if not isinstance(unit_expr, Expr):
raise UnitParseError("Unit representation must be a string or " \
@@ -736,4 +747,4 @@
>>> yt.define_unit("fortnight", two_weeks)
"""
_define_unit(default_unit_registry, symbol, value, tex_repr=tex_repr,
- offset=offset, prefixable=prefixable)
\ No newline at end of file
+ offset=offset, prefixable=prefixable)
https://bitbucket.org/yt_analysis/yt/commits/634cc55bf066/
Changeset: 634cc55bf066
Branch: yt
User: xarthisius
Date: 2017-04-21 11:36:41+00:00
Summary: Merged in ngoldbaum/yt (pull request #2563)
Make it possible to create a Unit object from a YTQuantity
Approved-by: John ZuHone <jzuhone at gmail.com>
Approved-by: Matt Turk <matthewturk at gmail.com>
Approved-by: Kacper Kowalik <xarthisius.kk at gmail.com>
Affected #: 3 files
diff -r e0add337739c8d6744207145757215c1f7204863 -r 634cc55bf0668d551109b3a35850bb6c369b47b4 doc/source/reference/api/api.rst
--- a/doc/source/reference/api/api.rst
+++ b/doc/source/reference/api/api.rst
@@ -149,6 +149,7 @@
~yt.units.unit_object.define_unit
~yt.units.unit_object.Unit
~yt.units.unit_registry.UnitRegistry
+ ~yt.units.unit_systems.UnitSystem
~yt.units.yt_array.YTArray
~yt.units.yt_array.YTQuantity
~yt.units.yt_array.uconcatenate
diff -r e0add337739c8d6744207145757215c1f7204863 -r 634cc55bf0668d551109b3a35850bb6c369b47b4 yt/units/tests/test_units.py
--- a/yt/units/tests/test_units.py
+++ b/yt/units/tests/test_units.py
@@ -26,6 +26,7 @@
fake_random_ds, assert_allclose_units, \
assert_almost_equal
from yt.units.unit_registry import UnitRegistry
+from yt.units import electrostatic_unit, elementary_charge
# dimensions
from yt.units.dimensions import \
@@ -507,3 +508,18 @@
unserialized_reg = UnitRegistry.from_json(json_reg)
assert_equal(reg.lut, unserialized_reg.lut)
+
+def test_creation_from_ytarray():
+ u1 = Unit(electrostatic_unit)
+ assert_equal(str(u1), 'esu')
+ assert_equal(u1, Unit('esu'))
+ assert_equal(u1, electrostatic_unit.units)
+
+ u2 = Unit(elementary_charge)
+ assert_equal(str(u2), '4.8032056e-10*esu')
+ assert_equal(u2, Unit('4.8032056e-10*esu'))
+ assert_equal(u1, elementary_charge.units)
+
+ assert_equal((u1/u2).base_value, electrostatic_unit/elementary_charge)
+
+ assert_raises(UnitParseError, Unit, [1, 2, 3]*elementary_charge)
diff -r e0add337739c8d6744207145757215c1f7204863 -r 634cc55bf0668d551109b3a35850bb6c369b47b4 yt/units/unit_object.py
--- a/yt/units/unit_object.py
+++ b/yt/units/unit_object.py
@@ -216,6 +216,17 @@
elif isinstance(unit_expr, Unit):
# grab the unit object's sympy expression.
unit_expr = unit_expr.expr
+ elif hasattr(unit_expr, 'units') and hasattr(unit_expr, 'value'):
+ # something that looks like a YTArray, grab the unit and value
+ if unit_expr.shape != ():
+ raise UnitParseError(
+ 'Cannot create a unit from a non-scalar YTArray, received: '
+ '%s' % (unit_expr, ))
+ value = unit_expr.value
+ if value == 1:
+ unit_expr = unit_expr.units.expr
+ else:
+ unit_expr = unit_expr.value*unit_expr.units.expr
# Make sure we have an Expr at this point.
if not isinstance(unit_expr, Expr):
raise UnitParseError("Unit representation must be a string or " \
@@ -736,4 +747,4 @@
>>> yt.define_unit("fortnight", two_weeks)
"""
_define_unit(default_unit_registry, symbol, value, tex_repr=tex_repr,
- offset=offset, prefixable=prefixable)
\ No newline at end of file
+ offset=offset, prefixable=prefixable)
Repository URL: https://bitbucket.org/yt_analysis/yt/
--
This is a commit notification from bitbucket.org. You are receiving
this because you have the service enabled, addressing the recipient of
this email.
More information about the yt-svn
mailing list