We'll be taking GitLab down for maintenance around 22 in the evening on the 15th of September, so this Sunday. Let us know (tt.mpi-cbg.de) if you experience any issues with it after the maintenance period.

Commit 9fb0329e authored by gonciarz's avatar gonciarz

Init from downloaded zip file

parents
% CONVERTSAMPLEFORMAT
% converts the format of training samples from detailed structs to simple
% feature and class label matrices respectively vectors
%
% syntax: [X,Y] = convertSampleFormat(samples,pattern)
%
% inputs: samples struct-array containing training samples as it
% is produced by
% "getSamplesFromSegmentedTracks.m"
% patterns struct containing the corresponding patterns
% definition
%
% output: X Matrix with the features of each sample as one
% row
% Y A vector with class labels of the feature
% vectors. "1" means member.
%
%
% Jo Helmuth, Computational Biophysics Laboratory, ETHZ
%
% last change: March 7, 2007
function [X,Y] = convertSampleFormat(samples,pattern)
props = samples{1}.properties;
nProps = length(props);
nSamples = length(samples);
patternTag = pattern.patternTag;
X = zeros(nSamples,nProps);
Y = zeros(nSamples,1);
for iSample = 1:nSamples
X(iSample,:) = samples{iSample}.properties;
sampleTag = samples{iSample}.patternTag;
if(sampleTag == patternTag)
Y(iSample) = 1;
else
Y(iSample) = 0;
end
end
% DEFINEPATTERNS
% Puts all relevant information for the segmentation of trajectories with
% respect to certain patterns in convenient struct. These structs are
% used by several functions of the toolbox.
%
% Fields:
% trajectoryFilterFctHandle: Empty or a handle to a trajectory filter
% function
% trajactoryFilterFctParams: Empty or a struct containing all parameters
% that the above function needs
% outputFilterFctHandle: A handle to a classification output filter
% function
% outputFilterFctParams: A struct containing all parameters that the
% above function needs
% outputPlotFigureNumber: Empty or a number in which classification
% outputs should be plotted
% propsFctHandle: A handle to a feature extraction function
% labelPostProcFctHandle: Empty or a handle to a function for
% post-processing step labeles
% svmStruct: A struct representing a trained SVM, as
% produced by rainSVM.m
% windowWidth: Length oftrajectory parts from which
% features are computed
% patternTag: A unique identifier for the pattern. Must
% not be zero
% patternName: A string with the name of the pattern
% plotColor: A string defining the plotting style of the
% pattern. Must be a legal argument to "plot"
%
% Jo Helmuth, Computational Biophysics Laboratory, ETHZ
%
% change: May 22, 2007
% last change: August 16, 2007. Made paths platform independent
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% DEFINE PATTERN 1
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% set pattern tag
patternTag = 1;
% set figure number for intermediate plots of output and filtered output.
% Leave the bracket empty for batch runs or if you don't need the plot
outputPlotFigureNumber = [];
% set windowWidth for pattern match evaluation
windowWidth = 10;
% load/define SVM
load(fullfile('..','Data','trainedSVM','svmStruct_DM.mat'));
% set outputFilterFctParams
outputFilterFctParams = struct( ...
'kernelWidth', 10, ...
'threshold', 0.5, ...
'minPatternLength', 10);
% put everything in a fat struct
pattern1 = struct( ...
'trajectoryFilterFctHandle', [], ...
'trajactoryFilterFctParams', [], ...
'outputFilterFctHandle', @outputFilter, ...
'outputFilterFctParams', outputFilterFctParams, ...
'outputPlotFigureNumber', outputPlotFigureNumber, ...
'propsFctHandle', @trackFeaturesTest, ...
'labelPostProcFctHandle', [], ...
'svmStruct', svmStruct_DM, ...
'windowWidth', windowWidth, ...
'patternTag', patternTag, ...
'patternName', 'directed motion', ...
'plotColor', 'r');
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% DEFINE PATTERN 2
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% set pattern tag
patternTag = 2;
% set figure number for intermediate plots of output and filtered output
% Leave the bracket empty for batch runs or if you don't need the plot
outputPlotFigureNumber = [];
% set windowWidth for pattern match evaluation
windowWidth = 10;
% load/define SVM
load(fullfile('..','Data','trainedSVM','svmStruct_Drift.mat'));
% set trajactoryFilterFctParams
trajectoryFilterFctParams = struct(...
'windowWidth', 10);
% set outputFilterFctParams
outputFilterFctParams = struct( ...
'kernelWidth', 10, ...
'threshold', 0.5, ...
'minPatternLength', 10);
% put everything in a fat struct
pattern2 = struct( ...
'trajectoryFilterFctHandle', @trajectoryFilterDrifts, ...
'trajactoryFilterFctParams', trajectoryFilterFctParams, ...
'outputFilterFctHandle', @outputFilter, ...
'outputFilterFctParams', outputFilterFctParams, ...
'outputPlotFigureNumber', outputPlotFigureNumber, ...
'propsFctHandle', @trackFeaturesTest, ...
'labelPostProcFctHandle', [], ...
'svmStruct', svmStruct_Drift, ...
'windowWidth', windowWidth, ...
'patternTag', patternTag, ...
'patternName', 'drifting motion', ...
'plotColor', 'm');
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% PUT IN CELL ARRAY
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
clear Patterns;
Patterns{1} = pattern1;
Patterns{2} = pattern2;
% EXTRACTSEGMENTDATA
% extracts segment data from segmented trajectories.
%
% syntax: [Segments,residuals] = extractSegmentData(Tracks, Patterns)
%
% inputs: Tracks cell array with trajectory data with the field
% labels is set, as done by segmentTrajectories.m
% Patterns cell array with structs containing pattern
% definition
%
% outputs: Segments cell array with a struct array for each pattern
% in Patterns{}, the structs contain data on the
% single segments
% residuals struct array with data on single stretches that
% were not assigned to any of the patterns in
% Patterns{}
%
% Takes the point data, filename etc. from each segment of the trajectories
% in Tracks{} and puts them in a struct array, one for each pattern. The
% structs are put in a cell array for convenience, where Segments{i}
% contains the segments that are patterns of the type defined by
% Patterns{i}. Information on segments that do not belong to any of the
% patterns in Patterns{} are stored in "residuals".
%
% Jo Helmuth, Computational Biophysics Laboratory, ETHZ
%
% last change: September 14, 2006
function [Segments,residuals] = extractSegmentData(Tracks, Patterns);
% number of pre-defined patterns
nPatterns = length(Patterns);
% fill a vector with the tags of the patterns
patternTags = zeros(nPatterns,1);
for iPattern = 1:nPatterns
patternTags(iPattern) = Patterns{iPattern}.patternTag;
end
% get a cell-array for storing the segments
Segments = cell(nPatterns,1);
% provide empty arrays
for iPattern = 1:nPatterns
Segments{iPattern} = [];
end
% pattern counters
segmentsCnt = zeros(nPatterns,1);
% provide empty residuals array
residuals = [];
% residuals counter
residualsCnt = 0;
% number of tracks
nTracks = length(Tracks);
% loop through tracks
for iTrack = 1:nTracks
% get labels
labels = Tracks{iTrack}.labels;
% number of labels in the track
nLabels = length(labels);
% try to find stretches that have the same label and then check if the
% label is known:
% label of the first step:
startStep = 1;
state = labels(startStep);
% this boolean gets true wen one of the two storing conditions is met
store = false;
% loop through labels, allow iLabel to exceed nLabels to make the
% capturing of the case when the last label is reached easier
for iLabel = 2:nLabels + 1
% moved over last step
if(iLabel > nLabels)
% index of last step
endStep = iLabel - 1;
store = true;
elseif(labels(iLabel) ~= state)
% index of last step
endStep = iLabel - 1;
store = true;
end
if(store == true)
% the label has changed or we have reached the last step.
% Store the segment:
% get point positions, note that the last point is the second
% point that defines the last step
x = Tracks{iTrack}.x(startStep:endStep + 1);
y = Tracks{iTrack}.y(startStep:endStep + 1);
points = [x y];
% get the number of the first frame
frameNo = Tracks{iTrack}.frameNos(startStep);
% compute the length of the segment in number of steps
l = endStep - startStep + 1;
% get filename
filename = Tracks{iTrack}.filename;
% get track number
trackNo = Tracks{iTrack}.trackNo;
% finally put everything in a struct
currentSegment = struct('filename',filename,'trackNo',trackNo,...
'iStartStep',startStep,'frameNo',frameNo,'length',l, ...
'points',points);
% find out which pattern number it is
patternNumber = find(patternTags == state);
% if a corresponding tag is found, store it as one of that
% pattern
if(~isempty(patternNumber))
% increment counter
segmentsCnt(patternNumber) = segmentsCnt(patternNumber) + 1;
% store
if(isempty(Segments{patternNumber}))
Segments{patternNumber} = currentSegment;
else
Segments{patternNumber}(segmentsCnt(patternNumber)) = ...
currentSegment;
end
else
% store it as a residual
residualsCnt = residualsCnt + 1;
if(isempty(residuals))
residuals = currentSegment;
else
residuals(residualsCnt) = currentSegment;
end
end
% update state and index of start step fro next segment
state = labels(min(iLabel,nLabels));
startStep = iLabel;
% reset store variable
store = false;
end % store?
end % loop over labels
end % loop over tracks
function labeledFilteredTracks = filterLabeledTracks(Tracks,pattern)
tffh = pattern.trajectoryFilterFctHandle;
nTracks = length(Tracks);
labeledFilteredTracks = cell(size(Tracks));
for iTrack = 1:nTracks
labeledFilteredTracks{iTrack} = tffh('filter',Tracks{iTrack},pattern);
oldlabels = Tracks{iTrack}.labels;
nFilteredSteps = length(labeledFilteredTracks{iTrack}.x)-1;
newlabels = tffh('transformLabelDown',oldlabels,nFilteredSteps,pattern);
labeledFilteredTracks{iTrack}.labels = newlabels;
end
% GETPERFORMANCE
% gets a performance measure of best feature sets of all possible sizes
%
% syntax: [maxPerf,bestDimsCell] = getPerformance(optimFeaturesStruct)
%
% inputs: optimFeaturesStruct A struct containing results from the
% feature set optimization performed by
% optimzeFeatureSet.m
%
%
% output: maxPerf A vector containing the average of sensitivity and
% specificity.The i-th entry holds the value of the
% best set of size i
% bestDimsCell A cell array containing the best feature sets of
% given sizes. The i-th entry contains a i-by-1
% matrix with the indices of the features that
% performed best.
%
%
% Jo Helmuth, Computational Biophysics Laboratory, ETHZ
%
% last change: March 7, 2007
function [maxPerf,bestDimsCell] = getPerformance(optimFeaturesStruct)
allDims = optimFeaturesStruct.allDims;
SensSpecAverage = optimFeaturesStruct.SensSpecAverage;
l_max = 1;
maxPerf = zeros(100,1);
for i = 1:length(allDims)
l = length(allDims{i});
l_max = max(l,l_max);
perf = SensSpecAverage(i,3);
if(perf > maxPerf(l))
maxPerf(l) = perf;
bestDimsCell{l} = allDims{i};
end
end
maxPerf = maxPerf(1:l_max);
\ No newline at end of file
% GETSAMPLESFROMSEGMENTEDTRACKS
% extracts all possible samples from a segmented track and places the
% sample information in a strcut
%
% syntax: allSamples = ...
% getSamplesFromSegmentedTrack(segmentedTracks,pattern)
%
% inputs: segmentedTracks cell array containing structs with trajectory
% data, as produced by "manuallySegmentTracks.m"
% patterns struct containing a pattern definition
%
% output: allSamples A cell array with structs containing sample
% information, such as point position data and
% features.
%
% Jo Helmuth, Computational Biophysics Laboratory, ETHZ
%
% last change: March 7, 2006
function allSamples = getSamplesFromSegmentedTrack(segmentedTracks,pattern)
nTracks = length(segmentedTracks);
allSamples = [];
featureFctHandle = pattern.propsFctHandle;
for iTrack = 1:nTracks
segmentedTrack = segmentedTracks{iTrack};
% check out the window width and move the window over the track, pick
% every sample that is possible, decide which type it is
% window width
w = pattern.windowWidth(1); % in steps
x = segmentedTrack.x; % N_points values
y = segmentedTrack.y;
labels = segmentedTrack.labels; % N_points - 1 values
nSteps = length(x)-1;
nWindows = nSteps - w + 1;
samples = cell(nWindows,1);
patternTag = pattern.patternTag;
patternName = pattern.patternName;
properties = featureFctHandle(x,y,w);
for iSample = 1:nWindows
xs = x(iSample:iSample+w);
ys = y(iSample:iSample+w);
% if any of the steps step is not the pattern, we consider the sample a RW
label = labels(iSample:iSample+w-1);
if(isempty(find(label~=patternTag)))
label = patternTag;
else
label = 0;
end
if(label==patternTag)
tag = patternTag;
name = patternName;
else
tag = 0;
name = 'RW';
end
sample = struct('x',xs,'y',ys,'patternTag',tag,'name',name,'properties',properties(iSample,:));
samples{iSample} = sample;
end
allSamples = joinSets(allSamples,samples);
end
function t = joinSets(t1,t2)
n1 = length(t1);
n2 = length(t2);
t = cell(n1+n2,1);
for i = 1:n1
t{i} = t1{i};
end
for i = 1:n2
t{i+n1} = t2{i};
end
\ No newline at end of file
% LOADDATA
% loads data from text input file
%
% syntax: data = loadData('filename',ncol, maxTrackLength)
%
% inputs: filename name of the text file to be read
% ncol number of columns in file
% minlen minimum length of a trajectory
% maxTrackLength max length of track in file
%
% output: data data in the format described below.
%
% loads the trajectory x,y-data in file 'filename' and returns a list
% of arrays data{} where data{i} is the i-th trajectory of the data
% set. data{i}(:,1) is the frame number, data{i}(:,2) is the x position
% and data{i}(:,3) the y position. data{i}(:,4) is the 0 order intensity
% moment, data{i}(:,5) the 2nd order intensity moment and data{i}(:,6)
% the non-particle discrimination score.
%
%
%
% Ivo Sbalzarini, July 29, 2003
%
% Last change, September 13, 2006
function data = loadData(filename,ncol,minlen,maxTrackLength)
maxNumTracks = 100000;
% initialize empty arrays
% provide a massive cell array to avoid dynamic creation
data = cell(1,maxNumTracks);
% check parameters
% if or(nargin<3, nargin>3),
% disp('ERROR: wrong number of input arguments. See "help loadData" !');
% return;
% end;
if(nargin == 2)
maxTrackLength = 5000;
end
if ncol < 1,
disp('loadData: ncol must be at least 1. Reset to 1.')
end;
% open file
infile = fopen(filename,'rt');
if infile == -1,
disp('Cannot open data file');
return;
end;
fmt = '';
for i=1:ncol,
fmt = [fmt,' %f'];
end
% read file line by line
k = 0; % initialize trajectory counter
% inserted by Jo
% allocate space for data block, assume the maximum length of trajectory is
% known, otherwise use default value of 5000
block = zeros(maxTrackLength,ncol);
lineCnt = 0;
while 1
% get line
line = fgetl(infile);
if ~is_comment(line),
% if line is shorter than 4 characters, consider it empty
if length(line) < 4,
if (lineCnt > minlen),
k = k + 1;
% store valid data for that track
data{k} = block(1:lineCnt,1:ncol);
end
lineCnt = 0;
else % if we are still in the same block
% decode line
a = sscanf(line,fmt);
% append line content
lineCnt = lineCnt + 1;
block(lineCnt,1:ncol) = a(1:ncol)';
end;
end;
% if end of file is reached, store last data and exit loop
if feof(infile),
if (lineCnt > minlen)
k = k + 1;
% store valid track data
data{k} = block(1:lineCnt,1:ncol);
end
break;
end;
end;
% close file
fclose(infile);
% allocate space for k cells
data2 = cell(1,k);
% copy valid data
for ik = 1:k
data2{ik} = data{ik};
end
data = data2;
disp(sprintf('%d trajectories read from file %s',length(data),filename))
return;
%
% checks if a line is a coment or not
%
function ic = is_comment(line)
ic = 0;
if (length(line) < 1),
return;
end;
pos = 1;
while and(pos<=length(line),ic == 0),
if line(pos) == '%',
ic = 1;
end;
if double(line(pos)) > 32,
break;
end;
pos = pos+1;
end;
return;
% LOADTRACKS
% loads trajectory data from multiple files and puts it in a convinient
% struct
%
% syntax: Tracks = loadTracks('inputfilename','datapathname',minlen,varargin{})
%
% inputs: inputfilename name of the text file containing the names of
% the trajectory files
% datapathname name of the path where the trajectory files are
% located
% minlen minimum length of trajectories to read.
% varargin{1} optional argument to specify the number of
% columns to be read from the trajectory files.
% Default is 6.
% varargin{2} optional argument to specify the maximum
% expected length of trajectories in the files.
% Default is 5000.
%
% output: Tracks data in the format described below.
%
% loads the x,y-data, the frame number of the point detections and optional
% columns of data in the files specified in the input file 'inputfilename'.
% The function returns a cell array Tracks{} whos elements are structs, one
% per trajectory. The fields of the structs are:
%
% 'filename': filename of the track
% 'trackNo': number of the track in 'filename'
% 'x': vector of x-coordinates
% 'y': vector of y-coordinates
% 'frameNos': vector with frame number of x,y-position
% 'labels': vector for step labels, still empty
% 'unnamedData':array containing data from additional columns in
% trajectory files
% 'validity': string for storing the trajectory validity
%