-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfuns_support.py
More file actions
executable file
·169 lines (147 loc) · 5.17 KB
/
Copy pathfuns_support.py
File metadata and controls
executable file
·169 lines (147 loc) · 5.17 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
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
import sys
import os
import itertools
import pickle
import pandas as pd
import numpy as np
import zipfile
from zipfile import ZipFile
import sys
import socket
def vprint(stmt, cond=True):
if cond:
print(stmt)
def no_diff(x, y):
return set(x) == set(y)
def cvec(z):
return np.atleast_2d(z).T
def get_split(x,pat='\\s',k=0,n=5):
return x.str.split(pat,n,True).iloc[:,k]
def t2n(x):
return x.cpu().detach().numpy()
def makeifnot(path):
if not os.path.exists(path):
print('making folder')
os.mkdir(path)
def str_subset(ss, pat, regex=True):
if not isinstance(ss, pd.Series):
ss = pd.Series(ss)
ss = ss[ss.str.contains(pat,regex=regex)]
ss.reset_index(None, drop=True, inplace=True)
return ss
def sigmoid(z):
return 1 / (1 + np.exp(-z))
def intax3(arr):
return np.apply_over_axes(np.sum, arr, 2)
def sumax3(arr):
ss = [arr[:, :, k].sum() for k in range(arr.shape[2])]
return np.array(ss)
def meanax3(arr):
ss = [arr[:, :, k].mean() for k in range(arr.shape[2])]
return np.array(ss)
def quantax3(arr,q=0.5):
ss = [np.quantile(arr[:, :, k],q) for k in range(arr.shape[2])]
return np.array(ss)
def ljoin(x):
return list(itertools.chain.from_iterable(x))
# Function to assign different base folders for different machines
def find_dir_cell():
cpu = socket.gethostname()
if cpu == 'RT5362WL-GGB':
print('On predator machine')
di_dir = {'windows':'D:\\projects\\GICell', 'wsl':'/mnt/d/projects/GICell'}
di_os = {'nt':'windows', 'posix':'wsl'}
dir_cell = di_dir[di_os[os.name]]
elif cpu == 'snowqueen':
print('On snowqueen machine')
dir_cell = '/data/GICell'
elif cpu == 'cavansite':
print('On cavansite')
dir_cell = '/data/erik/GICell'
elif cpu == 'malachite':
print('On malachite')
dir_cell = '/home/erik/projects/GICell'
else:
sys.exit('Where are we?!')
return dir_cell
def find_dir_GI():
cpu = socket.gethostname()
if cpu == 'RT5362WL-GGB':
print('On predator machine')
di_dir = {'windows':'D:\\projects\\GIOrdinal', 'wsl':'/mnt/d/projects/Ordinal'}
di_os = {'nt':'windows', 'posix':'wsl'}
dir_GI = di_dir[di_os[os.name]]
elif cpu == 'snowqueen':
print('On snowqueen machine')
dir_GI = '/data/GIOrdinal'
elif cpu == 'cavansite':
print('On cavansite')
dir_GI = '/data/erik/GIOrdinal'
elif cpu == 'malachite':
print('On malachite')
dir_GI = '/home/erik/projects/GIOrdinal'
else:
sys.exit('Where are we?!')
return dir_GI
# Zip a list of files
def zip_files(lst, fold, zip_fn):
tmp_base = os.getcwd()
os.chdir(fold)
with ZipFile(zip_fn, 'w') as zipMe:
for file in lst:
zipMe.write(file, compress_type=zipfile.ZIP_DEFLATED)
os.chdir(tmp_base)
# ---- CONVERT THE 3 HYPERPARAMETERS INTO HASH ---- #
# NOTE: returns an int
def hash_hp(df, cn):
# df=df_slice;cn=cn_hp
assert df.columns.isin(cn).sum() == len(cn)
assert len(df) == 1
df = df[cn].copy().reset_index(None,drop=True)
df = df.loc[0].reset_index().rename(columns={'index':'hp',0:'val'})
hp_string = pd.Series([df.apply(lambda x: x[0] + '=' + str(x[1]), 1).str.cat(sep='_')])
code_hash = pd.util.hash_pandas_object(hp_string).values[0]
return code_hash
# ---- FUNCTIONS TO READ/WRITE PICKLES ---- #
def read_pickle(path):
assert os.path.exists(path)
with open(path, 'rb') as handle:
di = pickle.load(handle)
return di
def write_pickle(di, path):
with open(path, 'wb') as handle:
pickle.dump(di, handle, protocol=pickle.HIGHEST_PROTOCOL)
# ---- Index come column of df (cn_val) to smallest point (cn_idx) --- #
def idx_first(df, cn_gg, cn_idx, cn_val):
if isinstance(cn_gg, str):
cn_gg = [cn_gg]
assert isinstance(cn_idx, str)
assert isinstance(cn_val, str)
df = df.copy()
cn_val_min = cn_val + '_mi'
idx_min = df.groupby(cn_gg).apply(lambda x: x[cn_idx].idxmin())
idx_min = idx_min.reset_index().rename(columns={0:'idx'})
val_min = df.loc[idx_min.idx,cn_gg + [cn_val]]
val_min.rename(columns={cn_val:cn_val_min}, inplace=True)
df = df.merge(val_min,'left',cn_gg)
df = df.assign(val_idx = lambda x: x[cn_val]/x[cn_val_min]*100).drop(columns=[cn_val_min])
df = df.drop(columns=cn_val).rename(columns={'val_idx':cn_val})
return df
# Function to convert torch array back to numpy array (vmax=255)
def torch2array(xx, vm=255):
d_shape = len(xx.shape)
assert (d_shape == 3) | (d_shape == 4), 'xx is not 3d or 4d'
if d_shape == 4: # from image
arr = xx.cpu().detach().numpy().transpose(2, 3, 1, 0)
arr = (arr * vm).astype(int)
else:
arr = xx.cpu().detach().numpy().transpose(1, 2, 0)
return arr
# Remove columns with "Unnamed" from pandas df
def drop_unnamed(x):
assert isinstance(x, pd.DataFrame)
cn = x.columns
cn_drop = list(cn[cn.str.contains('Unnamed')])
if len(cn_drop) > 0:
x = x.drop(columns=cn_drop)
return x