-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfiles.py
217 lines (183 loc) · 7.1 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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
import pickle
import lzma
import numpy as np
import os
def get_setting(dim=3, params_reg={'regression':'Linear'}, params_noise={'noise':'Gaussian'}, params_missing={}):
regression = params_reg['regression']
assert regression in ['Linear'], 'regression must be Linear.'
if 'mean' in params_reg:
mean = params_reg['mean']
else:
mean = 1
if 'scale' in params_reg:
scale = params_reg['scale']
else:
scale = 1
if params_reg['beta'] is not None:
beta = params_reg['beta']
else:
beta = np.full(dim,1)
if dim < 10:
name = 'Linear_d_'+str(dim)+'_beta_'+'_'.join(str(x) for x in beta)+'_Gaussian_Mean_'+str(mean)
else:
name = 'Linear_d_'+str(dim)+'_beta_varies_Gaussian_Mean_'+str(mean)+'_Scale_'+str(scale)
if 'prob_missing' in params_missing:
prob_missing = params_missing['prob_missing']
else:
prob_missing = 0.2
if 'mechanism' in params_missing:
if params_missing['mechanism'] == 'MNAR_mask_quantiles':
name = name + '_' + params_missing['mechanism'] + '_q_' + str(params_missing['q']) + '_'
else:
name = name + '_' + params_missing['mechanism'] + '_'
if 'id_setting' in params_missing:
name = name + 'id_'+str(params_missing['id_setting'])+'_'
else:
name = name + '_MCAR_'
name = name + str(prob_missing)
return name
def get_name_data(train_size, cal_size, params_test, dim=3, params_reg={}, params_noise={}, dataset=None, params_missing={}, seed=1):
"""
Parameters
----------
n : experiment sample size
dim : dimension of the covariates (i.e. X lies in R^dim)
regression : regression model, should be Linear
noise : noise type, can be Gaussian
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
"""
max_test_size = np.max(params_test['test_size'])
if dataset is None:
regression = params_reg['regression']
assert regression in ['Linear'], 'regression must be Linear.'
name = get_setting(dim=dim, params_reg=params_reg, params_noise=params_noise, params_missing=params_missing)
else:
name = dataset
name = name + '_seed_' + str(seed) + '_train_' + str(train_size) + '_cal_' + str(cal_size) + '_test_' + str(max_test_size)
if 'prob_missing' in list(params_missing.keys()):
name = name + '_prob_' + str(params_missing['prob_missing'])
return name
def get_name_data_imputed(train_size, cal_size, params_test, imputation,
dim=3, params_reg={}, params_noise={}, dataset=None, params_missing={}, seed=1):
"""
Parameters
----------
n : experiment sample size
dim : dimension of the covariates (i.e. X lies in R^dim)
regression : regression model, should be Linear
noise : noise type, can be Gaussian
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
"""
name = get_name_data(train_size, cal_size, params_test, dim=dim,
params_reg=params_reg, params_noise=params_noise, dataset=dataset, params_missing=params_missing, seed=seed)
if imputation is not None:
name = name + '_imputation_' + imputation
return name
def get_name_results(pipeline, train_size, cal_size, n_rep, imputation=None, d=3,
params_reg={}, params_noise={}, dataset=None, params_missing={}):
""" ...
Parameters
----------
pipeline :
params_method :
Returns
-------
name :
"""
# Results file name, depending on the method
if pipeline != 'Oracle':
name_method = pipeline+'_Imp_'+imputation
else:
name_method = pipeline
# Results directory name, depending on the data simulation
if dataset is not None:
name_directory = dataset
else:
name_directory = get_setting(dim=d, params_reg=params_reg, params_noise=params_noise, params_missing=params_missing)
if 'prob_missing' in list(params_missing.keys()):
name_directory = name_directory + '_train_' + str(train_size) + '_cal_' + str(cal_size) + '_prob_' + str(params_missing['prob_missing']) + '_rep_' + str(n_rep)
else:
name_directory = name_directory + '_train_' + str(train_size) + '_cal_' + str(cal_size) + '_rep_' + str(n_rep)
return name_directory, name_method
def load_file(parent, name, ext):
""" ...
Parameters
----------
parent :
name :
ext :
Returns
-------
file :
"""
assert ext in ['pkl', 'xz'], 'ext must be pkl or xz.'
path = parent + '/' + name + '.' + ext
if ext == 'pkl':
with open(path,'rb') as f:
file = pickle.load(f)
elif ext == 'xz':
with lzma.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', 'xz'], 'ext must be pkl or xz.'
path = parent + '/' + name + '.' + ext
if ext == 'pkl':
if not os.path.isdir(parent):
os.makedirs(parent)
with open(path,'wb') as f:
pickle.dump(file, f)
elif ext == 'xz':
if not os.path.isdir(parent):
os.makedirs(parent)
with lzma.open(path,'wb') as f:
pickle.dump(file, f)
def get_name_method(method, basemodel=None, mask='No', protection='No', exact=False):
if exact == True:
assert method == 'CQR_MDA', 'With MDA-Exact you should be masking.'
method = method + '_Exact'
if method == 'CQR_MDA':
method = method + '_Nested'
if method == 'Oracle':
name = method
elif method == 'Oracle_mean' and protection=='No':
name = method
elif method == 'Oracle_mean' and protection!='No':
name = '_'.join([method, protection])
elif protection == 'No' and mask == 'No':
name = '_'.join([method, basemodel])
elif method in ['QR', 'QR_TrainCal', 'CQR_MDA_Nested', 'CQR_MDA_Exact'] and mask == 'No':
name = '_'.join([method, basemodel])
elif method in ['QR', 'QR_TrainCal', 'CQR_MDA_Nested', 'CQR_MDA_Exact'] and mask == 'Yes':
name = '_'.join([method, basemodel, 'Mask'])
elif protection == 'No':
name = '_'.join([method, basemodel, 'Mask'])
elif mask == 'No':
name = '_'.join([method, basemodel, protection])
else:
name = '_'.join([method, basemodel, 'Mask', protection])
return name