For an analysis application I'm trying to:
- Get a list of filenames from the user using PyQt4 QFileDialog
- Extract some data from each file
- Plot the aggregated data using pyplot
However, getting the filenames this way causes pyplot.show() to take a really, really long time. I've distilled the problem down to the following test case:
import matplotlib.pyplot as plt
from PyQt4 import QtCore, QtGui
app = QtGui.QApplication(None)
fd = QtGui.QFileDialog(None)
filenames = fd.getOpenFileNames(caption='Select Data Files')
plt.plot([1,2,3,4,5])
plt.show()
Note that I'm not even doing anything with the filenames here--just getting them.
Selecting a single file from the dialog results in a plot time of 10 seconds. Interestingly, the time required for show() to complete goes up roughly linearly with the number of files selected. For 10 files it takes about 67 seconds for the plot to show.
This isn't a big deal for plotting data from a handful of files, but when aggregating data from Monte Carlo simulations where there are thousands of files it can take literally hours for the plot to show. Telling matplotlib to use the Qt4Agg backend results in the same behavior.
If I comment out the call to getOpenFileNames the script will complete in under a second.
I'm running the following versions of the relevant packages (should be latest):
- Python 2.7
- matplotlib 1.3.1
- python-sip 4.13.2-1
- python-qt4 4.9.1-2ubuntu1
- python-sip-dev 4.13.2-1
- python-qt4-dev 4.9.1-2ubuntu1
I uninstalled sip, qt4 and reinstalled them--same issue. I've seen the issue accross two separate machines--both running 32-bit Ubuntu 12.04.
Any help would be much appreciated--I've wasted an embarrassing amount of time waiting for plots to show.
Updates:
- The type and name of the files selected doesn't seem to matter.
- Cancelling or exiting the file dialog box results in no delay and an immediate plot.
- Running the script as sudo eliminates the issue; however, the file dialog looks different when I run as sudo and may be using a different gui backend that doesn't conflict with pyplot's use of PyQt, so may be a red herring.
- The program doesn't hang in getOpenFileNames, it hangs on the next call that uses PyQt. Whether that's a plot or another file dialog doesn't seem to matter--the first file dialog blocks both.
- Calling app.processEvents() after running the dialog doesn't help.
- Using PySide in place of PyQt4 results in the same behavior.
- Using getOpenFileName instead of getOpenFileNames results in the same behavior.
- Running getOpenFileNames with the DontUseNativeDialog option works (no delay)
- None of the other QFileDialog options have any effect (ShowDirsOnly, DontResolveSymlinks, DontConfirmOverwrite, ReadOnly, HideNameFilterDetails, DontUseSheet)