Skip to content

Commit 611ff30

Browse files
Start better annotating and satisfying "path-like" types
Part of the work for #218 Co-authored-by: Andrew Howe <arhowe00@gmail.com>
1 parent 7e21567 commit 611ff30

File tree

2 files changed

+26
-10
lines changed

2 files changed

+26
-10
lines changed

src/openlifu/db/database.py

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import shutil
77
from enum import Enum
88
from pathlib import Path
9-
from typing import List, Optional
9+
from typing import Dict, List, Optional, Union
1010

1111
import h5py
1212

@@ -20,7 +20,7 @@
2020
from .user import User
2121

2222
OnConflictOpts = Enum('OnConflictOpts', ['ERROR', 'OVERWRITE', 'SKIP'])
23-
23+
PathLike = Union[str, os.PathLike]
2424

2525
class Database:
2626
def __init__(self, path: Optional[str] = None):
@@ -268,7 +268,13 @@ def write_subject(self, subject, on_conflict=OnConflictOpts.ERROR):
268268

269269
self.logger.info(f"Added subject with ID {subject_id} to the database.")
270270

271-
def write_transducer(self, transducer, registration_surface_model_filepath: Optional[str] = None, transducer_body_model_filepath: Optional[str] = None, on_conflict: OnConflictOpts=OnConflictOpts.ERROR) -> None:
271+
def write_transducer(
272+
self,
273+
transducer,
274+
registration_surface_model_filepath: Optional[PathLike] = None,
275+
transducer_body_model_filepath: Optional[PathLike] = None,
276+
on_conflict: OnConflictOpts=OnConflictOpts.ERROR,
277+
) -> None:
272278
""" Writes a transducer object to database and copies the affiliated transducer data files to the database if provided. When a transducer that is already present in the database is being re-written,
273279
the associated model data files do not need to be provided if they have previously been added to the database.
274280
Args:
@@ -706,7 +712,7 @@ def load_photoscan(self, subject_id, session_id, photoscan_id, load_data = False
706712
return photoscan, (model_data, texture_data)
707713
return photoscan
708714

709-
def get_transducer_absolute_filepaths(self, transducer_id:str) -> dict:
715+
def get_transducer_absolute_filepaths(self, transducer_id:str) -> Dict[str,Optional[str]]:
710716
""" Returns the absolute filepaths to the model data files i.e. transducer body and registration surface
711717
model files affiliated with the transducer, with ID `transducer_id`. Unlike `load_transducer`, which
712718
specifies the relative paths to the model datafiles along with other transducer attributes, this function
@@ -719,10 +725,12 @@ def get_transducer_absolute_filepaths(self, transducer_id:str) -> dict:
719725
dict: A dictionary containing the absolute filepaths to the affiliated transducer data files with the following possible keys:
720726
- "id" (str): transducer ID
721727
- "name" (str): transducer name
722-
- "registration_surface_abspath" (str): absolute path to the transducer registration surface (open-surface mesh
728+
- "registration_surface_abspath" (str or None): absolute path to the transducer registration surface (open-surface mesh
723729
used for transducer tracking registration). This key is only included if there *is* an affiliated registration surface.
724-
- "transducer_body_abspath" (str): absolute path to the transducer body model (closed-surface mesh for visualizing the
730+
None if no registration surface is available.
731+
- "transducer_body_abspath" (str or None): absolute path to the transducer body model (closed-surface mesh for visualizing the
725732
transducer). This key is only included if there *is* an affiliated body model.
733+
None if no transducer body is available.
726734
"""
727735
transducer_metadata_filepath = self.get_transducer_filename(transducer_id)
728736
with open(transducer_metadata_filepath) as f:
@@ -732,9 +740,17 @@ def get_transducer_absolute_filepaths(self, transducer_id:str) -> dict:
732740
"name": transducer["name"],
733741
}
734742
if "registration_surface_filename" in transducer and transducer["registration_surface_filename"] is not None:
735-
transducer_filepaths_dict["registration_surface_abspath"] = Path(transducer_metadata_filepath).parent/transducer["registration_surface_filename"]
743+
transducer_filepaths_dict["registration_surface_abspath"] = str(
744+
Path(transducer_metadata_filepath).parent/transducer["registration_surface_filename"]
745+
)
746+
else:
747+
transducer_filepaths_dict["registration_surface_abspath"] = None
736748
if "transducer_body_filename" in transducer and transducer["transducer_body_filename"] is not None:
737-
transducer_filepaths_dict["transducer_body_abspath"] = Path(transducer_metadata_filepath).parent/transducer["transducer_body_filename"]
749+
transducer_filepaths_dict["transducer_body_abspath"] = str(
750+
Path(transducer_metadata_filepath).parent/transducer["transducer_body_filename"]
751+
)
752+
else:
753+
transducer_filepaths_dict["transducer_body_abspath"] = None
738754
return transducer_filepaths_dict
739755

740756
def load_standoff(self, transducer_id, standoff_id="standoff"):

tests/test_database.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -677,10 +677,10 @@ def test_get_transducer_absolute_filepaths(example_database:Database, tmp_path:P
677677
assert reconstructed_path.exists()
678678
assert reconstructed_path.name == registration_surface_path.name
679679
else:
680-
assert "registration_surface_abspath" not in absolute_file_paths
680+
assert absolute_file_paths["registration_surface_abspath"] is None
681681
if include_body_model:
682682
reconstructed_path = Path(absolute_file_paths["transducer_body_abspath"])
683683
assert reconstructed_path.exists()
684684
assert reconstructed_path.name == transducer_body_path.name
685685
else:
686-
assert "transducer_body_abspath" not in absolute_file_paths
686+
assert absolute_file_paths["transducer_body_abspath"] is None

0 commit comments

Comments
 (0)