Browse Source

Adding scaling to input infrastructure

Andrej 2 years ago
parent
commit
47b95ecf9f

+ 2 - 1
models/humanHG.json

@@ -5,6 +5,8 @@
       "richlyPerfused","fat","slowlyPerfused","hair",
       "brainBlood","brain",
       "liver","gut","intestine","feces","inorganicMercury"],
+   "commentScaled":"add compartments that only have inputs so they can be scaled to total input",
+   "scaled":["hair","inorganicMercury","feces"],
    "commentParameters":"every parameter must have a value, name is optional and used in SE computation",
    "volumeComment":"in a compartment w/o volume, value is mass",
    "volumes":{
@@ -18,7 +20,6 @@
       "gut":"gutVolume",
       "intestine":"intestineVolume",
       "redBloodCells":"redBloodCellsVolume",
-      "hair":"hairVolume",
       "brain":"brainVolume",
       "liver":"liverVolume"
       },

+ 0 - 1
models/humanHG_parameters1.json

@@ -41,7 +41,6 @@
         "kidneyVolume":{"derived":"kidneyVolume"},
         "richlyPerfusedVolume":{"derived":"richlyPerfusedVolume"},
         "fatVolume":{"derived":"fatVolume"},
-        "hairVolume":{"value":1},
         "slowlyPerfusedVolume":{"derived":"slowlyPerfusedVolume"},
         "brainBloodVolume":{"derived":"brainBloodVolume"},
         "brainVolume":{"derived":"brainVolume"},

+ 6 - 6
models/humanHGplusI.json

@@ -5,7 +5,9 @@
       "richlyPerfused","fat","slowlyPerfused","hair",
       "brainBlood","brain",
       "liver","gut","intestine","feces",
-      "plasmaI","liverI","kidneyI","hairI","brainI"],
+      "plasmaI","liverI","kidneyI","hairI","brainI","fecesI"],
+   "commentScaled":"add compartments that only have inputs so they can be scaled to total input",
+   "scaled":["hair","hairI","feces","fecesI","urine"],
    "commentParameters":"every parameter must have a value, name is optional and used in SE computation",
    "volumeComment":"in a compartment w/o volume, value is mass",
    "volumes":{
@@ -19,13 +21,11 @@
       "gut":"gutVolume",
       "intestine":"intestineVolume",
       "redBloodCells":"redBloodCellsVolume",
-      "hair":"hairVolume",
       "brain":"brainVolume",
       "liver":"liverVolume",
       "plasmaI":"plasmaVolume",
       "liverI":"liverVolume",
       "kidneyI":"kidneyVolume",
-      "hairI":"hairVolume",
       "brainI":"brainVolume"
       },
    "flows":{
@@ -59,9 +59,10 @@
          "brain->venous":"kBR",
          "liver->intestine":"kB",
          "intestine->gut":"kR",
+         "intestine->feces":"kF",
          "plasma->liverI":"dBL",
          "liverI->plasmaI":"kLB:I",
-         "liverI->feces":"kLF:I",
+         "liverI->fecesI":"kLF:I",
          "plasmaI->kidneyI":"kBK:I",
          "kidneyI->plasmaI":"kKB:I",
          "kidneyI->urine":"kKU:I",
@@ -69,7 +70,7 @@
          "brainI->plasmaI":"kBrB:I",
          "plasmaI->urine":"kBU:I",
          "plasmaI->brainI":"kBBr:I",
-         "plasmaI->feces":"kBF:I",
+         "plasmaI->fecesI":"kBF:I",
          "plasma->brainI":"dBBr"}},
     "pcComments":"always related to outflow of organs", 
     "partitionCoefficients":{
@@ -81,6 +82,5 @@
        "liver":"liverPC",
        "gut":"gutPC",
        "brain->venous":"brainPC",
-       "hair->venous":"hairPC",
        "redBloodCells->plasma":"rbcPC"}
 }

+ 0 - 8
models/humanHGplusI_parameters.json

@@ -6,8 +6,6 @@
         "brainPC":{"value":3,"dist":"lognormal","cv":0.3},
         "fatPC":{"value":0.15,"dist":"lognormal","cv":0.3},
         "gutPC":{"value":1,"dist":"lognormal","cv":0.7},
-        "hairPC":{"derived":"hairPC"},
-        "hairPCBlood":{"value":248.7,"dist":"lognormal","cv":0.7},
         "kidneyPC":{"value":4.0,"dist":"lognormal","cv":0.3},
         "liverPC":{"value":5.0,"dist":"lognormal","cv":0.3},
         "rbcPC":{"value":12.0,"dist":"lognormal","cv":0.3},
@@ -43,8 +41,6 @@
         "kidneyVolume":{"derived":"kidneyVolume"},
         "richlyPerfusedVolume":{"derived":"richlyPerfusedVolume"},
         "fatVolume":{"derived":"fatVolume"},
-        "hairVolume":{"value":1},
-        "fecesVolume":{"value":1},
         "slowlyPerfusedVolume":{"derived":"slowlyPerfusedVolume"},
         "brainBloodVolume":{"derived":"brainBloodVolume"},
         "brainVolume":{"derived":"brainVolume"},
@@ -203,10 +199,6 @@
            "type":"product",
            "a":"intestineVolumeFraction",
            "b":"bodyWeight"},
