diff --git a/src/aind_exaspim_soma_detection/utils/img_util.py b/src/aind_exaspim_soma_detection/utils/img_util.py index 7416ce4..86b1393 100644 --- a/src/aind_exaspim_soma_detection/utils/img_util.py +++ b/src/aind_exaspim_soma_detection/utils/img_util.py @@ -58,8 +58,16 @@ def get_patch(img, voxel, shape, from_center=True): Patch extracted from the given image. """ + # Get image patch coordiantes start, end = get_start_end(voxel, shape, from_center=from_center) - return img[0, 0, start[0]: end[0], start[1]: end[1], start[2]: end[2]] + valid_start = any([s < 0 for s in start]) + valid_end = any([e > img.shape[i + 2] for i, e in enumerate(end)]) + + # Get image patch + if valid_start and valid_end: + return img[0, 0, start[0]: end[0], start[1]: end[1], start[2]: end[2]] + else: + return np.zeros(shape) def sliding_window_coords_3d(img, window_shape, overlap): diff --git a/src/aind_exaspim_soma_detection/utils/util.py b/src/aind_exaspim_soma_detection/utils/util.py index 793b304..92d6c74 100644 --- a/src/aind_exaspim_soma_detection/utils/util.py +++ b/src/aind_exaspim_soma_detection/utils/util.py @@ -516,3 +516,33 @@ def sample_once(my_container): """ return sample(my_container, 1)[0] + + +def time_writer(t, unit="seconds"): + """ + Converts a runtime "t" to a larger unit of time if applicable. + + Parameters + ---------- + t : float + Runtime. + unit : str, optional + Unit of time that "t" is expressed in. + + Returns + ------- + float + Runtime + str + Unit of time. + + """ + assert unit in ["seconds", "minutes", "hours"] + upd_unit = {"seconds": "minutes", "minutes": "hours"} + if t < 60 or unit == "hours": + return t, unit + else: + t /= 60 + unit = upd_unit[unit] + t, unit = time_writer(t, unit=unit) + return t, unit