def test_scalar_casting(): tt = Frame(np.ones((5, 3)), frame_no=42) sum1 = tt.sum() assert_true(np.isscalar(sum1)) sum2 = tt.sum(keepdims=True) assert_equal(sum2.ndim, 2) assert_equal(sum2.frame_no, tt.frame_no)
def test_copy(): md_dict = {'a': 1} frame_no = 42 tt_base = Frame(np.ones((5, 3)), frame_no=frame_no, metadata=md_dict) tt = Frame(tt_base) assert_equal(tt.metadata, md_dict) assert_equal(tt.frame_no, frame_no)
def test_scalar_casting(): tt = Frame(np.ones((5, 3)), frame_no=42) sum1 = tt.sum() assert np.isscalar(sum1) sum2 = tt.sum(keepdims=True) assert sum2.ndim == 2 assert sum2.frame_no == tt.frame_no
def get_frame(self, j): self.data_buffer.seek(self._stride*j) s = self.data_buffer.read(self._stride) w, h = self._size result = np.fromstring(s, dtype='uint8').reshape((h, w, self.depth)) return Frame(self.process_func(result), frame_no=j)
def test_copy_update_md(): frame_no = 42 md_dict = {'a': 1} md_dict2 = {'b': 1} md_dict3 = {'a': 2, 'c': 3} tt_base = Frame(np.ones((5, 3)), frame_no=frame_no, metadata=md_dict) tt = Frame(tt_base, frame_no=frame_no, metadata=md_dict2) target_dict = dict(md_dict) target_dict.update(md_dict2) print(target_dict) print(tt.metadata) assert_equal(tt.metadata, target_dict) tt2 = Frame(tt_base, frame_no=frame_no, metadata=md_dict3) assert_equal(tt2.metadata, md_dict3)
def get_frame(self, j): md = dict() md['exposure'] = self.all_exposures[j] ts, sec_frac = self.frame_time_stamps[j] md['frame_time'] = {'datetime': ts, 'second_fraction': sec_frac} return Frame(self._get_frame(j), frame_no=j, metadata=md)
def get_frame(self, j): stream_label = ['Image', 'Item({0})'.format(j), 'Contents'] data = self._ole.openstream(stream_label).read() img = Image.fromstring('I;16L', self._im_sz, data) # Mysteriously, the image comes in rolled by 162 pixels! Roll it back. arr = np.roll(np.asarray(img, dtype=self._dtype), -162) return Frame(self.process_func(arr), frame_no=j)
def get_frame_2D(self, **coords): if self._handle is None: raise IOError('File is closed, unable to read data') _coords = {'t': 0, 'c': 0, 'z': 0, 'o': 0, 'm': 0} _coords.update(coords) i = h.Lim_GetSeqIndexFromCoords( self._lim_experiment, h.LIMUINT_4(int(_coords['t']), int(_coords['m']), int(_coords['z']), int(_coords['o']))) h.Lim_FileGetImageData(self._handle, i, self._buf_p, self._buf_md) im = np.ndarray(self._lim_frame_shape, self.pixel_type, self._buf_p_a).copy() if im.ndim == 3: im = im[:, :, _coords['c']] metadata = { 'x_um': self._buf_md.dXPos, 'y_um': self._buf_md.dYPos, 'z_um': self._buf_md.dZPos, 't_ms': self._buf_md.dTimeMSec, 'colors': self.colors, 'mpp': self.calibration, 'max_value': self.max_value } if hasattr(self, 'calibrationZ'): metadata['mppZ'] = self.calibrationZ metadata.update(coords) return Frame(im, metadata=metadata)
def get_frame_2D(self, **coords): """Actual reader, returns image as 2D numpy array and metadata as dict. """ _coords = {'t': 0, 'c': 0, 'z': 0} _coords.update(coords) if self.isRGB: _coords['c'] = 0 j = self.rdr.getIndex(int(_coords['z']), int(_coords['c']), int(_coords['t'])) if self.read_mode == 'jpype': im = np.frombuffer(self.rdr.openBytes(j)[:], dtype=self._pixel_type) elif self.read_mode == 'stringbuffer': im = self._jbytearr_stringbuffer(self.rdr.openBytes(j)) elif self.read_mode == 'javacasting': im = self._jbytearr_javacasting(self.rdr.openBytes(j)) im.shape = self._frame_shape_2D im = im.astype(self._pixel_type, copy=False) metadata = {'frame': j, 'series': self._series} if self.colors is not None: metadata['colors'] = self.colors if self.calibration is not None: metadata['mpp'] = self.calibration if self.calibrationZ is not None: metadata['mppZ'] = self.calibrationZ metadata.update(coords) for key, method in self.frame_metadata.items(): metadata[key] = getattr(self.metadata, method)(self._series, j) return Frame(im, metadata=metadata)
def get_frame(self, j): if j > self._count: raise ValueError("File does not contain this many frames") res = imread(self._filepaths[j], **self.kwargs) if res.dtype != self._dtype: res = res.astype(self._dtype) res = Frame(self.process_func(res), frame_no=j) return res
def get_frame(self, j): if j > self._count: raise ValueError("File does not contain this many frames") self._tiff.SetDirectory(j) res = self._tiff.read_image() if self._byte_swap: res = res.newbyteorder() return Frame(res, frame_no=j, metadata=self._read_metadata())
def get_frame(self, j): if j > self._count: raise ValueError("File does not contain this many frames") self._tiff.SetDirectory(j) res = self._tiff.read_image().byteswap(self._byte_swap) if res.dtype != self._dtype: res = res.astype(self._dtype) return Frame(self.process_func(res), frame_no=j)
def get_frame(self, i): cached_frame = self._cache[i % len(self._cache)] if cached_frame is None: cached_i = -1 else: cached_i = cached_frame.frame_no # return directly if the frame is in cache if cached_i == i: return cached_frame.to_frame() # check if we will have to seek to the frame if self._last_frame >= i or \ self._last_frame < i - self._fast_forward_thresh: frame = self.seek(i) # return directly if the seek was perfect (happens rarely) if frame is not None: if frame.frame_no == i: return frame.to_frame() # proceed through the frames result = None for frame in self._frame_generator: # first cache the frame self._cache[frame.frame_no % len(self._cache)] = frame self._last_frame = frame.frame_no if frame.frame_no < i: continue # go on towards the frame elif frame.frame_no == i: result = frame break else: # the frame was not inside the reader break else: # always restart the frame generator when it ends self._reset_demuxer() if result is None: # the requested frame actually does not exist. Can occur due to # a bad file, or due to inaccuracy of reader length __len__. # find it in the cache for other_i in range(i - 1, i - len(self._cache), -1): result = self._cache[other_i % len(self._cache)] if result is None: continue if result.frame_no < i: break else: # cache is empty: return an empty frame return Frame(np.zeros(self.frame_shape, dtype=self.pixel_type), frame_no=i) return result.to_frame()
def get_frame(self, j): '''Extracts the jth frame from the image sequence. if the frame does not exist return None''' try: self.im.seek(j) except EOFError: return None self.cur = self.im.tell() res = np.reshape(self.im.getdata(), self._im_sz).astype(self._dtype).T[::-1] return Frame(self.process_func(res), frame_no=j)
def get_frame(self, i): self._verify_frame_no(i) with FileLocker(self._file_lock): self._file.seek(self._image_offset + self._image_block_size * i) imdata = np.fromfile(self._file, self.pixel_type, self._pixel_count ).reshape(self._shape) # Timestamp immediately follows tfloat, ts = self._read_timestamp() md = {'time': ts, 'time_float': tfloat, 'gamut': self.metadata['gamut']} return Frame(imdata, frame_no=i, metadata=md)
def get_frame(self, j): # Find the packet this frame is in. packet_no = self._toc_cumsum.searchsorted(j, side='right') self._seek_packet(packet_no) # Find the location of the frame within the packet. if packet_no == 0: loc = j else: loc = j - self._toc_cumsum[packet_no - 1] frame = self._current_packet[loc] # av.VideoFrame return Frame(frame.to_ndarray(format='rgb24'), frame_no=j)
def get_frame(self, j): # Find the packet this frame is in. packet_no = self._toc.searchsorted(j, side='right') self._seek_packet(packet_no) # Find the location of the frame within the packet. if packet_no == 0: loc = j else: loc = j - self._toc[packet_no - 1] frame = self._current_packet[loc] # av.VideoFrame if frame.index != j: raise AssertionError("Seeking failed to obtain the correct frame.") result = np.asarray(frame.to_rgb().to_image()) return Frame(self.process_func(result).astype(self._dtype), frame_no=j)
def get_frame(self, j): '''Extracts the jth frame from the image sequence. if the frame does not exist return None''' # PIL does not support random access. If we need to rewind, re-open # the file. if j < self.cur: self.im = Image.open(self._filename) self.im.seek(j) elif j > self.cur: self.im.seek(j) elif j > len(self): raise IndexError("out of bounds; length is {0}".format(len(self))) # If j == self.cur, do nothing. self.cur = self.im.tell() res = np.reshape(self.im.getdata(), self._im_sz) return Frame(res, frame_no=j, metadata=self._read_metadata())
def __init__(self, filename, **kwargs): if imread is None: raise ImportError("One of the following packages are required for " "using the ImageReaderND: " "matplotlib or scikit-image.") super(ImageReaderND, self).__init__() self._data = Frame(imread(filename, **kwargs), frame_no=0) shape = self._data.shape if len(shape) == 2: # greyscale self._init_axis('y', shape[0]) self._init_axis('x', shape[1]) self._register_get_frame(self.get_frame_2D, 'yx') self.bundle_axes = 'yx' elif (len(shape) == 3) and (shape[2] in (3, 4)): # grayscale self._init_axis('y', shape[0]) self._init_axis('x', shape[1]) self._init_axis('c', shape[2]) self._register_get_frame(self.get_frame_2D, 'yxc') self.bundle_axes = 'yxc' else: raise IOError( 'The image has a shape that is not grayscale nor RGB:' ' {}'.format(shape))
def get_frame(self, i): return Frame(self._data, frame_no=0)
def get_frame_2D(self, **coords): i = coords['t'] if 't' in coords else 0 frame = self.reader.get_data(i) return Frame(frame, frame_no=i, metadata=frame.meta)
def to_frame(self): if self.arr is None: self.arr = Frame(_to_nd_array(self.frame), frame_no=self.frame_no, metadata=self.metadata) return self.arr
def get_frame_2D(self, **ind): return Frame(self._data, frame_no=0)
def get_frame(self, j): t = self._tiff[j] data = t.asarray() return Frame(self.process_func(data).astype(self._dtype), frame_no=j, metadata=self._read_metadata(t))
def to_frame(self): if self.arr is None: self.arr = Frame(self.frame.to_ndarray(format='rgb24'), frame_no=self.frame_no, metadata=self.metadata) return self.arr
def test_creation_md(): md_dict = {'a': 1} frame_no = 42 tt = Frame(np.ones((5, 3)), frame_no=frame_no, metadata=md_dict) assert_equal(tt.metadata, md_dict) assert_equal(tt.frame_no, frame_no)
def test_repr_html_(): _skip_if_no_PIL() # This confims a bugfix, where 16-bit images would raise # an error. Frame(10000 * np.ones((50, 50), dtype=np.uint16))._repr_html_()
def __iter__(self): iterable = self.reader.iter_data() for i in range(len(self)): frame = next(iterable) yield Frame(frame, frame_no=i, metadata=frame.meta)
def test_copy_override_frame(): frame_no = 42 tt_base = Frame(np.ones((5, 3)), frame_no=frame_no) frame_no_2 = 123 tt = Frame(tt_base, frame_no=frame_no_2) assert_equal(tt.frame_no, frame_no_2)
def get_frame(self, i): frame = self.reader.get_data(i) return Frame(frame, frame_no=i, metadata=frame.meta)