<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">"""
Compute and compare the precision between the CDB derived values
and the uncorrected values given that you want some given false
positive rate where false positives are those associations listed
in likelyfalse_drug_ae.
"""

import MySQLdb

db = MySQLdb.connect(host="localhost", port=3307, user="root", passwd="enter_your_password",db="project_aers")
c = db.cursor()

query = """
select stitch_id, umls_id
from likelyfalse_drug_ae
"""
c.execute(query)

lf = set(["%s-%s" % tuple(x) for x in c.fetchall()])

query = """
select stitch_id, umls_id
from gold_drug_ae
"""
c.execute(query)

gold = set(["%s-%s" % tuple(x) for x in c.fetchall()])

query = """
select stitch_id, umls_id
from pred_drug_events_e5
where prr &gt; 2
"""
c.execute(query)

epred = dict()
for sid, uid in c.fetchall():
    if not uid in epred:
        epred[uid] = set()
    
    epred[uid].add('%s-%s' % (sid, uid))

query = """
select stitch_id, umls_id, prr
from prop_pred_drug_events
"""
c.execute(query)

ppred = dict()
for sid, uid, prr in c.fetchall():
    if not uid in ppred:
        ppred[uid] = list()
    
    ppred[uid].append( (prr, sid) )

uids = set(ppred.keys()) &amp; set(epred.keys())

results = dict()
for uid in uids:
    target_prec = len(epred[uid] &amp; gold)/float(len(epred[uid]))
    
    if target_prec &gt; 0.0:
        gold_n = 0
        cutoff = None
        preds = sorted(ppred[uid])
        
        while len(preds) &gt; 0 and abs(len(set(['%s-%s' % (sid,uid) for prr, sid in preds]) &amp; gold)/float(len(preds))-target_prec) &gt; 0.01:
            junk = preds.pop()
        
        if len(preds) &gt; 0:
            results[uid] = dict()
            results[uid]['e-fpr'] = float(len(epred[uid] &amp; lf))/float(len(epred[uid]))
            
            predset = set(['%s-%s' % (sid, uid) for prr, sid in preds])
            results[uid]['p-fpr'] = float(len(predset &amp; lf))/float(len(predset))
            

</pre></body></html>