title: Pass variable to seperate file and then have it be "global"
body: <p>I noticed that the following questions were related: <a href="https://stackoverflow.com/questions/34136737/using-scipy-curve-fit-for-a-variable-number-of-parameters%22this%22">Variable Parameters Curve Fit 0</a>, <a href="https://stackoverflow.com/questions/34136737/using-scipy-curve-fit-for-a-variable-number-of-parameters%22Variable%20Parameter%20Curve%20Fit%201%22">Variable Parameters Curve Fit 1</a>, <a href="https://stackoverflow.com/questions/31705327/scipy-optimize-curve-fit-setting-a-fixed-parameter%22Hold%20Parameter%20Curve%20Fit%22">Hold Parameter Curve Fit 0.</a></p>
  
  <p>It creates an array of guess parameters and array stipulating which parameters to hold constant. It then generate the correct name to call and then use a wrapper to call it. It works but I want to put my definitions in a separate file called utilites.py and then call the definitions from the initial file. As you see wrapper() needs to find wrapperName(). Is there some way to pass wrapper name to a separate file and then make it accessible by all the definitions in utilities.py? If you try to pass wrapper an extra parameter curve_fit() breaks.</p>
  
  <pre><code>import numpy as np
  from numpy import random
  import scipy as sp
  from scipy.optimize import curve_fit
  import matplotlib.pyplot as pl
  
  def exp1(t,a1,tau1):
          val=0.
          val=(a1*np.exp(-t/tau1))*np.heaviside(t,0)
          return val
  
  def exp2(t,a1,tau1,a2,tau2):
          val=0.
          val=(a1*np.exp(-t/tau1)+a2*np.exp(-t/tau2))*np.heaviside(t,0)
          return val
  
  def exp3(t,a1,tau1,a2,tau2,a3,tau3):
          val=0.
          val=(a1*np.exp(-t/tau1)+a2*np.exp(-t/tau2)+a3*np.exp(-t/tau3))*np.heaviside(t,0)
          return val
  
  def exp4(t,a1,tau1,a2,tau2,a3,tau3,a4,tau4):
          val=0.
          val=(a1*np.exp(-t/tau1)+a2*np.exp(-t/tau2)+a3*np.exp(-t/tau3)+a3*np.exp(-t/tau3))*np.heaviside(t,0)
          return val
  
  def multiexp(t,args):
          val=np.zeros(len(t))
          for i in np.arange(0,len(args),2):
              val+=args[i]*np.exp(-t/args[i+1])*np.heaviside(t,0)
          return val
  
  def genWrapperName(hold,p0):
      counter=0
      if len(p0)==2:
          wrapperName='exp1(t,'
      elif len(p0)==4:
          wrapperName='exp2(t,'
      elif len(p0)==6:
          wrapperName='exp3(t,'
      elif len(p0)==8:
          wrapperName='exp4(t,'
      for i in range(0, len(hold)):
          if hold[i]:
              wrapperName+=str(p0[i])
          else:
              if i%2==0:
                  wrapperName+='args['+str(counter)+']'
                  counter+=1
              else:
                  wrapperName+='args['+str(counter)+']'
                  counter+=1
          if i&lt;len(hold)-1:
              wrapperName+=','
      wrapperName+=')'
      return wrapperName
  
  def wrapper(t,*args):
      global wrapperName
      return eval(wrapperName)
  
  #inital guess parameters
  p0=np.array([1.5,200.,2.,700.,3.,2400.])
  #list of parameters to hold in the least squares fit. 1 means hold
  hold=[0,1,0,1,0,0]
  holdloc=[i for i, x in enumerate(hold) if x]
  p1=np.delete(p0,holdloc)
  timepoints = np.arange(20.,4000.,20.)
  wrapperName=genWrapperName(hold,p0)
  y=exp3(timepoints,1.,300.,2.,800,1.,3000.)+np.random.normal(0, .1, size=len(timepoints))
  
  popt, pcov = curve_fit(exp3, timepoints, y, p0=p0)
  print 'unheld parameters:', popt
  print 'unheld covariance:', pcov
  
  popt, pcov = curve_fit(wrapper, timepoints, y, p0=p1)
  
  for i in range(0, len(hold)):
      if hold[i]:
          popt=np.insert(popt,i,p0[i])
  print 'held parameters:', popt
  print 'held covariance:', pcov
  yfit=multiexp(timepoints,popt)
  
  pl.plot(timepoints,y,timepoints,yfit)
  pl.show()
  </code></pre>