gpt4 book ai didi

python - 在cython中使用C函数读写文件中的数组

转载 作者:行者123 更新时间:2023-11-28 20:42:48 25 4
gpt4 key购买 nike

我正在用 cython 实例化一个类。我想声明其中一个实例,它是一个 float 值数组,方法是使用给定函数对其进行计算,然后使用 c 函数将其保存在二进制文件中。如果输入文件已经存在,为了进一步调用我的类,实例将通过从文件中读取数组来声明,否则它将再次计算它。

import numpy as np
cimport numpy as np
cimport cython

from libc.stdio cimport FILE, fopen, fwrite, fscanf, fclose, fseek, SEEK_END, ftell, stdout, stderr
cdef extern from "math.h":
double exp(double) nogil
double log(double) nogil

cdef class Hit(object):
cdef public double[::1] zs, Da
cdef char* path

def __cinit__(self, zs=None, path=None):
if path is None:
raise ValueError("Could not find a path to the file which contains the table of distances")
else:
self.path=path
if zs is None:
raise ValueError("You must give an array which contains the steps!")
self.zs=zs
cdef Py_ssize_t i, N
N=len(self.zs)
cdef FILE *ptr_fr
cdef FILE *ptr_fw
cdef double[::1] ptr_d = np.empty((N,))
ptr_fr = fopen(self.path, "rb")
ptr_fw = fopen(self.path, "wb")
if (ptr_fr==NULL):
print "I/O Error: cannot open file {}".format( self.path)
for i from N > i >= 0:
ptr_d[i]=log(self.zs[i]+1.) /(1- self.zs[i])**0.5
if (ptr_fw == NULL):
print "Unable to open file!\n"
else:
print "Opened file successfully for writing.\n"
fwrite(<void*>&ptr_d[0], sizeof(double), N, ptr_fw)
fclose(ptr_fw)
self.Da = ptr_d

else:
for i from N > i >= 0:
fscanf(ptr_fr,"%f", &ptr_d[i])

fclose(ptr_fr)
self.Da = ptr_d

当我第二次运行我的代码时,从读取文件返回到指针的值是正确的,但是我认为我将指针分配给内存 View 的方式有问题,因为 self 中的所有值.Da 实例为零。有什么建议吗?!!

最佳答案

我自己的问题的答案是:

import numpy as np
cimport numpy as np
cimport cython
from libc.stdio cimport FILE, fopen, fwrite, fscanf, fclose, fprintf, fseek, ftell, SEEK_END, rewind, fread
from numpy cimport float64_t
from libc.stdlib cimport malloc, free
cdef extern from "math.h":
double exp(double) nogil
double log(double) nogil

cdef class Hit(object):
cdef public double[::1] zs, Da
cdef char* path

@cython.boundscheck(False)
@cython.cdivision(True)
@cython.wraparound(False)
@cython.nonecheck(False)
def __cinit__(self, path=None, zs=None):
if path is None:
raise ValueError("Could not find a path to the file which contains the table of distances")
else:
self.path=path

if zs is None:
raise ValueError("You must give an array which contains the steps!")
self.zs=zs

cdef Py_ssize_t i, N, lSize
N=len(np.ascontiguousarray(self.zs))
print "Input file should have ",N
cdef FILE *ptr_fr
cdef FILE *ptr_fw
cdef size_t result
cdef double *ptr_d= <double *>malloc(N * sizeof(double))
ptr_fr = fopen(self.path, "rb")
if (ptr_fr==NULL):
print "I/O Error: cannot open file {}".format( self.path)
for i from N > i >= 0:
ptr_d[i]=log(self.zs[i])
print ptr_d[i]
ptr_fw = fopen(self.path, "wb")
if (ptr_fw == NULL):
print "Unable to open file!\n"

else:

print "Opened file successfully for writing.\n"
fwrite(ptr_d, sizeof(double), N, ptr_fw)
fclose(ptr_fw)
self.Da = np.asarray(<double[:N]>ptr_d)
else:
fseek (ptr_fr , 0 , SEEK_END)
lSize = ftell (ptr_fr)
print lSize
rewind (ptr_fr)
result=fread(ptr_d,sizeof(double),lSize ,ptr_fr )
for i from N > i >= 0:
print ptr_d[i]
fclose(ptr_fr)
self.Da = np.asarray(<double[:N]>ptr_d)
print np.ascontiguousarray(self.Da)

free(ptr_d)

关于python - 在cython中使用C函数读写文件中的数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29950407/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com