<jupyter_start><jupyter_text>Loads<jupyter_code>from google.colab import drive
drive.mount('/gdrive')
%cd /gdrive 
cd /gdrive/My\ Drive/nn 
%tensorflow_version 1.x
import argparse
import os
import pickle
import sys
import glob
import matplotlib as mpl
import matplotlib.pyplot as plt
plt.style.use('seaborn')
mpl.use('Agg')
%matplotlib inline

import numpy as np

np.random.seed(7)  # for reproducibility

import tensorflow as tf
tf.random.set_random_seed(5005)

from sklearn.model_selection import train_test_split, KFold
from sklearn.utils import class_weight


from tensorflow.python.keras.models import Model, load_model
from tensorflow.python.keras.layers import Input
from tensorflow.python.keras.layers import Dense, Flatten, Dropout
from tensorflow.python.keras.layers.convolutional import Conv1D
from tensorflow.python.keras.layers.pooling import MaxPooling1D
from tensorflow.python.keras.layers.pooling import AveragePooling1D
from tensorflow.keras.optimizers import Adam
from tensorflow.python.keras.callbacks import ModelCheckpoint, EarlyStopping
import tensorflow.python.keras.backend as K
from keras import regularizers
from tensorflow.python.keras.utils import plot_model 
from sklearn.metrics import precision_recall_curve, average_precision_score,roc_curve, auc , f1_score, classification_report

sys.path.append(".")
import utils
from utils import *

l2_lam = 5e-07 
l1_lam = 1e-08<jupyter_output>TensorFlow 1.x selected.<jupyter_text>Run on Zhou<jupyter_code>zhou_models_folder="./models/zhou"
zhou_all_data = r"dataset/zhou_train_test.pkl"

_,_,_,_, x_test_seq, y_test, _, _ = load_train_validate_test_data(zhou_all_data, input_len=150, kfold=1, only_test=True)

test_seqs = vecs2dna(x_test_seq)

models = load_models(zhou_models_folder)
# Get the scores for the dataset
get_scores(models,x_test_seq, y_test)
y_pred =  predict(models, x_test_seq)
f1 = f1_score(y_test, np.round(y_pred))
print("F1 score is %s" % (f1))
print(classification_report(y_test, np.round(y_pred)))
# Predict based on label 0
y_pred = np.zeros_like(y_test)
total_density_prediction = np.sum((y_test -y_pred) == 0)
accuracy = total_density_prediction / y_test.shape[0] * 100
print("Accuracy using only 0 %s" %accuracy)
# Create accuracy list per label per density
accuracy_list = []
prone_accuracy_list = []
resist_accuracy_list = []
for density_value in range(1,4):
  density_indexes = [i for i in range(len(test_seqs)) if (test_seqs[i].count("CG") == density_value)]
  y_pred =  np.round(predict(models, x_test_seq[density_indexes]))
  density_y_test = y_test[density_indexes]

  # Total
  total_density_prediction = np.sum((density_y_test -y_pred) == 0)
  accuracy = total_density_prediction / density_y_test.shape[0] * 100
  accuracy_list.append(accuracy)

  # Prone
  second_ind = density_y_test == 1
  prone_y_pred = y_pred[second_ind]
  y_good= density_y_test[second_ind]
  total_density_prediction = np.sum((y_good -prone_y_pred) == 0)
  if prone_y_pred.shape[0] == 0:
    accuracy = -1
  else:
    accuracy = total_density_prediction / prone_y_pred.shape[0] * 100
  prone_accuracy_list.append(accuracy)

  # Resist
  second_ind = density_y_test == 0
  resist_y_pred = y_pred[second_ind]
  y_good= density_y_test[second_ind]
  total_density_prediction = np.sum((y_good -resist_y_pred) == 0)
  if resist_y_pred.shape[0] == 0:
    accuracy = -1
  else:
    accuracy = total_density_prediction / resist_y_pred.shape[0] * 100
  resist_accuracy_list.append(accuracy)

ind = np.arange(3)  
width = 0.25       # the width of the bars

fig = plt.figure(1, dpi=200)
ax = fig.add_subplot(111)

rects1 = ax.bar(ind, accuracy_list, width, edgecolor='white', label="Total")
rects2 = ax.bar(ind+width, prone_accuracy_list, width,edgecolor='white', label="Hypo-Prone")
rects3 = ax.bar(ind+width*2, resist_accuracy_list, width, edgecolor='white', label="Hypo-Resistant")

ax.set_ylabel('% of True Prediction',fontsize="xx-large")
ax.set_xlabel("Density",fontsize="xx-large")
ax.set_xticks(ind+width)
ax.set_xticklabels( ["1","2","3"])

def autolabel(rects):
    for rect in rects:
        h = rect.get_height()
        ax.text(rect.get_x()+rect.get_width()/2., 1.05*h, '%d'%int(h),
                ha='center', va='bottom')

autolabel(rects1)
autolabel(rects2)
autolabel(rects3)

