Re: [pyqtgraph] Error when importing pyqtgraph

932 views
Skip to first unread message
Message has been deleted

Christian Gavin

unread,
Jul 12, 2012, 10:26:43 AM7/12/12
to pyqt...@googlegroups.com
Hmm, you are not using the lazy import recipe from ActiveState by any chance? If not, ignore my post.

I was trying to play around with that recipe and got this error among others. I won't recommend using the lazy import recipe since it doesn't handle sub packages and breaks introspection...

CG

On Thu, Jul 12, 2012 at 7:18 AM, Victoria Price <victoria...@gmail.com> wrote:
Hi all, I'm having a bit of trouble importing pyqtgraph.  I'm running Windows 7, Python 2.7, and PyQt 4.9.4.

First I recieved an error message that there was no module "python2_3".  Upon re-installing pyqtgraph, I got this error message:

Traceback (most recent call last):
  File "<pyshell#0>", line 1, in <module>
    import pyqtgraph
  File "C:\Python27\lib\site-packages\pyqtgraph\__init__.py", line 128, in <module>
    importAll('graphicsItems')
  File "C:\Python27\lib\site-packages\pyqtgraph\__init__.py", line 119, in importAll
    mod = __import__(path+"."+modName, globals(), locals(), fromlist=['*'])
  File "C:\Python27\lib\site-packages\pyqtgraph\graphicsItems\ArrowItem.py", line 2, in <module>
    import pyqtgraph.functions as fn
  File "C:\Python27\lib\site-packages\pyqtgraph\functions.py", line 19, in <module>
    SI_PREFIXES = asUnicode('yzafpnµm kMGTPEZY')
NameError: name 'asUnicode' is not defined

Any help would be fantastic!

-- [ You are subscribed to pyqt...@googlegroups.com. To unsubscribe, send email to pyqtgraph+...@googlegroups.com ]

Victoria Price

unread,
Jul 12, 2012, 10:46:17 AM7/12/12
to pyqt...@googlegroups.com
I'm not, unfortunately.  I'm a bit of a beginner with all of this stuff and was psyched to be pointed in the direction of this package because it's exactly what I need for image processing, so I'm not even quite sure I installed it correctly... when I downloaded it I extracted everything to my site packages folder, but it still doesn't want to work.  

Victoria Price

unread,
Jul 12, 2012, 11:11:01 AM7/12/12
to pyqt...@googlegroups.com
Behold, I got it to work!

Luke Campagnola

unread,
Jul 12, 2012, 11:14:34 AM7/12/12
to pyqt...@googlegroups.com
Excellent! Do you know what went wrong? 

Victoria Price

unread,
Jul 12, 2012, 1:21:50 PM7/12/12
to pyqt...@googlegroups.com
I downloaded the most recently updated version of pyqtgraph and that seemed to do the trick in terms of importing correctly.  However, now when I try and display something (ImageView, for example), it displays the appropriately sized window and then freezes and crashes.  I copied and pasted some of the example code into a new window and tried to run it and it did the same thing...any ideas?  

Luke Campagnola

unread,
Jul 12, 2012, 3:05:38 PM7/12/12
to pyqt...@googlegroups.com
I noticed earlier that you are using pyshell. Is this still the case? 
If so, try running the examples directly from python instead. 
If not, I'll need more details about how you are importing and using pyqtgraph.

Luke

On Thu, Jul 12, 2012 at 1:21 PM, Victoria Price <victoria...@gmail.com> wrote:
I downloaded the most recently updated version of pyqtgraph and that seemed to do the trick in terms of importing correctly.  However, now when I try and display something (ImageView, for example), it displays the appropriately sized window and then freezes and crashes.  I copied and pasted some of the example code into a new window and tried to run it and it did the same thing...any ideas?  

Victoria Price

unread,
Jul 13, 2012, 12:53:35 PM7/13/12
to pyqt...@googlegroups.com
I was successful in getting the examples to run directly from python.  I'm having a bit of trouble with inheritance issues, however... I have one .py file for reading in the file and file header info, and one .py file for using this information to create the image.  The code for processing the data to create the image is as follows:

"""

echogram.py

"""
import sys
import pyqtgraph as pg
from PyQt4.QtCore import *
from PyQt4.QtGui import *
import numpy as np
from fileheader import FileHeader, Frame

