Glavni_izdelava_IVP_modela.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. import numpy
  2. import os
  3. import json
  4. import time
  5. import cModel
  6. import importlib
  7. import runSolver
  8. import numpy.linalg
  9. #Zalaufaj metodo solve_matrics namest IVP, method seperately. Preverjas razlicne metode resevanja enega modela, pol ko bos pa naredi kopijo novega modela glede na članek kotinin
  10. importlib.reload(cModel)
  11. importlib.reload(runSolver)
  12. def getModel(solution, modelName):
  13. Q = solution[modelName]
  14. model = cModel.model()
  15. setupFile = Q['setup']
  16. modelFile = Q['model']
  17. parameterFile = Q['parameters']
  18. print('modelFile: {} {}'.format(modelFile, os.path.isfile(modelFile)))
  19. print('parameterFile: {} {}'.format(parameterFile, os.path.isfile(parameterFile)))
  20. model.parse(modelFile, parameterFile)
  21. return model
  22. def getSum(data,modelName):
  23. model=getModel(data,modelName)
  24. Q=data[modelName]['solution']
  25. lut=Q['lut']
  26. total=Q['sol'][-1,lut['total']]
  27. #print(total)
  28. fQ={}
  29. for x in lut:
  30. if x=='total':
  31. continue
  32. j=lut[x]
  33. try:
  34. v=model.get(model.mod['volumes'][x])['value']
  35. fQ[x]=Q['sol'][-1,j]*v/total
  36. except KeyError:
  37. #print('{} {}'.format(x,x in model.mod['scaled']))
  38. fQ[x]=Q['sol'][-1,j]
  39. print(sum(list(fQ.values())))
  40. print(sorted(fQ.items(), key=lambda item:item[1]))
  41. def mergeSolutions(seq):
  42. out={}
  43. #each element in sequence should have this components
  44. for v in ['t','sol','se','qt','sOut']:
  45. out[v]=numpy.concatenate([x[v] for x in seq])
  46. #copy last for settings
  47. for v in ['lut','lutSE','setup','model','parameters','qt','sOut']:
  48. out[v]=seq[-1][v]
  49. return out
  50. def updateSetup(job):
  51. setupFileSrc = job['setupFile']
  52. setupFile = os.path.join(job['jobDir'], 'setupInput.json')
  53. with open(setupFileSrc, 'r') as f:
  54. setup = json.load(f)
  55. try:
  56. setup.update(job['setupUpdates'])
  57. except KeyError:
  58. pass
  59. with open(setupFile, 'w+') as f:
  60. f.write(json.dumps(setup))
  61. return setupFile
  62. fh = os.path.expanduser('~')
  63. # Create job configurations
  64. job_configs = {
  65. 'cDiazepam': {
  66. 'jobDir': os.path.join(fh, 'Documents', 'Sola', 'IJS', 'PBPK_public', 'cDiazepam_IVP'),
  67. 'modelFile': os.path.join(fh, 'Documents', 'Sola', 'IJS', 'PBPK_public', 'models', 'cDiazepam.json'),
  68. 'parameterFile': os.path.join(fh, 'Documents', 'Sola', 'IJS', 'PBPK_public', 'models', 'cDiazepam_parameters2.json'),
  69. 'setupFile': os.path.join(fh, 'Documents', 'Sola', 'IJS', 'PBPK_public', 'setup', 'setupMinute.json'),
  70. 'srcDir': 'NONE',
  71. 'doCalculate': True
  72. },
  73. 'cDiazepam1': {
  74. 'jobDir': os.path.join(fh, 'Documents', 'Sola', 'IJS', 'PBPK_public', 'cDiazepam1_IVP'),
  75. 'modelFile': os.path.join(fh, 'Documents', 'Sola', 'IJS', 'PBPK_public', 'models', 'cDiazepam.json'),
  76. 'parameterFile': os.path.join(fh, 'Documents', 'Sola', 'IJS', 'PBPK_public', 'models', 'cDiazepam_parameters2.json'),
  77. 'setupFile': os.path.join(fh, 'Documents', 'Sola', 'IJS', 'PBPK_public', 'setup', 'setupHours.json'),
  78. 'srcDir': 'NONE',
  79. 'doCalculate': True
  80. },
  81. 'cDiazepamF': {
  82. 'jobDir': os.path.join(fh, 'Documents', 'Sola', 'IJS', 'PBPK_public', 'cDiazepamF_IVP'),
  83. 'modelFile': os.path.join(fh, 'Documents', 'Sola', 'IJS', 'PBPK_public', 'models', 'cDiazepam.json'),
  84. 'parameterFile': os.path.join(fh, 'Documents', 'Sola', 'IJS', 'PBPK_public', 'models', 'cDiazepam_parameters1.json'),
  85. 'setupFile': os.path.join(fh, 'Documents', 'Sola', 'IJS', 'PBPK_public', 'setup', 'setupMinute.json'),
  86. 'srcDir': 'NONE',
  87. 'doCalculate': True
  88. },
  89. 'cDiazepamB': {
  90. 'jobDir': os.path.join(fh, 'Documents', 'Sola', 'IJS', 'PBPK_public', 'cDiazepamB_IVP'),
  91. 'modelFile': os.path.join(fh, 'Documents', 'Sola', 'IJS', 'PBPK_public', 'models', 'cDiazepam.json'),
  92. 'parameterFile': os.path.join(fh, 'Documents', 'Sola', 'IJS', 'PBPK_public', 'models', 'cDiazepam_parameters1.json'),
  93. 'setupFile': os.path.join(fh, 'Documents', 'Sola', 'IJS', 'PBPK_public', 'setup', 'setupHours.json'),
  94. 'srcDir': 'NONE',
  95. 'doCalculate': True
  96. }
  97. }
  98. # Execute jobs
  99. for job_name, job in job_configs.items():
  100. if not job['doCalculate']:
  101. continue
  102. if not os.path.isdir(job['jobDir']):
  103. os.mkdir(job['jobDir'])
  104. # Adjust local setup file with ad-hoc modifications
  105. setupFile = updateSetup(job)
  106. # t0,y0,S1,lut,lutSE=runSolver.getStartPoint(setupFile) #Pridobivanje začetnih točk
  107. runSolver.main([setupFile, job['modelFile'], job['parameterFile']], job['jobDir'], job['srcDir'])
  108. #(t0,y0,S1,lut,lutSE=runSolver.getStartPoint(setup) ) #Pridobivanje začetnih točk
  109. #Zalaufaj metodo solve_matrics namest IVP, method seperately. Preverjas razlicne metode resevanja enega modela, pol ko bos pa naredi kopijo novega modela glede na članek kotinin