% Version 0.100 (Unsupported, unreleased)
%
% Code provided by Graham Taylor and Geoff Hinton
%
% For more information, see:
%    http://www.cs.toronto.edu/~gwtaylor/publications/icml2009
%
% Permission is granted for anyone to copy, use, modify, or distribute this
% program and accompanying programs and documents for any purpose, provided
% this copyright notice is retained and prominently displayed, along with
% a note saying that the original programs are available from our
% web page.
% The programs and documents are distributed without any warranty, expressed or
% implied.  As the programs were written for research purposes only, they have
% not been tested to the degree that would be advisable in any important
% application.  All use of these programs is entirely at the user's own risk.
%
% Train a factored, conditional RBM with three-way interactions
% On labeled mocap data (10 styles, CMU subject 137)
%
% demonstrates a trained model
% generating different styles based on initialization
% dataset we use is the "clipped" CMU 137 style walks
% Assumes that demo_train.m has already been run and a model has been
% saved to the "snapshots" directory



%initialize RAND,RANDN to a different state
% rand('state',sum(100*clock))
% randn('state',sum(100*clock))
rand('state',0)
randn('state',0)

%Our important Motion routines are in a subdirectory
addpath('./Motion')
addpath('./Scripts')
% addpath('./filter') %filtering stuff from SP toolbox

%filtering parameters
% r = 4; %Factor which to reduce sample rate
% Ord = 8; %Order
% R = .05; %R decibels of peak-to-peak ripple in the passband
% Wn = .8/r; %Cutoff frequency
% [bb,aa] = cheby1(Ord,R,Wn);

%load up the training data & preprocess (for initialization and playback)
%make_balanced_clipped_137
make_labels_av

%downsample to 60fps (make sure model is compatible)
for ii=1:length(Motion)
    Motion{ii}=Motion{ii}(1:2:end,:);
    %remember to downsample labels too
    Labels{ii}=Labels{ii}(1:2:end,:);
    
    OneHotLabels{ii} = zeros(size(Motion{ii},1),length(Motion),'single');
    OneHotLabels{ii}(:,ii)=1;   
end


fprintf(1,'Preprocessing data \n');

%Load in the layer 1 model (before pre-processing)
%We will read the order from here
load snapshots/lma_600hid_200fac_100feat_cd10_12taps_sharefeatfac_ep400.mat

%variable names needed for generation
[numdims, numfac] = size(visfac);
[numhid, junk] = size(hidfac);

n1 = nt; %preprocess2 looks at n1

%Run the 1st stage of pre-processing
%This converts to body-centered coordinates, and converts to ground-plane
%differences
preprocess1

%Run the 2nd stage of pre-processing
%This drops the zero/constant dimensions and builds mini-batches
preprocess2
numdims = size(batchdata,2); %data (visible) dimension

labeldata = [];
onehotdata = [];
for jj=1:length(Labels)
  labeldata = [labeldata; Labels{jj}];
  onehotdata = [onehotdata; OneHotLabels{jj}];
end



%initialization frames for each style
%good init positions (not in flat points) local to each seq
%inits = [1,1,1,1,1,1,1,1,1];

for jj=1:length(Labels)
    inits(jj) = find(onehotdata(:,jj),1);
end

%starting frame of first sequence of styles 1:8
seqstarts = [1,1,10,10,10,10,10,10,1];

lengs = [400,400,400,400,400,400,400,400,400];

initframes = seqstarts;% + inits;

numstyles = size(initframes,2);
styledesc = {'HVHA','NVHA','LVHA','HVLA','NVLA','LVLA','HVNA','LVNA','NVNA'};
labs      = {[3,3],  [2,3], [1,3], [3,1], [2,1], [1,1], [3,2], [1,2], [2,2]};



label_st = labs{3};
label_end = labs{1};

initdata = batchdata;


%set up figure for display
close all; 



    numframes = 800;
  fprintf('Generating %d frames from style ''%s''\n', ...
    numframes,styledesc{style});
  fr = initframes(style);
  
  
  
  
  labels = zeros(numframes,2,'single');
  %labels(:,style) = 1; %set the correct style label to be on
  labels(1:200,:) = repmat(label_st,200,1);
  
  for l=201:800
     labels(l,1) = labels(l-1,1) + 0.0025;
     labels(l,2) = labels(l-1,2);
  end
  
  %labels(601:end,:) = repmat(label_end,200,1);
  
%     for l=201:400
%      labels(l,1) = labels(l-1,1);
%      labels(l,2) = labels(l-1,2) - 0.005;
%   end
  
  %make sure labels are not being set in gen
  gen_sharefeatfac_real;
  
   h = figure(10+style);title(styledesc{style});
p = get(h,'Position'); p(3) = 2*p(3); %double width
set(h,'Position',p);
  %show hidden units
  subplot(1,2,1); imagesc(poshidprobs'); colormap gray;
  
%   %smooth with filter
%   %perform filtering in each dimension
%   for jj=1:size(visible,2)
%     visible(:,jj) = filtfilt(bb,aa,visible(:,jj));
%   end

  %show movie
  postprocess
  
   %newseq_s = smooth(newdata,3,'moving');
    %    newdata = reshape(newseq_s,size(newdata,1),size(newdata,2));
  
    newdata(:,1:3) = 0;
  subplot(1,2,2);   
  NN{style} = newdata;
  
  expPlayData(skel,newdata(2:end-2,:),1/60);

