Input Checks

To unify to a certain extent error messages there are two ways.

Check the input before actual computations

For checking input type and values for standard data types, we collected in pyrigi/_utils/_input_check.py a set of methods that are often needed, see the list below. For instance, pyrigi._utils._input_check.dimension() checks whether the parameter dim is a positive integer and raises an error otherwise.

Checks related to the class Graph are in graph._utils._input_check. For instance, graph._utils._input_check.no_loop() checks whether a graph is loop free and raises an error otherwise.

Example:

import pyrigi._utils._input_check as _input_check
import pyrigi.graph._utils._input_check as _graph_input_check

class Graph(nx.Graph):
    ...

    def method(self, dim: int):
        _input_check.dimension(dim)
        _graph_input_check.no_loop(self)
        ...

Note that these input checks are in a private module. However, we do test them.

Check the input at the end by exclusion

In some cases a method would run a conditional statement through all possible options of an input parameter. If the method reaches the end an error would be raised to tell the user that the option is not supported. For instance a parameter algorithm might have several supported values.

Example:

from pyrigi.exception import NotSupportedValueError

def method(self, algorithm: str = "default"):
    if algorithm == "alg1":
        ...
    elif algorithm == "alg2":
        ...
    else:
        raise NotSupportedValueError(algorithm, "algorithm", self.method)

Input check functions of Graph

The following functions can be used for input checks of Graph.

pyrigi.graph._utils._input_check.edge_format(graph, edge, loopfree=False)[source]

Check if an edge is a pair of (distinct) vertices of the graph and raise an error otherwise.

Parameters:
  • edge (Edge) – Edge for which the containment in the given graph is checked.

  • loopfree (bool) – If True, an error is raised if edge is a loop.

  • graph (Graph)

Return type:

None

pyrigi.graph._utils._input_check.edge_format_list(graph, edges)[source]

Apply edge_format() to all edges in a list.

Parameters:
Return type:

None

pyrigi.graph._utils._input_check.is_edge(graph, edge, vertices=None)[source]

Check if the given input is an edge of the graph with endvertices in vertices and raise an error otherwise.

Parameters:
  • edge (Edge) – an edge to be checked

  • vertices (Sequence[Vertex]) – Check if the endvertices of the edge are contained in the list vertices. If None, the function considers all vertices of the graph.

  • graph (Graph)

Return type:

None

pyrigi.graph._utils._input_check.is_edge_list(graph, edges, vertices=None)[source]

Apply is_edge() to all edges in a list.

Parameters:
  • edges (Sequence[Edge]) – A list of edges to be checked.

  • vertices (Sequence[Vertex]) – Check if the endvertices of the edges are contained in the list vertices. If None (default), the function considers all vertices of the graph.

  • graph (Graph)

Return type:

None

pyrigi.graph._utils._input_check.is_edge_order(graph, edge_order, name='')[source]

Check whether the provided edge_order contains the same elements as the graph edge set and raise an error otherwise.

The edge_order is also returned.

Parameters:
  • edge_order (Sequence[Edge]) – List of edges in the preferred order. If None, then all edges are returned using edge_list().

  • graph (Graph)

  • name (str)

Return type:

list[Edge]

pyrigi.graph._utils._input_check.is_vertex_order(graph, vertex_order, name='')[source]

Check whether the provided vertex_order contains the same elements as the graph vertex set and raise an error otherwise.

The vertex_order is also returned.

Parameters:
  • vertex_order (Sequence[Vertex]) – List of vertices in the preferred order. If None, then all vertices are returned using vertex_list().

  • graph (Graph)

  • name (str)

Return type:

list[Vertex]

pyrigi.graph._utils._input_check.no_loop(graph)[source]

Check whether the graph has loops and raise an error in case.

Return type:

None

Parameters:

graph (Graph)

pyrigi.graph._utils._input_check.vertex_members(graph, to_check, name='')[source]

Check whether the elements of a list are indeed vertices of the graph and raise error otherwise.

Parameters:
  • to_check (Union[Iterable[Vertex], Vertex]) – A vertex or Iterable of vertices for which the containment in the graph is checked.

  • name (str) – A name of the Iterable to_check can be picked.

  • graph (Graph)

Return type:

None

Input check functions of Framework

See the methods of FrameworkBase starting with _input_check.

General input check functions

Module for standard input checks.

pyrigi._utils._input_check.dimension(dim)[source]

Check whether an input dimension is a positive integer and raise an error otherwise.

Return type:

None

Parameters:

dim (int)

pyrigi._utils._input_check.dimension_for_algorithm(dim, possible, algorithm='')[source]

Check whether an input dimension is a member of list and raise an error otherwise.

Parameters:
  • dim (int) – Dimension to be checked

  • possible (list) – Values that are allowed

  • algorithm (str) – Name of the algorithm for the error message

Return type:

None

pyrigi._utils._input_check.equal(val1, val2, name1, name2='')[source]

Check whether an input parameter val1 is equal to val2 and raise an error otherwise.

Parameters:
  • val1 (int | float) – Values that shall be equal

  • val2 (int | float) – Values that shall be equal

  • name1 (str) – Name of the parameter val1

  • name2 (str) – Name of the parameter val2

Return type:

None

pyrigi._utils._input_check.greater(val1, val2, name1, name2='')[source]

Check whether an input parameter val1 is greater than or equal to val2 and raise an error otherwise.

Parameters:
  • val1 (int | float) – Value that shall be greater

  • val2 (int | float) – Value that shall be smaller/equal

  • name1 (str) – Name of the parameter val1

  • name2 (str) – Name of the parameter val2

Return type:

None

pyrigi._utils._input_check.greater_equal(val1, val2, name1, name2='')[source]

Check whether an input parameter val1 is greater than or equal to val2 and raise an error otherwise.

Parameters:
  • val1 (int | float) – Value that shall be greater/equal

  • val2 (int | float) – Value that shall be smaller

  • name1 (str) – Name of the parameter val1

  • name2 (str) – Name of the parameter val2

Return type:

None

pyrigi._utils._input_check.integrality_and_range(value, name, min_val=0, max_val=inf)[source]

Check whether an input parameter value is an integer in a certain range and raise an error otherwise.

Parameters:
  • value (int) – Value to be checked

  • name (str) – Name of the parameter

  • min_val (int) – Lower limit for the value

  • max_val (int) – Upper limit for the value

Return type:

None

pyrigi._utils._input_check.pebble_values(K, L)[source]

Check if K and L satisfy the pebble conditions K > 0 and 0 <= L < 2K and raise an error otherwise.

Return type:

None

Parameters:
pyrigi._utils._input_check.smaller_equal(val1, val2, name1, name2='')[source]

Check whether an input parameter val1 is smaller than or equal to val2 and raise an error otherwise.

Parameters:
  • val1 (int | float) – Value that shall be smaller/equal

  • val2 (int | float) – Value that shall be greater

  • name1 (str) – Name of the parameter val1

  • name2 (str) – Name of the parameter val2

Return type:

None