Skip to content

Commit fd52df3

Browse files
Enable lazy data loading in prediction check script
1 parent c065849 commit fd52df3

File tree

1 file changed

+18
-9
lines changed

1 file changed

+18
-9
lines changed

scripts/prediction/check_prediction.py

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,31 +7,36 @@
77
from elf.io import open_file
88

99

10+
def _load_n5(path, key, lazy):
11+
data = open_file(path, "r")[key]
12+
if not lazy:
13+
data = data[:]
14+
return data
15+
16+
1017
# will not work for very large images
11-
def check_prediction(input_path, output_folder, check_downsampled=False, input_key=None):
18+
def check_prediction(input_path, output_folder, check_downsampled=False, input_key=None, lazy=False):
1219
if input_key is None:
1320
input_ = imageio.imread(input_path)
1421
else:
15-
input_ = open_file(input_path, "r")[input_key][:]
22+
input_ = _load_n5(input_path, input_key, lazy)
1623

1724
if check_downsampled:
1825

1926
pred_path = os.path.join(output_folder, "predictions.zarr")
20-
with open_file(pred_path, "r") as f:
21-
prediction = f["prediction"][:]
27+
prediction = _load_n5(pred_path, "prediction", lazy)
2228

2329
seg_path = os.path.join(output_folder, "seg_downscaled.zarr")
24-
with open_file(seg_path, "r") as f:
25-
segmentation = f["segmentation"][:]
30+
segmentation = _load_n5(seg_path, "segmentation", lazy)
2631

2732
scale = (0.5, 0.5, 0.5)
2833

2934
else:
3035
seg_path = os.path.join(output_folder, "segmentation.zarr")
36+
segmentation = _load_n5(seg_path, "segmentation", lazy)
3137
with open_file(seg_path, "r") as f:
32-
segmentation = f["segmentation"][:]
3338
if "segmentation_postprocessed" in f:
34-
seg_pp = f["segmentation_postprocessed"][:]
39+
seg_pp = _load_n5(seg_path, "segmentation_postprocessed", lazy)
3540
else:
3641
seg_pp = None
3742

@@ -54,9 +59,13 @@ def main():
5459
parser.add_argument("-o", "--output_folder", required=True)
5560
parser.add_argument("-k", "--input_key", default=None)
5661
parser.add_argument("-c", "--check_downsampled", action="store_true")
62+
parser.add_argument("--lazy", action="store_true")
5763

5864
args = parser.parse_args()
59-
check_prediction(args.input, args.output_folder, check_downsampled=args.check_downsampled, input_key=args.input_key)
65+
check_prediction(
66+
args.input, args.output_folder, check_downsampled=args.check_downsampled,
67+
input_key=args.input_key, lazy=args.lazy
68+
)
6069

6170

6271
if __name__ == "__main__":

0 commit comments

Comments
 (0)