Skip to content

Commit

Permalink
All measurement results to a single MoBIE-readable csv
Browse files Browse the repository at this point in the history
  • Loading branch information
jhennies committed Jul 30, 2024
1 parent 871c813 commit c2a0cf2
Showing 1 changed file with 25 additions and 26 deletions.
51 changes: 25 additions & 26 deletions _includes/batch_processing/batch_measure_nuclei_shape.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# %%
# Batch analysis of 2D nuclei shape measurements


# %%
# Import python modules
from OpenIJTIFF import open_ij_tiff, save_ij_tiff
Expand All @@ -19,6 +20,10 @@ def analyse(image_filepath, output_folder):
# This prints which image is currently analysed
print("Analyzing:", image_filepath)

# Convert the image_filepath String to a Path,
# which is more convenient to create the output files
image_filepath = pathlib.Path(image_filepath)

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

# Binarize the image using auto-thresholding
Expand All @@ -31,10 +36,14 @@ def analyse(image_filepath, output_folder):
# 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')

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

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

# Round all measurements to 2 decimal places.
Expand All @@ -43,23 +52,12 @@ def analyse(image_filepath, output_folder):
# you may not want to round that much!
df = df.round(2)

# Save the results to disk
# Add the image and label filepaths to the data-frame
df['image'] = image_filepath
df['labels'] = label_image_filepath

# Convert the image_filepath String to a Path,
# which is more convenient to create the output files
image_filepath = pathlib.Path(image_filepath)

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

# Save the measurements table
# to a tab delimited text file (sep='\t')
# without row numbers (index=False)
table_path = output_folder / f"{image_filepath.stem}_measurements.csv"
df.to_csv(table_path, sep='\t', index=False)

return label_image_filepath
# Return the data-frame
return df


# %%
Expand All @@ -72,23 +70,24 @@ def analyse(image_filepath, output_folder):
# Create a list of the paths to all data
image_paths = [output_dir / "xy_8bit__mitocheck_incenp_t1.tif",
output_dir / "xy_8bit__mitocheck_incenp_t70.tif"]
# Create an empty list for the result label images
label_image_paths = []
# Create an empty list for the measurement results
result_dfs = []


# %%
# The loop which performs the analysis
for image_path in image_paths:

# Computes the analysis and returns the path of the resulting label image
label_image_path = analyse(image_path, output_dir)
# Computes the analysis and returns a data-frame with the resulting measurements
result_df = analyse(image_path, output_dir)

# Append the label image path to the list initialized before the loop
label_image_paths.append(label_image_path)
result_dfs.append(result_df)


# %%
# Create a table to view the results in MoBIE
df = pd.DataFrame(data={'image': image_paths, 'labels': label_image_paths})
mobie_table_path = output_dir + Path('mobie_table.csv')
df.to_csv(mobie_table_path, sep='\t', index=False)
# Concatenate the result data-frames to a single one which contains all results
final_df = pd.concat(result_dfs, ignore_index=True)
# Save the final results to disk
final_df.to_csv(output_dir / 'batch_processing_results.csv', sep='\t', index=False)

0 comments on commit c2a0cf2

Please sign in to comment.