Skip to content

Commit

Permalink
Merge pull request #243 from ihmeuw/collijk/bugfix/clean-up-artifact-…
Browse files Browse the repository at this point in the history
…test-warnings-clean

Squash warnings in artifact generation code
  • Loading branch information
collijk committed Dec 20, 2022
2 parents a635aa6 + e32f5b0 commit 298753d
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 91 deletions.
24 changes: 18 additions & 6 deletions src/vivarium/framework/artifact/artifact.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def __init__(self, path: Union[str, Path], filter_terms: List[str] = None):
self._draw_column_filter = _parse_draw_filters(filter_terms)
self._cache = {}

self.create_hdf_with_keyspace()
self.create_hdf_with_keyspace(self._path)
self._keys = Keys(self._path)

@property
Expand All @@ -62,12 +62,24 @@ def filter_terms(self) -> List[str]:
"""Filters that will be applied to the requested data on loads."""
return self._filter_terms

def create_hdf_with_keyspace(self):
@staticmethod
def create_hdf_with_keyspace(path: Path):
"""Creates the artifact HDF file and adds a node to track keys."""
if not self._path.is_file():
warnings.warn(f"No artifact found at {self._path}. Building new artifact.")
hdf.touch(self._path)
hdf.write(self._path, "metadata.keyspace", ["metadata.keyspace"])
if not path.is_file():
warnings.warn(f"No artifact found at {path}. Building new artifact.")
hdf.touch(path)

keys = hdf.get_keys(path)
if keys and "metadata.keyspace" not in keys:
raise ArtifactException(
"Attempting to construct an Artifact from a malformed existing file. "
"This can occur when constructing an Artifact from an existing file when "
"the existing file was generated by some other hdf writing mechanism "
"(e.g. pd.to_hdf) rather than generating the the file using this class "
"and a non-existent or empty hdf file."
)
if not keys:
hdf.write(path, "metadata.keyspace", ["metadata.keyspace"])

def load(self, entity_key: str) -> Any:
"""Loads the data associated with provided entity_key.
Expand Down
4 changes: 2 additions & 2 deletions src/vivarium/framework/artifact/hdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ def remove(path: Union[str, Path], entity_key: str):
file.remove_node(entity_key.path, recursive=True)


def get_keys(path: str) -> List[str]:
def get_keys(path: Union[str, Path]) -> List[str]:
"""Gets key representation of all paths in an HDF file.
Parameters
Expand Down Expand Up @@ -426,7 +426,7 @@ def _get_valid_filter_terms(filter_terms, colnames):
# then split each condition out
t = re.split("[&|]", t)
# get the unique columns referenced by this term
term_columns = set([re.split("[<=>\s]", i.strip())[0] for i in t])
term_columns = set([re.split(r"[<=>\s]", i.strip())[0] for i in t])
if not term_columns.issubset(colnames):
valid_terms.remove(term)
return valid_terms if valid_terms else None
Empty file.

0 comments on commit 298753d

Please sign in to comment.