igraph / rigraph

igraph R package
https://r.igraph.org
544 stars 200 forks source link

error #1289

Closed maelle closed 6 months ago

maelle commented 6 months ago

Related to #1286 (@Antonov548) I extracted all examples in an Rmd file to knit it and see what fails.

I get

Error in `sample_degseq()`:
! At vendor/cigraph/src/games/degree_sequence_vl/gengraph_mr-connected.cpp:150 : Cannot realize the given degree sequence as an undirected, simple graph. Invalid value
Backtrace:
 1. igraph::sample_degseq(degs, method = "vl")

for

### Name: sample_degseq
### Title: Generate random graphs with a given degree sequence
### Aliases: sample_degseq degseq
### Keywords: graphs

### ** Examples

## The simple generator
g <- sample_degseq(rep(2, 100))
degree(g)
is_simple(g) # sometimes TRUE, but can be FALSE
g2 <- sample_degseq(1:10, 10:1)
degree(g2, mode = "out")
degree(g2, mode = "in")

## The vl generator
g3 <- sample_degseq(rep(2, 100), method = "vl")
degree(g3)
is_simple(g3) # always TRUE

## Exponential degree distribution
## Note, that we correct the degree sequence if its sum is odd
degs <- sample(1:100, 100, replace = TRUE, prob = exp(-0.5 * (1:100)))
if (sum(degs) %% 2 != 0) {
  degs[1] <- degs[1] + 1
}
g4 <- sample_degseq(degs, method = "vl")
all(degree(g4) == degs)

## Power-law degree distribution
## Note, that we correct the degree sequence if its sum is odd
degs <- sample(1:100, 100, replace = TRUE, prob = (1:100)^-2)
if (sum(degs) %% 2 != 0) {
  degs[1] <- degs[1] + 1
}
g5 <- sample_degseq(degs, method = "vl")
all(degree(g5) == degs)

but running this code directly, or with reprex... does NOT lead to any error. Maybe due to some randomness?

szhorvat commented 6 months ago

Maybe due to some randomness?

Yes.

There's no guarantee that randomly picked integers will form a graphical degree sequence (i.e. that there's a graph with these degrees).

maelle commented 6 months ago

should we set the seed for this example then?

szhorvat commented 6 months ago

yes

Antonov548 commented 6 months ago

I also wondering what is the issue in github actions and should we install something to docker image to make it work?

Error in .External2(C_X11, d$display, d$width, d$height, d$pointsize,  : 
  unable to start device X11cairo
Calls: source -> withVisible -> eval -> eval -> eval -> eval -> x11
In addition: Warning message:
In x11(width = 10, height = 5) :
  unable to open connection to X11 display ''
maelle commented 6 months ago

let me try finding a seed that works

maelle commented 6 months ago

this might require making the example dependent on installing withr, or adding more boiler plate to preserve the seed.

maelle commented 6 months ago

another thing I want to fix is the use of the same name for two variables in the same examples (degs)

szhorvat commented 6 months ago

this might require making the example dependent on installing withr, or adding more boiler plate to preserve the seed.

Alternatively, you can keep generating degree sequences until is_graphical returns true. We can make it so that the probability of getting a good sequence is high (but it's next to impossible to make it guaranteed).

I'm not sure if R has a one-liner for this? In the Mathematica interface of igraph I even have a helper function for rejection sampling, i.e. for trying and retrying until a condition is satisfied.

maelle commented 6 months ago

@Antonov548 is your issue a distinct issue?

maelle commented 6 months ago

I feel the example would be simpler to read if we simply preserved the seed.

Antonov548 commented 6 months ago

I feel the example would be simpler to read if we simply preserved the seed.

I think so. It's more related to the fact there is quite a lot of examples which draws a charts. I don't have much expertise in it, but on GitHub actions there is not UI and I guess it's failing to create window with chart.

maelle commented 6 months ago

Ok, let's tackle this in #1300