R/graph_metrics/node_interactions.R

Defines functions get_from_to_edges_for_node

source("R/graph_metrics/input_data.R")

get_from_to_edges_for_node <-
  function(gc, node_name, from_or_to = TRUE) {
    if (from_or_to) {
      interactions <- E(gc$no_loops_full_graph) [from (node_name)]
    }
    else {
      interactions <- E(gc$no_loops_full_graph) [to (node_name)]
    }
    interactions <- attr(interactions, "vnames")
    node_names <- c()
    
    for (i in 1:length(interactions)) {
      edge_i <- interactions[i]
      tokens <- strsplit(edge_i, "|", fixed = TRUE)
      if (from_or_to) {
        node_names [i] <- tokens[[1]][2]
        
      }
      else {
        node_names [i] <- tokens[[1]][1]
      }
    }
    df <- data.frame(node_names = node_names)
    df <- df %>%
      group_by(node_names) %>%
      summarise(num_interactions = n(), .groups = "drop")
    df <-
      df[order(df$num_interactions, decreasing = TRUE), ]
    
    return(df)
    
  }

to_path <-
  paste(OUTPUT,
        "/",
        "node_interactions_with_other_nodes_simple_graph",
        suffix,
        sep = "")

graph_context <- build_graph_context(data)

nodes_to_explore <- c("M08","M20", "M21", "M22", "M25", "M26", "G06", "G07", "G08")
all_plots <- list()

total_node_interactions <- data.frame(names=nodes_to_explore, from=0, to=0)
for (i in seq_along(nodes_to_explore)) {
  node_interactions_from <-
    get_from_to_edges_for_node(graph_context,
                               nodes_to_explore[i],
                               from_or_to = TRUE)
  
  from_node <- generate_bar_plot(
    node_interactions_from,
    'node_names',
    'num_interactions',
    paste(
      "Outgoing connections for node",
      nodes_to_explore[i],
      "for the interval [",
      start,
      ",",
      end,
      "]"
    ),
    "Outgoing Connections",
    paste('Reached nodes from', nodes_to_explore[i]),
    "Num Interactions",
    month_as_name = FALSE,
    show_bar_values = TRUE
  )
  node_interactions_to <-
    get_from_to_edges_for_node(graph_context,
                               nodes_to_explore[i],
                               from_or_to = FALSE)
  total_node_interactions[i, c(-1)] = c(nrow(node_interactions_from), 
                                        nrow(node_interactions_to))
  to_node <- generate_bar_plot(
    node_interactions_to,
    'node_names',
    'num_interactions',
    paste(
      "Incoming connections for node",
      nodes_to_explore[i],
      "for the interval [",
      start,
      ",",
      end,
      "]"
    ),
    "Incoming Connections",
    paste('Connected nodes to', nodes_to_explore[i]),
    "Num Interactions",
    month_as_name = FALSE,
    show_bar_values = TRUE
  )
  grid_plot <-
    ggarrange(from_node,
              to_node,
              ncol = 1,
              nrow = 2)
  all_plots[[i]] <- grid_plot
}

plots_to_pdf(all_plots,
             to_path,
             paper_type,
             paper_height,
             paper_width)

# Print neighbourhood table
to_path <-
  paste(OUTPUT, "/","nodes_connectivity.csv", sep="")
write.csv(total_node_interactions, to_path)


# Print cumulative detection by station table
detection_by_stations <- data %>% 
  select(station) %>% 
  group_by(station) %>% 
  summarise(detections=n())  %>% 
  mutate(percentage=round ((detections/nrow(data)) * 100, 2)) %>% 
  arrange(desc(percentage)) %>% 
  head(15) %>% 
  mutate(cum=cumsum(percentage))

to_path <-
  paste(OUTPUT, "/","cumulative_detections_by_stations.csv", sep="")
write.csv(detection_by_stations, to_path)
d2gex/seabasstfm documentation built on July 29, 2022, 2:20 a.m.