Skip to content

Commit

Permalink
minor upds
Browse files Browse the repository at this point in the history
  • Loading branch information
anna-grim authored Jan 9, 2025
1 parent b94803e commit ff42667
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 31 deletions.
79 changes: 49 additions & 30 deletions src/aind_exaspim_soma_detection/utils/img_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,35 +228,6 @@ def plot_mips(img, clip_bool=False):
plt.show()


def rescale(img_patch, clip_bool=True):
"""
Rescales the input image to a [0, 65535] intensity range, with optional
clipping of extreme values.
Parameters
----------
img_patch : numpy.ndarray
Image patch to be rescaled.
clip_bool : bool, optional
If True, the resulting MIP will be clipped to the range [0, 1] during
rescaling. If False, no clipping is applied. The default is False.
Returns
-------
numpy.ndarray
Rescaled image.
"""
# Clip image
if clip_bool:
img_patch = np.clip(img_patch, 0, np.percentile(img_patch, 99))

# Rescale image
img_patch -= np.min(img_patch)
img_patch = (2**16 - 1) * (img_patch / np.max(img_patch))
return img_patch.astype(np.uint16)


def get_mip(img_patch, axis=0, clip_bool=False):
"""
Computes the Maximum Intensity Projection (MIP) along a specified axis and
Expand Down Expand Up @@ -292,7 +263,7 @@ def get_detections_img(shape, voxels):
----------
shape : Tuple[int]
Shape of the output detection image.
voxels : List[Tuple[int]
voxels : List[Tuple[int]]
List of voxel coordinates to be marked as detected in the output
image.
Expand All @@ -308,3 +279,51 @@ def get_detections_img(shape, voxels):
voxel = tuple([int(v) for v in voxel])
detections_img[voxel] = 1
return detections_img


# --- Utils ---
def normalize(img_patch):
"""
Rescales the input image to a [0, 1] intensity range.
Parameters
----------
img_patch : numpy.ndarray
Image patch to be normalized.
Returns
-------
numpy.ndarray
Normalized image.
"""
img_patch -= np.min(img_patch)
return img_patch / np.max(img_patch)


def rescale(img_patch, clip_bool=True):
"""
Rescales the input image to a [0, 65535] intensity range, with optional
clipping of extreme values.
Parameters
----------
img_patch : numpy.ndarray
Image patch to be rescaled.
clip_bool : bool, optional
If True, the resulting MIP will be clipped to the range [0, 1] during
rescaling. If False, no clipping is applied. The default is False.
Returns
-------
numpy.ndarray
Rescaled image.
"""
# Clip image
if clip_bool:
img_patch = np.clip(img_patch, 0, np.percentile(img_patch, 99))

# Rescale image
img_patch = (2**16 - 1) * normalize(img_patch)
return img_patch.astype(np.uint16)
3 changes: 2 additions & 1 deletion src/aind_exaspim_soma_detection/utils/ml_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def split_train_validation(examples, train_ratio=0.85):
return train_examples, valid_examples


def report_metrics(y, hat_y):
def report_metrics(y, hat_y, threshold):
"""
Computes and prints various evaluation metrics based on the true labels
"y" and predicted labels "hat_y".
Expand All @@ -68,6 +68,7 @@ def report_metrics(y, hat_y):
None
"""
hat_y = (hat_y > threshold).astype(int)
accuracy = accuracy_score(y, hat_y)
print("Accuracy:", round(accuracy, 4))
print("Accuracy Dif:", round(accuracy - np.sum(y) / len(y), 4))
Expand Down

0 comments on commit ff42667

Please sign in to comment.