From a0315b2fa19171508dfee24aa7704fecfc9cf6e6 Mon Sep 17 00:00:00 2001 From: Vatsalya Chaubey <39871718+vatch123@users.noreply.github.com> Date: Thu, 8 Aug 2019 00:33:13 +0530 Subject: [PATCH] Added the example to remove cosmic ray hits (#35) * added reduce cr example * fixed tox * Fixed deps * renamed example file * ran linters * Apply suggestions from code review Co-Authored-By: Nabil Freij * added few links * fixed docs build * Apply suggestions from code review Co-Authored-By: Nabil Freij * Apply suggestions from code review Co-Authored-By: Nabil Freij * Added note for jp2 * removed jp2 * changed parameters for better results * added parameter docs * deleted few lines * Apply suggestions from code review Co-Authored-By: Nabil Freij * Update examples/remove_CR_hits.py Co-Authored-By: Nabil Freij --- .circleci/config.yml | 2 +- changelog/35.doc.rst | 1 + examples/remove_CR_hits.py | 74 ++++++++++++++++++++++++++ setup.cfg | 2 +- sunkit_image/utils/tests/test_utils.py | 1 - sunkit_image/utils/utils.py | 9 ++-- tox.ini | 7 +++ 7 files changed, 90 insertions(+), 6 deletions(-) create mode 100644 changelog/35.doc.rst create mode 100644 examples/remove_CR_hits.py diff --git a/.circleci/config.yml b/.circleci/config.yml index 6f32aae0..2146b974 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -15,7 +15,7 @@ apt-run: &apt-install name: Install apt packages command: | sudo apt update - sudo apt install -y graphviz build-essential + sudo apt install -y graphviz permission-run: &permission-run name: Fix permssions for installing diff --git a/changelog/35.doc.rst b/changelog/35.doc.rst new file mode 100644 index 00000000..b203f126 --- /dev/null +++ b/changelog/35.doc.rst @@ -0,0 +1 @@ +Added an example on how to use `astroscrappy.detect_cosmics `__ to eliminate cosmic ray hits in solar images. diff --git a/examples/remove_CR_hits.py b/examples/remove_CR_hits.py new file mode 100644 index 00000000..78dbe55a --- /dev/null +++ b/examples/remove_CR_hits.py @@ -0,0 +1,74 @@ +""" +======================== +Removing Cosmic Ray Hits +======================== + +This example illustrates how to remove cosmic ray hits from a LASCO C2 FITS file. +using `astroscrappy.detect_cosmics `__. +Astroscrappy is a separate Python package and can be installed separately using ``pip`` or ``conda``. +""" + +import matplotlib.pyplot as plt + +from sunpy.map import Map + +import astroscrappy + +############################################################################### +# For more details on how to download and plot LASCO FITS file see +# SunPy's example `Downloading and plotting LASCO C3 data `__. +# To make this example work you need to have SunPy with all the "net" dependencies installed. + +from sunpy.net import Fido, attrs as a +from sunpy.io.file_tools import read_file + +############################################################################### +# In order to download the required FITS file, we use +# `Fido `, SunPy's downloader client. +# We need to define two search variables: a time range and the instrument. + +time_range = a.Time("2000/11/09 00:06", "2000/11/09 00:07") +instrument = a.Instrument("LASCO") +detector = a.Detector("C2") +result = Fido.search(time_range, instrument) + +downloaded_files = Fido.fetch(result[0]) +data, header = read_file(downloaded_files[0])[0] + +# Add the missing meta information to the header +header["CUNIT1"] = "arcsec" +header["CUNIT2"] = "arcsec" + +############################################################################### +# With this fix we can load it into a map and plot the results. + +lasco_map = Map(data, header) +fig1 = plt.figure() +lasco_map.plot() + +############################################################################### +# Now we will call the `astroscrappy.detect_cosmics `__ +# to remove the cosmic ray hits. +# This algorithm can perform well with both high and low noise levels in the original data. +# The function takes a `~numpy.ndarray` as input so we only pass the map data. +# This particular image has lots of high intensity cosmic ray hits which +# cannot be effectively removed by using the default set of parameters. +# So we reduce ``sigclip``, the Laplacian to noise ratio from 4.5 to 2 to mark more hits. +# We also reduce ``objlim``, the contrast between the Laplacian image and the fine structured image +# to clean the high intensity bright cosmic ray hits. +# We also modify the ``readnoise`` parameter to obtain better results. + +mask, clean_data = astroscrappy.detect_cosmics(lasco_map.data, sigclip=2, objlim=2, readnoise=4, verbose=True) +# This returns two variables - mask is a boolean array depicting whether there is +# a cosmic ray hit at that pixel, clean_data is the cleaned image after removing those +# hits. + +############################################################################### +# We can now plot the cleaned image. + +clean_map1 = Map(clean_data, lasco_map.meta) + +fig2 = plt.figure() +clean_map1.plot() + +plt.show() diff --git a/setup.cfg b/setup.cfg index 3d3f2dfb..d76b56ea 100644 --- a/setup.cfg +++ b/setup.cfg @@ -15,7 +15,7 @@ packages = find: include_package_data = True setup_requires = setuptools_scm install_requires = - sunpy==1.0.0rc2 + sunpy>=1.0.0 scikit-image [options.extras_require] diff --git a/sunkit_image/utils/tests/test_utils.py b/sunkit_image/utils/tests/test_utils.py index 7a446aad..0ccffe07 100644 --- a/sunkit_image/utils/tests/test_utils.py +++ b/sunkit_image/utils/tests/test_utils.py @@ -2,7 +2,6 @@ import pytest import astropy.units as u - from astropy.tests.helper import assert_quantity_allclose from sunkit_image.utils.utils import ( diff --git a/sunkit_image/utils/utils.py b/sunkit_image/utils/utils.py index 405d4938..e1c8abbd 100644 --- a/sunkit_image/utils/utils.py +++ b/sunkit_image/utils/utils.py @@ -4,11 +4,14 @@ import numpy as np import astropy.units as u -from sunpy.coordinates import frames from sunpy.map.maputils import all_coordinates_from_map -__all__ = ["equally_spaced_bins", "bin_edge_summary", "find_pixel_radii", - "get_radial_intensity_summary"] +__all__ = [ + "equally_spaced_bins", + "bin_edge_summary", + "find_pixel_radii", + "get_radial_intensity_summary", +] def equally_spaced_bins(inner_value=1, outer_value=2, nbins=100): diff --git a/tox.ini b/tox.ini index 126365b4..b07a7542 100644 --- a/tox.ini +++ b/tox.ini @@ -21,6 +21,13 @@ commands = [testenv:build_docs] basepython = python3.7 extras = all,docs +deps = + astroscrappy + beautifulsoup4 + drms + python-dateutil + zeep + tqdm commands = sphinx-build docs docs/_build/html -W -b html # This env requires tox-conda.