Пример #1
0
def loaddat (fname):
  try:
    if fname.endswith('.txt'):
      dat = np.loadtxt(fname)
      print('Loaded data in ' + fname + '. Extracting Spectrograms.')
      return dat
    elif fname.endswith('.param'):
      ntrial = paramrw.quickgetprm(paramf,'N_trials',int)
      basedir = os.path.join(dconf['datdir'],paramf.split(os.path.sep)[-1].split('.param')[0])
      #simdat.updatedat(paramf)
      #return paramf,simdat.ddat
      if ntrial > 1:
        ddat = readdpltrials(basedir,quickgetprm(paramf,'N_trials',int))
        #print('read dpl trials',ddat[0].shape)
        dout = np.zeros((ddat[0].shape[0],1+ntrial))
        #print('set dout shape',dout.shape)
        dout[:,0] = ddat[0][:,0]
        for i in range(ntrial):
          dout[:,i+1] = ddat[i][:,1]
        return dout
      else:
        ddat = np.loadtxt(os.path.join(basedir,'dpl.txt'))
        #print('ddat.shape:',ddat.shape)
        dout = np.zeros((ddat.shape[0],2))
        #print('dout.shape:',dout.shape)
        dout[:,0] = ddat[:,0]
        dout[:,1] = ddat[:,1]
        return dout
  except:
    print('Could not load data in ' + fname)
    return None
  return None
Пример #2
0
 def getNPyr(self):
     try:
         x = quickgetprm(self.paramf, 'N_pyr_x', int)
         y = quickgetprm(self.paramf, 'N_pyr_y', int)
         if type(x) == int and type(y) == int:
             return int(x * y * 2)
     except:
         return 0
Пример #3
0
 def getNPyr (self):
   # get the number of pyramidal neurons used in the simulation
   try:
     x = quickgetprm(self.paramf,'N_pyr_x',int)
     y = quickgetprm(self.paramf,'N_pyr_y',int)
     if type(x)==int and type(y)==int:
       return int(x * y * 2)
   except:
     return 0
Пример #4
0
 def getEVInputTimes (self):
   nprox, ndist = countEvokedInputs(self.paramf)
   ltprox, ltdist = [], []
   try:
     for i in range(nprox): ltprox.append(quickgetprm(self.paramf,'t_evprox_' + str(i+1), float))
     for i in range(ndist): ltdist.append(quickgetprm(self.paramf,'t_evdist_' + str(i+1), float))
   except:
     print('except in getEVInputTimes')
   return ltprox, ltdist
Пример #5
0
 def getEVInputTimes (self):
   # get the evoked input times
   nprox, ndist = countEvokedInputs(self.paramf)
   ltprox, ltdist = [], []
   for i in range(nprox):
     input_mu = quickgetprm(self.paramf,'t_evprox_' + str(i+1), float)
     input_sigma = quickgetprm(self.paramf,'sigma_t_evprox_' + str(i+1), float)
     ltprox.append((input_mu, input_sigma))
   for i in range(ndist):
     input_mu = quickgetprm(self.paramf,'t_evdist_' + str(i+1), float)
     input_sigma = quickgetprm(self.paramf,'sigma_t_evdist_' + str(i+1), float)
     ltdist.append((input_mu, input_sigma))
   return ltprox, ltdist
Пример #6
0
 def getNTrials(self):
     N_trials = 1
     try:
         xx = quickgetprm(self.paramf, 'N_trials', int)
         if type(xx) == int: N_trials = xx
     except:
         pass
     return N_trials
Пример #7
0
def getscalefctr(paramf):
    try:
        xx = quickgetprm(paramf, 'dipole_scalefctr', float)
        if type(xx) == float: return xx
    except:
        pass
    if 'dipole_scalefctr' in dconf:
        return dconf['dipole_scalefctr']
    return 30e3
Пример #8
0
def getscalefctr (paramf):
  # get dipole scaling factor parameter value from paramf file
  try:
    xx = quickgetprm(paramf,'dipole_scalefctr',float)
    if type(xx) == float: return xx
  except:
    pass
  if 'dipole_scalefctr' in dconf:
    return dconf['dipole_scalefctr']
  return 30e3
