-
Notifications
You must be signed in to change notification settings - Fork 10
/
files.py
164 lines (117 loc) · 4.07 KB
/
files.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import pickle
import csv
def get_name_data(n, regression='Friedman', noise='ARMA', params_reg={}, params_noise={}, seed=1):
"""
Parameters
----------
n : experiment sample size
regression : regression model, can be Friedman
noise : noise type, can be ARMA
params_reg : parameters of the regression part
params_noise : parameters of the noise, e.g. a dictionary {'ar': [1, ar1], 'ma':[1]}
to generate an AR(1) noise with coefficient -ar1
seed : random seed for reproducibility used in the experiment
Returns
-------
name : name of the file containing (if existing)
the generated data with the given parameters of simulations
"""
assert regression in ['Friedman','Linear'], 'regression must be Friedman or Linear.'
if regression == 'Friedman':
name = 'Friedman_'
elif regression == 'Linear':
name = 'Linear_'
assert noise in ['ARMA'], 'noise must be ARMA.'
if noise == 'ARMA':
ar = params_noise['ar']
ma = params_noise['ma']
ar_name = 'AR'
for p in range(1,len(ar)):
ar_name = ar_name + '_' + str(-ar[p])
ma_name = 'MA'
for q in range(1,len(ma)):
ma_name = ma_name + '_' + str(ma[q])
name = name + 'ARMA_' + ar_name + '_' + ma_name
if 'scale' in params_noise:
name = name + '_scale_' + str(params_noise['scale'])
if 'process_variance' in params_noise:
name = name + '_fixed_variance_' + str(params_noise['process_variance'])
name = name + '_seed_' + str(seed) + '_n_' + str(n)
return name
def get_name_results(method, n=None, online=True, randomized=False, params_method={}, basemodel='RF', regression=None, noise=None, params_reg={}, params_noise={}, dataset=None):
""" ...
Parameters
----------
method :
params_method :
Returns
-------
name :
"""
# Results file name, depending on the method
name_method = method + '_' + basemodel
if (method == 'ACP') & (params_method != {}):
name_method = name_method + '_gamma_' + str(params_method['gamma'])
if randomized:
name_method = name_method+'_randomized'
if not online:
name_method = name_method+'_offline'
# Results directory name, depending on the data simulation
#assert regression in ['Friedman','Linear'], 'regression must be Friedman or Linear.'
if regression == 'Friedman':
name_directory = 'Friedman_'
elif regression == 'Linear':
name_directory = 'Linear_'
#assert noise in ['ARMA'], 'noise must be ARMA.'
if noise == 'ARMA':
ar = params_noise['ar']
ma = params_noise['ma']
ar_name = 'AR'
for p in range(1,len(ar)):
ar_name = ar_name + '_' + str(-ar[p])
ma_name = 'MA'
for q in range(1,len(ma)):
ma_name = ma_name + '_' + str(ma[q])
name_directory = name_directory + 'ARMA_' + ar_name + '_' + ma_name
if 'scale' in params_noise:
name_directory = name_directory + '_scale_' + str(params_noise['scale'])
if 'process_variance' in params_noise:
name_directory = name_directory + '_fixed_variance_' + str(params_noise['process_variance'])
if dataset is not None:
name_directory = dataset
else:
name_directory = name_directory + '_n_' + str(n)
return name_directory, name_method
def load_file(parent, name, ext):
""" ...
Parameters
----------
parent :
name :
ext :
Returns
-------
file :
"""
assert ext in ['pkl'], 'ext must be pkl.'
path = parent + '/' + name + '.' + ext
if ext == 'pkl':
with open(path,'rb') as f:
file = pickle.load(f)
return file
def write_file(parent, name, ext, file):
""" ...
Parameters
----------
parent :
name :
ext :
file :
Returns
-------
"""
assert ext in ['pkl'], 'ext must be pkl.'
path = parent + '/' + name + '.' + ext
if ext == 'pkl':
with open(path,'wb') as f:
pickle.dump(file, f)