def __init__(self):
        '''

        This constructor is responsible for defining class variables.

        '''

        self.list_error = []
        self.sql = SQL()
        self.db_ml = current_app.config.get('SQL_DB')
Exemple #2
0
    def __init__(self, premodel_data=None):
        '''

        This constructor is responsible for defining class variables.

        '''

        self.premodel_data = premodel_data
        self.list_error = []
        self.sql = SQL()
        self.db_ml = current_app.config.get('DB_ML')
        self.classification = current_app.config.get('MODEL_TYPE')[0]
        self.regression = current_app.config.get('MODEL_TYPE')[1]
Exemple #3
0
    def __init__(self, premodel_data, session_type):
        '''

        This constructor is responsible for defining class variables.

        '''

        # class variables
        self.premodel_data = premodel_data
        self.session_type = session_type
        self.list_error = []
        self.sql = SQL()
        self.db_ml = current_app.config.get('DB_ML')
class ModelType(object):
    '''

    Note: this class explicitly inherits the 'new-style' class.

    '''

    def __init__(self):
        '''

        This constructor is responsible for defining class variables.

        '''

        self.list_error = []
        self.sql = SQL()
        self.db_ml = current_app.config.get('SQL_DB')

    def get_model_type(self, collection):
        '''

        This method is responsible for retrieving the model type, from the
        SQL database, using a fixed 'collection'.

        @collection, this supplied argument corresponding to the 'collection'
            column from the 'tbl_dataset_entity' database table.

        @sql_statement, is a sql format string, and not a python string.
            Therefore, '%s' is used for argument substitution.

        '''

        # select dataset
        self.sql.connect(self.db_ml)
        sql_statement = 'SELECT mtype.model'\
            ' FROM tbl_dataset_entity mid'\
            ' INNER JOIN tbl_model_type mtype'\
            ' ON mid.model_type = mtype.id_model'\
            ' WHERE mid.collection=%s'
        args = (collection)
        response = self.sql.execute('select', sql_statement, args)

        # retrieve any error(s)
        response_error = self.sql.get_errors()

        # return result
        if response_error:
            return {'error': response_error, 'result': None}
        else:
            return {'error': None, 'result': response['result'][0][0]}
class ModelType(object):
    '''

    Note: this class explicitly inherits the 'new-style' class.

    '''
    def __init__(self):
        '''

        This constructor is responsible for defining class variables.

        '''

        self.list_error = []
        self.sql = SQL()
        self.db_ml = current_app.config.get('SQL_DB')

    def get_model_type(self, collection):
        '''

        This method is responsible for retrieving the model type, from the
        SQL database, using a fixed 'collection'.

        @collection, this supplied argument corresponding to the 'collection'
            column from the 'tbl_dataset_entity' database table.

        @sql_statement, is a sql format string, and not a python string.
            Therefore, '%s' is used for argument substitution.

        '''

        # select dataset
        self.sql.connect(self.db_ml)
        sql_statement = 'SELECT mtype.model'\
            ' FROM tbl_dataset_entity mid'\
            ' INNER JOIN tbl_model_type mtype'\
            ' ON mid.model_type = mtype.id_model'\
            ' WHERE mid.collection=%s'
        args = (collection)
        response = self.sql.execute('select', sql_statement, args)

        # retrieve any error(s)
        response_error = self.sql.get_errors()

        # return result
        if response_error:
            return {'error': response_error, 'result': None}
        else:
            return {'error': None, 'result': response['result'][0][0]}
Exemple #6
0
    def __init__(self):
        '''

        This constructor is responsible for defining class variables.

        '''

        self.list_error = []
        self.sql = SQL()
        self.db_ml = current_app.config.get('DB_ML')
        self.model_list = current_app.config.get('MODEL_TYPE')

        if session.get('uid'):
            self.uid = int(session.get('uid'))
        else:
            self.uid = 0
Exemple #7
0
    def __init__(self, premodel_data=None, session_type=None):
        '''

        This constructor is responsible for defining class variables.

        @self.premodel_data, a dictionary with the following elements:

            {'uid': xx, 'id_entity': xx, 'title': yyy}

        Note: 'xx' denotes an integer value, 'yyy' a unicode string, and 'zz'
              representing a float value.

        '''

        self.premodel_data = premodel_data
        self.session_type = session_type
        self.list_error = []
        self.sql = SQL()
        self.db_ml = current_app.config.get('SQL_DB')
    def __init__(self):
        '''

        This constructor is responsible for defining class variables.

        '''

        self.list_error = []
        self.sql = SQL()
        self.db_ml = current_app.config.get('SQL_DB')
