Changed workflow for setting the masking value in the skull stripping#171
Changed workflow for setting the masking value in the skull stripping#171nicmuenster wants to merge 10 commits into
Conversation
… process to either a global custom value for all inputs or the minimum value within the input image
|
/format |
|
🤖 I will now format your code with black. Check the status here. |
There was a problem hiding this comment.
Pull request overview
Aligns skull-stripping mask application with the defacing workflow by supporting either a global masking/background value or a per-image minimum when filling voxels outside the brain mask.
Changes:
- Added
masking_valuesupport toBrainExtractorand updatedapply_mask()to fill outside-mask voxels using either the provided global value or the per-image minimum. - Updated
HDBetExtractorandSynthStripExtractorconstructors to accept/forwardmasking_value. - Minor whitespace cleanup in the registration package init.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
brainles_preprocessing/registration/__init__.py |
Removes stray blank lines/whitespace. |
brainles_preprocessing/brain_extraction/brain_extractor.py |
Introduces masking_value state and updates mask application to use background fill logic consistent with defacing. |
brainles_preprocessing/brain_extraction/synthstrip.py |
Adds masking_value parameter to the extractor constructor (currently not applied during extract() output writing). |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| def __init__( | ||
| self, border: int = 1, masking_value: Optional[Union[int, float]] = None | ||
| ): |
There was a problem hiding this comment.
masking_value is added to the constructor and forwarded to BrainExtractor, but SynthStripExtractor.extract() never uses self.masking_value when writing the masked output (it still hard-codes bg = np.min([0, img_data.min()])). This makes the new parameter ineffective and the docstring misleading. Update extract() to fill background using the same masking_value/per-image-min logic as BrainExtractor.apply_mask (or drop the parameter if not supported).
| # check whether a global masking value was passed, otherwise choose minimum | ||
| if self.masking_value is None: | ||
| current_masking_value = np.min(input_data) | ||
| else: | ||
| current_masking_value = ( | ||
| np.array(self.masking_value).astype(input_data.dtype).item() | ||
| ) | ||
| # Apply mask (element-wise either input or masking value) | ||
| masked_data = np.where( | ||
| mask_data.astype(bool), input_data, current_masking_value | ||
| ) |
There was a problem hiding this comment.
apply_mask() now supports a global masking_value and falls back to per-image minimum when unset, but the existing unit test only asserts that an output file is created. Please extend tests to assert that voxels outside the mask are set to the expected value for both cases (default min-per-image and a custom masking_value).
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
neuronflow
left a comment
There was a problem hiding this comment.
Do we need changes here?
|
From my understanding, updating the synthstrip init is still necessary, given that while the center modality might be handled without the apply_mask function in the preprocessor workflow (as seen here)
the moving modalities are then afterwards still handled by the apply_mask function:
|
|
@nicmuenster merge conflict needs to be resolved :) |
|
/format |
|
🤖 I will now format your code with black. Check the status here. |
|
@uturkbey do you have time to look into this? |
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.
Comments suppressed due to low confidence (1)
brainles_preprocessing/brain_extraction/brain_extractor.py:85
- The masking workflow now has two branches (explicit
masking_valuevs per-image minimum) and changes the masking semantics from multiplication tonp.where(..., current_masking_value), but existing tests only assert that an output file is created. Please add assertions covering the new behavior (e.g., that voxels outside the mask equalnp.min(input_data)whenmasking_value=None, and equal the provided value whenmasking_valueis set) to prevent regressions.
# check whether a global masking value was passed, otherwise choose minimum
if self.masking_value is None:
current_masking_value = np.min(input_data)
else:
current_masking_value = (
|
|
||
|
|
||
| class HDBetExtractor(BrainExtractor): | ||
| def __init__(self, masking_value: Optional[Union[int, float]] = None): | ||
| """ |
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.
Comments suppressed due to low confidence (2)
brainles_preprocessing/brain_extraction/synthstrip.py:39
masking_valueis now accepted and passed intoBrainExtractor, but SynthStrip still hard-codes the masked background value insideextract()(seebg = np.min([0, img_data.min()])andimg_data[mask == 0] = bgat lines 225-226). This means center-modality skull-stripped outputs written bySynthStripExtractor.extract()will ignore the newmasking_valuebehavior described in the docstring/PR.
border (int): Mask border threshold in mm. Defaults to 1.
masking_value (Optional[Union[int, float]], optional): global value to be inserted in the masked areas. Default is None which leads to the minimum of each respective image.
"""
super().__init__(masking_value=masking_value)
brainles_preprocessing/brain_extraction/brain_extractor.py:79
- The new
masking_valuebehavior inapply_mask()(global override vs per-image minimum) is not covered by assertions in the existing tests. Current tests only check that an output file is created, so regressions in the chosen background value or dtype casting would go unnoticed.
# check whether a global masking value was passed, otherwise choose minimum
if self.masking_value is None:
current_masking_value = np.min(input_data)
else:
current_masking_value = (
| from typing import Optional, Union | ||
| from enum import Enum | ||
| import numpy as np |
| Args: | ||
| masking_value (Optional[Union[int, float]], optional): global value to be inserted in the masked areas. Default is None which leads to the minimum of each respective image. | ||
| """ | ||
| super().__init__(masking_value=masking_value) |
@nicmuenster please also have a look at the surpressed comments |
|
I implmented the first comment, but I am not sure about the second one to be honest. I would also maybe first implement the modality specific masking values before creating specific tests for edge cases |
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 3 out of 3 changed files in this pull request and generated no new comments.
Comments suppressed due to low confidence (2)
brainles_preprocessing/brain_extraction/brain_extractor.py:6
Enumis imported but not used in this module. Keeping unused imports can confuse readers and may fail linting if/when a linter is added.
from enum import Enum
import numpy as np
brainles_preprocessing/brain_extraction/brain_extractor.py:85
- New masking behavior (global
masking_valuevs per-image minimum) is implemented here, but existing tests only assert that an output file is created. Add unit coverage that verifies masked voxels are set to the configured value, and thatNoneuses the input image minimum.
# check whether a global masking value was passed, otherwise choose minimum
if self.masking_value is None:
current_masking_value = np.min(input_data)
else:
current_masking_value = (
np.array(self.masking_value).astype(input_data.dtype).item()
)
# Apply mask (element-wise either input or masking value)
masked_data = np.where(
mask_data.astype(bool), input_data, current_masking_value
)
This change implements the the same process as in the defacing step to either use a global custom value for all inputs or the minimum value within the input image as the background value when masking an inage