Repository navigation
Updates to inspector plot: add rectangle mode and fix operation inside polygons - #528
Conversation
|
|
operation arg
| y = _coord_to_centers(da, ydim) | ||
| vx = poly['x']['value'].to(unit=x.unit).values | ||
| vy = poly['y']['value'].to(unit=y.unit).values | ||
| vx = poly['x']['value'].values |
There was a problem hiding this comment.
I don't think we need to convert the unit here? By construction, the xy locations returned by the polygon info are in the correct canvas units.
|
|
||
|
|
||
| @pytest.mark.usefixtures('_use_ipympl') | ||
| @pytest.mark.parametrize( |
There was a problem hiding this comment.
I found these tests rather difficult to follow, so I decided to re-write something simpler.
Please check it covers all previous cases.
|
Removing |
Hmm here is an example where performing a Maybe we should only allow |
operation arg|
Update: Instead of removing the |
jokasimr
left a comment
There was a problem hiding this comment.
Looks good!
The tests don't cover the exact same cases, but as far as I can tell they cover the relevant cases, and I agree they are better written like this.
| try: | ||
| # If there is a 2D coordinate in the data, we need to slice the other dimension | ||
| # first. The assumption here is that there would only be one multi-dimensional | ||
| # coordinate in a given DataArray (which is very likely the case). | ||
| if da.coords[y['dim']].ndim > 1: | ||
| return da[x['dim'], x['value']][y['dim'], y['value']] | ||
| else: | ||
| return da[y['dim'], y['value']][x['dim'], x['value']] | ||
| except IndexError: | ||
| # If the index is out of bounds, return an empty DataArray | ||
| return sc.full_like(da[y['dim'], 0][x['dim'], 0], value=np.nan, dtype=float) |
There was a problem hiding this comment.
I didn't really understand this, what situation are we dealing with here?
There was a problem hiding this comment.
Maybe I should have expanded the comment a little.
Say we have a DataArray where the x coord is 2d, and the y and z coords are 1d (typically you would only have one multi-d coord in a DataArray, even though Scipp does not raise if more are present).
If we first try to slice along x, Scipp will raise an error saying 2d coords cannot be used for label-based indexing (here we are slicing using da['x', sc.scalar(15.1, unit='m')]).
But if we first slice the y dim, then it slices out the column with the correct associated x coord, which is now 1d, and can then be sliced using label-based indexing.
There was a problem hiding this comment.
But how can we plot with x on one axis if it is a 2D coord? 🤔
There was a problem hiding this comment.
See the image below. A 2d x coord means that the x position varies with y.
You typically get this in neutron data reduction when converting to d-spacing for example (see https://scipp.github.io/scippneutron/tutorials/powder-diffraction.html#From-events-to-histogram).
Did that answer your question, or did you mean something else?
There was a problem hiding this comment.
I see now what you mean 👍
| try: | ||
| values = fix_empty_range(find_limits(x, scale=scale, pad=pad)) | ||
| except ValueError: | ||
| return dict.fromkeys(keys, None) |
There was a problem hiding this comment.
I guess this is what fixes the numpy issue, what was the problem?
There was a problem hiding this comment.
No, the numpy error was fixed in #529.
This is a fix for when we click outside the range covered by the image. It would return an array with all NaNs.
When this gets passed to the 1d figure, it raised an error saying it could not compute limits because all values were NaN.
This then meant that even adding subsequent points would not draw new lines on the 1d plot because every time a new line is added, it tries to auto-scale the axis, would loop through all lines and get the the line with all NaNs and then raise an error in the background (which you can see in the log console in debug mode).
The fix here is that if trying to computing limits fails, we return a bounding box with just Nones and this means it cleanly gets ignored when trying to compute limits for the whole axes.
This thus represents a slight change in behaviour: before, if you passed 1d data to a line plot with all nans, it raised an error. Now it does not raise, but also plots nothing on the axes. It was useful in some cases to get the error, but I could not find a good way to reconcile this and the possibility of the user clicking outside the range. This can easily happen when you have a 2D coordinate for example:

Some updates to the inspector plot:
A question arose while implementing this: there is an
operationargument that decides which reduction operation to apply along the 3rd dimension into the paper. This is asumby default, but can be amin,maxormean.Now, when we select a region inside a rectangle or a polygon, we need to also reduce that along the x and y dimensions before sending to the 1d plot along z. The operation there is currently always a sum, and I don't think it would make sense to do anything else than sum?
So the question is: does it even make sense to have other operations along the z dimension?
Update: I decided to remove the operation in the end.Edit: there is an example where performing a
meanoperation actually makes sense...https://scipp.github.io/plopp/getting-started/numpy-pandas-xarray.html#Interactive-tools
So instead of removing the
operation, we apply the same operation along the x/y dims inside a selected region.I think this makes sense: in the 1d plot, we would want to see what is the mean temperature inside that region, as a function of datetime.