Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improving Mesh class documentation #38

Closed
wants to merge 23 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
4bdb71c
Update scaffold.py
greeyun Aug 12, 2024
5062d5e
Added some dependencies.
MMal151 Aug 12, 2024
070a183
Mesh class for merging multiple meshes from the Scaffold class
frenchmatthew Aug 12, 2024
5198797
Merge pull request #29 from SPARC-FAIR-Codeathon/main
Moffhan Aug 12, 2024
b2654e0
Add zenodo.json
savindi-wijenayaka Aug 12, 2024
f482398
Merge pull request #31 from SPARC-FAIR-Codeathon/main_michael
MMal151 Aug 12, 2024
cba9a4c
Merge pull request #32 from SPARC-FAIR-Codeathon/zenodo
MMal151 Aug 12, 2024
1daac9e
Update scaffold.py
greeyun Aug 12, 2024
a1058a2
typo
frenchmatthew Aug 12, 2024
3de42a1
Merge pull request #30 from SPARC-FAIR-Codeathon/mesh_generation
savindi-wijenayaka Aug 12, 2024
fccb42f
Added some dependencies.
MMal151 Aug 12, 2024
53b7d1b
Add zenodo.json
savindi-wijenayaka Aug 12, 2024
ecf1e6f
Mesh class for merging multiple meshes from the Scaffold class
frenchmatthew Aug 12, 2024
b42a096
typo
frenchmatthew Aug 12, 2024
03738f4
Merge pull request #35 from SPARC-FAIR-Codeathon/clean-main
MMal151 Aug 12, 2024
8aeee0e
add export to stl in main
frenchmatthew Aug 12, 2024
dfd3600
updated __init__.py to include Meshes class
frenchmatthew Aug 12, 2024
1b9f3fe
inherit from PyVista multi-block and overload class with functionalit…
frenchmatthew Aug 12, 2024
e616d2a
integrate meshes class into scaffold class and test functionality is …
frenchmatthew Aug 12, 2024
df5dedc
rename mesh to meshes
frenchmatthew Aug 12, 2024
9c9b6fd
Remove redundant code
frenchmatthew Aug 13, 2024
869b688
use Meshes functionality to add mesh
frenchmatthew Aug 13, 2024
89e0340
add comment to mesh class
frenchmatthew Aug 13, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions .zenodo.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"creators": [
{
"orcid": "0000-0003-1876-7042",
"affiliation": "Auckland Bioengineering Institute, University of Auckland",
"name": "Hoffman, Michael"
},
{
"orcid": "0000-0002-5929-9794",
"affiliation": "Auckland Bioengineering Institute, University of Auckland",
"name": "Wijenayaka, Savindi"
},
{
"orcid": "0000-0002-4917-7144",
"affiliation": "Auckland Bioengineering Institute, University of Auckland",
"name": "Malik, Mishaim"
},
{
"orcid": "0000-0003-0175-3488",
"affiliation": "Auckland Bioengineering Institute, University of Auckland",
"name": "French, Matthew"
},
{
"orcid": "0000-0003-4758-769X",
"affiliation": "Auckland Bioengineering Institute, University of Auckland",
"name": "Gu, Yun"
}
],

"license": "Apache-2.0",

"title": "SPARC Python Imager (SPARC-SPy)"
}
2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ libstdcxx-ng==14.1.0
pandas==2.2.2
hurry.filesize==0.9
tabulate==0.9.0
sparc-me==3.0.0
numpy==2.0.1
2 changes: 2 additions & 0 deletions src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@
scaffold.update_mesh_label(original_name="unnamed", new_name="whole_stomach")

scaffold.plot()

scaffold.export("output.stl")
2 changes: 1 addition & 1 deletion src/sparc_spy/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
from sparc_spy.mesh import Mesh
from sparc_spy.meshes import Meshes
from sparc_spy.scaffold import Scaffold
3 changes: 0 additions & 3 deletions src/sparc_spy/mesh.py

This file was deleted.

106 changes: 106 additions & 0 deletions src/sparc_spy/meshes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
import os
import shutil
import pyvista as pv
from io import StringIO


class Meshes(pv.MultiBlock):
def __init__(self) -> None:
"""
This class is a subclass of pyvista.MultiBlock.
It is used to store multiple meshes with labels from the scaffold.
"""
super().__init__()

def add_mesh(self, label: str, mesh: pv.PolyData):
"""
This function adds a mesh to the MultiBlock object.
Args:
label (str): Label of the mesh.
mesh (pv.PolyData): Mesh to be added.
"""
self.append(label, mesh)

