
    0-Ph                         d dl mZ 	 d dlmZ d dlmZ dZn# e$ r dZ G d de          ZY nw xY w G d d	          Z	d
 Z
d ZdS )    )multiscale_basic_features)NotFittedError)RandomForestClassifierTFc                       e Zd ZdS )r   N)__name__
__module____qualname__     e/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/skimage/future/trainable_segmentation.pyr   r      s        r   r   c                   ,    e Zd ZdZddZd Zd Zd ZdS )TrainableSegmentera  Estimator for classifying pixels.

    Parameters
    ----------
    clf : classifier object, optional
        classifier object, exposing a ``fit`` and a ``predict`` method as in
        scikit-learn's API, for example an instance of
        ``RandomForestClassifier`` or ``LogisticRegression`` classifier.
    features_func : function, optional
        function computing features on all pixels of the image, to be passed
        to the classifier. The output should be of shape
        ``(m_features, *labels.shape)``. If None,
        :func:`skimage.feature.multiscale_basic_features` is used.

    Methods
    -------
    compute_features
    fit
    predict
    Nc                     |-t           rt          dd          | _        nt          d          || _        || _        d S )Nd   )n_estimatorsn_jobszOPlease install scikit-learn or pass a classifier instanceto TrainableSegmenter.)has_sklearnr   clfImportErrorfeatures_func)selfr   r   s      r   __init__zTrainableSegmenter.__init__%   sS    ; 1s2NNN!-  
 DH*r   c                 `    | j         t          | _         |                      |          | _        d S )N)r   r   features)r   images     r   compute_featuresz#TrainableSegmenter.compute_features2   s,    %!:D**511r   c                 f    |                      |           t          || j        | j                   dS )a  Train classifier using partially labeled (annotated) image.

        Parameters
        ----------
        image : ndarray
            Input image, which can be grayscale or multichannel, and must have a
            number of dimensions compatible with ``self.features_func``.
        labels : ndarray of ints
            Labeled array of shape compatible with ``image`` (same shape for a
            single-channel image). Labels >= 1 correspond to the training set and
            label 0 to unlabeled pixels to be segmented.
        N)r   fit_segmenterr   r   )r   r   labelss      r   fitzTrainableSegmenter.fit7   s4     	e$$$fdmTX66666r   c                 |    | j         t          | _         |                      |          }t          || j                  S )a  Segment new image using trained internal classifier.

        Parameters
        ----------
        image : ndarray
            Input image, which can be grayscale or multichannel, and must have a
            number of dimensions compatible with ``self.features_func``.

        Raises
        ------
        NotFittedError if ``self.clf`` has not been fitted yet (use ``self.fit``).
        )r   r   predict_segmenterr   )r   r   r   s      r   predictzTrainableSegmenter.predictG   s:     %!:D%%e,, 48444r   )NN)r   r   r	   __doc__r   r   r!   r$   r
   r   r   r   r      s_         *+ + + +2 2 2
7 7 7 5 5 5 5 5r   r   c                     | dk    }||         }| |                                          }|                    ||           |S )a_  Segmentation using labeled parts of the image and a classifier.

    Parameters
    ----------
    labels : ndarray of ints
        Image of labels. Labels >= 1 correspond to the training set and
        label 0 to unlabeled pixels to be segmented.
    features : ndarray
        Array of features, with the first dimension corresponding to the number
        of features, and the other dimensions correspond to ``labels.shape``.
    clf : classifier object
        classifier object, exposing a ``fit`` and a ``predict`` method as in
        scikit-learn's API, for example an instance of
        ``RandomForestClassifier`` or ``LogisticRegression`` classifier.

    Returns
    -------
    clf : classifier object
        classifier trained on ``labels``

    Raises
    ------
    NotFittedError if ``self.clf`` has not been fitted yet (use ``self.fit``).
    r   )ravelr!   )r    r   r   masktraining_datatraining_labelss         r   r   r   Z   sD    2 A:DTNMTl((**OGGM?+++Jr   c                    | j         }| j        dk    r|                     d|d         f          } 	 |                    |           }nb# t          $ r t	          d          t
          $ r=}|j        r/d|j        d         v r t          |j        d         dz   dz             |d}~ww xY w|                    |dd                   }|S )	a  Segmentation of images using a pretrained classifier.

    Parameters
    ----------
    features : ndarray
        Array of features, with the last dimension corresponding to the number
        of features, and the other dimensions are compatible with the shape of
        the image to segment, or a flattened image.
    clf : classifier object
        trained classifier object, exposing a ``predict`` method as in
        scikit-learn's API, for example an instance of
        ``RandomForestClassifier`` or ``LogisticRegression`` classifier. The
        classifier must be already trained, for example with
        :func:`skimage.future.fit_segmenter`.

    Returns
    -------
    output : ndarray
        Labeled array, built from the prediction of the classifier.
       r   zWYou must train the classifier `clf` firstfor example with the `fit_segmenter` function.z#x must consist of vectors of lengthr   
zLMaybe you did not use the same type of features for training the classifier.N)shapendimreshaper$   r   
ValueErrorargs)r   r   shpredicted_labelserroutputs         r   r#   r#   z   s   * 
B}q##RBL11;;x00 
 
 
=
 
 	
    8 	=!LL`a   I %%b"g..FMs   A "B&)8B!!B&N)skimage.featurer   sklearn.exceptionsr   sklearn.ensembler   r   r   	Exceptionr   r   r#   r
   r   r   <module>r;      s    5 5 5 5 5 5	111111777777KK   K         H5 H5 H5 H5 H5 H5 H5 H5V  @* * * * *s    //