honegumi.core package

Submodules

honegumi.core._honegumi module

This module contains Honegumi’s core functionality.

References

class honegumi.core._honegumi.ResultsCollector[source]

Bases: object

A class for collecting and summarizing results of pytest test runs.

https://stackoverflow.com/a/72278485/13697228

reports

A list of test reports generated during the test run.

Type:

List[pytest.TestReport]

collected

The number of test items collected for the test run.

Type:

int

exitcode

The exit code of the test run.

Type:

int

passed

A list of test reports for tests that passed.

Type:

List[pytest.TestReport]

failed

A list of test reports for tests that failed.

Type:

List[pytest.TestReport]

xfailed

A list of test reports for tests that were expected to fail but passed.

Type:

List[pytest.TestReport]

skipped

A list of test reports for tests that were skipped.

Type:

List[pytest.TestReport]

total_duration

The total duration of the test run in seconds.

Type:

float

Examples

>>> collector = ResultsCollector()
>>> # run pytest tests
>>> collector.total_duration
10.123456789
pytest_collection_modifyitems(items: List[Item]) None[source]

A pytest hook for modifying collected test items.

Parameters:

items (List[pytest.Item]) – A list of pytest.Item objects representing the collected test items.

Examples

>>> items = ...
>>> collector = ResultsCollector()
>>> collector.pytest_collection_modifyitems(items)
pytest_runtest_makereport(item: Item, call: CallInfo) None[source]

A pytest hook for collecting test reports.

Parameters:
  • item (pytest.Item) – The test item being run.

  • call (pytest.CallInfo) – The result of running the test item.

Examples

>>> item = ...
>>> call = ...
>>> collector = ResultsCollector()
>>> collector.pytest_runtest_makereport(item, call)
pytest_terminal_summary(terminalreporter: TerminalReporter, exitstatus: int) None[source]

A pytest hook for summarizing test results.

Parameters:
  • terminalreporter (pytest.terminal.TerminalReporter) – The terminal reporter object used to report test results.

  • exitstatus (int) – The exit status code of the test run.

Examples

>>> terminalreporter = ...
>>> exitstatus = ...
>>> collector = ResultsCollector()
>>> collector.pytest_terminal_summary(terminalreporter, exitstatus)
honegumi.core._honegumi.add_model_specific_keys(option_names, opt)[source]

Add model-specific keys to the options dictionary (in-place).

This function adds model-specific keys to the options dictionary opt. For example, if use_custom_gen is a hidden variable, and the model is FULLYBAYESIAN, then use_custom_gen should be True.

It also sets the value of the key model_kwargs based on the value of MODEL_OPT_KEY in opt.

Parameters:
  • option_names (list) – A list of option names.

  • opt (dict) – The options dictionary.

Examples

The following example is demonstrative, the range of cases may be expanded later.

>>> option_names = [
...     "objective",
...     "model",
...     "custom_gen",
...     "existing_data",
...     "sum_constraint",
...     "order_constraint",
...     "linear_constraint",
...     "composition_constraint",
...     "categorical",
...     "custom_threshold",
...     "fidelity",
...     "synchrony",
... ]
>>> opt = {
...     "objective": "single",
...     "model": "Default",
...     "existing_data": False,
...     "sum_constraint": False,
...     "order_constraint": False,
...     "linear_constraint": False,
...     "composition_constraint": False,
...     "categorical": False,
...     "custom_threshold": False,
...     "fidelity": "single",
...     "synchrony": "single",
... }
>>> add_model_specific_keys(option_names, opt)
{
    "objective": "single",
    "model": "Default",
    "existing_data": False,
    "sum_constraint": False,
    "order_constraint": False,
    "linear_constraint": False,
    "composition_constraint": False,
    "categorical": False,
    "custom_threshold": False,
    "fidelity": "single",
    "synchrony": "single",
    "custom_gen": False,
    "model_kwargs": {},
}
honegumi.core._honegumi.create_and_clear_dir(directory)[source]
honegumi.core._honegumi.fib(n)[source]

Fibonacci example function (just to demo CLI usage)

Parameters:

n (int) – integer

Returns:

n-th Fibonacci number

Return type:

int

honegumi.core._honegumi.gen_combs_with_keys(visible_option_names: List[str], visible_option_rows: List[dict])[source]

Generate a list of dictionaries, each representing a combination of options. Each dictionary uses option names as keys and the corresponding option from each combination as values.

