Version: current

Python Script

Description

The script block Python Script can run external Python functions, from .py files, so you can develop your own algorithms.

Preqequisite

Python 3.6 or above must be installed.

MxNKxL

Input

The input is MxN and your function will be receiving a dictionary as seen below. You can manipulate the keys of the dictionary but the variable type of each key should remain constant.

import numpy as np
data = {'msg': '', # use this key to return error messages to SAFE
'fs': 48000, # sample rate
'xlabel': 'Time', # x axis label
'ylabel': 'Amplitude', # y xais label
'X': np.array([[1,1,1], # x data
[2,2,2],
[3,3,3]]),
'Y': np.array([[5,2,5], # y data
[8,3,5],
[9,4,5]])}

Output

The output is KxL and is the return dictionary from you Python function. An example of some python processing can be seen below.

def foo(data):
"""
Example function that work with SAFE processing block
:param data: is the dictionary of data given to the function by SAFE
:return: the data dictionary processed by your script
"""
data['msg'] = "This is an error message"
data['fs'] = 12000
data['X'] = data['X'][:, 1:2] # make x single channel
data['Y'] = data['Y'][:, 1:2] + 10 # add 10 to channel 1 and make it single channel
return data
if __name__ == '__main__':
import sys
import json
import numpy as np
json_str = sys.stdin.read() # reads the encoded data passed from SAFE
input_data = {k: np.array(v) if type(v) is list else v for k, v in json.loads(json_str).items()} # decode data to numpy arrays
ret = globals()[sys.argv[1]](input_data) # function call
print(json.dumps({k.replace('\'', '"'): v.tolist() if type(v) is np.ndarray else v for k, v in ret.items()})) # encode and print output
### NO PRINTS AFTER THIS LINE ###

Attributes

AttributeValue
NamePython Script
File Path
Function Namefoo
Timeout (seconds)5
Channels1

Name

Name specifies the name of the block.

File Path

The filepath is the path to the Python .py file you want to use.

Function Name

The function name is the name of the function you want to run inside your Python script.

Timeout (seconds)

The timeout parameter determines how long time SAFE will wait for the script to execute.

Channels

Channels specifies the number of input(s)/output(s) the block will have.