def read_position(table, fields): ''' Users can show the details of existing positions by entering their ID. “Students” already applied are shown here. ''' result = {} pos_id = 0 name = 2 position_id = ui.user_inputs(['ID']) for row in table: if row[pos_id] == position_id: for i in range(len(row)): result[fields[i]] = row[i] students = [] application_table = data_manager.read_data('application.csv') position_id_in_application = 3 student_id_in_application = 2 for row in application_table: if row[position_id_in_application] == position_id: students.append(row[student_id_in_application]) student_table = data_manager.read_data('students.csv') list_of_students = [] student_name = 1 for row in student_table: for elements in students: if row[pos_id] == elements: list_of_students.append(row[student_name]) result['Applied Students'] = list_of_students return result
def question(question_id): file_data = data_manager.read_data('questions', int(question_id)) file_data["view_number"] = str(int(file_data["view_number"]) + 1) data_manager.update_question(question_id, file_data) file_data["submission_time"] = datetime.fromtimestamp(int(file_data["submission_time"])) answers = data_manager.read_data("answers", question_id) for answer in answers: answer["submission_time"] = datetime.fromtimestamp(int(answer["submission_time"])) return render_template('question.html', id=question_id, data=file_data, answers=answers)
def route_answer(answer_id): question_id = data_manager.get_question_id(answer_id) question = data_manager.read_data(file=data_manager.question, id=question_id) answer = data_manager.read_data(file=data_manager.answer, id=answer_id) url_delete = url_for('route_delete_answer', answer_id=answer_id) return render_template('answer.html', answer_id=answer_id, answer_header=data_manager.answer_header, answer=answer, question_header=data_manager.question_header, question=question, url_delete=url_delete)
def route_question(question_id): question = data_manager.read_data(id=question_id) print(question[0]['id']) answers = data_manager.read_data(file=data_manager.answer, id=question_id, id_key='question_id') question_header = data_manager.format_header() answer_header = data_manager.format_header(data_manager.answer_header) return render_template('question.html', question=question, answers=answers, question_header=question_header, answer_header=answer_header)
def read_company(table): ''' Users can show the details of existing companies by entering their ID. All “Position” of a company shows up here. ''' result = {} result_name = '' result_list = [] c_id = 0 name = 1 valid_id = False company_id = ui.user_inputs(['ID']) for row in table: if row[c_id] == company_id: valid_id = True result_name = str([row[name]]) break if valid_id != False: position_table = data_manager.read_data('position.csv') company_id_in_position = 3 for row in position_table: if row[company_id_in_position] == company_id: result_list.append(row[name]) for i in range(len(result_list)): result[result_name] = result_list #result[result_name] = result_list return result
def delete_student(tablet): ''' Users can delete existing students by entering their ID. Students cannot be deleted if they have an existing Application ''' searched_id=ui.user_inputs(["ID"]) application_table=data_manager.read_data("application.csv") student_index=2 can_be_deleted=True for row in application_table: if row[student_index]==searched_id: can_be_deleted=False break if can_be_deleted==True: student_id=0 to_delete_index=None for row in range(len(table)): if table[row][student_id]==searched_id: to_delete_index=row table.pop(to_delete_index) else: ui.print_error("Can't delete that person!") return table
def build_D(mode, labels): key_subtask = 'D' rows = data_manager.read_data(key_subtask, mode) ofname = data_manager.fname_pred(key_subtask, mode) indexer = Indexer(['positive', 'negative']) topic_count = {} for row, label in zip(rows, labels): topic = row[1] if topic not in topic_count: topic_count[topic] = np.zeros( indexer.size()) # create a numpy array: [0., 0.] topic_count[topic][indexer.idx(label)] += 1 fobj = open(ofname, 'w') for topic, count in topic_count.items(): n_sample = np.sum(count) dist = count / n_sample fobj.write('%s\t%s\n' % ( topic, '\t'.join(map(lambda k: '%.12f' % k, dist)), )) fobj.close()
def build_E(mode): key_subtask = 'E' rows = data_manager.read_data(key_subtask, mode) ofname = data_manager.fname_gold(key_subtask, mode) indexer = Indexer(['-2', '-1', '0', '1', '2']) topic_label = {} for row in rows: topic = row[1] label = row[2] if topic not in topic_label: topic_label[topic] = np.zeros(indexer.size()) topic_label[topic][indexer.idx(label)] += 1 fobj = open(ofname, 'w') for topic, labels in topic_label.items(): n_sample = np.sum(labels) dist = labels / n_sample fobj.write('%s\t%s\n' % (topic, '\t'.join(map(lambda k: '%.12f' % k, dist)))) fobj.close()
def create_position(table, fields): ''' Users can create new positions. A position has an ID, description, number of seats and a “Company ID”. Position IDs are unique amongst other positions. Descriptions cannot be empty. The number of seats must be greater than 0. Company ID must exist. ''' new_position = ui.user_inputs(fields) new_id = common.create_id(table) company_table = data_manager.read_data('company.csv') company_id = 2 check_company = common.check_id(new_position[company_id], company_table, 0) seats_id = 1 try: check_seats = int(new_position[seats_id]) > 0 except: check_seats = False if check_company and check_seats: new_position.insert(0, new_id) table.append(new_position) return table else: return []
def read_student(table): ''' Users can show the details of existing students by entering their ID. All “Application” of student shows up here. ''' user_id=0 user_name=1 result="" searched_id=ui.user_inputs(["ID"]) valid_id=False for row in table: if row[user_id]==searched_id: valid_id=True result+=row[user_name]+": " if valid_id==True: searching_table=data_manager.read_data("application.csv") position_list=[] student_id=2 position_id=3 for row in searching_table: if row[student_id]==searched_id: position_list.append(row[position_id]) position_id=0 position_name=1 searching_table=data_manager.read_data("position.csv") position_name_list=[] for element in position_list: for row in searching_table: if row[position_id]==element: position_name_list.append(row[position_name]) for element in range(len(position_name_list)): if element!=len(position_name_list)-1: result+=position_name_list[element]+", " else: result+=position_name_list[element] else: ui.print_error("Not found that ID!") return result
def A_down_up(answer_id): file_data = data_manager.read_answer(answer_id) file_data["vote_number"] = str(int(file_data["vote_number"]) - 1) data_manager.update_answer(answer_id, file_data) question_id = file_data["question_id"] question_data = data_manager.read_data('questions', str(question_id)) question_data["view_number"] = str(int(question_data["view_number"]) - 1) data_manager.update_question(question_id, question_data) return redirect('/question/' + str(question_id))
def build_C(mode): key_subtask = 'C' rows = data_manager.read_data(key_subtask, mode) ofname = data_manager.fname_gold(key_subtask, mode) fobj = open(ofname, 'w') for row in rows: fobj.write('%s\t%s\t%s\n' % (row[0], row[1], row[2])) fobj.close()
def build_C(mode, labels): key_subtask = 'C' rows = data_manager.read_data(key_subtask, mode) ofname = data_manager.fname_pred(key_subtask, mode) fobj = open(ofname, 'w') for row, label in zip(rows, labels): fobj.write('%s\t%s\t%s\n' % (row[0], row[1], label)) fobj.close()
def read_positions(table, fields): ''' Users can list existing positions. All attributes of a position are visible plus it’s displayed how many seats are already taken (e.g 2/1, one out of two seats are taken). ''' result_table2 = [] line = [] for i in range(len(table)): line = [] for j in range(len(table[i])): line.append(table[i][j]) result_table2.append(line) result_fields = fields.copy() result_fields.append('Company Name') result_fields.append('Seats already taken') position_id = 0 seats_id = 2 company_table = data_manager.read_data('company.csv') company_id = 0 company_name = 1 company_id_in_position = 3 application_table_full = data_manager.read_data('application.csv') application_table = [] for line in application_table_full: if line[1] == 'yes': application_table.append(line) position_id_in_application = 3 for row in result_table2: for company in company_table: if row[company_id_in_position] == company[company_id]: row.append(company[company_name]) count = 0 for application in application_table: if row[position_id] == application[position_id_in_application]: count += 1 row.append('{}/{}'.format(count, row[seats_id])) return (result_table2, result_fields)
def correlation_experiment(filename, language, embedding_function, name): data, character_encoder, tag_encoder, embedded_chars = read_data( filename, language) character_decoder = {v: k for k, v in character_encoder.items()} features = getphonfeatures() language_features = [ np.array(features[character_decoder[f]]) if character_decoder[f] in features else None for f in range(len(character_encoder)) ] feature_similarity = get_similarity_matrix(language_features, embedded_chars) embeddings = embedding_function(data, character_encoder, embedded_chars, character_decoder) similarities = [ get_similarity_matrix(m, embedded_chars) for m in embeddings ] rs = [ correlation(feature_similarity, similarities[i])[0] for i in [0, 1, 2] ] print("%s %s:" % (language, name)) print(" PEARSON R FOR EMBEDDING AND FEATURE REPR. SIMILARITIES:") print(" %s,DIM=5" % language, rs[0]) print(" %s,DIM=15" % language, rs[1]) print(" %s,DIM=30" % language, rs[2]) random_rs = [[], [], []] for i in range(N): random_embeddings = [matshuf(m) for m in embeddings] random_similarities = [ get_similarity_matrix(m, embedded_chars) for m in random_embeddings ] random_rs[0].append( correlation(feature_similarity, random_similarities[0])[0]) random_rs[1].append( correlation(feature_similarity, random_similarities[1])[0]) random_rs[2].append( correlation(feature_similarity, random_similarities[2])[0]) print((" P=%.2f CONF. INTERVALS FOR PEARSON R OF RANDOM ASSIGNMENT OF\n" % P) + " EMBEDDINGS TO PHONEMES AND PHONETIC FEATURE DESCRIPTIONS:") civals = [confidence_interval_value(random_rs[i]) for i in [0, 1, 2]] print(" %s,DIM=5" % language, confidence_interval_value(random_rs[0]), check_r(civals[0], rs[0]), rs[0]) print(" %s,DIM=15" % language, confidence_interval_value(random_rs[1]), check_r(civals[1], rs[1]), rs[1]) print(" %s,DIM=30" % language, confidence_interval_value(random_rs[2]), check_r(civals[2], rs[2]), rs[2]) print()
def update(question_id): file_data = data_manager.read_data("questions", question_id) if request.method == 'POST': question = { "id": question_id, "submission_time": file_data["submission_time"], "view_number": file_data['view_number'], "vote_number": file_data['vote_number'], "title": request.form.get('title'), "message": request.form.get("message"), "image": request.form.get("image") } data_manager.update_question(question_id, question) return redirect('/list') return render_template('edit.html', id=question_id, data=file_data)
def route_edit_question(question_id): question = data_manager.read_data(id=question_id) if request.method == 'POST': question = { 'id': question[0]['id'], 'title': request.form.get('title'), 'message': request.form.get('message'), 'submission_time': data_manager.get_time(), 'vote_number': question[0]['vote_number'], 'image': question[0]['image'], 'view_number': question[0]['view_number'] } data_manager.edit_message(question) return redirect(url_for('route_question', question_id=question_id)) return render_template('edit_question.html', title_field=question[0]['title'], message_field=question[0]['message'], specific_url=url_for('route_edit_question', question_id=question_id))
def all_questions(): questions = data_manager.read_data('questions') ordered_direction = "desc" ordered_by = "submission_time" args = request.args for elem in questions: elem["submission_time"] = datetime.fromtimestamp(int(elem["submission_time"])) if "ordered_direction" in args and "ordered_by" in args: ordered_direction = args.get('ordered_direction') ordered_by = args.get('ordered_by') if ordered_direction == "desc": try: questions = sorted(questions, key=lambda k: int(k[ordered_by]), reverse=True) except: questions = sorted(questions, key=lambda k: k[ordered_by], reverse=True) elif ordered_direction == "asc": try: questions = sorted(questions, key=lambda k: int(k[ordered_by])) except: questions = sorted(questions, key=lambda k: k[ordered_by]) return render_template("all_questions.html", questions=questions, ordered_direction=ordered_direction, ordered_by=ordered_by)
def delete_position(table): ''' Users can delete existing positions by entering their ID. Positions cannot be deleted if they have an existing “Application”. ''' position_id = ui.user_inputs(['ID']) line = -1 for i in range(len(table)): if table[i][0] == position_id: line = i application_table = data_manager.read_data('position.csv') position_id_in_application = 3 can_be_deleted = True if line >= 0: for row in application_table: if row[position_id_in_application] == position_id: can_be_deleted = False if can_be_deleted: table.pop(line) return table else: return []
def start(): while True: ui.print_menu(options, 'Back to Main Menu') user_input = ui.user_inputs(['number']) if user_input == '1': result_table = create_position( data_manager.read_data('position.csv'), position_fields) if result_table: data_manager.write_to_table(file_name, result_table) ui.print_progress('New record saved') else: ui.print_error('Invalid data \n Record not saved.') elif user_input == '2': ui.print_dictionary(read_position(table, position_fields)) elif user_input == '3': table_and_fields = read_positions(table, position_fields) ui.print_table(table_and_fields[0], table_and_fields[1]) elif user_input == '4': result_table = update_position(table) if result_table: data_manager.write_to_table(file_name, result_table) ui.print_progress('Record updated') else: ui.print_error('Invalid data \n Record not saved.') elif user_input == '5': result_table = delete_position(table) if result_table: data_manager.write_to_table(file_name, result_table) ui.print_progress('Record deleted') else: ui.print_error('Invalid data \n Record not saved.') elif user_input == '0': break else: ui.print_error('Wrong Number')
def route_index(): reverse_dict = {'Ascending': False, 'Descending': True} if request.args.get('sort_key') is None: sort_key = 'submission_time' sort_reverse = False else: sort_key = request.args.get('sort_key') sort_reverse = request.args.get('reverse') if sort_reverse == "False": sort_reverse = False else: sort_reverse = True questions = data_manager.read_data() sorted_questions = data_manager.sort_messages(questions, sort_key=sort_key, reverse_sorting=sort_reverse) header = data_manager.question_header formatted_header = data_manager.format_header(header) return render_template('list.html', questions=sorted_questions, header=header, formatted_header=formatted_header, reverse_dict=reverse_dict)
def delete_company(table): ''' Users can delete existing companies by entering their ID. Companies cannot be deleted if they have an existing “Position”. ''' company_id = ui.user_inputs(['ID']) line = None for i in range(len(table)): if table[i][0] == company_id: line = i position_table = data_manager.read_data('position.csv') company_id_in_position = 3 can_be_deleted = True if line != None: for row in position_table: if row[company_id_in_position] == company_id: can_be_deleted = False if can_be_deleted: table.pop(line) else: return [] return table
import time import matplotlib.pyplot as plt import tqdm from utils import conditioning_angle from numpy.linalg import lstsq from scipy.linalg import solve_triangular import scipy # Parameters: # Number of tries to compute mean TRIES = 20 # Number of first/last elements to remove from tries CUT = 8 # Read data A, b = read_data('data/ML-CUP19-TR.csv', add_augmented_columns=True) m, n = A.shape ######################################################################################################################## # Properties of matrix A # ######################################################################################################################## print("********** PROPERTIES OF THE MATRIX A **********") print('Dim: {} x {}'.format(m, n)) print("Min: {}, Max: {}".format(A.min(), A.max())) print("Rank of A is " + str(np.linalg.matrix_rank(A))) print("Condition number of matrix A is " + str(np.linalg.cond(A))) check = np.random.rand(A.shape[0], A.shape[1]) c_min = check.min() c_max = check.max() check = (((check - c_min) / (c_max - c_min)) * (A.max() - A.min())) + A.min() assert np.isclose(check.min(), A.min()) and np.isclose(check.max(), A.max())
import numpy as np from numpy.linalg import norm from data_manager import read_data import time import tqdm import matplotlib.pyplot as plt from conjugate_gradient import conjugate_gradient import random A, b = read_data('data/ML-CUP19-TR.csv') m, n = A.shape # Our solution start = time.perf_counter_ns() x, status, ite = conjugate_gradient(A, b) done = time.perf_counter_ns() elapsed = done - start print("our implementation: ns spent: ", elapsed) print("status: ", status) print("iterations: ", ite) print("||Ax - b|| = ", norm(np.matmul(A, x) - b)) print("||Ax - b||/||b|| =", np.divide(norm(np.matmul(A, x) - b), norm(b))) print( "||A*Ax - A*b|| =", norm( np.matmul(np.transpose(A), np.matmul(A, x)) - np.matmul(np.transpose(A), b))) # Library Least Squares solution start = time.perf_counter_ns() xnp = np.linalg.lstsq(A, b, rcond=None)
from argparse import ArgumentParser from data_manager import read_data parser = ArgumentParser(prog="hungry") parser.add_argument("type", type=str, choices=['view', 'message']) #parser.add_argument("did_send", type=str, choices=['true', 'false']) parser.add_argument('-id', '--user_id', type=int) parser.add_argument('-e', '--email', type=str) args = parser.parse_args() if args.type == "view": print(read_data(user_id=args.user_id)) print(read_data(email=args.email)) elif args.type == "message": print("send message")
def Q_vote_down(question_id): file_data = data_manager.read_data('questions', question_id) file_data["vote_number"] = str(int(file_data["vote_number"]) - 1) data_manager.update_question(question_id, file_data) return redirect('/list')
import ui import common import data_manager file_name = 'company.csv' table = data_manager.read_data('company.csv') options = [ 'Create Company', 'Read Company', 'Read Companies', 'Update Company', 'Delete Company' ] company_fields = ['ID', 'Name'] def create_company(table, fields): ''' Users can create new companies. Companies have an ID, name. IDs and names of companies are unique amongst other companies. ''' new_company = ui.user_inputs(fields) new_id = common.create_id(table) company_name = 0 company_name_in_company = 1 check_company = common.check_id(new_company[company_name], table, company_name_in_company) if check_company: return [] else: new_company.insert(0, new_id) table.append(new_company) return table
import ui import common import data_manager file_name = 'application.csv' table = data_manager.read_data(file_name) options = ['Create Application', 'Update Application', 'Delete Application'] application_fields = ['ID', 'Accepted', 'Student ID', 'Position ID'] def create_application(fields, table): ''' Users can create new applications. An application has an ID, an “accepted” field, a “Student ID” and a “Position ID”. IDs are unique amongst other applications. Student and Position IDs must exist. The “accepted” field stores whether the application was accepted by a company or not. ''' to_add = ui.user_inputs(fields) accepted_index = 0 if to_add[accepted_index] == "yes" or to_add[accepted_index] == "no": student_id_index = 1 position_id_index = 2 student_is_ok = False position_is_ok = False compare_table = data_manager.read_data("students.csv") student_id_index_from_student_table = 0
def create_application(fields, table): ''' Users can create new applications. An application has an ID, an “accepted” field, a “Student ID” and a “Position ID”. IDs are unique amongst other applications. Student and Position IDs must exist. The “accepted” field stores whether the application was accepted by a company or not. ''' to_add = ui.user_inputs(fields) accepted_index = 0 if to_add[accepted_index] == "yes" or to_add[accepted_index] == "no": student_id_index = 1 position_id_index = 2 student_is_ok = False position_is_ok = False compare_table = data_manager.read_data("students.csv") student_id_index_from_student_table = 0 for row in range(len(compare_table)): if compare_table[row][ student_id_index_from_student_table] == to_add[ student_id_index]: student_is_ok = True compare_table = data_manager.read_data("position.csv") position_id_index_in_position_table = 0 position_places_index = 2 max_places = None for row in range(len(compare_table)): if compare_table[row][ position_id_index_in_position_table] == to_add[ position_id_index]: max_places = int(compare_table[row][position_places_index]) position_is_ok = True break count = 0 if position_is_ok == True: application_table_position_id = 3 application_acceptance = 1 for row in table: if row[application_table_position_id]==to_add[position_id_index] \ and row[application_acceptance]=="yes": count += 1 if count >= max_places: position_is_ok = False table_student_id_index = 2 table_position_id = 3 for row in table: if row[table_student_id_index]==to_add[table_student_id_index-1] \ and row[table_position_id]==to_add[table_position_id-1]: position_is_ok = False if position_is_ok == True and student_is_ok == True: to_add.insert(0, common.create_id(table)) table.append(to_add) data_manager.write_to_table("application.csv", table) else: ui.print_error("Can't add that row.") else: ui.print_error("Wrong input(yes or no)")
import ui import common import data_manager file_name = 'position.csv' table = data_manager.read_data('position.csv') options = [ 'Create Position', 'Read Position', 'Read Positions', 'Update Position', 'Delete Position' ] position_fields = ['ID', 'Description', 'Seats', 'Company ID'] def create_position(table, fields): ''' Users can create new positions. A position has an ID, description, number of seats and a “Company ID”. Position IDs are unique amongst other positions. Descriptions cannot be empty. The number of seats must be greater than 0. Company ID must exist. ''' new_position = ui.user_inputs(fields) new_id = common.create_id(table) company_table = data_manager.read_data('company.csv') company_id = 2 check_company = common.check_id(new_position[company_id], company_table, 0) seats_id = 1 try: check_seats = int(new_position[seats_id]) > 0