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

Continuum BG Estimation #235

Merged
merged 38 commits into from
Nov 1, 2024
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
1022c73
initial commit of base class for BG estimation
ckarwin Aug 21, 2024
be7aa96
Merge branch 'cositools:develop' into develop
ckarwin Aug 21, 2024
a63cccd
fixed bug
ckarwin Aug 21, 2024
c50841c
initial commit of continuum BG estimation tutorial
ckarwin Aug 21, 2024
e482470
updated background estimation example
ckarwin Aug 21, 2024
c2725ad
Merge branch 'cositools:develop' into develop
ckarwin Aug 23, 2024
79ed12d
added code for continuum estimation
ckarwin Aug 23, 2024
012aa2a
updated main source code
ckarwin Aug 26, 2024
579d7c9
added example notebooks
ckarwin Aug 26, 2024
ed9fd8a
test codecov
ckarwin Aug 26, 2024
19206a5
removed non-used imports
ckarwin Aug 26, 2024
e795c6e
return codecov to default
ckarwin Aug 26, 2024
ad9dac4
fixed typo in method definition
ckarwin Sep 12, 2024
1ab6a65
Update __init__.py
ckarwin Oct 25, 2024
bed3465
Update __init__.py
ckarwin Oct 25, 2024
6ee0146
Update __init__.py
ckarwin Oct 25, 2024
0bd56bf
Merge branch 'cositools:develop' into develop
ckarwin Oct 25, 2024
154992c
Update __init__.py
ckarwin Oct 25, 2024
641be06
modified example notebook
ckarwin Oct 25, 2024
c403eab
Merge branch 'cositools:develop' into develop
ckarwin Oct 25, 2024
9660015
Merge branch 'develop' of https://github.com/ckarwin/cosipy into develop
ckarwin Oct 25, 2024
c26b630
modified main example
ckarwin Oct 25, 2024
4610fd8
added background tutorial to rst file
ckarwin Oct 25, 2024
0df9e1f
Merge branch 'cositools:develop' into develop
ckarwin Oct 25, 2024
951524e
Update __init__.py
ckarwin Oct 25, 2024
5b0cc79
Update __init__.py
ckarwin Oct 25, 2024
326c676
Update __init__.py
ckarwin Oct 25, 2024
f9caf2c
Merge branch 'cositools:develop' into develop
ckarwin Oct 25, 2024
8e6c3d7
Update __init__.py
ckarwin Oct 25, 2024
a566eac
Update __init__.py
ckarwin Oct 25, 2024
6ed1222
rm
Oct 25, 2024
42b1b8e
added unit tests
ckarwin Oct 25, 2024
f913d24
Merge branch 'cositools:develop' into develop
ckarwin Oct 31, 2024
654447f
addressed comments from HK
ckarwin Oct 31, 2024
d1ed6a7
updated continuum example after comments from HK
ckarwin Oct 31, 2024
e74d50e
updated unit test
ckarwin Oct 31, 2024
f280b1d
updated unit test and added test files
ckarwin Oct 31, 2024
b3877c3
more refinements to the example notebook
ckarwin Nov 1, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion codecov.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ coverage:


comment:
hide_project_coverage: false # Show both overall and delta coverage
hide_project_coverage: false # Show both overall and delta coverage
2 changes: 2 additions & 0 deletions cosipy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,5 @@
from .spacecraftfile import SpacecraftFile

from .ts_map import FastTSMap

from .background_estimation import ContinuumEstimation
356 changes: 356 additions & 0 deletions cosipy/background_estimation/ContinuumEstimation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,356 @@
# Imports:
import os
from astropy.coordinates import SkyCoord
from astropy import units as u
from cosipy.response import FullDetectorResponse, DetectorResponse
from cosipy.spacecraftfile import SpacecraftFile
from cosipy import BinnedData
from mhealpy import HealpixMap, HealpixBase
import matplotlib.pyplot as plt
import numpy as np
from scipy.stats import norm
import numpy.ma as ma
from tqdm import tqdm
import logging
logger = logging.getLogger(__name__)

