Skip to content

Commit

Permalink
Merge pull request #24 from catalystneuro/fix_custom_segmentation_int…
Browse files Browse the repository at this point in the history
…erface

Fix Widefield segmentation file names change
  • Loading branch information
CodyCBakerPhD authored Dec 3, 2023
2 parents f87bda4 + 39f9084 commit 8035c63
Show file tree
Hide file tree
Showing 6 changed files with 213 additions and 61 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from typing import Tuple

import numpy as np
from neuroconv.utils import FolderPathType
from neuroconv.utils import FolderPathType, FilePathType
from pymatreader import read_mat
from roiextractors import SegmentationExtractor

Expand All @@ -15,7 +15,10 @@ class WidefieldProcessedSegmentationExtractor(SegmentationExtractor):

def __init__(
self,
folder_path: FolderPathType,
info_mat_file_path: FilePathType,
roi_from_ref_mat_file_path: FilePathType,
vasculature_mask_file_path: FilePathType,
blue_pca_mask_file_path: FilePathType,
):
"""
The SegmentationExtractor for the downsampled (binned) Widefield imaging data.
Expand All @@ -25,7 +28,6 @@ def __init__(
- ROIfromRef.mat : contains the Allen area label of each pixel mapped onto the reference image of the mouse and registered to the session.
- vasculature_mask_2.mat : contains the vasculature mask on the downsampled (binned) session image.
- blue_pca_vasculature_mask_2.mat : contains the PCA mask for the blue channel.
- violet_pca_vasculature_mask_2.mat.mat : contains the PCA mask for the violet channel.
that contain the following variables:
- The Allen area label for each binned pixel
Expand All @@ -34,33 +36,28 @@ def __init__(
Parameters
----------
folder_path: FolderPathType
The path that points to the folder that contains the .mat files.
info_mat_file_path : FilePathType
The file path to the 'info.mat' file.
roi_from_ref_mat_file_path : FilePathType
The file that contains the Allen area label of each pixel mapped onto the reference image of the mouse and registered to the session.
vasculature_mask_file_path: FilePathType
The file that contains the vasculature mask on the downsampled (binned) session image.
blue_pca_mask_file_path: FilePathType
The file that contains the PCA mask for the blue channel.
"""
super().__init__()

self.folder_path = Path(folder_path)

expected_files = [
"info.mat",
"ROIfromRef.mat",
"vasculature_mask_2.mat",
"blue_pca_vasculature_mask_2.mat",
]
mat_file_paths = list(self.folder_path.glob("*.mat"))
assert mat_file_paths, f"The .mat files are missing from {folder_path}."
# assert all expected files are in the folder_path
for expected_file in expected_files:
assert (
self.folder_path / expected_file
).exists(), f"The file {expected_file} is missing from {folder_path}."

info_mat = read_mat(self.folder_path / "info.mat")
info_mat_file_path = Path(info_mat_file_path)
assert info_mat_file_path.exists(), f"The file '{info_mat_file_path}' does not exist."

info_mat = read_mat(str(info_mat_file_path))
assert "info" in info_mat, f"Could not find 'info' struct in 'info.mat'."
self._num_frames = info_mat["info"]["numFrames"]
self._sampling_frequency = info_mat["info"]["frameRate"]

