<commit_before>from classifierBase import *

class ClassifierRandomForest(ClassifierBase):
    #human readable information
    name = "RandomForest classifier" 
    description = "Basic RandomForest classifier"
    author = "HCI, University of Heidelberg"
    homepage = "http://hci.iwr.uni-heidelberg.de"

    #minimum required isotropic context
    #0 means pixel based classification
    #-1 means whole dataset
    minContext = 0
    treeCount = 10

    def __init__(self, treeCount = 10):
        self.treeCount = treeCount
        ClassifierBase.__init__(self)

    def train(self, features, labels):
        if features is None:
            return

        if not labels.dtype == numpy.uint32:
            labels = labels.astype(numpy.uint32)
        if not features.dtype == numpy.float32:
            features = features.astype(numpy.float32)

        self.unique_vals = numpy.unique(labels)

        if len(self.unique_vals) > 1:
            # print "Learning RF %d trees: %d labels given, %d classes, and %d features " % (treeCount, features.shape[0], len(numpy.unique(labels)), features.shape[1])
            #self.RF = vigra.learning.RandomForestOld(features, labels, treeCount=self.treeCount)
            self.RF = vigra.learning.RandomForest(treeCount=self.treeCount)
            self.RF.learnRF(features, labels)
        else:
            self.RF = None

        self.labels = labels
        self.features = features

    def predict(self, features):
        #3d: check that only 1D data arrives here
        if self.RF is not None and features is not None:
            if not features.dtype == numpy.float32:
                features = numpy.array(features, dtype=numpy.float32)
            return self.RF.predictProbabilities(features)
        else:
            return None
        
    def serialize(self, fileName, pathInFile):
        # cannot serilaze into grp because can not pass h5py handle to vigra yet
        # works only with new RF version
        self.RF.writeHDF5(fileName, pathInFile, True)

    @classmethod
    def deserialize(cls, fileName, pathInFile):
        classifier = cls()
        classifier.RF = vigra.learning.RandomForest(fileName, pathInFile)
        classifier.treeCount = classifier.RF.treeCount
        return classifier<commit_msg>just changed back to old random forest - we should really get rid of ither one<commit_after>from classifierBase import *

class ClassifierRandomForest(ClassifierBase):
    #human readable information
    name = "RandomForest classifier" 
    description = "Basic RandomForest classifier"
    author = "HCI, University of Heidelberg"
    homepage = "http://hci.iwr.uni-heidelberg.de"

    #minimum required isotropic context
    #0 means pixel based classification
    #-1 means whole dataset
    minContext = 0
    treeCount = 10

    def __init__(self, treeCount = 10):
        self.treeCount = treeCount
        ClassifierBase.__init__(self)

    def train(self, features, labels):
        if features is None:
            return

        if not labels.dtype == numpy.uint32:
            labels = labels.astype(numpy.uint32)
        if not features.dtype == numpy.float32:
            features = features.astype(numpy.float32)

        self.unique_vals = numpy.unique(labels)

        if len(self.unique_vals) > 1:
            # print "Learning RF %d trees: %d labels given, %d classes, and %d features " % (treeCount, features.shape[0], len(numpy.unique(labels)), features.shape[1])
            self.RF = vigra.learning.RandomForestOld(features, labels, treeCount=self.treeCount)
            #self.RF = vigra.learning.RandomForest(treeCount=self.treeCount)
            #self.RF.learnRF(features, labels)
        else:
            self.RF = None

        self.labels = labels
        self.features = features

    def predict(self, features):
        #3d: check that only 1D data arrives here
        if self.RF is not None and features is not None:
            if not features.dtype == numpy.float32:
                features = numpy.array(features, dtype=numpy.float32)
            return self.RF.predictProbabilities(features)
        else:
            return None
        
    def serialize(self, fileName, pathInFile):
        # cannot serilaze into grp because can not pass h5py handle to vigra yet
        # works only with new RF version
        self.RF.writeHDF5(fileName, pathInFile, True)

    @classmethod
    def deserialize(cls, fileName, pathInFile):
        classifier = cls()
        classifier.RF = vigra.learning.RandomForest(fileName, pathInFile)
        classifier.treeCount = classifier.RF.treeCount
        return classifier
{'fraction_non_alphanumeric': 0.08506044905008636, 'fraction_numerical': 0.011226252158894647, 'mean_word_length': 2.52319391634981, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6635023', 'n_tokens_mistral': 1336, 'n_tokens_neox': 1277, 'n_words': 369}