Esempio n. 1
0
	def __init__(self, questionID, algorithm, classifier, parameters, modelParams, optimizer, predict = 0):
		self.questionID = questionID
		self.algorithm = algorithm
		self.classifier = classifier
		self.parameters = parameters
		self.modelParams = modelParams
		self.api = nemoApi()
		self.config = nemoConfig()
		self.optimizer = optimizer
		self.predict = predict
		self.prediction = None
Esempio n. 2
0
def main():
	global API, CONFIG, QUEUE, finishedRun, dataLoad

	# Creating logger for logging to MAsterLog.log and console
	# logger = createLogger()

	# Load config file
	CONFIG = nemoConfig('config/dev.json')
	if CONFIG.CONFIG is None:
		print 'Could not load config file on . . .'
		return

	# Start zerorpc server for remote control
	t = threading.Thread(target=server)
	t.start()
	
	# Set semaphore
	SEMAPHORE = threading.BoundedSemaphore(CONFIG.MAX_NUM_THREADS)

	# Instantiate api for use by sub modules
	API = nemoApi(CONFIG.HOST, CONFIG.PORT, CONFIG.USER, CONFIG.PASS, CONFIG.DB)

	QUEUE = Queue.Queue()
	# Run indefinitely
	while True:
		RESULTS = API.fetchQuestions(CONFIG.MAX_QUEUE_SIZE, CONFIG.QUEUED_STATUS)
		for ROW in RESULTS:
   			QUEUE.put({
				'id': ROW['ID'],
				'makePrediction': ROW['MakePrediction'],
				'Optimizer': ROW['Optimizer'],
				'Classifier': ROW['Classifier']
			})
		if QUEUE.empty():
			 finishedRun = True
			 time.sleep(CONFIG.TIMEOUT)
		else:
		   finishedRun = False
		   while not QUEUE.empty():
		       SEMAPHORE.acquire()
		       t = threading.Thread(target=worker, args=(QUEUE.get(), SEMAPHORE))
		       t.start()
		QUEUE.join()
		finishedRun = True						
		while dataLoad:
				time.sleep(30)
				print "Waiting on data load"
Esempio n. 3
0
def main():
	# Load config file
	CONFIG = nemoConfig('config/dev.json')

	# Instantiate api
	API = nemoApi(CONFIG.HOST, CONFIG.PORT, CONFIG.USER, CONFIG.PASS, CONFIG.DB)
	newWrapper = WekaWrapper(411, 'RandomForest', 'weka.classifiers.meta.CVParameterSelection', ["-W", "weka.classifiers.trees.RandomForest", "-P", "K 1 3 3"], [], '', predict=0)
	newWrapper.run()
	#masterData = newWrapper.retrieveData(336, 'all')
	#learnerData = newWrapper.retrieveData(336, 'learner')
	#testData = newWrapper.retrieveData(312, 'test')
	#masterData.delete()
	#patient = API.fetchPatientJSON(336)
	#patientObj = newWrapper.buildPatientObject()

	x = API.getDataQuery(411, 'ALL')
	pdb.set_trace()
