From 7320abcc3ccdfbbe94d4b04720ac53d46ae93762 Mon Sep 17 00:00:00 2001 From: Jakob Voigts Date: Tue, 12 Mar 2019 15:25:46 -0400 Subject: [PATCH] added code for re-extracting spikes from raw data --- extract_spikes_from_raw.m | 167 ++++++++++++++++++++++++++++++++++++++ sc_getpolygon.m | 1 - sc_mua2features.m | 2 +- sc_plotinbox.m | 4 + 4 files changed, 172 insertions(+), 2 deletions(-) create mode 100644 extract_spikes_from_raw.m diff --git a/extract_spikes_from_raw.m b/extract_spikes_from_raw.m new file mode 100644 index 0000000..ca8f3ef --- /dev/null +++ b/extract_spikes_from_raw.m @@ -0,0 +1,167 @@ + + +%% load raw data + +set(0,'DefaultFigureWindowStyle','docked'); % fix matlab's figure positioning bug + +% raw data available on +% https://drive.google.com/drive/folders/1CwFcErgp3F3D6I2TB_hTtW1JAQB21TAC?usp=sharing +% +datapath='/home/jvoigts/Desktop/TT13_continuous_3/' + +out_dir='/home/jvoigts/Desktop/TT13_continuous_3/'; +out_name = 'marie_rsc_test.mat'; + +source_channels=[40 40 38 36]; + +data_raw=[]; +for ch=source_cahnnels % grab 4 channels of raw data from one tetrode + fname=sprintf('100_CH%d.continuous',ch) + [data, timestamps, info]=load_open_ephys_data_faster(fullfile(datapath,fname)); + data_raw(:,end+1) = data; +end; + +data_raw=data_raw.*info.header.bitVolts; +fs = info.header.sampleRate; + +%data_raw=data_raw(1:30000,:); % cut away some data for faster testing + +%% plot + +plotlim=50000; +figure(1); +clf; +hold on; +plot(data_raw(1:plotlim,:)); + + +%% filter + +clf; hold on; +[b,a] = butter(3, [300 3000]/(fs/2)); % choose filter (normalize bp freq. to nyquist freq.) + +data_bp=filtfilt(b,a,data_raw); %use zero phase filter + +%% plot filtered +offset=plotlim*0; +clf; +plot(data_bp([1:plotlim]+offset,:)); +hold on; + +%% find treshold crossings +treshold=-6; +crossed= min(data_bp,[],2)<-treshold; % trigger if _any_ channel crosses in neg. direction + +spike_onsets=find(diff(crossed)==1); + +length_sec=size(data,1)/fs; +fprintf('got %d candidate events in %dmin of data, ~%.2f Hz\n',numel(spike_onsets),round(length_sec/60),numel(spike_onsets)/length_sec); + +%% plot some spike onsets +for i=1:100%numel(spike_onsets) + if(spike_onsets(i)0; % dont plot noise cluster + scatter(dat_x(ii),dat_y(ii),(0.5+(spikes.cluster(ii)==cluster_selected))*20,spikes.cluster(ii)*2,'filled'); + plot(dat_x(spike_selected),dat_y(spike_selected),'ro','markerSize',10); + title(sprintf('current cluster %d, projection %d, %d spikes in cluster',cluster_selected,use_projection,sum(spikes.cluster==cluster_selected))); + + [x,y,b]=ginput(1); + + if b>47 & b <58 % number keys, cluster select + cluster_selected=b-48; + end; + + if b==30; use_projection=mod(use_projection,6)+1; end; % up/down: cycle trough projections + if b==31; use_projection=mod(use_projection-2,6)+1; end; % up/down: cycle trough projections + if b==27; disp('exited'); run=0; end; % esc: exit + + if b==43 | b==42; % +, add to cluster + t= imfreehand(ax,'Closed' ,1); + t.setClosed(1); + r=t.getPosition; + px=r(:,1);py=r(:,2); + in = inpolygon(dat_x,dat_y,px,py); + if b==43 % +, add + spikes.cluster(in)=cluster_selected; + else % *. intersect cluster (move all non selected to null cluster) + spikes.cluster(~in & spikes.cluster==cluster_selected)=1; + end; + end; + + if b==1 % left click - select individual waveform to plot + [~,spike_selected]=min((dat_x-x).^2 +(dat_y-y).^2); + end; + +end; diff --git a/sc_getpolygon.m b/sc_getpolygon.m index 199499a..aa17e5d 100644 --- a/sc_getpolygon.m +++ b/sc_getpolygon.m @@ -27,7 +27,6 @@ t= imfreehand(gca,'Closed' ,1); t.setClosed(1); r=t.getPosition; - px=r(:,1);py=r(:,2); % remap from screen space to feature space diff --git a/sc_mua2features.m b/sc_mua2features.m index ddfc39d..90e8db7 100644 --- a/sc_mua2features.m +++ b/sc_mua2features.m @@ -99,7 +99,7 @@ %% write data -features.data=zeros(6,length(mua.ts)); +features.data=zeros(2,length(mua.ts)); lastpercent=0; diff --git a/sc_plotinbox.m b/sc_plotinbox.m index a008c1c..04bbf72 100644 --- a/sc_plotinbox.m +++ b/sc_plotinbox.m @@ -20,6 +20,10 @@ function sc_plotinbox(x,y,fstr,c,s) fstr='k.'; end; +if min(c==[0 0 1])==1 + c=[.2 .7 1]; +end; + plot(x,y,fstr,'color',c,'MarkerSize',m) plot([-1 -1],[-1 1],'k');