def main():
    """
    This is the main function which calls all other functions to solve the
    optimisation model
    """
    # initialise the concreteModel
    RSO_model = ConcreteModel()

    # get the data input as objects
    Excel_file = 'Pyomo_RSO_Parameter_Input.xlsx'
    set_input, fixed_par, variable_par = data_construction(Excel_file)

    # set initialisation
    fset.set_initialisation(RSO_model, set_input)

    # parameter initialisation
    fpar.parameter_initialisation(RSO_model, fixed_par, variable_par)

    # variable initialisation
    fvar.variable_initialisation(RSO_model)

    # constraint initialisation
    fcon.constraint_definition(RSO_model)

    # set up the model
    opt = SolverFactory('cplex')

    results = opt.solve(RSO_model, tee=True, symbolic_solver_labels=True)

    RSO_model.solutions.store_to(results)

    results.write(filename='solution.yml')
    def save(self):
        '''Saves the summarised texts to the Summaries folder.

        Encodes saved files in UTF-8 Format - non-UTF-8 characters cannot be
        saved.
        
        Attributes:
            summaryNumber: The number of previous summaries + 1
            completeName: File name in the format of Summary_#_date.txt

        Raises:
            Exception: Any errors flagged and printed
        '''
        try:
            summaryNumber = str(v.settings["noOfSummaries"])
            completeName = ("Summary_#" + summaryNumber + "_" + v.date +
                            ".txt")
            with open(os.path.join('Summaries', completeName),
                      "w",
                      encoding="utf-8") as file:
                file.write(E.encrypt(self.summary))
            v.settings["noOfSummaries"] += 1
            v.save()
        except Exception as e:
            print(e)
示例#3
0
def build_max_size_array():
    start_day = Variables.get_start_day();
    end_day = Variables.get_end_day();
    max_size_array = {};
    
    for today in range(start_day, end_day + 1):
        max_size_array[Decimal(today)] = max_size_by_day(Decimal(today))
    
    return max_size_array
def deleteAll(tickerName):
    fileVariables = Variables()
    directory = fileVariables.directory
    fileEnding = fileVariables.returnFileEnding(tickerName)

    for i in fileEnding:
        try:
            os.remove(directory+i)
        except:
            pass
示例#5
0
    def commitPrefs(self):
        pref = vars.getVars("Pref")

        # On sauvegarde le nom du driver, car l'index peut changer
        pref["AUDIO_DRIVER"] = self.au.GetValue()

        # Max output channels available on the selected driver
        if self.radioSame.GetValue() == 1:
            pref["NCHNLS"] = self.currentDriverMaxChannels
        elif self.radioDiff.GetValue() == 1:
            pref["NCHNLS"] = int(self.chnls.GetValue())

        # Speakers setup
        pref["SPEAKERS_SETUP"] = self.numSpkChoices.GetSelection()
        if pref["SPEAKERS_SETUP"] == 0:
            vars.setVars("Speakers_setup", SETUP_STEREO)
            pref["NUM_SPEAKERS"] = len(SETUP_STEREO)
        elif pref["SPEAKERS_SETUP"] == 1:
            vars.setVars("Speakers_setup", SETUP_QUAD)
            pref["NUM_SPEAKERS"] = len(SETUP_QUAD)
        elif pref["SPEAKERS_SETUP"] == 2:
            vars.setVars("Speakers_setup", SETUP_OCTO_STEREO)
            pref["NUM_SPEAKERS"] = len(SETUP_OCTO_STEREO)
        elif pref["SPEAKERS_SETUP"] == 3:
            vars.setVars("Speakers_setup", SETUP_OCTO_DIAMAND)
            pref["NUM_SPEAKERS"] = len(SETUP_OCTO_DIAMAND)
        else:
            pass

        # OSC
        pref["OSCPORT"] = int(self.OSCPortText.GetValue())
示例#6
0
 def initSpkPos(self,pos):
     initPos = vars.getVars("Speakers_setup")
     spk = vars.getVars("Speakers")
     for i in spk:
         if i.isInside(pos):
             self.currentSpeaker = i
             spkIndex = spk.index(i)
             self.currentSpeaker.x = initPos[spkIndex][0] * self.size[0] # FL 02/09/2017
             self.currentSpeaker.y = initPos[spkIndex][1] * self.size[1] # FL 02/09/2017
             break
     self.speakerAdjusted() # FL 29/05/17
def start():
    v.stopped = False
    t1 = threading.Thread(target=serverLoop)
    t1.start()
    va.debug("Start Game")
    game()
    va.debug("After game")
    payload = {"username": v.username, "disconnect": True}
    jpayload = json.dumps(str(payload))
    r = requests.post(v.url, data=jpayload)
    v.stopped = True
示例#8
0
    def __init__(self, parent, pos=(0,0), size=(100,100), numSpeakers=2):
        wx.Panel.__init__(self, parent, pos=pos, size=size)
        self.SetBackgroundStyle(wx.BG_STYLE_CUSTOM)

        self.audio = vars.getVars("Audio")
        self.pos = None
        self.size = size
        self.currentCircle = None
        self.currentSpeaker = None
        self.isAList = False
        self.catch = False
        self.catchSpeaker = False
        self.shift = False
        self.alt = False
        self.s = False
        self.numSpeakers = numSpeakers # FL 29/05/17

        #OSC Variables
        self.incs = [0, 0, 0, 0]
        self.absPos = [0, 0, 0, 0] # FL 04/09/2017
        self.mode2 = False # FL 04/09/2017

        # Creation des cercles/sources
        self.blueCircle = Source(self.size[0]*BLUE_START[0], self.size[1]*BLUE_START[1], CIRCLE_RADIUS) 
        self.redCircle = Source(self.size[0]*RED_START[0], self.size[1]*RED_START[1], CIRCLE_RADIUS)


        speakers = []
        for i in range(self.numSpeakers):
            setup = vars.getVars("Speakers_setup")
            x, y = self.size[0]*setup[i][0], self.size[1]*setup[i][1] #FL 02/09/2017
            speakers.append(Speaker(x, y, SPEAKER_RADIUS))
        vars.setVars("Speakers", speakers)

#        print vars.getVars("Speakers")[0].c
        self.speakerAdjusted() # FL 29/05/17
      
        # méthode pour les controles
        self.Bind(wx.EVT_PAINT, self.onPaint)
        self.Bind(wx.EVT_LEFT_DOWN, self.onLeftDown)
        self.Bind(wx.EVT_LEFT_UP, self.onLeftUp)
        self.Bind(wx.EVT_RIGHT_DOWN, self.onRightDown)
        self.Bind(wx.EVT_MOTION, self.onMotion)
        
        self.Bind(wx.EVT_KEY_DOWN, self.onKeyDown)
        self.Bind(wx.EVT_KEY_UP, self.onKeyUp)
        
        self.on_timer()
示例#9
0
 def types(self, Trees, l, errores, var):
     correct = False
     for d in Trees:
         sr = ConvertFormat.ConvertFormat(l).Convert()
         if sr != False:
             result = d.PostOrder(d.root, sr)
             if result == None:
                 correct = True
                 if Search.Search().SearchTheVar(var, l) == True:
                     errores.append(
                         'Error la variable ya ha sido declarada anteriormente '
                         + l)
                 else:
                     au = l.split(' ')
                     if ReservedWords.ReservedWord().IsReservedWord(au[1]):
                         errores.append(
                             'Error no puedes nombrar a una varibale con una palabra reservada '
                             + l)
                     else:
                         var.append(
                             Variables.Variables(au[0], au[1],
                                                 self.AsignarValores(
                                                     au[0])))
                         errores.append('Cadena correcta ' + l)
                     break
         else:
             correct = False
     if correct == False:
         errores.append('Error en ' + l + ' Error de tipo')
     return errores, var
示例#10
0
    def drawWeights(self, t, ax, logId, command):
        matrix = self.refreshData(logId)

        # ax.clear()
        #Time Array
        tAr = matrix[0]
        id = 1
        for line in matrix[1:]:  # 0,120,20):
            mT = tAr
            if self.setRange and self.hardLim and len(line) > self.xRange:
                line = line[-self.xRange:]
                mT = tAr[-self.xRange:]
            setPixel = False
            if len(command) > 1:
                setPixel = command[1] == 'p'
            
            label = ''
            if setPixel:
                if id > Var.numBrainNeurons:
                    label = 'W {}'.format(id-Var.numBrainNeurons)
                else:
                    pixel = Var.idMapper(id)
                    label = 'Px({},{})'.format(pixel.x,pixel.y)
            else:
                label = 'W {}'.format(id)

            pLine = ax.plot(mT, line, label=label)
            ax.annotate(label + ": {:.2f}".format(float(line[-1])), xy=(mT[-1], line[-1]), size=11, va='center')
            id =id+1

        ax.set_title(self.commandsDic[command[0]])
        ax.set_ylabel('Weight')
示例#11
0
def drawWeights(i, ax, name):
    # print(i,ax,name)
    # coordinates = [[(float(i),j+((2.*i)/(j+1.)))for i in range(NENTRIES)]for j in range(NLINES)]
    with open(name + '.csv') as csv_file:
        # with open('left-weights.csv') as csv_file:
        csv_reader = csv.reader(csv_file, delimiter=',')
        matrix = []
        for i in csv_reader:
            matrix.append(i)
        # print(matrix)
        NLINES = len(matrix[1])
        NENTRIES = len(matrix)

        # NLINES = 17
        coordinates = [[(matrix[i][0], matrix[i][j])
                        for i in range(1, NENTRIES)]for j in range(1, NLINES)]
        # print(coordinates[0])

    # bins = np.arange(0,40,5)
    ax.clear()
    xLeft = 0.
    xRight = 0.
    for i in range(len(coordinates)):  # 0,120,20):
        line = coordinates[i]
        xA = []
        yA = []
        for (x, y) in line:
            xA.append(x)
            yA.append(float(y))
        # deviation = np.std(yA)

        # print('i: {} corresponds to Pixel({},{}) '.format(i, NUM_PIXEL_X -
        #                                                   (i_invers % (NUM_PIXEL_Y)), int(math.floor(i_invers/NUM_PIXEL_Y))))

        # if deviation>1:
        if i > 9:
            pLine = ax.plot(xA, yA, label='jointNeuron({})'.format(i))
            ax.annotate('jointNeuron({}): {:.2f} '.format(i, yA[len(
                yA)-1]), xy=(xA[len(xA)-1], yA[len(yA)-1]), size=11, va='center')

        else:
            p = Var.idMapper(i+1)
            pLine = ax.plot(xA, yA, label='Pixel({},{}) '.format(p.x, p.y))
            # if deviation > 3:

            ax.annotate('Pixel({},{}): {:.2f} '.format(p.x, p.y, yA[len(
                yA)-1]), xy=(xA[len(xA)-1], yA[len(yA)-1]), size=11, va='center')

        # plt.hist(yA,bins,histtype='bar',rwidth=0.3)
        if i == 0:
            if len(xA) > xRange:
                xLeft = xA[-xRange]
            xRight = xA[-1]

    ax.set_title(name[:-8])
    # ax.set_xlabel('Time')
    ax.set_ylabel('Weight')

    if setXLim and xLeft > 0.:
        ax.set_xlim(left=float(xLeft), right=float(xRight))
示例#12
0
 def radiusZone(self, e):
     x = e.value
     speakers = vars.getVars("Speakers")
     for i in range(len(speakers)):
         speakers[i].setZoneRad(x)
     self.surface.speakerAdjusted()
     self.surface.Refresh()
示例#13
0
 def masterAmp(self, e):
     x = e.value
     audio = vars.getVars("Audio")
     if x == -60:
         audio.player.mul = 0
     else:
         audio.player.mul = self.convertToMul(x)  #FL 22/05/17
示例#14
0
    def distance(self,pos):    
        for i in range(self.numSpeakers):
            SpkPos = vars.getVars("Speakers")[i].getCenter()
            SpkRad = vars.getVars("Speakers")[i].getZoneRad()
            dist = math.sqrt(math.pow((pos[0]-SpkPos[0]),2) + math.pow((pos[1]-SpkPos[1]),2))
            amp = max(0, 1. - dist / float(SpkRad))
                

            if self.isAList:
                self.audio.setBlueAmp(i,amp)
                self.audio.setRedAmp(i,amp)

            elif self.currentCircle == self.blueCircle:
                self.audio.setBlueAmp(i,amp)

            elif self.currentCircle == self.redCircle:
                self.audio.setRedAmp(i,amp)
示例#15
0
def server():        
    t = time.clock()
    payload = {"username": v.username, "position": (v.posx, v.posy)}
    jpayload = json.dumps(str(payload))
    try:
        r = requests.post(v.url, data=jpayload)
    except Exception as e:
        va.debug(e)
    # Response, status etc
    if r.status_code == 200:
        v.data = literal_eval(r.text)
        v.fetched = True
    else:
        print(r.status_code)
        print(r.text)
    #print(v.data)
    v.fetchTime = time.clock() - t
示例#16
0
def downloadAll(tickerName):
    fileVariables = Variables()
    directory = fileVariables.directory
    fileEnding = fileVariables.returnFileEnding(tickerName)
    urlList = fileVariables.returnUrlList(tickerName)
    
    if not os.path.exists(directory):
        os.makedirs(directory)
     
    counter = 0
    for i in urlList:
        try:
            request.urlretrieve(i,directory + fileEnding[counter])
            counter += 1
         
        except:
            print(i + " download failed")
示例#17
0
def get_corr_hist(tree, mva_name, variable, cut, type, mva_binning):
    nutple_varname = variable

    # we would not find "jet_pt_1_" in the variables list,
    # so we transform it to "jet_pt[1]" if it matches the
    # pattern
    match = IndexMatch.search(variable)
    if match:
        nutple_varname = "%s[%s]" % (variable[:-3], match.group(1))

    if nutple_varname in Variables.Variables:
        draw_var = "%s:%s" % (mva_name, Variables.conv_unit(nutple_varname))
        # conv_unit returns jet_pt[1] in the above case, transform it back
        draw_var = draw_var.replace(nutple_varname, variable)

        binning = "(%s,%d,%d,%d)" % (Variables.binning(nutple_varname),
                                     mva_binning[0], mva_binning[1],
                                     mva_binning[2])

        unit = Variables.unit(nutple_varname)
        if unit and unit != "":
            xtitle = "%s [%s]" % (Variables.title(nutple_varname), unit)
        else:
            xtitle = Variables.title(nutple_varname)
    else:
        draw_var = "%s:%s" % (mva_name, variable)
        binning = ""
        xtitle = variable

    hist_name = variable + "_hist_" + type
    draw_cmd = "%s>>%s%s" % (draw_var, hist_name, binning)

    #tree.Draw(draw_cmd, "%s * (mc_weight * sf_total * xs_weight)" % cut)
    tree.Draw(draw_cmd, "%s * (mc_weight)" % cut)
    hist = gDirectory.Get(hist_name)
    hist.SetDirectory(gROOT)

    hist.SetTitle(";%s;%s output" % (xtitle, mva_name))

    for xbin in xrange(hist.GetNbinsX() + 2):
        for ybin in xrange(hist.GetNbinsY() + 2):
            if hist.GetBinContent(xbin, ybin) < 0:
                hist.SetBinContent(xbin, ybin, 0)

    return hist
示例#18
0
    def onSpeaker(self,pos):
        spk = vars.getVars("Speakers")
        for i in spk:
            if i.isInside(pos):
                self.currentSpeaker = i
                self.catchSpeaker = True
                break

            else:
                self.currentSpeaker = None
                self.catchSpeaker = False
示例#19
0
    def _reset_param(self):
        Variables.clear_default_domain()
        Variables.set_default_domain({'H_bn_t':lv.H_bn,'H_dp_t':lv.H_dp,'H_ap_t':lv.H_ap,\
                                      'H_tt_t':lv.H_tt,'UA_tt':lv.UA,'H_bn_tt':lv.H_bn,\
                                      'H_dp_tt':lv.H_dp,'H_ap_tt':lv.H_ap,'H_tt_tt':lv.H_tt,\
                                      'H_bn_0':lv.H_bn,'H_dp_0':lv.H_dp,'H_ap_0':lv.H_ap,'H_tt_0':lv.H_tt})

        fH_bn_t_UAtt_H_bn_tt = Factor(('H_bn_t','UA_tt','H_bn_tt'))
        fH_bn_t_UAtt_H_bn_tt.zero()
        for inst in Utils.inst_filling({'H_bn_t':lv.H_bn,'UA_tt':lv.UA,'H_bn_tt':lv.H_bn}):
            if (('bn' in inst['UA_tt'] or inst['H_bn_t'] == 'o') and inst['H_bn_tt'] == 'o') or\
            (('bn' not in inst['UA_tt'] and inst['H_bn_t'] == 'x') and inst['H_bn_tt'] == 'x'):
                fH_bn_t_UAtt_H_bn_tt[inst] = 1
        fH_dp_t_UAtt_H_dp_tt = Factor(('H_dp_t','UA_tt','H_dp_tt'))
        fH_dp_t_UAtt_H_dp_tt.zero()
        for inst in Utils.inst_filling({'H_dp_t':lv.H_dp,'UA_tt':lv.UA,'H_dp_tt':lv.H_dp}):
            if (('dp' in inst['UA_tt'] or inst['H_dp_t'] == 'o') and inst['H_dp_tt'] == 'o') or\
            (('dp' not in inst['UA_tt'] and inst['H_dp_t'] == 'x') and inst['H_dp_tt'] == 'x'):
                fH_dp_t_UAtt_H_dp_tt[inst] = 1
        fH_ap_t_UAtt_H_ap_tt = Factor(('H_ap_t','UA_tt','H_ap_tt'))
        fH_ap_t_UAtt_H_ap_tt.zero()
        for inst in Utils.inst_filling({'H_ap_t':lv.H_ap,'UA_tt':lv.UA,'H_ap_tt':lv.H_ap}):
            if (('ap' in inst['UA_tt'] or inst['H_ap_t'] == 'o') and inst['H_ap_tt'] == 'o') or\
            (('ap' not in inst['UA_tt'] and inst['H_ap_t'] == 'x') and inst['H_ap_tt'] == 'x'):
                fH_ap_t_UAtt_H_ap_tt[inst] = 1
        fH_tt_t_UAtt_H_tt_tt = Factor(('H_tt_t','UA_tt','H_tt_tt'))
        fH_tt_t_UAtt_H_tt_tt.zero()
        for inst in Utils.inst_filling({'H_tt_t':lv.H_tt,'UA_tt':lv.UA,'H_tt_tt':lv.H_tt}):
            if (('tt' in inst['UA_tt'] or inst['H_tt_t'] == 'o') and inst['H_tt_tt'] == 'o') or\
            (('tt' not in inst['UA_tt'] and inst['H_tt_t'] == 'x') and inst['H_tt_tt'] == 'x'):
                fH_tt_t_UAtt_H_tt_tt[inst] = 1
        fHt_UAtt_Htt = fH_bn_t_UAtt_H_bn_tt * fH_dp_t_UAtt_H_dp_tt * fH_ap_t_UAtt_H_ap_tt * fH_tt_t_UAtt_H_tt_tt
        ls.store_model(fHt_UAtt_Htt,'_factor_Ht_UAtt_Htt.model')
        
        fGbn_Ht_SAtt_UAtt = CPT(Factor(('H_bn_t','H_dp_t','H_ap_t','H_tt_t','UA_tt')),child='UA_tt',cpt_force=True)
        factor_template = {'G_bn':lv.G_bn,'SA':lv.SA}
        for factor in Utils.inst_filling(factor_template):
            ls.store_model(fGbn_Ht_SAtt_UAtt,('_factor_%s_%s.model'%(factor['G_bn'],factor['SA'])).replace(':','-'))
        del fGbn_Ht_SAtt_UAtt
示例#20
0
    def onPaint(self, e):
        w,h = self.GetSize()

        dc = wx.AutoBufferedPaintDC(self)
        
        # Surface
        dc.SetBrush(wx.Brush(COLOR_BACK))
        dc.DrawRectangle(0, 0, w+1, h+1)
        
        # Le quadrillage
        dc.SetPen(wx.Pen(COLOR_GRID, 2))
#        for i in range(0, 600, 25):
#            dc.DrawLine(0, i, w, i)
#            dc.DrawLine(i, 0, i, h)

        dc.SetBrush(wx.Brush(COLOR_GRID,style=wx.TRANSPARENT))
        for i in range(8):
            dc.DrawCircle(w/2,h/2,GRID_CIRCLE_RADIUS*(7*i))
