Need help importing a single-channel EEG file (CSV)

Hi, I am new to the forums and am looking for help importing a single-channel CSV file. I am attaching my code below for reference.

import pandas as pd
import json
import numpy as np
import os
import csv
import mne
import matplotlib.pyplot as plt
import seaborn as sns
%matplotlib inline
from bs4 import BeautifulSoup
from urllib.request import urlopen
path="/Users/enthusiastprogrammer/Desktop/Datasets"
os.chdir(path)

data = np.genfromtxt('mydata.csv',delimiter=',')
ch_names = ['CH 1']
sfreq = 300 #Hz
#info = mne.create_info(ch_names=["2"], sfreq=300)
info = mne.create_info(ch_names=ch_names, sfreq=sfreq)
mne.io.RawArray(data, info)

And here’s my error:

ValueError                                Traceback (most recent call last)
<ipython-input-4-cd2014e5f50c> in <module>
      9 #info = mne.create_info(ch_names=["2"], sfreq=300)
     10 info = mne.create_info(ch_names=ch_names, sfreq=sfreq)
---> 11 mne.io.RawArray(data, info)

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

~/opt/anaconda3/lib/python3.8/site-packages/mne/io/array/array.py in __init__(self, data, info, first_samp, copy, verbose)
     60         data = np.asanyarray(orig_data, dtype=dtype)
     61         if data.ndim != 2:
---> 62             raise ValueError('Data must be a 2D array of shape (n_channels, '
     63                              'n_samples), got shape %s' % (data.shape,))
     64         if len(data) != len(info['ch_names']):

ValueError: Data must be a 2D array of shape (n_channels, n_samples), got shape (9595,)

Would appreciate if any of you could help ASAP - I scoured the internet and couldn’t find an answer. Thanks so much!

  • MNE-Python version: 0.23.4
  • operating system: MacOS Big Sur Version 11.6

Hello @enthusiastprogrammer and welcome to the forum!

As the error message suggests, the shape of your data array is one-dimensional, but it must be 2-dimensional. What you can do to correct this is:

data = np.atleast_2d(data)

Then it should work. Be sure to check the resulting dimensions of the array via data.shape; it should be (1, 9595) (1 channel, 9595 time points).

Best wishes,
Richard

PS: Please format code snippets as such to improve readability. To do this, when editing a posting simply select the code blocks and click on the “Preformatted text” button in the toolbar. I’ve already edited your posting accordingly. Thanks you!