如何简化以下代码,使其运行速度更快



我有一个包含许多2D图像(帧(的三维阵列。我想通过考虑每个像素值的阈值来去除背景,并在新的3D阵列中复制新元素。我写了以下代码行,但运行成本太高。如何加快此代码的速度?

ss = stack #3D array (571, 1040, 1392)
T,ni,nj = ss.shape
Background_intensity = np.ones([T,ni,nj])
Intensity = np.zeros([T,ni,nj])
DeltaF_F_max = np.zeros([T,ni,nj])
for t in range(T):
for i in range(ni):
for j in range(nj):
if ss[t,i,j]<12:
Background_intensity[t,i,j] = ss[t,i,j]
if Background_intensity[t,i,j] == 0 :
Background_intensity[t,i,j] = 1
else:
Intensity[t,i,j] = ss[t,i,j]
DeltaF_F_max[t,i,j]=(((Intensity[t,i,j] - Background_intensity[t,i,j])))/(Background_intensity[t,i,j])

我和Numpy尝试过这个。我不确定你得到了什么结果,但在我的Mac上大约需要20多岁。即使在我将所有大小减少了8倍之后,这也相当占用内存,因为您不需要int64来存储112255下的数字。

我想知道你是否需要一次完成571张图像,或者你是否可以做到这一点;在飞行中"因为你获得了它们,而不是把它们都聚集在一个巨大的团里。

您也可以考虑使用Numba来实现这一点,因为它非常擅长优化for循环-尝试将[numba]放在上面的搜索框中,或者看看这个例子-使用prange来并行CPU核心上的循环。

不管怎样,这是我的代码:

#!/usr/bin/env python3
# https://stackoverflow.com/q/71460343/2836621
import numpy as np
T, ni, nj = 571, 1040, 1392
# Create representative input data, such that around 1/3 of it is < 12 for testing
ss = np.random.randint(0,36,(T,ni,nj), np.uint8)
# Ravel into 1-D representation for simpler indexing
ss_r = ss.ravel()
# Create extra arrays but using 800MB rather than 6.3GB each, also ravelled
Background_intensity = np.ones(T*ni*nj, np.uint8)
Intensity = np.zeros(T*ni*nj, np.uint8)
# Make Boolean (True/False) mask of elements below threshold
mask = ss_r < 12
# Quick check here - print(np.count_nonzero(mask)/np.size(ss)) and check it is 0.333
# Set Background_intensity to "ss" according to mask
Background_intensity[mask] = ss_r[mask]
# Make sure no zeroes present
Background_intensity[Background_intensity==0] = 1
# This corresponds to the "else" of your original "if" statement
Intensity[~mask] = ss_r[~mask]
# Final calculation and reshaping back to original shape
DeltaF_F_max = (Intensity - Background_intensity)/Background_intensity
DeltaF_F_max.reshape((T,ni,nj))

最新更新