-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils_general.py
441 lines (353 loc) · 11.8 KB
/
utils_general.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
"""
General utilities
"""
import os
import logging
from datetime import datetime
from operator import attrgetter
from argparse import RawTextHelpFormatter, ArgumentDefaultsHelpFormatter
from pathlib import Path
import re
import numpy as np
from glob import glob
import pandas as pd
class SortedMenu(ArgumentDefaultsHelpFormatter):
def add_arguments(self, actions):
actions = sorted(actions, key=attrgetter('option_strings'))
super(SortedMenu, self).add_arguments(actions)
class CustomFormatter(SortedMenu, RawTextHelpFormatter):
pass
def create_parser(defaults):
"""
Creates the parser object with the log arguments included.
"""
try:
import cli
parser = cli.parser()
except Exception:
import argparse
parser = argparse.ArgumentParser(formatter_class=CustomFormatter)
parser.add_argument(
"--logdir",
type=str,
default=defaults['logdir'],
help="the log directory"
)
parser.add_argument(
"--logname",
type=str,
default=None,
help="the name of the logfile; specify is using the same logfile in append mode"
)
parser.add_argument(
"--logfile",
action="store_true",
default=defaults['logfile'],
help="create a log file"
)
parser.add_argument(
"--no-logfile",
dest="logfile",
action="store_false",
help="will not log on file"
)
parser.add_argument(
"--logscreen",
action="store_true",
default=defaults['logscreen'],
help="display the log on the screen"
)
parser.add_argument(
"--no-logscreen",
dest="logscreen",
action="store_false",
help="will not log on screen"
)
parser.add_argument(
"--overwrite",
action="store_true",
default=defaults['overwrite'],
help="overwrites the outputfile if it exists"
)
parser.add_argument(
'--test',
action='store_true',
help='test run'
)
return parser
def makedir_if_needed(dir):
if dir is not None:
if not os.path.exists(dir):
os.makedirs(dir)
else:
pass
def log_sysargs(logname, args, script=None, date=None,
to_file=True, to_screen=False, filemode='a'):
if date is None:
date = datetime.now()
if Path(logname).suffix != '.log':
log_filename = '{0}_{1}.log'.format(logname,
date.strftime('%Y%m%d-%H%M%S'))
else:
log_filename = logname
if not Path(log_filename).parent.exists():
makedir_if_needed(Path(log_filename).parent)
if not os.path.exists(log_filename):
filemode = 'w'
handlers = []
if to_file:
fh = logging.FileHandler(log_filename, mode=filemode) # new log everytime
formatter = logging.Formatter(
fmt='%(asctime)s %(message)s',
datefmt='%m/%d/%Y %I:%M:%S %p',
)
fh.setFormatter(formatter)
handlers.append(fh)
if to_screen:
sh = logging.StreamHandler()
formatter = logging.Formatter(
fmt='%(asctime)s %(message)s',
datefmt='%m/%d/%Y %I:%M:%S %p',
)
sh.setFormatter(formatter)
handlers.append(sh)
if len(handlers) != 0:
logging.basicConfig(
# filename=log_filename,
# filemode='w', # new log everytime
# format='%(asctime)s %(message)s',
# datefmt='%m/%d/%Y %I:%M:%S %p',
level=logging.INFO,
handlers=handlers,
)
if script is not None:
info_text = '\n\n******SCRIPT PARAMETERS******\n\n'
info_text += script + '\n'
for k, v in vars(args).items():
info_text += str(k) + '\t' + str(v) + '\n'
info_text += '\n*****************************\n'
logging.info(info_text)
def walk(path):
"""
Get all files in subdirectories
"""
for p in Path(path).iterdir():
if p.is_dir():
yield from walk(p)
continue
yield p.resolve()
def get_all_files(path):
for p in walk(Path(path)):
yield p
def glob_regex(pattern, path, sort=True):
path = str(path)
files = [f for f in os.listdir(path) if re.search(pattern, f)]
if sort:
files = sorted(files)
return files
def get_files_in_dir(directory, test=False, seed=1234):
directory = Path(directory)
filelist = sorted(list(directory.iterdir()))
if test:
rng = np.random.default_rng(seed=seed)
filelist = rng.choice(filelist, size=20, replace=False)
filelist = filelist.tolist()
return filelist
def get_pgids_in_dir(targetdir, ext='csv', suffix=None, as_frame=False):
if suffix is None:
search_term = str(targetdir / f'*.{ext}')
else:
search_term = str(targetdir / f'*{suffix}*.{ext}')
pg_ids = [int(Path(i).stem.split('_')[0]) for i in glob(search_term)]
srs = pd.Series(pg_ids).rename('pg_ids')
if as_frame:
srs = srs.to_frame()
return srs
def rename_dict_keys(d, key_mapping):
for orig, new in key_mapping.items():
if orig in d.keys():
d[new] = d.pop(orig)
return d
def try_gz_suffix(filename, as_path=True):
fname = Path(filename)
if not fname.exists():
fname = Path(f'{fname}.gz')
try:
assert fname.exists()
except AssertionError:
raise AssertionError(f'File does not exist: {fname}')
if as_path:
fname = Path(fname)
else:
fname = str(fname)
return fname
def try_no_gz_suffix(filename, as_path=True):
fname = Path(filename)
if not fname.exists():
fname = Path(str(fname).replace('.gz', ''))
try:
assert fname.exists()
except AssertionError:
raise AssertionError(f'File does not exist: {fname}')
if as_path:
fname = Path(fname)
else:
fname = str(fname)
return fname
def get_seed_index(seed, srs, label=None):
"""
Gets the row number of the seed in a seed file read as a pandas Series.
"""
idx = srs[srs == seed].index
if label is not None:
label = 'Index'
else:
label = f'{label} index'
try:
assert len(idx) == 1
except AssertionError:
raise Exception(f'{label} invalid: {idx}')
return idx
class NestedFunctions(object):
"""
Functions with kwargs that are recast as functions of functions.
Useful when used with pd.DataFrame.agg.
"""
@staticmethod
def percentile_fxn(percentile):
def pctile(x):
return np.percentile(x, percentile, axis=0)
pctile.__name__ = f'pctile_{percentile}'
return pctile
def split_arr_into_chunks(arr, chunksize):
"""
Splits an array into chunks of length chunksize; the last chunk
will be the remainder.
"""
return np.split(arr, np.arange(chunksize, len(arr), chunksize))
class BootstrapGroupby(object):
"""Functions necessary for bootstrapping."""
@classmethod
def subsample_df(cls, df, grp_by='pg_id', n=40, seed=42):
"""
Groups the dataframe by `grp_by` and subsamples, without replacement,
`n` entries. In the case of rows corresponding to different seeds, this
ensures that we have the same number of seeds for all `pg_ids`.
"""
size_srs = df.groupby(grp_by).size()
assert df.index.name == grp_by
idx_pass = size_srs[size_srs >= n].index
try:
assert len(idx_pass) > 0
except AssertionError:
raise AssertionError(f'Not enough for subsampling: n={n}\n{df.head}')
# try:
# assert size_srs.min() >= n
# except AssertionError:
# msg = f'Minimum n in series is {size_srs.min()}. Replace n to proceed.'
# raise AssertionError(msg)
subsample_df = df.loc[idx_pass].groupby(grp_by).apply(
lambda grp: grp.sample(n=n, replace=False, random_state=seed)
).droplevel(1)
return subsample_df
@classmethod
def generate_bootstrap_stats(cls, df, B, statistics,
grp_by='pg_id',
fillna=0, seed=None):
"""
Can be applied to a dataframe or to a groupby object. We set fillna=0
by default since we're working with NaN in variances of IMFs (an IMF
that doesn't exist will have an energy of 0)
Parameters
----------
statistics: array-like
list of functions that can be fed into pd.DataFrame.agg
"""
df = df.fillna(fillna) # copy
sq = np.random.SeedSequence(seed)
seedgen = sq.generate_state(B)
stat_res = [None] * B
for boot in range(B):
rng = np.random.default_rng(seedgen[boot])
boot_df = pd.DataFrame(
df.values[rng.choice(df.shape[0], size=df.shape[0], replace=True)],
columns=df.columns,
index=df.index # should all be the same, since this is for a particular pg_id
)
# takes too long
# boot_df = df.sample(frac=1, replace=True, random_state=seedgen[boot])
res = boot_df.agg(statistics)
res.index.name = 'stat'
res['boot_idx'] = boot
stat_res[boot] = res
stat_df = pd.concat(stat_res, axis=0).set_index('boot_idx', append=True).unstack(level=0)
stat_df = stat_df.agg([
np.mean,
np.median,
NestedFunctions.percentile_fxn(99.5),
NestedFunctions.percentile_fxn(97.5),
NestedFunctions.percentile_fxn(2.5),
NestedFunctions.percentile_fxn(0.5),
])
return stat_df
def get_seeds(seed_file=None, start_from_seed=None, end_at_seed=None,
start_idx=None, end_idx=None, seeds=None):
if seed_file is None and seeds is None:
seeds = None
elif seed_file is not None:
seed_file = Path(seed_file)
seeds = pd.read_csv(seed_file, header=None,
squeeze=True)
else:
seeds = seeds
if seeds is not None:
if start_from_seed is not None:
assert start_idx is not None
start_idx = get_seed_index(start_from_seed, seeds)
if end_at_seed is not None:
assert end_idx is not None
end_idx = get_seed_index(end_at_seed, seeds)
if start_idx is not None and end_idx is not None:
seeds = seeds[start_idx:end_idx + 1]
elif start_idx is not None and end_idx is None:
seeds = seeds[start_idx]
elif start_idx is None and end_idx is not None:
seeds = seeds[:end_idx+1]
else:
pass
return seeds
def nullable_string(val: str):
if val.lower() in ['none', '']:
return None
else:
return val
class sdict(dict):
"""
Creates a dictionary where the keys are strings, an which accepts
float and int keys, since it automatically converts them to strings.
This ensures that the dictionary keys are not affected by how
Python treats floats.
"""
def __init__(self, *args, **kw):
super(sdict,self).__init__(*args, **kw)
# self.itemlist = super(odict,self).keys()
def __getitem__(self, key):
if not isinstance(key, str):
key = str(key)
return super().__getitem__(key)
def __setitem__(self, key, value):
if not isinstance(key, str):
key = str(key)
return super().__setitem__(key, value)
def get_window_skip_size_from_str(lbl):
N_w = lbl.split('window=')[-1].split('_')[0]
N_s = lbl.split('skip=')[-1].split('_')[0]
try:
N_w = int(N_w)
except TypeError:
pass
try:
N_s = int(N_s)
except TypeError:
pass
return N_w, N_s