def export(self, output_filepath: str = "output.stl", base_path='.'):
"""
This function exports the meshes in the MultiBlock object to a single STL file.
Args:
output_filepath (str): Output file path.
base_path (str): Base path.
"""
save_multiblock_stl(self, output_filepath, base_path)

def items(self):
'''
This function should return the label and meshes (pv.PolyData) in the MultiBlock object.
Returns:
list: List of tuples containing label and mesh.
'''
blocks = []
for name in self.keys():
blocks.append((name, self[name]))

return blocks


def save_multiblock_stl(multiblock, filename, base_path='.'):
"""
This function saves the meshes in the MultiBlock object to a single STL file.
Args:
multiblock (MultiBlock): MultiBlock object containing meshes.
filename (str): Output file path.
base_path (str): Base path.
"""

names = multiblock.keys()
oname, ext = os.path.splitext(filename)
assert ext == '.stl'

# each individual stl file saved (output_filenames)
ofiles = [os.path.join(base_path, f'{oname}_{ii}') + '.stl' for ii in range(len(names))]

for ii, subpart in enumerate(multiblock):
subpart.save(ofiles[ii], binary=False)
change_first_line_of_file(ofiles[ii], f'solid {names[ii]}') # basically changes "solid" to "solid <solid_name>"

# merge files together
total_stl = ''
for fn in ofiles:
f = open(fn)
total_stl += f.read()
f.close()

# writes total stl file
total_file_name = os.path.join(base_path, oname + '.stl')
with open(total_file_name, 'w') as f:
f.write(total_stl)

# deletes previously written stl files
for fn in ofiles:
os.remove(fn)

return

def change_first_line_of_file(filename, new_first_line):
"""
This function changes the first line of a file.
Args:
filename (str): File name.
new_first_line (str): New first line.
"""

fr = open(filename, 'r')
first_line = fr.readline()
fr.close()
first_line_len = len(first_line)

new_first_line_len = len(new_first_line)
spaces_num = first_line_len - new_first_line_len
new_first_line = new_first_line + ' '*(spaces_num-1) + '\n'
fw = StringIO(new_first_line)
fr = open(filename, 'r+')
shutil.copyfileobj(fw, fr)
fr.close()
fw.close()
return

22 changes: 12 additions & 10 deletions src/sparc_spy/scaffold.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import numpy as np
import pyvista as pv

from sparc_spy import Mesh
from sparc_spy import Meshes


def populate_metadata(paths):
Expand Down Expand Up @@ -83,11 +83,11 @@ def populate_metadata(paths):


class Scaffold(object):
meshes: Dict
meshes: Meshes

def __init__(self, name, derivative_dir: str):
self.name = name
self.meshes = {}
self.meshes = Meshes()
self.metadata = populate_metadata(self.__read_jsons(derivative_dir))
self.geometry = self.build_scaffold(derivative_dir)

Expand Down Expand Up @@ -118,7 +118,7 @@ def build_scaffold(self, derivative_dir: str):
vertices = vertices.reshape(-1, 3)

mesh = pv.PolyData(vertices, faces)
self.meshes[label] = mesh
self.meshes[label] = mesh

def plot(self):
pv.global_theme.color_cycler = ["#DA627D", "#33658A", "#86BBD8", "#06969A", "#9A348E"]
Expand All @@ -128,26 +128,28 @@ def plot(self):
pl.add_legend()
pl.show()

def export(self, output_filepath: str = "output.vtk"):
"""Export the scaffold to a .vtk file
def export(self, output_filepath: str = "output.stl", base_path="."):
"""Export the scaffold to a .stl file

Args:
output_filepath (str, optional): output_filepath (str): Output file
path to save .vtk file. Defaults to "output.vtk".
"""
path to save .stl file. Defaults to "output.vtk".
"""
self.meshes.export(output_filepath, base_path)


def get_metadata(self):
"""Show a tabular view of metadata that is important to the user"""
return

def add_mesh(self, mesh_name: str, mesh: Mesh):
def add_mesh(self, mesh_name: str, mesh: pv.PolyData):
"""Modify self.meshes and add a new mesh to the list.

Args:
mesh_name (str): User defined name for the mesh
mesh (Mesh): Mesh containing new experimental data
"""
return
self.meshes[mesh_name] = mesh

def get_mesh_details(self):
"""List all meshes with their corresponding user defined names. These IDs
Expand Down