% 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)

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

make_labels_avwalk_mered

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

MotionData.data =  Motion;
MotionData.skel = skel;
MotionData.mean = 0;
MotionData.std = 0;
MotionData.batchdata = [];
MotionData.minibatch = [];
MotionData.batchdataindex = [];
MotionData.dropRate=4;
MotionData.batchsize = 100;

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

%Load in the layer 1 model (before pre-processing)
%We will read the order from here
%load snapshots/av_8walk_400hid_300fac_2feat_cd10_6taps_sharefeatfac_ep600.mat
%load snapshots/av_8walk_sean_pp_400hid_300fac_2feat_cd10_12taps_sharefeatfac_ep400
 load WalkGen_VA/Snapshots/av_8walk_sean_opp_400hid_300fac_2feat_cd10_6taps_sharefeatfac_ep900
 
%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


MotionData = Prep_InvarRoot(MotionData);
MotionData = Prep_RemoveEmpty(MotionData,1);

MotionData = Prep_MiniBatch(MotionData,n1);
batchdata = MotionData.batchdata;
minibatch = MotionData.minibatch;


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 = [40,30,30,100,100,100,100,100,1];

lengs = [200,180,180,300,300,300,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]};




% %%% FOR MERED 8 WALK
% seqstarts = [700,700,500,100,10,1000,10,200,200];
% lengs = [600,600,600,800,800,800,600,600,600];
% %%% /FOR MERED 8 WALK

initdata = batchdata;

%how many frames to generate (per sequence)
numframes = 600;

%set up figure for display
close all; 


for style = 1:numstyles
    numframes = lengs(style);
  fprintf('Generating %d frames from style ''%s''\n', ...
    numframes,styledesc{style});
  fr = initframes(style);
  %labels = zeros(numframes,numstyles,'single');
  %labels(:,style) = 1; %set the correct style label to be on
  labels = repmat(labs{style},numframes,1);
  
  %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; axis off
  
%   %smooth with filter
%   %perform filtering in each dimension
%   for jj=1:size(visible,2)
%     visible(:,jj) = filtfilt(bb,aa,visible(:,jj));
%   end

GenLog.visible = visible;
GenLog = Post_AddEmpty(GenLog, MotionData,1);
GenLog = Post_InvarRoot(GenLog, MotionData);
%GenLog = postprocess2(GenLog, MotionData);
% 
newdata_raw = GenLog.postvis;
newseq_s = smooth(GenLog.postvis,3,'moving');
GenLog.postvisfil = reshape(newseq_s,size(GenLog.postvis,1),size(GenLog.postvis,2));
% 
% newdata(:,1:6) = newdata_raw(:,1:6);
   % newdata(:,1:3) = 0;
  subplot(1,2,2);   
  NN{style} = GenLog.postvisfil;
  expPlayData(skel,GenLog.postvis,1/60);
end
