Fumagalli_Motta_Tarantino_2020.Notebooks.NotebookUtilities
1import matplotlib.pyplot as plt 2import Fumagalli_Motta_Tarantino_2020 as FMT20 3 4 5def configure_two_axes( 6 main="", 7 sub1="", 8 sub2="", 9 m1: FMT20.OptimalMergerPolicy = None, 10 m2: FMT20.OptimalMergerPolicy = None, 11 c1: int = 1, 12 c2: int = 2, 13 v1=FMT20.MergerPoliciesAssetRange, 14 v2=FMT20.MergerPoliciesAssetRange, 15 figsize=(11, 5), 16 **kwargs, 17) -> (plt.Figure, plt.Axes, plt.Axes): 18 """ 19 Creates a figure with two subplots in a row. 20 """ 21 fig = plt.figure(layout="constrained", figsize=figsize) 22 axes = fig.subplot_mosaic("""AB""") 23 fig.suptitle(main, fontweight="bold", fontsize="x-large") 24 fig.supylabel(kwargs.get("sub_y_label", "Merger Policy")) 25 m1, m2 = _get_model(m1, m2, c1, c2, **kwargs) 26 v1(model=m1, ax=axes["A"]).plot(**get_plot_kwargs(title=sub1, **kwargs)) 27 v2(model=m2, ax=axes["B"]).plot(**get_plot_kwargs(title=sub2, **kwargs)) 28 return fig, axes 29 30 31def get_plot_kwargs(title: str, **kwargs): 32 """ 33 Returns a standardized set of kwargs for the plots. 34 """ 35 return { 36 "title": title, 37 "legend": kwargs.get("legend", False), 38 "optimal_policy": True, 39 "y_label": kwargs.get("y_label", ""), 40 "parameters": False, 41 "thresholds": True, 42 "y_offset": -27, 43 } 44 45 46def get_model_by_id( 47 c: int, preferred_type=FMT20.OptimalMergerPolicy, **kwargs 48) -> FMT20.OptimalMergerPolicy: 49 """ 50 Returns a valid model from a preset configuration, which is identified by an integer id. 51 """ 52 p = FMT20.LoadParameters(config_id=c) 53 p.set_merger_policy(kwargs.get("policy", FMT20.MergerPolicies.Strict)) 54 try: 55 return _get_model_by_type(preferred_type, **p(), **kwargs) 56 except AssertionError: 57 try: 58 return _get_model_by_type(FMT20.ProCompetitive, **p(), **kwargs) 59 except AssertionError: 60 return _get_model_by_type( 61 FMT20.ResourceWaste, 62 distribution=kwargs.get( 63 "distribution", FMT20.Distributions.NormalDistribution 64 ), 65 **p(), 66 **kwargs, 67 ) 68 69 70def _get_model_by_type(model_type, **kwargs) -> FMT20.OptimalMergerPolicy: 71 return model_type( 72 asset_distribution=kwargs.get( 73 "distribution", FMT20.Distributions.UniformDistribution 74 ), 75 **kwargs, 76 ) 77 78 79def _get_model( 80 m1, m2, c1, c2, **kwargs 81) -> (FMT20.OptimalMergerPolicy, FMT20.OptimalMergerPolicy): 82 if (m1 is not None) and (m2 is not None): 83 return m1, m2 84 if ((m1 is not None) and (m2 is None)) or ((m1 is None) and (m2 is not None)): 85 raise NotImplementedError("Too less or too much None to survive") 86 return get_model_by_id( 87 c1, policy=kwargs.get("policy1", FMT20.MergerPolicies.Strict), **kwargs 88 ), get_model_by_id( 89 c2, policy=kwargs.get("policy2", FMT20.MergerPolicies.Strict), **kwargs 90 ) 91 92 93def get_distribution_labels( 94 distribution: type(FMT20.Distributions.NormalDistribution), long_label=False 95) -> str: 96 """ 97 Returns labels for the available distributions. 98 """ 99 if distribution == FMT20.Distributions.NormalDistribution: 100 return "Normal" + (" Distribution" if long_label else "") 101 if distribution == FMT20.Distributions.UniformDistribution: 102 return "Uniform" + (" Distribution" if long_label else "") 103 104 105def get_configurations() -> list[str]: 106 """ 107 Returns a list with all available configurations and their labels. 108 """ 109 output = [] 110 for i in range(0, 60): 111 try: 112 m = get_model_by_id(i) 113 output.append(f"{i} - {FMT20.IVisualize.get_model_label(type(m))}") 114 except FMT20.IDNotAvailableError: 115 pass 116 return output
def
configure_two_axes( main='', sub1='', sub2='', m1: Fumagalli_Motta_Tarantino_2020.Models.Base.OptimalMergerPolicy = None, m2: Fumagalli_Motta_Tarantino_2020.Models.Base.OptimalMergerPolicy = None, c1: int = 1, c2: int = 2, v1=<class 'Fumagalli_Motta_Tarantino_2020.Visualizations.VisualizeRanges.MergerPoliciesAssetRange'>, v2=<class 'Fumagalli_Motta_Tarantino_2020.Visualizations.VisualizeRanges.MergerPoliciesAssetRange'>, figsize=(11, 5), **kwargs) -> (<class 'matplotlib.figure.Figure'>, <class 'matplotlib.axes._axes.Axes'>, <class 'matplotlib.axes._axes.Axes'>):
6def configure_two_axes( 7 main="", 8 sub1="", 9 sub2="", 10 m1: FMT20.OptimalMergerPolicy = None, 11 m2: FMT20.OptimalMergerPolicy = None, 12 c1: int = 1, 13 c2: int = 2, 14 v1=FMT20.MergerPoliciesAssetRange, 15 v2=FMT20.MergerPoliciesAssetRange, 16 figsize=(11, 5), 17 **kwargs, 18) -> (plt.Figure, plt.Axes, plt.Axes): 19 """ 20 Creates a figure with two subplots in a row. 21 """ 22 fig = plt.figure(layout="constrained", figsize=figsize) 23 axes = fig.subplot_mosaic("""AB""") 24 fig.suptitle(main, fontweight="bold", fontsize="x-large") 25 fig.supylabel(kwargs.get("sub_y_label", "Merger Policy")) 26 m1, m2 = _get_model(m1, m2, c1, c2, **kwargs) 27 v1(model=m1, ax=axes["A"]).plot(**get_plot_kwargs(title=sub1, **kwargs)) 28 v2(model=m2, ax=axes["B"]).plot(**get_plot_kwargs(title=sub2, **kwargs)) 29 return fig, axes
Creates a figure with two subplots in a row.
def
get_plot_kwargs(title: str, **kwargs):
32def get_plot_kwargs(title: str, **kwargs): 33 """ 34 Returns a standardized set of kwargs for the plots. 35 """ 36 return { 37 "title": title, 38 "legend": kwargs.get("legend", False), 39 "optimal_policy": True, 40 "y_label": kwargs.get("y_label", ""), 41 "parameters": False, 42 "thresholds": True, 43 "y_offset": -27, 44 }
Returns a standardized set of kwargs for the plots.
def
get_model_by_id( c: int, preferred_type=<class 'Fumagalli_Motta_Tarantino_2020.Models.Base.OptimalMergerPolicy'>, **kwargs) -> Fumagalli_Motta_Tarantino_2020.Models.Base.OptimalMergerPolicy:
47def get_model_by_id( 48 c: int, preferred_type=FMT20.OptimalMergerPolicy, **kwargs 49) -> FMT20.OptimalMergerPolicy: 50 """ 51 Returns a valid model from a preset configuration, which is identified by an integer id. 52 """ 53 p = FMT20.LoadParameters(config_id=c) 54 p.set_merger_policy(kwargs.get("policy", FMT20.MergerPolicies.Strict)) 55 try: 56 return _get_model_by_type(preferred_type, **p(), **kwargs) 57 except AssertionError: 58 try: 59 return _get_model_by_type(FMT20.ProCompetitive, **p(), **kwargs) 60 except AssertionError: 61 return _get_model_by_type( 62 FMT20.ResourceWaste, 63 distribution=kwargs.get( 64 "distribution", FMT20.Distributions.NormalDistribution 65 ), 66 **p(), 67 **kwargs, 68 )
Returns a valid model from a preset configuration, which is identified by an integer id.
def
get_distribution_labels(distribution: type, long_label=False) -> str:
94def get_distribution_labels( 95 distribution: type(FMT20.Distributions.NormalDistribution), long_label=False 96) -> str: 97 """ 98 Returns labels for the available distributions. 99 """ 100 if distribution == FMT20.Distributions.NormalDistribution: 101 return "Normal" + (" Distribution" if long_label else "") 102 if distribution == FMT20.Distributions.UniformDistribution: 103 return "Uniform" + (" Distribution" if long_label else "")
Returns labels for the available distributions.
def
get_configurations() -> list[str]:
106def get_configurations() -> list[str]: 107 """ 108 Returns a list with all available configurations and their labels. 109 """ 110 output = [] 111 for i in range(0, 60): 112 try: 113 m = get_model_by_id(i) 114 output.append(f"{i} - {FMT20.IVisualize.get_model_label(type(m))}") 115 except FMT20.IDNotAvailableError: 116 pass 117 return output
Returns a list with all available configurations and their labels.