Skip to content

Commit

Permalink
allow ModelLibrary(asn_dict)
Browse files Browse the repository at this point in the history
  • Loading branch information
braingram committed Jun 10, 2024
1 parent f7bd7cc commit bd1716a
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 12 deletions.
40 changes: 28 additions & 12 deletions romancal/datamodels/library.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,23 +128,36 @@ def __init__(
else:
self._model_store = {}

# TODO path support
# TODO model list support
if isinstance(init, (str, Path)):
self._asn_path = os.path.abspath(
os.path.expanduser(os.path.expandvars(init))
)
self._asn_dir = os.path.dirname(self._asn_path)
if isinstance(init, MutableMapping):
asn_data = init
self._asn_dir = os.path.abspath(".")
self._asn = init

if self._asn_exptypes is not None:
raise NotImplementedError()

if self._asn_n_members is not None:
raise NotImplementedError()

self._members = self._asn["products"][0]["members"]

for member in self._members:
if "group_id" not in member:
filename = os.path.join(self._asn_dir, member["expname"])
member["group_id"] = _file_to_group_id(filename)
elif isinstance(init, (str, Path)):
asn_path = os.path.abspath(os.path.expanduser(os.path.expandvars(init)))
self._asn_dir = os.path.dirname(asn_path)

# TODO asn_table_name is there another way to handle this
self.asn_table_name = os.path.basename(self._asn_path)
self.asn_table_name = os.path.basename(asn_path)

# load association
# TODO why did ModelContainer make this local?
from ..associations import AssociationNotValidError, load_asn

try:
with open(self._asn_path) as asn_file:
with open(asn_path) as asn_file:
asn_data = load_asn(asn_file)
except AssociationNotValidError as e:
raise OSError("Cannot read ASN file.") from e
Expand Down Expand Up @@ -210,6 +223,12 @@ def __init__(
}
)

if self._asn_exptypes is not None:
raise NotImplementedError()

if self._asn_n_members is not None:
raise NotImplementedError()

# make a fake association
self._asn = {
# TODO other asn data?
Expand All @@ -221,9 +240,6 @@ def __init__(
}
self._members = self._asn["products"][0]["members"]

# _asn_dir?
# _asn_path?

elif isinstance(init, self.__class__):
# TODO clone/copy?
raise NotImplementedError()
Expand Down
10 changes: 10 additions & 0 deletions romancal/datamodels/tests/test_library.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import json
import os
from contextlib import nullcontext

import pytest
Expand Down Expand Up @@ -77,6 +78,15 @@ def test_load_asn(example_library):
assert len(example_library) == _N_MODELS


def test_init_from_asn(example_asn_path):
with open(example_asn_path) as f:
asn = load_asn(f)
# as association filenames are local we must be in the same directory
os.chdir(example_asn_path.parent)
lib = ModelLibrary(asn)
assert len(lib) == _N_MODELS


@pytest.mark.parametrize("asn_n_members", range(_N_MODELS))
def test_asn_n_members(example_asn_path, asn_n_members):
"""
Expand Down

0 comments on commit bd1716a

Please sign in to comment.