-
Notifications
You must be signed in to change notification settings - Fork 395
/
Copy pathutils.py
54 lines (44 loc) · 1.44 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
"""General use functions.
"""
import time
import argparse
import numpy as np
from scipy.optimize import nnls
################################################################################
def sync(i, start_time, timestep):
"""Syncs the stepped simulation with the wall-clock.
Function `sync` calls time.sleep() to pause a for-loop
running faster than the expected timestep.
Parameters
----------
i : int
Current simulation iteration.
start_time : timestamp
Timestamp of the simulation start.
timestep : float
Desired, wall-clock step of the simulation's rendering.
"""
if timestep > .04 or i%(int(1/(24*timestep))) == 0:
elapsed = time.time() - start_time
if elapsed < (i*timestep):
time.sleep(timestep*i - elapsed)
################################################################################
def str2bool(val):
"""Converts a string into a boolean.
Parameters
----------
val : str | bool
Input value (possibly string) to interpret as boolean.
Returns
-------
bool
Interpretation of `val` as True or False.
"""
if isinstance(val, bool):
return val
elif val.lower() in ('yes', 'true', 't', 'y', '1'):
return True
elif val.lower() in ('no', 'false', 'f', 'n', '0'):
return False
else:
raise argparse.ArgumentTypeError("[ERROR] in str2bool(), a Boolean value is expected")