def test_get_bkg_model_plot(idval): """Basic testing of get_bkg_model_plot """ setup_example_bkg_model(idval) if idval is None: bp = ui.get_bkg_model_plot() else: bp = ui.get_bkg_model_plot(idval) print(bp) assert bp.xlo == pytest.approx(_data_chan) assert bp.xhi == pytest.approx(_data_chan + 1) # TODO: this is the same output as test_get_bkg_model_plot_energy, # which doesn't make sense. yexp = _arf / (_bexpscale * 100) assert bp.y == pytest.approx(yexp) assert bp.title == 'Model' assert bp.xlabel == 'Channel' assert bp.ylabel == 'Counts/sec/channel'
def test_get_bkg_model_plot_energy(idval): """Basic testing of get_bkg_model_plot: energy """ # The way I have set up the data means that set_analysis # doesn't seem to change the setting for the background, # which should be tracked down (Sep 2019) but not just now. # setup_example_bkg_model(idval) if idval is None: ui.set_analysis('energy') ui.get_bkg().units = 'energy' bp = ui.get_bkg_model_plot() else: ui.set_analysis(idval, 'energy') ui.get_bkg(idval).units = 'energy' bp = ui.get_bkg_model_plot(idval) # TODO: is this a bug in the plotting code, or does it just # indicate that the test hasn't set up the correct invariants # (which may be true as the code above has to change the units # setting of the background object)? # # I was expecting bp.x to return energy and not channel values # assert bp.xlo == pytest.approx(_data_chan - 0.5) assert bp.xhi == pytest.approx(_data_chan + 0.5) # TODO: The factor of 100 comes from the bin width (0.1 keV), but # why is there a scaling by _bexpscale? yexp = _arf / (_bexpscale * 100) assert bp.y == pytest.approx(yexp) assert bp.title == 'Model' assert bp.xlabel == 'Energy (keV)' assert bp.ylabel == 'Counts/sec/keV'
def qq_export(id=None, bkg=False, outfile='qq.txt', elow=None, ehigh=None): """ Export Q-Q plot into a file for plotting. :param id: spectrum id to use (see get_bkg_plot/get_data_plot) :param bkg: whether to use get_bkg_plot or get_data_plot :param outfile: filename to write results into :param elow: low energy limit :param ehigh: low energy limit Example:: qq.qq_export('bg', outfile='my_bg_qq', elow=0.2, ehigh=10) """ # data d = ui.get_bkg_plot(id=id) if bkg else ui.get_data_plot(id=id) e = d.x mask = logical_and(e >= elow, e <= ehigh) data = d.y[mask].cumsum() d = ui.get_bkg_model_plot(id=id) if bkg else ui.get_model_plot(id=id) e = d.xlo mask = logical_and(e >= elow, e <= ehigh) e = e[mask] model = d.y[mask].cumsum() last_stat = ui.get_stat() ui.set_stat(ksstat) ks = ui.calc_stat() ui.set_stat(cvmstat) cvm = ui.calc_stat() ui.set_stat(adstat) ad = ui.calc_stat() ui.set_stat(last_stat) ad = ui.calc_stat() ui.set_stat('chi2gehrels') chi2 = ui.calc_stat() ui.set_stat('cstat') cstat = ui.calc_stat() ui.set_stat(last_stat) stats = dict(ks=ks, cvm=cvm, ad=ad, cstat=cstat, chi2=chi2) numpy.savetxt(outfile, numpy.transpose([e, data, model])) json.dump(stats, open(outfile + '.json', 'w'), indent=4)