Skip to content

Commit

Permalink
initial skeleton for loading sample data
Browse files Browse the repository at this point in the history
  • Loading branch information
chrishavlin committed Oct 22, 2024
1 parent f6b653c commit e1beb32
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 6 deletions.
17 changes: 16 additions & 1 deletion src/yt_napari/_ds_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@
from yt_napari.logging import ytnapari_log


def _load_sample(filename):
# TODO: check for pooch, pandas.
# TODO: catch key errors, etc.
ds = yt.load_sample(filename)
return ds

Check warning on line 16 in src/yt_napari/_ds_cache.py

View check run for this annotation

Codecov / codecov/patch

src/yt_napari/_ds_cache.py#L15-L16

Added lines #L15 - L16 were not covered by tests


class DatasetCache:
def __init__(self):
self.available = {}
Expand Down Expand Up @@ -53,7 +60,15 @@ def check_then_load(self, filename: str, cache_if_not_found: bool = True):
ds_callable = getattr(_special_loaders, callable_name)
ds = ds_callable()
else:
ds = yt.load(filename)
# TODO: have this sample files registry come from yt,
# just setting up the napari side for now. Should
# also add a config option maybe to handle name
# conflicts between sample files and local files?
sample_files = ["IsolatedGalaxy"]
if filename in sample_files:
ds = _load_sample(filename)

Check warning on line 69 in src/yt_napari/_ds_cache.py

View check run for this annotation

Codecov / codecov/patch

src/yt_napari/_ds_cache.py#L69

Added line #L69 was not covered by tests
else:
ds = yt.load(filename)

if ytcfg.get("yt_napari", "in_memory_cache") and cache_if_not_found:
self.add_ds(ds, filename)
Expand Down
17 changes: 12 additions & 5 deletions src/yt_napari/_model_ingestor.py
Original file line number Diff line number Diff line change
Expand Up @@ -758,14 +758,13 @@ def _process_validated_model(
return layer_list, timeseries_layers


def load_from_json(json_paths: List[str]) -> List[Layer]:
def load_from_json_strs(json_strs: List[str]) -> List[Layer]:
layer_lists = [] # we will concatenate layers across json paths
timeseries_layers = [] # timeseries layers handled separately
for json_path in json_paths:
for json_str in json_strs:
# InputModel is a pydantic class, the following will validate the json
with open(json_path, "r") as open_file:
model = InputModel.model_validate_json(open_file.read())

model = InputModel.model_validate_json(json_str)
print(model)
# now that we have a validated model, we can use the model attributes
# to execute the code that will return our array for the image
layer_lists_j, timeseries_layers_j = _process_validated_model(model)
Expand All @@ -785,6 +784,14 @@ def load_from_json(json_paths: List[str]) -> List[Layer]:
return out_layers


def load_from_json(json_paths: List[str]) -> List[Layer]:
json_strs = [] # list of json strings
for json_path in json_paths:
with open(json_path, "r") as open_file:
json_strs.append(open_file.read())
return load_from_json_strs(json_strs)


def _choose_ref_layer(
layer_list: List[SpatialLayer], method: Optional[str] = "first_in_list"
) -> ReferenceLayer:
Expand Down
50 changes: 50 additions & 0 deletions src/yt_napari/_sample_data.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import json

Check warning on line 1 in src/yt_napari/_sample_data.py

View check run for this annotation

Codecov / codecov/patch

src/yt_napari/_sample_data.py#L1

Added line #L1 was not covered by tests

from yt_napari import __version__
from yt_napari._model_ingestor import load_from_json_strs

Check warning on line 4 in src/yt_napari/_sample_data.py

View check run for this annotation

Codecov / codecov/patch

src/yt_napari/_sample_data.py#L3-L4

Added lines #L3 - L4 were not covered by tests

isogaldict = {

Check warning on line 6 in src/yt_napari/_sample_data.py

View check run for this annotation

Codecov / codecov/patch

src/yt_napari/_sample_data.py#L6

Added line #L6 was not covered by tests
"datasets": [
{
"filename": "IsolatedGalaxy",
"selections": {
"regions": [
{
"fields": [
{
"field_name": "Density",
"field_type": "enzo",
"take_log": True,
}
],
"left_edge": {
"value": [
0.4,
0.4,
0.4,
],
"unit": "Mpc",
},
"right_edge": {
"value": [
0.6,
0.6,
0.6,
],
"unit": "Mpc",
},
"resolution": [400, 400, 400],
},
]
},
"edge_units": "Mpc",
}
]
}


def isogal():
isogaldict["$schema"] = f"yt-napari_{__version__}.json"
json_objs = [json.dumps(isogaldict)]
result = load_from_json_strs(json_objs)
return result

Check warning on line 50 in src/yt_napari/_sample_data.py

View check run for this annotation

Codecov / codecov/patch

src/yt_napari/_sample_data.py#L46-L50

Added lines #L46 - L50 were not covered by tests
7 changes: 7 additions & 0 deletions src/yt_napari/napari.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ contributions:
- id: yt-napari.metadata_widget
title: Inspect the metadata for a yt dataset
python_name: yt_napari._widget_matadata:MetadataWidget
- id: yt-napari.data.isogal
title: Load Isolated Galaxy
python_name: yt_napari._sample_data:isogal
readers:
- command: yt-napari.get_reader
accepts_directories: false
Expand All @@ -25,3 +28,7 @@ contributions:
display_name: yt Time Series Reader
- command: yt-napari.metadata_widget
display_name: yt Metadata Explorer
sample_data:
- key: isogal
display_name: IsolatedGalaxy
command: yt-napari.data.isogal

0 comments on commit e1beb32

Please sign in to comment.