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

add inscopix extractor for cell_set #275

Draft
wants to merge 25 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
b2d2601
add inscopix extractor for cell_set
bendichter Jan 23, 2024
4877a1a
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jan 23, 2024
9c75a34
add tests
bendichter Jan 23, 2024
0d6006c
Merge remote-tracking branch 'origin/inscopix' into inscopix
bendichter Jan 23, 2024
442bf14
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jan 23, 2024
7b2f202
Update tests/test_inscopix_segmentation.py
bendichter Jan 23, 2024
4478d02
add isx to requirements-full.txt
bendichter Jan 23, 2024
86068a2
Merge remote-tracking branch 'origin/inscopix' into inscopix
bendichter Jan 23, 2024
d95f283
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jan 23, 2024
b98dc9f
Update requirements-full.txt
bendichter Jan 23, 2024
d07037d
give in to strong typing
bendichter Jan 23, 2024
a2196de
Merge remote-tracking branch 'origin/inscopix' into inscopix
bendichter Jan 23, 2024
3931852
Update run-tests.yml
bendichter Jan 23, 2024
8932556
fix data paths
bendichter Jan 23, 2024
44a49ca
Merge remote-tracking branch 'origin/inscopix' into inscopix
bendichter Jan 23, 2024
7915709
rmv py3.8
bendichter Jan 23, 2024
0d29f5e
add docstring to inscopix segmentation extrator module
bendichter Jan 23, 2024
1790769
add docstring to inscopix imaging extrator module
bendichter Jan 23, 2024
e21fe8f
Merge branch 'main' into inscopix
CodyCBakerPhD Mar 4, 2024
6b35be8
Merge branch 'main' into inscopix
bendichter May 13, 2024
5f49388
fix precommit tests
bendichter May 13, 2024
efa7ee7
Update .readthedocs.yml
bendichter May 13, 2024
5cfc38b
Update .readthedocs.yml
bendichter May 13, 2024
894da1a
Update CHANGELOG.md
bendichter May 13, 2024
44075b7
Update CHANGELOG.md
bendichter May 13, 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
### Features

* Updated testing workflows to include python 3.12, m1/intel macos, and dev tests to check neuroconv: [PR #317](https://github.com/catalystneuro/roiextractors/pull/317)
* Added `InscopixSegmentationExtractor` [PR #275](https://github.com/catalystneuro/roiextractors/pull/275)

### Fixes

Expand Down
1 change: 1 addition & 0 deletions requirements-full.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ tifffile>=2018.10.18
scanimage-tiff-reader==1.4.1.4
neuroconv[video]>=0.4.6 # Uses the VideoCaptureContext class
natsort>=8.3.1
isx>=1.0.3
2 changes: 2 additions & 0 deletions src/roiextractors/extractorlist.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
BrukerTiffSinglePlaneImagingExtractor,
MicroManagerTiffImagingExtractor,
)
from .extractors.inscopixextractors import InscopixSegmentationExtractor
from .extractors.sbximagingextractor import SbxImagingExtractor
from .extractors.memmapextractors import NumpyMemmapImagingExtractor
from .extractors.memmapextractors import MemmapImagingExtractor
Expand Down Expand Up @@ -60,6 +61,7 @@
ExtractSegmentationExtractor,
SimaSegmentationExtractor,
CaimanSegmentationExtractor,
InscopixSegmentationExtractor,
]

imaging_extractor_dict = {imaging_class.extractor_name: imaging_class for imaging_class in imaging_extractor_full_list}
Expand Down
3 changes: 3 additions & 0 deletions src/roiextractors/extractors/inscopixextractors/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
"""Extractors for Inscopix data."""

from .inscopixsegmentationextractor import InscopixSegmentationExtractor
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
"""Inscopix Segmentation Extractor."""

import numpy as np

from ...extraction_tools import PathType, ArrayType
from ...segmentationextractor import SegmentationExtractor


class InscopixSegmentationExtractor(SegmentationExtractor):
"""A segmentation extractor for Inscopix."""

extractor_name = "InscopixSegmentationExtractor"
installed = True # check at class level if installed or not
is_writable = False
mode = "file"
installation_mesg = "" # error message when not installed

def __init__(self, file_path: PathType):
"""Initialize a InscopixSegmentationExtractor instance.

Parameters
----------
file_path: str
The location of the folder containing Inscopix *.mat output file.
"""
import isx

SegmentationExtractor.__init__(self)
self.file_path = file_path
self.cell_set = isx.CellSet.read(str(file_path))

