def perform_prep_predict_t_creation(img_path_list, file_num, sub_save_location): os.chdir(sub_save_location) t, z, y_dim,x_dim, img = load_img(img_path_list[file_num]) folder_gt = str(file_num) + "_gt" folder_gt = os.path.join(sub_save_location,folder_gt) os.mkdir(folder_gt) folder_steps = str(file_num) + "_steps" folder_steps = os.path.join(sub_save_location,folder_steps) os.mkdir(folder_steps) # txt_name_log = open(destination + "/name_log.txt", "a") # txt_name_log.write("{}, {}\n".format(new_folder_name, img_path_list[file_num]), ) # txt_name_log.close() images_jump =2 #create new directory-path for z_num in tqdm(range(0,z)): for t_num in range(math.ceil(t/images_jump)-1): #create new directory-path file_folder = ("f_%03d" %(file_num)+"-" +"z_%03d"%(z_num) + "-"+"t_%03d" %(t_num)) os.chdir(folder_gt) os.mkdir(file_folder) os.chdir(folder_steps) os.mkdir(file_folder) GT_path_folder = os.path.join(folder_gt, file_folder) steps_path_folder = os.path.join(folder_steps, file_folder) os.chdir(steps_path_folder) #here put the image pngs into the folder (instead of creating the folder) #convert image to unit8 otherwise warning first = t_num* images_jump second = t_num*images_jump+images_jump img_save_1 = reshape_data(img, "TZXY","XY", T=first, Z=z_num) img_save_1 = create_3D_image(img_save_1, x_dim, y_dim) img_save_1 = convert(img_save_1, 0, 255, np.uint8) img_save_3 = reshape_data(img, "TZXY","XY", T=second, Z=z_num) img_save_3 = create_3D_image(img_save_3, x_dim, y_dim) img_save_3 = convert(img_save_3, 0, 255, np.uint8) img_save_2 = reshape_data(img, "TZXY","XY", T=first+1, Z=z_num) img_save_2 = create_3D_image(img_save_2, x_dim, y_dim) img_save_2 = convert(img_save_2, 0, 255, np.uint8) # saving images as PNG with png_writer.PngWriter("im1.png") as writer1: writer1.save(img_save_1) with png_writer.PngWriter("im3.png") as writer2: writer2.save(img_save_3) os.chdir(GT_path_folder) with png_writer.PngWriter("im2.png") as writer2: writer2.save(img_save_2)
def perform_max_z_creation(img_path_list, file_num, sub_save_location, split_training_test): os.chdir(sub_save_location) sub_folder = "sequences" sequence_path = os.path.join(sub_save_location, sub_folder) os.mkdir(sequence_path) os.chdir(sequence_path) t, z, y_dim,x_dim, img = load_img(img_path_list[file_num]) # txt_name_log = open(destination + "/name_log.txt", "a") # txt_name_log.write("{}, {}\n".format(new_folder_name, img_path_list[file_num]), ) # txt_name_log.close() for t_num in tqdm(range(0,t)): #create new directory-path file_folder = "%02d" % (t_num+1) t_folder = os.path.join(sequence_path, file_folder) os.mkdir(t_folder) os.chdir(t_folder) for z_num in range(0,z-2): slice_folder = "%04d" % (z_num+1) three_z_folder = os.path.join(t_folder, slice_folder) os.mkdir(three_z_folder) os.chdir(three_z_folder) #add new folder to txt-file decision_train_test = random.random() if decision_train_test < split_training_test: txt_file_train = open(sub_save_location + "/tri_trainlist.txt", "a") txt_file_train.write("{}/{}\n".format(file_folder,slice_folder)) txt_file_train.close() else: txt_file_test = open(sub_save_location + "/tri_testlist.txt", "a") txt_file_test.write("{}/{}\n".format(file_folder,slice_folder)) txt_file_test.close() #converting images in rgb and uint8 to save it like that img_save_1 = reshape_data(img, "TZXY","XY", T=t_num, Z=z_num) img_save_1 = create_3D_image(img_save_1, x_dim, y_dim) img_save_1 = convert(img_save_1, 0, 255, np.uint8) img_save_2 = reshape_data(img, "TZXY","XY", T=t_num, Z=z_num+1) img_save_2 = create_3D_image(img_save_2, x_dim, y_dim) img_save_2 = convert(img_save_2, 0, 255, np.uint8) img_save_3 = reshape_data(img, "TZXY","XY", T=t_num, Z=z_num+2) img_save_3 = create_3D_image(img_save_3, x_dim, y_dim) img_save_3 = convert(img_save_3, 0, 255, np.uint8) with png_writer.PngWriter("im1.png") as writer2: writer2.save(img_save_1) with png_writer.PngWriter("im2.png") as writer2: writer2.save(img_save_2) with png_writer.PngWriter("im3.png") as writer2: writer2.save(img_save_3) print("{}/{}\n".format(file_folder,slice_folder))
def test_reshape_data_kwargs_values( data, given_dims, return_dims, other_args, getitem_ops_for_expected, transposer, ): actual = reshape_data( data=data, given_dims=given_dims, return_dims=return_dims, **other_args, ) expected = data[getitem_ops_for_expected] if transposer is not None: if isinstance(data, np.ndarray): expected = np.transpose(expected, transposer) else: expected = da.transpose(expected, transposer) # Check that the output data is the same type as the input assert type(actual) == type(expected) if isinstance(data, da.core.Array): actual = actual.compute() expected = expected.compute() # Check actual data np.testing.assert_array_equal(actual, expected)
def test_reshape_data_values(data, given_dims, return_dims, idx_in, idx_out): slice_in = data[idx_in] actual = reshape_data(data=data, given_dims=given_dims, return_dims=return_dims) if isinstance(data, da.core.Array): slice_in = slice_in.compute() actual = actual.compute() np.testing.assert_array_equal(slice_in, actual[idx_out]) # Check that the output data is the same type as the input assert type(actual) == type(slice_in)
def upsample_z_creation(img_path_list, file_num, sub_save_location): os.chdir(sub_save_location) t, z, y_dim,x_dim, img = load_img(img_path_list[file_num]) folder_steps = str(file_num) + "_steps" folder_steps = os.path.join(sub_save_location,folder_steps) os.mkdir(folder_steps) # txt_name_log = open(destination + "/name_log.txt", "a") # txt_name_log.write("{}, {}\n".format(new_folder_name, img_path_list[file_num]), ) # txt_name_log.close() #create new directory-path for t_num in tqdm(range(0,t)): for z_num in range(z-1): #create new directory-path file_folder = ("f_%03d" %(file_num) + "-"+"t_%03d" %(t_num) +"-" +"z_%03d"%(z_num)) os.chdir(folder_steps) os.mkdir(file_folder) steps_path_folder = os.path.join(folder_steps, file_folder) os.chdir(steps_path_folder) # #here put the image pngs into the folder (instead of creating the folder) # #convert image to unit8 otherwise warning img_save_1 = reshape_data(img, "TZXY","XY", T=t_num, Z=z_num) img_save_1 = create_3D_image(img_save_1, x_dim, y_dim) img_save_1 = convert(img_save_1, 0, 255, np.uint8) img_save_2 = reshape_data(img, "TZXY","XY", T=t_num, Z=z_num+1) img_save_2 = create_3D_image(img_save_2, x_dim, y_dim) img_save_2 = convert(img_save_2, 0, 255, np.uint8) # # saving images as PNG with png_writer.PngWriter("im1.png") as writer1: writer1.save(img_save_1) with png_writer.PngWriter("im3.png") as writer2: writer2.save(img_save_2)
def test_reshape_data_shape( array_maker, data_shape, given_dims, return_dims, other_args, expected_shape, ): data = array_maker(data_shape) actual = reshape_data(data=data, given_dims=given_dims, return_dims=return_dims, **other_args) assert actual.shape == expected_shape # Check that the output data is the same type as the input assert type(actual) == type(data)
def field_worker( image_path, image_dims="CYX", out_dir=None, contrast_method="simple_quantile", contrast_kwargs=DEFAULT_CONTRAST_KWARGS, channels=DEFAULT_CHANNELS, channel_groups=DEFAULT_CHANNEL_GROUPS, verbose=False, ): r""" Process an entire field -- autocontrast + save, log info to df Args: image_path (str): location of input tiff image image_dims (str): input image dimension ordering, default="CYX" out_dir (str): where to save output images, default=None contrast_method (str): method for autocontrasting, default=="simple_quantile" contrast_kwargs (dict):, default=DEFAULT_CONTRAST_KWARGS channels (dict): {"name":index} map for input tiff, default=DEFAULT_CHANNELS channel_groups (dict): fluor/bf/seg grouping, default=DEFAULT_CHANNEL_GROUPS verbose (bool): print info while processing or not, default=False Returns: field_info_df (pd.DataFrame): info for each field, merged with info for each cell """ # early exit if file does not exist -- return df of just image_path = Path(image_path) if not image_path.is_file(): return pd.DataFrame({"field_image_path": [image_path]}) # set out dir if needed if out_dir is None: out_dir = Path.cwd() else: out_dir = Path(out_dir) out_dir = out_dir.absolute() # set out dir for rescaled images and create if needed output_field_image_dir = out_dir.joinpath("output_field_images") output_field_image_dir.mkdir(exist_ok=True) # contrast stretch the field channels Cmaxs, Cautos = read_and_contrast_image( image_path, contrast_method=contrast_method, contrast_kwargs=contrast_kwargs, channel_groups=channel_groups, image_dims=image_dims, verbose=verbose, ) # save original and rescaled field data rescaled_out_path = output_field_image_dir.joinpath( f"{image_path.stem}_rescaled.ome.tiff") field_info_df = pd.DataFrame({ "2D_fov_tiff_path": [image_path], "rescaled_2D_fov_tiff_path": [rescaled_out_path], }) # Reshape prior to sending to save out_data = np.array(Cautos) out_data = transforms.reshape_data(out_data, "CYX", "TZCYX", S=0) # Write with channel metadata channel_names = [ k for k, v in sorted(channels.items(), key=lambda kv: kv[1]) ] with OmeTiffWriter(rescaled_out_path, overwrite_file=True) as writer: writer.save(out_data, channel_names=channel_names) # extract napari annotation channel and grab unique labels for cells label_image = Cautos[channels["cell"]] labels = np.unique(label_image) cell_labels = np.sort(labels[labels > 0]) assert (cell_labels[0], cell_labels[-1]) == (1, len(cell_labels)) if verbose: print(f"processing {image_path}") print(f"found {len(cell_labels)} segmented cells") # partial function for iterating over all cells in an image with map _cell_worker_partial = partial( cell_worker, Cautos=Cautos, label_channel="cell", field_image_path=image_path, out_dir=out_dir, channels=channels, verbose=verbose, ) # iterate through all cells in an image all_cell_info_df = pd.concat(map(_cell_worker_partial, cell_labels), axis="rows", ignore_index=True) # merge cell-wise data into field-wise data all_cell_info_df["2D_fov_tiff_path"] = image_path field_info_df = field_info_df.merge(all_cell_info_df, how="inner") return field_info_df
def cell_worker( cell_label_value, Cautos=[], label_channel="cell", field_image_path="unnamed_image_field.tiff", out_dir=None, channels=DEFAULT_CHANNELS, verbose=False, ): r""" segment single cells + save, log info to df Args: cell_label_value (int): integer mask value in segmentation for this cell, Cautos (list): auto-contrasted images from single iimage field, default=[], label_channel (str): name of channel to use as the image mask, default=="cell", field_image_path (str): location of input tiff imagee field, default=="unnamed_image_field.tiff", out_dir (str): where to save output images, default==None, channels (dict): {"name":index} map for input tiff, default=DEFAULT_CHANNELS verbose (bool): print info while processing or not, default=False Returns: cell_info_df (pd.DataFrame): info for each cell """ field_image_path = Path(field_image_path) if out_dir is None: out_dir = Path.cwd() else: out_dir = Path(out_dir) out_dir = out_dir.absolute() img_out_dir = out_dir.joinpath("output_single_cell_images") img_out_dir.mkdir(exist_ok=True) # crop to single cell boundaries label_image = Cautos[channels[label_channel]] y, x = np.where(label_image == cell_label_value) crop_slice = np.s_[min(y):max(y) + 1, min(x):max(x) + 1] label_crop = label_image[crop_slice] mask = (label_crop == cell_label_value).astype(np.float64) out_data_sc = np.array([ img_as_ubyte_nowarn(rescale_intensity(c[crop_slice] * mask)) for c in Cautos ]) # Reshape prior to sending to save out_data_sc = transforms.reshape_data(out_data_sc, "CYX", "TZCYX", S=0) # save input field path, cell label value, and output single cell path out_image_path = img_out_dir.joinpath( f"{field_image_path.stem}_rescaled_cell{cell_label_value}.ome.tiff") cell_info_df = pd.DataFrame({ "2D_fov_tiff_path": [field_image_path], "cell_label_value": [cell_label_value], "rescaled_2D_single_cell_tiff_path": [out_image_path], }) # Write tiff with channel metadata channel_names = [ k for k, v in sorted(channels.items(), key=lambda kv: kv[1]) ] with OmeTiffWriter(out_image_path, overwrite_file=True) as writer: writer.save(out_data_sc, channel_names=channel_names) if verbose: print(f"cell_label_value = {cell_label_value}") return cell_info_df