Exemple #9
0
class Session(object):
    '''

    This class provides an interface to retrieve the 'session_name', and
    'id_entity' from the 'tbl_dataset_entity' sql database table.

    Note: this class is invoked within 'views.py'

    Note: this class explicitly inherits the 'new-style' class.

    '''
    def __init__(self):
        '''

        This constructor is responsible for defining class variables.

        '''

        self.list_error = []
        self.sql = SQL()
        self.db_ml = current_app.config.get('DB_ML')

    def get_all_sessions(self):
        '''

        This method is responsible for retrieving all sessions from the
        'tbl_dataset_entity' sql database table.

        '''

        # local variables
        list_session = []

        # sql query
        self.sql.connect(self.db_ml)
        sql_statement = 'SELECT id_entity, title FROM tbl_dataset_entity'
        response = self.sql.execute(sql_statement, 'select')

        # rebuild session list, get error(s) if any
        if response['result']:
            for item in response['result']:
                list_session.append({'id': item[0], 'title': item[1]})
            response_error = self.sql.get_errors()
        else:
            response_error = 'no previous session found in database'

        # disconnect from database
        self.sql.disconnect()

        # return result
        if response_error:
            return {'result': None, 'error': response_error}
        else:
            return {'result': list_session, 'error': None}
    def __init__(self, premodel_data=None, session_type=None):
        '''

        This constructor is responsible for defining class variables.

        @self.premodel_data, a dictionary with the following elements:

            {'uid': xx, 'id_entity': xx, 'title': yyy}

        Note: 'xx' denotes an integer value, 'yyy' a unicode string, and 'zz'
              representing a float value.

        '''

        self.premodel_data = premodel_data
        self.session_type = session_type
        self.list_error = []
        self.sql = SQL()
        self.db_ml = current_app.config.get('SQL_DB')
Exemple #11
0
class Entity(object):
    '''

    This class provides an interface to save, retrieve an SVM entity title,
    from the 'tbl_dataset_entity' sql database table.

    Note: this class is invoked within 'model_generate.py', 'base_data.py',
          and 'data_append.py'

    Note: this class explicitly inherits the 'new-style' class.

    '''
    def __init__(self, premodel_data=None, session_type=None):
        '''

        This constructor is responsible for defining class variables.

        @self.premodel_data, a dictionary with the following elements:

            {'uid': xx, 'id_entity': xx, 'title': yyy}

        Note: 'xx' denotes an integer value, 'yyy' a unicode string, and 'zz'
              representing a float value.

        '''

        self.premodel_data = premodel_data
        self.session_type = session_type
        self.list_error = []
        self.sql = SQL()
        self.db_ml = current_app.config.get('SQL_DB')

    def save(self):
        '''

        This method stores, or updates dataset entities into its corresponding
        'EAV data model' database table.

        @sql_statement, is a sql format string, and not a python string.
            Therefore, '%s' is used for argument substitution.

        Note: 'UTC_TIMESTAMP' returns the universal UTC datetime

        '''

        # insert / update dataset entity value
        self.sql.connect(self.db_ml)

        if self.session_type == 'data_append':
            sql_statement = 'UPDATE tbl_dataset_entity '\
                'SET uid_modified=%s, datetime_modified=UTC_TIMESTAMP() '\
                'WHERE id_entity=%s'
            args = (self.premodel_data['uid'], self.premodel_data['id_entity'])
            response = self.sql.execute('update', sql_statement, args)

        elif self.session_type == 'data_new':
            sql_statement = 'INSERT INTO tbl_dataset_entity '\
                '(title, collection, model_type, uid_created, datetime_created) '\
                'VALUES(%s, %s, %s, %s, UTC_TIMESTAMP())'
            args = (self.premodel_data['title'],
                    self.premodel_data['collection'],
                    self.premodel_data['model_type'],
                    self.premodel_data['uid'])
            response = self.sql.execute('insert', sql_statement, args)

        # retrieve any error(s)
        response_error = self.sql.get_errors()

        # return result
        if response_error:
            return {'status': False, 'error': response_error}
        else:
            return {'status': True, 'error': None, 'id': response['id']}

    def get_title(self, id_entity):
        '''

        This method is responsible for retrieving an SVM entity title, from the
        SQL database, using a fixed 'id_entity'.

        @id_entity, this supplied argument corresponds to the 'id_entity'
            column from the 'tbl_dataset_value' database table.

        @sql_statement, is a sql format string, and not a python string.
            Therefore, '%s' is used for argument substitution.

        '''

        # select dataset
        self.sql.connect(self.db_ml)
        sql_statement = 'SELECT title '\
            'FROM tbl_dataset_entity '\
            'WHERE id_entity=%s'
        args = (id_entity)
        response = self.sql.execute('select', sql_statement, args)

        # retrieve any error(s)
        response_error = self.sql.get_errors()

        # return result
        if response_error:
            return {'error': response_error, 'result': None}
        else:
            return {'error': None, 'result': response['result']}