def get_num_rois(self):
return self.cell_set.num_cells

def get_roi_image_masks(self, roi_ids=None) -> np.ndarray:
if roi_ids is None:
roi_idx_ = range(self.get_num_rois())
else:
all_ids = self.get_roi_ids()
roi_idx_ = [all_ids.index(i) for i in roi_ids]
return np.hstack([self.cell_set.get_cell_image_data(roi_id) for roi_id in roi_idx_])

def get_roi_ids(self) -> list:
return [self.cell_set.get_cell_name(x) for x in range(self.get_num_rois())]

def get_image_size(self) -> ArrayType:
num_pixels = self.cell_set.footer["spacingInfo"]["numPixels"]
return num_pixels["x"], num_pixels["y"]

def get_accepted_list(self) -> list:
return [id for x, id in enumerate(self.get_roi_ids()) if self.cell_set.get_cell_status(x) == "accepted"]

def get_rejected_list(self) -> list:
return [id for x, id in enumerate(self.get_roi_ids()) if self.cell_set.get_cell_status(x) == "rejected"]

def get_traces(self, roi_ids=None, start_frame=None, end_frame=None, name="raw") -> ArrayType:
if roi_ids is None:
roi_idx_ = range(self.get_num_rois())
else:
all_ids = self.get_roi_ids()
roi_idx_ = [all_ids.index(i) for i in roi_ids]
return np.vstack([self.cell_set.get_cell_trace_data(roi_id)[start_frame:end_frame] for roi_id in roi_idx_])

def get_num_frames(self) -> int:
return self.cell_set.footer["timingInfo"]["numTimes"]

def get_sampling_frequency(self) -> float:
return 1 / self.cell_set.timing.period.secs_float
55 changes: 55 additions & 0 deletions tests/test_inscopix_segmentation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import numpy as np
from numpy.testing import assert_array_equal

from roiextractors import InscopixSegmentationExtractor

from .setup_paths import OPHYS_DATA_PATH


def test_inscopix_segmentation_extractor():
file_path = OPHYS_DATA_PATH / "segmentation_datasets" / "inscopix" / "cellset.isxd"
extractor = InscopixSegmentationExtractor(file_path=file_path)

assert extractor.get_num_rois() == 4
assert extractor.get_roi_ids() == ["C0", "C1", "C2", "C3"]
assert extractor.get_accepted_list() == ["C0", "C1", "C2"]
assert extractor.get_rejected_list() == ["C3"]
assert extractor.get_image_size() == (398, 366)
assert extractor.get_num_frames() == 5444
img = extractor.get_roi_image_masks(["C1"])
assert img.shape == (366, 398)
np.testing.assert_allclose(extractor.get_sampling_frequency(), 9.998700168978033)
assert extractor.get_traces().shape == (4, 5444)
assert extractor.get_traces(start_frame=10, end_frame=20).shape == (4, 10)
assert extractor.get_traces(start_frame=10, end_frame=20, roi_ids=["C1"]).shape == (1, 10)


def test_inscopix_segmentation_extractor_part1():
file_path = OPHYS_DATA_PATH / "segmentation_datasets" / "inscopix" / "cellset_series_part1.isxd"
extractor = InscopixSegmentationExtractor(file_path=file_path)

assert extractor.get_num_rois() == 6
assert extractor.get_roi_ids() == ["C0", "C1", "C2", "C3", "C4", "C5"]
assert extractor.get_accepted_list() == []
assert extractor.get_rejected_list() == []
assert extractor.get_image_size() == (21, 21)
img = extractor.get_roi_image_masks(["C1"])
assert img.shape == (21, 21)
assert extractor.get_sampling_frequency() == 10.0
assert extractor.get_traces().shape == (6, 100)
assert extractor.get_traces(start_frame=10, end_frame=20).shape == (6, 10)
assert extractor.get_traces(start_frame=10, end_frame=20, roi_ids=["C1"]).shape == (1, 10)
assert extractor.get_num_frames() == 100


def test_inscopix_segmentation_extractor_empty():
file_path = OPHYS_DATA_PATH / "segmentation_datasets" / "inscopix" / "empty_cellset.isxd"
extractor = InscopixSegmentationExtractor(file_path=file_path)

assert extractor.get_num_rois() == 0
assert extractor.get_roi_ids() == []
assert extractor.get_accepted_list() == []
assert extractor.get_rejected_list() == []
assert extractor.get_image_size() == (5, 4)
assert extractor.get_sampling_frequency() == 40.0
assert extractor.get_num_frames() == 7
Loading