Ejemplo n.º 1
0
 def parse_nids(self, nids):
     """Convert list of nids to list of neurons"""
     r = self.experiment.r
     if nids == None:
         nids = sorted(r.n)  # use active neurons
     elif nids == 'quiet':
         nids = sorted(r.qn)  # use quiet neurons
     elif nids == 'all':
         nids = sorted(r.alln)  # use all neurons
     else:
         nids = tolist(nids)  # use specified nids
     neurons = [r.alln[nid] if nid in r.alln else None for nid in nids]
     return nids, neurons
Ejemplo n.º 2
0
 def parse_nids(self, nids):
     """Convert list of nids to list of neurons"""
     r = self.experiment.r
     if nids == None:
         nids = sorted(r.n.keys()) # use active neurons
     elif nids == 'quiet':
         nids = sorted(r.qn.keys()) # use quiet neurons
     elif nids == 'all':
         nids = sorted(r.alln.keys()) # use all neurons
     else:
         nids = tolist(nids) # use specified nids
     neurons = [ r.alln[nid] if nid in r.alln else None for nid in nids ]
     return nids, neurons
Ejemplo n.º 3
0
 def load(self, tracknames=None):
     treestr = self.level*TAB + self.id + '/'
     # print string to tree hierarchy and screen
     self.writetree(treestr + '\n')
     print(treestr)
     if tracknames != None:
         tracknames = tolist(tracknames)
         dirnames = tracknames
     else:
         # all track folder names for this animal:
         dirnames = [ dirname for dirname in os.listdir(self.path)
                      if os.path.isdir(os.path.join(self.path, dirname))
                      and dirname.lower().startswith('tr') ]
     dirnames.sort() # alphabetical order
     for dirname in dirnames:
         path = os.path.join(self.path, dirname)
         track = Track(path, animal=self)
         track.load()
         self.tr[track.id] = track
         self.__setattr__('tr' + str(track.id), track) # add shortcut attrib
Ejemplo n.º 4
0
 def load(self, tracknames=None):
     treestr = self.level * TAB + self.id + '/'
     # print string to tree hierarchy and screen
     self.writetree(treestr + '\n')
     print(treestr)
     if tracknames != None:
         tracknames = tolist(tracknames)
         dirnames = tracknames
     else:
         # all track folder names for this animal:
         dirnames = [
             dirname for dirname in os.listdir(self.path)
             if os.path.isdir(os.path.join(self.path, dirname))
             and dirname.lower().startswith('tr')
         ]
     dirnames.sort()  # alphabetical order
     for dirname in dirnames:
         path = os.path.join(self.path, dirname)
         track = Track(path, animal=self)
         track.load()
         self.tr[track.id] = track
         self.__setattr__('tr' + str(track.id),
                          track)  # add shortcut attrib
