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