Skip to content

Commit

Permalink
Merge pull request #706 from NEUBIAS/batch_processing
Browse files Browse the repository at this point in the history
Updates for batch processing
  • Loading branch information
tischi authored Jul 25, 2024
2 parents 4de6089 + 6a6785d commit 3304aac
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 14 deletions.
42 changes: 28 additions & 14 deletions _includes/batch_processing/batch_measure_nuclei_shape.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,40 @@

# %%
# Import python modules
from OpenIJTIFF import open_ij_tiff
from OpenIJTIFF import open_ij_tiff, save_ij_tiff
from skimage.measure import label, regionprops_table
from skimage.filters import threshold_otsu
from skimage.io import imsave
import pandas as pd
import pathlib
from pathlib import Path
from napari import Viewer

# %%
# Create a function that analyses one image
# Below, this function will be called several times, for all images
def analyse(image_path, output_folder):

# This prints which image is currently analysed
print("Analyzing:", image_path)

image, axes, scales, units = open_ij_tiff(image_path)

# Binarize the image using auto-thresholding
threshold = threshold_otsu(image)
print("Threshold:", threshold)
binary_image = image > threshold

# Perform connected components analysis (i.e create labels)
label_image = label(binary_image)

# Note that label returns 32 bit data which save_ij_tif below can't handle.
# We can safely convert to 16 bit as we know that we don't have too many objects
label_image = label(binary_image).astype('uint16')

# Measure calibrated (scaled) nuclei shapes
df = pd.DataFrame( regionprops_table(
df = pd.DataFrame(regionprops_table(
label_image,
properties = {'label', 'area'},
spacing = scales ) )
properties={'label', 'area'},
spacing=scales))

# Round all measurements to 2 decimal places.
# This increases the readability a lot,
# but depending on your scientific question,
Expand All @@ -44,8 +50,8 @@ def analyse(image_path, output_folder):
image_path = pathlib.Path(image_path)

# Save the labels
label_image_path = output_folder / f"{image_path.stem}_labels.tif"
imsave(label_image_path, label_image)
label_image_path = output_folder / f"{image_path.stem}_labels.tif"
save_ij_tiff(label_image_path, label_image, axes, scales, units)

# Save the measurements table
# to a tab delimited text file (sep='\t')
Expand All @@ -57,13 +63,21 @@ def analyse(image_path, output_folder):
# %%
# Assign an output folder
# Note: This uses your current working directory; you may want to change this to another folder on your computer
output_folder = Path.cwd()
output_dir = Path.cwd()

# %%
# Create a list of the paths to all data
image_paths = ["https://github.com/NEUBIAS/training-resources/raw/master/image_data/xy_8bit__mitocheck_incenp_t1.tif",
"https://github.com/NEUBIAS/training-resources/raw/master/image_data/xy_8bit__mitocheck_incenp_t70.tif"]

for image_path in image_paths:
print("Analyzing:", image_path)
analyse(image_path, output_folder)
analyse(image_path, output_dir)

# %%
# Plot the first output image to check if the pipeline worked
image1, *_ = open_ij_tiff(image_paths[0])
labels1, *_ = open_ij_tiff('xy_8bit__mitocheck_incenp_t1_labels.tif')

viewer = Viewer()
viewer.add_image(image1)
viewer.add_labels(labels1)
13 changes: 13 additions & 0 deletions _includes/batch_processing/batch_measure_nuclei_shapes.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,16 @@

- In a previous module [there is a workflow to measure the shapes of nuclei in one image](https://neubias.github.io/training-resources/workflow_segment_2d_nuclei_measure_shape/index.html#2dnuclei)
- Adapt this workflow for automated batch analysis of many images
- Start by building the skeleton of the workflow without filling in the functionality;

Note that the code below runs fine, but does not produce any results:

```
def analyse(image_path, output_folder):
print("Analyzing:", image_path)
for image_path in image_paths:
analyse(image_path, output_dir)
```

- Make sure the loop with the (almost) empty analyse function runs without error before filling in the image analysis steps

0 comments on commit 3304aac

Please sign in to comment.