-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #46 from chrishavlin/ds_caching
dataset cache
- Loading branch information
Showing
16 changed files
with
541 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
{ | ||
"title": "InputModel", | ||
"type": "object", | ||
"properties": { | ||
"data": { | ||
"title": "Data", | ||
"description": "list of datasets to load", | ||
"type": "array", | ||
"items": { | ||
"$ref": "#/definitions/DataContainer" | ||
} | ||
} | ||
}, | ||
"definitions": { | ||
"ytField": { | ||
"title": "ytField", | ||
"type": "object", | ||
"properties": { | ||
"field_type": { | ||
"title": "Field Type", | ||
"description": "a field type in the yt dataset", | ||
"type": "string" | ||
}, | ||
"field_name": { | ||
"title": "Field Name", | ||
"description": "a field in the yt dataset", | ||
"type": "string" | ||
}, | ||
"take_log": { | ||
"title": "Take Log", | ||
"description": "if true, will apply log10 to the selected data", | ||
"default": true, | ||
"type": "boolean" | ||
} | ||
} | ||
}, | ||
"SelectionObject": { | ||
"title": "SelectionObject", | ||
"type": "object", | ||
"properties": { | ||
"fields": { | ||
"title": "Fields", | ||
"description": "list of fields to load for this selection", | ||
"type": "array", | ||
"items": { | ||
"$ref": "#/definitions/ytField" | ||
} | ||
}, | ||
"left_edge": { | ||
"title": "Left Edge", | ||
"description": "the left edge (min x, min y, min z) in units of edge_units", | ||
"default": [ | ||
0.0, | ||
0.0, | ||
0.0 | ||
], | ||
"type": "array", | ||
"minItems": 3, | ||
"maxItems": 3, | ||
"items": [ | ||
{ | ||
"type": "number" | ||
}, | ||
{ | ||
"type": "number" | ||
}, | ||
{ | ||
"type": "number" | ||
} | ||
] | ||
}, | ||
"right_edge": { | ||
"title": "Right Edge", | ||
"description": "the right edge (max x, max y, max z) in units of edge_units", | ||
"default": [ | ||
1.0, | ||
1.0, | ||
1.0 | ||
], | ||
"type": "array", | ||
"minItems": 3, | ||
"maxItems": 3, | ||
"items": [ | ||
{ | ||
"type": "number" | ||
}, | ||
{ | ||
"type": "number" | ||
}, | ||
{ | ||
"type": "number" | ||
} | ||
] | ||
}, | ||
"resolution": { | ||
"title": "Resolution", | ||
"description": "the resolution at which to sample between the edges.", | ||
"default": [ | ||
400, | ||
400, | ||
400 | ||
], | ||
"type": "array", | ||
"minItems": 3, | ||
"maxItems": 3, | ||
"items": [ | ||
{ | ||
"type": "integer" | ||
}, | ||
{ | ||
"type": "integer" | ||
}, | ||
{ | ||
"type": "integer" | ||
} | ||
] | ||
} | ||
} | ||
}, | ||
"DataContainer": { | ||
"title": "DataContainer", | ||
"type": "object", | ||
"properties": { | ||
"filename": { | ||
"title": "Filename", | ||
"description": "the filename for the dataset", | ||
"type": "string" | ||
}, | ||
"selections": { | ||
"title": "Selections", | ||
"description": "list of selections to load in this dataset", | ||
"type": "array", | ||
"items": { | ||
"$ref": "#/definitions/SelectionObject" | ||
} | ||
}, | ||
"edge_units": { | ||
"title": "Edge Units", | ||
"description": "the units to use for left_edge and right_edge in the selections", | ||
"default": "code_length", | ||
"type": "string" | ||
}, | ||
"store_in_cache": { | ||
"title": "Store In Cache", | ||
"description": "if enabled, will store references to yt datasets to avoid reloading.", | ||
"default": true, | ||
"type": "boolean" | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,23 @@ | ||
# copies over schema files to the docs/_static folder and updates the schema.rst | ||
import argparse | ||
|
||
from yt_napari._data_model import _store_schema | ||
from yt_napari.schemas._manager import Manager | ||
|
||
|
||
def run_update(source_dir, schema_dir): | ||
|
||
m = Manager(schema_dir) | ||
m.update_docs(source_dir) | ||
|
||
|
||
if __name__ == "__main__": | ||
parser = argparse.ArgumentParser() | ||
parser.add_argument( | ||
"-v", "--version", type=str, default=None, help="the schema version to write" | ||
) | ||
|
||
args = parser.parse_args() | ||
if args.version is not None: | ||
_store_schema(version=args.version, overwrite_version=True) | ||
run_update("./docs", "./src/yt_napari/schemas") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,4 +8,5 @@ sphinx-jsonschema<1.19.0 | |
Jinja2<3.1.0 | ||
magicgui | ||
pytest-qt | ||
platformdirs | ||
-e . |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import yt | ||
|
||
from yt_napari.config import ytcfg | ||
from yt_napari.logging import ytnapari_log | ||
|
||
|
||
class DatasetCache: | ||
def __init__(self): | ||
self.available = {} | ||
self._most_recent: str = None | ||
|
||
def add_ds(self, ds, name: str): | ||
if name in self.available: | ||
ytnapari_log.warning(f"A dataset already exists for {name}. Overwriting.") | ||
self.available[name] = ds | ||
self._most_recent = name | ||
|
||
@property | ||
def most_recent(self): | ||
if self._most_recent is not None: | ||
return self.available[self._most_recent] | ||
return None | ||
|
||
def get_ds(self, name: str): | ||
if self.exists(name): | ||
return self.available[name] | ||
ytnapari_log.warning(f"{name} not found in cache.") | ||
return None | ||
|
||
def exists(self, name: str) -> bool: | ||
return name in self.available | ||
|
||
def rm_ds(self, name: str): | ||
self.available.pop(name, None) | ||
|
||
def rm_all(self): | ||
self.available = {} | ||
self._most_recent = None | ||
|
||
def check_then_load(self, filename: str): | ||
if self.exists(filename): | ||
ytnapari_log.info(f"loading {filename} from cache.") | ||
return self.get_ds(filename) | ||
else: | ||
ds = yt.load(filename) | ||
if ytcfg.get("yt_napari", "in_memory_cache"): | ||
self.add_ds(ds, filename) | ||
return ds | ||
|
||
|
||
dataset_cache = DatasetCache() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
def test_config_update(): | ||
|
||
# note: when the following import is at the top of the file, it errors: | ||
# UnboundLocalError: local variable 'ytcfg' referenced before assignment | ||
from yt_napari.config import _defaults, _get_updated_config, ytcfg | ||
|
||
current_vals = {} | ||
for setting in _defaults.keys(): | ||
current_vals[setting] = ytcfg.get("yt_napari", setting) | ||
|
||
ytcfg.remove_section("yt_napari") | ||
ytcfg = _get_updated_config(ytcfg) | ||
assert ytcfg.has_section("yt_napari") | ||
for setting, val in _defaults.items(): | ||
assert val == ytcfg.get("yt_napari", setting) | ||
|
||
# run it through again, make sure existing values are preserved | ||
ytcfg.set("yt_napari", "in_memory_cache", False) # (default is True) | ||
ytcfg = _get_updated_config(ytcfg) | ||
assert ytcfg.get("yt_napari", "in_memory_cache") is False | ||
|
||
# remove it and add a blank so that the defaults get applied | ||
ytcfg.remove_section("yt_napari") | ||
ytcfg.add_section("yt_napari") | ||
ytcfg = _get_updated_config(ytcfg) | ||
for setting, val in _defaults.items(): | ||
assert val == ytcfg.get("yt_napari", setting) | ||
|
||
# make sure our settings get reset to what they were coming in... | ||
ytcfg.update({"yt_napari": current_vals}) |
Oops, something went wrong.