diff --git a/.gitignore b/.gitignore index b493ebfd6..2588d41a2 100644 --- a/.gitignore +++ b/.gitignore @@ -120,3 +120,6 @@ cellpack/tests/outputs/ # data data/ **/converted/* + +# credentials +.creds \ No newline at end of file diff --git a/cellpack/autopack/Gradient.py b/cellpack/autopack/Gradient.py index be8dbf7b8..3c21ba9fe 100644 --- a/cellpack/autopack/Gradient.py +++ b/cellpack/autopack/Gradient.py @@ -207,12 +207,15 @@ def set_weights_by_mode(self): self.scaled_distances = self.get_normalized_values(self.distances) - if (max(self.scaled_distances) > 1.0) or (min(self.scaled_distances) < 0.0): + if (numpy.nanmax(self.scaled_distances) > 1.0) or ( + numpy.nanmin(self.scaled_distances) < 0.0 + ): raise ValueError( "CHECK CALCULATED DISTANCES", f"Max: {max(self.scaled_distances)}, Min: {min(self.scaled_distances)}", ) - if self.invert: + + if self.invert == "distance": self.scaled_distances = 1.0 - self.scaled_distances if self.weight_mode == "linear": self.weight = 1.0 - self.scaled_distances @@ -231,8 +234,17 @@ def set_weights_by_mode(self): # normalize the weight self.weight = self.get_normalized_values(self.weight) + if (numpy.nanmax(self.weight) > 1.0) or (numpy.nanmin(self.weight) < 0.0): + raise ValueError( + "CHECK CALCULATED WEIGHTS", + f"Max: {max(self.weight)}, Min: {min(self.weight)}", + ) + self.weight[numpy.isnan(self.weight)] = 0 + if self.invert == "weight": + self.weight = 1.0 - self.weight + # TODO: talk to Ludo about calculating gaussian weights def getMaxWeight(self, listPts): diff --git a/cellpack/autopack/interface_objects/default_values.py b/cellpack/autopack/interface_objects/default_values.py index 3ff2a468d..18d82716d 100644 --- a/cellpack/autopack/interface_objects/default_values.py +++ b/cellpack/autopack/interface_objects/default_values.py @@ -8,8 +8,8 @@ "weight_mode": "linear", "pick_mode": "linear", "description": "Linear gradient in the X direction", - "reversed": False, - "invert": False, + "reversed": False, # is the direction of the vector reversed? + "invert": None, # options: "weight", "distance" "mode_settings": {}, "weight_mode_settings": {}, } diff --git a/cellpack/autopack/interface_objects/gradient_data.py b/cellpack/autopack/interface_objects/gradient_data.py index db2c4666f..94df2692c 100644 --- a/cellpack/autopack/interface_objects/gradient_data.py +++ b/cellpack/autopack/interface_objects/gradient_data.py @@ -60,6 +60,15 @@ class ModeOptions(MetaEnum): scale_distance_between = "scale_distance_between" +class InvertOptions(MetaEnum): + """ + All available options for individual invert modes + """ + + weight = "weight" + distance = "distance" + + class WeightModeOptions(MetaEnum): """ All available options for individual weight modes @@ -118,6 +127,7 @@ def validate_gradient_data(self, gradient_data): self.validate_weight_mode_settings( gradient_data["weight_mode"], gradient_data.get("weight_mode_settings") ) + self.validate_invert_settings(gradient_data["invert"]) def validate_mode_settings(self, mode_name, mode_settings_dict): required_options = REQUIRED_MODE_OPTIONS.get(mode_name) @@ -151,6 +161,16 @@ def validate_weight_mode_settings( f"Missing required weight mode setting {option} for {weight_mode_name}" ) + def validate_invert_settings( + self, + invert_option, + ): + if not invert_option: + return + + if not InvertOptions.is_member(invert_option): + raise ValueError(f"Invalid gradient invert option: {invert_option}") + def set_mode_properties(self, gradient_data): if not gradient_data.get("mode_settings"): gradient_data["mode_settings"] = {} diff --git a/cellpack/bin/pack.py b/cellpack/bin/pack.py index a9a4ab84f..54608b8dc 100644 --- a/cellpack/bin/pack.py +++ b/cellpack/bin/pack.py @@ -29,6 +29,8 @@ def pack(recipe, config_path=None, analysis_config_path=None): :return: void """ + log.info(f"Running in {__file__}") + packing_config_data = ConfigLoader(config_path).config recipe_data = RecipeLoader( recipe, packing_config_data["save_converted_recipe"] diff --git a/cellpack/tests/recipes/v2/test_mesh_image_gradient.json b/cellpack/tests/recipes/v2/test_mesh_image_gradient.json index 7b22560f8..56487f05c 100644 --- a/cellpack/tests/recipes/v2/test_mesh_image_gradient.json +++ b/cellpack/tests/recipes/v2/test_mesh_image_gradient.json @@ -20,7 +20,7 @@ "weight_mode": "exponential", "pick_mode": "rnd", "mode": "vector", - "invert": true, + "invert": "weight", "mode_settings": { "direction": [ 0.707, @@ -34,7 +34,7 @@ ] }, "weight_mode_settings": { - "decay_length": 0.05 + "decay_length": 0.3 } } }, @@ -61,7 +61,7 @@ 1, 1 ], - "radius": 0.1, + "radius": 1, "packing_mode": "gradient", "gradient": "planar_gradient" } diff --git a/cellpack/tests/test_gradient_data.py b/cellpack/tests/test_gradient_data.py index 0242723d1..3cb5a70ed 100644 --- a/cellpack/tests/test_gradient_data.py +++ b/cellpack/tests/test_gradient_data.py @@ -22,7 +22,7 @@ "pick_mode": "linear", "description": "Linear gradient in the X direction", "reversed": False, - "invert": False, + "invert": None, "name": "gradient_name", "mode_settings": { "direction": [1, 0, 0], @@ -49,7 +49,7 @@ "pick_mode": "linear", "description": "Square gradient in the radial direction", "reversed": False, - "invert": False, + "invert": None, "name": "gradient_name", "mode_settings": { "center": [0, 0, 0], @@ -74,7 +74,7 @@ "pick_mode": "linear", "description": "Cubic gradient in the Y direction", "reversed": True, - "invert": False, + "invert": None, "name": "gradient_name", "mode_settings": {"direction": [0, -1, 0]}, "weight_mode_settings": {}, @@ -101,7 +101,7 @@ "description": "power law gradient from surface", "pick_mode": "linear", "reversed": False, - "invert": False, + "invert": None, "mode_settings": { "object": "object_name", },