Skip to content

Commit

Permalink
add invert options based on weight or distance
Browse files Browse the repository at this point in the history
  • Loading branch information
mogres committed Oct 9, 2023
1 parent 01424f2 commit 469ebe6
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 5 deletions.
5 changes: 4 additions & 1 deletion cellpack/autopack/Gradient.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,9 @@ def set_weights_by_mode(self):

self.scaled_distances = self.get_normalized_values(self.distances)

if self.invert == "distance":
self.scaled_distances = 1.0 - self.scaled_distances

if (max(self.scaled_distances) > 1.0) or (min(self.scaled_distances) < 0.0):
raise ValueError(
"CHECK CALCULATED DISTANCES",
Expand All @@ -231,7 +234,7 @@ def set_weights_by_mode(self):

self.weight[numpy.isnan(self.weight)] = 0

if self.invert:
if self.invert == "weight":
self.weight = 1.0 - self.weight

# TODO: talk to Ludo about calculating gaussian weights
Expand Down
2 changes: 1 addition & 1 deletion cellpack/autopack/interface_objects/default_values.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"pick_mode": "linear",
"description": "Linear gradient in the X direction",
"reversed": False, # is the direction of the vector reversed?
"invert": False, # is the gradient inverted? (this does weights = 1 - weights)
"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
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

0 comments on commit 469ebe6

Please sign in to comment.