# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# +
# %reload_ext autoreload
# %autoreload 2
# %pylab inline
# %matplotlib inline

import pandas as pd
import numpy as np
import pylab as pl
import scipy as sp
import sys
import os 
import seaborn as sns
from scipy import stats
from box import Box
import warnings

from IPython.core.display import display, HTML

warnings.simplefilter('ignore')

#TOP = "/ccte/home2/ttate/genra-ltea"
TOP = "/". join(os.getcwd().split('/')["-2"])+'/'
LIB = TOP+'genra'
if not LIB in sys.path:
    sys.path.insert(0, LIB)
    
os.environ['genra-py']=LIB

DATA_DIR = TOP +'/data/'
FIG_DIR = TOP +'/figs/'
# -

# LTEA Data
#
# chm, bio, tox, and chem clusters

D1 = TOP+'/data/ltea-2020/'

CHM1 = pd.read_csv(D1+'S7-chem.tsv', sep='\t')
#morgan, torsion, and toxprints 
TOX1 = pd.read_csv(D1+'S1-tox.tsv', sep='\t')
#toxref db v2 query 4.2020
BIO1 = pd.read_csv(D1+'S2-bio.tsv', sep='\t') 
#ltea group gene and assay level bio data
#BIO2 = pd.read_csv(D1+'S3-bio.tsv', sep='\t')
#ltea gene level bio data
CLS1 = pd.read_csv(D1+'S3-clust.tsv', sep='\t')

CLS1.head()

# Organizing Chemical Data 
#     -Chemical names:DTXSID, preferred_name
#     -toxprints: toxp_*
#     -morgan fps: mrgn_*
#     -topological torsion fps: tptr_*

CHM1.head()

CHM1 = CHM1.replace(['-'], np.nan)

CHM1.isnull().values.any()

# +
#set([i[0] for i in CHM1.columns.str.split('_')])
CHM1_nm = CHM1[['DTXSID', 'PREFERRED_NAME']].set_index('DTXSID')
X = CHM1 = CHM1.drop(['PREFERRED_NAME'], axis=1).set_index('DTXSID')

CHM11 = Box()
for i in set([i[0] for i in X.columns.str.split('_') ]):
    CHM11[i] =  X[X.columns[X.columns.str.match(i)]]
# -

CHM11.mrgn.head()

# Organizing Biological data
#
# gene_* gene level bio data
# assay_* assay level bio data

BIO1.head()

BIO1 = BIO1.rename(columns = {'dsstox_substance_id':'DTXSID'})

BIO1.isnull().values.any()
BIO1.isnull().sum().sum()

BIO1 = BIO1.dropna()
BIO1.shape

# +
BIO1 = BIO1.drop(['chnm', 'casn'], axis = 1).set_index('DTXSID')
#set([i[0] for i in BIO1.columns.str.split('_')])

BIO11 = Box()
for i in set([i[0] for i in BIO1.columns.str.split('_')]):
    BIO11[i]=BIO1[BIO1.columns[BIO1.columns.str.match(i)]]
# -

list(BIO11.keys())

# BIO Gene Level Data

# +
#BIO2 = BIO2.rename(columns = {'dsstox_substance_id':'DTXSID'})

#BIO2 = BIO2.drop(['chnm', 'casn'], axis = 1).set_index('DTXSID')
#set([i[0] for i in BIO1.columns.str.split('_')])

#BIO12 = Box()
#for i in set([i[0] for i in BIO2.columns.str.split('_')]):
 #   BIO12[i]=BIO2[BIO2.columns[BIO2.columns.str.match(i)]]

# +
#BIO2.head()

# +
#list(BIO12.keys())
# -

# Organizing Tox Data

TOX1.head()

TOX1 = TOX1.rename(columns = {'dsstox_substance_id':'DTXSID'})
#TOX1 = TOX1.replace(['-'], np.nan)

TOX1 = TOX1.drop(['preferred_name'], axis=1).set_index('DTXSID')
#set([i[0] for i in TOX1.columns.str.split('_')])
TOX11 = Box()
for i in set([i[0] for i in TOX1.columns.str.split('_')]):
    TOX11[i] = TOX1[TOX1.columns[TOX1.columns.str.match(i)]]

list(TOX11.keys())

# Liver Effects

TOX1.columns[TOX1.columns.str.find('liver')>-1]

CHM11.mrgn.shape

