예제 #1
0
    def save_ctrl_profiles(self, path, baseline_frames=5):
        """ Masks intensity profiles

        """
        # MASTER MASK PROFILES + DERIVATE PROFILE
        ca_deltaF = edge.deltaF(self.ca_profile())
        prot_deltaF = edge.deltaF(self.prot_profile(mask=self.master_mask))
        derivate_deltaF = edge.deltaF(self.derivate_profile)

        plt.figure(figsize=(15,4))
        plt.plot(self.time_line, ca_deltaF, label='Ca dye profile')
        plt.plot(self.time_line, prot_deltaF, label='FP profile')
        plt.plot(self.time_line[1:], derivate_deltaF, label='Ca dye derivate', linestyle='--')
        plt.plot(np.take(self.time_line[1:], self.stim_peak), np.take(derivate_deltaF, self.stim_peak), 'v',
                 label='stimulation peak', markersize=10, color='red')
        
        plt.grid(visible=True, linestyle=':')
        plt.xlabel('Time (s)')
        plt.ylabel('ΔF/F')
        plt.xticks(np.arange(0, np.max(self.time_line)+2, step=1/self.time_scale))
        plt.legend()
        plt.tight_layout()
        plt.suptitle(f'Master mask int profile, {self.img_name}, {self.stim_power}%', fontsize=20)
        plt.tight_layout()
        plt.savefig(f'{path}/{self.img_name}_profile_ca.png')

        # UP REGIONS PROFILES + CA PROFILE
        best_mask = self.up_diff_mask[self.best_up_mask_index]
        best_mask_total = best_mask != 0
        # best_mask_down = self.down_diff_mask[self.best_up_mask_index]

        plt.figure(figsize=(15, 4))
        plt.plot(self.time_line, edge.deltaF(self.prot_profile(mask=best_mask_total)),
                 label='total up mask', linestyle='--', linewidth=3, color='white')
        # plt.plot(self.time_line, edge.deltaF(self.prot_profile(mask=best_mask_down)),
        #          label='total down mask', linestyle=':', linewidth=3, color='white')
        plt.plot(np.take(self.time_line[1:], self.stim_peak), [0.5 for i in self.stim_peak], 'v',
                 label='stimulation peak', markersize=10, color='red')

        for up_region_lab in range(1, np.max(best_mask)+1):
            up_region_mask = best_mask == up_region_lab
            up_region_profile = edge.deltaF(self.prot_profile(mask=up_region_mask))
            plt.plot(self.time_line, up_region_profile, label=f'region {up_region_lab}', linewidth=0.75)

        plt.grid(visible=True, linestyle=':')
        plt.xlabel('Time (s)')
        plt.ylabel('ΔF/F')
        plt.xticks(np.arange(0, np.max(self.time_line)+2, step=1/self.time_scale))
        plt.legend()
        plt.tight_layout()
        plt.suptitle(f'Best up mask regions profiles, {self.img_name}, {self.stim_power}%', fontsize=20)
        plt.tight_layout()
        plt.savefig(f'{path}/{self.img_name}_profile_up.png')


        plt.close('all')
        logging.info(f'{self.img_name} control profiles saved!')
예제 #2
0
    def find_stimul_peak(self, h=0.15, d=3, l_lim=5, r_lim=18):
        """ Require master_mask, results of get_master_mask!
        Find peaks on deltaF/F Fluo-4 derivate profile.
        h - minimal peaks height, in ΔF/F0
        d - minimal distance between peaks, in frames

        """
        self.stim_peak = find_peaks(edge.deltaF(self.derivate_profile), height=h, distance=d)[0]
        self.stim_peak = self.stim_peak[(self.stim_peak >= l_lim) & (self.stim_peak <= r_lim)]  # filter outer peaks
        logging.info(f'Detected peaks: {self.stim_peak}')
예제 #3
0
    def updown_mask_int(self,
                        up_mask,
                        down_mask,
                        delta=False,
                        plot_path=False):
        """ Calculation mean intensity in masked area along frames series.
        Require two masks, increasing and decreasing regions.

        """
        up_list = [
            round(np.sum(ma.masked_where(~up_mask, img)) / np.sum(up_mask), 3)
            for img in self.img_series
        ]
        down_list = [
            round(
                np.sum(ma.masked_where(~down_mask, img)) / np.sum(down_mask),
                3) for img in self.img_series
        ]

        if delta:  # calculate ΔF/F0
            up_list = edge.deltaF(up_list)
            down_list = edge.deltaF(down_list)

        return np.asarray(up_list), np.asarray(down_list)
