_ArrayMemoryError: Unable to allocate



我正在尝试处理数据,但我不断运行此错误:numpy.core._exceptions。_ArrayMemoryError:无法为形状为(32761,32761)且数据类型为float64的数组分配8.00 gb内存这是我的代码:

import numpy as np
import csv
import tkinter as tk
from tkinter import filedialog
"""
Importing data 
"""
root= tk.Tk()
canvas1 = tk.Canvas(root, width = 300, height = 300, bg = 'gray1', relief = 'raised')
canvas1.pack()
def getCSV ():
global df

import_file_path = filedialog.askopenfilename()
df = np.genfromtxt(import_file_path, delimiter=' ')

print (df)

browseButton_CSV = tk.Button(text="      Import CSV File     ", command=getCSV, bg='OrangeRed4', fg='black', font=('helvetica', 12, 'bold'))
canvas1.create_window(150, 150, window=browseButton_CSV)
root.mainloop()
x, y, mag = df[:,0], df[:,1], df[:,3]
Xshape, Yshape, MAGshape = np.shape(x), np.shape(y), np.shape(mag)

def fftfreqs(x, y, shape, windowLen):
"""
Get two 2D-arrays with wavenumbers [rads/km] in x, y directions.
"""
nx = ny = shape[0]

dx = (x.max() - x.min())/(nx - 1)           # Spacing
fx = 2*np.pi*np.fft.fftfreq(windowLen[0], dx)

dy = (y.max() - y.min())/(ny - 1)           # Spacing
fy = 2*np.pi*np.fft.fftfreq(windowLen[1], dy)

return np.meshgrid(fy, fx)[::-1]

"""
Calculation of power spectrum density
"""
shap = (np.shape(x)[0], np.shape(y)[0])
kx, ky = fftfreqs(x, y, shap, shap)
pds = (abs(np.fft.fft2(np.reshape(mag, (1,shap[0])))))**2
"""
Calculation of Radially Averaged Power Spectrum
"""
nx, ny = pds.shape
max_radius = min(kx.max(), ky.max())
ring_width = max(np.unique(kx)[np.unique(kx) > 0][0], np.unique(ky)[np.unique(ky) > 0][0])
k = np.sqrt(kx**2 + ky**2)
pds_radial = []
k_radial = []
radius_i = -1
while True:
radius_i += 1
if radius_i*ring_width > max_radius:
break
else:
if radius_i == 0:
inside = k <= 0.5*ring_width
else:
inside = np.logical_and(k > (radius_i - 0.5)*ring_width, k <= (radius_i + 0.5)*ring_width)
pds_radial.append(pds[inside].mean())
k_radial.append(radius_i*ring_width)

我在8GB RAM系统上运行它,但我也试图在GOOGLE COLAB上运行它,但结果相同。提前感谢

我不是快速傅里叶变换的专家,所以我不能告诉你你所做的是否有意义。然而,我认为你的MemoryError来自这样一个事实,即你在同一个命令中处理的不仅仅是一个(32761,32761)数组。所以,也许你能分配第一个,但也许不能分配第二个;你懂的。看看我下面的建议,如果这对你有帮助,请告诉我。

import numpy as np
import tkinter as tk
from tkinter import filedialog

def getCSV():
global df
import_file_path = filedialog.askopenfilename()
df = np.genfromtxt(import_file_path, delimiter=' ')

def fftfreqs():
"""
Get two 2D-arrays with wavenumbers [rads/km] in x, y directions.
"""
nx = ny = df.shape[0]
dx = (x.max() - x.min()) / (nx - 1)  # Spacing
fx = 2 * np.pi * np.fft.fftfreq(nx, dx)
dy = (y.max() - y.min()) / (ny - 1)  # Spacing
fy = 2 * np.pi * np.fft.fftfreq(ny, dy)
return np.meshgrid(fy, fx)[::-1]

"""
Importing data
"""
root = tk.Tk()
canvas1 = tk.Canvas(root, width=300, height=300, bg='gray1', relief='raised')
canvas1.pack()
browseButton_CSV = tk.Button(text="      Import CSV File     ", command=getCSV,
bg='OrangeRed4', fg='black',
font=('helvetica', 12, 'bold'))
canvas1.create_window(150, 150, window=browseButton_CSV)
root.mainloop()
x, y, mag = df[:, 0], df[:, 1], df[:, 3]
"""
Calculation of power spectrum density
"""
kx, ky = fftfreqs()
pds = np.fft.fft2(np.reshape(mag, (1, df.shape[0]))) ** 2
"""
Calculation of Radially Averaged Power Spectrum
"""
max_radius = min(kx.max(), ky.max())
print(kx.shape)  # (32761, 32761)
print(ky.shape)  # (32761, 32761)
# Do not do this
ring_width = max(np.unique(kx)[np.unique(kx) > 0]
[0], np.unique(ky)[np.unique(ky) > 0][0])
# Do something like this instead
kx_unique = np.unique(kx)
# Process kx_unique
del kx_unique
ky_unique = np.unique(ky)
# Process ky_unique
del ky_unique
ring_width = max(, )
k = np.sqrt(kx**2 + ky**2)
pds_radial = []
k_radial = []
radius_i = -1
while True:
radius_i += 1
if radius_i * ring_width > max_radius:
break
else:
if radius_i == 0:
inside = k <= 0.5 * ring_width
else:
inside = np.logical_and(
k > (radius_i - 0.5) * ring_width,
k <= (radius_i + 0.5) * ring_width)
pds_radial.append(pds[inside].mean())
k_radial.append(radius_i * ring_width)

相关内容