If you used BRAINscript numeric functions in LAE, the following examples show how to complete the same task in the Python-based nodes.
You can test the following examples by taking a Create Data node, connecting it to a Transform node, then copy and pasting the examples into the ConfigureFields and ProcessRecords properties of the Transform node.
For more information about the Python functions, see the Python documentation, for example: https://docs.python.org/2/library/math.html
BRAINscript | Python |
---|---|
|
|
|
math.ceil(x) |
|
float() |
|
math.floor(x) |
|
int(x) |
|
Custom function |
|
long() |
|
round(x) |
|
math.pow() |
|
math.sqrt() |
abs(x)
Output the values of a numeric field as absolute numbers, e.g. the input value -27959 is output as 27959:
ConfigureFields
#Configure all fields from input 'inputs[0]' to be mapped
#to the corresponding fields on the output 'outputs[0]'
outputs[0] += inputs[0]
#Specify the new field as a floating point 'double' type
outputs[0]['abs_rand'] = float
ProcessRecords
#Copy all fields from input 'inputs[0]' to the
#corresponding output fields in output 'outputs[0]'.
#Copies the values of the fields that have been setup
#in the mapping defined in the ConfigureFields property
outputs[0] += inputs[0]
outputs[0]['abs_rand'] = abs(inputs[0]['rand'])
math.ceil(x)
Output the values of the IOU field as long data types, and round up the values to the nearest integer:
ConfigureFields
#Configure all fields from input 'inputs[0]' to be mapped
#to the corresponding fields on the output 'outputs[0]'
outputs[0] += inputs[0]
# Specify the new field as a long type
outputs[0]['ceil_IOU'] = long
# The math module is not automatically imported
from math import ceil
ProcessRecords
#Copy all fields from input 'inputs[0]' to the
#corresponding output fields in output 'outputs[0]'.
#Copies the values of the fields that have been setup
#in the mapping defined in the ConfigureFields property
outputs[0] += inputs[0]
outputs[0]['ceil_IOU'] = ceil(inputs[0]['IOU'])
float()
Convert numeric values to floating point numbers:
ConfigureFields
#Configure all fields from input 'inputs[0]' to be mapped
#to the corresponding fields on the output 'outputs[0]'
outputs[0] += inputs[0]
# Specify the new field as a floating point 'double' type
outputs[0]['double_rand'] = float
ProcessRecords
#Copy all fields from input 'inputs[0]' to the
#corresponding output fields in output 'outputs[0]'.
#Copies the values of the fields that have been setup
#in the mapping defined in the ConfigureFields property
outputs[0] += inputs[0]
outputs[0]['double_rand'] = float(inputs[0]['rand'])
math.floor(x)
Output the values of the IOU field as long data types, and round down the values to the nearest integer:
ConfigureFields
#Configure all fields from input 'inputs[0]' to be mapped
#to the corresponding fields on the output 'outputs[0]'
outputs[0] += inputs[0]
# Specify the new field as a long type
outputs[0]['floor_IOU'] = long
# The math module is not automatically imported
from math import floor
ProcessRecords
#Copy all fields from input 'inputs[0]' to the
#corresponding output fields in output 'outputs[0]'.
#Copies the values of the fields that have been setup
#in the mapping defined in the ConfigureFields property
outputs[0] += inputs[0]
outputs[0]['floor_IOU'] = floor(inputs[0]['IOU'])
int(x)
Convert values to int data types:
ConfigureFields
#Configure all fields from input 'inputs[0]' to be mapped
#to the corresponding fields on the output 'outputs[0]'
outputs[0] += inputs[0]
# Specify the new field as a floating point 'double' type
outputs[0]['int_IOU'] = int
ProcessRecords
#Copy all fields from input 'inputs[0]' to the
#corresponding output fields in output 'outputs[0]'.
#Copies the values of the fields that have been setup
#in the mapping defined in the ConfigureFields property
outputs[0] += inputs[0]
outputs[0]['int_IOU'] = int(inputs[0]['IOU'])
Custom function
Output a new Boolean field (True/False) which indicates whether a value is a number or not:
ConfigureFields
# Output the fields being tested
outputs[0] += inputs[0]['IOU']
outputs[0] += inputs[0]['junk']
# Specify the new fields as a boolean type
outputs[0]['is_number_IOU'] = bool
outputs[0]['is_number_junk'] = bool
# Define a custom function
def is_number(s):
if s is Null:
return False
else:
try:
float(s)
return True
except ValueError:
return False
ProcessRecords
#Copy all fields from input 'inputs[0]' to the
#corresponding output fields in output 'outputs[0]'.
#Copies the values of the fields that have been setup
#in the mapping defined in the ConfigureFields property
outputs[0] += inputs[0]
outputs[0]['is_number_IOU'] = is_number(inputs[0]['IOU'])
outputs[0]['is_number_junk'] = is_number(inputs[0]['junk'])
long()
Convert values to long data types:
ConfigureFields
#Configure all fields from input 'inputs[0]' to be mapped
#to the corresponding fields on the output 'outputs[0]'
outputs[0] += inputs[0]
# Specify the new field as an long type
outputs[0]['long_rand'] = long
ProcessRecords
#Copy all fields from input 'inputs[0]' to the
#corresponding output fields in output 'outputs[0]'.
#Copies the values of the fields that have been setup
#in the mapping defined in the ConfigureFields property
outputs[0] += inputs[0]
outputs[0]['long_rand'] = long(inputs[0]['rand'])
round(x)
Take the floating point values of the IOU field and round the values to the nearest decimal point:
ConfigureFields
#Configure all fields from input 'inputs[0]' to be mapped
#to the corresponding fields on the output 'outputs[0]'
outputs[0] += inputs[0]
# Specify the new field as a floating point 'double' type
outputs[0]['round_IOU'] = float
ProcessRecords
#Copy all fields from input 'inputs[0]' to the
#corresponding output fields in output 'outputs[0]'.
#Copies the values of the fields that have been setup
#in the mapping defined in the ConfigureFields property
outputs[0] += inputs[0]
# Round to one DP
outputs[0]['round_IOU'] = round(inputs[0]['IOU'], 1)
math.pow()
Create a new field that contains the square value of the records in the id field:
ConfigureFields
#Configure all fields from input 'inputs[0]' to be mapped
#to the corresponding fields on the output 'outputs[0]'
outputs[0] += inputs[0]
# Specify the new field as a floating point 'double' type
outputs[0]['squared_id'] = float
# The math module is not automatically imported
from math import pow
ProcessRecords
#Copy all fields from input 'inputs[0]' to the
#corresponding output fields in output 'outputs[0]'.
#Copies the values of the fields that have been setup
#in the mapping defined in the ConfigureFields property
outputs[0] += inputs[0]
outputs[0]['squared_id'] = pow(inputs[0]['id'], 2)
math.sqrt()
Output the square root of all records in the IOU field:
ConfigureFields
#Configure all fields from input 'inputs[0]' to be mapped
#to the corresponding fields on the output 'outputs[0]'
outputs[0] += inputs[0]
# Specify the new field as a floating point 'double' type
outputs[0]['sqrt_IOU'] = float
# The math module is not automatically imported
from math import sqrt
ProcessRecords
#Copy all fields from input 'inputs[0]' to the
#corresponding output fields in output 'outputs[0]'.
#Copies the values of the fields that have been setup
#in the mapping defined in the ConfigureFields property
outputs[0] += inputs[0]
outputs[0]['sqrt_IOU'] = sqrt(inputs[0]['IOU'])