def __init__(self): # create the main window self.gameSize = sf.Vector2(600, 600) self.window = sf.RenderWindow(sf.VideoMode(600, 600), "Pyng") self.playerPaddle = playerPaddle(self.gameSize) self.computerPaddle = computerPaddle(self.gameSize) self.ball = ball(self.gameSize) self.paddleSpeed = 400 self.computerScore = score(25, self.gameSize.y / 3) self.playerScore = score(25, 2 * self.gameSize.y / 3) try: # load a sprite to display texture = sf.Texture.from_file("Images/pyngLogo.png") self.sprite = sf.Sprite(texture) # load music to play self.music = sf.Music.from_file("Sounds/ambience_rain_outside.wav") self.music.loop = True except IOError: exit(1) # play the music self.music.play() self.clock = sf.Clock() self.clock.restart()
def main(): hmm = HMM() hmm.generate('./data/WSJ_02-21.pos') # hmm.generate('./data/xaa.pos') V = Viterbi() V.run(hmm.states, './data/WSJ_24.words', './data/WSJ_24_response.pos') score('./data/WSJ_24.pos', './data/WSJ_24_response.pos')
def main(): f1 = open("WSJ_02-21.pos","r") file2 = "WSJ_24.words" p,w=test(f1,file2) writeToFile(p,w) score("WSJ_24.pos","test.pos") os.remove("test.pos") f1 = open("WSJ.pos","r") file2 = "WSJ_23.words" p,w=test(f1,file2) writeToFile(p,w)
def simulateanneal(matrix, solution, printFlag=True, schedule=linearSchedule, maxTries=0): current = dict(solution) best = dict(solution) if maxTries == 0: maxTries = 1000 * len(best) if maxTries > 20000: maxTries = 20000 if maxTries < 5000: maxTries = 5000 cScore = score(matrix, solution, 0) bScore = score(matrix, solution, 0) jumps = 0 # make maxTries, T's schedule, and everything else really dependent on the NUMBER OF VERTICES. for t in range(maxTries): T = schedule(t, maxTries) neighbor = getNeighbor(current, T) # successors = getSuccessors(current, T) # neighbor = choice(successors) neighborScore = score(matrix, neighbor, (t * 4) / maxTries) if (printFlag): if T == 0: if (t % 100 == 0): print "Time:", t, "\tTemp:", T, "\tbScore:", bScore, "\tcScore:", cScore, "\tjumps:", jumps, "\tdeltaE:", ( neighborScore - cScore) else: if (t % 100 == 0): print "Time:", t, "\tTemp:", T, "\tbScore:", bScore, "\tcScore:", cScore, "\tjumps:", jumps, "\tdeltaE:", ( neighborScore - cScore), "\tchance:", math.exp( (neighborScore - cScore) / (0.005 * T / 5)) if neighborScore > cScore: current = dict(neighbor) cScore = neighborScore if neighborScore > bScore: best = dict(neighbor) bScore = neighborScore elif T == 0: continue elif random() < math.exp((neighborScore - cScore) / (0.0005 * T)): jumps += 1 current = dict(neighbor) cScore = neighborScore if bScore > cScore: return best, bScore else: return current, cScore
def climbhill(matrix, solution, printFlag=True, maxAttempts=2000): for tries in range(maxAttempts): neighbors = getSuccessorsHill(solution, 1) shuffle(neighbors) initialScore = currentScore = score(matrix, solution) if printFlag: if tries % 1 == 0: print "partial score: ", currentScore, " tries: ", tries for step in neighbors: nextScore = score(matrix, step) if nextScore > currentScore: solution = step currentScore = nextScore if initialScore == currentScore: break return solution, currentScore, tries
def get_scores_map(user_id): ''' Gets scores map for user with id user_id. The scores map contains all users besides user_id after the filtering step and the corresponding "candidate score" of each candidate after the weighting step. Arguments: user_id (int): id of user to construct candidate scores map. user_id corresponds to the index of the user in the responses matrix (from the parsed csv file) Returns: scores_map (int->float dict): map from user id to candidate score for each valid candidate for user with id user_id ''' # gets list of candidate user ids candidate_user_ids = filter(user_id) # initialize scores map: key is candidate id, value is candidate score (init to 0) scores_map = dict.fromkeys(candidate_user_ids, 0) # score each candidate for c_id in scores_map: candidate_score = score(user_id, c_id) scores_map[c_id] = candidate_score return get_normalized_map(scores_map)
def fillsemiglobalMatrix(seq1, seq2, matrix, seqType): #fill from up to bottom, left to right i = 1 while i <= len(seq1): j = 1 while j <= len(seq2): if i == len(seq1) or j == len(seq2): upGrid = (matrix[i][j - 1][0], i, j - 1) leftGrid = (matrix[i - 1][j][0], i - 1, j) else: upGrid = (matrix[i][j - 1][0] + gapScore(seqType), i, j - 1) leftGrid = (matrix[i - 1][j][0] + gapScore(seqType), i - 1, j) diagonalGrid = (matrix[i - 1][j - 1][0] + score(seq1[i - 1], seq2[j - 1], seqType), i - 1, j - 1) matrix[i][j] = upGrid if leftGrid[0] > matrix[i][j][0]: matrix[i][j] = leftGrid if diagonalGrid[0] > matrix[i][j][0]: matrix[i][j] = diagonalGrid j += 1 i += 1 return matrix
def search(target, worklist, callbacks): global ninput callback_start_scoring = callbacks[6] callback_scored = callbacks[7] callback_start_check = callbacks[8] callback_checked = callbacks[9] while worklist: input = worklist.pop() #print '[+] input %s' % input.filename child_inputs = expand_execution(input, callbacks) if not callback_start_check: print '[+] checking each new input' else: callback_start_check(len(child_inputs)) for input in child_inputs: if not callback_checked: os.write(sys.stdout.fileno(), ' %s' % input.filename.split('/')[-1]) fault = check(PARAM['PROGNAME'], PARAM['PROGARG'], input.filename, PARAM['FAULT_CHECKER'], PARAM['TAINT_STDIN']) if not callback_checked: os.write(sys.stdout.fileno(), '\b' * (len(input.filename.split('/')[-1]) + 4)) if fault: print '[+] ' + ('@' * 75) print ' Fault detected on file %s' % input.filename.split('/')[-1] print ' ' + ('@' * 75) else: callback_checked(input.number, fault) if fault: filecopy = os.path.join(PARAM['CRASH_FOLDER'], os.path.basename(input.filename)) shutil.copy(input.filename, filecopy) if not callback_start_scoring: print '[+] scoring each new input' else: callback_start_scoring(len(child_inputs)) for input in child_inputs: if not callback_scored: os.write(sys.stdout.fileno(), ' %s' % input.filename.split('/')[-1]) input.note = score(PARAM['PROGNAME'], PARAM['PROGARG'], input.filename, PARAM['TAINT_STDIN']) #input.note = random_score() if not callback_scored: os.write(sys.stdout.fileno(), '\b' * (len(input.filename.split('/')[-1]) + 4)) else: callback_scored(input) worklist += child_inputs worklist.sort(key=lambda x: x.note) # this is couner-intuitive, but a lot of blocks are executed on # completely wrong images if PARAM['PROGNAME'] == '/usr/bin/convert': worklist.reverse() session.save(target, PARAM, ninput, worklist)
def main(): stdscr = initscr() use_default_colors() start_color() noecho() curs_set(False) keypad(stdscr, True) fiy, fix = getmaxyx(stdscr) board_x = int(fix / 2) - 5 board_y = int(fiy / 2) - 5 board = newwin(10, 10, board_y, board_x) box(board) panel_game = new_panel(board) scoreboard = score('Score: 0', board_x, board_y) oplayer = Player(stdscr, '🛦', board_x, board_y, COLOR_YELLOW, COLOR_BLACK) top_panel(oplayer.panel) ialien = [] insert_alien(ialien, board_x, board_y) fis_mis_list = [] sec_mis_list = [] fis_mis_timer = [] while True: key = getch() if key == 113: break if key == 32: fis_mis_list.append(first_missile('i', oplayer.x, oplayer.y, COLOR_BLUE, COLOR_BLACK)) temp = threading.Timer(1, move_missile, [fis_mis_list, 1, len(fis_mis_list) - 1, ialien, scoreboard]) temp.daemon = True temp.start() elif key == 115: sec_mis_list.append(second_missile('l', oplayer.x, oplayer.y, COLOR_CYAN, COLOR_BLACK)) temp = threading.Timer(1, move_missile, [sec_mis_list, 2, len(sec_mis_list)-1, ialien, scoreboard]) temp.daemon = True temp.start() else: oplayer.move(key) update_panels() doupdate() endwin() return 0
def simulateanneal(matrix, solution, printFlag=True, schedule=linearSchedule, maxTries=0): current = dict(solution) best = dict(solution) if maxTries == 0: maxTries = 1000 * len(best) if maxTries > 20000: maxTries = 20000 if maxTries < 5000: maxTries = 5000 cScore = score(matrix, solution, 0) bScore = score(matrix, solution, 0) jumps = 0 # make maxTries, T's schedule, and everything else really dependent on the NUMBER OF VERTICES. for t in range(maxTries): T = schedule(t, maxTries) neighbor = getNeighbor(current, T) # successors = getSuccessors(current, T) # neighbor = choice(successors) neighborScore = score(matrix, neighbor, (t *4) / maxTries) if (printFlag): if T == 0: if (t % 100 == 0): print "Time:", t, "\tTemp:", T, "\tbScore:", bScore, "\tcScore:", cScore, "\tjumps:", jumps, "\tdeltaE:", (neighborScore - cScore) else: if (t % 100 == 0): print "Time:", t, "\tTemp:", T, "\tbScore:", bScore, "\tcScore:", cScore, "\tjumps:", jumps, "\tdeltaE:", (neighborScore - cScore), "\tchance:", math.exp((neighborScore - cScore) / (0.005*T/5)) if neighborScore > cScore: current = dict(neighbor) cScore = neighborScore if neighborScore > bScore: best = dict(neighbor) bScore = neighborScore elif T == 0: continue elif random() < math.exp((neighborScore - cScore) / (0.0005*T)): jumps += 1 current = dict(neighbor) cScore = neighborScore if bScore > cScore: return best, bScore else: return current, cScore
def reversi(): table = create_table() players = ['W', 'B'] current_player = 'W' count = 0 while True: # Switch player for player in players: if current_player != player: current_player = player break # Show valide valid move valid_moves = valid_choice_optimize(table, current_player) encoded_valid_moves = encode_valid_moves(valid_moves) # Check if have any available move or not if valid_moves == []: print('Player %s cannot play.' % current_player) count += 1 if count == 2: score(table) break continue print('Valid choices:', encoded_valid_moves) try: player_move = input('Player %s: ' % current_player) except EOFError: pass # User input's availability x = check_move(valid_moves, player_move) if x is False: print('Valid choices:', encoded_valid_moves) break _, encoded_player_move = check_move(valid_moves, player_move) # table = update_table_optimize(table, choice(valid_moves), current_player) table = update_table_optimize(table, encoded_player_move, current_player) # print(table) # Show result print_table(table) # Running mode count = 0 # If a player can move, reset the count
def k_fold(table, N, algo, columns, class_column, shuffle=False, score=score.f1_score): if shuffle: table.shuffle() step = int(ceil(float(len(table)) / N)) result = 0.0 for i in xrange(N): learning = table[:] test = learning[i * step:(i + 1) * step] del learning[i * step:(i + 1) * step] algo.learn(learning, columns, class_column) test_results = algo.test(test) result += score(*test_results) return result / N
def getscore(image): white = Image.fromarray(np.full_like(image,0)) image = Image.fromarray(image) mean, std = score(image, args.model) mean = round(mean, 2) std = round(std,2) #draw = ImageDraw.Draw(white) draw = ImageDraw.Draw(image) font = ImageFont.truetype(args.font, args.font_size) if args.mode == 'mean': word = "{}".format(mean) elif args.mode == 'std': word = "{}".format(std) text_w, text_h = draw.textsize(word, font) img_w, img_h = image.size draw.text((int((img_w-text_w)/2),int(img_h-text_h)/2),word, font=font,fill=(0, 0, 255)) #white = np.array(white) image = np.array(image) return image
def main(): image = Image.open(args.image_file) mean, std = score(image, args.model) mean = round(mean, 2) std = round(std, 2) print('Load: {} | score: {} % {}'.format(args.image_file, mean, std)) draw = ImageDraw.Draw(image) font = ImageFont.truetype(args.font, args.font_size) word = "{}%{}".format(mean, std) text_w, text_h = draw.textsize(word, font) img_w, img_h = image.size draw.text((int((img_w - text_w) / 2), img_h - text_h), word, font=font, fill=(0, 0, 255)) plt.imshow(image) plt.xticks([]) plt.yticks([]) plt.show()
def summary_code(message): summary_options = st.selectbox("Choose Summarizer", ['Text Rank', 'Luhn', 'LSA', 'Lex Rank']) if summary_options == 'Lex Rank': search1 = st.number_input('Input desired no. of minutes for article read:', key=1,min_value=3) if search1: st.text("Using Lex Rank Summarizer ..") summary_result = lex_rank(message, search1) st.success(summary_result) if st.button('Quality Check'): score(message) st.write('(FOR NERDS ONLY-Use above Quality Check option to evaluate summary)') elif summary_options == 'Luhn': search2 = st.number_input('Input desired no. of minutes for article read:', key=2,min_value=3) if search2: st.text("Using Luhn Summarizer ..") summary_result = luhn(message, search2) st.success(summary_result) if st.button('Quality Check'): score(message) st.write('(FOR NERDS ONLY-Use above Quality Check option to evaluate summary)') elif summary_options == 'LSA': search3 = st.number_input('Input desired no. of minutes for article read:', key=3,min_value=3) if search3: st.text("Using LSA Summarizer ..") summary_result = lsa(message, search3) st.success(summary_result) if st.button('Quality Check'): score(message) st.write('(FOR NERDS ONLY-Use above Quality Check option to evaluate summary)') else: search4 = st.number_input('Input desired no. of minutes for article read:', key=4,min_value=3) if search4: st.text("Using Text Rank Summarizer ..") summary_result = text_rank(message, search4) st.success(summary_result) if st.button('Quality Check'): score(message) st.write('(FOR NERDS ONLY-Use above Quality Check option to evaluate summary)')
def run(self): super(Score,self).pre_run() self.wer, self.code, self.log = score(self.model, self.tag, subset='swbd') super(Score,self).post_run() return str(self)
def test_typical(self): rolls = convert("X7/9-X-88/-6XXX81") total_score = score(rolls, 0, 1) self.assertEqual(total_score, 167)
""" already run in ~/project/scikit-learn-master/ as python -m sklearn.ensemble.boost.py have to set dd , col-headers in data file """ # load "data --workaround for module path issues" w = '/home/solver/project/scikit-learn-master/weight.pkl' e = '/home/solver/project/scikit-learn-master/entropybasic.pkl' i = '/home/solver/project/scikit-learn-master/incorrect.pkl' d = '/home/solver/project/scikit-learn-master/datahdrlbl.pkl' ab = score(w,e,i,d) #score.py object pd = pndsa(w,e,i,d) #pandadata.py object inherits from score-class # bin "bayesian_blocks is expensive so check" if os.path.isfile('bayesblock.pkl') : bayesfile=open('bayesblock.pkl','rb') bypkl = pickle.load(bayesfile) bynp = np.asarray( bypkl, dtype=np.float64 ) ab.bins = bynp bayesfile.close() else : ww = ab.wts.flat intervals = bayesian_blocks(ww) #array of optimal bin_edges
def search(target, worklist, callbacks): global ninput global paths global elapsed global querytime global start global pathssub global totalcon global cva_constraints global cva_paths callback_start_scoring = callbacks[6] callback_scored = callbacks[7] callback_start_check = callbacks[8] callback_checked = callbacks[9] accumlist = list(worklist) current = ninput #session.save(target, PARAM, ninput, worklist) #start = time.time() while worklist: #start = time.time() input = worklist.pop() #print '[+] input %s' % input.filename child_inputs = expand_execution(input, callbacks) if USE_ACCUM: continue if PARAM['PATH_BOUND'] > 0 and paths >= PARAM['PATH_BOUND']: if not USE_ACCUM: accumlist += child_inputs break if not callback_start_check: print '[+] checking each new input' else: callback_start_check(len(child_inputs)) for input in child_inputs: if not callback_checked: os.write(sys.stdout.fileno(), ' %s' % input.filename.split('/')[-1]) fault = check(PARAM['PROGNAME'], PARAM['PROGARG'], input.filename, PARAM['FAULT_CHECKER'], PARAM['TAINT_STDIN']) if not callback_checked: os.write(sys.stdout.fileno(), '\b' * (len(input.filename.split('/')[-1]) + 4)) if fault: print '[+] ' + ('@' * 75) print ' Fault detected on file %s' % input.filename.split( '/')[-1] print ' ' + ('@' * 75) else: callback_checked(input.number, fault) if fault: filecopy = os.path.join(PARAM['CRASH_FOLDER'], os.path.basename(input.filename)) shutil.copy(input.filename, filecopy) #elapsed = elapsed + (time.time() - start) if not callback_start_scoring: print '[+] scoring each new input' else: callback_start_scoring(len(child_inputs)) for input in child_inputs: if not callback_scored: os.write(sys.stdout.fileno(), ' %s' % input.filename.split('/')[-1]) input.note = score(PARAM['PROGNAME'], PARAM['PROGARG'], input.filename, PARAM['TAINT_STDIN']) #input.note = random_score() if not callback_scored: os.write(sys.stdout.fileno(), '\b' * (len(input.filename.split('/')[-1]) + 4)) else: callback_scored(input) worklist += child_inputs accumlist += child_inputs worklist.sort(key=lambda x: x.note) #worklist.sort(key=lambda x: x.note, reverse = True) #worklist.reverse() # this is counter-intuitive, but a lot of blocks are executed on # completely wrong images if PARAM['PROGNAME'] == '/usr/bin/convert': worklist.reverse() #session.save(target, PARAM, ninput, worklist) elapsed = (time.time() - start) session.save(target, PARAM, ninput, accumlist) print 'Paths Explored: %s Feasible Paths: %s Total Constraints: %s Actual Constraints: %s Time Taken: %s Valgrind Time: %s'\ % (paths,ninput - current,totalcon,pathssub,round(elapsed,2),round(querytime,2))
def test_one(self): rolls = convert("X--X--X--X--X--") total_score = score(rolls, 0, 1) self.assertEqual(total_score, 50)
def test_strikes(self): rolls = convert("XXXXXXXXXXXX") total_score = score(rolls, 0, 1) self.assertEqual(total_score, 300)
#time_to_end = score.something2 # Import activities into the activities list activities = [] itinerary = [] # ---------- # Helper functions def transportation(): '''Adds a transportation activity.''' def update(current_location, time_): '''Updates current time & location after an activity is added.''' time_remaining -= act.time + t_act.time # Main loop while time_remaining > time_to_end: best_score = -100 # set to lowest score for activity in activities: current_score = score(act) activities[act] = current_score if current_score > best_score: best_score = current_score best_act = act t_act = transportation() itinerary.append(best_act) update(current_location, current_time)
activities = [] itinerary = [] # ---------- # Helper functions def transportation(): '''Adds a transportation activity.''' def update(current_location, time_): '''Updates current time & location after an activity is added.''' time_remaining -= act.time + t_act.time # Main loop while time_remaining > time_to_end: best_score = -100 # set to lowest score for activity in activities: current_score = score(act) activities[act] = current_score if current_score > best_score: best_score = current_score best_act = act t_act = transportation() itinerary.append(best_act) update(current_location, current_time)
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) #print(frame.shape) faces = faceCascade.detectMultiScale( gray, scaleFactor=1.1, minNeighbors=5, minSize=(30, 30), ) # Draw a rectangle around the faces for (x, y, w, h) in faces: cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 2) img = Image.fromarray(frame[y:y+h,x:x+w,:]) #cv2.imshow('image',frame[y:y+h,x:x+w,:]) #cv2.waitKey(0) #cv2.destroyAllWindows() mean, std = score(img, args.model) mean = round(mean, 2) std = round(std,2) #print('beauty score: {:.2f}%{:.2f}'.format(mean,std)) cv2.putText(frame,str(mean)+'%'+str(std),(x,y), cv2.FONT_HERSHEY_SIMPLEX, 0.5,(0, 255, 0)) # Display the resulting frame cv2.imshow('Video', frame) if cv2.waitKey(1) & 0xFF == ord('q'): break # When everything is done, release the capture video_capture.release() cv2.destroyAllWindows()
def test_spares(self): rolls = convert("5/5/5/5/5/5/5/5/5/5/5") total_score = score(rolls, 0, 1) self.assertEqual(total_score, 150)
def test_three(self): rolls = convert("--------------------") total_score = score(rolls, 0, 1) self.assertEqual(total_score, 0)
def search(target, worklist, callbacks): global ninput global paths global elapsed global querytime global start global pathssub global totalcon global cva_constraints global cva_paths global graphplot global x global y global pcstrings callback_start_scoring = callbacks[6] callback_scored = callbacks[7] callback_start_check = callbacks[8] callback_checked = callbacks[9] accumlist = list(worklist) current = ninput #session.save(target, PARAM, ninput, worklist) #start = time.time() while worklist: #start = time.time() input = worklist.pop() #print '[+] input %s' % input.filename child_inputs = expand_execution(input, callbacks) if USE_ACCUM: continue if PARAM['PATH_BOUND'] > 0 and paths >= PARAM['PATH_BOUND']: if not USE_ACCUM: accumlist += child_inputs break; if not callback_start_check: print '[+] checking each new input' else: callback_start_check(len(child_inputs)) for input in child_inputs: if not callback_checked: os.write(sys.stdout.fileno(), ' %s' % input.filename.split('/')[-1]) fault = check(PARAM['PROGNAME'], PARAM['PROGARG'], input.filename, PARAM['FAULT_CHECKER'], PARAM['TAINT_STDIN']) if not callback_checked: os.write(sys.stdout.fileno(), '\b' * (len(input.filename.split('/')[-1]) + 4)) if fault: print '[+] ' + ('@' * 75) print ' Fault detected on file %s' % input.filename.split('/')[-1] print ' ' + ('@' * 75) else: callback_checked(input.number, fault) if fault: filecopy = os.path.join(PARAM['CRASH_FOLDER'], os.path.basename(input.filename)) shutil.copy(input.filename, filecopy) #elapsed = elapsed + (time.time() - start) if not callback_start_scoring: print '[+] scoring each new input' else: callback_start_scoring(len(child_inputs)) for input in child_inputs: if not callback_scored: os.write(sys.stdout.fileno(), ' %s' % input.filename.split('/')[-1]) input.note = score(PARAM['PROGNAME'], PARAM['PROGARG'], input.filename, PARAM['TAINT_STDIN']) #input.note = random_score() if not callback_scored: os.write(sys.stdout.fileno(), '\b' * (len(input.filename.split('/')[-1]) + 4)) else: callback_scored(input) worklist += child_inputs accumlist += child_inputs worklist.sort(key=lambda x: x.note) #worklist.sort(key=lambda x: x.note, reverse = True) #worklist.reverse() # this is counter-intuitive, but a lot of blocks are executed on # completely wrong images if PARAM['PROGNAME'] == '/usr/bin/convert': worklist.reverse() #session.save(target, PARAM, ninput, worklist) elapsed = (time.time() - start) session.save(target, PARAM, ninput, accumlist) print 'Paths Explored: %s Feasible Paths: %s Total Constraints: %s Actual Constraints: %s Time Taken: %s Valgrind Time: %s'\ % (paths,ninput - current,totalcon,pathssub,round(elapsed,2),round(querytime,2)) sx = [] radii = [] sy = [] i = 0; j = 0; for s1 in pcstrings : j = 0; for s2 in pcstrings : sx.append(i) sy.append(j) #print 's1 : %s' %s1 #print 's2 : %s' %s2 #sim = difflib.SequenceMatcher(a=s1.lower(),b=s2.lower()).ratio() sim = Levenshtein.seqratio(s1,s2) radii.append(0.05+(sim/5)) #print 'sx : %d' %i #print 'sy : %d' %j #print 'sim : %s' %round(sim,2) j += 1 i += 1 output_server("Similarity Visualization") TOOLS="resize,crosshair,pan,wheel_zoom,box_zoom,reset,tap,previewsave,box_select,poly_select,lasso_select" p = figure(tools=TOOLS) p.scatter(sx,sy,radius=radii,color='red',fill_alpha=0.6,name='pathsscatter') p.title = "Path Similarity : "+target p.xaxis.axis_label = 'Number' p.yaxis.axis_label = 'Number' show(p)
def sc(score): smallfont = pygame.font.SysFont("comicsansms", 25) text = smallfont.render("Score: " + str(score), True, black) gameDisplay.blit(text, [400, 0]) score()
创建成绩表 create table score( sid int not null primary key auto_increment, student_id int not null, course_id int not null, score int not null, foreign key(student_id) references student(sid) on delete cascade on update cascade, foreign key(course_id) references course(cid) on delete cascade on update cascade ); 插入数据 insert into score(student_id,course_id,score) values (1,1,60), (1,2,59), (2,2,99); 创建班级任职表 create table teach2cls( tcid int not null primary key auto_increment, tid int not null, cid int not null, foreign key(tid) references teacher(tid) on delete cascade on update cascade, foreign key(cid) references class(cid) on delete cascade on update cascade
def test_four(self): rolls = convert("5-X-91/-/537/X8-XXX") total_score = score(rolls, 0, 1) self.assertEqual(total_score, 142)
def train(model, dataloader, criterion, optimizer, logger, device, similarity_weight=None, grad_clip_norm_value=50): logger.debug('Training Start') model.train() total_top1, total_top5, total_, top1_score, top5_score = 0, 0, 0, 0, 0 loss = [] if similarity_weight is not None: classification_loss = [] similarity_loss = [] for batch_index, batch in enumerate(dataloader): optimizer.zero_grad() if similarity_weight is not None: output, batch_similarity = model( batch[dataloader.dataset.INDEX_IMAGE].to(device)) else: output = model(batch[dataloader.dataset.INDEX_IMAGE].to(device)) target = batch[dataloader.dataset.INDEX_TARGET].to(device) # accuracy _, predicted_class = output.topk(5, 1, True, True) top1, top5, total = score(predicted_class, target) total_top1 += top1 total_top5 += top5 total_ += total # loss if similarity_weight is not None: batch_similarity_loss = calculate_similarity_loss(batch_similarity) batch_classification_loss = criterion(output, target) batch_loss = batch_classification_loss + (similarity_weight * batch_similarity_loss) else: batch_loss = criterion(output, target) loss.append(batch_loss.item()) # backprop batch_loss.backward() torch.nn.utils.clip_grad_norm_(model.parameters(), grad_clip_norm_value) optimizer.step() # use mean metrics mean_loss = np.mean(loss) if similarity_weight is not None: classification_loss.append(batch_classification_loss.item()) similarity_loss.append(batch_similarity_loss.item()) mean_classification_loss = np.mean(classification_loss) mean_similarity_loss = np.mean(similarity_loss) top1_score = score_value(total_top1, total_) top5_score = score_value(total_top5, total_) if (batch_index + 1) % 10 == 0: if similarity_weight is not None: logger.debug('Training Batch {}/{}: Top1 Accuracy {:.4f} Top5 Accuracy {:.4f}'.format( batch_index + 1, len(dataloader), top1_score, top5_score) \ + 'Loss {:.4f} Classification Loss {:.4f} Similarity Loss {:.4f} Similarity Weight {:.2f}'.format( mean_loss, mean_classification_loss, mean_similarity_loss, similarity_weight)) else: logger.debug( 'Training Batch {}/{}: Top1 Accuracy {:.4f} Top5 Accuracy {:.4f} Loss {:.4f}' .format(batch_index + 1, len(dataloader), top1_score, top5_score, mean_loss)) if DEBUG: break logger.debug('Training End') return top1_score, top5_score, mean_loss
def test_two(self): rolls = convert("-/-/-/-/-/-/-/-/-/-/-") total_score = score(rolls, 0, 1) self.assertEqual(total_score, 100)
def __init__(self): hero = player.Player(480, 710) #calling Player class enemy = enemyships.Enemyships(480, 710) #calling enemyships class #bullet = shooting.Shooting(hero.rect.left,hero.rect.top)#calling shooting class pygame.init() bg_size = width, height = 480, 710 # backgroundsize screen = pygame.display.set_mode(bg_size) # set screen pygame.display.set_caption("Space invaders") background = pygame.image.load("image/background.png") # bg picture use_image = pygame.image.load( "image/game_pause_pressed.png") # pause button resume_image = pygame.image.load( "image/game_resume_pressed.png") # play button gameover_image = pygame.image.load("image/game_over.png") # game_over gameover_rect = gameover_image.get_rect() button_inst = pygame.image.load( "image/instruction.png") # instruction button button_quit = pygame.image.load("image/quit.png") # quit button bg_inst = pygame.image.load("image/bg_inst.png") # bg for instructions enemy_explosion_images = [] hero_explosion_images = [] enemy_explosion_list = [ pygame.image.load("image/enemy1_down1.png"), pygame.image.load("image/enemy1_down2.png"), pygame.image.load("image/enemy1_down3.png"), pygame.image.load("image/enemy1_down4.png") ] hero.explosion_list = [ pygame.image.load("image/hero_blowup_n1.png"), pygame.image.load("image/hero_blowup_n2.png"), pygame.image.load("image/hero_blowup_n3.png"), pygame.image.load("image/hero_blowup_n4.png") ] # ==================Initialize================== pygame.mixer.init() background = pygame.image.load( "image/background.png") # Load background image # ==========Load music and sound==================== # pygame.mixer.music.load("sound/background.wav") # pygame.mixer.music.set_volume(0.3) # bullet_sound = pygame.mixer.Sound("sound/bullet.wav") # pygame.mixer.Sound.set_volume(bullet_sound, 0.3) # enemy_down_sound = pygame.mixer.Sound("sound/enemy_down.wav") # pygame.mixer.Sound.set_volume(enemy_down_sound, 0.3) # hero_down_sound = pygame.mixer.Sound("sound/hero_down.wav") # pygame.mixer.Sound.set_volume(hero_down_sound, 0.3) # button_sound = pygame.mixer.Sound("sound/button.wav") # pygame.mixer.Sound.set_volume(button_sound, 0.3) # ==========Load image and button==================== # pause_image = pygame.image.load("image/pause.png") # # gameover_image = pygame.image.load("image/game_over.png") # gameover_rect = gameover_image.get_rect() # button_inst = pygame.image.load("image/instruction.png") # button_quit = pygame.image.load("image/quit.png") # rsume_image = pygame.image.load("image/game_resume_pressed.png") # bg_inst = pygame.image.load("image/bg_inst.png") # score_image = pygame.image.load("image/score.png") # top_score_image = pygame.image.load("image/top_score.png") # ==========Main Loop start==================== gameexit = False clock = pygame.time.Clock() while not gameexit: # while loop of game events - with a for loop for keys for player while gameexit == True: #game over page gameDisplay.fill(white) message_display("Game Over") pygame.display.update() screen.blit(background, (0, 0)) # image of background screen.blit(hero.image, hero.rect) # hero image screen.blit(enemy.image, enemy.rect) # enemy ship image # if button("B", 20, 20, 30, 30, white, black, "back"): #on playing screen B is for going back to the menu # game_intro() # if button("P", 20, 55, 30, 30, white, black, "pause"):#makes the P a puase button on playing screen # pause() # if button("P", 20, 55, 30, 30, white, black, "pause"): pygame.display.update() # update for event in pygame.event.get(): # for loop for player if event.type == pygame.QUIT: # if event is quit quit() elif event.type == pygame.KEYDOWN: # if up key is pressed the playermoves up if event.key == pygame.K_UP: hero.move_up() elif event.key == pygame.K_DOWN: # if down key is pressed the player moves down hero.move_down() elif event.key == pygame.K_LEFT: # if left key is pressed the player moves left hero.move_left() elif event.key == pygame.K_RIGHT: # if right key is pressed player moves right hero.move_right() # elif event.key == pygame.K_p: # pause() elif event.key == pygame.K_SPACE: #press space bar fires hero.shoot() if hero.rect.top == enemy.rect.bottom: # collision of hero and enemy ships gameexit = True crash() #message display of losing # calling enemy ships class enemy.move() # hits = pygame.sprite.spritecollideany(player,enemyships,False)#colliding # if hits: # gameexit = True if hero.rect.top > 740 or hero.rect.bottom < -740 or hero.rect.left < -480 or hero.rect.right > 480: # boundaries to keep player within the screen- outside of while loop because we don't want to cycle through this we want it at all times gameexit = True score(bullet - 1) #calling score funtion above # all_sprites.draw(screen) pygame.display.update()
def validate_autoencoder(model, loader, logger, device, reconstruction_grid_filename, manifold_filename, beta, gamma, criterion, save_reconstruction, distribution): logger.debug('Validation Start') model.eval() total_top1, total_top5, total_, top1_score, top5_score = 0, 0, 0, 0, 0 loss = [] all_mu = [] all_class = [] for batch_index, batch in enumerate(loader): batch_input = batch[loader.dataset.INDEX_IMAGE].to(device) batch_classification_target = batch[loader.dataset.INDEX_TARGET].to( device) batch_reconstruction_target = batch[ loader.dataset.INDEX_TARGET_IMAGE].to(device) batch_class_prediction, batch_reconstruction, mu, logvar = model( batch_input) # accuracy _, predicted_class = batch_class_prediction.topk(5, 1, True, True) top1, top5, total = score(predicted_class, batch_classification_target) total_top1 += top1 total_top5 += top5 total_ += total # loss classification_loss = criterion(batch_class_prediction, batch_classification_target) reconstruction_loss = calculate_reconstruction_loss( batch_reconstruction_target, batch_reconstruction, distribution) total_kld, dim_wise_kld, mean_kld = calculate_kl_divergence(mu, logvar) effective_kl = beta * total_kld effective_classification_loss = gamma * classification_loss batch_loss = reconstruction_loss + effective_kl + effective_classification_loss logger.debug('Batch Loss: {}'.format(batch_loss.item()) \ + ' Reconstruction Loss: {:.4f}'.format(reconstruction_loss.item()) \ + ' Effective-KL {:.4f}'.format(effective_kl.item()) \ + ' Effective-CE {:.4f}'.format(effective_classification_loss.item()) \ + ' Cross-Entropy {:.4f}'.format(classification_loss.item()) \ + ' KL-Divergence: {:.4f}'.format(total_kld.item()) \ + ' Mean-KLD: {:.4f}'.format(mean_kld.item())) loss.append(batch_loss.item()) mean_loss = np.mean(loss) top1_score = score_value(total_top1, total_) top5_score = score_value(total_top5, total_) if save_reconstruction: if batch_index == 0: n = min(batch_reconstruction_target.size(0), 8) comparison = torch.cat([ batch_reconstruction_target[:n], batch_reconstruction.view( *batch_reconstruction_target.shape)[:n] ]) save_image(comparison.cpu(), reconstruction_grid_filename, nrow=n, normalize=False) all_mu.append(mu) all_class.append(batch_classification_target) if (batch_index + 1) % 10 == 0: logger.debug('Validation Batch {}/{}: Loss {:.4f}'.format(batch_index + 1, len(loader), mean_loss) \ + ' Top1 Accuracy {:.4f} Top5 Accuracy {:.4f}'.format(top1_score, top5_score)) if DEBUG: break if save_reconstruction: all_mu = torch.cat(all_mu, dim=0).detach().cpu().numpy() all_class = torch.cat(all_class, dim=0).detach().cpu().numpy() plot_manifold(all_mu, all_class, manifold_filename) logger.debug('Validation End') return top1_score, top5_score, mean_loss
def search(target, worklist, callbacks): global ninput global paths global elapsed global querytime global start global pathssub global totalcon global cva_constraints global cva_paths callback_start_scoring = callbacks[6] callback_scored = callbacks[7] callback_start_check = callbacks[8] callback_checked = callbacks[9] accumlist = list(worklist) current = ninput #session.save(target, PARAM, ninput, worklist) #start = time.time() while worklist: #start = time.time() input = worklist.pop() #print '[+] input %s' % input.filename child_inputs = expand_execution(input, callbacks) if USE_ACCUM: continue if PARAM['PATH_BOUND'] > 0 and paths >= PARAM['PATH_BOUND']: if not USE_ACCUM: accumlist += child_inputs break; if not callback_start_check: print '[+] checking each new input' else: callback_start_check(len(child_inputs)) for input in child_inputs: if not callback_checked: os.write(sys.stdout.fileno(), ' %s' % input.filename.split('/')[-1]) fault = check(PARAM['PROGNAME'], PARAM['PROGARG'], input.filename, PARAM['FAULT_CHECKER'], PARAM['TAINT_STDIN']) if not callback_checked: os.write(sys.stdout.fileno(), '\b' * (len(input.filename.split('/')[-1]) + 4)) if fault: print '[+] ' + ('@' * 75) print ' Fault detected on file %s' % input.filename.split('/')[-1] print ' ' + ('@' * 75) else: callback_checked(input.number, fault) if fault: filecopy = os.path.join(PARAM['CRASH_FOLDER'], os.path.basename(input.filename)) shutil.copy(input.filename, filecopy) #elapsed = elapsed + (time.time() - start) if not callback_start_scoring: print '[+] scoring each new input' else: callback_start_scoring(len(child_inputs)) for input in child_inputs: if not callback_scored: os.write(sys.stdout.fileno(), ' %s' % input.filename.split('/')[-1]) input.note = score(PARAM['PROGNAME'], PARAM['PROGARG'], input.filename, PARAM['TAINT_STDIN']) #input.note = random_score() if not callback_scored: os.write(sys.stdout.fileno(), '\b' * (len(input.filename.split('/')[-1]) + 4)) else: callback_scored(input) worklist += child_inputs accumlist += child_inputs worklist.sort(key=lambda x: x.note) #worklist.sort(key=lambda x: x.note, reverse = True) #worklist.reverse() # this is counter-intuitive, but a lot of blocks are executed on # completely wrong images if PARAM['PROGNAME'] == '/usr/bin/convert': worklist.reverse() #session.save(target, PARAM, ninput, worklist) elapsed = (time.time() - start) session.save(target, PARAM, ninput, accumlist) print 'Paths Explored: %s Feasible Paths: %s Total Constraints: %s Actual Constraints: %s Time Taken: %s Valgrind Time: %s'\ % (paths,ninput - current,totalcon,pathssub,round(elapsed,2),round(querytime,2))
def train_autoencoder(model, loader, optimizer, logger, device, beta, gamma, criterion, distribution, grad_clip_norm_value=50): logger.debug('Training Start') model.train() total_top1, total_top5, total_, top1_score, top5_score = 0, 0, 0, 0, 0 loss = [] for batch_index, batch in enumerate(loader): optimizer.zero_grad() batch_input = batch[loader.dataset.INDEX_IMAGE].to(device) batch_classification_target = batch[loader.dataset.INDEX_TARGET].to( device) batch_reconstruction_target = batch[ loader.dataset.INDEX_TARGET_IMAGE].to(device) batch_class_prediction, batch_reconstruction, mu, logvar = model( batch_input) # accuracy _, predicted_class = batch_class_prediction.topk(5, 1, True, True) top1, top5, total = score(predicted_class, batch_classification_target) total_top1 += top1 total_top5 += top5 total_ += total # loss classification_loss = criterion(batch_class_prediction, batch_classification_target) reconstruction_loss = calculate_reconstruction_loss( batch_reconstruction_target, batch_reconstruction, distribution) total_kld, dim_wise_kld, mean_kld = calculate_kl_divergence(mu, logvar) effective_kl = beta * total_kld effective_classification_loss = gamma * classification_loss batch_loss = reconstruction_loss + effective_kl + effective_classification_loss logger.debug('Batch Loss: {}'.format(batch_loss.item()) \ + ' Reconstruction Loss: {:.4f}'.format(reconstruction_loss.item()) \ + ' Effective-KL {:.4f}'.format(effective_kl.item()) \ + ' Effective-CE {:.4f}'.format(effective_classification_loss.item()) \ + ' Cross-Entropy {:.4f}'.format(classification_loss.item()) \ + ' KL-Divergence: {:.4f}'.format(total_kld.item()) \ + ' Mean-KLD: {:.4f}'.format(mean_kld.item())) loss.append(batch_loss.item()) # backprop batch_loss.backward() torch.nn.utils.clip_grad_norm_(model.parameters(), grad_clip_norm_value) optimizer.step() # use mean metrics mean_loss = np.mean(loss) top1_score = score_value(total_top1, total_) top5_score = score_value(total_top5, total_) if (batch_index + 1) % 10 == 0: logger.debug('Training Batch {}/{}: Loss {:.4f}'.format(batch_index + 1, len(loader), mean_loss) \ + ' Top1 Accuracy {:.4f} Top5 Accuracy {:.4f}'.format(top1_score, top5_score)) if DEBUG: break logger.debug('Training End') return top1_score, top5_score, mean_loss
shuffle=True) train_title = train['title'] test_title = test['title'] vectorizer = TfidfVectorizer(strip_accents='unicode', analyzer='word', ngram_range=(1, 3), norm='l2') vectorizer.fit(train_title) vectorizer.fit(test_title) x_train = vectorizer.transform(train_title) y_train = train.drop(labels=['title'], axis=1) x_test = vectorizer.transform(test_title) y_test = test.drop(labels=['title'], axis=1) from sklearn.naive_bayes import BernoulliNB NB_pipeline = Pipeline([ ('clf', OneVsRestClassifier( BernoulliNB(alpha=1.0, fit_prior=True, class_prior=None))), ]) for category in categories: print('... Processing {}'.format(category)) # train the model using X_dtm & y NB_pipeline.fit(x_train, train[category]) # compute the testing accuracy prediction = NB_pipeline.predict(x_test) print('Test accuracy is {}'.format(score(test[category], prediction)))
def validate(model, dataloader, criterion, logger, device, similarity_weight=None): logger.debug('Validation Start') model.eval() total_top1, total_top5, total_, top1_score, top5_score = 0, 0, 0, 0, 0 loss = [] if similarity_weight is not None: classification_loss = [] similarity_loss = [] for batch_index, batch in enumerate(dataloader): if similarity_weight is not None: output, batch_similarity = model( batch[dataloader.dataset.INDEX_IMAGE].to(device)) else: output = model(batch[dataloader.dataset.INDEX_IMAGE].to(device)) target = batch[dataloader.dataset.INDEX_TARGET].to(device) _, predicted_class = output.topk(5, 1, True, True) top1, top5, total = score(predicted_class, target) total_top1 += top1 total_top5 += top5 total_ += total # loss if similarity_weight is not None: batch_classification_loss = criterion(output, target) batch_similarity_loss = calculate_similarity_loss(batch_similarity) batch_loss = batch_classification_loss + (similarity_weight * batch_similarity_loss) else: batch_loss = criterion(output, target) loss.append(batch_loss.item()) mean_loss = np.mean(loss) if similarity_weight is not None: classification_loss.append(batch_classification_loss.item()) similarity_loss.append(batch_similarity_loss.item()) mean_classification_loss = np.mean(classification_loss) mean_similarity_loss = np.mean(similarity_loss) top1_score = score_value(total_top1, total_) top5_score = score_value(total_top5, total_) if (batch_index + 1) % 10 == 0: if similarity_weight is not None: logger.debug('Validation Batch {}/{}: Top1 Accuracy {:.4f} Top5 Accuracy {:.4f}'.format( batch_index + 1, len(dataloader), top1_score, top5_score) \ + ' Loss {:.4f} Classification Loss {:.4f} Similarity Loss {:.4f} Similarity Weight {:.2f}'.format( mean_loss, mean_classification_loss, mean_similarity_loss, similarity_weight)) else: logger.debug( 'Validation Batch {}/{}: Top1 Accuracy {:.4f} Top5 Accuracy {:.4f} Loss {:.4f}' .format(batch_index + 1, len(dataloader), top1_score, top5_score, mean_loss)) if DEBUG: break logger.debug('Validation End') return top1_score, top5_score, mean_loss
SEPERATEDAY =date(2015,6, 30) BEGINDAY = date(2015, 2, 1) path=os.path.abspath(os.path.dirname(os.path.dirname(__file__)))+'\\data' os.chdir(path) ## change dir to '~/files' weibo_train_file_path = "weibo_train_data.txt" weibo_train_sort_path="weibo_train_data_sort.txt" weibo_predict_file_path = "weibo_predict_data.txt" weibo_predict_sort_path = "weibo_predict_data_sort.txt" weibo_train_five_file_path = "weibo_train_five.txt" weibo_train_last_file_path = "weibo_train_last.txt" uid_features_str_file_path="uid_features_str.txt" weibo_result_file_path="weibo_result.txt" starttime = datetime.now() generate_sortedfile(weibo_train_file_path,weibo_train_sort_path) print("1.Train sort has been completed") generate_sortedfile(weibo_predict_file_path,weibo_predict_sort_path) print ("2.Predict sort has been completed") train_date_split(weibo_train_sort_path, SEPERATEDAY, BEGINDAY,weibo_train_five_file_path,weibo_train_last_file_path) print ("3.Data segmentation has been completed") uid_features(weibo_train_five_file_path,uid_features_str_file_path) print ("4.User feature extraction has been completed") weibo_predict(uid_features_str_file_path, weibo_train_last_file_path,weibo_result_file_path) print ("5.Forecast has been completed") score=score(weibo_result_file_path,weibo_train_last_file_path) print ("6.The whole score is "+score) endtime = datetime.now() print ("Total running time: %f s" % (endtime - starttime).seconds)
def search(target, worklist, callbacks): global ninput callback_start_scoring = callbacks[6] callback_scored = callbacks[7] callback_start_check = callbacks[8] callback_checked = callbacks[9] while worklist: input = worklist.pop() #print '[+] input %s' % input.filename child_inputs = expand_execution(input, callbacks) if not callback_start_check: print '[+] checking each new input' else: callback_start_check(len(child_inputs)) for input in child_inputs: if not callback_checked: os.write(sys.stdout.fileno(), ' %s' % input.filename.split('/')[-1]) fault = check(PARAM['PROGNAME'], PARAM['PROGARG'], input.filename, PARAM['FAULT_CHECKER'], PARAM['TAINT_STDIN']) if not callback_checked: os.write(sys.stdout.fileno(), '\b' * (len(input.filename.split('/')[-1]) + 4)) if fault: print '[+] ' + ('@' * 75) print ' Fault detected on file %s' % input.filename.split( '/')[-1] print ' ' + ('@' * 75) else: callback_checked(input.number, fault) if fault: filecopy = os.path.join(PARAM['CRASH_FOLDER'], os.path.basename(input.filename)) shutil.copy(input.filename, filecopy) if not callback_start_scoring: print '[+] scoring each new input' else: callback_start_scoring(len(child_inputs)) for input in child_inputs: if not callback_scored: os.write(sys.stdout.fileno(), ' %s' % input.filename.split('/')[-1]) input.note = score(PARAM['PROGNAME'], PARAM['PROGARG'], input.filename, PARAM['TAINT_STDIN']) #input.note = random_score() if not callback_scored: os.write(sys.stdout.fileno(), '\b' * (len(input.filename.split('/')[-1]) + 4)) else: callback_scored(input) worklist += child_inputs worklist.sort(key=lambda x: x.note) # this is couner-intuitive, but a lot of blocks are executed on # completely wrong images if PARAM['PROGNAME'] == '/usr/bin/convert': worklist.reverse() session.save(target, PARAM, ninput, worklist)