add: fit2 and fit3;finish frame work

This commit is contained in:
liuyihui 2022-07-11 16:24:14 +08:00
parent cd286c72f1
commit 54c5277587
11 changed files with 195 additions and 73 deletions

BIN
Figure-0.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 30 KiB

BIN
Figure-1.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 30 KiB

BIN
Figure-2.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 30 KiB

BIN
Figure-3.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 31 KiB

BIN
Figure-4.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 31 KiB

40
cal.txt Normal file
View File

@ -0,0 +1,40 @@
0 0 0.01006 0.01002 -1.29111
0 1 0.00994 0.01009 -1.08340
0 2 0.01050 0.01024 -1.15601
0 3 0.00998 0.00984 -1.19307
0 4 0.01023 0.01009 -0.97504
0 5 0.00999 0.01011 -1.18887
0 6 0.01008 0.00989 -0.90181
0 7 0.00998 0.01002 -1.31894
1 0 0.01009 0.00998 -0.84118
1 1 0.01000 0.00988 -1.32354
1 2 0.00998 0.00997 -1.41626
1 3 0.00977 0.00994 -1.38833
1 4 0.01003 0.01026 -1.35276
1 5 0.00997 0.00981 -1.18387
1 6 0.00995 0.00987 -1.02832
1 7 0.01036 0.01005 -0.56795
2 0 0.01019 0.01026 -0.09668
2 1 0.01021 0.01024 -0.41860
2 2 0.01033 0.01006 0.23775
2 3 0.01027 0.01000 -0.47241
2 4 0.01003 0.01032 -0.22038
2 5 0.01030 0.01034 -0.39546
2 6 0.01004 0.01014 -0.29457
2 7 0.01089 0.01086 -0.92415
3 0 0.01071 0.01037 -0.46705
3 1 0.01092 0.01074 -1.06885
3 2 0.01053 0.01067 -0.68895
3 3 0.01059 0.01080 -0.96538
3 4 0.01098 0.01058 -0.51257
3 5 0.01117 0.01110 -1.20546
3 6 0.01042 0.01085 -1.06498
3 7 0.01055 0.01023 -1.16872
4 0 0.01017 0.01043 -0.73180
4 1 0.01034 0.01005 -0.89867
4 2 0.01039 0.01029 -0.59282
4 3 0.01038 0.01029 -0.83104
4 4 0.01019 0.01043 -0.56346
4 5 0.01065 0.01008 -1.12639
4 6 0.01041 0.01034 -0.64923
4 7 0.01000 0.01019 -1.03055

80
main.py
View File

@ -1,33 +1,79 @@
import os
import re
import numpy as np
from qdx import Bind
from qdx.utils import get_hist, file_filter
from tqdm import tqdm
from qdx import BindFilter, Bind
binds = []
BF = BindFilter()
from matplotlib import pyplot as plt
path = 'result/bind'
file_list = os.listdir(path)
reg = re.compile(r'(([0-9]{4})-([0-9])-([0-9])).txt')
pbar = tqdm(desc="Gaussian Mixture Bind Filter", total=len(file_list))
n, m = 5, 8
binds = [[j for j in range(m)] for i in range(n)]
for i in range(n):
for j in range(m):
binds[i][j] = Bind(i, j)
pbar = tqdm(desc="Gaussian Mixture Filter", total=len(file_list))
for file in file_list:
name, E, n, m = reg.match(file).groups()
bind = binds[int(i)][int(j)]
name, E, i, j = reg.match(file).groups()
file = os.path.join(path, file)
BF.filter(file)
# BF.draw('result/GMM/' + name + '.png')
# np.savetxt('result/bind-GMM/' + name + '.txt', BF.fit_data, fmt='%d')
binds.append(Bind(int(E), int(n), int(m), BF.fit_data))
_, data = file_filter(file)
# bind.draw_filter('result/GMM/' + name + '.png')
# np.savetxt('result/bind-GMM/' + name + '.txt', data, fmt='%d')
bind.add_data(int(E) / 100, data)
pbar.update(1)
pbar.close()
pbar = tqdm(desc="Bind Linear Fit", total=len(binds))
for bind in binds:
bind.fit()
bind.draw('result/L-FIT/' + bind.name + '.png')
pbar.update(1)
split_n = [8, 7, 7, 8, 8]
pbar = tqdm(desc="Bind Process", total=n * m)
for i in range(n):
for j in range(m):
bind: Bind = binds[i][j]
bind.filter()
bind.fit1()
bind.solve()
bind.split(n=split_n[i])
bind.fit2()
bind.draw_fit1('result/FIT1/' + bind.name + '.png')
bind.draw_fit2('result/FIT2/' + bind.name + '.png')
pbar.update(1)
pbar.close()
y = [
[620, 610, 600, 590, 580, 570, 560, 550],
[485, 475, 465, 455, 445, 435, 425],
[360, 350, 340, 330, 320, 310, 300],
[235, 225, 215, 205, 195, 185, 175, 165],
[100, 90, 80, 70, 60, 50, 40, 30]
]
for i in range(n):
k = 1
fig = plt.figure(figsize=(14, 6))
for j in range(m):
ax = fig.add_subplot(int(m / 2), 2, k)
bind = binds[i][j]
bind.fit3(y[i])
ny = bind.reg3.predict(bind.x1)
c, e = get_hist(ny, delta=0.5)
ax.scatter(e, c, s=0.5)
k += 1
fig.savefig('./Figure-' + str(i) + '.png', facecolor='w', transparent=False)
plt.tight_layout()
plt.close()
f = open('cal.txt', 'w')
for i in range(n):
for j in range(m):
bind = binds[i][j]
f.writelines('{:d} {:d} {:.5f} {:.5f} {:.5f}\n'.format(i, j, bind.k1, bind.k2, bind.b))