예제 #4
0
    # blue/red derivate images
    der_int = edge.series_derivate(cell.img_series,
                                   mask= 'full_frame',  # cell.mask_series[cell.max_frame_num],
                                   sd_mode='cell',
                                   sd_tolerance=False,
                                   sigma=1, kernel_size=3,
                                   left_w=1, space_w=0, right_w=1)  # ,
                                   # output_path=cell_path)
    amp_series = [np.sum(np.abs(der_int[i])) for i in range(len(der_int))]

    # control image of the cell with native image of max frame and hysteresis binary mask
    cell.save_ctrl_img(path=res_path)
    cell_int = cell.max_mask_int(plot_path=cell_path)

    # calc relative amount in up mask
    up_rel = edge.deltaF(up_int / cell_int, f_0_win=10)
    down_rel = edge.deltaF(down_int / cell_int, f_0_win=10)

    # calc delta F for mask series
    cell_delta = edge.deltaF(cell_int, f_0_win=10)
    up_delta = edge.deltaF(up_int, f_0_win=10)
    down_delta = edge.deltaF(down_int, f_0_win=10)
    # group all results by mask
    maskres_dict = {'cell':[cell_int, cell_delta, cell_int],
                 'up':[up_int, up_delta, up_rel],
                 'down':[down_int, down_delta, down_rel]}

    # saving results to CSV
    if save_csv:
      for val_num in range(len(cell_int)):
          for maskres_key in maskres_dict.keys():
예제 #5
0
#     for single_num in range(len(series_int)):
#         single_int = series_int[single_num]
#         df = df.append(pd.Series([int(cell_num+1), cell.exposure, cell.cycles, int(single_num+1), single_int],
#                        index=df.columns),
#                        ignore_index=True)


# PA in loading solution experiment (1 - no PA, 2 - with PA)
df = pd.DataFrame(columns=['cell', 'power', 'time', 'int'])
for cell_num in range(0, len(all_cells)):
    cell = all_cells[cell_num]
    logging.info('Image {} in progress'.format(cell.img_name))

    series_int, mask, gauss = cell.relInt(high_lim=0.8, init_low=0.05, mask_diff=40, sigma=3, noise_size=40)

    series_int = edge.deltaF(series_int)

    # try:                            # register exceptions from lowHyst function
    # 	series_int, mask, gauss = cell.relInt(high_lim=0.8, init_low=0.05, mask_diff=40, sigma=3, noise_size=40)
    # except RuntimeError:
    # 	logging.fatal('For image {} relative intensity DON`T calculated, RE!\n'.format(cell.img_name))
    # 	continue
    # except ValueError:
    # 	logging.fatal('For image {} relative intensity DON`T calculated, VE!\n'.format(cell.img_name))
    # 	continue

    for single_num in range(len(series_int)):
        single_int = series_int[single_num]
        df = df.append(pd.Series([cell.img_name, cell.feature, int(single_num+1), single_int],
                       index=df.columns),
                       ignore_index=True)
