Skip to content

Instantly share code, notes, and snippets.

View robertoostenveld's full-sized avatar

Robert Oostenveld robertoostenveld

View GitHub Profile
@robertoostenveld
robertoostenveld / unicorn2lsl.py
Created October 9, 2022 16:15
This is a native python implementation to stream data from a Unicorn EEG system to LSL. It works on macOS, Linux and Windows.
#!/usr/bin/env python
# Unicorn2lsl streams data from a Unicorn Hybrid Black EEG system to LSL
#
# Copyright (C) 2022 Robert Oostenveld
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
@robertoostenveld
robertoostenveld / plot_missing_channels.m
Created May 19, 2021 15:22
plot the channels that are now missing from our CTF275 MEG system
%%
cfg = [];
cfg.layout = 'CTF275_helmet.mat';
layout = ft_prepare_layout(cfg);
sel275 = {'MLC11', 'MLC12', 'MLC13', 'MLC14', 'MLC15', 'MLC16', 'MLC17', 'MLC21', 'MLC22', 'MLC23', 'MLC24', 'MLC25', 'MLC31', 'MLC32', 'MLC41', 'MLC42', 'MLC51', 'MLC52', 'MLC53', 'MLC54', 'MLC55', 'MLC61', 'MLC62', 'MLC63', 'MLF11', 'MLF12', 'MLF13', 'MLF14', 'MLF21', 'MLF22', 'MLF23', 'MLF24', 'MLF25', 'MLF31', 'MLF32', 'MLF33', 'MLF34', 'MLF35', 'MLF41', 'MLF42', 'MLF43', 'MLF44', 'MLF45', 'MLF46', 'MLF51', 'MLF52', 'MLF53', 'MLF54', 'MLF55', 'MLF56', 'MLF61', 'MLF62', 'MLF63', 'MLF64', 'MLF65', 'MLF66', 'MLF67', 'MLO11', 'MLO12', 'MLO13', 'MLO14', 'MLO21', 'MLO22', 'MLO23', 'MLO24', 'MLO31', 'MLO32', 'MLO33', 'MLO34', 'MLO41', 'MLO42', 'MLO43', 'MLO44', 'MLO51', 'MLO52', 'MLO53', 'MLP11', 'MLP12', 'MLP21', 'MLP22', 'MLP23', 'MLP31', 'MLP32', 'MLP33', 'MLP34', 'MLP35', 'MLP41', 'MLP42', 'MLP43', 'MLP44', 'MLP45', 'MLP51', 'MLP52', 'M
@robertoostenveld
robertoostenveld / replace.py
Created June 6, 2020 09:39
replace a string in a file with another one, the string can include newlines etcetera
import sys
filename = sys.argv[1]
f=open(filename,"rb")
s=f.read()
f.close()
s=s.replace(b'OLDSTRING',b'NEWSTRING')
f = open(filename, "wb")
@robertoostenveld
robertoostenveld / eegsynth_patch_example1.py
Created June 16, 2019 13:24
hypothetical example for a patch, assuming that modules are implemented as classes in an EEGsynth library
#!/usr/bin/env python
import sys
from EEGsynth import preprocessing, spectral, plotspectral, plotsignal, historycontrol, postprocessing
inipath = '/home/stephen/PycharmProjects/eegsynth/module/inputcontrol/'
patch = []
patch.append(preprocessing(inifile=inipath + 'preprocessing.ini'))
patch.append(spectral(inifile=inipath + 'spectral.ini'))
@robertoostenveld
robertoostenveld / prefixscale.m
Created June 12, 2019 12:08
MATLAB code that determines the scaling factor to convert an SI unit from one to another prefix
function scale = prefixscale(old, new)
% PREFIXSCALE determines the scaling factor to convert an SI unit from one to another prefix
%
% For example
% prefixscale('kg', 'mg')
% returns 1000000.
% Copyright (C) 2019, Robert Oostenveld
@robertoostenveld
robertoostenveld / truncate.m
Created November 21, 2018 09:25
MATLAB function to truncate the content of a file
function truncate(filename)
% TRUNCATE a file to 0 bytes, i.e. replace the file with an empty one
%
% Use as
% truncate(filename)
assert(exist(filename,'file'), 'file does not exists');
fid = fopen(filename, 'w');
fclose(fid);
@robertoostenveld
robertoostenveld / rename_brainvision_files.m
Created September 25, 2018 14:17
MATLAB function to rename all three files of a BrainVision EEG dataset. It also updates the details inside the files.
function rename_brainvision_files(oldheaderfile, newheaderfile)
% RENAME_BRAINVISION_FILES renames a BrainVision EEG dataset, which consists of a vhdr header
% file, vmrk marker file and a data file that usually has the extension dat, eeg or seg.
%
% Use as
% rename_brainvision_files(oldname, newname)
% where both the old and the new filename should be strings corresponding to the
% header file, i.e. including the vhdr extension.
%
@robertoostenveld
robertoostenveld / ft_read_header.m
Created March 2, 2018 08:36
logical flow in ft_read_xxx to support the plugin of new file formats
hdr = ft_read_header(filename, varargin)
filetype = ft_getopt(varargin, 'filetype');
if isempty(filetype)
filetype = ft_filetype(filename);
end
switch filetype
case 'aaa':
% do whatever it used to be doing
@robertoostenveld
robertoostenveld / example_format.m
Last active January 30, 2018 16:17
This demonstrates the plugin format for the FieldTrip fileio module.
function [varargout] = example_format(varargin)
% EXAMPLE_FORMAT demonstrates the plugin format for the FieldTrip fileio module.
%
% Use as
% hdr = ft_read_data('test.mff', 'headerformat', 'example_format')
% dat = ft_read_data('test.mff', 'headerformat', 'example_format', 'dataformat', 'example_format')
st = dbstack;
if length(st)>1 && strcmp(st(2).name, 'ft_read_header')
@robertoostenveld
robertoostenveld / interface.c
Created September 3, 2017 20:17
snippet of code to be inserted in interface.c to replace gethostbyname
int sockfd;
struct addrinfo hints, *servinfo, *p;
int rv;
memset(&hints, 0, sizeof hints);
hints.ai_family = AF_UNSPEC; // use AF_INET6 to force IPv6
hints.ai_socktype = SOCK_STREAM;
if ((rv = getaddrinfo(host, "http", &hints, &servinfo)) != 0) {
fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv));