def save_to_db(self, testing=False, postgresql=None):

        def insert_into_db(a_cursor):
            a_cursor.execute(
                "INSERT INTO experiments(experiment_dir, experiment_name) VALUES(%s, %s);",
                (self.experiment_dir, self.experiment_name))

        def update_db_entry(a_cursor):
            a_cursor.execute(
                "UPDATE experiments SET (experiment_name, experiment_dir) = (%s, %s) WHERE experiment_id = %s;",
                (self.experiment_name, self.experiment_dir, self.experiment_id))

        def save_to_db_main(a_cursor):
            if self.experiment_name not in list_all_experiment_names(a_cursor):
                insert_into_db(a_cursor)
                return self.from_db(experiment_name=self.experiment_name)
            elif self == self.from_db(experiment_name=self.experiment_name):
                return self
            else:
                update_db_entry(a_cursor)
                return self.from_db(experiment_name=self.experiment_name)

        if testing:
            with TestingCursor(postgresql) as cursor:
                return save_to_db_main(cursor)
        else:
            with Cursor() as cursor:
                return save_to_db_main(cursor)
示例#2
0
    def from_db(cls, session_id=None, session_dir=None, testing=False, postgresql=None):

        def by_id(a_cursor, a_session_id):
            a_cursor.execute("SELECT * FROM sessions WHERE session_id = %s;", (a_session_id,))
            return a_cursor.fetchone()

        def by_dir(a_cursor, a_session_dir):
            a_cursor.execute("SELECT * FROM sessions WHERE session_dir = %s;", (a_session_dir,))
            return a_cursor.fetchone()

        def from_db_main(a_cursor, a_session_id, a_session_dir):
            if a_session_id is not None:
                session_data = by_id(a_cursor, a_session_id)
            elif a_session_dir is not None:
                session_data = by_dir(a_cursor, a_session_dir)
            else:
                session_data = None

            if session_data is None:
                return None
            return cls(mouse_id=session_data[1], experiment_id=session_data[2], session_date=session_data[3],
                       session_dir=session_data[4], session_num=session_data[5], session_id=session_data[0])

        if testing:
            with TestingCursor(postgresql) as cursor:
                return from_db_main(cursor, session_id, session_dir)
        else:
            with Cursor() as cursor:
                return from_db_main(cursor, session_id, session_dir)
示例#3
0
    def save_to_db(self, testing=False, postgresql=None):

        def insert_into_db(a_cursor):
            a_cursor.execute("INSERT INTO sessions (mouse_id, experiment_id, session_date, session_dir, session_num) "
                             "VALUES (%s, %s, %s, %s, %s);",
                             (self.mouse_id, self.experiment_id, self.session_date, self.session_dir, self.session_num))

        def update_db_entry(a_cursor):
            a_cursor.execute("UPDATE sessions "
                             "SET (session_date, session_dir, session_num) = (%s, %s, %s) "
                             "WHERE session_id = %s;",
                             (self.session_date, self.session_dir, self.session_num, self.session_id))

        def save_to_db_main(a_cursor):
            return_session_id = self.from_db(session_id=self.session_id)
            return_session_dir = self.from_db(session_dir=self.session_dir)
            if self == return_session_id:
                return self
            elif return_session_id is None and return_session_dir is None:
                insert_into_db(a_cursor)
                return self.from_db(session_dir=self.session_dir)
            else:
                update_db_entry(a_cursor)
                return self.from_db(session_dir=self.session_dir)

        if testing:
            with TestingCursor(postgresql) as cursor:
                return save_to_db_main(cursor)
        else:
            with Cursor() as cursor:
                return save_to_db_main(cursor)
    def from_db(cls, experiment_name=None, experiment_id=None, testing=False, postgresql=None):
        experiment_name = util.prep_string_for_db(experiment_name)

        def by_experiment_name(a_cursor, exp_name):
            a_cursor.execute("SELECT * FROM experiments WHERE experiment_name = %s;", (exp_name,))
            return a_cursor.fetchone()

        def by_id(a_cursor, exp_id):
            a_cursor.execute("SELECT * FROM experiments WHERE experiment_id = %s;", (exp_id,))
            return a_cursor.fetchone()

        def from_db_main(a_cursor, exp_name, exp_id):
            if exp_name is not None:
                exp = by_experiment_name(a_cursor, exp_name)
            elif exp_id is not None:
                exp = by_id(a_cursor, exp_id)
            else:
                exp = None

            if exp is None:
                return None
            return cls(experiment_name=exp[2], experiment_dir=exp[1],
                       experiment_id=exp[0])

        if testing:
            with TestingCursor(postgresql) as cursor:
                return from_db_main(cursor, experiment_name, experiment_id)
        else:
            with Cursor() as cursor:
                return from_db_main(cursor, experiment_name, experiment_id)
