Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
This new version of JSAnalysis takes the raw X and Y joystick values and undecimates them and then uses them to create the joystick position trace used for analysis. The trace is baseline subtracted and rectified to ensure steady baseline throughout the session.

MAN
  • Loading branch information
YttriLab authored Apr 16, 2020
1 parent 20c7cc0 commit d6cd71e
Showing 1 changed file with 29 additions and 38 deletions.
67 changes: 29 additions & 38 deletions MATLAB Code/JSAnalysis.m
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
% This function takes in mouse name as the primary argument and will find data saved using SavemicroSDData.m
% This funtion will generate a MouseName_DATA.mat file (cell) with each row being a session and each column being a type of data for that session.
% After calling this function the first time, it will find any new data that has been saved using SavemicroSDData.m and append it to the MouseName_DATA.mat file.
%
%
% This funtion requires the following funtions
% getJS.m
% unDecimate.m
Expand All @@ -14,7 +14,7 @@
% hline - https://www.mathworks.com/matlabcentral/fileexchange/1039-hline-and-vline
% vline - https://www.mathworks.com/matlabcentral/fileexchange/1039-hline-and-vline
% numSubplots - https://www.mathworks.com/matlabcentral/fileexchange/26310-numsubplots-neatly-arrange-subplots
%
%


% Datebase should be changed to local directory where data will be saved change base directory to where the data can be found
Expand All @@ -30,21 +30,21 @@
%%

%find files
Database = 'C:\Users\yttri-lab\Documents\MATLAB\JS2'; %change to location where your data will be saved
Database = 'C:\Users\Yttri-lab\Documents\MATLAB\JS2\Test'; %change to location where your data will be saved

%change below basen variables to any location that your data is saved:
base1 = 'N:\yttri-lab\Data Transfer\241_SDData';
base2 = 'N:\yttri-lab\Data Transfer\239a_SDData'; % if only one location this variable can be removed or use "base2 = ''";
base1 = 'M:\Data Transfer\241_SDData';
base2 = 'M:\Data Transfer\239a_SDData'; % if only one location this variable can be removed or use "base2 = ''";

Bases = {base1 base2};
%to add more locations make new "basen" variables (location of data) and append to the above line as the following Bases = {base1 base2 basen};
Bases = {base1 base2};
%to add more locations make new "basen" variables (location of data) and append to the above line as the following Bases = {base1 base2 basen};

FF={}; nbases = size(Bases,2);
for i =1:nbases
files = dir([Bases{i} filesep MouseName filesep '*csv']);
Acell = struct2cell(files)';
mn = Acell(contains(Acell(:,1), MouseName),:);
FF = [FF; mn repmat({i},size(mn,1),1)]; %append location handle
FF = [FF; mn repmat({i},size(mn,1),1)]; %append location handle
end
files = FF;

Expand Down Expand Up @@ -122,28 +122,34 @@
DATA(sz+i,1:4) = getJS(filepth); %[tlt Date endtime data]; - - - can use csvread if you have no issues with your csv files
%data = [time EM TrialCt X Y pos baseX baseY SolOpenDuration DelaytoRew ITI Threshold]
data = DATA{i+sz,4}; ct = 2; threshold = data(end,12); nTrials = data(end,3);
TrialCt = data(end,3); SessionTime = ceil((data(end,1)/1000)/60); DATA{sz+i,6} = TrialCt/SessionTime;

%undecimate JS position
% Convert js sampling rate to 1 kHz
for j = [2 6] %column 2(eventmarker) and 6 (JS Position)
% Convert js sampling rate to 1 kHz
UndTimeEM = unDecimate(data,2); %column 2(eventmarker)

ct = 2;
for j = [4 5] %undecimate X and Y
nd = unDecimate(data,j);
if j == 2
if j == 4
D = zeros(size(nd,1),3);
D(:,1:2) = nd;
else
D(:,ct) = nd(:,2);
end
ct = ct+1;
end
DATA{sz+i,5} = D;
TrialCt = data(end,3);
SessionTime = ceil((data(end,1)/1000)/60);
DATA{sz+i,6} = TrialCt/SessionTime;
DATA{sz+i,16} = D;

