-
Notifications
You must be signed in to change notification settings - Fork 0
/
resultcomputation.py
610 lines (490 loc) · 22.1 KB
/
resultcomputation.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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
#!/usr/bin/env python
# encoding: utf-8
"""
resultcomputations.py
Created by Loic Matthey on 2014-01-17
Copyright (c) 2014 . All rights reserved.
"""
import numpy as np
import load_experimental_data
import utils
import scipy.stats.mstats as mstats
class ResultComputation():
"""
Class computing some result out of arrays.
This is called from JobWrapper at the end of its compute() function. It receives all arrays created by an ExperimentLauncher. So use meaningful and constant variable names if you want to reuse different ResultComputations
In generator_*, should specify the type of ResultComputation to use as an argument, everything will be instantiated appropriately later.
"""
def __init__(self, computation_name, debug=True):
self.computation_fct = None
self.computation_name = None
self.debug = debug
# Check that the computation name is correct
self.check_set_computation(computation_name)
if self.debug:
print "ResultComputation initialised\n > %s" % (self.computation_name)
def __str__(self):
'''
Write which ResultComputation you are
'''
return 'ResultComputation %s' % self.computation_name
def check_set_computation(self, computation_name):
'''
Look at the functions defined here, and verify that one matches the computation_name provided.
Looks for compute_result_{computation_name}()
'''
# Duck-typing check it
try:
fct_found = getattr(self, "compute_result_%s" % computation_name)
# All good.
self.computation_name = computation_name
self.computation_fct = fct_found
except AttributeError:
raise ValueError(
'ResultComputation %s not implemented' % computation_name)
def compute_result(self, all_variables):
'''
Dispatching method for different compute_result_* functions.
self.computation_name can be:
- distemfits: looks at result_em_fits, computes the distance to specific datasets.
TODO Find how to handle parameters... Should either load them from a file, or provide them as ExperimentLauncher arguments, but this is slightly tedious
'''
return self.computation_fct(all_variables)
##########################################################################
def compute_result_random(self, all_variables):
'''
Dummy result computation, where you just return a random value
'''
return np.random.rand()
def compute_result_distemfits_dataset(self,
all_variables,
experiment_id='bays09',
cache_array_name='result_dist_bays09',
variable_selection_slice=slice(
None, 4),
variable_selection_slice_cache=slice(
None, None),
metric='mse'):
'''
Result is the distance (sum squared) to experimental data fits
Assume that:
- result_em_fits exists. Does an average over repetitions_axis and sums over all others.
- metric = 'mse' | 'kl'
- variable_selection_slice: slice(0, 1) for kappa, slice(1, 4) for mixt proportions, slice(none, 4) for all EM params
'''
assert metric == 'mse' or metric == 'kl', "Metric should be {mse, kl}"
repetitions_axis = all_variables.get('repetitions_axis', -1)
if cache_array_name in all_variables:
# If already computed, use it
result_dist_allT = utils.nanmean(
all_variables[cache_array_name][:, variable_selection_slice_cache],
axis=repetitions_axis)
elif 'result_em_fits' in all_variables:
# Do some annoying slice manipulation
slice_valid_indices = variable_selection_slice.indices(
all_variables['result_em_fits'].shape[1])
# Create output variables
if metric == 'mse':
result_dist_allT = np.nan * np.empty(
(all_variables['T_space'].size,
slice_valid_indices[1] - slice_valid_indices[0]))
elif metric == 'kl':
result_dist_allT = np.nan * np.empty((all_variables['T_space'].size))
### Result computation
if experiment_id == 'bays09':
data_loaded = load_experimental_data.load_data_bays09(
fit_mixture_model=True)
elif experiment_id == 'gorgo11':
data_loaded = load_experimental_data.load_data_gorgo11(
fit_mixture_model=True)
else:
raise ValueError('wrong experiment_id {}'.format(experiment_id))
experimental_mixtures_mean = data_loaded['em_fits_nitems_arrays']['mean']
experimental_T_space = np.unique(data_loaded['n_items'])
curr_result = np.nan
for T_i, T in enumerate(all_variables['T_space']):
if T in experimental_T_space:
if metric == 'mse':
curr_result = (
experimental_mixtures_mean[variable_selection_slice,
experimental_T_space == T] -
all_variables['result_em_fits'][T_i, variable_selection_slice]
)**2.
elif metric == 'kl':
curr_result = utils.KL_div(
all_variables['result_em_fits'][T_i, variable_selection_slice],
experimental_mixtures_mean[variable_selection_slice,
experimental_T_space == T],
axis=0)
result_dist_allT[T_i] = utils.nanmean(
curr_result, axis=repetitions_axis)
else:
raise ValueError(
'array {}/result_em_fits not present, bad'.format(cache_array_name))
print result_dist_allT
# return the overall distance, over all parameters and number of items
return np.nansum(result_dist_allT)
def compute_result_distemfits_bays09(self, all_variables):
'''
Result is the distance (sum squared) to experimental data fits
Assume that:
- result_em_fits exists. Does an average over repetitions_axis and sums over all others.
'''
return self.compute_result_distemfits_dataset(
all_variables,
experiment_id='bays09',
cache_array_name='result_dist_bays09',
variable_selection_slice=slice(None, 4),
metric='mse')
def compute_result_distemfits_gorgo11(self, all_variables):
'''
Result is the distance (sum squared) to experimental data fits
Assume that:
- result_em_fits exists. Does an average over repetitions_axis and sums over all others.
'''
return self.compute_result_distemfits_dataset(
all_variables,
experiment_id='gorgo11',
cache_array_name='result_dist_gorgo11',
variable_selection_slice=slice(None, 4),
variable_selection_slice_cache=slice(None, 4),
metric='mse')
def compute_result_distemkappa_bays09(self, all_variables):
'''
Result is the distance (sum squared) to experimental data kappa
Assume that:
- result_em_fits exists. Does an average over repetitions_axis and sums over all others.
'''
return self.compute_result_distemfits_dataset(
all_variables,
experiment_id='bays09',
cache_array_name='result_dist_bays09',
variable_selection_slice=slice(0, 1),
variable_selection_slice_cache=slice(0, 1),
metric='mse')
def compute_result_distemkappalog_bays09(self, all_variables):
'''
Compute the distance to the experimental data kappa, but now take the log afterwards.
Should be better behaved.
Assume that:
- result_em_fits exists. Does an average over repetitions_axis and sums over all others.
'''
return np.log(self.compute_result_distemkappa_bays09(all_variables))
def compute_result_distemkappa_gorgo11(self, all_variables):
'''
Result is the distance (sum squared) to experimental data kappa
Assume that:
- result_em_fits exists. Does an average over repetitions_axis and sums over all others.
'''
return self.compute_result_distemfits_dataset(
all_variables,
experiment_id='gorgo11',
cache_array_name='result_dist_gorgo11',
variable_selection_slice=slice(0, 1),
variable_selection_slice_cache=slice(0, 1),
metric='mse')
def compute_result_distemkappalog_gorgo11(self, all_variables):
'''
Compute the distance to the experimental data kappa, but now take the log afterwards.
Should be better behaved.
Assume that:
- result_em_fits exists. Does an average over repetitions_axis and sums over all others.
'''
return np.log(self.compute_result_distemkappa_gorgo11(all_variables))
def compute_result_distemmixtKL_bays09(self, all_variables):
'''
Result is the distance (KL) to experimental mixture proportions
Assume that:
- result_em_fits exists. Does an average over repetitions_axis and sums over all others.
'''
return self.compute_result_distemfits_dataset(
all_variables,
experiment_id='bays09',
cache_array_name='result_dist_bays09_emmixt_KL',
variable_selection_slice=slice(1, 4),
variable_selection_slice_cache=slice(None, None),
metric='kl')
def compute_result_distemmixtKL_gorgo11(self, all_variables):
'''
Result is the distance (KL) to experimental mixture proportions
Assume that:
- result_em_fits exists. Does an average over repetitions_axis and sums over all others.
'''
return self.compute_result_distemfits_dataset(
all_variables,
experiment_id='gorgo11',
cache_array_name='result_dist_gorgo11_emmixt_KL',
variable_selection_slice=slice(1, 4),
variable_selection_slice_cache=slice(None, None),
metric='kl')
def compute_result_distem_logkappa_mixtKL_bays09(self,
all_variables,
normaliser_logkappa=1.0,
normaliser_mixtKL=1.0):
'''
Result is the sum of the emkappa_log and emmixtKL distances.
Should be normalized later, not sure how to pass it on.
Assume that:
- result_em_fits exists. Does an average over repetitions_axis and sums over all others.
'''
return self.compute_result_distemkappalog_bays09(
all_variables
) / normaliser_logkappa + self.compute_result_distemmixtKL_bays09(
all_variables) / normaliser_mixtKL
def compute_result_distem_logkappa_mixtKL_gorgo11(self,
all_variables,
normaliser_logkappa=1.0,
normaliser_mixtKL=1.0):
'''
Result is the sum of the emkappa_log and emmixtKL distances.
Should be normalized later, not sure how to pass it on.
Assume that:
- result_em_fits exists. Does an average over repetitions_axis and sums over all others.
'''
return self.compute_result_distemkappalog_gorgo11(
all_variables
) / normaliser_logkappa + self.compute_result_distemmixtKL_gorgo11(
all_variables) / normaliser_mixtKL
def compute_result_distfitexpbic(self, all_variables):
'''
Result is the summed BIC score of the FitExperiment result on all datasets
'''
if 'result_fitexperiments' in all_variables:
# We have result_fitexperiments, that's good.
# Make it work for either fixed T or multiple T.
if len(all_variables['result_fitexperiments'].shape) == 2:
# Single T. Average over axis -1
bic_summed = utils.nanmean(all_variables['result_fitexperiments'][0])
elif len(all_variables['result_fitexperiments'].shape) == 3:
# Multiple T. Average over axis -1, then sum
bic_summed = np.nansum(
utils.nanmean(
all_variables['result_fitexperiments'][:, 0], axis=-1))
else:
raise ValueError("wrong shape for result_fitexperiments: {}".format(
all_variables['result_fitexperiments'].shape))
else:
# We do not have it, could instantiate a FitExperiment with "normal" parameters and work from there instead
raise NotImplementedError(
'version without result_fitexperiments not implemented yet')
return bic_summed
def compute_result_distfit_bays09_bic(self, all_variables):
'''
Result is summed BIC score of FitExperiment to Bays09
'''
return self.compute_result_distfit_givenexp(
all_variables, experiment_id='bays09', metric_index=0)
def compute_result_distfit_gorgo11_bic(self, all_variables):
'''
Result is summed BIC score of FitExperiment to Gorgo11
'''
return self.compute_result_distfit_givenexp(
all_variables, experiment_id='gorgo11', metric_index=0)
def compute_result_distfit_bays09_ll90(self, all_variables):
'''
Result is summed negative LL, only top 90% each time.
'''
return -self.compute_result_distfit_givenexp(
all_variables, experiment_id='bays09', metric_index=2)
def compute_result_distfit_gorgo11_ll90(self, all_variables):
'''
Result is summed negative LL, only top 90% each time.
'''
return -self.compute_result_distfit_givenexp(
all_variables, experiment_id='gorgo11', metric_index=2)
def compute_result_distfit_givenexp(
self,
all_variables,
experiment_id='bays09',
metric_index=0,
target_array_name='result_fitexperiments_all'):
'''
Result is the summed BIC score of the FitExperiment result on a given dataset
'''
if target_array_name in all_variables:
# We have target_array_name (result_fitexperiments_all or result_fitexperiments_noiseconv_all say), that's good.
# Extract only the Bays09 result
experiment_index = all_variables['all_parameters'][
'experiment_ids'].index(experiment_id)
# Make it work for either fixed T or multiple T.
if len(all_variables[target_array_name].shape) == 3:
# Single T. Average over axis -1
bic_summed = utils.nanmean(
all_variables[target_array_name][metric_index, experiment_index])
elif len(all_variables[target_array_name].shape) == 4:
# Multiple T. Average over axis -1, then sum
bic_summed = np.nansum(
utils.nanmean(
all_variables[target_array_name][:, metric_index,
experiment_index],
axis=-1))
else:
raise ValueError(
"wrong shape for result_fitexperiments_all: {}".format(
all_variables[target_array_name].shape))
else:
# We do not have it, could instantiate a FitExperiment with "normal" parameters and work from there instead
raise NotImplementedError(
'version without result_fitexperiments_all not implemented yet')
return bic_summed
def compute_result_distfit_noiseconv_gorgo11_bic(self, all_variables):
'''
Result is summed BIC score of FitExperiment to Gorgo11, using a posterior convolved with a noise output distribution
'''
return self.compute_result_distfit_givenexp(
all_variables,
experiment_id='gorgo11',
metric_index=0,
target_array_name='result_fitexperiments_noiseconv_all')
def compute_result_distfit_noiseconv_bays09_bic(self, all_variables):
'''
Result is summed BIC score of FitExperiment to Bays09, using a posterior convolved with a noise output distribution
'''
return self.compute_result_distfit_givenexp(
all_variables,
experiment_id='bays09',
metric_index=0,
target_array_name='result_fitexperiments_noiseconv_all')
def compute_result_filenameoutput(self, all_variables):
'''
Result is filename of the outputted data.
Looks weird, but is actually useful :)
Assume that:
- dataio exists.
'''
variables_required = ['dataio']
if not set(variables_required) <= set(all_variables.keys()):
print "Error, missing variables for compute_result_distemfits: \nRequired: %s\nPresent%s" % (
variables_required, all_variables.keys())
return np.nan
# Extract the filename
return all_variables['dataio'].filename
def compute_result_dist_collapsedemfit_gorgo11seq(self, all_variables):
'''
Use the collapsed mixture fits to Gorgo11 Sequential.
Should be precomputed by FitExperimentSequential
'''
res_variant = 'result_emfit_mse'
if res_variant in all_variables:
repetitions_axis = all_variables.get('repetitions_axis', -1)
data_fits = all_variables[res_variant]
result_dist = utils.nanmean(data_fits, axis=repetitions_axis)
print result_dist
return result_dist
else:
raise ValueError("%s was not found in the outputs" % res_variant)
def compute_result_dist_emfit_scaled(self, all_variables):
'''
Use the scaled MSE to the mixture model fits.
Should have been precomputed by launchers_fitexperiment_allt.
'''
res_variant = 'result_emfit_mse_scaled'
if res_variant in all_variables:
repetitions_axis = all_variables.get('repetitions_axis', -1)
data_fits = all_variables[res_variant]
result_dist = np.nansum(utils.nanmean(data_fits, axis=repetitions_axis))
print result_dist
return result_dist
else:
raise ValueError("%s was not found in the outputs" % res_variant)
def _compute_dist_llvariant(self, all_variables, variant='ll'):
'''
Given outputs from FitExperimentAllT, will compute the summed LL,
as this seems to be an acceptable metric for data fitting.
'''
res_variant = 'result_%s_sum' % variant
if res_variant in all_variables:
# Average over repetitions and sum over the rest.
repetitions_axis = all_variables.get('repetitions_axis', -1)
result_dist = np.nansum(
utils.nanmean(-all_variables[res_variant], axis=repetitions_axis))
print result_dist
return result_dist
else:
raise ValueError("%s was not found in the outputs" % res_variant)
def compute_result_dist_ll_allt(self, all_variables):
'''
Given outputs from FitExperimentAllT, will compute the summed LL,
as this seems to be an acceptable metric for data fitting.
'''
return self._compute_dist_llvariant(all_variables, variant='ll')
def compute_result_dist_ll90_allt(self, all_variables):
'''
Given outputs from FitExperimentAllT, will compute the summed
LL90.
Discards the most outliers.
'''
return self._compute_dist_llvariant(all_variables, variant='ll90')
def compute_result_dist_ll92_allt(self, all_variables):
'''
Given outputs from FitExperimentAllT, will compute the summed
LL90.
Discards the most outliers.
'''
return self._compute_dist_llvariant(all_variables, variant='ll92')
def compute_result_dist_ll95_allt(self, all_variables):
'''
Given outputs from FitExperimentAllT, will compute the summed
LL90.
Discards the most outliers.
'''
return self._compute_dist_llvariant(all_variables, variant='ll95')
def compute_result_dist_ll97_allt(self, all_variables):
'''
Given outputs from FitExperimentAllT, will compute the summed
LL90.
Discards the most outliers.
'''
return self._compute_dist_llvariant(all_variables, variant='ll97')
def compute_result_dist_ll_median_allt(self, all_variables):
'''
Use the median of LL to get a score.
'''
if 'result_ll_n' in all_variables:
repetitions_axis = all_variables.get('repetitions_axis', -1)
data_ll = -all_variables['result_ll_n']
data_ll = data_ll.reshape(-1, data_ll.shape[repetitions_axis])
result_dist = utils.nanmean(np.nanmedian(data_ll, axis=0))
print result_dist
return result_dist
else:
raise ValueError("%s was not found in the outputs" % res_variant)
def compute_result_dist_prodll_allt(self, all_variables):
'''
Given outputs from FitExperimentAllT, will compute the geometric mean of the LL.
UGLY HACK: in order to keep track of the minLL, we return it here.
You should have a cma_iter_function that cleans it before cma_es.tell() is called...
'''
if 'result_ll_sum' in all_variables:
repetitions_axis = all_variables.get('repetitions_axis', -1)
# Shift to get LL > 0 always
currMinLL = np.min(all_variables['result_ll_sum'])
if currMinLL < all_variables['all_parameters']['shiftMinLL']:
all_variables['all_parameters']['shiftMinLL'] = currMinLL
# Remove the current minLL, to make sure fitness > 0
print 'Before: ', all_variables['result_ll_sum']
all_variables['result_ll_sum'] -= all_variables['all_parameters'][
'shiftMinLL']
all_variables['result_ll_sum'] += 0.001
print 'Shifted: ', all_variables['result_ll_sum']
result_dist_nll_geom = -mstats.gmean(
utils.nanmean(all_variables['result_ll_sum'], axis=repetitions_axis),
axis=-1)
print result_dist_nll_geom
return np.array([
result_dist_nll_geom, all_variables['all_parameters']['shiftMinLL']
])
else:
raise ValueError('result_ll_sum was not found in the outputs')
######################################################
## Unit tests
######################################################
def test_dummyresult_computation():
'''
Test if the dummy result computation works
'''
rc = ResultComputation('random')
all_variables = {}
print rc.compute_result(all_variables)
if __name__ == '__main__':
test_dummyresult_computation()