Exemple #12
0
class Account(object):
    '''

    This class provides an interface to the users account.

    Note: this class explicitly inherits the 'new-style' class.

    '''
    def __init__(self):
        '''

        This constructor is responsible for defining class variables.

        '''

        self.list_error = []
        self.sql = SQL()
        self.db_ml = current_app.config.get('SQL_DB')

    def save_account(self, username, email, password):
        '''

        This method stores a user account, along with their corresponding
        password into an 'EAV data model' database table.

        @sql_statement, is a sql format string, and not a python string.
            Therefore, '%s' is used for argument substitution.

        '''

        # insert / update dataset entity value
        self.sql.connect(self.db_ml)

        sql_statement = 'INSERT INTO tbl_user '\
            '(username, email, password, datetime_joined) '\
            'VALUES(%s, %s, %s, UTC_TIMESTAMP())'
        args = (username, email, password)
        response = self.sql.execute('insert', sql_statement, args)

        # retrieve any error(s)
        response_error = self.sql.get_errors()

        # return result
        if response_error:
            return {'status': False, 'error': response_error, 'id': None}
        else:
            return {'status': True, 'error': None, 'id': response['id']}

    def check_username(self, username):
        '''

        This method checks if the supplied username already exists.

        '''

        # select dataset
        self.sql.connect(self.db_ml)
        sql_statement = 'SELECT * '\
            'FROM tbl_user '\
            'WHERE username=%s'
        args = (username)
        response = self.sql.execute('select', sql_statement, args)

        # retrieve any error(s)
        response_error = self.sql.get_errors()

        # return result
        if response_error:
            return {'error': response_error, 'result': None}
        else:
            return {'error': None, 'result': response['result']}

    def check_email(self, email):
        '''

        This method checks if the supplied email already exists.

        '''

        # select dataset
        self.sql.connect(self.db_ml)
        sql_statement = 'SELECT * '\
            'FROM tbl_user '\
            'WHERE email=%s'
        args = (email)
        response = self.sql.execute('select', sql_statement, args)

        # retrieve any error(s)
        response_error = self.sql.get_errors()

        # return result
        if response_error:
            return {'error': response_error, 'result': None}
        else:
            return {'error': None, 'result': response['result']}

    def get_password(self, username):
        '''

        This method returns the hashed password for a supplied username.

        '''

        # select dataset
        self.sql.connect(self.db_ml)
        sql_statement = 'SELECT password '\
            'FROM tbl_user '\
            'WHERE username=%s'
        args = (username)
        response = self.sql.execute('select', sql_statement, args)

        # retrieve any error(s)
        response_error = self.sql.get_errors()

        # return result
        if response_error:
            return {'error': response_error, 'result': None}
        else:
            return {'error': None, 'result': response['result'][0][0]}

    def get_uid(self, username):
        '''

        This method returns the userid (i.e uid) for a supplied username.

        '''

        # select dataset
        self.sql.connect(self.db_ml)
        sql_statement = 'SELECT id_user '\
            'FROM tbl_user '\
            'WHERE username=%s'
        args = (username)
        response = self.sql.execute('select', sql_statement, args)

        # retrieve any error(s)
        response_error = self.sql.get_errors()

        # return result
        if response_error:
            return {'error': response_error, 'result': None}
        elif not response['result']:
            return {'error': 'no uid', 'result': None}
        else:
            return {'error': None, 'result': response['result'][0][0]}
