Created
September 21, 2022 18:05
-
-
Save adamltyson/1eee55738e87a68601ffeb477684a799 to your computer and use it in GitHub Desktop.
Representing BrainGlobe datasets with xarray
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import dask.array as da | |
| import xarray as xr | |
| import numpy as np | |
| from imlib.IO.cells import cells_xml_to_df | |
| from skimage.io import imread | |
| from dask import delayed | |
| from glob import glob | |
| from pathlib import Path | |
| def lazy_load_dir(path): | |
| path = Path(path) | |
| filenames = sorted(path.glob("*.tif")) | |
| sample = imread(filenames[0]) | |
| lazy_imread = delayed(imread) | |
| lazy_arrays = [lazy_imread(fn) for fn in filenames] | |
| dask_arrays = [ | |
| da.from_delayed(delayed_reader, shape=sample.shape, dtype=sample.dtype) | |
| for delayed_reader in lazy_arrays | |
| ] | |
| stack = da.stack(dask_arrays, axis=0) | |
| return stack | |
| ch2 = lazy_load_dir("/Users/adamtyson/data/MS_cx_left/stitchedImages_100/2") | |
| ch3 = lazy_load_dir("/Users/adamtyson/data/MS_cx_left/stitchedImages_100/3") | |
| ch4 = lazy_load_dir("/Users/adamtyson/data/MS_cx_left/stitchedImages_100/4") | |
| cells = cells_xml_to_df("/Users/adamtyson/analysis/MS_CX_left_cellfinder/points/cell_classification.xml") | |
| brain = da.stack([ch2, ch3, ch4], axis=0) | |
| voxel_spacing = [5, 2.31, 2.31] | |
| coords = { | |
| "channel": ["blue", "green", "red"], | |
| "posterior-anterior": voxel_spacing[0] * np.arange(brain.shape[1]), | |
| "right-left": voxel_spacing[1] * np.arange(brain.shape[2]), | |
| "superior-inferior": voxel_spacing[2] * np.arange(brain.shape[3]), | |
| } | |
| brain_xr = xr.DataArray(brain, | |
| dims=("channel", "posterior-anterior", "right-left","superior-inferior"), | |
| coords=coords, | |
| name="MS_CX_left") | |
| cell_xr = xr.Dataset.from_dataframe(cells).set_coords(["x", "y", "z"]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment