Source code for node_correlate
# This file is part of Sympathy for Data.
# Copyright (c) 2017, Combine Control Systems AB
#
# SYMPATHY FOR DATA COMMERCIAL LICENSE
# You should have received a link to the License with Sympathy for Data.
import numpy as np
from sympathy.api import node
from sympathy.api.exceptions import SyConfigurationError
from sympathy.api.nodeconfig import Port, Ports, Tag, Tags
[docs]
class Correlate(node.Node):
"""
"""
name = 'Correlate / Convolution'
author = 'Mathias Broxvall'
icon = 'convolution.svg'
description = (
'Performs linear correlation or convolution of two 1D signals'
)
nodeid = 'com.sympathyfordata.timeseriesanalysis.correlate'
tags = Tags(Tag.Analysis.SignalProcessing)
parameters = node.parameters()
parameters.set_string(
'operation',
label='Operation',
value='correlation',
description='Operation to perform',
editor=node.editors.combo_editor(
options=['correlation', 'convolution']))
parameters.set_string(
'mode',
label='Mode',
value='valid',
description='Mode for handling border data. \n'
'\n "Full" applies operation '
'on each point of overlap, giving a larger output than '
'inputs and boundary effects may be seen. \n \n "Same" '
'limits output length to max of input lengths and '
'boundary effects are still visible. \n \n "Valid" '
'performs operation only on fully overlapping data',
editor=node.editors.combo_editor(
options=['full', 'same', 'valid']))
parameters.set_string(
'combinations',
label='Combinations',
value='by column number',
description=(
'Determines which combinations of signals are operated on.'),
editor=node.editors.combo_editor(
options=[
'by column number', 'by column name', 'all combinations']))
inputs = Ports([
Port.Table('A', name='A'),
Port.Table('V', name='V')
])
outputs = Ports([
Port.Table('result', name='result')
])
def execute(self, node_context):
op = node_context.parameters['operation'].value
mode = node_context.parameters['mode'].value
combinations = node_context.parameters['combinations'].value
A_tbl = node_context.input['A']
V_tbl = node_context.input['V']
out = node_context.output['result']
def process_columns(A_col, V_col, name):
A = A_col.data
V = V_col.data
if op == 'correlation':
res = np.correlate(A, V, mode=mode)
elif op == 'convolution':
res = np.convolve(A, V, mode=mode)
else:
raise SyConfigurationError(
'Invalid operation parameter in correlation node')
out.set_column_from_array(name, res)
if combinations == 'by column number':
for i in range(
min(A_tbl.number_of_columns(), V_tbl.number_of_columns())):
process_columns(
A_tbl.cols()[i], V_tbl.cols()[i], '{}'.format(i))
elif combinations == 'by column name':
for A_col in A_tbl.cols():
if A_col.name in V_tbl.column_names():
V_col = V_tbl.col(A_col.name)
process_columns(A_col, V_col, A_col.name)
elif combinations == 'all combinations':
for A_col in A_tbl.cols():
for V_col in V_tbl.cols():
process_columns(A_col, V_col, A_col.name+'_'+V_col.name)
else:
raise SyConfigurationError('Invalid setting for combinations')