def add_mother_values_except_arvs(entry): """ This will take care of all of moms values except for her ARV regimens. - date_first_seen - lmp and/or edd - age_at_conception """ begin_date = entry.date_of_outcome - datetime.timedelta(360) preg_encobs = ( Obs.objects.using(FACES_OPENMRS_DB) .filter( person_id=entry.mother_id, voided=0, obs_datetime__gte=begin_date, obs_datetime__lte=entry.date_of_outcome ) .order_by("obs_datetime") ) # Date First Seen dates_first_seen = preg_encobs.filter(concept_id=1836, value_coded=1065) # print "How Many Date First Seens: ", len(dates_first_seen) for seen in dates_first_seen: # print " ", seen.value, " ", seen.obs_datetime entry.date_first_seen = seen.obs_datetime break # 1450 LAST MONTHLY PERIOD DATE lmps = preg_encobs.filter(concept_id=1450) # print "Number LMPS: ", len(lmps) for lmp in lmps: entry.lmp = lmp.value break # 1451 EXPECTED DELIVERY DATE edds = preg_encobs.filter(concept_id=1451) # print "Number EDDS: ", len(edds) for edd in edds: entry.edd = edd.value break # age at conception mother_patient = Patient.objects.using(FACES_OPENMRS_DB).get(pk=entry.mother_id) print "EntryID: ", entry.id, "Motherbirthdate: ", mother_patient.patient.birthdate, " Date first seen: ", entry.date_first_seen if entry.date_first_seen: print type(mother_patient.patient.birthdate) print type(entry.date_first_seen) entry.age_at_conception = calculate_age(mother_patient.patient.birthdate, entry.date_first_seen.date()) elif entry.lmp: entry.age_at_conception = calculate_age(mother_patient.patient.birthdate, entry.lmp) else: # Use the outcome if we absolutely have to entry.age_at_conception = calculate_age(mother_patient.patient.birthdate, entry.date_of_outcome) print "Age: ", entry.age_at_conception
def set_mother_values(entry): """ This will take care of all of moms values except for her ARV regimens. - date_first_seen - lmp and/or edd - age_at_conception """ begin_date = entry.date_of_outcome - datetime.timedelta(360) preg_encobs = entry.motherobs.filter(voided=0, encounter_datetime__gte=begin_date, encounter_datetime__lte=entry.date_of_outcome).order_by('encounter_datetime') for encob in preg_encobs: if encob.concept_id == 1836 and encob.value_coded == 1065: entry.date_first_seen = encob.encounter_datetime.date() # Skip the rest if date_first_seen wasn't available. if entry.date_first_seen == None: return preg_encobs = entry.motherobs.filter(voided=0, encounter_datetime__gte=entry.date_first_seen, encounter_datetime__lte=entry.date_of_outcome).order_by('encounter_datetime') for encob in preg_encobs: if entry.lmp == None and encob.concept_id == 1450: #entry.lmp = encob.value_datetime # TODO I believe faces really only uses LMP For this. pass elif entry.edd == None and encob.concept_id == 1451: entry.edd = encob.value_datetime # age_at_conception mother_patient = Patient.objects.using(FACES_OPENMRS_DB).get(pk=entry.mother_id) print "EntryID: ", entry.id, "Motherbirthdate: ", mother_patient.patient.birthdate, " Date first seen: ", entry.date_first_seen entry.age_at_conception = emrutils.calculate_age(mother_patient.patient.birthdate, entry.date_first_seen) print "Age: ", entry.age_at_conception entry.save(using=APR_DB)
def run378form(f378, cur_apr_id, save_entries=False): logger.info("Starting 378 encounter: %s" % (f378.encounter_id,)) # If we've already done this specific encounter we should skip it and # move on. entry = RegistryEntry() if RegistryEntry.objects.filter(child_initial_enc_id=f378.encounter_id).count() > 0: logger.info("This encounter id was already processed, skipping and moving on.") return entry # 1. Encounter ID entry.child_initial_enc_id = f378.encounter_id # 2. Age in days since encounter entry.age_first_seen = age_in_days_at_encounter(f378) # 3. Child Patient ID entry.child_id = f378.patient_id # Has this child been used previously, if so void the encounter. duplicate_entries = RegistryEntry.objects.filter( site=RegistryEntry.SITE_AMPATH, child_id=entry.child_id) if duplicate_entries.count() > 0: entry.voided = True entry.voided_reason = entry.VOIDED_DUPLICATE_CHILD_ENTRY entry.voided_duplicate = True child_patient = f378.patient # 4. Mom Patient ID entry.mother_id = get_linked_mother(f378) # 5. Length of Mom's ARV History entry.length_of_moms_arv_history = previous_encounters_in_days( entry.mother_id, f378.encounter_datetime.date()) entry.site = RegistryEntry.SITE_AMPATH entry.cohort_date = f378.encounter_datetime.date() entry.outcome = entry.OUTCOME_LIVE entry.date_of_outcome = child_patient.patient.birthdate entry.age_first_seen = (entry.cohort_date - entry.date_of_outcome).days entry.gender = child_patient.patient.gender init_obs = f378.obs_set # Birth Defect Noted """ Field: Birth Defect If concept 6246 or 6245 is used, we use that checkbox value. If the birth defect checkbox is left blank, then we look to see if any of the following concepts are recorded, and if they are we record those birth defects: 8320, 8321, 8322, 8323, 8324, 8325, 8326, 8327, 8328 aprfield""" if init_obs.filter(concept_id=6246, value_coded=6242).count() > 0: entry.birth_defect = RegistryEntry.DEFECT_NO elif init_obs.filter(concept_id=6245, value_coded=6242).count() > 0: entry.birth_defect = RegistryEntry.DEFECT_YES else: congab_concepts = [8320, 8321, 8322, 8323, 8324, 8325, 8326, 8327, 8328] congab_obs = init_obs.filter(concept_id__in = congab_concepts, voided=0) if congab_obs.count() > 0: entry.birth_defect = RegistryEntry.DEFECT_YES else: entry.birth_defect = RegistryEntry.DEFECT_UNKNOWN # Birth Weight weight_obs = init_obs.filter(concept_id=5916) if weight_obs.count() > 0: entry.birth_weight = weight_obs[0].value_numeric * 1000 if save_entries: entry.save(using=APR_DB) if entry.mother_id: entry.check_mother_linked = True mother = Patient.objects.using(AMRS_DB).get( patient_id=entry.mother_id) # Date First Seen... # LMP and/or EDD begin_date = entry.date_of_outcome - datetime.timedelta(360) log_data(entry, "Beginning date of LMP Search: %s" % (begin_date,), 'lmp') lmp_obs = Obs.objects.using(AMRS_DB).filter( concept_id=1836, obs_datetime__gte=begin_date, obs_datetime__lte=entry.date_of_outcome, person_id=entry.mother_id, voided=0, ).order_by('-obs_datetime') if lmp_obs.count() > 0: entry.lmp = lmp_obs[0].value_datetime for lmp in lmp_obs: log_data(entry, "Potential LMP Obs/Date: %s , %s" % (lmp.obs_id, lmp.value_datetime), 'lmp', 1836) # Age at Conception entry.age_at_conception = calculate_age(mother.patient.birthdate, entry.date_of_outcome - datetime.timedelta(days=270)) # Mother ARVS arvset = set_mother_arvs(entry) if len(arvset) == 0: entry.voided = True entry.voided_reason = entry.VOIDED_NO_ARV_HISTORY entry.voided_no_arv_history = True else: entry.check_mother_linked = False entry.voided = True entry.voided_reason = RegistryEntry.VOIDED_MOTHER_NOT_LINKED entry.voided_mother_not_linked = True # If we're not voided assign an APR ID if entry.voided == False: entry.apr_id = cur_apr_id if save_entries: entry.save(using=APR_DB) return entry