Seemingly random failures in formula #258

Closed
matthew-brett opened this Issue Feb 3, 2013 · 2 comments

Comments

Projects
None yet
1 participant
Owner

matthew-brett commented Feb 3, 2013

Testing on Python 3.3:

======================================================================
ERROR: nipy.algorithms.statistics.formula.tests.test_formula.test_formula_from_recarray
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/site-packages/numpy/core/fromnumeric.py", line 1507, in sum
    sum = a.sum
AttributeError: 'list' object has no attribute 'sum'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/site-packages/nose-1.2.1-py3.3.egg/nose/case.py", line 198, in runTest
    self.test(*self.arg)
  File "/Users/buildslave/osx-10.6/nipy-bdist-mpkg-3_3/build/venv/lib/python3.3/site-packages/nipy-0.3.0.dev-py3.3-macosx-10.6-intel.egg/nipy/algorithms/statistics/formula/tests/test_formula.py", line 133, in test_formula_from_recarray
    f = F.Formula.fromrec(D, drop='y')
  File "/Users/buildslave/osx-10.6/nipy-bdist-mpkg-3_3/build/venv/lib/python3.3/site-packages/nipy-0.3.0.dev-py3.3-macosx-10.6-intel.egg/nipy/algorithms/statistics/formula/formulae.py", line 538, in fromrec
    return np.sum(list(f.values()))
  File "/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/site-packages/numpy/core/fromnumeric.py", line 1510, in sum
    out=out, keepdims=keepdims)
  File "/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/site-packages/numpy/core/_methods.py", line 18, in _sum
    out=out, keepdims=keepdims)
  File "/Users/buildslave/osx-10.6/nipy-bdist-mpkg-3_3/build/venv/lib/python3.3/site-packages/nipy-0.3.0.dev-py3.3-macosx-10.6-intel.egg/nipy/algorithms/statistics/formula/formulae.py", line 587, in __add__
    f = self.__class__(np.hstack([self.terms, other.terms]))
TypeError: __init__() missing 1 required positional argument: 'levels'

======================================================================
FAIL: Doctest: nipy.algorithms.statistics.formula.formulae.Formula.__sub__
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/doctest.py", line 2154, in runTest
    raise self.failureException(self.format_failure(new.getvalue()))
AssertionError: Failed doctest test for nipy.algorithms.statistics.formula.formulae.Formula.__sub__
  File "/Users/buildslave/osx-10.6/nipy-bdist-mpkg-3_3/build/venv/lib/python3.3/site-packages/nipy-0.3.0.dev-py3.3-macosx-10.6-intel.egg/nipy/algorithms/statistics/formula/formulae.py", line 590, in __sub__

----------------------------------------------------------------------
File "/Users/buildslave/osx-10.6/nipy-bdist-mpkg-3_3/build/venv/lib/python3.3/site-packages/nipy-0.3.0.dev-py3.3-macosx-10.6-intel.egg/nipy/algorithms/statistics/formula/formulae.py", line 607, in nipy.algorithms.statistics.formula.formulae.Formula.__sub__
Failed example:
    f4.mean
Expected:
    _b0*x + _b1*z
Got:
    _b0*z + _b1*x

See: http://nipy.bic.berkeley.edu/builders/nipy-bdist-mpkg-3.3/builds/13/steps/shell_4/logs/stdio

The second failure looks to be a term order effect. The first I don't understand.

matthew-brett added a commit to matthew-brett/nipy that referenced this issue Aug 28, 2015

BF+TST: fix bug for adding and subtracting Factors
Without properly understanding the code, I previously (a8f6f44) made
Formula add and subtract return an instance of the added / subtracted
class, but that forced the assumption that the class constructor for the
subclass is the the same as Formula, which is not the case for Factor.
Fix and test.

Partially fixes gh-258.

matthew-brett added a commit to matthew-brett/nipy that referenced this issue Aug 28, 2015

BF+TST: fix bug for adding and subtracting Factors
Without properly understanding the code, I previously (a8f6f44) made
Formula add and subtract return an instance of the added / subtracted
class, but that forced the assumption that the class constructor for the
subclass is the the same as Formula, which is not the case for Factor.
Fix and test.

Partially fixes gh-258.

matthew-brett added a commit to matthew-brett/nipy that referenced this issue Aug 28, 2015

BF+TST: enforce order of terms in subtraction
Make order of terms in subtraction predictable.

Finished fix of gh-258.

matthew-brett added a commit to matthew-brett/nipy that referenced this issue Aug 28, 2015

BF+TST: fix bug for adding and subtracting Factors
Without properly understanding the code, I previously (a8f6f44) made
Formula add and subtract return an instance of the added / subtracted
class, but that forced the assumption that the class constructor for the
subclass is the the same as Formula, which is not the case for Factor.
Fix and test.

Partially fixes gh-258.

matthew-brett added a commit to matthew-brett/nipy that referenced this issue Aug 28, 2015

BF+TST: enforce order of terms in subtraction
Make order of terms in subtraction predictable.

Finished fix of gh-258.

matthew-brett added a commit that referenced this issue Aug 29, 2015

Merge pull request #348 from matthew-brett/fix-formula-errors
MRG: fix intermittent errors from Formula

Closes gh-258.

Intermittent errors testing Formula, turned out to be from:

* An error I introduced trying to make it easier to use sub-classes with
  Formula add and subtract;
* Unpredictable order of terms after subtraction, caused by use of set to find
  unique terms.

The problem with the add / sub inheritance was that the add and sub methods create a new object at the end, from a list of terms.  By using ``self.__class__`` to construct the output object, I am asserting that the class constructor has the same signature as the Formula class, and that is not true of Factor, which needs a name.

One way round that would be to have a ``from_terms`` method to Formula, and use that, but in fact I don't think its unreasonable to return a Formula from the Factor subtraction, the main logic of the Factor is in the constructor, and combining two factors can make something that isn't a pure Factor.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment