plopp 25.5.0__py3-none-any.whl → 25.6.1__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -59,7 +59,7 @@ class GraphicalView(View):
59
59
  title: str | None = None,
60
60
  figsize: tuple[float, float] | None = None,
61
61
  format: Literal['svg', 'png'] | None = None,
62
- legend: bool | tuple[float, float] = False,
62
+ legend: bool | tuple[float, float] = True,
63
63
  camera: Camera | None = None,
64
64
  autoscale: bool = True,
65
65
  ax: Any = None,
plopp/plotting/common.py CHANGED
@@ -76,64 +76,28 @@ def to_variable(obj) -> sc.Variable:
76
76
  return out
77
77
 
78
78
 
79
- def _ensure_data_array_coords(
80
- da: sc.DataArray, coords: list[str] | None
81
- ) -> sc.DataArray:
82
- if coords is None:
83
- coords = list(da.dims)
84
- elif missing := set(coords) - set(da.coords.keys()):
85
- raise ValueError(f"Specified coords do not exist: {missing}")
86
-
87
- # Remove unused coords
88
- da = da.drop_coords(list(set(da.coords) - set(coords)))
89
- # Assign dim coords where missing. The above checks ensure that all missing
90
- # coords are dim coords, i.e., that `name in out.dims`.
91
- da = da.assign_coords(
92
- {
93
- name: sc.arange(name, da.sizes[name], unit=None)
94
- for name in set(coords) - set(da.coords)
95
- }
96
- )
97
- return da
98
-
99
-
100
- def to_data_array(
101
- obj: Plottable | list,
102
- coords: list[str] | None = None,
103
- ) -> sc.DataArray:
79
+ def to_data_array(obj: Plottable | list) -> sc.DataArray:
104
80
  """
105
- Convert an input to a DataArray, potentially adding fake coordinates if they are
106
- missing.
81
+ Convert an input to a DataArray.
82
+ Returns a shallow copy of the input if it is already a DataArray.
107
83
 
108
84
  Parameters
109
85
  ----------
110
86
  obj:
111
87
  The input object to be converted.
112
- coords:
113
- If supplied, use these coords instead of the input's dimension coordinates.
114
88
  """
89
+ if isinstance(obj, sc.DataArray):
90
+ return obj.copy(deep=False)
115
91
  out = _maybe_to_variable(obj)
116
92
  if isinstance(out, sc.Variable):
117
93
  out = sc.DataArray(data=out)
118
94
  out = from_compatible_lib(out)
119
95
  if not isinstance(out, sc.DataArray):
120
96
  raise TypeError(f"Cannot convert input of type {type(obj)} to a DataArray.")
121
- out = _ensure_data_array_coords(out, coords)
122
- for name, coord in out.coords.items():
123
- if not coord.dims:
124
- raise ValueError(
125
- "Input data cannot be plotted: it has a scalar coordinate along "
126
- f"dimension {name}. Consider dropping this coordinate before plotting. "
127
- f"Use ``data.drop_coords('{name}').plot()``."
128
- )
129
- for name in out.coords:
130
- other_dims = [dim for dim in out.coords[name].dims if dim not in out.dims]
131
- for dim in other_dims:
132
- out.coords[name] = out.coords[name].mean(dim)
133
97
  return out
134
98
 
135
99
 
136
- def _check_size(da: sc.DataArray):
100
+ def _check_size(da: sc.DataArray) -> None:
137
101
  """
138
102
  Prevent slow figure rendering by raising an error if the data array exceeds a
139
103
  default size.
@@ -149,7 +113,7 @@ def _check_size(da: sc.DataArray):
149
113
  )
150
114
 
151
115
 
152
- def check_not_binned(da: sc.DataArray):
116
+ def check_not_binned(da: sc.DataArray) -> None:
153
117
  """
154
118
  Plopp cannot plot binned data.
155
119
  This function will raise an error if the input data is binned.
@@ -169,7 +133,7 @@ def check_not_binned(da: sc.DataArray):
169
133
  )
170
134
 
171
135
 
