def SplitTrainTest(lSamples): if config.get_bool('SPLIT_BY_EASY_HARD'): setEasy = set(data.file_to_obj(config.get_string('EASY_CONNECTIONS_LIST_FILE'))); lTrain = filter(lambda samp: samp.pddlconn.sPddlTo in setEasy, lSamples); lTest = filter(lambda samp: samp.pddlconn.sPddlTo not in setEasy, lSamples); print "NUM Train:", len(lTrain), "Test:", len(lTest); return lTrain, lTest; if config.get_bool('SPLIT_BY_FIRST_30'): setFirst30 = set(data.file_to_obj(config.get_string('FIRST_30_CONNECTIONS_LIST_FILE'))); lTrain = filter(lambda samp: samp.pddlconn.sPddlTo in setFirst30, lSamples); lTest = filter(lambda samp: samp.pddlconn.sPddlTo not in setFirst30, lSamples); print "NUM Train:", len(lTrain), "Test:", len(lTest); return lTrain, lTest; if config.get_bool('SPLIT_BY_SENTENCE'): lRandomIndexes, iSplit = SplitSampleBySentence(lSamples); else: lRandomIndexes = range(len(lSamples)); random.shuffle(lRandomIndexes); iSplit = len(lSamples)/2; if config.get_bool('TRAIN_AND_TEST_ON_ALL'): assert(not config.get_bool('TRAIN_ON_HALF_TEST_ON_ALL')); lTrainIndexes = range(len(lSamples)); lTestIndexes = range(len(lSamples)); elif config.get_bool('TRAIN_ON_HALF_TEST_ON_ALL'): lTrainIndexes = lRandomIndexes[:iSplit]; lTestIndexes = range(len(lSamples)); elif config.get_bool('TRAIN_ON_HALF_TEST_ON_HALF'): lTrainIndexes = lRandomIndexes[:iSplit]; lTestIndexes = lRandomIndexes[iSplit:]; else: assert False, 'No Test Train Split Method Specified'; lTrain = map(lambda x:lSamples[x], lTrainIndexes); lTest = map(lambda x:lSamples[x], lTestIndexes); return lTrain, lTest;
def LoadRewardsDict(): sFileRewards = config.get_string('REWARDS_FILE'); dConnRewardsIndexes = collections.defaultdict(lambda:{'iNumNeg':0, 'iNumPos':0}); iMaxIter = config.get_int('REWARDS_MAX_ITER'); iMinIter = config.get_int('REWARDS_MIN_ITER'); iCurIter = 0; for sLine in open(sFileRewards): sLine = sLine.strip(); lConns = sLine.split(); if len(lConns) > 5: iCurIter += 1; if iCurIter > iMaxIter: break; if iCurIter < iMinIter: continue; for sConn in lConns: iFrom, iTo, iNumPos, iNumNeg = sConn.split(':'); iNumPos = int(iNumPos); iNumNeg = int(iNumNeg); iFrom = int(iFrom); iTo = int(iTo); dConnRewardsIndexes[(iFrom,iTo)]['iNumNeg'] += iNumNeg; dConnRewardsIndexes[(iFrom,iTo)]['iNumPos'] += iNumPos; dIndexToPredList = predicate.PredDictFileToPredListDict(config.get_string('PRED_DICT_FILE'), lambda x:x.iIndex); dConnRewardsStrings = collections.defaultdict(lambda:{'iNumNeg':0, 'iNumPos':0}); for (iFrom, iTo), dPosNeg in dConnRewardsIndexes.items(): sFrom = dIndexToPredList[iFrom][0].GetObject(); sTo = dIndexToPredList[iTo][0].GetObject(); dConnRewardsStrings[(sFrom, sTo)]['iNumNeg'] += dPosNeg['iNumNeg']; dConnRewardsStrings[(sFrom, sTo)]['iNumPos'] += dPosNeg['iNumPos']; return dConnRewardsStrings;
def get_proxy_settings(config=None, conn=None): r""" Return proxy settings as a ProxySettings object The caller must specify either config or conn. Arguments: - `config`: A osdlyrics.config.Config object, this object is used to retrive proxy settings. If it is not set, the caller MUST set conn to a valid D-Bus connection to create a Config object - `conn`: A D-Bus connection object, this is used when `config` is not specified. """ if config is None and conn is None: raise ValueError('Either config or conn must be specified') if config is None: config = config.Config(conn) proxy_type = config.get_string('Download/proxy') if proxy_type.lower() == 'no': return ProxySettings(protocol='no') if proxy_type.lower() == 'manual': protocol = config.get_string('Download/proxy-type') host = config.get_string('Download/proxy-host') port = config.get_int('Download/proxy-port') username = config.get_string('Download/proxy-username') passwd = config.get_string('Download/proxy-passwd') return ProxySettings(protocol=protocol, host=host, port=port, username=username, password=passwd) if proxy_type.lower() == 'system': return detect_system_proxy()
def CalcConnFileFScore(): print "Calcing FScore for:", config.get_string('CONN_FILE'); setConnTups = predicate.ReadConnectionsFileToTupSet(); setGoldConns = LoadGoldStringConnSet(); if config.get_bool('ANALYZE_ON_HARD'): lEasy = data.file_to_obj(config.get_string('EASY_CONNECTIONS_LIST_FILE')); iTruePos = 0; iFalsePos = 0; for sFrom, sTo in setConnTups: if config.get_bool('ANALYZE_ON_HARD'): if sTo in lEasy: continue; if (sFrom, sTo) in setGoldConns: iTruePos += 1; else: iFalsePos += 1; iFalseNeg = len(setGoldConns)-iTruePos; if config.get_bool('ANALYZE_ON_HARD'): iFalseNeg = len(setGoldConns)-iTruePos-len(lEasy); iTrueNeg = 0; fPrecision = float(iTruePos)/float(iTruePos+iFalsePos) if iTruePos > 0 else 0; fRecall = float(iTruePos)/float(iTruePos+iFalseNeg) if iTruePos > 0 else 0; fScore = 2*fPrecision*fRecall/(fPrecision+fRecall) if (fPrecision*fRecall) > 0 else 0; print "TP:", iTruePos, "FP:", iFalsePos, "TN:", iTrueNeg, "FN:", iFalseNeg; print "Precision:", fPrecision; print "Recall:", fRecall; print "FScore:", fScore;
def ReadConnectionsFileToTupSet(): dIndexToPred = PredDictFileToPredDict(config.get_string('PRED_DICT_FILE'), lambda x:x.iIndex); setConnTups = set(); for sLine in open(config.get_string('CONN_FILE')): sLine = sLine.strip(); lSplit = sLine.split('|'); setConnTups.add((dIndexToPred[int(lSplit[1])].GetObject(), dIndexToPred[int(lSplit[2])].GetObject())); return setConnTups;
def __init__(self): exchange_name = config.get_string('grid', 'name') symbol = config.get_string('grid', 'symbol') super(Grid, self).__init__(exchange_name, symbol) self.base_line = config.get_float('grid', 'base_line') # 当前价格基准线 self.one_hand = config.get_float('grid', 'one_hand') # 一手买多少 logging.info("交易所:%s" % exchange_name) logging.info("交易对:%s" % symbol) logging.info("基线:%f" % self.base_line) logging.info("一格:%f" % self.one_hand)
def Train(lSamples, j = 1, bSuppress = False): sTrainFile = config.get_string('SVM_TRAIN'); sModelFile = config.get_string('SVM_MODEL'); Sample.WriteSvmFile(lSamples, sTrainFile); if config.get_bool('USE_SVM_PERF'): # version used for good svm results iRetVal = PrintAndRun("../../bin/svm_perf_learn -c 10 -w 3 -l 1 -b 0 " + sTrainFile + " " + "model.svm", bSuppress = bSuppress); #iRetVal = PrintAndRun("../../bin/svm_perf_learn -c 1 -w 3 -l 1 -b 0 " + sTrainFile + " " + "model.svm"); else: iRetVal = PrintAndRun("../../bin/svm_learn -c 10 -b 0 -m 1000 -j " + str(j) + ' ' + sTrainFile + " " + "model.svm", bSuppress = bSuppress);
def WriteFirst30SvmConnectionsFile(lGranularSamples): assert config.get_bool('SPLIT_BY_FIRST_30'); lTrainGranular, lTestGranular = SplitTrainTest(lGranularSamples); lTestCollapsed, dFeatureWeights = TrainAndTestSvm(lTrainGranular, lTestGranular); fScore, fPrec, fRecall = AnalyzePredsSimple(lTestCollapsed); Sample.CollapsedSample.WriteConnections(lTestCollapsed, config.get_string('FIRST_30_SVM_CONNECTIONS_FILE'), bAppend=False, bWritePredictions = True, bPosOnly = True); # note that this one is train on train and test on train (yes those words are correct) lTrainCollapsed, dFeatureWeights = TrainAndTestSvm(lTrainGranular, lTrainGranular); fScore, fPrec, fRecall = AnalyzePredsSimple(lTrainCollapsed); Sample.CollapsedSample.WriteConnections(lTrainCollapsed, config.get_string('FIRST_30_SVM_CONNECTIONS_FILE'), bAppend=True, bWritePredictions = True, bPosOnly = True);
def ReadSentencesFromTextFile(): sSentenceFile = config.get_string('SENTENCE_FILE'); sOldSentenceFile = config.get_string('OLD_SENTENCE_FILE'); sDepFile = config.get_string('DEPENDANCY_FILE'); lSentences = ReadSentencesFromTextFileSimple(sSentenceFile); if sDepFile != '': Sentence.AddDepFeatures(lSentences, sDepFile); if sOldSentenceFile != '': lOldSentences = ReadSentencesFromTextFileSimple(sOldSentenceFile); Sentence.AddOldSentenceFeatures(lSentences, lOldSentences); return lSentences;
def Test(lSamples, bSuppress = False): sTestFile = config.get_string('SVM_TEST'); sModelFile = config.get_string('SVM_MODEL'); sPredFile = config.get_string('SVM_PRED'); Sample.WriteSvmFile(lSamples, sTestFile); if config.get_bool('USE_SVM_PERF'): iRetVal = PrintAndRun("../../bin/svm_perf_classify " + sTestFile + " " + sModelFile + " " + sPredFile, bSuppress = bSuppress); else: iRetVal = PrintAndRun("../../bin/svm_classify " + sTestFile + " " + sModelFile + " " + sPredFile, bSuppress = bSuppress); sPredLines = open(sPredFile).readlines(); lPreds = map(lambda x:float(x), sPredLines); assert (len(lPreds) == len(lSamples)); for pred, sample in zip(lPreds, lSamples): sample.fPred = float(pred);
def LoadPredStringSet(): lPreds = PredDictFileToPredList(config.get_string('PRED_DICT_FILE')); setPreds = set(); for pred in lPreds: setPreds.add(pred.GetObject()); print "NumPreds:", len(setPreds); return setPreds;
def LoadGoldIndexesConnSet(): setGoldStringConns = LoadGoldStringConnSet(); dObjToPredList = predicate.PredDictFileToPredListDict(config.get_string('PRED_DICT_FILE'), lambda x:x.GetObject()); setGoldIndexConns = set(); for sFrom, sTo in setGoldStringConns: for predFrom in dObjToPredList[sFrom]: for predTo in dObjToPredList[sTo]: setGoldIndexConns.add((predFrom.iIndex,predTo.iIndex)); return setGoldIndexConns;
def GenAllGranularSamplesFromList(lSentences, sLogFileName): sSentenceLogFile = config.get_string('SENTENCE_LOG_FILE'); fLog = open(sSentenceLogFile, 'w'); lSamples = []; iNumLoopy = 0; for sentence in lSentences: iCurNumLoopy, lCurSamples = sentence.GenAllGranularSamples(fLog); lSamples.extend(lCurSamples); iNumLoopy += iCurNumLoopy; if iNumLoopy > 0: print "NUM LOOPY:", iNumLoopy; assert iNumLoopy < config.get_int('NUM_ALLOWED_LOOPY'), 'Too Many Loopy: ' + str(iNumLoopy) + ' NonLoopy: ' + str(len(lSamples)); sGoldDepFile = config.get_string('GOLD_DEP_FILE'); if sGoldDepFile != '': dGoldDeps = data.file_to_obj_with_comments(sGoldDepFile); # add the gold dep info for sample in lSamples: if (sample.pddlconn.sPddlTo in dGoldDeps) and (sample.pddlconn.sPddlFrom in dGoldDeps[sample.pddlconn.sPddlTo]): sample.bGoldPos = True; sPredDictFile = config.get_string('PRED_DICT_FILE'); if sPredDictFile != '': lPredicates = predicate.PredDictFileToPredList(sPredDictFile); dObjToPredList = collections.defaultdict(lambda:[]); for predCur in lPredicates: dObjToPredList[predCur.GetObject()].append(predCur); for sample in lSamples: sample.pddlconn.lFromPreds = dObjToPredList[sample.pddlconn.sPddlFrom]; sample.pddlconn.lToPreds = dObjToPredList[sample.pddlconn.sPddlTo]; else: assert False; #prune the unecessary features dFeatureCounts = collections.defaultdict(lambda:0); for sample in lSamples: for iFeature in sample.features.GetFeatureIndexList(): dFeatureCounts[iFeature] += 1; iMinFeatureCount = config.get_int('MIN_FEATURE_OCCURANCE_COUNT'); for sample in lSamples: for iFeature in sample.features.GetFeatureIndexList(): if dFeatureCounts[iFeature] < iMinFeatureCount: sample.features.RemoveFeature(iFeature); return lSamples;
def LoadAllFullRewardsFiles(): sRewardsDir = config.get_string('FULL_REWARDS_LOG_DIR'); lRewards = []; print "Loading 200 Files From:", sRewardsDir; for iFileNum in range(config.get_int('REWARDS_MIN_ITER'), config.get_int('REWARDS_MAX_ITER')): sFile = sRewardsDir + '/predictions.log.' + str(iFileNum); lRewards.extend(LoadSingleFullRewardsFile(sFile)); print "Done Loading"; print "NumFF:", Reward.ComputeNumFfsFromList(lRewards); sys.exit(-1); return lRewards;
def LoadGoldStringConnSet(): sGoldDepFile = config.get_string('GOLD_DEP_FILE'); assert(sGoldDepFile != ''); dGoldDeps = data.file_to_obj_with_comments(sGoldDepFile); setGoldConns = set(); for sTo, dFrom in dGoldDeps.items(): for sFrom in dFrom: if sFrom == 'num': continue; setGoldConns.add((sFrom, sTo)); return setGoldConns; iNumGold = len(setGoldConns);
def WriteBadPredCounts(dFalsePosCounts, dFalseNegCounts, dTruePosCounts, dTotalCounts): if config.get_string('FALSE_POS_COUNTS_FILE') != '': WriteCountsFile(dFalsePosCounts, dTotalCounts, config.get_string('FALSE_POS_COUNTS_FILE')); if config.get_string('FALSE_NEG_COUNTS_FILE') != '': WriteCountsFile(dFalseNegCounts, dTotalCounts, config.get_string('FALSE_NEG_COUNTS_FILE')); if config.get_string('TRUE_POS_COUNTS_FILE') != '': WriteCountsFile(dTruePosCounts, dTotalCounts, config.get_string('TRUE_POS_COUNTS_FILE'));
def Run(): if config.get_bool('CALC_CONN_FILE_FSCORE'): CalcConnFileFScore(); return; elif config.get_bool('CALC_EASY_HARD_CONNECTIONS'): CalcEasyHardConnections(); return; elif config.get_bool('LOAD_FULL_REWARDS'): LoadFullRewards(); return; lSentences = Sentence.ReadSentencesFromTextFile(); lGranularSamples = Sentence.GenAllGranularSamplesFromList(lSentences, 'sentences.log'); if config.get_bool('CALC_ALL_TEXT_FSCORE'): CalcAllTextFScore(lGranularSamples); elif config.get_string('GRANULAR_SAMPLE_FILE') != '': Sample.GranularSample.WriteList(lGranularSamples, config.get_string('GRANULAR_SAMPLE_FILE')); elif config.get_string('SENTENCES_AND_FEATURES_FILE') != '': Sample.GranularSample.WriteDebugFromList(lGranularSamples, config.get_string('SENTENCES_AND_FEATURES_FILE')); elif config.get_string('SVM_CONNECTIONS_FILE') != '': WriteSvmConnectionsFile(lGranularSamples); elif config.get_string('FIRST_30_SVM_CONNECTIONS_FILE') != '': WriteFirst30SvmConnectionsFile(lGranularSamples); elif config.get_string('COLLAPSED_MANUAL_TEXT_CONNECTIONS_FILE'): lCollapsed = Sample.CollapseSamples(lGranularSamples); Sample.CollapsedSample.WriteConnections(lCollapsed, config.get_string('COLLAPSED_MANUAL_TEXT_CONNECTIONS_FILE'), bPosOnly = True); elif config.get_string('COLLAPSED_ALL_TEXT_CONNECTIONS_FILE'): lCollapsed = Sample.CollapseSamples(lGranularSamples); Sample.CollapsedSample.WriteConnections(lCollapsed, config.get_string('COLLAPSED_ALL_TEXT_CONNECTIONS_FILE'), bPosOnly = False); elif config.get_bool('TRAIN_ON_REWARD_EVAL_ON_GOLD'): TrainOnRewardEvalOnGold(lGranularSamples); else: Evaluate(lGranularSamples);
def TrainOnRewardEvalOnGold(lGranularSamples): #lGranularSamples = lGranularSamples[:100]; assert(config.get_bool('COLLAPSE_FIRST')); dConnRewards = LoadRewardsDict(); fNegMultiplier = PrintDebugInfo(dConnRewards); #add the reward data to the samples themselves lCollapsedSamples = Sample.CollapseSamples(lGranularSamples); lNewSamples = []; for sample in lCollapsedSamples: dRewards = dConnRewards[sample.pddlconn.sPddlFrom, sample.pddlconn.sPddlTo]; if dRewards['iNumPos'] > 0: for iIter in range(dRewards['iNumPos']): sampleNew = copy.copy(sample); sampleNew._bPos = True; lNewSamples.append(sampleNew); else: for iIter in range(int(math.ceil(dRewards['iNumNeg']*fNegMultiplier))): sampleNew = copy.copy(sample); sampleNew._bPos = False; lNewSamples.append(sampleNew); lCollapsedSamples = lNewSamples; lFScores = []; lPrecisions = []; lRecalls = []; lTrainCollapsed, lTestCollapsed = SplitTrainTest(lCollapsedSamples); svm.Train(lTrainCollapsed); svm.Test(lTestCollapsed); # remove the duplicates setAlreadySeen = set(); lTestNoDups = []; for sample in lTestCollapsed: tKey = (sample.pddlconn.sPddlFrom, sample.pddlconn.sPddlTo); if tKey not in setAlreadySeen: setAlreadySeen.add(tKey); lTestNoDups.append(sample); fScore, fPrec, fRecall = AnalyzePredsSimple(lTestNoDups); Sample.CollapsedSample.WriteConnections(lTestNoDups, config.get_string('SVM_REWARD_CONNECTIONS_FILE'), bAppend=False, bWritePredictions = True, bPosOnly = True); fOut = open('debug.txt', 'w'); for sample in lTestCollapsed: print >> fOut, "Sample:", sample.pddlconn.sPddlFrom, sample.pddlconn.sPddlTo, sample.fPred; print "Precision: ", fPrec print "Recall: ", fRecall print "F-Score: ", fScore;
def LoadFullRewards(): dConnRewards = reward.LoadFullRewardsDict(); #print "Len:", len(dConnRewards); #for (sFrom, sTo), cr in dConnRewards.items(): # print sFrom, sTo, "Pos:", cr.iNumPos, "NE:", cr.iNumEarlierNeg, "NR:", cr.iNumNoReachNeg; #lGranularSamples = lGranularSamples[:100]; assert(config.get_bool('COLLAPSE_FIRST')); #add the reward data to the samples themselves lSentences = Sentence.ReadSentencesFromTextFile(); lGranularSamples = Sentence.GenAllGranularSamplesFromList(lSentences, 'sentences.log'); lCollapsedSamples = Sample.CollapseSamples(lGranularSamples); setEasy = set(data.file_to_obj(config.get_string('EASY_CONNECTIONS_LIST_FILE'))); lTrainCollapsed = filter(lambda sample: sample.pddlconn.sPddlTo in setEasy, lCollapsedSamples); lTestCollapsed = filter(lambda sample: sample.pddlconn.sPddlTo not in setEasy, lCollapsedSamples); lTrainingSamples = GenerateTrainingSamplesFromRewards(dConnRewards, lTrainCollapsed); if config.get_bool('SVM'): svm.Train(lTrainingSamples); svm.Test(lTestCollapsed); elif config.get_bool('LOG_LINEAR'): log_linear.TrainAndTestLogLinear(lTrainingSamples, lTestCollapsed); else: assert False; # remove the duplicates setAlreadySeen = set(); lTestNoDups = []; for sample in lTestCollapsed: tKey = (sample.pddlconn.sPddlFrom, sample.pddlconn.sPddlTo); if tKey not in setAlreadySeen: setAlreadySeen.add(tKey); lTestNoDups.append(sample); fScore, fPrec, fRecall = AnalyzePredsSimple(lTestNoDups); #Sample.CollapsedSample.WriteConnections(lTestNoDups, config.get_string('SVM_REWARD_CONNECTIONS_FILE'), # bAppend=False, bWritePredictions = True, bPosOnly = True); #fOut = open('debug.txt', 'w'); #for sample in lTestCollapsed: # print >> fOut, "Sample:", sample.pddlconn.sPddlFrom, sample.pddlconn.sPddlTo, sample.fPred; print "Precision: ", fPrec print "Recall: ", fRecall print "F-Score: ", fScore;
def CalcAllTextFScore(lGranularSamples): lCollapsedSamples = Sample.CollapseSamples(lGranularSamples); lSorted = data.file_to_obj(config.get_string('SORTED_CONNECTIONS_LIST_FILE')); lSorted.reverse(); lPos = [0 for i in range(len(lSorted))]; lNeg = [0 for i in range(len(lSorted))]; iPosTot = 0; iNegTot = 0; for sample in lCollapsedSamples: if sample.bPos: iPosTot += 1; else: iNegTot += 1; for i in range(len(lSorted)): if sample.pddlconn.sPddlTo != lSorted[i]: continue; if sample.bPos: lPos[i] += 1; else: lNeg[i] += 1; for i in range(len(lSorted)): fPrecision = float(lPos[i])/float(lPos[i] + lNeg[i]) if (lPos[i] + lNeg[i]) != 0 else 0; print lSorted[i], fPrecision; print "Overall Precision:", float(iPosTot)/float(iNegTot);
# coding=utf-8 # 通知功能 import requests import config _DING_NOTIFY_URL = 'https://oapi.dingtalk.com/robot/send?access_token=' \ + config.get_string('notify', 'ding_token') def notify_by_dingding(msg): """ 使用钉钉发送通知消息 :return: """ send_obj = {"msgtype": 'text', 'text': {'content': msg}} requests.post(_DING_NOTIFY_URL, json=send_obj) if __name__ == '__main__': notify_by_dingding('币价涨了')
def AnalyzePredsSimple(lSamples): if config.get_bool('FORCE_SINGLE_DIR'): dSamples = {}; for sample in lSamples: tKey = (sample.pddlconn.sPddlFrom, sample.pddlconn.sPddlTo); assert(tKey not in dSamples); dSamples[tKey] = sample; iNumTotal = 0; iNumCorrect = 0; iTruePos = 0; iFalsePos = 0; iTrueNeg = 0; iFalseNeg = 0; iThres = 0; if config.get_bool('SVM'): fThres = config.get_int('SVM_THRESHOLD'); elif config.get_bool('LOG_LINEAR'): fThres = 0.5 else: assert False; if config.get_bool('CALC_FSCORE_ON_GOLD'): setGoldStringConns = LoadGoldStringConnSet() iNumGold = len(setGoldStringConns); if config.get_bool('ANALYZE_ON_HARD'): lEasy = data.file_to_obj(config.get_string('EASY_CONNECTIONS_LIST_FILE')); fPredMin = sys.float_info.max; fPredMax = -sys.float_info.max; for sample in lSamples: if config.get_bool('ANALYZE_ON_HARD'): if sample.pddlconn.sPddlTo in lEasy: continue; if config.get_bool('TRAIN_ON_REWARD_EVAL_ON_GOLD'): bActual = sample.GetGoldPos(bIgnoreDir = config.get_bool('IGNORE_DIR_FOR_EVAL')); else: bActual = sample.GetPos(bIgnoreDir = config.get_bool('IGNORE_DIR_FOR_EVAL')); if config.get_bool('FORCE_SINGLE_DIR'): fPred = sample.fPred; tReverseKey = (sample.pddlconn.sPddlTo, sample.pddlconn.sPddlFrom); fReversePred = dSamples[tReverseKey].fPred if tReverseKey in dSamples else -sys.maxint; bNormalPred = (float(sample.fPred) > fThres); bPred = ((float(sample.fPred) > fThres) and (float(fPred) >= float(fReversePred))); if tReverseKey not in dSamples: print "FORCE-MISSING"; elif (bNormalPred == bActual) and (bPred != bActual): print "FORCE-BAD:", sample.pddlconn.sPddlFrom, sample.pddlconn.sPddlTo, fPred, fReversePred; elif (bNormalPred != bActual) and (bPred == bActual): print "FORCE-GOOD:", sample.pddlconn.sPddlFrom, sample.pddlconn.sPddlTo, fPred, fReversePred; else: print "FORCE-NEITHER:", sample.pddlconn.sPddlFrom, sample.pddlconn.sPddlTo, fPred, fReversePred; else: bPred = sample.GetPredPos(bIgnoreDir = config.get_bool('IGNORE_DIR_FOR_EVAL')); fPredMin = min(fPredMin, sample.fPred); fPredMax = max(fPredMax, sample.fPred); iNumTotal += 1; if bPred == bActual: iNumCorrect += 1; if bPred: if bActual: iTruePos += 1; else: iFalsePos += 1; else: if bActual: iFalseNeg += 1; else: iTrueNeg += 1; if config.get_bool('CALC_FSCORE_ON_GOLD'): iFalseNeg = iNumGold - iTruePos; if config.get_bool('ANALYZE_ON_HARD'): iFalseNeg = iNumGold - iTruePos - len(lEasy); fPrecision = float(iTruePos)/float(iTruePos+iFalsePos) if iTruePos > 0 else 0; fRecall = float(iTruePos)/float(iTruePos+iFalseNeg) if iTruePos > 0 else 0; fScore = 2*fPrecision*fRecall/(fPrecision+fRecall) if (fPrecision*fRecall) > 0 else 0; print "FPred: min:", fPredMin, "max:", fPredMax; print "FScore:", fScore, fPrecision, fRecall; print "Frac Correct:", float(iNumCorrect)/float(iNumTotal), iNumCorrect, iNumTotal; print "TP:", iTruePos, "FP:", iFalsePos, "TN:", iTrueNeg, "FN:", iFalseNeg; print "FracPos:", float(iTruePos+iFalsePos)/float(iTrueNeg+iFalseNeg+iTruePos+iFalsePos); return fScore, fPrecision, fRecall;
disp.print("Bat: %.2fV" % power.read_battery_voltage(), posy=60, fg=color.WHITE) disp.update() last_rx_time = 0 disp = display.open() v_old = 0 pause = 1 interrupt.set_callback(interrupt.BLE, ble_callback) interrupt.enable_callback(interrupt.BLE) try: vib_mode = int(config.get_string("exno_vib_mode")) except: pass try: led_mode = int(config.get_string("exno_led_mode")) except: pass disp.clear() disp.print(" Exp Notif", posy=0, fg=color.WHITE) disp.print(" Logger", posy=20, fg=color.WHITE) disp.print(" ", posy=40, fg=color.WHITE) disp.print(" BL On ->", posy=60, fg=color.WHITE) disp.update()