Example #1
0
 def __init__(self, medicalWorkerID, patientID, appointmentID,
              appointmentDateTime, reason, prescription):
     Patient.__init__(self, patientID)
     GP.__init__(self, medicalWorkerID)
     self.appointmentID = appointmentID
     self.appointmentDateTime = appointmentDateTime
     self.reason = reason
     self.prescription = prescription
def insert_chrom(tree,chromosome,subtree_chrom,IDlist):
    # insert_chrom function is used to insert the red nodes into suitable tree.
    #   tree: is the tree need be inserted after the red nodes extracted.
    #   chromosome: is the list that saves all extracted values of red nodes.
    #   subtree_chrom: is the list that save all extracted red nodes.
    #   IDlist: is the list that save all ID of the extracted node.
    #       Note: The tree is used as the input of this function need be deepcopy before.

    tree = copy.deepcopy(tree)
    subtree_chrom = copy.deepcopy(subtree_chrom)
    #drawtree(tree)

    length = len(chromosome)

    for i in range(length):
        subtree_chrom[i].valueofnode = chromosome[i] # insert all values into subtree_chrom before insert to GP_tree(tree).

    for i in range(len(chromosome)):
        tree = replaceSubtree(tree,subtree_chrom[i],IDlist[i])
        #drawNodeIDs(tree)
    ## update tree (substrate layer).
    UpdateNodeIDs(tree,0)
    #drawtree(tree)
    tree.childs[0].childs[0].valueofnode = []
    for i in range(len(tree.childs[0].childs[0].childs)):
        tree.childs[0].childs[0].valueofnode.append(tree.childs[0].childs[0].childs[i].valueofnode)
    temp = tree.childs[0].childs[0].valueofnode
    Sub = init.Sub()
    tree.childs[0].valueofnode = []
    for i in range(3):
        # calculate all of the real substrate's parameters.
        if i == 0:
            temp2 = round(((temp[i]+1)/2)*(Sub.rangeOx[1] - Sub.rangeOx[0]) + Sub.rangeOx[0],4) # Ox edge
        elif i == 1:
            temp2 = round(((temp[i]+1)/2)*(Sub.rangeOy[1] - Sub.rangeOy[0]) + Sub.rangeOy[0],4) # Oy edge.
        else:
            temp2 = round(((temp[i]+1)/2)*(Sub.rangeOz[1] - Sub.rangeOz[0]) + Sub.rangeOz[0],4) # Oz edge.
        tree.childs[0].valueofnode.append(temp2)

    # Secondly create patterns.
    MaxX = tree.childs[0].valueofnode[0] - Sub.decrease ### real MaxX,Y are decreased 1mm before to create
                                                  # any pattern unit(like L1,U1,U2,...).
    MaxY = tree.childs[0].valueofnode[1] - Sub.decrease
    Zsize = tree.childs[0].valueofnode[2]
    tree.substrate_size = [MaxX+Sub.decrease,MaxY+Sub.decrease,Zsize]
    MaxXY = [MaxX-Sub.decrease,MaxY-Sub.decrease]
    tree.childs[1].childs[0].childs[0] = gp.updateFullBlueTree(tree.childs[1].childs[0].childs[0],MaxXY,4)
    tree.childs[1].childs[0].valueofnode = gp.get_val_frombluetree(copy.deepcopy(tree.childs[1].childs[0].childs[0]))
    tree.childs[1].valueofnode = tree.childs[1].childs[0].valueofnode

    del subtree_chrom
    del temp
    del temp2

    return tree
Example #3
0
def update_tree(GP_prog):
    import initGlobal as init
    import update_state as us
    anten = init.AnT()
    Sub = init.Sub()
    L = init.L()
    U = init.U()
    low = init.lowlevel()
    GP = init.GP()
    #path = us.load_temporary_path()
    us.update_all_saved_parameters(anten, Sub, L, U, GP, low)

    # calculate all of edge of substrate.
    GP_prog.childs[0].childs[0].valueofnode = []
    for i in range(3):
        GP_prog.childs[0].childs[0].valueofnode.append(
            GP_prog.childs[0].childs[0].childs[i].valueofnode)

    temp = GP_prog.childs[0].childs[0].valueofnode

    GP_prog.childs[0].valueofnode = []
    for i in range(3):
        # calculate all of the real substrate's parameters.
        if i == 0:
            temp2 = round(((temp[i] + 1) / 2) *
                          (Sub.rangeOx[1] - Sub.rangeOx[0]) + Sub.rangeOx[0],
                          4)  # Ox edge
        elif i == 1:
            temp2 = round(((temp[i] + 1) / 2) *
                          (Sub.rangeOy[1] - Sub.rangeOy[0]) + Sub.rangeOy[0],
                          4)  # Oy edge.
        else:
            temp2 = round(((temp[i] + 1) / 2) *
                          (Sub.rangeOz[1] - Sub.rangeOz[0]) + Sub.rangeOz[0],
                          4)  # Oz edge.
        GP_prog.childs[0].valueofnode.append(temp2)

    # Secondly create patterns.
    MaxX = GP_prog.childs[0].valueofnode[
        0] - Sub.decrease  ### real MaxX,Y are decreased 1mm before to create
    # any pattern unit(like L1,U1,U2,...).
    MaxY = GP_prog.childs[0].valueofnode[1] - Sub.decrease
    Zsize = GP_prog.childs[0].valueofnode[2]
    GP_prog.substrate_size = [MaxX + Sub.decrease, MaxY + Sub.decrease, Zsize]

    GP_prog.childs[1].childs[0].childs[0] = gp.updateFullBlueTree(
        GP_prog.childs[1].childs[0].childs[0], [MaxX, MaxY], 5)
    GP_prog.childs[1].childs[0].valueofnode = gp.get_val_frombluetree(
        copy.deepcopy(GP_prog.childs[1].childs[0].childs[0]))
    GP_prog.childs[1].valueofnode = GP_prog.childs[1].childs[0].valueofnode

    return GP_prog
Example #4
0
    def __init__(self,
                 mean,
                 kernel,
                 tolr=1e-8,
                 hyperparameterMethod='default',
                 optimizationOptions='default'):
        nIS = len(mean)
        d = mean[0].dimension()
        for s in range(nIS):
            if not mean[s].dimension() == d:
                raise ValueError(
                    'All mean functions must have the same dimension')

        if len(kernel) == nIS:
            for s in range(nIS):
                if not kernel[s].dimension() == d:
                    raise ValueError(
                        'All mean functions must have the same dimension')
        else:
            raise ValueError(
                'The number of mean functions must match the number of covariance kernels'
            )

        if type(tolr) is list:
            if not len(tolr) == nIS:
                raise ValueError(
                    'The number of tolerance values must match the number of covariance kernels'
                )
        else:
            tolr = [tolr] * nIS

        if type(hyperparameterMethod) is list:
            if not len(hyperparameterMethod) == nIS:
                raise ValueError(
                    'The number of hyperparameter estimate methods must match the number of covariance kernels'
                )
            else:
                hyper = deepcopy(hyperparameterMethod)
        else:
            hyper = [hyperparameterMethod] * nIS

        if type(optimizationOptions) is list:
            if not len(optimizationOptions) == nIS:
                raise ValueError(
                    'The number of optimization option sets must match the number of covariance kernels'
                )
            else:
                opt = deepcopy(optimizationOptions)
        else:
            opt = [optimizationOptions] * nIS

        self.f = []
        for s in range(nIS):
            self.f += [GP.GP(mean[s], kernel[s], tolr[s], hyper[s], opt[s])]

        self.d = d
        self.nIS = nIS
        self.source = None
        self.x = None
        self.y = None
Example #5
0
 def itUI(self):
     global text
     text = QTextEdit()
     GP.PythonHighlighter(text)
     hbox = QHBoxLayout()
     hbox.addWidget(text)
     self.setLayout(hbox)
def calMaxdepBlueAble_fullTree(fulltree,maxBlue,nodeID):
    # calculate the maxdepable of a specified blue node in a full tree.
    # notice: this function only uses for blue tree.
    fullbluetree = copy.deepcopy(fulltree.childs[1].childs[0].childs[0])
    bluetree = gp.convertFullBluetree_to_oriBluetree(fullbluetree)
    del fullbluetree
    return calMaxdepAble(bluetree,maxBlue,nodeID)
Example #7
0
def PULLIMG():
    try:
        import GP
        imgName = GP.Capture()
        imgName = imgName.strip()
        f = open(imgName, 'rb')
        imgData = f.read()

        cmd = "BS+PUSHIMG=" + str(
            f.tell()
        ) + "\r\n"  #f.tell() gives the current position of file pointer and as have
        # read all the bytes using .read() it give the size of the file.

        print "Sending CMD:", cmd

        f.close()

        time.sleep(
            0.5
        )  #wait for a little time to let the basestation ready to receive the command BS+PUSH

        Udoo.write(cmd)

        print "Image Load Complete....Pushing Image in 1s."
        time.sleep(
            1
        )  # wait for a little time to let the basestation be ready to receive the Image

        Udoo.write(imgData)

        print "IMAGE sent."

    except Exception as e:
        print "Unexpected error:", e
def self_tuned_GP(step, dim, grid_width=8):
    cov_root = torch.eye(dim + 1, requires_grad=True)
    obsvar = torch.ones(1, requires_grad=True)
    k = GP.gaussian_kernel(cov_root)

    def no_time_step(x):
        return step(x[:-1]).detach()

    tuner = GPTuner(dim + 1, no_time_step, k, obsvar, 100)
    grid = gaussian_grid(torch.zeros(dim), torch.eye(dim), grid_width)
    grid = F.pad(grid, (0, 1))

    hyperopt = optim.SGD([cov_root, obsvar], lr=0.001)
    max_cov = torch.eye(dim)
    max_mean = torch.zeros(dim)

    def tuning_step():
        max_sample = tuner.sample_argmax(grid)[:, :dim]
        max_mean.mul_(0.9)
        max_mean.add_(0.1 * torch.mean(max_sample, 0))
        max_cov.mul_(0.9)
        max_cov.add_(0.1 * sample_cov(max_sample))

        grid[:, :dim].copy_(gaussian_grid(max_mean, max_cov, width=grid_width))
        tuner.thompson_step(grid)
        loss = -(tuner.log_prob().mean())
        print(max_mean, max_cov)
        loss.backward()
        hyperopt.step()
        grid[:, dim] += 1

    return tuning_step
def calCurrentMaxDepth_fullTree(fulltree,nodeID):
    # calculate the current maxdepth of a specified blue node in full tree.
    # notice: this function only uses for blue tree.
    fullbluetree = copy.deepcopy(fulltree.childs[1].childs[0].childs[0])
    bluetree = gp.convertFullBluetree_to_oriBluetree(fullbluetree)
    node = getSubtreeFromTree(bluetree,nodeID)
    del fullbluetree
    del bluetree
    return calCurrentMaxDepth(node)
Example #10
0
    def __init__(self, Y, dim):
        self.Xdim = dim
        self.N, self.Ydim = Y.shape
        """Use PCA to initalise the problem. Uses EM version in this case..."""
        myPCA_EM = PCA_EM(Y, dim)
        myPCA_EM.learn(100)
        X = np.array(myPCA_EM.m_Z)

        self.GP = GP.GP(X, Y)  #choose particular kernel here if so desired.
