keflavich / imf

Simple tools to work with the Initial Mass Function
MIT License
44 stars 17 forks source link

Failing tests #16

Closed segasai closed 4 years ago

segasai commented 4 years ago

There are 4 tests currently failing. I don't think I'll have time to fix them right now. But it's probably a good idea to figure out what's going on. Also it'd be good to set-up travis-ci testing to avoid this.

================================= FAILURES =================================== __ test_mmax[kroupa] ___

massfunc = 'kroupa'

@pytest.mark.parametrize('massfunc', imf.massfunctions.keys())
def test_mmax(massfunc):
    """
    Regression test for issue #4
    """

    if (not hasattr(imf.get_massfunc(massfunc), 'mmin')):
        pytest.skip("{0} doesn't have mmin defined".format(massfunc))

    c = imf.make_cluster(10000, mmax=1, mmin=0.01, massfunc=massfunc)
  assert c.max() <= 1

E assert 101.66580141247158 <= 1 E + where 101.66580141247158 = <built-in method max of numpy.ndarray object at 0x7f21fe7c61c0>() E + where <built-in method max of numpy.ndarray object at 0x7f21fe7c61c0> = array([0.04984712, 0.06659554, 0.07523479, ..., 0.60333851, 0.56690724,\n 2.2095286 ]).max

imf/tests/testimf.py:20: AssertionError ----------------------------- Captured stdout call ----------------------------- Total cluster mass is 10000.6 (limit was 10000) ____ test_mmax[salpeter] __

massfunc = 'salpeter'

@pytest.mark.parametrize('massfunc', imf.massfunctions.keys())
def test_mmax(massfunc):
    """
    Regression test for issue #4
    """

    if (not hasattr(imf.get_massfunc(massfunc), 'mmin')):
        pytest.skip("{0} doesn't have mmin defined".format(massfunc))

    c = imf.make_cluster(10000, mmax=1, mmin=0.01, massfunc=massfunc)
  assert c.max() <= 1

E assert 94.57976156632255 <= 1 E + where 94.57976156632255 = <built-in method max of numpy.ndarray object at 0x7f21fe7be800>() E + where <built-in method max of numpy.ndarray object at 0x7f21fe7be800> = array([0.40759118, 0.35635737, 0.38770893, ..., 0.56901474, 0.50279477,\n 0.75519362]).max

imf/tests/testimf.py:20: AssertionError ----------------------------- Captured stdout call ----------------------------- Total cluster mass is 9999.38 (limit was 10000) ____ test_mmax[chabrier] __

massfunc = 'chabrier'

@pytest.mark.parametrize('massfunc', imf.massfunctions.keys())
def test_mmax(massfunc):
    """
    Regression test for issue #4
    """

    if (not hasattr(imf.get_massfunc(massfunc), 'mmin')):
        pytest.skip("{0} doesn't have mmin defined".format(massfunc))
  c = imf.make_cluster(10000, mmax=1, mmin=0.01, massfunc=massfunc)

imf/tests/test_imf.py:18:


mcluster = 10000, massfunc = 'chabrier', verbose = False, silent = False tolerance = 0.0, stop_criterion = 'nearest', mmax = 1, kwargs = {'mmin': 0.01} expected_mass = -0.03687992251819145

def make_cluster(mcluster, massfunc='kroupa', verbose=False, silent=False,
                 tolerance=0.0, stop_criterion='nearest', mmax=120, **kwargs):
    """
    Sample from an IMF to make a cluster.  Returns the masses of all stars in the cluster

    massfunc must be a string
    tolerance is how close the cluster mass must be to the requested mass.
    If the last star is greater than this tolerance, the total mass will not be within
    tolerance of the requested

    stop criteria can be: 'nearest', 'before', 'after', 'sorted'

    kwargs are passed to `inverse_imf`
    """

    # use most common mass to guess needed number of samples
    #nsamp = mcluster / mostcommonmass[get_massfunc_name(massfunc)]
    #masses = inverse_imf(np.random.random(int(nsamp)), massfunc=massfunc, **kwargs)

    #mtot = masses.sum()
    #if verbose:
    #    print(("%i samples yielded a cluster mass of %g (%g requested)" %
    #          (nsamp,mtot,mcluster)))

    if (massfunc, get_massfunc(massfunc).mmin, mmax) in expectedmass_cache:
        expected_mass = expectedmass_cache[(massfunc,
                                            get_massfunc(massfunc).mmin, mmax)]
        assert expected_mass > 0
    else:
        expected_mass = get_massfunc(massfunc).m_integrate(get_massfunc(massfunc).mmin,
                                                           mmax)[0]
      assert expected_mass > 0

E AssertionError

imf/imf.py:451: AssertionError _ test_kroupainverses

def test_kroupa_inverses():
  assert np.abs(imf.inverse_imf(0, massfunc=imf.Kroupa(), mmin=0.01) - 0.01) < 2e-3

E AssertionError: assert 0.019902335806411077 < 0.002 E + where 0.019902335806411077 = <ufunc 'absolute'>((0.02990233580641108 - 0.01)) E + where <ufunc 'absolute'> = np.abs E + and 0.02990233580641108 = <function inverse_imf at 0x7f21fe8b56a8>(0, massfunc=<imf.imf.Kroupa object at 0x7f21fe5f7cc0>, mmin=0.01) E + where <function inverse_imf at 0x7f21fe8b56a8> = imf.inverse_imf E + and <imf.imf.Kroupa object at 0x7f21fe5f7cc0> = <class 'imf.imf.Kroupa'>() E + where <class 'imf.imf.Kroupa'> = imf.Kroupa

imf/tests/test_imf.py:84: AssertionError

=========================== short test summary info ============================ FAILED imf/tests/test_imf.py::test_mmax[kroupa] - assert 101.66580141247158 <= 1 FAILED imf/tests/test_imf.py::test_mmax[salpeter] - assert 94.57976156632255 ... FAILED imf/tests/test_imf.py::test_mmax[chabrier] - AssertionError FAILED imf/tests/test_imf.py::test_kroupa_inverses - AssertionError: assert 0... ============ 4 failed, 82 passed, 49 skipped, 3 warnings in 42.31s =========

keflavich commented 4 years ago

yeah, I broke things. I'll work on this, including ci

keflavich commented 4 years ago

I think I've fixed all the bugs I introduced, but I need to do some more work to get travis green again. I'm going to move to a policy of not committing directly to master after this.

keflavich commented 4 years ago

Tests pass locally now. Please open a new issue if you encounter anything else... I've been encountering some strange behaviors that are just uncaught typos, but they definitely should have been caught.