Skip to content

Commit

Permalink
SETSe NOT FLOW DEPENDENT
Browse files Browse the repository at this point in the history
I have removed the need for flow and edge capacity in all SETSe functions. This is becuase they are only useful on flow networks and not a major component of networks or graphs in general.
This is a major update and breaks all previous code using this package.
  • Loading branch information
Jonathan Bourne committed Apr 21, 2020
1 parent ed2ee3f commit 9b925ab
Show file tree
Hide file tree
Showing 30 changed files with 324 additions and 299 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: NetworkSpringEmbedding
Title: Calculating the Strain Elevation Tension Spring embeddings for a network
Version: 0.1
Version: 0.3
Authors@R: person("Jonathan", "Bourne", email = "jonathan.bourne.15@ucl.ac.uk.com", role = c("aut", "cre"))
Description: This package is used to calculate the Strain Elevation and Tension embeddings produced by the SETSe algorithm.
Depends: R (>= 3.4.2)
Expand Down
3 changes: 2 additions & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@ export(Create_stabilised_blocks)
export(Create_stabilised_blocks_expanded)
export(Distance)
export(ForceV_from_angle)
export(SETSe)
export(SETSe2)
export(SETSe_bicomp)
export(SETSe_bicomp_expanded)
export(SETSe_core)
export(SETSe_core_expanded)
export(SETSe_data_prep)
export(SETSe_data_prep_orig)
export(SETSe_expanded)
export(angle_from_solved_heights)
export(auto_SETSe)
Expand Down
17 changes: 2 additions & 15 deletions R/Create_balanced_blocks.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,11 @@
#'
#' @param g An igraph object. The network for which embeddings will be found
#' @param force A character vector. The name of the node attribute that is the force exerted by the nodes
#' @param flow A character vector. The name of the edge attribute that contains the flow over each edge
#' @return A list containing all the bi connected component where each component is balanced to have a net force of 0.
#'
#' @export