#            dc.DrawCircle(300,300,CIRCLE_RADIUS*(7*i))

        for i in range(self.numSpeakers):
            vars.getVars("Speakers")[i].draw(dc, COLOR_AV)
            vars.getVars("Speakers")[i].drawZone(dc, COLOR_AV)

        
        # ecriture de valeurs
        if self.catch:            
            # conversion des positions X et Y entre 0 et 1
            x = self.pos[0] / float(w)
            y = 1.- self.pos[1] / float(h)
            
            # affiche la position normalisee du pointeur
            dc.DrawText("%.3f, %.3f" % (x,y), 10, 10)
            
        # Les cercles
        self.blueCircle.draw(dc,COLOR_BLUE)
        self.redCircle.draw(dc,COLOR_RED)
示例#21
0
 def quit(self, e):
     if os.path.isfile(EMPTY_AUDIO_FILE):
         os.remove(EMPTY_AUDIO_FILE)
     audio = vars.getVars("Audio")
     audio.server.stop()
     try:
         self.audio.unregisterMeter()
     except:
         pass
     # FL - START 22/05/17
     try:
         self.Destroy()
     except:
         pass
     raise SystemExit
示例#22
0
def getWholeSeason(year):
    vars = Variables.Variables()
    teams = vars.teams
    
    months = ['october', 'november', 'december', 'january', 'febuary', 'march', 'april' ]
#     months = ['february', 'march', 'april' ]
    
    for month in months:
        NBAurl = "http://www.basketball-reference.com/leagues/NBA_" + str(year) + "_games-" + month + ".html" 
        
        try:
            tempWebFile = request.urlopen(NBAurl).read()
        except:
            return None
        tempData = BeautifulSoup(tempWebFile,"lxml")
        lines = tempData.find_all('tr')
        parseWholeSeason(lines)
示例#23
0
 def loadVars(self):
   '''
   Fills dictionary with variables to have their U Q'd
   Input: none
   Output: none
   '''
   print '\nLoading uncertain variables...'
   uVars = self.input_file('Variables/names','').split(' ')
   self.varDict={}
   for var in uVars:
     path=self.input_file('Variables/'+var+'/path',' ')
     dist=self.input_file('Variables/'+var+'/dist',' ')
     args=self.input_file('Variables/'+var+'/args',' ').split(' ')
     for a,arg in enumerate(args):
       args[a]=float(arg)
     self.varDict[var]=Variables.newVar(dist,var,path)
     self.varDict[var].setDist(args)
 def loadVars(self):
   '''
   Fills dictionary with variables to have their U Q'd
   Input: none
   Output: none
   '''
   print '\nLoading uncertain variables...'
   uVars = self.input_file('Variables/names','').split(' ')
   self.varDict={}
   for var in uVars:
     path=self.input_file('Variables/'+var+'/path',' ')
     dist=self.input_file('Variables/'+var+'/dist',' ')
     args=self.input_file('Variables/'+var+'/args',' ').split(' ')
     for a,arg in enumerate(args):
       args[a]=float(arg)
     self.varDict[var]=Variables.newVar(dist,var,path)
     self.varDict[var].setDist(args)
示例#25
0
 def loadVars(self):
     print '\nLoading uncertain variables...'
     uVars = self.input_file('Variables/names', '').split(' ')
     self.varDict = {}
     for var in uVars:
         path = self.input_file('Variables/' + var + '/path', ' ')
         dist = self.input_file('Variables/' + var + '/dist', ' ')
         args = self.input_file('Variables/' + var + '/args',
                                ' ').split(' ')
         #print 'current:',var
         for a, arg in enumerate(args):
             #print " ",a,arg
             args[a] = float(arg)
         impwt = self.input_file('Variables/' + var + '/weight', 1.0)
         self.varDict[var] = Variables.VariableFactory(
             dist, var, path, impwt)
         self.varDict[var].setDist(args)
示例#26
0
 def loadVars(self):
     print '\nLoading uncertain variables...'
     uVars = self.input_file('Variables/names', '').split(' ')
     self.varDict = {}
     doAnis = self.input_file('Sampler/SC/anis', 0)
     for var in uVars:
         path = self.input_file('Variables/' + var + '/path', ' ')
         dist = self.input_file('Variables/' + var + '/dist', ' ')
         args = self.input_file('Variables/' + var + '/args',
                                ' ').split(' ')
         for a, arg in enumerate(args):
             args[a] = float(arg)
         if doAnis:
             impwt = self.input_file('Variables/' + var + '/weight', 1.0)
         else:
             impwt = 1
         self.varDict[var] = Variables.VariableFactory(
             dist, var, path, impwt)
         self.varDict[var].setDist(args)
示例#27
0
def testOrthogonal(vartype,polyords=[2,4],quadord=3,target=0,verbose=False):
  import Variables as v
  var = v.newVar(vartype,'x')
  var.setDist([0,1.0])
  var.setQuadrature(order=quadord)
  tot = 0
  for l,val in enumerate(var.pts):
    tot+=var.evalNormPoly(val,polyords[0])*\
         var.evalNormPoly(val,polyords[1])*\
         var.wts[l]
  if abs(tot-target)<1.0e-9:
    if verbose:
      print 'Passed orders',polyords,'with quad order',quadord
    return True
  else:
    if verbose:
      print 'ERROR!  Failed orders',polyords,'with quad order',quadord
      print '    Got',tot,'instead of',target
    return False
示例#28
0
def getAllFFA(year):
    var = Variables.Variables()
    teams = var.teams
    offensiveRating = []
    
    for (i,j) in teams.items():
        """Do an insert sort type of thing"""
        ffaTemp = FFAteam(i,2017)
        if(len(offensiveRating) == 0): 
            offensiveRating.append([i,j,ffaTemp])
            continue
        
        for k in range(0,len(offensiveRating)):
            if(ffaTemp > offensiveRating[k][2]):
                offensiveRating.insert(k, [i, j, ffaTemp])
                break
            if(k == len(offensiveRating) - 1):
                offensiveRating.insert(k + 1, [i, j, ffaTemp])
                break

    return offensiveRating
示例#29
0
def testOrthogonal(vartype,
                   polyords=[2, 4],
                   quadord=3,
                   target=0,
                   verbose=False):
    import Variables as v
    var = v.newVar(vartype, 'x')
    var.setDist([0, 1.0])
    var.setQuadrature(order=quadord)
    tot = 0
    for l, val in enumerate(var.pts):
        tot+=var.evalNormPoly(val,polyords[0])*\
             var.evalNormPoly(val,polyords[1])*\
             var.wts[l]
    if abs(tot - target) < 1.0e-9:
        if verbose:
            print 'Passed orders', polyords, 'with quad order', quadord
        return True
    else:
        if verbose:
            print 'ERROR!  Failed orders', polyords, 'with quad order', quadord
            print '    Got', tot, 'instead of', target
        return False
示例#30
0
def testOrthogonal(vartype,
                   polyords=[2, 4],
                   quadord=3,
                   target=0,
                   verbose=False):
    import Variables as v
    var = v.newVar(vartype, 'x')
    var.setDist([0, 1.0])
    #print 'Check variable!'
    #print '  mean:',var.dist.mean()
    #print '   std:',var.dist.std()
    var.setQuadrature(order=quadord)
    tot = 0
    for l, val in enumerate(var.pts):
        #tot+=var.evalNormPoly(val,polyords[0])*\
        #     var.evalNormPoly(val,polyords[1])*\
        #     np.exp(-val*val)*\
        tot+=orthoNorms(var,val,polyords)*\
             var.wts[l]*\
             var.probdens(val)
    if abs(tot - target) < 1.0e-9:
        #print 'Passed orders',polyords,'with quad order',quadord
        return True
    else:
        if verbose:
            print 'checking', polyords
            print 'ERROR!  Failed orders', polyords, 'with quad order', quadord
            print '    Got', tot, 'instead of', target
            print '  val  :', val
            print '  poly1:', var.evalNormPoly(val, polyords[0])
            print '  poly2:', var.evalNormPoly(val, polyords[1])
            print '  wts  :', var.wts[l]
            print '  pdens:', var.probdens(val)
            print '  norms', 1. / (var.norm(polyords[0])) * 1. / (var.norm(
                polyords[1]))
        return False
示例#31
0
    def MAP_learn(self,make_correction_model=False):
        actCounts = {'bn':{'correct':0,'incorrect':0},
                             'dp':{'correct':0,'incorrect':0},
                             'ap':{'correct':0,'incorrect':0},
                             'tt':{'correct':0,'incorrect':0},
                             'yes':{'correct':0,'incorrect':0},
                             'no':{'correct':0,'incorrect':0},
                             'non-understanding':0}

        Variables.clear_default_domain()
        Variables.set_default_domain({'H_bn_t':lv.H_bn,'H_dp_t':lv.H_dp,'H_ap_t':lv.H_ap,\
                                      'H_tt_t':lv.H_tt,'UA_tt':lv.UA,'H_bn_tt':lv.H_bn,\
                                      'H_dp_tt':lv.H_dp,'H_ap_tt':lv.H_ap,'H_tt_tt':lv.H_tt,\
                                      'H_bn_0':lv.H_bn,'H_dp_0':lv.H_dp,'H_ap_0':lv.H_ap,\
                                      'H_tt_0':lv.H_tt})

        fHt_UAtt_Htt = ls.load_model('_factor_Ht_UAtt_Htt.model')

        for d, dialog in enumerate(lc.Corpus(self.data,prep=self.prep).dialogs()):
            if len(dialog.turns) > 40:
                continue
            
#            c = ((len(dialog.turns)-1)/5)-1
#            if c < 0: c = 0
            avg_cs = reduce(operator.add,map(lambda x:x['CS'],dialog.turns))/len(dialog.turns)
            if avg_cs > 0.7: c = 0
            elif avg_cs > 0.5: c = 1
            elif avg_cs > 0.3: c = 2
            else: c = 3
            
            self.q_class[c] += 1
            
            cm_ua_template = self.cm_ua_template[c]
            co_cs = self.co_cs[c]
            inco_cs = self.inco_cs[c]
            
