def test_classifier_run(): # ======================================= # refresh random select feature json file # ======================================= ECGRF.ECGrf.RefreshRandomFeatureJsonFile() # collect test record list sel1213 = conf['sel1213'] target_index = 11 # init rf classifier rf = ECGRF.ECGrf() # traning traininglist = [x for x in sel1213 \ if x != sel1213[target_index]] traininglist = traininglist[0:5] # prompt print '>>> Training {}'.format(traininglist) rf.training(traininglist) print '>>> Training finished.' # testing print 'testing:', sel1213[target_index] rf.testmdl(\ reclist = [sel1213[target_index],],\ TestResultFileName = \ os.path.join(\ projhomepath,\ 'tmp',\ 'tmp.out'.format(target_index)))
def TestingAndSaveResult(): sel1213 = conf['sel1213'] time0 = time.time() rf = ECGRF.ECGrf() rf.training(sel1213[0:1]) time1 = time.time() print 'Training time:', time1 - time0 ## test rf.testmdl(reclist=sel1213[0:1])
def TestAllQTdata(saveresultpath): # Leave Ntest out of 30 records to test # qt_loader = QTloader() QTreclist = qt_loader.getQTrecnamelist() print 'Totoal QT record number:{}'.format(len(QTreclist)) ## Training record list selall0 = conf["selQTall0"] selrecords = list(set(QTreclist) - set(selall0)) rf = ECGRF.ECGrf() # Multi Process rf.useParallelTest = True rf.TestRange = 'All' # ================ # evaluate time cost for each stage # ================ # clear debug logger ECGRF.debugLogger.clear() # display the time left to finish program one_round_time_cost = [] # ==================== # Training # ==================== ECGRF.debugLogger.dump('\n====Test Start ====\n') time0 = time.time() # training the rf classifier with reclist # # dump to debug logger rf.training(selrecords) # timing time1 = time.time() print 'Total Training time:', time1 - time0 ECGRF.debugLogger.dump('Total Training time: {:.2f} s\n'.format(time1 - time0)) # save the trained model savemodelpath = os.path.join(saveresultpath, 'QT_sel.mdl') with open(savemodelpath, 'w') as fout: pickle.dump(rf.mdl, fout) return ## test testinglist = selall0 print '\n>>Testing:', testinglist ECGRF.debugLogger.dump('\n======\n\nTest Set :{}'.format(selrecords)) rf.testrecords(reclist=selrecords, TestResultFileName=os.path.join(saveresultpath, 'TestResult.out'))
def leaveonetest(saveresultpath, StartFileIndex=0): # # test speed version of LOOT # # refresh random select feature json file # ECGRF.ECGrf.RefreshRandomFeatureJsonFile() sel1213 = conf['sel0116'] rf = ECGRF.ECGrf() ECGRF.debugLogger.clear() # display the time left to finish program one_round_time_cost = [] # reapeat test for i in range(StartFileIndex, len(sel1213)): # round start time time_round_start = time.time() print '====Test Index {} ===='.format(i) ECGRF.debugLogger.dump('\n====Test Index {} ====\n'.format(i)) time0 = time.time() # training the rf classifier with reclist # traininglist = [x for x in sel1213 \ if x != sel1213[i]] rf.training(traininglist) # timing time1 = time.time() print 'Total Training time:', time1 - time0 ECGRF.debugLogger.dump(\ 'Total Training time: {:.2f} s\n'.format(time1-time0)) ## test print 'testing:', sel1213[i] rf.testmdl(reclist = [sel1213[i],],\ TestResultFileName = \ os.path.join(saveresultpath,'hand{}.out'.format(i))) # round end time time_round_end = time.time() one_round_time_cost.append(time_round_end - time_round_start) time_cost_per_round = np.nanmean(one_round_time_cost) if time_cost_per_round is not None: print '\n[Time left:{:.1f}s({:.1f}min)]'.format( (len(sel1213) - i - 1) * time_cost_per_round, (len(sel1213) - i - 1) * time_cost_per_round / 60.0)
def testEval(picklefilename): with open(picklefilename, 'r') as fin: Results = pickle.load(fin) rfobj = ECGRF.ECGrf() fResults = rfobj.resfilter(Results) # show filtered results & raw results #for recname , recRes in Results: #rfobj.plotresult(Results[2],showExpertLabel = True) #rfobj.plotresult(fResults[2],figureID = 2,showExpertLabel = True) # Evaluate prediction result statistics # ECGstats = ecgEval.ECGstatistics(fResults[0:2]) ECGstats.eval(debug=True) ECGstats.dispstat0() ECGstats.plotevalresofrec(fResults[1][0], Results)
def leaveonetest(): sel1213 = conf['sel1213'] rf = ECGRF.ECGrf() # reapeat test for i in range(0, len(sel1213)): print '====Test Index {} ===='.format(i) time0 = time.time() traininglist = [x for x in sel1213 \ if x != sel1213[i]] rf.training(traininglist) time1 = time.time() print 'Training time:', time1 - time0 ## test print 'testing:', sel1213[i] rf.testmdl(reclist=[ sel1213[i], ])
def TestAllQTdata(saveresultpath): # Leave Ntest out of 30 records to test # qt_loader = QTloader() QTreclist = qt_loader.getQTrecnamelist() print 'Totoal QT record number:{}'.format(len(QTreclist)) ## Training record list testinglist = conf["selQTall0_test_set"] traininglist = list(set(QTreclist) - set(testinglist)) rf = ECGRF.ECGrf() # Multi Process rf.useParallelTest = True rf.TestRange = 'All' # clear debug logger ECGRF.debugLogger.clear() # ==================== # Training # ==================== ECGRF.debugLogger.dump('\n====Test Start ====\n') # training the rf classifier with reclist # # dump to debug logger time_cost_output = [] timing_for(rf.training, [ traininglist, ], prompt='Total Training time:', time_cost_output=time_cost_output) ECGRF.debugLogger.dump('Total Training time: {:.2f} s\n'.format( time_cost_output[-1])) # save trained mdl backupobj(rf.mdl, os.path.join(saveresultpath, 'trained_model.mdl')) ## test print '\n>>Testing:', testinglist ECGRF.debugLogger.dump('\n======\n\nTest Set :{}'.format(testinglist)) rf.testrecords(saveresultpath, reclist=testinglist)
def leaveNtest(saveresultpath, Ntest, StartFileIndex=0, TestRoundNum=30): # Leave Ntest out of 30 records to test # # refresh random select feature json file ECGRF.ECGrf.RefreshRandomFeatureJsonFile() selrecords = conf['sel0116'] rf = ECGRF.ECGrf() # clear debug logger ECGRF.debugLogger.clear() # display the time left to finish program one_round_time_cost = [] # reapeat test for i in range(StartFileIndex, TestRoundNum): # round start time time_round_start = time.time() print '====Test Index {} ===='.format(i) ECGRF.debugLogger.dump('\n====Test Index {} ====\n'.format(i)) # get N random selected test records indexlist = range(0, len(selrecords)) random.shuffle(indexlist) testindexlist = indexlist[0:Ntest] # training set trainingindexset = set(indexlist) testingindexset = set(testindexlist) trainingindexset -= testingindexset time0 = time.time() # training the rf classifier with reclist # traininglist = [selrecords[x] for x in trainingindexset] # dump to debug logger ECGRF.debugLogger.dump( '\n======\nTrainingset:{}\nTrainingRecords:{}\n'.format( trainingindexset, traininglist)) rf.training(traininglist) # timing time1 = time.time() print 'Total Training time:', time1 - time0 ECGRF.debugLogger.dump('Total Training time: {:.2f} s\n'.format(time1 - time0)) ## test testinglist = [selrecords[x] for x in testingindexset] print '\n>>Testing:', testinglist ECGRF.debugLogger.dump( '\n======\nTestingindexs:{}\nTestingRecords:{}\n'.format( testingindexset, testinglist)) rf.testmdl(reclist=testinglist, TestResultFileName=os.path.join(saveresultpath, 'hand{}.out'.format(i))) # round end time time_round_end = time.time() one_round_time_cost.append(time_round_end - time_round_start) time_cost_per_round = np.nanmean(one_round_time_cost) if time_cost_per_round is not None: debug_timeleft_info = '\n[Time left:{:.1f}s({:.1f}min)]\n'.format( (TestRoundNum - i - 1) * time_cost_per_round, (TestRoundNum - i - 1) * time_cost_per_round / 60.0) print debug_timeleft_info ECGRF.debugLogger.dump(debug_timeleft_info)
def __init__(self): self.rfobj = ECGRF.ECGrf() pass