# +
#z = CHM11.mrgn.index.intersection(Y.index)
#z
# -

# Analysis: CHR liver 
#    Chronic Liver toxicity data and morgan chemical structure descriptors

# +
Y = TOX1.chr_liver.dropna()
INT=CHM11.mrgn.index.intersection(Y.index).dropna()
if len(INT)>0:
    Yf = Y.copy()[INT]
    Yf[Yf>0]=1
    Yb=Yf.copy()
    Yb = Yb.astype(np.int)
    X = CHM11.mrgn.loc[INT]
else:
    pass
    

#Yf = Y.copy()
#Yf[Yf>0]=1
#Yb= Yf.copy()
#Yb = Yb.astype(np.int)
#I=CHM11.mrgn.index.intersection(Y.index)
#if len(I)>0:
 #   X = CHM11.mrgn.loc[I]
#else:
 #   pass
        
X.shape, Y.shape, Yf.shape, Yb.shape
# -

Yb.value_counts()

# Global Analysis 
# --Global analysis based on all the data, ignoring clusters. 
# --Uses 5 nearest neighbors and score using ROC AUC analysis

# +
from sklearn.model_selection import cross_validate
from sklearn.metrics import make_scorer, roc_auc_score
from genra.rax.skl.reg import *

Scorer1 = {
    'AUC':make_scorer(roc_auc_score),
}
GCO = GenRAPredValue(n_neighbors=5, algorithm='brute', metric='jaccard', n_jobs=2)

P = pd.DataFrame(cross_validate(GCO, X, Yb, cv=5, scoring=Scorer1))
R1 = pd.DataFrame(dict(mn=P.mean(), sd=P.std()))
R1

# +
from genra.rax.skl.reg import GenRAPredValue
from sklearn.metrics import make_scorer,explained_variance_score,roc_auc_score,r2_score
from sklearn.model_selection import GridSearchCV
from sklearn.model_selection import LeaveOneOut

#KNeighborsClassifier(n_neighbors=5, weights=’uniform’, algorithm=’auto’, leaf_size=30, p=2, 
#                     metric=’minkowski’, metric_params=None, n_jobs=1)[source]

GC1 = GenRAPredValue(algorithm='brute')

params={'n_neighbors': range(1,15),
        'metric':['euclidean','jaccard','manhattan']
       }


Grid3= GridSearchCV(estimator=GC1,param_grid=params,
                    n_jobs=-1,cv=5,#LeaveOneOut(),
                    verbose=1,
                    scoring=make_scorer(roc_auc_score))

Best3=Grid3.fit(X,Yb)
Best3.best_score_,Best3.best_params_
# -

# Local Predict Value
# Use leave out cross validation the get GenRA prediction for each chemical

# +
from sklearn.model_selection import cross_validate,LeaveOneOut
from sklearn.metrics import make_scorer,roc_auc_score,r2_score
from genra.rax.skl.reg import *


GC1 = GenRAPredValue(n_neighbors=8,algorithm='brute',metric='jaccard',n_jobs=3)

Loo = LeaveOneOut()
Loo.get_n_splits(X)
R = []
I = []
for trn_idx,tst_idx in Loo.split(X):
    X_trn,X_tst = X.iloc[trn_idx],X.iloc[tst_idx]
    Y_trn,Y_tst = Yf.iloc[trn_idx],Yf.iloc[tst_idx]
    Y_p = GC1.fit(X_trn,Y_trn).predict(X_tst)
    I.append(tst_idx[0])
    R.append(Y_p[0])

Perf1 = pd.DataFrame(dict(DTXSID=X.iloc[I].index,Y_true=Yb.iloc[I],Y_pred=R))\
          .reset_index(drop=True)\
          .merge(CLS1,on='DTXSID')

Perf1.loc[:,'Y_true']=Perf1.Y_true.astype(np.uint)
Grp = Perf1[['cl_id','Y_true','Y_pred']].groupby('cl_id')

def my_roc_score(Tr,Pr):
    if len(Tr.unique())==1:
        return None
    else:
        return roc_auc_score(Tr,Pr)
    
Perf_Val=pd.DataFrame(dict(auc_score=Grp.apply(lambda x: my_roc_score(x.Y_true,x.Y_pred)),
                           r2_score=Grp.apply(lambda x: r2_score(x.Y_true,x.Y_pred))))

Perf_Val.sort_values('auc_score',ascending=False)
# -

import seaborn as sns
sns.set(style='whitegrid')
sns.pairplot(Perf_Val)

# Chr_liver with gene level bio data

# +
Y = TOX1.chr_liver.dropna()
INT=BIO11.gene.index.intersection(Y.index)
if len(INT)>0:
    Yf = Y.copy()[INT]
    Yf[Yf>0]=1
    Yb=Yf.copy()
    Yb = Yb.astype(np.int)
    X = BIO11.gene.loc[INT]
else:
    pass
    

#Yf = Y.copy()
#Yf[Yf>0]=1
#Yb= Yf.copy()
#Yb = Yb.astype(np.int)
#I=CHM11.mrgn.index.intersection(Y.index)
#if len(I)>0:
 #   X = CHM11.mrgn.loc[I]
#else:
 #   pass
        
X.shape, Y.shape, Yf.shape, Yb.shape
# -

Yb.value_counts()

# +
from sklearn.model_selection import cross_validate
from sklearn.metrics import make_scorer, roc_auc_score
from genra.rax.skl.reg import *

Scorer1 = {
    'AUC':make_scorer(roc_auc_score),
}
GCO = GenRAPredValue(n_neighbors=5, algorithm='brute', metric='jaccard', n_jobs=2)

P = pd.DataFrame(cross_validate(GCO, X, Yb, cv=5, scoring=Scorer1))
R1 = pd.DataFrame(dict(mn=P.mean(), sd=P.std()))
R1

# +
from sklearn.model_selection import cross_validate,LeaveOneOut
from sklearn.metrics import make_scorer,roc_auc_score,r2_score
from genra.rax.skl.reg import *


GC1 = GenRAPredValue(n_neighbors=8,algorithm='brute',metric='jaccard',n_jobs=3)

Loo = LeaveOneOut()
Loo.get_n_splits(X)
R = []
I = []
for trn_idx,tst_idx in Loo.split(X):
    X_trn,X_tst = X.iloc[trn_idx],X.iloc[tst_idx]
    Y_trn,Y_tst = Yf.iloc[trn_idx],Yf.iloc[tst_idx]
    Y_p = GC1.fit(X_trn,Y_trn).predict(X_tst)
    I.append(tst_idx[0])
    R.append(Y_p[0])

Perf1 = pd.DataFrame(dict(DTXSID=X.iloc[I].index,Y_true=Yb.iloc[I],Y_pred=R))\
          .reset_index(drop=True)\
          .merge(CLS1,on='DTXSID')

Perf1.loc[:,'Y_true']=Perf1.Y_true.astype(np.uint)
Grp = Perf1[['cl_id','Y_true','Y_pred']].groupby('cl_id')

def my_roc_score(Tr,Pr):
    if len(Tr.unique())==1:
        return None
    else:
        return roc_auc_score(Tr,Pr)
    
Perf_Val=pd.DataFrame(dict(auc_score=Grp.apply(lambda x: my_roc_score(x.Y_true,x.Y_pred)),
                           r2_score=Grp.apply(lambda x: r2_score(x.Y_true,x.Y_pred))))

Perf_Val.sort_values('auc_score',ascending=False)
# -







# Local Predict Class
# Leave out each example to fit and predict

# +
from sklearn.model_selection import cross_validate,LeaveOneOut
from sklearn.metrics import make_scorer,f1_score,accuracy_score,precision_score,recall_score
from genra.rax.skl.cls import *


GC0 = GenRAPredClass(n_neighbors=8,algorithm='brute',metric='jaccard',n_jobs=2)

Loo = LeaveOneOut()
Loo.get_n_splits(X)
R = []
I = []
for trn_idx,tst_idx in Loo.split(X):
    X_trn,X_tst = X.iloc[trn_idx],X.iloc[tst_idx]
    Y_trn,Y_tst = Yb.iloc[trn_idx],Yb.iloc[tst_idx]
    Y_p = GC0.fit(X_trn,Y_trn).predict(X_tst)
    I.append(tst_idx[0])
    R.append(Y_p[0])

Perf1 = pd.DataFrame(dict(DTXSID=X.iloc[I].index,Y_true=Yb.iloc[I],Y_pred=R))\
          .reset_index(drop=True)\
          .merge(CLS1,on='DTXSID')

Grp = Perf1[['cl_id','Y_true','Y_pred']].groupby('cl_id')

