Generated reference › Custom Python Model Predict — Machine Learning/Python Models
kind: generated#block#machine-learning-python-models

Custom Python Model Predict — Machine Learning/Python Models

.py

Machine_Learning/Python_Models/Custom_Python_Model_Predict · 1 input / 1 output port(s) at insert · exports to Python

Description#

The block's own DESCRIPTION_HTML, rendered verbatim — the same text the config dialog's info panel and the library navigator show. Fix a wrong sentence in the block's .cpp (R-D9), never here.

Custom Python Model Predict

Machine Learning / Python Models

Runs a model you load in Python, once per sample: [y1, y2, …] = predict(model, [u1, u2, …]), where model = load_model(arguments) comes from a Python module you name. It follows the contract of MATLAB’s Custom Python Model Predict block, so the same module runs in both.

The module

  • load_model(...) – called once when the run starts, with the Load Model Arguments; returns the model.
  • predict(model, inputs) – called once per sample with the list of input arrays; returns a list with one array per output port.
  • reset_model(model) – optional; see Notes.

Ports

  • Inputs – u1, u2, … in port order, one array each in predict’s list: cast to that input’s entry of Input Data Types and brought to its entry of Input Ranks. A vector (a column or a row) of n is a 1-D array of n, as a Simulink 1-D signal is; an m×n matrix is an (m, n) array. One input by default; the count is user-editable.
  • Outputs – y1, y2, … in the order predict returns them: a single value is [1,1], a 1-D array of k is [k,1], a 2-D array keeps its shape. Numbers and booleans only. One output by default; the count is user-editable and must match what predict returns.

Parameters

  • Python Module Path – the .py file defining load_model and predict. Its folder is put on Python’s path, so it may import files beside it. Empty runs a built-in example whose predict returns every input times a gain (2 unless load_model is given one), so it needs as many outputs as inputs. A relative path is read from the directory the app was started in.
  • Load Model Arguments – what load_model is called with, comma-separated, as in MATLAB’s block: a number, Inf, NaN, true or false, or a bracketed matrix of numbers ([2 3] is a 1-D array, [1;2] and [1 2;3 4] are 2-D, [] is empty), crosses as a number or a numpy array; anything else crosses as its own text. MATLAB evaluates wider numeric expressions (pi, 1:3, 2*3): write those as their values here. Empty calls load_model with no arguments.
  • Input Data Types – one numpy type per input, comma-separated: float32 (the default, and what an empty entry means), float64, float16, float, int8, int16, int32, int64, int, uint8, uint16, uint32 or uint64. A shorter list is padded with float32. The cast rounds (float32) or truncates (the integer types).
  • Input Ranks – one rank per input, comma-separated: inherit (the default, and what an empty entry means) – 1 for a vector or a scalar, 2 for a matrix – or a whole number, reached by adding or removing TRAILING dimensions of size 1 (so a vector at rank 2 is an (n, 1) column). A shorter list is padded with inherit.
  • Preprocessing File – optional: a Python file defining preprocess(model, inputs), which receives the input list and returns the list predict gets.
  • Postprocessing File – optional: a Python file defining postprocess(model, outputs), which receives predict’s list and returns the list the outputs take.
  • Sampling Time (s) – zero or less inherits the solver's rate; a positive value runs the block at that period.

Code export

Python only – the generated module imports the same Python module and calls it exactly as the simulation does. The other nine targets (MATLAB, Java, Rust, C, C++, VHDL, Verilog, SystemVerilog, PLC Structured Text) are not supported: none can carry a Python interpreter, so an export to any of them fails loudly for this block.

Simulink bridge

Both directions, to statsPycoex/Custom Python Model Predict. Python Module Path ↔ PythonModulePath, Load Model Arguments ↔ LoadModelArgs, Preprocessing File ↔ PreprocessingFilePath, Postprocessing File ↔ PostprocessingFilePath. The port counts, Input Data Types and Input Ranks cross together as InputTable and OutputTable, one row per port. Input and output permutations and variable-size outputs have no setting here and are reported, not crossed. A block on the built-in example has no module to name and is reported on export. Sampling Time (s) → SampleTime, as on every block.

Notes

  • Discrete by nature: the model is called once per sample, never integrated.
  • Before the first sample, as in MATLAB’s block, predict runs ONCE on inputs of all ones – the output sizes are taken from it and must not change during the run – and then the module’s reset_model(model) runs, if it has one. A model that keeps state should reset it there; without reset_model that first call’s effect stays.
  • Needs the Python runtime and numpy; a module that fails to load or to predict is reported once, with the block’s path, and stops the run.

Code facts#

