def run(self): step_date = time() while True: # eviter d'encombrer le CPU quand il n'y a pas de requete t = time() sleep(t-step_date) step_date = t # analyser les derniers paquets for index in range(len(self.hosts)): packet = self.getpacket(index) if packet: if rep(packet, 'want file') : pathfrom = packet[10:] self.clearpacket(index) realpath = self.realpath(pathfrom) if realpath : self.send(index, 'accept') self.sendfile(index, realpath) else : self.send(index, 'reject') if rep(packet, 'take file'): pathto = packet[10:] self.clearpacket(index) realpath = self.realpath(pathto) if realpath: self.send(index, 'accept') self.recvfile(index, realpath) else: self.send(index, 'reject')
def look_human(self): syslog.syslog('Looking human for %s' % self.nickname) # build a streamer of a sample of tweets self.build_streamer() ## schedule each bot to tweet a random tweet pulled from corpus at random specified time depending on if its a weekday or not # get todays date today = date.today() # get whether its a weekday or weekend week_type = self.get_weektime(today.weekday()) # get minimum datetime and maximum datetime to spawn intervals in between them mintime = time(self.scheduled_tweets[week_type]['times'][0], 0) mindt = datetime.combine(today, mintime) maxtime = time(self.scheduled_tweets[week_type]['times'][1], 0) maxdt = datetime.combine(today, maxtime) # get each bot, and use gevent to spawn_later tasks based on the week_type with a random tweet for bot in self.bot_list: intervals = [ self.randtime(mindt, maxdt) for x in xrange(self.scheduled_tweets[week_type]['num_tweets']) ] s = ' '.join([ str(datetime.fromtimestamp(interval)) for interval in intervals ]) syslog.syslog('%s times to tweet -> %s' % (bot.name, s)) bot.last_intervals = intervals # assign the gevent to spawn_later by mapping each interval generated, find the time delta to determine number of seconds until event # and then pull a random tweet from the corpus map(lambda time: gevent.spawn_later(time - int(datetime.now().strftime('%s')), bot.tweet, self.get_random_tweet()), intervals) # reset corpus self.twitter_corpus = []
def pos_features(compactcorpus): """ Hier staan alle features die op dit moment gebruikt """ start = time() wrds = common_but_unique(ngrams_dict(1, authors, compactcorpus, 25, False), 8) bigrams = common_but_unique(ngrams_dict(2, authors, compactcorpus, 25, False), 8) trigrams = common_but_unique(ngrams_dict(3, authors, compactcorpus, 25, False), 8) # tag_bigrams =common_but_unique(ngrams_dict(2,authors,compact_to_tag(compactcorpus),20,False),15) #PAS OP Duurt erg lang om te gebruiken (dus ook nog niet getest...ivm tijd) skipgrams = common_but_unique(skipgrams_dict(authors, compactcorpus, 10), 10) minimal_wrdoccurence = ["wrd:" + wrd + ">" + str(num) for wrd in wrds for num in range(0, 1)] minimal_trigram_occurence = [ "tri:(" + str(tri[0]) + "," + str(tri[1]) + "," + str(tri[2]) + ")>" + str(num) for tri in trigrams for num in range(0, 1) ] minimal_bigram_occurence = [ "bi:(" + str(bi[0]) + "," + str(bi[1]) + ")>" + str(num) for bi in bigrams for num in range(0, 1) ] # minimal_skipgram_occurence = ["skip:("+str(skip[0])+","+str(skip[1])+","+str(skip[2])+")>"+str(num) for skip in skipgrams for num in range(0,1)] features = ( minimal_bigram_occurence + minimal_wrdoccurence + minimal_trigram_occurence ) # + minimal_skipgram_occurence print "pos feat in:" + str(time() - start) return features
def main(): print "\n\t\t*** readit ***\n" stream = itreestream(FILENAME, "Events") if not stream.good(): fatal("unable to open file: %s" % FILENAME) nevent = stream.size() print "\n\nNumber of events: %d" % nevent gpt = vdouble(2000) stream.select(GENPT, gpt); if not stream.good(): fatal("unable to get branch %s" % GENPT) print "enter event loop" c = 0.0 t0 = time() for event in stream: c += gpt.size() dt = time() - t0 rate = nevent/dt c /= nevent print "Read rate: %10.1f events/sec" % rate print "Average # GenParticles: %10.1f" % c
def lock(self,timeout=500,sleeptime=0.0001, sleep=time.sleep,Error=Error,time=time.time,error=os.error, hostname=hostname,ip=ip,mkdir=os.mkdir): if self.locked: return lockfilename = self.lockfilename lockinfo = '%s:%i' % (hostname,os.getpid()) stop = time() + timeout * 0.001 while 1: # These are rather time-critical try: mkdir(lockfilename) except error: # A non existent path will result in a time out. pass else: break sleep(sleeptime) if time() > stop: # Timeout... check whether it's a valid lock if not self.validate_lock(): continue raise Error,\ 'file "%s" is currently locked' % self.filename self.locked = 1
def resolution_latex(self, affiche=True, grille=None): """Lance la résolution de la grille par l'ordinateur avec affichage en LaTeX pour copier-coller dans le rapport""" # Lancement du chrono start = time() self.add_message("C'est parti !!!") # C'est parti !!! while not self.grille_suivi.fini(): if affiche : print(r"{\scriptsize") print(r"\begin{verbatim}") self.grille_suivi.affiche_adverse(grille) self.affiche_messages(affiche=affiche) print(r"\end{verbatim}}") print(r"\hrule") print() self.coup_suivant() # Fin de la partie if affiche : print(r"{\scriptsize") print(r"\begin{verbatim}") self.grille_suivi.affiche_adverse(grille) self.add_message("Partie terminée en %d coups" % self.essais) self.affiche_messages(affiche=affiche) print(r"\end{verbatim}}\hrule") # On renvoie de temps de résolution de la grille pour les tests de performance return time()-start
def feature_selection(filename, basefeatures, features, num_rounds, num_selections): """ voor de home made classifier: test elke feature in een lijst van features (features), je kunt ook basefeatures toevoegen (deze worden niet getest, maar wel meegenomen). Ook moet je een aantal auteurs meegeven waarop je wilt testen, het aantal 'testronden', en het aantal te selecteren features. De features worden dan getest dmv testfeatures2, deze geeft een precision per feature, de hoogste n (n=num_selections) features worden naar het opgegeven bestand weggeschreven. Args:(String,List of features, List of features(string/naam, function), Integer, Integer, Integer) Returns: Ordered List of features + schrijft een textbestand """ prec_dict = {} length = len(features) print "feature selection on: " + str(length) + " features." for f in features: start = time() prec_dict[f] = test_features2((basefeatures + [f]), num_rounds, "") length = length - 1 if (length % 5) == 0: print "Estimated time left:" + str(datetime.timedelta(seconds=(time() - start) * length)) print "selection of " + str(num_selections) + " has been made." selection = sorted(prec_dict.iteritems(), key=itemgetter(0), reverse=True)[:num_selections] print "writing to file:" + filename file = open(filename, "w") for (f, prec) in selection: file.write("[" + str(f) + "]:" + str(prec) + "\n") file.close() return selection
def cutoff(self, state, depth): board, player = state if board.is_finished(): return True if self.time_left - (time()-self.timer) <= 15 : self.timeout = True return depth >= 1 # Must cut with iterative depth if not self.previousSearchedBoard == None : action_played = self._action_played(board,self.previousSearchedBoard) if action_played : # sometimes bad things happen and action_playes is false move = Action(action_played[1][0],action_played[1][1]) if move.is_a_pattern(self.suicideDic) and depth % 2 == 0: return True # if a node-max is a suicide, cut it, we do not play the move elif move.is_a_pattern(self.mustDoDic) and depth % 2 == 1 : return True # if a node-min is a suicide for the opponent, cut it, he will not play that move ####################### maxtime = (self.time_left - 14) / ((1 + (37/(self.step+1))**1.8)/2) if (time()-self.timer) >= maxtime: # print(maxtime) self.timeout = True return True else: return depth == self.actualdepth ####################### return depth >= 1
def resolution(self, affiche=True, grille=None): """Lance la résolution de la grille par l'ordinateur""" # affiche : affichage ou non des informations (pour les tests) # Lancement du chrono start = time() self.add_message("C'est parti !!!") # C'est parti !!! while not self.grille_suivi.fini(): if affiche : clear() self.grille_suivi.affiche_adverse(grille) self.affiche_messages(affiche=affiche) self.coup_suivant() if affiche : input("Entrée pour continuer") # Fin de la partie if affiche : clear() self.grille_suivi.affiche_adverse(grille) self.add_message("Partie terminée en %d coups" % self.essais) self.affiche_messages(affiche=affiche) # On renvoie de temps de résolution de la grille pour les tests de performance return time()-start
def execute_query(self, query, commit = False): if self.params.db.verbose: from time import time st = time() self.query_count += 1 retry_count = 0 retry_max = 10 sleep_time = 0.1 while retry_count < retry_max: try: dbobj = get_db_connection(self.params) cursor = dbobj.cursor() cursor.execute(query) if commit: dbobj.commit() if self.params.db.verbose: print 'Query % 6d SQLTime Taken = % 10.6f seconds' % (self.query_count, time() - st), query[:min(len(query),145)] return cursor except OperationalError, e: if "Can't connect to MySQL server" not in str(e): raise e retry_count += 1 print "Couldn't connect to MYSQL, retry", retry_count time.sleep(sleep_time) sleep_time *= 2 except Exception, e: print "Couldn't execute MYSQL query. Query:" print query print "Exception:" print str(e) raise e
def conv(img,ancho,alto): tiemp = time() pixels =img.load() matrizX =([-1,0,1],[-2,0,2],[-1,0,1]) matrizY =([1,2,1],[0,0,0],[-1,-2,-1]) for i in range(ancho): for j in range(alto): sumx = 0 sumy = 0 a=3 for x in range(a): for y in range(a): try: sumx +=(pixels[x+i,y+j][0]*matrizX[x][y]) sumy += (pixels[x+i,y+j][0]*matrizY[x][y]) except: pass grad = math.sqrt(pow(sumx,2)+pow(sumy,2)) grad = int(grad) pixels[i,j] = (grad,grad,grad) im= img.save('conv.png') timei=time() timef= timei - tiemp print "Tiempo de ejecucion deteccion de bordes: "+str(timef)+"segundos"
def RBFS(self, node, fLimit): """Returns a solution, or failure and a new f-cost limit.""" if self.exceedsNMAX(): return ([], 0) if self.problem.goalTest(node.state): return (self.solution(node), 0) successors = [] for action in self.problem.actions(node.state): th1 = time() #h = self.heuristic.h(node.state) h = self.problem.heuristic(node.state) th2 = time() self.timeOnHeuris = self.timeOnHeuris + (th2-th1) successors.append(self.childNode(node, action, h)) if not successors: return (False, self.INFINITY) for s in successors: s.f = max(s.pathCost + s.h, node.f) while True: [best, alternative] = self.getFirstSecondLowest(successors) if best.f > fLimit: return (False, best.f) if alternative == None: [result, best.f] = self.RBFS(best, fLimit) else: [result, best.f] = self.RBFS(best, min(fLimit, alternative.f)) if result != False: return (result, best.f)
def handleMessage(self,permid,selversion,message): """ demultiplex message stream to handlers """ # Check auth if not self.requestAllowed(permid, message[0]): return False if message[0] in self.msg_handlers: # This is a one byte id. (For instance a regular # BitTorrent message) id_ = message[0] else: if DEBUG: print >> sys.stderr, time.asctime(),'-', "olapps: No handler found for", getMessageName(message[0:2]) return False if DEBUG: print >> sys.stderr, time.asctime(),'-', "olapps: handleMessage", getMessageName(id_), "v" + str(selversion) try: if DEBUG: st = time() ret = self.msg_handlers[id_](permid, selversion, message) et = time() diff = et - st if diff > 0: print >> sys.stderr,time.asctime(),'-', "olapps: ",getMessageName(id_),"TOOK %.5f" % diff return ret else: return self.msg_handlers[id_](permid, selversion, message) except: # Catch all print_exc() return False
def __init__(self, sconfig): self.sconfig = sconfig # time of initialisation self.epoch = time() # mapping from peer ids to (shorter) numbers self.peernr = {} # remember static peer information, such as IP # self.peerinfo[id] = info string self.peerinfo = {} # remember which peers we were connected to in the last report # self.connected[id] = timestamp when last seen self.connected = {} # collected reports self.buffered_reports = [] # whether to phone home to send collected data self.do_reporting = True # send data at this interval (seconds) self.report_interval = 30 # send first report immediately self.last_report_ts = 0 # record when we started (used as a session id) self.epoch = time()
def get_ui_to(prompt, toSec=None, tSleepSec=None): # import sys from time import time, sleep from msvcrt import getch, getche, kbhit if toSec==None: # wait forever userKey = raw_input(prompt) return userKey tElapsed = 0 t0 = time() if tSleepSec == None: tSleep = 0.1*toSec else: tSleep = tSleepSec while True: if tElapsed > toSec: print "Timeout after tElapsed secs...%.3f"%tElapsed userKey = '' break print "\n", prompt, if kbhit(): userKey = getche() while kbhit(): # flush input getch() # sys.stdin.flush() # userKey = raw_input(prompt) break # print "sleep tSleep secs...%.3f"%tSleep sleep(tSleep) tNow = time() tElapsed = tNow - t0 return userKey
def turn_left(): stop_all() start_turn = time() m_right.run(80) while time() - start_turn < TURN_TIME: continue stop_all()
def actualizar(self): self.texto.texto = "Vida: " + self.vida.__str__() + " Poder: " + self.poder.__str__() + " Puntaje: " + self.puntaje.__str__() if time() - self.tiempoUltimoChori > 5: self.pilas.colisiones.agregar(self, Chori.dameElChori(self.pilas), agarrar_chori) self.tiempoUltimoChori = time() if self.poder > 0: self.municion = pilasengine.actores.Misil try: self._habilidades[1]._municion = pilasengine.actores.Misil except: pass else: self.municion = MisilInvisible self._habilidades[1]._municion = MisilInvisible ganado = True for cipayo in self.cipayos: if not cipayo.esta_eliminado(): ganado = False if ganado: self.texto.texto = "La Victoria Ha triunfado!" self.texto.x=0 self.texto.y=0 self.texto.rotacion = [0,365] for cipayo in self.cipayos: cipayo.eliminar() cipayo.eliminar() pilasengine.actores.Nave.actualizar(self)
def curl_progress(self, download_t, download_d, upload_t, upload_d): curtime=time() # TODO: Make poke frequency variable # 5 seconds isn't too much and it makes the status bar in the vSphere GUI look nice :-) if (curtime - self.time_at_last_poke) >= 5: self.lease.HttpNfcLeaseProgress(percent = int(upload_d*100/upload_t)) self.time_at_last_poke = time()
def neednewbar1(self): l = time() - self.newbar["lasttime"] if l > self.newbar["timeout"]: self.newbar["lastbar"] = time() return True else: return False
def check_for_keep_alives(self): for protocol in self.factory.protocols: now = time() if (now - protocol.message_timeout) > constants.KEEP_ALIVE_TIMEOUT: print 'Keep Alive message sent' protocol.transport.write(str(KeepAlive())) protocol.message_timeout = time()
def test_hash_cleanup_listdir(self): file_list = [] def mock_listdir(path): return list(file_list) def mock_unlink(path): file_list.remove(os.path.basename(path)) with unit_mock({"os.listdir": mock_listdir, "os.unlink": mock_unlink}): # purge .data if there's a newer .ts file1 = normalize_timestamp(time()) + ".data" file2 = normalize_timestamp(time() + 1) + ".ts" file_list = [file1, file2] self.assertEquals(diskfile.hash_cleanup_listdir("/whatever"), [file2]) # purge .ts if there's a newer .data file1 = normalize_timestamp(time()) + ".ts" file2 = normalize_timestamp(time() + 1) + ".data" file_list = [file1, file2] self.assertEquals(diskfile.hash_cleanup_listdir("/whatever"), [file2]) # keep .meta and .data if meta newer than data file1 = normalize_timestamp(time()) + ".ts" file2 = normalize_timestamp(time() + 1) + ".data" file3 = normalize_timestamp(time() + 2) + ".meta" file_list = [file1, file2, file3] self.assertEquals(diskfile.hash_cleanup_listdir("/whatever"), [file3, file2]) # keep only latest of multiple .ts files file1 = normalize_timestamp(time()) + ".ts" file2 = normalize_timestamp(time() + 1) + ".ts" file3 = normalize_timestamp(time() + 2) + ".ts" file_list = [file1, file2, file3] self.assertEquals(diskfile.hash_cleanup_listdir("/whatever"), [file3])
def SolveAI(Solution): '''Main function of the AI, call it to solve a puzzle.''' # Start chrono time_start = time() # Solve indice = Solution.getIndice() p = Pile() grid = Grid(Solution.getLargeur(), Solution.getHauteur()) grid = Treatment(grid, indice) essay = Essay(grid, False, None) p.add(essay) while p.getLenght() != 0: essay_actual = p.drop() if testEnd(essay_actual.grid, Solution) == True: # End chrono print(time()-time_start) return essay_actual.grid elif essay_actual.en_cours == False: essay_actual.en_cours = True p.add(essay_actual) new_essay = New(essay_actual) if new_essay != None: p.add(new_essay) else: next_essay = Next(essay_actual) if next_essay != None: p.add(next_essay)
def DeliverJTAG_Commands(d, JTAG_COMS_REG, commands, count, debug=False, title="DeliverJTAG_Commands"): # CheckSpeed(d, "JTAG.COMMANDS_RAM", 32000) data = [] response = [] merged = 0 i = 0 n = 0 while merged<count: data.append(0x00000000) n = n+1 i = 0 while (merged<count) and (i<8): data[n-1] = data[n-1]|(commands[(n-1)*8+i]<<4*i) merged = merged + 1 i = i +1 while (i<8): data[n-1] = data[n-1]|(0xF<<4*i) i = i +1 # JTAG_COMS_REG.writeBlock(data) # response = JTAG_COMS_REG.readBlock(n) tick = time() Upload("JTAG.COMMANDS", data) tock = time() mem = count*4.0/(8.0*1024.0*1024.0) t = tock - tick # print "Upload %d commands = %f MB took %f sec. Speed %f MB/s"%(count, mem, t, mem/t) # response = Dump(d, "JTAG.COMMANDS_RAM", len(data)) d.getNode("JTAG.COMMANDS_AMOUNT").write(len(data)) d.getNode("JTAG.STATUS").write(0) d.getNode("JTAG.CTRL").write(0) d.dispatch() if debug: for i in range(len(data)): print title, i, hex(data[i])
def setup_for_lasso(self,lamb,comps): # Setup to Solve Lasso - add the extra term w) w_init = zeros(comps+1) # The word id numbers range from 1 to 60 - we need to subtract by 1 to get the id from wfs # Y_L is actually not going to just be the ID, it'll be the entire 218 vector representing that word Y_L = [self.wfs[i-1] for i in self.wordid_train[:,0]] Y = array(Y_L) #io.mmio.mmwrite('y_out_wfs_train.out',Y,field='real',precision=25) # In order to append the 218x21764 vector final_weight_vector = zeros((218,comps+1)) for i in range(0,218): t1 = time() # This call will return 1()X21764 vector weight_vector_i = self.solve_lasso(lamb,self.fmri_train,Y,240,comps,w_init,i) final_weight_vector[i,:] = weight_vector_i t2 = time() print "Iteration {0} took about: ".format(i), t2-t1 #print shape(final_weight_vector) # Write to a file # But, first convert to sparse format, ensure the precision and field is set sparse_weight_vector = coo_matrix(final_weight_vector) io.mmio.mmwrite('lasso_pca_weight_vec{0}.out'.format(lamb),sparse_weight_vector,field='real',precision=25)
def _sync_client(server_ip, server_port): # Connects to the server # Upon connection, the server immediately sends its current time (using the time() function) to the client # The client sends back the offset between what time the client's real time clock shows and the server's time # The server then sends the time to start the event and both server and client close the socket # If the start_time sent by the server is less than 0, this function returns START_NOW # Else this function returns the number of seconds between right now and the time that the event is to occur # receive server's time, return offset, receive start time port = int(server_port) client = socket( AF_INET, SOCK_STREAM ) print("attempting to connect to " + str(server_ip)) client.connect((server_ip, server_port)) print("connected") server_time = float((client.recv(BUFSIZE)).decode()) client_time = time() print("server time is " + str(server_time) + "; client time is " + str(client_time)) offset = server_time - client_time print("sending offset of " + str(offset)) client.send((str(offset)).encode()) start_time = float((client.recv(BUFSIZE)).decode()) print("start time is " + str(start_time)) client.close() print("connection to " + str(server_ip) + " closed") if start_time < 0: return START_NOW else: return (start_time - offset) - time()
def main(): img= str(raw_input('Nombre de imagen: ')) foto=Image.open(img)#Abrir la imagen ancho,alto=foto.size t_in=time() escalada=escala(foto,ancho,alto) escalada.save('escalada.png') filtromedio=filtro_medio(escalada,ancho,alto) filtromedio.save('filtromedio.jpg') diferenciada=diferencia(escalada,filtromedio,ancho,alto) diferenciada.save('diferencia.jpg') bordes=convolucion(diferenciada,ancho,alto) bordes.save('convolucion.jpg') umbrales=umbral(diferenciada,ancho,alto) umbrales.save('umbral.jpg') binaria=binarizacion(umbrales,ancho,alto) binaria.save('Binarizacion.jpg') datos,datos2=posibles(binaria,ancho,alto) dila=dilatacion(binaria,ancho,alto,datos) dila.save('dilatacion.jpg') form,x,y=formas(dila,ancho,alto) form.save('formas.jpg') foto=Image.open(img) final=recuadro(foto,x,y) final.save('Deteccion.jpg') recortada=recortar(foto,x,y) recortada.save('Recorte.jpg') t_fi=time() total=t_fi-t_in print "Tiempo de ejecuccion:"+str(total)+" segundos"
def hp_alg(self): while True: self.initNode = self.input_initNode() self.goalNode = self.input_goalNode() if self.goalNode in g.nodeList.keys() and self.initNode in g.nodeList.keys(): break start = time() path = [] g.getNode(self.initNode).dist = 0 currNode = g.getNode(self.initNode) path.append(currNode.getName()) while True: # pdb.set_trace() if currNode.getName() == self.goalNode: self.cost = currNode.dist break n = self.rand_neighbor(currNode) path.append(n.getName()) n.dist = currNode.dist + currNode.getCost(n) n.hop_count = n.hop_count + 1 currNode = n end = time() self.get_time(start, end) print "\nHops: " + str(len(path)-1) print "Cost: " + str(self.cost) print "Path: " + str(path) print "Time: " + self.time
def ReadoutJTAG_Results(d, STATUS_REG, JTAG_RESULTS_REG, count, debug=False, title="ReadoutJTAG_Results"): N = count//8 if count%8!=0: N = N+1 tick = time() data_is_valid = False while not data_is_valid: # sleep(0.001) valid = STATUS_REG.read() d.dispatch() data_is_valid = (valid&1)==1 if not data_is_valid: if debug: pass print title, "Data is not valid yet. Retry ..." tock = time() # print "Waiting about %f seconds for data to become valid"%(tock-tick) results = Dump("JTAG.COMMANDS", N) if debug: for i in range(N): print title, i, hex(results[i]) data = [] splitted = 0 word = 0 while splitted<count: i = 0 while (splitted<count) and (i<8): data.append(0x0000000F & (results[word]>>4*i)) splitted = splitted + 1 i = i + 1 word = word + 1; return data
def gettime(self): self.buf = '' self.ftp.retrlines('LIST ' +self.urlpath,self.readftplines) if self.buf == '': self.ftp.close() error('Error! file does not exist on the server') month,day,year = self.buf.split()[5:8] hour,min = '0','0' if len(year.split(':')) >=2: hour,min = year.split(':') year = gmtime(time())[0] else: year = int(year) month_d = {'Jan':1,'Feb':2,'Mar':3,'Apr':4,'May':5,'Jun':6, 'Jul':7,'Aug':8,'Sep':9,'Oct':10,'Nov':11,'Dec':12} newtime = mktime((year,month_d[month],int(day),int(hour), int(min),0,-1,-1,0)) c_time = time() if newtime > c_time: newtime = mktime((year-1,month_d[month],split(day), split(hour),split(min),0,-1,-1,0)) self.remotetime = newtime - timezone return self.remotetime
def monitorFiles( outfiles, seconds, timeoutms ): "Monitor set of files and return [(host, line)...]" devnull = open( '/dev/null', 'w' ) tails, fdToFile, fdToHost = {}, {}, {} for h, outfile in outfiles.iteritems(): tail = Popen( [ 'tail', '-f', outfile ], stdout=PIPE, stderr=devnull ) fd = tail.stdout.fileno() tails[ h ] = tail fdToFile[ fd ] = tail.stdout fdToHost[ fd ] = h # Prepare to poll output files readable = poll() for t in tails.values(): readable.register( t.stdout.fileno(), POLLIN ) # Run until a set number of seconds have elapsed endTime = time() + seconds while time() < endTime: fdlist = readable.poll(timeoutms) if fdlist: for fd, _flags in fdlist: f = fdToFile[ fd ] host = fdToHost[ fd ] # Wait for a line of output line = f.readline().strip() yield host, line else: # If we timed out, return nothing yield None, '' for t in tails.values(): t.terminate() devnull.close() # Not really necessary
import sys import time from class_vis import prettyPicture from prep_terrain_data import makeTerrainData import numpy as np import pylab as pl features_train, labels_train, features_test, labels_test = makeTerrainData() ################################################################################# ########################## DECISION TREE ################################# #### your code goes here from classifyDT import classify Tree_Test = classify(features_train, labels_train) t0 = time() terrain_pred = Tree_Test.predict(features_test) print("prediction time:", round(time() - t0, 3), "s") print(terrain_pred) from sklearn.metrics import accuracy_score acc = accuracy_score(labels_test, terrain_pred) ### be sure to compute the accuracy on the test set def submitAccuracies(): return {"acc": round(acc, 3)}
def run(self, input_socket): # Get a file-object for reading packets from the socket. # Using this ensures that you get exactly one packet per read. f_in = input_socket.makefile() model = GRUTheano(120,8,'./model.npz') s_t1 = np.zeros(64) s_t2 = np.zeros(64) prob_random = 20 while True: start_time = time() # Block until the engine sends us a packet. data = f_in.readline().strip() # If data is None, connection has closed. if not data: print "Gameover, engine disconnected." break # When appropriate, reply to the engine with a legal action. # The engine will ignore all spurious responses. # The engine will also check/fold for you if you return an # illegal action. # When sending responses, terminate each response with a newline # character (\n) or your bot will hang! word = data.split()[0] data = data.split() print data if word == "GETACTION": # Currently CHECK on every move. You'll want to change this.s cancall = False canfold = False cancheck = False canbet = False canraise = False to_call = 0 # Get relevant information from engine self.potSize = data[1] self.numBoardCards = int(data[2]) board_card_increment = 3+self.numBoardCards self.boardCards = [data[i] for i in range(3,board_card_increment)] self.numPrevActions = int(data[board_card_increment]) prev_action_increment = board_card_increment+1+self.numPrevActions self.prevActions = [data[i] for i in range(board_card_increment+1,prev_action_increment)] # Handle discards if self.last_discard != 0: for action in self.prevActions: action_split = action.split(':') if len(action_split) == 4: if self.last_discard==1: self.holeCard1=action_split[2] if self.last_discard==2: self.holeCard2=action_split[2] # Get Previous Opponent Bet if there was one for action in self.prevActions: action_split = action.split(':') if self.otherName in action: if 'BET' in action or 'RAISE' in action: raised = int(action_split[1]) committed = float(int(self.potSize)-raised)/2 to_call = raised self.opponent_action=bet_to_action_opp(committed+raised) self.numLegalActions = int(data[prev_action_increment]) legal_action_increment = prev_action_increment+1 legal_action_increment_end = legal_action_increment+self.numLegalActions self.legalActions = [data[i] for i in range(legal_action_increment,legal_action_increment_end)] # Figure out legal actions discard_round = False for action in self.legalActions: if 'BET' in action: action_split = action.split(':') canbet = True self.bet = 1 self.minBet=int(action_split[1]) self.maxBet=int(action_split[2]) if 'RAISE' in action: action_split = action.split(':') canraise= True self.bet = 2 self.minBet=int(action_split[1]) self.maxBet=int(action_split[2]) if 'DISCARD' in action: discard_round = True if 'FOLD' in action: canfold = True if 'CHECK' in action: cancheck = True if 'CALL' in action: cancall = True textaction = 'CHECK\n' # If all logic calls through the player should check if discard_round: # Calculations for discard equity hand_probs = return_probs([self.holeCard1,self.holeCard2]+self.boardCards) drop_1_probs = return_probs([self.holeCard2]+self.boardCards) drop_2_probs = return_probs([self.holeCard1]+self.boardCards) opponent_probs = return_probs(self.boardCards,taken=[self.holeCard1,self.holeCard2]) hand_equity = matchup(hand_probs,opponent_probs) drop_1_equity = matchup(drop_1_probs,opponent_probs) drop_2_equity = matchup(drop_2_probs,opponent_probs) hand_equity_ratio = float(hand_equity[0]-hand_equity[1])+.1 drop_1_equity_ratio = float(drop_1_equity[0]-drop_1_equity[1]) drop_2_equity_ratio = float(drop_2_equity[0]-drop_2_equity[1]) decision = np.argmax([hand_equity_ratio,drop_1_equity_ratio,drop_2_equity_ratio]) if decision==2: textaction = "DISCARD:{}\n".format(self.holeCard2) self.last_discard=2 elif decision==1: textaction = "DISCARD:{}\n".format(self.holeCard1) self.last_discard=1 else: textaction = "CHECK\n" self.last_discard=0 else: action = 0 # Initialize state player_1_cards = np.zeros(52) board = np.zeros(52) player_1_cards += np.eye(52)[card_to_num(self.holeCard1)] player_1_cards += np.eye(52)[card_to_num(self.holeCard2)] for card in self.boardCards: board += np.eye(52)[card_to_num(card)] opponent_action = self.opponent_action state = np.append(np.append(np.append(player_1_cards,board),opponent_action),self.last_action) prediction = np.ones(8) prediction,s_t1,s_t2 = model.forward_prop_step(state,s_t1,s_t2) action = np.argmax(prediction) # Make prediction, during training, make a random action some percentage of the time # if np.random.randint(100)<random_prob: # action = np.random.randint(8) self.last_action = np.eye(8)[action] if len(self.states)>0: self.states += [state] left_in_hand = 0 # if self.last_board_size != len(self.boardCards) and self.last_board_size!=0: # left_in_hand = (200-float(self.potSize)/2)*(1./len(self.boardCards)) # self.last_board_size==self.boardCards self.rewards += [(0,action,False)] else: self.states = [state] left_in_hand = 0 # if self.last_board_size != len(self.boardCards) and self.last_board_size!=0: # left_in_hand = (200-float(self.potSize)/2)*(1./len(self.boardCards)) # self.last_board_size==self.boardCards self.rewards = [(0,action,False)] rangemin,rangemax = action_to_bet_range(action) committed = float(int(self.potSize)-to_call)/2 print action for e in prediction: print e # Logic to allow bot to commit on the position it has decided on if cancall: if canfold and (committed+float(to_call))>rangemax: textaction = 'FOLD\n' elif canraise and rangemax-committed-to_call>self.minBet: bet_low = max(rangemin,committed)-committed bet_high = rangemax-committed bet_high = min(self.maxBet,bet_high) bet_low = max(self.minBet,bet_low) bet= np.random.randint(bet_low,bet_high) if bet_low!=bet_high else bet_low textaction = 'RAISE:{}\n'.format(bet) else: textaction = 'CALL\n' else: if canbet and rangemax-committed-to_call>self.minBet: bet_low = max(rangemin,committed)-committed bet_high = rangemax-committed bet_high = min(self.maxBet,bet_high) bet_low = max(self.minBet,bet_low) bet= np.random.randint(bet_low,bet_high) if bet_low!=bet_high else bet_low textaction = 'BET:{}\n'.format(bet) else: textaction = 'CHECK\n' s.send(textaction) elif word == "NEWHAND": self.holeCard1 = data[3] self.holeCard2 = data[4] self.last_board_size = 0 self.last_action = bet_0_25 self.opponent_action= opp_bet_0_25 elif word == "NEWGAME": self.yourName = data[1] self.otherName = data[2] elif word == "REQUESTKEYVALUES": # At the end, the engine will allow your bot save key/value pairs. # Send FINISH to indicate you're done. s.send("FINISH\n") # Clean up the socket. s.close()
ax = fig.add_subplot(1, 1, 1, projection='3d') for i in range(X.shape[0]): ax.text(X[i, 0], X[i, 1], X[i,2],str(digits.target[i]), color=plt.cm.Set1(y[i] / 10.), fontdict={'weight': 'bold', 'size': 9}) if title is not None: plt.title(title) if __name__ == '__main__': digits = dt.load_digits(n_class= 5) X = digits.data Y = digits.target print X.shape n_img_per_row = 20 img = np.zeros((10 * n_img_per_row, 10 * n_img_per_row)) for i in range(n_img_per_row): ix = 10 * i + 1 for j in range(n_img_per_row): iy = 10 * j + 1 img[ix:ix + 8, iy:iy + 8] = X[i * n_img_per_row + j].reshape((8, 8)) plt.imshow(img, cmap=plt.cm.binary) plt.title('A selection from the 64-dimensional digits dataset') print("Computing t-SNE embedding") print("Computing t-SNE embedding") tsne = manifold.TSNE(n_components=3, init='pca', random_state=0) t0 = time() X_tsne = tsne.fit_transform(X) # plot_embedding_2d(X_tsne[:,0:2],"t-SNE 2D") plot_embedding_3d(X_tsne,"t-SNE 3D (time %.2fs)" %(time() - t0))
def BeVRVH(d,dev2mac,dev2name,dev2net,mac2dev,mac2ip,mac2net,net2mac,arpcache,rtTable): done=False stop=False dest=None mac=None net=None sendSckt=None sTime=0 scktList=[] scktList.append(dev2name[d][1][1]) # sckt to interp for n in dev2name: # setup select list if dev2name[n][0]=='vs': for m in net2mac[dev2net[n]]: if m[0] in dev2mac[d]: scktList.append(m[1][0]) while not done: (sckt,unused,unused)=select.select(scktList,[],[],0) if sTime>0 and time()-sTime>1 and len(sckt)==0: sTime=0 print(d+': arpreq','to unknown Hostname time out') continue for s in sckt: pkt=pickle.loads(s.recv(255)) if(pkt[0]=='STOP'): stop=True elif len(pkt)>2 and pkt[2]==1: # arp req pkt rcvd for item in dev2mac[d]: if mac2ip[item]==pkt[4][1]: # arp req for this host,send reply sendSckt=None for i in net2mac: # get sckt to send pkt to if sendSckt==None: for x in net2mac[i]: if dev2mac[pkt[4][0]][0]==x[0]: sendSckt=x[1][0] break if sendSckt != None: # sckt found,send stuff arp_reply=[pkt[4][0],dev2mac[d],2,0,[pkt[4],dev2mac[d]]] arp_reply[3]=sys.getsizeof(arp_reply) sendSckt.send(pickle.dumps(arp_reply)) elif len(pkt)>2 and pkt[2]==2: # recvd arp reply sTime=0 print(mac2dev[pkt[1][0]]+": arpreply to "+str(pkt[0])+" on "+str(dev2mac[d])+": "+str([str(pkt[4][0][1]),pkt[4][1][0]])) arpcache[pkt[4][0][1]]=pkt[4][1][0] elif pkt[2]==3 or pkt[2]==4: # iptest if pkt[2]==4: pkt[4][4].append(d) if pkt[2]==4 and pkt[4][1]==d: print(d+': received traceroute from '+pkt[4][0]+'; route: \n\t'+str(pkt[4][4])) elif pkt[2]==3 and pkt[4][1]==d: print(d+': received ping from '+pkt[4][0]) elif pkt[2]==3 and pkt[4][1] in mac2ip[dev2mac[d][0]]: print(d+': received ping from '+pkt[4][0]) else: if pkt[4][1].find('.') >= 0: net=pkt[4][1].split('.')[0] else: net=dev2net[pkt[4][1]][0] for route in rtTable[d]: if route[0]==net: mac=route[1] if len(route) > 2: for m in mac2ip: if mac2ip[m]==route[2]: dest=m break else: if pkt[4][1].find('.') >= 0: for m in mac2ip: if mac2ip[m]==pkt[4][1]: dest=m break else: dest=dev2mac[pkt[4][1]][0] break elif route[0]=='def': mac=route[1] for m in mac2ip: if mac2ip[m]==route[2]: dest=m if mac==None: print(d+': **** no route to host: '+pkt[4][1]) break sendSckt=None for i in net2mac: if sendSckt==None: for x in net2mac[i]: if x[0]==mac: sendSckt=x[1][0] break if sendSckt != None: ip_pkt=[pkt[4][0],pkt[4][1],0,0,pkt[4][4]] mac_pkt=[dest,mac,pkt[2],0,ip_pkt] sendSckt.send(pickle.dumps(mac_pkt)) else: # macsend if pkt[0]=='macsend': if pkt[3]=='255': to_hostname='255' else: to_hostname=mac2dev[pkt[3]] print(d+": macsend to "+str(to_hostname)+" on "+pkt[2]+": "+pkt[1]) net_to_send=mac2ip[pkt[2]].split('.')[0] sendSckt=None for i in net2mac: # get sckt if sendSckt==None: for x in net2mac[i]: if pkt[2]==x[0]: sendSckt=x[1][0] break if sendSckt != None: # send stuff if found sckt mac_pkt=[pkt[3],pkt[2],0,0,pkt[1]] mac_pkt[3]=sys.getsizeof(mac_pkt) sendSckt.send(pickle.dumps(mac_pkt)) elif pkt[0]=='arptest': # send arp req to VS sendSckt=None for i in net2mac: # get sckt if sendSckt==None: for x in net2mac[i]: for mac in dev2mac[pkt[1]]: if mac2ip[mac].split('.')[0]==pkt[2].split('.')[0] and mac==x[0]: sendSckt=x[1][0] break if sendSckt != None: # send stuff if found sckt print(d+": arpreq to 255 on "+str(dev2mac[d])+": "+pkt[2]) arp_pkt=['255',pkt[1],1,0,[pkt[1],pkt[2]]] arp_pkt[3]=sys.getsizeof(arp_pkt) sendSckt.send(pickle.dumps(arp_pkt)) sTime=time() elif pkt[0]=='arpprt': print('[ARP Cache for '+d+']: '+str(arpcache)) elif pkt[0]=='iptest' or pkt[0]=='trtest': if pkt[0]=='iptest': print(d+': sent ping to '+pkt[2]) elif pkt[0]=='trtest': print(d+': sent traceroute to '+pkt[2]) if pkt[2].find('.') >= 0: net=pkt[2].split('.')[0] else: net=(mac2ip[dev2mac[pkt[2]][0]]).split('.')[0] for route in rtTable[d]: if route[0]==net: mac=route[1] if len(route) > 2: for m in mac2ip: if mac2ip[m]==route[2]: dest=m break else: if pkt[2].find('.') >= 0: for m in mac2ip: if mac2ip[m]==pkt[2]: dest=m break else: dest=dev2mac[pkt[2]][0] break elif route[0]=='def': mac=route[1] for m in mac2ip: if mac2ip[m]==route[2]: dest=m break if mac==None: print(d+': **** no route to host: '+pkt[2]) sendSckt=None for i in net2mac: if sendSckt==None: for x in net2mac[i]: if x[0]==mac: sendSckt=x[1][0] break if sendSckt != None: ip_pkt=[pkt[1],pkt[2],0,0,[]] code=3 if pkt[0]=='trtest': code=4 ip_pkt[4].append(d) mac_pkt=[dest,dev2mac[pkt[1]][0],code,0,ip_pkt] sendSckt.send(pickle.dumps(mac_pkt)) else: # only when VR/VS recvs from other VR/VS,not interp if pkt[0]=='255': from_hostname=mac2dev[pkt[1]] # bcast msg else: from_hostname=mac2dev[pkt[0]] # msg to this host print(d+": macsend from "+str(from_hostname)+" on "+str(pkt[1])+": "+pkt[4]) if stop: done=True break
print(train_X.shape, train_y.shape, test_X.shape, test_y.shape) # ------- Keras LSTM Model --------- # What does it mean that the internal state of the LSTM is reset at the end of each batch? # Answer: # # TRAIN MODEL model = Sequential() model.add(LSTM(50, input_shape=(train_X.shape[1], train_X.shape[2]))) model.add(Dense(1)) model.compile(loss='mae', optimizer='adam') # TENSORBOARD tensorboard = TensorBoard(log_dir="logs/{}".format(time())) # fit network start = time.time() history = model.fit(train_X, train_y, epochs=50, batch_size=256, validation_data=(test_X, test_y), verbose=2, shuffle=False, callbacks=[tensorboard]) # plot history plt.plot(history.history['loss'], label='train') plt.plot(history.history['val_loss'], label='test') plt.legend()
def main(): import sys import os import math import random import time from random import random, seed, randint from optparse import OptionParser from global_def import ERROR progname = os.path.basename(sys.argv[0]) usage = progname + """ [options] <inputfile> <outputfile> Forms chains of 2D images based on their similarities. Functionality: Order a 2-D stack of image based on pair-wise similarity (computed as a cross-correlation coefficent). Options 1-3 require image stack to be aligned. The program will apply orientation parameters if present in headers. The ways to use the program: 1. Use option initial to specify which image will be used as an initial seed to form the chain. sxchains.py input_stack.hdf output_stack.hdf --initial=23 --radius=25 2. If options initial is omitted, the program will determine which image best serves as initial seed to form the chain sxchains.py input_stack.hdf output_stack.hdf --radius=25 3. Use option circular to form a circular chain. sxchains.py input_stack.hdf output_stack.hdf --circular--radius=25 4. New circular code based on pairwise alignments sxchains.py aclf.hdf chain.hdf circle.hdf --align --radius=25 --xr=2 --pairwiseccc=lcc.txt 5. Circular ordering based on pairwise alignments sxchains.py vols.hdf chain.hdf mask.hdf --dd --radius=25 """ parser = OptionParser(usage, version=SPARXVERSION) parser.add_option( "--dd", action="store_true", help="Circular ordering without adjustment of orientations", default=False) parser.add_option( "--circular", action="store_true", help= "Select circular ordering (first image has to be similar to the last)", default=False) parser.add_option( "--align", action="store_true", help= "Compute all pairwise alignments and from the table of image similarities find the best chain", default=False) parser.add_option( "--initial", type="int", default=-1, help= "Specifies which image will be used as an initial seed to form the chain. (default = 0, means the first image)" ) parser.add_option( "--radius", type="int", default=-1, help="Radius of a circular mask for similarity based ordering") # import params for 2D alignment parser.add_option( "--ou", type="int", default=-1, help= "outer radius for 2D alignment < nx/2-1 (set to the radius of the particle)" ) parser.add_option( "--xr", type="int", default=0, help="range for translation search in x direction, search is +/xr (0)") parser.add_option( "--yr", type="int", default=0, help="range for translation search in y direction, search is +/yr (0)") #parser.add_option("--nomirror", action="store_true", default=False, help="Disable checking mirror orientations of images (default False)") parser.add_option("--pairwiseccc", type="string", default=" ", help="Input/output pairwise ccc file") (options, args) = parser.parse_args() global_def.BATCH = True if options.dd: nargs = len(args) if nargs != 3: print("must provide name of input and two output files!") return stack = args[0] new_stack = args[1] from utilities import model_circle from pap_statistics import ccc from pap_statistics import mono lend = EMUtil.get_image_count(stack) lccc = [None] * (old_div(lend * (lend - 1), 2)) for i in range(lend - 1): v1 = get_im(stack, i) if (i == 0 and nargs == 2): nx = v1.get_xsize() ny = v1.get_ysize() nz = v1.get_ysize() if options.ou < 1: radius = nx // 2 - 2 else: radius = options.ou mask = model_circle(radius, nx, ny, nz) else: mask = get_im(args[2]) for j in range(i + 1, lend): lccc[mono(i, j)] = [ccc(v1, get_im(stack, j), mask), 0] order = tsp(lccc) if (len(order) != lend): print(" problem with data length") from sys import exit exit() print("Total sum of cccs :", TotalDistance(order, lccc)) print("ordering :", order) for i in range(lend): get_im(stack, order[i]).write_image(new_stack, i) elif options.align: nargs = len(args) if nargs != 3: print("must provide name of input and two output files!") return from utilities import get_params2D, model_circle from fundamentals import rot_shift2D from pap_statistics import ccc from time import time from alignment import align2d, align2d_scf stack = args[0] new_stack = args[1] d = EMData.read_images(stack) if (len(d) < 6): ERROR( "Chains requires at least six images in the input stack to be executed", "sxchains.py", 1) """ # will align anyway try: ttt = d[0].get_attr('xform.params2d') for i in xrange(len(d)): alpha, sx, sy, mirror, scale = get_params2D(d[i]) d[i] = rot_shift2D(d[i], alpha, sx, sy, mirror) except: pass """ nx = d[0].get_xsize() ny = d[0].get_ysize() if options.ou < 1: radius = nx // 2 - 2 else: radius = options.ou mask = model_circle(radius, nx, ny) if (options.xr < 0): xrng = 0 else: xrng = options.xr if (options.yr < 0): yrng = xrng else: yrng = options.yr initial = max(options.initial, 0) from pap_statistics import mono lend = len(d) lccc = [None] * (old_div(lend * (lend - 1), 2)) from utilities import read_text_row if options.pairwiseccc == " " or not os.path.exists( options.pairwiseccc): st = time() for i in range(lend - 1): for j in range(i + 1, lend): # j>i meaning mono entry (i,j) or (j,i) indicates T i->j (from smaller index to larger) #alpha, sx, sy, mir, peak = align2d(d[i],d[j], xrng, yrng, step=options.ts, first_ring=options.ir, last_ring=radius, mode = "F") alpha, sx, sy, mir, peak = align2d_scf(d[i], d[j], xrng, yrng, ou=radius) lccc[mono(i, j)] = [ ccc(d[j], rot_shift2D(d[i], alpha, sx, sy, mir, 1.0), mask), alpha, sx, sy, mir ] #print " %4d %10.1f"%(i,time()-st) if ((not os.path.exists(options.pairwiseccc)) and (options.pairwiseccc != " ")): from utilities import write_text_row write_text_row([[initial, 0, 0, 0, 0]] + lccc, options.pairwiseccc) elif (os.path.exists(options.pairwiseccc)): lccc = read_text_row(options.pairwiseccc) initial = int(lccc[0][0] + 0.1) del lccc[0] for i in range(len(lccc)): T = Transform({ "type": "2D", "alpha": lccc[i][1], "tx": lccc[i][2], "ty": lccc[i][3], "mirror": int(lccc[i][4] + 0.1) }) lccc[i] = [lccc[i][0], T] tdummy = Transform({"type": "2D"}) maxsum = -1.023 for m in range(0, lend): #initial, initial+1): indc = list(range(lend)) lsnake = [[m, tdummy, 0.0]] del indc[m] lsum = 0.0 while len(indc) > 1: maxcit = -111. for i in range(len(indc)): cuc = lccc[mono(indc[i], lsnake[-1][0])][0] if cuc > maxcit: maxcit = cuc qi = indc[i] # Here we need transformation from the current to the previous, # meaning indc[i] -> lsnake[-1][0] T = lccc[mono(indc[i], lsnake[-1][0])][1] # If direction is from larger to smaller index, the transformation has to be inverted if (indc[i] > lsnake[-1][0]): T = T.inverse() lsnake.append([qi, T, maxcit]) lsum += maxcit del indc[indc.index(qi)] T = lccc[mono(indc[-1], lsnake[-1][0])][1] if (indc[-1] > lsnake[-1][0]): T = T.inverse() lsnake.append( [indc[-1], T, lccc[mono(indc[-1], lsnake[-1][0])][0]]) print(" initial image and lsum ", m, lsum) #print lsnake if (lsum > maxsum): maxsum = lsum init = m snake = [lsnake[i] for i in range(lend)] print(" Initial image selected : ", init, maxsum, " ", TotalDistance([snake[m][0] for m in range(lend)], lccc)) #for q in snake: print q from copy import deepcopy trans = deepcopy([snake[i][1] for i in range(len(snake))]) print([snake[i][0] for i in range(len(snake))]) """ for m in xrange(lend): prms = trans[m].get_params("2D") print " %3d %7.1f %7.1f %7.1f %2d %6.2f"%(snake[m][0], prms["alpha"], prms["tx"], prms["ty"], prms["mirror"], snake[m][2]) """ for k in range(lend - 2, 0, -1): T = snake[k][1] for i in range(k + 1, lend): trans[i] = T * trans[i] # To add - apply all transformations and do the overall centering. for m in range(lend): prms = trans[m].get_params("2D") #print " %3d %7.1f %7.1f %7.1f %2d %6.2f"%(snake[m][0], prms["alpha"], prms["tx"], prms["ty"], prms["mirror"], snake[m][2]) #rot_shift2D(d[snake[m][0]], prms["alpha"], prms["tx"], prms["ty"], prms["mirror"]).write_image(new_stack, m) rot_shift2D(d[snake[m][0]], prms["alpha"], 0.0, 0.0, prms["mirror"]).write_image(new_stack, m) order = tsp(lccc) if (len(order) != lend): print(" problem with data length") from sys import exit exit() print(TotalDistance(order, lccc)) print(order) ibeg = order.index(init) order = [order[(i + ibeg) % lend] for i in range(lend)] print(TotalDistance(order, lccc)) print(order) snake = [tdummy] for i in range(1, lend): # Here we need transformation from the current to the previous, # meaning order[i] -> order[i-1]] T = lccc[mono(order[i], order[i - 1])][1] # If direction is from larger to smaller index, the transformation has to be inverted if (order[i] > order[i - 1]): T = T.inverse() snake.append(T) assert (len(snake) == lend) from copy import deepcopy trans = deepcopy(snake) for k in range(lend - 2, 0, -1): T = snake[k] for i in range(k + 1, lend): trans[i] = T * trans[i] # Try to smooth the angles - complicated, I am afraid one would have to use angles forward and backwards # and find their average?? # In addition, one would have to recenter them """ trms = [] for m in xrange(lend): prms = trans[m].get_params("2D") trms.append([prms["alpha"], prms["mirror"]]) for i in xrange(3): for m in xrange(lend): mb = (m-1)%lend me = (m+1)%lend # angles order mb,m,me # calculate predicted angles mb->m """ for m in range(lend): prms = trans[m].get_params("2D") #rot_shift2D(d[order[m]], prms["alpha"], prms["tx"], prms["ty"], prms["mirror"]).write_image("metro.hdf", m) rot_shift2D(d[order[m]], prms["alpha"], 0.0, 0.0, prms["mirror"]).write_image(args[2], m) """ # This was an effort to get number of loops, inconclusive, to say the least from numpy import outer, zeros, float32, sqrt lend = len(d) cor = zeros(lend,float32) cor = outer(cor, cor) for i in xrange(lend): cor[i][i] = 1.0 for i in xrange(lend-1): for j in xrange(i+1, lend): cor[i,j] = lccc[mono(i,j)][0] cor[j,i] = cor[i,j] lmbd, eigvec = pca(cor) from utilities import write_text_file nvec=20 print [lmbd[j] for j in xrange(nvec)] print " G" mm = [-1]*lend for i in xrange(lend): # row mi = -1.0e23 for j in xrange(nvec): qt = eigvec[j][i] if(abs(qt)>mi): mi = abs(qt) mm[i] = j for j in xrange(nvec): qt = eigvec[j][i] print round(qt,3), # eigenvector print mm[i] print for j in xrange(nvec): qt = [] for i in xrange(lend): if(mm[i] == j): qt.append(i) if(len(qt)>0): write_text_file(qt,"loop%02d.txt"%j) """ """ print [lmbd[j] for j in xrange(nvec)] print " B" mm = [-1]*lend for i in xrange(lend): # row mi = -1.0e23 for j in xrange(nvec): qt = eigvec[j][i]/sqrt(lmbd[j]) if(abs(qt)>mi): mi = abs(qt) mm[i] = j for j in xrange(nvec): qt = eigvec[j][i]/sqrt(lmbd[j]) print round(qt,3), # eigenvector print mm[i] print """ """ lend=3 cor = zeros(lend,float32) cor = outer(cor, cor) cor[0][0] =136.77 cor[0][1] = 79.15 cor[0][2] = 37.13 cor[1][0] = 79.15 cor[2][0] = 37.13 cor[1][1] = 50.04 cor[1][2] = 21.65 cor[2][1] = 21.65 cor[2][2] = 13.26 lmbd, eigvec = pca(cor) print lmbd print eigvec for i in xrange(lend): # row for j in xrange(lend): print eigvec[j][i], # eigenvector print print " B" for i in xrange(lend): # row for j in xrange(lend): print eigvec[j][i]/sqrt(lmbd[j]), # eigenvector print print " G" for i in xrange(lend): # row for j in xrange(lend): print eigvec[j][i]*sqrt(lmbd[j]), # eigenvector print """ else: nargs = len(args) if nargs != 2: print("must provide name of input and output file!") return from utilities import get_params2D, model_circle from fundamentals import rot_shift2D from pap_statistics import ccc from time import time from alignment import align2d stack = args[0] new_stack = args[1] d = EMData.read_images(stack) try: print("Using 2D alignment parameters from header.") ttt = d[0].get_attr('xform.params2d') for i in range(len(d)): alpha, sx, sy, mirror, scale = get_params2D(d[i]) d[i] = rot_shift2D(d[i], alpha, sx, sy, mirror) except: pass nx = d[0].get_xsize() ny = d[0].get_ysize() if options.radius < 1: radius = nx // 2 - 2 else: radius = options.radius mask = model_circle(radius, nx, ny) init = options.initial if init > -1: print(" initial image: %d" % init) temp = d[init].copy() temp.write_image(new_stack, 0) del d[init] k = 1 lsum = 0.0 while len(d) > 1: maxcit = -111. for i in range(len(d)): cuc = ccc(d[i], temp, mask) if cuc > maxcit: maxcit = cuc qi = i # print k, maxcit lsum += maxcit temp = d[qi].copy() del d[qi] temp.write_image(new_stack, k) k += 1 print(lsum) d[0].write_image(new_stack, k) else: if options.circular: print("Using options.circular, no alignment") # figure the "best circular" starting image maxsum = -1.023 for m in range(len(d)): indc = list(range(len(d))) lsnake = [-1] * (len(d) + 1) lsnake[0] = m lsnake[-1] = m del indc[m] temp = d[m].copy() lsum = 0.0 direction = +1 k = 1 while len(indc) > 1: maxcit = -111. for i in range(len(indc)): cuc = ccc(d[indc[i]], temp, mask) if cuc > maxcit: maxcit = cuc qi = indc[i] lsnake[k] = qi lsum += maxcit del indc[indc.index(qi)] direction = -direction for i in range(1, len(d)): if (direction > 0): if (lsnake[i] == -1): temp = d[lsnake[i - 1]].copy() #print " forw ",lsnake[i-1] k = i break else: if (lsnake[len(d) - i] == -1): temp = d[lsnake[len(d) - i + 1]].copy() #print " back ",lsnake[len(d) - i +1] k = len(d) - i break lsnake[lsnake.index(-1)] = indc[-1] #print " initial image and lsum ",m,lsum #print lsnake if (lsum > maxsum): maxsum = lsum init = m snake = [lsnake[i] for i in range(len(d))] print(" Initial image selected : ", init, maxsum) print(lsnake) for m in range(len(d)): d[snake[m]].write_image(new_stack, m) else: # figure the "best" starting image print("Straight chain, no alignment") maxsum = -1.023 for m in range(len(d)): indc = list(range(len(d))) lsnake = [m] del indc[m] temp = d[m].copy() lsum = 0.0 while len(indc) > 1: maxcit = -111. for i in range(len(indc)): cuc = ccc(d[indc[i]], temp, mask) if cuc > maxcit: maxcit = cuc qi = indc[i] lsnake.append(qi) lsum += maxcit temp = d[qi].copy() del indc[indc.index(qi)] lsnake.append(indc[-1]) #print " initial image and lsum ",m,lsum #print lsnake if (lsum > maxsum): maxsum = lsum init = m snake = [lsnake[i] for i in range(len(d))] print(" Initial image selected : ", init, maxsum) print(lsnake) for m in range(len(d)): d[snake[m]].write_image(new_stack, m)
def uniqid(): """ Return a unique id """ from time import time return hex(int(time()*10000000))[2:]
import copy import getopt import javalang import os import re import sys import time from timeit import default_timer as time Debug = False Type_name = None Validators = {} Show_valid = False Start_time = time() class JavaParser: def __init__(self, file_path): file = open(file_path) source = file.read() file.close() try: self.tree = javalang.parse.parse(source) except: print file_path def get_node(self, tree): result = {}
def end(self, name): from time import time if name in self.times: self.times[name][1] += time() - self.times[name][0] self.times[name][3] = True
def print_report(total_only=False, detail_only=False): total_file_count = 0 total_required_count = 0 total_missing_count = 0 total_valid_count = 0 total_invalid_count = 0 current_file_count = 0 current_required_count = 0 current_missing_count = 0 current_valid_count = 0 current_invalid_count = 0 current_directory = None for file_path in sorted(Validators): directory = file_path[:file_path.find('/')] if current_directory is None or current_directory != directory: if total_only is False and current_directory is not None and detail_only is False: print_summary_report(current_file_count, current_required_count, current_missing_count, current_invalid_count, current_valid_count) current_file_count = 0 current_required_count = 0 current_missing_count = 0 current_valid_count = 0 current_invalid_count = 0 current_directory = directory if total_only is False and detail_only is False: print("\n" + current_directory + "\n" + '=' * len(current_directory) + "\n") total_file_count += 1 current_file_count += 1 total_required_count += len(Validators[file_path]) current_required_count += len(Validators[file_path]) show_path = False for validator in Validators[file_path]: if Type_name is not None and validator.tree["name"] != Type_name: continue report = validator.get_report() if total_only is False and validator.valid is not True and show_path is False: print(file_path) show_path = True if total_only is False and validator.valid is not True or Show_valid is True: print("\t" + validator.get_definition()) print("\t\t" + "\n\t".join(report)) if validator.valid is True: total_valid_count += 1 current_valid_count += 1 elif validator.missing is True: total_missing_count += 1 current_missing_count += 1 else: total_invalid_count += 1 current_invalid_count += 1 if total_only is False and detail_only is False: print_summary_report(current_file_count, current_required_count, current_missing_count, current_invalid_count, current_valid_count) if detail_only is False: print("Total Repository") print_summary_report(total_file_count, total_required_count, total_missing_count, total_invalid_count, total_valid_count) print("") print("Elapsed %4.2f s" % (time() - Start_time)) return total_valid_count == total_required_count
def test_with_context_args(self): from sh import time, ls with time(verbose=True, _with=True): out = ls().stderr self.assertTrue("Voluntary context switches" in out)
from collections import * from itertools import * from random import * from time import * from functools import * from fractions import * from math import * #from pe_lib import PrimeSieve ''' ''' f = open("p079_keylog.txt", "r") a = f.read() f.close() a = a.split("\n") print(a) def main(): print('HELLO WORLD') start = time() print('\n\n') print(main()) print('Program took %.02f seconds' % (time() - start))
# импортирование # импорт из стандартной библиотеки # 1 способ import time import random print(time.time()) print(random.random()) # 2 способ from time import time from random import random print(time()) print(random()) # импорт из собственного модуля # 1 способ import my_functions my_functions.show_msg() print(my_functions.simple_calc()) # 2 способ from my_functions import show_msg, simple_calc show_msg() print(simple_calc()) import my_functions as mfunc
def test_err_to_out(self): from sh import time, ls with time(_with=True): out = ls(_err_to_out=True) self.assertTrue("pagefaults" in out)
def infer_on_stream(args, client): """ Initialize the inference network, stream video to network, and output stats and video. :param args: Command line arguments parsed by `build_argparser()` :param client: MQTT client :return: None """ # Initialise the class # Load the weights and configutation to form the pretrained YOLOv3 model net = cv2.dnn.readNetFromDarknet("yolov3.cfg", "yolov3.weights") # Get the output layer names of the model layer_names = net.getLayerNames() layer_names = [layer_names[i[0] - 1] for i in net.getUnconnectedOutLayers()] # Set Probability threshold for detections prob_threshold = args.prob_threshold #get input shape net_input_shape = [1,3,416,416] ### TODO: Handle the input stream ### #check for input stream is a cam? input_stream = 0 if args.input == "cam" else args.input try: cap = cv2.VideoCapture(input_stream) except FileNotFoundError: print("File {} not available".format(input_stream)) except Exception as e: print("error on loading file:{}".format(e)) exit(1) number_input_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT)) number_input_frames = 1 if number_input_frames != -1 and number_input_frames < 0 else number_input_frames #variable initialisation current_count = 0 # hold current people in the frame total_count = 0 # hold total people passed by the frame duration = 0 # hold duration of people in the frame person_num_trigger = 3 # maximum total number before alarm min_duration = 20 # minimum time (s) before alarm k_ref = 5 # number of frames before account for non detection k = 0 det_time = [] #inference times array track_time = {} # tracked people in the frame entering start time current_id = [] # current people id in the frame mot_tracker = Sort(5,1) # initialize SORT algorithm for people tracking cap.open(input_stream) ### TODO: Loop until stream is over ### while cap.isOpened(): ### TODO: Read from the video capture ### flag, frame = cap.read() if not flag: break key_pressed = cv2.waitKey(60) ### TODO: Pre-process the image as needed ### #p_frame = cv2.resize(frame, (net_input_shape[3], net_input_shape[2])) #p_frame = p_frame.transpose((2,0,1)) #p_frame = p_frame.reshape(1, *p_frame.shape) # Contructing a blob from the input image blob = cv2.dnn.blobFromImage(frame, 1 / 255.0, (net_input_shape[3], net_input_shape[2]),swapRB=True, crop=False) ### TODO: Start asynchronous inference for specified request ### # Perform a forward pass of the YOLO object detector net.setInput(blob) #log.info("Starting inference...") start_time = time() outs = net.forward(layer_names) det_time.append( time() - start_time) objects = list() list_object=[] #print(key_pressed) ### TODO: Extract any desired stats from the results ###pip start_time = time() objects = process_result_cv(frame,outs,prob_threshold,prob_threshold) parsing_time = time() - start_time # Draw performance stats over frame inf_time_message = "Inference time: {:.3f} ms **** Inference mean time :{:.3f} ms ".format(det_time[-1] * 1e3,sum(det_time)*1e3/len(det_time)) parsing_message = "YOLO parsing time is {:.3f} ms".format(parsing_time * 1e3) cv2.putText(frame, inf_time_message, (15, 15), cv2.FONT_HERSHEY_COMPLEX, 0.5, (200, 10, 10), 1) cv2.putText(frame, parsing_message, (15, 30), cv2.FONT_HERSHEY_COMPLEX, 0.5, (10, 10, 200), 1) ### TODO: Calculate and send relevant information on ### ### current_count, total_count and duration to the MQTT server ### ### Topic "person": keys of "count" and "total" ### ### Topic "person/duration": key of "duration" ### num_detection = len(objects) #prepare input of SORT algorithm for obj in objects: list_object.append([obj["xmin"],obj["ymin"],obj["xmax"],obj["ymax"],obj["confidence"]]) # initialize tracking by sending input of SORT if num_detection > 0: tracked_objects = mot_tracker.update(np.array(list_object)) else: tracked_objects = mot_tracker.update(np.empty((0,5))) # check for new detection or exit of the frame delta = len(tracked_objects) - current_count # if new detection update current_count and current_id and initialise K if delta> 0 : current_count = len(tracked_objects) current_id = np.array(tracked_objects[:,4],dtype = int) k = 0 #if non detection but frame count less than k_ref, just update k elif delta < 0 and k <= k_ref: k += 1 #if non detection but frame count more than k_ref, count exit elif delta < 0 and k > k_ref: current_count = len(tracked_objects) current_id = np.array(tracked_objects[:,4],dtype = int) k = 0 #if non detection initialize k elif delta == 0 : k = 0 #list of id of exited people track_out = [] # loop to add start time, draw bounding box and text for each new detection for xmin, ymin, xmax, ymax, obj_id in tracked_objects: if str(int(obj_id)) not in track_time.keys() and delta >=0 : track_time[str(int(obj_id))] = time() time_spent = int(time() - track_time[str(int(obj_id))]) color = (int(min(1 * 12.5, 255)),min(1* 7, 255), min(1 * 5, 255)) cv2.rectangle(frame, (int(xmin), int(ymin)),(int(xmax), int(ymax)), color, 2) cv2.putText(frame,'#id:'+str(int(obj_id))+'| time:'+str(time_spent)+'s',(int(xmin), int(ymax - 7)), cv2.FONT_HERSHEY_COMPLEX, 0.8, (0,255,0), 1) #check for time spent mor than min time if time_spent> min_duration and time_spent < 3600 : cv2.putText(frame," > {} s".format(k_ref),(int(xmin), int(ymax - 45)), cv2.FONT_HERSHEY_COMPLEX, 0.8, (0,0,255), 1) # sent mqtt message for "person" topic MQTT_MSG_PERSON=json.dumps({"count": current_count }) client.publish("person", MQTT_MSG_PERSON) #check for exited people id and if so, send mqtt message for "person/duration" topic for key, data in track_time.items(): if int(key) not in current_id: duration = int(time()- data) total_count += 1 track_out.append(key) MQTT_MSG_DURATION=json.dumps({"duration": duration}) client.publish("person/duration", MQTT_MSG_DURATION) for i in track_out: del track_time[i] #check for total count number compared to person_num_trigger if total_count > person_num_trigger : cv2.putText(frame, "number: {} of total people more than trigger limit:{}".format(total_count,person_num_trigger), (15, 45), cv2.FONT_HERSHEY_COMPLEX, 0.5, (10, 10, 200), 1) ### TODO: Send the frame to the FFMPEG server ### sys.stdout.buffer.write(frame) sys.stdout.flush() ### TODO: Write an output image if `single_image_mode` ### if number_input_frames == 1: cv2.imwrite("out.png", frame) # Break if escape key pressed if key_pressed == 27: break # Release the capture and destroy any OpenCV windows cap.release() cv2.destroyAllWindows()
def bypass_hcaptcha(url): """ :param url: url to page which gives hcaptcha :return: Returns Response object (cookies stored for future use) """ host = urlparse(url).netloc bypassed = False session = requests.session() headers = { 'User-Agent': choice(( 'Mozilla/5.0 (Windows NT 6.2; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.90 Safari/537.36', 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_4) AppleWebKit/605.1.15 (KHTML, like Gecko)', 'Mozilla/5.0 (iPad; CPU OS 9_3_5 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Mobile/13G36', 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.130 Safari/537.36' )) } logger.info("Bypassing captcha...") #Retry until success while not bypassed: site_key = str(uuid4()) response = session.post('https://hcaptcha.com/getcaptcha', headers = headers, data = { 'sitekey': site_key, 'host': host }).json() try: key = response['key'] tasks = [row['task_key'] for row in response['tasklist']] job = response['request_type'] timestamp = round(time()) + choice(range(30, 120)) answers = dict(zip(tasks, [choice(['true', 'false']) for index in range(len(tasks))])) mouse_movements = [] last_movement = timestamp for index in range(choice(range(1000, 10000))): last_movement += choice(range(10)) mouse_movements.append([choice(range(500)), choice(range(500)), last_movement]) json = { 'job_mode': job, 'answers': answers, 'serverdomain': host, 'sitekey': site_key, 'motionData': { 'st': timestamp, 'dct': timestamp, 'mm': mouse_movements } } response = session.post(f'https://hcaptcha.com/checkcaptcha/{key}', json = json) response = response.json() bypassed = response['pass'] except (TypeError, KeyError): pass if bypassed: token = response['generated_pass_UUID'] resp = helpers.soupify(session.get(url)) bypass_url = f'https://{host}{resp.form.get("action")}' data = dict((x.get('name'), x.get('value')) for x in resp.select('form > input')) data.update({'id': resp.strong.text, 'g-recaptcha-response': token, 'h-captcha-response': token}) resp = session.post(bypass_url, data = data) if resp.status_code == 200: pickle.dump(resp.cookies, open(f'{tempfile.gettempdir()}/{host}', 'wb')) logger.info("Succesfully bypassed captcha!") return resp else: bypassed = False
def addtime(self): if self.sec >= 60: self.sec = 0 self.min += 1 if self.min >= 60: self.min = 0 self.hour += 1 def colorchange(self): if colorchange == True: self.color = (112, 128, 144) c = time() alarm = record() while True: for event in pygame.event.get(): if event.type == QUIT: exit() pressed_keys = pygame.key.get_pressed() screen.fill([176, 196, 222]) text = font1.render(str(c.day), True, (255, 255, 255)) display = font1.render(("am"), True, (255, 255, 255)) displayy = font1.render(("pm"), True, (255, 255, 255)) c.addtime() c.sec += 1 # add up one second each loop seconddisplay(c.sec, n) sx, sy, mx, my, hx, hy = getpos(c.sec, c.min, c.hour)
def update(self): screen.delete("highlight") screen.delete("tile") for x in range(8): for y in range(8): #Could replace the circles with images later, if I want if self.oldarray[x][y] == "w": screen.create_oval(54 + 50 * x, 54 + 50 * y, 96 + 50 * x, 96 + 50 * y, tags="tile {0}-{1}".format(x, y), fill="#aaa", outline="#aaa") screen.create_oval(54 + 50 * x, 52 + 50 * y, 96 + 50 * x, 94 + 50 * y, tags="tile {0}-{1}".format(x, y), fill="#fff", outline="#fff") elif self.oldarray[x][y] == "b": screen.create_oval(54 + 50 * x, 54 + 50 * y, 96 + 50 * x, 96 + 50 * y, tags="tile {0}-{1}".format(x, y), fill="#000", outline="#000") screen.create_oval(54 + 50 * x, 52 + 50 * y, 96 + 50 * x, 94 + 50 * y, tags="tile {0}-{1}".format(x, y), fill="#111", outline="#111") #Animation of new tiles screen.update() for x in range(8): for y in range(8): #Could replace the circles with images later, if I want if self.array[x][y] != self.oldarray[x][y] and self.array[x][ y] == "w": screen.delete("{0}-{1}".format(x, y)) #42 is width of tile so 21 is half of that #Shrinking for i in range(21): screen.create_oval(54 + i + 50 * x, 54 + i + 50 * y, 96 - i + 50 * x, 96 - i + 50 * y, tags="tile animated", fill="#000", outline="#000") screen.create_oval(54 + i + 50 * x, 52 + i + 50 * y, 96 - i + 50 * x, 94 - i + 50 * y, tags="tile animated", fill="#111", outline="#111") if i % 3 == 0: sleep(0.01) screen.update() screen.delete("animated") #Growing for i in reversed(range(21)): screen.create_oval(54 + i + 50 * x, 54 + i + 50 * y, 96 - i + 50 * x, 96 - i + 50 * y, tags="tile animated", fill="#aaa", outline="#aaa") screen.create_oval(54 + i + 50 * x, 52 + i + 50 * y, 96 - i + 50 * x, 94 - i + 50 * y, tags="tile animated", fill="#fff", outline="#fff") if i % 3 == 0: sleep(0.01) screen.update() screen.delete("animated") screen.create_oval(54 + 50 * x, 54 + 50 * y, 96 + 50 * x, 96 + 50 * y, tags="tile", fill="#aaa", outline="#aaa") screen.create_oval(54 + 50 * x, 52 + 50 * y, 96 + 50 * x, 94 + 50 * y, tags="tile", fill="#fff", outline="#fff") screen.update() elif self.array[x][y] != self.oldarray[x][y] and self.array[x][ y] == "b": screen.delete("{0}-{1}".format(x, y)) #42 is width of tile so 21 is half of that #Shrinking for i in range(21): screen.create_oval(54 + i + 50 * x, 54 + i + 50 * y, 96 - i + 50 * x, 96 - i + 50 * y, tags="tile animated", fill="#aaa", outline="#aaa") screen.create_oval(54 + i + 50 * x, 52 + i + 50 * y, 96 - i + 50 * x, 94 - i + 50 * y, tags="tile animated", fill="#fff", outline="#fff") if i % 3 == 0: sleep(0.01) screen.update() screen.delete("animated") #Growing for i in reversed(range(21)): screen.create_oval(54 + i + 50 * x, 54 + i + 50 * y, 96 - i + 50 * x, 96 - i + 50 * y, tags="tile animated", fill="#000", outline="#000") screen.create_oval(54 + i + 50 * x, 52 + i + 50 * y, 96 - i + 50 * x, 94 - i + 50 * y, tags="tile animated", fill="#111", outline="#111") if i % 3 == 0: sleep(0.01) screen.update() screen.delete("animated") screen.create_oval(54 + 50 * x, 54 + 50 * y, 96 + 50 * x, 96 + 50 * y, tags="tile", fill="#000", outline="#000") screen.create_oval(54 + 50 * x, 52 + 50 * y, 96 + 50 * x, 94 + 50 * y, tags="tile", fill="#111", outline="#111") screen.update() #Drawing of highlight circles for x in range(8): for y in range(8): if self.player == 0: if valid(self.array, self.player, x, y): screen.create_oval(68 + 50 * x, 68 + 50 * y, 32 + 50 * (x + 1), 32 + 50 * (y + 1), tags="highlight", fill="#008000", outline="#008000") if not self.won: #Draw the scoreboard and update the screen self.drawScoreBoard() screen.update() #If the computer is AI, make a move if self.player == 1: startTime = time() self.oldarray = self.array alphaBetaResult = self.alphaBeta(self.array, depth, -float("inf"), float("inf"), 1) self.array = alphaBetaResult[1] if len(alphaBetaResult) == 3: position = alphaBetaResult[2] self.oldarray[position[0]][position[1]] = "b" self.player = 1 - self.player deltaTime = round((time() - startTime) * 100) / 100 if deltaTime < 2: sleep(2 - deltaTime) nodes = 0 #Player must pass? self.passTest() else: screen.create_text(250, 550, anchor="c", font=("Consolas", 15), text="The game is done!")
""" A Python Singleton mixin class that makes use of some of the ideas found at http://c2.com/cgi/wiki?PythonSingleton. Just inherit from it and you have a singleton. No code is required in subclasses to create singleton behavior -- inheritance from Singleton is all that is needed. Singleton creation is threadsafe. USAGE: Just inherit from Singleton. If you need a constructor, include an __init__() method in your class as you usually would. However, if your class is S, you instantiate the singleton using S.getInstance() instead of S(). Repeated calls to S.getInstance() return the originally-created instance. For example: class S(Singleton): def __init__(self, a, b=1): pass S1 = S.getInstance(1, b=3) Most of the time, that's all you need to know. However, there are some other useful behaviors. Read on for a full description:
import uuid import numpy as np import redis import random from random import uniform from time import * from phonenumbers.phonenumberutil import region_code_for_country_code # f1=faker.Factory rds = redis.Redis(host='localhost', port=6379, decode_responses=True, db=0) # host是redis主机,需要redis服务端和客户端都启动 redis默认端口是6379 rds1 = redis.Redis(host='localhost', port=6379, decode_responses=True, db=1) # host是redis主机,需要redis服务端和客户端都启动 redis默认端口是6379 cur_t = time() class data_generator: """ A complete CRD """ def __init__(self, ID=None, callednumber=None, teltime=None, teltype=None, charge=None, result=None, type=None, pick_type_distribution="default",
import validator from camelcase import CamelCase import datetime from datetime import date import time from time import time # Today's Date import datetime today = datetime.date.today() print(today) # Timestamp today = date.today() timestamp = time() print(timestamp) # Pip module # Import custom module c = CamelCase() print(c.hump('hello there world')) email = 'test#test.com' if validate_email(email): print('Email is valid') else: print('Invalid Email')
def test_queue(cls): que = cls() que.push(123) assert que.pop() == 123 que.push(456) assert que.pop(0.0) == 456 que.push(789) assert que.pop(0.1) == 789 assert que.pop(0.0) is None assert que.pop(0.1) is None que.push(123) que.push(456) assert que.pop(0.5) == 123 assert que.pop() == 456 before = time() assert que.pop(0.1) is None after = time() assert after - before >= 0.1 before = time() assert que.pop(0.0) is None after = time() assert after - before < 0.01 ############################### go = Event() def pop(q, t = None): go.wait() current_thread().result = q.pop(t) ############################### th1 = Thread(target = pop, args = (que, )) th1.start() th2 = Thread(target = pop, args = (que, )) th2.start() go.set() que.push(1) sleep(0.5) assert (th1.is_alive() and not th2.is_alive() and th2.result == 1) or \ (th2.is_alive() and not th1.is_alive() and th1.result == 1) que.push(2) sleep(0.5) assert not th1.is_alive() and not th2.is_alive() assert (th1.result == 2 and th2.result == 1) or \ (th2.result == 2 and th1.result == 1) ############################### assert len(que) == 0 go.clear() th0 = Thread(target = pop, args = (que, 0.5)) th0.start() th1 = Thread(target = pop, args = (que, 1.5)) th1.start() th2 = Thread(target = pop, args = (que, 1.5)) th2.start() go.set() sleep(1.0) assert not th0.is_alive() and th0.result is None que.push("A") sleep(0.25) assert (th1.is_alive() and not th2.is_alive() and th2.result == "A") or \ (th2.is_alive() and not th1.is_alive() and th1.result == "A") sleep(0.5) assert not th1.is_alive() and not th2.is_alive() assert (th1.result == "A" and th2.result is None) or \ (th2.result == "A" and th1.result is None) ############################### lock = Lock() pushed = [] popped = [] start = time() go = Event() def th_proc(n): go.wait() i = 0 while time() < start + 10.0: if random() > 0.5: i += 1 x = (n, i) que.push(x) with lock: pushed.append(x) if random() > 0.5: x = que.pop(random() / 10) if x is not None: with lock: popped.append(x) x = que.pop(random() / 10) while x is not None: with lock: popped.append(x) x = que.pop(random() / 10) ths = [ Thread(target = th_proc, args = (i, )) for i in range(10) ] for th in ths: th.start() go.set() for th in ths: th.join() assert sorted(pushed) == sorted(popped)
def processSegSoftMovie(input_movie, network, outputs): # Setup some non-modifiable values... seg_movie_append = '_seg' # Set up the output streams... stillReading = False reader = imageio.get_reader(input_movie) if outputs['seg_mov']: writer_seg = imageio.get_writer(input_movie[:-4] + seg_movie_append + '.avi', fps=reader.get_meta_data()['fps'], codec='mpeg4', quality=10) stillReading = True # Start processing the data im_iter = reader.iter_data() #返回iter对象 framenum = 0 while (stillReading): start_time = time() frames = [] framenum = framenum + 1 * network['batch_size'] if framenum % 1000 == 0: print("Frame: " + str(framenum)) # get a batch of data for i in range(network['batch_size']): try: frame = np.uint8(next(im_iter)) frames.append( np.resize( frame[:, :, 1], (network['input_size'], network['input_size'], 1))) except StopIteration: stillReading = False break except RuntimeError: stillReading = False break if framenum % 1000 == 0: print('Batch Assembled in: ' + str(time() - start_time)) start_time = time() # with the batch of data going through the network, we can get a batch of output from the following: if stillReading: result_seg = network['sess'].run(fetches=[network['seg']], feed_dict={ network['image_placeholder']: frames, network['is_training']: False })[0] if framenum % 1000 == 0: print('Batch Processed in: ' + str(time() - start_time)) start_time = time() # Sequentially save the data for i in range(network['batch_size']): # Save the outputs and save only if the outfile pattern was identified if outputs['seg_mov']: seg_output = result_seg[i, :, :] writer_seg.append_data( (255 * seg_output[:, :]).astype('uint8')) if framenum % 1000 == 0: print('Batch Saved in: ' + str(time() - start_time))
def go_mountain_burn(graph_get, path, min_cost, old_cost): end_cost = old_cost lenth = len(path) end_path = cp.copy(path) best_path = cp.copy(path) best_cost = old_cost T = old_cost * old_cost / 0.85 ti = time() while T > old_cost * 1: T *= 0.85 for z in range(4): for i in range(lenth): for j in range(int(len(end_path[i]) / 2)): new_path = swap_one_point(end_path, i, j) cost = cal_path_cost(new_path, min_cost, graph_get) der = cost - old_cost if cost < old_cost: end_cost = cost end_path = new_path best_cost = cost best_path = new_path flag = random() if der >= 0: if flag < math.exp(-der / T): end_cost = cost end_path = new_path for i in range(lenth): end = int(len(end_path[i]) / 2) j = 0 while j < end: end = int(len(end_path[i]) / 2) for q in range(j + 1, end): if (int(len(end_path[i]) / 2) - 1) < j * 2: break new_path = swap_two_point(end_path, i, j, i, q) if cal_cap(new_path[i], graph_get) and cal_cap( new_path[i], graph_get): cost = cal_path_cost(new_path, min_cost, graph_get) der = cost - old_cost if cost < old_cost: best_cost = cost best_path = new_path end_cost = cost end_path = new_path flag = random() if der >= 0: if flag < math.exp(-der / T): end_cost = cost end_path = new_path for o in range(i + 1, lenth): for q in range(int(len(end_path[o]) / 2)): new_path = swap_two_point(end_path, i, j, o, q) if cal_cap(new_path[i], graph_get) and cal_cap( new_path[o], graph_get): cost = cal_path_cost(new_path, min_cost, graph_get) der = cost - old_cost if cost < old_cost: best_cost = cost best_path = new_path end_cost = cost end_path = new_path flag = random() if der >= 0: if flag < math.exp(-der / T): end_cost = cost end_path = new_path j += 1 new_ti = time() if ti - new_ti > 20: break return best_cost, best_path
def processMovie(input_movie, network, outputs): # Setup some non-modifiable values... ellfit_movie_append = '_ellfit' affine_movie_append = '_affine' crop_movie_append = '_crop' ellfit_output_append = '_ellfit' ellfit_feature_outputs_append = '_features' # Set up the output streams... stillReading = False reader = imageio.get_reader(input_movie) if outputs['ell_mov']: writer_ellfit = imageio.get_writer(input_movie[:-4] + ellfit_movie_append + '.avi', fps=reader.get_meta_data()['fps'], codec='mpeg4', quality=10) stillReading = True if outputs['aff_mov']: writer_affine = imageio.get_writer(input_movie[:-4] + affine_movie_append + '.avi', fps=reader.get_meta_data()['fps'], codec='mpeg4', quality=10) stillReading = True if outputs['crop_mov']: writer_crop = imageio.get_writer(input_movie[:-4] + crop_movie_append + '.avi', fps=reader.get_meta_data()['fps'], codec='mpeg4', quality=10) stillReading = True if outputs['ell_file']: file_ellfit = open(input_movie[:-4] + ellfit_output_append + '.npz', 'wb') stillReading = True if outputs['ell_features']: file_features = open( input_movie[:-4] + ellfit_feature_outputs_append + '.npz', 'wb') stillReading = True # Start processing the data im_iter = reader.iter_data() framenum = 0 while (stillReading): start_time = time() frames = [] framenum = framenum + 1 * network['batch_size'] if framenum % 1000 == 0: print("Frame: " + str(framenum)) for i in range(network['batch_size']): try: #frame = cv2.cvtColor(np.uint8(next(im_iter)), cv2.COLOR_BGR2GRAY) frame = np.uint8(next(im_iter)) #frames.append(np.resize(frame, (network['input_size'], network['input_size'], 1))) frames.append( np.resize( frame[:, :, 1], (network['input_size'], network['input_size'], 1))) except StopIteration: stillReading = False break except RuntimeError: stillReading = False break if framenum % 1000 == 0: print('Batch Assembled in: ' + str(time() - start_time)) start_time = time() if stillReading: if outputs['ell_features']: result, result_unscaled, features = network['sess'].run( fetches=[ network['network_eval_batch'], network['ellfit'], network['final_features'] ], feed_dict={ network['image_placeholder']: frames, network['is_training']: False }) else: result, result_unscaled = network['sess'].run( fetches=[network['network_eval_batch'], network['ellfit']], feed_dict={ network['image_placeholder']: frames, network['is_training']: False }) if framenum % 1000 == 0: print('Batch Processed in: ' + str(time() - start_time)) start_time = time() # Sequentially save the data for i in range(network['batch_size']): # Save the outputs and save only if the outfile pattern was identified if outputs['ell_mov']: plot = cv2.cvtColor(frames[i], cv2.COLOR_GRAY2RGB) result_temp = plot_ellipse(plot, result[i], (255, 0, 0)) writer_ellfit.append_data(result_temp.astype('u1')) if outputs['aff_mov']: plot = cv2.cvtColor(frames[i], cv2.COLOR_GRAY2RGB) angle = np.arctan2(result_unscaled[i, 5], result_unscaled[i, 4]) * 180 / np.pi affine_mat = np.float32([[ 1, 0, -result_unscaled[i, 0] + outputs['affine_crop_dim'] ], [ 0, 1, -result_unscaled[i, 1] + outputs['affine_crop_dim'] ]]) plot = cv2.warpAffine(plot, affine_mat, (outputs['affine_crop_dim'] * 2, outputs['affine_crop_dim'] * 2)) affine_mat = cv2.getRotationMatrix2D( (outputs['affine_crop_dim'], outputs['affine_crop_dim']), angle, 1.) plot = cv2.warpAffine(plot, affine_mat, (outputs['affine_crop_dim'] * 2, outputs['affine_crop_dim'] * 2)) affine_mat = np.float32( [[1, 0, -outputs['affine_crop_dim'] / 2], [0, 1, -outputs['affine_crop_dim'] / 2]]) plot = cv2.warpAffine(plot, affine_mat, (outputs['affine_crop_dim'], outputs['affine_crop_dim'])) writer_affine.append_data(plot.astype('u1')) if outputs['crop_mov']: plot = cv2.cvtColor(frames[i], cv2.COLOR_GRAY2RGB) angle = 0 affine_mat = np.float32([[ 1, 0, -result_unscaled[i, 0] + outputs['affine_crop_dim'] ], [ 0, 1, -result_unscaled[i, 1] + outputs['affine_crop_dim'] ]]) plot = cv2.warpAffine(plot, affine_mat, (outputs['affine_crop_dim'] * 2, outputs['affine_crop_dim'] * 2)) affine_mat = cv2.getRotationMatrix2D( (outputs['affine_crop_dim'], outputs['affine_crop_dim']), angle, 1.) plot = cv2.warpAffine(plot, affine_mat, (outputs['affine_crop_dim'] * 2, outputs['affine_crop_dim'] * 2)) affine_mat = np.float32( [[1, 0, -outputs['affine_crop_dim'] / 2], [0, 1, -outputs['affine_crop_dim'] / 2]]) plot = cv2.warpAffine(plot, affine_mat, (outputs['affine_crop_dim'], outputs['affine_crop_dim'])) writer_crop.append_data(plot.astype('u1')) if outputs['ell_file']: np.save(file_ellfit, result_unscaled[i, :], allow_pickle=False) if outputs['ell_features']: np.save(file_features, features[i, :], allow_pickle=False) if framenum % 1000 == 0: print('Batch Saved in: ' + str(time() - start_time)) if outputs['ell_file']: file_ellfit.close() if outputs['ell_features']: file_features.close()
def game(): global playing, gameStop, gradeIntro, foodIntro, sleepIntro, speechIntro, foodPlayer, foodThreshold global sleepThreshold, speechThreshold foodEnd = False sleepEnd = False speechEnd = False challenge = False challengeThreshold = 0 timePassed = 0 startTime = 0 firstNum = randint(0, 9) secondNum = randint(0, 9) if firstNum >= 5: gradeEnd = True else: gradeEnd = False correct = False speech = False obstacle_y = 400 sleepIcon = { 'x': 0, 'y': 400, 'column': randint(1, 3), 'fallSpeed': 1.5, } playerColumn = randint(1, 3) img = randIcon() text = randUp() up = 1 textTime = 0 textPassed = 0 textStart = 0 heart_y = -80 firstgo = True timeEnd = False while playing: while not gameStop: #because steam is better if time() > 120: gameStop = True timeEnd = True canvas.blit(bgSleep, (0, 400)) if time() >= sleepThreshold or challenge: ## sleepGame if not sleepIntro and not challenge: introSleep() sleepIntro = True if sleepIcon['y'] > 900: sleepIcon['column'] = randint(1, 3) sleepIcon['y'] = 320 img = randIcon() if sleepIcon['column'] == playerColumn and sleepIcon['y'] >= 670 and sleepIcon['y'] < 800: sleepEnd = True gameStop = True sleepIcon['y'] += sleepIcon['fallSpeed'] sleepIcon['x'] = (sleepIcon['column'] - 1) * 133 + 27 canvas.blit(img, (sleepIcon['x'], sleepIcon['y'])) canvas.blit(playerPillow, ((playerColumn - 1) * 133 + 27, 720)) sleepIcon['fallSpeed'] = max(1.5, min(7.5, 1.5 + time()/10 - sleepThreshold)) if challenge: sleepIcon['fallSpeed'] = max(3, min(14, 3 + time()/8)) #### canvas.blit(bgGrades, (0,0)) canvas.blit(bgSpeech, (400, 400)) canvas.blit(bgFood, (400, 0)) ## gradeGame gradesTime = ceil(max(5 - (time()/15), 1.5)) if challenge: gradesTime = ceil(max(3 - time()/10, 0.75)) if timePassed > gradesTime: correct = False if gradeEnd: canvas.blit(gradeWrong, (80, 50)) canvas.blit(border1, (0, 0)) pygame.display.update() pygame.time.delay(2000) gameStop = True break else: startTime = time() firstNum = randint(0, 9) secondNum = randint(0, 9) if firstNum >= 5: gradeEnd = True timePassed = time() - startTime ## gradesDisplay if (firstNum == 0): canvas.blit(gradeNumbers[secondNum], (140, 80)) else: canvas.blit(gradeNumbers[firstNum], (80, 80)) canvas.blit(gradeNumbers[secondNum], (200, 80)) if correct: canvas.blit(gradeCorrect, (80, 50)) for event in pygame.event.get(): if (event.type == pygame.QUIT): playing = False gameStop = True ## KEYPRESSES if event.type == KEYDOWN: ## grades if event.key == K_r: if firstNum >= 5: gradeEnd = False correct = True else: gradeEnd = True timePassed = gradesTime + 1 ## food if event.key == K_SPACE: foodPlayer['direction'] *= -1 foodPlayer['speed'] = max(2, min(7, 2 + time()/12 - foodThreshold/5)) if challenge: foodPlayer['speed'] += time()/5 ## sleep if event.key == K_LEFT: playerColumn = max(1, playerColumn - 1) if event.key == K_RIGHT: playerColumn = min(3, playerColumn + 1) # to play all the games immediately with a higher difficulty if event.key == K_i: if challengeThreshold < 10: challengeThreshold += 1 else: challenge = True ## speech if event.key == K_UP: heart_y = 485 if up == 1: speechEnd = False else: speechEnd = True if event.key == K_DOWN: heart_y = 675 if up == 0: speechEnd = False else: speechEnd = True ## pause if event.key == K_p: pausedGame() # ####### if time() >= foodThreshold or challenge: ## foodGame if not foodIntro and not challenge: introFood() foodIntro = True canvas.blit(garbageLeft, (425, 150)) canvas.blit(garbageRight, (725, 150)) if foodPlayer['x'] <= 450 or foodPlayer['x'] >= 675: foodEnd = True gameStop = True canvas.blit(foodBurger, (foodPlayer['x'] + foodPlayer['speed'] * foodPlayer['direction'], foodPlayer['y'])) foodPlayer['x'] += foodPlayer['speed'] * foodPlayer['direction'] ## speechGame textTime = ceil(min(max(8 - time()/7 + speechThreshold/7, 3) + 8, 8)) if challenge: textTime = ceil(max(4 - time()/5, 1.5)) textPassed = time() - textStart if time() >= speechThreshold or challenge: if not speechIntro and not challenge: introSpeech() speechIntro = True speechEnd = True firstgo = True if textPassed > textTime: speech = False if speechEnd and not firstgo: gameStop = True break elif speechEnd: firstgo = False up = randint(0, 1) if not speechEnd: if up == 0: text = randDown() else: text = randUp() speechEnd = True heart_y = -50 textStart = time() textPassed = 0 heart_x = 750 canvas.blit(text, (400, 400)) canvas.blit(heart, (heart_x, heart_y)) ## borders if time() - floor(time()) < 0.16: canvas.blit(border1, (0, 0)) elif time() - floor(time()) < 0.32: canvas.blit(border2, (0, 0)) elif time() - floor(time()) < 0.48: canvas.blit(border1, (0, 0)) elif time() - floor(time()) < 0.64: canvas.blit(border2, (0, 0)) elif time() - floor(time()) < 0.8: canvas.blit(border1, (0, 0)) elif time() - floor(time()) < 1: canvas.blit(border2, (0, 0)) if not gradeIntro: introGrades() gradeIntro = True pygame.display.update() clock.tick(60) canvas.fill((255, 255, 255)) pygame.time.delay(3000) if timeEnd: canvas.blit(congrats, (257, 258)) elif foodEnd: canvas.blit(endFood, (257, 258)) elif sleepEnd: x = randint(1, 2) if x == 1: canvas.blit(endSleep1, (257, 258)) else: canvas.blit(endSleep2, (257, 258)) elif speechEnd: x = randint(1, 2) if x == 1: canvas.blit(endSocial1, (257, 258)) else: canvas.blit(endSocial2, (257, 258)) elif gradeEnd: x = randint(1, 2) if x == 1: canvas.blit(endGrade1, (257, 258)) else: canvas.blit(endGrade2, (257, 258)) pygame.display.update() pygame.time.delay(10000) pygame.quit() break
mysam.quiet() if options.verbose: mysam.verbose() if options.debug: mysam.debug() if ',' in options.server: if not mysam.selectServer(options.server): print '''<ERROR: SAM servers not reachable/>\n''' quit() ############################################################################## now = int(round(time() / 60)) plist_init = {} plist_init["now"] = str(60000 * now) plist_init["before15min"] = str(60000 * (now - 15)) plist_init["before60min"] = str(60000 * (now - 60)) plist_init["before2h"] = str(60000 * (now - 120)) plist_init["before4h"] = str(60000 * (now - 240)) plist_init["before6h"] = str(60000 * (now - 360)) plist_init["before12h"] = str(60000 * (now - 720)) plist_init["before24h"] = str(60000 * (now - 1440)) plist_init["date"] = datetime.today().strftime("%Y%m%d") plist_init["time"] = datetime.today().strftime("%H%M%S") results = [] try:
def processMovie_v3(input_movie, network, outputs): # Setup some non-modifiable values... ellfit_movie_append = '_xyplot' crop_movie_append = '_crop' # Set up the output streams... stillReading = False reader = imageio.get_reader(input_movie) if outputs['ell_mov']: writer_ellfit = imageio.get_writer(input_movie[:-4] + ellfit_movie_append + '.avi', fps=reader.get_meta_data()['fps'], codec='mpeg4', quality=10) stillReading = True if outputs['crop_mov']: writer_crop = imageio.get_writer(input_movie[:-4] + crop_movie_append + '.avi', fps=reader.get_meta_data()['fps'], codec='mpeg4', quality=10) stillReading = True # Start processing the data im_iter = reader.iter_data() framenum = 0 while (stillReading): start_time = time() frames = [] framenum = framenum + 1 * network['batch_size'] if framenum % 1000 == 0: print("Frame: " + str(framenum)) for i in range(network['batch_size']): try: #frame = cv2.cvtColor(np.uint8(next(im_iter)), cv2.COLOR_BGR2GRAY) frame = np.uint8(next(im_iter)) #frames.append(np.resize(frame, (network['input_size'], network['input_size'], 1))) frames.append(np.resize(frame[:, :, 1], (480, 480, 1))) except StopIteration: stillReading = False break except RuntimeError: stillReading = False break if framenum % 1000 == 0: print('Batch Assembled in: ' + str(time() - start_time)) start_time = time() if stillReading: xhot, yhot = network['sess'].run( fetches=[network['xhot_est'], network['yhot_est']], feed_dict={ network['image_placeholder']: frames, network['is_training']: False }) if framenum % 1000 == 0: print('Batch Processed in: ' + str(time() - start_time)) start_time = time() # Sequentially save the data for i in range(network['batch_size']): if outputs['ell_mov']: plot = cv2.cvtColor(frames[i], cv2.COLOR_GRAY2RGB) # Place crosshair on predicted location... cv2.line( plot, (np.float32( np.argmax(xhot, 1)[i] / network['bin_per_px'] - 2), np.float32( np.argmax(yhot, 1)[i] / network['bin_per_px'])), (np.float32( np.argmax(xhot, 1)[i] / network['bin_per_px'] + 2), np.float32( np.argmax(yhot, 1)[i] / network['bin_per_px'])), (255, 0, 0)) cv2.line( plot, (np.float32( np.argmax(xhot, 1)[i] / network['bin_per_px']), np.float32( np.argmax(yhot, 1)[i] / network['bin_per_px'] - 2)), (np.float32( np.argmax(xhot, 1)[i] / network['bin_per_px']), np.float32( np.argmax(yhot, 1)[i] / network['bin_per_px'] + 2)), (255, 0, 0)) writer_ellfit.append_data(plot.astype('u1')) if outputs['crop_mov']: plot = cv2.cvtColor(frames[i], cv2.COLOR_GRAY2RGB) angle = 0 affine_mat = np.float32( [[ 1, 0, -np.argmax(xhot, 1)[i] / network['bin_per_px'] + outputs['affine_crop_dim'] ], [ 0, 1, -np.argmax(yhot, 1)[i] / network['bin_per_px'] + outputs['affine_crop_dim'] ]]) plot = cv2.warpAffine(plot, affine_mat, (outputs['affine_crop_dim'] * 2, outputs['affine_crop_dim'] * 2)) affine_mat = cv2.getRotationMatrix2D( (outputs['affine_crop_dim'], outputs['affine_crop_dim']), angle, 1.) plot = cv2.warpAffine(plot, affine_mat, (outputs['affine_crop_dim'] * 2, outputs['affine_crop_dim'] * 2)) affine_mat = np.float32( [[1, 0, -outputs['affine_crop_dim'] / 2], [0, 1, -outputs['affine_crop_dim'] / 2]]) plot = cv2.warpAffine(plot, affine_mat, (outputs['affine_crop_dim'], outputs['affine_crop_dim'])) writer_crop.append_data(plot.astype('u1')) if framenum % 1000 == 0: print('Batch Saved in: ' + str(time() - start_time))
model = MobileNet(config_args) print("Model is built successfully\n\n") # Summarizer creation summarizer = Summarizer(sess, config_args.summary_dir) # Train class trainer = Train(sess, model, data, summarizer) if config_args.to_train: try: print("Training...") trainer.train() print("Training Finished\n\n") except KeyboardInterrupt: trainer.save_model() if config_args.to_test: print("Final test!") ans_list = trainer.test('val') # ans_list = trainer.test('train') # print(len(ans_list)) # print(ans_list) print("Testing Finished\n\n") if __name__ == '__main__': b = time() main() e = time() print("耗时:", e - b)
#!/usr/bin/env python3.4 import sys import time sys.path.append('..') sys.path.append('../common') sys.path.append('../../common') from casicsdb import * from utils import * casicsdb = CasicsDB() github_db = casicsdb.open('github') repos = github_db.repos # Add new fields for content_type # ............................................................................. start = time() msg(repos.update_many({}, {'$set': {'text_languages': []}})) print('time elapsed: {}'.format(time() - start))