qgym.utils.input_validation module

This module contains generic input validation methods.

qgym.utils.input_validation.check_adjacency_matrix(adjacency_matrix)[source]

Check if a matrix is an adjacency matrix, i.e., a square matrix.

Parameters:

adjacency_matrix (ArrayLike) – Matrix to check.

Raises:

ValueError – When the provided input is not a square matrix.

Return type:

ndarray[Any, dtype[Any]]

Returns:

Square NDArray representation of adjacency_matrix.

qgym.utils.input_validation.check_bool(x, name, *, safe=False)[source]

Check if the variable x with name ‘name’ is a Boolean value.

Optionally, cast to Boolean value if it is not.

Parameters:
  • x (Any) – Variable to check.

  • name (str) – Name of the variable. This name will be displayed in possible error messages.

  • safe (bool) – If True raise a TypeError when x is not a bool. If False cast to bool.

Raises:

TypeError – If safe is True and x is not a Boolean value.

Return type:

bool

Returns:

Boolean representation of the input.

qgym.utils.input_validation.check_graph_is_valid_topology(graph, name)[source]

Check if graph with name ‘name’ is an instance of networkx.Graph, check if the graph is valid topology graph and check if the nodes are integers.

Parameters:
  • graph (Graph) – Graph to check.

  • name (str) – Name of the graph. This name will be displayed in possible error messages.

Raises:
  • TypeError – If graph is not an instance of networkx.Graph.

  • ValueError – If graph is not a valid topology graph.

Return type:

Graph

Returns:

The input graph.

qgym.utils.input_validation.check_instance(x, name, dtype)[source]

Check if x with name ‘name’ is an instance of dtype.

Parameters:
  • x (Any) – Variable to check.

  • name (str) – Name of the variable. This name will be displayed in possible error messages.

Raises:

TypeError – If x is not an instance of dtype.

Return type:

None

qgym.utils.input_validation.check_int(x, name, *, l_bound=None, u_bound=None, l_inclusive=True, u_inclusive=True)[source]

Check if the variable x with name ‘name’ is a real number.

Optionally, lower and upper bounds can also be checked.

Parameters:
  • x (Any) – Variable to check.

  • name (str) – Name of the variable. This name will be displayed in possible error messages.

  • l_bound (float | None) – Lower bound of x.

  • u_bound (float | None) – Upper bound of x.

  • l_inclusive (bool) – If True the lower bound is inclusive, otherwise the lower bound is exclusive.

  • u_inclusive (bool) – If True the upper bound is inclusive, otherwise the upper bound is exclusive.

Raises:
Return type:

int

Returns:

Floating point representation of x.

qgym.utils.input_validation.check_real(x, name, *, l_bound=None, u_bound=None, l_inclusive=True, u_inclusive=True)[source]

Check if the variable x with name ‘name’ is a real number.

Optionally, lower and upper bounds can also be checked.

Parameters:
  • x (Any) – Variable to check.

  • name (str) – Name of the variable. This name will be displayed in possible error messages.

  • l_bound (float | None) – Lower bound of x.

  • u_bound (float | None) – Upper bound of x.

  • l_inclusive (bool) – If True the lower bound is inclusive, otherwise the lower bound is exclusive.

  • u_inclusive (bool) – If True the upper bound is inclusive, otherwise the upper bound is exclusive.

Raises:
Return type:

float

Returns:

Floating point representation of x.

qgym.utils.input_validation.check_string(x, name, *, lower=False, upper=False)[source]

Check if the variable x with name ‘name’ is a string.

Optionally, the string can be converted to all lowercase or all uppercase letters.

Parameters:
  • x (str) – Variable to check.

  • name (str) – Name of the variable. This name will be displayed in possible error messages.

  • lower (bool) – If True, x will be returned with lowercase letters. Defaults to False.

  • upper (bool) – If True, x will be returned with uppercase letters. Default to False.

Raises:

TypeError – If x is not an instance of str.

Return type:

str

Returns:

Input string. Optionally, in lowercase or uppercase letters.

qgym.utils.input_validation.warn_if_negative(x, name)[source]

Give a warning when x is negative.

Parameters:
  • x (float) – Variable to check.

  • name (str) – Name of the variable. This name will be displayed in the warning.

Return type:

None

qgym.utils.input_validation.warn_if_positive(x, name)[source]

Give a warning when x is positive.

Parameters:
  • x (float) – Variable to check.

  • name (str) – Name of the variable. This name will be displayed in the warning.

Return type:

None