def test_get_tiff_param(self): with TiffFile(test_shg_image_path) as tiff_file: minor_axis, n_modes, xy_dim = get_tiff_param(tiff_file) self.assertEqual(2, minor_axis) self.assertEqual(1, n_modes) self.assertEqual((200, 200), xy_dim) with TiffFile(test_shg_pl_trans_image_path) as tiff_file: minor_axis, n_modes, xy_dim = get_tiff_param(tiff_file) self.assertEqual(1, minor_axis) self.assertEqual(3, n_modes) self.assertEqual((200, 200), xy_dim)
def _find_images(self): index = [] for fname in self._files: if fname.lower().endswith(('.tiff', '.tif')): img = TiffFile(fname) index += [(fname, i) for i in range(len(img.pages))] else: im = Image.open(fname) try: # this will raise an IOError if the file is not readable im.getdata()[0] except IOError: site = "http://pillow.readthedocs.org/en/latest/installation.html#external-libraries" raise ValueError('Could not load "%s"\nPlease see documentation at: %s' % (fname, site)) else: i = 0 while True: i += 1 index.append((fname, i)) try: im.seek(i) except EOFError: break if hasattr(im, 'fp') and im.fp: im.fp.close() self._frame_index = index return len(index)
def load_data(pattern): files = np.sort(glob.glob(pattern)) Lim = [] for i, f in enumerate(files): Lim.append(TiffFile(f).asarray()) im = np.stack(Lim) return (im)
def info(filename): """Gets the SIMS pages' metadata from a MibiTiff file. Args: filename: The path to the TIFF. Returns: A dictionary of metadata as could be supplied as kwargs to ``mibitof.mibi_image.MibiImage``, except with a ``channels`` key whose value is a list of (mass, target) tuples. """ metadata = {} channels = [] with TiffFile(filename) as tif: _check_software(tif) for page in tif.pages: description = _page_description(page) image_type = description['image.type'].lower() if image_type == 'sims': channels.append((description['channel.mass'], description['channel.target'])) # Get metadata on first SIMS page only if not metadata: metadata.update(_page_metadata(page, description)) metadata['conjugates'] = channels return metadata
def test_default_ranges(self): tiff.write(self.filename, self.image) with TiffFile(self.filename) as tif: for i, page in enumerate(tif.pages): self.assertEqual(page.tags['smin_sample_value'].value, 0) self.assertEqual(page.tags['smax_sample_value'].value, self.image.data[:, :, i].max())
def _find_images(self): index = [] for fname in self._files: if fname.lower().endswith(('.tiff', '.tif')): with open(fname, 'rb') as f: img = TiffFile(f) index += [(fname, i) for i in range(len(img.pages))] else: try: im = Image.open(fname) im.seek(0) except (IOError, OSError): continue i = 0 while True: try: im.seek(i) except EOFError: break index.append((fname, i)) i += 1 if hasattr(im, 'fp') and im.fp: im.fp.close() self._frame_index = index return len(index)
def motion_correction_piecewise(fname, splits, strides, overlaps, add_to_movie=0, template = None, max_shifts = (12,12),max_deviation_rigid = 3,newoverlaps = None, newstrides = None,\ upsample_factor_grid = 4, order = 'F',dview = None,save_movie= True, base_name = 'none'): ''' ''' with TiffFile(fname) as tf: d1, d2 = tf[0].shape T = len(tf) if type(splits) is int: idxs = np.array_split(list(range(T)), splits) else: idxs = splits save_movie = False if template is None: raise Exception('Not implemented') shape_mov = (d1 * d2, T) dims = d1, d2 if save_movie: if base_name is None: base_name = fname[:-4] fname_tot = base_name + '_d1_' + str(dims[0]) + '_d2_' + str( dims[1]) + '_d3_' + str(1 if len(dims) == 2 else dims[2]) + '_order_' + str( order) + '_frames_' + str(T) + '_.mmap' fname_tot = os.path.join(os.path.split(fname)[0], fname_tot) np.memmap(fname_tot, mode='w+', dtype=np.float32, shape=shape_mov, order=order) else: fname_tot = None pars = [] for idx in idxs: pars.append([ fname, fname_tot, idx, shape_mov, template, strides, overlaps, max_shifts, np.array(add_to_movie, dtype=np.float32), max_deviation_rigid, upsample_factor_grid, newoverlaps, newstrides ]) t1 = time.time() if dview is not None: res = dview.map_sync(tile_and_correct_wrapper, pars) else: res = list(map(tile_and_correct_wrapper, pars)) print((time.time() - t1)) return fname_tot, res
def load_image(path): ''' Read OME-Tiff file. Supported OME dimension orders are :'XYZCT','XYZTC','XYCZT','XYTCZ','XYCTZ' and 'XYTZC' (in order of incresing speed). For files with channels where SamplesPerPixel>1 the C dimension will also contain the different Samples separately. For example a 4 channel rgb image (SamplesPerPixel=3) will contain 4*3 channels. Returns ndarray with normalized shape where axes of unit length are also marked. Note that order of axis will be from the slowest to the fastest changing as returned by TiffFile. ''' #Ignore some tiffile warnings that always occur ??Bug?? warnings.filterwarnings("ignore", message="ome-xml: index out of range") warnings.filterwarnings("ignore", message="ome-xml: not well-formed") with TiffFile(path, is_ome=True) as tif: #Check if image is OME-Tiff #if not tif.is_ome: #raise TypeError('The file is corrupt or not an OME-tiff file!') #Load image into nd array images = tif.asarray() #Load metadata ome_metadata = utils.xml2dict(tif[0].tags['image_description'].value, sanitize=True, prefix=None) return images, ome_metadata
def read_multiimg_stack(tiffile, return_img=True): """ Use the tifffile.py library through Scikit-Image to read multipage bioformat files such as .tif/.lsm files. Parameters ---------- tiffile : str input .tif/.lsm file to read, can be multipage .tif (string) return_img : bool boolean True/False to specify if the image should be read as a numpy array or just the object be returned. Returns ------- img_object : Python object A read image object containing the attributes: pages, series and micromanager_metadata. imgs : numpy array (only if return_img=True) an (n_frames x n_slices x n_channels x n_rows x n_cols) image. """ from skimage.external.tifffile import TiffFile im_object = TiffFile(Tiffile) if return_img: imgs = im_object.asarray() return im_object, imgs else: return im_object
def open_tiff(file, sktiff): if sktiff: tif = TiffFile(file, fastij = False) Ltif = len(tif) else: tif = ScanImageTiffReader(file) Ltif = tif.shape()[0] return tif, Ltif
def get_image(file_name): """ Read image from file and load into numpy array """ ext = os.path.splitext(file_name.lower())[-1] if ext == '.tif' or ext == '.tiff': return np.float32(TiffFile(file_name).asarray()) return np.float32(imread(file_name))
def test_custom_ranges(self): ranges = list(zip([1]*5, range(2, 7))) tiff.write(self.filename, self.image, ranges=ranges) with TiffFile(self.filename) as tif: for i, page in enumerate(tif.pages): self.assertEqual(page.tags['smin_sample_value'].value, ranges[i][0]) self.assertEqual(page.tags['smax_sample_value'].value, ranges[i][1])
def get_image(file_name): ext = os.path.splitext(file_name.lower())[-1] if ext in {'.tif', '.tiff'}: img = np.asarray(np.float32(TiffFile(file_name).asarray())) img = np.tile(np.expand_dims(img, axis=-1), (1, 1, 3)) return img / np.max(img) img = np.asarray(np.float32(imread(file_name))) img = np.tile(np.expand_dims(img, axis=-1), (1, 1, 3)) return ((img / np.max(img)) * 255).astype(int)
def read(filename, sims=True, sed=False, optical=False, label=False): """Reads MIBI data from an IonpathMIBI TIFF file. Args: filename: The path to the TIFF. sims: Boolean for whether to return the SIMS (MIBI) data. Defaults to True. sed: Boolean for whether to return the SED data. Defaults to False. optical: Boolean for whether to return the optical image. Defaults to False. label: Boolean for whether to return the slide label image. Defauls to False. Returns: A tuple of the image types set to True in the parameters, in the order SIMS, SED, Optical, Label (but including only those types specified). The SIMS data will be returned as a ``mibitof.mibi_image.MibiImage`` instance; the other image types will be returned as numpy arrays. If an image type is selected to be returned but is not present in the image, it will be returned as None. Raises: ValueError: Raised if the input file is not of the IonpathMIBI format, or if no image type selected to be returned. """ return_types = collections.OrderedDict([('sims', sims), ('sed', sed), ('optical', optical), ('label', label)]) if not any((val for val in return_types.values())): raise ValueError( 'At least one image type must be specified to return.') to_return = {} metadata = {} sims_data = [] channels = [] with TiffFile(filename) as tif: _check_software(tif) for page in tif.pages: description = _page_description(page) image_type = description['image.type'].lower() if sims and image_type == 'sims': channels.append((description['channel.mass'], description['channel.target'])) sims_data.append(page.asarray()) # Get metadata on first SIMS page only if not metadata: metadata.update(_page_metadata(page, description)) elif return_types.get(image_type): to_return[image_type] = page.asarray() if sims: to_return['sims'] = mi.MibiImage(np.stack(sims_data, axis=2), channels, **metadata) return_vals = tuple( to_return.get(key) for key, val in return_types.items() if val) if len(return_vals) == 1: return return_vals[0] return return_vals
def tiff_viewer(): # Use a GUI to get the patient data .csv # TODO: Add progress bar for loading tiff_file = sg.PopupGetFile('Choose video file (.tif/.tiff) to open:') if tiff_file is None: quit() sg.Popup('Results', 'The value returned from PopupGetFile', tiff_file) # Using skimage aka scikit-image from SciPy video_file = io.imread(tiff_file) with TiffFile(tiff_file) as tif: # image_stack = tif.asarray() for page in tif.pages: for tag in page.tags.values(): tag_name, tag_value = tag.name, tag.value image = page.asarray() video_shape = video_file.shape num_frames = video_file.shape[0] print('video shape: ', video_shape) print('Width x Height: ', video_file.shape[1], video_file.shape[2]) print('# of Frames: ', num_frames) # show the image plt.figure(1) plt.imshow(video_file[0], cmap='gray') plt.axis('off') # plt.show() # define the window layout layout = [[sg.Text('TIFF Video Viewer', size=(15, 1), pad=((510, 0), 3), justification='center', font='Helvetica 20')], [sg.Canvas(size=(600, 600), key='canvas')], # [sg.Image(filename='', key='image')], [sg.Slider(range=(1, num_frames), size=(115, 10), orientation='h', key='frame_slider')], [sg.Button('Exit', size=(10, 2), pad=((600, 0), 3), font='Helvetica 14')]] # create the window window = sg.Window('Tiff Viewer', no_titlebar=False).Layout(layout) window.Layout(layout).Finalize() # Start main GUI window while True: event, values = window.Read(timeout=5) # Poll every 100 ms if event is 'Exit' or event is None: exit(69) frame_current = int(values['frame_slider']) # print('Current frame #', frame_current) image_current = video_file[frame_current-1] canvas = window.FindElement('canvas').TKCanvas fig = plt.gcf() plt.imshow(image_current, cmap='gray') fig_photo = draw_figure(canvas, fig)
def get_image(file_name): """Read image from file and returns it as a tensor Args: file_name (str): path to image file Returns: numpy.array: numpy array of image data """ ext = os.path.splitext(file_name.lower())[-1] if ext == '.tif' or ext == '.tiff': return np.float32(TiffFile(file_name).asarray()) return np.float32(imread(file_name))
def get_image_source(path): ''' Return the image type. Currently only imageJ and ome Tiff files are supported. ''' with TiffFile(path) as tif: if tif.is_imagej: output = 'imagej' if tif.is_imagej: output = 'ome' return output
def open_tiff(file, sktiff): ''' opens tiff with either ScanImageTiffReader or skimage returns tiff and its length ''' if sktiff: tif = TiffFile(file, fastij=False) Ltif = len(tif) else: tif = ScanImageTiffReader(file) tsize = tif.shape() if len(tsize) < 3: # single page tiffs Ltif = 1 else: Ltif = tif.shape()[0] return tif, Ltif
def file_reader(filename, record_by='image', **kwds): """Read data from tif files using Christoph Gohlke's tifffile library Parameters ---------- filename: str record_by: {'image'} Has no effect because this format only supports recording by image. """ with TiffFile(filename, **kwds) as tiff: dc = tiff.asarray() axes = tiff.series[0]['axes'] if tiff.is_rgb: dc = rgb_tools.regular_array2rgbx(dc) axes = axes[:-1] op = {} names = [axes_label_codes[axis] for axis in axes] axes = [ { 'size': size, 'name': unicode(name), #'scale': scales[i], #'offset' : origins[i], #'units' : unicode(units[i]), } for size, name in zip(dc.shape, names) ] op = {} for key, tag in tiff[0].tags.iteritems(): op[key] = tag.value return [{ 'data': dc, 'original_metadata': op, 'metadata': { 'General': { 'original_filename': os.path.split(filename)[1] }, "Signal": { 'signal_type': "", 'record_by': "image", }, }, }]
def load_image(self, filename): with TiffFile(filename) as tiff_file: image = tiff_file.asarray() minor_axis, n_modes, xy_dim = get_tiff_param(tiff_file) logger.debug(f"Number of image modes = {n_modes}") logger.debug(f"Size of image = {xy_dim}") if minor_axis is not None: n_stacks = image.shape[minor_axis] logger.debug(f"Number of stacks = {n_stacks}") acc_number = get_accumulation_number(filename) logger.debug(f"Using accumulation number = {acc_number}") image = self._format_image(image, minor_axis=minor_axis, acc_number=acc_number) return image
def visual(background, img_path, interval, iterations): rgb = hill_shade(background) # create hillshade image rows = rgb.shape[0] cols = rgb.shape[1] display_image( rgb, "Inundation simulation with rainfall intensity 5 cm/h", max_plot=False ) plt.pause(3) layer = None for iter in range(1, iterations + 1): img_files = os.listdir(img_path) if iter % 2 == 0: # simulate water level decreasing img_files = reversed(img_files) layer.remove() elif iter > 1 and iter % 2 == 1: plt.imshow(rgb) plt.pause(1) text = None for i, img in enumerate(img_files): if i > 0: layer.remove() img_file = os.path.join(img_path, img) with TiffFile(img_file) as tif: img_arr = tif.asarray() img_arr = np.ma.masked_where(img_arr == 0, img_arr) layer = plt.imshow(img_arr, alpha=0.5) if iter % 2 != 0: text = plt.text( cols / 2, -5, "Water level increasing: Time = {} hours".format(i + 1), fontsize=20, ha="center", ) else: text = plt.text( cols / 2, -5, "Water level decreasing ...", fontsize=20, ha="center" ) plt.pause(interval) text.remove() return True
def visual(background, img_path, interval): plt.figure() fig = plt.gcf() fig.clf() ax1 = fig.add_subplot(1, 2, 1) rgb = hill_shade(bk_img) ax1.imshow(rgb) ax2 = fig.add_subplot(1, 2, 2) ax_u = ax2.imshow(rgb) plt.pause(1) # bnd = None left = None right = None img_files = os.listdir(img_path) img_files = reversed(img_files) for i, img in enumerate(img_files): print(img) if i > 0: # del ax1.collections[0] left.remove() right.remove() img_file = os.path.join(img_path, img) with TiffFile(img_file) as tif: img_arr = tif.asarray() # img_arr[img_arr == 0] = np.NaN dist = calculateDist(img_arr) img_arr = np.ma.masked_where(img_arr == 0, img_arr) # del ax1.collections[0] # ax_u.set_data(img_arr) # ax1.contour(dist) left = ax2.imshow(dist, cmap="binary") # ax_u.set_data(img_arr) right = ax1.imshow(img_arr, alpha=.5) # ax2.imshow(img_arr) fig.canvas.draw() # bnd.remove() plt.pause(interval) return True
def info(filename): """Gets the metadata from a MibiTiff file. Args: filename: The path to the TIFF. Returns: A dictionary of metadata as could be supplied as kwargs to :class:`mibidata.mibi_image.MibiImage`, except with a ``channels`` key whose value is a list of (mass, target) tuples. """ metadata = {} channels = [] with TiffFile(filename) as tif: _check_software(tif) for page in tif.pages: description, image_type = _page_description(page) if image_type == 'sims': _get_page_data(page, description, metadata, channels) metadata['conjugates'] = channels return metadata
def save_tiff_to_hdf(): #cluster = LocalCluster(processes=False) #client = Client(cluster) size_chunk = 500 path = "/data.nst/share/data/packer_calcium_mice/2019-11-07_J061_t-003" save_dir = os.path.join('/data.nst/jdehning/packer_data', os.path.basename(os.path.normpath(path))) save_name = os.path.basename(os.path.normpath(path)) filename = find_file('*.tif', path) if len(filename) > 1: raise RuntimeError("More than one tif found") else: filename = filename[0] with dask.config.set(scheduler='single-threaded'): with TiffFile(os.path.join(path, filename), fastij=False) as reader: os.makedirs(save_dir, exist_ok=True) length = len(reader) num_chunks = (length - 1) // size_chunk + 1 dask_array = create_dask_array(reader, num_chunks, size_chunk) dask_array = dask_array[:dask_array.shape[0] // 2, ::, ::] dask_array.to_hdf5(os.path.join(save_dir, save_name), '/x')
def openTiff(filename, isMask=False, args=None): raw = openRaw(filename, isMask=isMask, args=args) info = {} if filename.lower().find('tif') >= 0: try: with TiffFile(filename) as tiffdata: for page in tiffdata: for tag in page.tags.values(): t, v = tag.name, tag.value info[t] = v except: pass try: nonRaw = ImageWrapper(_openCV2(filename), info=info, to_mask=isMask, filename=filename) if raw is not None and raw.size[0] > nonRaw.size[0] and raw.size[1] > nonRaw.size[1]: return raw return nonRaw except: return raw return raw
def save_tiff_to_xr(): #cluster = LocalCluster(processes=False) #client = Client(cluster) size_chunk = 500 path = "/data.nst/share/data/packer_calcium_mice/2019-11-08_RL065/2019-11-08_RL065_t-001/" filename = "2019-11-08_RL065_t-001_Cycle00001_Ch3.tif" subsampling_list = [(4, 4), (5, 5)] for subs in subsampling_list: with dask.config.set(scheduler='single-threaded'): with TiffFile(path + filename, fastij=False) as reader: length = len(reader) num_chunks = (length - 1) // size_chunk + 1 dask_array = create_dask_array(reader, num_chunks, size_chunk) dask_array = dask_array[:, ::subs[0], ::subs[1]] #print(dask_array.shape) #print(dask_array) #ds = xr.Dataset({'activity':(['time', 'x','y'], dask_array)}) #ds.to_netcdf('/scratch.local/jdehning/calcium_subsampled/'+'2019-11-08_RL065_t-001_Cycle00001_Ch3_1x2.nc') #ds.to_zarr('/scratch.local/jdehning/calcium_subsampled/'+'2019-11-08_RL065_t-001_Cycle00001_Ch3_1x2.zr') dask_array.to_hdf5( '/data.nst/jdehning/packer_data/calcium_subsampled/' + '2019-11-08_RL065_t-001_Cycle00001_Ch3_{}x{}.hdf5'.format( *subs), '/x')
def visual(background, img_path, interval, iterations): rgb = hill_shade(bk_img) figManager = plt.get_current_fig_manager() figManager.window.showMaximized() plt.imshow(rgb) plt.pause(1) layer = None for iter in range(1, iterations + 1): img_files = os.listdir(img_path) if iter % 2 == 0: img_files = reversed(img_files) layer.remove() elif iter > 1 and iter % 2 == 1: # layer.remove() # plt.clf() plt.imshow(rgb) plt.pause(1) # plt.show() for i, img in enumerate(img_files): print(img) # layer.remove() if i > 0: layer.remove() print("remove {}".format(img)) # elif i == 0 and iter % 2 == 0: # plt.imshow(rgb) img_file = os.path.join(img_path, img) with TiffFile(img_file) as tif: img_arr = tif.asarray() img_arr = np.ma.masked_where(img_arr == 0, img_arr) layer = plt.imshow(img_arr, alpha=.5) plt.pause(interval) return True
def _metadata(file): with TiffFile(file) as tif: meta = tif.info() metadata = {} for line in meta.splitlines(): _, _, key_x = line.partition('x_resolution (2I)') _, _, key_y = line.partition('y_resolution (2I)') _, _, key_z = line.partition('spacing:') _, _, key_unit = line.partition('unit:') if key_x: x_data = [int(x.group()) for x in re.finditer(r'\d+', key_x)] x_resolution = 1 / (x_data[0] / x_data[1]) metadata['x_resolution'] = x_resolution if key_y: y_data = [int(x.group()) for x in re.finditer(r'\d+', key_y)] y_resolution = 1 / (y_data[0] / y_data[1]) metadata['y_resolution'] = y_resolution if key_z: metadata['z_resolution'] = float(key_z) if key_unit: metadata['unit'] = key_unit return metadata
def can_load(self, filename): """Perform check to see whether file is formatted correctly to be loaded""" try: with TiffFile(filename) as tiff_file: # Check is this is Olympus FluoView formatted if tiff_file.is_fluoview: return True # Check is this is ImageJ formatted if tiff_file.is_imagej: return True # Check if this is test data _, description = lookup_page(tiff_file.pages[0]) desc_dict = json.loads(description) for key in ['minor_axis', 'n_modes', 'xy_dim']: _ = desc_dict[key] except Exception as e: logger.info(f'File type not supported: {e}') return False return True
def read(file, sims=True, sed=False, optical=False, label=False, masses=None, targets=None): """Reads MIBI data from an IonpathMIBI TIFF file. Args: file: The string path or an open file object to a MIBItiff file. sims: Boolean for whether to return the SIMS (MIBI) data. Defaults to True. sed: Boolean for whether to return the SED data. Defaults to False. optical: Boolean for whether to return the optical image. Defaults to False. label: Boolean for whether to return the slide label image. Defaults to False. masses: A list of integer masses. If specified, only channels corresponding to these masses will be included in the returned MibiImage. Either masses or targets can be specified, not both. targets: A list of string targets. If specified, only channels corresponding to these targets will be included in the returned MibiImage. Either masses or targets can be specified, not both. Returns: A tuple of the image types set to True in the parameters, in the order SIMS, SED, Optical, Label (but including only those types specified). The SIMS data will be returned as a :class:`mibidata.mibi_image.MibiImage` instance; the other image types will be returned as numpy arrays. If an image type is selected to be returned but is not present in the image, it will be returned as None. If returning SIMS data and the masses or targets parameters are set, only those channels will be included in the MibiImage instance, otherwise all channels present in the file will be returned. Raises: ValueError: Raised if * The input file is not of the IONpath MIBItiff format * No image type is selected to be returned. * Both masses and targets are specified. """ return_types = collections.OrderedDict([('sims', sims), ('sed', sed), ('optical', optical), ('label', label)]) if not any((val for val in return_types.values())): raise ValueError( 'At least one image type must be specified to return.') if masses and targets: raise ValueError( 'Either masses or targets can be specified, not both.') to_return = {} metadata = {} sims_data = [] channels = [] with TiffFile(file) as tif: _check_software(tif) for page in tif.pages: description, image_type = _page_description(page) if sims and image_type == 'sims' and _include_page( description, masses, targets): _get_page_data(page, description, metadata, channels) sims_data.append(page.asarray()) elif return_types.get(image_type): to_return[image_type] = page.asarray() if sims: if (targets or masses) and not sims_data: raise ValueError('None of the channels specified for inclusion ' 'are present in file.') image = mi.MibiImage(np.stack(sims_data, axis=2), channels, **metadata) if masses: missing_masses = list(set(masses) - set(image.masses)) if missing_masses: warnings.warn(f'Requested masses not found in file: ' f'{missing_masses}') if targets: missing_targets = list(set(targets) - set(image.targets)) if missing_targets: warnings.warn(f'Requested targets not found in file: ' f'{missing_targets}') to_return['sims'] = image return_vals = tuple( to_return.get(key) for key, val in return_types.items() if val) if len(return_vals) == 1: return return_vals[0] return return_vals