Beispiel #1
0
def merge_cells(angle_indices):
    def connection(index_a, index_b):
        if (index_a == 0 or index_a == 2) and (index_b == 0 or index_b == 2):
            angle_difference = min((index_a - index_b) % 180, (index_b - index_a) % 180)
            return abs(angle_difference) <= 0
        else:
            return False

    grid_size = len(angle_indices)
    num_grid_elements = grid_size * grid_size

    graph = np.zeros((num_grid_elements, num_grid_elements))
    for i in range(grid_size):
        for j in range(grid_size):
            index = np.ravel_multi_index((i, j), (grid_size, grid_size))

            if i > 0 and connection(angle_indices[i, j], angle_indices[i - 1, j]):
                graph[index, np.ravel_multi_index((i - 1, j), (grid_size, grid_size))] = 1

            if i < (grid_size - 1) and connection(angle_indices[i, j], angle_indices[i + 1, j]):
                graph[index, np.ravel_multi_index((i + 1, j), (grid_size, grid_size))] = 1

            if j > 0 and connection(angle_indices[i, j], angle_indices[i, j - 1]):
                graph[index, np.ravel_multi_index((i, j - 1), (grid_size, grid_size))] = 1

            if j < (grid_size - 1) and connection(angle_indices[i, j], angle_indices[i, j + 1]):
                graph[index, np.ravel_multi_index((i, j + 1), (grid_size, grid_size))] = 1

    n_components, labels = connected_components(graph, directed=False, return_labels=True)
    return (labels + 1).reshape(grid_size, grid_size)
Beispiel #2
0
def generateGridAdj(nrows, ncols):
    Adj = np.zeros((nrows * ncols,nrows * ncols))
    
    
    for i in range(ncols):
        for j in range(nrows):
            k = np.ravel_multi_index((i,j), dims=(ncols, nrows), order='F')
            if i > 0:
                ii = i-1
                jj = j
                kk = np.ravel_multi_index((ii,jj), dims=(ncols, nrows), order='F')
                Adj[k,kk] = 1
                        
            if i<ncols-1:
                ii=i+1
                jj=j
                kk = np.ravel_multi_index((ii,jj), dims=(ncols, nrows), order='F')
                Adj[k,kk] = 1
            
            if j>0:
                ii=i
                jj=j-1
                kk = np.ravel_multi_index((ii,jj), dims=(ncols, nrows), order='F')
                Adj[k,kk] = 1
            if j<nrows-1:
                ii=i
                jj=j+1
                kk = np.ravel_multi_index((ii,jj), dims=(ncols, nrows), order='F')
                Adj[k,kk] = 1
        
    return Adj
def create_sparse_pre_rate_matrix(state_space_shape, row, col, rate):
    """
    Create the pre-rate matrix with empty diagonal.

    """
    # check conformability of input arrays
    ndim = len(state_space_shape)
    assert_equal(len(row.shape), 2)
    assert_equal(len(col.shape), 2)
    assert_equal(len(rate.shape), 1)
    assert_equal(row.shape[0], rate.shape[0])
    assert_equal(col.shape[0], rate.shape[0])
    assert_equal(row.shape[1], ndim)
    assert_equal(col.shape[1], ndim)

    # Define the transition rate matrix after the multivariate process
    # is collapsed to a univariate process.
    nstates = np.prod(state_space_shape)
    mrow = np.ravel_multi_index(row.T, state_space_shape)
    mcol = np.ravel_multi_index(col.T, state_space_shape)

    # Diagonal entries are allowed for computing dwell times,
    # but they are not allowed for transition expectations.
    #assert_(not np.any(mrow == mcol))

    # create the sparse pre_Q matrix from the sparse arrays
    return coo_matrix((rate, (mrow, mcol)), (nstates, nstates))
Beispiel #4
0
def reverse_interpolate_two_array(value1, array1, value2, array2, delta1=0.1, delta2=0.1):
    """
    Tries to reverse interpolate two vales from two arrays with the same dimensions, and finds a common index
    for value1 and value2 in their respective arrays. the deltas define the search radius for a close value match
    to the arrays.

    :return: index1, index2
    """
    tth_ind = np.argwhere(np.abs(array1 - value1) < delta1)
    azi_ind = np.argwhere(np.abs(array2 - value2) < delta2)

    tth_ind_ravel = np.ravel_multi_index((tth_ind[:, 0], tth_ind[:, 1]), dims=array1.shape)
    azi_ind_ravel = np.ravel_multi_index((azi_ind[:, 0], azi_ind[:, 1]), dims=array2.shape)

    common_ind_ravel = np.intersect1d(tth_ind_ravel, azi_ind_ravel)
    result_ind = np.unravel_index(common_ind_ravel, dims=array1.shape)

    while len(result_ind[0]) > 1:
        if np.max(np.diff(array1)) > 0:
            delta1 = np.max(np.diff(array1[result_ind]))

        if np.max(np.diff(array2)) > 0:
            delta2 = np.max(np.diff(array2[result_ind]))

        tth_ind = np.argwhere(np.abs(array1[result_ind] - value1) < delta1)
        azi_ind = np.argwhere(np.abs(array2[result_ind] - value2) < delta2)

        print(result_ind)

        common_ind = np.intersect1d(tth_ind, azi_ind)
        result_ind = (result_ind[0][common_ind], result_ind[1][common_ind])

    return result_ind[0], result_ind[1]
Beispiel #5
0
 def map_to_phon(self):
     '''decibel are converted into phon which are a logarithmic unit to human loudness sensation'''
     # number of bark bands, matrix length in time dim
     n_bands = self.processed.shape[0]
     t       = self.processed.shape[1]
 
     
     # DB-TO-PHON BARK-SCALE-LIMIT TABLE
     # introducing 1 level more with level(1) being infinite
     # to avoid (levels - 1) producing errors like division by 0
     table_dim = n_bands; 
     cbv       = np.concatenate((np.tile(np.inf,(table_dim,1)), 
                                 self.loudn_bark[:,0:n_bands].transpose()),1)
     
     # the matrix 'levels' stores the correct Phon level for each datapoint
     # init lowest level = 2
     levels = np.tile(2,(n_bands,t)) 
     
     for lev in range(1,6): 
         db_thislev = np.tile(np.asarray([cbv[:,lev]]).transpose(),(1,t))
         levels[np.where(self.processed > db_thislev)] = lev + 2
     #ok here we compute indexes that match the cbv array such that when indexing the cbv array we get back an matrix with the proper dimensions
     #this indices match the upper or lower phon level according to the current value of spl in our matrix
     cbv_ind_hi = np.ravel_multi_index(dims=(table_dim,7), multi_index=np.array([np.tile(np.array([range(0,table_dim)]).transpose(),(1,t)), levels-1]), order='F') 
     cbv_ind_lo = np.ravel_multi_index(dims=(table_dim,7), multi_index=np.array([np.tile(np.array([range(0,table_dim)]).transpose(),(1,t)), levels-2]), order='F') 
 
     #for each datapoint in our matrix a interpolation factor 0 < f < 1 is calculated
     ifac = (self.processed[:,0:t] - cbv.transpose().ravel()[cbv_ind_lo]) / (cbv.transpose().ravel()[cbv_ind_hi] - cbv.transpose().ravel()[cbv_ind_lo])
     
     ifac[np.where(levels==2)] = 1 # keeps the upper phon value;
     ifac[np.where(levels==8)] = 1 # keeps the upper phon value;
 
     #finally the interpolation is computed here
     self.processed[:,0:t] = AudioAnalytics.phons.transpose().ravel()[levels - 2] + (ifac * (AudioAnalytics.phons.transpose().ravel()[levels - 1] - AudioAnalytics.phons.transpose().ravel()[levels - 2])) # OPT: pre-calc diff    
def TwoPeriodSun(constant,delta,slope,slopedir,lat):
    # First derive A1 and A2 from the normal procedure    
    A1,A2 = SunHours(delta,slope,slopedir,lat)
    
    # Then calculate the other two functions.
    # Initialize function    
    
    a,b,c = Constants(delta,slope,slopedir,lat)
    riseSlope, setSlope = BoundsSlope(a,b,c)  
    
    B1 = np.maximum(riseSlope,setSlope)
    B2 = np.minimum(riseSlope,setSlope)
    
    Angle_B1 = AngleSlope(a,b,c,B1)
    Angle_B2 = AngleSlope(a,b,c,B2) 
    
    B1[abs(Angle_B1) > 0.001] = np.pi - B1[abs(Angle_B1) > 0.001]
    B2[abs(Angle_B2) > 0.001] = -np.pi - B2[abs(Angle_B2) > 0.001]    
    
    # Check if two periods really exist
    ID = np.ravel_multi_index(np.where(np.logical_and(B2 >= A1, B1 >= A2) == True),a.shape)
    Val = IntegrateSlope(constant,B2.flat[ID],B1.flat[ID],delta,slope.flat[ID],slopedir.flat[ID],lat.flat[ID])
    ID = ID[Val < 0]
    
    # Finally calculate resulting values
    Vals = np.zeros(B1.shape)

    Vals.flat[ID] = (IntegrateSlope(constant,A1.flat[ID],B2.flat[ID],delta,slope.flat[ID],slopedir.flat[ID],lat.flat[ID])  + 
                       IntegrateSlope(constant,B1.flat[ID],A2.flat[ID],delta,slope.flat[ID],slopedir.flat[ID],lat.flat[ID]))
    ID = np.ravel_multi_index(np.where(Vals == 0),a.shape)   
    Vals.flat[ID] = IntegrateSlope(constant,A1.flat[ID],A2.flat[ID],delta,slope.flat[ID],slopedir.flat[ID],lat.flat[ID])
    
    return(Vals)
Beispiel #7
0
        def setInSlot(self, slot, key, value):
            shape = self.inputs["shape"].value
            eraseLabel = self.inputs["eraser"].value
            neutralElement = 0
    
            self.lock.acquire()
            #fix slicing of single dimensions:            
            start, stop = sliceToRoi(key, shape, extendSingleton = False)
            start = start.floor()
            stop = stop.floor()
            
            tempKey = roiToSlice(start-start, stop-start, hardBind = True)
            
            stop += numpy.where(stop-start == 0,1,0)

            key = roiToSlice(start,stop)

            updateShape = tuple(stop-start)
    
            update = self._denseArray[key].copy()
            
            update[tempKey] = value

            startRavel = numpy.ravel_multi_index(numpy.array(start, numpy.int32),shape)
            
            #insert values into dict
            updateNZ = numpy.nonzero(numpy.where(update != neutralElement,1,0))
            updateNZRavelSmall = numpy.ravel_multi_index(updateNZ, updateShape)
            
            if isinstance(value, numpy.ndarray):
                valuesNZ = value.ravel()[updateNZRavelSmall]
            else:
                valuesNZ = value
    
            updateNZRavel = numpy.ravel_multi_index(updateNZ, shape)
            updateNZRavel += startRavel        
    
            self._denseArray.ravel()[updateNZRavel] = valuesNZ        
            
            valuesNZ = self._denseArray.ravel()[updateNZRavel]
            
            self._denseArray.ravel()[updateNZRavel] =  valuesNZ       
    
            
            td = blist.sorteddict(zip(updateNZRavel.tolist(),valuesNZ.tolist()))
       
            self._sparseNZ.update(td)
            
            #remove values to be deleted
            updateNZ = numpy.nonzero(numpy.where(update == eraseLabel,1,0))
            if len(updateNZ)>0:
                updateNZRavel = numpy.ravel_multi_index(updateNZ, shape)
                updateNZRavel += startRavel    
                self._denseArray.ravel()[updateNZRavel] = neutralElement
                for index in updateNZRavel:
                    self._sparseNZ.pop(index)
            
            self.lock.release()
            
            self.outputs["Output"].setDirty(key)
Beispiel #8
0
def transform2phon(matrix):

    # number of bark bands, matrix length in time dim
    n_bands = matrix.shape[0]
    t       = matrix.shape[1]

    # DB-TO-PHON BARK-SCALE-LIMIT TABLE
    # introducing 1 level more with level(1) being infinite
    # to avoid (levels - 1) producing errors like division by 0

    #%%table_dim = size(CONST_loudn_bark,2);
    table_dim = n_bands; # OK
    cbv       = np.concatenate((np.tile(np.inf,(table_dim,1)), loudn_bark[:,0:n_bands].transpose()),1) # OK

    # init lowest level = 2
    levels = np.tile(2,(n_bands,t)) # OK

    for lev in range(1,6): # OK
        db_thislev = np.tile(np.asarray([cbv[:,lev]]).transpose(),(1,t))
        levels[np.where(matrix > db_thislev)] = lev + 2

    # the matrix 'levels' stores the correct Phon level for each data point
    cbv_ind_hi = np.ravel_multi_index(dims=(table_dim,7), multi_index=np.array([np.tile(np.array([range(0,table_dim)]).transpose(),(1,t)), levels-1]), order='F')
    cbv_ind_lo = np.ravel_multi_index(dims=(table_dim,7), multi_index=np.array([np.tile(np.array([range(0,table_dim)]).transpose(),(1,t)), levels-2]), order='F')

    # interpolation factor % OPT: pre-calc diff
    ifac = (matrix[:,0:t] - cbv.transpose().ravel()[cbv_ind_lo]) / (cbv.transpose().ravel()[cbv_ind_hi] - cbv.transpose().ravel()[cbv_ind_lo])

    ifac[np.where(levels==2)] = 1 # keeps the upper phon value;
    ifac[np.where(levels==8)] = 1 # keeps the upper phon value;

    # phons has been initialized globally above

    matrix[:,0:t] = phons.transpose().ravel()[levels - 2] + (ifac * (phons.transpose().ravel()[levels - 1] - phons.transpose().ravel()[levels - 2])) # OPT: pre-calc diff
    return(matrix)
Beispiel #9
0
    def __init__(self, model):
        """ Initialize the filtered stim model
        """
        self.model = model
        N = model['N']
        prms = model['network']['weight']

        self.prior = create_prior(prms['prior'])
	
        # Implement refractory period by having negative mean on self loops
        if 'refractory_prior' in prms:
            #self.mu[np.diag_indices(N)] = prms['mu_refractory']
            self.refractory_prior = create_prior(prms['refractory_prior'])

            # Get the upper and lower diagonal indices so that we can evaluate
            # the log prob of the refractory weights separately from the
            # log prob of the regular weights
            self.diags = np.ravel_multi_index(np.diag_indices(N), (N,N))
            lower = np.ravel_multi_index(np.tril_indices(N,k=-1), (N,N))
            upper = np.ravel_multi_index(np.triu_indices(N,k=1), (N,N))
            self.nondiags = np.concatenate((lower, upper))

        # Define weight matrix
        self.W_flat = T.dvector(name='W')
        self.W = T.reshape(self.W_flat,(N,N))

        if hasattr(self, 'refractory_prior'):
            self.log_p = self.prior.log_p(self.W.take(self.nondiags)) + \
                         self.refractory_prior.log_p(self.W.take(self.diags))
        else:
            self.log_p = self.prior.log_p(self.W)