View File

@ -3,16 +3,7 @@ import matplotlib.pyplot as plt
from astropy.modeling import models, fitting
from sklearn.mixture import GaussianMixture
from sklearn.linear_model import LinearRegression
def get_hist(data, maxN=50):
step = 1
edge = np.arange(data.min(), data.max() + 1, step)
count, _ = np.histogram(data, bins=edge)
while count.max() <= maxN:
step += 1
edge = np.arange(data.min(), data.max() + 1, step)
count, _ = np.histogram(data, bins=edge)
return count, (edge[1:] + edge[:-1]) / 2
from .utils import get_hist
class Bind(object):
flag = 0
@ -32,7 +23,7 @@ class Bind(object):
self.E2 = E
self.x2, self.y2 = data[:, 0].reshape(-1, 1), data[:, 1].reshape(-1, 1)
def fit(self):
def fit1(self):
self.reg1 = LinearRegression()
self.reg1.fit(self.x1, self.y1)
self.K1 = self.reg1.coef_[0][0]
@ -43,13 +34,26 @@ class Bind(object):
self.K2 = self.reg2.coef_[0][0]
self.C2 = self.reg2.intercept_[0]
def filter(self):
self.fit1()
ny1 = self.y1 - self.reg1.predict(self.x1)
ny2 = self.y2 - self.reg2.predict(self.x2)
idx1 = np.where(ny1 <= ny1.std())[0]
idx2 = np.where(ny2 <= ny2.std())[0]
self.x1 = self.x1[idx1]
self.y1 = self.y1[idx1]
self.x2 = self.x2[idx2]
self.y2 = self.y2[idx2]
def solve(self):
self.k1 = (self.E1 - self.E2) / (self.C1 - self.C2)
self.k2 = -(self.K1 + self.K2) / (self.k1 * 2)
self.k2 = -(self.K1 + self.K2) * self.k1 / 2
self.b = (self.E1 - self.C1 * self.k1 + self.E2 - self.C2 * self.k1) / 2
def split(self, n=7):
self.cluster = []
self.clusters = []
data = np.concatenate((self.x1, self.y1), axis=1)
model = GaussianMixture(n_components=n)
@ -58,16 +62,17 @@ class Bind(object):
ny = model.predict(data)
for i in np.unique(ny):
idx = np.where(ny == i)[0]
self.cluster.append(data[idx])
self.clusters.append(data[idx])
self.cluster = np.array(self.cluster)
self.clusters = np.array(self.clusters, dtype=object)
self.clusters = sorted(self.clusters, key=lambda x: len(x))
def fit2(self):
self.fx = []
self.fy = []
fitter = fitting.LevMarLSQFitter()
for data in self.cluster:
for data in self.clusters:
x1, x2 = data[:, 0], data[:, 1]
c1, e1 = get_hist(x1)
c2, e2 = get_hist(x2)
@ -78,12 +83,16 @@ class Bind(object):
self.fx.append(fitted_model1.mean.value)
self.fy.append(fitted_model2.mean.value)
def pX(self, x1=None, x2=None):
x1 = x1 if x1 else self.x1
x2 = x2 if x2 else self.y1
self.px = (self.k2 * x2 - self.k1 * x1) * self.L / self.E1 + self.C
idx = np.argsort(self.fx)
def draw(self, title):
def fit3(self, y):
self.fx = np.sort(self.fx)[::-1].reshape(-1, 1)
self.reg3 = LinearRegression()
self.reg3.fit(self.fx, y)
self.K3 = self.reg3.coef_[0]
self.C3 = self.reg3.intercept_
def draw_fit1(self, title):
fig = plt.figure(figsize=(8, 8))
ax = fig.add_subplot(1, 1, 1)
ax.scatter(self.x1, self.y1, s=0.1, c='black', label=r'$E_1$')
@ -94,9 +103,32 @@ class Bind(object):
fig.savefig(title, facecolor='w', transparent=False)
plt.close()
def draw_split(self, title):
fig = plt.figure(figsize=(8, 8))
ax = fig.add_subplot(1, 1, 1)
for data in self.clusters:
ax.scatter(data[:, 0], data[:, 1], s=0.1)
fig.savefig(title, facecolor='w', transparent=False)
plt.close()
def draw_fit2(self, title):
k, n = 1, len(self.clusters)
fig = plt.figure(figsize=(8, 10))
for data in self.clusters:
ax = fig.add_subplot(n, 1, k)
x1, x2 = data[:, 0], data[:, 1]
c1, e1 = get_hist(x1)
c2, e2 = get_hist(x2)
ax.scatter(e1, c1, s=0.1)
ax.scatter(e2, c2, s=0.1)
k += 1
plt.tight_layout()
fig.savefig(title, facecolor='w', transparent=False)
plt.close()
@property
def name(self):
return '{:d}-{:d}-{:d}-{:d}'.format(self.E1, self.E2, self.n, self.m)
return '{:.1f}-{:.1f}-{:d}-{:d}'.format(self.E1, self.E2, self.n, self.m)
@property
def RSquare1(self):

