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

set_attenuation_coefficients function directly into the fnrgf function. #239

Closed
wants to merge 8 commits into from

Conversation

Abinash-bit
Copy link
Contributor

@Abinash-bit Abinash-bit commented Oct 27, 2024

PR Description

  1. Removed set_attenuation_coefficients from __all__:

    This function is now private and not exported.

  2. Integrated _set_attenuation_coefficients into fnrgf:

    The function is now a private helper function within fnrgf.

  3. Updated fnrgf to accept range_mean, range_std, and cutoff as parameters:

    These parameters are used to set the attenuation coefficients internally.

  4. Removed the public set_attenuation_coefficients function:

    It is now a private helper function _set_attenuation_coefficients.

I have written a testing script for the refactored code.

`import numpy as np
import pytest
import astropy.units as u
import sunpy.map
from sunpy.data.sample import AIA_171_IMAGE
from sunkit_image.radial import intensity_enhance, nrgf, fnrgf, rhef
from sunkit_image.utils import equally_spaced_bins


smap = sunpy.map.Map(AIA_171_IMAGE)


radial_bin_edges = equally_spaced_bins(0, 2, 10) * u.R_sun

def test_intensity_enhance():
    enhanced_map = intensity_enhance(smap, radial_bin_edges)
    assert isinstance(enhanced_map, sunpy.map.GenericMap)
    assert enhanced_map.data.shape == smap.data.shape

def test_nrgf():
    nrgf_map = nrgf(smap, radial_bin_edges)
    assert isinstance(nrgf_map, sunpy.map.GenericMap)
    assert nrgf_map.data.shape == smap.data.shape

def test_fnrgf():
    order = 3
    range_mean = [1.0, 0.0]
    range_std = [1.0, 0.0]
    cutoff = 0
    ratio_mix = [15, 1]
    fnrgf_map = fnrgf(smap, radial_bin_edges, order, range_mean, range_std, cutoff, ratio_mix)
    assert isinstance(fnrgf_map, sunpy.map.GenericMap)
    assert fnrgf_map.data.shape == smap.data.shape

def test_rhef():
    rhef_map = rhef(smap, radial_bin_edges)
    assert isinstance(rhef_map, sunpy.map.GenericMap)
    assert rhef_map.data.shape == smap.data.shape
`

output of testing script.

Screenshot 2024-10-27 171425

This fixes #220

Comment on lines +399 to +400
range_mean=None,
range_std=None,
Copy link
Contributor

Choose a reason for hiding this comment

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

Can these be set to the defaults in the private function but as a tuple instead of a list?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

`def _set_attenuation_coefficients(order, range_mean=None, range_std=None, cutoff=0):

"""
This is a helper function to Fourier Normalizing Radial Gradient Filter
(`sunkit_image.radial.fnrgf`).

This function sets the attenuation coefficients in the one of the following two manners:

If ``cutoff`` is ``0``, then it will set the attenuation coefficients as linearly decreasing between
the range ``range_mean`` for the attenuation coefficients for mean approximation and ``range_std`` for
the attenuation coefficients for standard deviation approximation.

If ``cutoff`` is not ``0``, then it will set the last ``cutoff`` number of coefficients equal to zero
while all the others the will be set as linearly decreasing as described above.

.. note::

    This function only describes some of the ways in which attenuation coefficients can be calculated.
    The optimal coefficients depends on the size and quality of image. There is no generalized formula
    for choosing them and its up to the user to choose a optimum value.

.. note::

    The returned maps have their ``plot_settings`` changed to remove the extra normalization step.

Parameters
----------
order : `int`
    The order of the Fourier approximation.
range_mean : `tuple`, optional
    A tuple of length ``2`` which contains the highest and lowest values between which the coefficients for
    mean approximation be calculated in a linearly decreasing manner.
range_std : `tuple`, optional
    A tuple of length ``2`` which contains the highest and lowest values between which the coefficients for
    standard deviation approximation be calculated in a linearly decreasing manner.
cutoff : `int`, optional
    The numbers of coefficients from the last that should be set to ``zero``.

Returns
-------
`numpy.ndarray`
    A numpy array of shape ``[2, order + 1]`` containing the attenuation coefficients for the Fourier
    coffiecients. The first row describes the attenustion coefficients for the Fourier coefficients of
    the mean approximation. The second row contains the attenuation coefficients for the Fourier coefficients
    of the standard deviation approximation.
"""




if range_std is None:
    range_std = (1.0, 0.0)
if range_mean is None:
    range_mean = (1.0, 0.0)
attenuation_coefficients = np.zeros((2, order + 1))
attenuation_coefficients[0, :] = np.linspace(range_mean[0], range_mean[1], order + 1)
attenuation_coefficients[1, :] = np.linspace(range_std[0], range_std[1], order + 1)

if cutoff > (order + 1):
    msg = "Cutoff cannot be greater than order + 1."
    raise ValueError(msg)

if cutoff != 0:
    attenuation_coefficients[:, (-1 * cutoff) :] = 0

return attenuation_coefficients`

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Is it correct now?

