Group level analysis BIDS for fnirs , problems

  • MNE version: 1.2
  • operating system: Windows 10
  • mne-bids 0.11
  • mne-nirs 0.4.0

I followed the instructions to install mne_bids, and did something like this in my function:

from mne_bids import BIDSPath,  print_dir_tree, make_report, write_raw_bids
    

raw = Preprocessing_individual.read_hitachi([file_path1_raw, file_path2_raw])

 montage = Preprocessing_individual.self_montage(file_path_pos, file_path_csv)
 raw.set_montage(montage)
  
  event_dict = {'Sp': 1,
                'Rot-TS': 2}
  
  events = mne.find_events(raw)
  events = clean_events(events)
  
  event_desc = {v: k for k, v in event_dict.items()}
  
  # write and set annotations
  annotation = mne.annotations_from_events(events=events, sfreq=raw.info['sfreq'],
                                           event_desc=event_desc,
                                           orig_time=raw.info['meas_date'])
  raw.set_annotations(annotation)
  raw.annotations.set_durations(20)
  
  bids_path = BIDSPath(subject=str(participant_number),task='intelligible speech',
                       description='uncleaned', root='C:/Users/rebec/fNIRS-project//Data/BIDS')
  
  write_raw_bids(raw,bids_path,events=events,events_id=event_desc,montage=montage)

However, I got an import Error and I am not sure why

  File ~\Anaconda3\envs\old_mne\lib\site-packages\mne_bids\write.py:1611 in write_raw_bids
    raise ValueError(f'Unrecognized file format {ext}')

ValueError: Unrecognized file format .csv

Thank you for your help and effort!
Best,
Rebecka

Hello @rfahrn, could you please share the output of

print(raw.filenames)

? Thanks!

Hi, thank you for the message, the following gets printed out after print(raw.filenames) :
(‘C:\Users\rebec\fNIRS-project\Data\S01\S01_MES_Probe1.csv’,)

Loading C:\Users\rebec\fNIRS-project\Data\S01\S01_MES_Probe1.csv
Reading Hitachi fNIRS file version 1.25
Constructing pairing matrix for ETG-7000 (3x5)
Loading C:\Users\rebec\fNIRS-project\Data\S01\S01_MES_Probe2.csv
Reading Hitachi fNIRS file version 1.25
Constructing pairing matrix for ETG-7000 (3x5)
---------------
('C:\\Users\\rebec\\fNIRS-project\\Data\\S01\\S01_MES_Probe1.csv',)

the method ‘read_hitachi’ changed in the new MNEversion 1.2, where I am now able to pass in two hitachi-files, could that cause a problem or is it the raw-hitachi or can I somehow convert the hitachi raws to fif files? I am not sure how to proceed. :thinking:

.read_hitachi([file_path1_raw, file_path2_raw])

Thanks @rfahrn! This seems like a shortcoming in MNE-BIDS.

@sappelhoff Can you take a look please? I think we’re prematurely throwing an exception here.

1 Like

What is Preprocessing_individual in your code example?

Perhaps we just don’t support Hitachi files yet?

1 Like

But shouldn’t we simply write to FIFF or so, then? Maybe I’m confused :slight_smile:

1 Like

it just reads the hitachi-raw files i put the individual analysis in a seperate script, because I also made a custom montage with DigMontage

It basically just calls this:

    """
    Read Hitachi-csv files
    :param hitachi_path csv files:  list of strings
    :returns raw: instance of RawHitachi - raw object containing Hitachi data
    """
    raw = mne.io.read_raw_hitachi(hitachi_path)
    return raw

# raw = read_hitachi(['C:/Users/rebec/fNIRS-project/Data/S11/S11_MES_Probe1.csv', 'C:/Users/rebec/fNIRS-project/Data/S11/S11_MES_Probe2.csv'])

But shouldn’t we simply write to FIFF or so, then?

That’s what we could do, but I think we’d first have to add the file format here: https://github.com/mne-tools/mne-bids/blob/46b0a5300ed5c17ca93b8bbf1d9542069597ef62/mne_bids/config.py#L46-L69

it just reads the hitachi-raw files i put the individual analysis in a seperate script, because I also made a custom montage with DigMontage

I see, thank you

… overall I think it’d be easier if you just convert your hitachi files to FIF and then use mne-bids.

If this is a widely used file format though, we could consider adding it to the list I mentioned above.

1 Like

Okay thank you very much for your reply @richard @sappelhoff how can I convert the Raw Hitachi / .csv file into a FIF file?:thinking:

The .csv file is generated by the hitachi device.

Best,
Rebecka

Just call raw.save after reading it in via MNE, I think.

2 Likes

Couldn’t one simply load the data and then pass the Raw object to write_raw_bids(…, format=…, allow_prelod=True)?

1 Like

I have now got the raw.fif files, should I still be using: the mne_bids.write_raw_bids() method and just pass in the raw.fif files? @sappelhoff

@richard I tried that and still got an Issue.

I have now got the raw.fif files, should I still be using: the mne_bids.write_raw_bids() method and just pass in the raw.fif files? @sappelhoff

yes, just treat your data as FIF from now on

1 Like

