add: Bind Filter and Fit
This commit is contained in:
parent
3dafde3a67
commit
f3931069bd
1
.gitignore
vendored
1
.gitignore
vendored
@ -11,6 +11,7 @@ venv/
|
||||
|
||||
# build cache
|
||||
build/
|
||||
__pycache__
|
||||
|
||||
# config
|
||||
.vscode
|
||||
|
@ -1,36 +0,0 @@
|
||||
import os
|
||||
import numpy as np
|
||||
from tqdm import tqdm
|
||||
from matplotlib import pyplot as plt
|
||||
from sklearn.mixture import GaussianMixture
|
||||
|
||||
path = 'result/bind'
|
||||
file_list = os.listdir(path)
|
||||
|
||||
pbar = tqdm(total=len(file_list))
|
||||
|
||||
for file in file_list:
|
||||
name = file.split('.')[0]
|
||||
file = os.path.join(path, file)
|
||||
data = np.loadtxt(file, dtype=np.uint16)
|
||||
x = data[:, 0]
|
||||
y = data[:, 1]
|
||||
|
||||
model = GaussianMixture(n_components=2)
|
||||
model.fit(data)
|
||||
|
||||
nx = np.array([])
|
||||
ny = model.predict(data)
|
||||
fig = plt.figure(figsize=(8, 8))
|
||||
for cluster in np.unique(ny):
|
||||
idx = np.where(ny == cluster)[0]
|
||||
nx = idx if len(idx) > len(nx) else nx
|
||||
plt.scatter(data[idx, 0], data[idx, 1], s=0.1)
|
||||
fig.savefig('result/GMM/' + name + '.png')
|
||||
plt.close()
|
||||
|
||||
np.savetxt('result/bind-GMM/' + name + '.txt', data[nx], fmt='%d')
|
||||
|
||||
pbar.update(1)
|
||||
|
||||
pbar.close()
|
30
main.py
Normal file
30
main.py
Normal file
@ -0,0 +1,30 @@
|
||||
import os
|
||||
import re
|
||||
import numpy as np
|
||||
from tqdm import tqdm
|
||||
from qdx import BindFilter, Bind
|
||||
|
||||
binds = []
|
||||
BF = BindFilter()
|
||||
|
||||
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))
|
||||
|
||||
for file in file_list:
|
||||
name, E, n, m = reg.match(file).groups()
|
||||
file = os.path.join(path, file)
|
||||
|
||||
BF(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))
|
||||
|
||||
pbar.update(1)
|
||||
|
||||
break
|
||||
|
||||
pbar.close()
|
28
qdx/Bind.py
Normal file
28
qdx/Bind.py
Normal file
@ -0,0 +1,28 @@
|
||||
from cProfile import label
|
||||
import numpy as np
|
||||
import matplotlib.pyplot as plt
|
||||
from sklearn.linear_model import LinearRegression
|
||||
|
||||
|
||||
class Bind(object):
|
||||
k, b = 0, 0
|
||||
|
||||
def __init__(self, E, n, m, data):
|
||||
self.E = E
|
||||
self.n, self.m = n, m
|
||||
self.x, self.y = data[:, 0], data[:, 1]
|
||||
|
||||
def fit(self):
|
||||
self.reg = LinearRegression()
|
||||
self.reg.fit(self.x, self.y)
|
||||
self.k = self.reg.coef_[0][0]
|
||||
self.b = self.reg.intercept_[0]
|
||||
|
||||
def draw(self, title):
|
||||
fig = plt.figure(figsize=(8, 8))
|
||||
ax = fig.add_subplot(1, 1, 1)
|
||||
ax.scatter(self.x, self.y, s=0.1, c='k', label='raw')
|
||||
ax.plot(self.x, self.reg.predict(self.x), c='r', label='fit')
|
||||
ax.legend()
|
||||
fig.savefig(title)
|
||||
plt.close()
|
33
qdx/BindFilter.py
Normal file
33
qdx/BindFilter.py
Normal file
@ -0,0 +1,33 @@
|
||||
import numpy as np
|
||||
from matplotlib import pyplot as plt
|
||||
from sklearn.mixture import GaussianMixture
|
||||
|
||||
|
||||
class BindFilter(object):
|
||||
def __init__(self):
|
||||
pass
|
||||
|
||||
def fit(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.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()
|
11
qdx/Block.py
Normal file
11
qdx/Block.py
Normal file
@ -0,0 +1,11 @@
|
||||
from .Bind import Bind
|
||||
|
||||
|
||||
class Block(object):
|
||||
binds = []
|
||||
|
||||
def __init__(self) -> None:
|
||||
pass
|
||||
|
||||
def addBind(self, b: Bind):
|
||||
self.binds.append(b)
|
2
qdx/__init__.py
Normal file
2
qdx/__init__.py
Normal file
@ -0,0 +1,2 @@
|
||||
from .Bind import Bind
|
||||
from .BindFilter import BindFilter
|
Loading…
Reference in New Issue
Block a user