def getTrainingTimeCourseDataDict(training_data_dict, data_dir): trainingTimeCourseDataDict = {} for FullPN, ECDFileName in training_data_dict.items(): aTimeCouse = ECDDataFile() aTimeCouse.load(os.sep.join((data_dir.rstrip(os.sep), ECDFileName))) trainingTimeCourseDataDict[FullPN] = dict(ECDFileName=ECDFileName, ECDData=aTimeCouse) return trainingTimeCourseDataDict
def saveECD(LoggerList, FullPN): Data = LoggerList[0].getData() Data = np.matrix(Data) Data = Data[:, 0] Data = Data.tolist() rabelrows = ['time'] for i in range(len(LoggerList)): Next_Data = LoggerList[i].getData() rabel = LoggerList[i].getName() if len(Next_Data) > 1: rabelrows.append(rabel) else: pass for j in range(len(Data)): if len(Next_Data) > RECORDING_TIME * 200: Data[j].append(Next_Data[2 * j][1]) elif len(Next_Data) > RECORDING_TIME * 100: Data[j].append(Next_Data[j][1]) else: pass Data.insert(0, rabelrows) Data = np.array(Data) ECDFile = ECDDataFile(Data) ECDFile.setDataName(VmLogger.getName()) ECDFile.setNote(FullPN + 'is' + str(VALUE_OF_GX)) FullPN = FullPN.replace(':', '_') FullPN = FullPN.replace('/', '_') filename = FullPN + str(VALUE_OF_GX) + '.ecd' ECDFile.save(filename)
def saveECD(LoggerList, FullPN): Data = LoggerList[0].getData() Data = np.matrix(Data) Data = Data[:, 0] Data = Data.tolist() rabelrows = ["time"] for i in range(len(LoggerList)): Next_Data = LoggerList[i].getData() rabel = LoggerList[i].getName() if len(Next_Data) > 1: rabelrows.append(rabel) else: pass for j in range(len(Data)): if len(Next_Data) > RECORDING_TIME * 200: Data[j].append(Next_Data[2 * j][1]) elif len(Next_Data) > RECORDING_TIME * 100: Data[j].append(Next_Data[j][1]) else: pass Data.insert(0, rabelrows) Data = np.array(Data) ECDFile = ECDDataFile(Data) ECDFile.setDataName(VmLogger.getName()) ECDFile.setNote(FullPN + "is" + str(VALUE_OF_GX)) FullPN = FullPN.replace(":", "_") FullPN = FullPN.replace("/", "_") filename = FullPN + str(VALUE_OF_GX) + ".ecd" ECDFile.save(filename)
def saveLoggerData(self, fullpn=0, aSaveDirectory='./Data', aStartTime=-1, anEndTime=-1, anInterval=-1): # ------------------------------------------------- # Check type. # ------------------------------------------------- aLoggerNameList = [] if type(fullpn) == str: aLoggerNameList.append(fullpn) elif not fullpn: aLoggerNameList = self.getLoggerList() elif type(fullpn) == list: aLoggerNameList = fullpn elif type(fullpn) == tuple: aLoggerNameList = fullpn else: self.message( "%s is not suitable type.\nuse string or list or tuple" % fullpn) return # ------------------------------------------------- # Execute saving. # ------------------------------------------------- if not os.path.isdir(aSaveDirectory): os.mkdir(aSaveDirectory) # creates instance datafilemanager aDataFileManager = DataFileManager() # sets root directory to datafilemanager aDataFileManager.setRootDirectory(aSaveDirectory) aFileIndex = 0 # gets all list of selected property name for aFullPNString in aLoggerNameList: #(2) # ------------------------------------------------- # from [Variable:/CELL/CYTOPLASM:E:Value] # to [Variable_CELL_CYTOPLASM_E_Value] # ------------------------------------------------- aRootIndex = aFullPNString.find(':/') aFileName = aFullPNString[:aRootIndex] + aFullPNString[aRootIndex + 1:] aFileName = aFileName.replace(':', '_') aFileName = aFileName.replace('/', '_') aECDDataFile = ECDDataFile() aECDDataFile.setFileName(aFileName) # ------------------------------------------------- # Gets logger # ------------------------------------------------- # need check if the logger exists aLoggerStub = self.createLoggerStub(aFullPNString) if not aLoggerStub.exists(): aErrorMessage = '\nLogger doesn\'t exist.!\n' self.message(aErrorMessage) return None aLoggerStartTime = aLoggerStub.getStartTime() aLoggerEndTime = aLoggerStub.getEndTime() if aStartTime == -1 or anEndTime == -1: # gets start time and end time from logger aStartTime = aLoggerStartTime anEndTime = aLoggerEndTime else: # checks the value if not (aLoggerStartTime < aStartTime < aLoggerEndTime): aStartTime = aLoggerStartTime if not (aLoggerStartTime < anEndTime < aLoggerEndTime): anEndTime = aLoggerEndTime # ------------------------------------------------- # gets the matrix data from logger. # ------------------------------------------------- if anInterval == -1: # gets data with specifing interval aMatrixData = aLoggerStub.getData(aStartTime, anEndTime) else: # gets data without specifing interval aMatrixData = aLoggerStub.getData(aStartTime, anEndTime, anInterval) # sets data name aECDDataFile.setDataName(aFullPNString) # sets matrix data aECDDataFile.setData(aMatrixData) # ------------------------------------------------- # adds data file to data file manager # ------------------------------------------------- aDataFileManager.getFileMap()[ ` aFileIndex `] = aECDDataFile aFileIndex = aFileIndex + 1 # for(2) try: #(1) aDataFileManager.saveAll() except: #try(1) # ------------------------------------------------- # displays error message and exit this method. # ------------------------------------------------- import traceback print __name__, aErrorMessageList = traceback.format_exception( sys.exc_type, sys.exc_value, sys.exc_traceback) for aLine in aErrorMessageList: self.message(aLine) aErrorMessage = "Error : could not save [%s] " % aFullPNString self.message(aErrorMessage) else: # try(1) # ------------------------------------------------- # displays error message and exit this method. # ------------------------------------------------- aSuccessMessage = " All files you selected are saved. " self.message(aSuccessMessage)
#run for recording simulation log of all variable in this model. VmLogger.setLoggerPolicy([1, 0.1, 1, 1000000]) run(RECORDING_TIME) #save simulation log of membrane potential in files as csv and ecd. pretimecourse = VmLogger.getData() precsvfile = open('Vm' + '_' + FullPN_GX + '_' + str(VALUE_OF_GX) + '.csv', 'w') writer = csv.writer(precsvfile) pretimecourse_csv = np.matrix(pretimecourse) pretimecourse_csv = pretimecourse_csv.tolist() name = ['time', 'Vm'] pretimecourse_csv.insert(0, ['time', 'Vm']) writer.writerows(pretimecourse_csv) pretimecourse = ECDDataFile(pretimecourse) pretimecourse.setDataName(VmLogger.getName()) pretimecourse.setNote(VmLogger.getName()) pretimecourse.save('Vm' + '_' + FullPN_GX + '_' + str(VALUE_OF_GX) + '.ecd') #save simulation log of all variable except for membrane potential in a file as csv. saveCSV(allLoggerList, FullPN_GX) # print currentnumber if currentnumber == endnumber - 1: print "end" else: print "continue"
aLoggerList.append( aLogger ) # -------------------------------------------------------- # (4) run # -------------------------------------------------------- run( DULATION + INTERVAL ) # -------------------------------------------------------- # (5) read training time-course # -------------------------------------------------------- aTrainingTimeCourseList = [] for i in range( len(TRAINING_DATA_FILE_LIST) ): aTimeCouse = ECDDataFile() aTimeCouse.load( _DATA_ + os.sep + TRAINING_DATA_FILE_LIST[i] ) aTrainingTimeCourseList.append( aTimeCouse ) # -------------------------------------------------------- # (6) save predicted time-course # -------------------------------------------------------- aPredictedTimeCouseList = [] for i in range( len(aLoggerList) ): aTimeCouse = ECDDataFile( aLoggerList[i].getData(START_TIME, \ DULATION + INTERVAL, \ INTERVAL) ) aTimeCouse.setDataName( aLoggerList[i].getName() ) aTimeCouse.setNote( 'Predicted %s' %VARIABLE_LIST_FOR_LOGGER[i] )
allLoggerList = createallLoger(tree[1]) # run for recording simulation log of all variable in this model. VmLogger.setLoggerPolicy([1, 0.1, 1, 1000000]) run(RECORDING_TIME) # save simulation log of membrane potential in files as csv and ecd. pretimecourse = VmLogger.getData() precsvfile = open("Vm" + "_" + FullPN_GX + "_" + str(VALUE_OF_GX) + ".csv", "w") writer = csv.writer(precsvfile) pretimecourse_csv = np.matrix(pretimecourse) pretimecourse_csv = pretimecourse_csv.tolist() name = ["time", "Vm"] pretimecourse_csv.insert(0, ["time", "Vm"]) writer.writerows(pretimecourse_csv) pretimecourse = ECDDataFile(pretimecourse) pretimecourse.setDataName(VmLogger.getName()) pretimecourse.setNote(VmLogger.getName()) pretimecourse.save("Vm" + "_" + FullPN_GX + "_" + str(VALUE_OF_GX) + ".ecd") # save simulation log of all variable except for membrane potential in a file as csv. saveCSV(allLoggerList, FullPN_GX) # print currentnumber if currentnumber == endnumber - 1: print "end" else: print "continue"
R2['k'] = k2_net R4['k'] = k4_net R5['k'] = k5_net R7['k'] = k1_net R8['k'] = k2_net R10['k'] = k4_net R11['k'] = k5_net if MODEL_FILE == 'model4-0.eml': pass else: try: R13 = createEntityStub('Process:/:R13') R14 = createEntityStub('Process:/:R14') R13['k'] = KI R14['k'] = KI #print KI except: # processive model doesn't have R13, 14 pass lkpp = createLoggerStub('Variable:/:Kpp:Value') lkpp.create() run(DURATION) from ecell.ECDDataFile import * message(OUTFILE) ECDDataFile(lkpp.getData()).save(OUTFILE) message(lkpp.getData())
def saveLoggerData( self, fullpn=0, aSaveDirectory='./Data', aStartTime=-1, anEndTime=-1, anInterval=-1 ): # ------------------------------------------------- # Check type. # ------------------------------------------------- aLoggerNameList = [] if type( fullpn ) == str: aLoggerNameList.append( fullpn ) elif not fullpn : aLoggerNameList = self.getLoggerList() elif type( fullpn ) == list: aLoggerNameList = fullpn elif type( fullpn ) == tuple: aLoggerNameList = fullpn else: self.message( "%s is not suitable type.\nuse string or list or tuple"%fullpn ) return # ------------------------------------------------- # Execute saving. # ------------------------------------------------- if not os.path.isdir( aSaveDirectory ): os.mkdir( aSaveDirectory ) # creates instance datafilemanager aDataFileManager = DataFileManager() # sets root directory to datafilemanager aDataFileManager.setRootDirectory( aSaveDirectory ) aFileIndex=0 # gets all list of selected property name for aFullPNString in aLoggerNameList: #(2) # ------------------------------------------------- # from [Variable:/CELL/CYTOPLASM:E:Value] # to [Variable_CELL_CYTOPLASM_E_Value] # ------------------------------------------------- aRootIndex = aFullPNString.find( ':/' ) aFileName = aFullPNString[:aRootIndex]+aFullPNString[aRootIndex+1:] aFileName = aFileName.replace( ':', '_' ) aFileName = aFileName.replace( '/', '_' ) aECDDataFile = ECDDataFile() aECDDataFile.setFileName( aFileName ) # ------------------------------------------------- # Gets logger # ------------------------------------------------- # need check if the logger exists aLoggerStub = self.createLoggerStub( aFullPNString ) if not aLoggerStub.exists(): aErrorMessage='\nLogger doesn\'t exist.!\n' self.message( aErrorMessage ) return None aLoggerStartTime= aLoggerStub.getStartTime() aLoggerEndTime= aLoggerStub.getEndTime() if aStartTime == -1 or anEndTime == -1: # gets start time and end time from logger aStartTime = aLoggerStartTime anEndTime = aLoggerEndTime else: # checks the value if not ( aLoggerStartTime < aStartTime < aLoggerEndTime ): aStartTime = aLoggerStartTime if not ( aLoggerStartTime < anEndTime < aLoggerEndTime ): anEndTime = aLoggerEndTime # ------------------------------------------------- # gets the matrix data from logger. # ------------------------------------------------- if anInterval == -1: # gets data with specifing interval aMatrixData = aLoggerStub.getData( aStartTime, anEndTime ) else: # gets data without specifing interval aMatrixData = aLoggerStub.getData( aStartTime, anEndTime, anInterval ) # sets data name aECDDataFile.setDataName(aFullPNString) # sets matrix data aECDDataFile.setData(aMatrixData) # ------------------------------------------------- # adds data file to data file manager # ------------------------------------------------- aDataFileManager.getFileMap()[`aFileIndex`] = aECDDataFile aFileIndex = aFileIndex + 1 # for(2) try: #(1) aDataFileManager.saveAll() except: #try(1) # ------------------------------------------------- # displays error message and exit this method. # ------------------------------------------------- import traceback print __name__, aErrorMessageList = traceback.format_exception(sys.exc_type,sys.exc_value,sys.exc_traceback) for aLine in aErrorMessageList: self.message( aLine ) aErrorMessage= "Error : could not save [%s] " %aFullPNString self.message( aErrorMessage ) else: # try(1) # ------------------------------------------------- # displays error message and exit this method. # ------------------------------------------------- aSuccessMessage= " All files you selected are saved. " self.message( aSuccessMessage )
# print some values message( 't= \t%s' % getCurrentTime() ) message( 'S:Value= \t%s' % S.getProperty( 'Value' ) ) message( 'S:MolarConc= \t%s' % S.getProperty( 'MolarConc' ) ) # run duration = 1000 message( '\n' ) message( 'run %s sec.\n' % duration ) run( duration ) # print results message( 't= \t%s' % getCurrentTime() ) message( 'S:Value= \t%s' % S.getProperty( 'Value' ) ) message( 'S:MolarConc= \t%s' % S.getProperty( 'MolarConc' ) ) message( '\n' ) from ecell.ECDDataFile import * message('saving S.ecd..') aDataFile = ECDDataFile( S_Logger.getData(0,2000,.5) ) aDataFile.setDataName( S_Logger.getName() ) aDataFile.setNote( '' ) aDataFile.save( 'S.ecd' ) #message('loading') #aNewFile = ECDDataFile() #aNewFile.load( 'S.ecd' ) #print aNewFile.getData()[:10]
#loadModel('model3a.eml') #loadModel('model3.eml') loadModel('model4.eml') lkpp = createLoggerStub('Variable:/:Kpp:Value') lkpp.create() #lk = createLoggerStub('Variable:/:K:Value') #lk.create() run(1200) from ecell.ECDDataFile import * #ECDDataFile(lkpp.getData()).save('Kpp_ODE_0.ecd') ECDDataFile(lkpp.getData()).save('Kpp2.ecd') #ECDDataFile(lk.getData()).save('K.ecd')
allLoggerList = createallLoger(tree[1]) run(RECORDING_TIME) saveCSV(allLoggerList,FullPN_GX) pretimecourse = VmLogger.getData() precsvfile = open('Vm' + '_' + FullPN_GX + '_' + str(VALUE_OF_GX) + '.csv','w') writer = csv.writer(precsvfile) pretimecourse_csv = np.matrix(pretimecourse) pretimecourse_csv = pretimecourse_csv.tolist() name = ['time','Vm'] pretimecourse_csv.insert(0,['time','Vm']) writer.writerows(pretimecourse_csv) pretimecourse = ECDDataFile(pretimecourse) pretimecourse.setDataName(VmLogger.getName()) pretimecourse.setNote(VmLogger.getName()) pretimecourse.save('Vm' + '_' + FullPN_GX + '_' + str(VALUE_OF_GX) + '.ecd') currentnumber += 1 print currentnumber if currentnumber == endnumber: print "end" else: print "continue" print GXList[currentnumber] print FullPN_GX
theFullPNs = dict( f = CURVE_DATA_DICT.keys(), b = PARAMETERS.keys() ) # J_r 残差のヤコビアン(m×n行列、全要素ゼロで初期化) J_r = np.zeros(( len( theFullPNs['f'] ), len( theFullPNs['b'] ))) J_r_next = copy.deepcopy( J_r ) # βの現在値のリスト p = [] for b in theFullPNs['b']: p.append( PARAMETERS[ b ] ) # トレーニングデータを格納した辞書 target_data_dict = {} for FullPN, ECDFileName in CURVE_DATA_DICT.items(): aTimeCouse = ECDDataFile() aTimeCouse.load( os.sep.join(( CURVE_DATA_DIR.rstrip( os.sep ), ECDFileName )) ) target_data_dict[ FullPN ] = getTargetDataPoints( aTimeCouse.getData(), T_START, T_END, T_INTERVAL ) """ for FullPN, tc in target_data_dict.items(): print "\n" + FullPN for dp in tc: print "{} : {}".format( dp[0], dp[1] ) """ # -------------------------------------------------------- # (3) 反復計算 # -------------------------------------------------------- beta_prev = copy.deepcopy( beta_dict )
PREFIX_OF_PREDICTED_TIMECOURSE = 'pre' # -------------------------------------------------------- # (1) load eml file # -------------------------------------------------------- setModel( EM, 'simple.em' ) # -------------------------------------------------------- # (2) read training time-course # -------------------------------------------------------- TRAINING_TIME_COURSE_DATA_DICT = {} for FullPN, ECDFileName in TRAINING_DATA_DICT.items(): aTimeCouse = ECDDataFile() aTimeCouse.load( os.sep.join(( TRAINING_DATA_DIR.rstrip( os.sep ), ECDFileName )) ) TRAINING_TIME_COURSE_DATA_DICT[ FullPN ] = dict( ECDFileName = ECDFileName, ECDData = aTimeCouse ) # -------------------------------------------------------- # (3) set parameter # -------------------------------------------------------- EntityStubDict = {} for FullPN, value in PARAMETERS.items(): PN = FullPN.split(':')[ -1 ] FullID = FullPN[ : len( FullPN ) - len( PN ) - 1 ] if FullID not in EntityStubDict: EntityStubDict[ FullID ] = createEntityStub( FullID ) EntityStubDict[ FullID ].setProperty( PN, value )
# -------------------------------------------------------- # (3) create logger stubs # -------------------------------------------------------- logger_dict = {} for a_target_FullPN in TARGET: logger_dict[a_target_FullPN] = createLoggerStub(a_target_FullPN) logger_dict[a_target_FullPN].create() # -------------------------------------------------------- # (4) run # -------------------------------------------------------- run(11.0) step(1) # -------------------------------------------------------- # (6) save predicted time-course # -------------------------------------------------------- aPredictedTimeCouseList = [] for a_FullPN, a_Logger in logger_dict.items(): a_time_course = ECDDataFile(a_Logger.getData(0, 11, 0.01)) a_time_course.setDataName(a_Logger.getName()) a_time_course.setNote('Predicted {}'.format(a_FullPN)) a_time_course.save('{}.ecd'.format(a_FullPN.split(':')[2])) # end of this file