Passed
Pull Request — master (#11)
by
unknown
13:49
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
from astropy.io import fits
0 ignored issues
show
Unused Code introduced by
Unused fits imported from astropy.io
Loading history...
introduced by
Imports from package astropy are not grouped
Loading history...
5
6
from threeML.exceptions.custom_exceptions import custom_warnings
7
from astromodels.core.sky_direction import SkyDirection
8
9
from healpix_roi_base import HealpixROIBase, _RING, _NESTED
0 ignored issues
show
Coding Style introduced by
Relative import 'healpix_roi_base', should be 'hawc_hal.region_of_interest.healpix_roi_base'
Loading history...
Unused Code introduced by
Unused _RING imported from healpix_roi_base
Loading history...
10
from healpix_cone_roi import HealpixConeROI, _get_radians
0 ignored issues
show
Coding Style introduced by
Relative import 'healpix_cone_roi', should be 'hawc_hal.region_of_interest.healpix_cone_roi'
Loading history...
11
from ..healpix_handling import radec_to_vec
0 ignored issues
show
Unused Code introduced by
Unused radec_to_vec imported from healpix_handling
Loading history...
12
from ..flat_sky_projection import FlatSkyProjection
13
14
15
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...
16
17
    def __init__(self, model_radius, map=None, file = None, threshold=0.5, *args, **kwargs):
0 ignored issues
show
Coding Style introduced by
No space allowed around keyword argument assignment
Loading history...
Bug Best Practice introduced by
This seems to re-define the built-in map.

It is generally discouraged to redefine built-ins as this makes code very hard to read.

Loading history...
Bug Best Practice introduced by
This seems to re-define the built-in file.

It is generally discouraged to redefine built-ins as this makes code very hard to read.

Loading history...
introduced by
Keyword argument before variable positional arguments list in the definition of __init__ function
Loading history...
18
        """
19
        A cone Region of Interest defined by a healpix map (can be read from a fits file).
20
        User needs to supply a cone region (center and radius) defining the plane projection for the model map.
21
22
        Examples:
23
24
            Model map centered on (R.A., Dec) = (1.23, 4.56) in J2000 ICRS coordinate system, with a radius of 5 degrees, ROI defined in healpix map in fitsfile:
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (161/120).

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

Loading history...
25
26
            > roi = HealpixMapROI(5.0, ra=1.23, dec=4.56, file = "myROI.fits" )
27
28
            Model map centered on (L, B) = (1.23, 4.56) (Galactic coordiantes) with a radius of 30 arcmin, ROI defined on-the-fly in healpix map:
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (145/120).

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

Loading history...
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 file is not None or map is  not None, "Must supply either healpix map or fits file to create HealpixMapROI"
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (122/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
         
0 ignored issues
show
Coding Style introduced by
Trailing whitespace
Loading history...
47
        self._threshold  = threshold
0 ignored issues
show
Coding Style introduced by
Exactly one space required before assignment
Loading history...
48
 
0 ignored issues
show
Coding Style introduced by
Trailing whitespace
Loading history...
49
        self.read_map( map=map, file=file )
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...
50
51
52
    def read_map( self, map=None, file=None ):
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...
Bug Best Practice introduced by
This seems to re-define the built-in map.

It is generally discouraged to redefine built-ins as this makes code very hard to read.

Loading history...
Bug Best Practice introduced by
This seems to re-define the built-in file.

It is generally discouraged to redefine built-ins as this makes code very hard to read.

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...
53
        assert file is not None or map is  not None, "Must supply either healpix map or fits file"
54
 
0 ignored issues
show
Coding Style introduced by
Trailing whitespace
Loading history...
55
        if map is not None:
56
            map = map
57
            self._filename = None
58
59
        elif file is not None:
60
            self._filename = file
61
            map =  hp.fitsfunc.read_map(self._filename)
0 ignored issues
show
Coding Style introduced by
Exactly one space required after assignment
Loading history...
62
63
        self._maps = {}
64
65
        self._original_nside = hp.npix2nside( map.shape[0] )
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...
66
        self._maps[self._original_nside] = map
67
        
0 ignored issues
show
Coding Style introduced by
Trailing whitespace
Loading history...
68
        self.check_roi_inside_model()
69
70
71
    def check_roi_inside_model( self ):
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...
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...
72
      
0 ignored issues
show
Coding Style introduced by
Trailing whitespace
Loading history...
73
        active_pixels = self.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...
74
        
0 ignored issues
show
Coding Style introduced by
Trailing whitespace
Loading history...
75
        radius = np.rad2deg(self._model_radius_radians)
76
        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...
77
        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 after bracket
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...
Coding Style introduced by
No space allowed before bracket
Loading history...
78
        
0 ignored issues
show
Coding Style introduced by
Trailing whitespace
Loading history...
79
        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...
80
        
0 ignored issues
show
Coding Style introduced by
Trailing whitespace
Loading history...
81
        if not all(p in model_pixels for p in active_pixels):
82
            custom_warnings.warn("Some pixels inside your ROI are not contained in the model map.")
83
84
    def to_dict(self):
85
86
        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...
87
88
        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...
89
             'ra': ra,
90
             'dec': dec,
91
             'model_radius_deg': np.rad2deg(self._model_radius_radians),
92
             'map': self._maps[self._original_nside],
93
             'threshold': self._threshold,
94
             'file': self._filename }
0 ignored issues
show
Coding Style introduced by
No space allowed before bracket
Loading history...
95
96
        return s
97
98
    @classmethod
99
    def from_dict(cls, data):
100
101
        return cls(data['model_radius_deg'], threshold = data['threshold'], map = data['map'], ra=data['ra'], dec=data['dec'], file=data['file'])
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (145/120).

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

Loading history...
Coding Style introduced by
No space allowed around keyword argument assignment
Loading history...
102
103
    def __str__(self):
104
105
        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...
106
              (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...
107
               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...
108
109
        if self._filename is not None: 
0 ignored issues
show
Coding Style introduced by
Trailing whitespace
Loading history...
110
            s  =  "%s,  data ROI from %s" %  (s, self._filename)
0 ignored issues
show
Coding Style introduced by
Exactly one space required around assignment
Loading history...
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...
111
            
0 ignored issues
show
Coding Style introduced by
Trailing whitespace
Loading history...
112
        return s
113
114
    def display(self):
115
116
        print(self)
0 ignored issues
show
Unused Code Coding Style introduced by
There is an unnecessary parenthesis after print.
Loading history...
117
118
    @property
119
    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...
120
121
        return self._get_ra_dec()
122
123
    @property
124
    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...
125
        return self._model_radius_radians * u.rad
126
127
    @property
128
    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...
129
        return self._threshold
130
131
    def _get_ra_dec(self):
132
133
        lon, lat = self._center.get_ra(), self._center.get_dec()
134
135
        return lon, lat
136
137
    def _active_pixels(self, nside, ordering):
138
139
        nest = ordering is _NESTED
0 ignored issues
show
Unused Code introduced by
The variable nest seems to be unused.
Loading history...
140
            
0 ignored issues
show
Coding Style introduced by
Trailing whitespace
Loading history...
141
        if not nside in self._maps:
142
          self._maps[nside] = hp.ud_grade(self._maps[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...
143
144
        pixels_inside_map = np.where( self._maps[ nside ] >= self._threshold )[0]
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...
145
146
        return pixels_inside_map
147
148 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...
149
150
        # Decide side for image
151
152
        # Compute number of pixels, making sure it is going to be even (by approximating up)
153
        npix_per_side = 2 * int(np.ceil(np.rad2deg(self._model_radius_radians) / pixel_size_deg))
154
155
        # Get lon, lat of center
156
        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...
157
158
        # This gets a list of all RA, Decs for an AIT-projected image of npix_per_size x npix_per_side
159
        flat_sky_proj = FlatSkyProjection(ra, dec, pixel_size_deg, npix_per_side, npix_per_side)
160
161
        return flat_sky_proj
162
0 ignored issues
show
coding-style introduced by
Trailing newlines
Loading history...
163