为什么我调用cython扩展时ipython和python返回不同的输出?

2024-06-26 00:23:41 发布

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

我使用的是ubuntu14.04和使用Cython扩展的编码算法,以便投影和反向投影大型矩阵。你知道吗

我已经编写了2个月了,我刚刚意识到,当我从CLI或iPython调用Python测试脚本时,我会得到不同的输出和结果。你知道吗

因此,为了回答一些问题,软件由三层组成:C代码(用于执行前向和后向投影)、Cython和算法。你知道吗

你知道吗压缩测试.py(算法)看起来像这样:

import time
from cythonmodule import forproj
from cythonmodule import backproj

def Algorithm(data,imax):
    toc=time.clock()
    res=[]
    print('Algorithm in progress')
    for i in range(imax):
        a = forproj(data)
        b = backproj(data)
        res.append(a,b)
        tic = time.clock()
        if i ==1:
             tval=(tic-toc)*imax
             print('Estimated time until completion (s):'+str(tval))
    print('Algorithm complete')
    return sum(res)


initval=np.arange(45).reshape(3,3,5)
algres = Algorithm(initval,100)

现在,当我使用python setup.py build_ext --inplace构建Cython时,我得到了上面提到的ipython和python之间的不同结果。你知道吗

$ python comptest.py
Algorithm in progress.
Estimated time until completion (s): 39.288388
Algorithm complete.

iPython的输出在时间和最终输出上都非常不同:

$ipython comptest.py
Algorithm in progress.
Esitmated time until completetion (s): 14.63456
Algorithm complete. 

由于python本身不能如此剧烈地改变值,因此我只能假设python和iPython运行的Cython扩展(以及底层的C函数)必须是不同的。你知道吗

我已经确保python版本是相同的(2.7.13)。有没有办法让我看看Python和iPython在调用什么扩展?实际的Cython代码在我的目录中位于哪里?我能找回iPython在用什么吗?你知道吗

更新:

所以我一直在看python和ipython的LineProfile输出,我刚刚意识到这两个的输出非常不同,不仅是backproj和proj,还有python实现的函数。你知道吗

当我打印系统版本但是,对于两者,它返回相同的结果:

Python:

>>> sys.version
'2.7.13 |Anaconda custom (64-bit)| (default, Dec 20 2016, 23:09:15) \n[GCC          4.4.7 20120313 (Red Hat 4.4.7-1)]'

线条轮廓:

Line #      Hits         Time  Per Hit   % Time  Line Contents
==============================================================
62                                           def Algorithm(data,imax):
63         1            4      4.0      0.0      res1=[]
64         1            2      2.0      0.0      res2=[]
65       101          113      1.1      0.0      for i in range(imax):
66       100      5143186  51431.9     73.6          a=forptoj(data)
67       100      1632400  16324.0     23.4          b=backproj(data)
68       100        90969    909.7      1.3          a = a.clip(min=0)
69       100        16414    164.1      0.2          b = b.clip(min=0)
70       100          221      2.2      0.0          res1.append(a)
71       100           79      0.8      0.0          res2.append(b)
72                                           
73         1       107541 107541.0      1.5      return sum(res1),sum(res2)

伊皮顿:

In [2]: sys.version
Out[2]: '2.7.13 |Anaconda custom (64-bit)| (default, Dec 20 2016, 23:09:15) \n[GCC 4.4.7 20120313 (Red Hat 4.4.7-1)]'

线条轮廓:

   Line #      Hits         Time  Per Hit   % Time  Line Contents
   ==============================================================
    62                                           def Algorithm(data,imax):
    63         1            4      4.0      0.0      res1=[]
    64         1            2      2.0      0.0      res2=[]
    65       101          127      1.3      0.0      for i in range(imax):
    66       100      5926013  59260.1     75.3          a=backproj(data)
    67       100      1724570  17245.7     21.9          b=backproj(data)
    68       100        94297    943.0      1.2          a = a.clip(min=0)
    69       100        16758    167.6      0.2          b = b.clip(min=0)
    70       100          238      2.4      0.0          res1.append(a)
    71       100           72      0.7      0.0          res2.append(b)
    72                                           
    73         1       108388 108388.0      1.4      return sum(res1),sum(res2)

差别非常细微,但由于这两个输出应该是相同的,因此提出了一个问题:python在这两种情况下是否真的做了相同的事情。你知道吗


Tags: inpydatatimeipythonlinealgorithmcython
1条回答
网友
1楼 · 发布于 2024-06-26 00:23:41

嘿,我解决了这个问题。我在本页上查看了conda如何建立一个新的环境,与操作系统上以前的所有偏见隔离开来:

https://conda.io/docs/py2or3.html

因为它重置了所有的python版本,所以我检查了这个,结果发现它实际上是python(或者Cython??)搞砸了。如果有人知道为什么会这样,请告诉我!你知道吗

不管怎样,给康达建立一个新的环境,比如:

$ conda create -n py27 python=2.7 anaconda

然后像这样检查:

$source activate py27 

这似乎用不同的输出重置了导致问题的原因。你知道吗

就像他们说的,如果有疑问,就尽可能地重置一切!!你知道吗

谢谢你们的帮助,继续编码。你知道吗

相关问题 更多 >