def save_model(model, model_info, x_test, y_test, model_type, disease): """ Saves given model, json info and characteristics in DB. :param model: :param model_info: :param x_test: :param y_test: :param model_type: :param disease: """ K.clear_session() conn = Connection('database/mysql_connection_settings.json') model_name = disease + time.strftime("%Y-%m-%d_%H%M%S") model.save('predictor/models/' + model_name) with open('predictor/models/' + model_name, 'rb') as f: model = pickle.load(f) json_path = model_name + '-model_info.json' with open('predictor/models/' + json_path, 'w') as f: json.dump(model_info, f) conn.do_query( 'INSERT INTO model(train_date, acc, model_type,disease, dataset_description, model_path) values (\"' + time.strftime("%Y-%m-%d_%H:%M:%S") + '\",\"' + str(round(evaluate_model(model, x_test, y_test)[1], 3)) + '\",\"' + model_type + '\",\"' + disease + '\",\"' + json_path + '\",\"' + model_name + '\");') conn.connection.commit()
def update_user_rol(uid, new_rol): """ Updates rol in DB for given user. :param uid: :param new_rol: """ conn = Connection() conn.do_query('UPDATE user SET rol=\"' + new_rol + '\" WHERE id=' + uid + ';') conn.connection.commit()
def get_model_acc(disease_name, model_name): """ Returns model test ACC for given disease and model type. :param disease_name: :param model_name: :return: test ACC (list of string) """ conn = Connection() model_id = \ conn.do_query('SELECT id from model WHERE model_type="' + model_name + '" AND disease="' + disease_name + '";')[0] return conn.do_query('SELECT acc from model where id="' + str(model_id) + '";')
def get_patient_from_db(id_patient): conn = Connection() uid = conn.do_query('SELECT id FROM patient WHERE patient_id="' + id_patient + '";') if uid: return uid[0] else: conn.do_query( 'INSERT INTO patient(patient_id) VALUES (\'' + id_patient + '\');') conn.connection.commit() uid = conn.do_query('SELECT id FROM patient WHERE patient_id="' + id_patient + '";') print(uid) return uid[0]
def insert_new_user(username, email, password, rol): """ Inserts new user into DB :param username: :param email: :param password: :param rol: """ input_password = hashlib.sha256(password.encode("utf8")) hex_dig = input_password.hexdigest() conn = Connection() conn.do_query( 'INSERT INTO user(username, password, email, rol) VALUES (\'' + username + '\',\'' + hex_dig + '\',\'' + email + '\',\'' + rol + '\');') conn.connection.commit()
def get_user_rol(email): """ Returns user rol from DB for given user :param email: :return: user rol (string) """ conn = Connection() rol = conn.do_query('SELECT rol FROM user WHERE email=\"' + email + '\";') return rol[0]
def insert_prediction(date, expression_file_path, result, disease_name, model_name, patient_id, user_email): """ Inserts new prediction query into DB. :param date: :param expression_file_path: :param result: :param disease_name: :param model_name: :param patient_id: :param user_email: """ conn = Connection() model_id = conn.do_query('SELECT id from model WHERE model_type="' + model_name + '" AND disease="' + disease_name + '";')[0] patient_id = get_patient_from_db(patient_id) user_id = conn.do_query('SELECT id from user WHERE email="' + user_email + '";')[0] conn.do_query( 'INSERT INTO prediction(datetime, expression_file_path, result, model_id, patient_id, user_id) VALUES (\'' + date + '\',\'' + expression_file_path + '\',\'' + result + '\',\'' + str(model_id) + '\',\'' + str(patient_id) + '\',\'' + str(user_id) + '\');') conn.connection.commit()
def delete_by_id(table, uid): """ Deletes user given its uid. :param table: :param uid: """ conn = Connection() if table == "user" and len(conn.do_query('SELECT id FROM user WHERE rol=\"Admin\";')) == 1: # Invalid to delete last admin return False else: to_delete = conn.do_query('SELECT * FROM ' + table + ' WHERE id = \'' + str(uid) + '\';') if to_delete is not None: conn.do_query('DELETE FROM ' + table + ' WHERE id = \'' + str(uid) + '\';') conn.connection.commit() deleted = conn.do_query('SELECT * FROM ' + table + ';') return True else: return False
def generate_table_from_db(table_name): conn = Connection() cols = conn.do_query( 'SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME=\"' + table_name + '\" AND TABLE_SCHEMA = \'' + conn.get_database() + '\' ORDER BY ORDINAL_POSITION;') index_id = cols.index('id') num_cols = len(cols) table = conn.do_query_mult_col( 'SELECT * FROM ' + table_name + ';') if cols is not None: body = '<form class="needs-validation" id="form_funciona" name="user_form" method="post" action="/administration/' + table_name + '/insert" enctype=multipart/form-data>' body += '<table class="table" id="table">\ <thead>' cols.append(' ') # For delete column cols.append(' ') # For update column body += new_head(tuple(cols)) body += '</thead> \ <tbody>' if table is not None: row_num = 1 for row in table: row_id = row[index_id] body += new_row(row).replace('</tr>', '<td><a href="/administration/' + table_name + '/delete/' + str( row_id) + '"><span class="glyphicon glyphicon-remove"></span></a></td>') # Adds delete button to each row body += '<td><a href="#"><span class="glyphicon glyphicon-pencil" onclick="update_db(' + str( row_num) + ')"></span></a></td></tr>' # Adds delete button to each row row_num += 1 insert_row = list() if table_name == 'model': insert_row = insert_row_model(num_cols, index_id, insert_row, cols) else: for i in range(1, num_cols + 1): if i == index_id + 1: insert_row.append(' ') else: insert_row.append('<input class="form-control" name=\"' + cols[ i - 1] + '\" style="border-width: thin; border-radius: 10px ; box-sizing: border-box; ' 'width: 100%" ' 'type="text" required></input>') insert_row.append( '<button class="btn btn-default" type="submit"><a href="#"><span class="glyphicon glyphicon-plus"></span></a></button>') insert_row.append(' ') body += new_insert_row_form(insert_row) print(body) body += ' </tbody>' body += '</table>' body += '</form>' return body