From 9d788da2f114b171da05bd5f90681bfb64345e43 Mon Sep 17 00:00:00 2001 From: Artur Glavic Date: Wed, 8 Oct 2025 15:21:58 +0200 Subject: [PATCH] Minor fixed and addition of linear color plots --- eos/file_reader.py | 8 ++++---- eos/options.py | 9 +++++++++ eos/projection.py | 30 +++++++++++++++++++++--------- eos/reduction_e2h.py | 23 ++++++++++++++--------- 4 files changed, 48 insertions(+), 22 deletions(-) diff --git a/eos/file_reader.py b/eos/file_reader.py index 5a59c68..b7b8ff9 100644 --- a/eos/file_reader.py +++ b/eos/file_reader.py @@ -55,15 +55,15 @@ class AmorHeader: self.hdf.close() del(self.hdf) - def _replace_if_missing(self, key, nicos_key, dtype=float): + def _replace_if_missing(self, key, nicos_key, dtype=float, suffix=''): try: return dtype(self.hdf[f'/entry1/Amor/{key}'][0]) except(KeyError, IndexError): if NICOS_CACHE_DIR: try: - logging.warning(f" using parameter {nicos_key} from nicos cache") + logging.info(f" using parameter {nicos_key} from nicos cache") year_date = self.fileDate.strftime('%Y') - call = f'{GREP} {NICOS_CACHE_DIR}nicos-{nicos_key}/{year_date}' + call = f'{GREP} {NICOS_CACHE_DIR}nicos-{nicos_key}/{year_date}{suffix}' value = str(subprocess.getoutput(call)).split('\t')[-1] return dtype(value) except Exception: @@ -152,7 +152,7 @@ class AmorHeader: chopperSeparation, detectorDistance, chopperDetectorDistance) self.timing = AmorTiming(ch1TriggerPhase, ch2TriggerPhase, chopperSpeed, chopperPhase, tau) - polarizationConfigLabel = self._replace_if_missing('polarization/configuration/average_value', 'polarization_config_label', int) + polarizationConfigLabel = self._replace_if_missing('polarization/configuration/average_value', 'polarization_config_label', int, suffix='/*') polarizationConfig = fileio.Polarization(polarizationConfigs[polarizationConfigLabel]) logging.debug(f' polarization configuration: {polarizationConfig} (index {polarizationConfigLabel})') diff --git a/eos/options.py b/eos/options.py index 3b798f0..3db56c6 100644 --- a/eos/options.py +++ b/eos/options.py @@ -605,6 +605,7 @@ class E2HPlotArguments(StrEnum): Default = 'def' OutputFile = 'file' Logarithmic = 'log' + Linear = 'lin' @dataclass class E2HReductionConfig(ArgParsable): @@ -680,6 +681,14 @@ class E2HReductionConfig(ArgParsable): }, ) + max_events: int = field( + default = 10_000_000, + metadata={ + 'group': 'input data', + 'help': 'maximum number of events read at once', + }, + ) + @dataclass class E2HConfig: diff --git a/eos/projection.py b/eos/projection.py index 9b2753d..1ab4efb 100644 --- a/eos/projection.py +++ b/eos/projection.py @@ -275,15 +275,14 @@ class LZProjection(ProjectionInterface): else: cmap=False + if not 'norm' in kwargs: + kwargs['norm'] = LogNorm() + if self.is_normalized: - if not 'norm' in kwargs: - kwargs['norm'] = LogNorm(2e-3, 2.0) self._graph = plt.pcolormesh(self.lamda, self.alphaF, self.data.ref, **kwargs) if cmap: plt.colorbar(label='R') else: - if not 'norm' in kwargs: - kwargs['norm'] = LogNorm() self._graph = plt.pcolormesh(self.lamda, self.alphaF, self.data.I, **kwargs) if cmap: plt.colorbar(label='I / cpm') @@ -348,20 +347,23 @@ class ReflectivityProjector(ProjectionInterface): self._graph = plt.errorbar(self.data.Q, self.data.R, xerr=self.data.dQ, yerr=self.data.dR, **kwargs) self._graph_axis = plt.gca() + plt.title('Reflectivity (might be improperly normalized)') plt.yscale('log') plt.xlabel('Q / $\\AA^{-1}$') plt.ylabel('R') def update_plot(self): - ln, (errx_top, errx_bot, erry_top, erry_bot), (barsx, barsy) = self._graph.lines + ln, _, (barsx, barsy) = self._graph yerr_top = self.data.R+self.data.dR yerr_bot = self.data.R-self.data.dR + xerr_top = self.data.Q+self.data.dQ + xerr_bot = self.data.Q-self.data.dQ - errx_top.set_ydata(self.data.R) - errx_bot.set_ydata(self.data.R) - erry_top.set_ydata(yerr_top) - erry_bot.set_ydata(yerr_bot) + new_segments_x = [np.array([[xt, y], [xb, y]]) for xt, xb, y in zip(xerr_top, xerr_bot, self.data.R)] + new_segments_y = [np.array([[x, yt], [x, yb]]) for x, yt, yb in zip(self.data.Q, yerr_top, yerr_bot)] + barsx.set_segments(new_segments_x) + barsy.set_segments(new_segments_y) ln.set_ydata(self.data.R) @@ -561,6 +563,8 @@ class CombinedProjection(ProjectionInterface): from matplotlib import pyplot as plt fig = plt.gcf() axs = fig.add_gridspec(self.grid_size[0], self.grid_size[1]) + # axs = fig.add_gridspec(self.grid_size[0]+1, self.grid_size[1], + # height_ratios=[1.0 for i in range(self.grid_size[0])]+[0.2]) self._axes = [] for pi, placement in zip(self.projections, self.projection_placements): if len(placement) == 2: @@ -568,7 +572,15 @@ class CombinedProjection(ProjectionInterface): else: ax = fig.add_subplot(axs[placement[0]:placement[1], placement[2]:placement[3]]) pi.plot(**dict(kwargs)) + # Create the RangeSlider + # from matplotlib.widgets import RangeSlider + # slider_ax = fig.add_subplot(axs[self.grid_size[0], :]) + # self._slider = RangeSlider(slider_ax, "Plot Range", 0., 1., valinit=(0., 1.)) + # self._slider.on_changed(self.update_range) def update_plot(self): for pi in self.projections: pi.update_plot() + + # def update_range(self, event): + # ... \ No newline at end of file diff --git a/eos/reduction_e2h.py b/eos/reduction_e2h.py index 7aaab28..6e4f494 100644 --- a/eos/reduction_e2h.py +++ b/eos/reduction_e2h.py @@ -89,6 +89,8 @@ class E2HReduction: if self.config.reduction.plot in [E2HPlotSelection.All, E2HPlotSelection.LT, E2HPlotSelection.YZ, E2HPlotSelection.TZ]: self.plot_kwds['colorbar'] = True self.plot_kwds['cmap'] = str(self.config.reduction.plot_colormap) + if self.config.reduction.plotArgs==E2HPlotArguments.Linear: + self.plot_kwds['norm'] = None def reduce(self): if self.config.reduction.plot in [E2HPlotSelection.All, E2HPlotSelection.LT, E2HPlotSelection.Q]: @@ -159,8 +161,9 @@ class E2HReduction: def read_data(self): fileName = self.file_list[self.file_index] - self.file_index += 1 - self.dataset = AmorEventData(fileName) + self.dataset = AmorEventData(fileName, max_events=self.config.reduction.max_events) + if self.dataset.EOF or fileName==self.file_list[-1]: + self.file_index += 1 self.event_actions(self.dataset) self.dataset.update_header(self.header) @@ -179,16 +182,16 @@ class E2HReduction: def create_title(self): output = "Events to Histogram - " output += ",".join(["#"+os.path.basename(fi)[9:15].lstrip('0') for fi in self.file_list]) - output += f"\n$\\mu$={self.dataset.geometry.mu:.2f}" - output += f" $\\nu$={self.dataset.geometry.nu:.2f}" + output += f" ($\\mu$={self.dataset.geometry.mu:.2f} ;" + output += f" $\\nu$={self.dataset.geometry.nu:.2f})" if self.config.reduction.update: - output += f"\n at "+datetime.now().strftime("%m/%d/%Y %I:%M:%S") + output += f"\n at "+datetime.now().strftime("%m/%d/%Y %H:%M:%S") return output def create_graph(self): plt.suptitle(self.create_title()) self.projection.plot(**self.plot_kwds) - plt.tight_layout() + plt.tight_layout(pad=0.5) def replace_dataset(self, latest): new_files = self.path_resolver.resolve(f'{latest}') @@ -212,12 +215,14 @@ class E2HReduction: if latest>current: self.replace_dataset(latest) return - if os.path.getmtime(self.file_list[-1])<=self._last_mtime: + # if all events were read last time, only load more if file was modified + if self.dataset.EOF and os.path.getmtime(self.file_list[-1])<=self._last_mtime: return self._last_mtime = os.path.getmtime(self.file_list[-1]) try: - update_data = AmorEventData(self.file_list[-1], self.dataset.last_index+1) + update_data = AmorEventData(self.file_list[-1], self.dataset.last_index+1, + max_events=self.config.reduction.max_events) except EOFError: return logging.info(" updating with new data") @@ -228,5 +233,5 @@ class E2HReduction: self.projection.project(update_data, self.monitor) self.projection.update_plot() - plt.title(self.create_title()) + plt.suptitle(self.create_title()) plt.draw() \ No newline at end of file