Пример #1
0
def get_movie_header(rgb_files_with_path, correct_bad_pixels,
                     correct_stray_light):  # todo: rewrite to change it as a class
    """
        get the movie "header" info
    :param rgb_files_with_path:
    :return: size(n_row, n_col, n_frame), and frame_rate
    """

    # open one channel first to get the basics of the movie
    ext = os.path.splitext(rgb_files_with_path[0])[1]

    if ext == '.isxd':
        tmp = isx.Movie(rgb_files_with_path[0])
        # frame_shape = tmp.shape
        num_frames = tmp.num_frames
        frame_rate = tmp.frame_rate
        frame_period = tmp.get_frame_period()
        data_type = tmp.data_type
        tmp.close()
    elif ext == '.tif':
        tmp = Image.open(rgb_files_with_path[0])
        # frame_shape = tmp.size[::-1]
        num_frames = tmp.n_frames
        frame_rate = 20  # tmp.frame_rate #todo: does the tif file always have frame rate info? what's the tag name??
        frame_period = 50000
        tmp.close()

    # get an example frame to get accurate frame_shape (especially necessary when correct_bad_pixels == True
    tmp = get_rgb_frame(rgb_files_with_path, 0, correct_stray_light=correct_stray_light,
                        correct_bad_pixels=correct_bad_pixels)
    frame_shape = tmp.shape[1:3]

    return frame_shape, num_frames, frame_period, data_type, frame_rate
Пример #2
0
    def correct_bad_pixels(self):
        """
            correct the first frame to get updated n_row, n_col
        :param correct_bad_pixels:
        :return:
        """
        filename_with_path = self.filename_with_path
        channel = self.channel
        ext = self.extension

        if ext == '.isxd':
            mov = isx.Movie(filename_with_path)
            im = mov.read_frame(0)
        elif ext == '.tif':
            mov = Image.open(filename_with_path)
            mov.seek(0)
            im = np.array(mov)
        mov.close()

        x = DiscardBadPixels()
        # method0 = getattr(x, 'correct')
        # im = method0(im, channel)
        im = x.discard4channel(im, channel)

        self.n_row = im.shape[0]
        self.n_col = im.shape[1]
        self.shape = (self.n_row, self.n_col)
        self.correct_bad_pixels = True
Пример #3
0
    def __init__(self, filename_with_path):  # correct_bad_pixels, correct_stray_light):
        self.filename_with_path = filename_with_path

        # open movie to get the basics
        tmp = os.path.splitext(filename_with_path)
        filename = os.path.basename(tmp[0])
        ext = tmp[1]

        if ext == '.isxd':
            mov = isx.Movie(filename_with_path)
            frame_shape = mov.shape
            n_row = frame_shape[0]
            n_col = frame_shape[1]
            n_frame = mov.num_frames
            frame_rate = mov.frame_rate
            frame_period = mov.get_frame_period()
            data_type = mov.data_type
        elif ext == '.tif':
            mov = Image.open(filename_with_path)
            frame_shape = mov.size[::-1]
            n_row = frame_shape[0]
            n_col = frame_shape[1]
            n_frame = 2000  # mov.n_frames  #it's too slow to get the n_frames tag from a tif file
            frame_rate = 20  # mov.frame_rate #todo: does the tif file always have frame rate info? what's the tag name??
            frame_period = int(10 ** 6 / frame_rate)
            data_type = np.uint16
        mov.close()

        self.n_row = n_row
        self.n_col = n_col
        self.shape = frame_shape
        self.n_frame = n_frame
        self.frame_rate = frame_rate
        self.frame_period = frame_period
        self.data_type = data_type
        self.extension = ext

        channel = []
        for thischannel in ['red', 'green', 'blue']:
            if thischannel in filename:
                channel.append(thischannel)

        self.channel = channel

        self.pixel_corrected = False
Пример #4
0
def generate_meanProj_subBg_files(root_dir, save_path, channel, dff=None):
    """
        get mean intensity projection image for all red and green images
    """

    if dff is None:
        dff = False

    fn = [
        f for f in listdir(root_dir)
        if (isfile(join(root_dir, f)) and '_{}.isxd'.format(channel) in f)
    ]
    print('{} files have been found, they are \n {}'.format(len(fn), fn))

    isx.initialize()
    for i, thisfile in enumerate(fn):
        mov = isx.Movie(join(root_dir, thisfile))
        n_frame = mov.num_frames
        n_frame = min(n_frame, 1000)
        shape = mov.shape
        n_row = shape[0]
        n_col = shape[1]
        tmp = np.empty([n_row, n_col, n_frame])
        for j in range(n_frame):
            tmp[:, :, j] = mov.read_frame(j)

        mov_mean = np.mean(tmp, axis=2)

        if dff:
            mov_dff = np.divide(np.subtract(tmp, mov_mean[:, :, np.newaxis]),
                                mov_mean[:, :, np.newaxis])
            mov_dff_max = np.max(mov_dff, axis=2)
            im = Image.fromarray(mov_dff_max)
            thisfile_basename = '{}_dff'.format(splitext(thisfile)[0])
        else:
            mov_mean_median = np.median(mov_mean.flatten())
            mov_mean = mov_mean - mov_mean_median
            im = Image.fromarray(mov_mean)
            thisfile_basename = splitext(thisfile)[0]

        im.save(join(save_path, thisfile_basename + '.tif'))
        print('the {}th file {} is completed'.format(i, thisfile))
        mov.close()
        im.close()
    isx.shutdown()
