Exemple #1
0
    def pdf(self, fit=True, params=[], force=False, min=None, max=None,
            return_samples=False, psamples=None,numsamples=None):


        if not self.params and not params:
            raise ValueError("Cannot generate PDF for response function without params.")

        if params:
            saved_params = self.params
            saved_vars = self.vars

            for newp in params:
                for i, p in enumerate(self.params):
                    if p.name == newp.name:
                        print "REPLACING\n\t%s\nWITH\n\t%s\n" % (p, newp)
                        if not force:
                            self.check_pdf_range(p, newp)
                        self.params[i] = newp
            self.vars = self.params2vars(self.params)

        # get parameter pdf samples
        if psamples is None:
            xseed = get_psamples(self.params,num=numsamples) #FR                
        else:
            xseed = psamples

        results = np.array(self.evala(xseed))

        if min is None or max is None:
            calc_min, calc_max = self.minmax()

        if min is None:
            if np.min(results) < calc_min:
                min = np.min(results)
            else:
                min = calc_min

        if max is None:
            if np.max(results) > calc_max:
                max = np.max(results)
            else:
                max = calc_max

        if params:
            self.params = saved_params
            self.vars = saved_vars

        if return_samples:
            return ExperimentalPDF(results, fit=fit, min=min, max=max, force=force), results
        else:
            return ExperimentalPDF(results, fit=fit, min=min, max=max, force=force)
Exemple #2
0
    def pdf(self, fit=True, params=[], force=False, min=None, max=None,
            return_samples=False, psamples=None):

        if not self.params and not params:
            raise ValueError("Cannot generate PDF for response function without params.")

        if params:
            saved_params = self.params
            saved_vars = self.vars

            for newp in params:
                for i, p in enumerate(self.params):
                    if p.name == newp.name:
                        print("REPLACING\n\t%s\nWITH\n\t%s\n" % (p, newp))
                        if not force:
                            self.check_pdf_range(p, newp)
                        self.params[i] = newp
            self.vars = self.params2vars(self.params)

        # get parameter pdf samples
        if psamples is None:
            xseed = get_psamples(self.params)
        else:
            xseed = psamples

        results = self.evala(xseed)

        # If the response surface is constant, 'results' is a constant.
        # We will need to return an appropriate array filled with it.
        if type(results) != np.ndarray:
            val = results
            results = np.empty(xseed.shape[0])
            results.fill(val)

        if min is None:
            min = np.min(results)

        if max is None:
            max = np.max(results)

        if params:
            self.params = saved_params
            self.vars = saved_vars

        if return_samples:
            return ExperimentalPDF(results, fit=fit, min=min, max=max, force=force), results
        else:
            return ExperimentalPDF(results, fit=fit, min=min, max=max, force=force)
Exemple #3
0
class CustomParameter(Parameter):
    '''
    Class implementing a Parameter with a Custom distribution.

    Args:
      name: Name of the parameter. This should be a short name,
        like a variable.
      description:  A longer description of the parameter.
      kwargs: Keyword args.  Valid args are:

        ============ ===============================================
        Arg          Description
        ============ ===============================================
        pdf          :class:`PDF`
        use_samples  Use data samples attached to pdf (if available)
        ============ ===============================================
    '''
    def __init__(self, name, description, **kwargs):
        Parameter.__init__(self, name, description, **kwargs)
        self.use_samples = kwargs.get('use_samples')
        self.pdf = kwargs.get('pdf')

        if self.pdf is None and self.caldata is None:
            self.usage(0)

        if self.pdf is not None and (isinstance(self.caldata, list) or isinstance(self.caldata, PDF)):
            self.usage(1)

        # If calibration data was supplied and nothing else, fit it to a PDF.
        if self.caldata is not None and self.pdf is None:
            self.pdf = self.caldata

        if self.caldata is not None:
            if isinstance(self.caldata, list):
                # A list of PDFs.  Sample from all of them to produce a PDF
                d = np.array([x.ds(200) for x in self.caldata]).flatten()
                self.pdf = ExperimentalPDF(fit=1, data=d)
            elif isinstance(self.caldata, PDF):
                self.pdf = self.caldata

        # if pdf was an array or list of PDFs, fix it
        if isinstance(self.pdf, np.ndarray):
            self.pdf = ExperimentalPDF(self.pdf, fit=1)
        elif isinstance(self.pdf, list):
            # A list of PDFs.  Sample from all of them to produce a PDF
            d = np.array([x.ds(200) for x in self.pdf]).flatten()
            self.pdf = ExperimentalPDF(fit=1, data=d)

    def usage(self, msg):
        if msg == 0:
            raise ValueError("Error: You must specify a pdf, data, or caldata.")
        elif msg == 1:
            raise ValueError("Error: You specified a pdf and caldata consisting of a PDF or list of PDFs.")

    # This is what you see when the object is printed
    def __str__(self):
        return "CustomParameter %s (%s)\n%s" %\
            (self.name, self.description, self.pdf.__str__())
