This repository has been archived by the owner on Jul 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathss_repd_pairings.py
69 lines (63 loc) · 2.43 KB
/
ss_repd_pairings.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
"""
Extracting REPD groupings from SS dataset
- Jamie Taylor <jamie.taylor@sheffield.ac.uk>
- Ethan Jones <ejones18@sheffield.ac.uk>
- First Authored: 2020-04-10
"""
import os
import argparse
import pandas as pd
def get_matches(filename):
"""
Extract REPD groupings from CSV
"""
pairings = pd.read_csv(filename)
pairings = pairings.drop(columns=['SOLAR_MEDIA_REF', 'RO_Generator_ID', 'SS_SUB_ID'])
pairings = pairings.loc[pairings.REPD_REF_ID.notnull()].reset_index(drop=True)
matches = []
for i in pairings.index:
if i >= 1:
if pairings.SS_ID[i-1] == pairings.SS_ID[i]:
matches.append(matches[i-1] + 1)
else:
matches.append(1)
else:
matches.append(1)
pairings['matches'] = matches
pairings = pairings.convert_dtypes()
pairings.set_index(['SS_ID', 'matches'], inplace=True)
pairings.sort_index(inplace=True)
return pairings
def get_groups_from_matches(matches):
"""
Unstacks dataframe
"""
grouped_repd_ids = matches.unstack(level=-1)
return grouped_repd_ids
def main(input_file, output_file=None):
"""
Get REPD grouping from SS dataset and save to CSV.
"""
matches = get_matches(input_file)
groups = get_groups_from_matches(matches)
if output_file is not None:
groups.to_csv(output_file, index=False)
return groups
def parse_options():
"""Parse command line options."""
parser = argparse.ArgumentParser(description=("This is a command line interface (CLI) for "
"the ss_repd_groupings.py module"),
epilog="Jamie Taylor & Ethan Jones, 2020-04-10")
parser.add_argument("-f", "--input-file", dest="input_file", action="store", type=str,
required=True, metavar="</path/to/file>",
help="Specify the path to the input CSV file.")
parser.add_argument("-o", "--output-file", dest="output_file", action="store", type=str,
required=True, metavar="</path/to/file>",
help="Specify the path to the output CSV file.")
options = parser.parse_args()
if not os.path.isfile(options.input_file):
raise Exception(f"The input file '{options.input_file}' does not exist.")
return options
if __name__ == "__main__":
OPTIONS = parse_options()
main(OPTIONS.input_file, OPTIONS.output_file)