Metadata-Version: 2.1
Name: sparsity-pattern
Version: 0.4.5
Summary: Generate different types of sparsity pattern for sparse matrices.
Home-page: http://github.com/ulf1/sparsity-pattern
Author: Ulf Hamster
Author-email: 554c46@gmail.com
License: Apache License 2.0
Platform: UNKNOWN
Requires-Python: >=3.6
License-File: LICENSE

|PyPI version| |PyPi downloads| |DOI| |sparsity-pattern| |Total alerts|
|Language grade: Python|

sparsity-pattern
================

Generate different types of sparsity pattern for sparse matrices.

Installation
------------

The ``sparsity-pattern`` `git
repo <http://github.com/ulf1/sparsity-pattern>`__ is available as `PyPi
package <https://pypi.org/project/sparsity-pattern>`__

::

   pip install sparsity-pattern
   pip install git+ssh://git@github.com/ulf1/sparsity-pattern.git

Usage
-----

The ``block``-diagonal pattern for tensorflow

.. code:: py

   import sparsity_pattern
   import tensorflow as tf

   n_rows, n_cols = 10, 12
   idx = sparsity_pattern.get('block', min(n_rows, n_cols), block_sizes=[3, 1, 2])

   mat = tf.sparse.SparseTensor(
       dense_shape=(n_rows, n_cols),
       indices=tf.convert_to_tensor(idx, dtype=tf.int64),
       values=range(1, len(idx)+1))

   print(tf.sparse.to_dense(mat))

The ``circle`` pattern for pytorch

.. code:: py

   import sparsity_pattern
   import torch

   n_rows, n_cols = 5, 7
   idx = sparsity_pattern.get('circle', min(n_rows, n_cols), offsets=[1, 2])

   mat = torch.sparse_coo_tensor(
       indices=torch.tensor(idx).transpose(0, 1), 
       values=range(1, len(idx)+1),
       size=[n_rows, n_cols])

   print(mat.to_dense())

The ``triu`` pattern for scipy

.. code:: py

   import sparsity_pattern
   import scipy.sparse
   import numpy as np

   n, k = 4, -1
   idx = sparsity_pattern.get('triu', n, k)
   idx_rows, idx_cols = np.array(idx)[:, 0], np.array(idx)[:, 1]
   mat = scipy.sparse.lil_matrix((n, n), dtype=np.int64)
   mat[idx_rows, idx_cols] = range(1, len(idx)+1)

   print(mat.todense())

Check the
`examples <https://github.com/ulf1/sparsity-pattern/tree/master/examples>`__
folder for more notebooks.

Appendix
--------

Install a virtual environment
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

::

   python3 -m venv .venv
   source .venv/bin/activate
   pip3 install --upgrade pip
   pip3 install -r requirements-dev.txt
   pip3 install -r requirements-demo.txt

(If your git repo is stored in a folder with whitespaces, then don’t use
the subfolder ``.venv``. Use an absolute path without whitespaces.)

Python commands
~~~~~~~~~~~~~~~

-  Jupyter for the examples: ``jupyter lab``
-  Check syntax:
   ``flake8 --ignore=F401 --exclude=$(grep -v '^#' .gitignore | xargs | sed -e 's/ /,/g')``
-  Run Unit Tests: ``pytest``

Publish

.. code:: sh

   pandoc README.md --from markdown --to rst -s -o README.rst
   python setup.py sdist 
   twine upload -r pypi dist/*

Clean up
~~~~~~~~

::

   find . -type f -name "*.pyc" | xargs rm
   find . -type d -name "__pycache__" | xargs rm -r
   rm -r .pytest_cache
   rm -r .venv

Support
~~~~~~~

Please `open an
issue <https://github.com/ulf1/sparsity-pattern/issues/new>`__ for
support.

License and citation
~~~~~~~~~~~~~~~~~~~~

This software is licensed under Apache License 2.0 and archived on
`Zenodo <https://doi.org/10.5281/zenodo.4357290>`__. If you would like
to cite the software, please use this DOI:
`10.5281/zenodo.4357290 <https://doi.org/10.5281/zenodo.4357290>`__.

Contributing
~~~~~~~~~~~~

Please contribute using `Github
Flow <https://guides.github.com/introduction/flow/>`__. Create a branch,
add commits, and `open a pull
request <https://github.com/ulf1/sparsity-pattern/compare/>`__.

.. |PyPI version| image:: https://badge.fury.io/py/sparsity-pattern.svg
   :target: https://badge.fury.io/py/sparsity-pattern
.. |PyPi downloads| image:: https://img.shields.io/pypi/dm/sparsity-pattern
   :target: https://img.shields.io/pypi/dm/sparsity-pattern
.. |DOI| image:: https://zenodo.org/badge/245850728.svg
   :target: https://zenodo.org/badge/latestdoi/245850728
.. |sparsity-pattern| image:: https://snyk.io/advisor/python/sparsity-pattern/badge.svg
   :target: https://snyk.io/advisor/python/sparsity-pattern
.. |Total alerts| image:: https://img.shields.io/lgtm/alerts/g/ulf1/sparsity-pattern.svg?logo=lgtm&logoWidth=18
   :target: https://lgtm.com/projects/g/ulf1/sparsity-pattern/alerts/
.. |Language grade: Python| image:: https://img.shields.io/lgtm/grade/python/g/ulf1/sparsity-pattern.svg?logo=lgtm&logoWidth=18
   :target: https://lgtm.com/projects/g/ulf1/sparsity-pattern/context:python