Пример #5
0
def get_rgb_pixel_time(rgb_files_with_path, select_frame_idx=None, select_pixel_idx=None, correct_stray_light=None,
                       correct_bad_pixels=None):
    import os
    from PIL import Image

    # Get intensity for specific pixels at specific frames
    ext = os.path.splitext(rgb_files_with_path[0])[1]
    if ext == '.isxd':
        tmp = isx.Movie(rgb_files_with_path[0])
        frame_shape = tmp.shape
        n_pixels = frame_shape[0] * frame_shape[1]
        n_frames = tmp.num_frames
        tmp.close()
    elif ext == '.tif':
        tmp = Image.open(rgb_files_with_path[0])
        frame_shape = tmp.size[::-1]
        n_pixels = frame_shape[0] * frame_shape[1]
        n_frames = tmp.n_frames
    else:
        frame_shape = []
        n_frames = []
        n_pixels = []

    if select_frame_idx is None:
        select_frame_idx = np.arange(0, n_frames - 1)
    if select_pixel_idx is None:
        select_pixel_idx = np.arange(0, n_pixels - 1)
    if correct_stray_light is None:
        correct_stray_light = False
    if correct_bad_pixels is None:
        correct_bad_pixels = False

    rgb_pixel_time = np.empty([3, len(select_pixel_idx), len(select_frame_idx)])
    print('Collect frame', end='')
    this_rgb_frame = np.empty([frame_shape[0], frame_shape[1]])
    for i, frame_idx in enumerate(select_frame_idx):
        print('...', end='')
        this_rgb_frame = get_rgb_frame(rgb_files_with_path, frame_idx, correct_stray_light=correct_stray_light,
                                       correct_bad_pixels=correct_bad_pixels)
        rgb_pixel_time[:, :, i] = this_rgb_frame.reshape([-1, n_pixels])[:, select_pixel_idx]
        print('frame {}'.format(frame_idx))

    return rgb_pixel_time
Пример #6
0
    def get_header(self):
        """
            open movie for each file, and get movie header
        :return:
        """
        ext = self.ext
        rgb_filenames_with_path = self.rgb_filenames_with_path

        n_mov = len(rgb_filenames_with_path)
        mov_list = [0]*n_mov
        for i, thisfile in enumerate(rgb_filenames_with_path):

            if ext == '.isxd':
                mov = isx.Movie(thisfile)
                frame_shape = mov.shape
                n_row = frame_shape[0]
                n_col = frame_shape[1]
                n_frame = mov.num_frames
                frame_rate = mov.frame_rate
                frame_period = mov.get_frame_period()
                data_type = mov.data_type
            elif ext == '.tif':
                mov = Image.open(thisfile)
                frame_shape = mov.size[::-1]
                n_row = frame_shape[0]
                n_col = frame_shape[1]
                n_frame = 2000  # mov.n_frames  #it's too slow to get the n_frames tag from a tif file
                frame_rate = 20  # mov.frame_rate #todo: does the tif file always have frame rate info? what's the tag name??
                frame_period = int(10 ** 6 / frame_rate)
                data_type = np.uint16
            mov_list[i] = mov

            self.mov = mov_list
            self.n_row = n_row
            self.n_col = n_col
            self.shape = frame_shape
            self.n_frame = n_frame
            self.frame_rate = frame_rate
            self.frame_period = frame_period
            self.data_type = data_type

            self.pixel_corrected = False
            self.correct_stray_light = False
