Rambatino / CHAID

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

Export error #97

Closed majbahUSC closed 4 years ago

majbahUSC commented 4 years ago

Hi, while exporting the tree, I am getting the following error. Capture

Rambatino commented 4 years ago

I see.

I've not tested this on windows. Will fix now

Rambatino commented 4 years ago

Have you tried passing --export-path?

majbahUSC commented 4 years ago

Yes, but I got the same error using the path as well.

Rambatino commented 4 years ago

Can you paste the error in with the full command when you pass --export-path. Thanks!

mjpieters commented 4 years ago

You should really avoid creating paths by appending strings, the standard os.path and pathlib libraries are far more reliable and help avoid cross-platform issues. Next, don't assume that /tmp is the temporary directory, Python has a tempfile module to handle these issues.

I'd create a tempfile.TemporaryDirectory() object, so that any files in it are automatically deleted when you are done rendering:

# in .render()
if path is None:
    path = os.path.join('trees', datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") + '.gv')
with tempfile.TemporaryDirectory() as self.tempdir:
    # graphviz code, now ending in
    g.render(path, view=view)

and

# in bar_chart
file = os.path.join(self.tempdir, format(time.time(), ".20f").replace('.', '') + '.png')

and remove all references to self.files, including self.files.append(file) and the remove_tmp_files method. That moves responsibility to the standard Python library for generating paths that are compatible with Linux, Mac and Windows, and for cleaning up the temporary files you generate (they'll be gone when the with block exits).

Ranji321 commented 4 years ago

You should really avoid creating paths by appending strings, the standard os.path and pathlib libraries are far more reliable and help avoid cross-platform issues. Next, don't assume that /tmp is the temporary directory, Python has a tempfile module to handle these issues.

I'd create a tempfile.TemporaryDirectory() object, so that any files in it are automatically deleted when you are done rendering:

# in .render()
if path is None:
    path = os.path.join('trees', datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") + '.gv')
with tempfile.TemporaryDirectory() as self.tempdir:
    # graphviz code, now ending in
    g.render(path, view=view)

and

# in bar_chart
file = os.path.join(self.tempdir, format(time.time(), ".20f").replace('.', '') + '.png')

and remove all references to self.files, including self.files.append(file) and the remove_tmp_files method. That moves responsibility to the standard Python library for generating paths that are compatible with Linux, Mac and Windows, and for cleaning up the temporary files you generate (they'll be gone when the with block exits).

Hi, I did that but still it is 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