Skip to main content
Version: current

Python Script


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


Python 3.6 or above must be installed.



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
'Y': np.array([[5,2,5], # y data


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 = # 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


NamePython Script
File Path
Function Namefoo
Timeout (seconds)5


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 specifies the number of input(s)/output(s) the block will have.