def test_issue_16(): """Check one of the examples from #16""" s = Session() s._add_model_types(sherpa.models.basic) s.dataspace1d(0.01, 11, 0.01, id=1) s.dataspace1d(2, 5, 0.1, id="tst") s.set_source(1, 'powlaw1d.pl1') s.set_source('tst', 'powlaw1d.pltst') assert s.list_data_ids() == [1, 'tst'] assert s.list_model_ids() == [1, 'tst'] assert s.list_model_components() == ['pl1', 'pltst'] s.delete_model(id='tst') s.delete_model_component("pltst") s.delete_data(id='tst') assert s.list_data_ids() == [1] assert s.list_model_ids() == [1] assert s.list_model_components() == ['pl1'] s.delete_model(id=1) s.delete_model_component("pl1") s.delete_data(id=1) assert s.list_data_ids() == [] assert s.list_model_ids() == [] assert s.list_model_components() == []
def test_delete_model_component_warning(caplog): """Check we get a warning (which ends up being issue #16)""" s = Session() s._add_model_types(sherpa.models.basic) s.set_source('const1d.mdl + gauss1d.mdl2') assert s.list_model_components() == ['mdl', 'mdl2'] assert len(caplog.records) == 0 s.delete_model_component('mdl2') assert len(caplog.records) == 1 lname, lvl, msg = caplog.record_tuples[0] assert lname == "sherpa.ui.utils" assert lvl == logging.WARNING assert msg == "the model component 'gauss1d.mdl2' is found in model 1 and cannot be deleted" assert s.list_model_components() == ['mdl', 'mdl2']
def test_paramprompt(): """Does paramprompt work?""" s = Session() assert s.list_model_ids() == [] assert s.list_model_components() == [] # Add in some models s._add_model_types(sherpa.models.basic) models = s.list_models() assert models != [] assert 'const1d' in models s.create_model_component('const1d', 'm1') assert s.list_model_ids() == [] expected = ['m1'] assert s.list_model_components() == expected # Now there are multiple components do not rely on any ordering # provided by list_model_components() # s.paramprompt(True) # paramprompt doesn't affect create_model_component (as shown # by the fact that we don't have to mock stdin) # s.create_model_component('const1d', 'm2') assert s.list_model_ids() == [] expected = set(expected) expected.add('m2') assert set(s.list_model_components()) == expected # it does affect set_model # # pytest errors out if you try to read from stdin in a test, so # try to work around this here using # https://stackoverflow.com/questions/13238566/python-equivalent-of-input-using-sys-stdin # An alternative would be just to check that the error is # raised, and the text, but then we don't get to test the # behaviour of the paramprompt code. # with patch("sys.stdin", StringIO(u"2.1")): s.set_model('const1d.mx') assert s.list_model_ids() == [1] expected.add('mx') assert set(s.list_model_components()) == expected mx = s.get_model_component('mx') assert mx.c0.val == pytest.approx(2.1) with patch("sys.stdin", StringIO(u"2.1e-3 , 2.0e-3, 1.2e-2")): s.set_model('x', 'const1d.my') assert set(s.list_model_ids()) == set([1, 'x']) expected.add('my') assert set(s.list_model_components()) == expected my = s.get_model_component('my') assert my.c0.val == pytest.approx(2.1e-3) assert my.c0.min == pytest.approx(2.0e-3) assert my.c0.max == pytest.approx(1.2e-2) with patch("sys.stdin", StringIO(u"2.1e-3 ,, 1.2e-2")): s.set_model(2, 'const1d.mz') assert set(s.list_model_ids()) == set([1, 2, 'x']) expected.add('mz') assert set(s.list_model_components()) == expected mz = s.get_model_component('mz') assert mz.c0.val == pytest.approx(2.1e-3) assert mz.c0.min == pytest.approx(- parameter.hugeval) assert mz.c0.max == pytest.approx(1.2e-2) with patch("sys.stdin", StringIO(u"-12.1,-12.1")): s.set_model('const1d.f1') assert set(s.list_model_ids()) == set([1, 2, 'x']) expected.add('f1') assert set(s.list_model_components()) == expected f1 = s.get_model_component('f1') assert f1.c0.val == pytest.approx(-12.1) assert f1.c0.min == pytest.approx(-12.1) assert f1.c0.max == pytest.approx(parameter.hugeval) with patch("sys.stdin", StringIO(u" ,-12.1")): s.set_model('const1d.f2') # stop checking list_model_ids expected.add('f2') assert set(s.list_model_components()) == expected f2 = s.get_model_component('f2') assert f2.c0.val == pytest.approx(1.0) assert f2.c0.min == pytest.approx(-12.1) assert f2.c0.max == pytest.approx(parameter.hugeval) with patch("sys.stdin", StringIO(u" ,")): s.set_model('const1d.f3') f3 = s.get_model_component('f3') assert f3.c0.val == pytest.approx(1.0) assert f3.c0.min == pytest.approx(- parameter.hugeval) assert f3.c0.max == pytest.approx(parameter.hugeval) with patch("sys.stdin", StringIO(u" ,, ")): s.set_model('const1d.f4') f4 = s.get_model_component('f4') assert f4.c0.val == pytest.approx(1.0) assert f4.c0.min == pytest.approx(- parameter.hugeval) assert f4.c0.max == pytest.approx(parameter.hugeval)
def test_paramprompt(): """Does paramprompt work?""" s = Session() assert s.list_model_ids() == [] assert s.list_model_components() == [] # Add in some models import sherpa.models.basic s._add_model_types(sherpa.models.basic) assert s.list_models() != [] s.create_model_component('const1d', 'm1') assert s.list_model_ids() == [] expected = ['m1'] assert s.list_model_components() == expected # Now there are multiple components do not rely on any ordering # provided by list_model_components() # s.paramprompt(True) # paramprompt doesn't affect create_model_component (as shown # by the fact that we don't have to mock stdin) # s.create_model_component('const1d', 'm2') assert s.list_model_ids() == [] expected = set(expected) expected.add('m2') assert set(s.list_model_components()) == expected # it does affect set_model # # pytest errors out if you try to read from stdin in a test, so # try to work around this here using # https://stackoverflow.com/questions/13238566/python-equivalent-of-input-using-sys-stdin # An alternative would be just to check that the error is # raised, and the text, but then we don't get to test the # behaviour of the paramprompt code. # with patch("sys.stdin", StringIO(u"2.1")): s.set_model('const1d.mx') assert s.list_model_ids() == [1] expected.add('mx') assert set(s.list_model_components()) == expected mx = s.get_model_component('mx') assert mx.c0.val == pytest.approx(2.1) with patch("sys.stdin", StringIO(u"2.1e-3 , 2.0e-3, 1.2e-2")): s.set_model('x', 'const1d.my') assert set(s.list_model_ids()) == set([1, 'x']) expected.add('my') assert set(s.list_model_components()) == expected my = s.get_model_component('my') assert my.c0.val == pytest.approx(2.1e-3) assert my.c0.min == pytest.approx(2.0e-3) assert my.c0.max == pytest.approx(1.2e-2) with patch("sys.stdin", StringIO(u"2.1e-3 ,, 1.2e-2")): s.set_model(2, 'const1d.mz') assert set(s.list_model_ids()) == set([1, 2, 'x']) expected.add('mz') assert set(s.list_model_components()) == expected mz = s.get_model_component('mz') assert mz.c0.val == pytest.approx(2.1e-3) assert mz.c0.min == pytest.approx(- parameter.hugeval) assert mz.c0.max == pytest.approx(1.2e-2) with patch("sys.stdin", StringIO(u"-12.1,-12.1")): s.set_model('const1d.f1') assert set(s.list_model_ids()) == set([1, 2, 'x']) expected.add('f1') assert set(s.list_model_components()) == expected f1 = s.get_model_component('f1') assert f1.c0.val == pytest.approx(-12.1) assert f1.c0.min == pytest.approx(-12.1) assert f1.c0.max == pytest.approx(parameter.hugeval) with patch("sys.stdin", StringIO(u" ,-12.1")): s.set_model('const1d.f2') # stop checking list_model_ids expected.add('f2') assert set(s.list_model_components()) == expected f2 = s.get_model_component('f2') assert f2.c0.val == pytest.approx(1.0) assert f2.c0.min == pytest.approx(-12.1) assert f2.c0.max == pytest.approx(parameter.hugeval) with patch("sys.stdin", StringIO(u" ,")): s.set_model('const1d.f3') f3 = s.get_model_component('f3') assert f3.c0.val == pytest.approx(1.0) assert f3.c0.min == pytest.approx(- parameter.hugeval) assert f3.c0.max == pytest.approx(parameter.hugeval) with patch("sys.stdin", StringIO(u" ,, ")): s.set_model('const1d.f4') f4 = s.get_model_component('f4') assert f4.c0.val == pytest.approx(1.0) assert f4.c0.min == pytest.approx(- parameter.hugeval) assert f4.c0.max == pytest.approx(parameter.hugeval)