colosseum.utils.formatter
1from typing import Union 2 3import numpy as np 4import toolz 5 6 7def clear_agent_mdp_class_name(class_name: str) -> str: 8 """ 9 Returns 10 ------- 11 str 12 A nicely formatted version of the class name. 13 """ 14 return ( 15 class_name.replace("Continuous", "") 16 .replace("Episodic", "") 17 .replace("QL", "Q-l") 18 ) 19 20 21def cleaner(x) -> Union[float, str]: 22 """ 23 Returns 24 ------- 25 Union[float, str] 26 A cleaned version of the object given in input that is ready to be transformed to string. 27 """ 28 from colosseum.mdp.simple_grid.base import SimpleGridReward 29 from colosseum.mdp.utils import MDPCommunicationClass 30 31 if "numpy" in str(type(x)) and "bool" not in str(type(x)): 32 return float(np.round(x, 5)) 33 if "scipy" in str(type(x)) and "frozen" in str(type(x)): 34 return ( 35 f"{x.dist.name.capitalize()}" 36 f"({', '.join(map(str, map(lambda y : float(np.round(y, 2)), x.args)))})" 37 ) 38 if type(x) == float: 39 return float(np.round(x, 5)) 40 if type(x) in [MDPCommunicationClass, SimpleGridReward]: 41 return x.name 42 return x 43 44 45def clean_for_storing(inp: Union[dict, list]) -> Union[dict, list]: 46 """ 47 Returns 48 ------- 49 Union[dict, list] 50 The list or the values of the dictionary given in input to a form that is ready to be stored. 51 """ 52 if type(inp) == dict: 53 return toolz.valmap(cleaner, inp) 54 elif type(inp) == list: 55 return list(map(cleaner, inp)) 56 raise NotImplementedError( 57 f"'clean_for_storing' not implemented for type {type(inp)}." 58 ) 59 60 61def clean_for_file_path(s: str) -> str: 62 """ 63 Returns 64 ------- 65 str 66 The string given in input with all symbols that are not allowed for file names removed. 67 """ 68 return ( 69 s.replace("_", "-") 70 .replace(".", "_") 71 .replace(" ", "") 72 .replace("(", "") 73 .replace(")", "") 74 .replace(",", "__") 75 )
def
clear_agent_mdp_class_name(class_name: str) -> str:
8def clear_agent_mdp_class_name(class_name: str) -> str: 9 """ 10 Returns 11 ------- 12 str 13 A nicely formatted version of the class name. 14 """ 15 return ( 16 class_name.replace("Continuous", "") 17 .replace("Episodic", "") 18 .replace("QL", "Q-l") 19 )
Returns
- str: A nicely formatted version of the class name.
def
cleaner(x) -> Union[float, str]:
22def cleaner(x) -> Union[float, str]: 23 """ 24 Returns 25 ------- 26 Union[float, str] 27 A cleaned version of the object given in input that is ready to be transformed to string. 28 """ 29 from colosseum.mdp.simple_grid.base import SimpleGridReward 30 from colosseum.mdp.utils import MDPCommunicationClass 31 32 if "numpy" in str(type(x)) and "bool" not in str(type(x)): 33 return float(np.round(x, 5)) 34 if "scipy" in str(type(x)) and "frozen" in str(type(x)): 35 return ( 36 f"{x.dist.name.capitalize()}" 37 f"({', '.join(map(str, map(lambda y : float(np.round(y, 2)), x.args)))})" 38 ) 39 if type(x) == float: 40 return float(np.round(x, 5)) 41 if type(x) in [MDPCommunicationClass, SimpleGridReward]: 42 return x.name 43 return x
Returns
- Union[float, str]: A cleaned version of the object given in input that is ready to be transformed to string.
def
clean_for_storing(inp: Union[dict, list]) -> Union[dict, list]:
46def clean_for_storing(inp: Union[dict, list]) -> Union[dict, list]: 47 """ 48 Returns 49 ------- 50 Union[dict, list] 51 The list or the values of the dictionary given in input to a form that is ready to be stored. 52 """ 53 if type(inp) == dict: 54 return toolz.valmap(cleaner, inp) 55 elif type(inp) == list: 56 return list(map(cleaner, inp)) 57 raise NotImplementedError( 58 f"'clean_for_storing' not implemented for type {type(inp)}." 59 )
Returns
- Union[dict, list]: The list or the values of the dictionary given in input to a form that is ready to be stored.
def
clean_for_file_path(s: str) -> str:
62def clean_for_file_path(s: str) -> str: 63 """ 64 Returns 65 ------- 66 str 67 The string given in input with all symbols that are not allowed for file names removed. 68 """ 69 return ( 70 s.replace("_", "-") 71 .replace(".", "_") 72 .replace(" ", "") 73 .replace("(", "") 74 .replace(")", "") 75 .replace(",", "__") 76 )
Returns
- str: The string given in input with all symbols that are not allowed for file names removed.