[yt-svn] commit/yt: MatthewTurk: Porting simple answer testing to main 3.0 branch.
commits-noreply at bitbucket.org
commits-noreply at bitbucket.org
Mon Jan 27 05:01:27 PST 2014
1 new commit in yt:
https://bitbucket.org/yt_analysis/yt/commits/505717937bcd/
Changeset: 505717937bcd
Branch: yt-3.0
User: MatthewTurk
Date: 2014-01-27 14:00:53
Summary: Porting simple answer testing to main 3.0 branch.
Affected #: 1 file
diff -r b3e969705199c12226c7e8e24a3f7d110f872e56 -r 505717937bcd242f8ceafa1b5646ff921b20a52d yt/testing.py
--- a/yt/testing.py
+++ b/yt/testing.py
@@ -514,3 +514,87 @@
[44,48,48],
],
]
+
+def check_results(func):
+ r"""This is a decorator for a function to verify that the (numpy ndarray)
+ result of a function is what it should be.
+
+ This function is designed to be used for very light answer testing.
+ Essentially, it wraps around a larger function that returns a numpy array,
+ and that has results that should not change. It is not necessarily used
+ inside the testing scripts themselves, but inside testing scripts written
+ by developers during the testing of pull requests and new functionality.
+ If a hash is specified, it "wins" and the others are ignored. Otherwise,
+ tolerance is 1e-8 (just above single precision.)
+
+ The correct results will be stored if the command line contains
+ --answer-reference , and otherwise it will compare against the results on
+ disk. The filename will be func_results_ref_FUNCNAME.cpkl where FUNCNAME
+ is the name of the function being tested.
+
+ This will raise an exception if the results are not correct.
+
+ Examples
+ --------
+
+ @check_results
+ def my_func(pf):
+ return pf.domain_width
+
+ my_func(pf)
+ """
+ def compute_results(func):
+ def _func(*args, **kwargs):
+ name = kwargs.pop("result_basename", func.func_name)
+ rv = func(*args, **kwargs)
+ if hasattr(rv, "convert_to_cgs"):
+ rv.convert_to_cgs()
+ _rv = rv.ndarray_view()
+ else:
+ _rv = rv
+ mi = _rv.min()
+ ma = _rv.max()
+ st = _rv.std(dtype="float64")
+ su = _rv.sum(dtype="float64")
+ si = _rv.size
+ ha = md5.md5(_rv.tostring()).hexdigest()
+ fn = "func_results_ref_%s.cpkl" % (name)
+ with open(fn, "wb") as f:
+ cPickle.dump( (mi, ma, st, su, si, ha), f)
+ return rv
+ return _func
+ from yt.mods import unparsed_args
+ if "--answer-reference" in unparsed_args:
+ return compute_results(func)
+
+ def compare_results(func):
+ def _func(*args, **kwargs):
+ name = kwargs.pop("result_basename", func.func_name)
+ rv = func(*args, **kwargs)
+ if hasattr(rv, "convert_to_cgs"):
+ rv.convert_to_cgs()
+ _rv = rv.ndarray_view()
+ else:
+ _rv = rv
+ vals = (_rv.min(),
+ _rv.max(),
+ _rv.std(dtype="float64"),
+ _rv.sum(dtype="float64"),
+ _rv.size,
+ md5.md5(_rv.tostring()).hexdigest() )
+ fn = "func_results_ref_%s.cpkl" % (name)
+ if not os.path.exists(fn):
+ print "Answers need to be created with --answer-reference ."
+ return False
+ with open(fn, "rb") as f:
+ ref = cPickle.load(f)
+ print "Sizes: %s (%s, %s)" % (vals[4] == ref[4], vals[4], ref[4])
+ assert_allclose(vals[0], ref[0], 1e-8, err_msg="min")
+ assert_allclose(vals[1], ref[1], 1e-8, err_msg="max")
+ assert_allclose(vals[2], ref[2], 1e-8, err_msg="std")
+ assert_allclose(vals[3], ref[3], 1e-8, err_msg="sum")
+ assert_equal(vals[4], ref[4])
+ print "Hashes equal: %s" % (vals[-1] == ref[-1])
+ return rv
+ return _func
+ return compare_results(func)
Repository URL: https://bitbucket.org/yt_analysis/yt/
--
This is a commit notification from bitbucket.org. You are receiving
this because you have the service enabled, addressing the recipient of
this email.
More information about the yt-svn
mailing list