Skip to content

Commit

Permalink
Merge branch 'master' into issue_169
Browse files Browse the repository at this point in the history
  • Loading branch information
l-johnston authored Oct 20, 2020
2 parents 827f912 + aaa2b24 commit f427623
Show file tree
Hide file tree
Showing 8 changed files with 96 additions and 19 deletions.
22 changes: 22 additions & 0 deletions HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,28 @@
History
=======

2.8.0 (2020-10-05)
------------------

* Dropped support for Python 3.5.
* Add ``delta_degC`` and ``delta_degF`` units to support temperature difference
arithmetic. See `PR #152
<https://github.com/yt-project/unyt/pull/152>`_. Thank you to Lee Johnston
(@l-johnston on GitHub) for the contribution.
* Fix an issue where a subsequent load of the unit registry with units that are
equal but not identical leads to a crash. See `PR #158
<https://github.com/yt-project/unyt/pull/158>`_. Thank you to Matthew Turk
(@matthewturk on GitHub) for the initial bug report and fix.
* Add force unit ``kip`` and pressure unit ``psi``. Thank you to P. Talley
(@otaithleigh on GitHub) for the contribution. See `PR #162
<https://github.com/yt-project/unyt/pull/162>`_.
* Fix an issue where arithmetic operations on units defined in different
registries and having the conversion defined in one direction would lead to a
crash. See `PR #164 <https://github.com/yt-project/unyt/pull/164>`_. Thank
you to Clément Robert (@neutrinoceros on GitHub) for the initial bug report
and fix.


2.7.2 (2020-06-29)
------------------

Expand Down
1 change: 0 additions & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ universal = 1
test = pytest

[tool:pytest]
collect_ignore = ['setup.py']
norecursedirs = benchmarks paper .*

[versioneer]
Expand Down
9 changes: 5 additions & 4 deletions tox.ini
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
[tox]
envlist = py36-docs,begin,py35-dependencies,py35-versions,py{35,36,37,38},py38-unyt-module-test-function,end
envlist = py36-docs,begin,py36-dependencies,py36-versions,py{36,37,38},py38-unyt-module-test-function,end

[travis]
python =
3.8: py38, py38-unyt-module-test-function
3.7: py37
3.6: py36, py36-docs
3.5: py35, py35-dependencies, py35-versions

[testenv]
setenv =
Expand All @@ -33,8 +32,9 @@ commands =
pytest --cov=unyt --cov-append --doctest-modules --doctest-plus --doctest-rst --basetemp={envtmpdir} -W once
coverage report --omit='.tox/*'

[testenv:py35-versions]
[testenv:py36-versions]
deps =
docutils
pytest
sympy==1.2
numpy==1.13.3
Expand All @@ -50,8 +50,9 @@ commands =
pytest --cov=unyt --cov-append --basetemp={envtmpdir} -W once
coverage report --omit='.tox/*'

[testenv:py35-dependencies]
[testenv:py36-dependencies]
deps =
docutils
pytest
sympy
numpy
Expand Down
9 changes: 4 additions & 5 deletions unyt/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -843,8 +843,8 @@ def in_units(self, units, equivalence=None, **kwargs):
RuntimeWarning,
stacklevel=2,
)
new_dtype = np.dtype("f" + str(dsize))
conversion_factor = new_dtype.type(conversion_factor)
new_dtypekind = "c" if self.dtype.kind == "c" else "f"
new_dtype = np.dtype(new_dtypekind + str(dsize))
ret = np.asarray(self.ndview * conversion_factor, dtype=new_dtype)
if offset:
np.subtract(ret, offset, ret)
Expand Down Expand Up @@ -982,8 +982,6 @@ def in_base(self, unit_system=None):
else:
to_units = self.units.get_base_equivalent(unit_system)
conv, offset = self.units.get_conversion_factor(to_units, self.dtype)
new_dtype = np.dtype("f" + str(self.dtype.itemsize))
conv = new_dtype.type(conv)
ret = self.v * conv
if offset:
ret = ret - offset
Expand Down Expand Up @@ -2334,7 +2332,8 @@ def loadtxt(fname, dtype="float", delimiter="\t", usecols=None, comments="#"):
# Here we catch the first line of numbers
col_words = line.strip().split(delimiter)
for word in col_words:
float(word)
# test that word can be converted to a number
complex(word)
num_cols = len(col_words)
break
f.close()
Expand Down
8 changes: 4 additions & 4 deletions unyt/mpl_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,11 +178,11 @@ def __call__(self):
@property
def label_style(self):
"""str: One of the following set, ``{'()', '[]', '/'}``.
These choices correspond to the following unit labels:
These choices correspond to the following unit labels:
* ``'()'`` -> ``'(unit)'``
* ``'[]'`` -> ``'[unit]'``
* ``'/'`` -> ``'q_x / unit'``
* ``'()'`` -> ``'(unit)'``
* ``'[]'`` -> ``'[unit]'``
* ``'/'`` -> ``'q_x / unit'``
"""
return self._labelstyle