示例#5
0
    def save_to_db(self, testing=False, postgresql=None):
        def insert_into_db(a_cursor):
            a_cursor.execute(
                "INSERT INTO trials (experiment_id, folder_id, trial_dir, trial_date) VALUES (%s, %s, %s, %s);",
                (self.experiment_id, self.folder_id, self.trial_dir,
                 util.convert_date_int_yyyymmdd(self.trial_date)))

        def update_db_entry(a_cursor):
            a_cursor.execute(
                "UPDATE trials "
                "SET (trial_dir, trial_date) = (%s, %s) "
                "WHERE trial_id = %s;",
                (self.trial_dir, self.trial_date, self.trial_id))

        def save_to_db_main(a_cursor):
            return_trial_id = self.from_db(trial_id=self.trial_id)
            return_trial_dir = self.from_db(trial_dir=self.trial_dir)
            if self == return_trial_id:
                return self
            elif return_trial_id is None and return_trial_dir is None:
                insert_into_db(a_cursor)
                return self.from_db(trial_dir=self.trial_dir)
            else:
                update_db_entry(a_cursor)
                return self.from_db(trial_dir=self.trial_dir)

        if testing:
            with TestingCursor(postgresql) as cursor:
                return save_to_db_main(cursor)
        else:
            with Cursor() as cursor:
                return save_to_db_main(cursor)
    def save_to_db(self, testing=False, postgresql=None):
        def insert_into_db(a_cursor):
            a_cursor.execute(
                "INSERT INTO blind_folders (folder_id, reviewer_id, blind_name) "
                "VALUES (%s, %s, %s);",
                (self.folder_id, self.reviewer_id, self.blind_name))

        def update_db_entry(a_cursor):
            a_cursor.execute(
                "UPDATE blind_folders "
                "SET (reviewer_id, blind_name) = (%s, %s) "
                "WHERE blind_folder_id = %s;",
                (self.reviewer_id, self.blind_name, self.folder_id))

        def save_to_db_main(a_cursor):
            return_blind_folder_id = self.from_db(
                blind_folder_id=self.blind_folder_id)
            return_blind_name = self.from_db(blind_name=self.blind_name)
            if self == return_blind_folder_id:
                return self.from_db(blind_name=self.blind_name)
            elif return_blind_folder_id is None and return_blind_name is None:
                insert_into_db(a_cursor)
                return self.from_db(blind_name=self.blind_name)
            else:
                update_db_entry(a_cursor)
                return self.from_db(blind_name=self.blind_name)

        if testing:
            with TestingCursor(postgresql) as cursor:
                return save_to_db_main(cursor)
        else:
            with Cursor() as cursor:
                return save_to_db_main(cursor)
