<reponame>20032303092/StackEPI
# In[ ]:
import math
import os
import time

os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
os.environ["CUDA_VISIBLE_DEVICES"] = "0"
from model import get_model
import numpy as np
from keras.callbacks import Callback
from datetime import datetime
from sklearn.metrics import roc_auc_score, average_precision_score
from sklearn.model_selection import train_test_split


def time_since(start):
    s = time.time() - start
    # s = 62 - start
    m = math.floor(s / 60)
    s -= m * 60
    h = math.floor(m / 60)
    m -= h * 60
    return '%dh %dm %ds' % (h, m, s)


class roc_callback(Callback):
    def __init__(self, val_data, name):
        self.en = val_data[0]
        self.pr = val_data[1]
        self.y = val_data[2]
        self.name = name

    def on_train_begin(self, logs={}):
        return

    def on_train_end(self, logs={}):
        return

    def on_epoch_begin(self, epoch, logs={}):
        return

    def on_epoch_end(self, epoch, logs={}):
        y_pred = self.model.predict([self.en, self.pr])
        auc_val = roc_auc_score(self.y, y_pred)
        aupr_val = average_precision_score(self.y, y_pred)

        self.model.save_weights("./model/specificModel/%sModel%d.h5" % (self.name, epoch))

        print('\r auc_val: %s ' % str(round(auc_val, 4)), end=100 * ' ' + '\n')
        print('\r aupr_val: %s ' % str(round(aupr_val, 4)), end=100 * ' ' + '\n')
        return

    def on_batch_begin(self, batch, logs={}):
        return

    def on_batch_end(self, batch, logs={}):
        return


# names = ['HUVEC', 'HeLa-S3', 'K562', 'NHEK', 'all', 'all-NHEK']
msgs = []
names = ['GM12878', 'HUVEC', 'HeLa-S3', 'IMR90', 'K562', 'NHEK']
for name in names:
    start = time.time()
    t1 = datetime.now().strftime('%Y-%m-%d-%H:%M:%S')
    # The data used here is the sequence processed by data_processing.py.
    Data_dir = '../data/epivan/%s/' % name
    train = np.load(Data_dir + '%s_train.npz' % name)
    # test=np.load(Data_dir+'%s_test.npz'%name)
    X_en_tra, X_pr_tra, y_tra = train['X_en_tra'], train['X_pr_tra'], train['y_tra']
    # X_en_tes,X_pr_tes,y_tes=test['X_en_tes'],test['X_pr_tes'],test['y_tes']

    X_en_tra, X_en_val, X_pr_tra, X_pr_val, y_tra, y_val = train_test_split(
        X_en_tra, X_pr_tra, y_tra, test_size=0.05, stratify=y_tra, random_state=250)

    model = get_model()
    model.summary()
    print('Traing %s cell line specific model...' % name)

    back = roc_callback(val_data=[X_en_val, X_pr_val, y_val], name=name)
    history = model.fit([X_en_tra, X_pr_tra], y_tra, validation_data=([X_en_val, X_pr_val], y_val), epochs=15,
                        batch_size=64,
                        callbacks=[back])

    t2 = datetime.now().strftime('%Y-%m-%d-%H:%M:%S')
    msg = name + " 开始时间:" + t1 + " 结束时间：" + t2 + " spend time: " + time_since(start)
    print(msg)
    msgs.append(msg)

for msg in msgs:
    print(msg)

# In[ ]:
import math
import os
import time

os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
os.environ["CUDA_VISIBLE_DEVICES"] = "0"
from model import get_model
import numpy as np
from keras.callbacks import Callback
from datetime import datetime
from sklearn.metrics import roc_auc_score, average_precision_score
from sklearn.model_selection import train_test_split


def time_since(start):
    s = time.time() - start
    # s = 62 - start
    m = math.floor(s / 60)
    s -= m * 60
    h = math.floor(m / 60)
    m -= h * 60
    return '%dh %dm %ds' % (h, m, s)


class roc_callback(Callback):
    def __init__(self, val_data, name):
        self.en = val_data[0]
        self.pr = val_data[1]
        self.y = val_data[2]
        self.name = name

    def on_train_begin(self, logs={}):
        return

    def on_train_end(self, logs={}):
        return

    def on_epoch_begin(self, epoch, logs={}):
        return

    def on_epoch_end(self, epoch, logs={}):
        y_pred = self.model.predict([self.en, self.pr])
        auc_val = roc_auc_score(self.y, y_pred)
        aupr_val = average_precision_score(self.y, y_pred)

        self.model.save_weights("./model/specificModel/%sModel%d.h5" % (self.name, epoch))

        print('\r auc_val: %s ' % str(round(auc_val, 4)), end=100 * ' ' + '\n')
        print('\r aupr_val: %s ' % str(round(aupr_val, 4)), end=100 * ' ' + '\n')
        return

    def on_batch_begin(self, batch, logs={}):
        return

    def on_batch_end(self, batch, logs={}):
        return


# names = ['HUVEC', 'HeLa-S3', 'K562', 'NHEK', 'all', 'all-NHEK']
msgs = []
names = ['GM12878', 'HUVEC', 'HeLa-S3', 'IMR90', 'K562', 'NHEK']
for name in names:
    start = time.time()
    t1 = datetime.now().strftime('%Y-%m-%d-%H:%M:%S')
    # The data used here is the sequence processed by data_processing.py.
    Data_dir = '../data/epivan/%s/' % name
    train = np.load(Data_dir + '%s_train.npz' % name)
    # test=np.load(Data_dir+'%s_test.npz'%name)
    X_en_tra, X_pr_tra, y_tra = train['X_en_tra'], train['X_pr_tra'], train['y_tra']
    # X_en_tes,X_pr_tes,y_tes=test['X_en_tes'],test['X_pr_tes'],test['y_tes']

    X_en_tra, X_en_val, X_pr_tra, X_pr_val, y_tra, y_val = train_test_split(
        X_en_tra, X_pr_tra, y_tra, test_size=0.05, stratify=y_tra, random_state=250)

    model = get_model()
    model.summary()
    print('Traing %s cell line specific model...' % name)

    back = roc_callback(val_data=[X_en_val, X_pr_val, y_val], name=name)
    history = model.fit([X_en_tra, X_pr_tra], y_tra, validation_data=([X_en_val, X_pr_val], y_val), epochs=15,
                        batch_size=64,
                        callbacks=[back])

    t2 = datetime.now().strftime('%Y-%m-%d-%H:%M:%S')
    msg = name + " 开始时间:" + t1 + " 结束时间：" + t2 + " spend time: " + time_since(start)
    print(msg)
    msgs.append(msg)

for msg in msgs:
    print(msg)
# In[ ]: # s = 62 - start # names = ['HUVEC', 'HeLa-S3', 'K562', 'NHEK', 'all', 'all-NHEK'] # The data used here is the sequence processed by data_processing.py. # test=np.load(Data_dir+'%s_test.npz'%name) # X_en_tes,X_pr_tes,y_tes=test['X_en_tes'],test['X_pr_tes'],test['y_tes']