class Entity(object):
    '''

    This class provides an interface to save, retrieve an SVM entity title,
    from the 'tbl_dataset_entity' sql database table.

    Note: this class is invoked within 'model_generate.py', 'base_data.py',
          and 'data_append.py'

    Note: this class explicitly inherits the 'new-style' class.

    '''

    def __init__(self, premodel_data=None, session_type=None):
        '''

        This constructor is responsible for defining class variables.

        @self.premodel_data, a dictionary with the following elements:

            {'uid': xx, 'id_entity': xx, 'title': yyy}

        Note: 'xx' denotes an integer value, 'yyy' a unicode string, and 'zz'
              representing a float value.

        '''

        self.premodel_data = premodel_data
        self.session_type = session_type
        self.list_error = []
        self.sql = SQL()
        self.db_ml = current_app.config.get('SQL_DB')

    def save(self):
        '''

        This method stores, or updates dataset entities into its corresponding
        'EAV data model' database table.

        @sql_statement, is a sql format string, and not a python string.
            Therefore, '%s' is used for argument substitution.

        Note: 'UTC_TIMESTAMP' returns the universal UTC datetime

        '''

        # insert / update dataset entity value
        self.sql.connect(self.db_ml)

        if self.session_type == 'data_append':
            sql_statement = 'UPDATE tbl_dataset_entity '\
                'SET uid_modified=%s, datetime_modified=UTC_TIMESTAMP() '\
                'WHERE id_entity=%s'
            args = (self.premodel_data['uid'], self.premodel_data['id_entity'])
            response = self.sql.execute('update', sql_statement, args)

        elif self.session_type == 'data_new':
            sql_statement = 'INSERT INTO tbl_dataset_entity '\
                '(title, collection, model_type, uid_created, datetime_created) '\
                'VALUES(%s, %s, %s, %s, UTC_TIMESTAMP())'
            args = (
                self.premodel_data['title'],
                self.premodel_data['collection'],
                self.premodel_data['model_type'],
                self.premodel_data['uid']
            )
            response = self.sql.execute('insert', sql_statement, args)

        # retrieve any error(s)
        response_error = self.sql.get_errors()

        # return result
        if response_error:
            return {'status': False, 'error': response_error}
        else:
            return {'status': True, 'error': None, 'id': response['id']}

    def get_title(self, id_entity):
        '''

        This method is responsible for retrieving an SVM entity title, from the
        SQL database, using a fixed 'id_entity'.

        @id_entity, this supplied argument corresponds to the 'id_entity'
            column from the 'tbl_dataset_value' database table.

        @sql_statement, is a sql format string, and not a python string.
            Therefore, '%s' is used for argument substitution.

        '''

        # select dataset
        self.sql.connect(self.db_ml)
        sql_statement = 'SELECT title '\
            'FROM tbl_dataset_entity '\
            'WHERE id_entity=%s'
        args = (id_entity)
        response = self.sql.execute('select', sql_statement, args)

        # retrieve any error(s)
        response_error = self.sql.get_errors()

        # return result
        if response_error:
            return {'error': response_error, 'result': None}
        else:
            return {'error': None, 'result': response['result']}

    def get_collections(self, uid):
        '''

        This method is responsible for retrieving all collections, for a
        specified user, in ascending order, for the 'datetime_created' column.

        @sql_statement, is a sql format string, and not a python string.
            Therefore, '%s' is used for argument substitution.

        '''

        # select entity
        self.sql.connect(self.db_ml)
        sql_statement = 'SELECT collection '\
            'FROM tbl_dataset_entity '\
            'WHERE uid_created=%s '\
            'ORDER BY datetime_created'
        args = (uid)
        response = self.sql.execute('select', sql_statement, args)

        # retrieve any error(s)
        response_error = self.sql.get_errors()

        # return result
        if response_error:
            return {'error': response_error, 'result': None}
        else:
            return {'error': None, 'result': response['result'][0]}

    def get_collection_count(self, uid):
        '''

        This method is responsible for retrieving the total collection count,
        for a specified user.

        @sql_statement, is a sql format string, and not a python string.
            Therefore, '%s' is used for argument substitution.

        '''

        # select entity
        self.sql.connect(self.db_ml)
        sql_statement = 'SELECT COUNT(*) '\
            'FROM('\
            'SELECT collection '\
            'FROM tbl_dataset_entity '\
            'WHERE uid_created=%s'\
            ') AS c'
        args = (uid)
        response = self.sql.execute('select', sql_statement, args)

        # retrieve any error(s)
        response_error = self.sql.get_errors()

        # return result
        if response_error:
            return {'error': response_error, 'result': None}
        else:
            return {'error': None, 'result': int(response['result'][0][0])}

    def remove_entity(self, uid, collection):
        '''

        This method is responsible deleting an entity, with respect to a
        defined uid, and collection.

        @sql_statement, is a sql format string, and not a python string.
            Therefore, '%s' is used for argument substitution.

        '''

        # delete entity
        self.sql.connect(self.db_ml)
        sql_statement = 'Delete '\
            'FROM tbl_dataset_entity '\
            'WHERE (uid_created=%s AND collection=%s)'
        args = (uid, collection)
        response = self.sql.execute('delete', sql_statement, args)

        # retrieve any error(s)
        response_error = self.sql.get_errors()

        # return result
        if response_error:
            return {'error': response_error, 'result': None}
        else:
            return {'error': None, 'result': int(response['id'])}
