def matches(self): session = Session() q = session.query(Matches) qf = q.filter(Matches.source == self.source['node_id'], Matches.destination == self.destination['node_id']) odf = pd.read_sql(qf.statement, q.session.bind).set_index('id') df = pd.DataFrame(odf.values, index=odf.index.values, columns=odf.columns.values) df.index.name = 'id' # Explicit close to get the session cleaned up session.close() return DbDataFrame(df, parent=self, name='matches')
def masks(self): res = Session().query(Edges.masks).\ filter(Edges.source == self.source['node_id']).\ filter(Edges.destination == self.destination['node_id']).\ first() if res: df = pd.DataFrame.from_records(res[0]) df.index = df.index.map(int) else: ids = list(map(int, self.matches.index.values)) df = pd.DataFrame(index=ids) df.index.name = 'match_id' return DbDataFrame(df, parent=self, name='masks')
def costs(self): # these are np.float coming out, sqlalchemy needs ints ids = list(map(int, self.matches.index.values)) res = Session().query(Costs).filter(Costs.match_id.in_(ids)).all() #qf = q.filter(Costs.match_id.in_(ids)) if res: # Parse the JSON dicts in the cost field into a full dimension dataframe costs = {r.match_id:r._cost for r in res} df = pd.DataFrame.from_records(costs).T # From records is important because from_dict drops rows with empty dicts else: df = pd.DataFrame(index=ids) df.index.name = 'match_id' return DbDataFrame(df, parent=self, name='costs')