Skip to content

Commit

Permalink
Merge pull request #78 from sebi06/work_in_progress
Browse files Browse the repository at this point in the history
added marimo notebooks
  • Loading branch information
sebi06 authored Jan 19, 2025
2 parents 71cea12 + 7d0ea80 commit c1a05ef
Show file tree
Hide file tree
Showing 9 changed files with 766 additions and 246 deletions.
Binary file added data/S3_1Pos_2Mosaic_T2Z3_CH2_sm.czi
Binary file not shown.
223 changes: 223 additions & 0 deletions demo/notebooks/__marimo__/read_czi_pixeldata_marimo.ipynb

Large diffs are not rendered by default.

535 changes: 293 additions & 242 deletions demo/notebooks/read_czi_metadata.ipynb

Large diffs are not rendered by default.

78 changes: 78 additions & 0 deletions demo/notebooks/read_czi_metadata_marimo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import marimo

__generated_with = "0.10.8"
app = marimo.App()


@app.cell
def _():
import marimo as mo
import pandas as pd
from czitools.metadata_tools import czi_metadata as czimd
from czitools.utils import misc
from pathlib import Path
import os
import glob
return Path, czimd, glob, misc, mo, os, pd


@app.cell
def _(mo):
file_browser = mo.ui.file_browser(multiple=False)

# Display the file browser
mo.vstack([file_browser])
return (file_browser,)


@app.cell
def _(file_browser):
filepath = str(file_browser.path(0))
print(f"Filepath: {filepath}")
return (filepath,)


@app.cell
def _(czimd, filepath):
# get only specific metadata
czi_dimensions = czimd.CziDimensions(filepath)
print(f"SizeS: {czi_dimensions.SizeS}")
print(f"SizeT: {czi_dimensions.SizeT}")
print(f"SizeZ: {czi_dimensions.SizeZ}")
print(f"SizeC: {czi_dimensions.SizeC}")
print(f"SizeY: {czi_dimensions.SizeY}")
print(f"SizeX: {czi_dimensions.SizeX}")
return (czi_dimensions,)


@app.cell
def _(czimd, filepath, misc, mo):
# get the complete metadata at once as one big class
mdata = czimd.CziMetadata(filepath)

# get the CZI metadata dictionary directly from filename
mdict = czimd.create_md_dict_red(mdata, sort=False, remove_none=True)

# convert metadata dictionary to a pandas dataframe
mdframe = misc.md2dataframe(mdict)

mo.vstack([mo.ui.table(mdframe)])
return mdata, mdframe, mdict


@app.cell
def _(filepath, misc, mo):
# get the planetable for the CZI file
pt = misc.get_planetable(filepath,
norm_time=True,
pt_complete=True,
t=0,
c=0,
z=0)

mo.vstack([mo.ui.table(pt)])
return (pt,)


if __name__ == "__main__":
app.run()
168 changes: 168 additions & 0 deletions demo/notebooks/read_czi_pixeldata_marimo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
import marimo

__generated_with = "0.10.14"
app = marimo.App(auto_download=["ipynb"])


@app.cell
def _():
import marimo as mo
import pandas as pd
from czitools.metadata_tools import czi_metadata as czimd
from czitools.utils import misc
from czitools.read_tools import read_tools as czird
import dask.array as da
from pathlib import Path
import os
import glob
from matplotlib import pyplot as plt
import matplotlib.cm as cm
from matplotlib.colors import LinearSegmentedColormap
return (
LinearSegmentedColormap,
Path,
cm,
czimd,
czird,
da,
glob,
misc,
mo,
os,
pd,
plt,
)


@app.cell
def _(mo):
file_browser = mo.ui.file_browser(multiple=False)

# Display the file browser
mo.vstack([file_browser])
return (file_browser,)


@app.cell
def _(file_browser):
filepath = str(file_browser.path(0))
print(f"Filepath: {filepath}")
return (filepath,)


