Skip to content

Commit d2aa6d8

Browse files
committed
empty crop without AZ annotation
1 parent b769a8e commit d2aa6d8

File tree

2 files changed

+98
-1
lines changed

2 files changed

+98
-1
lines changed

scripts/cooper/revision/assort_new_az_data.py

Lines changed: 80 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from glob import glob
33

44
import h5py
5+
import tifffile
56
import numpy as np
67
from tqdm import tqdm
78
from skimage.transform import resize
@@ -12,6 +13,7 @@
1213
INTER_ROOT = "/mnt/ceph-hdd/cold/nim00007/AZ_predictions"
1314
OUTPUT_ROOT = "/mnt/ceph-hdd/cold/nim00007/new_AZ_train_data"
1415
STEM_INPUT="/mnt/lustre-emmy-hdd/usr/u12095/synaptic_reconstruction/for_revison/postprocessed_AZ"
16+
TIF_INPUT = "/mnt/ceph-hdd/cold/nim00007/new_AZ_train_data/stem/"
1517

1618

1719
def _check_data(files, label_folder, check_thinned):
@@ -264,6 +266,82 @@ def crop_stem():
264266
f.create_dataset("labels/az", data=az_crop, compression="lzf")
265267
crop_id += 1
266268

269+
def get_bounding_box_3d(file_path, raw_volume):
270+
volume = tifffile.imread(file_path)
271+
filename = os.path.basename(file_path)
272+
print(f"filename {filename}")
273+
274+
# Find the z index where the 2D rectangle is located (non-zero slice)
275+
z_indices = np.where(np.any(volume, axis=(1, 2)))[0]
276+
277+
if len(z_indices) == 0:
278+
raise ValueError("No non-zero 2D rectangle found in the volume.")
279+
280+
z_rect = z_indices[0]
281+
282+
# Get the 2D mask from that slice
283+
mask_2d = volume[z_rect]
284+
y_indices, x_indices = np.where(mask_2d)
285+
286+
if len(x_indices) == 0 or len(y_indices) == 0:
287+
raise ValueError("Found slice has no non-zero pixels.")
288+
289+
x_min, x_max = x_indices.min(), x_indices.max() + 1
290+
y_min, y_max = y_indices.min(), y_indices.max() + 1
291+
292+
# Determine z_start and z_end based on filename
293+
if filename.endswith("_toend.tif"):
294+
z_start, z_end = z_rect, raw_volume.shape[0]
295+
elif filename.endswith("_tostart.tif"):
296+
z_start, z_end = 0, z_rect + 1
297+
else:
298+
print("here?")
299+
z_start, z_end = z_rect, z_rect + 1
300+
301+
# Return bounding box as slices, usable directly for numpy indexing
302+
return (
303+
slice(z_start, z_end),
304+
slice(y_min, y_max),
305+
slice(x_min, x_max)
306+
)
307+
308+
def neg_crop_stem():
309+
input_name = "mask_for_neg_example"#"04_hoi_stem_examples_minusSVseg"
310+
output_name = "stem_cropped2"
311+
312+
input_folder = TIF_INPUT
313+
tif_input_folder = os.path.join(TIF_INPUT, input_name)
314+
output_folder = os.path.join(OUTPUT_ROOT, output_name)
315+
os.makedirs(output_folder, exist_ok=True)
316+
tif_files = glob(os.path.join(tif_input_folder, "*.tif"))
317+
print(f"tif_files {tif_files}")
318+
319+
for ff in tqdm(tif_files):
320+
input_path = os.path.join(input_folder, os.path.basename(ff).replace('_tostart.tif', '.h5').replace('_toend.tif', '.h5'))
321+
with h5py.File(input_path, "r") as f:
322+
raw_full = f["raw"][:]
323+
324+
325+
output_path = os.path.join(output_folder, os.path.basename(ff).replace('_tostart.tif', '_cropped_noAZ.h5').replace('_toend.tif', '_cropped_noAZ.h5'))
326+
if os.path.exists(output_path):
327+
print(f"Skipping existing file: {output_path}")
328+
continue
329+
330+
331+
bb = get_bounding_box_3d(ff, raw_full)
332+
print(f"bb {bb}")
333+
334+
raw_crop = raw_full[bb]
335+
336+
337+
import napari
338+
v = napari.Viewer()
339+
v.add_image(raw_crop)
340+
napari.run()
341+
342+
with h5py.File(output_path, "a") as f:
343+
f.create_dataset("raw", data=raw_crop, compression="lzf")
344+
267345
def main():
268346
# assort_tem()
269347
# assort_chemical_fixation()
@@ -273,7 +351,8 @@ def main():
273351
# assort_wichmann()
274352
#crop_wichmann()
275353

276-
crop_stem()
354+
#crop_stem()
355+
neg_crop_stem()
277356

278357

279358
if __name__ == "__main__":
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import h5py
2+
3+
files = [
4+
"/mnt/ceph-hdd/cold/nim00007/new_AZ_train_data/stem_cropped2_rescaled/36859_H2_SP_02_rec_2Kb1dawbp_crop_crop1.h5",
5+
"/mnt/ceph-hdd/cold/nim00007/new_AZ_train_data/stem_cropped2_rescaled/36859_H2_SP_07_rec_2Kb1dawbp_crop_crop1.h5"
6+
]
7+
8+
for file in files:
9+
with h5py.File(file, "r+") as f:
10+
# Load the replacement data
11+
gt = f["labels/az"][:]
12+
13+
# Delete the existing dataset if it exists
14+
if "labels/az_thin" in f:
15+
del f["labels/az_thin"]
16+
17+
# Recreate the dataset with the new data
18+
f.create_dataset("labels/az_thin", data=gt)

0 commit comments

Comments
 (0)