172
- def check_allowed_dtypes(da: sc.DataArray):
136
+ def check_allowed_dtypes(da: sc.DataArray) -> None:
173
137
  """
174
138
  Currently, Plopp cannot plot data that contains vector and matrix dtypes.
175
139
  This function will raise an error if the input data type is not supported.
@@ -185,10 +149,108 @@ def check_allowed_dtypes(da: sc.DataArray):
185
149
  )
186
150
 
187
151
 
188
- def _all_dims_sorted(var, order='ascending'):
152
+ def _all_dims_sorted(var, order='ascending') -> bool:
153
+ """
154
+ Check if all dimensions of a variable are sorted in the specified order.
155
+ This is used to ensure that the coordinates are sorted before plotting.
156
+ """
189
157
  return all(sc.allsorted(var, dim, order=order) for dim in var.dims)
190
158
 
191
159
 
160
+ def _rename_dims_from_coords(da: sc.DataArray, coords: Iterable[str]) -> sc.DataArray:
161
+ """
162
+ If coordinates are provided, rename the dimensions of the data array to match the
163
+ names of the coordinates, so that they effectively become the dimension coordinates.
164
+ """
165
+ renamed_dims = {}
166
+ underlying_dims = set()
167
+ for dim in coords:
168
+ underlying = da.coords[dim].dims[-1]
169
+ if underlying in underlying_dims:
170
+ raise ValueError(
171
+ "coords: Cannot use more than one coordinate associated with "
172
+ f"the same underlying dimension ({underlying})."
173
+ )
174
+ renamed_dims[underlying] = dim
175
+ underlying_dims.add(underlying)
176
+ return da.rename_dims(**renamed_dims)
177
+
178
+
179
+ def _add_missing_dimension_coords(da: sc.DataArray) -> sc.DataArray:
180
+ """
181
+ Add missing dimension coordinates to the data array.
182
+ If a dimension does not have a coordinate, it will be added with a range of values.
183
+ """
184
+ return da.assign_coords(
185
+ {
186
+ dim: sc.arange(dim, da.sizes[dim], unit=None)
187
+ for dim in da.dims
188
+ if dim not in da.coords
189
+ }
190
+ )
191
+
192
+
193
+ def _drop_non_dimension_coords(da: sc.DataArray) -> sc.DataArray:
194
+ """
195
+ Drop all coordinates that are not dimension coordinates.
196
+ This is useful to ensure that only the coordinates that are actually used for
197
+ plotting are kept.
198
+ """
199
+ return da.drop_coords([name for name in da.coords if name not in da.dims])
200
+
201
+
202
+ def _handle_coords_with_left_over_dimensions(da: sc.DataArray) -> sc.DataArray:
203
+ """
204
+ In some rare cases, the coordinate may have dimensions that are not present in the
205
+ data array. This can happen for example when a 2d coord with bin edges is sliced
206
+ along one dimension, leaving a coordinate with the other dimension, and a dimension
207
+ of length 2 (because of the bin edges) in th sliced dimension.
208
+
209
+ This function will handle such cases by averaging the coordinate over the left-over
210
+ dimensions, effectively reducing the coordinate to a single value for each
211
+ dimension that is not present in the data array.
212
+ """
213
+ return da.assign_coords(
214
+ {
215
+ name: da.coords[name].mean(
216
+ [dim for dim in da.coords[name].dims if dim not in da.dims]
217
+ )
218
+ for name in da.coords
219
+ }
220
+ )
221
+
222
+
223
+ def _check_coord_sanity(da: sc.DataArray) -> None:
224
+ """
225
+ Warn if any coordinate is not sorted. This can lead to unpredictable results
226
+ when plotting.
227
+ Also, raise an error if any coordinate is scalar.
228
+ """
229
+ for name, coord in da.coords.items():
230
+ try:
231
+ if not (
232
+ _all_dims_sorted(coord, order='ascending')
233
+ or _all_dims_sorted(coord, order='descending')
234
+ ):
235
+ warnings.warn(
236
+ 'The input contains a coordinate with unsorted values '
237
+ f'({name}). The results may be unpredictable. '
238
+ 'Coordinates can be sorted using '
239
+ '`scipp.sort(data, dim="to_be_sorted", order="ascending")`.',
240
+ RuntimeWarning,
241
+ stacklevel=2,
242
+ )
243
+ except sc.DTypeError:
244
+ pass
245
+
246
+ if not coord.dims:
247
+ raise ValueError(
248
+ "Input data cannot be plotted: it has a scalar coordinate along "
249
+ f"dimension {name}. Consider dropping this coordinate before plotting. "
250
+ f"Use ``data.drop_coords('{name}').plot()``."
251
+ )
252
+
253
+
192
254
  def preprocess(
193
255
  obj: Plottable | list,
194
256
  name: str | None = None,
@@ -219,7 +281,7 @@ def preprocess(
219
281
  elif coords is not None:
220
282
  coords = list(coords)
221
283
 
222
- out = to_data_array(obj, coords)
284
+ out = to_data_array(obj)
223
285
  check_not_binned(out)
224
286
  check_allowed_dtypes(out)
225
287
  if name is not None:
@@ -227,34 +289,11 @@ def preprocess(
227
289
  if not ignore_size:
228
290
  _check_size(out)
229
291
  if coords is not None:
230
- renamed_dims = {}
231
- for dim in coords:
232
- underlying = out.coords[dim].dims[-1]
233
- if underlying in renamed_dims:
234
- raise ValueError(
235
- "coords: Cannot use more than one coordinate associated with "
236
- f"the same underlying dimension ({underlying})."
237
- )
238
- renamed_dims[underlying] = dim
239
- out = out.rename_dims(**renamed_dims)
240
- for n, coord in out.coords.items():
241
- if (coord.ndim == 0) or (n not in out.dims):
242
- continue
243
- try:
244
- if not (
245
- _all_dims_sorted(coord, order='ascending')
246
- or _all_dims_sorted(coord, order='descending')
247
- ):
248
- warnings.warn(
249
- 'The input contains a coordinate with unsorted values '
250
- f'({n}). The results may be unpredictable. '
251
- 'Coordinates can be sorted using '
252
- '`scipp.sort(data, dim="to_be_sorted", order="ascending")`.',
253
- RuntimeWarning,
254
- stacklevel=2,
255
- )
256
- except sc.DTypeError:
257
- pass
292
+ out = _rename_dims_from_coords(out, coords)
293
+ out = _add_missing_dimension_coords(out)
294
+ out = _drop_non_dimension_coords(out)
295
+ out = _handle_coords_with_left_over_dimensions(out)
296
+ _check_coord_sanity(out)
258
297
  return out
259
298
 
260
299
 
plopp/plotting/xyplot.py CHANGED
@@ -23,7 +23,17 @@ def _make_data_array(x: sc.Variable, y: sc.Variable) -> sc.DataArray:
23
23
  y:
24
24
  The variable to use as the data.
25
25
  """
