Skip to content

Commit

Permalink
improve teset coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
chrishavlin committed Jul 7, 2023
1 parent c5e5c7f commit fe85281
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 26 deletions.
4 changes: 0 additions & 4 deletions src/yt_napari/_model_ingestor.py
Original file line number Diff line number Diff line change
Expand Up @@ -380,8 +380,6 @@ def _process_slice(
def _load_2D_slices(ds, m_data: DataContainer, layer_list: list) -> list:

for slice in m_data.selections.slices:
if slice.normal == "":
continue

if slice.center is None:
c = None
Expand Down Expand Up @@ -472,10 +470,8 @@ def _choose_ref_layer(
) -> ReferenceLayer:
# decides on which layer to use as the reference
if method == "first_in_list":
print("using first in list")
ref_layer_id = 0
elif method == "smallest_volume":
print("small vol")
min_vol = None
for layer_id, layer in enumerate(layer_list):
ld = layer[3] # the layer domain
Expand Down
31 changes: 31 additions & 0 deletions src/yt_napari/_tests/test_model_ingestor.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,3 +208,34 @@ def test_metadata():
fake_data = np.ones((10, 10))
md = _mi.create_metadata_dict(fake_data, layer_domain, True, a="a")
assert isinstance(md, dict)


def test_ref_layer_selection(domains_to_test):
# assemble some fake layer tuples
im_type = "image"

spatial_layer_list = []
for d in domains_to_test.domain_sets:
layer_domain = _mi.LayerDomain(d.left_edge, d.right_edge, d.resolution)
im = np.random.random(d.resolution)
spatial_layer_list.append((im, {}, im_type, layer_domain))

# add another small volume layer
le = unyt.unyt_array([0.1, 0.1, 0.1], "m")
re = unyt.unyt_array([0.2, 0.2, 0.2], "m")
res = (3, 4, 5)
small_vol_domain = _mi.LayerDomain(le, re, resolution=res)
im = np.random.random(res)
spatial_layer_list.append((im, {}, im_type, small_vol_domain))

first_ref = _mi._choose_ref_layer(spatial_layer_list, method="first_in_list")
expected_ref = spatial_layer_list[0][3]
assert np.all(first_ref.left_edge == expected_ref.left_edge)
assert np.all(first_ref.right_edge == expected_ref.right_edge)

smal_ref = _mi._choose_ref_layer(spatial_layer_list, method="smallest_volume")
assert np.all(small_vol_domain.left_edge == smal_ref.left_edge)
assert np.all(small_vol_domain.right_edge == smal_ref.right_edge)

with pytest.raises(ValueError, match="method must be one of"):
_ = _mi._choose_ref_layer(spatial_layer_list, method="not_a_method")
69 changes: 47 additions & 22 deletions src/yt_napari/_tests/test_slices_json.py
Original file line number Diff line number Diff line change
@@ -1,33 +1,58 @@
import pytest

from yt_napari._data_model import InputModel
from yt_napari._model_ingestor import _choose_ref_layer, _process_validated_model

jdict = {
"$schema": "yt-napari_0.0.2.json",
"data": [
{
"filename": "_ytnapari_load_grid",
"selections": {
"slices": [
{
"fields": [{"field_name": "density", "field_type": "gas"}],
"resolution": [400, 400],
"normal": "x",
"slice_width": {"value": 0.25, "unit": "code_length"},
"slice_height": {"value": 0.25, "unit": "code_length"},
"center": {"value": [0.5, 0.5, 0.5], "unit": "code_length"},
}
]
},
}
],
}
jdicts = []
jdicts.append(
{
"$schema": "yt-napari_0.0.2.json",
"data": [
{
"filename": "_ytnapari_load_grid",
"selections": {
"slices": [
{
"fields": [{"field_name": "density", "field_type": "gas"}],
"resolution": [400, 400],
"normal": "x",
"slice_width": {"value": 0.25, "unit": "code_length"},
"slice_height": {"value": 0.25, "unit": "code_length"},
"center": {"value": [0.5, 0.5, 0.5], "unit": "code_length"},
}
]
},
}
],
}
)
jdicts.append(
{
"$schema": "yt-napari_0.0.2.json",
"data": [
{
"filename": "_ytnapari_load_grid",
"selections": {
"slices": [
{
"fields": [{"field_name": "density", "field_type": "gas"}],
"normal": "x",
}
]
},
}
],
}
)


def test_basic_slice_validation():
@pytest.mark.parametrize("jdict", jdicts)
def test_basic_slice_validation(jdict):
_ = InputModel.parse_obj(jdict)


def test_slice_load(yt_ugrid_ds_fn):
@pytest.mark.parametrize("jdict", jdicts)
def test_slice_load(yt_ugrid_ds_fn, jdict):
im = InputModel.parse_obj(jdict)
layer_lists = _process_validated_model(im)
ref_layer = _choose_ref_layer(layer_lists)
Expand Down
2 changes: 2 additions & 0 deletions src/yt_napari/_tests/test_widget_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ def test_widget_reader_add_selections(make_napari_viewer, yt_ugrid_ds_fn):
sel = list(r.active_selections.values())[0]
assert isinstance(sel, SelectionEntry)
assert sel.selection_type == "Region"
sel.expand()
sel.expand()
r.layer_deletion_button.click()
assert len(r.active_selections) == 0

Expand Down

0 comments on commit fe85281

Please sign in to comment.