def __init__(self, formData, construct=False): '''Constructor Parameters: formData - A FormData instance construct - If True this method calls construct immediately, otherwise construct must be called at a later stage. This allows parameters to be modified.''' self.formData = formData self.runString = None self.errorData = None self.job = None self.jobManager = None try: self.options = UtilityFunctions.DefaultWebAppConfiguration() self.connection = UtilityFunctions.ConnectionFromConfiguration( self.options) self.webServerDirectory = self.options.get('WEB APPLICATION', 'webServerDirectory') self.uploadLimit = int( self.options.get('WEB APPLICATION', 'uploadLimit')) except Core.Exceptions.EnvironmentError, data: self._setErrorData(data) return
def initUI(self): self.setGeometry(300, 300, *GB.WINDOW_SIZE) self.setWindowTitle(GB.WINDOW_NAME) self.downloadButton.action = 'download' self.downloadButton.move(75, 20) self.downloadButton.resize(100, 100) self.downloadButton.clicked.connect(self.onClick) self.uploadButton.action = 'upload' self.uploadButton.move(225, 20) self.uploadButton.resize(100, 100) self.uploadButton.clicked.connect(self.onClick) SIZE = 20 self.settingsButton.action = 'settings' self.settingsButton.move(*list(map(lambda x: x - SIZE, GB.WINDOW_SIZE))) self.settingsButton.resize(SIZE, SIZE) self.settingsButton.clicked.connect(self.onClick) self.downloadButtonLabel.setText(self.downloadButton.action) self.downloadButtonLabel.move(100, 100) self.uploadButtonLabel.setText(self.uploadButton.action) self.uploadButtonLabel.move(260, 100) self.settingsButtonLabel.setText(self.settingsButton.action) self.settingsButtonLabel.move(GB.WINDOW_SIZE[0] - SIZE - 60, GB.WINDOW_SIZE[1] - 20) if GB.isDebugEnabled: UF.debugOutput('successfully created the whole interface of MainForm.')
def initUI(self): self.setGeometry(500, 500, GB.WINDOW_SIZE[0], GB.WINDOW_SIZE[1] * 1.5) self.setWindowTitle(GB.WINDOW_NAME) self.sharingCodeLabel.field.setReadOnly(True) font = QtGui.QFont() font.setPointSize(16) self.sharingCodeLabel.field.setFont(font) self.sharingCodeLabel.field.setText(UF.convertCode(UF.getIP(), False)) self.pathButton.action = 'browse' self.pathButton.setText('browse') self.pathButton.setGeometry(270, 130, 60, 25) self.pathButton.clicked.connect(self.onClick) self.readyButton.setGeometry(70, 160, 260, 30) self.readyButton.action = 'ready' self.readyButton.clicked.connect(self.onClick) self.progressBar.setGeometry(70, 370, 250, 30) self.progressBar.setMaximum(100) self.progressBar.setValue(0) self.updateUI() UF.debugOutput('successfully initialized UI of receive form')
def main(): finalCheck = False # global secondGo global numberChoice global memberNum while finalCheck == False: UtilityFunctions.cls() print("Receipt Options") print("1. Get all receipts") # print("2. Get specific receipt") print("2. Get all receipts for member") print("0. Quit") numberChoice = int(input("Choose receipt option: ")) if (numberChoice == 0): finalCheck = True exit() if (numberChoice == 1): print("All receipts") getAllReceipts() finalCheck = True # if (numberChoice==2): # print("Print specific receipt") # filename = input("Enter filename") # if os.path.isfile(filename): # printDetailsOfReceipt(filename) # else: # print("Enter valid filename") if (numberChoice == 2): print("Print all receipts for member") memberNum = input("Enter member number") getAllReceiptsForMember(memberNum)
def initUI(self): self.resize(*GB.WINDOW_SIZE) self.setWindowTitle(GB.WINDOW_NAME + ' Settings') self.sharingCodeLabel.field.setReadOnly(True) font = QtGui.QFont() font.setPointSize(16) self.sharingCodeLabel.field.setFont(font) self.sharingCodeLabel.field.setText(UF.convertCode(UF.getIP(), False)) self.pathButton.action = 'browse' self.pathButton.setText('browse') self.pathButton.resize(60, 25) self.pathButton.move(270, 140) self.pathButton.clicked.connect(self.onClick) self.saveButton.action = 'save' self.saveButton.setText('apply') self.saveButton.resize(60, 25) self.saveButton.move(GB.WINDOW_SIZE[0] // 2 - 30, GB.WINDOW_SIZE[1] - 30) self.saveButton.clicked.connect(self.onClick) self.aboutButton = QPushButton(self) self.aboutButton.setText('about') self.aboutButton.action = 'about' self.aboutButton.move(10, GB.WINDOW_SIZE[1] - 30) self.aboutButton.resize(60, 25) self.aboutButton.setFlat(True) self.aboutButton.clicked.connect(self.onClick) self.updateUI() UF.debugOutput('successfully initialized UI of settings form')
def playHangman(group): #allows the user to play hangman from console or command line word = group[0] numtries = group[1] print "You are allowed " + str(numtries) + " incorrect guesses" wrongcount = 0 #the number of wrong guesses so far wordnospaces = word.replace(' ', '') wordnolines = wordnospaces.replace('\n', '') wordset = set(wordnolines) #the list of correct letters the user should be guessing guesswordset = set('') #the list of correct guesses the user has made allwordsguessed = set('') #the list of all the guesses the user had made letterset = set('ABCDEFGHIJKLMOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz') print '_'*len(word) input = raw_input('Guess a letter: ') guess = set(input) while wrongcount < numtries and guesswordset != wordset: if guess.issubset(letterset) == False or len(guess) != 1: #the user did not guess a letter input = raw_input('Letters only. Guess a letter: ') guess = set(input) elif guess.issubset(wordset) == False: #the user did not guess a correct letter wrongcount += 1 if wrongcount == numtries: #the user used up all their incorrect guesses print 'You are hung! Word was: ' + word + '.\n' return UtilityFunctions.give_information(input, allwordsguessed, word, guesswordset, numtries, wrongcount) UtilityFunctions.input = raw_input('No ' + input + '. Guess another letter: ') guess = set(input) elif guess.issubset(wordset): #the user guessed a correct letter guesswordset.add(input) UtilityFunctions.give_information(input, allwordsguessed, word, guesswordset, numtries, wrongcount) if guesswordset == wordset: #the user guessed all the letters in the word print "Congratulations!\n" return input = raw_input('Correct. Guess another letter: ') guess = set(input)
def update_amplitudes(self, KS_amplitude_file, KL_amplitude_file): if not (KS_amplitude_file == self.KS_amplitude_file): self.A_KS, self.s12, self.s13 = uf.load_amplitude(KS_amplitude_file) self.KS_amplitude_file = KS_amplitude_file if not (KL_amplitude_file == self.KL_amplitude_file): self.A_KL, self.s12L, self.s13L = uf.load_amplitude(KL_amplitude_file) self.KL_amplitude_file = KL_amplitude_file if not (np.array_equal(self.s12, self.s12L) and np.array_equal(self.s13, self.s13L)): print "Exiting: Different Dalitz coordinates in files:" print " KS: {}".format(KS_amplitude_file) print " KL: {}".format(KL_amplitude_file) for i, x in enumerate(self.s12): if self.s12L[i] != x: print i, self.s12[i], self.s12L[i], self.s13[i], self.s13L[i] sys.exit(-1) # Define the amplitudes for a D0 and D0bar decay # Here it is assumed that the input A_KS is actually for K1, the CP-even state. It is A(K1 -> D0 h+h-) # and it is assumed that the input A_KL is actually for K2, the CP-odd state. It is A(K2 -> D0 h+h-) # The eps appearing here is due to EFFECT 1: the NON-EXACT MIRROR ASYMMETRY of the D(bar)hh amplitude # (EFFECT 2 being the presence of direct KL->pipi) A_KS_D0 = self.A_KS - self.amplitude_asym_factor*self.eps*self.A_KL # KS in K1, K2 basis A_KL_D0 = self.A_KL - self.amplitude_asym_factor*self.eps*self.A_KS # KL in K1, K2 basis A_KS_D0bar = self.A_KS.transpose() + self.amplitude_asym_factor*self.eps*self.A_KL.transpose() # Use CP sign when transposing for conjugate A_KL_D0bar = -self.A_KL.transpose() - self.amplitude_asym_factor*self.eps*self.A_KS.transpose() # The 2D dalitz coordinate dependent self.A_D0 = Amplitude(A_KS_D0 , self.s12, self.s13, self.KL_to_pipi_factor*A_KL_D0 , self.eps, self.parameters) self.A_D0bar = Amplitude(A_KS_D0bar, self.s12, self.s13, self.KL_to_pipi_factor*A_KL_D0bar, self.eps, self.parameters) # delete the cached predicted yields, as they may change self.predicted_yields = {}
def test_AlignmentArray(): F = UtilityFunctions.FastaToDict("tests/test_fasta.fasta") ali = UtilityFunctions.AlignmentArray(F.values()) comp = [['A', 'C', 'G', 'T', 'T', "-"], ['A', 'A', 'C', 'T'], ['C', 'T', 'A', 'G']] comp = np.array(comp) assert np.array_equal(comp, ali)
def compare(): data = uf.ReadData('data_movie5', False) y = np.array(data.cm) t = np.array(data.time) N = len(y) Ts = 0.02 dataTruth = uf.ReadData('truth_movie5', False) s_groundtruth = np.array(dataTruth.cm) s_groundtruth = s_groundtruth + 0.6 v = np.diff(y)/Ts a = np.diff(v)/Ts v = np.insert(v, 0, 0) a = np.insert(a, 0, 0) a = np.insert(a, 0, 0) #y = uf.preFilter(y) #y = uf.preFilter2(y) x, P = kalman.filter(y, N, Ts) outdata = pd.DataFrame(columns=['time', 'truth', 'sensor', 'kalman']) for i in range(len(t)): outdata = outdata.append({'time': np.round(t[i] * 1000), 'truth': np.round(s_groundtruth[i], 4), 'sensor': np.round(y[i], 4), 'kalman': np.round(x[i, 0], 4)}, ignore_index = True) outdata.to_csv('kalman_results_movie5.csv', index=False, sep=';') uf.plot_filterresult(t, y, s_groundtruth, v, a, x, P)
def __init__(self, KS_amplitude_file, efficiency=None, binning_file="input/KsPiPi_optimal.pickle"): A_KS, s12, s13 = uf.load_amplitude(KS_amplitude_file) self.amplitude = Amplitude(A_KS, s12, s13) self.s12 = s12 self.s13 = s13 bin_def, bin_def_s12, bin_def_s13 = uf.load_binning(binning_file) self.binning = Binning(bin_def, bin_def_s12, bin_def_s13, s12, s13) if efficiency == None: efficiency = DefaultEfficiency(s12, s13) self.efficiency = efficiency self.Fi, self.ci, self.si = self.calculate_Fi_ci_si() self.Fi_inv = np.flip(self.Fi, 0) self.sqrt_Fi_Fi_inv = np.sqrt(self.Fi * self.Fi_inv) self.bin_num = self.binning.get_number_of_bins() self.channel_num = 1 # only fit one Dh channel return
def EigenvectorCentralityExperiment(G, min_target, max_target, filename): print nx.info(G) print 'average eigenvector centrality: ', UF.average_eigenvector_centrality(G) X_eigenvector_centrality = [] Y_nD = [] target = min_target while target <= max_target: copyG = G.copy() new_G = SimulatedAnnealing(copyG, target, eigenvector_centrality_cost_function) eigenvector_centrality = UF.average_eigenvector_centrality(new_G) nD = SCT.controllability(new_G) X_eigenvector_centrality.append(eigenvector_centrality) Y_nD.append(nD) print "target = ", target, " EC = ", eigenvector_centrality, 'nD = ', nD target += 0.01 s = 'results/' + filename with open(s, "w") as f: for i in range(len(Y_nD)): print >> f, "%f %f"%(X_eigenvector_centrality[i], Y_nD[i]) return (X_eigenvector_centrality, Y_nD)
def acceptCategory(): UtilityFunctions.cls() if (len(userPurchasedItems) > 0): printUserPurchasedItems(userPurchasedItems) print("*" * 50) categorynum = 1 categoryList = [] # print("-" * 50) print("Select Category of Item to Purchase") for category in inventory: print(str(categorynum) + ". " + category) categoryList.append(category) categorynum = categorynum + 1 print("0. Quit") selectedcategorynum = -1 while True: invalidInput = False try: selectedcategorynum = int(input("Input : ")) except Exception: invalidInput = True if (selectedcategorynum >= categorynum): invalidInput = True if (invalidInput): print("Invalid selection") else: break if (selectedcategorynum == 0): selectedcategory = "0" else: selectedcategory = categoryList[selectedcategorynum - 1] return selectedcategory
def fine_tune_binary_models(x_train, y_train, x_val, y_val, df_val_predictions, pca, params_gradBoost_intial, params_NN_initial): """Calls upon various grid-search functions to fine-tune the previous model iterations of Gradient boosted trees and the neural network""" x_train_processed = process_data_existingPCA(x_train, pca) x_val_processed = process_data_existingPCA(x_val, pca) # obtain the fine-tuned parameters for GB Trees and NN's param_grid_GradBoost, param_grid_nn = calulate_fine_tuned_grid_parameters( params_NN_initial, params_gradBoost_intial) # perform fine-tuned grid search for GB Trees grid_search_gradBoost = GradientBoostedTreesGridSearch( x_train_processed, y_train, param_grid_GradBoost) UtilityFunctions.model_results(grid_search_gradBoost, x_val_processed, y_val, df_val_predictions, 'GrBoost - finetuned') # perform fine-tuned grid search for Neural Nets grid_search_NN = NeuralNetworkGridSearch(x_train_processed, y_train, param_grid_nn) UtilityFunctions.model_results(grid_search_NN, x_val_processed, y_val, df_val_predictions, 'NN - finetuned') # print validation set results print("\n\n***** Predictions from fine-tuned models on validation set") print(df_val_predictions) return grid_search_gradBoost, grid_search_NN
def __init__( self, generator_models=None, # Model used to generate yields (with efficiency/mat interactions set, etc) param_sets=None, # A Nx3 or Nx5 with physics param to use, angles in RAD N_signal=[14000, 14000 / 0.075], KL_amplitude_files=[], # Can specify more than one KL-amplitude file empty=False): if empty: return if generator_models == None: raise ValueError( "You MUST supply list od generator models to non-empty BiasStudy" ) if param_sets == []: raise ValueError( "You MUST supply param_sets to non-empty BiasStudy") self.no_printed_warning = True self.generator_models = generator_models self.param_sets = param_sets self.param_sets_deg = [] p_dicts = [] for p in param_sets: p_deg = [] p_dict = {} p_dict['g_val'] = p[0] p_dict['g_val_deg'] = uf.rad_to_deg(p[0]) p_dict['r_val'] = p[1] p_dict['d_val'] = p[2] p_dict['d_val_deg'] = uf.rad_to_deg(p[2]) p_dicts.append(p_dict) self.param = pd.DataFrame(p_dicts) self.yields = pd.DataFrame(columns=[ 'param_index', 'gen_model_index', 'KL_amp_index', 'yield_set' ]) self.fits = pd.DataFrame(columns=[ 'param_index', 'gen_model_index', 'KL_amp_index', 'type', 'result', 'success' ]) self.results = pd.DataFrame(columns=[ 'param_index', 'gen_model_index', 'KL_amp_index', 'param', 'val', 'bias', 'uncertainty' ]) self.N_signal = N_signal self.KL_amplitude_files = KL_amplitude_files if KL_amplitude_files == []: self.KL_amplitude_files = [generator_model.KL_amplitude_file] self.yields_init = False
def test_reverseComplementAlignment(): F = UtilityFunctions.FastaToDict("tests/test_data/test_fasta.fasta") ali = UtilityFunctions.AlignmentArray(F.values()) rcomp = [['-', 'A', 'A', 'C', 'G', 'T'], ['A', 'G', 'T', 'T'], ['C', 'T', 'A', 'G']] rcomp = np.array(rcomp) assert np.array_equal(UtilityFunctions.reverseComplementAlignment(ali), rcomp)
def updateUI(self): self.applyAddressButton.setDisabled(self.isCorrectAddress) self.sharingCoreLabel.field.setReadOnly(self.isCorrectAddress) self.pathButton.setDisabled(not self.isCorrectAddress) self.pathLabel.field.setDisabled(not self.isCorrectAddress) self.sendButton.setDisabled(not self.isCorrectAddress) UF.debugOutput('successfully updated UI of send form')
def alignWithRef(currentD, reference_dict, pD, outdir): for i in currentD: query_seq = currentD[i]['consensus'] query_ali = currentD[i]['alignment'] query_names = currentD[i]['names'] q_matrix, nt_inds = Consensus.makeAlignmentMatrix(query_ali) candidates = UtilityFunctions.FastaToDict(reference_dict) for target_nam, target_seq in candidates.items(): qm = copy.copy(q_matrix) target_ali = UtilityFunctions.AlignmentArray([target_seq]) # Align the query and target consensus sequences result = Alignment.SWalign(query_seq, target_seq, pD, useSub=True) # Check the if the consensus sequences are a good match is_match = Alignment.alignmentMeetsCriteria(result, query_seq, target_seq, pD) if is_match[0]: result['alignment'] = is_match[1] # get the full alignment for the two consensus sequences result = Alignment.getAlignmentFull(result, query_seq, target_seq, pD) ali, matrix = Consensus.expandAlignment(result, query_ali, target_ali, qm, nt_inds) cons = Consensus.collapseAlignment(matrix, nt_inds) names = query_names + [target_nam] result2 = Alignment.SWalign(query_seq, cons, pD, useSub=True) is_match_2 = Alignment.alignmentMeetsCriteria(result2, query_seq, cons, pD) result2['alignment'] = is_match_2[1] result2 = Alignment.getAlignmentFull(result2, query_seq, cons, pD) a2 = UtilityFunctions.AlignmentArray([query_seq]) ali, matrix = Consensus.expandAlignment(result2, a2, ali, qm, nt_inds) names = ["*consensus_%s" % i] + names path = "%s/final_clusters/consensus_%s_to_%s_ali.fasta" % ( outdir, i, target_nam.split(" ")[0]) out = open(path, "w") for j, nam in enumerate(names): out.write(">%s\n%s\n" % (nam, "".join(list(ali[j])))) out.close()
def test_lengthFromCIGAR(): cigar1 = '6M22X2Y12D6I' assert UtilityFunctions.lengthFromCIGAR(cigar1) == 48 assert UtilityFunctions.lengthFromCIGAR(cigar1, excludeI=True) == 18 assert UtilityFunctions.lengthFromCIGAR(cigar1, excludeD=True) == 12 assert UtilityFunctions.lengthFromCIGAR(cigar1, excludeI=True, excludeD=True) == 6 assert UtilityFunctions.lengthFromCIGAR(cigar1, mOnly=True) == 6
def fit_from_result( self, fit_result, start_guess=[uf.deg_to_rad(75.), 0.1, uf.deg_to_rad(130.)]): xy_vector = np.array(fit_result.x[0:4]) # xm ym xp yp xy_cov_mat = np.array(fit_result.cov_mat[0:4, 0:4]) # covariance matrix return self.fit(xy_vector, xy_cov_mat, start_guess)
def save(self): try: fileEntry = open(GB.RES_DB_SETTINGS, 'w+') fileEntry.write(GB.savePath) except Exception as e: UtilityFunctions.debugOutput('failed to read from settings.txt. stack:', e) return finally: UtilityFunctions.debugOutput('successfully loaded settings')
def __init__(self): super().__init__() self.upperLabel = QLabel(parent=self, text='made with love.\nby Lunar') self.rightlabel = QLabel(parent=self, text=' version: ' + GB.VERSION + '\nLicenced with GNU GPL v3') self.picLabelOSLogo = QLabel(self) self.picLabelLunarLogo = QLabel(self) self.initUI() UF.debugOutput('successfully created About Form')
def generate(self, **kargs): for _ in range(50): pa_size_bits = RandomUtils.random32(1, 20) pa_size = 2**pa_size_bits pa_align_bits = RandomUtils.random32(1, pa_size_bits) pa_align = 2**pa_align_bits amo_mem_attr = self.choice( ("AMONone", "AMOSwap", "AMOLogical", "AMOArithmetic")) coherency_mem_attr = self.choice( ("CoherentL1", "CoherentL2", "CoherentL3", "Incoherent")) idempotency_mem_attr = self.choice( ("ReadIdempotent", "ReadNonIdempotent")) arch_mem_attributes = "%s,%s,%s" % ( amo_mem_attr, coherency_mem_attr, idempotency_mem_attr, ) impl_mem_attributes = self.choice( ("Debug", "CLINT", "PLIC", "GPIO")) # Constrain the PA to be in the valid VA range, so flat mapping doesn't fail pa_range = "0x0-0x7fffffffffff" if VirtualMemory.getPagingMode() == EPagingMode.Sv39: pa_range = "0x0-0x3fffffffff" start_addr = self.genPA( Size=pa_size, Align=pa_align, Type="D", MemAttrArch=arch_mem_attributes, MemAttrImpl=impl_mem_attributes, Range=pa_range, ) end_addr = start_addr + pa_size - 1 self._recordMemoryAttributes(arch_mem_attributes, start_addr, end_addr) self._recordMemoryAttributes(impl_mem_attributes, start_addr, end_addr) pa_for_va = RandomUtils.random64(start_addr, end_addr) va_size = RandomUtils.random32(1, (end_addr - pa_for_va + 1)) va_align_bits = RandomUtils.random32(0, (va_size.bit_length() - 1)) va_align = 2**va_align_bits pa_for_va = UtilityFunctions.getAlignedValue(pa_for_va, va_align) va_size = UtilityFunctions.getAlignedValue(va_size, va_align) self.genVAforPA( PA=pa_for_va, Size=va_size, Type="D", ) self._verifyMemoryAttributes()
def onClick(self): UF.debugOutput('click button:', self.sender().action) try: if self.sender().action == 'download': self.receiveForm.show() if self.sender().action == 'upload': self.sendForm.show() if self.sender().action == 'settings': self.settingsForm.show() except Exception: UF.debugOutput(sys.exc_info())
def onClick(self): UF.debugOutput('click:', self.sender().action) if self.sender().action == 'browse': path = str( QFileDialog.getExistingDirectory(self, "Select Directory")) if path: self.savePath = path UF.debugOutput('set output location to ', self.savePath) self.updateUI() if self.sender().action == 'ready': self.readyFlag = True self.updateUI()
def fit_observables(self, fit_model, channel_num, name="default", quiet=False): nf = len(self.KL_amplitude_files) npar = len(self.param_sets) total_fits = nf * npar n_xy = 6 if channel_num == 1: n_xy = 4 self.results["{}-fit-success".format(name)] = np.zeros((nf, npar)) self.results["{}-xy-val".format(name)] = np.zeros((nf, npar, n_xy)) self.results["{}-xy-bias".format(name)] = np.zeros((nf, npar, n_xy)) self.results["{}-xy-res".format(name)] = {} fit_n = 0 print "Fitting yields for observables for :", name print " Fit:", fit_n for fi, f in enumerate(self.KL_amplitude_files): for pi, p in enumerate(self.param_sets): if channel_num == 1: input_xy = uf.get_xy(p[0:3]) avg_yields = self.yields["{}_{}".format(fi, pi)][0:32] else: if len(p) < 5: raise ValueError( "Cannot fit with 2 channel Model, only generated 1 channel yields" ) input_xy = uf.get_xy_xi(p) avg_yields = self.yields["{}_{}".format(fi, pi)] sys.stdout.write( "\033[F\033[K") #back to previous line, clear line fit_n += 1 if not quiet: print(" Fit: {}/{}".format(fit_n, total_fits)) # Yields calculated using FullModel.py direct_result = fit_model.fit(avg_yields, quiet=True) self.results["{}-xy-res".format(name)]["{}_{}".format( fi, pi)] = direct_result self.results["{}-xy-val".format(name)][ fi, pi, :] = direct_result.x[0:n_xy] self.results["{}-xy-bias".format(name)][fi, pi, :] = np.array( direct_result.x[0:n_xy]) - np.array(input_xy) self.results["{}-fit-success".format(name)][ fi, pi] = direct_result.success return
def check_outliers(trian_df, test_df, save_to_folder=None): #join train and test data sets for better visualization of outliers combined_df = trian_df.append(test_df) print('\nJoined train rows {} with test rows {}. New total {}\n'.format( trian_df.shape[0], test_df.shape[0], combined_df.shape[0])) print('\nBefore replacing outliers.\n') plot1 = utils.plot_feature_distribution(combined_df, 'DAYS_EMPLOYED') if (save_to_folder != None): plot1.savefig(save_to_folder + '\\DAYS_EMPLOYED_distribution.png', bbox_inches='tight') plot1.show() plot2 = utils.plot_feature_distribution(combined_df, 'AMT_INCOME_TOTAL') if (save_to_folder != None): plot2.savefig(save_to_folder + '\\AMT_INCOME_TOTAL_distribution.png', bbox_inches='tight') plot2.show() plot1.close() plot2.close() combined_df = utils.replace_outliers(combined_df) print('\nAfter replacing outliers.\n') plot1 = utils.plot_feature_distribution(combined_df, 'DAYS_EMPLOYED') if (save_to_folder != None): plot1.savefig(save_to_folder + '\\DAYS_EMPLOYED_corrected_distribution.png', bbox_inches='tight') plot1.show() plot2 = utils.plot_feature_distribution(combined_df, 'AMT_INCOME_TOTAL') if (save_to_folder != None): plot2.savefig(save_to_folder + '\\AMT_INCOME_TOTAL_corrected_distribution.png', bbox_inches='tight') plot2.show() plot1.close() plot2.close() del combined_df
def AddScoreToTable(row, compId, eventId, SportsSession): global Log playerName = row[TournamentColumnConstants.PLAYER] playerQuery = SportsSession.query(Player).filter(Player.name == playerName) if playerQuery.count() == 0: Log.log(logging.DEBUG, "Adding score: Player {0} does not exist in table".format(playerName)) return playerId = playerQuery.all()[0].id scoreQuery = SportsSession.query(Score).filter(Score.playerID == playerId, Score.eventID == eventId, Score.competitionLevelID == compId).count() if(scoreQuery > 0): Log.log(logging.DEBUG, "Adding score: Score already exists for playerId {0} eventId {1} and competitionLevel {2}".format(playerId, eventId, compId)) return SportsSession.add(Score(playerID = playerId, eventID = eventId, competitionLevelID = compId,\ R1 = UtilityFunctions.GetColumnValuesInRow(TournamentColumnConstants.R1, row),\ R2 = UtilityFunctions.GetColumnValuesInRow(TournamentColumnConstants.R2, row),\ R3 = UtilityFunctions.GetColumnValuesInRow(TournamentColumnConstants.R3, row),\ R4 = UtilityFunctions.GetColumnValuesInRow(TournamentColumnConstants.R4, row),\ R5 = UtilityFunctions.GetColumnValuesInRow(TournamentColumnConstants.R5, row),\ total = UtilityFunctions.GetColumnValuesInRow(TournamentColumnConstants.TOTAL, row),\ toPar = str(UtilityFunctions.GetColumnValuesInRow(TournamentColumnConstants.TOPAR, row)),\ pos = str(UtilityFunctions.GetColumnValuesInRow(TournamentColumnConstants.POS, row))))\ print "SportsSession has successfully added scores {0}".format(playerName) return None
def logisitic_regression_initial_benchmark(df_val_predictions, x_train_processed, x_val_processed, y_train, y_val): """Performs regularised logistic regression""" logReg = sklearn.linear_model.LogisticRegression() logReg.fit(x_train_processed, y_train) print("\n***** Outcomes for logistic regression") print("\nLog Regression training set accuracy", logReg.score(x_train_processed, y_train)) print("\nLog Regression validation set accuracy", logReg.score(x_train_processed, y_train)) UtilityFunctions.model_results(logReg, x_val_processed, y_val, df_val_predictions, 'Log-Reg') return logReg
def updateUI(self): self.pathLabel.field.setText(self.savePath) if self.readyFlag: self.readyButton.setText('ready to connect!') while True: if self.receiveFile(): UF.okDialog('Successfully received the file.') break else: if not UF.okDialog( 'Failed to receive the file. Press ok to try again.' ): break else: self.readyButton.setText('be ready!')
def autoPlayHangman(group): #an AI player will play hangman word = group[0] sortedChars = buildStrategy(word) #the list of letters the AI will guess, with more common letters coming first index = 0 #index will increase as we move through sortedChars numtries = group[1] print "AI is allowed " + str(numtries) + " incorrect guesses" wrongcount = 0 wordnospaces = word.replace(' ', '') wordnolines = wordnospaces.replace('\n', '') wordset = set(wordnolines) guesswordset = set('') allwordsguessed = set('') letterset = set('ABCDEFGHIJKLMOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz') print '_'*len(word) input = sortedChars[index][0] #print ("AI guessing: " + input +"\n") guess = set(input) while wrongcount < numtries and guesswordset != wordset: print ("AI guessing: " + input) if guess.issubset(letterset) == False or len(guess) != 1: input = raw_input('Letters only. Guess a letter: ') guess = set(input) elif guess.issubset(wordset) == False: wrongcount += 1 if wrongcount == numtries: print 'AI is hung! Word was: ' + word + '.\n' return UtilityFunctions.give_information(input, allwordsguessed, word, guesswordset, numtries, wrongcount) print("AI was wrong. AI will guess another letter\n") index+=1 if index == len(sortedChars): #if print 'AI is stumped! Word was: ' + word + '.\n' return input = sortedChars[index][0] guess = set(input) elif guess.issubset(wordset): guesswordset.add(input) UtilityFunctions.give_information(input, allwordsguessed, word, guesswordset, numtries, wrongcount) if guesswordset == wordset: print "AI has beaten hangman! To play again, chose a file\n" return print("AI guessed correctly. AI will guess another letter\n") index +=1 if index == len(sortedChars): #if print 'AI is stumped! Word was: ' + word + '.\n' return input = sortedChars[index][0] guess = set(input)
def makeAlignmentMatrix(alignment): ''' Make a matrix which will be used to record the details of the current alignment. Each row is a nucleotide (including IUPAC nts) and each column is a position in the alignment. The cells are the number of each nt in this column. This can then be used to make the consensus sequence. ''' # import the IUPAC nts D1, D2 = UtilityFunctions.IUPAC() nts = list(D1.keys()) + ["-"] # make a dictionary to show which row in the matrix corresponds with # each nt nt_inds = dict((n, i) for i, n in enumerate(nts)) # create an empty matrix with one row, plus one column for each nt in # the sequence sequence = alignment[0, :] alignment_tab = np.zeros((len(nts), len(sequence))) # fill in the matrix with the nts in the first sequence for i in range(np.shape(alignment)[0]): sequence = alignment[i, :] for col, char in enumerate(sequence): nt_ind = nt_inds[char] alignment_tab[nt_ind, col] += 1 return (alignment_tab, nt_inds)
def collapseAlignment(matrix, nt_inds): D1, D2 = UtilityFunctions.IUPAC() Z = sorted(zip(nt_inds.keys(), nt_inds.values()), key=lambda x: x[1]) nt_keys = np.array([z[0] for z in Z])[:-1] nt_vals = np.array([z[1] for z in Z])[:-1] C = [] for column in matrix.T: column = column[nt_vals] maxi = column == max(column) if sum(maxi) == 0 or sum(column) == 0: C.append("-") elif sum(maxi) == 1: C.append(nt_keys[maxi][0]) else: maxi_nt = nt_keys[maxi] maxi_string = "".join(list(maxi_nt)) if maxi_string not in D2: if maxi_string in ['A', 'C', 'G', 'T']: C.append(maxi_string) else: C.append("N") else: # there is >1 - use an IUPAC character iupac = D2[maxi_string] C.append(iupac) cons = "".join(C) return (cons)
def myLoadImage(): dlg = tkFileDialog.Open(filetypes=[('image files', '*.jpg'), ('All files', '*')]) fl = dlg.show() if fl != '': print fl imageCanvas.delete('all') fls = unicodedata.normalize('NFKD', fl).encode('ascii', 'ignore') img[0] = Image.open(fl) img[0] = img[0].resize((600, 600), Image.ANTIALIAS) tkimg[0] = ImageTk.PhotoImage(img[0]) imageCanvas.create_image((0, 0), anchor=NW, image=tkimg[0]) for i in range(21): # Number of distortion pictures created par = np.float64(i - 10) / 10 tempImage = UF.myDistort(fls, par) tempImage = tempImage[:, :, ::-1] tempFileName = 'tempFile.jpg' cv2.imwrite(tempFileName, tempImage) cvimgTemp[i] = tempImage.copy() imgTemp = Image.open(tempFileName) imgTemp = imgTemp.resize((600, 600), Image.ANTIALIAS) tkimgTemp[i] = ImageTk.PhotoImage(imgTemp) print 'Finished image with param: ' + str(par) UF.os.remove('tempFile.jpg') tkMessageBox.showinfo('Loading Image Process', 'Loading Cache Memory: \n STATUS - DONE')
def executeHangman(): #the main method, runs the game while 1 > 0: #loops the game infinitely unless the user wants to quit file = csv.reader(open("words.csv", "rU"), dialect=csv.excel_tab) newlist = UtilityFunctions.formatHangmanData(file) word = UtilityFunctions.create_word(newlist) whichMode = int(raw_input("User or AI game? (0 for User, 1 for AI) ")) #0 for user, 1 for AI if whichMode == 0: playHangman(word) cont = raw_input('Type y to play again. Type n to quit\n ') if (cont == 'n'): return elif whichMode == 1: autoPlayHangman(word) cont = raw_input('Type y to play again. Type n to quit\n ') if (cont == 'n'): return word = UtilityFunctions.create_word(newlist)
def CombinationBetweennessClosenessExperiments(G, min_target, step, max_target, filename): print nx.info(G) print "Combination Indicator X :", UF.closeness_betweenness_combination_centrality(G) X_centrality = [] Y_nD = [] target = min_target while target <= max_target: copyG = G.copy() new_G = SimulatedAnnealing(G, target, combination_betweenness_closeness_centrality_cost_function) centrality = UF.closeness_betweenness_combination_centrality(new_G) nD = SCT.controllability(G) X_centrality.append(centrality) Y_nD.append(nD) print 'target = ', target, "X = ", centrality, "nD = ", nD target += step s = 'results/' + filename with open(s, "w") as f: for i in range(len(Y_nD)): print >> f, "%f %f"%(X_centrality[i], Y_nD[i]) return (X_centrality, Y_nD)
### NW Networks #n = 100 #for k in range(2, 8, 2): # for p in [0.1, 0.2, 0.3]: # G = NM.directed_newman_watts_strogatz_graph(n, k, p) # filename = "NW_CombinationX_n%dK%dp%f"%(n,k,p) # CombinationBetweennessClosenessExperiments(G, 0.05, 0.05, 0.99, filename) ## BA Networks #n = 100 #for m in range(2, 6): # G = NM.directed_barabasi_albert_graph(n, m) # filename = "BA_CombinationX_n%dm%d"%(n, m) # CombinationBetweennessClosenessExperiments(G, 0.05, 0.05, 0.99, filename) n = 1000; m = 3; G = NM.directed_barabasi_albert_graph(n, m) combine = UF.closeness_betweenness_combination_centrality(G) nD = SCT.controllability(G) print '************************ INIT INFO ************************\n' print nx.info(G) print 'average X: ', combine print 'Init ND:', nD print '************************ INIT INFO ************************\n' target = 0.15 new_G = SimulatedAnnealing(G, target, betweenness_centrality_cost_function) combine = UF.closeness_betweenness_combination_centrality(new_G) nD = SCT.controllability(new_G)
def closeness_centrality_cost_function(G, target): return math.fabs(UF.average_closeness_centrality(G) - target)
def eigenvector_centrality_cost_function(G, target): return math.fabs(UF.average_eigenvector_centrality(G) - target)
def combination_betweenness_closeness_centrality_cost_function(G, target): return math.fabs(UF.closeness_betweenness_combination_centrality(G) - target)
babyRecFlag = True while(runEpisode == 1): ## Reset / Update episodeTime = episodeTime + 1 simulationTime = simulationTime + 1 motherWrist = [] motherShoulder = [] motherElbow = [] motherHead = [] babyWrist = [] babyShoulder = [] babyElbow = [] babyHead = [] ## Receive / Parse message animMessage = win32file.ReadFile(p, 4096)[1] uf.parseMessage(animMessage, motherWrist, motherShoulder, motherElbow, motherHead, babyWrist, babyShoulder, babyElbow, babyHead) uf.writeReceivedCoordinatesToFile(babyFile, motherWrist, motherShoulder, motherElbow, motherHead, babyWrist, babyShoulder, babyElbow, babyHead, simulationTime) ## Manages decisions if(babyEpisodeStatus == 'TO_START'): messageToSend = 'INIT ' babyEpisodeStatus = 'INITIALIZED' print "BABY TO_START" elif(babyEpisodeStatus == 'INITIALIZED'): print "BABY INITIALIZED" #makes so baby always must walk at least a little if(motherHead[0] - babyHead[0] > GESTURE_THRESHOLD): babyEpisodeStatus = 'WALK' messageToSend = 'DO_NOTHING' moWristInit = np.array([motherWrist[0],motherWrist[1],motherWrist[2]]) elif(babyEpisodeStatus == 'WALK'):
prevHead = [0,0,0] while(runEpisode == 1): ## Reset / Update episodeTime = episodeTime + 1 simulationTime = simulationTime + 1 motherWrist = [] motherShoulder = [] motherElbow = [] motherHead = [] babyWrist = [] babyShoulder = [] babyElbow = [] babyHead = [] ## Receive / Parse message animMessage = win32file.ReadFile(p, 4096)[1] uf.parseMessage(animMessage, motherWrist, motherShoulder, motherElbow, motherHead, babyWrist, babyShoulder, babyElbow, babyHead) uf.writeReceivedCoordinatesToFile(motherFile, motherWrist, motherShoulder, motherElbow, motherHead, babyWrist, babyShoulder, babyElbow, babyHead, simulationTime) if(motherEpisodeStatus == 'TO_START'): messageToSend = 'INIT ' motherEpisodeStatus = 'INITIALIZED' print "Mother TO_START" elif(motherEpisodeStatus == 'INITIALIZED'): messageToSend = 'DO_NOTHING' motherEpisodeStatus = 'WATCHING' babyReachTarget = motherWrist print "Mother INITIALIZED" elif(motherEpisodeStatus == 'WATCHING'): diffHead = map(operator.sub, babyHead, prevHead) if(diffHead[0] != 0 or diffHead[1] != 0 or diffHead[2] != 0): prevHead = babyHead
def betweenness_centrality_cost_function(G, target): return math.fabs(UF.average_betweenness_centrality(G) - target)
N_rm = int(N * remove_fraction) nodes = nx.nodes(G) print "Before Removing:\n" print "N = ", N print "L = ", L random.shuffle(nodes) rm_nodes = nodes[0:N_rm] G.remove_nodes_from(rm_nodes) print "\n after removing:" print "N = ", G.number_of_nodes() print "L = ", G.number_of_edges() print "<k> = ", float(2 * G.number_of_edges()) / G.number_of_nodes() avg_deg = UF.average_degree(G); print "<k>: ", avg_deg; avg_bet = UF.average_betweenness_centrality(G); print "<B>: ", avg_bet; #APL = nx.average_shortest_path_length(G) #print "APL: ", APL N_core = nx.core_number(G) core_values = N_core.values() leaves = [x for x in core_values if x <= 1] print "#leaves:", len(leaves) print "#core:", G.number_of_nodes() - len(leaves) print "#core/#N:", (G.number_of_nodes() - len(leaves))/float(G.number_of_nodes())
episodeTime = 0 babyFile = open('trainer_babyTrajectory_Dummy.txt', 'w') while(runSimulation == 1): motherWrist = [] motherShoulder = [] motherElbow = [] motherHead = [] babyWrist = [] babyShoulder = [] babyElbow = [] babyHead = [] animMessage = win32file.ReadFile(p, 4096)[1] uf.parseMessage(animMessage, motherWrist, motherShoulder, motherElbow, motherHead, babyWrist, babyShoulder, babyElbow, babyHead) uf.writeReceivedCoordinatesToFile(babyFile, motherWrist, motherShoulder, motherElbow, motherHead, babyWrist, babyShoulder, babyElbow, babyHead, episodeTime) episodeTime = episodeTime + 1 if (simulationStatus == 'TRAIN'): messageToSend = 'MOVE_HEAD ' + str(BABY_INITIAL_POSITION[0]) + ' ' + str(BABY_INITIAL_POSITION[1]) + ' ' + str(BABY_INITIAL_POSITION[2]) + ' ' + str(episodeTime) simulationStatus = 'INITIALIZED' print "Initialized baby to initial position" elif (simulationStatus == 'INITIALIZED'): babyReachTarget = babyHead babyReachTarget = np.array(babyReachTarget) babyReachTarget[2] = babyReachTarget[2] + 2 messageToSend = 'DO_NOTHING' simulationStatus = 'REACH' elif(simulationStatus == 'REACH'):