#            MAP decoding
#            print 'processing dialog #%d(%s)...'%(d,dialog.id)
            Variables.change_variable('H_bn_0',lv.H_bn)
            Variables.change_variable('H_dp_0',lv.H_dp)
            Variables.change_variable('H_ap_0',lv.H_ap)
            Variables.change_variable('H_tt_0',lv.H_tt)
        
            dialog_factors = []
            for t, turn in enumerate(dialog.abs_turns):
                tmp_fHt_UAtt_Htt = Factor(('H_bn_%s'%t,'H_dp_%s'%t,'H_ap_%s'%t,'H_tt_%s'%t,\
                                           'UA_%s'%(t+1),'H_bn_%s'%(t+1),'H_dp_%s'%(t+1),\
                                           'H_ap_%s'%(t+1),'H_tt_%s'%(t+1)),
                        new_domain_variables={'UA_%s'%(t+1):lv.UA,'H_bn_%s'%(t+1):lv.H_bn,\
                                              'H_dp_%s'%(t+1):lv.H_dp,'H_ap_%s'%(t+1):lv.H_ap,\
                                              'H_tt_%s'%(t+1):lv.H_tt})
                tmp_fHt_UAtt_Htt[:] = fHt_UAtt_Htt[:]
        #            tmp_fHt_UAtt_Htt = fHt_UAtt_Htt.copy_rename({'H_bn_t':'H_bn_%s'%i,'H_dp_t':'H_dp_%s'%i,'H_ap_t':'H_ap_%s'%i,'H_tt_t':'H_tt_%s'%i,'UA_tt':'UA_%s'%(i+1),'H_bn_tt':'H_bn_%s'%(i+1),'H_dp_tt':'H_dp_%s'%(i+1),'H_ap_tt':'H_ap_%s'%(i+1),'H_tt_tt':'H_tt_%s'%(i+1)})            
                tmp_fGbn_Ht_SAtt_UAtt = Factor(('H_bn_%s'%t,'H_dp_%s'%t,'H_ap_%s'%t,'H_tt_%s'%t,'UA_%s'%(t+1)))
        #            tmp_fGbn_Ht_SAtt_UAtt = Factor(('H_bn_%s'%i,'H_dp_%s'%i,'H_ap_%s'%i,'H_tt_%s'%i,'UA_%s'%(i+1)),new_domain_variables={'UA_%s'%(i+1):UA,'H_bn_%s'%i:H_bn,'H_dp_%s'%i:H_dp,'H_ap_%s'%i:H_ap,'H_tt_%s'%i:H_tt})
                try:
                    tmp_fGbn_Ht_SAtt_UAtt[:] = \
                    ls.load_model(('_factor_%s_%s.model'%(dialog.abs_goal['G_bn'],turn['SA'][0])).replace(':','-'))[:]
                except:
                    print ('Error:cannot find _factor_%s_%s.model'%(dialog.abs_goal['G_bn'],turn['SA'][0])).replace(':','-')
                    exit()
                tmp_fUAtt_Ott = Factor(('UA_%s'%(t+1),))
                tmp_fUAtt_Ott[:] = lo.getObsFactor(turn,use_cs=True)[:]
                factor = tmp_fHt_UAtt_Htt * tmp_fGbn_Ht_SAtt_UAtt * tmp_fUAtt_Ott
                dialog_factors.append(factor.copy(copy_domain=True))
            
            jfr = JFR(SFR(dialog_factors))
            jfr.condition({'H_bn_0':'x','H_dp_0':'x','H_ap_0':'x','H_tt_0':'x'})
            jfr.map_calibrate()

#            Populate error table using inferred and observed actions
            for t, turn in enumerate(dialog.turns):
                rf = jfr.factors_containing_variable('UA_%s'%(t+1))
                max_ua = max(zip(rf[0].insts(),rf[0][:]),key=operator.itemgetter(1))[0][-1]
                if max_ua == 'yes' and 'no' in turn['UA'] or\
                max_ua == 'no' and 'yes' in turn['UA']:
                    print 'turn %d(%s): '%(t,turn) + max_ua + ' vs.' + ','.join(turn['UA'])
                obs_ua_template = []
                for act in turn['UA']:
                    if act.find('I:bn') == 0:# and max_ua.find('I:bn') > -1 and not dialog.goal['G_bn'] == '':
                        if dialog.goal['G_bn'] == act.split(':')[-1]: 
                            obs_ua_template.append('I:bn:o')
                            actCounts['bn']['correct'] += 1
                        else: 
                            obs_ua_template.append('I:bn:x')
                            actCounts['bn']['incorrect'] += 1
                            if dialog.goal['G_bn'] not in self.cm_bn:
                                self.cm_bn[dialog.goal['G_bn']] = {act.split(':')[-1]:1}
                            elif act.split(':')[-1] not in self.cm_bn[dialog.goal['G_bn']]:
                                self.cm_bn[dialog.goal['G_bn']][act.split(':')[-1]] = 1
                            else:
                                self.cm_bn[dialog.goal['G_bn']][act.split(':')[-1]] += 1
#                            try:
#                                self.cm_bn[dialog.goal['G_bn']][act.split(':')[-1]] += 1
#                            except:
#                                try: self.cm_bn[dialog.goal['G_bn']][act.split(':')[-1]] = 1
#                                except: self.cm_bn[dialog.goal['G_bn']] = {act.split(':')[-1]:1}
                    elif act.find('I:dp') == 0:# and max_ua.find('I:dp') > -1 and not dialog.goal['G_dp'] == '':
                        if dialog.goal['G_dp'] == act.split(':')[-1]: 
                            obs_ua_template.append('I:dp:o')
                            actCounts['dp']['correct'] += 1
                        else: 
                            obs_ua_template.append('I:dp:x')
                            actCounts['dp']['incorrect'] += 1
                            if dialog.goal['G_dp'] not in self.cm_p:
                                self.cm_p[dialog.goal['G_dp']] = {act.split(':')[-1]:1}
                            elif act.split(':')[-1] not in self.cm_p[dialog.goal['G_dp']]:
                                self.cm_p[dialog.goal['G_dp']][act.split(':')[-1]] = 1
                            else:
                                self.cm_p[dialog.goal['G_dp']][act.split(':')[-1]] += 1
#                            try:
#                                self.cm_p[dialog.goal['G_dp']][act.split(':')[-1]] += 1
#                            except:
#                                try: self.cm_p[dialog.goal['G_dp']][act.split(':')[-1]] = 1
#                                except: self.cm_p[dialog.goal['G_dp']] = {act.split(':')[-1]:1}
                    elif act.find('I:ap') == 0:# and max_ua.find('I:ap') > -1 and not dialog.goal['G_ap'] == '':
                        if dialog.goal['G_ap'] == act.split(':')[-1]: 
                            obs_ua_template.append('I:ap:o')
                            actCounts['ap']['correct'] += 1
                        else: 
                            obs_ua_template.append('I:ap:x')
                            actCounts['ap']['incorrect'] += 1
                            if dialog.goal['G_ap'] not in self.cm_p:
                                self.cm_p[dialog.goal['G_ap']] = {act.split(':')[-1]:1}
                            elif act.split(':')[-1] not in self.cm_p[dialog.goal['G_ap']]:
                                self.cm_p[dialog.goal['G_ap']][act.split(':')[-1]] = 1
                            else:
                                self.cm_p[dialog.goal['G_ap']][act.split(':')[-1]] += 1
#                            try:
#                                self.cm_p[dialog.goal['G_ap']][act.split(':')[-1]] += 1
#                            except:
#                                try: self.cm_p[dialog.goal['G_ap']][act.split(':')[-1]] = 1
#                                except: self.cm_p[dialog.goal['G_ap']] = {act.split(':')[-1]:1}
                    elif act.find('I:tt') == 0:# and max_ua.find('I:tt') > -1 and not dialog.goal['G_tt'] == '':
                        if dialog.goal['G_tt'] == act.split(':')[-1]: 
                            obs_ua_template.append('I:tt:o')
                            actCounts['tt']['correct'] += 1
                        else: 
                            obs_ua_template.append('I:tt:x')
                            actCounts['tt']['incorrect'] += 1
                            if dialog.goal['G_tt'] not in self.cm_tt:
                                self.cm_tt[dialog.goal['G_tt']] = {act.split(':')[-1]:1}
                            elif act.split(':')[-1] not in self.cm_tt[dialog.goal['G_tt']]:
                                self.cm_tt[dialog.goal['G_tt']][act.split(':')[-1]] = 1
                            else:
                                self.cm_tt[dialog.goal['G_tt']][act.split(':')[-1]] += 1
#                            try:
#                                self.cm_tt[dialog.goal['G_tt']][act.split(':')[-1]] += 1
#                            except:
#                                try: self.cm_tt[dialog.goal['G_tt']][act.split(':')[-1]] = 1
#                                except: self.cm_tt[dialog.goal['G_tt']] = {act.split(':')[-1]:1}
#                    elif act.find('I:') == 0:
#                        obs_ua_template.append(':'.join(act.split(':')[:-1])) 
                    else:
                        obs_ua_template.append(act)
#                if ','.join(sorted(obs_ua_template)).find('yes,') > -1 or\
#                ','.join(sorted(obs_ua_template)).find(',yes') > -1 or\
#                ','.join(sorted(obs_ua_template)).find('no,') > -1 or\
#                ','.join(sorted(obs_ua_template)).find(',no') > -1:
#                    print 'Check'
#                    print dialog.id
#                    print t
#                    print ','.join(sorted(obs_ua_template))
                
                if max_ua not in cm_ua_template:
                    cm_ua_template[max_ua] = {','.join(sorted(obs_ua_template)):1}
                elif ','.join(sorted(obs_ua_template)) not in cm_ua_template[max_ua]:
                    cm_ua_template[max_ua][','.join(sorted(obs_ua_template))] = 1
                else:
                    cm_ua_template[max_ua][','.join(sorted(obs_ua_template))] += 1
#                try:
#                    cm_ua_template[max_ua][','.join(sorted(obs_ua_template))] += 1
#                except:
#                    try: cm_ua_template[max_ua][','.join(sorted(obs_ua_template))] = 1
#                    except: cm_ua_template[max_ua] = {','.join(sorted(obs_ua_template)):1}
                
                if len(obs_ua_template) == 1:
#                    try:
                    if len(obs_ua_template[0].split(':')) == 3:
                        dummy,field,val = obs_ua_template[0].split(':')
                        if val == 'o':
                            co_cs[field].append(turn['CS'])
                        else:
                            inco_cs[field].append(turn['CS'])
