-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrescale.py
136 lines (120 loc) · 3.42 KB
/
rescale.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
import matplotlib.pylab as plt
import math
def _rescale_logy(x, y0, y1, plot=False):
"""
usage: _rescale_logy(range(1, 5000), 1, 10, True)
:param x: number list
:param y0: min
:param y1: max
:param plot: True or False
:return: transformed version of x
"""
x1 = max(x)
x0 = min(x)
beta1 = (math.log(y1) - math.log(y0)) / (x1 - x0)
beta0 = math.log(y1) - x1 * beta1
lnx = [beta0 + beta1 * el for el in x]
xtransforme = [math.exp(el) for el in lnx]
if plot:
plt.plot(x, xtransforme)
return xtransforme
def _rescale_log10y(x, y0, y1, plot=False):
"""
usage: _rescale_log10y(range(1, 5000), 1, 10, True)
"""
x1 = max(x)
x0 = min(x)
beta1 = (math.log10(y1) - math.log10(y0)) / (x1 - x0)
beta0 = math.log10(y1) - x1 * beta1
lnx = [beta0 + beta1 * el for el in x]
xtransforme = [10**el for el in lnx]
if plot:
plt.plot(x, xtransforme)
return xtransforme
def _rescale_log2y(x, y0, y1):
"""
usage: _rescale_log10y(range(1, 5000), 1, 10, True)
"""
x1 = max(x)
x0 = min(x)
x1 = max(x)
x0 = min(x)
beta1 = (math.log2(y1) - math.log2(y0)) / (x1 - x0)
beta0 = math.log2(y1) - x1 * beta1
lnx = [beta0 + beta1 * el for el in x]
xtransforme = [2**el for el in lnx]
if plot:
plt.plot(x, xtransforme)
return xtransforme
def _rescale_logx(x, y0, y1, plot=False):
"""
usage: _rescale_logx(range(1, 5000), 1, 10, True)
:param x:
:param y0:
:param y1:
:return:
"""
x1 = max(x)
x0 = min(x)
beta1 = (math.exp(y1) - math.exp(y0)) / (x1 - x0)
beta0 = math.exp(y1) - x1 * beta1
lnx = [beta0 + beta1 * el for el in x]
xtransforme = [math.log(el) for el in lnx]
if plot:
plt.plot(x, xtransforme)
return xtransforme
def _rescale_log10x(x, y0, y1, plot=False):
"""
usage: _rescale_log10x(range(1, 5000), 1, 10, True)
:param x:
:param y0:
:param y1:
:param plot:
:return:
"""
x1 = max(x)
x0 = min(x)
beta1 = (10**y1 - 10**y0) / (x1 - x0)
beta0 = 10**y1 - x1 * beta1
lnx = [beta0 + beta1 * el for el in x]
xtransforme = [math.log10(el) for el in lnx]
if plot:
plt.plot(x, xtransforme)
return xtransforme
def _rescale_log2x(x, y0, y1, plot=False):
"""
usage: _rescale_log2x(range(1, 5000), 1, 10, True)
"""
x1 = max(x)
x0 = min(x)
beta1 = (2**y1 - 2**y0) / (x1 - x0)
beta0 = 2**y1 - x1 * beta1
lnx = [beta0 + beta1 * el for el in x]
xtransforme = [math.log2(el) for el in lnx]
if plot:
plt.plot(x, xtransforme)
return xtransforme
def _rescale_linear(x, y0, y1, plot=False):
x1 = max(x)
x0 = min(x)
beta1 = (y1 - y0) / (x1 - x0)
beta0 = y1 - x1 * beta1
xtransforme = [beta0 + beta1 * el for el in x]
if plot:
plt.plot(x, xtransforme)
return xtransforme
def rescale(x, y0, y1, method="log2x", plot=False):
if method == "logx":
return _rescale_logx(x, y0, y1, plot)
elif method == "log10x":
return _rescale_log10x(x, y0, y1, plot)
elif method == "logy":
return _rescale_logy(x, y0, y1, plot)
elif method == "log2y":
return _rescale_log2y(x, y0, y1, plot)
elif method == "log10y":
return _rescale_log10y(x, y0, y1, plot)
elif method == "log2x":
return _rescale_log2x(x, y0, y1, plot)
else:
return _rescale_linear(x, y0, y1, plot)