Example #1
0
def loop_opening_get_spine(binary_arr,
                           bone_prior=None,
                           output_prefix=None,
                           allow_debug=True,
                           hyper_opening_times=1):

    # calc bone prior
    # sternum_bone_df = pd.DataFrame({})
    _opening_times = 0
    while True:
        # circulation times
        with timer('_________label'):
            label_arr = skimage.measure.label(binary_arr, connectivity=2)
        del binary_arr

        with timer('_________arr to sparse df'):
            # add select objective.
            sparse_df, cluster_df = arr_to_sparse_df(label_arr=label_arr,
                                                     sort=True,
                                                     sort_key='c.count',
                                                     keep_by_top=True,
                                                     top_nth=10,
                                                     keep_by_threshold=True,
                                                     threshold_min=4000)
        del label_arr
        with timer('_________collect spine and judge connected'):
            glb_spine_connected_rib, _remaining_bone_df = judge_collect_spine_judge_connected_rib(
                sparse_df=sparse_df,
                cluster_df=cluster_df,
                bone_prior=bone_prior,
                output_prefix=output_prefix,
                opening_times=_opening_times)

        if output_prefix is not None:
            _remaining_bone_df.to_csv("{}/is_spine_opening_{}th.csv".format(
                output_prefix, _opening_times),
                                      index=False)
        del sparse_df, cluster_df

        if (glb_spine_connected_rib is False) or (_opening_times >=
                                                  hyper_opening_times):
            break
        _opening_times = _opening_times + 1
        with timer('_________sparse df to arr'):
            binary_arr = sparse_df_to_arr(
                arr_expected_shape=bone_prior.get_prior_shape(),
                sparse_df=_remaining_bone_df,
                fill_bool=True)

        with timer('_________binary opening'):
            binary_arr = loop_morphology_binary_opening(
                binary_arr, use_cv=False, opening_times=_opening_times)

    return _remaining_bone_df