Пример #7
0
def write_cssp_movie(rgb_basename_with_path,
                     extension=None,
                     correct_stray_light=None,
                     correct_bad_pixels=None,
                     save_pathname=None,
                     save_basename=None):
    """
        Write GCaMP and RGeco movie after color signal splitting.

    :param rgb_files_with_path:
    :param save_path:
    :param save_filename:
    :return:
    """

    import pymsgbox

    if extension is None:
        extension = '.isxd'

    # todo: rewrite get_exp_label function, make it as a class with method like exp.led, exp.probe
    # exp = get_exp_label(rgb_files_basename)
    # exp.led.blue = 1
    # exp.led.lime = 0.5
    # exp.probe = ['GCaMP', 'RGeco']
    # aM = calc_aMatrix_for_rgb_signal_split(exp.probe, exp.led, cssp=None)
    exp_led = [1.8, 0.9]
    exp_channel = ['red', 'green', 'blue']
    exp_probe = ['GCaMP', 'RGeco', 'Autofluo']
    aM = calc_aMatrix_for_rgb_signal_split(exp_led, cssp=None)
    aM_inv = np.linalg.inv(aM)

    if save_pathname is None:
        save_pathname = os.path.dirname(rgb_basename_with_path)
    if not os.path.exists(save_pathname):
        os.mkdir(save_pathname)
    rgb_basename = os.path.basename(rgb_basename_with_path)
    if save_basename is None:
        save_basename = rgb_basename
    save_filenames_with_path = [os.path.join(save_pathname, '{}_{}.isxd'.format(save_basename, probe)) for probe in
                               exp_probe]
    if correct_stray_light is None:
        correct_stray_light = False
    if correct_bad_pixels is None:
        correct_bad_pixels = False

    rgb_filenames_with_path = find_rgb_files(rgb_basename_with_path,
                                             extension=extension,
                                             channel_list=exp_channel)

    header = MovieHeader(rgb_filenames_with_path[0])
    if correct_bad_pixels:
        header.correct_bad_pixels()

    n_ch = len(exp_channel)
    n_frame = header.n_frame
    n_probe = len(exp_probe)

    output_mov_list = [0] * n_probe
    for i in range(n_probe):
        if os.path.exists(save_filenames_with_path[i]):
            os.remove(save_filenames_with_path[i])
            pymsgbox.alert('File exists! do you want to rewrite {}'.format(save_filenames_with_path[i]))
        output_mov_list[i] = isx.Movie(save_filenames_with_path[i],
                                       frame_period=header.frame_period,
                                       shape=header.shape,
                                       num_frames=header.n_frame,
                                       data_type=header.data_type)
    print('Writing frame...')
    for frame_idx in range(n_frame):
        rgb_frame = get_rgb_frame(rgb_filenames_with_path, frame_idx,
                                  correct_stray_light=correct_stray_light,
                                  correct_bad_pixels=correct_bad_pixels)
        xyz = rgb_signal_split(rgb_frame, aM_inv)

        # xyz_min = xyz.reshape((n_probe, header.n_row * header.n_col)).min(axis=1)
        # xyz = np.subtract(xyz, xyz_min[:, np.newaxis, np.newaxis])
        # xyz[xyz < 0] = 0
        xyz += 30000

        for i in range(n_probe):
            output_mov_list[i].write_frame(xyz[i, :, :], frame_idx)
        if (frame_idx + 1) / 10 != 0 and (frame_idx + 1) % 10 == 0:
            print('...')
            print('\n'.join(map(str, range(frame_idx - 9, frame_idx + 1))))

    print('... all frames done!')

    for i in range(n_probe):
        output_mov_list[i].close()
Пример #8
0
def write_corrected_rgb_isxd_movie(rgb_basename_with_path,
                                   extension=None,
                                   correct_bad_pixels=None,
                                   correct_stray_light=None,
                                   save_pathname=None,
                                   save_basename=None):
    """
        rewrite the movie to an .isxd file after different options, such as discard bad pixels, correct microscope stray
        light, etc.
    :param input_filename_with_path: input .isxd or .tif movie
    :param correct_bad_pixel:
    :param correct_stray_light:
    :param save_pathname:
    :param save_filename:
    :return:
    """

    import pymsgbox

    if extension is None:
        extension = '.isxd'
    if save_pathname is None:
        save_pathname = os.path.dirname(rgb_basename_with_path)
        save_pathname = os.path.join(save_pathname, 'corrected')
    if not os.path.exists(save_pathname):
        os.mkdir(save_pathname)

    rgb_basename = os.path.basename(rgb_basename_with_path)
    if save_basename is None:
        save_basename = rgb_basename

    if correct_bad_pixels is None:
        correct_bad_pixels = False
    if correct_stray_light is None:
        correct_stray_light = False

    channel = ['red', 'green', 'blue']  # exp['channels']
    # todo: rewrite get_exp_label() to add channel info. I am having a stronger feeling to write a class for
    # todo: movie such that each movie is a class with all movie data and header info

    rgb_filenames_with_path = find_rgb_files(rgb_basename_with_path,
                                             extension=extension,
                                             channel_list=channel)

    save_filenames_with_path = [os.path.join(save_pathname, '{}_{}.isxd'
                                             .format(save_basename, thischannel)) for thischannel in channel]

    header = MovieHeader(rgb_filenames_with_path[0])
    if correct_bad_pixels:
        header.correct_bad_pixels()

    n_ch = len(channel)
    n_frame = header.n_frame

    output_mov_list = [0] * n_ch
    for i in range(n_ch):
        if os.path.exists(save_filenames_with_path[i]):
            pymsgbox.alert('File exists! do you want to rewrite {}'.format(save_filenames_with_path[i]))
            os.remove(save_filenames_with_path[i])
        output_mov_list[i] = isx.Movie(save_filenames_with_path[i],
                                       frame_period=header.frame_period,
                                       shape=header.shape,
                                       num_frames=header.n_frame,
                                       data_type=header.data_type)
    print('Writing frame...')
    for frame_idx in range(n_frame):
        rgb_frame = get_rgb_frame(rgb_filenames_with_path, frame_idx,
                                  correct_stray_light=correct_stray_light,
                                  correct_bad_pixels=correct_bad_pixels)
        for i in range(n_ch):
            output_mov_list[i].write_frame(rgb_frame[i, :, :], frame_idx)

        if (frame_idx + 1) / 10 != 0 and (frame_idx + 1) % 10 == 0:
            print('...')
            print('\n'.join(map(str, range(frame_idx - 9, frame_idx + 1))))
    print('... all frames done!')

    for i in range(n_ch):
        output_mov_list[i].close()

    return save_filenames_with_path
