chipiron.utils package

Subpackages

Submodules

chipiron.utils.chi_nn module

Module for the ChiNN class

class chipiron.utils.chi_nn.ChiNN[source]

Bases: Module

The Generic Neural network class of chipiron

init_weights() None[source]

Initialize the weights of the model.

load_weights_from_file(path_to_param_file: str | PathLike[str]) None[source]

Loads the neural network weights from a file or initializes them if the file doesn’t exist.

Parameters:
  • path_to_param_file (str) – The path to the parameter file.

  • authorisation_to_create_file (bool) – Flag indicating whether the program has authorization to create a new file.

Returns:

None

log_readable_model_weights_to_file(file_path: str) None[source]

chipiron.utils.comparable module

This module defines the Comparable class, which is an abstract base class for objects that can be compared.

Classes that inherit from Comparable must implement the __lt__ method, which defines the less-than comparison operation.

class chipiron.utils.comparable.Comparable[source]

Bases: object

An abstract base class for objects that can be compared.

Subclasses of Comparable must implement the __lt__ method to define the less-than comparison.

None
__lt__(self, other

Any) -> bool: Abstract method that compares the object with another object and returns True if the object is less than the other object, False otherwise.

chipiron.utils.dataclass module

Module to check if an object is a dataclass

class chipiron.utils.dataclass.DataClass(*args, **kwargs)[source]

Bases: Protocol

Protocol to represent a dataclass.

This protocol is used to check if an object is a dataclass by checking for the presence of the __dataclass_fields__ attribute.

class chipiron.utils.dataclass.IsDataclass(*args, **kwargs)[source]

Bases: Protocol

Protocol to represent a dataclass.

This protocol is used to check if an object is a dataclass by checking for the presence of the __dataclass_fields__ attribute.

chipiron.utils.dataclass.custom_asdict_factory(data: Iterable[Any]) dict[Any, Any][source]

chipiron.utils.dict_of_numbered_dict_with_pointer_on_max module

Module for DictOfNumberedDictWithPointerOnMax class.

class chipiron.utils.dict_of_numbered_dict_with_pointer_on_max.DictOfNumberedDictWithPointerOnMax[source]

Bases: Generic

A dictionary-like data structure that stores numbered dictionaries and keeps track of the maximum half move.

half_moves

A dictionary that stores numbered dictionaries.

Type:

dict[int, dict[T_Key, T_Value]]

max_half_move

The maximum half move value.

Type:

int | None

__setitem__(self, node

T_Key, value: T_Value) -> None: Adds an item to the data structure.

__getitem__(self, node

T_Key) -> T_Value: Retrieves an item from the data structure.

__bool__(self) bool[source]

Checks if the data structure is non-empty.

__contains__(self, node

T_Key) -> bool: Checks if an item is present in the data structure.

popitem(self) tuple[T_Key, T_Value][source]

Removes and returns the item with the maximum half move value.

popitem() tuple[T_Key, T_Value][source]

Removes and returns the item with the maximum half move value.

Returns:

The key-value pair of the removed item.

Return type:

tuple[T_Key, T_Value]

Raises:

AssertionError – If the data structure is empty.

class chipiron.utils.dict_of_numbered_dict_with_pointer_on_max.HasHalfMove(*args, **kwargs)[source]

Bases: Protocol

property half_move: int

Get the half move count of the node.

Returns:

The half move count of the node.

chipiron.utils.logger module

chipiron.utils.logger.suppress_logging(logger: Logger, level: int = 30) Generator[None, None, None][source]

chipiron.utils.my_random module

random for chipiron

chipiron.utils.my_random.set_seeds(seed: int = 0) None[source]

Set all the base seeds.

Parameters:

seed (int) – the seed

chipiron.utils.my_value_sorted_dict module

Module for sorting a dictionary by ascending order

chipiron.utils.my_value_sorted_dict.sort_dic(dic: dict[Any, CT]) dict[Any, CT][source]

Sorts a dictionary by ascending order of values.

Parameters:

dic (dict[Any, CT]) – The dictionary to be sorted.

Returns:

The sorted dictionary.

Return type:

dict[Any, CT]

chipiron.utils.null_object module

Module that contains the NullObject class.

class chipiron.utils.null_object.NullObject(*args: Any, **kwargs: Any)[source]

Bases: object

The NullObject class is a null object implementation in Python. Null objects are objects that always
and reliably “do nothing.” They are often used as placeholders or default values when an actual object

is not available or needed.

The NullObject class provides the following methods: - __init__: Initializes the NullObject instance. - __call__: Allows the NullObject instance to be called as a function. - __repr__: Returns a string representation of the NullObject instance. - __nonzero__: Returns 0 to indicate that the NullObject instance is considered False. - __getattr__: Handles attribute access on the NullObject instance. - __setattr__: Handles attribute assignment on the NullObject instance. - __delattr__: Handles attribute deletion on the NullObject instance.

