sunkit-image icon indicating copy to clipboard operation
sunkit-image copied to clipboard

implements stara

Open Deus1704 opened this issue 10 months ago • 13 comments

PR Description

This PR Implements the STARA algorithm (#37) based upon @Cadair's implementation. Example way of using stara [Old]:

import astropy.units as u
import sunpy.map
from sunkit_image.stara import *

#Assuming data is present in /data
maps = sunpy.map.Map("./data/hmi*")
maps = [m.resample((1024, 1024) * u.pix) for m in maps]

segs = get_segs(maps, limb_filter_value = 10* u.percent)

plot_sunspots(segs, maps)

Outputs:

image image

Currently understanding figure tests, will add in due time. Till then suggestions are requested.

  • [x] Write figure tests
  • [x] Better function names/structure of stara API
  • [x] Changelog
  • [x] Add Example

Deus1704 avatar Mar 31 '24 02:03 Deus1704

pre-commit.ci autofix

Deus1704 avatar Mar 31 '24 02:03 Deus1704

It would be better to run the pre commit locally before you commit the changes.

nabobalis avatar Apr 01 '24 18:04 nabobalis

It would be better to run the pre commit locally before you commit the changes.

The issue is that the pre-commit on running locally is changing files which I didn't even touch

Deus1704 avatar Apr 01 '24 18:04 Deus1704

It would be better to run the pre commit locally before you commit the changes.

The issue is that the pre-commit on running locally is changing files which I didn't even touch

What files?

nabobalis avatar Apr 01 '24 18:04 nabobalis

What files?

Files like, granule.py, radial.py and their corresponding test files.

Deus1704 avatar Apr 01 '24 19:04 Deus1704

What files?

Files like, granule.py, radial.py and their corresponding test files.

What did it change?

nabobalis avatar Apr 01 '24 19:04 nabobalis

What did it change?

It changed the import order within all of these files. Specifically the position of import sunpy .

Deus1704 avatar Apr 01 '24 19:04 Deus1704

Odd, we should track that down

nabobalis avatar Apr 01 '24 19:04 nabobalis

Odd, we should track that down

I think that won't be worth it, since it was still following the general sunpy convention of having the order of imports as Core, external, internal.. [Right part of the below image] This is the change in order after isort was applied image

Deus1704 avatar Apr 01 '24 19:04 Deus1704

So all that is left is the example?

nabobalis avatar Apr 01 '24 21:04 nabobalis

So all that is left is the example?

yeah, I'll add those in some time later this week.

Deus1704 avatar Apr 01 '24 21:04 Deus1704

@nabobalis apologies for the inactivity last week, as I was busy with my lab exams..

I've now added the base example for stara in this PR. Considering the current size of this PR, I suggest adding the remaining related examples of this algorithm, such as visualizing the number of sunspots and their distribution over the years, as well as the magnetic flux strength (which would include the HMI magnetograms), in a separate PR.

Deus1704 avatar Apr 11 '24 08:04 Deus1704

@nabobalis apologies for the inactivity last week, as I was busy with my lab exams..

That is ok, your exams are more important than this PR.

I've now added the base example for stara in this PR. Considering the current size of this PR, I suggest adding the remaining related examples of this algorithm, such as visualizing the number of sunspots and their distribution over the years,

This does not seem like an example, the amount of data needed to process that would not be trivial.

as well as the magnetic flux strength (which would include the HMI magnetograms), in a separate PR.

I see no harm in adding this to the current example at the end?

nabobalis avatar Apr 11 '24 14:04 nabobalis

The doc build is failing:

<unknown>:33: WARNING: py:obj reference target not found: sunkit_image.stara.stara
<unknown>:33: WARNING: Failed to create a cross reference. A title or caption not found: 'sphx_glr_download_generated_gallery_finding_sunspots_using_stara.py'

For the first one, we need to add a new api entry, if you see https://github.com/sunpy/sunkit-image/tree/main/docs/code_ref a new file will need to be added calleds stara.rst and like other files, an automodapi line should be added.

FOr the second one, I think the filename needs to be adjusted?

nabobalis avatar May 21 '24 17:05 nabobalis

Looks like we need to bump the figure test env. I can do that in a separate PR

nabobalis avatar May 27 '24 15:05 nabobalis

This didn't mess up with the WCS when resampled:

hmi_map_original = sunpy.map.Map('hmi.ic_45s.2024.05.08_165530_TAI.continuum.fits')
hmi_downsampled = hmi_map_original.resample((128, 128) * u.pixel)
hmi_downsampled.peek()

image

Deus1704 avatar May 31 '24 17:05 Deus1704

Can you see if that works with stara?

nabobalis avatar May 31 '24 17:05 nabobalis

Can you see if that works with stara?

This (128*128) one particularly has some issue with the stara. The error is RuntimeError: footprint array has incorrect shape. I'll try finding a workaround/reason for this error. Also this file(138KB) is slightly above the threshold size limit of 100KB

Deus1704 avatar May 31 '24 17:05 Deus1704

Can you see if that works with stara?

This (128*128) one particularly has some issue with the stara. The error is RuntimeError: footprint array has incorrect shape. I'll try finding a workaround/reason for this error. Also this file(138KB) is slightly above the threshold size limit of 100KB

What is the full trace back?

nabobalis avatar May 31 '24 18:05 nabobalis

What is the full trace back?

RuntimeError                              Traceback (most recent call last)
/workspace/sunkit-image/rand.ipynb Cell 49 line 1
----> 1 segs= stara(hmi_hard, limb_filter=20*u.percent)

File /workspace/.pyenv_mirror/user/current/lib/python3.12/site-packages/astropy/units/decorators.py:313, in QuantityInput.__call__.<locals>.wrapper(*func_args, **func_kwargs)
    311 # Call the original function with any equivalencies in force.
    312 with add_enabled_equivalencies(self.equivalencies):
--> 313     return_ = wrapped_function(*func_args, **func_kwargs)
    315 # Return
    316 ra = wrapped_signature.return_annotation

File /workspace/sunkit-image/sunkit_image/stara.py:71, in stara(smap, circle_radius, median_box, threshold, limb_filter)
     69 # Median filter to remove detections based on hot pixels
     70 m_pix = int((median_box / smap.scale[0]).to_value(u.pix))
---> 71 med = median(data, square(m_pix), behavior="ndimage")
     73 # Construct the pixel structuring element
     74 c_pix = int((circle_radius / smap.scale[0]).to_value(u.pix))

File /workspace/.pyenv_mirror/user/current/lib/python3.12/site-packages/skimage/filters/_median.py:80, in median(image, footprint, out, mode, cval, behavior)
     78 if footprint is None:
     79     footprint = ndi.generate_binary_structure(image.ndim, image.ndim)
---> 80 return ndi.median_filter(
     81     image, footprint=footprint, output=out, mode=mode, cval=cval
     82 )

File /workspace/.pyenv_mirror/user/current/lib/python3.12/site-packages/scipy/ndimage/_filters.py:1588, in median_filter(input, size, footprint, output, mode, cval, origin, axes)
   1541 @_ni_docstrings.docfiller
   1542 def median_filter(input, size=None, footprint=None, output=None,
   1543                   mode="reflect", cval=0.0, origin=0, *, axes=None):
   1544     """
   1545     Calculate a multidimensional median filter.
   1546 
   (...)
   1586     >>> plt.show()
   1587     """
-> 1588     return _rank_filter(input, 0, size, footprint, output, mode, cval,
   1589                         origin, 'median', axes=axes)

File /workspace/.pyenv_mirror/user/current/lib/python3.12/site-packages/scipy/ndimage/_filters.py:1448, in _rank_filter(input, rank, size, footprint, output, mode, cval, origin, operation, axes)
   1446 fshape = [ii for ii in footprint.shape if ii > 0]
   1447 if len(fshape) != input.ndim:
-> 1448     raise RuntimeError('footprint array has incorrect shape.')
   1449 for origin, lenf in zip(origins, fshape):
   1450     if (lenf // 2 + origin < 0) or (lenf // 2 + origin >= lenf):

RuntimeError: footprint array has incorrect shape.

Deus1704 avatar May 31 '24 18:05 Deus1704

Can you check if its 256 by 256 or 512 by 512?

Is the code only going to work if its 4k by 4k?

nabobalis avatar May 31 '24 18:05 nabobalis

Can you check if its 256 by 256 or 512 by 512?

Is the code only going to work if its 4k by 4k?

It works if its anything >= 256 by 256 pixels.

Deus1704 avatar May 31 '24 19:05 Deus1704

Can you check if its 256 by 256 or 512 by 512? Is the code only going to work if its 4k by 4k?

It works if its anything >= 256 by 256 pixels.

Should a comment to the function about this.

So how large is the HMI image if its 256 by 256?

Could we store the 128 by 128 file and then just upscale it?

nabobalis avatar May 31 '24 19:05 nabobalis

Should a comment to the function about this.

Comment like "the data array provided to the stara should be larger than 128*128 pixels" ?

So how large is the HMI image if its 256 by 256?

523KB

Could we store the 128 by 128 file and then just upscale it?

This is really great idea, it works! Only issue with 128 by 128 pixels now would be the size of it (138KB). But we could still create 64 by 64 (which would be around 42KB)and then upsample it. Tried this, it works too.

Deus1704 avatar May 31 '24 19:05 Deus1704

Should a comment to the function about this.

Comment like "the data array provided to the stara should be larger than 128*128 pixels" ?

That works.

So how large is the HMI image if its 256 by 256?

523KB

That is too large.

Could we store the 128 by 128 file and then just upscale it?

This is really great idea, it works! Only issue with 128 by 128 pixels now would be the size of it (138KB). But we could still create 64 by 64 (which would be around 42KB)and then upsample it. Tried this, it works too.

How does the 64 by 64 one look and how does it look upscaled?

I am very curious.

nabobalis avatar May 31 '24 19:05 nabobalis

How does the 64 by 64 one look and how does it look upscaled?

I am very curious.

this is 64 by 64 image

while this is the upscaled 512 by 512 image

Deus1704 avatar May 31 '24 19:05 Deus1704

Those look really rough but as long as they work!

nabobalis avatar May 31 '24 21:05 nabobalis

I have finally updated the figure test for this. Have a look at these images created from the data array in conftest before image after image

While I have just extracted the data from a small HMI map, the data array looks big due to pre-commit's linting style. I will although try to figure out some more elegant way but for the time being this would be fine?

Deus1704 avatar Jun 06 '24 21:06 Deus1704

Could the data be a FITS file instead?

nabobalis avatar Jun 06 '24 21:06 nabobalis

Could the data be a FITS file instead?

yeah it can be but I thought we agreed to create a test case and not a fits file when discussed due to issues with size?

Deus1704 avatar Jun 06 '24 21:06 Deus1704