Skip to content

Commit 7e21567

Browse files
Fix handling of null transducer models (#218)
1 parent 1dcff78 commit 7e21567

File tree

2 files changed

+52
-13
lines changed

2 files changed

+52
-13
lines changed

src/openlifu/db/database.py

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -707,29 +707,33 @@ def load_photoscan(self, subject_id, session_id, photoscan_id, load_data = False
707707
return photoscan
708708

709709
def get_transducer_absolute_filepaths(self, transducer_id:str) -> dict:
710-
""" Returns the absolute filepaths to the model data files i.e. transducer body and registration surface model files affiliated with the transducer, with ID `transducer_id`.
711-
Unlike `load_transducer`, which specifies the relative paths to the model datafiles along with other transducer attributes, this function only returns the absolute filepaths to
712-
the datafiles based on the Database directory.
710+
""" Returns the absolute filepaths to the model data files i.e. transducer body and registration surface
711+
model files affiliated with the transducer, with ID `transducer_id`. Unlike `load_transducer`, which
712+
specifies the relative paths to the model datafiles along with other transducer attributes, this function
713+
only returns the absolute filepaths to the datafiles based on the Database directory.
714+
713715
Args:
714716
transducer_id: Transducer ID
717+
715718
Returns:
716-
dict: A dictionary containing the absolute filepaths to the affiliated transducer data files with the following keys:
719+
dict: A dictionary containing the absolute filepaths to the affiliated transducer data files with the following possible keys:
717720
- "id" (str): transducer ID
718721
- "name" (str): transducer name
719-
- "registration_surface_abspath" (str): absolute path to the transducer registration surface (open-surface mesh used for transducer tracking registration)
720-
- "transducer_body_abspath" (str): absolute path to the transducer body model (closed-surface mesh for visualizing the transducer)
722+
- "registration_surface_abspath" (str): absolute path to the transducer registration surface (open-surface mesh
723+
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
725+
transducer). This key is only included if there *is* an affiliated body model.
721726
"""
722727
transducer_metadata_filepath = self.get_transducer_filename(transducer_id)
723728
with open(transducer_metadata_filepath) as f:
724729
transducer = json.load(f)
725-
if "registration_surface_filename" not in transducer and "transducer_body_filename" not in transducer:
726-
raise ValueError(f"Data filepaths not found affiliated with transducer {transducer_id}")
727-
transducer_filepaths_dict = {"id": transducer["id"],\
728-
"name": transducer["name"],
729-
}
730-
if "registration_surface_filename" in transducer:
730+
transducer_filepaths_dict = {
731+
"id": transducer["id"],
732+
"name": transducer["name"],
733+
}
734+
if "registration_surface_filename" in transducer and transducer["registration_surface_filename"] is not None:
731735
transducer_filepaths_dict["registration_surface_abspath"] = Path(transducer_metadata_filepath).parent/transducer["registration_surface_filename"]
732-
if "transducer_body_filename" in transducer:
736+
if "transducer_body_filename" in transducer and transducer["transducer_body_filename"] is not None:
733737
transducer_filepaths_dict["transducer_body_abspath"] = Path(transducer_metadata_filepath).parent/transducer["transducer_body_filename"]
734738
return transducer_filepaths_dict
735739

tests/test_database.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -649,3 +649,38 @@ def test_write_transducer(example_database:Database, example_transducer: Transdu
649649
example_transducer.registration_surface_filename = "bogus_transducer_model.obj"
650650
with pytest.raises(ValueError, match="file associated with transducer"):
651651
example_database.write_transducer(example_transducer, on_conflict=OnConflictOpts.OVERWRITE)
652+
653+
@pytest.mark.parametrize("include_registration_surface", [True, False])
654+
@pytest.mark.parametrize("include_body_model", [True, False])
655+
def test_get_transducer_absolute_filepaths(example_database:Database, tmp_path:Path, include_registration_surface:bool, include_body_model:bool):
656+
transducer = Transducer(id="transducer_for_test_get_transducer_absolute_filepaths")
657+
658+
registration_surface_path = None
659+
transducer_body_path = None
660+
if include_registration_surface:
661+
registration_surface_path = Path(tmp_path/"test_db_files/example_registration_surface.obj")
662+
registration_surface_path.parent.mkdir(parents=True, exist_ok=True)
663+
registration_surface_path.touch()
664+
if include_body_model:
665+
transducer_body_path = Path(tmp_path/"test_db_files/example_transducer_body.obj")
666+
transducer_body_path.parent.mkdir(parents=True, exist_ok=True)
667+
transducer_body_path.touch()
668+
669+
example_database.write_transducer(
670+
transducer=transducer,
671+
registration_surface_model_filepath = registration_surface_path,
672+
transducer_body_model_filepath = transducer_body_path,
673+
)
674+
absolute_file_paths = example_database.get_transducer_absolute_filepaths("transducer_for_test_get_transducer_absolute_filepaths")
675+
if include_registration_surface:
676+
reconstructed_path = Path(absolute_file_paths["registration_surface_abspath"])
677+
assert reconstructed_path.exists()
678+
assert reconstructed_path.name == registration_surface_path.name
679+
else:
680+
assert "registration_surface_abspath" not in absolute_file_paths
681+
if include_body_model:
682+
reconstructed_path = Path(absolute_file_paths["transducer_body_abspath"])
683+
assert reconstructed_path.exists()
684+
assert reconstructed_path.name == transducer_body_path.name
685+
else:
686+
assert "transducer_body_abspath" not in absolute_file_paths

0 commit comments

Comments
 (0)