Skip to content

Commit

Permalink
* add progress bar
Browse files Browse the repository at this point in the history
* disable save dumps
* update compartment grid saving and loading
* load specific compartment grid attributes
* add power-law and exponential weight mode fro gradients
  • Loading branch information
mogres committed Jul 7, 2023
1 parent 6e5d762 commit f383ee2
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 54 deletions.
1 change: 0 additions & 1 deletion cellpack/autopack/Analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -2423,7 +2423,6 @@ def doloop(
"""
if seed_list is None:
seed_list = self.getHaltonUnique(number_of_packings)

packing_basename = f"{self.env.name}_{config_name}_{recipe_version}"
numpy.savetxt(
self.env.out_folder / f"seeds_{packing_basename}.txt",
Expand Down
124 changes: 81 additions & 43 deletions cellpack/autopack/Environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@
from .randomRot import RandomRot

from cellpack.autopack.interface_objects.meta_enum import MetaEnum
from tqdm import tqdm

try:
helper = autopack.helper
Expand Down Expand Up @@ -167,6 +168,7 @@ def __init__(self, config=None, recipe=None):
self.boundingBox = numpy.array(recipe["bounding_box"])
self.spacing = config["spacing"]
self.load_from_grid_file = config["load_from_grid_file"]
self.show_progress_bar = config["show_progress_bar"]
self.name = name
self.version = recipe.get("version", "default")
# saving/pickle option
Expand Down Expand Up @@ -292,7 +294,7 @@ def __init__(self, config=None, recipe=None):
self.rb_func_dic = {}
# need options for the save/server data etc....
# should it be in __init__ like other general options ?
self.dump = True
self.dump = False
self.dump_freq = 120.0
self.jsondic = None

Expand Down Expand Up @@ -1016,7 +1018,6 @@ def saveGridToFile(self, gridFileOut):
Save the current grid and the compartment grid information in a file. (pickle)
"""
d = os.path.dirname(gridFileOut)
print("SAVED GRID TO ", gridFileOut)
if not os.path.exists(d):
print("gridfilename path problem", gridFileOut)
return
Expand All @@ -1025,6 +1026,7 @@ def saveGridToFile(self, gridFileOut):

for compartment in self.compartments:
compartment.saveGridToFile(f)
print("SAVED GRID TO ", gridFileOut)
f.close()

def saveGridLogsAsJson(self, gridFileOut):
Expand Down Expand Up @@ -1053,26 +1055,60 @@ def saveGridLogsAsJson(self, gridFileOut):
json.dump(data, fp=f)
f.close()

@staticmethod
def get_attributes_to_update():
return [
"faces",
"vertices",
"vnormals",
"filename",
"ref_obj",
"bb",
"center",
"encapsulating_radius",
"radius",
"insidePoints",
"surfacePoints",
"surfacePointsCoords",
"surfacePointsNormals",
"ogsurfacePoints",
"ogsurfacePointsNormals",
"OGsrfPtsBht",
"closestId",
]

def restore_grids_from_pickle(self, grid_file_path):
"""
Read and setup the grid from the given filename. (pickle)
"""
print(f"Loading grid from {grid_file_path}")

with open(grid_file_path, "rb") as file_obj:
# load env grid
self.grid = load_object_from_pickle(self.grid, file_obj)
grid_obj = load_object_from_pickle(file_obj)
self.grid = grid_obj

# load compartment grids
for ct, compartment in enumerate(self.compartments):
self.compartments[ct] = load_object_from_pickle(compartment, file_obj)
for ct, _ in enumerate(self.compartments):
comp_obj = load_object_from_pickle(file_obj)
for update_attr in self.get_attributes_to_update():
setattr(
self.compartments[ct],
update_attr,
getattr(comp_obj, update_attr),
)

# load mesh store
self.mesh_store = load_object_from_pickle(self.mesh_store, file_obj)
mesh_store_obj = load_object_from_pickle(file_obj)
self.mesh_store = mesh_store_obj

# clear the triangles_tree cache
for _, geom in self.mesh_store.scene.geometry.items():
geom._cache.delete("triangles_tree")

# reset grid
self.grid.reset()

def save_grids_to_pickle(self, grid_file_path):
"""
Save the current grid and compartment grids to file. (pickle)
Expand Down Expand Up @@ -1101,8 +1137,8 @@ def restoreGridFromFile(self, gridFileName):
f = open(gridFileName, "rb")
self.readArraysFromFile(f)
for ct, compartment in enumerate(self.compartments):
# compartment.readGridFromFile(f)
compartment = compartment.load_compartment_object(f)
compartment.readGridFromFile(f)
# compartment = compartment.load_compartment_object(f)
aInteriorGrids.append(compartment.insidePoints)
aSurfaceGrids.append(compartment.surfacePoints)
compartment.OGsrfPtsBht = spatial.cKDTree(
Expand Down Expand Up @@ -1417,45 +1453,42 @@ def buildGrid(self, rebuild=True):
else:
self.build_compartment_grids()

self.exteriorVolume = self.grid.computeExteriorVolume(
compartments=self.compartments,
space=self.smallestProteinSize,
fbox_bb=self.fbox_bb,
)
# save grids to pickle
# self.saveGridToFile(self.grid_file_out)
self.grid.filename = self.grid_file_out
self.previous_grid_file = self.grid_file_out
self.save_grids_to_pickle(self.grid_file_out)

r = self.exteriorRecipe
if r:
r.setCount(self.exteriorVolume) # should actually use the fillBB
self.exteriorVolume = self.grid.computeExteriorVolume(
compartments=self.compartments,
space=self.smallestProteinSize,
fbox_bb=self.fbox_bb,
)

if not rebuild:
for c in self.compartments:
c.setCount()
else:
self.grid.distToClosestSurf_store = self.grid.distToClosestSurf[:]
r = self.exteriorRecipe
if r:
r.setCount(self.exteriorVolume) # should actually use the fillBB

distance = self.grid.distToClosestSurf # [:]
nbFreePoints = nbPoints # -1
if not rebuild:
for c in self.compartments:
c.setCount()
else:
self.grid.distToClosestSurf_store = self.grid.distToClosestSurf[:]

distance = self.grid.distToClosestSurf # [:]
nbFreePoints = nbPoints # -1
for i, mingrs in enumerate(self.molecules): # ( jtrans, rotMatj, self, ptInd )
nbFreePoints = self.onePrevIngredient(
i, mingrs, distance, nbFreePoints, self.molecules
)
for organelle in self.compartments:
for i, mingrs in enumerate(
self.molecules
organelle.molecules
): # ( jtrans, rotMatj, self, ptInd )
nbFreePoints = self.onePrevIngredient(
i, mingrs, distance, nbFreePoints, self.molecules
i, mingrs, distance, nbFreePoints, organelle.molecules
)
for organelle in self.compartments:
for i, mingrs in enumerate(
organelle.molecules
): # ( jtrans, rotMatj, self, ptInd )
nbFreePoints = self.onePrevIngredient(
i, mingrs, distance, nbFreePoints, organelle.molecules
)
self.grid.nbFreePoints = nbFreePoints

# save grids to pickle
# self.saveGridToFile(self.grid_file_out)
self.grid.filename = self.grid_file_out
self.previous_grid_file = self.grid_file_out

self.save_grids_to_pickle(self.grid_file_out)
self.grid.nbFreePoints = nbFreePoints

if self.use_gradient and len(self.gradients) and rebuild:
for g in self.gradients:
Expand Down Expand Up @@ -1663,7 +1696,7 @@ def reset(self):
self.resetIngrRecip(r)
self.molecules = []
for orga in self.compartments:
# orga.reset()
orga.reset()
rs = orga.surfaceRecipe
self.resetIngrRecip(rs)
ri = orga.innerRecipe
Expand Down Expand Up @@ -2208,7 +2241,8 @@ def pack_grid(
dump_freq = self.dump_freq # 120.0#every minute
dump = self.dump
stime = time()

if self.show_progress_bar:
pbar = tqdm(total=totalNumMols, mininterval=0, miniters=1)
while nbFreePoints:
self.log.info(
".........At start of while loop, with vRangeStart = %d", vRangeStart
Expand Down Expand Up @@ -2252,7 +2286,6 @@ def pack_grid(
)
if self.afviewer.renderDistance:
self.afviewer.vi.displayParticleVolumeDistance(distances, self)

current_ingr_compartment = ingr.compNum
# compute dpad which is the distance at which we need to update
# distances after the drop is successfull
Expand Down Expand Up @@ -2342,6 +2375,8 @@ def pack_grid(
# if ingredient_too_close:
# print("GOT A FAIL", self.grid.masterGridPositions[ptInd])
# nbFreePoints = 0
if self.show_progress_bar:
pbar.update(1)
else:
self.log.info("rejected %r", ingr.rejectionCounter)

Expand Down Expand Up @@ -2399,6 +2434,7 @@ def pack_grid(
self.thresholdPriorities.append(np + previousThresh)
previousThresh = np + float(previousThresh)
self.activeIngr = self.activeIngr0 + self.activeIngr12

if dump and ((time() - stime) > dump_freq):
print("SAVING", self.result_file)
all_ingr_as_array = self.prep_molecules_for_save(
Expand All @@ -2420,6 +2456,8 @@ def pack_grid(
)

t2 = time()
if self.show_progress_bar:
pbar.close()
self.log.info("time to fill %d", t2 - t1)
all_ingr_as_array = self.prep_molecules_for_save(
distances, free_points, nbFreePoints
Expand Down
6 changes: 4 additions & 2 deletions cellpack/autopack/Gradient.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,8 +216,10 @@ def set_weights_by_mode(self):
elif self.weight_mode == "power":
self.weight = (1.0 - scaled_distances) ** self.weight_mode_settings["power"]
elif self.weight_mode == "exponential":
self.weight = numpy.exp(-scaled_distances / self.weight_mode_settings["decay_length"])

self.weight = numpy.exp(
-scaled_distances / self.weight_mode_settings["decay_length"]
)

self.weight[numpy.isnan(self.weight)] = 0
# TODO: talk to Ludo about calculating gaussian weights

Expand Down
8 changes: 6 additions & 2 deletions cellpack/autopack/interface_objects/gradient_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class WeightModes(MetaEnum):
"""
All available weight modes
"""

LINEAR = "linear"
SQUARE = "square"
CUBE = "cube"
Expand Down Expand Up @@ -63,6 +64,7 @@ class WeightModeOptions(MetaEnum):
"""
All available options for individual weight modes
"""

power = "power"
decay_length = "decay_length"

Expand Down Expand Up @@ -132,8 +134,10 @@ def validate_mode_settings(self, mode_name, mode_settings_dict):
raise ValueError(
f"Missing required mode setting {option} for {mode_name}"
)

def validate_weight_mode_settings(self, weight_mode_name, weight_mode_settings_dict):

def validate_weight_mode_settings(
self, weight_mode_name, weight_mode_settings_dict
):
required_options = REQUIRED_WEIGHT_MODE_OPTIONS.get(weight_mode_name)

if required_options is None:
Expand Down
1 change: 1 addition & 0 deletions cellpack/autopack/loaders/config_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ class ConfigLoader(object):
"save_converted_recipe": False,
"show_grid_plot": False,
"show_sphere_trees": False,
"show_progress_bar": False,
"spacing": None,
"use_periodicity": False,
"version": 1.0,
Expand Down
6 changes: 3 additions & 3 deletions cellpack/autopack/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,12 +172,12 @@ def get_paired_key(val_dict, key1=None, key2=None):
return key


def load_object_from_pickle(input_object, pickle_file_object):
def load_object_from_pickle(pickle_file_object):
"""
Update an object from a pickle file
"""
try:
input_object = pickle.load(pickle_file_object)
output_object = pickle.load(pickle_file_object)
except Exception as e:
raise ValueError(f"Error loading saved object: {e}")
return input_object
return output_object
4 changes: 1 addition & 3 deletions cellpack/tests/test_gradient_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,9 +135,7 @@ def test_gradient_data(input, expected_options):
{
"mode": "surface",
"weight_mode": "power",
"mode_settings": {
"object": "object_name"
}
"mode_settings": {"object": "object_name"},
},
"gradient_name",
),
Expand Down

0 comments on commit f383ee2

Please sign in to comment.