#                    except:
                    else:
                        if obs_ua_template[0] == 'yes':
                            if config.getboolean('UserSimulation','extendedSystemActionSet'):
                                if dialog.abs_turns[t]['SA'][0] in ['C:bn:o','C:dp:o','C:ap:o','C:tt:o']:
                                    co_cs['yes'].append(turn['CS'])
                                elif dialog.abs_turns[t]['SA'][0] in ['C:bn:x','C:dp:x','C:ap:x','C:tt:x']:
                                    inco_cs['yes'].append(turn['CS'])
                            else:
                                if dialog.abs_turns[t]['SA'][0] == 'C:o':
                                    co_cs['yes'].append(turn['CS'])
                                elif dialog.abs_turns[t]['SA'][0] == 'C:x':
                                    inco_cs['yes'].append(turn['CS'])
                        elif obs_ua_template[0] == 'no':
                            if config.getboolean('UserSimulation','extendedSystemActionSet'):
                                if dialog.abs_turns[t]['SA'][0] in ['C:bn:x','C:dp:x','C:ap:x','C:tt:x']:
                                    co_cs['no'].append(turn['CS'])
                                elif dialog.abs_turns[t]['SA'][0] in ['C:bn:o','C:dp:o','C:ap:o','C:tt:o']:
                                    inco_cs['no'].append(turn['CS'])
                            else:
                                if dialog.abs_turns[t]['SA'][0] == 'C:x':
                                    co_cs['no'].append(turn['CS'])
                                elif dialog.abs_turns[t]['SA'][0] == 'C:o':
                                    inco_cs['no'].append(turn['CS'])
                else:
                    try:
                        if make_correction_model and len(obs_ua_template) == 2 and 'no' in obs_ua_template:
                            if ','.join(obs_ua_template).find(':x') > -1:
                                inco_cs['correction'].append(turn['CS'])
                            else:
                                co_cs['correction'%len(obs_ua_template)].append(turn['CS'])
                        else:    
                            if ','.join(obs_ua_template).find(':x') > -1:
                                inco_cs['multi%d'%len(obs_ua_template)].append(turn['CS'])
                            else:
                                co_cs['multi%d'%len(obs_ua_template)].append(turn['CS'])
                    except:
                        print len(obs_ua_template)

        for c in range(self.q_class_max):
            cm_ua_template = self.cm_ua_template[c]
            co_cs = self.co_cs[c]
            inco_cs = self.inco_cs[c]

            co_cs['single'] = co_cs['bn'] + co_cs['dp'] + co_cs['ap'] +\
             co_cs['tt'] + co_cs['yes'] + co_cs['no'] 
            inco_cs['single'] = inco_cs['bn'] + inco_cs['dp'] + inco_cs['ap'] +\
             inco_cs['tt'] + inco_cs['yes'] + inco_cs['no'] 
    
            for n in range(2,6,1):
                co_cs['multi'] += co_cs['multi%d'%n]
                inco_cs['multi'] += inco_cs['multi%d'%n]
    
            co_cs['total'] = co_cs['single'] + co_cs['multi']
            inco_cs['total'] = inco_cs['single'] + inco_cs['multi']
             
        print 'Writing parameters...'
        def make_dist(ft):
            tot = sum(ft.values())
            for k, v in ft.items(): ft[k] = float(v)/tot
            return ft

        def make_dists(cm):
            for key in cm.keys():
                cm[key] = make_dist(cm[key])
            return cm

        def generate_cs_pd(cs):
            cs_pd = {}
            for key in cs.keys():
                cs_pd[key] = {}
                for val in cs[key]:
                    try:
                        cs_pd[key][int(val/0.01)/float(100)] += 1
                    except:
                        cs_pd[key][int(val/0.01)/float(100)] = 1
            return make_dists(cs_pd)
            
        ls.store_model(make_dists(self.cm_bn),'_confusion_matrix_bn.model')
        ls.store_model(make_dists(self.cm_p),'_confusion_matrix_p.model')
        ls.store_model(make_dists(self.cm_tt),'_confusion_matrix_tt.model')
        ls.store_model(make_dist(self.q_class),'_quality_class.model')
        for c in range(self.q_class_max): 
            ls.store_model(make_dists(self.cm_ua_template[c]),'_confusion_matrix_ua_class_%d.model'%c)
            ls.store_model(self.co_cs[c],'_correct_confidence_score_class_%d.model'%c)
            ls.store_model(self.inco_cs[c],'_incorrect_confidence_score_class_%d.model'%c)
            ls.store_model(generate_cs_pd(self.co_cs[c]),'_correct_confidence_score_prob_dist_class_%d.model'%c)
            ls.store_model(generate_cs_pd(self.inco_cs[c]),'_incorrect_confidence_score_prob_dist_class_%d.model'%c)

        pprint.pprint(actCounts)
示例#32
0
def writeVarToDisk():
    vars.writeCeciliaPrefsToFile()
示例#33
0
                              pady=10,
                              command=ocr.lift)

        # Packing the buttons and making them fill the bar on the X-Axis
        ocrButton.pack(fill="x")
        tldrButton.pack(fill="x")
        settingsButton.pack(fill="x")

        # Displays the first page (Home_Page)
        home.show()


# ====== Python Boiler Plate ====== #

if __name__ == "__main__":
    # File checking and save loading
    try:
        v.load()
    except:
        pass
    checkFiles()

    # Interface Boiler-Plate
    root = tk.Tk()
    root.title("Combined OCR & TL;DR Software")
    root.configure(bg="#DADFE1")
    main = MainWindow(root)
    main.pack(side="top", fill="both", expand=True)
    root.wm_geometry("1000x700")
    root.mainloop()
示例#34
0
def getHistoricalPrices(tickerName):
    fileVariables = Variables()
    directory = fileVariables.directory
    fileEnding = fileVariables.returnFileEnding(tickerName)

    try:
        csv_file = open(directory + tickerName + '-Y.csv', "r")
        reader = csv.reader(csv_file, delimiter=' ')
        yahoo_data_array = []
    except:
        try:
            csv_file = open(directory + tickerName + '.csv', "r")
            reader = csv.reader(csv_file, delimiter=' ')
            yahoo_data_array = []
        except:
            print('Error : File Not Opening')

    switched = False
    for lines in reader:
        if ('Date' in lines or 'Date' in lines[0].split(',')):
            split = lines[0].split(',')
            if (split[5] == 'Volume'):
                switched = True
            pass
        else:
            yahoo_data_array.insert(0, lines[0].split(','))


#     print(switched)
    """If volume and adj close is switched, switch it around [5] and [6]"""
    if (switched == True):
        index = 0
        for i in yahoo_data_array:
            temp = []
            if (i[5] == 'null'):
                del yahoo_data_array[index]
                continue
            for j in range(0, 5):
                temp.append(i[j])
            temp.append(i[6])
            temp.append(i[5])
            del yahoo_data_array[index]
            yahoo_data_array.insert(index, temp)
            index += 1

    for i in yahoo_data_array:
        dates = i[0].split('-')
        i[0] = dates[0] + '/' + dates[1] + '/' + dates[2]

    firstDate = float(yahoo_data_array[0][0].split('/')[0])
    if (firstDate < 2016):
        yahoo_data_array.reverse()

    i = 0
    while (i < len(yahoo_data_array)):
        if ('null' in yahoo_data_array[i]):
            del yahoo_data_array[i]
            i -= 1
        i += 1

    yahoo_data_array = addHistoricalSplit(yahoo_data_array)

    return yahoo_data_array
DATABASE_NAME = 'ConcertoDb_TIF_WA6358_59_b9500bbf-f52a-474a-92c5-b863ed31d004'  #TIF Database
VEHICLE_NUMBER = '2'
DEFINITIONNUMBER_EVENT = '41'  # consult SQL-query "get_DefinitionNumber_and_respective_DinGroup" to find the correct DefinitionNumber
DEFINITIONNUMBER_ASV = '6'
NUMBER_ASV_POSITIONS = 7  # only the first NUMBER_ASV_POSITIONS positions of ASV string are relevant
START_EVENTS_FORMATTED = datetime.strptime(
    '2017-01-17',
    "%Y-%m-%d")  # point of time of very first event AND event ASV
TIMEFRAME_EVENT_ASV = 3 * 24  # hours
TIME_AFTER_LAST_EVENT = 10 * 24  # hours (no_event values start at that point of time)
TIMEFRAME_NO_EVENT_ASV = 6 * 24  # hours

FRACTION_TEST = 0.3  # fraction of examples that will be train data

SQL_PART_ROUTINE = Variables.get_sql_join(
    DATABASE_NAME, VEHICLE_NUMBER
)  # SQL query that executes relevant joins, not containing SELECT statement
#####################################

print(
    '-----------------------\nSVM on Concerto Data\napproach: TRANSMISSION\n-----------------------'
)

bool_reload = False

if SL.check_dir(
):  # folder "__cached_data__" exists - use it or set bool to reload from SQL database
    bool_reload = VS.yes_no(
        'Use previously stored data for SVC approach? [yes/no]')

    if bool_reload:  # using stored data
示例#36
0
 def onSelectButton(self, data):
     surface = vars.getVars("Surface")
     if data % 2 == 1:  # JR 07/09/2017
         surface.modeChange()
 def getParameterNames( self ):
     return Variables.getNames( self.getVariables() )
示例#38
0
      'R:bn',\
      'R:dp',\
      'R:ap',\
      'R:tt',\
      'C:o',\
      'C:x',\
      'C:-'\
      'I:bus'\
      )

H_bn = ('x','o')
H_dp = ('x','o')
H_ap = ('x','o')
H_tt = ('x','o')

Variables.clear_default_domain()

''' 
Let's Go Dynamic Graphical Model 

Initial State
Transition Network
Evidences

'''

fH0 = Factor(('H_bn_0','H_dp_0','H_ap_0','H_tt_0'),
            data=[0]*16,\
            new_domain_variables={'H_bn_0':H_bn,'H_dp_0':H_dp,'H_ap_0':H_ap,'H_tt_0':H_tt})
fH0[{'H_bn_0':'x','H_dp_0':'x','H_ap_0':'x','H_tt_0':'x'}] = 1
示例#39
0
文件: LetsgoMain.py 项目: junion/LGus
def extract_usr_model():
    import Variables
    from Parameters import Factor
    import LetsgoCorpus as lc
    import LetsgoSerializer as ls
    import LetsgoVariables as lv
    
    Variables.clear_default_domain()
    Variables.set_default_domain({'H_bn_t':lv.H_bn,'H_dp_t':lv.H_dp,'H_ap_t':lv.H_ap,\
                                  'H_tt_t':lv.H_tt,'UA_tt':lv.UA,'H_bn_tt':lv.H_bn,\
                                  'H_dp_tt':lv.H_dp,'H_ap_tt':lv.H_ap,'H_tt_tt':lv.H_tt})

    # estimate the ratio of dialogs w/ bn and dialogs w/o bn in the goal
    #
     
    try:
        fGbnO_Ht_SAttCO_UAtt = ls.load_model('_factor_o_C-o.model').marginalise_onto(['UA_tt']).normalised()
        fGbnX_Ht_SAttCO_UAtt = ls.load_model('_factor_x_C-o.model').marginalise_onto(['UA_tt']).normalised()
        fGbn_Ht_SAttCO_UAtt = (fGbnO_Ht_SAttCO_UAtt + fGbnX_Ht_SAttCO_UAtt).normalised()
    except:
        print ('Error:cannot find model')
        exit()

    try:
        fGbnO_Ht_SAttCX_UAtt = ls.load_model('_factor_o_C-x.model').marginalise_onto(['UA_tt']).normalised()
