Rambatino / CHAID

A python implementation of the common CHAID algorithm
Apache License 2.0
149 stars 50 forks source link

Issue in running the "tree.render(path=None, view=False)" #99

Closed Ranji321 closed 4 years ago

Ranji321 commented 4 years ago

Running the following code tree.render(path=None, view=False)

is through this error.

File "C:\Users\ps\AppData\Local\Continuum\anaconda3\lib\site-packages\graphviz\files.py", line 166, in save with io.open(filepath, 'w', encoding=self.encoding) as fd:

OSError: [Errno 22] Invalid argument: 'trees/2020-01-07 16:00:08.gv'

any solution on this please..?

mjpieters commented 4 years ago

The library tries to generate the GraphViz definition file in a directory relative to the current working directory. If you don't have a trees subdirectory in your current working directory, or you can't write to that directory, you'd get errors.

Set path to a location to write that file to where the directory does exist, instead.

Ranji321 commented 4 years ago

The library tries to generate the GraphViz definition file in a directory relative to the current working directory. If you don't have a trees subdirectory in your current working directory, or you can't write to that directory, you'd get errors.

Set path to a location to write that file to where the directory does exist, instead.

Thank you for the quick reply. I did the following

from CHAID import Tree import pandas as pd import numpy as np import os

os.chdir('C:\Users\data\AppData\Local\Continuum\anaconda3\Lib\site-packages\graphviz')

df=pd.read_csv('C:\Users\ps\chaid_pro.csv')

independent_variable_columns =['gende','no_renewal','accident_no', 'complaint_count', 'CC_counts']

dep_variable = 'switch' tree = Tree.from_pandas_df(df, dict(zip(independent_variable_columns, ['nominal']*38)), dep_variable, max_depth=2)

tree.to_tree() tree.render()

but still i m finding the following error

**File "C:\Users\data\AppData\Local\Continuum\anaconda3\lib\site-packages\graphviz\files.py", line 166, in save with io.open(filepath, 'w', encoding=self.encoding) as fd:

OSError: [Errno 22] Invalid argument: 'trees/2020-01-07 19:23:19.gv'**

mjpieters commented 4 years ago

No, you seem to have misunderstood me.

So

tree = Tree.from_pandas_df(df, dict(zip(independent_variable_columns, ['nominal']*38)), dep_variable, max_depth=2)

tree.to_tree()
tree.render(path=r"C:\Users\ps\chaid_pro_tree.gv")
Ranji321 commented 4 years ago

No, you seem to have misunderstood me.

  • Don't use os.chdir() to change the working directory to the folder where you have a related library installed. You don't want to write data to installed Python libraries. You are now trying to write to C:\Users\data\AppData\Local\Continuum\anaconda3\Lib\site-packages\graphviz\trees. Don't do that, there is no trees directory there nor should there be.
  • Don't use os.chdir() at all, just add a path argument to tree.render()

So

tree = Tree.from_pandas_df(df, dict(zip(independent_variable_columns, ['nominal']*38)), dep_variable, max_depth=2)

tree.to_tree()
tree.render(path=r"C:\Users\ps\chaid_pro_tree.gv")

I tried that it is showing the error as FileNotFoundError: [WinError 3] The system cannot find the path specified: '/tmp/157841490190849018096923828125.png'

mjpieters commented 4 years ago

That's issue #97, I have issued a pull request (#100) that attempts to fix those issues.

Ranji321 commented 4 years ago

That's issue #97, I have issued a pull request (#100) that attempts to fix those issues.

Hi, I followed pull request but still I m getting as PermissionError: [WinError 32] The process cannot access the file because it is being used by another process: 'C:\Users\PS\AppData\Local\Temp\tmpus2ps0tk\tmp_eub1j8w.png'

I deleted temp files and restarted but still the same. Please help me to fix this. Thank you

mjpieters commented 4 years ago

Since you are now talking about my pull request #100 for issue #97, it'd be helpful to leave your comment on one of those two places. Also, for Python exceptions the full traceback is always going to be helpful, because that tells us not only the exception, but also where it was thrown and how Python got there.

In this case, I can see what goes wrong, I didn't account for Windows' temp file handling, it locks down the permissions more than we'd want for this case (I think it's the graphviz dot command that complains that it can't read the images named in the graph spec).

Using mkstemp was overkill for generating unique image filenames anyway, I've simplified it and the files should no longer be locked down. Please try again.

Rambatino commented 4 years ago

@mjpieters thanks for looking into this. I've merged those other PRs, although I think moving forward we're not going to actively support python 2.x.

@Ranji321 I'm going to close this issue, I've released CHAID 5.3.0, so may you please install it via pip install CHAID==5.30 (must be using python3, as there isn't a py2 wheel for it).

If there are still issues can you open a new issue with the stack trace and the version details of all relevant libraries.

Thanks!