
    i[*                         S r SSKJr  SSKJr  SSKJr  SSKJr	  SSK
JrJrJrJr   " S	 S
5      r " S S\5      r " S S\5      rg)z"
Pipeline utils for this package.
    )proto   )SPECIFICATION_VERSION   )_feature_management)model)set_classifier_interface_paramsset_regressor_interface_paramsset_training_featuresset_transform_interface_paramsc                   @    \ rS rSrSrSS jrS rS rS rS r	S	 r
S
rg)Pipeline   a  
A pipeline model that exposes a sequence of models as a single model,
It requires a set of inputs, a sequence of other models and a set of outputs.

This class is the base class for :py:class:`PipelineClassifier` and
:py:class:`PipelineRegressor`, which contain a sequence ending in a classifier
or regressor and themselves behave like a classifier or regressor.  This class
may be used directly for a sequence of feature transformer objects.

Nc                     [         R                  R                  5       n[        Ul        UR
                    [        XAX#5      nX@l        g)a  
Create a pipeline of models to be executed sequentially.

Parameters
----------

input_features: [list of 2-tuples]
    Name(s) of the input features, given as a list of `('name', datatype)`
    tuples.  The datatypes entry can be any of the data types defined in the
    :py:mod:`models.datatypes` module.

output_features: [list of features]
    Name(s) of the output features, given as a list of
    `('name',datatype)` tuples.  The datatypes entry can be any of the
    data types defined in the :py:mod:`models.datatypes` module.  All features
    must be either defined in the inputs or be produced by one of the
    contained models.

N)_proto	Model_pb2Model_SPECIFICATION_VERSIONspecificationVersionpipeliner   specselfinput_featuresoutput_featurestraining_featuresr   s        Z/var/www/html/ai-image-ml/venv/lib/python3.13/site-packages/coremltools/models/pipeline.py__init__Pipeline.__init__#   s?    ( %%'$:! 	-/

 	    c                 <    UR                   (       a  [        S5      eg )NzNNew sub-models cannot be added after the pipeline has been marked as updatable)isUpdatable
ValueError)r   r   s     r   )_validate_updatable_pipeline_on_add_model2Pipeline._validate_updatable_pipeline_on_add_modelD   s     `  r    c                    U R                  U R                  5        [        U[        R                  5      (       a  UR
                  nU R                  R                  nUR                  R                  5       nUR                  U5        ga=  
Add a protobuf spec or :py:class:`models.MLModel` instance to the pipeline.

All input features of this model must either match the input_features
of the pipeline, or match the outputs of a previous model.

Parameters
----------
spec: [MLModel, Model_pb2]
    A protobuf spec or MLModel instance containing a model.
N)
r$   r   
isinstance_modelMLModel_specr   modelsaddCopyFrom)r   r   r   	step_specs       r   	add_modelPipeline.add_modelJ   s_     	66tyyAdFNN++::D99%%OO'')	4 r    c                    [        UR                  5      nUS:  a  [        S5      eUR                  S S  H  nUR                  (       d  M  [        S5      e   UR                  US-
     nUR                  (       d  [        S5      eSUl        g )Nr   zmPipeline does not seem to have any models. It should be marked as updatable only after adding all sub-models.z>Only the last model can be updatable in an updatable pipeline.zEA pipeline can be made updatable only if the last model is updatable.T)lenr,   r#   r"   )r   r   r   
num_modelsr   
last_models         r   '_validate_sub_models_and_make_updatable0Pipeline._validate_sub_models_and_make_updatable`   s    )
>  __Sb)E    T  * __Z!^4
%%W   r    c                 d    U R                  U R                  R                  U R                  5        g N)r7   r   r   )r   s    r   make_updatablePipeline.make_updatablev   s     44TYY5G5GSr    c                 2    U R                   n[        X!5        gz
Set the training inputs of the network spec.

Parameters
----------
training_input: [tuple]
    List of training input names and type of the network.
Nr   r   r   training_inputr   s      r   set_training_inputPipeline.set_training_inputy        yyd3r    r   r:   )__name__
__module____qualname____firstlineno____doc__r   r$   r0   r7   r;   rB   __static_attributes__ r    r   r   r      s'    	B!, ,T
4r    r   c                   H   ^  \ rS rSrSrSS jrU 4S jrU 4S jrS rSr	U =r
$ )	PipelineRegressor   a  
A pipeline model that exposes a sequence of models as a single model,
It requires a set of inputs, a sequence of other models and a set of outputs.
In this case the pipeline itself behaves as a regression model by designating
a real valued output feature as its 'predicted feature'.
c                     [         R                  R                  5       n[        Ul        UR
                    [        XAX#5      nX@l        g)a  
Create a set of pipeline models given a set of model specs.  The final
output model must be a regression model.

Parameters
----------

input_features: [list of 2-tuples]
    Name(s) of the input features, given as a list of `('name', datatype)`
    tuples.  The datatypes entry can be any of the data types defined in the
    :py:mod:`models.datatypes` module.

output_features: [list of features]
    Name(s) of the output features, given as a list of
    `('name',datatype)` tuples.  The datatypes entry can be any of the
    data types defined in the :py:mod:`models.datatypes` module.  All features
    must be either defined in the inputs or be produced by one of the
    contained models.

N)r   r   r   r   r   pipelineRegressorr
   r   r   s        r   r   PipelineRegressor.__init__   sA    * %%'$:! 	-/

 	r    c                 $  > [         TU ]  U R                  5        [        U[        R
                  5      (       a  UR                  nU R                  R                  R                  nUR                  R                  5       nUR                  U5        gr'   )superr$   r   r(   r)   r*   r+   rQ   r   r,   r-   r.   r   r   r   r/   	__class__s       r   r0   PipelineRegressor.add_model   se     	9$))DdFNN++::D99..77OO'')	4 r    c                 v   > [         TU ]  U R                  R                  R                  U R                  5        g r:   )rT   r7   r   rQ   r   r   rV   s    r   r;    PipelineRegressor.make_updatable   s)    7II''00$))	
r    c                 2    U R                   n[        X!5        gr>   r?   r@   s      r   rB   $PipelineRegressor.set_training_input   rD   r    rE   r:   rF   rG   rH   rI   rJ   r   r0   r;   rB   rK   __classcell__rV   s   @r   rN   rN      s"    B!,


4 
4r    rN   c                   J   ^  \ rS rSrSr SS jrU 4S jrU 4S jrS rSr	U =r
$ )	PipelineClassifier   a)  
A pipeline model that exposes a sequence of models as a single model,
It requires a set of inputs, a sequence of other models and a set of outputs.
In this case the pipeline itself behaves as a classification model by designating
a discrete categorical output feature as its 'predicted feature'.
c                     [         R                  " X25      n[        R                  R	                  5       n[
        Ul        [        UUUSUU5      nUR                    XPl	        g)a  
Create a set of pipeline models given a set of model specs.  The last
model in this list must be a classifier model.

Parameters
----------
input_features: [list of 2-tuples]
    Name(s) of the input features, given as a list of `('name', datatype)`
    tuples.  The datatypes entry can be any of the data types defined in the
    :py:mod:`models.datatypes` module.

class_labels: [list]
    A list of string or integer class labels to use in making predictions.
    This list must match the class labels in the model outputting the categorical
    predictedFeatureName

output_features: [list]
    A string or a list of two strings specifying the names of the two
    output features, the first being a class label corresponding
    to the class with the highest predicted score, and the second being
    a dictionary mapping each class to its score. If `output_features`
    is a string, it specifies the predicted class label and the class
    scores is set to the default value of `"classProbability."`

pipelineClassifierN)
r   .process_or_validate_classifier_output_featuresr   r   r   r   r   r	   rd   r   )r   r   class_labelsr   r   r   s         r   r   PipelineClassifier.__init__   sc    : .\\
 %%'$:!. 
 	 	r    c                 $  > [         TU ]  U R                  5        [        U[        R
                  5      (       a  UR                  nU R                  R                  R                  nUR                  R                  5       nUR                  U5        gr'   )rT   r$   r   r(   r)   r*   r+   rd   r   r,   r-   r.   rU   s       r   r0   PipelineClassifier.add_model  se     	9$))DdFNN++::D99//88OO'')	4 r    c                 ~   > [         [        U ]  U R                  R                  R
                  U R                  5        g r:   )rT   ra   r7   r   rd   r   rY   s    r   r;   !PipelineClassifier.make_updatable&  s,     $OII((11499	
r    c                 2    U R                   n[        X!5        gr>   r?   r@   s      r   rB   %PipelineClassifier.set_training_input+  rD   r    rE   )NNr]   r_   s   @r   ra   ra      s)     UY0d!*


4 
4r    ra   N)rJ   coremltoolsr   r    r   r   r   r   r)   _interface_managementr	   r
   r   r   r   rN   ra   rL   r    r   <module>rq      sL    ( > !  l4 l4^N4 N4b^4 ^4r    