UrbanAnalyst / dodgr

Distances on Directed Graphs in R
https://urbananalyst.github.io/dodgr/
127 stars 16 forks source link

dodgr_flows_disperse behavior seems inconsistent #142

Closed romainFr closed 4 years ago

romainFr commented 4 years ago

I'm probably misunderstanding what the function is supposed to do, but I was perplexed by results when running it on a very minimal example, say:

graph1 <- data.frame(from=c("a","a","d","d"),
                to=c("b","c","a","b"),
                d=c(5,6,3,2))

dodgr_flows_disperse(graph1,from="d",k=1,dens=1)
  from to d      flow
1    a  b 5 0.0000000
2    a  c 6 0.0000000
3    d  a 3 0.2689414
4    d  b 2 0.7310586

My first incomprehension is why the flow is not dispersed to the edges coming from a. Then comes the even more perplexing bit:

graph2 <- data.frame(from=c("d","d","a","a"),
                     to=c("a","b","b","c"),
                     d=c(3,2,5,6))

dodgr_flows_disperse(graph2,from="d",k=1,dens=1)
  from to d flow
1    d  a 3    0
2    d  b 2    0
3    a  b 5    0
4    a  c 6    1

With the exact same graph (the only difference is the order of the edges in the data frame), I get a different result. Am I doing something absurd (I'm guessing I am)? And thanks for the package, btw!

mpadge commented 4 years ago

yep, thank you so much @romainFr, you were indeed right in being confused there. It was a bug that i had fixed in all other flow aggregation routines, but not flows_disperse. The above commit fixes and now gives the following (with k=10 just to yield higher values to ease visual comparison):

library(dodgr)
graph1 <- data.frame(from=c("a","a","d","d"),
                     to=c("b","c","a","b"),
                     d=c(5,6,3,2))
dodgr_flows_disperse(graph1,from="d",k=10,dens=1)
#>   from to d      flow
#> 1    a  b 5 0.0000000
#> 2    a  c 6 0.2067880
#> 3    d  a 3 0.3767922
#> 4    d  b 2 0.4164198

graph2 <- data.frame(from=c("d","d","a","a"),
                     to=c("a","b","b","c"),
                     d=c(3,2,5,6))
dodgr_flows_disperse(graph2,from="d",k=10,dens=1)
#>   from to d      flow
#> 1    d  a 3 0.3767922
#> 2    d  b 2 0.4164198
#> 3    a  b 5 0.0000000
#> 4    a  c 6 0.2067880

Created on 2020-08-27 by the reprex package (v0.3.0)

(And there is no flow dispersed from a to b, because d->b is the shortest path there, so all flow from d to b disperses along that path only.)


I'll add this bug fix to news straight away and ping you there. Thanks for helping to improve the package!