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_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 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_model_path(disease, model_type): """ Returns first DB occurrence of model filepath given a disease and a model type. :param disease: :param model_type: :return: model filepath (string) """ conn = Connection() cancers_models = conn.do_query_mult_col( 'SELECT model_path FROM model WHERE disease="' + disease + '" AND model_type="' + model_type + '";') return cancers_models[0]
def get_cancers_models(): """ Returns disease and model type for each of the models in DB. :return: disease,model_type (dict). """ conn = Connection() cancers_models = conn.do_query_mult_col('SELECT disease, model_type FROM model;') if cancers_models is None: return dict() else: models_dict = dict() for row in cancers_models: models_dict.setdefault(row[0], []).append(row[1]) return models_dict
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 hist_from_db(): conn = Connection() y = conn.do_query_mult_col('SELECT datetime FROM prediction;') if y: x = list() for i in range(0, len(y)): x.append(y[i][0].day) print(x) data = [go.Histogram(x=x)] first_plot_url = py.plot(data, filename='./template/plot.html', auto_open=False) with open(first_plot_url.replace("file://", "")) as plot_html_file: return plot_html_file.read() else: return "<h1 style=\"position: fixed; top: 50%;left: 40%;\">Nothing to show</h1>"
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
def generate_records_table(username): rol = get_user_rol(username) if rol == "Doctor": cols = ('PATIENT ID', 'DATE', 'DATA', 'MODEL DISEASE', 'MODEL TYPE', 'OUTPUT') body = '<table class="table" id="table">\ <thead>' body += new_head(cols) body += '</thead> \ <tbody>' conn = Connection() prediction = conn.do_query_mult_col( 'SELECT PRE.patient_id, PRE.datetime, PRE.expression_file_path, M.disease, M.model_type, PRE.result FROM prediction PRE, user U, model M WHERE U.email=\"' + username + '\" and U.id=PRE.user_id and PRE.model_id=M.id;') if prediction is not None: # There are data to show for row in prediction: body += new_row(row) body += ' </tbody>\ </table>' return body elif rol == "Admin": cols = ('PREDICTION ID', 'USER ID', 'DATE', 'MODEL') body = '<table class="table" id="table">\ <thead>' body += new_head(cols) body += '</thead> \ <tbody>' conn = Connection() prediction = conn.do_query_mult_col( 'SELECT PRE.id, PRE.user_id, PRE.datetime, PRE.model_id FROM prediction PRE;') if prediction is not None: # There are data to show for row in prediction: body += new_row(row) body += ' </tbody>\ </table>' return body
def get_json_values(disease, model_type): """ Returns json model data in html table format for given disease and model type. :param disease: :param model_type: :return: html table (string) """ conn = Connection() json_file = conn.do_query_mult_col( 'SELECT dataset_description FROM model WHERE disease="' + disease + '" AND model_type="' + model_type + '";')[0] acc = get_model_acc(disease, model_type) with open('predictor/models/'+json_file[0]) as f: data = json.load(f) html= """<table class="table"> <tbody> <tr> <th scope="row">Description</th> <td>"""+data['description']+"""</td> </tr> <tr> <th scope="row">Number of variables</th> <td>"""+str(data['num_of_variables'])+"""</td> </tr> <tr> <th scope="row">Class name</th> <td>"""+data['class_info']['name']+"""</td> </tr> <tr> <th scope="row">Class values</th> <td>"""+str(data['class_info']['values'])+"""</td> </tr> <tr> <th scope="row">Accuracy</th> <td>"""+str(acc[0])+"""</td> </tr> </tbody> </table>""" return html
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 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 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()
import hashlib from database.mysql_connector import Connection conn = Connection() def user_validation(user, password): """ Checks if the user and password are correct in DB. :param user: :param password: :return: True/False (OK/USER ERROR) """ input_password = hashlib.sha256(password.encode("utf8")) hex_dig = input_password.hexdigest() true_password = conn.do_query('SELECT password FROM user WHERE email=\"' + user + '\";') if len(true_password) == 0: return False if true_password[0] == hex_dig: return True return False def user_registration(firstname, lastname, email, password, repeated_password, rol): """ Checks if patient has already been included in DB. Else includes it. :param firstname: :param lastname: