Raw.apply_function() problem

Hi,
(new user here)
I am trying to transform (with an exponential function) a sensor channel in a Raw object using the apply_function() using MNE-Python version: 0.23, in Win10.

The sensor channel is set as type ‘bio’ by raw.set_channel_types().
The function is this:

def transformfun(inputsignal):
  return numpy.exp(inputsignal * 0.000001 * 0.8302202 - 0.793399)

Applying this by
raw.apply_function(transformfun, picks=sensor_channel_pick)
yields wrong results.

So I tried something simpler:

def xfun(input):
  return (input + 1.0)
raw.apply_function(xfun, picks=sensor_channel_pick)
print("from: ", numpy.array([1871424.4365692139, 1873908.8773727417]))
print("to  : ",xfun(numpy.array([1871424.4365692139, 1873908.8773727417])))

resulting in:

from:  [1871424.43656921 1873908.87737274]
to  :  [1871425.43656921 1873909.87737274]

When I apply it to the raw object (saving before and after states in two dataframes):

df=raw.to_data_frame(start=40000,stop=40100,picks=sensor_channel_pick)
df.to_csv('before.csv',index=False)
raw.apply_function(xfun, picks=sensor_channel_pick)`
df=raw.to_data_frame(start=40000,stop=40100,picks=sensor_channel_pick)
df.to_csv('after.csv',index=False)

The first 3 lines of “before.csv” look like this:

time,HL2
40000,1871424.4365692139
40001,1873908.8773727417

Whereas the first 3 lines of “after.csv” look like this:

time,HL2
40000,2871424.436569214
40001,2873908.8773727417

That is, instead of adding 1, using apply_function adds 1000000. My guess is that there is an additional implicit transformation (V vs. microv?) somewhere (maybe related to setting channel type to ‘bio’).

Is this intended behavior? Is there a way to directly transform the numbers as seen in the dataframes? Or what is the appropriate channel type?

Thank you,
János

Hello,

MNE internally stores all (?) data in SI units. So for EEG, it will be in Volts. You can get this data via raw.get_data()

The apply_function() method operates on this array of data.

The to_data_frame() method applies some scaling such that e.g. for EEG, it would output uV, to help with readability (who wants tiny numbers in their spreadsheet?)

You can control this via the scalings parameter.

Best wishes,

Richard

Thank you for the explanation! Got it working. Thank you!