Custom Code

If you have a lack of functionality on the Datrics platform you can make your own custom brick.
There are two ways to make your own brick:
  1. Datrics CLI wich can be installed on your computer. Go here to know how to use it.
  1. Datrics web platform.

Datrics web platform

notion image
Firstly click on Custom Bricks folder at the left sidebar and then click on + new brick then upload tar archive with brick configuration.

Uploading tar archive with brick configuration

Tar archive can be created by Datrics CLI or by console command.
  1. Datrics CLI
    1. datrics create
      datrics push
  1. Console command
    1. tar cvf brick.tar manifest.yml brick_custom_code.py
Upload a tar archive (up to 500 Kbytes) which contains two files: brick_custom_code.py and manifest.yml
Example of brick_custom_code.py:
class BrickCustomCode(Brick):

		def validate_args(self, args):
        return True
    
    def validate_inputs(self):
        if check_number_of_inputs(self, 2):
            if not self.get_input_by_index(0).hasValue():
                self.messages.append('input 0 is empty')
            elif not self.get_input_by_index(1).hasValue():
                self.messages.append('input 1 is empty')

        return len(self.messages) == 0

    def perform_execution(self, ctx: ExecutionContext):
        data1 = self.get_input_by_index(0).value
        data2 = self.get_input_by_index(1).value
        res = pd.concat([data1, data2])
        self.fill_outputs_by_type_with_value('data', res)
        self.status = BrickStatus.SUCCESS

    def perform_light_run(self, ctx: ExecutionContext):
        data1 = self.get_input_by_index(0).value
        data2 = self.get_input_by_index(1).value
        res = pd.concat([data1, data2])
        self.fill_outputs_by_type_with_value('data', res)
Class for all bricks must have the same name - BrickCustomCode and inherit Brick.
Methods validate_args, validate_inputs, perform_execution, perform_light_run are obligatory to implement.
 
manifest.yml helps to make brick description.
Example:
version: 1.0
title: Custom Union Data
inputs:
  - name: data
    type: data
  - name: data
    type: data
outputs:
  - name: data
    type: data
Now we have only one type of inputs and outputs - data. But you can change the name of that input or output.
notion image
 
Import packages. Custom code bricks do not support imports because of security reasons. But we provided list of globals that would be helpful for making bricks:
# Brick
DatricsData
Brick
BrickStatus
BrickOutput
BrickInput
ExecutionContext

# Input handling functions
check_number_of_inputs
check_input_with_type_has_value
check_input_model_has_optional_target_variable
check_input_with_index_has_value
check_column_in_input_of_type

# Argument handling functions
check_argument_exists
check_argument_not_none
check_argument_not_empty
check_argument_mathces_type
check_argument_in_list
check_argument_list_in_list
check_argument_in_boundaries

# Calculate metrics functions
transform_types
calculate_metric_r2
calculate_metric_rmse
calculate_metric_mape
calculate_metric_accuracy
calculate_precision_recall_fscore_support

# ML libs
pd or pandas (can use short name and full)
np or numpy (can use short name and full)
ast
scipy
sklearn

# Additional globals
datetime
math
string
re
undefined