R/graph_metrics/all_individuals_monthly_metrics.R

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

all_monthly_data <- data %>%
  slide_data_by_date_interval(start, end, granularity = granularity)
to_path <-
  paste(OUTPUT,
        "/",
        "all_individuals_centrality_metrics",
        suffix,
        sep = "")
monthly_node_metrics <- get_node_monthly_metrics(all_monthly_data)

#------------------------------------------------------------------
#  Run heatmaps and barplots for all individuals montly metrics for
#  full, simple and loops graph
#------------------------------------------------------------------
log_info("Building metrics timeseries for all individuals...")
main_title <-
  paste("from",
        format_start_end(start),
        "to",
        format_start_end(end))
heatmap_plots <-
  build_all_timeseries_metrics (
    monthly_node_metrics,
    main_title,
    min_row_sum = 2,
    by_col_rows_order = HEAT_MAP_ORDER
  )
stat_plots <-
  build_all_timeseries_metric_stats(monthly_node_metrics,
                                    main_title, show_bar_values = FALSE)
all_plots <- c(rbind(heatmap_plots, stat_plots))


#... Print a heatmap per per metric and graph type for the timeseries
plots_to_pdf(all_plots,
             to_path,
             paper_type,
             paper_height,
             paper_width)

log_info("Metrics timeseries for all individuals has been processed")


#------------------------------------------------------------------
# Generate multiple grid graph for metrics
#------------------------------------------------------------------

# (1)
monthly_apl_metrics <- get_apl_monthly_metrics(data)
monthly_only_apl <-
  monthly_apl_metrics [, c('period', 'mean_distance')]

# (2) Build detection matrix
monthly_detections <- slide_data_by_date_interval(data,
                                                  start,
                                                  end,
                                                  granularity,
                                                  as_list = FALSE)

detections <- build_all_detection_matrices(monthly_detections,
                                           ind_or_station = FALSE)
# (3) Build metric matrix
metric_details <- rbind_all_metrics(monthly_node_metrics)

# (4) Merge detections, node metrics and apl metrics
full_metric_details <-
  merge_metrics_detections(metric_details$full_metrics, detections$all)
full_metric_details <- full_metric_details %>% select(-station)


# (5) Get grid for the full metric graph and yearly

metric_names <-
  c ("detections",
     "mean_distance",
     "degree",
     "betweenness",
     "eigenvector",
     "loops")
max_bars <- 4
years <- c("2020", "2021", "2022")


grid_plots <-
  generate_full_graph_grid_metrics_yearly(full_metric_details,
                                          monthly_only_apl,
                                          years,
                                          max_bars)

main_common_title <-
  "Original Graph: months with the highest metric scores and number of detections per year"
tgrob <- text_grob(main_common_title, size = 18, just = "centre")

grid_title_plot <- ggarrange(
  plotlist = list (as_ggplot(tgrob)),
  ncol = 1,
  nrow = 1
)
plot_list <- c(list(grid_title_plot), grid_plots)
all_plots <-
  ggarrange(
    plotlist = plot_list,
    ncol = 1,
    nrow = length(plot_list),
    heights = c(1, 5, 5, 5)
  )

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

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


# (6) Get grid for the full metric graph and for the whole series
metric_names <-
  c ("detections",
     "mean_distance",
     "degree",
     "betweenness",
     "eigenvector",
     "loops")
max_bars <- 6
grid_plots <-
  generate_full_graph_grid_metrics_whole_series(full_metric_details,
                                                monthly_only_apl,
                                                max_bars)

main_common_title <-
  "Original Graph: months with the highest metric scores and number of detections for the entire time series"
tgrob <- text_grob(main_common_title, size = 15, just = "centre")

grid_title_plot <- ggarrange(
  plotlist = list (as_ggplot(tgrob)),
  ncol = 1,
  nrow = 1
)

plot_list <- c(list(grid_title_plot), grid_plots)
all_plots <-
  ggarrange(
    plotlist = plot_list,
    ncol = 1,
    nrow = length(plot_list),
    heights = c(1, 5, 5)
  )

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

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


# (8)  Get grid metrics for the full graph for stations for
# the whole series
metric_names <-
  c ("detections", "degree", "betweenness", "eigenvector", "loops")
max_bars <- 10
full_metric_details <-
  merge_metrics_detections(metric_details$full_metrics, detections$all)
full_metric_details <- full_metric_details %>% select(-period)

grid_plots <-
  generate_all_graph_grid_metrics_stations_whole_series(full_metric_details,
                                                        max_bars,
                                                        is_full_graph = TRUE)

main_common_title <-
  "Original Graph: stations with highest metric scores and number of detections for the entire time series"
tgrob <- text_grob(main_common_title, size = 15, just = "centre")

grid_title_plot <- ggarrange(
  plotlist = list (as_ggplot(tgrob)),
  ncol = 1,
  nrow = 1
)
plot_list <- c(list(grid_title_plot), grid_plots)
all_plots <-
  ggarrange(
    plotlist = plot_list,
    ncol = 1,
    nrow = length(plot_list),
    heights = c(1, 5, 5)
  )

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

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


# (9)  Get grid metrics for the simple graph and simplified for stations for
# the whole series

metric_names <-
  c ("degree", "betweenness", "eigenvector")
max_bars <- 10
simplified_metric_details <-
  merge_metrics_detections(metric_details$simple_metrics,
                           detections$all,
                           is_full_graph = FALSE)

simplified_metric_details <-
  simplified_metric_details %>% select(-period)

grid_plots <-
  generate_all_graph_grid_metrics_stations_whole_series(simplified_metric_details,
                                                        max_bars,
                                                        is_full_graph = FALSE)

main_common_title <-
  "Simplified Graph: stations with the highest metric scores for the entire time series"
tgrob <- text_grob(main_common_title, size = 18, just = "centre")

grid_title_plot <- ggarrange(
  plotlist = list (as_ggplot(tgrob)),
  ncol = 1,
  nrow = 1
)
plot_list <- c(list(grid_title_plot), grid_plots)
all_plots <-
  ggarrange(
    plotlist = plot_list,
    ncol = 1,
    nrow = 3,
    heights = c(1, 5, 5)
  )


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

plots_to_pdf(list(all_plots),
             to_path,
             paper_type,
             paper_height,
             paper_width)
d2gex/seabasstfm documentation built on July 29, 2022, 2:20 a.m.