Пример #9
0
def get_rgb_frame(rgb_files, frame_idx, camera_bias=None, correct_stray_light=None, correct_bad_pixels=None):
    import os
    from PIL import Image

    # get movie info for each channel
    if camera_bias is None:
        camera_bias = 172
    if correct_stray_light is None:
        correct_stray_light = False

    if correct_bad_pixels is None:
        correct_bad_pixels = False

    ext = os.path.splitext(rgb_files[0])[1]
    if ext == '.isxd':  # for .isxd files
        # # For debug purpose: check if all channels are correct
        # for this_file in rgb_files:
        #     this_movie = isx.Movie(this_file)
        #     print('Frame rate:{:0.2f} Hz'.format(this_movie.frame_rate))
        #     print('#of frames:{}'.format(this_movie.num_frames))
        #     print('Movie size: {} rows by {} columns'.format(this_movie.shape[0], this_movie.shape[1]))
        #     this_movie.close()
        #     # may need to handle cases when the files detected are not red/green/blue channels, ect.

        this_movie = isx.Movie(rgb_files[0])
        n_row = this_movie.shape[0]
        n_col = this_movie.shape[1]
        this_movie.close()

        n_ch = len(rgb_files)
        rgb_frame = np.empty([n_ch, n_row, n_col])

        for i, this_file in enumerate(rgb_files):
            this_movie = isx.Movie(this_file)
            frame_idx = int(frame_idx)
            this_frame = this_movie.read_frame(frame_idx)
            rgb_frame[i, :, :] = this_frame - camera_bias
            this_movie.close()

    elif ext == '.tif':
        # # For debug purpose: check if all channels are correct
        # for this_file in rgb_files:
        #     this_movie = Image.open(this_file)
        #     print('Frame rate:{:0.2f} Hz'.format(this_movie.frame_rate))
        #     print('#of frames:{}'.format(this_movie.n_frames))
        #     print('Movie size: {} rows by {} columns'.format(this_movie.size[1], this_movie.size[0]))

        this_movie = Image.open(rgb_files[0])
        n_row = this_movie.size[1]
        n_col = this_movie.size[0]
        this_movie.close()

        n_ch = len(rgb_files)
        rgb_frame = np.zeros((n_ch, n_row, n_col))

        for i, this_file in enumerate(rgb_files):
            this_movie = Image.open(this_file)
            this_movie.seek(frame_idx)
            rgb_frame[i, :, :] = np.array(this_movie)
            this_movie.close()

    rgb_files_root = find_rgb_channels(rgb_files)[1]
    rgb_files_root = os.path.split(rgb_files_root)[1]
    exp = get_exp_label(rgb_files_root)
    exp_label = exp['label']

    if correct_stray_light:
        rgb_frame = subtract_stray_light(rgb_frame, exp_label)

    if correct_bad_pixels:
        x = DiscardBadPixels()
        x.discard4channel(rgb_frame)
        rgb_frame = discard_bad_pixels(rgb_frame)

    return rgb_frame
