Skip to content
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

Update SegmentationExtractor #366

Open
wants to merge 32 commits into
base: 0.6.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
986f264
renamed neuropil --> background
pauladkisson Sep 24, 2024
0362270
removed channels and num_planes
pauladkisson Sep 25, 2024
575fac8
renamed imagingextractor_mixin to imaging_extractor_mixin
pauladkisson Sep 25, 2024
0483635
removed channels
pauladkisson Sep 25, 2024
0aa7d8d
renamed imagingextractor_mixin to imaging_extractor_mixin
pauladkisson Sep 25, 2024
9ed3179
fixed bug with all None roi_responses
pauladkisson Sep 25, 2024
372fb8e
corrected _image_masks typo
pauladkisson Sep 25, 2024
0c11101
added initial tests and refactored segmentation extractor to use abst…
pauladkisson Sep 26, 2024
4bf2630
removed write_segmentation
pauladkisson Sep 26, 2024
21a8240
updated get_background_pixel_masks
pauladkisson Sep 26, 2024
116ddfb
removed properties from numpysegmentationextractor
pauladkisson Sep 26, 2024
b7a4411
reordered segmentation methods
pauladkisson Sep 26, 2024
28af405
split background response from roi responses
pauladkisson Sep 26, 2024
e135ffe
added some tests
pauladkisson Sep 26, 2024
e6e2e4b
added background_image_masks to numpyseg
pauladkisson Sep 26, 2024
232db7f
finished simple tests (w/o parameterization)
pauladkisson Sep 26, 2024
9daf8b7
fixed bug with num_background
pauladkisson Sep 27, 2024
367cc7b
refactored numpysegmentationextractor
pauladkisson Sep 27, 2024
7199605
extracted different init modes into separate fns
pauladkisson Sep 27, 2024
468e91a
updated docstring
pauladkisson Sep 27, 2024
809fda1
extracted get_default_roi_locations_from_image_masks to extraction_tools
pauladkisson Sep 27, 2024
8d8733c
extracted get_roi_indices to base segmentation clas
pauladkisson Sep 27, 2024
5c4005d
reordered methods to match base
pauladkisson Sep 27, 2024
76f325c
added tests for get_roi_indices
pauladkisson Sep 27, 2024
24de1fe
added parameterization for test
pauladkisson Sep 27, 2024
b3d9352
added parameterization for test
pauladkisson Sep 27, 2024
bf8b546
flattened parameterization
pauladkisson Sep 27, 2024
b031781
fixed typo
pauladkisson Sep 27, 2024
cbd1691
added tests fromFile
pauladkisson Sep 27, 2024
64aac0e
added TODO
pauladkisson Sep 27, 2024
fedb4bd
added frame_slice
pauladkisson Sep 28, 2024
2dd85af
get_args for compound types
pauladkisson Sep 30, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 37 additions & 10 deletions src/roiextractors/extraction_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
DtypeType = DTypeLike
IntType = Union[int, np.integer]
FloatType = float
NoneType = type(None)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this used? Is it needed? I think generally None is used in typehinting to express the None type



def raise_multi_channel_or_depth_not_implemented(extractor_name: str):
Expand Down Expand Up @@ -244,18 +245,16 @@ def read_numpy_memmap_video(
return video_memap


def _pixel_mask_extractor(image_mask_, _roi_ids) -> list:
"""Convert image mask to pixel mask.
def _pixel_mask_extractor(image_masks: np.ndarray) -> list:
"""Convert image masks to pixel masks.

Pixel masks are an alternative data format for storage of image masks which relies on the sparsity of the images.
The location and weight of each non-zero pixel is stored for each mask.

Parameters
----------
image_mask_: numpy.ndarray
image_masks: numpy.ndarray
Dense representation of the ROIs with shape (number_of_rows, number_of_columns, number_of_rois).
_roi_ids: list
List of roi ids with length number_of_rois.

Returns
-------
Expand All @@ -265,11 +264,11 @@ def _pixel_mask_extractor(image_mask_, _roi_ids) -> list:
the pixel.
"""
pixel_mask_list = []
for i, roiid in enumerate(_roi_ids):
image_mask = np.array(image_mask_[:, :, i])
_locs = np.where(image_mask > 0)
_pix_values = image_mask[image_mask > 0]
pixel_mask_list.append(np.vstack((_locs[0], _locs[1], _pix_values)).T)
for i in range(image_masks.shape[2]):
image_mask = image_masks[:, :, i]
locs = np.where(image_mask > 0)
pix_values = image_mask[image_mask > 0]
pixel_mask_list.append(np.vstack((locs[0], locs[1], pix_values)).T)
return pixel_mask_list


Expand Down Expand Up @@ -684,3 +683,31 @@ def get_package(
f"\nThe required package'{package_name}' is not installed!\n"
f"To install this package, please run\n\n\t{installation_instructions}\n"
)


def get_default_roi_locations_from_image_masks(image_masks: np.ndarray) -> np.ndarray:
"""Calculate the default ROI locations from given image masks.

This function takes a 3D numpy array of image masks and computes the median
coordinates of the maximum values in each 2D mask. The result is a 2D numpy
array where each column represents the (x, y) coordinates of the ROI for
each mask.

Parameters
----------
image_masks : np.ndarray
A 3D numpy array of shape (height, width, num_rois) containing the image masks.

Returns
-------
np.ndarray
A 2D numpy array of shape (2, num_rois) where each column contains the
(x, y) coordinates of the ROI for each mask.
"""
num_rois = image_masks.shape[2]
roi_locations = np.zeros([2, num_rois], dtype="int")
for i in range(num_rois):
image_mask = image_masks[:, :, i]
max_value_indices = np.where(image_mask == np.amax(image_mask))
roi_locations[:, i] = np.array([np.median(max_value_indices[0]), np.median(max_value_indices[1])]).T
return roi_locations
Loading
Loading