Passed
Push — master ( bc1462...77290c )
by Giacomo
11:38
created

SparseHealpix.__sub__()   A

Complexity

Conditions 1

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 10
rs 10
c 0
b 0
f 0
cc 1
nop 2
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_ids, other_map.pixels_ids)
80
81
        added = self.as_partial() + other_map.as_partial()
82
83
        sparse_added = SparseHealpix(added, self._pixels_ids, self.nside)
84
        
0 ignored issues
show
Coding Style introduced by
Trailing whitespace
Loading history...
85
        return sparse_added
86
87
    def __sub__(self, other_map):
88
89
        # Make sure they have the same pixels
90
        assert np.array_equal(self._pixels_ids, other_map.pixels_ids)
91
92
        subtraction = self.as_partial() - other_map.as_partial()
93
94
        sparse_subtracted = SparseHealpix(subtraction, self._pixels_ids, self.nside)
95
96
        return sparse_subtracted
97
98
    def as_dense(self):
99
        """
100
        Returns the dense (i.e., full sky) representation of the map. Note that this means unwrapping the map,
101
        and the memory usage increases a lot.
102
103
        :return: the dense map, suitable for use with healpy routine (among other uses)
104
        """
105
106
        # Make the full Healpix map
107
        new_map = np.full(self.npix, self._fill_value)
108
109
        # Assign the active pixels their values
110
        new_map[self._pixels_ids] = self._partial_map
111
112
        return new_map
113
114
    def as_partial(self):
115
116
        return self._partial_map
117
118
    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...
119
120
        assert new_values.shape == self._partial_map.shape
121
122
        self._partial_map[:] = new_values
123
124
    @property
125
    def pixels_ids(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...
126
        return self._pixels_ids
127
128
129
130
class DenseHealpix(HealpixWrapperBase):
131
    """
132
    A dense (fullsky) healpix map. In this case partial and complete are the same map.
133
134
    """
135
136
    def __init__(self, healpix_array):
137
138
        self._dense_map = healpix_array
139
140
        super(DenseHealpix, self).__init__(nside=hp.npix2nside(healpix_array.shape[0]), sparse=False)
141
142
    def as_dense(self):
143
        """
144
        Returns the complete (i.e., full sky) representation of the map. Since this is a dense map, this is identical
145
        to the input map
146
147
        :return: the complete map, suitable for use with healpy routine (among other uses)
148
        """
149
150
        return self._dense_map
151
152
    def as_partial(self):
153
154
        return self._dense_map
155
156
    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...
157
158
        assert new_values.shape == self._dense_map.shape
159
160
        self._dense_map[:] = new_values
161
0 ignored issues
show
coding-style introduced by
Trailing newlines
Loading history...
162