Skip to content

Commit 6415a5e

Browse files
committed
New:
Calculate Pearsonr values
1 parent 852afd6 commit 6415a5e

File tree

1 file changed

+37
-20
lines changed

1 file changed

+37
-20
lines changed

utilities/plot_learning_rate.py

Lines changed: 37 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import numpy
1515
import plotly
1616
import scipy.optimize
17+
import scipy.stats
1718

1819

1920
PROCESS_COUNT = int(os.cpu_count() / 2)
@@ -37,26 +38,32 @@ def __call__(cls, path, _data):
3738
_data['max'].extend([0] * cls.origins)
3839
_data['min'].extend([0] * cls.origins)
3940

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'])
4143
data = dict(
4244
x=_data['x'][:-cls.origins] or _data['x'],
4345
y=_data['y'][:-cls.origins] or _data['y'],
4446
max=_data['max'][:-cls.origins] or _data['max'],
4547
min=_data['min'][:-cls.origins] or _data['min'])
4648

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")
5154

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)
5461

5562
cls.counter += 1
5663
cls.lock = False
5764

5865
@classmethod
59-
def bar(cls, path, _data, formula, fit_x, fit_y):
66+
def bar(cls, path, _data, formula, fit_x, fit_y, **kwargs):
6067
data = [
6168
plotly.graph_objs.Scatter(
6269
x=_data['x'],
@@ -81,7 +88,9 @@ def bar(cls, path, _data, formula, fit_x, fit_y):
8188
title="{}<br>{}".format(
8289
path[path.rfind('/') + 1:].replace(
8390
'.learning_rate.result.json.', ' '),
84-
formula))
91+
formula) + "".join(
92+
["<br>{}: {}".format(key, value)
93+
for key, value in kwargs.items()]))
8594
fig = plotly.graph_objs.Figure(data=data, layout=layout)
8695
plotly.offline.plot(
8796
fig,
@@ -91,7 +100,7 @@ def bar(cls, path, _data, formula, fit_x, fit_y):
91100
filename="{}.error_bar.html".format(path))
92101

93102
@classmethod
94-
def scatter(cls, path, _data, formula, fit_x, fit_y):
103+
def scatter(cls, path, _data, formula, fit_x, fit_y, **kwargs):
95104
data = [
96105
plotly.graph_objs.Scatter(
97106
x=_data['x'],
@@ -109,7 +118,9 @@ def scatter(cls, path, _data, formula, fit_x, fit_y):
109118
title="{}<br>{}".format(
110119
path[path.rfind('/') + 1:].replace(
111120
'.learning_rate.result.json.', ' '),
112-
formula))
121+
formula) + "".join(
122+
["<br>{}: {}".format(key, value)
123+
for key, value in kwargs.items()]))
113124
fig = plotly.graph_objs.Figure(data=data, layout=layout)
114125
plotly.offline.plot(
115126
fig,
@@ -124,57 +135,63 @@ def exponenial_func(x, a, b, c):
124135

125136
@classmethod
126137
def exp_fit(cls, _x, _y):
127-
x = _x
128-
y = _y
138+
y = numpy.array(_y)
139+
x = numpy.array(_x)
129140
a, b, c = scipy.optimize.curve_fit(cls.exponenial_func, _x, _y)[0]
130141
__x = numpy.array(list(range(len(_x) * 2)))
131142
__x = __x / (max(__x) - min(__x)) * (max(x) - min(x)) + min(x)
132143
__y = cls.exponenial_func(__x, a, b, c)
144+
predicted_y = cls.exponenial_func(x, a, b, c)
133145

134146
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()
136148

137149
@classmethod
138150
def lsq_exp_fit(cls, _x, _y):
139151
y = 1 - numpy.array(_y)
140152
y = numpy.log(y)
141-
x = _x
153+
x = numpy.array(_x)
142154
k, e = numpy.polyfit(x, y, 1)
143155
__x = numpy.array(list(range(len(_x) * 2)))
144156
__x = __x / (max(__x) - min(__x)) * (max(x) - min(x)) + min(x)
145157
__y = 1 - numpy.e ** (k * __x + e)
158+
predicted_y = 1 - numpy.e ** (k * x + e)
146159

147160
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()
149162

150163
@classmethod
151164
def lsq_ln_fit(cls, _x, _y):
152165
y = numpy.log(_y)
153-
x = _x
166+
x = numpy.array(_x)
154167
k, c = numpy.polyfit(x, y, 1)
155168
__x = numpy.array(list(range(len(_x) * 2)))
156169
__x = __x / (max(__x) - min(__x)) * (max(x) - min(x)) + min(x)
157170
__y = numpy.exp(k * __x + c)
171+
predicted_y = numpy.exp(k * x + c)
158172

159173
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()
161175

162176
@staticmethod
163177
def logistic_linearisation(y):
178+
if y == 'formula':
179+
return "ln(y^(-1) - 1) = kx + c"
164180
y = numpy.array(y)
165181
return numpy.log(y**(-1) - 1)
166182

167183
@classmethod
168184
def lsq_logistic_fit(cls, _x, _y):
169185
y = cls.logistic_linearisation(_y)
170-
x = _x
186+
x = numpy.array(_x)
171187
k, c = numpy.polyfit(x, y, 1)
172188
__x = numpy.array(list(range(len(_x) * 2)))
173189
__x = __x / (max(__x) - min(__x)) * (max(x) - min(x)) + min(x)
174190
__y = 1 / (1 + numpy.exp(k * __x + c))
191+
predicted_y = 1 / (1 + numpy.exp(k * x + c))
175192

176193
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()
178195

179196

180197
class PlotGraph(metaclass=GraphPlotter):

0 commit comments

Comments
 (0)