コード例 #1
0
ファイル: translateModel.py プロジェクト: alissonb/gymnoRTus
def main():
    description = 'Translates a model trained by gymnotools into C source code for compiling the recog module'
    parser = ArgParser(description=description, formatter_class=argparse.RawTextHelpFormatter)

    parser.add_argument('--filter', required=True, type=argparse.FileType('r'), help='Feature filter')
    parser.add_argument('--rescale', required=True, type=argparse.FileType('rb'), help='Rescaling factors')
    parser.add_argument('--svm', required=True, type=argparse.FileType('r'), help='SVM model')

    args = parser.parse_args()

    filtered_features = map(int, args.filter.xreadlines())

    rescale_num_features = os.fstat(args.rescale.fileno()).st_size // np.dtype(np.float32).itemsize // 2
    rescale = np.memmap(args.rescale, mode='r', dtype=np.float32, shape=(2, rescale_num_features))
    rescale = np.array(rescale)

    if rescale.shape[1] != len(filtered_features):
        rescale = rescale[:, filtered_features]

    svm = SVMModel(args.svm)
    assert svm.elements == rescale.shape[1], 'SVM model trained for an incorrect number of features'

    gen_filter('feature_filter.h', filtered_features)
    gen_rescale('rescaling_factors.h', rescale)
    gen_svm('svm_model.h', svm)
    gen_cfg('generated_cfg.h')
コード例 #2
0
ファイル: calibrate.py プロジェクト: alissonb/gymnoRTus
def main():
    parser = ArgParser(
        description='Calibrates DC offsets for the OffsetSubtractor',
        formatter_class=argparse.RawDescriptionHelpFormatter)
    
    parser.add_argument('--middlescale', dest='action', action='store_const',
                        const=middlescale, default=parser.print_help,
                        help='Sets the offset to the middle of the scale')
    parser.add_argument('--curroff', dest='action', action='store_const',
                        const=curroff, default=parser.print_help,
                        help='Calibrates to the current measured offset')
    
    args = parser.parse_args()
    args.action()
コード例 #3
0
ファイル: spikes.py プロジェクト: alissonb/gymnoRTus
def main():
    description = "Generates a .spikes file from winCollect FIFO output"
    parser = ArgParser(description=description, formatter_class=argparse.RawTextHelpFormatter)

    parser.add_argument("fifofile", type=argparse.FileType("r"), help="Input data (from winCollect FIFO)")
    parser.add_argument("outfile", type=argparse.FileType("w"), help="Output file (.spikes)")
    parser.add_argument("--fixedwin", help="Fixed window (use for single-fish data files)", action="store_true")
    parser.add_argument("--windowSize", type=int, default=cfg.SpikesWinSize, help="Window size, if --fixedwin")
    parser.add_argument(
        "--afterRef", type=int, default=cfg.SpikesAfterRef, help="Samples after reference point, if --fixedwin"
    )
    parser.add_argument("--saturation", type=str, help="high,low saturation level to filter out")
    parser.add_argument("--onlyabove", type=float, default=0.0, help="Only output spikes above this amplitude")
    parser.add_argument("--winlen", type=argparse.FileType("w"), help="Output original window lengths to a text file")

    args = parser.parse_args()

    fifofile = args.fifofile
    nchan = cfg.EnabledCh
    outfile = args.outfile

    fixedwin = args.fixedwin
    windowSize = args.windowSize
    afterRef = args.afterRef

    saturation = args.saturation
    satHigh = np.inf
    satLow = -np.inf
    if saturation is not None:
        satHigh = float(saturation.split(",")[0].strip())
        satLow = float(saturation.split(",")[1].strip())
    onlyAbove = args.onlyabove
    winlenFile = args.winlen

    afterRef += 1  # calc correction
    lastEventLen = 0

    for size, ref, ts, win in iter_fifo_windows(fifofile):
        if winlenFile:
            winlenFile.write("%d\n" % size)
        if fixedwin == True:
            samples = windowSize
            window = np.zeros((windowSize, nchan))
            win = win.reshape((size, nchan))
            # cut end
            if ref > afterRef:
                win = win[: -ref + afterRef, :]
                ref = afterRef
                size = win.shape[0]
            # cut start
            if size > windowSize:
                win = win[size - windowSize :, :]
                size = win.shape[0]
            # copy to fixed size window
            assert ref <= afterRef
            assert size <= windowSize
            endpos = windowSize - afterRef + ref
            startpos = endpos - size
            window[startpos:endpos, :] = win
            # fill any remaining samples with DC level
            for i in xrange(0, startpos):
                window[i, :] = window[startpos, :]
            for i in xrange(endpos, windowSize):
                window[i, :] = window[endpos - 1, :]
            # make array plain once again
            window = window.reshape((windowSize * nchan,))
        else:
            samples = size
            window = win

        filtered_windowsDic = check_window(window, nchan, samples, onlyAbove, satLow, satHigh)
        offset = ts * nchan * np.dtype("float32").itemsize

        outfile.write(struct.pack("i", lastEventLen))
        outfile.write(struct.pack("q", offset))
        outfile.write(struct.pack("i", samples))
        outfile.write(struct.pack("i", len(filtered_windowsDic)))
        lastLengths = 0
        for c in sorted(filtered_windowsDic.keys()):
            outfile.write(struct.pack("i", c))
            buff = filtered_windowsDic[c].astype(np.dtype("float32")).tostring()
            outfile.write(buff)
            lastLengths = lastLengths + 4 + len(buff)

        lastEventLen = headerLen + lastLengths