class QEchogram(QObject):
    def __init__(self):
        super(QEchogram, self).__init__()
        self.__colorTable=[]
        self.colorTable=None
        self.threshold=[50,255]
        self.painter=None
        self.image=None
        
        self._fileHeader = None

    def initFromFile(self, filename):
        self._fileHeader=FileHeader(filename)

    def processEchogram(self):
        fileHeader = self._fileHeader
        frame=Frame(Fileheader.infile)
        echoData=frame.data

        #fileName = fileName

        self.size=[echoData.shape[0],echoData.shape[1]]

        #  define the size of the data (and resulting image)
        #size = [96, 512]

        #  create a color table for our image
        #  first define the colors as RGB triplets
        colorTable =  [(255,255,255),
                       (159,159,159),
                       (95,95,95),
                       (0,0,255),
                       (0,0,127),
                       (0,191,0),
                       (0,127,0),
                       (255,255,0),
                       (255,127,0),
                       (255,0,191),
                       (255,0,0),
                       (166,83,60),
                       (120,60,40),
                       (200,200,200)]

    #  then create a color table for Qt - this encodes the color table
    #  into a list of 32bit integers (4 bytes) where each byte is the
    #  red, green, blue and alpha 8 bit values. In this case we don't
    #  set alpha so it defaults to 255 (opaque)
        ctLength = len(colorTable)
        self.__ctLength=ctLength
        __colorTable = []
        for c in colorTable:
            __colorTable.append(QColor(c[0],c[1],c[2]).rgb())




        echoData = np.round((echoData - self.threshold[0])*(float(self.__ctLength)/(self.threshold[1]-self.threshold[0])))
        echoData[echoData < 0] = 0
        echoData[echoData > self.__ctLength-1] = self.__ctLength-1
        echoData = echoData.astype(np.uint8)
        self.data=echoData

    #  create an image from our numpy data
        image = QImage(echoData.data, echoData.shape[1], echoData.shape[0], echoData.shape[1],
                   QImage.Format_Indexed8)
        image.setColorTable(__colorTable)

    #  convert to ARGB
        image = image.convertToFormat(QImage.Format_ARGB32)


    #  save the image to file
        image.save('test.png')
        self.image=QImage(self.size[0],self.size[1],QImage.Format_ARGB32)
        self.painter=QPainter(self.image)
        self.painter.drawImage(QRect(0.0,0.0,self.size[0],self.size[1]),image)
        self.painter.end()

    def getData(self):
        return self.data
    
    def getImage(self):
        return self.image

    def getPixmap(self):
        return QPixmap.fromImage(self.image)

  
When I try and use this for anything, I get a "FileHeader has no attribute infile" error.  I'm trying to keep things in separate programs for brevity's sake, so I'm trying to figure out how to create a third file for the actual widgets and displaying of images.  I'm quite new at all of this and get tangled up in inheritance stuff...any pointers?

Luke Campagnola

unread,
Jul 13, 2012, 3:17:13 PM7/13/12
to pyqt...@googlegroups.com
Hi Victoria,
This isn't really the place for general programming help; I'd like to keep the topic to pyqtgraph. 

But since you're here already:

1) Regarding the error you're getting, I'm not sure what object is supposed to have an 'infile' argument, but you have a few different spellings of fileheader that you might be mixing up: FileHeader, fileHeader, Fileheader, and self._fileHeader.

2) Regarding the rest of processEchogram, once you have echoData available, it should be as simple as calling pg.image(echoData) to display it--no need to rescale / recolor the data or worry about creating a QImage. 

The call to pg.image(...) will create and return an ImageView widget instance, which you can read about here: http://luke.campagnola.me/code/pyqtgraph/documentation/widgets/imageview.html
There are many options for controlling the scaling and coloring of the image from there; let me know if I can clarify that at all.

Luke

-- [ You are subscribed to pyqt...@googlegroups.com. To unsubscribe, send email to pyqtgraph+...@googlegroups.com ]

Victoria Price

unread,
Jul 13, 2012, 4:06:25 PM7/13/12
to pyqt...@googlegroups.com
Sorry about that, and thank you for the tips.  I got it to run!  I can't find anywhere in the documentation how to switch to an RGB color scale?

Victoria Price

unread,
Jul 13, 2012, 4:09:26 PM7/13/12
to pyqt...@googlegroups.com
Actually...is there a way to rotate the image?  I tried reshaping the data array but it gives me something very distorted. 

Luke Campagnola

unread,
Jul 13, 2012, 4:14:02 PM7/13/12
to pyqt...@googlegroups.com
numpy.rot90 should rotate your array for you.

-- [ You are subscribed to pyqt...@googlegroups.com. To unsubscribe, send email to pyqtgraph+...@googlegroups.com ]

Luke Campagnola

unread,
Jul 13, 2012, 4:18:53 PM7/13/12
to pyqt...@googlegroups.com
The grey gradient on the right side of the ImageView is editable--right click for a predefined set of gradients or left click to add new color stops. The colors can also be set up programatically; let me know if that interests you.

On Fri, Jul 13, 2012 at 4:06 PM, Victoria Price <victoria...@gmail.com> wrote:
Sorry about that, and thank you for the tips.  I got it to run!  I can't find anywhere in the documentation how to switch to an RGB color scale?

Victoria Price

unread,
Jul 16, 2012, 2:23:38 PM7/16/12
to pyqt...@googlegroups.com
Oddly enough, it doesn't seem to rotate it in the image display... 
-- [ You are subscribed to pyqt...@googlegroups.com. To unsubscribe, send email to pyqtgraph+unsubscribe@googlegroups.com ]

Luke Campagnola

unread,
Jul 16, 2012, 2:29:59 PM7/16/12
to pyqt...@googlegroups.com
I should clarify--rot90 does not rotate the array in-place; it returns a rotated array.
Reply all
Reply to author
Forward
0 new messages