class ContinuumEstimation:

def calc_psr(self, ori_file, detector_response, coord, output_file, nside=16):
Copy link
Contributor

Choose a reason for hiding this comment

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

In the future (not in this PR), we can move this part outside this class because the point source calculation is also performed in several different classes, e.g., point source injector.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, that makes sense.


"""Calculates point source response (PSR) in Galactic coordinates.

Parameters
----------
ori_file : str
Full path to orienation file.
detector_response : str
Full path to detector response file.
coord : tuple
tuple giving Galactic longitude and latitude of source in degrees: (l,b).
nside : int, optional
nside of scatt map (default is 16).
output_file : str
Prefix of output file (will have .h5 extension).
"""

# Orientatin file:
sc_orientation = SpacecraftFile.parse_from_file(ori_file)

Check warning on line 38 in cosipy/background_estimation/ContinuumEstimation.py

View check run for this annotation

Codecov / codecov/patch

cosipy/background_estimation/ContinuumEstimation.py#L38

Added line #L38 was not covered by tests
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you use sc_orientation directly instead of the path to an orientation file?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok, done.


# Detector response:
dr = detector_response

Check warning on line 41 in cosipy/background_estimation/ContinuumEstimation.py

View check run for this annotation

Codecov / codecov/patch

cosipy/background_estimation/ContinuumEstimation.py#L41

Added line #L41 was not covered by tests

# Scatt map:
scatt_map = sc_orientation.get_scatt_map(nside = nside, coordsys = 'galactic')

Check warning on line 44 in cosipy/background_estimation/ContinuumEstimation.py

View check run for this annotation

Codecov / codecov/patch

cosipy/background_estimation/ContinuumEstimation.py#L44

Added line #L44 was not covered by tests

# Calculate PSR:
coord = coord*u.deg
coord = SkyCoord(l=coord[0],b=coord[1],frame='galactic')
with FullDetectorResponse.open(dr) as response:
self.psr = response.get_point_source_response(coord = coord, scatt_map = scatt_map)

Check warning on line 50 in cosipy/background_estimation/ContinuumEstimation.py

View check run for this annotation

Codecov / codecov/patch

cosipy/background_estimation/ContinuumEstimation.py#L47-L50

Added lines #L47 - L50 were not covered by tests

# Save:
self.psr.write(output_file + ".h5")

Check warning on line 53 in cosipy/background_estimation/ContinuumEstimation.py

View check run for this annotation

Codecov / codecov/patch

cosipy/background_estimation/ContinuumEstimation.py#L53

Added line #L53 was not covered by tests
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you return psr here? I think it would be better than saving a PSR file outside this function: users receive psr from this function and save it by themselves if they want.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, done.


return

Check warning on line 55 in cosipy/background_estimation/ContinuumEstimation.py

View check run for this annotation

Codecov / codecov/patch

cosipy/background_estimation/ContinuumEstimation.py#L55

Added line #L55 was not covered by tests

def load_psr_from_file(self, psr_file):

"""Loads point source response from h5 file.

Parameters
----------
psr_file : str
Full path to precomputed response file (.h5 file).
"""

logger.info("...loading the pre-computed image response ...")
Copy link
Contributor

Choose a reason for hiding this comment

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

-> "...loading the pre-computed point source response ..." ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Changed.

self.psr = DetectorResponse.open(psr_file)
logger.info("--> done")

Check warning on line 69 in cosipy/background_estimation/ContinuumEstimation.py

View check run for this annotation

Codecov / codecov/patch

cosipy/background_estimation/ContinuumEstimation.py#L67-L69

Added lines #L67 - L69 were not covered by tests

return

Check warning on line 71 in cosipy/background_estimation/ContinuumEstimation.py

View check run for this annotation

Codecov / codecov/patch

cosipy/background_estimation/ContinuumEstimation.py#L71

Added line #L71 was not covered by tests

