-
Notifications
You must be signed in to change notification settings - Fork 75
Split confulent lesions scil_labels_from_mask #1308
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,6 +8,8 @@ | |
| import numpy as np | ||
| from scipy import ndimage as ndi | ||
| from scipy.spatial import cKDTree | ||
| from skimage.segmentation import watershed | ||
| from skimage.feature import peak_local_max | ||
|
|
||
| from scilpy.tractanalysis.reproducibility_measures import compute_bundle_adjacency_voxel | ||
|
|
||
|
|
@@ -72,7 +74,7 @@ def get_binary_mask_from_labels(atlas, label_list): | |
|
|
||
|
|
||
| def get_labels_from_mask(mask_data, labels=None, background_label=0, | ||
| min_voxel_count=0): | ||
| min_voxel_count=0, min_distance=0): | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It would be clearer if it was None and if your condition was |
||
| """ | ||
| Get labels from a binary mask which contains multiple blobs. Each blob | ||
| will be assigned a label, by default starting from 1. Background will | ||
|
|
@@ -90,14 +92,34 @@ def get_labels_from_mask(mask_data, labels=None, background_label=0, | |
| min_voxel_count: int, optional | ||
| Minimum number of voxels for a blob to be considered. Blobs with fewer | ||
| voxels will be ignored. | ||
| min_distance : int, optional | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it should be explicit in the docstring that this trigger a change to a watershed algorithm |
||
| The minimum voxels separating the peaks of two | ||
| neighboring blobs. If a confluent blob is detected, it will be | ||
| split into multiple labels based on this distance. If None, no | ||
| splitting is performed. | ||
|
|
||
| Returns | ||
| ------- | ||
| label_map: np.ndarray | ||
| The labels. | ||
| """ | ||
| # Get the number of structures and assign labels to each blob | ||
| label_map, nb_structures = ndi.label(mask_data) | ||
| if min_distance: | ||
| distance = ndi.distance_transform_edt(mask_data) | ||
| coords = peak_local_max( | ||
| distance, | ||
| min_distance=min_distance, | ||
| labels=mask_data, | ||
| threshold_abs=0 | ||
| ) | ||
| mask = np.zeros(distance.shape, dtype=bool) | ||
| mask[tuple(coords.T)] = True | ||
| markers, _ = ndi.label(mask) | ||
| label_map = watershed(-distance, markers, mask=mask_data) | ||
| nb_structures = np.max(label_map) | ||
| else: | ||
| label_map, nb_structures = ndi.label(mask_data) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe a small comment saying "Any contigous regions touching even by a single voxel will be considered a single label" |
||
|
|
||
| if min_voxel_count: | ||
| new_count = 0 | ||
| for label in range(1, nb_structures + 1): | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this will allow you to do what you want here:
p.add_argument('--watershed', metavar='MIN_DISTANCE', type=int, nargs='?', const=1, help='Use a watershed algorithm, if provided the parameter (default: 1 voxel) allow to ...')
This make so p.watershed is None by default, if you use --watershep it is 1 by default, but you can do --watershed 10 to change it.