Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Plot 2d coords #17

Merged
merged 12 commits into from
Sep 19, 2022
Merged

Plot 2d coords #17

merged 12 commits into from
Sep 19, 2022

Conversation

nvaytet
Copy link
Member

@nvaytet nvaytet commented Sep 15, 2022

import scipp as sc
import numpy as np
import plopp as pp

# make some data
N = 10
M = 5
x = np.arange(N+1).astype(np.float64)
y = np.arange(M+1).astype(np.float64)
xx, yy = np.meshgrid(x, y[:-1])
z = np.random.random([M, N])
for i in range(M):
    xx[i] *= (i + 1.0)

da = sc.DataArray(data=sc.array(dims=['y', 'x'], values=z, unit='counts'),
 coords={'x': sc.array(dims=['y', 'x'], values=xx, unit='m'),
         'y': sc.array(dims=['y'], values=y, unit='m')})

pp.plot(da)

Screenshot at 2022-09-15 16-47-27

Fixes #3

Comment on lines 82 to 88
def test_plot_2d_coord_limits():
p = pp.plot(dense_data_array(ndim=2, ragged=True))
assert np.array_equal(p._ax.get_xlim(), [-0.5, 88.5])
assert np.array_equal(p._ax.get_ylim(), [-0.5, 49.5])
p = pp.plot(dense_data_array(ndim=2, ragged=True, binedges=True))
assert np.array_equal(p._ax.get_xlim(), [0.0, 89.0])
assert np.array_equal(p._ax.get_ylim(), [0.0, 50.0])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tests like this don't follow what we have, e.g., learned from the "Software Engineering @ Google@ book. This is just checking some numbers, it is unclear what they mean (since we do not see the data setup), and probably the test will break if dense_data_array changes somehow?

Copy link
Member Author

@nvaytet nvaytet Sep 16, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True. So is doing assert np.array_equal(p._ax.get_xlim(), [da.coords['xx'].min().value, da.coords['xx'].max().value]) better? It still involves the xx dim, which may change in the dense data array.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On second thought, I think I will just remove this test. It sounds like it's going to break all the time, and it in fact has nothing to do with 2d coords, the problem was there also with 1d coords. It's the bin edges which is important.

}).transpose(self._data.dims).values
out = {
'xy'[axis - 1]:
np.repeat(broadcasted_coord, 2, axis=axis)[clip],
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@SimonHeybrock do you have a good idea on how we could do more of this with scipp instead of numpy? I think the axis parameter is a bit bug-prone, would be nice to just use dimension labels...

along dimension `dim`.
"""
index = x.dims.index(dim) + 1
dummy_dim = '_'
Copy link
Member

@SimonHeybrock SimonHeybrock Sep 16, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is going to break if that matches a dim name, use uuid.uuid4() (grep in scipp to see how we do this in other places).

Comment on lines 104 to 111
# Broadcast 1d coord to 2d and repeat along 1d dim
broadcasted_coord = repeat(broadcast(xy[dim_1d[0]],
sizes={
**xy[dim_2d[0]].sizes,
**xy[dim_1d[0]].sizes
}).transpose(self._data.dims),
dim=dim_1d[1],
n=2)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can the repeat and broadcast be swapped? Is this expensive otherwise?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm would not be very easy I think. I can add a TODO note?

Comment on lines 101 to 102
if dim_2d is None:
return xy['x'].values, xy['y'].values, z
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this avoiding the potentially costly stuff below for "normal" coords?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes

@nvaytet nvaytet merged commit 2dc6352 into main Sep 19, 2022
@nvaytet nvaytet deleted the 2d_coords branch September 19, 2022 07:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Plotting with 2D coords does not work
2 participants