1 '''
2 Created on 30.06.2010
3
4 @author: Robert
5 '''
6 from numpy import mean, std, load
7 from scipy.stats import skew, kurtosis, mstats
8 from dsc_suite.tools.toolpaths import FOLDER_FOR_DATA, FOLDER_FOR_PLOTS
9 from os import path
10 import pylab
11
12
13 CHARACTERISTICS_LIST = {'mean value' : lambda x : x.mean(),
14 'standard deviation' : lambda x : x.std(),
15 'skew' : skew,
16 'kurtosis' : kurtosis,
17 'five percent quantile' : lambda x : mstats.mquantiles(x, [0.05])[0],
18 'minimum' : lambda x : x.min(),
19 'maximum' : lambda x : x.max(),
20 'range' : lambda x : x.max()-x.min()}
21
22
29
30
31
32
33
36 result = []
37 if samples == 0:
38 samples = len(data)
39 for i in range(samples):
40 result.append(characteristic_function(data[start_index:i+start_index+1]))
41 return result
42
43 -def test_dynamic_leveling(data, samples=2000, cut_away=0, trials=5,
44 characteristic='mean value', final_value=False,
45 sum_stats=False, create_png=False):
46 """ Plot leveling of the given characteristic.
47
48 data ... cost criteria in chronological order
49 (out of optimization, format: numpy.array)
50 samples ... maximum of samples considered for leveling
51 cut_away ... number of data points to omit (skip initial stage)
52 trials ... how many different trials are used
53 characteristic ... statistical property to investigate
54 final_value ... if True, outputs the statistical property
55 calculated over the complete data set
56 sum_stats ... if True, replace trials with their mean and std values
57 create_png ... if True, a .png-file is created (otherwise interactive use)
58
59 Plot the leveling of the given characteristic. X-axis represents the
60 number of used samples to achieve the corresponding value. If sum_stats
61 is True, the standard deviation and the mean value is calculated over all
62 trials for each number of used samples.
63
64 To generate several trials, data is split into multiple parts. Thus, the
65 current approach to generate different trials is only applicable for
66 Monte Carlo generated date (randomly and independent at each point).
67
68 TODO!!! To investigate the leveling for other kind of data a new approach
69 is necessary! Until that use trials = 1 in the accordant cases.
70
71 """
72 assert(characteristic in CHARACTERISTICS_LIST.keys())
73 characteristic_function = CHARACTERISTICS_LIST[characteristic]
74 pylab.interactive(False)
75
76
77 if sum_stats:
78 complete_data = []
79 if final_value:
80 c_final = characteristic_function(data)
81 pylab.plot([cut_away+1, samples], [c_final, c_final], c='black', ls='--', lw=1.5)
82 for i in range(trials):
83 start_index = ((len(data) - samples) // trials) * i
84
85 print '.',
86 trial_data = dynamic_characteristic_leveling(data, samples, start_index, characteristic_function)
87 if sum_stats:
88 complete_data.append(trial_data)
89 else:
90 pylab.plot(range(cut_away+1, samples+1), trial_data[cut_away:])
91 if sum_stats:
92
93 pylab.subplot(2, 1, 1)
94 pylab.plot([std(x) for x in zip(*complete_data)])
95
96 pylab.subplot(2, 1, 2)
97 pylab.plot([mean(x) for x in zip(*complete_data)])
98 if create_png:
99 i = 1
100 filename = 'Statistic Characteristic Evolution %s %02i.png' % (strftime("%Y-%m-%d %H%M%S"), i)
101 while path.exists(FOLDER_FOR_PLOTS + '/' + filename):
102 i += 1
103 filename = 'Statistic Characteristic Evolution %s %02i.png' % (strftime("%Y-%m-%d %H%M%S"), i)
104 pylab.savefig(FOLDER_FOR_PLOTS + '/' + filename)
105 pylab.close()
106 return filename
107 else:
108 pylab.show()
109