Calculating the Average of Continuous Channels

One of the frequently asked questions is how to calculate the average of multiple continuous channels in NeuroExplorer. Prior to NeuroExplorer version 5.104, we had to use a rather cumbersome function LinearCombinationOfContVars. For example, to calculate the average of channels FP01 and FP02 we would execute this script line:

nex.LinearCombinationOfContVars(doc, "average of FP01 and FP02", doc["FP01"], 0.5, doc["FP02"], 0.5)

Fortunately, Python is a very flexible language. After an update of NeuroExplorer internal Python code (that uses new functions  ContAdd, ContMult and ContAddCont), it is now possible to use arithmetic expressions using continuous channels:

import nex
doc = nex.GetActiveDocument()

# average of two channels
doc["average of FP01 and FP02"] = (doc["FP01"]+doc["FP02"])/2.0

# subtract a constant baseline
baseline = 100
doc["FP01 with baseline subtracted"] = doc["FP01"]-baseline

# subtract the reference channel
referenceChannelName = 'FP16'
doc["FP01 with ref. channel subtracted"] = doc["FP01"]-doc[referenceChannelName]

# calculate the average of all continuous channels containing FP in channel name
count = 0
for i in range(nex.GetVarCount(doc, 'continuous')):
 name = nex.GetVarName(doc, i+1, 'continuous')
 if 'FP' in name:
   if count == 0:
     doc['FP average'] = doc[name]
   else:
     doc['FP average'] += doc[name]
   count += 1
 
if count > 0:
 doc['FP average'] = doc['FP average']/float(count)