Пример #1
0
    def test_ini(self, client):
        ini = client.get(
            url_for("init", init_code='invalid_ini_code_sdfsad6324234df'))
        # No existing record, so setup the system.
        assert ini.json == ["", "", 0]
        assert ini.status == '200 OK'

        ini_service = InitialService()
        conn = ini_service.get_connection()
        user_service = UserService(conn)
        user_service.insert("*****@*****.**", "qi chang",
                            "ASDF#$#!!@$!123", "sdfsad6324234df", 0)
        user_service.insert("*****@*****.**", "hua fu", "ASDF#$#!!@$!123",
                            "12345678", 0)

        ini = client.get(
            url_for("init", init_code='invalid_ini_code_sdfsad6324234df'))
        assert ini.json == []
        assert ini.status == '200 OK'

        ini = client.get(url_for("init", init_code='sdfsad6324234df'))

        assert ini.json[0] == "qi chang"
        assert ini.json[1] == "*****@*****.**"
        assert ini.status == '200 OK'
    def test_delete(self):
        ini_service = InitialService()
        conn = ini_service.get_connection()

        instance_service = InstanceService(conn)
        # In case the record doesn't exist.
        try:
            instance_service.insert("Stroke Annotation-del1", "CT", "Stroke annotation descriptions.",
                                    "./data/stroke_instance-del1", 0, 50, 10, 0)
        except Error:
            pass

        last_rowid = instance_service.query({"name": "Stroke Annotation-del1"})[0]["instance_id"]

        labelcandidates_service = LabelCandidatesService(conn)
        labelcandidates_service.insert(last_rowid, 0, None, "Ischemic Stroke")
        candidate_id = labelcandidates_service.query({"text": "Ischemic Stroke", "instance_id": last_rowid})[0][
            "candidate_id"]

        status = labelcandidates_service.delete({"candidate_id": candidate_id})
        assert status is True

        labelcandidates_service.insert(last_rowid, 0, None, "Ischemic Stroke")
        candidate_id = labelcandidates_service.query({"text": "Ischemic Stroke", "instance_id": last_rowid})

        status = labelcandidates_service.delete({"text": "Ischemic Stroke", "instance_id": last_rowid})
        assert status is True

        status = labelcandidates_service.delete({"text": "Ischemic Stroke", "instance_id": last_rowid})
        assert status is False
    def test_update(self):
        ini_service = InitialService()
        conn = ini_service.get_connection()

        instance_service = InstanceService(conn)
        try:
            instance_service.insert("Stroke Annotation-update1", "CT", "Stroke annotation descriptions.",
                                    "./data/stroke_instance-update1", 0, 50, 10, 0)
        except Error:
            pass

        last_rowid = instance_service.query({"name": "Stroke Annotation-update1"})[0]["instance_id"]

        labelcandidates_service = LabelCandidatesService(conn)
        labelcandidates_service.insert(last_rowid, 0, None, "Ischemic Stroke-update1")

        status = labelcandidates_service.update({"instance_id": last_rowid},
                                                {"text": "Ischemic Stroke-update2", "label_type": 1})
        update_item = labelcandidates_service.query({"instance_id": last_rowid})
        assert status is True
        assert update_item[0]["text"] == "Ischemic Stroke-update2"

        status = labelcandidates_service.update({"instance_id": 1000},
                                                {"text": "Ischemic Stroke-update2", "label_type": 1})
        assert status is False
