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 custom segmentation #8

Merged
merged 5 commits into from
Jul 1, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import numpy as np
from pymatreader import read_mat
from roiextractors import SegmentationExtractor


class Vu2024SegmentationExtractor(SegmentationExtractor):
extractor_name = "Vu2024Segmentation"

def __init__(self, file_path: str, sampling_frequency: float, accepted_list: list[int] = None):
"""
Create a Vu2024SegmentationExtractor instance from a .mat file that contains the ROI locations and image masks.
"""
super().__init__()
self.file_path = file_path

data = read_mat(file_path)

self._image_masks = data["ROImasks"] # e.g. shape of (378, 381, 103) height, width, num_rois
self._num_rows, self._num_columns, self._num_rois = self._image_masks.shape
self._image_size = (self._num_rows, self._num_columns)
self._num_frames = data["F"].shape[0]

self._roi_locations = data["ROIs"].transpose() # e.g. shape of (103, 2) num_rois, (x, y)

if accepted_list is None:
accepted_list = list(range(self._num_rois))
self._accepted_list = accepted_list
self._rejected_list = [i for i in range(self._num_rois) if i not in self._accepted_list]

self._sampling_frequency = sampling_frequency
self._times = None

def get_num_frames(self) -> int:
return self._num_frames

def get_accepted_list(self) -> list[int]:
return self._accepted_list

def get_rejected_list(self) -> list[int]:
return self._rejected_list

def get_image_size(self) -> tuple[int, int]:
return self._image_size

def get_roi_ids(self) -> list:
return list(range(self.get_num_rois()))

def get_num_rois(self) -> int:
return self._num_rois

def get_roi_locations(self, roi_ids=None) -> np.ndarray:
if roi_ids is None:
return self._roi_locations
else:
return self._roi_locations[roi_ids]

def get_roi_image_masks(self, roi_ids=None) -> np.ndarray:
if roi_ids is None:
return self._image_masks
else:
return self._image_masks[:, :, roi_ids]
1 change: 1 addition & 0 deletions src/howe_lab_to_nwb/vu2024/interfaces/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
from .vu2024_fiberphotometryinterface import Vu2024FiberPhotometryInterface
from .cxdimaginginterface import CxdImagingInterface
from .vu2024_segmentationinterface import Vu2024SegmentationInterface
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
from neuroconv.datainterfaces.ophys.basesegmentationextractorinterface import BaseSegmentationExtractorInterface
from neuroconv.utils import FilePathType

from howe_lab_to_nwb.vu2024.extractors.vu2024_segmentationextractor import Vu2024SegmentationExtractor


class Vu2024SegmentationInterface(BaseSegmentationExtractorInterface):
"""The interface for reading the ROI masks and locations from custom .mat files from the Howe Lab."""

display_name = "Vu2024 Segmentation"
associated_suffixes = (".mat",)
info = "Interface for Vu2024 segmentation data."

Extractor = Vu2024SegmentationExtractor

def __init__(
self, file_path: FilePathType, sampling_frequency: float, accepted_list: list = None, verbose: bool = True
):
"""
DataInterface for reading ROI masks and locations from custom .mat files from the Howe Lab.

Parameters
----------
file_path : FilePathType
Path to the .mat file that contains the ROI masks and locations.
sampling_frequency : float
The sampling frequency of the data.
accepted_list : list, optional
A list of the accepted ROIs.
verbose : bool, default: True
controls verbosity.
"""
super().__init__(file_path=file_path, sampling_frequency=sampling_frequency, accepted_list=accepted_list)
self.verbose = verbose
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,16 @@ def single_wavelength_session_to_nwb(
dict(FiberPhotometry=dict(stub_test=stub_test, fiber_locations_metadata=fiber_locations_metadata))
)

# Add ROI segmentation
accepted_list = [fiber_ind for fiber_ind, fiber in enumerate(fiber_locations_metadata) if fiber["location"] != ""]
roi_source_data = dict(
file_path=str(raw_fiber_photometry_file_path),
sampling_frequency=sampling_frequency,
accepted_list=accepted_list,
)
source_data.update(dict(Segmentation=roi_source_data))
conversion_options.update(dict(Segmentation=dict(stub_test=stub_test)))

converter = Vu2024NWBConverter(source_data=source_data)

# Add datetime to conversion
Expand Down Expand Up @@ -135,7 +145,7 @@ def single_wavelength_session_to_nwb(
excitation_wavelength_in_nm = 470
indicator = "dLight1.3b"

nwbfile_path = Path("/Volumes/t7-ssd/Howe/nwbfiles/GridDL-18_211110.nwb")
nwbfile_path = Path("/Volumes/t7-ssd/Howe/nwbfiles/ROIs_GridDL-18_211110.nwb")
stub_test = True

single_wavelength_session_to_nwb(
Expand Down
7 changes: 6 additions & 1 deletion src/howe_lab_to_nwb/vu2024/vu2024nwbconverter.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@
from neuroconv.datainterfaces import TiffImagingInterface
from neuroconv.utils import DeepDict

from howe_lab_to_nwb.vu2024.interfaces import Vu2024FiberPhotometryInterface, CxdImagingInterface
from howe_lab_to_nwb.vu2024.interfaces import (
Vu2024FiberPhotometryInterface,
CxdImagingInterface,
Vu2024SegmentationInterface,
)


class Vu2024NWBConverter(NWBConverter):
Expand All @@ -14,6 +18,7 @@ class Vu2024NWBConverter(NWBConverter):
Imaging=CxdImagingInterface,
ProcessedImaging=TiffImagingInterface,
FiberPhotometry=Vu2024FiberPhotometryInterface,
Segmentation=Vu2024SegmentationInterface,
)

def get_metadata_schema(self) -> dict:
Expand Down
Loading