module testing.test_utils.tests_helper
#
Short summary#
module mlprodict.testing.test_utils.tests_helper
Inspired from sklearn-onnx, handles two backends.
Functions#
function |
truncated documentation |
---|---|
Used to compare decision path. |
|
Compares the processing time several functions. |
|
Runs the appropriate conversion method. |
|
Trains and dumps a model for a binary classification problem. The function trains a model and calls |
|
Saves data with pickle, saves the model with pickle and onnx, runs and saves the predictions for the given model. … |
|
Trains and dumps a model for a binary classification problem. The function trains a model and calls |
|
Trains and dumps a model for a binary classification problem. The function trains a model and calls |
|
Trains and dumps a model for a multi regression problem. The function trains a model and calls |
|
Trains and dumps a model for a One Class outlier problem. The function trains a model and calls |
|
Trains and dumps a model for a regression problem. The function trains a model and calls |
|
Fits a classification model. |
|
Fits a classification model. |
|
Fits a classification model. |
|
Fits a regression model. |
|
Measures the time for a given expression. |
|
Returns a series of repeat time measures for number executions of code assuming fct is a function. |
Documentation#
Inspired from sklearn-onnx, handles two backends.
- mlprodict.testing.test_utils.tests_helper._has_decision_function(model)#
- mlprodict.testing.test_utils.tests_helper._has_predict_proba(model)#
- mlprodict.testing.test_utils.tests_helper._has_transform_model(model)#
- mlprodict.testing.test_utils.tests_helper._raw_score_binary_classification(model, X)#
- mlprodict.testing.test_utils.tests_helper._save_model_dump(model, folder, basename, names)#
- mlprodict.testing.test_utils.tests_helper.binary_array_to_string(mat)#
Used to compare decision path.
- mlprodict.testing.test_utils.tests_helper.compute_benchmark(fcts, number=10, repeat=100)#
Compares the processing time several functions.
- Parameters:
fcts – dictionary
{'name': fct}
number – number of time to run the expression (and then divide by this number to get an average)
repeat – number of times to repeat the computation of the above average
- Returns:
list of [{‘name’: name, ‘time’: …}]
- mlprodict.testing.test_utils.tests_helper.convert_model(model, name, input_types)#
Runs the appropriate conversion method.
- Parameters:
model – model, scikit-learn, keras, or coremltools object
name – model name
input_types – input types
- Returns:
onnx model
- mlprodict.testing.test_utils.tests_helper.dump_binary_classification(model, suffix='', folder=None, allow_failure=None, comparable_outputs=None, verbose=False, label_string=False, benchmark=False, methods=None, nrows=None)#
Trains and dumps a model for a binary classification problem. The function trains a model and calls
dump_data_and_model
.Every created filename will follow the pattern:
<folder>/<prefix><task><classifier-name><suffix>.<data|expected|model|onnx>.<pkl|onnx>
.
- mlprodict.testing.test_utils.tests_helper.dump_data_and_model(data, model, onnx_model=None, basename='model', folder=None, inputs=None, backend=('python', 'onnxruntime'), context=None, allow_failure=None, methods=None, dump_error_log=None, benchmark=None, comparable_outputs=None, intermediate_steps=False, fail_evenif_notimplemented=False, verbose=False, classes=None, check_error=None, disable_optimisation=False)#
Saves data with pickle, saves the model with pickle and onnx, runs and saves the predictions for the given model. This function is used to test a backend (runtime) for onnx.
- Parameters:
data – any kind of data
model – any model
onnx_model – onnx model or None to use an onnx converters to convert it only if the model accepts one float vector
basename – three files are writen
<basename>.data.pkl
,<basename>.model.pkl
,<basename>.model.onnx
folder – files are written in this folder, it is created if it does not exist, if folder is None, it looks first in environment variable
ONNXTESTDUMP
, otherwise, it is placed into'temp_dump'
.inputs – standard type or specific one if specified, only used is parameter onnx is None
backend – backend used to compare expected output and runtime output. Two options are currently supported: None for no test, ‘onnxruntime’ to use module onnxruntime,
python
to use the python runtiume.context – used if the model contains a custom operator such as a custom Keras function…
allow_failure – None to raise an exception if comparison fails for the backends, otherwise a string which is then evaluated to check whether or not the test can fail, example:
"StrictVersion(onnx.__version__) < StrictVersion('1.3.0')"
dump_error_log –
if True, dumps any error message in a file
<basename>.err
, if it is None, it checks the environmentvariable
ONNXTESTDUMPERROR
benchmark – if True, runs a benchmark and stores the results into a file
<basename>.bench
, if None, it checks the environment variableONNXTESTBENCHMARK
verbose – additional information
methods – ONNX may produce one or several results, each of them is equivalent to the output of a method from the model class, this parameter defines which methods is equivalent to ONNX outputs. If not specified, it falls back into a default behaviour implemented for classifiers, regressors, clustering.
comparable_outputs – compares only these outputs
intermediate_steps – displays intermediate steps in case of an error
fail_evenif_notimplemented – the test is considered as failing even if the error is due to onnxuntime missing the implementation of a new operator defiend in ONNX.
classes – classes names (only for classifier, mandatory if option ‘nocl’ is used)
check_error – do not raise an exception if the error message contains this text
disable_optimisation – disable all optimisations onnxruntime could do
- Returns:
the created files
Some convention for the name, Bin for a binary classifier, Mcl for a multiclass classifier, Reg for a regressor, MRg for a multi-regressor. The name can contain some flags. Expected outputs refer to the outputs computed with the original library, computed outputs refer to the outputs computed with a ONNX runtime.
-CannotLoad
: the model can be converted but the runtime cannot load it-Dec3
: compares expected and computed outputs up to 3 decimals (5 by default)-Dec4
: compares expected and computed outputs up to 4 decimals (5 by default)-NoProb
: The original models computed probabilites for two classes size=(N, 2) but the runtime produces a vector of size N, the test will compare the second column to the column-Out0
: only compares the first output on both sides-Reshape
: merges all outputs into one single vector and resizes it before comparing-SkipDim1
: before comparing expected and computed output, arrays with a shape like (2, 1, 2) becomes (2, 2)-SklCol
: scikit-learn operator applies on a column and not a matrix
If the backend is not None, the function either raises an exception if the comparison between the expected outputs and the backend outputs fails or it saves the backend output and adds it to the results.
- mlprodict.testing.test_utils.tests_helper.dump_multilabel_classification(model, suffix='', folder=None, allow_failure=None, verbose=False, label_string=False, first_class=0, comparable_outputs=None, benchmark=False, backend=('python', 'onnxruntime'))#
Trains and dumps a model for a binary classification problem. The function trains a model and calls
dump_data_and_model
.Every created filename will follow the pattern:
<folder>/<prefix><task><classifier-name><suffix>.<data|expected|model|onnx>.<pkl|onnx>
.
- mlprodict.testing.test_utils.tests_helper.dump_multiple_classification(model, suffix='', folder=None, allow_failure=None, verbose=False, label_string=False, first_class=0, comparable_outputs=None, benchmark=False, methods=None)#
Trains and dumps a model for a binary classification problem. The function trains a model and calls
dump_data_and_model
.Every created filename will follow the pattern:
<folder>/<prefix><task><classifier-name><suffix>.<data|expected|model|onnx>.<pkl|onnx>
.
- mlprodict.testing.test_utils.tests_helper.dump_multiple_regression(model, suffix='', folder=None, allow_failure=None, comparable_outputs=None, verbose=False, benchmark=False)#
Trains and dumps a model for a multi regression problem. The function trains a model and calls
dump_data_and_model
.Every created filename will follow the pattern:
<folder>/<prefix><task><classifier-name><suffix>.<data|expected|model|onnx>.<pkl|onnx>
.
- mlprodict.testing.test_utils.tests_helper.dump_one_class_classification(model, suffix='', folder=None, allow_failure=None, comparable_outputs=None, verbose=False, benchmark=False, methods=None)#
Trains and dumps a model for a One Class outlier problem. The function trains a model and calls
dump_data_and_model
.Every created filename will follow the pattern:
<folder>/<prefix><task><classifier-name><suffix>.<data|expected|model|onnx>.<pkl|onnx>
.
- mlprodict.testing.test_utils.tests_helper.dump_single_regression(model, suffix='', folder=None, allow_failure=None, comparable_outputs=None, benchmark=False)#
Trains and dumps a model for a regression problem. The function trains a model and calls
dump_data_and_model
.Every created filename will follow the pattern:
<folder>/<prefix><task><classifier-name><suffix>.<data|expected|model|onnx>.<pkl|onnx>
.
- mlprodict.testing.test_utils.tests_helper.fit_classification_model(model, n_classes, is_int=False, pos_features=False, label_string=False, random_state=42, is_bool=False, n_features=20)#
Fits a classification model.
- mlprodict.testing.test_utils.tests_helper.fit_classification_model_simple(model, n_classes, is_int=False, pos_features=False)#
Fits a classification model.
- mlprodict.testing.test_utils.tests_helper.fit_multilabel_classification_model(model, n_classes=5, n_labels=2, n_samples=400, n_features=20, is_int=False)#
Fits a classification model.
- mlprodict.testing.test_utils.tests_helper.fit_regression_model(model, is_int=False, n_targets=1, is_bool=False, factor=1.0, n_features=10, n_samples=500, n_informative=10)#
Fits a regression model.
- mlprodict.testing.test_utils.tests_helper.timeexec(fct, number, repeat)#
Measures the time for a given expression.
- Parameters:
fct – function to measure (as a string)
number – number of time to run the expression (and then divide by this number to get an average)
repeat – number of times to repeat the computation of the above average
- Returns:
dictionary
- mlprodict.testing.test_utils.tests_helper.timeit_repeat(fct, number, repeat)#
Returns a series of repeat time measures for number executions of code assuming fct is a function.