Example #2
0
    def detect_multi_ribs(self):

        _, _, y_min = self.get_basic_axis_feature(feature='min')
        _, _, y_max = self.get_basic_axis_feature(feature='max')

        if len(self.bone_data) < 20000:
            return

        if y_min < self.y_mid_line and y_max < self.y_mid_line:
            rib_min_or_max = 'min'
            shadow_threhold = (y_min + 2 * y_max) // 3
        elif y_max > self.y_mid_line and y_min > self.y_mid_line:
            rib_min_or_max = 'max'
            shadow_threhold = (2 * y_min + y_max) // 3
        else:
            return

        # print("############ hello")

        map2d_df = self.bone_data.groupby(['y', 'z']).agg({'x': 'sum'})
        map2d_df.reset_index(inplace=True)

        map2d_image = np.ones((self.arr_shape[0], self.arr_shape[2]))
        map2d_image[(map2d_df['z'].values, map2d_df['y'].values)] = 0

        if rib_min_or_max is 'min':
            map2d_image[:, :shadow_threhold] = 0
        else:
            map2d_image[:, shadow_threhold:] = 0
        """
        plt.imshow(map2d_image)
        plt.title("{}.map2d_image".format(self.bone_data['c'].unique()[0]))
        plt.show()
        """

        label_arr = skimage.measure.label(map2d_image, connectivity=2)
        index = label_arr.nonzero()
        sparse_df = pd.DataFrame({
            'y': index[1],
            'z': index[0],
            'c': label_arr[index]
        })
        cluster_df = sparse_df.groupby('c').agg({'c': ['count']})
        cluster_df.columns = ['c.count']
        max_c_count = cluster_df['c.count'].max()
        cluster_df.reset_index(inplace=True)
        cluster_df.rename(columns={'index': 'c'})
        cluster_df = cluster_df[(cluster_df['c.count'] > 10)
                                & (cluster_df['c.count'] < max_c_count)]

        if len(cluster_df) == 0:
            return
        print("############ hello2")
        self.multi_ribs = True

        multi_ribs_num = len(cluster_df) + 1

        sparse_df = sparse_df[sparse_df['c'].isin(cluster_df['c'].values)]
        thin_line_df = sparse_df.groupby(['c', 'y']).agg({'z': ['mean']})
        thin_line_df.columns = ['z']
        thin_line_df['z'] = thin_line_df['z'].apply(lambda x: np.int(x))
        thin_line_df.reset_index(inplace=True)

        if rib_min_or_max is 'min':
            choose_point = thin_line_df.groupby('c').agg({'y': ['max']})
        else:
            choose_point = thin_line_df.groupby('c').agg({'y': ['min']})
        choose_point.columns = ['y']
        choose_point.reset_index(inplace=True)
        choose_point.rename(columns={'index': 'c'})
        thin_point_df = thin_line_df.merge(choose_point,
                                           on=['c', 'y'],
                                           how='inner')
        thin_point_df.reset_index(inplace=True)
        if rib_min_or_max is 'min':
            thin_point_df.rename(columns={'y': 'y.min'}, inplace=True)
            thin_point_df['y.max'] = y_max
        else:
            thin_point_df.rename(columns={'y': 'y.max'}, inplace=True)
            thin_point_df['y.min'] = y_min

        print("thin_point_df columns :", thin_point_df.columns)

        def make_cartesian(df1=None, df2=None, cartesian_key='cartesian_key'):
            df1[cartesian_key] = 1
            df2[cartesian_key] = 1
            df3 = df1.merge(df2, on=cartesian_key)
            df3.drop([cartesian_key], axis=1, inplace=True)
            return df3

        cartesian_all = make_cartesian(df1=pd.DataFrame(
            {'y': np.arange(y_min, y_max, 1)}),
                                       df2=thin_point_df)

        cartesian_all = cartesian_all[
            (cartesian_all['y'] <= cartesian_all['y.max'])
            & (cartesian_all['y'] >= cartesian_all['y.min'])]
        """
        plt.imshow(map2d_image)
        for e in cartesian_all['c'].unique():
            temp_df = cartesian_all[cartesian_all['c'] == e]
            plt.plot(temp_df['y'], temp_df['z'])
        plt.title("{}.map2d_image_thin_lines".format(self.bone_data['c'].unique()[0]))
        plt.show()
        """

        old_class_id = self.bone_data['c'].unique()[0]
        new_bone_data_df = self.bone_data.merge(cartesian_all,
                                                on=['y', 'z'],
                                                how='left')
        # print("before choose:{},cnt:{}".format(old_class_id, len(new_bone_data_df)))
        new_bone_data_df = new_bone_data_df[
            new_bone_data_df['y.min'].notnull()]
        # print("after choose:{},cnt:{}".format(old_class_id, len(new_bone_data_df)))
        new_bone_data_df.drop(['y.min', 'y.max'], axis=1, inplace=True)

        new_bone_data_3d = sparse_df_to_arr(arr_expected_shape=self.arr_shape,
                                            sparse_df=new_bone_data_df)
        new_bone_data_3d_label = skimage.measure.label(new_bone_data_3d,
                                                       connectivity=2)

        new_bone_data_df, _ = arr_to_sparse_df(
            label_arr=new_bone_data_3d_label,
            sort=True,
            sort_key='c.count',
            keep_by_top=True,
            top_nth=multi_ribs_num)
        new_bone_data_df['c'] = new_bone_data_df['c'].apply(
            lambda x: "{}-{}".format(old_class_id, x))
        self.bone_data = new_bone_data_df