26
- return sc.DataArray(data=y, coords={x.dim: x})
26
+ if x.ndim != 1 or y.ndim != 1:
27
+ raise sc.DimensionError(
28
+ f'Expected 1 dimension, got {x.ndim} for x and {y.ndim} for y.'
29
+ )
30
+ dim = x.dim
31
+ return sc.DataArray(
32
+ data=sc.array(dims=[dim], values=y.values, variances=y.variances, unit=y.unit)
33
+ if y.dim != dim
34
+ else y,
35
+ coords={dim: x},
36
+ )
27
37
 
28
38
 
29
39
  def xyplot(
@@ -48,7 +58,4 @@ def xyplot(
48
58
  """
49
59
  x = Node(to_variable, x)
50
60
  y = Node(to_variable, y)
51
- dim = x().dim
52
- if dim != y().dim:
53
- raise sc.DimensionError("Dimensions of x and y must match")
54
61
  return linefigure(Node(_make_data_array, x=x, y=y), **kwargs)
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: plopp
3
- Version: 25.5.0
3
+ Version: 25.6.1
4
4
  Summary: Visualization library for Scipp
5
5
  Author: Scipp contributors
6
6
  License: BSD 3-Clause License
@@ -43,11 +43,11 @@ plopp/graphics/bbox.py,sha256=3WeMgu5LgWIXLJTYVT7_USwaAcS7cQPNCk6B0gvuNGo,3596
43
43
  plopp/graphics/camera.py,sha256=fXc1YH7Tp-rwKkcYeE-mHrxMz3SKauj5xJCRuKv-QPs,5323
44
44
  plopp/graphics/colormapper.py,sha256=u5cADKU9lrRkJ_a969i_5z-mdT0-lBnEonc0PW_xSXA,9643
45
45
  plopp/graphics/figures.py,sha256=PLWQLnQNWz8epubYlK7HLSkvPGJmTAmHjJM2DqpvLe8,2735
46
- plopp/graphics/graphicalview.py,sha256=u5sz3CebhVLoEeKPpzlBlPfRrRORxDmruyfhLXDbH00,9436
46
+ plopp/graphics/graphicalview.py,sha256=07xpJVnvuNlaWGsJshg_wL81jG57JhRxI23E7_WYI2M,9435
47
47
  plopp/graphics/tiled.py,sha256=gXYZVAs6wRFKezzN3VlEvm7_z2zx3IDYfGZQZqrpL80,1386
48
48
  plopp/plotting/__init__.py,sha256=s25RIbKplHSRn0jjtLADPQl72JLf_KLRMRCfeMDI_UA,205
49
49
  plopp/plotting/__init__.pyi,sha256=95wYGfM5PT0Y0Ov_ue7rBrHvHxrtex-2WEYtfFzBvE0,475
50
- plopp/plotting/common.py,sha256=7xR2aWUWzKyxshlPn8B-OWQ5Hy8wiMn-qtPywkLsNRs,9515
50
+ plopp/plotting/common.py,sha256=_DmUEpWk2n4uz2-KwhzdctbC0WvRU9GsAMFDgfPnUo8,10965
51
51
  plopp/plotting/inspector.py,sha256=fT4v342pqTzihKaweoGD4WaQ5eDdyngK7W451Mh2Ffw,3586
52
52
  plopp/plotting/mesh3d.py,sha256=KjAtlVlLYslwgP_uE067WtJRgNURkhaz0PPUuYLlJyw,2778
53
53
  plopp/plotting/plot.py,sha256=WQzw1vHGljWY1tQU1InQWe2-M-vCW2IRiL-v7cnw46w,3886
@@ -55,7 +55,7 @@ plopp/plotting/scatter.py,sha256=ybPWFHFwxt_Bzv3Dy-yAFCRHW5HQEggcNihpzWduH7A,316
55
55
  plopp/plotting/scatter3d.py,sha256=oizd-BCgmvNu_fSV6FQt9oe7lPn4h2Zc1ohKmor-d78,3819
56
56
  plopp/plotting/slicer.py,sha256=A3g26czD6GM3vBlVS-IBOnCOlpaERqUhjT-_POYJEHQ,5877
57
57
  plopp/plotting/superplot.py,sha256=0EnzgSmNrdPHXooqj6GNR0IAApEHyfjgxYIPgPVO_5o,1416
58
- plopp/plotting/xyplot.py,sha256=t_0GC2TlC6QZPKN4tfgLRUUBDhJ5zoNJjsRyYxLX_xI,1448
58
+ plopp/plotting/xyplot.py,sha256=zi-RcsgonnR747aH3zp75at4ntgZ0t0rSdKEeVlReMI,1641
59
59
  plopp/widgets/__init__.py,sha256=s25RIbKplHSRn0jjtLADPQl72JLf_KLRMRCfeMDI_UA,205
60
60
  plopp/widgets/__init__.pyi,sha256=4hVX7I3VEjE3oy2AGVvguYsIgg7cVrhKGpX7xVgGe9Y,844
61
61
  plopp/widgets/box.py,sha256=v5Gd3sY0WXcc3UfkcXxZboPG02sfOMXIo50tmpxPwVs,1806
@@ -68,8 +68,8 @@ plopp/widgets/slice.py,sha256=ihiGEGGva_2WZIug-kizg1I5SFdMzu4_8DDzuHWIKeA,4212
68
68
  plopp/widgets/style.py,sha256=Fw4S8te0xDyT8-kWu4Ut35aLgyr98qMlWZRbXbd1-dk,184
69
69
  plopp/widgets/toolbar.py,sha256=ueNeOZSC2suAG7nCJzqwl8DmtWD5c2J8VkuP-XzJRwI,4683
70
70
  plopp/widgets/tools.py,sha256=9l7LmB8qG0OTt5ZJihJzqRna1ouxJthO_Ahnq7WKTtY,7172
71
- plopp-25.5.0.dist-info/licenses/LICENSE,sha256=fCqnkzCwkGneGtgOidkO2b3ed6SGZT0yhuworuJG0K0,1553
72
- plopp-25.5.0.dist-info/METADATA,sha256=bFuzQ8XlAfiKyqMeiONKwEdMhN1te45ZHTGETPx8CtE,4533
73
- plopp-25.5.0.dist-info/WHEEL,sha256=Nw36Djuh_5VDukK0H78QzOX-_FQEo6V37m3nkm96gtU,91
74
- plopp-25.5.0.dist-info/top_level.txt,sha256=Lk_GLFh37eX-3PO3c6bPkDpir7GjJmbQHYza3IpIphc,6
75
- plopp-25.5.0.dist-info/RECORD,,
71
+ plopp-25.6.1.dist-info/licenses/LICENSE,sha256=fCqnkzCwkGneGtgOidkO2b3ed6SGZT0yhuworuJG0K0,1553
72
+ plopp-25.6.1.dist-info/METADATA,sha256=1rzml_3ajA-prOwWEd_8KI8EfxZiyV6DRFcl3nTvf68,4533
73
+ plopp-25.6.1.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
74
+ plopp-25.6.1.dist-info/top_level.txt,sha256=Lk_GLFh37eX-3PO3c6bPkDpir7GjJmbQHYza3IpIphc,6
75
+ plopp-25.6.1.dist-info/RECORD,,
@@ -1,5 +1,5 @@
1
1
  Wheel-Version: 1.0
2
- Generator: setuptools (80.7.1)
2
+ Generator: setuptools (80.9.0)
3
3
  Root-Is-Purelib: true
4
4
  Tag: py3-none-any
5
5