-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathextract_dataset.py
executable file
·64 lines (50 loc) · 2.04 KB
/
extract_dataset.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
#!/usr/bin/env python
import h5py
import numpy as np
import pandas as pd
from argparse import ArgumentParser
import yaml
from explore import Explorer
from collections import OrderedDict
def main():
args = parse_args()
extractor = Extractor(args.config)
extractor.run()
def parse_args():
description = 'extract dataset from a set of input files'
parser = ArgumentParser(description=description)
parser.add_argument('config', help='YAML file describing how to extract data')
return parser.parse_args()
class Extractor(object):
def __init__(self, config):
self.config = yaml.load(open(config))
self.datadict = OrderedDict()
def run(self):
for filename, features in self.config['files'].iteritems():
for path, details in features['datasets'].iteritems():
explorer = Explorer(filename, path)
self.extract_feature(explorer, details)
df = pd.DataFrame(self.datadict)
df.to_csv(self.config['outfile'], index=False)
def extract_feature(self, explorer, details):
name = details['name']
firstday = self.config['firstday']
lastday = self.config['lastday']
dayoffset = self.config['files'][explorer.filename]['dayoffset']
start = firstday + dayoffset
end = lastday + dayoffset
if self.config['mode'] == 'time_series':
if details['aggregation'] == 'all':
# Extract the data set for each day of the time series
# as a separate feature.
for day in range(firstday, lastday + 1):
data = explorer.extract_slice(day + dayoffset)
self.datadict["{0}.{1}".format(name, day)] = data
elif details['aggregation'] == 'mean':
data = explorer.extract_aggregate_data(start, end, np.mean)
self.datadict[name + '_mean'] = data
else:
data = explorer.extract_regression_data(start, end)
self.datadict[name] = data
if __name__ == '__main__':
main()