def run_exp_data(): root_dir = '/ariel/data2/Alice/NV3_DualColor/NV3_color_sensor_12bit/V3_71/20171127/led1' select_frames = [0] 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)) rgb_files_with_path = [os.path.join(root_dir, file) for file in rgb_files] for i, frame_idx in enumerate(select_frames): this_frame = isxrgb.get_rgb_frame(rgb_files_with_path, frame_idx, correct_bad_green_pixels=False) if i == 0: shape = this_frame.shape stack = np.empty( [shape[0], shape[1], shape[2], len(select_frames)]) stack[:, :, :, i] = this_frame plt.figure() plt.imshow(stack[1, :, :, 0]) figure_name = 'x_all' plt.savefig(figure_name)
def main(): root_dir = '/Volumes/data2/Alice/NV3_DualColor/K_Lab/Cohort1/SO171214A/spatial_bandpass_movie' # root_dir = '/Volumes/data2/Alice/NV3_DualColor/NV3_color_sensor_12bit/Scope_Autofluorescence/NV3-04/led2_4_coverOff' correct_stray_light = False select_frames = [10] 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)) rgb_files_with_path = [os.path.join(root_dir, file) for file in rgb_files] for i, frame_idx in enumerate(select_frames): this_frame = isxrgb.get_rgb_frame( rgb_files_with_path, frame_idx, correct_bad_green_pixels=False, correct_stray_light=correct_stray_light) if i == 0: shape = this_frame.shape stack = np.empty( [shape[0], shape[1], shape[2], len(select_frames)]) stack[:, :, :, i] = this_frame # plt.figure() # plt.imshow(stack[0, :, :, 0]) #cmap='gray') #, clim=[350, 500]) # plt.colorbar() isxrgb.show_rgb_frame(stack[:, :, :, 0], cmap='gray', clim=[0, 200]) #'viridis' figure_name = 'NV3-04_spatial_bandpass' plt.suptitle(figure_name) if correct_stray_light: figure_name = '{}_correct'.format(figure_name) plt.savefig(figure_name)
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 calc_params_for_rgb_signal_split(root_dir_group_list, time_range, max_n_frame, correct_stray_light=None, correct_bad_pixels=None, save_pathname=None, save_filename=None): if correct_bad_pixels is None: correct_bad_pixels = False if correct_stray_light is None: correct_stray_light = False file_index = 0 # current only handle the first file in each group. todo: for multiple files for i in range(len(root_dir_group_list)): root_dir_group_list[i] = root_dir_group_list[i][0:2] # isx.initialize() ch = ['red', 'green', 'blue'] n_group = len(root_dir_group_list) max_n_file = max( len(this_group_root_dir_list) for this_group_root_dir_list in root_dir_group_list) - 1 n_ch = len(ch) rgb_frame_file_group_info = [] cur_short_recording = math.inf """ get file info first, if there is any input file info unexpected, we can error ahead of time """ for group_idx, this_group_root_dir_list in enumerate(root_dir_group_list): rgb_frame_file_group_info.append({}) rgb_frame_file_group_info[group_idx]['tissue'] = [] rgb_frame_file_group_info[group_idx]['microscope'] = [] rgb_frame_file_group_info[group_idx]['led_name'] = [] rgb_frame_file_group_info[group_idx]['led_power'] = [] rgb_frame_file_group_info[group_idx]['rgb_file_root_with_path'] = [] 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)) """ get experiment info """ exp = isxrgb.get_exp_label(rgb_files_root) rgb_frame_file_group_info[group_idx]['led_power'].append( exp['led_power']) rgb_frame_file_group_info[group_idx]['tissue'].append( exp['tissue']) rgb_frame_file_group_info[group_idx]['microscope'].append( exp['microscope']) rgb_frame_file_group_info[group_idx]['led_name'].append( exp['led_name']) rgb_frame_file_group_info[group_idx][ 'rgb_file_root_with_path'] = os.path.join( root_dir, rgb_files_root) """ Open each files and validate conditions before massive reading frames in the next step. This step prepare for the massive file reading and stop the code if input files are not valid. """ rgb_files_with_path = [ os.path.join(root_dir, file) for file in rgb_files ] frame_shape, n_frames, frame_period, data_type, frame_rate = isxrgb.get_movie_header( rgb_files_with_path, correct_bad_pixels=correct_bad_pixels, correct_stray_light=correct_stray_light) frame_range = np.array(time_range) * frame_rate cur_short_recording = min(cur_short_recording, n_frames / frame_rate) if frame_range[1] > n_frames: sys.exit('time_range is too long, there is an input file ' 'lasting for {} seconds'.format(n_frames / frame_rate)) print('The shortest recording last for {} seconds'.format( cur_short_recording)) """ read image data """ for group_idx, this_group_root_dir_list in enumerate(root_dir_group_list): for file_idx in [file_idx ]: # 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))] rgb_files, rgb_files_root = isxrgb.find_rgb_channels(fn) rgb_files_with_path = [ os.path.join(root_dir, file) for file in rgb_files ] frame_shape, num_frames, frame_period, data_type, frame_rate = isxrgb.get_movie_header( rgb_files_with_path, correct_bad_pixels=correct_bad_pixels, correct_stray_light=correct_stray_light) n_pixels = frame_shape[0] * frame_shape[1] frame_range = np.array(time_range) * frame_rate step = math.ceil(frame_range[1] / max_n_frame) select_frame_idx = np.arange(frame_range[0], frame_range[1], step) 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): this_rgb_frame = isxrgb.get_rgb_frame( rgb_files_with_path, frameIdx, correct_stray_light=correct_stray_light, correct_bad_pixels=correct_bad_pixels) rgb_frame_stack[:, :, :, i] = this_rgb_frame if (i + 1) / 10 != 0 and (i + 1) % 10 == 0: print('...') print('\n'.join(map(str, select_frame_idx[(i - 9):(i + 1)]))) # calculate variance across time for R/G/B rgb_frame_stack_var4time = np.var(rgb_frame_stack, axis=3) # calculate average intensity across time for R/G/B rgb_frame_stack_mean4time = np.mean(rgb_frame_stack, axis=3) if file_idx == 0 and group_idx == 0: shape = rgb_frame_stack.shape rgb_frame_file_group = np.empty( (shape[0], shape[1], shape[2], max_n_file, n_group)) rgb_frame_file_group_var = np.empty( (shape[0], shape[1], shape[2], max_n_file, n_group)) rgb_frame_file_group[:, :, :, file_idx, group_idx] = rgb_frame_stack_mean4time rgb_frame_file_group_var[:, :, :, file_idx, group_idx] = rgb_frame_stack_var4time """ calculate parameters """ # get tissue name tissue = [ rgb_frame_file_group_info[i]['tissue'][file_idx] for i in range(n_group) ] print('Tissue: {}'.format(tissue)) # find the group of the files for selected led led = ['Blue', 'Lime'] n_led = len(led) led_group_idx = mu.find([ rgb_frame_file_group_info[i]['led_name'][file_idx] for i in range(n_group) ], led) tmp = [] for i in range(n_group): tmp += led_group_idx[i] led_group_idx = tmp pled = [ rgb_frame_file_group_info[i]['led_power'][file_idx] for i in led_group_idx ] # calculate a1r, a1g, a1b, a2r, a2g, a2b (see document) # red channel (index=0) and Blue LED (index=0) will be the numerator F_de = rgb_frame_file_group[0, :, :, file_idx, led_group_idx[0]] a = np.empty((n_ch, n_led)) aff = np.empty((n_ch, frame_shape[0], frame_shape[1], n_led)) # full frame for i in range(n_led): for j in range(n_ch): F_nu = rgb_frame_file_group[j, :, :, file_idx, led_group_idx[i]] tmp = (F_nu / F_de) * (pled[0] / pled[i]) aff[j, :, :, i] = tmp a[j, i] = np.mean(tmp) cssp = { 'tissue': tissue, 'led': led, 'channel': ch, 'dimension_name': ['channel', 'led'], 'a': a, 'aff': aff, 'rgb_frame_file_group': rgb_frame_file_group, 'rgb_frame_file_group_var': rgb_frame_file_group_var, 'info': { 'root_dir_group_list': root_dir_group_list, 'correct_stray_light': correct_stray_light, 'time_range': time_range, 'time_range_unit': 'second' } } """ save result """ if save_pathname is None: save_pathname = '/ariel/data2/Sabrina/data/result/json' if save_filename is None: save_filename = 'cssp_{}.json'.format(tissue[0]) save_filename_with_path = join(save_pathname, save_filename) with open(save_filename_with_path, 'w') as f: json.dump(cssp, f, cls=mu.NumpyEncoder) f.close() return save_filename_with_path
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()
def main(): isx.initialize() ch = ['red', 'green', 'blue'] n_group = len(root_dir_group_list) max_n_file = max( len(this_group_root_dir_list) for this_group_root_dir_list in root_dir_group_list) - 1 n_ch = len(ch) rgb_frame_file_group_info = [] for group_idx, this_group_root_dir_list in enumerate(root_dir_group_list): rgb_frame_file_group_info.append({}) rgb_frame_file_group_info[group_idx]['tissue'] = [] rgb_frame_file_group_info[group_idx]['microscope'] = [] rgb_frame_file_group_info[group_idx]['led_name'] = [] rgb_frame_file_group_info[group_idx]['led_power'] = [] 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_filenames, rgb_basename = isxrgb.find_rgb_channels(fn) print('rgb files are \n {}'.format(rgb_filenames)) exp_label = isxrgb.get_exp_label(rgb_basename) rgb_filenames_with_path = [ os.path.join(root_dir, file) for file in rgb_filenames ] header = isxrgb.MovieHeader(rgb_filenames_with_path[0]) if correct_bad_pixels: header.correct_bad_pixels() frame_range = np.array(time_range) * header.frame_rate select_frame_idx = np.arange(frame_range[0], frame_range[1]) rgb_frame_stack = np.zeros( (len(ch), header.n_row, header.n_col, 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_filenames_with_path, frameIdx, correct_stray_light=correct_stray_light, correct_bad_pixels=correct_bad_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: shape = rgb_frame_stack.shape rgb_frame_file_group = np.empty( (shape[0], shape[1], shape[2], max_n_file, n_group)) rgb_frame_file_group[:, :, :, file_idx, group_idx] = rgb_frame_stack_mean4time # get experimental parameter from exp_label rgb_frame_file_group_info[group_idx]['tissue'].append( exp_label['tissue']) rgb_frame_file_group_info[group_idx]['microscope'].append( exp_label['microscope']) rgb_frame_file_group_info[group_idx]['led_name'].append( exp_label['led_name']) # select pixels to plot n_pixels = header.n_row * header.n_col if random_pixels: select_pixels = random.sample(range(n_pixels), n_select_pixels) else: select_pixels = np.arange(0, n_pixels - 1, int(n_pixels / n_select_pixels)) # plot example pixels across files fig = plt.figure(figsize=(8, 5)) # (7, 10)) if check_additivity: n_group2plot = n_group + 1 else: n_group2plot = n_group gs = plt.GridSpec(3, n_group2plot, left=0.05, right=0.95, wspace=0.3, width_ratios=[4, 4, 4, 3]) ax_list = [] for j in range(n_group2plot): tmp = [] for i in range(n_ch): tmp.append(plt.subplot(gs[i, j])) ax_list.append(tmp) # tmp = [ax_list[i:i + n_ch] for i in range(0, len(ax_list) - 1, n_ch)] # ax_list = tmp plot_rgb_intensity_vs_ledPower(rgb_frame_file_group, rgb_frame_file_group_info, select_pixels, ax_list=ax_list[:-1]) for group_idx in range(n_group): isxrgb.show_rgb_frame(rgb_frame_file_group[:, :, :, 0, group_idx], ax_list=ax_list[group_idx], clim=[0, 2000], cmap=None, colorbar=False) label = '{} ({}mW) LED'.format( rgb_frame_file_group_info[group_idx]['led_name'][0], rgb_frame_file_group_info[group_idx]['led_power'][0]) if group_idx == 2: label = '{} ({}mW) LED \n+ {} ({}mW) LED'.format( rgb_frame_file_group_info[0]['led_name'][0], rgb_frame_file_group_info[0]['led_power'][0], rgb_frame_file_group_info[1]['led_name'][0], rgb_frame_file_group_info[1]['led_power'][0]) ax_list[group_idx][0].set_title(label, fontsize=10) for i in [0, 1, 2]: ax_list[group_idx][i].axis('off') plt.sca(ax_list[group_idx][i]) plt.tick_params(axis='both', which='major', labelsize=8) ax_list[0][0].axis('on') if check_additivity: # create predicted rgb_frame_predicted = np.sum(rgb_frame_file_group[:, :, :, :, [0, 1]], axis=4) plot_measured_vs_predicted(rgb_frame_file_group[:, :, :, :, -1], rgb_frame_predicted, select_pixels, ax_list=ax_list[:][-1]) # camera_gain = [0, 1, 2, 3] # todo: parse gain parameter from lookup table? # plot_rgb_intensity_vs_cameraGain(rgb_frame_file_group, rgb_frame_file_group_info, select_pixels, camera_gain, # ax_list=ax_list) # plot_rgb_intensity_coverOn_vs_coverOff(rgb_frame_file_group, rgb_frame_file_group_info, select_pixels, # ax_list=ax_list) # plt.show() figure_title = '{}_{}_measured_vs_predicted'.format( rgb_frame_file_group_info[group_idx]['tissue'][0], rgb_frame_file_group_info[group_idx]['microscope'][0]) if correct_stray_light: figure_title = '{}_correct'.format(figure_title) htitle = plt.suptitle('{}, {} example pixels'.format( figure_title, n_select_pixels)) # if ax_list is not None: # gs.tight_layout(fig, rect=[0.02, 0.02, 0.96, 0.95]) plt.savefig('{}/figures/{}'.format(os.getcwd(), figure_title), dpi=600, facecolor='w', edgecolor='w', orientation='portrait', papertype=None, format=None, transparent=False, bbox_inches=None, pad_inches=0.1, frameon=None) isx.shutdown()