Esempio n. 4
0
def run(id, optimizer, algorithm):
    api = nemoApi()
    config = nemoConfig()

    # Fetch question info
    info = api.fetchQuestionInfo(id)

    # Declare vars to collect
    parameters = []
    options = None
    tweak = None
    tweakParam = None
    tweakValue = None

    # Determine which algorithm to use based on feedback
    print "Running algorithm analyzer on QuestionID " + str(id)
    if algorithm is None:
        if info is None:
            # If this is the first time running, choose one at random
            print "Picking algorithm for the first time"
            algorithms = {k: v for k, v in config.ALGORITHMS.iteritems() if v['Active'] is True}
            algorithm = random.choice(algorithms.keys())
        else:
            # Need to check if algorithm picked is currently active in config file
            # IF we need to switch algs based on info of ai model
            if info['AIFeedback'] == '\x01': #If AIFeedback is true
                print "AIFeedback is true"
                algorithm = info['Algorithm']
            else:
                print info['AIFeedback']
                print "AIFeedback is false, picking new algorithm"
                algorithms = {k: v for k, v in config.ALGORITHMS.iteritems() if v['Active'] is True and k != info['Algorithm']}
                algorithm = random.choice(algorithms.keys())
                print "Algorithm chosen: " + algorithm

    # Get parameters
    latestAIModel = api.fetchLatestAIModelByAlgorithm(id, algorithm)
    modelParams = []
    modelParamsToSave = []
    if latestAIModel is not None:
        modelID = latestAIModel['ID']
        modelParams = api.fetchAIModelParams(modelID)
        print "Model ID " + str(modelID)
        print modelParams
    # else:

    # Pick an optimizer if none was passed
    if optimizer is None:
        optimizers = {k: v for k, v in config.ALGORITHMS[algorithm]['Optimizers'].iteritems()}
        if len(optimizers) > 0:
            optimizer = random.choice(optimizers.keys())
    print 'Optimizer chosen: ' + optimizer

    # Pick a parameter from that optimizer
    tweaks = {k: v for k, v in config.ALGORITHMS[algorithm]['Optimizers'][optimizer].iteritems()}
    if len(tweaks) > 0:
        tweak = random.choice(tweaks.keys())

    if tweak is not None:
        tweakParam = config.ALGORITHMS[algorithm]['Optimizers'][optimizer][tweak]['param']
        tweakValue = config.ALGORITHMS[algorithm]['Optimizers'][optimizer][tweak]['value']

    print "Tweak param " + str(tweakParam)
    print "Tweak value " + str(tweakValue)

    newParam = AIParam(None, tweakParam, tweakValue, optimizer)
    # Add this param only if it isn't already added
    existingParam = next((x for x in modelParams if x.Param == newParam.Param and x.param_use == newParam.param_use), None)
    if existingParam is None and tweakParam is not None and tweakValue is not None:
        modelParams.append(newParam)

    # If we are using options, only pass options to classifier
    if optimizer == 'Options':
        for mParam in modelParams:
            # I think this part is wrong, the truthiness of the downloaded from DB Optional Params I was unable to figure out
            # PLEASE FIX
            if mParam.param_use == 'Options' and (mParam.Value == True or mParam.Value == 1 or mParam.Value == "1"):
                modelParamsToSave.append(mParam)
                parameters = parameters + [mParam.Param]

    # If we are using cvparams, only pass cvparams to classifier
    elif optimizer == 'CVParams':
        for mParam in modelParams:
            if mParam.param_use == 'CVParams' and mParam.Value is not None:
                modelParamsToSave.append(mParam)
                parameters = parameters + ["-P", (mParam.Param + ' ' + mParam.Value)]

    # If we are using ensemble, only pass ensemble params to classifier
    elif optimizer == 'Ensemble':
        for mParam in modelParams:
            if mParam.param_use == 'Ensemble' and mParam.Value is not None:
                modelParamsToSave.append(mParam)
                parameters = parameters + [mParam.Param, mParam.Value]

    # Run
    instance = None
    if optimizer == 'Options':
        if algorithm == "SMO":
            instance = WekaWrapper(id, algorithm, 'weka.classifiers.functions.SMO', parameters, modelParamsToSave, optimizer)
        elif algorithm == "RandomForest":
            instance = WekaWrapper(id, algorithm, 'weka.classifiers.trees.RandomForest', parameters, modelParamsToSave, optimizer)
        elif algorithm == "NaiveBayes":
            instance = WekaWrapper(id, algorithm, 'weka.classifiers.bayes.NaiveBayes', parameters, modelParamsToSave, optimizer)
        elif algorithm == "J48":
            instance = WekaWrapper(id, algorithm, 'weka.classifiers.trees.J48', parameters, modelParamsToSave, optimizer)
        elif algorithm == "Perceptron":
            instance = WekaWrapper(id, algorithm, 'weka.classifiers.functions.MultilayerPerceptron', parameters, modelParamsToSave, optimizer)
    elif optimizer == 'CVParams':
        if algorithm == "SMO":
            parameters = parameters + ["-W", "weka.classifiers.functions.SMO"]
            instance = WekaWrapper(id, algorithm, 'weka.classifiers.meta.CVParameterSelection', parameters, modelParamsToSave, optimizer)
        elif algorithm == "RandomForest":
            parameters = parameters + ["-W", "weka.classifiers.trees.RandomForest"]
            instance = WekaWrapper(id, algorithm, 'weka.classifiers.meta.CVParameterSelection', parameters, modelParamsToSave, optimizer)
        elif algorithm == "NaiveBayes":
            parameters = parameters + ["-W", "weka.classifiers.bayes.NaiveBayes"]
            instance = WekaWrapper(id, algorithm, 'weka.classifiers.meta.CVParameterSelection', parameters, modelParamsToSave, optimizer)
        elif algorithm == "J48":
            parameters = parameters + ["-W", "weka.classifiers.trees.J48"]
            instance = WekaWrapper(id, algorithm, 'weka.classifiers.meta.CVParameterSelection', parameters, modelParamsToSave, optimizer)
        elif algorithm == "Perceptron":
            parameters = parameters + ["-W", "weka.classifiers.functions.MultilayerPerceptron"]
            instance = WekaWrapper(id, algorithm, 'weka.classifiers.meta.CVParameterSelection', parameters, modelParamsToSave, optimizer)
    elif optimizer == 'FeatureSelection':
        if algorithm == "SMO":
            parameters = parameters + ["-W", "weka.classifiers.functions.SMO", "-E", "weka.attributeSelection.CfsSubsetEval -M", "-S", "weka.attributeSelection.BestFirst -D 1 -N 5"]
            instance = WekaWrapper(id, algorithm, 'weka.classifiers.meta.AttributeSelectedClassifier', parameters, modelParamsToSave, optimizer)
        elif algorithm == "RandomForest":
            parameters = parameters + ["-W", "weka.classifiers.trees.RandomForest", "-E", "weka.attributeSelection.CfsSubsetEval -M", "-S", "weka.attributeSelection.BestFirst -D 1 -N 5"]
            instance = WekaWrapper(id, algorithm, 'weka.classifiers.meta.AttributeSelectedClassifier', parameters, modelParamsToSave, optimizer)
        elif algorithm == "NaiveBayes":
            parameters = parameters + ["-W", "weka.classifiers.bayes.NaiveBayes", "-E", "weka.attributeSelection.CfsSubsetEval -M", "-S", "weka.attributeSelection.BestFirst -D 1 -N 5"]
            instance = WekaWrapper(id, algorithm, 'weka.classifiers.meta.AttributeSelectedClassifier', parameters, modelParamsToSave, optimizer)
        elif algorithm == "J48":
            parameters = parameters + ["-W", "weka.classifiers.trees.J48", "-E", "weka.attributeSelection.CfsSubsetEval -M", "-S", "weka.attributeSelection.BestFirst -D 1 -N 5"]
            instance = WekaWrapper(id, algorithm, 'weka.classifiers.meta.AttributeSelectedClassifier', parameters, modelParamsToSave, optimizer)
        elif algorithm == "Perceptron":
            parameters = parameters + ["-W", "weka.classifiers.functions.MultilayerPerceptron", "-E", "weka.attributeSelection.CfsSubsetEval -M", "-S", "weka.attributeSelection.BestFirst -D 1 -N 5"]
            instance = WekaWrapper(id, algorithm, 'weka.classifiers.meta.AttributeSelectedClassifier', parameters, modelParamsToSave, optimizer)
    elif optimizer == 'Ensemble':
        if algorithm == "SMO":
            parameters = parameters + ["-B", "weka.classifiers.functions.SMO"]
            instance = WekaWrapper(id, algorithm, 'weka.classifiers.meta.Stacking', parameters, modelParamsToSave, optimizer)
        elif algorithm == "RandomForest":
            parameters = parameters + ["-B", "weka.classifiers.trees.RandomForest"]
            instance = WekaWrapper(id, algorithm, 'weka.classifiers.meta.Stacking', parameters, modelParamsToSave, optimizer)
        elif algorithm == "NaiveBayes":
            parameters = parameters + ["-B", "weka.classifiers.bayes.NaiveBayes"]
            instance = WekaWrapper(id, algorithm, 'weka.classifiers.meta.Stacking', parameters, modelParamsToSave, optimizer)
        elif algorithm == "J48":
            parameters = parameters + ["-B", "weka.classifiers.trees.J48"]
            instance = WekaWrapper(id, algorithm, 'weka.classifiers.meta.Stacking', parameters, modelParamsToSave, optimizer)
        elif algorithm == "Perceptron":
            parameters = parameters + ["-B", "weka.classifiers.functions.MultilayerPerceptron"]
            instance = WekaWrapper(id, algorithm, 'weka.classifiers.meta.Stacking', parameters, modelParamsToSave, optimizer)
    return instance
