def read(self, **query): if 'data_inserimento' in query.keys(): if isinstance(query['data_inserimento'], unicode): query['data_inserimento'] = date_from_json(query['data_inserimento']) if 'id_patient' in query.keys(): query['id_patient'] = self._get_patient(str(query['id_patient'])) if 'id_examination' in query.keys(): query['id'] = query.pop('id_examination') if query != {}: logger.debug(query) examination = Examination.objects(**query) if examination.count() == 0: message = Message(ExaminationNoRecordHeader(), data=examination) return message message = Message(ExaminationCorrectHeader(), data=examination) return message
def _get_correct_examinations(): examination_list = Examination.objects(status_name='completed', data_inserimento=datetime(2016, 2, 2, 0, 0)) patient_id = [e.id_patient.id for e in examination_list] counter = Counter(patient_id) items_ = counter.elements() c = items_.next() while counter[c] <= 2: c = items_.next() return Examination.objects(id_patient=c, data_inserimento=datetime(2016, 2, 2, 0, 0), status_name='completed')
def _check_examination(self, list_id_examinations): if len(list_id_examinations) == 0: message = "No examination" return Message(ReportErrorHeader(message=message), data=None) examination_list = Examination.objects(id__in=list_id_examinations) patient_list = [] data_examination = [] status_list = [] for e in examination_list: patient_list.append(e.id_patient.id) data_examination.append(e.data_inserimento) status_list.append(e.status_name) if len(np.unique(patient_list)) > 1: message = "Examinations are from different patient" logger.debug(np.unique(patient_list)) return Message(ReportErrorHeader(message=message), data=None) if len(np.unique(data_examination)) > 1: message = "Examinations are from different date" logger.debug(np.unique(data_examination)) return Message(ReportErrorHeader(message=message), data=None) if 'closed' in status_list: message = "Examinations are already reported" logger.debug(np.unique(status_list)) return Message(ReportErrorHeader(message=message), data=None) return examination_list
def _get_examination(self, id_examination): """Deprecated""" examination = Examination.objects(id=str(id_examination)) if examination == None: logger.error("No examination in the database") return 'ERR' self._currentExamination = examination.first() return examination
def test_model(self): print "---- Testing Examination ----" patient1 = Patient.objects(first_name="Tecla").first() user1 = User.objects(username='******').first() user2 = User.objects(username='******').first() typology = Typology.objects(modality='MR').first() priority = Priority.objects(priority_name='ALTA').first() examination = Examination(id_patient=patient1, medico_richiedente='Pinco Pallo', data_inserimento=datetime(year=2016, day=02, month=02), id_priority=priority, id_typology=typology, id_creator=user1, id_technician=user2, accession_number='12345665', codice_esenzione='67577568', examination_note='ok') print '----------------------' examination.save() assert examination.status.name == 'new' print '------ Object saved -----------' examination.status.start(examination) assert examination.status_name == 'scheduled' examination.status.go(examination) examination.status.pause(examination) assert examination.status_name == 'incomplete' examination.status.start(examination) examination.status.go(examination) assert examination.status_name == 'running' examination.status.eject(examination) examination.status.stop(examination) assert examination.status_name == 'rescheduled' examination.status.start(examination) assert examination.status_name == 'rescheduled' examination.status.go(examination) examination.status.finish(examination) assert examination.status_name == 'completed' examination.status.eject(examination) assert examination.status_name == 'reported'
def run(database_config, n_loaded=50): db_config = parse_db_config(database_config) _ = easyris_connect(**db_config) #client = connect(database, port=port) Examination.drop_collection() priority_list = Priority.objects() typology_list = Typology.objects() patient_list = Patient.objects() user1 = User.objects(username='******').first() user2 = User.objects(username='******').first() for _ in range(n_loaded): ip = random.randint(0, len(patient_list) - 1) it = random.randint(0, len(typology_list) - 1) ipr = random.randint(0, len(priority_list) - 1) patient = patient_list[ip] typology = typology_list[it] priority = priority_list[ipr] random_number = random.randint(1, 10) if random_number > 5: now = datetime.now() date_ = datetime(year=now.year, month=now.month, day=now.day) if random_number == 8: status_name = 'completed' status = CompletedExaminationStatus() else: status_name = 'new' status = NewExaminationStatus() else: date_ = datetime(year=2016, month=02, day=02) status_name = 'completed' status = CompletedExaminationStatus() examination = Examination(id_patient=patient, medico_richiedente='Mauro Caffarini', data_inserimento=date_, id_priority=priority, id_typology=typology, id_technician=user2, id_creator=user1, status_name=status_name, status=status, codice_esenzione='67577568', examination_note='ok') examination.save()
def run(database_config, n_loaded=50): db_config = parse_db_config(database_config) _ = easyris_connect(**db_config) Report.drop_collection() user = User.objects(username='******').first() examination_list = Examination.objects(status_name='completed', data_inserimento=datetime.datetime(year=2016, month=02, day=02)) # Trovo i pazienti con piu esami id_patient_list = [e.id_patient.id_patient for e in examination_list] counter = collections.Counter(id_patient_list) if len(counter) < n_loaded: n_loaded = len(counter) / 2 print n_loaded for i in range(n_loaded): id_patient = counter.keys()[i] patient = Patient.objects(id_patient=id_patient).first() patient_examination = Examination.objects(id_patient=patient.id, status_name='completed', data_inserimento=datetime.datetime(year=2016, month=02, day=02)) dates_ = np.unique([e.data_inserimento for e in patient_examination]) for d_ in dates_: data_ = datetime.datetime(d_.year, d_.month, d_.day) same_date_examination = Examination.objects(id_patient=patient.id, data_inserimento=data_, status_name='completed') report_log = ReportAction( user=user, action='create' ) report = Report( id_examination = [e for e in same_date_examination], report_text = "Il paziente bla bla bla", action_list = [report_log], id_patient = id_patient, status=SuspendedReportStatus(), status_name='suspended' ) report.save() for e in same_date_examination: e.modify(status=ClosedExaminationStatus(), status_name='closed') e.save()
def test_accession_number(self): print "---- Testing Examination ----" patient1 = Patient.objects(first_name="Tecla").first() user1 = User.objects(username='******').first() user2 = User.objects(username='******').first() typology_qs = Typology.objects(room='RM1.5T', distretto_corporeo='TESTA/COLLO') typology1 = typology_qs[0] typology2 = typology_qs[1] assert typology1.examination_name != typology2.examination_name priority = Priority.objects(priority_name='ALTA').first() examination1 = Examination( id_patient=patient1, medico_richiedente='Pinco Pallo', data_inserimento=datetime(year=2016, day=05, month=02), id_priority=priority, id_typology=typology1, id_creator=user1, id_technician=user2, #accession_number='12345665', codice_esenzione='67577568', examination_note='ok') examination2 = Examination( id_patient=patient1, medico_richiedente='Pinco Pallo', data_inserimento=datetime(year=2016, day=05, month=02), id_priority=priority, id_typology=typology2, id_creator=user1, id_technician=user2, #accession_number='12345665', codice_esenzione='67577568', examination_note='ok') examination1.save() examination2.save() assert examination1.accession_number == examination2.accession_number examination3 = Examination( id_patient=patient1, medico_richiedente='Pinco Pallo', data_inserimento=datetime(year=2016, day=05, month=03), id_priority=priority, id_typology=typology2, id_creator=user1, id_technician=user2, #accession_number='12345665', codice_esenzione='67577568', examination_note='ok') examination3.save() assert examination2.accession_number != examination3.accession_number return
def create(self, **query): pt_controller = PatientController() pt_message = pt_controller.read(id_patient=query['id_patient']) if pt_message.header.code == 101: return pt_message query['id_patient'] = pt_message.data[0] logger.debug(pt_message.data[0].first_name) if 'data_inserimento' in query.keys(): query['data_inserimento'] = parse_date(query['data_inserimento']) logger.debug(query['data_inserimento']) query['id_creator'] = User.objects(username=query['id_creator']).first() list_examination = query.pop('exams') if len(list_examination) == 0: message = Message(ExaminationErrorHeader(message='No Examination in Request')) logger.error(message.header.message) return message examination = None for i, exam_ in enumerate(list_examination): typology = Typology.objects(examination_name=exam_['nome']).first() priority = Priority.objects(priority_name=exam_['priority']).first() query = self._check_fields(query, id_priority=priority, id_typology=typology, ) if not isinstance(query, dict): # It is a message! return query qs = Examination.objects(**query) logger.debug(query) logger.debug("No. of examinations: "+str(len(qs))) if len(qs) != 0: continue examination = Examination(**query) pacs_data = _build_pacs_data(examination) send_to_pacs.apply_async(link_error=pacs_error_handler.s(), kwargs={'data':pacs_data}, ) #send_to_pacs(pacs_data) try: examination.save() except (FieldDoesNotExist, NotUniqueError, SaveConditionError) as err: txt_message = "On %s examination: %s" % (str(i+1), err.message) logger.error(txt_message) message = Message(ExaminationErrorHeader(message=txt_message)) return message logger.debug("Examination created: "+str(examination.data_inserimento)) examination = self._get_examination(examination.id) examination.status = NewExaminationStatus(examination) if examination == None: message = "Examination already stored" examination = qs else: message = 'Examination created correctly' message = Message(ExaminationCorrectHeader(message=message), data=examination) self._currentExamination = examination return message