<jupyter_start><jupyter_text>Overall<jupyter_code>def getRange(x):
    return max(x)-min(x)
plt.figure()
plt.plot([-3,2], [-3,2], ls="--", c=".3", alpha=0.5)
ax = sns.scatterplot(y_compr_te['actual'].values.flatten(), y_compr_te['predicted'].values.flatten(),
                     s = 1, alpha=0.05, linewidth=0, color='steelblue')
ax.set(xlabel='Actual', ylabel='Predicted', xlim=[-3,2], ylim=[-3,2])
# get the R2 scores
df_scores = pd.read_csv('%s/anlyz/agg_summary.csv' % dir_in_Lx, index_col=0)
# get actual/predicted from model
actual = pd.melt(y_compr_te['actual'])
actual = actual.rename(columns={'variable':'gene'})
actual.set_index('gene', drop=True, inplace=True)
pred = pd.melt(y_compr_te['predicted'])
pred = pred.rename(columns={'variable':'gene'})
pred.set_index('gene', drop=True, inplace=True)
df = pd.concat([actual,pred], axis=1)
df.columns = ['actual', 'predicted']
df_merged = df<jupyter_output><empty_output><jupyter_text>correlationCompare the CERES score ranges between actual and predicted<jupyter_code>dm_data_pkl_file = '../out/20.0817 proc_data/gene_effect/dm_data.pkl'
dm_data = pickle.load(open(dm_data_pkl_file,'rb'))
df_crispr = dm_data.df_crispr.copy()
df_crispr.columns = df_crispr.columns.str.extract('^(.*)\s').squeeze().values
df_crispr.columns = df_crispr.columns.str.extract('^(.*)\s').squeeze().values
range_actual = df_crispr.apply(lambda x: getRange(x), axis=0)
range_pred = df_merged.groupby('gene').apply(lambda x: getRange(x['predicted']))
ax = sns.scatterplot(range_actual, range_pred)
ax.set(xlabel='actual CERES range', ylabel='predicted CERES range')<jupyter_output><empty_output><jupyter_text>How many models are predicting just more/less a constant?<jupyter_code>pred_range = df_merged.groupby('gene').apply(lambda x: getRange(x['predicted']))
pred_range = pred_range.rename('0')
pred_range.plot.hist()
c = (pred_range < 0.45).sum()
pct = c / len(pred_range)
print("total number of models that's predicting more or less constants: %d" % c)
print("%% of models that's predicting more or less constants: %.2f" % pct)
c = (pred_range > 0.45).sum()
pct = c / len(pred_range)
print("total number of models that's predicting with differential values: %d" % c)
print("%% of models that's predicting with differential values: %.2f" % pct)<jupyter_output>total number of models that's predicting with differential values: 698
% of models that's predicting with differential values: 0.04<jupyter_text>Check specific genes<jupyter_code>gene_names = pred_range.index[(pred_range < 0.4) & (pred_range > 0.35)]
gene_names[1]
gene_name = 'AAAS'
res = pickle.load(open('%s/model_perf/y_compr_%s.pkl' % (dir_in_Lx, gene_name),'rb'))
df = res['te']
plt.plot([-2.5,1], [-2.5,1], ls="--", c=".3", alpha=0.5)
ax = sns.scatterplot(df.y_actual, df.y_pred)
ax.set(xlabel='actual', ylabel='predicted', xlim=[-2.5,1], ylim=[-2.5,1])<jupyter_output><empty_output><jupyter_text>Break down by class (based on strict cut-offs of CERES)<jupyter_code># alternatively get dependency class by gene effect cut-offs
dm_data_pkl_file = '../out/20.0817 proc_data/gene_effect/dm_data.pkl'
dm_data = pickle.load(open(dm_data_pkl_file,'rb'))