Пример #9
0
  def getInputDistrib (self):
    import scipy.stats as stats

    dinput = {'evprox': [], 'evdist': [], 'prox': [], 'dist': [], 'pois': []}
    try:
      sim_tstop = quickgetprm(self.paramf,'tstop',float)
      sim_dt = quickgetprm(self.paramf,'dt',float)
    except FileNotFoundError:
      return dinput

    num_step = ceil(sim_tstop / sim_dt) + 1
    times = np.linspace(0, sim_tstop, num_step)
    ltprox, ltdist = self.getEVInputTimes()
    for prox in ltprox:
      pdf = stats.norm.pdf(times, prox[0], prox[1])
      dinput['evprox'].append((times,pdf))
    for dist in ltdist:
      pdf = stats.norm.pdf(times, dist[0], dist[1])
      dinput['evdist'].append((times,pdf))
    return dinput
Пример #10
0
def updatedat (paramf):
  # update data dictionary (ddat) from the param file
  if debug: print('paramf:',paramf)
  try:
    getinputfiles(paramf)
    for k in ['dpl','spk']:
      if not os.path.isfile(dfile[k]): return False
    ddat['dpl'] = np.loadtxt(dfile['dpl'])
    if os.path.isfile(dfile['spec']): ddat['spec'] = np.load(dfile['spec'])
    else: ddat['spec'] = None
    ddat['spk'] = np.loadtxt(dfile['spk'])
    ddat['dpltrials'] = readdpltrials(basedir,quickgetprm(paramf,'N_trials',int))
    return True
  except:
    return False
Пример #11
0
    def loadDisplayData(self, fname=None):
        if fname is None or fname is False:
            fname = QFileDialog.getOpenFileName(self,
                                                'Open .param or .txt file',
                                                'data')
            fname = os.path.abspath(fname[0])
        if not fname: return
        dat = loaddat(fname)
        self.dat = dat
        try:
            try:
                fmax = quickgetprm(paramf, 'f_max_spec', float)
            except:
                fmax = 40.
            f, lspec, avgdipole, avgspec = extractspec(dat, fmax=fmax)
            self.ntrial = len(lspec)
            self.updateCB()
            self.printStat('Extracted ' + str(len(lspec)) +
                           ' spectrograms from ' + fname)
            self.lextspec = lspec
            self.lextfiles.append(fname)
            self.avgdipole = avgdipole
            self.avgspec = avgspec
            self.lF.append(f)
        except:
            self.printStat('Could not extract Spectrograms from ' + fname)

        try:
            if len(self.lextspec) > 0:
                self.printStat('Plotting Spectrograms.')
                self.m.lextspec = self.lextspec
                self.m.dat = self.dat
                self.m.avgspec = self.avgspec
                self.m.avgdipole = self.avgdipole
                self.m.plot()
                self.m.draw()  # make sure new lines show up in plot
                self.printStat('')
        except:
            self.printStat('Could not plot data from ' + fname)
Пример #12
0
def updatedat (paramf):
  # update data dictionary (ddat) from the param file

  global basedir
  if debug: print('paramf:',paramf)
  getinputfiles(paramf)

  for k in ['dpl','spk']:
    if k in ddat:
      del ddat[k]
    silent = not os.path.exists(basedir)
    ddat[k] = readtxt(dfile[k], silent)
    if len(ddat[k]) == 0:
      del ddat[k]

  if not 'dpl' in ddat or not 'spk' in ddat:
    raise ValueError

  ddat['dpltrials'] = readdpltrials(basedir,quickgetprm(paramf,'N_trials',int))

  if os.path.isfile(dfile['spec']):
    ddat['spec'] = np.load(dfile['spec'])
  else:
    ddat['spec'] = None
Пример #13
0
else: plt.rcParams['font.size'] = dconf['fontsize'] = 10

# colors for the different cell types
dclr = {'L2_pyramidal' : 'g',
        'L5_pyramidal' : 'r',
        'L2_basket' : 'w', 
        'L5_basket' : 'b'}

ntrial = 1; tstop = -1; outparamf = spkpath = paramf = ''; EvokedInputs = OngoingInputs = PoissonInputs = False; 

for i in range(len(sys.argv)):
  if sys.argv[i].endswith('.txt'):
    spkpath = sys.argv[i]
  elif sys.argv[i].endswith('.param'):
    paramf = sys.argv[i]
    tstop = paramrw.quickgetprm(paramf,'tstop',float)
    ntrial = paramrw.quickgetprm(paramf,'N_trials',int)
    EvokedInputs = paramrw.usingEvokedInputs(paramf)
    OngoingInputs = paramrw.usingOngoingInputs(paramf)
    PoissonInputs = paramrw.usingPoissonInputs(paramf)
    outparamf = os.path.join(dconf['datdir'],paramf.split('.param')[0].split(os.path.sep)[-1],'param.txt')

try:
  extinputs = spikefn.ExtInputs(spkpath, outparamf)
except ValueError:
  print("Error: could not load spike timings from %s" % spkpath)

