qumphy.misc.misc module

File: qumphy/misc.py Project: 22HLT01 QUMPHY Contact: nando.hegemann@ptb.de Gitlab: https://gitlab.com/qumphy Description: Miscellaneous functions.

qumphy.misc.misc.batch(iterable, n=1)[source]

Split iterable into different batches of batchsize n.

Return type:

Iterator

Parameters:
  • iterable (array_like) – Iterable to split.

  • n (int, default=1) – Batch size.

Yields:

Iterable for different batches.

qumphy.misc.misc.eval_argument_parser()[source]
qumphy.misc.misc.eval_torch_model_by_numpy_ndarray(model, data)[source]

Evaluate a torch model (nn.Module) with a numpy ndarray.

Return type:

ndarray

Parameters:
  • model (nn.Module) – Torch model.

  • data (np.ndarray) – Input data.

Returns:

Model output predictions.

Return type:

np.ndarray

qumphy.misc.misc.instantiate_class(config)[source]

Instantiate a class from a dictionary config.

The config dictionary should have a “class_path” key with the path to the class (e.g., ‘my_module.MyClass’). It should also have an “init_args” key with a dictionary of arguments to pass to the class constructor.

If the config dictionary has a “classes” key, the function will recursively instantiate the classes specified in the list and pass them as arguments to the class constructor. The “classes” key should be a list of dictionaries, each with a “class_path” key and a “keyword” key specifying the keyword argument to pass the class instance to. If the “class_list” key is present, a list of classes will be instantiated and passed to the same keyword argument.

Example: >>> config = { … “class_path”: “my_module.Class1”, … “init_args”: {“first_arg”: 1, “second_arg”: 2}, … “classes”: [ … {“keyword”: “third_arg”, “class_path”: “my_module.Class2”, “init_args”: {“x”: 3}}, … {“keyword”: “fourth_arg”, “class_list”: [ … {“class_path”: “my_module.Class3”, “init_args”: {“y”: 4}}, … {“class_path”: “my_module.Class4”, “init_args”: {“z”: 5}} … ] … ] … }

Return type:

object

Parameters:

config (dict) – A dictionary with the class path and arguments to pass to the class constructor.

Returns:

An instance of the specified class.

Return type:

object

qumphy.misc.misc.instantiate_class_from_string(class_path, *init_args, **init_kwargs)[source]

Instantiate a class from a given string path.

Parameters:
  • class_path (str) – The full path to the class (e.g., ‘my_module.MyClass’).

  • *init_args – Arguments to pass to the class constructor.

  • **init_kwargs – Keyword arguments to pass to the class constructor.

Returns:

An instance of the specified class.

Return type:

object

qumphy.misc.misc.parse_value(value)[source]

Try parsing a string as a boolean, integer, or float. If parsing fails, return the original string.

Parameters:

value (str) – The string to parse.

Returns:

The parsed value, or the original string if parsing fails.

Return type:

object

qumphy.misc.misc.set_value_at_nested_key(d, path, value)[source]
qumphy.misc.misc.str2dict(text)[source]

Convert a string of the format “a.b.c:value” or “a.b.c=value” into a nested dictionary.

The value part of the string is parsed as a boolean, integer, float, or string.

Parameters:

text (str) – The string to convert.

Returns:

A nested dictionary where the keys are the parts of the string separated by ‘.’, and the value is separated by ‘:’.

Return type:

dict

Examples

>>> str2dict("a.b.c:1")
{'a': {'b': {'c': '1'}}}
>>> str2dict("x.y.z:foo")
{'x': {'y': {'z': 'foo'}}}
qumphy.misc.misc.train_argument_parser()[source]
qumphy.misc.misc.update_dictionary(d1, d2)[source]