Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open json files as ModelLibrary if romancal is installed #389

Merged
merged 6 commits into from
Sep 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My only question is if this should be against the dev version or the last stable version? dev feels right, but I want to make sure you agree.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree I think dev is most useful here since the versions tend to march along together.

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}