colosseum.hardness.measures.utils

 1import os
 2from glob import glob
 3from typing import TYPE_CHECKING, Any, Dict, Type, Union
 4
 5import yaml
 6
 7from colosseum.utils import cleaner
 8
 9if TYPE_CHECKING:
10    from colosseum.mdp import BaseMDP
11
12
13def find_hardness_report_file(
14    mdp: "BaseMDP", hardness_reports_folder="hardness_reports"
15) -> Union[str, None]:
16    """
17    tries to find a previously calculated hardness report for an MDP instance.
18
19    Parameters
20    ----------
21    mdp : MDP
22        the MDP instance for which it will be looking for the hardness report.
23    hardness_reports_folder : str
24        the folder in which the hardness reports are stored
25    Returns
26    -------
27    Union[str, None]
28        The file path for the cache file or None if no file has been found.
29    """
30    seed_report = glob(f"{hardness_reports_folder}{os.sep}{type(mdp).__name__}_*.yml")
31    for existing_report in seed_report:
32        if type(mdp).__name__ not in existing_report:
33            continue
34        with open(existing_report, "r") as f:
35            report = yaml.load(f, yaml.Loader)
36        same_mdp = True
37        for k, v in report["MDP parameters"].items():
38            if not same_mdp:
39                break
40            same_mdp = cleaner(getattr(mdp, k)) == v
41        for k, v in report["MDP graph metrics"].items():
42            if not same_mdp:
43                break
44            same_mdp = mdp.graph_metrics[k] == v
45        if same_mdp:
46            return existing_report
47    return None
48
49
50def get_average_measure_values(
51    mdp_class: Type["BaseMDP"],
52    mdp_kwargs: Dict[str, Any],
53    n_seeds: int = None,
54    measure_names_max_values: Dict[str, float] = None,
55) -> Union[None, Dict[str, float]]:
56    """
57    computes the values of the measures given in inputs averaged across several seed if necessary. Note that this is
58    only necessary when the MDP structure changes for different seeds.
59
60    Parameters
61    ----------
62    mdp_class : Type["BaseMDP"]
63        The MDP class for which the measures will be computed.
64    mdp_kwargs : Dict[str, Any]
65        The parameters that are used to instantiate the MDP.
66    n_seeds : int, optional
67        The number of seeds for the average. By default, it is set to five when necessary.
68    measure_names_max_values : Dict[str, float], optional
69        The dictionary whose keys are the names of the measure of hardness and the values are the maximum value that
70        we allow them to be. By default, it is set to 'dict(diameter=200, value_norm=5.0)'.
71
72    Returns
73    -------
74    Union[None, Dict[str, float]]
75        The values of the measures. If any of the measure produces a value that is higher than the limit, the function
76        returns None.
77    """
78    if mdp_class.does_seed_change_MDP_structure():
79        n_seeds = 5 if n_seeds is None else n_seeds
80        assert n_seeds > 1
81    else:
82        n_seeds = 1 if n_seeds is None else n_seeds
83        assert n_seeds == 1
84
85    if measure_names_max_values is None:
86        measure_names_max_values = dict(diameter=200, value_norm=5.0)
87    average_values = dict(
88        zip(measure_names_max_values.keys(), [0] * len(measure_names_max_values))
89    )
90
91    for seed in range(n_seeds):
92        mdp = mdp_class(seed=seed, **mdp_kwargs)
93        for m_name, m_max_value in measure_names_max_values.items():
94            m_value = mdp.get_measure_from_name(m_name)
95            if m_value is None or m_value > m_max_value:
96                return None
97            average_values[m_name] += m_value / n_seeds
98    return average_values
def find_hardness_report_file( mdp: colosseum.mdp.base.BaseMDP, hardness_reports_folder='hardness_reports') -> Optional[str]:
14def find_hardness_report_file(
15    mdp: "BaseMDP", hardness_reports_folder="hardness_reports"
16) -> Union[str, None]:
17    """
18    tries to find a previously calculated hardness report for an MDP instance.
19
20    Parameters
21    ----------
22    mdp : MDP
23        the MDP instance for which it will be looking for the hardness report.
24    hardness_reports_folder : str
25        the folder in which the hardness reports are stored
26    Returns
27    -------
28    Union[str, None]
29        The file path for the cache file or None if no file has been found.
30    """
31    seed_report = glob(f"{hardness_reports_folder}{os.sep}{type(mdp).__name__}_*.yml")
32    for existing_report in seed_report:
33        if type(mdp).__name__ not in existing_report:
34            continue
35        with open(existing_report, "r") as f:
36            report = yaml.load(f, yaml.Loader)
37        same_mdp = True
38        for k, v in report["MDP parameters"].items():
39            if not same_mdp:
40                break
41            same_mdp = cleaner(getattr(mdp, k)) == v
42        for k, v in report["MDP graph metrics"].items():
43            if not same_mdp:
44                break
45            same_mdp = mdp.graph_metrics[k] == v
46        if same_mdp:
47            return existing_report
48    return None