Пример #4
0
    def test_query_and_create(self):
        ini_service = InitialService()
        conn = ini_service.get_connection()

        label_service = LabelService(conn)
        label_result = label_service.query({"series_id": 1, "user_id": 1, "file_id": "2_DCM.dcm"})
        assert len(label_result) == 0
        insert_status = label_service.insert(1, 1, "2_DCM.dcm", "content_data_232rwer2r232erwer2r")
        assert insert_status is True
        label_result = label_service.query({"series_id": 1, "user_id": 1, "file_id": "2_DCM.dcm"})
        assert len(label_result) == 1

        insert_status = label_service.insert(1, 1, "2_DCM.dcm", "content_data2222222_232rwer2r232erwer2r")
        assert insert_status is True
        label_result = label_service.query({"series_id": 1, "user_id": 1, "file_id": "2_DCM.dcm"})
        assert len(label_result) == 1
        assert label_result[0]["content"] == "content_data2222222_232rwer2r232erwer2r"

        insert_status = label_service.insert(1, 1, "3_DCM.dcm", "content_data3333_232rwer2r232erwer2r")
        assert insert_status is True
        label_result = label_service.query({"series_id": 1, "user_id": 1, "file_id": "3_DCM.dcm"})
        assert len(label_result) == 1
        assert label_result[0]["series_id"] == 1
        assert label_result[0]["user_id"] == 1
        assert label_result[0]["file_id"] == "3_DCM.dcm"
        assert label_result[0]["content"] == "content_data3333_232rwer2r232erwer2r"
Пример #5
0
 def test_initial_database_file(self):
     ini_service = InitialService()
     conn = ini_service.get_connection()
     cur = conn.cursor()
     cur.execute('select * from users')
     records = cur.fetchall()
     assert (len(records) == 2)
     has_record = ini_service.has_records()
     assert (has_record is True)
Пример #6
0
    def setup_class(self):
        print("Setup Testing Environment....")
        setup_sql()
        qry = open(os.path.join(DbPool.DATABASE_PATH, "mock_data.sql"), 'r').read()

        ini_service = InitialService()
        conn = ini_service.get_connection()
        cur = conn.cursor()
        cur.executescript(qry)
Пример #7
0
    def test_update(self):
        ini_service = InitialService()
        conn = ini_service.get_connection()
        label_service = LabelService(conn)
        status = label_service.update({"series_id": 1, "user_id": 1, "file_id": "not_exist_DCM.dcm"},
                                      {"content": "asf"})
        assert status is False

        status = label_service.update({"series_id": 1, "user_id": 1, "file_id": "2_DCM.dcm"},
                                      {"content": "asf"})
        assert status is True
Пример #8
0
    def test_initial_database_nofile(self):
        ini_service = InitialService()
        conn = ini_service.get_connection()
        records = conn.cursor().fetchall()
        assert (len(records) == 0)

        has_record = ini_service.has_records()
        assert (has_record is False)

        rowid = mock_data_1(conn)
        assert (rowid != 0)

        has_record = ini_service.has_records()
        assert (has_record is True)
    def test_create(self):
        ini_service = InitialService()
        conn = ini_service.get_connection()

        labelcandidates_service = LabelCandidatesService(conn)

        insert_status = labelcandidates_service.insert(1, 0, None, "Ischemic Stroke")

        assert insert_status is False

        instance_service = InstanceService(conn)
        instance_service.insert("Stroke annotation", "CT", "description...", "./data/stroke_data", 0, 100, 10, 0)
        last_rowid = instance_service.query({"name": "Stroke annotation"})[0]["instance_id"]

        insert_rowid = labelcandidates_service.insert(last_rowid, 0, None, "Ischemic Stroke")
        assert insert_rowid == 1
Пример #10
0
    def test_deleteUser(self):

        ini_service = InitialService()
        conn = ini_service.get_connection()
        user_service = UserService(conn)
        # In case the record doesn't exist.
        try:
            user_service.insert("*****@*****.**", "qi chang",
                                "**(SDFDSF", None, 0)
        except Error:
            pass
        result = user_service.delete("*****@*****.**")
        assert result is True

        result = user_service.delete("*****@*****.**")
        assert result is False
Пример #11
0
    def init(init_code=None):

        ini_service = InitialService()
        has_record = ini_service.has_records()

        if not has_record:
            return jsonify(["", "", 0]), 200
        elif init_code is not None:
            user_service = UserService(get_conn())
            user = user_service.query({"init_code": init_code})
            if len(user) > 0:
                user = user[0]
                return jsonify(
                    [user["name"], user["email"], user["user_type"]]), 200

        return jsonify([]), 200
