From 22220af6a079fcdd7b0f95f0c9adb85d1438624f Mon Sep 17 00:00:00 2001 From: Lizzzka007 <gashchuk2011@mail.ru> Date: Mon, 19 Jun 2023 18:16:20 +0300 Subject: [PATCH] Add contour ani --- plotter/Plotter.py | 77 +++++++++++++++++++++++++++++++++++++++++++-- plotter/ProcData.py | 1 - plotter/__init__.py | 1 + plotter/main.py | 16 +++++++++- setup.py | 10 +++--- 5 files changed, 96 insertions(+), 9 deletions(-) diff --git a/plotter/Plotter.py b/plotter/Plotter.py index b6351bc..aeff289 100755 --- a/plotter/Plotter.py +++ b/plotter/Plotter.py @@ -76,7 +76,7 @@ class Plotter: for p in self.data: p.process_file(ndim, self.var, self.mval) - elif args.func == self.plot_contour: + elif args.func == self.plot_contour or args.func == self.ani_plot_contour: ndim = 2 if self.var == None and len(self.variable_names) != 0: self.var = [self.variable_names[i] for i in range(ndim, len(self.variable_names))] @@ -175,6 +175,75 @@ class Plotter: else: plt.close(fig) if self.if_save_result: fig.savefig(self.out + fig_names[i] + fig_end) + def __get_min_max_bar(self): + self.filename = natsort.natsorted(self.filename,reverse=False) + self.vals = {var : [] for var in self.var} + + for var in self.var: + max_val = -1e9 + min_val = 1e9 + + for data in self.data: + maval = np.max(data.data[var]) + mival = np.min(data.data[var]) + + if maval > max_val: + max_val = maval + if mival < min_val: + min_val = mival + + self.vals[var] = np.array([min_val, max_val]) + + def __ani_plot_contour(self): + if self.if_save_result: + # png_names = {var:[] for var in self.var} + os.system("mkdir -p " + self.out) + os.system("mkdir -p " + self.ani_pngs_dir) + + x_name = self.variable_names[0] + y_name = self.variable_names[1] + + if self.oname == None: + fig_names = self.var + fig_end = ".gif" + else: + fig_names = self.oname + fig_end = "" + + X = self.data[0].data[x_name] + Y = self.data[0].data[y_name] + + i = 0 + for var in self.var: + title = var + counter = 0 + png_names = [] + + for data in self.data: + fig,ax=plt.subplots(1,1) + ax.set_title(title) + ax.set_xlabel(x_name) + ax.set_ylabel(y_name) + + Z = data.data[var] + cp = ax.contourf(X, Y, Z, vmin=self.vals[var][0], vmax=self.vals[var][1]) + fig.colorbar(cp) # Add a colorbar to a plot + plt.close(fig) + figname = var + str(counter) + fig.savefig(self.ani_pngs_dir + figname + '.png') + name = self.ani_pngs_dir + figname + '.png' + png_names.append(name) + counter += 1 + + images = [] + for file_name in png_names: + images.append(imageio.v2.imread(file_name)) + + imageio.mimsave(self.out + fig_names[i] + fig_end, images, duration = 5, ) + i += 1 + + shutil.rmtree(self.ani_pngs_dir) + def __avg(self, data, var_name): cx = data['cx'] cy = data['cy'] @@ -228,4 +297,8 @@ class Plotter: self.__avg_plot() def plot_contour(self): - self.__plot_contour() \ No newline at end of file + self.__plot_contour() + + def ani_plot_contour(self): + self.__get_min_max_bar() + self.__ani_plot_contour() \ No newline at end of file diff --git a/plotter/ProcData.py b/plotter/ProcData.py index 157c018..c340654 100755 --- a/plotter/ProcData.py +++ b/plotter/ProcData.py @@ -111,7 +111,6 @@ class ProcData: if self.mval != None: print(self.mval) for col, abs_val in zip(column_names, self.mval): - # print(col, abs_val) check_variable_idx = np.where(self.variable_names == col)[0][0] idx = np.where(np.abs(file_data[check_variable_idx]) > abs_val)[0] file_data[check_variable_idx][idx] = np.nan diff --git a/plotter/__init__.py b/plotter/__init__.py index 9ac9bcc..7c9b81a 100755 --- a/plotter/__init__.py +++ b/plotter/__init__.py @@ -4,5 +4,6 @@ from .main import dump from .main import plot from .main import ani_plot from .main import plot_contour +from .main import ani_plot_contour from .main import avg_plot diff --git a/plotter/main.py b/plotter/main.py index 1dd5a52..d18e2b2 100755 --- a/plotter/main.py +++ b/plotter/main.py @@ -4,7 +4,7 @@ from pathlib import PurePath import natsort import os -manual_plot=False +manual_plot=True save_result=True def dump(filename, var=[None], mval=[None]): @@ -40,6 +40,20 @@ def plot_contour(filename, out = './', oname = [None], var=[None], mval=[None]): Plot.set(args, if_manual_plot=manual_plot, if_save_result=save_result) Plot.plot_contour() +def ani_plot_contour(filename, out = './', oname = [None], var=[None], mval=[None]): + pp = str(PurePath(filename).parent) + '/' + name = str(PurePath(filename).name ) + command = 'find ' + pp + ' -name ' + "'" + name + "'" + output_stream = os.popen(command) + out_res = output_stream.read().split('\n')[0:-1] + output_stream.close() + names = natsort.natsorted(out_res,reverse=False) + + Plot = Plotter() + args = Args(names, Plot.ani_plot_contour, var=var, mval=mval, oname=oname, out=out) + Plot.set(args, if_manual_plot=manual_plot, if_save_result=save_result) + Plot.ani_plot_contour() + def avg_plot(filename, out = './', oname = 'fig.png', var=[None], mval=[None]): Plot = Plotter() args = Args([filename], Plot.avg_plot, var=var, mval=mval, oname=oname, out=out) diff --git a/setup.py b/setup.py index 31c77ef..3b5f560 100755 --- a/setup.py +++ b/setup.py @@ -4,11 +4,11 @@ if __name__ == "__main__": setuptools.setup( name='plotter', version="0.1", - install_requires=[ - "numpy>=1.23.5", - "matplotlib >=3.6.2", - "natsort >=8.2.0", - "imageio >=2.22.4"], + # install_requires=[ + # "numpy>=1.23.5", + # "matplotlib >=3.6.2", + # "natsort >=8.2.0", + # "imageio >=2.22.4"], packages=setuptools.find_packages( where='.', -- GitLab