Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OverFlowError when calling : SourceEstimate.plot(...) #12606

Closed
ilianAZZ opened this issue May 7, 2024 · 3 comments · Fixed by #12612
Closed

OverFlowError when calling : SourceEstimate.plot(...) #12606

ilianAZZ opened this issue May 7, 2024 · 3 comments · Fixed by #12612
Labels
Milestone

Comments

@ilianAZZ
Copy link

ilianAZZ commented May 7, 2024

Description of the problem

Hello,

I'm trying to use the SourceEstimation.plot to get the 3D brain graph with the red activated areas.

When calling SourceEstimation.plot, i'm facing an Python Exception: `OverflowError: cannot convert float infinity to integer``

I'm sure the problem is from the SourceEstimate.data. Beacause after adding 1e-14 to all elements of this array, the exception is lo longer here.

My SourceEstimate.data matrix is a float64, it does not contains any INF or NAN :

print("== After editing data")
print("Shape", data_recreate.shape)
print("Min, Max", np.min(data_recreate), np.max(data_recreate))
print("Contains NaN", np.isnan(data_recreate).any())
print("Contains inf", np.isinf(data_recreate).any())
print("All types", set(type(element) for row in data_recreate for element in row))

stc_recreate = mne.SourceEstimate(data=data_recreate, vertices=vertices_recreate, tmin=tmin_recreate, tstep=tstep_recreate, subject=subject_recreate)

