Completed
Push — master ( 1a1c5e...aaefb0 )
by Giacomo
23:04
created

HealpixMapROI.display()   A

Complexity

Conditions 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nop 1
1
import numpy as np
0 ignored issues
show
Coding Style introduced by
This module should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
2
import astropy.units as u
3
import healpy as hp
4
5
from threeML.exceptions.custom_exceptions import custom_warnings
6
from astromodels.core.sky_direction import SkyDirection
7
8
from hawc_hal.region_of_interest.healpix_roi_base import HealpixROIBase
9
from hawc_hal.region_of_interest.healpix_cone_roi import HealpixConeROI, _get_radians
10
from ..flat_sky_projection import FlatSkyProjection
11
12
13
class HealpixMapROI(HealpixROIBase):
0 ignored issues
show
Coding Style introduced by
This class should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
14
15
    def __init__(self, model_radius, roimap=None, roifile=None, threshold=0.5, *args, **kwargs):
0 ignored issues
show
introduced by
Keyword argument before variable positional arguments list in the definition of __init__ function
Loading history...
16
        """
17
        A cone Region of Interest defined by a healpix map (can be read from a fits file).
18
        User needs to supply a cone region (center and radius) defining the plane projection for the model map.
19
20
        Examples:
21
22
            Model map centered on (R.A., Dec) = (1.23, 4.56) in J2000 ICRS coordinate system,
23
            with a radius of 5 degrees, ROI defined in healpix map in fitsfile:
24
25
            > roi = HealpixMapROI(5.0, ra=1.23, dec=4.56, file = "myROI.fits" )
26
27
            Model map centered on (L, B) = (1.23, 4.56) (Galactic coordiantes)
28
            with a radius of 30 arcmin, ROI defined on-the-fly in healpix map:
29
30
            > roi = HealpixMapROI(30.0 * u.arcmin, l=1.23, dec=4.56, map = my_roi)
31
32
        :param model_radius: radius of the model cone. Either an astropy.Quantity instance, or a float, in which case it
33
        is assumed to be the radius in degrees
34
        :param map: healpix map containing the ROI.
35
        :param file: fits file containing a healpix map with the ROI.
36
        :param threshold: value below which pixels in the map will be set inactive (=not in ROI).
37
        :param args: arguments for the SkyDirection class of astromodels
38
        :param kwargs: keywords for the SkyDirection class of astromodels
39
        """
40
 
0 ignored issues
show
Coding Style introduced by
Trailing whitespace
Loading history...
41
        assert roifile is not None or roimap is not None, "Must supply either healpix map or fitsfile to create HealpixMapROI"
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (126/120).

This check looks for lines that are too long. You can specify the maximum line length.

Loading history...
42
43
        self._center = SkyDirection(*args, **kwargs)
44
45
        self._model_radius_radians = _get_radians(model_radius)
46
47
        self._threshold = threshold
48
49
        self.read_map(roimap=roimap, roifile=roifile)
50
51
52
    def read_map(self, roimap=None, roifile=None):
0 ignored issues
show
Coding Style introduced by
This method should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
53
        assert roifile is not None or roimap is  not None, \
54
                    "Must supply either healpix map or fits file"
55
 
0 ignored issues
show
Coding Style introduced by
Trailing whitespace
Loading history...
56
        if roimap is not None:
57
            roimap = roimap
58
            self._filename = None
59
60
        elif roifile is not None:
61
            self._filename = roifile
62
            roimap =  hp.fitsfunc.read_map(self._filename)
0 ignored issues
show
Coding Style introduced by
Exactly one space required after assignment
Loading history...
63
64
        self._roimaps = {}
65
66
        self._original_nside = hp.npix2nside(roimap.shape[0])
67
        self._roimaps[self._original_nside] = roimap
68
        
0 ignored issues
show
Coding Style introduced by
Trailing whitespace
Loading history...
69
        self.check_roi_inside_model()
70
71
72
    def check_roi_inside_model(self):
0 ignored issues
show
Coding Style introduced by
This method should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
73
74
        active_pixels = self.active_pixels(self._original_nside)
75
76
        radius = np.rad2deg(self._model_radius_radians)
77
        ra, dec = self.ra_dec_center
0 ignored issues
show
Coding Style Naming introduced by
Variable name "ra" doesn't conform to snake_case naming style ('(([a-z_][a-z0-9_]2,30)|(_[a-z0-9_]*)|(__[a-z][a-z0-9_]+__))$' pattern)

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
78
        temp_roi =  HealpixConeROI(data_radius = radius , model_radius=radius, ra=ra, dec=dec)
0 ignored issues
show
Coding Style introduced by
Exactly one space required after assignment
Loading history...
Coding Style introduced by
No space allowed around keyword argument assignment
Loading history...
Coding Style introduced by
No space allowed before comma
Loading history...
79
80
        model_pixels = temp_roi.active_pixels( self._original_nside )
0 ignored issues
show
Coding Style introduced by
No space allowed after bracket
Loading history...
Coding Style introduced by
No space allowed before bracket
Loading history...
81
82
        if not all(p in model_pixels for p in active_pixels):
83
            custom_warnings.warn("Some pixels inside your ROI are not contained in the model map.")
84
85
    def to_dict(self):