Example #11
0
    def __init__(self, Y, dim, nparticles=100):
        self.Xdim = dim
        self.T, self.Ydim = Y.shape
        """Use PCA to initalise the problem. Uses EM version in this case..."""
        myPCA_EM = PCA_EM(Y, dim)
        myPCA_EM.learn(300)
        X = np.array(myPCA_EM.m_Z)

        self.observation_GP = GP.GP(X, Y)

        #create a linear kernel for the dynamics
        k = kernels.linear(-1, -1)
        self.dynamic_GP = GP.GP(X[:-1], X[1:], k)

        #initialise the samples from the state /latent variables
        self.particles = np.zeros((self.T, nparticles, self.Xdim))

        #sample for x0 TODO: variable priors on X0
        self.particles[0, :, :] = np.random.randn(nparticles, self.Xdim)
Example #12
0
    def add_gp(first_name, last_name, email, password, gp_list, calendar_list):
        """
        Placeholder code. This method would create a GP object and set it to inactive initially.
        The admin can then activate/deactivate the account as needed with the other methods.
        """
        gp = GP(first_name, last_name, email, password)
        gp_list[email] = gp

        calendar = Calendar(email)
        calendar_list[email] = calendar
Example #13
0
def BetaMatrix(DistanceMatrix, parameter, method="gradient", GP=None):
    if method == "gradient":
        beta0 = parameter[0]
        phi = parameter[1]
        BetaMatrix = beta0 * np.exp(-DistanceMatrix * phi)
        #K takes distance -arg paratemeters->
        #returns
        #self.BetaMatrix=np.array(BetaMatrix)
        return BetaMatrix
    elif method == "powerlaw":
        parameter = np.array(parameter)
        if parameter.size != 3:
            raise ValueError(
                "powerlaw method need 3 arguments as beta0,phi,omega")
        beta0 = parameter[0]
        phi = parameter[1]
        omega = parameter[2]
        BetaMatrix = beta0 * 1 / (phi**2 + DistanceMatrix**2)**omega
        return BetaMatrix
    elif method == "GaussianProcess":
        m = GP.size
        n = (1 + np.sqrt(1 + 8 * m)) / 2
        BetaMatrix = gp.LowerTriangularVectorToSymmetricMatrix(GP, n)
        return np.exp(BetaMatrix)
    elif method == "gradientGaussianProcess":
        beta0 = parameter[0]
        phi = parameter[1]
        n = (1 + np.sqrt(1 + 8 * GP.size)) / 2
        BetaMatrix = beta0 * np.exp(-DistanceMatrix * phi)
        BetaMatrix = BetaMatrix * np.exp(
            gp.LowerTriangularVectorToSymmetricMatrix(GP, n))
        return BetaMatrix
    elif method == "powerlawGaussianProcess":
        parameter = np.array(parameter)
        beta0 = parameter[0]
        phi = parameter[1]
        omega = parameter[2]
        n = (1 + np.sqrt(1 + 8 * GP.size)) / 2
        BetaMatrix = beta0 * 1 / (phi**2 + DistanceMatrix**2)**omega
        BetaMatrix = BetaMatrix * np.exp(
            gp.LowerTriangularVectorToSymmetricMatrix(GP, n))
        return BetaMatrix
Example #14
0
 def Likelihood(self,parameter,GP):
     gamma=parameter[-1]
     BetaMatrix=cr.BetaMatrix(self.DistanceMatrix,np.delete(parameter,-1),self.method)
     ####Add random  effect GP
     BetaMatrix=np.exp(np.log(BetaMatrix)+gp.LowerTriangularVectorToSymmetricMatrix(GP,BetaMatrix.shape[0]))
     probabilityMatrix=self.ProbabilityMatrix(BetaMatrix,gamma)
     change=self.change
     probabilityMatrix[change==0]=1-probabilityMatrix[change==0]
     loglikelihoodMatrix=np.log(probabilityMatrix)
     logLikelihood=loglikelihoodMatrix.sum(1).sum()
     return logLikelihood 
    def Mainprocess(self):
        parameter = self.initial_parameter
        record = parameter
        Accept = np.zeros((self.IterNa, self.dimension))
        GP = self.initialGP
        self.AcceptGP = np.zeros(self.IterNa)
        self.recordGP = GP
        self.CovUpdate = gp.UpdatingCov(GP.size)
        f = open('GP.csv', 'w')
        f2 = open('wP.csv', 'w')
        writer = csv.writer(f, delimiter=',')
        writer2 = csv.writer(f2, delimiter=',')
        for i in range(0, self.IterNa):
            if self.parameterMode != "c":
                '''
                if parameterMode is constant, the parameter do not move, just avoid the MH algorithm
                value is as the initialValue
                the parameter part is as the mean of GP
                '''
                for j in range(0, self.dimension):
                    result = self.OnestepMetropolis(self.density[j], parameter,
                                                    self.sigma[j], GP, j)
                    Accept[i, j] = result[0]
                    parameter[j] = result[1]
            #update recover rate sigma
            '''j=self.dimension-1
            result=self.OnestepMetropolisGPAdaptive(j,self.density[j],parameter,self.sigma[j],GP,j)
            Accept[i,j]=result[0]
            parameter[j]=result[1]
            '''
            record = np.vstack((record, parameter))
            writer2.writerow(parameter)

            if self.GPmode != "c":
                resultGP = self.OnestepMetropolisGPAdaptive(
                    i, self.GPdensity, parameter, self.GaussianProcess, GP)
                self.AcceptGP[i] = resultGP[0]
                self.recordGP = np.vstack((self.recordGP, resultGP[1]))
                GP = resultGP[1]
                writer.writerow(GP)
            ##############################################
            #self.AdaptiveCovariance=gp.updateCor(GP)#####
            ##############################################
        self.record = record
        self.Accept = Accept
        #if self.GPmode!="c":
        #self.recordGP=recordGP
        #self.AcceptGP=AcceptGP

        f.close()
        f2.close()
    def OnestepMetropolisGPAdaptive(self, j, densityGP, parameter,
                                    GaussProcess, CurrentGP):
        '''
        The adaptive MCMC method. Change the Covariance Matrix everytime
        Use the empirical covariance for the MCMC output

        
        Monte Carlo not Markov Chain
        MCnMC XD

        '''

        if j < self.AdaptiveConstant:
            #if iterative<200 then do the sample update
            newGP = GaussProcess.StandardSampleForGP(CurrentGP)
        else:
            #if j>200 then do the adaptive Sampler
            newGP = gp.AdaptiveSampleForGP(CurrentGP,
                                           self.CovUpdate.getOnlineCov())
            #newGP=gp.SampleForGPCov(CurrentGP,np.cov(self.recordGP.T))
            #newGP=GaussProcess.AdaptiveSampleForGPSpecial(CurrentGP,np.cov(self.recordGP.T))
            #newGP=GaussProcess.AdaptiveSampleForGPSpecial(CurrentGP,self.CovUpdate.getOnlineCov())
        #---->Seems we need online update cholesky decomposition  (/゚Д゚)/
        #the update for the covariance is outside this function!

        #need rebuild the sample with History data
        #Something like newGP=GaussProcess.SampleForGP(CurrentGP,self.recordGP)
        #Then the following is the same

        #Accept the new beta value with the probability f(beta_start)/f(beta)
        lognew = densityGP(parameter, GaussProcess, newGP)
        logold = densityGP(parameter, GaussProcess, CurrentGP)
        mid = lognew - logold
        p = min(
            np.exp(mid), 1
        )  ####################Transfor p/p,or use log normal:btw: p/p=1 solved
        #print(p)
        if np.random.uniform(0, 1) < p:
            #Accept the new Value
            self.CovUpdate.updateOnlineCov(newGP)
            return [1, newGP]
            #count the number of accept
        else:
            self.CovUpdate.updateOnlineCov(CurrentGP)
            return [0, CurrentGP]
