Validate patient identity in breast cohort links
This commit is contained in:
@@ -169,3 +169,20 @@ def test_newer_noneligible_result_changes_revision_and_excludes_patient(client,
|
||||
aggregate = client.get('/cama/analisis/indicadores?corte=2026-10-20').get_json()
|
||||
assert next(g for g in aggregate['grupos'] if g['codigo'] == 'BIRADS_5')['n'] == 5
|
||||
assert len(client.get('/cama/seguimiento' + query(client)).get_json()['items']) == 5
|
||||
|
||||
|
||||
def test_cross_patient_links_and_observations_are_not_used(cohort_db):
|
||||
"""A mismatched patient or an order before the mammogram cannot supply a result."""
|
||||
with Session(cohort_db) as session:
|
||||
# The biopsy order is linked to patient 1's mammogram but belongs to patient 2.
|
||||
session.get(TABLAS.Procedimiento, 102).pac_id = 2
|
||||
# Patient 3's otherwise linked biopsy order predates the mammogram.
|
||||
session.get(TABLAS.Procedimiento, 106).fecha = date(2025, 1, 1)
|
||||
# The BI-RADS observation belongs to a different patient.
|
||||
session.get(TABLAS.Observacion, 6).pac_id = 5
|
||||
session.commit()
|
||||
reader = CamaCohort(session, b'x' * 32)
|
||||
rows = {row['id_seudonimo']: row for row in reader.rows(date(2027, 12, 1))}
|
||||
assert reader._id(6) not in rows
|
||||
assert rows[reader._id(1)]['biopsia'] is None
|
||||
assert rows[reader._id(3)]['biopsia'] is None
|
||||
|
||||
+10
-8
@@ -69,9 +69,9 @@ class CamaCohort:
|
||||
p = procedures.get(identifier)
|
||||
return bool(p and _known(loads.get(p.carga_id), cutoff))
|
||||
|
||||
def observation_known(identifier, result):
|
||||
def observation_known(identifier, result, patient):
|
||||
o = observations.get((identifier, result))
|
||||
return bool(o and _known(loads.get(o.carga_id), cutoff))
|
||||
return bool(o and o.pac_id == patient and _known(loads.get(o.carga_id), cutoff))
|
||||
|
||||
def load_time(record):
|
||||
value = loads.get(record.carga_id) if record else None
|
||||
@@ -82,7 +82,8 @@ class CamaCohort:
|
||||
taken, reported = _date(row['fecha_toma']), _date(row['fecha_reporte'])
|
||||
if not (taken and reported and taken <= reported <= cutoff and row['birads']
|
||||
and procedure_known(row['procedimiento_id']) and procedure_known(row['reporte_id'])
|
||||
and observation_known(row['reporte_id'], 'mastografia.birads')):
|
||||
and procedures[row['reporte_id']].pac_id == row['pac_id']
|
||||
and observation_known(row['reporte_id'], 'mastografia.birads', row['pac_id'])):
|
||||
continue
|
||||
key = (taken, str(row['fuente_id']), reported, row['reporte_id'])
|
||||
prior = candidates.get(row['pac_id'])
|
||||
@@ -101,12 +102,13 @@ class CamaCohort:
|
||||
if key not in orders or rank > orders[key][0]:
|
||||
orders[key] = (rank, p)
|
||||
for row in self.session.execute(select(biopsias(t))).mappings():
|
||||
reported, ordered = _date(row['fecha_reporte']), _date(row['fecha_solicitud'])
|
||||
reported, ordered, taken = (_date(row['fecha_reporte']), _date(row['fecha_solicitud']),
|
||||
_date(row['fecha_toma']))
|
||||
procedure = procedures.get(row['procedimiento_id'])
|
||||
order = procedures.get(procedure.padre_id) if procedure else None
|
||||
if not (reported and ordered and ordered <= reported <= cutoff and row['toma_id']
|
||||
if not (taken and reported and ordered and taken <= ordered <= reported <= cutoff and row['toma_id']
|
||||
and row['pac_id'] == row['toma_pac_id'] and procedure_known(row['procedimiento_id'])
|
||||
and order and procedure_known(order.procedimiento_id)):
|
||||
and order and order.pac_id == row['pac_id'] and procedure_known(order.procedimiento_id)):
|
||||
continue
|
||||
key = (row['pac_id'], row['toma_id'])
|
||||
rank = (reported, str(row['fuente_id']), row['procedimiento_id'])
|
||||
@@ -124,7 +126,7 @@ class CamaCohort:
|
||||
if not order:
|
||||
found_order = orders.get((patient, m['procedimiento_id']))
|
||||
order = found_order[1] if found_order and _date(found_order[1].fecha) >= _date(m['fecha_toma']) else None
|
||||
grade = b['grado'] if b and observation_known(b['procedimiento_id'], 'biopsia.grado') else None
|
||||
grade = b['grado'] if b and observation_known(b['procedimiento_id'], 'biopsia.grado', patient) else None
|
||||
rows.append({
|
||||
'id_seudonimo': self._id(patient), 'mastografia_fecha': _date(m['fecha_toma']).isoformat(),
|
||||
'mastografia_reporte_fecha': _date(m['fecha_reporte']).isoformat(),
|
||||
@@ -137,6 +139,6 @@ class CamaCohort:
|
||||
'biopsia_solicitud_carga_inicio': load_time(order),
|
||||
'biopsia_reporte_carga_inicio': load_time(procedures[b['procedimiento_id']]) if b else None,
|
||||
'biopsia_resultado_carga_inicio': load_time(observations.get((b['procedimiento_id'], 'biopsia.grado')))
|
||||
if b and observation_known(b['procedimiento_id'], 'biopsia.grado') else None,
|
||||
if b and observation_known(b['procedimiento_id'], 'biopsia.grado', patient) else None,
|
||||
})
|
||||
return sorted(rows, key=lambda row: (-date.fromisoformat(row['mastografia_fecha']).toordinal(), row['id_seudonimo']))
|
||||
|
||||
Reference in New Issue
Block a user