[VoxBo] spike-finder tool

Daniel Drucker ddrucker at psych.upenn.edu
Wed Jan 9 13:46:49 EST 2008


Sorry, forgot to paste the script!

#!/usr/bin/env python
# global signal spikes finder
# Daniel Drucker <ddrucker at psych.upenn.edu>

import sys, os, re, math
from optparse import OptionParser

def RefRead(filePath):
    values = []
    for line in open(filePath, 'rU').readlines():
        if re.match(';',line): continue # skip comment lines
        try:
            values += [float(line)]
        except: pass
    return values

def main():
    usage = "usage: %prog [options]\nRun in the directory that
contains all the _01, _02 etc directories."
    parser = OptionParser(usage=usage)
    parser.add_option("-d", "--deviations", dest="deviations",
                      default=3.5, help="use DEVIATIONS many
deviations", action="store", type="float", metavar="DEVIATIONS")
    parser.add_option("-s","--smooth",
                      default=False, help="look at files starting with
s (e.g., smoothed files)", action="store_const", dest="smoothed",
default="", const="s")
    (options, args) = parser.parse_args()

    values = []

    # walk the tree and get the files
    for d in os.listdir("."):
        # is this a data directory?
        if re.match('^\w\d\d\d\d\d\d\w_\d\d$', d):
            for f in os.listdir(d):
                # is this a global signal file?
                if re.match('^' + options.smoothed + d + '_GS.ref$', f):
                    values += RefRead(os.path.join(d,f))

    sum = sumsq = n = 0
    for v in values:
        sum += v
        sumsq += v**2
        n+=1

    sd = math.sqrt((sumsq - sum**2 / n) / n)
    mean = sum/n
    dev = sd * options.deviations

    spikes = []
    for i in range(n):
        if abs(values[i] - mean) > dev:
            spikes += [str(i)]

    if spikes:
        print 'newcov spike "' + ",".join(spikes) + '"'
        print "     type n"
        print "end"
    else:
        sys.stderr.write('No spikes found.')


if __name__ == "__main__":
    main()


On Jan 9, 2008 1:46 PM, Daniel Drucker <ddrucker at psych.upenn.edu> wrote:
> > I present a little utility that does the job: /home/ddrucker/bin/spikes
>
> A slightly modified version is 'spikespy' - it's written in Python
> instead of perl, and does some command line option parsing.
>
> Daniel
>


More information about the voxbo-general mailing list