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

Add albedo correction #161

Open
wants to merge 21 commits into
base: main
Choose a base branch
from
Open

Conversation

samaloney
Copy link
Collaborator

@samaloney samaloney commented Sep 12, 2024

PR Description

A a function to read in precomputed greens table and return spectrum with albedo correction, would close #128

  • Split function at the moment every time the function is called we would make web request and load a file from disk both very slow not ideal for fitting. The idea would be split up into at least two parts depending on the use case/s. As far as I know often $\theta$ is fix so the loading and interpolation between $\theta$ can be cached. So one function to load the appropriate greens matrix file and interpolate for given angle. A second function would call the previous function and do the energy intepotalation (as this could change in a fit e.g. gain correction) and fold the given spectrum through obtained green matrix.
  • Turn in to astropy model e.g. spec | albedo (still waiting on proper units support in astroy for this work Should the model composition operator, |, support units changing astropy/astropy#17302)
  • Docs and tests

Recreated Fig 2 upper from the paper

albedo_fig

@edkontar
Copy link

I have found IDL code used in 2005 to generate Green Matrices for OSPEX and put it on github:

https://github.com/edkontar/albedo

@natsat0919
Copy link
Contributor

I have added the changes we made to the albedo model during the workshop. For now we made the model "unitless" until we resolve the astropy model unit problems with SRM. All the units-related lines are commented so it can be easily updated.

@samaloney samaloney requested a review from jajmitchell January 6, 2025 16:28
A very minor edit to the evaluate step in the Albedo class which allows unit handling to work with Scipy fitting. This was previously not working at the point of calling get_albedo_matrix, simply adding in self.theta here solves the problem.
The model can now be applied to a spectrum which can be fit by the astropy fitting routines with units handled correctly. However there is still an issue with fitting theta as a free parameter, practically this isnt an issue, however should in principle work.
The code has been restrcutred such that Astropy fitting is now fully functional and all required funtionality is contained within the class rather than externally.
Copy link
Collaborator Author

@samaloney samaloney left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One of the main reason to separate things is to make them testable with these change it will be harder to test so I wonder are they necessary?

sunkit_spex/models/physical/albedo.py Outdated Show resolved Hide resolved
sunkit_spex/models/physical/albedo.py Outdated Show resolved Hide resolved
@edkontar
Copy link

In OSPEX/drm_correct_albedo.pro, the anisotropy parameter is “a coefficient showing the ratio of the flux in observer direction to the flux downwards." A smaller value means more of an effect from albedo. A value of 1000 means very little effect. if anisotropy=1 (default) the source is isotropic. However, in the paper, the coefficient α that accounts for anisotropy of the source in the Eddington approximation – that is, "distinct but constant specific intensities in the downward and in the upward hemispheres, in the ratio α” i.e. the anisotropy described as the reciprocal of the anisotropy parameter in the OSPEX.

Should the description state the definition of "α" was assumed as in OSPEX?

class Albedo(FittableModel):
r"""
Aldedo model which adds albdeo correction to input spectrum.
Following [Kontar2006]_ using precomputed green matrices distributed as part of [SSW]_.
.. [Kontar2006] https://doi.org/10.1051/0004-6361:20053672
.. [SSW] https://www.lmsal.com/solarsoft/
Parameters
==========
energy_edges :
Energy edges associated with input spectrum
theta :
Angle between Sun-observer line and X-ray source
anisotropy :
Ratio of the flux in observer direction to the flux downwards, 1 for an isotropic source

@jajmitchell
Copy link

jajmitchell commented Jan 20, 2025

I have reverted the code back to its state before the previous commit with some small edits, the functions are now defined outside of the class and the class is used a wrapper, therefore the tests should now work, is it ok for me to push these changes? The albedo model is now functional with astropy fitters.

@samaloney
Copy link
Collaborator Author

Go for it, sounds good

The functions have been moved out of the class which now functions as a wrapper. The lru_cache method is applied to both the _calculate_albedo_matrix and _get_green_matrix functions, the outputs when fitting are identical with or without the decorators, and the code runs much quicker this way.
jajmitchell and others added 2 commits January 21, 2025 16:23
Fixes errors in the test process.

Co-authored-by: Shane Maloney <[email protected]>
@samaloney
Copy link
Collaborator Author

Ok so I think this is good to go, @natsat0919 and @jajmitchell could you review?

Copy link

@jajmitchell jajmitchell left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that this PR is ready to be merged. I am still trying to get the albedo model to work with a matrix model in astropy class format and with the thermal model in astropy format, which may require some fiddling with the unit handling, we could delay the merge here and get the unit handling all working together before merging, or merge and then deal with this in a differently titled pull request as the albedo model does function now as a stand alone and this is a wider problem. My instinct would be to merge, then open a "compound model unit handling" pull request or issue, but am easy either way.

@jajmitchell
Copy link

@samaloney, just realised that the anisotropy parameter needs to have the "name" argument added in its definition, apologies that I spotted this after adding my review, are you happy for me to push this minor change then re-review?

@samaloney
Copy link
Collaborator Author

pre-commit.ci autofix

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add albedo correction
5 participants