-
Notifications
You must be signed in to change notification settings - Fork 0
/
merge_biigle_reports.py
65 lines (49 loc) · 2.18 KB
/
merge_biigle_reports.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
import os
import zipfile
import shutil
import argparse
import requests
import pandas as pd
from tqdm import tqdm
from biigle.biigle import Api
# Example:
# python merge_biigle_reports.py -i C:\Users\cgros\Downloads\292_csv_image_annotation_report(1) -t 839-vme-morpho-taxa.csv -o 20210908_biigle_vme.csv
def get_parser():
parser = argparse.ArgumentParser(add_help=False)
# MANDATORY ARGUMENTS
mandatory_args = parser.add_argument_group('MANDATORY ARGUMENTS')
mandatory_args.add_argument('-i', '--ifolder', required=True, type=str,
help='Input folder containing zip archives for each survey.')
mandatory_args.add_argument('-t', '--ifname', required=True, type=str,
help='Input filename in each zip archives.')
mandatory_args.add_argument('-o', '--ofname', required=True, type=str,
help='CSV filename output.')
# OPTIONAL ARGUMENTS
optional_args = parser.add_argument_group('OPTIONAL ARGUMENTS')
optional_args.add_argument('-h', '--help', action='help', default=argparse.SUPPRESS,
help='Shows function documentation.')
return parser
def merge_biigle_reports(folder_i, fname_i, fname_o):
# Loop across archived folders
lst_df = []
for survey_zip in os.listdir(folder_i):
if survey_zip.endswith(".zip"):
path_zip = os.path.join(folder_i, survey_zip)
archive = zipfile.ZipFile(path_zip, 'r')
try:
df = pd.read_csv(archive.open(fname_i))
print("Found {} annotations in {}...".format(len(df), survey_zip))
lst_df.append(df)
except:
print("No annotation found in {}...".format(survey_zip))
df_stacked = pd.concat(lst_df, axis=0)
print("\nTotal number of annotations: {}...".format(len(df_stacked)))
print("Saving result in: {}...".format(fname_o))
df_stacked.to_csv(fname_o, index=False)
def main():
parser = get_parser()
args = parser.parse_args()
# Run function
merge_biigle_reports(folder_i=args.ifolder, fname_i=args.ifname, fname_o=args.ofname)
if __name__ == "__main__":
main()