def laod_full_data(self, data_file, data_yaml):
Copy link
Contributor

Choose a reason for hiding this comment

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

load (typo?)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yup, thanks! Fixed now.


"""Loads binned data to be used as a template for the background estimate.

Parameters
----------
data_file : str
Full path to binned data (must be .h5 file).
data_yaml : str
Full path to the dataIO yaml file used for binning the data.

Notes
-----
In practice, the data file used for estimating the background
should be the full dataset.

The full data binning needs to match the PSR.
"""

self.full_data = BinnedData(data_yaml)
self.full_data.load_binned_data_from_hdf5(data_file)
self.estimated_bg = self.full_data.binned_data.project('Em', 'Phi', 'PsiChi')

Check warning on line 94 in cosipy/background_estimation/ContinuumEstimation.py

View check run for this annotation

Codecov / codecov/patch

cosipy/background_estimation/ContinuumEstimation.py#L92-L94

Added lines #L92 - L94 were not covered by tests
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you move the line 92 inside continuum_bg_estimation because the background model is not yet generated at this point? And, maybe, estimated_bg can be just an output of the function continuum_bg_estimation.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.


return

Check warning on line 96 in cosipy/background_estimation/ContinuumEstimation.py

View check run for this annotation

Codecov / codecov/patch

cosipy/background_estimation/ContinuumEstimation.py#L96

Added line #L96 was not covered by tests

def mask_from_cumdist(self, psichi_map, containment, make_plots=False):

"""
Determines masked pixels from cumulative distribution of
the point source response.

Parameters
----------
psichi_map : histpy:Histogram
Point source response projected onto psichi. This can be
either a slice of Em and Phi, or the full projection. Note
that psichi is a HealpixMap axis in histpy.
containment : float
The percentage (non-inclusive) of the cumulative distribution
to use for the mask, i.e. all pixels that fall below this value
in the cumulative distribution will be masked.
make_plots : bool
Option to plot cumulative distribution.

Note
----
The cumulative distribution is an estimate of the angular
resolution measure (ARM), which is a measure of the PSF
for Compton imaging.
"""

# Get healpix map:
h = psichi_map
m = HealpixMap(base = HealpixBase(npix = h.nbins), data = h.contents)

Check warning on line 126 in cosipy/background_estimation/ContinuumEstimation.py

View check run for this annotation

Codecov / codecov/patch

cosipy/background_estimation/ContinuumEstimation.py#L125-L126

Added lines #L125 - L126 were not covered by tests

# Sort data in descending order:
sorted_data = np.sort(m)[::-1]

Check warning on line 129 in cosipy/background_estimation/ContinuumEstimation.py

View check run for this annotation

Codecov / codecov/patch

cosipy/background_estimation/ContinuumEstimation.py#L129

Added line #L129 was not covered by tests

# Calculte the cummulative distribution
cumdist = np.cumsum(sorted_data) / sum(sorted_data)

Check warning on line 132 in cosipy/background_estimation/ContinuumEstimation.py

View check run for this annotation

Codecov / codecov/patch

cosipy/background_estimation/ContinuumEstimation.py#L132

Added line #L132 was not covered by tests

# Get indices of sorted array
self.sorted_indices = np.argsort(h.contents.value)[::-1]

Check warning on line 135 in cosipy/background_estimation/ContinuumEstimation.py

View check run for this annotation

Codecov / codecov/patch

cosipy/background_estimation/ContinuumEstimation.py#L135

Added line #L135 was not covered by tests

# Define mask based on fraction of total exposure (i.e. counts):
self.arm_mask = cumdist >= containment
self.arm_mask = ~self.arm_mask

Check warning on line 139 in cosipy/background_estimation/ContinuumEstimation.py

View check run for this annotation

Codecov / codecov/patch

cosipy/background_estimation/ContinuumEstimation.py#L138-L139

Added lines #L138 - L139 were not covered by tests

