def test_ri_random_data(self): from elf.evaluation import rand_index shape = (256, 256) x = np.random.randint(0, 100, size=shape) y = np.random.randint(0, 100, size=shape) ari, ri = rand_index(x, y, ignore_gt=[0]) ari_exp = adapted_rand_ref(x, y)[0] self.assertAlmostEqual(ari, ari_exp, places=2)
def test_ri_random_data(self): print("Blob") from elf.evaluation import rand_index shape = (256, 256) x = np.random.randint(0, 100, size=shape) y = np.random.randint(0, 100, size=shape) ari, ri = rand_index(x, y, ignore_gt=[0]) ari_exp = adapted_rand(x, gy) self.assertAlmostEqual(ari, ari_exp)
def test_components_nifty_toy(self): uv_ids, node_labels, expected_cc = self.toy_problem() g = nifty.graph.undirectedGraph(int(uv_ids.max()) + 1) g.insertEdges(uv_ids) self.assertEqual(g.numberOfNodes, uv_ids.max() + 1) self.assertEqual(g.numberOfEdges, len(uv_ids)) result = nifty.graph.connectedComponentsFromNodeLabels(g, node_labels) self.assertEqual(len(result), len(expected_cc)) ri, _ = rand_index(result, expected_cc) self.assertAlmostEqual(ri, 0.)
def test_label_with_background(self): from elf.parallel import label from elf.evaluation import rand_index shape = 3 * (32, ) block_shape = 3 * (16, ) x = np.random.randint(0, 20, size=shape).astype('uint32') res = np.zeros_like(x) res = label(x, res, block_shape=block_shape, with_background=True) exp = label_reference(x) # there is a tiny differnce in ri, maybe some border artifacts? _, ri = rand_index(res, exp) self.assertAlmostEqual(ri, 1., places=3)
def test_ri(self): from elf.evaluation import rand_index f = z5py.File(self.path) ds_gt = f[self.gt_key] ds_gt.n_threads = 4 gt = ds_gt[self.bb] ds_seg = f[self.seg_key] ds_seg.n_threads = 4 seg = ds_seg[self.bb] ari, ri = rand_index(seg, gt, ignore_gt=[0]) ari_exp = adapted_rand(seg, gt) self.assertAlmostEqual(ari, ari_exp)
def _check_result(self, mode, check_for_equality=True, threshold=.5): with z5py.File(self.output_path) as f: res = f[self.output_key][:] with z5py.File(self.input_path) as f: inp = f[self.input_key][:] if mode == 'greater': expected = label(inp > threshold) elif mode == 'less': expected = label(inp < threshold) elif mode == 'equal': expected = label(inp == threshold) self.assertEqual(res.shape, expected.shape) if check_for_equality: score = rand_index(res, expected)[0] self.assertAlmostEqual(score, 0., places=4)
def test_graph_connected_components(self): from cluster_tools.postprocess import ConnectedComponentsWorkflow task = ConnectedComponentsWorkflow self.compute_graph(ignore_label=False) # check the graph again g = self.compute_nifty_graph() g1 = ndist.Graph(self.output_path, self.graph_key) self.assertEqual(g.numberOfNodes, g1.numberOfNodes) self.assertEqual(g.numberOfEdges, g1.numberOfEdges) self.assertTrue(np.allclose(g.uvIds(), g1.uvIds())) assignment_key = 'initial_assignments' assignments = self.make_assignments(g, self.output_path, assignment_key) # compute expected components expected = nifty.graph.connectedComponentsFromNodeLabels( g, assignments) vigra.analysis.relabelConsecutive(expected, out=expected) out_key = 'connected_components' t = task(tmp_folder=self.tmp_folder, config_dir=self.config_folder, target=self.target, max_jobs=self.max_jobs, problem_path=self.output_path, graph_key=self.graph_key, assignment_path=self.output_path, assignment_key=assignment_key, output_path=self.output_path, assignment_out_key=out_key) ret = luigi.build([t], local_scheduler=True) self.assertTrue(ret) # load the output components with z5py.File(self.output_path) as f: results = f[out_key][:] # compare self.assertEqual(results.shape, expected.shape) ri, _ = rand_index(results, expected) self.assertAlmostEqual(ri, 0.)
def test_components_dist_toy(self): uv_ids, node_labels, expected_cc = self.toy_problem() graph_path = os.path.join(self.tmp_folder, 'graph.n5') with z5py.File(graph_path) as f: g = f.create_group('graph') g.attrs['numberOfEdges'] = len(uv_ids) g.create_dataset('edges', data=uv_ids, chunks=(int(1e5), 2), compression='raw') g = ndist.Graph(graph_path, 'graph') self.assertEqual(g.numberOfNodes, uv_ids.max() + 1) self.assertEqual(g.numberOfEdges, len(uv_ids)) result = ndist.connectedComponentsFromNodes(g, node_labels, True) self.assertEqual(len(result), len(expected_cc)) ri, _ = rand_index(result, expected_cc) self.assertAlmostEqual(ri, 0.)