Esempio n. 1
0
def test_paramprompt_eof(caplog):
    """What happens when we end early?"""

    s = Session()
    s._add_model_types(sherpa.models.basic)

    cpt1 = s.create_model_component('const1d', 'bob')
    cpt2 = s.create_model_component('gauss1d', 'fred')

    s.paramprompt(True)
    assert len(caplog.records) == 0

    with pytest.raises(EOFError):
        with SherpaVerbosity('INFO'):
            with patch("sys.stdin", StringIO("\n5,1,5\n2\n")):
                s.set_source(bob + fred)

    assert len(caplog.records) == 0

    assert cpt1.c0.val == pytest.approx(1)
    assert cpt1.c0.min < -3e38
    assert cpt1.c0.max > 3e38

    assert cpt2.fwhm.val == pytest.approx(5)
    assert cpt2.fwhm.min == pytest.approx(1)
    assert cpt2.fwhm.max == pytest.approx(5)

    assert cpt2.pos.val == pytest.approx(2)
    assert cpt2.pos.min < -3e38
    assert cpt2.pos.max > 3e38

    assert cpt2.ampl.val == pytest.approx(1)
    assert cpt2.ampl.min < -3e38
    assert cpt2.ampl.max > 3e38
Esempio n. 2
0
def test_paramprompt_single_parameter_check_invalid_max_out_of_bound(caplog):
    """Note this creates two warnings"""

    s = Session()
    s._add_model_types(sherpa.models.basic)

    s.paramprompt(True)
    assert len(caplog.records) == 0

    with SherpaVerbosity('INFO'):
        with patch("sys.stdin", StringIO(",,typo\n,,-200")):
            s.set_source("scale1d.bob")

    assert len(caplog.records) == 2
    lname, lvl, msg = caplog.record_tuples[0]
    assert lname == "sherpa.ui.utils"
    assert lvl == logging.INFO
    assert msg == "Please provide a float value; could not convert string to float: 'typo'"

    lname, lvl, msg = caplog.record_tuples[1]
    assert lname == "sherpa.models.parameter"
    assert lvl == logging.WARN
    assert msg == "parameter bob.c0 greater than new maximum; bob.c0 reset to -200"

    mdl = s.get_model_component('bob')
    assert mdl.c0.val == pytest.approx(-200)
    assert mdl.c0.min < -3e38
    assert mdl.c0.max == pytest.approx(-200)
Esempio n. 3
0
def test_paramprompt_multi_parameter(caplog):
    """Check that paramprompt works with multiple parameters"""

    s = Session()
    s._add_model_types(sherpa.models.basic)

    cpt1 = s.create_model_component('const1d', 'bob')
    cpt2 = s.create_model_component('gauss1d', 'fred')

    s.paramprompt(True)
    assert len(caplog.records) == 0

    with SherpaVerbosity('INFO'):
        with patch("sys.stdin", StringIO("\n5,1,5\n\n-5, -5, 0")):
            s.set_source(bob + fred)

    assert len(caplog.records) == 0

    assert cpt1.c0.val == pytest.approx(1)
    assert cpt1.c0.min < -3e38
    assert cpt1.c0.max > 3e38

    assert cpt2.fwhm.val == pytest.approx(5)
    assert cpt2.fwhm.min == pytest.approx(1)
    assert cpt2.fwhm.max == pytest.approx(5)

    assert cpt2.pos.val == pytest.approx(0)
    assert cpt2.pos.min < -3e38
    assert cpt2.pos.max > 3e38

    assert cpt2.ampl.val == pytest.approx(-5)
    assert cpt2.ampl.min == pytest.approx(-5)
    assert cpt2.ampl.max == pytest.approx(0)
Esempio n. 4
0
def test_paramprompt_single_parameter_check_too_many_commas(caplog):
    """Check we tell users there was a problem"""

    s = Session()
    s._add_model_types(sherpa.models.basic)

    s.paramprompt(True)
    assert len(caplog.records) == 0

    with SherpaVerbosity('INFO'):
        with patch("sys.stdin", StringIO(",,,,\n12")):
            s.set_source("scale1d.bob")

    assert len(caplog.records) == 1
    lname, lvl, msg = caplog.record_tuples[0]
    assert lname == "sherpa.ui.utils"
    assert lvl == logging.INFO
    assert msg == "Error: Please provide a comma-separated list of floats; e.g. val,min,max"

    mdl = s.get_model_component('bob')
    assert mdl.c0.val == pytest.approx(12)
    assert mdl.c0.min < -3e38
    assert mdl.c0.max > 3e38

    # remove the bob symbol from the global table
    s.clean()
