[yt-svn] commit/yt: 11 new changesets
commits-noreply at bitbucket.org
commits-noreply at bitbucket.org
Mon Sep 25 15:59:53 PDT 2017
11 new commits in yt:
https://bitbucket.org/yt_analysis/yt/commits/bd406b866838/
Changeset: bd406b866838
User: jzuhone
Date: 2017-09-19 14:46:59+00:00
Summary: Add equivalence to in_units and to
Affected #: 1 file
diff -r 92f604cded4654e4d63653cbec574dee30d11d68 -r bd406b866838c09bbc1a6dcda5731508a9ff45c6 yt/units/yt_array.py
--- a/yt/units/yt_array.py
+++ b/yt/units/yt_array.py
@@ -589,38 +589,48 @@
"""
return self.convert_to_units(self.units.get_mks_equivalent())
- def in_units(self, units):
+ def in_units(self, units, equivalence=None, **kwargs):
"""
- Creates a copy of this array with the data in the supplied units, and
- returns it.
+ Creates a copy of this array with the data in the supplied
+ units, and returns it. Optionally, an equivalence can be
+ specified to convert to an equivalent quantity which is not
+ in the same dimensions. All additional keyword arguments are
+ passed to the equivalency if necessary.
Parameters
----------
units : Unit object or string
The units you want to get a new quantity in.
+ equivalence : string, optional
+ The equivalence you wish to use. To see which
+ equivalencies are supported for this unitful
+ quantity, try the :meth:`list_equivalencies`
+ method. Default: None
Returns
-------
YTArray
+ """
+ if equivalence is None:
+ new_units = _unit_repr_check_same(self.units, units)
+ (conversion_factor, offset) = self.units.get_conversion_factor(new_units)
- """
- new_units = _unit_repr_check_same(self.units, units)
- (conversion_factor, offset) = self.units.get_conversion_factor(new_units)
-
- new_array = type(self)(self.ndview * conversion_factor, new_units)
+ new_array = type(self)(self.ndview * conversion_factor, new_units)
- if offset:
- np.subtract(new_array, offset*new_array.uq, new_array)
+ if offset:
+ np.subtract(new_array, offset*new_array.uq, new_array)
- return new_array
+ return new_array
+ else:
+ return self.to_equivalent(units, equivalence, **kwargs)
- def to(self, units):
+ def to(self, units, equivalence=None, **kwargs):
"""
An alias for YTArray.in_units().
See the docstrings of that function for details.
"""
- return self.in_units(units)
+ return self.in_units(units, equivalence=equivalence, **kwargs)
def in_base(self, unit_system="cgs"):
"""
https://bitbucket.org/yt_analysis/yt/commits/b7d460c3497e/
Changeset: b7d460c3497e
User: jzuhone
Date: 2017-09-19 14:49:51+00:00
Summary: Separate out docstring
Affected #: 1 file
diff -r bd406b866838c09bbc1a6dcda5731508a9ff45c6 -r b7d460c3497e64ddd05b307c6163182db6a22b0f yt/units/yt_array.py
--- a/yt/units/yt_array.py
+++ b/yt/units/yt_array.py
@@ -592,10 +592,12 @@
def in_units(self, units, equivalence=None, **kwargs):
"""
Creates a copy of this array with the data in the supplied
- units, and returns it. Optionally, an equivalence can be
- specified to convert to an equivalent quantity which is not
- in the same dimensions. All additional keyword arguments are
- passed to the equivalency if necessary.
+ units, and returns it.
+
+ Optionally, an equivalence can be specified to convert to an
+ equivalent quantity which is not in the same dimensions. All
+ additional keyword arguments are passed to the equivalency if
+ necessary.
Parameters
----------
https://bitbucket.org/yt_analysis/yt/commits/ccb26c0b8a87/
Changeset: ccb26c0b8a87
User: jzuhone
Date: 2017-09-19 14:50:03+00:00
Summary: Add to_value method to return quantities without units
Affected #: 1 file
diff -r b7d460c3497e64ddd05b307c6163182db6a22b0f -r ccb26c0b8a8787be50b690d0780a3499000f8b10 yt/units/yt_array.py
--- a/yt/units/yt_array.py
+++ b/yt/units/yt_array.py
@@ -634,6 +634,33 @@
"""
return self.in_units(units, equivalence=equivalence, **kwargs)
+ def to_value(self, units, equivalence=None, **kwargs):
+ """
+ Creates a copy of this array with the data in the supplied
+ units, and returns it without units. Output is therefore a
+ bare NumPy array.
+
+ Optionally, an equivalence can be specified to convert to an
+ equivalent quantity which is not in the same dimensions. All
+ additional keyword arguments are passed to the equivalency if
+ necessary.
+
+ Parameters
+ ----------
+ units : Unit object or string
+ The units you want to get a new quantity in.
+ equivalence : string, optional
+ The equivalence you wish to use. To see which
+ equivalencies are supported for this unitful
+ quantity, try the :meth:`list_equivalencies`
+ method. Default: None
+
+ Returns
+ -------
+ NumPy array
+ """
+ return self.in_units(units, equivalence=equivalence, **kwargs).value
+
def in_base(self, unit_system="cgs"):
"""
Creates a copy of this array with the data in the specified unit system,
https://bitbucket.org/yt_analysis/yt/commits/616673d3c668/
Changeset: 616673d3c668
User: jzuhone
Date: 2017-09-19 15:58:18+00:00
Summary: Make units optional so that we can return just the value of the array or quantity in its current units
Affected #: 1 file
diff -r ccb26c0b8a8787be50b690d0780a3499000f8b10 -r 616673d3c668155b326e2611272469b761ce9bca yt/units/yt_array.py
--- a/yt/units/yt_array.py
+++ b/yt/units/yt_array.py
@@ -634,7 +634,7 @@
"""
return self.in_units(units, equivalence=equivalence, **kwargs)
- def to_value(self, units, equivalence=None, **kwargs):
+ def to_value(self, units=None, equivalence=None, **kwargs):
"""
Creates a copy of this array with the data in the supplied
units, and returns it without units. Output is therefore a
@@ -647,8 +647,10 @@
Parameters
----------
- units : Unit object or string
- The units you want to get a new quantity in.
+ units : Unit object or string, optional
+ The units you want to get the bare quantity in. If not
+ specified, the value will be returned in the current units.
+
equivalence : string, optional
The equivalence you wish to use. To see which
equivalencies are supported for this unitful
@@ -659,8 +661,11 @@
-------
NumPy array
"""
- return self.in_units(units, equivalence=equivalence, **kwargs).value
-
+ if units is None:
+ return self.value
+ else:
+ return self.in_units(units, equivalence=equivalence, **kwargs).value
+
def in_base(self, unit_system="cgs"):
"""
Creates a copy of this array with the data in the specified unit system,
https://bitbucket.org/yt_analysis/yt/commits/a456bdf7ebba/
Changeset: a456bdf7ebba
User: jzuhone
Date: 2017-09-19 16:18:57+00:00
Summary: If the units have the same dimensions, fall back to in_units
Affected #: 1 file
diff -r 616673d3c668155b326e2611272469b761ce9bca -r a456bdf7ebbabcf894be198f3ce9c3a3a501bc32 yt/units/yt_array.py
--- a/yt/units/yt_array.py
+++ b/yt/units/yt_array.py
@@ -728,6 +728,8 @@
>>> a.to_equivalent("keV", "thermal")
"""
conv_unit = Unit(unit, registry=self.units.registry)
+ if self.units.same_dimensions_as(conv_unit):
+ return self.in_units(conv_unit)
this_equiv = equivalence_registry[equiv]()
oneway_or_equivalent = (
conv_unit.has_equivalent(equiv) or this_equiv._one_way)
https://bitbucket.org/yt_analysis/yt/commits/60d1cd74347d/
Changeset: 60d1cd74347d
User: jzuhone
Date: 2017-09-19 16:34:20+00:00
Summary: Make docstrings clearer and more explicit
Affected #: 1 file
diff -r a456bdf7ebbabcf894be198f3ce9c3a3a501bc32 -r 60d1cd74347dd3e5303da3284ad9a3fe6e38b458 yt/units/yt_array.py
--- a/yt/units/yt_array.py
+++ b/yt/units/yt_array.py
@@ -595,9 +595,13 @@
units, and returns it.
Optionally, an equivalence can be specified to convert to an
- equivalent quantity which is not in the same dimensions. All
- additional keyword arguments are passed to the equivalency if
- necessary.
+ equivalent quantity which is not in the same dimensions.
+
+ .. note::
+
+ All additional keyword arguments are passed to the
+ equivalency, which should be used if that particular
+ equivalency requires them.
Parameters
----------
@@ -641,9 +645,13 @@
bare NumPy array.
Optionally, an equivalence can be specified to convert to an
- equivalent quantity which is not in the same dimensions. All
- additional keyword arguments are passed to the equivalency if
- necessary.
+ equivalent quantity which is not in the same dimensions.
+
+ .. note::
+
+ All additional keyword arguments are passed to the
+ equivalency, which should be used if that particular
+ equivalency requires them.
Parameters
----------
https://bitbucket.org/yt_analysis/yt/commits/42c3956f2cfd/
Changeset: 42c3956f2cfd
User: jzuhone
Date: 2017-09-19 17:21:21+00:00
Summary: Make to_value return a float for YTQuantity
Affected #: 1 file
diff -r 60d1cd74347dd3e5303da3284ad9a3fe6e38b458 -r 42c3956f2cfd63c22eb2c02164a3c41885894d6d yt/units/yt_array.py
--- a/yt/units/yt_array.py
+++ b/yt/units/yt_array.py
@@ -670,9 +670,13 @@
NumPy array
"""
if units is None:
- return self.value
+ v = self.value
else:
- return self.in_units(units, equivalence=equivalence, **kwargs).value
+ v = self.in_units(units, equivalence=equivalence, **kwargs).value
+ if isinstance(self, YTQuantity):
+ return float(v)
+ else:
+ return v
def in_base(self, unit_system="cgs"):
"""
https://bitbucket.org/yt_analysis/yt/commits/597843232253/
Changeset: 597843232253
User: jzuhone
Date: 2017-09-19 21:58:28+00:00
Summary: Fix link here
Affected #: 1 file
diff -r 42c3956f2cfd63c22eb2c02164a3c41885894d6d -r 5978432322531d092cd5463c0b09725853c29e35 doc/source/analyzing/units/fields_and_unit_conversion.rst
--- a/doc/source/analyzing/units/fields_and_unit_conversion.rst
+++ b/doc/source/analyzing/units/fields_and_unit_conversion.rst
@@ -1,4 +1,4 @@
-.. _data_selection_and_fields:
+.. _fields_and_unit_conversion:
Fields and Unit Conversion
==========================
https://bitbucket.org/yt_analysis/yt/commits/a0b738ad4475/
Changeset: a0b738ad4475
User: jzuhone
Date: 2017-09-19 22:15:49+00:00
Summary: Update unit docs
Affected #: 2 files
diff -r 5978432322531d092cd5463c0b09725853c29e35 -r a0b738ad4475f4570713ac835bdea6079a4b0bb0 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
@@ -322,7 +322,20 @@
"cell_type": "markdown",
"metadata": {},
"source": [
- "Since we have a copy of the data, we can mess with it however we wish without disturbing the original data returned by the yt data object."
+ "Since we have a copy of the data, we can mess with it however we wish without disturbing the original data returned by the yt data object.\n",
+ "\n",
+ "There is yet another way to return a copy of the array data in a `YTArray` or the floating-point value of a `YTQuantity`, which also allows for the possibility to convert to different units. This is done using the `to_value` method:"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "print(dens.to_value()) # Don't change units\n",
+ "print(dens.to_value(\"Msun\")) # Change units to solar masses\n",
+ "print(dens[0].to_value(\"lbm\")) # Pick the first value and change its units to pounds"
]
},
{
@@ -388,7 +401,9 @@
{
"cell_type": "code",
"execution_count": null,
- "metadata": {},
+ "metadata": {
+ "collapsed": true
+ },
"outputs": [],
"source": [
"from astropy import units as u\n",
@@ -410,7 +425,9 @@
{
"cell_type": "code",
"execution_count": null,
- "metadata": {},
+ "metadata": {
+ "collapsed": true
+ },
"outputs": [],
"source": [
"a = np.random.random(size=10) * u.km/u.s\n",
@@ -437,7 +454,9 @@
{
"cell_type": "code",
"execution_count": null,
- "metadata": {},
+ "metadata": {
+ "collapsed": true
+ },
"outputs": [],
"source": [
"temp = dd[\"temperature\"]\n",
@@ -464,7 +483,9 @@
{
"cell_type": "code",
"execution_count": null,
- "metadata": {},
+ "metadata": {
+ "collapsed": true
+ },
"outputs": [],
"source": [
"from yt.units import kboltz\n",
@@ -496,7 +517,8 @@
"source": [
"k1 = kboltz.to_astropy()\n",
"k2 = yt.YTQuantity.from_astropy(kb)\n",
- "print (k1 == k2)"
+ "print(k1)\n",
+ "print(k2)"
]
},
{
@@ -520,7 +542,9 @@
{
"cell_type": "code",
"execution_count": null,
- "metadata": {},
+ "metadata": {
+ "collapsed": true
+ },
"outputs": [],
"source": [
"from pint import UnitRegistry\n",
@@ -542,7 +566,9 @@
{
"cell_type": "code",
"execution_count": null,
- "metadata": {},
+ "metadata": {
+ "collapsed": true
+ },
"outputs": [],
"source": [
"ptemp = temp.to_pint()"
@@ -593,9 +619,7 @@
{
"cell_type": "code",
"execution_count": null,
- "metadata": {
- "collapsed": false
- },
+ "metadata": {},
"outputs": [],
"source": [
"from yt.units import clight\n",
@@ -612,9 +636,7 @@
{
"cell_type": "code",
"execution_count": null,
- "metadata": {
- "collapsed": false
- },
+ "metadata": {},
"outputs": [],
"source": [
"from yt import YTQuantity\n",
@@ -633,7 +655,7 @@
"cell_type": "code",
"execution_count": null,
"metadata": {
- "collapsed": false
+ "collapsed": true
},
"outputs": [],
"source": [
@@ -643,9 +665,7 @@
{
"cell_type": "code",
"execution_count": null,
- "metadata": {
- "collapsed": false
- },
+ "metadata": {},
"outputs": [],
"source": [
"dd = ds.all_data()\n",
@@ -655,23 +675,23 @@
],
"metadata": {
"kernelspec": {
- "display_name": "Python 3",
+ "display_name": "Python [default]",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
- "version": 3.0
+ "version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
- "version": "3.5.1"
+ "version": "3.6.1"
}
},
"nbformat": 4,
- "nbformat_minor": 0
+ "nbformat_minor": 1
}
diff -r 5978432322531d092cd5463c0b09725853c29e35 -r a0b738ad4475f4570713ac835bdea6079a4b0bb0 doc/source/analyzing/units/6)_Unit_Equivalencies.ipynb
--- a/doc/source/analyzing/units/6)_Unit_Equivalencies.ipynb
+++ b/doc/source/analyzing/units/6)_Unit_Equivalencies.ipynb
@@ -18,15 +18,13 @@
"* `\"number_density\"`: conversions between density and number density ($n = \\rho/\\mu{m_p}$)\n",
"* `\"sound_speed\"`: conversions between temperature and sound speed for an ideal gas ($c_s^2 = \\gamma{k_BT}/\\mu{m_p}$)\n",
"\n",
- "A `YTArray` or `YTQuantity` can be converted to an equivalent using `to_equivalent`, where the unit and the equivalence name are provided as arguments:"
+ "A `YTArray` or `YTQuantity` can be converted to an equivalent using `in_units` (previously described in [Fields and Unit Conversion](fields_and_unit_conversion.html)), where the unit and the equivalence name are provided as additional arguments:"
]
},
{
"cell_type": "code",
"execution_count": null,
- "metadata": {
- "collapsed": false
- },
+ "metadata": {},
"outputs": [],
"source": [
"import yt\n",
@@ -37,12 +35,12 @@
"\n",
"dd = ds.all_data()\n",
"\n",
- "print (dd[\"temperature\"].to_equivalent(\"erg\", \"thermal\"))\n",
- "print (dd[\"temperature\"].to_equivalent(\"eV\", \"thermal\"))\n",
+ "print (dd[\"temperature\"].in_units(\"erg\", equivalence=\"thermal\"))\n",
+ "print (dd[\"temperature\"].in_units(\"eV\", equivalence=\"thermal\"))\n",
"\n",
"# Rest energy of the proton\n",
"from yt.units import mp\n",
- "E_p = mp.to_equivalent(\"GeV\", \"mass_energy\")\n",
+ "E_p = mp.in_units(\"GeV\", equivalence=\"mass_energy\")\n",
"print (E_p)"
]
},
@@ -56,16 +54,31 @@
{
"cell_type": "code",
"execution_count": null,
- "metadata": {
- "collapsed": false
- },
+ "metadata": {},
"outputs": [],
"source": [
"from yt.units import clight\n",
"v = 0.1*clight\n",
- "g = v.to_equivalent(\"dimensionless\", \"lorentz\")\n",
+ "g = v.in_units(\"dimensionless\", equivalence=\"lorentz\")\n",
"print (g)\n",
- "print (g.to_equivalent(\"c\", \"lorentz\"))"
+ "print (g.in_units(\"c\", equivalence=\"lorentz\"))"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "The previously described `to_value` method, which works like `in_units` except that it returns a bare NumPy array or floating-point number, also accepts equivalencies:"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "print (dd[\"temperature\"].to_value(\"erg\", equivalence=\"thermal\"))\n",
+ "print (mp.to_value(\"GeV\", equivalence=\"mass_energy\"))"
]
},
{
@@ -85,14 +98,12 @@
{
"cell_type": "code",
"execution_count": null,
- "metadata": {
- "collapsed": false
- },
+ "metadata": {},
"outputs": [],
"source": [
"print (dd[\"density\"].max())\n",
- "print (dd[\"density\"].to_equivalent(\"cm**-3\", \"number_density\").max())\n",
- "print (dd[\"density\"].to_equivalent(\"cm**-3\", \"number_density\", mu=0.75).max())"
+ "print (dd[\"density\"].in_units(\"cm**-3\", equivalence=\"number_density\").max())\n",
+ "print (dd[\"density\"].in_units(\"cm**-3\", equivalence=\"number_density\", mu=0.75).max())"
]
},
{
@@ -105,13 +116,11 @@
{
"cell_type": "code",
"execution_count": null,
- "metadata": {
- "collapsed": false
- },
+ "metadata": {},
"outputs": [],
"source": [
- "print (dd[\"temperature\"].to_equivalent(\"km/s\", \"sound_speed\").mean())\n",
- "print (dd[\"temperature\"].to_equivalent(\"km/s\", \"sound_speed\", gamma=4./3., mu=0.5).mean())"
+ "print (dd[\"temperature\"].in_units(\"km/s\", equivalence=\"sound_speed\").mean())\n",
+ "print (dd[\"temperature\"].in_units(\"km/s\", equivalence=\"sound_speed\", gamma=4./3., mu=0.5).mean())"
]
},
{
@@ -138,9 +147,7 @@
{
"cell_type": "code",
"execution_count": null,
- "metadata": {
- "collapsed": false
- },
+ "metadata": {},
"outputs": [],
"source": [
"Q1 = YTQuantity(1.0,\"C\")\n",
@@ -161,13 +168,11 @@
{
"cell_type": "code",
"execution_count": null,
- "metadata": {
- "collapsed": false
- },
+ "metadata": {},
"outputs": [],
"source": [
"from yt.units import qp # the elementary charge in esu\n",
- "qp_SI = qp.to_equivalent(\"C\",\"SI\") # convert to Coulombs\n",
+ "qp_SI = qp.in_units(\"C\", equivalence=\"SI\") # convert to Coulombs\n",
"print (qp)\n",
"print (qp_SI)"
]
@@ -182,13 +187,11 @@
{
"cell_type": "code",
"execution_count": null,
- "metadata": {
- "collapsed": false
- },
+ "metadata": {},
"outputs": [],
"source": [
"B = YTQuantity(1.0,\"T\") # magnetic field in Tesla\n",
- "print (B, B.to_equivalent(\"gauss\",\"CGS\")) # convert to Gauss"
+ "print (B, B.in_units(\"gauss\", equivalence=\"CGS\")) # convert to Gauss"
]
},
{
@@ -201,15 +204,13 @@
{
"cell_type": "code",
"execution_count": null,
- "metadata": {
- "collapsed": false
- },
+ "metadata": {},
"outputs": [],
"source": [
"I = YTQuantity(1.0,\"A\")\n",
- "I_cgs = I.to_equivalent(\"statA\",\"CGS\")\n",
+ "I_cgs = I.in_units(\"statA\", equivalence=\"CGS\")\n",
"R = YTQuantity(1.0,\"ohm\")\n",
- "R_cgs = R.to_equivalent(\"statohm\",\"CGS\")\n",
+ "R_cgs = R.in_units(\"statohm\", equivalence=\"CGS\")\n",
"P = I**2*R\n",
"P_cgs = I_cgs**2*R_cgs"
]
@@ -224,9 +225,7 @@
{
"cell_type": "code",
"execution_count": null,
- "metadata": {
- "collapsed": false
- },
+ "metadata": {},
"outputs": [],
"source": [
"print (P_cgs.units.dimensions == P.units.dimensions)\n",
@@ -250,15 +249,13 @@
{
"cell_type": "code",
"execution_count": null,
- "metadata": {
- "collapsed": false
- },
+ "metadata": {},
"outputs": [],
"source": [
"from yt.utilities.exceptions import YTInvalidUnitEquivalence\n",
"\n",
"try:\n",
- " x = v.to_equivalent(\"angstrom\", \"spectral\")\n",
+ " x = v.in_units(\"angstrom\", equivalence=\"spectral\")\n",
"except YTInvalidUnitEquivalence as e:\n",
" print (e)"
]
@@ -273,9 +270,7 @@
{
"cell_type": "code",
"execution_count": null,
- "metadata": {
- "collapsed": false
- },
+ "metadata": {},
"outputs": [],
"source": [
"print (mp.has_equivalent(\"compton\"))\n",
@@ -292,9 +287,7 @@
{
"cell_type": "code",
"execution_count": null,
- "metadata": {
- "collapsed": false
- },
+ "metadata": {},
"outputs": [],
"source": [
"E_p.list_equivalencies()"
@@ -303,7 +296,7 @@
],
"metadata": {
"kernelspec": {
- "display_name": "Python 3",
+ "display_name": "Python [default]",
"language": "python",
"name": "python3"
},
@@ -317,9 +310,9 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
- "version": "3.5.1"
+ "version": "3.6.1"
}
},
"nbformat": 4,
- "nbformat_minor": 0
+ "nbformat_minor": 1
}
https://bitbucket.org/yt_analysis/yt/commits/a0b5f557b513/
Changeset: a0b5f557b513
User: jzuhone
Date: 2017-09-20 12:08:23+00:00
Summary: New tests
Affected #: 1 file
diff -r a0b738ad4475f4570713ac835bdea6079a4b0bb0 -r a0b5f557b513e40a1f77630019b99fc2aec206fc yt/units/tests/test_ytarray.py
--- a/yt/units/tests/test_ytarray.py
+++ b/yt/units/tests/test_ytarray.py
@@ -991,6 +991,17 @@
for op in [operator.abs, operator.neg, operator.pos]:
unary_op_registry_comparison(op)
+def test_to_value():
+
+ a = YTArray([1.0, 2.0, 3.0], "kpc")
+ assert_equal(a.to_value(), np.array([1.0, 2.0, 3.0]))
+ assert_equal(a.to_value(), a.value)
+ assert_equal(a.to_value("km"), a.in_units("km").value)
+
+ b = YTQuantity(5.5, "Msun")
+ assert_equal(b.to_value(), 5.5)
+ assert_equal(b.to_value("g"), b.in_units("g").value)
+
@requires_module("astropy")
def test_astropy():
from yt.utilities.on_demand_imports import _astropy
@@ -1113,83 +1124,88 @@
# Mass-energy
- E = mp.to_equivalent("keV","mass_energy")
+ E = mp.in_units("keV","mass_energy")
assert_equal(E, mp*clight*clight)
- assert_allclose_units(mp, E.to_equivalent("g", "mass_energy"))
+ assert_allclose_units(mp, E.in_units("g", "mass_energy"))
# Thermal
T = YTQuantity(1.0e8,"K")
- E = T.to_equivalent("W*hr","thermal")
+ E = T.in_units("W*hr","thermal")
assert_equal(E, (kboltz*T).in_units("W*hr"))
- assert_allclose_units(T, E.to_equivalent("K", "thermal"))
+ assert_allclose_units(T, E.in_units("K", "thermal"))
# Spectral
l = YTQuantity(4000.,"angstrom")
- nu = l.to_equivalent("Hz","spectral")
+ nu = l.in_units("Hz","spectral")
assert_equal(nu, clight/l)
E = hcgs*nu
- l2 = E.to_equivalent("angstrom", "spectral")
+ l2 = E.in_units("angstrom", "spectral")
assert_allclose_units(l, l2)
nu2 = clight/l2.in_units("cm")
assert_allclose_units(nu, nu2)
- E2 = nu2.to_equivalent("keV", "spectral")
+ E2 = nu2.in_units("keV", "spectral")
assert_allclose_units(E2, E.in_units("keV"))
# Sound-speed
mu = 0.6
gg = 5./3.
- c_s = T.to_equivalent("km/s","sound_speed")
+ c_s = T.in_units("km/s", equivalence="sound_speed")
assert_equal(c_s, np.sqrt(gg*kboltz*T/(mu*mh)))
- assert_allclose_units(T, c_s.to_equivalent("K","sound_speed"))
+ assert_allclose_units(T, c_s.in_units("K","sound_speed"))
mu = 0.5
gg = 4./3.
- c_s = T.to_equivalent("km/s","sound_speed", mu=mu, gamma=gg)
+ c_s = T.in_units("km/s","sound_speed", mu=mu, gamma=gg)
assert_equal(c_s, np.sqrt(gg*kboltz*T/(mu*mh)))
- assert_allclose_units(T, c_s.to_equivalent("K","sound_speed", mu=mu, gamma=gg))
+ assert_allclose_units(T, c_s.in_units("K","sound_speed", mu=mu, gamma=gg))
# Lorentz
v = 0.8*clight
- g = v.to_equivalent("dimensionless","lorentz")
+ g = v.in_units("dimensionless","lorentz")
g2 = YTQuantity(1./np.sqrt(1.-0.8*0.8), "dimensionless")
assert_allclose_units(g, g2)
- v2 = g2.to_equivalent("mile/hr", "lorentz")
+ v2 = g2.in_units("mile/hr", "lorentz")
assert_allclose_units(v2, v.in_units("mile/hr"))
# Schwarzschild
- R = mass_sun_cgs.to_equivalent("kpc","schwarzschild")
+ R = mass_sun_cgs.in_units("kpc","schwarzschild")
assert_equal(R.in_cgs(), 2*G*mass_sun_cgs/(clight*clight))
- assert_allclose_units(mass_sun_cgs, R.to_equivalent("g", "schwarzschild"))
+ assert_allclose_units(mass_sun_cgs, R.in_units("g", "schwarzschild"))
# Compton
- l = me.to_equivalent("angstrom","compton")
+ l = me.in_units("angstrom","compton")
assert_equal(l, hcgs/(me*clight))
- assert_allclose_units(me, l.to_equivalent("g", "compton"))
+ assert_allclose_units(me, l.in_units("g", "compton"))
# Number density
rho = mp/u.cm**3
- n = rho.to_equivalent("cm**-3","number_density")
+ n = rho.in_units("cm**-3","number_density")
assert_equal(n, rho/(mh*0.6))
- assert_allclose_units(rho, n.to_equivalent("g/cm**3","number_density"))
+ assert_allclose_units(rho, n.in_units("g/cm**3","number_density"))
- n = rho.to_equivalent("cm**-3","number_density", mu=0.75)
+ n = rho.in_units("cm**-3", equivalence="number_density", mu=0.75)
assert_equal(n, rho/(mh*0.75))
- assert_allclose_units(rho, n.to_equivalent("g/cm**3","number_density", mu=0.75))
+ assert_allclose_units(rho, n.in_units("g/cm**3", equivalence="number_density", mu=0.75))
# Effective temperature
T = YTQuantity(1.0e4, "K")
- F = T.to_equivalent("erg/s/cm**2","effective_temperature")
+ F = T.in_units("erg/s/cm**2",equivalence="effective_temperature")
assert_equal(F, stefan_boltzmann_constant_cgs*T**4)
- assert_allclose_units(T, F.to_equivalent("K", "effective_temperature"))
+ assert_allclose_units(T, F.in_units("K", equivalence="effective_temperature"))
+
+ # to_value test
+
+ assert_equal(F.value, T.to_value("erg/s/cm**2", equivalence="effective_temperature"))
+ assert_equal(n.value, rho.to_value("cm**-3", equivalence="number_density", mu=0.75))
def test_electromagnetic():
from yt.units.dimensions import charge_mks, pressure, current_cgs, \
@@ -1199,19 +1215,19 @@
# Various tests of SI and CGS electromagnetic units
- qp_mks = qp.to_equivalent("C", "SI")
+ qp_mks = qp.in_units("C", "SI")
assert_equal(qp_mks.units.dimensions, charge_mks)
assert_array_almost_equal(qp_mks.v, 10.0*qp.v/speed_of_light_cm_per_s)
- qp_cgs = qp_mks.to_equivalent("esu", "CGS")
+ qp_cgs = qp_mks.in_units("esu", "CGS")
assert_array_almost_equal(qp_cgs, qp)
assert_equal(qp_cgs.units.dimensions, qp.units.dimensions)
- qp_mks_k = qp.to_equivalent("kC", "SI")
+ qp_mks_k = qp.in_units("kC", "SI")
assert_array_almost_equal(qp_mks_k.v, 1.0e-2*qp.v/speed_of_light_cm_per_s)
B = YTQuantity(1.0, "T")
- B_cgs = B.to_equivalent("gauss", "CGS")
+ B_cgs = B.in_units("gauss", "CGS")
assert_equal(B.units.dimensions, magnetic_field_mks)
assert_equal(B_cgs.units.dimensions, magnetic_field_cgs)
assert_array_almost_equal(B_cgs, YTQuantity(1.0e4, "gauss"))
@@ -1223,13 +1239,13 @@
assert_array_almost_equal(u_mks.in_cgs(), u_cgs)
I = YTQuantity(1.0, "A")
- I_cgs = I.to_equivalent("statA", "CGS")
+ I_cgs = I.in_units("statA", equivalence="CGS")
assert_array_almost_equal(I_cgs, YTQuantity(0.1*speed_of_light_cm_per_s, "statA"))
- assert_array_almost_equal(I_cgs.to_equivalent("mA", "SI"), I.in_units("mA"))
+ assert_array_almost_equal(I_cgs.in_units("mA", equivalence="SI"), I.in_units("mA"))
assert_equal(I_cgs.units.dimensions, current_cgs)
R = YTQuantity(1.0, "ohm")
- R_cgs = R.to_equivalent("statohm", "CGS")
+ R_cgs = R.in_units("statohm", "CGS")
P_mks = I*I*R
P_cgs = I_cgs*I_cgs*R_cgs
assert_equal(P_mks.units.dimensions, power)
@@ -1238,7 +1254,7 @@
assert_array_almost_equal(P_cgs.in_mks(), YTQuantity(1.0, "W"))
V = YTQuantity(1.0, "statV")
- V_mks = V.to_equivalent("V", "SI")
+ V_mks = V.in_units("V", "SI")
assert_array_almost_equal(V_mks.v, 1.0e8*V.v/speed_of_light_cm_per_s)
def test_ytarray_coercion():
https://bitbucket.org/yt_analysis/yt/commits/6952560f1d68/
Changeset: 6952560f1d68
User: ngoldbaum
Date: 2017-09-25 22:59:02+00:00
Summary: Merge pull request #1563 from jzuhone/unit_enhancement
Two small enhancements to YTArray and YTQuantity
Affected #: 5 files
diff -r 92f604cded4654e4d63653cbec574dee30d11d68 -r 6952560f1d68aee46a5910f0cafa287a01c98aa1 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
@@ -322,7 +322,20 @@
"cell_type": "markdown",
"metadata": {},
"source": [
- "Since we have a copy of the data, we can mess with it however we wish without disturbing the original data returned by the yt data object."
+ "Since we have a copy of the data, we can mess with it however we wish without disturbing the original data returned by the yt data object.\n",
+ "\n",
+ "There is yet another way to return a copy of the array data in a `YTArray` or the floating-point value of a `YTQuantity`, which also allows for the possibility to convert to different units. This is done using the `to_value` method:"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "print(dens.to_value()) # Don't change units\n",
+ "print(dens.to_value(\"Msun\")) # Change units to solar masses\n",
+ "print(dens[0].to_value(\"lbm\")) # Pick the first value and change its units to pounds"
]
},
{
@@ -388,7 +401,9 @@
{
"cell_type": "code",
"execution_count": null,
- "metadata": {},
+ "metadata": {
+ "collapsed": true
+ },
"outputs": [],
"source": [
"from astropy import units as u\n",
@@ -410,7 +425,9 @@
{
"cell_type": "code",
"execution_count": null,
- "metadata": {},
+ "metadata": {
+ "collapsed": true
+ },
"outputs": [],
"source": [
"a = np.random.random(size=10) * u.km/u.s\n",
@@ -437,7 +454,9 @@
{
"cell_type": "code",
"execution_count": null,
- "metadata": {},
+ "metadata": {
+ "collapsed": true
+ },
"outputs": [],
"source": [
"temp = dd[\"temperature\"]\n",
@@ -464,7 +483,9 @@
{
"cell_type": "code",
"execution_count": null,
- "metadata": {},
+ "metadata": {
+ "collapsed": true
+ },
"outputs": [],
"source": [
"from yt.units import kboltz\n",
@@ -496,7 +517,8 @@
"source": [
"k1 = kboltz.to_astropy()\n",
"k2 = yt.YTQuantity.from_astropy(kb)\n",
- "print (k1 == k2)"
+ "print(k1)\n",
+ "print(k2)"
]
},
{
@@ -520,7 +542,9 @@
{
"cell_type": "code",
"execution_count": null,
- "metadata": {},
+ "metadata": {
+ "collapsed": true
+ },
"outputs": [],
"source": [
"from pint import UnitRegistry\n",
@@ -542,7 +566,9 @@
{
"cell_type": "code",
"execution_count": null,
- "metadata": {},
+ "metadata": {
+ "collapsed": true
+ },
"outputs": [],
"source": [
"ptemp = temp.to_pint()"
@@ -593,9 +619,7 @@
{
"cell_type": "code",
"execution_count": null,
- "metadata": {
- "collapsed": false
- },
+ "metadata": {},
"outputs": [],
"source": [
"from yt.units import clight\n",
@@ -612,9 +636,7 @@
{
"cell_type": "code",
"execution_count": null,
- "metadata": {
- "collapsed": false
- },
+ "metadata": {},
"outputs": [],
"source": [
"from yt import YTQuantity\n",
@@ -633,7 +655,7 @@
"cell_type": "code",
"execution_count": null,
"metadata": {
- "collapsed": false
+ "collapsed": true
},
"outputs": [],
"source": [
@@ -643,9 +665,7 @@
{
"cell_type": "code",
"execution_count": null,
- "metadata": {
- "collapsed": false
- },
+ "metadata": {},
"outputs": [],
"source": [
"dd = ds.all_data()\n",
@@ -655,23 +675,23 @@
],
"metadata": {
"kernelspec": {
- "display_name": "Python 3",
+ "display_name": "Python [default]",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
- "version": 3.0
+ "version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
- "version": "3.5.1"
+ "version": "3.6.1"
}
},
"nbformat": 4,
- "nbformat_minor": 0
+ "nbformat_minor": 1
}
diff -r 92f604cded4654e4d63653cbec574dee30d11d68 -r 6952560f1d68aee46a5910f0cafa287a01c98aa1 doc/source/analyzing/units/6)_Unit_Equivalencies.ipynb
--- a/doc/source/analyzing/units/6)_Unit_Equivalencies.ipynb
+++ b/doc/source/analyzing/units/6)_Unit_Equivalencies.ipynb
@@ -18,15 +18,13 @@
"* `\"number_density\"`: conversions between density and number density ($n = \\rho/\\mu{m_p}$)\n",
"* `\"sound_speed\"`: conversions between temperature and sound speed for an ideal gas ($c_s^2 = \\gamma{k_BT}/\\mu{m_p}$)\n",
"\n",
- "A `YTArray` or `YTQuantity` can be converted to an equivalent using `to_equivalent`, where the unit and the equivalence name are provided as arguments:"
+ "A `YTArray` or `YTQuantity` can be converted to an equivalent using `in_units` (previously described in [Fields and Unit Conversion](fields_and_unit_conversion.html)), where the unit and the equivalence name are provided as additional arguments:"
]
},
{
"cell_type": "code",
"execution_count": null,
- "metadata": {
- "collapsed": false
- },
+ "metadata": {},
"outputs": [],
"source": [
"import yt\n",
@@ -37,12 +35,12 @@
"\n",
"dd = ds.all_data()\n",
"\n",
- "print (dd[\"temperature\"].to_equivalent(\"erg\", \"thermal\"))\n",
- "print (dd[\"temperature\"].to_equivalent(\"eV\", \"thermal\"))\n",
+ "print (dd[\"temperature\"].in_units(\"erg\", equivalence=\"thermal\"))\n",
+ "print (dd[\"temperature\"].in_units(\"eV\", equivalence=\"thermal\"))\n",
"\n",
"# Rest energy of the proton\n",
"from yt.units import mp\n",
- "E_p = mp.to_equivalent(\"GeV\", \"mass_energy\")\n",
+ "E_p = mp.in_units(\"GeV\", equivalence=\"mass_energy\")\n",
"print (E_p)"
]
},
@@ -56,16 +54,31 @@
{
"cell_type": "code",
"execution_count": null,
- "metadata": {
- "collapsed": false
- },
+ "metadata": {},
"outputs": [],
"source": [
"from yt.units import clight\n",
"v = 0.1*clight\n",
- "g = v.to_equivalent(\"dimensionless\", \"lorentz\")\n",
+ "g = v.in_units(\"dimensionless\", equivalence=\"lorentz\")\n",
"print (g)\n",
- "print (g.to_equivalent(\"c\", \"lorentz\"))"
+ "print (g.in_units(\"c\", equivalence=\"lorentz\"))"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "The previously described `to_value` method, which works like `in_units` except that it returns a bare NumPy array or floating-point number, also accepts equivalencies:"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "print (dd[\"temperature\"].to_value(\"erg\", equivalence=\"thermal\"))\n",
+ "print (mp.to_value(\"GeV\", equivalence=\"mass_energy\"))"
]
},
{
@@ -85,14 +98,12 @@
{
"cell_type": "code",
"execution_count": null,
- "metadata": {
- "collapsed": false
- },
+ "metadata": {},
"outputs": [],
"source": [
"print (dd[\"density\"].max())\n",
- "print (dd[\"density\"].to_equivalent(\"cm**-3\", \"number_density\").max())\n",
- "print (dd[\"density\"].to_equivalent(\"cm**-3\", \"number_density\", mu=0.75).max())"
+ "print (dd[\"density\"].in_units(\"cm**-3\", equivalence=\"number_density\").max())\n",
+ "print (dd[\"density\"].in_units(\"cm**-3\", equivalence=\"number_density\", mu=0.75).max())"
]
},
{
@@ -105,13 +116,11 @@
{
"cell_type": "code",
"execution_count": null,
- "metadata": {
- "collapsed": false
- },
+ "metadata": {},
"outputs": [],
"source": [
- "print (dd[\"temperature\"].to_equivalent(\"km/s\", \"sound_speed\").mean())\n",
- "print (dd[\"temperature\"].to_equivalent(\"km/s\", \"sound_speed\", gamma=4./3., mu=0.5).mean())"
+ "print (dd[\"temperature\"].in_units(\"km/s\", equivalence=\"sound_speed\").mean())\n",
+ "print (dd[\"temperature\"].in_units(\"km/s\", equivalence=\"sound_speed\", gamma=4./3., mu=0.5).mean())"
]
},
{
@@ -138,9 +147,7 @@
{
"cell_type": "code",
"execution_count": null,
- "metadata": {
- "collapsed": false
- },
+ "metadata": {},
"outputs": [],
"source": [
"Q1 = YTQuantity(1.0,\"C\")\n",
@@ -161,13 +168,11 @@
{
"cell_type": "code",
"execution_count": null,
- "metadata": {
- "collapsed": false
- },
+ "metadata": {},
"outputs": [],
"source": [
"from yt.units import qp # the elementary charge in esu\n",
- "qp_SI = qp.to_equivalent(\"C\",\"SI\") # convert to Coulombs\n",
+ "qp_SI = qp.in_units(\"C\", equivalence=\"SI\") # convert to Coulombs\n",
"print (qp)\n",
"print (qp_SI)"
]
@@ -182,13 +187,11 @@
{
"cell_type": "code",
"execution_count": null,
- "metadata": {
- "collapsed": false
- },
+ "metadata": {},
"outputs": [],
"source": [
"B = YTQuantity(1.0,\"T\") # magnetic field in Tesla\n",
- "print (B, B.to_equivalent(\"gauss\",\"CGS\")) # convert to Gauss"
+ "print (B, B.in_units(\"gauss\", equivalence=\"CGS\")) # convert to Gauss"
]
},
{
@@ -201,15 +204,13 @@
{
"cell_type": "code",
"execution_count": null,
- "metadata": {
- "collapsed": false
- },
+ "metadata": {},
"outputs": [],
"source": [
"I = YTQuantity(1.0,\"A\")\n",
- "I_cgs = I.to_equivalent(\"statA\",\"CGS\")\n",
+ "I_cgs = I.in_units(\"statA\", equivalence=\"CGS\")\n",
"R = YTQuantity(1.0,\"ohm\")\n",
- "R_cgs = R.to_equivalent(\"statohm\",\"CGS\")\n",
+ "R_cgs = R.in_units(\"statohm\", equivalence=\"CGS\")\n",
"P = I**2*R\n",
"P_cgs = I_cgs**2*R_cgs"
]
@@ -224,9 +225,7 @@
{
"cell_type": "code",
"execution_count": null,
- "metadata": {
- "collapsed": false
- },
+ "metadata": {},
"outputs": [],
"source": [
"print (P_cgs.units.dimensions == P.units.dimensions)\n",
@@ -250,15 +249,13 @@
{
"cell_type": "code",
"execution_count": null,
- "metadata": {
- "collapsed": false
- },
+ "metadata": {},
"outputs": [],
"source": [
"from yt.utilities.exceptions import YTInvalidUnitEquivalence\n",
"\n",
"try:\n",
- " x = v.to_equivalent(\"angstrom\", \"spectral\")\n",
+ " x = v.in_units(\"angstrom\", equivalence=\"spectral\")\n",
"except YTInvalidUnitEquivalence as e:\n",
" print (e)"
]
@@ -273,9 +270,7 @@
{
"cell_type": "code",
"execution_count": null,
- "metadata": {
- "collapsed": false
- },
+ "metadata": {},
"outputs": [],
"source": [
"print (mp.has_equivalent(\"compton\"))\n",
@@ -292,9 +287,7 @@
{
"cell_type": "code",
"execution_count": null,
- "metadata": {
- "collapsed": false
- },
+ "metadata": {},
"outputs": [],
"source": [
"E_p.list_equivalencies()"
@@ -303,7 +296,7 @@
],
"metadata": {
"kernelspec": {
- "display_name": "Python 3",
+ "display_name": "Python [default]",
"language": "python",
"name": "python3"
},
@@ -317,9 +310,9 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
- "version": "3.5.1"
+ "version": "3.6.1"
}
},
"nbformat": 4,
- "nbformat_minor": 0
+ "nbformat_minor": 1
}
diff -r 92f604cded4654e4d63653cbec574dee30d11d68 -r 6952560f1d68aee46a5910f0cafa287a01c98aa1 doc/source/analyzing/units/fields_and_unit_conversion.rst
--- a/doc/source/analyzing/units/fields_and_unit_conversion.rst
+++ b/doc/source/analyzing/units/fields_and_unit_conversion.rst
@@ -1,4 +1,4 @@
-.. _data_selection_and_fields:
+.. _fields_and_unit_conversion:
Fields and Unit Conversion
==========================
diff -r 92f604cded4654e4d63653cbec574dee30d11d68 -r 6952560f1d68aee46a5910f0cafa287a01c98aa1 yt/units/tests/test_ytarray.py
--- a/yt/units/tests/test_ytarray.py
+++ b/yt/units/tests/test_ytarray.py
@@ -991,6 +991,17 @@
for op in [operator.abs, operator.neg, operator.pos]:
unary_op_registry_comparison(op)
+def test_to_value():
+
+ a = YTArray([1.0, 2.0, 3.0], "kpc")
+ assert_equal(a.to_value(), np.array([1.0, 2.0, 3.0]))
+ assert_equal(a.to_value(), a.value)
+ assert_equal(a.to_value("km"), a.in_units("km").value)
+
+ b = YTQuantity(5.5, "Msun")
+ assert_equal(b.to_value(), 5.5)
+ assert_equal(b.to_value("g"), b.in_units("g").value)
+
@requires_module("astropy")
def test_astropy():
from yt.utilities.on_demand_imports import _astropy
@@ -1113,83 +1124,88 @@
# Mass-energy
- E = mp.to_equivalent("keV","mass_energy")
+ E = mp.in_units("keV","mass_energy")
assert_equal(E, mp*clight*clight)
- assert_allclose_units(mp, E.to_equivalent("g", "mass_energy"))
+ assert_allclose_units(mp, E.in_units("g", "mass_energy"))
# Thermal
T = YTQuantity(1.0e8,"K")
- E = T.to_equivalent("W*hr","thermal")
+ E = T.in_units("W*hr","thermal")
assert_equal(E, (kboltz*T).in_units("W*hr"))
- assert_allclose_units(T, E.to_equivalent("K", "thermal"))
+ assert_allclose_units(T, E.in_units("K", "thermal"))
# Spectral
l = YTQuantity(4000.,"angstrom")
- nu = l.to_equivalent("Hz","spectral")
+ nu = l.in_units("Hz","spectral")
assert_equal(nu, clight/l)
E = hcgs*nu
- l2 = E.to_equivalent("angstrom", "spectral")
+ l2 = E.in_units("angstrom", "spectral")
assert_allclose_units(l, l2)
nu2 = clight/l2.in_units("cm")
assert_allclose_units(nu, nu2)
- E2 = nu2.to_equivalent("keV", "spectral")
+ E2 = nu2.in_units("keV", "spectral")
assert_allclose_units(E2, E.in_units("keV"))
# Sound-speed
mu = 0.6
gg = 5./3.
- c_s = T.to_equivalent("km/s","sound_speed")
+ c_s = T.in_units("km/s", equivalence="sound_speed")
assert_equal(c_s, np.sqrt(gg*kboltz*T/(mu*mh)))
- assert_allclose_units(T, c_s.to_equivalent("K","sound_speed"))
+ assert_allclose_units(T, c_s.in_units("K","sound_speed"))
mu = 0.5
gg = 4./3.
- c_s = T.to_equivalent("km/s","sound_speed", mu=mu, gamma=gg)
+ c_s = T.in_units("km/s","sound_speed", mu=mu, gamma=gg)
assert_equal(c_s, np.sqrt(gg*kboltz*T/(mu*mh)))
- assert_allclose_units(T, c_s.to_equivalent("K","sound_speed", mu=mu, gamma=gg))
+ assert_allclose_units(T, c_s.in_units("K","sound_speed", mu=mu, gamma=gg))
# Lorentz
v = 0.8*clight
- g = v.to_equivalent("dimensionless","lorentz")
+ g = v.in_units("dimensionless","lorentz")
g2 = YTQuantity(1./np.sqrt(1.-0.8*0.8), "dimensionless")
assert_allclose_units(g, g2)
- v2 = g2.to_equivalent("mile/hr", "lorentz")
+ v2 = g2.in_units("mile/hr", "lorentz")
assert_allclose_units(v2, v.in_units("mile/hr"))
# Schwarzschild
- R = mass_sun_cgs.to_equivalent("kpc","schwarzschild")
+ R = mass_sun_cgs.in_units("kpc","schwarzschild")
assert_equal(R.in_cgs(), 2*G*mass_sun_cgs/(clight*clight))
- assert_allclose_units(mass_sun_cgs, R.to_equivalent("g", "schwarzschild"))
+ assert_allclose_units(mass_sun_cgs, R.in_units("g", "schwarzschild"))
# Compton
- l = me.to_equivalent("angstrom","compton")
+ l = me.in_units("angstrom","compton")
assert_equal(l, hcgs/(me*clight))
- assert_allclose_units(me, l.to_equivalent("g", "compton"))
+ assert_allclose_units(me, l.in_units("g", "compton"))
# Number density
rho = mp/u.cm**3
- n = rho.to_equivalent("cm**-3","number_density")
+ n = rho.in_units("cm**-3","number_density")
assert_equal(n, rho/(mh*0.6))
- assert_allclose_units(rho, n.to_equivalent("g/cm**3","number_density"))
+ assert_allclose_units(rho, n.in_units("g/cm**3","number_density"))
- n = rho.to_equivalent("cm**-3","number_density", mu=0.75)
+ n = rho.in_units("cm**-3", equivalence="number_density", mu=0.75)
assert_equal(n, rho/(mh*0.75))
- assert_allclose_units(rho, n.to_equivalent("g/cm**3","number_density", mu=0.75))
+ assert_allclose_units(rho, n.in_units("g/cm**3", equivalence="number_density", mu=0.75))
# Effective temperature
T = YTQuantity(1.0e4, "K")
- F = T.to_equivalent("erg/s/cm**2","effective_temperature")
+ F = T.in_units("erg/s/cm**2",equivalence="effective_temperature")
assert_equal(F, stefan_boltzmann_constant_cgs*T**4)
- assert_allclose_units(T, F.to_equivalent("K", "effective_temperature"))
+ assert_allclose_units(T, F.in_units("K", equivalence="effective_temperature"))
+
+ # to_value test
+
+ assert_equal(F.value, T.to_value("erg/s/cm**2", equivalence="effective_temperature"))
+ assert_equal(n.value, rho.to_value("cm**-3", equivalence="number_density", mu=0.75))
def test_electromagnetic():
from yt.units.dimensions import charge_mks, pressure, current_cgs, \
@@ -1199,19 +1215,19 @@
# Various tests of SI and CGS electromagnetic units
- qp_mks = qp.to_equivalent("C", "SI")
+ qp_mks = qp.in_units("C", "SI")
assert_equal(qp_mks.units.dimensions, charge_mks)
assert_array_almost_equal(qp_mks.v, 10.0*qp.v/speed_of_light_cm_per_s)
- qp_cgs = qp_mks.to_equivalent("esu", "CGS")
+ qp_cgs = qp_mks.in_units("esu", "CGS")
assert_array_almost_equal(qp_cgs, qp)
assert_equal(qp_cgs.units.dimensions, qp.units.dimensions)
- qp_mks_k = qp.to_equivalent("kC", "SI")
+ qp_mks_k = qp.in_units("kC", "SI")
assert_array_almost_equal(qp_mks_k.v, 1.0e-2*qp.v/speed_of_light_cm_per_s)
B = YTQuantity(1.0, "T")
- B_cgs = B.to_equivalent("gauss", "CGS")
+ B_cgs = B.in_units("gauss", "CGS")
assert_equal(B.units.dimensions, magnetic_field_mks)
assert_equal(B_cgs.units.dimensions, magnetic_field_cgs)
assert_array_almost_equal(B_cgs, YTQuantity(1.0e4, "gauss"))
@@ -1223,13 +1239,13 @@
assert_array_almost_equal(u_mks.in_cgs(), u_cgs)
I = YTQuantity(1.0, "A")
- I_cgs = I.to_equivalent("statA", "CGS")
+ I_cgs = I.in_units("statA", equivalence="CGS")
assert_array_almost_equal(I_cgs, YTQuantity(0.1*speed_of_light_cm_per_s, "statA"))
- assert_array_almost_equal(I_cgs.to_equivalent("mA", "SI"), I.in_units("mA"))
+ assert_array_almost_equal(I_cgs.in_units("mA", equivalence="SI"), I.in_units("mA"))
assert_equal(I_cgs.units.dimensions, current_cgs)
R = YTQuantity(1.0, "ohm")
- R_cgs = R.to_equivalent("statohm", "CGS")
+ R_cgs = R.in_units("statohm", "CGS")
P_mks = I*I*R
P_cgs = I_cgs*I_cgs*R_cgs
assert_equal(P_mks.units.dimensions, power)
@@ -1238,7 +1254,7 @@
assert_array_almost_equal(P_cgs.in_mks(), YTQuantity(1.0, "W"))
V = YTQuantity(1.0, "statV")
- V_mks = V.to_equivalent("V", "SI")
+ V_mks = V.in_units("V", "SI")
assert_array_almost_equal(V_mks.v, 1.0e8*V.v/speed_of_light_cm_per_s)
def test_ytarray_coercion():
diff -r 92f604cded4654e4d63653cbec574dee30d11d68 -r 6952560f1d68aee46a5910f0cafa287a01c98aa1 yt/units/yt_array.py
--- a/yt/units/yt_array.py
+++ b/yt/units/yt_array.py
@@ -589,38 +589,94 @@
"""
return self.convert_to_units(self.units.get_mks_equivalent())
- def in_units(self, units):
+ def in_units(self, units, equivalence=None, **kwargs):
"""
- Creates a copy of this array with the data in the supplied units, and
- returns it.
+ Creates a copy of this array with the data in the supplied
+ units, and returns it.
+
+ Optionally, an equivalence can be specified to convert to an
+ equivalent quantity which is not in the same dimensions.
+
+ .. note::
+
+ All additional keyword arguments are passed to the
+ equivalency, which should be used if that particular
+ equivalency requires them.
Parameters
----------
units : Unit object or string
The units you want to get a new quantity in.
+ equivalence : string, optional
+ The equivalence you wish to use. To see which
+ equivalencies are supported for this unitful
+ quantity, try the :meth:`list_equivalencies`
+ method. Default: None
Returns
-------
YTArray
+ """
+ if equivalence is None:
+ new_units = _unit_repr_check_same(self.units, units)
+ (conversion_factor, offset) = self.units.get_conversion_factor(new_units)
- """
- new_units = _unit_repr_check_same(self.units, units)
- (conversion_factor, offset) = self.units.get_conversion_factor(new_units)
-
- new_array = type(self)(self.ndview * conversion_factor, new_units)
+ new_array = type(self)(self.ndview * conversion_factor, new_units)
- if offset:
- np.subtract(new_array, offset*new_array.uq, new_array)
+ if offset:
+ np.subtract(new_array, offset*new_array.uq, new_array)
- return new_array
+ return new_array
+ else:
+ return self.to_equivalent(units, equivalence, **kwargs)
- def to(self, units):
+ def to(self, units, equivalence=None, **kwargs):
"""
An alias for YTArray.in_units().
See the docstrings of that function for details.
"""
- return self.in_units(units)
+ return self.in_units(units, equivalence=equivalence, **kwargs)
+
+ def to_value(self, units=None, equivalence=None, **kwargs):
+ """
+ Creates a copy of this array with the data in the supplied
+ units, and returns it without units. Output is therefore a
+ bare NumPy array.
+
+ Optionally, an equivalence can be specified to convert to an
+ equivalent quantity which is not in the same dimensions.
+
+ .. note::
+
+ All additional keyword arguments are passed to the
+ equivalency, which should be used if that particular
+ equivalency requires them.
+
+ Parameters
+ ----------
+ units : Unit object or string, optional
+ The units you want to get the bare quantity in. If not
+ specified, the value will be returned in the current units.
+
+ equivalence : string, optional
+ The equivalence you wish to use. To see which
+ equivalencies are supported for this unitful
+ quantity, try the :meth:`list_equivalencies`
+ method. Default: None
+
+ Returns
+ -------
+ NumPy array
+ """
+ if units is None:
+ v = self.value
+ else:
+ v = self.in_units(units, equivalence=equivalence, **kwargs).value
+ if isinstance(self, YTQuantity):
+ return float(v)
+ else:
+ return v
def in_base(self, unit_system="cgs"):
"""
@@ -684,6 +740,8 @@
>>> a.to_equivalent("keV", "thermal")
"""
conv_unit = Unit(unit, registry=self.units.registry)
+ if self.units.same_dimensions_as(conv_unit):
+ return self.in_units(conv_unit)
this_equiv = equivalence_registry[equiv]()
oneway_or_equivalent = (
conv_unit.has_equivalent(equiv) or this_equiv._one_way)
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