@app.cell
def _(LinearSegmentedColormap, czird, da, filepath):
def hex_to_rgb(hex_color: str) -> tuple[int, int, int]:
"""
Convert a hexadecimal color string to an RGB tuple.
Args:
hex_color (str): A string representing a color in hexadecimal format (e.g., "#RRGGBB").
Returns:
tuple[int, int, int]: A tuple containing the RGB values as floats in the range [0, 1].
"""

hex_color = hex_color.lstrip("#")
rgb = tuple(int(hex_color[i : i + 2], 16) / 255.0 for i in (0, 2, 4))

return rgb

# get 6d array with dimension order STCZYX(A)
array6d, mdata = czird.read_6darray(filepath, use_dask=False, chunk_zyx=True)

# show dask array structure
if isinstance(array6d, da.Array):
print(array6d)
else:
print("Shape:", array6d.shape, "dtype:", array6d.dtype)

# get array dimensions
dims = array6d.shape[:-2]
dims_names = ["S", "T", "C", "Z"]

cmaps = []

for ch in range(mdata.image.SizeC):
chname = mdata.channelinfo.names[ch]
rgb = hex_to_rgb(mdata.channelinfo.colors[ch][3:])
cmaps.append(LinearSegmentedColormap.from_list(chname, [(0, 0, 0), rgb]))
return (
array6d,
ch,
chname,
cmaps,
dims,
dims_names,
hex_to_rgb,
mdata,
rgb,
)


@app.cell
def _(czimd, mdata, misc, mo):
# get the CZI metadata dictionary directly from filename
mdict = czimd.create_md_dict_red(mdata, sort=False, remove_none=True)

# convert metadata dictionary to a pandas dataframe
mdframe = misc.md2dataframe(mdict)

mo.vstack([mo.ui.table(mdframe)])
return mdframe, mdict


@app.cell
def _(dims, mo):
scene = mo.ui.slider(
start=0,
stop=dims[0] - 1,
step=1,
label=f"scene [0 - {dims[0]-1}]",
show_value=True,
)

time = mo.ui.slider(
start=0,
stop=dims[1] - 1,
step=1,
label=f"time [0 - {dims[1]-1}]",
show_value=True,
)

channel = mo.ui.slider(
start=0,
stop=dims[2] - 1,
step=1,
label=f"channel [0 - {dims[2]-1}]",
show_value=True,
)

zplane = mo.ui.slider(
start=0,
stop=dims[3] - 1,
step=1,
label=f"zplane [0 - {dims[3]-1}]",
show_value=True,
)

mo.vstack([scene, time, channel, zplane])
return channel, scene, time, zplane


@app.cell
def _(channel, scene, show_2dplane, time, zplane):
show_2dplane(scene.value, time.value, channel.value, zplane.value)
return


@app.cell
def _(array6d, cmaps, plt):
def show_2dplane(s, t, c, z):
plt.figure(figsize=(8, 8))
plt.imshow(array6d[s, t, c, z, ...], cmap=cmaps[c], vmin=None, vmax=None)
plt.tight_layout()
return plt.gca()
return (show_2dplane,)


if __name__ == "__main__":
app.run()
2 changes: 1 addition & 1 deletion demo/notebooks/read_czi_pixeldata_simple.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.8"
"version": "3.11.7"
}
},
"nbformat": 4,
Expand Down
2 changes: 1 addition & 1 deletion demo/testing/process.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# -*- coding: utf-8 -*-
pip# -*- coding: utf-8 -*-

#################################################################
# File : process.py
Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[metadata]
name = czitools
version = 0.7.5
version = 0.7.6
author = Sebastian Rhode
author_email = [email protected]
url = https://github.com/sebi06/czitools
Expand Down
2 changes: 1 addition & 1 deletion src/czitools/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# __init__.py
# version of the czitools package
__version__ = "0.7.5"
__version__ = "0.7.6"

0 comments on commit c1a05ef

Please sign in to comment.