for cid, kid, val in q.all():
if cid not in row_index:
row_index[cid] = len(row_index)
if kid not in col_index:
col_index[kid] = len(col_index)
X[row_index[cid], col_index[kid]] = val
// Return as an AnnotationMatrix
return self.matrix_cls(X, candidate_set=candidate_set, key_set=key_set)
After Change
// First, we query to construct the column index map
kid_to_col = {}
col_to_kid = {}
q = session.query(AnnotationKey.id).filter(AnnotationKey.sets.contains(key_set)).order_by(AnnotationKey.id)
for kid, in q.all():
if kid not in kid_to_col:
j = len(kid_to_col)
// Create both mappings
kid_to_col[kid] = j
col_to_kid[j] = kid
// Construct the query
q = session.query(self.annotation_cls.candidate_id, self.annotation_cls.key_id, self.annotation_cls.value)
q = q.join(Candidate, AnnotationKey)
q = q.filter(Candidate.sets.contains(candidate_set)).filter(AnnotationKey.sets.contains(key_set))
q = q.order_by(self.annotation_cls.candidate_id).yield_per(1000)