Skip to content

Commit 3501408

Browse files
committed
Add template for processing
1 parent 01d1352 commit 3501408

File tree

2 files changed

+95
-32
lines changed

2 files changed

+95
-32
lines changed

flamingo_tools/segmentation/sgn_detection.py

Lines changed: 35 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,7 @@ def extend_block(block_id):
5757
outer_block = block.outerBlock
5858
inner_block = block.innerBlock
5959

60-
# TODO get the indices and coordinates of the markers in the INNER block
61-
# markers_in_block_ids = [int(i) for i in np.unique(inner_block)[1:]]
60+
# get the indices and coordinates of the markers in the INNER block
6261
mask = (
6362
(inner_block.begin[0] <= markers[:, 0]) & (markers[:, 0] <= inner_block.end[0]) &
6463
(inner_block.begin[1] <= markers[:, 1]) & (markers[:, 1] <= inner_block.end[1]) &
@@ -67,33 +66,35 @@ def extend_block(block_id):
6766
markers_in_block_ids = np.where(mask)[0]
6867
markers_in_block_coords = markers[markers_in_block_ids]
6968

70-
# TODO offset the marker coordinates with respect to the OUTER block
71-
markers_in_block_coords = [coord - outer_block.begin for coord in markers_in_block_coords]
72-
markers_in_block_coords = [[round(c) for c in coord] for coord in markers_in_block_coords]
73-
markers_in_block_coords = np.array(markers_in_block_coords, dtype=int)
74-
z, y, x = markers_in_block_coords.T
69+
# proceed if detections fall within inner block
70+
if len(markers_in_block_coords) > 0:
71+
markers_in_block_coords = [coord - outer_block.begin for coord in markers_in_block_coords]
72+
markers_in_block_coords = [[round(c) for c in coord] for coord in markers_in_block_coords]
7573

76-
# Shift index by one so that zero is reserved for background id
77-
markers_in_block_ids += 1
74+
markers_in_block_coords = np.array(markers_in_block_coords, dtype=int)
75+
z, y, x = markers_in_block_coords.T
7876

79-
# Create the seed volume.
80-
outer_block_shape = tuple(end - begin for begin, end in zip(outer_block.begin, outer_block.end))
81-
seeds = np.zeros(outer_block_shape, dtype="uint32")
82-
seeds[z, y, x] = markers_in_block_ids
77+
# Shift index by one so that zero is reserved for background id
78+
markers_in_block_ids += 1
8379

84-
# Compute the distance map.
85-
distance = distance_transform_edt(seeds == 0, sampling=sampling)
80+
# Create the seed volume.
81+
outer_block_shape = tuple(end - begin for begin, end in zip(outer_block.begin, outer_block.end))
82+
seeds = np.zeros(outer_block_shape, dtype="uint32")
83+
seeds[z, y, x] = markers_in_block_ids
8684

87-
# And extend the seeds
88-
mask = distance < extension_distance
89-
segmentation = watershed(distance.max() - distance, markers=seeds, mask=mask)
85+
# Compute the distance map.
86+
distance = distance_transform_edt(seeds == 0, sampling=sampling)
9087

91-
# Write the segmentation. Note: we need to lock here because we write outside of our inner block
92-
bb = tuple(slice(begin, end) for begin, end in zip(outer_block.begin, outer_block.end))
93-
with lock:
94-
this_output = output[bb]
95-
this_output[mask] = segmentation[mask]
96-
output[bb] = this_output
88+
# And extend the seeds
89+
mask = distance < extension_distance
90+
segmentation = watershed(distance.max() - distance, markers=seeds, mask=mask)
91+
92+
# Write the segmentation. Note: we need to lock here because we write outside of our inner block
93+
bb = tuple(slice(begin, end) for begin, end in zip(outer_block.begin, outer_block.end))
94+
with lock:
95+
this_output = output[bb]
96+
this_output[mask] = segmentation[mask]
97+
output[bb] = this_output
9798

9899
n_blocks = blocking.numberOfBlocks
99100
with futures.ThreadPoolExecutor(n_threads) as tp:
@@ -146,10 +147,18 @@ def sgn_detection(
146147
detection_path = os.path.join(output_folder, "SGN_detection.tsv")
147148
if not os.path.exists(detection_path):
148149
input_ = zarr.open(output_path, "r")[prediction_key]
149-
detections = find_local_maxima(
150+
detections_maxima = find_local_maxima(
150151
input_, block_shape=block_shape, min_distance=4, threshold_abs=0.5, verbose=True, n_threads=16,
151152
)
152153

154+
# Save the result in mobie compatible format.
155+
detections = np.concatenate(
156+
[np.arange(1, len(detections_maxima) + 1)[:, None], detections_maxima[:, ::-1]], axis=1
157+
)
158+
detections = pd.DataFrame(detections, columns=["spot_id", "x", "y", "z"])
159+
detections.to_csv(detection_path, index=False, sep="\t")
160+
161+
# extend detection
153162
shape = input_.shape
154163
chunks = (128, 128, 128)
155164
segmentation_path = os.path.join(output_folder, "segmentation.zarr")
@@ -161,17 +170,11 @@ def sgn_detection(
161170
)
162171

163172
distance_based_marker_extension(
164-
markers=detections,
173+
markers=detections_maxima,
165174
output=output_dataset,
166175
extension_distance=extension_distance,
167176
sampling=sampling,
168177
block_shape=(128, 128, 128),
169178
n_threads=n_threads,
170179
)
171180

172-
# Save the result in mobie compatible format.
173-
detections = np.concatenate(
174-
[np.arange(1, len(detections) + 1)[:, None], detections[:, ::-1]], axis=1
175-
)
176-
detections = pd.DataFrame(detections, columns=["spot_id", "x", "y", "z"])
177-
detections.to_csv(detection_path, index=False, sep="\t")
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
#!/bin/bash
2+
#SBATCH --job-name=synapse-detect
3+
#SBATCH -t 03:00:00 # estimated time, adapt to your needs
4+
#SBATCH --mail-type=FAIL # send mail when job begins and ends
5+
6+
#SBATCH -p grete:shared # the partition
7+
#SBATCH -G A100:1 # For requesting 1 A100 GPU.
8+
#SBATCH -A nim00007
9+
#SBATCH -c 4
10+
#SBATCH --mem 32G
11+
12+
source ~/.bashrc
13+
# micromamba activate micro-sam_gpu
14+
micromamba activate sam
15+
16+
# Print out some info.
17+
echo "Submitting job with sbatch from directory: ${SLURM_SUBMIT_DIR}"
18+
echo "Home directory: ${HOME}"
19+
echo "Working directory: $PWD"
20+
echo "Current node: ${SLURM_NODELIST}"
21+
22+
# Run the script
23+
#python myprogram.py $SLURM_ARRAY_TASK_ID
24+
25+
# SCRIPT_REPO=/user/schilling40/u15000/flamingo-tools
26+
SCRIPT_REPO=/user/pape41/u12086/Work/my_projects/flamingo-tools
27+
cd "$SCRIPT_REPO"/flamingo_tools/segmentation/ || exit
28+
29+
export SCRIPT_DIR=$SCRIPT_REPO/scripts
30+
31+
# name of cochlea, as it appears in MoBIE and the NHR
32+
COCHLEA=$1
33+
# model of SGN detection, e.g. v5b
34+
MODEL_VERSION=$2
35+
36+
# data on NHR
37+
MOBIE_DIR=/mnt/vast-nhr/projects/nim00007/data/moser/cochlea-lightsheet/mobie_project/cochlea-lightsheet/
38+
export INPUT_PATH="$MOBIE_DIR"/"$COCHLEA"/images/ome-zarr/PV.ome.zarr
39+
40+
# data on MoBIE
41+
# export INPUT_PATH="$COCHLEA"/images/ome-zarr/PV.ome.zarr
42+
# use --s3 flag for script
43+
44+
export OUTPUT_FOLDER=/mnt/vast-nhr/projects/nim00007/data/moser/cochlea-lightsheet/predictions/"$COCHLEA"/SGN_detect-"$MODEL_VERSION"
45+
46+
if ! [[ -f $OUTPUT_FOLDER ]] ; then
47+
mkdir -p "$OUTPUT_FOLDER"
48+
fi
49+
50+
export MODEL=/mnt/vast-nhr/projects/nim00007/data/moser/cochlea-lightsheet/trained_models/SGN/sgn-detection-"$MODEL_VERSION".pt
51+
INPUT_KEY="s0"
52+
53+
echo "OUTPUT_FOLDER $OUTPUT_FOLDER"
54+
echo "MODEL $MODEL"
55+
56+
python ~/flamingo-tools/scripts/sgn_detection/sgn_detection.py \
57+
--input "$INPUT_PATH" \
58+
--input_key $INPUT_KEY \
59+
--output_folder "$OUTPUT_FOLDER" \
60+
--model "$MODEL"

0 commit comments

Comments
 (0)