Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix set_source output from save_all #100

Merged
merged 2 commits into from
Oct 9, 2015
Merged

Fix set_source output from save_all #100

merged 2 commits into from
Oct 9, 2015

Conversation

DougBurke
Copy link
Contributor

Release Notes

This fixes a bug whereby the save_all function would not create the necessary set_source() line. This does not fix all cases, but it does the simple PHA case such as

load_pha('src.pi')
set_source(xsphabs.gal * powlaw1d.pl)
save_all('test.out')

This is bug #97.

It also ensures that files created by save_all can be run using IPython's %run directive, by explicitly importing the sherpa.astro.ui module.

The sherpa.astro.ui.save_session function has been removed: this was never documented and was never meant to be released as it did not work correctly. This cleanup also removes the sherpa.ui.utils.loggable class.

Details

This is an extremely simplified version of PR #98 which only addresses one problem, and does not add a test suite. It is however very easy to review.

Test

With the simple test case

% cat test.py
from sherpa.astro import ui

ui.load_data(1, 'sherpa-test-data/sherpatest/threads/pha_intro/3c273.pi')

ui.notice(0.5, 6)

ui.set_source(ui.powlaw1d.pl)

pl.gamma = 0.7
pl.ampl = 1e-4

print("statistic = {}".format(ui.get_stat_name()))
print("          = {}".format(ui.calc_stat()))

then with the master branch

In [2]: %run test.py
/home/naridge/local/anaconda/envs/sherpa-save-all-simple/lib/python2.7/site-packages/IPython/kernel/__init__.py:13: ShimWarning: The `IPython.kernel` package has been deprecated. You should import from ipykernel or jupyter_client instead.
  "You should import from ipykernel or jupyter_client instead.", ShimWarning)
WARNING: failed to import sherpa.astro.xspec; XSPEC models will not be available
WARNING: systematic errors were not found in file 'sherpa-test-data/sherpatest/threads/pha_intro/3c273.pi'
statistical errors were found in file 'sherpa-test-data/sherpatest/threads/pha_intro/3c273.pi' 
but not used; to use them, re-read with use_errors=True
read ARF file sherpa-test-data/sherpatest/threads/pha_intro/3c273.arf
read RMF file sherpa-test-data/sherpatest/threads/pha_intro/3c273.rmf
WARNING: systematic errors were not found in file 'sherpa-test-data/sherpatest/threads/pha_intro/3c273_bg.pi'
statistical errors were found in file 'sherpa-test-data/sherpatest/threads/pha_intro/3c273_bg.pi' 
but not used; to use them, re-read with use_errors=True
read background file sherpa-test-data/sherpatest/threads/pha_intro/3c273_bg.pi
statistic = chi2gehrels
WARNING: data set 1 has associated backgrounds, but they have not been subtracted, nor have background models been set
          = 805.560470279

In [3]: ui.save_all('out.before')

and with this PR

In [1]: %run test.py
/home/naridge/local/anaconda/envs/sherpa-save-all-simple/lib/python2.7/site-packages/IPython/kernel/__init__.py:13: ShimWarning: The `IPython.kernel` package has been deprecated. You should import from ipykernel or jupyter_client instead.
  "You should import from ipykernel or jupyter_client instead.", ShimWarning)
WARNING: failed to import sherpa.astro.xspec; XSPEC models will not be available
WARNING: systematic errors were not found in file 'sherpa-test-data/sherpatest/threads/pha_intro/3c273.pi'
statistical errors were found in file 'sherpa-test-data/sherpatest/threads/pha_intro/3c273.pi' 
but not used; to use them, re-read with use_errors=True
read ARF file sherpa-test-data/sherpatest/threads/pha_intro/3c273.arf
read RMF file sherpa-test-data/sherpatest/threads/pha_intro/3c273.rmf
WARNING: systematic errors were not found in file 'sherpa-test-data/sherpatest/threads/pha_intro/3c273_bg.pi'
statistical errors were found in file 'sherpa-test-data/sherpatest/threads/pha_intro/3c273_bg.pi' 
but not used; to use them, re-read with use_errors=True
read background file sherpa-test-data/sherpatest/threads/pha_intro/3c273_bg.pi
statistic = chi2gehrels
WARNING: data set 1 has associated backgrounds, but they have not been subtracted, nor have background models been set
          = 805.560470279

In [2]: ui.save_all('out.after')

The difference in the output is

% diff -u out.before out.after 
--- out.before  2015-09-10 16:05:40.179118565 -0400
+++ out.after   2015-09-10 16:10:22.970712995 -0400
@@ -1,3 +1,4 @@
+from sherpa.astro.ui import *
 import numpy

 ######### Load Data Sets
@@ -90,5 +91,7 @@

 ######### Set Source, Pileup and Background Models

+set_source(1, powlaw1d.pl)
+

Note it now contains a set_source statement.

WIth the new output I can restore it and get the same statistic value back:

In [2]: execfile('out.after')
/home/naridge/local/anaconda/envs/sherpa-save-all-simple/lib/python2.7/site-packages/IPython/kernel/__init__.py:13: ShimWarning: The `IPython.kernel` package has been deprecated. You should import from ipykernel or jupyter_client instead.
  "You should import from ipykernel or jupyter_client instead.", ShimWarning)
WARNING: failed to import sherpa.astro.xspec; XSPEC models will not be available
WARNING: systematic errors were not found in file 'sherpa-test-data/sherpatest/threads/pha_intro/3c273.pi'
statistical errors were found in file 'sherpa-test-data/sherpatest/threads/pha_intro/3c273.pi' 
but not used; to use them, re-read with use_errors=True
read ARF file sherpa-test-data/sherpatest/threads/pha_intro/3c273.arf
read RMF file sherpa-test-data/sherpatest/threads/pha_intro/3c273.rmf
WARNING: systematic errors were not found in file 'sherpa-test-data/sherpatest/threads/pha_intro/3c273_bg.pi'
statistical errors were found in file 'sherpa-test-data/sherpatest/threads/pha_intro/3c273_bg.pi' 
but not used; to use them, re-read with use_errors=True
read background file sherpa-test-data/sherpatest/threads/pha_intro/3c273_bg.pi
WARNING: systematic errors were not found in file 'sherpa-test-data/sherpatest/threads/pha_intro/3c273_bg.pi'
statistical errors were found in file 'sherpa-test-data/sherpatest/threads/pha_intro/3c273_bg.pi' 
but not used; to use them, re-read with use_errors=True

In [3]: calc_stat(1)
WARNING: data set 1 has associated backgrounds, but they have not been subtracted, nor have background models been set
Out[3]: 805.560470279486

If you tried this with the original output then - as well as having to ensure Sherpa was imported - the calc_stat() call will fail because there's no source model set up (due to the missing set_source line):

In [1]: from sherpa.astro.ui import *
/home/naridge/local/anaconda/envs/sherpa-save-all-simple/lib/python2.7/site-packages/IPython/kernel/__init__.py:13: ShimWarning: The `IPython.kernel` package has been deprecated. You should import from ipykernel or jupyter_client instead.
  "You should import from ipykernel or jupyter_client instead.", ShimWarning)
WARNING: failed to import sherpa.astro.xspec; XSPEC models will not be available

In [2]: execfile('out.before')
WARNING: systematic errors were not found in file 'sherpa-test-data/sherpatest/threads/pha_intro/3c273.pi'
statistical errors were found in file 'sherpa-test-data/sherpatest/threads/pha_intro/3c273.pi' 
but not used; to use them, re-read with use_errors=True
read ARF file sherpa-test-data/sherpatest/threads/pha_intro/3c273.arf
read RMF file sherpa-test-data/sherpatest/threads/pha_intro/3c273.rmf
WARNING: systematic errors were not found in file 'sherpa-test-data/sherpatest/threads/pha_intro/3c273_bg.pi'
statistical errors were found in file 'sherpa-test-data/sherpatest/threads/pha_intro/3c273_bg.pi' 
but not used; to use them, re-read with use_errors=True
read background file sherpa-test-data/sherpatest/threads/pha_intro/3c273_bg.pi
WARNING: systematic errors were not found in file 'sherpa-test-data/sherpatest/threads/pha_intro/3c273_bg.pi'
statistical errors were found in file 'sherpa-test-data/sherpatest/threads/pha_intro/3c273_bg.pi' 
but not used; to use them, re-read with use_errors=True

In [3]: calc_stat(1)
---------------------------------------------------------------------------
IdentifierErr                             Traceback (most recent call last)
<ipython-input-3-b8cf30f8d7e5> in <module>()
----> 1 calc_stat(1)

<string> in calc_stat(id, *otherids)

/home/naridge/data/my-sherpa/sherpa-save-all-simple/sherpa/ui/utils.pyc in calc_stat(self, id, *otherids)
   7825 
   7826         """
-> 7827         ids, f = self._get_fit(id, otherids)
   7828         return f.calc_stat()
   7829 

/home/naridge/data/my-sherpa/sherpa-save-all-simple/sherpa/ui/utils.pyc in _get_fit(self, id, otherids, estmethod)
   7434     def _get_fit(self, id, otherids=(), estmethod=None):
   7435 
-> 7436         fit_to_ids, datasets, models = self._prepare_fit(id, otherids)
   7437 
   7438         self._add_extra_data_and_models(fit_to_ids, datasets, models)

/home/naridge/data/my-sherpa/sherpa-save-all-simple/sherpa/ui/utils.pyc in _prepare_fit(self, id, otherids)
   7428         # If no data sets have models assigned to them, stop now.
   7429         if len(models) < 1:
-> 7430             raise IdentifierErr("nomodels")
   7431 
   7432         return fit_to_ids, datasets, models

IdentifierErr: model stack is empty

This fixes a bug whereby the save_all function would not create the
necessary set_source() line. This does not fix all cases, but it
does the simple PHA case such as

    load_pha('src.pi')
    set_source(xsphabs.gal * powlaw1d.pl)
    save_all('test.out')
@olaurino olaurino modified the milestone: 4.8rc1 Oct 6, 2015
@olaurino olaurino self-assigned this Oct 6, 2015
@olaurino
Copy link
Member

olaurino commented Oct 8, 2015

@DougBurke could you please review the latest commit and give me your blessing? Thanks!

@DougBurke
Copy link
Contributor Author

@olaurino Looks good to me. I've also updated the release notes to mention this removal.

olaurino added a commit to olaurino/sherpa that referenced this pull request Oct 9, 2015
@dtnguyen2 dtnguyen2 mentioned this pull request Oct 9, 2015
@olaurino olaurino merged commit 4ba7b89 into master Oct 9, 2015
@olaurino olaurino deleted the bug/save-all-simple branch October 9, 2015 20:22
DougBurke added a commit that referenced this pull request Feb 10, 2016
The sherpa.utils.TraceCalls class is not used in Sherpa and so has
been removed. Was it part of the loggable prototype work that was
removed in PR #100? See changeset 4ba7b89
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants