-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.py
More file actions
50 lines (38 loc) · 1.39 KB
/
Copy pathutil.py
File metadata and controls
50 lines (38 loc) · 1.39 KB
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
"""
Utility functions module.
"""
import logging
import numpy as np
logging.basicConfig(level=logging.INFO)
# formatter = logging.Formatter('\033[1A\x1b[2K%(message)s')
ROUND_DECIMALS = 3
def periodic_logging(n, msg=None, v=25000):
"""Log number message if n is divisible by v. Default msg value is n."""
# TODO: overwrite previous info line, and lower default v.
v = max(v, 1)
if not msg:
msg = n
if n % v == 0:
logging.info(msg)
return
def rd(n, d=ROUND_DECIMALS):
"""Round n to the nearest d decimal points."""
return np.round(n, d).item()
def std_to_std_of_mean(std_array, weights=None):
"""
Given an array of the standard deviations of some quantities, what is the standard deviation of the quantities'
(possibly weighted) average.
"""
if weights is None:
weights = np.ones(len(std_array))
weights = weights / np.sum(weights)
return np.sqrt(np.sum(np.square(np.multiply(std_array, weights))))
def std_to_std_of_sum(std_array, mean_array, counts_array):
"""
Given an array of the standard deviations of some quantities, what is the standard deviation of the quantities' sum.
"""
# std = np.sqrt(s2 / s0 - (s1 / s0) ** 2)
s2 = ((std_array ** 2 + mean_array ** 2) * counts_array).sum()
s1 = (mean_array * counts_array).sum()
s0 = counts_array.sum()
return np.sqrt(s2 / s0 - (s1 / s0) ** 2)