Пример #12
0
    def test_delete(self):
        ini_service = InitialService()
        conn = ini_service.get_connection()

        label_service = LabelService(conn)

        status = label_service.delete({"series_id": 1, "user_id": 1, "file_id": "not_exist_DCM.dcm"})
        assert status is False

        label_service.insert(1, 1, "2_DCM.dcm", "content_data_232rwer2r232erwer2r")
        label_result = label_service.query({"series_id": 1, "user_id": 1, "file_id": "2_DCM.dcm"})
        assert len(label_result) == 1
        status = label_service.delete({"series_id": 1, "user_id": 1, "file_id": "2_DCM.dcm"})
        assert status is True
        label_result = label_service.query({"series_id": 1, "user_id": 1, "file_id": "2_DCM.dcm"})
        assert len(label_result) == 0
    def test_query_one_record(self):
        ini_service = InitialService()
        conn = ini_service.get_connection()
        instance_service = InstanceService(conn)

        labelcandidates_service = LabelCandidatesService(conn)
        # In case the record doesn't exist.
        try:
            instance_service.insert("Stroke annotation", "CT", "Stroke annotation descriptions.",
                                    "./data/stroke_data", 0, 50, 10, 0)
        except Error:
            print(Error)
            pass
        last_rowid = instance_service.query({"name": "Stroke annotation"})[0]["instance_id"]
        labelcandidates_service.insert(last_rowid, 0, None, "Ischemic Stroke")
        result = labelcandidates_service.query({"instance_id": last_rowid})
        assert result[0]["text"] == "Ischemic Stroke"
Пример #14
0
    def test_instance_detail(self, client):
        ini_service = InitialService()
        conn = ini_service.get_connection()

        label_candidate = LabelCandidatesService(conn)
        label_candidate.insert(1, 0, 0,
                               "['10,10;12,12;14,16;','10,10;12,12;14,16;']")
        label_candidate.insert(1, 1, 1, "['selectoption1','selectoption2']")

        res = client.get(url_for('instance_detail', instance_id=1))

        # Verify instance record items.
        assert res.json["name"] == "Stroke Annotation Task"
        assert res.json["modality"] == "CT"
        assert len(res.json["label_candidates"]) == 2

        res = client.get(url_for('instance_detail', instance_id=1000))

        assert res.json == {}
Пример #15
0
    def test_instance_detail_delete(self, client):
        ini_service = InitialService()
        conn = ini_service.get_connection()

        instance_service = InstanceService(conn)
        instance_id = instance_service.insert(
            "Stroke Annotation Task2", "CT",
            "The stroke CT scans for annotation tasks.",
            "tests/services/sample_data", 0, 200, 1, 0)

        res = client.delete(url_for('instance_detail',
                                    instance_id=instance_id))
        assert res.json == {}
        assert res.status == "200 OK"

        res = client.delete(url_for('instance_detail',
                                    instance_id=instance_id))
        assert res.json == {}
        assert res.status == "404 NOT FOUND"
