I'm trying to draw some SHAP plots in Python to gain a deeper understanding of the output of my machine learning models. This is the method I'm calling in a for loop:
def plotAndSaveSHAPSummary(model,train_data,x_train,pathToSHAPPlots):
shap_values = model.get_feature_importance(train_data, type='ShapValues')
expected_value = shap_values[0,-1]
shap_values = shap_values[:,:-1]
shap.summary_plot(shap_values,x_train,max_display=20,show=False)
plt.savefig(pathToSHAPPlots+'/SHAP Plots/SHAP_Plot'+str(counter)+'.png',dpi=300,bbox_inches='tight')
plt.clf()
The plots are saved to the disk as expected but after each call of the savefig method, I get the following error message:
Exception in Tkinter callback
Traceback (most recent call last):
File "D:\PathTo\Anaconda\Lib\tkinter\__init__.py", line 1705, in __call__
return self.func(*args)
File "D:\PathTo\Anaconda\Lib\tkinter\__init__.py", line 749, in callit
func(*args)
File "D:\PathTo\Anaconda\lib\site-packages\matplotlib\backends\_backend_tk.py", line 270, in idle_draw
self.draw()
File "D:\PathTo\Anaconda\lib\site-packages\matplotlib\backends\backend_tkagg.py", line 9, in draw
super(FigureCanvasTkAgg, self).draw()
File "D:\PathTo\Anaconda\lib\site-packages\matplotlib\backends\backend_agg.py", line 393, in draw
self.figure.draw(self.renderer)
File "D:\PathTo\Anaconda\lib\site-packages\matplotlib\backend_bases.py", line 1535, in _draw
def _draw(renderer): raise Done(renderer)
matplotlib.backend_bases._get_renderer.<locals>.Done: <matplotlib.backends.backend_agg.RendererAgg object at 0x000002066B288288>
Any ideas how to get rid of this exception?
THNX! Re: Exception in Tkinter callback. It's just a minor GUI bug when the GUI tries to clear objects which don't exist. As you say, it doesn't impact training, and it doesn't seem to be consistent, so it hasn't been prioritized for fixing. Re: Exception in Tkinter callback.
Sometimes, there might be a case, while updating the variable information, we need to process some extra operations such as read, write, or undefined. Tkinter provides a way to update the variable with a callback function trace (self, mode, callback) which takes the operation of the process such as read (r), write (w), or undefined (u).
The script actually works but every now and then it throws some "silent" exceptions within the Tkinter thread that does not let the whole application crash:
On the basis of these values, the callback decides what the process needs to do in the callback function. The other two values define the variable which needs to be traced (contains widget information) and the index of the variable.
I started getting this error too after update of one of the libs, probably matplot lib. The suggestion to use plt.pause
described in the comment worked but it is internally calling plt.show()
which was unacceptable for me. I ended up adding canvas.start_event_loop
just before the savefig
import sys
def _save(self, filename: str) -> None:
fig1 = plt.gcf()
fig1.set_size_inches(6, 2.8)
plt.draw()
fig1.subplots_adjust(left=0.20)
fig1.canvas.start_event_loop(sys.float_info.min) #workaround for Exception in Tkinter callback
fig1.savefig(self.dst_dir + '/' + filename + '.png', dpi=220, bbox_inches='tight')
fig1.clf()
plt.close()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With