Esempio n. 5
0
def test_est_errors_works_single_parameter(mdlcls, method, getter, clean_ui):
    """This is issue #1397.

    Rather than require XSPEC, we create a subclass of the Parameter
    class to check it works. We are not too concerned with the actual
    results hence the relatively low tolerance on the numeric checks.

    """

    mdl = mdlcls()

    ui.load_arrays(1, [1, 2, 3, 4], [4, 2, 1, 3.5])
    ui.set_source(mdl)
    with SherpaVerbosity("ERROR"):
        ui.fit()

        # this is where #1397 fails with Const2
        method(mdl.con)

    atol = 1e-4
    assert ui.calc_stat() == pytest.approx(0.7651548418626658, abs=atol)

    results = getter()
    assert results.parnames == (f"{mdl.name}.con", )
    assert results.sigma == pytest.approx(1.0)

    assert results.parvals == pytest.approx((2.324060647544594, ), abs=atol)

    # The covar errors are -/+ 1.3704388763054511
    #     conf             -1.3704388763054511 / +1.3704388763054514
    #     proj             -1.3704388762971822 / +1.3704388763135826
    #
    err = 1.3704388763054511
    assert results.parmins == pytest.approx((-err, ), abs=atol)
    assert results.parmaxes == pytest.approx((err, ), abs=atol)
Esempio n. 6
0
def test_paramprompt_single_parameter_check_invalid_max(caplog):

    s = Session()
    s._add_model_types(sherpa.models.basic)

    s.paramprompt(True)
    assert len(caplog.records) == 0

    with SherpaVerbosity('INFO'):
        with patch("sys.stdin", StringIO("-2,,typo\n-200,,-2")):
            s.set_source("scale1d.bob")

    assert len(caplog.records) == 1
    lname, lvl, msg = caplog.record_tuples[0]
    assert lname == "sherpa.ui.utils"
    assert lvl == logging.INFO
    assert msg == "Please provide a float value; could not convert string to float: 'typo'"

    mdl = s.get_model_component('bob')
    assert mdl.c0.val == pytest.approx(-200)
    assert mdl.c0.min < -3e38
    assert mdl.c0.max == pytest.approx(-2)

    # remove the bob symbol from the global table
    s.clean()
Esempio n. 7
0
def check_imgdata_unconvolved(caplog):
    """What is the behavior when we do not add the PSF to plot_pvalue?

    Note we add a check of the screen output here.
    """

    # include a check of the screen output
    #
    with caplog.at_level('INFO', logger='sherpa'):
        with SherpaVerbosity('INFO'):
            ui.plot_pvalue(c1, c1 + g1, num=40, bins=5)

    assert len(caplog.records) == 1

    lname, lvl, msg = caplog.record_tuples[0]
    assert lname == 'sherpa.ui.utils'
    assert lvl == logging.INFO

    # Do not use equality tests for the numeric values in case
    # there are numpy-version differences in the number of
    # significant figures.
    #
    toks = msg.split('\n')
    assert len(toks) == 5
    assert toks[0] == 'Likelihood Ratio Test'
    assert toks[1].startswith('null statistic   =  2391.2696')
    assert toks[2].startswith('alt statistic    =  353.82')
    assert toks[3].startswith('likelihood ratio =  2037.446')
    assert toks[4] == 'p-value          <  0.025'

    tmp = ui.get_pvalue_results()

    assert tmp.null == pytest.approx(2391.2696310023503)
    assert tmp.alt == pytest.approx(353.8235336370698)
    assert tmp.lr == pytest.approx(2037.4460973652804)

    assert tmp.samples.shape == (40, 1)
    assert tmp.stats.shape == (40, 2)
    assert tmp.ratios.shape == (40, )

    tmp = ui.get_pvalue_plot(2037.4460973652804)

    assert tmp.lr == pytest.approx(2037.4460973652804)

    assert tmp.xlabel == 'Likelihood Ratio'
    assert tmp.ylabel == 'Frequency'
    assert tmp.title == 'Likelihood Ratio Distribution'

    assert tmp.ratios.shape == (40, )
    assert tmp.xlo.shape == (6, )
    assert tmp.xhi.shape == (6, )
    assert tmp.y.shape == (6, )