Пример #16
0
    def test_list_all_studies(self, client):
        res = client.get(url_for('list_all_studies', instance_id=1))
        assert res.json == {}
        assert res.status == '200 OK'

        # Setup studies sample data
        ini_service = InitialService()
        conn = ini_service.get_connection()
        instance_service = InstanceService(conn)
        instance_service.insert("Stroke Annotation Task", "CT",
                                "The stroke CT scans for annotation tasks.",
                                "tests/services/sample_data", 0, 200, 1, 0)
        result = instance_service.query({})
        assert result[0]["instance_id"] == 1
        assert result[0]["name"] == "Stroke Annotation Task"

        studies_service = StudiesService(conn)
        studies_service.insert(1, "Dicom_691_2", 25, 0, "folder", 100, 1)
        studies_service.insert(1, "Raw_1003/3CH_tagging", 30, 1, "folder", 100,
                               1)

        series_service = SeriesService(conn)
        series_service.insert(1, "series1", "series1/path", ["1"], 1, "200",
                              "200", "0.5", "0.5", "0.5", 512, 512, 512, 1,
                              100101, "", "", "")
        series_service.insert(1, "series2", "series2/path", ["1"], 1, "200",
                              "200", "0.5", "0.5", "0.5", 512, 512, 512, 1,
                              100101, "", "", "")
        series_service.insert(2, "series3", "series3/path", ["1"], 1, "200",
                              "200", "0.5", "0.5", "0.5", 512, 512, 512, 1,
                              100101, "", "", "")
        series_service.insert(2, "series4", "series4/path", ["1"], 1, "200",
                              "200", "0.5", "0.5", "0.5", 512, 512, 512, 1,
                              100101, "", "", "")

        res = client.get(url_for('list_all_studies', instance_id=1))
        assert len(res.json[0]) == 2
        assert res.json[0][0]["instance_id"] == 1
        assert res.json[0][0]["patient_uid"] == "Dicom_691_2"
        assert res.json[1][0]["instance_id"] == 1
        assert res.json[1][0]["patient_uid"] == "Raw_1003/3CH_tagging"
        assert res.status == '200 OK'
Пример #17
0
    def test_list_series_in_one_study(self, client):
        ini_service = InitialService()
        conn = ini_service.get_connection()

        series_service = SeriesService(conn)
        series_service.delete({"study_id": 1})
        series_service.delete({"study_id": 2})

        series_service.insert(
            2, "series_description1", "series_path",
            "['export0001.dcm','export0002.dcm','export0003.dcm','export0004.dcm']",
            4, None, None, None, None, None, None, None, None, None, None,
            None, None, None)
        res = client.get(url_for('list_all_series', study_id=1))
        assert res.json == {}

        res = client.get(url_for('list_all_series', study_id=2))
        assert len(res.json) == 1
        assert res.json[0]["study_id"] == 2
        assert res.json[0]["series_description"] == "series_description1"
        assert len(eval(res.json[0]["series_files_list"])) == 4
Пример #18
0
    def test_createUser(self):
        ini_service = InitialService()
        conn = ini_service.get_connection()
        cursor = conn.cursor()

        user_service = UserService(conn)
        user_service.insert("*****@*****.**", "qi chang", "**(SDFDSF",
                            None, 0)

        cursor.execute("SELECT * from users")
        records = cursor.fetchall()
        assert len(records) == 1

        # email is a unique key, should just update record.
        result = user_service.insert("*****@*****.**", "qi chang",
                                     "**(SDFDSF", None, 0)
        assert result is False

        cursor.execute("SELECT * from users")
        records = cursor.fetchall()
        assert len(records) == 1
Пример #19
0
    def test_queryUser(self):
        ini_service = InitialService()
        conn = ini_service.get_connection()
        user_service = UserService(conn)

        # In case the record doesn't exist.
        try:
            user_service.insert("*****@*****.**", "qi chang",
                                "**(SDFDSF", None, 0)
        except Error:
            pass
        result = user_service.query({"email": "*****@*****.**"})

        assert result[0]["email"] == "*****@*****.**"
        assert result[0]["name"] == "qi chang"
        assert "pwd" not in result[0]
        # assert result[0]["pwd"] == '**(SDFDSF'
        assert result[0]["init_code"] is None
        assert result[0]["user_type"] == 0

        result = user_service.query({"email": "*****@*****.**"})
        assert len(result) == 0
Пример #20
0
 def get_conn():
     ini_service = InitialService()
     return ini_service.get_connection()