def classifyDep(x):
    if all(x < -0.5):
        return 'common_essential (<-0.5)'
    elif all((x < 0.5) & (x > -0.5)):
        return 'common_nonessential (near 0s)'
    else:
        return 'selective_essential (others)'

df_crispr = dm_data.df_crispr.copy()
df_crispr.columns = df_crispr.columns.str.extract('^(.*)\s').squeeze().values
df_crispr.columns = df_crispr.columns.str.extract('^(.*)\s').squeeze().values
dep_class = df_crispr.apply(lambda x: classifyDep(x), axis=0)
dep_class = dep_class.rename('0')
dep_class.value_counts()
dep_class.value_counts() / dep_class.shape[0] # percentage
# get actual/predicted from model
actual = pd.melt(y_compr_te['actual'])
actual = actual.rename(columns={'variable':'gene'})
actual.set_index('gene', drop=True, inplace=True)
pred = pd.melt(y_compr_te['predicted'])
pred = pred.rename(columns={'variable':'gene'})
pred.set_index('gene', drop=True, inplace=True)
df = pd.concat([actual,pred], axis=1)
df.columns = ['actual', 'predicted']
df_merged = df.merge(dep_class, left_index=True, right_index=True)
df_merged.columns = ['actual', 'predicted', 'class']
g = sns.FacetGrid(df_merged, col="class")
g.map(sns.scatterplot, 'actual', 'predicted',
      s = 1, alpha=0.05, linewidth=0, color='steelblue')
plt.figure()
plt.plot([-3,2], [-3,2], ls="--", c=".3", alpha=0.5)
ax = sns.scatterplot(x='actual', y='predicted', data=df_merged, hue=df_merged['class'].tolist(),
                     s = 1, alpha=0.05, linewidth=0)
ax.set(xlabel='Actual', ylabel='Predicted', xlim=[-3,2], ylim=[-3,2])<jupyter_output><empty_output><jupyter_text>CorrelationCompare the CERES score ranges between actual and predicted<jupyter_code>df = pd.concat([range_actual, range_pred], axis=1)
df = df.merge(dep_class, left_index=True, right_index=True)
df.columns = ['actual','predicted','class']
g = sns.FacetGrid(df, col="class")
g.map(sns.scatterplot, 'actual', 'predicted',
      s = 2, alpha=0.8, linewidth=0, color='steelblue')
from scipy.stats import spearmanr
def calcCorr(x):
    a = x.dropna()
    return spearmanr(a['actual'], a['predicted'])
    
df.groupby('class').apply(calcCorr)<jupyter_output><empty_output><jupyter_text>Scores<jupyter_code>df = df_scores.merge(dep_class, left_index=True, right_index=True)
c = df.columns[:-1].tolist()
c.append('class')
df.columns = c
g = sns.FacetGrid(df, col="class")
g.map(sns.boxplot, 'score_full',color='steelblue')
df.groupby('class').apply(np.mean).T
df.groupby('class').aggregate(np.median).T<jupyter_output><empty_output><jupyter_text>Predicted range<jupyter_code>df = df_scores.merge(dep_class, left_index=True, right_index=True)
c = df.columns[:-1].tolist()
c.append('class')
df.columns = c

df = df.merge(pred_range, left_index=True, right_index=True)
c = df.columns[:-1].tolist()
c.append('pred_range')
df.columns = c
df['constant'] = df.pred_range<0.45
# number of models in each group that are constant models
df.groupby('class')['constant'].apply(sum)
# % of models in each group that are constant models
df.groupby('class')['constant'].apply(lambda x: sum(x)/len(x))<jupyter_output><empty_output><jupyter_text>Out of 18333 targets, - ~2K genes are mostly common non essentials: recall 0.82 | 100% models are constant models- ~537 genes are mostly common essentials: recall 0.98 | 80% models are constant models- ~15K genes are others: recall 0.87 | 96% models are constant models  Break down by SD and range (and looser def of common non/essentials, allowing 1-2 genes outside of range)<jupyter_code># alternatively get dependency class by gene effect cut-offs
def classifyDep(x):
    n = len(x)
    if (np.std(x)>0.25) and ((max(x)-min(x))>0.6) and (sum(x<-0.5)>0):
        return 'selective essential (sd and range)'
    elif (n - sum((x < 0.5) & (x > -0.5))) < 3 :
        return 'mostly common_nonessential (near 0s)'
    if (n - sum(x < -0.5)) < 3:
        return 'mostly common_essential (<-0.5)'
    else:
        return 'Others'

