Skip to content

Commit

Permalink
nbody -> nindiv
Browse files Browse the repository at this point in the history
  • Loading branch information
Maljoras committed Aug 11, 2016
1 parent 429e67c commit be414ea
Show file tree
Hide file tree
Showing 45 changed files with 281 additions and 281 deletions.
44 changes: 22 additions & 22 deletions +xy/+core/@BatchClassifier/BatchClassifier.m
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
properties
npca = 15;
nlfd = 0;
nbody = 2;
nindiv = 2;
minBatchN = 50; % minimal number of features for updateing the batch
noveltyThres = 0.10;
regularizer = 1e-10;
Expand Down Expand Up @@ -32,7 +32,7 @@
function obj = loadobj(S);
if isstruct(S)

obj = xy.core.BatchClassifier(S.nbody,S.featdim);
obj = xy.core.BatchClassifier(S.nindiv,S.featdim);
for f = fieldnames(S)'
if isprop(obj,f{1})
obj.(f{1}) = S.(f{1});
Expand All @@ -55,7 +55,7 @@ function initialize(self,batchsample)
% CAUTION: also computes the pca from it....


if ~iscell(batchsample) || length(batchsample)~=self.nbody || ...
if ~iscell(batchsample) || length(batchsample)~=self.nindiv || ...
all(cellfun('isempty',batchsample))
xy.helper.verbose('WARNING: provide a batch for each fish!');
keyboard
Expand All @@ -77,9 +77,9 @@ function initialize(self,batchsample)
if self.npca>0 || self.nlfd>0
Z = cat(1,batchsample{:});
% do some weak oulyier detection
[pc,a] = xy.helper.emclustering(Z,self.nbody+2);
[pc,a] = xy.helper.emclustering(Z,self.nindiv+2);
[~,cl] = max(pc,[],2);
idx = find(a<self.noveltyThres/self.nbody);
idx = find(a<self.noveltyThres/self.nindiv);
goodidx = ~ismember(cl,idx);

cl = cellfun(@(x,k)k*ones(size(x,1),1),batchsample,num2cell(1:length(batchsample)),'uni',0);
Expand All @@ -102,19 +102,19 @@ function initialize(self,batchsample)

% initialize
self.d = d;
self.n = zeros(self.nbody,1);
self.mu = zeros(self.nbody,d);
self.Sigma = repmat(eye(d),[1,1,self.nbody]);
self.invSigma = repmat(eye(d),[1,1,self.nbody]); % dummy
self.norm = ones(self.nbody,1);
self.n = zeros(self.nindiv,1);
self.mu = zeros(self.nindiv,d);
self.Sigma = repmat(eye(d),[1,1,self.nindiv]);
self.invSigma = repmat(eye(d),[1,1,self.nindiv]); % dummy
self.norm = ones(self.nindiv,1);

% convert the batchsample
l = cellfun('isempty',batchsample);
X = self.convertBatch(batchsample(~l));

% initial setting
s = 0;
for i = 1:self.nbody
for i = 1:self.nindiv
if ~l(i)
s = s+1;
assert(d==size(X{s},2));
Expand All @@ -132,9 +132,9 @@ function initialize(self,batchsample)
function [classprob] = predictWithoutPreProcessing(self,X)

b = size(X,1);
classprob = zeros(b,self.nbody);
classprob = zeros(b,self.nindiv);

for j = 1:self.nbody
for j = 1:self.nindiv
if self.n(j)
xm = bsxfun(@minus,X,self.mu(j,:));
xms = xm*self.invSigma(:,:,j);
Expand Down Expand Up @@ -182,7 +182,7 @@ function init(self,batchsample)
self.initialize(batchsample)
end

function self = BatchClassifier(nbody,dim,varargin) % constructor
function self = BatchClassifier(nindiv,dim,varargin) % constructor

self = self@handle();

Expand All @@ -198,7 +198,7 @@ function init(self,batchsample)
end
end

self.nbody = nbody;
self.nindiv = nindiv;
self.featdim = dim;
end

Expand Down Expand Up @@ -256,7 +256,7 @@ function init(self,batchsample)
prob = nan(size(fishidx));
l = cellfun(@(x)(size(x,1)),batchsample);
if ~self.isInit() % not yet initialized
if all(l>=minBatchN) && length(unique(fishidx))==self.nbody
if all(l>=minBatchN) && length(unique(fishidx))==self.nindiv
self.initialize(batchsample);
assignedIdentityIdx = fishidx;
end
Expand Down Expand Up @@ -330,7 +330,7 @@ function init(self,batchsample)
end


cost = zeros(self.nbody,self.nbody);
cost = zeros(self.nindiv,self.nindiv);
s = 0;
validclasses = true(size(assumedClassIdx));
for i = assumedClassIdx
Expand Down Expand Up @@ -383,9 +383,9 @@ function init(self,batchsample)

function mu = getMeans(self);
% returns the means of the classes in "readable" format
mu = zeros([self.nbody,self.featdim]);
mu = zeros([self.nindiv,self.featdim]);

for i = 1:self.nbody
for i = 1:self.nindiv
if self.nlfd
x = self.lfdpc*self.mu(i,:)';
else
Expand Down Expand Up @@ -413,10 +413,10 @@ function plotMeans(self)
end

clf;
[r1,r2] = xy.helper.getsubplotnumber(self.nbody+(self.nbody>1));
[r1,r2] = xy.helper.getsubplotnumber(self.nindiv+(self.nindiv>1));
mu = self.getMeans();

for i = 1:self.nbody
for i = 1:self.nindiv
a = subplot(r1,r2,i,'align');

imagesc(squeeze(mu(i,:,:)));
Expand All @@ -428,7 +428,7 @@ function plotMeans(self)
end

% difference of the last 2 fish
if self.nbody>1
if self.nindiv>1
a = subplot(r1,r2,i+1,'align');
z = squeeze(mu(end-1,:,:) - mu(end,:,:));
imagesc(z);
Expand Down
4 changes: 2 additions & 2 deletions +xy/+core/@ClassProbHistory/ClassProbHistory.m
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@

methods

function self = ClassProbHistory(nbody, varargin)
function self = ClassProbHistory(nindiv, varargin)
% constructor
self = self@handle();

Expand All @@ -54,7 +54,7 @@
end
end

self.buffer = nan(self.nHistory,nbody);
self.buffer = nan(self.nHistory,nindiv);
self.weightbuffer = nan(self.nHistory,1);
self.currentIdx = 0;
self.age = 0;
Expand Down
70 changes: 35 additions & 35 deletions +xy/+core/@DAGraph/DAGraph.m
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
% distances) on the way and finds the shortest and likeliest
% (according to the classprobs of individual fish).
%
% update is fast and linear in N = ndet*nhyp*nbody. per time step.
% update is fast and linear in N = ndet*nhyp*nindiv. per time step.
% Backtracing to find the tracks needs a single through the past
% time steps for each xy.
%
Expand Down Expand Up @@ -36,7 +36,7 @@


properties (SetAccess=private,GetAccess=private)
nbody = [];
nindiv = [];
frameCounter = 0;
dagPos = [];
dagIdx = [];
Expand All @@ -49,7 +49,7 @@
methods(Static);
function obj = loadobj(S);
if isstruct(S)
obj = xy.core.DAGraph(S.nbody,S.nhyp);
obj = xy.core.DAGraph(S.nindiv,S.nhyp);
for f = fieldnames(S)'
if isprop(obj,f{1})
obj.(f{1}) = S.(f{1});
Expand Down Expand Up @@ -95,25 +95,25 @@ function setOpts(self,opts)
background = 0.1;
foreground = 0.12;

nbody = self.nbody;
nindiv = self.nindiv;
nobj = self.nhyp;
assert(self.dim==2,'Test only available for dim==2');

cutoff = velocity*dt;
[Hb,Ha] = butter(4,cutoff,'low');
trace = filtfilt(Hb,Ha,randn(nt,2,nbody));
trace = filtfilt(Hb,Ha,randn(nt,2,nindiv));
trace = trace/std(trace(:));

% noisy observations of the tracks
otrace = trace + randn(size(trace))*sig;

% add some noise detections
if nbody<nobj
nadd = nobj-nbody;
if nindiv<nobj
nadd = nobj-nindiv;
pos =randn(nt,self.dim,nadd);

otrace = cat(3,otrace, pos);
for i = nbody+1:nobj
for i = nindiv+1:nobj
ridx = find(rand(nt,1)>pfalsealarm);
otrace(ridx,:,i) = NaN;
end
Expand All @@ -127,9 +127,9 @@ function setOpts(self,opts)
end


br = background*rand(nt,nbody,nobj);
br = background*rand(nt,nindiv,nobj);
classprob = br;
for i = 1:nbody
for i = 1:nindiv
classprob(:,i,i) = classprob(:,i,i) + foreground*rand(nt,1,1);
end
for j = 1:nobj
Expand All @@ -139,7 +139,7 @@ function setOpts(self,opts)

% distance for tracking
dsig = sig;
col = parula(nbody+1);
col = parula(nindiv+1);
sym = 'xos<>^vp';

% permute the positions (obj index is random)
Expand All @@ -150,14 +150,14 @@ function setOpts(self,opts)
repmat(ridx,[1,dim,1]));
rotrace = otrace(idx);