#        print fGbnO_Ht_SAtt_UAttX
        fGbnX_Ht_SAttCX_UAtt = ls.load_model('_factor_x_C-x.model').marginalise_onto(['UA_tt']).normalised()
#        print fGbnX_Ht_SAtt_UAttX
        fGbn_Ht_SAttCX_UAtt = (fGbnO_Ht_SAttCX_UAtt + fGbnX_Ht_SAttCX_UAtt).normalised()
#        print fGbn_Ht_SAtt_UAttX
    except:
        print ('Error:cannot find model')
        exit()

    try:
        fGbnO_Ht_SAttBN_UAtt = ls.load_model('_factor_o_R-bn.model').marginalise_onto(['UA_tt']).normalised()
#        print fGbnO_Ht_SAttBN_UAtt
        fGbnX_Ht_SAttBN_UAtt = ls.load_model('_factor_x_R-bn.model').marginalise_onto(['UA_tt']).normalised()
#        print fGbnX_Ht_SAttBN_UAtt
        fGbn_Ht_SAttBN_UAtt = (fGbnO_Ht_SAttBN_UAtt + fGbnX_Ht_SAttBN_UAtt).normalised()
#        print fGbn_Ht_SAttBN_UAtt
    except:
        print ('Error:cannot find model')
        exit()

    try:
        fGbnO_Ht_SAttDP_UAtt = ls.load_model('_factor_o_R-dp.model').marginalise_onto(['UA_tt']).normalised()
#        print fGbnO_Ht_SAttDP_UAtt
        fGbnX_Ht_SAttDP_UAtt = ls.load_model('_factor_x_R-dp.model').marginalise_onto(['UA_tt']).normalised()
#        print fGbnX_Ht_SAttDP_UAtt
        fGbn_Ht_SAttDP_UAtt = (fGbnO_Ht_SAttDP_UAtt + fGbnX_Ht_SAttDP_UAtt).normalised()
#        print fGbn_Ht_SAttDP_UAtt
    except:
        print ('Error:cannot find model')
        exit()

    try:
        fGbnO_Ht_SAttAP_UAtt = ls.load_model('_factor_o_R-ap.model').marginalise_onto(['UA_tt']).normalised()
#        print fGbnO_Ht_SAttAP_UAtt
        fGbnX_Ht_SAttAP_UAtt = ls.load_model('_factor_x_R-ap.model').marginalise_onto(['UA_tt']).normalised()
#        print fGbnX_Ht_SAttAP_UAtt
        fGbn_Ht_SAttAP_UAtt = (fGbnO_Ht_SAttAP_UAtt + fGbnX_Ht_SAttAP_UAtt).normalised()
#        print fGbn_Ht_SAttAP_UAtt
    except:
        print ('Error:cannot find model')
        exit()

    try:
        fGbnO_Ht_SAttTT_UAtt = ls.load_model('_factor_o_R-tt.model').marginalise_onto(['UA_tt']).normalised()
#        print fGbnO_Ht_SAttTT_UAtt
        fGbnX_Ht_SAttTT_UAtt = ls.load_model('_factor_x_R-tt.model').marginalise_onto(['UA_tt']).normalised()
#        print fGbnX_Ht_SAttTT_UAtt
        fGbn_Ht_SAttTT_UAtt = (fGbnO_Ht_SAttTT_UAtt + fGbnX_Ht_SAttTT_UAtt).normalised()
#        print fGbn_Ht_SAttTT_UAtt
    except:
        print ('Error:cannot find model')
        exit()

    try:
        fGbnO_Ht_SAttOP_UAtt = ls.load_model('_factor_o_R-open.model').marginalise_onto(['UA_tt']).normalised()
#        print fGbnO_Ht_SAttOP_UAtt
        fGbnX_Ht_SAttOP_UAtt = ls.load_model('_factor_x_R-open.model').marginalise_onto(['UA_tt']).normalised()
#        print fGbnX_Ht_SAttOP_UAtt
        fGbn_Ht_SAttOP_UAtt = (fGbnO_Ht_SAttOP_UAtt + fGbnX_Ht_SAttOP_UAtt).normalised()
#        print fGbn_Ht_SAttOP_UAtt
    except:
        print ('Error:cannot find model')
        exit()

    um = {}
    um['C-o'] = fGbn_Ht_SAttCO_UAtt.insts_data_dict()
    um['C-x'] = fGbn_Ht_SAttCX_UAtt.insts_data_dict()
    um['R-bn'] = fGbn_Ht_SAttBN_UAtt.insts_data_dict()
    um['R-dp'] = fGbn_Ht_SAttDP_UAtt.insts_data_dict()
    um['R-ap'] = fGbn_Ht_SAttAP_UAtt.insts_data_dict()
    um['R-tt'] = fGbn_Ht_SAttTT_UAtt.insts_data_dict()
    um['R-open'] = fGbn_Ht_SAttOP_UAtt.insts_data_dict()
    
    print um
    
    print 'Writing parameters...'
        
    ls.store_model(um,'_user_action.model')
    
    print 'Done'