Example #17
0
import GP as gp
#plt.ion()
#plt.style.use('ggplot')
population=50

#model1=gc2.heteregeneousModel(population,[0.4,10,0.3],True,True,"gradient","uniform",False)
model1=gc2.heteregeneousModel(population,[1000,20,1.5,0.3],True,True,"powerlaw","uniform",True)
model1.Animate()
#estimate=lk2.Estimation(model1.record,model1.geo,method="powerlaw")
estimate=lk2.Estimation(model1.record,model1.geo,method="gradient")
#Metro=mp3.multiMetropolis(1000,[estimate.GammaPosteriorBeta0,estimate.GammaPosteriorGamma,estimate.GammaPosteriorPhi],[0.1,0.1,5],[0.5,0.5,0.4])
#Metro=mp3.multiMetropolis(1000,[partial(estimate.GammaPriorGeneralPosterior,i=0),partial(estimate.GammaPriorGeneralPosterior,i=1),partial(estimate.GammaPriorGeneralPosterior,i=2)],[0.1,0.1,5],[0.5,0.5,0.4])
#Metro=mp3.multiMetropolis(1000,[estimate.GammaPosteriorBeta0,estimate.GammaPosteriorGamma],[0.1,0.1],[0.4,0.4])
#Metro=mp3.multiMetropolis(1000,[partial(estimate.GammaPriorGeneralPosterior,i=0),partial(estimate.GammaPriorGeneralPosterior,i=1),partial(estimate.GammaPriorGeneralPosterior,i=2),partial(estimate.GammaPriorGeneralPosterior,i=3)],[3,0.1,0.9,1],[0.5,0.5,0.4,0.4])
#InitialGP=np.zeros(population*(population-1)/2)
InitialGP=gp.InitialGP(estimate.DistanceMatrix,np.array((1,1)))
GPDoc=gp.GaussianProcess(estimate.DistanceMatrix,np.array((1,np.mean(estimate.DistanceMatrix))))
################
InitialGP=GPDoc.SampleForGP(np.zeros(population*(population-1)/2))
BetaMatrix=model1.BetaMatrix
BetaMatrix3=cr.BetaMatrix(model1.DistanceMatrix,[1,1])
gp.BetaMatrixPlot(model1.DistanceMatrix,[BetaMatrix,np.exp(np.log(BetaMatrix)+gp.LowerTriangularVectorToSymmetricMatrix(InitialGP,BetaMatrix.shape[0])),BetaMatrix3],3)
test=estimate.GaussianPriorGP([0.1,0.9,1],GPDoc,InitialGP)

Metro=mp3.multiMetropolis(1000,[partial(estimate.GammaPriorGeneralPosterior,i=0),partial(estimate.GammaPriorGeneralPosterior,i=1),partial(estimate.GammaPriorGeneralPosterior,i=2)],[0.1,0.9,1],[0.7,0.5,0.7],InitialGP,GPDoc,estimate.GaussianPriorGP,"Change")
np.savetxt("GP.csv", Metro.recordGP, delimiter=",")
np.savetct("ParameterRecord.csv",Metro.record,delimiter=",")
Metro.showplot(0)
Metro.printall(0)
Metro.showplot(1)
Metro.printall(1)
import matplotlib.pyplot as plt
import Metropolis3 as mp3
import generator_temp_transprob as gc2
import likelihodPhi as lk2
import coordinate as cr
from scipy.stats import beta
from functools import partial
import GP as gp

population = 50
model1 = gc2.heteregeneousModel(population, [0.4, 0.1, 0.3], True, True)
model1.Animate()
estimate = lk2.Estimation(model1.record, model1.geo)
InitialGP = np.zeros(population * (population - 1) /
                     2)  #zero GP and no transform
GPDoc = gp.GaussianProcess(estimate.DistanceMatrix,
                           np.array((1, np.mean(estimate.DistanceMatrix))))
