Exemple #1
0
    def obtain_tissue_tmax(self):
        cbf_seq = np.zeros((256 * 256))
        tmax_seq = np.zeros((256 * 256))
        comb_seq = np.zeros((256 * 256))
        for ii in tqdm(range(256 * 256)):
            y = int(ii // 256)
            x = int(ii % 256)
            if self.brain_mask[y, x] == 0:
                continue

            tissue_lst = [0] * len(self.img_lst)
            for idx, img in enumerate(self.images):
                tissue_lst[idx] = get_ct_value_neighbor_avg(
                    img, x, y, self.brain_mask, self.img_mask, g_d)

            array_ct_value_tissue = np.array(tissue_lst)
            array_ct_value_tissue_bl = baseline_process(
                array_ct_value_tissue, g_std_t)
            array_ct_value_tissue_bl_f = filter_lp(array_ct_value_tissue,
                                                   cutoff_l=None,
                                                   cutoff_h=0.41,
                                                   ftype="lowpass")
            final_signal_tissue, base_tissue = baseline_correction(
                array_ct_value_tissue_bl_f)
            tmax_seq[ii] = np.argmax(final_signal_tissue)
            cbf_seq[ii] = final_signal_tissue.max()
            comb_seq[ii] = tmax_seq[ii] * cbf_seq[ii]

        return tmax_seq.reshape((256, 256)), cbf_seq.reshape(
            (256, 256)), comb_seq.reshape((256, 256))
Exemple #2
0
    def get_tissue_signal(self):
        output = np.zeros((256, 256, len(self.img_lst)))
        for ii in tqdm(range(256 * 256)):
            y_t_i = int(ii // 256)
            x_t_i = int(ii % 256)

            if self.brain_mask[y_t_i, x_t_i] == 0:
                continue

            length = len(self.img_lst)
            tissue_lst = [0] * length
            for idx, img in enumerate(self.images):
                tissue_lst[idx] = get_ct_value_neighbor_avg(
                    img, x_t_i, y_t_i, self.brain_mask, self.img_mask, g_d)

            array_ct_value_tissue = np.array(tissue_lst)
            # array_ct_value_tissue_bl = baseline_process(array_ct_value_tissue, g_std_t)
            array_ct_value_tissue_bl_f = filter_lp(array_ct_value_tissue,
                                                   cutoff_l=None,
                                                   cutoff_h=0.41,
                                                   ftype="lowpass")
            final_signal_tissue, base_tissue = baseline_correction(
                array_ct_value_tissue_bl_f)
            output[y_t_i, x_t_i, :] = final_signal_tissue

        return output
Exemple #3
0
    def compute_irf_time(self, index):
        '''
        Input:
            index:the index of pixel in flat image
            isPad: expand time axis for extensive distribution of Tmax
        output: Tmax of irf

        Describe:
            if the time of peak in tissue is after the time of peak in aif, tmax = tissue_peak_time - aif_peak_time
            else tmax = total_time - (aif_peak_time - tissue_peak_time)
        '''
        y_t_i = int(index // 256)
        x_t_i = int(index % 256)

        if self.brain_mask[y_t_i, x_t_i] == 0:
            return

        length = len(self.final_signal_aif)

        tissue_lst = [0] * length
        for idx, img in enumerate(self.images):
            tissue_lst[idx] = get_ct_value_neighbor_avg(
                img, x_t_i, y_t_i, self.brain_mask, self.img_mask, g_d)

        array_ct_value_tissue = np.array(tissue_lst)
        array_ct_value_tissue_bl = baseline_process(array_ct_value_tissue,
                                                    g_std_t)
        array_ct_value_tissue_bl_f = filter_lp(array_ct_value_tissue,
                                               cutoff_l=None,
                                               cutoff_h=0.41,
                                               ftype="lowpass")
        final_signal_tissue, base_tissue = baseline_correction(
            array_ct_value_tissue_bl_f)

        aif_delay = np.min(np.where(self.final_signal_aif > 0))
        tissue_delay = np.min(np.where(final_signal_tissue > 0))

        irf_delay = abs(tissue_delay - aif_delay)

        aif_peak = np.argmax(self.final_signal_aif)
        tissue_peak = np.argmax(final_signal_tissue)
        max_aif = np.max(self.final_signal_aif)
        self.cbf_img[y_t_i,
                     x_t_i] = (1 / max_aif) * np.max(final_signal_tissue)
        self.cbv_img[y_t_i, x_t_i] = trapz(
            final_signal_tissue, np.arange(len(final_signal_tissue)),
            dx=1) / trapz(self.final_signal_aif,
                          np.arange(len(self.final_signal_aif)),
                          dx=1)

        if tissue_peak >= aif_peak:
            self.irf_img[y_t_i, x_t_i] += (tissue_peak - aif_peak)
Exemple #4
0
    def get_aif_signal(self):

        ret, img_mask_aif_bin = cv2.threshold(self.aif_mask, 127, 255,
                                              cv2.THRESH_BINARY)
        contours, hierarchy = cv2.findContours(img_mask_aif_bin, cv2.RETR_TREE,
                                               cv2.CHAIN_APPROX_SIMPLE)
        x_aif, y_aif, w_aif, h_aif = cv2.boundingRect(contours[0])

        # ---- get aif tdc array ----
        with parallel_backend('threading', n_jobs=-1):
            aif_lst = Parallel()(delayed(cal_average_aif_value)(
                *[img, x_aif, y_aif, w_aif, h_aif]) for img in self.images)

        array_ct_value_aif = np.array(aif_lst)
        array_ct_value_aif_bl = baseline_process(array_ct_value_aif, g_std_t)
        array_ct_value_aif_bl_f = filter_lp(array_ct_value_aif_bl,
                                            cutoff_l=None,
                                            cutoff_h=0.41,
                                            ftype="lowpass")
        return array_ct_value_aif_bl_f, contours
Exemple #5
0
    def compute_irf(self, index):
        '''
        Input:
            index:the index of pixel in flat image
            isPad: expand time axis for extensive distribution of Tmax
        Output: Tmax of irf

        Describe:
                Compute irf using Tikhonov svd
        '''
        y_t_i = int(index // 256)
        x_t_i = int(index % 256)

        if self.brain_mask[y_t_i, x_t_i] == 0:
            return

        tissue_lst = [0] * len(self.img_lst)
        for idx, img in enumerate(self.images):
            tissue_lst[idx] = get_ct_value_neighbor_avg(
                img, x_t_i, y_t_i, self.brain_mask, self.img_mask, g_d)

        array_ct_value_tissue = np.array(tissue_lst)
        array_ct_value_tissue_bl = baseline_process(array_ct_value_tissue,
                                                    g_std_t)
        array_ct_value_tissue_bl_f = filter_lp(array_ct_value_tissue,
                                               cutoff_l=None,
                                               cutoff_h=0.41,
                                               ftype="lowpass")

        final_signal_tissue, base_tissue = baseline_correction(
            array_ct_value_tissue_bl_f)
        residual_func = deconvolution(self.final_signal_aif,
                                      final_signal_tissue)

        t_idx = np.argmax(residual_func)
        self.irf_img[y_t_i, x_t_i] += t_idx
        self.cbf_img[y_t_i, x_t_i] += np.max(residual_func)
Exemple #6
0
    def show_R_signal(self):
        final_signal_aif, base_aif = baseline_correction(self.aif_seq,
                                                         name="aif")
        for x_t_i in range(100, 256):
            for y_t_i in range(100, 256):
                if self.brain_mask[y_t_i, x_t_i] == 0:
                    continue

                # ---draw contours---
                img = cv2.imread(
                    r"{}/{}".format(self.img_path, self.img_lst[0]),
                    cv2.IMREAD_COLOR)
                img = cv2.drawContours(img, self.aif_contours, 0,
                                       (153, 0, 153), 1)
                img = cv2.drawContours(img, self.mask_contours, 0, (255, 0, 0),
                                       1)
                cv2.rectangle(img, (max(0, x_t_i - 2), max(y_t_i - 2, 0)),
                              (min(256, x_t_i + 2), min(256, y_t_i + 2)),
                              (227, 23, 13), 1)

                # ---Tissue TDC---

                with parallel_backend('threading', n_jobs=-1):
                    tissue_lst = Parallel()(
                        delayed(get_ct_value_neighbor_avg)(*[img, x_t_i, y_t_i, self.brain_mask, self.img_mask, g_d]) \
                        for img in self.images)

                array_ct_value_tissue = np.array(tissue_lst)
                array_ct_value_tissue_bl = baseline_process(
                    array_ct_value_tissue, g_std_t)
                array_ct_value_tissue_bl_f = filter_lp(
                    array_ct_value_tissue_bl,
                    cutoff_l=None,
                    cutoff_h=0.41,
                    ftype="lowpass")
                final_signal_tissue, base_tissue = baseline_correction(
                    array_ct_value_tissue_bl_f)

                # --- Compute IRF ---
                residual_func = deconv_nonparam_alg_tikhonov_svd(
                    self.final_signal_aif,
                    final_signal_tissue,
                    lamdaa=self.lamda)
                residual_func_blur, sig = deconvolution(self.final_signal_aif,
                                                        final_signal_tissue,
                                                        show=True)

                # --- Show Main Peak ---
                #                 residual_func_baseline  = BaselineRemoval(residual_func).IModPoly(2)
                #                 residual_func_baseline[residual_func_baseline < 0] = 0

                #                 peaks, properties = find_peaks(residual_func_baseline, prominence = 0)

                #                 left_bases = properties['left_bases']
                #                 right_bases = properties['right_bases']
                #                 idex = np.argmax(residual_func_baseline[peaks])

                #                 residual_func_baseline[:left_bases[idex]] = 0
                #                 residual_func_baseline[right_bases[idex]:] = 0

                tissue_t = np.argmax(final_signal_tissue)
                aif_t = np.argmax(final_signal_aif)
                print("tissue tmax:", tissue_t, "aif tmax:", aif_t,
                      "irf tmax:", tissue_t - aif_t)

                # inverse aif
                cir_aif = circulant(self.final_signal_aif)
                inver_sig = np.linalg.inv(cir_aif)
                o_sig = inver_sig[0, :].copy()

                pdb.set_trace()
                # --- Plot Image ---
                plt.figure(figsize=(15, 7))

                display.clear_output(wait=True)
                plt.subplot(2, 4, 1)
                plt.imshow(img)
                #                 plt.title("{}, x:{}, y:{}".format(x_t_i, y_t_i))

                plt.subplot(2, 4, 2)
                plt.plot(residual_func, label="irf signal")
                #                 plt.plot(residual_func_blur, label = "irf_blur signal")
                #             plt.legend()
                plt.minorticks_on()
                plt.title('IRF')

                plt.subplot(2, 4, 3)
                plt.plot(array_ct_value_tissue_bl,
                         label="array_tissue_signal_f")
                plt.plot(base_tissue, label="tissue without baseline shift")
                plt.plot(final_signal_tissue, label="final_signal_tissue")
                plt.legend()
                plt.title('Tissue TDC')

                plt.subplot(2, 4, 4)
                plt.plot(final_signal_aif, label="aif signal")
                plt.plot(final_signal_tissue, label="tissue signal")
                plt.plot(sig * 100, label='reg inv_aif')
                plt.legend()
                plt.title('AIF & Tissue TDC')

                plt.subplot(2, 4, 5)
                plt.plot(self.aif_seq, label="array_aif_bl_f")
                plt.plot(base_aif, label="aif without baseline shift")
                plt.plot(final_signal_aif, label="final_signal_aif")
                plt.legend()
                plt.title('AIF TDC')

                plt.subplot(2, 4, 6)
                plt.plot(o_sig * 100, label='inverse aif')
                plt.plot(final_signal_tissue, label='tissue')
                plt.legend()
                plt.title('inverse aif')

                plt.subplot(2, 4, 7)
                plt.plot(residual_func_blur)
                #                 plt.plot(residual_func * 10, label = 'svd irf')
                plt.title('irf blur')

                plt.show()
                plt.pause(0.8)
                plt.close()