Esempio n. 8
0
def test_logging_verbosity_contextmanager(caplog):

    with caplog.at_level(logging.INFO, logger='sherpa'):
        logger = logging.getLogger('sherpa.some_module')
        logger.warning('1: should be seen')
        assert len(caplog.records) == 1

        with SherpaVerbosity('ERROR'):
            logger.warning('2: Should not be seen')
        assert len(caplog.records) == 1

        logger.warning('3: should be seen')
        assert len(caplog.records) == 2
Esempio n. 9
0
def test_paramprompt_single_parameter_combo_works(caplog):

    s = Session()
    s._add_model_types(sherpa.models.basic)

    s.paramprompt(True)
    assert len(caplog.records) == 0

    with SherpaVerbosity('INFO'):
        with patch("sys.stdin", StringIO("-2,-10,10")):
            s.set_source("scale1d.bob")

    assert len(caplog.records) == 0
    mdl = s.get_model_component('bob')
    assert mdl.c0.val == pytest.approx(-2)
    assert mdl.c0.min == pytest.approx(-10)
    assert mdl.c0.max == pytest.approx(10)
Esempio n. 10
0
def test_load_data(loader, make_data_path, clean_astro_ui, caplog):
    """Ensure that loading a single file to a non-integer id works.

    This is just to make sure that the support for PHA2 files in both
    load_data and load_pha does not change the single-file case.

    """
    infile = make_data_path('3c273.pi')
    bgfile = make_data_path('3c273_bg.pi')

    arf = make_data_path('3c273.arf')
    rmf = make_data_path('3c273.rmf')

    assert ui.list_data_ids() == []

    with SherpaVerbosity('INFO'):
        loader('foo', infile)

    assert ui.list_data_ids() == ['foo']

    msg1 = f"systematic errors were not found in file '{infile}'"
    msg2 = f"statistical errors were found in file '{infile}' \n" + \
        "but not used; to use them, re-read with use_errors=True"
    msg3 = f"read ARF file {arf}"
    msg4 = f"read RMF file {rmf}"

    msg5 = f"systematic errors were not found in file '{bgfile}'"
    msg6 = f"statistical errors were found in file '{bgfile}' \n" + \
        "but not used; to use them, re-read with use_errors=True"
    msg7 = f"read background file {bgfile}"

    assert caplog.record_tuples[0] == ('sherpa.astro.io', logging.WARNING,
                                       msg1)
    assert caplog.record_tuples[1] == ('sherpa.astro.io', logging.INFO, msg2)
    assert caplog.record_tuples[2] == ('sherpa.astro.io', logging.INFO, msg3)
    assert caplog.record_tuples[3] == ('sherpa.astro.io', logging.INFO, msg4)

    assert caplog.record_tuples[4] == ('sherpa.astro.io', logging.WARNING,
                                       msg5)
    assert caplog.record_tuples[5] == ('sherpa.astro.io', logging.INFO, msg6)
    assert caplog.record_tuples[6] == ('sherpa.astro.io', logging.INFO, msg7)

    assert len(caplog.records) == 7
Esempio n. 11
0
def test_paramprompt_single_parameter_max_works(caplog):

    s = Session()
    s._add_model_types(sherpa.models.basic)

    s.paramprompt(True)
    assert len(caplog.records) == 0

    with SherpaVerbosity('INFO'):
        with patch("sys.stdin", StringIO(",,100")):
            s.set_source("scale1d.bob")

    assert len(caplog.records) == 0
    mdl = s.get_model_component('bob')
    assert mdl.c0.val == pytest.approx(1)
    assert mdl.c0.min < -3e38
    assert mdl.c0.max == pytest.approx(100)

    # remove the bob symbol from the global table
    s.clean()
Esempio n. 12
0
report('pha.get_filter()')

