Skip to content

Commit

Permalink
Merge pull request #193 from mesoscope/feature/results-in-memory
Browse files Browse the repository at this point in the history
Feature/results in memory
  • Loading branch information
meganrm authored Oct 13, 2023
2 parents 5da57e0 + d35738d commit aae7ed1
Show file tree
Hide file tree
Showing 21 changed files with 892 additions and 869 deletions.
49 changes: 3 additions & 46 deletions cellpack/autopack/Analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ def getDistanceFrom(self, target, parents=None, **options):
objects : list of object or list of points
"""
# get distance from object to the target.
# all object are in h.molecules and orga.molecules
# all object are in env.packed_objects
# get options

if type(target) == list or type(target) == tuple:
Expand Down Expand Up @@ -447,23 +447,6 @@ def rectangle_circle_area(self, bbox, center, radius):
area = bbox[0][1] ** 2
return area

def getAxeValue(self, ingrname, axe=0):
ingredient_positions = [
self.env.molecules[i][0][axe]
for i in range(len(self.env.molecules))
if self.env.molecules[i][2].name == ingrname
]
return ingredient_positions

def getAxesValues(self, positions):
pp = numpy.array(positions).transpose()
if len(positions) == 0:
return 1, 1, 1
px = pp[0]
py = pp[1]
pz = pp[2]
return px, py, pz

def getVolumeShell(self, bbox, radii, center):
# rectangle_circle_area
volumes = []
Expand All @@ -473,9 +456,6 @@ def getVolumeShell(self, bbox, radii, center):
r2 = radii[i + 1]
v1 = self.g.calc_volume(r1, box_size0 / 2.0)
v2 = self.g.calc_volume(r2, box_size0 / 2.0)
# if v1 == 0 or v2 == 0 :
# volumes.append((4./3.)*numpy.pi*(numpy.power(r2,3)-numpy.power(r1, 3)))
# else :
volumes.append(v2 - v1)
return volumes

Expand Down Expand Up @@ -1774,7 +1754,7 @@ def pack(
t2 = time()
run_time = t2 - t1
print(f"time to run pack_grid for {self.env.place_method}: {run_time:0.2f}")
print(f"num placed: {len(self.env.molecules)}")
print(f"num placed: {len(self.env.packed_objects.get_ingredients())}")
if show_plotly_plot:
min_bound, max_bound = self.env.get_bounding_box_limits()
width = max_bound - min_bound
Expand All @@ -1787,7 +1767,7 @@ def pack(
range=[min_bound[1] - 0.2 * width[1], max_bound[1] + 0.2 * width[1]]
)
self.plotly.update_title(
f"{self.env.place_method} took {str(round(t2 - t1, 2))}s, packed {len(self.env.molecules)}"
f"{self.env.place_method} took {str(round(t2 - t1, 2))}s, packed {len(self.env.packed_objects.get_ingredients())}"
)
self.plotly.make_grid_heatmap(self.env)
self.plotly.add_ingredient_positions(self.env)
Expand Down Expand Up @@ -1875,29 +1855,6 @@ def plot_one_result_2d(
plt.close() # closes the current figure
return ingrpos

# res=plotOneResult(None,filename="results_seed_8.json")

def plot_one_result_3D(self, filename, width=1000.0):
plt.close("all") # closes the current figure
pos = []
s = []
c = []
for i in range(len(self.env.molecules)):
m = self.env.molecules[i]
pos.append(numpy.array(m[0]).tolist())
s.append(m[2].encapsulating_radius ** 2)
c.append(m[2].color)
fig = plt.figure()
ax = fig.gca(projection="3d")
x, y, z = numpy.array(pos).transpose()
ax.scatter(x, y, z, s=s, c=c)
ax.legend()
ax.set_xlim3d(0, width)
ax.set_ylim3d(0, width)
ax.set_zlim3d(0, width)
plt.savefig(filename)
return x, y, z, s, c

def set_ingredient_color(self, ingr):
"""
Sets the color of an ingredient
Expand Down
35 changes: 28 additions & 7 deletions cellpack/autopack/Compartment.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@
import cellpack.autopack as autopack
from cellpack.autopack import transformation as tr, binvox_rw
from cellpack.autopack.BaseGrid import gridPoint
from cellpack.autopack.interface_objects.packed_objects import (
PackedObject,
PackedObjects,
)
from .Recipe import Recipe
from .ray import (
makeMarchingCube,
Expand Down Expand Up @@ -138,6 +142,7 @@ def __init__(self, name, object_info):
self.ref_obj = None
self.meshType = None
self.representations = object_info["representations"]
self.packed_objects = PackedObjects()
if self.representations.has_mesh():
self.gname = self.representations.get_mesh_name()
self.meshType = self.representations.get_mesh_format()
Expand Down Expand Up @@ -172,7 +177,6 @@ def __init__(self, name, object_info):
# is added to a Environment. Positivefor surface pts
# negative for interior points
# self.parent = None
self.molecules = []
# list of ( (x,y,z), rotation, ingredient) triplet generated by fill
self.overwriteSurfacePts = True
# do we discretize surface point per edges
Expand Down Expand Up @@ -207,7 +211,6 @@ def reset(self):
# list of grid point indices on compartment surface
self.surfacePoints = None
self.surfacePointsNormals = {} # will be point index:normal
# self.molecules = []

def transformMesh(self, pos, rotation):
rot = tr.matrix_from_quaternion(rotation).transpose()
Expand Down Expand Up @@ -266,6 +269,17 @@ def initialize_shape(self, mesh_store):
self.encapsulating_radius = radius
self.radius = mesh_store.get_smallest_radius(self.gname, center)

def store_packed_object(self, env):
packed_object = PackedObject(
position=[0, 0, 0],
rotation=numpy.identity(4),
radius=self.radius,
pt_index=-1,
ingredient=self,
is_compartment=True,
)
env.packed_objects.add(packed_object)

def addShapeRB(self, env):
# in case our shape is a regular primitive
if self.stype == "capsule":
Expand Down Expand Up @@ -503,10 +517,10 @@ def setInnerRecipe(self, recipe):
self.innerRecipe.number = self.number
recipe.compartment = self # weakref.ref(self)
for ingr in recipe.ingredients:
ingr.compNum = -self.number
ingr.compartment_id = -self.number
if hasattr(ingr, "compMask"):
if not ingr.compMask:
ingr.compMask = [ingr.compNum]
ingr.compMask = [ingr.compartment_id]

def setSurfaceRecipe(self, recipe):
"""set the inner recipe that define the ingredient to pack at the surface"""
Expand All @@ -516,7 +530,7 @@ def setSurfaceRecipe(self, recipe):
self.surfaceRecipe.number = self.number
recipe.compartment = self # weakref.ref(self)
for ingr in recipe.ingredients:
ingr.compNum = self.number
ingr.compartment_id = self.number

def getCenter(self):
"""get the center of the mesh (vertices barycenter)"""
Expand Down Expand Up @@ -3200,12 +3214,19 @@ def create_voxelization(
voxel_size,
image_size,
position,
mesh_store,
hollow=False,
**kwargs,
):
"""
Creates a voxelization mask at the position of the compartment
"""
if "mesh_store" not in kwargs:
raise RuntimeError(
"mesh_store must be provided for compartment voxelization"
)

mesh_store = kwargs["mesh_store"]
hollow = kwargs.get("hollow", False)

relative_position = position - bounding_box[0]
mask = self.create_voxelized_mask(
*image_size,
Expand Down
Loading

0 comments on commit aae7ed1

Please sign in to comment.