-
Notifications
You must be signed in to change notification settings - Fork 0
/
AdditionalFunc.py
148 lines (120 loc) · 3.97 KB
/
AdditionalFunc.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
import numpy as np
# Geometric methods
def centroid(vertexes):
x = np.array([coord[0] for coord in vertexes])
y = np.array([coord[1] for coord in vertexes])
cx, cy, A = 0, 0, 0
for i in range(len(x)-1):
cx += (x[i] + x[i+1])*(x[i]*y[i+1]-x[i+1]*y[i])
cy += (y[i] + y[i+1])*(x[i]*y[i+1]-x[i+1]*y[i])
A += (x[i]*y[i+1]-x[i+1]*y[i])
A = 0.5*A
cx = cx/(6*A)
cy = cy/(6*A)
return np.array([cx, cy])
def linear_distance(a, b):
return np.sqrt((a[0]-b[0])**2+(a[1]-b[1])**2)
def MassCenter1D(arr):
mass = 0
for i, v in enumerate(arr):
mass += v*i
if np.sum(arr)==0:
return 0
else:
return mass/np.sum(arr)
def PeakIsolation(arr, max_ind, buffer):
# Scan toward left.
progress = True
ind = 0
tmp = arr[max_ind]
left_mini = 0
while progress==True:
ind+=1
if int(max_ind-ind)==-1:
left_mini = int(max_ind-ind+1)
#print('minimum at boundary.')
left_len = ind-1
progress = False
elif arr[int(max_ind-ind)]>arr.mean() or arr[int(max_ind-ind)]<tmp:
tmp = arr[int(max_ind-ind)]
else:
try:
B = arr[int(max_ind-ind-np.arange(buffer))]
except:
B = arr[:int(max_ind-ind)]
if np.all(B>=tmp):
left_mini = int(max_ind-ind+1)
left_len = ind-1
progress = False
# Scan toward right.
progress = True
ind = 0
tmp = arr[max_ind]
right_mini = 0
while progress==True:
ind+=1
if int(max_ind+ind)==len(arr)-1:
#print('right minimum at boundary!!!')
right_mini = int(max_ind+left_len)
progress = False
elif arr[int(max_ind+ind)]<tmp or arr[int(max_ind+ind)]>arr.mean():
tmp = arr[int(max_ind+ind)]
else:
try:
B = arr[int(max_ind+ind+np.arange(buffer))]
#print(B)
except:
B = arr[int(max_ind+ind):]
if np.all(B>=tmp):
if ind-1 < left_len:
right_mini = int(max_ind+left_len)
else:
right_mini = int(max_ind+ind-1)
progress = False
return (left_mini, right_mini)
#return (int(max_ind-5), int(max_ind+5))
def confidence_ellipse(x, y, ax, n_std=3.0, facecolor='none', **kwargs):
"""
Create a plot of the covariance confidence ellipse of `x` and `y`
Parameters
----------
x, y : array_like, shape (n, )
Input data.
ax : matplotlib.axes.Axes
The axes object to draw the ellipse into.
n_std : float
The number of standard deviations to determine the ellipse's radiuses.
Returns
-------
matplotlib.patches.Ellipse
Other parameters
----------------
kwargs : `~matplotlib.patches.Patch` properties
"""
if len(x) != len(y):
raise ValueError("x and y must be the same size")
cov = np.cov(x, y)
pearson = cov[0, 1]/np.sqrt(cov[0, 0] * cov[1, 1])
# Using a special case to obtain the eigenvalues of this
# two-dimensionl dataset.
ell_radius_x = np.sqrt(1 + pearson)
ell_radius_y = np.sqrt(1 - pearson)
ellipse = Ellipse((0, 0),
width=ell_radius_x * 2,
height=ell_radius_y * 2,
facecolor=facecolor,
**kwargs)
# Calculating the stdandard deviation of x from
# the squareroot of the variance and multiplying
# with the given number of standard deviations.
scale_x = np.sqrt(cov[0, 0]) * n_std
mean_x = np.mean(x)
# calculating the stdandard deviation of y ...
scale_y = np.sqrt(cov[1, 1]) * n_std
mean_y = np.mean(y)
transf = transforms.Affine2D() \
.rotate_deg(45) \
.scale(scale_x, scale_y) \
.translate(mean_x, mean_y)
ellipse.set_transform(transf + ax.transData)
return ax.add_patch(ellipse)