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

Chimerapy function + test #45

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

Conversation

FionnStack
Copy link
Collaborator

Changed the Chimerapy function to include sphinx NumPy style Python docstrings and created a file to test the function

@FionnStack FionnStack requested a review from samaloney February 11, 2025 14:17
Copy link
Member

@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.

Nearly there just a few small things

# Assuming process_solar_images and plot_mask have been imported from your module
from chimerapy.chimera_func import process_solar_images

class TestSolarImageProcessing(unittest.TestCase):
Copy link
Member

Choose a reason for hiding this comment

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

Sorry I should have said we use pytest and generally don't use class based tests.

self.assertEqual(final_mask.shape, self.m171.data.shape)

unique_values = np.unique(final_mask)
self.assertTrue(np.all(np.isin(unique_values, [0, 1])))
Copy link
Member

Choose a reason for hiding this comment

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

We really want to test the mask matches the expected mask given the data

Comment on lines 62 to 63
if __name__ == "__main__":
unittest.main()
Copy link
Member

Choose a reason for hiding this comment

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

Remove

Suggested change
if __name__ == "__main__":
unittest.main()

Copy link
Member

Choose a reason for hiding this comment

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

Were these changes on purpose?

Copy link
Member

Choose a reason for hiding this comment

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

Maybe rename this to just chimera.py

from sunpy.map import all_coordinates_from_map, coordinate_is_on_solar_disk


def process_solar_images(m171, m193, m211):
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
def process_solar_images(m171, m193, m211):
def generate_candiate_mask(m171, m193, m211):

@FionnStack
Copy link
Collaborator Author

