addpath('nrrd_read_write_rensonnet')

%patientID='2SMobr';

%path='/home/studen/temp/dynamicSPECT';

%load ventricle data
%caFile=[ patientID '_Ventricle.mcsv'];
%caFile=fullfile(path,caFile);
%opts = detectImportOptions(caFile,'FileType','text');
%ca=readmatrix(caFile,opts);

[cax,cm]=loadTime(path,patientID);
%w=cay/sum(cay);
%w(1)=1;
%caspline=csaps(cax,cay,p,[],w);

%x=lsqnonlin(biexp,[20 3 10 5.5 0.1],[0,0,0,0,0],[Inf,Inf,Inf, Inf, Inf]);
%chat0=[20, 3, 10, 5.5, 0.1];
%chat0=[10 0.8 10 100];
%lb=zeros(size(chat0));
%ub=Inf*ones(size(chat0));
%chat0=[chat0 cax(1:2)'];
%lb=[lb cax(1:2)'];
%ub=[ub cax(1:2)'];
%chat=lsqcurvefit(@biexp,chat0 ,cax,cay,lb,ub);

[c1 c2]=size(cax);

volumeNamePattern=[ patientID '_Volume'];
s0=[volumeNamePattern '0.nrrd'];
ofile=fullfile(path,patientID,sprintf('%s_Volume0.nrrd',patientID));
headerInfo = nhdr_nrrd_read(ofile,1);
a0=headerInfo.data;
[i1,i2,i3]=size(a0);

data=zeros(i1,i2,i3,c1);

for i=1:c1
    s=sprintf('%s_Volume%d.nrrd',patientID,i-1);
    ofile=fullfile(path,patientID,s);
    hInfo = nhdr_nrrd_read(ofile,1);
    hInfo.data(hInfo.data<0)=0;
    data(:,:,:,i)=hInfo.data/cm(i);
end

%A=reshape(data,[],c1);
%find most common responses
%[centers,U]=fcm(A,5);
%data(ix,iy,iz,:)=A((iz-1)*i1*i2+(iy-1)*i1+ix,:)