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

Test failure with TestValidateAttrs.test_validating_attrs #2803

Closed
shoyer opened this issue Mar 5, 2019 · 6 comments
Closed

Test failure with TestValidateAttrs.test_validating_attrs #2803

shoyer opened this issue Mar 5, 2019 · 6 comments

Comments

@shoyer
Copy link
Member

shoyer commented Mar 5, 2019

This is due to setting multi-dimensional attributes being an error, as of the latest netCDF4-Python release: https://github.com/Unidata/netcdf4-python/blob/master/Changelog

E.g., as seen on Appveyor: https://ci.appveyor.com/project/shoyer/xray/builds/22834250/job/9q0ip6i3cchlbkw2

================================== FAILURES ===================================
___________________ TestValidateAttrs.test_validating_attrs ___________________
self = <xarray.tests.test_backends.TestValidateAttrs object at 0x00000096BE5FAFD0>
    def test_validating_attrs(self):
        def new_dataset():
            return Dataset({'data': ('y', np.arange(10.0))},
                           {'y': np.arange(10)})
    
        def new_dataset_and_dataset_attrs():
            ds = new_dataset()
            return ds, ds.attrs
    
        def new_dataset_and_data_attrs():
            ds = new_dataset()
            return ds, ds.data.attrs
    
        def new_dataset_and_coord_attrs():
            ds = new_dataset()
            return ds, ds.coords['y'].attrs
    
        for new_dataset_and_attrs in [new_dataset_and_dataset_attrs,
                                      new_dataset_and_data_attrs,
                                      new_dataset_and_coord_attrs]:
            ds, attrs = new_dataset_and_attrs()
    
            attrs[123] = 'test'
            with raises_regex(TypeError, 'Invalid name for attr'):
                ds.to_netcdf('test.nc')
    
            ds, attrs = new_dataset_and_attrs()
            attrs[MiscObject()] = 'test'
            with raises_regex(TypeError, 'Invalid name for attr'):
                ds.to_netcdf('test.nc')
    
            ds, attrs = new_dataset_and_attrs()
            attrs[''] = 'test'
            with raises_regex(ValueError, 'Invalid name for attr'):
                ds.to_netcdf('test.nc')
    
            # This one should work
            ds, attrs = new_dataset_and_attrs()
            attrs['test'] = 'test'
            with create_tmp_file() as tmp_file:
                ds.to_netcdf(tmp_file)
    
            ds, attrs = new_dataset_and_attrs()
            attrs['test'] = {'a': 5}
            with raises_regex(TypeError, 'Invalid value for attr'):
                ds.to_netcdf('test.nc')
    
            ds, attrs = new_dataset_and_attrs()
            attrs['test'] = MiscObject()
            with raises_regex(TypeError, 'Invalid value for attr'):
                ds.to_netcdf('test.nc')
    
            ds, attrs = new_dataset_and_attrs()
            attrs['test'] = 5
            with create_tmp_file() as tmp_file:
                ds.to_netcdf(tmp_file)
    
            ds, attrs = new_dataset_and_attrs()
            attrs['test'] = 3.14
            with create_tmp_file() as tmp_file:
                ds.to_netcdf(tmp_file)
    
            ds, attrs = new_dataset_and_attrs()
            attrs['test'] = [1, 2, 3, 4]
            with create_tmp_file() as tmp_file:
                ds.to_netcdf(tmp_file)
    
            ds, attrs = new_dataset_and_attrs()
            attrs['test'] = (1.9, 2.5)
            with create_tmp_file() as tmp_file:
                ds.to_netcdf(tmp_file)
    
            ds, attrs = new_dataset_and_attrs()
            attrs['test'] = np.arange(5)
            with create_tmp_file() as tmp_file:
                ds.to_netcdf(tmp_file)
    
            ds, attrs = new_dataset_and_attrs()
            attrs['test'] = np.arange(12).reshape(3, 4)
            with create_tmp_file() as tmp_file:
>               ds.to_netcdf(tmp_file)
xarray\tests\test_backends.py:3450: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
xarray\core\dataset.py:1323: in to_netcdf
    compute=compute)
xarray\backends\api.py:767: in to_netcdf
    unlimited_dims=unlimited_dims)
xarray\backends\api.py:810: in dump_to_store
    unlimited_dims=unlimited_dims)
xarray\backends\common.py:262: in store
    self.set_attributes(attributes)
xarray\backends\common.py:278: in set_attributes
    self.set_attribute(k, v)
xarray\backends\netCDF4_.py:418: in set_attribute
    _set_nc_attribute(self.ds, key, value)
xarray\backends\netCDF4_.py:294: in _set_nc_attribute
    obj.setncattr(key, value)
netCDF4\_netCDF4.pyx:2781: in netCDF4._netCDF4.Dataset.setncattr
    ???
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
>   ???
E   ValueError: multi-dimensional array attributes not supported
netCDF4\_netCDF4.pyx:1514: ValueError
@jhamman
Copy link
Member

jhamman commented Mar 12, 2019

@shoyer - I'm surprised we had a test for this behavior. Based on Unidata/netcdf4-python#841, it seems we don't want this behavior to work. Can we just remove this test (or better yet, insure the ValueError is raised)?

@shoyer
Copy link
Member Author

shoyer commented Mar 12, 2019

Yes, those both sound like good options to me.

@hmaarrfk
Copy link
Contributor

Sorry for posting on such an old thread.

Are attrs supposed to support 1D arrays?

@dcherian
Copy link
Contributor

I remember being surprised by this:
xr.tutorial.load_dataset("air_temperature").air.attrs["actual_range"] gives
array([185.16, 322.1 ], dtype=float32)

@hmaarrfk
Copy link
Contributor

With the netcdf4 back end, I'm not able to save a 1D attr dataset.

I can save my dataset with the h5netcdf backend

@hmaarrfk
Copy link
Contributor

Sorry for noise. It seems that 1D arrays are still supported.

I still had a 2D array lingering in my codebase.

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

No branches or pull requests

4 participants