@sappelhoff I treated it now as a filepath but i am not sure if it recognizes that it’s a raw fif data, @richard I got the following Traceback raw_file must be an instance of BaseRaw, got <class ‘str’:

    mne_bids.write_raw_bids(raw_file_path,bids_path)

  File <decorator-gen-631>:12 in write_raw_bids

  File ~\Anaconda3\envs\old_mne\lib\site-packages\mne_bids\write.py:1536 in write_raw_bids
    raise ValueError('raw_file must be an instance of BaseRaw, '

ValueError: raw_file must be an instance of BaseRaw, got <class 'str'>

What could be the issue?

I tried the following:

def write_bids(participant_list):
    root = "C:/Users/rebec/fNIRS-project/Data/BIDS"
    for part in participant_list:
        raw_file_path = "C:/Users/rebec/fNIRS-project/Data/S" + str(part) +'/S' + str(part) +'_raw.fif'
        bids_path = BIDSPath(subject=str(part),task='intelligible speech',
                     root=root)
        mne_bids.write_raw_bids(raw_file_path,bids_path)
        
list_par = ['01','04','05','07','08','09',11,12,15,16,17,18,30,31,32,33,34,35,36,37] #excluded 06
write_bids(list_par)

Given that this is fNIRS, perhaps @rob-luke can help.

In your code snippet above, the issue is that you pass a string, not a raw object. You have to read the string via mne to get the raw object. see mne.io.read_raw — MNE 1.3.dev0 documentation

1 Like

Thank you @sappelhoff :pray:t2:, I adapted the code now, to raw_obj = mne.io.read_raw(raw_file_path) but still get a Traceback (see below) @richard:

def write_bids(participant_list):
    root = "C:/Users/rebec/fNIRS-project/Data/BIDS"
    for part in participant_list:
        raw_file_path = "C:/Users/rebec/fNIRS-project/Data/S" + str(part) +'/S' + str(part) +'_raw.fif'
        raw_obj = mne.io.read_raw_fif(raw_file_path)
        bids_path = BIDSPath(subject=str(part),task='intelligible speech',
                     root=root)
        mne_bids.write_raw_bids(raw_obj,bids_path)
        
list_par = ['01','04','05','07','08','09',11,12,15,16,17,18,30,31,32,33,34,35,36,37] #excluded 06
write_bids(list_par)

Traceback:

Traceback (most recent call last):

  File ~\fNIRS-project\untitled4.py:41
    write_bids(list_par)

  File ~\fNIRS-project\untitled4.py:38 in write_bids
    mne_bids.write_raw_bids(raw_obj,bids_path)

  File <decorator-gen-621>:12 in write_raw_bids

  File ~\Anaconda3\envs\old_mne\lib\site-packages\mne_bids\write.py:1834 in write_raw_bids
    _write_dig_bids(bids_path, raw, montage, acpc_aligned,

  File ~\Anaconda3\envs\old_mne\lib\site-packages\mne_bids\dig.py:396 in _write_dig_bids
    montage = raw.get_montage()

  File ~\Anaconda3\envs\old_mne\lib\site-packages\mne\io\meas_info.py:182 in get_montage
    ch_pos = _get_fnirs_ch_pos(info)

  File ~\Anaconda3\envs\old_mne\lib\site-packages\mne\io\meas_info.py:2918 in _get_fnirs_ch_pos
    srcs, dets = _fnirs_optode_names(info)

  File ~\Anaconda3\envs\old_mne\lib\site-packages\mne\preprocessing\nirs\nirs.py:279 in _fnirs_optode_names
    sources = np.unique([int(regex.match(ch).groups()[0])

  File ~\Anaconda3\envs\old_mne\lib\site-packages\mne\preprocessing\nirs\nirs.py:279 in <listcomp>
    sources = np.unique([int(regex.match(ch).groups()[0])

AttributeError: 'NoneType' object has no attribute 'groups'

Not sure what’s going on and what’s the problem… :sweat_smile:

I did the following before I converted the raw to a FIF file:

raw = Preprocessing_individual.read_hitachi([pos_file_path_1, pos_file_path_2]) # reads Hitachi raw 
        montage = Preprocessing_individual.self_montage(file_path=montage_path_pos, csv_file=montage_path_csv)  # for conversio of pos.file to csv for DigMontage 
        raw.set_montage(montage)  # using DigMontage add it to raw
        
        # load Data
        raw.load_data()

        # read Events from Hitachi-Raw and create an annotation
        events = mne.find_events(raw)
        events = Preprocessing_individual.clean_events(events) # cleaning events selecting 50 rather than 100

        event_dict = {'Sp': 1,
                      'Rot-TS': 2,
                      'Rot-Blesser': 3,
                      'NV': 4,
                      'NV-Rot': 5}

        event_desc = {v: k for k, v in event_dict.items()}

        # write and set annotations
        annotation = mne.annotations_from_events(events=events, sfreq=raw.info['sfreq'],
                                                 event_desc=event_desc,
                                                 orig_time=raw.info['meas_date'])
        raw.set_annotations(annotation)
        raw.annotations.set_durations(20)
        raw.save(file_path,overwrite=True)

update: For anyone who has the same issue, I wrote the raw Hitachi into a SNIRF file and now it worked just fine, however it gives a later error

2 Likes