Skip to content

Commit

Permalink
Merge pull request #143 from pagreene/save-the-curations
Browse files Browse the repository at this point in the history
Convert curation API to return JSON.
  • Loading branch information
pagreene committed Nov 5, 2020
2 parents fbe4327 + c3820a6 commit 10cd7db
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 17 deletions.
20 changes: 13 additions & 7 deletions indra_db/client/principal/curation.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@
logger = logging.getLogger(__name__)


def submit_curation(hash_val, tag, curator, ip, text=None,
ev_hash=None, source='direct_client', db=None):
def submit_curation(hash_val, tag, curator, ip, text=None, ev_hash=None,
source='direct_client', stmt_json=None, ev_json=None,
db=None):
"""Submit a curation for a given preassembled or raw extraction.
Parameters
Expand All @@ -36,14 +37,19 @@ def submit_curation(hash_val, tag, curator, ip, text=None,
The name of the access point through which the curation was performed.
The default is 'direct_client', meaning this function was used
directly. Any higher-level application should identify itself here.
stmt_json : Optional[dict]
The JSON of a preassembled or raw statement that was curated.
ev_json : Optional[dict]
The JSON of the evidence that was curated.
db : DatabaseManager
A database manager object used to access the database.
"""
if db is None:
db = get_db('primary')

inp = {'tag': tag, 'text': text, 'curator': curator, 'ip': ip,
'source': source, 'pa_hash': hash_val, 'source_hash': ev_hash}
'source': source, 'pa_hash': hash_val, 'source_hash': ev_hash,
'pa_json': stmt_json, 'ev_json': ev_json}

logger.info("Adding curation: %s" % str(inp))

Expand Down Expand Up @@ -85,7 +91,7 @@ def get_curations(db=None, **params):
else:
constraints.append(getattr(cur, key) == val)

return db.select_all(cur, *constraints)
return [c.to_json() for c in db.select_all(cur, *constraints)]


def get_grounding_curations(db=None):
Expand All @@ -108,15 +114,15 @@ def get_grounding_curations(db=None):
groundings = {}
for cur in curs:
# If there is no curation given, we skip it
if not cur.text:
if not cur['text']:
continue
# We now try to match the standard pattern for grounding curation
cur_text = cur.text.strip()
cur_text = cur['text'].strip()
match = re.match('^\[(.*)\] -> ([^ ]+)$', cur_text)
# We log any instances of curations that don't match the pattern
if not match:
logger.info('"%s" by %s does not match the grounding curation '
'pattern.' % (cur_text, cur.curator))
'pattern.' % (cur_text, cur['curator']))
continue
txt, dbid_str = match.groups()
# We now get a dict of curated mappings to return
Expand Down
5 changes: 4 additions & 1 deletion indra_db/schemas/principal_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -594,11 +594,14 @@ class Curation(Base, IndraDBTable):
source = Column(String)
ip = Column(INET)
date = Column(DateTime, default=func.now())
pa_json = Column(JSONB)
ev_json = Column(JSONB)

def to_json(self):
return {attr: getattr(self, attr)
for attr in ['id', 'pa_hash', 'source_hash', 'tag', 'text',
'date', 'curator', 'source']}
'date', 'curator', 'source', 'pa_json',
'ev_json']}

table_dict[Curation.__tablename__] = Curation

Expand Down
5 changes: 2 additions & 3 deletions rest_api/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ def expand_meta_row():
if w_cur_counts:
curations = get_curations(pa_hash=set(entry_hash_lookup.keys()))
for cur in curations:
for entry in entry_hash_lookup[cur.pa_hash]:
for entry in entry_hash_lookup[cur['pa_hash']]:
entry['cur_count'] += 1

res_json = result.json()
Expand Down Expand Up @@ -362,8 +362,7 @@ def submit_curation_endpoint(hash_val, **kwargs):
@app.route('/curation/list/<stmt_hash>/<src_hash>', methods=['GET'])
def list_curations(stmt_hash, src_hash):
curations = get_curations(pa_hash=stmt_hash, source_hash=src_hash)
curation_json = [cur.to_json() for cur in curations]
return jsonify(curation_json)
return jsonify(curations)


def main():
Expand Down
12 changes: 6 additions & 6 deletions rest_api/call_handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -284,13 +284,13 @@ def get_curation_counts(result):
cur_counts = {}
for curation in curations:
# Update the overall counts.
if curation.pa_hash not in cur_counts:
cur_counts[curation.pa_hash] = 0
cur_counts[curation.pa_hash] += 1
if curation['pa_hash'] not in cur_counts:
cur_counts[curation['pa_hash']] = 0
cur_counts[curation['pa_hash']] += 1

# Work these counts into the evidence dict structure.
for ev_json in result.results[curation.pa_hash]['evidence']:
if str(ev_json['source_hash']) == str(curation.source_hash):
for ev_json in result.results[curation['pa_hash']]['evidence']:
if str(ev_json['source_hash']) == str(curation['source_hash']):
ev_json['num_curations'] = \
ev_json.get('num_curations', 0) + 1
break
Expand Down Expand Up @@ -372,7 +372,7 @@ def produce_response(self, result):
rel['hashes'] = None
curations = get_curations(pa_hash=set(rel_hash_lookup.keys()))
for cur in curations:
for rel in rel_hash_lookup[cur.pa_hash]:
for rel in rel_hash_lookup[cur['pa_hash']]:
rel['cur_count'] += 1

logger.info("Returning with %s results after %.2f seconds."
Expand Down

0 comments on commit 10cd7db

Please sign in to comment.