Repository: martinfleis/momepy
Pull request #92 (2019-10-10 21:52:21)

BUG: straightness centrality of disconnected node
Straightness centrality of disconnected node currently raises error due to division by zero. Fixed.

codecov[bot]: # [Codecov](https://codecov.io/gh/martinfleis/momepy/pull/92?src=pr&el=h1) Report
> Merging [#92](https://codecov.io/gh/martinfleis/momepy/pull/92?src=pr&el=desc) into [master](https://codecov.io/gh/martinfleis/momepy/commit/857c9a5c1a2fdbf29f670064b5ab44f2fabd1a6c?src=pr&el=desc) will **decrease** coverage by `0.03%`.
> The diff coverage is `75%`.

[![Impacted file tree graph](https://codecov.io/gh/martinfleis/momepy/pull/92/graphs/tree.svg?width=650&token=i04999UxkK&height=150&src=pr)](https://codecov.io/gh/martinfleis/momepy/pull/92?src=pr&el=tree)

```diff
@@            Coverage Diff             @@
##           master      #92      +/-   ##
==========================================
- Coverage   93.26%   93.23%   -0.04%     
==========================================
  Files           9        9              
  Lines        2436     2438       +2     
==========================================
+ Hits         2272     2273       +1     
- Misses        164      165       +1
```


| [Impacted Files](https://codecov.io/gh/martinfleis/momepy/pull/92?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [momepy/graph.py](https://codecov.io/gh/martinfleis/momepy/pull/92/diff?src=pr&el=tree#diff-bW9tZXB5L2dyYXBoLnB5) | `97.1% <75%> (-0.4%)` | :arrow_down: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/martinfleis/momepy/pull/92?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/martinfleis/momepy/pull/92?src=pr&el=footer). Last update [857c9a5...abcdc12](https://codecov.io/gh/martinfleis/momepy/pull/92?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).


Patch:
diff --git a/momepy/graph.py b/momepy/graph.py
index 52cce56..2ecd5de 100644
--- a/momepy/graph.py
+++ b/momepy/graph.py
@@ -993,8 +993,11 @@ def _straightness_centrality(G, weight, normalized=True):
             straightness_centrality[n] = straightness * (1.0 / (len(G) - 1.0))
             # normalize to number of nodes-1 in connected part
             if normalized:
-                s = (len(G) - 1.0) / (len(sp) - 1.0)
-                straightness_centrality[n] *= s
+                if len(sp) > 1:
+                    s = (len(G) - 1.0) / (len(sp) - 1.0)
+                    straightness_centrality[n] *= s
+                else:
+                    straightness_centrality[n] = 0
         else:
             straightness_centrality[n] = 0.0
     return straightness_centrality


Test patch:
diff --git a/tests/test_graph.py b/tests/test_graph.py
index e4c1c06..fe79b81 100644
--- a/tests/test_graph.py
+++ b/tests/test_graph.py
@@ -125,12 +125,18 @@ class TestGraph:
         net2 = mm.straightness_centrality(
             self.network, normalized=False, name="nonnorm"
         )
+        G = self.network.copy()
+        G.add_node(1)
+        net3 = mm.straightness_centrality(G)
         check = 0.8574045143712158
         nonnorm = 0.8574045143712158
         assert (
             net.nodes[(1603650.450422848, 6464368.600601688)]["straightness"] == check
         )
         assert net2.nodes[(1603650.450422848, 6464368.600601688)]["nonnorm"] == nonnorm
+        assert (
+            net3.nodes[(1603650.450422848, 6464368.600601688)]["straightness"] == check
+        )
 
     def test_local_straightness_centrality(self):
         net = mm.local_straightness_centrality(self.network)