Parameters:
  • visible_option_names (list of str) – A list of option names. These will be used as the keys in the output dictionaries.

  • visible_option_rows (list of dict) – A list of dictionaries, each containing an β€˜options’ key associated with a list of options. The β€˜options’ from each dictionary are combined to form the output dictionaries.

Returns:

A list of dictionaries, each representing a combination of options. Each dictionary uses option names as keys and the corresponding option from each combination as values.

Return type:

list of dict

Examples

>>> visible_option_names = ['color', 'size']
>>> visible_option_rows = [
...     {"options": ["red", "blue"]},
...     {"options": ["small", "large"]},
... ]
>>> gen_combs_with_keys(visible_option_names, visible_option_rows)
[
    {"color": "red", "size": "small"},
    {"color": "red", "size": "large"},
    {"color": "blue", "size": "small"},
    {"color": "blue", "size": "large"},
]
honegumi.core._honegumi.generate_lookup_dict(df, option_names, key)[source]

Generate a lookup dictionary from a pandas DataFrame.

Examples

>>> df = pd.DataFrame(
>>>     {
>>>         "option1": ["a", "b", "c"],
>>>         "option2": [1, 2, 3],
>>>         "key": ["foo", "bar", "baz"],
>>>     }
>>> )
>>> generate_lookup_dict(df, ['option1', 'option2'], 'key')
{'a,1': 'foo', 'b,2': 'bar', 'c,3': 'baz'}
honegumi.core._honegumi.get_rendered_template_stem(datum, option_names)[source]

Returns a string that represents the rendered template stem based on the given data and option names.

Filenames still have strict character limits even if longpaths enabled on Windows (https://stackoverflow.com/a/61628356/13697228), so use folder structure instead

Parameters:
  • data (dict) – A dictionary containing the data to be used in the rendered template stem.

  • option_names (list) – A list of strings representing the names of the options to be included in the rendered template stem.

Returns:

A string representing the rendered template stem.

Return type:

str

Examples

>>> data = {'option1': 'value1', 'option2': 'value2'}
>>> option_names = ['option1', 'option2']
>>> get_rendered_template_stem(data, option_names)
'option1-value1+option2-value2'
honegumi.core._honegumi.is_incompatible(opt)[source]

Check if the given option dictionary contains incompatible options.

An option is considered incompatible if it cannot be used together with another option. For example, if the model is fully Bayesian, it cannot use the custom generator (use_custom_gen). Similarly, if the objective is single, it cannot use the custom threshold (use_custom_threshold).

Parameters:

opt (dict) – The option dictionary to check for incompatibility.

Returns:

True if any incompatibility is found among the options, False otherwise.

Return type:

bool

honegumi.core._honegumi.main(args)[source]

Wrapper allowing fib() to be called with string arguments in a CLI fashion

Instead of returning the value from fib(), it prints the result to the stdout in a nicely formatted message.

Parameters:

args (List[str]) – command line parameters as list of strings (for example ["--verbose", "42"]).

honegumi.core._honegumi.parse_args(args)[source]

Parse command line parameters

Parameters:

args (List[str]) – command line parameters as list of strings (for example ["--help"]).

Returns:

command line parameters namespace

Return type:

argparse.Namespace

honegumi.core._honegumi.prep_datum_for_render(option_names, datum)[source]

Checks the compatibility of the given options and updates the datum dictionary with the rendered template stem and compatibility status.

Parameters:
  • option_names (list) – The full list of option names in order to generate the filename stem.

  • datum (dict) – The dictionary of option key-value pairs.

Return type:

None

Examples

>>> check_compatibility_and_update(['option1', 'option2'], {'option1': 'value1', 'option2': 'value2'}) # noqa: E501
OUTPUT

Notes

This function will always set the β€˜dummy’ key in the β€˜datum’ dictionary to False.

honegumi.core._honegumi.run()[source]

Calls main() passing the CLI arguments extracted from sys.argv

This function can be used as entry point to create console scripts with setuptools.

honegumi.core._honegumi.setup_logging(loglevel)[source]

Setup basic logging

Parameters:

loglevel (int) – minimum loglevel for emitting messages

honegumi.core._honegumi.unpack_rendered_template_stem(rendered_template_stem)[source]

This function takes a rendered template stem as input and returns a dictionary of options and their values.

Parameters:

rendered_template_stem (str) – The rendered template stem to be unpacked.

Returns:

A dictionary containing the options and their values.

Return type:

dict

Examples

>>> unpack_rendered_template_stem("option1-value1+option2-value2+option3-value3")
{'option1': 'value1', 'option2': 'value2', 'option3': 'value3'}

Module contents