question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Possible error in handling of coords when using `real_dim`

See original GitHub issue

I ran into an error when running xrft functions. The error seems to occur when the xarray DataArray has multiple coordinates that depend on the dimension that is passed to the real_dim flag. Below is (my attempt at) a minimal reproducible example that yields an error included just below.

# Create dummy data array
data = np.arange(-100,100) 

# Add coordinate time and time2, both of which are attached to dimension 'time'
coord = {'time':np.linspace(0,10,len(data)),'time2':('time',np.arange(len(data)))}

# Define the DataArray
da = xr.DataArray(data,dims='time',coords=coord)

# Compute dft
xrft.dft(da,dim='time',real_dim='time')
Value Error
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-60-ee5ce90d1fc2> in <module>
----> 1 xrft.dft(da,dim='time',real_dim='time')

/srv/conda/envs/notebook/lib/python3.8/site-packages/xrft/xrft.py in dft(da, spacing_tol, dim, real_dim, shift, detrend, window, true_phase, true_amplitude, chunks_to_segments, prefix, **kwargs)
    438 
    439     newcoords, swap_dims = _new_dims_and_coords(da, dim, k, prefix)
--> 440     daft = xr.DataArray(
    441         f, dims=da.dims, coords=dict([c for c in da.coords.items() if c[0] not in dim])
    442     )

/srv/conda/envs/notebook/lib/python3.8/site-packages/xarray/core/dataarray.py in __init__(self, data, coords, dims, name, attrs, indexes, fastpath)
    408             data = _check_data_shape(data, coords, dims)
    409             data = as_compatible_data(data)
--> 410             coords, dims = _infer_coords_and_dims(data.shape, coords, dims)
    411             variable = Variable(dims, data, attrs, fastpath=True)
    412             indexes = dict(

/srv/conda/envs/notebook/lib/python3.8/site-packages/xarray/core/dataarray.py in _infer_coords_and_dims(shape, coords, dims)
    156         for d, s in zip(v.dims, v.shape):
    157             if s != sizes[d]:
--> 158                 raise ValueError(
    159                     f"conflicting sizes for dimension {d!r}: "
    160                     f"length {sizes[d]} on the data but length {s} on "

ValueError: conflicting sizes for dimension 'time': length 101 on the data but length 200 on coordinate 'time2'

So I think the trouble here is that in taking the FT, the dimension ‘time’ is converted to ‘freq_time’, but then xrft and xarray do not know how to handle coordinates that are attached to the ‘time’ dimension. The above error is easily avoided by removing the coordinate causing trouble (in the above case that would be time2). Perhaps this is the best option? If so, then a more targeted error message might be helpful for users.

I’d be happy to put in a PR for this, but want to get feedback on how to handle this first.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:6 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
roxyboycommented, Aug 24, 2021

I doubt that after taking the FFT, any coordinate attached to the dimension upon which it was taken (i.e. time in your example) will be useful to the user but… who knows. So, if you can find a work around to keep the extra metadata, I think that would be the most versatile PR. Otherwise, I think raising an error with a proper message that “the dimension over which the FFT is taken should only have one coordinate” would be sufficient.

0reactions
roxyboycommented, Aug 30, 2021

Closed via #163

Read more comments on GitHub >

github_iconTop Results From Across the Web

Allow for creating sf objects with missing coordinates... #1034
Some sf functions support missing coordinates, e.g., it is possible to create a sf object with an empty point. Allowing for creating sf...
Read more >
Error in Converting XY Coordinates to Lat-Long Using r proj4 ...
The idea here is to take a spreadsheet with only X and Y available and create a new sheet with latitude and longitude...
Read more >
CCP4 Coordinate Library Project / RWBROOK interface
RWBROOK interface functions: Error Handling and Return Codes. Almost every RWBROOK interface function returns a code reporting on the success of the operation....
Read more >
ORCA Input Library - Geometry input - Google Sites
Using the UseSym keyword it is possible to have ORCA detect the point group, symmetrize the coordinates and change the origin and produce...
Read more >
TBC not handling my Site Calibration | Trimble Business Center
My computer still gives me the same error but also if I use this method it reverses my coordinate system back to the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found