roi_mat = read_mat(self.folder_path / "ROIfromRef.mat")
roi_from_ref_mat_file_path = Path(roi_from_ref_mat_file_path)
assert roi_from_ref_mat_file_path.exists(), f"The file '{roi_from_ref_mat_file_path}' does not exist."
roi_mat = read_mat(str(roi_from_ref_mat_file_path))
assert "ROIcentroids" in roi_mat, f"Could not find 'ROIcentroids' in 'ROIfromRef.mat'."
self._roi_locations = roi_mat[
"ROIcentroids"
Expand All @@ -78,12 +75,16 @@ def __init__(
self._dtype = self._image_masks.dtype

# Contrast based vasculature mask
vasculature_mask = read_mat(self.folder_path / "vasculature_mask_2.mat")
assert "mask_binned" in vasculature_mask, f"Could not find 'mask_binned' in 'vasculature_mask_2.mat'."
vasculature_mask_file_path = Path(vasculature_mask_file_path)
assert vasculature_mask_file_path.exists(), f"The file '{vasculature_mask_file_path}' does not exist."
vasculature_mask = read_mat(str(vasculature_mask_file_path))
assert "mask_binned" in vasculature_mask, f"Could not find 'mask_binned' in '{vasculature_mask_file_path}'."
self._image_vasculature = vasculature_mask["mask_binned"]

# PCA mask (separate for blue and violet)
pca_mask_blue = read_mat(self.folder_path / f"blue_pca_vasculature_mask_2.mat")
blue_pca_mask_file_path = Path(blue_pca_mask_file_path)
assert blue_pca_mask_file_path.exists(), f"The file '{blue_pca_mask_file_path}' does not exist."
pca_mask_blue = read_mat(str(blue_pca_mask_file_path))
assert "mask" in pca_mask_blue, f"Could not find 'mask' in 'blue_pca_vasculature_mask_2.mat'."
self._image_pca_blue = pca_mask_blue["mask"]

Expand All @@ -92,7 +93,8 @@ def _compute_image_masks(self, pixel_mask):
num_rois = self.get_num_rois()
image_mask = np.zeros(shape=(*self._image_size, num_rois), dtype=np.uint8)
for roi_ind, pixel_mask_roi in enumerate(pixel_mask):
pixel_mask_roi = pixel_mask_roi[0]
if isinstance(pixel_mask_roi, list):
pixel_mask_roi = pixel_mask_roi[0]
if len(pixel_mask_roi) == 0:
# there are rois with no pixels
continue
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from neuroconv.datainterfaces.ophys.basesegmentationextractorinterface import BaseSegmentationExtractorInterface
from neuroconv.tools import get_module
from neuroconv.utils import FolderPathType
from neuroconv.utils import FilePathType
from pynwb import NWBFile

from pinto_lab_to_nwb.widefield.extractors.widefield_processed_segmentationextractor import (
Expand All @@ -15,15 +15,34 @@ class WidefieldProcessedSegmentationinterface(BaseSegmentationExtractorInterface

Extractor = WidefieldProcessedSegmentationExtractor

def __init__(self, folder_path: FolderPathType, verbose: bool = True):
def __init__(
self,
info_mat_file_path: FilePathType,
roi_from_ref_mat_file_path: FilePathType,
vasculature_mask_file_path: FilePathType,
blue_pca_mask_file_path: FilePathType,
verbose: bool = True,
):
"""
Parameters
----------
folder_path : FolderPathType
info_mat_file_path : FilePathType
The file path to the 'info.mat' file.
roi_from_ref_mat_file_path : FilePathType
The file that contains the Allen area label of each pixel mapped onto the reference image of the mouse and registered to the session.
vasculature_mask_file_path: FilePathType
The file that contains the vasculature mask on the downsampled (binned) session image.
blue_pca_mask_file_path: FilePathType
The file that contains the PCA mask for the blue channel.
verbose : bool, default: True
"""
super().__init__(folder_path=folder_path)
super().__init__(
info_mat_file_path=info_mat_file_path,
roi_from_ref_mat_file_path=roi_from_ref_mat_file_path,
vasculature_mask_file_path=vasculature_mask_file_path,
blue_pca_mask_file_path=blue_pca_mask_file_path,
)
self.verbose = verbose

def get_metadata(self) -> dict:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import numpy as np
from neuroconv import BaseDataInterface
from neuroconv.tools import get_module
from neuroconv.utils import FolderPathType
from neuroconv.utils import FolderPathType, FilePathType
from pymatreader import read_mat
from pynwb import NWBFile
from pynwb.base import Images
Expand All @@ -13,37 +13,57 @@
class WidefieldSegmentationImagesBlueInterface(BaseDataInterface):
"""The custom interface to add the blue channel manual and vasculature mask to the NWBFile."""

def __init__(self, folder_path: FolderPathType, verbose: bool = True):
def __init__(
self,
vasculature_mask_file_path: FilePathType,
manual_mask_file_path: FilePathType,
manual_mask_struct_name: str,
verbose: bool = True,
):
"""
The interface to add the summary images to the NWBFile.
Parameters
----------
folder_path : FolderPathType
vasculature_mask_file_path : FilePathType
The file path to the vasculature mask file.
manual_mask_file_path : FilePathType
The file path to the manual mask file.
manual_mask_struct_name: str
The name of the struct in the manual mask file that contains the manual mask (e.g. "regMask" or "reg_manual_mask").
verbose : bool, default: True
"""
super().__init__(folder_path=folder_path)
self.folder_path = Path(folder_path)
super().__init__(vasculature_mask_file_path=vasculature_mask_file_path)
self.vasculature_mask_file_path = Path(vasculature_mask_file_path)
assert (
self.vasculature_mask_file_path.exists()
), f"The vasculature mask file '{vasculature_mask_file_path}' does not exist."

self.manual_mask_file_path = Path(manual_mask_file_path)
assert self.manual_mask_file_path.exists(), f"The manual mask file '{manual_mask_file_path}' does not exist."

self.manual_mask_struct_name = manual_mask_struct_name

self.verbose = verbose

self._image_vasculature = self._load_vasculature_mask()
self._image_manual = self._load_manual_mask()

def _load_vasculature_mask(self) -> np.ndarray:
vasculature_mask_file_path = self.folder_path / "vasculature_mask_2.mat"
assert vasculature_mask_file_path.exists(), f"The vasculature mask file is missing from {self.folder_path}."
vasculature_mask_mat = read_mat(str(vasculature_mask_file_path))
assert "mask" in vasculature_mask_mat, f"The vasculature mask is missing from {vasculature_mask_file_path}."
vasculature_mask_mat = read_mat(str(self.vasculature_mask_file_path))
assert (
"mask" in vasculature_mask_mat
), f"The vasculature mask is missing from {self.vasculature_mask_file_path}."
vasculature_mask = vasculature_mask_mat["mask"]

return vasculature_mask

def _load_manual_mask(self):
manual_mask_file_path = Path(self.folder_path) / "regManualMask.mat"
assert manual_mask_file_path.exists(), f"The manual mask file is missing from {self.folder_path}."
manual_mask_mat = read_mat(str(manual_mask_file_path))
assert "regMask" in manual_mask_mat, f"The manual mask is missing from {manual_mask_file_path}."
manual_mask = manual_mask_mat["regMask"]
manual_mask_mat = read_mat(self.manual_mask_file_path)
assert (
self.manual_mask_struct_name in manual_mask_mat
), f"The manual mask is missing from {self.manual_mask_file_path}."
manual_mask = manual_mask_mat[self.manual_mask_struct_name]

return manual_mask

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import numpy as np
from neuroconv import BaseDataInterface
from neuroconv.tools import get_module
from neuroconv.utils import FolderPathType
from neuroconv.utils import FilePathType
from pymatreader import read_mat
from pynwb import NWBFile
from pynwb.base import Images
Expand All @@ -13,28 +13,28 @@
class WidefieldSegmentationImagesVioletInterface(BaseDataInterface):
"""The custom interface to add the violet channel PCA mask to the NWBFile."""

def __init__(self, folder_path: FolderPathType, verbose: bool = True):
def __init__(self, violet_pca_mask_file_path: FilePathType, verbose: bool = True):
"""
The interface to add the summary images to the NWBFile.
Parameters
----------
folder_path : FolderPathType
violet_pca_mask_file_path : FilePathType
The file path to the violet channel PCA mask file.
verbose : bool, default: True
"""
super().__init__(folder_path=folder_path)
self.folder_path = Path(folder_path)
super().__init__(violet_pca_mask_file_path=violet_pca_mask_file_path)
self.violet_pca_mask_file_path = Path(violet_pca_mask_file_path)
assert (
self.violet_pca_mask_file_path.exists()
), f"The violet channel PCA mask file '{violet_pca_mask_file_path}' does not exist."
self.verbose = verbose

self._image_pca_violet = self._load_pca_mask()

def _load_pca_mask(self) -> np.ndarray:
pca_mask_file_path = self.folder_path / "violet_pca_vasculature_mask_2.mat"
assert (
pca_mask_file_path.exists()
), f"The PCA mask file for the violet channel is missing from {self.folder_path}."
pca_mask_violet = read_mat(str(pca_mask_file_path))
assert "vasc_mask" in pca_mask_violet, f"Could not find 'vasc_mask' in 'violet_pca_vasculature_mask_2.mat'."
pca_mask_violet = read_mat(str(self.violet_pca_mask_file_path))
assert "vasc_mask" in pca_mask_violet, f"Could not find 'vasc_mask' in '{self.violet_pca_mask_file_path}'."
pca_mask = pca_mask_violet["vasc_mask"]

return pca_mask
Expand Down
69 changes: 64 additions & 5 deletions src/pinto_lab_to_nwb/widefield/widefield_convert_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@ def session_to_nwb(
strobe_sequence_file_path: FilePathType,
processed_imaging_file_path: FilePathType,
info_file_path: FilePathType,
vasculature_mask_file_path: FilePathType,
manual_mask_file_path: FilePathType,
manual_mask_struct_name: str,
roi_from_ref_mat_file_path: FilePathType,
binned_vasculature_mask_file_path: FilePathType,
binned_blue_pca_mask_file_path: FilePathType,
binned_violet_pca_mask_file_path: FilePathType,
subject_metadata_file_path: Optional[FilePathType] = None,
stub_test: bool = False,
):
Expand All @@ -34,9 +41,23 @@ def session_to_nwb(
widefield_imaging_folder_path: FolderPathType
The folder path that contains the Micro-Manager OME-TIF imaging output (.ome.tif files).
strobe_sequence_file_path: FilePathType
The file path to the strobe sequence file. This file should contain the 'strobe_session_key' key.
The file path to the strobe sequence file. This file should contain the 'strobe_session_key' key.
info_file_path: FilePathType
The file path to the Matlab file with information about the imaging session (e.g. 'frameRate').
vasculature_mask_file_path: FilePathType
The file path that contains the contrast based vasculature mask on the full size session image (blue channel).
manual_mask_file_path: FilePathType
The file path that contains the manual mask on the full size session image (blue channel).
manual_mask_struct_name: str
The name of the struct in the manual mask file that contains the manual mask (e.g. "regMask" or "reg_manual_mask").
roi_from_ref_mat_file_path: FilePathType
The file path that contains the Allen area label of each pixel mapped onto the reference image of the mouse and registered to the session.
binned_vasculature_mask_file_path: FilePathType
The file path that contains the contrast based vasculature mask on the downsampled (binned) session image (blue channel).
binned_blue_pca_mask_file_path: FilePathType
The file path that contains the PCA mask for the blue channel.
binned_violet_pca_mask_file_path: FilePathType
The file path that contains the PCA mask for the violet channel.
subject_metadata_file_path: FilePathType, optional
The file path to the subject metadata file. This file should contain the 'metadata' key.
stub_test: bool, optional
Expand Down Expand Up @@ -106,13 +127,18 @@ def session_to_nwb(
source_data.update(
dict(
SegmentationProcessedBlue=dict(
folder_path=str(widefield_imaging_folder_path),
info_mat_file_path=str(info_file_path),
roi_from_ref_mat_file_path=str(roi_from_ref_mat_file_path),
vasculature_mask_file_path=str(binned_vasculature_mask_file_path),
blue_pca_mask_file_path=str(binned_blue_pca_mask_file_path),
),
SummaryImagesBlue=dict(
folder_path=str(widefield_imaging_folder_path),
vasculature_mask_file_path=str(vasculature_mask_file_path),
manual_mask_file_path=str(manual_mask_file_path),
manual_mask_struct_name=manual_mask_struct_name,
),
SummaryImagesViolet=dict(
folder_path=str(widefield_imaging_folder_path),
violet_pca_mask_file_path=str(binned_violet_pca_mask_file_path),
),
)
)
Expand Down Expand Up @@ -156,13 +182,39 @@ def session_to_nwb(
# Parameters for conversion

# The folder path that contains the raw imaging data in Micro-Manager OME-TIF format (.ome.tif files).
imaging_folder_path = Path("/Users/weian/data/DrChicken_20230419_20hz")
# imaging_folder_path = Path("/Users/weian/data/Cherry/20230802/Cherry_20230802_20hz_1")
imaging_folder_path = Path("/Volumes/t7-ssd/Pinto/DrChicken_20230419_20hz")
# The file path to the strobe sequence file.
strobe_sequence_file_path = imaging_folder_path / "strobe_seq_1_2.mat"
# The file path to the downsampled imaging data in Matlab format (.mat file).
processed_imaging_path = imaging_folder_path / "rawf_full.mat"
# The file path to the Matlab file with information about the imaging session (e.g. 'frameRate').
info_file_path = imaging_folder_path / "info.mat"

# Parameters for custom segmentation and summary images
# The file path that contains the contrast based vasculature mask on the full size session image (blue channel).
vasculature_mask_file_path = imaging_folder_path / "vasculature_mask_2.mat"

# The file path that contains the manual mask on the full size session image (blue channel).
# manual_mask_file_path = imaging_folder_path / "reg_manual_mask_jlt6316_Cherry_20230802_1_1_1.mat"
manual_mask_file_path = imaging_folder_path / "regManualMask.mat"
# The name of the struct in the manual mask file that contains the manual mask (e.g. "regMask" or "reg_manual_mask").
# manual_mask_struct_name = "reg_manual_mask"
manual_mask_struct_name = "regMask"

# The file path that contains the Allen area label of each pixel mapped onto the reference image of the mouse and registered to the session.
# roi_from_ref_mat_file_path = imaging_folder_path / "ROIfromRef_1.mat"
roi_from_ref_mat_file_path = imaging_folder_path / "ROIfromRef.mat"

# The file path that contains the contrast based vasculature mask on the downsampled (binned) session image (blue channel).
binned_vasculature_mask_file_path = imaging_folder_path / "vasculature_mask_2.mat"

# The file path that contains the PCA mask for the blue channel.
binned_blue_pca_mask_file_path = imaging_folder_path / "blue_pca_vasculature_mask_2.mat"

# The file path that contains the PCA mask for the violet channel.
binned_violet_pca_mask_file_path = imaging_folder_path / "violet_pca_vasculature_mask_2.mat"

subject_metadata_file_path = "/Volumes/t7-ssd/Pinto/Behavior/subject_metadata.mat"
# The file path to the NWB file that will be created.
nwbfile_path = Path("/Volumes/t7-ssd/Pinto/nwbfiles/widefield/DrChicken_20230419_20hz.nwb")
Expand All @@ -175,6 +227,13 @@ def session_to_nwb(
strobe_sequence_file_path=strobe_sequence_file_path,
processed_imaging_file_path=processed_imaging_path,
info_file_path=info_file_path,
vasculature_mask_file_path=vasculature_mask_file_path,
manual_mask_file_path=manual_mask_file_path,
manual_mask_struct_name=manual_mask_struct_name,
roi_from_ref_mat_file_path=roi_from_ref_mat_file_path,
binned_vasculature_mask_file_path=binned_vasculature_mask_file_path,
binned_blue_pca_mask_file_path=binned_blue_pca_mask_file_path,
binned_violet_pca_mask_file_path=binned_violet_pca_mask_file_path,
subject_metadata_file_path=subject_metadata_file_path,
stub_test=stub_test,
)
Loading

0 comments on commit 8035c63

Please sign in to comment.