Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Finalize segmentation #6

Draft
wants to merge 51 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
51 commits
Select commit Hold shift + click to select a range
6cdb987
Initial commit
MikeLippincott Dec 2, 2024
007c4ca
inital commit + preprocessing (#1)
MikeLippincott Dec 4, 2024
e438dd0
gitignore
MikeLippincott Dec 12, 2024
aa116ed
Merge remote-tracking branch 'upstream/main'
MikeLippincott Dec 12, 2024
8b7f04b
Merge remote-tracking branch 'upstream/main'
MikeLippincott Dec 20, 2024
be53653
Merge remote-tracking branch 'upstream/main'
MikeLippincott Dec 20, 2024
74584fc
progress capture
MikeLippincott Dec 23, 2024
dcfc868
test on alpine
MikeLippincott Dec 24, 2024
9d5a1b2
testing on alpine
MikeLippincott Dec 25, 2024
94ddb5d
alpine errors fixed?
MikeLippincott Dec 25, 2024
cf33dea
add fix
MikeLippincott Dec 25, 2024
92ca33e
run again
MikeLippincott Dec 25, 2024
d61ed99
run on alpine
MikeLippincott Dec 25, 2024
8415ea6
init segs
MikeLippincott Dec 25, 2024
0d19755
run all
MikeLippincott Dec 25, 2024
9877a77
test running all
MikeLippincott Dec 26, 2024
fffdf32
fix slurm script
MikeLippincott Dec 26, 2024
c53a001
update runs
MikeLippincott Dec 26, 2024
43dc6c1
fix run
MikeLippincott Dec 27, 2024
74d335c
prep segmentations for cellprofiler
MikeLippincott Dec 27, 2024
f7b34dd
run all segs
MikeLippincott Dec 27, 2024
c1ea7f1
rerun_pipe in the correct order
MikeLippincott Dec 27, 2024
24b218b
rerun
MikeLippincott Dec 27, 2024
9e37507
fix slurm timing
MikeLippincott Dec 29, 2024
6ef9854
spelling fixes
MikeLippincott Dec 29, 2024
7a6a93e
progress capture
MikeLippincott Dec 30, 2024
5cbf1df
update file checker
MikeLippincott Dec 30, 2024
1a6b7f8
run on HPC
MikeLippincott Dec 31, 2024
6cd2557
testing on HPC
MikeLippincott Dec 31, 2024
2f41427
fix slurm workflow
MikeLippincott Dec 31, 2024
2184d17
fix slurm workflow
MikeLippincott Dec 31, 2024
6067b93
test dir testing
MikeLippincott Dec 31, 2024
35e48de
test dir testing
MikeLippincott Dec 31, 2024
be736ac
process all on HPC please
MikeLippincott Dec 31, 2024
1a1e115
testing on HPC
MikeLippincott Jan 1, 2025
fbeb16e
testing continues
MikeLippincott Jan 1, 2025
b1086cc
run attempt
MikeLippincott Jan 1, 2025
1a6ab56
attempting success
MikeLippincott Jan 1, 2025
8f98173
run on HPC
MikeLippincott Jan 1, 2025
ebbf4b0
run again
MikeLippincott Jan 2, 2025
9ae4e8b
rerun on HPC
MikeLippincott Jan 2, 2025
dc79637
rerun the bad runs
MikeLippincott Jan 4, 2025
1413ebc
attempt to run all on alpine again
MikeLippincott Jan 7, 2025
2848d7c
add todo
MikeLippincott Jan 7, 2025
9d62760
progress capture
MikeLippincott Jan 9, 2025
0d1edb4
rerun segmentations
MikeLippincott Jan 9, 2025
425e9ec
segmentation on HPC
MikeLippincott Jan 9, 2025
d393fa4
fixing segmentaiton
MikeLippincott Jan 9, 2025
1d9f5e7
fix child scripts
MikeLippincott Jan 12, 2025
5bc41dc
edit organoid compatibility
MikeLippincott Jan 12, 2025
ec6ac2c
rerun example segmentations
MikeLippincott Jan 16, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -165,3 +165,8 @@ cython_debug/
data/*
1.segment_images/examples/raw_z_input/*
1.segment_images/processed_data/*
1.segment_images/scripts/job_ids.txt
1.segment_images/*.out

# SLURM output_files
*.out
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ repos:

# remove unused imports
- repo: https://github.com/hadialqattan/pycln.git
rev: v2.4.0
rev: v2.5.0
hooks:
- id: pycln

Expand Down
1 change: 1 addition & 0 deletions 0.preprocessing_data/HPC_preprocessing.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ cd scripts/ || exit

python 0.update_file_structure.py --HPC True
python 1.make_z-stack_images.py
python 3.data_validation.py

cd .. || exit

Expand Down
2 changes: 2 additions & 0 deletions 0.preprocessing_data/local_preprocessing.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ cd scripts/ || exit

python 0.update_file_structure.py --HPC False
python 1.make_z-stack_images.py
python 3.data_validation.py


cd .. || exit

Expand Down
123 changes: 123 additions & 0 deletions 0.preprocessing_data/notebooks/3.data_validation.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": []
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import pathlib\n",
"\n",
"import numpy as np\n",
"import skimage.io as io"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"def image_directory_check(\n",
" image_directory: pathlib.Path, expected_number_of_files: int = 5\n",
") -> int:\n",
" counter = 0\n",
" files = [x for x in image_directory.iterdir() if x.is_file()]\n",
" if len(files) < expected_number_of_files or len(files) > expected_number_of_files:\n",
" print(f\"{image_directory} has {len(files)} files\")\n",
" counter += 1\n",
" for file in files:\n",
" image = io.imread(file)\n",
" # check that image contains non-zero values\n",
" if np.count_nonzero(image) == 0:\n",
" print(f\"{file} contains only zeros\")\n",
" counter += 1\n",
" return counter"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"z_stack_images_dir = pathlib.Path(\"../../data/z-stack_images/\").resolve(strict=True)\n",
"normalized_z_stack_images_dir = pathlib.Path(\"../../data/normalized_z/\").resolve(\n",
" strict=True\n",
")"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"/home/lippincm/Documents/GFF_3D_organoid_profiling_pipeline/data/z-stack_images/F3-1/F3-1_555.tif contains only zeros\n",
"/home/lippincm/Documents/GFF_3D_organoid_profiling_pipeline/data/z-stack_images/F3-1/F3-1_488.tif contains only zeros\n",
"/home/lippincm/Documents/GFF_3D_organoid_profiling_pipeline/data/z-stack_images/F3-1/F3-1_405.tif contains only zeros\n",
"/home/lippincm/Documents/GFF_3D_organoid_profiling_pipeline/data/z-stack_images/F3-1/F3-1_TRANS.tif contains only zeros\n",
"/home/lippincm/Documents/GFF_3D_organoid_profiling_pipeline/data/z-stack_images/F3-1/F3-1_640.tif contains only zeros\n",
"/home/lippincm/Documents/GFF_3D_organoid_profiling_pipeline/data/normalized_z/F3-1 has 0 files\n",
"Total number of issues in z-stack images: 5\n",
"Total number of issues in normalized z-stack images: 1\n"
]
}
],
"source": [
"z_stack_images_dir_directories = [x for x in z_stack_images_dir.iterdir() if x.is_dir()]\n",
"\n",
"z_stack_counter = 0\n",
"for dir in z_stack_images_dir_directories:\n",
" directory_counter = image_directory_check(\n",
" image_directory=dir, expected_number_of_files=5\n",
" )\n",
" z_stack_counter += directory_counter\n",
"\n",
"normalized_z_stack_images_dir_directories = [\n",
" x for x in normalized_z_stack_images_dir.iterdir() if x.is_dir()\n",
"]\n",
"normalized_z_stack_counter = 0\n",
"for dir in normalized_z_stack_images_dir_directories:\n",
" directory_counter = image_directory_check(\n",
" image_directory=dir, expected_number_of_files=5\n",
" )\n",
" normalized_z_stack_counter += directory_counter\n",
"\n",
"print(f\"Total number of issues in z-stack images: {z_stack_counter}\")\n",
"print(\n",
" f\"Total number of issues in normalized z-stack images: {normalized_z_stack_counter}\"\n",
")"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "gff_preprocessing_env",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.15"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
24 changes: 24 additions & 0 deletions 1.segment_images/HPC_segmentation.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/bin/bash
#SBATCH --nodes=1
#SBATCH --ntasks=1
#SBATCH --partition=amilan
#SBATCH --qos=long
#SBATCH --account=amc-general
#SBATCH --time=7-00:00:00
#SBATCH --output=main_job_submit_output-%j.out

cd slurm_scripts || exit

job1=$(sbatch 0.inital_segmentation_HPC.sh | awk '{print $4}')

job2=$(sbatch --dependency=afterok:$job1 1a.process_segmentation_parent.sh | awk '{print $4}')

job3=$(sbatch --dependency=afterok:$job2 2a.process_segmentation_parent.sh | awk '{print $4}')

job4=$(sbatch --dependency=afterok:$job3 3a.process_segmentation_parent.sh | awk '{print $4}')

job5=$(sbatch --dependency=afterok:$job4 4.check_job_status.sh | awk '{print $4}')

echo "All jobs submitted"

exit 0
1 change: 1 addition & 0 deletions 1.segment_images/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ For nuclei and cells, we take a sliding window of n z-slices, where we perform m
We then decouple the max-projection to collect a segmentation mask per z-slice.
We then output the masks across all z-slices per image set as a stacked-TIFF file.

Well F4-2 for the NF0014 group throws an error in cellpose - I will exclude these images from the segmentation process.
66 changes: 32 additions & 34 deletions 1.segment_images/local_segmentation.sh
Original file line number Diff line number Diff line change
@@ -1,45 +1,43 @@
#!/bin/bash

NOTEBOOK=False

# activate cellprofiler environment
conda activate GFF_segmentation

jupyter nbconvert --to=script --FilesWriter.build_directory=scripts/ notebooks/*.ipynb

# run the notebooks with papermill
if [ "$NOTEBOOK" = True ]; then
cd notebooks/ || exit
papermill 0.segment_nuclei_organoids.ipynb 0.segment_nuclei_organoids.ipynb
papermill 1.segment_cells_organoids.ipynb 1.segment_cells_organoids.ipynb
papermill 2.make_nuclei_segmentation_videos.ipynb 2.make_nuclei_segmentation_videos.ipynb
papermill 3.make_cell_segmentation_videos.ipynb 3.make_cell_segmentation_videos.ipynb
cd ../ || exit
fi

if [ "$NOTEBOOK" = False ]; then
cd scripts/ || exit
# get all input directories in specified directory
z_stack_dir="../../data/z-stack_images/"
input_dirs=$(ls -d $z_stack_dir*)


total_dirs=$(echo "$input_dirs" | wc -l)
current_dir=0

# loop through all input directories
for dir in $input_dirs; do
dir=${dir%*/}
current_dir=$((current_dir + 1))
echo -ne "Processing directory $current_dir of $total_dirs\r"
python 0.segment_nuclei_organoids.py --input_dir "$dir" --window_size 3 --clip_limit 0.05
python 1.segment_cells_organoids.py --input_dir "$dir" --window_size 3 --clip_limit 0.1
python 2.make_nuclei_segmentation_videos.py --input_dir "$dir"
python 3.make_cell_segmentation_videos.py --input_dir "$dir"