Esempio n. 5
0
def predict(id):

    print "Running prediction on QuestionID " + str(id)

    api = nemoApi()
    config = nemoConfig()

    # Fetch question info
    info = api.fetchQuestionInfo(id)

    # Declare vars to collect
    algorithm = None
    parameters = []
    options = None
    optimizer = None

    # Get parameters
    latestAIModel = api.fetchBestAIModelByQuestion(id)
    modelParams = []
    modelParamsToSave = []
    if latestAIModel is not None:
        modelID = latestAIModel['ID']
        modelParams = api.fetchAIModelParams(modelID)
        print "Model ID " + str(modelID)
        print modelParams

        algorithm = latestAIModel['Algorithm']
        optimizer = latestAIModel['Optimizer']

        # If we are using options, only pass options to classifier
        if optimizer == 'Options':
            for mParam in modelParams:
                # I think this part is wrong, the truthiness of the downloaded from DB Optional Params I was unable to figure out
                # PLEASE FIX
                if mParam.param_use == 'Options' and (mParam.Value == True or mParam.Value == 1 or mParam.Value == "1"):
                    modelParamsToSave.append(mParam)
                    parameters = parameters + [mParam.Param]

        # If we are using cvparams, only pass cvparams to classifier
        elif optimizer == 'CVParams':
            for mParam in modelParams:
                if mParam.param_use == 'CVParams' and mParam.Value is not None:
                    modelParamsToSave.append(mParam)
                    parameters = parameters + ["-P", (mParam.Param + ' ' + mParam.Value)]

        # If we are using ensemble, only pass ensemble params to classifier
        elif optimizer == 'Ensemble':
            for mParam in modelParams:
                if mParam.param_use == 'Ensemble' and mParam.Value is not None:
                    modelParamsToSave.append(mParam)
                    parameters = parameters + [mParam.Param, mParam.Value]

        # Run
        instance = None
        if optimizer == 'Options':
            if algorithm == "SMO":
                instance = WekaWrapper(id, algorithm, 'weka.classifiers.functions.SMO', parameters, modelParamsToSave, optimizer, predict=1)
            elif algorithm == "RandomForest":
                instance = WekaWrapper(id, algorithm, 'weka.classifiers.trees.RandomForest', parameters, modelParamsToSave, optimizer, predict=1)
            elif algorithm == "NaiveBayes":
                instance = WekaWrapper(id, algorithm, 'weka.classifiers.bayes.NaiveBayes', parameters, modelParamsToSave, optimizer, predict=1)
            elif algorithm == "J48":
                instance = WekaWrapper(id, algorithm, 'weka.classifiers.trees.J48', parameters, modelParamsToSave, optimizer, predict=1)
            elif algorithm == "Perceptron":
                instance = WekaWrapper(id, algorithm, 'weka.classifiers.functions.MultilayerPerceptron', parameters, modelParamsToSave, optimizer, predict=1)
        elif optimizer == 'CVParams':
            if algorithm == "SMO":
                parameters = parameters + ["-W", "weka.classifiers.functions.SMO"]
                instance = WekaWrapper(id, algorithm, 'weka.classifiers.meta.CVParameterSelection', parameters, modelParamsToSave, optimizer, predict=1)
            elif algorithm == "RandomForest":
                parameters = parameters + ["-W", "weka.classifiers.trees.RandomForest"]
                instance = WekaWrapper(id, algorithm, 'weka.classifiers.meta.CVParameterSelection', parameters, modelParamsToSave, optimizer, predict=1)
            elif algorithm == "NaiveBayes":
                parameters = parameters + ["-W", "weka.classifiers.bayes.NaiveBayes"]
                instance = WekaWrapper(id, algorithm, 'weka.classifiers.meta.CVParameterSelection', parameters, modelParamsToSave, optimizer, predict=1)
            elif algorithm == "J48":
                parameters = parameters + ["-W", "weka.classifiers.trees.J48"]
                instance = WekaWrapper(id, algorithm, 'weka.classifiers.meta.CVParameterSelection', parameters, modelParamsToSave, optimizer, predict=1)
            elif algorithm == "Perceptron":
                parameters = parameters + ["-W", "weka.classifiers.functions.MultilayerPerceptron"]
                instance = WekaWrapper(id, algorithm, 'weka.classifiers.meta.CVParameterSelection', parameters, modelParamsToSave, optimizer, predict=1)
        elif optimizer == 'FeatureSelection':
            if algorithm == "SMO":
                parameters = parameters + ["-W", "weka.classifiers.functions.SMO", "-E", "weka.attributeSelection.CfsSubsetEval -M", "-S", "weka.attributeSelection.BestFirst -D 1 -N 5"]
                instance = WekaWrapper(id, algorithm, 'weka.classifiers.meta.AttributeSelectedClassifier', parameters, modelParamsToSave, optimizer, predict=1)
            elif algorithm == "RandomForest":
                parameters = parameters + ["-W", "weka.classifiers.trees.RandomForest", "-E", "weka.attributeSelection.CfsSubsetEval -M", "-S", "weka.attributeSelection.BestFirst -D 1 -N 5"]
                instance = WekaWrapper(id, algorithm, 'weka.classifiers.meta.AttributeSelectedClassifier', parameters, modelParamsToSave, optimizer, predict=1)
            elif algorithm == "NaiveBayes":
                parameters = parameters + ["-W", "weka.classifiers.bayes.NaiveBayes", "-E", "weka.attributeSelection.CfsSubsetEval -M", "-S", "weka.attributeSelection.BestFirst -D 1 -N 5"]
                instance = WekaWrapper(id, algorithm, 'weka.classifiers.meta.AttributeSelectedClassifier', parameters, modelParamsToSave, optimizer, predict=1)
            elif algorithm == "J48":
                parameters = parameters + ["-W", "weka.classifiers.trees.J48", "-E", "weka.attributeSelection.CfsSubsetEval -M", "-S", "weka.attributeSelection.BestFirst -D 1 -N 5"]
                instance = WekaWrapper(id, algorithm, 'weka.classifiers.meta.AttributeSelectedClassifier', parameters, modelParamsToSave, optimizer, predict=1)
            elif algorithm == "Perceptron":
                parameters = parameters + ["-W", "weka.classifiers.functions.MultilayerPerceptron", "-E", "weka.attributeSelection.CfsSubsetEval -M", "-S", "weka.attributeSelection.BestFirst -D 1 -N 5"]
                instance = WekaWrapper(id, algorithm, 'weka.classifiers.meta.AttributeSelectedClassifier', parameters, modelParamsToSave, optimizer, predict=1)
        elif optimizer == 'Ensemble':
            if algorithm == "SMO":
                parameters = parameters + ["-B", "weka.classifiers.functions.SMO"]
                instance = WekaWrapper(id, algorithm, 'weka.classifiers.meta.Stacking', parameters, modelParamsToSave, optimizer, predict=1)
            elif algorithm == "RandomForest":
                parameters = parameters + ["-B", "weka.classifiers.trees.RandomForest"]
                instance = WekaWrapper(id, algorithm, 'weka.classifiers.meta.Stacking', parameters, modelParamsToSave, optimizer, predict=1)
            elif algorithm == "NaiveBayes":
                parameters = parameters + ["-B", "weka.classifiers.bayes.NaiveBayes"]
                instance = WekaWrapper(id, algorithm, 'weka.classifiers.meta.Stacking', parameters, modelParamsToSave, optimizer, predict=1)
            elif algorithm == "J48":
                parameters = parameters + ["-B", "weka.classifiers.trees.J48"]
                instance = WekaWrapper(id, algorithm, 'weka.classifiers.meta.Stacking', parameters, modelParamsToSave, optimizer, predict=1)
            elif algorithm == "Perceptron":
                parameters = parameters + ["-B", "weka.classifiers.functions.MultilayerPerceptron"]
                instance = WekaWrapper(id, algorithm, 'weka.classifiers.meta.Stacking', parameters, modelParamsToSave, optimizer, predict=1)
        return instance
    else:
        return None