def web_length_metric(gold_swc, test_swc, method, rad_threshold, len_threshold): gold_tree = SwcTree() test_tree = SwcTree() gold_tree.load_list(adjust_swcfile(gold_swc)) test_tree.load_list(adjust_swcfile(test_swc)) config = { 'method': method, 'len_threshold': len_threshold, 'rad_threshold': rad_threshold } recall, precision, vertical_tree = length_metric(gold_swc_tree=gold_tree, test_swc_tree=test_tree, abs_dir="", config=config) # gold_tree.radius_limit(10) # test_tree.radius_limit(10) result = { 'recall': recall, 'precision': precision, 'gold_swc': swc_to_list(gold_tree), 'test_swc': swc_to_list(test_tree), 'vertical_swc': vertical_tree } return result
def test_len_1(self): goldtree = SwcTree() testTree = SwcTree() goldtree.load( "D:\gitProject\mine\PyNeval\\test\data_example\gold\\2_18_gold.swc" ) testTree.load( "D:\gitProject\mine\PyNeval\\test\data_example\\test\\2_18_test.swc" ) recall1, precision1, vertical_tree = length_metric( gold_swc_tree=goldtree, test_swc_tree=testTree, abs_dir="D:\gitProject\mine\PyNeval", config=read_json( "D:\gitProject\mine\PyNeval\config\length_metric.json")) recall2, precision2, vertical_tree = length_metric( gold_swc_tree=testTree, test_swc_tree=goldtree, abs_dir="D:\gitProject\mine\PyNeval", config=read_json( "D:\gitProject\mine\PyNeval\config\length_metric.json")) self.assertEqual(recall1, 0.7030284661586385) self.assertEqual(recall2, 0.8685749970409651) self.assertEqual(precision1, 0.7174338664651955) self.assertEqual(precision2, 0.8511348243456205)
def test_len_2(self): goldtree = SwcTree() testTree = SwcTree() goldtree.load( "D:\gitProject\mine\PyNeval\\test\data_example\gold\\30_18_10_gold.swc" ) testTree.load( "D:\gitProject\mine\PyNeval\\test\data_example\\test\\30_18_10_test.swc" ) recall1, precision1, vertical_tree = length_metric( gold_swc_tree=goldtree, test_swc_tree=testTree, abs_dir="D:\gitProject\mine\PyNeval", config=read_json( "D:\gitProject\mine\PyNeval\config\length_metric.json")) recall2, precision2, vertical_tree = length_metric( gold_swc_tree=testTree, test_swc_tree=goldtree, abs_dir="D:\gitProject\mine\PyNeval", config=read_json( "D:\gitProject\mine\PyNeval\config\length_metric.json")) self.assertEqual(recall1, 0.6818040837428125) self.assertEqual(recall2, 0.6971179817818407) self.assertEqual(precision1, 0.6327624077188985) self.assertEqual(precision2, 0.7511474781551107)
def test_len_4(self): goldtree = SwcTree() testTree = SwcTree() goldtree.load( "D:\gitProject\mine\PyNeval\\test\data_example\gold\\conner.swc") testTree.load( "D:\gitProject\mine\PyNeval\\test\data_example\\test\\conner.swc") recall1, precision1, vertical_tree = length_metric( gold_swc_tree=goldtree, test_swc_tree=testTree, abs_dir="D:\gitProject\mine\PyNeval", config=read_json( "D:\gitProject\mine\PyNeval\config\length_metric.json")) recall2, precision2, vertical_tree = length_metric( gold_swc_tree=testTree, test_swc_tree=goldtree, abs_dir="D:\gitProject\mine\PyNeval", config=read_json( "D:\gitProject\mine\PyNeval\config\length_metric.json")) self.assertEqual(recall1, 1.0) self.assertEqual(recall2, 0.0) self.assertEqual(precision1, 0.9385873563259137) self.assertEqual(precision2, 0.0)
def test_len_3(self): goldtree = SwcTree() testTree = SwcTree() goldtree.load( "D:\gitProject\mine\PyNeval\\test\data_example\gold\\34_23_10_gold.swc" ) testTree.load( "D:\gitProject\mine\PyNeval\\test\data_example\\test\\34_23_10_test.swc" ) recall1, precision1, vertical_tree = length_metric( gold_swc_tree=goldtree, test_swc_tree=testTree, abs_dir="D:\gitProject\mine\PyNeval", config=read_json( "D:\gitProject\mine\PyNeval\config\length_metric.json")) recall2, precision2, vertical_tree = length_metric( gold_swc_tree=testTree, test_swc_tree=goldtree, abs_dir="D:\gitProject\mine\PyNeval", config=read_json( "D:\gitProject\mine\PyNeval\config\length_metric.json")) self.assertEqual(recall1, 0.6092362772577844) self.assertEqual(recall2, 0.5585330586568052) self.assertEqual(precision1, 0.5318779478680355) self.assertEqual(precision2, 0.6397682076225172)
def test_5(self): testTree = SwcTree() goldTree = SwcTree() goldTree.load("D:\gitProject\mine\PyNeval\\test\data_example\gold\diadem\diadem7.swc") testTree.load("D:\gitProject\mine\PyNeval\\test\data_example\\test\diadem\diadem7.swc") get_default_threshold(goldTree) score = diadem_metric(swc_test_tree=testTree, swc_gold_tree=goldTree, config=read_json("D:\gitProject\mine\PyNeval\config\diadem_metric.json")) print(score) self.assertEqual(score, 0.5)
def test_3(self): testTree = SwcTree() goldTree = SwcTree() goldTree.load("D:\gitProject\mine\PyNeval\\test\data_example\gold\ExampleGoldStandard.swc") testTree.load("D:\gitProject\mine\PyNeval\\test\data_example\\test\ExampleTest.swc") # goldtree.load("D:\gitProject\mine\PyNeval\\test\data_example\\gold\\ExampleGoldStandard.swc") # testTree.load("D:\gitProject\mine\PyNeval\\test\data_example\\test\\ExampleTest.swc") get_default_threshold(goldTree) score = diadem_metric(swc_test_tree=testTree, swc_gold_tree=goldTree, config=read_json("D:\gitProject\mine\PyNeval\config\diadem_metric.json")) print(score) self.assertEqual(score, 0.9564541213063764)
def test2(): start = time.time() swc_tree = SwcTree() swc_tree.load("D:\gitProject\mine\PyNeval\\test\data_example\gold\ExampleGoldStandard.swc") bin_tree = convert_to_binarytree(swc_tree) test_print_bin_tree(bin_tree) print(time.time() - start)
class TestGetLcaLength(unittest.TestCase): test_swc_tree = SwcTree() test_rtree = None id_edge_dict = None def init(self): self.test_swc_tree.load( "../../data_example/unit_test/get_nearby_edges_tree1.swc") self.test_rtree = get_edge_rtree(self.test_swc_tree) self.id_edge_dict = get_idedge_dict(self.test_swc_tree) self.test_swc_tree.get_lca_preprocess() def test_1(self): self.init() line_tuple_a = tuple([ self.test_swc_tree.node_from_id(8), self.test_swc_tree.node_from_id(7) ]) line_tuple_b = tuple([ self.test_swc_tree.node_from_id(7), self.test_swc_tree.node_from_id(6) ]) e_node1 = EuclideanPoint(center=[1.03, -2.1, 6.31]) e_node2 = EuclideanPoint(center=[3.87, 0.98, 1.17]) test_length = get_lca_length(self.test_swc_tree, \ line_tuple_a, \ line_tuple_b, \ Line(e_node_1=e_node1, e_node_2=e_node2)) self.assertEqual(test_length, 9.589854552695694) def test_2(self): self.init() line_tuple_a = tuple([ self.test_swc_tree.node_from_id(4), self.test_swc_tree.node_from_id(3) ]) line_tuple_b = tuple([ self.test_swc_tree.node_from_id(15), self.test_swc_tree.node_from_id(9) ]) e_node1 = EuclideanPoint(center=[-1.89657, 6.51822, -1.40403]) e_node2 = EuclideanPoint(center=[-2.02446, 0.54277, 7.48183]) test_length = get_lca_length(self.test_swc_tree, \ line_tuple_a, \ line_tuple_b, \ Line(e_node_1=e_node1, e_node_2=e_node2)) self.assertEqual(test_length, 34.372721303735716)
def read_swc_trees(swc_file_paths): swc_tree_list = [] if os.path.isfile(swc_file_paths): if not (swc_file_paths[-4:] == ".swc" or swc_file_paths[-4:] == ".SWC"): print(swc_file_paths + "is not a tif file") return None swc_tree = SwcTree() swc_tree.load(swc_file_paths) swc_tree_list.append(swc_tree) elif os.path.isdir(swc_file_paths): for file in os.listdir(swc_file_paths): swc_tree_list += read_swc_trees(swc_file_paths=os.path.join(swc_file_paths, file)) return swc_tree_list
def web_diadem_metric(gold_swc, test_swc, config): gold_tree = SwcTree() test_tree = SwcTree() gold_tree.load_list(adjust_swcfile(gold_swc)) test_tree.load_list(adjust_swcfile(test_swc)) diadem_metric(swc_gold_tree=gold_tree, swc_test_tree=test_tree, config=config) # gold_tree.radius_limit(10) # test_tree.radius_limit(10) print(swc_to_list(test_tree)) result = { 'gold_swc': swc_to_list(gold_tree), 'test_swc': swc_to_list(test_tree), 'weight_sum': g_weight_sum, 'score_sum': g_score_sum, 'final_score': g_final_score } return result
def test_rtree(self): swctree = SwcTree() swctree.load( "D:\gitProject\mine\PyNeval\\test\data_example\\test\\30_18_10_test.swc" ) rtree = get_edge_rtree(swctree) id_edge_dict = get_idedge_dict(swctree) point = SwcNode(center=[22.5822, 172.856, 300.413]) (line_tuple, dis) = get_nearby_edges(rtree, point, id_edge_dict, point.radius())[0] print( point.distance( Line(coords=[[15.3249, 130.821, 327.012], [19.8495, 132.384, 323.395]]))) print("dis = {}, line:{}, {}".format(dis, line_tuple[0]._pos, line_tuple[1]._pos))
def reconstruct_tree(swc_tree, is_activate, down_pa): new_swc_tree = SwcTree() node_list = [node for node in PreOrderIter(swc_tree.root())] id_node_map = {-1: new_swc_tree.root()} for node in node_list: if node.is_virtual(): continue if is_activate[node.get_id()]: tmp_node = SwcNode() tmp_node._id = node.get_id() tmp_node._type = node._type tmp_node._pos = copy.copy(node._pos) tmp_node._radius = node._radius pa = id_node_map[down_pa[node].get_id()] tmp_node.parent = pa id_node_map[node.get_id()] = tmp_node return new_swc_tree
class TestGetLcaLength(unittest.TestCase): test_swc_tree = SwcTree() test_rtree = None id_edge_dict = None def init(self): self.test_swc_tree.load( "../../data_example/unit_test/get_nearby_edges_tree1.swc") self.test_rtree = get_edge_rtree(self.test_swc_tree) self.id_edge_dict = get_idedge_dict(self.test_swc_tree) self.test_swc_tree.get_lca_preprocess() def test_1(self): line_tuple_a = tuple([ self.test_swc_tree.node_from_id(8), self.test_swc_tree.node_from_id(7) ]) line_tuple_b = tuple([ self.test_swc_tree.node_from_id(7), self.test_swc_tree.node_from_id(6) ]) e_node1 = EuclideanPoint(center=[1.03, -2.1, 6.31]) e_node2 = EuclideanPoint(center=[3.87, 0.98, 1.17])
get_self_match_edges_e_fast(swc_tree=new_swc_tree, rad_threshold=dis_threshold, len_threshold=length_threshold, list_size=swc_tree.node_count(), mode="not_self", DEBUG=False) color_origin_tree(new_swc_tree, swc_tree) delete_overlap_node(new_swc_tree) swc_save(new_swc_tree, out_path) if __name__ == '__main__': file_path = "D:\gitProject\mine\PyNeval\\test\data_example\gold\swc_data_2" out_path = "D:\gitProject\mine\PyNeval\output\swc_data_2" files = os.listdir(file_path) for file in files: tree = SwcTree() tree.clear() tree.load(os.path.join(file_path, file)) config = read_json( "D:\gitProject\mine\PyNeval\config\overlap_detect.json") overlap_clean(tree, os.path.join(out_path, "clean_" + file), config) # node = SwcNode(center=[101, 100.1, 100], radius=0.6) # son = SwcNode(center=[100, 100, 100], radius=0.5) # pa = SwcNode(center=[102, 100, 100], radius=0.5) # print(itp_ok(node=node, son=son, pa=pa))
if gold_node.distance(get_kdtree_data(node)) > dis_threshold: gold_node_knn.remove(node) if DEBUG: print("knn of gold = {}".format(gold_node_knn)) for son in gold_node.children: son_node_knn = test_kdtree.search_knn(son._pos, knn) if DEBUG: print("son of gold = {}".format(son_node_knn)) match = check_match(gold_node_knn, son_node_knn, edge_set, id_center_dict) if match is not None: match_edge[tuple([gold_node, son])] = match if DEBUG: print(len(match_edge)) return match_edge # for diadem metric # def find if __name__ == "__main__": goldtree = SwcTree() goldtree.load( "D:\gitProject\mine\PyNeval\\test\data_example\gold\\ExampleGoldStandard.swc" ) get_default_threshold(goldtree) testTree = SwcTree() testTree.load( "D:\gitProject\mine\PyNeval\\test\data_example\\test\\ExampleTest.swc") get_match_edges_p(goldtree, testTree, 3, True)
def read_from_str(swc_str): swc_tree = SwcTree() swc_list = adjust_swcfile(swc_str) swc_tree.load_list(swc_list) return swc_tree
# gold_tree.radius_limit(10) # test_tree.radius_limit(10) print(swc_to_list(test_tree)) result = { 'gold_swc': swc_to_list(gold_tree), 'test_swc': swc_to_list(test_tree), 'weight_sum': g_weight_sum, 'score_sum': g_score_sum, 'final_score': g_final_score } return result if __name__ == "__main__": testTree = SwcTree() goldTree = SwcTree() goldTree.load( "D:\gitProject\mine\PyNeval\\test\data_example\gold\diadem\diadem11.swc" ) testTree.load( "D:\gitProject\mine\PyNeval\\test\data_example\\test\diadem\diadem11.swc" ) # goldTree.load("D:\gitProject\mine\PyNeval\\test\data_example\\gold\\ExampleGoldStandard.swc") # testTree.load("D:\gitProject\mine\PyNeval\\test\data_example\\test\\ExampleTest.swc") get_default_threshold(goldTree) diadem_metric(swc_test_tree=testTree, swc_gold_tree=goldTree, config=read_json(
def test_get_bintree(): goldtree = SwcTree() goldtree.load("D:\gitProject\mine\PyNeval\\test\data_example\\test\\ExampleTest.swc") bintree = convert_to_binarytree(goldtree) return bintree