Skip to content

Commit

Permalink
added instantiate_from_parameters in ModelMap
Browse files Browse the repository at this point in the history
  • Loading branch information
hiyoneda committed Jun 7, 2024
1 parent cd6eb66 commit b8e2a80
Showing 1 changed file with 29 additions and 13 deletions.
42 changes: 29 additions & 13 deletions cosipy/image_deconvolution/modelmap.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import warnings
import astropy.units as u
import numpy as np
import healpy as hp
import copy

import logging
logger = logging.getLogger(__name__)

from histpy import Histogram, Axes, Axis, HealpixAxis

from cosipy.threeml.custom_functions import get_integrated_spectral_model
Expand Down Expand Up @@ -39,17 +41,17 @@ def __init__(self,

if energy_edges.unit != u.keV:

warnings.warn(f"The unit of the given energy_edges is {energy_edges.unit}. It is converted to keV.")
logger.warning(f"The unit of the given energy_edges is {energy_edges.unit}. It is converted to keV.")
energy_edges = energy_edges.to('keV')

self.image_axis = HealpixAxis(nside = nside,
scheme = scheme,
coordsys = coordsys,
label = label_image)
image_axis = HealpixAxis(nside = nside,
scheme = scheme,
coordsys = coordsys,
label = label_image)

self.energy_axis = Axis(edges = energy_edges, label = label_energy, scale = "log")
energy_axis = Axis(edges = energy_edges, label = label_energy, scale = "log")

axes = Axes([self.image_axis, self.energy_axis])
axes = Axes([image_axis, energy_axis])

super().__init__(axes, sparse = False, unit = 1 / u.s / u.cm**2 / u.sr) # unit might be specified in the input parameter.

Expand All @@ -70,14 +72,22 @@ def open(cls, filename, name = 'hist'):
del hist
return modelmap

def set_values_from_parameters(self, algorithm_name, parameter):
@classmethod
def instantiate_from_parameters(cls, parameter):

new = cls(nside = parameter['nside'],
energy_edges = parameter['energy_edges'] * u.keV,
scheme = parameter['scheme'],
coordsys = parameter['coordinate'])

return new

def set_values_from_parameters(self, parameter):
"""
Set the values of this model map accordinng to the specified algorithm.
Parameters
----------
algorithm_name : str
Algorithm name to fill the values.
parameter : py:class:`cosipy.config.Configurator`
Parameters for the specified algorithm.
Expand All @@ -87,8 +97,11 @@ def set_values_from_parameters(self, algorithm_name, parameter):
parameter should be {'values': [ flux value at 1st energy bin (without unit), flux value at 2nd energy bin, ...]}.
"""

algorithm_name = parameter['algorithm']
algorithm_parameter = parameter['parameter_'+algorithm_name]

if algorithm_name == "flat":
for idx, value in enumerate(parameter['values']):
for idx, value in enumerate(algorithm_parameter['values']):
self[:,idx:idx+1] = value * self.unit
# elif algorithm_name == ...
# ...
Expand Down Expand Up @@ -126,7 +139,10 @@ def mask_pixels(self, mask, fill_value = 0):
if not isinstance(fill_value, u.quantity.Quantity):
fill_value *= self.contents.unit

self[:] = np.where(mask.contents, self.contents, fill_value)
modelmap_new = copy.deepcopy(self)
modelmap_new[:] = np.where(mask.contents, modelmap_new.contents, fill_value)

return modelmap_new

def smoothing(self, fwhm = 0.0 * u.deg, sigma = None):
"""
Expand Down

0 comments on commit b8e2a80

Please sign in to comment.