Skip to content

Commit

Permalink
refactor: coordinate conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
anna-grim authored Jan 13, 2025
1 parent 833c966 commit 911efde
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 15 deletions.
30 changes: 17 additions & 13 deletions src/aind_exaspim_soma_detection/utils/img_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def get_patch(img, voxel, shape, from_center=True):
"""
start, end = get_start_end(voxel, shape, from_center=from_center)
return img[0, 0, start[2]: end[2], start[1]: end[1], start[0]: end[0]]
return img[0, 0, start[0]: end[0], start[1]: end[1], start[2]: end[2]]


def sliding_window_coords_3d(img, window_shape, overlap):
Expand All @@ -85,18 +85,18 @@ def sliding_window_coords_3d(img, window_shape, overlap):
"""
# Calculate stride based on the overlap and window size
stride = tuple(w - o for w, o in zip(window_shape, overlap))
z_stride, y_stride, x_stride = stride
i_stride, j_stride, k_stride = stride

# Get dimensions of the window
_, _, z_dim, y_dim, x_dim = img.shape
z_win, y_win, x_win = window_shape
_, _, i_dim, j_dim, k_dim = img.shape
i_win, j_win, k_win = window_shape

# Loop over the with the sliding window
coords = []
for x in range(0, x_dim - x_win + 1, x_stride):
for y in range(0, y_dim - y_win + 1, y_stride):
for z in range(0, z_dim - z_win + 1, z_stride):
coords.append((x, y, z))
for i in range(0, i_dim - i_win + 1, i_stride):
for j in range(0, j_dim - j_win + 1, j_stride):
for k in range(0, k_dim - k_win + 1, k_stride):
coords.append((i, j, k))
return coords


Expand Down Expand Up @@ -131,22 +131,26 @@ def get_start_end(voxel, shape, from_center=True):


# --- Coordinate Conversions ---
def to_physical(voxel):
def to_physical(voxel, multiscale):
"""
Converts the given coordinate from voxels to physical space.
Parameters
----------
voxel : ArrayLike
Voxel oordinate to be converted.
Voxel coordinate to be converted.
multiscale
Level in the image pyramid that the voxel coordinate must index into.
Returns
-------
Tuple[int]
Physical coordinate of "voxel".
"""
return tuple([voxel[i] * ANISOTROPY[i] for i in range(3)])
voxel = voxel[::-1]
return tuple([voxel[i] * ANISOTROPY[i] * 2**multiscale for i in range(3)])


def to_voxels(xyz, multiscale):
Expand All @@ -168,7 +172,7 @@ def to_voxels(xyz, multiscale):
"""
scaling_factor = 1.0 / 2**multiscale
voxel = scaling_factor * (xyz / np.array(ANISOTROPY))
return np.round(voxel).astype(int)
return np.round(voxel).astype(int)[::-1]


def local_to_physical(local_voxel, offset, multiscale):
Expand All @@ -193,7 +197,7 @@ def local_to_physical(local_voxel, offset, multiscale):
"""
global_voxel = np.array([v + o for v, o in zip(local_voxel, offset)])
return to_physical(global_voxel * 2**multiscale)
return to_physical(global_voxel, multiscale)


# --- Visualizations ---
Expand Down
6 changes: 4 additions & 2 deletions src/aind_exaspim_soma_detection/utils/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ def read_swc(path):
return [float(xyz_str[i]) + offset[i] for i in range(3)]


def write_points(output_dir, points, color=None, prefix=""):
def write_points(output_dir, points, color=None, prefix="", radius=20):
"""
Writes a list of 3D points to individual SWC files in the specified
directory.
Expand All @@ -318,6 +318,8 @@ def write_points(output_dir, points, color=None, prefix=""):
prefix : str, optional
String that is prefixed to the filenames of the SWC files. Default is
an empty string.
radius : float, optional
Radius to be used in swc file.
Returns
--------
Expand All @@ -332,7 +334,7 @@ def write_points(output_dir, points, color=None, prefix=""):
filename = prefix + str(i + 1) + ".swc"
path = os.path.join(output_dir, filename)
threads.append(
executor.submit(write_point, path, xyz, 20, color=color)
executor.submit(write_point, path, xyz, radius, color=color)
)


Expand Down

0 comments on commit 911efde

Please sign in to comment.