-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move dataset loading methods out into a single-dispatch function (#274)
* Move dataset loading methods out into a single-dispatch function It's a bit messy at the moment, will want some tidying up * Update more loading commands that I somehow missed * Forgot to commit the actual loader because I'm a fool * Changelog * Found another old call hiding in the docs * Swap one no-op for another to appease the coverage gods * Code is now constructed so that this will never run * Add basic test for loading TiledDataset * Use a test file that actually exists. Maybe that will help * Update dkist/dataset/loader.py Co-authored-by: Stuart Mumford <[email protected]> * Update dkist/dataset/loader.py Co-authored-by: Stuart Mumford <[email protected]> * Revive Dataset.from_ loading methods and deprecate Plus tests * Raise error for unrecognised types in single-dispatch * Pass load_dataset up to dkist * Update changelog/274.feature.rst Co-authored-by: Stuart Mumford <[email protected]> * Use load_dataset in aia generation script * Update docs to use load_dataset * Add note for later work * Add jsonschema as explicit dependency Fixes #276 * asdf needs jsconschema <4 apparently * Missed a from_asdf * Helps if you use variables that exist * Use pytest.warns in tests instead of pytest.raises Because it wasn't marking code as run * Add test to cover invalid input to loader * Replace AstropyDeprecationWarning with new DKISTDeprecationWarning * Imports are hard * Forgot to add some important things * Apply suggestions from code review * Update dkist/dataset/loader.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --------- Co-authored-by: Stuart Mumford <[email protected]> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
- Loading branch information
1 parent
914f873
commit 1cdd106
Showing
15 changed files
with
145 additions
and
57 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 @@ | ||
Add a new `dkist.load_dataset` function to combine and replace ``Dataset.from_directory()`` and ``Dataset.from_asdf()``. |
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 @@ | ||
Add jsonschema as an explicit dependency (previously it was provided by asdf). |
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,2 +1,3 @@ | ||
from .dataset import Dataset | ||
from .loader import load_dataset | ||
from .tiled_dataset import TiledDataset |
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,75 @@ | ||
import importlib.resources as importlib_resources | ||
from pathlib import Path | ||
from functools import singledispatch | ||
|
||
from jsonschema.exceptions import ValidationError | ||
|
||
import asdf | ||
|
||
|
||
@singledispatch | ||
def load_dataset(target): | ||
"""Function to load a Dataset from either an asdf file path or a directory.""" | ||
raise TypeError("Input type not recognised. Must be a string or pathlib.Path referencing a " | ||
".asdf file or a directory containing one.") | ||
|
||
|
||
@load_dataset.register | ||
def _load_from_string(path: str): | ||
# TODO Adjust this to accept URLs as well | ||
return _load_from_path(Path(path)) | ||
|
||
|
||
@load_dataset.register | ||
def _load_from_path(path: Path): | ||
path = path.expanduser() | ||
if not path.is_dir(): | ||
if not path.exists(): | ||
raise ValueError(f"File {path} does not exist.") | ||
return _load_from_asdf(path) | ||
else: | ||
return _load_from_directory(path) | ||
|
||
|
||
def _load_from_directory(directory): | ||
""" | ||
Construct a `~dkist.dataset.Dataset` from a directory containing one | ||
asdf file and a collection of FITS files. | ||
""" | ||
base_path = Path(directory).expanduser() | ||
asdf_files = tuple(base_path.glob("*.asdf")) | ||
|
||
if not asdf_files: | ||
raise ValueError("No asdf file found in directory.") | ||
elif len(asdf_files) > 1: | ||
raise NotImplementedError("Multiple asdf files found in this " | ||
"directory. Use from_asdf to specify which " | ||
"one to use.") # pragma: no cover | ||
|
||
asdf_file = asdf_files[0] | ||
|
||
return _load_from_asdf(asdf_file) | ||
|
||
|
||
def _load_from_asdf(filepath): | ||
""" | ||
Construct a dataset object from a filepath of a suitable asdf file. | ||
""" | ||
from dkist.dataset import TiledDataset | ||
filepath = Path(filepath).expanduser() | ||
base_path = filepath.parent | ||
try: | ||
with importlib_resources.as_file(importlib_resources.files("dkist.io") / "level_1_dataset_schema.yaml") as schema_path: | ||
with asdf.open(filepath, custom_schema=schema_path.as_posix(), | ||
lazy_load=False, copy_arrays=True) as ff: | ||
ds = ff.tree['dataset'] | ||
if isinstance(ds, TiledDataset): | ||
for sub in ds.flat: | ||
sub.files.basepath = base_path | ||
else: | ||
ds.files.basepath = base_path | ||
return ds | ||
|
||
except ValidationError as e: | ||
err = f"This file is not a valid DKIST Level 1 asdf file, it fails validation with: {e.message}." | ||
raise TypeError(err) from 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
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,7 @@ | ||
from astropy.utils.decorators import deprecated as astropy_deprecated | ||
|
||
from .exceptions import DKISTDeprecationWarning | ||
|
||
|
||
def deprecated(*args, **kwargs): | ||
return astropy_deprecated(*args, warning_type=DKISTDeprecationWarning, **kwargs) |
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,2 @@ | ||
class DKISTDeprecationWarning(DeprecationWarning): | ||
pass |
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