Skip to content

Commit ecd7181

Browse files
committed
Fix misc issues
1 parent 93ddc4e commit ecd7181

8 files changed

Lines changed: 160 additions & 110 deletions

File tree

MDANSE_GUI/Src/MDANSE_GUI/Tabs/Models/PlottingContext.py

Lines changed: 52 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,11 @@
1717

1818
import copy
1919
import functools
20-
from collections.abc import Generator, Iterable, Sequence
2120
from contextlib import suppress
2221
from itertools import islice
2322
from math import prod
2423
from pathlib import Path
25-
from typing import TYPE_CHECKING, Literal, NamedTuple, overload
24+
from typing import TYPE_CHECKING, NamedTuple
2625

2726
import h5py
2827
import matplotlib.pyplot as mpl
@@ -32,15 +31,15 @@
3231
from matplotlib.colors import to_hex as mpl_to_hex
3332
from matplotlib.lines import lineStyles
3433
from matplotlib.markers import MarkerStyle
35-
from more_itertools import first, locate, nth, nth_product, sort_together, unzip
34+
from more_itertools import first, locate, nth, nth_product
3635
from qtpy.QtCore import QModelIndex, Qt, Signal, Slot
3736
from qtpy.QtGui import QColor, QStandardItem, QStandardItemModel
3837

3938
from MDANSE.IO.IOUtils import summarise_array
4039
from MDANSE.MLogging import LOG
4140

4241
if TYPE_CHECKING:
43-
from collections.abc import Iterable
42+
from collections.abc import Generator, Iterable, Sequence
4443