Example #3
0
    def cut_multi_ribs(self):
        _, _, y_min = self.get_basic_axis_feature(feature='min')
        _, _, y_max = self.get_basic_axis_feature(feature='max')

        if ((y_min + y_max) // 2) > self.y_mid_line:
            direction = 'right'
        elif ((y_min + y_max) // 2) < self.y_mid_line:
            direction = 'left'
        else:
            return

        map2d_df = self.bone_data.groupby(['y', 'z']).agg({'x': 'sum'})
        map2d_df.reset_index(inplace=True)

        map2d_image = np.ones((self.arr_shape[0], self.arr_shape[2]))
        map2d_image[(map2d_df['z'].values, map2d_df['y'].values)] = 0

        if direction is 'left':
            map2d_image[:, :((y_min + y_max) // 2)] = 0
        else:
            map2d_image[:, ((y_min + y_max) // 2):] = 0

        label_arr = skimage.measure.label(map2d_image, connectivity=2)
        index = label_arr.nonzero()
        sparse_df = pd.DataFrame({
            'y': index[1],
            'z': index[0],
            'c': label_arr[index]
        })
        cluster_df = sparse_df.groupby('c').agg({'c': ['count']})
        cluster_df.columns = ['c.count']
        max_c_count = cluster_df['c.count'].max()
        cluster_df.reset_index(inplace=True)
        cluster_df.rename(columns={'index': 'c'})
        cluster_df = cluster_df[(cluster_df['c.count'] > 30)
                                & (cluster_df['c.count'] < max_c_count)]

        if len(cluster_df) == 0:
            return

        multi_ribs_num = len(cluster_df) + 1

        sparse_df = sparse_df[sparse_df['c'].isin(cluster_df['c'].values)]

        if direction is 'left':
            thin_line_df = sparse_df.groupby(['c']).agg({'y': ['max']})
        else:
            thin_line_df = sparse_df.groupby(['c']).agg({'y': ['min']})

        thin_line_df.columns = ['y']
        thin_line_df.reset_index(inplace=True)
        thin_line_df.rename(columns={'index': 'c'}, inplace=True)
        thin_line_point = sparse_df.merge(thin_line_df,
                                          on=['c', 'y'],
                                          how='inner')

        if direction is 'left':
            thin_line_point.rename(columns={'y': 'y.min'}, inplace=True)
            thin_line_point['y.max'] = y_max
        else:
            thin_line_point.rename(columns={'y': 'y.max'}, inplace=True)
            thin_line_point['y.min'] = y_min

        def make_cartesian(df1=None, df2=None, cartesian_key='cartesian_key'):
            df1[cartesian_key] = 1
            df2[cartesian_key] = 1
            df3 = df1.merge(df2, on=cartesian_key)
            df3.drop([cartesian_key], axis=1, inplace=True)
            return df3

        cartesian_all = make_cartesian(df1=pd.DataFrame(
            {'y': np.arange(y_min, y_max + 1, 1)}),
                                       df2=thin_line_point)

        cartesian_all = cartesian_all[
            (cartesian_all['y'] >= cartesian_all['y.min'])
            & (cartesian_all['y'] <= cartesian_all['y.max'])]
        """
        plt.imshow(map2d_image)
        for e in cartesian_all['c'].unique():
            temp_df = cartesian_all[cartesian_all['c'] == e]
            plt.plot(temp_df['y'], temp_df['z'])
        plt.title("{}.map2d_image_thin_lines".format(self.bone_data['c'].unique()[0]))
        plt.show()
        """

        old_class_id = self.bone_data['c'].unique()[0]
        new_bone_data_df = self.bone_data.merge(cartesian_all,
                                                on=['y', 'z'],
                                                how='left')
        new_bone_data_df = new_bone_data_df[new_bone_data_df['y.min'].isnull()]
        new_bone_data_df.drop(['y.min', 'y.max'], axis=1, inplace=True)

        new_bone_data_3d = sparse_df_to_arr(arr_expected_shape=self.arr_shape,
                                            sparse_df=new_bone_data_df)
        new_bone_data_3d_label = skimage.measure.label(new_bone_data_3d,
                                                       connectivity=2)

        new_bone_data_df, _ = arr_to_sparse_df(
            label_arr=new_bone_data_3d_label,
            sort=True,
            sort_key='c.count',
            keep_by_top=True,
            top_nth=multi_ribs_num)
        new_bone_data_df['c'] = new_bone_data_df['c'].apply(
            lambda x: "{}-{}".format(old_class_id, x))
        self.bone_data = sparse_df_remove_min(sparse_df=new_bone_data_df,
                                              threshold_min=5000)
Example #4
0
    if not os.path.exists(ribs_df_path):
        print("{} ct not exist".format(_id))
        continue

    ribs_df = pd.read_csv(ribs_df_path,
                          dtype={
                              'x': np.int,
                              'y': np.int,
                              'z': np.int,
                              'c': np.str
                          })

    rib_data = util.sparse_df_to_arr(arr_expected_shape=[
        ribs_df['z'].max() + 30, ribs_df['x'].max() + 30,
        ribs_df['y'].max() + 30
    ],
                                     sparse_df=ribs_df,
                                     fill_bool=True)

    #related_locations = unavail_files[unavail_files['id'] == _id]['location_id'].unique()
    #temp_df = nii_loc_df[nii_loc_df['location_id'].isin(related_locations)]
    temp_df = nii_loc_df[nii_loc_df['id'] == _id]

    fig, ax = plt.subplots(1)
    ax.imshow(rib_data.sum(axis=1))

    for index, row in temp_df.iterrows():
        # exchange x with y, label reason.
        location_id = row['location_id']
        location_part = location_id.split('-')
        y_min, y_max = row['box.y.min'], row['box.y.max']