Commit 9a140f0f authored by Pauline Pommeret's avatar Pauline Pommeret

Learning PG Cursor

parent a3fa15b7
...@@ -5,6 +5,151 @@ ...@@ -5,6 +5,151 @@
import psycopg2 import psycopg2
import psycopg2.extras import psycopg2.extras
import itertools
class LearningPGCursor(object):
"""
A PG Cursor dedicated to learning functionalities (we use
standard Cursor instead of DictCursor, which order of result
is not guaranteed.
"""
def __init__(self):
"""
Starts a psycopg2 cursor and populates:
- ``self._conn``
- ``self._cur``
Parameters:
- ``self``
Returns:
- nothing
"""
self._conn = psycopg2.connect(database='itpp')
self._cur = self._conn.cursor()
def fetch_averaged_specific_sequence(self, seq_id):
# XXX meh.
data_list = []
data_list.extend(self.fetch_trx_data(seq_id))
data_list.extend(self.fetch_averaged_md_data(seq_id))
data_list.extend(self.fetch_averaged_correlations(seq_id))
return data_list
def fetch_averaged_sequence_data(self):
"""
"""
seq_query = """SELECT
id, label, name
FROM
sequences
ORDER BY
id
ASC;"""
self._cur.execute(seq_query)
sequences = self._cur.fetchall()
possible_answers = list(set([seq[1] for seq in sequences]))
answers = [possible_answers.index(seq[1]) for seq in sequences]
data_list = []
i = 0
for sequence in sequences:
seq_id = sequence[0]
data_list.append([])
data_list[i].extend(self.fetch_trx_data(seq_id))
data_list[i].extend(self.fetch_averaged_md_data(seq_id))
data_list[i].extend(self.fetch_averaged_correlations(seq_id))
i += 1
return data_list, answers, possible_answers
def fetch_averaged_md_data(self, seq_id):
"""
"""
data_list = []
for md_type in ["rise", "roll", "shift", "slide", "tilt", "twist"]:
md_query = """SELECT
avg(complete_peak_freq), avg(complete_peak), avg(complete_size), avg(center_peak_freq),\
avg(center_peak), avg(center_size), avg(sliding_peak_freq), avg(sliding_peak), avg(sliding_size)
FROM
md_%s
WHERE
seq_id = %s
GROUP BY
seq_id;""" % (md_type, seq_id)
self._cur.execute(md_query)
t = self._cur.fetchone()
if not t:
t = [0.0]*9
data_list.append(t)
return [elem for elem in itertools.chain(*data_list)]
def fetch_averaged_correlations(self, seq_id):
"""
Fetches data from correlation table, if some values are missing,
set values to 0.0
"""
data_dict = {}
types_a = ["rise", "roll", "shift", "slide", "tilt", "twist"]
types_b = ["roll", "shift", "slide", "tilt", "twist"]
couples = []
for type_a in types_a:
if type_a in types_b:
types_b.remove(type_a)
for type_b in types_b:
couples.append("%s/%s" % (type_a, type_b))
del types_a, types_b
for couple in couples:
data_dict[couple] = []
corr_query="""SELECT
type_a, type_b, avg(spearman_complete::int::float), avg(spearman_center::int::float), avg(pearson_complete::int::float), avg(pearson_center::int::float)
FROM
correlations
WHERE
seq_id = %s
GROUP BY
type_a, type_b
ORDER BY
type_a, type_b
ASC;""" % (seq_id, )
self._cur.execute(corr_query)
data = self._cur.fetchall()
for tpl in data:
data_dict["%s/%s" % (tpl[0], tpl[1])].extend(tpl[2:])
del data
data = []
for couple in couples:
if not data_dict[couple]:
data.extend([0.0]*4)
else:
data.extend(data_dict[couple])
return data
def fetch_trx_data(self, seq_id):
"""
Fetch trx data from DB
"""
trx_query = """SELECT
complete_peak_freq, complete_peak, complete_size, center_peak_freq, center_peak, center_size, sliding_peak_freq, sliding_peak, sliding_size
FROM
trx
WHERE
seq_id = %s;""" % (seq_id,)
self._cur.execute(trx_query)
return list(self._cur.fetchone())
class PGCursor(object): class PGCursor(object):
""" """
PostGreSQL Python Cursor. PostGreSQL Python Cursor.
...@@ -26,6 +171,23 @@ class PGCursor(object): ...@@ -26,6 +171,23 @@ class PGCursor(object):
self._conn.set_session(autocommit = True) self._conn.set_session(autocommit = True)
self._cur = self._conn.cursor(cursor_factory = psycopg2.extras.DictCursor) self._cur = self._conn.cursor(cursor_factory = psycopg2.extras.DictCursor)
def check_sequence(self, seq):
"""
Checks if seq is already recorded in database.
"""
verif_query = """SELECT
id
FROM
sequences
WHERE
sequence=%s;"""
self._cur.execute(verif_query, (seq.sequence,))
ret = self._cur.fetchone()
if ret:
return ret[0]
else:
return False
def store_sequence(self, seq): def store_sequence(self, seq):
""" """
Stores the sequence data in database Stores the sequence data in database
...@@ -38,6 +200,7 @@ class PGCursor(object): ...@@ -38,6 +200,7 @@ class PGCursor(object):
- nothing - nothing
""" """
# This query is designed to add the sequence metadata in the database. # This query is designed to add the sequence metadata in the database.
# We give explicitly the columns of the table we want to fill, and we also give the dict keys for the VALUES, # We give explicitly the columns of the table we want to fill, and we also give the dict keys for the VALUES,
# so we only have to give seq to cur.execute() as if seq was a dict. # so we only have to give seq to cur.execute() as if seq was a dict.
...@@ -101,3 +264,15 @@ class PGCursor(object): ...@@ -101,3 +264,15 @@ class PGCursor(object):
"type_b": type_b, "type_b": type_b,
}) })
self._cur.execute(corr_query, dic_to_sql) self._cur.execute(corr_query, dic_to_sql)
return seq_id
def remove_seq(self, seq_id):
"""
Removes a sequence from database
"""
for md_type in ["rise", "roll", "shift", "slide", "tilt", "twist"]:
self._cur.execute("DELETE FROM md_%s WHERE seq_id=%s" % (md_type, seq_id))
self._cur.execute('DELETE FROM trx WHERE seq_id=%s', (seq_id,))
self._cur.execute('DELETE FROM correlations WHERE seq_id=%s', (seq_id,))
self._cur.execute('DELETE FROM sequences WHERE id=%s', (seq_id,))
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment