Skip to content

Commit

Permalink
fix bug in computing LCWA; add comments for readability
Browse files Browse the repository at this point in the history
  • Loading branch information
naga-karthik committed Jun 7, 2024
1 parent f4b1f68 commit 91edc16
Showing 1 changed file with 8 additions and 10 deletions.
18 changes: 8 additions & 10 deletions MetricsReloaded/metrics/pairwise_measures.py
Original file line number Diff line number Diff line change
Expand Up @@ -1364,22 +1364,20 @@ def lesion_count_weighted_by_assignment(self):
pred_lesion, num_pred_lesions = ndimage.label(prediction[idx_sample, ...])
truth_lesion, num_truth_lesions = ndimage.label(truth[idx_sample, ...])

# reshape for use with sklearn precision
pred_reshape = np.reshape(prediction[idx_sample, ...], (np.prod(pred_shape[1:])))
truth_reshape = np.reshape(truth[idx_sample, ...], (np.prod(truth_shape[1:])))

# pre-allocate cost matrix
cost_matrix = np.zeros((num_pred_lesions, num_truth_lesions))

# compute cost matrix
for idx_pred in range(num_pred_lesions):
pred_lesion = pred_reshape == idx_pred
# NOTE: 0 is the background class so we start from 1
for idx_pred in range(1, num_pred_lesions+1):
pred = (pred_lesion == idx_pred).reshape(-1)

for idx_truth in range(num_truth_lesions):
truth_lesion = truth_reshape == idx_truth
for idx_truth in range(1, num_truth_lesions+1):
truth = (truth_lesion == idx_truth).reshape(-1)

# use precision scores as edge weights in the bipartite graph
cost_matrix[idx_pred, idx_truth] = precision_score(truth_lesion, pred_lesion)
# compute precision scores to use as edge weights in the bipartite graph
# NOTE: sklearn's precision requires 1D arrays as input
cost_matrix[idx_pred-1, idx_truth-1] = precision_score(y_true=truth, y_pred=pred)

# compute the optimal assignment
row_ind, col_ind = optimize.linear_sum_assignment(cost_matrix=cost_matrix, maximize=True)
Expand Down

0 comments on commit 91edc16

Please sign in to comment.