% extract histogram out of MUSHRA ratings
%
% You need R (see https://www.r-project.org/) for this

%*****************************************************************************
% Copyright (c) 2015      Fiete Winter                                       *
%                         Institut fuer Nachrichtentechnik                   *
%                         Universitaet Rostock                               *
%                         Richard-Wagner-Strasse 31, 18119 Rostock, Germany  *
%                                                                            *
% This file is part of the supplementary material for Fiete Winter's         *
% scientific work and publications                                           *
%                                                                            *
% You can redistribute the material and/or modify it  under the terms of the *
% GNU  General  Public  License as published by the Free Software Foundation *
% , either version 3 of the License,  or (at your option) any later version. *
%                                                                            *
% This Material is distributed in the hope that it will be useful, but       *
% WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY *
% or FITNESS FOR A PARTICULAR PURPOSE.                                       *
% See the GNU General Public License for more details.                       *
%                                                                            *
% You should  have received a copy of the GNU General Public License along   *
% with this program. If not, see <http://www.gnu.org/licenses/>.             *
%                                                                            *
% http://github.com/fietew/publications           fiete.winter@uni-rostock.de*
%*****************************************************************************

[labels, ir_files, stimuli_pos] = coloration_get_labels();

%% Create Table for R's Signifance Analysis
[score, sdx] = coloration_parse_results(1:17);

Nset = size(score,1);  % number of MUSHRA experiments
Ncond = size(score,2);  % number of conditions
Nsub = size(score,3);  % number of subjects

edges = -1:0.1:1;  % edges of bins for histogram

figure(1);
for kdx=1:Nset  
  % box-whisker plot for the data
  subplot(2,3,kdx);
  boxplot(squeeze(score(kdx,:,:)).', sdx(kdx,:,1).');
  ylim([-1,1]);
  title(stimuli_pos{kdx}, 'Interpreter', 'none')
  
  % plotting
  gp_save_table(sprintf( 'results_set%d.txt', kdx), ...
    '', ...
    [], ...
    labels(sdx(kdx,:,1).'), ...
    squeeze(score(kdx,:,:)));
end

%% R Stuff
% Execute R script to compute significance
status = system('Rscript analyse_results.r');

%% Final Data for gnuplot
for kdx=1:Nset  
  
  signi = csvread(sprintf( 'significances_set%d.txt', kdx), 2, 0);
    
  % median,quartiles and signi differences from the reference
  gp_save_table(sprintf( 'stats_%s.txt', stimuli_pos{kdx}), ...
    'Stats', ...
    {'"Med."', '"1st Q"', '"3rd Q."', '"Signi"', '"CI"'}, ...
    labels(sdx(kdx,:,1).'), ...
    median(score(kdx,:,:),3).', ...
    prctile(score(kdx,:,:),25,3).', ...    
    prctile(score(kdx,:,:),75,3).', ...
    signi, ...
    ci(squeeze(score(kdx,:,:)),2));  
end

%% All Data and clean up

%
subjects = arrayfun(@num2str, 1:Nsub, 'UniformOutput', false);

for kdx=1:Nset  
  
  signi = csvread(sprintf( 'significances_set%d.txt', kdx), 2, 0);
    
  % median, quartiles and signi differences from the reference
  gp_save_table(sprintf( 'full_results_%s.txt', stimuli_pos{kdx}), ...
    'Stats followed by individual ratings of subjects', ...
    {'"IRs"', '"Med."', '"1st Q"', '"3rd Q."', '"Signi"', '"CI"', subjects{:} }, ...
    labels(sdx(kdx,:,1).'), ...
    ir_files(sdx(kdx,:,1).'), ...
    median(score(kdx,:,:),3).', ...
    prctile(score(kdx,:,:),25,3).', ...    
    prctile(score(kdx,:,:),75,3).', ...
    signi, ...
    ci(squeeze(score(kdx,:,:)),2), ...
    squeeze(score(kdx,:,:)));  
  
  % clean up
  delete(sprintf( 'significances_set%d.txt', kdx))
  delete(sprintf( 'results_set%d.txt', kdx))
end