Пример #10
0
def main():
    isx.initialize()

    # input files
    root_dir = ('/ariel/data2/Alice/NV3_DualColor/D_Lab/' #NV3_color_sensor_12bit/'    #
                'Masa/20170816/led2') #'/Scope_Autofluorescence/NV3-04/led2_0_gain1_coverOff')  #'V3-63/20170807/led2')  #'V3-17/20170714') #'V3-55/20170710') #'V3-55/V3_55_20170717') #'V3-75/20171127/led2') #'V3_39/20170807/led2')  #'V3-17/20170714')  #'Scope_Autofluorescence/NV3-04/led2_4_gain1_coverOff')  #NV3-04/led2_4_coverOff')  #
    # 'Scope_Autofluorescence/NV3-01/led2_0') #
    # root_dir = '/Users/Sabrina/workspace/data/pipeline_s'
    fn = [f for f in listdir(root_dir) if isfile(join(root_dir, f))]
    print('{} files have been found, they are \n {}'.format(len(fn), fn))

    # find the rgb channel, and sort fn as r/g/b
    ch = ['red', 'green', 'blue']
    rgb_files, rgb_files_root = isxrgb.find_rgb_channels(fn, channel_list=ch)
    print('rgb files are \n {}'.format(rgb_files))

    exp = isxrgb.get_exp_label(rgb_files_root)

    rgb_files_with_path = [os.path.join(root_dir, file) for file in rgb_files]

    # open one channel first to get the basics of the movie
    ext = os.path.splitext(rgb_files_with_path[0])[1]

    if ext == '.isxd':
        tmp = isx.Movie(rgb_files_with_path[0])
        frame_shape = tmp.shape
        n_pixels = frame_shape[0] * frame_shape[1]
        n_frames = tmp.num_frames
        frame_rate = tmp.frame_rate
        tmp.close()
    elif ext == '.tif':
        tmp = Image.open(rgb_files_with_path[0])
        frame_shape = tmp.size[::-1]
        n_pixels = frame_shape[0] * frame_shape[1]
        n_frames = tmp.n_frames
        frame_rate = 20 #tmp.frame_rate #todo: does the tif file always have frame rate info? what's the tag name??
        tmp.close()

    # get an example frame to get accurate frame_shape (especially necessary when correct_bad_pixels == True
    tmp = isxrgb.get_rgb_frame(rgb_files_with_path, 0, correct_stray_light=correct_stray_light,
                               correct_bad_pixels=correct_bad_pixels)
    frame_shape = tmp.shape[1:3]
    n_pixels = frame_shape[0] * frame_shape[1]

    ####
    if analyze_example_frame:
        for frame_idx in example_frame_idx:
            # get data
            example_rgb_frame = isxrgb.get_rgb_frame(rgb_files_with_path, frame_idx,
                                                     camera_bias=None,
                                                     correct_stray_light=correct_stray_light,
                                                     correct_bad_pixels=correct_bad_pixels)
            # prepare figure
            fig = plt.figure(figsize=(18, 8))
            gs = gsp.GridSpec(2, 4, wspace=0.2)
            plt.suptitle('{}\n\n{}'.format(exp['label'], rgb_files_root)) #, rgb_files_root, frame_idx))   #{}  frame # {}

            # show rgb images
            hax = list()
            for k in range(3):
                hax.append(plt.subplot(gs[0, k]))
            # tmp = np.zeros((3, 20, 30))
            # tmp[:, 0:10, 0:15] = example_rgb_frame[:, 0:10, 0:15]
            # show_rgb_frame(tmp, ax_list=hax, clim=clim)
            isxrgb.show_rgb_frame(example_rgb_frame, cmap='gray', ax_list=hax, clim=clim)

            # plot the histogram
            hax = plt.subplot(gs[0, 3])
            plot_rbg_pixels_hist(example_rgb_frame, ax=hax)
            hax.set_xlim(clim)
            pos = hax.get_position()
            pos.x0 = pos.x0 + 0.3 * pos.width
            hax.set_position(pos)

            # plot scatter plot to show the ratio for r / g, g / b, and b / r pair
            hax = []
            for k in range(3):
                hax.append(plt.subplot(gs[1, k]))
            show_rgb_ratio(example_rgb_frame, ax_list=hax, n_select_pixels=1000)

        gs.tight_layout(fig, rect=[0.02, 0.02, 0.96, 0.95])
        figure_name = '{}/figures/{}_file{}_frame{}'.format(os.getcwd(), exp['label'], rgb_files_root[-2:], frame_idx)
        if correct_stray_light:
            figure_name = '{}_correct'.format(figure_name)
        plt.savefig(figure_name, dpi=None, facecolor='w', edgecolor='w',
                    orientation='portrait', papertype=None, format=None,
                    transparent=False, bbox_inches=None, pad_inches=0,
                    frameon=None)

    ####
    if analyze_time_pixel:
        # get data
        select_frame_idx = np.arange(0, n_frames - 1, int(n_frames / n_select_time_points))
        select_pixel_idx = np.arange(0, n_pixels - 1, int(n_pixels / n_select_pixels))
        if random_pixels:
            select_pixel_idx = random.sample(range(n_pixels), int(n_pixels / n_select_pixels))

        rgb_pixel_time = isxrgb.get_rgb_pixel_time(rgb_files_with_path, select_frame_idx, select_pixel_idx,
                                                   correct_stray_light=correct_stray_light,
                                                   correct_bad_pixels=correct_bad_pixels)
        # make figure for plots
        plt.figure(figsize=(10, 8))
        gs = gsp.GridSpec(3, 2, width_ratios=[3, 1], wspace=0.3)
        plt.suptitle('{}\n\n {}  selected {} frames'.format(exp['label'], rgb_files_root, len(select_frame_idx)))

        hax = list()
        for i in range(2):
            for k in range(3):
                hax.append(plt.subplot(gs[k, i]))  # for k in range(3) and i in range(2)))

        plot_rgb_pixel_time(rgb_pixel_time, select_frame_idx, frame_rate, measure_name=measure_name, ax_list=hax,
                            fit_method=fit_method, ch=None)

        # plt.show()
        figure_name = '{}/figures/{}_time_{}_file{}'.format(os.getcwd(), exp['label'], measure_name, rgb_files_root[-2:])
        if correct_stray_light:
            figure_name = '{}_correct'.format(figure_name)
        plt.savefig(figure_name, dpi=None, facecolor='w', edgecolor='w',
                    orientation='portrait', papertype=None, format=None,
                    transparent=False, bbox_inches=None, pad_inches=0.1,
                    frameon=None)

    if analyze_time:
        select_frame_idx = np.arange(0, n_frames - 1, int(n_frames / n_select_time_points))
        rgb_stack = np.empty((len(ch), len(select_frame_idx)))
        print('Collect frame', end='')
        for i, frameIdx in enumerate(select_frame_idx):  # show randomly selected frames
            print('...', end='')
            this_rgb_frame = isxrgb.get_rgb_frame(rgb_files_with_path, frameIdx,
                                                  correct_stray_light=correct_stray_light,
                                                  correct_bad_pixels=correct_bad_pixels)
            # calculate average intensity across the frame for R/G/B
            rgb_stack[:, i] = np.mean(this_rgb_frame.reshape(-1, len(ch)), axis=0)
            print('frame {}'.format(frameIdx))

        # total_drop = (rgb_stack[:, 0] - rgb_stack[:, -1])/rgb_stack[:, 0]

        plt.figure(figsize=(10, 10))
        gs = gsp.GridSpec(3, 1)
        htitle = plt.suptitle(
            '{}\n\n {} photobleaching selected {} frames'.format(exp['label'], rgb_files_root, len(select_frame_idx)))

        hax = []
        for k in range(len(ch)):
            hax.append(plt.subplot(gs[k]))

        for i in range(len(ch)):
            plt.sca(hax[i])
            x2plot = select_frame_idx / frame_rate / 60
            y2plot = rgb_stack[i, :]
            plt.plot(x2plot, y2plot, color=ch[i], linestyle='None', marker='.', markersize=2)

            a, k, c, y_hat = mm.fit_exponential(x2plot, y2plot, linear_method=True, c0=0)
            a, k, c, y_hat = mm.fit_exponential(x2plot, y2plot, linear_method=False, p0=[a, k, c])
            plt.plot(x2plot, y_hat, label='$F = %0.2f e^{%0.2f t} + %0.2f$' % (a, k, c))
            hax[i].legend(bbox_to_anchor=(1, 1))
            hax[i].set_ylabel('F_mean', color=ch[i])
            pb_measured = np.diff(y2plot[[-1, 0]]) / y2plot[0]
            pb_fitted = np.diff(y_hat[[-1, 0]]) / y2plot[0]
            pb_measured_s = '{:.0%}'.format(pb_measured[0])
            pb_fitted_s = '{:.0%}'.format(pb_fitted[0])
            label = '{}           {}'.format(pb_measured_s, pb_fitted_s)

            if i == 0:
                label = 'Total fluorescence drop in {:.1f} min is measured as {} fitted as {}'.format(x2plot[-1],
                                                                                                      pb_measured_s,
                                                                                                      pb_fitted_s)
            plt.text(x2plot[-1], hax[i].get_ylim()[-1] - 0.2 * np.diff(hax[i].get_ylim()), label,
                     horizontalalignment='right', verticalalignment='top', color=ch[i])
        hax[-1].set_xlabel('time (min)')

        # plt.show()
        figure_name = '{}/figures/{}_photobleaching_file{}'.format(os.getcwd(), exp['label'], rgb_files_root[-2:])
        if correct_stray_light:
            figure_name = '{}_correct'.format(figure_name)
        plt.savefig(figure_name, dpi=None, facecolor='w', edgecolor='w',
                    orientation='portrait', papertype=None, format=None,
                    transparent=False, bbox_inches=None, pad_inches=0.1,
                    frameon=None)
