Skip to content

Commit d392155

Browse files
authored
Merge pull request #29 from sandialabs/25-uqpc-workflow
25 uqpc workflow
2 parents cd9f49b + dc65b92 commit d392155

31 files changed

+1792
-562
lines changed

apps/awkies/getrange.x

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#!/bin/bash
2+
#=====================================================================================
3+
4+
# Given a Nxd matrix of samples, compute dx2 ranges
5+
# Example: getrange.x samples.dat > ranges.dat
6+
7+
shopt -s expand_aliases
8+
alias awk="awk -v OFMT='%.15e'"
9+
10+
if [ $# -lt 1 ]; then
11+
echo "Number of arguments can not be less than 1"
12+
echo "Syntax: $0 <filename> [<cushion_fraction>]"
13+
exit
14+
elif [ $# -gt 2 ]; then
15+
echo "Number of arguments can not be greater than 2"
16+
echo "Syntax: $0 <filename> [<cushion_fraction>]"
17+
exit
18+
elif [ $# -eq 1 ]; then
19+
fr=0.0
20+
elif [ $# -eq 2 ]; then
21+
fr=$2
22+
fi
23+
24+
filename=$1
25+
26+
DIM=`awk 'END{print NF}' $filename`
27+
28+
for (( COL=1; COL<=$DIM ; COL++ )); do
29+
30+
awk 'BEGIN {
31+
valmin=1e+100;
32+
valmax=-1e+100;
33+
line=1;
34+
}
35+
{
36+
if( $(col) < valmin )
37+
{
38+
valmin=$(col);
39+
}
40+
if( $(col) > valmax )
41+
{
42+
valmax=$(col);
43+
}
44+
}
45+
END{
46+
print valmin-fr*(valmax-valmin), valmax+fr*(valmax-valmin)
47+
}' col=$COL $filename
48+
done

apps/awkies/scale.x

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#!/bin/bash -e
2+
# Scales matrix data to or from a given parameter domain to [-1,1]^d
3+
# scale.x <input> <to or from> <domain> <output>
4+
5+
shopt -s expand_aliases
6+
alias awk="awk -v OFMT='%.15e'"
7+
8+
IN_FILE=$1
9+
TO_FROM=$2
10+
DOM_FILE=$3
11+
OUT_FILE=$4
12+
13+
N=`awk 'END{print NR}' $IN_FILE`
14+
D=`awk 'END{print NR}' $DOM_FILE`
15+
16+
DD=`awk 'END{print NF}' $IN_FILE`
17+
18+
## check that DD=D
19+
20+
echo "" > $OUT_FILE
21+
for (( i=1; i<=$D ; i++ )); do
22+
A=`awk 'NR==i{print $1}' i=$i $DOM_FILE`
23+
B=`awk 'NR==i{print $2}' i=$i $DOM_FILE`
24+
25+
if [ "$TO_FROM" = "from" ]; then
26+
awk '{print -1.+2.*($i-a)/(b-a)}' i=$i a=$A b=$B $IN_FILE > tmp
27+
elif [ "$TO_FROM" = "to" ]; then
28+
awk '{print (a+b)/2.+$i*(b-a)/2.}' i=$i a=$A b=$B $IN_FILE > tmp
29+
else
30+
echo "Second argument has to be 'to' or 'from'"
31+
exit
32+
fi
33+
paste $OUT_FILE tmp > tmpp; mv tmpp $OUT_FILE
34+
35+
done

apps/awkies/transpose.x

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#!/bin/bash -e
2+
# Transpose a matrix: assumes all lines have same number
3+
# of fields
4+
5+
# Example: transpose_file.x file_in > file_out
6+
7+
shopt -s expand_aliases
8+
alias awk="awk -v OFMT='%.15e'"
9+
10+
exec awk '
11+
NR == 1 {
12+
n = NF
13+
for (i = 1; i <= NF; i++)
14+
row[i] = $i
15+
next
16+
}
17+
{
18+
if (NF > n)
19+
n = NF
20+
for (i = 1; i <= NF; i++)
21+
row[i] = row[i] " " $i
22+
}
23+
END {
24+
for (i = 1; i <= n; i++)
25+
print row[i]
26+
}' ${1+"$@"}

apps/pc_fit.py

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@
8787
################################################################################
8888

8989

90-
pcrv = pc_fit(x[indtrn], y[indtrn], order=order, pctype=pctype, method=method, eta=1.e-3)
90+
pcrv, lregs = pc_fit(x[indtrn], y[indtrn], order=order, pctype=pctype, method=method, eta=1.e-3)
9191
ypred=pcrv.function(x)
9292

9393
if nout>=1000:
@@ -113,7 +113,7 @@
113113
for isam in range(0, nsam, nevery):
114114
f = plt.figure(figsize=(12,4))
115115
plt.plot(xc, y[isam,:], 'bo-', ms=8, label='Model')
116-
plt.plot(xc, ypred[isam,:], 'go-', ms=8, label='PC apprx.')
116+
plt.plot(xc, ypred[isam,:], 'go-', ms=8, label='PC Apprx.')
117117
plt.title(f'Sample #{isam+1}')
118118
plt.xlabel(xlabel)
119119
plt.ylabel(ylabel)
@@ -128,13 +128,6 @@
128128

129129
plot_sens(mainsens,pars,cases,vis="bar", ncol=5, par_labels=pnames, case_labels=[str(i) for i in cases], lbl_size=25, xticklabel_size=18, legend_size=18, figname='sens_pc.png')
130130

131-
# plot_sens(sensdata, pars, cases,
132-
# vis="bar", reverse=False, topsens=None,
133-
# par_labels=None, case_labels=None, colors=None,
134-
# xlbl='', title='', grid_show=True,
135-
# legend_show=2, legend_size=10, ncol=4,
136-
# lbl_size=22, yoffset=0.1,
137-
# xdatatick=None, xticklabel_size=None, xticklabel_rotation=0,
138-
# figname='sens.png')
139131

140132
savepk(pcrv, 'pcrv')
133+
savepk(lregs, 'lregs')

apps/pc_prep.py

Lines changed: 38 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,45 @@
11
#!/usr/bin/env python
22

3-
################################################################################
4-
# Input PC generation given mvn, samples or marginal pc
5-
################################################################################
6-
7-
8-
9-
# Input
10-
# Usage : pc_prep.py <format> <filename> <input_pcorder>
11-
# e.g. : pc_prep.py marg marg_pc.txt 3
12-
# : pc_prep.py sam inp_sam.txt 3
13-
# : pc_prep.py mvn mean.txt cov.txt
14-
# Output : pcf.txt
15-
16-
173
import os
184
import sys
5+
import argparse
196
import numpy as np
207

218
from pytuq.utils.mindex import get_mi
229
from pytuq.utils.xutils import safe_cholesky
10+
from pytuq.workflows.fits import pc_ros
11+
12+
################################################################################
13+
################################################################################
14+
################################################################################
15+
16+
usage_str = 'Input PC generation given mvn, samples or marginal PC.'
17+
parser = argparse.ArgumentParser(description=usage_str)
18+
parser.add_argument("-f", "--fmt", dest="input_format", type=str, default='sam', help="Input format", choices=['marg', 'sam', 'mvn'])
19+
parser.add_argument("-i", "--inp", dest="filename", type=str, default='xsam.txt', help="Input filename: marginal coefficients (if format is marg), samples (if format is sam), mean (if format is mvn).")
20+
parser.add_argument("-c", "--cov", dest="cov_filename", type=str, default='cov.txt', help="Covariance filename (relevant if format is mvn).")
21+
parser.add_argument("-p", "--pco", dest="pcorder", type=int, default=1, help="PC order (relevant if format is marg or sam).")
22+
parser.add_argument("-t", "--pct", dest="pctype", type=str, default='HG', help="PC type (relevant if format is sam).")
23+
args = parser.parse_args()
24+
25+
################################################################################
26+
################################################################################
27+
################################################################################
2328

24-
input_format=sys.argv[1]
25-
filename=sys.argv[2]
29+
input_format=args.input_format
30+
filename=args.filename
2631

2732
if input_format == "marg" or input_format == "sam":
28-
input_pcorder=int(sys.argv[3])
29-
elif input_format == "mvn":
30-
filename2=sys.argv[3]
33+
pcorder=args.pcorder
34+
if input_format == "mvn":
35+
cov_filename=args.cov_filename
36+
if input_format == "sam":
37+
pctype = args.pctype
38+
3139

40+
################################################################################
41+
################################################################################
42+
################################################################################
3243

3344
if input_format=="marg":
3445

@@ -50,8 +61,8 @@
5061
margpc_all.append(margpc_cur)
5162

5263

53-
assert(input_pcorder >= maxord)
54-
mindex_totalorder=get_mi(input_pcorder, dim)
64+
assert(pcorder >= maxord)
65+
mindex_totalorder=get_mi(pcorder, dim)
5566

5667
mindex=np.zeros((1,dim),dtype=int)
5768
cfs=np.zeros((1,dim))
@@ -81,18 +92,15 @@
8192

8293

8394
elif input_format=="sam":
84-
sam = np.loadtxt(filename)
85-
ros = Rosenblatt(sam)
86-
print("Not implemented yet")
87-
sys.exit()
88-
# TODO: make ex_ros_pc.py a function and use it here
95+
sams = np.loadtxt(filename)
8996

90-
# cmd=uqtkbin+'pce_quad -o '+str(input_pcorder)+' -w HG -f '+filename+ ' > pcq.log; mv PCcoeff.dat pcf.txt'
91-
# os.system(cmd)
97+
nsam, dim = sams.shape
98+
pcrv = pc_ros(sams, pctype=pctype, order=pcorder, nreg=nsam, bwfactor=1.0)
99+
np.savetxt('pcf.txt', np.array(pcrv.coefs).T)
92100

93101
elif input_format=="mvn":
94102
mean = np.loadtxt(filename)
95-
cov = np.loadtxt(filename2)
103+
cov = np.loadtxt(cov_filename)
96104

97105
dim = mean.shape[0]
98106

@@ -106,4 +114,4 @@
106114
np.savetxt('pcf.txt', param_pcf)
107115

108116
else:
109-
print("pc_prep.py : Input format not recognized. Must be marg or sam.")
117+
print("pc_prep.py : Input format not recognized. Must be marg or sam or mvn.")

apps/pc_sam.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,26 @@
22

33
import os
44
import sys
5+
import argparse
56
import numpy as np
67

78
from pytuq.rv.pcrv import PCRV
89
from pytuq.utils.mindex import get_mi
910

1011

11-
pccf_file = sys.argv[1]
12-
pc_type = sys.argv[2]
13-
nsam = int(sys.argv[3])
12+
usage_str = 'Sampling multivariate PC given coefficient file.'
13+
parser = argparse.ArgumentParser(description=usage_str)
14+
parser.add_argument("-f", "--pcf", dest="pcf_file", type=str, default='pcf.txt', help="PC coefficient file: each column is PC coefficient vector for the corresponding dimension.")
15+
parser.add_argument("-t", "--pct", dest="pc_type", type=str, default='HG', help="PC type", choices=['LU', 'HG'])
16+
parser.add_argument("-n", "--nsam", dest="nsam", type=int, default=111, help="Number of requested samples.")
17+
args = parser.parse_args()
1418

15-
pccf = np.loadtxt(pccf_file)
19+
20+
pcf_file = args.pcf_file
21+
pc_type = args.pc_type
22+
nsam = args.nsam
23+
24+
pccf = np.loadtxt(pcf_file)
1625
if len(pccf.shape)==1:
1726
pccf = pccf[:, np.newaxis]
1827

apps/plot_cov.py

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import scipy.stats as ss
88
import matplotlib.pyplot as plt
99

10-
from pytuq.utils.plotting import myrc
10+
from pytuq.utils.plotting import myrc, plot_cov, plot_cov_tri
1111

1212
myrc()
1313

@@ -36,25 +36,14 @@
3636
dim_show = len(ind_show)
3737

3838

39-
f=3.
40-
4139
for ii in range(dim_show):
4240
for jj in range(ii+1,dim_show):
4341
i, j = ind_show[ii], ind_show[jj]
44-
x = np.linspace(mean[i]-f*np.sqrt(cov[i,i]), mean[i]+f*np.sqrt(cov[i,i]), 100)
45-
y = np.linspace(mean[j]-f*np.sqrt(cov[j,j]), mean[j]+f*np.sqrt(cov[j,j]), 100)
46-
X, Y = np.meshgrid(x, y)
47-
48-
try:
49-
rv = ss.multivariate_normal([mean[i], mean[j]], [[cov[i,i], cov[i,j]],[cov[j,i], cov[j,j]]], allow_singular=True)
50-
XY = np.dstack((X, Y))
51-
52-
Z = rv.pdf(XY)
53-
plt.contour(X,Y,Z)
54-
plt.xlabel('p'+str(i+1))
55-
plt.ylabel('p'+str(j+1))
56-
plt.savefig('cov_'+str(i)+'_'+str(j)+'.png')
57-
plt.clf()
58-
59-
except ValueError:
60-
print(f"Covariance for pair ({i},{j}) is not positive-semidefinite.")
42+
43+
mm = np.array([mean[i], mean[j]])
44+
cc = np.array([[cov[i,i], cov[i,j]],[cov[j,i], cov[j,j]]])
45+
plot_cov(mm, cc, f=3., pnames=[f'p{i}', f'p{j}'], ngr=100, savefig=True)
46+
plt.clf()
47+
48+
plot_cov_tri(mean[ind_show], cov[np.ix_(ind_show, ind_show)], names=[f'p{i}' for i in ind_show])
49+

apps/plot_ens.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,28 +5,31 @@
55
import numpy as np
66
import matplotlib.pyplot as plt
77

8+
from pytuq.utils.plotting import myrc
89

10+
myrc()
911

1012
usage_str = 'Script to plot ensemble.'
1113
parser = argparse.ArgumentParser(description=usage_str)
12-
#parser.add_argument('ind_show', type=int, nargs='*',
13-
# help="indices of requested parameters (count from 0)")
14-
1514
parser.add_argument("-y", "--ydata", dest="ydata", type=str, default='ytrain.dat',
1615
help="Ydata file")
1716

1817
args = parser.parse_args()
1918

2019
ydata = np.loadtxt(args.ydata)
20+
if len(ydata.shape)==1:
21+
ydata = ydata[:, np.newaxis]
2122

2223

2324
nsam, nout = ydata.shape
2425

25-
ind_plot=np.arange(nout)
2626

27-
nout_plot=len(ind_plot)
2827

2928
for i in range(nsam):
30-
plt.plot(np.arange(1, nout+1)[ind_plot], ydata[i, ind_plot], 'b-', lw=0.1)
29+
plt.plot(np.arange(1, nout+1), ydata[i, :], 'bo-', markeredgecolor='w', lw=0.5)
3130

31+
plt.xticks(np.arange(1, nout+1))
32+
plt.xtickslabels = [str(i) for i in range(1, nout+1)]
33+
plt.xlabel('Output Id')
34+
plt.ylabel('Output Value')
3235
plt.savefig('ensemble.png')

apps/plot_pcoord.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@
7272
labels=labels[::args.every]
7373

7474
for i in range(ndg):
75-
print("Plotting %d / %d " % (i+1,ndg))
75+
print(f"Plotting parallel coordinates for output {i+1} / {ndg}")
7676
#names=range(1+i*ndcut,min(1+(i+1)*ndcut,ndim+1))
7777

7878
values=xdata[::args.every,i*ndcut:min((i+1)*ndcut, ndim)].T
@@ -90,7 +90,7 @@
9090
parallel_coordinates(pnames_this,
9191
values[:, np.array(labels)==lab],
9292
list(labels_),
93-
'pcoord_'+str(i+1)+'_lab'+lab+'.png')
93+
'pcoord_'+str(i+1)+'_lab_'+lab+'.png')
9494

9595

9696
#labels_only=labels[labels==True]

0 commit comments

Comments
 (0)