Beispiel #10
0
def _offsets_to_raveled_neighbors(image_shape, structure, center):
    """Compute offsets to a samples neighbors if the image would be raveled.

    Parameters
    ----------
    image_shape : tuple
        The shape of the image for which the offsets are computed.
    structure : ndarray
        A structuring element determining the neighborhood expressed as an
        n-D array of 1's and 0's.
    center : sequence
        Tuple of indices specifying the center of `selem`.

    Returns
    -------
    offsets : ndarray
        Linear offsets to a samples neighbors in the raveled image, sorted by
        their Euclidean distance from the center.

    Examples
    --------
    >>> _offsets_to_raveled_neighbors((4, 5), np.ones((4, 3)), (1, 1))
    array([-5, -1,  1,  5, -6, -4,  4,  6, 10,  9, 11])
    """
    structure = structure.copy()  # Don't modify original input
    structure[tuple(center)] = 0  # Ignore the center; it's not a neighbor
    connection_indices = np.transpose(np.nonzero(structure))
    offsets = (np.ravel_multi_index(connection_indices.T, image_shape) -
               np.ravel_multi_index(center, image_shape))
    squared_distances = np.sum((connection_indices - center) ** 2, axis=1)
    return offsets[np.argsort(squared_distances)]
    def _init_indices(self):
        # Moore neighbourhood for now. Will see others later.
        self.n_neighbours = 8 # Moore
        cols, rows = map(np.ndarray.flatten, np.meshgrid(*self._valid_range()))
        self.inner_indices = np.ravel_multi_index((rows, cols),
                                                  self.dims,
                                                  order='C')
        left_cols, right_cols = cols - 1, cols + 1
        up_rows, down_rows = rows - 1, rows + 1
        neighbour_pix_locs = [(up_rows, left_cols), (up_rows, cols),
                              (up_rows, right_cols),
                              (rows, left_cols), (rows, cols),
                              (rows, right_cols),
                              (down_rows, left_cols), (down_rows, cols),
                              (down_rows, right_cols)]

        self.indices = np.empty((self.inner_indices.size,
                                 self.n_neighbours + 1),
                                dtype=np.int)
        for i in xrange(len(neighbour_pix_locs)):
            self.indices[:,i] \
                    = np.ravel_multi_index(neighbour_pix_locs[i],
                                           self.dims,
                                           mode='wrap',
                                           order='C')
Beispiel #12
0
def test_rmi():
    I1 = _rmi([3, 4], [10, 10])
    assert_equal(I1, 34)
    I1 = _rmi([0, 0], [10, 10])
    assert_equal(I1, 0)
    assert_raises(ValueError, _rmi, [10, 0], [10, 10])

    try:
        from numpy import ravel_multi_index
    except ImportError:
        raise nose.SkipTest()

    # Dtype of random integers is system dependent
    A, B, C, D = np.random.randint(0, 1000, size=[4, 100])
    I1 = _rmi([A, B], dims=[1000, 1000])
    I2 = ravel_multi_index([A, B], dims=[1000, 1000])
    assert_array_equal(I1, I2)
    I1 = _rmi([A, B, C, D], dims=[1000]*4)
    I2 = ravel_multi_index([A, B, C, D], dims=[1000]*4)
    assert_array_equal(I1, I2)
    # Check for overflow with small int types
    indices = np.random.randint(0, 255, size=(2, 100))
    dims = (1000, 1000)
    I1 = _rmi(indices, dims=dims)
    I2 = ravel_multi_index(indices, dims=dims)
    assert_array_equal(I1, I2)
Beispiel #13
0
def create_sparse_rate_matrix(state_space_shape, row, col, rate):
    """
    Create the rate matrix.

    """
    # check conformability of input arrays
    ndim = len(state_space_shape)
    assert_equal(len(row.shape), 2)
    assert_equal(len(col.shape), 2)
    assert_equal(len(rate.shape), 1)
    assert_equal(row.shape[0], rate.shape[0])
    assert_equal(col.shape[0], rate.shape[0])
    assert_equal(row.shape[1], ndim)
    assert_equal(col.shape[1], ndim)

    # create the sparse Q matrix from the sparse arrays
    nstates = np.prod(state_space_shape)
    mrow = np.ravel_multi_index(row.T, state_space_shape)
    mcol = np.ravel_multi_index(col.T, state_space_shape)
    Q = coo_matrix((rate, (mrow, mcol)), (nstates, nstates))

    # get the dense array of exit rates, and set the diagonal
    exit_rates = Q.sum(axis=1).A.flatten()
    Q.setdiag(-exit_rates)

    return Q
Beispiel #14
0
def dig(this, next):
    # first select active mesh from next level, aligned to this level
    next.data = next.data[activemask(next, this.Nmesh)]

    # calculate the index of the covered mesh on this level
    index_from_next = numpy.ravel_multi_index(
        next.data["gps"].T // (next.Nmesh // this.Nmesh), (this.Nmesh, this.Nmesh, this.Nmesh)
    )

    # index this level
    index = numpy.ravel_multi_index(this.data["gps"].T, (this.Nmesh, this.Nmesh, this.Nmesh))

    preserve_mask2 = exist(index, index_from_next)
    next.data = next.data[preserve_mask2]

    if next.DownSample > 1:
        # downsample levels need to carry on the interpolation
        # of previous level
        next.data["delta"] += trilinearinterp(
            next.data["gps"] * (1.0 * this.Nmesh / next.Nmesh),
            this.data["gps"],
            this.data["delta"],
            (next.Nmesh, next.Nmesh, next.Nmesh),
        )
        next.data["disp"] += trilinearinterp(
            next.data["gps"] * (1.0 * this.Nmesh / next.Nmesh),
            this.data["gps"],
            this.data["disp"],
            (next.Nmesh, next.Nmesh, next.Nmesh),
        )

    # locate those to be removed
    covered_mask = exist(index_from_next, index)
    preserve_mask = ~covered_mask
    this.data = this.data[preserve_mask]
Beispiel #15
0
    def test_dtypes(self):
        # Test with different data types
        for dtype in [np.int16, np.uint16, np.int32,
                      np.uint32, np.int64, np.uint64]:
            coords = np.array(
                [[1, 0, 1, 2, 3, 4], [1, 6, 1, 3, 2, 0]], dtype=dtype)
            shape = (5, 8)
            uncoords = 8*coords[0]+coords[1]
            assert_equal(np.ravel_multi_index(coords, shape), uncoords)
            assert_equal(coords, np.unravel_index(uncoords, shape))
            uncoords = coords[0]+5*coords[1]
            assert_equal(
                np.ravel_multi_index(coords, shape, order='F'), uncoords)
            assert_equal(coords, np.unravel_index(uncoords, shape, order='F'))

            coords = np.array(
                [[1, 0, 1, 2, 3, 4], [1, 6, 1, 3, 2, 0], [1, 3, 1, 0, 9, 5]],
                dtype=dtype)
            shape = (5, 8, 10)
            uncoords = 10*(8*coords[0]+coords[1])+coords[2]
            assert_equal(np.ravel_multi_index(coords, shape), uncoords)
            assert_equal(coords, np.unravel_index(uncoords, shape))
            uncoords = coords[0]+5*(coords[1]+8*coords[2])
            assert_equal(
                np.ravel_multi_index(coords, shape, order='F'), uncoords)
            assert_equal(coords, np.unravel_index(uncoords, shape, order='F'))
Beispiel #16
0
 def compute_offsets(self):
 
 #    Compute neighbor offsets index.
     mask_coords = self.mask_coords
     vol_num = self.vol_num
     nb_num = self.nb_num
     dims = self.dims
     mask_idx = np.ravel_multi_index(self.mask_coords.T, dims)
      
     offsets = self.nb_shape.compute_off()
     ds_idx = np.arange(vol_num)
     volid = np.zeros(nb_num*vol_num)
     nbid = np.zeros(nb_num*vol_num)
     is_nb = np.zeros(nb_num*vol_num)
     
     
     count = 0
     for v in range(self.vol_num):
         v3d = mask_coords[v,:]
         nb3d = v3d + offsets.T
         imgnb = is_in_image(nb3d, dims)
         nb1d = np.ravel_multi_index(nb3d[imgnb, :].T, dims)
         is_in_mask = np.in1d(nb1d,mask_idx)
         nb1d = nb1d[is_in_mask]
         nb_idx_in_mask = np.in1d(mask_idx,nb1d)
         nb1d = mask_idx[nb_idx_in_mask]
         nb_idx_in_ds = np.nonzero(nb_idx_in_mask)[0]
         volnb_num = len(nb1d)
         volid[ count: count + volnb_num]= np.tile(v, volnb_num)
         nbid[ count: count + volnb_num] = nb_idx_in_ds
         is_nb[ count: count + volnb_num] = 1
         count  = count + volnb_num
     neighbor_sparse_matrix = sparse.csc_matrix((is_nb,(volid,nbid)),shape = (vol_num,vol_num))
     return neighbor_sparse_matrix
Beispiel #17
0
def unpack_array_c(input_data, output_shape):
    """Unpack an array."""
    input_data  = np.asarray(input_data)
    output_data = _prepare_arrays(input_data, output_shape)
    input_shape = input_data.shape
    
    # Total number of elements in each type of array.
    nft = np.prod(input_shape)
    nn  = np.prod(output_shape)
    
    # Extract single dimension parameters, where relevant.
    ny = output_shape[0]
    nf = input_shape[-1]
    nx = output_shape[-1]
    
    # Determine the central element.
    nfo = nf - 1 if nx % 2 == 0 else nf
    for i in range(nft):
        
        # Extract the cartesian coordinates in the original grid.
        y = i // nf
        x = i %  nf
        
        # re-map onto the wider grid.
        io = (y * nx) + x
        ii = (y * nf) + x
        output_data.flat[io] = input_data.flat[ii]
        
        # Check our indexing math with numpy's version.
        assert np.allclose([y,x], np.unravel_index(ii, input_shape))
        assert np.allclose([io], np.ravel_multi_index([y, x], output_shape))
        
        # If we are at y=0, flip around center.
        if y == 0 and 0 < x < nfo:
            yo = 0
            xo = nx - x
            io = (yo * nx) + xo
            
            # Check our math!
            assert np.allclose([io], np.ravel_multi_index([yo, xo], output_shape))
            assert np.allclose([yo, xo], np.unravel_index(io, output_shape))
            
            # Insert data.
            output_data.flat[io] = np.conj(input_data.flat[ii])
            
        # If we are beyond y=0, flip in both axes.
        if y > 0 and 0 < x < nfo:
            yo = ny - y
            xo = nx - x
            io = (yo * nx) + xo
            
            # Check our math!
            assert np.allclose([io], np.ravel_multi_index([yo, xo], output_shape))
            assert np.allclose([yo, xo], np.unravel_index(io, output_shape))
            
            # Insert data.
            output_data.flat[io] = np.conj(input_data.flat[ii])
    
    return output_data
Beispiel #18
0
 def test_clipmodes(self):
     # Test clipmodes
     assert_equal(np.ravel_multi_index([5,1,-1,2], (4,3,7,12), mode='wrap'),
                  np.ravel_multi_index([1,1,6,2], (4,3,7,12)))
     assert_equal(np.ravel_multi_index([5,1,-1,2], (4,3,7,12),
                              mode=('wrap','raise','clip','raise')),
                  np.ravel_multi_index([1,1,0,2], (4,3,7,12)))
     assert_raises(ValueError, np.ravel_multi_index, [5,1,-1,2], (4,3,7,12))
Beispiel #19
0
def build_diff_matrix(int_band, dxvec, shape):
    """Builds a Laplacian matrix"""
    # TODO: generalize this!
    shape = shape.astype(np.int64)
    dim = len(shape)
    if dim == 3:
        # Assume now that band is not a linear index, but a 3D index (ie, int_band)
        dx, dy, dz = dxvec
        weights = np.array([-2/dx**2 -2/dy**2 -2/dz**2,
                             1/dx**2, 1/dx**2,
                             1/dy**2, 1/dy**2,
                             1/dz**2, 1/dz**2])

        offsets = np.array([[ 0, 0, 0],
                            [ 1, 0, 0],
                            [-1, 0, 0],
                            [ 0, 1, 0],
                            [ 0,-1, 0],
                            [ 0, 0, 1],
                            [ 0, 0,-1]])

        Li = np.tile(np.arange(int_band.shape[0])[:, np.newaxis], weights.size)
        Lj = np.zeros_like(Li)
        Ls = np.zeros_like(Li, dtype=np.float)
        i, j, k = int_band.T  #np.ravel_multi_index(band.T, shape)

        for c in xrange(weights.size):
            ii = i + offsets[c, 0]
            jj = j + offsets[c, 1]
            kk = k + offsets[c, 2]
            Ls[:, c] = weights[c]
            Lj[:, c] = np.ravel_multi_index((ii, jj, kk), shape)
        L = coo_matrix((Ls.ravel(), (Li.ravel(), Lj.ravel())),
                       (int_band.shape[0], shape[0] * shape[1] * shape[2])).tocsr()
        return L[:, np.ravel_multi_index(int_band.T, shape)]
    elif dim == 2:
        dx, dy, dz = dxvec
        weights = np. array([-2/dx**2 -2/dy**2,
                              1/dx**2, 1/dx**2,
                              1/dy**2, 1/dy**2])
        offsets = np.array([[ 0, 0],
                            [ 1, 0],
                            [-1, 0],
                            [ 0, 1],
                            [ 0,-1]])
        Li = np.tile(np.arange(int_band.shape[0])[:, np.newaxis], weights.size)
        Lj = np.zeros_like(Li)
        Ls = np.zeros_like(Li, dtype=np.float)
        i, j = int_band.T  #np.ravel_multi_index(band.T, shape)

        for c in xrange(weights.size):
            ii = i + offsets[c, 0]
            jj = j + offsets[c, 1]
            Ls[:, c] = weights[c]
            Lj[:, c] = np.ravel_multi_index((ii, jj), shape)
        L = coo_matrix((Ls.ravel(), (Li.ravel(), Lj.ravel())),
                       (int_band.shape[0], shape[0] * shape[1])).tocsr()
        return L[:, np.ravel_multi_index(int_band.T, shape)]
Beispiel #20
0
def _inpaint_biharmonic_single_channel(mask, out, limits):
    # Initialize sparse matrices
    matrix_unknown = sparse.lil_matrix((np.sum(mask), out.size))
    matrix_known = sparse.lil_matrix((np.sum(mask), out.size))

    # Find indexes of masked points in flatten array
    mask_i = np.ravel_multi_index(np.where(mask), mask.shape)

    # Find masked points and prepare them to be easily enumerate over
    mask_pts = np.array(np.where(mask)).T

    # Iterate over masked points
    for mask_pt_n, mask_pt_idx in enumerate(mask_pts):
        # Get bounded neighborhood of selected radius
        b_lo, b_hi = _get_neighborhood(mask_pt_idx, 2, out.shape)

        # Create biharmonic coefficients ndarray
        neigh_coef = np.zeros(b_hi - b_lo)
        neigh_coef[tuple(mask_pt_idx - b_lo)] = 1
        neigh_coef = laplace(laplace(neigh_coef))

        # Iterate over masked point's neighborhood
        it_inner = np.nditer(neigh_coef, flags=['multi_index'])
        for coef in it_inner:
            if coef == 0:
                continue
            tmp_pt_idx = np.add(b_lo, it_inner.multi_index)
            tmp_pt_i = np.ravel_multi_index(tmp_pt_idx, mask.shape)

            if mask[tuple(tmp_pt_idx)]:
                matrix_unknown[mask_pt_n, tmp_pt_i] = coef
            else:
                matrix_known[mask_pt_n, tmp_pt_i] = coef

    # Prepare diagonal matrix
    flat_diag_image = sparse.dia_matrix((out.flatten(), np.array([0])),
                                        shape=(out.size, out.size))

    # Calculate right hand side as a sum of known matrix's columns
    matrix_known = matrix_known.tocsr()
    rhs = -(matrix_known * flat_diag_image).sum(axis=1)

    # Solve linear system for masked points
    matrix_unknown = matrix_unknown[:, mask_i]
    matrix_unknown = sparse.csr_matrix(matrix_unknown)
    result = spsolve(matrix_unknown, rhs)

    # Handle enormous values
    result = np.clip(result, *limits)

    result = result.ravel()

    # Substitute masked points with inpainted versions
    for mask_pt_n, mask_pt_idx in enumerate(mask_pts):
        out[tuple(mask_pt_idx)] = result[mask_pt_n]

    return out
