forked from NathanPhilliber/DATA-Capstone
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
104 lines (77 loc) · 2.75 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import os
import shutil
import sys
import importlib
import inspect
PROJECT_ROOT = os.path.dirname(os.path.abspath(__file__))
GEN_DIR = os.path.join(PROJECT_ROOT, "datagen/matlab_scripts")
MODELS_DIR = os.path.join(PROJECT_ROOT, "models")
NETWORKS_DIR = os.path.join(MODELS_DIR, "networks")
DATA_ROOT = os.path.join(PROJECT_ROOT, "data")
DATA_DIR = os.path.join(DATA_ROOT, "datasets")
MODEL_RES_DIR = os.path.join(DATA_ROOT, "results")
TRAIN_DATASET_PREFIX = "train"
TEST_DATASET_PREFIX = "test"
DATASET_FILE_TYPE = "pkl"
DATAGEN_CONFIG = "gen_info.json"
WEIGHTS_FILENAME = "weights.h5"
TRAIN_INFO_FILENAME = "info.json"
COMET_KEY = "rKj0YN2SYHxxZ5dYvS3WJ1jkz"
COMET_SAVE_FILENAME = "comet-experiment.json"
PROJECT_NAME = 'data-capstone-nasa'
DEFAULT_BATCH_SIZE = 32
DEFAULT_N_EPOCHS = 10
RESULT_DIR_DELIM = "_"
def to_local_path(path):
return path.replace(PROJECT_ROOT, "")[len(os.path.sep):]
def try_create_directory(directory, silent=False):
"""
Create directory if it doesn't already exist
:param directory: dir path
:param silent: bool, if True then do not print messages
:returns bool, True if dir was created
"""
try:
os.mkdir(directory)
return True
except FileExistsError:
if not silent:
print(f"Info: '{directory}' already exists.")
return False
def check_clear_directory(directory, force=False):
"""
Check if there are files in a directory. If there are prompt to delete
:param directory: dir to check
:param force: do not prompt for deletion
:return: True if directory is clear, False otherwise
"""
files = os.listdir(directory)
if len(files) == 0:
return True
answer = 'y'
if not force:
print(f"Warning '{directory}' contains the following files:")
print("\t" + "\n\t".join(files))
answer = input("\nWould you like to remove them? (y/n) ")
if answer.lower() == 'y':
try:
shutil.rmtree(directory)
os.mkdir(directory)
return True
except:
print("Error while creating directory. ")
return False
def get_modules(dirpath):
mods = []
for file in sorted(os.listdir(dirpath)):
if file[:2] != "__":
localpath = to_local_path(dirpath)
package_name = ".".join(os.path.split(localpath)) + "." + os.path.splitext(file)[0]
mod = importlib.import_module(package_name)
mods.append((mod, package_name))
return mods
def get_classes(module, package_name):
return [m[0] for m in inspect.getmembers(module, inspect.isclass) if m[1].__module__ == package_name]
try_create_directory(DATA_ROOT, silent=True)
try_create_directory(DATA_DIR, silent=True)
try_create_directory(MODEL_RES_DIR, silent=True)