def _reg_nae_tconst(pattern, qrs): """ Temporal constraints for regular beats not coming after ectopic beats. """ beats = pattern.evidence[o.QRS] idx = beats.index(qrs) assert not _is_ectopic(idx) hyp = pattern.hypothesis tnet = pattern.last_tnet prev = beats[idx - 1] if idx > 3: #We create a new temporal network for the new trigeminy cycle. tnet.remove_constraint(hyp.end, prev.time) tnet = ConstraintNetwork() pattern.temporal_constraints.append(tnet) rrev = beats[idx - 3].time.start - beats[idx - 4].time.start ##RR evolution constraint. else: rrev = pattern.evidence[o.Cardiac_Rhythm][0].meas.rr[0] tnet.add_constraint(prev.time, qrs.time, Iv(rrev - C.RR_MAX_DIFF, rrev + C.RR_MAX_DIFF)) BASIC_TCONST(pattern, qrs) tnet.add_constraint(qrs.start, qrs.end, C.NQRS_DUR) tnet.set_before(qrs.time, hyp.end) #Constraints with the precedent T Wave _qrs_after_twave(pattern, qrs) #Morphology should be similar to the previous QRS, since both are normal qrs.shape = prev.shape qrs.paced = prev.paced
def test_set_before(self): v0, v1 = [Interval(-1, x) for x in range(2)] nw = ConstraintNetwork() nw.set_before(v0, v1) nw.minimize_network() assert v0 < v1 assert v0.start == v1.start assert v0.start == -1 nw.set_before(v1, v0) nw.minimize_network() assert v0 == v1 assert v0 == v1 assert v0.start == -1
def _deflection_tconst(pattern, defl): """Temporal constraints of the posterior deflections""" defls = pattern.evidence[o.Deflection] idx = defls.index(defl) hyp = pattern.hypothesis tnet = pattern.last_tnet prev = defls[idx - 1] tnet.remove_constraint(hyp.end, prev.time) #We create a new temporal network for the cyclic observations tnet = ConstraintNetwork() tnet.add_constraint(prev.time, defl.time, C.VFLUT_WW_INTERVAL) pattern.temporal_constraints.append(tnet) BASIC_TCONST(pattern, defl) tnet.add_constraint(defl.start, defl.end, Iv(0, C.VFLUT_WW_INTERVAL.end)) tnet.set_before(defl.time, hyp.end)
def _ect_qrs_tconst(pattern, qrs): """ Temporal constraints for ectopic beats, which appear after every regular beat. """ beats = pattern.evidence[o.QRS] idx = beats.index(qrs) tnet = pattern.last_tnet hyp = pattern.hypothesis if idx > 0: prev = beats[idx - 1] #After the second couplet, every ectopic beat introduces a new temporal #network in the pattern to make it easier the minimization. if idx > 3: tnet.remove_constraint(hyp.end, prev.time) #We create a new temporal network for the cyclic observations tnet = ConstraintNetwork() pattern.temporal_constraints.append(tnet) #The duration of each couplet should not have high instantaneous #variations. refrr = beats[idx - 2].time.end - beats[idx - 3].time.start tnet.add_constraint( prev.time, qrs.time, Iv(refrr - C.RR_MAX_DIFF, refrr + C.RR_MAX_DIFF)) #We guide the morphology search to be similar to the previous #ectopic QRS complex. qrs.shape = beats[idx - 2].shape #The reference RR varies from an upper limit to the last measurement, #through the contextual previous rhythm. refrr = C.BRADY_RR.end stdrr = 0.1 * refrr if pattern.evidence[o.Cardiac_Rhythm] and idx == 1: mrr, srr = pattern.evidence[o.Cardiac_Rhythm][0].meas.rr if mrr > 0: refrr, stdrr = mrr, srr elif idx > 1: refrr, stdrr = hyp.meas.rr #Ectopic beats must be advanced wrt the reference RR tnet.add_constraint( prev.time, qrs.time, Iv(C.TACHY_RR.start, max(C.TACHY_RR.start, refrr - stdrr))) #Beats cannot overlap tnet.add_constraint(prev.end, qrs.start, Iv(C.TQ_INTERVAL_MIN, np.Inf)) BASIC_TCONST(pattern, qrs) tnet.add_constraint(qrs.start, qrs.end, C.QRS_DUR) tnet.set_before(qrs.time, hyp.end) #Constraints with the precedent T Wave _qrs_after_twave(pattern, qrs)
def _qrsn_tconst(pattern, qrs): """ Temporal constraints for the QRS complexes. """ beats = pattern.evidence[o.QRS] idx = beats.index(qrs) hyp = pattern.hypothesis tnet = pattern.last_tnet obseq = pattern.obs_seq oidx = pattern.get_step(qrs) prev = beats[idx-1] #In cyclic observations, we have to introduce more networks to simplify #the minimization operation. tnet.remove_constraint(hyp.end, prev.time) tnet = ConstraintNetwork() pattern.temporal_constraints.append(tnet) meanrr, stdrr = pattern.hypothesis.meas.rr rr_bounds = Iv(min(C.ASYSTOLE_RR.start, meanrr-stdrr+C.RR_MAX_DIFF), C.ASYSTOLE_RR.start) tnet.add_constraint(prev.time, qrs.time, rr_bounds) tnet.add_constraint(prev.start, qrs.start, rr_bounds) tnet.add_constraint(prev.end, qrs.end, rr_bounds) tnet.set_before(prev.end, qrs.start) #If there is a prior T Wave, it must finish before the start #of the QRS complex. if isinstance(obseq[oidx-1], o.TWave): prevt = obseq[oidx-1] tnet.set_before(prevt.end, qrs.start) BASIC_TCONST(pattern, qrs) tnet.add_constraint(qrs.start, qrs.end, C.QRS_DUR) tnet.set_before(qrs.time, hyp.end) #We can introduce constraints on the morphology of the new QRS complex. if hyp.morph and not qrs.frozen: qrs.shape = hyp.morph
def _qrs_tconst(pattern, qrs): """ Temporal constraints to observe a new QRS complex. """ beats = pattern.evidence[o.QRS] idx = beats.index(qrs) hyp = pattern.hypothesis tnet = pattern.last_tnet obseq = pattern.obs_seq oidx = pattern.get_step(qrs) #The environment complex sets the start of the rhythm observation. if pattern.get_evidence_type(qrs)[1] is ENVIRONMENT: tnet.set_equal(hyp.start, qrs.time) else: if idx > 0: prev = beats[idx - 1] tnet.remove_constraint(hyp.end, prev.time) #We create a new temporal network for the cyclic observations tnet = ConstraintNetwork() tnet.add_constraint(prev.time, qrs.time, rr_bounds) if rr_bounds is not C.TACHY_RR: #Also bounding on begin and end, but with relaxed variation #margin. rlx_rrb = Iv(rr_bounds.start - C.TMARGIN, rr_bounds.end + C.TMARGIN) tnet.add_constraint(prev.start, qrs.start, rlx_rrb) tnet.add_constraint(prev.end, qrs.end, rlx_rrb) tnet.set_before(prev.end, qrs.start) #If there is a prior T Wave, it must finish before the start #of the QRS complex. if isinstance(obseq[oidx - 1], o.TWave): prevt = obseq[oidx - 1] tnet.set_before(prevt.end, qrs.start) ##RR evolution constraint. We combine the statistical limits #with a dynamic evolution. if idx > 1: prev2 = beats[idx - 2] rrev = prev.time.start - prev2.time.start if hyp.meas.rr[0] > 0: meanrr, stdrr = hyp.meas.rr const = Iv( min(0.8 * rrev, rrev - C.RR_MAX_DIFF, meanrr - 2 * stdrr), max(1.2 * rrev, rrev + C.RR_MAX_DIFF, meanrr + 2 * stdrr)) else: const = Iv(min(0.8 * rrev, rrev - C.RR_MAX_DIFF), max(1.2 * rrev, rrev + C.RR_MAX_DIFF)) tnet.add_constraint(prev.time, qrs.time, const) pattern.temporal_constraints.append(tnet) #TODO improve if not qrs.frozen and hyp.morph: nullsh = o.QRSShape() refbeat = next((b for b in reversed(beats[:idx]) if not b.clustered and all( b.shape.get(lead, nullsh).tag == hyp.morph[lead].tag for lead in hyp.morph)), None) if refbeat is not None: qrs.shape = refbeat.shape qrs.paced = refbeat.paced BASIC_TCONST(pattern, qrs) tnet.add_constraint(qrs.start, qrs.end, C.QRS_DUR) tnet.set_before(qrs.time, hyp.end)