<commit_before>from classifierBase import *

class ClassifierRandomForest(ClassifierBase):
    #human readable information
    name = "RandomForest classifier New" 
    description = "Basic RandomForest classifier with extensions"
    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):
        ClassifierBase.__init__(self)
        self.treeCount = treeCount
        

    def train(self, features, labels, isInteractive):
        if features is None:
            return
        if features.shape[0] != labels.shape[0]:
            print " 3, 2 ,1 ... BOOOM!! #features != # labels"
        if not labels.dtype == numpy.uint32:
            labels = labels.astype(numpy.uint32)
        if not features.dtype == numpy.float32:
            features = features.astype(numpy.float32)

        if labels.ndim == 1:
            labels.shape = labels.shape + (1,)
        
        self.unique_vals = numpy.unique(labels)
        
        # Have to set this becauce the new rf dont set mtry properly by default
        mtry = max(1,int(numpy.sqrt(features.shape[1]))+1) 
        
        self.RF = vigra.learning.RandomForest(treeCount=self.treeCount)
        
        oob = self.RF.learnRF(features, labels)
        print "Out-of-bag error %f" % oob
        
    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<commit_msg>fix some classifierRandomForest bugs<commit_after>from classifierBase import *

class ClassifierRandomForest(ClassifierBase):
    #human readable information
    name = "RandomForest classifier New" 
    description = "Basic RandomForest classifier with extensions"
    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):
        ClassifierBase.__init__(self)
        self.treeCount = treeCount
        

    def train(self, features, labels, isInteractive):
        self.RF = None
        if features is None:
            return
        if features.shape[0] != labels.shape[0]:
            # #features != # labels"
            return
        if not labels.dtype == numpy.uint32:
            labels = labels.astype(numpy.uint32)
        if not features.dtype == numpy.float32:
            features = features.astype(numpy.float32)

        if labels.ndim == 1:
            labels.shape = labels.shape + (1,)
        
        self.unique_vals = numpy.unique(labels)
        
        # Have to set this becauce the new rf dont set mtry properly by default
        mtry = max(1,int(numpy.sqrt(features.shape[1]))+1) 
        
        self.RF = vigra.learning.RandomForest(treeCount=self.treeCount)
        
        oob = self.RF.learnRF(features, labels)
        print "Out-of-bag error %f" % oob
        
    def predict(self, features):
        #3d: check that only 1D data arrives here
        if self.RF is not None and features is not None and len(self.unique_vals) > 1:
            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
{'fraction_non_alphanumeric': 0.08591235454320377, 'fraction_numerical': 0.015350334241148799, 'mean_word_length': 2.2345876701361087, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 4, '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': '5825094', 'n_tokens_mistral': 1185, 'n_tokens_neox': 1128, 'n_words': 335}