[yt-svn] commit/yt: 19 new changesets
commits-noreply at bitbucket.org
commits-noreply at bitbucket.org
Mon Jan 9 10:18:06 PST 2017
19 new commits in yt:
https://bitbucket.org/yt_analysis/yt/commits/1a053f074b1b/
Changeset: 1a053f074b1b
Branch: yt
User: jzuhone
Date: 2017-01-08 01:09:29+00:00
Summary: create_new_unit for default unit registry
Affected #: 2 files
diff -r 276ece4b97fbac9af28f5b1aebf0a177b2eae954 -r 1a053f074b1b1ca24af849e6524776f103b5f15a yt/__init__.py
--- a/yt/__init__.py
+++ b/yt/__init__.py
@@ -99,6 +99,8 @@
import yt.utilities.physical_constants as physical_constants
import yt.units as units
+from yt.units.unit_object import \
+ create_new_unit
from yt.units.yt_array import \
YTArray, \
YTQuantity, \
diff -r 276ece4b97fbac9af28f5b1aebf0a177b2eae954 -r 1a053f074b1b1ca24af849e6524776f103b5f15a yt/units/unit_object.py
--- a/yt/units/unit_object.py
+++ b/yt/units/unit_object.py
@@ -694,3 +694,7 @@
power_string = ""
units.append("(%s)%s" % (unit_string, power_string))
return " * ".join(units)
+
+def create_new_unit(symbol, base_value, dimensions, tex_repr=None, offset=None):
+ default_unit_registry.add(symbol, base_value, dimensions,
+ tex_repr=tex_repr, offset=offset)
\ No newline at end of file
https://bitbucket.org/yt_analysis/yt/commits/408464ad85be/
Changeset: 408464ad85be
Branch: yt
User: jzuhone
Date: 2017-01-08 01:21:52+00:00
Summary: Something we can use for the dataset version also
Affected #: 1 file
diff -r 1a053f074b1b1ca24af849e6524776f103b5f15a -r 408464ad85be215672d79a73b220969b9d469cb0 yt/units/unit_object.py
--- a/yt/units/unit_object.py
+++ b/yt/units/unit_object.py
@@ -695,6 +695,9 @@
units.append("(%s)%s" % (unit_string, power_string))
return " * ".join(units)
+def _create_new_unit(registry, symbol, base_value, dimensions, tex_repr=None, offset=None):
+ registry.add(symbol, base_value, dimensions, tex_repr=tex_repr, offset=offset)
+
def create_new_unit(symbol, base_value, dimensions, tex_repr=None, offset=None):
- default_unit_registry.add(symbol, base_value, dimensions,
- tex_repr=tex_repr, offset=offset)
\ No newline at end of file
+ _create_new_unit(default_unit_registry, symbol, base_value, dimensions,
+ tex_repr=tex_repr, offset=offset)
\ No newline at end of file
https://bitbucket.org/yt_analysis/yt/commits/8a875a218dcb/
Changeset: 8a875a218dcb
Branch: yt
User: jzuhone
Date: 2017-01-08 01:25:07+00:00
Summary: Parse dimension string correctly
Affected #: 1 file
diff -r 408464ad85be215672d79a73b220969b9d469cb0 -r 8a875a218dcb48b227a2ee842bd45ab5211c1df8 yt/units/unit_object.py
--- a/yt/units/unit_object.py
+++ b/yt/units/unit_object.py
@@ -12,7 +12,7 @@
# The full license is in the file COPYING.txt, distributed with this software.
#-----------------------------------------------------------------------------
-from yt.extern.six import text_type
+from yt.extern.six import text_type, string_types
from sympy import \
Expr, Mul, Add, Number, \
Pow, Symbol, Integer, \
@@ -696,8 +696,10 @@
return " * ".join(units)
def _create_new_unit(registry, symbol, base_value, dimensions, tex_repr=None, offset=None):
+ if isinstance(dimensions, string_types):
+ dimensions = Symbol(dimensions, positive=True)
registry.add(symbol, base_value, dimensions, tex_repr=tex_repr, offset=offset)
-
+
def create_new_unit(symbol, base_value, dimensions, tex_repr=None, offset=None):
_create_new_unit(default_unit_registry, symbol, base_value, dimensions,
tex_repr=tex_repr, offset=offset)
\ No newline at end of file
https://bitbucket.org/yt_analysis/yt/commits/19e762eff8bb/
Changeset: 19e762eff8bb
Branch: yt
User: jzuhone
Date: 2017-01-08 01:25:27+00:00
Summary: do this from a dataset also
Affected #: 1 file
diff -r 8a875a218dcb48b227a2ee842bd45ab5211c1df8 -r 19e762eff8bb234ea7d3223b233d57f151d0946d yt/data_objects/static_output.py
--- a/yt/data_objects/static_output.py
+++ b/yt/data_objects/static_output.py
@@ -45,7 +45,8 @@
NoParameterShelf, \
output_type_registry
from yt.units.dimensions import current_mks
-from yt.units.unit_object import Unit, unit_system_registry
+from yt.units.unit_object import Unit, unit_system_registry, \
+ _create_new_unit
from yt.units.unit_registry import UnitRegistry
from yt.fields.derived_field import \
ValidateSpatial
@@ -1336,6 +1337,11 @@
def max_level(self, value):
self._max_level = value
+ def create_new_unit(self, symbol, base_value, dimensions, tex_repr=None,
+ offset=None):
+ _create_new_unit(self.unit_registry, symbol, base_value, dimensions,
+ tex_repr=tex_repr, offset=offset)
+
def _reconstruct_ds(*args, **kwargs):
datasets = ParameterFileStore()
ds = datasets.get_ds_hash(*args)
https://bitbucket.org/yt_analysis/yt/commits/df2e9c8e336c/
Changeset: df2e9c8e336c
Branch: yt
User: jzuhone
Date: 2017-01-08 01:35:53+00:00
Summary: This is much better. Use YTQuantities to define both the value and the dimensions
Affected #: 2 files
diff -r 19e762eff8bb234ea7d3223b233d57f151d0946d -r df2e9c8e336c94b35c39f3c035961df7302fb85e yt/data_objects/static_output.py
--- a/yt/data_objects/static_output.py
+++ b/yt/data_objects/static_output.py
@@ -1337,10 +1337,9 @@
def max_level(self, value):
self._max_level = value
- def create_new_unit(self, symbol, base_value, dimensions, tex_repr=None,
- offset=None):
- _create_new_unit(self.unit_registry, symbol, base_value, dimensions,
- tex_repr=tex_repr, offset=offset)
+ def create_new_unit(self, symbol, value, tex_repr=None, offset=None):
+ _create_new_unit(self.unit_registry, symbol, value, tex_repr=tex_repr,
+ offset=offset)
def _reconstruct_ds(*args, **kwargs):
datasets = ParameterFileStore()
diff -r 19e762eff8bb234ea7d3223b233d57f151d0946d -r df2e9c8e336c94b35c39f3c035961df7302fb85e yt/units/unit_object.py
--- a/yt/units/unit_object.py
+++ b/yt/units/unit_object.py
@@ -12,7 +12,7 @@
# The full license is in the file COPYING.txt, distributed with this software.
#-----------------------------------------------------------------------------
-from yt.extern.six import text_type, string_types
+from yt.extern.six import text_type
from sympy import \
Expr, Mul, Add, Number, \
Pow, Symbol, Integer, \
@@ -695,11 +695,13 @@
units.append("(%s)%s" % (unit_string, power_string))
return " * ".join(units)
-def _create_new_unit(registry, symbol, base_value, dimensions, tex_repr=None, offset=None):
- if isinstance(dimensions, string_types):
- dimensions = Symbol(dimensions, positive=True)
- registry.add(symbol, base_value, dimensions, tex_repr=tex_repr, offset=offset)
+def _create_new_unit(registry, symbol, value, tex_repr=None, offset=None):
+ from yt.units.yt_array import YTQuantity
+ if not isinstance(value, YTQuantity):
+ raise RuntimeError("\"value\" must be a YTQuantity!")
+ base_value = float(value.in_base(unit_system='cgs-ampere'))
+ registry.add(symbol, base_value, value.units.dimensions, tex_repr=tex_repr, offset=offset)
-def create_new_unit(symbol, base_value, dimensions, tex_repr=None, offset=None):
- _create_new_unit(default_unit_registry, symbol, base_value, dimensions,
- tex_repr=tex_repr, offset=offset)
\ No newline at end of file
+def create_new_unit(symbol, value, tex_repr=None, offset=None):
+ _create_new_unit(default_unit_registry, symbol, value, tex_repr=tex_repr,
+ offset=offset)
\ No newline at end of file
https://bitbucket.org/yt_analysis/yt/commits/9825ad40899a/
Changeset: 9825ad40899a
Branch: yt
User: jzuhone
Date: 2017-01-08 01:43:01+00:00
Summary: Change this to define_unit
Affected #: 2 files
diff -r df2e9c8e336c94b35c39f3c035961df7302fb85e -r 9825ad40899acdd24608fda2bd58711adfa67824 yt/data_objects/static_output.py
--- a/yt/data_objects/static_output.py
+++ b/yt/data_objects/static_output.py
@@ -46,7 +46,7 @@
output_type_registry
from yt.units.dimensions import current_mks
from yt.units.unit_object import Unit, unit_system_registry, \
- _create_new_unit
+ _define_unit
from yt.units.unit_registry import UnitRegistry
from yt.fields.derived_field import \
ValidateSpatial
@@ -1337,9 +1337,9 @@
def max_level(self, value):
self._max_level = value
- def create_new_unit(self, symbol, value, tex_repr=None, offset=None):
- _create_new_unit(self.unit_registry, symbol, value, tex_repr=tex_repr,
- offset=offset)
+ def define_unit(self, symbol, value, tex_repr=None, offset=None):
+ _define_unit(self.unit_registry, symbol, value, tex_repr=tex_repr,
+ offset=offset)
def _reconstruct_ds(*args, **kwargs):
datasets = ParameterFileStore()
diff -r df2e9c8e336c94b35c39f3c035961df7302fb85e -r 9825ad40899acdd24608fda2bd58711adfa67824 yt/units/unit_object.py
--- a/yt/units/unit_object.py
+++ b/yt/units/unit_object.py
@@ -695,13 +695,12 @@
units.append("(%s)%s" % (unit_string, power_string))
return " * ".join(units)
-def _create_new_unit(registry, symbol, value, tex_repr=None, offset=None):
+def _define_unit(registry, symbol, value, tex_repr=None, offset=None):
from yt.units.yt_array import YTQuantity
if not isinstance(value, YTQuantity):
raise RuntimeError("\"value\" must be a YTQuantity!")
base_value = float(value.in_base(unit_system='cgs-ampere'))
registry.add(symbol, base_value, value.units.dimensions, tex_repr=tex_repr, offset=offset)
-def create_new_unit(symbol, value, tex_repr=None, offset=None):
- _create_new_unit(default_unit_registry, symbol, value, tex_repr=tex_repr,
- offset=offset)
\ No newline at end of file
+def define_unit(symbol, value, tex_repr=None, offset=None):
+ _define_unit(default_unit_registry, symbol, value, tex_repr=tex_repr, offset=offset)
\ No newline at end of file
https://bitbucket.org/yt_analysis/yt/commits/abde2044f2df/
Changeset: abde2044f2df
Branch: yt
User: jzuhone
Date: 2017-01-08 01:46:22+00:00
Summary: Missed this
Affected #: 1 file
diff -r 9825ad40899acdd24608fda2bd58711adfa67824 -r abde2044f2df8c6e6d1c7bb9566e5f2d3dc63eb6 yt/__init__.py
--- a/yt/__init__.py
+++ b/yt/__init__.py
@@ -99,8 +99,7 @@
import yt.utilities.physical_constants as physical_constants
import yt.units as units
-from yt.units.unit_object import \
- create_new_unit
+from yt.units.unit_object import define_unit
from yt.units.yt_array import \
YTArray, \
YTQuantity, \
https://bitbucket.org/yt_analysis/yt/commits/4c92b3ce3d7a/
Changeset: 4c92b3ce3d7a
Branch: yt
User: jzuhone
Date: 2017-01-08 01:51:37+00:00
Summary: Don’t overwrite units we already have
Affected #: 1 file
diff -r abde2044f2df8c6e6d1c7bb9566e5f2d3dc63eb6 -r 4c92b3ce3d7a13af5bdb8887120456dd16388fed yt/units/unit_object.py
--- a/yt/units/unit_object.py
+++ b/yt/units/unit_object.py
@@ -698,7 +698,9 @@
def _define_unit(registry, symbol, value, tex_repr=None, offset=None):
from yt.units.yt_array import YTQuantity
if not isinstance(value, YTQuantity):
- raise RuntimeError("\"value\" must be a YTQuantity!")
+ raise RuntimeError("The \"value\" argument must be a YTQuantity!")
+ if symbol in registry:
+ raise RuntimeError("The symbol \"%s\" is already in the unit registry!" % symbol)
base_value = float(value.in_base(unit_system='cgs-ampere'))
registry.add(symbol, base_value, value.units.dimensions, tex_repr=tex_repr, offset=offset)
https://bitbucket.org/yt_analysis/yt/commits/4fdcfa59f4c1/
Changeset: 4fdcfa59f4c1
Branch: yt
User: jzuhone
Date: 2017-01-08 01:59:33+00:00
Summary: We allow for float values, but the result is a dimensionless unit. We also allow for (value, unit) tuples.
Affected #: 1 file
diff -r 4c92b3ce3d7a13af5bdb8887120456dd16388fed -r 4fdcfa59f4c1f7b700e8ba0fb532ffd5ea5c2afd yt/units/unit_object.py
--- a/yt/units/unit_object.py
+++ b/yt/units/unit_object.py
@@ -696,13 +696,17 @@
return " * ".join(units)
def _define_unit(registry, symbol, value, tex_repr=None, offset=None):
+ from yt.funcs import iterable
from yt.units.yt_array import YTQuantity
- if not isinstance(value, YTQuantity):
- raise RuntimeError("The \"value\" argument must be a YTQuantity!")
if symbol in registry:
raise RuntimeError("The symbol \"%s\" is already in the unit registry!" % symbol)
+ if iterable(value):
+ value = YTQuantity(value[0], value[1])
+ else:
+ value = YTQuantity(value)
base_value = float(value.in_base(unit_system='cgs-ampere'))
- registry.add(symbol, base_value, value.units.dimensions, tex_repr=tex_repr, offset=offset)
+ dimensions = value.units.dimensions
+ registry.add(symbol, base_value, dimensions, tex_repr=tex_repr, offset=offset)
def define_unit(symbol, value, tex_repr=None, offset=None):
_define_unit(default_unit_registry, symbol, value, tex_repr=tex_repr, offset=offset)
\ No newline at end of file
https://bitbucket.org/yt_analysis/yt/commits/e01ef0bfa7bd/
Changeset: e01ef0bfa7bd
Branch: yt
User: jzuhone
Date: 2017-01-08 02:01:01+00:00
Summary: Simplify this. The only thing we allow for is a (value, unit) tuple
Affected #: 1 file
diff -r 4fdcfa59f4c1f7b700e8ba0fb532ffd5ea5c2afd -r e01ef0bfa7bd30a9fc050d2f1087f35948c1f8f4 yt/units/unit_object.py
--- a/yt/units/unit_object.py
+++ b/yt/units/unit_object.py
@@ -696,14 +696,10 @@
return " * ".join(units)
def _define_unit(registry, symbol, value, tex_repr=None, offset=None):
- from yt.funcs import iterable
from yt.units.yt_array import YTQuantity
if symbol in registry:
raise RuntimeError("The symbol \"%s\" is already in the unit registry!" % symbol)
- if iterable(value):
- value = YTQuantity(value[0], value[1])
- else:
- value = YTQuantity(value)
+ value = YTQuantity(value[0], value[1])
base_value = float(value.in_base(unit_system='cgs-ampere'))
dimensions = value.units.dimensions
registry.add(symbol, base_value, dimensions, tex_repr=tex_repr, offset=offset)
https://bitbucket.org/yt_analysis/yt/commits/a4817eb4af9d/
Changeset: a4817eb4af9d
Branch: yt
User: jzuhone
Date: 2017-01-08 02:03:29+00:00
Summary: On second thought, we’ll allow for YTQuantity
Affected #: 1 file
diff -r e01ef0bfa7bd30a9fc050d2f1087f35948c1f8f4 -r a4817eb4af9d7fbb32c8d52bc50dbc349730d954 yt/units/unit_object.py
--- a/yt/units/unit_object.py
+++ b/yt/units/unit_object.py
@@ -699,7 +699,8 @@
from yt.units.yt_array import YTQuantity
if symbol in registry:
raise RuntimeError("The symbol \"%s\" is already in the unit registry!" % symbol)
- value = YTQuantity(value[0], value[1])
+ if not isinstance(value, YTQuantity):
+ value = YTQuantity(value[0], value[1])
base_value = float(value.in_base(unit_system='cgs-ampere'))
dimensions = value.units.dimensions
registry.add(symbol, base_value, dimensions, tex_repr=tex_repr, offset=offset)
https://bitbucket.org/yt_analysis/yt/commits/ef5c9bff16a0/
Changeset: ef5c9bff16a0
Branch: yt
User: jzuhone
Date: 2017-01-08 02:11:21+00:00
Summary: Make this mutable
Affected #: 1 file
diff -r a4817eb4af9d7fbb32c8d52bc50dbc349730d954 -r ef5c9bff16a0729292a44c52bfd14d05ed2ddb7b yt/units/unit_lookup_table.py
--- a/yt/units/unit_lookup_table.py
+++ b/yt/units/unit_lookup_table.py
@@ -187,7 +187,7 @@
"u": r"\mu",
}
-prefixable_units = (
+prefixable_units = [
"m",
"pc",
"mcm",
@@ -217,7 +217,7 @@
"ohm",
"statohm",
"Sv",
-)
+]
default_base_units = {
dimensions.mass: 'g',
https://bitbucket.org/yt_analysis/yt/commits/b1bed8a95d7b/
Changeset: b1bed8a95d7b
Branch: yt
User: jzuhone
Date: 2017-01-08 02:12:26+00:00
Summary: Allow one to specify whether or not a new unit should be prefixable. Added in docstrings.
Affected #: 2 files
diff -r ef5c9bff16a0729292a44c52bfd14d05ed2ddb7b -r b1bed8a95d7b43bf634651b69cddb4f520cdfc70 yt/data_objects/static_output.py
--- a/yt/data_objects/static_output.py
+++ b/yt/data_objects/static_output.py
@@ -1337,9 +1337,33 @@
def max_level(self, value):
self._max_level = value
- def define_unit(self, symbol, value, tex_repr=None, offset=None):
+ def define_unit(self, symbol, value, tex_repr=None, offset=None, prefixable=False):
+ """
+ Define a new unit and add it to the dataset's unit registry.
+
+ Parameters
+ ----------
+ symbol : string
+ The symbol for the new unit.
+ value : (value, unit) tuple or YTQuantity
+ The definition of the new unit in terms of some other units. For example,
+ one would define a new "mph" unit with (1.0, "mile/hr")
+ tex_repr : string, optional
+ The LaTeX representation of the new unit. If one is not supplied, it will
+ be generated automatically based on the symbol string.
+ offset : float, optional
+ The default offset for the unit. If not set, an offset of 0 is assumed.
+ prefixable : boolean, optional
+ Whether or not the new unit can use SI prefixes. Default: False
+
+ Examples
+ --------
+ >>> ds.define_unit("mph", (1.0, "mile/hr"))
+ >>> two_weeks = YTQuantity(14.0, "days")
+ >>> ds.define_unit("fortnight", two_weeks)
+ """
_define_unit(self.unit_registry, symbol, value, tex_repr=tex_repr,
- offset=offset)
+ offset=offset, prefixable=prefixable)
def _reconstruct_ds(*args, **kwargs):
datasets = ParameterFileStore()
diff -r ef5c9bff16a0729292a44c52bfd14d05ed2ddb7b -r b1bed8a95d7b43bf634651b69cddb4f520cdfc70 yt/units/unit_object.py
--- a/yt/units/unit_object.py
+++ b/yt/units/unit_object.py
@@ -695,7 +695,7 @@
units.append("(%s)%s" % (unit_string, power_string))
return " * ".join(units)
-def _define_unit(registry, symbol, value, tex_repr=None, offset=None):
+def _define_unit(registry, symbol, value, tex_repr=None, offset=None, prefixable=False):
from yt.units.yt_array import YTQuantity
if symbol in registry:
raise RuntimeError("The symbol \"%s\" is already in the unit registry!" % symbol)
@@ -704,6 +704,33 @@
base_value = float(value.in_base(unit_system='cgs-ampere'))
dimensions = value.units.dimensions
registry.add(symbol, base_value, dimensions, tex_repr=tex_repr, offset=offset)
+ if prefixable:
+ prefixable_units.append(symbol)
-def define_unit(symbol, value, tex_repr=None, offset=None):
- _define_unit(default_unit_registry, symbol, value, tex_repr=tex_repr, offset=offset)
\ No newline at end of file
+def define_unit(symbol, value, tex_repr=None, offset=None, prefixable=False):
+ """
+ Define a new unit and add it to the default unit registry.
+
+ Parameters
+ ----------
+ symbol : string
+ The symbol for the new unit.
+ value : (value, unit) tuple or YTQuantity
+ The definition of the new unit in terms of some other units. For example,
+ one would define a new "mph" unit with (1.0, "mile/hr")
+ tex_repr : string, optional
+ The LaTeX representation of the new unit. If one is not supplied, it will
+ be generated automatically based on the symbol string.
+ offset : float, optional
+ The default offset for the unit. If not set, an offset of 0 is assumed.
+ prefixable : boolean, optional
+ Whether or not the new unit can use SI prefixes. Default: False
+
+ Examples
+ --------
+ >>> yt.define_unit("mph", (1.0, "mile/hr"))
+ >>> two_weeks = YTQuantity(14.0, "days")
+ >>> 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
https://bitbucket.org/yt_analysis/yt/commits/79ab33526da7/
Changeset: 79ab33526da7
Branch: yt
User: jzuhone
Date: 2017-01-08 03:13:38+00:00
Summary: Tests for define_unit
Affected #: 1 file
diff -r b1bed8a95d7b43bf634651b69cddb4f520cdfc70 -r 79ab33526da70a696a0594b5f726c56818b1de53 yt/units/tests/test_define_unit.py
--- /dev/null
+++ b/yt/units/tests/test_define_unit.py
@@ -0,0 +1,25 @@
+from yt.units.unit_object import define_unit
+from yt.units.yt_array import YTQuantity
+from yt.convenience import load
+from yt.testing import requires_file
+
+def test_define_unit():
+ define_unit("mph", (1.0, "mile/hr"))
+ a = YTQuantity(2.0, "mph")
+ b = YTQuantity(1.0, "mile")
+ c = YTQuantity(1.0, "hr")
+ assert a == 2.0*b/c
+ d = YTQuantity(1000.0, "cm**3")
+ define_unit("L", d, prefixable=True)
+ e = YTQuantity(1.0, "mL")
+ f = YTQuantity(1.0, "cm**3")
+ assert e == f
+
+gslr = "GasSloshingLowRes/sloshing_low_res_hdf5_plt_cnt_0300"
+ at requires_file(gslr)
+def test_define_unit_dataset():
+ ds = load(gslr)
+ ds.define_unit("fortnight", (14.0, "day"))
+ a = ds.quan(1.0, "fortnight")
+ b = ds.quan(3600.0*24.0*14.0, "code_time")
+ assert a == b
https://bitbucket.org/yt_analysis/yt/commits/0206f49a45ec/
Changeset: 0206f49a45ec
Branch: yt
User: jzuhone
Date: 2017-01-08 04:01:00+00:00
Summary: Docs for define_unit
Affected #: 1 file
diff -r 79ab33526da70a696a0594b5f726c56818b1de53 -r 0206f49a45ec3eb56532c78bdb5019fbf0c2f6f3 doc/source/analyzing/units/2)_Fields_and_unit_conversion.ipynb
--- a/doc/source/analyzing/units/2)_Fields_and_unit_conversion.ipynb
+++ b/doc/source/analyzing/units/2)_Fields_and_unit_conversion.ipynb
@@ -557,6 +557,100 @@
"print (temp, type(temp))\n",
"print (ptemp, type(ptemp))"
]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "### Defining New Units"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "yt also provides a way to define your own units. Suppose you wanted to define a new unit for \"miles per hour\", the familiar \"mph\", which is not already in yt. One can do this by calling `yt.define_unit()`:"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {
+ "collapsed": true
+ },
+ "outputs": [],
+ "source": [
+ "yt.define_unit(\"mph\", (1.0, \"mile/hr\"))"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Once this unit is defined, it can be used in the same way as any other unit:"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {
+ "collapsed": false
+ },
+ "outputs": [],
+ "source": [
+ "from yt.units import clight\n",
+ "print (clight.to('mph'))"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "If you want to define a new unit which is prefixable (like SI units), you can set `prefixable=True` when defining the unit:"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {
+ "collapsed": false
+ },
+ "outputs": [],
+ "source": [
+ "from yt import YTQuantity\n",
+ "yt.define_unit(\"L\", (1000.0, \"cm**3\"), prefixable=True)\n",
+ "print (YTQuantity(1.0, \"mL\").to(\"cm**3\"))"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "`yt.define_unit()` defines new units for all yt operations. However, new units can be defined for particular datasets only as well using `ds.define_unit()`, which has the same signature:"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {
+ "collapsed": false
+ },
+ "outputs": [],
+ "source": [
+ "ds.define_unit(\"M_star\", (2.0e13, \"Msun\"))"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {
+ "collapsed": false
+ },
+ "outputs": [],
+ "source": [
+ "dd = ds.all_data()\n",
+ "print(dd.quantities.total_mass().to(\"M_star\"))"
+ ]
}
],
"metadata": {
@@ -580,4 +674,4 @@
},
"nbformat": 4,
"nbformat_minor": 0
-}
\ No newline at end of file
+}
https://bitbucket.org/yt_analysis/yt/commits/384c216b4696/
Changeset: 384c216b4696
Branch: yt
User: jzuhone
Date: 2017-01-08 16:05:43+00:00
Summary: Add define_unit to API docs
Affected #: 1 file
diff -r 0206f49a45ec3eb56532c78bdb5019fbf0c2f6f3 -r 384c216b46968fb9ad43bb344ebc93f44b2f945e doc/source/reference/api/api.rst
--- a/doc/source/reference/api/api.rst
+++ b/doc/source/reference/api/api.rst
@@ -155,12 +155,12 @@
yt.data_objects.static_output.Dataset.arr
yt.data_objects.static_output.Dataset.quan
+ ~yt.units.unit_object.define_unit
~yt.units.unit_object.Unit
~yt.units.unit_registry.UnitRegistry
~yt.units.yt_array.YTArray
~yt.units.yt_array.YTQuantity
-
Frontends
---------
https://bitbucket.org/yt_analysis/yt/commits/a4073031d01e/
Changeset: a4073031d01e
Branch: yt
User: jzuhone
Date: 2017-01-09 16:00:18+00:00
Summary: Add an error message if value isn’t a tuple
Affected #: 1 file
diff -r 384c216b46968fb9ad43bb344ebc93f44b2f945e -r a4073031d01e95f1c7207cbfa024645b92f12cca yt/units/unit_object.py
--- a/yt/units/unit_object.py
+++ b/yt/units/unit_object.py
@@ -22,6 +22,7 @@
from sympy.parsing.sympy_parser import \
parse_expr, auto_number, rationalize
from keyword import iskeyword
+from yt.funcs import iterable
from yt.units.dimensions import \
base_dimensions, temperature, \
dimensionless, current_mks, \
@@ -700,6 +701,8 @@
if symbol in registry:
raise RuntimeError("The symbol \"%s\" is already in the unit registry!" % symbol)
if not isinstance(value, YTQuantity):
+ if not iterable(value):
+ raise RuntimeError("\"value\" needs to be a (value, unit) tuple!")
value = YTQuantity(value[0], value[1])
base_value = float(value.in_base(unit_system='cgs-ampere'))
dimensions = value.units.dimensions
https://bitbucket.org/yt_analysis/yt/commits/c38a8527f34b/
Changeset: c38a8527f34b
Branch: yt
User: jzuhone
Date: 2017-01-09 16:10:25+00:00
Summary: Just do it this way
Affected #: 1 file
diff -r a4073031d01e95f1c7207cbfa024645b92f12cca -r c38a8527f34b5312cdf05ae1eaf7ddab5a567a77 yt/units/unit_object.py
--- a/yt/units/unit_object.py
+++ b/yt/units/unit_object.py
@@ -22,7 +22,6 @@
from sympy.parsing.sympy_parser import \
parse_expr, auto_number, rationalize
from keyword import iskeyword
-from yt.funcs import iterable
from yt.units.dimensions import \
base_dimensions, temperature, \
dimensionless, current_mks, \
@@ -701,7 +700,7 @@
if symbol in registry:
raise RuntimeError("The symbol \"%s\" is already in the unit registry!" % symbol)
if not isinstance(value, YTQuantity):
- if not iterable(value):
+ if not isinstance(value, tuple):
raise RuntimeError("\"value\" needs to be a (value, unit) tuple!")
value = YTQuantity(value[0], value[1])
base_value = float(value.in_base(unit_system='cgs-ampere'))
https://bitbucket.org/yt_analysis/yt/commits/462126c6d9ae/
Changeset: 462126c6d9ae
Branch: yt
User: jzuhone
Date: 2017-01-09 16:42:11+00:00
Summary: Thanks to Nathan for this suggestion
Affected #: 1 file
diff -r c38a8527f34b5312cdf05ae1eaf7ddab5a567a77 -r 462126c6d9aefcbb363814d3a406df4fa5ebe959 yt/units/unit_object.py
--- a/yt/units/unit_object.py
+++ b/yt/units/unit_object.py
@@ -696,13 +696,14 @@
return " * ".join(units)
def _define_unit(registry, symbol, value, tex_repr=None, offset=None, prefixable=False):
- from yt.units.yt_array import YTQuantity
+ from yt.units.yt_array import YTQuantity, iterable
if symbol in registry:
raise RuntimeError("The symbol \"%s\" is already in the unit registry!" % symbol)
if not isinstance(value, YTQuantity):
- if not isinstance(value, tuple):
+ if iterable(value) and len(value) == 2:
+ value = YTQuantity(value[0], value[1])
+ else:
raise RuntimeError("\"value\" needs to be a (value, unit) tuple!")
- value = YTQuantity(value[0], value[1])
base_value = float(value.in_base(unit_system='cgs-ampere'))
dimensions = value.units.dimensions
registry.add(symbol, base_value, dimensions, tex_repr=tex_repr, offset=offset)
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