# Plot cummulative distribution and corresponding masks:
if make_plots == True:
plt.plot(cumdist)
plt.title("Cumulative Distribution")
plt.xlabel("Pixel")
plt.ylabel("Fraction of Counts")
plt.savefig("cumdist.png")
plt.show()
plt.close()

Check warning on line 149 in cosipy/background_estimation/ContinuumEstimation.py

View check run for this annotation

Codecov / codecov/patch

cosipy/background_estimation/ContinuumEstimation.py#L142-L149

Added lines #L142 - L149 were not covered by tests

return

Check warning on line 151 in cosipy/background_estimation/ContinuumEstimation.py

View check run for this annotation

Codecov / codecov/patch

cosipy/background_estimation/ContinuumEstimation.py#L151

Added line #L151 was not covered by tests
Copy link
Contributor

Choose a reason for hiding this comment

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

Is it possible to return arm_mask and sorted_indices? Assigning them as member parameters might be confusing because it will be hard to know from which sliced data self.arm_mask and sorted_indices are generated. My suggestion is that arm_mask and sorted_indices are explicitly generated for each loop in continuum_bg_estimation and they are used as input parameters of simple_inpainting.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I made your suggested changes.


def simple_inpainting(self, m_data):
Copy link
Contributor

Choose a reason for hiding this comment

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

Following the above comment, can you add arm_mask and sorted_indices as input parameters?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.


"""Highly simplistic method for inpainting masked region in CDS.

This method relies on the input healpix map having a ring
ordering. For each masked pixel, it searches to the left (i.e.
lower pixel numbers) until reaching the first non-zero pixel.
It then search to the right (i.e. higher pixel numbers) until
again finding the first non-zero pixel. The mean of the two
values is used for filling in the masked pixel.

Parameters
----------
m_data : array-like
HealpixMap object, containing projection of PSR onto psichi.

Returns
-------
interp_list : array
Values for the inpainting, corresponding to the masked pixels.
"""

# Get mean of masked data for edge cases (simple solution for now):
# CK: It would be better if this were at least the mean of an
# np masked array object, but a better method is anyways needed.
masked_mean = np.mean(m_data)

Check warning on line 178 in cosipy/background_estimation/ContinuumEstimation.py

View check run for this annotation

Codecov / codecov/patch

cosipy/background_estimation/ContinuumEstimation.py#L178

Added line #L178 was not covered by tests

# Get interpolation values:
interp_list_low = []
interp_list_high = []
for i in range(0,len(self.sorted_indices[self.arm_mask])):

Check warning on line 183 in cosipy/background_estimation/ContinuumEstimation.py

View check run for this annotation

Codecov / codecov/patch

cosipy/background_estimation/ContinuumEstimation.py#L181-L183

Added lines #L181 - L183 were not covered by tests

this_index = self.sorted_indices[self.arm_mask][i]

Check warning on line 185 in cosipy/background_estimation/ContinuumEstimation.py

View check run for this annotation

Codecov / codecov/patch

cosipy/background_estimation/ContinuumEstimation.py#L185

Added line #L185 was not covered by tests

# Search left:
k = 1
search_left = True
while search_left == True:

Check warning on line 190 in cosipy/background_estimation/ContinuumEstimation.py

View check run for this annotation

Codecov / codecov/patch

cosipy/background_estimation/ContinuumEstimation.py#L188-L190

Added lines #L188 - L190 were not covered by tests

if this_index-k < 0:
logger.info("Edge case!")
interp_list_low.append(masked_mean)
search_left = False
break

Check warning on line 196 in cosipy/background_estimation/ContinuumEstimation.py

View check run for this annotation

Codecov / codecov/patch

cosipy/background_estimation/ContinuumEstimation.py#L192-L196

Added lines #L192 - L196 were not covered by tests

next_value = m_data[this_index-k]
if next_value == 0:
k += 1
if next_value != 0:
interp_list_low.append(next_value)
search_left = False

Check warning on line 203 in cosipy/background_estimation/ContinuumEstimation.py

View check run for this annotation

Codecov / codecov/patch

