Skip to content

Instantly share code, notes, and snippets.

@adamltyson
Created January 16, 2023 18:49
Show Gist options
  • Save adamltyson/026db057ecc00542e49a06cbc781b9f3 to your computer and use it in GitHub Desktop.
Save adamltyson/026db057ecc00542e49a06cbc781b9f3 to your computer and use it in GitHub Desktop.
Extract timecourse of one channel from a labels layer in napari
# import packages
import numpy as np
import pandas as pd
from skimage.measure import label
# assign arrays to simpler variables
labels = label(viewer.layers["Labels"].data[0])
red_data = viewer.layers["NAME_OF_RED_CHANNEL"].data
# intialise empty list
timecourses = []
# for each ROI drawn
for roi_index in range(labels.max()):
# add 1 due to 0 based indexing
mask_2d = labels == roi_index + 1
# create a 3D mask by repeating the 2D ROI
mask_3d = np.broadcast_to(mask_2d, red_data.shape)
# calculate mean of red channel in ROI over time
timecourse = (red_data * mask_3d).mean(axis=(1, 2))
# add single timecourse to list
timecourses.append(np.squeeze(timecourse))
# convert to dataframe
df = pd.DataFrame(timecourses)
df.to_csv("/path/to/save/file.csv")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment