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 12 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?

Comment on lines 133 to 134
@lru_cache
def _get_green_matrix(self, theta: float) -> RegularGridInterpolator:
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Why move onto the object?


self.energy_edges = kwargs.pop("energy_edges")

self._get_green_matrix(theta)
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Don't think this is needed as already cached by the decorators

Choose a reason for hiding this comment

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

Perhaps we can remove the decorator then? referencing in the init step seems to be more inline with the Astropy API. From looking over the Astropy API all model classes have their reliant functions handled internally, the previous method was encountering issues with fitting theta as a free parameter, I think at least in part because of the functions being defined outside of the class, although I could be wrong about that. Either way, this version solves that issue and fully functions with the Astropy fitters, and seemingly produces correct results, also it is more in line with the Astropy model class API. I guess it comes back to the main question of to what degree we want to align ourselves with the API

@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.
Comment on lines +99 to +102
if hasattr(theta, "unit"):
theta = Quantity(theta.value,theta.unit)
else:
theta = theta*u.deg
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Suggested change
if hasattr(theta, "unit"):
theta = Quantity(theta.value,theta.unit)
else:
theta = theta*u.deg
if not isinstance(theta, Quantity)
theta = theta*u.deg

Copy link
Contributor

Choose a reason for hiding this comment

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

hey it looks like y'all are making good progress on this, i am gonna put a couple style suggestions along these lines if that's ok. feel free to reject them if you want but yea

Fixes errors in the test process.

Co-authored-by: Shane Maloney <[email protected]>
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