# Path: treecat/validate.py
# def split_data(ragged_index, num_rows, num_parts, partid):
# def read_param_csv(param_csv_path, **options):
# def train_task(dataset_path, model_path, config_str):
# def process_train_task(task):
# def train(dataset_path, param_csv_path, models_dir, **options):
# def process_eval_task(task):
# def eval(dataset_path, param_csv_path, models_dir, result_path, **options):
#     N = num_rows
#     V = ragged_index.shape[0] - 1
#     R = ragged_index[-1]
#     N, R = data.shape
#     V = len(ragged_index) - 1
# 
# Path: treecat/generate.py
# def generate_dataset_file(num_rows, num_cols, num_cats=4, rate=1.0):
#     """Generate a random dataset.
# 
#     Returns:
#         The path to a gzipped pickled data table.
#     """
#     path = os.path.join(DATA, '{}-{}-{}-{:0.1f}.dataset.pkz'.format(
#         num_rows, num_cols, num_cats, rate))
#     if os.path.exists(path):
#         return path
#     print('Generating {}'.format(path))
#     if not os.path.exists(DATA):
#         os.makedirs(DATA)
#     dataset = generate_dataset(num_rows, num_cols, num_cats, rate)
#     pickle_dump(dataset, path)
#     return path
# 
# Path: treecat/testutil.py
# TESTDATA = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'testdata')
# 
# Path: treecat/testutil.py
# @contextlib.contextmanager
# def tempdir():
#     dirname = tempfile.mkdtemp()
#     try:
#         yield dirname
#     finally:
#         shutil.rmtree(dirname)


import os
from treecat import validate
from treecat.generate import generate_dataset_file
from treecat.testutil import TESTDATA
from treecat.testutil import tempdir
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function



PARAM_CSV = os.path.join(TESTDATA, 'tuning.csv')


def test_train_eval():
    dataset_path = generate_dataset_file(5, 7)
    with tempdir() as dirname:
        models_dir = os.path.join(dirname, 'models')
        result_path = os.path.join(dirname, 'tune_clusters.pkz')
        validate.train(dataset_path, PARAM_CSV, models_dir)

# Path: treecat/validate.py
# def split_data(ragged_index, num_rows, num_parts, partid):
# def read_param_csv(param_csv_path, **options):
# def train_task(dataset_path, model_path, config_str):
# def process_train_task(task):
# def train(dataset_path, param_csv_path, models_dir, **options):
# def process_eval_task(task):
# def eval(dataset_path, param_csv_path, models_dir, result_path, **options):
#     N = num_rows
#     V = ragged_index.shape[0] - 1
#     R = ragged_index[-1]
#     N, R = data.shape
#     V = len(ragged_index) - 1
# 
# Path: treecat/generate.py
# def generate_dataset_file(num_rows, num_cols, num_cats=4, rate=1.0):
#     """Generate a random dataset.
# 
#     Returns:
#         The path to a gzipped pickled data table.
#     """
#     path = os.path.join(DATA, '{}-{}-{}-{:0.1f}.dataset.pkz'.format(
#         num_rows, num_cols, num_cats, rate))
#     if os.path.exists(path):
#         return path
#     print('Generating {}'.format(path))
#     if not os.path.exists(DATA):
#         os.makedirs(DATA)
#     dataset = generate_dataset(num_rows, num_cols, num_cats, rate)
#     pickle_dump(dataset, path)
#     return path
# 
# Path: treecat/testutil.py
# TESTDATA = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'testdata')
# 
# Path: treecat/testutil.py
# @contextlib.contextmanager
# def tempdir():
#     dirname = tempfile.mkdtemp()
#     try:
#         yield dirname
#     finally:
#         shutil.rmtree(dirname)


# Path: treecat/validate_test.py
import os
from treecat import validate
from treecat.generate import generate_dataset_file
from treecat.testutil import TESTDATA
from treecat.testutil import tempdir

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function



PARAM_CSV = os.path.join(TESTDATA, 'tuning.csv')


def test_train_eval():
    dataset_path = generate_dataset_file(5, 7)
    with tempdir() as dirname:
        models_dir = os.path.join(dirname, 'models')
        result_path = os.path.join(dirname, 'tune_clusters.pkz')
        validate.train(dataset_path, PARAM_CSV, models_dir)

        validate.eval(dataset_path, PARAM_CSV, models_dir, result_path)