Perf_CLS=pd.DataFrame(dict(f1_score=Grp.apply(lambda x: f1_score(x.Y_true,x.Y_pred)),
                           sp_score=Grp.apply(lambda x: precision_score(x.Y_true,x.Y_pred)),
                           sn_score=Grp.apply(lambda x: recall_score(x.Y_true,x.Y_pred))))
                                                                             
Perf_CLS.sort_values('f1_score',ascending=False)
# -

import seaborn as sns
sns.set(style='whitegrid')
sns.pairplot(Perf_CLS)



# Local Predict Value
# Leave out each example to fit and predict

# +
from sklearn.model_selection import cross_validate,LeaveOneOut
from sklearn.metrics import make_scorer,explained_variance_score,roc_auc_score,r2_score
from genra.rax.skl.reg import *


GC1 = GenRAPredValue(n_neighbors=5,algorithm='brute',metric='jaccard',n_jobs=2)

Loo = LeaveOneOut()
Loo.get_n_splits(X)
R = []
I = []
for trn_idx,tst_idx in Loo.split(X):
    X_trn,X_tst = X.iloc[trn_idx],X.iloc[tst_idx]
    Y_trn,Y_tst = Yf.iloc[trn_idx],Yf.iloc[tst_idx]
    Y_p = GC1.fit(X_trn,Y_trn).predict(X_tst)
    I.append(tst_idx[0])
    R.append(Y_p[0])

Perf1 = pd.DataFrame(dict(DTXSID=X.iloc[I].index,Y_true=Yb.iloc[I],Y_pred=R))\
          .reset_index(drop=True)\
          .merge(CLS1,on='DTXSID')

Perf1.loc[:,'Y_true']=Perf1.Y_true.astype(np.uint)
Grp = Perf1[['cl_id','Y_true','Y_pred']].groupby('cl_id')

def my_roc_score(Tr,Pr):
    if len(Tr.unique())==1:
        return None
    else:
        return roc_auc_score(Tr,Pr)
    
Perf_Val=pd.DataFrame(dict(auc_score=Grp.apply(lambda x: my_roc_score(x.Y_true,x.Y_pred)),
                           r2_score=Grp.apply(lambda x: r2_score(x.Y_true,x.Y_pred)),
                           expvr_score=Grp.apply(lambda x: explained_variance_score(x.Y_true,x.Y_pred))))
                                                                             
Perf_Val.sort_values('auc_score',ascending=False)
# -

import seaborn as sns
sns.set(style='whitegrid')
sns.pairplot(Perf_Val)

# Analyze all endpoints and descriptors

del(P)

# +
from sklearn.model_selection import cross_validate,LeaveOneOut
from sklearn.metrics import make_scorer,explained_variance_score,roc_auc_score,r2_score
from genra.rax.skl.reg import *
import pdb

def loocv_evaluate(Est,X_in,Y_in,**kwargs):
    Loo = LeaveOneOut()
    Loo.get_n_splits(X_in)
    R = []
    I = []
    for trn_idx,tst_idx in Loo.split(X_in):
        X_trn,X_tst = X_in.iloc[trn_idx],X_in.iloc[tst_idx]
        Y_trn,Y_tst = Y_in.iloc[trn_idx],Y_in.iloc[tst_idx]
        Y_p = Est.fit(X_trn,Y_trn).predict(X_tst)
        I.append(tst_idx[0])
        R.append(Y_p[0])

    Perf = pd.DataFrame(dict(DTXSID=X_in.iloc[I].index,
                             Y_true=Y_in.iloc[I],
                             Y_pred=R))\
              .reset_index(drop=True)
    Perf.loc[:,'Y_true']=Perf.Y_true.astype(np.uint)
    
    return Perf

def my_roc_score(Tr,Pr):
    if len(Tr.unique())==1:
        return None
    else:
        I1 = np.logical_and(pd.notnull(Pr),pd.notnull(Tr))
        return roc_auc_score(Tr[I1],Pr[I1])
    
