diff --git a/planet/models/expression_specificity.py b/planet/models/expression_specificity.py index 0f89023..2cee026 100644 --- a/planet/models/expression_specificity.py +++ b/planet/models/expression_specificity.py @@ -21,6 +21,7 @@ def calculate_specificities(species_id, description): new_method = ExpressionSpecificityMethod() new_method.species_id = species_id new_method.description = description + conditions = [] profiles = db.engine.execute(db.select([ExpressionProfile.__table__.c.id, ExpressionProfile.__table__.c.profile]). where(ExpressionProfile.__table__.c.species_id == species_id) @@ -28,18 +29,43 @@ def calculate_specificities(species_id, description): for profile_id, profile in profiles: profile_data = json.loads(profile) - profile_mean = {k: mean(v) for k, v in profile_data['data'].items()} + for condition in profile_data['order']: + if condition not in conditions: + conditions.append(condition) + + new_method.conditions = json.dumps(conditions) + + db.session.add(new_method) + db.session.commit() + + specificities = [] + + for profile_id, profile in profiles: + profile_data = json.loads(profile) + profile_means = {k: mean(v) for k, v in profile_data['data'].items()} - print(profile_id, profile_mean) for condition in profile_data['order']: - score = expression_specificity(condition, profile_mean) + score = expression_specificity(condition, profile_means) + new_specificity = { + 'profile_id': profile_id, + 'condition': condition, + 'score': score, + 'method_id': new_method.id, + } + + specificities.append(new_specificity) + if len(specificities) > 400: + db.engine.execute(ExpressionSpecificity.__table__.insert(), specificities) + specificities = [] + + db.engine.execute(ExpressionSpecificity.__table__.insert(), specificities) class ExpressionSpecificity(db.Model): __tablename__ = 'expression_specificity' id = db.Column(db.Integer, primary_key=True) - profile = db.Column(db.Integer, db.ForeignKey('expression_profiles.id'), index=True) + profile_id = db.Column(db.Integer, db.ForeignKey('expression_profiles.id'), index=True) condition = db.Column(db.String(255), index=True) score = db.Column(db.Float, index=True) method_id = db.Column(db.Integer, db.ForeignKey('expression_specificity_method.id'), index=True) diff --git a/utils/vector.py b/utils/vector.py index f299621..4be0334 100644 --- a/utils/vector.py +++ b/utils/vector.py @@ -1,5 +1,6 @@ from math import sqrt + def dot_prod(a, b): """ Calculates the dot product of two lists with values @@ -18,4 +19,4 @@ def norm(a): :param a: list of values :return: the Frobenius norm """ - return sqrt(sum([abs(i)**2 for i in a])) \ No newline at end of file + return sqrt(sum([i**2 for i in a]))