Passed
Pull Request — master (#17)
by
unknown
09:47
created

SparseHealpix.pixels()   A

Complexity

Conditions 1

Size

Total Lines 3
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
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 healpy as hp
3
import pandas as pd
4
from ..special_values import UNSEEN
5
6
7
def _not_implemented():  # pragma: no cover
8
9
    raise RuntimeError("You cannot use the base class. Use the derived classes.")
10
11
12
class HealpixWrapperBase(object):
13
    """
14
    A class which wrap a numpy array containing an healpix map, in order to expose always the same interface
15
    independently of whether the underlying map is sparse or dense
16
    """
17
18
    def __init__(self, sparse, nside):
19
20
        self._nside = int(nside)
21
        self._npix = hp.nside2npix(self._nside)
22
        self._pixel_area = hp.nside2pixarea(self._nside, degrees=True)
23
        self._sparse = bool(sparse)
24
25
    @property
26
    def is_sparse(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...
27
        return self._sparse
28
29
    @property
30
    def nside(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...
31
        return self._nside
32
33
    @property
34
    def npix(self):
35
        """
36
        :return: total number of pixels for this nside. Note that mymap.npix is equivalent to
37
        healpy.nside2npix(mymap.nside)
38
        """
39
        return self._npix
40
41
    @property
42
    def pixel_area(self):
43
        """
44
        :return: area (solid angle) of the healpix pixel in sq. degrees
45
        """
46
        return self._pixel_area
47
48
    def as_dense(self):  # pragma: no cover
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...
Coding Style introduced by
This method could be written as a function/class method.

If a method does not access any attributes of the class, it could also be implemented as a function or static method. This can help improve readability. For example

class Foo:
    def some_method(self, x, y):
        return x + y;

could be written as

class Foo:
    @classmethod
    def some_method(cls, x, y):
        return x + y;
Loading history...
49
50
        return _not_implemented()
51
52
    def as_partial(self):  # pragma: no cover
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...
Coding Style introduced by
This method could be written as a function/class method.

If a method does not access any attributes of the class, it could also be implemented as a function or static method. This can help improve readability. For example

class Foo:
    def some_method(self, x, y):
        return x + y;

could be written as

class Foo:
    @classmethod
    def some_method(cls, x, y):
        return x + y;
Loading history...
53
54
        return _not_implemented()
55
56
    def to_pandas(self):
57
        """
58
        Returns a pandas Series with the dense representation of the data
59
60
        :return: pd.Series, type
61
        """
62
63
        return pd.Series(self.as_partial())
64
65
66
class SparseHealpix(HealpixWrapperBase):
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...
67
68
    def __init__(self, partial_map, pixels_ids, nside, fill_value=UNSEEN):
69
70
        self._partial_map = partial_map
71
        self._pixels_ids = pixels_ids
72
        self._fill_value = fill_value
73
74
        super(SparseHealpix, self).__init__(sparse=True, nside=nside)
75
76
    def __add__(self, other_map):
77
78
        # Make sure they have the same pixels
79
        assert np.array_equal(self.pixels, other_map.pixels)
80
81
        # inflate them
82
        big_self_map = self.as_dense()
83
        
0 ignored issues
show
Coding Style introduced by
Trailing whitespace
Loading history...
84
        big_other_map = other_map.as_dense()
85
        
0 ignored issues
show
Coding Style introduced by
Trailing whitespace
Loading history...
86
        # add them
87
        dense_added = big_self_map + big_other_map
88
89
        # deflate
90
        sparse_added = SparseHealpix(dense_added[self.pixels], self.pixels, self.nside)
91
        
0 ignored issues
show
Coding Style introduced by
Trailing whitespace
Loading history...
92
        return sparse_added
93
94
    def __sub__(self, other_map):
95
96
        # Make sure they have the same pixels
97
        assert np.array_equal(self._pixels_ids, other_map._pixels_ids)
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like _pixels_ids was declared protected and should not be accessed from this context.

Prefixing a member variable _ is usually regarded as the equivalent of declaring it with protected visibility that exists in other languages. Consequentially, such a member should only be accessed from the same class or a child class:

class MyParent:
    def __init__(self):
        self._x = 1;
        self.y = 2;

class MyChild(MyParent):
    def some_method(self):
        return self._x    # Ok, since accessed from a child class

class AnotherClass:
    def some_method(self, instance_of_my_child):
        return instance_of_my_child._x   # Would be flagged as AnotherClass is not
                                         # a child class of MyParent
Loading history...
98
99
        big_other_map = other_map.as_dense()
100
        big_self_map = self.as_dense()
101
102
        subtraction = big_self_map - big_other_map
103
104
        sparse_subtracted = SparseHealpix(subtraction[self.pixels], self.pixels, self.nside)
105
106
        return sparse_subtracted
107
108
    def as_dense(self):
109
        """
110
        Returns the dense (i.e., full sky) representation of the map. Note that this means unwrapping the map,
111
        and the memory usage increases a lot.
112
113
        :return: the dense map, suitable for use with healpy routine (among other uses)
114
        """
115
116
        # Make the full Healpix map
117
        new_map = np.full(self.npix, self._fill_value)
118
119
        # Assign the active pixels their values
120
        new_map[self.pixels] = self._partial_map
121
122
        return new_map
123
124
    def as_partial(self):
125
126
        return self._partial_map
127
128
    def set_new_values(self, new_values):
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
130
        assert new_values.shape == self._partial_map.shape
131
132
        self._partial_map[:] = new_values
133
134
    @property
135
    def pixels(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...
136
        return self._pixels_ids
137
138
139
140
class DenseHealpix(HealpixWrapperBase):
141
    """
142
    A dense (fullsky) healpix map. In this case partial and complete are the same map.
143
144
    """
145
146
    def __init__(self, healpix_array):
147
148
        self._dense_map = healpix_array
149
150
        super(DenseHealpix, self).__init__(nside=hp.npix2nside(healpix_array.shape[0]), sparse=False)
151
152
    def as_dense(self):
153
        """
154
        Returns the complete (i.e., full sky) representation of the map. Since this is a dense map, this is identical
155
        to the input map
156
157
        :return: the complete map, suitable for use with healpy routine (among other uses)
158
        """
159
160
        return self._dense_map
161
162
    def as_partial(self):
163
164
        return self._dense_map
165
166
    def set_new_values(self, new_values):
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...
167
168
        assert new_values.shape == self._dense_map.shape
169
170
        self._dense_map[:] = new_values
171
0 ignored issues
show
coding-style introduced by
Trailing newlines
Loading history...
172