コード例 #1
0
def test3():
  simulation = runner.Runner(),
  yamlVarFile = "inputParams.yaml",
  testState = "Cai",         
  # parameters to vary 
  variedParamDict = {
    "kon":  [0.5,0.2],         
    "koff":  [5.0,0.2],         
    },

  # get trial simulation results 
  returnData = []
  for i in range(2):
    varDict={
            'kon':0.4+i*0.1,'koff':4.4}

    returnDict=simulate(
      varDict=varDict,        # dictionary of parameters to be used for simulation
 #   returnDict=dict(),    # dictionary output to be returned by simulation
      jobDuration = 25e3   # [ms]
    )

    cai = analyze.GetData('Cai',returnDict['data'])
    returnData.append( cai )
    

  if 1:
    raise RuntimeError("DSF")
    # once above works, do
    traces = cellDetect()
    for trace in traces:
        outputObj=outPutList["Cai"]
        outputObj.vals = cai
        outputObj.ts   = ts   

    results.cellNum=1
    allResults.append(results)
    print("make plots with cell number, legend") 

  timeRange = [0, 2] # where to measure 
  vals = cai
  outputList= { 
    #"Cai":OutputObj("Cai","val_vs_time",[  0, 2],
    #[1,0.5,0.15],timeInterpolations=[  0,1,2]) # check that interpolated values at 0, 100, 200 are 1, 0.5 ...
    "Cai":OutputObj("Cai","val_vs_time",timeRange,
    vals,timeInterpolations=ts) # check that interpolated values at 0, 100, 200 are 1, 0.5 ...
    }

  results = run(
    simulation,
    yamlVarFile = yamlVarFile,            
    variedParamDict = variedParamDict,
    jobDuration = 30e3, # [ms]
    numRandomDraws = 8,  
    numIters = 5,    
    #sigmaScaleRate = 0.45,
    outputList = outputList,
    debug = True
)
コード例 #2
0
def DisplayFit(simulation,
               odeModel=None,
               jobDuration=30e3,
               tsteps=None,
               fixedParamDict=None,
               results=None,
               output_dir="."):
    print("Running demo with new parameters for comparison against truth")

    # run job with best parameters
    outputList = results['outputList']
    varDict = results[
        'bestFitDict']  # {variedParamKey: results['bestFitParam']}
    jobDict = {
        'simulation': simulation,
        'odeModel': odeModel,
        'varDict': varDict,
        'fixedParamDict': fixedParamDict,
        'jobNum': 0,
        'jobDuration': jobDuration,
        'tsteps': tsteps,
        'outputList': results['outputList']
    }
    dummy, workerResults = workerParams(jobDict,
                                        skipProcess=True,
                                        verbose=True)

    # cludgy way of plotting result
    for key in results['outputList'].keys():
        1
    #key = outputList.keys()[0]
    obj = outputList[key]
    testStateName = obj.odeKeyName
    data = workerResults.outputResults
    dataSub = analyze.GetData(data, testStateName)

    plt.figure()
    ts = dataSub.t
    plt.plot(ts, dataSub.valsIdx, label="pred")
    #print("SDF",obj.truthValue)
    #print(obj.timeInterpolations)
    #print( isinstance( None, np.ndarray ) ) # obj.timeInterpolations,np.ndarray))
    #if isinstance( obj.timeInterpolations,np.ndarray):
    #print(np.size(obj.timeInterpolations))

    if np.size(obj.timeInterpolations) > 1:
        plt.scatter(obj.timeInterpolations, obj.truthValue, label="truth")
    else:
        plt.plot([np.min(ts), np.max(ts)], [obj.truthValue, obj.truthValue],
                 'r--',
                 label="truth")

    plt.title(testStateName)
    plt.legend(loc=0)
    file_path = os.path.join(output_dir, testStateName + ".png")
    plt.gcf().savefig(file_path)

    return data
