from svc.utils import linrange, linspace

def tune(**env):
    prepareData(env=env)
    train(env=env)
    forcealignTrn(env=env)
    smooth(env=env)
    scale(env=env)
    res = decodeHldt()
    return res['cAcc'], res['uCorr']

params = {
    'DATA_REDUCTION': linrange(10,100,10),
}

params = Grid.cartezianGrid(params)

value, tuned_params = params.tune(tune, logger=logger)

params.writeCSV(os.path.join(env['BUILD_DIR'], 'tune_data_reduction.csv'))

env.update(tuned_params)

# I know that all data are always the best
#all()
moveResults()


SCALE_CONCEPT12_RANGE = +-0.6
SCALE_CONCEPT12_STEP  =   0.2

for i in range(n_iters):
    logger.info("_" * 80)
    logger.info('')
    logger.info("Setting tuning steps:")
    logger.info("=====================")
    logger.info("   SCALE_CONCEPT12_STEP: %.2f" % SCALE_CONCEPT12_STEP)
    logger.info("   SCALE_PUSHPOP_STEP  : %.2f" % SCALE_PUSHPOP_STEP)
    logger.info("_" * 80)
    logger.info('')
    logger.info('')

    params = {
        'SCALE_PUSHPOP': linrange(SCALE_PUSHPOP, SCALE_PUSHPOP_RANGE, SCALE_PUSHPOP_STEP),
        'SCALE_CONCEPT12': linrange(SCALE_CONCEPT12, SCALE_CONCEPT12_RANGE, SCALE_CONCEPT12_STEP),
    }

    params = Grid.cartezianGrid(params)

    value, tuned_params = params.tune(tune_scale, logger=logger)

    if i == 0:
        fn = 'tune_cued_scale.csv'
    else:
        fn = 'tune_cued_scale%d.csv' % (i+1, )
    params.writeCSV(os.path.join(env['BUILD_DIR'], fn))

    SCALE_CONCEPT12 = tuned_params['SCALE_CONCEPT12']
    SCALE_CONCEPT12_RANGE = +-SCALE_CONCEPT12_STEP
settings['DATA_REDUCTION'] = 100


def tune(**env):
    prepareData(env=env)
    train(env=env)
    forcealignTrn(env=env)
    smooth(env=env)
    scale(env=env)
    res = decodeHldt()
    return res['sActAcc'], res['iF']


#    return res['cAcc'], res['uCorr']

params = {
    'TRAIN_DATA_REDUCTION': linrange(10, 100, 10),
}

params = Grid.cartezianGrid(params)

value, tuned_params = params.tune(tune, logger=logger)

params.writeCSV(os.path.join(env['BUILD_DIR'], 'tune_data_reduction.csv'))

env.update(tuned_params)

# I know that all data are always the best
#all()
moveResults()
from svc.utils import linrange, linspace

def tune(**env):
    settings.update(env)
    all(noDcd=True, moveResults=False)
    res = decodeHldt()
    return res['cAcc'], res['uCorr']

if 'test' not in argv:
    params = {
        'TRAIN_DATA_REDUCTION': linrange(5,100,5),
    }
else:
    params = {
        'TRAIN_DATA_REDUCTION': [5, 10],
    }

params = Grid.cartezianGrid(params)

value, tuned_params = params.tune(tune, logger=logger)

params.writeCSV(os.path.join(env['BUILD_DIR'], 'tune_train_data_reduction.csv'))

settings.update(tuned_params)

all(moveResults=False)