InitialGP = GPDoc.SampleForGP(np.zeros(population * (population - 1) / 2))
GPDoc = gp.GaussianProcess(estimate.DistanceMatrix,
                           np.array((0.01, np.mean(estimate.DistanceMatrix))))
Metro = mp3.multiMetropolis(3000, None, [0.4, 0.1, 0.3], None, InitialGP,
                            GPDoc, estimate.GaussianStandardPriorGP, "Change",
                            "c", 1000)

gp.kernelFunctonPlot(model1.DistanceMatrix, Metro.recordGP, Metro.record,
                     "gradient")
gp.kernelFunctonPlotRebuild(model1.DistanceMatrix, Metro.recordGP,
                            Metro.record[0, :], "gradient", Metro.record[0, :],
                            InitialGP)
Metro.printAcceptRateGP()
Metro.plotOneComponentGP(0)
Metro.plotOneComponentGP(1)
def makeGP_prog(maxSub, maxPat, maxBlue, ismaxdep):
    # makeGP_prog makes a entire GP tree.
    # whre maxSub: the maximum depth of the genSub sub-tree.
    # maxPat: the maximum depth of the genPat sub-tree.
    # maxBlue: the maximum depth of the Blue sub-tree.
    # ismaxdep: specify whether GP tree will be created with maxdepth of all branch or not.
    GP_prog = gp.node()
    Sub = init.Sub()
    if init.Re_trainning:
        import update_state as us
        anten = init.AnT()
        Sub = init.Sub()
        L = init.L()
        U = init.U()
        low = init.lowlevel()
        GP = init.GP()
        #path = us.load_temporary_path()
        us.update_all_saved_parameters(anten, sub, L, U, GP, low)

    GP_prog.strname = 'GP_prog'
    GP_prog.childs = []
    GP_prog.funcORter = 'func'

    # firstly need to create the substrate tree.
    if maxSub == 1:
        # create addsub3 tree.
        gensub1 = gp.node()
        gensub1.strname = 'gensub1'
        gensub1.type = 4
        gensub1.childs = []
        gensub1.childs.append(gp.addsub3())
        gensub1.funcORter = 'func'
        GP_prog.childs.append(gensub1)
    else:  # when maxSub != 1.
        pass

    # calculate all of edge of substrate.
    GP_prog.childs[0].childs[0].valueofnode = []
    for i in range(3):
        GP_prog.childs[0].childs[0].valueofnode.append(
            GP_prog.childs[0].childs[0].childs[i].valueofnode)

    temp = GP_prog.childs[0].childs[0].valueofnode

    GP_prog.childs[0].valueofnode = []
    for i in range(3):
        # calculate all of the real substrate's parameters.
        if i == 0:
            temp2 = round(((temp[i] + 1) / 2) *
                          (Sub.rangeOx[1] - Sub.rangeOx[0]) + Sub.rangeOx[0],
                          4)  # Ox edge
        elif i == 1:
            temp2 = round(((temp[i] + 1) / 2) *
                          (Sub.rangeOy[1] - Sub.rangeOy[0]) + Sub.rangeOy[0],
                          4)  # Oy edge.
        else:
            temp2 = round(((temp[i] + 1) / 2) *
                          (Sub.rangeOz[1] - Sub.rangeOz[0]) + Sub.rangeOz[0],
                          4)  # Oz edge.
        GP_prog.childs[0].valueofnode.append(temp2)

    # Secondly create patterns.
    MaxX = GP_prog.childs[0].valueofnode[
        0] - Sub.decrease  ### real MaxX,Y are decreased 1mm before to create
    # any pattern unit(like L1,U1,U2,...).
    MaxY = GP_prog.childs[0].valueofnode[1] - Sub.decrease
    Zsize = GP_prog.childs[0].valueofnode[2]
    GP_prog.substrate_size = [MaxX + Sub.decrease, MaxY + Sub.decrease, Zsize]

    [tree, lastnode] = gp.makeBlueTree(maxBlue, ismaxdep, 0, MaxX, MaxY)
    fullbluetree = gp.genFullBlueTree(tree)

    fullbluetree = gp.updateFullBlueTree(fullbluetree, [MaxX, MaxY], 1)

    if maxPat == 1:
        # create
        genpat1 = gp.node()
        genpat1.strname = 'genpat1'
        genpat1.type = 5
        genpat1.childs = []
        bluetree1 = gp.node()
        bluetree1.strname = 'bluetree1'
        bluetree1.type = 2
        bluetree1.funcORter == 'func'
        bluetree1.childs = []
        bluetree1.childs.append(fullbluetree)
        genpat1.childs.append(bluetree1)
        genpat1.funcORter = 'func'
        GP_prog.childs.append(genpat1)
        GP_prog.childs[1].childs[0].valueofnode = gp.get_val_frombluetree(
            copy.deepcopy(fullbluetree))
        GP_prog.childs[1].valueofnode = GP_prog.childs[1].childs[0].valueofnode

    return GP_prog
import numpy as np
from scipy.stats import norm
import matplotlib.pyplot as plt
import Metropolis3 as mp3
import generator_temp_transprob as gc2
import likelihodPhi as lk2
import coordinate as cr
from scipy.stats import beta
from functools import partial
import GP as gp
population=25
model1=gc2.heteregeneousModel(population,[0.4,0.1,0.3],True,True,"gradient","uniform",False)
#model1.Animate()
estimate=lk2.Estimation(model1.record,model1.geo,method="gradient")
GPDoc=gp.GaussianProcess(estimate.DistanceMatrix,np.array((1,np.mean(estimate.DistanceMatrix))))
InitialGP=GPDoc.SampleForGP(np.zeros(population*(population-1)/2))

