from fastcore.test import test_eq
Scientific project utils
Inspirations
Naming Tools
def test_valtostring():
assert valtostring(valtostring) == "valtostring"
assert valtostring(1.2345) == "1.23"
assert valtostring(True) == str(True)
test_valtostring()
savename
savename (params:dict, prefix:str='', suffix:str='', allowedtypes:Tuple=(<class 'int'>, <class 'float'>, <class 'str'>, <class 'bool'>), accesses:list[str]=None, ignores:list[str]=[], connector:str='_', equals:str='=', expand:list[str]=[], val2string:Callable=<multiple-dispatch function valtostring (with 0 registered and 3 pending method(s))>, sort:bool=True)
Create a shorthand name based on the parameters in the dictionary c
.
Type | Default | Details | |
---|---|---|---|
params | dict | The input dictionary containing the parameters. | |
prefix | str | The prefix to start the name with. | |
suffix | str | The suffix to end the name with. | |
allowedtypes | Tuple | (<class ‘int’>, <class ‘float’>, <class ‘str’>, <class ‘bool’>) | Only values of these types are included. (default: (int, float, str, bool, timedelta)) |
accesses | list | None | Specific keys to include in the name. |
ignores | list | [] | Specific keys to ignore. |
connector | str | _ | String used to connect key-value pairs. |
equals | str | = | Connector between key and value. |
expand | list | [] | Keys that will be expanded to their nested savename. |
val2string | Callable | valtostring | Function to convert values to strings. |
sort | bool | True | Whether to sort the keys alphabetically. |
Returns | str | The generated shorthand name. |
# Test savename
def test_savename():
= {"a": 0.153456453, "b": 5, "mode": "double"}
d "a=0.15_b=5_mode=double")
test_eq(savename(d), ="n"), "n_a=0.15_b=5_mode=double")
test_eq(savename(d, prefix="n"), "a=0.15_b=5_mode=double.n")
test_eq(savename(d, suffix
test_eq(="n", suffix="n", connector="-"), "n-a=0.15-b=5-mode=double.n"
savename(d, prefix
)=(str,)), "mode=double")
test_eq(savename(d, allowedtypes
test_eq(=" | ", equals=" = "), "a = 0.15 | b = 5 | mode = double"
savename(d, connector
)
= {"never": "gonna", "give": "you", "up": "!"}
rick "give=you_never=gonna_up=!")
test_eq(savename(rick), =["up"]), "give=you_never=gonna")
test_eq(savename(rick, ignores
= {"a": 1, "b": {"c": 2, "d": 3}}
nested_dict
test_eq(=(int, dict), expand=["b"]), "a=1_b=(c=2,d=3)"
savename(nested_dict, allowedtypes
)
test_savename()
append_prefix_suffix
append_prefix_suffix (name:str, prefix:str, suffix:str)
Directory tools
plotsdir
plotsdir (subdir='figures')
datadir
datadir (subdir='data')
projectdir
projectdir ()
setup_run_dir
setup_run_dir (c:dict, base_dir:Callable=<function datadir>, change_dir:bool=True, **kwargs)
Create a run directory based on the parameters in the dictionary c
.
Type | Default | Details | |
---|---|---|---|
c | dict | The dictionary containing the parameters. | |
base_dir | Callable | datadir | The base directory to create the run directory in. |
change_dir | bool | True | |
kwargs | |||
Returns | Path | The run directory. |
Saving Tools
safesave
safesave (file:str, data, save_func:Callable=functools.partial(<function action at 0x7f6502c87b00>, 'save'))
Produce or Load
produce_or_load
produce_or_load (f:Callable, config:dict={}, path:pathlib.Path=Path('/hom e/runner/work/beforerr_dev/beforerr_dev/data'), suffix:str='pickle', prefix:str=None, force:bool=False, verbose:bool=True, action_kwargs:dict={}, filename:Union[Callable,str,NoneType]=None, **kwargs)
produce_or_load_file
produce_or_load_file (f:Callable, config:dict, file:pathlib.Path, force:bool=False, verbose:bool=True, load_func:Callable=functools.partial(<function action at 0x7f6502c87b00>, 'load'), save_func:Callable=functools.partial(<function action at 0x7f6502c87b00>, 'save'), **kwargs)