idx = sub2ind(size(classprob),repmat((1:nt)',[1,nbody,nobj]),...
repmat(1:nbody,[nt,1,nobj]),...
repmat(ridx,[1,nbody,1]));
idx = sub2ind(size(classprob),repmat((1:nt)',[1,nindiv,nobj]),...
repmat(1:nindiv,[nt,1,nobj]),...
repmat(ridx,[1,nindiv,1]));
rclassprob = classprob(idx);


clf;
for i = 1:nbody
for i = 1:nindiv
plot(squeeze(trace(:,1,i)),squeeze(trace(:,2,i)),'color',col(i,:));
hold on;
end
Expand Down Expand Up @@ -198,7 +198,7 @@ function setOpts(self,opts)
end

self.checkOverlap();
self.plotTraces(1:self.nbody,1:self.nbody);
self.plotTraces(1:self.nindiv,1:self.nindiv);

end

Expand All @@ -224,7 +224,7 @@ function setOpts(self,opts)
% NOTE: if ifish and ihyp have the same length ifish(i),
% ihyp(i) is plotted instead

col = parula(self.nbody+1);
col = parula(self.nindiv+1);
sym = 'xos<>^vp';

if nargin<4
Expand Down Expand Up @@ -254,18 +254,18 @@ function setOpts(self,opts)
if nargin<2 || isempty(objidx)
[rtrace,objidx] = self.backtrace();
end
objidx = reshape(objidx,[],self.nbody,self.nhyp);
objidx = reshape(objidx,[],self.nindiv,self.nhyp);

if nargin<3
verbosity = 2;
end

% check for potential overlaps
n = self.nbody;
n = self.nindiv;
eqmsk = zeros([size(objidx,1),n*(n-1)/2]);
s = 0;allL = [];
for i = 1:self.nbody
for j = i+1:self.nbody
for i = 1:self.nindiv
for j = i+1:self.nindiv
s = s+1;
eqmsk(:,s) = (objidx(:,i,i) == objidx(:,j,j));
if sum(eqmsk(:,s))>1
Expand Down Expand Up @@ -321,7 +321,7 @@ function setOpts(self,opts)
end

if nargin<2 || isempty(ifish)
[ifish,mobj] = ndgrid(1:self.nbody,1:self.nhyp);
[ifish,mobj] = ndgrid(1:self.nindiv,1:self.nhyp);
ifish = ifish(:)';
mobj = mobj(:)';
end
Expand Down Expand Up @@ -367,20 +367,20 @@ function setOpts(self,opts)
function reset(self,initPos,tframe);
% init pos has to be in fishID order...

self.dagI = self.startPenalty*ones(self.nbody,self.nhyp);
self.dagI(find(eye(self.nbody))) = 0;
self.dagI = self.startPenalty*ones(self.nindiv,self.nhyp);
self.dagI(find(eye(self.nindiv))) = 0;

self.dagIdx = nan(self.nbody,self.nhyp,self.memoryBlock);
self.dagIdx = nan(self.nindiv,self.nhyp,self.memoryBlock);
self.dagPos = nan(self.dim,self.nhyp,self.memoryBlock);

if self.saveDagIif
self.dagIsave = nan(self.nbody,self.nhyp,self.memoryBlock);
self.dagIsave = nan(self.nindiv,self.nhyp,self.memoryBlock);
self.dagIsave(:,:,1) = self.dagI;
end

self.frameCounter = 1; % next step if 2

for i = 1:self.nbody
for i = 1:self.nindiv
self.dagIdx(i,:,1) = i;
end

Expand Down Expand Up @@ -433,7 +433,7 @@ function update(self,classProb,detections,costOfNonAssignment)
% Directed Acyclic Graph minimal path problem in an online manner
% (Nodes are in topological order) based on distance and class
% probs. DETECTIONS are the positions/features of the detections
% in format NDIM x NDETECT. CLASSPROB is NBODY x NDETECT
% in format NDIM x NDETECT. CLASSPROB is NINDIV x NDETECT


dist = xy.helper.pdist2Euclidean(self.dagPos(:,:,self.frameCounter)',detections');
Expand All @@ -456,7 +456,7 @@ function update(self,classProb,detections,costOfNonAssignment)
% computes the Directed Acyclic Graph minimal path problem in
% an online manner (Nodes are in topological order). SFCOST is
% cost matrix of size NHYP x NDETECT
% DETECTIONS shoue be NDIM x NDETECT. CLASSPROB is NBODY x
% DETECTIONS shoue be NDIM x NDETECT. CLASSPROB is NINDIV x
% NDETECT

ndetect = size(sfcost,2);
Expand All @@ -469,7 +469,7 @@ function update(self,classProb,detections,costOfNonAssignment)
elseif ndetect<self.nhyp
dist = costOfNonAssignment*ones(self.nhyp,self.nhyp);
dist(:,1:ndetect) = sfcost;
cl = zeros(self.nbody,self.nhyp);
cl = zeros(self.nindiv,self.nhyp);
if ~isempty(classProb)
cl(:,1:ndetect) = classProb;
end
Expand Down Expand Up @@ -499,15 +499,15 @@ function update(self,classProb,detections,costOfNonAssignment)
self.frameCounter = self.frameCounter + 1;

if size(self.dagIdx,3)<self.frameCounter
self.dagIdx = cat(3,self.dagIdx,nan(self.nbody,self.nhyp,self.memoryBlock));
self.dagIdx = cat(3,self.dagIdx,nan(self.nindiv,self.nhyp,self.memoryBlock));

self.dagPos = cat(3,self.dagPos,nan(2,self.nhyp,self.memoryBlock));

% to avoid overflow
self.dagI = self.dagI - min(self.dagI(:));

if self.saveDagIif
self.dagIsave = cat(3,self.dagIsave,nan(self.nbody,self.nhyp,self.memoryBlock));
self.dagIsave = cat(3,self.dagIsave,nan(self.nindiv,self.nhyp,self.memoryBlock));
end

end
Expand All @@ -529,10 +529,10 @@ function update(self,classProb,detections,costOfNonAssignment)
end
end

function self = DAGraph(nbody,nhyp,opts)
% self = DAGraph(nbody,nhyp)
function self = DAGraph(nindiv,nhyp,opts)
% self = DAGraph(nindiv,nhyp)
self.nhyp = nhyp;
self.nbody = nbody;
self.nindiv = nindiv;

if nargin>2
self.setOpts(opts);
Expand Down

0 comments on commit be414ea

Please sign in to comment.