classifier.py 3.23 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
#!/usr/bin/python
# -*- coding: utf-8 -*-
#
"""
    Abstract class for classifiers.
    
    Name: classifier.py
    Author: Alessandro dos Santos Ferreira ( santosferreira.alessandro@gmail.com )
"""

11 12
from interface.interface import InterfaceException as IException

13 14 15
from abc import ABCMeta, abstractmethod

class Classifier(object):
16
    """Abstract class for classifiers algorithms."""
17 18 19 20
    
    __metaclass__ = ABCMeta
    
    def get_name(self):
21 22 23 24 25 26 27
        """Return the name of class.
        
        Returns
        -------
        name : string
            Returns the name of instantiated class.
        """
28
        return self.__class__.__name__
29 30 31
        
    @staticmethod
    def confusion_matrix(labels, matrix, title = None):
32 33 34 35 36 37 38 39 40 41 42
        """Return a formatted confusion matrix.
        
        Returns
        -------
        labels : list of string
            List of name of classes of confusion matrix.
        matrix : list of list of int
            Matrix of confusion.
        title : string, optional, default = None
            Title of confusion matrix.
        """
43 44 45 46 47 48 49 50 51 52 53 54
        title = "Confusion Matrix" if title is None else "Confusion Matrix " + title

        info = "===  " + title + " ===\n" 
        info += "\t".join(labels) + "\t<-- classified as\n"
        for i in range(0, len(labels)):
            for val in matrix[i]:
                info += str(int(val)) + "\t"
            info += "| %s\n" % (labels[i])
        
        info += "\n\n"
        
        return info
55 56 57
    
    @abstractmethod
    def get_config(self):
58 59 60
        """Return configuration of classifier. 
        Implement this method to extend this class with a new classifier algorithm.
        """
61 62 63 64
        pass
    
    @abstractmethod
    def set_config(self, configs):
65 66 67
        """Update configuration of classifier. 
        Implement this method to extend this class with a new classifier algorithm.
        """
68 69 70 71
        pass
    
    @abstractmethod
    def get_summary_config(self):
72 73 74
        """Return fomatted summary of configuration. 
        Implement this method to extend this class with a new classifier algorithm.
        """
75 76 77
        pass
    
    def must_train(self):
78 79
        """Return if classifier must be trained. 
        """
80
        return False
Gabriel Kirsten's avatar
 
Gabriel Kirsten committed
81 82 83 84 85 86

    def must_extract_features(self):
        """Return if classifier must be extracted features. 
        """
        return True

87
    def train(self, dataset, training_data, force = False):
88 89
        """Perform the training of classifier. 
        """
90 91 92
        pass

    @abstractmethod
93
    def classify(self, dataset, test_dir = None, test_data = None, image = None):
94 95 96
        """Perform the classification. 
        Implement this method to extend this class with a new classifier algorithm.
        """
97
        pass
98 99
    
    def cross_validate(self, detail = True):
100 101
        """Perform cross validation using trained data.
        """
102
        raise IException("Method not available for this classifier")
103 104
    
    def experimenter(self):
105 106
        """Perform a test using all classifiers available. 
        """
107
        raise IException("Method not available for this classifier")
108 109

    def reset(self):
110 111 112
        """Clean all data of classification. 
        Implement this method to extend this class with a new classifier algorithm.
        """
113
        pass