Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
20 changes: 17 additions & 3 deletions bin/crop_hdf5.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,10 +122,12 @@ def IlastikPrepOME(
full_h5 = h5py.File(pathlib.Path(os.path.join(output, h5_name)), "r")
im_nuc = full_h5[str(im_stem)][:, :, :, nuclei_index]
im = full_h5[str(im_stem)][:, :, :, :]
# Track previous crops as (h_up, h_down, w_lt, w_rt) tuples
previous_crops = []
indices = {}
count = 0
count = 1
thresh = filters.threshold_otsu(im_nuc[:, :, :])
while count < crop_amount:
while count < crop_amount+1:
# Get random height value that falls within crop range of the edges
extension_h = crop_size[0] // 2
h = random.randint(extension_h, im_nuc.shape[1] - extension_h)
Expand All @@ -134,6 +136,16 @@ def IlastikPrepOME(
extension_w = crop_size[1] // 2
w = random.randint(extension_w, im_nuc.shape[2] - extension_w)
w_lt, w_rt = w - extension_w, w + extension_w
# Check if the new crop overlaps with any previous crop
overlap = False
for prev_crop in previous_crops:
prev_h_up, prev_h_down, prev_w_lt, prev_w_rt = prev_crop
if not (h_down <= prev_h_up or h_up >= prev_h_down or w_rt <= prev_w_lt or w_lt >= prev_w_rt):
overlap = True
break
# If overlap, try again. Otherwise, save the crop and continue
if overlap:
continue
# Crop the image with these coordinates expanding from center
crop = im_nuc[:, h_up:h_down, w_lt:w_rt]
crop_name = pathlib.Path(os.path.join(output, (im_stem + "_crop" + str(count) + ".hdf5")))
Expand All @@ -147,7 +159,9 @@ def IlastikPrepOME(
h5_crop.close()
print("Finished exporting " + crop_name.stem + ".hdf5")
# Add one to the counter
count = count + 1
count += 1
# Add the current crop coordinates to the list of previous crops
previous_crops.append((h_up, h_down, w_lt, w_rt))
# Add the indices to a table to store the cropped indices
indices.update({crop_name.stem: [(h_up, h_down), (w_lt, w_rt)]})
# Export the indices to a text file to track the cropped regions
Expand Down
2 changes: 1 addition & 1 deletion bin/crop_tiff.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

# Create a function to create crops from a tiff image and a dictionary of crop coordinates
def create_crops(tiff_image, crop_dict):
for index, (crop_name, crop) in enumerate(crop_dict.items()):
for index, (crop_name, crop) in enumerate(crop_dict.items(), start=1):
crop_image = tiff_image[:, crop[0][0] : crop[0][1], crop[1][0] : crop[1][1]]
basename = os.path.basename(args.input)
basename = os.path.splitext(basename)[0]
Expand Down
8 changes: 4 additions & 4 deletions tests/main.nf.test.snap
Original file line number Diff line number Diff line change
Expand Up @@ -362,11 +362,11 @@
"pipeline_info/nf_core_molkart_software_mqc_versions.yml",
"training_subset",
"training_subset/hdf5",
"training_subset/hdf5/nuc_only_nuclear_gridfilled_clahe_crop0.hdf5",
"training_subset/hdf5/nuc_only_nuclear_gridfilled_clahe_crop1.hdf5",
"training_subset/hdf5/nuc_only_nuclear_gridfilled_clahe_crop2.hdf5",
"training_subset/tiff",
"training_subset/tiff/nuc_only_nuclear_gridfilled_clahe_crop0.tiff",
"training_subset/tiff/nuc_only_nuclear_gridfilled_clahe_crop1.tiff"
"training_subset/tiff/nuc_only_nuclear_gridfilled_clahe_crop1.tiff",
"training_subset/tiff/nuc_only_nuclear_gridfilled_clahe_crop2.tiff"
],
[
"nuc_only_nuclear_gridfilled.tiff:md5,123763d54f05b2274690b3b84f9690b1",
Expand All @@ -379,6 +379,6 @@
"nf-test": "0.9.2",
"nextflow": "24.10.5"
},
"timestamp": "2025-03-05T17:00:52.037627778"
"timestamp": "2025-03-25T10:00:57.349249698"
}
}