Skip to content

Commit 2950f56

Browse files
Attach the 'use it?' field to Text output (#1203)
* Attach the 'use it?' field to Text output * Apply suggestions from code review Co-authored-by: Jacob Wilkins <46597752+oerc0122@users.noreply.github.com> Signed-off-by: Maciej Bartkowiak <108934199+MBartkowiakSTFC@users.noreply.github.com> * Replace 0 placeholder with an underscore --------- Signed-off-by: Maciej Bartkowiak <108934199+MBartkowiakSTFC@users.noreply.github.com> Co-authored-by: Jacob Wilkins <46597752+oerc0122@users.noreply.github.com>
1 parent c6c4cc8 commit 2950f56

File tree

2 files changed

+74
-25
lines changed

2 files changed

+74
-25
lines changed

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

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -496,22 +496,26 @@ def generate_curve_label(
496496
self,
497497
index_tuple: list[int],
498498
axis_lookup: list[str],
499-
) -> str:
499+
*,
500+
skip_text: bool = False,
501+
) -> str | float:
500502
"""Get a meaningful label for a subset of data.
501503
502504
Used when plotting 1D arrays out of a multidimensional array.
503505
504506
Parameters
505507
----------
506508
index_tuple : list[int]
507-
indices of the 1D data array position in the ND array
509+
indices of the 1D data array position in the ND array.
508510
axis_lookup : list[str]
509-
Names of the axes to use
511+
Names of the axes to use.
512+
skip_text : bool, optional
513+
If set to true, omits the text parts of the label. By default False.
510514
511515
Returns
512516
-------
513-
str
514-
A string label for the plot legend.
517+
str | float
518+
A string label for the plot legend or a number for Text plotter.
515519
516520
"""
517521
if self._n_dim < 2:
@@ -540,13 +544,17 @@ def generate_curve_label(
540544
picked_value = round(picked_value, 1)
541545

542546
label += f"{axis_label}={picked_value} {axis_unit}, "
547+
if skip_text:
548+
return float(picked_value)
543549
return label.rstrip(", ")
544550

545551
def curves_vs_axis(
546552
self,
547553
x_axis_details: tuple[str, str],
548554
max_limit: int = 1,
549-
) -> list[np.ndarray]:
555+
*,
556+
skip_label_text: bool = False,
557+
) -> dict[int, np.ndarray]:
550558
"""Prepare a set of curves for plotting.
551559
552560
Parameters
@@ -555,10 +563,12 @@ def curves_vs_axis(
555563
Name and original unit of the primary plotting axis
556564
max_limit : int, optional
557565
Maximum number of curves allowed by plotter, by default 1
566+
skip_label_text: bool, optional
567+
Whether to skip the axis name and unit in the curve label, by default False.
558568
559569
Returns
560570
-------
561-
list[np.ndarray]
571+
dict[int, np.ndarray]
562572
List of data arrays ready for plotting
563573
564574
"""
@@ -610,6 +620,7 @@ def curves_vs_axis(
610620
self._curve_labels[index_tuple] = self.generate_curve_label(
611621
index_tuple,
612622
label_lookup,
623+
skip_text=skip_label_text,
613624
)
614625

615626
return self._curves

MDANSE_GUI/Src/MDANSE_GUI/Tabs/Plotters/Text.py

Lines changed: 56 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,11 @@
3030
if TYPE_CHECKING:
3131
from qtpy.QtWidgets import QTextBrowser
3232

33-
from MDANSE_GUI.Tabs.Models.PlottingContext import PlottingContext, SingleDataset
33+
from MDANSE_GUI.Tabs.Models.PlottingContext import (
34+
PlotArgs,
35+
PlottingContext,
36+
SingleDataset,
37+
)
3438

3539

3640
class DatasetFormatter:
@@ -82,7 +86,7 @@ def take_new_input(self, pc: PlottingContext):
8286

8387
for databundle in self._plotting_context.datasets().values():
8488
header, data = self.process_data(
85-
databundle.dataset,
89+
databundle,
8690
main_axis=databundle.main_axis,
8791
)
8892
self._new_text.append(
@@ -97,7 +101,7 @@ def datasets_for_csv(self):
97101
return ["No data selected"]
98102

99103
for databundle in self._plotting_context.datasets().values():
100-
yield self.process_data(databundle.dataset, main_axis=databundle.main_axis)
104+
yield self.process_data(databundle, main_axis=databundle.main_axis)
101105

102106
def make_dataset_header(self, dataset: SingleDataset, comment_character="#"):
103107
"""Return the dataset informartion as text.
@@ -141,7 +145,12 @@ def join_for_gui(
141145
data = data_array
142146

143147
text_data = "\n".join(
144-
separator.join(str(round(x, self._rounding_prec)) for x in line)
148+
separator.join(
149+
str(round(x, self._rounding_prec))
150+
if hasattr(x, "__round__")
151+
else str(x)
152+
for x in line
153+
)
145154
for line in data
146155
)
147156

@@ -154,21 +163,22 @@ def join_for_gui(
154163

155164
def process_data(
156165
self,
157-
dataset: SingleDataset,
166+
databundle: PlotArgs,
158167
main_axis: str | None = None,
159168
):
160-
"""Wrapper for approriately handling ND data."""
169+
"""Wrapper for appropriately handling ND data."""
170+
dataset = databundle.dataset
161171

162172
if dataset._n_dim == 1:
163-
return self.process_1D_data(dataset)
173+
return self.process_1D_data(databundle)
164174
if dataset._n_dim == 2:
165-
return self.process_2D_data(dataset, main_axis=main_axis)
175+
return self.process_2D_data(databundle, main_axis=main_axis)
166176

167-
return self.process_ND_data(dataset)
177+
return self.process_ND_data(databundle)
168178

169179
def process_1D_data(
170180
self,
171-
dataset: SingleDataset,
181+
databundle: PlotArgs,
172182
) -> tuple[list[str], Iterator[Iterator[float]]]:
173183
"""Turn a 1D array into text.
174184
@@ -178,8 +188,8 @@ def process_1D_data(
178188
179189
Parameters
180190
----------
181-
dataset : SingleDataset
182-
A SingleDataset read from an .MDA file (HDF5).
191+
dataset : PlotArgs
192+
A SingleDataset with the GUI parameters.
183193
184194
Returns
185195
-------
@@ -189,6 +199,7 @@ def process_1D_data(
189199
A data table with 2 columns.
190200
191201
"""
202+
dataset = databundle.dataset
192203
header_lines, _ = self.make_dataset_header(
193204
dataset, comment_character=self._comment
194205
)
@@ -214,16 +225,16 @@ def process_1D_data(
214225

215226
def process_2D_data(
216227
self,
217-
dataset: SingleDataset,
228+
databundle: PlotArgs,
218229
*,
219230
main_axis: str | None = None,
220231
) -> tuple[list[str], Iterator[Iterator[float]]]:
221232
"""Convert a 2D data array into text.
222233
223234
Parameters
224235
----------
225-
dataset : SingleDataset
226-
A SingleDataset read from an .MDA file (HDF5).
236+
dataset : PlotArgs
237+
A SingleDataset with the GUI parameters.
227238
main_axis : str or None
228239
Main axis to plot.
229240
@@ -243,6 +254,8 @@ def process_2D_data(
243254
v
244255
245256
"""
257+
dataset = databundle.dataset
258+
246259
header_lines, comment_char = self.make_dataset_header(
247260
dataset, comment_character=self._comment
248261
)
@@ -267,18 +280,43 @@ def process_2D_data(
267280

268281
LOG.debug(f"process_2D_data: axis {ax_key} has length {len(axis)}")
269282

270-
rc = "column" if n == flip_array else "row"
283+
rc = "column" if n else "row"
271284
header_lines.append(
272285
f"{comment_char} first {rc} is {ax_key} in units {new_unit}"
273286
)
274287

275288
LOG.debug(f"Data shape: {dataset._data.shape}")
289+
try:
290+
best_unit, best_axis = (
291+
dataset._axes_units[databundle.main_axis],
292+
databundle.main_axis,
293+
)
294+
except KeyError:
295+
best_unit, best_axis = dataset.longest_axis()
276296

297+
if self._is_preview:
298+
curves_limit = self._preview_columns if flip_array else self._preview_lines
299+
else:
300+
curves_limit = (
301+
dataset._data.shape[0] if flip_array else dataset._data.shape[1]
302+
)
303+
304+
multi_curves = np.vstack(
305+
list(
306+
dataset.curves_vs_axis(
307+
(best_unit, best_axis), max_limit=curves_limit, skip_label_text=True
308+
).values()
309+
)
310+
)
277311
# Add corner nil
278-
xaxis = prepend(0.0, new_axes[axis_numbers[1]].flat)
312+
xaxis = prepend("_", new_axes[axis_numbers[flip_array]].flat)
279313

280314
# Add axes to data
281-
data_lines = zip(new_axes[axis_numbers[0]].flat, dataset.data, strict=True)
315+
data_lines = zip(
316+
dataset._curve_labels.values(),
317+
multi_curves,
318+
strict=True,
319+
)
282320

283321
# Put xaxis in
284322
temp = prepend(xaxis, data_lines)

0 commit comments

Comments
 (0)