load protosessions session
% load outlieronallsessions outlier
% load outlieronallsessionsOutlierRank8 outlier
load outlieronallsessionsOutlierRank20 outlier
sesid=6;
k=3;

z(:,:,1)=outlier(sesid,k).SMTP;        
z(:,:,2)=outlier(sesid,k).SMTP_xnew; 
z(:,:,3)=outlier(sesid,k).SMTP_xnew2;  
z(:,:,4)=outlier(sesid,k).SMTP_xnew3;  
z(:,:,5)=outlier(sesid,k).SMTP_xnew4;  
z(:,:,6)=outlier(sesid,k).SMTP_xnew5;  
z(:,:,7)=outlier(sesid,k).SMTP_xnew6;  
z(:,:,8)=outlier(sesid,k).SMTP_xnew7;  
z(:,:,9)=outlier(sesid,k).SMTP_xnew8;  
z(:,:,10)=outlier(sesid,k).SMTP_xnew9;  
z(:,:,11)=outlier(sesid,k).SMTP_xnew10; 
z(:,:,12)=outlier(sesid,k).SMTP_xnew11; 
z(:,:,13)=outlier(sesid,k).SMTP_xnew12; 
z(:,:,14)=outlier(sesid,k).SMTP_xnew13; 
z(:,:,15)=outlier(sesid,k).SMTP_xnew14; 
z(:,:,16)=outlier(sesid,k).SMTP_xnew15; 
z(:,:,17)=outlier(sesid,k).SMTP_xnew16; 

fig(1,1)=figure('WindowState','maximized');
set(fig(1,1),'PaperPosition',[.25 .25 10.5 8],'PaperOrientation','landscape','PaperSize',[11 8.5])
plot(squeeze(z(1,:,:)),'LineWidth',2)
set(fig(1,1).CurrentAxes,'XGrid','on','YGrid','on','GridAlpha',.3,'FontSize',18);
xlabel('Rank');ylabel('SEP (mg/dL)');title(['Post Outlier Removed LOOP SEP vs PLS Rank: Session ',int2str(sesid),' ',char(session(sesid).refmeternames{k})])

fig(2,1)=figure('WindowState','maximized');
set(fig(2,1),'PaperPosition',[.25 .25 10.5 8],'PaperOrientation','landscape','PaperSize',[11 8.5])
plot(squeeze(z(2,:,:)),'LineWidth',2)
set(fig(2,1).CurrentAxes,'XGrid','on','YGrid','on','GridAlpha',.3,'FontSize',18);
xlabel('Rank');ylabel('MARD (mg/dL)');title(['Post Outlier Removed LOOP MARD vs PLS Rank: Session ',int2str(sesid),' ',char(session(sesid).refmeternames{k})])


fig(3,1)=figure('WindowState','maximized');
set(fig(3,1),'PaperPosition',[.25 .25 10.5 8],'PaperOrientation','landscape','PaperSize',[11 8.5])
plot(squeeze(z(8,:,:)),'LineWidth',2);
set(fig(3,1).CurrentAxes,'XGrid','on','YGrid','on','GridAlpha',.3,'FontSize',18);
xlabel('Rank');ylabel('Clarke A Zone (%)');title(['Post Outlier Removed LOOP Clarke A-Zone vs PLS Rank: Session ',int2str(sesid),' ',char(session(sesid).refmeternames{k})])


%NOTE That max A Zone % occurs as some rank.  Check to see which method(s)
%are responsible

fig(4,1)=figure('WindowState','maximized');
set(fig(4,1),'PaperPosition',[.25 .25 10.5 8],'PaperOrientation','landscape','PaperSize',[11 8.5])
plot(squeeze(z(8,28,:)),'LineWidth',2);
set(fig(4,1).CurrentAxes,'XGrid','on','YGrid','on','GridAlpha',.3,'FontSize',18);
xlabel('Outlier Method Number: x+1, didx');ylabel('Clarke A Zone (%)');title(['Post Outlier Removed LOOP Clarke A-Zone vs Outlier Method #: Session ',int2str(sesid),' ',char(session(sesid).refmeternames{k})])


%look at loop preds for cal with outliers left out

did=outlier(sesid,k).did7;
yref=outlier(sesid,k).loop_xnew7(:,end);
ypre=outlier(sesid,k).loop_xnew7(:,28 );
tim=session(sesid).spectimes;
predwout=[tim,yref,ypre];
prednout=predwout;prednout(~did,:)=nan;

fig(5,1)=figure('WindowState','maximized');
set(fig(5,1),'PaperPosition',[.25 .25 10.5 8],'PaperOrientation','landscape','PaperSize',[11 8.5])
plot((tim-tim(1))*24*60,[ypre,yref],'LineWidth',2)
set(fig(5,1).CurrentAxes,'XGrid','on','YGrid','on','GridAlpha',.3,'FontSize',18);
xlabel('Time (minutes)');ylabel('Glucose (mg/dL)');title(['Post Outlier Removed LOOP vs Time: Outliers in Predictions, Session ',int2str(sesid),' ',char(session(sesid).refmeternames{k})])

fig(6,1)=figure('WindowState','maximized');
set(fig(6,1),'PaperPosition',[.25 .25 10.5 8],'PaperOrientation','landscape','PaperSize',[11 8.5])
plot((tim(did)-tim(1))*24*60,[ypre(did),yref(did)],'LineWidth',2)
set(fig(6,1).CurrentAxes,'XGrid','on','YGrid','on','GridAlpha',.3,'FontSize',18);
xlabel('Time (minutes)');ylabel('Glucose (mg/dL)');title(['Post Outlier Removed LOOP vs Time: Outliers not in Predictions, Session ',int2str(sesid),' ',char(session(sesid).refmeternames{k})])

fig(7,1)=figure('WindowState','maximized');
set(fig(7,1),'PaperPosition',[.25 .25 10.5 8],'PaperOrientation','landscape','PaperSize',[11 8.5])
plot(avp(prednout(:,1)-tim(1),6)*24*60,avp(prednout(:,2:3),6),'LineWidth',2)
set(fig(7,1).CurrentAxes,'XGrid','on','YGrid','on','GridAlpha',.3,'FontSize',18);
xlabel('Time (minutes)');ylabel('Glucose (mg/dL)');title(['Post Outlier Removed LOOP vs Time: Outliers not in Predictions, Avg by Sitting, Session ',int2str(sesid),' ',char(session(sesid).refmeternames{k})])

lims=cat(1,cat(1,fig(5:7,1).CurrentAxes).YLim);
lims=[min(lims(:,1)),max(lims(:,2))];
fig(5,1).CurrentAxes.YLim=lims;
fig(6,1).CurrentAxes.YLim=lims;
fig(7,1).CurrentAxes.YLim=lims;

[tot,per,fig(8,1)]=clarke(avp(prednout(:,2),6),avp(prednout(:,3),6));
fig(8,1).CurrentAxes.Title.String={'Clarke Error Grid: Post Outlier Removed LOOP';['Outliers not in Predictions, Session ',int2str(sesid),' ',char(session(sesid).refmeternames{k})]};

%uncomment next line to generate a pdf with the above plots
% for i=1:size(fig,1);exportgraphics(figure(fig(i).Number), 'OutlierAnalysisfullRankMethod8080124.pdf', 'Append', true);end
% for i=1:size(fig,1);exportgraphics(figure(fig(i).Number), 'OutlierAnalysisRank8Method8080124.pdf', 'Append', true);end
% for i=1:size(fig,1);exportgraphics(figure(fig(i).Number), 'OutlierAnalysisRank20Method8080124.pdf', 'Append', true);end