Metro=mp3.multiMetropolis(1000,None,[0.3,0.3,0.3],None,InitialGP,GPDoc,estimate.GaussianPriorGP,"Change","c")
gp.GPPlot(model1.DistanceMatrix,Metro.recordGP)
gp.kernelFunctonPlot(model1.DistanceMatrix,Metro.recordGP,Metro.record,"gradient")
gp.kernelFunctonPlotRebuild(model1.DistanceMatrix,Metro.recordGP,[0.3,0.3,0.3],"gradient",[0.4,0.1,0.3],InitialGP)
Metro.printAcceptRateGP()
Example #21
0
import numpy as np
import generator_temp_transprob as gc
import coordinate as cr
import Metropolis3 as mp3
import likelihodPhi as llk
import GP as gp
geo = cr.geodata(40)
Distance = cr.DistanceMatrix(geo)
Corr = gp.CovarianceMatrix(Distance, np.array((1, 0.01)))
Chol = np.linalg.cholesky(Corr)
print(Chol)
Example #22
0
model1.Animate()
#estimate=lk2.Estimation(model1.record,model1.geo,method="powerlaw")
estimate=lk2.Estimation(model1.record,model1.geo,method="gradient")
#Metro=mp3.multiMetropolis(1000,[estimate.GammaPosteriorBeta0,estimate.GammaPosteriorGamma,estimate.GammaPosteriorPhi],[0.1,0.1,5],[0.5,0.5,0.4])
#Metro=mp3.multiMetropolis(1000,[partial(estimate.GammaPriorGeneralPosterior,i=0),partial(estimate.GammaPriorGeneralPosterior,i=1),partial(estimate.GammaPriorGeneralPosterior,i=2)],[0.1,0.1,5],[0.5,0.5,0.4])
#Metro=mp3.multiMetropolis(1000,[estimate.GammaPosteriorBeta0,estimate.GammaPosteriorGamma],[0.1,0.1],[0.4,0.4])
#Metro=mp3.multiMetropolis(1000,[partial(estimate.GammaPriorGeneralPosterior,i=0),partial(estimate.GammaPriorGeneralPosterior,i=1),partial(estimate.GammaPriorGeneralPosterior,i=2),partial(estimate.GammaPriorGeneralPosterior,i=3)],[3,0.1,0.9,1],[0.5,0.5,0.4,0.4])
#InitialGP=np.zeros(population*(population-1)/2)
InitialGP=gp.InitialGP(estimate.DistanceMatrix,np.array((1,1)))
GPDoc=gp.GaussianProcess(estimate.DistanceMatrix,np.array((1,np.mean(estimate.DistanceMatrix))))
################
InitialGP=GPDoc.SampleForGP(np.zeros(population*(population-1)/2))
BetaMatrix=model1.BetaMatrix
BetaMatrix3=cr.BetaMatrix(model1.DistanceMatrix,[1,1])
gp.BetaMatrixPlot(model1.DistanceMatrix,[BetaMatrix,np.exp(np.log(BetaMatrix)+gp.LowerTriangularVectorToSymmetricMatrix(InitialGP,BetaMatrix.shape[0])),BetaMatrix3],3)
test=estimate.GaussianPriorGP([0.1,0.9,1],GPDoc,InitialGP)

