Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ENH: include assign_self_weight (formerly fill_diagonal) in Graph #670

Merged
merged 10 commits into from
Dec 29, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions libpysal/graph/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -1405,9 +1405,11 @@

def eliminate_zeros(self):
"""Remove graph edges with zero weight

Eliminates edges with weight == 0 that do not encode an
isolate. This is useful to clean-up edges that will make
no effect in operations like :meth:`lag`.

Returns
-------
Graph
Expand All @@ -1419,6 +1421,45 @@
zeros = (self._adjacency == 0) != isolates
return Graph(self._adjacency[~zeros], is_sorted=True)

def fill_diagonal(self, val=1):
"""Fill Graph with values inserted along the main diagonal.

Value for each ``focal == neighbor`` location in the graph is set to ``val``.

Parameters
----------
val : float | array-like
Defines the value(s) to which the weights matrix diagonal should
be set. If a constant is passed then each element along the
diagonal will get this value (default is 1). An array of length
Graph.n can be passed to set explicit values to each element along
martinfleis marked this conversation as resolved.
Show resolved Hide resolved
the diagonal (assumed to be in the same order as original data).

Returns
-------
Graph
A new Graph with new values along the diagonal
"""
addition = pd.Series(
val,
index=pd.MultiIndex.from_arrays(
[self.unique_ids, self.unique_ids], names=["focal", "neighbor"]
),
name="weight",
)
adj = (
pd.concat([self.adjacency.drop(self.isolates), addition])
.reindex(self.unique_ids, level=0)
.reindex(self.unique_ids, level=1)
)
return Graph(adj, is_sorted=True)

def fill_diagonal_sparse(self):
knaaptime marked this conversation as resolved.
Show resolved Hide resolved
sp = self.sparse
sp = sp.tolil()
sp.setdiag(1)
return Graph.from_sparse(sp, ids=self.unique_ids)

Check warning on line 1461 in libpysal/graph/base.py

View check run for this annotation

Codecov / codecov/patch

libpysal/graph/base.py#L1458-L1461

Added lines #L1458 - L1461 were not covered by tests


def _arrange_arrays(heads, tails, weights, ids=None):
"""
Expand Down
16 changes: 16 additions & 0 deletions libpysal/graph/tests/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -966,3 +966,19 @@ def test_subgraph(self):
),
)
pd.testing.assert_series_equal(expected, sub._adjacency, check_dtype=False)

def test_fill_diagonal(self):
contig = graph.Graph.build_contiguity(self.nybb)
diag = contig.fill_diagonal()
assert len(diag._adjacency) == 15
assert diag._adjacency.sum() == 15

diag_array = contig.fill_diagonal([2, 3, 4, 5, 6])
assert len(diag_array._adjacency) == 15
assert diag_array._adjacency.sum() == 30

for i, val in enumerate(range(2, 7)):
assert (
diag_array._adjacency[(contig.unique_ids[i], contig.unique_ids[i])]
== val
)