pha.units = 'channel'
report('pha.get_filter()')
plot.prepare(pha)
plot.plot(xlog=True, ylog=True)
pha.units = 'energy'
savefig('pha_filtered_channel.png')

# Grouping

os.chdir(basedir)
print(f'--> jumping to {basedir}')
from sherpa.utils.logging import SherpaVerbosity
with SherpaVerbosity('ERROR'):
    pha1 = read_pha('3c273.pi')
    pha2 = read_pha('3c273.pi')
    pha3 = read_pha('3c273.pi')

os.chdir(cwd)

pha1.notice(0.5, 7)
pha2.notice(0.5, 7)
pha3.notice(0.5, 7)

pha1.ungroup()
pha3.group_counts(40)

plt.subplot(2, 1, 1)
plot.prepare(pha1)
Esempio n. 13
0
def test_write_pha_fits_with_extras_roundtrip(tmp_path, caplog):
    """PHA-I with grouping/quality/errors/header

    This covers issue #488 - that is, should the output use the correct
    data type for columns? At present the code does not.

    """

    chans = np.arange(1, 5, dtype=np.int32)
    counts = np.asarray([1, 0, 3, 2], dtype=np.int32)
    grouping = np.asarray([1, -1, 1, 1], dtype=np.int16)
    quality = np.asarray([0, 0, 0, 2], dtype=np.int16)
    etime = 1023.4
    bscal = 0.05
    ascal = np.asarray([1, 1, 0.9, 0.9])

    hdr = {"TELESCOP": "CHANDRA", "INSTRUME": "ACIS", "FILTER": "NONE",
           "CHANTYPE": "PI",
           "DETCHANS": 10,  # This intentionally does not match the data
           "OBJECT": "Made up source",
           "CORRFILE": "None",
           # This will cause a warning when reading in the file
           "ANCRFILE": "made-up-ancrfile.fits",
           # "structural keywords" which match DETCHANS
           "TLMIN1": 1, "TLMAX1": 10}

    pha = DataPHA("testy",
                  chans.astype(np.float64),
                  counts.astype(np.float32),
                  grouping=grouping.astype(np.float32),
                  quality=quality.astype(np.float64),
                  exposure=etime,
                  backscal=bscal,
                  areascal=ascal,
                  header=hdr)

    outfile = tmp_path / "out.pi"
    io.write_pha(str(outfile), pha, ascii=False, clobber=False)
    pha = None

    assert len(caplog.record_tuples) == 0

    with SherpaVerbosity("INFO"):
        inpha = io.read_pha(str(outfile))

    assert len(caplog.record_tuples) == 1
    lname, lvl, msg = caplog.record_tuples[0]
    assert lname == "sherpa.astro.io"
    assert lvl == logging.WARNING

    # message depends on the backend
    if backend_is("crates"):
        assert msg.startswith("File ")
        assert msg.endswith("/made-up-ancrfile.fits does not exist.")
    elif backend_is("pyfits"):
        assert msg.startswith("file '")
        assert msg.endswith("/made-up-ancrfile.fits' not found")

    assert isinstance(inpha, DataPHA)
    assert inpha.channel == pytest.approx(chans)
    assert inpha.counts == pytest.approx(counts)
    assert inpha.grouping == pytest.approx(grouping)
    assert inpha.quality == pytest.approx(quality)
    assert inpha.exposure == pytest.approx(etime)
    assert inpha.backscal == pytest.approx(bscal)
    assert inpha.areascal == pytest.approx(ascal)
    for field in ["staterror", "syserror", "bin_lo", "bin_hi"]:
        assert getattr(inpha, field) is None

    assert inpha.grouped
    assert not inpha.subtracted
    assert inpha.units == "channel"
    assert inpha.rate
    assert inpha.plot_fac == 0
    assert inpha.response_ids == []
    assert inpha.background_ids == []

    if backend_is("crates"):
        check_write_pha_fits_with_extras_roundtrip_crates(outfile, etime, bscal)

    elif backend_is("pyfits"):
        check_write_pha_fits_with_extras_roundtrip_pyfits(outfile, etime, bscal)

    else:
        raise RuntimeError(f"Unknown io backend: {io.backend}")