-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.py
173 lines (117 loc) · 4.21 KB
/
common.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
"""
Most commonly used methods/constants, used across the whole project
"""
import os
import time
import logging
# ---------------------------------------------------------------------
# --- Config
# ---------------------------------------------------------------------
# set where is the root folder of this project, relative of this file
ROOT_DIR = '../'
# set where is the source root folder of this project, relative of ROOT
SRC_DIR = 'src'
# set where is the data folder, relative of ROOT
DATA_DIR = 'data'
# set where is the temp folder is, relative of ROOT
TEMP_DIR = 'data/temp'
# ---------------------------------------------------------------------
# --- Constants
# ---------------------------------------------------------------------
COL_SPEAKER = 'speaker'
COL_START = 'from'
COL_END = 'end'
COL_TEXT = 'text'
COLS_STRUC_TRANSCRIPT = [COL_SPEAKER, COL_START, COL_END, COL_TEXT]
# ---------------------------------------------------------------------
# --- Commonly used functions
# ---------------------------------------------------------------------
def path2id(audio_fpath, level_from=-2, level_to=None):
"""
E.g. from
"/home/ons21553/wspace/interview-transcripts/data/recordings/bbc_interview/audio.mp3"
makes
"bbc_interview__audio"
"""
audio_fpath = os.path.abspath(audio_fpath)
_id = '.'.join(audio_fpath.split('.')[:-1])
_id = '__'.join(_id.split('/')[level_from:level_to])
return _id
def setup_logging():
logging.basicConfig(
level=logging.DEBUG,
format='%(levelname)s: %(asctime)s: %(message)s'
)
def get_fname_without_ext(fpath):
base_name = os.path.basename(fpath)
fname = '.'.join(base_name.split('.')[:-1])
return fname
def get_fname_with_ext(fpath):
return os.path.basename(fpath)
def create_directories_if_necessary(path):
"""
Given a path, creates all the directories necessary till the last '/'
encountered. E.g.
if '/path/to/' exists and the path argument is '/path/to/file/is/this',
calling this would create '/path/to/file/is/'
"""
if '/' not in path:
return
dir_path = path[0:path.rfind('/') + 1]
if os.path.exists(dir_path):
return
os.makedirs(dir_path)
def from_root(path, create_if_needed=False):
"""
Returns path with project root prepended
"""
this_file_dir = os.path.realpath(os.path.dirname(__file__))
proj_root = os.path.join(this_file_dir, ROOT_DIR)
result_path = os.path.join(proj_root, path)
if create_if_needed:
create_directories_if_necessary(result_path)
return result_path
def from_src_root(path, create_if_needed=False):
"""
Returns path with project root prepended
"""
return from_root(f'{SRC_DIR}/{path}', create_if_needed=create_if_needed)
def from_data_root(path, create_if_needed=False):
"""
Returns path with data project root prepended
"""
return from_root(f'{DATA_DIR}/{path}', create_if_needed=create_if_needed)
def from_temp_dir(path, create_if_needed=False):
"""
Returns path with data project root prepended
"""
return from_root(f'{TEMP_DIR}/{path}', create_if_needed=create_if_needed)
# ---------------------------------------------------------------------
# --- Timing of code
# ---------------------------------------------------------------------
__start_times = {}
def start_timing(id, msg=''):
__start_times[id] = time.time()
print()
print('v'*20)
print(f'START of timing {id}: {msg}')
def end_timing(id, msg=''):
start_time = __start_times[id]
duration = time.time() - start_time
minutes = int(duration // 60)
secs = duration % 60
print(f'END of timing {id}: {msg} ({minutes}m, {secs:.1f}s)')
print('^'*20)
print()
# ---------------------------------------------------------------------
# --- Executing
# ---------------------------------------------------------------------
# run these to create dir structure first time if not existing
from_src_root('', create_if_needed=True)
from_data_root('', create_if_needed=True)
from_temp_dir('', create_if_needed=True)
if __name__ == '__main__':
# test
print(from_src_root('some_src'))
print(from_data_root('some_data'))
print(from_temp_dir('some_temp'))