extinputs.add_delay_times()

alldat = {}
Пример #14
0
tstop = -1
ntrial = 1
scalefctr = 30e3
dplpath = ''
paramf = ''
for i in range(len(sys.argv)):
    if sys.argv[i].endswith('.txt'):
        dplpath = sys.argv[i]
    elif sys.argv[i].endswith('.param'):
        paramf = sys.argv[i]
        scalefctr = paramrw.find_param(paramf, 'dipole_scalefctr')
        if type(scalefctr) != float and type(scalefctr) != int:
            scalefctr = 30e3
        tstop = paramrw.find_param(paramf, 'tstop')
        ntrial = paramrw.quickgetprm(paramf, 'N_trials', int)

basedir = os.path.join(dconf['datdir'],
                       paramf.split(os.path.sep)[-1].split('.param')[0])

ddat = {}
ddat['dpltrials'] = readdpltrials(basedir, ntrial)
try:
    ddat['dpl'] = np.loadtxt(os.path.join(basedir, 'dpl.txt'))
except:
    print('Could not load', dplpath)
    quit()


class DipoleCanvas(FigureCanvas):
    def __init__(self,
Пример #15
0
  def plotsimdat (self):
    # plot the simulation data

    self.gRow = 0
    bottom = 0.0

    only_create_axes = False
    if not os.path.isfile(self.paramf):
      only_create_axes = True
      DrawSpec = False
      xl = (0.0, 1.0)
    else:
      # setup the figure axis for drawing the dipole signal
      dinty = self.getInputs()

      # try loading data. ignore failures
      try:
        updatedat(self.paramf)
        loaded_dat = True
      except ValueError:
        loaded_dat = False
        pass

      xl = (0.0, quickgetprm(self.paramf,'tstop',float))
      if dinty['Ongoing'] or dinty['Evoked'] or dinty['Poisson']:
        xo = self.plotinputhist(xl, dinty)
        if xo:
          self.gRow = xo[1]

      # whether to draw the specgram - should draw if user saved it or have ongoing, poisson, or tonic inputs
      DrawSpec = loaded_dat and \
                'spec' in ddat and \
                (find_param(dfile['outparam'],'save_spec_data') or dinty['Ongoing'] or dinty['Poisson'] or dinty['Tonic'])

    if DrawSpec: # dipole axis takes fewer rows if also drawing specgram
      self.axdipole = self.figure.add_subplot(self.G[self.gRow:5,0]) # dipole
      bottom = 0.08
    else:
      self.axdipole = self.figure.add_subplot(self.G[self.gRow:-1,0]) # dipole

    yl = (-0.001,0.001)
    self.axdipole.set_ylim(yl)
    self.axdipole.set_xlim(xl)

    left = 0.08
    w,h=getscreengeom()
    if w < 2800: left = 0.1
    self.figure.subplots_adjust(left=left,right=0.99,bottom=bottom,top=0.99,hspace=0.1,wspace=0.1) # reduce padding

    if only_create_axes:
      return

    try:
      updatedat(self.paramf)
    except ValueError:
      if 'dpl' not in ddat:
        # failed to load dipole data, nothing more to plot
        return

    ds = None
    xl = (0,ddat['dpl'][-1,0])
    dt = ddat['dpl'][1,0] - ddat['dpl'][0,0]

    # get spectrogram if it exists, then adjust axis limits but only if drawing spectrogram
    if DrawSpec:
      if ddat['spec'] is not None:
        ds = ddat['spec'] # spectrogram
        xl = (ds['time'][0],ds['time'][-1]) # use specgram time limits
      else:
        DrawSpec = False

    sampr = 1e3/dt # dipole sampling rate
    sidx, eidx = int(sampr*xl[0]/1e3), int(sampr*xl[1]/1e3) # use these indices to find dipole min,max

    N_trials = self.getNTrials()
    if debug: print('simdat: N_trials:',N_trials)

    yl = [0,0]
    yl[0] = min(yl[0],np.amin(ddat['dpl'][sidx:eidx,1]))
    yl[1] = max(yl[1],np.amax(ddat['dpl'][sidx:eidx,1]))

    if not self.optMode:
      # skip for optimization
      for lsim in lsimdat: # plot average dipoles from prior simulations
        olddpl = lsim[1]
        if debug: print('olddpl has shape ',olddpl.shape,len(olddpl[:,0]),len(olddpl[:,1]))
        self.axdipole.plot(olddpl[:,0],olddpl[:,1],'--',color='black',linewidth=self.gui.linewidth)

      if N_trials>1 and dconf['drawindivdpl'] and len(ddat['dpltrials']) > 0: # plot dipoles from individual trials
        for dpltrial in ddat['dpltrials']:
          self.axdipole.plot(dpltrial[:,0],dpltrial[:,1],color='gray',linewidth=self.gui.linewidth)
          yl[0] = min(yl[0],dpltrial[sidx:eidx,1].min())
          yl[1] = max(yl[1],dpltrial[sidx:eidx,1].max())

      if conf.dconf['drawavgdpl'] or N_trials <= 1:
        # this is the average dipole (across trials)
        # it's also the ONLY dipole when running a single trial
        self.axdipole.plot(ddat['dpl'][:,0],ddat['dpl'][:,1],'k',linewidth=self.gui.linewidth+1)
        yl[0] = min(yl[0],ddat['dpl'][sidx:eidx,1].min())
        yl[1] = max(yl[1],ddat['dpl'][sidx:eidx,1].max())
    else:
      for idx, opt in enumerate(optdat):
        optdpl = opt[1]
        if idx == len(optdat) - 1:
          # only show the last optimization
          self.axdipole.plot(optdpl[:,0],optdpl[:,1],'k',color='gray',linewidth=self.gui.linewidth+1)
          yl[0] = min(yl[0],optdpl[sidx:eidx,1].min())
          yl[1] = max(yl[1],optdpl[sidx:eidx,1].max())

      if self.hasinitoptdata():
        # show initial dipole in dotted black line
        self.axdipole.plot(initial_ddat['dpl'][:,0],initial_ddat['dpl'][:,1],'--',color='black',linewidth=self.gui.linewidth)
        yl[0] = min(yl[0],initial_ddat['dpl'][sidx:eidx,1].min())
        yl[1] = max(yl[1],initial_ddat['dpl'][sidx:eidx,1].max())

    scalefctr = getscalefctr(self.paramf)
    NEstPyr = int(self.getNPyr() * scalefctr)

    if NEstPyr > 0:
      self.axdipole.set_ylabel(r'Dipole (nAm $\times$ '+str(scalefctr)+')\nFrom Estimated '+str(NEstPyr)+' Cells',fontsize=dconf['fontsize'])
    else:
      self.axdipole.set_ylabel(r'Dipole (nAm $\times$ '+str(scalefctr)+')\n',fontsize=dconf['fontsize'])
    self.axdipole.set_xlim(xl); self.axdipole.set_ylim(yl)

    if DrawSpec: #
      if debug: print('ylim is : ', np.amin(ddat['dpl'][sidx:eidx,1]),np.amax(ddat['dpl'][sidx:eidx,1]))

      p_exp = ExpParams(self.paramf, debug=debug)
      if len(p_exp.expmt_groups) > 0:
        expmt_group = p_exp.expmt_groups[0]
      else:
        expmt_group = None
      p = p_exp.return_pdict(expmt_group, 0)

      gRow = 6
      self.axspec = self.figure.add_subplot(self.G[gRow:10,0]); # specgram
      cax = self.axspec.imshow(ds['TFR'],extent=(ds['time'][0],ds['time'][-1],ds['freq'][-1],ds['freq'][0]),aspect='auto',origin='upper',cmap=plt.get_cmap(p['spec_cmap']))
      self.axspec.set_ylabel('Frequency (Hz)',fontsize=dconf['fontsize'])
      self.axspec.set_xlabel('Time (ms)',fontsize=dconf['fontsize'])
      self.axspec.set_xlim(xl)
      self.axspec.set_ylim(ds['freq'][-1],ds['freq'][0])
      cbaxes = self.figure.add_axes([0.6, 0.49, 0.3, 0.005])
      cb = plt.colorbar(cax, cax = cbaxes, orientation='horizontal') # horizontal to save space
    else:
      self.axdipole.set_xlabel('Time (ms)',fontsize=dconf['fontsize'])
Пример #16
0
  def plotinputhist (self, xl, dinty):
    """ plot input histograms
        xl = x axis limits
        dinty = dict of input types used, determines how many/which axes created/displayed
    """

    extinputs = None
    plot_distribs = False

    sim_tstop = quickgetprm(self.paramf,'tstop',float)
    sim_dt = quickgetprm(self.paramf,'dt',float)
    num_step = ceil(sim_tstop / sim_dt) + 1
    times = np.linspace(0, sim_tstop, num_step)

    try:
      extinputs = spikefn.ExtInputs(dfile['spk'], dfile['outparam'])
      extinputs.add_delay_times()
      dinput = extinputs.inputs
    except ValueError:
      dinput = self.getInputDistrib()
      plot_distribs = True

    if len(dinput['dist']) <= 0 and len(dinput['prox']) <= 0 and \
        len(dinput['evdist']) <= 0 and len(dinput['evprox']) <= 0 and \
        len(dinput['pois']) <= 0:
      if debug: print('all hists 0!')
      return False

    self.hist=hist={x:None for x in ['feed_dist','feed_prox','feed_evdist','feed_evprox','feed_pois']}

    hasPois = len(dinput['pois']) > 0 and dinty['Poisson'] # this ensures synaptic weight > 0

    gRow = 0
    self.axdist = self.axprox = self.axpois = None # axis objects

    # check poisson inputs, create subplot
    if hasPois:
      self.axpois = self.figure.add_subplot(self.G[gRow,0])
      gRow += 1

    # check distal inputs, create subplot
    if (len(dinput['dist']) > 0 and dinty['OngoingDist']) or \
       (len(dinput['evdist']) > 0 and dinty['EvokedDist']):
      self.axdist = self.figure.add_subplot(self.G[gRow,0])
      gRow+=1

    # check proximal inputs, create subplot
    if (len(dinput['prox']) > 0 and dinty['OngoingProx']) or \
       (len(dinput['evprox']) > 0 and dinty['EvokedProx']):
      self.axprox = self.figure.add_subplot(self.G[gRow,0])
      gRow+=1


    # check input types provided in simulation
    if extinputs is not None and self.hassimdata(): # only valid param.txt file after sim was run
      if debug:
        print(len(dinput['dist']),len(dinput['prox']),len(dinput['evdist']),len(dinput['evprox']),len(dinput['pois']))

      if hasPois: # any Poisson inputs?
        extinputs.plot_hist(self.axpois,'pois',times,'auto',xl,color='k',hty='step',lw=self.gui.linewidth+1)

      if len(dinput['dist']) > 0 and dinty['OngoingDist']: # dinty condition ensures synaptic weight > 0
        extinputs.plot_hist(self.axdist,'dist',times,'auto',xl,color='g',lw=self.gui.linewidth+1)

      if len(dinput['prox']) > 0 and dinty['OngoingProx']: # dinty condition ensures synaptic weight > 0
        extinputs.plot_hist(self.axprox,'prox',times,'auto',xl,color='r',lw=self.gui.linewidth+1)

      if len(dinput['evdist']) > 0 and dinty['EvokedDist']: # dinty condition ensures synaptic weight > 0
        extinputs.plot_hist(self.axdist,'evdist',times,'auto',xl,color='g',hty='step',lw=self.gui.linewidth+1)

      if len(dinput['evprox']) > 0 and dinty['EvokedProx']: # dinty condition ensures synaptic weight > 0
        extinputs.plot_hist(self.axprox,'evprox',times,'auto',xl,color='r',hty='step',lw=self.gui.linewidth+1)
    elif plot_distribs:
      if len(dinput['evprox']) > 0 and dinty['EvokedProx']: # dinty condition ensures synaptic weight > 0
        prox_tot = np.zeros(len(dinput['evprox'][0][0]))
        for prox in dinput['evprox']:
          prox_tot += prox[1]
        plot = self.axprox.plot(dinput['evprox'][0][0],prox_tot,color='r',lw=self.gui.linewidth,label='evprox distribution')
        self.axprox.set_xlim(dinput['evprox'][0][0][0],dinput['evprox'][0][0][-1])
      if len(dinput['evdist']) > 0 and dinty['EvokedDist']: # dinty condition ensures synaptic weight > 0
        dist_tot = np.zeros(len(dinput['evdist'][0][0]))
        for dist in dinput['evdist']:
          dist_tot += dist[1]
        plot = self.axdist.plot(dinput['evdist'][0][0],dist_tot,color='g',lw=self.gui.linewidth,label='evdist distribution')
        self.axprox.set_xlim(dinput['evdist'][0][0][0],dinput['evdist'][0][0][-1])

    ymax = 0
    for ax in [self.axpois, self.axdist, self.axprox]:
      if not ax is None:
        if ax.get_ylim()[1] > ymax:
          ymax = ax.get_ylim()[1]

    if ymax == 0:
      if debug: print('all hists None!')
      return False
    else:
      for ax in [self.axpois, self.axdist, self.axprox]:
        if not ax is None:
          ax.set_ylim(0,ymax)
      if self.axdist:
        self.axdist.invert_yaxis()
      for ax in [self.axpois,self.axdist,self.axprox]:
        if ax:
          ax.set_xlim(xl)
          ax.legend(loc=1)  # legend in upper right
      return True,gRow