%MainDBN: Do all the steps of the DBN learning program

% APRENDIZAJE AUTOMÁTICO (G. Pajares y J. M. de la Cruz, Eds.)
% Editorial: RA-MA
% Capítulo 6: Redes Bayesianas
% Autores: Diane Oyen, Eva Besada-Portas

clear;close all;

% Load data
dataorig_sub2run1 = importdata('dataSub2run1.csv'); %The second is our second
dataorig_sub2run2 = importdata('dataSub2run2.csv');
dataorig_sub1run1 = importdata('dataSub3run1.csv'); %The third is our first
dataorig_sub1run2 = importdata('dataSub3run2.csv');

[T N] = size(dataorig_sub2run1);

%Labels: we use the real names or the numbers depending on the situation
namesorig = importdata('roiNames.txt'); 
for i=1:9
    namesnum{i}=sprintf(' %d',i);
end
for i=10:N
    namesnum{i}=sprintf('%d',i);
end

% Quantize data
numBins = 4; %Number of bins used to cuatizy the data
% Find the min and max for each timeseries, then linearly space boundaries
datadis_sub1run1 = quantize(dataorig_sub1run1, numBins);
datadis_sub1run2 = quantize(dataorig_sub1run2, numBins);
datadis_sub2run1 = quantize(dataorig_sub2run1, numBins);
datadis_sub2run2 = quantize(dataorig_sub2run2, numBins);
nodesizes = numBins*ones(1,N); %Number of bins of each variable

% Learn DBN for one subject on one movie
max_fan_in = 2; %Maximum number of parents

inter_sub1run1 = learn_dbn_greedy(datadis_sub1run1, nodesizes, max_fan_in);
h=figure;  set(h,'Name','RBD del Sujeto 1','Number','off');
subplot(2,1,1);
draw_dbn(inter_sub1run1,namesnum);
title('Sujeto 1 Pelicula 1');
disp('Families for Subject 2, Movie 1');
display_families(inter_sub1run1, namesorig);

% Learn other DBNs
inter_sub1run2 = learn_dbn_greedy(datadis_sub1run2, nodesizes, max_fan_in);
subplot(2,1,2);
draw_dbn(inter_sub1run2,namesnum);
title('Sujeto 1 Pelicula 2');
disp('Families for Subject 2, Movie 2');
display_families(inter_sub1run2, namesorig);

inter_sub2run1 = learn_dbn_greedy(datadis_sub2run1, nodesizes, max_fan_in);
h=figure;  set(h,'Name','RBD del Sujeto 2','Number','off');
subplot(2,1,1);
draw_dbn(inter_sub2run1,namesnum);
title('Sujeto 2 Pelicula 1');
disp('Families for Subject 3, Movie 1');
display_families(inter_sub2run1, namesorig);

inter_sub2run2 = learn_dbn_greedy(datadis_sub2run2, nodesizes, max_fan_in);
subplot(2,1,2);
draw_dbn(inter_sub2run2,namesnum);
title('Sujeto 2 Pelicula 2');
disp('Families for Subject 3, Movie 2');
display_families(inter_sub2run2, namesorig);


% Look at CPTs
% best to limit max_fan_in to 2
dbns = {inter_sub1run1, inter_sub1run2,inter_sub2run1,inter_sub2run2};
data = {datadis_sub1run1, datadis_sub1run2, datadis_sub2run1, datadis_sub2run2};
namesexp={'Sujeto 1 Pelicula 1','Sujeto 1 Pelicula 2','Sujeto 2 Pelicula 1','Sujeto 2 Pelicula 2'}
CPT = cell(length(dbns), N);
for i = 1:length(dbns)
  for child = 1:N
    CPT{i,child} = learn_cpt(dbns{i}, child, data{i}, nodesizes);
    familyNames = namesnum([child; find(dbns{i}(:,child))]);
    
    l=rem(child-1,4)+1;
    if l==1
        h=figure;
        set(h,'Name',namesexp{i},'Number','off');
    end
    draw_cpt(CPT{i,child}, familyNames,l);
    colormap('gray');
    %colormap('autumn');
    %hgsave(strcat('dbn', int2str(i), 'CPT', namesorig{child}, '.fig'));
  end
end

% Check each learned structure against other datasets
score = zeros(length(data), length(dbns));
for i=1:length(data)
  for j=1:length(dbns)
    score(j,i) = calc_dbn_score(data{i}, dbns{j}, nodesizes, CPT(j,:));
  end
end
score