cd scripts/ || exit
# get all input directories in specified directory
# z_stack_dir="../../data/z-stack_images"
z_stack_dir="../../data/test_dir/"
mapfile -t input_dirs < <(ls -d "$z_stack_dir"/*)

total_dirs=$(echo "${input_dirs[@]}" | wc -w)
echo "Total directories: $total_dirs"
current_dir=0
compartments=( "nuclei" "cell" )

# loop through all input directories
for dir in "${input_dirs[@]}"; do
dir=${dir%*/}
current_dir=$((current_dir + 1))
echo -ne "Processing directory $current_dir of $total_dirs\r"
python 0.segment_nuclei_organoids.py --input_dir "$dir" --window_size 3 --clip_limit 0.05
python 1.segment_cells_organoids.py --input_dir "$dir" --window_size 3 --clip_limit 0.1
python 2.segment_whole_organoids.py --input_dir "$dir" --window_size 3 --clip_limit 0.1
for compartment in "${compartments[@]}"; do
python 3.segmentation_decoupling.py --input_dir "$dir" --compartment "$compartment"
python 4.reconstruct_3D_masks.py --input_dir "$dir" --compartment "$compartment" --radius_constraint 10
python 6.make_cell_segmentation_videos.py --input_dir "$dir" --compartment "$compartment"
done
echo -ne "\n"
cd ../ || exit
fi
python 5.create_cytoplasm_masks.py --input_dir "$dir"
python 6.make_cell_segmentation_videos.py --input_dir "$dir" --compartment "cytoplasm"
python 7.clean_up_segmentation.py

done
echo -ne "\n"
cd ../ || exit


# deactivate cellprofiler environment
conda deactivate
Expand Down
Loading