create_balanced_blocks <- function(g, force = "net_generation", flow = "power_flow"){
create_balanced_blocks <- function(g, force = "force"){
#This function creates a list of biconnected components or blocks.
#These blocks are balanced such that the connecting vertices contain all the power of the missing part of the network
#balancing prevents the network reaching a steady state non-zero velocity.
Expand Down Expand Up @@ -69,19 +68,7 @@ create_balanced_blocks <- function(g, force = "net_generation", flow = "power_fl
left_join(biconnected_component, ., by = "name") %>%
mutate(temp = ifelse(is.na(AuxPower), !!sym(force), AuxPower)) #

# This has been commented out as it is unclear what benefits it provides and causes errors when flow
# is only a nominal value not a real part of the network
# #makes the nodes of the two node 1 edge block have the same power as that which flows between them.
# #this forces a balance
# #The nodes are by default in alphabetical order so the polarity of the power flow will always be correct
# if(nrow(edge_df)==1){
# balanced_component_df <- balanced_component_df %>%
# mutate(temp = edge_df %>%
# pull(., !!sym(flow)) %>% c(., -.) %>%
# #should this rounding be kept? maybe tiny value aren't a problem? the thing is solved seperately anyway
# round(., 10)) #rounding stops tiny values
# }


Component_j <- {!(get.vertex.attribute(g, "name") %in% balanced_component_df$name)} %>%
(1:vcount(g))[.] %>%
delete.vertices(g,.)
Expand Down
9 changes: 3 additions & 6 deletions R/Create_stabilised_blocks .R
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
#' @param OriginBlock_number An integer. this is the origin block chosen from the
#' create_stable_blocks function. Usually this will be the largest block.
#' @param force A character string. This is the node attribute that contains the force the nodes exert on the network.
#' @param flow A character string. This is the edge attribute that is the power flow on the edges.
#' @param edge_name A character string. This is the edge attribute that contains the edge_name of the edges.
#' @param tstep A numeric. The time interval used to iterate through the network dynamics.
#' @param coef_drag A numeric. This sets the multiplier of friction. Only use if you want to be annoyed and confused
Expand All @@ -27,8 +26,7 @@
Create_stabilised_blocks <- function(g,
OriginBlock,
OriginBlock_number,
force ="net_generation",
flow = "power_flow",
force ="force",
edge_name = "edge_name",
tstep=0.1,
coef_drag = coef_drag,
Expand All @@ -41,7 +39,7 @@ Create_stabilised_blocks <- function(g,

#Seperate out the graph into balanced blocks
#This step will have already been done, but it is fast and simplifies the requirements for the function
List_of_BiConComps <- create_balanced_blocks(g, force = force, flow = flow)
List_of_BiConComps <- create_balanced_blocks(g, force = force)

#remove the Origin block so it doesn't have to be calculated again
BlockNumbers <-(1:length(List_of_BiConComps))[-OriginBlock_number]
Expand All @@ -53,8 +51,7 @@ Create_stabilised_blocks <- function(g,
map(~{

Out <- SETSe(List_of_BiConComps[[.x]],
force =force,
flow = flow,
force =force,
tstep = tstep,
coef_drag = coef_drag,
tol = tol*sum(abs(get.vertex.attribute(List_of_BiConComps[[.x]], force)))/total_force, #the force has to be scaled to the component
Expand Down
11 changes: 4 additions & 7 deletions R/Create_stabilised_blocks_expanded.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
#' @param OriginBlock_number An integer. this is the origin block chosen from the
#' create_stable_blocks function. Usually this will be the largest block.
#' @param force A character string. This is the node attribute that contains the force the nodes exert on the network.
#' @param flow A character string. This is the edge attribute that is the power flow on the edges.
#' @param edge_name A character string. This is the edge attribute that contains the edge_name of the edges.
#' @param tstep A numeric. The time interval used to iterate through the network dynamics.
#' @param coef_drag A numeric. This sets the multiplier of friction. Only use if you want to be annoyed and confused
Expand All @@ -26,8 +25,7 @@
Create_stabilised_blocks_expanded <- function(g,
OriginBlock,
OriginBlock_number,
force ="net_generation",
flow = "power_flow",
force ="force",
edge_name = "edge_name",
tstep=0.1,
coef_drag = coef_drag,
Expand All @@ -40,7 +38,7 @@ Create_stabilised_blocks_expanded <- function(g,

#Seperate out the graph into balanced blocks
#This step will have already been done, but it is fast and simplifies the requirements for the function
List_of_BiConComps <- create_balanced_blocks(g, force = force, flow = flow)
List_of_BiConComps <- create_balanced_blocks(g, force = force)

#remove the Origin block so it doesn't have to be calculated again
BlockNumbers <-(1:length(List_of_BiConComps))[-OriginBlock_number]
Expand All @@ -49,8 +47,7 @@ Create_stabilised_blocks_expanded <- function(g,
map(~{

Out <- SETSe_expanded(List_of_BiConComps[[.x]],
force =force,
flow = flow,
force =force,
tstep = tstep,
coef_drag = coef_drag,
tol = tol,
Expand Down Expand Up @@ -94,7 +91,7 @@ Create_stabilised_blocks_expanded <- function(g,

message("creating adjustment matrices")
component_adjust_mat <- adjust_components(g, max_iter = max_iter,
force = force, flow = flow)
force = force)


#print(table(relative_blocks$Iter))
Expand Down
68 changes: 27 additions & 41 deletions R/SETSe.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,12 @@
#'
#' This is the basic SETS embeddings algorithm, it outputs all elements of the embeddings as well as convergence dynamics. It is a
#' wrapper around the core SETS algorithm which requires data preparation and only produces node embeddings and entwork dynamics.
#' Flow and capacity are only inlcuded for legacy reasons and an arbitray value should be used for both if it is not applicable.
#'
#' This function is often used in conjunction with \code{Create_stabilised_blocks} and \code{create_balanced_blocks}
#'
#' @param g An igraph object
#' @param force A character string. This is the node attribute that contains the force the nodes exert on the network.
#' @param flow A character string. This is the edge attribute that is the power flow on the edges.
#' @param distance A character string. The edge attribute that contains the original/horizontal distance between nodes.
#' @param capacity A character string. This is the edge attribute that is the flow limit of the edges.
#' @param edge_name A character string. This is the edge attribute that contains the edge_name of the edges.
#' @param k A character string. This is k for the moment don't change it.
#' @param tstep A numeric. The time interval used to iterate through the network dynamics.
Expand All @@ -20,8 +17,6 @@
#' @param tol A numeric. The tolerance factor for early stopping.
#' @param sparse Logical. Whether or not the function should be run using sparse matrices. must match the actual matrix, this could prob be automated
#' @param two_node_solution Logical. The newton-raphson algo is used to find the correct angle
#' @param include_edges Logical. Whether the function will return the edge strain and tension data or just elevation and dynamics.
#' Currently default is set to FALSE.
#' @param sample Integer. The dynamics will be stored only if the iteration number is a multiple of the sample.
#' This can greatly reduce the size of the results file for large numbers of iterations. Must be a multiple of the max_iter
#'
Expand All @@ -30,31 +25,27 @@
#' @seealso \code{\link{Create_stabilised_blocks}} \code{\link{create_balanced_blocks}}
#' @export

SETSe <- function(g,
force ="net_generation",
flow = "power_flow",
distance = "distance",
capacity = "capacity",
edge_name = "edge_name",
k ="k",
tstep = 0.02,
mass = 1,
max_iter = 20000,
coef_drag = 1,
tol = 1e-6,
sparse = FALSE,
two_node_solution = TRUE,
include_edges = FALSE,
sample = 1){

SETSe2 <- function(g,
force ="force",
distance = "distance",
edge_name = "edge_name",
k ="k",
tstep = 0.02,
mass = 1,
max_iter = 20000,
coef_drag = 1,
tol = 1e-6,
sparse = FALSE,
two_node_solution = TRUE,
sample = 1){

#helper function that prepares the data
Prep <- SETSe_data_prep(g = g,
force = force,
flow = flow,
distance = distance,
mass = mass,
edge_name = edge_name,
sparse = sparse)
force = force,
distance = distance,
mass = mass,
edge_name = edge_name,
sparse = sparse)

#do special case solution I should change this to a standalone function for ease of reading but it isn't important
if(nrow(Prep$Link)==1 & two_node_solution){
Expand Down Expand Up @@ -93,7 +84,7 @@ SETSe <- function(g,
)
#Solves using the iterative method.
} else{

#The core algorithm
Out <- SETSe_core(
node_embeddings = Prep$node_embeddings,
Expand All @@ -111,18 +102,13 @@ SETSe <- function(g,

}

if(include_edges){

#Extract edge tension and strain from the network
Out$edge_embeddings <- calc_tension_strain(g = g,
Out$node_embeddings,
distance = distance,
capacity = capacity,
flow = flow,
edge_name = edge_name,
k = k)

}

#Extract edge tension and strain from the network
Out$edge_embeddings <- calc_tension_strain(g = g,
Out$node_embeddings,
distance = distance,
edge_name = edge_name,
k = k)


return(Out)
Expand Down
22 changes: 6 additions & 16 deletions R/SETSe_bicomp.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@
#'
#' @param g An igraph object
#' @param force A character string. This is the node attribute that contains the force the nodes exert on the network.
#' @param flow A character string. This is the edge attribute that is the power flow on the edges.
#' @param distance A character string. The edge attribute that contains the original/horizontal distance between nodes.
#' @param capacity A character string. This is the edge attribute that is the flow limit of the edges.
#' @param edge_name A character string. This is the edge attribute that contains the edge_name of the edges.
#' @param k A character string. This is k for the moment don't change it.
#' @param tstep A numeric. The time interval used to iterate through the network dynamics.
Expand All @@ -19,12 +17,10 @@
#' @param sparse Logical. Whether sparse matrices will be used. This becomes valubale for larger networks
#'
#' @return A list containing 3 dataframes, the dataframe of the node embeddings, edge embeddings, and network dynamics
#'@export
#' @export
SETSe_bicomp <- function(g,
force = "force",
flow = "flow",
distance = "distance",
capacity = "capacity",
force = "force",
distance = "distance",
edge_name = "edge_name",
k = "k",
tstep,
Expand All @@ -37,8 +33,7 @@ SETSe_bicomp <- function(g,

#seperate out the network into blocks
List_of_BiConComps <- create_balanced_blocks(g,
force = force,
flow = flow)
force = force)

#find the largest component and use that as the origin block
OriginBlock_number <-List_of_BiConComps %>% map_dbl(~vcount(.x)) %>% which.max()
Expand All @@ -49,11 +44,9 @@ SETSe_bicomp <- function(g,
total_force <- sum(abs(get.vertex.attribute(g, force)))

#use the largest block to set the simulation parameters k and m.
#k needs to be sufficiently stretched to allow enough topology variation. otherwise all that happens is a
#surface angled in the direct of net power flow. Which is interesting but not that interesting
#k needs to be sufficiently stretched to allow enough topology variation.
OriginBlock <- SETSe(g = List_of_BiConComps[[OriginBlock_number]],
force =force,
flow = flow,
distance = distance,
edge_name = edge_name,
tstep = tstep,
Expand All @@ -71,7 +64,6 @@ SETSe_bicomp <- function(g,
OriginBlock = OriginBlock,
OriginBlock_number = OriginBlock_number,
force = force,
flow = flow,
distance = distance,
edge_name = edge_name,
tstep = tstep,
Expand All @@ -87,9 +79,7 @@ SETSe_bicomp <- function(g,
#Extract edge tension and strain from the network
tension_strain_embeddings <- calc_tension_strain(g = g,
height_embeddings$node_embeddings,
distance = distance,
capacity = capacity,
flow = flow,
distance = distance,
edge_name = edge_name,
k = k)

Expand Down
8 changes: 1 addition & 7 deletions R/SETSe_bicomp_expanded.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@
#'
#' @param g An igraph object
#' @param force A character string. This is the node attribute that contains the force the nodes exert on the network.
#' @param flow A character string. This is the edge attribute that is the power flow on the edges.
#' @param distance A character string. The edge attribute that contains the original/horizontal distance between nodes.
#' @param capacity A character string. This is the edge attribute that is the flow limit of the edges.
#' @param edge_name A character string. This is the edge attribute that contains the edge_name of the edges.
#' @param k A character string. This is k for the moment don't change it.
#' @param tstep A numeric. The time interval used to iterate through the network dynamics.
Expand All @@ -21,7 +19,6 @@
#'@export
SETSe_bicomp_expanded <- function(g,
force = "force",
flow = "flow",
distance = "distance",
capacity = "capacity",
edge_name = "edge_name",
Expand All @@ -35,8 +32,7 @@ SETSe_bicomp_expanded <- function(g,

#seperate out the network into blocks
List_of_BiConComps <- create_balanced_blocks(g,
force = force,
flow = flow)
force = force)

#find the largest component and use that as the origin block
OriginBlock_number <-List_of_BiConComps %>% map_dbl(~vcount(.x)) %>% which.max()
Expand All @@ -48,7 +44,6 @@ SETSe_bicomp_expanded <- function(g,
#surface angled in the direct of net power flow. Which is interesting but not that interesting
OriginBlock <- SETSe_expanded(g = List_of_BiConComps[[OriginBlock_number]],
force =force,
flow = flow,
distance = distance,
edge_name = edge_name,
tstep = tstep,
Expand All @@ -65,7 +60,6 @@ SETSe_bicomp_expanded <- function(g,
OriginBlock = OriginBlock,
OriginBlock_number = OriginBlock_number,
force = force,
flow = flow,
distance = distance,
edge_name = edge_name,
tstep = tstep,
Expand Down
Loading

0 comments on commit 9b925ab

Please sign in to comment.