function [Lt,tch]=run_cclass(mod,ds,nc,ilyr,ft,thr,dnnds,typ)
% ft=2 (test), ft=1 (val), ft=0 (train)
% typ='jout' (PBN), 'out' (AEC), or 'cout' (DPBN)
if iscell(ds),
   nc=length(ds);
   Lt=[];
   tch=[];
   for i=1:nc,
      L=[];
      for j=1:nc,
        fn=[mod '_' ds{j}  '_' ds{i} '_' typ num2str(ft) '.mat'];
        fprintf('loading %s\n',fn);
        p=load(fn);
        if strcmp(typ,'out'),
           L=[L p.c_out(:)];
        else,
           L=[L p.l_out(:)];
        end;
      end;
      tt=zeros(size(L,1),nc); tt(:,i)=1;
      Lt=[Lt; L];
      tch=[tch; tt];
   end;
   if strcmp(typ,'cout') ||  strcmp(typ,'out'),
      lm=min(Lt,[],2);
      Lt = Lt-repmat(lm,1,3);
      Lt=exp(-Lt);
      subplot(121); imagesc(tch)
      subplot(122); imagesc(Lt)
      ne=count_err(Lt',tch');
      fprintf('Errs=%d of %d\n',ne,size(Lt,1))
      return
   end;

   ns=size(Lt,1);
   classname=[mod '_' dnnds];
   fpdnn=[classname '_' dnnds '_out' num2str(ft) '.mat'];
   fprintf('Loading dnn output file %s\n',fpdnn);
   if exist(fpdnn,'file'),
      pdnn=load(fpdnn);
      cmd=['Ldnn=pdnn.h' num2str(ilyr) ';'];
      eval(cmd);
   else,
      Ldnn=0;
   end;
   
   subplot(121)
   [lm,im]=max(Lt,[],2);
   plot(im+randn(size(im))/20,'r.')
   subplot(122)
   as=exp([log(.00001):.1:log(1000)]);na=length(as);
   for ia=1:na,ne(ia)=count_err(Ldnn'+Lt'*as(ia),tch'); end; 
   semilogx(as,ne/ns*100,'ro-');
   text(as(1),ne(1)/ns*100,'FWD')
   text(as(end),ne(end)/ns*100,'PBN')
   fprintf('Min error %d of %d, %f percent\n',min(ne),ns, min(ne)/ns*100)
   
   return
end;
load([ds '.mat'],'tch','tchv','tch2');
%mod='tut6d'; ds='ds818283';
classname=[mod '_' ds];
fpdnn=[classname '_' ds '_out' num2str(ft) '.mat'];
fprintf('Loading dnn output file %s\n',fpdnn);
if exist(fpdnn,'file'),
   pdnn=load(fpdnn);
   cmd=['Ldnn=pdnn.h' num2str(ilyr) ';'];
   eval(cmd);
else,
   Ldnn=0;
end;
L=[];
L2=0;
for i=1:nc,
 fn=[classname '_c',num2str(i) '_' ds '_jout' num2str(ft) '.mat'];
 fprintf('Loading class--dependent PBN  output file %s\n',fn);
 p{i}=load(fn);
 fn=[classname '_c',num2str(i) '_' ds '_out' num2str(ft) '.mat']; 
 fprintf('Loading class--dependent FWD  output file %s\n',fn);
 pb{i}=load(fn);
 cmd=['L2t=pb{i}.h' num2str(ilyr) ';'];
 eval(cmd);
 L2=L2 + L2t;
 L=[L p{i}.l_out(:)+thr(i)];
end;

switch(ft),
  case 0, tch=tch;
  case 1, tch=tchv;
  case 2, tch=tch2;
end;
[lm,im]=max(L,[],2);
ns=size(L,1);
fprintf('PBN errs=%d of %d\n',count_err(L',tch),ns);
subplot(121)
plot(im+randn(size(im))/20,'r.')
subplot(122)
as=exp([log(.00001):.1:log(1000)]);na=length(as);
whos L2 L
for ia=1:na,ne(ia)=count_err(L2'+L'*as(ia),tch); end; 
semilogx(as,ne/ns*100,'ro-');
text(as(1),ne(1)/ns*100,'FWD')
text(as(end),ne(end)/ns*100,'PBN')
fprintf('Min errors %d of %d, %f percent\n',min(ne),ns, min(ne)/ns*100)
hold on
[nm,im]=min(ne);

clf
set(gca,'fontsize',24)
for ia=1:na,ne(ia)=count_err(Ldnn'+(L)'*as(ia),tch); end; 
semilogx(as,ne/ns*100,'b.-');
text(as(1),ne(1)/ns*100,'DNN')
grid on
xlabel('DNN<-- Comb. Factor -->PBN')
ylabel('Error, percent')
hold off 
fprintf('Min errors (comb) %d of %d, %f percent\n',min(ne),ns, min(ne)/ns*100)

