Comments (18)
OK so I figured it out.
The problem is graphviz is dumb and can't handle a WindowsPath object. So instead when sending the file name to graphviz i needed to add ".as_posix()" to the path.parent line in dotfilename = g.save(directory=path.parent.as_posix(), filename=path.stem) as shown below.
def save(self, filename):
"""
Save the svg of this tree visualization into filename argument.
Mac platform can save any file type (.pdf, .png, .svg). Other platforms
would fail with errors. See https://github.com/parrt/dtreeviz/issues/4
"""
path = Path(filename)
if not path.parent.exists:
makedirs(path.parent)
g = graphviz.Source(self.dot, format='svg')
dotfilename = g.save(directory=path.parent.as_posix(), filename=path.stem)
from dtreeviz.
one thing I noticed in the source code:
if I change the view function to print the tmp and svgfilename variables like so:
def view(self):
tmp = tempfile.gettempdir()
print(tmp)
svgfilename = f"{tmp}/DTreeViz_{getpid()}.svg"
print(svgfilename)
self.save(svgfilename)
view(svgfilename)
I get these paths:
C:\Users\Will\AppData\Local\Temp
C:\Users\Will\AppData\Local\Temp/DTreeViz_6232.svg
I would assume the forward slash before DTreeViz_6232.svg would cause issues no?
I checked this by changing the line:
svgfilename = f"{tmp}/DTreeViz_{getpid()}.svg"
to:
svgfilename = f"{tmp}\DTreeViz_{getpid()}.svg"
but i get the same error so maybe i'm wrong.
from dtreeviz.
which windows btw?
from dtreeviz.
Windows 10 Pro Version 1803 OS Build 17134.285
from dtreeviz.
I am new to github so I am not sure if I am able to comment on closed issues but I'm working through the dtreeviz demo in R using the reticulate package and seem to be running into a similar problem.
My OS: Windows 10 Home Version 1803 OS Build 17134.407.
To install Graphviz, I ran pip install dtreeviz
in the Anaconda Prompt, downloaded graphviz-2.38.msi, updated my Path environment variable and rebooted.
Running (base) C:\\Users\\Merchants>where dot
in the Anaconda Prompt returns
C:\\Program Files (x86)\\Graphviz2.38\\bin\\dot.exe
as expected. And running dot -V
returns dot - graphviz version 2.38.0 (20140413.2041)
.
However, when I run the classifcation example:
import os
os.environ['QT_QPA_PLATFORM_PLUGIN_PATH'] = 'C:\\Users\\Merchants\\Anaconda3\\Library\\plugins\\platforms'
from sklearn.datasets import *
from sklearn import tree
from dtreeviz.trees import *
classifier = tree.DecisionTreeClassifier(max_depth=2) # limit depth of tree
iris = load_iris()
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()
I get this error:
Error in py_run_string_impl(code, local, convert) :
OSError: [WinError 6] The handle is invalid
Detailed traceback:
File "<string>", line 1, in <module>
File "C:\Users\MERCHA~1\ANACON~1\lib\site-packages\dtreeviz\trees.py", line 68, in view
self.save(svgfilename)
File "C:\Users\MERCHA~1\ANACON~1\lib\site-packages\dtreeviz\trees.py", line 100, in save
stdout, stderr = run(cmd, capture_output=True, check=True, quiet=False)
File "C:\Users\MERCHA~1\ANACON~1\lib\site-packages\graphviz\backend.py", line 147, in run
proc = subprocess.Popen(cmd, startupinfo=get_startupinfo(), **kwargs)
File "C:\Users\MERCHA~1\ANACON~1\lib\subprocess.py", line 709, in __init__
errread, errwrite) = self._get_handles(stdin, stdout, stderr)
File "C:\Users\MERCHA~1\ANACON~1\lib\subprocess.py", line 1006, in _get_handles
p2cread = _winapi.GetStdHandle(_winapi.STD_INPUT_HANDLE)
The traceback is different from the one posted by @Whamp.
When I run:
import os
import subprocess
proc = subprocess.Popen(['dot','-V'])
print( os.getenv('Path') )
the output contains the path: C:\Program Files (x86)\Graphviz2.38\bin
.
However, when I run:
import graphviz.backend as be
cmd = ["dot", "-V"]
stdout, stderr = be.run(cmd, capture_output=True, check=True, quiet=False)
print( stderr )
I get this error:
OSError: [WinError 6] The handle is invalid
Detailed traceback:
File "<string>", line 1, in <module>
File "C:\Users\MERCHA~1\ANACON~1\lib\site-packages\graphviz\backend.py", line 147, in run
proc = subprocess.Popen(cmd, startupinfo=get_startupinfo(), **kwargs)
File "C:\Users\MERCHA~1\ANACON~1\lib\subprocess.py", line 709, in __init__
errread, errwrite) = self._get_handles(stdin, stdout, stderr)
File "C:\Users\MERCHA~1\ANACON~1\lib\subprocess.py", line 1006, in _get_handles
p2cread = _winapi.GetStdHandle(_winapi.STD_INPUT_HANDLE)
I'm relatively new to Python so I'm not sure what these errors are telling me or how to go about fixing them. Where am I going wrong? Any help would be greatly appreciated.
from dtreeviz.
Hmm...I've no idea I'm afraid. Searching for OSError: [WinError 6] The handle is invalid
finds a whole bunch of stuff related to Windows/Python/Video editing. I don't use Windows regularly and have no idea how to troubleshoot that. :(
from dtreeviz.
No problem, thank you for following up so quickly.
from dtreeviz.
I'm having the same problem. Could you please tell me in which file you made this change? where is dotfilename?
from dtreeviz.
@saminsemsar whats your code look like?
from dtreeviz.
here's the code:
!pip install --quiet dtreeviz
from dtreeviz.trees import dtreeviz # remember to load the package
print("package imported")
viz = dtreeviz(clf, X, y,
target_name="target",
feature_names=feature_names,
class_names=list('fruit_label'))
viz
when I install dtreeviz, It automatically installs graphviz 0.19.1.
even after adding my graphviz path to system path. I'm still getting this error:
FileNotFoundError Traceback (most recent call last)
c:\users\dearuser\appdata\local\programs\python\python38\lib\site-packages\graphviz\backend\execute.py in run_check(cmd, input_lines, encoding, capture_output, quiet, **kwargs)
84 else:
---> 85 proc = subprocess.run(cmd, **kwargs)
86 except OSError as e:
c:\users\dearuser\appdata\local\programs\python\python38\lib\subprocess.py in run(input, capture_output, timeout, check, *popenargs, **kwargs)
492
--> 493 with Popen(*popenargs, **kwargs) as process:
494 try:
c:\users\dearuser\appdata\local\programs\python\python38\lib\subprocess.py in init(self, args, bufsize, executable, stdin, stdout, stderr, preexec_fn, close_fds, shell, cwd, env, universal_newlines, startupinfo, creationflags, restore_signals, start_new_session, pass_fds, encoding, errors, text)
857
--> 858 self._execute_child(args, executable, preexec_fn, close_fds,
859 pass_fds, cwd, env,
c:\users\dearuser\appdata\local\programs\python\python38\lib\subprocess.py in _execute_child(self, args, executable, preexec_fn, close_fds, pass_fds, cwd, env, startupinfo, creationflags, shell, p2cread, p2cwrite, c2pread, c2pwrite, errread, errwrite, unused_restore_signals, unused_start_new_session)
1310 try:
-> 1311 hp, ht, pid, tid = _winapi.CreateProcess(executable, args,
1312 # no special security
FileNotFoundError: [WinError 2] The system cannot find the file specified
The above exception was the direct cause of the following exception:
ExecutableNotFound Traceback (most recent call last)
c:\users\dearuser\appdata\local\programs\python\python38\lib\site-packages\IPython\core\formatters.py in call(self, obj)
343 method = get_real_method(obj, self.print_method)
344 if method is not None:
--> 345 return method()
346 return None
347 else:
c:\users\dearuser\appdata\local\programs\python\python38\lib\site-packages\dtreeviz\trees.py in repr_svg(self)
35
36 def repr_svg(self):
---> 37 return self.svg()
38
39 def svg(self):
c:\users\dearuser\appdata\local\programs\python\python38\lib\site-packages\dtreeviz\trees.py in svg(self)
39 def svg(self):
40 """Render tree as svg and return svg text."""
---> 41 svgfilename = self.save_svg()
42 with open(svgfilename, encoding='UTF-8') as f:
43 svg = f.read()
c:\users\dearuser\appdata\local\programs\python\python38\lib\site-packages\dtreeviz\trees.py in save_svg(self)
52 tmp = tempfile.gettempdir()
53 svgfilename = os.path.join(tmp, f"DTreeViz_{os.getpid()}.svg")
---> 54 self.save(svgfilename)
55 return svgfilename
56
c:\users\dearuser\appdata\local\programs\python\python38\lib\site-packages\dtreeviz\trees.py in save(self, filename)
80 graphviz.backend.run(cmd, capture_output=True, check=True, quiet=False)
81 else:
---> 82 graphviz.backend.execute.run_check(cmd, capture_output=True, check=True, quiet=False)
83
84 if filename.endswith(".svg"):
c:\users\dearuser\appdata\local\programs\python\python38\lib\site-packages\graphviz\backend\execute.py in run_check(cmd, input_lines, encoding, capture_output, quiet, **kwargs)
86 except OSError as e:
87 if e.errno == errno.ENOENT:
---> 88 raise ExecutableNotFound(cmd) from e
89 raise
90
ExecutableNotFound: failed to execute 'dot', make sure the Graphviz executables are on your systems' PATH
I don't know what to do. My python version is 3.8. I'm runing the code in jupyter notebook.
from dtreeviz.
Did you install graphviz is installed? Can you run dot
from command line?
from dtreeviz.
No, In command line dot is not recognized. not even when I cd to where the graphviz directory is located.
from dtreeviz.
When I download and install graphviz from here https://graphviz.org/download/ and unistall the graphviz that dtreeviz has installed, in this one there's a bin folder and a dot.exe file which the other one didn't have. but still, after I add it to system path, It is still not recognized from cmd. This time though, in cmd, when I cd to where graphviz/bin is located dot is recognized.
from dtreeviz.
from dtreeviz.
Thank you for trying to help me. I'll try.
from dtreeviz.
hey @saminsemsar , are you using anaconda to manage your python libraries? really helps.
I may be misremembering but I didn't download graphviz from their website. Instead, I got dtreeviz working by installing in a new conda environment:
conda install -c conda-forge python-graphviz dtreeviz
Sorry @parrt , I ignored the pip instructions on the dtreeviz install page 😛
from dtreeviz.
No, I'm not using anaconda but maybe I should start using it as you said. I just found out that dot.py does get run from cmd, what is not recognized is dot -v.
from dtreeviz.
from dtreeviz.
Related Issues (20)
- Add support for TensorFlow GradientBoostedTreesModel model
- _regr_leaf_viz calculates the mean for prediction value.
- WARNING:matplotlib.font_manager:findfont: Font family 'Arial' not found. HOT 2
- Decision Tree visualize wrong path HOT 1
- When using dataset that is different from the training for trees models - does not draw HOT 1
- Support for RandomForest HOT 5
- Visualize custom decision tree HOT 1
- how to use dtreeviz in streamlit HOT 2
- VisualisationNotYetSupportedError: get_min_samples_leaf() is not implemented yet for XGBoost. HOT 4
- TypeError: list indices must be integers or slices, not numpy.float64 HOT 5
- Crash when leaf nodes have no samples HOT 1
- Out of memory when calling viz.view() HOT 2
- Integrate AI explanation
- CatBoost need to be supported. HOT 1
- AttributeError: module 'dtreeviz' has no attribute 'model' on Windows platform, works fine on Google colab
- tfdf.keras.CartModel support? HOT 1
- TypeError: 'int' object is not subscriptable HOT 3
- Development requirement in `setup.py` HOT 1
- Adjust Bounding Box of saved svg file
- DTreeViz crashes, if descision tree was built with objects, that are interpretable as numbers.
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from dtreeviz.