Description: Wrapper that sorts sparse matrix stored in CSR format
(without exposing the permutation).
510620
public static int cusparseScsru2csr_bufferSizeExt(
        cusparseHandle handle, 
        int m, 
        int n, 
        int nnz, 
        Pointer csrVal, 
        Pointer csrRowPtr, 
        Pointer csrColInd, 
        csru2csrInfo info, 
        long[] pBufferSizeInBytes)
    {
        return checkResult(cusparseScsru2csr_bufferSizeExtNative(handle, m, n, nnz, csrVal, csrRowPtr, csrColInd, info, pBufferSizeInBytes));
    }
123484
public static void sort(int n, int[] Xip, int Xip_offset, int[] Xlen, int Xlen_offset,
			double[] LU, int[] Tp, int[] Tj, double[] Tx, int[] W)
	{
		/*int[]*/double[] Xi ;
		double[] Xx ;
		int p, i, j, nz, tp, xlen, pend ;
		int[] len = new int[1] ;
		int[] Xi_offset = new int[1] ;
		int[] Xx_offset = new int[1] ;

		ASSERT (klu_valid_LU (n, FALSE, Xip, Xip_offset, Xlen, Xlen_offset, LU)) ;

		/* count the number of entries in each row of L or U */
		for (i = 0 ; i < n ; i++)
		{
			W [i] = 0 ;
		}
		for (j = 0 ; j < n ; j++)
		{
			Xi = Xx = GET_POINTER (LU, Xip, Xip_offset, Xlen, Xlen_offset, Xi_offset, Xx_offset, j, len) ;
			for (p = 0 ; p < len[0] ; p++)
			{
				W [(int) Xi [Xi_offset[0] + p]]++ ;
			}
		}

		/* construct the row pointers for T */
		nz = 0 ;
		for (i = 0 ; i < n ; i++)
		{
			Tp [i] = nz ;
			nz += W [i] ;
		}
		Tp [n] = nz ;
		for (i = 0 ; i < n ; i++)
		{
			W [i] = Tp [i] ;
		}

		/* transpose the matrix into Tp, Ti, Tx */
		for (j = 0 ; j < n ; j++)
		{
			Xi = Xx = GET_POINTER (LU, Xip, Xip_offset, Xlen, Xlen_offset, Xi_offset, Xx_offset, j, len) ;
			for (p = 0 ; p < len[0] ; p++)
			{
				tp = W [(int) Xi [Xi_offset[0] + p]]++ ;
				Tj [tp] = j ;
				Tx [tp] = Xx [Xx_offset[0] + p] ;
			}
		}

		/* transpose the matrix back into Xip, Xlen, Xi, Xx */
		for (j = 0 ; j < n ; j++)
		{
			W [j] = 0 ;
		}
		for (i = 0 ; i < n ; i++)
		{
			pend = Tp [i+1] ;
			for (p = Tp [i] ; p < pend ; p++)
			{
				j = Tj [p] ;
				Xi = Xx = GET_POINTER (LU, Xip, Xip_offset, Xlen, Xlen_offset, Xi_offset, Xx_offset, j, len) ;
				xlen = W [j]++ ;
				Xi [Xi_offset[0] + xlen] = i ;
				Xx [Xx_offset[0] + xlen] = Tx [p] ;
			}
		}

		ASSERT (klu_valid_LU (n, FALSE, Xip, Xip_offset, Xlen, Xlen_offset, LU)) ;
	}
552026
def _construct(x, categories):
    
    # type: (np.ndarray, np.ndarray) -> scipy.sparse.csr_matrix
    data = np.ones(len(x))
    rows = np.arange(len(x))
    columns = x.ravel()
    return scipy.sparse.csr_matrix(
        (data, (rows, columns)), shape=(len(x), len(categories))
    )
