Search code examples
pythongraphvizdotpydot

How to solve "[Errno 9] Bad file descriptor" emerging from "pydotplus.graph_from_dot_file"?


I have a function setted up to model some data with a DeccisionTreeClassifier algorithm where I can adjust the max depth of the tree. This function returns the score, the confussion matrix as well as it generate a dot file with the tree which is converted to a svg. Btw, I am working in Windows.

This function works like a charm for max depth from 1 to 5, but when going max_depth=6+ it crash with a [Errno 9] Bad file descriptor.

def dtc (x_train, y_train, x_test, y_test, max_depth):
    dtc_model = tree.DecisionTreeClassifier(max_depth=max_depth)
    dtc_model.fit(x_train, y_train)
    dtc_score=dtc_model.score(x_test, y_test)
    dtc_scoret=dtc_model.score(x_train, y_train)
    y_dtc = dtc_model.predict(x_test)
    dtc_matrix= confusion_matrix(y_test,y_dtc)
    tree.export_graphviz(dtc_model,out_file=(r'tree'+str(max_depth)+'.dot'),feature_names=list_variables)
    graph = pydotplus.graph_from_dot_file(r'tree'+str(max_depth)+'.dot')  
    graph.write_svg(r'tree'+str(max_depth)+'.svg')
    return dtc_score, dtc_scoret, dtc_matrix


results1=dtc (x_train, y_train, x_test, y_test, 1)
results2=dtc (x_train, y_train, x_test, y_test, 2)
results3=dtc (x_train, y_train, x_test, y_test, 3)
results4=dtc (x_train, y_train, x_test, y_test, 4)
results5=dtc (x_train, y_train, x_test, y_test, 5)
results6=dtc (x_train, y_train, x_test, y_test, 6)
results7=dtc (x_train, y_train, x_test, y_test, 7)
results8=dtc (x_train, y_train, x_test, y_test, 8)
results9=dtc (x_train, y_train, x_test, y_test, 9)
results10=dtc (x_train, y_train, x_test, y_test, 10)

Until result5 it all works well but after result6 I got this error:

Traceback (most recent call last):

  File "<ipython-input-19-60d2876d3701>", line 1, in <module>
    results6=dtc (x_train, y_train, x_test, y_test, 6)

  File "<ipython-input-11-6cb4ba135170>", line 63, in dtc
    graph = pydotplus.graph_from_dot_file(r'tree'+str(max_depth)+'.dot')

  File "C:\XXX\librerias_anaconda\pydotplus\graphviz.py", line 314, in graph_from_dot_file
    data = fd.read()

OSError: [Errno 9] Bad file descriptor

I have read that this is a bug that sometimes happen in Windows but do not know why or how can I solve.

Sorry if anything is "bad posted", first time asking a question. Thanks in advance to anyone that can provide any help


Solution

  • We have been facing this kind of issues while using sklearn's graph_from_dot_file function. More specifically we came across with this behaviour using an Anaconda Python 3.6.5 and trying to load a dot file from a network drive.

    In our case, switching to a local (non network drive) dot location seemed to solve the issue. It may be a workaround for your problem.

    Regards,