Skip to content

Commit

Permalink
Open json files as ModelLibrary if romancal is installed (#389)
Browse files Browse the repository at this point in the history
  • Loading branch information
braingram authored Sep 26, 2024
1 parent 2734f19 commit ce9eb29
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 9 deletions.
6 changes: 6 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,9 @@ jobs:
with:
envs: |
- linux: rad
test_with_romancal:
uses: OpenAstronomy/github-actions-workflows/.github/workflows/tox.yml@v1
with:
envs: |
- linux: withromancal
coverage: codecov
1 change: 1 addition & 0 deletions changes/389.feature.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Open ``.json`` files as ``ModelLibrary`` if ``romancal`` is installed.
17 changes: 8 additions & 9 deletions src/roman_datamodels/datamodels/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,14 @@ def rdm_open(init, memmap=False, **kwargs):
-------
`DataModel`
"""
if isinstance(init, str | Path):
if Path(init).suffix.lower() == ".json":
try:
from romancal.datamodels.library import ModelLibrary

return ModelLibrary(init)
except ImportError:
raise ImportError("Please install romancal to allow opening associations with roman_datamodels")
with validate.nuke_validation():
if isinstance(init, DataModel):
# Copy the object so it knows not to close here
Expand All @@ -76,14 +84,5 @@ def rdm_open(init, memmap=False, **kwargs):
if (model_type := type(asdf_file.tree["roman"])) in MODEL_REGISTRY:
return MODEL_REGISTRY[model_type](asdf_file, **kwargs)

if isinstance(init, str):
exts = Path(init).suffixes
if not exts:
raise ValueError(f"Input file path does not have an extension: {init}")

# Assume json files are asn and return them
if exts[0] == "json":
return init

asdf_file.close()
raise TypeError(f"Unknown datamodel type: {model_type}")
21 changes: 21 additions & 0 deletions tests/test_open.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import json
from pathlib import Path

import asdf
Expand Down Expand Up @@ -236,3 +237,23 @@ def test_rdm_open_non_datamodel():

with pytest.raises(TypeError, match=r"Unknown datamodel type: .*"):
rdm_open(Path(__file__).parent / "data" / "not_a_datamodel.asdf")


def test_open_asn(tmp_path):
romancal = pytest.importorskip("romancal")

fn = tmp_path / "test.json"
asn = {
"products": [
{
"members": [],
"name": "foo",
}
],
}
with open(fn, "w") as f:
json.dump(asn, f)

lib = datamodels.open(fn)

assert isinstance(lib, romancal.datamodels.ModelLibrary)
17 changes: 17 additions & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ env_list =
test{,-devdeps}{,-pyargs,-cov}-xdist
test-numpy{120,121,122}-xdist
build-{docs,dist}
withromancal

# tox environments are constructed with so-called 'factors' (or terms)
# separated by hyphens, e.g. test-devdeps-cov. Lines below starting with factor:
Expand Down Expand Up @@ -69,3 +70,19 @@ deps =
build
commands =
python -m build .

[testenv:withromancal]
allowlist_externals =
git
bash
deps =
pytest-cov
commands_pre =
bash -c "pip freeze -q | grep 'roman_datamodels @' > {env_tmp_dir}/requirements.txt"
pip install git+https://github.com/spacetelescope/romancal.git
pip install -r {env_tmp_dir}/requirements.txt
pip freeze
commands =
pytest tests/test_open.py::test_open_asn \
--cov=tests --cov-config pyproject.toml --cov-report term-missing --cov-report xml \
{posargs}

0 comments on commit ce9eb29

Please sign in to comment.