home / github / issues

Menu
  • Search all tables
  • GraphQL API

issues: 289837692

This data as json

id node_id number title user state locked assignee milestone comments created_at updated_at closed_at author_association active_lock_reason draft pull_request body reactions performed_via_github_app state_reason repo type
289837692 MDU6SXNzdWUyODk4Mzc2OTI= 1839 Add simple array creation functions for easier unit testing 1386642 closed 0     3 2018-01-19T01:53:20Z 2020-01-19T04:21:10Z 2020-01-19T04:21:10Z CONTRIBUTOR      

When I am writing unit tests for routines that involve DataArray objects many lines of code are devoted to creating mock objects. Here is an example of a unit test I recently wrote to test some code which computes the fluid derivative of a field given the velocity. ```python def test_material_derivative():

dims = ['x', 'y', 'z', 'time']
coords = {dim: np.arange(10) for dim in dims}
shape = [coords[dim].shape[0] for dim in coords]

f = xr.Dataset({'f': (dims, np.ones(shape))}, coords=coords)
f = f.f

one = 0 *f +1
zero = 0*f


md = material_derivative(zero, one, zero, f.x + 0*f)
np.testing.assert_array_almost_equal(md.values, 0)

md = material_derivative(one, zero, zero, f.x + 0*f)
np.testing.assert_array_almost_equal(md.isel(x=slice(1,-1)).values,
                                     one.isel(x=slice(1,-1)).values)

md = material_derivative(zero, one, zero, f.y + 0*f)
np.testing.assert_array_almost_equal(md.isel(y=slice(1,-1)).values,
                                     one.isel(y=slice(1,-1)).values)

md = material_derivative(zero, zero, one, f.z + 0*f)
np.testing.assert_array_almost_equal(md.isel(z=slice(1,-1)).values,
                                     one.isel(z=slice(1,-1)).values)

```

As you can see, I devote many lines to initializing a 4D data array of all ones, where all the coordinates are np.arange(10) objects. It isn't too hard to do this once, but it gets pretty annoying to do many times, especially when I forget how the DataArray and Dataset constructors work. Now, I can do something like xr.DataArray(np.ones(...)), but I would still have to initialize the coordinates if I use them.

In any case, having some sort of functions like xr.ones, xr.zeros, and xr.rand which initialize the coordinates and data would be very nice.

{
    "url": "https://api.github.com/repos/pydata/xarray/issues/1839/reactions",
    "total_count": 0,
    "+1": 0,
    "-1": 0,
    "laugh": 0,
    "hooray": 0,
    "confused": 0,
    "heart": 0,
    "rocket": 0,
    "eyes": 0
}
  completed 13221727 issue

Links from other tables

  • 1 row from issues_id in issues_labels
  • 3 rows from issue in issue_comments
Powered by Datasette · Queries took 0.9ms · About: xarray-datasette