hawc_hal.region_of_interest.healpix_cone_roi   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 135
Duplicated Lines 10.37 %

Importance

Changes 0
Metric Value
wmc 14
eloc 63
dl 14
loc 135
rs 10
c 0
b 0
f 0

12 Methods

Rating   Name   Duplication   Size   Complexity  
A HealpixConeROI.ra_dec_center() 0 4 1
A HealpixConeROI.model_radius() 0 3 1
A HealpixConeROI.display() 0 3 1
A HealpixConeROI.data_radius() 0 4 1
A HealpixConeROI.from_dict() 0 4 1
A HealpixConeROI._active_pixels() 0 9 1
A HealpixConeROI._get_healpix_vec() 0 7 1
A HealpixConeROI._get_ra_dec() 0 5 1
A HealpixConeROI.to_dict() 0 11 1
A HealpixConeROI.__init__() 0 26 1
A HealpixConeROI.__str__() 0 7 1
A HealpixConeROI.get_flat_sky_projection() 14 14 1

1 Function

Rating   Name   Duplication   Size   Complexity  
A _get_radians() 0 11 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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 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...
5
6
from astromodels.core.sky_direction import SkyDirection
0 ignored issues
show
introduced by
third party import "from astromodels.core.sky_direction import SkyDirection" should be placed before "from healpix_roi_base import HealpixROIBase, _RING, _NESTED"
Loading history...
7
8
from ..healpix_handling import radec_to_vec
9
from ..flat_sky_projection import FlatSkyProjection
10
11
12
def _get_radians(my_angle):
13
14
    if isinstance(my_angle, u.Quantity):
15
16
        my_angle_radians = my_angle.to(u.rad).value
17
18
    else:
19
20
        my_angle_radians = np.deg2rad(my_angle)
21
22
    return my_angle_radians
23
24
25
class HealpixConeROI(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...
26
27
    def __init__(self, data_radius, model_radius, *args, **kwargs):
28
        """
29
        A cone Region of Interest defined by a center and a radius.
30
31
        Examples:
32
33
            ROI centered on (R.A., Dec) = (1.23, 4.56) in J2000 ICRS coordinate system, with a radius of 5 degrees:
34
35
            > roi = HealpixConeROI(5.0, ra=1.23, dec=4.56)
36
37
            ROI centered on (L, B) = (1.23, 4.56) (Galactic coordiantes) with a radius of 30 arcmin:
38
39
            > roi = HealpixConeROI(30.0 * u.arcmin, l=1.23, dec=4.56)
40
41
        :param data_radius: radius of the cone. Either an astropy.Quantity instance, or a float, in which case it is assumed
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (124/120).

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

Loading history...
42
        to be the radius in degrees
43
        :param model_radius: radius of the model cone. Either an astropy.Quantity instance, or a float, in which case it
44
        is assumed to be the radius in degrees
45
        :param args: arguments for the SkyDirection class of astromodels
46
        :param kwargs: keywords for the SkyDirection class of astromodels
47
        """
48
49
        self._center = SkyDirection(*args, **kwargs)
50
51
        self._data_radius_radians = _get_radians(data_radius)
52
        self._model_radius_radians = _get_radians(model_radius)
53
54
    def to_dict(self):
55
56
        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...
57
58
        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...
59
             'ra': ra,
60
             'dec': dec,
61
             'data_radius_deg': np.rad2deg(self._data_radius_radians),
62
             'model_radius_deg': np.rad2deg(self._model_radius_radians)}
63
64
        return s
65
66
    @classmethod
67
    def from_dict(cls, data):
68
69
        return cls(data['data_radius_deg'], data['model_radius_deg'], ra=data['ra'], dec=data['dec'])
70
71
    def __str__(self):
72
73
        s = ("%s: Center (R.A., Dec) = (%.3f, %.3f), data radius = %.3f deg, model radius: %.3f deg" %
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...
74
              (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...
75
               self.data_radius.to(u.deg).value, self.model_radius.to(u.deg).value))
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...
76
77
        return s
78
79
    def display(self):
80
81
        print(self)
0 ignored issues
show
Unused Code Coding Style introduced by
There is an unnecessary parenthesis after print.
Loading history...
82
83
    @property
84
    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...
85
86
        return self._get_ra_dec()
87
88
    @property
89
    def data_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...
90
91
        return self._data_radius_radians * u.rad
92
93
    @property
94
    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...
95
        return self._model_radius_radians * u.rad
96
97
    def _get_ra_dec(self):
98
99
        lon, lat = self._center.get_ra(), self._center.get_dec()
100
101
        return lon, lat
102
103
    def _get_healpix_vec(self):
104
105
        lon, lat = self._get_ra_dec()
106
107
        vec = radec_to_vec(lon, lat)
108
109
        return vec
110
111
    def _active_pixels(self, nside, ordering):
112
113
        vec = self._get_healpix_vec()
114
115
        nest = ordering is _NESTED
116
117
        pixels_inside_cone = hp.query_disc(nside, vec, self._data_radius_radians, inclusive=False, nest=nest)
118
119
        return pixels_inside_cone
120
121 View Code Duplication
    def get_flat_sky_projection(self, pixel_size_deg):
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...
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
122
123
        # Decide side for image
124
125
        # Compute number of pixels, making sure it is going to be even (by approximating up)
126
        npix_per_side = 2 * int(np.ceil(np.rad2deg(self._model_radius_radians) / pixel_size_deg))
127
128
        # Get lon, lat of center
129
        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...
130
131
        # This gets a list of all RA, Decs for an AIT-projected image of npix_per_size x npix_per_side
132
        flat_sky_proj = FlatSkyProjection(ra, dec, pixel_size_deg, npix_per_side, npix_per_side)
133
134
        return flat_sky_proj
135
0 ignored issues
show
coding-style introduced by
Trailing newlines
Loading history...
136