- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
所以我尝试使用 line_profiler
在我自己的 python 脚本中分析一个函数,因为我想要逐行计时。唯一的问题是该函数是 Cython 函数,并且 line_profiler
无法正常工作。在第一次运行时,它只是因错误而崩溃。然后我添加了
!python
cython: profile=True
cython: linetrace=True
cython: binding=True
在我的脚本的顶部,现在它运行正常,除了时间和统计数据是空白的!
有没有办法将 line_profiler
与 Cythonized 函数一起使用?
我可以分析非 Cythonized 函数,但它比 Cythonized 慢得多,以至于我无法使用来自分析的信息 - 纯 python 函数的缓慢使得我无法改进 Cython一个。
这是我要分析的函数的代码:
class motif_hit(object):
__slots__ = ['position', 'strand']
def __init__(self, int position=0, int strand=0):
self.position = position
self.strand = strand
#the decorator for line_profiler
@profile
def find_motifs_cython(list bed_list, list matrices=None, int limit=0, int mut=0):
cdef int q = 3
cdef list bg = [0.25, 0.25, 0.25, 0.25]
cdef int matrices_length = len(matrices)
cdef int results_length = 0
cdef int results_length_shuffled = 0
cdef np.ndarray upper_adjust_list = np.zeros(matrices_length, np.int)
cdef np.ndarray lower_adjust_list = np.zeros(matrices_length, np.int)
#this one need to be a list for MOODS
cdef list threshold_list = [None for _ in xrange(matrices_length)]
cdef list matrix_list = [None for _ in xrange(matrices_length)]
cdef np.ndarray results_list = np.zeros(matrices_length, np.object)
cdef int count_seq = len(bed_list)
cdef int mat
cdef int i, j, k
cdef int position, strand
cdef list result, results, results_shuffled
cdef dict result_temp
cdef int length
if count_seq > 0:
for mat in xrange(matrices_length):
matrix_list[mat] = matrices[mat]['matrix'].tolist()
#change that for a class
results_list[mat] = {'kmer': matrices[mat]['kmer'],
'motif_count': 0,
'pos_seq_count': 0,
'motif_count_shuffled': 0,
'pos_seq_count_shuffled': 0,
'ratio': 0,
'sequence_positions': np.empty(count_seq, np.object)}
length = len(matrices[mat]['kmer'])
#wrong with imbalanced matrices
upper_adjust_list[mat] = int(ceil(length / 2.0))
lower_adjust_list[mat] = int(floor(length / 2.0))
#upper_adjust_list[mat] = 0
#lower_adjust_list[mat] = 0
#-0.1 to adjust for a division floating point bug (4.99999 !< 5, but is < 4.9!)
threshold_list[mat] = MOODS.max_score(matrix_list[mat]) - float(mut) - 0.1
#for each sequence
for i in xrange(count_seq):
item = bed_list[i]
#TODO: remove the Ns, but it might unbalance
results = MOODS.search(str(item.sequence[limit:item.total_length - limit]), matrix_list, threshold_list, q=q, bg=bg, absolute_threshold=True, both_strands=True)
results_shuffled = MOODS.search(str(item.sequence_shuffled[limit:item.total_length - limit]), matrix_list, threshold_list, q=q, bg=bg, absolute_threshold=True, both_strands=True)
results = results[0:len(matrix_list)]
results_shuffled = results_shuffled[0:len(matrix_list)]
results_length = len(results)
#for each matrix
for j in xrange(results_length):
result = results[j]
result_shuffled = results_shuffled[j]
upper_adjust = upper_adjust_list[j]
lower_adjust = lower_adjust_list[j]
result_length = len(result)
result_length_shuffled = len(result_shuffled)
if result_length > 0:
results_list[j]['pos_seq_count'] += 1
results_list[j]['sequence_positions'][i] = np.empty(result_length, np.object)
#for each motif
for k in xrange(result_length):
position = result[k][0]
strand = result[k][1]
if position >= 0:
strand = 0
adjust = upper_adjust
else:
position = -position
strand = 1
adjust = lower_adjust
results_list[j]['motif_count'] += 1
results_list[j]['sequence_positions'][i][k] = motif_hit(position + adjust + limit, strand)
if result_length_shuffled > 0:
results_list[j]['pos_seq_count_shuffled'] += 1
#for each motif
for k in xrange(result_length_shuffled):
results_list[j]['motif_count_shuffled'] += 1
#j = j + 1
#i = i + 1
for i in xrange(results_length):
result_temp = results_list[i]
result_temp['ratio'] = float(result_temp['pos_seq_count']) / float(count_seq)
return results_list
我很确定三重嵌套循环是主要的缓慢部分 - 它的工作只是重新排列来自 MOODS 的结果,C 模块执行主要工作。
最佳答案
Till Hoffmann 在此处提供了有关在 Cython 中使用 line_profiler 的有用信息:How to profile cython functions line-by-line .
我引用他的解决方案:
Robert Bradshaw 帮助我获得了用于 cdef
函数的 Robert Kern 的 line_profiler
工具,我想我应该在 stackoverflow
上分享结果。
简而言之,设置一个常规的 .pyx
文件并构建脚本和 pass to cythonize
linetrace
compiler directive启用分析和线路跟踪:
from Cython.Build import cythonize
cythonize('hello.pyx', compiler_directives={'linetrace': True})
您可能还想设置 ( undocumented ) directive binding
为 True
。
另外,您应该通过修改您的扩展
设置来定义 C 宏 CYTHON_TRACE=1
extensions = [
Extension('test', ['test.pyx'], define_macros=[('CYTHON_TRACE', '1')])
]
在 iPython
笔记本中使用 %%cython
魔法的工作示例如下: http://nbviewer.ipython.org/gist/tillahoffmann/296501acea231cbdf5e7
关于Python Line_profiler 和 Cython 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24144931/
嗨,我是 Python 新手程序员。我想要逐行分析,因此尝试安装 line_profiler 包。但它不起作用。这是我所做的: 顺便说一下,我使用的是 64 位 Windows 8.1,并且正在使用
我已经使用 pip install line_profiler 安装了 line_profiler。我在 Mac OSX 10.8.5 上使用 Python 2.7。 我运行 kernprof -l
尝试将 line_profiler 用作 API。正在关注their docs和 this tutorial (向下滚动到行分析),我得到了一个用于分析一些 numpy ufunc 的极简测试用例:
我知道这是我的安装失败,我很抱歉把这个无趣且无关紧要的问题放在你的脚下,但对于我来说,我无法弄清楚出了什么问题并且我已经没有想法了。我希望有人能够快速指出显而易见的问题。 我正在尝试分析一个 pyth
我想知道如何运行 Python 的 line_profiler以 this question 的答案中给出的格式逐行执行时间. 我安装了模块并如下调用其 LineProfiler 对象,但我得到的输出
我正在尝试在 python 中进行线路配置文件和内存配置文件。我通过导入 line_profiler从 line_profiler 导入 LineProfiler当我运行我的代码时,我收到错误消息:M
我只找到了相关的帖子,但看起来它不适用于此处。我正在遵循 Python 优化指南 Profiling and Optimizing Jupyter Notebooks - A Comprehensiv
所以我尝试使用 line_profiler 在我自己的 python 脚本中分析一个函数,因为我想要逐行计时。唯一的问题是该函数是 Cython 函数,并且 line_profiler 无法正常工作。
我打字 sudo pip install "line_profiler" 我明白了 Downloading/unpacking line-profiler Could not find a ver
基于对 this question 的回答我正在尝试使用 line_profiler具有 cythonized 功能。 关于上述问题,接受的答案为我们提供了一个关于如何将其与 jupyter note
我尝试使用 line_profiler 模块来获取 Python 文件的逐行配置文件。这是我到目前为止所做的: 1) 从 pypi 安装 line_profiler通过使用 .exe 文件(我在 Wi
我已经用 确定了一些长期运行的 pytest 测试 py.test --durations=10 我现在想用 line_profiler 或 cprofile 之类的东西来检测其中一个测试。我真的很想
使用 Ubuntu 14.04.5 LTS。尝试使用 sudo pip3 install line_profiler 安装 line_profiler,现在当我运行 sudo pip3 时,我得到以下
我正在尝试使用 line_profiler 模块分析一些 Python 代码,但我无法让它工作。我在 Windows 7 上使用 Python 2.7.6。 在以下 test.py 文件上运行 ker
我想用优秀的line_profiler ,但只是在某些时候。为了让它工作,我添加了 @profile 在每个函数调用之前,例如 @profile def myFunc(args): blah
运行 kernprof 时: kernprof -l script_to_profile.py 输出存储在二进制文件中,可以在终端/命令行中读取。 有没有办法将结果输出到文本文件? 这似乎是一个有用的
问题 大多数 iPython “魔术函数”对我来说都能很好地工作:%hist、%time、%prun 等。但是,我注意到 %lprun 在我最初安装的 iPython 中找不到。 尝试解决 然后我发现
我是一名优秀的程序员,十分优秀!