Exemple #1
0
    def update_particles(self, delta):
        # radial: posx + posy
        norm = numpy.sqrt(self.particle_pos[:, 0] ** 2 + self.particle_pos[:, 1] ** 2)
        # XXX prevent div by 0
        norm = numpy.select([norm == 0], [0.0000001], default=norm)
        posx = self.particle_pos[:, 0] / norm
        posy = self.particle_pos[:, 1] / norm

        radial = numpy.array([posx, posy])
        tangential = numpy.array([-posy, posx])

        # update dir
        radial = numpy.swapaxes(radial, 0, 1)
        radial *= self.particle_rad
        tangential = numpy.swapaxes(tangential, 0, 1)
        tangential *= self.particle_tan

        self.particle_dir += (tangential + radial + self.particle_grav) * delta

        # update pos with updated dir
        self.particle_pos += self.particle_dir * delta

        # life
        self.particle_life -= delta

        # color
        self.particle_color += self.particle_delta_color * delta

        # if life < 0, set alpha in 0
        self.particle_color[:, 3] = numpy.select([self.particle_life[:, 0] < 0], [0], default=self.particle_color[:, 3])
def image_read_np(file_place):
	im = imread(file_place)
	if len(im.shape) == 2:
		im = im[:, :, np.newaxis]
		im = np.repeat(im, 3, axis=2)
	h, w, _ = im.shape
	if h < w:
		im = skimage.transform.resize(im, (224, w*224/h), preserve_range=True)
	else:
		im = skimage.transform.resize(im, (h*224/w, 224), preserve_range=True)

	# Central crop to 224x224
	h, w, _ = im.shape
	im = im[h//2-112:h//2+112, w//2-112:w//2+112]
    
	rawim = np.copy(im).astype('uint8')
    
	# Shuffle axes to c01
	im = np.swapaxes(np.swapaxes(im, 1, 2), 0, 1)

	# Convert to BGR
	im = im[::-1, :, :]

	im = im - MEAN_VALUES
	return rawim.transpose(2, 0, 1).astype(np.float32)
def rotate_data(bg, overlay, slices_list, axis_name, shape):
    # Rotate the data as required
    # Return the rotated data, and an updated slice list if necessary
    if axis_name == 'axial':
        # Align so that right is right
        overlay = np.rot90(overlay)
        overlay = np.fliplr(overlay)
        bg = np.rot90(bg)
        bg = np.fliplr(bg)
    
    elif axis_name == 'coronal':
        overlay = np.rot90(overlay)
        bg = np.rot90(bg)
        overlay = np.flipud(np.swapaxes(overlay, 0, 2))
        bg = np.flipud(np.swapaxes(bg, 0, 2))
        slices_list[1] = [ shape - n - 3 for n in slices_list[1] ] 
        
    elif axis_name == 'sagittal':
        overlay = np.flipud(np.swapaxes(overlay, 0, 2))
        bg = np.flipud(np.swapaxes(bg, 0, 2))
    
    else:
        print '\n************************'
        print 'ERROR: data could not be rotated\n'
        parser.print_help()
        sys.exit()
    
    return bg, overlay, slices_list
def prep_image(im, IMAGE_W, IMAGE_H, BGR=BGR):
    if len(im.shape) == 2:
        im = im[:, :, np.newaxis]
        im = np.repeat(im, 3, axis=2)
    h, w, _ = im.shape
    if h*IMAGE_W < w*IMAGE_H:
        im = skimage.transform.resize(im, (IMAGE_H, w*IMAGE_H//h), preserve_range=True)
    else:
        im = skimage.transform.resize(im, (h*IMAGE_W//w, IMAGE_W), preserve_range=True)        

    # Central crop
    h, w, _ = im.shape
    im = im[h//2-IMAGE_H//2:h//2+IMAGE_H//2, w//2-IMAGE_W//2:w//2+IMAGE_W//2]
    
    rawim = im.astype('uint8')
    
    # Shuffle axes to c01
    im = np.swapaxes(np.swapaxes(im, 1, 2), 0, 1)
    
    # Convert RGB to BGR
    if not BGR:
        im = im[::-1, :, :]

    im = im - MEAN_VALUES
    return rawim, floatX(im[np.newaxis])
Exemple #5
0
def rgb2caffe(im, downscale=True, out_size=(128, 171)):
    '''
    Converts an RGB image to caffe format and downscales it as needed by C3D

    Parameters
    ----------
    im an RGB image
    downscale

    Returns
    -------
    a caffe image (channel,height, width) in BGR format

    '''
    im=np.copy(im)
    if len(im.shape)==2: # Make sure the image has 3 channels
        im = color.gray2rgb(im)

    if downscale:
        h, w, _ = im.shape
        im = skimage.transform.resize(im, out_size, preserve_range=True)
    else:
        im=np.array(im,theano.config.floatX)
    im = np.swapaxes(np.swapaxes(im, 1, 2), 0, 1)

    # Convert to BGR
    im = im[::-1, :, :]

    return np.array(im,theano.config.floatX)
Exemple #6
0
def convert(matrix, TTT, axis=-1):
    """ Apply linear matrix transformation to an array of color triples.

    Parameters
    ----------
    matrix : float array (3, 3)
        The transformation to apply.
    TTT : float array
        The set of colors to transform.
    axis : int, optional
        The axis of `TTT` along which the color triples extend.

    Returns
    -------
    OUT : float array
        The transformed colors.
    """

    TTT = np.asarray(TTT)
    if axis != 0:
        TTT = np.swapaxes(TTT, 0, axis)
    oldshape = TTT.shape
    TTT = np.reshape(TTT, (3, -1))
    OUT = np.dot(matrix, TTT)
    OUT.shape = oldshape
    if axis != 0:
        OUT = np.swapaxes(OUT, axis, 0)
    return OUT
def load_minibatch(input_list, color, labels, start,num):
    # Enforce maximum on start
    start = max(0,start)

    # Enforce minimum on end
    end = start + num
    end = min(len(input_list), end)

    # Isolate files
    files = input_list[start:end]

    images = []
    for file in files:
        img = caffe.io.load_image(file, color)
        
        # Handle incorrect image dims for uncropped images
        # TODO: Get uncropped images to import correctly
        if img.shape[0] == 3 or img.shape[0] == 1:
            img = np.swapaxes(np.swapaxes(img, 0, 1), 1, 2)
        
        # BUG FIX: Is this ok?
        # color=True gets the correct desired dimension of WxHx3
        # But color=False gets images of WxHx1. Need WxHx3 or will get "Index out of bounds" exception
        # Fix by concatenating three copies of the image
        if img.shape[2] == 1:
            img = cv.merge([img,img,img])

        # Add image array to batch
        images.append(img)

    labelsReduced = labels[start:end]
    return images, labelsReduced
Exemple #8
0
def read_aps_13id(
        fname, group='/xrfmap/roimap/sum_cor', proj=None, sino=None):
    """
    Read APS 13-ID standard data format.

    Parameters
    ----------
    fname : str
        Path to hdf5 file.

    group : str, optional
        Path to the group inside hdf5 file where data is located.

    proj : {sequence, int}, optional
        Specify projections to read. (start, end, step)

    sino : {sequence, int}, optional
        Specify sinograms to read. (start, end, step)

    Returns
    -------
    ndarray
        3D tomographic data.
    """
    tomo = tio.read_hdf5(fname, group, slc=(None, proj, sino))
    tomo = np.swapaxes(tomo, 0, 1)
    tomo = np.swapaxes(tomo, 1, 2).copy()
    return tomo
Exemple #9
0
    def load_image(self, impath, normalize=False):

        if os.path.splitext(impath)[1] == ".npz":
            im = np.load(impath)["im"]

        else:

            im = skimage.io.imread(impath)
            if normalize:
                im = im / 255.0

            im = resize(im, self.imshape, mode="nearest")

        if self.mean_im is not None:
            im -= self.mean_im

        # shuffle from (W,H,3) to (3,W,H)
        if not self.greyscale:
            im = np.swapaxes(im, 0, 2)
            im = np.swapaxes(im, 1, 2)
        else:
            if im.ndim == 3:
                im = skimage.color.rgb2grey(im)

        return im
Exemple #10
0
def _raw_fftnd(x, s, axes, direction, overwrite_x, work_function):
    """ Internal auxiliary function for fftnd, ifftnd."""
    if s is None:
        if axes is None:
            s = x.shape
        else:
            s = numpy.take(x.shape, axes)

    s = tuple(s)
    if axes is None:
        noaxes = True
        axes = list(range(-x.ndim, 0))
    else:
        noaxes = False
    if len(axes) != len(s):
        raise ValueError("when given, axes and shape arguments "
                         "have to be of the same length")

    for dim in s:
        if dim < 1:
            raise ValueError("Invalid number of FFT data points (%d) specified." % s)

    # No need to swap axes, array is in C order
    if noaxes:
        for i in axes:
            x, copy_made = _fix_shape(x, s[i], i)
            overwrite_x = overwrite_x or copy_made
        return work_function(x,s,direction,overwrite_x=overwrite_x)

    # We ordered axes, because the code below to push axes at the end of the
    # array assumes axes argument is in ascending order.
    id = numpy.argsort(axes)
    axes = [axes[i] for i in id]
    s = [s[i] for i in id]

    # Swap the request axes, last first (i.e. First swap the axis which ends up
    # at -1, then at -2, etc...), such as the request axes on which the
    # operation is carried become the last ones
    for i in range(1, len(axes)+1):
        x = numpy.swapaxes(x, axes[-i], -i)

    # We can now operate on the axes waxes, the p last axes (p = len(axes)), by
    # fixing the shape of the input array to 1 for any axis the fft is not
    # carried upon.
    waxes = list(range(x.ndim - len(axes), x.ndim))
    shape = numpy.ones(x.ndim)
    shape[waxes] = s

    for i in range(len(waxes)):
        x, copy_made = _fix_shape(x, s[i], waxes[i])
        overwrite_x = overwrite_x or copy_made

    r = work_function(x, shape, direction, overwrite_x=overwrite_x)

    # reswap in the reverse order (first axis first, etc...) to get original
    # order
    for i in range(len(axes), 0, -1):
        r = numpy.swapaxes(r, -i, axes[-i])

    return r
def acorr(x, axis=-1, onesided=False, scale='none'):
    """Compute autocorrelation of x along given axis.

    Parameters
    ----------
        x : array-like
            signal to correlate.
        axis : int
            axis along which autocorrelation is computed.
        onesided: bool, optional
            if True, only returns the right side of the autocorrelation.
        scale: {'none', 'coeff'}
            scaling mode. If 'coeff', the correlation is normalized such as the
            0-lag is equal to 1.

    Notes
    -----
        Use fft for computation: is more efficient than direct computation for
        relatively large n.
    """
    if not np.isrealobj(x):
        raise ValueError("Complex input not supported yet")
    if not scale in ['none', 'coeff']:
        raise ValueError("scale mode %s not understood" % scale)

    maxlag = x.shape[axis]
    nfft = 2 ** nextpow2(2 * maxlag - 1)

    if axis != -1:
        x = np.swapaxes(x, -1, axis)
    a = _acorr_last_axis(x, nfft, maxlag, onesided, scale)
    if axis != -1:
        a = np.swapaxes(a, -1, axis)
    return a
Exemple #12
0
def preprocess_image(im_file):
    """
    preprocess image to 256 for neural network to work
    """
    # ways to get image on the web
    # import io
    # ext = url.split('.')[-1]
    # im = plt.imread(io.BytesIO(urllib.urlopen(url).read()), ext)

    im = plt.imread(open(im_file, 'r'))

    # resize to smalled dimension of 256 while preserving aspect ratio
    h, w, c = im.shape

    if h < w:
        im = skimage.transform.resize(im, (256, w/h*256), preserve_range=True)
    else:
        im = skimage.transform.resize(im, (h/w*256, 256), preserve_range=True)

    h, w, c = im.shape

    # central crop to 224x224
    im = im[h//2-112:h//2+112, w//2-112:w//2+112]

    rawim = np.copy(im).astype('uint8')

    # Shuffle axes to c01
    im = np.swapaxes(np.swapaxes(im, 1, 2), 0, 1)

    # Convert to BGR
    im = im[::-1, :, :]

    im = im - MEAN_IMAGE
    return rawim, floatX(im[np.newaxis])
 def _npBatchMatmul(self, x, y, adjoint_a, adjoint_b):
   # output's shape depends on adj[0] and adj[1]
   if adjoint_a:
     x = np.conjugate(np.swapaxes(x, -1, -2))
   if adjoint_b:
     y = np.conjugate(np.swapaxes(y, -1, -2))
   return np.matmul(x, y)
    def rotateLeft(self):
        self._fakeEntities = None
        self._Blocks = swapaxes(self._Blocks, 1, 2)[:, ::-1, :]  # x=z; z=-x
        if "Biomes" in self.root_tag:
            self.root_tag["Biomes"].value = swapaxes(self.root_tag["Biomes"].value, 0, 1)[::-1, :]

        self.root_tag["Data"].value = swapaxes(self.root_tag["Data"].value, 1, 2)[:, ::-1, :]  # x=z; z=-x
        self._update_shape()

        blockrotation.RotateLeft(self.Blocks, self.Data)

        log.info(u"Relocating entities...")
        for entity in self.Entities:
            for p in "Pos", "Motion":
                if p == "Pos":
                    zBase = self.Length
                else:
                    zBase = 0.0
                newX = entity[p][2].value
                newZ = zBase - entity[p][0].value

                entity[p][0].value = newX
                entity[p][2].value = newZ
            entity["Rotation"][0].value -= 90.0
            if entity["id"].value in ("Painting", "ItemFrame") or MCEDIT_IDS.get(entity["id"]) in ('DEFS_ENTITIES_PAINTING', 'DEFS_ENTITIES_ITEM_FRAME'):
                x, z = entity["TileX"].value, entity["TileZ"].value
                newx = z
                newz = self.Length - x - 1

                entity["TileX"].value, entity["TileZ"].value = newx, newz
                facing = entity.get("Facing", entity.get("Direction"))
                if facing is None:
                    dirFacing = entity.get("Dir")
                    if dirFacing is not None:
                        if dirFacing.value == 0:
                            dirFacing.value = 2
                        elif dirFacing.value == 2:
                            dirFacing.value = 0
                        facing = dirFacing
                    else:
                        raise Exception("None of tags Facing/Direction/Dir found in entity %s during rotating -  %r" % (entity["id"].value, entity))
                facing.value = (facing.value - 1) % 4

        for tileEntity in self.TileEntities:
            if 'x' not in tileEntity:
                continue

            newX = tileEntity["z"].value
            newZ = self.Length - tileEntity["x"].value - 1

            tileEntity["x"].value = newX
            tileEntity["z"].value = newZ

        if "TileTicks" in self.root_tag:
            for tileTick in self.TileTicks:
                newX = tileTick["z"].value
                newZ = tileTick["x"].value

                tileTick["x"].value = newX
                tileTick["z"].value = newZ
Exemple #15
0
    def _read_img(self, img, label):
        if self.regress_overlay:
            label = label
        else:
            label = 0

        img = np.array(img)
        img = img.reshape(self.crop_size, self.crop_size)
        #cv2.imshow("GRAY", img)
        #cv2.waitKey(0)
        
        if self.scale_size is not None:
            img = cv2.resize(img, (self.scale_size, self.scale_size), interpolation=cv2.INTER_AREA).astype(float)        
    

        img = img.reshape(img.shape[0], img.shape[1], 1)           
        img = np.swapaxes(img, 0, 2)
        img = np.swapaxes(img, 1, 2)  # (c, h, w)
        
        label_1 = label#20161026
        flag = False
##        for t in label:
##            if t > 1:
##                print("label_1: ", label_1)
##                flag =True

        
        label = np.array(label_1)             
        img = np.expand_dims(img, axis=0)  # (1, c, h, w) or (1, h, w)
        #print(label)

        return img, label, False#label_1, label_2, flag
Exemple #16
0
  def convert_to_batched_episodes(self, episodes, max_length=None):
    """Convert batch-major list of episodes to time-major batch of episodes."""
    lengths = [len(ep[-2]) for ep in episodes]
    max_length = max_length or max(lengths)

    new_episodes = []
    for ep, length in zip(episodes, lengths):
      initial, observations, actions, rewards, terminated = ep
      observations = [np.resize(obs, [max_length + 1] + list(obs.shape)[1:])
                      for obs in observations]
      actions = [np.resize(act, [max_length + 1] + list(act.shape)[1:])
                 for act in actions]
      pads = np.array([0] * length + [1] * (max_length - length))
      rewards = np.resize(rewards, [max_length]) * (1 - pads)
      new_episodes.append([initial, observations, actions, rewards,
                           terminated, pads])

    (initial, observations, actions, rewards,
     terminated, pads) = zip(*new_episodes)
    observations = [np.swapaxes(obs, 0, 1)
                    for obs in zip(*observations)]
    actions = [np.swapaxes(act, 0, 1)
               for act in zip(*actions)]
    rewards = np.transpose(rewards)
    pads = np.transpose(pads)

    return (initial, observations, actions, rewards, terminated, pads)
Exemple #17
0
 def _read_img(self, img_name, label_name):
     img = Image.open(os.path.join(self.root_dir, img_name))
     label = Image.open(os.path.join(self.root_dir, label_name))
     assert img.size == label.size
     img = np.array(img, dtype=np.float32)  # (h, w, c)
     label = np.array(label)  # (h, w)
     if self.cut_off_size is not None:
         max_hw = max(img.shape[0], img.shape[1])
         min_hw = min(img.shape[0], img.shape[1])
         if min_hw > self.cut_off_size:
             rand_start_max = round(np.random.uniform(0, max_hw - self.cut_off_size - 1))
             rand_start_min = round(np.random.uniform(0, min_hw - self.cut_off_size - 1))
             if img.shape[0] == max_hw :
                 img = img[rand_start_max : rand_start_max + self.cut_off_size, rand_start_min : rand_start_min + self.cut_off_size]
                 label = label[rand_start_max : rand_start_max + self.cut_off_size, rand_start_min : rand_start_min + self.cut_off_size]
             else :
                 img = img[rand_start_min : rand_start_min + self.cut_off_size, rand_start_max : rand_start_max + self.cut_off_size]
                 label = label[rand_start_min : rand_start_min + self.cut_off_size, rand_start_max : rand_start_max + self.cut_off_size]
         elif max_hw > self.cut_off_size:
             rand_start = round(np.random.uniform(0, max_hw - min_hw - 1))
             if img.shape[0] == max_hw :
                 img = img[rand_start : rand_start + min_hw, :]
                 label = label[rand_start : rand_start + min_hw, :]
             else :
                 img = img[:, rand_start : rand_start + min_hw]
                 label = label[:, rand_start : rand_start + min_hw]
     reshaped_mean = self.mean.reshape(1, 1, 3)
     img = img - reshaped_mean
     img = np.swapaxes(img, 0, 2)
     img = np.swapaxes(img, 1, 2)  # (c, h, w)
     img = np.expand_dims(img, axis=0)  # (1, c, h, w)
     label = np.array(label)  # (h, w)
     label = np.expand_dims(label, axis=0)  # (1, h, w)
     return (img, label)
def _eval_fun(f, tmp, n, axis, nm, overwrite_x):
    if axis == -1 or axis == len(tmp.shape) - 1:
        return f(tmp, n, nm, overwrite_x)

    tmp = np.swapaxes(tmp, axis, -1)
    tmp = f(tmp, n, nm, overwrite_x)
    return np.swapaxes(tmp, axis, -1)
def load_image(file_name):
    """
    Load and preprocess an image
    """
    MEAN_VALUE = numpy.array([103.939, 116.779, 123.68]).reshape((3,1,1))
    image = Image.open(file_name)
    im = numpy.array(image)

    # Resize so smallest dim = 256, preserving aspect ratio
    if len(im.shape) == 2:
        im = im[:, :, numpy.newaxis]
        im = numpy.repeat(im, 3, axis=2)
    h, w, _ = im.shape
    if h < w:
        im = skimage.transform.resize(im, (256, w*256/h), preserve_range=True)
    else:
        im = skimage.transform.resize(im, (h*256/w, 256), preserve_range=True)

    # Central crop to 224x224
    h, w, _ = im.shape
    im = im[h//2-112:h//2+112, w//2-112:w//2+112]

    rawim = numpy.copy(im).astype('uint8')

    # Shuffle axes to c01
    im = numpy.swapaxes(numpy.swapaxes(im, 1, 2), 0, 1)

    # Convert to BGR
    im = im[::-1, :, :]

    im = im - MEAN_VALUE
    return rawim, floatX(im[numpy.newaxis])
    def get_minibatch(self, firstIdxHMDB, lastIdxHMDB, firstIdxUCF, lastIdxUCF, batch_size,
                      data_specs, return_tuple):
 
        x = np.zeros([batch_size, self.data2.shape[1]] , dtype="float32")
        y = np.zeros([batch_size, self.nbTags],  dtype="float32")
        second_y = np.zeros([batch_size, self.nbTags2],  dtype="float32")
		
        #UCF
        dataTmp = self.data[firstIdxUCF:lastIdxUCF,0:1570800]
        # re-scale back to float32 (UCF101)
        dataTmp=dataTmp.astype("float32")
        dataTmp *= (1./255.)
        x[0:(lastIdxUCF -firstIdxUCF),:] = dataTmp
        #import pdb; pdb.set_trace()
        y[0:(lastIdxUCF -firstIdxUCF),:] = self.labels[firstIdxUCF:lastIdxUCF]
        second_y[0:(lastIdxUCF -firstIdxUCF),:] = 0.0	
        #HMDB
        dataTmp = self.data2[firstIdxHMDB:lastIdxHMDB]
        x[(lastIdxUCF -firstIdxUCF):,:] = dataTmp
        y[(lastIdxUCF -firstIdxUCF):,:] = 0.0;
        second_y[(lastIdxUCF -firstIdxUCF):,:] = self.labels2[firstIdxHMDB:lastIdxHMDB];
		
        y = np.concatenate((y, second_y), axis=1)
        x = x.reshape(batch_size, self.nb_t, self.nb_x, self.nb_y, self.nb_feats)
        x  = np.swapaxes(x, 1, 2) # 'b, 0, 't', 1, 'c'
        x  = np.swapaxes(x, 2, 3) # 'b, 0, 1, 't', 'c'
      
        print "Returned a minibatch", lastIdxHMDB, lastIdxUCF
        return x, y
Exemple #21
0
 def defects_hessian_ind(self):
     """Indices of nonzero elements of ODE equality constraints Hessian."""
     ncolpt = self.collocation.n
     ncolinterv = self.collocation.ninterv
     d2f_dx2_i, d2f_dx2_j, d2f_dx2_k = self.model.d2f_dx2_ind
     d2f_dq2_i, d2f_dq2_j, d2f_dq2_k = self.model.d2f_dq2_ind
     d2f_dx_dq_i, d2f_dx_dq_j, d2f_dx_dq_k = self.model.d2f_dx_dq_ind
     d2f_dx2_k = np.asarray(d2f_dx2_k, dtype=int)
     d2f_dq2_k = np.asarray(d2f_dq2_k, dtype=int)
     d2f_dx_dq_k = np.asarray(d2f_dx_dq_k, dtype=int)
     
     offsets = np.arange(self.npieces * self.collocation.ninterv)
     offsets = offsets.reshape((self.npieces, self.collocation.ninterv, 1))
     offsets *= self.model.nx
     d2fr_dx2_i = self.unravel_pieces(self.expand_x_ind(d2f_dx2_i))
     d2fr_dq2_i = self.unravel_pieces(self.expand_q_ind(d2f_dq2_i))
     d2fr_dx_dq_i = self.unravel_pieces(self.expand_q_ind(d2f_dx_dq_i))
     d2fr_dx2_j = self.unravel_pieces(self.expand_x_ind(d2f_dx2_j))
     d2fr_dq2_j = self.unravel_pieces(self.expand_q_ind(d2f_dq2_j))
     d2fr_dx_dq_j = self.unravel_pieces(self.expand_x_ind(d2f_dx_dq_j))
     d2fr_dx2_k = np.swapaxes(offsets + d2f_dx2_k, 1, 2)
     d2fr_dq2_k = np.swapaxes(offsets + d2f_dq2_k, 1, 2)
     d2fr_dx_dq_k = np.swapaxes(offsets + d2f_dx_dq_k, 1, 2)
     i = utils.flat_cat(np.repeat(d2fr_dx2_i, ncolinterv),
                        np.repeat(d2fr_dq2_i, ncolinterv),
                        np.repeat(d2fr_dx_dq_i, ncolinterv))
     j = utils.flat_cat(np.repeat(d2fr_dx2_j, ncolinterv),
                        np.repeat(d2fr_dq2_j, ncolinterv),
                        np.repeat(d2fr_dx_dq_j, ncolinterv))
     k = utils.flat_cat(np.repeat(d2fr_dx2_k, ncolpt, 0),
                        np.repeat(d2fr_dq2_k, ncolpt, 0),
                        np.repeat(d2fr_dx_dq_k, ncolpt, 0))
     return (i, j, k)
Exemple #22
0
def plot_color_filters(x, idx, title=""):
    num_filters = x.shape[0]
    numrows = 10
    numcols = int(np.ceil(num_filters/10))
    plt.figure(figsize=(numrows, numcols))
    gs = gridspec.GridSpec(numcols, numrows)
    gs.update(wspace=0.1)
    gs.update(hspace=0.0)

    print("plotting color filters")
    for i in range(num_filters):
        ax = plt.subplot(gs[i])
        w = x[i, :, :, :]
        w = np.swapaxes(w, 0, 1)
        w = np.swapaxes(w, 1, 2)

        #normalize
        
        r = w[:,:,0] - np.min(w[:,:,0])
        g = w[:,:,1] - np.min(w[:,:,1])
        b = w[:,:,2] - np.min(w[:,:,2])
        r = r * 1.0 / np.max(r)
        g = g * 1.0 / np.max(g)
        b = b * 1.0 / np.max(b)
        w = np.dstack((r,g,b))
        ax.imshow(w,
                  cmap=plt.cm.gist_yarg,
                  interpolation='nearest',
                  aspect='equal')
        ax.set_xticklabels([])
        ax.set_yticklabels([])
        ax.axis('off')
    plt.savefig(os.path.join('convfilters', title+ '_' + str(idx) + '_convcaustics.png'))
    print(os.path.join('convfilters', title+ '_' + str(idx) + '_convcaustics.png'))
    plt.close('all')
Exemple #23
0
def prepare_image(img, width, means):
    
    # if not RGB, force 3 channels
    if len(img.shape) == 2:
        img = img[:, :, np.newaxis]
        img = np.repeat(img, 3, axis=2)
    h, w, _ = img.shape
    if h < w:
        img = skimage.transform.resize(img, (width, w*width/h), preserve_range=True)
    else:
        img = skimage.transform.resize(img, (h*width/w, width), preserve_range=True)

    # crop the center
    h, w, _ = img.shape
    img = img[h//2 - width//2:h//2 + width//2, w//2 - width//2:w//2 + width//2]
    
    rawim = np.copy(img).astype('uint8')
    
    # shuffle axes to c01
    img = np.swapaxes(np.swapaxes(img, 1, 2), 0, 1)
    
    # convert RGB to BGR
    img = img[::-1, :, :]
    
    # zero mean scaling
    img = img - means
    
    return rawim, floatX(img[np.newaxis])
Exemple #24
0
    def initializeIndices(self, ni, nj):
        if not ni==None:
            self.ni = numpy.array(ni,int)
        if not nj==None:
            self.nj = numpy.array(nj,int)
        if self.mSide==-1:
            if ni==None:
                self.ni = [1,self.mComp.ms[0].shape[0],1]
            if nj==None:
                self.nj = [1,self.mComp.ms[2].shape[0],1]
        else:
            if ni==None:
                self.ni = [1,0,1]
            if nj==None:
                self.nj = [1,self.mComp.ms[0].shape[0],1]

        if self.fRot==0:
            self.rotate = lambda P: P
            self.flip = lambda nu, nv: [nu,nv]
        elif self.fRot==1:
            self.rotate = lambda P: numpy.swapaxes(P,0,1)[::-1,:]
            self.flip = lambda nu, nv: [nv[::-1],nu]
        elif self.fRot==2:
            self.rotate = lambda P: P[::-1,::-1]
            self.flip = lambda nu, nv: [nu[::-1],nv[::-1]]
        elif self.fRot==3:
            self.rotate = lambda P: numpy.swapaxes(P,0,1)[:,::-1]
            self.flip = lambda nu, nv: [nv,nu[::-1]]

        self.fK = self.rotate(self.fComp.Ks[self.fFace])[self.fNW[0]:self.fNW[0]+sum(self.ni),self.fNW[1]:self.fNW[1]+sum(self.nj)]
Exemple #25
0
def reorder_array(the_array, order_list):
	# put time first as by agrreement in IDTxL
	time_dimension = order_list.index("time")
	if time_dimension != 1:
		the_array = np.swapaxes(the_array,1,time_dimension)
		# also swap the list to reflect the new arrangement
		order_list[1], order_list[time_dimension] = \
		order_list[time_dimension], order_list[1]
		
	# put channel second
	channel_dimension = order_list.index("channel")
	if channel_dimension !=2:
		the_array = np.swapaxes(the_array,2,channel_dimension)
		# also swap the list to reflect the new arrangement
		order_list[2], order_list[channel_dimension] = \
		order_list[channel_dimension], order_list[2]
		
	# put repetitions third - unnecessary in principle as n-1 permutations
	# are guaranteed to sort our array dimensions for n dimensions
	#assert order_list.index("repetition") == 3, print('something went wrong with reordering')
	
	# uncomment the following code when expanding
	#repetition_dimension = order_list.index("repetition")
	#if repetition_dimension !=2:
	#	the_array = np.swapaxes(the_array,2,repetition_dimension)
	#	# also swap the list to reflect the new arrangement
	#	order_list[3], order_list[repetition_dimension] = \
	#	order_list[repetition_dimension], order_list[3]
	
	# put further dimensions fourth in future versions...
	return the_array
Exemple #26
0
    def dGamma_dgam(self,gam_IM,gam_MD1,gam_D1D2,gam_D2R,
                    lam_IM,lam_MD1,lam_D1D2,lam_D2R):

        dgdgam_IM, dgdgam_MD1, dgdgam_D1D2, dgdgam_D2R = \
            self.dFrequencyCostdGamma_dgdgam(gam_IM,gam_MD1,gam_D1D2,gam_D2R)
        dedgam_IM, dedgam_MD1, dedgam_D1D2, dedgam_D2R = \
            self.dEmissionCostdGamma_dedgam(gam_IM,gam_MD1,gam_D1D2,gam_D2R)

        w_IM = np.reshape(np.tile(np.swapaxes(self.w[None],0,1),
                                  np.prod(dedgam_IM.shape[1:])).flatten(),
                          dedgam_IM.shape)
        w_MD1 = np.reshape(np.tile(np.swapaxes(self.w[None],0,1),
                                   np.prod(dedgam_MD1.shape[1:])).flatten(),
                           dedgam_MD1.shape)
        w_D1D2 = np.reshape(np.tile(np.swapaxes(self.w[None],0,1),
                                    np.prod(dedgam_D1D2.shape[1:])).flatten(),
                            dedgam_D1D2.shape)
        w_D2R = np.reshape(np.tile(np.swapaxes(self.w[None],0,1),
                                   np.prod(dedgam_D2R.shape[1:])).flatten(),
                           dedgam_D2R.shape)

        dgam_IM = dgdgam_IM + w_IM*dedgam_IM - self.u_IM*lam_IM
        dgam_MD1 = dgdgam_MD1 + w_MD1*dedgam_MD1 - self.u_MD1*lam_MD1
        dgam_D1D2 = dgdgam_D1D2 + w_D1D2*dedgam_D1D2 - self.u_D1D2*lam_D1D2
        dgam_D2R = dgdgam_D2R + w_D2R*dedgam_D2R - self.u_D2R*lam_D2R

        return dgam_IM, dgam_MD1, dgam_D1D2, dgam_D2R
 def generate_batch(num_samples, obj_type='circle'):
     # generate a minibatch of trajectories
     traj_pos, traj_vel = TRAJ.generate_trajectories(num_samples, traj_len)
     traj_x = traj_pos[:,:,0]
     traj_y = traj_pos[:,:,1]
     # draw the trajectories
     center_x = to_fX( traj_x.T.ravel() )
     center_y = to_fX( traj_y.T.ravel() )
     delta = to_fX( np.ones(center_x.shape) )
     sigma = to_fX( np.ones(center_x.shape) )
     if obj_type == 'circle':
         W = paint_circle(center_y, center_x, delta, 0.05*sigma)
     else:
         W = paint_cross(center_y, center_x, delta, 0.05*sigma)
     # shape trajectories into a batch for passing to the model
     batch_imgs = np.zeros((num_samples, traj_len, obs_dim))
     batch_coords = np.zeros((num_samples, traj_len, 2))
     for i in range(num_samples):
         start_idx = i * traj_len
         end_idx = start_idx + traj_len
         img_set = W[start_idx:end_idx,:]
         batch_imgs[i,:,:] = img_set
         batch_coords[i,:,0] = center_x[start_idx:end_idx]
         batch_coords[i,:,1] = center_y[start_idx:end_idx]
     batch_imgs = np.swapaxes(batch_imgs, 0, 1)
     batch_coords = np.swapaxes(batch_coords, 0, 1)
     return [to_fX( batch_imgs ), to_fX( batch_coords )]
Exemple #28
0
def predictions_grid_color(samples, predictions, k, imgshape, title):
    batch_size = samples.shape[0]
    print("printintg predictions:")
    print(samples.shape)
    print(imgshape)
    print(predictions.shape)
    samples = samples.reshape(batch_size, imgshape[1], imgshape[2], imgshape[3])
    predictions = predictions.reshape(batch_size, imgshape[1], imgshape[2], imgshape[3])
    plt.figure(figsize=(10, 10))
    gs = gridspec.GridSpec(5, 2)
    for i in range(10):
        ax = plt.subplot(gs[i])
        if i % 2 == 0:
            w = samples[i/2, :, :, :]
        else:
            w = predictions[i/2, :, :, :]
        w = np.swapaxes(w, 0, 1)
        w = np.swapaxes(w, 1, 2)
        ax.imshow(w,
                  cmap=plt.cm.gist_yarg,
                  interpolation='nearest',
                  aspect='equal')
        ax.set_xticklabels([])
        ax.set_yticklabels([])
        ax.axis('off')
    gs.update(wspace=0)
    plt.savefig(os.path.join('convpredictions', str(k) + '_' + title + '.png'),
                bbox_inches='tight')
    plt.close('all')
Exemple #29
0
    def dLinkFlow_df(self,f_IM,f_MD1,f_D1D2,f_D2R,
                     lam_IM,lam_MD1,lam_D1D2,lam_D2R):

        dcdf_IM, dcdf_MD1, dcdf_D1D2, dcdf_D2R = \
            self.dOperationalCostdLinkFlow_dcdf(f_IM,f_MD1,f_D1D2,f_D2R)
        dedf_IM, dedf_MD1, dedf_D1D2, dedf_D2R = \
            self.dEmissionCostdLinkFlow_dedf(f_IM,f_MD1,f_D1D2,f_D2R)

        w_IM = np.reshape(np.tile(np.swapaxes(self.w[None],0,1),
                                  np.prod(dedf_IM.shape[1:])).flatten(),
                          dedf_IM.shape)
        w_MD1 = np.reshape(np.tile(np.swapaxes(self.w[None],0,1),
                                   np.prod(dedf_MD1.shape[1:])).flatten(),
                           dedf_MD1.shape)
        w_D1D2 = np.reshape(np.tile(np.swapaxes(self.w[None],0,1),
                                    np.prod(dedf_D1D2.shape[1:])).flatten(),
                            dedf_D1D2.shape)
        w_D2R = np.reshape(np.tile(np.swapaxes(self.w[None],0,1),
                                   np.prod(dedf_D2R.shape[1:])).flatten(),
                           dedf_D2R.shape)

        df_IM = dcdf_IM + w_IM*dedf_IM + lam_IM
        df_MD1 = dcdf_MD1 + w_MD1*dedf_MD1 + lam_MD1
        df_D1D2 = dcdf_D1D2 + w_D1D2*dedf_D1D2 + lam_D1D2
        df_D2R = dcdf_D2R + w_D2R*dedf_D2R + lam_D2R

        return df_IM, df_MD1, df_D1D2, df_D2R
Exemple #30
0
def plot_predictions(samples, predictions, k, batch_size, imgshape):
    samples = samples.reshape(batch_size, 3, imgshape, imgshape)
    predictions = predictions.reshape(batch_size, 3, imgshape, imgshape)
    samples = np.swapaxes(samples, 1,2)
    samples = np.swapaxes(samples, 2,3)
    predictions = np.swapaxes(predictions, 1,2)
    predictions = np.swapaxes(predictions, 2,3)
    for i in range(samples.shape[0]):
        fig = plt.figure(figsize=(5, 5))
        plt.imshow(samples[i, :, :, :],
                cmap=plt.cm.gist_yarg, interpolation='nearest',
                aspect='equal')
        path = "convpredictions"
        fname = str(k) + '_' + str(i) + 'sample_IN.png'
        plt.subplots_adjust(left=0, right=1, bottom=0, top=1)
        plt.savefig(os.path.join(path, fname), dpi=imgshape)
        plt.close('all')
        fig = plt.figure(figsize=(5, 5))
        print(predictions[i, :, :, :].shape)
        print(predictions[i, :, :, :])
        plt.imshow(predictions[i, :, :, :],
                cmap=plt.cm.gist_yarg, interpolation='nearest',
                aspect='equal')
        fname = str(k) + '_' + str(i) + 'sample_OUT.png'
        plt.subplots_adjust(left=0, right=1, bottom=0, top=1)
        plt.savefig(os.path.join(path, fname), dpi=imgshape)
        plt.close('all')
 def observation(self, observation):
     return np.swapaxes(observation, 2, 0)
Exemple #32
0
    ir_stream.set_video_mode(video_modes[0])
    for mode in video_modes:
        print(mode)

# Loop
while True:
    if depth:
        # Grab a new depth frame
        depth_frame = depth_stream.read_frame()
        depth_frame_data = depth_frame.get_buffer_as_uint16()

        # Put the depth frame into a numpy array and reshape it
        depth_img = np.frombuffer(depth_frame_data, dtype=np.uint16)
        depth_img.shape = (1, 480, 640)
        depth_img = np.concatenate((depth_img, depth_img, depth_img), axis=0)
        depth_img = np.swapaxes(depth_img, 0, 2)
        depth_img = np.swapaxes(depth_img, 0, 1)
        depth_img = cv2.convertScaleAbs(depth_img, alpha=(255.0 / 65535.0))
        # Display the reshaped depth frame using OpenCV
        cv2.imshow("Depth Image", depth_img)

    if color:
        # Grab a new color frame
        color_frame = color_stream.read_frame()
        color_frame_data = color_frame.get_buffer_as_uint8()

        # Put the color frame into a numpy array, reshape it, and convert from bgr to rgb
        color_img = np.frombuffer(color_frame_data, dtype=np.uint8)
        color_img.shape = (480, 640, 3)
        color_img = color_img[..., ::-1]
        # Display the reshaped depth frame using OpenCV
Exemple #33
0
from absl.testing import absltest
from absl.testing import parameterized

from jax import jvp
from jax import numpy as np
from jax import scipy as jsp
from jax import test_util as jtu
from jax.lib import xla_bridge

from jaxlib import lapack

from jax.config import config
config.parse_flags_with_absl()

T = lambda x: onp.swapaxes(x, -1, -2)


def float_types():
  return {onp.dtype(xla_bridge.canonicalize_dtype(dtype))
          for dtype in [onp.float32, onp.float64]}

def complex_types():
  return {onp.dtype(xla_bridge.canonicalize_dtype(dtype))
          for dtype in [onp.complex64, onp.complex128]}


class NumpyLinalgTest(jtu.JaxTestCase):

  @parameterized.named_parameters(jtu.cases_from_list(
      {"testcase_name":
 def _swap_axes(self):
     (x_train, y_train), (x_test, y_test) = self.mnist
     x_train = np.swapaxes(x_train, 1, 3)
     x_test = np.swapaxes(x_test, 1, 3)
     self.mnist = (x_train, y_train), (x_test, y_test)
Exemple #35
0
async def run(env: DerkSession, app: DerkAppInstance):
    for iteration in range(ITERATIONS):
        print("\n-----------------------------ITERATION " + str(iteration) +
              "-----------------------------")

        if iteration % save_model_every == 0:
            past_models.append(copy.deepcopy(agent))

        if iteration in model_checkpoint_schedule:
            torch.save(agent.state_dict(), save_folder + "/" + str(iteration))

        new_configs = [{
            "slots": [
                random.choice(arm_weapons),
                random.choice(misc_weapons),
                random.choice(tail_weapons)
            ]
        } for i in range(3 * n_arenas // 2)]
        await app.update_away_team_config(new_configs)
        await app.update_home_team_config(new_configs)
        observation = []
        done = []
        action = []
        reward = []
        state = None
        observation_n = await env.reset()
        while True:
            action_n, state = agent.get_action(observation_n, state)

            #act in environment and observe the new obervation and reward (done tells you if episode is over)
            observation_n, reward_n, done_n, _ = await env.step(action_n)

            #collect experience data to learn from
            observation.append(observation_n)
            reward.append(reward_n)
            done.append(done_n)
            action.append(action_n)

            if all(done_n):
                break

        # reshapes all collected data to [episode num, timestep]
        observation = np.swapaxes(np.array(observation), 0, 1)
        reward = np.swapaxes(np.array(reward), 0, 1)
        done = np.swapaxes(np.array(done), 0, 1)
        action = np.swapaxes(np.array(action), 0, 1)

        #learn from experience
        print("Training with PPO")
        agent.update(observation, action, reward)

        if iteration % 2 == 0:
            testing_against = max(0, (iteration - eval_against_gap) //
                                  save_model_every)
        if iteration % 4 == 0:
            testing_against = 0
        print("\nEvaluating against iteration ",
              testing_against * save_model_every)

        curr_agent_state = None
        past_agent_state = None

        observation_n = await env.reset()
        while True:
            #get actions for agent (first half of observations) and random agent (second half of observations)
            agent_action_n, curr_agent_state = agent.get_action(
                observation_n[:env.n_agents // 2], curr_agent_state)
            past_action_n, past_agent_state = past_models[
                testing_against].get_action(observation_n[env.n_agents // 2:],
                                            past_agent_state)
            action_n = agent_action_n.tolist() + past_action_n.tolist()

            #act in environment and observe the new obervation and reward (done tells you if episode is over)
            observation_n, reward_n, done_n, _ = await env.step(action_n)

            if all(done_n):
                total_home_reward = 0
                total_away_reward = 0
                for i in range(len(env.team_stats) // 2):
                    total_home_reward += env.team_stats[i][0]
                    total_away_reward += env.team_stats[i][1]
                total_home_reward /= (len(env.team_stats) // 2)
                total_away_reward /= (len(env.team_stats) // 2)

                print("Agent avg reward:", total_home_reward, " Iteration",
                      testing_against * save_model_every, "reward:",
                      total_away_reward)
                break
Exemple #36
0
ndarray.resize(new_shape[, refcheck])   Change shape and size of array in-place.
ndarray.transpose(*axes)                Returns a view of the array with axes transposed.
ndarray.swapaxes(axis1, axis2)          Return a view of the array with axis1 and axis2 interchanged.
ndarray.flatten([order])                Return a copy of the array collapsed into one dimension.
ndarray.ravel([order])                  Return a flattened array.
ndarray.squeeze([axis])                 Remove single-dimensional entries from the shape of a.
转置(transpose)和轴对换(swapaxes)
"""
a = np.array([[1], [4]], np.int32)
print(a.squeeze())
print(a.squeeze(axis=1))
a = np.array([[[0], [1], [2]]])
print(a.squeeze(axis=0))
print("--")
x = np.array([[1, 2, 3]])
print(np.swapaxes(x, 0, 1))
print("--")
x = np.array([[[0, 1], [2, 3]], [[4, 5], [6, 7]]])
print(x, "------", x[0, 1, 1])
# 比如 x[0,1,1]=2 第一维度,1第二维度,1第三维度
x_swap = x.swapaxes(0, 2)  # #就是将第一个维度和第三个维度交换
# 那么x_swap[1,1,0] = 2
print(x_swap, "------", x_swap[1, 1, 0])

print(" -------------------------item 选择和操作 methods")

a = [4, 3, 5, 7, 6, 8]
indices = [0, 1, 4]
print(np.take(a, indices))

a = np.array(a)
    def interpolate(self, x, idx, slice_idx):
        """
        Compute the interpolated value over this grid dimension.

        Parameters
        ----------
        x : ndarray
            The coordinates to sample the gridded data at. First array element is the point to
            interpolate here. Remaining elements are interpolated on sub tables.
        idx : integer
            Interval index for x.
        slice_idx : list of <slice>
            Slice object containing indices of data points requested by parent interpolating
            tables.

        Returns
        -------
        ndarray
            Interpolated values.
        ndarray
            Derivative of interpolated values with respect to this independent and child
            independents.
        ndarray
            Derivative of interpolated values with respect to values for this and subsequent table
            dimensions.
        ndarray
            Derivative of interpolated values with respect to grid for this and subsequent table
            dimensions.
        """
        grid = self.grid
        subtable = self.subtable

        # Extrapolate high
        if idx == len(grid) - 1:
            idx -= 1

        if subtable is not None:
            # Interpolate between values that come from interpolating the subtables in the
            # subsequent dimensions.
            nx = len(x)

            values, subderivs, _, _ = subtable.evaluate(x[1:], slice_idx=slice_idx)
            sec_deriv = self.compute_coeffs(grid, values, x)

            step = grid[idx + 1] - grid[idx]
            r_step = 1.0 / step
            a = (grid[idx + 1] - x[0]) * r_step
            b = (x[0] - grid[idx]) * r_step
            fact = 1.0 / 6.0

            interp_values = a * values[..., idx] + b * values[..., idx + 1] + \
                ((a * a * a - a) * sec_deriv[..., idx] +
                 (b * b * b - b) * sec_deriv[..., idx + 1]) * (step * step * fact)

            # Derivatives

            tshape = list(interp_values.shape)
            tshape.append(nx)
            derivs = np.empty(tuple(tshape), dtype=x.dtype)

            derivs[..., 0] = r_step * (values[..., idx + 1] - values[..., idx]) + \
                (((3.0 * b * b - 1) * sec_deriv[..., idx + 1] -
                  (3.0 * a * a - 1) * sec_deriv[..., idx]) * (step * fact))

            if nx == 2:
                dsec = self.compute_coeffs(grid, subderivs, x)
                derivs[..., 1] = ((a * a * a - a) * dsec[..., idx] +
                                  (b * b * b - b) * dsec[..., idx + 1]) * (step * step * fact)

                derivs[..., 1] += a * subderivs[..., idx] + b * subderivs[..., idx + 1]

            else:
                dsec = self.compute_coeffs(grid, np.swapaxes(subderivs, -1, -2), x)
                derivs[..., 1:] = ((a * a * a - a) * dsec[..., idx] +
                                   (b * b * b - b) * dsec[..., idx + 1]) * (step * step * fact)

                derivs[..., 1:] += a * subderivs[..., idx, :] + b * subderivs[..., idx + 1, :]

            return interp_values, derivs, None, None

        values = self.values

        # The coefficients can be cached because it is computed for every grid segment in
        # the model.
        if self.second_derivs is None:
            self.second_derivs = self.compute_coeffs(grid, values, x)
        sec_deriv = self.second_derivs

        # Perform the interpolation
        step = grid[idx + 1] - grid[idx]
        r_step = 1.0 / step
        a = (grid[idx + 1] - x) * r_step
        b = (x - grid[idx]) * r_step
        fact = 1.0 / 6.0

        val = a * values[..., idx] + b * values[..., idx + 1] + \
            ((a * a * a - a) * sec_deriv[..., idx] +
             (b * b * b - b) * sec_deriv[..., idx + 1]) * (step * step * fact)

        deriv = r_step * (values[..., idx + 1] - values[..., idx]) + \
            ((3.0 * b * b - 1) * sec_deriv[..., idx + 1] -
             (3.0 * a * a - 1) * sec_deriv[..., idx]) * (step * fact)

        return val, deriv, None, None
    def synthesize_speech_to_landmark(self):
        if(Speech2LandmarkModel=="Eskimez"):
            if __name__=="__main__":
                audio_path = "../bText2Speech/OUT/temp.wav"
            else:
                audio_path = "./bText2Speech/OUT/temp.wav"
            abs_path = os.path.abspath(os.path.dirname(__file__))
            if(os.path.isfile(audio_path)):
                with sess.as_default():
                    with sess.graph.as_default():
                        landmarks = generate_landmarks(audio_path,self.audio_landmark_model,abs_path)
            else:
                raise FileNotFoundError("Audio path not found, please use the pipeline correctly")
            return landmarks
        elif(Speech2LandmarkModel=="speechdriven"):                    
            if __name__=="__main__":
                audio_path = "../bText2Speech/OUT/temp.wav"
            else:
                audio_path = "./bText2Speech/OUT/temp.wav"
            self.fs, self.audio_clip = wav.read(os.path.join("bText2Speech","OUT","temp.wav"))
            self.audio_clip = self.audio_clip*1000
            self.audio_clip = self.audio_clip.astype(np.uint8)
            # Define the codec and create VideoWriter object
            Mouth = [[48, 49], [49, 50], [50, 51], [51, 52], [52, 53], [53, 54], [54, 55], [55, 56], [56, 57], \
                 [57, 58], [58, 59], [59, 48] ]
            Mouth2 = [[60, 61], [61, 62], [62, 63], [63, 64], [64, 65], [65, 66], \
                 [66, 67], [67, 60]]
            #   out = cv2.VideoWriter('output.mp4', -1, 20.0, (640,480))
            faces = []
            move = 0.5
            move_idx = 0
            size = 256
            fourcc = cv2.VideoWriter_fourcc(*'MP4V')
            save_temp_dir = os.path.join(os.path.dirname(root_file_path),'OUT/temp.mp4')
            save_dir = os.path.join(os.path.dirname(root_file_path),'OUT/out.mp4')
            out = cv2.VideoWriter(save_temp_dir,fourcc, 25.0, (size,size))
            """
            Randomly Choose one of the actors 
            """
            actor = np.random.choice(["2.avi"])
            cap = cv2.VideoCapture(os.path.join(os.path.dirname(root_file_path),"Eskimez","actor/{}".format(actor)))
            
            vid, aud = self.va(self.still_frame, self.audio_clip, fs=self.fs)
            count = 0
            for idx in range(vid.shape[0]):        
                image = np.array(vid[idx])
                image = np.swapaxes(image,0,2)
                image = np.swapaxes(image,0,1).astype(np.uint8)
                #image = cv2.cvtColor(image,cv2.COLOR_BGR2RGB)
                if(cap.isOpened()):
                    ret, face = cap.read()
                    face = cv2.cvtColor(face,cv2.COLOR_RGB2BGR)
                    if(count%2==0 and idx<vid.shape[0]-25):
                        face_rec = face_recognition.face_landmarks(image)[0]
                        item = face_rec
                        item2 = []
                        for rec in face_rec.values():
                            item2 += rec    
                        item2 = np.array(item2)
                    scale = 2.5
                    shiftx = 0
                    shifty = -40
                    #scale = 2.8
                    #shiftx = -10
                    #shifty = -60
                    #scale = 3
                    #shiftx = -20
                    #shifty = -80
                    count +=1

                    mouth_polygon = []
                    for mouth_item in Mouth:
                        keypoint = item2[mouth_item][0]
                        location = [int(keypoint[0]*scale+shiftx),int(keypoint[1]*scale+shifty)]
                        mouth_polygon.append(location)
                    mouth_polygon = np.array([mouth_polygon],dtype=np.int32)
                    if(idx<vid.shape[0]-25):
                        cv2.fillPoly(face,mouth_polygon,(0,0,0),lineType=cv2.LINE_AA)
                    else:
                        cv2.fillPoly(face,mouth_polygon,(40,40,255),lineType=cv2.LINE_AA)
                    mouth_polygon = []
                    for mouth_item in Mouth2:
                        keypoint = item2[mouth_item][0]
                        location = [int(keypoint[0]*scale+shiftx),int(keypoint[1]*scale+shifty)]
                        mouth_polygon.append(location)
                    mouth_polygon = np.array([mouth_polygon],dtype=np.int32)
                    if(idx<vid.shape[0]-25):
                        cv2.fillPoly(face,mouth_polygon,(0,0,0),lineType=cv2.LINE_AA)
                    else:
                        cv2.fillPoly(face,mouth_polygon,(40,40,255),lineType=cv2.LINE_AA)

                    mouth_polygon = []  
                    for keypoint in item['bottom_lip']:
                        #keypoint = item[mouth_item][0]
                        location = [int(keypoint[0]*scale+shiftx),int(keypoint[1]*scale+shifty)]
                        mouth_polygon.append(location)
                    mouth_polygon = np.array([mouth_polygon],dtype=np.int32)
                    cv2.fillPoly(face,mouth_polygon,(40,40,255),lineType=cv2.LINE_AA)
                    
                    mouth_polygon = []
                    for keypoint in item['top_lip']:
                        #keypoint = item[mouth_item][0]
                        location = [int(keypoint[0]*scale+shiftx),int(keypoint[1]*scale+shifty)]
                        mouth_polygon.append(location)
                    mouth_polygon = np.array([mouth_polygon],dtype=np.int32)
                    cv2.fillPoly(face,mouth_polygon,(40,40,255),lineType=cv2.LINE_AA)
                    

                    #cv2.imshow("hi",face)
                    #k = cv2.waitKey(0)
                    #if(k==ord('q')):
                    #    break
                    out.write(face)
                else:
                    print("actor loading failed")
                    break                    
                #face = cv2.cvtColor(face,cv2.COLOR_BGR2RGB)
            
            cap.release()
            out.release()    
            #cv2.destroyAllWindows()
            stabilize(save_temp_dir,save_dir)
Exemple #39
0
 def stat_fun_rm1wayANOVA(*args):
     return f_mway_rm(np.swapaxes(args, 1, 0),
                      factor_levels=[len(levellist)],
                      effects='A',
                      correction=True,
                      return_pvals=False)[0]
    def __getitem__(self, idx):

        img_path = self.list_path[idx]

        Batch_set = []
        surv_time_train = []
        status_train = []

        all_vgg = []

        vgg_clus = [[] for i in range(self.cluster_num)]

        Train_vgg_file = np.load(img_path)

        con_vgg, con_path, con_cluster = [], [], []

        mask = np.ones(self.cluster_num, dtype=np.float32)

        for i in range(1):  # How many wsi in the patient

            cur_vgg = Train_vgg_file['vgg_features']
            cur_patient = Train_vgg_file['pid']
            cur_time = Train_vgg_file['time']
            cur_status = Train_vgg_file['status']
            cur_path = Train_vgg_file['img_path']
            cur_cluster = Train_vgg_file['cluster_num']

            for id, each_patch_cls in enumerate(cur_cluster):
                vgg_clus[each_patch_cls].append(cur_vgg[id])

            Batch_set.append(
                (cur_vgg, cur_patient, cur_status, cur_time, cur_cluster))

            np_vgg_fea = []
            for i in range(self.cluster_num):
                if len(vgg_clus[i]) == 0:
                    clus_feat = np.zeros((1, 4096), dtype=np.float32)
                    mask[i] = 0
                else:
                    if self.random:
                        curr_feat = vgg_clus[i]
                        ind = np.arange(len(curr_feat))
                        np.random.shuffle(ind)
                        clus_feat = np.asarray([curr_feat[i] for i in ind])
                    else:
                        clus_feat = np.asarray(vgg_clus[i])
                clus_feat = np.swapaxes(clus_feat, 1, 0)
                # clus_feat = np.expand_dims(clus_feat, 0)
                clus_feat = np.expand_dims(clus_feat, 1)
                np_vgg_fea.append(clus_feat)

            all_vgg.append(np_vgg_fea)

        for each_set in Batch_set:
            surv_time_train.append(each_set[3])
            status_train.append(each_set[2])

        surv_time_train = np.asarray(surv_time_train)
        status_train = np.asarray(status_train)

        np_cls_num = np.asarray(cur_cluster)

        sample = {
            'feat': all_vgg[0],
            'mask': mask,
            'time': surv_time_train[0],
            'status': status_train[0],
            'cluster_num': np_cls_num
        }

        if self.transform:
            sample = self.transform(sample)

        return sample
def get_SM_series(time=800,
                  epsilon=.9,
                  gradient=.04,
                  gradienttype=2,
                  yBaseline=10000,
                  F=2,
                  filtipmax=20,
                  tokenstrength=1,
                  randfilextension=-1,
                  randfilretract=-1,
                  want_RGB=True,
                  NewVersion=True,
                  ymax=116):
    """
    Crucial Assumption: This counts all VEGFR activations in specific grid sites as discrete units!
    
    parameters
    ----------
    time: int
        How many time steps of simulation
    epsilon: float
        Guidance Parameter for msm model
    gradient: float
        Gradient Steepness parameter (Vconcst)
    gradienttype: int
        Gradient Type. 2 for normal, 5 for semi-uniform?
    yBaseline: float
        Baseline y parameter for gradient type 5.
    F: float
        Filopodia extension parameter. More = more filopodia extension. standard 2. 
    filtipmax: float
        parameter determining How long filopodia wait till retracting
    tokenStrength: float
        Some other parameter for msm that I don't know the details of.
        
    NOT COARSE GRAINED AT ALL?
    Outputs Mseries and Sseries, a flattened list of time series for the entire sensory map
    """
    if NewVersion:

        time_of_patterning = time
        world = springAgent.World(epsilon, gradient, gradienttype, F,
                                  filtipmax, tokenstrength, randfilextension,
                                  randfilretract)
        world.simulateTimestep()
        alternative_argmask = np.load('alternative_argmask.npy')

        #Gathering Time Series. Don't discretize here, so you can try different methods later.
        Mseries = [[] for _ in range(40)]
        Sseries = [[] for _ in range(40)]
        SseriesRGB = [[] for _ in range(40)]
        uniqueargs = []
        uniqueargsRGB = []
        undecided = True
        for t in range(time):
            if t < (100000000000 + 50):
                if t % 100 == 0: print t
                world.simulateTimestep()
                #     Sworldcube = pass
                Mworldcube = np.array(world.getGridMapOfFilopodiaMovement())
                Mworldcube = np.swapaxes(
                    Mworldcube[:, :, :, 0], 1,
                    2)  #This shouldnt need to happen in the future
                Msensorymap = np.array(
                    sensorymap_from_datacube(Mworldcube, zaxis=1, ymax=ymax))
                flattened_Msensorymap = Msensorymap.sum(
                    axis=0
                )  #Coarse Graining Space to lower dimensionality (cube to square)
                #try vectorizing all of that, below:
                Msensorymap_masked = np.array([
                    flattened_Msensorymap[alternative_argmask[i][0],
                                          alternative_argmask[i, 1]]
                    for i in range(len(alternative_argmask))
                ])
                for i in range(len(Mseries)):
                    Mseries[i].append(Msensorymap_masked[i])

                Sdata = world.getGridSiteData()
                Sworldcube = np.array(processdata(Sdata, ymax=ymax))
                if undecided:
                    if (np.abs(Sworldcube[:, :, :, -1].sum() -
                               Sworldcube[:, :, :, -5].sum()) > 600):
                        time_of_patterning = t
                        undecided = False
                #         if t==(time-1):

                #             return Sworldcube.astype(bool).astype(int)
                #             pass
                SworldcubeRGB = np.swapaxes(Sworldcube[:, :, :, 2], 1, 2)
                #         print SworldcubeRGB.shape
                Sworldcube = Sworldcube.astype(bool).astype(
                    int)  #Counting all activations as 1
                Sworldcube = np.swapaxes(
                    Sworldcube[:, :, :, 5], 1, 2) + np.swapaxes(
                        Sworldcube[:, :, :, -2], 1, 2
                    )  #both cells. Also deleting other data than activations

                summedcube = Sworldcube.sum(
                    axis=0
                )  #flatten it in order to see where to normalize later.
                summedcubeRGB = SworldcubeRGB.sum(axis=0)

                args = np.argwhere(
                    summedcube >= 30
                )  # normalize the membrane layer bits to focus on Filopodia

                argsRGB = np.argwhere(
                    summedcubeRGB >= np.max(summedcubeRGB) / 2)

                uniqueargs.append(np.unique(np.concatenate(np.array(args))))
                uniqueargsRGB.append(
                    np.unique(np.concatenate(np.array(argsRGB))))
                for i in range(len(args)):
                    Sworldcube[:, args[i][0], args[i][1]] = 0  # or 1/40
                for i in range(len(argsRGB)):
                    SworldcubeRGB[:, argsRGB[i][0], argsRGB[i][1]] = 0
                SsensorymapRGB = np.array(
                    sensorymap_from_datacube(SworldcubeRGB, zaxis=1,
                                             ymax=ymax))
                Ssensorymap = np.array(
                    sensorymap_from_datacube(Sworldcube, zaxis=1, ymax=ymax))

                flattened_Ssensorymap = Ssensorymap.sum(axis=0)
                flattened_SsensorymapRGB = SsensorymapRGB.sum(axis=0)

                Ssensorymap_masked = np.array([
                    flattened_Ssensorymap[alternative_argmask[i][0],
                                          alternative_argmask[i, 1]]
                    for i in range(len(alternative_argmask))
                ])
                Ssensorymap_maskedRGB = np.array([
                    flattened_SsensorymapRGB[alternative_argmask[i][0],
                                             alternative_argmask[i, 1]]
                    for i in range(len(alternative_argmask))
                ])
                for i in range(len(Sseries)):
                    Sseries[i].append(Ssensorymap_masked[i])
                for i in range(len(SseriesRGB)):
                    SseriesRGB[i].append(Ssensorymap_maskedRGB[i])
            else:
                break

    Mseries = np.array(Mseries)
    Sseries = np.array(Sseries)
    SseriesRGB = np.array(SseriesRGB)

    if want_RGB == True:
        return Mseries, Sseries, SseriesRGB
    else:
        return Mseries, Sseries
Exemple #42
0
 def quantile(self, q, **kwargs):
     return np.swapaxes(np.quantile(self, q, **kwargs), 0, -1)
def change_orientation(im_src, orientation, im_dst=None, inverse=False):
    """
    :return: an image with changed orientation
    :param im_src: source image
    :param orientation: orientation string (SCT "from" convention)
    :param im_dst: destination image (can be the source image for in-place
                   operation, can be unset to generate one)

    Notes:

    - the resulting image has no path member set
    - if the source image is < 3D, it is reshaped to 3D and the destination is 3D
    """

    if len(im_src.data.shape) < 3:
        pass # Will reshape to 3D
    elif len(im_src.data.shape) == 3:
        pass # OK, standard 3D volume
    elif len(im_src.data.shape) == 4:
        pass # OK, standard 4D volume
    elif len(im_src.data.shape) == 5 and im_src.header.get_intent()[0] == "vector":
        pass # OK, physical displacement field
    else:
        raise NotImplementedError("Don't know how to change orientation for this image")

    im_src_orientation = im_src.orientation
    im_dst_orientation = orientation
    if inverse:
        im_src_orientation, im_dst_orientation = im_dst_orientation, im_src_orientation


    perm, inversion = _get_permutations(im_src_orientation, im_dst_orientation)

    if im_dst is None:
        im_dst = im_src.copy()
        im_dst._path = None

    im_src_data = im_src.data
    if len(im_src_data.shape) < 3:
        im_src_data = im_src_data.reshape(tuple(list(im_src_data.shape) + ([1]*(3-len(im_src_data.shape)))))

    # Update data by performing inversions and swaps

    # axes inversion (flip)
    data = im_src_data[::inversion[0], ::inversion[1], ::inversion[2]]

    # axes manipulations (transpose)
    if perm == [1, 0, 2]:
        data = np.swapaxes(data, 0, 1)
    elif perm == [2, 1, 0]:
        data = np.swapaxes(data, 0, 2)
    elif perm == [0, 2, 1]:
        data = np.swapaxes(data, 1, 2)
    elif perm == [2, 0, 1]:
        data = np.swapaxes(data, 0, 2)  # transform [2, 0, 1] to [1, 0, 2]
        data = np.swapaxes(data, 0, 1)  # transform [1, 0, 2] to [0, 1, 2]
    elif perm == [1, 2, 0]:
        data = np.swapaxes(data, 0, 2)  # transform [1, 2, 0] to [0, 2, 1]
        data = np.swapaxes(data, 1, 2)  # transform [0, 2, 1] to [0, 1, 2]
    elif perm == [0, 1, 2]:
        # do nothing
        pass
    else:
        raise NotImplementedError()


    # Update header

    im_src_aff = im_src.hdr.get_best_affine()
    aff = nibabel.orientations.inv_ornt_aff(
     np.array((perm, inversion)).T,
     im_src_data.shape)
    im_dst_aff = np.matmul(im_src_aff, aff)

    im_dst.header.set_qform(im_dst_aff)
    im_dst.header.set_sform(im_dst_aff)
    im_dst.header.set_data_shape(data.shape)
    im_dst.data = data

    return im_dst
Exemple #44
0
 def rot90_3d(self, m, k=1, axis=2):
     """Rotate an array by 90 degrees in the counter-clockwise direction around the given axis"""
     m = np.swapaxes(m, 2, axis)
     m = np.rot90(m, k)
     m = np.swapaxes(m, 2, axis)
     return m
Exemple #45
0
    def sample(self):

        if self.tr_cond == 'all_gains':
            G = (1.0 / self.stim_dur) * np.random.choice(
                [1.0], size=(self.n_loc, self.batch_size))
            #            GG = G
            G = np.repeat(G, self.n_in, axis=0).T
            G = np.tile(G, (self.stim_dur, 1, 1))
            G = np.swapaxes(G, 0, 1)
        else:
            G = (0.5 / self.stim_dur) * np.random.choice(
                [1.0], size=(1, self.batch_size))
            G = np.repeat(G, self.n_in * self.n_loc, axis=0).T
            G = np.tile(G, (self.stim_dur, 1, 1))
            G = np.swapaxes(G, 0, 1)

        delay_durs = np.random.choice([10, 40, 70, 100],
                                      size=(self.batch_size, ))
        S1 = np.pi * np.random.rand(self.n_loc, self.batch_size)
        S = S1.T.copy()
        S1 = np.repeat(S1, self.n_in, axis=0).T
        S1 = np.tile(S1, (self.stim_dur, 1, 1))
        S1 = np.swapaxes(S1, 0, 1)

        # Noisy responses
        L1 = G * np.exp(self.kappa * (np.cos(2.0 * (S1 - np.tile(
            self.phi,
            (self.batch_size, self.stim_dur, self.n_loc)))) - 1.0))  # stim
        Ld = (self.spon_rate / self.max_delay) * np.ones(
            (self.batch_size, self.max_delay, self.nneuron))  # delay
        Lr = (self.spon_rate / self.max_delay) * np.ones(
            (self.batch_size, self.resp_dur, self.nneuron))  # resp

        R1 = np.random.poisson(L1)
        Rd = np.random.poisson(Ld)
        Rr = np.random.poisson(Lr)

        example_input = np.concatenate((R1, Rd, Rr), axis=1)
        example_output = np.repeat(S[:, np.newaxis, :], self.total_dur, axis=1)
        example_mask = np.ones((self.batch_size, self.total_dur),
                               dtype=np.bool)
        for i in range(self.batch_size):
            example_mask[i, (self.stim_dur +
                             delay_durs[i]):(self.stim_dur +
                                             self.max_delay)] = False
            example_input[i, (self.stim_dur +
                              delay_durs[i]):(self.stim_dur +
                                              self.max_delay), :] = 0.0
            example_output[i, (self.stim_dur +
                               delay_durs[i]):(self.stim_dur +
                                               self.max_delay), :] = 0.0

        cum_R1 = np.sum(R1, axis=1)
        mu_x = np.asarray([
            np.arctan2(
                np.dot(cum_R1[:, i * self.n_in:(i + 1) * self.n_in],
                       np.sin(2.0 * self.phi)),
                np.dot(cum_R1[:, i * self.n_in:(i + 1) * self.n_in],
                       np.cos(2.0 * self.phi))) for i in range(self.n_loc)
        ]) / 2.0
        mu_x = (mu_x > 0.0) * mu_x + (mu_x < 0.0) * (mu_x + np.pi)
        mu_x = mu_x.T
        mu_x = np.repeat(mu_x[:, np.newaxis, :], self.total_dur, axis=1)

        return delay_durs, example_input, example_output, example_mask, S, mu_x
    def class_gradient(self,
                       x: np.ndarray,
                       label: Optional[Union[int, List[int]]] = None,
                       training_mode: bool = False,
                       **kwargs) -> np.ndarray:
        """
        Compute per-class derivatives w.r.t. `x`.

        :param x: Sample input with shape as expected by the model.
        :param label: Index of a specific per-class derivative. If an integer is provided, the gradient of that class
                      output is computed for all samples. If multiple values are provided, the first dimension should
                      match the batch size of `x`, and each value will be used as target for its corresponding sample in
                      `x`. If `None`, then gradients for all classes will be computed for each sample.
        :param training_mode: `True` for model set to training mode and `'False` for model set to evaluation mode.
        :return: Array of gradients of input features w.r.t. each class in the form
                 `(batch_size, nb_classes, input_shape)` when computing for all classes, otherwise shape becomes
                 `(batch_size, 1, input_shape)` when `label` parameter is specified.
        """
        # Check value of label for computing gradients
        if not (label is None or (isinstance(label, (int, np.integer))
                                  and label in range(self.nb_classes)) or
                (isinstance(label, np.ndarray) and len(label.shape) == 1 and
                 (label < self.nb_classes).all()
                 and label.shape[0] == x.shape[0])):
            raise ValueError("Label %s is out of range." % str(label))

        # Check shape of preprocessed `x` because of custom function for `_class_gradients`
        x_preprocessed, _ = self._apply_preprocessing(x, y=None, fit=False)
        shape_match = [
            i is None or i == j
            for i, j in zip(self._input_shape, x_preprocessed.shape[1:])
        ]
        if not all(shape_match):
            raise ValueError(
                "Error when checking x: expected preprocessed x to have shape {} but got array with shape {}"
                .format(self._input_shape, x_preprocessed.shape[1:]))

        self._init_class_gradients(label=label)

        if label is None:
            # Compute the gradients w.r.t. all classes
            gradients = np.swapaxes(
                np.array(self._class_gradients([x_preprocessed])), 0, 1)

        elif isinstance(label, (int, np.integer)):
            # Compute the gradients only w.r.t. the provided label
            gradients = np.swapaxes(np.array(self._class_gradients_idx[label](
                [x_preprocessed, int(training_mode)])),
                                    axis1=0,
                                    axis2=1)  # type: ignore
            assert gradients.shape == (x_preprocessed.shape[0],
                                       1) + x_preprocessed.shape[1:]

        else:
            # For each sample, compute the gradients w.r.t. the indicated target class (possibly distinct)
            unique_label = list(np.unique(label))
            gradients = np.array([
                self._class_gradients_idx[l](
                    [x_preprocessed, int(training_mode)]) for l in unique_label
            ])
            gradients = np.swapaxes(np.squeeze(gradients, axis=1), 0, 1)
            lst = [unique_label.index(i) for i in label]
            gradients = np.expand_dims(gradients[np.arange(len(gradients)),
                                                 lst],
                                       axis=1)

        gradients = self._apply_preprocessing_gradient(x, gradients)

        return gradients
Exemple #47
0
    def sample(self):
        if self.tr_cond == 'all_gains':
            G = (1.0 / self.stim_dur) * np.random.choice(
                [1.0], size=(self.n_loc, self.batch_size))
            G = np.repeat(G, self.n_in, axis=0).T
            G = np.tile(G, (self.stim_dur, 1, 1))
            G = np.swapaxes(G, 0, 1)
        else:
            G = (0.5 / self.stim_dur) * np.random.choice(
                [1.0], size=(1, self.batch_size))
            G = np.repeat(G, self.n_in * self.n_loc, axis=0).T
            G = np.tile(G, (self.stim_dur, 1, 1))
            G = np.swapaxes(G, 0, 1)

        H = (1.0 / self.resp_dur) * np.ones(
            (self.batch_size, self.resp_dur, self.nneuron))

        delay_durs = np.random.choice([10, 40, 70, 100],
                                      size=(self.batch_size, ))

        # Stimuli
        S1 = 80.0 * np.random.rand(self.n_loc,
                                   self.batch_size) - 40.0  # first stimulus
        S2 = 80.0 * np.random.rand(self.n_loc,
                                   self.batch_size) - 40.0  # second stimulus

        # Larger/smaller indicator
        C = (S1 > S2).flatten() + 0.0

        S1 = np.repeat(S1, self.n_in, axis=0).T
        S1 = np.tile(S1, (self.stim_dur, 1, 1))
        S1 = np.swapaxes(S1, 0, 1)

        S2 = np.repeat(S2, self.n_in, axis=0).T
        S2 = np.tile(S2, (self.resp_dur, 1, 1))
        S2 = np.swapaxes(S2, 0, 1)

        # Noisy responses
        L1 = G * np.exp(
            -(0.5 / self.sig_tc**2) *
            (S1 - np.tile(self.phi,
                          (self.batch_size, self.stim_dur, self.n_loc)))**2)
        L2 = H * np.exp(
            -(0.5 / self.sig_tc**2) *
            (S2 - np.tile(self.phi,
                          (self.batch_size, self.resp_dur, self.n_loc)))**2)
        Ld = (self.spon_rate / self.max_delay) * np.ones(
            (self.batch_size, self.max_delay, self.nneuron))  # delay

        R1 = np.random.poisson(L1)
        R2 = np.random.poisson(L2)
        Rd = np.random.poisson(Ld)

        example_input = np.concatenate((R1, Rd, R2), axis=1)
        example_output = np.repeat(C[:, np.newaxis], self.total_dur, axis=1)
        example_output = np.repeat(example_output[:, :, np.newaxis], 1, axis=2)
        example_mask = np.ones((self.batch_size, self.total_dur),
                               dtype=np.bool)
        for i in range(self.batch_size):
            example_mask[i, (self.stim_dur +
                             delay_durs[i]):(self.stim_dur +
                                             self.max_delay)] = False
            example_input[i, (self.stim_dur +
                              delay_durs[i]):(self.stim_dur +
                                              self.max_delay), :] = 0.0
            example_output[i, (self.stim_dur +
                               delay_durs[i]):(self.stim_dur +
                                               self.max_delay), :] = 0.0

        cum_R1 = np.sum(R1, axis=1)
        cum_R2 = np.sum(R2, axis=1)

        mu_x = np.dot(cum_R1, self.phi) / np.sum(cum_R1, axis=1)
        mu_y = np.dot(cum_R2, self.phi) / np.sum(cum_R2, axis=1)

        v_x = self.sig_tc**2 / np.sum(cum_R1, axis=1)
        v_y = self.sig_tc**2 / np.sum(cum_R2, axis=1)

        if self.n_loc == 1:
            d = scistat.norm.cdf(0.0, mu_y - mu_x, np.sqrt(v_x + v_y))
        else:
            d = scistat.norm.cdf(0.0, mu_y - mu_x, np.sqrt(v_x + v_y))
        P = d
        return delay_durs, example_input, example_output, example_mask, C, P
Exemple #48
0
    def sample(self):

        if self.tr_cond == 'all_gains':
            G = (1.0 / self.stim_dur) * np.random.choice(
                [1.0], size=(self.n_loc, self.batch_size))
            G = np.repeat(G, self.n_in, axis=0).T
            G = np.tile(G, (self.stim_dur, 1, 1))
            G = np.swapaxes(G, 0, 1)
        else:
            G = (0.5 / self.stim_dur) * np.random.choice(
                [1.0], size=(1, self.batch_size))
            G = np.repeat(G, self.n_in * self.n_loc, axis=0).T
            G = np.tile(G, (self.stim_dur, 1, 1))
            G = np.swapaxes(G, 0, 1)

        C = np.random.choice([0.0, 1.0], size=(self.batch_size, ))
        C0ind = np.where(C == 0.0)[0]  # change
        C1ind = np.where(C == 1.0)[0]  # change

        S1 = np.pi * np.random.rand(self.n_loc, self.batch_size)
        Sboth = S1.T.copy()
        S = np.expand_dims(Sboth[:, 0], axis=1)
        S[C1ind, 0] = Sboth[C1ind, 1]

        S1 = np.repeat(S1, self.n_in, axis=0).T
        S1 = np.tile(S1, (self.stim_dur, 1, 1))
        S1 = np.swapaxes(S1, 0, 1)

        # Noisy responses
        L1 = G * np.exp(self.kappa * (np.cos(2.0 * (S1 - np.tile(
            self.phi,
            (self.batch_size, self.stim_dur, self.n_loc)))) - 1.0))  # stim
        Ld = (self.spon_rate / self.delay_dur) * np.ones(
            (self.batch_size, self.delay_dur, self.nneuron))  # delay
        Lr = (self.spon_rate / self.resp_dur) * np.ones(
            (self.batch_size, self.resp_dur, self.nneuron))
        Lr[C0ind, :, :self.n_in] = 5.0 * Lr[C0ind, :, :self.n_in]  # cue 0
        Lr[C1ind, :, self.n_in:] = 5.0 * Lr[C1ind, :, self.n_in:]  # cue 1

        R1 = np.random.poisson(L1)
        Rd = np.random.poisson(Ld)
        Rr = np.random.poisson(Lr)

        example_input = np.concatenate((R1, Rd, Rr), axis=1)
        example_output = np.repeat(S[:, np.newaxis, :], self.total_dur, axis=1)

        cum_R1 = np.sum(R1, axis=1)
        mu_x = np.asarray([
            np.arctan2(
                np.dot(cum_R1[:, i * self.n_in:(i + 1) * self.n_in],
                       np.sin(2.0 * self.phi)),
                np.dot(cum_R1[:, i * self.n_in:(i + 1) * self.n_in],
                       np.cos(2.0 * self.phi))) for i in range(self.n_loc)
        ]) / 2.0
        mu_x = (mu_x > 0.0) * mu_x + (mu_x < 0.0) * (mu_x + np.pi)
        mu_x = mu_x.T
        # mu_x           = np.repeat(mu_x[:,np.newaxis,:],self.total_dur,axis=1)
        mu_aux = np.expand_dims(mu_x[:, 0], axis=1)
        mu_aux[C1ind, 0] = mu_x[C1ind, 1]

        return example_input, example_output, S, mu_aux
Exemple #49
0
rng = np.random.RandomState(23455)
dbfolder = './panoptic_cleaned/'

# dbNames = ('data_panoptic_haggling_all',
#                  'data_panoptic_haggling_buyers',
#                  'data_panoptic_haggling_sellers',
#                  'data_panoptic_haggling_losers',
#                  'data_panoptic_haggling_winners'
#                   )
#dbName = 'data_panoptic_haggling_sellers'
dbName = 'data_panoptic_haggling_buyers'
rawdata = np.load(dbfolder + dbName+ '.npz') #(frames, 240, 73)

X = rawdata['clips']
X = np.swapaxes(X, 1, 2).astype(np.float32) #(17944, 73, 240)
X_stdd = (X - preprocess['Xmean']) / preprocess['Xstd']


batchsize = 1
window = X.shape[2]

# 2021
# 1
#3283
#with torch.no_grad():
for _ in range(100):
    index = rng.randint(X.shape[0])
    print(index)
    Xorgi = X[index:index+1,:,:]
    Xorgi_stdd = X_stdd[index:index+1,:,:]  #Input (batchSize,73,240) 
Exemple #50
0
    def sample(self):
        if self.tr_cond == 'all_gains':
            G = (1.0 / self.stim_dur) * np.random.choice(
                [1.0], size=(self.n_loc, self.batch_size))
            G = np.repeat(G, self.n_in, axis=0).T
            G = np.tile(G, (self.stim_dur, 1, 1))
            G = np.swapaxes(G, 0, 1)
        else:
            G = (0.5 / self.stim_dur) * np.random.choice(
                [1.0], size=(1, self.batch_size))
            G = np.repeat(G, self.n_in * self.n_loc, axis=0).T
            G = np.tile(G, (self.stim_dur, 1, 1))
            G = np.swapaxes(G, 0, 1)

        H = (1.0 / self.resp_dur) * np.ones(
            (self.batch_size, self.resp_dur, self.nneuron))

        # Target presence/absence and stimuli
        C = np.random.choice([0.0, 1.0], size=(self.batch_size, ))
        C1ind = np.where(C == 1.0)[0]  # change

        delay_durs = np.random.choice([10, 40, 70, 100],
                                      size=(self.batch_size, ))
        S1 = np.pi * np.random.rand(self.n_loc, self.batch_size)
        S2 = S1.copy()
        S1 = np.repeat(S1, self.n_in, axis=0).T
        S1 = np.tile(S1, (self.stim_dur, 1, 1))
        S1 = np.swapaxes(S1, 0, 1)

        S2[np.random.randint(0, self.n_loc, size=(len(C1ind), )),
           C1ind] = np.pi * np.random.rand(len(C1ind))
        S2 = np.repeat(S2, self.n_in, axis=0).T
        S2 = np.tile(S2, (self.resp_dur, 1, 1))
        S2 = np.swapaxes(S2, 0, 1)

        # Noisy responses
        L1 = G * np.exp(self.kappa * (np.cos(2.0 * (S1 - np.tile(
            self.phi,
            (self.batch_size, self.stim_dur, self.n_loc)))) - 1.0))  # stim 1
        L2 = H * np.exp(self.kappa * (np.cos(2.0 * (S2 - np.tile(
            self.phi,
            (self.batch_size, self.resp_dur, self.n_loc)))) - 1.0))  # stim 2
        Ld = (self.spon_rate / self.max_delay) * np.ones(
            (self.batch_size, self.max_delay, self.nneuron))  # delay

        R1 = np.random.poisson(L1)
        R2 = np.random.poisson(L2)
        Rd = np.random.poisson(Ld)

        example_input = np.concatenate((R1, Rd, R2), axis=1)
        example_output = np.repeat(C[:, np.newaxis], self.total_dur, axis=1)
        example_output = np.repeat(example_output[:, :, np.newaxis], 1, axis=2)
        example_mask = np.ones((self.batch_size, self.total_dur),
                               dtype=np.bool)
        for i in range(self.batch_size):
            example_mask[i, (self.stim_dur +
                             delay_durs[i]):(self.stim_dur +
                                             self.max_delay)] = False
            example_input[i, (self.stim_dur +
                              delay_durs[i]):(self.stim_dur +
                                              self.max_delay), :] = 0.0
            example_output[i, (self.stim_dur +
                               delay_durs[i]):(self.stim_dur +
                                               self.max_delay), :] = 0.0

        cum_R1 = np.sum(R1, axis=1)
        cum_R2 = np.sum(R2, axis=1)

        mu_x = np.asarray([
            np.arctan2(
                np.dot(cum_R1[:, i * self.n_in:(i + 1) * self.n_in],
                       np.sin(2.0 * self.phi)),
                np.dot(cum_R1[:, i * self.n_in:(i + 1) * self.n_in],
                       np.cos(2.0 * self.phi))) for i in range(self.n_loc)
        ])
        mu_y = np.asarray([
            np.arctan2(
                np.dot(cum_R2[:, i * self.n_in:(i + 1) * self.n_in],
                       np.sin(2.0 * self.phi)),
                np.dot(cum_R2[:, i * self.n_in:(i + 1) * self.n_in],
                       np.cos(2.0 * self.phi))) for i in range(self.n_loc)
        ])

        temp_x = np.asarray([
            np.swapaxes(np.multiply.outer(cum_R1, cum_R1), 1, 2)[i, i, :, :]
            for i in range(self.batch_size)
        ])
        temp_y = np.asarray([
            np.swapaxes(np.multiply.outer(cum_R2, cum_R2), 1, 2)[i, i, :, :]
            for i in range(self.batch_size)
        ])

        kappa_x = np.asarray([
            np.sqrt(
                np.sum(temp_x[:, i * self.n_in:(i + 1) * self.n_in,
                              i * self.n_in:(i + 1) * self.n_in] *
                       np.repeat(np.cos(
                           np.subtract(
                               np.expand_dims(self.phi, axis=1),
                               np.expand_dims(self.phi,
                                              axis=1).T))[np.newaxis, :, :],
                                 self.batch_size,
                                 axis=0),
                       axis=(1, 2))) for i in range(self.n_loc)
        ])
        kappa_y = np.asarray([
            np.sqrt(
                np.sum(temp_y[:, i * self.n_in:(i + 1) * self.n_in,
                              i * self.n_in:(i + 1) * self.n_in] *
                       np.repeat(np.cos(
                           np.subtract(
                               np.expand_dims(self.phi, axis=1),
                               np.expand_dims(self.phi,
                                              axis=1).T))[np.newaxis, :, :],
                                 self.batch_size,
                                 axis=0),
                       axis=(1, 2))) for i in range(self.n_loc)
        ])

        if self.n_loc == 1:
            d = np.i0(kappa_x) * np.i0(kappa_y) / np.i0(
                np.sqrt(kappa_x**2 + kappa_y**2 +
                        2.0 * kappa_x * kappa_y * np.cos(mu_y - mu_x)))
        else:
            d = np.nanmean(np.i0(kappa_x) * np.i0(kappa_y) / np.i0(
                np.sqrt(kappa_x**2 + kappa_y**2 +
                        2.0 * kappa_x * kappa_y * np.cos(mu_y - mu_x))),
                           axis=0)

        P = d / (d + 1.0)
        return delay_durs, example_input, example_output, example_mask, C, P
def test_mobilebert_model_small_cfg(compute_layout):
    cfg = MobileBertModel.get_cfg()
    cfg.defrost()
    cfg.MODEL.vocab_size = 100
    cfg.MODEL.num_layers = 2
    cfg.MODEL.hidden_size = 128
    cfg.MODEL.num_heads = 2
    cfg.MODEL.compute_layout = compute_layout
    cfg.freeze()

    # Generate TN layout
    cfg_tn = cfg.clone()
    cfg_tn.defrost()
    cfg_tn.MODEL.layout = 'TN'
    cfg_tn.freeze()

    batch_size = 4
    sequence_length = 16
    num_mask = 3
    inputs = mx.np.random.randint(0, 10, (batch_size, sequence_length))
    token_types = mx.np.random.randint(0, 2, (batch_size, sequence_length))
    valid_length = mx.np.random.randint(3, sequence_length, (batch_size, ))
    masked_positions = mx.np.random.randint(0, 3, (batch_size, num_mask))

    mobile_bert_model = MobileBertModel.from_cfg(cfg)
    mobile_bert_model.initialize()
    mobile_bert_model.hybridize()
    mobile_bert_model_tn = MobileBertModel.from_cfg(cfg_tn)
    mobile_bert_model_tn.share_parameters(mobile_bert_model.collect_params())
    mobile_bert_model_tn.hybridize()
    contextual_embedding, pooled_out = mobile_bert_model(
        inputs, token_types, valid_length)
    contextual_embedding_tn, pooled_out_tn = mobile_bert_model_tn(
        inputs.T, token_types.T, valid_length)
    assert_allclose(contextual_embedding.asnumpy(),
                    np.swapaxes(contextual_embedding_tn.asnumpy(), 0, 1), 1E-4,
                    1E-4)
    assert_allclose(pooled_out.asnumpy(), pooled_out_tn.asnumpy(), 1E-4, 1E-4)

    # Test for MobileBertForMLM
    mobile_bert_mlm_model = MobileBertForMLM(cfg)
    mobile_bert_mlm_model.initialize()
    mobile_bert_mlm_model.hybridize()
    mobile_bert_mlm_model_tn = MobileBertForMLM(cfg_tn)
    mobile_bert_mlm_model_tn.share_parameters(
        mobile_bert_mlm_model.collect_params())
    mobile_bert_model_tn.hybridize()
    contextual_embedding, pooled_out, mlm_scores = mobile_bert_mlm_model(
        inputs, token_types, valid_length, masked_positions)
    contextual_embedding_tn, pooled_out_tn, mlm_scores_tn =\
        mobile_bert_mlm_model_tn(inputs.T, token_types.T, valid_length, masked_positions)
    assert_allclose(contextual_embedding.asnumpy(),
                    np.swapaxes(contextual_embedding_tn.asnumpy(), 0, 1), 1E-4,
                    1E-4)
    assert_allclose(pooled_out_tn.asnumpy(), pooled_out.asnumpy(), 1E-4, 1E-4)
    assert_allclose(mlm_scores_tn.asnumpy(), mlm_scores.asnumpy(), 1E-4, 1E-4)

    # Test for MobileBertForPretrain
    mobile_bert_pretrain_model = MobileBertForPretrain(cfg)
    mobile_bert_pretrain_model.initialize()
    mobile_bert_pretrain_model.hybridize()
    mobile_bert_pretrain_model_tn = MobileBertForPretrain(cfg_tn)
    mobile_bert_pretrain_model_tn.share_parameters(
        mobile_bert_pretrain_model.collect_params())
    mobile_bert_pretrain_model_tn.hybridize()
    contextual_embedding, pooled_out, nsp_score, mlm_scores =\
        mobile_bert_pretrain_model(inputs, token_types, valid_length, masked_positions)
    contextual_embedding_tn, pooled_out_tn, nsp_score_tn, mlm_scores_tn = \
        mobile_bert_pretrain_model_tn(inputs.T, token_types.T, valid_length, masked_positions)
    assert_allclose(contextual_embedding.asnumpy(),
                    np.swapaxes(contextual_embedding_tn.asnumpy(), 0, 1), 1E-4,
                    1E-4)
    assert_allclose(pooled_out.asnumpy(), pooled_out_tn.asnumpy(), 1E-4, 1E-4)
    assert_allclose(nsp_score.asnumpy(), nsp_score_tn.asnumpy(), 1E-4, 1E-4)
    assert_allclose(mlm_scores.asnumpy(), mlm_scores_tn.asnumpy(), 1E-4, 1E-4)
Exemple #52
0
    def sample(self):

        if self.tr_cond == 'all_gains':
            G1 = (1.0 / self.stim1_dur) * np.random.choice(
                [1.0], size=(self.n_loc, self.batch_size))
            G1 = np.repeat(G1, self.n_in, axis=0).T
            G1 = np.tile(G1, (self.stim1_dur, 1, 1))
            G1 = np.swapaxes(G1, 0, 1)

            G2 = (1.0 / self.stim2_dur) * np.random.choice(
                [1.0], size=(self.n_loc, self.batch_size))
            G2 = np.repeat(G2, self.n_in, axis=0).T
            G2 = np.tile(G2, (self.stim2_dur, 1, 1))
            G2 = np.swapaxes(G2, 0, 1)
        else:
            G1 = (0.5 / self.stim1_dur) * np.random.choice(
                [1.0], size=(self.n_loc, self.batch_size))
            G1 = np.repeat(G1, self.n_in, axis=0).T
            G1 = np.tile(G1, (self.stim1_dur, 1, 1))
            G1 = np.swapaxes(G1, 0, 1)

            G2 = (0.5 / self.stim2_dur) * np.random.choice(
                [1.0], size=(self.n_loc, self.batch_size))
            G2 = np.repeat(G2, self.n_in, axis=0).T
            G2 = np.tile(G2, (self.stim2_dur, 1, 1))
            G2 = np.swapaxes(G2, 0, 1)

        C = np.random.choice([0.0, 1.0], size=(self.batch_size, ))
        C0ind = np.where(C == 0.0)[0]  # change
        C1ind = np.where(C == 1.0)[0]  # change

        S1 = np.pi * np.random.rand(self.n_loc, self.batch_size)
        S2 = np.pi * np.random.rand(self.n_loc, self.batch_size)
        S = S1.T.copy()
        S[C1ind, 0] = S2.T[C1ind, 0]

        S1 = np.repeat(S1, self.n_in, axis=0).T
        S1 = np.tile(S1, (self.stim1_dur, 1, 1))
        S1 = np.swapaxes(S1, 0, 1)

        S2 = np.repeat(S2, self.n_in, axis=0).T
        S2 = np.tile(S2, (self.stim2_dur, 1, 1))
        S2 = np.swapaxes(S2, 0, 1)

        # Noisy responses
        L1 = G1 * np.exp(self.kappa * (np.cos(2.0 * (S1 - np.tile(
            self.phi,
            (self.batch_size, self.stim1_dur, self.n_loc)))) - 1.0))  # stim 1
        Ld = (self.spon_rate / self.delay_dur) * np.ones(
            (self.batch_size, self.delay_dur, self.nneuron))  # delay
        L2 = G2 * np.exp(self.kappa * (np.cos(2.0 * (S2 - np.tile(
            self.phi,
            (self.batch_size, self.stim2_dur, self.n_loc)))) - 1.0))  # stim 2

        Lk = (self.spon_rate / self.keep_dur) * np.ones(
            (self.batch_size, self.keep_dur, self.nneuron))  # delay
        Lr = (self.spon_rate / self.resp_dur) * np.ones(
            (self.batch_size, self.resp_dur, self.nneuron))
        Lr[C0ind, :, ::2] = 5.0 * Lr[C0ind, :, ::2]  # cue 0
        Lr[C1ind, :, 1::2] = 5.0 * Lr[C1ind, :, 1::2]  # cue 1

        R1 = np.random.poisson(L1)
        Rd = np.random.poisson(Ld)
        R2 = np.random.poisson(L2)
        Rk = np.random.poisson(Lk)
        Rr = np.random.poisson(Lr)

        example_input = np.concatenate((R1, Rd, R2, Rk, Rr), axis=1)
        example_output = np.repeat(S[:, np.newaxis, :], self.total_dur, axis=1)

        cum_R1 = np.sum(R1, axis=1)
        mu1_x = np.asarray([
            np.arctan2(
                np.dot(cum_R1[:, i * self.n_in:(i + 1) * self.n_in],
                       np.sin(2.0 * self.phi)),
                np.dot(cum_R1[:, i * self.n_in:(i + 1) * self.n_in],
                       np.cos(2.0 * self.phi))) for i in range(self.n_loc)
        ]) / 2.0
        mu1_x = (mu1_x > 0.0) * mu1_x + (mu1_x < 0.0) * (mu1_x + np.pi)
        mu1_x = mu1_x.T

        cum_R2 = np.sum(R2, axis=1)
        mu2_x = np.asarray([
            np.arctan2(
                np.dot(cum_R2[:, i * self.n_in:(i + 1) * self.n_in],
                       np.sin(2.0 * self.phi)),
                np.dot(cum_R2[:, i * self.n_in:(i + 1) * self.n_in],
                       np.cos(2.0 * self.phi))) for i in range(self.n_loc)
        ]) / 2.0
        mu2_x = (mu2_x > 0.0) * mu2_x + (mu2_x < 0.0) * (mu2_x + np.pi)
        mu2_x = mu2_x.T
        # mu_x           = np.repeat(mu_x[:,np.newaxis,:],self.total_dur,axis=1)

        mu1_x[C1ind, 0] = mu2_x[C1ind, 0]

        return example_input, example_output, S, mu1_x
class TestColorconv(TestCase):

    img_rgb = data.colorwheel()
    img_grayscale = data.camera()
    img_rgba = np.array([[[0, 0.5, 1, 0], [0, 0.5, 1, 1],
                          [0, 0.5, 1, 0.5]]]).astype(np.float)

    colbars = np.array([[1, 1, 0, 0, 1, 1, 0, 0], [1, 1, 1, 1, 0, 0, 0, 0],
                        [1, 0, 1, 0, 1, 0, 1, 0]]).astype(np.float)
    colbars_array = np.swapaxes(colbars.reshape(3, 4, 2), 0, 2)
    colbars_point75 = colbars * 0.75
    colbars_point75_array = np.swapaxes(colbars_point75.reshape(3, 4, 2), 0, 2)

    xyz_array = np.array([
        [[0.4124, 0.21260, 0.01930]],  # red
        [[0, 0, 0]],  # black
        [[.9505, 1., 1.089]],  # white
        [[.1805, .0722, .9505]],  # blue
        [[.07719, .15438, .02573]],  # green
    ])
    lab_array = np.array([
        [[53.233, 80.109, 67.220]],  # red
        [[0., 0., 0.]],  # black
        [[100.0, 0.005, -0.010]],  # white
        [[32.303, 79.197, -107.864]],  # blue
        [[46.229, -51.7, 49.898]],  # green
    ])

    luv_array = np.array([
        [[53.233, 175.053, 37.751]],  # red
        [[0., 0., 0.]],  # black
        [[100., 0.001, -0.017]],  # white
        [[32.303, -9.400, -130.358]],  # blue
        [[46.228, -43.774, 56.589]],  # green
    ])

    # RGBA to RGB
    def test_rgba2rgb_conversion(self):
        rgba = self.img_rgba
        rgb = rgba2rgb(rgba)
        expected = np.array([[[1, 1, 1], [0, 0.5, 1], [0.5, 0.75,
                                                       1]]]).astype(np.float)
        self.assertEqual(rgb.shape, expected.shape)
        assert_almost_equal(rgb, expected)

    def test_rgba2rgb_error_grayscale(self):
        self.assertRaises(ValueError, rgba2rgb, self.img_grayscale)

    def test_rgba2rgb_error_rgb(self):
        self.assertRaises(ValueError, rgba2rgb, self.img_rgb)

    def test_rgba2rgb_dtype(self):
        rgba = self.img_rgba.astype('float64')
        rgba32 = img_as_float32(rgba)

        assert rgba2rgb(rgba).dtype == rgba.dtype
        assert rgba2rgb(rgba32).dtype == rgba32.dtype

    # RGB to HSV
    def test_rgb2hsv_conversion(self):
        rgb = img_as_float(self.img_rgb)[::16, ::16]
        hsv = rgb2hsv(rgb).reshape(-1, 3)
        # ground truth from colorsys
        gt = np.array([
            colorsys.rgb_to_hsv(pt[0], pt[1], pt[2])
            for pt in rgb.reshape(-1, 3)
        ])
        assert_almost_equal(hsv, gt)

    def test_rgb2hsv_error_grayscale(self):
        self.assertRaises(ValueError, rgb2hsv, self.img_grayscale)

    def test_rgb2hsv_error_one_element(self):
        self.assertRaises(ValueError, rgb2hsv, self.img_rgb[0, 0])

    def test_rgb2hsv_dtype(self):
        rgb = img_as_float(self.img_rgb)
        rgb32 = img_as_float32(self.img_rgb)

        assert rgb2hsv(rgb).dtype == rgb.dtype
        assert rgb2hsv(rgb32).dtype == rgb32.dtype

    # HSV to RGB
    def test_hsv2rgb_conversion(self):
        rgb = self.img_rgb.astype("float32")[::16, ::16]
        # create HSV image with colorsys
        hsv = np.array([
            colorsys.rgb_to_hsv(pt[0], pt[1], pt[2])
            for pt in rgb.reshape(-1, 3)
        ]).reshape(rgb.shape)
        # convert back to RGB and compare with original.
        # relative precision for RGB -> HSV roundtrip is about 1e-6
        assert_almost_equal(rgb, hsv2rgb(hsv), decimal=4)

    def test_hsv2rgb_error_grayscale(self):
        self.assertRaises(ValueError, hsv2rgb, self.img_grayscale)

    def test_hsv2rgb_error_one_element(self):
        self.assertRaises(ValueError, hsv2rgb, self.img_rgb[0, 0])

    def test_hsv2rgb_dtype(self):
        rgb = self.img_rgb.astype("float32")[::16, ::16]
        # create HSV image with colorsys
        hsv = np.array([
            colorsys.rgb_to_hsv(pt[0], pt[1], pt[2])
            for pt in rgb.reshape(-1, 3)
        ],
                       dtype='float64').reshape(rgb.shape)
        hsv32 = hsv.astype('float32')

        assert hsv2rgb(hsv).dtype == hsv.dtype
        assert hsv2rgb(hsv32).dtype == hsv32.dtype

    # RGB to XYZ
    def test_rgb2xyz_conversion(self):
        gt = np.array([[[0.950456, 1., 1.088754],
                        [0.538003, 0.787329, 1.06942],
                        [0.592876, 0.28484, 0.969561],
                        [0.180423, 0.072169, 0.950227]],
                       [[0.770033, 0.927831, 0.138527],
                        [0.35758, 0.71516, 0.119193],
                        [0.412453, 0.212671, 0.019334], [0., 0., 0.]]])
        assert_almost_equal(rgb2xyz(self.colbars_array), gt)

    # stop repeating the "raises" checks for all other functions that are
    # implemented with color._convert()
    def test_rgb2xyz_error_grayscale(self):
        self.assertRaises(ValueError, rgb2xyz, self.img_grayscale)

    def test_rgb2xyz_error_one_element(self):
        self.assertRaises(ValueError, rgb2xyz, self.img_rgb[0, 0])

    def test_rgb2xyz_dtype(self):
        img = self.colbars_array
        img32 = img.astype('float32')

        assert rgb2xyz(img).dtype == img.dtype
        assert rgb2xyz(img32).dtype == img32.dtype

    # XYZ to RGB
    def test_xyz2rgb_conversion(self):
        assert_almost_equal(xyz2rgb(rgb2xyz(self.colbars_array)),
                            self.colbars_array)

    def test_xyz2rgb_dtype(self):
        img = rgb2xyz(self.colbars_array)
        img32 = img.astype('float32')

        assert xyz2rgb(img).dtype == img.dtype
        assert xyz2rgb(img32).dtype == img32.dtype

    # RGB<->XYZ roundtrip on another image
    def test_xyz_rgb_roundtrip(self):
        img_rgb = img_as_float(self.img_rgb)
        assert_array_almost_equal(xyz2rgb(rgb2xyz(img_rgb)), img_rgb)

    # RGB<->HED roundtrip with ubyte image
    def test_hed_rgb_roundtrip(self):
        img_rgb = img_as_ubyte(self.img_rgb)
        new = img_as_ubyte(hed2rgb(rgb2hed(img_rgb)))
        assert_equal(new, img_rgb)

    # RGB<->HED roundtrip with float image
    def test_hed_rgb_float_roundtrip(self):
        img_rgb = img_as_float(self.img_rgb)
        assert_array_almost_equal(hed2rgb(rgb2hed(img_rgb)), img_rgb)

    # RGB<->HDX roundtrip with ubyte image
    def test_hdx_rgb_roundtrip(self):
        from skimage.color.colorconv import hdx_from_rgb, rgb_from_hdx
        img_rgb = self.img_rgb
        conv = combine_stains(separate_stains(img_rgb, hdx_from_rgb),
                              rgb_from_hdx)
        assert_equal(img_as_ubyte(conv), img_rgb)

    # RGB<->HDX roundtrip with float image
    def test_hdx_rgb_roundtrip_float(self):
        from skimage.color.colorconv import hdx_from_rgb, rgb_from_hdx
        img_rgb = img_as_float(self.img_rgb)
        conv = combine_stains(separate_stains(img_rgb, hdx_from_rgb),
                              rgb_from_hdx)
        assert_array_almost_equal(conv, img_rgb)

    # RGB to RGB CIE
    def test_rgb2rgbcie_conversion(self):
        gt = np.array([[[0.1488856, 0.18288098, 0.19277574],
                        [0.01163224, 0.16649536, 0.18948516],
                        [0.12259182, 0.03308008, 0.17298223],
                        [-0.01466154, 0.01669446, 0.16969164]],
                       [[0.16354714, 0.16618652, 0.0230841],
                        [0.02629378, 0.1498009, 0.01979351],
                        [0.13725336, 0.01638562, 0.00329059], [0., 0., 0.]]])
        assert_almost_equal(rgb2rgbcie(self.colbars_array), gt)

    def test_rgb2rgbcie_dtype(self):
        img = self.colbars_array.astype('float64')
        img32 = img.astype('float32')

        assert rgb2rgbcie(img).dtype == img.dtype
        assert rgb2rgbcie(img32).dtype == img32.dtype

    # RGB CIE to RGB
    def test_rgbcie2rgb_conversion(self):
        # only roundtrip test, we checked rgb2rgbcie above already
        assert_almost_equal(rgbcie2rgb(rgb2rgbcie(self.colbars_array)),
                            self.colbars_array)

    def test_rgbcie2rgb_dtype(self):
        img = rgb2rgbcie(self.colbars_array).astype('float64')
        img32 = img.astype('float32')

        assert rgbcie2rgb(img).dtype == img.dtype
        assert rgbcie2rgb(img32).dtype == img32.dtype

    def test_convert_colorspace(self):
        colspaces = ['HSV', 'RGB CIE', 'XYZ', 'YCbCr', 'YPbPr', 'YDbDr']
        colfuncs_from = [
            hsv2rgb, rgbcie2rgb, xyz2rgb, ycbcr2rgb, ypbpr2rgb, ydbdr2rgb
        ]
        colfuncs_to = [
            rgb2hsv, rgb2rgbcie, rgb2xyz, rgb2ycbcr, rgb2ypbpr, rgb2ydbdr
        ]

        assert_almost_equal(
            convert_colorspace(self.colbars_array, 'RGB', 'RGB'),
            self.colbars_array)

        for i, space in enumerate(colspaces):
            gt = colfuncs_from[i](self.colbars_array)
            assert_almost_equal(
                convert_colorspace(self.colbars_array, space, 'RGB'), gt)
            gt = colfuncs_to[i](self.colbars_array)
            assert_almost_equal(
                convert_colorspace(self.colbars_array, 'RGB', space), gt)

        self.assertRaises(ValueError, convert_colorspace, self.colbars_array,
                          'nokey', 'XYZ')
        self.assertRaises(ValueError, convert_colorspace, self.colbars_array,
                          'RGB', 'nokey')

    def test_rgb2gray(self):
        x = np.array([1, 1, 1]).reshape((1, 1, 3)).astype(np.float)
        g = rgb2gray(x)
        assert_array_almost_equal(g, 1)

        assert_equal(g.shape, (1, 1))

    def test_rgb2gray_contiguous(self):
        x = np.random.rand(10, 10, 3)
        assert rgb2gray(x).flags["C_CONTIGUOUS"]
        assert rgb2gray(x[:5, :5]).flags["C_CONTIGUOUS"]

    def test_rgb2gray_alpha(self):
        x = np.random.rand(10, 10, 4)
        with expected_warnings(['Non RGB image conversion']):
            assert rgb2gray(x).ndim == 2

    def test_rgb2gray_on_gray(self):
        rgb2gray(np.random.rand(5, 5))

    def test_rgb2gray_dtype(self):
        img = np.random.rand(10, 10, 3).astype('float64')
        img32 = img.astype('float32')

        assert rgb2gray(img).dtype == img.dtype
        assert rgb2gray(img32).dtype == img32.dtype

    # test matrices for xyz2lab and lab2xyz generated using
    # http://www.easyrgb.com/index.php?X=CALC
    # Note: easyrgb website displays xyz*100
    def test_xyz2lab(self):
        assert_array_almost_equal(xyz2lab(self.xyz_array),
                                  self.lab_array,
                                  decimal=3)

        # Test the conversion with the rest of the illuminants.
        for I in ["d50", "d55", "d65", "d75"]:
            for obs in ["2", "10"]:
                fname = "color/tests/data/lab_array_{0}_{1}.npy".format(I, obs)
                lab_array_I_obs = np.load(fetch(fname))
                assert_array_almost_equal(lab_array_I_obs,
                                          xyz2lab(self.xyz_array, I, obs),
                                          decimal=2)
        for I in ["a", "e"]:
            fname = "color/tests/data/lab_array_{0}_2.npy".format(I)
            lab_array_I_obs = np.load(fetch(fname))
            assert_array_almost_equal(lab_array_I_obs,
                                      xyz2lab(self.xyz_array, I, "2"),
                                      decimal=2)

    def test_xyz2lab_dtype(self):
        img = self.xyz_array.astype('float64')
        img32 = img.astype('float32')

        assert xyz2lab(img).dtype == img.dtype
        assert xyz2lab(img32).dtype == img32.dtype

    def test_lab2xyz(self):
        assert_array_almost_equal(lab2xyz(self.lab_array),
                                  self.xyz_array,
                                  decimal=3)

        # Test the conversion with the rest of the illuminants.
        for I in ["d50", "d55", "d65", "d75"]:
            for obs in ["2", "10"]:
                fname = "color/tests/data/lab_array_{0}_{1}.npy".format(I, obs)
                lab_array_I_obs = np.load(fetch(fname))
                assert_array_almost_equal(lab2xyz(lab_array_I_obs, I, obs),
                                          self.xyz_array,
                                          decimal=3)
        for I in ["a", "e"]:
            fname = "lab_array_{0}_2.npy".format(I, obs)
            lab_array_I_obs = np.load(fetch('color/tests/data/' + fname))
            assert_array_almost_equal(lab2xyz(lab_array_I_obs, I, "2"),
                                      self.xyz_array,
                                      decimal=3)

        # And we include a call to test the exception handling in the code.
        try:
            xs = lab2xyz(lab_array_I_obs, "NaI", "2")  # Not an illuminant
        except ValueError:
            pass

        try:
            xs = lab2xyz(lab_array_I_obs, "d50", "42")  # Not a degree
        except ValueError:
            pass

    def test_lab2xyz_dtype(self):
        img = self.lab_array.astype('float64')
        img32 = img.astype('float32')

        assert lab2xyz(img).dtype == img.dtype
        assert lab2xyz(img32).dtype == img32.dtype

    def test_rgb2lab_brucelindbloom(self):
        """
        Test the RGB->Lab conversion by comparing to the calculator on the
        authoritative Bruce Lindbloom
        [website](http://brucelindbloom.com/index.html?ColorCalculator.html).
        """
        # Obtained with D65 white point, sRGB model and gamma
        gt_for_colbars = np.array([[100, 0, 0], [97.1393, -21.5537, 94.4780],
                                   [91.1132, -48.0875, -14.1312],
                                   [87.7347, -86.1827, 83.1793],
                                   [60.3242, 98.2343, -60.8249],
                                   [53.2408, 80.0925, 67.2032],
                                   [32.2970, 79.1875, -107.8602], [0, 0, 0]]).T
        gt_array = np.swapaxes(gt_for_colbars.reshape(3, 4, 2), 0, 2)
        assert_array_almost_equal(rgb2lab(self.colbars_array),
                                  gt_array,
                                  decimal=2)

    def test_lab_rgb_roundtrip(self):
        img_rgb = img_as_float(self.img_rgb)
        assert_array_almost_equal(lab2rgb(rgb2lab(img_rgb)), img_rgb)

    def test_rgb2lab_dtype(self):
        img = self.colbars_array.astype('float64')
        img32 = img.astype('float32')

        assert rgb2lab(img).dtype == img.dtype
        assert rgb2lab(img32).dtype == img32.dtype

    def test_lab2rgb_dtype(self):
        img = self.lab_array.astype('float64')
        img32 = img.astype('float32')

        assert lab2rgb(img).dtype == img.dtype
        assert lab2rgb(img32).dtype == img32.dtype

    # test matrices for xyz2luv and luv2xyz generated using
    # http://www.easyrgb.com/index.php?X=CALC
    # Note: easyrgb website displays xyz*100
    def test_xyz2luv(self):
        assert_array_almost_equal(xyz2luv(self.xyz_array),
                                  self.luv_array,
                                  decimal=3)

        # Test the conversion with the rest of the illuminants.
        for I in ["d50", "d55", "d65", "d75"]:
            for obs in ["2", "10"]:
                fname = "color/tests/data/luv_array_{0}_{1}.npy".format(I, obs)
                luv_array_I_obs = np.load(fetch(fname))
                assert_array_almost_equal(luv_array_I_obs,
                                          xyz2luv(self.xyz_array, I, obs),
                                          decimal=2)
        for I in ["a", "e"]:
            fname = "color/tests/data/luv_array_{0}_2.npy".format(I)
            luv_array_I_obs = np.load(fetch(fname))
            assert_array_almost_equal(luv_array_I_obs,
                                      xyz2luv(self.xyz_array, I, "2"),
                                      decimal=2)

    def test_xyz2luv_dtype(self):
        img = self.xyz_array.astype('float64')
        img32 = img.astype('float32')

        assert xyz2luv(img).dtype == img.dtype
        assert xyz2luv(img32).dtype == img32.dtype

    def test_luv2xyz(self):
        assert_array_almost_equal(luv2xyz(self.luv_array),
                                  self.xyz_array,
                                  decimal=3)

        # Test the conversion with the rest of the illuminants.
        for I in ["d50", "d55", "d65", "d75"]:
            for obs in ["2", "10"]:
                fname = "color/tests/data/luv_array_{0}_{1}.npy".format(I, obs)
                luv_array_I_obs = np.load(fetch(fname))
                assert_array_almost_equal(luv2xyz(luv_array_I_obs, I, obs),
                                          self.xyz_array,
                                          decimal=3)
        for I in ["a", "e"]:
            fname = "color/tests/data/luv_array_{0}_2.npy".format(I, obs)
            luv_array_I_obs = np.load(fetch(fname))
            assert_array_almost_equal(luv2xyz(luv_array_I_obs, I, "2"),
                                      self.xyz_array,
                                      decimal=3)

    def test_luv2xyz_dtype(self):
        img = self.luv_array.astype('float64')
        img32 = img.astype('float32')

        assert luv2xyz(img).dtype == img.dtype
        assert luv2xyz(img32).dtype == img32.dtype

    def test_rgb2luv_brucelindbloom(self):
        """
        Test the RGB->Lab conversion by comparing to the calculator on the
        authoritative Bruce Lindbloom
        [website](http://brucelindbloom.com/index.html?ColorCalculator.html).
        """
        # Obtained with D65 white point, sRGB model and gamma
        gt_for_colbars = np.array([[100, 0, 0], [97.1393, 7.7056, 106.7866],
                                   [91.1132, -70.4773, -15.2042],
                                   [87.7347, -83.0776, 107.3985],
                                   [60.3242, 84.0714, -108.6834],
                                   [53.2408, 175.0151, 37.7564],
                                   [32.2970, -9.4054, -130.3423], [0, 0, 0]]).T
        gt_array = np.swapaxes(gt_for_colbars.reshape(3, 4, 2), 0, 2)
        assert_array_almost_equal(rgb2luv(self.colbars_array),
                                  gt_array,
                                  decimal=2)

    def test_rgb2luv_dtype(self):
        img = self.colbars_array.astype('float64')
        img32 = img.astype('float32')

        assert rgb2luv(img).dtype == img.dtype
        assert rgb2luv(img32).dtype == img32.dtype

    def test_luv2rgb_dtype(self):
        img = self.luv_array.astype('float64')
        img32 = img.astype('float32')

        assert luv2rgb(img).dtype == img.dtype
        assert luv2rgb(img32).dtype == img32.dtype

    def test_luv_rgb_roundtrip(self):
        img_rgb = img_as_float(self.img_rgb)
        assert_array_almost_equal(luv2rgb(rgb2luv(img_rgb)), img_rgb)

    def test_lab_rgb_outlier(self):
        lab_array = np.ones((3, 1, 3))
        lab_array[0] = [50, -12, 85]
        lab_array[1] = [50, 12, -85]
        lab_array[2] = [90, -4, -47]
        rgb_array = np.array([
            [[0.501, 0.481, 0]],
            [[0, 0.482, 1.]],
            [[0.578, 0.914, 1.]],
        ])
        assert_almost_equal(lab2rgb(lab_array), rgb_array, decimal=3)

    def test_lab_full_gamut(self):
        a, b = np.meshgrid(np.arange(-100, 100), np.arange(-100, 100))
        L = np.ones(a.shape)
        lab = np.dstack((L, a, b))
        for value in [0, 10, 20]:
            lab[:, :, 0] = value
            with expected_warnings(['Color data out of range']):
                lab2xyz(lab)

    def test_lab_lch_roundtrip(self):
        rgb = img_as_float(self.img_rgb)
        lab = rgb2lab(rgb)
        lab2 = lch2lab(lab2lch(lab))
        assert_array_almost_equal(lab2, lab)

    def test_rgb_lch_roundtrip(self):
        rgb = img_as_float(self.img_rgb)
        lab = rgb2lab(rgb)
        lch = lab2lch(lab)
        lab2 = lch2lab(lch)
        rgb2 = lab2rgb(lab2)
        assert_array_almost_equal(rgb, rgb2)

    def test_lab_lch_0d(self):
        lab0 = self._get_lab0()
        lch0 = lab2lch(lab0)
        lch2 = lab2lch(lab0[None, None, :])
        assert_array_almost_equal(lch0, lch2[0, 0, :])

    def test_lab_lch_1d(self):
        lab0 = self._get_lab0()
        lch0 = lab2lch(lab0)
        lch1 = lab2lch(lab0[None, :])
        assert_array_almost_equal(lch0, lch1[0, :])

    def test_lab_lch_3d(self):
        lab0 = self._get_lab0()
        lch0 = lab2lch(lab0)
        lch3 = lab2lch(lab0[None, None, None, :])
        assert_array_almost_equal(lch0, lch3[0, 0, 0, :])

    def _get_lab0(self):
        rgb = img_as_float(self.img_rgb[:1, :1, :])
        return rgb2lab(rgb)[0, 0, :]

    def test_yuv(self):
        rgb = np.array([[[1.0, 1.0, 1.0]]])
        assert_array_almost_equal(rgb2yuv(rgb), np.array([[[1, 0, 0]]]))
        assert_array_almost_equal(rgb2yiq(rgb), np.array([[[1, 0, 0]]]))
        assert_array_almost_equal(rgb2ypbpr(rgb), np.array([[[1, 0, 0]]]))
        assert_array_almost_equal(rgb2ycbcr(rgb), np.array([[[235, 128,
                                                              128]]]))
        assert_array_almost_equal(rgb2ydbdr(rgb), np.array([[[1, 0, 0]]]))
        rgb = np.array([[[0.0, 1.0, 0.0]]])
        assert_array_almost_equal(
            rgb2yuv(rgb), np.array([[[0.587, -0.28886916, -0.51496512]]]))
        assert_array_almost_equal(
            rgb2yiq(rgb), np.array([[[0.587, -0.27455667, -0.52273617]]]))
        assert_array_almost_equal(rgb2ypbpr(rgb),
                                  np.array([[[0.587, -0.331264, -0.418688]]]))
        assert_array_almost_equal(rgb2ycbcr(rgb),
                                  np.array([[[144.553, 53.797, 34.214]]]))
        assert_array_almost_equal(rgb2ydbdr(rgb),
                                  np.array([[[0.587, -0.883, 1.116]]]))

    def test_yuv_roundtrip(self):
        img_rgb = img_as_float(self.img_rgb)[::16, ::16]
        assert_array_almost_equal(yuv2rgb(rgb2yuv(img_rgb)), img_rgb)
        assert_array_almost_equal(yiq2rgb(rgb2yiq(img_rgb)), img_rgb)
        assert_array_almost_equal(ypbpr2rgb(rgb2ypbpr(img_rgb)), img_rgb)
        assert_array_almost_equal(ycbcr2rgb(rgb2ycbcr(img_rgb)), img_rgb)
        assert_array_almost_equal(ydbdr2rgb(rgb2ydbdr(img_rgb)), img_rgb)

    def test_rgb2yuv_dtype(self):
        img = self.colbars_array.astype('float64')
        img32 = img.astype('float32')

        assert rgb2yuv(img).dtype == img.dtype
        assert rgb2yuv(img32).dtype == img32.dtype

    def test_yuv2rgb_dtype(self):
        img = rgb2yuv(self.colbars_array).astype('float64')
        img32 = img.astype('float32')

        assert yuv2rgb(img).dtype == img.dtype
        assert yuv2rgb(img32).dtype == img32.dtype

    def test_rgb2yiq_conversion(self):
        rgb = img_as_float(self.img_rgb)[::16, ::16]
        yiq = rgb2yiq(rgb).reshape(-1, 3)
        gt = np.array([
            colorsys.rgb_to_yiq(pt[0], pt[1], pt[2])
            for pt in rgb.reshape(-1, 3)
        ])
        assert_almost_equal(yiq, gt, decimal=2)
def grid_diff(c, b, a, d_arr, val=3):
    #m = np.swapaxes(d_arr.values[c,:, b-val:b+val+1, a-val:a+val+1], 0, -1)
    m = np.swapaxes(d_arr[c,:, b-val:b+val+1, a-val:a+val+1], 0, -1)
    return m-np.nanmean(m, axis=(0,1))
Exemple #55
0
def remove_outlier_cuda(arr, dif, size=3, axis=0):
    """
    Remove high intensity bright spots from a 3D array along axis 0
    dimension using GPU.

    Parameters
    ----------
    arr : ndarray
        Input array.
    dif : float
        Expected difference value between outlier value and
        the median value of the array.
    size : int
        Size of the median filter.
    axis : int, optional
        Axis along which outlier removal is performed.

    Returns
    -------
    ndarray
       Corrected array.

    Example
    -------
    >>> import tomocuda
    >>> tomocuda.remove_outlier_cuda(arr, dif, 5)

    For more information regarding install and using tomocuda, check
    https://github.com/kyuepublic/tomocuda for more information

    """

    arr = dtype.as_float32(arr)
    dif = np.float32(dif)

    try:
        import tomocuda

        winAllow = range(2, 16)

        if (axis != 0):
            arr = np.swapaxes(arr, 0, axis)

        if size in winAllow:
            prjsize = arr.shape[0]
            loffset = int(size / 2)
            roffset = int((size - 1) / 2)
            imsizex = arr.shape[2]
            imsizey = arr.shape[1]

            filter = tomocuda.mFilter(imsizex, imsizey, prjsize, size)
            out = np.zeros(shape=(prjsize, imsizey, imsizex), dtype=np.float32)

            for step in range(prjsize):
                im_noisecu = arr[step].astype(np.float32)
                im_noisecu = np.lib.pad(im_noisecu, ((loffset, roffset),
                                                     (loffset, roffset)),
                                        'symmetric')
                im_noisecu = im_noisecu.flatten()

                filter.setCuImage(im_noisecu)
                filter.run2DRemoveOutliner(size, dif)
                results = filter.retreive()
                results = results.reshape(imsizey, imsizex)
                out[step] = results

            if (axis != 0):
                out = np.swapaxes(out, 0, axis)
        else:
            warnings.warn("Window size not support, using cpu outlier removal")
            out = remove_outlier(arr, dif, size)

    except ImportError:
        warnings.warn("The tomocuda is not support, using cpu outlier removal")
        out = remove_outlier(arr, dif, size)

    return out
def upsampleswplbls(seg, lbls):
    segx = seg.shape[1]
    segy = seg.shape[2]
    segz = seg.shape[0]

    lblsx = lbls.shape[1]
    lblsy = lbls.shape[2]
    lblsz = lbls.shape[0]

    if segx != lblsx:

        if segx == lblsy:

            print('Swapping x-y')
            reslbls = np.swapaxes(lbls, 1, 2)

        else:

            if segx > lblsx:

                print('Upsampling labels to clarity resolution')

            else:

                print('Downsampling labels to voxelized clarity resolution')

            rx = float(segx) / lblsx
            ry = float(segy) / lblsy
            rz = float(segz) / lblsz

            reslbls = sp.ndimage.zoom(lbls, (rz, rx, ry), order=0)

            print('Segmentation shape:', seg.shape)

            print('Resampled labels shape:', reslbls.shape)

            resx = reslbls.shape[1]

            if segx != resx:
                print('Swapping x-y')
                reslbls = np.swapaxes(reslbls, 1, 2)

    else:

        if segz != lblsz:

            if segx > lblsx:

                print('Upsampling labels to clarity resolution')

            else:

                print('Downsampling labels to voxelized clarity resolution')

            rx = float(segx) / lblsx
            ry = float(segy) / lblsy
            rz = float(segz) / lblsz

            reslbls = sp.ndimage.zoom(lbls, (rz, rx, ry), order=0)

        else:

            reslbls = lbls

    return reslbls
Exemple #57
0
    def fit(self, X, y=None):
        """
        Parameters
        ----------
        X : ndarray, shape (n_trials,  n_samples, n_channels)
            Training data, already filtered.
        y : ndarray, shape (n_trials,) | None, optional
            labels corresponding to each trial, not used (mentioned for sklearn comp)

        Returns
        -------
        self : RASR instance.
            the fitted RASR estimator.
        """
        X = check_array(X, allow_nd=True)
        shapeX = X.shape
        if len(shapeX) == 3:
            # concatenate all epochs
            Nt, Ns, Ne = shapeX  # 3D array (not fully sklearn-compatible). First dim should always be trials.
        else:
            raise ValueError(
                "X.shape should be (n_trials, n_samples, n_electrodes).")

        assert Ne < Ns, "number of samples should be higher than number of electrodes, check than \n" \
                        + "X.shape is (n_trials,  n_samples, n_channels)."

        if shapeX[0] < 100:
            raise ValueError(
                "Training requires at least 100 of trials to fit.")

        self.Ne_ = Ne  # save attribute for testing

        epochs = X.copy()
        epochs = check_array(epochs, allow_nd=True)

        # estimate covariances matrices
        covmats = covariances(
            np.swapaxes(epochs, 1, 2),
            estimator=self.estimator)  # (n_trials, n_channels, n_times)
        covmats = check_array(covmats, allow_nd=True)

        # geometric median
        # NOTE: while the term geometric median is used, it is NOT riemannian median but euclidian median, i.e.
        # it might be suboptimal for Symmetric Positive Definite matrices.

        logger.debug("geometric median")
        # covmean = mean_covariance(covmats, metric=self.metric_mean)
        covmean = np.reshape(
            geometric_median(
                np.reshape(
                    covmats,
                    (covmats.shape[0], covmats.shape[1] * covmats.shape[2]))),
            (covmats.shape[1], covmats.shape[2]))

        self.mixing_ = sqrtm(covmean)  # estimate matrix matrix

        # TODO: implement both manifold-aware PCA (rASR) and standard PCA (ASR)
        evals, evecs = eigh(self.mixing_)  # compute PCA
        indx = np.argsort(evals)  # sort in ascending
        evecs = evecs[:, indx]
        epochs = np.tensordot(epochs, evecs,
                              axes=(2, 0))  # apply PCA to epochs

        # RMS on sliding window
        rms_sliding = _rms(epochs)

        dist_params = np.zeros(
            (Ne,
             4))  # mu, sig, alpha, beta parameters of estimated distribution

        #TODO: use joblib to parrallelize this loop (code bottleneck)
        for c in range(Ne):
            dist_params[c, :] = _fit_eeg_distribution(
                rms_sliding[:, c], **self.args_eeg_distribution)
        self.threshold_ = np.diag(dist_params[:, 0] + self.rejection_cutoff *
                                  dist_params[:, 1]).dot(np.transpose(evecs))

        logger.debug("rASR calibrated")

        return self
    def __call__(self, x, y=None, quality=None):
        """
        Apply JPEG compression to sample `x`.

        :param x: Sample to compress with shape `(batch_size, width, height, depth)`. `x` values are expected to be in
               the data range [0, 1].
        :type x: `np.ndarray`
        :param y: Labels of the sample `x`. This function does not affect them in any way.
        :type y: `np.ndarray`
        :param quality: The image quality, on a scale from 1 (worst) to 95 (best). Values above 95 should be avoided.
        :type quality: `int`
        :return: compressed sample.
        :rtype: `np.ndarray`
        """
        clip_values = (0, 1)

        if quality is not None:
            self.set_params(quality=quality)

        assert self.channel_index < len(x.shape)

        # Swap channel index
        if self.channel_index < 3:
            x_ = np.swapaxes(x, self.channel_index, 3)
        else:
            x_ = x.copy()

        # Convert into `uint8`
        x_ = x_ * 255
        x_ = x_.astype("uint8")

        # Convert to 'L' mode
        if x_.shape[-1] == 1:
            x_ = np.reshape(x_, x_.shape[:-1])

        # Compress one image per time
        for i, xi in enumerate(x_):
            if len(xi.shape) == 2:
                xi = Image.fromarray(xi, mode='L')
            elif xi.shape[-1] == 3:
                xi = Image.fromarray(xi, mode='RGB')
            else:
                logger.log(level=40,
                           msg="Currently only support `RGB` and `L` images.")
                raise NotImplementedError(
                    "Currently only support `RGB` and `L` images.")

            out = BytesIO()
            xi.save(out, format="jpeg", quality=self.quality)
            xi = Image.open(out)
            xi = np.array(xi)
            x_[i] = xi
            del out

        # Expand dim if black/white images
        if len(x_.shape) < 4:
            x_ = np.expand_dims(x_, 3)

        # Convert to old dtype
        x_ = x_ / 255.0
        x_ = x_.astype(NUMPY_DTYPE)

        # Swap channel index
        if self.channel_index < 3:
            x_ = np.swapaxes(x_, self.channel_index, 3)

        x_ = np.clip(x_, clip_values[0], clip_values[1])

        return x_
 def _get_split_unnormalized(self, db_name, split):
     data = np.load(os.path.join(self.splits_path[split], db_name))['clips']
     data = np.swapaxes(data, 1, 2)
     data = self.remove_foot_contact_info(data)
     return data
Exemple #60
0
def median_filter_cuda(arr, size=3, axis=0):
    """
    Apply median filter to 3D array along 0 axis with GPU support.
    The winAllow is for A6000, Tian X support 3 to 8

    Parameters
    ----------
    arr : ndarray
        Input array.
    size : int, optional
        The size of the filter.
    axis : int, optional
        Axis along which median filtering is performed.

    Returns
    -------
    ndarray
        Median filtered 3D array.

    Example
    -------
    import tomocuda
    tomocuda.remove_outlier_cuda(arr, dif, 5)

    For more information regarding install and using tomocuda, check
    https://github.com/kyuepublic/tomocuda for more information
    """

    try:
        import tomocuda

        winAllow = range(2, 16)

        if (axis != 0):
            arr = np.swapaxes(arr, 0, axis)

        if size in winAllow:
            loffset = int(size / 2)
            roffset = int((size - 1) / 2)
            prjsize = arr.shape[0]
            imsizex = arr.shape[2]
            imsizey = arr.shape[1]

            filter = tomocuda.mFilter(imsizex, imsizey, prjsize, size)
            out = np.zeros(shape=(prjsize, imsizey, imsizex), dtype=np.float32)

            for step in range(prjsize):
                # im_noisecu = arr[:][step][:].astype(np.float32)
                im_noisecu = arr[step].astype(np.float32)
                im_noisecu = np.lib.pad(im_noisecu, ((loffset, roffset),
                                                     (loffset, roffset)),
                                        'symmetric')
                im_noisecu = im_noisecu.flatten()

                filter.setCuImage(im_noisecu)
                filter.run2DFilter(size)
                results = filter.retreive()
                results = results.reshape(imsizey, imsizex)
                out[step] = results

            if (axis != 0):
                out = np.swapaxes(out, 0, axis)
        else:
            warnings.warn("Window size not support, using cpu median filter")
            out = median_filter(arr, size, axis)

    except ImportError:
        warnings.warn("The tomocuda is not support, using cpu median filter")
        out = median_filter(arr, size, axis)

    return out