Renaming Brain Vision Recorder EEG recordings

Renaiming Brain Vision Recorder files is a pain. Here’s a python script that makes it (more of) a breeze.

The problem

Brain Vision Recorder is a widely used software for EEG-recordings. Every EEG-recording is saved as three files:

  1. One .eeg file, containing all the data
  2. One .vmrk/.amrk/.bmrk file, containing markers and a reference to the .eeg file
  3. One .vhdr/.ahdr/.bhdr file, containing header information and references to the marker file and data file. This is the file that represents the recording and opened by analysis software

Because of these internal references between the files, it is quite laborious (and risky) to rename a recording. I often rename my recordings before analysis. I use the Python script below for this purpose.

The script

import os, fileinput

# Specify folder with .eeg and meta-files
folder = '/home/jonas/Desktop/test/'

# Loop through all files
for filename in sorted(os.listdir(folder)):
    filebase, filetype = os.path.splitext(filename)

    # Only operate on selected file extensions
    if filetype in ('.eeg', '.bhdr', '.vhdr','.ahdr','.bmrk','.amrk','.vmrk'):
        print 'processing '+filename+'...'

        # Generate new name
        newname = 'XXXX' + filename

        # Actually rename file
        os.rename(folder+filename,folder+newname)

        # Updates header and marker files to match actual filenames
        filebase, filetype = os.path.splitext(newname)
        if filetype != '.eeg':
            for line in fileinput.FileInput(folder +newname, inplace=1):
                # For header-files and marker-files
                if 'DataFile=' in line: line = 'DataFile='+filebase+'.eeg'

                # For header-files
                elif 'MarkerFile=' in line:
                    tmp_str = 'MarkerFile='+filebase
                    if filetype == '.bhdr': tmp_str +='.bmrk'
                    if filetype == '.ahdr': tmp_str +='.amrk'
                    if filetype == '.vhdr': tmp_str +='.vmrk'
                    line = tmp_str
                print line.replace('\n','')

How to modify the script

The important part is just below the “#Generate new name”. You can use this in two ways. First, you can rename your files in the browser and type in

newname = filename

This will simply update the header information in the header- and marker-files. Secondly, you can use all sorts of generative filename generation. E.g. this takes a subject ID from the middle of the filename and adds it to the front after a paradigm-name:

newname = 'aud-oddball, subject ' +filename[6:10] + filename[:6] + filename[10:]

I hope this helps someone out there.