Exemple #14
0
class Feature(object):
    '''

    This class provides an interface to retrieve, and store the expected
    number of features that can be expected in a given dataset, and each
    feature instance into corresponding database tables (using EAV data
    model).

    Note: this class is invoked within 'base_data.py'

    Note: this class explicitly inherits the 'new-style' class.

    '''
    def __init__(self, premodel_data=None):
        '''

        This constructor is responsible for defining class variables.

        '''

        self.premodel_data = premodel_data
        self.list_error = []
        self.sql = SQL()
        self.db_ml = current_app.config.get('DB_ML')
        self.classification = current_app.config.get('MODEL_TYPE')[0]
        self.regression = current_app.config.get('MODEL_TYPE')[1]

    def save_count(self):
        '''

        This method stores the number of features that can be expected in a
        given observation.

        @sql_statement, is a sql format string, and not a python string.
            Therefore, '%s' is used for argument substitution.

        '''

        # insert / update dataset value(s)
        self.sql.connect(self.db_ml)
        sql_statement = 'INSERT INTO tbl_feature_count (id_entity, '\
            'count_features) VALUES(%s, %s)'
        args = (
            self.premodel_data['id_entity'],
            self.premodel_data['count_features'],
        )
        response = self.sql.execute(sql_statement, 'insert', args)

        # retrieve any error(s), disconnect from database
        response_error = self.sql.get_errors()
        self.sql.disconnect()

        # return result
        if response_error:
            return {'status': False, 'error': response_error}
        else:
            return {'status': True, 'error': None, 'id': response['id']}

    def save_feature(self, model_type):
        '''

        This method can store, or update an existing SVM dataset stored in
        corresponding database tables (using EAV data model).

        @sql_statement, is a sql format string, and not a python string.
            Therefore, '%s' is used for argument substitution.

        Note: 'UTC_TIMESTAMP' returns the universal UTC datetime

        '''

        # insert / update dataset value(s)
        self.sql.connect(self.db_ml)
        if model_type == self.classification:
            sql_statement = 'INSERT INTO tbl_svm_data (id_entity, '\
                'dep_variable_label, indep_variable_label, '\
                'indep_variable_value) VALUES(%s, %s, %s, %s)'
        elif model_type == self.regression:
            sql_statement = 'INSERT INTO tbl_svr_data (id_entity, '\
                'criterion, indep_variable_label, indep_variable_value) '\
                'VALUES(%s, %s, %s, %s)'
        dataset = self.premodel_data['premodel_dataset']
        args = (
            self.premodel_data['id_entity'],
            dataset['dep_variable_label'],
            dataset['indep_variable_label'],
            dataset['indep_variable_value'],
        )
        response = self.sql.execute(sql_statement, 'insert', args)

        # retrieve any error(s), disconnect from database
        response_error = self.sql.get_errors()
        self.sql.disconnect()

        # return result
        if response_error:
            return {'status': False, 'error': response_error}
        else:
            return {'status': True, 'error': None, 'id': response['id']}

    def get_dataset(self, id_entity, model):
        '''

        This method retrieves a correspondinng dataset, from corresponding
        'EAV data model' database table(s), using a fixed 'id_entity'.

        @id_entity, this supplied argument corresponds to the 'id_entity'
            column from the 'tbl_dataset_value' database table.

        @sql_statement, is a sql format string, and not a python string.
            Therefore, '%s' is used for argument substitution.

        @model, is the model type (i.e. svm, svr)

        '''

        # local variables
        list_model_type = current_app.config.get('MODEL_TYPE')

        # establish connection
        self.sql.connect(self.db_ml)

        # case 1: svm data
        if model == list_model_type[0]:
            sql_statement = 'SELECT dep_variable_label, '\
                'indep_variable_label, indep_variable_value '\
                'FROM tbl_svm_data '\
                'WHERE id_entity=%s'

        # case 2: svr data
        elif model == list_model_type[1]:
            sql_statement = 'SELECT criterion, indep_variable_label, '\
                'indep_variable_value '\
                'FROM tbl_svr_data '\
                'WHERE id_entity=%s'

        # get dataset
        args = (id_entity)
        response = self.sql.execute(sql_statement, 'select', args)

        # retrieve any error(s), disconnect from database
        response_error = self.sql.get_errors()
        self.sql.disconnect()

        # return result
        if response_error:
            return {'status': False, 'error': response_error, 'result': None}
        else:
            return {
                'status': True,
                'error': None,
                'result': response['result'],
            }

    def get_count(self, id_entity):
        '''

        This method retrieves the number of features that can be expected in
        any given observation, from a particular dataset instance (id_entity).

        @id_entity, this supplied argument corresponds to the 'id_entity'
            column from the 'tbl_dataset_value' database table.

        @sql_statement, is a sql format string, and not a python string.
            Therefore, '%s' is used for argument substitution.
        '''

        self.sql.connect(self.db_ml)
        sql_statement = 'SELECT count_features '\
            'FROM tbl_feature_count '\
            'WHERE id_entity=%s'
        args = (id_entity)
        response = self.sql.execute(sql_statement, 'select', args)

        # retrieve any error(s), disconnect from database
        response_error = self.sql.get_errors()
        self.sql.disconnect()

        # return result
        if response_error:
            return {'status': False, 'error': response_error, 'result': None}
        else:
            return {
                'status': True,
                'error': None,
                'result': response['result'],
            }
Exemple #15
0
class Session(object):
    '''

    This class provides an interface to retrieve the 'id_entity', and
    'collection' from the 'tbl_dataset_entity' sql database table.

    Note: this class is invoked within 'views.py'

    Note: this class explicitly inherits the 'new-style' class.

    '''
    def __init__(self):
        '''

        This constructor is responsible for defining class variables.

        '''

        self.list_error = []
        self.sql = SQL()
        self.db_ml = current_app.config.get('SQL_DB')

    def get_session_id(self, collection):
        '''

        This method is responsible for retrieving the 'session_id', given
        that the 'collection' is known.

        '''

        self.sql.connect(self.db_ml)
        sql_statement = 'SELECT id_entity FROM tbl_dataset_entity '\
            'WHERE collection=%s'
        args = (collection)
        response = self.sql.execute('select', sql_statement, args)

        # retrieve any error(s)
        response_error = self.sql.get_errors()

        # return result
        if response_error:
            return {'result': None, 'error': response_error}
        else:
            return {'result': response['result'][0][0], 'error': None}

    def get_all_collections(self):
        '''

        This method is responsible for retrieving all collections from the
        'tbl_dataset_entity' sql database table.

        '''

        # local variables
        list_session = []

        # sql query
        self.sql.connect(self.db_ml)
        sql_statement = 'SELECT id_entity, collection FROM tbl_dataset_entity'
        response = self.sql.execute('select', sql_statement)

        # rebuild session list, get error(s) if any
        if response['result']:
            for item in response['result']:
                list_session.append({'id': item[0], 'collection': item[1]})
            response_error = self.sql.get_errors()
        else:
            response_error = 'no previous collection found in database'

        # return result
        if response_error:
            return {'result': None, 'error': response_error}
        else:
            return {'result': list_session, 'error': None}
Exemple #16
0
class Prediction(object):
    '''

    This class provides an interface to save, or retrieve, a previously
    generated svm or, svr prediction result.

    Note: this class explicitly inherits the 'new-style' class.

    '''
    def __init__(self):
        '''

        This constructor is responsible for defining class variables.

        '''

        self.list_error = []
        self.sql = SQL()
        self.db_ml = current_app.config.get('DB_ML')
        self.model_list = current_app.config.get('MODEL_TYPE')

        if session.get('uid'):
            self.uid = int(session.get('uid'))
        else:
            self.uid = 0

    def save(self, data, model_type, title):
        '''

        This method stores the corresponding prediction.

        @sql_statement, is a sql format string, and not a python string.
            Therefore, '%s' is used for argument substitution.

        Note: 'UTC_TIMESTAMP' returns the universal UTC datetime

        '''

        # local variables
        result = data['result']

        # insert prediction
        self.sql.connect(self.db_ml)

        if model_type == 'svm':
            classes = data['classes']
            probability = data['probability']
            decision_function = data['decision_function']

            # svm results
            sql_statement = 'INSERT INTO tbl_prediction_results '\
                '(model_type, title, result, uid_created, datetime_created) '\
                'VALUES(%s, %s, %s, %s, UTC_TIMESTAMP())'
            args = (self.model_list.index(model_type) + 1, title, result,
                    self.uid)
            svm_results = self.sql.execute(
                sql_statement,
                'insert',
                args,
            )

            # svm classes
            for x in classes:
                sql_statement = 'INSERT INTO tbl_svm_results_class '\
                    '(id_result, class) VALUES(%s, %s)'
                args = (svm_results['id'], x)
                self.sql.execute(
                    sql_statement,
                    'insert',
                    args,
                )

            # svm probability
            for x in probability:
                sql_statement = 'INSERT INTO tbl_svm_results_probability '\
                    '(id_result, probability) VALUES(%s, %s)'
                args = (svm_results['id'], x)
                self.sql.execute(
                    sql_statement,
                    'insert',
                    args,
                )

            # svm decision function
            for x in decision_function:
                sql_statement = 'INSERT INTO tbl_svm_results_decision_function '\
                    '(id_result, decision_function) VALUES(%s, %s)'
                args = (
                    svm_results['id'],
                    x,
                )
                self.sql.execute(
                    sql_statement,
                    'insert',
                    args,
                )

        elif model_type == 'svr':
            # svr results
            sql_statement = 'INSERT INTO tbl_prediction_results '\
                '(model_type, title, result, uid_created, datetime_created) '\
                'VALUES(%s, %s, %s, %s, UTC_TIMESTAMP())'
            args = (self.model_list.index(model_type) + 1, title, result,
                    self.uid)
            svr_results = self.sql.execute(
                sql_statement,
                'insert',
                args,
            )

            # svr r2
            sql_statement = 'INSERT INTO tbl_svr_results_r2 '\
                '(id_result, r2) VALUES(%s, %s)'
            args = (svr_results['id'], data['r2'])
            self.sql.execute(
                sql_statement,
                'insert',
                args,
            )

        # retrieve any error(s), disconnect from database
        response_error = self.sql.get_errors()
        self.sql.disconnect()

        # return result
        if response_error:
            return {'error': response_error, 'result': 1}
        else:
            return {'error': None, 'result': 0}

    def get_all_titles(self, model_type=None):
        '''

        This method retrieves all stored predictions for the current user.

        @model_type, constrains the 'select' result to a specified model type.
            Otherwise, defaults to return results for all model types.

        @sql_statement, is a sql format string, and not a python string.
            Therefore, '%s' is used for argument substitution.

        '''

        # select prediction
        self.sql.connect(self.db_ml)

        if model_type in self.model_list:
            sql_statement = 'SELECT title, datetime_created ' \
                'FROM tbl_prediction_results '\
                'WHERE uid_created=%s '\
                'AND model_type=%s'
            args = (self.uid, self.model_list.index(model_type) + 1)
            response = self.sql.execute(sql_statement, 'select', args)

        elif model_type == 'all':
            sql_statement = 'SELECT title, datetime_created '\
                'FROM tbl_prediction_results '\
                'WHERE uid_created=%s'
            args = (self.uid)
            response = self.sql.execute(sql_statement, 'select', args)

        # retrieve any error(s), disconnect from database
        response_error = self.sql.get_errors()
        self.sql.disconnect()

        # return result
        if response_error:
            return {'status': False, 'error': response_error, 'result': None}
        else:
            return {
                'status': True,
                'error': None,
                'result': response['result'],
            }

    def get_result(self, id_result):
        '''

        This method retrieves a prediction result, based on the supplied
            id_result.

        @sql_statement, is a sql format string, and not a python string.
            Therefore, '%s' is used for argument substitution.

        '''

        # select result
        self.sql.connect(self.db_ml)

        sql_statement = 'SELECT result FROM tbl_prediction_results '\
            'WHERE id_result=%s'
        args = (id_result, )
        response = self.sql.execute(sql_statement, 'select', args)

        # retrieve any error(s), disconnect from database
        response_error = self.sql.get_errors()
        self.sql.disconnect()

        # return result
        if response_error:
            return {'status': False, 'error': response_error, 'result': None}
        else:
            return {
                'status': True,
                'error': None,
                'result': response['result'],
            }

    def get_model_type(self, id_result):
        '''

        This method retrieves the 'model_type' name given the 'id_result'.

        @sql_statement, is a sql format string, and not a python string.
            Therefore, '%s' is used for argument substitution.

        '''

        # select parameter
        self.sql.connect(self.db_ml)

        sql_statement = 'SELECT model FROM tbl_model_type '\
            'WHERE id_model '\
            'IN (SELECT model_type '\
            'FROM tbl_prediction_results '\
            'WHERE id_result=%s)'
        args = (id_result, )
        response = self.sql.execute(sql_statement, 'select', args)

        # retrieve any error(s), disconnect from database
        response_error = self.sql.get_errors()
        self.sql.disconnect()

        # return result
        if response_error:
            return {'status': False, 'error': response_error, 'result': None}

        else:
            return {
                'status': False,
                'error': 'No sql logic executed',
                'result': response['result'][0][0]
            }

    def get_value(self, id_result, model_type, param):
        '''

        This method retrieves a specified param, with respect to a supplied
            id_result, and 'model_type', for a given prediction result.

        @model_type, constrains the 'select' result to a specified model type.

        @param, specifies which table, and corresponding column parameter to
            query, and select from:

            - class: requires svm 'model_type'
            - decision_function: requires svm 'model_type'
            - probability: requires svm 'model_type'
            - r2: requires svr 'model_type'

        @sql_statement, is a sql format string, and not a python string.
            Therefore, '%%s' is used for argument substitution.

        '''

        # select parameter
        self.sql.connect(self.db_ml)

        if model_type in self.model_list:
            if param in ['class', 'decision_function', 'probability', 'r2']:
                sql_statement = 'SELECT %s FROM tbl_%s_results_%s '\
                    'WHERE id_result=%%s' % (param, model_type, param)
                args = (id_result, )
                response = self.sql.execute(sql_statement, 'select', args)

                return {
                    'status': True,
                    'error': None,
                    'result': response['result'],
                }

        # retrieve any error(s), disconnect from database
        response_error = self.sql.get_errors()
        self.sql.disconnect()

        # return result
        if response_error:
            return {'status': False, 'error': response_error, 'result': None}

        else:
            return {
                'status': False,
                'error': 'No sql logic executed',
                'result': None
            }
