def write_image(image_path, pixels, pixel_type, c=0, z=0, t=0, size_c=1, size_z=1, size_t=1, channel_names=None, move_to=None): """ Write an image file using bioformats. Bioformats uses file extensions (e.g., .job, .gif, etc) when reading and writing image files, so the Galaxy dataset naming convention of setting all file extensions as .dat is handled by setting the move_to parameter to the value of the Galaxy dataset path. """ bioformats.write_image(pathname=image_path, pixels=pixels, pixel_type=pixel_type, c=c, z=z, t=t, size_c=size_c, size_z=size_z, size_t=size_t, channel_names=channel_names) if move_to is not None and os.path.exists(move_to): shutil.move(image_path, os.path.abspath(move_to))
def main(): stack = load_tif_stack( "/home/rhein/workspace/chloroplasts/yfp+2386-tc-1-cytoD_decon_stable_Ch1_yfp.ics movie.tif", limit=None ) stack = np.array([rgb2gray(s) for s in stack]) print stack.shape, stack.dtype for i, im in enumerate(stack): lne, _ = scanal(im, 0.5 + 2 ** np.arange(2, 3), 4.0, 0.0, np.ones(2)) # lne **= .5 seg = lne > (lne.mean() + 1.0 * lne.std()) print i, lne.mean(), lne.std() skel = skeleton(seg) # plt.figure(str(i)) # plt.subplot(311), plt.imshow(im, 'gray', interpolation='nearest') # plt.subplot(312), plt.imshow(skel, 'gray', interpolation='nearest') # plt.subplot(313), plt.imshow(seg, 'gray', interpolation='nearest') seg = (seg != 0).astype(float) seg *= 255 seg = seg.astype(np.uint8) bioformats.write_image( "/home/rhein/workspace/chloroplasts/seg.tif", seg, bioformats.PT_UINT8, z=i, size_z=len(stack) ) skel = (skel != 0).astype(float) skel *= 255 skel = skel.astype(np.uint8) bioformats.write_image( "/home/rhein/workspace/chloroplasts/skel.tif", skel, bioformats.PT_UINT8, z=i, size_z=len(stack) )
def test_load_objects(): r = numpy.random.RandomState() r.seed(1101) labels = r.randint(0, 10, size=(30, 20)).astype(numpy.uint8) handle, name = tempfile.mkstemp(".png") bioformats.write_image(name, labels, bioformats.PT_UINT8) os.close(handle) png_path, png_file = os.path.split(name) sbs_dir = os.path.join(tests.modules.example_images_directory(), "ExampleSBSImages") csv_text = """%s_%s,%s_%s,%s_DNA,%s_DNA %s,%s,Channel2-01-A-01.tif,%s """ % ( cellprofiler.measurement.C_OBJECTS_FILE_NAME, OBJECTS_NAME, cellprofiler.measurement.C_OBJECTS_PATH_NAME, OBJECTS_NAME, cellprofiler.measurement.C_FILE_NAME, cellprofiler.measurement.C_PATH_NAME, png_file, png_path, sbs_dir, ) pipeline, module, csv_name = make_pipeline(csv_text) assert isinstance(pipeline, cellprofiler.pipeline.Pipeline) assert isinstance(module, cellprofiler.modules.loaddata.LoadData) module.wants_images.value = True try: image_set_list = cellprofiler.image.ImageSetList() measurements = cellprofiler.measurement.Measurements() workspace = cellprofiler.workspace.Workspace(pipeline, module, None, None, measurements, image_set_list) pipeline.prepare_run(workspace) key_names, g = pipeline.get_groupings(workspace) assert len(g) == 1 module.prepare_group(workspace, g[0][0], g[0][1]) image_set = image_set_list.get_image_set(g[0][1][0] - 1) object_set = cellprofiler.object.ObjectSet() workspace = cellprofiler.workspace.Workspace(pipeline, module, image_set, object_set, measurements, image_set_list) module.run(workspace) objects = object_set.get_objects(OBJECTS_NAME) assert numpy.all(objects.segmented == labels) assert (measurements.get_current_image_measurement( cellprofiler.measurement.FF_COUNT % OBJECTS_NAME) == 9) for feature in ( cellprofiler.measurement.M_LOCATION_CENTER_X, cellprofiler.measurement.M_LOCATION_CENTER_Y, cellprofiler.measurement.M_NUMBER_OBJECT_NUMBER, ): value = measurements.get_current_measurement(OBJECTS_NAME, feature) assert len(value) == 9 finally: bioformats.formatreader.clear_image_reader_cache() os.remove(name) os.remove(csv_name)
def writefile(filename, image): # write image data # if file exists already, we have to remove it to prevent java error of # unmatching pixel dimensions if os.path.isfile(filename): os.remove(filename) bioformats.write_image( filename, image.astype(ome.PT_FLOAT), pixel_type=ome.PT_FLOAT)
def test_11_01_load_objects(self): r = np.random.RandomState() r.seed(1101) labels = r.randint(0, 10, size=(30, 20)).astype(np.uint8) handle, name = tempfile.mkstemp(".png") write_image(name, labels, PT_UINT8) os.close(handle) png_path, png_file = os.path.split(name) sbs_dir = os.path.join(example_images_directory(), "ExampleSBSImages") csv_text = """%s_%s,%s_%s,%s_DNA,%s_DNA %s,%s,Channel2-01-A-01.tif,%s """ % (L.C_OBJECTS_FILE_NAME, OBJECTS_NAME, L.C_OBJECTS_PATH_NAME, OBJECTS_NAME, L.C_FILE_NAME, L.C_PATH_NAME, png_file, png_path, sbs_dir) pipeline, module, csv_name = self.make_pipeline(csv_text) assert isinstance(pipeline, cpp.Pipeline) assert isinstance(module, L.LoadData) module.wants_images.value = True try: image_set_list = cpi.ImageSetList() measurements = cpmeas.Measurements() workspace = cpw.Workspace( pipeline, module, None, None, measurements, image_set_list) pipeline.prepare_run(workspace) key_names, g = pipeline.get_groupings(workspace) self.assertEqual(len(g), 1) module.prepare_group(workspace, g[0][0], g[0][1]) image_set = image_set_list.get_image_set(g[0][1][0] - 1) object_set = cpo.ObjectSet() workspace = cpw.Workspace(pipeline, module, image_set, object_set, measurements, image_set_list) module.run(workspace) objects = object_set.get_objects(OBJECTS_NAME) self.assertTrue(np.all(objects.segmented == labels)) self.assertEqual(measurements.get_current_image_measurement( L.I.FF_COUNT % OBJECTS_NAME), 9) for feature in (L.I.M_LOCATION_CENTER_X, L.I.M_LOCATION_CENTER_Y, L.I.M_NUMBER_OBJECT_NUMBER): value = measurements.get_current_measurement( OBJECTS_NAME, feature) self.assertEqual(len(value), 9) finally: clear_image_reader_cache() os.remove(name) os.remove(csv_name)
def test_11_01_load_objects(self): r = np.random.RandomState() r.seed(1101) labels = r.randint(0,10, size=(30,20)).astype(np.uint8) handle, name = tempfile.mkstemp(".png") write_image(name, labels, PT_UINT8) os.close(handle) png_path, png_file = os.path.split(name) sbs_dir = os.path.join(example_images_directory(), "ExampleSBSImages") csv_text = """%s_%s,%s_%s,%s_DNA,%s_DNA %s,%s,Channel2-01-A-01.tif,%s """ % (L.C_OBJECTS_FILE_NAME, OBJECTS_NAME, L.C_OBJECTS_PATH_NAME, OBJECTS_NAME, L.C_FILE_NAME, L.C_PATH_NAME, png_file, png_path, sbs_dir) pipeline, module, csv_name = self.make_pipeline(csv_text) assert isinstance(pipeline, cpp.Pipeline) assert isinstance(module, L.LoadData) module.wants_images.value = True try: image_set_list = cpi.ImageSetList() measurements = cpmeas.Measurements() workspace = cpw.Workspace( pipeline, module, None, None, measurements, image_set_list) pipeline.prepare_run(workspace) key_names, g = pipeline.get_groupings(workspace) self.assertEqual(len(g), 1) module.prepare_group(workspace, g[0][0], g[0][1]) image_set = image_set_list.get_image_set(g[0][1][0]-1) object_set = cpo.ObjectSet() workspace = cpw.Workspace(pipeline, module, image_set, object_set, measurements, image_set_list) module.run(workspace) objects = object_set.get_objects(OBJECTS_NAME) self.assertTrue(np.all(objects.segmented == labels)) self.assertEqual(measurements.get_current_image_measurement( L.I.FF_COUNT % OBJECTS_NAME), 9) for feature in (L.I.M_LOCATION_CENTER_X, L.I.M_LOCATION_CENTER_Y, L.I.M_NUMBER_OBJECT_NUMBER): value = measurements.get_current_measurement( OBJECTS_NAME, feature) self.assertEqual(len(value), 9) finally: clear_image_reader_cache() os.remove(name) os.remove(csv_name)
def write_hyper(image_array, filename, path=None): """ Writes an image array as a multiplane TIFF file. Iterates over the dimensions of the image array and writes the TIFF image file using python bioformats write_image function. Args: image_array: An ndarray containing images with the dimension order [Y, X, C, Z, T] filename: The name of the TIFF file you want to save. path: The directory you want to save the TIFF file to. None will save to current working directory """ import bioformats as bf import os if path: fullfile = os.path.join(path, filename) else: fullfile = filename if image_array.dtype == 'uint16': pixel_type = bf.PT_UINT16 elif image_array.dtype == 'uint8': pixel_type = bf.PT_UINT8 else: pixel_type = bf.PT_UINT16 [_, _, c_size, z_size, t_size] = image_array.shape #The order the hyperstack is written is critical #This will write the hypderstack in XYCTZ order for t_idx in range(t_size): for z_idx in range(z_size): for c_idx in range(c_size): bf.write_image(fullfile, image_array[:, :, c_idx, z_idx, t_idx], pixel_type, c=c_idx, z=z_idx, t=t_idx, size_c=c_size, size_z=z_size, size_t=t_size)
def save_image(img, output_path, overwrite=False): """Extract and save a preview image for an input image. Parameters ---------- img: ndarray N x M array of grayscale pixel intentsities. output_path: str Path to which the output file is to be saved. overwrite: boolean, (default False) Specifies whether or not to overwrite an existing file if a file already exist at the output path. """ if os.path.exists(output_path): if not overwrite: raise Exception("Ouput file %s already exists and parameter" "overwrite=False" % output_path) else: os.remove(output_path) bioformats.write_image(output_path, img, bioformats.PT_UINT8)
def extract_img(file_name, ch): meta = bf.get_omexml_metadata(path=file_name) meta = meta.encode('ascii', 'ignore') mdroot = ETree.fromstring(meta) calib = 0 img_dim = 0 num_stacks = 0 for m in mdroot: for l in m: if l.tag.endswith("Pixels"): calib = float(l.attrib['PhysicalSizeY']) img_dim = int(l.attrib['SizeX']) num_stacks = int(l.attrib['SizeZ']) ir = bf.ImageReader(path=file_name) f = '' if ch == 1: f = mDir + '/c1/' else: f = mDir + '/c2/' try: os.mkdir(f) print("Extracting into for:", f) for j in range(num_stacks): fn1 = file_name.split('.') img1 = ir.read(c=ch - 1, z=j, rescale=False) bf.write_image(f + str(j) + '_' + fn1[0].split('/')[1] + '.png', img1, pixel_type='uint8') print("Progress: [%f]" % ((100.0 * j) / num_stacks), end="\r") except OSError: print("Found slices at:", f) gc.collect() return img_dim, calib, num_stacks
def write_image( image_path, pixels, pixel_type, c=0, z=0, t=0, size_c=1, size_z=1, size_t=1, channel_names=None, move_to=None ): """ Write an image file using bioformats. Bioformats uses file extensions (e.g., .job, .gif, etc) when reading and writing image files, so the Galaxy dataset naming convention of setting all file extensions as .dat is handled by setting the move_to parameter to the value of the Galaxy dataset path. """ bioformats.write_image( pathname=image_path, pixels=pixels, pixel_type=pixel_type, c=c, z=z, t=t, size_c=size_c, size_z=size_z, size_t=size_t, channel_names=channel_names ) if move_to is not None and os.path.exists( move_to ): shutil.move( image_path, os.path.abspath( move_to ) )
def main(): stack = load_tif_stack( '/home/rhein/workspace/chloroplasts/yfp+2386-tc-1-cytoD_decon_stable_Ch1_yfp.ics movie.tif', limit=None) stack = np.array([rgb2gray(s) for s in stack]) print stack.shape, stack.dtype for i, im in enumerate(stack): lne, _ = scanal(im, .5 + 2**np.arange(2, 3), 4., 0., np.ones(2)) # lne **= .5 seg = lne > (lne.mean() + 1. * lne.std()) print i, lne.mean(), lne.std() skel = skeleton(seg) # plt.figure(str(i)) # plt.subplot(311), plt.imshow(im, 'gray', interpolation='nearest') # plt.subplot(312), plt.imshow(skel, 'gray', interpolation='nearest') # plt.subplot(313), plt.imshow(seg, 'gray', interpolation='nearest') seg = (seg != 0).astype(float) seg *= 255 seg = seg.astype(np.uint8) bioformats.write_image('/home/rhein/workspace/chloroplasts/seg.tif', seg, bioformats.PT_UINT8, z=i, size_z=len(stack)) skel = (skel != 0).astype(float) skel *= 255 skel = skel.astype(np.uint8) bioformats.write_image('/home/rhein/workspace/chloroplasts/skel.tif', skel, bioformats.PT_UINT8, z=i, size_z=len(stack))
def save_image(img, output_path, overwrite=False): """ Extract and save a preview image for an input image param img: N x M array of grayscale pixel intentsities. type img: ndarray param output_path: Path to which the output file is to be saved. type output_path: string param overwrite: Specifies whether or not to overwrite an existing file if a file already exist at the output path. type overwrite: bool raises Exception: bla """ if os.path.exists(output_path): if not overwrite: raise Exception("Ouput file %s already exists and parameter" "overwrite=False" % output_path) os.remove(output_path) bioformats.write_image(output_path, img, bioformats.PT_UINT8)
def test_12_01_load_unicode(self): base_directory = tempfile.mkdtemp() directory = u"\u2211\u03B1" filename = u"\u03B2.jpg" base_path = os.path.join(base_directory, directory) os.mkdir(base_path) path = os.path.join(base_path, filename) csv_filename = u"\u03b3.csv" csv_path = os.path.join(base_path, csv_filename) unicode_value = u"\u03b4.csv" try: r = np.random.RandomState() r.seed(1101) labels = r.randint(0, 10, size=(30, 20)).astype(np.uint8) write_image(path, labels, PT_UINT8) csv_text = ( "Image_FileName_MyFile,Image_PathName_MyFile,Metadata_Unicode\n" "%s,%s,%s\n" % (filename.encode('utf8'), base_path.encode('utf8'), unicode_value.encode('utf8'))) pipeline, module, _ = self.make_pipeline(csv_text, csv_path) image_set_list = cpi.ImageSetList() m = cpmeas.Measurements() workspace = cpw.Workspace(pipeline, module, None, None, m, image_set_list) self.assertTrue(module.prepare_run(workspace)) self.assertEqual(len(m.get_image_numbers()), 1) key_names, group_list = pipeline.get_groupings(workspace) self.assertEqual(len(group_list), 1) group_keys, image_numbers = group_list[0] self.assertEqual(len(image_numbers), 1) module.prepare_group(workspace, group_keys, image_numbers) image_set = image_set_list.get_image_set(image_numbers[0] - 1) workspace = cpw.Workspace(pipeline, module, image_set, cpo.ObjectSet(), m, image_set_list) module.run(workspace) pixel_data = image_set.get_image("MyFile").pixel_data self.assertEqual(pixel_data.shape[0], 30) self.assertEqual(pixel_data.shape[1], 20) value = m.get_current_image_measurement("Metadata_Unicode") self.assertEqual(value, unicode_value) finally: if os.path.exists(path): try: os.unlink(path) except: pass if os.path.exists(csv_path): try: os.unlink(csv_path) except: pass if os.path.exists(base_path): try: os.rmdir(base_path) except: pass if os.path.exists(base_directory): try: os.rmdir(base_directory) except: pass
def test_12_01_load_unicode(self): base_directory = tempfile.mkdtemp() directory = u"\u2211\u03B1" filename = u"\u03B2.jpg" base_path = os.path.join(base_directory, directory) os.mkdir(base_path) path = os.path.join(base_path, filename) csv_filename = u"\u03b3.csv" csv_path = os.path.join(base_path, csv_filename) unicode_value = u"\u03b4.csv" try: r = np.random.RandomState() r.seed(1101) labels = r.randint(0,10, size=(30,20)).astype(np.uint8) write_image(path, labels, PT_UINT8) csv_text = ("Image_FileName_MyFile,Image_PathName_MyFile,Metadata_Unicode\n" "%s,%s,%s\n" % (filename.encode('utf8'), base_path.encode('utf8'), unicode_value.encode('utf8'))) pipeline, module, _ = self.make_pipeline(csv_text, csv_path) image_set_list = cpi.ImageSetList() m = cpmeas.Measurements() workspace = cpw.Workspace(pipeline, module, None, None, m, image_set_list) self.assertTrue(module.prepare_run(workspace)) self.assertEqual(len(m.get_image_numbers()), 1) key_names, group_list = pipeline.get_groupings(workspace) self.assertEqual(len(group_list), 1) group_keys, image_numbers = group_list[0] self.assertEqual(len(image_numbers), 1) module.prepare_group(workspace, group_keys, image_numbers) image_set = image_set_list.get_image_set(image_numbers[0]-1) workspace = cpw.Workspace(pipeline, module, image_set, cpo.ObjectSet(), m, image_set_list) module.run(workspace) pixel_data = image_set.get_image("MyFile").pixel_data self.assertEqual(pixel_data.shape[0], 30) self.assertEqual(pixel_data.shape[1], 20) value = m.get_current_image_measurement("Metadata_Unicode") self.assertEqual(value, unicode_value) finally: if os.path.exists(path): try: os.unlink(path) except: pass if os.path.exists(csv_path): try: os.unlink(csv_path) except: pass if os.path.exists(base_path): try: os.rmdir(base_path) except: pass if os.path.exists(base_directory): try: os.rmdir(base_directory) except: pass
def findFocussedCZI(df, output_dir, method='BREN', imageSizeThreshXY=None, show=False): """ Find most focussed CZI image from dataframe of 'filepaths' to CZI image stacks of 96-well plate well images. params ------ df (DataFrame): pandas DataFrame containing 'filepath' column of full paths to CZI files output_dir (str): output directory path to save results method (str): focus measure algorithm supported methods ----------------- GLVA: Graylevel variance (Krotkov, 86) BREN: Brenner's (Santos, 97) imageSizeThreshXY (list/array) [int,int]: minimum threshold X,Y image size """ file_df_list = [] df = df.sort_values(by=['filepath']).reset_index(drop=True) n = len(df['filepath']) for f, file in enumerate(df['filepath']): print("\nProcessing file %d/%d (%.1f%%)" % (f + 1, n, ((f + 1) / n) * 100)) # extract metadata from filename file = str(file) fname, dname = os.path.basename(file), os.path.basename( os.path.dirname(file)) plateID = fname.split('.')[0] conc_mM = plateID.split("mM")[0].split("_")[-1] # get the actual image reader rdr = bioformats.get_image_reader(None, path=file) #with bioformats.ImageReader(path=file, url=None, perform_init=True) as reader: # image_arrays = reader.read(file) #image_arrays = czifile.imread(file) #image_arrays.shape # get total image series count try: # for "whatever" reason the number of total image series can only be accessed this way totalseries = int(rdr.rdr.getSeriesCount()) except: totalseries = 1 # in case there is only ONE series #totalseries = image_arrays.shape[1] #zSlices = image_arrays.shape[3] # OPTIONAL: Get metadata (obtain instrument info) #omeXMLObject = getMetadata(file) #meta = readMetadata(omeXMLObject) # parse the CZI file file_info = [] too_small_log = [] # Loop over wells (series) for sc in range(totalseries): # Set reader to series rdr.rdr.setSeries(sc) # img = np.array(image_arrays[0,sc,0,0,:,:,0]) # hor_profile = img.any(axis=0) # ver_profile = img.any(axis=1) # hor_first = np.where(hor_profile != 0)[0].min() # hor_last = np.where(hor_profile != 0)[0].max() # ver_first = np.where(ver_profile != 0)[0].min() # ver_last = np.where(ver_profile != 0)[0].max() # Filter small images if imageSizeThreshXY: x, y = rdr.rdr.getSizeX(), rdr.rdr.getSizeY() # x = hor_last - hor_first # y = ver_last - ver_first if (x <= imageSizeThreshXY[0] and y <= imageSizeThreshXY[1]): too_small_log.append(sc) else: # get number of z-slices zSlices = rdr.rdr.getImageCount() # Loop over z-slices for zc in range(zSlices): img = rdr.read(c=None, z=0, t=0, series=sc, index=zc, rescale=False) # img = np.array(image_arrays[0, sc, 0, zc, ver_first:ver_last, hor_first:hor_last, 0]) # plt.imshow(img); plt.pause(3); plt.close() # measure focus of raw image (uint16) fm = fmeasure(img, method) # store image info file_info.append([file, plateID, conc_mM, sc, zc, fm]) if len(too_small_log) > 0: print("WARNING: %d image series were omitted (image size too small)"\ % len(too_small_log)) # create dataframe from list of recorded data colnames = [ 'filepath', 'plateID', 'GFP_mM', 'seriesID', 'z_slice_number', 'focus_measure' ] file_df = pd.DataFrame.from_records(file_info, columns=colnames) # store file info file_df_list.append(file_df) # get images with max focus for each well/GFP concentration focussed_images_df = file_df[file_df['focus_measure'] == \ file_df.groupby(['seriesID'])['focus_measure']\ .transform(max)] print("%d most focussed images found." % focussed_images_df.shape[0]) # save most focussed images print("Saving most focussed images..") # Add dname to outDir when analysing multiple replicate folders at a time if df.shape[0] == len([i for i in df['filepath'] if dname in str(i)]): # We are analysing a single replicate folder outDir = os.path.join(output_dir, plateID) else: # We are analysing multiple replicate folders outDir = os.path.join(output_dir, dname, plateID) if not os.path.exists(outDir): os.makedirs(outDir) for i in range(focussed_images_df.shape[0]): if (i + 1) % 8 == 0: print("%d/%d" % (i + 1, focussed_images_df.shape[0])) # Extract image metadata from filename img_info = focussed_images_df.iloc[i] sc = img_info['seriesID'] zc = img_info['z_slice_number'] # We do NOT want to rescale images if comparing between them rdr.rdr.setSeries(sc) img = rdr.read(c=None, z=0, t=0, series=sc, index=zc, rescale=False) # img = image_arrays[0,sc,0,zc,:,:,0] assert img.dtype == np.uint16 # Convert image to 8-bit (Warning: some information will be lost) #import cv2 #img = cv2.convertScaleAbs(img, alpha=(255.0/65535.0)) #assert img.dtype == np.uint8 img = crop_image_nonzero(img) if show: plt.close('all') plt.imshow(img) plt.pause(5) # save as TIFF image outPath = os.path.join(outDir, plateID + '_s%dz%d' % (sc, zc) + '.tif') # Save as TIFF (bioformats) bioformats.write_image(pathname=outPath, pixels=img, pixel_type=bioformats.PT_UINT16) # Save TIF image as '.npy' for compatibility with ilastik software # np.save(outPath.replace('.tif','.npy'), # img[None, None, None, :, :], # allow_pickle=True) # concatenate dataframe from CZI file info df = pd.concat(file_df_list, axis=0, ignore_index=True) # save focus measures to file outDir = os.path.join(output_dir, 'focus_measures.csv') df.to_csv(outDir, index=False) focussed_images_df = df[df['focus_measure'] == \ df.groupby(['plateID','GFP_mM','seriesID'])['focus_measure']\ .transform(max)].reset_index(drop=True) return focussed_images_df
parser.add_argument('input', type=str) parser.add_argument('psf', type=str) parser.add_argument('output_prefix', type=str) parser.add_argument('channel', type=int, default=0) args = parser.parse_args() print(args) inputpath = args.input psfpath = args.psf outputprefix = args.output_prefix channel = args.channel NX, NY, NZ, NC, NT = get_movie_shape(inputpath) psf = read_psf(psfpath) for t, frame in enumerate(movie_input_generator(inputpath, channel=channel)): print('Processing frame {0}'.format(t)) algo = fd_restoration.RichardsonLucyDeconvolver(3).initialize() res = algo.run(fd_data.Acquisition(data=frame, kernel=psf), niter=30).data bf.write_image(outputprefix.format(t), res, z=0, t=0, c=0, size_z=NZ, size_t=1, size_c=1, pixel_type=bf.omexml.PT_FLOAT) javabridge.kill_vm()
def main(): javabridge.start_vm(class_path=bioformats.JARS) try: src, dest, radius, percentile, stackfile = parse_arguments() if h5py.is_hdf5(src): h5file = h5py.File(src, mode="r") stack = h5file["stack"] else : with open(src, "rb") as fd: rdr = csv.reader(fd) header = rdr.next() imageset_rows = list(rdr) columns = dict([(col.lower(), i) for i, col in enumerate(header)]) image_sets = [] if "url" in columns.keys(): for row in imageset_rows: url = row[columns["url"]] series = row[columns["series"]] if "series" in columns else 0 frame = row[columns["frame"]] if "frame" in columns else 0 image_sets.append((url, series, frame)) images = make_url_image_reader(image_sets) else: for row in imageset_rows: path = os.path.join(row[columns["pathname"]], row[columns["filename"]]) series = row[columns["series"]] if "series" in columns else 0 frame = row[columns["frame"]] if "frame" in columns else 0 image_sets.append((path, series, frame)) images = make_file_image_reader(image_sets) if stackfile is None: h5fd, h5fn = tempfile.mkstemp(".h5") os.close(h5fd) else: h5fn = stackfile h5file = h5py.File(h5fn, "w") stack = make_stack(images, h5file) if stack.dtype.itemsize == 2: # # If we have a 16-bit image, we first calculate the median # of the top 8 bits, then subtract the median that we find # from the image and do another 8 bits. # For round 2, it doesn't matter that values are 7 bits above # or below the median - they only contribute to the ranking. # # Since the division rounds down, the median at one level will # always be below the actual median # pixel_type = bioformats.PT_UINT16 def xform1(a, index): i, j = np.mgrid[0:a.shape[0], 0:a.shape[1]] i = i.flatten() j = j.flatten() b = (a[i, j] / (2 ** 8)).astype(np.uint32) return coo_matrix( (np.ones(i.shape[0], np.int32), (i, b)), shape = (a.shape[0], 256), dtype = np.uint32).toarray() result = median_filter3d(stack, None, radius, percentile, xform1) result = result.astype(np.uint32) * (2 ** 8) def xform2(a, index): i, j = np.mgrid[0:a.shape[0], 0:a.shape[1]] i = i.flatten() j = j.flatten() l = result[index ,: ] b = (a[i, j]-l[i]).astype(np.int32) b[b < 0] = 0 b[b >= (2**8)] = (2**8)-1 return coo_matrix( (np.ones(i.shape[0], np.int32), (i, b.astype(np.uint32))), shape = (a.shape[0], 256), dtype = np.uint32).toarray() level2 = median_filter3d(stack, None, radius, percentile, xform2) result = level2.astype(np.uint32) + result else: pixel_type = bioformats.PT_UINT8 def xform(a, index): i, j = np.mgrid[0:a.shape[0], 0:a.shape[1]] i = i.flatten() j = j.flatten() return coo_matrix( (np.ones(i.shape[0], np.int32), (i, a[i, j])), shape = (a.shape[0], 256), dtype = np.uint32).toarray() result = median_filter3d(stack, None, radius, percentile, xform) bioformats.write_image(dest, result, pixel_type) h5file.close() if not h5py.is_hdf5(src) and stackfile is None: os.remove(h5fn) finally: javabridge.kill_vm()
imageshape=getImageShape(path) psfshapes=[getImageShape(p) for p in psfpaths] psfs = [ np.zeros(shape,np.float32) for shape in psfshapes] for c in range(len(psfs)): with bf.ImageReader(path=psfpaths[c]) as psfreader: for z in range(psfshapes[c][4]): psfs[c][:,:,0,0,z]=psfreader.read(c=0,t=z,z=0,rescale=False) psfs = [psf.reshape((psf.shape[0],psf.shape[1],psf.shape[4])) for psf in psfs] psfs = [psf[24:-24,24:-24,:] for psf in psfs] deconvobj=None with tf.Session() as session: with bf.ImageReader(path=path) as reader: for c in range(imageshape[3]): for t in range(imageshape[4]): image=np.zeros((imageshape[0],imageshape[1],imageshape[2])) for z in range(imageshape[2]): image[:,:,z]=reader.read(c=c,t=t,z=z,rescale=False) image=image/np.max(image) if deconvobj==None: deconvobj=HSPIRAL(image.shape,psfs[c].shape) result = deconvobj.run(image,psfs[c],session) bf.write_image(pathname='MyrSCARlifeRRokG_8-c%d-t%03d.ome.tif'%(c,t),pixels=result,pixel_type="float",c=0,t=0,size_c=1,size_z=result.shape[2],size_t=1)