def step03():

  assert isfile( cfg.dtadir+"/train_trn_.tsv.gz" );

  with FeatureDiscretizer( cfg.dtadir+"/fdp.pickle", "w" ) as fdp:
    with FeatureDiscretizer( cfg.dtadir+"/fdq.pickle", "w" ) as fdq:
      with KPCAContinuousFrontend( cfg.dtadir+"/kpcacfe.pickle", "r" ) as kpcacfe:
        with HomebrewContinuousFrontend( cfg.dtadir+"/hbcfe.pickle", "r" ) as hbcfe:
          with CategoricalFrontend( cfg.dtadir+"/cfe.pickle", "r" ) as cfe:
            with BinaryFrontend( cfg.dtadir+"/bfe.pickle", "r" ) as bfe:

              rows = da_read( cfg.dtadir+"/train_trn_.tsv.gz" );

              i = 0;

              for ( id_, y, c, b, x ) in rows:

                i += 1;
                # print( i );
                # if i >= 100:
                #   break;
                
                c = cfe( c );
                b = bfe( b );
                xp = hbcfe( x );
                xq = kpcacfe( x );

                is_enough = [];
                is_enough.append( fdp.train( xp ) );
                is_enough.append( fdq.train( xq ) );

                if all( is_enough ):
                  break;
def step02():

  assert isfile( cfg.dtadir+"/train_trn_.tsv.gz" );

  with KPCAContinuousFrontend( cfg.dtadir+"/kpcacfe.pickle", "w" ) as kpcacfe:
    with HomebrewContinuousFrontend( cfg.dtadir+"/hbcfe.pickle", "w" ) as hbcfe:
      with CategoricalFrontend( cfg.dtadir+"/cfe.pickle", "w" ) as cfe:
        with BinaryFrontend( cfg.dtadir+"/bfe.pickle", "w" ) as bfe:

          rows = da_read( cfg.dtadir+"/train_trn_.tsv.gz" );

          i = 0;

          for ( id_, y, c, b, x ) in rows:

            i += 1;
            # print( i );
            # if i >= 100:
            #   break;

            is_enough = [];
            is_enough.append( bfe.train( b ) );
            is_enough.append( cfe.train( [c] ) );
            is_enough.append( hbcfe.train( x ) );
            is_enough.append( kpcacfe.train( x ) );

            if all( is_enough ):
              break;
def step05( modelf ):

  assert isfile( cfg.dtadir+"/train_trn_.tsv.gz" );

  with FeatureSelector( cfg.dtadir+"/fs.pickle", "r" ) as fs:
    with FeatureDiscretizer( cfg.dtadir+"/fdp.pickle", "r" ) as fdp:
      with FeatureDiscretizer( cfg.dtadir+"/fdq.pickle", "r" ) as fdq:
        with KPCAContinuousFrontend( cfg.dtadir+"/kpcacfe.pickle", "r" ) as kpcacfe:
          with HomebrewContinuousFrontend( cfg.dtadir+"/hbcfe.pickle", "r" ) as hbcfe:
            with CategoricalFrontend( cfg.dtadir+"/cfe.pickle", "r" ) as cfe:
              with BinaryFrontend( cfg.dtadir+"/bfe.pickle", "r" ) as bfe:

                if modelf == "mdlp.kch":

                  mdl_ \
                    = BKNNModel(
                          cfg.dtadir+"/"+modelf, "w",
                          cfe, bfe, hbcfe, fdp, fs, 7
                        );

                elif modelf == "mdlq.kch":

                  fs.bypass_x = True;

                  mdl_ \
                    = BKNNModel(
                          cfg.dtadir+"/"+modelf, "w",
                          cfe, bfe, kpcacfe, fdq, fs, 7
                        );

                with mdl_ as mdl:

                  rows = da_read( cfg.dtadir+"/train_trn_.tsv.gz" );

                  i = 0;

                  for ( id_, y, c, b, x ) in rows:

                    i += 1;
                    #print( i );
                    #if i > 10000:
                    #  break;

                    if mdl.train( ( y, c, b, x ) ):
                      break;