PyKrige Memory error of numpy.core._exceptions._ArrayMemoryError

Viewed 27

I am trying to use the PyKrige module for the first time and ran the following script

import numpy as np
import pandas as pd
import glob
from pykrige.ok import OrdinaryKriging
from pykrige.kriging_tools import write_asc_grid
import pykrige.kriging_tools as kt
import matplotlib.pyplot as plt
from matplotlib.colors import LinearSegmentedColormap
from matplotlib.patches import Path, PathPatch

datafile='C:/myfile.xyz'
df=pd.read_csv(datafile,delimiter='\t')

print(df)

lons=np.array(df['x'])
lats=np.array(df['y'])
data=np.array(df['z'])

grid_space = 0.5
grid_lon = np.arange(np.amin(lons), np.amax(lons), grid_space) #grid_space is the desired delta/step of the output array
grid_lat = np.arange(np.amin(lats), np.amax(lats), grid_space)

OK = OrdinaryKriging(lons, lats, data, variogram_model='gaussian', verbose=True, enable_plotting=False,nlags=20)
z1, ss1 = OK.execute('grid', grid_lon, grid_lat)

kt.write_asc_grid(grid_lon, grid_lat, z1, filename="C:/users/public/output.asc")

print('job complete')

This runs for about a minute and then I get this error

Traceback (most recent call last):
  File "c:\Users\anonymous\Documents\pykrige_kriginginterpolation.py", line 25, in <module>
    z1, ss1 = OK.execute('grid', grid_lon, grid_lat)
  File "C:\Python10\lib\site-packages\pykrige\ok.py", line 847, in execute
    a = self._get_kriging_matrix(n)
  File "C:\Python10\lib\site-packages\pykrige\ok.py", line 642, in _get_kriging_matrix
    a[:n, :n] = -self.variogram_function(self.variogram_model_parameters, d)
  File "C:\Python10\lib\site-packages\pykrige\variogram_models.py", line 45, in gaussian_variogram_model
    return psill * (1.0 - np.exp(-(d**2.0) / (range_ * 4.0 / 7.0) ** 2.0)) + nugget
numpy.core._exceptions._ArrayMemoryError: Unable to allocate 10.2 GiB for an array with shape (36935, 36935) and data type float64

My computer has enough memory, so I'm not sure how to bypass this as i want to check that the results are as expected?

Please also note, as I've said this is a first attempt at using PyKrige to see if I can get it working - if anyone notices any obvious errors, please let me know!

0 Answers
Related