def create_stray_light_fitting_files(root_dir_group_list, time_range,
                                     save_filename_straylight_data, save_filename_straylight,
                                     channel=None, correct_bad_green_pixels=None,
                                     ):

    if channel is None:
        ch = ['red', 'green', 'blue']
    if correct_bad_green_pixels is None:
        correct_bad_green_pixels = False

    straylight = {}
    straylight['file_info'] = []
    max_n_file = max(len(this_group_root_dir_list) for this_group_root_dir_list in root_dir_group_list) - 1

    for group_idx, this_group_root_dir_list in enumerate(root_dir_group_list):
        straylight['file_info'].append({})
        straylight['file_info'][group_idx]['tissue'] = []
        straylight['file_info'][group_idx]['microscope'] = []
        straylight['file_info'][group_idx]['led_name'] = []
        straylight['file_info'][group_idx]['led_power'] = []
        straylight['file_info'][group_idx]['filename'] = []

        for file_idx in range(len(this_group_root_dir_list) - 1):
            root_dir = os.path.join(this_group_root_dir_list[0], this_group_root_dir_list[file_idx + 1])
            fn = [f for f in listdir(root_dir) if isfile(join(root_dir, f))]
            print('{} files have been found, they are \n {}'.format(len(fn), fn))

            # find the rgb channel, and sort fn as r/g/b
            rgb_files, rgb_files_root = isxrgb.find_rgb_channels(fn)
            print('rgb files are \n {}'.format(rgb_files))

            exp_label = isxrgb.get_exp_label(rgb_files_root)

            rgb_files_with_path = [os.path.join(root_dir, file) for file in rgb_files]

            # open one channel first to get the frame numbers
            ext = os.path.splitext(rgb_files_with_path[0])[1]

            if ext == '.isxd':
                tmp = isx.Movie(rgb_files_with_path[0])
                frame_shape = tmp.shape
                n_pixels = frame_shape[0] * frame_shape[1]
                n_frames = tmp.num_frames
                frame_rate = tmp.frame_rate
                tmp.close()
            elif ext == '.tif':
                tmp = Image.open(rgb_files_with_path[0])
                frame_shape = tmp.size[::-1]
                n_pixels = frame_shape[0] * frame_shape[1]
                n_frames = tmp.n_frames
                frame_rate = tmp.frame_rate
                tmp.close()

            # get an example frame to get accurate frame_shape
            # (especially necessary when correct_bad_green_pixels == True
            tmp = isxrgb.get_rgb_frame(rgb_files_with_path, 0, correct_bad_green_pixels=correct_bad_green_pixels)
            frame_shape = tmp.shape[1:3]
            n_pixels = frame_shape[0] * frame_shape[1]

            frame_range = np.array(time_range) * frame_rate
            select_frame_idx = np.arange(frame_range[0], frame_range[1])
            rgb_frame_stack = np.zeros((len(ch), frame_shape[0], frame_shape[1], len(select_frame_idx)))
            print('Collect frame', end='')
            for i, frameIdx in enumerate(select_frame_idx):  # show randomly selected frames
                print('...', end='')
                this_rgb_frame = isxrgb.get_rgb_frame(rgb_files_with_path, frameIdx,
                                                      correct_bad_green_pixels=correct_bad_green_pixels)
                rgb_frame_stack[:, :, :, i] = this_rgb_frame
                print('frame {}'.format(frameIdx))

            # calculate average intensity across time for R/G/B
            rgb_frame_stack_mean4time = np.mean(rgb_frame_stack, axis=3)
            # rgb_frame_stack_std4time = np.std(rgb_frame_stack, axis=3)

            if file_idx == 0 and group_idx == 0:
                rgb_frame_file_group = np.empty((len(ch), frame_shape[0], frame_shape[1],
                                                 max_n_file, len(root_dir_group_list)))
                rgb_frame_file_group.fill(np.nan)

            rgb_frame_file_group[:, :, :, file_idx, group_idx] = rgb_frame_stack_mean4time

            # get experimental parameter from exp_label
            idx1 = exp_label.rfind('(')
            idx2 = exp_label.rfind(')')
            straylight['file_info'][group_idx]['led_power'].append(
                float(exp_label[idx1 + 1:idx2 - 2].replace(',', '.', 1)))
            idx3 = exp_label[0:idx1].rfind(',')
            idx4 = exp_label[0:idx3].rfind(',')
            straylight['file_info'][group_idx]['tissue'].append(exp_label[0:idx4])
            straylight['file_info'][group_idx]['microscope'].append(exp_label[idx4 + 2:idx3])
            straylight['file_info'][group_idx]['led_name'].append(exp_label[idx3 + 2:idx1 - 1])
            straylight['file_info'][group_idx]['filename'].append(rgb_files_root)

        led_power = straylight['file_info'][group_idx]['led_power']
        led_name = straylight['file_info'][group_idx]['led_name'][0]
        straylight[led_name] = {}

        # fit F - Power(LED) for each pixel
        x = led_power
        b1 = np.empty(n_pixels)
        b0 = np.empty(n_pixels)
        straylight[led_name]['b0'] = np.empty((len(ch), frame_shape[0], frame_shape[1]))
        straylight[led_name]['b1'] = np.empty((len(ch), frame_shape[0], frame_shape[1]))
        for i in range(len(ch)):
            tmp = np.reshape(rgb_frame_file_group[i, :, :, :, group_idx], [n_pixels, -1])
            for count, j in enumerate(range(n_pixels)):
                if np.mod(count, 10000) == 0:
                    print('fitting pixel # {} ...'.format(j))
                y = tmp[j, :]
                y = y[~np.isnan(y)]
                coeff, y_hat = mm.linear_regression(x, y)  # fit with linear
                b0[j] = coeff[0]
                b1[j] = coeff[1]
                # y_hat = m * x + b
            straylight[led_name]['b0'][i, :, :] = b0.reshape([frame_shape[0], frame_shape[1]])
            straylight[led_name]['b1'][i, :, :] = b1.reshape([frame_shape[0], frame_shape[1]])

    # save the result into files
    save_data_file = open(save_filename_straylight_data, 'wb')
    pickle.dump(rgb_frame_file_group, save_data_file)
    save_data_file.close()

    save_file = open(save_filename_straylight, 'wb')
    pickle.dump(straylight, save_file)
    save_file.close()