示例#7
0
    def from_db(cls, eartag, experiment_name, testing=False, postgresql=None):
        def from_db_main(a_cursor, a_mouse, a_experiment):
            a_cursor.execute(
                "SELECT * FROM participant_details WHERE mouse_id = %s AND experiment_id = %s;",
                (a_mouse.mouse_id, a_experiment.experiment_id))
            participant_details = cursor.fetchone()
            if participant_details is None:
                return None
            return cls(mouse,
                       experiment,
                       participant_dir=participant_details[6],
                       start_date=participant_details[3],
                       end_date=participant_details[4],
                       exp_spec_details=participant_details[5],
                       detail_id=participant_details[0])

        if testing:
            with TestingCursor(postgresql) as cursor:
                mouse = Mouse.from_db(eartag,
                                      testing=testing,
                                      postgresql=postgresql)
                experiment = Experiment.from_db(experiment_name,
                                                testing=testing,
                                                postgresql=postgresql)
                return from_db_main(cursor, mouse, experiment)
        else:
            with Cursor() as cursor:
                mouse = Mouse.from_db(eartag)
                experiment = Experiment.from_db(experiment_name)
                return from_db_main(cursor, mouse, experiment)
示例#8
0
    def from_db(cls, eartag=None, mouse_id=None, testing=False, postgresql=None):

        def by_eartag(a_cursor, a_eartag):
            a_cursor.execute("SELECT * FROM mouse WHERE eartag = %s;", (a_eartag,))
            return a_cursor.fetchone()

        def by_id(a_cursor, a_mouse_id):
            a_cursor.execute("SELECT * FROM mouse WHERE mouse_id = %s;", (a_mouse_id,))
            return a_cursor.fetchone()

        def from_db_main(a_cursor, a_eartag, a_mouse_id):
            if eartag is not None:
                mouse_data = by_eartag(a_cursor, a_eartag)
            elif mouse_id is not None:
                mouse_data = by_id(a_cursor, a_mouse_id)
            else:
                mouse_data = None

            if mouse_data is None:
                return None
            return cls(eartag=mouse_data[1], birthdate=mouse_data[2],
                       genotype=util.decode_genotype(mouse_data[3]), sex=mouse_data[4], mouse_id=mouse_data[0])

        if testing:
            with TestingCursor(postgresql) as cursor:
                return from_db_main(cursor, eartag, mouse_id)
        else:
            with Cursor() as cursor:
                return from_db_main(cursor, eartag, mouse_id)
示例#9
0
    def save_to_db(self, testing=False, postgresql=None):

        def insert_into_db(a_cursor):
            a_cursor.execute("INSERT INTO mouse (eartag, birthdate, genotype, sex) VALUES (%s, %s, %s, %s);",
                             (self.eartag, self.birthdate, util.encode_genotype(self.genotype), self.sex))

        def update_db_entry(a_cursor):
            a_cursor.execute("UPDATE mouse "
                             "SET (birthdate, genotype, sex) = (%s, %s, %s) "
                             "WHERE eartag = %s;",
                             (self.birthdate, self.genotype, self.sex, self.eartag))

        def save_to_db_main(a_cursor):
            if self.from_db(eartag=self.eartag) is None:
                insert_into_db(a_cursor)
                return self.from_db(eartag=self.eartag)
            elif self == self.from_db(eartag=self.eartag):
                return self
            else:
                update_db_entry(a_cursor)
                return self.from_db(eartag=self.eartag)

        if testing:
            with TestingCursor(postgresql) as cursor:
                if self.from_db(eartag=self.eartag) is None:
                    return save_to_db_main(cursor)
        else:
            with Cursor() as cursor:
                return save_to_db_main(cursor)
    def save_to_db(self, testing=False, postgresql=None):
        def insert_into_db(a_cursor):
            a_cursor.execute(
                "INSERT INTO blind_trials (trial_id, folder_id, full_path) VALUES (%s, %s, %s);",
                (self.trial_id, self.folder_id, str(self.full_path)))

        def update_db_entry(a_cursor):
            a_cursor.execute(
                "UPDATE blind_trials "
                "SET (trial_id, folder_id, full_path) = (%s, %s, %s) "
                "WHERE blind_trial_id = %s;",
                (self.trial_id, self.folder_id, str(
                    self.full_path), self.blind_trial_id))

        def save_to_db_main(a_cursor):
            return_blind_trial_id = self.from_db(
                blind_trial_id=self.blind_trial_id)
            return_full_path = self.from_db(full_path=self.full_path)
            if self == return_blind_trial_id:
                return self
            elif return_blind_trial_id is None and return_full_path is None:
                insert_into_db(a_cursor)
                return self.from_db(full_path=self.full_path)
            else:
                update_db_entry(a_cursor)
                return self.from_db(full_path=self.full_path)

        if testing:
            with TestingCursor(postgresql) as cursor:
                return save_to_db_main(cursor)
        else:
            with Cursor() as cursor:
                return save_to_db_main(cursor)
