From f3931069bde0442c04693bd76e710d5a80df4e73 Mon Sep 17 00:00:00 2001 From: YiHui Liu Date: Fri, 8 Jul 2022 21:21:48 +0800 Subject: [PATCH] add: Bind Filter and Fit --- .gitignore | 1 + GaussianMixture.py | 36 ------------------------------------ main.py | 30 ++++++++++++++++++++++++++++++ qdx/Bind.py | 28 ++++++++++++++++++++++++++++ qdx/BindFilter.py | 33 +++++++++++++++++++++++++++++++++ qdx/Block.py | 11 +++++++++++ qdx/__init__.py | 2 ++ 7 files changed, 105 insertions(+), 36 deletions(-) delete mode 100644 GaussianMixture.py create mode 100644 main.py create mode 100644 qdx/Bind.py create mode 100644 qdx/BindFilter.py create mode 100644 qdx/Block.py create mode 100644 qdx/__init__.py diff --git a/.gitignore b/.gitignore index 3c918f3..3c2fb15 100644 --- a/.gitignore +++ b/.gitignore @@ -11,6 +11,7 @@ venv/ # build cache build/ +__pycache__ # config .vscode diff --git a/GaussianMixture.py b/GaussianMixture.py deleted file mode 100644 index e12e29a..0000000 --- a/GaussianMixture.py +++ /dev/null @@ -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() diff --git a/main.py b/main.py new file mode 100644 index 0000000..1a4e04c --- /dev/null +++ b/main.py @@ -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() diff --git a/qdx/Bind.py b/qdx/Bind.py new file mode 100644 index 0000000..d502932 --- /dev/null +++ b/qdx/Bind.py @@ -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() diff --git a/qdx/BindFilter.py b/qdx/BindFilter.py new file mode 100644 index 0000000..c045261 --- /dev/null +++ b/qdx/BindFilter.py @@ -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() diff --git a/qdx/Block.py b/qdx/Block.py new file mode 100644 index 0000000..53aee42 --- /dev/null +++ b/qdx/Block.py @@ -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) diff --git a/qdx/__init__.py b/qdx/__init__.py new file mode 100644 index 0000000..8554d2d --- /dev/null +++ b/qdx/__init__.py @@ -0,0 +1,2 @@ +from .Bind import Bind +from .BindFilter import BindFilter