Fix combine_masks function: Handle numpy arrays properly to prevent ValueError
Browse files- Fix ambiguous truth value error when masks is a numpy array
- Add proper type checking for numpy arrays vs lists
- Handle edge cases (scalar arrays, empty arrays, multi-dimensional arrays)
- Improve error handling and logging
utils.py
CHANGED
|
@@ -131,17 +131,39 @@ def group_images_by_subject(image_files: List[str]) -> Dict[str, Dict[str, Any]]
|
|
| 131 |
return subject_groups
|
| 132 |
|
| 133 |
|
| 134 |
-
def combine_masks(masks
|
| 135 |
"""
|
| 136 |
Combine multiple mask arrays into a single mask.
|
| 137 |
|
| 138 |
Args:
|
| 139 |
-
masks: List of mask arrays
|
| 140 |
|
| 141 |
Returns:
|
| 142 |
Combined mask array or None if no valid masks
|
| 143 |
"""
|
| 144 |
-
if
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 145 |
return None
|
| 146 |
|
| 147 |
mask_arrays = []
|
|
@@ -152,17 +174,22 @@ def combine_masks(masks: List[np.ndarray]) -> Optional[np.ndarray]:
|
|
| 152 |
# Try to convert to numpy
|
| 153 |
try:
|
| 154 |
mask_np = np.array(mask)
|
| 155 |
-
|
|
|
|
| 156 |
except Exception as e:
|
| 157 |
-
logger.
|
| 158 |
continue
|
| 159 |
|
| 160 |
-
if
|
| 161 |
return None
|
| 162 |
|
| 163 |
# Combine all masks using logical OR
|
| 164 |
-
|
| 165 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 166 |
|
| 167 |
|
| 168 |
def create_output_image(
|
|
|
|
| 131 |
return subject_groups
|
| 132 |
|
| 133 |
|
| 134 |
+
def combine_masks(masks) -> Optional[np.ndarray]:
|
| 135 |
"""
|
| 136 |
Combine multiple mask arrays into a single mask.
|
| 137 |
|
| 138 |
Args:
|
| 139 |
+
masks: List of mask arrays, or numpy array, or None
|
| 140 |
|
| 141 |
Returns:
|
| 142 |
Combined mask array or None if no valid masks
|
| 143 |
"""
|
| 144 |
+
if masks is None:
|
| 145 |
+
return None
|
| 146 |
+
|
| 147 |
+
# Handle numpy array input (convert to list)
|
| 148 |
+
if isinstance(masks, np.ndarray):
|
| 149 |
+
if masks.ndim == 0: # Scalar
|
| 150 |
+
return None
|
| 151 |
+
elif masks.ndim == 1: # 1D array - might be empty
|
| 152 |
+
if len(masks) == 0:
|
| 153 |
+
return None
|
| 154 |
+
masks = [masks] # Convert to list
|
| 155 |
+
else: # Multi-dimensional array - treat as single mask
|
| 156 |
+
return masks
|
| 157 |
+
|
| 158 |
+
# Handle list/tuple input
|
| 159 |
+
if not isinstance(masks, (list, tuple)):
|
| 160 |
+
# Try to convert to list
|
| 161 |
+
try:
|
| 162 |
+
masks = list(masks)
|
| 163 |
+
except Exception:
|
| 164 |
+
return None
|
| 165 |
+
|
| 166 |
+
if len(masks) == 0:
|
| 167 |
return None
|
| 168 |
|
| 169 |
mask_arrays = []
|
|
|
|
| 174 |
# Try to convert to numpy
|
| 175 |
try:
|
| 176 |
mask_np = np.array(mask)
|
| 177 |
+
if mask_np.size > 0: # Only add non-empty arrays
|
| 178 |
+
mask_arrays.append(mask_np)
|
| 179 |
except Exception as e:
|
| 180 |
+
logger.debug(f"Could not convert mask to numpy: {e}")
|
| 181 |
continue
|
| 182 |
|
| 183 |
+
if len(mask_arrays) == 0:
|
| 184 |
return None
|
| 185 |
|
| 186 |
# Combine all masks using logical OR
|
| 187 |
+
try:
|
| 188 |
+
combined_mask = np.any(mask_arrays, axis=0)
|
| 189 |
+
return combined_mask
|
| 190 |
+
except Exception as e:
|
| 191 |
+
logger.error(f"Error combining masks: {e}", exc_info=True)
|
| 192 |
+
return None
|
| 193 |
|
| 194 |
|
| 195 |
def create_output_image(
|