예제 #6
0
    def save_profile_df(self, id_suffix):
        """ Masked regions intensity profiles data frame

        """
        self.profile_df = pd.DataFrame(columns=['ID',           # recording ID
                                                'power',        # 405 nm stimulation power (%)
                                                'ch',           # channel (FP or Ca dye)
                                                'frame',        # frame number
                                                'time',         # frame time (s)
                                                'mask',         # mask type (master, up, down)
                                                'mask_region',  # mask region (1 for master or down)
                                                'mean',         # mask mean intensity
                                                'delta',        # mask ΔF/F
                                                'rel'])         # mask sum / master mask sum
        # Ca dye
        ca_profile = self.ca_profile()
        ca_profile_delta = edge.deltaF(ca_profile, f_0_win=self.baseline_frames)
        for ca_val in range(0, len(self.ca_series)):
            point_series = pd.Series([f'{self.img_name}{id_suffix}', # recording ID
                                              self.stim_power,               # 405 nm stimulation power (%)
                                              'ca',                          # channel (FP or Ca dye)
                                              ca_val+1,                      # frame number
                                              self.time_line[ca_val],        # frame time (s)
                                              'master',                      # mask type (only master for Ca dye channel)
                                              1,                             # mask region (1 for master or down)
                                              ca_profile[ca_val],            # mask mean intensity
                                              ca_profile_delta[ca_val],      # mask ΔF/F
                                              1],                            # mask sum / master mask sum (1 for Ca dye channel)
                                      index=self.profile_df.columns)
            self.profile_df = self.profile_df.append(point_series, ignore_index=True)
        
        # FP intensity
        # FP master
        fp_master_profile = self.prot_profile(mask=self.master_mask)
        fp_master_profile_delta = edge.deltaF(fp_master_profile, f_0_win=self.baseline_frames)
        fp_master_sum = np.asarray([round(np.sum(ma.masked_where(~self.master_mask, img)), 3) for img in self.prot_series]) # intensity sum for master mask

        for fp_val in range(0, len(self.prot_series)):
            point_series = pd.Series([f'{self.img_name}{id_suffix}',            # recording ID
                                              self.stim_power,                  # 405 nm stimulation power (%)
                                              'fp',                             # channel (FP or Ca dye)
                                              fp_val+1,                         # frame number
                                              self.time_line[fp_val],           # frame time (s)
                                              'master',                         # mask type (only master for Ca dye channel)
                                              1,                                # mask region (1 for master or down)
                                              fp_master_profile[fp_val],        # mask mean intensity
                                              fp_master_profile_delta[fp_val],  # mask ΔF/F
                                              1],                               # mask sum / master mask sum (1 for Ca dye channel)
                                     index=self.profile_df.columns)
            self.profile_df = self.profile_df.append(point_series, ignore_index=True)

        # FP down regions
        fp_down_profile = self.prot_profile(mask=self.down_diff_mask[self.best_up_mask_index])
        fp_down_profile_delta = edge.deltaF(fp_down_profile, f_0_win=self.baseline_frames)
        fp_down_sum = np.asarray([round(np.sum(ma.masked_where(~self.down_diff_mask[self.best_up_mask_index], img)), 3) for img in self.prot_series])
        for fp_val in range(0, len(self.prot_series)):
            point_series = pd.Series([f'{self.img_name}{id_suffix}',                               # recording ID
                                              self.stim_power,                                     # 405 nm stimulation power (%)
                                              'fp',                                                # channel (FP or Ca dye)
                                              fp_val+1,                                            # frame number
                                              self.time_line[fp_val],                              # frame time (s)
                                              'down',                                              # mask type (only master for Ca dye channel)
                                              1,                                                   # mask region (1 for master or down)
                                              fp_down_profile[fp_val],                             # mask mean intensity
                                              fp_down_profile_delta[fp_val],                       # mask ΔF/F
                                              fp_down_sum[fp_val]/fp_master_sum[fp_val]],  # mask sum / master mask sum (1 for Ca dye channel)
                                     index=self.profile_df.columns)
            self.profile_df = self.profile_df.append(point_series, ignore_index=True)

        # FP up regions
        best_up_mask = self.up_diff_mask[self.best_up_mask_index]
        best_up_mask_prop = measure.regionprops(best_up_mask)

        fp_up_profile_dict = {}
        for i in best_up_mask_prop:  # calculate profiles for each up region
            best_up_mask_region = best_up_mask == i.label
            fp_up_profile = self.prot_profile(mask=best_up_mask_region)
            fp_up_profile_delta = edge.deltaF(fp_up_profile, f_0_win=self.baseline_frames)
            fp_up_sum = np.asarray([round(np.sum(ma.masked_where(~best_up_mask_region, img)), 3) for img in self.prot_series])

            fp_up_profile_dict.update({i.label: [fp_up_profile, fp_up_profile_delta, fp_up_sum]})
        for fp_val in range(0, len(self.prot_series)):
            for up_region_key in fp_up_profile_dict.keys():
                up_region = fp_up_profile_dict.get(up_region_key)

                point_series = pd.Series([f'{self.img_name}{id_suffix}',                    # recording ID
                                          self.stim_power,                                  # 405 nm stimulation power (%)
                                          'fp',                                             # channel (FP or Ca dye)
                                          fp_val+1,                                         # frame number
                                          self.time_line[fp_val],                           # frame time (s)
                                          'up',                                             # mask type (only master for Ca dye channel)
                                          up_region_key,                                    # mask region (1 for master or down)
                                          up_region[0][fp_val],                             # mask mean intensity
                                          up_region[1][fp_val],                             # mask ΔF/F
                                          up_region[2][fp_val]/fp_master_sum[fp_val]],      # mask sum / master mask sum (1 for Ca dye channel)
                                        index=self.profile_df.columns)
                self.profile_df = self.profile_df.append(point_series, ignore_index=True)

        logging.info(f'Recording profile data frame {self.profile_df.shape} created')
        return self.profile_df
예제 #7
0
        sd_mode='cell',
        sd_tolerance=False,
        sigma=1,
        kernel_size=3,
        left_w=2,
        space_w=1,
        right_w=2)  #,
    # output_path=cell_path)
    amp_series = [np.sum(np.abs(der_int[i])) for i in range(len(der_int))]

    # control image of the cell with native image of max frame and hysteresis binary mask
    cell.save_ctrl_img(path=res_path)
    cell_int = cell.max_mask_int(plot_path=cell_path)

    # calc delta F for mask series
    cell_delta = edge.deltaF(cell_int, f_0_win=max_frame_number - 1)

    # group all results by mask
    maskres_dict = {'cell': [cell_int, cell_delta]}

    # saving results to CSV
    # NOT READY!
    if save_csv:
        for val_num in range(len(cell_int)):
            int_val = cell_int[val_num]
            delta_val = cell_delta[val_num]
            df = df.append(
                pd.Series(
                    [
                        cell.img_name,  # cell file name
                        cell.feature,  # 405 nm power