# libpysal.weights.WSP

class libpysal.weights.WSP(sparse, id_order=None, index=None)

  Thin `W` class for `spreg`.

  Parameters:
    sparse scipy.sparse.{matrix-type}

      NxN object from `scipy.sparse`

  Attributes:
    n `python:int`

      description

    `s0` `python:float`

      `s0` is defined as:

    `trcWtW_WW` `python:float`

      Trace of \(W^{'}W + WW\).

  Examples

  From GAL information

      >>> import scipy.sparse
      >>> from libpysal.weights import WSP
      >>> rows = [0, 1, 1, 2, 2, 3]
      >>> cols = [1, 0, 2, 1, 3, 3]
      >>> weights =  [1, 0.75, 0.25, 0.9, 0.1, 1]
      >>> sparse = scipy.sparse.csr_matrix((weights, (rows, cols)), shape=(4,4))
      >>> w = WSP(sparse)
      >>> w.s0
      4.0
      >>> w.trcWtW_WW
      6.395
      >>> w.n
      4

  __init__(sparse, id_order=None, index=None)

  Methods

  `__init__`(sparse[, id_order, index]) |
  `from_W`(W) | Constructs a `WSP` object from the `W`'s sparse matrix.
  `to_W`([silence_warnings]) | Convert a pysal WSP object (thin weights matrix) to a pysal W object.

  Attributes

  `diagWtW_WW` | Diagonal of \(W^{'}W + WW\).
  `id_order` | An ordered list of ids, assumed to match the ordering in `sparse`.
  `s0` | `s0` is defined as:
  `trcWtW_WW` | Trace of \(W^{'}W + WW\).

  property diagWtW_WW

    Diagonal of \(W^{'}W + WW\).

  classmethod from_W(W)

    Constructs a `WSP` object from the `W`’s sparse matrix.

    Parameters:
      W `libpysal.weights.W`

        A PySAL weights object with a sparse form and ids.

    Returns:
      `A` `WSP` instance.
  property id_order

    An ordered list of ids, assumed to match the ordering in `sparse`.

  property s0

    `s0` is defined as:

    \[s0=\sum_i \sum_j w_{i,j}\]
  to_W(silence_warnings=False)

    Convert a pysal WSP object (thin weights matrix) to a pysal W object.

    Parameters:
      self `WSP`

        PySAL sparse weights object.

      silence_warnings bool

        Switch to `True` to turn off print statements for every observation with islands. Default is `False`, which does not silence warnings.

    Returns:
      w `W`

        PySAL weights object.

    Examples

        >>> from libpysal.weights import lat2SW, WSP, WSP2W

    Build a 10x10 `scipy.sparse` matrix for a rectangular 2x5 region of cells (rook contiguity), then construct a `libpysal` sparse weights object (`self`).

        >>> sp = lat2SW(2, 5)
        >>> self = WSP(sp)
        >>> self.n
        10
        >>> print(self.sparse[0].todense())
        [[0 1 0 0 0 1 0 0 0 0]]

    Convert this sparse weights object to a standard PySAL weights object.

        >>> w = WSP2W(self)
        >>> w.n
        10
        >>> print(w.full()[0][0])
        [0. 1. 0. 0. 0. 1. 0. 0. 0. 0.]

  property trcWtW_WW

    Trace of \(W^{'}W + WW\).