cwatson / brainGraph

Graph theory analysis of brain MRI data
173 stars 51 forks source link

robustness function, random error #25

Open mmpust opened 3 years ago

mmpust commented 3 years ago

Thank you for providing this nice R package. I want to calculate my network (igraph) robustness and vulnerability. When I run the robustness function with degree and betweeness centrality measures, it does run fine. However, for the random argument, I always get the following error message:

Error in { : task 3 failed - "Invalid vertex names"

Any ideas on how to solve the problem? Why is it just a problem with "random" and not the other two arguments? Thank you!

cwatson commented 3 years ago

When random is chosen, it uses the vertex names to remove vertices one-by-one. Does your graph have vertex names?

martaesse commented 3 years ago

Hello everyone! I am also trying to calculate my network robustness with the following command:

robustness(net_joint, type=c("vertex"), measure=c("random"))

but the error is always:

Error in { : task 3 failed - "Invalid vertex names"

as that reported by @mmpust !! I'm not really an R expert and maybe I can't import the vertex names correctly. Maybe can someone help me?

library("igraph") net_joint <- graph_from_data_frame(d=links, vertices=nodes, directed=F)

head(nodes) id name tipologia 1 nodo1 Highway 2 nodo2 Highway 3 nodo3 Highway 4 nodo4 Highway 5 nodo5 Highway 6 nodo6 Highway

net_joint

IGRAPH 41f50f0 UNW- 108 160 --

V(net_joint)$name [1] "nodo1" "nodo2" "nodo3" "nodo4" "nodo5" "nodo6" "nodo7" "nodo8" "nodo9"
[10] "nodo10" "nodo11" "nodo12" "nodo13" "nodo14" "nodo15" "nodo16" "nodo17" "nodo18" [19] "nodo19" "nodo20" "nodo21" "nodo22" "nodo23" "nodo24" "nodo25" "nodo26" "nodo27" [28] "nodo28" "nodo29" "nodo30" "nodo31" "nodo32" "nodo33" "nodo34" "nodo35" "nodo36" [37] "nodo37" "nodo38" "nodo39" "nodo40" "nodo41" "nodo42" "nodo43" "nodo44" "nodo45" [46] "nodo46" "nodo47" "nodo48" "nodo49" "nodo50" "nodo51" "nodo52" "nodo53" "nodo54" [55] "nodo55" "nodo56" "nodo57" "nodo58" "nodo59" "nodo60" "nodo61" "nodo62" "nodo63" [64] "nodo64" "nodo65" "nodo66" "nodo67" "nodo68" "nodo69" "nodo70" "nodo71" "nodo72" [73] "nodo73" "nodo74" "nodo75" "nodo76" "nodo77" "nodo78" "nodo79" "nodo80" "nodo81" [82] "nodo82" "nodo83" "nodo84" "nodo85" "nodo86" "nodo87" "nodo88" "nodo89" "nodo90" [91] "nodo91" "nodo92" "nodo93" "nodo94" "nodo95" "nodo96" "nodo97" "nodo98" "nodo99" [100] "nodo100" "nodo101" "nodo102" "nodo103" "nodo104" "nodo105" "nodo106" "nodo107" "nodo108"

robustness(net_joint, type=c("vertex"), measure=c("random")) Error in { : task 3 failed - "Invalid vertex names"

I really thank anyone could help me!!

mmpust commented 3 years ago

I was not able to solve the problem, even though the vertexes in my network have unique names.

cwatson commented 3 years ago

If one or both of you could send me (via emai if you prefer) the graph that is causing errors (as a .rds file) I can take a look and should be able to make a bugfix update soon (maybe this weekend).

martaesse commented 3 years ago

Thank you very much for your answer @cwatson. Here you find attached my graph. As you'll see it is a very very simple graph and I think the problem is related to my lack of experience with R (and with graphs in general). You will find my problem a bit silly, so thank you for your time.

graph.zip

cwatson commented 3 years ago

@martaesse I loaded your graph and was able to run, without error: robustness(g, type='vertex', measure='random')

I am not sure why you would see an error. Your post shows that your graph has names, but the graph you sent me is unnamed (they are just numbers); however, when I assign the same names I am still able to run the function.

Are you running on Windows, by chance? Please provide some system information, including the version of brainGraph you are using.

martaesse commented 3 years ago

Thank you @cwatson. I'm sorry I sent you a graph with different names from one in the post: this is because I did a lot of tests changing the names of the nodes, hoping that the error was in the type of name assigned, i.e. whether it was a number or a string. While the robustness command with the "degree" and "betweenness" option always works, the "random" one never does. (Robustness also works by removing edges randomly). Anyway, yes I use windows (Windows 10 Pro, version 20H2. 64-bit Operating System). Brain Graph is 3.0.0 version and R version 4.0.4. I really thank you for the help you are giving me.

cwatson commented 3 years ago

I see. My first recommendation would be to use a Linux system, as I have seen more foreach-related issues on Windows systems. I don't do development on Windows so it would be difficult for me to update the function and test it to make sure it works, as it works just fine on my computer. But I will try to update it so it doesn't use vertex names either way.

Out of curiosity, do you also see an error when you do robustness(g, type='edge', measure='random')?

A quick fix you could try would be to assign vertex names using one of the atlases in brainGraph. For example, in your specific case, you could do something like V(g)$name <- aal116$name[1:108] but of course this isn't a permanent solution.

martaesse commented 3 years ago

I really thank you. I tried your method to assign vertex name using aal116, but unfortunetely it does not work: I always get the same error Error in { : task 3 failed - "Invalid vertex names". As you suggest, I can use a Linux system to overcome this problem. This is really useful information for me. About your question..i don t get any error when using robustness(g, type='edge', measure='random'), everything works fine. Thanks again for your help!

xyang2uchicago commented 2 years ago

Hi, Christopher,

I have the same issue even when I running the code on Linux.

sessionInfo() R version 4.2.0 (2022-04-22) Platform: x86_64-pc-linux-gnu (64-bit) Running under: Scientific Linux 7.4 (Nitrogen)

Matrix products: default BLAS/LAPACK: /software/openblas-0.3.13-el7-x86_64/lib/libopenblas_haswellp-r0.3.13.so

locale: [1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C [3] LC_TIME=en_US.UTF-8 LC_COLLATE=en_US.UTF-8 [5] LC_MONETARY=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8 [7] LC_PAPER=en_US.UTF-8 LC_NAME=C [9] LC_ADDRESS=C LC_TELEPHONE=C [11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C

attached base packages: [1] stats graphics grDevices utils datasets methods base

other attached packages: [1] foreach_1.5.2 ggplot2_3.3.6 data.table_1.14.2 dplyr_1.0.9 [5] brainGraph_3.0.0 igraph_1.3.1

loaded via a namespace (and not attached): [1] pillar_1.7.0 compiler_4.2.0 BiocManager_1.30.18 [4] iterators_1.0.14 tools_4.2.0 lifecycle_1.0.1 [7] tibble_3.1.7 gtable_0.3.0 lattice_0.20-45 [10] pkgconfig_2.0.3 rlang_1.0.2 Matrix_1.4-1 [13] cli_3.3.0 DBI_1.1.2 parallel_4.2.0 [16] withr_2.5.0 generics_0.1.2 vctrs_0.4.1 [19] grid_4.2.0 tidyselect_1.1.2 glue_1.6.2 [22] R6_2.5.1 fansi_1.0.3 purrr_0.3.4 [25] magrittr_2.0.3 scales_1.2.0 codetools_0.2-18 [28] ellipsis_0.3.2 MASS_7.3-56 abind_1.4-5 [31] assertthat_0.2.1 colorspace_2.0-3 permute_0.9-7 [34] utf8_1.2.2 doParallel_1.0.17 munsell_0.5.0 [37] crayon_1.5.1

My object is very simply as below and does have names for each vertex.

g graph_list[[15]] IGRAPH a7ef063 UN-- 33 144 --

  • attr: name (v/c), logFC_ave (v/n), FDR (v/n), combined_score | (e/n), btwn (e/n)
  • edges from a7ef063 (vertex names): [1] Icam2--Cd34 Icam2--Cd34 Gata2--Cd34 Gata2--Cd34 Icam2--Cd40 [6] Icam2--Cd40 Cd34 --Cd40 Cd34 --Cd40 Gata2--Hhex Gata2--Hhex [11] Cd34 --Hhex Cd34 --Hhex Gata2--Sox17 Gata2--Sox17 Cd34 --Sox17 [16] Cd34 --Sox17 Hhex --Sox17 Hhex --Sox17 Icam2--Cdh5 Icam2--Cdh5 [21] Gata2--Cdh5 Gata2--Cdh5 Cd34 --Cdh5 Cd34 --Cdh5 Hhex --Cdh5 [26] Hhex --Cdh5 Sox17--Cdh5 Sox17--Cdh5 Npr1 --Cdh5 Npr1 --Cdh5 [31] Npr1 --Gngt2 Npr1 --Gngt2 Icam2--Igf1 Icam2--Igf1 Gata2--Igf1
  • ... omitted several edges

However, i can't go through to get the random estimation

robustness(g, "vertex", measure='random', N=3) Error in { : task 3 failed - "Invalid vertex names"

Could you please have a look and give solution?

Thank you, Holly

tenguzame commented 1 year ago

Greetings, I am encountering the very same error, both on Windows and Linux. Using R v.4.2.1 on RStudio. I tried changing the vertex names, assigning random ones in different combinations, by using only strings or whatever, but the results are always the same: Error in { : task 3 failed - "Invalid vertex names" Has anybody managed to find a workaround? Best regards

cwatson commented 1 year ago

Sorry for such a late response. I can replicate the error with some data provided by another user, so I'll have to work on an update that avoids using the vertex names. It does not appear to be a Windows issue, nor is it a R v3.x vs. v4.x issue.

I'll try to get a bugfix update in the next few weeks.

scottwlee commented 1 year ago

Hello, I also exprience this problem, when I use "vertext" type and "random" measure. Could you look over it again, cwatson?

cwatson commented 1 year ago

Yes, I'll have to keep working on it. Sorry for such a long delay.

sacrull commented 8 months ago

Hello, I think I figured out what the issue was. I believe when it was going through the second loop it was deleting the vertices from the graph and when it tried to run the loop for the second time the graph (g) had no vertices since they were previously deleted and that is why it had the error.

if (type == 'vertex') {
    if (measure == 'random') {
      max.comp <- foreach(i=seq_len(N), .combine='cbind') %dopar% {
        ord <- V(g)$name[index[, i]]
        tmp <- rep.int(orig_max, n)
        for (j in seq_len(n - 1L)) { 
          g <- delete_vertices(g, ord[j]) #this specifically was causing the error i believe
          tmp[j + 1L] <- max(components(g)$csize)
        }
        tmp
      }
      max.comp.removed <- rowMeans(max.comp)
    }

I fixed it by changing it by changing that piece of code to:

if (type == 'vertex') {
    if (measure == 'random') {
      max.comp <- foreach(i=seq_len(N), .combine='cbind') %dopar% {
        ord <- igraph::V(g)$name[index[, i]]
        tmp <- rep.int(orig_max, n)
        g.new <- g # added line
        for (j in seq_len(n - 1L)) {
          g.new <- igraph::delete_vertices(g.new, ord[j]) #updated line
          tmp[j + 1L] <- max(igraph::components(g.new)$csize) # updated line
        }
        tmp
      }
      max.comp.removed <- rowMeans(max.comp)

    }

Hope this helps and thank you for this wonderful package!