-
Notifications
You must be signed in to change notification settings - Fork 0
/
format_roi_csv.py
71 lines (57 loc) · 1.91 KB
/
format_roi_csv.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
from operator import attrgetter
import pandas as pd
import argparse
from pathlib import Path
from find_ellipses import calculate_parameters, save_result
def parse_args() -> argparse.Namespace:
class SortingHelpFormatter(argparse.HelpFormatter):
"""
Sort help menu options alphabetically.
"""
def add_arguments(self, actions):
actions = sorted(actions, key=attrgetter("option_strings"))
super(SortingHelpFormatter, self).add_arguments(actions)
# Parse CLI arguments
parser = argparse.ArgumentParser(
prog="format_roi_csv.py", formatter_class=SortingHelpFormatter
)
parser.add_argument("src", type=str, help="input roi csv from ImageJ")
parser.add_argument("-s", "--sort", type=str, choices=["x", "y", None], help="sort by x or y")
parser.add_argument("-m", "--metadata", type=str, help="metadata txt file")
args = parser.parse_args()
return args
def main():
# Parse arguments
args = parse_args()
# Get paths
file_path = Path(args.src)
if args.metadata:
meta_path = Path(args.metadata)
else:
meta_path = Path(args.src)
# Create dataframes
rois = pd.read_csv(file_path)
out = pd.DataFrame(
columns = [
"x center",
"y center",
"x diameter",
"y diameter",
"angle",
],
)
# Populate dataframe
out["x center"] = rois["X"] + rois["Width"] / 2
out["y center"] = rois["Y"] + rois["Height"] / 2
out["x diameter"] = rois["Width"]
out["y diameter"] = rois["Height"]
out["angle"] = pd.Series(0.0, index=out.index)
# Sort order
if args.sort is not None:
out.sort_values(f"{args.sort} center", inplace=True, ignore_index=True)
# Add parameters
calculate_parameters(meta_path, out)
# Save results
save_result(file_path, out, "_res")
if __name__ == "__main__":
main()