-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutils.py
More file actions
139 lines (100 loc) · 3.57 KB
/
utils.py
File metadata and controls
139 lines (100 loc) · 3.57 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
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
import pandas as pd
import numpy as np
import os
from datetime import datetime
from PIL import Image
import torch
import numpy as np
import matplotlib.pyplot as plt
def normalize_to_neg_one_to_one(img):
return img * 2 - 1
def unnormalize_to_zero_to_one(t):
return (t + 1) * 0.5
def numpy_to_pil(images):
if images.ndim == 3:
images = images[None, ...]
images = (images * 255).round().astype("uint8")
pil_images = [Image.fromarray(image) for image in images]
return pil_images
def match_shape(values, broadcast_array, tensor_format="pt"):
values = values.flatten()
while len(values.shape) < len(broadcast_array.shape):
values = values[..., None]
if tensor_format == "pt":
values = values.to(broadcast_array.device)
return values
def clip(tensor, min_value=None, max_value=None):
if isinstance(tensor, np.ndarray):
return np.clip(tensor, min_value, max_value)
elif isinstance(tensor, torch.Tensor):
return torch.clamp(tensor, min_value, max_value)
raise ValueError("Tensor format is not valid is not valid - " \
f"should be numpy array or torch tensor. Got {type(tensor)}.")
def m(v):
nonnull = v[np.isnan(v) == False]
return np.mean(nonnull)
def std(v):
nonnull = v[np.isnan(v) == False]
return np.std(nonnull)
def infill_null(v):
v[np.isnan(v)] = 0
return v
def remove_outliers(lists):
b = np.ones(lists[0].shape)
for l in lists:
q1 = np.nanquantile(l,0.25)
q3 = np.nanquantile(l,0.75)
iqr = q3 - q1
lower = q1 - 1.5*iqr
upper = q3 + 1.5*iqr
b = np.logical_and(b, np.logical_or(l > lower , np.isnan(l)))
b = np.logical_and(b, np.logical_or(l < upper , np.isnan(l)))
return b
def norm(v):
nonnull = v[np.isnan(v) == False]
max = np.nanmax(nonnull)
min = np.nanmin(nonnull)
return 2*((v - min) / (max - min))-1
def get_local_gaussian(ys, numbins=50):
max = np.nanmax(ys)
min = np.nanmin(ys)
bins = np.linspace(min, max, retstep=numbins)[0]
s_ys = np.array(sorted(ys, reverse=True))
d, m, s = [], [], []
for i in range(numbins-1):
low = bins[i]
high = bins[i+1]
tbool = np.logical_and(low<=s_ys, s_ys<=high)
data = s_ys[tbool]
d.append(len(data))
m.append(data.mean() if not np.isnan(data.mean()) else low)
s.append(data.std() if not np.isnan(data.std()) else 0.01)
d = np.array(d) / sum(d)
return d, np.array(m), np.array(s)
def sample_local_gaussian(v, noise_type=None, numbins=50):
d,m,s = get_local_gaussian(v, numbins=numbins)
num = sum(np.isnan(v))
samples = np.random.choice(numbins-1, num, p=d)
rand_n = np.random.randn(num)
adjust = m[samples] + 1.2 * rand_n *s[samples]
# override
#adjust = np.zeros(adjust.shape)
if noise_type == "zeros":
adjust = np.zeros(adjust.shape)
if noise_type == "uniform":
adjust = np.random.uniform(low=-1.0, high=1.0, size=adjust.shape)
if noise_type == "gaussian":
adjust = np.random.normal(size=adjust.shape)
v[np.isnan(v)] = adjust
return v, (d,m,s)
def sample_noise(b, dmss, noise_type=None, numbins=50):
if noise_type == "uniform":
return np.random.uniform(low=-1.0, high=1.0, size=(b,12))
if noise_type == "gaussian":
return np.random.normal(size=(b,12))
vs = []
for d,m,s in dmss:
samples = np.random.choice(numbins-1, b, p=d)
rand_n = np.random.randn(b)
vs.append(m[samples] + 1.2 * rand_n *s[samples])
return np.stack(vs, axis=-1)