Skip to content

Commit

Permalink
change to compartment_id
Browse files Browse the repository at this point in the history
  • Loading branch information
meganrm committed Sep 21, 2023
1 parent 3b97426 commit b665e77
Show file tree
Hide file tree
Showing 13 changed files with 101 additions and 109 deletions.
6 changes: 3 additions & 3 deletions cellpack/autopack/Compartment.py
Original file line number Diff line number Diff line change
Expand Up @@ -503,10 +503,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 +516,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
70 changes: 35 additions & 35 deletions cellpack/autopack/Environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -509,7 +509,7 @@ def save_result(
self.store()
Writer(format=self.format_output).save(
self,
kwds=["compNum"],
kwds=["compartment_id"],
result=True,
quaternion=True,
all_ingr_as_array=all_ingr_as_array,
Expand Down Expand Up @@ -1057,11 +1057,11 @@ def getIngrFromNameInRecipe(self, name, r):
# return ingr
return None

def get_ingredient_by_name(self, name, compNum=None):
def get_ingredient_by_name(self, name, compartment_id=None):
"""
Given an ingredient name and optionally the compartment number, retrieve the ingredient object instance
"""
if compNum is None:
if compartment_id is None:
r = self.exteriorRecipe
ingr = self.getIngrFromNameInRecipe(name, r)
if ingr is not None:
Expand All @@ -1075,23 +1075,23 @@ def get_ingredient_by_name(self, name, compNum=None):
ingr = self.getIngrFromNameInRecipe(name, ri)
if ingr is not None:
return ingr
elif compNum == 0:
elif compartment_id == 0:
r = self.exteriorRecipe
ingr = self.getIngrFromNameInRecipe(name, r)
if ingr is not None:
return ingr
else:
return None
elif compNum > 0:
o = self.compartments[compNum - 1]
elif compartment_id > 0:
o = self.compartments[compartment_id - 1]
rs = o.surfaceRecipe
ingr = self.getIngrFromNameInRecipe(name, rs)
if ingr is not None:
return ingr
else:
return None
else: # <0
o = self.compartments[(compNum * -1) - 1]
o = self.compartments[(compartment_id * -1) - 1]
ri = o.innerRecipe
ingr = self.getIngrFromNameInRecipe(name, ri)
if ingr is not None:
Expand All @@ -1107,7 +1107,7 @@ def setExteriorRecipe(self, recipe):
self.exteriorRecipe = recipe
recipe.compartment = self # weakref.ref(self)
for ingr in recipe.ingredients:
ingr.compNum = 0
ingr.compartment_id = 0

def BuildCompartmentsGrids(self):
"""
Expand Down Expand Up @@ -1260,7 +1260,7 @@ def onePrevIngredient(self, i, mingrs, distance, nbFreePoints, marray):
centT = ingr.transformPoints(jtrans, rotMatj, ingr.positions[-1])
insidePoints = {}
newDistPoints = {}
mr = self.get_dpad(ingr.compNum)
mr = self.get_dpad(ingr.compartment_id)
spacing = self.smallestProteinSize
jitter = ingr.getMaxJitter(spacing)
dpad = ingr.min_radius + mr + jitter
Expand Down Expand Up @@ -1566,12 +1566,12 @@ def pickIngredient(self, vThreshStart, verbose=0):
# print (r,n,ingr.name,len(self.activeIngr)) #Graham turned back on 5/16/12, but may be costly
return ingr

def get_dpad(self, compNum):
def get_dpad(self, compartment_id):
"""Return the largest encapsulating_radius and use it for padding"""
mr = 0.0
if compNum == 0: # cytoplasm -> use cyto and all surfaces
if compartment_id == 0: # cytoplasm -> use cyto and all surfaces
for ingr1 in self.activeIngr:
if ingr1.compNum >= 0:
if ingr1.compartment_id >= 0:
if hasattr(ingr1, "max_radius"):
r = ingr1.max_radius
else:
Expand All @@ -1580,7 +1580,7 @@ def get_dpad(self, compNum):
mr = r
else:
for ingr1 in self.activeIngr:
if ingr1.compNum == compNum or ingr1.compNum == -compNum:
if ingr1.compartment_id == compartment_id or ingr1.compartment_id == -compartment_id:
if hasattr(ingr1, "max_radius"):
r = ingr1.max_radius
else:
Expand Down Expand Up @@ -2003,20 +2003,20 @@ def pack_grid(
)
if self.afviewer.renderDistance:
self.afviewer.vi.displayParticleVolumeDistance(distances, self)
current_ingr_compartment = ingr.compNum
current_ingr_compartment = ingr.compartment_id
# compute dpad which is the distance at which we need to update
# distances after the drop is successfull
max_radius = self.get_dpad(current_ingr_compartment)

self.log.info(
f"picked Ingr radius {ingr.min_radius}, compNum {current_ingr_compartment}"
f"picked Ingr radius {ingr.min_radius}, compartment_id {current_ingr_compartment}"
)

# find the points that can be used for this ingredient
##

if ingr.compNum > 0:
compartment = self.compartments[ingr.compNum - 1]
if ingr.compartment_id > 0:
compartment = self.compartments[ingr.compartment_id - 1]
surface_points = compartment.surfacePoints
res = [True, surface_points[int(random() * len(surface_points))]]
else:
Expand Down Expand Up @@ -2159,7 +2159,7 @@ def pack_grid(
nbFreePoints,
)
stime = time()
self.log.info(f"placed {len(self.packed_objects)}")
self.log.info(f"placed {len(self.packed_objects.get())}")
if self.saveResult:
self.save_result(
free_points,
Expand Down Expand Up @@ -2187,7 +2187,7 @@ def restore_molecules_array(self, ingr):
# if len(ingr.results):
# for elem in ingr.results:
# TODO: fix this to reset ingredients from results
# if ingr.compNum == 0:
# if ingr.compartment_id == 0:
# self.molecules.append(
# [elem[0], numpy.array(elem[1]), ingr, 0, ingr.radius]
# )
Expand All @@ -2198,18 +2198,18 @@ def restore_molecules_array(self, ingr):

def restore(self, result, orgaresult, freePoint, tree=False):
# should we used the grid ? the freePoint can be computed
# result is [pos,rot,ingr.name,ingr.compNum,ptInd]
# orgaresult is [[pos,rot,ingr.name,ingr.compNum,ptInd],[pos,rot,ingr.name,ingr.compNum,ptInd]...]
# result is [pos,rot,ingr.name,ingr.compartment_id,ptInd]
# orgaresult is [[pos,rot,ingr.name,ingr.compartment_id,ptInd],[pos,rot,ingr.name,ingr.compartment_id,ptInd]...]
# after restore we can build the grid and fill!
# ingredient based dictionary
# TODO: refactor with new packed_objects

ingredients = {}
molecules = []
for elem in result:
pos, rot, name, compNum, ptInd = elem
pos, rot, name, compartment_id, ptInd = elem
# needto check the name if it got the comp rule
ingr = self.get_ingredient_by_name(name, compNum)
ingr = self.get_ingredient_by_name(name, compartment_id)
if ingr is not None:
molecules.append([pos, numpy.array(rot), ingr, ptInd])
if name not in ingredients:
Expand All @@ -2226,8 +2226,8 @@ def restore(self, result, orgaresult, freePoint, tree=False):
for i, o in enumerate(self.compartments):
molecules = []
for elem in orgaresult[i]:
pos, rot, name, compNum, ptInd = elem
ingr = self.get_ingredient_by_name(name, compNum)
pos, rot, name, compartment_id, ptInd = elem
ingr = self.get_ingredient_by_name(name, compartment_id)
if ingr is not None:
molecules.append([pos, numpy.array(rot), ingr, ptInd])
if name not in ingredients:
Expand Down Expand Up @@ -2309,10 +2309,10 @@ def getOneIngrJson(self, ingr, ingrdic):
return (
ingrdic["results"],
ingr.composition_name,
ingr.compNum,
ingr.compartment_id,
1,
ingr.encapsulating_radius,
) # ingrdic["compNum"],1,ingrdic["encapsulating_radius"]
) # ingrdic["compartment_id"],1,ingrdic["encapsulating_radius"]

def load_asTxt(self, resultfilename=None):
if resultfilename is None:
Expand Down Expand Up @@ -2511,7 +2511,7 @@ def load_asJson(self, resultfilename=None):

def dropOneIngrJson(self, ingr, rdic):
adic = OrderedDict() # [ingr.name]
adic["compNum"] = ingr.compNum
adic["compartment_id"] = ingr.compartment_id
adic["encapsulating_radius"] = float(ingr.encapsulating_radius)
adic["results"] = []
for r in ingr.results:
Expand Down Expand Up @@ -2600,17 +2600,17 @@ def convertPickleToText(self, resultfilename=None, norga=0):
rfile.close()
rfile = open(resultfilename + ".txt", "w")
line = ""
for pos, rot, ingrName, compNum, ptInd in result:
line += self.dropOneIngr(pos, rot, ingrName, compNum, ptInd)
# result.append([pos,rot,ingr.name,ingr.compNum,ptInd])
for pos, rot, ingrName, compartment_id, ptInd in result:
line += self.dropOneIngr(pos, rot, ingrName, compartment_id, ptInd)
# result.append([pos,rot,ingr.name,ingr.compartment_id,ptInd])
rfile.write(line)
rfile.close()
for i in range(norga):
orfile = open(resultfilename + "_organelle_" + str(i) + ".txt", "w")
result = []
line = ""
for pos, rot, ingrName, compNum, ptInd in orgaresult[i]:
line += self.dropOneIngr(pos, rot, ingrName, compNum, ptInd)
for pos, rot, ingrName, compartment_id, ptInd in orgaresult[i]:
line += self.dropOneIngr(pos, rot, ingrName, compartment_id, ptInd)
orfile.write(line)
orfile.close()
# freepoint
Expand Down Expand Up @@ -3100,7 +3100,7 @@ def create_voxelization(self, image_data, image_size, voxel_size, hollow=False):
"""
channel_colors = []
for obj in self.packed_objects.get():

ingredient = obj.ingredient
if obj.name not in image_data:
image_data[obj.name] = numpy.zeros(image_size, dtype=numpy.uint8)
if obj.color is not None:
Expand All @@ -3113,7 +3113,7 @@ def create_voxelization(self, image_data, image_size, voxel_size, hollow=False):
mesh_store = self.mesh_store

else:
obj_instance = self.get_ingredient_class(obj.ingredient_type)
obj_instance = self.get_ingredient_class(ingredient.ingredient_type)
mesh_store = None

image_data[obj.name] = obj_instance.create_voxelization(
Expand Down
12 changes: 6 additions & 6 deletions cellpack/autopack/Graphics.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,10 +241,10 @@ def addMasterIngr(self, ingr, parent=None):
@type parent: hostObject
@param parent: specify a parent to insert under
"""
if ingr.compNum == 0:
if ingr.compartment_id == 0:
compartment = self.histo
else:
compartment = self.histo.compartments[abs(ingr.compNum) - 1]
compartment = self.histo.compartments[abs(ingr.compartment_id) - 1]
name = "%s_%s" % (ingr.name, compartment.name)
gi = self.checkCreateEmpty(name, parent=parent)
self.orgaToMasterGeom[ingr] = gi
Expand Down Expand Up @@ -1205,7 +1205,7 @@ def displayIngrResults(self, ingredient, doSphere=False, doMesh=True):
print("no ingredient provided")
return
o = ingredient.recipe.compartment
# comp = ingredient.compNum
# comp = ingredient.compartment_id
verts = []
radii = []
matrices = []
Expand Down Expand Up @@ -1986,13 +1986,13 @@ def addIngredientFromGeom(self, name, ingrobj, recipe=None, **kw):
if ingr is not None:
if recipe is None:
self.histo.exteriorRecipe.addIngredient(ingr)
ingr.compNum = 0
ingr.compartment_id = 0
g = self.vi.getObject(self.name + "_cytoplasm")
self.addMasterIngr(ingr, parent=g)
ingr.env = self.histo
else:
recipe.addIngredient(ingr)
ingr.compNum = recipe.number
ingr.compartment_id = recipe.number
# g = self.vi.getObject("O" + o.name)
ingr.env = self.histo
rep = self.vi.getObject(ingr.composition_name + "_mesh")
Expand All @@ -2002,7 +2002,7 @@ def addIngredientFromGeom(self, name, ingrobj, recipe=None, **kw):
ingr.meshFile = autopack.cache_geoms + os.sep + ingr.composition_name
ingr.meshName = ingr.composition_name + "_mesh"
ingr.saveDejaVuMesh(ingr.meshFile)
self.addMasterIngr(ingr, parent=self.orgaToMasterGeom[ingr.compNum])
self.addMasterIngr(ingr, parent=self.orgaToMasterGeom[ingr.compartment_id])
return ingr

def addCompartmentFromGeom(self, name, obj, **kw):
Expand Down
Loading

0 comments on commit b665e77

Please sign in to comment.