Permalink
Browse files

Add default_evaluator() function, use it in `barvinok' script and uni…

…t tests. Also refactored conftest.
  • Loading branch information...
catch22 committed Jul 6, 2016
1 parent 7ca4a5b commit 517916b5ddd58a05f41c7ee59793d441d4fce554
Showing with 79 additions and 21 deletions.
  1. +33 −1 barvikron/parfun.py
  2. +19 −6 barvikron/scripts/serial.py
  3. +1 −1 setup.py
  4. +26 −13 tests/conftest.py
@@ -1,7 +1,10 @@
from __future__ import absolute_import
import os
import numpy as np
import whichcraft
__all__ = ['VectorPartitionFunction', 'EvaluatorBase']
__all__ = ['VectorPartitionFunction', 'EvaluatorBase', 'default_evaluator',
'NoEvaluatorFound']
class VectorPartitionFunction(object):
@@ -31,3 +34,32 @@ class EvaluatorBase(object):
def eval(self, vpn, b):
raise NotImplementedError
class NoEvaluatorFound(Exception):
pass
def default_evaluator():
"""
Find and instantiate best available evaluator.
"""
from .barvinok import BarvinokEvaluator
from .latte import LatteEvaluator
# first try environment variables
if 'BARVIKRON_BARVINOK' in os.environ:
return BarvinokEvaluator(os.environ['BARVIKRON_BARVINOK'])
if 'BARVIKRON_LATTE' in os.environ:
return LatteEvaluator(os.environ['BARVIKRON_LATTE'])
# then try to find executables
path = whichcraft.which('barvinok_count')
if path:
return BarvinokEvaluator(path)
path = whichcraft.which('count')
if path:
return LatteEvaluator(path)
raise NoEvaluatorFound()
@@ -1,6 +1,6 @@
from __future__ import absolute_import, print_function
import click
from .. import BarvinokEvaluator, LatteEvaluator, kronecker_weight_multiplicity, kronecker
import click, sys
from .. import BarvinokEvaluator, LatteEvaluator, kronecker_weight_multiplicity, kronecker, default_evaluator, NoEvaluatorFound
from . import WeightParamType, enable_logging
@@ -33,10 +33,23 @@ def main(partitions, weight_multiplicity, barvinok, latte, verbose):
enable_logging()
# instantiate evaluator
assert bool(barvinok) != bool(
latte), 'Specify either --barvinok or --latte.'
evaluator = BarvinokEvaluator(barvinok) if barvinok else LatteEvaluator(
latte)
if barvinok and latte:
click.echo('Specify either --barvinok or --latte (but not both).',
err=True)
sys.exit(1)
if barvinok:
evaluator = BarvinokEvaluator(barvinok)
elif latte:
evaluator = LatteEvaluator(latte)
else:
try:
evaluator = default_evaluator()
except NoEvaluatorFound:
click.echo(
'No partition function evaluator found. Specify --barvinok or --latte option.',
err=True)
sys.exit(1)
# compute Kronecker coefficient
if weight_multiplicity:
@@ -36,7 +36,7 @@
'Development Status :: 4 - Beta',
'Environment :: Console',
],
install_requires=['Click', 'numpy', 'six'],
install_requires=['Click', 'numpy', 'six', 'whichcraft'],
packages=['barvikron'],
tests_require=['pytest'],
entry_points='''
@@ -1,5 +1,10 @@
from __future__ import absolute_import
from barvikron import BarvinokEvaluator, LatteEvaluator
from barvikron import BarvinokEvaluator, LatteEvaluator, default_evaluator
import pytest
def pytest_namespace():
return {'evaluators': []}
def pytest_addoption(parser):
@@ -17,18 +22,26 @@ def pytest_addoption(parser):
help="evaluate partition functions using LaTTe (https://www.math.ucdavis.edu/~latte/)")
def pytest_generate_tests(metafunc):
if 'evaluator' in metafunc.fixturenames:
evaluators = []
def pytest_configure(config):
# add barvinok?
barvinok_path = config.getoption("--barvinok")
if barvinok_path:
pytest.evaluators.append(BarvinokEvaluator(barvinok_path))
# add latte?
latte_path = config.getoption("--latte")
if latte_path:
pytest.evaluators.append(LatteEvaluator(latte_path))
# add barvinok?
barvinok_path = metafunc.config.getoption("--barvinok")
if barvinok_path:
evaluators.append(BarvinokEvaluator(barvinok_path))
# no evaluator specified? add default
if not pytest.evaluators:
pytest.evaluators.append(default_evaluator())
# add latte? (XXX)
latte_path = metafunc.config.getoption("--latte")
if latte_path:
evaluators.append(LatteEvaluator(latte_path))
metafunc.parametrize('evaluator', evaluators)
def pytest_report_header(config):
return "evaluators: %s" % ', '.join(str(e) for e in pytest.evaluators)
def pytest_generate_tests(metafunc):
if 'evaluator' in metafunc.fixturenames:
metafunc.parametrize('evaluator', pytest.evaluators)

0 comments on commit 517916b

Please sign in to comment.