Skip to content

Commit

Permalink
sessions renamed to trials, function set for 1d and 3d analysis almos…
Browse files Browse the repository at this point in the history
…t complete
  • Loading branch information
mattgaidica committed Apr 28, 2014
1 parent 60c11d4 commit 81ec0db
Show file tree
Hide file tree
Showing 8 changed files with 69 additions and 70 deletions.
16 changes: 9 additions & 7 deletions analyzeSessions.m → analyzeTrials.asv
Expand Up @@ -2,9 +2,11 @@

% This function requires that a video has already been cropped and pawData has already been saved
% into the sessions folder for each of 3 viewing angles
function analyzeSessions(frameRate,pxToMm,pelletCoords)
function analyzeTrials(frameRate,pxToMm,pelletCoords)
workingDirectory = uigetdir;
% load all the .mat sessions created for each video, from each angle
workingDirectoryParts = strsplit(workingDirectory,filesep);
sessionName = workingDirectoryParts(end);
% load all the .mat trials created for each video, from each angle
leftSessions = dir(fullfile(workingDirectory,'left','sessions','*.mat'));
centerSessions = dir(fullfile(workingDirectory,'center','sessions','*.mat'));
rightSessions = dir(fullfile(workingDirectory,'right','sessions','*.mat'));
Expand Down Expand Up @@ -42,18 +44,18 @@ function analyzeSessions(frameRate,pxToMm,pelletCoords)

