brainvisa / brainvisa.github.io

Source of BrainVISA developers site
2 stars 2 forks source link

Strange interaction between aims and matplotlib #118

Closed rbregand closed 1 year ago

rbregand commented 1 year ago

Hi,

I noticed a weird behavior when aims and matplotlib are used in the same code. You will found the code to replicate the issue below :

from soma import aims
import matplotlib.pyplot as plt

img = aims.read("/home/remy.bregand/Bureau/test_dcm2niix/GREim/DV/dcm/PV.nii")
plt.plot([1, 2])
plt.show()

output error : QWidget: Cannot create a QWidget without QApplication

The problem doesn't not occur when I'm using nibabel instead of aims to read the nifti file.

import nibabel
import matplotlib.pyplot as plt

img = nibabel.load("/home/remy.bregand/Bureau/test_dcm2niix/GREim/DV/dcm/PV.nii")
plt.plot([1, 2])
plt.show()

Environment :

Have you already notice it ? This issue is not critical, I just want you to be aware of it.

Thanks for your time

denisri commented 1 year ago

Oh OK, this is normal... You have run this code in a python script, shell, or ipython, but without running the Qt event loop. Pyaims makes use of matplotlib, and of Qt, so at some point it sets the matplotlib backend to use Qt. Thus plt.plot() needs a QApplication instance and a running event loop. I'm not sure when this backend is forced, I thought it was only when importing the Qt wapper (soma.qt_gui.qt_backend) and I don't see when it is used by just importing aims and using aims.read(), but that's apparently what happens. To solve it:

rbregand commented 1 year ago

Ok, I understand. Thanks for the explanation, I will keep this solution in mind the next time this error occurs. It's alright for me, I will close the issue