def aiMove(): data_path = "C:\\Users\\darth_000\\Desktop\\workspaces\\python\\GadLearner\\" board_InFileName = "boardIN.txt" board_OutFileName = "boardOUT.txt" fIN = open(data_path + board_InFileName,"r") fOUT = open(data_path + board_OutFileName,"w+") gad = mGAD.GAD() gad.ai.init(mai.ALGORITHMIC) gad.player, gad.currentBoard = loadBoard_FromCPP(fIN) #gad.player = mGAD.B print("Player color is: ", gad.player) print("Input board is:") mioutils.dumpBoard(gad.currentBoard) computerMoved, x, y, _, _ = gad.computerMove() if not computerMoved: x = -1 y = -1 print("Computer passed") else: print("Computer moved in x=", x, " y=", y) print("Output board is") mioutils.dumpBoard(gad.currentBoard) saveBoard_ToCPP(gad.currentBoard, x, y, fOUT) fIN.close fOUT.close
def _process_twtas(self, u_name, u_name_device): unit = self.spacecraft.twtas[u_name] processed_unit = {} shared_tl_off = True if 'off_cmd' in unit.lcamp.cmds_pids.keys(): off_cmd = unit.lcamp.cmds_pids['off_cmd'] if 'off_cmd' in unit.cmds_pids.keys(): if off_cmd.num != unit.cmds_pids['off_cmd'].num: shared_tl_off = False else: shared_tl_off = False if shared_tl_off: processed_unit['name'] = unit.cmds_pids['on_cmd'].des[:-3].replace( ' ', '_').replace(',', '') else: processed_unit['name'] = unit.name.replace(' ', '_') processed_unit['on_cmd'] = IOUtils.underscore_it( unit.cmds_pids['on_cmd'].num) processed_unit['off_cmd'] = IOUtils.underscore_it( unit.cmds_pids['off_cmd'].num) processed_unit['subbus_pid'] = IOUtils.underscore_it(unit.subbus.num) processed_unit['power_val'] = 0.0 if 'bus_curnt_tlm' in unit.cmds_pids.keys(): processed_unit['bus_curnt_tlm'] = IOUtils.underscore_it( unit.cmds_pids['bus_curnt_tlm'].num) return processed_unit
def set_program(self, event): """ Kick off spacecraft info reading and display script names in windows Description: Creates Reader and Writer objects, commencing reading of inputs. Then displays script names. """ # Clear output self.out_text.config(state=tkinter.NORMAL) self.out_text.delete(1.0,self.output_text_len+1.0) self.out_text.config(state=tkinter.DISABLED) # Clear listboxes self.a_htr_display.delete(0, len(self.ah_scripts)) self.cf_htr_display.delete(0, len(self.cfh_scripts)) self.twta_display.delete(0, len(self.twta_scripts)) # Create spacecraft self.spacecraft = Components.Spacecraft() self.spacecraft.name = self.program_combo.get() self.spacecraft.script_dir = self.programs[self.spacecraft.name][0] self.spacecraft.data_dir = self.programs[self.spacecraft.name][1] self.spacecraft.db_dir = self.programs[self.spacecraft.name][2] self.author = self.author_text.get() # Create I/O objects try: self.reader = IOUtils.Reader(gui=self, spacecraft=self.spacecraft) self.reader.execute() self.writer = IOUtils.Writer(gui=self, dbase=self.reader.dbase, spacecraft=self.spacecraft, author=self.author) self.output_text('') except Exception as error: self.output_text('Failed to read spacecraft info: %s' % error) out = traceback.format_exc() self.output_text(out) print(out)
def main(): known_ids, groundT = groundTruth() idsToWrite = getIdsToWrite() train_matrix, pred_matrix, weights = ensemble(known_ids, idsToWrite) final_prediction = regression(train_matrix, pred_matrix, groundT, weights) final_prediction = np.clip(final_prediction, 1, 5) IOUtils.writeFileEnsemble(final_prediction)
def _process_lcamps(self, u_name, u_name_device): unit = self.spacecraft.twtas[u_name] processed_unit = {} if 'on_cmd' and 'off_cmd' in unit.lcamp.cmds_pids.keys(): processed_unit['name'] = unit.lcamp.name.replace(' ', '_') processed_unit['on_cmd'] = IOUtils.underscore_it(unit.lcamp.cmds_pids['on_cmd'].num) processed_unit['off_cmd'] = IOUtils.underscore_it(unit.lcamp.cmds_pids['off_cmd'].num) processed_unit['subbus_pid'] = IOUtils.underscore_it(unit.subbus.num) processed_unit['power_val'] = 0.0 return processed_unit
def _process_heaters(self, u_name, u_name_device): unit = self.spacecraft.cf_heaters[u_name] processed_unit = {} processed_unit['name'] = u_name_device processed_unit['on_cmd'] = IOUtils.underscore_it(unit.cmds_pids['on_cmd'].num) processed_unit['off_cmd'] = IOUtils.underscore_it(unit.cmds_pids['off_cmd'].num) if 'cur_mon_tlm' in unit.cmds_pids.keys(): processed_unit['cur_mon_tlm'] = IOUtils.underscore_it(unit.cmds_pids['cur_mon_tlm'].num) processed_unit['subbus_pid'] = IOUtils.underscore_it(unit.subbus.num) processed_unit['power_val'] = float(unit.power_val) return processed_unit
def __init__(self, fontPath, dictPath, scales, degrees, templatePath=""): self.fontList = [ osj(fontPath, font) for font in IOUtils.GetFilesList(fontPath) ] self.scales = scales self.degrees = degrees self.charDict = IOUtils.LoadDict(dictPath) self.parms = self.GenParms() self.hasTemplate = False if len(templatePath) != 0: self.hasTemplate = True self.templates = IOUtils.LoadTemplate(templatePath)
def _process_misc(self, u_name, u_name_device, cmds, macros, tlm_misc): # Find on/off in database processed_unit = {} processed_unit['name'] = u_name_device.replace('Side_', '') if re.search('_\d', u_name[-2:]): u_name_t = u_name[:-2] elif re.search('mro.*side', u_name.lower()): u_name_t = u_name + ' dc' else: u_name_t = u_name cmd = self.dbase.get_cmd_pid(cmds, '%s on' % u_name_t) if len(cmd) == 0: cmd = self.dbase.get_cmd_pid(macros, '%s on' % u_name_t) if len(cmd) == 1: cmd = cmd[next(iter(cmd))].num else: self.gui.output_text(' Warning - ON cmd mixed up for %s' % u_name) return 0 processed_unit['on_cmd'] = IOUtils.underscore_it(cmd) cmd = self.dbase.get_cmd_pid(cmds, '%s off' % u_name_t) if len(cmd) == 0: cmd = self.dbase.get_cmd_pid(macros, '%s on' % u_name_t) if len(cmd) == 1: cmd = cmd[next(iter(cmd))].num else: self.gui.output_text(' Warning - OFF cmd mixed up for %s' % u_name) return 0 processed_unit['off_cmd'] = IOUtils.underscore_it(cmd) processed_unit['power_val'] = 0.0 processed_unit['subbus_pid'] = IOUtils.underscore_it( self.dbase.carved_subbuses[u_name].num) if u_name_t in self.dbase.mmdc_assoc.keys(): master_unit = self.dbase.mmdc_assoc[u_name_t] if 'MMDC' in master_unit: tlm_search = 'MMDC ' + master_unit[ 4:] + ' Side ' + u_name_device[-1] + ' Curnt Sense' elif 'CPSU' in master_unit: if u_name_device[-1] == '1': tlm_search = 'CPSU ' + master_unit[ 4:] + ' EPC P Output Shunt Curnt' else: tlm_search = 'CPSU ' + master_unit[ 4:] + ' EPC R Output Shunt Curnt' else: tlm_search = 'do not search, blerg' cmd = self.dbase.get_cmd_pid(tlm_misc, tlm_search) if len(cmd) == 1: cmd = cmd[next(iter(cmd))].num processed_unit['bus_curnt_tlm'] = IOUtils.underscore_it(cmd) return processed_unit
def _finish(won=False): IOUtils.clear() if won: print "You won!" answer = "" while (answer.lower() != "y") and (answer.lower() != "n"): answer = raw_input("Would you like to play again? y/N: ") if answer.lower() == "y": return True elif (answer == "") or (answer.lower() == "n"): return False
def _loop(): while _running: # Drawing IOUtils.clear() board.draw() # Updating updateVal = _update(IOUtils.getch()) if updateVal != None: return updateVal if _hasWon(): return _finish(True)
def _process_lcamps(self, u_name, u_name_device): unit = self.spacecraft.twtas[u_name] processed_unit = {} if 'on_cmd' and 'off_cmd' in unit.lcamp.cmds_pids.keys(): processed_unit['name'] = unit.lcamp.name.replace(' ', '_') processed_unit['on_cmd'] = IOUtils.underscore_it( unit.lcamp.cmds_pids['on_cmd'].num) processed_unit['off_cmd'] = IOUtils.underscore_it( unit.lcamp.cmds_pids['off_cmd'].num) processed_unit['subbus_pid'] = IOUtils.underscore_it( unit.subbus.num) processed_unit['power_val'] = 0.0 return processed_unit
def calculatetccl(builders, tolerance=0.01, res_file="", pretty_print=True, progress=True, max_threads=9): """ Concurrently calculates the ccl for an array of DynamicSimulationBuilders The results are outputted to IOUtils.res_file, and the progress to IOUtils.log_file """ dynamic_simulations = [each.build() for each in builders] if progress: IOUtils.log("IT HAS BEGAN \n") if pretty_print: header = "Disturbance".ljust(40) + "Control".ljust(35) + "T_ccl".ljust( 10) + "\n" if res_file == "": IOUtils.res(header) else: IOUtils.logToFile(header, res_file) start = current_milli_time() with concurrent.futures.ProcessPoolExecutor( max_workers=max_threads) as executor: for each in dynamic_simulations: executor.submit(t_ccl.determineT_ccl, each, tolerance, res_file, pretty_print, progress) end = current_milli_time() if progress: IOUtils.log("Total time: " + str((end - start) / 60000 / 60) + " h " + str((end - start) / 60000 % 60) + " m " + str((end - start) / 1000 % 60) + " s\n")
def _process_tcnr(self, u_name, u_name_device, cmds): # Find on/off in database processed_unit = {} # num = re.search('\d+.*', u_name).group(0) num = IOUtils.get_rd(u_name) if re.search('bcn|beacon', u_name.lower()): search_str = 'xmtr' u_name_device = 'BcnXmtr_' + num elif re.search('tlm', u_name.lower()): search_str = 'xmtr' if '(2' in u_name: # Corrections for old RD schema num = re.search('\d\d\d', u_name) if num: num = num.group(0) else: num = '22' + re.search('\d', u_name).group(0) u_name_device = 'TlmXmtr_' + num elif re.search('twt', u_name.lower()): search_str = 'twt' processed_unit['name'] = u_name_device cmds_t = self.dbase.get_cmd_pid(cmds, '%s' % num) cmds_t = self.dbase.get_cmd_pid(cmds_t, search_str) cmds_t = self.dbase.get_cmd_pid(cmds_t, 'rang', '!=') cmds_tt = {} for cmd in cmds_t.values(): # Filter out linked commands if ',' not in cmd.des: cmds_tt[cmd.num] = cmd cmd = self.dbase.get_cmd_pid(cmds_tt, 'on') if len(cmd) == 1: cmd = cmd[next(iter(cmd))].num else: self.gui.output_text(' Warning - ON cmd mixed up for %s' % u_name) return 0 processed_unit['on_cmd'] = IOUtils.underscore_it(cmd) cmd = self.dbase.get_cmd_pid(cmds_tt, 'off') if len(cmd) == 1: cmd = cmd[next(iter(cmd))].num else: self.gui.output_text(' Warning - OFF cmd mixed up for %s' % u_name) return 0 processed_unit['off_cmd'] = IOUtils.underscore_it(cmd) processed_unit['power_val'] = 0.0 processed_unit['subbus_pid'] = IOUtils.underscore_it( self.dbase.carved_subbuses[u_name].num) return processed_unit
def _process_heaters(self, u_name, u_name_device): unit = self.spacecraft.cf_heaters[u_name] processed_unit = {} processed_unit['name'] = u_name_device processed_unit['on_cmd'] = IOUtils.underscore_it( unit.cmds_pids['on_cmd'].num) processed_unit['off_cmd'] = IOUtils.underscore_it( unit.cmds_pids['off_cmd'].num) if 'cur_mon_tlm' in unit.cmds_pids.keys(): processed_unit['cur_mon_tlm'] = IOUtils.underscore_it( unit.cmds_pids['cur_mon_tlm'].num) processed_unit['subbus_pid'] = IOUtils.underscore_it(unit.subbus.num) processed_unit['power_val'] = float(unit.power_val) return processed_unit
def _process_tcnr(self, u_name, u_name_device, cmds): # Find on/off in database processed_unit = {} # num = re.search('\d+.*', u_name).group(0) num = IOUtils.get_rd(u_name) if re.search('bcn|beacon', u_name.lower()): search_str = 'xmtr' u_name_device = 'BcnXmtr_' + num elif re.search('tlm', u_name.lower()): search_str = 'xmtr' if '(2' in u_name: # Corrections for old RD schema num = re.search('\d\d\d', u_name) if num: num = num.group(0) else: num = '22' + re.search('\d', u_name).group(0) u_name_device = 'TlmXmtr_' + num elif re.search('twt', u_name.lower()): search_str = 'twt' processed_unit['name'] = u_name_device cmds_t = self.dbase.get_cmd_pid(cmds,'%s' % num) cmds_t = self.dbase.get_cmd_pid(cmds_t, search_str) cmds_t = self.dbase.get_cmd_pid(cmds_t, 'rang', '!=') cmds_tt = {} for cmd in cmds_t.values(): # Filter out linked commands if ',' not in cmd.des: cmds_tt[cmd.num] = cmd cmd = self.dbase.get_cmd_pid(cmds_tt,'on') if len(cmd) == 1: cmd = cmd[next(iter(cmd))].num else: self.gui.output_text(' Warning - ON cmd mixed up for %s' % u_name) return 0 processed_unit['on_cmd'] = IOUtils.underscore_it(cmd) cmd = self.dbase.get_cmd_pid(cmds_tt,'off') if len(cmd) == 1: cmd = cmd[next(iter(cmd))].num else: self.gui.output_text(' Warning - OFF cmd mixed up for %s' % u_name) return 0 processed_unit['off_cmd'] = IOUtils.underscore_it(cmd) processed_unit['power_val'] = 0.0 processed_unit['subbus_pid'] = IOUtils.underscore_it(self.dbase.carved_subbuses[u_name].num) return processed_unit
def _process_misc(self, u_name, u_name_device, cmds, macros, tlm_misc): # Find on/off in database processed_unit = {} processed_unit['name'] = u_name_device.replace('Side_','') if re.search('_\d', u_name[-2:]): u_name_t = u_name[:-2] elif re.search('mro.*side', u_name.lower()): u_name_t = u_name + ' dc' else: u_name_t = u_name cmd = self.dbase.get_cmd_pid(cmds,'%s on' % u_name_t) if len(cmd) == 0: cmd = self.dbase.get_cmd_pid(macros,'%s on' % u_name_t) if len(cmd) == 1: cmd = cmd[next(iter(cmd))].num else: self.gui.output_text(' Warning - ON cmd mixed up for %s' % u_name) return 0 processed_unit['on_cmd'] = IOUtils.underscore_it(cmd) cmd = self.dbase.get_cmd_pid(cmds,'%s off' % u_name_t) if len(cmd) == 0: cmd = self.dbase.get_cmd_pid(macros,'%s on' % u_name_t) if len(cmd) == 1: cmd = cmd[next(iter(cmd))].num else: self.gui.output_text(' Warning - OFF cmd mixed up for %s' % u_name) return 0 processed_unit['off_cmd'] = IOUtils.underscore_it(cmd) processed_unit['power_val'] = 0.0 processed_unit['subbus_pid'] = IOUtils.underscore_it(self.dbase.carved_subbuses[u_name].num) if u_name_t in self.dbase.mmdc_assoc.keys(): master_unit = self.dbase.mmdc_assoc[u_name_t] if 'MMDC' in master_unit: tlm_search = 'MMDC ' + master_unit[4:] + ' Side ' + u_name_device[-1] + ' Curnt Sense' elif 'CPSU' in master_unit: if u_name_device[-1] == '1': tlm_search = 'CPSU ' + master_unit[4:] + ' EPC P Output Shunt Curnt' else: tlm_search = 'CPSU ' + master_unit[4:] + ' EPC R Output Shunt Curnt' else: tlm_search = 'do not search, blerg' cmd = self.dbase.get_cmd_pid(tlm_misc, tlm_search) if len(cmd) == 1: cmd = cmd[next(iter(cmd))].num processed_unit['bus_curnt_tlm'] = IOUtils.underscore_it(cmd) return processed_unit
def getCategories(): categories_file_path = os.path.dirname( __file__) + '/DB1BAirports/2011Q1_AIRPORTS.csv' categories = IOUtils.getCategories(categories_file_path, 0, 2, sep=',', header=False) return categories
def getCategories(): locations_file_path = os.path.dirname( __file__) + '/Porto_Taxis/poi_locations.csv' categories = IOUtils.getCategories(locations_file_path, 0, 5, sep=',', header=False) return categories
def playOneMatchImpl(self, decisionType, learner, verbose): self.score = 0 gameSituations = [] gameDecisions = [] self.respawnSnake() self.respawnFruit() gameOver = False while not gameOver: if verbose: mioutils.outputBoard(self.currentBoard) sample = mlearner.mapDNNInputVar(self) wd = 0 moveIsValid = False while True: if decisionType == MoveType_random: move = self.getRandomMove() if decisionType == MoveType_predict: move = self.getPredictedMove(sample, learner) #print("Predicted move is: ", moveMnemonics[move]) if self.isMoveValid(move): moveIsValid = True break if wd >= LOOP_WATCH_DOG: break wd += 1 if not moveIsValid: print( "Could't evaluate a valid move - this Individual is quitting the match" ) gameOver = True else: looping = self.checkIfLooping(move) if looping: #print("Player is looping - this Individual is forced to quit the match") self.looped = True gameOver = True else: gameSituations.append(sample) gameOver = self.snakeProceed(move) decision = mlearner.mapDNNOutputVar(move) #print("Move = ", move , "; Decision: ", decision) gameDecisions.append(decision) if gameOver: if verbose: mioutils.outputBoard(self.currentBoard) return gameSituations, gameDecisions, self.score
def test(): game = mgame.Game() mioutils.outputBoard(game.currentBoard) game.respawnSnake() mioutils.outputBoard(game.currentBoard) game.snakeGoRight() mioutils.outputBoard(game.currentBoard) game.growUp() mioutils.outputBoard(game.currentBoard)
def loadBoard_FromCPP(fIN): playerColor = None firstLine = fIN.readline() #print("Player color from file: [", firstLine, "]") if "B" in firstLine or "b" in firstLine: playerColor = mGAD.B if "W" in firstLine or "w" in firstLine: playerColor = mGAD.W return playerColor, mioutils.readBoardFromFile(fIN)
def getLocations(): locations_file_path = os.path.dirname( __file__) + '/Porto_Taxis/poi_locations.csv' # locations = IOUtils.readLocations(locations_file_path,0,1,2,sep=',',header=False) locations = IOUtils.readLocations(locations_file_path, 0, 3, 4, sep=',', header=False) return locations
def getLocations(): locations_file_path = os.path.dirname( __file__) + '/DB1BAirports/MASTER_CORD.csv' # locations = IOUtils.readLocations(locations_file_path,0,1,2,sep=',',header=False) locations = IOUtils.readLocations(locations_file_path, 0, 1, 2, sep=',', header=True) return locations
def getIdsToWrite(): """ Returns the indices of the predictions to write to csv for evaluation. """ df = pd.read_csv("../data/sampleSubmission.csv") ids = np.array(df['Id']) idsToWrite = np.zeros((len(ids), 2)) for i in range(np.shape(ids)[0]): row, col = IOUtils.parseId(ids[i]) idsToWrite[i] = row, col return idsToWrite.astype(int)
def GenerateImgs(self, count, textsFile, imgType="PURE", noiseMode="Imgaug"): #type: PURE, NOISE texts = IOUtils.LoadTexts(textsFile) tImages = [] tNumLabels = [] tCharLabels = [] textsSize = len(texts) textID = [] parmID = [] logging.info("Processing. Start to generate bin text images.") logging.info("Processing. Texts number: %d", textsSize) logging.info("Processing. Images number: %d", count) logging.info("Processing. Size of pramaters: %d", len(self.parms)) for i in range(textsSize): textID.append(i) parmID.append(random.randint(0, len(self.parms) - 1)) for i in range(count): if i % textsSize == 0: random.shuffle(textID) idx = i % textsSize textId = textID[idx] parmId = parmID[idx] font = FT(self.parms[parmId]) if (not (imgType == "PURE")) and self.hasTemplate: resImage, resLabel, resState = self.GenerateImg( 32, 32, 32, 5, 5, font, texts[textId], True, noiseMode) else: resImage, resLabel, resState = self.GenerateImg( 32, 32, 32, 5, 5, font, texts[textId], False, ) if resState is True: tImages.append(resImage) tNumLabels.append(resLabel) tCharLabels.append(texts[textId]) else: random.shuffle(textID) if i % 100 == 0: logging.info("Prosessing. %d images have been generated.", i) return tImages, tNumLabels, tCharLabels
def _process_twtas(self, u_name, u_name_device): unit = self.spacecraft.twtas[u_name] processed_unit = {} shared_tl_off = True if 'off_cmd' in unit.lcamp.cmds_pids.keys(): off_cmd = unit.lcamp.cmds_pids['off_cmd'] if 'off_cmd' in unit.cmds_pids.keys(): if off_cmd.num != unit.cmds_pids['off_cmd'].num: shared_tl_off = False else: shared_tl_off = False if shared_tl_off: processed_unit['name'] = unit.cmds_pids['on_cmd'].des[:-3].replace(' ', '_').replace(',', '') else: processed_unit['name'] = unit.name.replace(' ', '_') processed_unit['on_cmd'] = IOUtils.underscore_it(unit.cmds_pids['on_cmd'].num) processed_unit['off_cmd'] = IOUtils.underscore_it(unit.cmds_pids['off_cmd'].num) processed_unit['subbus_pid'] = IOUtils.underscore_it(unit.subbus.num) processed_unit['power_val'] = 0.0 if 'bus_curnt_tlm' in unit.cmds_pids.keys(): processed_unit['bus_curnt_tlm'] = IOUtils.underscore_it(unit.cmds_pids['bus_curnt_tlm'].num) return processed_unit
def splitSetCSV(validationPercentage, height,width, inp="../data/data_train.csv"): df = pd.read_csv(inp) ids=np.array(df['Id']) pred=np.array(df['Prediction']) nrRatings = np.shape(ids)[0] nrTrain = (int)(nrRatings*(1-validationPercentage)) nrTest = nrRatings-nrTrain indicesTrain = np.random.choice(nrRatings,nrTrain,replace=False) Xtrain = np.zeros((height,width)) Xtest = np.zeros((height,width)) for i in indicesTrain: row,col=IOUtils.parseId(ids[i]) Xtrain[row,col] = pred[i] indicesTest = np.setdiff1d(np.arange(nrRatings), indicesTrain) for i in indicesTest: row,col=IOUtils.parseId(ids[i]) Xtest[row,col] = pred[i] if os.path.exists("temporary.csv"): os.remove("temporary.csv") return Xtrain,Xtest,nrTrain, nrTest
def getSequences(min_length=2): ''' Load Taxi trips in file reported in file trajectories_amenity.csv with a length of at least min_length. Note: if DataModUtils.removeRepetitions is called on the results some sequences may be of length < min_length ''' filepath_traces = os.path.dirname( __file__) + '/Porto_Taxis/trajectories_amenity.csv' temp_seqs = IOUtils.readFile(filepath_traces) sequences = [] for s in temp_seqs: if len(s) >= min_length: sequences.append(s) return sequences
def ComputeMeanStds(self): meanStds = [] imagesList = IOUtils.GetFilesList(self.pathToTemplateImages) logging.info("Done. Load all template images: "+str(len(imagesList))) count = 0 for imageName in imagesList: count += 1; if count % 10 == 0: logging.info("Processing. Compute mean & std: "+str(count)) img = cv2.imread(osj(self.pathToTemplateImages,imageName)) meanPre, stdPre, meanBack, stdBack = self.ComputeMeanStd(img) if(np.abs(meanPre-meanBack)<30): logging.warning("Processing. The values of background and foreground are close: %f", np.abs(meanPre-meanBack)) tempmeanStd = [meanPre, stdPre, meanBack, stdBack] meanStds.append(tempmeanStd) return meanStds
def add_units(self, app_c_data, app_f_data): """ Populate spacecraft object with cf heaters Description: Iterates through thermal STIRS appendices to obtain cf heater properties (cmds, tlm, etc.), then populates spacecraft object cf heater list. Arguments: app_c_data -- Heaters. Dictionary of heater name:(dictionary of cmd/tlm id:cmd/tlm object) app_f_data -- Heater sfty relays. Dictionary of htr sfty rly name:(dictionary of cmd/tlm id:cmd/tlm object) """ for htr_name in sorted(app_c_data.keys()): if 'Htr' in htr_name and 'Side' not in htr_name: htr = Components.Heater() htr.name = htr_name htr.cmds_pids = app_c_data[htr_name][0] htr.power_val = app_c_data[htr_name][1] # Standardize naming convention htr_name = IOUtils.adjust_htr_name(htr_name) if htr_name in self.dbase.carved_subbuses.keys(): htr.subbus = self.dbase.carved_subbuses[htr_name] htr.tray = self.dbase.carved_prefixes[htr_name] else: # See if not exact match, but simply 'contained in' print_error = True for htr_name_t in self.dbase.carved_subbuses.keys(): if re.search('^' + htr_name, htr_name_t): htr.subbus = self.dbase.carved_subbuses[htr_name_t] htr.tray = self.dbase.carved_prefixes[htr_name_t] print_error = False break if print_error: self.gui.output_text( ' Warning - Subbus not associated for ' + htr.name + '. Assuming LVC.') htr.subbus = self.dbase.subbuses['LVC'] htr.tray = '' for sfty_rly_name in app_f_data.keys(): if re.search('^' + htr.name, sfty_rly_name): htr.sfty_rly.name = sfty_rly_name htr.sfty_rly.cmds_pids = app_f_data[sfty_rly_name] break self.spacecraft.cf_heaters[htr.name] = htr if 'batt' not in htr_name.lower(): self._carve(htr.name)
def add_units(self, app_c_data, app_f_data): """ Populate spacecraft object with cf heaters Description: Iterates through thermal STIRS appendices to obtain cf heater properties (cmds, tlm, etc.), then populates spacecraft object cf heater list. Arguments: app_c_data -- Heaters. Dictionary of heater name:(dictionary of cmd/tlm id:cmd/tlm object) app_f_data -- Heater sfty relays. Dictionary of htr sfty rly name:(dictionary of cmd/tlm id:cmd/tlm object) """ for htr_name in sorted(app_c_data.keys()): if 'Htr' in htr_name and 'Side' not in htr_name: htr = Components.Heater() htr.name = htr_name htr.cmds_pids = app_c_data[htr_name][0] htr.power_val = app_c_data[htr_name][1] # Standardize naming convention htr_name = IOUtils.adjust_htr_name(htr_name) if htr_name in self.dbase.carved_subbuses.keys(): htr.subbus = self.dbase.carved_subbuses[htr_name] htr.tray = self.dbase.carved_prefixes[htr_name] else: # See if not exact match, but simply 'contained in' print_error = True for htr_name_t in self.dbase.carved_subbuses.keys(): if re.search('^'+htr_name, htr_name_t): htr.subbus = self.dbase.carved_subbuses[htr_name_t] htr.tray = self.dbase.carved_prefixes[htr_name_t] print_error = False break if print_error: self.gui.output_text(' Warning - Subbus not associated for ' + htr.name + '. Assuming LVC.') htr.subbus = self.dbase.subbuses['LVC'] htr.tray = '' for sfty_rly_name in app_f_data.keys(): if re.search('^'+htr.name,sfty_rly_name): htr.sfty_rly.name = sfty_rly_name htr.sfty_rly.cmds_pids = app_f_data[sfty_rly_name] break self.spacecraft.cf_heaters[htr.name] = htr if 'batt' not in htr_name.lower(): self._carve(htr.name)
def calculatetccl(builders,tolerance=0.01,res_file="",pretty_print=True,progress=True,max_threads=9): """ Concurrently calculates the ccl for an array of DynamicSimulationBuilders The results are outputted to IOUtils.res_file, and the progress to IOUtils.log_file """ dynamic_simulations = [each.build() for each in builders] if progress: IOUtils.log("IT HAS BEGAN \n") if pretty_print: header = "Disturbance".ljust(40)+"Control".ljust(35)+"T_ccl".ljust(10)+"\n" if res_file == "": IOUtils.res(header) else: IOUtils.logToFile(header,res_file) start = current_milli_time() with concurrent.futures.ProcessPoolExecutor(max_workers=max_threads) as executor: for each in dynamic_simulations: executor.submit(t_ccl.determineT_ccl,each,tolerance,res_file,pretty_print,progress) end = current_milli_time() if progress: IOUtils.log("Total time: "+str((end-start)/60000/60)+" h "+str((end-start)/60000%60)+" m "+str((end-start)/1000%60)+" s\n")
def extractBuggyCommits(input_filename, local_repos_directory, output_directory): projects = ProjectLoader.getReposPlainName(input_filename) print(projects) for projectName in projects: bug_counter = 0 bugFixes = [] print("Analyzing", projectName) if (projectName == 'pytorch' or projectName == 'react-native'): continue if (os.path.exists( str(output_directory) + "/" + str(projectName) + "_bug_fixing_commits") and os.path.isfile( str(output_directory) + "/" + str(projectName) + "_bug_fixing_commits")): print(projectName, "already analyzed, skipping...") continue # 1. iterate over each project # 2. find all commits that fixed bugs using syntactic analysis # 3. find the commit that caused the bug # 4. for the commit that caused the bug, extract how many files were in the change set, the number of lines changed (added or removed), the author, how many commits the file has had to it, the number of contributors that have contributed to the file, contributor experience which returns the percentage of # lines authored by the highest contributor of a file, the hunks count # commits count, contributor count and contributor experience are in process metrics startTime = time.time() for commit in RepositoryMining( local_repos_directory + "/" + str(projectName), only_in_branch='master', only_no_merge=True, since=datetime.datetime(2019, 6, 1, 0, 0, 0)).traverse_commits(): commit_msg = commit.msg containsBug = 'bug' in commit_msg.casefold() containsPatch = 'patch' in commit_msg.casefold() containsFix = 'fix' in commit_msg.casefold() containsBugIdentifier = bool(re.search('#+\d', commit_msg)) if (containsBug and (containsFix or containsPatch or containsBugIdentifier)) or ( containsFix and containsBugIdentifier): bug_counter = bug_counter + 1 # get the list of modified files in the fix listFixedFiles = commit.modifications numFilesModifiedForFix = 0 numLinesAddedForFix = 0 numLinesRemovedForFix = 0 totalComplexityFixedFiles = 0 fileComplexityCount = 0 averageComplexityFixedFiles = -1 totalLinesOfCodeAllFiles = 0 changedMethods = 0 numFilesMoved = 0 for file in listFixedFiles: sourceCodeLanguage = LanguageDetector.detect(file.filename) if (sourceCodeLanguage == None or file.nloc == 0): continue if (file.nloc): totalLinesOfCodeAllFiles = totalLinesOfCodeAllFiles + file.nloc numFilesModifiedForFix = numFilesModifiedForFix + 1 numLinesAddedForFix = numLinesAddedForFix + file.added numLinesRemovedForFix = numLinesRemovedForFix + file.removed if file.complexity: fileComplexityCount = fileComplexityCount + 1 totalComplexityFixedFiles = totalComplexityFixedFiles + file.complexity changedMethods = changedMethods + len(file.changed_methods) if (numFilesModifiedForFix == 0): continue if (fileComplexityCount != 0): averageComplexityFixedFiles = totalComplexityFixedFiles / fileComplexityCount bugFixInfo = { "commit_hash": commit.hash, "author": commit.author.name, "total_complexity": totalComplexityFixedFiles, "average_complexity": averageComplexityFixedFiles, "sum_nloc": totalLinesOfCodeAllFiles, "num_files": numFilesModifiedForFix, "lines_added": numLinesAddedForFix, "lines_removed": numLinesRemovedForFix, "commit_date": commit.author_date, "branches": commit.branches, "num_methods_changed": changedMethods } bugFixes.append(bugFixInfo) tempMap = {projectName: bugFixes} IOUtils.writeBugMap(tempMap, output_directory, "_bug_fixing_commits") endTime = time.time() print("time", endTime - startTime)
def RidgePostProcessing(): A, Ind = IOUtils.initialization() final = np.load("../../CIL_results/results/RSVD.npy") for row, col in Ind: final[row, col] = A[row, col] factor = 12 U, s, ZT = np.linalg.svd(final, full_matrices=True) D = np.diag(s[:factor]) U = np.matmul(U[:, :factor], np.sqrt(D)) ZT = np.matmul(np.sqrt(D), ZT[:factor, :]) lambda_3 = 0.7 postFinal = np.zeros((A.shape[0], A.shape[1])) for user in range(len(final)): if user % 100 == 0: print(user) rated_movies_indices = Ind[ Ind[:, 0] == user, 1] # vector of indices of movies rated by user user y = A[ user, rated_movies_indices] # vector of ratings to the rated movies by user user vj = [ZT.T[mov, :] for mov in rated_movies_indices ] # shape vj: num_rated_movies * #factors norm = np.linalg.norm(vj, axis=1) vi = [ZT.T[mov, :] for mov in np.arange(0, 1000) ] # shape (1000-num_rated_movies) * #factors norm_vi = np.linalg.norm( vi, axis=1) # vector of shape (1000-num_rated_movies) Xi = np.zeros((len(vi), len(vi[0]))) for i in range(len(vi)): Xi[i] = vi[i] / norm_vi[ i] # shape (1000-num_rated_movies) * #factors X = np.zeros((len(vj), len(vj[0]))) for i in range(len(vj)): X[i] = vj[i] / norm[i] # shape X: num_rated_movies * #factors s = np.matmul( np.exp(2 * (np.subtract(np.matmul(Xi, X.T), 1))), np.matmul( np.linalg.inv( np.exp(2 * (np.subtract(np.matmul(X, X.T), 1))) + lambda_3 * np.identity(len(vj))), y.T)) postFinal[user, :] = s postFinal = np.clip(postFinal, 1, 5) now = datetime.datetime.fromtimestamp( time.time()).strftime('%Y-%m-%d %H:%M:%S') np.save("Ridge" + now + "", postFinal)
def _process_acs_misc(self): processed_units = {} processed_units_sort = [] # ACE processed_units['ACE_1'] = {} processed_units['ACE_1']['name'] = 'ACE_1' processed_units['ACE_1']['on_cmd'] = 'AC_00100' processed_units['ACE_1']['off_cmd'] = 'AC_00105' processed_units['ACE_1']['power_val'] = 0.0 processed_units['ACE_1']['subbus_pid'] = IOUtils.underscore_it(self.dbase.carved_subbuses['ACE_1'].num) processed_units_sort.append('ACE_1') processed_units['ACE_2'] = {} processed_units['ACE_2']['name'] = 'ACE_2' processed_units['ACE_2']['on_cmd'] = 'AC_00200' processed_units['ACE_2']['off_cmd'] = 'AC_00205' processed_units['ACE_2']['power_val'] = 0.0 processed_units['ACE_2']['subbus_pid'] = IOUtils.underscore_it(self.dbase.carved_subbuses['ACE_2'].num) processed_units_sort.append('ACE_2') # ICU units: BA_GSP, Encoder if 'ICU_1' in self.dbase.carved_subbuses.keys(): processed_units['BA_GSP_1'] = {} processed_units['BA_GSP_1']['name'] = 'BA_GSP_1' processed_units['BA_GSP_1']['on_cmd'] = 'HW_03570' processed_units['BA_GSP_1']['off_cmd'] = 'HW_03575' processed_units['BA_GSP_1']['power_val'] = 0.0 processed_units['BA_GSP_1']['subbus_pid'] = IOUtils.underscore_it(self.dbase.carved_subbuses['ICU_1'].num) processed_units_sort.append('BA_GSP_1') processed_units['BA_GSP_2'] = {} processed_units['BA_GSP_2']['name'] = 'BA_GSP_2' processed_units['BA_GSP_2']['on_cmd'] = 'HW_04010' processed_units['BA_GSP_2']['off_cmd'] = 'HW_04015' processed_units['BA_GSP_2']['power_val'] = 0.0 processed_units['BA_GSP_2']['subbus_pid'] = IOUtils.underscore_it(self.dbase.carved_subbuses['ICU_2'].num) processed_units_sort.append('BA_GSP_2') processed_units['Encoder_1'] = {} processed_units['Encoder_1']['name'] = 'Encoder_1' processed_units['Encoder_1']['on_cmd'] = 'DH_01800' processed_units['Encoder_1']['off_cmd'] = 'DH_01805' processed_units['Encoder_1']['power_val'] = 0.0 processed_units['Encoder_1']['subbus_pid'] = IOUtils.underscore_it(self.dbase.carved_subbuses['ICU_1'].num) processed_units_sort.append('Encoder_1') processed_units['Encoder_2'] = {} processed_units['Encoder_2']['name'] = 'Encoder_2' processed_units['Encoder_2']['on_cmd'] = 'DH_01850' processed_units['Encoder_2']['off_cmd'] = 'DH_01855' processed_units['Encoder_2']['power_val'] = 0.0 processed_units['Encoder_2']['subbus_pid'] = IOUtils.underscore_it(self.dbase.carved_subbuses['ICU_2'].num) processed_units_sort.append('Encoder_2') # ST if 'AC26100' in self.dbase.cmd.keys(): # AC26100 - ST 1 ON Seq processed_units['ST_1'] = {} processed_units['ST_1']['name'] = 'ST_1' processed_units['ST_1']['on_cmd'] = 'AC_26110' processed_units['ST_1']['off_cmd'] = 'AC_26115' processed_units['ST_1']['power_val'] = 4.5 processed_units['ST_1']['subbus_pid'] = 'PWD90065' processed_units_sort.append('ST_1') processed_units['ST_2'] = {} processed_units['ST_2']['name'] = 'ST_2' processed_units['ST_2']['on_cmd'] = 'AC_26210' processed_units['ST_2']['off_cmd'] = 'AC_26215' processed_units['ST_2']['power_val'] = 4.5 processed_units['ST_2']['subbus_pid'] = 'PWD90065' processed_units_sort.append('ST_2') # ES processed_units['ES_1'] = {} processed_units['ES_1']['name'] = 'ES_1' processed_units['ES_1']['on_cmd'] = 'AC_21000' processed_units['ES_1']['off_cmd'] = 'AC_21005' processed_units['ES_1']['power_val'] = 4.7 processed_units['ES_1']['subbus_pid'] = 'PWD90065' processed_units_sort.append('ES_1') processed_units['ES_2'] = {} processed_units['ES_2']['name'] = 'ES_2' processed_units['ES_2']['on_cmd'] = 'AC_22000' processed_units['ES_2']['off_cmd'] = 'AC_22005' processed_units['ES_2']['power_val'] = 4.7 processed_units['ES_2']['subbus_pid'] = 'PWD90065' processed_units_sort.append('ES_2') # TEC processed_units['TECpri'] = {} processed_units['TECpri']['name'] = 'TECpri' processed_units['TECpri']['on_cmd'] = 'AC_24100' processed_units['TECpri']['off_cmd'] = 'AC_24080' processed_units['TECpri']['power_val'] = 4.8 processed_units['TECpri']['subbus_pid'] = 'PWD90065' processed_units_sort.append('TECpri') processed_units['TECstby'] = {} processed_units['TECstby']['name'] = 'TECstby' processed_units['TECstby']['on_cmd'] = 'AC_25100' processed_units['TECstby']['off_cmd'] = 'AC_25080' processed_units['TECstby']['power_val'] = 4.8 processed_units['TECstby']['subbus_pid'] = 'PWD90065' processed_units_sort.append('TECstby') # Gyro if 'AC14350' in self.dbase.cmd.keys(): # AC_14350 - RLG 1 ON Seq processed_units['RLG_1'] = {} processed_units['RLG_1']['name'] = 'RLG_1' processed_units['RLG_1']['on_cmd'] = 'AC_14360' processed_units['RLG_1']['off_cmd'] = 'AC_14365' processed_units['RLG_1']['power_val'] = 23.0 processed_units['RLG_1']['subbus_pid'] = 'PWD90065' processed_units_sort.append('RLG_1') processed_units['RLG_2'] = {} processed_units['RLG_2']['name'] = 'RLG_2' processed_units['RLG_2']['on_cmd'] = 'AC_15360' processed_units['RLG_2']['off_cmd'] = 'AC_15365' processed_units['RLG_2']['power_val'] = 23.0 processed_units['RLG_2']['subbus_pid'] = 'PWD90065' processed_units_sort.append('RLG_2') if 'AC16350' in self.dbase.cmd.keys(): # AC16350 - RLG 3 ON Seq processed_units['RLG_3'] = {} processed_units['RLG_3']['name'] = 'RLG_3' processed_units['RLG_3']['on_cmd'] = 'AC_16360' processed_units['RLG_3']['off_cmd'] = 'AC_16365' processed_units['RLG_3']['power_val'] = 23.0 processed_units['RLG_3']['subbus_pid'] = 'PWD90065' processed_units_sort.append('RLG_3') return processed_units_sort, processed_units
def findBugCausingCommits(projectMap, local_repos_directory, output_directory): bugInducingProjectMap = {} for project, commits in projectMap.items(): print("finding bug causing commits for ", str(local_repos_directory) + "/" + project) if (os.path.exists( str(output_directory) + "/" + str(project) + "_bug_causing_commits") and os.path.isfile( str(output_directory) + "/" + str(project) + "_bug_causing_commits")): print(project, "already analyzed, skipping...") continue repo_path = str(local_repos_directory) + "/" + project repo = GitRepository(repo_path) startTime = time.time() bugInducingCommits = [] hashes = [x["commit_hash"] for x in commits] try: # analyze each bug fix for this project for bugFix in RepositoryMining( repo_path, only_commits=hashes).traverse_commits(): # get the commits that last touched the modified lines of the files commitsLastTouchedFix = repo.get_commits_last_modified_lines( bugFix) bugCausingHashes = set([]) for filename, fileCommit in commitsLastTouchedFix.items(): for fileHash in fileCommit: bugCausingHashes.add(fileHash) hashList = [x for x in bugCausingHashes] # get average statistics about each of these commits # number of files modified for the commit # number of lines added for the commit # number of lines removed for the commit # number of methods changed for the commit # author of the commit # the elapsed time for the bug fix # branches for bugCausingCommit in RepositoryMining( repo_path, only_commits=hashList).traverse_commits(): numModifiedFiles = len(bugCausingCommit.modifications) linesAdded = 0 linesRemoved = 0 numMethodsChanged = 0 sum_nloc = 0 numFilesWithComplexity = 0 sumComplexity = 0 if numModifiedFiles <= 0: continue for modification in bugCausingCommit.modifications: sourceCodeLanguage = LanguageDetector.detect( modification.filename) try: if (sourceCodeLanguage == None or modification.nloc == 0 or modification.nloc is None): continue except: pass sum_nloc = sum_nloc + modification.nloc linesAdded = linesAdded + modification.added linesRemoved = linesRemoved + modification.removed numMethodsChanged = numMethodsChanged + len( modification.changed_methods) if modification.complexity: numFilesWithComplexity = numFilesWithComplexity + 1 sumComplexity = sumComplexity + modification.complexity averageComplexityFixedFiles = 0 if (numFilesWithComplexity != 0): averageComplexityFixedFiles = sumComplexity / numFilesWithComplexity bugInducingInfo = { "commit_hash": bugCausingCommit.hash, "author": bugCausingCommit.author.name, "total_complexity": sumComplexity, "average_complexity": averageComplexityFixedFiles, "sum_nloc": sum_nloc, "num_files": numModifiedFiles, "lines_added": linesAdded, "lines_removed": linesRemoved, "commit_date": bugCausingCommit.author_date, "branches": bugCausingCommit.branches, "num_methods_changed": numMethodsChanged, "time_to_fix": bugFix.author_date - bugCausingCommit.author_date } # print(bugInducingInfo["commit_hash"]) # print(bugInducingInfo["author"]) # print(bugInducingInfo["total_complexity"]) # print(bugInducingInfo["average_complexity"]) # print(bugInducingInfo["sum_nloc"]) # print(bugInducingInfo["num_files"]) # print(bugInducingInfo["lines_added"]) # print(bugInducingInfo["lines_removed"]) # print(bugInducingInfo["commit_date"]) # print(bugInducingInfo["branches"]) # print(bugInducingInfo["num_methods_changed"]) # print(bugInducingInfo["time_to_fix"]) bugInducingCommits.append(bugInducingInfo) tempMap = {project: bugInducingCommits} IOUtils.writeBugMap(tempMap, output_directory, "_bug_causing_commits") endTime = time.time() print("time", endTime - startTime) except: print("FAILED FOR", project) pass
def getLocations(): locations_file_path = os.path.dirname(__file__)+'/Lloyds_maritime/table_places.csv' locations = IOUtils.readLocations(locations_file_path,4,6,7) return locations
def getSequences(): filepath_traces = os.path.dirname(__file__)+'/Lloyds_maritime/apr2009_oct2009/portseq_apr2009_oct2009.csv' sequences = IOUtils.readFile(filepath_traces) return sequences
import channel_utils
def main(): print("GAD start") print() print("GAD Copyright 2019 Eugenio Menegatti") print("This program comes with ABSOLUTELY NO WARRANTY; type D at the color selection prompt for details.") print("This is free software, and you are welcome to redistribute it") print("under certain conditions; type C at the color selection prompt for details.") print("-----") print() gad = mGAD.GAD() gad.ai.init(mai.DEFAULT_AI_TYPE) quit = gad.playerChooseColor() if not quit: startTime = time.time() print() gameOver = False playerPassed = False starting = True prevBoard = copy.deepcopy(gad.currentBoard) while not quit and not gameOver: if starting: starting = False if gad.player.upper() == mGAD.MOVES_FIRST: mioutils.outputBoard( gad.currentBoard ) print("Player is: ", gad.player) quit, playerPassed = gad.humanMove() if not playerPassed: # da correggere saveMove(prevBoard, x, y, heuristic) pass else: if gad.player == mGAD.N: print() print("Player is: W") print("Computer is moving for player, please wait") print() computerMoved, x, y, heuristic, prediction = gad.noPlayerMove() if computerMoved: #gad.saveMove(prevBoard, x, y, heuristic) prevBoard = copy.deepcopy(gad.currentBoard) playerPassed = not computerMoved else: mioutils.outputBoard( gad.currentBoard ) print("Player is: ", gad.player) quit, playerPassed = gad.humanMove() if not quit: mioutils.outputBoard( gad.currentBoard ) print("Computer is thinking, please wait.") print() computerMoved, x, y, heuristic, prediction = gad.computerMove() if computerMoved: print("Computer moves in ", getOutputFromMoveCoords(x, y), " with heuristic gain = ", heuristic, " and prediction = ", prediction) #No perche' salvo l'albero in AI gad.saveMove(prevBoard, x, y, heuristic) if gad.ai.aiType == mai.LEARNING: gad.savePrediction(heuristic, prediction) prevBoard = copy.deepcopy(gad.currentBoard) if playerPassed and not computerMoved: gameOver = True if not gameOver: gameOver = checkGameOver(gad) if gameOver: endTime = time.time() elapsedTime = endTime - startTime print("Game over") print() print("Final board:") mioutils.outputBoard( gad.currentBoard ) print() blackCount, whiteCount = gad.getCheckersCount(gad.currentBoard) if blackCount > whiteCount: if gad.player == mGAD.B: print("Player wins") else: print("Computer wins") if whiteCount > blackCount: if gad.player == mGAD.W: print("Player wins") else: print("Computer wins") if blackCount == whiteCount: print("Game draw") print("White count = %d. Black Count = %d" %(whiteCount, blackCount)) print("Game time: %.1f seconds" %(elapsedTime)) gad.closeFiles() print() print("GAD end")
def saveBoard_ToCPP(board, x, y, fOUT): fOUT.write( str(x) + " " + str(y) + str('\n') ) mioutils.writeBoardToFile(board, fOUT)