# Put a legend to the right of the current axis
ax.legend(loc='center left', bbox_to_anchor=(1, 0.5),fontsize="xx-large")
plt.xticks(fontsize="xx-large")
plt.yticks(fontsize="xx-large")
fig.savefig("sup2_b_left_up.pdf", bbox_inches='tight')

modelsb = load_models("./models/bian")
y_pred =  predict(modelsb, x_test_seq)
# Create accuracy list per label per density
accuracy_list = []
prone_accuracy_list = []
resist_accuracy_list = []
for density_value in range(1,4):
  density_indexes = [i for i in range(len(test_seqs)) if (test_seqs[i].count("CG") == density_value)]
  y_pred =  np.round(predict(modelsb, x_test_seq[density_indexes]))
  density_y_test = y_test[density_indexes]

  # Total
  total_density_prediction = np.sum((density_y_test -y_pred) == 0)
  accuracy = total_density_prediction / density_y_test.shape[0] * 100
  accuracy_list.append(accuracy)

  # Prone
  second_ind = density_y_test == 1
  prone_y_pred = y_pred[second_ind]
  y_good= density_y_test[second_ind]
  total_density_prediction = np.sum((y_good -prone_y_pred) == 0)
  if prone_y_pred.shape[0] == 0:
    accuracy = -1
  else:
    accuracy = total_density_prediction / prone_y_pred.shape[0] * 100
  prone_accuracy_list.append(accuracy)

  # Resist
  second_ind = density_y_test == 0
  resist_y_pred = y_pred[second_ind]
  y_good= density_y_test[second_ind]
  total_density_prediction = np.sum((y_good -resist_y_pred) == 0)
  if resist_y_pred.shape[0] == 0:
    accuracy = -1
  else:
    accuracy = total_density_prediction / resist_y_pred.shape[0] * 100
  resist_accuracy_list.append(accuracy)

ind = np.arange(3)  
width = 0.25       # the width of the bars

fig = plt.figure(1, dpi=200)
ax = fig.add_subplot(111)

rects1 = ax.bar(ind, accuracy_list, width, edgecolor='white', label="Total")
rects2 = ax.bar(ind+width, prone_accuracy_list, width,edgecolor='white', label="Hypo-Prone")
rects3 = ax.bar(ind+width*2, resist_accuracy_list, width, edgecolor='white', label="Hypo-Resistant")

ax.set_ylabel('% of True Prediction',fontsize="xx-large")
ax.set_xlabel("Density",fontsize="xx-large")
ax.set_xticks(ind+width)
ax.set_xticklabels( ["1","2","3"])

def autolabel(rects):
    for rect in rects:
        h = rect.get_height()
        ax.text(rect.get_x()+rect.get_width()/2., 1.05*h, '%d'%int(h),
                ha='center', va='bottom')

autolabel(rects1)
autolabel(rects2)
autolabel(rects3)

# Put a legend to the right of the current axis
ax.legend(loc='center left', bbox_to_anchor=(1, 0.5),fontsize="xx-large")
plt.xticks(fontsize="xx-large")
plt.yticks(fontsize="xx-large")
fig.savefig("sup2_b_right_up.pdf", bbox_inches='tight')<jupyter_output><empty_output><jupyter_text>Run on the scwgbs (Bian) dataset<jupyter_code>scgwbs_models_folder="./models/bian"
scgwbs_data = r"dataset/bian_crc01_train_test.pkl"

_,_,_,_, x_test_seq, y_test, _, _ = load_train_validate_test_data(scgwbs_data, input_len=150, kfold=1, only_test=True)

test_seqs = vecs2dna(x_test_seq)
models = load_models(scgwbs_models_folder)
# Get the scores for the dataset
get_scores(models,x_test_seq, y_test)
y_pred =  predict(models, x_test_seq)
f1 = f1_score(y_test, np.round(y_pred))
print("F1 score is %s" % (f1))
print(classification_report(y_test, np.round(y_pred)))
# Predict based on label 0
y_pred = np.zeros_like(y_test)
total_density_prediction = np.sum((y_test -y_pred) == 0)
accuracy = total_density_prediction / y_test.shape[0] * 100
print("Accuracy using only 0 %s" %accuracy)
# Create accuracy list per label per density
accuracy_list = []
prone_accuracy_list = []
resist_accuracy_list = []
for density_value in range(1,4):
  density_indexes = [i for i in range(len(test_seqs)) if (test_seqs[i].count("CG") == density_value)]
  y_pred =  np.round(predict(models, x_test_seq[density_indexes]))
  density_y_test = y_test[density_indexes]

  # Total
  total_density_prediction = np.sum((density_y_test -y_pred) == 0)
  accuracy = total_density_prediction / density_y_test.shape[0] * 100
  accuracy_list.append(accuracy)

  # Prone
  second_ind = density_y_test == 1
  prone_y_pred = y_pred[second_ind]
  y_good= density_y_test[second_ind]
  total_density_prediction = np.sum((y_good -prone_y_pred) == 0)
  if prone_y_pred.shape[0] == 0:
    accuracy = -1
  else:
    accuracy = total_density_prediction / prone_y_pred.shape[0] * 100
  prone_accuracy_list.append(accuracy)

  # Resist
  second_ind = density_y_test == 0
  resist_y_pred = y_pred[second_ind]
  y_good= density_y_test[second_ind]
  total_density_prediction = np.sum((y_good -resist_y_pred) == 0)
  if resist_y_pred.shape[0] == 0:
    accuracy = -1
  else:
    accuracy = total_density_prediction / resist_y_pred.shape[0] * 100
  resist_accuracy_list.append(accuracy)

ind = np.arange(3)  
width = 0.25       # the width of the bars

fig = plt.figure(1, dpi=200)
ax = fig.add_subplot(111)

rects1 = ax.bar(ind, accuracy_list, width, edgecolor='white', label="Total")
rects2 = ax.bar(ind+width, prone_accuracy_list, width,edgecolor='white', label="Hypo-Prone")
rects3 = ax.bar(ind+width*2, resist_accuracy_list, width, edgecolor='white', label="Hypo-Resistant")

ax.set_ylabel('% of True Prediction',fontsize="xx-large")
ax.set_xlabel("Density",fontsize="xx-large")
ax.set_xticks(ind+width)
ax.set_xticklabels( ["1","2","3"])

def autolabel(rects):
    for rect in rects:
        h = rect.get_height()
        ax.text(rect.get_x()+rect.get_width()/2., 1.05*h, '%d'%int(h),
                ha='center', va='bottom')

autolabel(rects1)
autolabel(rects2)
autolabel(rects3)

# Put a legend to the right of the current axis
ax.legend(loc='center left', bbox_to_anchor=(1, 0.5),fontsize="xx-large")
plt.xticks(fontsize="xx-large")
plt.yticks(fontsize="xx-large")
fig.savefig("sup2_b_right_down.pdf", bbox_inches='tight')
# Create accuracy list per label per density

modelsz = load_models(zhou_models_folder)

accuracy_list = []
prone_accuracy_list = []
resist_accuracy_list = []
for density_value in range(1,4):
  density_indexes = [i for i in range(len(test_seqs)) if (test_seqs[i].count("CG") == density_value)]
  y_pred =  np.round(predict(modelsz, x_test_seq[density_indexes]))
  density_y_test = y_test[density_indexes]

  # Total
  total_density_prediction = np.sum((density_y_test -y_pred) == 0)
  accuracy = total_density_prediction / density_y_test.shape[0] * 100
  accuracy_list.append(accuracy)

  # Prone
  second_ind = density_y_test == 1
  prone_y_pred = y_pred[second_ind]
  y_good= density_y_test[second_ind]
  total_density_prediction = np.sum((y_good -prone_y_pred) == 0)
  if prone_y_pred.shape[0] == 0:
    accuracy = -1
  else:
    accuracy = total_density_prediction / prone_y_pred.shape[0] * 100
  prone_accuracy_list.append(accuracy)

  # Resist
  second_ind = density_y_test == 0
  resist_y_pred = y_pred[second_ind]
  y_good= density_y_test[second_ind]
  total_density_prediction = np.sum((y_good -resist_y_pred) == 0)
  if resist_y_pred.shape[0] == 0:
    accuracy = -1
  else:
    accuracy = total_density_prediction / resist_y_pred.shape[0] * 100
  resist_accuracy_list.append(accuracy)

ind = np.arange(3)  
width = 0.25       # the width of the bars

fig = plt.figure(1, dpi=200)
ax = fig.add_subplot(111)

rects1 = ax.bar(ind, accuracy_list, width, edgecolor='white', label="Total")
rects2 = ax.bar(ind+width, prone_accuracy_list, width,edgecolor='white', label="Hypo-Prone")
rects3 = ax.bar(ind+width*2, resist_accuracy_list, width, edgecolor='white', label="Hypo-Resistant")

ax.set_ylabel('% of True Prediction',fontsize="xx-large")
ax.set_xlabel("Density",fontsize="xx-large")
ax.set_xticks(ind+width)
ax.set_xticklabels( ["1","2","3"])

def autolabel(rects):
    for rect in rects:
        h = rect.get_height()
        ax.text(rect.get_x()+rect.get_width()/2., 1.05*h, '%d'%int(h),
                ha='center', va='bottom')

autolabel(rects1)
autolabel(rects2)
autolabel(rects3)

# Put a legend to the right of the current axis
ax.legend(loc='center left', bbox_to_anchor=(1, 0.5),fontsize="xx-large")
plt.xticks(fontsize="xx-large")
plt.yticks(fontsize="xx-large")
fig.savefig("sup2_b_left_down.pdf", bbox_inches='tight')<jupyter_output><empty_output>
StarcoderdataJupyterStructuredCleanDedup