def plotMITdbTestResult(): RFfolder = os.path.join(\ projhomepath,\ 'TestResult',\ 'pc',\ 'r3') TargetRecordList = [ 'sel38', 'sel42', ] # ========================== # plot prediction result # ========================== reslist = glob.glob(os.path.join(\ RFfolder,'*')) for fi, fname in enumerate(reslist): # block *.out if fname[-4:] == '.out' or '.json' in fname: continue print 'file name:', fname currecname = os.path.split(fname)[-1] if currecname not in TargetRecordList: pass pdb.set_trace() with open(fname, 'r') as fin: (recID, reslist) = pickle.load(fin) # load signal mitdb = MITdbLoader() rawsig = mitdb.load(recID) # plot res resploter = ECGResultPloter(rawsig, reslist) resploter.plot()
def plotMITdbTestResult(): RFfolder = os.path.join(\ projhomepath,\ 'TestResult',\ 'pc',\ 'r3') TargetRecordList = [ 'sel38', 'sel42', ] # ========================== # plot prediction result # ========================== reslist = glob.glob(os.path.join(\ RFfolder,'*')) for fi, fname in enumerate(reslist): # block *.out # filter file name if fname[-4:] == '.out' or '.json' in fname: continue currecname = os.path.split(fname)[-1] if currecname not in TargetRecordList: pass if not currecname.startswith('result'): continue print 'processing file', fname, '...' with open(fname, 'r') as fin: (recID, reslist) = pickle.load(fin) print 'pickle file loaded.' # load signal from MITdb print 'loading signal data from MITdb...' mitdb = MITdbLoader() rawsig = mitdb.load(recID) print 'signal loaded.' # filter result list resfilter = ResultFilter(reslist) reslist = resfilter.groupresult() # plot res resploter = ECGResultPloter(rawsig, reslist) dispRange = (20000, 21000) savefolderpath = os.path.join(curfolderpath, 'tmp', 'MITdbTestResult') # debug #pdb.set_trace() #resploter.plot() #resploter.plotAndsave(os.path.join(savefolderpath,recID),plotTitle = 'ID:{},Range:{}'.format(recID,dispRange),dispRange = dispRange) resploter.plot(plotTitle=recID) pdb.set_trace()
def save_FNFP(self,FN,FP,OutputFolder = r'MIT_FNFP'): # save pickle recname = None if len(FN['recname']) >0: recname = FN['recname'][0] elif len(FP['recname']) > 0: recname = FP['recname'][0] if recname is None: return None with open(os.path.join(OutputFolder,recname+'.pkl'),'w') as fout: pickle.dump(dict(FN = FN,FP = FP),fout) # save matlab .mat file FNlist = [] FPlist = [] FNlist.extend(FN['pos']) FPlist.extend(FP['pos']) # save raw sig too mitdb = MITdbLoader() rawsig = mitdb.load(recname) scipy.io.savemat(os.path.join(OutputFolder,recname+'.mat'),dict(FNlist = FNlist,FPlist = FPlist,recname = recname,MIT_rawsig = rawsig))
def plotMITdbTestResult(): RFfolder = os.path.join(\ projhomepath,\ 'TestResult',\ 'pc',\ 'r3') TargetRecordList = ['sel38','sel42',] # ========================== # plot prediction result # ========================== reslist = glob.glob(os.path.join(\ RFfolder,'*')) for fi,fname in enumerate(reslist): # block *.out # filter file name if fname[-4:] == '.out' or '.json' in fname: continue currecname = os.path.split(fname)[-1] if currecname not in TargetRecordList: pass if not currecname.startswith('result'): continue print 'processing file name:',fname with open(fname,'r') as fin: (recID,reslist) = pickle.load(fin) # load signal mitdb = MITdbLoader() rawsig = mitdb.load(recID) # plot res # group result labels resgrper = ECGGrouper(reslist) reslist = resgrper.group_local_result(reslist) resploter = ECGResultPloter(rawsig,reslist) dispRange = (20000,21000) savefolderpath = ur'E:\ECGResults\MITdbTestResult\pic' # debug #resploter.plot() resploter.plot(plotTitle = 'ID:{},Range:{}'.format(recID,dispRange),dispRange = dispRange)
def analyse(self,csvfilepath = r'tmp\MITdb_FN_FP_statistics.csv'): # analysis false negtive status # analysis false positive status #RFfolder = os.path.join(\ #projhomepath,\ #'TestResult',\ #'pc',\ #'r3') RFfolder = self.RFfolder # write FN statistics to csv file csv = CSVwriter(csvfilepath) title_line = ['record name','False Negtive Number','Expert Label Number','Sensitivity','False Positive Number','True Positive Number','P+'] csv.output([title_line,]) # ========================== # analysis FN # ========================== match_dist_thres = 50 nFN = 0 nExpLabel = 0 reslist = glob.glob(os.path.join(RFfolder,'*')) N_reslist = len(reslist) for fi,fname in enumerate(reslist): # block *.out # filter file name if fname[-4:] == '.out' or '.json' in fname: continue currecname = os.path.split(fname)[-1] #if currecname not in ['result_231','result_108']: #continue if not currecname.startswith('result'): continue print 'processing file:',fname print '{} files left.'.format(N_reslist-fi-1) with open(fname,'r') as fin: (recID,reslist) = pickle.load(fin) # load signal cFN = 0 mitdb = MITdbLoader() rawsig = mitdb.load(recID) cExpLabel = len(mitdb.markpos) # analyse FN curFN,curFP,N_TP = self.getFN_FP(reslist,mitdb.markpos,recID) # debug plot #self.debug_FN_FP(rawsig,reslist,curFN,curFP) self.save_FNFP(curFN,curFP) HumanMarkFolder = r'F:\LabGit\ECG_RSWT\PaperResults\Matlab_MIT_Marker\ver1_0\MIT_keepout_Region'; curFN,curFP = self.filterFNFP_with_humanMarks(curFN,curFP,HumanMarkFolder,Not_In_Range_Mark = False); HumanMarkFolder = r'F:\LabGit\ECG_RSWT\PaperResults\Matlab_MIT_Marker\ver1_0\MIT_keepin_Region'; curFN,curFP = self.filterFNFP_with_humanMarks(curFN,curFP,HumanMarkFolder,Not_In_Range_Mark = True); cFN = len(curFN['pos']) cFP = len(curFP['pos']) # to csv csv.output([[currecname,cFN,cExpLabel,1-float(cFN)/cExpLabel,cFP,N_TP,1-(float(cFP)/(cFP+N_TP))],]) # total stat nFN += cFN nExpLabel += cExpLabel print print 'record[{}] FN rate:{},PD rate:{}'.format(fname,float(cFN)/cExpLabel,1-float(cFN)/cExpLabel) # ================================= # total FN # ================================= print 'Total FN rate:{},PD rate:{}'.format(float(nFN)/nExpLabel,1.0-float(nFN)/nExpLabel)
def FN_stat(csvfilepath = r'MITdb_FalseNegtive_stat.csv'): # analysis false negtive status RFfolder = os.path.join(\ projhomepath,\ 'TestResult',\ 'pc',\ 'r3') # write FN statistics to csv file csv = CSVwriter(csvfilepath) title_line = ['record name','False Negtive number','Expert Label Number'] csv.output([title_line,]) TargetRecordList = ['sel38','sel42',] # ========================== # analysis FN # ========================== match_dist_thres = 50 nFN = 0 nExpLabel = 0 reslist = glob.glob(os.path.join(RFfolder,'*')) N_reslist = len(reslist) for fi,fname in enumerate(reslist): # block *.out # filter file name if fname[-4:] == '.out' or '.json' in fname: continue currecname = os.path.split(fname)[-1] if currecname not in TargetRecordList: pass if not currecname.startswith('result'): continue print 'processing file:',fname print '{} files left.'.format(N_reslist-fi-1) with open(fname,'r') as fin: (recID,reslist) = pickle.load(fin) # load signal cFN = 0 cExpLabel = 0 mitdb = MITdbLoader() rawsig = mitdb.load(recID) # plot res # group result labels resgrper = ECGGrouper(reslist) reslist = resgrper.group_local_result(reslist) res_poslist,res_labellist = zip(*reslist) #-------------------------------------- # for each expert label find its closest match,if distance of this match exceeds match_dist_thres,then it is a classification dismatch. N_res = len(res_poslist) cExpLabel = len(mitdb.markpos) for expmpos in mitdb.markpos: # find the closest match in reslist close_ind = bisect.bisect_left(res_poslist,expmpos) matchdist = abs(res_poslist[close_ind]-expmpos) if close_ind+1<N_res: matchdist = min(matchdist,abs(res_poslist[close_ind+1]-expmpos)) if matchdist>match_dist_thres: cFN+=1 # to csv csv.output([[currecname,cFN,cExpLabel],]) # total stat nFN += cFN nExpLabel += cExpLabel print print 'record[{}] FN rate:{},PD rate:{}'.format(fname,float(cFN)/cExpLabel,1-float(cFN)/cExpLabel) # ================================= # total FN # ================================= print 'Total FN rate:{},PD rate:{}'.format(float(nFN)/nExpLabel,1.0-float(nFN)/nExpLabel)