def create_directories_chop(self, base_dir, prefix="", as_array=False, **kwargs): """ create directory structure for prepared images, and chop each image into an image per cell. Parameters: ----------- base_dir : string Path to directory in which to hold training and test datasets. A directory will be created if it does not already exist prefix: string prefix for image file names as_array: Boolean if True will save as a numpy array. If False, then images are saved as RGB .png files. **kwargs: additional arguments to chop functions """ utils.make_dir(base_dir) for group in self.img_dict.keys(): for key, img_list in self.img_dict[group].items(): # create directory item/key from key dir_path = os.path.join(os.path.abspath(base_dir), group, key) utils.make_dir(dir_path) # create and save images in dir_path for i, img in enumerate(img_list, 1): rgb_img = self.convert_to_rgb(img) # convert_to_rgb is a bit of a misnomer, actually just stacks # an image collection to a numpy array, can work with more # than three channels # # chop image into sub-img per cell # sometimes there is an error where we don't have all the # channel to stack into an array, not sure what is causing # this though we can skip any errors and carry on processing # the images rather than crash the entire session # # probably a much better way to handle this, but screw it try: sub_img_array = chop.chop_nuclei(rgb_img, **kwargs) for j, sub_img in enumerate(sub_img_array, 1): if as_array: # save as numpy array img_name = "{}_img_{}_{}.npy".format( prefix, i, j) full_path = os.path.join( os.path.abspath(dir_path), img_name) np.save(file=full_path, arr=sub_img, allow_pickle=False) else: # save as .png (has to be RGB) img_name = "{}_img_{}_{}.png".format( prefix, i, j) full_path = os.path.join( os.path.abspath(dir_path), img_name) io.imsave(fname=full_path, arr=sub_img) except ValueError: pass
def save_chopped(arr, directory, prefix="img", ext=".png", save_as="img"): """ Save chopped array from chop_nuclei() to a directory. Each image will be saved individually and consecutively numbered. Parameters: ----------- arr : np.ndarray numpy array from chop_nuclei() directory : string directory in which to save the images. prefix : string (default : "img") image prefix ext : string (default : ".png") file extension. options are .png and .jpg if saving as an image. Otherwise recommended extension for numpy arrays is .npy """ assert isinstance(arr, np.ndarray) _check_ext_args(ext) utils.make_dir(directory) # loop through images in array and save with consecutive numbers if save_as == "img": for i, img in enumerate(arr, 1): img_name = "{}_{}{}".format(prefix, i, ext) full_path = os.path.join(os.path.abspath(directory), img_name) io.imsave(fname=full_path, arr=img) else: for i, img in enumerate(arr, 1): arr_name = "arr_{}.npy".format(i) full_path = os.path.join(os.path.abspath(directory), arr_name) np.save(full_path, img)
def create_directories_chop(self, base_dir, **kwargs): """ create directory structure for prepared images, and chop each image into an image per cell. Parameters: ----------- base_dir : string Path to directory in which to hold training and test datasets. A directory will be created if it does not already exist **kwargs: additional arguments to chop functions """ utils.make_dir(base_dir) for group in self.img_dict.keys(): for key, img_list in self.img_dict[group].items(): # create directory item/key from key dir_path = os.path.join(os.path.abspath(base_dir), group, key) utils.make_dir(dir_path) # create and save images in dir_path for i, img in enumerate(img_list, 1): rgb_img = self.convert_to_rgb(img) # chop image into sub-img per cell # sometimes there is an error where we don't have all the # channel to stack into an array, not sure what is causing # this though we can skip any errors and carry on processing # the images rather than crash the entire session # # probably a much better way to handle this, but screw it try: sub_img_array = chop.chop_nuclei(rgb_img, **kwargs) for j, sub_img in enumerate(sub_img_array, 1): img_name = "img_{}_{}.npy".format(i, j) full_path = os.path.join(os.path.abspath(dir_path), img_name) np.save(file=full_path, arr=sub_img) except ValueError: pass
def create_directories(self, base_dir): """ create directory structure for prepared images Parameters: ----------- base_dir : string Path to directory in which to hold training and test datasets A directory will be created if it does not already exist """ utils.make_dir(base_dir) # create training and test directories for group in self.img_dict.keys(): for key, img_list in self.img_dict[group].items(): # create directory item/key from key dir_path = os.path.join(os.path.abspath(base_dir), group, key) utils.make_dir(dir_path) # create and save images in dir_path for i, img in enumerate(img_list, 1): # need to load images and merge rgb_img = self.convert_to_rgb(img) self.write_array_to_disk(img=rgb_img, name="img_{}".format(i), path=dir_path)
def create_directories_chop_par(self, base_dir, n_jobs=-1, size=200): """ create directory structure for prepared images, and chop each image into an image per cell. Parameters: ----------- base_dir : string Path to directory in which to hold training and test datasets. A directory will be created if it does not already exist **kwargs: additional arguments to chop functions """ if n_jobs < 1: n_jobs = multiprocessing.cpu_count() utils.make_dir(base_dir) for group in self.img_dict.keys(): for key, img_list in self.img_dict[group].items(): # create directory item/key from key dir_path = os.path.join(os.path.abspath(base_dir), group, key) utils.make_dir(dir_path) Parallel(n_jobs=n_jobs)(delayed(chopper)(img, dir_path, size) for img in img_list)