|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# -*- coding: utf-8; -*- |
| 3 | + |
| 4 | +# Eppes command line routine |
| 5 | +# Reads files defined in a conf file given as input argument with -c (default name is eppes.cfg). |
| 6 | +# Overwrites mufile, sigfile, wfile and nfile on output. |
| 7 | + |
| 8 | +import numpy as np |
| 9 | +import eppes |
| 10 | +# from eppes import config |
| 11 | + |
| 12 | +def main(*args): |
| 13 | + |
| 14 | + # Read configuration |
| 15 | + opts = eppes.config.parseargs(*args[1:]) |
| 16 | + |
| 17 | + # define bounds |
| 18 | + if len(opts.boundsfile)>0: |
| 19 | + bounds = np.loadtxt(opts.boundsfile) |
| 20 | + else: |
| 21 | + bounds = None |
| 22 | + |
| 23 | + # load mu and sig from file |
| 24 | + mu = np.loadtxt(opts.mufile) |
| 25 | + sig = np.loadtxt(opts.sigfile) |
| 26 | + |
| 27 | + if (mu.shape[0] != sig.shape[0]) | (mu.shape[0] != sig.shape[1]) : |
| 28 | + print('mu and sig shapes do not match') |
| 29 | + return 1 |
| 30 | + |
| 31 | + # sample only, no update |
| 32 | + if opts.sampleonly == 1: |
| 33 | + newsample = eppes.propose(opts.nsample,mu,sig,bounds=bounds) |
| 34 | + if opts.lognor: |
| 35 | + newsample = np.exp(newsample) |
| 36 | + if opts.verbosity: |
| 37 | + print(mu) |
| 38 | + print(sig) |
| 39 | + print(newsample) |
| 40 | + np.savetxt(opts.sampleout,newsample) |
| 41 | + return 0 |
| 42 | + |
| 43 | + # load W and N, current sample and the scores |
| 44 | + w = np.loadtxt(opts.wfile) |
| 45 | + n = np.loadtxt(opts.nfile) |
| 46 | + oldsample = np.loadtxt(opts.samplein) |
| 47 | + scores = np.loadtxt(opts.scorefile) |
| 48 | + nsample = oldsample.shape[0] |
| 49 | + if opts.lognor: |
| 50 | + oldsample = np.log(oldsample) |
| 51 | + |
| 52 | + |
| 53 | + eppes.tomatrix(scores) |
| 54 | + if scores.shape[0] != nsample: |
| 55 | + print('scores and sample do not match') |
| 56 | + return 1 |
| 57 | + # turn scores into ranks |
| 58 | + if opts.useranks: |
| 59 | + scores = eppes.rankscores(scores) |
| 60 | + if opts.verbosity: |
| 61 | + print(scores) |
| 62 | + |
| 63 | + if np.size(np.shape(scores)) > 1: |
| 64 | + if np.shape(scores)[1] > 1: |
| 65 | + scores = eppes.combinescores(scores,opts.combine_method) |
| 66 | + eppes.tomatrix(scores) |
| 67 | + if opts.verbosity: |
| 68 | + print(scores) |
| 69 | + |
| 70 | + # resample and update parameters mu,w,sig,n |
| 71 | + theta,wout = eppes.logresample(oldsample,scores) |
| 72 | + mu,w,sig,n = eppes.eppesupdate(theta,mu,w,sig,n, maxsteprel=opts.maxsteprel) |
| 73 | + |
| 74 | + # optionally, fix N and W |
| 75 | + if opts.maxn>0: |
| 76 | + n = min(n,opts.maxn) |
| 77 | + if len(opts.w00file)>0: |
| 78 | + w00 = np.loadtxt(opts.w00file) |
| 79 | + if w.shape != sig.shape: |
| 80 | + print('error in w00 shape, not using it') |
| 81 | + else: |
| 82 | + w = w + w00 |
| 83 | + |
| 84 | + # generate new sample, based on updated mu and sig |
| 85 | + newsample = eppes.propose(opts.nsample,mu,sig,bounds=bounds) |
| 86 | + |
| 87 | + if opts.lognor: |
| 88 | + newsample = np.exp(newsample) |
| 89 | + |
| 90 | + if opts.verbosity: |
| 91 | + print(mu) |
| 92 | + print(sig) |
| 93 | + print(newsample) |
| 94 | + print(n) |
| 95 | + |
| 96 | + # save all back to files (they are over written!) |
| 97 | + np.savetxt(opts.sampleout,newsample) |
| 98 | + np.savetxt(opts.mufile,mu) |
| 99 | + np.savetxt(opts.sigfile,sig) |
| 100 | + np.savetxt(opts.wfile,w) |
| 101 | + np.savetxt(opts.nfile,np.array([n])) |
| 102 | + if len(opts.winfofile)>0: |
| 103 | + np.savetxt(opts.winfofile,wout) |
| 104 | + return 0 |
| 105 | + |
| 106 | + |
| 107 | +if __name__ == "__main__": |
| 108 | + import sys |
| 109 | + status = main(*sys.argv) |
| 110 | + # need some nice way to pass failure to the caller |
| 111 | + if status: |
| 112 | + sys.exit("Some error happened during the call, status="+str(status)) |
0 commit comments