def main(): # configurations filename = config.DP_CONFIG["filename"] filedir = config.DP_CONFIG["filedir"] outputdir = config.DP_CONFIG["output_file_directory"] categorical_columns = config.DP_CONFIG["categorical_columns"] epsilon_list = config.DP_CONFIG["epsilon_list"] label_filename = config.DP_CONFIG["label_filename"] train_size = config.DP_CONFIG["train_size"] epoch = config.DP_CONFIG["epoch"] original = Original(filedir, filename, categorical_columns) label = Label(filedir, label_filename, original) ## DP class diffprivacy = DiffPrivacy(original, outputdir) # multiprocessing DP print("Get DP data...") pool = Pool() pool.map(diffprivacy.dp, epsilon_list) # Validation print("Start Get Metric...") categorical_results = [] continuous_results = [] model_results = [] for epsilon in tqdm([0, *epsilon_list]): if epsilon == 0: validation = Validation(epsilon, original, original, label, train_size, epoch) validation.process() model_results.extend(validation.model_result) continue dp = Data(epsilon, outputdir) validation = Validation(epsilon, original, dp, label, train_size, epoch) validation.process() categorical_results.append(validation.category_result) continuous_results.append(validation.continuous_result) print(validation.model_result) model_results.extend(validation.model_result) write_pkl_file(categorical_results, outputdir, "categorical_results.pkl") write_pkl_file(continuous_results, outputdir, "continuous_results.pkl") write_pkl_file(model_results, outputdir, "model_results.pkl")
def validate(datadir=os.getcwd()): """ Ensure data files are well formatted and consistent. """ indexer = Indexer(datadir) indexer.load() indexer.index() validator = Validation(indexer) validator.validate()
def test_NewYear_YearOne_YearIncreasing(self): my_validation=Validation() my_service=Service(my_validation) self.assertEqual(my_service.get_year(),1) my_service.set_units_feed(2000) my_service.new_year() self.assertEqual(my_service.get_year(),2)
def __isDirAValidSubject(self, source): """Verify if the directory source may be consider a valid subject Args: source: an input directory returns: True if the current directory qualify as a toad subject, False otherwise """ result = False dir = os.path.abspath(source) if os.path.isdir(dir): if self.config.getboolean('arguments', 'validation'): if Validation(dir, self.getLogger(), self.__copyConfig(dir)).run(): self.info( "{} is a valid subject, adding it to the list.".format( os.path.basename(dir))) result = True else: self.warning("{} will be discarded".format( os.path.basename(dir))) else: self.warning( "Skipping validation have been requested, this option is dangerous" ) result = True else: self.warning( "{} doesn't look a directory, it will be discarded".format( dir)) return result
def test_GrainstocksAfterRatInfestation_RatInfestation_NewGrainstocks(self): my_validation=Validation() my_service=Service(my_validation) previous_grainstock=my_service.get_grain_stocks() my_service.set_rats_ate(100) my_service.grainstocks_after_rat_infestation() self.assertLess(my_service.get_grain_stocks(), previous_grainstock)
def __init__(self): self.flt_dao = FlightDAO() self.validator = Validation() self.flight_id = tk.IntVar() self.airline_name = tk.StringVar() self.from_location = tk.StringVar() self.to_location = tk.StringVar() self.depature_time = tk.IntVar() self.arrival_time = tk.IntVar() self.duration = tk.IntVar() self.total_seats = tk.IntVar() self.flight_price = tk.IntVar() # List of employee ids - lb for listbox self.lb_ids = None # Messagebox title self.mb_title_bar = "Employee CRUD" pass
def setUp(self): dao = AccountDao hasher = Hash dao.get_password = lambda self, user_id: "1111" hasher.get_hash_result = lambda self, password: "******" self.to_test = Validation(dao, hasher)
def train(param, model): # Launch the graph with tf.Session() as sess: sess.run(model.init) step = 0 # Keep training until reach max iterations while step < param.training_iters: #generate train data---total data batch_x = param.x_train batch_y = param.y_train batch_x = batch_x.reshape( (param.batch_size, param.n_steps, param.n_input)) batch_y = np.reshape(batch_y, (-1, param.n_classes)) fd = {model.xx: batch_x, model.y: batch_y} # Run optimization op (backprop) sess.run(model.optimizer, feed_dict=fd) #get the gradient or residual grads_wrt_input = sess.run(tf.gradients(model.cost, model.x), feed_dict=fd) residual = sess.run(tf.concat(1, grads_wrt_input)) print(residual[0, :]) if step % param.display_step == 0: # Calculate batch accuracy acc = sess.run(model.accuracy, feed_dict=fd) # Calculate batch loss loss = sess.run(model.cost, feed_dict=fd) print("Iter " + str(step) + ", Minibatch Loss= " + \ "{:.6f}".format(loss) + ", Training Accuracy= " + \ "{:.5f}".format(acc)) step += 1 print("Optimization Finished!") #test data test_data = param.x_test.reshape( (len(param.x_test), param.n_steps, param.n_input)) test_label = np.reshape(param.y_test, (-1, 2)) fd = {model.xx: test_data, model.y: test_label} loss = sess.run(model.cost, feed_dict=fd) print("{:.6f}".format(loss)) print("Testing Accuracy:", \ sess.run(accuracy, feed_dict=fd)) #get the predict classifier and probability value classifier_value = sess.run([classifier_collection], feed_dict={model.xx: test_data}) proba_value = sess.run([proba_collection], feed_dict={model.xx: test_data}) v = Validation() v.calculateF1(param.label_value, classifier_value[0]) v.allValidation(param.laebel_value, proba_value[0][:, 1])
def __init__(self): """ Initialise Salesperson class. """ # Data access object self.salesperson_dao = SalespersonDAO() # Validation object self.validator = Validation() # Form fields self.salesperson_id = tk.StringVar() self.title = tk.StringVar() self.firstname = tk.StringVar() self.surname = tk.StringVar() self.position = tk.StringVar() self.work_phone = tk.StringVar() self.email = tk.StringVar() # List of salesperson ids self.lb_ids = None # Messagebox title self.mb_title_bar = "Salesperson CRUD" pass
def main(): validator = Validation() while True: print "Pick a number, any number." num = raw_input() # validate if a number was entered. Otherwise return x num = validator.isNumber(num) if (num != False): break a = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89] b = [] lt_five = [] # loop through the list a # create a new list with all numbers less than num # create another list with all numbers less 5 for number in a: if (number < num): b.append(number) if (number < 5): lt_five.append(number) # Print the list and number entered. # Results are converted to string print "numbers less than " + str(num) + ": " + str(b) # msg = '{header}{lst}'.format(header=num, lst=b) print "numbers less than 5 in the entire list: " + str(lt_five)
def __init__(self): self.utils = Utilities() self.validation = Validation() self.entry = Entry() self.menu = Menu() self.results = list()
def __init__(self): """ Initialise CarGUI class. """ # Data access object self.car_dao = CarDAO() # Validation object self.validator = Validation() # Form fields self.car_id = tk.StringVar() self.make = tk.StringVar() self.model = tk.StringVar() self.registration = tk.StringVar() self.manufacture_year = tk.StringVar() self.colour = tk.StringVar() # List of car ids self.lb_ids = None # Messagebox title self.mb_title_bar = "Car CRUD" pass
def __init__(self): # Instantiate a data access object # Contains methods to access the database self.customer_dao = CustomerDAO() # Instantiate a validation object # Contains methods to validate input fields self.validator = Validation() # Form fields # Instantiate stringvars - hold data entered in fields of form self.customer_id = tk.StringVar() self.customer_name = tk.StringVar() self.customer_surname = tk.StringVar() self.customer_address = tk.StringVar() self.customer_city = tk.StringVar() self.customer_state = tk.StringVar() self.customer_postcode = tk.StringVar() self.customer_phone = tk.StringVar() # List of customer ids - lb for listbox self.lb_ids = None # Messagebox title self.mb_title_bar = "Customer CRUD" pass
def __init__(self): # Instantiate a data access object # Contains methods to access the database self.emp_dao = EmployeeDAO() # Instantiate a validation object # Contains methods to validate input fields self.validator = Validation() # Form fields # Instantiate stringvars - hold data entered in fields of form self.employee_id = tk.StringVar() self.firstname = tk.StringVar() self.lastname = tk.StringVar() self.title = tk.StringVar() self.email = tk.StringVar() self.work_phone = tk.StringVar() self.date_employed = tk.StringVar() self.employer_id = tk.StringVar() # List of employee ids - lb for listbox self.lb_ids = None # Messagebox title self.mb_title_bar = "Employee CRUD" pass
def __init__(self): # Instantiate a data access object # Contains methods to access the database self.payd_dao = PaydetailsDAO() # Instantiate a validation object # Contains methods to validate input fields self.validator = Validation() # Form fields # Instantiate stringvars to hold the data entered on the form self.pay_details_id = tk.StringVar() self.hourly_rate = tk.StringVar() self.total_hours = tk.StringVar() self.net_pay = tk.StringVar() self.total_deductions = tk.StringVar() self.total_pay = tk.StringVar() self.pay_slip_id = tk.StringVar() # List of product ids - lb for listbox self.lb_ids = None # Messagebox title self.mb_title_bar = "Paydetails CRUD"
def post_friends(): request_form = [('email', 'email', request.form['email'])] sanitize = Validation(request_form) email_query = 'SELECT id FROM users WHERE email = :email' email_exists = mysql.query_db(email_query, {'email': request.form['email']}) print email_exists[0]['id'] if len(email_exists) == 0: sanitize.errors.append( 'Email provided does not belong to a current user!') if len(sanitize.errors) == 0: data = { 'user_id': int(session['user']['user_id']), 'friend_id': email_exists[0]['id'], 'status': request.form['status'] } if data['status'] == 'Confirmed': update_query = 'UPDATE friendships SET status="Confirmed" WHERE friend_id = :user_id and user_id = :friend_id' mysql.query_db(update_query, data) insert_friend = 'INSERT INTO friendships (user_id, friend_id, status, created_at, updated_at) VALUES ( :user_id, :friend_id, :status, now(), now() )' row_id = mysql.query_db(insert_friend, data) flash("Your friend request is " + data['status']) return redirect('/friends') else: session['data'] = sanitize.data for error in sanitize.errors: flash(error) return redirect('/friends/add')
def test_NewLandTrade_LandSold_CorrectAmount(self): my_validation=Validation() my_service=Service(my_validation) my_service.set_acres_owned(1) my_service.set_acres_trade(-1) my_service.new_land_trade() self.assertEqual(my_service.get_acres_owned(), 0)
def add(self, login, password, passwordMatch): """ Adds user. :param login: string :param password: string :param passwordMatch: string :return: string """ v = Validation() if v.validateLogin(login=login): if v.validatePassword(password): if v.validatePasswordMatch(password, passwordMatch): h = Hash() salt = h.generateSalt() hashedPassword = h.encryptPassword(password, salt) result = self.db.addUser(login, hashedPassword, salt) if result == 'login_exist': return "Login already exists." else: if result == True: return "Account created successfully." else: return f"Database error occurred. Check {cfg.log['output']} file." else: return "Passwords do not match." else: return "Password is too weak. Password should have at least 8 characters and one character of each type: lowercase, uppercase, digit and special character." else: return "Login should have between 8 - 16 characters."
def test_Validation_PlantAcres_Error(self): my_validation=Validation() #my_service=Service(my_validation) try: my_validation.plant_acres(1000,900) assert(False) except ValidationError as myerror: self.assertEqual(str(myerror),'Not enough acres!')
def test_Validation_LandBuy_Error(self): my_validation=Validation() #my_service=Service(my_validation) try: my_validation.land_buy(100,10,900) assert(False) except ValidationError as myerror: self.assertEqual(str(myerror),'Not enough grain to buy acres!')
def test_Validation_LandSell_Error(self): my_validation=Validation() #my_service=Service(my_validation) try: my_validation.land_sell(-1000,900) assert(False) except ValidationError as myerror: self.assertEqual(str(myerror),'Not enough land to sell!')
def _validation_domains(self) -> Dict[str, list]: """Вызов методов для проверки валидации доменов Return: dict: словарь с валидными и не валидными доменами """ validator = Validation(self.domains_puny) validator.checking_len_domains() return validator.checking_valid_domains()
def __init__(self): self.data_loader = DataLoader(cfg) self.__build_model() self.log_config('config', cfg, self.train_summary_writer, 0) self.validation = Validation(cfg.validation, self.data_loader, self.valid_summary_writer, self.network, 'Valid')
def report(): Validation(root.csv, root.img_folder).report(root.save_folder + root.save_name, tkinter=True) file_to_remove = os.listdir() file_to_remove = [i for i in file_to_remove if i.startswith('temp_')] bouton4['text'] = 'Done !' for i in file_to_remove: os.remove(i)
def save(self, opts={}): relation_changes = 'relationships' in opts and bool( self.changed_relations(recursive=True)) if self.changed_attributes() or relation_changes: save_params = util.SaveParams(self).generate(opts) response = self.__update_or_save(save_params) self.__after_save(response) return Validation(self).validate_response(response) else: return True
def read(self): with open(self.filename, "r") as f: text = f.readlines() nameList = [] for name in text: # check if a name is valid, if not ignore it if Validation().isValid(name.strip()): nameList.append(Name(name.strip())) else: print('Ignored as invalid name format: ' + name) return nameList
def __init__(self): self._empname = input("enter name: ") self._empemail = input("enter email: ") e_val = Validation() if (e_val.validateemail(self._empemail) == False): exit(0) self._empmob = input("enter mobile no: ") if (e_val.validatemobile(self._empmob) == False): exit(0) self._emptype = input("enter type: ") self._empexp = int(input("enter experience: ")) self._empsalary = self.getsalary()
def __init__(self): """ The initialiser is used to "instantiate" attributes of the class. The attributes are the "variables" that have been declared "outside" of the methods of the class. Some attributes may have not been declared as they may be created any where in the class methods (python allows this). Attributes are like global variables for the class, as they are available to any method in the class. And, to use them, they must be prefixed with "self." This differentiates them from "local" variables which are defined/created and used within a single method If you need to pass the value of a local variable from one method to another, then you must pass "parameters" to the method We cannot create the GUI here as we need to return a reference to the frame created. Hence, we need to implement a 'normal' function to do this e.g. create_gui() Parameters (apart from self): None Return: None """ # Instantiate a data access object # Contains methods to access the database self.emp_dao = StaffDAO() # Instantiate a validation object # Contains methods to validate input fields self.validator = Validation() # Form fields # Instantiate stringvars - hold data entered in fields of form self.staff_id = tk.StringVar() self.first_name = tk.StringVar() self.last_name = tk.StringVar() self.title = tk.StringVar() self.email = tk.StringVar() self.sex = tk.StringVar() self.contact_no = tk.IntVar() # List of employee ids - lb for listbox self.lb_ids = None # Messagebox title self.mb_title_bar = "Staff CRUD" pass
def main(): columns, x_train, y_train, x_test, y_test = preprocessing() random_forest_ID3 = RandomForest(columns[:-1], ['age', 'hours-per-week', 'capital-gain', 'capital-loss'], Criterion.ID3, np.vstack((x_train, x_test)), 10) decision_tree_ID3 = DecisionTreeClassifier(columns[:-1], ['age', 'hours-per-week', 'capital-gain', 'capital-loss'], Criterion.ID3) random_forest_GINI = RandomForest(columns[:-1], ['age', 'hours-per-week', 'capital-gain', 'capital-loss'], Criterion.GINI, np.vstack((x_train, x_test)), 10) decision_tree_GINI = DecisionTreeClassifier(columns[:-1], ['age', 'hours-per-week', 'capital-gain', 'capital-loss'], Criterion.GINI) decision_tree_ID3.set_attribute_values(np.vstack((x_train, x_test))) decision_tree_GINI.set_attribute_values(np.vstack((x_train, x_test))) validation = Validation(x_train, y_train, x_test, y_test) print('K-fold validation:\n\n') print('Criteri ID3:\n') print('Random forest:\n') score = validation.score_cross_val(3, random_forest_ID3) print(f'Accuracy mitjana: {np.array(score[Measure.ACC]).mean()}\n') print(f'Specificity mitjana: {np.array(score[Measure.SPEC]).mean()}\n') print('Decision tree:\n') score = validation.score_cross_val(3, decision_tree_ID3) print(f'Accuracy mitjana: {np.array(score[Measure.ACC]).mean()}\n') print(f'Specificity mitjana: {np.array(score[Measure.SPEC]).mean()}\n') print('Criteri GINI:\n') print('Random forest:\n') score = validation.score_cross_val(3, random_forest_GINI) print(f'Accuracy mitjana: {np.array(score[Measure.ACC]).mean()}\n') print(f'Specificity mitjana: {np.array(score[Measure.SPEC]).mean()}\n') print('Decision tree:\n') score = validation.score_cross_val(3, decision_tree_GINI) print(f'Accuracy mitjana: {np.array(score[Measure.ACC]).mean()}\n') print(f'Specificity mitjana: {np.array(score[Measure.SPEC]).mean()}\n') print('Final model: Random Forest\n') print('Resultats finals: \n') final_measure = validation.final_measure(random_forest_ID3) print(f'Accuracy mitjana: {np.array(final_measure[Measure.ACC]).mean()}\n') print(f'Specificity mitjana: {np.array(final_measure[Measure.SPEC]).mean()}\n') print('\n\n Exemple d arbre de decisió entrenat amb totes les dades disponible a out/resultat.txt') #Imprimim un arbre de decisió entrenat amb totes les dades, per visualitzar, tot i no ser el millor model x_data = np.vstack((x_train, x_test)) y_data = np.hstack((y_train, y_test)) decision_tree_ID3.fit(x_data, y_data) write_to_file(decision_tree_ID3)
def input(self): d_i = self.dictionary_inverted() x = int(input("number for the row: ")) y = int(input("number for the column: ")) symbol = input("Chose between x and o to put: ") choice = int(input("Write 1 if you wnat to save or 0 if you don't: ")) if choice == 1: self.service.save_file() elif choice != 0: print("Wrong number!") Validation().validation_input(x, y, symbol, self.board) self.service.move( x, y, d_i[symbol] ) # punem asa ca atunci cand se apeleaza in afisare sa preia 1 sau -1 sau 0 pentru a pune cum trebuie caracterele