-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtGD_saAnalyzer.py
161 lines (157 loc) · 6.42 KB
/
tGD_saAnalyzer.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
import math
from os import sys
from os import path
import numpy as np
import pandas as pd
import compress_pickle as pkl
from SALib.analyze import delta, pawn, rbd_fast, hdmr
import MoNeT_MGDrivE as monet
import matplotlib.pyplot as plt
# import squarify
import tGD_aux as aux
import tGD_gene as drv
from collections import Counter
from more_itertools import locate
if monet.isNotebook():
(USR, DRV, QNT, AOI, THS, MOI) = ('srv', 'tGD', '50', 'HLT', '0.1', 'CPT')
else:
(USR, DRV, QNT, AOI, THS, MOI) = sys.argv[1:]
exp = '100'
###############################################################################
# Setting Paths Up and Reading SA Constants
###############################################################################
(SAMPLES_NUM, VARS_RANGES) = (aux.SA_SAMPLES, aux.SA_RANGES)
(drive, land) = (
drv.driveSelector(DRV, AOI, popSize=aux.POP_SIZE),
[[0], ]
)
(gene, fldr) = (drive.get('gDict'), drive.get('folder'))
(PT_ROT, PT_IMG, PT_DTA, PT_PRE, PT_OUT, PT_MTR) = aux.selectPath(USR, DRV, exp)
PT_ROT = "/".join(PT_ROT.split("/")[:-2])
PT_OUT = path.join(PT_ROT, 'ML')
PT_IMG = path.join(PT_OUT, 'img')
[monet.makeFolder(i) for i in [PT_OUT, PT_IMG]]
PT_SUMS = path.join(PT_ROT, 'SUMMARY')
###############################################################################
# Read SA Files
###############################################################################
# (PROBLEM, SAMPLER, EXP) = (
# pkl.load(path.join(PT_MTR, 'SA_experiment.pkl')),
# np.load(path.join(PT_MTR, 'SA_experiment.npy')),
# pd.read_csv (path.join(PT_MTR, 'SA_experiment.csv'))
# )
###############################################################################
# Read Results CSV
###############################################################################
thsStr = str(int(float(THS)*100))
(fName_I, fName_R, fName_C) = (
'SCA_{}_{}Q_{}T.csv'.format(AOI, QNT, thsStr),
'REG_{}_{}Q_{}T.csv'.format(AOI, QNT, thsStr),
'CLS_{}_{}Q_{}T.csv'.format(AOI, QNT, thsStr)
)
RES = pd.read_csv(path.join(PT_OUT, fName_I))
###############################################################################
# Explore
###############################################################################
headerInd = list(RES.columns)
uqVal = {i: len(list(RES[i].unique())) for i in headerInd}
resNum = RES.shape
###############################################################################
# Read SA Files
###############################################################################
(PROBLEM, SAMPLER, EXP) = (
pkl.load(path.join(PT_MTR, 'SA_experiment.pkl')),
np.load(path.join(PT_MTR, 'SA_experiment.npy')),
pd.read_csv (path.join(PT_MTR, 'SA_experiment.csv'))
)
###############################################################################
# Assemble Output Vector
###############################################################################
headExp = list(RES.columns)
headRes = [i for i in RES.columns if i[0]=='i']
saVars = set([i[0] for i in ([i for i in VARS_RANGES if (len(i[1])>1)])])
saCnst = set([i[0] for i in ([i for i in VARS_RANGES if (len(i[1])<=1)])])
rsnst = set([i.split('_')[-1] for i in headRes]) - set(PROBLEM['names'])
# Generate filter -------------------------------------------------------------
# ix = 70
(FEATS, LABLS) = (
[i for i in headerInd if i[0]=='i'],
[i for i in headerInd if i[0]!='i']
)
FEATS = [i for i in FEATS if uqVal[i]>1]
bounds = [(min(RES[i]), max(RES[i])) for i in FEATS]
problem = {
'num_vars': len(FEATS),
'names': FEATS,
'bounds': bounds,
'dists': ['unif']*len(FEATS),
'sample_scaled': True
}
(X, Y) = (np.asarray(RES[FEATS]), np.asarray(RES[MOI]))
print(problem)
###############################################################################
# Run SA
###############################################################################
SA_delta = delta.analyze(PROBLEM, SAMPLER, Y, print_to_console=False)
SA_pawn = pawn.analyze(PROBLEM, SAMPLER, Y, print_to_console=False)
SA_hdmr = hdmr.analyze(PROBLEM, SAMPLER, Y, print_to_console=False)
SA_fast = rbd_fast.analyze(PROBLEM, SAMPLER, Y, print_to_console=False)
# Compile dataframes ----------------------------------------------------------
pawnDF = pd.DataFrame(SA_pawn)
deltaDF = pd.DataFrame(SA_delta)
hdmrDF = pd.DataFrame({'S1': SA_hdmr['ST'], 'S1_conf': SA_hdmr['ST_conf'], 'names': SA_hdmr['names']})
fastDF = pd.DataFrame(SA_fast)
###############################################################################
# Plot SA
###############################################################################
delta = deltaDF[['names', 'delta', 'S1']]
pawn = pawnDF[['names', 'mean', 'median']]
fast = hdmrDF[['names', 'S1']]
hdmr = fastDF.iloc[:len(aux.SA_RANGES)][['names', 'S1']]
# Re-shape --------------------------------------------------------------------
iVar = [i[0] for i in aux.DATA_HEAD[:-1]]
validFeat = [sal[0] for sal in aux.SA_RANGES if (len(sal[1])>1)]
# Assemble dataframe ----------------------------------------------------------
df = pd.DataFrame([
['Delta', *aux.getSASortedDF(delta, 'S1', validFeat)],
['PAWN', *aux.getSASortedDF(pawn, 'median', validFeat)],
['FAST', *aux.getSASortedDF(fast, 'S1', validFeat)],
['HDMR', *aux.getSASortedDF(hdmr, 'S1', validFeat)],
],
columns=['name']+validFeat
)
dfT = df.transpose()
new_header = dfT.iloc[0]
dfT = dfT[1:]
dfT.columns = new_header
dfT = dfT.reset_index()
dfT.sort_values('Delta', ascending=True, inplace=True)
# Barchart --------------------------------------------------------------------
clr = [
'#FF1A4BAA', '#8338ecAA', '#3a86ffAA', '#00f5d4AA',
'#8d99aeAA', '#cdb4dbAA', '#03045eAA'
]
(fig, ax) = plt.subplots(figsize=(4, 2))
dfT.plot.barh(
x='index', stacked=False, xlim=(0, 1), ax=ax,
ylabel='', xlabel='',
title='', logx=False, color=clr
)
plt.legend(loc='lower right', frameon=False)
plt.savefig(
path.join(PT_IMG, f'SA-{AOI}_{MOI}-{QNT}_qnt.svg'),
format='svg', facecolor='w', bbox_inches='tight',
pad_inches=0.1, dpi=300, transparent=False
)
###############################################################################
# Export to Disk
###############################################################################
outPairs = list(zip(
['Delta', 'PAWN', 'HDMR', 'FAST'],
[deltaDF, pawnDF, hdmrDF, fastDF],
[SA_delta, SA_pawn, SA_hdmr, SA_fast]
))
for (name, df, dct) in outPairs:
fName = path.join(PT_MTR, f'SA-{AOI}_{MOI}-{name}-{QNT}_qnt')
df.to_csv(fName+'.csv', index=False)
pkl.dump(dct, fName+'.pkl')