def _uhat(self, results): nc = len(self.grid[:, 0]) ndim = len(self.params) level = self.level x = self.grid weight = x[:, ndim] weight = weight / np.sum(weight) x = x[:, 0:ndim] nterm = nelms(self.level, ndim) jf = np.zeros((nc, nterm)) h2 = jacobi_e2_nd(ndim, self.level, 0, 0) for i in range(0, nc): jf[i, :] = legendre_nd(x[i, :], ndim, level) # calculate uhat uhat = np.zeros((nterm)) for i in range(0, nterm): uhat[i] = np.sum(results.T * jf[:, i] * weight) / h2[i] poly = self.polyrs(uhat) # collect parameters and results in one array pgrid = self.grid.copy() for i, p in enumerate(self.params): pmin, pmax = p.pdf.srange pgrid[:, i] *= (pmax - pmin) / 2.0 pgrid[:, i] += (pmax + pmin) / 2.0 actual_data = np.column_stack((pgrid[:, :-1], results)) rs = ResponseFunc(poly, params=self.params, data=actual_data) vprint(1, "\tSurface = %s" % rs.eqn) srmse, srmsep = rs.rmse() vprint(1, "\tRMSE = %.2e (%.2e %%)" % (srmse, srmsep)) return rs
def start(*args): usage = """Usage: puq start [-f file.hdf5] script.py [args]. where 'script' is a PUQ control script and 'args' are optional arguments passed to run() in the control script.""" parser = OptionParser(usage) parser.add_option("-f", type='string', help="Output filename. '.hdf5' will be appended if necessary. Default is 'sweep_' followed by a timestamp.") (opt, ar) = parser.parse_args(args=list(args)) if len(ar) == 0: parser.print_help() sys.exit(-1) scriptname = ar[0] vprint(2, "Start %s" % scriptname) module = os.path.splitext(os.path.split(scriptname)[1])[0] sys.path = [os.getcwd()] + sys.path try: _temp = __import__(module, globals(), locals(), [], 0) except ImportError: exception("Error importing '%s'" % scriptname) return sw = _temp.run(*ar[1:]) if not scriptname.endswith(".py"): scriptname += ".py" sw.input_script = os.path.abspath(scriptname) res = sw.run(opt.f) if res: sw.analyze() return res
def extend(*args): debug(args) usage = "Usage: sweep extend [--num num] hdf5_filename." parser = OptionParser(usage) parser.add_option("--num", type='int', default=0) (opt, ar) = parser.parse_args(args=list(args)) sweep = load_internal(ar) cname = sweep.psweep.__class__.__name__ vprint(1, "Extending %s.hdf5 using %s" % (sweep.fname, cname)) if cname == 'MonteCarlo': if opt.num <= 0: print("Monte Carlo extend requires a valid num argument.") print(usage) return sweep.psweep.extend(opt.num) else: sweep.psweep.extend() return sweep.run()
def extend(*args): debug(args) usage = "Usage: sweep extend [--num num] hdf5_filename." parser = OptionParser(usage) parser.add_option("--num", type='int', default=0) (opt, ar) = parser.parse_args(args=list(args)) sweep = load_internal(ar) cname = sweep.psweep.__class__.__name__ vprint(1, "Extending %s.hdf5 using %s" % (sweep.fname, cname)) if cname == 'MonteCarlo': if opt.num <= 0: print "Monte Carlo extend requires a valid num argument." print usage return sweep.psweep.extend(opt.num) else: sweep.psweep.extend() return sweep.run()
def run(self, fn=None, overwrite=False, dryrun=False): #FR """ Calls PSweep.run() to run all the jobs in the Sweep. Collect the data from the outputs and call the PSweep analyze method. If the PSweep method has an iterative callback defined, call it, otherwise return. Args: fn(string): HDF5 filename for output. '.hdf5' will be appended to the filename if necessary. If fn is None, a filename will be generated starting with "sweep\_" followed by a timestamp. overwrite(boolean): If True and fn is not None, will silently overwrite any previous files of the same name. Returns: True on success. """ if fn is not None: self.fname = os.path.splitext(fn)[0] fn = self.fname + '.hdf5' if os.path.exists(fn): if not overwrite: done = False while 1: ans = raw_input('%s already exists. Replace (Y/N):' % fn) try: if ans.upper() == 'N': done = True break elif ans.upper() == 'Y': break except: pass print "Please answer with 'Y' or 'N'\n" if done: sys.exit(-1) os.remove(fn) vprint(1, 'Saving run to %s.hdf5' % self.fname) return self.psweep.run(self, dryrun) #FR
def run(self, fn=None, overwrite=False): """ Calls PSweep.run() to run all the jobs in the Sweep. Collect the data from the outputs and call the PSweep analyze method. If the PSweep method has an iterative callback defined, call it, otherwise return. Args: fn(string): HDF5 filename for output. '.hdf5' will be appended to the filename if necessary. If fn is None, a filename will be generated starting with "sweep\_" followed by a timestamp. overwrite(boolean): If True and fn is not None, will silently overwrite any previous files of the same name. Returns: True on success. """ if fn is not None: self.fname = os.path.splitext(fn)[0] fn = self.fname + '.hdf5' if os.path.exists(fn): if not overwrite: done = False while 1: ans = raw_input('%s already exists. Replace (Y/N):' % fn) try: if ans.upper() == 'N': done = True break elif ans.upper() == 'Y': break except: pass print "Please answer with 'Y' or 'N'\n" if done: sys.exit(-1) os.remove(fn) vprint(1, 'Saving run to %s.hdf5' % self.fname) return self.psweep.run(self)
def extend(self, num=None): self.level += 1 vprint(1, "Extending Smolyak to level %d" % self.level) oldsize = self.grid.shape[0] self._calculate_params() self.pgrid = self.pgrid[oldsize:]
def sensitivity(self, data): """ Elementary Effects Screening see http://en.wikipedia.org/wiki/Elementary_effects_method """ vprint(1, "\nSENSITIVITY:") ee = {} # strip out weight column and rescale to [0,1] grid = (self.grid[:, :-1] + 1) / 2 vgrid = np.column_stack((grid, data)) numcols = grid.shape[1] # Each parameter's value is in a column, so for each # column, create a new grid without that column, then look for # duplicate rows. for coln in range(0, numcols): ee[coln] = [] newgrid = grid[:, [x for x in range(0, numcols) if x != coln]] # (alternative) newgrid = np.delete(grid, coln, axis=1) rowlist = defaultdict(list) for n, row in enumerate(newgrid): rowlist[tuple(row)].append(n) for r in list(rowlist.keys()): if len(rowlist[r]) < 2: del rowlist[r] # For each list of duplicate rows, create an array with all the # parameter values and the output value. Iterate through it to for r in rowlist: rdata = None for rr in rowlist[r]: if rdata is None: rdata = vgrid[rr] else: rdata = np.vstack((rdata, vgrid[rr])) rdata = rdata[rdata[:, coln].argsort()] Y = None for d in rdata: if Y is not None: ee[coln] = np.append(ee[coln], (d[-1] - Y) / (d[coln] - X)) #print (d[-1] - Y) / (d[coln] - X) Y = d[-1] X = d[coln] max_name_len = max(map(len, [p.name for p in self.params])) sens = {} for n, p in enumerate(self.params): std = np.std(ee[n]) ustar = np.mean(np.abs(ee[n])) sens[p.name] = {'std': std, 'ustar': ustar} #p.sensitivity_ustar = ustar #p.sensitivity_dev = std sorted_list = sorted(list(sens.items()), key=lambda a: a[1]['ustar'], reverse=True) vprint(1, "Var%s u* dev" % (' ' * (max_name_len))) vprint(1, '-' * (28 + max_name_len)) for item in sorted_list: pad = ' ' * (max_name_len - len(item[0])) vprint( 1, "%s%s %.4e %.4e" % (pad, item[0], item[1]['ustar'], item[1]['std'])) return pickle(sorted_list)
def sensitivity(self, data): """ Elementary Effects Screening see http://en.wikipedia.org/wiki/Elementary_effects_method """ vprint(1, "\nSENSITIVITY:") ee = {} # strip out weight column and rescale to [0,1] grid = (self.grid[:, :-1] + 1) / 2 vgrid = np.column_stack((grid, data)) numcols = grid.shape[1] # Each parameter's value is in a column, so for each # column, create a new grid without that column, then look for # duplicate rows. for coln in range(0, numcols): ee[coln] = [] newgrid = grid[:, [x for x in range(0, numcols) if x != coln]] # (alternative) newgrid = np.delete(grid, coln, axis=1) rowlist = defaultdict(list) for n, row in enumerate(newgrid): rowlist[tuple(row)].append(n) for r in rowlist.keys(): if len(rowlist[r]) < 2: del rowlist[r] # For each list of duplicate rows, create an array with all the # parameter values and the output value. Iterate through it to for r in rowlist: rdata = None for rr in rowlist[r]: if rdata is None: rdata = vgrid[rr] else: rdata = np.vstack((rdata, vgrid[rr])) rdata = rdata[rdata[:, coln].argsort()] Y = None for d in rdata: if Y is not None: ee[coln] = np.append(ee[coln], (d[-1] - Y) / (d[coln] - X)) #print (d[-1] - Y) / (d[coln] - X) Y = d[-1] X = d[coln] max_name_len = max(map(len, [p.name for p in self.params])) sens = {} for n, p in enumerate(self.params): std = np.std(ee[n]) ustar = np.mean(np.abs(ee[n])) sens[p.name] = {'std': std, 'ustar': ustar} #p.sensitivity_ustar = ustar #p.sensitivity_dev = std sorted_list = sorted(sens.items(), lambda x, y: cmp(y[1]['ustar'], x[1]['ustar'])) vprint(1, "Var%s u* dev" % (' '*(max_name_len))) vprint(1, '-'*(28+max_name_len)) for item in sorted_list: pad = ' '*(max_name_len - len(item[0])) vprint(1, "%s%s %.4e %.4e" % (pad, item[0], item[1]['ustar'], item[1]['std'])) return pickle(sorted_list)