def test_y_true_equals_y_pred(self): y_true, _ = _sample1(10, 10, 30, 30, True) y_pred = y_true.copy() # Test basic initialization o = metrics.ObjectMetrics(y_true, y_pred, force_event_links=True) # Check that object numbers are integers assert isinstance(o.n_true, int) assert isinstance(o.n_pred, int) assert o.n_true == o.n_pred # metrics should be perfect since y_true == y_pred assert o.recall == 1 assert o.precision == 1 assert o.f1 == 1 assert o.jaccard == 1 assert o.dice == 1 # test other properties assert o.correct_detections == o.n_pred assert o.missed_detections == 0 assert o.gained_detections == 0 assert o.splits == 0 assert o.merges == 0 assert o.catastrophes == 0 assert o.gained_det_from_split == 0 assert o.missed_det_from_merge == 0 assert o.true_det_in_catastrophe == 0 assert o.pred_det_in_catastrophe == 0 assert o.missed_props == [] assert o.merge_props == [] assert o.split_props == [] assert o.gained_props == []
def test_calc_iou(self): # TODO: test correctness y_true, y_pred = _sample1(10, 10, 30, 30, True) o = metrics.ObjectMetrics(y_true, y_pred) # Check that it is not equal to initial value assert np.count_nonzero(o.iou) != 0 assert np.count_nonzero(o.seg_thresh) != 0 # check that image without any background passes y_true, y_pred = _dense_sample() o = metrics.ObjectMetrics(y_true=y_true, y_pred=y_pred) # Check that it is not equal to initial value assert np.count_nonzero(o.iou) != 0 assert np.count_nonzero(o.seg_thresh) != 0
def test_calc_iou_3D(self): # TODO: test correctness y_true, y_pred = _sample1_3D(10, 10, 30, 30, True, 8) o = metrics.ObjectMetrics(y_true, y_pred, is_3d=True) # Check that it is not equal to initial value assert np.count_nonzero(o.iou) != 0 assert np.count_nonzero(o.seg_thresh) != 0
def test_init(self): y_true, _ = _sample1(10, 10, 30, 30, True) # Test basic initialization o = metrics.ObjectMetrics(y_true, y_true) # Test __repr__ print(o) # Test using float dtype warns but still works o = metrics.ObjectMetrics( y_true.astype('float'), y_true.astype('float')) # test errors thrown for improper ndim inputs y_true = np.zeros(shape=(10)) # too few dimensions with pytest.raises(ValueError): metrics.ObjectMetrics(y_true, y_true) y_true = np.zeros(shape=(10, 5, 5, 5)) # too many dimensions with pytest.raises(ValueError): metrics.ObjectMetrics(y_true, y_true) # test errors thrown for improper ndim inputs with 3d data y_true = np.zeros(shape=(10, 15)) # too few dimensions with pytest.raises(ValueError): metrics.ObjectMetrics(y_true, y_true, is_3d=True) y_true = np.zeros(shape=(10, 15, 15, 10)) # too many dimensions with pytest.raises(ValueError): metrics.ObjectMetrics(y_true, y_true, is_3d=True) # Test mismatched input size with pytest.raises(ValueError): metrics.ObjectMetrics(y_true, y_true[0])
def test_multi_split_error(self): # 1 cell split into 3 # forced event links to ensure accurate assignment y_true, y_pred = _sample2_3(10, 10, 30, 30, merge=False, similar_size=False) o = metrics.ObjectMetrics(y_true, y_pred, force_event_links=True) assert o.splits == 1 assert o.gained_det_from_split == 2 assert o.split_props != []
def test_multi_merge_error(self): # 3 cells merged together # forced event links to ensure accurate assignment y_true, y_pred = _sample2_3(10, 10, 30, 30, merge=True, similar_size=False) o = metrics.ObjectMetrics(y_true, y_pred, force_event_links=True) assert o.merges == 1 assert o.missed_det_from_merge == 2 assert o.merge_props != []
def test__get_props(self): y_true, y_pred = _sample1(10, 10, 30, 30, True) o = metrics.ObjectMetrics(y_true, y_true) props = o._get_props('correct') # assert props != [] # Test _get_props with invalid detection type with pytest.raises(ValueError): o._get_props('invalid_type')
def test_y_true_empty(self): y_pred, _ = _sample1(10, 10, 30, 30, True) # Test basic initialization with empty array y_pred o = metrics.ObjectMetrics(np.zeros_like(y_pred), y_pred, force_event_links=True) assert o.n_true == 0 assert o.correct_detections == 0 assert o.gained_detections == o.n_pred assert o.recall == 0 assert o.precision == 0 assert o.f1 == 0 assert o.jaccard == 0 assert o.gained_det_from_split == 0 assert o.missed_det_from_merge == 0 assert o.true_det_in_catastrophe == 0 assert o.pred_det_in_catastrophe == 0
def test_split_error(self): y_true, y_pred = _sample1(10, 10, 30, 30, False) o = metrics.ObjectMetrics(y_true, y_pred) assert o.splits == 1 assert o.gained_det_from_split == 1 assert o.split_props != []
def test_merge_error(self): y_true, y_pred = _sample1(10, 10, 30, 30, True) o = metrics.ObjectMetrics(y_true, y_pred) assert o.merges == 1 assert o.missed_det_from_merge == 1 assert o.merge_props != []