class Session(object):
    '''

    This class provides an interface to retrieve the 'id_entity', and
    'collection' from the 'tbl_dataset_entity' sql database table.

    Note: this class is invoked within 'views.py'

    Note: this class explicitly inherits the 'new-style' class.

    '''

    def __init__(self):
        '''

        This constructor is responsible for defining class variables.

        '''

        self.list_error = []
        self.sql = SQL()
        self.db_ml = current_app.config.get('SQL_DB')

    def get_session_id(self, collection):
        '''

        This method is responsible for retrieving the 'session_id', given
        that the 'collection' is known.

        '''

        self.sql.connect(self.db_ml)
        sql_statement = 'SELECT id_entity FROM tbl_dataset_entity '\
            'WHERE collection=%s'
        args = (collection)
        response = self.sql.execute('select', sql_statement, args)

        # retrieve any error(s)
        response_error = self.sql.get_errors()

        # return result
        if response_error:
            return {'result': None, 'error': response_error}
        else:
            return {'result': response['result'][0][0], 'error': None}

    def get_all_collections(self):
        '''

        This method is responsible for retrieving all collections from the
        'tbl_dataset_entity' sql database table.

        '''

        # local variables
        list_session = []

        # sql query
        self.sql.connect(self.db_ml)
        sql_statement = 'SELECT id_entity, collection FROM tbl_dataset_entity'
        response = self.sql.execute('select', sql_statement)

        # rebuild session list, get error(s) if any
        if response['result']:
            for item in response['result']:
                list_session.append({'id': item[0], 'collection': item[1]})
            response_error = self.sql.get_errors()
        else:
            response_error = 'no previous collection found in database'

        # return result
        if response_error:
            return {'result': None, 'error': response_error}
        else:
            return {'result': list_session, 'error': None}