示例#40
0
    def Sampling_learn(self,make_correction_model=False):
        start_time = time.strftime("%a, %d %b %Y %H:%M:%S +0000", time.gmtime())
        
        actCounts = {'bn':{'correct':0,'incorrect':0},
                             'dp':{'correct':0,'incorrect':0},
                             'ap':{'correct':0,'incorrect':0},
                             'tt':{'correct':0,'incorrect':0},
                             'yes':{'correct':0,'incorrect':0},
                             'no':{'correct':0,'incorrect':0},
                             'non-understanding':0}

        Variables.clear_default_domain()
        Variables.set_default_domain({'H_bn_t':lv.H_bn,'H_dp_t':lv.H_dp,'H_ap_t':lv.H_ap,\
                                      'H_tt_t':lv.H_tt,'UA_tt':lv.UA,'H_bn_tt':lv.H_bn,\
                                      'H_dp_tt':lv.H_dp,'H_ap_tt':lv.H_ap,'H_tt_tt':lv.H_tt,\
                                      'H_bn_0':lv.H_bn,'H_dp_0':lv.H_dp,'H_ap_0':lv.H_ap,\
                                      'H_tt_0':lv.H_tt})

        fHt_UAtt_Htt = ls.load_model('_factor_Ht_UAtt_Htt.model')
        
               
        for d, dialog in enumerate(lc.Corpus(self.data,prep=self.prep).dialogs()):
            if len(dialog.turns) > 40:
                continue
            
            print 'processing dialog #%d...'%d

            avg_cs = reduce(operator.add,map(lambda x:x['CS'],dialog.turns))/len(dialog.turns)
            if avg_cs > 0.7: c = 0
            elif avg_cs > 0.5: c = 1
            elif avg_cs > 0.3: c = 2
            else: c = 3
            
            self.q_class[c] += 1
            
            cm_ua_template = self.cm_ua_template[c]
            co_cs = self.co_cs[c]
            inco_cs = self.inco_cs[c]
        
            Variables.change_variable('H_bn_0',lv.H_bn)
            Variables.change_variable('H_dp_0',lv.H_dp)
            Variables.change_variable('H_ap_0',lv.H_ap)
            Variables.change_variable('H_tt_0',lv.H_tt)
        
            dialog_factors = []
            for t, turn in enumerate(dialog.abs_turns):
                tmp_fHt_UAtt_Htt = Factor(('H_bn_%s'%t,'H_dp_%s'%t,'H_ap_%s'%t,'H_tt_%s'%t,\
                                           'UA_%s'%(t+1),'H_bn_%s'%(t+1),'H_dp_%s'%(t+1),\
                                           'H_ap_%s'%(t+1),'H_tt_%s'%(t+1)),
                        new_domain_variables={'UA_%s'%(t+1):lv.UA,'H_bn_%s'%(t+1):lv.H_bn,\
                                              'H_dp_%s'%(t+1):lv.H_dp,'H_ap_%s'%(t+1):lv.H_ap,\
                                              'H_tt_%s'%(t+1):lv.H_tt})
                tmp_fHt_UAtt_Htt[:] = fHt_UAtt_Htt[:]
        #            tmp_fHt_UAtt_Htt = fHt_UAtt_Htt.copy_rename({'H_bn_t':'H_bn_%s'%i,'H_dp_t':'H_dp_%s'%i,'H_ap_t':'H_ap_%s'%i,'H_tt_t':'H_tt_%s'%i,'UA_tt':'UA_%s'%(i+1),'H_bn_tt':'H_bn_%s'%(i+1),'H_dp_tt':'H_dp_%s'%(i+1),'H_ap_tt':'H_ap_%s'%(i+1),'H_tt_tt':'H_tt_%s'%(i+1)})            
                tmp_fGbn_Ht_SAtt_UAtt = Factor(('H_bn_%s'%t,'H_dp_%s'%t,'H_ap_%s'%t,'H_tt_%s'%t,'UA_%s'%(t+1)))
        #            tmp_fGbn_Ht_SAtt_UAtt = Factor(('H_bn_%s'%i,'H_dp_%s'%i,'H_ap_%s'%i,'H_tt_%s'%i,'UA_%s'%(i+1)),new_domain_variables={'UA_%s'%(i+1):UA,'H_bn_%s'%i:H_bn,'H_dp_%s'%i:H_dp,'H_ap_%s'%i:H_ap,'H_tt_%s'%i:H_tt})
                try:
                    tmp_fGbn_Ht_SAtt_UAtt[:] = \
                    ls.load_model(('_factor_%s_%s.model'%(dialog.abs_goal['G_bn'],turn['SA'][0])).replace(':','-'))[:]
                except:
                    print ('Error:cannot find _factor_%s_%s.model'%(dialog.abs_goal['G_bn'],turn['SA'][0])).replace(':','-')
                    exit()
                tmp_fUAtt_Ott = Factor(('UA_%s'%(t+1),))
                tmp_fUAtt_Ott[:] = lo.getObsFactor(turn,use_cs=True)[:]
                factor = tmp_fHt_UAtt_Htt * tmp_fGbn_Ht_SAtt_UAtt * tmp_fUAtt_Ott
                dialog_factors.append(factor.copy(copy_domain=True))
            
            jfr = JFR(SFR(dialog_factors))
            jfr.condition({'H_bn_0':'x','H_dp_0':'x','H_ap_0':'x','H_tt_0':'x'})
            jfr.calibrate()
            
            # Populate error table using inferred and observed actions
            for t, turn in enumerate(dialog.turns):
                obs_ua_template = []
                for act in turn['UA']:
                    if act.find('I:bn') == 0:# and max_ua.find('I:bn') > -1 and not dialog.goal['G_bn'] == '':
                        if dialog.goal['G_bn'] == act.split(':')[-1]: 
                            obs_ua_template.append('I:bn:o')
                            actCounts['bn']['correct'] += 1
                        else: 
                            obs_ua_template.append('I:bn:x')
                            actCounts['bn']['incorrect'] += 1
                            if dialog.goal['G_bn'] not in self.cm_bn:
                                self.cm_bn[dialog.goal['G_bn']] = {act.split(':')[-1]:1}
                            elif act.split(':')[-1] not in self.cm_bn[dialog.goal['G_bn']]:
                                self.cm_bn[dialog.goal['G_bn']][act.split(':')[-1]] = 1
                            else:
                                self.cm_bn[dialog.goal['G_bn']][act.split(':')[-1]] += 1
                    elif act.find('I:dp') == 0:# and max_ua.find('I:dp') > -1 and not dialog.goal['G_dp'] == '':
                        if dialog.goal['G_dp'] == act.split(':')[-1]: 
                            obs_ua_template.append('I:dp:o')
                            actCounts['dp']['correct'] += 1
                        else: 
                            obs_ua_template.append('I:dp:x')
                            actCounts['dp']['incorrect'] += 1
                            if dialog.goal['G_dp'] not in self.cm_p:
                                self.cm_p[dialog.goal['G_dp']] = {act.split(':')[-1]:1}
                            elif act.split(':')[-1] not in self.cm_p[dialog.goal['G_dp']]:
                                self.cm_p[dialog.goal['G_dp']][act.split(':')[-1]] = 1
                            else:
                                self.cm_p[dialog.goal['G_dp']][act.split(':')[-1]] += 1
                    elif act.find('I:ap') == 0:# and max_ua.find('I:ap') > -1 and not dialog.goal['G_ap'] == '':
                        if dialog.goal['G_ap'] == act.split(':')[-1]: 
                            obs_ua_template.append('I:ap:o')
                            actCounts['ap']['correct'] += 1
                        else: 
                            obs_ua_template.append('I:ap:x')
                            actCounts['ap']['incorrect'] += 1
                            if dialog.goal['G_ap'] not in self.cm_p:
                                self.cm_p[dialog.goal['G_ap']] = {act.split(':')[-1]:1}
                            elif act.split(':')[-1] not in self.cm_p[dialog.goal['G_ap']]:
                                self.cm_p[dialog.goal['G_ap']][act.split(':')[-1]] = 1
                            else:
                                self.cm_p[dialog.goal['G_ap']][act.split(':')[-1]] += 1
                    elif act.find('I:tt') == 0:# and max_ua.find('I:tt') > -1 and not dialog.goal['G_tt'] == '':
                        if dialog.goal['G_tt'] == act.split(':')[-1]: 
                            obs_ua_template.append('I:tt:o')
                            actCounts['tt']['correct'] += 1
                        else: 
                            obs_ua_template.append('I:tt:x')
                            actCounts['tt']['incorrect'] += 1
                            if dialog.goal['G_tt'] not in self.cm_tt:
                                self.cm_tt[dialog.goal['G_tt']] = {act.split(':')[-1]:1}
                            elif act.split(':')[-1] not in self.cm_tt[dialog.goal['G_tt']]:
                                self.cm_tt[dialog.goal['G_tt']][act.split(':')[-1]] = 1
                            else:
                                self.cm_tt[dialog.goal['G_tt']][act.split(':')[-1]] += 1
                    else:
                        obs_ua_template.append(act)

                if len(obs_ua_template) == 1:
                    if len(obs_ua_template[0].split(':')) == 3:
                        dummy,field,val = obs_ua_template[0].split(':')
                        if val == 'o':
                            co_cs[field].append(turn['CS'])
                        else:
                            inco_cs[field].append(turn['CS'])
                    else:
                        if obs_ua_template[0] == 'yes':
                            if config.getboolean('UserSimulation','extendedSystemActionSet'):
                                if dialog.abs_turns[t]['SA'][0] in ['C:bn:o','C:dp:o','C:ap:o','C:tt:o']:
                                    co_cs['yes'].append(turn['CS'])
                                elif dialog.abs_turns[t]['SA'][0] in ['C:bn:x','C:dp:x','C:ap:x','C:tt:x']:
                                    inco_cs['yes'].append(turn['CS'])
                            else:
                                if dialog.abs_turns[t]['SA'][0] == 'C:o':
                                    co_cs['yes'].append(turn['CS'])
                                elif dialog.abs_turns[t]['SA'][0] == 'C:x':
                                    inco_cs['yes'].append(turn['CS'])
                        elif obs_ua_template[0] == 'no':
                            if config.getboolean('UserSimulation','extendedSystemActionSet'):
                                if dialog.abs_turns[t]['SA'][0] in ['C:bn:x','C:dp:x','C:ap:x','C:tt:x']:
                                    co_cs['no'].append(turn['CS'])
                                elif dialog.abs_turns[t]['SA'][0] in ['C:bn:o','C:dp:o','C:ap:o','C:tt:o']:
                                    inco_cs['no'].append(turn['CS'])
                            else:
                                if dialog.abs_turns[t]['SA'][0] == 'C:x':
                                    co_cs['no'].append(turn['CS'])
                                elif dialog.abs_turns[t]['SA'][0] == 'C:o':
                                    inco_cs['no'].append(turn['CS'])
                else:
                    try:
                        if make_correction_model and len(obs_ua_template) == 2 and 'no' in obs_ua_template:
                            if ','.join(obs_ua_template).find(':x') > -1:
                                inco_cs['correction'].append(turn['CS'])
                            else:
                                co_cs['correction'%len(obs_ua_template)].append(turn['CS'])
                        else:    
                            if ','.join(obs_ua_template).find(':x') > -1:
                                inco_cs['multi%d'%len(obs_ua_template)].append(turn['CS'])
                            else:
                                co_cs['multi%d'%len(obs_ua_template)].append(turn['CS'])
                    except:
                        print len(obs_ua_template)

                rf = jfr.factors_containing_variable('UA_%s'%(t+1))
                ua_factor = rf[0].copy().marginalise_onto(['UA_%s'%(t+1)]).normalised()#[{'UA_1':inst['UA_tt']}]
#                print ua_factor               
                ua_pt = dict(zip(map(lambda x:x[0],ua_factor.insts()),ua_factor[:]))
#                print ua_pt
                for r in range(10):
                    sampled_ua = MultinomialSampler(ua_pt).sample()
#                    print sampled_ua
                
                    if sampled_ua not in cm_ua_template:
                        cm_ua_template[sampled_ua] = {','.join(sorted(obs_ua_template)):1}
                    elif ','.join(sorted(obs_ua_template)) not in cm_ua_template[sampled_ua]:
                        cm_ua_template[sampled_ua][','.join(sorted(obs_ua_template))] = 1
                    else:
                        cm_ua_template[sampled_ua][','.join(sorted(obs_ua_template))] += 1
                    
        for c in range(self.q_class_max):
            cm_ua_template = self.cm_ua_template[c]
            co_cs = self.co_cs[c]
            inco_cs = self.inco_cs[c]

            co_cs['single'] = co_cs['bn'] + co_cs['dp'] + co_cs['ap'] +\
             co_cs['tt'] + co_cs['yes'] + co_cs['no'] 
            inco_cs['single'] = inco_cs['bn'] + inco_cs['dp'] + inco_cs['ap'] +\
             inco_cs['tt'] + inco_cs['yes'] + inco_cs['no'] 
    
            for n in range(2,6,1):
                co_cs['multi'] += co_cs['multi%d'%n]
                inco_cs['multi'] += inco_cs['multi%d'%n]
    
            co_cs['total'] = co_cs['single'] + co_cs['multi']
            inco_cs['total'] = inco_cs['single'] + inco_cs['multi']
             
        print 'Writing parameters...'
        def make_dist(ft):
            tot = sum(ft.values())
            for k, v in ft.items(): ft[k] = float(v)/tot
            return ft

        def make_dists(cm):
            for key in cm.keys():
                cm[key] = make_dist(cm[key])
            return cm

        def generate_cs_pd(cs):
            cs_pd = {}
            for key in cs.keys():
                cs_pd[key] = {}
                for val in cs[key]:
                    try:
                        cs_pd[key][int(val/0.01)/float(100)] += 1
                    except:
                        cs_pd[key][int(val/0.01)/float(100)] = 1
            return make_dists(cs_pd)
            
        ls.store_model(make_dists(self.cm_bn),'_confusion_matrix_bn.model')
        ls.store_model(make_dists(self.cm_p),'_confusion_matrix_p.model')
        ls.store_model(make_dists(self.cm_tt),'_confusion_matrix_tt.model')
        ls.store_model(make_dist(self.q_class),'_quality_class.model')
        for c in range(self.q_class_max): 
            ls.store_model(make_dists(self.cm_ua_template[c]),'_confusion_matrix_ua_class_%d.model'%c)
            ls.store_model(self.co_cs[c],'_correct_confidence_score_class_%d.model'%c)
            ls.store_model(self.inco_cs[c],'_incorrect_confidence_score_class_%d.model'%c)
            ls.store_model(generate_cs_pd(self.co_cs[c]),'_correct_confidence_score_prob_dist_class_%d.model'%c)
            ls.store_model(generate_cs_pd(self.inco_cs[c]),'_incorrect_confidence_score_prob_dist_class_%d.model'%c)

        pprint.pprint(actCounts)

        end_time = time.strftime("%a, %d %b %Y %H:%M:%S +0000", time.gmtime())
       
        print 'Start time: %s'%start_time
        print 'End time: %s'%end_time
