Exemplo n.º 1
0
def Consensusread(dirPath,verbose = 0):
    """Read a Consensus model from disk and return as a ConsensusClassifier instance. """
    # Read data from disk
    #This removes any trailing '/'
    dirPath = os.path.realpath(str(dirPath)) 
    # This assures that all related files will be inside a folder
    try:
        domainFile = dataUtilities.DataTable(os.path.join(dirPath,"trainDomain.tab"))

        #Load the models
        modelFiles = glob.glob(os.path.join(dirPath,'C*.model'))
        if len(modelFiles) < 2:
                if self.verbose > 0: print "ERROR: Missing model files in ",dirPath    
                return None
        else:
                classifiers = []
                for mFile in modelFiles:
                    classifiers.append(AZBaseClasses.modelRead(mFile))
                    if not classifiers[-1]:
                        if self.verbose > 0: print "ERROR: Could not load the model ",mFile
                        return None
    except:
        if verbose > 0: print "ERROR: It was not possible to load the Consensus model"
        return None
    return ConsensusClassifier(classifiers=classifiers ,classVar = domainFile.domain.classVar, verbose = verbose, domain = domainFile.domain)
Exemplo n.º 2
0
def Consensusread(dirPath,verbose = 0):
    """Read a Consensus model from disk and return as a ConsensusClassifier instance. """
    # Read data from disk
    #This removes any trailing '/'
    dirPath = os.path.realpath(str(dirPath)) 
    basicStat = None
    NTrainEx = None
    imputeData = None
    # This assures that all related files will be inside a folder
    try:
        domainFile = dataUtilities.DataTable(os.path.join(dirPath,"trainDomain.tab"))

        #Load the models
        modelFiles = glob.glob(os.path.join(dirPath,'C*.model'))
        if len(modelFiles) < 2:
                if verbose > 0: print "ERROR: Missing model files in ",dirPath    
                return None
        else:
                classifiers = []
                for mFile in modelFiles:
                    classifiers.append(AZBaseClasses.modelRead(mFile))
                    if not classifiers[-1]:
                        if verbose > 0: print "ERROR: Could not load the model ",mFile
                        return None
                    else:
                        #Try to load the imputeData, basicStat and NTrainEx from a model that saved it!
                        if hasattr(classifiers[-1], "basicStat") and classifiers[-1].basicStat and not basicStat:
                            basicStat = classifiers[-1].basicStat
                        if hasattr(classifiers[-1], "NTrainEx") and classifiers[-1].NTrainEx and not NTrainEx:
                            NTrainEx = classifiers[-1].NTrainEx
                        if hasattr(classifiers[-1], "imputeData") and classifiers[-1].imputeData and not imputeData:
                            imputeData = classifiers[-1].imputeData
                            domainFile = imputeData #This is needed for domain compatibilitu betwene imputer and domain var

    except:
        if verbose > 0: print "ERROR: It was not possible to load the Consensus model"
        return None
    return ConsensusClassifier(classifiers=classifiers ,varNames = [attr.name for attr in domainFile.domain.attributes],classVar = domainFile.domain.classVar, verbose = verbose, domain = domainFile.domain, basicStat = basicStat, NTrainEx = NTrainEx, imputeData = imputeData)
Exemplo n.º 3
0
def Consensusread(dirPath, verbose=0):
    """Read a Consensus model from disk and return as a ConsensusClassifier instance. """
    # Read data from disk
    #This removes any trailing '/'
    dirPath = os.path.realpath(str(dirPath))
    basicStat = None
    NTrainEx = None
    imputeData = None
    expression = None
    weights = None
    # This assures that all related files will be inside a folder
    try:
        domainFile = dataUtilities.DataTable(
            os.path.join(dirPath, "trainDomain.tab"))

        learnerFilename = os.path.join(dirPath, 'learnerDict.pkl')
        expressionListFilename = os.path.join(dirPath, 'expressionList.pkl')
        expressionFilename = os.path.join(dirPath, 'expression.pkl')
        weightsFilename = os.path.join(dirPath, 'weights.pkl')

        #Load the models
        modelFiles = glob.glob(os.path.join(dirPath, 'C*.model'))
        modelFiles.sort()
        if len(modelFiles) < 2:
            if verbose > 0: print "ERROR: Missing model files in ", dirPath
            return None
        else:

            if os.path.exists(learnerFilename):
                #
                # We have a custom expression to read
                #

                dictionaryFile = open(learnerFilename, 'rb')
                classifiers = pickle.load(dictionaryFile)
                dictionaryFile.close()

                models = []
                for mFile in modelFiles:
                    models.append(AZBaseClasses.modelRead(mFile))

                for k, v in classifiers.iteritems():
                    classifiers[k] = models[v]

                #Try to load the imputeData, basicStat and NTrainEx from a model that saved it!
                if hasattr(classifiers.itervalues().next(),
                           "basicStat") and classifiers.itervalues().next(
                           ).basicStat and not basicStat:
                    basicStat = classifiers.itervalues().next().basicStat
                if hasattr(classifiers.itervalues().next(),
                           "NTrainEx") and classifiers.itervalues().next(
                           ).NTrainEx and not NTrainEx:
                    NTrainEx = classifiers.itervalues().next().NTrainEx
                if hasattr(classifiers.itervalues().next(),
                           "imputeData") and classifiers.itervalues().next(
                           ).imputeData and not imputeData:
                    imputeData = classifiers.itervalues().next().imputeData
                    domainFile = imputeData  #This is needed for domain compatibility between imputer and domain var

                if os.path.exists(expressionListFilename):
                    file = open(expressionListFilename)
                    expression = pickle.load(file)
                    file.close()
                else:
                    file = open(expressionFilename)
                    expression = pickle.load(file)
                    file.close()

                if os.path.exists(weightsFilename):
                    file = open(weightsFilename)
                    weights = pickle.load(file)
                    file.close()

            else:
                #
                # Default expression to read
                #

                classifiers = []
                for mFile in modelFiles:
                    classifiers.append(AZBaseClasses.modelRead(mFile))

                if not classifiers[-1]:
                    if verbose > 0:
                        print "ERROR: Could not load the model ", mFile
                    return None
                else:
                    #Try to load the imputeData, basicStat and NTrainEx from a model that saved it!
                    if hasattr(
                            classifiers[-1], "basicStat"
                    ) and classifiers[-1].basicStat and not basicStat:
                        basicStat = classifiers[-1].basicStat
                    if hasattr(classifiers[-1], "NTrainEx"
                               ) and classifiers[-1].NTrainEx and not NTrainEx:
                        NTrainEx = classifiers[-1].NTrainEx
                    if hasattr(
                            classifiers[-1], "imputeData"
                    ) and classifiers[-1].imputeData and not imputeData:
                        imputeData = classifiers[-1].imputeData
                        domainFile = imputeData  #This is needed for domain compatibilitu betwene imputer and domain var

    except:
        if verbose > 0:
            print "ERROR: It was not possible to load the Consensus model"
        return None
    return ConsensusClassifier(
        classifiers=classifiers,
        expression=expression,
        weights=weights,
        varNames=[attr.name for attr in domainFile.domain.attributes],
        classVar=domainFile.domain.classVar,
        verbose=verbose,
        domain=domainFile.domain,
        basicStat=basicStat,
        NTrainEx=NTrainEx,
        imputeData=imputeData)
