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

Update get_rfi_mask to work with Python 3.11 #17

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
11 changes: 10 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
dist
build
eggs
.eggs
parts
bin
var
Expand All @@ -28,6 +29,7 @@ pip-log.txt
nosetests.xml
*junit-*
htmlcov
.pytest_cache

# Translations
*.mo
Expand All @@ -42,4 +44,11 @@ output/*.html
output/*/index.html

# Sphinx
docs/_build
docs/_build

# Virtual environments
.virt
virt

# Media
img
4 changes: 2 additions & 2 deletions README.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
=============================
====================================================
SEEK: Signal Extraction and Emission Kartographer
=============================
====================================================

.. image:: https://travis-ci.org/cosmo-ethz/seek.png?branch=master
:target: https://travis-ci.org/cosmo-ethz/seek
Expand Down
2 changes: 1 addition & 1 deletion docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@
# Add any paths that contain custom static files (such as style sheets) here,
# relative to this directory. They are copied after the builtin static files,
# so a file named "default.css" will overwrite the builtin "default.css".
html_static_path = ['_static']
# html_static_path = ['_static']

# If not '', a 'Last updated on:' timestamp is inserted at every page bottom,
# using the given strftime format.
Expand Down
2 changes: 1 addition & 1 deletion docs/mitigation.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ RFI mitigation
================

SEEK's RFI mitigation follows the `Offringa et al. <http://arxiv.org/pdf/1002.1957v1.pdf>`_ `SumThreshold` algorithm.
It's implemented in pure Python and JIT-compiled for speed with the `HOPE <https://github.com/cosmo-ethz/hope>`_ package.
It's implemented in pure Python. It is no longer JIT-compiled with the `HOPE <https://github.com/cosmo-ethz/hope>`_ package as `HOPE` is now deprecated.

It can easily be used without of the SEEK data processing pipeline::

Expand Down
30 changes: 15 additions & 15 deletions docs/seek.rst
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
seek Package
seek package
============

:mod:`seek` Package
-------------------

.. automodule:: seek.__init__
:members:
:undoc-members:
:show-inheritance:

Subpackages
-----------

.. toctree::
:maxdepth: 4

seek.calibration
seek.config
seek.mapmaking
seek.mitigation
seek.plugins
seek.utils

seek.calibration
seek.config
seek.mapmaking
seek.mitigation
seek.plugins
seek.utils
Module contents
---------------

.. automodule:: seek
:members:
:undoc-members:
:show-inheritance:
27 changes: 27 additions & 0 deletions example_rfi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import cv2 as cv
import numpy.ma as ma

from seek.mitigation import sum_threshold

try:
spec = cv.imread("img/spec.png", cv.IMREAD_GRAYSCALE)
if spec is None:
raise FileNotFoundError()
except:
print("File not found. Try again.")
exit()

spec_masked = ma.array(spec, mask=ma.nomask)

rfi_mask = sum_threshold.get_rfi_mask(
tod=spec_masked,
mask=None,
chi_1=35000,
eta_i=[0.5],
#eta_i=[0.5, 0.55, 0.62, 0.75, 1],
normalize_standing_waves=True,
suppress_dilation=False,
plotting=True,
sm_kwargs=None,
di_kwargs=None
)
12 changes: 12 additions & 0 deletions requirements-rfi.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Requirements for the `seek.mitigation.get_rfi_mask` function
numpy==2.0.1
scipy==1.14.0

# Requirements to run the example_rfi.py file
opencv-python==4.10.0.84
matplotlib==3.9.1

# Requirements to successfully run the Makefile options
flake8
pytest
sphinx
1 change: 0 additions & 1 deletion requirements.readthedocs.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ scipy
Cython
h5py
mock
hope
pyephem
six
astropy
Expand Down
1 change: 0 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ scipy
Cython
h5py
mock
hope
pyephem
six
healpy
Expand Down
2 changes: 0 additions & 2 deletions seek/calibration/fitting.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,7 @@

import numpy as np
from scipy.optimize import curve_fit
import hope