Пример #21
0
                    content_3D = labeldb[f"study:{study_uid}-series:{series_uid}/label"][()]
                    print(f"Import label for: study:{study_uid}-series:{series_uid}/")
                    for i in range(len(slice_file_name)):
                        content_2D = content_3D[:, :, i]
                        x_dim = content_2D.shape[0]
                        y_dim = content_2D.shape[1]
                        content_1D = np.reshape(content_2D, x_dim * y_dim)

                        unique_id = np.unique(content_1D).tolist()
                        compressed_content_1D = LabelService.compress_content(content_1D)
                        content = {
                            "labelmap2D": {"pixelData": compressed_content_1D, "segmentsOnLabelmap": unique_id,
                                           "dataLength": content_1D.shape[0]}}
                        labelService.insert(series[0]['series_id'], 1, slice_file_name[i],
                                            str.encode(json.dumps(content)))
                    seriesService.update({"series_instance_uid": series_uid}, {SERIES_STATUS.mask_is_ready.value})

        elif load_type == 'mhd':
            raise NotImplementedError()


if __name__ == "__main__":
    ini_service = InitialService()
    importService = ImportService(ini_service.get_connection())
    # importService.import_annotations("h5", "/Users/qichang/PycharmProjects/pytorch-template/data/ACDC/processed/Export-1-Cardial_MRI_DB-0-predict-final-x4.h5")
    importService.import_annotations("h5",
                                     "/ajax/users/qc58/work/projects/pytorch-template/saved/test_results/ACDC_segmentation_exp1/test_result-ACDC_segmentation_exp1_512x512_smooth.h5")

# http://127.0.0.1:5000/load_data/2/NYU_CMR_Raw
# http://127.0.0.1:5000/export_label/studies/2
Пример #22
0
    def test_updateUser(self):
        ini_service = InitialService()
        conn = ini_service.get_connection()
        user_service = UserService(conn)
        try:
            user_service.insert("*****@*****.**", "qi chang",
                                "**(SDFDSF", None, 0)
            user_result = user_service.query(
                {'email': '*****@*****.**'})
            assert user_result[0]['name'] == 'qi chang'
            assert 'pwd' not in user_result
            assert user_result[0]['init_code'] is None
            assert user_result[0]['user_type'] == 0

            user_result = user_service.query({
                'email': '*****@*****.**',
                'pwd': '**(SDFDSF'
            })
            assert user_result[0]['name'] == 'qi chang'
            assert 'pwd' not in user_result
            assert user_result[0]['init_code'] is None
            assert user_result[0]['user_type'] == 0

            user_result = user_service.query({
                'email': '*****@*****.**',
                'pwd': 'WrongPwd'
            })
            assert len(user_result) == 0

        except Error:
            pass
        result = user_service.update("*****@*****.**", {
            "name": "MK",
            "pwd": "pwdpwd",
            "init_code": "*&**I",
            "user_type": 1
        })
        assert result is True
        user_result = user_service.query({'email': '*****@*****.**'})
        assert user_result[0]['name'] == 'MK'
        assert 'pwd' not in user_result
        assert user_result[0]['init_code'] == '*&**I'
        assert user_result[0]['user_type'] == 1

        user_result = user_service.query({
            'email': '*****@*****.**',
            'pwd': 'pwdpwd'
        })
        assert user_result[0]['name'] == 'MK'
        assert 'pwd' not in user_result
        assert user_result[0]['init_code'] == '*&**I'
        assert user_result[0]['user_type'] == 1

        user_result = user_service.query({
            'email': '*****@*****.**',
            'pwd': '**(SDFDSF'
        })
        assert len(user_result) == 0

        # not existing email will return False
        result = user_service.update("*****@*****.**", {
            "name": "MK",
            "pwd": "***",
            "init_code": "*&**I",
            "user_type": 1
        })
        assert result is False

        # unqualified key will ignored.

        result = user_service.update(
            "*****@*****.**", {
                "email": "*****@*****.**",
                "name": "MK",
                "pwd": "***",
                "init_code": "*&**I",
                "user_type": 1
            })
        assert result is True
        result = user_service.query({"email": "*****@*****.**"})
        assert len(result) == 1