FactValue
registered typeMachine_Learning/Python_Models/Custom_Python_Model_Predict
familyMachine_Learning/Python_Models
solver environment classICoreBlock_0_Machine_Learning_1_Python_Models_2_Custom_Python_Model_Predict
sourcesrc/ICoreBlocks/ICoreBlockLibrary/Blocks/Machine_Learning/Python_Models/Custom_Python_Model_Predict/ICoreBlock_0_Machine_Learning_1_Python_Models_2_Custom_Python_Model_Predict.cpp
headersrc/ICoreBlocks/ICoreBlockLibrary/Blocks/Machine_Learning/Python_Models/Custom_Python_Model_Predict/ICoreBlock_0_Machine_Learning_1_Python_Models_2_Custom_Python_Model_Predict.h
default size on canvas130 × 80 px
ports at insert1 in, 1 out
code generators implementedPython

Ports#

#DirectionSignal typeDescription label
1inICoreDouble—
2outICoreDouble—

Ports the constructor creates. A block whose port list changes with its configuration adds or removes ports at load time; the count above is the one a freshly inserted block has.

Configuration variables#

Config variableDefaultSimulink parameter
Python Module Path—PythonModulePath
Load Model Arguments—LoadModelArgs
Input Data Typesfloat32not crossed
Input Ranksinheritnot crossed
Preprocessing File—PreprocessingFilePath
Postprocessing File—PostprocessingFilePath

Every block also carries Sampling Time (s) from ICoreBlockSolverEnvironment: zero or less inherits the solver's rate, a positive value runs the block at that period.

supportSupport::Both
Simulink pathstatsPycoex/Custom Python Model Predict
port-count rulePortsParam::PycoexCustomTables
SampleTime parameteryes
deliberately not crossedInput Data Types, Input Ranks
ICore configSimulink parameterValue translation
Python Module PathPythonModulePathpasses through
Load Model ArgumentsLoadModelArgspasses through
Preprocessing FilePreprocessingFilePathpasses through
Postprocessing FilePostprocessingFilePathpasses through

Caveat (shown to the user): the port counts, Input Data Types and Input Ranks cross together as InputTable and

Catalog contract: src/ICoreBlocks/ICoreCoder/ICoreCommandSystem/SimulinkBridge/ICoreSimulinkBlockCatalog.h

Description vs code#

The lists agree. check_block_descriptions.py finds no disagreement between the description's Ports, Parameters, Code export and Simulink bridge lists and the code's.

The verdict above is tools/docs/check_block_descriptions.py (P7.1), which compares LISTS. It cannot read a sentence: "stateless" on a block with a state, an initial-value semantic the recursion does not implement, a "not synthesizable" caveat the HDL banner contradicts. That is the agent audit (P7.3) on BLOCK_DESCRIPTION_AUDIT.md, and this tool's green is not a substitute for one.

File banner (developer view)#

The top comment of the block's .cpp — the maths, the realization and the export strategy, addressed to whoever changes it. It must not contradict the description above (P7.5).

Custom Python Model Predict -- a Python module's load_model / predict, sample by sample statsPycoex/Custom Python Model Predict runs any model a Python module can load: the module defines load_model(*args) and predict(model, inputs), and may define reset_model(model). This block does the same in the embedded interpreter, under the contract measured on R2026a and written up in ICorePythonModelSupport -- each input cast to its numpy type (float32 unless set) and brought to its rank ("inherit" is 1 for a vector or a scalar, 2 for a matrix), preprocess / predict / postprocess, and load_model plus one predict on inputs of all ones before the run, then reset_model.

With no module the block runs a built-in example (every input times a gain, 2 unless load_model is given one), so its factory configuration runs.

DISCRETE-ONLY: called once per sample, never integrated. Python-only export.

Sample results#

Custom Python Model Predict — Repeating Sequence Stair: [-2 -1 -0.5 0 0.5 1 2 3], one entry per sampleCustom Python Model Predict — Repeating Sequence Stair: [-2 -1 -0.5 0 0.5 1 2 3], one entry per sample05-2-10123inputoutput
tin ICoreDouble-Out-0out ICoreDouble-Out-0
0-2-4
0.40.51
0.8-2-4
1.20.51
1.6-2-4
20.51
2.4-2-4
2.80.51
3.2-2-4
3.60.51
4-2-4
4.40.51
4.8-2-4
5.20.51

Every 4th of 60 samples, from the table stimulus.

The same rig also ran:

StimulusWhat it isOutput range
impulseImpulse: one sample of 1 at k = 5, 0 elsewhere (Repeating Sequence Stair)0 … 2
rampRamp: slope 1 from t = 00 … 11.8
sineSine Wave: amplitude 1, 2 rad/s, no phase, no bias-2 … 1.999
stepStep: 0 -> 1 at t = 1 s0 … 2

Plotted: table — Repeating Sequence Stair: [-2 -1 -0.5 0 0.5 1 2 3], one entry per sample

Category static · sample time 0.1 · 60 steps · commit c3e68843be2458022bf387235205d5741823eb64 · produced by docsSample --out <folder> --blocks Scikit_Learn_Model_Predict Custom_Python_Model_Predict --steps 60 · data docs/generated/samples/Machine_Learning__Python_Models__Custom_Python_Model_Predict.json · the SVG is generated from those numbers by tools/docs/plot_svg.py, so it is a run and not a drawing (R-D10).