%% extra script for unifying approximation
clc
clear all
% close all

%% load prior data
caseNameBase = 'ureaGenericGrowth';
caseList = {'original', 'scaled', 'scaledReduced'};
[result pbeDef] = loadLocalPbeResult(caseNameBase);

%% extend decomposition
% decExt = computeUnifyingDec(pbeDef.dec);
% error('bah')

%% generate projection
generateUnifyingApprox(pbeDef, 3, caseNameBase, caseList)
generateUnifyingApprox(pbeDef, 2, caseNameBase, caseList)




%% evaluate a case
% thisCase = [caseNameBase '_2_PCA_scaledReduced'];
% thisCase = [caseNameBase '_2_PCA_original'];
thisCase = [caseNameBase '_3_uPCA_scaled'];
% thisCase = [caseNameBase '_2_volume_scaled'];

executeLocalPbeCase('ureaGenericGrowth_3_uPCA_scaledManual')
[resultApprox pbeDefApprox] = loadLocalPbeResult(...
    'ureaGenericGrowth_3_uPCA_scaledManual');
dataReference = readResultData(result, pbeDef);
dataApproxDynamic = readResultData(resultApprox, pbeDefApprox);
dataApprox = load(['database' filesep thisCase '.matx'], '-mat');
dataApprox = dataApprox.dataApprox

%% compare to direct projection
hoMatrixReference = dataReference.hcMatrix;
% hoUnifiedVector = computePartitionIndexDec(pbeDef.dec, hoMatrixReference, 1e-3, 1);
heMatrixReference = mapToExtendedEmbedding(hoMatrixReference, ...
    dataApprox.dec.properData);
heMatrix = dataApprox.dec.properData.projectionMatrix * heMatrixReference;
% hcMatrix = mapFromProperEmbedding(heMatrixReference, pbeDefApprox.dec, 1e-3);
% heMatrix = pbeDefApprox.dec.properData.mappingNewToProper * hcMatrix;
% coordinate space
printTimeEvolution(dataReference.timeVector, heMatrix, ...
    heMatrixReference, {'1', '2', '3', '4', '5', '6'})
ylim([-1 1]*1e-3);

% measures
measMatrixReference = computeMeasureDec(pbeDef.dec, 'hC', ...
    hoMatrixReference, {'volume', 'surface', 'meanWidth'}, 1e-4);
measMatrix = computeMeasureDec(dataApprox.dec.properData, 'hC', ...
    heMatrix, {'volume', 'surface', 'meanWidth'}, 1e-4, [], 1);
printTimeEvolution(dataReference.timeVector, ...
    measMatrix, measMatrixReference, ...
    {'volume', 'surface', 'meanWidth'})
% ylim([-0.1 0.1]);
% 

%% compare to time evolution
hcMatrix = dataApproxDynamic.hcMatrix;
heMatrix = dataApprox.dec.properData.mappingNewToProper*hcMatrix;
measMatrix = computeMeasureDec(dataApprox.dec.properData, 'hC', ...
    heMatrix, {'volume', 'surface', 'meanWidth'}, 1e-4, [], 1);
printTimeEvolution(dataReference.timeVector, measMatrix, ...
    measMatrixReference, {'volume', 'surface', 'meanWidth'})
% ylim([-0.1 0.1]);
title('time evolution');

% % load point set
% load(['data/' caseNameBase '_pointSet_scaled'], '-mat');
% heMatrix = mapToExtendedEmbedding(dataPointSet.hcMatrix, decExt);
% dataApprox = approximateDec(heMatrix, decExt, ...
%         'method', 'PCA', ...
%         'targetDim', targetDimension);

    
    
% convert some vector
% hoVector = [0.2 1 1.1]'
% heVector = mapToExtendedEmbedding(hoVector, decExt)
% hVectorHo = decExt.originalData.mappingReducedToFull*hoVector
% hVectorHe = decExt.mappingReducedToFull*heVector
% 
% figure,viewPolytope(struct('A', decExt.originalData.A, 'h', hVectorHo));
% figure,viewPolytope(struct('A', decExt.A, 'h', hVectorHe));
% figure,viewPolytope(struct('A', decExt.A, 'h', decExt.mappingReducedToFull * ...
%     sum(decExt.se', 2)));