Metro=mp3.multiMetropolis(1000,[partial(estimate.GammaPriorGeneralPosterior,i=0),partial(estimate.GammaPriorGeneralPosterior,i=1),partial(estimate.GammaPriorGeneralPosterior,i=2)],[0.1,0.9,1],[0.7,0.5,0.7],InitialGP,GPDoc,estimate.GaussianPriorGP,"Change")
np.savetxt("GP.csv", Metro.recordGP, delimiter=",")
Metro.showplot(0)
Metro.printall(0)
Metro.showplot(1)
Metro.printall(1)
Metro.showplot(2)
Metro.printall(2)
Metro.plotcountour(0,1)
Metro.plotcountour(1,2)
Metro.plotcountour(0,2)
gp.GPPlot(model1.DistanceMatrix,Metro.recordGP)
gp.kernelFunctonPlot(model1.DistanceMatrix,Metro.recordGP,Metro.record,"gradient")
Example #23
0
                tmp.append(std_13[4][y][x])
                tmp.append(std_17[4][y][x])
                tmp.append(std_21[4][y][x])

        if (s == 2 or s == 3 or s == 4):
            tmp.append(int(images[5][y][x]))
            if (i == 1):
                tmp.append(mean_13[5][y][x])
                tmp.append(mean_17[5][y][x])
                tmp.append(mean_21[5][y][x])
            if (i == 2):
                tmp.append(std_13[5][y][x])
                tmp.append(std_17[5][y][x])
                tmp.append(std_21[5][y][x])

        # sobels:mean_17[y][x],
        #print (tmp)
        row.append(tmp)
    data.append(row)
    '''
# num gens, pop size, cross rate, mut rate
gp = GP.GP(60, 750, 0.85, 0.1)
gp.setData(data, labels)
gp.setTrain(points, gt_img)
gp.setUpGP("Data-" + str(s) + "-Test", args.test_num)
gp.runGP()

f = open("Completed/Complete_Log.txt", 'a+')
f.write("Completed: " + "Data-" + str(s) + "-Test" + str(args.test_num) + "\n")
f.close()
    if isinstance(feature_new, int) or isinstance(feature_new, float):
        return 999
    if isinstance(feature_new, np.ndarray):
        feature_new = pd.Series(feature_new)
    if not isinstance(feature_new, pd.Series):
        return 999 # np.float64
    if feature_new.isnull().sum()>0 or feature_new.var()==0:
        return 999
    f = log_loss(y, feature_new.map(np.tanh))
    print(f)
    return f

# =============================================================================
# optimize
# =============================================================================
gp = GP.GP(X, y, 2, -3, 3, population=100, generation=10,
        feval=get_fitness_logloss, maximize=False, n_jobs=10)

gp.fit()

print(gp[0].eval())
print(gp[0].fitness, gp[0].parse())




#==============================================================================
utils.end(__file__)


Example #25
0
    def __init__(self, dim_in, dim_hidden_initial, dim_hidden_max, dim_out, \
        s2_0, \
        T, length_scale_sgcp, variance_sgcp, proposal_std_cM, \
        rate_density_ub, \
        prior_w_sig2 = 1., prior_b_sig2 = 1., \
        sig2 = None, prior_sig2_alpha = None, prior_sig2_beta = None,
        infer_wb =True, infer_cK = True, infer_cM=True, infer_gM=True, infer_gK=True, infer_M=True, infer_K=True, infer_rate_density_ub=True,
        rate_density_ub_prior_alpha = None, rate_density_ub_prior_beta = None, rate_density_ub_proposal_std = 2.,
        use_gp_term = True, set_gp_to_mean = False):
        super(RBFN, self).__init__()

        # inference overrides
        self.infer_wb = infer_wb
        self.infer_cK = infer_cK
        self.infer_cM = infer_cM
        self.infer_gM = infer_gM
        self.infer_gK = infer_gK
        self.infer_M = infer_M
        self.infer_K = infer_K
        self.infer_rate_density_ub = infer_rate_density_ub

        self.use_gp_term = use_gp_term
        self.set_gp_to_mean = set_gp_to_mean

        # architecture
        self.dim_in = dim_in
        self.dim_hidden_initial = dim_hidden_initial
        self.dim_hidden = dim_hidden_initial  # this will change
        self.dim_hidden_max = dim_hidden_max
        self.dim_out = dim_out

        # parameters
        if self.infer_cK:
            self.cK = nn.Parameter(torch.Tensor(dim_in, dim_hidden_max))
        else:
            self.cK = torch.empty(dim_in, dim_hidden_max)

        if self.infer_gM or self.infer_gK:
            self.cK.requires_grad = True

        if self.infer_wb:
            self.w = nn.Parameter(torch.Tensor(dim_out, dim_hidden_max))
            self.b = nn.Parameter(torch.Tensor(dim_out))
        else:
            self.w = torch.empty(dim_out, dim_hidden_max)
            self.b = torch.empty(dim_out)

        self.mask = torch.zeros(dim_hidden_max, dtype=torch.bool)
        self.mask[:dim_hidden_initial] = 1

        # momentum parameters
        if self.infer_cK:
            self.p_c = torch.empty(self.cK.shape)

        if self.infer_wb:
            self.p_w = torch.empty(self.w.shape)
            self.p_b = torch.empty(self.b.shape)

        # priors
        self.prior_w_sig2 = torch.tensor(np.sqrt(s2_0 / np.pi) * prior_w_sig2)
        self.prior_b_sig2 = torch.tensor(prior_b_sig2)

        if prior_sig2_alpha is None or prior_sig2_beta is None:
            self.infer_sig2 = False
            self.sig2 = torch.tensor(sig2)
        else:
            self.infer_sig2 = True
            self.prior_sig2_alpha = prior_sig2_alpha
            self.prior_sig2_beta = prior_sig2_beta

        self.rate_density_ub_prior_alpha = rate_density_ub_prior_alpha
        self.rate_density_ub_prior_beta = rate_density_ub_prior_beta
        self.rate_density_ub_proposal_std = rate_density_ub_proposal_std

        ########### SGCP
        self.T = T
        self.rate_density_ub = torch.tensor(rate_density_ub)
        self.prob_birth_thinned = 0.5
        self.proposal_std_cM = proposal_std_cM
        self.fudge = 8e-4  #self.fudge = 8e-3
        self.V = T[1] - T[0]  # Volume of region

        self.gK = nn.Parameter(torch.Tensor(dim_hidden_max))

        # GP:
        self.kernel = GP.RBFkernel(length_scale_sgcp, variance_sgcp)
        self.gp = GP.GP(self.kernel, self.fudge)

        self.h = lambda x: s2_0 * x**2

        self.p_gK = torch.empty(self.dim_hidden_max)
        ###########

        # initialize
        self.sample_parameters()  # added for fixing parameters version
        self.init_parameters()
        self._sample_momentum()
        self.sample_parameters_sgcp()

        self.have_ground_truth = False
Example #26
0
'''
This file contains the function that read the MCMC results in the current file
'''
import os
import numpy as np
import GP as gp
import coordinate as cr
a = os.listdir()
#print (a)
markGP = [s for s in a if s.find('GP') != -1 and s.find(".csv") != -1]
markparameter = [
    s for s in a if s.find('parameter') != -1 and s.find(".csv") != -1
]

print(markGP)
print(markparameter)
print("plot which data")
k = input()  #list start with 0
k = int(k)
recordGP = np.loadtxt(markGP[k], delimiter=",")
recordParameter = np.loadtxt(markparameter[k], delimiter=",")
geo = np.loadtxt("geo_uniform.txt")
DistanceMatrix = cr.DistanceMatrix(geo)
gp.kernelFunctonPlot(DistanceMatrix, recordGP, recordParameter, "gradient")
InitialGP = recordGP[0, :]
gp.kernelFunctonPlotRebuild(DistanceMatrix, recordGP, recordParameter[0, :],
                            "gradient", recordParameter[0, :], InitialGP)
 def predict(self, x):
     mean, var = GP.predict(self.history_x.all(), self.history_y.all(),
                            self.obs_var().expand(len(self.history_y)), x,
                            mean_zero, self.k)
     var += self.eps * torch.eye(var.shape[0])
     return mean, var
Example #28
0
import numpy as np
from scipy.stats import norm
import matplotlib.pyplot as plt
#import Metropolis as mp
#import Metropolis2 as mp2
import Metropolis3 as mp3
#import generator_temp as gc
import generator_temp_transprob as gc2
#import likelihood as lk
import likelihodPhi as lk2
import coordinate as cr
from scipy.stats import beta
from functools import partial
import GP as gp
'''
geo=cr.geodata(50)
geo=cr.geodata(50,"uniform",xbound=100.0,ybound=100.0,history=False)
Distance=cr.DistanceMatrix(geo)
BetaMatrix=cr.BetaMatrix(Distance,[0.3,5])
gp.BetaMatrixPlot(Distance,BetaMatrix)
'''
model1 = gc2.heteregeneousModel(50, [0.3, 5, 0.3])
gp.BetaMatrixPlot(model1.DistanceMatrix, model1.BetaMatrix, 1)

#gp.BetaMatrixPlot(model1.DistanceMatrix,model1.BetaMatrix)
Example #29
0
def index(func):
    return GP.getgenfunction(func, False)
Example #30
0
def realTime(func):
    return GP.getgenfunction(func, True)
Example #31
0
    x1 = np.sort(25 * np.random.rand(1, N) -
                 25 * np.random.rand(1, N)).reshape(N, 1)
    x2 = np.sort(25 * np.random.rand(1, N) -
                 25 * np.random.rand(1, N)).reshape(N, 1)
    x1s = np.linspace(-24, 24, num=300).reshape(300, 1)
    x2s = np.linspace(-24, 24, num=300).reshape(300, 1)
    x = np.hstack((x1, x2))
    xs = np.hstack((x1s, x2s))
    y = x1**2 - 10 * x1 * (np.sin(x2))**3 + np.random.normal(scale=10,
                                                             size=(N, 1))
    ys = x1s**2 - 10 * x1s * (np.sin(x2s))**3

    hyp = np.array([[-3.0], [-4.0], [6.0]])

    # Perform standard Guassian Process
    GPR = GP.GPRegression(x, y, noise=True)
    GPR.SetKernel('Gaussian')
    GPR.SetHyp(hyp)
    start = time.time()
    GPR.OptimizeHyp(maxnumlinesearch=20, random_starts=4)
    end = time.time()
    print('Standard GP done in %.8f seconds' % (end - start))
    gp_opttime = end - start
    GPR.GPR()
    GPR.Predict(xs)

    # Perform a Structured Kernel Interpolation regression
    KISSGP = GP.GPRegression(x, y, noise=True)
    KISSGP.GenerateGrid([m, m])
    KISSGP.Interpolate(scheme='cubic')
    KISSGP.SetKernel('Gaussian')
Example #32
0
    def train_with_GP(self):
        input_ph = tf.placeholder(shape=[batch_size, 28, 28, 1],
                                  dtype=tf.float32)
        label_ph = tf.placeholder(shape=[
            batch_size,
        ], dtype=tf.int32)

        predict = self.forward(input_ph)

        loss_tensor = tf.reduce_mean(predict.sg_ce(target=label_ph))

        # use to update network parameters
        optim = tf.sg_optim(loss_tensor, optim='Adam', lr=1e-3)

        # use saver to save a new model
        saver = tf.train.Saver()

        sess = tf.Session()
        with tf.sg_queue_context(sess):
            # inital
            tf.sg_init(sess)

            # train by GP guilding
            for e in range(max_ep):
                previous_loss = None
                for i in range(Mnist.train.num_batch):
                    [image_array, label_array
                     ] = sess.run([Mnist.train.image, Mnist.train.label])

                    if (e == 0 or e == 1
                        ):  # first and second epoch train no noisy image
                        loss = sess.run([loss_tensor, optim],
                                        feed_dict={
                                            input_ph: image_array,
                                            label_ph: label_array
                                        })[0]
                        print 'Baseline loss = ', loss
                    elif (
                            e == 2
                    ):  # third epoch train with gp image and original image
                        gpIn1 = np.squeeze(image_array)
                        gpIn2 = np.zeros((28, 28))
                        image_gp = GP(gpIn1, gpIn2, seed=0.8)
                        image_gp2 = image_gp[np.newaxis, ...]
                        image_gp2 = image_gp2[..., np.newaxis]
                        loss = sess.run([loss_tensor, optim],
                                        feed_dict={
                                            input_ph: image_array,
                                            label_ph: label_array
                                        })[0]
                        print 'GP without nosiy loss = ', loss
                        loss = sess.run([loss_tensor, optim],
                                        feed_dict={
                                            input_ph: image_gp2,
                                            label_ph: label_array
                                        })[0]
                        print 'GP loss = ', loss
                    else:  # other epoch train with gp evolution
                        gpIn1 = np.squeeze(image_array)
                        gpIn2 = np.zeros((28, 28))
                        image_gp = GP(gpIn1, gpIn2, seed=random.random())
                        image_gp2 = image_gp[np.newaxis, ...]
                        image_gp2 = image_gp2[..., np.newaxis]
                        loss = sess.run([loss_tensor, optim],
                                        feed_dict={
                                            input_ph: image_array,
                                            label_ph: label_array
                                        })[0]
                        print 'GP without nosiy loss = ', loss
                        loss = sess.run([loss_tensor, optim],
                                        feed_dict={
                                            input_ph: image_gp2,
                                            label_ph: label_array
                                        })[0]
                        print 'GP loss = ', loss
                        if loss < previous_loss:
                            for i in range(5):
                                loss = sess.run([loss_tensor, optim],
                                                feed_dict={
                                                    input_ph: image_gp2,
                                                    label_ph: label_array
                                                })[0]
                                gpIn1 = image_gp2
                                image_gp2[0, :, :,
                                          0] = GP(gpIn1[0, :, :, 0],
                                                  gpIn2,
                                                  seed=random.random())
                                print 'GP EV loss = ', loss

                previous_loss = loss
                saver.save(sess,
                           os.path.join(save_dir, 'gp_model'),
                           global_step=e)
        # close session
        sess.close()