44
55import numpy as np
66import pandas as pd
7+ import os
78from aicsshparam import shtools
89
910from datastep import Step , log_run_params
1011from .avgshape_utils import run_shcoeffs_analysis , save_mesh_as_stl
12+ from .avgshape_utils import get_smooth_and_coarse_mesh_from_voxelization
13+ from .avgshape_utils import save_mesh_as_obj , save_voxelization , save_displacement_map
1114
1215###############################################################################
1316
@@ -50,13 +53,10 @@ def run(self, sh_df=None, struct="Nuc", **kwargs):
5053 # If no dataframe is passed in, load manifest from previous step
5154 if sh_df is None :
5255 sh_df = pd .read_csv (
53- self .step_local_staging_dir .parent / "shparam_"
54- f"{ struct } " / "manifest.csv"
56+ self .step_local_staging_dir .parent / "shparam" /
57+ f"shparam_ { struct } " / "manifest.csv" , index_col = "CellId "
5558 )
5659
57- # Fix filepaths and use cell id as dataframe index
58- sh_df = sh_df .set_index ("CellId" , drop = True )
59-
6060 # Load sh coefficients of all samples in manifest
6161 coeffs_df = pd .DataFrame ([])
6262 for CellId in sh_df .index :
@@ -66,9 +66,20 @@ def run(self, sh_df=None, struct="Nuc", **kwargs):
6666 )
6767
6868 # Create directory to hold results from this step
69- avg_data_dir = self .step_local_staging_dir / "avgshape_data"
69+ struct_dir = self .step_local_staging_dir / f"avgshape_{ struct } "
70+ struct_dir .mkdir (parents = True , exist_ok = True )
71+
72+ avg_data_dir = struct_dir / f"avgshape_data"
7073 avg_data_dir .mkdir (parents = True , exist_ok = True )
7174
75+ # Move init and run parameters to structure dir to avoid overwriting
76+ for filetype in ["init" , "run" ]:
77+ filename = f"{ filetype } _parameters.json"
78+ os .rename (
79+ self .step_local_staging_dir / filename ,
80+ struct_dir / filename
81+ )
82+
7283 # Perform some per-cell analysis
7384 run_shcoeffs_analysis (df = coeffs_df , savedir = avg_data_dir , struct = struct )
7485
@@ -82,18 +93,42 @@ def run(self, sh_df=None, struct="Nuc", **kwargs):
8293 coeffs_avg = coeffs_avg .reshape (- 2 , lmax , lmax )
8394
8495 # Here we use the new meshing implementation for a more evenly distributed mesh
96+ '''
8597 mesh_avg, _ = shtools.get_even_reconstruction_from_coeffs(
8698 coeffs=coeffs_avg,
8799 npoints=1024
88100 )
101+ '''
102+
103+ mesh_avg , grid_avg = shtools .get_reconstruction_from_coeffs (
104+ coeffs = coeffs_avg ,
105+ )
89106
90107 shtools .save_polydata (
91108 mesh = mesh_avg ,
92109 filename = str (avg_data_dir / f"avgshape_{ struct } .ply" )
93110 )
94111
112+ # Save mesh as obj
113+ save_mesh_as_obj (mesh_avg , avg_data_dir / f"avgshape_{ struct } .obj" )
114+
115+ # Save displacement map
116+ save_displacement_map (grid_avg , avg_data_dir / f"avgshape_dmap_{ struct } .tif" )
117+
118+ # Save mesh as image
119+ domain = save_voxelization (mesh_avg , avg_data_dir / f"avgshape_{ struct } .tif" )
120+
95121 # Save mesh as stl file for blender import
96- save_mesh_as_stl (mesh_avg , str (avg_data_dir / f"avgshape_{ struct } .stl" ))
122+ save_mesh_as_stl (mesh_avg , avg_data_dir / f"avgshape_{ struct } .stl" )
123+
124+ # Remesh voxelization
125+ remesh_avg = get_smooth_and_coarse_mesh_from_voxelization (domain , sigma = 3 , npoints = 2000 )
126+
127+ # Save remesh as PLY
128+ shtools .save_polydata (
129+ mesh = remesh_avg ,
130+ filename = str (avg_data_dir / f"avgshape_remesh_{ struct } .ply" )
131+ )
97132
98133 # Save avg coeffs to csv file
99134 coeffs_df_avg .to_csv (
@@ -104,12 +139,16 @@ def run(self, sh_df=None, struct="Nuc", **kwargs):
104139 self .manifest = pd .DataFrame ({
105140 "Label" : "Average_mesh" ,
106141 "AvgShapeFilePath" : avg_data_dir / f"avgshape_{ struct } .ply" ,
142+ "AvgShapeRemeshFilePath" : avg_data_dir / f"avgshape_remesh_{ struct } .ply" ,
107143 "AvgShapeFilePathStl" : avg_data_dir / f"avgshape_{ struct } .stl" ,
144+ "AvgShapeFilePathObj" : avg_data_dir / f"avgshape_{ struct } .obj" ,
145+ "AvgShapeFilePathTif" : avg_data_dir / f"avgshape_{ struct } .tif" ,
146+ "AvgShapeDMapFilePathTif" : avg_data_dir / f"avgshape_dmap_{ struct } .tif" ,
108147 "Structure" : struct ,
109148 }, index = [0 ])
110149
111150 # Save manifest as csv
112151 self .manifest .to_csv (
113- self . step_local_staging_dir / Path (f"manifest.csv" ), index = False
152+ struct_dir / Path (f"manifest.csv" ), index = False
114153 )
115154 return self .manifest
0 commit comments