Skip to content

Commit

Permalink
Export packing outputs as a tiff file (#166)
Browse files Browse the repository at this point in the history
* refactor loops over ingredients and compartments in ImageWriter

* linting

* formatting with black

* remove duplicated image export
  • Loading branch information
mogres authored Jun 23, 2023
1 parent c337176 commit 80dc23d
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 50 deletions.
1 change: 0 additions & 1 deletion cellpack/autopack/Analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
from cellpack.autopack.upy.colors import map_colors
from cellpack.autopack.utils import check_paired_key, get_paired_key
from cellpack.autopack.writers.ImageWriter import ImageWriter

import concurrent.futures
import multiprocessing

Expand Down
79 changes: 73 additions & 6 deletions cellpack/autopack/Environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -585,7 +585,8 @@ def save_result(

self.log.info("time to save result file %d", time() - t0)
if vAnalysis == 1:
# START Analysis Tools: Graham added back this big chunk of code for analysis tools and graphic on 5/16/12
# START Analysis Tools: Graham added back this big chunk of code
# for analysis tools and graphic on 5/16/12
# Needs to be cleaned up into a function and proper uPy code
# totalVolume = self.grid.gridVolume*unitVol
unitVol = self.grid.gridSpacing**3
Expand Down Expand Up @@ -754,8 +755,9 @@ def save_result(
self.log.info("self.gridVolume = %d", self.grid.gridVolume)
self.log.info("histoVol.timeUpDistLoopTotal = %d", self.timeUpDistLoopTotal)

# END Analysis Tools: Graham added back this big chunk of code for analysis tools and graphic on 5/16/12
# Needs to be cleaned up into a function and proper uPy code
# END Analysis Tools: Graham added back this big chunk of code
# for analysis tools and graphic on 5/16/12
# Needs to be cleaned up into a function and proper uPy code
self.log.info("time to save end %d", time() - t0)

def loadResult(
Expand Down Expand Up @@ -1392,7 +1394,6 @@ def buildGrid(self, rebuild=True):
if self.previous_grid_file is not None:
distance = self.grid.distToClosestSurf # [:]
nbFreePoints = nbPoints # -1
# Graham turned this off on 5/16/12 to match August Repair for May Hybrid
for i, mingrs in enumerate(
self.molecules
): # ( jtrans, rotMatj, self, ptInd )
Expand Down Expand Up @@ -1724,8 +1725,9 @@ def pickIngredient(self, vThreshStart, verbose=0):
ingr = self.activeIngr[0]
else:
# prob = uniform(vRangeStart,1.0)
# Graham 9/21/11 This is wrong...vRangeStart is the point index,
# need active list i.e. thresholdPriority to be limited
# #Graham 9/21/11 This is wrong...
# vRangeStart is the point index, need active list
# i.e. thresholdPriority to be limited
prob = uniform(0, 1.0)
ingrInd = 0
for threshProb in self.thresholdPriorities:
Expand Down Expand Up @@ -3350,3 +3352,68 @@ def applyStep(self, step):
# if self.traj.traj_type=="dcd" or self.traj.traj_type=="xyz":
self.traj.applyState_primitive_name(self, step)
# ho can we apply to parent instance the rotatiotn?

def create_voxelization(self, image_data, image_size, voxel_size, hollow=False):
"""
Update the image data for all molecules in the recipe by creating voxelized
representations.
Parameters
----------
image_data: numpy.ndarray
The image data to update.
image_size: list
The size of the image data.
voxel_size: float
The size of a voxel in the image data.
hollow: bool
If True, the voxelization will be hollow.
Returns
----------
image_data: numpy.ndarray
The updated image data.
"""
channel_colors = []
for pos, rot, ingr, _ in self.molecules:
if ingr.name not in image_data:
image_data[ingr.name] = numpy.zeros(image_size, dtype=numpy.uint8)
if ingr.color is not None:
color = ingr.color
if all([x <= 1 for x in ingr.color]):
color = [int(col * 255) for col in ingr.color]
channel_colors.append(color)

image_data[ingr.name] = ingr.create_voxelization(
image_data=image_data[ingr.name],
bounding_box=self.boundingBox,
voxel_size=voxel_size,
image_size=image_size,
position=pos,
rotation=rot,
)

for compartment in self.compartments:
if compartment.name not in image_data:
image_data[compartment.name] = numpy.zeros(
image_size, dtype=numpy.uint8
)
if hasattr(compartment, "color") and compartment.color is not None:
color = compartment.color
if all([x <= 1 for x in compartment.color]):
color = [int(col * 255) for col in compartment.color]
channel_colors.append(color)
else:
channel_colors.append([0, 255, 0])

image_data[compartment.name] = compartment.create_voxelization(
image_data=image_data[compartment.name],
bounding_box=self.boundingBox,
voxel_size=voxel_size,
image_size=image_size,
position=compartment.position,
mesh_store=self.mesh_store,
hollow=hollow,
)

return image_data, channel_colors
46 changes: 3 additions & 43 deletions cellpack/autopack/writers/ImageWriter.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,50 +178,10 @@ def create_voxelization(self):
"""
Creates a voxelized representation of the current scene
"""
channel_colors = []

for pos, rot, ingr, _ in self.env.molecules:
if ingr.name not in self.image_data:
self.image_data[ingr.name] = numpy.zeros(
self.image_size, dtype=numpy.uint8
)
if ingr.color is not None:
color = ingr.color
if all([x <= 1 for x in ingr.color]):
color = [int(col * 255) for col in ingr.color]
channel_colors.append(color)

self.image_data[ingr.name] = ingr.create_voxelization(
image_data=self.image_data[ingr.name],
bounding_box=self.env.boundingBox,
voxel_size=self.voxel_size,
image_size=self.image_size,
position=pos,
rotation=rot,
)

for compartment in self.env.compartments:
if compartment.name not in self.image_data:
self.image_data[compartment.name] = numpy.zeros(
self.image_size, dtype=numpy.uint8
)
if hasattr(compartment, "color") and compartment.color is not None:
color = compartment.color
if all([x <= 1 for x in compartment.color]):
color = [int(col * 255) for col in compartment.color]
channel_colors.append(color)
else:
channel_colors.append([0, 255, 0])

self.image_data[compartment.name] = compartment.create_voxelization(
image_data=self.image_data[compartment.name],
bounding_box=self.env.boundingBox,
voxel_size=self.voxel_size,
image_size=self.image_size,
position=compartment.position,
mesh_store=self.env.mesh_store,
hollow=self.hollow,
)
self.image_data, channel_colors = self.env.create_voxelization(
self.image_data, self.image_size, self.voxel_size, self.hollow
)

concatenated_image = numpy.zeros(
(len(self.image_data), *self.image_size), dtype=numpy.uint8
Expand Down
30 changes: 30 additions & 0 deletions cellpack/tests/packing-configs/test_image_config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"name": "test_image_config_convolved",
"format": "simularium",
"inner_grid_method": "trimesh",
"live_packing": false,
"ordered_packing": false,
"out": "out/",
"overwrite_place_method": true,
"place_method": "spheresSST",
"save_analyze_result": true,
"show_grid_plot": true,
"number_of_packings": 1,
"spacing": null,
"use_periodicity": false,
"show_sphere_trees": false,
"load_from_grid_file": true,
"save_converted_recipe": true,
"image_export_options": {
"hollow": false,
"voxel_size": [1,1,1],
"projection_axis": "z",
"convolution_options": {
"psf": "gaussian",
"psf_parameters":{
"sigma": 2,
"size": [5,5,5]
}
}
}
}

0 comments on commit 80dc23d

Please sign in to comment.