Exemple #4
0
    def __init__(self, name, description, **kwargs):
        Parameter.__init__(self, name, description, **kwargs)
        self.use_samples = kwargs.get('use_samples')
        self.pdf = kwargs.get('pdf')

        if self.pdf is None and self.caldata is None:
            self.usage(0)

        if self.pdf is not None and (isinstance(self.caldata, list) or isinstance(self.caldata, PDF)):
            self.usage(1)

        # If calibration data was supplied and nothing else, fit it to a PDF.
        if self.caldata is not None and self.pdf is None:
            self.pdf = self.caldata

        if self.caldata is not None:
            if isinstance(self.caldata, list):
                # A list of PDFs.  Sample from all of them to produce a PDF
                d = np.array([x.ds(200) for x in self.caldata]).flatten()
                self.pdf = ExperimentalPDF(fit=1, data=d)
            elif isinstance(self.caldata, PDF):
                self.pdf = self.caldata

        # if pdf was an array or list of PDFs, fix it
        if isinstance(self.pdf, np.ndarray):
            self.pdf = ExperimentalPDF(self.pdf, fit=1)
        elif isinstance(self.pdf, list):
            # A list of PDFs.  Sample from all of them to produce a PDF
            d = np.array([x.ds(200) for x in self.pdf]).flatten()
            self.pdf = ExperimentalPDF(fit=1, data=d)
Exemple #5
0
    def _do_pdf(self, hf, data):
        if self.response:
            # The response surface was built using Uniform distributions.
            # We are interested in the mean and deviation of the data
            # that would have been produced using the real PDFs. For this,
            # we need to compute a weighted mean and deviation
            weights = np.prod([p.pdf.pdf(p.values) for p in self.params], 0)
            tweight = np.sum(weights)

            mean = np.average(data, weights=weights)
            dev = np.sqrt(np.dot(weights, (data - mean)**2) / tweight)
            rsd = np.vstack(([p.values for p in self.params], data))
            rs = pickle(SampledFunc(*rsd, params=self.params))
            print "Mean   = %s" % mean
            print "StdDev = %s" % dev
            return [('response', rs), ('mean', mean), ('dev', dev)]
        else:
            pdf = ExperimentalPDF(data, fit=0)
            mean = np.mean(data)
            dev = np.std(data)
            print "Mean   = %s" % mean
            print "StdDev = %s" % dev
            return [('pdf', pickle(pdf)), ('samples', data), ('mean', mean),
                    ('dev', dev)]
Exemple #6
0
    def _do_pdf(self, hf, data):
        if self.response:
            print('Morris method does not support creating response surfaces')
            raise ValueError
        else:
            pdf = ExperimentalPDF(data, fit=0)
            mean = np.mean(data)
            dev = np.std(data)
            print "Mean   = %s" % mean
            print "StdDev = %s" % dev

            #############
            #analyze results
            ############

            #get the inputs
            inputs = hf['/input/params']
            numparams = len(inputs)

            #N(D+1) x D
            # realizations=np.empty((np.size(data,0),numparams ))

            # f=open(self._salib_paramFile,'w')
            # i=0
            # for p in inputs:
            # aParam=unpickle(hf['/input/params'][p].value)
            # print(aParam.name)
            # #we now have a parameter
            # f.write('{}\t{}\t{}\n'.format(aParam.name,aParam.pdf.range[0],aParam.pdf.range[1]))

            # # get the values
            # realizations[:,i]=aParam.values
            # i+=1

            # f.close()

            #check to make sure the order in which the parameters were initially sampled by SALib
            #was the order in which they were actually sampled by puq
            # np.savetxt(self._salib_realizationsFile,realizations)
            # if os.path.getsize(self._salib_realizationsFile_verify) == os.path.getsize(self._salib_realizationsFile):
            # if not filecmp.cmp(self._salib_realizationsFile_verify, self._salib_realizationsFile, shallow=False):
            # raise Exception('The order in which the parameter samples were constructed is different than the sampled order!')
            # else:
            # raise Exception('The order in which the parameter samples were constructed is different than the sampled order!')

            #get the outputs
            outputs = hf['/output/data']
            numputputs = len(outputs)

            #SALib expects each output variable in a single column
            np.savetxt(self._salib_analysisFile, data)

            #Note: the delimiters for all the files passed to the analyze function must be the same
            s = SAa.morris.analyze(self._salib_paramFile,
                                   self._salib_realizationsFile,
                                   self._salib_analysisFile,
                                   column=0)

            #put things in the same format as the smolyak module
            sens = {}
            for key, val in s.iteritems():
                sens[key] = {
                    'u': val[0],
                    'std': val[1],
                    'ustar': val[2],
                    'ustar_conf95': val[3]
                }
                #senstxt+='{}\t{}\t{}\t{}'.format(key,val[0],val[1],val[2],val[3])

            sorted_list = sorted(
                sens.items(), lambda x, y: cmp(y[1]['ustar'], x[1]['ustar']))

            #os.remove(salib_paramFile)

            return [('pdf', pickle(pdf)), ('samples', data), ('mean', mean),
                    ('dev', dev), ('sensitivity', pickle(sorted_list))]