# Path: loom/util.py
# @contextlib.contextmanager
# def tempdir(cleanup_on_error=True):
#     oldwd = os.getcwd()
#     wd = tempfile.mkdtemp()
#     try:
#         os.chdir(wd)
#         yield wd
#         cleanup_on_error = True
#     finally:
#         os.chdir(oldwd)
#         if cleanup_on_error:
#             shutil.rmtree(wd)


import os
import numpy.random
import parsable
import loom.schema
import loom.hyperprior
import loom.config
import loom.runner
import loom.documented
from distributions.lp.clustering import PitmanYor
from loom.util import tempdir
from distributions.io.stream import open_compressed, json_load

@parsable.command
def generate(
        feature_type='mixed',
        row_count=1000,
        feature_count=100,
        density=0.5,
        rows_out='rows.pbs.gz',
        model_out='model.pb.gz',
        groups_out=None,
        assign_out=None,
        init_out=None,
        debug=False,
        profile=None):
    '''
    Generate a synthetic dataset.
    '''
    root = os.getcwd()
    rows_out = os.path.abspath(rows_out)
    model_out = os.path.abspath(model_out)
    if groups_out is not None:
        groups_out = os.path.abspath(groups_out)
    if assign_out is not None:
        assign_out = os.path.abspath(assign_out)
    if init_out is not None:
        init_out = os.path.abspath(init_out)

    features = generate_features(feature_count, feature_type)
    model = generate_model(features)


# Path: loom/util.py
# @contextlib.contextmanager
# def tempdir(cleanup_on_error=True):
#     oldwd = os.getcwd()
#     wd = tempfile.mkdtemp()
#     try:
#         os.chdir(wd)
#         yield wd
#         cleanup_on_error = True
#     finally:
#         os.chdir(oldwd)
#         if cleanup_on_error:
#             shutil.rmtree(wd)


# Path: loom/generate.py
import os
import numpy.random
import parsable
import loom.schema
import loom.hyperprior
import loom.config
import loom.runner
import loom.documented
from distributions.lp.clustering import PitmanYor
from loom.util import tempdir
from distributions.io.stream import open_compressed, json_load


@parsable.command
def generate(
        feature_type='mixed',
        row_count=1000,
        feature_count=100,
        density=0.5,
        rows_out='rows.pbs.gz',
        model_out='model.pb.gz',
        groups_out=None,
        assign_out=None,
        init_out=None,
        debug=False,
        profile=None):
    '''
    Generate a synthetic dataset.
    '''
    root = os.getcwd()
    rows_out = os.path.abspath(rows_out)
    model_out = os.path.abspath(model_out)
    if groups_out is not None:
        groups_out = os.path.abspath(groups_out)
    if assign_out is not None:
        assign_out = os.path.abspath(assign_out)
    if init_out is not None:
        init_out = os.path.abspath(init_out)

    features = generate_features(feature_count, feature_type)
    model = generate_model(features)


    with tempdir(cleanup_on_error=(not debug)):