from lattices import SCC_Lattice, VO2_Lattice from spin_image import SpinImage import numpy as np import matplotlib.pyplot as plt import matplotlib.patches as patches import matplotlib import scipy import scipy.signal import tqdm from extractors import Rect_Evaluator, Voronoi_Evaluator, Image_Wrapper class Plotter: def __init__(self, lat): self.lattice = lat self.length_2 = 0.05 def reduce(self, arr): arr = np.array(arr) arr = arr.flatten() return np.mean(arr) # return np.sum(arr[np.argpartition(arr, -8)[-8:]]) def rect_at_point(self, x, y, color): length_2 = self.length_2 rect = patches.Rectangle( (x - length_2, y - length_2), 2 * length_2, 2 * length_2, linewidth=1, edgecolor=color, facecolor="none", ) return rect def plot(self, freqx, freqy, intens, ax_log=None, ax_lin=None, vmax=None): if ax_log: t = ax_log.imshow( intens, extent=(np.min(freqx), np.max(freqx), np.min(freqy), np.max(freqy)), norm=matplotlib.colors.LogNorm(vmin=10), cmap="viridis", origin="lower" ) plt.colorbar(t, ax=ax_log) if ax_lin: t = ax_lin.imshow( intens, extent=(np.min(freqx), np.max(freqx), np.min(freqy), np.max(freqy)), #vmax=vmax, cmap="viridis", origin="lower" ) plt.colorbar(t, ax=ax_lin) def rotate(x, y, angle): radian = angle / 180 * 2 * np.pi return np.cos(radian) * x - np.sin(radian) * y, np.sin(radian) * x + np.cos(radian) * y def test_square(): LEN = 40 # lat = SCC_Lattice(LEN, LEN) lat = VO2_Lattice(LEN, LEN) plot = Plotter(lat) pos_x, pos_y = lat.get_from_mask(np.zeros((40, 40))) # pos_x, pos_y = rotate(pos_x, pos_y, 30) si = SpinImage(pos_x, pos_y) fig, axs = plt.subplots(2, 2) si.pad_it_square(10) si.plot(axs[0, 0], 2) # si.gaussian(LEN) # si.blur(3) si.plot(axs[0, 1], 2) plt.pause(0.1) fx, fy, intens = si.fft() plot.plot(fx, fy, intens, axs[1, 0], axs[1, 1]) plt.savefig("test.png") plt.show() def test_mixed(): LEN = 40 lat = VO2_Lattice(LEN, LEN) plot = Plotter(lat) all_rutile = np.stack(lat.reci()[0]).T all_mono = np.stack(lat.reci()[1]).T all_mono2 = np.stack(lat.reci()[2]).T rect = Voronoi_Evaluator([all_rutile, all_mono, all_mono2]) pos_x, pos_y = lat.get_from_mask(np.zeros((40, 40))) si = SpinImage(pos_x, pos_y) si.pad_it_square(10) fx, fy, intens_rutile = si.fft() pos_x, pos_y = lat.get_from_mask(np.ones((40, 40))) si = SpinImage(pos_x, pos_y) si.pad_it_square(10) fx, fy, intens_mono = si.fft() mask_misk = np.ones((40, 40)) ind = np.arange(mask_misk.size) np.random.shuffle(ind) mask_misk[np.unravel_index(ind[:800], (40, 40))] = False pos_x, pos_y = lat.get_from_mask(mask_misk) si = SpinImage(pos_x, pos_y) si.pad_it_square(10) fx, fy, intens_mixed = si.fft() intens_rutile = rect.extract_paint( Image_Wrapper(intens_rutile, fx=fx, fy=fy)) intens_mono = rect.extract_paint( Image_Wrapper(intens_mono, fx=fx, fy=fy)) intens_mixed = rect.extract_paint( Image_Wrapper(intens_mixed, fx=fx, fy=fy)) fig, axs = plt.subplots(2, 3) plot.plot(freqx=fx, freqy=fy, intens=intens_rutile, ax_log=axs[0, 0], ax_lin=axs[1, 0], vmax=10e7) plot.plot(freqx=fx, freqy=fy, intens=intens_mono, ax_log=axs[0, 2], ax_lin=axs[1, 2], vmax=10e7) plot.plot(freqx=fx, freqy=fy, intens=intens_mixed, ax_log=axs[0, 1], ax_lin=axs[1, 1], vmax=10e7) for ax in axs.flatten(): ax.set_xlim(-1, 1) ax.set_ylim(-1, 1) plt.show() def random(seed): np.random.seed(seed) LEN = 40 lat = VO2_Lattice(LEN, LEN) plot = Plotter(lat) maske = np.zeros((LEN, LEN)) ind = np.arange(LEN * LEN) np.random.shuffle(ind) reci_lattice = lat.reci() out = [[] for x in range(len(reci_lattice))] percentage = [] counter = 0 for i in tqdm.tqdm(ind): maske[np.unravel_index(i, (LEN, LEN))] = True counter += 1 if np.mod(counter, 100) != 0: continue pos_x, pos_y = lat.get_from_mask(maske) si = SpinImage(pos_x, pos_y) si.pad_it_square(10) fx, fy, intens = si.fft() for tup, lis in zip(reci_lattice, out): point_x, point_y = tup point_x = point_x.flatten() point_y = point_y.flatten() peaks = [] for px, py in zip(point_x, point_y): peaks.append(np.sum(plot.extract_rect(intens, px, py, fx, fy))) lis.append(peaks) percentage.append(np.sum(maske)) percentage = np.array(percentage) percentage /= np.max(percentage) np.savez(f"random_{seed}.npz", percentage=percentage, out=out) def sample_index(p): i = np.random.choice(np.arange(p.size), p=p.ravel()) return np.unravel_index(i, p.shape) def ising(seed): np.random.seed(seed) LEN = 80 temp = 0.1 maske = np.zeros((LEN, LEN), dtype=bool) lat = VO2_Lattice(LEN, LEN) plot = Plotter(lat) reci_lattice = lat.reci() out = [[] for x in range(len(reci_lattice))] percentage = [] counter = 0 for i in tqdm.tqdm(range(LEN*LEN)): probability = np.roll(maske, 1, axis=0).astype(float) probability += np.roll(maske, -1, axis=0).astype(float) probability += np.roll(maske, 1, axis=1).astype(float) probability += np.roll(maske, -1, axis=1).astype(float) probability = np.exp(probability/temp) probability[maske] = 0 probability /= np.sum(probability) maske[sample_index(probability)] = True counter += 1 if np.mod(counter, 100) != 0: continue pos_x, pos_y = lat.get_from_mask(maske) si = SpinImage(pos_x, pos_y) si.pad_it_square(10) # si.gaussian(LEN) fx, fy, intens = si.fft() for tup, lis in zip(reci_lattice, out): point_x, point_y = tup point_x = point_x.flatten() point_y = point_y.flatten() peaks = [] for px, py in zip(point_x, point_y): peaks.append(np.sum(plot.extract_rect(intens, px, py, fx, fy))) lis.append(peaks) percentage.append(np.mean(maske)) percentage = np.array(percentage) percentage /= np.max(percentage) np.savez(f"ising_{temp}_{seed}.npz", percentage=percentage, out=out) # for o in out: # plt.scatter(percentage, o/o[0]) # plt.plot([0, 1], [1, o[-1]/o[0]]) # plt.pause(1) if __name__ == "__main__": # test_square() test_mixed() plt.show() # random() np.random.seed(1234) for i in np.random.randint(0, 10000, 2): random(i) ising(i) plt.show()