Centrality: private API

PortfolioOptimisers.assert_no_weight_channel_args — Function
assert_no_weight_channel_args(::Type{T}, args::Tuple, S::Type, shape::AbstractString,
                              channel::AbstractString) where {T}

Refuse an entry of args that reaches a second weight channel.

Both guards of a splatted args make this refusal. An entry point of Graphs.jl takes its weights in a positional slot. An entry of args with that type is then a second weight channel, and the declared channel already gives the weights. assert_centrality_args and assert_tree_args differ only in the type that reaches a channel, and in the declared channel that they name. Both call this function with their own S, shape and channel.

The message gives the index and the type of the entry. The library splats args, so the caller sees no argument names.

Algorithm

  1. Find idx, the index of the first entry of args that is an S.
  2. Throw a ConflictingArgumentError when idx is not nothing. The message holds T, shape, channel, idx and the type of the entry.

Arguments

  • T: Algorithm type, which the error message names.
  • args: Positional arguments for the function of Graphs.jl.
  • S: The type of an entry that reaches a weight slot, such as AbstractMatrix.
  • shape: Name of that type, for the error message.
  • channel: Sentence that names the declared channel that the caller must use.

Validation

Returns

  • nothing.

Related

source
PortfolioOptimisers.assert_centrality_args — Function
assert_centrality_args(::Type{T}, args::Tuple) where {T}

Refuse a matrix inside a centrality algorithm's args.

calc_centrality splats args into the centrality function of Graphs.jl. A matrix in args is then a distmx, a second way to weight the graph that no polarity declares. centrality_polarity is the declared way. It selects the weights that the mathematics of the algorithm needs, from the structure that the estimator built. The second channel is not safe:

  • distmx is the third positional argument of Graphs.betweenness_centrality. A matrix in args binds to vs, and the call overflows the stack inside Graphs.degree. A caller can catch the StackOverflowError, and the process continues. Only the call fails.
  • distmx is the second positional argument of Graphs.closeness_centrality. A matrix in args ran, and replaced the polarity with no error. A matrix of the wrong size raised a BoundsError, and not a DimensionMismatch.
  • Graphs.stress_centrality has no distmx.

The guard accepts every other entry. A list of vertices or a sample count is a real positional argument of those functions, and it does not set weights.

kwargs needs no second guard. A keyword binds by name, so a matrix in kwargs cannot reach a positional slot. None of the four functions declares a keyword whose value is a matrix. Each of normalize, endpoints, rng and seed refuses a matrix. A matrix in kwargs always raises a MethodError or a TypeError.

Algorithm

  1. Refuse an AbstractMatrix in args with assert_no_weight_channel_args, which names centrality_polarity as the declared weight channel.

Arguments

  • T: Centrality algorithm type, which the error message names.
  • args: Positional arguments for the centrality function of Graphs.jl.

Validation

Returns

  • nothing.

Related

source
PortfolioOptimisers.assert_tree_args — Function
assert_tree_args(::Type{T}, args::Tuple, kwargs::NamedTuple) where {T}

Refuse a second weight channel in the args and the kwargs of a spanning tree algorithm.

calc_mst splats both fields into the spanning tree function of Graphs.jl. Every weight channel that they can reach re-weights or re-orients the tree that calc_weighted_adjacency_graph builds. The graph that it gives to calc_mst carries the distances from the de and the ce of the estimator, and the default distmx of Graphs.jl is those weights. A caller who fills these fields replaces weights that are already correct, and no error shows it:

  • kruskal_mst, boruvka_mst and prim_mst all take distmx as their second positional argument. A matrix in args replaces the distances of the estimator. Such a matrix often has the correct size, and the call then returns a valid tree of the wrong graph.
  • kruskal_mst also takes a weight_vector in that slot, which replaces the weights in the same way.
  • minimize in kwargs inverts the direction of the search. A minimised distance defines the tree branch, as calc_weighted_adjacency_graph and SimilarityPolarity state. minimize = false gives a maximum spanning tree, and every consumer reads it as a minimum spanning tree.

The guard accepts every entry that is not a matrix or a vector, and every other keyword. Those reach no weight channel. The three functions declare no other weight channel, so a wrong entry raises an error at the call.

Algorithm

  1. Refuse an AbstractMatrix or an AbstractVector in args with assert_no_weight_channel_args. The message names the graph that calc_weighted_adjacency_graph built as the declared weight channel.
  2. Throw a ConflictingArgumentError when kwargs holds the key minimize. The message holds T and the value of that key.

Arguments

  • T: Spanning tree algorithm type, which the error message names.
  • args: Positional arguments for the spanning tree function of Graphs.jl.
  • kwargs: Keyword arguments for the same function.

Validation

Returns

  • nothing.

Related

source