def Statistics(request): ''' Query ''' countable_case=uncountable_case=() cases = c8.objects.all() for case in cases: if calculate_age(case.date_of_birth, case.date_of_admission) <= 18 or case.patient_admission_status == 1 or case.type_of_admission == 1 or case.type_of_admission == 2 or case.patient_discharge_status == 1 or case.patient_discharge_status == 3: uncountable_case += (case,) else: countable_case += (case,) ''' Working ''' s_days = hap_days = hf_days = cabg_days = ka_days = ih_days = taa_days = c_days = v_days = () sd_days = hapd_days = hfd_days = cabgd_days = kad_days = ihd_days = taad_days = cd_days = vd_days = () sde_days = hapde_days = hfde_days = cabgde_days = kade_days = ihde_days = taade_days = cde_days = vde_days = () s_dates = hap_dates = hf_dates = cabg_dates = ka_dates = ih_dates = taa_dates = c_dates = v_dates = () for case in countable_case: if not case.diagnosis_group: #Stroke s_days += ((case.date_of_discharge - case.date_of_admission).days+1,) s_dates += (case.date_of_admission,) if case.was_surgical_procedure == 1: sd_days += ((case.date_of_surgical_procedure - case.date_of_admission).days+1,) sde_days += ((case.date_of_discharge - case.date_of_surgical_procedure).days+1,) elif case.diagnosis_group == 1: #Hospital acquired pneumonia hap_days += ((case.date_of_discharge - case.date_of_admission).days+1,) hap_dates += (case.date_of_admission,) if case.was_surgical_procedure == 1: hapd_days += ((case.date_of_surgical_procedure - case.date_of_admission).days+1,) hapde_days += ((case.date_of_discharge - case.date_of_surgical_procedure).days+1,) elif case.diagnosis_group == 2: #Hip fracture hf_days += ((case.date_of_discharge - case.date_of_admission).days+1,) hf_dates += (case.date_of_admission,) if case.was_surgical_procedure == 1: hfd_days += ((case.date_of_surgical_procedure - case.date_of_admission).days+1,) hfde_days += ((case.date_of_discharge - case.date_of_surgical_procedure).days+1,) elif case.diagnosis_group == 3: #CABG cabg_days += ((case.date_of_discharge - case.date_of_admission).days+1,) cabg_dates += (case.date_of_admission,) if case.was_surgical_procedure == 1: cabgd_days += ((case.date_of_surgical_procedure - case.date_of_admission).days+1,) cabgde_days += ((case.date_of_discharge - case.date_of_surgical_procedure).days+1,) elif case.diagnosis_group == 4: #Knee arthroscopy ka_days += ((case.date_of_discharge - case.date_of_admission).days+1,) ka_dates += (case.date_of_admission,) if case.was_surgical_procedure == 1: kad_days += ((case.date_of_surgical_procedure - case.date_of_admission).days+1,) kade_days += ((case.date_of_discharge - case.date_of_surgical_procedure).days+1,) elif case.diagnosis_group == 5: #Inguinal hernia ih_days += ((case.date_of_discharge - case.date_of_admission).days+1,) ih_dates += (case.date_of_admission,) if case.was_surgical_procedure == 1: ihd_days += ((case.date_of_surgical_procedure - case.date_of_admission).days+1,) ihde_days += ((case.date_of_discharge - case.date_of_surgical_procedure).days+1,) elif case.diagnosis_group == 6: #Tonsillectomy and/or adenoidectomy taa_days += ((case.date_of_discharge - case.date_of_admission).days+1,) taa_dates += (case.date_of_admission,) if case.was_surgical_procedure == 1: taad_days += ((case.date_of_surgical_procedure - case.date_of_admission).days+1,) taade_days += ((case.date_of_discharge - case.date_of_surgical_procedure).days+1,) elif case.diagnosis_group == 7: #Cholecystectomy c_days += ((case.date_of_discharge - case.date_of_admission).days+1,) c_dates += (case.date_of_admission,) if case.was_surgical_procedure == 1: cd_days += ((case.date_of_surgical_procedure - case.date_of_admission).days+1,) cde_days += ((case.date_of_discharge - case.date_of_surgical_procedure).days+1,) elif case.diagnosis_group == 8: #Varicose veins - stripping and ligation v_days += ((case.date_of_discharge - case.date_of_admission).days+1,) v_dates += (case.date_of_admission,) if case.was_surgical_procedure == 1: vd_days += ((case.date_of_surgical_procedure - case.date_of_admission).days+1,) vde_days += ((case.date_of_discharge - case.date_of_surgical_procedure).days+1,) ''' Stroke ''' try: s_first_date = sorted(s_dates)[0] except: s_first_date = 0 try: s_last_date = sorted(s_dates, reverse=True)[0] except: s_last_date = 0 try: s_avg = float(sum(s_days)) / len(s_days) except: s_avg = 0 try: s_med = median(s_days) except: s_med = 0 try: sd_avg = float(sum(sd_days)) / len(sd_days) except: sd_avg = 0 try: sd_med = median(sd_days) except: sd_med = 0 try: sde_avg = float(sum(sde_days)) / len(sde_days) except: sde_avg = 0 try: sde_med = median(sde_days) except: sde_med = 0 ''' Hospital acquired pneumonia ''' try: hap_first_date = sorted(hap_dates)[0] except: hap_first_date = 0 try: hap_last_date = sorted(hap_dates, reverse=True)[0] except: hap_last_date = 0 try: hap_avg = float(sum(hap_days)) / len(hap_days) except: hap_avg = 0 try: hap_med = median(hap_days) except: hap_med = 0 try: hapd_avg = float(sum(hapd_days)) / len(hapd_days) except: hapd_avg = 0 try: hapd_med = median(hapd_days) except: hapd_med = 0 try: hapde_avg = float(sum(hapde_days)) / len(hapde_days) except: hapde_avg = 0 try: hapde_med = median(hapde_days) except: hapde_med = 0 ''' Hip fracture ''' try: hf_first_date = sorted(hf_dates)[0] except: hf_first_date = 0 try: hf_last_date = sorted(hf_dates, reverse=True)[0] except: hf_last_date = 0 try: hf_avg = float(sum(hf_days)) / len(hf_days) except: hf_avg = 0 try: hf_med = median(hf_days) except: hf_med = 0 try: hfd_avg = float(sum(hfd_days)) / len(hfd_days) except: hfd_avg = 0 try: hfd_med = median(hfd_days) except: hfd_med = 0 try: hfde_avg = float(sum(hfde_days)) / len(hfde_days) except: hfde_avg = 0 try: hfde_med = median(hfde_days) except: hfde_med = 0 ''' CABG ''' try: cabg_first_date = sorted(cabg_dates)[0] except: cabg_first_date = 0 try: cabg_last_date = sorted(cabg_dates, reverse=True)[0] except: cabg_last_date = 0 try: cabg_avg = float(sum(cabg_days)) / len(cabg_days) except: cabg_avg = 0 try: cabg_med = median(cabg_days) except: cabg_med = 0 try: cabgd_avg = float(sum(cabgd_days)) / len(cabgd_days) except: cabgd_avg = 0 try: cabgd_med = median(cabgd_days) except: cabgd_med = 0 try: cabgde_avg = float(sum(cabgde_days)) / len(cabgde_days) except: cabgde_avg = 0 try: cabgde_med = median(cabgde_days) except: cabgde_med = 0 ''' Knee arthroscopy ''' try: ka_first_date = sorted(ka_dates)[0] except: ka_first_date = 0 try: ka_last_date = sorted(ka_dates, reverse=True)[0] except: ka_last_date = 0 try: ka_avg = float(sum(ka_days)) / len(ka_days) except: ka_avg = 0 try: ka_med = median(ka_days) except: ka_med = 0 try: kad_avg = float(sum(kad_days)) / len(kad_days) except: kad_avg = 0 try: kad_med = median(kad_days) except: kad_med = 0 try: kade_avg = float(sum(kade_days)) / len(kade_days) except: kade_avg = 0 try: kade_med = median(kade_days) except: kade_med = 0 ''' Inguinal hernia ''' try: ih_first_date = sorted(ih_dates)[0] except: ih_first_date = 0 try: ih_last_date = sorted(ih_dates, reverse=True)[0] except: ih_last_date = 0 try: ih_avg = float(sum(ih_days)) / len(ih_days) except: ih_avg = 0 try: ih_med = median(ih_days) except: ih_med = 0 try: ihd_avg = float(sum(ihd_days)) / len(ihd_days) except: ihd_avg = 0 try: ihd_med = median(ihd_days) except: ihd_med = 0 try: ihde_avg = float(sum(ihde_days)) / len(ihde_days) except: ihde_avg = 0 try: ihde_med = median(ihde_days) except: ihde_med = 0 ''' Tonsillectomy and/or adenoidectomy ''' try: taa_first_date = sorted(taa_dates)[0] except: taa_first_date = 0 try: taa_last_date = sorted(taa_dates, reverse=True)[0] except: taa_last_date = 0 try: taa_avg = float(sum(taa_days)) / len(taa_days) except: taa_avg = 0 try: taa_med = median(taa_days) except: taa_med = 0 try: taad_avg = float(sum(taad_days)) / len(taad_days) except: taad_avg = 0 try: taad_med = median(taad_days) except: taad_med = 0 try: taade_avg = float(sum(taade_days)) / len(taade_days) except: taade_avg = 0 try: taade_med = median(taade_days) except: taade_med = 0 ''' Cholecystectomy ''' try: c_first_date = sorted(c_dates)[0] except: c_first_date = 0 try: c_last_date = sorted(c_dates, reverse=True)[0] except: c_last_date = 0 try: c_avg = float(sum(c_days)) / len(c_days) except: c_avg = 0 try: c_med = median(c_days) except: c_med = 0 try: cd_avg = float(sum(cd_days)) / len(cd_days) except: cd_avg = 0 try: cd_med = median(cd_days) except: cd_med = 0 try: cde_avg = float(sum(cde_days)) / len(cde_days) except: cde_avg = 0 try: cde_med = median(cde_days) except: cde_med = 0 ''' Varicose veins - stripping and ligation ''' try: v_first_date = sorted(v_dates)[0] except: v_first_date = 0 try: v_last_date = sorted(v_dates, reverse=True)[0] except: v_last_date = 0 try: v_avg = float(sum(v_days)) / len(v_days) except: v_avg = 0 try: v_med = median(v_days) except: v_med = 0 try: vd_avg = float(sum(vd_days)) / len(vd_days) except: vd_avg = 0 try: vd_med = median(vd_days) except: vd_med = 0 try: vde_avg = float(sum(vde_days)) / len(vde_days) except: vde_avg = 0 try: vde_med = median(vde_days) except: vde_med = 0 ''' Counting ''' diagnosis = ( (_('Stroke'), s_first_date, s_last_date, s_avg, s_med, sd_avg, sd_med, sde_avg, sde_med, len(s_days) ), (_('Hospital acquired pneumonia'), hap_first_date, hap_last_date, hap_avg, hap_med, hapd_avg, hapd_med, hapde_avg, hapde_med, len(hap_days)), (_('Hip fracture'), hf_first_date, hf_last_date, hf_avg, hf_med, hfd_avg, hfd_med, hfde_avg, hfde_med, len(hf_days)), (_('CABG'), cabg_first_date, cabg_last_date, cabg_avg, cabg_med, cabgd_avg, cabgd_med, cabgde_avg, cabgde_med, len(cabg_days)), (_('Knee arthroscopy'), ka_first_date, ka_last_date, ka_avg, ka_med, kad_avg, kad_med, kade_avg, kade_med, len(ka_days)), (_('Inguinal hernia'), ih_first_date, ih_last_date, ih_avg, ih_med, ihd_avg, ihd_med, ihde_avg, ihde_med, len(ih_days)), (_('Tonsillectomy and/or adenoidectomy'), taa_first_date, taa_last_date, taa_avg, taa_med, taad_avg, taad_med, taade_avg, taade_med, len(taa_days)), (_('Cholecystectomy'), c_first_date, c_last_date, c_avg, c_med, cd_avg, cd_med, cde_avg, cde_med, len(c_days)), (_('Varicose veins - stripping and ligation'), v_first_date, v_last_date, v_avg, v_med, vd_avg, vd_med, vde_avg, vde_med, len(v_days)), ) quantity = (2,0,0,0,0,0,0,0,0) ''' Displaying ''' context = { "overall": len(cases), "removed": len(uncountable_case), "counted": len(countable_case), "diagnosis": diagnosis } return render_to_response('c8_statistics.html', context, context_instance=RequestContext(request))
def CountStatistics(cases, notView=True): ''' Query ''' countable_case=uncountable_case=() for case in cases: if calculate_age(case.date_of_birth, case.date_of_admission) <= 18 or case.patient_admission_status == 1 or case.type_of_admission == 1 or case.type_of_admission == 2 or case.patient_discharge_status == 1 or case.patient_discharge_status == 3: uncountable_case += (case,) else: if (case.diagnosis_group == 0 and (case.icd == "I61" or case.icd == "I62" or case.icd == "I63" or case.icd == "I64")) or (case.diagnosis_group == 1 and (case.icd == "J13" or case.icd == "J14" or case.icd == "J15" or case.icd == "J18" or case.icd == "A48.1")) or (case.diagnosis_group == 2 and (case.icd == "S72.0" or case.icd == "S72.1" or case.icd == "S72.2")) or (case.diagnosis_group == 3 and (case.drg == "177A" or case.drg == "177B" or case.drg == "177C" or case.drg == "177D" or case.drg == "190A" or case.drg == "192A" or case.drg == "192B")) or (case.diagnosis_group == 4 and (case.drg == "398A" or case.drg == "398B")) or (case.diagnosis_group == 5 and (case.icd == "K40" or case.drg == "281B" or case.drg == "282A" or case.drg == "282B")) or (case.diagnosis_group == 6 and case.drg == "097A") or (case.diagnosis_group == 7 and (case.drg == "344" or case.drg == "345" or case.drg == "369Z")) or (case.diagnosis_group == 8 and case.drg == "2030"): countable_case += (case,) else: uncountable_case += (case,) if len(countable_case) < 60 and notView: return False ''' Working ''' s_days = hap_days = hf_days = cabg_days = ka_days = ih_days = taa_days = c_days = v_days = () sd_days = hapd_days = hfd_days = cabgd_days = kad_days = ihd_days = taad_days = cd_days = vd_days = () sde_days = hapde_days = hfde_days = cabgde_days = kade_days = ihde_days = taade_days = cde_days = vde_days = () s_dates = hap_dates = hf_dates = cabg_dates = ka_dates = ih_dates = taa_dates = c_dates = v_dates = () for case in countable_case: if not case.diagnosis_group: #Stroke s_days += ((case.date_of_discharge - case.date_of_admission).days+1,) s_dates += (case.date_of_admission,) if case.was_surgical_procedure == 1: sd_days += ((case.date_of_surgical_procedure - case.date_of_admission).days+1,) sde_days += ((case.date_of_discharge - case.date_of_surgical_procedure).days+1,) elif case.diagnosis_group == 1: #Hospital acquired pneumonia hap_days += ((case.date_of_discharge - case.date_of_admission).days+1,) hap_dates += (case.date_of_admission,) if case.was_surgical_procedure == 1: hapd_days += ((case.date_of_surgical_procedure - case.date_of_admission).days+1,) hapde_days += ((case.date_of_discharge - case.date_of_surgical_procedure).days+1,) elif case.diagnosis_group == 2: #Hip fracture hf_days += ((case.date_of_discharge - case.date_of_admission).days+1,) hf_dates += (case.date_of_admission,) if case.was_surgical_procedure == 1: hfd_days += ((case.date_of_surgical_procedure - case.date_of_admission).days+1,) hfde_days += ((case.date_of_discharge - case.date_of_surgical_procedure).days+1,) elif case.diagnosis_group == 3: #CABG cabg_days += ((case.date_of_discharge - case.date_of_admission).days+1,) cabg_dates += (case.date_of_admission,) if case.was_surgical_procedure == 1: cabgd_days += ((case.date_of_surgical_procedure - case.date_of_admission).days+1,) cabgde_days += ((case.date_of_discharge - case.date_of_surgical_procedure).days+1,) elif case.diagnosis_group == 4: #Knee arthroscopy ka_days += ((case.date_of_discharge - case.date_of_admission).days+1,) ka_dates += (case.date_of_admission,) if case.was_surgical_procedure == 1: kad_days += ((case.date_of_surgical_procedure - case.date_of_admission).days+1,) kade_days += ((case.date_of_discharge - case.date_of_surgical_procedure).days+1,) elif case.diagnosis_group == 5: #Inguinal hernia ih_days += ((case.date_of_discharge - case.date_of_admission).days+1,) ih_dates += (case.date_of_admission,) if case.was_surgical_procedure == 1: ihd_days += ((case.date_of_surgical_procedure - case.date_of_admission).days+1,) ihde_days += ((case.date_of_discharge - case.date_of_surgical_procedure).days+1,) elif case.diagnosis_group == 6: #Tonsillectomy and/or adenoidectomy taa_days += ((case.date_of_discharge - case.date_of_admission).days+1,) taa_dates += (case.date_of_admission,) if case.was_surgical_procedure == 1: taad_days += ((case.date_of_surgical_procedure - case.date_of_admission).days+1,) taade_days += ((case.date_of_discharge - case.date_of_surgical_procedure).days+1,) elif case.diagnosis_group == 7: #Cholecystectomy c_days += ((case.date_of_discharge - case.date_of_admission).days+1,) c_dates += (case.date_of_admission,) if case.was_surgical_procedure == 1: cd_days += ((case.date_of_surgical_procedure - case.date_of_admission).days+1,) cde_days += ((case.date_of_discharge - case.date_of_surgical_procedure).days+1,) elif case.diagnosis_group == 8: #Varicose veins - stripping and ligation v_days += ((case.date_of_discharge - case.date_of_admission).days+1,) v_dates += (case.date_of_admission,) if case.was_surgical_procedure == 1: vd_days += ((case.date_of_surgical_procedure - case.date_of_admission).days+1,) vde_days += ((case.date_of_discharge - case.date_of_surgical_procedure).days+1,) ''' Stroke ''' try: s_first_date = sorted(s_dates)[0] except: s_first_date = 0 try: s_last_date = sorted(s_dates, reverse=True)[0] except: s_last_date = 0 try: s_avg = float(sum(s_days)) / len(s_days) except: s_avg = 0 try: s_med = median(s_days) except: s_med = 0 try: sd_avg = float(sum(sd_days)) / len(sd_days) except: sd_avg = 0 try: sd_med = median(sd_days) except: sd_med = 0 try: sde_avg = float(sum(sde_days)) / len(sde_days) except: sde_avg = 0 try: sde_med = median(sde_days) except: sde_med = 0 ''' Hospital acquired pneumonia ''' try: hap_first_date = sorted(hap_dates)[0] except: hap_first_date = 0 try: hap_last_date = sorted(hap_dates, reverse=True)[0] except: hap_last_date = 0 try: hap_avg = float(sum(hap_days)) / len(hap_days) except: hap_avg = 0 try: hap_med = median(hap_days) except: hap_med = 0 try: hapd_avg = float(sum(hapd_days)) / len(hapd_days) except: hapd_avg = 0 try: hapd_med = median(hapd_days) except: hapd_med = 0 try: hapde_avg = float(sum(hapde_days)) / len(hapde_days) except: hapde_avg = 0 try: hapde_med = median(hapde_days) except: hapde_med = 0 ''' Hip fracture ''' try: hf_first_date = sorted(hf_dates)[0] except: hf_first_date = 0 try: hf_last_date = sorted(hf_dates, reverse=True)[0] except: hf_last_date = 0 try: hf_avg = float(sum(hf_days)) / len(hf_days) except: hf_avg = 0 try: hf_med = median(hf_days) except: hf_med = 0 try: hfd_avg = float(sum(hfd_days)) / len(hfd_days) except: hfd_avg = 0 try: hfd_med = median(hfd_days) except: hfd_med = 0 try: hfde_avg = float(sum(hfde_days)) / len(hfde_days) except: hfde_avg = 0 try: hfde_med = median(hfde_days) except: hfde_med = 0 ''' CABG ''' try: cabg_first_date = sorted(cabg_dates)[0] except: cabg_first_date = 0 try: cabg_last_date = sorted(cabg_dates, reverse=True)[0] except: cabg_last_date = 0 try: cabg_avg = float(sum(cabg_days)) / len(cabg_days) except: cabg_avg = 0 try: cabg_med = median(cabg_days) except: cabg_med = 0 try: cabgd_avg = float(sum(cabgd_days)) / len(cabgd_days) except: cabgd_avg = 0 try: cabgd_med = median(cabgd_days) except: cabgd_med = 0 try: cabgde_avg = float(sum(cabgde_days)) / len(cabgde_days) except: cabgde_avg = 0 try: cabgde_med = median(cabgde_days) except: cabgde_med = 0 ''' Knee arthroscopy ''' try: ka_first_date = sorted(ka_dates)[0] except: ka_first_date = 0 try: ka_last_date = sorted(ka_dates, reverse=True)[0] except: ka_last_date = 0 try: ka_avg = float(sum(ka_days)) / len(ka_days) except: ka_avg = 0 try: ka_med = median(ka_days) except: ka_med = 0 try: kad_avg = float(sum(kad_days)) / len(kad_days) except: kad_avg = 0 try: kad_med = median(kad_days) except: kad_med = 0 try: kade_avg = float(sum(kade_days)) / len(kade_days) except: kade_avg = 0 try: kade_med = median(kade_days) except: kade_med = 0 ''' Inguinal hernia ''' try: ih_first_date = sorted(ih_dates)[0] except: ih_first_date = 0 try: ih_last_date = sorted(ih_dates, reverse=True)[0] except: ih_last_date = 0 try: ih_avg = float(sum(ih_days)) / len(ih_days) except: ih_avg = 0 try: ih_med = median(ih_days) except: ih_med = 0 try: ihd_avg = float(sum(ihd_days)) / len(ihd_days) except: ihd_avg = 0 try: ihd_med = median(ihd_days) except: ihd_med = 0 try: ihde_avg = float(sum(ihde_days)) / len(ihde_days) except: ihde_avg = 0 try: ihde_med = median(ihde_days) except: ihde_med = 0 ''' Tonsillectomy and/or adenoidectomy ''' try: taa_first_date = sorted(taa_dates)[0] except: taa_first_date = 0 try: taa_last_date = sorted(taa_dates, reverse=True)[0] except: taa_last_date = 0 try: taa_avg = float(sum(taa_days)) / len(taa_days) except: taa_avg = 0 try: taa_med = median(taa_days) except: taa_med = 0 try: taad_avg = float(sum(taad_days)) / len(taad_days) except: taad_avg = 0 try: taad_med = median(taad_days) except: taad_med = 0 try: taade_avg = float(sum(taade_days)) / len(taade_days) except: taade_avg = 0 try: taade_med = median(taade_days) except: taade_med = 0 ''' Cholecystectomy ''' try: c_first_date = sorted(c_dates)[0] except: c_first_date = 0 try: c_last_date = sorted(c_dates, reverse=True)[0] except: c_last_date = 0 try: c_avg = float(sum(c_days)) / len(c_days) except: c_avg = 0 try: c_med = median(c_days) except: c_med = 0 try: cd_avg = float(sum(cd_days)) / len(cd_days) except: cd_avg = 0 try: cd_med = median(cd_days) except: cd_med = 0 try: cde_avg = float(sum(cde_days)) / len(cde_days) except: cde_avg = 0 try: cde_med = median(cde_days) except: cde_med = 0 ''' Varicose veins - stripping and ligation ''' try: v_first_date = sorted(v_dates)[0] except: v_first_date = 0 try: v_last_date = sorted(v_dates, reverse=True)[0] except: v_last_date = 0 try: v_avg = float(sum(v_days)) / len(v_days) except: v_avg = 0 try: v_med = median(v_days) except: v_med = 0 try: vd_avg = float(sum(vd_days)) / len(vd_days) except: vd_avg = 0 try: vd_med = median(vd_days) except: vd_med = 0 try: vde_avg = float(sum(vde_days)) / len(vde_days) except: vde_avg = 0 try: vde_med = median(vde_days) except: vde_med = 0 ''' Counting ''' diagnosis = [ [_('Stroke'), s_first_date, s_last_date, s_avg, s_med, sd_avg, sd_med, sde_avg, sde_med, len(s_days)], [_('Hospital acquired pneumonia'), hap_first_date, hap_last_date, hap_avg, hap_med, hapd_avg, hapd_med, hapde_avg, hapde_med, len(hap_days)], [_('Hip fracture'), hf_first_date, hf_last_date, hf_avg, hf_med, hfd_avg, hfd_med, hfde_avg, hfde_med, len(hf_days)], [_('CABG'), cabg_first_date, cabg_last_date, cabg_avg, cabg_med, cabgd_avg, cabgd_med, cabgde_avg, cabgde_med, len(cabg_days)], [_('Knee arthroscopy'), ka_first_date, ka_last_date, ka_avg, ka_med, kad_avg, kad_med, kade_avg, kade_med, len(ka_days)], [_('Inguinal hernia'), ih_first_date, ih_last_date, ih_avg, ih_med, ihd_avg, ihd_med, ihde_avg, ihde_med, len(ih_days)], [_('Tonsillectomy and/or adenoidectomy'), taa_first_date, taa_last_date, taa_avg, taa_med, taad_avg, taad_med, taade_avg, taade_med, len(taa_days)], [_('Cholecystectomy'), c_first_date, c_last_date, c_avg, c_med, cd_avg, cd_med, cde_avg, cde_med, len(c_days)], [_('Varicose veins - stripping and ligation'), v_first_date, v_last_date, v_avg, v_med, vd_avg, vd_med, vde_avg, vde_med, len(v_days)], ] ''' Displaying ''' context = { "overall": len(cases), "removed": len(uncountable_case), "counted": len(countable_case), "diagnosis": diagnosis } return context
def CountStatistics(operation_cases, workplace, startDate = False, endDate = False): ''' Query ''' countable_case=uncountable_case=() raw_surgery_data=display_stats=() i=0 for ocase in operation_cases: pdateerrors=ordateerrors=missing_fields=() tn=ame_ami_noc=number_of_cases=aa2=at1=0 mk1=aa1=ami1=mka1=ame1=() #operating rooms if startDate and endDate: patient_cases = c9_patient.objects.filter(added_by__personel__workplace = workplace, central_operating_unit=ocase.central_operating_unit, operating_unit=ocase.operating_unit, date__gte = startDate, date__lte = endDate) else: patient_cases = c9_patient.objects.filter(added_by__personel__workplace = workplace, central_operating_unit=ocase.central_operating_unit, operating_unit=ocase.operating_unit) try: mk2_sat = (datetime.combine(datetime.today(), ocase.saturday_close_time) - datetime.combine(datetime.today(), ocase.saturday_open_time)).seconds * ocase.saturday_staffed_days / 60 except: mk2_sat = 0 try: mk2_sun = (datetime.combine(datetime.today(), ocase.sunday_close_time) - datetime.combine(datetime.today(), ocase.sunday_open_time)).seconds * ocase.sunday_staffed_days / 60 except: mk2_sun = 0 try: mk2_week = (datetime.combine(datetime.today(), ocase.weekday_close_time) - datetime.combine(datetime.today(), ocase.weekday_open_time)).seconds * ocase.weekday_staffed_days / 60 except: mk2_week = 0 mk2 = mk2_sat + mk2_sun + mk2_week if not ocase.saturday_staffed_days: ocase.saturday_staffed_days = 0 if not ocase.sunday_staffed_days: ocase.sunday_staffed_days = 0 at2 = ocase.sunday_staffed_days + ocase.saturday_staffed_days + ocase.weekday_staffed_days limit = (ocase.observation_ends - ocase.observation_begins).days if ocase.observation_ends < ocase.observation_begins or ocase.saturday_open_time > ocase.saturday_close_time or ocase.sunday_open_time > ocase.sunday_close_time or ocase.weekday_open_time > ocase.weekday_close_time: ordateerrors = (ocase.central_operating_unit+" "+ocase.operating_unit,) for pcase in patient_cases: current_error = False if not (pcase.surgery_end and pcase.surgery_start) or pcase.patient_arrive_time > pcase.surgery_start or pcase.surgery_start > pcase.surgery_end or pcase.surgery_end > pcase.patient_leave_time: pdateerrors += (pcase.patient_identifier,) current_error = True #operations in the same operating room if ocase.observation_begins <= pcase.date <= ocase.observation_ends: #operations in the same operating room under observation if pcase.type_of_day == 1: current_close_time = ocase.saturday_close_time current_open_time = ocase.saturday_open_time if not current_close_time and not current_open_time: missing_fields = (pcase.patient_identifier,) break elif pcase.type_of_day == 2: current_close_time = ocase.sunday_close_time current_open_time = ocase.sunday_open_time if not current_close_time and not current_open_time: missing_fields = (pcase.patient_identifier,) break else: current_close_time = ocase.weekday_close_time current_open_time = ocase.weekday_open_time if current_close_time < pcase.patient_leave_time: #overtime tn += 1 patient_leave_time = current_close_time at1 = (datetime.combine(datetime.today(), pcase.patient_leave_time) - datetime.combine(datetime.today(), current_close_time)).seconds / 60 else: patient_leave_time = pcase.patient_leave_time try: if pcase.surgery_end > current_close_time > pcase.surgery_start: mka_surgery_end = current_close_time else: mka_surgery_end = pcase.surgery_end except: mka_surgery_end = pcase.surgery_end if pcase.patient_arrive_time < current_close_time: try: mk1 += ( (datetime.combine(datetime.today(), patient_leave_time) - datetime.combine(datetime.today(), pcase.patient_arrive_time)).seconds / 60,) except: mk1 += (0,) try: mka1 += ( (datetime.combine(datetime.today(), mka_surgery_end) - datetime.combine(datetime.today(), pcase.surgery_start)).seconds / 60,) except: mka1 += (0,) try: aa1 += ((datetime.combine(datetime.today(), pcase.anesthesia_end) - datetime.combine(datetime.today(), pcase.anesthesia_start)).seconds / 60,) aa2 += 1 except: aa1 += (0,) if not current_error: try: ame1 += ((datetime.combine(datetime.today(), pcase.surgery_start) - datetime.combine(datetime.today(), pcase.patient_arrive_time)).seconds / 60,) except: ame1 += (0,) try: ami1 += ( (datetime.combine(datetime.today(), mka_surgery_end) - datetime.combine(datetime.today(), pcase.surgery_start)).seconds / 60,) except: ami1 += (0,) ame_ami_noc += 1 number_of_cases += 1 raw_surgery_data += ({ "name":ocase.central_operating_unit+" "+ocase.operating_unit, 'ordateerrors': ordateerrors, 'pdateerrors': pdateerrors, "tn": tn, 'mk1': mk1, "mk2": mk2, 'mka1': mka1, 'number_of_cases': number_of_cases, 'ame_ami_noc': ame_ami_noc, 'ami1': ami1, 'limit': limit, 'aa1': aa1, 'aa2': aa2, 'ame1': ame1, 'at1': at1, 'at2': at2, 'missing_fields' : missing_fields, },) i += 1 ''' Working & Counting ''' for operating_room in raw_surgery_data: try: mk = float(sum(operating_room['mk1'])) / operating_room['mk2'] * 100 except: mk = 0 try: mka = float(sum(operating_room['mka1'])) / operating_room['mk2'] * 100 except: mka = 0 try: amt = getMinSec( float(sum(operating_room['mk1'])) / operating_room['number_of_cases'] ) except: amt = 0 try: mmt = getMinSec( float(median(operating_room['mk1'])) ) except: mmt = 0 try: aa = getMinSec( float(sum(operating_room['aa1'])) / operating_room['aa2'] ) except: aa = 0 try: ma = getMinSec( float(median(operating_room['aa1'])) ) except: ma = 0 try: ami = getMinSec( float(sum(operating_room['ami1'])) / operating_room['ame_ami_noc'] ) except: ami = 0 try: mmi = getMinSec( float(median(operating_room['ami1'])) ) except: mmi = 0 try: ame = getMinSec( float(sum(operating_room['ame1'])) / operating_room['ame_ami_noc'] ) except: ame = 0 try: mme = getMinSec( float(median(operating_room['ame1'])) ) except: mme = 0 try: at = getMinSec( float(operating_room['at1']) / operating_room['at2'] ) except: at = 0 try: attn = getMinSec( float(operating_room['at1']) / operating_room['tn'] ) except: attn = 0 display_stats += ({ 'name': operating_room['name'], 'slug': "slug_"+slugify(operating_room['name']).replace('-',''), 'tn' : operating_room['tn'], 'mk' : mk, 'cases': operating_room['limit'], 'ordateerrors': operating_room['ordateerrors'], 'pdateerrors': operating_room['pdateerrors'], 'mka' : mka, 'amt' : amt, 'mmt' : mmt, 'aa' : aa, 'ma' : ma, 'ami' : ami, 'mmi' : mmi, 'ame' : ame, 'mme' : mme, 'at' : at, 'attn' : attn, 'missing_fields' : operating_room['missing_fields'], },) i += 1 ''' Displaying ''' context = { "removed": len(uncountable_case), "counted": len(countable_case), "display_stats": display_stats, } return context