def run_genra_val(X,Y,Cls=pd.DataFrame(),n=8,j=3):    
    GCi = GenRAPredValue(n_neighbors=n,algorithm='brute',
                         metric='jaccard',n_jobs=j)
    P  = loocv_evaluate(GCi,X,Y).dropna()
    if Cls.shape[0]>0:
        P   = P.merge(Cls,on='DTXSID')
        Grp = P[['cl_id','Y_true','Y_pred']].groupby('cl_id')
        Perf= pd.DataFrame(dict(auc_score=Grp.apply(lambda x: my_roc_score(x.Y_true,x.Y_pred)),
                                r2_score=Grp.apply(lambda x: r2_score(x.Y_true,x.Y_pred)),
                                expvr_score=Grp.apply(lambda x: explained_variance_score(x.Y_true,x.Y_pred))))
    else:
        Perf = pd.Series(dict(auc_score= my_roc_score(P.Y_true,P.Y_pred),
                              r2_score = r2_score(P.Y_true,P.Y_pred),
                              expvr_score=explained_variance_score(P.Y_true,P.Y_pred)))    
    return Perf

def run_genra_cls(X,Y,Cls=pd.DataFrame(),n=8,j=3):    
    GCi = GenRAPredClass(n_neighbors=n,algorithm='brute',
                         metric='jaccard',n_jobs=j)
    P  = loocv_evaluate(GCi,X,Y).dropna()
    if Cls.shape[0]>0:
        P   = P.merge(Cls,on='DTXSID')
        Grp = P[['cl_id','Y_true','Y_pred']].groupby('cl_id')
        Perf= pd.DataFrame(dict(f1_score=Grp.apply(lambda x: f1_score(x.Y_true,x.Y_pred)),
                               sp_score=Grp.apply(lambda x: precision_score(x.Y_true,x.Y_pred)),
                               sn_score=Grp.apply(lambda x: recall_score(x.Y_true,x.Y_pred))))
    else:
        Perf = pd.Series(dict(f1_score= f1_score(P.Y_true,P.Y_pred),
                              sp_score = precision_score(P.Y_true,P.Y_pred),
                              sn_score=recall_score(P.Y_true,P.Y_pred)))    
    return Perf



# -

# Global

# +
PERF_chm=[]
Chm  = list(CHM11.keys())


for Effect in TOX1.columns:
    #print(Effect)
    Yf = TOX1[Effect].dropna()
    Yf[Yf>0]=1
    Yb = Yf.copy()
    Yb = Yb.astype(np.int)
    
    for C in Chm:
        INT=CHM11[C].index.intersection(Yb.index)
        if len(INT)>0:
            X = CHM11[C].loc[INT].dropna()
        if X.shape[0]>20:
            print(Effect,C,X.shape)
            P1 = run_genra_cls(X,Yb).to_dict()
            P2 = run_genra_val(X,Yf).to_dict()
            P1.update(P2)
            P1.update(dict(tox=Effect,dt='chm',dn=C))
            PERF_chm.append(P1)
    X = CHM1.loc[INT].dropna()
    if X.shape[0]>20:
        print(C,X.shape)
        P1 = run_genra_cls(X,Yb).to_dict()
        P2 = run_genra_val(X,Yf).to_dict()
        P1.update(P2)
        P1.update(dict(tox=Effect,dt='chm',dn='all'))
        PERF_chm.append(P1)
        break
# -

pd.DataFrame(PERF_chm).sort_values('auc_score',ascending=False)

Yf

# +
PERF_bio=[]
Bio  = list(BIO11.keys())

for Effect in TOX1.columns:
    #print(Effect)
    Yf = TOX1[Effect].dropna()
    Yf[Yf>0]=1
    Yb = Yf.copy()
    Yb = Yb.astype(np.int)
    
    for B in Bio:
        INT1 = BIO11[B].index.intersection(Yb.index)
        if len(INT1)>0:
            X= BIO11[B].loc[INT1]
            #Yb= Yb.loc[X.index]
            #Yf= Yf.loc[X.index]
        if X.shape[0]>20:
            print(Effect,B,X.shape)
            P1 = run_genra_cls(X,Yb).to_dict()
            P2 = run_genra_val(X,Yf).to_dict()
            P1.update(P2)
            P1.update(dict(tox=Effect,dt='bio',dn=B))
            PERF_bio.append(P1)

    X = BIO1.loc[INT1]
    #Yb= Yb.loc[X.index]
    #Yf= Yf.loc[X.index]
    if X.shape[0]>20:
        print(C,X.shape)
        P1 = run_genra_cls(X,Yb).to_dict()
        P2 = run_genra_val(X,Yf).to_dict()
        P1.update(P2)
        P1.update(dict(tox=Effect,dt='bio',dn='all'))
        PERF_bio.append(P1)
        break
