Test In Colab

User Guide Tutorial 01: Plugins

This tutorial shows how to load TemporAI estimators (a.k.a. models), which are Plugins.

Loading a Plugin

All estimators (a.k.a. models) in TemporAI are implemented as Plugins, for ease of extending the library.

Each Plugin has two plugin-specific attributes: its name and category.

You can load a plugin in two ways: * From python module (file), * From API.

From its python module (file):

[ ]:
# Example of loading the prediction.one_off.classification.nn_classifier plugin from the module:

from tempor.methods.prediction.one_off.classification.plugin_nn_classifier import NeuralNetClassifier

nn_classifier = NeuralNetClassifier(n_iter=100)

print(f"Plugin class:\n{NeuralNetClassifier}\n")
print(f"Plugin instance:\n{nn_classifier}")
Plugin class:
<class 'tempor.methods.prediction.one_off.classification.plugin_nn_classifier.NeuralNetClassifier'>

Plugin instance:
NeuralNetClassifier(
    name='nn_classifier',
    category='prediction.one_off.classification',
    plugin_type='method',
    params={
        'n_static_units_hidden': 100,
        'n_static_layers_hidden': 2,
        'n_temporal_units_hidden': 102,
        'n_temporal_layers_hidden': 2,
        'n_iter': 100,
        'mode': 'RNN',
        'n_iter_print': 10,
        'batch_size': 100,
        'lr': 0.001,
        'weight_decay': 0.001,
        'window_size': 1,
        'device': None,
        'dataloader_sampler': None,
        'dropout': 0,
        'nonlin': 'relu',
        'random_state': 0,
        'clipping_value': 1,
        'patience': 20,
        'train_ratio': 0.8
    }
)

Or from the plugin API, as below.

Note the tempor.methods.plugin_loader object - this allows loading plugins by API.

[ ]:
from tempor import plugin_loader

# ^ Import the `plugin_loader`.

nn_classifier_cls = plugin_loader.get_class("prediction.one_off.classification.nn_classifier")
# ^ Get the plugin class from API by the fully-qualified plugin name.
#   The fully-qualified plugin name is "<PLUGIN CATEGORY>.<PLUGIN NAME>".

nn_classifier = nn_classifier_cls(n_iter=100)

print(f"Plugin class:\n{nn_classifier_cls}\n")
print(f"Plugin instance:\n{nn_classifier}")
Plugin class:
<class 'tempor.methods.prediction.one_off.classification.plugin_nn_classifier.NeuralNetClassifier'>

Plugin instance:
NeuralNetClassifier(
    name='nn_classifier',
    category='prediction.one_off.classification',
    plugin_type='method',
    params={
        'n_static_units_hidden': 100,
        'n_static_layers_hidden': 2,
        'n_temporal_units_hidden': 102,
        'n_temporal_layers_hidden': 2,
        'n_iter': 100,
        'mode': 'RNN',
        'n_iter_print': 10,
        'batch_size': 100,
        'lr': 0.001,
        'weight_decay': 0.001,
        'window_size': 1,
        'device': None,
        'dataloader_sampler': None,
        'dropout': 0,
        'nonlin': 'relu',
        'random_state': 0,
        'clipping_value': 1,
        'patience': 20,
        'train_ratio': 0.8
    }
)

You can also get the plugin instance directly (rather than the class) from the API, as follows:

Listing all available Plugins

You can list all Plugins currently available in TemporAI as follows:

[ ]:
from tempor import plugin_loader

# Use plugin_loader.list():
all_plugins = plugin_loader.list()

# Displaying using pretty print here for clarity:
from rich.pretty import pprint

pprint(all_plugins, indent_guides=False)
{
    'prediction': {
        'one_off': {
            'classification': ['cde_classifier', 'ode_classifier', 'nn_classifier', 'laplace_ode_classifier'],
            'regression': ['laplace_ode_regressor', 'nn_regressor', 'ode_regressor', 'cde_regressor']
        },
        'temporal': {'classification': ['seq2seq_classifier'], 'regression': ['seq2seq_regressor']}
    },
    'preprocessing': {
        'encoding': {'static': ['static_onehot_encoder'], 'temporal': ['ts_onehot_encoder']},
        'imputation': {
            'static': ['static_tabular_imputer'],
            'temporal': ['ffill', 'ts_tabular_imputer', 'bfill']
        },
        'nop': ['nop_transformer'],
        'scaling': {
            'static': ['static_minmax_scaler', 'static_standard_scaler'],
            'temporal': ['ts_minmax_scaler', 'ts_standard_scaler']
        }
    },
    'time_to_event': ['ts_coxph', 'ts_xgb', 'dynamic_deephit'],
    'treatments': {
        'one_off': {'regression': ['synctwin_regressor']},
        'temporal': {'classification': ['crn_classifier'], 'regression': ['crn_regressor']}
    }
}

Note that plugin categories are hierarchical (nested).

To quickly view the plugin fully qualified name (fqn) for all plugins, use plugin_loader.list_full_names().

These are the names to use in plugin_loader.{get,get_class} calls.

[ ]:
plugin_loader.list_full_names()
['prediction.one_off.classification.cde_classifier',
 'prediction.one_off.classification.ode_classifier',
 'prediction.one_off.classification.nn_classifier',
 'prediction.one_off.classification.laplace_ode_classifier',
 'prediction.one_off.regression.laplace_ode_regressor',
 'prediction.one_off.regression.nn_regressor',
 'prediction.one_off.regression.ode_regressor',
 'prediction.one_off.regression.cde_regressor',
 'prediction.temporal.classification.seq2seq_classifier',
 'prediction.temporal.regression.seq2seq_regressor',
 'preprocessing.encoding.static.static_onehot_encoder',
 'preprocessing.encoding.temporal.ts_onehot_encoder',
 'preprocessing.imputation.static.static_tabular_imputer',
 'preprocessing.imputation.temporal.ffill',
 'preprocessing.imputation.temporal.ts_tabular_imputer',
 'preprocessing.imputation.temporal.bfill',
 'preprocessing.nop.nop_transformer',
 'preprocessing.scaling.static.static_minmax_scaler',
 'preprocessing.scaling.static.static_standard_scaler',
 'preprocessing.scaling.temporal.ts_minmax_scaler',
 'preprocessing.scaling.temporal.ts_standard_scaler',
 'time_to_event.ts_coxph',
 'time_to_event.ts_xgb',
 'time_to_event.dynamic_deephit',
 'treatments.one_off.regression.synctwin_regressor',
 'treatments.temporal.classification.crn_classifier',
 'treatments.temporal.regression.crn_regressor']

A note on Plugin types

All the Plugins so far have been of the "method" plugin type. This is the default plugin type. You could in fact have passed plugin_type="method" to the methods above to achieve the same result, e.g.:

plugin_loader.list(plugin_type="method")

TemporAI has multiple plugin types: * "method": algorithms/methods, which can fit(). predict() etc. * "datasource": a data source, which returns a specific dataset when calling load().

You can list plugin types as below.

[ ]:
plugin_loader.list_plugin_types()
['datasource', 'method', 'metric']

You can list all plugins across all the different plugin types by passing plugin_type="all".

[ ]:
from rich.pretty import pprint

plugins_across_all_types = plugin_loader.list(plugin_type="all")

pprint(plugins_across_all_types, indent_guides=False)
{
    'datasource': {
        'prediction': {'one_off': ['sine', 'google_stocks'], 'temporal': ['uci_diabetes', 'dummy_prediction']},
        'time_to_event': ['pbc'],
        'treatments': {'one_off': ['pkpd'], 'temporal': ['dummy_treatments']}
    },
    'method': {
        'prediction': {
            'one_off': {
                'classification': ['cde_classifier', 'ode_classifier', 'nn_classifier', 'laplace_ode_classifier'],
                'regression': ['laplace_ode_regressor', 'nn_regressor', 'ode_regressor', 'cde_regressor']
            },
            'temporal': {'classification': ['seq2seq_classifier'], 'regression': ['seq2seq_regressor']}
        },
        'preprocessing': {
            'encoding': {'static': ['static_onehot_encoder'], 'temporal': ['ts_onehot_encoder']},
            'imputation': {
                'static': ['static_tabular_imputer'],
                'temporal': ['ffill', 'ts_tabular_imputer', 'bfill']
            },
            'nop': ['nop_transformer'],
            'scaling': {
                'static': ['static_minmax_scaler', 'static_standard_scaler'],
                'temporal': ['ts_minmax_scaler', 'ts_standard_scaler']
            }
        },
        'time_to_event': ['ts_coxph', 'ts_xgb', 'dynamic_deephit'],
        'treatments': {
            'one_off': {'regression': ['synctwin_regressor']},
            'temporal': {'classification': ['crn_classifier'], 'regression': ['crn_regressor']}
        }
    },
    'metric': {
        'prediction': {
            'one_off': {
                'regression': ['mse', 'mae', 'r2'],
                'classification': [
                    'accuracy',
                    'f1_score_micro',
                    'f1_score_macro',
                    'f1_score_weighted',
                    'kappa',
                    'kappa_quadratic',
                    'recall_micro',
                    'recall_macro',
                    'recall_weighted',
                    'precision_micro',
                    'precision_macro',
                    'precision_weighted',
                    'mcc',
                    'aucprc',
                    'aucroc'
                ]
            }
        },
        'time_to_event': ['c_index', 'brier_score']
    }
}