403892
def hash_vector(self, v, querying=False):
        
        if scipy.sparse.issparse(v):
            # If vector is sparse, make sure we have the CSR representation
            # of the projection matrix
            if self.normals_csr == None:
                self.normals_csr = scipy.sparse.csr_matrix(self.normals)
            # Make sure that we are using CSR format for multiplication
            if not scipy.sparse.isspmatrix_csr(v):
                v = scipy.sparse.csr_matrix(v)
            # Project vector onto all hyperplane normals
            projection = self.normals_csr.dot(v)
        else:
            # Project vector onto all hyperplane normals
            projection = numpy.dot(self.normals, v)
        # Return binary key
        return [''.join(['1' if x > 0.0 else '0' for x in projection])]
447679
def _to_ijv(ss, row_levels=(0, ), column_levels=(1, ), sort_labels=False):
    
    # index and column levels must be a partition of the index
    _check_is_partition([row_levels, column_levels], range(ss.index.nlevels))

    # from the SparseSeries: get the labels and data for non-null entries
    values = ss._data.internal_values()._valid_sp_values

    nonnull_labels = ss.dropna()

    def get_indexers(levels):
        """ Return sparse coords and dense labels for subset levels """

        # TODO: how to do this better? cleanly slice nonnull_labels given the
        # coord
        values_ilabels = [tuple(x[i] for i in levels)
                          for x in nonnull_labels.index]
        if len(levels) == 1:
            values_ilabels = [x[0] for x in values_ilabels]

        # # performance issues with groupby ###################################
        # TODO: these two lines can rejplace the code below but
        # groupby is too slow (in some cases at least)
        # labels_to_i = ss.groupby(level=levels, sort=sort_labels).first()
        # labels_to_i[:] = np.arange(labels_to_i.shape[0])

        def _get_label_to_i_dict(labels, sort_labels=False):
            """ Return OrderedDict of unique labels to number.
            Optionally sort by label.
            """
            labels = Index(lmap(tuple, labels)).unique().tolist()  # squish
            if sort_labels:
                labels = sorted(list(labels))
            d = OrderedDict((k, i) for i, k in enumerate(labels))
            return (d)

        def _get_index_subset_to_coord_dict(index, subset, sort_labels=False):
            ilabels = list(zip(*[index._get_level_values(i) for i in subset]))
            labels_to_i = _get_label_to_i_dict(ilabels,
                                               sort_labels=sort_labels)
            labels_to_i = Series(labels_to_i)
            if len(subset) > 1:
                labels_to_i.index = MultiIndex.from_tuples(labels_to_i.index)
                labels_to_i.index.names = [index.names[i] for i in subset]
            else:
                labels_to_i.index = Index(x[0] for x in labels_to_i.index)
                labels_to_i.index.name = index.names[subset[0]]

            labels_to_i.name = 'value'
            return (labels_to_i)

        labels_to_i = _get_index_subset_to_coord_dict(ss.index, levels,
                                                      sort_labels=sort_labels)
        # #####################################################################
        # #####################################################################

        i_coord = labels_to_i[values_ilabels].tolist()
        i_labels = labels_to_i.index.tolist()

        return i_coord, i_labels

    i_coord, i_labels = get_indexers(row_levels)
    j_coord, j_labels = get_indexers(column_levels)

    return values, i_coord, j_coord, i_labels, j_labels
649594
public static Matrix fromCSV(String csv) {
        StringTokenizer lines = new StringTokenizer(csv, "\n");
        Matrix result = DenseMatrix.zero(10, 10);
        int rows = 0;
        int columns = 0;

        while (lines.hasMoreTokens()) {
            if (result.rows() == rows) {
                result = result.copyOfRows((rows * 3) / 2 + 1);
            }

            StringTokenizer elements = new StringTokenizer(lines.nextToken(), ", ");
            int j = 0;
            while (elements.hasMoreElements()) {
                if (j == result.columns()) {
                    result = result.copyOfColumns((j * 3) / 2 + 1);
                }

                double x = Double.parseDouble(elements.nextToken());
                result.set(rows, j++, x);
            }

            rows++;
            columns = j > columns ? j : columns;
        }

        return result.copyOfShape(rows, columns);
    }
