-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathagg.py
60 lines (47 loc) · 2.12 KB
/
agg.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
import argparse
import os
import pandas as pd
argParser = argparse.ArgumentParser()
argParser.add_argument('-m', '--match', type = str, help = 'String to match solution files')
argParser.add_argument('-s', '--sols_dir', type = str, help = 'Filepath to solutions directory')
argParser.add_argument('-o', '--output_filepath', type = str, help = 'Output filepath')
argParser.add_argument('-c', '--cleanup', action = 'store_true', help = 'Flag - remove individual solution files')
argParser.add_argument('-v', '--var_names', action = 'store_true', help = 'Flag - store variable names')
args = argParser.parse_args()
# Check that string match is provided
if not args.match:
raise Exception('No match string provided.')
# Generate output filepath name if not provided
if not args.output_filepath:
args.output_filepath = f'{args.match}_sols.csv'
# Initialize an empty Pandas DataFrame to which solutions will later be appended
sols = pd.DataFrame()
# Iterate through files in solutions directory
for file in os.listdir(args.sols_dir):
# Check if string match is in filepath
if args.match in file:
if args.sols_dir:
filepath = os.path.join(args.sols_dir, file)
else:
filepath = file
# Check that filepath/format is valid
try:
sol = pd.read_csv(filepath)
except:
print(f'Omitting matched file {filepath} - invalid file format')
continue
# Check that variable names are consistent with the aggregated DataFrame
try:
pivoted = pd.DataFrame([sol['Value'].tolist()], columns = sol['VarName'].tolist())
sols = pd.concat([sols, pivoted])
except:
print(f'Omitting matched file {filepath} - inconsistent variable names')
continue
# Remove individual solution files is cleanup flag is specified
if args.cleanup:
os.remove(filepath)
# Write variable names to output file if var_names flag is specified
if args.var_names:
sols.to_csv(args.output_filepath, index = False)
else:
sols.to_csv(args.output_filepath, header = False, index = False)