Esempio n. 1
0
    def test_migrate_institution(self):
        engine = self._get_temp_db(2)
        t_db = TransactionDB(engine)
        last_message = {
            'data': {
                'dicom_info': {
                    't1': {
                        'header': {
                            'InstitutionName': 'MockInstitution'
                        }
                    }
                }
            }
        }
        tr_1 = Transaction()
        tr_1.last_message = json.dumps(last_message)
        t_id = t_db.create_transaction(tr_1)

        # remove institution field
        session = t_db.session
        tr_2 = t_db.get_transaction(t_id)
        tr_2.institution = ''
        session.commit()
        self.assertEqual('', t_db.get_transaction(t_id).institution)

        # execute migrate python script
        model = get_transaction_model(engine)
        migrations.migrate_institution(session, model)
        session.commit()
        tr_2 = t_db.get_transaction(t_id)
        self.assertEqual('MockInstitution', tr_2.institution)

        t_db.close()
Esempio n. 2
0
    def test_migrate_report_qa_2(self):
        engine = self._get_temp_db(5)
        t_db = TransactionDB(engine)
        last_message = {
            'data': {
                'report_qa_score_outcomes': {
                    'mdbrain_nd': 'good',
                    'mdbrain_ms': 'acceptable'
                }
            }
        }
        tr_1 = Transaction()
        tr_1.last_message = json.dumps(last_message)
        t_id = t_db.create_transaction(tr_1)
        tr_1 = t_db.get_transaction(t_id)
        # by default TransactionsDB doesn't set this field
        self.assertEqual(None, tr_1.qa_score)

        # execute migrate python script
        model = get_transaction_model(engine)
        migrations.migrate_qa_scores(t_db.session, model)
        t_db.session.commit()

        tr_2 = t_db.get_transaction(t_id)
        self.assertTrue('mdbrain_ms:acceptable' in tr_2.qa_score)
        self.assertTrue('mdbrain_nd:good' in tr_2.qa_score)
        t_db.close()
Esempio n. 3
0
    def test_migrate_analysis_type_2(self):
        engine = self._get_temp_db(5)
        t_db = TransactionDB(engine)
        last_message = {
            'data': {
                'report_pdf_paths': {
                    'mdbrain_nd': 'path1',
                    'mdbrain_ms': 'path2'
                }
            }
        }
        tr_1 = Transaction()
        tr_1.last_message = json.dumps(last_message)
        t_id = t_db.create_transaction(tr_1)
        tr_1 = t_db.get_transaction(t_id)
        # by default TransactionsDB doesn't set this field
        self.assertEqual(None, tr_1.analysis_type)

        # execute migrate python script
        model = get_transaction_model(engine)
        migrations.migrate_analysis_types(t_db.session, model)
        t_db.session.commit()

        tr_2 = t_db.get_transaction(t_id)
        self.assertTrue('mdbrain_ms' in tr_2.analysis_type)
        self.assertTrue('mdbrain_nd' in tr_2.analysis_type)
        t_db.close()
Esempio n. 4
0
    def test_migrate_study_date(self):
        engine = self._get_temp_db(4)
        t_db = TransactionDB(engine)
        last_message = {
            'data': {
                'dicom_info': {
                    't1': {
                        'header': {
                            'StudyDate': '20190101'
                        }
                    }
                }
            }
        }
        tr_1 = Transaction()
        tr_1.last_message = json.dumps(last_message)
        t_id = t_db.create_transaction(tr_1)
        tr_1 = t_db.get_transaction(t_id)
        # by default TransactionsDB doesn't set this field
        self.assertEqual(None, tr_1.study_date)

        # execute migrate python script
        model = get_transaction_model(engine)
        migrations.migrate_study_date(t_db.session, model)
        t_db.session.commit()

        tr_2 = t_db.get_transaction(t_id)
        self.assertEqual('20190101', tr_2.study_date)

        t_db.close()
Esempio n. 5
0
    def test_migrate_version(self):
        engine = self._get_temp_db(5)
        t_db = TransactionDB(engine)
        last_message = {'data': {'version': '2.2.1'}}
        tr_1 = Transaction()
        tr_1.last_message = json.dumps(last_message)
        t_id = t_db.create_transaction(tr_1)
        tr_1 = t_db.get_transaction(t_id)
        # by default TransactionsDB doesn't set this field
        self.assertEqual(None, tr_1.version)

        # execute migrate python script
        model = get_transaction_model(engine)
        migrations.migrate_version(t_db.session, model)
        t_db.session.commit()

        tr_2 = t_db.get_transaction(t_id)
        self.assertEqual('2.2.1', tr_2.version)
        t_db.close()
Esempio n. 6
0
    def test_migrate_sequences(self):
        engine = self._get_temp_db(3)
        t_db = TransactionDB(engine)
        last_message = {
            'data': {
                'dicom_info': {
                    't1': {
                        'header': {
                            'SeriesDescription': 'T1_sequence'
                        }
                    },
                    't2': {
                        'header': {
                            'SeriesDescription': 'T2_sequence'
                        }
                    }
                }
            }
        }
        tr_1 = Transaction()
        tr_1.last_message = json.dumps(last_message)
        t_id = t_db.create_transaction(tr_1)

        # remove sequences field
        session = t_db.session
        tr_2 = t_db.get_transaction(t_id)
        tr_2.institution = ''
        session.commit()
        self.assertEqual('', t_db.get_transaction(t_id).institution)

        # execute migrate python script
        model = get_transaction_model(engine)
        migrations.migrate_sequences(session, model)
        session.commit()
        tr_2 = t_db.get_transaction(t_id)
        self.assertEqual('T1_sequence;T2_sequence', tr_2.sequences)

        t_db.close()
    def create_transaction(
            self, t: Transaction,
            user_id=None, product_id=None, analysis_type=None,
            qa_score=None,
            processing_state='waiting',
            task_state='queued') -> int:
        """will set the provided transaction object as queued,
        add it to the DB and return the transaction id.

        If the transaction has a last_message JSON with chosen T1/T2,
        it will index the sequence names as well.

        Parameters
        ----------
        user_id: int
        product_id: int
        """
        try:
            if task_state == 'failed':
                t.task_state = TaskState.failed
            else:
                t.task_state = TaskState.queued

            t.processing_state = processing_state
            if not t.creation_date:
                t.creation_date = datetime.datetime.utcnow()
            if product_id:
                t.product_id = product_id
            if analysis_type:
                t.analysis_type = analysis_type
            if qa_score:
                t.qa_score = qa_score
            self.session.add(t)
            # when we commit, we get the transaction ID
            self.session.commit()
            if user_id:
                user = self.session.query(User).get(user_id)
                if not user:
                    raise TransactionDBException(("The provided user doesn't "
                                                  "exist"))
                ut = UserTransaction()
                ut.user_id = user_id
                ut.transaction_id = t.transaction_id
                self.session.add(ut)
            self.session.commit()

            # set the transaction id in the task object
            if t.last_message:
                try:
                    lm = json.loads(t.last_message)
                    lm['t_id'] = t.transaction_id
                    t.last_message = json.dumps(lm)
                except Exception:
                    pass
            # index.set_index_institution(t)
            index.set_index_sequences(t)
            self.session.commit()
            return t.transaction_id
        except Exception:
            self.session.rollback()
            try:
                if(t.transaction_id):
                    self.session.delete(t)
            except:
                pass
            raise