# -

pd.DataFrame(PERF_bio).sort_values('f1_score',ascending=False)

# +
PERF_bio2=[]
Bio  = list(BIO12.keys())

for Effect in TOX1.columns:
    #print(Effect)
    Yf = TOX1[Effect].dropna()
    Yf[Yf>0]=1
    Yb = Yf.copy()
    Yb = Yb.astype(np.int)
    
    for B in Bio:
        INT1 = BIO12[B].index.intersection(Yb.index)
        if len(INT1)>0:
            X= BIO12[B].loc[INT1].dropna()
            Yb= Yb.loc[X.index]
            Yf= Yf.loc[X.index]
        if X.shape[0]>20:
            print(Effect,B,X.shape)
            P1 = run_genra_cls(X,Yb).to_dict()
            P2 = run_genra_val(X,Yf).to_dict()
            P1.update(P2)
            P1.update(dict(tox=Effect,dt='bio',dn=B))
            PERF_bio.append(P1)

    X = BIO2.loc[INT1]
    Yb= Yb.loc[X.index]
    Yf= Yf.loc[X.index]
    if X.shape[0]>20:
        print(C,X.shape)
        P1 = run_genra_cls(X,Yb).to_dict()
        P2 = run_genra_val(X,Yf).to_dict()
        P1.update(P2)
        P1.update(dict(tox=Effect,dt='bio',dn='all'))
        PERF_bio2.append(P1)
        break
# -

pd.DataFrame(PERF_bio).sort_values('f1_score',ascending=False)

# LOCAL

# +
PERF=[]
Chm  = list(CHM11.keys())


for Effect in ['chr_liver']:#TOX1.columns:
    #print(Effect)
    Yf = TOX1[Effect].dropna()
    Yf[Yf>0]=1
    Yb = Yf.copy()
    Yb = Yb.astype(np.int)
    
    for C in Chm:
        INT=CHM11[C].index.intersection(Yb.index)
        if len(INT)>0:
            X = CHM11[C].loc[INT].dropna()
        if X.shape[0]>20:
            print(Effect,C,X.shape)
            P1 = run_genra_cls(X,Yb,Cls=CLS1)
            P2 = run_genra_val(X,Yf,Cls=CLS1)
            P1 = P1.merge(P2,left_index=True,right_index=True)
            P1.insert(0,'tox',Effect)
            P1.insert(1,'dt','chm')
            P1.insert(2,'dn',C)
            PERF.append(P1)
            
    for B in Bio:
        INT1=BIO11[B].index.intersection(Yb.index)
        X = BIO11[B].loc[INT1].dropna()
        Yb= Yb.loc[X.index]
        Yf= Yf.loc[X.index]
        if X.shape[0]>20:
            print(Effect,B,X.shape)
            P1 = run_genra_cls(X,Yb,Cls=CLS1)
            P2 = run_genra_val(X,Yf,Cls=CLS1)
            P1 = P1.merge(P2,left_index=True,right_index=True)
            P1.insert(0,'tox',Effect)
            P1.insert(1,'dt','bio')
            P1.insert(2,'dn',B)
            PERF.append(P1)


# -

PERF_local = pd.concat(PERF).sort_values('auc_score',ascending=False)

PERF_local.head(20)

# dev_liver

# +
PERF=[]
Chm  = list(CHM11.keys())


for Effect in ['dev_liver']:#TOX1.columns:
    #print(Effect)
    Yf = TOX1[Effect].dropna()
    Yf[Yf>0]=1
    Yb = Yf.copy()
    Yb = Yb.astype(np.int)
    
    for C in Chm:
        INT=CHM11[C].index.intersection(Yb.index)
        if len(INT)>0:
            X = CHM11[C].loc[INT].dropna()
        if X.shape[0]>20:
            print(Effect,C,X.shape)
            P1 = run_genra_cls(X,Yb,Cls=CLS1)
            P2 = run_genra_val(X,Yf,Cls=CLS1)
            P1 = P1.merge(P2,left_index=True,right_index=True)
            P1.insert(0,'tox',Effect)
            P1.insert(1,'dt','chm')
            P1.insert(2,'dn',C)
            PERF.append(P1)
            
    for B in Bio:
        INT1=BIO11[B].index.intersection(Yb.index)
        X = BIO11[B].loc[INT1].dropna()
        Yb= Yb.loc[X.index]
        Yf= Yf.loc[X.index]
        if X.shape[0]>20:
            print(Effect,B,X.shape)
            P1 = run_genra_cls(X,Yb,Cls=CLS1)
            P2 = run_genra_val(X,Yf,Cls=CLS1)
            P1 = P1.merge(P2,left_index=True,right_index=True)
            P1.insert(0,'tox',Effect)
            P1.insert(1,'dt','bio')
            P1.insert(2,'dn',B)
            PERF.append(P1)

