14
14
import numpy
15
15
import plotly
16
16
import scipy .optimize
17
+ import scipy .stats
17
18
18
19
19
20
PROCESS_COUNT = int (os .cpu_count () / 2 )
@@ -37,26 +38,32 @@ def __call__(cls, path, _data):
37
38
_data ['max' ].extend ([0 ] * cls .origins )
38
39
_data ['min' ].extend ([0 ] * cls .origins )
39
40
40
- formula , fit_x , fit_y = cls .lsq_logistic_fit (_data ['x' ], _data ['y' ])
41
+ formula , fit_x , fit_y , predicted_y = cls .lsq_logistic_fit (
42
+ _data ['x' ], _data ['y' ])
41
43
data = dict (
42
44
x = _data ['x' ][:- cls .origins ] or _data ['x' ],
43
45
y = _data ['y' ][:- cls .origins ] or _data ['y' ],
44
46
max = _data ['max' ][:- cls .origins ] or _data ['max' ],
45
47
min = _data ['min' ][:- cls .origins ] or _data ['min' ])
46
48
47
- # data['y'] = cls.logistic_linearisation(data['y'])
48
- # data['max'] = cls.logistic_linearisation(data['max'])
49
- # data['min'] = cls.logistic_linearisation(data['min'])
50
- # fit_y = cls.logistic_linearisation(fit_y)
49
+ data ['y' ] = cls .logistic_linearisation (data ['y' ])
50
+ data ['max' ] = cls .logistic_linearisation (data ['max' ])
51
+ data ['min' ] = cls .logistic_linearisation (data ['min' ])
52
+ fit_y = cls .logistic_linearisation (fit_y )
53
+ formula = cls .logistic_linearisation ("formula" )
51
54
52
- cls .scatter (path , data , formula , fit_x , fit_y )
53
- cls .bar (path , data , formula , fit_x , fit_y )
55
+ pearsonr_y = predicted_y
56
+ y = _data ['y' ]
57
+
58
+ pearsonr = scipy .stats .pearsonr (y , pearsonr_y )
59
+ cls .scatter (path , data , formula , fit_x , fit_y , Pearsonr = pearsonr )
60
+ cls .bar (path , data , formula , fit_x , fit_y , Pearsonr = pearsonr )
54
61
55
62
cls .counter += 1
56
63
cls .lock = False
57
64
58
65
@classmethod
59
- def bar (cls , path , _data , formula , fit_x , fit_y ):
66
+ def bar (cls , path , _data , formula , fit_x , fit_y , ** kwargs ):
60
67
data = [
61
68
plotly .graph_objs .Scatter (
62
69
x = _data ['x' ],
@@ -81,7 +88,9 @@ def bar(cls, path, _data, formula, fit_x, fit_y):
81
88
title = "{}<br>{}" .format (
82
89
path [path .rfind ('/' ) + 1 :].replace (
83
90
'.learning_rate.result.json.' , ' ' ),
84
- formula ))
91
+ formula ) + "" .join (
92
+ ["<br>{}: {}" .format (key , value )
93
+ for key , value in kwargs .items ()]))
85
94
fig = plotly .graph_objs .Figure (data = data , layout = layout )
86
95
plotly .offline .plot (
87
96
fig ,
@@ -91,7 +100,7 @@ def bar(cls, path, _data, formula, fit_x, fit_y):
91
100
filename = "{}.error_bar.html" .format (path ))
92
101
93
102
@classmethod
94
- def scatter (cls , path , _data , formula , fit_x , fit_y ):
103
+ def scatter (cls , path , _data , formula , fit_x , fit_y , ** kwargs ):
95
104
data = [
96
105
plotly .graph_objs .Scatter (
97
106
x = _data ['x' ],
@@ -109,7 +118,9 @@ def scatter(cls, path, _data, formula, fit_x, fit_y):
109
118
title = "{}<br>{}" .format (
110
119
path [path .rfind ('/' ) + 1 :].replace (
111
120
'.learning_rate.result.json.' , ' ' ),
112
- formula ))
121
+ formula ) + "" .join (
122
+ ["<br>{}: {}" .format (key , value )
123
+ for key , value in kwargs .items ()]))
113
124
fig = plotly .graph_objs .Figure (data = data , layout = layout )
114
125
plotly .offline .plot (
115
126
fig ,
@@ -124,57 +135,63 @@ def exponenial_func(x, a, b, c):
124
135
125
136
@classmethod
126
137
def exp_fit (cls , _x , _y ):
127
- x = _x
128
- y = _y
138
+ y = numpy . array ( _y )
139
+ x = numpy . array ( _x )
129
140
a , b , c = scipy .optimize .curve_fit (cls .exponenial_func , _x , _y )[0 ]
130
141
__x = numpy .array (list (range (len (_x ) * 2 )))
131
142
__x = __x / (max (__x ) - min (__x )) * (max (x ) - min (x )) + min (x )
132
143
__y = cls .exponenial_func (__x , a , b , c )
144
+ predicted_y = cls .exponenial_func (x , a , b , c )
133
145
134
146
return "y = a * e^(b * x) + c<br> a = {}, b = {}, c = {}" .format (
135
- a , b , c ), __x , __y
147
+ a , b , c ), __x . tolist () , __y . tolist (), predicted_y . tolist ()
136
148
137
149
@classmethod
138
150
def lsq_exp_fit (cls , _x , _y ):
139
151
y = 1 - numpy .array (_y )
140
152
y = numpy .log (y )
141
- x = _x
153
+ x = numpy . array ( _x )
142
154
k , e = numpy .polyfit (x , y , 1 )
143
155
__x = numpy .array (list (range (len (_x ) * 2 )))
144
156
__x = __x / (max (__x ) - min (__x )) * (max (x ) - min (x )) + min (x )
145
157
__y = 1 - numpy .e ** (k * __x + e )
158
+ predicted_y = 1 - numpy .e ** (k * x + e )
146
159
147
160
return "y = 1 - e^(ke + e)<br> k = {}, e = {}" .format (
148
- k , e ), __x .tolist (), __y .tolist ()
161
+ k , e ), __x .tolist (), __y .tolist (), predicted_y . tolist ()
149
162
150
163
@classmethod
151
164
def lsq_ln_fit (cls , _x , _y ):
152
165
y = numpy .log (_y )
153
- x = _x
166
+ x = numpy . array ( _x )
154
167
k , c = numpy .polyfit (x , y , 1 )
155
168
__x = numpy .array (list (range (len (_x ) * 2 )))
156
169
__x = __x / (max (__x ) - min (__x )) * (max (x ) - min (x )) + min (x )
157
170
__y = numpy .exp (k * __x + c )
171
+ predicted_y = numpy .exp (k * x + c )
158
172
159
173
return "y = e^(kx + c)<br> k = {}, c = {}" .format (
160
- k , c ), __x .tolist (), __y .tolist ()
174
+ k , c ), __x .tolist (), __y .tolist (), predicted_y . tolist ()
161
175
162
176
@staticmethod
163
177
def logistic_linearisation (y ):
178
+ if y == 'formula' :
179
+ return "ln(y^(-1) - 1) = kx + c"
164
180
y = numpy .array (y )
165
181
return numpy .log (y ** (- 1 ) - 1 )
166
182
167
183
@classmethod
168
184
def lsq_logistic_fit (cls , _x , _y ):
169
185
y = cls .logistic_linearisation (_y )
170
- x = _x
186
+ x = numpy . array ( _x )
171
187
k , c = numpy .polyfit (x , y , 1 )
172
188
__x = numpy .array (list (range (len (_x ) * 2 )))
173
189
__x = __x / (max (__x ) - min (__x )) * (max (x ) - min (x )) + min (x )
174
190
__y = 1 / (1 + numpy .exp (k * __x + c ))
191
+ predicted_y = 1 / (1 + numpy .exp (k * x + c ))
175
192
176
193
return "y = 1 / (1 + e^(kx + c))<br> k = {}, c = {}" .format (
177
- k , c ), __x .tolist (), __y .tolist ()
194
+ k , c ), __x .tolist (), __y .tolist (), predicted_y . tolist ()
178
195
179
196
180
197
class PlotGraph (metaclass = GraphPlotter ):
0 commit comments