Skip to content

Commit

Permalink
Add draft loaders for suite2p processed data using pynapple 🍍 and NWB
Browse files Browse the repository at this point in the history
  • Loading branch information
lauraporta committed Jul 26, 2024
1 parent 2d0f8ec commit 200f1c5
Showing 1 changed file with 48 additions and 0 deletions.
48 changes: 48 additions & 0 deletions derotation/postprocessing/load_processed_data.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
from pathlib import Path

import numpy as np
import pynapple as nap
from skimage.measure import find_contours

from derotation.postprocessing.neuropil_subtraction import neuropil_subtraction


def load_suite2p_data(path: Path) -> nap.NWBFile:
suite2p_data = nap.load_file(path)
return suite2p_data


def get_dff(suite2p_data: nap.NWBFile) -> tuple:
raw_fluorescence = suite2p_data["RoiResponseSeries"]

neuropil = suite2p_data["Neuropil"]

dff, r = neuropil_subtraction(
raw_fluorescence[:].values.T, neuropil[:].values.T
)

dff = nap.Tsd(t=raw_fluorescence.t, d=dff.T)
timebase = raw_fluorescence.t

return dff, timebase


def get_plane_segmentation(suite2p_data: nap.NWBFile) -> tuple:
plane_seg = (
suite2p_data.nwb.processing["ophys"]
.data_interfaces["ImageSegmentation"]
.plane_segmentations["PlaneSegmentation"]
)

ROI_centroids = plane_seg.ROICentroids[:]
is_cell = plane_seg.Accepted[:]
labels = plane_seg.image_mask[:]

contours = [find_contours(c)[0] for c in labels[is_cell.astype(bool)]]

return ROI_centroids, is_cell, labels, contours


def load_registered_binary(path: Path, shape: tuple) -> np.memmap:
registered = np.memmap(path, shape=shape, dtype="int16")
return registered

0 comments on commit 200f1c5

Please sign in to comment.