# Path: loom/test/util.py
# def for_each_dataset(fun):
#     @functools.wraps(fun)
#     def test_one(dataset):
#         paths = loom.store.get_paths(dataset, sample_count=2)
#         for key, path in loom.store.iter_paths(dataset, paths):
#             if not os.path.exists(path):
#                 raise ValueError(
#                     'missing {} at {},\n  first `python -m loom.datasets test`'
#                     .format(key, path))
#         kwargs = {'name': dataset}
#         kwargs.update(paths['query'])
#         kwargs.update(paths['consensus'])
#         kwargs.update(paths['samples'][0])
#         kwargs.update(paths['ingest'])
#         kwargs.update(paths)
#         fun(**kwargs)
# 
#     @functools.wraps(fun)
#     def test_all():
#         for dataset in loom.datasets.TEST_CONFIGS:
#             yield test_one, dataset
# 
#     return test_all
# 
# def load_rows(filename):
#     rows = []
#     for string in protobuf_stream_load(filename):
#         row = Row()
#         row.ParseFromString(string)
#         rows.append(row)
#     return rows


import numpy
import loom.datasets
import loom.preql
import loom.query
from math import log
from nose.tools import assert_almost_equal
from nose.tools import assert_greater
from nose.tools import assert_less
from nose.tools import assert_less_equal
from distributions.fileutil import tempdir
from distributions.util import density_goodness_of_fit
from distributions.util import discrete_goodness_of_fit
from loom.test.util import for_each_dataset, load_rows
    row[fi] = None
    to_sample = [i == fi for i in range(len(row))]
    samples = server.sample(to_sample, row, SAMPLE_COUNT)
    val = samples[0][fi]
    base_score = server.score(row)
    if isinstance(val, bool) or isinstance(val, int):
        probs_dict = {}
        samples = [sample[fi] for sample in samples]
        for sample in set(samples):
            row[fi] = sample
            probs_dict[sample] = numpy.exp(
                server.score(row) - base_score)
        if len(probs_dict) == 1:
            assert_almost_equal(probs_dict[sample], 1., places=SCORE_PLACES)
            return
        if min(probs_dict.values()) < MIN_CATEGORICAL_PROB:
            return
        gof = discrete_goodness_of_fit(samples, probs_dict, plot=True)
    elif isinstance(val, float):
        probs = numpy.exp([
            server.score(sample) - base_score
            for sample in samples
        ])
        samples = [sample[fi] for sample in samples]
        gof = density_goodness_of_fit(samples, probs, plot=True)
    assert_greater(gof, MIN_GOODNESS_OF_FIT)


@for_each_dataset
def test_samples_match_scores(root, rows, **unused):

# Path: loom/test/util.py
# def for_each_dataset(fun):
#     @functools.wraps(fun)
#     def test_one(dataset):
#         paths = loom.store.get_paths(dataset, sample_count=2)
#         for key, path in loom.store.iter_paths(dataset, paths):
#             if not os.path.exists(path):
#                 raise ValueError(
#                     'missing {} at {},\n  first `python -m loom.datasets test`'
#                     .format(key, path))
#         kwargs = {'name': dataset}
#         kwargs.update(paths['query'])
#         kwargs.update(paths['consensus'])
#         kwargs.update(paths['samples'][0])
#         kwargs.update(paths['ingest'])
#         kwargs.update(paths)
#         fun(**kwargs)
# 
#     @functools.wraps(fun)
#     def test_all():
#         for dataset in loom.datasets.TEST_CONFIGS:
#             yield test_one, dataset
# 
#     return test_all
# 
# def load_rows(filename):
#     rows = []
#     for string in protobuf_stream_load(filename):
#         row = Row()
#         row.ParseFromString(string)
#         rows.append(row)
#     return rows


# Path: loom/test/test_query_math.py
import numpy
import loom.datasets
import loom.preql
import loom.query
from math import log
from nose.tools import assert_almost_equal
from nose.tools import assert_greater
from nose.tools import assert_less
from nose.tools import assert_less_equal
from distributions.fileutil import tempdir
from distributions.util import density_goodness_of_fit
from distributions.util import discrete_goodness_of_fit
from loom.test.util import for_each_dataset, load_rows

    row[fi] = None
    to_sample = [i == fi for i in range(len(row))]
    samples = server.sample(to_sample, row, SAMPLE_COUNT)
    val = samples[0][fi]
    base_score = server.score(row)
    if isinstance(val, bool) or isinstance(val, int):
        probs_dict = {}
        samples = [sample[fi] for sample in samples]
        for sample in set(samples):
            row[fi] = sample
            probs_dict[sample] = numpy.exp(
                server.score(row) - base_score)
        if len(probs_dict) == 1:
            assert_almost_equal(probs_dict[sample], 1., places=SCORE_PLACES)
            return
        if min(probs_dict.values()) < MIN_CATEGORICAL_PROB:
            return
        gof = discrete_goodness_of_fit(samples, probs_dict, plot=True)
    elif isinstance(val, float):
        probs = numpy.exp([
            server.score(sample) - base_score
            for sample in samples
        ])
        samples = [sample[fi] for sample in samples]
        gof = density_goodness_of_fit(samples, probs, plot=True)
    assert_greater(gof, MIN_GOODNESS_OF_FIT)


@for_each_dataset
def test_samples_match_scores(root, rows, **unused):

    rows = load_rows(rows)