df_crispr = dm_data.df_crispr.copy()
df_crispr.columns = df_crispr.columns.str.extract('^(.*)\s').squeeze().values
df_crispr.columns = df_crispr.columns.str.extract('^(.*)\s').squeeze().values
dep_class = df_crispr.apply(lambda x: classifyDep(x), axis=0)
dep_class = dep_class.rename('0')
dep_class.value_counts()
dep_class.value_counts() / dep_class.shape[0] # percentage
# get actual/predicted from model
actual = pd.melt(y_compr_te['actual'])
actual = actual.rename(columns={'variable':'gene'})
actual.set_index('gene', drop=True, inplace=True)
pred = pd.melt(y_compr_te['predicted'])
pred = pred.rename(columns={'variable':'gene'})
pred.set_index('gene', drop=True, inplace=True)
df = pd.concat([actual,pred], axis=1)
df.columns = ['actual', 'predicted']
df_merged = df.merge(dep_class, left_index=True, right_index=True)
df_merged.columns = ['actual', 'predicted', 'class']
g = sns.FacetGrid(df_merged, col="class")
g.map(sns.scatterplot, 'actual', 'predicted',
      s = 1, alpha=0.05, linewidth=0, color='steelblue')<jupyter_output><empty_output><jupyter_text>CorrelationCompare the CERES score ranges between actual and predicted<jupyter_code>df = pd.concat([range_actual, range_pred], axis=1)
df = df.merge(dep_class, left_index=True, right_index=True)
df.columns = ['actual','predicted','class']
g = sns.FacetGrid(df, col="class")
g.map(sns.scatterplot, 'actual', 'predicted',
      s = 2, alpha=0.8, linewidth=0, color='steelblue')
from scipy.stats import spearmanr
def calcCorr(x):
    a = x.dropna()
    return spearmanr(a['actual'], a['predicted'])
    
df.groupby('class').apply(calcCorr)<jupyter_output><empty_output><jupyter_text>Scores<jupyter_code>df = df_scores.merge(dep_class, left_index=True, right_index=True)
c = df.columns[:-1].tolist()
c.append('class')
df.columns = c
g = sns.FacetGrid(df, col="class")
g.map(sns.boxplot, 'score_full',color='steelblue')
df.groupby('class').apply(np.mean).T
df.groupby('class').aggregate(np.median).T<jupyter_output><empty_output><jupyter_text>Predicted range<jupyter_code>df = df_scores.merge(dep_class, left_index=True, right_index=True)
c = df.columns[:-1].tolist()
c.append('class')
df.columns = c

df = df.merge(pred_range, left_index=True, right_index=True)
c = df.columns[:-1].tolist()
c.append('pred_range')
df.columns = c
df['constant'] = df.pred_range<0.45
# number of models in each group that are constant models
df.groupby('class')['constant'].apply(sum)
# % of models in each group that are constant models
df.groupby('class')['constant'].apply(lambda x: sum(x)/len(x))<jupyter_output><empty_output><jupyter_text>Out of 18333 targets, - ~7K genes are mostly common non essentials: recall 0.81 | 100% of models are constant models- ~600 genes are mostly common essentials: recall 0.98 | 86% of models are constant models- ~161 genes are differential essentials: recall 0.99 | 31% of models are constant models- ~10K genes are others: recall 0.89 | 97% of models are constant models  Break down by class (based on gene dependency)<jupyter_code># dependency class based on gene dependency (which is a model fit, to get at the probability of being essential)
dep_class = pd.read_csv('../out/20.0817 proc_data_baseline/gene_effect/gene_essential_classification.csv', header=None, index_col=0, squeeze=True)
dep_class.value_counts()
dep_class.value_counts() / dep_class.shape[0] # percentage
# get actual/predicted from model
actual = pd.melt(y_compr_te['actual'])
actual = actual.rename(columns={'variable':'gene'})
actual.set_index('gene', drop=True, inplace=True)
pred = pd.melt(y_compr_te['predicted'])
pred = pred.rename(columns={'variable':'gene'})
pred.set_index('gene', drop=True, inplace=True)
df = pd.concat([actual,pred], axis=1)
df.columns = ['actual', 'predicted']
df_merged = df.merge(dep_class, left_index=True, right_index=True)
df_merged.columns = ['actual', 'predicted', 'class']
g = sns.FacetGrid(df_merged, col="class")
g.map(sns.scatterplot, 'actual', 'predicted',
      s = 1, alpha=0.05, linewidth=0, color='steelblue')
plt.figure()
plt.plot([-3,2], [-3,2], ls="--", c=".3", alpha=0.5)
ax = sns.scatterplot(x='actual', y='predicted', data=df_merged, hue=df_merged['class'].tolist(),
                     s = 1, alpha=0.05, linewidth=0)
ax.set(xlabel='Actual', ylabel='Predicted', xlim=[-3,2], ylim=[-3,2])<jupyter_output><empty_output><jupyter_text>CorrelationCompare the CERES score ranges between actual and predicted<jupyter_code>df = pd.concat([range_actual, range_pred], axis=1)
df = df.merge(dep_class, left_index=True, right_index=True)
df.columns = ['actual','predicted','class']
g = sns.FacetGrid(df, col="class")
g.map(sns.scatterplot, 'actual', 'predicted',
      s = 2, alpha=0.8, linewidth=0, color='steelblue')
from scipy.stats import spearmanr
def calcCorr(x):
    a = x.dropna()
    return spearmanr(a['actual'], a['predicted'])
    
df.groupby('class').apply(calcCorr)<jupyter_output><empty_output><jupyter_text>Scores<jupyter_code>df = df_scores.merge(dep_class, left_index=True, right_index=True)
c = df.columns[:-1].tolist()
c.append('class')
df.columns = c
g = sns.FacetGrid(df, col="class")
g.map(sns.boxplot, 'score_full',color='steelblue')
df.groupby('class').apply(np.mean).T
df.groupby('class').aggregate(np.median).T<jupyter_output><empty_output><jupyter_text>Predicted range<jupyter_code>df = df_scores.merge(dep_class, left_index=True, right_index=True)
c = df.columns[:-1].tolist()
c.append('class')
df.columns = c

df = df.merge(pred_range, left_index=True, right_index=True)
c = df.columns[:-1].tolist()
c.append('pred_range')
df.columns = c
df['constant'] = df.pred_range<0.45
# number of models in each group that are constant models
df.groupby('class')['constant'].apply(sum)
# % of models in each group that are constant models
df.groupby('class')['constant'].apply(lambda x: sum(x)/len(x))<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.17264851485148514, 'fraction_numerical': 0.019879331683168317, 'mean_word_length': 6.206800445930881, 'pattern_counts': {'":': 0, '<': 78, '<?xml version=': 0, '>': 71, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '16', 'dir': 'jupyter-structured-clean-dedup', 'id': '284617', 'license': 'MIT', 'n_tokens_mistral': 5009, 'n_tokens_neox': 4782, 'n_words': 945, 'path': 'notebooks/19a-anlyz_run04_byGeneClass.ipynb', 'repo_name': 'pritchardlabatpsu/cga', 'size': '12928'}