Пример #12
0
def run_test_data():
    root_dir = '/Users/Sabrina/git/myWork/dualColor/result/isxd'  #xyz.isxd'
    #'/ariel/data2/Sabrina/NV3_01_greenpixel_bitsdrop/'
    filename = 'xyz.isxd'  #'v3-01_gr_0ff.isxd' #'v3-01_gb_0ff.isxd' #'v3-01_blue_0ff.isxd' #'v3-01_red_0ff.isxd' #'Movie_2018-01-30-12-53-38_red_only.isxd'  #'Movie_2018-01-30-12-56-08_blue_only.isxd'  #'img_2018-03-08-15-41-21 A80043050047 colorbars.tif' #'img_2018-03-08-15-43-46 A80043050039 colorbars.tif'
    # #'A80043050039.tif' #'Movie_2018-01-30-12-55-17_greenr_raw.isxd' #'Movie_2018-01-30-12-56-52_greenb_raw.isxd' #'both_green_fff_2016-02-11-14-15-10_raw.isxd' #
    select_frames = [0]
    microscope_name = 'NV3-1'  #'JH_cropEdge'

    filename_with_path = os.path.join(root_dir, filename)
    ext = os.path.splitext(filename_with_path)[1]

    if ext == '.tif':
        this_movie = Image.open(filename_with_path)
        n_row = this_movie.size[1]
        n_col = this_movie.size[0]
        n_frame = this_movie.n_frames
    elif ext == '.isxd':
        this_movie = isx.Movie(filename_with_path)
        # print('Frame rate:{:0.2f} Hz'.format(this_movie.frame_rate))
        n_row = this_movie.shape[0]
        n_col = this_movie.shape[1]
        n_frame = this_movie.num_frames
    print('#of frames:{}'.format(n_frame))
    print('Movie size: {} rows by {} columns'.format(n_row, n_col))

    stack = np.empty((n_row, n_col, len(select_frames)))
    # get first frame from each channel
    for i, frame_idx in enumerate(select_frames):
        if ext == '.tif':
            this_movie.seek(frame_idx)
            stack[:, :, i] = np.array(this_movie)
            this_movie.close()
        elif ext == '.isxd':
            stack[:, :, i] = this_movie.read_frame(frame_idx)
    this_movie.close()

    # n_colorPan_y_rng = {'w': [3, 155], 'y': [156, 315], 'c': [316, 475], 'g': [476, 635],
    #                     'm': [636, 795], 'r': [796, 955], 'b': [956, 1115], 'k': [1116, 1275]}
    # select_colorPan = 'g'
    # u, u_inverse, u_count = np.unique(stack[:, np.arange(n_colorPan_y_rng[select_colorPan][0],
    #                 n_colorPan_y_rng[select_colorPan][1]+1)], return_inverse=True, return_counts=True)

    # stack = stack[2:-2, 1:-1, :]
    u, u_inverse, u_count = np.unique(stack[:, :, 0],
                                      return_inverse=True,
                                      return_counts=True)

    # add pixel value and the corresponding count
    label = []
    for i in range(len(u)):
        label.append('{} (n={})'.format(int(u[i]), u_count[i]))
        if u[i] == 0:
            label[i] = '{}'.format(int(u[i]))

    fig = plt.figure(figsize=(8, 8))
    gs = plt.GridSpec(1,
                      2,
                      width_ratios=[20, 1],
                      right=0.8,
                      top=0.5,
                      bottom=0.05)
    hax = []
    for i in range(2):
        hax.append(plt.subplot(gs[i]))

    plt.suptitle('{} \n{}'.format(microscope_name, filename))
    plt.sca(hax[0])
    im = plt.imshow(stack[:, :, 0], cmap='jet', aspect='equal')  #0:20, 0:30
    # hax[0].xaxis.set_ticks(range(0, 30, 5))
    # hax[0].yaxis.set_ticks(range(0, 20, 5))

    # plt.sca(hax[1])
    # hax[1].set_ylim(u[[0, -1]])
    # s=''
    # for i in range(len(u)-1, -1, -1):
    #     s = '{}\n{}'.format(s, label[i])
    # plt.text(2, 0, '{}'.format(s), fontsize=8)

    cbar = plt.colorbar(
        im, cax=hax[1])  # , location='right', orientation='vertical')
    pos = hax[0].get_position()
    pos0 = hax[1].get_position()
    hax[1].set_position([pos0.x0, pos.y0, pos0.width, pos.height])

    cbar.set_ticks(u[1::])
    cbar.set_ticklabels(label[1::])

    figure_name = '{}/figures/{}_{}'.format(os.getcwd(), microscope_name,
                                            filename[0:-5])
    plt.savefig(figure_name,
                dpi=600,
                facecolor='w',
                edgecolor='w',
                orientation='portrait',
                papertype=None,
                format=None,
                transparent=False,
                bbox_inches=None,
                pad_inches=0,
                frameon=None)

    plt.show()