Given a list of irregularly spaced data points that are used to describe a line or curve or some noisy function, I would like to convert the data to an image-like numpy array, where each "pixel" either contains data or not (say, 1/0 for data / no data). A similar question was asked here; however, I do not necessarily have a data point for each possible x position, and my points are not necessarily integer values.
For example, say I wanted my output image array to span -10 <= x <= 30, 0 <= y <= 20 with each element in the array spanning 1 distance unit (image array would be 20x40). I have data points [(0.1, 0), (20, 5), (23.5, 18)] that I want to "plot". I would like to find all elements in the array that fall on the line segments created by connecting these points, and populate the array by setting these elements equal to 1 and the rest equal to 0.
EDIT: This is a start, but it seems to convert an entire plot into an image, not just the data (so, it includes axes, labels, etc).
Related
I have an Numpy array of the size (size_x, size_y) holding different values. These values are a Gaussian random field and the size in both dimension is given.
Also, I have a Numpy array of the size (nr_points, 2) with an amount of two-dimensional coordinates. nr_points is the amount of xy-coordinates in this array and given.
The sizes (size_x, size_y) are different of the bounding box of all points given in the second array.
How do I efficiently scale and map the values of the first array to the points?
Here is a graphical sketch of the desired task.
Normalize the coordinate values to the range of the size of the field array which will probably produce fractional (not-integer) coordinates.
scale = (field_array_size - 1) / (coord_max - coord_min)
scaled_coords = coordinates * scale
normed_coords = scaled_coords - scaled_coords_min
coordinate x values should be scaled to the field array x dimension size
coordinate y values should be scaled to the field array y dimension size
You can only index the field array with integers so you have two choices:
round the new coordinates to zero decimal places and convert to ints then use them as indices
interpolate the field array values using the new coordinates
I have an array of object with size (22, 2) for Y axis, and the corresponding x-axis is an array of object with size (22, 2) as well. I need to calculate the Area between the curve y = f(x), and the x-axis, for each array.
I wrote this code:
Area = np.trapz(y,x)
I got an array of object in which each module consists of many elements, but I expected to get a single value (area) for each y=f(x).
How can I calculate correctly the area for each function f(x) stored in the array of object?
I'm trying to convert between coordinate systems for two set of 1D arrays. I know how to make this particular conversion if both 1D arrays had the same length, but that is not the case, as seen below:
y = np.linspace(0,360, 90)
x = np.linspace(2,90,22)
The overall goal is to create a meshgrid out of the new set of arrays, but getting to that point seems to be rather challenging. I've initially tried using two for loops that enumerates from 0 to 89, and then from 0 to 21, but that was a dead end.
I am trying to subtract two matrices of different shapes using broadcasting. However I am stuck on some point. Need simple solution of how to solve the problem.
Literally I am evaluating data on a grid (first step is subtracting). For example I have 5 grid points grid = (-20,-10, 0, 10, 20) and array of data of length 100.
Line:
u = grid.reshape((ngrid, 1)) - data
works perfectly fine. ngrid = 5 in this trivial example.
Output is matrix of 5 rows and 100 columns, so each point of data is evaluated on each point of grid.
Next I want to do it for 2 grids and 2 data sets simultaneously (data is of size (2x100, e.g. 2 randn arrays). I have already succeeded in subtracting two data sets from one grid, but using two grids throws an error.
In the example below a is vertical array of the grid, length 5 points and data is array of random data of the shape (100,2).
In this case u is is tuple (2,5,100), so u[0] and u[1] has 5 rows and 100 columns, meaning that data was subtracted correctly from the grid.
Second line of the code is what I am trying to do. The error is following:
ValueError: operands could not be broadcast together with shapes (5,2) (2,1,100)
u = a - data.T[:, None] # a is vertical grid of 5 elements. Works ok.
u = grid_test - data.T[:, None] # grid_test is 2 column 5 row matrix of 2 grids. Error.
What I need is kind of same line of code as above, but it should work if "a" contains 2 columns, e.g. two different grids. So in the end expected result is "u", which contains in addition to above described results another two matrices where same data (both arrays) evaluated on the second grid.
Unfortunately I cannot use any loops - only vectorization and broadcasting.
Thanks in advance.
I have been trouble working out how to use the scipy.interpolate functions (either LinearNDInterpolator, griddata or Preferably NearestNDInterpolator)
There are some tutorials online but i am confused what form my data needs to be in.
The online documentation for nearestND is terrible.
The function asks for:
x : (Npoints, Ndims) ndarray of floats
Data point coordinates.
y : (Npoints,) ndarray of float or complex
Data point values.
I have data in the form: lat,long,data,time held within an xarray dataset. There are some gaps in the data I would like to fill in.
I don't understand how to tell the function my x points.
i have tried (lat,long) as a tuple and np.meshgrid(lat,long) but can't seem to get it going.
Any help on how i can pass my lat,long coordinates into the function? Bonus points for time coordinates as well to make the estimates more robust through the third dimension.
Thanks!
i have tried (lat,long) as a tuple
If lat and long are 1D arrays or lists, try this:
points = np.array((lat, long)).T # make a 2D array of shape Npoints x 2
nd = NearestNDInterpolator(points, data)
The you can compute interpolated values as nd(lat1, long1), etc.
Scipy provides multivariate interpolation methods for both unstructured data and data point regularly placed on a grid. Unstructured data means the data could be provided as a list of non-ordered points. It seems that your data is structured: it is an array of size (480, 2040). However, the NearestNDInterpolator works on unstructured data. The flatten method can be used to transform the array to a list (1d) of value (of length 480*2040). The same have to be done for the coordinates. meshgrid is used to have the coordinates for every points of the grid, and again flatten is used to obtain a "list" of 2d coordinates (an array of shape 480*2040 x 2).
Here is an example which go from structured data to unstructured:
import numpy as np
lat = np.linspace(2, 6, 10)
lon = np.linspace(5, 9, 14)
latM, lonM = np.meshgrid(lat, lon) # M is for Matrix
dataM = np.sin(latM)*np.cos(lonM) # example of data, Matrix form
from scipy.interpolate import NearestNDInterpolator
points = np.array((latM.flatten(), lonM.flatten())).T
print( points.shape )
# >>> (140, 2)
f_nearest = NearestNDInterpolator(points, dataM.flatten())
f_nearest(5, 5)
Working with NaNs should not be a big problem in this case, because it is just a missing point in the list, except that the coordinates of the missing points have to be removed from the list too.