From 398e376fc9697aca969bfa537a055d23accbf1d6 Mon Sep 17 00:00:00 2001 From: Noe Brucy Date: Sun, 25 Jul 2021 10:26:56 +0200 Subject: [PATCH] [plotter] perso rcParams is now contextualized within the Plotter --- plotter.py | 216 ++++++++++++++++++++++++++--------------------------- 1 file changed, 106 insertions(+), 110 deletions(-) diff --git a/plotter.py b/plotter.py index 7c2367d..19d6a6c 100644 --- a/plotter.py +++ b/plotter.py @@ -173,10 +173,6 @@ class Plotter(Aggregator, BaseProcessor): # Define rules self.def_rules() - # Load rcParams - if self.params.rcParams is not None: - plt.rcParams.update(params.rcParams) - # Generate astrophysix's simulations object self.gen_simus() @@ -269,116 +265,116 @@ class Plotter(Aggregator, BaseProcessor): """ Open storage and figure if needed before processing a rule """ - - # Set full name according to argument - if arg is not None: - name_full = ( - name - + "_" - + str(arg) - .replace(" ", "") - .replace("[", "") - .replace("]", "") - .replace(",", "_") - .replace("'", "") - .replace("/", "") - ) - else: - name_full = name - - # get filetype of the output - filetype = filetype_from_ext[self.params.out.ext] - - # Select runs and nums - if select is not None: - runs, nums = self.selector.select(**select) - else: - runs = self.runs - nums = self.nums - - datafiles = [] - - if rule.kind == "snapshot" or rule.kind == "cells": - run_num = [(run, num) for run in runs for num in nums[run]] - if movie: - filenames = {run: [] for run in runs} - elif rule.kind == "comp": - run_num = [(None, None)] - if movie: - self._log(f"No movie possible for rule {name}", "WARNING") - movie = False - else: - run_num = [(run, None) for run in runs] - if movie: - self._log(f"No movie possible for rule {name}", "WARNING") - movie = False - - onefigure = False # If axes are provided, only save/close once - if ax is not None: - onefigure = True - plot_filename = self._find_filename(name_full) - - for i, (run, num) in enumerate(run_num): - - # Find filename - if not onefigure: - plot_filename = self._find_filename(name_full, run, num) - - # Find ax - try: - real_ax = ax[i] - except TypeError as e: - if ax is None: - _, real_ax = plt.subplots(1, 1) - elif not_array_error(e): - real_ax = ax - else: - raise - - # Find underlying processor - if rule.kind == "snapshot": - self.current_processor = self.snaps[run][num] + with plt.rc_context(self.params.rcParams): + # Set full name according to argument + if arg is not None: + name_full = ( + name + + "_" + + str(arg) + .replace(" ", "") + .replace("[", "") + .replace("]", "") + .replace(",", "_") + .replace("'", "") + .replace("/", "") + ) else: - self.current_processor = self.study + name_full = name - # Call plot routine - close = (not onefigure) or (i == len(run_num) - 1) - plot_info = self._plot_rule( - rule, - arg, - plot_filename, - overwrite, - ax=real_ax, - close=close, - run=run, - **kwargs, - ) + # get filetype of the output + filetype = filetype_from_ext[self.params.out.ext] + + # Select runs and nums + if select is not None: + runs, nums = self.selector.select(**select) + else: + runs = self.runs + nums = self.nums + + datafiles = [] + + if rule.kind == "snapshot" or rule.kind == "cells": + run_num = [(run, num) for run in runs for num in nums[run]] + if movie: + filenames = {run: [] for run in runs} + elif rule.kind == "comp": + run_num = [(None, None)] + if movie: + self._log(f"No movie possible for rule {name}", "WARNING") + movie = False + else: + run_num = [(run, None) for run in runs] + if movie: + self._log(f"No movie possible for rule {name}", "WARNING") + movie = False + + onefigure = False # If axes are provided, only save/close once + if ax is not None: + onefigure = True + plot_filename = self._find_filename(name_full) + + for i, (run, num) in enumerate(run_num): + + # Find filename + if not onefigure: + plot_filename = self._find_filename(name_full, run, num) + + # Find ax + try: + real_ax = ax[i] + except TypeError as e: + if ax is None: + _, real_ax = plt.subplots(1, 1) + elif not_array_error(e): + real_ax = ax + else: + raise + + # Find underlying processor + if rule.kind == "snapshot": + self.current_processor = self.snaps[run][num] + else: + self.current_processor = self.study + + # Call plot routine + close = (not onefigure) or (i == len(run_num) - 1) + plot_info = self._plot_rule( + rule, + arg, + plot_filename, + overwrite, + ax=real_ax, + close=close, + run=run, + **kwargs, + ) + + # Save in astrophysix format + df = rule.datafile(name, arg) + df[filetype] = plot_filename + if movie: + filenames[run].append(plot_filename) + if plot_info is not None: + df.plot_info = plot_info + if num is not None: + snap = self.snaps[run][num].snapshot + + if overwrite and df.name in snap.datafiles: + del snap.datafiles[df.name] + elif df.name not in snap.datafiles: + snap.datafiles.add(df) + + if snap not in self.simulations[run].snapshots: + self.simulations[run].snapshots.add(snap) + + datafiles.append(df) - # Save in astrophysix format - df = rule.datafile(name, arg) - df[filetype] = plot_filename if movie: - filenames[run].append(plot_filename) - if plot_info is not None: - df.plot_info = plot_info - if num is not None: - snap = self.snaps[run][num].snapshot - - if overwrite and df.name in snap.datafiles: - del snap.datafiles[df.name] - elif df.name not in snap.datafiles: - snap.datafiles.add(df) - - if snap not in self.simulations[run].snapshots: - self.simulations[run].snapshots.add(snap) - - datafiles.append(df) - - if movie: - for run in runs: - clip = ImageSequenceClip(filenames[run], fps=movie_fps) - movie_filename = self._find_filename(name_full, run=run, ext=".mp4") - clip.write_videofile(movie_filename) + for run in runs: + clip = ImageSequenceClip(filenames[run], fps=movie_fps) + movie_filename = self._find_filename(name_full, run=run, ext=".mp4") + clip.write_videofile(movie_filename) return datafiles