4544
NUMBERS_FOR_SLICE = 3
4645
NUMBERS_FOR_RANGE = 2
@@ -619,7 +618,7 @@ def curves_vs_axis(
619618
x_axis = self.x_axis(axis_label)
620619

621620
if self._data.ndim == 1:
622-
yield axis_label, (x_axis, self.data)
621+
yield "", (x_axis, self.data)
623622
return
624623

625624
data_shape = self._data.shape
@@ -686,67 +685,94 @@ def curves_vs_axis(
686685

687686
def planes_vs_axis(
688687
self,
689-
axis_number: int,
690-
max_limit: int = 1,
691-
) -> Generator[tuple[str, npt.NDArray[np.floating]]]:
688+
main_axis: str,
689+
max_limit: int = 9,
690+
) -> Generator[tuple[str, npt.NDArray[np.floating], tuple[str, str]]]:
692691
"""Prepare for plotting 2D subsets of an ND array.
693692
694693
Parameters
695694
----------
696-
axis_number : int
697-
index of the axis perpendicular to the plotted array
695+
main_axis : str
696+
Label of the axis perpendicular to the plotted array.
698697
699698
Yields
700699
------
701-
str
700+
main_label : str
702701
Grid label.
703-
npt.NDArray[np.floating]
702+
image_array : npt.NDArray[np.floating]
704703
2D array.
705-
704+
axis_labels : tuple[str, ...]
705+
Labels for each axis.
706706
"""
707+
main_axis_index = self.main_axis_index(main_axis)
708+
other_labels = self._axis_labels(main_axis)
709+
707710
match self._data.ndim:
708711
case 1:
709712
pass
710-
case 2 if axis_number == 1:
711-
yield self._labels["medium"], self.data.T
713+
case 2 if main_axis_index == 1:
714+
yield self._labels["medium"], self.data.T, (main_axis, other_labels[0])
712715
case 2:
713-
yield self._labels["medium"], self.data
716+
yield self._labels["medium"], self.data, (main_axis, other_labels[0])
714717
case 3:
715718
perpendicular_axis_name, perpendicular_axis = nth(
716-
self._axes.items(), axis_number, default=(None, None)
719+
self._axes.items(), main_axis_index, default=(None, None)
717720
)
718721

719722
if perpendicular_axis is None:
720723
return
721724

722-
reordered_view = np.moveaxis(self.data, axis_number, 0)
725+
reordered_view = np.moveaxis(self.data, main_axis_index, 0)
723726

724727
for plane_number in self.curve_ind(max_limit):
728+
if plane_number > len(reordered_view):
729+
continue
730+
725731
yield (
726732
f"{self._labels['minimal']}:{perpendicular_axis_name}={perpendicular_axis[plane_number]}",
727733
reordered_view[plane_number],
734+
other_labels,
728735
)
729736
case _:
730737
raise NotImplementedError(
731738
f"Cannot handle {self._data.ndim}-dimensional data."
732739
)
733740

734-
def main_axis_index(self, main_axis: str | None, *, default: int) -> int:
741+
def main_axis_index(
742+
self, main_axis: str | None, *, default: int | None = None
743+
) -> int:
735744
"""Find index of main axis.
736745
737746
Parameters
738747
----------
739748
main_axis : str
740749
Main axis name to search for.
741-
default : int
750+
default : int, optional
742751
Index if ``main_axis`` not found.
743752
744753
Returns
745754
-------
746755
int
747756
Index of main axis.
757+
758+
Raises
759+
------
760+
ValueError
761+
Axis not found and no default.
748762
"""
749-
return first(locate(self._axes, pred=lambda x: x == main_axis), default)
763+
ind = first(locate(self._axes, pred=main_axis.__eq__), default)
764+
if ind is None:
765+
raise ValueError(
766+
f"Cannot find axis {main_axis} in {','.join(self._axes.keys())}"
767+
)
768+
return ind
769+
770+
def _axis_labels(self, main_axis: str) -> tuple[str] | tuple[str, str]:
771+
main_axis_index = self.main_axis_index(main_axis)
772+
773+
return tuple(
774+
label for i, label in enumerate(self._axes) if i != main_axis_index
775+
)
750776

751777

752778
plotting_column_labels = [
@@ -1057,18 +1083,16 @@ def delete_dataset(self, index: QModelIndex):
10571083
self._datasets.pop(dkey, None)
10581084

10591085
def planes(
1060-
self, default_axis: int = 0, planes_per_dataset: int | None = None
1061-
) -> Generator[tuple[PlotArgs, str, npt.NDArray[np.floating]]]:
1086+
self, default_axis: int | None = None, planes_per_dataset: int | None = None
1087+
) -> Generator[tuple[PlotArgs, str, npt.NDArray[np.floating], tuple[str, str]]]:
10621088
for databundle in self.datasets().values():
10631089
ds = databundle.dataset
10641090

1065-
for label, plane in islice(
1066-
ds.planes_vs_axis(
1067-
ds.main_axis_index(databundle.main_axis, default=default_axis)
1068-
),
1091+
for label, plane, axis_labels in islice(
1092+
ds.planes_vs_axis(databundle.main_axis),
10691093
planes_per_dataset,
10701094
):
1071-
yield databundle, label, plane
1095+
yield databundle, label, plane, axis_labels
10721096

10731097
def curves(
10741098
self, curves_per_dataset: int | None = None

MDANSE_GUI/Src/MDANSE_GUI/Tabs/Plotters/Grid.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -146,27 +146,26 @@ def plot(
146146
self._normalisation_errors = []
147147
self.apply_settings(plotting_context)
148148

149-
nplots = min(
149+
self._n_curves = min(
150150
sum(db.dataset.n_curves for db in plotting_context.datasets().values()),
151151
self._plot_limit,
152152
)
153-
grid_size = self.grid_size(nplots)
153+
grid_size = self.grid_size(self.n_curves)
154154
gs = self._figure.add_gridspec(*grid_size)
155155

156156
for ind, (databundle, label, curve) in enumerate(
157157
islice(flatten(plotting_context.curves()), self._plot_limit)
158158
):
159159
axes = target.add_subplot(gs[ind])
160-
self._axes_titles.append(databundle.dataset._name)
160+
self._axes_titles.append(f"{databundle.dataset._name} {label}")
161161

162162
self._plot_single(
163163
axes,
164164
curve,
165165
databundle,
166-
label=label,
166+
label="",
167167
colour=databundle.colour,
168168
)
169-
axes.legend()
170169
self._axes.append(axes)
171170

172171
self.toggle_legend(plotting_context.use_legend)

MDANSE_GUI/Src/MDANSE_GUI/Tabs/Plotters/Grouped.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -201,14 +201,17 @@ def plot(
201201

202202
self.height_max, self.length_max = 0.0, 0.0
203203
self._figure = target
204+
204205
self._axes = []
205206
self._axes_titles = []
206207
self._backup_curves = []
207208
self._active_curves = []
208209
self._normalisation_errors = []
210+
209211
self.apply_settings(plotting_context)
210212

211-
nplots = min(ilen(plotting_context.curves()), self._plot_limit)
213+
self._n_curves = sum(ilen(curves) for curves in plotting_context.curves())
214+
nplots = min(len(plotting_context.datasets()), self._plot_limit)
212215
grid_size = self.grid_size(nplots)
213216
gs = self._figure.add_gridspec(*grid_size)
214217
limits = [(0.0, 0.0, 0.0, 0.0) for _ in range(nplots)]
@@ -229,16 +232,18 @@ def plot(
229232

230233
colours = self.colours(db.colour, ds.n_curves)
231234

232-
for (databundle, label, curve), colour in zip(
233-
dataclump, colours, strict=True
235+
for curve_ind, ((databundle, label, curve), colour) in enumerate(
236+
zip(dataclump, colours, strict=True)
234237
):
235238
self._plot_single(
236239
axes,
237240
curve,
238241
databundle,
242+
ind=curve_ind,
239243
label=label,
240244
colour=colour,
241245
)
246+
242247
axes.legend()
243248
self._axes.append(axes)
244249
limits[ind] = (*axes.get_xlim(), *axes.get_ylim())
@@ -283,6 +288,7 @@ def _plot_single(
283288
curve: tuple[np.ndarray, np.ndarray] | tuple[np.ndarray],
284289
databundle: PlotArgs,
285290
*,
291+
ind: int,
286292
label: str,
287293
colour: tuple[float, float, float] | str,
288294
):
@@ -304,7 +310,7 @@ def _plot_single(
304310
lines: list[Line2D] = axes.plot(
305311
*curve,
306312
linestyle=databundle.line_style,
307-
label=label,
313+
label=self.label(label, ind, n_curves=databundle.dataset.n_curves),
308314
color=colour,
309315
)
310316

0 commit comments

Comments
 (0)