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.

Error raised when array has additional 2D coordinates

See original GitHub issue

In Harmonica we usually produce 2D grids out of scattered data whose dimensions are northing and easting. In addition, we often have an extra coordinate: the upward, which is the vertical coordinate of each point of the grid. The upward coordinate is included into the list of coordinates inside the array, but as a 2D array with the same dimensions as the grid itself.

When trying to compute the fft over this kind of arrays, xrft is raising and error because it recognizes the upward as a bad coordinate. I leave a minimal working example to reproduce the error:

import numpy as np
import xarray as xr
import xrft

# Build a sample array
# --------------------
# Generate easting and northing
easting = np.linspace(-5e3, 3e3, 91)
northing = np.linspace(4e3, 10e3, 61)

# Generate sample data and the upward coordinate
ee, nn = np.meshgrid(easting, northing)
upward = np.ones_like(ee)
data = ee ** 2 + nn ** 2

# Build the array
dims = ("northing", "easting")
da = xr.DataArray(
    data,
    coords={"easting": easting, "northing": northing, "upward": (dims, upward)},
    dims=dims,
)
print(da)

# Compute FFT
# -----------
xrft.fft(da, true_phase=True, true_amplitude=True)

<xarray.DataArray (northing: 61, easting: 91)>
array([[4.10000000e+07, 4.01190123e+07, 3.92538272e+07, ...,
        2.39649383e+07, 2.44745679e+07, 2.50000000e+07],
       [4.18100000e+07, 4.09290123e+07, 4.00638272e+07, ...,
        2.47749383e+07, 2.52845679e+07, 2.58100000e+07],
       [4.26400000e+07, 4.17590123e+07, 4.08938272e+07, ...,
        2.56049383e+07, 2.61145679e+07, 2.66400000e+07],
       ...,
       [1.21040000e+08, 1.20159012e+08, 1.19293827e+08, ...,
        1.04004938e+08, 1.04514568e+08, 1.05040000e+08],
       [1.23010000e+08, 1.22129012e+08, 1.21263827e+08, ...,
        1.05974938e+08, 1.06484568e+08, 1.07010000e+08],
       [1.25000000e+08, 1.24119012e+08, 1.23253827e+08, ...,
        1.07964938e+08, 1.08474568e+08, 1.09000000e+08]])
Coordinates:
  * easting   (easting) float64 -5e+03 -4.911e+03 -4.822e+03 ... 2.911e+03 3e+03
  * northing  (northing) float64 4e+03 4.1e+03 4.2e+03 ... 9.8e+03 9.9e+03 1e+04
    upward    (northing, easting) float64 1.0 1.0 1.0 1.0 ... 1.0 1.0 1.0 1.0
Traceback (most recent call last):
  File "/home/santi/tmp/test.py", line 27, in <module>
    xrft.fft(da, true_phase=True, true_amplitude=True)
  File "/home/santi/.miniforge3/envs/default/lib/python3.9/site-packages/xrft/xrft.py", line 392, in fft
    raise ValueError(
ValueError: The input array contains coordinate variable(s) (['upward']) whose dims include the transform dimension(s) `northing`. Please drop these coordinates (`.drop(['upward']`) before invoking xrft.

Version of xrft: v0.4.0


I think we could improve the detection of the bad coordinates in a way that it doesn’t include any n-dimensional coordinate (where n > 1) so we don’t need to drop them before passing them to fft.

What do you think?

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:7 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
santisolercommented, Nov 11, 2021

I’ve just noticed that pad has a similar problem: it raises a cryptic xarray error when trying to pad a grid with the extra upward coordinate. So probably a single private function should take care of the task of ignoring additional coordinates for both cases, and only a simple call to it inside fft and pad should do the trick.

1reaction
roxyboycommented, Nov 11, 2021

PR #163 was where we implemented the error detection as an easy fix to issue #162 .

Read more comments on GitHub >

github_iconTop Results From Across the Web

Error in code, for program that asks input of index for 2d array ...
Your program should then ask the user for a pair of coordinates, (x, y), separated by a space and return the value at...
Read more >
Problem in setting dimension using rioxarray set_spatial_dims()
I have created a xarray object (data, lats, and lons are 2D ndarray) and set a coordinate system as follows, and it is...
Read more >
TypeError: 'int' object is not subscriptable - STechies
In simple terms, this error occurs when your program has a variable that is treated as an array by your function, but actually,...
Read more >
Chapter 4. NumPy Basics: Arrays and Vectorized Computation
copy() . With higher dimensional arrays, you have many more options. In a two-dimensional array, the elements at each index are no longer...
Read more >
Operations on arrays - OpenCV
Calculates x and y coordinates of 2D vectors from their magnitude and angle. ... If the input array is neither of them, 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