示例#41
0
os.chdir(uncDir)
#print 'in dir:',inputDir
os.chdir(inputDir)
inputDir = os.getcwd()
os.chdir(uncDir)

#load variables
uVars = input_file('Variables/names','').split(' ')
varDict={}
for var in uVars:
  path=input_file('Variables/'+var+'/path',' ')
  dist=input_file('Variables/'+var+'/dist',' ')
  args=input_file('Variables/'+var+'/args',' ').split(' ')
  for a,arg in enumerate(args):
    args[a]=float(arg)
  varDict[var]=Variables.newVar(dist,var,path)
  varDict[var].setDist(args)

#build sampler
samplerType = input_file('Sampler/active','')
if samplerType in ['mc','MC','mC','MonteCarlo','Monte Carlo']:
  sampler = spr.MonteCarlo(varDict,input_file)
elif samplerType in ['SC','sc','StochPoly']:
  sampler = spr.StochasticPoly(varDict,input_file)
else:
  raise IOError ('Sampler type not recognized: |'+samplerType+'|')

#for var in varDict.values():
#  print var.name,var.pts,var.wts
#print sampler.runords
示例#42
0
    def EM_learn(self):
        print 'Parameter learning start...'
        start_time = time.strftime("%a, %d %b %Y %H:%M:%S +0000", time.gmtime())
        logliks = []
        
        if not self.inc:
            self._reset_param()
        else:
            Variables.clear_default_domain()
            Variables.set_default_domain({'H_bn_t':lv.H_bn,'H_dp_t':lv.H_dp,'H_ap_t':lv.H_ap,\
                                          'H_tt_t':lv.H_tt,'UA_tt':lv.UA,'H_bn_tt':lv.H_bn,\
                                          'H_dp_tt':lv.H_dp,'H_ap_tt':lv.H_ap,'H_tt_tt':lv.H_tt,\
                                          'H_bn_0':lv.H_bn,'H_dp_0':lv.H_dp,'H_ap_0':lv.H_ap,\
                                          'H_tt_0':lv.H_tt})

        fHt_UAtt_Htt = ls.load_model('_factor_Ht_UAtt_Htt.model')
        
        prevloglik = -1000000000000000
        loglik = 0.0
        for i in range(self.iter):
            print 'Iterate %d'%i
            ess = {}
            factor_template = {'G_bn':lv.G_bn,'SA':lv.SA}
            for factor in Utils.inst_filling(factor_template):
                ess[('_factor_%s_%s.model'%(factor['G_bn'],factor['SA'])).replace(':','-')] = \
                Factor(('H_bn_t','H_dp_t','H_ap_t','H_tt_t','UA_tt'))
                ess[('_factor_%s_%s.model'%(factor['G_bn'],factor['SA'])).replace(':','-')][:] = \
                [sys.float_info.min] * len(ess[('_factor_%s_%s.model'%(factor['G_bn'],factor['SA'])).replace(':','-')][:])
#                ess[('_factor_%s_%s.model'%(factor['G_bn'],factor['SA'])).replace(':','-')].zero()

            for d, dialog in enumerate(lc.Corpus(self.data,prep=self.prep).dialogs()):
                if len(dialog.turns) > 40:
                    continue
                
                print 'processing dialog #%d...'%d
            
                Variables.change_variable('H_bn_0',lv.H_bn)
                Variables.change_variable('H_dp_0',lv.H_dp)
                Variables.change_variable('H_ap_0',lv.H_ap)
                Variables.change_variable('H_tt_0',lv.H_tt)
            
                dialog_factors = []
                for t, turn in enumerate(dialog.abs_turns):
                    tmp_fHt_UAtt_Htt = Factor(('H_bn_%s'%t,'H_dp_%s'%t,'H_ap_%s'%t,'H_tt_%s'%t,\
                                               'UA_%s'%(t+1),'H_bn_%s'%(t+1),'H_dp_%s'%(t+1),\
                                               'H_ap_%s'%(t+1),'H_tt_%s'%(t+1)),
                            new_domain_variables={'UA_%s'%(t+1):lv.UA,'H_bn_%s'%(t+1):lv.H_bn,\
                                                  'H_dp_%s'%(t+1):lv.H_dp,'H_ap_%s'%(t+1):lv.H_ap,\
                                                  'H_tt_%s'%(t+1):lv.H_tt})
                    tmp_fHt_UAtt_Htt[:] = fHt_UAtt_Htt[:]
            #            tmp_fHt_UAtt_Htt = fHt_UAtt_Htt.copy_rename({'H_bn_t':'H_bn_%s'%i,'H_dp_t':'H_dp_%s'%i,'H_ap_t':'H_ap_%s'%i,'H_tt_t':'H_tt_%s'%i,'UA_tt':'UA_%s'%(i+1),'H_bn_tt':'H_bn_%s'%(i+1),'H_dp_tt':'H_dp_%s'%(i+1),'H_ap_tt':'H_ap_%s'%(i+1),'H_tt_tt':'H_tt_%s'%(i+1)})            
                    tmp_fGbn_Ht_SAtt_UAtt = Factor(('H_bn_%s'%t,'H_dp_%s'%t,'H_ap_%s'%t,'H_tt_%s'%t,'UA_%s'%(t+1)))
            #            tmp_fGbn_Ht_SAtt_UAtt = Factor(('H_bn_%s'%i,'H_dp_%s'%i,'H_ap_%s'%i,'H_tt_%s'%i,'UA_%s'%(i+1)),new_domain_variables={'UA_%s'%(i+1):UA,'H_bn_%s'%i:H_bn,'H_dp_%s'%i:H_dp,'H_ap_%s'%i:H_ap,'H_tt_%s'%i:H_tt})
                    try:
                        tmp_fGbn_Ht_SAtt_UAtt[:] = \
                        ls.load_model(('_factor_%s_%s.model'%(dialog.abs_goal['G_bn'],turn['SA'][0])).replace(':','-'))[:]
                    except:
                        print ('Error:cannot find _factor_%s_%s.model'%(dialog.abs_goal['G_bn'],turn['SA'][0])).replace(':','-')
                        exit()
                    tmp_fUAtt_Ott = Factor(('UA_%s'%(t+1),))
                    tmp_fUAtt_Ott[:] = lo.getObsFactor(turn,use_cs=True)[:]
                    factor = tmp_fHt_UAtt_Htt * tmp_fGbn_Ht_SAtt_UAtt * tmp_fUAtt_Ott
                    dialog_factors.append(factor.copy(copy_domain=True))
                
                jfr = JFR(SFR(dialog_factors))
                jfr.condition({'H_bn_0':'x','H_dp_0':'x','H_ap_0':'x','H_tt_0':'x'})
                jfr.calibrate()
                
                for t, turn in enumerate(dialog.abs_turns):
                    rf = jfr.factors_containing_variable('UA_%s'%(t+1))
                    from operator import add
                    if t == 0:
                        for inst in Utils.inst_filling({'H_bn_t':['x'],'H_dp_t':['x'],\
                                                        'H_ap_t':['x'],'H_tt_t':['x'],'UA_tt':lv.UA}):
                            ess[('_factor_%s_%s.model'%(dialog.abs_goal['G_bn'],turn['SA'][0])).replace(':','-')][inst] += \
                            rf[0].copy().marginalise_onto(['UA_1']).normalised()[{'UA_1':inst['UA_tt']}]
                        loglik += math.log(rf[0].copy().z())
                        print 'dialog loglik: %e'%loglik
                    else:  
                        ess[('_factor_%s_%s.model'%(dialog.abs_goal['G_bn'],turn['SA'][0])).replace(':','-')][:] =\
                        map(add,ess[('_factor_%s_%s.model'%(dialog.abs_goal['G_bn'],turn['SA'][0])).replace(':','-')][:],\
                            rf[0].copy().marginalise_onto(['H_bn_%s'%t,'H_dp_%s'%t,'H_ap_%s'%t,\
                                                           'H_tt_%s'%t,'UA_%s'%(t+1)]).normalised()[:])
                    
            print 'Writing parameters...'
            factor_template = {'G_bn':lv.G_bn,'SA':lv.SA}
            for factor in Utils.inst_filling(factor_template):
                factor = ('_factor_%s_%s.model'%(factor['G_bn'],factor['SA'])).replace(':','-')
                ls.store_model(CPT(ess[factor],child='UA_tt',cpt_force=True),factor)
            
            logliks.append(loglik)
            relgain = ((loglik - prevloglik)/math.fabs(prevloglik))
            print 'prevloglik: %e'%prevloglik
            print 'loglik: %e'%loglik
            print 'relgain: %e'%relgain
        
            if relgain < self.tol:
                break
            
            prevloglik = loglik
            loglik = 0.0
        
        print 'Parameter learning done'    
        
        end_time = time.strftime("%a, %d %b %Y %H:%M:%S +0000", time.gmtime())
        
        print 'Log likelihood (%d): %s'%(len(logliks),logliks)
        print 'Start time: %s'%start_time
        print 'End time: %s'%end_time
示例#43
0
import Variables as Var

# style.use('seaborn-poster')
# style.use('ggplot')
style.use('seaborn-bright')
style.use('seaborn-whitegrid')
style.use('seaborn-paper')

AnimInterv = 5000
xRange = 600

setXLim = False  # True
animated = False  # False

for i in range(Var.numBrainNeurons):
    p = Var.idMapper(i + 1)
    print('i: {} corresponds to Pixel({},{}) '.format(i, p.x, p.y))


def drawWeights(i, ax, name):
    # print(i,ax,name)
    # coordinates = [[(float(i),j+((2.*i)/(j+1.)))for i in range(NENTRIES)]for j in range(NLINES)]
    with open(name + '.csv') as csv_file:
        # with open('left-weights.csv') as csv_file:
        csv_reader = csv.reader(csv_file, delimiter=',')
        matrix = []
        for i in csv_reader:
            matrix.append(i)
        # print(matrix)
        NLINES = len(matrix[1])
        NENTRIES = len(matrix)
示例#44
0
import Variables as v
import matplotlib.pyplot as plt
import numpy as np

test=v.newVar('lognormal')
test.setDist([10.,4.])
vals=test.sample(n=int(1e6))
hist,bounds=np.histogram(vals,bins=100)
ctrs=np.zeros(len(bounds)-1)
for c in range(len(ctrs)):
  ctrs[c]=0.5*(bounds[c]+bounds[c+1])

plt.plot(ctrs,hist)
plt.show()
print np.average(vals)