Coverage for mlair/configuration/path_config.py: 100%
57 statements
« prev ^ index » next coverage.py v6.4.2, created at 2022-12-02 15:24 +0000
« prev ^ index » next coverage.py v6.4.2, created at 2022-12-02 15:24 +0000
1"""Functions related to path and os name setting."""
2import getpass
3import logging
4import os
5import re
6import shutil
7import socket
8from typing import Union
10# ROOT_PATH = os.path.abspath(os.path.join(os.path.dirname(__file__), "..", ".."))
11ROOT_PATH = os.getcwd()
14def prepare_host(create_new=True, data_path=None) -> str:
15 """
16 Set up host path.
18 INFO: This functions is designed to handle known hosts. For proper working, please add your hostname hardcoded here.
19 Otherwise parse your custom data_path in kwargs. If data_path is provided, hardcoded paths for known hosts will be
20 ignored!
22 :param create_new: Create new path if enabled
23 :param data_path: Parse your custom path (and therefore ignore preset paths fitting to known hosts)
25 :return: full path to data
26 """
27 if data_path is None:
28 hostname = get_host()
29 user = getpass.getuser()
30 runner_regex = re.compile(r"runner-.*-project-2411-concurrent-\d+")
31 if hostname == "ZAM144":
32 data_path = f"/home/{user}/Data/toar/"
33 elif hostname == "zam347":
34 data_path = f"/home/{user}/Data/toar/"
35 elif (len(hostname) > 2) and (hostname[:2] == "jr"):
36 data_path = f"/p/project/cjjsc42/{user}/DATA/toar/"
37 elif (len(hostname) > 2) and (hostname[:2] in ['jw', 'ju'] or hostname[:5] in ['hdfml']):
38 data_path = f"/p/project/deepacf/intelliaq/{user}/DATA/MLAIR/"
39 elif runner_regex.match(hostname) is not None:
40 data_path = f"/home/{user}/mlair/data/"
41 else:
42 data_path = os.path.join(os.getcwd(), "data")
44 if not os.path.exists(data_path):
45 try:
46 if create_new:
47 check_path_and_create(data_path)
48 return data_path
49 else:
50 raise PermissionError
51 except PermissionError:
52 raise NotADirectoryError(f"path '{data_path}' does not exist for host '{hostname}'.")
53 else:
54 logging.debug(f"set path to: {data_path}")
55 return data_path
58def set_experiment_path(name: str, path: str = None) -> str:
59 """
60 Set name of experiment and its path.
62 * Experiment path is set to `<experiment_path>/<exp_name>` if provided or `ROOT_PATH/<exp_name>` otherwise
64 :param name: custom experiment name
65 :param path: custom experiment path
67 :return: full experiment path
68 """
69 if path is None:
70 experiment_path = os.path.abspath(os.path.join(ROOT_PATH, name))
71 else:
72 experiment_path = os.path.join(os.path.abspath(path), name)
73 return experiment_path
76def set_experiment_name(name: str = None, sampling: Union[str, tuple] = None) -> str:
77 """
78 Set name of experiment and its path.
80 * Experiment name is set to `TestExperiment` if not provided. If a name is given, this string is expanded
81 by suffix `_network`. Experiment name is always expanded by `_<sampling>` as ending suffix if sampling is given.
83 :param name: custom experiment name
84 :param sampling: sampling rate as string to add to experiment name
86 :return: experiment name
87 """
88 if name is None:
89 experiment_name = "TestExperiment"
90 else:
91 experiment_name = f"{name}_network"
92 if sampling is not None:
93 if not isinstance(sampling, str):
94 sampling = sampling[-1]
95 experiment_name += f"_{sampling}"
96 return experiment_name
99def set_bootstrap_path(bootstrap_path: str, data_path: str) -> str:
100 """
101 Set path for bootstrap input data.
103 Either use given bootstrap_path or create additional folder in same directory like data path.
105 :param bootstrap_path: custom path to store bootstrap data
106 :param data_path: path of data for default bootstrap path
108 :return: full bootstrap path
109 """
110 if bootstrap_path is None:
111 bootstrap_path = os.path.join(data_path, "bootstrap")
112 check_path_and_create(bootstrap_path)
113 return os.path.abspath(bootstrap_path)
116def check_path_and_create(path: str, remove_existing: bool = False) -> None:
117 """
118 Check a given path and create if not existing.
120 :param path: path to check and create
121 :param remove_existing: if set to true an existing folder is removed and replaced by a new one (default False).
122 """
123 try:
124 os.makedirs(path)
125 logging.debug(f"Created path: {path}")
126 except FileExistsError:
127 if remove_existing is True:
128 logging.debug(f"Remove / clean path: {path}")
129 shutil.rmtree(path)
130 check_path_and_create(path, remove_existing=False)
131 else:
132 logging.debug(f"Path already exists: {path}")
135def get_host():
136 return socket.gethostname()