get raw data numpy array

  • MNE version: 1.0.2
  • operating system: Windows 10
import mne
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

raw = mne.io.read_raw_edf('C:/Users//SC4001E0-PSG.edf', preload=True)
print(raw)
print(raw.ch_names)

info = mne.create_info(ch_names=['EEG Fpz-Cz'],
                       ch_types=['eeg'],
                       sfreq=100)

raw1 = raw.pick_channels(['EEG Fpz-Cz'])
np_raw = mne.io.RawArray(raw1, info)
print(np_raw)

ValueError: All picks must be < n_channels (1), got 1

There is only one channel in the data, hence you cannot pick, as suggested by the error message.

1 Like

Right, but If erase this raw1 = raw.['EEG Fpz-Cz'], it says All picks must be < n_channels (7), got 7.

The raw data used consists of seven channels.

Please post the full code and complete error message (the complete traceback)

import mne
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

raw = mne.io.read_raw_edf('C:/Users//SC4001E0-PSG.edf', preload=True)
print(raw)
print(raw.ch_names)

info = mne.create_info(ch_names=['EEG Fpz-Cz'],
                       ch_types=['eeg'],
                       sfreq=100)

np_raw = mne.io.RawArray(raw, info)
print(np_raw)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_11320/1799635321.py in <module>
     15                        sfreq=100)
     16 
---> 17 np_raw=mne.io.RawArray(raw, info)
     18 print(np_raw)

<decorator-gen-219> in __init__(self, data, info, first_samp, copy, verbose)

~\anaconda3\lib\site-packages\mne\io\array\array.py in __init__(self, data, info, first_samp, copy, verbose)
     55         _validate_type(info, 'info', 'info')
     56         _check_option('copy', copy, ('data', 'info', 'both', 'auto', None))
---> 57         dtype = np.complex128 if np.any(np.iscomplex(data)) else np.float64
     58         orig_data = data
     59         data = np.asanyarray(orig_data, dtype=dtype)

<__array_function__ internals> in iscomplex(*args, **kwargs)

~\anaconda3\lib\site-packages\numpy\lib\type_check.py in iscomplex(x)
    238 
    239     """
--> 240     ax = asanyarray(x)
    241     if issubclass(ax.dtype.type, _nx.complexfloating):
    242         return ax.imag != 0

~\anaconda3\lib\site-packages\numpy\core\_asarray.py in asanyarray(a, dtype, order, like)
    169         return _asanyarray_with_like(a, dtype=dtype, order=order, like=like)
    170 
--> 171     return array(a, dtype, copy=False, order=order, subok=True)
    172 
    173 

~\anaconda3\lib\site-packages\mne\io\base.py in __getitem__(self, item)
    807 
    808         """  # noqa: E501
--> 809         return self._getitem(item)
    810 
    811     def _getitem(self, item, return_times=True):

~\anaconda3\lib\site-packages\mne\io\base.py in _getitem(self, item, return_times)
    810 
    811     def _getitem(self, item, return_times=True):
--> 812         sel, start, stop = self._parse_get_set_params(item)
    813         if self.preload:
    814             data = self._data[sel, start:stop]

~\anaconda3\lib\site-packages\mne\io\base.py in _parse_get_set_params(self, item)
    742                                "and time)")
    743 
--> 744         sel = _picks_to_idx(self.info, item[0])
    745 
    746         if isinstance(item[1], slice):

~\anaconda3\lib\site-packages\mne\io\pick.py in _picks_to_idx(info, picks, none, exclude, allow_empty, with_ref_meg, return_kind)
   1067                          % (-n_chan, orig_picks))
   1068     if (picks >= n_chan).any():
-> 1069         raise ValueError('All picks must be < n_channels (%d), got %r'
   1070                          % (n_chan, orig_picks))
   1071     picks %= n_chan  # ensure positive

ValueError: All picks must be < n_channels (7), got 7

Does RawArray actually accept Raw? I thought it needs an array as input.

In any case, I don’t understand what you’re even trying to do here? Why do you load a raw file, just to take it apart and generate a RawArray manually from it? This doesn’t make any sense to me :thinking:

In order to use the function of yasa, any channel in edf must be converted to a numpy array.

Why don’t you just use raw.get_data() to get an array?

1 Like

Good. I asked because I couldn’t find raw.get_data with ctrl+F in api.

1 Like

rather than “using ctrl+f in the API” (not clear where exactly you were searching?) I suggest using the search box on our website. You’ll see that I was able to see several results when searching for raw.get_data; the second one is the most appropriate result but any of the first three would likely have told you what you needed to know: