forked from IDEALLab/IH-GAN_CMAME_2022
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
52 lines (40 loc) · 1.15 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
"""
Utility functions
Author(s): Wei Chen ([email protected])
"""
import os
import itertools
import time
import numpy as np
def convert_sec(sec):
if sec < 60:
return "%.2f sec" % sec
elif sec < (60 * 60):
return "%.2f min" % (sec / 60)
else:
return "%.2f hr" % (sec / (60 * 60))
class ElapsedTimer(object):
def __init__(self):
self.start_time = time.time()
def elapsed_time(self):
return convert_sec(time.time() - self.start_time)
def gen_grid(d, points_per_axis, lb=0., rb=1.):
''' Generate a grid in a d-dimensional space
within the range [lb, rb] for each axis '''
lincoords = []
for i in range(0, d):
lincoords.append(np.linspace(lb, rb, points_per_axis))
coords = list(itertools.product(*lincoords))
return np.array(coords)
def mean_err(metric_list):
n = len(metric_list)
mean = np.mean(metric_list)
std = np.std(metric_list)
err = 1.96*std/n**.5
return mean, err
def safe_remove(filename):
if os.path.exists(filename):
os.remove(filename)
def create_dir(path):
if not os.path.isdir(path):
os.mkdir(path)