-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpyneval.py
175 lines (154 loc) · 6.2 KB
/
pyneval.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import argparse
import sys,os,platform
from pyneval.io.read_swc import read_swc_trees
from pyneval.io.read_json import read_json
from pyneval.metric.diadem_metric import diadem_metric
from pyneval.metric.length_metric import length_metric
from cli.overlap_detect import overlap_clean
metric_list = [
"diadem_metric",
"overall_length",
"matched_length",
"overlap_clean",
"DM","OL","ML","OC"
]
def read_parameters():
parser = argparse.ArgumentParser(
description="pymet 1.0"
)
parser.add_argument(
"--test",
"-T",
help="the route of the test file",
required=False,
nargs='*',
)
parser.add_argument(
"--gold",
"-G",
help="the route of the gold file",
required=True
)
parser.add_argument(
"--metric",
"-M",
help="choose a metric method",
required=True
)
parser.add_argument(
"--output",
"-O",
help="the route of the output file.\nif not specified, output to screen",
required=False
)
parser.add_argument(
"--config",
"-C",
help="special config for different metric method",
required=False
)
parser.add_argument(
"--reverse",
"-R",
help="output the answer when we switch the gold and test tree",
required=False
)
return parser.parse_args()
# command program
def pyneval(DEBUG=True):
# init path parameter
abs_dir = os.path.abspath("")
sys.path.append(abs_dir)
sys.path.append(os.path.join(abs_dir, "src"))
sys.path.append(os.path.join(abs_dir, "test"))
# read parameter
args = read_parameters()
# set config
# gold/test files
if args.test is None:
test_swc_files = []
else:
test_swc_files = [os.path.join(abs_dir, path) for path in args.test]
gold_swc_file = os.path.join(abs_dir, args.gold)
# reverse
reverse = args.reverse
if reverse is None:
reverse = False
# metric
metric = args.metric
if metric not in metric_list:
raise Exception("[Error: ] Unknown metric method {}".format(
metric
))
# output path
output_dest = args.output
if output_dest is not None:
output_dest = os.path.join(abs_dir, output_dest)
# config
config = args.config
if config is None:
if platform.system() == "Windows":
if metric == "diadem_metric" or metric == "DM":
config = os.path.join(abs_dir, "config\\diadem_metric.json")
if metric in ["overall_length", "matched_length", "OL", "ML"]:
config = os.path.join(abs_dir, "config\\length_metric.json")
if metric in ["overlap_clean", "OD"]:
config = os.path.join(abs_dir, "config\\overlap_clean.json")
elif platform.system() == "Linux":
if metric == "diadem_metric" or metric == "DM":
config = os.path.join(abs_dir, "config/diadem_metric.json")
if metric in ["overall_length", "matched_length", "OL", "ML"]:
config = os.path.join(abs_dir, "config/length_metric.json")
if metric in ["overlap_clean", "OD"]:
config = os.path.join(abs_dir, "config/overlap_clean.json")
if DEBUG:
print("Config = {}".format(config))
# read test trees, gold trees and configs
test_swc_trees = []
for test_swc_file in test_swc_files:
test_swc_trees += read_swc_trees(test_swc_file)
gold_swc_trees = read_swc_trees(gold_swc_file)
config = read_json(config)
# info: how many trees read
print("There are {} test image(s) and {} gold image(s)".format(len(test_swc_trees), len(gold_swc_trees)))
if len(gold_swc_trees) == 0:
raise Exception("[Error: ] No gold image detected")
if len(gold_swc_trees) > 1:
print("[Warning: ] More than one gold image detected, only the first one will be used")
# entries to different metrics
gold_swc_treeroot = gold_swc_trees[0]
for test_swc_treeroot in test_swc_trees:
if metric == "diadem_metric" or metric == "DM":
diadem_metric(swc_test_tree=test_swc_treeroot,
swc_gold_tree=gold_swc_treeroot,
config=read_json("D:\gitProject\mine\PyNeval\config\diadem_metric.json"))
if reverse:
diadem_metric(gold_swc_treeroot, test_swc_treeroot)
if metric == "overall_length" or metric == "OL":
config["method"] = 1
length_metric(gold_swc_treeroot, test_swc_treeroot,
abs_dir, config)
if reverse:
length_metric(test_swc_treeroot, gold_swc_treeroot,
abs_dir, config)
if metric == "matched_length" or metric == "ML":
config["method"] = 2
length_metric(gold_swc_treeroot, test_swc_treeroot,
abs_dir, config)
if reverse:
config["detail"] = config["detail"][:-4] + "_reverse.swc"
length_metric(test_swc_treeroot, gold_swc_treeroot,
abs_dir, config)
if metric == "overlap_clean" or metric == "OC":
# debug
print("entry")
print(config["radius_threshold"])
print(config["length_threshold"])
print(output_dest)
overlap_clean(gold_swc_treeroot, output_dest, config)
if __name__ == "__main__":
pyneval()
# python ./pyneval.py --test D:\gitProject\mine\PyNeval\test\data_example\test\30_18_10_test.swc --gold D:\gitProject\mine\PyNeval\test\data_example\gold\30_18_10_gold.swc --metric matched_length --reverse true
# python ./pyneval.py --test D:\gitProject\mine\PyNeval\test\data_example\test\diadem\diadem1.swc --gold D:\gitProject\mine\PyNeval\test\data_example\gold\diadem\diadem1.swc --metric diadem_metric
# python ./pyneval.py --test D:\gitProject\mine\PyNeval\test\data_example\test\34_23_10_test.swc --gold D:\gitProject\mine\PyNeval\test\data_example\gold\34_23_10_gold.swc --metric diadem_metric
# python ./pyneval.py --gold D:\gitProject\mine\PyNeval\test\data_example\gold\overlap\overlap_sample5.swc --metric overlap_clean --output D:\gitProject\mine\PyNeval\output\overlap\overlap_output5.swc