Beispiel #21
0
    def _make_hdu_sparse(data, npix, hdu, header):
        shape = data.shape

        # We make a copy, because below we modify `data` to handle non-finite entries
        # TODO: The code below could probably be simplified to use expressions
        # that create new arrays instead of in-place modifications
        # But first: do we want / need the non-finite entry handling at all and always cast to 64-bit float?
        data = data.copy()

        if len(shape) == 2:
            data_flat = np.ravel(data)
            data_flat[~np.isfinite(data_flat)] = 0
            nonzero = np.where(data_flat > 0)
            value = data_flat[nonzero].astype(float)

            cols = [
                fits.Column('PIX', 'J', array=nonzero[0]),
                fits.Column('VALUE', 'E', array=value),
            ]
        elif npix[0].size == 1:
            shape_flat = shape[:-2] + (shape[-1] * shape[-2],)
            data_flat = np.ravel(data).reshape(shape_flat)
            data_flat[~np.isfinite(data_flat)] = 0
            nonzero = np.where(data_flat > 0)
            channel = np.ravel_multi_index(nonzero[:-1], shape[:-2])
            value = data_flat[nonzero].astype(float)

            cols = [
                fits.Column('PIX', 'J', array=nonzero[-1]),
                fits.Column('CHANNEL', 'I', array=channel),
                fits.Column('VALUE', 'E', array=value),
            ]
        else:
            data_flat = []
            channel = []
            pix = []
            for i, _ in np.ndenumerate(npix[0]):
                data_i = np.ravel(data[i[::-1]])
                data_i[~np.isfinite(data_i)] = 0
                pix_i = np.where(data_i > 0)
                data_i = data_i[pix_i]
                data_flat += [data_i]
                pix += pix_i
                channel += [np.ones(data_i.size, dtype=int) *
                            np.ravel_multi_index(i[::-1], shape[:-2])]

            pix = np.concatenate(pix)
            channel = np.concatenate(channel)
            value = np.concatenate(data_flat).astype(float)

            cols = [
                fits.Column('PIX', 'J', array=pix),
                fits.Column('CHANNEL', 'I', array=channel),
                fits.Column('VALUE', 'E', array=value),
            ]

        return fits.BinTableHDU.from_columns(cols, header=header, name=hdu)
Beispiel #22
0
 def symmetrize(self, method=None, copy=False):
   '''Symmetrizes (ignores method). Returns a copy if copy=True.'''
   if copy:
     return SymmEdgePairGraph(self._pairs.copy(),
                              num_vertices=self._num_vertices)
   shape = (self._num_vertices, self._num_vertices)
   flat_inds = np.union1d(np.ravel_multi_index(self._pairs.T, shape),
                          np.ravel_multi_index(self._pairs.T[::-1], shape))
   self._pairs = np.transpose(np.unravel_index(flat_inds, shape))
   return self
Beispiel #23
0
def row_extents(cube, dim_order=None):
    if dim_order is None:
        dim_order = MS_DIM_ORDER

    shape = cube.dim_global_size(*dim_order)
    lower = cube.dim_lower_extent(*dim_order)
    upper = tuple(u-1 for u in cube.dim_upper_extent(*dim_order))

    return (np.ravel_multi_index(lower, shape),
        np.ravel_multi_index(upper, shape) + 1)
Beispiel #24
0
def getVectorField(Map, sign=True, colorMatrix=None):
 X,Y,cardinal = Map.shape
 uMatrix = getUmatrix(Map)
 uMatrix_ravel = uMatrix.flatten()
 distanceMatrix = scipy.spatial.distance.squareform(scipy.spatial.distance.pdist(Map.reshape(X*Y,cardinal)))
 vectorsField = numpy.zeros((X,Y,2))
 vectors_unit = [(-1/numpy.sqrt(2),-1/numpy.sqrt(2)),(-1,0),(-1/numpy.sqrt(2),1/numpy.sqrt(2)),(0,-1),(0,1),(1/numpy.sqrt(2),-1/numpy.sqrt(2)),(1,0),(1/numpy.sqrt(2),1/numpy.sqrt(2))]
 for i in range(X):
  for j in range(Y):
   iRef = numpy.ravel_multi_index((i%X,j%Y),(X,Y))
   jRef1 = numpy.ravel_multi_index(((i-1)%X,(j-1)%Y),(X,Y))
   jRef2 = numpy.ravel_multi_index(((i-1)%X,(j)%Y),(X,Y))
   jRef3 = numpy.ravel_multi_index(((i-1)%X,(j+1)%Y),(X,Y))
   jRef4 = numpy.ravel_multi_index(((i)%X,(j-1)%Y),(X,Y))
   jRef5 = numpy.ravel_multi_index(((i)%X,(j+1)%Y),(X,Y))
   jRef6 = numpy.ravel_multi_index(((i+1)%X,(j-1)%Y),(X,Y))
   jRef7 = numpy.ravel_multi_index(((i+1)%X,(j)%Y),(X,Y))
   jRef8 = numpy.ravel_multi_index(((i+1)%X,(j+1)%Y),(X,Y))
   norms = [distanceMatrix[iRef,jRef1], distanceMatrix[iRef,jRef2], distanceMatrix[iRef,jRef3], distanceMatrix[iRef,jRef4], distanceMatrix[iRef,jRef5], distanceMatrix[iRef,jRef6], distanceMatrix[iRef,jRef7], distanceMatrix[iRef,jRef8]]
   if sign:
    signs = [numpy.sign(uMatrix_ravel[iRef]-uMatrix_ravel[e]) for e in [jRef1,jRef2,jRef3,jRef4,jRef5,jRef6,jRef7,jRef8]]
    norms = numpy.array(signs)*numpy.array(norms)
   vectors = numpy.atleast_2d(norms).T*numpy.array(vectors_unit)
   vectorsField[i,j] = vectors.sum(axis=0)
 if colorMatrix == None:
  vectorsFieldPlot = matplotlib.pyplot.quiver(vectorsField[:,:,1], vectorsField[:,:,0], uMatrix, units='xy', pivot='tail')
 else:
  vectorsFieldPlot = matplotlib.pyplot.quiver(vectorsField[:,:,1], vectorsField[:,:,0], colorMatrix, units='xy', pivot='tail')
 return vectorsField
Beispiel #25
0
def _random_correlated_image(mean, sigma, image_shape, alpha=0.3, rng=None):
    """
    Creates a random image with correlated neighbors.
    pixel covariance is sigma^2, direct neighors pixel covariance is alpha * sigma^2.

    Parameters
    ----------
    mean : the mean value of the image pixel values.
    sigma : the std dev of image pixel values.
    image_shape : tuple, shape = (3, )
    alpha : the neighbors correlation factor.
    rng : random number generator (a numpy.random.RandomState instance).
    """
    dim_x, dim_y, dim_z = image_shape
    dim_image = dim_x * dim_y * dim_z

    correlated_image = 0
    for neighbor in [(1, 0, 0), (0, 1, 0), (0, 0, 1)]:
        corr_data = []
        corr_i = []
        corr_j = []
        for i, j, k in [(0, 0, 0), neighbor]:
            d2 = 1.0 * (i*i + j*j + k*k)
            ind = np.asarray(np.mgrid[0:dim_x-i, 0:dim_y-j, 0:dim_z-k], dtype=np.int)
            ind = ind.reshape((3, (dim_x - i) * (dim_y - j) * (dim_z - k)))
            corr_i.extend(np.ravel_multi_index(ind, (dim_x, dim_y, dim_z)).tolist())
            corr_j.extend(np.ravel_multi_index(ind + np.asarray([i, j, k])[:, None],
                                          (dim_x, dim_y, dim_z)).tolist())
            if i>0 or j>0 or k>0:
                corr_i.extend(np.ravel_multi_index(ind + np.asarray([i, j, k])[:, None],
                                              (dim_x, dim_y, dim_z)).tolist())
                corr_j.extend(np.ravel_multi_index(ind, (dim_x, dim_y, dim_z)).tolist())
            if i==0 and j==0 and k==0:
                corr_data.extend([3.0] * ind.shape[1])
            else:
                corr_data.extend([alpha * 3.0] * 2 * ind.shape[1])

        correlation = scisp.csc_matrix((corr_data, (corr_i, corr_j)), shape=(dim_image, dim_image))

        factor = cholesky(correlation)
        L = factor.L()
        P = factor.P()[None, :]
        P = scisp.csc_matrix((np.ones(dim_image),
                              np.vstack((P, np.asarray(range(dim_image))[None, :]))),
                             shape=(dim_image, dim_image))

        sq_correlation = P.dot(L)
        X = rng.normal(0, 1, dim_image)
        Y = sq_correlation.dot(X)
        Y = Y.reshape((dim_x, dim_y, dim_z))
        X = X.reshape((dim_x, dim_y, dim_z))
        correlated_image += Y
    correlated_image /= 3

    return correlated_image * sigma + mean
Beispiel #26
0
 def numpy_run_cut(self, cut, coords):
     batch, y1, x1, ch, out_y, out_x = coords
     cut_index = self.numpy_run_cut_offset(
         cut, numpy.ravel_multi_index((batch, out_y, out_x, ch),
                                      self.output.shape))
     i, j = numpy.unravel_index(cut_index, cut.shape)
     idx = numpy.ravel_multi_index((batch, y1 + i, x1 + j, ch),
                                   self.input.shape)
     val = numpy.ravel(self.input.mem)[idx]
     self.input_offset.mem[batch, out_y, out_x, ch] = idx
     return val
Beispiel #27
0
def adding_to_gridbit(bit, gridtf, dims):
    refbit = bit.copy()
    adding = False
    for i in refbit:
        for delta in zip(adj_x, adj_y):
            j = np.array(delta) + np.unravel_index(i, dims)
            if gridtf[np.ravel_multi_index(j, dims, 'wrap')]:
                gridtf[np.ravel_multi_index(j, dims, 'wrap')] = False
                bit.add(np.ravel_multi_index(j, dims, 'wrap'))
                adding = True
    return adding
Beispiel #28
0
 def test_clipmodes(self):
     # Test clipmodes
     assert_equal(
         np.ravel_multi_index([5, 1, -1, 2], (4, 3, 7, 12), mode="wrap"),
         np.ravel_multi_index([1, 1, 6, 2], (4, 3, 7, 12)),
     )
     assert_equal(
         np.ravel_multi_index([5, 1, -1, 2], (4, 3, 7, 12), mode=("wrap", "raise", "clip", "raise")),
         np.ravel_multi_index([1, 1, 0, 2], (4, 3, 7, 12)),
     )
     assert_raises(ValueError, np.ravel_multi_index, [5, 1, -1, 2], (4, 3, 7, 12))
Beispiel #29
0
 def basis_span(self, i):
     self.check_index(i)
     indx = np.unravel_index(i, self.shape)
     min_indices = []
     max_indices = []
     for j in range(len(self.shape)):
         min_indices.append(np.min(self.space_list[j].basis_span(indx[j])))
         max_indices.append(np.max(self.space_list[j].basis_span(indx[j])))
     i1 = np.ravel_multi_index(min_indices, self.shape)
     i2 = np.ravel_multi_index(max_indices, self.shape)
     return (i1, i2)
def skeleton_to_gt_graph(skeleton, with_coordinates = True, verbose = True):
  """Converts a binary skeleton image to a networkx graph
  
  Arguments:
    skeleton (array): 2d/3d binary skeleton image
    
  Returns:
    dict: dict of adjacency information with entries node_id : [neighbours]
  """
  dim = skeleton.ndim;
  shape =skeleton.shape;
  
  coords, nh = skeleton_to_list(skeleton, with_neighborhoods = True);
  nnodes = coords.shape[0];
  
  coordids = np.ravel_multi_index(coords.T, shape);  
  coordids2ids = { k:i for i,k in enumerate(coordids) };
    
  # create graph
  if verbose:
    print('creating graph...');
  g = gt.Graph(directed = False);
  g.add_vertex(nnodes);
  if with_coordinates:
      if verbose:
        print('creating coordinate properties...')
      vp = g.new_vertex_property('int', coords[:,0]);
      g.vertex_properties['x'] = vp;
      vp = g.new_vertex_property('int', coords[:,1]);
      g.vertex_properties['y'] = vp;
      if dim > 2:
          vp = g.new_vertex_property('int', coords[:,2]);
          g.vertex_properties['z'] = vp;
  
  for i, pos in enumerate(coords):
    if verbose and i % 1000 == 0:
      print('%d/%d nodes constructed...' % (i, len(coords)));
    #print 'nh'
    #print nh[i]
    posnh = np.transpose(np.where(nh[i]));
    #print 'pos'
    #print pos
    #print posnh
    if len(posnh) > 0:
      posnh = np.array(posnh + pos - 1);
      #print posnh
      #print posnh.shape
      ids = np.ravel_multi_index(posnh.T, shape);
      #print ids
      for j in [coordids2ids[k] for k in ids]:
        if i < j:
          g.add_edge(i,j);
  
  return g
Beispiel #31
0
    except ValueError:
        print '--> Please enter an integer'
while True:
    try:
        hifac = float(raw_input('--> Nyquist range factor: '))
        break
    except ValueError:
        print '--> Please enter a float'
print ' '
print ' '

### ETC ###

# dynamic variable names
for (j, k), img in np.ndenumerate(temp2d):
    index = np.ravel_multi_index((j, k), table2.shape)
    exec("pixel%d_flux = np.array(None)" % index)
    exec("pixel%d_time = np.array(None)" % index)

# filling the flux array
for l in range(stitchcounter):
    m = l + 1
    exec("second_flux%d = np.zeros([xsize, ysize%d])" % (m, m))
    exec("flux = flux%d" % m)
    for (i, j, k), val in np.ndenumerate(flux):
        index = (j + 1) * k + (x - k) * j
        exec("second_flux%d[index, i] = val" % m)

exp = raw_input('--> Export data for each pixel? (y/n) ')
while exp != "y" and exp != "n":
    exp = raw_input('--> Please enter y or n: ')
Beispiel #32
0
 def grid_coordinates_to_indices(self, coordinates=None):
     # pdb.set_trace()
     if coordinates is None:
         return np.arange(self.size)
     return np.ravel_multi_index(coordinates, self.shape)