class Account(object):
    '''

    This class provides an interface to the users account.

    Note: this class explicitly inherits the 'new-style' class.

    '''

    def __init__(self):
        '''

        This constructor is responsible for defining class variables.

        '''

        self.list_error = []
        self.sql = SQL()
        self.db_ml = current_app.config.get('SQL_DB')

    def save_account(self, username, email, password):
        '''

        This method stores a user account, along with their corresponding
        password into an 'EAV data model' database table.

        @sql_statement, is a sql format string, and not a python string.
            Therefore, '%s' is used for argument substitution.

        '''

        # insert / update dataset entity value
        self.sql.connect(self.db_ml)

        sql_statement = 'INSERT INTO tbl_user '\
            '(username, email, password, datetime_joined) '\
            'VALUES(%s, %s, %s, UTC_TIMESTAMP())'
        args = (username, email, password)
        response = self.sql.execute('insert', sql_statement, args)

        # retrieve any error(s)
        response_error = self.sql.get_errors()

        # return result
        if response_error:
            return {'status': False, 'error': response_error, 'id': None}
        else:
            return {'status': True, 'error': None, 'id': response['id']}

    def check_username(self, username):
        '''

        This method checks if the supplied username already exists.

        '''

        # select dataset
        self.sql.connect(self.db_ml)
        sql_statement = 'SELECT * '\
            'FROM tbl_user '\
            'WHERE username=%s'
        args = (username)
        response = self.sql.execute('select', sql_statement, args)

        # retrieve any error(s)
        response_error = self.sql.get_errors()

        # return result
        if response_error:
            return {'error': response_error, 'result': None}
        else:
            return {'error': None, 'result': response['result']}

    def check_email(self, email):
        '''

        This method checks if the supplied email already exists.

        '''

        # select dataset
        self.sql.connect(self.db_ml)
        sql_statement = 'SELECT * '\
            'FROM tbl_user '\
            'WHERE email=%s'
        args = (email)
        response = self.sql.execute('select', sql_statement, args)

        # retrieve any error(s)
        response_error = self.sql.get_errors()

        # return result
        if response_error:
            return {'error': response_error, 'result': None}
        else:
            return {'error': None, 'result': response['result']}

    def get_password(self, username):
        '''

        This method returns the hashed password for a supplied username.

        '''

        # select dataset
        self.sql.connect(self.db_ml)
        sql_statement = 'SELECT password '\
            'FROM tbl_user '\
            'WHERE username=%s'
        args = (username)
        response = self.sql.execute('select', sql_statement, args)

        # retrieve any error(s)
        response_error = self.sql.get_errors()

        # return result
        if response_error:
            return {'error': response_error, 'result': None}
        else:
            return {'error': None, 'result': response['result'][0][0]}

    def get_uid(self, username):
        '''

        This method returns the userid (i.e uid) for a supplied username.

        '''

        # select dataset
        self.sql.connect(self.db_ml)
        sql_statement = 'SELECT id_user '\
            'FROM tbl_user '\
            'WHERE username=%s'
        args = (username)
        response = self.sql.execute('select', sql_statement, args)

        # retrieve any error(s)
        response_error = self.sql.get_errors()

        # return result
        if response_error:
            return {'error': response_error, 'result': None}
        elif not response['result']:
            return {'error': 'no uid', 'result': None}
        else:
            return {'error': None, 'result': response['result'][0][0]}
Exemple #19
0
class Observation(object):
    '''

    This class provides an interface to store observation labels, provided
    from corresponding dataset(s) into corresponding database tables.

    Note: this class is invoked within 'base_data.py'

    Note: this class explicitly inherits the 'new-style' class.

    '''
    def __init__(self, premodel_data, session_type):
        '''

        This constructor is responsible for defining class variables.

        '''

        # class variables
        self.premodel_data = premodel_data
        self.session_type = session_type
        self.list_error = []
        self.sql = SQL()
        self.db_ml = current_app.config.get('DB_ML')

    def save_label(self):
        '''

        This method can store, or update an existing set of observation labels
        in corresponding database tables (using EAV data model).

        @sql_statement, is a sql format string, and not a python string.
            Therefore, '%s' is used for argument substitution.

        Note: 'UTC_TIMESTAMP' returns the universal UTC datetime

        '''

        # insert / update feature label(s)
        self.sql.connect(self.db_ml)

        # add labels (append case)
        if self.session_type in ['data_append', 'data_new']:

            # check if observation label exists in database
            sql_statement = 'SELECT * FROM tbl_observation_label '\
                'WHERE dep_variable_label=%s '\
                'AND id_entity=%s'
            args = (self.premodel_data['label'],
                    self.premodel_data['id_entity'])
            response = self.sql.execute(sql_statement, 'select', args)

            # add labels if not exist
            if not response['result']:
                sql_statement = 'INSERT INTO tbl_observation_label '\
                    '(id_entity, dep_variable_label) VALUES(%s, %s)'
                args = (self.premodel_data['id_entity'],
                        self.premodel_data['label'])
                self.sql.execute(
                    sql_statement,
                    'insert',
                    args,
                )

        # retrieve any error(s), disconnect from database
        response_error = self.sql.get_errors()
        self.sql.disconnect()

        # return result
        if response_error:
            return {'status': False, 'error': response_error}
        else:
            return {'status': True, 'error': None}