Skip to content

get_transducer_absolute_filepaths cannot handle null #218

@ebrahimebrahim

Description

@ebrahimebrahim

Since the type of both Transducer.registration_surface_filename and Transducer.transducer_body_filename are given as Optional[str], these should both be allowed to be set to None, or equivalently null in the json files. However Database.get_transducer_absolute_filepaths will fail to handle this case because it is checking to see whether the attributes are present rather than checking to see whether the attributes are non-None:

def get_transducer_absolute_filepaths(self, transducer_id:str) -> dict:
""" 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`.
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
the datafiles based on the Database directory.
Args:
transducer_id: Transducer ID
Returns:
dict: A dictionary containing the absolute filepaths to the affiliated transducer data files with the following keys:
- "id" (str): transducer ID
- "name" (str): transducer name
- "registration_surface_abspath" (str): absolute path to the transducer registration surface (open-surface mesh used for transducer tracking registration)
- "transducer_body_abspath" (str): absolute path to the transducer body model (closed-surface mesh for visualizing the transducer)
"""
transducer_metadata_filepath = self.get_transducer_filename(transducer_id)
with open(transducer_metadata_filepath) as f:
transducer = json.load(f)
if "registration_surface_filename" not in transducer and "transducer_body_filename" not in transducer:
raise ValueError(f"Data filepaths not found affiliated with transducer {transducer_id}")
transducer_filepaths_dict = {"id": transducer["id"],\
"name": transducer["name"],
}
if "registration_surface_filename" in transducer:
transducer_filepaths_dict["registration_surface_abspath"] = Path(transducer_metadata_filepath).parent/transducer["registration_surface_filename"]
if "transducer_body_filename" in transducer:
transducer_filepaths_dict["transducer_body_abspath"] = Path(transducer_metadata_filepath).parent/transducer["transducer_body_filename"]
return transducer_filepaths_dict

To see the bug, try get_transducer_absolute_filepaths on a Transducer that has either of those attributes set to None/null -- it raises an exception because it attempts to construct a Path using None.

Besides fixing this issue, it would also be nice to make get_transducer_absolute_filepaths just return an empty dict if there is no absolute filepath info rather than raising an exception -- it should be perfectly fine for there to be no absolute filepath info because a Transducer with Transducer.registration_surface_filename and Transducer.transducer_body_filename set to None is perfectly valid as things currently stand.


Traceability Information:

This information was populated automatically by a script on 2025-06-09T18:02:22Z.

Metadata

Metadata

Labels

No labels
No labels

Type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions