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
Add XSPEC table absorption model to spectral table model #824
Conversation
Please add You can directly add the file to Concerning format -- yes, I'd pretty much always would prefer formats are described over at |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I left a few inline comments.
filename = str(make_path(filename)) | ||
|
||
# Check if parameter value is in range | ||
table_param = fits.open(filename)['PARAMETERS'] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you try and use astropy.table.Table
here?
We use it almost everywhere else instead of astropy.io.fits.BinTable
directly.
To read a specific HDU:
Table.read(filename, hdu='MY_HDU_NAME')
filename = '$GAMMAPY_EXTRA/datasets/ebl/ebl_franceschini.fits.gz' | ||
table_model = TableModel.from_file(filename=filename, param=0.3) | ||
""" | ||
energy_col = 'ENERG' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You're using this energy_col = 'ENERG'
two times below, with string formatting.
IMO it would be simpler to remove it, and just use the column names directly below.
|
||
Examples | ||
-------- | ||
Fill table from a model |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To get proper syntax highlighting, you have to indent the code in the docstring by four spaces, like this:
Here's some code:
print('hello!')
fill_value=-np.Inf, | ||
bounds_error=False, | ||
kind='cubic') | ||
|
||
@classmethod | ||
def from_file(cls, filename, param): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is param
???
Mention it with one line (or add a Parameters
section) in the docstring?
…eter y_label in plot function
Hi @cdeil ,
The files are already there. The test failed on Travis because of a precision problem. Locally it works fine for me. For now I just changed the parameter value for the test.
Oké, I'll have a look at that later. Thanks for your comments, I took all of them into account! |
Doesn't it work if you pass I had a discussion with @adonath about this and we decided it might be better do rename |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Minor stuff
kind='cubic') | ||
@classmethod | ||
def from_file(cls, filename, param): | ||
"""A Table containing aborbed values from a XSPEC model |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
typo: absorbed
(?)
table_param = Table.read(filename, hdu='PARAMETERS') | ||
param_min = table_param['MINIMUM'] | ||
param_max = table_param['MAXIMUM'] | ||
assert(param >= param_min and param <= param_max), "Parameter\ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've never seen a hard line break within a string literal coded like this, So I don't know if it's "allowed". What I've seen many time is
a = " Very long string, It's so long that we need a new "
"line somewhere"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A coding rule for Gammapy (and Astropy / ... most projects): Never use assert
to check inputs, raise an exception instead.
Here:
if param_min <= param <= param_max:
raise ValueError(<message here>)
energy = energy_bounds.log_centers | ||
|
||
# Get spectrum values (no interpolation, take closest value for param) | ||
table_spectra = Table.read(filename, hdu='SPECTRA') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it ok to hardcode the hdu
names? Is this always the same for the format you're using, or is it better to pass the hdu names as kwargs?
kind='cubic') | ||
@classmethod | ||
def from_file(cls, filename, param): | ||
"""A Table containing aborbed values from a XSPEC model |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe call the method read_<something>
instead of from_file
?
E.g. read_xspec_absorbed
or read_ebl
?
I think we always use the term "read" for classmethods reading from files, or at least I'm +1 to do that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Left two minor inline comments.
File containing the XSPEC model | ||
param : float | ||
Model parameter value | ||
Examples |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think this docstring will render correctly.
You need to put an empty line before the Examples
heading, and I think also colon before the code block.
Can you please check locally with
python setup.py build_docs
open docs/_build/html/index.html
and then check this docstring on the HGPS page?
We will add automated checks for docstrings in the future, there's a discussion for Astropy generally how to best do it. For now, checking docstrings is done manually.
@requires_data('gammapy-extra') | ||
def test_table_model_from_file(): | ||
filename = '$GAMMAPY_EXTRA/datasets/ebl/ebl_franceschini.fits.gz' | ||
energy_range = (0.03, 10) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No need for local variables here IMO, since only used once.
Just pass as keyword arguments below.
I tried that and it didn't work. I didn't push a lot to investiguate where the problem was.
Done,
Fixed
Fixed
Yes, it is a standard
Fixed And multiple typos fixed. Thanks! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good to me.
@joleroi - Do you want to do a final review, or should we merge?
(I'd suggest postponing plot y-axis clean-up to a separate issue / PR)
Hi @cdeil and @joleroi ,
I added a class method to
TableModel
to import models from XSPEC (see flx2tab).Maybe we could propose this format on https://gamma-astro-data-formats.readthedocs.io/en/latest/? @cdeil, @registerrier , some volunteers?
I also made optional the logarithm scale value on the y-axis and I added a plot function because, as I understood it, the SpectralModel
plot
method was really attached to have some physical units for the y-values.Thanks!