parrt / dtreeviz

A python library for decision tree visualization and model interpretation.
MIT License
2.97k stars 333 forks source link

Warning: No loadimage plugin for "svg:cairo" in Mac #19

Closed arundathi-shanthini closed 6 years ago

arundathi-shanthini commented 6 years ago

I have been trying to use dtreeviz to visualise iris data using my Mac.

#Iris data was loaded in an earlier part of the code   
from sklearn.datasets import *
from sklearn import tree
from dtreeviz.trees import *  

classifier = tree.DecisionTreeClassifier(max_depth=2)  # limit depth of tree
classifier.fit(iris.data, iris.target)

viz = dtreeviz(classifier, 
               iris.data, 
               iris.target,
               target_name='variety',
               feature_names=iris.feature_names, 
               class_names=["setosa", "versicolor", "virginica"]  # need class_names for classifier
              )  
viz.view()

Despite installing graphviz as:

brew install graphviz --with-librsvg --with-app --with-pango

I get the following output.

screen shot 2018-10-30 at 14 59 24

Solutions proposed in #4 did not work for me either. On downloading the zipped file in the thread and using dot -Tsvg t.dot > t.svg I could view the combined file. However, this did not work for me after deleting the contents of the folder III from the current directory.

Also, I noticed that the individual plots of only some of the nodes get stored in the location: "/private/var/folders/gs/r3f6yn8n4zj570qsj4s9rvnm0000gp/T/DTreeViz_19418" This included leaf1, leaf3, leaf4, node0, node2, legend and two other files named DTreeViz.

parrt commented 6 years ago

Hi @Aru2612 that's weird. which OS X version? Are you sure you have latest dtreeviz via pip?

arundathi-shanthini commented 6 years ago

Hi @parrt, I am using macOS High Sierra (Version: 10.13.6). I installed the package using the command pip install git+https://github.com/parrt/dtreeviz.git and the final message was Successfully installed dtreeviz-0.2.2. I am using Python 3.6.6 :: Anaconda, Inc. and graphviz 2.40.1 with librsvg 2.44.8.

parrt commented 6 years ago

hi @Aru2612 Hmm...that all sounds good. The leaf1 etc... nodes are generated into that tmp dir so that is fine. You might try uninstalling graphviz via brew then running that installation again. Are you getting any error messages that all that you can see? What are you using for the SVG viewer? There are permissions issues sometimes if you're doing it from a browser etc. I use gapplin.

arundathi-shanthini commented 6 years ago

Hi @parrt I am using gapplin as well. I think this is a problem with brew. They have reported an issue 15 hours ago here. It now gives me an error message saying build failed on using --with-app and when I exclude that it gives me:

==> Reinstalling graphviz --with-pango --with-librsvg
==> Downloading https://www.mirrorservice.org/sites/distfiles.macports.org/graphviz/graphviz-2.40.1.tar.gz
Already downloaded: /Users/arundathishanthini/Library/Caches/Homebrew/downloads/1c469743457f48b96f1f3fca3f9444a15736b2840413f31cc7e5f4a6bb5c8510--graphviz-2.40.1.tar.gz
==> ./configure --prefix=/usr/local/Cellar/graphviz/2.40.1 --disable-php --disable-swig --with-quartz --without-fr
==> make install
🍺  /usr/local/Cellar/graphviz/2.40.1: 504 files, 11.3MB, built in 2 minutes 25 seconds

I also realised that because graphviz was already installed from before using pip, my code seemed to have been using that . But now after having done pip uninstall graphviz which installs graphviz 0.10.1 and having tried reinstalling graphviz using brew reinstall graphviz --with-librsvg --with-pango(the output of which is pasted above), it gives me a ModuleNotFoundError: No module named 'graphviz'

parrt commented 6 years ago

Hmm...the pip graphviz should be just a library wrapper that calls the dot executable installed via brew. i haven't tried 0.10.1 graphviz pypi lib but 0.9 worked. No module issue should be resolved with a pip install.

arundathi-shanthini commented 6 years ago

I think the problem is that brew installed packages are not being found by Anaconda. I noticed it gets installed to a folder called Cellar. When I do conda list only graphviz 0.10.1 <pip> shows up amongst other packages. The other option is to install graphviz using conda install -c anaconda graphviz

The following packages will be downloaded:

package                    |            build
---------------------------|-----------------
pango-1.42.4               |       h060686c_0         523 KB  anaconda
graphviz-2.40.1            |       hefbbd9a_2         6.7 MB  anaconda
------------------------------------------------------------
                                       Total:         7.2 MB

The following NEW packages will be INSTALLED:

graphviz: 2.40.1-hefbbd9a_2  anaconda   

The following packages will be REMOVED:

librsvg:  2.40.19-h02b147b_0 conda-forge

The following packages will be UPDATED:

pango:    1.40.14-he752989_2 conda-forge --> 1.42.4-h060686c_0 anaconda

However, on attempting to download librsvg again, it attempts to downgrade graphviz to version 2.38.0 and with that version the output is similar to the one I got previously. brew install graphviz --with-librsvg --with-app --with-pango still gives a build error however brew install graphviz --with-librsvg --with-pango seems to be working.

parrt commented 6 years ago

Yep, Cellar is standard brew location. I think conda is wrong way to install as that tries to overwrite libs not just python stuff. pip should work. Ok, so I'll close as you have it working but thanks for figuring this out on your machine!

xih commented 5 years ago

I'm getting a similar error: CalledProcessError: Command '['dot', '-Tsvg:cairo', '-o', '/var/folders/7v/wxwmd93x6xl7shxp7ffpnc7c0000gn/T/DTreeViz_73508.svg', '/var/folders/7v/wxwmd93x6xl7shxp7ffpnc7c0000gn/T/DTreeViz_73508']' returned non-zero exit status 1. with conda and jupyter notebook.

image

I tried bothbrew install graphviz --with-librsvg --with-pango and brew install graphviz --with-librsvg --with-app --with-pango, and they both install fine but getting this error.

image

any tips on what I should try next?

xih commented 5 years ago

I tried cp -r /usr/local/Cellar/graphviz /miniconda3/envs/MY_ENV_NAME/lib/python3.6/site-packages

to link the homebrew installation of graphviz with my conda virtualenv,

however I am now getting an import error image

xih commented 5 years ago

@parrt

parrt commented 5 years ago

Try making file t.dot with content digraph T { A -> B } and then

dot -Tsvg:cairo -o t.svg t.dot

Does that get a t.svg file?

parrt commented 5 years ago

try uninstalling with brew then reinstalling.

arundathi-shanthini commented 5 years ago

@xih For me the problem was solved when I tried it outside the anaconda environment. The problem is anaconda and brew doesn't go together. Using anaconda on Windows worked for me. But on Mac when I had tried on a different laptop which didn't have anaconda installed at all it worked perfectly fine with brew install graphviz --with-librsvg --with-pango. Anaconda does not seem to recognise brew installed libraries. I think instead you could try working in a virtual python environment outside anaconda to get it to work on your Mac.

parrt commented 5 years ago

Hmm...it could be an env thing for anaconda. I never use those. just plain anaconda. Do you use those conda env things? I use nothing but anaconda so it's more likely something else. Did dot -Tsvg:cairo -o t.svg t.dot work? That's all that you need.

Either way, good to know!

arundathi-shanthini commented 5 years ago

I am using just Miniconda. So when I launch python it by default launches Python 3.6.7 |Anaconda, Inc.| (default, Oct 23 2018, 14:01:38). I had tried dot -Tsvg:cairo -o t.svg t.dot but could not get it to work for me.

parrt commented 5 years ago

Well, if dot doesn't work then there is no hope of python using it. What error do you get?

thhsfdc commented 5 years ago

I had a similar issue after a re-imaging of my machine. I installed this sequence

brew install pango librsvg
brew install cairo
brew install graphviz

and did not get it working :-(

Luckily I had a backup from before and found some missing lib in the new install. I copied the file /usr/local/Cellar/graphviz/2.40.1/lib/graphviz/libgvplugin_pango.6.dylib from backup into the very same place of my new install and than ln -s libgvplugin_pango.6.dylib libgvplugin_pango.dylib that this lib is as needed linked.

additionally I moved the backed up config6 to the new install. There were the entries for the cairo renderer like

    loadimage {         
        gd:gd 1         
        gd2:gd 1        
        gif:gd 1        
        jpeg:gd 1       
        jpe:gd 1        
        jpg:gd 1        
        png:gd 1        
        xbm:gd 1        
        gd:ps 1         
#FAILS      gd:lasi 1       
        gd2:ps 1        
#FAILS      gd2:lasi 1      
        gif:ps 1        
#FAILS      gif:lasi 1      
        jpeg:ps 1       
        jpg:ps 1        
        jpe:ps 1        
#FAILS      jpeg:lasi 1     
#FAILS      jpg:lasi 1      
#FAILS      jpe:lasi 1      
        png:ps 1        
#FAILS      png:lasi 1      
        xbm:ps 1        
#FAILS      xbm:lasi 1      
        gd:cairo 1      
        gd2:cairo 1     
        gif:cairo 1     
        jpeg:cairo 1        
        jpg:cairo 1     
        jpe:cairo 1     
        png:cairo -1        
        xbm:cairo 1     
    }               
    device {            
        gif:cairo 10        
        wbmp:cairo 5        
        jpe:cairo 5     
        jpeg:cairo 5        
        jpg:cairo 5     
        png:cairo 5     
        gd:cairo 5      
        gd2:cairo 5     
    }               
...
libgvplugin_pango.6.dylib cairo {
    render {        
        cairo 10    
    }           
    textlayout {        
        textlayout 10   
    }           
    loadimage {     
        png:cairo 1 
#FAILS      png:lasi 2  
        png:ps 2    
    }           
    device {        
        png:cairo 10    
        ps:cairo -10    
        pdf:cairo 10    
        svg:cairo -10   
    }           
}               
...
    device {        
        bmp:cairo 7 
        gif:cairo 7 
        exr:cairo 7 
        icns:cairo 7    
        ico:cairo 7 
        jpe:cairo 7 
        jpeg:cairo 7    
        jpg:cairo 7 
        jp2:cairo 7 
        pct:cairo 7 
        pict:cairo 7    
        png:cairo 7 
        psd:cairo 7 
        sgi:cairo 7 
        tif:cairo 7 
        tiff:cairo 7    
        tga:cairo 7 
    }           

and now a command line like this java -jar schemaspy-6.0.0-rc2.jar -dp force-metadata-jdbc-driver-3.0.jar:force-jdbc.jar net.sourceforge.schemaspy.Main -lq -t force -u $username -p $password -norows -o $directory -db $db -s $db -cat $db -desc $descr -connprops custom\\=true\;excludes\\=$exc_objects\;includes\\=$inc_objects\;url\\=https\://demo--uatint.cs10.my.salesforce.com/services/Soap/u/39.0 -gv /usr/local/Cellar/graphviz/2.40.1/ -hq -renderer :cairo

works perfect.

Still - indeed there is a danger that this setup can be overwritten ... so I hope some adjustements to the graphviz procedure will resolve

parrt commented 5 years ago

Wow. Did you move to 10.14 with new image? This is all so frustrating!!!

thhsfdc commented 5 years ago

@parrt I had been on 10.14.3 before and after. However, did install this kind of while ago. Not sure if even before upgrading...