Beispiel #1
0
import os, os.path
from time import time
import numarray
from numarray import random_array
import random

DSN = "dbname=test port = 5435"

# in order to always generate the same random sequence
random.seed(19)
random_array.seed(19, 20)

def flatten(l):
    """Flattens list of tuples l."""
    return map(lambda x: x[0], l)

def fill_arrays(start, stop):
    col_i = numarray.arange(start, stop, type=numarray.Int32)
    if userandom:
        col_j = random_array.uniform(0, nrows, shape=[stop-start])
    else:
        col_j = numarray.array(col_i, type=numarray.Float64)
    return col_i, col_j

# Generator for ensure pytables benchmark compatibility
def int_generator(nrows):
    step = 1000*100
    j = 0
    for i in xrange(nrows):
        if i >= step*j:
            stop = (j+1)*step
import os, os.path
from time import time
import numarray
from numarray import random_array
import random

DSN = "dbname=test port = 5435"

# in order to always generate the same random sequence
random.seed(19)
random_array.seed(19, 20)


def flatten(l):
    """Flattens list of tuples l."""
    return map(lambda x: x[0], l)


def fill_arrays(start, stop):
    col_i = numarray.arange(start, stop, type=numarray.Int32)
    if userandom:
        col_j = random_array.uniform(0, nrows, shape=[stop - start])
    else:
        col_j = numarray.array(col_i, type=numarray.Float64)
    return col_i, col_j


# Generator for ensure pytables benchmark compatibility
def int_generator(nrows):
    step = 1000 * 100
    j = 0
import bisect       # for appending elements to a sorted list
import logging
#Major Packages
import numarray as na
import numarray.mlab
from numarray.random_array import randint, seed
from numarray.ieeespecial import setnan, getnan
#Library Specific Modules
import graph
import delegate

import distributions
import potentials
import inference

seed()
#logging.basicConfig(level= logging.INFO)

class BVertex(graph.Vertex):
    def __init__(self, name, discrete = True, nvalues = 2, observed = True):
        '''
        Name neen't be a string but must be hashable and immutable.
        if discrete = True:
                nvalues = number of possible values for variable contained in Vertex
        if discrete = False:
                nvalues is not relevant = 0
        observed = True means that this node CAN be observed
        '''
        graph.Vertex.__init__(self, name)
        self.distribution = None
        if discrete:
Beispiel #4
0
def main(argv=None):
    if argv is None:
        argv = sys.argv

    if len(argv)!=6:
        print "Usage: %s <method> <frequency> <vmin> <vmax> <datafile>"%argv[0]
        return 1

    hybrid = "%s_hybrid"%argv[1]
    simplex = "%s_simplex"%argv[1]
    dfile = argv[5]
    hybrid_out = "%s.hybrid_output"%dfile
    simplex_out = "%s.simplex_output"%dfile
    temp_in = "%s.temp_input"%dfile
    frequency = argv[2]
    vmin = argv[3]
    vmax = argv[4]

    os.system("%s %s %s %s %s 200 300 3 %s"%(hybrid,dfile,frequency,vmin,vmax,hybrid_out))

    fin = open(hybrid_out,"r")
    params = []
    paramnames = []
    v = []
    tin = []
    tfit = []
    for l in fin:
        tokens = l.split()
        if tokens[0]=='#':
            if tokens[1]!="Attained":
                paramnames.append(tokens[1])
                params.append(float(tokens[2]))
        else:
            v.append(float(tokens[0]))
            tin.append(float(tokens[1]))
            tfit.append(float(tokens[2]))

    fin.close()
    
    tres = [tfit[i]-tin[i] for i in xrange(len(tin))]
    rms = reduce(lambda x,y: x+y*y, tres,0.0)   
    rms = math.sqrt(rms/len(tres))
    prmstr = ' '.join([str(i) for i in params])
    
    rnd.seed()
    meanparams = [0.0]*len(params)
    rmsparams = [0.0]*len(params)
    niter = 100
    for i in xrange(niter):
        ret = 1
        while(ret!=0):
            rmsarr = rnd.normal(0.0,rms,len(tfit))
            trand = [tfit[j]+rmsarr[j] for j in xrange(len(tfit))]
            fout = open(temp_in,"w")
            for j in range(len(v)):
                print >>fout,"%f\t%g"%(v[j],trand[j])
            fout.close()
            ret=os.system("%s %s %s %s %s %s %s"%(simplex,temp_in,frequency,vmin,vmax,prmstr,simplex_out))

        simplex_params = []
        fin = open(simplex_out,"r")
        for l in fin:
            tokens = l.split()
            if tokens[0]=='#':
                if tokens[1]!="Attained":
                    simplex_params.append(float(tokens[2]))
        fin.close()
        for j in xrange(len(params)):
            meanparams[j]+=simplex_params[j]
            rmsparams[j]+=simplex_params[j]*simplex_params[j]

    rmsparams=map(lambda x,y: math.sqrt((x-(y**2)/niter)/niter),rmsparams,meanparams)
    meanparams=[x/niter for x in meanparams]

    print "Residual RMS: ",rms
    print "Parameter\tMean\t\tSigma\t\tBest"
    for i in range(len(paramnames)):
        print "%s\t\t%f\t%f\t%f"%(paramnames[i],meanparams[i],rmsparams[i],params[i])

    os.remove(simplex_out)
    os.remove(temp_in)
    return 0