chipiron.utils.path_variables module

chipiron.utils.small_tools module

This module contains utility functions and classes for small tools.

class chipiron.utils.small_tools.Interval(min_value: float | None = None, max_value: float | None = None)[source]

Bases: object

Represents an interval with a minimum and maximum value.

max_value: float | None = None
min_value: float | None = None
chipiron.utils.small_tools.dict_alphabetic_str(dic: dict[Any, Any]) str[source]

Convert a dictionary to a string with keys sorted alphabetically.

Parameters:

dic – The dictionary to convert.

Returns:

A string representation of the dictionary with keys sorted alphabetically.

chipiron.utils.small_tools.distance_number_to_interval(value: float, interval: Interval) float[source]

Calculate the distance between a number and an interval.

Parameters:
  • value – The number.

  • interval – The interval.

Returns:

The distance between the number and the interval.

Raises:

AssertionError – If the interval has missing values.

chipiron.utils.small_tools.get_package_root_path(package_name: str) str[source]
chipiron.utils.small_tools.intersect_intervals(interval_1: Interval, interval_2: Interval) Interval | None[source]

Find the intersection of two intervals.

Parameters:
  • interval_1 – The first interval.

  • interval_2 – The second interval.

Returns:

The intersection of the two intervals, or None if there is no intersection.

Raises:

AssertionError – If any of the intervals have missing values.

chipiron.utils.small_tools.mkdir_if_not_existing(folder_path: str | PathLike[str]) None[source]

Create a directory at the specified path.

Parameters:

folder_path – The path to the directory.

Raises:
  • FileNotFoundError – If the parent directory does not exist.

  • FileExistsError – If the directory already exists.

chipiron.utils.small_tools.nth_key(dct: dict[_T, Any], n: int) _T[source]

Get the nth key from a dictionary.

Parameters:
  • dct – The dictionary.

  • n – The index of the key to retrieve.

Returns:

The nth key from the dictionary.

chipiron.utils.small_tools.rec_merge_dic(a: dict[Any, Any], b: dict[Any, Any]) dict[Any, Any][source]

Recursively merge two dictionaries.

Parameters:
  • a – The first dictionary.

  • b – The second dictionary.

Returns:

The merged dictionary.

chipiron.utils.small_tools.resolve_package_path(path_to_file: str | Path) str[source]

Replace ‘package://’ at the start of the path with the chipiron package root.

Parameters:

path_to_file (str or Path) – Input path, possibly starting with ‘package://’.

Returns:

Resolved absolute path.

Return type:

str

chipiron.utils.small_tools.softmax(x: list[float], temperature: float) ndarray[Any, dtype[float64]][source]

Compute softmax values for each set of scores in x.

Parameters:
  • x – The list of scores.

  • temperature – The temperature parameter.

Returns:

The softmax values.

chipiron.utils.small_tools.unique_int_from_list(a_list: list[int | None]) int | None[source]

Generate a unique integer from a list of two integers.

Parameters:

a_list – A list of two integers.

Returns:

The unique integer generated using the Cantor pairing function.

Raises:

AssertionError – If the list does not contain exactly two elements.

chipiron.utils.small_tools.yaml_fetch_args_in_file(path_file: str | PathLike[str]) dict[Any, Any][source]

Fetch arguments from a YAML file.

Parameters:

path_file – The path to the YAML file.

Returns:

A dictionary containing the arguments.

Module contents

Module that contains small tools that are used in the project.

chipiron.utils.dict_alphabetic_str(dic: dict[Any, Any]) str[source]

Convert a dictionary to a string with keys sorted alphabetically.

Parameters:

dic – The dictionary to convert.

Returns:

A string representation of the dictionary with keys sorted alphabetically.

chipiron.utils.mkdir_if_not_existing(folder_path: str | PathLike[str]) None[source]

Create a directory at the specified path.

Parameters:

folder_path – The path to the directory.

Raises:
  • FileNotFoundError – If the parent directory does not exist.

  • FileExistsError – If the directory already exists.

chipiron.utils.rec_merge_dic(a: dict[Any, Any], b: dict[Any, Any]) dict[Any, Any][source]

Recursively merge two dictionaries.

Parameters:
  • a – The first dictionary.

  • b – The second dictionary.

Returns:

The merged dictionary.

chipiron.utils.unique_int_from_list(a_list: list[int | None]) int | None[source]

Generate a unique integer from a list of two integers.

Parameters:

a_list – A list of two integers.

Returns:

The unique integer generated using the Cantor pairing function.

Raises:

AssertionError – If the list does not contain exactly two elements.

chipiron.utils.yaml_fetch_args_in_file(path_file: str | PathLike[str]) dict[Any, Any][source]

Fetch arguments from a YAML file.

Parameters:

path_file – The path to the YAML file.

Returns:

A dictionary containing the arguments.