`C:\Users\aoife\CHIMERApy\chimerapy\tests>pytest test_chimera.py
================================================= test session starts =================================================
platform win32 -- Python 3.13.1, pytest-8.3.4, pluggy-1.5.0
rootdir: C:\Users\aoife\CHIMERApy
configfile: pyproject.toml
plugins: asdf-4.0.0, doctestplus-1.4.0
collected 1 item

test_chimera.py F [100%]

====================================================== FAILURES =======================================================
____________________________________________ test_generate_candidate_mask _____________________________________________

def test_generate_candidate_mask():

    result_mask = generate_candidate_mask(aia171, aia193, aia211)

    expected_shape = aia171.data.shape
    assert result_mask.shape == expected_shape, "Mask shape does not match expected shape."

    expected_mask = mask_map
  np.testing.assert_allclose(result_mask, expected_mask, atol=0.1)

test_chimera.py:22:


......\AppData\Local\Programs\Python\Python313\Lib\site-packages\numpy\testing_private\utils.py:1674: in compare
return np._core.numeric.isclose(x, y, rtol=rtol, atol=atol,
......\AppData\Local\Programs\Python\Python313\Lib\site-packages\numpy_core\numeric.py:2447: in isclose
result = (less_equal(abs(x-y), atol + rtol * abs(y))
......\AppData\Local\Programs\Python\Python313\Lib\site-packages\sunpy\map\mapbase.py:561: in rsub
return self.neg().add(value)
......\AppData\Local\Programs\Python\Python313\Lib\site-packages\sunpy\util\decorators.py:216: in inner
return func(instance, value)
......\AppData\Local\Programs\Python\Python313\Lib\site-packages\sunpy\map\mapbase.py:551: in add
new_data = self.quantity + value


self = <Quantity [[0., 0., 0., ..., 0., 0., 0.],
[0., 0., 0., ..., 0., 0., 0.],
[0., 0., 0., ..., 0., 0... [0., 0., 0., ..., 0., 0., 0.],
[0., 0., 0., ..., 0., 0., 0.],
[0., 0., 0., ..., 0., 0., 0.]]>
function = <ufunc 'add'>, method = 'call'
inputs = (<Quantity [[0., 0., 0., ..., 0., 0., 0.],
[0., 0., 0., ..., 0., 0., 0.],
[0., 0., 0., ..., 0., ... 0., 0., ..., 0., 0., 0.],
[0., 0., 0., ..., 0., 0., 0.],
[0., 0., 0., ..., 0., 0., 0.]]>, False)
kwargs = {}, converters = [None, None], unit = Unit(dimensionless), out = None
arrays = [array([[0., 0., 0., ..., 0., 0., 0.],
[0., 0., 0., ..., 0., 0., 0.],
[0., 0., 0., ..., 0., 0., 0.],
..., 0., 0., 0.],
[0., 0., 0., ..., 0., 0., 0.],
[0., 0., 0., ..., 0., 0., 0.]], shape=(1024, 1024)), False]
input_ = False, converter = None

def __array_ufunc__(self, function, method, *inputs, **kwargs):
    """Wrap numpy ufuncs, taking care of units.

    Parameters
    ----------
    function : callable
        ufunc to wrap.
    method : str
        Ufunc method: ``__call__``, ``at``, ``reduce``, etc.
    inputs : tuple
        Input arrays.
    kwargs : keyword arguments
        As passed on, with ``out`` containing possible quantity output.

    Returns
    -------
    result : `~astropy.units.Quantity` or `NotImplemented`
        Results of the ufunc, with the unit set properly.
    """
    # Determine required conversion functions -- to bring the unit of the
    # input to that expected (e.g., radian for np.sin), or to get
    # consistent units between two inputs (e.g., in np.add) --
    # and the unit of the result (or tuple of units for nout > 1).
    try:
        converters, unit = converters_and_unit(function, method, *inputs)

        out = kwargs.get("out")
        # Avoid loop back by turning any Quantity output into array views.
        if out is not None:
            # If pre-allocated output is used, check it is suitable.
            # This also returns array view, to ensure we don't loop back.
            if function.nout == 1:
                out = out[0]
            out_array = check_output(out, unit, inputs, function=function)
            # Ensure output argument remains a tuple.
            kwargs["out"] = (out_array,) if function.nout == 1 else out_array

        if method == "reduce" and "initial" in kwargs and unit is not None:
            # Special-case for initial argument for reductions like
            # np.add.reduce.  This should be converted to the output unit as
            # well, which is typically the same as the input unit (but can
            # in principle be different: unitless for np.equal, radian
            # for np.arctan2, though those are not necessarily useful!)
            kwargs["initial"] = self._to_own_unit(
                kwargs["initial"], check_precision=False, unit=unit
            )

        # Same for inputs, but here also convert if necessary.
        arrays = []
        for input_, converter in zip(inputs, converters):
            input_ = getattr(input_, "value", input_)
            arrays.append(converter(input_) if converter else input_)

        # Call our superclass's __array_ufunc__
      result = super().__array_ufunc__(function, method, *arrays, **kwargs)

E numpy._core._exceptions._ArrayMemoryError: Unable to allocate 8.00 MiB for an array with shape (1024, 1024) and data type float64

......\AppData\Local\Programs\Python\Python313\Lib\site-packages\astropy\units\quantity.py:658: MemoryError
================================================== warnings summary ===================================================
....\examples\paper_figures.py:227
C:\Users\aoife\CHIMERApy\examples\paper_figures.py:227: RuntimeWarning: divide by zero encountered in power
fit193v211 = 2600 * xx**-threshold_193v211

....\examples\paper_figures.py:349
C:\Users\aoife\CHIMERApy\examples\paper_figures.py:349: RuntimeWarning: divide by zero encountered in log10
d171_clipped = np.clip(np.log10(m171.data), d171_min, d171_max)

....\examples\paper_figures.py:349
C:\Users\aoife\CHIMERApy\examples\paper_figures.py:349: RuntimeWarning: invalid value encountered in log10
d171_clipped = np.clip(np.log10(m171.data), d171_min, d171_max)

....\examples\paper_figures.py:352
C:\Users\aoife\CHIMERApy\examples\paper_figures.py:352: RuntimeWarning: divide by zero encountered in log10
d193_clipped = np.clip(np.log10(m193.data), d193_min, d193_max)

....\examples\paper_figures.py:352
C:\Users\aoife\CHIMERApy\examples\paper_figures.py:352: RuntimeWarning: invalid value encountered in log10
d193_clipped = np.clip(np.log10(m193.data), d193_min, d193_max)

....\examples\paper_figures.py:355
C:\Users\aoife\CHIMERApy\examples\paper_figures.py:355: RuntimeWarning: divide by zero encountered in log10
d211_clipped = np.clip(np.log10(m211.data), d211_min, d211_max)

....\examples\paper_figures.py:355
C:\Users\aoife\CHIMERApy\examples\paper_figures.py:355: RuntimeWarning: invalid value encountered in log10
d211_clipped = np.clip(np.log10(m211.data), d211_min, d211_max)

......\AppData\Local\Programs\Python\Python313\Lib\site-packages\astropy\visualization\lupton_rgb.py:305
C:\Users\aoife\AppData\Local\Programs\Python\Python313\Lib\site-packages\astropy\visualization\lupton_rgb.py:305: RuntimeWarning: invalid value encountered in divide
fInorm = np.where(Int <= 0, 0, np.true_divide(fI, Int))

......\AppData\Local\Programs\Python\Python313\Lib\site-packages\astropy\visualization\basic_rgb.py:153
C:\Users\aoife\AppData\Local\Programs\Python\Python313\Lib\site-packages\astropy\visualization\basic_rgb.py:153: RuntimeWarning: invalid value encountered in cast
return image_rgb.astype(output_dtype)

....\examples\paper_figures.py:362
C:\Users\aoife\CHIMERApy\examples\paper_figures.py:362: RuntimeWarning: divide by zero encountered in divide
mask_171_211 = (d171_clipped_scaled / d211_clipped_scaled) >= (

....\examples\paper_figures.py:362
C:\Users\aoife\CHIMERApy\examples\paper_figures.py:362: RuntimeWarning: invalid value encountered in divide
mask_171_211 = (d171_clipped_scaled / d211_clipped_scaled) >= (

....\examples\paper_figures.py:370
C:\Users\aoife\CHIMERApy\examples\paper_figures.py:370: RuntimeWarning: divide by zero encountered in divide
mask_171_193 = (d171_clipped_scaled / d193_clipped_scaled) >= (

....\examples\paper_figures.py:370
C:\Users\aoife\CHIMERApy\examples\paper_figures.py:370: RuntimeWarning: invalid value encountered in divide
mask_171_193 = (d171_clipped_scaled / d193_clipped_scaled) >= (

chimerapy/tests/test_chimera.py::test_generate_candidate_mask
C:\Users\aoife\CHIMERApy\chimerapy\chimera.py:40: RuntimeWarning: divide by zero encountered in log10
d171_clipped = np.clip(np.log10(m171.data), d171_min, d171_max)

chimerapy/tests/test_chimera.py::test_generate_candidate_mask
C:\Users\aoife\CHIMERApy\chimerapy\chimera.py:40: RuntimeWarning: invalid value encountered in log10
d171_clipped = np.clip(np.log10(m171.data), d171_min, d171_max)

chimerapy/tests/test_chimera.py::test_generate_candidate_mask
C:\Users\aoife\CHIMERApy\chimerapy\chimera.py:43: RuntimeWarning: divide by zero encountered in log10
d193_clipped = np.clip(np.log10(m193.data), d193_min, d193_max)

chimerapy/tests/test_chimera.py::test_generate_candidate_mask
C:\Users\aoife\CHIMERApy\chimerapy\chimera.py:43: RuntimeWarning: invalid value encountered in log10
d193_clipped = np.clip(np.log10(m193.data), d193_min, d193_max)

chimerapy/tests/test_chimera.py::test_generate_candidate_mask
C:\Users\aoife\CHIMERApy\chimerapy\chimera.py:46: RuntimeWarning: divide by zero encountered in log10
d211_clipped = np.clip(np.log10(m211.data), d211_min, d211_max)

chimerapy/tests/test_chimera.py::test_generate_candidate_mask
C:\Users\aoife\CHIMERApy\chimerapy\chimera.py:46: RuntimeWarning: invalid value encountered in log10
d211_clipped = np.clip(np.log10(m211.data), d211_min, d211_max)

chimerapy/tests/test_chimera.py::test_generate_candidate_mask
C:\Users\aoife\CHIMERApy\chimerapy\chimera.py:49: RuntimeWarning: divide by zero encountered in divide
mask_171_211 = (d171_clipped_scaled / d211_clipped_scaled) >= (

chimerapy/tests/test_chimera.py::test_generate_candidate_mask
C:\Users\aoife\CHIMERApy\chimerapy\chimera.py:49: RuntimeWarning: invalid value encountered in divide
mask_171_211 = (d171_clipped_scaled / d211_clipped_scaled) >= (

chimerapy/tests/test_chimera.py::test_generate_candidate_mask
C:\Users\aoife\CHIMERApy\chimerapy\chimera.py:57: RuntimeWarning: divide by zero encountered in divide
mask_171_193 = (d171_clipped_scaled / d193_clipped_scaled) >= (

chimerapy/tests/test_chimera.py::test_generate_candidate_mask
C:\Users\aoife\CHIMERApy\chimerapy\chimera.py:57: RuntimeWarning: invalid value encountered in divide
mask_171_193 = (d171_clipped_scaled / d193_clipped_scaled) >= (

chimerapy/tests/test_chimera.py: 6294 warnings
C:\Users\aoife\AppData\Local\Programs\Python\Python313\Lib\site-packages\sunpy\map\mapbase.py:767: SunpyMetadataWarning: Could not parse unit string "1 / s" as a valid FITS unit.
See https://docs.sunpy.org/en/stable/how_to/fix_map_metadata.html for how to fix metadata before loading it with sunpy.map.Map.
See https://fits.gsfc.nasa.gov/fits_standard.html for the FITS unit standards.
warn_metadata(f'Could not parse unit string "{unit_str}" as a valid FITS unit.\n'

-- Docs: https://docs.pytest.org/en/stable/how-to/capture-warnings.html
=============================================== short test summary info ===============================================
FAILED test_chimera.py::test_generate_candidate_mask - numpy._core._exceptions._ArrayMemoryError: Unable to allocate 8.00 MiB for an array with shape (1024, 1024) and dat...
==================================== 1 failed, 6317 warnings in 189.58s (0:03:09) =====================================`

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.

2 participants