def get_frame(self, i): if np.issubdtype(self._dtype, np.float): frame = np.random.random(self._shape).astype(self._dtype) else: frame = np.random.randint(0, np.iinfo(self._dtype).max, self._shape).astype(self._dtype) return Frame(frame, frame_no=i)
def plot_to_frame(fig, width=512, close_fig=False, fig_size_inches=None, bbox_inches=None): """ Renders a matplotlib figure or axes object into a numpy array containing RGBA data of the rendered image. Parameters ---------- fig : matplotlib Figure or Axes object width : integer The width of the resulting frame, in pixels close_fig : boolean When True, the figure will be closed after plotting fig_size_inches : tuple The figure (height, width) in inches. If None, the size is not changed. bbox_inches : {None, 'standard', 'tight'} When 'tight', tight layout is used. Returns ------- pims.Frame object containing RGBA values (dtype uint8) """ if mpl is None: raise ImportError("This feature requires matplotlib.") from pims import Frame if isinstance(fig, mpl.axes.Axes): fig = fig.figure if fig_size_inches is not None: if fig_size_inches[0] == 0 or fig_size_inches[1] == 0: raise ValueError('Figure size cannot be zero.') if bbox_inches is None: tight_layout = fig.get_tight_layout() elif str(bbox_inches) == 'standard': tight_layout = False elif str(bbox_inches) == 'tight': tight_layout = True else: raise ValueError("bbox_inches must be in {None, 'standard', 'tight'}") buf = BytesIO() with _fig_size_cntx(fig, fig_size_inches, tight_layout) as fig: width_in, height_in = fig.get_size_inches() dpi = width / width_in if tight_layout: # slower, but allows tight layout fig.savefig(buf, format='png', dpi=dpi) buf.seek(0) image = plt.imread(buf) else: # faster, but only possible without tight layout fig.savefig(buf, format='rgba', dpi=dpi) buf.seek(0) buf_shape = (int(height_in * dpi), int(width_in * dpi), 4) image = np.fromstring(buf.read(), dtype='uint8').reshape(*buf_shape) if close_fig: plt.close(fig) return Frame(image)
def get_frame(self, i): key_number, elem_number = self._toc[i] key = self.keys[key_number] with h5py.File(self.master_filepath, "r") as f: try: img = f['entry']['data'][key][ elem_number] # Eiger firmware v1.3.0 and onwards except KeyError: img = f['entry'][key][elem_number] # Older firmwares return Frame(img, frame_no=i)
def main(frames, metadata, roi): frame_stack_total = Frame(frames) ROI_size_1D = 4 y = int(roi[0]) x = int(roi[1]) frame_stack = frame_stack_total[:, y - ROI_size_1D:y + ROI_size_1D + 1, x - ROI_size_1D:x + ROI_size_1D + 1] return frame_stack
def noisy_image(self, noise_level): """Adds noise to the current image, uniformly distributed between 0 and `noise_level`, not including noise_level.""" if noise_level <= 0: return self.image if np.issubdtype(self.dtype, np.integer): noise = np.random.poisson(noise_level, self.shape) else: noise = np.clip( np.random.normal(noise_level, noise_level / 2, self.shape), 0, self.saturation) noisy_image = np.clip(self.image + noise, 0, self.saturation) return Frame(np.array(noisy_image, dtype=self.dtype))
def plots_to_frame(figures, width=512, close_fig=False, fig_size_inches=None, bbox_inches=None): """ Renders an iterable of matplotlib figures or axes objects into a pims Frame object, that will be displayed as scrollable stack in IPython. Parameters ---------- figures : iterable of matplotlib Figure or Axes objects width : integer The width of the resulting frame, in pixels close_fig : boolean When True, the figure will be closed after plotting fig_size_inches : tuple The figure (height, width) in inches. If None, the size is not changed. bbox_inches : {'tight', None} When 'tight', tight layout is used. Returns ------- pims.Frame object containing a stack of RGBA values (dtype uint8) """ if mpl is None: raise ImportError("This feature requires matplotlib.") from pims import Frame if isinstance(figures, mpl.axes.Axes) or \ isinstance(figures, mpl.figure.Figure): raise ValueError('Use plot_to_frame for single figures, or supply ' 'an iterable of figures to plots_to_frame.') width = int(width) h = None frames = [] for n, fig in enumerate(figures): im = plot_to_frame(fig, width, close_fig, fig_size_inches, bbox_inches) if h is None: h = im.shape[0] else: # make the image the same size as the first image if im.shape[0] != h: im = np.pad(im[:h], ((0, max(0, h - im.shape[0])), (0, 0), (0, 0)), mode=str('constant')) frames.append(im) return Frame(np.array(frames))
def get_frame(self, frame_no): """ Returns a frame (image) as a np.array. """ self.f.seek(self.video_header.VideoHeaderSize) # absolute seek self.f.seek(frame_no * self.frame_size_bytes + self.frame_header_size, 1) # relative seek image = [] unpack_format = '{:}B'.format(self._frame_shape[0]) for i in range(self._frame_shape[1]): image.append( struct.unpack(unpack_format, self.f.read(self._frame_shape[0]))) image = np.array(image, dtype=self._dtype) return Frame(image, frame_no=frame_no)
def __init__(self, shape, size, dtype=np.uint8, saturation=None, hard_radius=None, signal=None, noise=0, feat_func=feat_gauss, **feat_kwargs): self.ndim = len(shape) self.shape = shape self.dtype = dtype self.image = Frame(np.zeros(shape, dtype=dtype)) self.size = validate_tuple(size, self.ndim) self.isotropic = np.all([self.size[1:] == self.size[:-1]]) self.feat_func = feat_func self.feat_kwargs = feat_kwargs self.noise = noise if saturation is None and np.issubdtype(dtype, np.integer): self.saturation = np.iinfo(dtype).max elif saturation is None and np.issubdtype(dtype, np.float): self.saturation = 1 else: self.saturation = saturation if signal is None: self.signal = self.saturation else: self.signal = signal self.center = tuple([s // 2 for s in shape]) self.hard_radius = hard_radius self._coords = [] self.pos_columns = ['z', 'y', 'x'][-self.ndim:] if self.isotropic: self.size_columns = ['size'] else: self.size_columns = ['size_z', 'size_y', 'size_x'][-self.ndim:]
def get_frame(self, i): img = self._data_array[i] return Frame(img, frame_no=i)
def get_frame(self, i): return Frame(self._data[i], frame_no=i)
def get_frame(self, i): dataset = self._entry['data_{:06d}'.format(1 + (i // self.images_per_file))] img = dataset[i % self.images_per_file] return Frame(img, frame_no=i)
def get_frame(self, ind): self.im.clear() pos = self._f.loc[self._f['frame'] == ind, self.pos_columns].values for _pos in pos: self.im.draw_feature(_pos) return Frame(self.im(), frame_no=ind)
def get_frame(self, i): # had to return Frame to be friendly with pims_pipeline operations... img = self._data[i].compute() return Frame(img, frame_no=i)
def get_frame(self,i): '''The get_frame object.''' return Frame(self.imgs[i%self.dims[0],:,:],frame_no=i)
def get_frame(self, i): return Frame(self._dataset[self._start + i], frame_no=i)
def get_frame_2D(self, **ind): result_order = ['c', 'm', 't', 'x', 'y', 'z'] result = [ind[a] for a in result_order] metadata = {i: ind[i] for i in result_order} return Frame([result], metadata=metadata).astype(np.uint8)
#%% Main if __name__ == '__main__': #pr.enable() for name in filenames: with ND2_Reader(name) as ND2: ## parse ND2 info frames = ND2 metadata = ND2.metadata #frames = frames[0:2] frame_stack_total = Frame(frames) roi_finder = roi_finding.roi_finder( ROI_SIZE, frames[0]) #, intensity_min = 800) fitter = fitting.scipy_last_fit_guess(metadata, ROI_SIZE, WAVELENGTH, roi_finder.intensity_min, "ScipyLastFitGuess", 5) roi_locations = roi_finder.main(frames[0], fitter) #roi_locations = roi_locations[0:4, :] start = time.time() processes = [] q = mp.Queue() for i in range(0, n_processes):