319197
def _store_unsorted_results(self, smooth, residual):
        """"""
        if self._original_index_of_xvalue:
            # data was sorted. Unsort it here.
            self.smooth_result = numpy.zeros(len(self.y))
            self.cross_validated_residual = numpy.zeros(len(residual))
            original_x = numpy.zeros(len(self.y))
            for i, (xval, smooth_val, residual_val) in enumerate(zip(self.x, smooth, residual)):
                original_index = self._original_index_of_xvalue[i]
                original_x[original_index] = xval
                self.smooth_result[original_index] = smooth_val
                self.cross_validated_residual[original_index] = residual_val
                self.x = original_x
        else:
            # no sorting was done. just apply results
            self.smooth_result = smooth
            self.cross_validated_residual = residual
668952
def bin_kb_sparse(M, positions, length=10):
    

    try:
        from scipy.sparse import coo_matrix
    except ImportError as e:
        print(str(e))
        print("I am peforming dense normalization by default.")
        return bin_kb_dense(M.todense(), positions=positions)
    r = M.tocoo()
    unit = 10**3
    ul = unit * length
    units = positions / ul
    n = len(positions)
    indices = np.floor(units)
    row = [indices[np.floor(i)] for i in r.row / ul]
    col = [indices[np.floor(j)] for j in r.col / ul]
    binned_indices = positions[
        [i for i in range(n - 1) if np.ceil(units[i]) < np.ceil(units[i + 1])]]
    return coo_matrix((r.data, (row, col))), binned_indices
476480
public static int cusparseScsr2dense(
        cusparseHandle handle, 
        int m, 
        int n, 
        cusparseMatDescr descrA, 
        Pointer csrSortedValA, 
        Pointer csrSortedRowPtrA, 
        Pointer csrSortedColIndA, 
        Pointer A, 
        int lda)
    {
        return checkResult(cusparseScsr2denseNative(handle, m, n, descrA, csrSortedValA, csrSortedRowPtrA, csrSortedColIndA, A, lda));
    }
1262204
def sparse_is_desireable(lhs, rhs):
    '''
    
    '''
    return False
    if len(lhs.shape) == 1:
        return False
    else:
        lhs_rows, lhs_cols = lhs.shape

    if len(rhs.shape) == 1:
        rhs_rows = 1
        rhs_cols = rhs.size
    else:
        rhs_rows, rhs_cols = rhs.shape

    result_size = lhs_rows * rhs_cols

    if sp.issparse(lhs) and sp.issparse(rhs):
        return True
    elif sp.issparse(lhs):
        lhs_zero_rows = lhs_rows - np.unique(lhs.nonzero()[0]).size
        rhs_zero_cols = np.all(rhs==0, axis=0).sum()
        
    elif sp.issparse(rhs):
        lhs_zero_rows = np.all(lhs==0, axis=1).sum()
        rhs_zero_cols = rhs_cols- np.unique(rhs.nonzero()[1]).size
    else:
        lhs_zero_rows = np.all(lhs==0, axis=1).sum()
        rhs_zero_cols = np.all(rhs==0, axis=0).sum()

    num_zeros = lhs_zero_rows * rhs_cols + rhs_zero_cols * lhs_rows - lhs_zero_rows * rhs_zero_cols

    # A sparse matrix uses roughly 16 bytes per nonzero element (8 + 2 4-byte inds), while a dense matrix uses 8 bytes per element. So the break even point for sparsity is 50% nonzero. But in practice, it seems to be that the compression in a csc or csr matrix gets us break even at ~65% nonzero, which lets us say 50% is a conservative, worst cases cutoff.
    return (float(num_zeros) / float(size)) >= 0.5
619999
public static int cusparseShyb2csr(
        cusparseHandle handle, 
        cusparseMatDescr descrA, 
        cusparseHybMat hybA, 
        Pointer csrSortedValA, 
        Pointer csrSortedRowPtrA, 
        Pointer csrSortedColIndA)
    {
        return checkResult(cusparseShyb2csrNative(handle, descrA, hybA, csrSortedValA, csrSortedRowPtrA, csrSortedColIndA));
    }
