def stack_to_tif(stack_path, compress=1, wipe=False): """ :param stack_path: string, full path of .stack file to be converted to .tif :param compress: int, compression level to use for tif file :param wipe: bool, if True stack file will be deleted after saving as tif :return: """ from numpy import array_equal from os import remove from os.path import split, sep from numpy import fromfile import volTools as volt from skimage.external import tifffile as tif dims = volt.getStackDims(split(stack_path)[0] + sep) im = fromfile(stack_path, dtype='int16') im = im.reshape(dims[-1::-1]) tif_path = stack_path.split('.')[0] + '.tif' tif.imsave(tif_path, im, compress=compress) if wipe: check_file = tif.imread(tif_path) if array_equal(check_file, im): print('Deleting {0}...'.format(stack_path)) remove(stack_path) else: print('{0} and {1} differ... something went wrong!'.format(stack_path, tif_path))
def save_TiffStack(dataset,filename="image",outputPath="./"): filename = filename.rstrip(".tiff") filename = filename.rstrip(".tif") outputFile = outputPath + filename + ".tiff" print("saving ... : " + outputFile) skTiff.imsave(outputFile,dataset) print("save complete: " + outputFile)
def save_tiff(img_stack, pwd): if pwd.endswith('.tif'): tiff.imsave(pwd, img_stack) else: for z,frame in enumerate(img_stack): fname = pwd + '_%03d' % (z+1) + '.tif' dir, file = os.path.split(fname) if not os.path.exists(dir): os.makedirs(dir) tiff.imsave(fname, frame)
def save_frame_to_tif(timestep, label_image, options): if options.is_groundtruth: filename = options.output_dir + '/man_track' + format(timestep, "0{}".format(options.filename_zero_padding)) + '.tif' else: filename = options.output_dir + '/mask' + format(timestep, "0{}".format(options.filename_zero_padding)) + '.tif' # label_image = np.swapaxes(label_image, 0, 1) if len(label_image.shape) == 2: # 2d vigra.impex.writeImage(label_image.astype('uint16'), filename) else: # 3D label_image = np.transpose(label_image, axes=[2, 0, 1]) tifffile.imsave(filename, label_image.astype('uint16'))
def save_log(self): if self.display_length is None: self.clear() raise LookupError("Please display sequence first!") if self.file_name is None: self._get_file_name() if self.keep_display == True: self.file_name += '-complete' elif self.keep_display == False: self.file_name += '-incomplete' # set up log object directory = os.path.join(self.log_dir, 'visual_display_log') if not (os.path.isdir(directory)): os.makedirs(directory) logFile = dict(self.seq_log) displayLog = dict(self.__dict__) displayLog.pop('seq_log') displayLog.pop('sequence') logFile.update({'presentation': displayLog}) file_name = self.file_name + ".pkl" # generate full log dictionary path = os.path.join(directory, file_name) ft.saveFile(path, logFile) print("\nLog file generated successfully. Log file path: ") print('{}'.format(path)) if self.is_save_sequence: tf.imsave(os.path.join(directory, self.file_name + '.tif'), self.sequence.astype(np.float32)) print('\nSequence file generated successfully. File path: ') print('{}'.format(os.path.join(directory, self.file_name + '.tif'))) backupFileFolder = self._get_backup_folder() if backupFileFolder is not None: if not (os.path.isdir(backupFileFolder)): os.makedirs(backupFileFolder) backupFilePath = os.path.join(backupFileFolder, file_name) ft.saveFile(backupFilePath, logFile) if self.is_save_sequence: tf.imsave(os.path.join(backupFileFolder, self.file_name + '.tif'), self.sequence.astype(np.float32)) print("\nBackup log file generated successfully. Backup log file path: ") print('{}'.format(backupFilePath)) else: print("\nDid not find backup path, no backup was saved.")
def transform(f): path = f city_dir_name = f.split("/")[-3] image = tifffile.imread(path) bands = [] for band in range(8): bands.append(equalize_adapthist(image[..., band]) * 2047) img = np.array(np.stack(bands, axis=-1), dtype="uint16") clahe_city_dir = os.path.join(wdata_dir, city_dir_name) os.makedirs(clahe_city_dir, exist_ok=True) mul_dir = os.path.join(clahe_city_dir, 'CLAHE-MUL-PanSharpen') os.makedirs(mul_dir, exist_ok=True) tifffile.imsave(os.path.join(mul_dir, f.split("/")[-1]), img, planarconfig='contig')
def SaveTopographImage(self, FileName=None): if FileName is None or FileName == False: # Set the default filename to the type of image (SumImage, etc...) FileName = os.path.join(self.Defaults['DefaultFileDialogDir'], str(self.comboTopograph.itemData(self.comboTopograph.currentIndex())) + '.tif') Opts = QtWidgets.QFileDialog.Option(0x40) # QtWidgets.QFileDialog.HideNameFilterDetails FileName, _ = QtWidgets.QFileDialog.getSaveFileName(self, caption='Save Topograph Image.', directory=FileName, filter='TIF image (*.tif);;All files(*.*)', options=Opts) if FileName != '': # Store the default path now that the user has selected a file. self.Defaults['DefaultFileDialogDir'], _ = os.path.split(str(FileName)) # Write the image file to disk. imsave(FileName, self.canvasTopograph.ImageData[:].astype('float32'))
def _save_frame_to_tif(self, timestep, label_image, output_dir, filename_zero_padding=3): """ Save a single frame to a 2D or 3D tif """ filename = os.path.join(output_dir, f"mask{timestep:0{filename_zero_padding}}.tif") # import ipdb; ipdb.set_trace() label_image = np.swapaxes(label_image.squeeze(), 0, 1) if len(label_image.shape) == 2: # 2d vigra.impex.writeImage(label_image.astype('uint16'), filename) elif len(label_image.shape) == 3: # 3D label_image = np.transpose(label_image, axes=[2, 0, 1]) tifffile.imsave(filename, label_image.astype('uint16')) else: raise ValueError("Image had the wrong dimensions, can only save 2 or 3D images with a single channel")
def stack_writer(out_dir="",file_name="",stack=[],metadata=""): stack_data=np.array(stack) md_name=file_name+".html" file_name+=".tif" full_save_path=os.path.join(out_dir,file_name) full_md_save_path=os.path.join(out_dir,md_name) imsave(full_save_path,stack_data) if metadata: if isinstance(metadata,str): with open(full_md_save_path,"w") as md_file: md_file.write(metadata) elif isinstance(metadata, list): metadata_str="<html>%s</html>" % "<br>".join(metadata) with open(full_md_save_path,"w") as md_file: md_file.write(metadata_str)
def stack_to_tif(stack_path): """ :param stack_path: string, full path of .stack file to be converted to .tif :return: """ from os.path import split, sep from numpy import fromfile import volTools as volt from skimage.external import tifffile as tif dims = volt.getStackDims(split(stack_path)[0] + sep) im = fromfile(stack_path, dtype='int16') im = im.reshape(dims[-1::-1]) tif_path = stack_path.split('.')[0] + '.tif' tif.imsave(tif_path, im, compress=1)
def imsave_al(image, im, path): dic = oib.image_info(im) if dic['time_interval'] == None: t = 0 else: t = float(dic['time_interval']) sx = (1/float(dic['xsize'])) filename = [os.path.splitext(filename)[0] for filename in os.listdir(path) if filename.endswith(('.nd2'))] filename i = 0 for img in image: i+=1 filename_save = (path+ '/' + ('%s%i' %(filename[0], i)) + '.tif') filename_save if os.path.isfile(filename_save) == True: print 'the file already exist' else: tifffile.imsave(filename_save, img.transpose(0,1,4,2,3), imagej=True, resolution = (sx,sx), metadata = {'mode' : 'color', 'finterval' : t, 'unit' : 'micron'})
def file_writer(filename, signal, **kwds): """Writes data to tif using Christoph Gohlke's tifffile library Parameters ---------- filename: str signal: a Signal instance """ data = signal.data if signal.is_rgbx is True: data = rgb_tools.rgbx2regular_array(data) photometric = "rgb" else: photometric = "minisblack" if description not in kwds: if signal.metadata.General.title: kwds['description'] = signal.metadata.General.title imsave(filename, data, software="hyperspy", photometric=photometric, **kwds)
def importRawJCamF(path, saveFolder=None, dtype=np.dtype('<u2'), headerLength=116, tailerLength=218, column=2048, row=2048, frame=None, # how many frame to read crop=None): if frame: data = np.fromfile(path, dtype=dtype, count=frame * column * row + headerLength) header = data[0:headerLength] tailer = [] mov = data[headerLength:].reshape((frame, column, row)) else: data = np.fromfile(path, dtype=dtype) header = data[0:headerLength] tailer = data[len(data) - tailerLength:len(data)] frame = (len(data) - headerLength - tailerLength) / (column * row) mov = data[headerLength:len(data) - tailerLength].reshape((frame, column, row)) if saveFolder: if crop: try: mov = mov[:, crop[0]:crop[1], crop[2]:crop[3]] fileName = path.split('\\')[-1] + '_cropped.tif' except Exception as e: print 'importRawJCamF: Can not understant the paramenter "crop":' + str( crop) + '\ncorp should be: [rowStart,rowEnd,colStart,colEnd]' print '\nTrace back: \n' + e else: fileName = path.split('\\')[-1] + '.tif' tf.imsave(os.path.join(saveFolder, fileName), mov) return mov, header, tailer
else: ims = axs2[1].imshow( (np.abs(pupil)), extent=[-Kextent * n, Kextent * n, -Kextent * n, Kextent * n]) #plot the phase of the pupil axs2[1].set_title('|Pupil|') # finally, render the figures plt.show() ############################ ##### Save Psf to .tif file if SaveData: wavelength = 0.520 #um #you may want to specify a wavelength to save a calibrated PSF from skimage.external import tifffile as tif psf16 = np.transpose(np.abs(PSF), (2, 0, 1)) psf16 = (psf16 * (2**16 - 1) / np.amax(psf16)).astype( 'uint16') #normalize and convert to 16 bit psf16.shape = 1, N, 1, N, N, 1 # dimensions in TZCYXS order sampling = pixel_size / n * wavelength tif.imsave('psf.tif', psf16, imagej=True, resolution=(1.0 / sampling, 1.0 / sampling), metadata={ 'spacing': sampling, 'unit': 'um' })
def write_ometiff(output_path, array, omexml_string): tifffile.imsave(output_path, array, photometric='minisblack', description=omexml_string, metadata={'axes': 'TZCXY'})
def tif_writer(numpy_array="",path="",md_string=""): imsave("%s.tif" % path,numpy_array) if md_string: with open("%s.html" % path,"w") as md_file: md_file.write(md_string)
zyx = load_np(converted_points) zyx = np.asarray([ str((int(xx[0]), int(xx[1]), int(xx[2]))) for xx in load_np(converted_points) ]) from collections import Counter zyx_cnt = Counter(zyx) #manually call transformix.. c_rfe = '/home/wanglab/wang/pisano/tracing_output/antero_4x/20170115_tp_bl6_lob6a_1000r_02/20170115_tp_bl6_lob6a_1000r_647_010na_z7d5um_125msec_10povlp_resized_ch00_resampledforelastix.tif' transformed_dst = '/home/wanglab/wang/pisano/tracing_output/antero_4x/20170115_tp_bl6_lob6a_1000r_02/3dunet_output/transformed_points' transformfile = '/home/wanglab/wang/pisano/tracing_output/antero_4x/20170115_tp_bl6_lob6a_1000r_02/elastix/TransformParameters.1.txt' transformix_command_line_call(c_rfe, transformed_dst, transformfile) #cell_registered channel cell_reg = tifffile.imread( '/home/wanglab/wang/pisano/tracing_output/antero_4x/20170115_tp_bl6_lob6a_1000r_02/3dunet_output/transformed_points/result.tif' ) cell_cnn = np.zeros_like(cell_reg) for zyx, v in zyx_cnt.iteritems(): z, y, x = [ int(xx) for xx in zyx.replace( '(', '', ).replace(')', '').split(',') ] cell_cnn[z, y, x] = v * 100 merged = np.stack([cell_cnn, cell_reg, np.zeros_like(cell_reg)], -1) tifffile.imsave('/home/wanglab/Downloads/merged.tif', merged) #out = np.concatenate([cell_cnn, cell_reg, ], 0)
'(', '', ).replace(')', '').split(',') ] try: cnn_cellvolloaded[z, y, x] = v * 100 except Exception as e: print(e) merged = np.stack([ cnn_cellvolloaded, cellvolloaded, np.zeros_like(cellvolloaded) ], -1) merged = np.swapaxes(merged, 0, 2) #reorient to horizontal tifffile.imsave( os.path.join( dst, 'generate_downsized_overlay_{}_points_merged_resampled_for_elastix.tif' .format(os.path.basename(fld))), merged) #EXAMPLE USING LIGHTSHEET - assumes marking centers in the 'raw' full sized cell channel. This will transform those centers into "atlas" space (in this case the moving image) #in this case the "inverse transform has the atlas as the moving image in the first step, and the autofluorescence channel as the moving image in the second step r2s0 = [ xx for xx in listall(cellvol.inverse_elastixfld, 'reg2sig_TransformParameters.0.txt') if 'cellch' in xx ][0] r2s1 = [ xx for xx in listall(cellvol.inverse_elastixfld, 'reg2sig_TransformParameters.1.txt') if 'cellch' in xx ][0]
self.stack = self.stack.astype(np.uint8) # test s = AlignStack('C:/Users/ad245339/Desktop/images/Dissolution Mesbah-Tocino 2011/AM39-FDA-suivi disso X5000') s.run4() s.cropage() imsave('test.tif', s.stackAligne) """ """
def build_iscat_training(bf_filepaths, iscat_filepaths, sampling=4): """Creates iscat training data and target in data/iscat_seg/[REF_FRAMES / MASKS] for the iSCAT cell segmentation task ARGS: bf_filepaths (list(str)): filepaths of all the bright field images to input as returned by utilitiues.load_data_paths() iscat_filepaths (list(str)): filepaths of all the iscat images to input as returned by utilitiues.load_data_paths() sampling (int): sampling interval of the saved images (lower storage footprint) """ OUT_PATH = DATA_PATH + 'iscat_seg/' os.makedirs(os.path.join(OUT_PATH, 'REF_FRAMES/'), exist_ok=True) os.makedirs(os.path.join(OUT_PATH, 'MASKS/'), exist_ok=True) # Range of non filtered elements [px] min_size, max_size = 1, 13 iscat_stacks = (utilities.load_imgs(path) for path in iscat_filepaths) bf_stacks = (utilities.load_imgs(path) for path in bf_filepaths) # Returns the metadata of the exwperiments such as frame rate metadatas = get_experiments_metadata(iscat_filepaths) if torch.cuda.is_available(): device = torch.cuda.current_device() torch.cuda.set_device(device) print("Running on: {:s}".format(torch.cuda.get_device_name(device))) cuda = torch.device('cuda') else: # Doesn't run on CPU only machines comment if no GPU print("No CUDA device found") sys.exit(1) unet = UNetCell(1, 1, device=cuda, bilinear_upsampling=False) unet.load_state_dict(torch.load('outputs/saved_models/bf_unet.pth')) for i, (bf_stack, iscat_stack, metadata) in enumerate(zip(bf_stacks, iscat_stacks, metadatas)): if i < 45: continue bf_stack = bf_stack.astype('float32') print(bf_stack.shape) if bf_stack.shape[1:] != iscat_stack.shape[1:]: bf_stack = processing.coregister(bf_stack, 1.38) print(bf_stack.shape) normalize(bf_stack) # Samples iscat image to correct for the difference in framefate iscat_stack = iscat_stack[::sampling * int(metadata['iscat_fps'] / metadata['tirf_fps'])] torch_stack = torch.from_numpy(bf_stack).unsqueeze(1).cuda() mask = unet.predict_stack( torch_stack).detach().squeeze().cpu().numpy() > 0.05 mask = morphology.grey_erosion(mask * 255, structure=processing.structural_element( 'circle', (3, 5, 5))) mask = morphology.grey_closing(mask, structure=processing.structural_element( 'circle', (3, 7, 7))) mask = (mask > 50).astype('uint8') # Median filtering and normalization iscat_stack = processing.image_correction(iscat_stack) # Contrast enhancement iscat_stack = processing.enhance_contrast(iscat_stack, 'stretching', percentile=(1, 99)) # Fourier filtering of image iscat_stack = processing.fft_filtering(iscat_stack, min_size, max_size, True) iscat_stack = processing.enhance_contrast(iscat_stack, 'stretching', percentile=(3, 97)) for j in range(0, min(iscat_stack.shape[0], mask.shape[0]), sampling): if iscat_stack[j].shape == mask[j].shape: # Doesn't save images without detected cells if mask[j].max() == 0: continue print("\rSaving to stack_{}_{}.png".format(i + 1, j + 1), end=' ' * 5) tifffile.imsave( os.path.join(OUT_PATH, 'REF_FRAMES/', "stack_{}_{}.png".format(i + 1, j + 1)), rescale(iscat_stack[j])) tifffile.imsave( os.path.join(OUT_PATH, 'MASKS/', "mask_{}_{}.png".format(i + 1, j + 1)), mask[j] * 255) else: print("Error, shape: {}, {}".format(iscat_stack[j].shape, mask[j].shape)) break print('')
def test(self): del self.WGANVGG # load self.WGANVGG_G = WGAN_VGG_generator().to(self.device) self.load_model() # compute PSNR, SSIM, RMSE ori_psnr_avg, ori_ssim_avg = 0, 0 pred_psnr_avg, pred_ssim_avg = 0, 0 with torch.no_grad(): num_total_img = len(self.test_list) for img_idx, img_path in enumerate(self.test_list): img_name = os.path.basename(img_path) img_path = os.path.abspath(img_path) print("[{}/{}] processing {}".format( img_idx, num_total_img, os.path.abspath(img_path))) gt_img_path = self.test_gt_list[img_idx] gt_img = imread(gt_img_path) input_img = imread(img_path) img_patch_dataset = ImageDataset(self.opt, input_img) img_patch_dataloader = DataLoader( dataset=img_patch_dataset, batch_size=self.opt.batch_size, shuffle=False) img_shape = img_patch_dataset.get_img_shape() pad_img_shape = img_patch_dataset.get_padded_img_shape() out_list = [] for i, x in enumerate(img_patch_dataloader): x = x.float().to(self.device) pred = self.WGANVGG_G(x) pred = pred.to('cpu').detach().numpy() out_list.append(pred) out = np.concatenate(out_list, axis=0) out = out.squeeze() img_name = 'out-' + img_name base_name = os.path.basename(self.opt.checkpoint_dir) test_result_dir = os.path.join(self.opt.test_result_dir, base_name) if not os.path.exists(test_result_dir): os.makedirs(test_result_dir) dst_img_path = os.path.join(test_result_dir, img_name) out_img = mp.recon_patches(out, pad_img_shape[1], pad_img_shape[0], self.opt.patch_size, self.opt.patch_offset) out_img = mp.unpad_img(out_img, self.opt.patch_offset, img_shape) input_img = torch.Tensor(input_img) out_img = torch.Tensor(out_img) gt_img = torch.Tensor(gt_img) input_img = self.trunc( self.denormalize_(input_img).cpu().detach()) out_img = self.trunc(self.denormalize_(out_img).cpu().detach()) gt_img = self.trunc(self.denormalize_(gt_img).cpu().detach()) # x = self.trunc(self.denormalize_(x)) # out_img = self.trunc(self.denormalize_(out_img)) # gt_img = self.trunc(self.denormalize_(gt_img)) data_range = self.trunc_max - self.trunc_min original_result, pred_result = compute_measure( input_img, gt_img, out_img, data_range) op, oos, _ = original_result pp, ps, _ = pred_result ori_psnr_avg += op ori_ssim_avg += oos pred_psnr_avg += pp pred_ssim_avg += ps out_img = self.normalize_(out_img) out_img = out_img.cpu().numpy() imsave(dst_img_path, out_img) aop = ori_psnr_avg / (img_idx + 1) aos = ori_ssim_avg / (img_idx + 1) app = pred_psnr_avg / (img_idx + 1) aps = pred_ssim_avg / (img_idx + 1) print( "((ORIGIN)) PSNR : {:.5f}, SSIM : {:.5f}, ((PREP)) PSNR : {:.5f}, SSIM : {:.5f}" .format(aop, aos, app, aps))
b = [] x_range = np.arange(0, 100, 1) y_range = np.arange(0, 100, 1) z_range = np.arange(40, 43, 3) try: a, b = fastScan(piStage, ps, n_captures=3000, x_range=x_range, y_range=y_range, z_range=z_range) print('TotalTime:', time.time() - start0) print(piStage.CloseConnection()) ps.close() imsave('dataA.tif', a) imsave('dataB.tif', a) from pylab import * matshow(a[0]) show(0) except: traceback.print_exc() print(piStage.CloseConnection()) ps.close() #plot(dataA) #plot(dataB) #show(0)
as of march 2019 numpy and scikit-image need updating to newer versions using conda conda install -c anaconda numpy=1.16.0 conda install -c anaconda scikit-image=0.14.2 as does tensor flow gpu - the version installed by flowdec have a dll load error. watch out for cuda / tensorflow versions compatabiliries. conda install -c anaconda tensorflow-gpu I got a load dll error before this install, but tensor flow updated to v1.13 tested ok """ from skimage.external.tifffile import imsave, imread from flowdec import data as fd_data from flowdec import restoration as fd_restoration # Load test image from same dir as we execute in raw = imread('C1-YeastTNA1_1516_conv_RG_26oC_003.tif') # Load psf kernel image from same dir kernel = imread('gpsf_3D_1514_a3_001_WF-sub105.tif') # Run the deconvolution process and note that deconvolution initialization is best kept separate from # execution since the "initialize" operation corresponds to creating a TensorFlow graph, which is a # relatively expensive operation and should not be repeated across multiple executions n_iter = 500 algo = fd_restoration.RichardsonLucyDeconvolver(raw.ndim).initialize() res = algo.run(fd_data.Acquisition(data=raw, kernel=kernel), niter=n_iter).data # save the result, print('Saving result image TIFF file') # using skimage.external.tifffile.imsave imsave(('result' + str(n_iter) + 'iterations.tif'), res)
from skimage.external import tifffile print(sys.argv) print(os.environ["SLURM_ARRAY_TASK_ID"]) jobid = int(os.environ["SLURM_ARRAY_TASK_ID"]) src = "/jukebox/scratch/kellyms" pths = [ os.path.join(src, xx) for xx in os.listdir(src) if "m6" in xx or "f6" in xx ] if jobid > len(pths) + 1: print("array jobs greater than number of brains") else: print("processing brain: {}".format(pths[jobid])) arrpth = os.path.join( pths[jobid], "transformed_annotations/transformed_annotations.npy") dst = os.path.join(pths[jobid], "annotations_as_single_tifs") if not os.path.exists(dst): os.mkdir(dst) arr = np.lib.format.open_memmap(arrpth, dtype="uint16", mode="r") print("\nfile with shape: {}".format(arr.shape)) for z in range(arr.shape[0]): tifffile.imsave(os.path.join( dst, "annotation_Z{}.tif".format(str(z).zfill(4))), arr[z], compress=6) print("\nmade z plane # {}".format(z))
mm = stat.readDataGroup(mm_du_heatmaps) fm = stat.readDataGroup(fm_du_heatmaps) mf = stat.readDataGroup(mf_du_heatmaps) #find mean and standard deviation of heatmap in each group mm_mean = np.mean(mm, axis=0) mm_std = np.std(mm, axis=0) fm_mean = np.mean(fm, axis=0) fm_std = np.std(fm, axis=0) mf_mean = np.mean(mf, axis=0) mf_std = np.std(mf, axis=0) #write mean and standard dev maps to destination tifffile.imsave(os.path.join(pvaldst, "mm_mean.tif"), np.transpose(mm_mean, [1, 0, 2]).astype("float32")) tifffile.imsave(os.path.join(pvaldst, "mm_std.tif"), np.transpose(mm_std, [1, 0, 2]).astype("float32")) tifffile.imsave(os.path.join(pvaldst, "fm_mean.tif"), np.transpose(fm_mean, [1, 0, 2]).astype("float32")) tifffile.imsave(os.path.join(pvaldst, "fm_std.tif"), np.transpose(fm_std, [1, 0, 2]).astype("float32")) tifffile.imsave(os.path.join(pvaldst, "mf_mean.tif"), np.transpose(mf_mean, [1, 0, 2]).astype("float32")) tifffile.imsave(os.path.join(pvaldst, "mf_std.tif"), np.transpose(mf_std, [1, 0, 2]).astype("float32")) #Generate the p-values map ##########################
def generate_patch(**params): """ Function to patch up data and make into memory mapped array Inputs ----------- src = folder containing tiffs patch_dst = location to save memmap array patchlist = list of patches generated from make_indices function stridesize = (90,90,30) - stride size in 3d ZYX patchsize = (180,180,60) - size of window ZYX mode = "folder" #"folder" = list of files where each patch is a file, "memmap" = 4D array of patches by Z by Y by X Returns ------------ location of patched memory mapped array of shape (patches, patchsize_z, patchsize_y, patchsize_x) """ #load array input_arr = load_memmap_arr( os.path.join(params["data_dir"], "input_memmap_array.npy")) #set patch destination patch_dst = os.path.join(params["data_dir"], "input_chnks") #set variables if patch_dst[-4:] == ".npy": patch_dst = patch_dst[:-4] if not os.path.exists(patch_dst): os.mkdir(patch_dst) window = params["window"] patchsize = params["patchsz"] jobid = int(params["jobid"]) #set patch no. to run through cnn #select the file to process for this array job if jobid > len(params["patchlist"]) - 1: sys.stdout.write("\njobid {} > number of files".format(jobid)) sys.stdout.flush() else: #patch for i, p in enumerate(params["patchlist"]): if i == jobid: v = input_arr[p[0]:p[0] + patchsize[0], p[1]:p[1] + patchsize[1], p[2]:p[2] + patchsize[2]] #padding to prevent cnn erros if v.shape[0] < window[0]: pad = np.zeros( (window[0] - v.shape[0], v.shape[1], v.shape[2])) v = np.append(v, pad, axis=0) if v.shape[1] < window[1]: pad = np.zeros( (v.shape[0], window[1] - v.shape[1], v.shape[2])) v = np.append(v, pad, axis=1) if v.shape[2] < window[2]: pad = np.zeros( (v.shape[0], v.shape[1], window[2] - v.shape[2])) v = np.append(v, pad, axis=2) #saving out tifffile.imsave(os.path.join( patch_dst, "patch_{}.tif".format(str(i).zfill(10))), v.astype("float32"), compress=1) if params["verbose"]: print("{} of {}".format(i, len(params["patchlist"]))) #return return patch_dst
#run_list = ['scenes_run1','scenes_run2','scenes_run3','scenes_run3','scenes_run4','scenes_run5','scenes_run6','scenes_run7','scenes_run8','scenes_run9','scenes_run10'] #figure out directories to search dst_dir = os.path.join(rootdir, animalid, session, acquisition, 'all_combined', 'block_reduced') if not os.path.isdir(dst_dir): os.makedirs(dst_dir) for run in run_list: #run = run_list[0] data_dir = os.path.join(rootdir, animalid, session, acquisition, run) raw_dir = glob.glob(os.path.join(data_dir, 'raw*'))[0] print(raw_dir) src_dir = os.path.join(raw_dir, '*.tif') for fn in glob.glob(src_dir): i0 = findOccurrences(fn, '/')[-1] i1 = findOccurrences(fn, '_')[-1] new_fn = '%s_%s_%s' % (acquisition, run, fn[i1 + 1:]) print(new_fn) stack0 = imread(fn) stack1 = block_mean_stack(stack0, (2, 2)) print(stack1.shape) imsave(os.path.join(dst_dir, new_fn), stack1)
requires installation of python and other python packlages, which are included in the anaconda python release. pyCUDAdecon and instructions to install and use are at https://github.com/tlambert03/pycudadecon """ #import required packages from pycudadecon import decon, make_otf from skimage.external.tifffile import imsave # set input a and output file paths image_path = 'C1-YeastTNA1_1516_conv_RG_26oC_003.tif' psf_path = 'gpsf_3D_1514_a3_001_WF-sub105.tif' otfOutPath = 'otf.tif' # do the deconvolution on GPU numIters = 30 result = decon(image_path, psf_path, n_iters=numIters) print("result data type is " + str(result.dtype)) print('results numpy array shape is ') print(result.shape) # save the result, print('Saving result image TIFF file') # using skimage.external.tifffile.imsave imsave(('result' + str(numIters) + 'iterations.tif'), result) #, imagej) # save otf tiff file from PSF image tiff. print('making otf') make_otf(psf_path, otfOutPath, dzpsf=0.15, dxpsf=0.05, wavelength=510, na=1.3, nimm=1.333, otf_bgrd=None, krmax=0, fixorigin=10, cleanup_otf=True, max_otf_size=60000)
labels = np.unique(fov_ins_array[fov_ins_array > 0]) i = 0 for label in labels: i += 1 if i > 5: break fov_seg_array = closing(fov_ins_array == label, ball(1)) fov_label_array = skimage.measure.label(fov_seg_array) fov_labels = np.unique(fov_label_array[fov_label_array > 0]) label_sel = fov_labels[np.argmax( [np.sum(fov_label_array == x) for x in fov_labels])] fov_seg_array = fov_label_array == label_sel if np.sum(fov_seg_array) < 50: continue fov_skel_array, skel = skel_cal(fov_seg_array) skels_array[fov_skel_array > 0] = 1 ends, vecs = ends_cal(skel) ends_array[ends[:, 0], ends[:, 1], ends[:, 2]] = 1 fname = osp.splitext(osp.basename(fov_ins_path))[0] fpath_ends = osp.join(osp.dirname(fov_ins_path), "{}_ends.tif".format(fname)) fpath_skels = osp.join(osp.dirname(fov_ins_path), "{}_skels.tif".format(fname)) tifffile.imsave(fpath_ends, ends_array) tifffile.imsave(fpath_skels, skels_array)
def main(): # Initialize common configurations common_config = Config("../../config/common.config") # Get image sequence path and filename path = common_config["Image Sequence"]["Path"] filename = common_config["Image Sequence"]["Filename"] try: input_folder = sys.argv[1] make_legend = True except: # Get output folder output_folder = os.path.join("..", "..", "output", "tracking/", common_config["Output"]["Folder"]) # Get latest folder latest_results = get_latest_folder(output_folder) # Make input folder input_folder = os.path.join(output_folder, latest_results) # Do not make legend make_legend = False print "Input folder:", input_folder # Read trajectories filenames = filter(lambda fn: fn.lower().endswith(".zip"), os.listdir(input_folder)) labels = map(lambda fn: os.path.splitext(fn)[0], filenames) # Upload trajectories contours = {label : unzip_csv(os.path.join(input_folder, filename)) for filename, label in zip(filenames, labels)} contours_n = len(contours) # Read image sequence input_sequence = np.squeeze(imread(os.path.join(path, filename))).astype(np.float32) input_sequence /= input_sequence.max() frame_n = len(input_sequence) try: fns, colors = colors_from_csv(os.path.join(input_folder, "colors.csv")) color_map = {fn: clr for fn, clr in zip(fns, colors)} except: colors = get_random_colors(contours_n, "hsv") color_map = {fn: clr for fn, clr in zip(labels, colors)} rgb_stack = [] filaments_mask = np.zeros_like(input_sequence[0], dtype=np.uint16) for frame_i in xrange(frame_n): sys.stdout.write("\r Frame: " + str(frame_i)) image_rgb = np.dstack([input_sequence[frame_i]] * 3) for filament_i, filament_data in enumerate(contours.items()): tname, tcoords = filament_data try: fiber_color = color_map[tname] except: fiber_color = colors[fiber_color] cv2.polylines(image_rgb, [tcoords[frame_i].astype(np.int32)], False, fiber_color, 1, 8) cv2.polylines(filaments_mask[frame_i], [tcoords[frame_i].astype(np.int32)], False, filament_i + 1) rgb_stack.append((image_rgb * 255).astype(np.uint8)) sys.stdout.write("\r Finished.\n") imsave(os.path.join(input_folder, "overlay.tif"), np.asarray(rgb_stack)) imsave(os.path.join(input_folder, "index.tif"), filaments_mask) # Make legend if make_legend: labels = contours.keys() filament = [contour[0] for contour in contours.values()] legend = gen_legend(filament, labels, colors, input_sequence[0]) # Save legend imsave(os.path.join(input_folder, "legend.tif"), (legend * 255).astype(np.uint8))
p2=swc_array[i + 1, 2:5][::-1], size=shape) else: print("The line {} have error. s: {}".format(i, s)) break mask[line > 0] = label mask = dilation(mask, ball(1)) return mask if __name__ == "__main__": # test() # m = Connect_2_points(p1=[173, 140, 10], p2=[173, 139, 9]) # m = Connect_2_points(p1=[172, 136, 3], p2=[173, 138, 4]) # m = Connect_2_points(p1=[0, 0, 3], p2=[0, 0, 8]) # print(m.sum()) # connect_2_points_() swc_path = r"C:\Users\Administrator\Desktop\Syn_chaos_300_5_2_4_6_000\swcs\Syn_chaos_300_5_2_4_6_000_4.swc" path, name = os.path.split(swc_path) label = str_to_float(name.split('.')[0].split('_')[-1]) swc_num = np.array(psc(path, name.split('.')[0])) c_e = find_cross_end(swc_num) mask = sparse2dense_Loop(swc_num, shape=[300, 300, 300], label=label) tifffile.imsave(os.path.join(path, name.split('.')[0] + '.tif'), (mask > 0).astype(np.float32)) print('')
array = labelled [0] # array = array.astype(np.uint8) # calculating frequency of label numbers unique, counts = np.unique(array, return_counts=True) #threshold frequency(volume) volume_thres = 400 # list of objects smaller than threshold rmv = [] for i in range(len(counts)): if counts[i] < volume_thres: rmv.append(i) #%% for i in range(50): image = array[i] image = vol_filter(image) imsave('00000' + str(i) + '.tif', image) ## single image of stack #filtered = np.zeros(stack.shape,dtype = np.uint8) #for idx, frame in tqdm(enumerate(array)): # filtered[idx] = vol_filter(frame) #save_path = r'\Users\aishw\Desktop\ImgProc\zstack\PythonVer\Ver2\Filtered.tif' #imsave(save_path, filtered)
def toarray(f): with open(f) as fid: return frombuffer(fid.read(), dtype).reshape(dims, order='F') os.system('mkdir neurofinder.%s/images-tif' % name) if downsample: for i in range(len(files) / 4): if i * 4 + 4 <= len(files): tmp = zeros(dims) for j in range(4): im = toarray(files[i * 4 + j]) im = im.clip(0, im.max()).astype('uint16') tmp += im tmp = (tmp / 4.0).astype('uint16') tifffile.imsave( 'neurofinder.%s/images-tif/image%05g.tiff' % (name, i), im) else: for i, f in enumerate(files): im = toarray(f) im = im.clip(0, im.max()).astype('uint16') tifffile.imsave( 'neurofinder.%s/images-tif/image%05g.tiff' % (name, i), im) os.system('rm -rf neurofinder.%s/images' % name) os.system('mv neurofinder.%s/images-tif neurofinder.%s/images' % (name, name)) else: print('skipping tiff conversion\n\n') print('creating zip\n\n') os.system('zip -r neurofinder.%s.zip neurofinder.%s' % (name, name))
def identify_structures_w_contours(jobid, cores=5, make_color_images=False, overlay_on_original_data=False, consider_only_multipln_contours=False, **kwargs): '''function to take 3d detected contours and apply elastix transform ''' #######################inputs and setup################################################# ###inputs kwargs = load_kwargs(**kwargs) outdr = kwargs['outputdirectory'] vols = kwargs['volumes'] reg_vol = [xx for xx in vols if xx.ch_type == 'regch'][0] ###get rid of extra jobs if jobid >= len([xx for xx in vols if xx.ch_type != 'regch' ]): ###used to end jobs if too many are called print('jobid({}) >= volumes {}'.format( jobid, len([xx for xx in vols if xx.ch_type != 'regch']))) return ###volumes to process: each job represents a different contour volume vol_to_process = [xx for xx in vols if xx.ch_type != 'regch'][jobid] ch = vol_to_process.channel print(vol_to_process.ch_type) #find appropriate folders for contours if vol_to_process.ch_type == 'cellch': detect3dfld = reg_vol.celldetect3dfld coordinatesfld = reg_vol.cellcoordinatesfld elif vol_to_process.ch_type == 'injch': detect3dfld = reg_vol.injdetect3dfld coordinatesfld = reg_vol.injcoordinatesfld #set scale and atlas xscl, yscl, zscl = reg_vol.xyz_scale ###micron/pixel zmx, ymx, xmx = reg_vol.fullsizedimensions AtlasFile = reg_vol.atlasfile print('Using {} CORES'.format(cores)) try: p except NameError: p = mp.Pool(cores) resizefactor = kwargs['resizefactor'] brainname = reg_vol.brainname ############################################################################################################ #######################use regex to sort np files by ch and then by zpln#################################### ############################################################################################################ fl = [f for f in os.listdir(detect3dfld) if '.p' in f and 'ch' in f] #sorted for raw files reg = re.compile(r'(.*h+)(?P<ch>\d{2})(.*)(.p)') matches = map(reg.match, fl) ##load .np files sys.stdout.write( '\njobid({}), loading ch{} .p files to extract contour_class objects....' .format(jobid, ch)) contour_class_lst = [] for fl in [ os.path.join(detect3dfld, ''.join(xx.groups())) for xx in matches if xx.group('ch')[-2:] in ch ]: tmpkwargs = {} pckl = open(fl, 'rb') tmpkwargs.update(pickle.load(pckl)) pckl.close() if consider_only_multipln_contours == False: tmplst = tmpkwargs['single'] [tmplst.append(xx) for xx in tmpkwargs['multi']] elif consider_only_multipln_contours == True: tmplst = tmpkwargs['multi'] [contour_class_lst.append(xx) for xx in tmplst] sys.stdout.write('\ndone loading contour_class objects.\n') #check for successful loading if len(contour_class_lst) == 0: print('Length of contours in ch{} was {}, ending process...'.format( jobid, len(contour_class_lst))) try: p.terminate() except: 1 return ############################################################################################################ ##############################make color files############################################################## ############################################################################################################ if make_color_images == True: sys.stdout.write('\nmaking 3d planes...') sys.stdout.flush() valid_plns = range(0, zmx + 1) svlc = os.path.join(outdr, 'ch{}_3dcontours'.format(ch)) removedir(svlc) if overlay_on_original_data == False: ovly = False elif overlay_on_original_data == True: ovly = True iterlst = [] [ iterlst.append( (ch, svlc, contour_class_lst, valid_plns, outdr, vol_to_process, resizefactor, contour_class_lst, consider_only_multipln_contours, ovly, core, cores)) for core in range(cores) ] p.starmap(ovly_3d, iterlst) lst = os.listdir(svlc) lst1 = [os.path.join(svlc, xx) for xx in lst] lst1.sort() del lst del iterlst ###load ims and return dct of keys=str(zpln), values=np.array sys.stdout.write( '\n3d planes made, saved in {},\nnow compressing into single tifffile' .format(svlc)) imstack = tifffile.imread(lst1) del lst1 if len(imstack.shape) > 3: imstack = np.squeeze(imstack) try: ###check for orientation differences, i.e. from horiztonal scan to sagittal for atlas registration imstack = np.swapaxes(imstack, *kwargs['swapaxes']) except: pass tiffstackpth = os.path.join( outdr, '3D_contours_ch{}_{}'.format(ch, brainname)) tifffile.imsave(tiffstackpth, imstack.astype('uint16')) del imstack gc.collect() shutil.rmtree(svlc) sys.stdout.write('\ncolor image stack made for ch{}'.format(ch)) else: sys.stdout.write('\nmake_color_images=False, not creating images') ############################################################################################################ ######################apply point transform and make transformix input file################################# ############################################################################################################ ###find centers and add 1's to make nx4 array for affine matrix multiplication to account for downsizing ###everything is in PIXELS contourarr = np.empty((len(contour_class_lst), 3)) for i in range(len(contour_class_lst)): contourarr[i, ...] = contour_class_lst[ i].center ###full sized dimensions: if 3x3 tiles z(~2000),y(7680),x(6480) before any rotation try: contourarr = swap_cols( contourarr, *kwargs['swapaxes'] ) ###change columns to account for orientation changes between brain and atlas: if horizontal to sagittal==>x,y,z relative to horizontal; zyx relative to sagittal z, y, x = swap_cols(np.array([ vol_to_process.fullsizedimensions ]), *kwargs['swapaxes'])[ 0] ##convert full size cooridnates into sagittal atlas coordinates sys.stdout.write('\nSwapping Axes') except: ###if no swapaxes then just take normal z,y,x dimensions in original scan orientation z, y, x = vol_to_process.fullsizedimensions sys.stdout.write('\nNo Swapping of Axes') d1, d2 = contourarr.shape nx4centers = np.ones((d1, d2 + 1)) nx4centers[:, :-1] = contourarr ###find resampled elastix file dim print(os.listdir(outdr)) print([xx.channel for xx in vols if xx.ch_type == 'regch']) with tifffile.TiffFile([ os.path.join(outdr, f) for f in os.listdir(outdr) if 'resampledforelastix' in f and 'ch{}'.format( [xx.channel for xx in vols if xx.ch_type == 'regch'][0]) in f ][0]) as tif: zr = len(tif.pages) yr, xr = tif.pages[0].shape tif.close() ####create transformmatrix trnsfrmmatrix = np.identity(4) * ( zr / z, yr / y, xr / x, 1) ###downscale to "resampledforelastix size" sys.stdout.write('trnsfrmmatrix:\n{}\n'.format(trnsfrmmatrix)) #nx4 * 4x4 to give transform trnsfmdpnts = nx4centers.dot(trnsfrmmatrix) ##z,y,x sys.stdout.write('first three transformed pnts:\n{}\n'.format( trnsfmdpnts[0:3])) #create txt file, with elastix header, then populate points txtflnm = '{}_zyx_transformedpnts_ch{}.txt'.format(brainname, ch) pnts_fld = os.path.join(outdr, 'transformedpoints_pretransformix') makedir(pnts_fld) transforminput = os.path.join(pnts_fld, txtflnm) removedir(transforminput) ###prevent adding to an already made file writer(pnts_fld, 'index\n{}\n'.format(len(trnsfmdpnts)), flnm=txtflnm) sys.stdout.write( '\nwriting centers to transfomix input points text file: {}....'. format(transforminput)) stringtowrite = '\n'.join([ '\n'.join(['{} {} {}'.format(i[2], i[1], i[0])]) for i in trnsfmdpnts ]) ####this step converts from zyx to xyz***** writer(pnts_fld, stringtowrite, flnm=txtflnm) #[writer(pnts_fld, '{} {} {}\n'.format(i[2],i[1],i[0]), flnm=txtflnm, verbose=False) for i in trnsfmdpnts] ####this step converts from zyx to xyz***** sys.stdout.write('...done writing centers.') sys.stdout.flush() del trnsfmdpnts, trnsfrmmatrix, nx4centers, contourarr gc.collect() ############################################################################################################ ####################################elastix for inverse transform########################################### ############################################################################################################ transformfile = make_inverse_transform(vol_to_process, cores, **kwargs) assert os.path.exists(transformfile) sys.stdout.write( '\n***Elastix Inverse Transform File: {}***'.format(transformfile)) ############################################################################################################ ####################################transformix############################################################# ############################################################################################################ if make_color_images != False: #apply transform to 3d_tiffstack: transformimageinput = tiffstackpth elastixpth = os.path.join(outdr, 'elastix') trnsfrm_outpath = os.path.join( elastixpth, '3D_contours_ch{}_{}'.format(ch, brainname)) makedir(trnsfrm_outpath) writer(trnsfrm_outpath, '\nProcessing ch{} 3D...'.format(ch)) #transformfiles=[os.path.join(elastixpth, xx) for xx in os.listdir(os.path.join(outdr, 'elastix')) if "TransformParameters" in xx]; mxx=max([xx[-5] for xx in transformfiles]) #transformfile=os.path.join(elastixpth, 'TransformParameters.{}.txt'.format(mxx)) trnsfrm_out_file = os.path.join(trnsfrm_outpath, 'result.tif') #output of transformix transformimageinput_resized = transformimageinput[:-4] + '_resampledforelastix.tif' print('Resizing {}'.format(transformimageinput_resized)) resample_par(cores, transformimageinput, AtlasFile, svlocname=transformimageinput_resized, singletifffile=True, resamplefactor=1.7) sp.call([ 'transformix', '-in', transformimageinput_resized, '-out', trnsfrm_outpath, '-tp', transformfile ]) writer(trnsfrm_outpath, '\n Transformix File Generated: {}'.format(trnsfrm_out_file)) writer( trnsfrm_outpath, '\n Passing colorcode: {} file as {}'.format( ch, os.path.join(trnsfrm_outpath, 'depthcoded.png'))) ###depth coded image of transformix result; not functional yet #depth.colorcode(trnsfrm_out_file, trnsfrm_outpath) #getvoxels(trnsfrm_out_file, os.path.join(trnsfrm_outpath, 'zyx_voxels_{}.npy'.format(ch))) #allen_compare(AtlasFile, svlc, trnsfrm_outpath) ##if successful delete contour cooridnates and maybe contourdetect3d flds ############################################################ ##############apply transform to points##################### elastixpth = os.path.join(outdr, 'elastix_inverse_transform') trnsfrm_outpath = os.path.join(elastixpth, 'ch{}_3dpoints'.format(ch)) makedir(trnsfrm_outpath) writer(trnsfrm_outpath, '\n***********Starting Transformix for: {}***********'.format(ch)) sys.stdout.flush() #transformfiles=[os.path.join(elastixpth, xx) for xx in os.listdir(os.path.join(outdr, 'elastix')) if "TransformParameters" in xx]; mxx=max([xx[-5] for xx in transformfiles]) #transformfile=os.path.join(elastixpth, 'TransformParameters.{}.txt'.format(mxx)) trnsfrm_out_file = os.path.join( trnsfrm_outpath, 'outputpoints.txt') #MIGHT NEED TO CHANGE THIS sp.call([ 'transformix', '-def', transforminput, '-out', trnsfrm_outpath, '-tp', transformfile ]) #sp.call(['transformix', '-def', 'all', '-out', trnsfrm_outpath, '-tp', transformfile]) ##displacement field writer(trnsfrm_outpath, '\n Transformix File Generated: {}'.format(trnsfrm_out_file)) #################################################################################### ##############generate list and image overlaid onto allen atlas##################### #################################################################################### name = 'job{}_{}'.format(jobid, vol_to_process.ch_type) transformed_pnts_to_allen(trnsfrm_out_file, ch, cores, name=name, **kwargs) writer(outdr, '*************STEP 5*************\n Finished') print('end of script') try: p.terminate() except: 1 return
def save_tiff(img, filename): tifffile.imsave(filename, img)
def transformed_pnts_to_allen(trnsfrm_out_file, ch, cores, point_or_index=None, name=False, **kwargs): '''function to take elastix point transform file and return anatomical locations of those points point_or_index=None/point/index: determines which transformix output to use: point is more accurate, index is pixel value(?) Elastix uses the xyz convention rather than the zyx numpy convention ###ASSUMES INPUT OF XYZ ''' #####inputs assert type(trnsfrm_out_file) == str if point_or_index == None: point_or_index = 'OutputPoint' elif point_or_index == 'point': point_or_index = 'OutputPoint' elif point_or_index == 'index': point_or_index = 'OutputIndexFixed' try: #check to see if pool processes have already been spawned p except NameError: p = mp.Pool(cores) kwargs = load_kwargs(**kwargs) vols = kwargs['volumes'] reg_vol = [xx for xx in vols if xx.ch_type == 'regch'][0] ####load files id_table = pd.read_excel( os.path.join(kwargs['packagedirectory'], 'supp_files/id_table.xlsx') ) ##use for determining neuroanatomical locations according to allen ann = sitk.GetArrayFromImage(sitk.ReadImage( kwargs['annotationfile'])) ###zyx with open(trnsfrm_out_file, "rb") as f: lines = f.readlines() f.close() #####populate post-transformed array of contour centers sys.stdout.write('\n{} points detected'.format(len(lines))) arr = np.empty((len(lines), 3)) for i in range(len(lines)): arr[i, ...] = lines[i].split()[lines[i].split().index(point_or_index) + 3:lines[i].split().index(point_or_index) + 6] #x,y,z #optional save out of points np.save(kwargs['outputdirectory'] + '/injection/zyx_voxels.npy', np.asarray([(z, y, x) for x, y, z in arr])) pnts = transformed_pnts_to_allen_helper_func(arr, ann) pnt_lst = [xx for xx in pnts if xx != 0] if len(pnt_lst) == 0: raise ValueError('pnt_lst is empty') else: sys.stdout.write('\nlen of pnt_lst({})'.format(len(pnt_lst))) imstack = brain_structure_keeper( ann, True, *pnt_lst) ###annotation file, true=to depict density, list of pnts df = count_structure_lister(id_table, *pnt_lst) #########save out imstack and df nametosave = '{}{}_{}'.format(name, ch, reg_vol.brainname) tifffile.imsave( os.path.join(kwargs['outputdirectory'], nametosave + '_structure_density_map.tif'), imstack) excelfl = os.path.join(kwargs['outputdirectory'], nametosave + '_stuctures_table.xlsx') df.to_excel(excelfl) print('file saved as: {}'.format(excelfl)) try: p.terminate() except: 1 return
def pool_injections_for_analysis(**kwargs): inputlist = kwargs['inputlist'] dst = kwargs['dst']; makedir(dst) injscale = kwargs['injectionscale'] if 'injectionscale' in kwargs else 1 imagescale = kwargs['imagescale'] if 'imagescale' in kwargs else 1 axes = kwargs['reorientation'] if 'reorientation' in kwargs else ('0','1','2') cmap = kwargs['colormap'] if 'colormap' in kwargs else 'plasma' id_table = kwargs['id_table'] if 'id_table' in kwargs else '/jukebox/temp_wang/pisano/Python/lightsheet/supp_files/allen_id_table.xlsx' save_tif = kwargs['save_tif'] if 'save_tif' in kwargs else False num_sites_to_keep = kwargs['num_sites_to_keep'] if 'num_sites_to_keep' in kwargs else 1 nonzeros = [] ann = sitk.GetArrayFromImage(sitk.ReadImage(kwargs['annotation'])) if kwargs['crop']: ann = eval('ann{}'.format(kwargs['crop'])) allen_id_table=pd.read_excel(id_table) for i in range(len(inputlist)): impth = inputlist[i] animal = os.path.basename(os.path.dirname(os.path.dirname(os.path.dirname(impth)))) print('\n\n_______\n{}'.format(animal)) print(' loading:\n {}'.format(animal)) im = tifffile.imread(impth) if kwargs['crop']: im = eval('im{}'.format(kwargs['crop']))#; print im.shape #reorient to coronal? #segment arr = find_site(im, thresh=kwargs['threshold'], filter_kernel=kwargs['filter_kernel'], num_sites_to_keep=num_sites_to_keep)*injscale if save_tif: tifffile.imsave(os.path.join(dst,'{}'.format(os.path.dirname(impth))+'_inj.tif'), arr.astype('float32')) #optional 'save_individual' if kwargs['save_individual']: im = im*imagescale a=np.concatenate((np.max(im, axis=0), np.max(arr.astype('uint16'), axis=0)), axis=1) b=np.concatenate((np.fliplr(np.rot90(np.max(fix_orientation(im, axes=axes), axis=0),k=3)), np.fliplr(np.rot90(np.max(fix_orientation(arr.astype('uint16'), axes=axes), axis=0),k=3))), axis=1) plt.figure() plt.imshow(np.concatenate((b,a), axis=0), cmap=cmap, alpha=1); plt.axis('off') plt.savefig(os.path.join(dst,'{}'.format(animal)+'.pdf'), dpi=300, transparent=True) plt.close() #cell counts to csv print(' finding nonzero pixels for voxel counts...') nz = np.nonzero(arr) nonzeros.append(zip(*nz)) #<-for pooled image pos = transformed_pnts_to_allen_helper_func(np.asarray(zip(*[nz[2], nz[1], nz[0]])), ann) tdf = count_structure_lister(allen_id_table, *pos) if i == 0: df = tdf.copy() countcol = 'count' if 'count' in df.columns else 'cell_count' df.drop([countcol], axis=1, inplace=True) df[animal] = tdf[countcol] df.to_csv(os.path.join(dst,'voxel_counts.csv')) print('\n\nCSV file of cell counts, saved as {}\n\n\n'.format(os.path.join(dst,'voxel_counts.csv'))) #condense nonzero pixels nzs = [str(x) for xx in nonzeros for x in xx] #this list has duplicates if two brains had the same voxel w label c = Counter(nzs) array = np.zeros(im.shape) print('Collecting nonzero pixels for pooled image...') tick = 0 #generating pooled array where voxel value = total number of brains with that voxel as positive for k,v in c.iteritems(): k = [int(xx) for xx in k.replace('(','').replace(')','').split(',')] array[k[0], k[1], k[2]] = int(v) tick+=1 if tick % 50000 == 0: print(' {}'.format(tick)) #load atlas and generate final figure print('Generating final figure...') atlas = tifffile.imread(kwargs['atlas']) arr = fix_orientation(array, axes=axes) #cropping #if 'crop_atlas' not in kwargs: if kwargs['crop']: atlas = eval('atlas{}'.format(kwargs['crop'])) atlas = fix_orientation(atlas, axes=axes) my_cmap = eval('plt.cm.{}(np.arange(plt.cm.RdBu.N))'.format(cmap)) my_cmap[:1,:4] = 0.0 my_cmap = mpl.colors.ListedColormap(my_cmap) my_cmap.set_under('w') plt.figure() plt.imshow(np.max(atlas, axis=0), cmap='gray') plt.imshow(np.max(arr, axis=0), alpha=0.99, cmap=my_cmap); plt.colorbar(); plt.axis('off') dpi = int(kwargs['dpi']) if 'dpi' in kwargs else 300 plt.savefig(os.path.join(dst,'heatmap.pdf'), dpi=dpi, transparent=True); plt.close() print('Saved as {}'.format(os.path.join(dst,'heatmap.pdf'))) return df
def savetiff(name, data): fdir = os.path.dirname(name) if not os.path.isdir(fdir): os.makedirs(fdir, exist_ok=True) skitiff.imsave(name, data)
def writeOmeTiff(outputPath, array, omexmlString): tifffile.imsave(outputPath, array, description=omexmlString, metadata={'axes': 'TZCXY'})
# -*- coding: utf-8 -*- import os, sys from pydaily import filesystem from skimage.external.tifffile import imsave sys.path.insert(0, '.') from kfb_io.io_image import patch_read_slide if __name__ == "__main__": input_root = str(sys.argv[1]) # kfb folder save_root = str(sys.argv[2]) # tif folder if not os.path.exists(save_root): os.makedirs(save_root) kfb_files = filesystem.find_ext_files(input_root, "kfb") for ind, this_kfb_path in enumerate(kfb_files): print("Processing {}/{}".format(ind+1, len(kfb_files))) img_name = os.path.basename(this_kfb_path) img_name_noext = os.path.splitext(img_name)[0] this_raw_data = patch_read_slide(this_kfb_path, level=1) save_path = os.path.join(save_root, img_name_noext+'.tif') imsave(save_path, this_raw_data, compress=9, bigtiff=True)
def build_hand_segmentation_iscat_testval(): """Creates dataset of manually segmented iSCAT images for validation and testing""" OUT_PATH_CELL = DATA_PATH + 'hand_seg_iscat_cell/' os.makedirs(os.path.join(OUT_PATH_CELL, 'REF_FRAMES/'), exist_ok=True) os.makedirs(os.path.join(OUT_PATH_CELL, 'MASKS/'), exist_ok=True) OUT_PATH_PILI = DATA_PATH + 'hand_seg_iscat_pili/' os.makedirs(os.path.join(OUT_PATH_PILI, 'REF_FRAMES/'), exist_ok=True) os.makedirs(os.path.join(OUT_PATH_PILI, 'MASKS/'), exist_ok=True) ROOT_TEST_PATH = "data/hand-segmentation/" iscat_files = glob.glob(os.path.join(ROOT_TEST_PATH, 'iSCAT/*.tif')) cell_seg_files = glob.glob(os.path.join(ROOT_TEST_PATH, 'cell_seg/*.txt')) pili_seg_files = glob.glob(os.path.join(ROOT_TEST_PATH, 'pili_seg/*.txt')) iscat_files.sort() cell_seg_files.sort() pili_seg_files.sort() for i, (iscat, cell_seg, pili_seg) in enumerate( zip(iscat_files, cell_seg_files, pili_seg_files)): # Loading tirf and iSCAT images iscat_stack = tifffile.imread(iscat) # iSCAT preprocessing iscat_stack = processing.image_correction(iscat_stack) iscat_stack = processing.enhance_contrast(iscat_stack, 'stretching', percentile=(1, 99)) iscat_stack = processing.fft_filtering(iscat_stack, 1, 13, True) iscat_stack = processing.enhance_contrast(iscat_stack, 'stretching', percentile=(3, 97)) # Loading ground truth masks mask_cell = utilities.cell_mask_from_segmentation(cell_seg) mask_pili = utilities.pili_mask_from_segmentation(pili_seg) for j in range(0, iscat_stack.shape[0], 8): print("\rSaving to stack_{}_{}.png".format(i + 1, j + 1), end=' ' * 5) tifffile.imsave( os.path.join(OUT_PATH_CELL, 'REF_FRAMES/', "stack_{}_{}.png".format(i + 1, j + 1)), rescale(iscat_stack[j])) tifffile.imsave( os.path.join(OUT_PATH_CELL, 'MASKS/', "mask_{}_{}.png".format(i + 1, j + 1)), mask_cell[j // 2].astype('uint8')) print('') for j in range(iscat_stack.shape[0]): if not (mask_pili != 0).any(): continue print("\rSaving to stack_{}_{}.png".format(i + 1, j + 1), end=' ' * 5) tifffile.imsave( os.path.join(OUT_PATH_PILI, 'REF_FRAMES/', "stack_{}_{}.png".format(i + 1, j + 1)), rescale(iscat_stack[j])) tifffile.imsave( os.path.join(OUT_PATH_PILI, 'MASKS/', "mask_{}_{}.png".format(i + 1, j + 1)), mask_pili[j].astype('uint8')) print('')
"a") as txt: txt.write("Allen Atlas CCF coordinates (in zyx):\n%s" % zyx_rois) #atlas (horizontal) atl = tifffile.imread( "/jukebox/LightSheetData/witten-mouse/atlas/average_template_25_sagittal_forDVscans.tif" ) atl = np.transpose(atl, [2, 1, 0]) atl_cnn = np.zeros_like(atl) for i in range(zyx_rois.shape[0]): atl_cnn[zyx_rois[i][0], zyx_rois[i][1], zyx_rois[i][2]] = 1 merged = np.stack([atl, atl_cnn, np.zeros_like(atl)], -1) tifffile.imsave( os.path.join( dst, "{}_ROI_merged_to_Allen_horizontal.tif".format(brain)), merged) coronal = np.transpose(merged, [1, 0, 2, 3]) #make coronal sections #save out coronal sections - based on the fact that you click 5 points in each site in HORIZONTAL sectionss tifffile.imsave( os.path.join(dst, "{}_ROI_merged_to_Allen_coronal.tif".format(brain)), coronal) #doing a max projection, in case you just want to look at that maxip = np.max(coronal, 0) alpha = 0.6 #determines transparency, don"t need to alter cmap = matplotlib.colors.LinearSegmentedColormap.from_list(
from skimage.external.tifffile import imsave, imread import os result_dir = "../../../data/CycleGAN-data/result_mayo_blur_gaussian/128_longskip_mstd_r9/128_longskip_mstd_r9/test_latest/FakeClean/" result_list = sorted(os.listdir(result_dir)) for i in range(len(result_list)): im = imread(result_dir + result_list[i]) im[im < 0] = 0 im[im > 1] = 1 print(result_dir + result_list[i]) imsave(result_dir + result_list[i], im)
def overlay_qc(args): #unpacking this way for multiprocessing fld, folder_suffix, output_folder, verbose, doubletransform, make_volumes = args try: #get 3dunet cell dataframe csv file input_csv = listdirfull(os.path.join(fld, folder_suffix), ".csv") assert len(input_csv) == 1, "multiple csv files" dataframe = pd.read_csv(input_csv[0]) #location to save out dst = os.path.join(output_folder, os.path.basename(fld)); makedir(dst) #EXAMPLE USING LIGHTSHEET - assumes marking centers in the "raw" full sized cell channel. This will transform those #centers into "atlas" space (in this case the moving image) #in this case the "inverse transform has the atlas as the moving image in the first step, #and the autofluorescence channel as the moving image in the second step #NOTE - it seems that the registration of cell to auto is failing on occasion....thus get new files... ################################ cell_inverse_folder = listdirfull(os.path.join(fld, "elastix_inverse_transform"), "cellch")[0] a2r = listall(cell_inverse_folder, "atlas2reg_TransformParameters"); a2r.sort() r2s = listall(cell_inverse_folder, "reg2sig_TransformParameters"); r2s.sort() #possibly remove #IMPORTANT. the idea is to apply cfos->auto->atlas transformfiles = r2s + a2r if doubletransform else a2r #might get rid of r2s lightsheet_parameter_dictionary = os.path.join(fld, "param_dict.p") converted_points = generate_transformed_cellcount(dataframe, dst, transformfiles, lightsheet_parameter_dictionary, verbose=verbose) #load and convert to single voxel loc zyx = np.asarray([str((int(xx[0]), int(xx[1]), int(xx[2]))) for xx in np.nan_to_num(np.load(converted_points))]) from collections import Counter zyx_cnt = Counter(zyx) #check... if make_volumes: #manually call transformix kwargs = load_dictionary(lightsheet_parameter_dictionary) vol = [xx for xx in kwargs["volumes"] if xx.ch_type == "cellch"][0].resampled_for_elastix_vol transformed_vol = os.path.join(dst, "transformed_volume"); makedir(transformed_vol) if not doubletransform: transformfiles = [os.path.join(fld, "elastix/TransformParameters.0.txt"), os.path.join(fld, "elastix/TransformParameters.1.txt")] transformfiles = modify_transform_files(transformfiles, transformed_vol) #copy over elastix files transformix_command_line_call(vol, transformed_vol, transformfiles[-1]) else: v=[xx for xx in kwargs["volumes"] if xx.ch_type == "cellch"][0] #sig to reg tps = [listall(os.path.dirname(v.ch_to_reg_to_atlas), "/TransformParameters.0")[0], listall(os.path.dirname(v.ch_to_reg_to_atlas), "/TransformParameters.1")[0]] #reg to atlas transformfiles = tps+[os.path.join(fld, "elastix/TransformParameters.0.txt"), os.path.join(fld, "elastix/TransformParameters.1.txt")] transformfiles = modify_transform_files(transformfiles, transformed_vol) #copy over elastix files transformix_command_line_call(vol, transformed_vol, transformfiles[-1]) #cell_registered channel cell_reg = tifffile.imread(os.path.join(transformed_vol, "result.tif")) tifffile.imsave(os.path.join(transformed_vol, "result.tif"), cell_reg, compress=1) cell_cnn = np.zeros_like(cell_reg) tarr = []; badlist=[] for zyx,v in zyx_cnt.items(): z,y,x = [int(xx) for xx in zyx.replace("(","",).replace(")","").split(",")] tarr.append([z,y,x]) try: cell_cnn[z,y,x] = v*100 except: badlist.append([z,y,x]) #apply x y dilation r = 2 selem = ball(r)[int(r/2)] cell_cnn = cell_cnn.astype("uint8") cell_cnn = np.asarray([cv2.dilate(cell_cnn[i], selem, iterations = 1) for i in range(cell_cnn.shape[0])]) tarr=np.asarray(tarr) if len(badlist)>0: print("{} errors in mapping with cell_cnn shape {}, each max dim {}, \npossibly due to a registration overshoot \ or not using double transform\n\n{}".format(len(badlist), cell_cnn.shape, np.max(tarr,0), badlist)) merged = np.stack([cell_cnn, cell_reg, np.zeros_like(cell_reg)], -1) tifffile.imsave(os.path.join(transformed_vol, "merged.tif"), merged)#, compress=1) #out = np.concatenate([cell_cnn, cell_reg, ], 0) #####check at the resampled for elastix phase before transform...this mapping looks good... if make_volumes: #make zyx numpy arry zyx = dataframe[["z","y","x"]].values kwargs = load_dictionary(lightsheet_parameter_dictionary) vol = [xx for xx in kwargs["volumes"] if xx.ch_type =="cellch"][0] fullsizedimensions = get_fullsizedims_from_kwargs(kwargs) #don"t get from kwargs["volumes"][0].fullsizedimensions it"s bad! use this instead zyx = fix_contour_orientation(zyx, verbose=verbose, **kwargs) #now in orientation of resample zyx = points_resample(zyx, original_dims = fix_dimension_orientation(fullsizedimensions, **kwargs), resample_dims = tifffile.imread(vol.resampled_for_elastix_vol).shape, verbose = verbose)[:, :3] #cell channel cell_ch = tifffile.imread(vol.resampled_for_elastix_vol) cell_cnn = np.zeros_like(cell_ch) tarr = []; badlist=[] for _zyx in zyx: z,y,x = [int(xx) for xx in _zyx] tarr.append([z,y,x]) try: cell_cnn[z,y,x] = 100 except: badlist.append([z,y,x]) tarr=np.asarray(tarr) merged = np.stack([cell_cnn, cell_ch, np.zeros_like(cell_ch)], -1) tifffile.imsave(os.path.join(transformed_vol, "resampled_merged.tif"), merged)#, compress=1) except Exception as e: print(e) with open(error_file, "a") as err_fl: err_fl.write("\n\n{} {}\n\n".format(fld, e))
def save_tiff(path, np_image): imsave(path, np_image) print("{} save".format(path))
def sim_images(mphi=None, ephi=None, pscope=None, isl_shape=None, del_px=1, def_val=0, add_random=False, save_path=None, save_name=None, isl_thk=20, isl_xip0=50, mem_thk=50, mem_xip0=1000, v=1, Filter=True): """Simulate LTEM images for a given electron phase shift through a sample. This function returns LTEM images simulated for in-focus and at +/- def_val for comparison to experimental data and reconstruction. It was primarily written for simulating images of magnetic island structures, and as such the sample is defined in two parts: a uniform support membrane across the region and islands of magnetic material defined by an array isl_shape. The magnetization is defined with 2D arrays corresponding to the x- and y-components of the magnetization vector. There are many required parameters here that must be set to account for the support membrane. The default values apply to 20nm permalloy islands on a 50nm SiN membrane window. There is a known bug where sharp edges in the ephi creates problems with the image simulations. As a workaround, this function applies a light gaussian filter (sigma = 1 pixel) to the ephi and isl_shape arrays. This can be controlled with the ``filter`` argument. Args: mphi (2D array): Numpy array of size (M, N), magnetic phase shift ephi (2D array): Numpy array of size (M, N), Electrostatic phase shift pscope (``Microscope`` object): Contains all microscope parameters as well as methods for propagating electron wave functions. isl_shape (2D/3D array): Array of size (z,y,x) or (y,x). If 2D the thickness will be taken as the isl_shape values multiplied by isl_thickness. If 3D, the isl_shape array will be summed along the z-axis becoming and multiplied by isl_thickness. (Default) None -> uniform flat material with thickness = isl_thk. del_px (Float): Scale factor (nm/pixel). Default = 1. def_val (Float): The defocus values at which to calculate the images. add_random (Float): Whether or not to add amorphous background to the simulation. True or 1 will add a default background, any other value will be multiplied to scale the additional phase term. save_path: String. Will save a stack [underfocus, infocus, overfocus] as well as (mphi+ephi) as tiffs along with a params.text file. (Default) None -> Does not save. save_name (str): Name prepended to saved files. v (int): Verbosity. Set v=0 to suppress print statements. isl_thk (float): Island thickness (nm). Default 20 (nm). isl_xip0 (float): Island extinction distance (nm). Default 50 (nm). mem_thk (float): Support membrane thickness (nm). Default 50 (nm). mem_xip0 (float): Support membrane extinction distance (nm). Default 1000 (nm). Filter (Bool): Apply a light gaussian filter to ephi and isl_shape. Returns: tuple: (Tphi, im_un, im_in, im_ov) - Tphi (`2D array`) -- Numpy array of size (M,N). Total electron phase shift (ephi+mphi). - im_un (`2D array`) -- Numpy array of size (M,N). Simulated image at delta z = -def_val. - im_in (`2D array`) -- Numpy array of size (M,N). Simulated image at zero defocus. - im_ov (`2D array`) -- Numpy array of size (M,N). Simulated image at delta z = +def_val. """ vprint = print if v>=1 else lambda *a, **k: None if Filter: ephi = gaussian_filter(ephi, sigma=1) Tphi = mphi + ephi vprint(f'Total fov is ({np.shape(Tphi)[1]*del_px:.3g},{np.shape(Tphi)[0]*del_px:.3g}) nm') dy, dx = Tphi.shape if add_random: if type(add_random) == bool: add_random = 1.0 ran_phi = np.random.uniform(low = -np.pi/128*add_random, high = np.pi/128*add_random, size=[dy,dx]) if np.max(ephi) > 1: # scale by ephi only if it's given and relevant ran_phi *= np.max(ephi) Tphi += ran_phi #amplitude if isl_shape is None: thk_map = np.ones(Tphi.shape)*isl_thk else: if type(isl_shape) != np.ndarray: isl_shape = np.array(isl_shape) if isl_shape.ndim == 3: thk_map = np.sum(isl_shape, axis=0)*isl_thk elif isl_shape.ndim == 2: thk_map = isl_shape*isl_thk else: vprint(textwrap.dedent(f""" Mask given must be 2D (y,x) or 3D (z,y,x) array. It was given as a {isl_shape.ndim} dimension array.""")) sys.exit(1) if Filter: thk_map = gaussian_filter(thk_map, sigma=1) Amp = np.exp((-np.ones([dy,dx]) * mem_thk / mem_xip0) - (thk_map / isl_xip0)) ObjWave = Amp * (np.cos(Tphi) + 1j * np.sin(Tphi)) # compute unflipped images qq = dist(dy, dx, shift=True) pscope.defocus = 0.0 im_in = pscope.getImage(ObjWave,qq,del_px) pscope.defocus = -def_val im_un = pscope.getImage(ObjWave,qq,del_px) pscope.defocus = def_val im_ov = pscope.getImage(ObjWave,qq,del_px) if save_path is not None: vprint(f'saving to {save_path}') im_stack = np.array([im_un, im_in, im_ov]) if not os.path.exists(save_path): os.makedirs(save_path) res = 1/del_px tifffile.imsave(os.path.join(save_path, f'{save_name}_align.tiff'), im_stack.astype('float32'), imagej = True, resolution = (res, res), metadata={'unit': 'nm'}) tifffile.imsave(os.path.join(save_path, f'{save_name}_phase.tiff'), Tphi.astype('float32'), imagej = True, resolution = (res, res), metadata={'unit': 'nm'}) with io.open(os.path.join(save_path, f'{save_name}_params.txt'), 'w') as text: text.write(f"def_val (nm) \t{def_val:g}\n") text.write(f"del_px (nm/pix) \t{del_px:g}\n") text.write(f"scope En. (V) \t{pscope.E:g}\n") text.write(f"im_size (pix) \t({dy:g},{dx:g})\n") text.write(f"sample_thk (nm) \t{isl_thk:g}\n") text.write(f"sample_xip0 (nm) \t{isl_xip0:g}\n") text.write(f"mem_thk (nm) \t{mem_thk:g}\n") text.write(f"mem_xip0 (nm) \t{mem_xip0:g}\n") text.write(f"add_random \t{add_random:g}\n") return (Tphi, im_un, im_in, im_ov)
def record(model, class_indices): #valdir = os.path.join(args.data, 'val') #val_data = MSdata(image_dir=valdir, resize_height=max(input_size), resize_width=max(input_size)) #val_loader = DataLoader(dataset=val_data, batch_size=args.batch_size, shuffle=False) #for i, (input, target) in enumerate(val_loader): class_name = list(class_indices.keys()) data_name = os.path.basename(args.data) ads_path = os.path.join(os.path.dirname(args.data), 'AEs', data_name, args.arch, args.tasks, args.attack_type) ads_record = open( os.path.join( ads_path, '{}_{}_{}_{}.txt'.format(data_name, args.arch, args.tasks, args.attack_type)), 'w') file_path = os.path.join(args.data, args.tasks) files = get_fils(file_path) mean = np.array([ 1353.036, 1116.468, 1041.475, 945.344, 1198.498, 2004.878, 2376.699, 2303.738, 732.957, 12.092, 1818.820, 1116.271, 2602.579 ]) mean = mean / mean.max() std = np.array([ 65.479, 154.008, 187.997, 278.508, 228.122, 356.598, 456.035, 531.570, 98.947, 1.188, 378.993, 303.851, 503.181 ]) std = std / std.max() preprocessing = dict(mean=mean, std=std, axis=-3) fmodel = foolbox.models.PyTorchModel(model, bounds=(0, 1), num_classes=len(class_name), preprocessing=preprocessing) attack = ad_type(fmodel, args.attack_type) for batch_files in yield_mb(files, args.batch_size, shuffle=False): categorical_label_from_full_file_name(batch_files, class_indices) images, labels = data_generator(batch_files, model.input_size[1:], class_indices) labels = np.argmax(labels, axis=1) #print(images.shape, labels.shape) cfds = fmodel.forward(images) cfds = softmax(cfds, axis=1) adversarials = attack(images, labels) ads_cfds = fmodel.forward(adversarials) ads_cfds = softmax(ads_cfds, axis=1) for i in range(args.batch_size): _class = batch_files[i].split('/')[-2] file_name = os.path.basename(batch_files[i]).split('.')[-2] model_idx = cfds[i].argmax(-1) model_cla = class_name[model_idx] model_cfs = cfds[i].max() * 100 model_flg = (model_idx == labels[i]) ads_idx = ads_cfds[i].argmax(-1) ads_cla = class_name[ads_idx] ads_cfs = ads_cfds[i].max() * 100 ads_flg = bool(1 - (ads_idx == labels[i])) ads_record.write( "{_class}+{file_name}+{model_cfs:.2f}+{ads_cla}+{ads_cfs:.2f}.tif,{_class},{model_cla},{model_idx},{model_cfs:.2f},{model_flg},{ads_cla},{ads_idx},{ads_cfs:.2f},{ads_flg}\n" .format(_class=_class, file_name=file_name, model_idx=model_idx, model_cla=model_cla, model_cfs=model_cfs, model_flg=model_flg, ads_idx=ads_idx, ads_cla=ads_cla, ads_cfs=ads_cfs, ads_flg=ads_flg)) if model_flg and ads_flg: image_path = os.path.join(ads_path, _class) imsave( "{image_path}/{_class}+{file_name}+{model_cfs:.2f}+{ads_cla}+{ads_cfs:.2f}.tif" .format(image_path=image_path, _class=_class, file_name=file_name, model_cfs=model_cfs, ads_cla=ads_cla, ads_cfs=ads_cfs), img_as_ubyte(np.transpose(adversarials[i], (1, 2, 0))))
binary_global = img > global_thresh block_size = 40 # True False binary matrix represent color value of the img using adaptive thresholding binary_adaptive = threshold_adaptive(img, block_size, offset=0) # 0 1 binary matrix img_bin_global = clear_border(img_as_uint(binary_global)) # 0 1 binary matrix img_bin_adaptive = clear_border(img_as_uint(binary_adaptive)) savedImg = img_as_float(binary_adaptive) imsave('../pics/result.tif', img_as_uint(binary_adaptive)) bin_pos_mat = ocr.binary_matrix_to_position(binary_adaptive) fig, axes = plt.subplots(nrows=3, figsize=(7, 8)) ax0, ax1, ax2 = axes figure(1) subplot(311) imshow(image) subplot(312) scatter(bin_pos_mat[:,1], bin_pos_mat[:,0]) # imshow(np.flipud(clustered)) subplot(313) imshow(binary_adaptive)