コード例 #3
0
def ProcessWorkerOutputs(data, outputList, tag=99):
    """Given data dictionary, pulls out subsection of data

  Data subset is evaluated based on 'obj.mode', which defines the type of analysis done.
  See OutputObj class definition and ProcessDataArray function

  Parameters
  ----------
  data : dict
      The output data from the simulation. Of the structure:
          data = {
              "t": <array of time points>
              <for each measurable>
              "<measurable name>": <measurable values at time points>
          }
  outputList : dict
      Dictionary holding the name(s) of the measurable(s) as keys and `OutputObj`(s) as the values.
  tag : int, optional
      The job number for this job, by default 99. NOT CURRENTLY USED.

  Returns
  -------
  dict
      The new, processed, outputResults, where everything except the result is copied from given 
      `outputList`. `outputList[key].result` is the newly processed results.
  """
    outputResults = {}
    for key, obj in outputList.items():
        dataSub = analyze.GetData(data, obj.odeKeyName)

        result = analyze.ProcessDataArray(dataSub,
                                          obj.mode,
                                          obj.timeRange,
                                          obj.timeInterpolations,
                                          key=key)

        resultObj = copy.copy(obj)
        resultObj.result = result
        outputResults[key] = resultObj

    return outputResults
コード例 #4
0
def ProcessWorkerOutputs(data,outputList, tag=99):
  outputResults = {}
  ###CMTprint "outputList: ", outputList
  for key,obj in outputList.items():
    ###CMTprint "key: ", key, "obj: ", obj
    ###CMTprint "outputList: ", outputList
    ###CMTprint "in the for loop"
    ###CMTprint "obj.timeRange: ", obj.timeRange
    dataSub = analyze.GetData(data, obj.name)
    #print('dataSub', dataSub)

    ###CMTprint "dataSub: ", dataSub
    ###CMTprint "dataSub.valsIdx: ", dataSub.valsIdx
    ###CMTprint "damax",np.max(dataSub.t)
    result = analyze.ProcessDataArray(dataSub,obj.mode,obj.timeRange,obj.timeInterpolations,key=key)

    #output.result = result
    resultObj = copy.copy(obj)
    resultObj.result = result
    #outputResults.append( resultObj )
    outputResults[key]=resultObj

  return outputResults
コード例 #5
0
def FitData(
        recordedData,
        jobDuration=1e3, # duration of simulation
        simulation=None,      
        yamlFile = None,
        variedParamDict = None,
        outputList = None,
        testState = None, # if not none, it contains the data key you want to print (like 'Cai') 
        numRandomDraws = 2,
        numIters = 2
        ): 

  if simulation is None:
      import runner 
      simulation = runner.Runner()

  # parameters to vary/initial guesses
  if variedParamDict is None:
      variedParamDict = {
          "kon":  [0.5,0.2],         
          "koff":  [1.5,0.2],         
      }

  if outputList is None:
      timeRange = [0,9] # in [s]
      # what we are fitting to 
      outputList= { 
      #"Cai":OutputObj("Cai","val_vs_time",[  0, 2],
      #[1,0.5,0.15],timeInterpolations=[  0,1,2]) # check that interpolated values at 0, 100, 200 are 1, 0.5 ...
          "Cai":OutputObj("Cai","val_vs_time",timeRange,
                          recordedDatai['Cai'],
                          timeInterpolations=recordedDatai['t']) # check that interpolated values at 0, 100, 200 are 1, 0.5 ...
      }        

  import analyze
  #cellNum = 0
  #recordedDatai = recordedData[ cellNum ]
  #numRandomDraws = 10
  #numIters = 5
  
  numCells = np.shape( recordedData )[0]
  for cellNum in range(numCells):
      recordedDatai = recordedData[ cellNum ]
      
      # actual fitting process 
      #simulation = runner.Runner()
      results = fA.run(
          simulation,
          yamlVarFile = yamlFile,                 
          variedParamDict = variedParamDict,
          jobDuration = jobDuration, # [ms]
          numRandomDraws = numRandomDraws,  
          numIters = numIters,    
          #sigmaScaleRate = 0.45,
          outputList = outputList,
          debug = False,
          verboseLevel=1
      )
      
      # store results from fitting 
      recordedDatai['bestFitDict']  = results['bestFitDict']
      fits = results['bestFitDict']
      
      #print(fits)  
      if testState is not None:
        plt.figure()
        data = analyze.GetData(results['data'],testState)
        #ts = analyze.GetData(results['data'],'t')
        plt.plot(data.t,data.valsIdx,label="fit"+str(fits))#str(fits))
        ts = recordedDatai['t']*1e-3
        datai = recordedDatai[testState] 
        plt.plot(ts,datai,label="input")
        title=testState+"%d"%cellNum
        plt.title(title)
        plt.legend()
        plt.gcf().savefig(title+".png")