Skip to content

Commit

Permalink
OD review: replace mo, tmo by miic_obj, tmiic_obj
Browse files Browse the repository at this point in the history
  • Loading branch information
franck-simon committed Sep 4, 2024
1 parent 3b29af1 commit 86d12cf
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 82 deletions.
24 changes: 12 additions & 12 deletions R/miic.plot.R
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
#' and their width is based on the conditional mutual information
#' minus the complexity cost.
#'
#' @param mo [a miic object, required]
#' @param miic_obj [a miic object, required]
#'
#' The object returned by the \code{\link{miic}} execution.
#'
Expand Down Expand Up @@ -144,18 +144,18 @@
#'
#' }
#-------------------------------------------------------------------------------
export <- function (mo, method="igraph", pcor_palette=NULL,
export <- function (miic_obj, method="igraph", pcor_palette=NULL,
display="compact", show_self_loops=TRUE)
{
if ( is.null(mo$summary) )
if ( is.null(miic_obj$summary) )
stop("The inferred network does not exist")
if ( (!is.null(method)) && (method != "igraph") )
stop("Method not supported")

if ( is.null(mo$tmiic) )
return (getIgraph(mo, pcor_palette=pcor_palette))
if ( is.null(miic_obj$tmiic) )
return (getIgraph(miic_obj, pcor_palette=pcor_palette))
else
return (tmiic_getIgraph (mo, pcor_palette=pcor_palette,
return (tmiic_getIgraph (miic_obj, pcor_palette=pcor_palette,
display=display, show_self_loops=show_self_loops))
}

Expand All @@ -173,7 +173,7 @@ export <- function (mo, method="igraph", pcor_palette=NULL,
#' (negative is blue, null is gray and positive is red) and their width is
#' based on the conditional mutual information minus the complexity cost.
#'
#' @param mo [a miic object]
#' @param miic_obj [a miic object]
#' The object returned by the \code{\link{miic}} execution.
#' @param pcor_palette The color palette used to represent the partial correlations
#' (the color of the edges). The palette must be able to handle 201 shades
Expand All @@ -189,15 +189,15 @@ export <- function (mo, method="igraph", pcor_palette=NULL,
#' \code{\link[igraph]{igraph.plotting}} for the detailed description of the
#' plotting parameters and \code{\link[igraph]{layout}} for different layouts.
#-------------------------------------------------------------------------------
getIgraph <- function(mo, pcor_palette = NULL) {
if (is.null(mo$summary)) {
getIgraph <- function(miic_obj, pcor_palette = NULL) {
if (is.null(miic_obj$summary)) {
stop("The inferred network does not exist.")
}
if (!base::requireNamespace("igraph", quietly = TRUE)) {
stop("Package 'igraph' is required.")
}

summary = mo$summary[mo$summary$type %in% c('P', 'TP', 'FP'), ]
summary = miic_obj$summary[miic_obj$summary$type %in% c('P', 'TP', 'FP'), ]
if (nrow(summary) > 0) {
# Re-order summary so that all edges go from "x" to "y"
for(row in 1:nrow(summary)){
Expand All @@ -218,8 +218,8 @@ getIgraph <- function(mo, pcor_palette = NULL) {
}

# Create igraph object from summary
ig_graph = igraph::graph_from_data_frame(summary,
vertices=colnames(mo$adj_matrix))
ig_graph = igraph::graph_from_data_frame (summary,
vertices=colnames(miic_obj$adj_matrix) )

# Set nodes visuals
igraph::V(ig_graph)$color <- "lightblue"
Expand Down
68 changes: 34 additions & 34 deletions R/tmiic.plot.R
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
# conditional mutual information minus the complexity cost.
#
# params:
# - tmo: a tmiic object, returned by the miic execution in temporal mode
# - tmiic_obj: a tmiic object, returned by the miic execution in temporal mode
#
# - display: string. Optional, default value "compact".
# Possible values are "raw", "lagged", "compact", "combine", "unique", "drop":
Expand Down Expand Up @@ -67,15 +67,15 @@
#
# returns: an igraph graph object.
#-------------------------------------------------------------------------------
tmiic_getIgraph <- function (tmo, display="compact",
tmiic_getIgraph <- function (tmiic_obj, display="compact",
show_self_loops=TRUE, pcor_palette=NULL)
{
if (display == "lagged")
tmo$summary = tmo$tmiic$stationarity
tmiic_obj$summary = tmiic_obj$tmiic$stationarity
else if (display != "raw")
tmo <- tmiic_flatten_network (tmo, flatten_mode=display,
tmiic_obj <- tmiic_flatten_network (tmiic_obj, flatten_mode=display,
keep_edges_on_same_node=show_self_loops)
graph <- getIgraph (tmo, pcor_palette=pcor_palette)
graph <- getIgraph (tmiic_obj, pcor_palette=pcor_palette)

if (display %in% c("raw", "lagged") )
{
Expand All @@ -86,8 +86,8 @@ tmiic_getIgraph <- function (tmo, display="compact",
else
{
igraph::E(graph)$curved = FALSE
if ( "lag" %in% colnames(tmo$summary) )
igraph::E(graph)$label <- tmo$summary$lag
if ( "lag" %in% colnames(tmiic_obj$summary) )
igraph::E(graph)$label <- tmiic_obj$summary$lag
}
return(graph)
}
Expand All @@ -106,9 +106,9 @@ tmiic_getIgraph <- function (tmo, display="compact",
#
# @return [a tmiic object] The modified tmiic object
#-----------------------------------------------------------------------------
tmiic_prepare_edges_for_plotting <- function (tmo)
tmiic_prepare_edges_for_plotting <- function (tmiic_obj)
{
df_edges <- tmo$summary[tmo$summary$type %in% c('P', 'TP', 'FP'), ]
df_edges <- tmiic_obj$summary[tmiic_obj$summary$type %in% c('P', 'TP', 'FP'), ]
if (nrow(df_edges) <= 0)
df_edges$xy = character(0)
else
Expand Down Expand Up @@ -145,8 +145,8 @@ tmiic_prepare_edges_for_plotting <- function (tmo)
}
}
}
tmo$summary <- df_edges
return (tmo)
tmiic_obj$summary <- df_edges
return (tmiic_obj)
}

#-------------------------------------------------------------------------------
Expand All @@ -163,9 +163,9 @@ tmiic_prepare_edges_for_plotting <- function (tmo)
#
# @return df_mult [a dataframe] The dataframe containing the multiple edges
#-------------------------------------------------------------------------------
tmiic_get_multiple_edges_for_plotting <- function (tmo)
tmiic_get_multiple_edges_for_plotting <- function (tmiic_obj)
{
df_mult <- tmo$summary
df_mult <- tmiic_obj$summary
if (nrow(df_mult) <= 0)
df_mult$count <- numeric(0)
else
Expand Down Expand Up @@ -423,17 +423,17 @@ tmiic_compute_row_layout_greedy_recurs <- function (node_left, node_right,
# The layout is completed with nodes without edges to produce the final
# layer 0 layout.
#
# param: tmo, the object returned by the miic execution in temporal mode
# param: tmiic_obj, the object returned by the miic execution in temporal mode
#
# returns: a list, the position along an axis for each node
#-------------------------------------------------------------------------------
tmiic_compute_row_layout_greedy <- function (tmo)
tmiic_compute_row_layout_greedy <- function (tmiic_obj)
{
list_nodes_not_lagged <- tmo$state_order$var_names
list_nodes_not_lagged <- tmiic_obj$state_order$var_names
#
# Filter out self edges, count and summarize edges regardless their lags
#
tmiic_flat <- tmiic_flatten_network (tmo)
tmiic_flat <- tmiic_flatten_network (tmiic_obj)
df_edges <- tmiic_flat$summary
df_edges <- df_edges[(df_edges$x != df_edges$y),]
if (nrow (df_edges) == 0)
Expand Down Expand Up @@ -490,15 +490,15 @@ tmiic_compute_row_layout_greedy <- function (tmo)
# This function computes the layout so that the less layers
# has a node, the more to the exteriors it will be placed.
#
# param: tmo, a tmiic object returned by the execution of miic
# param: tmiic_obj, a tmiic object returned by the execution of miic
# in temporal mode ("raw" graph_type)
#
# returns: a list, the position along an axis for each node
#-------------------------------------------------------------------------------
tmiic_compute_row_layout_layers <- function (tmo)
tmiic_compute_row_layout_layers <- function (tmiic_obj)
{
n_nodes_not_lagged <- nrow(tmo$state_order)
list_n_layers_back <- tmo$state_order$n_layers - 1
n_nodes_not_lagged <- nrow(tmiic_obj$state_order)
list_n_layers_back <- tmiic_obj$state_order$n_layers - 1
n_layers_back_max <- max (list_n_layers_back)
#
# Precompute the rows on the grid, putting nodes with the less lags
Expand Down Expand Up @@ -547,19 +547,19 @@ tmiic_compute_row_layout_layers <- function (tmo)
# This function computes the layout using Sugiyama algorithm to
# minimize crossing edges
#
# param: tmo, a tmiic object returned by the execution of miic
# param: tmiic_obj, a tmiic object returned by the execution of miic
# in temporal mode ("raw" graph_type)
#
# returns: a list, the position along an axis for each node
#-------------------------------------------------------------------------------
tmiic_compute_row_layout_sugiyama <- function (tmo)
tmiic_compute_row_layout_sugiyama <- function (tmiic_obj)
{
list_nodes_not_lagged <- tmo$state_order$var_names
list_nodes_not_lagged <- tmiic_obj$state_order$var_names
n_nodes_not_lagged <- length(list_nodes_not_lagged)
#
# Filter out self edges, count and summarize edges regardless their lags
#
tmiic_flat <- tmiic_flatten_network(tmo)
tmiic_flat <- tmiic_flatten_network(tmiic_obj)
df_edges <- tmiic_flat$summary
df_edges <- df_edges[(df_edges$x != df_edges$y),]
if (nrow(df_edges) == 0)
Expand Down Expand Up @@ -596,7 +596,7 @@ tmiic_compute_row_layout_sugiyama <- function (tmo)
# graphs
#
# params:
# - tmo, a tmiic object returned by the miic's execution in temporal mode.
# - tmiic_obj, a tmiic object returned by the miic's execution in temporal mode.
#
# - display: string. optional, default value "raw".
# Possible values are "raw" and "lagged".
Expand Down Expand Up @@ -625,7 +625,7 @@ tmiic_compute_row_layout_sugiyama <- function (tmo)
#
# returns: a matrix, the layout to use for drawing
#-------------------------------------------------------------------------------
tmiic_compute_grid_layout <- function (tmo, display="raw",
tmiic_compute_grid_layout <- function (tmiic_obj, display="raw",
positioning="greedy", orientation="L")
{
if (! display %in% c("raw", "lagged") )
Expand All @@ -635,7 +635,7 @@ tmiic_compute_grid_layout <- function (tmo, display="raw",
if (! orientation %in% c("L", "P") )
stop ("Error: Invalid orientation parameter")

nodes_not_lagged <- tmo$state_order$var_names
nodes_not_lagged <- tmiic_obj$state_order$var_names
n_nodes_not_lagged <- length (nodes_not_lagged)
#
# Precompute the layer 0 layout
Expand All @@ -652,18 +652,18 @@ tmiic_compute_grid_layout <- function (tmo, display="raw",
list_pos_of_nodes <- unlist (list_pos_of_nodes)
}
if (positioning == "layers")
list_pos_of_nodes <- tmiic_compute_row_layout_layers (tmo)
list_pos_of_nodes <- tmiic_compute_row_layout_layers (tmiic_obj)
if (positioning == "greedy")
list_pos_of_nodes <- tmiic_compute_row_layout_greedy (tmo)
list_pos_of_nodes <- tmiic_compute_row_layout_greedy (tmiic_obj)
if (positioning == "sugiyama")
list_pos_of_nodes <- tmiic_compute_row_layout_sugiyama (tmo)
list_pos_of_nodes <- tmiic_compute_row_layout_sugiyama (tmiic_obj)
if ( is.null (list_pos_of_nodes) )
stop ("Error: Layout can not be infered")
#
# As contextual nodes are placed in an extra column/row when display is "raw",
# here we update the nodes positions to maintain a "nice" display
#
is_contextual <- tmo$state_order$is_contextual
is_contextual <- tmiic_obj$state_order$is_contextual
if ( (display == "raw") & (sum(is_contextual) > 0) )
{
list_pos_upd <- list_pos_of_nodes
Expand Down Expand Up @@ -708,9 +708,9 @@ tmiic_compute_grid_layout <- function (tmo, display="raw",
#
# Place contextual and lag0 nodes
#
list_n_layers_back <- tmo$state_order$n_layers - 1
list_n_layers_back <- tmiic_obj$state_order$n_layers - 1
n_layers_back_max <- max (list_n_layers_back)
list_delta_t <- tmo$state_order$delta_t
list_delta_t <- tmiic_obj$state_order$delta_t
max_lags <- max (list_n_layers_back * list_delta_t)

df_layout <- data.frame ( col=integer(), row=integer() )
Expand Down
Loading

0 comments on commit 86d12cf

Please sign in to comment.