initial_time = 0.1
brain = stc_recreate.plot(
    subjects_dir=subjects_dir,
    initial_time=initial_time,
    smoothing_steps=7,
    hemi='both',

Output :

== After editing data
Shape (7498, 305)
Min, Max 0.0 1.8034394545323202e-10
Contains NaN False
Contains inf False
All types {<class 'numpy.float64'>}

I then supposed that this error was caused by the presence of "0" in the data.
So I tried with a matrix that contains multiple lines with 0, things goes well.

data_recreate = stc_copy.data.copy()
data_recreate += 1e-14   #  Avoir OverflowException

data_recreate[:, 1:10] = 0
data_recreate[0:200:, :] = 0

So I suppose this as an issue. Let me know if I'm doing something wrong.

Best

File c:\Users\Ilian\Documents\dev\best-python\.venv\lib\site-packages\mne\source_estimate.py:743, in _BaseSourceEstimate.plot(self, subject, surface, hemi, colormap, time_label, smoothing_steps, transparent, alpha, time_viewer, subjects_dir, figure, views, colorbar, clim, cortex, size, background, foreground, initial_time, time_unit, backend, spacing, title, show_traces, src, volume_options, view_layout, add_data_kwargs, brain_kwargs, verbose)
    [709](file:///C:/Users/Ilian/Documents/dev/best-python/.venv/lib/site-packages/mne/source_estimate.py:709) @copy_function_doc_to_method_doc(plot_source_estimates)
    [710](file:///C:/Users/Ilian/Documents/dev/best-python/.venv/lib/site-packages/mne/source_estimate.py:710) def plot(
    [711](file:///C:/Users/Ilian/Documents/dev/best-python/.venv/lib/site-packages/mne/source_estimate.py:711)     self,
   (...)
    [741](file:///C:/Users/Ilian/Documents/dev/best-python/.venv/lib/site-packages/mne/source_estimate.py:741)     verbose=None,
    [742](file:///C:/Users/Ilian/Documents/dev/best-python/.venv/lib/site-packages/mne/source_estimate.py:742) ):
--> [743](file:///C:/Users/Ilian/Documents/dev/best-python/.venv/lib/site-packages/mne/source_estimate.py:743)     brain = plot_source_estimates(
    [744](file:///C:/Users/Ilian/Documents/dev/best-python/.venv/lib/site-packages/mne/source_estimate.py:744)         self,
    [745](file:///C:/Users/Ilian/Documents/dev/best-python/.venv/lib/site-packages/mne/source_estimate.py:745)         subject,
    [746](file:///C:/Users/Ilian/Documents/dev/best-python/.venv/lib/site-packages/mne/source_estimate.py:746)         surface=surface,
    [747](file:///C:/Users/Ilian/Documents/dev/best-python/.venv/lib/site-packages/mne/source_estimate.py:747)         hemi=hemi,
    [748](file:///C:/Users/Ilian/Documents/dev/best-python/.venv/lib/site-packages/mne/source_estimate.py:748)         colormap=colormap,
    [749](file:///C:/Users/Ilian/Documents/dev/best-python/.venv/lib/site-packages/mne/source_estimate.py:749)         time_label=time_label,
    [750](file:///C:/Users/Ilian/Documents/dev/best-python/.venv/lib/site-packages/mne/source_estimate.py:750)         smoothing_steps=smoothing_steps,
    [751](file:///C:/Users/Ilian/Documents/dev/best-python/.venv/lib/site-packages/mne/source_estimate.py:751)         transparent=transparent,
    [752](file:///C:/Users/Ilian/Documents/dev/best-python/.venv/lib/site-packages/mne/source_estimate.py:752)         alpha=alpha,
    [753](file:///C:/Users/Ilian/Documents/dev/best-python/.venv/lib/site-packages/mne/source_estimate.py:753)         time_viewer=time_viewer,
    [754](file:///C:/Users/Ilian/Documents/dev/best-python/.venv/lib/site-packages/mne/source_estimate.py:754)         subjects_dir=subjects_dir,
    [755](file:///C:/Users/Ilian/Documents/dev/best-python/.venv/lib/site-packages/mne/source_estimate.py:755)         figure=figure,
    [756](file:///C:/Users/Ilian/Documents/dev/best-python/.venv/lib/site-packages/mne/source_estimate.py:756)         views=views,
    [757](file:///C:/Users/Ilian/Documents/dev/best-python/.venv/lib/site-packages/mne/source_estimate.py:757)         colorbar=colorbar,
    [758](file:///C:/Users/Ilian/Documents/dev/best-python/.venv/lib/site-packages/mne/source_estimate.py:758)         clim=clim,
    [759](file:///C:/Users/Ilian/Documents/dev/best-python/.venv/lib/site-packages/mne/source_estimate.py:759)         cortex=cortex,
    [760](file:///C:/Users/Ilian/Documents/dev/best-python/.venv/lib/site-packages/mne/source_estimate.py:760)         size=size,
    [761](file:///C:/Users/Ilian/Documents/dev/best-python/.venv/lib/site-packages/mne/source_estimate.py:761)         background=background,
    [762](file:///C:/Users/Ilian/Documents/dev/best-python/.venv/lib/site-packages/mne/source_estimate.py:762)         foreground=foreground,
    [763](file:///C:/Users/Ilian/Documents/dev/best-python/.venv/lib/site-packages/mne/source_estimate.py:763)         initial_time=initial_time,
    [764](file:///C:/Users/Ilian/Documents/dev/best-python/.venv/lib/site-packages/mne/source_estimate.py:764)         time_unit=time_unit,
    [765](file:///C:/Users/Ilian/Documents/dev/best-python/.venv/lib/site-packages/mne/source_estimate.py:765)         backend=backend,
    [766](file:///C:/Users/Ilian/Documents/dev/best-python/.venv/lib/site-packages/mne/source_estimate.py:766)         spacing=spacing,
    [767](file:///C:/Users/Ilian/Documents/dev/best-python/.venv/lib/site-packages/mne/source_estimate.py:767)         title=title,
    [768](file:///C:/Users/Ilian/Documents/dev/best-python/.venv/lib/site-packages/mne/source_estimate.py:768)         show_traces=show_traces,
...
-> [4189](file:///C:/Users/Ilian/Documents/dev/best-python/.venv/lib/site-packages/mne/viz/_brain/_brain.py:4189)     fscale_power = int(np.log10(fmax))
   [4190](file:///C:/Users/Ilian/Documents/dev/best-python/.venv/lib/site-packages/mne/viz/_brain/_brain.py:4190)     if fscale_power < 0:
   [4191](file:///C:/Users/Ilian/Documents/dev/best-python/.venv/lib/site-packages/mne/viz/_brain/_brain.py:4191)         fscale_power -= 1

OverflowError: cannot convert float infinity to integer

Steps to reproduce

import numpy as np
from scipy import linalg

import mne
from mne.datasets import sample
from mne.viz import plot_sparse_source_estimates

data_path = sample.data_path()
meg_path = data_path / "MEG" / "sample"
fwd_fname = meg_path / "sample_audvis-meg-eeg-oct-6-fwd.fif"
ave_fname = meg_path / "sample_audvis-ave.fif"
cov_fname = meg_path / "sample_audvis-shrunk-cov.fif"
subjects_dir = data_path / "subjects"
condition = "Left Auditory"

# Read noise covariance matrix
noise_cov = mne.read_cov(cov_fname)
# Handling average file
evoked = mne.read_evokeds(ave_fname, condition=condition, baseline=(None, 0))
evoked.crop(tmin=0.04, tmax=0.18)

evoked = evoked.pick(picks="meg", exclude="bads")
# Handling forward solution
forward = mne.read_forward_solution(fwd_fname)


fname_stc = meg_path / "sample_audvis-meg"

stc_sample = mne.read_source_estimate(fname_stc, subject="sample")


data_recreate = np.load("Matrix_data_cause_stc_plot_error.mat.npy")

vertices_recreate = stc_sample.vertices
tmin_recreate = stc_sample.tmin
tstep_recreate = stc_sample.tstep
subject_recreate = stc_sample.subject

stc_recreate = mne.SourceEstimate(data=data_recreate, vertices=vertices_recreate, tmin=tmin_recreate, tstep=tstep_recreate, subject=subject_recreate)

initial_time = 0.1
brain = stc_recreate.plot(
    subjects_dir=subjects_dir,
    initial_time=initial_time,
    smoothing_steps=7,
    hemi='both',
)

Link to data

Matrix_data_cause_stc_plot_error.mat.zip

Expected results

image

Actual results


OverflowError Traceback (most recent call last)
Cell In[17], line 37
34 stc_recreate = mne.SourceEstimate(data=data_recreate, vertices=vertices_recreate, tmin=tmin_recreate, tstep=tstep_recreate, subject=subject_recreate)
36 initial_time = 0.1
---> 37 brain = stc_recreate.plot(
38 subjects_dir=subjects_dir,
39 initial_time=initial_time,
40 smoothing_steps=7,
41 hemi='both',
42 )

File c:\Users\Ilian\Documents\dev\best-python.venv\lib\site-packages\mne\source_estimate.py:793, in _BaseSourceEstimate.plot(self, subject, surface, hemi, colormap, time_label, smoothing_steps, transparent, alpha, time_viewer, subjects_dir, figure, views, colorbar, clim, cortex, size, background, foreground, initial_time, time_unit, backend, spacing, title, show_traces, src, volume_options, view_layout, add_data_kwargs, brain_kwargs, verbose)
759 @copy_function_doc_to_method_doc(plot_source_estimates)
760 def plot(
761 self,
(...)
791 verbose=None,
792 ):
--> 793 brain = plot_source_estimates(
794 self,
795 subject,
796 surface=surface,
797 hemi=hemi,
798 colormap=colormap,
799 time_label=time_label,
800 smoothing_steps=smoothing_steps,
801 transparent=transparent,
802 alpha=alpha,
803 time_viewer=time_viewer,
804 subjects_dir=subjects_dir,
805 figure=figure,
806 views=views,
807 colorbar=colorbar,
808 clim=clim,
809 cortex=cortex,
810 size=size,
811 background=background,
812 foreground=foreground,
813 initial_time=initial_time,
814 time_unit=time_unit,
815 backend=backend,
816 spacing=spacing,
817 title=title,
818 show_traces=show_traces,
819 src=src,
820 volume_options=volume_options,
821 view_layout=view_layout,
822 add_data_kwargs=add_data_kwargs,
823 brain_kwargs=brain_kwargs,
824 verbose=verbose,
825 )
826 return brain

File :12, in plot_source_estimates(stc, subject, surface, hemi, colormap, time_label, smoothing_steps, transparent, alpha, time_viewer, subjects_dir, figure, views, colorbar, clim, cortex, size, background, foreground, initial_time, time_unit, backend, spacing, title, show_traces, src, volume_options, view_layout, add_data_kwargs, brain_kwargs, verbose)

File c:\Users\Ilian\Documents\dev\best-python.venv\lib\site-packages\mne\viz_3d.py:2427, in plot_source_estimates(stc, subject, surface, hemi, colormap, time_label, smoothing_steps, transparent, alpha, time_viewer, subjects_dir, figure, views, colorbar, clim, cortex, size, background, foreground, initial_time, time_unit, backend, spacing, title, show_traces, src, volume_options, view_layout, add_data_kwargs, brain_kwargs, verbose)
2425 else:
2426 with use_3d_backend(backend):
-> 2427 return _plot_stc(
2428 stc,
2429 overlay_alpha=alpha,
2430 brain_alpha=alpha,
2431 vector_alpha=alpha,
2432 cortex=cortex,
2433 foreground=foreground,
2434 size=size,
2435 scale_factor=None,
2436 show_traces=show_traces,
2437 src=src,
2438 volume_options=volume_options,
2439 view_layout=view_layout,
2440 add_data_kwargs=add_data_kwargs,
2441 brain_kwargs=brain_kwargs,
2442 **kwargs,
2443 )

File c:\Users\Ilian\Documents\dev\best-python.venv\lib\site-packages\mne\viz_3d.py:2597, in _plot_stc(stc, subject, surface, hemi, colormap, time_label, smoothing_steps, subjects_dir, views, clim, figure, initial_time, time_unit, background, time_viewer, colorbar, transparent, brain_alpha, overlay_alpha, vector_alpha, cortex, foreground, size, scale_factor, show_traces, src, volume_options, view_layout, add_data_kwargs, brain_kwargs)
2594 del kwargs
2596 if time_viewer:
-> 2597 brain.setup_time_viewer(time_viewer=time_viewer, show_traces=show_traces)
2598 else:
2599 brain.show()

File c:\Users\Ilian\Documents\dev\best-python.venv\lib\site-packages\mne\viz_brain_brain.py:601, in Brain.setup_time_viewer(failed resolving arguments)
599 self._configure_shortcuts()
600 self._configure_picking()
--> 601 self._configure_dock()
602 self._configure_tool_bar()
603 self._configure_menu()

File c:\Users\Ilian\Documents\dev\best-python.venv\lib\site-packages\mne\viz_brain_brain.py:1027, in Brain._configure_dock(self)
1025 self._configure_dock_playback_widget(name="Playback")
1026 self._configure_dock_orientation_widget(name="Orientation")
-> 1027 self._configure_dock_colormap_widget(name="Color Limits")
1028 self._configure_dock_trace_widget(name="Trace")
1030 # Smoothing widget

File c:\Users\Ilian\Documents\dev\best-python.venv\lib\site-packages\mne\viz_brain_brain.py:867, in Brain._configure_dock_colormap_widget(self, name)
866 def _configure_dock_colormap_widget(self, name):
--> 867 fmin, fmax, fscale, fscale_power = _get_range(self)
868 rng = [fmin * fscale, fmax * fscale]
869 self._data["fscale"] = fscale

File c:\Users\Ilian\Documents\dev\best-python.venv\lib\site-packages\mne\viz_brain_brain.py:4181, in _get_range(brain)
4179 fscale_power = 0
4180 else:
-> 4181 fscale_power = int(np.log10(fmax))
4182 if fscale_power < 0:
4183 fscale_power -= 1

OverflowError: cannot convert float infinity to integer

Additional information

Platform Windows-10-10.0.22621-SP0
Python 3.9.0 (tags/v3.9.0:9cf6752, Oct 5 2020, 15:34:40) [MSC v.1927 64 bit (AMD64)]
Executable c:\Users\Ilian\Documents\dev\best-python.venv\Scripts\python.exe
CPU Intel64 Family 6 Model 165 Stepping 2, GenuineIntel (8 cores)
Memory 31.8 GB

Core
├☑ mne 1.7.0 (latest release)
├☑ numpy 1.26.4 (OpenBLAS 0.3.23.dev with 8 threads)
├☑ scipy 1.13.0
└☑ matplotlib 3.8.4 (backend=module://matplotlib_inline.backend_inline)

Numerical (optional)
├☑ sklearn 1.4.2
├☑ nibabel 5.2.1
├☑ nilearn 0.10.4
├☑ pandas 2.2.2
└☐ unavailable numba, dipy, openmeeg, cupy, h5io, h5py

Visualization (optional)
├☑ pyvista 0.43.5 (OpenGL 4.5.0 NVIDIA 552.12 via NVIDIA GeForce GTX 1650 Ti/PCIe/SSE2)
├☑ pyvistaqt 0.11.0
├☑ vtk 9.3.0
├☑ qtpy 2.4.1 (PyQt6=6.6.1)
└☐ unavailable ipympl, pyqtgraph, mne-qt-browser, ipywidgets, trame_client, trame_server, trame_vtk, trame_vuetify

Ecosystem (optional)
└☐ unavailable mne-bids, mne-nirs, mne-features, mne-connectivity, mne-icalabel, mne-bids-pipeline, neo, eeglabio, edfio, mffpy, pybv

@ilianAZZ ilianAZZ added the BUG label May 7, 2024
Copy link

welcome bot commented May 7, 2024

Hello! 👋 Thanks for opening your first issue here! ❤️ We will try to get back to you soon. 🚴

@ilianAZZ ilianAZZ changed the title OverFlowError when calling : SourceEstimate.plot(...) OverFlowError when calling : SourceEstimate.plot(...) May 7, 2024
@drammock
Copy link
Member

Thanks for the report. It appears it's happening because the maximum value of your data is zero (which may indicate a mistake somewhere in your script; is data with no positive values plausible for you?)

The error is happening here:

def _get_range(brain):
"""Get the data limits.
Since they may be very small (1E-10 and such), we apply a scaling factor
such that the data range lies somewhere between 0.01 and 100. This makes
for more usable sliders. When setting a value on the slider, the value is
multiplied by the scaling factor and when getting a value, this value
should be divided by the scaling factor.
"""
val = np.abs(np.concatenate(list(brain._current_act_data.values())))
fmin, fmax = np.min(val), np.max(val)
if 1e-02 <= fmax <= 1e02:
fscale_power = 0
else:
fscale_power = int(np.log10(fmax))
if fscale_power < 0:
fscale_power -= 1
fscale = 10**-fscale_power
return fmin, fmax, fscale, fscale_power

@wmvanvliet you wrote this code in your UI event system work (#11891), any chance you have time to take a look?

@wmvanvliet
Copy link
Contributor

Ah, the np.log10() call needs protection to prevent no.log10(0) of course. My bad :) PR incoming

@larsoner larsoner added this to the 1.8 milestone May 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants