#coding: UTF-8 # Code for model comparison import ckdsimulation_ln as ckdsim # you have to import another source code provided as additional file import random import math import numpy as np from multiprocessing import Process, Queue import csv import codecs import time import gc SIM_INT = 4 # Simulation interval (times per year) #----------------------------------- # Model comparison def modelcomp(simtimes, nsize, dec1, dec2, years, bootsize = 10000, initAge = 40, initeGFR = 70, female = 0.5): lifeyear_FTM_total = [] #Fixed Transition probability Model lifeyear_eDM_total = [] #eGFR dependent model Dialen_FTM_total = [] #Dialysis length Dialen_eDM_total = [] CumDialen_FTM_total = [] #Cumulative dialysis length CumDialen_eDM_total = [] for j in range(0, simtimes): # Simulation carried out multiple times # Transition probabilities acquired from input eGFR decline speed p, msim = ckdsim.eGFR2tp(nsize = nsize, splesize = bootsize, x1 = dec1, \ x2 = dec2, years = years, initAge = initAge, initeGFR = initeGFR, \ female = female) # Fixed model virtual cohort using aforementioned transition probailities FTMsim = ckdsim.Fixed_Model_sim(coh = nsize, years = years, initAge = initAge, initStatus = ckdsim.ckdconv(initeGFR), female = female, \ x1 = p[0], x2 = p[1], x3 = p[2], x4 = p[3], x5 = p[4], x6 = p[5]) lifeyear_FTM = [] lifeyear_eDM = [] diapt_FTM = 0 diapt_eDM = 0 Dialen_FTM = [] Dialen_eDM = [] Cumdia_FTM = 0 Cumdia_eDM = 0 for i in range(0, nsize): # Calculate mean life year lifeyear_FTM.append(FTMsim[i][years * SIM_INT - 1][0]) lifeyear_eDM.append(msim[i][years * SIM_INT - 1][0]) hdtmp_FTM = [] hdtmp_eDM = [] for k in range(0, years * SIM_INT): hdtmp_FTM.append(FTMsim[i][k][2] * FTMsim[i][k][3]) hdtmp_eDM.append(ckdsim.ckdconv(msim[i][k][2]) * msim[i][k][3]) # Calculate cumulative dialysis period Cumdia_FTM = Cumdia_FTM + hdtmp_FTM.count(7) / SIM_INT Cumdia_eDM = Cumdia_eDM + hdtmp_eDM.count(7) / SIM_INT # Count patients who initiated dialysis diapt_FTM = diapt_FTM + int(7 in hdtmp_FTM) diapt_eDM = diapt_eDM + int(7 in hdtmp_eDM) # Calculate mean dialysis length of each patient if diapt_FTM != 0: Dialen_FTM.append(Cumdia_FTM / diapt_FTM) else: Dialen_FTM.append(0) if diapt_eDM != 0: Dialen_eDM.append(Cumdia_eDM / diapt_eDM) else: Dialen_eDM.append(0) lifeyear_FTM_total.append(np.mean(lifeyear_FTM)) lifeyear_eDM_total.append(np.mean(lifeyear_eDM)) Dialen_FTM_total.append(np.mean(Dialen_FTM)) Dialen_eDM_total.append(np.mean(Dialen_eDM)) CumDialen_FTM_total.append(Cumdia_FTM) CumDialen_eDM_total.append(Cumdia_eDM) del msim del FTMsim gc.collect() res = [np.mean(lifeyear_FTM_total), np.std(lifeyear_FTM_total), \ np.mean(lifeyear_eDM_total), np.std(lifeyear_eDM_total), \ np.mean(Dialen_FTM_total), np.std(Dialen_FTM_total), \ np.mean(Dialen_eDM_total), np.std(Dialen_eDM_total), \ np.mean(CumDialen_FTM_total), np.std(CumDialen_FTM_total), \ np.mean(CumDialen_eDM_total), np.std(CumDialen_eDM_total)] return res #----------------------------------- if __name__ == "__main__": time1 = time.time() re = modelcomp(20, nsize = 10000, dec1 = 4.8, dec2 = 7.5, years = 60, \ bootsize = 10000, initAge = 35, initeGFR = 75, female = 0.5) # input parameters of 1st base case time2 = time.time() elapsedtime = time2 - time1 print(re) print(elapsedtime)