cosipy/background_estimation/ContinuumEstimation.py#L198-L203

Added lines #L198 - L203 were not covered by tests

# Search right:
j = 1
search_right = True
while search_right == True:

Check warning on line 208 in cosipy/background_estimation/ContinuumEstimation.py

View check run for this annotation

Codecov / codecov/patch

cosipy/background_estimation/ContinuumEstimation.py#L206-L208

Added lines #L206 - L208 were not covered by tests

if this_index+j >= len(self.psr.axes['PsiChi'].centers)-1:
Copy link
Contributor

Choose a reason for hiding this comment

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

if this_index+j >= self.psr.axes['PsiChi'].nbins-1:

Copy link
Contributor Author

Choose a reason for hiding this comment

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

More elegant, thanks! Changed.

logger.info("Edge case!")
interp_list_high.append(masked_mean)
search_right = False
break

Check warning on line 214 in cosipy/background_estimation/ContinuumEstimation.py

View check run for this annotation

Codecov / codecov/patch

cosipy/background_estimation/ContinuumEstimation.py#L210-L214

Added lines #L210 - L214 were not covered by tests

next_value = m_data[this_index+j]
if next_value == 0:
j += 1
if next_value != 0:
interp_list_high.append(next_value)
search_right = False

Check warning on line 221 in cosipy/background_estimation/ContinuumEstimation.py

View check run for this annotation

Codecov / codecov/patch

cosipy/background_estimation/ContinuumEstimation.py#L216-L221

Added lines #L216 - L221 were not covered by tests

interp_list_low = np.array(interp_list_low)
interp_list_high = np.array(interp_list_high)
interp_list = (interp_list_low + interp_list_high) / 2.0

Check warning on line 225 in cosipy/background_estimation/ContinuumEstimation.py

View check run for this annotation

Codecov / codecov/patch

cosipy/background_estimation/ContinuumEstimation.py#L223-L225

Added lines #L223 - L225 were not covered by tests

return interp_list

Check warning on line 227 in cosipy/background_estimation/ContinuumEstimation.py

View check run for this annotation

Codecov / codecov/patch

cosipy/background_estimation/ContinuumEstimation.py#L227

Added line #L227 was not covered by tests

def continuum_bg_estimation(self, data_file, data_yaml, psr_file, \
output_file, containment=0.4, make_plots=False,\
e_loop="default", s_loop="default"):

"""Estimates continuum background.

Parameters
----------
data_file : str
Full path to binned data (must be .h5 file).
data_yaml : str
Full path to the dataIO yaml file used for binning the data.
psr_file : str
Full path to point source respone file.
output_file : str
Prefix of output file for estimated background (will be
saved as .h5 file).
containment : float, optional
The percentage (non-inclusive) of the cumulative distribution
to use for the mask, i.e. all pixels that fall below this value
in the cumulative distribution will be masked. Default is 0.4.
make_plots : bool, optional
Option to make some plots of the data, response, and masks.
Default is False.
e_loop : tuple, optional
Option to pass tuple specifying which energy range to
loop over. This must coincide with the energy bins. The default
is all bins.
s_loop : tuple, optional
Option to pass tuple specifying which Phi anlge range to
loop over. This must coincide with the Phi bins. The default
is all bins.
"""

# Load data to be used for BG estimation:
self.laod_full_data(data_file,data_yaml)

Check warning on line 264 in cosipy/background_estimation/ContinuumEstimation.py

View check run for this annotation

Codecov / codecov/patch

cosipy/background_estimation/ContinuumEstimation.py#L264

Added line #L264 was not covered by tests
hiyoneda marked this conversation as resolved.
Show resolved Hide resolved

# Load point source respone:
self.load_psr_from_file(psr_file)

Check warning on line 267 in cosipy/background_estimation/ContinuumEstimation.py

View check run for this annotation

Codecov / codecov/patch

cosipy/background_estimation/ContinuumEstimation.py#L267

Added line #L267 was not covered by tests

# Defaults for energy and scattering angle loops:
if e_loop == "default":
e_loop = (0,len(self.psr.axes['Em'].centers))
if s_loop == "default":
s_loop = (0,len(self.psr.axes['Phi'].centers))

Check warning on line 273 in cosipy/background_estimation/ContinuumEstimation.py

View check run for this annotation

Codecov / codecov/patch

cosipy/background_estimation/ContinuumEstimation.py#L270-L273

Added lines #L270 - L273 were not covered by tests

# Progress bar:
e_tot = e_loop[1] - e_loop[0]
s_tot = s_loop[1] - s_loop[0]
num_lines = e_tot*s_tot
pbar = tqdm(total=num_lines)

Check warning on line 279 in cosipy/background_estimation/ContinuumEstimation.py

View check run for this annotation

Codecov / codecov/patch

cosipy/background_estimation/ContinuumEstimation.py#L276-L279

Added lines #L276 - L279 were not covered by tests

# Loop through all bins of energy and phi:
for E in range(e_loop[0],e_loop[1]):
for s in range(s_loop[0],s_loop[1]):

Check warning on line 283 in cosipy/background_estimation/ContinuumEstimation.py

View check run for this annotation

Codecov / codecov/patch

cosipy/background_estimation/ContinuumEstimation.py#L282-L283

Added lines #L282 - L283 were not covered by tests

pbar.update(1) # update progress bar
logger.info("Bin %s %s" %(str(E),str(s)))

Check warning on line 286 in cosipy/background_estimation/ContinuumEstimation.py

View check run for this annotation

Codecov / codecov/patch

cosipy/background_estimation/ContinuumEstimation.py#L285-L286

Added lines #L285 - L286 were not covered by tests

# Get PSR slice:
h = self.psr.slice[{'Em':E, 'Phi':s}].project('PsiChi')

Check warning on line 289 in cosipy/background_estimation/ContinuumEstimation.py

View check run for this annotation

Codecov / codecov/patch

cosipy/background_estimation/ContinuumEstimation.py#L289

Added line #L289 was not covered by tests

# Get mask:
self.mask_from_cumdist(h, containment, make_plots=make_plots)

Check warning on line 292 in cosipy/background_estimation/ContinuumEstimation.py

View check run for this annotation

Codecov / codecov/patch

cosipy/background_estimation/ContinuumEstimation.py#L292

Added line #L292 was not covered by tests

# Mask data:
h_data = self.full_data.binned_data.project('Em', 'Phi', 'PsiChi').slice[{'Em':E, 'Phi':s}].project('PsiChi')
m_data = HealpixMap(base = HealpixBase(npix = h_data.nbins), data = h_data.contents.todense())
m_data[self.sorted_indices[self.arm_mask]] = 0

Check warning on line 297 in cosipy/background_estimation/ContinuumEstimation.py

View check run for this annotation

Codecov / codecov/patch

cosipy/background_estimation/ContinuumEstimation.py#L295-L297

Added lines #L295 - L297 were not covered by tests

# Skip this iteration if map is all zeros:
if len(m_data[m_data[:] > 0]) == 0:
logger.info("All zeros and so skipping iteration!")
continue

Check warning on line 302 in cosipy/background_estimation/ContinuumEstimation.py

View check run for this annotation

Codecov / codecov/patch

cosipy/background_estimation/ContinuumEstimation.py#L300-L302

Added lines #L300 - L302 were not covered by tests

# Get interpolated values:
interp_list = self.simple_inpainting(m_data)

Check warning on line 305 in cosipy/background_estimation/ContinuumEstimation.py

View check run for this annotation

Codecov / codecov/patch

cosipy/background_estimation/ContinuumEstimation.py#L305

Added line #L305 was not covered by tests

# Update estimated BG:
for p in range(len(self.sorted_indices[self.arm_mask])):
self.estimated_bg[E,s,self.sorted_indices[self.arm_mask][p]] = interp_list[p]

Check warning on line 309 in cosipy/background_estimation/ContinuumEstimation.py