Exemplo n.º 4
0
def Consensusread(dirPath,verbose = 0):
    """Read a Consensus model from disk and return as a ConsensusClassifier instance. """
    # Read data from disk
    #This removes any trailing '/'
    dirPath = os.path.realpath(str(dirPath)) 
    basicStat = None
    NTrainEx = None
    imputeData = None
    expression = None
    weights = None
    # This assures that all related files will be inside a folder
    try:
        domainFile = dataUtilities.DataTable(os.path.join(dirPath, "trainDomain.tab"))
        
        learnerFilename = os.path.join(dirPath, 'learnerDict.pkl')
        expressionListFilename = os.path.join(dirPath, 'expressionList.pkl')
        expressionFilename = os.path.join(dirPath, 'expression.pkl')
        weightsFilename = os.path.join(dirPath, 'weights.pkl')

        #Load the models
        modelFiles = glob.glob(os.path.join(dirPath,'C*.model'))
        modelFiles.sort()
        if len(modelFiles) < 2:
                if verbose > 0: print "ERROR: Missing model files in ",dirPath    
                return None
        else:

            if os.path.exists(learnerFilename):
                #
                # We have a custom expression to read
                #

                dictionaryFile = open(learnerFilename, 'rb')
                classifiers = pickle.load(dictionaryFile)
                dictionaryFile.close()

                models = []
                for mFile in modelFiles:
                    models.append(AZBaseClasses.modelRead(mFile))

                for k, v in classifiers.iteritems():
                    classifiers[k] = models[v]

                #Try to load the imputeData, basicStat and NTrainEx from a model that saved it!
                if hasattr(classifiers.itervalues().next(), "basicStat") and classifiers.itervalues().next().basicStat and not basicStat:
                    basicStat = classifiers.itervalues().next().basicStat
                if hasattr(classifiers.itervalues().next(), "NTrainEx") and classifiers.itervalues().next().NTrainEx and not NTrainEx:
                    NTrainEx = classifiers.itervalues().next().NTrainEx
                if hasattr(classifiers.itervalues().next(), "imputeData") and classifiers.itervalues().next().imputeData and not imputeData:
                    imputeData = classifiers.itervalues().next().imputeData
                    domainFile = imputeData #This is needed for domain compatibility between imputer and domain var

                if os.path.exists(expressionListFilename):
                    file = open(expressionListFilename)
                    expression = pickle.load(file)
                    file.close()
                else:
                    file = open(expressionFilename)
                    expression = pickle.load(file)
                    file.close()

                if os.path.exists(weightsFilename):
                    file = open(weightsFilename)
                    weights = pickle.load(file)
                    file.close()
                    
            else:
                #
                # Default expression to read
                #
                
                classifiers = []
                for mFile in modelFiles:
                    classifiers.append(AZBaseClasses.modelRead(mFile))

                if not classifiers[-1]:
                    if verbose > 0:
                        print "ERROR: Could not load the model ",mFile
                    return None
                else:
                    #Try to load the imputeData, basicStat and NTrainEx from a model that saved it!
                    if hasattr(classifiers[-1], "basicStat") and classifiers[-1].basicStat and not basicStat:
                        basicStat = classifiers[-1].basicStat
                    if hasattr(classifiers[-1], "NTrainEx") and classifiers[-1].NTrainEx and not NTrainEx:
                        NTrainEx = classifiers[-1].NTrainEx
                    if hasattr(classifiers[-1], "imputeData") and classifiers[-1].imputeData and not imputeData:
                        imputeData = classifiers[-1].imputeData
                        domainFile = imputeData #This is needed for domain compatibilitu betwene imputer and domain var

    except:
        if verbose > 0: print "ERROR: It was not possible to load the Consensus model"
        return None
    return ConsensusClassifier(classifiers=classifiers,
                               expression=expression,
                               weights=weights,
                               varNames = [attr.name for attr in domainFile.domain.attributes],
                               classVar = domainFile.domain.classVar,
                               verbose = verbose,
                               domain = domainFile.domain,
                               basicStat = basicStat,
                               NTrainEx = NTrainEx,
                               imputeData = imputeData)