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

Exe 2084 bug in local g #205

Merged
merged 4 commits into from
Dec 20, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [x.x.x] - ???

### Fixed

- In some cases `local_g` would return scrambled results due to inconsistent node ordering, this
has now been fixed.

## [0.19.0] - 2024-12-10

### Changed
Expand Down
12 changes: 9 additions & 3 deletions src/pixelator/graph/backends/implementations/_networkx.py
Original file line number Diff line number Diff line change
Expand Up @@ -266,9 +266,15 @@ def ecount(self):
"""Get the total number of edges in the Graph instance."""
return self._raw.number_of_edges()

def get_adjacency_sparse(self) -> csr_matrix:
"""Get the sparse adjacency matrix."""
return nx.to_scipy_sparse_array(self._raw)
def get_adjacency_sparse(
self, node_ordering: Iterable[Any] | None = None
) -> csr_matrix:
"""Get the sparse adjacency matrix.

:param node_ordering: Control the node ordering in the adjacency matrix
:return: a sparse adjacency matrix
"""
return nx.to_scipy_sparse_array(self._raw, nodelist=node_ordering)

def connected_components(self) -> NetworkxBasedVertexClustering:
"""Get the connected components in the Graph instance."""
Expand Down
10 changes: 8 additions & 2 deletions src/pixelator/graph/backends/protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,14 @@ def ecount(self) -> int:
"""Get the total number of edges in the Graph instance."""
...

def get_adjacency_sparse(self) -> csr_matrix:
"""Get the sparse adjacency matrix."""
def get_adjacency_sparse(
self, node_ordering: Iterable[Any] | None = None
) -> csr_matrix:
"""Get the sparse adjacency matrix.

:param node_ordering: Control the node ordering in the adjacency matrix
:return: a sparse adjacency matrix
"""
...

def connected_components(self) -> VertexClustering:
Expand Down
18 changes: 13 additions & 5 deletions src/pixelator/graph/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,15 @@ def ecount(self):
"""Get the total number of edges in the Graph instance."""
return self._backend.ecount()

def get_adjacency_sparse(self) -> csr_matrix:
"""Get the sparse adjacency matrix."""
return self._backend.get_adjacency_sparse()
def get_adjacency_sparse(
self, node_ordering: Iterable[Any] | None = None
) -> csr_matrix:
"""Get the sparse adjacency matrix.

:param node_ordering: Control the node ordering in the adjacency matrix
:return: a sparse adjacency matrix
"""
return self._backend.get_adjacency_sparse(node_ordering=node_ordering)

def connected_components(self) -> VertexClustering:
"""Get the connected components in the Graph instance."""
Expand Down Expand Up @@ -317,9 +323,11 @@ def local_g(
expression of the node itself. Default is 'gi'.
:return: A DataFrame of local G-scores for each node and marker.
"""
counts = self.node_marker_counts
A = self.get_adjacency_sparse(node_ordering=counts.index)
return local_g(
A=self.get_adjacency_sparse(),
counts=self.node_marker_counts,
A=A,
counts=counts,
k=k,
use_weights=use_weights,
normalize_counts=normalize_counts,
Expand Down
4 changes: 4 additions & 0 deletions src/pixelator/graph/node_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,10 @@ def local_g(
as it enhances spatial trends across neighborhoods in the graph, even if the marker counts within
individual nodes are sparse.

Note that it is important that the node ordering in the sparse adjacency matrix, and
the counts matrix line up. If calling this method directly the caller is responsible for
ensuring that this contract is fulfilled.

.. [1] Bivand, R.S., Wong, D.W.S. Comparing implementations of global and
local indicators of spatial association. TEST 27, 716–748 (2018).
https://doi.org/10.1007/s11749-018-0599-x
Expand Down
Loading