U
    ?h                     @   sx   d dl mZ z d dlmZ d dlmZ dZW n( ek
rT   dZG dd deZY nX G dd	 d	Z	d
d Z
dd ZdS )    )multiscale_basic_features)NotFittedError)RandomForestClassifierTFc                   @   s   e Zd ZdS )r   N)__name__
__module____qualname__ r   r   W/var/www/html/venv/lib/python3.8/site-packages/skimage/future/trainable_segmentation.pyr   
   s   r   c                   @   s2   e Zd ZdZdddZdd Zdd Zd	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                 C   s6   |d kr&t rtddd| _q,tdn|| _|| _d S )Nd   )Zn_estimatorsZn_jobszOPlease install scikit-learn or pass a classifier instanceto TrainableSegmenter.)has_sklearnr   clfImportErrorfeatures_func)selfr   r   r   r   r	   __init__$   s    zTrainableSegmenter.__init__c                 C   s    | j d krt| _ |  || _d S )N)r   r   features)r   imager   r   r	   compute_features1   s    
z#TrainableSegmenter.compute_featuresc                 C   s   |  | 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   labelsr   r   r	   fit6   s    
zTrainableSegmenter.fitc                 C   s&   | j dkr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``).
        N)r   r   predict_segmenterr   )r   r   r   r   r   r	   predictF   s    

zTrainableSegmenter.predict)NN)r   r   r   __doc__r   r   r   r   r   r   r   r	   r
      s
   
r
   c                 C   s,   | 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   )Zravelr   )r   r   r   maskZtraining_dataZtraining_labelsr   r   r	   r   Y   s
    r   c              
   C   s   | j }| jdkr"| d|d f} z|| }W nl tk
rL   tdY nR tk
r } z4|jrd|jd krt|jd d d n|W 5 d}~X Y nX ||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.segmentation.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ndimZreshaper   r   
ValueErrorargs)r   r   shZpredicted_labelserroutputr   r   r	   r   y   s&    
r   N)Zskimage.featurer   Zsklearn.exceptionsr   Zsklearn.ensembler   r   r   	Exceptionr
   r   r   r   r   r   r	   <module>   s   K 