Expand Down
10 changes: 10 additions & 0 deletions unyt/tests/test_units.py
Original file line number Diff line number Diff line change
Expand Up @@ -858,6 +858,16 @@ def test_degC():
assert str(a) == "1 °C"


def test_degC_with_SIprefixes():
assert_allclose_units(1 * Unit("mdegC"), 0.001 * Unit("degC"))
assert_allclose_units(1 * Unit("degC"), 1000 * Unit("mdegC"))
assert_allclose_units(73 * Unit("degF"), 22777.779 * Unit("mdegC"))
assert_allclose_units(22777.779 * Unit("mdegC"), 73 * Unit("degF"))
assert_allclose_units(22777.779 * Unit("mdegC"), 532.67 * Unit("R"))
assert_allclose_units(1 * Unit("mK"), -273149.0 * Unit("mdegC"))
assert_allclose_units(1 * Unit("mdegC"), 273151.0 * Unit("mK"))


def test_delta_degC():
a = 1 * Unit("delta_degC")
assert str(a) == "1 Δ°C"
Expand Down
34 changes: 34 additions & 0 deletions unyt/tests/test_unyt_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -2447,3 +2447,37 @@ def test_masked_array():
assert_array_equal(np.ma.compressed(marr_masked), unyt_array([1, 2], "s"))
# executing the repr should not raise an exception
marr.__repr__()


def test_complexvalued():
freq = unyt_array([1j, 1j * 10], "Hz")
arr = 1 / (Unit("F") * Unit("Ω") * freq)
arr = arr.to("dimensionless")
assert arr.units.is_dimensionless
assert np.all(arr.v == np.asarray([-1j, -1j * 0.1]))
arr = unyt_array([1j, 1j * 10], "mJ")
arr.convert_to_base()
assert_allclose_units(arr, unyt_array([1j * 0.001, 1j * 0.01], "J"))
arr.convert_to_units("mJ")
assert_allclose_units(arr, unyt_array([1j, 1j * 10], "mJ"))
arr.convert_to_mks()
assert_allclose_units(arr, unyt_array([1j * 0.001, 1j * 0.01], "J"))
arr.convert_to_cgs()
assert_allclose_units(arr, unyt_array([1j * 10000, 1j * 100000], "erg"))
arr.convert_to_equivalent("K", "thermal")
assert_allclose_units(
arr, unyt_array([1j * 7.24297157e19, 1j * 7.24297157e20], "K")
)
arr = arr.to_equivalent("J", "thermal")
assert_allclose_units(arr, unyt_array([1j * 0.001, 1j * 0.01], "J"))
assert_allclose_units(arr.to_ndarray(), np.asarray([1j * 0.001, 1j * 0.01]))
assert_allclose_units(arr.to_value(), np.asarray([1j * 0.001, 1j * 0.01]))
assert arr.tolist() == [1j * 0.001, 1j * 0.01]
assert_allclose_units(arr.in_units("mJ"), unyt_array([1j, 1j * 10], "mJ"))
assert_allclose_units(arr.in_base(), unyt_array([1j * 0.001, 1j * 0.01], "J"))
assert_allclose_units(arr.in_cgs(), unyt_array([1j * 10000, 1j * 100000], "erg"))
assert_allclose_units(arr.in_mks(), unyt_array([1j * 0.001, 1j * 0.01], "J"))
file = "test_complexvalued.txt"
savetxt(file, arr)
farr = loadtxt(file, dtype=np.complex128)
assert_allclose_units(farr, unyt_array([1j * 0.001, 1j * 0.01], "J"))
22 changes: 17 additions & 5 deletions unyt/unit_object.py
Original file line number Diff line number Diff line change
Expand Up @@ -936,13 +936,25 @@ def _get_conversion_factor(old_units, new_units, dtype):
raise UnitConversionError(
old_units, old_units.dimensions, new_units, new_units.dimensions
)
ratio = old_units.base_value / new_units.base_value
if old_units.base_offset == 0 and new_units.base_offset == 0:
old_basevalue = old_units.base_value
old_baseoffset = old_units.base_offset
new_basevalue = new_units.base_value
new_baseoffset = new_units.base_offset
ratio = old_basevalue / new_basevalue
if old_baseoffset == 0 and new_baseoffset == 0:
return (ratio, None)
else:
# the dimensions are the same, so both are temperatures, where
# it's legal to convert units so no need to do error checking
return ratio, ratio * old_units.base_offset - new_units.base_offset
# the dimensions are either temperature or angle (lat, lon)
if old_units.dimensions == temperature:
# for degree Celsius, back out the SI prefix scaling from
# offset scaling for degree Fahrenheit
old_prefix, _ = _split_prefix(str(old_units), old_units.registry.lut)
if old_prefix != "":
old_baseoffset /= old_basevalue
new_prefix, _ = _split_prefix(str(new_units), new_units.registry.lut)
if new_prefix != "":
new_baseoffset /= new_basevalue
return ratio, ratio * old_baseoffset - new_baseoffset


#
Expand Down

0 comments on commit f427623

Please sign in to comment.