Glavni_izdelava_Matrix_modela.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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_Matrix'),
  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. 'setupUpdates': {'mode': 'solveMatrix', 'method': 'separately'}, #Mode:IVP mode:LSOD
  71. 'srcDir': 'NONE',
  72. 'doCalculate': True
  73. },
  74. 'cDiazepam1': {
  75. 'jobDir': os.path.join(fh, 'Documents', 'Sola', 'IJS', 'PBPK_public', 'cDiazepam1_Matrix'),
  76. 'modelFile': os.path.join(fh, 'Documents', 'Sola', 'IJS', 'PBPK_public', 'models', 'cDiazepam.json'),
  77. 'parameterFile': os.path.join(fh, 'Documents', 'Sola', 'IJS', 'PBPK_public', 'models', 'cDiazepam_parameters2.json'),
  78. 'setupFile': os.path.join(fh, 'Documents', 'Sola', 'IJS', 'PBPK_public', 'setup', 'setupHours.json'),
  79. 'setupUpdates': {'mode': 'solveMatrix', 'method': 'separately'}, #Mode:IVP mode:LSOD
  80. 'srcDir': 'NONE',
  81. 'doCalculate': True
  82. },
  83. 'cDiazepamF': {
  84. 'jobDir': os.path.join(fh, 'Documents', 'Sola', 'IJS', 'PBPK_public', 'cDiazepamF_Matrix'),
  85. 'modelFile': os.path.join(fh, 'Documents', 'Sola', 'IJS', 'PBPK_public', 'models', 'cDiazepam.json'),
  86. 'parameterFile': os.path.join(fh, 'Documents', 'Sola', 'IJS', 'PBPK_public', 'models', 'cDiazepam_parameters1.json'),
  87. 'setupFile': os.path.join(fh, 'Documents', 'Sola', 'IJS', 'PBPK_public', 'setup', 'setupMinute.json'),
  88. 'setupUpdates': {'mode': 'solveMatrix', 'method': 'separately'},
  89. 'srcDir': 'NONE',
  90. 'doCalculate': True
  91. },
  92. 'cDiazepamB': {
  93. 'jobDir': os.path.join(fh, 'Documents', 'Sola', 'IJS', 'PBPK_public', 'cDiazepamB_Matrix'),
  94. 'modelFile': os.path.join(fh, 'Documents', 'Sola', 'IJS', 'PBPK_public', 'models', 'cDiazepam.json'),
  95. 'parameterFile': os.path.join(fh, 'Documents', 'Sola', 'IJS', 'PBPK_public', 'models', 'cDiazepam_parameters1.json'),
  96. 'setupFile': os.path.join(fh, 'Documents', 'Sola', 'IJS', 'PBPK_public', 'setup', 'setupHours.json'),
  97. 'setupUpdates': {'mode': 'solveMatrix', 'method': 'separately'},
  98. 'srcDir': 'NONE',
  99. 'doCalculate': True
  100. }
  101. }
  102. # Execute jobs
  103. for job_name, job in job_configs.items():
  104. if not job['doCalculate']:
  105. continue
  106. if not os.path.isdir(job['jobDir']):
  107. os.mkdir(job['jobDir'])
  108. # Adjust local setup file with ad-hoc modifications
  109. setupFile = updateSetup(job)
  110. # t0,y0,S1,lut,lutSE=runSolver.getStartPoint(setupFile) #Pridobivanje začetnih točk
  111. runSolver.main([setupFile, job['modelFile'], job['parameterFile']], job['jobDir'], job['srcDir'])
  112. #(t0,y0,S1,lut,lutSE=runSolver.getStartPoint(setup) ) #Pridobivanje začetnih točk
  113. #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