_ArrayMemoryError:无法分配

2024-05-15 09:30:00 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图处理数据,但经常出现以下错误:numpy.core.\u异常。\u ArrayMemoryError:无法为具有形状(32761、32761)和数据类型float64的数组分配8.00 GiB 这是我的代码:

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内存系统上运行它,但我也尝试在GOOGLE COLAB上运行它,但结果相同。 提前谢谢


Tags: importdfnpwidthmaxtkuniquepds
1条回答
网友
1楼 · 发布于 2024-05-15 09:30:00

我远非快速傅里叶变换专家,因此我无法告诉您所做的是否有意义。但是,我认为您的MemoryError来自这样一个事实:您在同一个命令中处理的数组不止一个(3276132761)。所以,也许你能分配第一个,但也许不能分配第二个;你明白了。看看我下面的建议,让我知道这是否对你有所改善

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)

相关问题 更多 >