231068
public static DMatrixSparseCSC permutationMatrix(int[] p, boolean inverse, int N, DMatrixSparseCSC P) {

        if( P == null )
            P = new DMatrixSparseCSC(N,N,N);
        else
            P.reshape(N,N,N);
        P.indicesSorted = true;
        P.nz_length = N;

        // each column should have one element inside of it
        if( !inverse ) {
            for (int i = 0; i < N; i++) {
                P.col_idx[i + 1] = i + 1;
                P.nz_rows[p[i]] = i;
                P.nz_values[i] = 1;
            }
        } else {
            for (int i = 0; i < N; i++) {
                P.col_idx[i + 1] = i + 1;
                P.nz_rows[i] = p[i];
                P.nz_values[i] = 1;
            }
        }

        return P;
    }
444580
public static void sort(DenseMatrix64F matrix, RowComparator comparator)
    {
        int len = matrix.numCols;
        quickSort(matrix.data, 0, matrix.numRows-1, len, comparator, new double[len], new double[len]);
    }
219773
def sparsify(sess, eval_model, pruning_strategy, pruning_params):
  """"""
  weights = tf.trainable_variables()

  def should_prune(name):
    """Whether to prune a weight or not."""
    in_whitelist = not pruning_params.white_list or any(
        e in name for e in pruning_params.white_list)
    in_blacklist = any(e in name for e in pruning_params.black_list)

    if pruning_params.white_list and not in_whitelist:
      return False
    elif in_blacklist:
      return False

    return True

  weights = [w for w in weights if should_prune(w.name)]
  tf.logging.info("Pruning weights: %s" % weights)
  unpruned_weights = sess.run(weights)

  reset_op = tf.no_op()
  for w, ow in zip(weights, unpruned_weights):
    op = tf.assign(w, ow)
    reset_op = tf.group(reset_op, op)

  for sparsity in pruning_params.sparsities:
    set_weights_op = tf.no_op()
    for w in weights:
      op = tf.assign(w, pruning_strategy(w, sparsity))
      set_weights_op = tf.group(set_weights_op, op)
    sess.run(set_weights_op)

    acc = eval_model()
    tf.logging.info("\tPruning to sparsity = %f: acc = %f" % (sparsity, acc))
    sess.run(reset_op)
858756
def tostype(self, stype):
        
        # pylint: disable= no-member, protected-access
        if stype == 'csr':
            raise ValueError("cast_storage from row_sparse to csr is not supported")
        return op.cast_storage(self, stype=stype)
977399
def load_matrix_sparse(filename):
    coo = np.load(filename)

    """"""
    if len(coo.shape) == 2 and coo.shape[1] == 3:
        row = coo[:, 0]
        col = coo[:, 1]
        values = coo[:, 2]

        """Check if imaginary part of row and col is zero"""
        if np.all(np.isreal(row)) and np.all(np.isreal(col)):
            row = row.real
            col = col.real

            """Check if first and second column contain only integer entries"""
            if np.all(is_integer(row)) and np.all(is_integer(col)):

                """Convert row and col to int"""
                row = row.astype(int)
                col = col.astype(int)

                """Create coo-matrix"""
                A = scipy.sparse.coo_matrix((values, (row, col)))
                return A
            else:
                raise ValueError('File contains non-integer entries for row and col.')
        else:
            raise ValueError('File contains complex entries for row and col.')
    else:
        raise ValueError('Given file is not a sparse matrix in coo-format.')
1061856
def sparse(csv, sparse):
    ''' 
    '''
    import pandas as pd
    df = pd.read_csv(csv, index_col=0, header=0)
    pd.Series(df.index).to_csv(sparse + ".rownames", index=False)
    pd.Series(df.columns.values).to_csv(sparse + ".colnames", index=False)
    with open(sparse, "w+b") as out_handle:
        scipy.io.mmwrite(out_handle, scipy.sparse.csr_matrix(df))