Skip to content

Commit

Permalink
cleanup ModelLibrary attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
braingram committed Jun 10, 2024
1 parent bd1716a commit d64e269
Showing 1 changed file with 13 additions and 24 deletions.
37 changes: 13 additions & 24 deletions romancal/datamodels/library.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import asdf
from roman_datamodels import open as datamodels_open

from romancal.associations import AssociationNotValidError, load_asn

__all__ = ["LibraryError", "BorrowError", "ClosedLibraryError", "ModelLibrary"]


Expand Down Expand Up @@ -113,10 +115,7 @@ def __init__(
memmap=False,
temp_directory=None,
):
self._asn_exptypes = asn_exptypes
self._asn_n_members = asn_n_members
self._on_disk = on_disk

self._open = False
self._ledger = {}

Expand All @@ -133,10 +132,10 @@ def __init__(
self._asn_dir = os.path.abspath(".")
self._asn = init

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

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

self._members = self._asn["products"][0]["members"]
Expand All @@ -153,25 +152,22 @@ def __init__(
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(asn_path) as asn_file:
asn_data = load_asn(asn_file)
except AssociationNotValidError as e:
raise OSError("Cannot read ASN file.") from e

Check warning on line 159 in romancal/datamodels/library.py

View check run for this annotation

Codecov / codecov/patch

romancal/datamodels/library.py#L159

Added line #L159 was not covered by tests

if self._asn_exptypes is not None:
if asn_exptypes is not None:
asn_data["products"][0]["members"] = [
m
for m in asn_data["products"][0]["members"]
if m["exptype"] in self._asn_exptypes
if m["exptype"] in asn_exptypes
]

if self._asn_n_members is not None:
if asn_n_members is not None:
asn_data["products"][0]["members"] = asn_data["products"][0]["members"][
: self._asn_n_members
:asn_n_members
]

# make members easier to access
Expand Down Expand Up @@ -223,10 +219,10 @@ def __init__(
}
)

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

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

# make a fake association
Expand All @@ -247,7 +243,7 @@ def __init__(
raise NotImplementedError()

# make sure first model is loaded in memory (as expected by stpipe)
if self._asn_n_members == 1:
if asn_n_members == 1:
# FIXME stpipe also reaches into _models (instead of _model_store)
self._models = [self._load_member(0)]

Expand All @@ -268,12 +264,6 @@ def _to_read_only(obj):

return asdf.treeutil.walk_and_modify(self._asn, _to_read_only)

# TODO we may want to not expose this as it could go out-of-sync
# pretty easily with the actual models.
# @property
# def members(self):
# return self.asn['products'][0]['members']

@property
def group_names(self):
names = set()
Expand Down Expand Up @@ -359,7 +349,6 @@ def _load_member(self, index):
# FIXME tweakreg also expects table_name and pool_name
model.meta.asn["table_name"] = self.asn_table_name
model.meta.asn["pool_name"] = self.asn["asn_pool"]
# this returns an OPEN model, it's up to calling code to close this
return model

def __copy__(self):
Expand Down Expand Up @@ -483,8 +472,8 @@ def index(self, attribute, copy=False):
self.discard(i, model)
yield copy_func(attr)

Check warning on line 473 in romancal/datamodels/library.py

View check run for this annotation

Codecov / codecov/patch

romancal/datamodels/library.py#L468-L473

Added lines #L468 - L473 were not covered by tests

def map_function(self, function, write=False):
if write:
def map_function(self, function, modify=False):
if modify:
cleanup = self.discard

Check warning on line 477 in romancal/datamodels/library.py

View check run for this annotation

Codecov / codecov/patch

romancal/datamodels/library.py#L476-L477

Added lines #L476 - L477 were not covered by tests
else:
cleanup = self.__setitem__
Expand Down

0 comments on commit d64e269

Please sign in to comment.