Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix silenced unit conversion bugs #2897

2 changes: 2 additions & 0 deletions yt/data_objects/tests/test_numpy_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ def test_mean_sum_integrate():


def test_min_max():
fields = ("density", "temperature")
units = ("g/cm**3", "K")
for nprocs in [-1, 1, 2, 16]:
fields = ["density", "temperature"]
units = ["g/cm**3", "K"]
Expand Down
3 changes: 1 addition & 2 deletions yt/fields/field_info_container.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
from typing import Optional, Tuple

import numpy as np
from unyt.exceptions import UnitConversionError

from yt._maintenance.deprecation import issue_deprecation_warning
from yt.fields.field_exceptions import NeedsConfiguration
Expand Down Expand Up @@ -588,7 +587,7 @@ def check_derived_fields(self, fields_to_check=None):
AttributeError,
KeyError,
# code smells -> those are very likely bugs
UnitConversionError, # solved in GH PR 2897 ?
# UnitConversionError, # solved in GH PR 2897 ?
# RecursionError is clearly a bug, and was already solved once
# in GH PR 2851
RecursionError,
Expand Down
12 changes: 11 additions & 1 deletion yt/fields/fluid_fields.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import numpy as np
from unyt.exceptions import UnitConversionError

from yt.funcs import mylog
from yt.geometry.geometry_handler import is_curvilinear
Expand Down Expand Up @@ -172,7 +173,16 @@ def _pressure(field, data):
)

def _kT(field, data):
return (pc.kboltz * data[ftype, "temperature"]).in_units("keV")
ret = pc.kboltz * data[ftype, "temperature"]
try:
return ret.in_units("keV")
except UnitConversionError as err:
# Chances are this is a non-weighted projection object and the
# 'temperature' field is expressed in K*cm
# see GH PR 2897
raise NotImplementedError(
"Can not define a kT field if temperature can not be converted to 'K'. "
) from err

registry.add_field(
(ftype, "kT"),
Expand Down
8 changes: 4 additions & 4 deletions yt/fields/vector_operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,10 +170,10 @@ def create_vector_fields(registry, basename, field_units, ftype="gas", slice_inf
xn, yn, zn = [(ftype, f"{basename}_{ax}") for ax in "xyz"]

# Is this safe?
if registry.ds.dimensionality < 3:
zn = ("index", "zeros")
if registry.ds.dimensionality < 2:
yn = ("index", "zeros")
# if registry.ds.dimensionality < 3:
# zn = ("index", "zeros")
# if registry.ds.dimensionality < 2:
# yn = ("index", "zeros")

create_relative_field(
registry,
Expand Down
16 changes: 14 additions & 2 deletions yt/testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@
import shutil
import tempfile
import unittest
import warnings

import matplotlib
import numpy as np
from more_itertools import always_iterable
from more_itertools import always_iterable, zip_equal
from numpy.random import RandomState
from unyt.exceptions import UnitOperationError

Expand Down Expand Up @@ -228,6 +229,17 @@ def fake_random_ds(
):
from yt.loaders import load_uniform_grid

if fields is None:
fields = ("density", "velocity_x", "velocity_y", "velocity_z")
if units is not None:
warnings.warn(
"Received `units` keyword argument but no `fields`, "
"ignoring `units`."
)
units = ("g/cm**3", "cm/s", "cm/s", "cm/s")
if units is None:
raise ValueError("missing required keyword argument `units`.")

prng = RandomState(0x4D3D3D3)
if not is_sequence(ndims):
ndims = [ndims, ndims, ndims]
Expand Down Expand Up @@ -259,7 +271,7 @@ def fake_random_ds(
else:
offsets.append(0.0)
data = {}
for field, offset, u in zip(fields, offsets, units):
for field, offset, u in zip_equal(fields, offsets, units):
v = (prng.random_sample(ndims) - offset) * peak_value
if field[0] == "all":
v = v.ravel()
Expand Down
1 change: 0 additions & 1 deletion yt/utilities/lib/tests/test_geometry_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

_fields = ("density", "velocity_x", "velocity_y", "velocity_z")
_units = ("g/cm**3", "cm/s", "cm/s", "cm/s")

# TODO: error compact/spread bits for incorrect size
# TODO: test msdb for [0,0], [1,1], [2,2] etc.

Expand Down
1 change: 1 addition & 0 deletions yt/visualization/volume_rendering/tests/test_lenses.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ def setUp(self):
self.curdir, self.tmpdir = None, None

self.field = ("gas", "density")
# TODO: Check this (I wrote this as self.field[1] in an earlier draft)
self.ds = fake_random_ds(32, fields=(self.field,), units=("g/cm**3",))
self.ds.index

Expand Down