View File

@ -1,33 +0,0 @@
import numpy as np
from matplotlib import pyplot as plt
from sklearn.mixture import GaussianMixture
class BindFilter(object):
def __init__(self):
pass
def filter(self, file):
self.clusters = []
self.fit_data = np.array([])
data = np.loadtxt(file, dtype=np.uint16)
model = GaussianMixture(n_components=2)
model.fit(data)
ny = model.predict(data)
for i in np.unique(ny):
idx = np.where(ny == i)[0]
self.fit_data = idx if len(idx) > len(self.fit_data) else self.fit_data
self.clusters.append(data[idx])
self.data = data
self.fit_data = data[self.fit_data]
def draw(self, title):
fig = plt.figure(figsize=(8, 8))
ax = fig.add_subplot(1, 1, 1)
for cluster in self.clusters:
ax.scatter(cluster[:, 0], cluster[:, 1], s=0.1)
fig.savefig(title)
plt.close()

View File

@ -1,2 +1 @@
from .Bind import Bind
from .BindFilter import BindFilter

38
qdx/utils.py Normal file
View File

@ -0,0 +1,38 @@
import numpy as np
import matplotlib.pyplot as plt
from sklearn.mixture import GaussianMixture
def file_filter(file):
filter_data = []
fit_data = np.array([])
data = np.loadtxt(file, dtype=np.uint16)
model = GaussianMixture(n_components=2)
model.fit(data)
ny = model.predict(data)
for i in np.unique(ny):
idx = np.where(ny == i)[0]
fit_data = idx if len(idx) > len(fit_data) else fit_data
filter_data.append(data[idx])
return filter_data, data[fit_data]
def draw_filter(data, title):
fig = plt.figure(figsize=(8, 8))
ax = fig.add_subplot(1, 1, 1)
for cluster in data:
ax.scatter(cluster[:, 0], cluster[:, 1], s=0.1)
fig.savefig(title, facecolor='w', transparent=False)
plt.close()
def get_hist(data, delta=1, maxN=50):
step = delta
edge = np.arange(data.min(), data.max() + 1, step)
count, _ = np.histogram(data, bins=edge)
while count.max() <= maxN:
step += delta
edge = np.arange(data.min(), data.max() + 1, step)
count, _ = np.histogram(data, bins=edge)
return count, (edge[1:] + edge[:-1]) / 2