[Python scipy] Upscale / downscale 2D data

Vocabulary Book. Reprinting a lot because it is troublesome to search every time. There is an original URL.

Sometimes it gets confusing:

--Upscale: Coarse resolution --Downscale: Finer resolution

Value interpolation is summarized here: [SciPy.org: Interpolation (scipy.interpolate)] (https://docs.scipy.org/doc/scipy-0.14.0/reference/interpolate.html)

Of these, the one that is often used for upscale / downscale

Example using RegularGridInterpolator

[stackoverflow: Scipy interpolation with masked data?] (https://stackoverflow.com/questions/35807321/scipy-interpolation-with-masked-data) Is easy to understand. In the case of MaskedArray, it is faster to convert the mask itself (rather than filling the masked part with np.nan).

import numpy as np
from scipy import interpolate

def conv_resol(arr, newshape, *args, **kwargs):
    '''
    Args:
        arr [2d ndarray or MaskedArray]
        newshape [tuple of int]
        *args, **kwargs: for interpolate.RegularGridInterpolator
    '''
    nx0, ny0 = arr.shape
    nx1, ny1 = newshape
    x0 = np.linspace(0, 1, nx0)
    y0 = np.linspace(0, 1, ny0)
    x1 = np.linspace(0, 1, nx1)
    y1 = np.linspace(0, 1, ny1)
    x1, y1 = np.meshgrid(x1, y1) # x1 [ny1,nx1], y1 [ny1, nx1]
    xy1 = np.array((x1, y1)).T   # xy1 [nx1, ny1, 2]
    arr1 = interpolate.RegularGridInterpolator((x0, y0), arr, *args, **kwargs)(xy1)
    if isinstance(arr, np.ma.MaskedArray):
        mask1 = interpolate.RegularGridInterpolator((x0, y0), arr.mask.astype('float'), *args, **kwargs)(xy1) > 0.0
        return np.ma.masked_array(arr1, mask=mask1)
    return arr1

test

import matplotlib.pyplot as plt


nx0, ny0 = 10, 20
arr0 = np.arange(nx0 * ny0).reshape(nx0, ny0) * (1.0 / (nx0 * ny0))

nx1, ny1 = 5, 10
arr1 = conv_resol(arr0, (nx1, ny1))

nx2, ny2 = 20, 40
arr2 = conv_resol(arr0, (nx2, ny2))

plt.figure(figsize=(12,8))
plt.subplot(2,2,1)
plt.imshow(arr0, vmin=0.0, vmax=1.0)
plt.subplot(2,2,3)
plt.imshow(arr1, vmin=0.0, vmax=1.0)
plt.subplot(2,2,4)
plt.imshow(arr2, vmin=0.0, vmax=1.0)

image.png

Recommended Posts

[Python scipy] Upscale / downscale 2D data
Data analysis python
[python] Read data
Create 3D scatter plot with SciPy + matplotlib (Python)
Data analysis with python 2
Python Data Visualization Libraries
Calculate 2D IDCT ~ Python
Data analysis overview python
Data cleaning using Python
Python: Diagram of 2D data distribution (kernel density estimation)
Python data analysis template
[Python] Sorting Numpy data
Normarize data with Scipy
Data analysis with Python
Sample data created with python
My python data analysis container
Handle Ambient data in Python
data structure python push pop
Python for Data Analysis Chapter 4
Export 3D data from QGIS
Display UTM-30LX data in Python
Create 3d gif with python3
Get Youtube data with python
Python: 3D array image (numpy.array)
python> Handling of 2D arrays
My python data analytics environment
Python application: data visualization # 2: matplotlib
Python data analysis learning notes
Python for Data Analysis Chapter 2
AtCoder ABC 182 Python (A ~ D)
Image data type conversion [Python]
Data analysis using python pandas
Python 2D array trap [Copy array]
Interpolate 2D data with scipy.interpolate.griddata
python> tuple> data, address = s.recvfrom (10000)
Python for Data Analysis Chapter 3
Read json data with python
[Python] [3D line graph] Multiple data in one graph, axis values in characters