我写了一些应该处理微观数据的实验代码。我使用了pyqt4,EMAN2和visvis。由于图像文件有4096 * 4096个数组,我需要对它们进行下采样,我使用scipy.misc.imresize()
。虽然这个代码在没有pyqt环绕的情况下运行得非常好,但它在程序本身并不是这样,并引发了段错误。
这是python代码:
#!/usr/bin/python
import sys
import math
import numpy
import scipy
from PyQt4 import QtGui, QtCore
from PyQt4.QtGui import QAction,QIcon,QFileDialog,QPixmap,QHBoxLayout,\
QGraphicsScene, QGraphicsView, QGraphicsPixmapItem,\
QPen, QImage
from PyQt4.QtCore import SLOT,SIGNAL,Qt,QSize,QString, QByteArray
from EMAN2 import EMData, EMNumPy
from numpy import mean
from scipy.misc import imresize
from PyQt4 import QtGui, QtCore
import visvis as vv
app = vv.use('pyqt4')
class SpringMain(QtGui.QMainWindow):
thickness = 20
def __init__(self):
QtGui.QMainWindow.__init__(self)
self.setWindowTitle('Spring')
self.data = None
self.view = None
########
# VisVis
########
Figure = app.GetFigureClass()
self.fig = Figure(self)
self.setCentralWidget(self.fig._widget)
self.setGeometry(300,300,300,300)
self.show()
######
# Menu
######
load = QAction(QIcon('icons/load.png'), 'Load', self)
load.setShortcut('Ctrl+L')
load.setStatusTip('Load Tiff')
self.connect(load,SIGNAL('triggered()'),self.onLoad)
algo = QAction(QIcon('icons/load.png'), 'Algorithm', self)
algo.setShortcut('Ctrl+A')
algo.setStatusTip('Apply Algorithm')
self.connect(algo,SIGNAL('triggered()'),self.onAlgo)
exit = QtGui.QAction(QtGui.QIcon('icons/exit.png'), 'Exit', self)
exit.setShortcut('Ctrl+Q')
exit.setStatusTip('Exit application')
self.connect(exit,QtCore.SIGNAL('triggered()'), QtCore.SLOT('close()'))
self.statusBar()
menubar = self.menuBar()
_file = menubar.addMenu('&File')
_file.addAction(exit)
_file.addAction(load)
_file.addAction(algo)
def onLoad(self):
fileName = QFileDialog.getOpenFileName(self, "Open Image", "/home/kai","EMData Files (*.mrc)")
self.data = EMNumPy.em2numpy(EMData(str(fileName)))
self.view = imresize(self.data,0.125)
#vv.imshow(data)
def onAlgo(self):
p1 = QPen(Qt.red,2,Qt.DashDotLine)
self.paintMarker([50,50],[200,200],p1)
p2 = QPen(Qt.green,1,Qt.SolidLine)
self.paintMarker([150,50],[400,100],p2)
p3 = QPen(Qt.blue,1,Qt.DashLine)
def paintMarker(self,start,stop,pen=None):
x0,y0 = start
x1,y1 = stop
v = [x1-x0,y1-y0]
norm= float(self.thickness)/math.sqrt(v[0]**2+v[1]**2)
vOrth = [v[1],-v[0]]
if pen is None:
pen = QPen(Qt.green,2,Qt.SolidLine)
self.scene.addLine(x0-norm*vOrth[0],y0-norm*vOrth[1],\
x0+norm*vOrth[0],y0+norm*vOrth[1],pen=pen)
self.scene.addLine(x0-norm*vOrth[0]+v[0],y0-norm*vOrth[1]+v[1],\
x0+norm*vOrth[0]+v[0],y0+norm*vOrth[1]+v[1],\
pen=pen)
self.scene.addLine(x0,y0,x1,y1,pen=pen)
self.scene.addLine(x0+norm*vOrth[0],y0+norm*vOrth[1],\
x0+norm*vOrth[0]+v[0],y0+norm*vOrth[1]+v[1],\
pen=pen)
self.scene.addLine(x0-norm*vOrth[0],y0-norm*vOrth[1],\
x0-norm*vOrth[0]+v[0],y0-norm*vOrth[1]+v[1]\
,pen=pen)
if True:
qtApp = QtGui.QApplication([''])
m = SpringMain()
qtApp.exec_()
else:
em = EMData("IntermediateImage_20131016_200656.mrc")
np = EMNumPy.em2numpy(em)
im=imresize(np,0.125)
print im.shape
不多,正如你所看到的那样。我设法使用gdb提取堆栈跟踪,但无法真正解释它:
#0 _aligned_strided_to_strided_size4_srcstride0 (dst=0xa6dfd0 "\030",
dst_stride=0, src=0x7fffa7fff010 <Address 0x7fffa7fff010 out of bounds>,
__NPY_UNUSED_TAGGEDsrc_stride=0, N=1, __NPY_UNUSED_TAGGEDsrc_itemsize=4,
__NPY_UNUSED_TAGGEDdata=0x0)
at numpy/core/src/multiarray/lowlevel_strided_loops.c.src:217
#1 0x00007ffff5c85ef5 in raw_array_assign_array (ndim=1, shape=0x2a8cb70,
dst_dtype=<optimized out>, dst_data=0xa6dfd0 "\030", dst_strides=
0x2a8cb80, src_dtype=<optimized out>, src_data=
0x7fffa7fff010 <Address 0x7fffa7fff010 out of bounds>, src_strides=
0x7fffffffb770) at numpy/core/src/multiarray/array_assign_array.c:96
#2 0x00007ffff5c86859 in PyArray_AssignArray (dst=0x1f4d990, src=0x1f4dc60,
wheremask=0x0, casting=<optimized out>)
at numpy/core/src/multiarray/array_assign_array.c:351
#3 0x00007ffff57d06d4 in PyArray_InitializeReduceResult (result=0x1f4d990,
operand=0x1f4dc10, axis_flags=0x7fffffffc090 "\001\001\372\001",
reorderable=<optimized out>, out_skip_first_count=0x7fffffffbba8, funcname=
0x7ffff580119b "minimum") at numpy/core/src/umath/reduction.c:338
#4 0x00007ffff57d0c0c in PyUFunc_ReduceWrapper (operand=0x1f4dc10, out=0x0,
wheremask=<optimized out>, operand_dtype=0x7ffff5f94f00, result_dtype=
0x7ffff5f94f00, casting=NPY_UNSAFE_CASTING, axis_flags=
0x7fffffffc090 "\001\001\372\001", reorderable=1, keepdims=0, subok=0,
assign_identity=0, loop=0x7ffff57f2450 <reduce_loop>, data=0xa44080,
buffersize=8192, funcname=0x7ffff580119b "minimum")
---Type <return> to continue, or q <return> to quit---
at numpy/core/src/umath/reduction.c:480
#5 0x00007ffff57f39b7 in PyUFunc_Reduce (keepdims=0, odtype=0x7ffff5f94f00,
axes=0x7fffffffbf30, naxes=<optimized out>, out=0x0, arr=0x1f4dc10, ufunc=
0xa44080) at numpy/core/src/umath/ufunc_object.c:2884
#6 PyUFunc_GenericReduction (ufunc=<optimized out>, args=<optimized out>,
kwds=<optimized out>, operation=<optimized out>)
at numpy/core/src/umath/ufunc_object.c:3854
#7 0x000000000056d4a4 in PyEval_EvalFrameEx ()
#8 0x00000000005747c0 in PyEval_EvalCodeEx ()
#9 0x00000000005697b0 in function_call ()
#10 0x000000000043a8b6 in PyObject_Call ()
#11 0x00007ffff5cf6ef3 in forward_ndarray_method (self=0x1f4dc10,
args=<optimized out>, kwds=0x0, forwarding_callable=
<function at remote 0x7ffff7e48578>)
at numpy/core/src/multiarray/methods.c:101
#12 0x000000000056d4a4 in PyEval_EvalFrameEx ()
#13 0x00000000005747c0 in PyEval_EvalCodeEx ()
#14 0x000000000056da48 in PyEval_EvalFrameEx ()
#15 0x00000000005747c0 in PyEval_EvalCodeEx ()
#16 0x000000000056da48 in PyEval_EvalFrameEx ()
#17 0x00000000005747c0 in PyEval_EvalCodeEx ()
#18 0x000000000056da48 in PyEval_EvalFrameEx ()
#19 0x00000000005747c0 in PyEval_EvalCodeEx ()
---Type <return> to continue, or q <return> to quit---
#20 0x00000000005697b0 in function_call ()
#21 0x000000000043a8b6 in PyObject_Call ()
#22 0x00000000004fd6ea in instancemethod_call.8548 ()
#23 0x000000000043a8b6 in PyObject_Call ()
#24 0x000000000043b626 in PyEval_CallObjectWithKeywords ()
#25 0x00007fffedc28f80 in sip_api_invoke_slot (slot=<optimized out>, sigargs=
()) at /build/buildd/sip4-4.13.2/siplib/qtlib.c:203
#26 0x00007fffed94e396 in PyQtProxy::invokeSlot(qpycore_slot const&, void**) ()
from /usr/lib/python2.7/dist-packages/PyQt4/QtCore.so
#27 0x00007fffed94e690 in PyQtProxy::unislot(void**) ()
from /usr/lib/python2.7/dist-packages/PyQt4/QtCore.so
#28 0x00007fffed94e772 in PyQtProxy::qt_metacall(QMetaObject::Call, int, void**) () from /usr/lib/python2.7/dist-packages/PyQt4/QtCore.so
#29 0x00007ffff11a3489 in QMetaObject::activate(QObject*, QMetaObject const*, int, void**) () from /usr/lib/x86_64-linux-gnu/libQtCore.so.4
#30 0x00007ffff16ab132 in QAction::triggered(bool) ()
from /usr/lib/x86_64-linux-gnu/libQtGui.so.4
#31 0x00007ffff16ab31f in QAction::activate(QAction::ActionEvent) ()
from /usr/lib/x86_64-linux-gnu/libQtGui.so.4
#32 0x00007ffff11a8446 in QObject::event(QEvent*) ()
from /usr/lib/x86_64-linux-gnu/libQtCore.so.4
#33 0x00007ffff16ab3f0 in QAction::event(QEvent*) ()
from /usr/lib/x86_64-linux-gnu/libQtGui.so.4
---Type <return> to continue, or q <return> to quit---
#34 0x00007ffff2667a23 in event (a0=0x20c2a20, this=0x10a77b0)
at sipQtGuipart9.cpp:39803
#35 sipQAction::event (this=0x10a77b0, a0=0x20c2a20) at sipQtGuipart9.cpp:39795
#36 0x00007ffff16b1894 in QApplicationPrivate::notify_helper(QObject*, QEvent*)
() from /usr/lib/x86_64-linux-gnu/libQtGui.so.4
#37 0x00007ffff16b6713 in QApplication::notify(QObject*, QEvent*) ()
from /usr/lib/x86_64-linux-gnu/libQtGui.so.4
#38 0x00007ffff2667a99 in notify (a1=0x20c2a20, a0=0x10a77b0, this=0x1dd7d40)
at sipQtGuipart9.cpp:35916
#39 sipQApplication::notify (this=0x1dd7d40, a0=0x10a77b0, a1=0x20c2a20)
at sipQtGuipart9.cpp:35908
#40 0x00007ffff118ee9c in QCoreApplication::notifyInternal(QObject*, QEvent*)
() from /usr/lib/x86_64-linux-gnu/libQtCore.so.4
#41 0x00007ffff1192c6a in QCoreApplicationPrivate::sendPostedEvents(QObject*, int, QThreadData*) () from /usr/lib/x86_64-linux-gnu/libQtCore.so.4
#42 0x00007ffff11bdf93 in ?? () from /usr/lib/x86_64-linux-gnu/libQtCore.so.4
#43 0x00007ffff0618d13 in g_main_context_dispatch ()
from /lib/x86_64-linux-gnu/libglib-2.0.so.0
#44 0x00007ffff0619060 in ?? () from /lib/x86_64-linux-gnu/libglib-2.0.so.0
#45 0x00007ffff0619124 in g_main_context_iteration ()
from /lib/x86_64-linux-gnu/libglib-2.0.so.0
#46 0x00007ffff11be3bf in QEventDispatcherGlib::processEvents(QFlags<QEventLoop::ProcessEventsFlag>) () from /usr/lib/x86_64-linux-gnu/libQtCore.so.4
---Type <return> to continue, or q <return> to quit---
#47 0x00007ffff1759d5e in ?? () from /usr/lib/x86_64-linux-gnu/libQtGui.so.4
#48 0x00007ffff118dc82 in QEventLoop::processEvents(QFlags<QEventLoop::ProcessEventsFlag>) () from /usr/lib/x86_64-linux-gnu/libQtCore.so.4
#49 0x00007ffff118ded7 in QEventLoop::exec(QFlags<QEventLoop::ProcessEventsFlag>) () from /usr/lib/x86_64-linux-gnu/libQtCore.so.4
#50 0x00007ffff1192f67 in QCoreApplication::exec() ()
from /usr/lib/x86_64-linux-gnu/libQtCore.so.4
#51 0x00007ffff2633c5e in meth_QApplication_exec_ (sipArgs=<optimized out>)
at sipQtGuipart9.cpp:37856
#52 0x000000000056d4a4 in PyEval_EvalFrameEx ()
#53 0x00000000005747c0 in PyEval_EvalCodeEx ()
#54 0x0000000000569ee1 in PyRun_FileExFlags ()
#55 0x000000000056a6b3 in PyRun_SimpleFileExFlags ()
#56 0x000000000056ba75 in Py_Main ()
#57 0x00007ffff68cd76d in __libc_start_main (main=0x41bae0 <main>, argc=2,
ubp_av=0x7fffffffe138, init=<optimized out>, fini=<optimized out>,
rtld_fini=<optimized out>, stack_end=0x7fffffffe128) at libc-start.c:226
#58 0x000000000041bb11 in _start ()
你们这些先生们或女士们都能理解这一点吗?
谢谢!
启