Skip to content

Tips on defining markers

Markus edited this page Sep 23, 2016 · 9 revisions

Propaply your using a dynamic language like matlab, python or julia to analyze your the data create by an experiment using LSL. Each dynamic language proposes features to dynamicaly create structures contain information. You can make use of them if you define your markers in a way that they can get easily parsed to such a structure.

For instance, matlab supports so called dynamic expressions.

>> t.('bla') = 'blub'

Which results in:

t =

bla: 'blub'

a struct with the field bla containing the value blub.

With this knowledge in mind. We could imaging the following pattern for a marker.

MarkerProperty(PropertyValue)

Which could easily be parsed through a regular expression:

((\w+)((\w+)))

>> [ match tokens ] = regexp(ts,tp,'match', 'tokens')

which results in:

match = 

'bla(blub) '    'fizz(buzz) '    'foo(bar)'


tokens = 

{1x2 cell}    {1x2 cell}    {1x2 cell}

The interesting things are the tokens! These could be used to define our struct through using them as name - value pairs defining the structs fields!

>> x.(tokens{1}{1}) = tokens{1}{2}

 x =
    bla: 'blub'

Unfortunately, the approach:

cellfun(@(n,v) setfield(x, n, v), tokens)

won't work due to restrictions of the anonymous function which is not allowed to relie on a state (x). So use we might use a loop:

x = struct();
for i = 1 : numel(tokens)
   cur = tokens{i};
   x.(cur{1}) = cur{2};
end

so we could get a struct containing all fields with their values for each marker which provides it's properties throught the pattern shown above.

Sure - we might need to convert the values to specific types (eg. double). But that's another story.

Python

Same thing different syntax - suprisingly a bit more complex. The reason is that Python did not support nameless structs like matlab. We could help us with namedtuples.

import re
from collections import namedtuple 

ts = "bla(blub) fizz(buzz) foo(bar)"
tp = "\s*(?P<name>\w+)\((?P<val>\w+)\)\s*"

m = re.findall(tp, ts)

fieldnames = [ tup[0] for tup in m ]
marker = namedtuple('Marker', fieldnames)
for i in range(len(fieldnames)):
	t = m[i] 
	setattr(marker, t[0], t[1])
Clone this wiki locally