forked from RunzheStat/D2OPE
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_util.py
304 lines (243 loc) · 8.74 KB
/
_util.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
###################### GPU Manager ######################
# gpus = tf.config.experimental.list_physical_devices('GPU')
# if gpus:
# try:
# for gpu in gpus:
# tf.config.experimental.set_memory_growth(gpu, True)
# except RuntimeError as e:
# print(e)
######################
# !for i in $(sudo lsof /dev/nvidia2 | grep python | awk '{print $2}' | sort -u); do sudo kill -9 $i; done
##################################################################
# !pip install ray gym silence_tensorflow livelossplot tensorflow_probability tf_agents
# conda
# do not try the below first
# !pip install tensorflow tensorflow-gpu --upgrade
# %matplotlib inline
# !tar -czf data.tar.gz data
# !tar -czf code.tar.gz code
# !autopep8 --in-place --aggressive --aggressive brac_dual_agent.py
# instance.method = MethodType(method, instance)
#############################################################################
from inspect import getsource
from importlib import reload
from livelossplot import PlotLosses
import pytz
import tensorflow as tf
import silence_tensorflow.auto
# tz_NY = pytz.timezone('America/New_York')
# dt.now(tz_NY).strftime("%D:%H:%M:%S")
from typing import Dict, List, Set, Tuple
from datetime import datetime as dt
import itertools
import io
import sys
import gym
import ray
import warnings
warnings.simplefilter("ignore")
# from sagemaker import get_execution_role
# role = get_execution_role()
from IPython.display import clear_output
from tqdm import tqdm
# https://github.com/tqdm/tqdm
# pbar = tqdm(["a", "b", "c", "d"])
# for char in pbar:
# time.sleep(0.25)
# pbar.set_description("Processing %s" % char)
# for i in tqdm(range(10)):
from types import MethodType
import functools
from functools import reduce
import os
#############################################################################
# Packages
import scipy as sp
import pandas as pd
from pandas import DataFrame as DF
# import statsmodels.api as sm # !pip install statsmodels
from matplotlib.pyplot import hist
import pickle
from scipy.stats import truncnorm
import matplotlib.pyplot as plt
####################################
# Random
import random
from random import seed as rseed
from numpy.random import seed as npseed
from numpy import absolute as np_abs
from numpy.random import normal as rnorm
from numpy.random import uniform as runi
from numpy.random import binomial as rbin
from numpy.random import poisson as rpoisson
from numpy.random import shuffle,randn, permutation # randn(d1,d2) is d1*d2 i.i.d N(0,1)
from numpy import squeeze
from numpy.linalg import solve
####################################
# Numpy
import numpy as np
from numpy import mean, var, std, median
from numpy import array as arr
from numpy import sqrt, log, cos, sin, exp, dot, diag, ones, identity, zeros, roll, multiply, stack, concatenate, transpose
from numpy import concatenate as v_add
from numpy.linalg import norm, inv
from numpy import apply_along_axis as apply
from numpy.random import multinomial, choice
####################################
# sklearn
import sklearn as sk
from sklearn import preprocessing as pre
from sklearn.ensemble import RandomForestRegressor
from sklearn.metrics import mean_squared_error as mse
from sklearn.metrics import r2_score
from sklearn.linear_model import LinearRegression as lm
from sklearn.model_selection import KFold
from sklearn.model_selection import GridSearchCV
from scipy.special import softmax
#############################################################################
os.environ["OMP_NUM_THREADS"] = "1"
os.environ["OPENBLAS_NUM_THREADS"] = "1"
np.set_printoptions(precision = 4)
#############################################################################
import time
now = time.time
import smtplib, ssl
import datetime, pytz
def EST():
return datetime.datetime.now().astimezone(pytz.timezone('US/Eastern')).strftime("%H:%M, %m/%d")
#############################################################################
dash = "--------------------------------------"
DASH = "\n" + "--------------------------------------" + "\n"
Dash = "\n" + dash
dasH = dash + "\n"
#############################################################################
def unzip(path, zip_type = "tar_gz"):
if zip_type == "tar_gz":
import tarfile
tar = tarfile.open(path, "r:gz")
tar.extractall()
tar.close()
elif zip_type == "zip":
from zipfile import ZipFile
with ZipFile(path, 'r') as zipObj:
# Extract all the contents of zip file in current directory
zipObj.extractall()
#############################################################################
#%% utility funs
from multiprocessing import Pool
import multiprocessing
n_cores = multiprocessing.cpu_count()
def mute():
sys.stdout = open(os.devnull, 'w')
def fun(f, q_in, q_out):
while True:
i, x = q_in.get()
if i is None:
break
q_out.put((i, f(x)))
def parmap(f, X, nprocs = multiprocessing.cpu_count(), **args):#-2
q_in = multiprocessing.Queue(1)
q_out = multiprocessing.Queue()
def g(x):
return f(x, **args)
proc = [multiprocessing.Process(target=fun, args=(g, q_in, q_out))
for _ in range(nprocs)]
for p in proc:
p.daemon = True
p.start()
sent = [q_in.put((i, x)) for i, x in enumerate(X)]
[q_in.put((None, None)) for _ in range(nprocs)]
res = [q_out.get() for _ in range(len(sent))]
[p.join() for p in proc]
return [x for i, x in sorted(res)]
def setminus(A, B):
return [item for item in A if item not in B]
def listinlist2list(theList):
return [item for sublist in theList for item in sublist]
def if_exist(obj):
return obj in locals() or obj in globals()
def getSize(one_object):
print(one_object.memory_usage().sum() / 1024 ** 2, "MB")
# print(sys.getsizeof(one_object) // 1024, "MB")
def dump(file, path):
pickle.dump(file, open(path, "wb"))
def load(path):
return pickle.load(open(path, "rb"))
def get_MB(a):
MB = sys.getsizeof(a) / 1024 / 1024
return MB
def hstack_all_comb(array1, array2):
# array1 is on the left and also changes faster
res = np.hstack([
np.tile(array1, (array2.shape[0], 1))
, np.repeat(array2, array1.shape[0], axis=0)]
)
return res
def quantile(a, p):
r = [a[0] for a in DF(a).quantile(p).values]
return np.round(r, 3)
def flatten(l):
# list of sublist -> list
return [item for sublist in l for item in sublist]
def change_rate(old_targets, new_targets, numpy = False):
if numpy:
diff = np.mean(abs(new_targets-old_targets)) / (np.mean(abs(old_targets))+1e-6)
else:
diff = abs(new_targets-old_targets).mean() / (abs(old_targets).mean()+1e-6)
return min(1.0, diff)
#############################################################################
def print_all(dat, column_only = True):
if column_only:
with pd.option_context('display.max_columns', None): # more options can be specified also
print(dat)
else:
with pd.option_context('display.max_rows', None, 'display.max_columns', None): # more options can be specified also
print(dat)
def quantile(a):
return np.percentile(a, range(0,110,10))
#############################################################################
# !pip install termcolor
from termcolor import colored, cprint
# https://pypi.org/project/termcolor/#description
def printR(theStr):
print(colored(theStr, 'red'))
def printG(theStr):
print(colored(theStr, 'green'))
def printB(theStr):
print(colored(theStr, 'blue'))
def sets_intersection(d):
return list(reduce(set.intersection, [set(item) for item in d ]))
def select_each_row(array, idx):
return np.take_along_axis(array, idx[:,None], axis=1)
def subtract_each_column(mat, col):
return (mat.transpose() - col).transpose()
def sample_split(L, N):
""" replay buffer?
"""
kf = KFold(n_splits=L)
kf.get_n_splits(zeros(N))
split_ind = {}
k = 0
for i, j in kf.split(range(N)):
split_ind[k] = {"train_ind" : i, "test_ind" : j}
k += 1
return split_ind
def row_repeat(mat, rep, full_block = False):
if full_block:
return np.tile(mat, (rep, 1))
else:
return np.repeat(mat, rep, axis=0)
def multiplyList(myList) :
# Multiply elements one by one
result = 1
for x in myList:
result = result * x
return result
def smooth_loss(loss, freq):
loss = np.array(loss).copy()
return np.mean(loss.reshape(-1, freq), axis = -1)
############################################################
def SARS2traj(SARS, S_dim = 3):
states = arr([sars[0][:S_dim] for sars in SARS])
actions = arr([sars[1] for sars in SARS])
return states, actions