Skip to content

Instantly share code, notes, and snippets.

@afrendeiro
Last active June 17, 2022 10:25
Show Gist options
  • Save afrendeiro/b779e84365c1b8a080d9de36c6b0a7cc to your computer and use it in GitHub Desktop.
Save afrendeiro/b779e84365c1b8a080d9de36c6b0a7cc to your computer and use it in GitHub Desktop.
from pathlib import Path
import tifffile
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.backends.backend_pdf import PdfPages
from stardist.models import StarDist2D
from csbdeep.utils import normalize
_dir = Path("HumanLiverH35")
files = list(
filter(lambda x: not x.name.startswith("."), sorted(_dir.glob("*.ome.tif")))
)
_dapi = list()
_stack = list()
channels = list()
for f in tqdm(files):
name = f.stem.split("ROI-01_A-")[-1].replace(".ome", "")
if name.startswith("DAPI"):
_dapi.append(tifffile.imread(f))
else:
_stack.append(tifffile.imread(f))
channels.append(name)
dapi = np.asarray(_dapi).mean(0)
del _dapi
# stack = np.asarray(_stack)
model = StarDist2D.from_pretrained("2D_versatile_fluo")
mask = model.predict_instances(normalize(dapi))[0]
fig, ax = plt.subplots()
ax.imshow(np.log1p(dapi))
ax.contour(mask, cmap="Reds", linewidths=0.25)
ax.axis("off")
ax.set(title="DAPI")
fig.savefig("mask.pdf")
plt.close(fig)
with PdfPages("stack.pdf") as pdf:
fig, ax = plt.subplots()
ax.imshow(np.log1p(dapi))
ax.axis("off")
ax.set(title="DAPI")
pdf.savefig(fig)
plt.close(fig)
for channel, image in tqdm(zip(channels, _stack), total=len(channels)):
fig, ax = plt.subplots()
ax.imshow(np.log1p(image))
ax.axis("off")
ax.set(title=channel)
pdf.savefig(fig)
plt.close(fig)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment