sup3r.utilities.regridder.Regridder
- class Regridder(source_meta, target_meta, cache_pattern=None, leaf_size=4, k_neighbors=4, n_chunks=100, max_distance=None, max_workers=None)[source]
Bases:
object
Basic Regridder class. Builds ball tree and runs all queries to create full arrays of indices and distances for neighbor points. Computes array of weights used to interpolate from old grid to new grid.
Get weights and indices used to map from source grid to target grid
- Parameters:
source_meta (pd.DataFrame) – Set of coordinates for source grid
target_meta (pd.DataFrame) – Set of coordinates for target grid
cache_pattern (str | None) – Pattern for cached indices and distances for ball tree. Will load these if provided. Should be of the form ‘./{array_name}.pkl’ where array_name will be replaced with either ‘indices’ or ‘distances’.
leaf_size (int, optional) – leaf size for BallTree
k_neighbors (int, optional) – number of nearest neighbors to use for interpolation
n_chunks (int) – Number of spatial chunks to use for tree queries. The total number of points in the target_meta will be split into n_chunks and the points in each chunk will be queried at the same time.
max_distance (float | None) – Max distance to new grid points from original points before filling with nans.
max_workers (int | None) – Max number of workers to use for running all tree queries needed to building full set of indices and distances for each target_meta coordinate.
Methods
Cache indices and distances from ball tree query
get_all_queries
([max_workers])Query ball tree for all coordinates in the target_meta and store results
get_spatial_chunk
(s_slice)Get list of coordinates in target_meta specified by the given spatial slice
Initialize arrays for tree queries and either load query cache or perform all queries
interpolate
(distance_chunk, values)Interpolate to new coordinates based on distances from those coordinates and the values of the points at those distances
Load cached indices and distances from ball tree query
query_tree
(s_slice)Get indices and distances for points specified by the given spatial slice
run
(source_meta, target_meta[, ...])Query tree for every point in target_meta to get full set of indices and distances for the neighboring points in the source_meta.
save_query
(s_slice)Save tree query for coordinates specified by given spatial slice
Attributes
MAX_DISTANCE
MIN_DISTANCE
Check if cache exists before building tree.
Mask for points too far from original grid
Get name of cache distances file
Get distances for all tree queries.
Get name of cache indices file
Get indices for all tree queries.
Build ball tree from source_meta
Get weights used for regridding
- property distances
Get distances for all tree queries.
- property indices
Get indices for all tree queries.
- init_queries()[source]
Initialize arrays for tree queries and either load query cache or perform all queries
- classmethod run(source_meta, target_meta, cache_pattern=None, leaf_size=4, k_neighbors=4, n_chunks=100, max_workers=None)[source]
Query tree for every point in target_meta to get full set of indices and distances for the neighboring points in the source_meta.
- Parameters:
source_meta (pd.DataFrame) – Set of coordinates for source grid
target_meta (pd.DataFrame) – Set of coordinates for target grid
cache_pattern (str | None) – Pattern for cached indices and distances for ball tree. Will load these if provided. Should be of the form ‘./{array_name}.pkl’ where array_name will be replaced with either ‘indices’ or ‘distances’.
leaf_size (int, optional) – leaf size for BallTree
k_neighbors (int, optional) – number of nearest neighbors to use for interpolation
n_chunks (int) – Number of spatial chunks to use for tree queries. The total number of points in the target_meta will be split into n_chunks and the points in each chunk will be queried at the same time.
max_workers (int | None) – Max number of workers to use for running all tree queries needed to building full set of indices and distances for each target_meta coordinate.
- property weights
Get weights used for regridding
- property cache_exists
Check if cache exists before building tree.
- property tree
Build ball tree from source_meta
- get_all_queries(max_workers=None)[source]
Query ball tree for all coordinates in the target_meta and store results
- property index_file
Get name of cache indices file
- property distance_file
Get name of cache distances file
- get_spatial_chunk(s_slice)[source]
Get list of coordinates in target_meta specified by the given spatial slice
- Parameters:
s_slice (slice) – slice specifying which spatial indices in the target grid should be selected. This selects n_points from the target grid
- Returns:
ndarray – Array of n_points in target_meta selected by s_slice.
- query_tree(s_slice)[source]
Get indices and distances for points specified by the given spatial slice
- Parameters:
s_slice (slice) – slice specifying which spatial indices in the target grid should be selected. This selects n_points from the target grid
- Returns:
distances (ndarray) – Array of distances for neighboring points for each point selected by s_slice. (n_ponts, k_neighbors)
indices (ndarray) – Array of indices for neighboring points for each point selected by s_slice. (n_ponts, k_neighbors)
- property dist_mask
Mask for points too far from original grid
- Returns:
mask (ndarray) – Bool array for points outside original grid extent
- classmethod interpolate(distance_chunk, values)[source]
Interpolate to new coordinates based on distances from those coordinates and the values of the points at those distances
- Parameters:
distance_chunk (ndarray) – Chunk of the full array of distances where distances[i] gives the list of k_neighbors distances to the source coordinates to be used for interpolation for the i-th coordinate in the target data. (n_points, k_neighbors)
values (ndarray) – Array of values corresponding to the point distances with shape (temporal, n_points, k_neighbors)
- Returns:
ndarray – Time series of values at interpolated points with shape (temporal, n_points)
- __call__(data)[source]
Regrid given spatiotemporal data over entire grid
- Parameters:
data (ndarray) – Spatiotemporal data to regrid to target_meta. Data can be flattened in the spatial dimension to match the target_meta or be in a 2D spatial grid, e.g.: (spatial, temporal) or (spatial_1, spatial_2, temporal)
- Returns:
out (ndarray) – Flattened regridded spatiotemporal data (spatial, temporal)