bpdl.metric_similarity module¶
Introducing some used similarity measures fro atlases and etc.
Copyright (C) 2015-2020 Jiri Borovec <jiri.borovec@fel.cvut.cz>
-
bpdl.metric_similarity.
compare_atlas_adjusted_rand
(a1, a2)[source]¶ using adjusted rand and transform original values from (-1, 1) to (0, 1) http://scikit-learn.org/stable/modules/generated/sklearn.metrics.adjusted_rand_score.html
- Parameters
a1 – np.array<height, width>
a2 – np.array<height, width>
- Return float
with 0 means no difference
>>> atlas1 = np.zeros((7, 15), dtype=int) >>> atlas1[1:4, 5:10] = 1 >>> atlas1[5:7, 6:13] = 2 >>> atlas2 = np.zeros((7, 15), dtype=int) >>> atlas2[2:5, 7:12] = 1 >>> atlas2[4:7, 7:14] = 2 >>> compare_atlas_adjusted_rand(atlas1, atlas1) 0.0 >>> compare_atlas_adjusted_rand(atlas1, atlas2) 0.656...
-
bpdl.metric_similarity.
compare_atlas_rnd_pairs
(a1, a2, rand_seed=None)[source]¶ compare two atlases as taking random pixels pairs from both and evaluate that the are labeled equally of differently
- Parameters
a1 – np.array<height, width>
a2 – np.array<height, width>
rand_seed – random initialization
- Return float
with 0 means no difference
>>> atlas1 = np.zeros((7, 15), dtype=int) >>> atlas1[1:4, 5:10] = 1 >>> atlas1[5:7, 6:13] = 2 >>> atlas2 = np.zeros((7, 15), dtype=int) >>> atlas2[2:5, 7:12] = 1 >>> atlas2[4:7, 7:14] = 2 >>> compare_atlas_rnd_pairs(atlas1, atlas1) 0.0 >>> round(compare_atlas_rnd_pairs(atlas1, atlas2, rand_seed=0), 5) 0.37143
-
bpdl.metric_similarity.
compare_matrices
(m1, m2)[source]¶ sum all element differences and divide it by number of elements
- Parameters
m1 – np.array<height, width>
m2 – np.array<height, width>
- Return float
>>> seg1 = np.zeros((7, 15), dtype=int) >>> seg1[1:4, 5:10] = 3 >>> seg1[5:7, 6:13] = 2 >>> seg2 = np.zeros((7, 15), dtype=int) >>> seg2[2:5, 7:12] = 1 >>> seg2[4:7, 7:14] = 3 >>> compare_matrices(seg1, seg1) 0.0 >>> compare_matrices(seg1, seg2) 0.819...
-
bpdl.metric_similarity.
compare_weights
(c1, c2)[source]¶ - Parameters
c1 – np.array<height, width>
c2 – np.array<height, width>
- Return float
>>> np.random.seed(0) >>> compare_weights(np.random.randint(0, 2, (10, 5)), ... np.random.randint(0, 2, (10, 5))) 0.44
-
bpdl.metric_similarity.
compute_classif_metrics
(y_true, y_pred, metric_averages=('macro', 'weighted'))[source]¶ compute standard metrics for multi-class classification
- Parameters
- Return {str
float}:
>>> y_true = np.array([0] * 3 + [1] * 5) >>> y_pred = np.array([0] * 5 + [1] * 3) >>> dist_sm = compute_classif_metrics(y_true, y_pred) >>> pair_sm = [(n, dist_sm[n]) for n in sorted(dist_sm.keys())] >>> pair_sm [('ARS', 0.138...), ('accuracy', 0.75), ('confusion', [[3, 0], [2, 3]]), ('f1_macro', 0.800...), ('f1_weighted', 0.849...), ('precision_macro', 0.800...), ('precision_weighted', 0.75), ('recall_macro', 0.749...), ('recall_weighted', 0.749...), ('support_macro', None), ('support_weighted', None)] >>> y_true = np.array([0] * 5 + [1] * 5 + [2] * 5) >>> y_pred = np.array([0] * 5 + [1] * 3 + [2] * 7) >>> dist_sm = compute_classif_metrics(y_true, y_pred) >>> pair_sm = [(n, dist_sm[n]) for n in sorted(dist_sm.keys())] >>> pair_sm [('ARS', 0.641...), ('accuracy', 0.866...), ('confusion', [[5, 0, 0], [0, 3, 2], [0, 0, 5]]), ('f1_macro', 0.904...), ('f1_weighted', 0.904...), ('precision_macro', 0.866...), ('precision_weighted', 0.866...), ('recall_macro', 0.861...), ('recall_weighted', 0.861...), ('support_macro', None), ('support_weighted', None)]
-
bpdl.metric_similarity.
compute_labels_overlap_matrix
(seg1, seg2)[source]¶ compute overlap between tho segmentation atlasess) with same sizes
- Parameters
seg1 – np.array<height, width>
seg2 – np.array<height, width>
- Return ndarray
np.array<height, width>
>>> seg1 = np.zeros((7, 15), dtype=int) >>> seg1[1:4, 5:10] = 3 >>> seg1[5:7, 6:13] = 2 >>> seg2 = np.zeros((7, 15), dtype=int) >>> seg2[2:5, 7:12] = 1 >>> seg2[4:7, 7:14] = 3 >>> compute_labels_overlap_matrix(seg1, seg1) array([[76, 0, 0, 0], [ 0, 0, 0, 0], [ 0, 0, 14, 0], [ 0, 0, 0, 15]]) >>> compute_labels_overlap_matrix(seg1, seg2) array([[63, 4, 0, 9], [ 0, 0, 0, 0], [ 2, 0, 0, 12], [ 9, 6, 0, 0]])
-
bpdl.metric_similarity.
relabel_max_overlap_merge
(seg_ref, seg_relabel, keep_bg=True)[source]¶ relabel the second segmentation cu that maximise relative overlap for each pattern (object), if one pattern in reference atlas is likely composed from multiple patterns in relabel atlas, it merge them NOTE: it skips background class - 0
- Parameters
seg_ref (ndarray) – segmentation
seg_relabel (ndarray) – segmentation
keep_bg (bool) –
- Return ndarray
>>> atlas1 = np.zeros((7, 15), dtype=int) >>> atlas1[1:4, 5:10] = 1 >>> atlas1[5:7, 3:13] = 2 >>> atlas2 = np.zeros((7, 15), dtype=int) >>> atlas2[0:3, 7:12] = 1 >>> atlas2[3:7, 1:7] = 2 >>> atlas2[4:7, 7:14] = 3 >>> atlas2[:2, :3] = 5 >>> relabel_max_overlap_merge(atlas1, atlas2, keep_bg=True) array([[1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0], [1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0], [0, 2, 2, 2, 2, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0], [0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0], [0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0], [0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0]]) >>> relabel_max_overlap_merge(atlas2, atlas1, keep_bg=True) array([[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0, 0], [0, 0, 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0, 0]]) >>> relabel_max_overlap_merge(atlas1, atlas2, keep_bg=False) array([[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 2, 2, 2, 2, 2, 2, 2, 0], [0, 0, 0, 0, 0, 0, 0, 2, 2, 2, 2, 2, 2, 2, 0], [0, 0, 0, 0, 0, 0, 0, 2, 2, 2, 2, 2, 2, 2, 0]])
-
bpdl.metric_similarity.
relabel_max_overlap_unique
(seg_ref, seg_relabel, keep_bg=True)[source]¶ relabel the second segmentation cu that maximise relative overlap for each pattern (object), the relation among patterns is 1-1 NOTE: it skips background class - 0
- Parameters
seg_ref (ndarray) – segmentation
seg_relabel (ndarray) – segmentation
keep_bg (bool) –
- Return ndarray
>>> atlas1 = np.zeros((7, 15), dtype=int) >>> atlas1[1:4, 5:10] = 1 >>> atlas1[5:7, 3:13] = 2 >>> atlas2 = np.zeros((7, 15), dtype=int) >>> atlas2[0:3, 7:12] = 1 >>> atlas2[3:7, 1:7] = 2 >>> atlas2[4:7, 7:14] = 3 >>> atlas2[:2, :3] = 5 >>> relabel_max_overlap_unique(atlas1, atlas2, keep_bg=True) array([[5, 5, 5, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0], [5, 5, 5, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0], [0, 3, 3, 3, 3, 3, 3, 0, 0, 0, 0, 0, 0, 0, 0], [0, 3, 3, 3, 3, 3, 3, 2, 2, 2, 2, 2, 2, 2, 0], [0, 3, 3, 3, 3, 3, 3, 2, 2, 2, 2, 2, 2, 2, 0], [0, 3, 3, 3, 3, 3, 3, 2, 2, 2, 2, 2, 2, 2, 0]]) >>> relabel_max_overlap_unique(atlas2, atlas1, keep_bg=True) array([[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 0, 0], [0, 0, 0, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 0, 0]]) >>> relabel_max_overlap_unique(atlas1, atlas2, keep_bg=False) array([[5, 5, 5, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0], [5, 5, 5, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0], [0, 3, 3, 3, 3, 3, 3, 0, 0, 0, 0, 0, 0, 0, 0], [0, 3, 3, 3, 3, 3, 3, 2, 2, 2, 2, 2, 2, 2, 0], [0, 3, 3, 3, 3, 3, 3, 2, 2, 2, 2, 2, 2, 2, 0], [0, 3, 3, 3, 3, 3, 3, 2, 2, 2, 2, 2, 2, 2, 0]])