% save data
mkdir(fullfile(workingDirectory,'_xyzData'));
save(fullfile(workingDirectory,'_xyzData','xyzData'),'allAlignedXyzPawCenters','allAlignedXyzDistPawCenters',...
save(fullfile(workingDirectory,'_xyzData',sessionName,'_xyzData'),'allAlignedXyzPawCenters','allAlignedXyzDistPawCenters',...
'allXyzPawCenters','allXyzDistPawCenters');

% create plots and save images/figures
h1 = plot1dDistance(allAlignedXyzDistPawCenters);
saveas(h1,fullfile(workingDirectory,'_xyzData','1dDistancePlot'),'png');
saveas(h1,fullfile(workingDirectory,'_xyzData','1dDistancePlot'),'fig');
saveas(h1,fullfile(workingDirectory,'_xyzData',sessionName,'_1dDistancePlot'),'png');
saveas(h1,fullfile(workingDirectory,'_xyzData',sessionName,'_1dDistancePlot'),'fig');

h2 = plot3dDistance(allAlignedXyzPawCenters);
% use view() to rotate and save a couple angles
saveas(h2,fullfile(workingDirectory,'_xyzData','3dDistancePlot'),'png');
saveas(h2,fullfile(workingDirectory,'_xyzData','3dDistancePlot'),'fig');
saveas(h2,fullfile(workingDirectory,'_xyzData',sessionName,'_3dDistancePlot'),'png');
saveas(h2,fullfile(workingDirectory,'_xyzData',sessionName,'_3dDistancePlot'),'fig');
else
disp('The session counts do not match, why not? Fix that and try again');
end
Expand Down
64 changes: 34 additions & 30 deletions analyzeSessions.asv → analyzeTrials.m
@@ -1,59 +1,61 @@
% >> analyzeSessions(150,0.1053,STRUCT(left,center,right))
% >> analyzeTrials(150,0.1053,STRUCT(left,center,right))

% This function requires that a video has already been cropped and pawData has already been saved
% into the sessions folder for each of 3 viewing angles
function analyzeSessions(frameRate,pxToMm,pelletCoords)
% into the trials folder for each of 3 viewing angles
function analyzeTrials(frameRate,pxToMm,pelletCoords)
workingDirectory = uigetdir;
% load all the .mat sessions created for each video, from each angle
leftSessions = dir(fullfile(workingDirectory,'left','sessions','*.mat'));
centerSessions = dir(fullfile(workingDirectory,'center','sessions','*.mat'));
rightSessions = dir(fullfile(workingDirectory,'right','sessions','*.mat'));
workingDirectoryParts = strsplit(workingDirectory,filesep);
sessionName = workingDirectoryParts(end);
% load all the .mat trials created for each video, from each angle
leftTrials = dir(fullfile(workingDirectory,'left','trials','*.mat'));
centerTrials = dir(fullfile(workingDirectory,'center','trials','*.mat'));
rightTrials = dir(fullfile(workingDirectory,'right','trials','*.mat'));

% just make sure there are equal session for each angle
if(numel(leftSessions) == numel(centerSessions) && numel(leftSessions) == numel(rightSessions))
if(numel(leftTrials) == numel(centerTrials) && numel(leftTrials) == numel(rightTrials))
% load the pawCenter variables from the session files
allLeftPawCenters = loadPawCenters(leftSessions,fullfile(workingDirectory,'left','sessions'));
allCenterPawCenters = loadPawCenters(centerSessions,fullfile(workingDirectory,'center','sessions'));
allRightPawCenters = loadPawCenters(rightSessions,fullfile(workingDirectory,'right','sessions'));
allLeftPawCenters = loadPawCenters(leftTrials,fullfile(workingDirectory,'left','trials'));
allCenterPawCenters = loadPawCenters(centerTrials,fullfile(workingDirectory,'center','trials'));
allRightPawCenters = loadPawCenters(rightTrials,fullfile(workingDirectory,'right','trials'));

% combines the paw centers from each video into an nx3 matrix, where n=numberOfFrames in the
% video, and 3=[x y z] data in relation to the pellet
allXyzPawCenters = cell(1,numel(leftSessions)); % number of videos
for i=1:numel(leftSessions)
allXyzPawCenters = cell(1,numel(leftTrials)); % number of videos
for i=1:numel(leftTrials)
allXyzPawCenters{i} = createXyzPawCenters(allLeftPawCenters{i},allCenterPawCenters{i},...
allRightPawCenters{i},pelletCoords);
allXyzPawCenters{i} = allXyzPawCenters{i}.*pxToMm; % convert pixels to distance
end

% takes the [x y z] data just created and calculates a single distance based on all three
% coordinates in relation to the pellet
allXyzDistPawCenters = cell(1,numel(leftSessions));
for i=1:numel(leftSessions)
allXyzDistPawCenters = cell(1,numel(leftTrials));
for i=1:numel(leftTrials)
allXyzDistPawCenters{i} = createXyzDistPawCenters(allXyzPawCenters{i});
end

% aligns data based on distance threshold, ultimately compresses the data set slightly (see
% alignData function for more)
allAlignedXyzPawCenters = cell(1,numel(leftSessions));
allAlignedXyzDistPawCenters = cell(1,numel(leftSessions));
for i=25:numel(leftSessions)
allAlignedXyzPawCenters = cell(1,numel(leftTrials));
allAlignedXyzDistPawCenters = cell(1,numel(leftTrials));
for i=25:numel(leftTrials)
[allAlignedXyzPawCenters{i},allAlignedXyzDistPawCenters{i}] = alignData(allXyzPawCenters{i},allXyzDistPawCenters{i});
end

% save data
mkdir(fullfile(workingDirectory,'_xyzData'));
save(fullfile(workingDirectory,'_xyzData','xyzData'),'allAlignedXyzPawCenters','allAlignedXyzDistPawCenters',...
save(fullfile(workingDirectory,'_xyzData',sessionName,'_xyzData'),'allAlignedXyzPawCenters','allAlignedXyzDistPawCenters',...
'allXyzPawCenters','allXyzDistPawCenters');

% create plots and save images/figures
h1 = plot1dDistance(allAlignedXyzDistPawCenters);
saveas(h1,fullfile(workingDirectory,'_xyzData','1dDistancePlot'),'png');
saveas(h1,fullfile(workingDirectory,'_xyzData','1dDistancePlot'),'fig');
saveas(h1,fullfile(workingDirectory,'_xyzData',sessionName,'_1dDistancePlot'),'png');
saveas(h1,fullfile(workingDirectory,'_xyzData',sessionName,'_1dDistancePlot'),'fig');

h2 = plot3dDistance(allAlignedXyzPawCenters);
% use view() to rotate and save a couple angles
saveas(h2,fullfile(workingDirectory,'_xyzData','3dDistancePlot'),'png');
saveas(h2,fullfile(workingDirectory,'_xyzData','3dDistancePlot'),'fig');
saveas(h2,fullfile(workingDirectory,'_xyzData',sessionName,'_3dDistancePlot'),'png');
saveas(h2,fullfile(workingDirectory,'_xyzData',sessionName,'_3dDistancePlot'),'fig');
else
disp('The session counts do not match, why not? Fix that and try again');
end
Expand Down Expand Up @@ -99,9 +101,10 @@ function analyzeSessions(frameRate,pxToMm,pelletCoords)
end
end

% Creates [x y z] data for a single session/video
% Creates [x y z] data for a single session/video. Not the most elegant way of handling the logic,
% but intended to remain readable and workable.
function xyzPawCenters=createXyzPawCenters(leftPawCenters,centerPawCenters,rightPawCenters,pelletCoords)
% x=C, y=mean(L,R), z=mean(L,C,R), *where L, C, and R are
% x=C, y=mean(L,R), z=mean(L,C,R), *where L, C, and R are not NaN
frameCount = size(leftPawCenters,1);
xyzPawCenters = NaN(frameCount,3);
for i=1:frameCount
Expand All @@ -125,7 +128,7 @@ function analyzeSessions(frameRate,pxToMm,pelletCoords)
else
xyzPawCenters(i,2) = NaN;
end
% calculate z, not the most elegant way, but at least it is readable
% calculate z
if(~isnan(leftPawCenters(i,1)) && ~isnan(centerPawCenters(i,1)) && ~isnan(rightPawCenters(i,1)))
leftDist = pelletCoords.left(2)-leftPawCenters(i,2); % y-axis
centerDist = pelletCoords.center(2)-centerPawCenters(i,2); % y-axis
Expand Down Expand Up @@ -155,10 +158,11 @@ function analyzeSessions(frameRate,pxToMm,pelletCoords)
end
end

function allPawCenters=loadPawCenters(sessions,sessionsPath)
allPawCenters = cell(1,numel(sessions));
for i=1:numel(sessions)
load(fullfile(sessionsPath,sessions(i).name));
% Load and return the pawCenters variable from a session file (one video).
function allPawCenters=loadPawCenters(trials,trialsPath)
allPawCenters = cell(1,numel(trials));
for i=1:numel(trials)
load(fullfile(trialsPath,trials(i).name));
allPawCenters{i} = pawCenters; % "pawCenters" variable is loaded via .mat file
end
end
12 changes: 6 additions & 6 deletions runFolder.m → createTrialData.m
@@ -1,13 +1,13 @@
% pixelBounds is a struct with 3 fields, hsvBounds is an array
function runFolder(pixelBounds,hsvBounds)
function createTrialData(pixelBounds,hsvBounds)
workingDirectory = uigetdir;

% crop videos
% originalVideos = dir(fullfile(workingDirectory,'*.avi'));
% for i=1:size(originalVideos,1)
% videoFile = fullfile(workingDirectory,originalVideos(i).name);
% cropVideo(videoFile,pixelBounds);
% end
originalVideos = dir(fullfile(workingDirectory,'*.avi'));
for i=1:size(originalVideos,1)
videoFile = fullfile(workingDirectory,originalVideos(i).name);
cropVideo(videoFile,pixelBounds);
end

% get data from all crops
fields = fieldnames(pixelBounds);
Expand Down
5 changes: 2 additions & 3 deletions createVideo.m
Expand Up @@ -2,7 +2,6 @@
function [pawCenters,pawHulls] = createVideo(videoFile,hsvBounds)

[pawCenters,pawHulls] = getDataFromVideo(videoFile,hsvBounds);
%pawCenters = cleanCentroids(pawCenters); %cant do this here, only after session save
[pathstr,name,ext] = fileparts(videoFile);

video = VideoReader(videoFile);
Expand Down Expand Up @@ -35,6 +34,6 @@
end

close(newVideo);
mkdir(fullfile(pathstr,'sessions'));
save(fullfile(pathstr,'sessions',name),'pawCenters','pawHulls','hsvBounds');
mkdir(fullfile(pathstr,'trials'));
save(fullfile(pathstr,'trials',name),'pawCenters','pawHulls','hsvBounds');
end
23 changes: 14 additions & 9 deletions getPelletCoords.m
@@ -1,10 +1,15 @@
function coords=getPelletCoords()
[videoName,videoPath] = uigetfile('*.avi');
videoFile = fullfile(videoPath,videoName);
video = VideoReader(videoFile);
imshow(read(video,50));
disp('Identify pellet, press ENTER when done...');
[x,y] = ginput;
close;
coords=[x,y];
function pelletCoords=getPelletCoords(boundNames)
pelletCoords = {};
for i=1:size(boundNames,2)
disp(['Navigate to video for "',boundNames{i},'"...']);
[videoName,videoPath] = uigetfile('*.avi');
videoFile = fullfile(videoPath,videoName);
video = VideoReader(videoFile);
figure;
imshow(read(video,1));
disp('Identify pellet, press ENTER when done...');
[x,y] = ginput;
close;
pelletCoords.(boundNames{i}) = [x,y];
end
end
4 changes: 3 additions & 1 deletion getPixelBounds.m
@@ -1,11 +1,13 @@
% >> pixelBounds = getPixelBounds({'left','center','right'});
function pixelBounds=getPixelBounds(boundNames)
disp('Select one of your videos...');
[videoName,videoPath] = uigetfile('*.avi');
videoFile = fullfile(videoPath,videoName);
video = VideoReader(videoFile);
pixelBounds = {};
im = read(video,1);

figure;

for i=1:size(boundNames,2)
disp(['Create ROI for "',boundNames{i},'"...']);
h_im = imshow(im);
Expand Down
2 changes: 1 addition & 1 deletion px2mm.m → getPx2mm.m
@@ -1,4 +1,4 @@
function pxToMm=px2mm(knownDistMm)
function pxToMm=getPx2mm(knownDistMm)
[videoName,videoPath] = uigetfile('*.avi');
videoFile = fullfile(videoPath,videoName);
video = VideoReader(videoFile);
Expand Down
13 changes: 0 additions & 13 deletions plot1dDistance.asv

This file was deleted.

0 comments on commit 81ec0db

Please sign in to comment.