Ejemplo n.º 5
0
 def plot(self, t0=None, t1=None, chanis=None, gain=1, c='k', alpha=1.0, yunits='um',
          yticks=None, title=True, xlabel=True, relative2t0=False, lim2stim=False,
          scalebar=True, lw=4, figsize=(20, 6.5)):
     """Plot chanis of LFP data between t0 and t1 in sec. Unfortunatley, setting an alpha <
     1 doesn't seem to reveal detail when a line obscures itself, such as when plotting a
     very long time series. relative2t0 controls whether to plot relative to t0, or
     relative to start of ADC clock. lim2stim limits the time range only to when a stimulus
     was on screen, i.e. to the outermost times of non-NULL din. If only one chan is
     requested, it's plotted on a mV scale instead of a spatial scale."""
     self.get_data()
     ts = self.get_tssec() # full set of timestamps, in sec
     if t0 == None:
         t0, t1 = ts[0], ts[-1]
     if t1 == None:
         t1 = t0 + 10 # 10 sec window
     if chanis == None:
         chanis = range(len(self.chans)) # all chans
     if lim2stim:
         t0, t1 = self.apply_lim2stim(t0, t1)
     t0i, t1i = ts.searchsorted((t0, t1))
     ts = ts[t0i:t1i] # constrained set of timestamps, in sec
     chanis = tolist(chanis)
     nchans = len(chanis)
     # grab desired channels and time range:
     data = self.data[chanis][:, t0i:t1i]
     if nchans > 1: # convert uV to um:
         totalgain = self.UV2UM * gain
         data = data * totalgain
     else: # convert uV to mV:
         data = data / 1000
         yunits = 'mV'
     nt = len(ts)
     assert nt == data.shape[1]
     if relative2t0:
         # convert ts to time from t0, otherwise plot time from start of ADC clock:
         ts -= t0
     x = np.tile(ts, nchans)
     x.shape = nchans, nt
     segments = np.zeros((nchans, nt, 2)) # x vals in col 0, yvals in col 1
     segments[:, :, 0] = x
     if nchans > 1:
         segments[:, :, 1] = -data # set to -ve here because of invert_yaxis() below
     else:
         segments[:, :, 1] = data
     if nchans > 1: # add y offsets:
         maxypos = 0
         for chanii, chani in enumerate(chanis):
             chan = self.chans[chani]
             ypos = self.chanpos[chan][1] # in um
             segments[chanii, :, 1] += ypos # vertical distance below top of probe
             maxypos = max(maxypos, ypos)
         if yunits == 'mm': # convert from um to mm
             segments[:, :, 1] /= 1000
             maxypos = maxypos / 1000 # convert from int to float
             totalgain = totalgain / 1000
     lc = LineCollection(segments, linewidth=1, linestyle='-', colors=c, alpha=alpha,
                         antialiased=True, visible=True)
     f = pl.figure(figsize=figsize)
     a = f.add_subplot(111)
     a.add_collection(lc) # add to axes' pool of LCs
     if scalebar: # add vertical scale bar at end of last channel to represent 1 mV:
         if nchans > 1:
             ymin, ymax = maxypos-500*totalgain, maxypos+500*totalgain # +/- 0.5 mV
         else:
             ymin, ymax = -0.5, 0.5 # mV
         a.vlines(ts.max()*0.99, ymin, ymax, lw=lw, colors='e')
     a.autoscale(enable=True, tight=True)
     # depending on relative2t0 above, x=0 represents either t0 or time ADC clock started:
     a.set_xlim(xmin=0)
     if nchans > 1:
         a.invert_yaxis() # for spatial scale
     if yticks != None:
         a.set_yticks(yticks)
     # turn off annoying "+2.41e3" type offset on x axis:
     formatter = mpl.ticker.ScalarFormatter(useOffset=False)
     a.xaxis.set_major_formatter(formatter)
     if xlabel:
         a.set_xlabel("time (s)")
     if yunits == 'um':
         a.set_ylabel("depth ($\mu$m)")
     elif yunits == 'mm':
         a.set_ylabel("depth (mm)")
     elif yunits == 'mV':
         a.set_ylabel("LFP (mV)")
     titlestr = lastcmd()
     gcfm().window.setWindowTitle(titlestr)
     if title:
         a.set_title(titlestr)
         a.text(0.998, 0.99, '%s' % self.r.name, transform=a.transAxes,
                horizontalalignment='right', verticalalignment='top')
     f.tight_layout(pad=0.3) # crop figure to contents
     self.f = f
     return self