@nabobalis
Copy link
Contributor

Can you modify the tests in the PR please?

@Abinash-bit
Copy link
Contributor Author

Can you modify the tests in the PR please?

are you referring to this test scripts?

``import numpy as np
import pytest
import astropy.units as u
import sunpy.map
from sunpy.data.sample import AIA_171_IMAGE
from sunkit_image.radial import intensity_enhance, nrgf, fnrgf, rhef
from sunkit_image.utils import equally_spaced_bins


smap = sunpy.map.Map(AIA_171_IMAGE)


radial_bin_edges = equally_spaced_bins(0, 2, 10) * u.R_sun

def test_intensity_enhance():
    enhanced_map = intensity_enhance(smap, radial_bin_edges)
    assert isinstance(enhanced_map, sunpy.map.GenericMap)
    assert enhanced_map.data.shape == smap.data.shape

def test_nrgf():
    nrgf_map = nrgf(smap, radial_bin_edges)
    assert isinstance(nrgf_map, sunpy.map.GenericMap)
    assert nrgf_map.data.shape == smap.data.shape

def test_fnrgf():
    order = 3
    range_mean = [1.0, 0.0]
    range_std = [1.0, 0.0]
    cutoff = 0
    ratio_mix = [15, 1]
    fnrgf_map = fnrgf(smap, radial_bin_edges, order, range_mean, range_std, cutoff, ratio_mix)
    assert isinstance(fnrgf_map, sunpy.map.GenericMap)
    assert fnrgf_map.data.shape == smap.data.shape

def test_rhef():
    rhef_map = rhef(smap, radial_bin_edges)
    assert isinstance(rhef_map, sunpy.map.GenericMap)
    assert rhef_map.data.shape == smap.data.shape
``

@nabobalis
Copy link
Contributor

Well either those or changes made to the existing tests. Whatever works.

@Abinash-bit
Copy link
Contributor Author

Ok Sure..

@Abinash-bit
Copy link
Contributor Author

One last question , how can we connect? Is there any weekly calls?

@nabobalis
Copy link
Contributor

There is a weekly meeting: https://sunpy.org/about/meetings/

@Abinash-bit
Copy link
Contributor Author

I have added a testing script named radial_test.py

Screenshot 2024-10-28 191810
This is the output

@nabobalis
Copy link
Contributor

Unfortunately, a new file won't work. You have to instead modify the existing tests and add any new tests to that existing test file.

@nabobalis
Copy link
Contributor

Another PR was merged in that has created a conflict unfortunately.

@Abinash-bit
Copy link
Contributor Author

Sorry , but in today's meeting i need to discuss some doubts , because the test script failed when i added to the existing test script.

As i am new , please guide me what needs to be done to resolve the conflict also.

@nabobalis
Copy link
Contributor

Sorry , but in today's meeting i need to discuss some doubts , because the test script failed when i added to the existing test script.

Can you tell me what happened in detail?