示例#11
0
    def from_db(cls,
                session_id=None,
                reviewer_id=None,
                session_score_count_id=None,
                testing=False,
                postgresql=None):
        def by_id(a_cursor, a_session_score_count_id):
            a_cursor.execute(
                "SELECT * FROM session_score_counts WHERE session_score_count_id = %s;",
                (a_session_score_count_id, ))
            return a_cursor.fetchone()

        def by_session_reviewer(a_cursor, a_session_id, a_reviewer_id):
            a_cursor.execute(
                "SELECT * FROM session_score_counts WHERE session_id = %s AND reviewer_id = %s;",
                (a_session_id, a_reviewer_id))
            return a_cursor.fetchone()

        def from_db_main(a_cursor, a_session_id, a_reviewer_id,
                         a_session_score_count_id):
            if session_score_count_id is not None:
                session_score_count_data = by_id(a_cursor,
                                                 a_session_score_count_id)
            elif session_id is not None and reviewer_id is not None:
                session_score_count_data = by_session_reviewer(
                    a_cursor, a_session_id, a_reviewer_id)
            else:
                session_score_count_data = None

            if session_score_count_data is None:
                return None
            a_session_score_count_id, a_session_id, a_reviewer_id, score0, score1, score2, score3, score4, score5, \
            score6, score7, score8, score9, abnormal_movt_score, grooming_score = session_score_count_data
            return cls(session_id=a_session_id,
                       reviewer_id=a_reviewer_id,
                       score0=score0,
                       score1=score1,
                       score2=score2,
                       score3=score3,
                       score4=score4,
                       score5=score5,
                       score6=score6,
                       score7=score7,
                       score8=score8,
                       score9=score9,
                       abnormal_movt_score=abnormal_movt_score,
                       grooming_score=grooming_score)

        if testing:
            with TestingCursor(postgresql) as cursor:
                return from_db_main(cursor, session_id, reviewer_id,
                                    session_score_count_id)
        else:
            with Cursor() as cursor:
                return from_db_main(cursor, session_id, reviewer_id,
                                    session_score_count_id)
    def delete_from_db(self, testing=False, postgresql=None):

        def delete_from_db_main(a_cursor, experiment_id):
            a_cursor.execute("DELETE FROM experiments WHERE experiment_id = %s", (experiment_id,))

        if testing:
            with TestingCursor(postgresql) as cursor:
                delete_from_db_main(cursor, self.experiment_id)
        else:
            with Cursor() as cursor:
                delete_from_db_main(cursor, self.experiment_id)
示例#13
0
    def delete_from_db(self, testing=False, postgresql=None):

        def delete_from_db_main(a_cursor):
            a_cursor.execute("DELETE FROM mouse WHERE mouse_id = %s", (self.mouse_id,))

        if testing:
            with TestingCursor(postgresql) as cursor:
                delete_from_db_main(cursor)
        else:
            with Cursor() as cursor:
                delete_from_db_main(cursor)
示例#14
0
    def delete_from_db(self, testing=False, postgresql=None):
        def delete_from_db_main(a_cursor):
            a_cursor.execute(
                "DELETE FROM session_score_counts WHERE session_score_count_id = %s",
                (self.session_score_count_id, ))

        if testing:
            with TestingCursor(postgresql) as cursor:
                delete_from_db_main(cursor)
        else:
            with Cursor() as cursor:
                delete_from_db_main(cursor)