tries to find a previously calculated hardness report for an MDP instance.

Parameters
  • mdp (MDP): the MDP instance for which it will be looking for the hardness report.
  • hardness_reports_folder (str): the folder in which the hardness reports are stored
Returns
  • Union[str, None]: The file path for the cache file or None if no file has been found.
def get_average_measure_values( mdp_class: Type[colosseum.mdp.base.BaseMDP], mdp_kwargs: Dict[str, Any], n_seeds: int = None, measure_names_max_values: Dict[str, float] = None) -> Optional[Dict[str, float]]:
51def get_average_measure_values(
52    mdp_class: Type["BaseMDP"],
53    mdp_kwargs: Dict[str, Any],
54    n_seeds: int = None,
55    measure_names_max_values: Dict[str, float] = None,
56) -> Union[None, Dict[str, float]]:
57    """
58    computes the values of the measures given in inputs averaged across several seed if necessary. Note that this is
59    only necessary when the MDP structure changes for different seeds.
60
61    Parameters
62    ----------
63    mdp_class : Type["BaseMDP"]
64        The MDP class for which the measures will be computed.
65    mdp_kwargs : Dict[str, Any]
66        The parameters that are used to instantiate the MDP.
67    n_seeds : int, optional
68        The number of seeds for the average. By default, it is set to five when necessary.
69    measure_names_max_values : Dict[str, float], optional
70        The dictionary whose keys are the names of the measure of hardness and the values are the maximum value that
71        we allow them to be. By default, it is set to 'dict(diameter=200, value_norm=5.0)'.
72
73    Returns
74    -------
75    Union[None, Dict[str, float]]
76        The values of the measures. If any of the measure produces a value that is higher than the limit, the function
77        returns None.
78    """
79    if mdp_class.does_seed_change_MDP_structure():
80        n_seeds = 5 if n_seeds is None else n_seeds
81        assert n_seeds > 1
82    else:
83        n_seeds = 1 if n_seeds is None else n_seeds
84        assert n_seeds == 1
85
86    if measure_names_max_values is None:
87        measure_names_max_values = dict(diameter=200, value_norm=5.0)
88    average_values = dict(
89        zip(measure_names_max_values.keys(), [0] * len(measure_names_max_values))
90    )
91
92    for seed in range(n_seeds):
93        mdp = mdp_class(seed=seed, **mdp_kwargs)
94        for m_name, m_max_value in measure_names_max_values.items():
95            m_value = mdp.get_measure_from_name(m_name)
96            if m_value is None or m_value > m_max_value:
97                return None
98            average_values[m_name] += m_value / n_seeds
99    return average_values

computes the values of the measures given in inputs averaged across several seed if necessary. Note that this is only necessary when the MDP structure changes for different seeds.

Parameters
  • mdp_class (Type["BaseMDP"]): The MDP class for which the measures will be computed.
  • mdp_kwargs (Dict[str, Any]): The parameters that are used to instantiate the MDP.
  • n_seeds (int, optional): The number of seeds for the average. By default, it is set to five when necessary.
  • measure_names_max_values (Dict[str, float], optional): The dictionary whose keys are the names of the measure of hardness and the values are the maximum value that we allow them to be. By default, it is set to 'dict(diameter=200, value_norm=5.0)'.
Returns
  • Union[None, Dict[str, float]]: The values of the measures. If any of the measure produces a value that is higher than the limit, the function returns None.