-       "hairPC":{
-          "type":"ratio",
-          "a":"hairPCBlood",
-          "b":"slowlyPerfusedPC"},
        "kB":{
           "type":"product",
           "a":"kBScaled",

+ 61 - 4
pythonScripts/cModel.py

@@ -12,6 +12,7 @@ class model:
    def __init__(self):
       self.compartments={}
       self.seJ={}
+      self.scaled=[]
         
    def add_source(self,compartmentName,formula):
       self.compartments[compartmentName]['source']=formula
@@ -233,7 +234,8 @@ class model:
          pcParName=self.mod['partitionCoefficients'][pc]
          pcPar=pars[pcParName]
          print('\t{}:{} [{}]'.format(pc,pcParName,self.get(pcParName)))
-
+   
+   def inspectSE(self):
 
       print('SE parameters')
       for p in self.seJ:
@@ -253,8 +255,12 @@ class model:
       with open(parameterFile,'r') as f:
          self.parSetup=json.load(f)
 
+      self.mod['compartments'].append('total')
       for m in self.mod['compartments']:
          self.add_compartment(m)
+      
+      for m in self.mod['scaled']:
+         self.scaled.append(m)
 
       self.add_default_parameters()
       #standard parameters such as one,zero etc.
@@ -322,15 +328,44 @@ class model:
       pars['zero']={'value':0}
    
 
-   def M(self,t):
+   def M(self,t,y=numpy.array([])):
       for i in self.dM:
          for j in self.dM[i]:
             self.fM[i,j]=self.dM[i][j](t)
       #create an array and fill it with outputs of function at t
+      if (y.size==0):
+         return self.fM
+      self.set_scaledM(t,y)
       return self.fM
 
+   def set_scaledM(self,t,y):
+      #prevent zero division
+      eps=1e-8
+      for c in self.scaled:
+         i=self.lut[c]
+         it=self.lut['total']
+         try:
+            k=numpy.copy(self.originalK[i])
+         except AttributeError:
+            k=numpy.copy(self.fM[i,:])
+            self.originalK={}
+            self.originalK[i]=k
+            #make another copy
+            k=numpy.copy(self.originalK[i])
+         except KeyError:
+            k=numpy.copy(self.fM[i,:])
+            self.originalK[i]=k
+            #make another copy
+            k=numpy.copy(self.originalK[i])
+
+         k[i]=k[i]-self.u(t)[it]
+         #scale all inputs by total input mass
+         for j in range(self.n):
+            self.fM[i,j]=k[j]/(y[it]+eps)
+
    def u(self,t):
       ub=[f(t) for f in self.fu]
+      ub[self.lut['total']]=sum(ub)
       return numpy.array(ub)
 
    def jacobiFull(self,t):
@@ -344,14 +379,36 @@ class model:
 
 
 
-   def fSS(self,t):
+   def fSS(self,t,y=numpy.array([])):
       for k in self.qSS:
          for i in self.qSS[k]:
             for j in self.qSS[k][i]:
                #print('[{},{},{}] {}'.format(k,i,j,self.qSS[k][i][j]))
                self.SS[k,i,j]=(self.qSS[k][i][j])(t)
+      if y.size==0:
+         return self.SS
+      self.set_scaledSS(t,y)
       return self.SS
  
+   def set_scaledSS(self,t,y):
+      #prevent zero division
+      eps=1e-8
+      for c in self.scaled:
+         it=self.lut['total']
+         i=self.lut[c]
+         try:
+            dkdp=numpy.copy(self.originalSS[i])
+         except AttributeError:
+            dkdp=numpy.copy(self.SS[:,i,:])
+            self.originalSS={}
+            self.originalSS[i]=dkdp
+            dkdp=numpy.copy(self.originalSS[i])
+         except KeyError:
+            dkdp=numpy.copy(self.SS[:,i,:])
+            self.originalSS[i]=dkdp
+            dkdp=numpy.copy(self.originalSS[i])
+         self.SS[:,i,:]=dkdp/(y[it]+eps)
+      #should add error on u!
                      
    def fSY(self,y,t):
       #M number of sensitivity parameters
@@ -360,7 +417,7 @@ class model:
 
       #assume a tabulated solution y(t) at t spaced intervals
 
-      qS=self.fSS(t).dot(y)
+      qS=self.fSS(t,y).dot(y)
       #qS is MxN
       #but NxM is expected, so do a transpose
 

File diff suppressed because it is too large
+ 6 - 2
pythonScripts/compartmentModel.ipynb


+ 1 - 1
pythonScripts/ivp.py

@@ -48,7 +48,7 @@ def dfdySFull(t,S,system):
     mS=numpy.reshape(S,(system.n,system.m+1))
     #system.fS(y,t) is NxM matrix where M are parameters
     y=mS[:,0]
-    mOut=system.M(t).dot(mS)+system.fSY(y,t)
+    mOut=system.M(t,y).dot(mS)+system.fSY(y,t)
     try:
       system.iPrint+=1
     except AttributeError:

+ 5 - 1
pythonScripts/runSolver.py

@@ -268,7 +268,11 @@ def loadSolutionFromRef(setup, loadAll=False):
    #parFileString=ds['rows'][0]['parameterFileString']
    #parFiles=parFileString.split(';')
    remoteDir=fb.formatPathURL(setup['project'],'/'.join(['jobs',ref]))
-   localDir=os.path.join(os.path.expanduser('~'),'temp','jobDir')
+
+   try:
+      localDir=setup['localDir']
+   except KeyError:
+      localDir=os.path.join(os.path.expanduser('~'),'temp','jobDir')
    if not os.path.isdir(localDir):
       os.mkdir(localDir)
 

Some files were not shown because too many files changed in this diff