UnboundLocalError: local variable 'newpath' referenced before assignment

Hello!
I need help . every time I plot my data, the error message below appears. This bug appeared a few days ago.

---------------------------------------------------------------------------
UnboundLocalError                         Traceback (most recent call last)
<ipython-input-11-f60196099aa7> in <module>
----> 1 raw.plot()

C:\ProgramData\Anaconda3\lib\site-packages\mne\io\base.py in plot(self, events, duration, start, n_channels, bgcolor, color, bad_color, event_color, scalings, remove_dc, order, show_options, title, show, block, highpass, lowpass, filtorder, clipping, show_first_samp, proj, group_by, butterfly, decim, noise_cov, event_id, show_scrollbars, show_scalebars, time_format, precompute, use_opengl, theme, verbose)
   1556                         show_scalebars=show_scalebars, time_format=time_format,
   1557                         precompute=precompute, use_opengl=use_opengl,
-> 1558                         theme=theme, verbose=verbose)
   1559 
   1560     @verbose

<C:\ProgramData\Anaconda3\lib\site-packages\decorator.py:decorator-gen-310> in plot_raw(raw, events, duration, start, n_channels, bgcolor, color, bad_color, event_color, scalings, remove_dc, order, show_options, title, show, block, highpass, lowpass, filtorder, clipping, show_first_samp, proj, group_by, butterfly, decim, noise_cov, event_id, show_scrollbars, show_scalebars, time_format, precompute, use_opengl, theme, verbose)

C:\ProgramData\Anaconda3\lib\site-packages\mne\viz\raw.py in plot_raw(raw, events, duration, start, n_channels, bgcolor, color, bad_color, event_color, scalings, remove_dc, order, show_options, title, show, block, highpass, lowpass, filtorder, clipping, show_first_samp, proj, group_by, butterfly, decim, noise_cov, event_id, show_scrollbars, show_scalebars, time_format, precompute, use_opengl, theme, verbose)
    298             title = f'{title}{extra}'
    299             if len(title) > 60:
--> 300                 title = _shorten_path_from_middle(title)
    301     elif not isinstance(title, str):
    302         raise TypeError(f'title must be None or a string, got a {type(title)}')

C:\ProgramData\Anaconda3\lib\site-packages\mne\viz\utils.py in _shorten_path_from_middle(fpath, max_len, replacement)
   2271             if len(newpath) < max_len:
   2272                 break
-> 2273         return newpath
   2274     return fpath
   2275 

UnboundLocalError: local variable 'newpath' referenced before assignment

can you paste the path of the file you’re trying to plot?

Alex

import numpy as np
import matplotlib.pyplot as plt
import pathlib
import mne
import os

raw = mne.io.read_raw_nihon(‘DA96301A.EEG’)
raw.plot()

Could you share the output of:

import mne
mne.sys_info()

And the output of:

import mne
raw = mne.io.read_raw_nihon('DA96301A.EEG')
print (raw.filenames)
print (raw._filenames)

And could you check if:

from mne.viz.utils import _shorten_path_from_middle
_shorten_path_from_middle('DA96301A.EEG')

raises the same error or not?

Yes! Raises the same error !

Could you share the output of:

import mne
mne.sys_info()

Platform: Windows-10-10.0.19041-SP0
Python: 3.7.6 (default, Jan 8 2020, 20:23:39) [MSC v.1916 64 bit (AMD64)]
Executable: C:\ProgramData\Anaconda3\python.exe
CPU: Intel64 Family 6 Model 158 Stepping 10, GenuineIntel: 12 cores
Memory: 63.9 GB

mne: 1.0.dev0
numpy: 1.18.1 {blas=mkl_rt, lapack=mkl_rt}
scipy: 1.4.1
matplotlib: 3.1.3 {backend=module://ipykernel.pylab.backend_inline}

sklearn: 0.23.2
numba: 0.48.0
nibabel: 3.2.1
nilearn: 0.8.0
dipy: 1.3.0
cupy: Not found
pandas: 1.0.1
pyvista: Not found
pyvistaqt: Not found
ipyvtklink: Not found
vtk: Not found
PyQt5: 5.9.2
ipympl: Not found
pooch: v1.6.0

mne_bids: Not found
mne_nirs: Not found
mne_features: Not found
mne_qt_browser: Not found
mne_connectivity: Not found

from mne.viz.utils import _shorten_path_from_middle
_shorten_path_from_middle('DA96301A.EEG')

Well… now I am a bit surprised. This code snippet does not raise for me, neither with mne 0.24.1 nor with the latest 1.0.dev0 version.

I would suggest trying to create a fresh environment and install MNE in it via the default channels. See if it still raises in a fresh env.

1 Like

An UnboundLocalError is raised when a local variable is referenced before it has been assigned. In most cases this will occur when trying to modify a local variable before it is actually assigned within the local scope. Python doesn’t have variable declarations, so it has to figure out the scope of variables itself. It does so by a simple rule: If there is an assignment to a variable inside a function, that variable is considered local.

Python has lexical scoping by default, which means that although an enclosed scope can access values in its enclosing scope, it cannot modify them (unless they’re declared global with the global keyword). A closure binds values in the enclosing environment to names in the local environment. The local environment can then use the bound value, and even reassign that name to something else, but it can’t modify the binding in the enclosing environment. UnboundLocalError happend because when python sees an assignment inside a function then it considers that variable as local variable and will not fetch its value from enclosing or global scope when we execute the function. However, to modify a global variable inside a function, you must use the global keyword.