Skip to content

Commit

Permalink
* rename histo to histogram
Browse files Browse the repository at this point in the history
* add documentation for image export
* move validation to `Ingredient.py`
  • Loading branch information
mogres committed Jul 6, 2023
1 parent 40da3be commit b526258
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 70 deletions.
30 changes: 15 additions & 15 deletions cellpack/autopack/Analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -407,9 +407,9 @@ def grabResultFromTXT(self, n, doanalyze=False):
numpy.array(e3[2]),
delimiter=",",
)
self.histo(e3[0], ingrname + "_euler_X.png")
self.histo(e3[1], ingrname + "_euler_Y.png")
self.histo(e3[2], ingrname + "_euler_Z.png")
self.histogram(e3[0], ingrname + "_euler_X.png")
self.histogram(e3[1], ingrname + "_euler_Y.png")
self.histogram(e3[2], ingrname + "_euler_Z.png")
return ingrpos, ingrrot

# should take any type of list...
Expand Down Expand Up @@ -491,7 +491,7 @@ def rdf_3d(self, ingr):
numpy.array(self.env.ingredient_positions[ingr.name]),
delimiter=",",
)
self.histo(distances, basename + ingr.name + "_histo.png")
self.histogram(distances, basename + ingr.name + "_histo.png")
numpy.savetxt(
basename + ingr.name + "_distances.csv",
numpy.array(distances),
Expand Down Expand Up @@ -600,7 +600,7 @@ def rdf_2d(self, ingr):
numpy.array(self.env.ingredient_positions[ingr.name]),
delimiter=",",
)
self.histo(distances, basename + ingr.name + "_histo.png")
self.histogram(distances, basename + ingr.name + "_histo.png")
numpy.savetxt(
basename + ingr.name + "_distances.csv",
numpy.array(distances),
Expand Down Expand Up @@ -654,7 +654,7 @@ def plot_position_distribution_total(self, all_positions):
pos_sph = self.cartesian_to_sph(pos_xyz)
all_pos = numpy.hstack([pos_xyz, pos_sph])
for ind, dim in enumerate(self.get_list_of_dims()):
self.histo(
self.histogram(
all_pos[:, ind],
self.figures_path
/ f"all_ingredient_histo_{dim}_{self.env.basename}.png",
Expand All @@ -670,7 +670,7 @@ def plot_position_distribution(self, ingr):
pos_sph = self.cartesian_to_sph(pos_xyz)
all_pos = numpy.hstack([pos_xyz, pos_sph])
for ind, dim in enumerate(self.get_list_of_dims()):
self.histo(
self.histogram(
all_pos[:, ind],
self.figures_path / f"{ingr.name}_histo_{dim}_{self.env.basename}.png",
title_str=ingr.name,
Expand All @@ -691,7 +691,7 @@ def plot_occurence_distribution(self, ingr):
x_label="seed",
y_label="occurrences",
)
self.histo(
self.histogram(
distances=numpy.array(occ),
filename=self.figures_path
/ f"{ingr.name}_occurrence_{self.env.basename}_histo.png",
Expand All @@ -707,7 +707,7 @@ def plot_distance_distribution(self, all_ingredient_distances):
for ingr_key, distances in all_ingredient_distances.items():
if len(distances) <= 1:
continue
self.histo(
self.histogram(
distances=numpy.array(distances),
filename=self.figures_path
/ f"{ingr_key}_pairwise_distances_{self.env.basename}.png",
Expand Down Expand Up @@ -1704,7 +1704,7 @@ def get_parametrized_representation(

return all_spilr

def histo(self, distances, filename, title_str="", x_label="", y_label=""):
def histogram(self, distances, filename, title_str="", x_label="", y_label=""):
plt.clf()
# calculate histogram
nbins = int(numpy.sqrt(len(distances)))
Expand Down Expand Up @@ -2584,7 +2584,7 @@ def doloop(

# plot histograms for all combined distances
if len(all_center_distance_array) > 1:
self.histo(
self.histogram(
all_center_distance_array,
self.figures_path
/ f"all_ingredient_center_distances_{self.env.basename}.png",
Expand All @@ -2594,7 +2594,7 @@ def doloop(
)

if len(all_center_distance_array) > 1:
self.histo(
self.histogram(
all_pairwise_distance_array,
self.figures_path
/ f"all_ingredient_pairwise_distances_{self.env.basename}.png",
Expand All @@ -2605,21 +2605,21 @@ def doloop(

# plot the angle
if len(all_ingredient_angle_array) > 1:
self.histo(
self.histogram(
all_ingredient_angle_array[0],
self.figures_path / f"all_angles_X_{self.env.basename}.png",
title_str="all_ingredients",
x_label="angles X",
y_label="count",
)
self.histo(
self.histogram(
all_ingredient_angle_array[1],
self.figures_path / f"all_angles_Y_{self.env.basename}.png",
title_str="all_ingredients",
x_label="angles Y",
y_label="count",
)
self.histo(
self.histogram(
all_ingredient_angle_array[2],
self.figures_path / f"all_angles_Z_{self.env.basename}.png",
title_str="all_ingredients",
Expand Down
8 changes: 8 additions & 0 deletions cellpack/autopack/Compartment.py
Original file line number Diff line number Diff line change
Expand Up @@ -3131,6 +3131,14 @@ def create_voxelized_mask(
):
"""
Creates a mask of the compartment voxelization
:param x_width: number of voxels in the x direction
:param y_width: number of voxels in the y direction
:param z_width: number of voxels in the z direction
:param center: center of the voxelization
:param voxel_size: size of the voxels
:param mesh_store: mesh store
:param hollow: if True, the mask will be hollow otherwise it will fill in the segmentation
"""
if voxel_size is None:
voxel_size = numpy.array([1, 1, 1], dtype=int)
Expand Down
58 changes: 3 additions & 55 deletions cellpack/autopack/Environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,6 @@
from .trajectory import dcdTrajectory, molbTrajectory
from .randomRot import RandomRot

from cellpack.autopack.interface_objects.meta_enum import MetaEnum

try:
helper = autopack.helper
except ImportError:
Expand All @@ -108,29 +106,6 @@
verbose = 0


class CountDistributions(MetaEnum):
"All available count distributions"
UNIFORM = "uniform"
NORMAL = "normal"
LIST = "list"


class CountOptions(MetaEnum):
"All available count options"
MIN = "min"
MAX = "max"
MEAN = "mean"
STD = "std"
LIST_VALUES = "list_values"


REQUIRED_COUNT_OPTIONS = {
CountDistributions.UNIFORM: [CountOptions.MIN, CountOptions.MAX],
CountDistributions.NORMAL: [CountOptions.MEAN, CountOptions.STD],
CountDistributions.LIST: [CountOptions.LIST_VALUES],
}


class Environment(CompartmentList):
"""
The Environment class
Expand Down Expand Up @@ -340,35 +315,6 @@ def setSeed(self, seedNum):
self.seed_set = True
self.seed_used = SEED

@staticmethod
def validate_ingredient_info(ingredient_info):
"""
Validates ingredient info and returns validated ingredient info
"""
if "count" not in ingredient_info:
raise Exception("Ingredient info must contain a count")

if ingredient_info["count"] < 0:
raise Exception("Ingredient count must be greater than or equal to 0")

if "count_options" in ingredient_info:
count_options = ingredient_info["count_options"]
if "distribution" not in count_options:
raise Exception("Ingredient count options must contain a distribution")
if not CountDistributions.is_member(count_options["distribution"]):
raise Exception(
f"{count_options['distribution']} is not a valid count distribution"
)
for required_option in REQUIRED_COUNT_OPTIONS.get(
count_options["distribution"], []
):
if required_option not in count_options:
raise Exception(
f"Missing option '{required_option}' for {count_options['distribution']} distribution"
)

return ingredient_info

def _prep_ingredient_info(self, composition_info, ingredient_name=None):
objects_dict = self.recipe_data["objects"]
object_key = composition_info["object"]
Expand All @@ -379,7 +325,9 @@ def _prep_ingredient_info(self, composition_info, ingredient_name=None):
ingredient_name if ingredient_name is not None else object_key
)
ingredient_info["object_name"] = object_key
ingredient_info = self.validate_ingredient_info(ingredient_info)
ingredient_info = ingredient.Ingredient.validate_ingredient_info(
ingredient_info
)
return ingredient_info

def _step_down(self, compartment_key, prev_compartment=None):
Expand Down
53 changes: 53 additions & 0 deletions cellpack/autopack/ingredient/Ingredient.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,37 @@
from cellpack.autopack.upy.simularium.simularium_helper import simulariumHelper
import cellpack.autopack as autopack
from cellpack.autopack.ingredient.agent import Agent
from cellpack.autopack.interface_objects.meta_enum import MetaEnum

helper = autopack.helper
reporthook = None
if helper is not None:
reporthook = helper.reporthook


class CountDistributions(MetaEnum):
"All available count distributions"
UNIFORM = "uniform"
NORMAL = "normal"
LIST = "list"


class CountOptions(MetaEnum):
"All available count options"
MIN = "min"
MAX = "max"
MEAN = "mean"
STD = "std"
LIST_VALUES = "list_values"


REQUIRED_COUNT_OPTIONS = {
CountDistributions.UNIFORM: [CountOptions.MIN, CountOptions.MAX],
CountDistributions.NORMAL: [CountOptions.MEAN, CountOptions.STD],
CountDistributions.LIST: [CountOptions.LIST_VALUES],
}


class IngredientInstanceDrop:
def __init__(self, ptId, position, rotation, ingredient, rb=None):
self.ptId = ptId
Expand Down Expand Up @@ -336,6 +360,35 @@ def __init__(
self.organism = ""
# add tiling property ? as any ingredient coud tile as hexagon. It is just the packing type

@staticmethod
def validate_ingredient_info(ingredient_info):
"""
Validates ingredient info and returns validated ingredient info
"""
if "count" not in ingredient_info:
raise Exception("Ingredient info must contain a count")

if ingredient_info["count"] < 0:
raise Exception("Ingredient count must be greater than or equal to 0")

if "count_options" in ingredient_info:
count_options = ingredient_info["count_options"]
if "distribution" not in count_options:
raise Exception("Ingredient count options must contain a distribution")
if not CountDistributions.is_member(count_options["distribution"]):
raise Exception(
f"{count_options['distribution']} is not a valid count distribution"
)
for required_option in REQUIRED_COUNT_OPTIONS.get(
count_options["distribution"], []
):
if required_option not in count_options:
raise Exception(
f"Missing option '{required_option}' for {count_options['distribution']} distribution"
)

return ingredient_info

def reset(self):
"""reset the states of an ingredient"""
self.counter = 0
Expand Down

0 comments on commit b526258

Please sign in to comment.