# -

PERF_local = pd.concat(PERF).sort_values('auc_score',ascending=False)
PERF_local.head(20)

# mgr_liver

# +
PERF=[]
Chm  = list(CHM11.keys())


for Effect in ['mgr_liver']:#TOX1.columns:
    #print(Effect)
    Yf = TOX1[Effect].dropna()
    Yf[Yf>0]=1
    Yb = Yf.copy()
    Yb = Yb.astype(np.int)
    
    for C in Chm:
        INT=CHM11[C].index.intersection(Yb.index)
        if len(INT)>0:
            X = CHM11[C].loc[INT].dropna()
        if X.shape[0]>20:
            print(Effect,C,X.shape)
            P1 = run_genra_cls(X,Yb,Cls=CLS1)
            P2 = run_genra_val(X,Yf,Cls=CLS1)
            P1 = P1.merge(P2,left_index=True,right_index=True)
            P1.insert(0,'tox',Effect)
            P1.insert(1,'dt','chm')
            P1.insert(2,'dn',C)
            PERF.append(P1)
            
    for B in Bio:
        INT1=BIO11[B].index.intersection(Yb.index)
        X = BIO11[B].loc[INT1].dropna()
        Yb= Yb.loc[X.index]
        Yf= Yf.loc[X.index]
        if X.shape[0]>20:
            print(Effect,B,X.shape)
            P1 = run_genra_cls(X,Yb,Cls=CLS1)
            P2 = run_genra_val(X,Yf,Cls=CLS1)
            P1 = P1.merge(P2,left_index=True,right_index=True)
            P1.insert(0,'tox',Effect)
            P1.insert(1,'dt','bio')
            P1.insert(2,'dn',B)
            PERF.append(P1)

# -

PERF_local = pd.concat(PERF).sort_values('auc_score',ascending=False)
PERF_local.head(20)

# sub

# +
PERF=[]
Chm  = list(CHM11.keys())


for Effect in ['sub_liver']:#TOX1.columns:
    #print(Effect)
    Yf = TOX1[Effect].dropna()
    Yf[Yf>0]=1
    Yb = Yf.copy()
    Yb = Yb.astype(np.int)
    
    for C in Chm:
        INT=CHM11[C].index.intersection(Yb.index)
        if len(INT)>0:
            X = CHM11[C].loc[INT].dropna()
        if X.shape[0]>20:
            print(Effect,C,X.shape)
            P1 = run_genra_cls(X,Yb,Cls=CLS1)
            P2 = run_genra_val(X,Yf,Cls=CLS1)
            P1 = P1.merge(P2,left_index=True,right_index=True)
            P1.insert(0,'tox',Effect)
            P1.insert(1,'dt','chm')
            P1.insert(2,'dn',C)
            PERF.append(P1)
            
    for B in Bio:
        INT1=BIO11[B].index.intersection(Yb.index)
        X = BIO11[B].loc[INT1].dropna()
        Yb= Yb.loc[X.index]
        Yf= Yf.loc[X.index]
        if X.shape[0]>20:
            print(Effect,B,X.shape)
            P1 = run_genra_cls(X,Yb,Cls=CLS1)
            P2 = run_genra_val(X,Yf,Cls=CLS1)
            P1 = P1.merge(P2,left_index=True,right_index=True)
            P1.insert(0,'tox',Effect)
            P1.insert(1,'dt','bio')
            P1.insert(2,'dn',B)
            PERF.append(P1)
# -

PERF_local = pd.concat(PERF).sort_values('auc_score',ascending=False)
PERF_local.head(20)

TOX1.columns[TOX1.columns.str.find('_liver')>-1]

# +
PERF=[]
Chm  = list(CHM11.keys())