示例#15
0
    def delete_from_db(self, testing=False, postgresql=None):
        def delete_from_db_main(a_cursor):
            a_cursor.execute(
                "DELETE FROM participant_details WHERE detail_id = %s",
                (self.detail_id, ))

        if testing:
            with TestingCursor(postgresql) as cursor:
                delete_from_db_main(cursor)
        else:
            with Cursor() as cursor:
                delete_from_db_main(cursor)
    def from_db(cls,
                full_path=None,
                blind_trial_id=None,
                reviewer_id=None,
                trial_id=None,
                testing=False,
                postgresql=None):
        def by_full_path(a_cursor, a_full_path):
            a_cursor.execute("SELECT * FROM blind_trials WHERE full_path = %s",
                             (a_full_path, ))
            return a_cursor.fetchone()

        def by_id(a_cursor, a_blind_trial_id):
            a_cursor.execute(
                "SELECT * FROM blind_trials WHERE blind_trial_id = %s;",
                (a_blind_trial_id, ))
            return a_cursor.fetchone()

        def by_reviewer_trial_ids(a_cursor, a_reviewer_id, a_trial_id):
            a_cursor.execute(
                "SELECT * FROM blind_trials_all_upstream_ids WHERE reviewer_id = %s and trial_id = %s;",
                (a_reviewer_id, a_trial_id))
            return a_cursor.fetchone()

        def from_db_main(a_cursor, a_full_path, a_blind_trial_id,
                         a_reviewer_id, a_trial_id):
            a_full_path = str(a_full_path)
            if a_blind_trial_id is not None:
                blind_trial_data = by_id(a_cursor, a_blind_trial_id)
            elif full_path is not None:
                blind_trial_data = by_full_path(a_cursor, a_full_path)
            elif reviewer_id is not None and trial_id is not None:
                blind_trial_data = by_reviewer_trial_ids(
                    a_cursor, a_reviewer_id, a_trial_id)
            else:
                blind_trial_data = None

            if blind_trial_data is None:
                return None
            return cls(trial_id=blind_trial_data[1],
                       folder_id=blind_trial_data[2],
                       full_path=blind_trial_data[3],
                       blind_trial_id=blind_trial_data[0])

        if testing:
            with TestingCursor(postgresql) as cursor:
                return from_db_main(cursor, full_path, blind_trial_id,
                                    reviewer_id, trial_id)
        else:
            with Cursor() as cursor:
                return from_db_main(cursor, full_path, blind_trial_id,
                                    reviewer_id, trial_id)
    def from_db(cls,
                blind_name=None,
                blind_folder_id=None,
                reviewer_id=None,
                folder_id=None,
                testing=False,
                postgresql=None):
        def by_reviewer_folder(a_cursor, a_reviewer_id, a_folder_id):
            a_cursor.execute(
                "SELECT * FROM blind_folders WHERE reviewer_id = %s AND folder_id = %s;",
                (a_reviewer_id, a_folder_id))
            return a_cursor.fetchone()

        def by_blind_name(a_cursor, a_blind_name):
            a_cursor.execute(
                "SELECT * FROM blind_folders WHERE blind_name = %s;",
                (a_blind_name, ))
            return a_cursor.fetchone()

        def by_id(a_cursor, a_blind_folder_id):
            a_cursor.execute(
                "SELECT * FROM blind_folders WHERE blind_folder_id = %s;",
                (a_blind_folder_id, ))
            return a_cursor.fetchone()

        def from_db_main(a_cursor, a_blind_name, a_blind_folder_id,
                         a_reviewer_id, a_folder_id):
            if a_blind_folder_id is not None:
                blind_folder_data = by_id(a_cursor, a_blind_folder_id)
            elif a_blind_name is not None:
                blind_folder_data = by_blind_name(a_cursor, a_blind_name)
            elif a_reviewer_id is not None and a_cursor is not None:
                blind_folder_data = by_reviewer_folder(a_cursor, a_reviewer_id,
                                                       a_folder_id)
            else:
                blind_folder_data = None

            if blind_folder_data is None:
                return None
            return cls(folder_id=blind_folder_data[1],
                       reviewer_id=blind_folder_data[2],
                       blind_name=blind_folder_data[3],
                       blind_folder_id=blind_folder_data[0])

        if testing:
            with TestingCursor(postgresql) as cursor:
                return from_db_main(cursor, blind_name, blind_folder_id,
                                    reviewer_id, folder_id)
        else:
            with Cursor() as cursor:
                return from_db_main(cursor, blind_name, blind_folder_id,
                                    reviewer_id, folder_id)
