Skip to content

Commit 8c0a11e

Browse files
fix: update checks in set_colormap_label
1 parent 5783657 commit 8c0a11e

File tree

1 file changed

+20
-19
lines changed

1 file changed

+20
-19
lines changed

src/ipyniivue/widget.py

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -207,16 +207,14 @@ def set_colormap_label(self, color_map: dict):
207207
----------
208208
color_map : dict
209209
A dictionary containing the colormap information.
210-
It must have the following keys:
210+
It can have the following keys:
211211
212-
**Required keys**:
212+
**Optional keys**:
213213
- `'R'`: list of numbers
214214
- `'G'`: list of numbers
215215
- `'B'`: list of numbers
216216
- `'A'`: list of numbers
217217
- `'I'`: list of numbers
218-
219-
**Optional keys**:
220218
- `'min'`: number
221219
- `'max'`: number
222220
- `'labels'`: list of strings
@@ -237,23 +235,26 @@ def set_colormap_label(self, color_map: dict):
237235
nv.volumes[0].set_colormap_label(color_map)
238236
239237
"""
240-
# Validate that required keys are present and are lists of numbers
241-
required_keys = ["R", "G", "B", "A", "I"]
242-
for key in required_keys:
243-
if key not in color_map:
244-
raise ValueError(f"ColorMap must include required key '{key}'")
245-
if not isinstance(color_map[key], list):
246-
raise TypeError(f"ColorMap key '{key}' must be a list")
247-
if not all(isinstance(x, (int, float)) for x in color_map[key]):
248-
raise TypeError(f"All elements in ColorMap key '{key}' must be numbers")
238+
list_keys = ["R", "G", "B", "A", "I"]
239+
lengths = []
249240

250-
# Check that all required lists have the same length
251-
lengths = [len(color_map[key]) for key in required_keys]
252-
if len(set(lengths)) != 1:
241+
# Validate that any present list keys are lists of numbers, and collect lengths
242+
for key in list_keys:
243+
if key in color_map:
244+
if not isinstance(color_map[key], list):
245+
raise TypeError(f"ColorMap key '{key}' must be a list")
246+
if not all(isinstance(x, (int, float)) for x in color_map[key]):
247+
raise TypeError(f"All elements in ColorMap '{key}' must be numbers")
248+
lengths.append(len(color_map[key]))
249+
250+
# If more than one list is present, check that all lengths are equal
251+
if lengths and len(set(lengths)) != 1:
253252
raise ValueError(
254253
"All 'R', 'G', 'B', 'A', 'I' lists must have the same length"
255254
)
256255

256+
common_length = lengths[0] if lengths else None
257+
257258
# Validate optional keys
258259
if "min" in color_map and not isinstance(color_map["min"], (int, float)):
259260
raise TypeError("ColorMap 'min' must be a number")
@@ -266,10 +267,10 @@ def set_colormap_label(self, color_map: dict):
266267
raise TypeError("ColorMap 'labels' must be a list of strings")
267268
if not all(isinstance(label, str) for label in color_map["labels"]):
268269
raise TypeError("All elements in ColorMap 'labels' must be strings")
269-
if len(color_map["labels"]) != lengths[0]:
270+
# Ensure labels length matches
271+
if common_length is not None and len(color_map["labels"]) != common_length:
270272
raise ValueError(
271-
"ColorMap 'labels' must have the same length "
272-
"as 'R', 'G', 'B', 'A', 'I' lists"
273+
"ColorMap 'labels' must have the same length as other lists"
273274
)
274275

275276
self.send({"type": "set_colormap_label", "data": [color_map]})

0 commit comments

Comments
 (0)