pixyz.utils

pixyz.utils.set_epsilon(eps)[source]

Set a epsilon parameter.

Parameters:eps (int or float) –

Examples

>>> from unittest import mock
>>> with mock.patch('pixyz.utils._EPSILON', 1e-07):
...     set_epsilon(1e-06)
...     epsilon()
1e-06
pixyz.utils.epsilon()[source]

Get a epsilon parameter.

Returns:
Return type:int or float

Examples

>>> from unittest import mock
>>> with mock.patch('pixyz.utils._EPSILON', 1e-07):
...     epsilon()
1e-07
pixyz.utils.get_dict_values(dicts, keys, return_dict=False)[source]

Get values from dicts specified by keys.

When return_dict is True, return values are in dictionary format.

Parameters:
  • dicts (dict) –
  • keys (list) –
  • return_dict (bool) –
Returns:

Return type:

dict or list

Examples

>>> get_dict_values({"a":1,"b":2,"c":3}, ["b"])
[2]
>>> get_dict_values({"a":1,"b":2,"c":3}, ["b", "d"], True)
{'b': 2}
pixyz.utils.delete_dict_values(dicts, keys)[source]

Delete values from dicts specified by keys.

Parameters:
  • dicts (dict) –
  • keys (list) –
Returns:

new_dicts

Return type:

dict

Examples

>>> delete_dict_values({"a":1,"b":2,"c":3}, ["b","d"])
{'a': 1, 'c': 3}
pixyz.utils.detach_dict(dicts)[source]

Detach all values in dicts.

Parameters:dicts (dict) –
Returns:
Return type:dict
pixyz.utils.replace_dict_keys(dicts, replace_list_dict)[source]

Replace values in dicts according to replace_list_dict.

Parameters:
  • dicts (dict) – Dictionary.
  • replace_list_dict (dict) – Dictionary.
Returns:

replaced_dicts – Dictionary.

Return type:

dict

Examples

>>> replace_dict_keys({"a":1,"b":2,"c":3}, {"a":"x","b":"y"})
{'x': 1, 'y': 2, 'c': 3}
>>> replace_dict_keys({"a":1,"b":2,"c":3}, {"a":"x","e":"y"})  # keys of `replace_list_dict`
{'x': 1, 'b': 2, 'c': 3}
pixyz.utils.replace_dict_keys_split(dicts, replace_list_dict)[source]

Replace values in dicts according to replace_list_dict.

Replaced dict is splitted by replaced_dict and remain_dict.

Parameters:
  • dicts (dict) – Dictionary.
  • replace_list_dict (dict) – Dictionary.
Returns:

  • replaced_dict (dict) – Dictionary.
  • remain_dict (dict) – Dictionary.

Examples

>>> replace_list_dict = {'a': 'loc'}
>>> x_dict = {'a': 0, 'b': 1}
>>> print(replace_dict_keys_split(x_dict, replace_list_dict))
({'loc': 0}, {'b': 1})
pixyz.utils.tolist(a)[source]

Convert a given input to the dictionary format.

Parameters:a (list or other) –
Returns:
Return type:list

Examples

>>> tolist(2)
[2]
>>> tolist([1, 2])
[1, 2]
>>> tolist([])
[]
pixyz.utils.sum_samples(samples)[source]

Sum a given sample across the axes.

Parameters:samples (torch.Tensor) – Input sample. The number of this axes is assumed to be 4 or less.
Returns:Sum over all axes except the first axis.
Return type:torch.Tensor

Examples

>>> a = torch.ones([2])
>>> sum_samples(a).size()
torch.Size([2])
>>> a = torch.ones([2, 3])
>>> sum_samples(a).size()
torch.Size([2])
>>> a = torch.ones([2, 3, 4])
>>> sum_samples(a).size()
torch.Size([2])
pixyz.utils.print_latex(obj)[source]

Print formulas in latex format.

Parameters:obj (pixyz.distributions.distributions.Distribution, pixyz.losses.losses.Loss or pixyz.models.model.Model.) –
pixyz.utils.convert_latex_name(name)[source]