def add_czi(self, filename): """Add an additional czi file containing additional image(s). Arguments: filename (str): Path to the czi file to be added to the existing MultiFinder object. """ new_czi = czi_io.load_multi_czi(filename) stripped_fname = filename.split('/')[1] if len(new_czi[0].shape) == 4: # if it's a single img czi, not multi new_czi[0] = np.expand_dims(new_czi[0], axis=0) if new_czi[0].shape[1:] != self.cell_im.shape[1:]: # if diff CZXY raise ValueError('The new czi has a different shape than cell_im.') if new_czi[1] != self.cell_channels: raise ValueError('The new czi uses non-matching channels.') self.filenames.append(stripped_fname) self.f_to_s[stripped_fname] = range( self.cell_im.shape[0], self.cell_im.shape[0] + new_czi.shape[0]) self.cell_im = np.concatenate(self.cell_im, new_czi)
def __init__(self, filename, bg_index=-1, bg_filename='', log_path=None, oof_svm=None, optim=False): """Create a MultiFinder object. Arguments: filename (str): The path to an image file to initialize the object. bg_index (int, optional): If the initializing file is .czi format, this indicates the index within the czi file array that corresponds to the background image with no cells. Defaults to -1, which means the background image isn't contained within the initializing image file. bg_filename (str, optional): The path to an image file corresponding to the background image. This argument must be provided if the background stage position is not contained in the multi-image file provided by filename or an exception will occur. It should not be provided if the background image is contained within a multi-image file provided by filename. log_path (str, optional): The path to a log directory for saving images with defects in blur elimination. Images from blur elimination aren't saved unless this is provided. oof_svm (str, optional): Path to a trained scikit-learn svm.SVC() pickle to be used for eliminating out-of-focus planes of the stack. optim (bool, optional): Indicates whether or not this run is being performed on a subset of the data for threshold optimization purposes. Default is False (extract all images from the chosen file); if True, will only pull out the first three images. """ self.filenames = [filename] if bg_index == -1: if bg_filename == '': warn('No background image provided during initialization.') self.bg_origin = 'separate' # separate czi or tiff file self.bg_filename = bg_filename self.log_path = log_path if self.log_path is not None: if not os.path.isdir(self.log_path): try: os.makedirs(self.log_path) except FileExistsError: pass self.oof_svm = oof_svm else: self.bg_origin = 'slice' # slice of a multi-czi if '.tif' in self.filenames[0]: self.cell_im = io.imread(self.filenames[0]) # TODO: implement adding dimensions for multi-img/channels both # here and a method to add them later elif '.czi' in self.filenames[0]: cell_czi = czi_io.load_multi_czi(self.filenames[0]) self.cell_im = cell_czi[0] # if cell_im has shape C-Z-X-Y, not IMG-C-Z-X-Y, add axis for img if len(self.cell_im.shape) == 4: self.cell_im = np.expand_dims(self.cell_im, axis=0) if optim: print('Optimization mode: only using first three images.') if self.cell_im.shape[0] > 2: self.cell_im = self.cell_im[0:3, :, :, :, :] # add filename:slice #s dict to indicate which imgs came from where self.f_to_s = {self.filenames[0]: range(0, self.cell_im.shape[0])} self.cell_channels = cell_czi[1] if self.bg_filename != '': if '.tif' in self.bg_filename: self.bg_im = io.imread(self.bg_filename) # TODO: implement adding dimensions for multi-img/channels both # here and a method to add them later elif '.czi' in self.bg_filename: bg_czi = czi_io.load_single_czi(self.bg_filename) self.bg_im = np.expand_dims(bg_czi[0], axis=0) self.bg_channels = bg_czi[1] elif bg_index != -1: self.bg_im = self.cell_im[bg_index, :, :, :, :] # remove parts of cell_im that correspond to bg bg_mask = np.ones(shape=self.cell_im.shape, dtype=bool) bg_mask[bg_index, :, :, :, :] = False self.cell_im = self.cell_im[bg_mask] self.bg_channels = self.cell_channels if self.oof_svm is not None: self.flagged_oof_ims = np.zeros(self.cell_im.shape[0]) self.flagged_z_ims = np.zeros(self.cell_im.shape[0])
im_vector = im_vector[order_vect] slice_vector = slice_vector[order_vect] # open czi ref_csv ref_df = pd.read_csv( '/n/denic_lab/Users/nweir/python_packages/csth-imaging/8-7_im_list.csv') ind = 0 grad_arr = np.empty(shape=(50, 500)) print('beginning image processing....') print('----------------------------------------------------------------------') for i in np.unique(czi_vector): print('Processing czi #' + str(i + 1) + ' of 15') c_ims = im_vector[czi_vector == i] c_slices = slice_vector[czi_vector == i] c_czi = ref_df['files'].iloc[i] print(' opening czi...') (im_array, channels) = czi_io.load_multi_czi(c_czi) im_array = im_array[:, channels.index(633), :, :, :] # get 633 sub-array for j in range(0, c_ims.shape[0]): print(' processing slice ' + str(j + 1) + ' of ' + str(c_ims.shape[0])) curr_slice = im_array[c_ims[j], c_slices[j], :, :].astype('uint16') io.imsave( '/n/denic_lab/Lab/csth-output/svm_test_633/' + str(ind) + '.tif', curr_slice) print(' generating gradient...') grad_im = nd.gaussian_gradient_magnitude(curr_slice, sigma=(0.25, 0.25)) grad_im = grad_im.astype('float32') / np.amax(grad_im.flatten()) grad_im = grad_im * 65535 # make 16 bit print(' calculating histogram...') hist, bin_edges = np.histogram(grad_im.flatten(),
args = parser.parse_args() print(args) czi_dir = args.czi_dir if not czi_dir.endswith('/'): czi_dir = czi_dir + '/' output_dir = args.out_dir os.chdir(czi_dir) if not output_dir.endswith('/'): output_dir = output_dir + '/' if not os.path.exists(output_dir): os.makedirs(output_dir) czi_list = [f for f in os.listdir() if '.czi' in f] for f in czi_list: if 'Empty' in f: continue print('current file: ' + f) czi_arr, channel_arr = czi_io.load_multi_czi(czi_dir + f) print('czi array shape:') print(czi_arr.shape) if len(czi_arr.shape) == 4: czi_arr = np.expand_dims(czi_arr, axis=0) for im in range(0, czi_arr.shape[0]): for c in range(0, czi_arr.shape[1]): if channel_arr[c] == 561: channel = '594' else: channel = str(channel_arr[c]) io.imsave(output_dir+f[:-4]+'_'+str(im)+'_'+channel+'.tif', czi_arr[im, c, :, :, :])