Beispiel #1
0
    def pw_transform(self, pwfn, verbose=False, **kwargs):
        """Spawn another frame sequence, pixelwise applying a user-provided
        function.

	Parameters:
	  - `pwfn`: (`func`) -- a ``f(vector) -> vector`` function
	  - `verbose`: (`Bool`) -- whether to be verbose while going through
            the pixels
	  - `**kwargs``: keyword arguments to be passed to `self.pix_iter`
	"""
	#nrows, ncols = self.shape()[:2]
        if 'dtype' in kwargs:
	    dtype = kwargs['dtype']
	else:
	    dtype = _dtype_
	L = len(pwfn(np.random.randn(len(self))))
	#testv = pwfn(self.pix_iter(rand=True,**kwargs).next()[0])
	#L = len(testv)
	out = lib.memsafe_arr((L,) + self.shape(), dtype)
        for v, row, col in self.pix_iter(**kwargs):
	    if verbose:
		sys.stderr.write('\rworking on pixel (%03d,%03d)'%(row, col))
            out[:,row,col] = pwfn(v)
	    if hasattr(out, 'flush'):
		out.flush()

        ## assuming the dz is not changed. if it *is*, provide new meta
        ## in kwargs
        if 'meta' in kwargs:
            newmeta = kwargs['meta'].copy()
        else:
            newmeta = self.meta.copy() 
        return FSeq_arr(out, meta=newmeta)
Beispiel #2
0
def supp_from_obj(object,
                  min_level=0,
                  max_level=10,
                  verbose=0,
                  mode=0,
                  weights=None):
    """Return support arrays from object"""
    sh = object.labels.shape
    new_shape = [object.level + 1] + list(sh)
    out = lib.memsafe_arr(new_shape, _dtype_)
    flat = flat_tree(object)
    nfl = len(flat)
    if weights is None:
        weights = [1] * (object.level + 1)
    for j, n in enumerate(flat):
        if verbose:
            sys.stderr.write('\rnode %d out of %d' % (j + 1, nfl))
        if n.level > min_level and n.level <= max_level:
            val = weights[n.level]
            if mode == 1:
                out[n.level][n.labels == n.ind] = val
            elif mode == 2:
                out[n.level][n.labels == n.ind] = n.ind
            else:
                out[n.level][n.slice] = np.where(n.labels[n.slice] == n.ind,
                                                 val, 0)
    return out
Beispiel #3
0
    def as3darray(self,  fslice=None, crop=None,
                  dtype = _dtype_):
	"""Return the frames as a `3D` array.

	//An alternative way is to use the __getitem__ interface:
	//``data = np.asarray(fs[10:100])``
	

	Parameters:
	  - `fslice`: (`int`, `tuple-like` or `None`) --
          [start,] stop [, step] to go through frames
	  - `crop`: (`slice` or `None`) -- a crop (tuple of slices) to take from each frame
	  - `dtype`: (`type`) -- data type to use. Default, ``np.float64``

	Returns:
	  `3D` array `d`, where frames are stored in higher dimensions, such
	  that ``d[0]`` is the first frame, etc.
	"""
        if fslice is None or isinstance(fslice, int):
            fslice = (fslice, )
        shape = self.shape(crop)
	newshape = (len(self),) + shape
        out = lib.memsafe_arr(newshape, dtype)
        for k,frame in enumerate(itt.islice(self.frames(), *fslice)):
            out[k,:,:] = frame[crop]
        out = out[:k+1]
        if hasattr (out, 'flush'):
            out.flush()
        return out
Beispiel #4
0
def supp_from_connectivity(graph, nlevels):
    nodes = lib.flatten(graph)
    sh = nodes[0].labels.shape
    new_shape = [nlevels] + list(sh)
    out = lib.memsafe_arr(new_shape, _dtype_) * 0.0
    for n in nodes:
        out[n.level][n.labels > 0] = 1.0
    return out