Ejemplo n.º 6
0
 def plot(self,
          t0=None,
          t1=None,
          chanis=None,
          gain=1,
          c='k',
          alpha=1.0,
          yunits='um',
          yticks=None,
          title=True,
          xlabel=True,
          relative2t0=False,
          lim2stim=False,
          scalebar=True,
          lw=4,
          figsize=(20, 6.5)):
     """Plot chanis of LFP data between t0 and t1 in sec. Unfortunatley, setting an alpha <
     1 doesn't seem to reveal detail when a line obscures itself, such as when plotting a
     very long time series. relative2t0 controls whether to plot relative to t0, or
     relative to start of ADC clock. lim2stim limits the time range only to when a stimulus
     was on screen, i.e. to the outermost times of non-NULL din. If only one chan is
     requested, it's plotted on a mV scale instead of a spatial scale."""
     self.get_data()
     ts = self.get_tssec()  # full set of timestamps, in sec
     if t0 == None:
         t0, t1 = ts[0], ts[-1]
     if t1 == None:
         t1 = t0 + 10  # 10 sec window
     if chanis == None:
         chanis = range(len(self.chans))  # all chans
     if lim2stim:
         t0, t1 = self.apply_lim2stim(t0, t1)
     t0i, t1i = ts.searchsorted((t0, t1))
     ts = ts[t0i:t1i]  # constrained set of timestamps, in sec
     chanis = tolist(chanis)
     nchans = len(chanis)
     # grab desired channels and time range:
     data = self.data[chanis][:, t0i:t1i]
     if nchans > 1:  # convert uV to um:
         totalgain = self.UV2UM * gain
         data = data * totalgain
     else:  # convert uV to mV:
         data = data / 1000
         yunits = 'mV'
     nt = len(ts)
     assert nt == data.shape[1]
     if relative2t0:
         # convert ts to time from t0, otherwise plot time from start of ADC clock:
         ts -= t0
     x = np.tile(ts, nchans)
     x.shape = nchans, nt
     segments = np.zeros((nchans, nt, 2))  # x vals in col 0, yvals in col 1
     segments[:, :, 0] = x
     if nchans > 1:
         segments[:, :,
                  1] = -data  # set to -ve here because of invert_yaxis() below
     else:
         segments[:, :, 1] = data
     if nchans > 1:  # add y offsets:
         maxypos = 0
         for chanii, chani in enumerate(chanis):
             chan = self.chans[chani]
             ypos = self.chanpos[chan][1]  # in um
             segments[chanii, :,
                      1] += ypos  # vertical distance below top of probe
             maxypos = max(maxypos, ypos)
         if yunits == 'mm':  # convert from um to mm
             segments[:, :, 1] /= 1000
             maxypos = maxypos / 1000  # convert from int to float
             totalgain = totalgain / 1000
     lc = LineCollection(segments,
                         linewidth=1,
                         linestyle='-',
                         colors=c,
                         alpha=alpha,
                         antialiased=True,
                         visible=True)
     f = pl.figure(figsize=figsize)
     a = f.add_subplot(111)
     a.add_collection(lc)  # add to axes' pool of LCs
     if scalebar:  # add vertical scale bar at end of last channel to represent 1 mV:
         if nchans > 1:
             ymin, ymax = maxypos - 500 * totalgain, maxypos + 500 * totalgain  # +/- 0.5 mV
         else:
             ymin, ymax = -0.5, 0.5  # mV
         a.vlines(ts.max() * 0.99, ymin, ymax, lw=lw, colors='e')
     a.autoscale(enable=True, tight=True)
     # depending on relative2t0 above, x=0 represents either t0 or time ADC clock started:
     a.set_xlim(xmin=0)
     if nchans > 1:
         a.invert_yaxis()  # for spatial scale
     if yticks != None:
         a.set_yticks(yticks)
     # turn off annoying "+2.41e3" type offset on x axis:
     formatter = mpl.ticker.ScalarFormatter(useOffset=False)
     a.xaxis.set_major_formatter(formatter)
     if xlabel:
         a.set_xlabel("time (s)")
     if yunits == 'um':
         a.set_ylabel("depth ($\mu$m)")
     elif yunits == 'mm':
         a.set_ylabel("depth (mm)")
     elif yunits == 'mV':
         a.set_ylabel("LFP (mV)")
     titlestr = lastcmd()
     gcfm().window.setWindowTitle(titlestr)
     if title:
         a.set_title(titlestr)
         a.text(0.998,
                0.99,
                '%s' % self.r.name,
                transform=a.transAxes,
                horizontalalignment='right',
                verticalalignment='top')
     f.tight_layout(pad=0.3)  # crop figure to contents
     self.f = f
     return self