86
87
        ra, dec = self.ra_dec_center
0 ignored issues
show
Coding Style Naming introduced by
Variable name "ra" doesn't conform to snake_case naming style ('(([a-z_][a-z0-9_]2,30)|(_[a-z0-9_]*)|(__[a-z][a-z0-9_]+__))$' pattern)

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
88
89
        s = {'ROI type': type(self).__name__.split(".")[-1],
0 ignored issues
show
Coding Style Naming introduced by
Variable name "s" doesn't conform to snake_case naming style ('(([a-z_][a-z0-9_]2,30)|(_[a-z0-9_]*)|(__[a-z][a-z0-9_]+__))$' pattern)

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
90
             'ra': ra,
91
             'dec': dec,
92
             'model_radius_deg': np.rad2deg(self._model_radius_radians),
93
             'roimap': self._roimaps[self._original_nside],
94
             'threshold': self._threshold,
95
             'roifile': self._filename }
0 ignored issues
show
Coding Style introduced by
No space allowed before bracket
Loading history...
96
97
        return s
98
99
    @classmethod
100
    def from_dict(cls, data):
101
102
        return cls(data['model_radius_deg'], threshold=data['threshold'],
103
                   roimap=data['roimap'], ra=data['ra'],
104
                   dec=data['dec'], roifile=data['roifile'])
105
106
    def __str__(self):
107
108
        s = ("%s: Center (R.A., Dec) = (%.3f, %.3f), model radius: %.3f deg, threshold = %.2f" %
0 ignored issues
show
Coding Style Naming introduced by
Variable name "s" doesn't conform to snake_case naming style ('(([a-z_][a-z0-9_]2,30)|(_[a-z0-9_]*)|(__[a-z][a-z0-9_]+__))$' pattern)

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
109
              (type(self).__name__, self.ra_dec_center[0], self.ra_dec_center[1],
0 ignored issues
show
Coding Style introduced by
Wrong continued indentation (remove 1 space).
Loading history...
110
               self.model_radius.to(u.deg).value, self._threshold))
0 ignored issues
show
Bug introduced by
Module 'astropy.units' has no 'deg' member; maybe 'dex'?

This check looks for calls to members that are non-existent. These calls will fail.

The member could have been renamed or removed.

Loading history...
111
112
        if self._filename is not None: 
0 ignored issues
show
Coding Style introduced by
Trailing whitespace
Loading history...
113
            s = "%s, data ROI from %s" % (s, self._filename)
0 ignored issues
show
Coding Style Naming introduced by
Variable name "s" doesn't conform to snake_case naming style ('(([a-z_][a-z0-9_]2,30)|(_[a-z0-9_]*)|(__[a-z][a-z0-9_]+__))$' pattern)

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
114
            
0 ignored issues
show
Coding Style introduced by
Trailing whitespace
Loading history...
115
        return s
116
117
    def display(self):
118
119
        print(self)
0 ignored issues
show
Unused Code Coding Style introduced by
There is an unnecessary parenthesis after print.
Loading history...
120
121
    @property
122
    def ra_dec_center(self):
0 ignored issues
show
Coding Style introduced by
This method should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
123
124
        return self._get_ra_dec()
125
126
    @property
127
    def model_radius(self):
0 ignored issues
show
Coding Style introduced by
This method should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
128
        return self._model_radius_radians * u.rad
129
130
    @property
131
    def threshold(self):
0 ignored issues
show
Coding Style introduced by
This method should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
132
        return self._threshold
133
134
    def _get_ra_dec(self):
135
136
        lon, lat = self._center.get_ra(), self._center.get_dec()
137
138
        return lon, lat
139
140
    def _active_pixels(self, nside, ordering):
141
142
        if not nside in self._roimaps:
143
          self._roimaps[nside] = hp.ud_grade(self._roimaps[self._original_nside], nside_out=nside)
0 ignored issues
show
Coding Style introduced by
The indentation here looks off. 12 spaces were expected, but 10 were found.
Loading history...
144
145
        pixels_inside_roi = np.where(self._roimaps[nside] >= self._threshold)[0]
146
147
        return pixels_inside_roi
148
149 View Code Duplication
    def get_flat_sky_projection(self, pixel_size_deg):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
Coding Style introduced by
This method should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
150
151
        # Decide side for image
152
153
        # Compute number of pixels, making sure it is going to be even (by approximating up)
154
        npix_per_side = 2 * int(np.ceil(np.rad2deg(self._model_radius_radians) / pixel_size_deg))
155
156
        # Get lon, lat of center
157
        ra, dec = self._get_ra_dec()
0 ignored issues
show
Coding Style Naming introduced by
Variable name "ra" doesn't conform to snake_case naming style ('(([a-z_][a-z0-9_]2,30)|(_[a-z0-9_]*)|(__[a-z][a-z0-9_]+__))$' pattern)

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
158
159
        # This gets a list of all RA, Decs for an AIT-projected image of npix_per_size x npix_per_side
160
        flat_sky_proj = FlatSkyProjection(ra, dec, pixel_size_deg, npix_per_side, npix_per_side)
161
162
        return flat_sky_proj
163
0 ignored issues
show
coding-style introduced by
Trailing newlines
Loading history...
164