Python Pyinstaller可执行文件的Numpy部分导入
我正在为一个GUI应用程序编写代码,该程序运行良好,但是我希望将其捆绑到一个exe/app自包含文件中,以使其尽可能友好。 作为代码的一部分,我编写了一个Cython扩展模块,它可以非常快地执行一些线性代数。此模块使用Numpy将元组的输入元组转换为数组,然后使用Numpy数组上的memoryview执行所有计算。这一切都非常棒,速度也非常快,但是,当我制作包时,我需要导入整个Numpy包,根据版本的不同,它的大小在50 Mb到200 Mb之间,而我实际上只需要Numpy.ndarray和Numpy.empty。我的可执行文件不带Numpy为10 Mb,带。。。 我不使用任何其他函数,并且我自己编写了基本运算中的所有线性代数。Cython代码如下Python Pyinstaller可执行文件的Numpy部分导入,python,arrays,numpy,cython,pyinstaller,Python,Arrays,Numpy,Cython,Pyinstaller,我正在为一个GUI应用程序编写代码,该程序运行良好,但是我希望将其捆绑到一个exe/app自包含文件中,以使其尽可能友好。 作为代码的一部分,我编写了一个Cython扩展模块,它可以非常快地执行一些线性代数。此模块使用Numpy将元组的输入元组转换为数组,然后使用Numpy数组上的memoryview执行所有计算。这一切都非常棒,速度也非常快,但是,当我制作包时,我需要导入整个Numpy包,根据版本的不同,它的大小在50 Mb到200 Mb之间,而我实际上只需要Numpy.ndarray和Num
from numpy import empty, array, int32
cimport cython
@cython.cdivision(True)
cdef int MCD(int a, int b):
cdef int r
if a > b:
a, b = b, a
r = a % b
while r > 0:
a, b = b, r
r = a % b
return b
@cython.wraparound(False)
@cython.cdivision(True)
cdef int GCD(int [:] iterable):
cdef int index, res
res = 0
for index in range(len(iterable)):
if iterable[index] != 0:
if res == 0:
res = abs(iterable[index])
else:
res = MCD(res, abs(iterable[index]))
if res == 0:
res = 1
return res
@cython.wraparound(False)
@cython.cdivision(True)
cdef int [:] C_Gauss(int [:, :] Arr):
cdef int [:, :] arr, arr_, new_arr
cdef int x_max, y_max, i, j, ii, det, g, c_
cdef int [:] temp, sol
arr_ = Arr
arr = arr_.T.copy()
x_max, y_max = arr.shape[0], arr.shape[1]
sol = arr_[:y_max-1, 0].copy()
sol [:] = 0
for j in range (y_max-1):
if arr[j,j] == 0:
for jj in range(j+1, x_max):
if arr[jj, j] != 0:
temp = arr[j, :].copy()
arr [j, :] = arr [jj, :]
arr [jj, : ] = temp
break
else:
break
for i in range(j+1, x_max):
if arr[i, j] != 0:
for jj in range(y_max-1, j-1, -1): #####
arr[i, jj] = arr[j,j]*arr [i, jj]- arr[i,j]*arr[j,jj]
g = GCD(arr[i, :])
if g != 1:
for jj in range(y_max-1, j, -1):
arr [i , jj] = arr [i , jj]//g
else:
for jj in range(y_max-1, x_max):
if arr[jj, y_max-1] != 0:
break
else:
new_arr = arr[:y_max-1, :]
det = 1
for j in range (y_max-2, 0, -1):
det *= new_arr[j,j]
for i in range(j):
if new_arr[i, j] != 0:
c_ = new_arr[i,j]
for jj in range(i, y_max):
new_arr[i, jj] = new_arr[i,jj]*new_arr[j, j] - new_arr[j,jj]*c_
g = GCD(new_arr[i, :])
for jj in range(i, y_max):
new_arr [i, jj] = new_arr [i, jj]//g
for j in range(y_max-1):
new_arr[j, y_max-1] *= det
new_arr[j, y_max-1] = new_arr[j, y_max-1]//new_arr[j, j]
sol = new_arr[:, y_max-1]
return sol
@cython.wraparound(False)
@cython.cdivision(True)
def Screen(tuple families, tuple solution_vector, Py_ssize_t solution_code, Py_ssize_t no_triplets):
cdef int [:, :] input_data, vectors, Augmented
cdef int [:] solution, combinations, codes, multiplicities, indices, ratios, sol_indices
cdef int N, I, J, K, CombinedAminoAcids, indexing, is_negative, half_mark, mcd
cdef list output = []
input_data = array(families, order = 'F', dtype = int32)
solution = array(solution_vector, dtype = int32)
combinations = empty(shape = (no_triplets,), dtype = int32)
codes = input_data[:, 0]
indices = input_data[:, 1]
multiplicities = input_data[:, 2]
vectors = input_data[:, 3:]
N = solution.shape[0]
M = input_data.shape[0]
Augmented = empty(shape = (no_triplets+1, N), dtype = int32)
Augmented[no_triplets, :] = solution
for I in range(no_triplets):
combinations[I] = M -1 - I
combinations[no_triplets - 1] += 1
indexing = 0
half_mark = no_triplets // 2 + 1
while combinations[0] > no_triplets - 1:
K = no_triplets - 1
for J in range(no_triplets - 1, -1, -1):
if combinations[J] > no_triplets - 1 - J:
K = J
break
combinations[K] -= 1
for J in range(1, no_triplets - K):
combinations[K + J] = combinations[K] - J
CombinedAminoAcids = 0
for I in range(no_triplets):
CombinedAminoAcids |= codes[combinations[I]]
if CombinedAminoAcids == solution_code:
for I in range(no_triplets):
Augmented[I, :] = vectors[combinations[I], :]
indexing += 1
ratios = C_Gauss(Augmented)
sol_indices = ratios.copy()
is_negative = 0
for I in range(no_triplets):
if ratios[I] == 0:
break
elif ratios[I] < 0:
is_negative += 1
ratios[I] *= multiplicities[combinations[I]]
sol_indices[I] = indices[combinations[I]]
else:
mcd = GCD(ratios)
if is_negative >= half_mark:
mcd *= -1
for I in range(no_triplets):
ratios[I] /= mcd
output.append( (tuple(sol_indices), tuple(ratios)) )
return output
从numpy导入空、数组、int32
西姆波特赛顿酒店
@cython.cdivision(真)
cdef内部MCD(内部a、内部b):
cdef int r
如果a>b:
a、 b=b,a
r=a%b
当r>0时:
a、 b=b,r
r=a%b
返回b
@cython.wrapparound(假)
@cython.cdivision(真)
cdef int GCD(int[:]iterable):
cdef int索引,res
res=0
对于范围内的索引(len(iterable)):
如果可编辑[索引]!=0:
如果res==0:
res=abs(可编辑[索引])
其他:
res=MCD(res,abs(可编辑[索引])
如果res==0:
res=1
返回res
@cython.wrapparound(假)
@cython.cdivision(真)
cdef int[:]C_Gauss(int[:,:]Arr):
cdef int[:,:]arr,arr_uu,new_uarr
cdef int x_max,y_max,i,j,ii,det,g,c_
cdef int[:]温度,溶胶
arr=arr
arr=arr_uu.T.copy()
x_max,y_max=arr.shape[0],arr.shape[1]
sol=arr\u[:y\u max-1,0].copy()
溶胶[:]=0
对于范围内的j(y_max-1):
如果arr[j,j]==0:
对于范围内的jj(j+1,x_最大值):
如果arr[jj,j]!=0:
temp=arr[j,:].copy()
arr[j,:]=arr[jj,:]
arr[jj,:]=温度
打破
其他:
打破
对于范围内的i(j+1,x_最大值):
如果arr[i,j]!=0:
对于范围内的jj(y_max-1,j-1,-1):#####
arr[i,jj]=arr[j,j]*arr[i,jj]-arr[i,j]*arr[j,jj]
g=GCD(arr[i,:]))
如果g!=1:
对于范围内的jj(y_max-1,j,-1):
arr[i,jj]=arr[i,jj]//g
其他:
对于范围内的jj(y_max-1,x_max):
如果arr[jj,y_max-1]!=0:
打破
其他:
new_arr=arr[:y_max-1,:]
det=1
对于范围内的j(y_max-2,0,-1):
det*=新的_arr[j,j]
对于范围(j)内的i:
如果新的[i,j]!=0:
c_uu=new_uarr[i,j]
对于范围内的jj(i,y_max):
new_arr[i,jj]=new_arr[i,jj]*new_arr[j,j]-new_arr[j,jj]*c_
g=GCD(新阵列[i,:])
对于范围内的jj(i,y_max):
new_arr[i,jj]=new_arr[i,jj]//g
对于范围内的j(y_max-1):
新的\u arr[j,y\u max-1]*=det
new_arr[j,y_max-1]=new_arr[j,y_max-1]//new_arr[j,j]
sol=新阵列[:,y_max-1]
返回溶胶
@cython.wrapparound(假)
@cython.cdivision(真)
def屏幕(元组族、元组解向量、Py_ssize_t解代码、Py_ssize_t no_三元组):
cdef int[:,:]输入数据,向量,增强
cdef int[:]解决方案、组合、代码、多重性、索引、比率、sol_索引
cdef int N,I,J,K,组合氨基酸,索引,为负,半标记,mcd
cdef列表输出=[]
输入数据=数组(族,顺序='F',数据类型=int32)
解决方案=数组(解决方案\向量,数据类型=int32)
组合=空(形状=(无三元组),数据类型=int32)
代码=输入数据[:,0]
索引=输入数据[:,1]
多重性=输入数据[:,2]
向量=输入数据[:,3:]
N=溶液。形状[0]
M=输入数据。形状[0]
Augmented=empty(shape=(无三元组+1,N),dtype=int32)
扩充[无三元组:]=解决方案
对于范围内的I(无三联体):
组合[I]=M-1-I
组合[无三联体-1]+=1
索引=0
half_mark=无三胞胎//2+1
而组合[0]>没有三联体-1:
K=无三联体-1
对于范围内的J(无三联体-1,-1,-1):
如果组合[J]>没有三联体-1-J:
K=J
打破
组合[K]-=1
对于范围内的J(1,无三联体-K):
组合[K+J]=组合[K]-J
组合氨基酸=0
对于范围内的I(无三联体):
组合氨基酸|=代码[组合[I]]
如果组合氨基酸==溶液\代码:
对于范围内的I(无三联体):
增广[I,:]=向量[组合[I],:]
索引+=1
比率=C_高斯(增强)
sol_指数=比率。复制()
是负=0吗
对于范围内的I(无三联体):
如果比率[I]==0:
打破
elif比率[I]<0:
是负的+=1吗
比率[I]*=多重性[组合[I]]
溶胶指数[I]=指数[组合[I]]
其他:
mcd=GCD(比率)
如果为负>=半分:
mcd*=-1
对于范围内的I(无三联体):
比率[I]/=mcd
append((元组(sol_索引)、元组(比率)))
返回输出
我有没有办法避免导入整个Numpy包?我只需要转换函数元组的输入元组