DataStructureOperations module

Provided data structure related operations such as remove row from matrix.

DataStructureOperations.CombineMatricesRowWise(MainMatrix, AddedMatrix, RemoveFirstZerosRow=True, Sparse=False)[source]

Stack twoe matrices vertically (row wise). You must make sure MainMatrix and AddedMatrix have appropriate dimensions, i.e. have same number of columns.

Parameters:
  • MainMatrix (SparseMatrix) – The main matrix that you want to add the AddedMatrix.
  • AddedMatrix (SparseMatrix) – The matrix added followed by the main matrix.
  • RemoveFirstZerosRow (Boolean) – When MainMatrix is empty, this method will automatically add a row with all zeros in the first row of MainMatirx. When this is True, Result will auto remove the first all zeros row when returns. Otherwise you need to manually do it.
  • Sparse (Boolean) – If Sparse is True, the matrix will be automatically converted to sparse matrix and it’s less space consuming and slower.
Returns:

Result: The result of Stacking matrices vertically (row wise).

Return type:

NumpyArray/SparseMatrix

DataStructureOperations.CombineSparseMatricesRowWise(MainMatrix, AddedMatrix, RemoveFirstZerosRow=True)[source]

Stack two scipy sparse matrices vertically (row wise). Will initialize the main matrix to be two dimensional csr_matrix with all zero elements if the main matrix is empty. You can use .toarray() to convert final result to numpy array(not a sparse matrix) You must make sure MainMatrix and AddedMatrix have appropriate dimensions, i.e. have same number of columns.

Parameters:
  • MainMatrix (SparseMatrix) – The main matrix that you want to add the AddedMatrix.
  • AddedMatrix (SparseMatrix) – The matrix added followed by the main matrix.
  • RemoveFirstZerosRow (Boolean) – When MainMatrix is empty, this method will automatically add a row with all zeros in the first row of MainMatirx. When this is True, Result will auto remove the first all zeros row when returns. Otherwise you need to manually do it.
Returns:

Result: The result of Stacking sparse matrices vertically (row wise).

Return type:

SparseMatrix

DataStructureOperations.ConvertSparseMatrixToSparseTensor(SparseMatrix, TensorType=<sphinx.ext.autodoc.importer._MockObject object>)[source]

Convert scipy sparse matrix to PyTorch sparse tensor

Refer to https://discuss.pytorch.org/t/creating-a-sparse-tensor-from-csr-matrix/13658/5

Parameters:
  • SparseMatrix – scipy sparse matrix to be converted
  • TensorType – Target PyTorch sparse tensor type.
Returns:

SparseTensor

DataStructureOperations.DeleteCsrMatrixRow(mat, i)[source]

Delete a row in a scipy.sparse.csr_matrix.

Parameters:
  • mat (scipy.sparse.csr_matrix) – The scipy.sparse.csr_matrix you want to operate on.
  • i (Int) – The row number that you want to delete
Returns:

SparseMatrix mat: The result of deleted sparse matrix.

Return type:

SparseMatrix

DataStructureOperations.DeleteLilMatrixRow(mat, i)[source]

Delete a row in a scipy.sparse.lil_matrix.

Parameters:
  • mat (scipy.sparse.lil_matrix) – The scipy.sparse.lil_matrix you want to operate on.
  • i (Int) – The row number that you want to delete
Returns:

SparseMatrix mat: The result of deleted sparse matrix.

Return type:

SparseMatrix

DataStructureOperations.FlattenList(List)[source]

Flatten a list using itertools no matter how many nest it has.

Example::
>>> FlattenList([['foo', 'baz'], ['gg']])
['foo', 'baz', 'gg']
>>> FlattenList([[['foo', 'baz'], ['gg']]])
['foo', 'baz', 'gg']
Parameters:List[Variant] – The list you want to flatten
Returns:List: Flattened list
Return type:List[Variant]
DataStructureOperations.IfTwoSparseMatrixEqual(SparseMatrix1, SparseMatrix2)[source]

Check if two scipy sparse matrix is exactly the same.

Parameters:
  • SparseMatrix1 (SparseMatrix) – The first scipy sparse matrix.
  • SparseMatrix2 (SparseMatrix) – The second scipy sparse matrix.
Returns:

Equal: True if they are equal, otherwise will be false.

Return type:

Boolean

DataStructureOperations.SparseDenseElementwiseMultiply(SparseTensor, DenseTensor, TensorType=<sphinx.ext.autodoc.importer._MockObject object>)[source]

Used for PyTorch elementwise sparse tensor and dense tensor multiplication.

Refer to https://stackoverflow.com/questions/56880166/how-to-multiply-a-dense-matrix-by-a-sparse-matrix-element-wise-in-pytorch

Parameters:
  • SparseTensor – A PyTorch sparse tensor
  • DenseTensor – A PyTorch dense tensor
  • TensorType – Target PyTorch sparse tensor type.
Returns:

SparseTensor

DataStructureOperations.SparseTensorSlice(SparseTensor, RowIndexRange, ColumnIndexRange)[source]

Used for slicing PyTorch coo sparse tensors. Will use scipy sparse matrix as intermediate variables.

Example: SparseTensorSlice(sparse_tensor, [1,2], [3,4]) SparseTensorSlice(sparse_tensor, range(1,3), range(3,5))

Parameters:
  • SparseTensor (torch.sparse.FloatTensor) – A PyTorch sparse tensor
  • RowIndexRange (List/Range) – A list of rows need to be selected
  • ColumnIndexRange (List/Range) – A list of columns need to be selected
Returns:

SparseTensor