From 6c08ab9bd5f383f4338896207d146d8597f2e437 Mon Sep 17 00:00:00 2001 From: Roger Herikstad Date: Thu, 15 Jun 2017 11:22:12 +0800 Subject: [PATCH] Adds new type for spike waveforms --- src/SpikeSorter.jl | 1 + src/io.jl | 32 ++++++++++++++++++++++++++++++++ src/types.jl | 5 +++++ test/io.jl | 9 +++++++++ test/runtests.jl | 4 ++++ 5 files changed, 51 insertions(+) create mode 100644 src/io.jl create mode 100644 test/io.jl create mode 100644 test/runtests.jl diff --git a/src/SpikeSorter.jl b/src/SpikeSorter.jl index d53db78..1730f26 100644 --- a/src/SpikeSorter.jl +++ b/src/SpikeSorter.jl @@ -8,6 +8,7 @@ include("viterbi.jl") include("readfiles.jl") include("features.jl") include("extraction.jl") +include("io.jl") import GUICheck if GUICheck.hasgui() include("plot.jl") diff --git a/src/io.jl b/src/io.jl new file mode 100644 index 0000000..bb45ff2 --- /dev/null +++ b/src/io.jl @@ -0,0 +1,32 @@ +import Base.write + +function Base.write(io::IO, waveforms::SpikeWaveforms) + #write a header + headersize = 64 + nbytes = 0 + npoints, nchannels, nspikes = size(waveforms.waveforms) + nbytes += write(io, npoints) + nbytes += write(io, nchannels) + nbytes += write(io, nspikes) + seek(io, headersize) + + #write timestamps, followed by waveforms + nbytes += write(io, waveforms.timestamps) + nbytes += write(io, waveforms.waveforms) + nbytes +end + +function Base.read(io::IO, ::Type{SpikeWaveforms}) + #read the header + headersize = 64 + npoints = read(io, Int64) + nchannels = read(io, Int64) + nspikes = read(io, Int64) + seek(io, headersize) + + #read timestamps + timestamps = read(io, Float64, nspikes) + waveforms = read(io, Float64, (npoints, nchannels, nspikes)) + SpikeWaveforms(waveforms, timestamps) +end + diff --git a/src/types.jl b/src/types.jl index 3f29b17..0967c2e 100644 --- a/src/types.jl +++ b/src/types.jl @@ -1,5 +1,10 @@ import HDF5 +type SpikeWaveforms + waveforms::Array{Float64,3} + timestamps::Array{Float64,1} +end + type TemplateFile templates::Array{Float64,3} cinv::Array{Float64,2} diff --git a/test/io.jl b/test/io.jl new file mode 100644 index 0000000..9fd41bd --- /dev/null +++ b/test/io.jl @@ -0,0 +1,9 @@ +wf = SpikeSorter.SpikeWaveforms(randn(60,1,1024),cumsum(rand(1024))) +fname = tempname() + +write(fname, wf) +wf2 = read(fname, SpikeSorter.SpikeWaveforms) + +@test_approx_eq wf.timestamps wf2.timestamps +@test_approx_eq wf.waveforms wf2.waveforms + diff --git a/test/runtests.jl b/test/runtests.jl new file mode 100644 index 0000000..5639fd0 --- /dev/null +++ b/test/runtests.jl @@ -0,0 +1,4 @@ +using Base.Test +using SpikeSorter + +include("io.jl")