%calculate Euclidean distance for JS position and baseline subtract
%median value and rectify
X = D(:,2); Y = D(:,3); pos = sqrt((X.^2)+(Y.^2)); med = nanmedian(pos); blrec = abs(pos-med);
DATA{sz+i,5} = [UndTimeEM blrec];

%plot JS trace
f = figure; plot(D(:,3))
f = figure; plot(blrec)
hold on
plot(D(:,2)*25) %eventmarker (EM)
plot(UndTimeEM(:,2)*25) %eventmarker (EM)
plot(repmat(threshold, size(D,1),1))
tlt = sprintf('%s\n%d trials in %d mins',filenames{i+sz}, TrialCt,SessionTime);
legend({'pos', 'EM', 'threshold'})
Expand All @@ -152,24 +158,9 @@
pth = [Database filesep MouseName filesep FILES{i,1}(1:end-4) '_trace.fig']; %location/name of figure
savefig(f,pth)


ct = 2;
for j = [4 5] %undecimate X and Y
nd = unDecimate(data,j);
if j == 4
D = zeros(size(nd,1),3);
D(:,1:2) = nd;
else
D(:,ct) = nd(:,2);
end
ct = ct+1;
end
DATA{sz+i,16} = D;

%%
%get reach times

data = DATA{i+sz,5};
data = DATA{sz+i,5};
Date = DATA{i+sz,2};
[reachStart, reachStop, reach0, pos1, pos2] = getReachTimes(data, MouseName, Date); %[reachstarttime reachstoptimes fullreachtrace ReachesAlignedtoStart ReachesAlignedtoStop]
hold on;plot(data(~isnan(data(:,2)),2)*25)%add in event marker (EM)
Expand All @@ -183,7 +174,7 @@
for k = 1:size(reachStart,2)
mn = reach0(reachStart(k):reachStart(k)+400);
MD(k) = max(mn)-mn(1); %max dispalcement
PA(k) = max(mn);%peak amplitude
PA(k) = max(mn);%peak amplitude
%figure; plot((reach0(reachStart(k):reachStart(k)+400)))
end
DATA{sz+i,12} = MD; DATA{i+sz,13} = PA;
Expand All @@ -192,7 +183,7 @@
pre = 249; post = 1500;
reaches_A = zeros(size(reachStart,2),1750);
d = reach0;
figure(F); subplot(2,2,2); %plot all reaches and average on summary figure
figure(F); subplot(2,2,2); %plot all reaches and average on summary figure
for r = 1:size(reachStart,2)
if reachStart(r)<pre
reaches_A(r,:) = [nan(1,pre ) d(reachStart(r):reachStart(r) + post)'];
Expand Down Expand Up @@ -221,7 +212,7 @@
figure; %plot mean and sem reach
plot(m); hold on
fill(X, Y, 'b', 'facealpha', .25, 'edgealpha', .25, 'edgecolor', 'b'); hold on
hline(threshold)
hline(threshold)
vline(pre)

%PVA - position velocity acceleration
Expand Down Expand Up @@ -346,7 +337,7 @@
pth = [Database filesep MouseName filesep mn];

save(pth , 'DATA','-v7.3');
hands(i) = F; %add summary figure to hands variable
hands(i) = F; %add summary figure to hands variable
end
%%
%extra plotting
Expand All @@ -359,7 +350,7 @@
sesh = zeros(size(mn,1)-1,1);
for k = 2:size(mn,1)
ems = data(mn(k-1)+1:mn(k),2)==3;
[~,trials] = bwlabel(ems);
[~,trials] = bwlabel(ems);
sesh(k-1) = trials/5;
end
DATA{j,15} = sesh;
Expand Down

0 comments on commit d6cd71e

Please sign in to comment.