def challenge(record, alarm_type): """Evaluates the presence of a given alarm in a given record""" assert alarm_type in (ASYST, BRAD, TACH, VFLUT, VTACH), ('Unknown alarm type {0}'.format(alarm_type)) annots = MIT.read_annotations(INTERP_DIR + str(record) + '.igqrs') rec = None if alarm_type == VTACH: rec = MIT.load_MIT_record(DB_DIR + str(record)) rec.leads = [VALID_LEAD_NAMES[l] for l in rec.leads] return EVAL_ALARM_F[alarm_type](annots, rec)
parser.add_argument('-c', metavar='cluster', required=True, help=('Extension of the file containing the clustering' ' information.')) parser.add_argument('-o', metavar='oann', default='cls', help=('Save annotations with classified QRS complexes' ' as annotator oann (default: cls)')) args = parser.parse_args() rec = MIT.load_MIT_record(args.r) set_sampling_freq(rec.frequency) print('Classifying record {0}'.format(args.r)) #Reconstruction of the abductive interpretation annots = MIT.read_annotations('{0}.{1}'.format(args.r, args.a)) interp = interp2annots.ann2interp(rec, annots) #Cluster information clusters = load_clustering(args.r, args.c, interp.observations) #QRS feature extraction features = get_features(interp) #Cluster feature extraction for c in clusters: clusters[c] = Cluster(clusters[c], get_cluster_features(clusters[c], features)) #Key function to compare clusters: First, we check clusters with more than #30 beats; then, the clusters with more REGULAR or AFIB beats, and finally #we sort by the number of beats in the cluster keyf = lambda cl, ft=features: (int(len(cl[1].beats) < 30), -ilen( b for b in cl[1].beats if b in ft and ft[b].pos in (REGULAR, AFIB)), -len(cl[1].beats))
ANN = '.iqrs' RHNAMES = { b'(N': 'Normal rhythm', b'(SVTA': 'Tachycardia', b'(SBR': 'Bradycardia', b'(AFIB': 'Atrial Fibrillation', b'(T': 'Trigeminy', b'(B': 'Bigeminy', b'(VFL': 'Ventricular Flutter', b'P': 'Absence of rhythm' } for rec in RECORDS: rhctr = Counter() anns = MIT.read_annotations(PATH + rec + ANN) rhythms = (a for a in anns if a.code in (ECGCodes.RHYTHM, ECGCodes.VFON)) try: start = next(rhythms) except StopIteration: continue print('Interpretation results for record {0}:'.format(rec)) print('Rhythm analysis:') nect = len([a for a in anns if a.aux == b'(EXT']) nbk = len([a for a in anns if a.aux == b'(BK']) ncpt = len([a for a in anns if a.aux == b'(CPT']) while True: end = next(rhythms, anns[-1]) if start.aux in RHNAMES: rhctr[start.aux] += end.time - start.time elif start.code == ECGCodes.VFON:
'2015_BMI/validation/training_dataset/') #DB = '/home/tomas/Escritorio/afdb/' #DB = '/tmp/mit/' ANN = '.iqrs' #ANN = '.ibatr' OANN = '.rhy' RECORDS = [l.strip() for l in open(DB + 'RECORDS')] for rec in RECORDS: if os.path.isfile(DB+rec+OANN): print('Annotator "{0}" already exists. Skipping record {1}'.format( OANN, rec)) continue print('Converting record {0}'.format(rec)) anns = MIT.read_annotations(DB+rec+ANN) record = MIT.load_MIT_record(DB+rec) assert record.frequency == SAMPLING_FREQ record.leads = [VALID_LEAD_NAMES[l] for l in record.leads] interp = i2a.ann2interp(record, anns) afibs = list(interp.get_observations(o.Atrial_Fibrillation)) i = 0 while i < len(afibs): print('{0}/{1}'.format(i,len(afibs))) afib = afibs[i] beats = list(interp.get_observations(o.QRS, filt=lambda q, af=afib: af.earlystart <= q.time.start <= af.lateend)) rpks = np.array([qrs.time.start for qrs in beats]) #We obtain the shape representing the AFIB morphology as the qrs #matching the shape with more other qrss within the rhythm. ctr = collections.Counter()
if __name__ == "__main__": #Config variables PATH = '/tmp/mit/' REF = '.atr' TEST = '.cls' MWIN = ms2sp(150.0) #Records to be interpreted can be selected from command line SLC_STR = '0:48' if len(sys.argv) < 2 else sys.argv[1] #We get a slice from the input string SLC = slice(*[{True: lambda n: None, False: int}[x == ''](x) for x in (SLC_STR.split(':') + ['', '', ''])[:3]]) CMATS = {} for REC in [l.strip() for l in open(PATH + 'RECORDS')][SLC]: print('Record {}'.format(REC)) tp = fn = fp = 0 ref = [a for a in MIT.read_annotations(PATH + REC + REF) if MIT.is_qrs_annotation(a)] for a in ref: a.code = C.aami_to_mit(C.mit_to_aami(a.code)) test = [a for a in MIT.read_annotations(PATH + REC + TEST) if MIT.is_qrs_annotation(a)] for a in test: a.code = C.aami_to_mit(C.mit_to_aami(a.code)) tags = sorted(set(a.code for a in test).union(a.code for a in ref)) #The -1 tag is used for false positives and false negatives. tags.insert(0, -1) cmat = np.zeros((len(tags), len(tags))) i = j = 0 while i < len(ref) and j < len(test): if abs(ref[i].time - test[j].time) <= MWIN: #True positive, introduced in the corresponding matrix cell
ep_seq = tree.find('ns:return/mg_di:diResponse/mg_di:additionalInfo', NS) for episode in ep_seq.findall('ns2:observationResult', NS): rhythm = Rhythm() rhythm.code = RHTAG tp = episode.find('ns2:observationEventTime', NS) start = dateutil.parser.parse(tp.attrib['low']) end = dateutil.parser.parse(tp.attrib['high']) #FIXME we need to ignore timezone for the moment rhythm.start = start.replace(tzinfo=None) rhythm.end = end.replace(tzinfo=None) mbg.add(rhythm) abd = sortedcontainers.SortedList() for f in glob.glob(DB_DIR + devid + '*' + ANN): reftime = MIT.get_datetime(f[:-len(ANN)]) annots = MIT.read_annotations(f) if not annots: continue sig_episodes.add( Iv(reftime, reftime + dt.timedelta(milliseconds=s2m(annots[-1].time)))) for r in (a for a in annots if a.code == ECGCodes.RHYTHM and a.aux == RHTAG): rhythm = Rhythm() rhythm.code = r.aux rhythm.start = reftime + dt.timedelta(milliseconds=s2m(r.time)) end = next((a.time for a in annots if a.time > r.time and a.code == ECGCodes.RHYTHM), annots[-1].time) rhythm.end = reftime + dt.timedelta(milliseconds=s2m(end)) abd.add(rhythm)
return outstr if __name__ == "__main__": DB = '/tmp/mit/' RECORDS = [100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 111, 112, 113, 114, 115, 116, 117, 118, 119, 121, 122, 123, 124, 200, 201, 202, 203, 205, 207, 208, 209, 210, 212, 213, 214, 215, 217, 219, 220, 221, 222, 223, 228, 230, 231, 232, 233, 234] REF_ANN = '.atr' TEST_ANN = '.rhy' epicmpres = {tag : np.zeros(8) for tag in EXCLUSION_TAGS} results = {} for rec in RECORDS: results[rec] = defaultdict(int) ref = MIT.read_annotations(DB + str(rec) + REF_ANN) test = MIT.read_annotations(DB + str(rec) + TEST_ANN) reclen = ref[-1].time #Rhythm loading in form of intervals ref_rhythms = [] test_rhythms = [] for alst in (ref, test): rlst = ref_rhythms if alst is ref else test_rhythms for ann in (a for a in alst if a.code is ECGCodes.RHYTHM): if rlst: rlst[-1].end = ann.time newrhythm = Rhythm() newrhythm.code = ann.aux.strip('\x00') newrhythm.start = ann.time rlst.append(newrhythm) rlst[-1].end = alst[-1].time