Skip to content

Commit 724212e

Browse files
committed
Add functionality to create photoscan object from absolute filepath info (#198)
1 parent a3eaf4a commit 724212e

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

src/openlifu/photoscan.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,23 @@ def to_file(self, filename):
8787
with open(filename, 'w') as file:
8888
file.write(self.to_json(compact = False))
8989

90+
@staticmethod
91+
def from_absolute_filepaths_info(photoscan_absolute_filepath_info: Dict) -> "Photoscan":
92+
"""
93+
Create a photoscan from a photoscan dictionary containing absolute filepath information for
94+
the affiliated data (keys: id, name, model_abspath, texture_abspath, photoscan_approved).
95+
A photoscan info dictionary can be obtained from a Database using `get_photoscan_absolute_filepaths_info`.
96+
"""
97+
photoscan_dict = {"id": photoscan_absolute_filepath_info["id"],\
98+
"name": photoscan_absolute_filepath_info["name"],\
99+
"model_filename": Path(photoscan_absolute_filepath_info["model_abspath"]).name,
100+
"texture_filename": Path(photoscan_absolute_filepath_info["texture_abspath"]).name,
101+
"photoscan_approved": photoscan_absolute_filepath_info["photoscan_approved"]}
102+
if "mtl_abspath" in photoscan_absolute_filepath_info:
103+
photoscan_dict["mtl_filename"] = Path(photoscan_absolute_filepath_info["mtl_abspath"]).name
104+
105+
return Photoscan.from_dict(photoscan_dict)
106+
90107
def load_data_from_filepaths(model_abspath: str, texture_abspath: str) -> Tuple[vtk.vtkPolyData, vtk.vtkImageData]:
91108
"""
92109
This function returns the data directly from the model and texture filepaths without requiring a photoscan object.

tests/test_photoscans.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ def test_load_photoscan(example_database:Database, tmp_path:Path):
3535
subject_id = "example_subject"
3636
session_id = "example_session"
3737
photoscan_id = "example_photoscan"
38-
photoscan_info = example_database.get_photoscan_info(subject_id, session_id, photoscan_id)
38+
photoscan_info = example_database.get_photoscan_absolute_filepaths_info(subject_id, session_id, photoscan_id)
3939
[model, texture] = load_data_from_filepaths(photoscan_info["model_abspath"], photoscan_info["texture_abspath"])
4040
assert model is not None
4141
assert texture is not None
@@ -51,6 +51,10 @@ def test_load_photoscan(example_database:Database, tmp_path:Path):
5151
assert isinstance(model, vtkPolyData)
5252
assert isinstance(texture,vtkImageData)
5353

54+
# From photoscan info
55+
photoscan_from_info = Photoscan.from_absolute_filepaths_info(photoscan_info)
56+
assert photoscan == photoscan_from_info
57+
5458
bogus_file = Path(tmp_path/"test_db_files/bogus_photoscan.pdf")
5559
bogus_file.parent.mkdir(parents=True, exist_ok=True)
5660
bogus_file.touch()
@@ -64,6 +68,7 @@ def test_load_photoscan(example_database:Database, tmp_path:Path):
6468
with pytest.raises(FileNotFoundError, match="does not exist"):
6569
load_data_from_filepaths(bogus_file, photoscan_info["texture_abspath"])
6670

71+
6772
def test_convert_between_ras_and_lps():
6873

6974
# Create example data

0 commit comments

Comments
 (0)