def create_views_main(db_details, main_user):
    Database.initialize(database=db_details['database'],
                        host=db_details['host'],
                        port=5432,
                        user=main_user['user'],
                        password=main_user['password'])
    with Cursor() as cursor:
        create_view_all_participants_all_experiments(cursor)
        create_view_folders_all_upstream_ids(cursor)
        create_view_trials_all_upstream_ids(cursor)
        create_view_blind_folders_all_upstream_ids(cursor)
        create_view_blind_trials_all_upstream_ids(cursor)
        create_view_trial_scores_all_ids(cursor)
示例#19
0
    def save_to_db(self, testing=False, postgresql=None):
        def insert_into_db(a_cursor):
            a_cursor.execute(
                "INSERT INTO session_score_counts (session_id, reviewer_id, score0, score1, score2, "
                "score3, score4, score5, score6, score7, score8, score9, abnormal_movt_score, "
                "grooming_score) "
                "VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s);",
                (self.session_id, self.reviewer_id, int(self.score0),
                 int(self.score1), int(self.score2), int(self.score3),
                 int(self.score4), int(self.score5), int(self.score6),
                 int(self.score7), int(self.score8), int(self.score9),
                 int(self.abnormal_movt_score), int(self.grooming_score)))

        def update_db_entry(a_cursor):
            a_cursor.execute(
                "UPDATE session_score_counts "
                "SET (score0, score1, score2, score3, score4, score5, score6, score7, score8, score9, "
                "abnormal_movt_score, grooming_score) "
                "  = (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s) "
                "WHERE session_id = %s AND reviewer_id = %s;",
                (int(self.score0), int(self.score1), int(self.score2),
                 int(self.score3), int(self.score4), int(self.score5),
                 int(self.score6), int(self.score7), int(self.score8),
                 int(self.score9), int(self.abnormal_movt_score),
                 int(self.grooming_score), self.session_id, self.reviewer_id))

        def save_to_db_main(a_cursor):
            if self.from_db(session_id=self.session_id,
                            reviewer_id=self.reviewer_id) is None:
                insert_into_db(a_cursor)
                return self.from_db(session_id=self.session_id,
                                    reviewer_id=self.reviewer_id)
            elif self == self.from_db(session_id=self.session_id,
                                      reviewer_id=self.reviewer_id):
                return self
            else:
                update_db_entry(a_cursor)
                return self.from_db(session_id=self.session_id,
                                    reviewer_id=self.reviewer_id)

        if testing:
            with TestingCursor(postgresql) as cursor:
                return save_to_db_main(cursor)
        else:
            with Cursor() as cursor:
                return save_to_db_main(cursor)
示例#20
0
    def from_db(cls,
                trial_id=None,
                reviewer_id=None,
                trial_score_id=None,
                testing=False,
                postgresql=None):
        def by_id(a_cursor, a_trial_score_id):
            a_cursor.execute(
                "SELECT * FROM trial_scores WHERE trial_score_id = %s;",
                (a_trial_score_id, ))
            return a_cursor.fetchone()

        def by_trial_reviewer(a_cursor, a_trial_id, a_reviewer_id):
            a_cursor.execute(
                "SELECT * FROM trial_scores WHERE trial_id = %s AND reviewer_id = %s;",
                (a_trial_id, a_reviewer_id))
            return a_cursor.fetchone()

        def from_db_main(a_cursor, a_trial_id, a_reviewer_id,
                         a_trial_score_id):
            if trial_score_id is not None:
                trial_score_data = by_id(a_cursor, a_trial_score_id)
            elif trial_id is not None and reviewer_id is not None:
                trial_score_data = by_trial_reviewer(a_cursor, a_trial_id,
                                                     a_reviewer_id)
            else:
                trial_score_data = None

            if trial_score_data is None:
                return None
            return cls(trial_id=trial_score_data[1],
                       reviewer_id=trial_score_data[2],
                       trial_num=trial_score_data[3],
                       reach_score=trial_score_data[4],
                       abnormal_movt_score=trial_score_data[5],
                       grooming_score=trial_score_data[6],
                       trial_score_id=trial_score_data[0])

        if testing:
            with TestingCursor(postgresql) as cursor:
                return from_db_main(cursor, trial_id, reviewer_id,
                                    trial_score_id)
        else:
            with Cursor() as cursor:
                return from_db_main(cursor, trial_id, reviewer_id,
                                    trial_score_id)
def create_all_tables_main(db_details, main_user):
    Database.initialize(database=db_details['database'],
                        host=db_details['host'],
                        port=5432,
                        user=main_user['user'],
                        password=main_user['password'])
    with Cursor() as cursor:
        cursor.execute("CREATE EXTENSION IF NOT EXISTS \"uuid-ossp\";")
        create_mouse_table(cursor)
        create_experiments_table(cursor)
        create_participant_details_table(cursor)
        create_sessions_table(cursor)
        create_folders_table(cursor)
        create_trials_table(cursor)
        create_reviewers_table(cursor)
        create_blind_folders_table(cursor)
        create_blind_trials_table(cursor)
        create_trial_score_table(cursor)
示例#22
0
    def save_to_db(self, testing=False, postgresql=None):
        def insert_into_db(a_cursor):
            a_cursor.execute(
                "INSERT INTO trial_scores (trial_id, reviewer_id, trial_num, "
                "  reach_score, abnormal_movt_score, grooming_score) "
                "VALUES (%s, %s, %s, %s, %s, %s);",
                (self.trial_id, self.reviewer_id, int(self.trial_num),
                 int(self.reach_score), bool(
                     self.abnormal_movt_score), bool(self.grooming_score)))

        def update_db_entry(a_cursor):
            a_cursor.execute(
                "UPDATE trial_scores "
                "SET (trial_num, reach_score, abnormal_movt_score, grooming_score) "
                "  = (%s, %s, %s, %s) "
                "WHERE trial_id = %s AND reviewer_id = %s;",
                (int(self.trial_num), int(self.reach_score),
                 bool(self.abnormal_movt_score), bool(
                     self.grooming_score), self.trial_id, self.reviewer_id))

        def save_to_db_main(a_cursor):
            if self.from_db(trial_id=self.trial_id,
                            reviewer_id=self.reviewer_id) is None:
                insert_into_db(a_cursor)
                return self.from_db(trial_id=self.trial_id,
                                    reviewer_id=self.reviewer_id)
            elif self == self.from_db(trial_id=self.trial_id,
                                      reviewer_id=self.reviewer_id):
                return self
            else:
                update_db_entry(a_cursor)
                return self.from_db(trial_id=self.trial_id,
                                    reviewer_id=self.reviewer_id)

        if testing:
            with TestingCursor(postgresql) as cursor:
                return save_to_db_main(cursor)
        else:
            with Cursor() as cursor:
                return save_to_db_main(cursor)