View check run for this annotation

Codecov / codecov/patch

cosipy/background_estimation/ContinuumEstimation.py#L308-L309

Added lines #L308 - L309 were not covered by tests

# Option to make some plots:
if make_plots == True:

Check warning on line 312 in cosipy/background_estimation/ContinuumEstimation.py

View check run for this annotation

Codecov / codecov/patch

cosipy/background_estimation/ContinuumEstimation.py#L312

Added line #L312 was not covered by tests

# Plot true response:
m_dummy = HealpixMap(base = HealpixBase(npix = h.nbins), data = h.contents)
plot,ax = m_dummy.plot('mollview')
plt.title("True Response")
plt.show()
plt.close()

Check warning on line 319 in cosipy/background_estimation/ContinuumEstimation.py

View check run for this annotation

Codecov / codecov/patch

cosipy/background_estimation/ContinuumEstimation.py#L315-L319

Added lines #L315 - L319 were not covered by tests

# Plot masked response:
m_dummy[self.sorted_indices[self.arm_mask]] = 0
plot,ax = m_dummy.plot('mollview')
plt.title("Masked Response")
plt.show()
plt.close()

Check warning on line 326 in cosipy/background_estimation/ContinuumEstimation.py

View check run for this annotation

Codecov / codecov/patch

cosipy/background_estimation/ContinuumEstimation.py#L322-L326

Added lines #L322 - L326 were not covered by tests

# Plot true data:
m_data_dummy = HealpixMap(base = HealpixBase(npix = h_data.nbins), data = h_data.contents.todense())
plot,ax = m_data_dummy.plot('mollview')
plt.title("True Data")
plt.show()
plt.close()

Check warning on line 333 in cosipy/background_estimation/ContinuumEstimation.py

View check run for this annotation

Codecov / codecov/patch

cosipy/background_estimation/ContinuumEstimation.py#L329-L333

Added lines #L329 - L333 were not covered by tests

# Plot masked data:
plot,ax = m_data.plot('mollview')
plt.title("Masked Data")
plt.show()
plt.close()

Check warning on line 339 in cosipy/background_estimation/ContinuumEstimation.py

View check run for this annotation

Codecov / codecov/patch

cosipy/background_estimation/ContinuumEstimation.py#L336-L339

Added lines #L336 - L339 were not covered by tests

# Plot masked data with interpolated values:
m_data[self.sorted_indices[self.arm_mask]] = interp_list
plot,ax = m_data.plot('mollview')
plt.title("Interpolated Data (Estimated BG)")
plt.show()
plt.close()

Check warning on line 346 in cosipy/background_estimation/ContinuumEstimation.py

View check run for this annotation

Codecov / codecov/patch

cosipy/background_estimation/ContinuumEstimation.py#L342-L346

Added lines #L342 - L346 were not covered by tests

# Close progress bar:
pbar.close()

Check warning on line 349 in cosipy/background_estimation/ContinuumEstimation.py

View check run for this annotation

Codecov / codecov/patch

cosipy/background_estimation/ContinuumEstimation.py#L349

Added line #L349 was not covered by tests

# Write estimated BG file:
logger.info("Writing file...")
self.estimated_bg.write(output_file,overwrite=True)
logger.info("Finished!")

Check warning on line 354 in cosipy/background_estimation/ContinuumEstimation.py

View check run for this annotation

Codecov / codecov/patch

cosipy/background_estimation/ContinuumEstimation.py#L352-L354

Added lines #L352 - L354 were not covered by tests

return

Check warning on line 356 in cosipy/background_estimation/ContinuumEstimation.py

View check run for this annotation

Codecov / codecov/patch

cosipy/background_estimation/ContinuumEstimation.py#L356

Added line #L356 was not covered by tests
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you return estimated_bg here? It would be better that estimated_bg is saved by users outside this function.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

1 change: 1 addition & 0 deletions cosipy/background_estimation/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .ContinuumEstimation import ContinuumEstimation
Loading
Loading