Skip to content

Commit

Permalink
Add STL exporter for scaffolds.
Browse files Browse the repository at this point in the history
  • Loading branch information
hsorby committed Nov 21, 2023
1 parent af370d0 commit f4d8b26
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 4 deletions.
36 changes: 32 additions & 4 deletions src/sparc/client/zinchelper.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import re

from cmlibs.exporter.vtk import ArgonSceneExporter as VTKExporter

Check warning on line 5 in src/sparc/client/zinchelper.py

View workflow job for this annotation

GitHub Actions / reviewdog

[formatters] reported by reviewdog 🐶 Raw Output: src/sparc/client/zinchelper.py:5:-from cmlibs.exporter.vtk import ArgonSceneExporter as VTKExporter
from cmlibs.exporter.stl import ArgonSceneExporter as STLExporter
from cmlibs.utils.zinc.field import get_group_list

Check warning on line 7 in src/sparc/client/zinchelper.py

View workflow job for this annotation

GitHub Actions / reviewdog

[formatters] reported by reviewdog 🐶 Raw Output: src/sparc/client/zinchelper.py:6:+from cmlibs.exporter.vtk import ArgonSceneExporter as VTKExporter
from cmlibs.zinc.context import Context
from cmlibs.zinc.result import RESULT_OK
Expand Down Expand Up @@ -104,14 +105,12 @@ def download_files(
assert response.status_code == 200
return file_list[0]["name"]

def get_scaffold_vtk(self, dataset_id, output_location=None):
def _get_scaffold(self, dataset_id):
"""
Generates a VTK file for the scaffold settings of a dataset.
Get a scaffold settings file from the Pennsieve.
Args:
dataset_id (int): The ID of the dataset to generate the VTK file for.
output_location (str): The output location for the generated VTK file.
If not provided, a default of the current working directory is used.
"""
scaffold_setting_file = self.download_files(
limit=1,
Expand All @@ -129,12 +128,41 @@ def get_scaffold_vtk(self, dataset_id, output_location=None):

sm = scaffolds.Scaffolds_decodeJSON(c["scaffold_settings"]["scaffoldPackage"])
sm.generate(self._region)

def get_scaffold_vtk(self, dataset_id, output_location=None):
"""
Generates a VTK file for the scaffold settings of a dataset.
Args:
dataset_id (int): The ID of the dataset to generate the VTK file for.
output_location (str): The output location for the generated VTK file.
If not provided, a default of the current working directory is used.
"""
self._get_scaffold(dataset_id)

if not output_location:
output_location = "."

ex = VTKExporter(output_location, "scaffold")
ex.export_vtk_from_scene(self._region.getScene())

def get_scaffold_stl(self, dataset_id, output_location=None):
"""
Generates an STL file for the scaffold settings of a dataset.
Args:
dataset_id (int): The ID of the dataset to generate the STL file for.
output_location (str): The output location for the generated STL file.
If not provided, a default of the current working directory is used.
"""
self._get_scaffold(dataset_id)

if not output_location:
output_location = "."

ex = STLExporter(output_location, "scaffold")
ex.export_stl_from_scene(self._region.getScene())

def get_mbf_vtk(self, dataset_id, dataset_file, output_file=None):
"""
Generates a VTK file for an MBF XML segmentation file.
Expand Down
30 changes: 30 additions & 0 deletions tests/test_zinc.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,36 @@ def test_export_scaffold_into_vtk_format(zinc):
os.remove(output_file)


def test_export_scaffold_into_stl_format(zinc):
# create a temporary output file
output_location = os.path.abspath(

Check warning on line 45 in tests/test_zinc.py

View workflow job for this annotation

GitHub Actions / reviewdog

[formatters] reported by reviewdog 🐶 Raw Output: tests/test_zinc.py:45:- output_location = os.path.abspath( tests/test_zinc.py:46:- os.path.join(os.path.dirname(__file__), "resources/") tests/test_zinc.py:47:- ) tests/test_zinc.py:43:+ output_location = os.path.abspath(os.path.join(os.path.dirname(__file__), "resources/"))

Check warning on line 45 in tests/test_zinc.py

View workflow job for this annotation

GitHub Actions / runner / black

[blackfmt] reported by reviewdog 🐶 Raw Output: tests/test_zinc.py:45:- output_location = os.path.abspath( tests/test_zinc.py:46:- os.path.join(os.path.dirname(__file__), "resources/") tests/test_zinc.py:47:- ) tests/test_zinc.py:43:+ output_location = os.path.abspath(os.path.join(os.path.dirname(__file__), "resources/"))
os.path.join(os.path.dirname(__file__), "resources/")
)

# ensure the function returns None if the dataset has no Scaffold_Creator-settings.json file
invalid_dataset_id = 1000000
result = None
with pytest.raises(RuntimeError):
result = zinc.get_scaffold_stl(invalid_dataset_id, output_location)
assert result is None

# ensure the function raises an error if the downloaded file is not scaffold_settings file
dataset_id = 77
with pytest.raises(AssertionError):
zinc.get_scaffold_stl(dataset_id, output_location)

# ensure the function generates a VTK file with valid content
dataset_id = 292
zinc.get_scaffold_stl(dataset_id, output_location)

output_file = os.path.join(output_location, "scaffold_zinc_graphics.stl")
assert os.path.exists(output_file)
assert os.path.getsize(output_file) > 0

# Clean up the temporary output file
os.remove(output_file)


def test_export_scaffold_into_vtk_format_with_default_output_location(zinc):
# ensure the function generates a VTK file with valid content
dataset_id = 292
Expand Down

0 comments on commit f4d8b26

Please sign in to comment.