code: def spike_latency(signal, threshold, fs):
      
      over, = np.where(signal>threshold)
      segments, = np.where(np.diff(over) > 1)
  
      if len(over) > 1:
          if len(segments) == 0:
              # only signal peak
              idx = over[0] + np.argmax(signal[over[0]:over[-1]])
              latency = float(idx)/fs
          elif segments[0] == 0:
              #first point in singleton
              latency = float(over[0])/fs
          else:
              idx = over[0] + np.argmax(signal[over[0]:over[segments[0]]])
              latency = float(idx)/fs
      elif len(over) > 0:
          latency = float(over[0])/fs
      else:
          latency = np.nan
  
      return latency
docs: Find the latency of the first spike over threshold
  
      :param signal: Spike trace recording (vector)
      :type signal: numpy array
      :param threshold: Threshold value to determine spikes
      :type threshold: float
      :returns: float -- Time of peak of first spike, or None if no values over threshold
  
      This is the same as the first value returned from calc_spike_times