コード例 #1
0
    def test_train_test(self):
        c = Params()
        c.load_config_file(JSON_PATH)

        sp = SlidePredictor(c)
        sp.train_test(file_name="slide_predictor_data_h5.npz",
                      test_name="slide_predictor_testdata_h5.npz")
コード例 #2
0
    def test_extract_testdata_feature(self):
        c = Params()
        c.load_config_file(JSON_PATH)

        sp = SlidePredictor(c)
        Noraml = [
            3, 5, 6, 7, 9, 12, 14, 15, 17, 18, 19, 20, 22, 23, 24, 25, 28, 31,
            32, 34, 35, 36, 37, 39, 41, 42, 43, 44, 45, 47, 49, 50, 53, 54, 55,
            56, 57, 58, 59, 60, 62, 63, 67, 70, 72, 76, 77, 78, 80, 81, 83, 85,
            86, 87, 88, 89, 91, 92, 93, 95, 96, 98, 100, 101, 103, 106, 107,
            109, 111, 112, 114, 115, 118, 119, 120, 123, 124, 125, 126, 127,
            128, 129, 130
        ]
        Tumor = [
            1, 2, 4, 8, 10, 11, 13, 16, 21, 26, 27, 29, 30, 33, 38, 40, 46, 48,
            51, 52, 61, 64, 65, 66, 68, 69, 71, 73, 74, 75, 79, 82, 84, 90, 94,
            97, 99, 102, 104, 105, 108, 110, 113, 116, 117, 121, 122
        ]
        Tumor_names = ["Test_{:0>3d}".format(i) for i in Tumor]
        Normal_names = ["Test_{:0>3d}".format(i) for i in Noraml]
        dim = 18
        feature_data, label_data = sp.extract_slide_features(
            tag=0, normal_names=Normal_names, tumor_names=Tumor_names, DIM=dim)
        sp.save_train_data(
            feature_data,
            label_data,
            filename="slide_predictor_testdata_h{}.npz".format(dim),
            append=False)
コード例 #3
0
    def test_data_augment(self):
        c = Params()
        c.load_config_file(JSON_PATH)

        sp = SlidePredictor(c)
        sp.data_augment("slide_predictor_data_d5.npz",
                        "slide_predictor_augdata_d5.npz",
                        count=500)
コード例 #4
0
    def output_result_csv(self, sub_path, tag, chosen=None):
        '''
        生成用于FROC检测用的CSV文件
        :param chosen: 选择哪些切片的结果将都计算,如果为None,则目录下所有的npz对应的结果将被计算
        :return:
        '''
        project_root = self._params.PROJECT_ROOT
        save_path = "{}/results".format(project_root)

        sp = SlidePredictor(self._params)

        if tag == 0:
            code = "_history.npz"
        else:
            code = "_history_v{}.npz".format(tag)

        K = len(code)

        for result_file in os.listdir(save_path):
            ext_name = os.path.splitext(result_file)[1]
            slice_id = result_file[:-K]
            if chosen is not None and slice_id not in chosen:
                continue

            if ext_name == ".npz" and code in result_file:
                history, label_map, x1, x2, y1, y2 = self.load_history_labelmap(
                    result_file, slice_id, is_load_label_map=False)
                # is_Tumor = sp.predict(history, x1, y1, x2, y2)
                is_Tumor = True

                candidated_result = []
                if is_Tumor:
                    candidated_result = self.search_local_extremum_points_max3(
                        history, x1, y1, x2, y2)

                print("candidated count =", len(candidated_result))

                csv_filename = "{0}/{1}/{2}.csv".format(
                    save_path, sub_path, slice_id)
                with open(csv_filename, 'w', newline='') as f:
                    f_csv = csv.writer(f)
                    for item in candidated_result:
                        f_csv.writerow([item["prob"], item["x"], item["y"]])

                print("Completed ", slice_id)
        return
コード例 #5
0
    def test_extract_feature(self):
        c = Params()
        c.load_config_file(JSON_PATH)

        sp = SlidePredictor(c)

        Tumor_names = ["Tumor_{:0>3d}".format(i)
                       for i in range(1, 112)]  # 1, 112
        Normal_names = ["Normal_{:0>3d}".format(i)
                        for i in range(1, 161)]  # 1, 161
        dim = 18
        feature_data, label_data = sp.extract_slide_features(
            tag=0, normal_names=Normal_names, tumor_names=Tumor_names, DIM=dim)
        sp.save_train_data(feature_data,
                           label_data,
                           filename="slide_predictor_data_h{}.npz".format(dim),
                           append=False)
コード例 #6
0
    def test_calculate_E1(self):
        sp = SlidePredictor(self._params)

        Tumor_names = ["Tumor_{:0>3d}".format(i)
                       for i in range(1, 112)]  # 1, 112
        Normal_names = ["Normal_{:0>3d}".format(i)
                        for i in range(1, 161)]  # 1, 161

        dim = 5
        feature_data, label_data = sp.extract_slide_features(
            tag=0, normal_names=Normal_names, tumor_names=Tumor_names, DIM=dim)

        rand = random.randint(1, 100)
        print("rand", rand)
        X_train, X_test, y_train, y_test = train_test_split(feature_data,
                                                            label_data,
                                                            test_size=0.2,
                                                            random_state=rand)

        max_iter = 10000

        #'kernel': ('linear'),
        parameters = {
            'C': [0.0001, 0.001, 0.01, 0.1, 0.5, 1, 1.2, 1.5, 2.0, 10]
        }

        svc = svm.SVC(
            kernel='linear',
            probability=True,
            max_iter=max_iter,
            verbose=0,
        )
        grid = GridSearchCV(svc, parameters, cv=5)
        grid.fit(X_train, y_train)
        print("The best parameters are %s with a score of %0.2f" %
              (grid.best_params_, grid.best_score_))

        clf = grid.best_estimator_
        sp.save_model(clf, "linearsvm", X_train, y_train, X_test, y_test)