Skip to content

Commit

Permalink
Fix gradient inversion (#196)
Browse files Browse the repository at this point in the history
* update weights instead of distances while inverting gradient

* add credential file to gitignore

* add info in default values

* Log location of pack

* fix linting

* add invert options based on weight or distance

* * invert distances after checking for outliers
* check for outliers in weights

* updating tests
  • Loading branch information
mogres authored Oct 13, 2023
1 parent 9105668 commit 5da57e0
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 11 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -120,3 +120,6 @@ cellpack/tests/outputs/
# data
data/
**/converted/*

# credentials
.creds
16 changes: 14 additions & 2 deletions cellpack/autopack/Gradient.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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):
Expand Down
4 changes: 2 additions & 2 deletions cellpack/autopack/interface_objects/default_values.py
Original file line number Diff line number Diff line change
Expand Up @@ -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": {},
}
20 changes: 20 additions & 0 deletions cellpack/autopack/interface_objects/gradient_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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"] = {}
Expand Down
2 changes: 2 additions & 0 deletions cellpack/bin/pack.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down
6 changes: 3 additions & 3 deletions cellpack/tests/recipes/v2/test_mesh_image_gradient.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"weight_mode": "exponential",
"pick_mode": "rnd",
"mode": "vector",
"invert": true,
"invert": "weight",
"mode_settings": {
"direction": [
0.707,
Expand All @@ -34,7 +34,7 @@
]
},
"weight_mode_settings": {
"decay_length": 0.05
"decay_length": 0.3
}
}
},
Expand All @@ -61,7 +61,7 @@
1,
1
],
"radius": 0.1,
"radius": 1,
"packing_mode": "gradient",
"gradient": "planar_gradient"
}
Expand Down
8 changes: 4 additions & 4 deletions cellpack/tests/test_gradient_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand All @@ -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],
Expand All @@ -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": {},
Expand All @@ -101,7 +101,7 @@
"description": "power law gradient from surface",
"pick_mode": "linear",
"reversed": False,
"invert": False,
"invert": None,
"mode_settings": {
"object": "object_name",
},
Expand Down

0 comments on commit 5da57e0

Please sign in to comment.