diff --git a/doc/_notebooks/mad_array.ipynb b/doc/_notebooks/mad_array.ipynb index 1dad5f1918cd33df6bfe1194ddf0032517fe80d2..0735c49489e44b8a1db836279b9872120fd77cc0 100644 --- a/doc/_notebooks/mad_array.ipynb +++ b/doc/_notebooks/mad_array.ipynb @@ -199,9 +199,9 @@ } }, "source": [ - "## Properties\n", + "## Methods and properties\n", "\n", - "A *MadArray* has attributes that give information about the masking." + "A *MadArray* has methods and properties that give information about the masking." ] }, { @@ -219,7 +219,7 @@ "outputs": [], "source": [ "# mask of non-missing elements\n", - "print(Am.known_mask)" + "print(Am.get_known_mask())" ] }, { @@ -237,7 +237,7 @@ "outputs": [], "source": [ "# mask of missing elements\n", - "print(Am.unknown_mask)" + "print(Am.get_unknown_mask())" ] }, { diff --git a/madarrays/__init__.py b/madarrays/__init__.py index 926da2907a4c98f62bd259d9604c6fd6336d01c6..1bde3ad90590cc59ff330ff2711ffea38dfb3625 100644 --- a/madarrays/__init__.py +++ b/madarrays/__init__.py @@ -40,8 +40,12 @@ # along with this program. If not, see <http://www.gnu.org/licenses/>. # # ######### COPYRIGHT ######### -"""This modules implements data structures adapted to handle missing entries in -audio data structures.""" +"""Data structures adapted to handle audio signals with missing data + +.. moduleauthor:: Ronan Hamon +.. moduleauthor:: Valentin Emiya +.. moduleauthor:: Florent Jaillet +""" from .mad_array import MadArray from .waveform import Waveform diff --git a/madarrays/mad_array.py b/madarrays/mad_array.py index 9fcfac603bf8c9b2031e843c345efa231f0f16cf..e9478f98e9bfeed5b4faab956f6ef149a0c054ee 100644 --- a/madarrays/mad_array.py +++ b/madarrays/mad_array.py @@ -69,25 +69,25 @@ def _merge_masks(ma1, ma2): if ma1._complex_masking or ma2._complex_masking: if ma1._complex_masking: - mm1 = ma1.unknown_magnitude_mask - mp1 = ma1.unknown_phase_mask + mm1 = ma1.get_unknown_mask('magnitude') + mp1 = ma1.get_unknown_mask('phase') else: - mm1 = ma1.unknown_mask - mp1 = ma1.unknown_mask + mm1 = ma1.get_unknown_mask('any') + mp1 = ma1.get_unknown_mask('any') if ma2._complex_masking: - mm2 = ma2.unknown_magnitude_mask - mp2 = ma2.unknown_phase_mask + mm2 = ma2.get_unknown_mask('magnitude') + mp2 = ma2.get_unknown_mask('phase') else: - mm2 = ma2.unknown_mask - mp2 = ma2.unknown_mask + mm2 = ma2.get_unknown_mask('any') + mp2 = ma2.get_unknown_mask('any') mask_magnitude = np.logical_or(mm1, mm2) mask_phase = np.logical_or(mp1, mp2) return {'mask_magnitude': mask_magnitude, 'mask_phase': mask_phase} else: - return {'mask': np.logical_or(ma1.unknown_mask, ma2.unknown_mask)} + return {'mask': (ma1.get_unknown_mask()) | (ma2.get_unknown_mask())} UFUNC_NOT_RETURNING_MADARRAYS = ['bitwise_and', 'bitwise_or', 'bitwise_xor', @@ -232,7 +232,7 @@ class MadArray(np.ndarray): if mask is None: if isinstance(data, MadArray): - mask = data.unknown_mask + mask = data.get_unknown_mask() else: mask = np.zeros(_data.shape, dtype=np.bool) else: @@ -249,8 +249,8 @@ class MadArray(np.ndarray): if mask_magnitude is None: if isinstance(data, MadArray) and mask_phase is None: - mask_magnitude = data.unknown_magnitude_mask - mask_phase = data.unknown_phase_mask + mask_magnitude = data.get_unknown_mask('magnitude') + mask_phase = data.get_unknown_mask('phase') else: mask_magnitude = np.zeros_like(data, dtype=np.bool) else: @@ -380,170 +380,6 @@ class MadArray(np.ndarray): self._masked_indexing = state[-1] super().__setstate__(state[0:-3]) - @property - def known_mask(self): - """Boolean mask for known coefficients (boolean nd-array). - - Boolean array with entries set to True if the corresponding value - in the object is known. If complex masking, entries set to True if - both magnitude and phase are known. - """ - if self._complex_masking: - return self._mask == 0 - else: - return ~self._mask - - @property - def unknown_mask(self): - """Boolean mask for unknown coefficients (boolean nd-array). - - Boolean array with values set to True if the corresponding value in - the object is unknown. If complex masking, entries set to True if - magnitude and/or phase are unknown. - """ - if self._complex_masking: - return self.any_unknown_mask - else: - return np.copy(self._mask) - - @property - def any_unknown_mask(self): - """Boolean mask for coefficients with unknown magnitude and/or phase - (boolean nd-array). - - Not defined if non complex masking. - - Boolean array with entries set to True if magnitude and/or phase - are unknown. - - Raises - ------ - ValueError - If masking is not complex. - """ - self._raise_if_not_complex_masking() - return ~(self._mask == 0) - - @property - def all_unknown_mask(self): - """Boolean mask for coefficients with both unknown magnitude and phase - (boolean nd-array). - - Not defined if non complex masking. - - Boolean array with entries set to True if both magnitude and phase - are unknown. - - Raises - ------ - ValueError - If masking is not complex. - """ - self._raise_if_not_complex_masking() - return self._mask == 3 - - @property - def known_phase_mask(self): - """Boolean mask for coefficients with known phase (boolean nd-array). - - Not defined for int or float entries. - - Boolean array with entries set to True if phase is known. - - Raises - ------ - ValueError - If masking is not complex. - """ - self._raise_if_not_complex_masking() - return ~np.logical_or(self._mask == 1, self._mask == 3) - - @property - def unknown_phase_mask(self): - """Boolean mask for coefficients with unknown phase (boolean nd-array). - - Not defined for int or float entries. - - Boolean array with entries set to True if phase is unknown. - - Raises - ------ - ValueError - If masking is not complex. - """ - self._raise_if_not_complex_masking() - return np.logical_or(self._mask == 1, self._mask == 3) - - @property - def known_magnitude_mask(self): - """Boolean mask for coefficients with known magnitude - (boolean nd-array). - - Not defined for int or float entries. - - Boolean array with entries set to True if magnitude is known. - - Raises - ------ - ValueError - If masking is not complex. - """ - self._raise_if_not_complex_masking() - return ~np.logical_or(self._mask == 2, self._mask == 3) - - @property - def unknown_magnitude_mask(self): - """Boolean mask for coefficients with unknown magnitude - (boolean nd-array). - - Not defined for int or float entries. - - Boolean array with entries set to True if magnitude is unknown. - - Raises - ------ - ValueError - If masking is not complex. - """ - self._raise_if_not_complex_masking() - return np.logical_or(self._mask == 2, self._mask == 3) - - @property - def unknown_phase_only_mask(self): - """Boolean mask for coefficients with unknown phase and known magnitude - (boolean nd-array). - - Not defined for int or float entries. - - Boolean array with entries set to True if phase is unknown and - magnitude is known. - - Raises - ------ - ValueError - If masking is not complex. - """ - self._raise_if_not_complex_masking() - return self._mask == 1 - - @property - def unknown_magnitude_only_mask(self): - """Boolean mask for coefficients with unknown magnitude and known phase - (boolean nd-array). - - Not defined for int or float entries. - - Boolean array with entries set to True if magnitude is unknown and - phase is known. - - Raises - ------ - ValueError - If masking is not complex. - """ - self._raise_if_not_complex_masking() - return self._mask == 2 - @property def n_missing_data(self): """Number of missing data (double or tuple). @@ -553,10 +389,10 @@ class MadArray(np.ndarray): complex. """ if self._complex_masking: - return (np.sum(self.unknown_magnitude_mask), - np.sum(self.unknown_phase_mask)) + return (np.sum(self.get_unknown_mask('magnitude')), + np.sum(self.get_unknown_mask('phase'))) else: - return np.sum(self.unknown_mask) + return np.sum(self.get_unknown_mask()) @property def ratio_missing_data(self): @@ -567,15 +403,10 @@ class MadArray(np.ndarray): complex. """ if self._complex_masking: - return (np.average(self.unknown_magnitude_mask), - np.average(self.unknown_phase_mask)) + return (np.average(self.get_unknown_mask('magnitude')), + np.average(self.get_unknown_mask('phase'))) else: - return np.average(self.unknown_mask) - - def _raise_if_not_complex_masking(self): - if not self._complex_masking: - errmsg = 'Method not defined if masking is not complex.' - raise ValueError(errmsg) + return np.average(self.get_unknown_mask()) def is_masked(self): """Indicate if one or several elements are masked.""" @@ -608,13 +439,13 @@ class MadArray(np.ndarray): data = np.array(self) if fill_value is not None: if self._complex_masking: - upom = self.unknown_phase_only_mask - umom = self.unknown_magnitude_only_mask + upom = self.get_unknown_mask('phase only') + umom = self.get_unknown_mask('magnitude only') data[upom] = np.abs(data[upom]) data[umom] = np.exp(1j * np.angle(data[umom])) - data[self.all_unknown_mask] = fill_value + data[self.get_unknown_mask('all')] = fill_value else: - data[self.unknown_mask] = fill_value + data[self.get_unknown_mask()] = fill_value return data def __eq__(self, other): @@ -659,7 +490,7 @@ class MadArray(np.ndarray): if np.issubdtype(self.dtype, np.integer): arr = arr.astype(np.float64) - arr[self.unknown_mask] = np.nan + arr[self.get_unknown_mask()] = np.nan arr_str = np.ndarray.__str__(arr) if np.isrealobj(arr): arr_str = arr_str.replace('nan', ' x') @@ -670,7 +501,7 @@ class MadArray(np.ndarray): arr_str = arr_str.replace('.', '') if self._complex_masking: - n_all_unknown = np.count_nonzero(self.all_unknown_mask) + n_all_unknown = np.count_nonzero(self.get_unknown_mask('all')) string = 'MadArray, dtype={0}, ' \ '{1[0]} missing magnitudes ({2[0]:.1%}) ' \ 'and {1[1]} missing phases ({2[1]:.1%}), ' \ @@ -692,3 +523,71 @@ class MadArray(np.ndarray): def __repr__(self): string = '<MadArray at {}>' return string.format(hex(id(self))) + + def get_known_mask(self, mask_type='any'): + """Boolean mask for known coefficients. + + TODO: finish this + + Parameters + ---------- + mask_type : {'any', 'all', 'magnitude', 'phase', 'magnitude only', \ + 'phase only'} + TODO + + Returns + ------- + mask : boolean nd-array + Boolean array with entries set to True if the corresponding value + in the object is known. + + Raises + ------ + ValueError + If ``mask_type`` has an invalid value. + """ + return ~self.get_unknown_mask(mask_type) + + def get_unknown_mask(self, mask_type='any'): + """Boolean mask for unknown coefficients. + + TODO: finish this + + Parameters + ---------- + mask_type : {'any', 'all', 'magnitude', 'phase', 'magnitude only', \ + 'phase only'} + TODO + + Returns + ------- + mask : boolean nd-array + Boolean array with values set to True if the corresponding value in + the object is unknown. + + Raises + ------ + ValueError + If ``mask_type`` has an invalid value. + """ + if self._complex_masking: + if mask_type == 'any': + return self._mask != 0 + elif mask_type == 'all': + return self._mask == 3 + elif mask_type == 'magnitude': + return (self._mask == 3) | (self._mask == 2) + elif mask_type == 'phase': + return (self._mask == 3) | (self._mask == 1) + elif mask_type == 'magnitude only': + return self._mask == 2 + elif mask_type == 'phase only': + return self._mask == 1 + else: + if mask_type in ('any', 'all', 'magnitude', 'phase'): + return np.copy(self._mask) + elif mask_type in ('magnitude only', 'phase only'): + return np.zeros_like(self._mask, dtype=np.bool) + + errmsg = 'Invalid value for mask_type: {}'.format(mask_type) + raise ValueError(errmsg) diff --git a/madarrays/tests/test_madarray.py b/madarrays/tests/test_madarray.py index fdf0fff117ee2781dbf0dca6a2fb1f88b5046a52..f700816c7a88e99158400f0a8e1703b25b35ba85 100644 --- a/madarrays/tests/test_madarray.py +++ b/madarrays/tests/test_madarray.py @@ -109,30 +109,12 @@ class TestMadArray: assert ma.dtype == x.dtype assert not ma._complex_masking assert not ma._masked_indexing - np.testing.assert_equal(ma.unknown_mask, self.empty_mask) - np.testing.assert_equal(ma.known_mask, self.full_mask) + np.testing.assert_equal(ma.get_unknown_mask(), self.empty_mask) + np.testing.assert_equal(ma.get_known_mask(), self.full_mask) assert ma.ratio_missing_data == 0.0 assert ma.n_missing_data == 0 assert not ma.is_masked() - match = 'Method not defined if masking is not complex' - with pytest.raises(ValueError, match=match): - ma.any_unknown_mask - with pytest.raises(ValueError, match=match): - ma.all_unknown_mask - with pytest.raises(ValueError, match=match): - ma.known_phase_mask - with pytest.raises(ValueError, match=match): - ma.unknown_phase_mask - with pytest.raises(ValueError, match=match): - ma.known_magnitude_mask - with pytest.raises(ValueError, match=match): - ma.unknown_magnitude_mask - with pytest.raises(ValueError, match=match): - ma.unknown_phase_only_mask - with pytest.raises(ValueError, match=match): - ma.unknown_magnitude_only_mask - def test_init_masked(self): for x in [self.x_float, self.x_int, self.x_complex]: @@ -143,31 +125,36 @@ class TestMadArray: assert not ma._masked_indexing assert ma.is_masked() np.testing.assert_equal(ma.to_np_array(), x) - np.testing.assert_equal(ma.unknown_mask, self.m) - np.testing.assert_equal(ma.known_mask, ~self.m) + np.testing.assert_equal(ma.get_unknown_mask(), self.m) + np.testing.assert_equal(ma.get_known_mask(), ~self.m) + np.testing.assert_equal(ma.get_unknown_mask('any'), self.m) + np.testing.assert_equal(ma.get_known_mask('any'), ~self.m) + np.testing.assert_equal(ma.get_unknown_mask('all'), self.m) + np.testing.assert_equal(ma.get_known_mask('all'), ~self.m) + np.testing.assert_equal(ma.get_unknown_mask('phase'), self.m) + np.testing.assert_equal(ma.get_known_mask('phase'), ~self.m) + np.testing.assert_equal(ma.get_unknown_mask('magnitude'), self.m) + np.testing.assert_equal(ma.get_known_mask('magnitude'), ~self.m) + np.testing.assert_equal(ma.get_unknown_mask('phase only'), + self.empty_mask) + np.testing.assert_equal(ma.get_known_mask('phase only'), + self.full_mask) + np.testing.assert_equal(ma.get_unknown_mask('magnitude only'), + self.empty_mask) + np.testing.assert_equal(ma.get_known_mask('magnitude only'), + self.full_mask) + + match = 'Invalid value for mask_type: error' + with pytest.raises(ValueError, match=match): + ma.get_known_mask('error') + with pytest.raises(ValueError, match=match): + ma.get_unknown_mask('error') + assert ma.ratio_missing_data, 0.5 assert ma.n_missing_data, x.size//2 assert ma.is_masked() assert not id(ma._mask) == id(self.m) - match = 'Method not defined if masking is not complex' - with pytest.raises(ValueError, match=match): - ma.any_unknown_mask - with pytest.raises(ValueError, match=match): - ma.all_unknown_mask - with pytest.raises(ValueError, match=match): - ma.known_phase_mask - with pytest.raises(ValueError, match=match): - ma.unknown_phase_mask - with pytest.raises(ValueError, match=match): - ma.known_magnitude_mask - with pytest.raises(ValueError, match=match): - ma.unknown_magnitude_mask - with pytest.raises(ValueError, match=match): - ma.unknown_phase_only_mask - with pytest.raises(ValueError, match=match): - ma.unknown_magnitude_only_mask - bad_shape = [item + np.random.randint(1, 100) for item in self.shape] match = 'Mask shape \(\d+,( \d+)?\) and data shape \(\d+,( \d+)?\) '\ @@ -196,22 +183,30 @@ class TestMadArray: assert ma.is_masked() np.testing.assert_equal(ma.to_np_array(), x) - np.testing.assert_equal(ma.known_mask, + np.testing.assert_equal(ma.get_known_mask(), np.logical_and(~self.mm, ~self.mp)) - np.testing.assert_equal(ma.unknown_mask, + np.testing.assert_equal(ma.get_unknown_mask(), np.logical_or(self.mm, self.mp)) - np.testing.assert_equal(ma.any_unknown_mask, + np.testing.assert_equal(ma.get_unknown_mask('any'), np.logical_or(self.mm, self.mp)) - np.testing.assert_equal(ma.all_unknown_mask, + np.testing.assert_equal(ma.get_unknown_mask('all'), np.logical_and(self.mm, self.mp)) - np.testing.assert_equal(ma.known_phase_mask, ~self.mp) - np.testing.assert_equal(ma.unknown_phase_mask, self.mp) - np.testing.assert_equal(ma.known_magnitude_mask, ~self.mm) - np.testing.assert_equal(ma.unknown_magnitude_mask, self.mm) - np.testing.assert_equal(ma.unknown_phase_only_mask, + np.testing.assert_equal(ma.get_known_mask('phase'), ~self.mp) + np.testing.assert_equal(ma.get_unknown_mask('phase'), self.mp) + np.testing.assert_equal(ma.get_known_mask('magnitude'), ~self.mm) + np.testing.assert_equal(ma.get_unknown_mask('magnitude'), self.mm) + np.testing.assert_equal(ma.get_unknown_mask('phase only'), np.logical_and(self.mp, ~self.mm)) - np.testing.assert_equal(ma.unknown_magnitude_only_mask, + np.testing.assert_equal(ma.get_unknown_mask('magnitude only'), np.logical_and(~self.mp, self.mm)) + + match = 'Invalid value for mask_type: error' + with pytest.raises(ValueError, match=match): + ma.get_known_mask('error') + with pytest.raises(ValueError, match=match): + ma.get_unknown_mask('error') + + assert ma.ratio_missing_data == (0.5, 0.5) assert ma.n_missing_data == (x.size//2, x.size//2) @@ -224,20 +219,21 @@ class TestMadArray: assert ma.is_masked() np.testing.assert_equal(ma.to_np_array(), x) - np.testing.assert_equal(ma.known_mask, ~self.mm) - np.testing.assert_equal(ma.unknown_mask, self.mm) - np.testing.assert_equal(ma.any_unknown_mask, self.mm) - np.testing.assert_equal(ma.all_unknown_mask, + np.testing.assert_equal(ma.get_known_mask(), ~self.mm) + np.testing.assert_equal(ma.get_unknown_mask(), self.mm) + np.testing.assert_equal(ma.get_unknown_mask('any'), self.mm) + np.testing.assert_equal(ma.get_unknown_mask('all'), np.zeros(self.shape, dtype=np.bool)) - np.testing.assert_equal(ma.known_phase_mask, + np.testing.assert_equal(ma.get_known_mask('phase'), np.ones(self.shape, dtype=np.bool)) - np.testing.assert_equal(ma.unknown_phase_mask, + np.testing.assert_equal(ma.get_unknown_mask('phase'), np.zeros(self.shape, dtype=np.bool)) - np.testing.assert_equal(ma.known_magnitude_mask, ~self.mm) - np.testing.assert_equal(ma.unknown_magnitude_mask, self.mm) - np.testing.assert_equal(ma.unknown_phase_only_mask, + np.testing.assert_equal(ma.get_known_mask('magnitude'), ~self.mm) + np.testing.assert_equal(ma.get_unknown_mask('magnitude'), self.mm) + np.testing.assert_equal(ma.get_unknown_mask('phase only'), np.zeros(self.shape, dtype=np.bool)) - np.testing.assert_equal(ma.unknown_magnitude_only_mask, self.mm) + np.testing.assert_equal(ma.get_unknown_mask('magnitude only'), + self.mm) assert ma.ratio_missing_data == (0.5, 0) assert ma.n_missing_data == (x.size//2, 0) @@ -250,19 +246,19 @@ class TestMadArray: assert ma.is_masked() np.testing.assert_equal(ma.to_np_array(), x) - np.testing.assert_equal(ma.known_mask, ~self.mp) - np.testing.assert_equal(ma.unknown_mask, self.mp) - np.testing.assert_equal(ma.any_unknown_mask, self.mp) - np.testing.assert_equal(ma.all_unknown_mask, + np.testing.assert_equal(ma.get_known_mask(), ~self.mp) + np.testing.assert_equal(ma.get_unknown_mask(), self.mp) + np.testing.assert_equal(ma.get_unknown_mask('any'), self.mp) + np.testing.assert_equal(ma.get_unknown_mask('all'), np.zeros(self.shape, dtype=np.bool)) - np.testing.assert_equal(ma.known_phase_mask, ~self.mp) - np.testing.assert_equal(ma.unknown_phase_mask, self.mp) - np.testing.assert_equal(ma.known_magnitude_mask, + np.testing.assert_equal(ma.get_known_mask('phase'), ~self.mp) + np.testing.assert_equal(ma.get_unknown_mask('phase'), self.mp) + np.testing.assert_equal(ma.get_known_mask('magnitude'), np.ones(self.shape, dtype=np.bool)) - np.testing.assert_equal(ma.unknown_magnitude_mask, + np.testing.assert_equal(ma.get_unknown_mask('magnitude'), np.zeros(self.shape, dtype=np.bool)) - np.testing.assert_equal(ma.unknown_phase_only_mask, self.mp) - np.testing.assert_equal(ma.unknown_magnitude_only_mask, + np.testing.assert_equal(ma.get_unknown_mask('phase only'), self.mp) + np.testing.assert_equal(ma.get_unknown_mask('magnitude only'), np.zeros(self.shape, dtype=np.bool)) assert ma.ratio_missing_data == (0, 0.5) assert ma.n_missing_data == (0, x.size//2) @@ -288,14 +284,14 @@ class TestMadArray: old_ma = MadArray(x, self.m) ma = MadArray(old_ma) - np.testing.assert_equal(ma.unknown_mask, self.m) + np.testing.assert_equal(ma.get_unknown_mask(), self.m) assert id(old_ma) != id(ma) assert id(old_ma._mask) != id(ma._mask) old_ma = MadArray(x, self.m) ma = MadArray(old_ma, mask=self.mp) - np.testing.assert_equal(ma.unknown_mask, self.mp) + np.testing.assert_equal(ma.get_unknown_mask(), self.mp) assert id(old_ma) != id(ma) assert id(old_ma._mask) != id(ma._mask) @@ -306,8 +302,8 @@ class TestMadArray: old_ma = MadArray(x, mask_phase=self.mp, mask_magnitude=self.mm) ma = MadArray(old_ma) - np.testing.assert_equal(ma.unknown_phase_mask, self.mp) - np.testing.assert_equal(ma.unknown_magnitude_mask, self.mm) + np.testing.assert_equal(ma.get_unknown_mask('phase'), self.mp) + np.testing.assert_equal(ma.get_unknown_mask('magnitude'), self.mm) assert id(old_ma) != id(ma) assert id(old_ma._mask) != id(ma._mask) assert ma._complex_masking @@ -315,34 +311,34 @@ class TestMadArray: old_ma = MadArray(x, mask_phase=self.mp, mask_magnitude=self.mm) ma = MadArray(old_ma, mask_phase=self.mm, mask_magnitude=self.mp) - np.testing.assert_equal(ma.unknown_phase_mask, self.mm) - np.testing.assert_equal(ma.unknown_magnitude_mask, self.mp) + np.testing.assert_equal(ma.get_unknown_mask('phase'), self.mm) + np.testing.assert_equal(ma.get_unknown_mask('magnitude'), self.mp) assert id(old_ma) != id(ma) assert id(old_ma._mask) != id(ma._mask) old_ma = MadArray(x, mask_phase=self.mp, mask_magnitude=self.mm) ma = MadArray(old_ma, mask_phase=self.mm) - np.testing.assert_equal(ma.unknown_phase_mask, self.mm) - np.testing.assert_equal(ma.unknown_magnitude_mask, - np.zeros_like(x, dtype=np.bool)) + np.testing.assert_equal(ma.get_unknown_mask('phase'), self.mm) + np.testing.assert_equal(ma.get_unknown_mask('magnitude'), + self.empty_mask) assert id(old_ma) != id(ma) assert id(old_ma._mask) != id(ma._mask) old_ma = MadArray(x, mask_phase=self.mp, mask_magnitude=self.mm) ma = MadArray(old_ma, mask_magnitude=self.mp) - np.testing.assert_equal(ma.unknown_phase_mask, - np.zeros_like(x, dtype=np.bool)) - np.testing.assert_equal(ma.unknown_magnitude_mask, self.mp) + np.testing.assert_equal(ma.get_unknown_mask('phase'), + self.empty_mask) + np.testing.assert_equal(ma.get_unknown_mask('magnitude'), self.mp) assert id(old_ma) != id(ma) assert id(old_ma._mask) != id(ma._mask) old_ma = MadArray(x, self.m) ma = MadArray(old_ma, mask_phase=self.mp, mask_magnitude=self.mm) - np.testing.assert_equal(ma.unknown_phase_mask, self.mp) - np.testing.assert_equal(ma.unknown_magnitude_mask, self.mm) + np.testing.assert_equal(ma.get_unknown_mask('phase'), self.mp) + np.testing.assert_equal(ma.get_unknown_mask('magnitude'), self.mm) assert id(old_ma) != id(ma) assert id(old_ma._mask) != id(ma._mask) assert ma._complex_masking @@ -350,9 +346,9 @@ class TestMadArray: old_ma = MadArray(x, self.m) ma = MadArray(old_ma, mask_phase=self.mp) - np.testing.assert_equal(ma.unknown_phase_mask, self.mp) - np.testing.assert_equal(ma.unknown_magnitude_mask, - np.zeros_like(x, dtype=np.bool)) + np.testing.assert_equal(ma.get_unknown_mask('phase'), self.mp) + np.testing.assert_equal(ma.get_unknown_mask('magnitude'), + self.empty_mask) assert id(old_ma) != id(ma) assert id(old_ma._mask) != id(ma._mask) assert ma._complex_masking @@ -360,9 +356,9 @@ class TestMadArray: old_ma = MadArray(x, self.m) ma = MadArray(old_ma, mask_magnitude=self.mm) - np.testing.assert_equal(ma.unknown_phase_mask, - np.zeros_like(x, dtype=np.bool)) - np.testing.assert_equal(ma.unknown_magnitude_mask, self.mm) + np.testing.assert_equal(ma.get_unknown_mask('phase'), + self.empty_mask) + np.testing.assert_equal(ma.get_unknown_mask('magnitude'), self.mm) assert id(old_ma) != id(ma) assert id(old_ma._mask) != id(ma._mask) assert ma._complex_masking @@ -388,8 +384,8 @@ class TestMadArray: assert isinstance(ma_slice, MadArray) np.testing.assert_equal(ma_slice, self.x_float[self.indexes]) - np.testing.assert_equal(ma_slice.unknown_mask, self.m[self.indexes]) - np.testing.assert_equal(ma_slice.known_mask, ~self.m[self.indexes]) + np.testing.assert_equal(ma_slice.get_unknown_mask(), self.m[self.indexes]) + np.testing.assert_equal(ma_slice.get_known_mask(), ~self.m[self.indexes]) assert ma_slice.ratio_missing_data == np.mean(self.m[self.indexes]) assert ma_slice.n_missing_data == np.sum(self.m[self.indexes]) assert ma.is_masked() @@ -405,9 +401,9 @@ class TestMadArray: assert isinstance(ma_slice, MadArray) np.testing.assert_equal(ma_slice.shape, self.shape) np.testing.assert_equal(ma_slice, self.x_float) - np.testing.assert_equal(ma_slice.unknown_mask, + np.testing.assert_equal(ma_slice.get_unknown_mask(), np.logical_or(~m_index, self.m)) - np.testing.assert_equal(ma_slice.known_mask, + np.testing.assert_equal(ma_slice.get_known_mask(), np.logical_and(m_index, ~self.m)) assert (ma_slice.ratio_missing_data == np.mean( np.logical_or(~m_index, self.m))) @@ -429,13 +425,13 @@ class TestMadArray: xma = ma.to_np_array(0) np.testing.assert_almost_equal( - xma[ma.unknown_phase_only_mask], - np.abs(self.x_float[ma.unknown_phase_only_mask]) + 0 * 1j) + xma[ma.get_unknown_mask('phase only')], + np.abs(self.x_float[ma.get_unknown_mask('phase only')]) + 0 * 1j) np.testing.assert_almost_equal( - xma[ma.unknown_magnitude_only_mask], - np.exp( - 1j * np.angle(self.x_float[ma.unknown_magnitude_only_mask]))) - np.testing.assert_equal(xma[ma.all_unknown_mask], 0 + 0j) + xma[ma.get_unknown_mask('magnitude only')], + np.exp(1j * np.angle( + self.x_float[ma.get_unknown_mask('magnitude only')]))) + np.testing.assert_equal(xma[ma.get_unknown_mask('all')], 0 + 0j) ma = MadArray(self.x_float, self.m) xma = ma.to_np_array() @@ -571,7 +567,7 @@ class TestMadArray: ma_eq = ma == self.x_float assert type(ma_eq) == np.ndarray assert ma_eq.dtype == np.bool - assert np.all(ma_eq[ma.known_mask]) + assert np.all(ma_eq[ma.get_known_mask()]) neq = ma != self.x2_float assert type(neq) == np.ndarray @@ -616,9 +612,9 @@ class TestMadArray: tma = ma_float.T - np.testing.assert_equal(tma.unknown_mask, self.m.T) - np.testing.assert_equal(np.array(tma)[tma.known_mask], - self.x_float.T[tma.known_mask]) + np.testing.assert_equal(tma.get_unknown_mask(), self.m.T) + np.testing.assert_equal(np.array(tma)[tma.get_known_mask()], + self.x_float.T[tma.get_known_mask()]) def test_pickle(self): diff --git a/madarrays/tests/test_waveform.py b/madarrays/tests/test_waveform.py index 4c24ab6c847ec10dbb439ec0ae16d916dc617211..5ca6de8fa5fa1d349f2cd6fe2b1b849757c8b295 100644 --- a/madarrays/tests/test_waveform.py +++ b/madarrays/tests/test_waveform.py @@ -49,6 +49,7 @@ using 'warnings.catch_warnings(record=True)'). .. moduleauthor:: Ronan Hamon .. moduleauthor:: Valentin Emiya +.. moduleauthor:: Florent Jaillet """ import pickle import pytest diff --git a/madarrays/tests/utils.py b/madarrays/tests/utils.py index 57a8f32739e6e3e3818cb30058e7fc0f7c5ff50c..ca8bb145cc10f71ba42ba49d7ecf61b2c60c48ef 100644 --- a/madarrays/tests/utils.py +++ b/madarrays/tests/utils.py @@ -39,7 +39,12 @@ # along with this program. If not, see <http://www.gnu.org/licenses/>. # # ######### COPYRIGHT ######### -"""Utils functions for testing.""" +"""Utils functions for testing. + +.. moduleauthor:: Ronan Hamon +.. moduleauthor:: Valentin Emiya +.. moduleauthor:: Florent Jaillet +""" import numpy as np diff --git a/madarrays/waveform.py b/madarrays/waveform.py index 6f839b59a3b6a43950b465c4fac808415579dc19..3bed1d6f3ec9f73682410998c80017ec0fc8dfd5 100644 --- a/madarrays/waveform.py +++ b/madarrays/waveform.py @@ -691,7 +691,7 @@ class Waveform(MadArray): -------- scipy.signal.hilbert """ - if np.any(self.unknown_mask): + if np.any(self.get_unknown_mask()): errmsg = 'Waveform has missing samples.' raise TypeError(errmsg) @@ -827,7 +827,7 @@ class Waveform(MadArray): errmsg = 'Unsupported target type {}.' raise TypeError(errmsg.format(dtype)) - return Waveform(y, mask=self.unknown_mask, fs=self.fs) + return Waveform(y, mask=self.get_unknown_mask(), fs=self.fs) def copy(self): return Waveform(self)