|
1 | 1 | #!/usr/bin/env python |
2 | 2 |
|
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 | | - |
17 | 3 | import os |
18 | 4 | import sys |
| 5 | +import argparse |
19 | 6 | import numpy as np |
20 | 7 |
|
21 | 8 | from pytuq.utils.mindex import get_mi |
22 | 9 | 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 | +################################################################################ |
23 | 28 |
|
24 | | -input_format=sys.argv[1] |
25 | | -filename=sys.argv[2] |
| 29 | +input_format=args.input_format |
| 30 | +filename=args.filename |
26 | 31 |
|
27 | 32 | 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 | + |
31 | 39 |
|
| 40 | +################################################################################ |
| 41 | +################################################################################ |
| 42 | +################################################################################ |
32 | 43 |
|
33 | 44 | if input_format=="marg": |
34 | 45 |
|
|
50 | 61 | margpc_all.append(margpc_cur) |
51 | 62 |
|
52 | 63 |
|
53 | | - assert(input_pcorder >= maxord) |
54 | | - mindex_totalorder=get_mi(input_pcorder, dim) |
| 64 | + assert(pcorder >= maxord) |
| 65 | + mindex_totalorder=get_mi(pcorder, dim) |
55 | 66 |
|
56 | 67 | mindex=np.zeros((1,dim),dtype=int) |
57 | 68 | cfs=np.zeros((1,dim)) |
|
81 | 92 |
|
82 | 93 |
|
83 | 94 | 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) |
89 | 96 |
|
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) |
92 | 100 |
|
93 | 101 | elif input_format=="mvn": |
94 | 102 | mean = np.loadtxt(filename) |
95 | | - cov = np.loadtxt(filename2) |
| 103 | + cov = np.loadtxt(cov_filename) |
96 | 104 |
|
97 | 105 | dim = mean.shape[0] |
98 | 106 |
|
|
106 | 114 | np.savetxt('pcf.txt', param_pcf) |
107 | 115 |
|
108 | 116 | 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.") |
0 commit comments