for Effect in TOX1.filter(regex = '_liver$'):#TOX1.columns:
    #print(Effect)
    Yf = TOX1[Effect].dropna()
    Yf[Yf>0]=1
    Yb = Yf.copy()
    Yb = Yb.astype(np.int)
    
    for C in Chm:
        INT=CHM11[C].index.intersection(Yb.index)
        if len(INT)>0:
            X = CHM11[C].loc[INT].dropna()
        if X.shape[0]>20:
            print(Effect,C,X.shape)
            P1 = run_genra_cls(X,Yb,Cls=CLS1)
            P2 = run_genra_val(X,Yf,Cls=CLS1)
            P1 = P1.merge(P2,left_index=True,right_index=True)
            P1.insert(0,'tox',Effect)
            P1.insert(1,'dt','chm')
            P1.insert(2,'dn',C)
            PERF.append(P1)
            
    for B in Bio:
        INT1=BIO11[B].index.intersection(Yb.index)
        X = BIO11[B].loc[INT1].dropna()
        Yb= Yb.loc[X.index]
        Yf= Yf.loc[X.index]
        if X.shape[0]>20:
            print(Effect,B,X.shape)
            P1 = run_genra_cls(X,Yb,Cls=CLS1)
            P2 = run_genra_val(X,Yf,Cls=CLS1)
            P1 = P1.merge(P2,left_index=True,right_index=True)
            P1.insert(0,'tox',Effect)
            P1.insert(1,'dt','bio')
            P1.insert(2,'dn',B)
            PERF.append(P1)

# -

PERF_local = pd.concat(PERF).sort_values('auc_score',ascending=False)
PERF_local.head(50)

# +
PERF_chm=[]
Chm  = list(CHM11.keys())


for Effect in TOX1.filter(regex = '_liver$'):
    #print(Effect)
    Yf = TOX1[Effect].dropna()
    Yf[Yf>0]=1
    Yb = Yf.copy()
    Yb = Yb.astype(np.int)
    
    for C in Chm:
        INT=CHM11[C].index.intersection(Yb.index)
        if len(INT)>0:
            X = CHM11[C].loc[INT].dropna()
        if X.shape[0]>20:
            print(Effect,C,X.shape)
            P1 = run_genra_cls(X,Yb).to_dict()
            P2 = run_genra_val(X,Yf).to_dict()
            P1.update(P2)
            P1.update(dict(tox=Effect,dt='chm',dn=C))
            PERF_chm.append(P1)
    X = CHM1.loc[INT].dropna()
    if X.shape[0]>20:
        print(C,X.shape)
        P1 = run_genra_cls(X,Yb).to_dict()
        P2 = run_genra_val(X,Yf).to_dict()
        P1.update(P2)
        P1.update(dict(tox=Effect,dt='chm',dn='all'))
        PERF_chm.append(P1)
        break
# -

pd.DataFrame(PERF_chm).sort_values('f1_score',ascending=False)



# +
PERF_bio=[]
Bio  = list(BIO11.keys())

for Effect in TOX1.filter(regex = '_liver$'):
    #print(Effect)
    Yf = TOX1[Effect].dropna()
    Yf[Yf>0]=1
    Yb = Yf.copy()
    Yb = Yb.astype(np.int)
    
    for B in Bio:
        INT1 = BIO11[B].index.intersection(Yb.index)
        if len(INT1)>0:
            X= BIO11[B].loc[INT1].dropna()
            Yb= Yb.loc[X.index]
            Yf= Yf.loc[X.index]
        if X.shape[0]>20:
            print(Effect,B,X.shape)
            P1 = run_genra_cls(X,Yb).to_dict()
            P2 = run_genra_val(X,Yf).to_dict()
            P1.update(P2)
            P1.update(dict(tox=Effect,dt='bio',dn=B))
            PERF_bio.append(P1)

    X = BIO1.loc[INT1]
    Yb= Yb.loc[X.index]
    Yf= Yf.loc[X.index]
    if X.shape[0]>20:
        print(C,X.shape)
        P1 = run_genra_cls(X,Yb).to_dict()
        P2 = run_genra_val(X,Yf).to_dict()
        P1.update(P2)
        P1.update(dict(tox=Effect,dt='bio',dn='all'))
        PERF_bio2.append(P1)
        break
# -

pd.DataFrame(PERF_bio).sort_values('f1_score',ascending=False)

StarcoderdataJupyterScriptsDedupFiltered