As i am new , please guide me what needs to be done to resolve the conflict also.

Probably better to see a guide like https://www.atlassian.com/git/tutorials/using-branches/merge-conflicts or a stackoverflow answer: https://stackoverflow.com/questions/161813/how-do-i-resolve-merge-conflicts-in-a-git-repository

than for me to try and badly explain it.

@Abinash-bit
Copy link
Contributor Author

Abinash-bit commented Oct 30, 2024

  1. Error 1: ValueError: could not broadcast input array from shape (2,2) into shape (2,)
    Location: test_fnrgf function.

  2. Ensure that range_mean and range_std are passed as tuples or lists of length 2.

Error 2: TypeError: expected non-empty vector for x
Location: test_intensity_enhance_new function

The error occurs in the _fit_polynomial_to_log_radial_intensity function.

The np.polyfit function expects a non-empty vector for the x parameter, but it is receiving an empty array.

This happens because the radial_intensity array is empty, which means there are no valid data points to fit the polynomial.

Error 3: ValueError: could not broadcast input array from shape (4,4) into shape (4,)
Location: test_fnrgf_new function

Similar to Error 1, this error occurs in the _set_attenuation_coefficients function.

The range_mean parameter is expected to be a tuple or list of length 2, but it is being passed as an array of shape (4, 4).

The function tries to broadcast this array into a shape (4,), which is not possible and results in a ValueError

Error 4: RuntimeWarning: Mean of empty slice
Location: test_rhef_new function

The error occurs in the apply_upsilon function.

The np.nanmean function is called on an empty array, which results in a RuntimeWarning.

This happens because the data[here] array is empty, meaning there are no valid data points to calculate the mean.

@nabobalis
Copy link
Contributor

I am not sure I can provide more information based on your comment.
It seems to summarize the problem and where to look for issues.

Are these the failures from https://github.com/sunpy/sunkit-image/actions/runs/11568097414/job/32199475873?pr=239 ? Or happening locally?

@nabobalis
Copy link
Contributor

Ah we forgot to discuss this at the weekly meeting, sorry about that.

How do you want to proceed?

@Abinash-bit
Copy link
Contributor Author

Screenshot 2024-10-31 224231

I am getting this issue now.

@nabobalis
Copy link
Contributor

Is this with the main branch merged in?

@Abinash-bit
Copy link
Contributor Author

Yes

@Abinash-bit
Copy link
Contributor Author

I Supressed my changes , I have git cloned everything again and tried running pytest test_radial.py i am getting this error.

Screenshot 2024-11-01 085535

@nabobalis
Copy link
Contributor

Can you push the changes so we can debug together?

@nabobalis
Copy link
Contributor

I Supressed my changes , I have git cloned everything again and tried running pytest test_radial.py i am getting this error.

Screenshot 2024-11-01 085535

This means you did not re-install the library before you ran the test suite, it will cause that warning to occur.

@Abinash-bit
Copy link
Contributor Author

Screenshot 2024-11-01 103016

All the tests passed.

i have pushed the changes both in radial.py and test_radial.py

@nabobalis
Copy link
Contributor

I am unsure what happened but you seem to have changes from the main branch here but without main merged in and that has caused there to be more file conflicts. Can you try doing git pull upstream main and merging in main to see if that can be resolved?

radial_test.py Outdated
Copy link
Contributor

Choose a reason for hiding this comment

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

So this file will need to be deleted. Any useful tests here will need to be moved to the main test file used for radial functions.

@@ -248,30 +239,6 @@ def test_multifig_rhef(smap):
return fig


def test_set_attenuation_coefficients():
Copy link
Contributor

Choose a reason for hiding this comment

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

We still need to test the function, so this should be brought back.

@Abinash-bit
Copy link
Contributor Author

Hey @nabobalis please check this.

@nabobalis
Copy link
Contributor

Hey @nabobalis please check this.

Check what?

@nabobalis nabobalis closed this Nov 4, 2024
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.

One of these functions is not like the others
2 participants