Beispiel #33
0
def create_adr_features(features,
                        durations,
                        id2idx,
                        k,
                        feature_grid,
                        kmeans=True):
    pca = PCA(n_components=1)
    print("Num features: ", len(features))
    if durations is not None:
        durations = np.array(durations)

    #normalize
    np_features = np.array(features)
    print("Feature shape", np_features.shape)

    if kmeans:
        clustering = KMeans(n_clusters=min(k, len(np_features)),
                            random_state=0).fit(np_features)
        labels = clustering.labels_
        centroids = clustering.cluster_centers_
    else:
        clustering = MiniSom(k,
                             k,
                             np_features.shape[1],
                             sigma=0.5,
                             learning_rate=0.2,
                             neighborhood_function='gaussian',
                             random_seed=10)
        clustering.train_batch(features, 500, verbose=True)
        labels = np.array([clustering.winner(x) for x in features]).T
        labels = np.ravel_multi_index(labels, (k, k))

    print("Labels shape", labels.shape)
    print("Labels count:", len(Counter(labels)))

    data = []
    for id, idxs in id2idx.items():
        doc_distrib = labels[idxs]
        doc_embeds = np_features[idxs]

        if kmeans:
            centroid_velocities = np.zeros((len(doc_distrib) - 1))
            embed_velocities = np.zeros((len(doc_distrib) - 1))
            for i in range(1, len(doc_distrib)):
                len(doc_distrib)
                label1 = doc_distrib[i]
                label2 = doc_distrib[i - 1]
                cs_centroids = cosine_similarity([centroids[label1]],
                                                 [centroids[label2]])[0][0]
                cs_embeds = cosine_similarity([doc_embeds[i]],
                                              [doc_embeds[i - 1]])[0][0]
                embed_velocities[i - 1] = cs_embeds
                centroid_velocities[i - 1] = cs_centroids

            if len(centroid_velocities) > 128:
                centroid_velocities = centroid_velocities[:128]
                embed_velocities = embed_velocities[:128]

            else:
                centroid_velocities = np.pad(
                    centroid_velocities, (0, 128 - len(centroid_velocities)),
                    'constant',
                    constant_values=(0, 0))
                embed_velocities = np.pad(embed_velocities,
                                          (0, 128 - len(embed_velocities)),
                                          'constant',
                                          constant_values=(0, 0))

            centroid_embeds = np.zeros((len(doc_distrib), doc_embeds.shape[1]))
            for i in range(len(doc_distrib)):
                label = doc_distrib[i]

                centroid_embed = centroids[label]
                centroid_embeds[i, :] = centroid_embed

            centroid_embeds = pca.fit_transform(centroid_embeds).squeeze()
            if len(centroid_embeds) > 128:
                centroid_embeds = centroid_embeds[:128]
            else:
                centroid_embeds = np.pad(centroid_embeds,
                                         (0, 128 - len(centroid_embeds)),
                                         'constant',
                                         constant_values=(0, 0))

        label_velocities = []
        for i in range(1, len(doc_distrib)):
            diff_labels = doc_distrib[i] - doc_distrib[i - 1]
            label_velocities.append(diff_labels)

        label_acceleration = []
        for i in range(1, len(label_velocities)):
            diff_labels = label_velocities[i] - label_velocities[i - 1]
            label_acceleration.append(diff_labels)

        #get count features
        c = Counter(doc_distrib)
        num_all = len(doc_distrib)
        counts = []
        for i in range(k):
            if i in c:
                counts.append(c[i])
            else:
                counts.append(0)
        counts = [x / num_all for x in counts]

        #get embedding features
        embeds = pca.fit_transform(doc_embeds).squeeze()
        if len(embeds) > 128:
            embeds = embeds[:128]
        else:
            embeds = np.pad(embeds, (0, 128 - len(embeds)),
                            'constant',
                            constant_values=(0, 0))

        #duration
        if durations is not None:
            doc_dur = durations[idxs]
            dur_dict = defaultdict(int)
            all_dur = sum(doc_dur)
            for l, dur in zip(doc_distrib, doc_dur):
                dur_dict[l] += dur
            doc_durations = []
            for i in range(k):
                if i in dur_dict:
                    doc_durations.append(dur_dict[i] / all_dur)
                else:
                    doc_durations.append(0)
            #print(id, doc_durations)

        features = id.split('-')
        if 'duration' in feature_grid and durations is not None:
            features = features + doc_durations
        if 'counts' in feature_grid:
            features = features + counts
        if 'embeds' in feature_grid:
            features = features + list(embeds)
        if 'centroid_embeds' in feature_grid:
            features = features + list(centroid_embeds)
        if 'embed_velocity' in feature_grid:
            features = features + list(embed_velocities)
        if 'centroid_velocity' in feature_grid and kmeans:
            features = features + list(centroid_velocities)
        data.append(features)
    return data
Beispiel #34
0
  def _get_sparse_rpn_targets(self, coords, anchors, anchor_coords, gt_boxes, gt_rotations,
                              gt_classes, rpn_strides):
    def _split_layer(target, num_layer_anchors):
      assert num_layer_anchors[-1] == target.shape[0]
      return np.split(target, num_layer_anchors)
    num_layer_anchors = np.cumsum([x.shape[0] for x in anchor_coords])
    # Precompute anchors that may potentially hit based on input coordinates.
    anchor_dcoords = np.floor(np.concatenate(anchor_coords) / rpn_strides[-1]).astype(int)
    anchor_ddim = anchor_dcoords.max(0) - anchor_dcoords.min(0) + 2
    anchor_idxs = np.ravel_multi_index((anchor_dcoords - anchor_dcoords.min(0)).T, anchor_ddim)
    anchor_kernel_perm = np.array(list(itertools.product(*[range(-1, 2)] * 3)))
    anchor_masks = []
    for coord in coords:
      anchor_hit = np.full(np.prod(anchor_ddim), False)
      coords_dcoords = np.unique(np.floor(coord / rpn_strides[-1]).astype(int), axis=0)
      coords_perm = (np.tile(np.expand_dims(coords_dcoords, 0), (3 ** 3, 1, 1))
                     + np.expand_dims(anchor_kernel_perm, 1)).reshape(-1, 3)
      coords_idxs = np.ravel_multi_index((coords_perm - anchor_dcoords.min(0)).T, anchor_ddim)
      anchor_hit[coords_idxs] = True
      anchor_masks.append(anchor_hit[anchor_idxs])
    rpn_match, rpn_bbox, rpn_rotation, rpn_cls = self._get_rpn_targets(
        anchors, gt_boxes, gt_rotations, gt_classes, anchor_masks=anchor_masks)
    # Split dense regression targets based on layer index.
    anchors = _split_layer(anchors, num_layer_anchors)
    num_anchors = self.anchor_ratios.shape[0]
    num_batch = len(rpn_match)
    anchors_layer = []
    anchor_coords_layer = []
    rpn_match_layer = []
    rpn_bbox_layer = []
    rpn_cls_layer = []
    rpn_rot_layer = []
    for batch_idx in range(num_batch):
      mask_layer = _split_layer(anchor_masks[batch_idx], num_layer_anchors)
      batch_layer_anchors = np.cumsum([i.sum() for i in mask_layer][:-1])
      rpn_positive_mask = np.where(rpn_match[batch_idx] == 1)
      rpn_aligned_positive_mask = np.where(~np.all(rpn_bbox[batch_idx] == 0, -1))

      anchors_layer.append([ac[ml] for ac, ml in zip(anchors, mask_layer)])
      anchor_coords_layer.append([ac[ml] for ac, ml in zip(anchor_coords, mask_layer)])
      rpn_match_layer.append(_split_layer(rpn_match[batch_idx], batch_layer_anchors))

      rpn_bbox_aligned = np.zeros_like(rpn_bbox[batch_idx])
      rpn_bbox_aligned[rpn_positive_mask] = rpn_bbox[batch_idx][rpn_aligned_positive_mask]
      rpn_bbox_aligned = _split_layer(rpn_bbox_aligned, batch_layer_anchors)
      rpn_bbox_layer.append(rpn_bbox_aligned)

      rpn_cls_aligned = np.ones_like(rpn_cls[batch_idx]) * -1
      rpn_cls_aligned[rpn_positive_mask] = rpn_cls[batch_idx][rpn_aligned_positive_mask]
      rpn_cls_aligned = _split_layer(rpn_cls_aligned, batch_layer_anchors)
      rpn_cls_layer.append(rpn_cls_aligned)

      if rpn_rotation is not None:
        rpn_rotation_aligned = np.zeros_like(rpn_rotation[batch_idx])
        rpn_rotation_aligned[rpn_positive_mask] = rpn_rotation[batch_idx][rpn_aligned_positive_mask]
        rpn_rotation_aligned = _split_layer(rpn_rotation_aligned, batch_layer_anchors)
        rpn_rot_layer.append(rpn_rotation_aligned)
    anchors_layer = list(zip(*anchors_layer))
    anchor_coords_layer = list(zip(*anchor_coords_layer))
    rpn_match_layer = list(zip(*rpn_match_layer))
    rpn_bbox_layer = list(zip(*rpn_bbox_layer))
    rpn_cls_layer = list(zip(*rpn_cls_layer))
    rpn_rot_layer = None if rpn_rotation is None else list(zip(*rpn_rot_layer))
    # Accumulate regression target in sparse tensor format.
    sparse_anchor_centers = []
    sparse_anchor_coords = []
    sparse_rpn_matches = []
    sparse_rpn_bboxes = []
    sparse_rpn_cls = []
    sparse_rpn_rotations = [] if rpn_rotation is not None else None
    for layer_idx in range(len(anchor_coords)):
      tensor_stride = rpn_strides[layer_idx]
      sparse_anchor_centers.append(
          torch.from_numpy(np.vstack(anchors_layer[layer_idx]).reshape(-1, num_anchors * 6)))
      sub_anchor_coords = ME.utils.batched_coordinates([ac[::num_anchors] for ac
                                                        in anchor_coords_layer[layer_idx]])
      sub_anchor_coords[:, 1:] -= tensor_stride // 2
      sparse_anchor_coords.append(sub_anchor_coords)
      sparse_rpn_matches.append(
          torch.from_numpy(np.concatenate(rpn_match_layer[layer_idx]).reshape(-1, num_anchors)))
      sparse_rpn_bboxes.append(
          torch.from_numpy(np.concatenate(rpn_bbox_layer[layer_idx]).reshape(-1, num_anchors * 6)))
      sparse_rpn_cls.append(
          torch.from_numpy(np.concatenate(rpn_cls_layer[layer_idx]).reshape(-1, num_anchors)))
      if sparse_rpn_rotations is not None:
        sparse_rpn_rotations.append(
            torch.from_numpy(np.concatenate(rpn_rot_layer[layer_idx]).reshape(-1, num_anchors)))
    # Precompute positive/ambiguous target coordinates for sparse generative RPN.
    anchor_match_coords = []
    for batch_idx in range(num_batch):
      batch_match_coords = []
      for layer_idx in range(len(anchor_coords)):
        layer_match = rpn_match_layer[layer_idx][batch_idx]
        layer_coords = anchor_coords_layer[layer_idx][batch_idx]
        positive_match = np.where(layer_match == 1)
        if np.any(positive_match):
          positive_coords = np.unique(layer_coords[positive_match], axis=0)
        else:
          positive_coords = np.zeros((0, 3))
        ambiguous_match = np.where(layer_match == 0)
        if np.any(ambiguous_match):
          ambiguous_coords = np.unique(layer_coords[ambiguous_match], axis=0)
        else:
          ambiguous_coords = np.zeros((0, 3))
        batch_match_coords.append((positive_coords, ambiguous_coords))
      anchor_match_coords.append(batch_match_coords)
    return (anchor_match_coords, sparse_anchor_centers, sparse_anchor_coords, sparse_rpn_matches,
            sparse_rpn_bboxes, sparse_rpn_rotations, sparse_rpn_cls)
    print(np.max(a))
    print(count)
    a = np.array(a)[np.array(a)>0].astype(np.int)
    count+=1
    img_mont_ = all_masks_gt[np.array(a)].squeeze()
    shps_img = img_mont_.shape
    img_mont = montage2d(img_mont_)
    shps_img_mont = np.array(img_mont.shape)//50
    pl.figure(figsize=(20,30))
    pl.imshow(img_mont)    
    inp = pl.ginput(n=0,timeout = -100000)    
    imgs_to_exclude = []
    inp = np.ceil(np.array(inp)/50).astype(np.int)-1
    if len(inp)>0:
        
        imgs_to_exclude = img_mont_[np.ravel_multi_index([inp[:,1],inp[:,0]],shps_img_mont)]
#        pl.imshow(montage2d(imgs_to_exclude))    
        wrong.append(np.array(a)[np.ravel_multi_index([inp[:,1],inp[:,0]],shps_img_mont)])
    np.save('temp_label_pos_minions.npy',wrong)    
    pl.close()
#%%
pl.imshow(montage2d(all_masks_gt[np.concatenate(wrong)].squeeze()))
#%% 
lab_pos_wrong = np.load('temp_label_pos_minions.npy')
lab_neg_wrong = np.load('temp_label_neg_plus_minions.npy')

labels_gt_cur = labels_gt.copy()
labels_gt_cur[np.concatenate(lab_pos_wrong)] = 0
labels_gt_cur[np.concatenate(lab_neg_wrong)] = 1

np.savez('ground_truth_comoponents_curated_minions.npz',all_masks_gt = all_masks_gt,labels_gt_cur = labels_gt_cur)
Beispiel #36
0
def _action_from_move(move: chess.Move) -> Action:
    return np.ravel_multi_index(
        (move.from_square, move.to_square, move.promotion != None),
        _action_shape)
Beispiel #37
0
 def getIndex(self, s):
     return np.ravel_multi_index(s, self.state_shape)
Beispiel #38
0
def bincount_app(a):
    a2D = a.reshape(-1, a.shape[-1])
    col_range = (256, 256, 256)  # generically : a2D.max(0)+1
    a1D = np.ravel_multi_index(a2D.T, col_range)
    return np.unravel_index(np.bincount(a1D).argmax(), col_range)
def frontalize(img, proj_matrix, ref_U, eyemask):
    ACC_CONST = 800
    img = img.astype('float32')
    print("query image shape:", img.shape)

    bgind = np.sum(np.abs(ref_U), 2) == 0
    # count the number of times each pixel in the query is accessed
    threedee = np.reshape(ref_U, (-1, 3), order='F').transpose()
    temp_proj = proj_matrix * np.vstack(
        (threedee, np.ones((1, threedee.shape[1]))))
    temp_proj2 = np.divide(temp_proj[0:2, :], np.tile(temp_proj[2, :], (2, 1)))

    bad = np.logical_or(
        temp_proj2.min(axis=0) < 1, temp_proj2[1, :] > img.shape[0])
    bad = np.logical_or(bad, temp_proj2[0, :] > img.shape[1])
    bad = np.logical_or(bad, bgind.reshape((-1), order='F'))
    bad = np.asarray(bad).reshape((-1), order='F')

    temp_proj2 -= 1

    badind = np.nonzero(bad > 0)[0]
    temp_proj2[:, badind] = 0

    ind = np.ravel_multi_index(
        (np.asarray(temp_proj2[1, :].round(), dtype='int64'),
         np.asarray(temp_proj2[0, :].round(), dtype='int64')),
        dims=img.shape[:-1],
        order='F')

    synth_frontal_acc = np.zeros(ref_U.shape[:-1])
    ind_frontal = np.arange(0, ref_U.shape[0] * ref_U.shape[1])

    c, ic = np.unique(ind, return_inverse=True)
    bin_edges = np.r_[-np.Inf, 0.5 * (c[:-1] + c[1:]), np.Inf]
    count, bin_edges = np.histogram(ind, bin_edges)
    synth_frontal_acc = synth_frontal_acc.reshape(-1, order='F')
    synth_frontal_acc[ind_frontal] = count[ic]
    synth_frontal_acc = synth_frontal_acc.reshape((320, 320), order='F')
    synth_frontal_acc[bgind] = 0
    synth_frontal_acc = cv2.GaussianBlur(synth_frontal_acc, (15, 15),
                                         30.,
                                         borderType=cv2.BORDER_REPLICATE)

    #remap
    mapX = temp_proj2[0, :].astype(np.float32)
    mapY = temp_proj2[1, :].astype(np.float32)

    mapX = np.reshape(mapX, (-1, 320), order='F')
    mapY = np.reshape(mapY, (-1, 320), order='F')

    frontal_raw = cv2.remap(img, mapX, mapY, cv2.INTER_CUBIC)

    frontal_raw = frontal_raw.reshape((-1, 3), order='F')
    frontal_raw[badind, :] = 0
    frontal_raw = frontal_raw.reshape((320, 320, 3), order='F')

    # which side has more occlusions?
    midcolumn = np.round(ref_U.shape[1] / 2)
    sumaccs = synth_frontal_acc.sum(axis=0)
    sum_left = sumaccs[0:midcolumn].sum()
    sum_right = sumaccs[midcolumn + 1:].sum()
    sum_diff = sum_left - sum_right

    if np.abs(sum_diff) > ACC_CONST:  # one side is ocluded
        ones = np.ones((ref_U.shape[0], midcolumn))
        zeros = np.zeros((ref_U.shape[0], midcolumn))
        if sum_diff > ACC_CONST:  # left side of face has more occlusions
            weights = np.hstack((zeros, ones))
        else:  # right side of face has more occlusions
            weights = np.hstack((ones, zeros))
        weights = cv2.GaussianBlur(weights, (33, 33),
                                   60.5,
                                   borderType=cv2.BORDER_REPLICATE)

        # apply soft symmetry to use whatever parts are visible in ocluded side
        synth_frontal_acc /= synth_frontal_acc.max()
        weight_take_from_org = 1. / np.exp(0.5 + synth_frontal_acc)
        weight_take_from_sym = 1 - weight_take_from_org

        weight_take_from_org = np.multiply(weight_take_from_org,
                                           np.fliplr(weights))
        weight_take_from_sym = np.multiply(weight_take_from_sym,
                                           np.fliplr(weights))

        weight_take_from_org = np.tile(
            weight_take_from_org.reshape(320, 320, 1), (1, 1, 3))
        weight_take_from_sym = np.tile(
            weight_take_from_sym.reshape(320, 320, 1), (1, 1, 3))
        weights = np.tile(weights.reshape(320, 320, 1), (1, 1, 3))

        denominator = weights + weight_take_from_org + weight_take_from_sym
        frontal_sym = np.multiply(frontal_raw, weights) + np.multiply(
            frontal_raw, weight_take_from_org) + np.multiply(
                np.fliplr(frontal_raw), weight_take_from_sym)
        frontal_sym = np.divide(frontal_sym, denominator)

        # exclude eyes from symmetry
        frontal_sym = np.multiply(frontal_sym, 1 - eyemask) + np.multiply(
            frontal_raw, eyemask)
        frontal_raw[frontal_raw > 255] = 255
        frontal_raw[frontal_raw < 0] = 0
        frontal_raw = frontal_raw.astype('uint8')
        frontal_sym[frontal_sym > 255] = 255
        frontal_sym[frontal_sym < 0] = 0
        frontal_sym = frontal_sym.astype('uint8')
    else:  # both sides are occluded pretty much to the same extent -- do not use symmetry
        frontal_sym = frontal_raw
    return frontal_raw, frontal_sym
Beispiel #40
0
    def _get_multi_index(self, arr, indices):
        """Mimic multi dimensional indexing.

        Parameters
        ----------
        arr : ndarray
            Array to be indexed.
        indices : tuple of index objects

        Returns
        -------
        out : ndarray
            An array equivalent to the indexing operation (but always a copy).
            `arr[indices]` should be identical.
        no_copy : bool
            Whether the indexing operation requires a copy. If this is `True`,
            `np.may_share_memory(arr, arr[indicies])` should be `True` (with
            some exceptions for scalars and possibly 0-d arrays).

        Notes
        -----
        While the function may mostly match the errors of normal indexing this
        is generally not the case.
        """
        in_indices = list(indices)
        indices = []
        # if False, this is a fancy or boolean index
        no_copy = True
        # number of fancy/scalar indexes that are not consecutive
        num_fancy = 0
        # number of dimensions indexed by a "fancy" index
        fancy_dim = 0
        # NOTE: This is a funny twist (and probably OK to change).
        # The boolean array has illegal indexes, but this is
        # allowed if the broadcast fancy-indices are 0-sized.
        # This variable is to catch that case.
        error_unless_broadcast_to_empty = False

        # We need to handle Ellipsis and make arrays from indices, also
        # check if this is fancy indexing (set no_copy).
        ndim = 0
        ellipsis_pos = None  # define here mostly to replace all but first.
        for i, indx in enumerate(in_indices):
            if indx is None:
                continue
            if isinstance(indx, np.ndarray) and indx.dtype == bool:
                no_copy = False
                if indx.ndim == 0:
                    raise IndexError
                # boolean indices can have higher dimensions
                ndim += indx.ndim
                fancy_dim += indx.ndim
                continue
            if indx is Ellipsis:
                if ellipsis_pos is None:
                    ellipsis_pos = i
                    continue  # do not increment ndim counter
                raise IndexError
            if isinstance(indx, slice):
                ndim += 1
                continue
            if not isinstance(indx, np.ndarray):
                # This could be open for changes in numpy.
                # numpy should maybe raise an error if casting to intp
                # is not safe. It rejects np.array([1., 2.]) but not
                # [1., 2.] as index (same for ie. np.take).
                # (Note the importance of empty lists if changing this here)
                indx = np.array(indx, dtype=np.intp)
                in_indices[i] = indx
            elif indx.dtype.kind != 'b' and indx.dtype.kind != 'i':
                raise IndexError(
                    'arrays used as indices must be of integer (or boolean) type'
                )
            if indx.ndim != 0:
                no_copy = False
            ndim += 1
            fancy_dim += 1

        if arr.ndim - ndim < 0:
            # we can't take more dimensions then we have, not even for 0-d arrays.
            # since a[()] makes sense, but not a[(),]. We will raise an error
            # later on, unless a broadcasting error occurs first.
            raise IndexError

        if ndim == 0 and None not in in_indices:
            # Well we have no indexes or one Ellipsis. This is legal.
            return arr.copy(), no_copy

        if ellipsis_pos is not None:
            in_indices[ellipsis_pos:ellipsis_pos +
                       1] = [slice(None, None)] * (arr.ndim - ndim)

        for ax, indx in enumerate(in_indices):
            if isinstance(indx, slice):
                # convert to an index array
                indx = np.arange(*indx.indices(arr.shape[ax]))
                indices.append(['s', indx])
                continue
            elif indx is None:
                # this is like taking a slice with one element from a new axis:
                indices.append(['n', np.array([0], dtype=np.intp)])
                arr = arr.reshape((arr.shape[:ax] + (1, ) + arr.shape[ax:]))
                continue
            if isinstance(indx, np.ndarray) and indx.dtype == bool:
                if indx.shape != arr.shape[ax:ax + indx.ndim]:
                    raise IndexError

                try:
                    flat_indx = np.ravel_multi_index(np.nonzero(indx),
                                                     arr.shape[ax:ax +
                                                               indx.ndim],
                                                     mode='raise')
                except:
                    error_unless_broadcast_to_empty = True
                    # fill with 0s instead, and raise error later
                    flat_indx = np.array([0] * indx.sum(), dtype=np.intp)
                # concatenate axis into a single one:
                if indx.ndim != 0:
                    arr = arr.reshape(
                        (arr.shape[:ax] +
                         (np.prod(arr.shape[ax:ax + indx.ndim]), ) +
                         arr.shape[ax + indx.ndim:]))
                    indx = flat_indx
                else:
                    # This could be changed, a 0-d boolean index can
                    # make sense (even outside the 0-d indexed array case)
                    # Note that originally this is could be interpreted as
                    # integer in the full integer special case.
                    raise IndexError
            else:
                # If the index is a singleton, the bounds check is done
                # before the broadcasting. This used to be different in <1.9
                if indx.ndim == 0:
                    if indx >= arr.shape[ax] or indx < -arr.shape[ax]:
                        raise IndexError
            if indx.ndim == 0:
                # The index is a scalar. This used to be two fold, but if fancy
                # indexing was active, the check was done later, possibly
                # after broadcasting it away (1.7. or earlier). Now it is always
                # done.
                if indx >= arr.shape[ax] or indx < -arr.shape[ax]:
                    raise IndexError
            if len(indices
                   ) > 0 and indices[-1][0] == 'f' and ax != ellipsis_pos:
                # NOTE: There could still have been a 0-sized Ellipsis
                # between them. Checked that with ellipsis_pos.
                indices[-1].append(indx)
            else:
                # We have a fancy index that is not after an existing one.
                # NOTE: A 0-d array triggers this as well, while
                # one may expect it to not trigger it, since a scalar
                # would not be considered fancy indexing.
                num_fancy += 1
                indices.append(['f', indx])

        if num_fancy > 1 and not no_copy:
            # We have to flush the fancy indexes left
            new_indices = indices[:]
            axes = list(range(arr.ndim))
            fancy_axes = []
            new_indices.insert(0, ['f'])
            ni = 0
            ai = 0
            for indx in indices:
                ni += 1
                if indx[0] == 'f':
                    new_indices[0].extend(indx[1:])
                    del new_indices[ni]
                    ni -= 1
                    for ax in range(ai, ai + len(indx[1:])):
                        fancy_axes.append(ax)
                        axes.remove(ax)
                ai += len(indx) - 1  # axis we are at
            indices = new_indices
            # and now we need to transpose arr:
            arr = arr.transpose(*(fancy_axes + axes))

        # We only have one 'f' index now and arr is transposed accordingly.
        # Now handle newaxis by reshaping...
        ax = 0
        for indx in indices:
            if indx[0] == 'f':
                if len(indx) == 1:
                    continue
                # First of all, reshape arr to combine fancy axes into one:
                orig_shape = arr.shape
                orig_slice = orig_shape[ax:ax + len(indx[1:])]
                arr = arr.reshape(
                    (arr.shape[:ax] + (np.prod(orig_slice).astype(int), ) +
                     arr.shape[ax + len(indx[1:]):]))

                # Check if broadcasting works
                if len(indx[1:]) != 1:
                    res = np.broadcast(*indx[1:])  # raises ValueError...
                else:
                    res = indx[1]
                # unfortunately the indices might be out of bounds. So check
                # that first, and use mode='wrap' then. However only if
                # there are any indices...
                if res.size != 0:
                    if error_unless_broadcast_to_empty:
                        raise IndexError
                    for _indx, _size in zip(indx[1:], orig_slice):
                        if _indx.size == 0:
                            continue
                        if np.any(_indx >= _size) or np.any(_indx < -_size):
                            raise IndexError
                if len(indx[1:]) == len(orig_slice):
                    if np.product(orig_slice) == 0:
                        # Work around for a crash or IndexError with 'wrap'
                        # in some 0-sized cases.
                        try:
                            mi = np.ravel_multi_index(indx[1:],
                                                      orig_slice,
                                                      mode='raise')
                        except:
                            # This happens with 0-sized orig_slice (sometimes?)
                            # here it is a ValueError, but indexing gives a:
                            raise IndexError('invalid index into 0-sized')
                    else:
                        mi = np.ravel_multi_index(indx[1:],
                                                  orig_slice,
                                                  mode='wrap')
                else:
                    # Maybe never happens...
                    raise ValueError
                arr = arr.take(mi.ravel(), axis=ax)
                arr = arr.reshape(
                    (arr.shape[:ax] + mi.shape + arr.shape[ax + 1:]))
                ax += mi.ndim
                continue

            # If we are here, we have a 1D array for take:
            arr = arr.take(indx[1], axis=ax)
            ax += 1

        return arr, no_copy
Beispiel #41
0
        if rand == 1:
            d_x = d_y = (snr * m)**0.5
            hat_r_s = hat_r_s_naive_fixed_stim(r_n, r_s, d_x, d_y, n_x, n_y, n,
                                               m)
        else:
            hat_r_s = hat_r_s_naive_rand_stim(r_n, r_s, d_x, d_y, n_x, n_y, n)
        da.loc[snr, n, r_s, r_n] = hat_r_s
    das.append(da)
    plt.figure(figsize=(5.6, 5.6))
    ticks = np.linspace(-1, 1, 9)
    #tick_labels = ['' , '-0.5', '' , '-1'][::-1]+['0', '', '0.5', '', '1']
    for i in range(3):
        for j in range(3):

            dat = da[:, i, j]
            k = np.ravel_multi_index((i, j), dims=(3, 3))
            plt.subplot(3, 3, k + 1)

            for q in range(3):
                plt.plot(dat.coords['r_n'].values, dat[q], color=colors[q])
            plt.axis('square')
            plt.ylim(-1, 1)
            plt.xlim(-1, 1)
            plt.xticks(ticks)
            plt.yticks(ticks)
            #plt.grid()

            if i == 0:
                plt.title('\n \n' + '$r_s$=' + str(dat.coords['r_s'].values))
            if j == 2:
                plt.ylabel('$n=$' + str(dat.coords['n'].values),
def dwel_image_random_sampling(maskfile, outfile, classes=None, numsamples=100, \
                                   maskb=1, ancfile=None, ancb=1, \
                                   maskcircle=None):
    """
    Generate random samples from a mask image file of DWEL data. Currently only
    support simply random sampling design.

    Args:

        maskfile: mask image file. 1 (not 0) is valid pixels where random
        samples will be drawn from.

        maskb (int): band index of mask in the mask file, with first band being
        1.

        ancb (int): band index of anciilary attribute to attach to random
        samples, with first band being 1.

        maskcircle (tuple): 3-element tuple giving (center_x, center_y, radius)
        of a circle within which random samples are drawn from.
    
    Returns:

    """
    # read mask file
    maskds = gdal.Open(maskfile, gdal.GA_ReadOnly)
    maskband = maskds.GetRasterBand(maskb)
    mask = maskband.ReadAsArray(0, 0, maskband.XSize, maskband.YSize).astype(np.int)
    # read ancillary attribute band if it is given
    addatt = False
    if not (ancfile is None):
        ancds = gdal.Open(ancfile, gdal.GA_ReadOnly)
        ancband = ancds.GetRasterBand(ancb)
        if (ancband.XSize != maskband.XSize) or (ancband.YSize != maskband.YSize):
            print "Warning: Dimension of ancillary attribute image differs from input mask image."
            print "No attribute will be added"
        else:
            ancatt = ancband.ReadAsArray(0, 0, ancband.XSize, ancband.YSize)
            addatt = True
        ancds = None

    maskds = None

    validx, validy = np.nonzero(mask)
    # if a circle is given to define the area for random samples
    if maskcircle is not None:
        tmpflag = (validx - maskcircle[0])**2 + (validy - maskcircle[1])**2 <= maskcircle[2]**2
        validx = validx[tmpflag]
        validy = validy[tmpflag]

    # get the indices of valid samples for all classes
    validind = np.ravel_multi_index((validx, validy), \
                                        (maskband.YSize, maskband.XSize))
    if classes is None:
        if len(validind) >= numsamples:
            randind = np.random.permutation(len(validind))[:numsamples]
        else:
            print "Error: number of valid pixels is fewer than requested number of samples"
            print "No samples are generated. Exit"
            maskds = None
            return ()
        sampleind = validind[randind]
    else:
        numclass = len(classes)
        if numclass != len(numsamples):
            maskds = None
            raise RuntimeError('Number of classes is {0:d} while only {1:d} numbers for sample counts are given!'.format(numclass, len(numsamples)))
        else:
            mask_vec = mask.flatten()[validind]
            sampleind = [ draw_samples(validind[mask_vec==cls], ns) for cls, ns in zip(classes, numsamples) ]
            sampleind = np.hstack(sampleind)


    if addatt:
        ancatt = np.ravel(ancatt, order='C')
        sampleatt = ancatt[sampleind]
    samplepos = np.unravel_index(sampleind, (maskband.YSize, maskband.XSize), order='C')

    # convert pixel location to default coordinates for a raster in QGIS.
    ypos = samplepos[0].astype(float)*(-1) - 0.5
    xpos = samplepos[1].astype(float) + 0.5

    # write sampels to output file and attribute if they are supplied
    headerstr = \
        "Random samples from " + maskfile + "\n" + \
        "Band of mask = {0:d}\n".format(maskb) + \
        "Class code = "
    for cls in classes:
        headerstr += "{0:d}, ".format(cls)
    headerstr = headerstr.rstrip(', ') + "\nNumber of samples = "
    for ns in numsamples:
        headerstr += "{0:d}, ".format(ns)
    headerstr = headerstr.rstrip(', ') + "\n"

    numsamples = len(sampleind)
    if addatt:
        outmat = np.hstack(( xpos.reshape(numsamples, 1), \
                                 ypos.reshape(numsamples, 1), \
                                 sampleatt.reshape(numsamples, 1) ))
        headerstr = headerstr + \
            "Attributes from " + ancfile + "\n" + \
            "Band of attribute = {0:d}\n".format(ancb) + \
            "X, Y, Attribute"
        fmtstr = ['%.3f', '%.3f', '%.3f']
    else:
        outmat = np.hstack(( xpos.reshape(numsamples, 1), \
                                 ypos.reshape(numsamples, 1) ))
        headerstr = headerstr + \
            "Attributes None\n" + \
            "Band of attribute = None\n" + \
            "X, Y"
        fmtstr = ['%.3f', '%.3f']

    np.savetxt(outfile, outmat, fmt=fmtstr, delimiter=',', header=headerstr, comments="")
Beispiel #43
0
def lonlat_2_ind(lon,lat,extent,cellsize,dims):
    row,col = lonlat_2_rowcol(lon,lat,extent,cellsize)
    ind = np.ravel_multi_index((row, col), dims) 
    return ind
    def precomputed_analysis_vote_cls(self, num_fts=4096, dense=True):
        def lambda_func(arr, patch_size):
            res = skimage.util.view_as_windows(
                arr, (patch_size, patch_size, patch_size, patch_size, 2))
            res = np.squeeze(res, 4)
            return res

        #print("Starting Analysis")
        assert not self.auto_close_sess, "Need to keep sess open"

        feature_response = self.run_ft(num_fts=num_fts)

        flattened_features = feature_response.reshape((num_fts, -1)).T

        # Use np.unravel_index to recover x,y coordinate
        spread = max(1, self.patch_size // self.stride)

        responses = np.zeros(
            (self.max_h_ind + spread - 1, self.max_w_ind + spread - 1,
             self.max_h_ind + spread - 1, self.max_w_ind + spread - 1),
            dtype=np.float32)
        vote_counts = np.zeros(
            (self.max_h_ind + spread - 1, self.max_w_ind + spread - 1,
             self.max_h_ind + spread - 1, self.max_w_ind + spread - 1)) + 1e-4
        t0 = time.time()

        # Get all possible combinations of patches
        iterator = self.argless_extract_inds(dense=dense)
        futures = []
        inds_cpy = []
        # responses_vote_count_as_patches = self.pool.submit(lambda_func,responses,spread)
        for inds in iterator:
            # print(inds.shape)
            patch_a_inds = inds[:, :2]
            patch_b_inds = inds[:, 2:]

            a_ind = np.ravel_multi_index(patch_a_inds.T,
                                         [self.max_h_ind, self.max_w_ind])
            b_ind = np.ravel_multi_index(patch_b_inds.T,
                                         [self.max_h_ind, self.max_w_ind])
            futures.append(
                self.pool.submit(self.solver.sess.run,
                                 self.solver.net.pc_cls_pred,
                                 feed_dict={
                                     self.net.precomputed_features:
                                     flattened_features,
                                     self.net.im_a_index: a_ind,
                                     self.net.im_b_index: b_ind
                                 }))

            inds_cpy.append(inds)

        # responses_vote_count_as_patches = responses_vote_count_as_patches.result()

        for idx, future in enumerate(futures):
            preds_ = future.result()
            inds = inds_cpy[idx]
            t = time.time()
            # responses_vote_count_as_patches[inds] += [preds_[i],1]
            for i in range(preds_.shape[0]):
                responses[inds[i][0]:(inds[i][0] + spread),
                          inds[i][1]:(inds[i][1] + spread),
                          inds[i][2]:(inds[i][2] + spread),
                          inds[i][3]:(inds[i][3] + spread)] += preds_[i]
                vote_counts[inds[i][0]:(inds[i][0] + spread),
                            inds[i][1]:(inds[i][1] + spread),
                            inds[i][2]:(inds[i][2] + spread),
                            inds[i][3]:(inds[i][3] + spread)] += 1
            #print('precomputed analysis time : {}'.format(time.time() - t))
        if self.auto_close_sess:
            self.solver.sess.close()
        out = (responses / vote_counts)
        # self.cr.queue.close()
        return out
Beispiel #45
0
    def step(self, action):
        """
        Updates the environment with the given action
        
        Takes as inputs
        - action : array of value € [0,1] representing an action
        Returns
        - observable : 3d numpy array of shape (height, width, channels) representing the new state of the environment
        - reward : reward given to the agent for the performed action
        - done : boolean indicating if new state is a terminal state
        - infos : dictionary of informations (for debugging purpose)
        """

        assert self.target is not None, "The target not define, to do so reset the environment"

        if self.state.step_type == environment.StepType.LAST:
            return (self.getObservable(),
                    1 / ((LibMyPaintInterface._distance_l2(self.state.observation["canvas"], self.target) + 1e-9) / self.max_norm),
                    True,
                    {"info": "To continue reset the environment"})

        self.actions.append(action)

        action_spec = self.env.action_spec()

        # extract the values
        if self.coord_type == "cart":
            x_start, y_start, x_control, y_control, x_end, y_end, brush_pressure, brush_size, color_1, color_2, color_3 = action
            # map the coordinates to the right interval
            x_start = LibMyPaintInterface._map_to_int_interval(x_start, 0, self.grid_size - 1)
            y_start = LibMyPaintInterface._map_to_int_interval(y_start, 0, self.grid_size - 1)
            x_control = LibMyPaintInterface._map_to_int_interval(x_control, 0, self.grid_size - 1)
            y_control = LibMyPaintInterface._map_to_int_interval(y_control, 0, self.grid_size - 1)
            x_end = LibMyPaintInterface._map_to_int_interval(x_end, 0, self.grid_size - 1)
            y_end = LibMyPaintInterface._map_to_int_interval(y_end, 0, self.grid_size - 1)

        elif self.coord_type == "hilb":
            start, control, end, brush_pressure, brush_size, color_1, color_2, color_3 = action
            # map the coordonates to the right interval
            start = int(LibMyPaintInterface._map_to_int_interval(start, 0, pow(2, m.log(self.grid_size, 2) * 2) - 1))
            control = int(LibMyPaintInterface._map_to_int_interval(control, 0, pow(2, m.log(self.grid_size, 2) * 2) - 1))
            end = int(LibMyPaintInterface._map_to_int_interval(end, 0, pow(2, m.log(self.grid_size, 2) * 2) - 1))
            x_start, y_start = self.hilbert_curve.coordinates_from_distance(start)
            x_control, y_control = self.hilbert_curve.coordinates_from_distance(control)
            x_end, y_end = self.hilbert_curve.coordinates_from_distance(end)

        brush_pressure = LibMyPaintInterface._map_to_int_interval(brush_pressure,
                                                                  action_spec["pressure"].minimum,
                                                                  action_spec["pressure"].maximum)

        brush_size = LibMyPaintInterface._map_to_int_interval(brush_size,
                                                              action_spec["size"].minimum,
                                                              action_spec["size"].maximum)

        color_1 = LibMyPaintInterface._map_to_int_interval(color_1,
                                                           action_spec[self.color_name[0]].minimum,
                                                           action_spec[self.color_name[1]].maximum)

        color_2 = LibMyPaintInterface._map_to_int_interval(color_2,
                                                           action_spec[self.color_name[1]].minimum,
                                                           action_spec[self.color_name[1]].maximum)

        color_3 = LibMyPaintInterface._map_to_int_interval(color_3,
                                                           action_spec[self.color_name[2]].minimum,
                                                           action_spec[self.color_name[2]].maximum)

        # go to the start of the curve
        move = {"control": np.ravel_multi_index((x_start, y_start),
                                                (self.grid_size, self.grid_size)).astype("int32"),
                "end": np.ravel_multi_index((x_start, y_start),
                                            (self.grid_size, self.grid_size)).astype("int32"),
                "flag": np.int32(0),
                "pressure": np.int32(brush_pressure),
                "size": np.int32(brush_size),
                self.color_name[0]: np.int32(color_1),
                self.color_name[1]: np.int32(color_2),
                self.color_name[2]: np.int32(color_3)}
        self.state = self.env.step(move)

        if self.state.step_type == environment.StepType.LAST:
            return (self.getObservable(),
                    1 / ((LibMyPaintInterface._distance_l2(self.state.observation["canvas"], self.target) + 1e-9) / self.max_norm),
                    True,
                    {})

        # draw the curve
        draw = {"control": np.ravel_multi_index((x_control, y_control),
                                                (self.grid_size, self.grid_size)).astype("int32"),
                "end": np.ravel_multi_index((x_end, y_end),
                                            (self.grid_size, self.grid_size)).astype("int32"),
                "flag": np.int32(1),
                "pressure": np.int32(brush_pressure),
                "size": np.int32(brush_size),
                self.color_name[0]: np.int32(color_1),
                self.color_name[1]: np.int32(color_2),
                self.color_name[2]: np.int32(color_3)}
        self.state = self.env.step(draw)

        if self.state.step_type == environment.StepType.LAST:
            return (self.getObservable(),
                    1 / ((LibMyPaintInterface._distance_l2(self.state.observation["canvas"], self.target) + 1e-9) / self.max_norm),
                    True,
                    {})

        return (self.getObservable(),
                0,
                False,
                {})
def bin_to_idx(bin_loc, resolution):
    return np.ravel_multi_index((bin_loc[0], bin_loc[1]), resolution)
Beispiel #47
0
def matlab_compat_ravel_multi_index(*xyz, shape):
    return 1 + np.ravel_multi_index(xyz, shape, order='F')
Beispiel #48
0
    def marginal_prob(self, prob, wires=None):
        r"""Return the marginal probability of the computational basis
        states by summing the probabiliites on the non-specified wires.

        If no wires are specified, then all the basis states representable by
        the device are considered and no marginalization takes place.

        .. note::

            If the provided wires are not in the order as they appear on the device,
            the returned marginal probabilities take this permutation into account.

            For example, if the addressable wires on this device are ``Wires([0, 1, 2])`` and
            this function gets passed ``wires=[2, 0]``, then the returned marginal
            probability vector will take this 'reversal' of the two wires
            into account:

            .. math::

                \mathbb{P}^{(2, 0)}
                            = \left[
                               |00\rangle, |10\rangle, |01\rangle, |11\rangle
                              \right]

        Args:
            prob: The probabilities to return the marginal probabilities
                for
            wires (Iterable[Number, str], Number, str, Wires): wires to return
                marginal probabilities for. Wires not provided
                are traced out of the system.

        Returns:
            array[float]: array of the resulting marginal probabilities.
        """

        if wires is None:
            # no need to marginalize
            return prob

        wires = Wires(wires)
        # determine which subsystems are to be summed over
        inactive_wires = Wires.unique_wires([self.wires, wires])

        # translate to wire labels used by device
        device_wires = self.map_wires(wires)
        inactive_device_wires = self.map_wires(inactive_wires)

        # reshape the probability so that each axis corresponds to a wire
        prob = self._reshape(prob, [2] * self.num_wires)

        # sum over all inactive wires
        prob = self._flatten(
            self._reduce_sum(prob, inactive_device_wires.labels))

        # The wires provided might not be in consecutive order (i.e., wires might be [2, 0]).
        # If this is the case, we must permute the marginalized probability so that
        # it corresponds to the orders of the wires passed.
        basis_states = np.array(
            list(itertools.product([0, 1], repeat=len(device_wires))))
        perm = np.ravel_multi_index(
            basis_states[:, np.argsort(np.argsort(device_wires))].T,
            [2] * len(device_wires))
        return self._gather(prob, perm)
Beispiel #49
0
def chaccum(A,
            radiusRange,
            method='phasecode',
            objPolarity='bright',
            edgeThresh=[]):

    maxNumElemNHoodMat = 1e6

    # all(A(:) == A(1));
    # Assume that image is not flat

    # Get the input image in the correct format
    A = getGrayImage(A)

    ## Calculate gradient
    Gx, Gy, gradientImg = imgradient(A)

    Ex, Ey = getEdgePixels(gradientImg, edgeThresh)
    E = np.stack((Ey, Ex))

    # Not sure np.ravel_multi_index is equivalent with Matlab Function sub2ind
    idxE = np.ravel_multi_index(E,
                                tuple(gradientImg.shape),
                                mode='raise',
                                order='C')

    # Adding 0.0001 is to include end point of the radius
    radiusRange = np.arange(radiusRange[0], radiusRange[1] + 0.0001, 0.5)

    if objPolarity == 'bright':
        RR = radiusRange
    elif objPolarity == 'dark':
        RR = -radiusRange

    ### This implementation only includes 'phasecode' mode

    lnR = np.log(radiusRange)
    phi = ((lnR - lnR[0]) / (lnR[-1] - lnR[0]) * 2 * np.pi) - np.pi
    # Modified form of Log-coding from Eqn. 8 in [3]

    Opca = np.exp(np.sqrt(-1 + 0j) * phi)
    w0 = Opca / (2 * np.pi * radiusRange)

    xcStep = int(maxNumElemNHoodMat // RR.shape[0])

    lenE = Ex.shape[0]
    M, N = A.shape[0], A.shape[1]
    accumMatrix = np.zeros((M, N))

    for i in range(0, lenE + 1, xcStep):

        Ex_chunk = Ex[i:int(np.min((i + xcStep - 1, lenE)))]
        Ey_chunk = Ey[i:int(np.min((i + xcStep - 1, lenE)))]
        idxE_chunk = idxE[i:int(np.min((i + xcStep - 1, lenE)))]

        chunckX = np.divide(
            Gx[np.unravel_index(idxE_chunk, Gx.shape, 'C')],
            gradientImg[np.unravel_index(idxE_chunk, gradientImg.shape, 'C')])

        chunckY = np.divide(
            Gy[np.unravel_index(idxE_chunk, Gy.shape, 'C')],
            gradientImg[np.unravel_index(idxE_chunk, gradientImg.shape, 'C')])

        # Eqns. 10.3 & 10.4 from Machine Vision by E. R. Davies
        xc = matlib.repmat(Ex_chunk, RR.shape[0], 1).T + bsxfun_1d(
            -RR, chunckX)
        yc = matlib.repmat(Ey_chunk, RR.shape[0], 1).T + bsxfun_1d(
            -RR, chunckY)

        xc = np.round(xc)
        yc = np.round(yc)

        w = matlib.repmat(w0, xc.shape[0], 1)

        ## Determine which edge pixel votes are within the image domain
        # Record which candidate center positions are inside the image rectangle.
        M, N = A.shape[0], A.shape[1]

        inside = (xc >= 1) & (xc <= N) & (yc >= 1) & (yc < M)
        # Keep rows that have at least one candidate position inside the domain.
        rows_to_keep = np.any(inside, 1)
        xc = xc[rows_to_keep, :]
        yc = yc[rows_to_keep, :]
        w = w[rows_to_keep, :]
        inside = inside[rows_to_keep, :]

        ## Accumulate the votes in the parameter plane
        xc = xc[inside]
        yc = yc[inside]

        xyc = np.asarray(np.stack((xc[:], yc[:])), dtype=np.int64)

        accumMatrix = accumMatrix + accum(xyc.T, w[inside], size=[M, N])

        del xc, yc, w  # These are cleared to create memory space for the next loop. Otherwise out-of-memory at xc = bsxfun... in the next loop.

    return accumMatrix, gradientImg
Beispiel #50
0
def df_add_counts(df, cols):
	arr_slice = df[cols].values
	unq, unqtags, counts = np.unique(np.ravel_multi_index(arr_slice.T, arr_slice.max(0) + 1),
									 return_inverse=True, return_counts=True)
	df["_".join(cols)+'_count'] = counts[unqtags]
Beispiel #51
0
def project_to_basis(y3d, edges, los=[0, 0, 1], poles=[]):
	"""
	Project a 3D statistic on to the specified basis. The basis will be one
	of:

	- 2D (`x`, `mu`) bins: `mu` is the cosine of the angle to the line-of-sight
	- 2D (`x`, `ell`) bins: `ell` is the multipole number, which specifies
	  the Legendre polynomial when weighting different `mu` bins

	.. note::

		The 2D (`x`, `mu`) bins will be computed only if `poles` is specified.
		See return types for further details.

	Notes
	-----
	*   the `mu` range extends from 0.0 to 1.0
	*   the `mu` bins are half-inclusive half-exclusive, except the last bin
		is inclusive on both ends (to include `mu = 1.0`)

	Parameters
	----------
	y3d : RealField or ComplexField
		the 3D array holding the statistic to be projected to the specified basis
	edges : list of arrays, (2,)
		list of arrays specifying the edges of the desired `x` bins and `mu` bins
	los : array_like,
		the line-of-sight direction to use, which `mu` is defined with
		respect to; default is [0, 0, 1] for z.
	poles : list of int, optional
		if provided, a list of integers specifying multipole numbers to
		project the 2d `(x, mu)` bins on to
	hermitian_symmetric : bool, optional
		Whether the input array `y3d` is Hermitian-symmetric, i.e., the negative
		frequency terms are just the complex conjugates of the corresponding
		positive-frequency terms; if ``True``, the positive frequency terms
		will be explicitly double-counted to account for this symmetry

	Returns
	-------
	result : tuple
		the 2D binned results; a tuple of ``(xmean_2d, mumean_2d, y2d, N_2d)``, where:

		- xmean_2d : array_like, (Nx, Nmu)
			the mean `x` value in each 2D bin
		- mumean_2d : array_like, (Nx, Nmu)
			the mean `mu` value in each 2D bin
		- y2d : array_like, (Nx, Nmu)
			the mean `y3d` value in each 2D bin
		- N_2d : array_like, (Nx, Nmu)
			the number of values averaged in each 2D bin

	pole_result : tuple or `None`
		the multipole results; if `poles` supplied it is a tuple of ``(xmean_1d, poles, N_1d)``,
		where:

		- xmean_1d : array_like, (Nx,)
			the mean `x` value in each 1D multipole bin
		- poles : array_like, (Nell, Nx)
			the mean multipoles value in each 1D bin
		- N_1d : array_like, (Nx,)
			the number of values averaged in each 1D bin
	"""
	comm = y3d.pm.comm
	x3d = y3d.x
	hermitian_symmetric = numpy.iscomplexobj(y3d)

	from scipy.special import legendre

	# setup the bin edges and number of bins
	xedges, muedges = edges
	x2edges = xedges**2
	Nx = len(xedges) - 1
	Nmu = len(muedges) - 1

	# always make sure first ell value is monopole, which
	# is just (x, mu) projection since legendre of ell=0 is 1
	do_poles = len(poles) > 0
	_poles = [0]+sorted(poles) if 0 not in poles else sorted(poles)
	legpoly = [legendre(l) for l in _poles]
	ell_idx = [_poles.index(l) for l in poles]
	Nell = len(_poles)

	# valid ell values
	if any(ell < 0 for ell in _poles):
		raise ValueError("in `project_to_basis`, multipole numbers must be non-negative integers")

	# initialize the binning arrays
	musum = numpy.zeros((Nx+2, Nmu+2))
	xsum = numpy.zeros((Nx+2, Nmu+2))
	ysum = numpy.zeros((Nell, Nx+2, Nmu+2), dtype=y3d.dtype) # extra dimension for multipoles
	Nsum = numpy.zeros((Nx+2, Nmu+2), dtype='i8')

	# if input array is Hermitian symmetric, only half of the last
	# axis is stored in `y3d`
	symmetry_axis = -1 if hermitian_symmetric else None

	# iterate over y-z planes of the coordinate mesh
	for slab in SlabIterator(x3d, axis=0, symmetry_axis=symmetry_axis):

		# the square of coordinate mesh norm
		# (either Fourier space k or configuraton space x)
		xslab = slab.norm2()

		# if empty, do nothing
		if len(xslab.flat) == 0: continue

		# get the bin indices for x on the slab
		dig_x = numpy.digitize(xslab.flat, x2edges)

		# make xslab just x
		xslab **= 0.5

		# get the bin indices for mu on the slab
		mu = slab.mu(los) # defined with respect to specified LOS
		dig_mu = numpy.digitize(abs(mu).flat, muedges)

		# make the multi-index
		multi_index = numpy.ravel_multi_index([dig_x, dig_mu], (Nx+2,Nmu+2))

		# sum up x in each bin (accounting for negative freqs)
		xslab[:] *= slab.hermitian_weights
		xsum.flat += numpy.bincount(multi_index, weights=xslab.flat, minlength=xsum.size)

		# count number of modes in each bin (accounting for negative freqs)
		Nslab = numpy.ones_like(xslab) * slab.hermitian_weights
		Nsum.flat += numpy.bincount(multi_index, weights=Nslab.flat, minlength=Nsum.size)

		# compute multipoles by weighting by Legendre(ell, mu)
		for iell, ell in enumerate(_poles):

			# weight the input 3D array by the appropriate Legendre polynomial
			weighted_y3d = legpoly[iell](mu) * y3d[slab.index]

			# add conjugate for this kx, ky, kz, corresponding to
			# the (-kx, -ky, -kz) --> need to make mu negative for conjugate
			# Below is identical to the sum of
			# Leg(ell)(+mu) * y3d[:, nonsingular]	(kx, ky, kz)
			# Leg(ell)(-mu) * y3d[:, nonsingular].conj()  (-kx, -ky, -kz)
			# or
			# weighted_y3d[:, nonsingular] += (-1)**ell * weighted_y3d[:, nonsingular].conj()
			# but numerically more accurate.
			if hermitian_symmetric:

				if ell % 2: # odd, real part cancels
					weighted_y3d.real[slab.nonsingular] = 0.
					weighted_y3d.imag[slab.nonsingular] *= 2.
				else:  # even, imag part cancels
					weighted_y3d.real[slab.nonsingular] *= 2.
					weighted_y3d.imag[slab.nonsingular] = 0.

			# sum up the weighted y in each bin
			weighted_y3d *= (2.*ell + 1.)
			ysum[iell,...].real.flat += numpy.bincount(multi_index, weights=weighted_y3d.real.flat, minlength=Nsum.size)
			if numpy.iscomplexobj(ysum):
				ysum[iell,...].imag.flat += numpy.bincount(multi_index, weights=weighted_y3d.imag.flat, minlength=Nsum.size)

		# sum up the absolute mag of mu in each bin (accounting for negative freqs)
		mu[:] *= slab.hermitian_weights
		musum.flat += numpy.bincount(multi_index, weights=abs(mu).flat, minlength=musum.size)

	# sum binning arrays across all ranks
	xsum  = comm.allreduce(xsum)
	musum = comm.allreduce(musum)
	ysum  = comm.allreduce(ysum)
	Nsum  = comm.allreduce(Nsum)

	# add the last 'internal' mu bin (mu == 1) to the last visible mu bin
	# this makes the last visible mu bin inclusive on both ends.
	ysum[..., -2] += ysum[..., -1]
	musum[:, -2]  += musum[:, -1]
	xsum[:, -2]   += xsum[:, -1]
	Nsum[:, -2]   += Nsum[:, -1]

	# reshape and slice to remove out of bounds points
	sl = slice(1, -1)
	with numpy.errstate(invalid='ignore'):

		# 2D binned results
		y2d	   = (ysum[0,...] / Nsum)[sl,sl] # ell=0 is first index
		xmean_2d  = (xsum / Nsum)[sl,sl]
		mumean_2d = (musum / Nsum)[sl, sl]
		N_2d	  = Nsum[sl,sl]

		# 1D multipole results (summing over mu (last) axis)
		if do_poles:
			N_1d	 = Nsum[sl,sl].sum(axis=-1)
			xmean_1d = xsum[sl,sl].sum(axis=-1) / N_1d
			poles	= ysum[:, sl,sl].sum(axis=-1) / N_1d
			poles	= poles[ell_idx,...]

	# return y(x,mu) + (possibly empty) multipoles
	result = (xmean_2d, mumean_2d, y2d, N_2d)
	pole_result = (xmean_1d, poles, N_1d) if do_poles else None
	return result, pole_result
Beispiel #52
0
def integrate_visibility_from_rays(origins, directions, alpha_grid,
                                   visibility_grid, scene_params, grid_params):
  """Ray marches rays through a grid and marks visited voxels as visible.

  This function adds the visibility value (1-accumulated_alpha) into the
  visibility_grid passed as a parameter.

  Args:
    origins: An np.array [N, 3] of the ray origins.
    directions: An np.array [N, 3] of ray directions.
    alpha_grid: A [cW, cH, cD, 1] numpy array for the alpha values in the
       low-res culling grid.
    visibility_grid: A [cW, cH, cD, 1] numpy array for the visibility values in
      the low-res culling grid. Note that this function will be adding
      visibility values into this grid.
    scene_params: A dict for scene specific params (bbox, rotation, resolution).
    grid_params: A dict with parameters describing the high-res voxel grid which
      the atlas is representing.
  """
  # Extract the relevant parameters from the dictionaries.
  min_xyz = scene_params['min_xyz']
  worldspace_t_opengl = scene_params['worldspace_T_opengl']
  voxel_size = grid_params['_voxel_size']
  grid_size = grid_params['_grid_size']

  # Set up the rays and transform them to the voxel grid coordinate space.
  num_rays = origins.shape[0]
  opengl_t_worldspace = np.linalg.inv(worldspace_t_opengl)

  directions_norm = np.linalg.norm(directions, axis=-1, keepdims=True)
  directions /= directions_norm

  origins_hom = np.concatenate(
      [origins, np.ones_like(origins[Ellipsis, 0:1])], axis=-1)
  origins_hom = origins_hom.reshape((-1, 4)).dot(opengl_t_worldspace)
  origins_opengl = origins_hom[Ellipsis, 0:3].reshape(origins.shape)
  directions_opengl = directions.dot(opengl_t_worldspace[0:3, 0:3])

  origins_grid = (origins_opengl - min_xyz) / voxel_size
  directions_grid = directions_opengl
  inv_directions_grid = 1.0 / directions_grid

  # Now set the near and far distance of each ray to match the cube which
  # the voxel grid is defined in.
  min_distances, max_distances = rays_aabb_intersection(
      np.zeros_like(min_xyz), grid_size, origins_grid, inv_directions_grid)
  invalid_mask = min_distances > max_distances
  min_distances[invalid_mask] = 0
  max_distances[invalid_mask] = 0

  # The NeRF near/far bounds have been set for unnormalized ray directions, so
  # we need to scale our bounds here to compensate for normalizing.
  near_in_voxels = directions_norm[Ellipsis, 0] * scene_params['near'] / voxel_size
  far_in_voxels = directions_norm[Ellipsis, 0] * scene_params['far'] / voxel_size

  min_distances = np.maximum(near_in_voxels, min_distances)
  max_distances = np.maximum(near_in_voxels, max_distances)
  max_distances = np.minimum(far_in_voxels, max_distances)
  num_steps = int(0.5 + max_distances.max() - min_distances.min())

  # Finally, set up the accumulation buffers we need for ray marching.
  raveled_alpha = alpha_grid.ravel()
  total_visibility = np.ones((1, num_rays, 1), dtype=np.float32)
  min_distances = np.expand_dims(min_distances, -1)
  for i in range(num_steps):
    visibility_mask = (total_visibility >= 0.01)[0]
    if not visibility_mask.max():
      break

    current_distances = min_distances + 0.5 + i
    active_current_distances = current_distances[visibility_mask]
    active_max_distances = max_distances[visibility_mask[Ellipsis, 0]]
    if active_current_distances.min() >= active_max_distances.max():
      break

    positions_grid = origins_grid + directions_grid * current_distances

    epsilon = 0.1
    valid_mask = (positions_grid[Ellipsis, 0] < grid_size[Ellipsis, 0] - 0.5 - epsilon)
    valid_mask *= (positions_grid[Ellipsis, 1] < grid_size[Ellipsis, 1] - 0.5 - epsilon)
    valid_mask *= (positions_grid[Ellipsis, 2] < grid_size[Ellipsis, 2] - 0.5 - epsilon)
    valid_mask *= (positions_grid[Ellipsis, 0] > 0.5 + epsilon)
    valid_mask *= (positions_grid[Ellipsis, 1] > 0.5 + epsilon)
    valid_mask *= (positions_grid[Ellipsis, 2] > 0.5 + epsilon)
    invalid_mask = np.logical_not(valid_mask)
    if not valid_mask.max():
      continue

    invalid_mask = np.logical_not(valid_mask)
    positions_grid -= 0.5
    alpha = np.zeros((1, num_rays, 1), dtype=np.float32)

    # Use trilinear interpolation for smoother results.
    offsets_xyz = [(x, y, z)  # pylint: disable=g-complex-comprehension
                   for x in [0.0, 1.0]
                   for y in [0.0, 1.0]
                   for z in [0.0, 1.0]]
    for dx, dy, dz in offsets_xyz:
      grid_indices = np.floor(positions_grid + np.array([dx, dy, dz])).astype(
          np.int32)
      weights = 1 - np.abs(grid_indices - positions_grid)
      weights = weights.prod(axis=-1).reshape(alpha.shape)

      grid_indices[invalid_mask] = 0
      grid_indices = grid_indices.reshape(-1, 3).T

      trilinear_alpha = np.take(
          raveled_alpha, np.ravel_multi_index(grid_indices, alpha_grid.shape))
      alpha += weights * trilinear_alpha.reshape(alpha.shape)

      # Splat the visibility value into the visibility grid.
      weighted_visibilities = weights * total_visibility
      weighted_visibilities[0, invalid_mask] = 0.0
      visibility_grid.ravel()[np.ravel_multi_index(
          grid_indices, visibility_grid.shape)] += (
              weighted_visibilities[0, :, 0])

    alpha[0, invalid_mask] = 0.0
    total_visibility *= 1.0 - alpha
Beispiel #53
0
    def get_flattened_index(self, voxel: Voxel) -> int:
        """Return the flattened index of a voxel inside the grid.

        This is a convenience method that wraps numpy.ravel_multi_index.
        """
        return np.ravel_multi_index(voxel, self.shape)
Beispiel #54
0
 def __init__(self, dem="", auxtopo=False, filled=False, verbose=False, verb_func=print):
     """
     Class that define a network object (topologically sorted giver-receiver cells)
     
     Parameters:
     ===========
     dem : *DEM object* or *str*
       topopy.DEM instance with the input Digital Elevation Model, or path to a previously saved Flow object. If the 
       parameter is an empty string, it will create an empty Flow instance.
     filled : boolean
       Boolean to check if input DEM was already pit-filled. The fill algoritm implemented in the DEM object, 
       althoug fast, consumes a lot of memory. In some cases could be necessary fill the DEM with alternative GIS tools.
     auxtopo : boolean
       Boolean to determine if a auxiliar topography is used (much slower). The auxiliar topography is calculated with 
       elevation differences between filled and un-filled dem. If filled is True, auxtopo is ignored (cannot compute differences)
     verbose : boolean
       Boolean to show processing messages in console to known the progress. Usefull with large DEMs to se the evolution.
     
     References:
     -----------
     The algoritm to created the topologically sorted network has been adapted to Python from FLOWobj.m 
     by Wolfgang Schwanghart (version of 17. August, 2017) included in TopoToolbox matlab codes (really
     smart algoritms there!). If use, please cite:
             
     Schwanghart, W., Scherler, D., 2014. Short Communication: TopoToolbox 2 - 
     MATLAB-based software for topographic analysis and modeling in Earth 
     surface sciences. Earth Surf. Dyn. 2, 1–7. https://doi.org/10.5194/esurf-2-1-2014
     """
     
     if dem == "":
         # Creates an empty Flow object
         self._size = (1, 1)
         self._dims = (1, 1)
         self._geot = (0., 1., 0., 0., 0., -1.)
         self._cellsize = (self._geot[1], self._geot[5])
         self._proj = ""
         self._ncells = 1
         self._nodata_pos = np.array([], dtype=np.int32)
         self._ix = np.array([], dtype=np.int32)
         self._ixc = np.array([], dtype=np.int32)
     
     elif type(dem) == str:
         # Loads the Flow object in GeoTiff format
         try:
             self.load_gtiff(dem)
         except:
             raise FlowError("Error opening the Geotiff")
     else:
         try:
             # Set Network properties
             self._size = dem.get_size()
             self._dims = dem.get_dims()
             self._geot = dem.get_geotransform()
             self._cellsize = dem.get_cellsize()
             self._proj = dem.get_projection()
             self._ncells = dem.get_ncells()
             self._nodata_pos = np.ravel_multi_index(dem.get_nodata_pos(), self._dims)            
             # Get topologically sorted nodes (ix - givers, ixc - receivers)
             self._ix, self._ixc = sort_pixels(dem, auxtopo=auxtopo, filled=filled, verbose=verbose, verb_func=verb_func)
             # Recalculate NoData values
             self._nodata_pos = self._get_nodata_pos()
         except:
             raise FlowError("Unexpected Error creating the Flow object")
Beispiel #55
0
 def grid_coordinates_to_indices(self, coordinates=None):
     # Annoyingly, this doesn't work for negative indices.
     # The mode='wrap' parameter only works on positive indices.
     if coordinates is None:
         return np.arange(self.size)
     return np.ravel_multi_index(coordinates, self.shape)
Beispiel #56
0
def tt_construct_layer7(filename, feature_x):

    mat_dict = {}
    mat_dict.update(scipy.io.loadmat(filename))

    a = mat_dict['TT_mat']

    dim_arr = a['d'][0][0][0, 0]
    #n_arr    = a['n'][0][0][0:,0]
    #ps_arr   = a['ps'][0][0][0:,0]
    #core_arr = a['core'][0][0][0:,0]
    ranks = a['r'][0][0][0:, 0]
    bias = a['bias'][0][0]

    right_modes = a['right_modes'][0][0][0, 0:]
    right_modes = np.array(right_modes, dtype=np.int32)

    left_modes = a['left_modes'][0][0][0, 0:]
    left_modes = np.array(left_modes, dtype=np.int32)

    L = np.prod(left_modes)
    R = np.prod(right_modes)

    core_tensor_0 = a['tensor'][0][0][0, 0]
    core_tensor_1 = a['tensor'][0][0][0, 1]
    core_tensor_2 = a['tensor'][0][0][0, 2]
    core_tensor_3 = a['tensor'][0][0][0, 3]

    column_len = L
    row_len = R

    if dim_arr > 2:
        shape = left_modes * right_modes.ravel()
    else:
        shape = [left_modes[0], right_modes[0]]

    tensor_column_len = shape[0]
    tensor_row_len = shape[1]
    tensor_depth_len = shape[2]
    tensor_channel_len = shape[3]

    y_out = np.zeros((L, 1), dtype=float, order='F')

    #t0 = time.time()
    for i in range(0, tensor_row_len):
        core_mat_0 = np.reshape(core_tensor_0[:, i, :], [ranks[0], ranks[1]],
                                order='F')
        for j in range(0, tensor_column_len):
            core_mat_1 = np.reshape(core_tensor_1[:, j, :],
                                    [ranks[1], ranks[2]],
                                    order='F')
            tmp1 = np.dot(core_mat_0, core_mat_1)
            for k in range(0, tensor_depth_len):

                core_mat_2 = np.reshape(core_tensor_2[:, k, :],
                                        [ranks[2], ranks[3]],
                                        order='F')
                tmp2 = np.dot(tmp1, core_mat_2)

                ind1 = np.zeros(tensor_channel_len) + i
                ind2 = np.zeros(tensor_channel_len) + j
                ind3 = np.zeros(tensor_channel_len) + k
                ind4 = np.arange(tensor_channel_len)

                indy = np.ravel_multi_index([
                    ind1.astype('int64'),
                    ind2.astype('int64'),
                    ind3.astype('int64'),
                    ind4.astype('int64')
                ], (tensor_row_len, tensor_column_len, tensor_depth_len,
                    tensor_channel_len),
                                            order='F')

                row_index = np.mod(indy, column_len)

                column_index = np.floor(indy / column_len).astype('int64')

                tmp3 = np.dot(
                    tmp2,
                    core_tensor_3.reshape([ranks[3], tensor_channel_len],
                                          order='F'))

                tmp4 = np.multiply(
                    tmp3,
                    feature_x[[column_index]].reshape([1, tensor_channel_len],
                                                      order='F'))
                for l in range(0, tensor_channel_len):
                    y_out[row_index[l]] = y_out[row_index[l]] + tmp4[0, l]

    #t1 = time.time()
    #print(y_out)
    #frameinfo = getframeinfo(currentframe())
    #print(frameinfo.filename, frameinfo.lineno)
    #print(t1-t0)
    y_out = y_out + bias
    return y_out
Beispiel #57
0
def _get_facet_chunk(store, varname, iternum, nfacet, klevels, nx, nz, nfaces,
                     dtype, mask_override, domain, pad_before, pad_after):

    fs, path = store.get_fs_and_full_path(varname, iternum)

    assert (nfacet >= 0) & (nfacet < _nfacets)

    file = fs.open(path)

    # insert singleton axis for time (if not grid var) and k level
    facet_shape = (1, ) + _facet_shape(nfacet, nx, nfaces)
    facet_shape = (1, ) + facet_shape if iternum is not None else facet_shape

    level_data = []


    if (store.shrunk and iternum is not None) or \
       (store.shrunk_grid and iternum is None):
        # the store tells us whether we need a mask or not
        point = _get_variable_point(varname, mask_override)
        mykey = nx if domain == 'global' else f'{domain}_{nx}'
        index = all_index_data[mykey][point]
        zgroup = store.open_mask_group()
        mask = zgroup['mask_' + point].astype('bool')
    else:
        index = None
        mask = None

    # Offset start/end read position due to padding facet before me
    pre_pad = np.cumsum([x + y for x, y in zip(pad_before, pad_after)])
    pre_pad = shift_and_pad(pre_pad, left=False).compute()
    tot_pad = pre_pad[-1] + pad_after[-1]

    for k in klevels:
        assert (k >= 0) & (k < nz)

        # figure out where in the file we have to read to get the data
        # for this level and facet
        if index:
            i = np.ravel_multi_index((k, nfacet), (nz, _nfacets))
            start = index[i]
            end = index[i + 1]
        else:
            level_start = k * (nx**2 * nfaces - nx * tot_pad)
            facet_start, facet_end = _uncompressed_facet_index(
                nfacet, nx, nfaces)
            start = level_start + facet_start
            end = level_start + facet_end - nx * pad_after[nfacet]
            end = end - nx * (pad_before[nfacet]) if k * nfacet == 0 else end

            start, end = [
                x - nx * pre_pad[nfacet] if k + nfacet != 0 else x
                for x in [start, end]
            ]

        read_offset = start * dtype.itemsize  # in bytes
        read_length = (end - start) * dtype.itemsize  # in bytes
        file.seek(read_offset)
        buffer = file.read(read_length)
        data = np.frombuffer(buffer, dtype=dtype)

        assert len(data) == (end - start)

        if mask:
            mask_level = mask[k]
            mask_facets = _faces_to_facets(mask_level, nfaces)
            this_mask = mask_facets[nfacet]
            data = _decompress(data, this_mask, dtype)

        elif pad_before[nfacet] + pad_after[nfacet] > 0:

            # Extra care for pad after with rotated fields
            data = _pad_facet(data, facet_shape, _facet_reshape[nfacet],
                              pad_before[nfacet], pad_after[nfacet], dtype)

        # this is the shape this facet is supposed to have
        data.shape = facet_shape
        level_data.append(data)

    out = np.concatenate(level_data, axis=-4)
    return out
Beispiel #58
0
# args = sys.argv

# Loading in Machine Learning models
#####################################
y_regress = joblib.load('modeldump\sc4k_yreg.pkl')
y_estimator = joblib.load('modeldump\sc4k_xaxis_class.pkl')
x_regress = joblib.load('modeldump\sc4k_xreg.pkl')
x_class = joblib.load('modeldump\sc4k_target_xaxisrestricted.pkl')
#####################################

# Initialising the Heart structure
a = ps.Heart(nu=0.2, delta=0.0, fakedata=True)
# Randomises the rotor x,y position
cp_x_pos = randint(30, 169)
cp_y_pos = randint(0, 199)
a.set_circuit(np.ravel_multi_index([cp_y_pos,cp_x_pos],(200,200)))
tissue_reset = False

# Initialising ECG recording (randomises the probe x,y position)
current_ecg_x_pos = randint(30, 169)
current_ecg_y_pos = randint(0, 199)
ecg_processing = at.ECG(centre=(current_ecg_y_pos, current_ecg_x_pos), m='g_single')

# Initialising the animation window
app = QtGui.QApplication([])
win = pg.GraphicsWindow(border=True)
win.show()
win.setWindowTitle('animation')
w1 = win.addLayout()
view = w1.addViewBox()
img = pg.ImageItem()
Beispiel #59
0
    def compute_edt(self, msk=None, limits=None, verbose=True):
        """
        Compute the Euclidean distance transform for the given ROI binary mask.

        Keyword arguments:
            :msK:       edge mask
            :limits:
        """
        import time
        if msk is None:
            msk = self.mask
        if msk is None:
            return None

        ND = len(msk.data.shape)  # Number of Dimensions
        edt = np.empty_like(msk.data, dtype=np.float)
        edt.fill(np.inf)
        root = np.zeros(msk.data.shape + (ND, ), dtype=np.uint16)

        idx = Index(np.nonzero(msk.data))
        edt[idx] = 0
        root[idx] = idx.array
        counter = 0
        total_voxels = 0
        while idx:
            counter += 1
            total_voxels += idx.array.shape[0]
            t0 = time.clock()
            newidx = None
            if verbose:
                print('Iteration {0}: {1} voxels, {2} total voxels, '.format(
                    counter, idx.array.shape[0], total_voxels))
            for dim in range(ND):
                for offset in [-1, 1]:
                    (idx_off, inbounds) = idx.offset(dim, offset, msk)
                    idx_inbounds = idx.apply_mask(inbounds)
                    idx_off = idx_off.apply_mask(inbounds)

                    dist = idx_off.array - root[idx_inbounds]
                    for dim2 in range(ND):
                        dist[:, dim2] *= msk.spacing[dim2]
                    dist = np.sum(np.square(dist), axis=1)

                    update_edt = (dist < edt[idx_off])
                    idx_update = idx_inbounds.apply_mask(update_edt)
                    idx_off = idx_off.apply_mask(update_edt)
                    edt[idx_off] = dist[update_edt]
                    root[idx_off] = root[idx_update]

                    if idx_off.array.size:
                        if newidx is None:
                            newidx = idx_off.array
                        else:
                            newidx = np.vstack((newidx, idx_off.array))
            if newidx is None:
                idx = None
            else:
                unique_idx = np.unique(
                    np.ravel_multi_index(tuple(newidx.T), msk.data.shape))
                idx = Index(np.unravel_index(unique_idx, msk.data.shape))
            if verbose:
                print(time.clock() - t0, 'seconds')
        if verbose:
            print('Total iterations:', counter)
            print('Total voxels:', total_voxels)
        edt_out = image.Image()
        edt_out.copy_information(msk)
        edt_out.set_image(np.sqrt(edt))
        return edt_out
Beispiel #60
0
def _as_1d(dense_shape, *edges):
    edges = np.concatenate(edges, axis=0)
    edges = np.ravel_multi_index(edges.T, dense_shape)
    edges.sort()
    return edges