示例#23
0
    def save_to_db(self, testing=False, postgresql=None):
        def insert_into_db(a_cursor):
            a_cursor.execute(
                "INSERT INTO participant_details "
                "(mouse_id, experiment_id, start_date, end_date, exp_spec_details, participant_dir) "
                "VALUES (%s, %s, %s, %s, %s, %s);",
                (self.mouse.mouse_id, self.experiment.experiment_id,
                 self.start_date, self.end_date, Json(
                     self.exp_spec_details), self.participant_dir))

        def update_db_entry(a_cursor):
            a_cursor.execute(
                "UPDATE participant_details "
                "SET (start_date, end_date, exp_spec_details, participant_dir) = (%s, %s, %s, %s) "
                "WHERE detail_id = %s;",
                (self.start_date, self.end_date, Json(self.exp_spec_details),
                 self.participant_dir, self.detail_id))

        def save_to_db_main(a_cursor):
            if self.from_db(self.mouse.eartag,
                            self.experiment.experiment_name) is None:
                insert_into_db(a_cursor)
                return self.from_db(self.mouse.eartag,
                                    self.experiment.experiment_name)
            elif self == self.from_db(self.mouse.eartag,
                                      self.experiment.experiment_name):
                return self
            else:
                update_db_entry(a_cursor)
                return self.from_db(self.mouse.eartag,
                                    self.experiment.experiment_name)

        if testing:
            with TestingCursor(postgresql) as cursor:
                return save_to_db_main(cursor)
        else:
            with Cursor() as cursor:
                return save_to_db_main(cursor)
示例#24
0
    def from_db(cls,
                trial_dir=None,
                trial_id=None,
                testing=False,
                postgresql=None):
        def by_dir(a_cursor, a_trial_dir):
            a_cursor.execute("SELECT * FROM trials WHERE trial_dir = %s",
                             (a_trial_dir, ))
            return a_cursor.fetchone()

        def by_id(a_cursor, a_trial_id):
            a_cursor.execute("SELECT * FROM trials WHERE trial_id = %s",
                             (a_trial_id, ))
            return a_cursor.fetchone()

        def from_db_main(a_cursor, a_trial_dir, a_trial_id):
            if a_trial_id is not None:
                trial_data = by_id(a_cursor, a_trial_id)
            elif a_trial_dir is not None:
                trial_data = by_dir(a_cursor, a_trial_dir)
            else:
                trial_data = None

            if trial_data is None:
                return None
            return cls(experiment_id=trial_data[1],
                       folder_id=trial_data[2],
                       trial_dir=trial_data[3],
                       trial_date=trial_data[4],
                       trial_id=trial_data[0])

        if testing:
            with TestingCursor(postgresql) as cursor:
                return from_db_main(cursor, trial_dir, trial_id)
        else:
            with Cursor() as cursor:
                return from_db_main(cursor, trial_dir, trial_id)
示例#25
0
def list_all_blind_folder_ids(experiment):
    with Cursor() as cursor:
        return queries.list_all_blind_folder_ids(cursor, experiment.experiment_id)
示例#26
0
def list_all_blind_names_for_reviewer_experiment(reviewer, experiment):
    with Cursor() as cursor:
        return queries.list_all_blind_names_for_reviewer_experiment(cursor, reviewer.reviewer_id,
                                                                    experiment.experiment_id)
示例#27
0
def list_all_blinded_trial_full_paths_for_reviewer_experiment(reviewer, experiment):
    with Cursor() as cursor:
        return queries.list_all_blind_trials_full_paths_for_reviewer_experiment(cursor, reviewer.reviewer_id,
                                                                                experiment.experiment_id)
示例#28
0
def list_all_session_dir_for_experiment(experiment):
    with Cursor() as cursor:
        return queries.list_all_session_dir_for_for_experiment(cursor, experiment.experiment_id)
示例#29
0
def list_engaged_reaches_by_mouse_session():
    with Cursor() as cursor:
        return queries.list_engaged_reaches_by_mouse_session(cursor)
示例#30
0
def list_all_trial_dir(experiment):
    with Cursor() as cursor:
        return queries.list_all_trial_dirs_for_experiment(cursor, experiment.experiment_id)