@hope.jit
def gauss(x, a, x0, sigma, b, c):
"""
Gaussian model plus a linear background.
Expand Down
39 changes: 24 additions & 15 deletions seek/mitigation/sum_threshold.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,14 @@

author: jakeret
'''

from __future__ import print_function, division, absolute_import, unicode_literals

import warnings

import numpy as np
from scipy import ndimage

import hope

from seek.mitigation import sum_threshold_utils
from seek.utils.tod_utils import get_empty_mask
from seek.utils import filter
Expand All @@ -41,7 +42,6 @@
STRUCT_SIZE = 3


@hope.jit
def _sumthreshold(data, mask, i, chi, ds0, ds1):
"""
The operation of summing and thresholding.
Expand Down Expand Up @@ -111,7 +111,7 @@ def _run_sumthreshold(data, init_mask, eta, M, chi_i, sm_kwargs, plotting=True):
st_mask = _sumthreshold(res.T, st_mask.T, m, chi, *res.T.shape).T

if plotting:
sum_threshold_utils.plot_steps(data, st_mask, smoothed_data, res, "%s (%s)"%(eta, chi_i))
sum_threshold_utils.plot_steps(data, st_mask, smoothed_data, res, f"{eta} ({chi_i})")

return st_mask

Expand All @@ -125,7 +125,7 @@ def binary_mask_dilation(mask, struct_size_0, struct_size_1):

:return: dilated mask
"""
struct = np.ones((struct_size_0, struct_size_1), np.bool)
struct = np.ones((struct_size_0, struct_size_1), np.bool_)
return ndimage.binary_dilation(mask, structure=struct, iterations=2)


Expand Down Expand Up @@ -159,19 +159,26 @@ def get_rfi_mask(tod, mask=None, chi_1=35000, eta_i=[0.5, 0.55, 0.62, 0.75, 1],

:return mask: the mask covering the identified RFI
"""
if mask is None and not suppress_dilation:
warnings.warn("mask=None and suppress_dilation=False: Dilation will not be performed on an empty mask.", UserWarning)
suppress_dilation = True

data = tod.data

if mask is None:
mask = get_empty_mask(data.shape)

if sm_kwargs is None: sm_kwargs = get_sm_kwargs()
if sm_kwargs is None:
sm_kwargs = get_sm_kwargs()

if plotting: sum_threshold_utils.plot_moments(data)
if plotting:
sum_threshold_utils.plot_moments(data, "Time and Frequency Statistics")

if normalize_standing_waves:
data = normalize(data, mask)

if plotting: sum_threshold_utils.plot_moments(data)
if plotting:
title = "Time and Frequency Statistics After Normalizing Standing Waves"
sum_threshold_utils.plot_moments(data, title)

p = 1.5
m = np.arange(1, MAX_PIXELS)
Expand All @@ -184,13 +191,15 @@ def get_rfi_mask(tod, mask=None, chi_1=35000, eta_i=[0.5, 0.55, 0.62, 0.75, 1],

dilated_mask = st_mask
if not suppress_dilation:
if di_kwargs is None: di_kwargs = get_di_kwrags()
if di_kwargs is None:
di_kwargs = get_di_kwargs()

dilated_mask = binary_mask_dilation(dilated_mask - mask, **di_kwargs)
dilated_mask = binary_mask_dilation(np.logical_xor(dilated_mask, mask), **di_kwargs)

if plotting: sum_threshold_utils.plot_dilation(st_mask, mask, dilated_mask)
if plotting:
sum_threshold_utils.plot_dilation(st_mask, mask, dilated_mask)

return dilated_mask+mask
return dilated_mask + mask

def get_sm_kwargs(kernel_m=KERNEL_M, kernel_n=KERNEL_N, sigma_m=SIGMA_M, sigma_n=SIGMA_N):
"""
Expand All @@ -205,7 +214,7 @@ def get_sm_kwargs(kernel_m=KERNEL_M, kernel_n=KERNEL_N, sigma_m=SIGMA_M, sigma_n
"""
return dict(M=kernel_m, N=kernel_n, sigma_m=sigma_m, sigma_n=sigma_n)

def get_di_kwrags(struct_size_0=STRUCT_SIZE, struct_size_1=STRUCT_SIZE):
def get_di_kwargs(struct_size_0=STRUCT_SIZE, struct_size_1=STRUCT_SIZE):
"""
Creates a dict with the dilation keywords.

Expand All @@ -229,7 +238,7 @@ def get_sumthreshold_kwargs(params):
params.sm_sigma_m,
params.sm_sigma_n,)

di_kwargs = get_di_kwrags(params.struct_size_0, params.struct_size_1)
di_kwargs = get_di_kwargs(params.struct_size_0, params.struct_size_1)

return sm_kwargs, di_kwargs

Expand Down
Loading