Changeset 7946:b24b60963453 in orange


Ignore:
Timestamp:
05/27/11 12:08:38 (3 years ago)
Author:
ales_erjavec <ales.erjavec@…>
Branch:
default
Convert:
5c3154152a02866e068a0c0cca4ccdd3b0a64597
Message:

Using temp files for graphviz dot output (due to EINTR errors when piping output).

File:
1 edited

Legend:

Unmodified
Added
Removed
  • orange/OrangeWidgets/Unsupervised/OWInteractionGraph.py

    r7050 r7946  
    262262 
    263263        import tempfile, os 
    264         fhandle, fname = tempfile.mkstemp('dot') 
     264        fhandle, fname = tempfile.mkstemp('.dot') 
    265265        os.close(fhandle) 
    266266        f = file(fname, "wt") 
    267267        self.interactionMatrix.exportGraph(f, significant_digits=3,positive_int=8,negative_int=8,absolute_int=0,url=1) 
     268        f.flush() 
    268269        f.close() 
    269270        del f 
    270271 
    271         # execute dot 
    272272        import subprocess 
     273        def run_dot(dotfile, format): 
     274            # execute dot command to retrieve an image in 'format' 
     275            # and an ismap formated dot output. 
     276            # Using temp files instead of direct pipe output due to EINTR 
     277            # errors 
     278            image_handle, image_name = tempfile.mkstemp('.' + format) 
     279            os.close(image_handle) 
     280             
     281            ismap_handle, ismap_name = tempfile.mkstemp('.ismap') 
     282            os.close(ismap_handle) 
     283             
     284#            print image_name 
     285#            subprocess.check_call(" ".join(["dot", fname, "-T" + format, "-o" + image_name]), shell=True) 
     286#            subprocess.check_call(" ".join(["dot", fname, "-Tismap", "-o" + ismap_name]), shell=True) 
     287            subprocess.check_call(["dot", fname, "-T" + format, "-o" + image_name]) 
     288            subprocess.check_call(["dot", fname, "-Tismap", "-o" + ismap_name]) 
     289 
     290             
     291            image_data = open(image_name, "rb").read() 
     292            ismap_data = open(ismap_name, "rb").read() 
     293             
     294            os.remove(image_name) 
     295            os.remove(ismap_name) 
     296             
     297            return image_data, ismap_data 
     298 
     299        self.error([0, 1]) 
     300        ismap = "" 
     301        image = "" 
     302        pixmap = QPixmap() 
    273303        try: 
    274             textPng = subprocess.Popen(["dot", fname, "-Tpng"], stdout=subprocess.PIPE).communicate()[0] 
    275             textPlainList = subprocess.Popen(["dot", fname, "-Tismap"], stdout=subprocess.PIPE).communicate()[0].splitlines() 
     304            # testing different formats, sometimes the png output from 'dot' 
     305            # fails to load in Qt (no idea why). 
     306            for format in ["png", "gif", "jpeg", "svg"]:  
     307                image, ismap = run_dot(fname, format) 
     308                qimage = QImage.fromData(QByteArray(image), format) 
     309                pixmap = QPixmap.fromImage(qimage) 
     310                if not pixmap.isNull(): 
     311                    break 
    276312        except OSError, ex: 
    277             textPng = "" 
    278             textPlainList = [] 
     313            self.error(0, "This widget needs 'graphviz' software package. You can freely download it from the web.") 
     314        except subprocess.CalledProcessError, ex: 
     315            self.error(0, "This widget needs 'graphviz' software package. You can freely download it from the web.") 
     316             
     317        if image and pixmap.isNull(): 
     318            self.error(1, "Could not load image from 'dot' output.") 
     319         
     320        textPlainList = ismap.splitlines() 
    279321         
    280322        os.remove(fname) 
    281  
    282         # if the output from the pipe was empty, then the software isn't installed correctly 
    283         if len(textPng) == 0: 
    284             self.error(0, "This widget needs 'graphviz' software package. You can freely download it from the web.") 
    285  
    286         # create a picture 
    287         pixmap = QPixmap() 
    288         pixmap.loadFromData(textPng) 
     323         
    289324        if hasattr(self, "canvasPixmap"): 
    290325            self.canvasPixmap.setPixmap(pixmap) 
Note: See TracChangeset for help on using the changeset viewer.