Add files using upload-large-folder tool
Browse filesThis view is limited to 50 files because it contains too many changes.
See raw diff
- .github/workflows/main.yml +31 -0
- venv/lib/python3.13/site-packages/fsspec-2025.10.0.dist-info/licenses/LICENSE +29 -0
- venv/lib/python3.13/site-packages/idna/__pycache__/__init__.cpython-313.pyc +0 -0
- venv/lib/python3.13/site-packages/idna/__pycache__/codec.cpython-313.pyc +0 -0
- venv/lib/python3.13/site-packages/idna/__pycache__/compat.cpython-313.pyc +0 -0
- venv/lib/python3.13/site-packages/idna/__pycache__/core.cpython-313.pyc +0 -0
- venv/lib/python3.13/site-packages/idna/__pycache__/intranges.cpython-313.pyc +0 -0
- venv/lib/python3.13/site-packages/idna/__pycache__/package_data.cpython-313.pyc +0 -0
- venv/lib/python3.13/site-packages/numpy/_utils/__pycache__/__init__.cpython-313.pyc +0 -0
- venv/lib/python3.13/site-packages/numpy/_utils/__pycache__/_convertions.cpython-313.pyc +0 -0
- venv/lib/python3.13/site-packages/numpy/_utils/__pycache__/_inspect.cpython-313.pyc +0 -0
- venv/lib/python3.13/site-packages/numpy/_utils/__pycache__/_pep440.cpython-313.pyc +0 -0
- venv/lib/python3.13/site-packages/numpy/ma/__pycache__/__init__.cpython-313.pyc +0 -0
- venv/lib/python3.13/site-packages/numpy/ma/__pycache__/extras.cpython-313.pyc +0 -0
- venv/lib/python3.13/site-packages/numpy/ma/__pycache__/mrecords.cpython-313.pyc +0 -0
- venv/lib/python3.13/site-packages/numpy/ma/__pycache__/testutils.cpython-313.pyc +0 -0
- venv/lib/python3.13/site-packages/numpy/ma/tests/__init__.py +0 -0
- venv/lib/python3.13/site-packages/numpy/ma/tests/__pycache__/__init__.cpython-313.pyc +0 -0
- venv/lib/python3.13/site-packages/numpy/ma/tests/__pycache__/test_arrayobject.cpython-313.pyc +0 -0
- venv/lib/python3.13/site-packages/numpy/ma/tests/__pycache__/test_deprecations.cpython-313.pyc +0 -0
- venv/lib/python3.13/site-packages/numpy/ma/tests/__pycache__/test_mrecords.cpython-313.pyc +0 -0
- venv/lib/python3.13/site-packages/numpy/ma/tests/__pycache__/test_old_ma.cpython-313.pyc +0 -0
- venv/lib/python3.13/site-packages/numpy/ma/tests/__pycache__/test_regression.cpython-313.pyc +0 -0
- venv/lib/python3.13/site-packages/numpy/ma/tests/__pycache__/test_subclassing.cpython-313.pyc +0 -0
- venv/lib/python3.13/site-packages/numpy/ma/tests/test_arrayobject.py +40 -0
- venv/lib/python3.13/site-packages/numpy/ma/tests/test_core.py +0 -0
- venv/lib/python3.13/site-packages/numpy/ma/tests/test_deprecations.py +87 -0
- venv/lib/python3.13/site-packages/numpy/ma/tests/test_extras.py +1998 -0
- venv/lib/python3.13/site-packages/numpy/ma/tests/test_mrecords.py +497 -0
- venv/lib/python3.13/site-packages/numpy/ma/tests/test_old_ma.py +942 -0
- venv/lib/python3.13/site-packages/numpy/ma/tests/test_regression.py +100 -0
- venv/lib/python3.13/site-packages/numpy/ma/tests/test_subclassing.py +469 -0
- venv/lib/python3.13/site-packages/numpy/matrixlib/__pycache__/__init__.cpython-313.pyc +0 -0
- venv/lib/python3.13/site-packages/numpy/matrixlib/__pycache__/defmatrix.cpython-313.pyc +0 -0
- venv/lib/python3.13/site-packages/numpy/matrixlib/tests/__init__.py +0 -0
- venv/lib/python3.13/site-packages/numpy/matrixlib/tests/__pycache__/__init__.cpython-313.pyc +0 -0
- venv/lib/python3.13/site-packages/numpy/matrixlib/tests/__pycache__/test_defmatrix.cpython-313.pyc +0 -0
- venv/lib/python3.13/site-packages/numpy/matrixlib/tests/__pycache__/test_interaction.cpython-313.pyc +0 -0
- venv/lib/python3.13/site-packages/numpy/matrixlib/tests/__pycache__/test_masked_matrix.cpython-313.pyc +0 -0
- venv/lib/python3.13/site-packages/numpy/matrixlib/tests/__pycache__/test_matrix_linalg.cpython-313.pyc +0 -0
- venv/lib/python3.13/site-packages/numpy/matrixlib/tests/__pycache__/test_multiarray.cpython-313.pyc +0 -0
- venv/lib/python3.13/site-packages/numpy/matrixlib/tests/__pycache__/test_numeric.cpython-313.pyc +0 -0
- venv/lib/python3.13/site-packages/numpy/matrixlib/tests/__pycache__/test_regression.cpython-313.pyc +0 -0
- venv/lib/python3.13/site-packages/numpy/matrixlib/tests/test_defmatrix.py +455 -0
- venv/lib/python3.13/site-packages/numpy/matrixlib/tests/test_interaction.py +360 -0
- venv/lib/python3.13/site-packages/numpy/matrixlib/tests/test_masked_matrix.py +240 -0
- venv/lib/python3.13/site-packages/numpy/matrixlib/tests/test_matrix_linalg.py +105 -0
- venv/lib/python3.13/site-packages/numpy/matrixlib/tests/test_multiarray.py +17 -0
- venv/lib/python3.13/site-packages/numpy/matrixlib/tests/test_numeric.py +18 -0
- venv/lib/python3.13/site-packages/numpy/matrixlib/tests/test_regression.py +31 -0
.github/workflows/main.yml
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# This is a basic workflow to help you get started with Actions
|
| 2 |
+
|
| 3 |
+
name: CI
|
| 4 |
+
|
| 5 |
+
# Controls when the workflow will run
|
| 6 |
+
on:
|
| 7 |
+
# Triggers the workflow on push or pull request events but only for the main branch
|
| 8 |
+
push:
|
| 9 |
+
branches: [ main ]
|
| 10 |
+
pull_request:
|
| 11 |
+
branches: [ main ]
|
| 12 |
+
|
| 13 |
+
# Allows you to run this workflow manually from the Actions tab
|
| 14 |
+
workflow_dispatch:
|
| 15 |
+
|
| 16 |
+
# A workflow run is made up of one or more jobs that can run sequentially or in parallel
|
| 17 |
+
jobs:
|
| 18 |
+
# This workflow contains a single job called "build"
|
| 19 |
+
build:
|
| 20 |
+
# The type of runner that the job will run on
|
| 21 |
+
runs-on: ubuntu-latest
|
| 22 |
+
|
| 23 |
+
# Steps represent a sequence of tasks that will be executed as part of the job
|
| 24 |
+
steps:
|
| 25 |
+
|
| 26 |
+
- name: Checkout code
|
| 27 |
+
uses: actions/checkout@v2
|
| 28 |
+
|
| 29 |
+
# Runs a single command using the runners shell
|
| 30 |
+
- name: docker compose
|
| 31 |
+
run: docker-compose up -d
|
venv/lib/python3.13/site-packages/fsspec-2025.10.0.dist-info/licenses/LICENSE
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
BSD 3-Clause License
|
| 2 |
+
|
| 3 |
+
Copyright (c) 2018, Martin Durant
|
| 4 |
+
All rights reserved.
|
| 5 |
+
|
| 6 |
+
Redistribution and use in source and binary forms, with or without
|
| 7 |
+
modification, are permitted provided that the following conditions are met:
|
| 8 |
+
|
| 9 |
+
* Redistributions of source code must retain the above copyright notice, this
|
| 10 |
+
list of conditions and the following disclaimer.
|
| 11 |
+
|
| 12 |
+
* Redistributions in binary form must reproduce the above copyright notice,
|
| 13 |
+
this list of conditions and the following disclaimer in the documentation
|
| 14 |
+
and/or other materials provided with the distribution.
|
| 15 |
+
|
| 16 |
+
* Neither the name of the copyright holder nor the names of its
|
| 17 |
+
contributors may be used to endorse or promote products derived from
|
| 18 |
+
this software without specific prior written permission.
|
| 19 |
+
|
| 20 |
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
| 21 |
+
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
| 22 |
+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
| 23 |
+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
| 24 |
+
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
| 25 |
+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
| 26 |
+
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
| 27 |
+
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
| 28 |
+
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
| 29 |
+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
venv/lib/python3.13/site-packages/idna/__pycache__/__init__.cpython-313.pyc
ADDED
|
Binary file (865 Bytes). View file
|
|
|
venv/lib/python3.13/site-packages/idna/__pycache__/codec.cpython-313.pyc
ADDED
|
Binary file (5.29 kB). View file
|
|
|
venv/lib/python3.13/site-packages/idna/__pycache__/compat.cpython-313.pyc
ADDED
|
Binary file (875 Bytes). View file
|
|
|
venv/lib/python3.13/site-packages/idna/__pycache__/core.cpython-313.pyc
ADDED
|
Binary file (16.9 kB). View file
|
|
|
venv/lib/python3.13/site-packages/idna/__pycache__/intranges.cpython-313.pyc
ADDED
|
Binary file (2.59 kB). View file
|
|
|
venv/lib/python3.13/site-packages/idna/__pycache__/package_data.cpython-313.pyc
ADDED
|
Binary file (196 Bytes). View file
|
|
|
venv/lib/python3.13/site-packages/numpy/_utils/__pycache__/__init__.cpython-313.pyc
ADDED
|
Binary file (4.07 kB). View file
|
|
|
venv/lib/python3.13/site-packages/numpy/_utils/__pycache__/_convertions.cpython-313.pyc
ADDED
|
Binary file (848 Bytes). View file
|
|
|
venv/lib/python3.13/site-packages/numpy/_utils/__pycache__/_inspect.cpython-313.pyc
ADDED
|
Binary file (9.26 kB). View file
|
|
|
venv/lib/python3.13/site-packages/numpy/_utils/__pycache__/_pep440.cpython-313.pyc
ADDED
|
Binary file (19.1 kB). View file
|
|
|
venv/lib/python3.13/site-packages/numpy/ma/__pycache__/__init__.cpython-313.pyc
ADDED
|
Binary file (1.67 kB). View file
|
|
|
venv/lib/python3.13/site-packages/numpy/ma/__pycache__/extras.cpython-313.pyc
ADDED
|
Binary file (79.1 kB). View file
|
|
|
venv/lib/python3.13/site-packages/numpy/ma/__pycache__/mrecords.cpython-313.pyc
ADDED
|
Binary file (33.9 kB). View file
|
|
|
venv/lib/python3.13/site-packages/numpy/ma/__pycache__/testutils.cpython-313.pyc
ADDED
|
Binary file (12.5 kB). View file
|
|
|
venv/lib/python3.13/site-packages/numpy/ma/tests/__init__.py
ADDED
|
File without changes
|
venv/lib/python3.13/site-packages/numpy/ma/tests/__pycache__/__init__.cpython-313.pyc
ADDED
|
Binary file (177 Bytes). View file
|
|
|
venv/lib/python3.13/site-packages/numpy/ma/tests/__pycache__/test_arrayobject.cpython-313.pyc
ADDED
|
Binary file (2.21 kB). View file
|
|
|
venv/lib/python3.13/site-packages/numpy/ma/tests/__pycache__/test_deprecations.cpython-313.pyc
ADDED
|
Binary file (4.85 kB). View file
|
|
|
venv/lib/python3.13/site-packages/numpy/ma/tests/__pycache__/test_mrecords.cpython-313.pyc
ADDED
|
Binary file (30.4 kB). View file
|
|
|
venv/lib/python3.13/site-packages/numpy/ma/tests/__pycache__/test_old_ma.cpython-313.pyc
ADDED
|
Binary file (63.1 kB). View file
|
|
|
venv/lib/python3.13/site-packages/numpy/ma/tests/__pycache__/test_regression.cpython-313.pyc
ADDED
|
Binary file (7.45 kB). View file
|
|
|
venv/lib/python3.13/site-packages/numpy/ma/tests/__pycache__/test_subclassing.cpython-313.pyc
ADDED
|
Binary file (27.8 kB). View file
|
|
|
venv/lib/python3.13/site-packages/numpy/ma/tests/test_arrayobject.py
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import pytest
|
| 2 |
+
|
| 3 |
+
import numpy as np
|
| 4 |
+
from numpy.ma import masked_array
|
| 5 |
+
from numpy.testing import assert_array_equal
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
def test_matrix_transpose_raises_error_for_1d():
|
| 9 |
+
msg = "matrix transpose with ndim < 2 is undefined"
|
| 10 |
+
ma_arr = masked_array(data=[1, 2, 3, 4, 5, 6],
|
| 11 |
+
mask=[1, 0, 1, 1, 1, 0])
|
| 12 |
+
with pytest.raises(ValueError, match=msg):
|
| 13 |
+
ma_arr.mT
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
def test_matrix_transpose_equals_transpose_2d():
|
| 17 |
+
ma_arr = masked_array(data=[[1, 2, 3], [4, 5, 6]],
|
| 18 |
+
mask=[[1, 0, 1], [1, 1, 0]])
|
| 19 |
+
assert_array_equal(ma_arr.T, ma_arr.mT)
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
ARRAY_SHAPES_TO_TEST = (
|
| 23 |
+
(5, 2),
|
| 24 |
+
(5, 2, 3),
|
| 25 |
+
(5, 2, 3, 4),
|
| 26 |
+
)
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
@pytest.mark.parametrize("shape", ARRAY_SHAPES_TO_TEST)
|
| 30 |
+
def test_matrix_transpose_equals_swapaxes(shape):
|
| 31 |
+
num_of_axes = len(shape)
|
| 32 |
+
vec = np.arange(shape[-1])
|
| 33 |
+
arr = np.broadcast_to(vec, shape)
|
| 34 |
+
|
| 35 |
+
rng = np.random.default_rng(42)
|
| 36 |
+
mask = rng.choice([0, 1], size=shape)
|
| 37 |
+
ma_arr = masked_array(data=arr, mask=mask)
|
| 38 |
+
|
| 39 |
+
tgt = np.swapaxes(arr, num_of_axes - 2, num_of_axes - 1)
|
| 40 |
+
assert_array_equal(tgt, ma_arr.mT)
|
venv/lib/python3.13/site-packages/numpy/ma/tests/test_core.py
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
venv/lib/python3.13/site-packages/numpy/ma/tests/test_deprecations.py
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Test deprecation and future warnings.
|
| 2 |
+
|
| 3 |
+
"""
|
| 4 |
+
import io
|
| 5 |
+
import textwrap
|
| 6 |
+
|
| 7 |
+
import pytest
|
| 8 |
+
|
| 9 |
+
import numpy as np
|
| 10 |
+
from numpy.ma.core import MaskedArrayFutureWarning
|
| 11 |
+
from numpy.ma.testutils import assert_equal
|
| 12 |
+
from numpy.testing import assert_warns
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
class TestArgsort:
|
| 16 |
+
""" gh-8701 """
|
| 17 |
+
def _test_base(self, argsort, cls):
|
| 18 |
+
arr_0d = np.array(1).view(cls)
|
| 19 |
+
argsort(arr_0d)
|
| 20 |
+
|
| 21 |
+
arr_1d = np.array([1, 2, 3]).view(cls)
|
| 22 |
+
argsort(arr_1d)
|
| 23 |
+
|
| 24 |
+
# argsort has a bad default for >1d arrays
|
| 25 |
+
arr_2d = np.array([[1, 2], [3, 4]]).view(cls)
|
| 26 |
+
result = assert_warns(
|
| 27 |
+
np.ma.core.MaskedArrayFutureWarning, argsort, arr_2d)
|
| 28 |
+
assert_equal(result, argsort(arr_2d, axis=None))
|
| 29 |
+
|
| 30 |
+
# should be no warnings for explicitly specifying it
|
| 31 |
+
argsort(arr_2d, axis=None)
|
| 32 |
+
argsort(arr_2d, axis=-1)
|
| 33 |
+
|
| 34 |
+
def test_function_ndarray(self):
|
| 35 |
+
return self._test_base(np.ma.argsort, np.ndarray)
|
| 36 |
+
|
| 37 |
+
def test_function_maskedarray(self):
|
| 38 |
+
return self._test_base(np.ma.argsort, np.ma.MaskedArray)
|
| 39 |
+
|
| 40 |
+
def test_method(self):
|
| 41 |
+
return self._test_base(np.ma.MaskedArray.argsort, np.ma.MaskedArray)
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
class TestMinimumMaximum:
|
| 45 |
+
|
| 46 |
+
def test_axis_default(self):
|
| 47 |
+
# NumPy 1.13, 2017-05-06
|
| 48 |
+
|
| 49 |
+
data1d = np.ma.arange(6)
|
| 50 |
+
data2d = data1d.reshape(2, 3)
|
| 51 |
+
|
| 52 |
+
ma_min = np.ma.minimum.reduce
|
| 53 |
+
ma_max = np.ma.maximum.reduce
|
| 54 |
+
|
| 55 |
+
# check that the default axis is still None, but warns on 2d arrays
|
| 56 |
+
result = assert_warns(MaskedArrayFutureWarning, ma_max, data2d)
|
| 57 |
+
assert_equal(result, ma_max(data2d, axis=None))
|
| 58 |
+
|
| 59 |
+
result = assert_warns(MaskedArrayFutureWarning, ma_min, data2d)
|
| 60 |
+
assert_equal(result, ma_min(data2d, axis=None))
|
| 61 |
+
|
| 62 |
+
# no warnings on 1d, as both new and old defaults are equivalent
|
| 63 |
+
result = ma_min(data1d)
|
| 64 |
+
assert_equal(result, ma_min(data1d, axis=None))
|
| 65 |
+
assert_equal(result, ma_min(data1d, axis=0))
|
| 66 |
+
|
| 67 |
+
result = ma_max(data1d)
|
| 68 |
+
assert_equal(result, ma_max(data1d, axis=None))
|
| 69 |
+
assert_equal(result, ma_max(data1d, axis=0))
|
| 70 |
+
|
| 71 |
+
|
| 72 |
+
class TestFromtextfile:
|
| 73 |
+
def test_fromtextfile_delimitor(self):
|
| 74 |
+
# NumPy 1.22.0, 2021-09-23
|
| 75 |
+
|
| 76 |
+
textfile = io.StringIO(textwrap.dedent(
|
| 77 |
+
"""
|
| 78 |
+
A,B,C,D
|
| 79 |
+
'string 1';1;1.0;'mixed column'
|
| 80 |
+
'string 2';2;2.0;
|
| 81 |
+
'string 3';3;3.0;123
|
| 82 |
+
'string 4';4;4.0;3.14
|
| 83 |
+
"""
|
| 84 |
+
))
|
| 85 |
+
|
| 86 |
+
with pytest.warns(DeprecationWarning):
|
| 87 |
+
result = np.ma.mrecords.fromtextfile(textfile, delimitor=';')
|
venv/lib/python3.13/site-packages/numpy/ma/tests/test_extras.py
ADDED
|
@@ -0,0 +1,1998 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Tests suite for MaskedArray.
|
| 2 |
+
Adapted from the original test_ma by Pierre Gerard-Marchant
|
| 3 |
+
|
| 4 |
+
:author: Pierre Gerard-Marchant
|
| 5 |
+
:contact: pierregm_at_uga_dot_edu
|
| 6 |
+
|
| 7 |
+
"""
|
| 8 |
+
import itertools
|
| 9 |
+
import warnings
|
| 10 |
+
|
| 11 |
+
import pytest
|
| 12 |
+
|
| 13 |
+
import numpy as np
|
| 14 |
+
from numpy._core.numeric import normalize_axis_tuple
|
| 15 |
+
from numpy.ma.core import (
|
| 16 |
+
MaskedArray,
|
| 17 |
+
arange,
|
| 18 |
+
array,
|
| 19 |
+
count,
|
| 20 |
+
getmaskarray,
|
| 21 |
+
masked,
|
| 22 |
+
masked_array,
|
| 23 |
+
nomask,
|
| 24 |
+
ones,
|
| 25 |
+
shape,
|
| 26 |
+
zeros,
|
| 27 |
+
)
|
| 28 |
+
from numpy.ma.extras import (
|
| 29 |
+
_covhelper,
|
| 30 |
+
apply_along_axis,
|
| 31 |
+
apply_over_axes,
|
| 32 |
+
atleast_1d,
|
| 33 |
+
atleast_2d,
|
| 34 |
+
atleast_3d,
|
| 35 |
+
average,
|
| 36 |
+
clump_masked,
|
| 37 |
+
clump_unmasked,
|
| 38 |
+
compress_nd,
|
| 39 |
+
compress_rowcols,
|
| 40 |
+
corrcoef,
|
| 41 |
+
cov,
|
| 42 |
+
diagflat,
|
| 43 |
+
dot,
|
| 44 |
+
ediff1d,
|
| 45 |
+
flatnotmasked_contiguous,
|
| 46 |
+
in1d,
|
| 47 |
+
intersect1d,
|
| 48 |
+
isin,
|
| 49 |
+
mask_rowcols,
|
| 50 |
+
masked_all,
|
| 51 |
+
masked_all_like,
|
| 52 |
+
median,
|
| 53 |
+
mr_,
|
| 54 |
+
ndenumerate,
|
| 55 |
+
notmasked_contiguous,
|
| 56 |
+
notmasked_edges,
|
| 57 |
+
polyfit,
|
| 58 |
+
setdiff1d,
|
| 59 |
+
setxor1d,
|
| 60 |
+
stack,
|
| 61 |
+
union1d,
|
| 62 |
+
unique,
|
| 63 |
+
vstack,
|
| 64 |
+
)
|
| 65 |
+
from numpy.ma.testutils import (
|
| 66 |
+
assert_,
|
| 67 |
+
assert_almost_equal,
|
| 68 |
+
assert_array_equal,
|
| 69 |
+
assert_equal,
|
| 70 |
+
)
|
| 71 |
+
from numpy.testing import assert_warns, suppress_warnings
|
| 72 |
+
|
| 73 |
+
|
| 74 |
+
class TestGeneric:
|
| 75 |
+
#
|
| 76 |
+
def test_masked_all(self):
|
| 77 |
+
# Tests masked_all
|
| 78 |
+
# Standard dtype
|
| 79 |
+
test = masked_all((2,), dtype=float)
|
| 80 |
+
control = array([1, 1], mask=[1, 1], dtype=float)
|
| 81 |
+
assert_equal(test, control)
|
| 82 |
+
# Flexible dtype
|
| 83 |
+
dt = np.dtype({'names': ['a', 'b'], 'formats': ['f', 'f']})
|
| 84 |
+
test = masked_all((2,), dtype=dt)
|
| 85 |
+
control = array([(0, 0), (0, 0)], mask=[(1, 1), (1, 1)], dtype=dt)
|
| 86 |
+
assert_equal(test, control)
|
| 87 |
+
test = masked_all((2, 2), dtype=dt)
|
| 88 |
+
control = array([[(0, 0), (0, 0)], [(0, 0), (0, 0)]],
|
| 89 |
+
mask=[[(1, 1), (1, 1)], [(1, 1), (1, 1)]],
|
| 90 |
+
dtype=dt)
|
| 91 |
+
assert_equal(test, control)
|
| 92 |
+
# Nested dtype
|
| 93 |
+
dt = np.dtype([('a', 'f'), ('b', [('ba', 'f'), ('bb', 'f')])])
|
| 94 |
+
test = masked_all((2,), dtype=dt)
|
| 95 |
+
control = array([(1, (1, 1)), (1, (1, 1))],
|
| 96 |
+
mask=[(1, (1, 1)), (1, (1, 1))], dtype=dt)
|
| 97 |
+
assert_equal(test, control)
|
| 98 |
+
test = masked_all((2,), dtype=dt)
|
| 99 |
+
control = array([(1, (1, 1)), (1, (1, 1))],
|
| 100 |
+
mask=[(1, (1, 1)), (1, (1, 1))], dtype=dt)
|
| 101 |
+
assert_equal(test, control)
|
| 102 |
+
test = masked_all((1, 1), dtype=dt)
|
| 103 |
+
control = array([[(1, (1, 1))]], mask=[[(1, (1, 1))]], dtype=dt)
|
| 104 |
+
assert_equal(test, control)
|
| 105 |
+
|
| 106 |
+
def test_masked_all_with_object_nested(self):
|
| 107 |
+
# Test masked_all works with nested array with dtype of an 'object'
|
| 108 |
+
# refers to issue #15895
|
| 109 |
+
my_dtype = np.dtype([('b', ([('c', object)], (1,)))])
|
| 110 |
+
masked_arr = np.ma.masked_all((1,), my_dtype)
|
| 111 |
+
|
| 112 |
+
assert_equal(type(masked_arr['b']), np.ma.core.MaskedArray)
|
| 113 |
+
assert_equal(type(masked_arr['b']['c']), np.ma.core.MaskedArray)
|
| 114 |
+
assert_equal(len(masked_arr['b']['c']), 1)
|
| 115 |
+
assert_equal(masked_arr['b']['c'].shape, (1, 1))
|
| 116 |
+
assert_equal(masked_arr['b']['c']._fill_value.shape, ())
|
| 117 |
+
|
| 118 |
+
def test_masked_all_with_object(self):
|
| 119 |
+
# same as above except that the array is not nested
|
| 120 |
+
my_dtype = np.dtype([('b', (object, (1,)))])
|
| 121 |
+
masked_arr = np.ma.masked_all((1,), my_dtype)
|
| 122 |
+
|
| 123 |
+
assert_equal(type(masked_arr['b']), np.ma.core.MaskedArray)
|
| 124 |
+
assert_equal(len(masked_arr['b']), 1)
|
| 125 |
+
assert_equal(masked_arr['b'].shape, (1, 1))
|
| 126 |
+
assert_equal(masked_arr['b']._fill_value.shape, ())
|
| 127 |
+
|
| 128 |
+
def test_masked_all_like(self):
|
| 129 |
+
# Tests masked_all
|
| 130 |
+
# Standard dtype
|
| 131 |
+
base = array([1, 2], dtype=float)
|
| 132 |
+
test = masked_all_like(base)
|
| 133 |
+
control = array([1, 1], mask=[1, 1], dtype=float)
|
| 134 |
+
assert_equal(test, control)
|
| 135 |
+
# Flexible dtype
|
| 136 |
+
dt = np.dtype({'names': ['a', 'b'], 'formats': ['f', 'f']})
|
| 137 |
+
base = array([(0, 0), (0, 0)], mask=[(1, 1), (1, 1)], dtype=dt)
|
| 138 |
+
test = masked_all_like(base)
|
| 139 |
+
control = array([(10, 10), (10, 10)], mask=[(1, 1), (1, 1)], dtype=dt)
|
| 140 |
+
assert_equal(test, control)
|
| 141 |
+
# Nested dtype
|
| 142 |
+
dt = np.dtype([('a', 'f'), ('b', [('ba', 'f'), ('bb', 'f')])])
|
| 143 |
+
control = array([(1, (1, 1)), (1, (1, 1))],
|
| 144 |
+
mask=[(1, (1, 1)), (1, (1, 1))], dtype=dt)
|
| 145 |
+
test = masked_all_like(control)
|
| 146 |
+
assert_equal(test, control)
|
| 147 |
+
|
| 148 |
+
def check_clump(self, f):
|
| 149 |
+
for i in range(1, 7):
|
| 150 |
+
for j in range(2**i):
|
| 151 |
+
k = np.arange(i, dtype=int)
|
| 152 |
+
ja = np.full(i, j, dtype=int)
|
| 153 |
+
a = masked_array(2**k)
|
| 154 |
+
a.mask = (ja & (2**k)) != 0
|
| 155 |
+
s = 0
|
| 156 |
+
for sl in f(a):
|
| 157 |
+
s += a.data[sl].sum()
|
| 158 |
+
if f == clump_unmasked:
|
| 159 |
+
assert_equal(a.compressed().sum(), s)
|
| 160 |
+
else:
|
| 161 |
+
a.mask = ~a.mask
|
| 162 |
+
assert_equal(a.compressed().sum(), s)
|
| 163 |
+
|
| 164 |
+
def test_clump_masked(self):
|
| 165 |
+
# Test clump_masked
|
| 166 |
+
a = masked_array(np.arange(10))
|
| 167 |
+
a[[0, 1, 2, 6, 8, 9]] = masked
|
| 168 |
+
#
|
| 169 |
+
test = clump_masked(a)
|
| 170 |
+
control = [slice(0, 3), slice(6, 7), slice(8, 10)]
|
| 171 |
+
assert_equal(test, control)
|
| 172 |
+
|
| 173 |
+
self.check_clump(clump_masked)
|
| 174 |
+
|
| 175 |
+
def test_clump_unmasked(self):
|
| 176 |
+
# Test clump_unmasked
|
| 177 |
+
a = masked_array(np.arange(10))
|
| 178 |
+
a[[0, 1, 2, 6, 8, 9]] = masked
|
| 179 |
+
test = clump_unmasked(a)
|
| 180 |
+
control = [slice(3, 6), slice(7, 8), ]
|
| 181 |
+
assert_equal(test, control)
|
| 182 |
+
|
| 183 |
+
self.check_clump(clump_unmasked)
|
| 184 |
+
|
| 185 |
+
def test_flatnotmasked_contiguous(self):
|
| 186 |
+
# Test flatnotmasked_contiguous
|
| 187 |
+
a = arange(10)
|
| 188 |
+
# No mask
|
| 189 |
+
test = flatnotmasked_contiguous(a)
|
| 190 |
+
assert_equal(test, [slice(0, a.size)])
|
| 191 |
+
# mask of all false
|
| 192 |
+
a.mask = np.zeros(10, dtype=bool)
|
| 193 |
+
assert_equal(test, [slice(0, a.size)])
|
| 194 |
+
# Some mask
|
| 195 |
+
a[(a < 3) | (a > 8) | (a == 5)] = masked
|
| 196 |
+
test = flatnotmasked_contiguous(a)
|
| 197 |
+
assert_equal(test, [slice(3, 5), slice(6, 9)])
|
| 198 |
+
#
|
| 199 |
+
a[:] = masked
|
| 200 |
+
test = flatnotmasked_contiguous(a)
|
| 201 |
+
assert_equal(test, [])
|
| 202 |
+
|
| 203 |
+
|
| 204 |
+
class TestAverage:
|
| 205 |
+
# Several tests of average. Why so many ? Good point...
|
| 206 |
+
def test_testAverage1(self):
|
| 207 |
+
# Test of average.
|
| 208 |
+
ott = array([0., 1., 2., 3.], mask=[True, False, False, False])
|
| 209 |
+
assert_equal(2.0, average(ott, axis=0))
|
| 210 |
+
assert_equal(2.0, average(ott, weights=[1., 1., 2., 1.]))
|
| 211 |
+
result, wts = average(ott, weights=[1., 1., 2., 1.], returned=True)
|
| 212 |
+
assert_equal(2.0, result)
|
| 213 |
+
assert_(wts == 4.0)
|
| 214 |
+
ott[:] = masked
|
| 215 |
+
assert_equal(average(ott, axis=0).mask, [True])
|
| 216 |
+
ott = array([0., 1., 2., 3.], mask=[True, False, False, False])
|
| 217 |
+
ott = ott.reshape(2, 2)
|
| 218 |
+
ott[:, 1] = masked
|
| 219 |
+
assert_equal(average(ott, axis=0), [2.0, 0.0])
|
| 220 |
+
assert_equal(average(ott, axis=1).mask[0], [True])
|
| 221 |
+
assert_equal([2., 0.], average(ott, axis=0))
|
| 222 |
+
result, wts = average(ott, axis=0, returned=True)
|
| 223 |
+
assert_equal(wts, [1., 0.])
|
| 224 |
+
|
| 225 |
+
def test_testAverage2(self):
|
| 226 |
+
# More tests of average.
|
| 227 |
+
w1 = [0, 1, 1, 1, 1, 0]
|
| 228 |
+
w2 = [[0, 1, 1, 1, 1, 0], [1, 0, 0, 0, 0, 1]]
|
| 229 |
+
x = arange(6, dtype=np.float64)
|
| 230 |
+
assert_equal(average(x, axis=0), 2.5)
|
| 231 |
+
assert_equal(average(x, axis=0, weights=w1), 2.5)
|
| 232 |
+
y = array([arange(6, dtype=np.float64), 2.0 * arange(6)])
|
| 233 |
+
assert_equal(average(y, None), np.add.reduce(np.arange(6)) * 3. / 12.)
|
| 234 |
+
assert_equal(average(y, axis=0), np.arange(6) * 3. / 2.)
|
| 235 |
+
assert_equal(average(y, axis=1),
|
| 236 |
+
[average(x, axis=0), average(x, axis=0) * 2.0])
|
| 237 |
+
assert_equal(average(y, None, weights=w2), 20. / 6.)
|
| 238 |
+
assert_equal(average(y, axis=0, weights=w2),
|
| 239 |
+
[0., 1., 2., 3., 4., 10.])
|
| 240 |
+
assert_equal(average(y, axis=1),
|
| 241 |
+
[average(x, axis=0), average(x, axis=0) * 2.0])
|
| 242 |
+
m1 = zeros(6)
|
| 243 |
+
m2 = [0, 0, 1, 1, 0, 0]
|
| 244 |
+
m3 = [[0, 0, 1, 1, 0, 0], [0, 1, 1, 1, 1, 0]]
|
| 245 |
+
m4 = ones(6)
|
| 246 |
+
m5 = [0, 1, 1, 1, 1, 1]
|
| 247 |
+
assert_equal(average(masked_array(x, m1), axis=0), 2.5)
|
| 248 |
+
assert_equal(average(masked_array(x, m2), axis=0), 2.5)
|
| 249 |
+
assert_equal(average(masked_array(x, m4), axis=0).mask, [True])
|
| 250 |
+
assert_equal(average(masked_array(x, m5), axis=0), 0.0)
|
| 251 |
+
assert_equal(count(average(masked_array(x, m4), axis=0)), 0)
|
| 252 |
+
z = masked_array(y, m3)
|
| 253 |
+
assert_equal(average(z, None), 20. / 6.)
|
| 254 |
+
assert_equal(average(z, axis=0), [0., 1., 99., 99., 4.0, 7.5])
|
| 255 |
+
assert_equal(average(z, axis=1), [2.5, 5.0])
|
| 256 |
+
assert_equal(average(z, axis=0, weights=w2),
|
| 257 |
+
[0., 1., 99., 99., 4.0, 10.0])
|
| 258 |
+
|
| 259 |
+
def test_testAverage3(self):
|
| 260 |
+
# Yet more tests of average!
|
| 261 |
+
a = arange(6)
|
| 262 |
+
b = arange(6) * 3
|
| 263 |
+
r1, w1 = average([[a, b], [b, a]], axis=1, returned=True)
|
| 264 |
+
assert_equal(shape(r1), shape(w1))
|
| 265 |
+
assert_equal(r1.shape, w1.shape)
|
| 266 |
+
r2, w2 = average(ones((2, 2, 3)), axis=0, weights=[3, 1], returned=True)
|
| 267 |
+
assert_equal(shape(w2), shape(r2))
|
| 268 |
+
r2, w2 = average(ones((2, 2, 3)), returned=True)
|
| 269 |
+
assert_equal(shape(w2), shape(r2))
|
| 270 |
+
r2, w2 = average(ones((2, 2, 3)), weights=ones((2, 2, 3)), returned=True)
|
| 271 |
+
assert_equal(shape(w2), shape(r2))
|
| 272 |
+
a2d = array([[1, 2], [0, 4]], float)
|
| 273 |
+
a2dm = masked_array(a2d, [[False, False], [True, False]])
|
| 274 |
+
a2da = average(a2d, axis=0)
|
| 275 |
+
assert_equal(a2da, [0.5, 3.0])
|
| 276 |
+
a2dma = average(a2dm, axis=0)
|
| 277 |
+
assert_equal(a2dma, [1.0, 3.0])
|
| 278 |
+
a2dma = average(a2dm, axis=None)
|
| 279 |
+
assert_equal(a2dma, 7. / 3.)
|
| 280 |
+
a2dma = average(a2dm, axis=1)
|
| 281 |
+
assert_equal(a2dma, [1.5, 4.0])
|
| 282 |
+
|
| 283 |
+
def test_testAverage4(self):
|
| 284 |
+
# Test that `keepdims` works with average
|
| 285 |
+
x = np.array([2, 3, 4]).reshape(3, 1)
|
| 286 |
+
b = np.ma.array(x, mask=[[False], [False], [True]])
|
| 287 |
+
w = np.array([4, 5, 6]).reshape(3, 1)
|
| 288 |
+
actual = average(b, weights=w, axis=1, keepdims=True)
|
| 289 |
+
desired = masked_array([[2.], [3.], [4.]], [[False], [False], [True]])
|
| 290 |
+
assert_equal(actual, desired)
|
| 291 |
+
|
| 292 |
+
def test_weight_and_input_dims_different(self):
|
| 293 |
+
# this test mirrors a test for np.average()
|
| 294 |
+
# in lib/test/test_function_base.py
|
| 295 |
+
y = np.arange(12).reshape(2, 2, 3)
|
| 296 |
+
w = np.array([0., 0., 1., .5, .5, 0., 0., .5, .5, 1., 0., 0.])\
|
| 297 |
+
.reshape(2, 2, 3)
|
| 298 |
+
|
| 299 |
+
m = np.full((2, 2, 3), False)
|
| 300 |
+
yma = np.ma.array(y, mask=m)
|
| 301 |
+
subw0 = w[:, :, 0]
|
| 302 |
+
|
| 303 |
+
actual = average(yma, axis=(0, 1), weights=subw0)
|
| 304 |
+
desired = masked_array([7., 8., 9.], mask=[False, False, False])
|
| 305 |
+
assert_almost_equal(actual, desired)
|
| 306 |
+
|
| 307 |
+
m = np.full((2, 2, 3), False)
|
| 308 |
+
m[:, :, 0] = True
|
| 309 |
+
m[0, 0, 1] = True
|
| 310 |
+
yma = np.ma.array(y, mask=m)
|
| 311 |
+
actual = average(yma, axis=(0, 1), weights=subw0)
|
| 312 |
+
desired = masked_array(
|
| 313 |
+
[np.nan, 8., 9.],
|
| 314 |
+
mask=[True, False, False])
|
| 315 |
+
assert_almost_equal(actual, desired)
|
| 316 |
+
|
| 317 |
+
m = np.full((2, 2, 3), False)
|
| 318 |
+
yma = np.ma.array(y, mask=m)
|
| 319 |
+
|
| 320 |
+
subw1 = w[1, :, :]
|
| 321 |
+
actual = average(yma, axis=(1, 2), weights=subw1)
|
| 322 |
+
desired = masked_array([2.25, 8.25], mask=[False, False])
|
| 323 |
+
assert_almost_equal(actual, desired)
|
| 324 |
+
|
| 325 |
+
# here the weights have the wrong shape for the specified axes
|
| 326 |
+
with pytest.raises(
|
| 327 |
+
ValueError,
|
| 328 |
+
match="Shape of weights must be consistent with "
|
| 329 |
+
"shape of a along specified axis"):
|
| 330 |
+
average(yma, axis=(0, 1, 2), weights=subw0)
|
| 331 |
+
|
| 332 |
+
with pytest.raises(
|
| 333 |
+
ValueError,
|
| 334 |
+
match="Shape of weights must be consistent with "
|
| 335 |
+
"shape of a along specified axis"):
|
| 336 |
+
average(yma, axis=(0, 1), weights=subw1)
|
| 337 |
+
|
| 338 |
+
# swapping the axes should be same as transposing weights
|
| 339 |
+
actual = average(yma, axis=(1, 0), weights=subw0)
|
| 340 |
+
desired = average(yma, axis=(0, 1), weights=subw0.T)
|
| 341 |
+
assert_almost_equal(actual, desired)
|
| 342 |
+
|
| 343 |
+
def test_onintegers_with_mask(self):
|
| 344 |
+
# Test average on integers with mask
|
| 345 |
+
a = average(array([1, 2]))
|
| 346 |
+
assert_equal(a, 1.5)
|
| 347 |
+
a = average(array([1, 2, 3, 4], mask=[False, False, True, True]))
|
| 348 |
+
assert_equal(a, 1.5)
|
| 349 |
+
|
| 350 |
+
def test_complex(self):
|
| 351 |
+
# Test with complex data.
|
| 352 |
+
# (Regression test for https://github.com/numpy/numpy/issues/2684)
|
| 353 |
+
mask = np.array([[0, 0, 0, 1, 0],
|
| 354 |
+
[0, 1, 0, 0, 0]], dtype=bool)
|
| 355 |
+
a = masked_array([[0, 1 + 2j, 3 + 4j, 5 + 6j, 7 + 8j],
|
| 356 |
+
[9j, 0 + 1j, 2 + 3j, 4 + 5j, 7 + 7j]],
|
| 357 |
+
mask=mask)
|
| 358 |
+
|
| 359 |
+
av = average(a)
|
| 360 |
+
expected = np.average(a.compressed())
|
| 361 |
+
assert_almost_equal(av.real, expected.real)
|
| 362 |
+
assert_almost_equal(av.imag, expected.imag)
|
| 363 |
+
|
| 364 |
+
av0 = average(a, axis=0)
|
| 365 |
+
expected0 = average(a.real, axis=0) + average(a.imag, axis=0) * 1j
|
| 366 |
+
assert_almost_equal(av0.real, expected0.real)
|
| 367 |
+
assert_almost_equal(av0.imag, expected0.imag)
|
| 368 |
+
|
| 369 |
+
av1 = average(a, axis=1)
|
| 370 |
+
expected1 = average(a.real, axis=1) + average(a.imag, axis=1) * 1j
|
| 371 |
+
assert_almost_equal(av1.real, expected1.real)
|
| 372 |
+
assert_almost_equal(av1.imag, expected1.imag)
|
| 373 |
+
|
| 374 |
+
# Test with the 'weights' argument.
|
| 375 |
+
wts = np.array([[0.5, 1.0, 2.0, 1.0, 0.5],
|
| 376 |
+
[1.0, 1.0, 1.0, 1.0, 1.0]])
|
| 377 |
+
wav = average(a, weights=wts)
|
| 378 |
+
expected = np.average(a.compressed(), weights=wts[~mask])
|
| 379 |
+
assert_almost_equal(wav.real, expected.real)
|
| 380 |
+
assert_almost_equal(wav.imag, expected.imag)
|
| 381 |
+
|
| 382 |
+
wav0 = average(a, weights=wts, axis=0)
|
| 383 |
+
expected0 = (average(a.real, weights=wts, axis=0) +
|
| 384 |
+
average(a.imag, weights=wts, axis=0) * 1j)
|
| 385 |
+
assert_almost_equal(wav0.real, expected0.real)
|
| 386 |
+
assert_almost_equal(wav0.imag, expected0.imag)
|
| 387 |
+
|
| 388 |
+
wav1 = average(a, weights=wts, axis=1)
|
| 389 |
+
expected1 = (average(a.real, weights=wts, axis=1) +
|
| 390 |
+
average(a.imag, weights=wts, axis=1) * 1j)
|
| 391 |
+
assert_almost_equal(wav1.real, expected1.real)
|
| 392 |
+
assert_almost_equal(wav1.imag, expected1.imag)
|
| 393 |
+
|
| 394 |
+
@pytest.mark.parametrize(
|
| 395 |
+
'x, axis, expected_avg, weights, expected_wavg, expected_wsum',
|
| 396 |
+
[([1, 2, 3], None, [2.0], [3, 4, 1], [1.75], [8.0]),
|
| 397 |
+
([[1, 2, 5], [1, 6, 11]], 0, [[1.0, 4.0, 8.0]],
|
| 398 |
+
[1, 3], [[1.0, 5.0, 9.5]], [[4, 4, 4]])],
|
| 399 |
+
)
|
| 400 |
+
def test_basic_keepdims(self, x, axis, expected_avg,
|
| 401 |
+
weights, expected_wavg, expected_wsum):
|
| 402 |
+
avg = np.ma.average(x, axis=axis, keepdims=True)
|
| 403 |
+
assert avg.shape == np.shape(expected_avg)
|
| 404 |
+
assert_array_equal(avg, expected_avg)
|
| 405 |
+
|
| 406 |
+
wavg = np.ma.average(x, axis=axis, weights=weights, keepdims=True)
|
| 407 |
+
assert wavg.shape == np.shape(expected_wavg)
|
| 408 |
+
assert_array_equal(wavg, expected_wavg)
|
| 409 |
+
|
| 410 |
+
wavg, wsum = np.ma.average(x, axis=axis, weights=weights,
|
| 411 |
+
returned=True, keepdims=True)
|
| 412 |
+
assert wavg.shape == np.shape(expected_wavg)
|
| 413 |
+
assert_array_equal(wavg, expected_wavg)
|
| 414 |
+
assert wsum.shape == np.shape(expected_wsum)
|
| 415 |
+
assert_array_equal(wsum, expected_wsum)
|
| 416 |
+
|
| 417 |
+
def test_masked_weights(self):
|
| 418 |
+
# Test with masked weights.
|
| 419 |
+
# (Regression test for https://github.com/numpy/numpy/issues/10438)
|
| 420 |
+
a = np.ma.array(np.arange(9).reshape(3, 3),
|
| 421 |
+
mask=[[1, 0, 0], [1, 0, 0], [0, 0, 0]])
|
| 422 |
+
weights_unmasked = masked_array([5, 28, 31], mask=False)
|
| 423 |
+
weights_masked = masked_array([5, 28, 31], mask=[1, 0, 0])
|
| 424 |
+
|
| 425 |
+
avg_unmasked = average(a, axis=0,
|
| 426 |
+
weights=weights_unmasked, returned=False)
|
| 427 |
+
expected_unmasked = np.array([6.0, 5.21875, 6.21875])
|
| 428 |
+
assert_almost_equal(avg_unmasked, expected_unmasked)
|
| 429 |
+
|
| 430 |
+
avg_masked = average(a, axis=0, weights=weights_masked, returned=False)
|
| 431 |
+
expected_masked = np.array([6.0, 5.576271186440678, 6.576271186440678])
|
| 432 |
+
assert_almost_equal(avg_masked, expected_masked)
|
| 433 |
+
|
| 434 |
+
# weights should be masked if needed
|
| 435 |
+
# depending on the array mask. This is to avoid summing
|
| 436 |
+
# masked nan or other values that are not cancelled by a zero
|
| 437 |
+
a = np.ma.array([1.0, 2.0, 3.0, 4.0],
|
| 438 |
+
mask=[False, False, True, True])
|
| 439 |
+
avg_unmasked = average(a, weights=[1, 1, 1, np.nan])
|
| 440 |
+
|
| 441 |
+
assert_almost_equal(avg_unmasked, 1.5)
|
| 442 |
+
|
| 443 |
+
a = np.ma.array([
|
| 444 |
+
[1.0, 2.0, 3.0, 4.0],
|
| 445 |
+
[5.0, 6.0, 7.0, 8.0],
|
| 446 |
+
[9.0, 1.0, 2.0, 3.0],
|
| 447 |
+
], mask=[
|
| 448 |
+
[False, True, True, False],
|
| 449 |
+
[True, False, True, True],
|
| 450 |
+
[True, False, True, False],
|
| 451 |
+
])
|
| 452 |
+
|
| 453 |
+
avg_masked = np.ma.average(a, weights=[1, np.nan, 1], axis=0)
|
| 454 |
+
avg_expected = np.ma.array([1.0, np.nan, np.nan, 3.5],
|
| 455 |
+
mask=[False, True, True, False])
|
| 456 |
+
|
| 457 |
+
assert_almost_equal(avg_masked, avg_expected)
|
| 458 |
+
assert_equal(avg_masked.mask, avg_expected.mask)
|
| 459 |
+
|
| 460 |
+
|
| 461 |
+
class TestConcatenator:
|
| 462 |
+
# Tests for mr_, the equivalent of r_ for masked arrays.
|
| 463 |
+
|
| 464 |
+
def test_1d(self):
|
| 465 |
+
# Tests mr_ on 1D arrays.
|
| 466 |
+
assert_array_equal(mr_[1, 2, 3, 4, 5, 6], array([1, 2, 3, 4, 5, 6]))
|
| 467 |
+
b = ones(5)
|
| 468 |
+
m = [1, 0, 0, 0, 0]
|
| 469 |
+
d = masked_array(b, mask=m)
|
| 470 |
+
c = mr_[d, 0, 0, d]
|
| 471 |
+
assert_(isinstance(c, MaskedArray))
|
| 472 |
+
assert_array_equal(c, [1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1])
|
| 473 |
+
assert_array_equal(c.mask, mr_[m, 0, 0, m])
|
| 474 |
+
|
| 475 |
+
def test_2d(self):
|
| 476 |
+
# Tests mr_ on 2D arrays.
|
| 477 |
+
a_1 = np.random.rand(5, 5)
|
| 478 |
+
a_2 = np.random.rand(5, 5)
|
| 479 |
+
m_1 = np.round(np.random.rand(5, 5), 0)
|
| 480 |
+
m_2 = np.round(np.random.rand(5, 5), 0)
|
| 481 |
+
b_1 = masked_array(a_1, mask=m_1)
|
| 482 |
+
b_2 = masked_array(a_2, mask=m_2)
|
| 483 |
+
# append columns
|
| 484 |
+
d = mr_['1', b_1, b_2]
|
| 485 |
+
assert_(d.shape == (5, 10))
|
| 486 |
+
assert_array_equal(d[:, :5], b_1)
|
| 487 |
+
assert_array_equal(d[:, 5:], b_2)
|
| 488 |
+
assert_array_equal(d.mask, np.r_['1', m_1, m_2])
|
| 489 |
+
d = mr_[b_1, b_2]
|
| 490 |
+
assert_(d.shape == (10, 5))
|
| 491 |
+
assert_array_equal(d[:5, :], b_1)
|
| 492 |
+
assert_array_equal(d[5:, :], b_2)
|
| 493 |
+
assert_array_equal(d.mask, np.r_[m_1, m_2])
|
| 494 |
+
|
| 495 |
+
def test_masked_constant(self):
|
| 496 |
+
actual = mr_[np.ma.masked, 1]
|
| 497 |
+
assert_equal(actual.mask, [True, False])
|
| 498 |
+
assert_equal(actual.data[1], 1)
|
| 499 |
+
|
| 500 |
+
actual = mr_[[1, 2], np.ma.masked]
|
| 501 |
+
assert_equal(actual.mask, [False, False, True])
|
| 502 |
+
assert_equal(actual.data[:2], [1, 2])
|
| 503 |
+
|
| 504 |
+
|
| 505 |
+
class TestNotMasked:
|
| 506 |
+
# Tests notmasked_edges and notmasked_contiguous.
|
| 507 |
+
|
| 508 |
+
def test_edges(self):
|
| 509 |
+
# Tests unmasked_edges
|
| 510 |
+
data = masked_array(np.arange(25).reshape(5, 5),
|
| 511 |
+
mask=[[0, 0, 1, 0, 0],
|
| 512 |
+
[0, 0, 0, 1, 1],
|
| 513 |
+
[1, 1, 0, 0, 0],
|
| 514 |
+
[0, 0, 0, 0, 0],
|
| 515 |
+
[1, 1, 1, 0, 0]],)
|
| 516 |
+
test = notmasked_edges(data, None)
|
| 517 |
+
assert_equal(test, [0, 24])
|
| 518 |
+
test = notmasked_edges(data, 0)
|
| 519 |
+
assert_equal(test[0], [(0, 0, 1, 0, 0), (0, 1, 2, 3, 4)])
|
| 520 |
+
assert_equal(test[1], [(3, 3, 3, 4, 4), (0, 1, 2, 3, 4)])
|
| 521 |
+
test = notmasked_edges(data, 1)
|
| 522 |
+
assert_equal(test[0], [(0, 1, 2, 3, 4), (0, 0, 2, 0, 3)])
|
| 523 |
+
assert_equal(test[1], [(0, 1, 2, 3, 4), (4, 2, 4, 4, 4)])
|
| 524 |
+
#
|
| 525 |
+
test = notmasked_edges(data.data, None)
|
| 526 |
+
assert_equal(test, [0, 24])
|
| 527 |
+
test = notmasked_edges(data.data, 0)
|
| 528 |
+
assert_equal(test[0], [(0, 0, 0, 0, 0), (0, 1, 2, 3, 4)])
|
| 529 |
+
assert_equal(test[1], [(4, 4, 4, 4, 4), (0, 1, 2, 3, 4)])
|
| 530 |
+
test = notmasked_edges(data.data, -1)
|
| 531 |
+
assert_equal(test[0], [(0, 1, 2, 3, 4), (0, 0, 0, 0, 0)])
|
| 532 |
+
assert_equal(test[1], [(0, 1, 2, 3, 4), (4, 4, 4, 4, 4)])
|
| 533 |
+
#
|
| 534 |
+
data[-2] = masked
|
| 535 |
+
test = notmasked_edges(data, 0)
|
| 536 |
+
assert_equal(test[0], [(0, 0, 1, 0, 0), (0, 1, 2, 3, 4)])
|
| 537 |
+
assert_equal(test[1], [(1, 1, 2, 4, 4), (0, 1, 2, 3, 4)])
|
| 538 |
+
test = notmasked_edges(data, -1)
|
| 539 |
+
assert_equal(test[0], [(0, 1, 2, 4), (0, 0, 2, 3)])
|
| 540 |
+
assert_equal(test[1], [(0, 1, 2, 4), (4, 2, 4, 4)])
|
| 541 |
+
|
| 542 |
+
def test_contiguous(self):
|
| 543 |
+
# Tests notmasked_contiguous
|
| 544 |
+
a = masked_array(np.arange(24).reshape(3, 8),
|
| 545 |
+
mask=[[0, 0, 0, 0, 1, 1, 1, 1],
|
| 546 |
+
[1, 1, 1, 1, 1, 1, 1, 1],
|
| 547 |
+
[0, 0, 0, 0, 0, 0, 1, 0]])
|
| 548 |
+
tmp = notmasked_contiguous(a, None)
|
| 549 |
+
assert_equal(tmp, [
|
| 550 |
+
slice(0, 4, None),
|
| 551 |
+
slice(16, 22, None),
|
| 552 |
+
slice(23, 24, None)
|
| 553 |
+
])
|
| 554 |
+
|
| 555 |
+
tmp = notmasked_contiguous(a, 0)
|
| 556 |
+
assert_equal(tmp, [
|
| 557 |
+
[slice(0, 1, None), slice(2, 3, None)],
|
| 558 |
+
[slice(0, 1, None), slice(2, 3, None)],
|
| 559 |
+
[slice(0, 1, None), slice(2, 3, None)],
|
| 560 |
+
[slice(0, 1, None), slice(2, 3, None)],
|
| 561 |
+
[slice(2, 3, None)],
|
| 562 |
+
[slice(2, 3, None)],
|
| 563 |
+
[],
|
| 564 |
+
[slice(2, 3, None)]
|
| 565 |
+
])
|
| 566 |
+
#
|
| 567 |
+
tmp = notmasked_contiguous(a, 1)
|
| 568 |
+
assert_equal(tmp, [
|
| 569 |
+
[slice(0, 4, None)],
|
| 570 |
+
[],
|
| 571 |
+
[slice(0, 6, None), slice(7, 8, None)]
|
| 572 |
+
])
|
| 573 |
+
|
| 574 |
+
|
| 575 |
+
class TestCompressFunctions:
|
| 576 |
+
|
| 577 |
+
def test_compress_nd(self):
|
| 578 |
+
# Tests compress_nd
|
| 579 |
+
x = np.array(list(range(3 * 4 * 5))).reshape(3, 4, 5)
|
| 580 |
+
m = np.zeros((3, 4, 5)).astype(bool)
|
| 581 |
+
m[1, 1, 1] = True
|
| 582 |
+
x = array(x, mask=m)
|
| 583 |
+
|
| 584 |
+
# axis=None
|
| 585 |
+
a = compress_nd(x)
|
| 586 |
+
assert_equal(a, [[[ 0, 2, 3, 4],
|
| 587 |
+
[10, 12, 13, 14],
|
| 588 |
+
[15, 17, 18, 19]],
|
| 589 |
+
[[40, 42, 43, 44],
|
| 590 |
+
[50, 52, 53, 54],
|
| 591 |
+
[55, 57, 58, 59]]])
|
| 592 |
+
|
| 593 |
+
# axis=0
|
| 594 |
+
a = compress_nd(x, 0)
|
| 595 |
+
assert_equal(a, [[[ 0, 1, 2, 3, 4],
|
| 596 |
+
[ 5, 6, 7, 8, 9],
|
| 597 |
+
[10, 11, 12, 13, 14],
|
| 598 |
+
[15, 16, 17, 18, 19]],
|
| 599 |
+
[[40, 41, 42, 43, 44],
|
| 600 |
+
[45, 46, 47, 48, 49],
|
| 601 |
+
[50, 51, 52, 53, 54],
|
| 602 |
+
[55, 56, 57, 58, 59]]])
|
| 603 |
+
|
| 604 |
+
# axis=1
|
| 605 |
+
a = compress_nd(x, 1)
|
| 606 |
+
assert_equal(a, [[[ 0, 1, 2, 3, 4],
|
| 607 |
+
[10, 11, 12, 13, 14],
|
| 608 |
+
[15, 16, 17, 18, 19]],
|
| 609 |
+
[[20, 21, 22, 23, 24],
|
| 610 |
+
[30, 31, 32, 33, 34],
|
| 611 |
+
[35, 36, 37, 38, 39]],
|
| 612 |
+
[[40, 41, 42, 43, 44],
|
| 613 |
+
[50, 51, 52, 53, 54],
|
| 614 |
+
[55, 56, 57, 58, 59]]])
|
| 615 |
+
|
| 616 |
+
a2 = compress_nd(x, (1,))
|
| 617 |
+
a3 = compress_nd(x, -2)
|
| 618 |
+
a4 = compress_nd(x, (-2,))
|
| 619 |
+
assert_equal(a, a2)
|
| 620 |
+
assert_equal(a, a3)
|
| 621 |
+
assert_equal(a, a4)
|
| 622 |
+
|
| 623 |
+
# axis=2
|
| 624 |
+
a = compress_nd(x, 2)
|
| 625 |
+
assert_equal(a, [[[ 0, 2, 3, 4],
|
| 626 |
+
[ 5, 7, 8, 9],
|
| 627 |
+
[10, 12, 13, 14],
|
| 628 |
+
[15, 17, 18, 19]],
|
| 629 |
+
[[20, 22, 23, 24],
|
| 630 |
+
[25, 27, 28, 29],
|
| 631 |
+
[30, 32, 33, 34],
|
| 632 |
+
[35, 37, 38, 39]],
|
| 633 |
+
[[40, 42, 43, 44],
|
| 634 |
+
[45, 47, 48, 49],
|
| 635 |
+
[50, 52, 53, 54],
|
| 636 |
+
[55, 57, 58, 59]]])
|
| 637 |
+
|
| 638 |
+
a2 = compress_nd(x, (2,))
|
| 639 |
+
a3 = compress_nd(x, -1)
|
| 640 |
+
a4 = compress_nd(x, (-1,))
|
| 641 |
+
assert_equal(a, a2)
|
| 642 |
+
assert_equal(a, a3)
|
| 643 |
+
assert_equal(a, a4)
|
| 644 |
+
|
| 645 |
+
# axis=(0, 1)
|
| 646 |
+
a = compress_nd(x, (0, 1))
|
| 647 |
+
assert_equal(a, [[[ 0, 1, 2, 3, 4],
|
| 648 |
+
[10, 11, 12, 13, 14],
|
| 649 |
+
[15, 16, 17, 18, 19]],
|
| 650 |
+
[[40, 41, 42, 43, 44],
|
| 651 |
+
[50, 51, 52, 53, 54],
|
| 652 |
+
[55, 56, 57, 58, 59]]])
|
| 653 |
+
a2 = compress_nd(x, (0, -2))
|
| 654 |
+
assert_equal(a, a2)
|
| 655 |
+
|
| 656 |
+
# axis=(1, 2)
|
| 657 |
+
a = compress_nd(x, (1, 2))
|
| 658 |
+
assert_equal(a, [[[ 0, 2, 3, 4],
|
| 659 |
+
[10, 12, 13, 14],
|
| 660 |
+
[15, 17, 18, 19]],
|
| 661 |
+
[[20, 22, 23, 24],
|
| 662 |
+
[30, 32, 33, 34],
|
| 663 |
+
[35, 37, 38, 39]],
|
| 664 |
+
[[40, 42, 43, 44],
|
| 665 |
+
[50, 52, 53, 54],
|
| 666 |
+
[55, 57, 58, 59]]])
|
| 667 |
+
|
| 668 |
+
a2 = compress_nd(x, (-2, 2))
|
| 669 |
+
a3 = compress_nd(x, (1, -1))
|
| 670 |
+
a4 = compress_nd(x, (-2, -1))
|
| 671 |
+
assert_equal(a, a2)
|
| 672 |
+
assert_equal(a, a3)
|
| 673 |
+
assert_equal(a, a4)
|
| 674 |
+
|
| 675 |
+
# axis=(0, 2)
|
| 676 |
+
a = compress_nd(x, (0, 2))
|
| 677 |
+
assert_equal(a, [[[ 0, 2, 3, 4],
|
| 678 |
+
[ 5, 7, 8, 9],
|
| 679 |
+
[10, 12, 13, 14],
|
| 680 |
+
[15, 17, 18, 19]],
|
| 681 |
+
[[40, 42, 43, 44],
|
| 682 |
+
[45, 47, 48, 49],
|
| 683 |
+
[50, 52, 53, 54],
|
| 684 |
+
[55, 57, 58, 59]]])
|
| 685 |
+
|
| 686 |
+
a2 = compress_nd(x, (0, -1))
|
| 687 |
+
assert_equal(a, a2)
|
| 688 |
+
|
| 689 |
+
def test_compress_rowcols(self):
|
| 690 |
+
# Tests compress_rowcols
|
| 691 |
+
x = array(np.arange(9).reshape(3, 3),
|
| 692 |
+
mask=[[1, 0, 0], [0, 0, 0], [0, 0, 0]])
|
| 693 |
+
assert_equal(compress_rowcols(x), [[4, 5], [7, 8]])
|
| 694 |
+
assert_equal(compress_rowcols(x, 0), [[3, 4, 5], [6, 7, 8]])
|
| 695 |
+
assert_equal(compress_rowcols(x, 1), [[1, 2], [4, 5], [7, 8]])
|
| 696 |
+
x = array(x._data, mask=[[0, 0, 0], [0, 1, 0], [0, 0, 0]])
|
| 697 |
+
assert_equal(compress_rowcols(x), [[0, 2], [6, 8]])
|
| 698 |
+
assert_equal(compress_rowcols(x, 0), [[0, 1, 2], [6, 7, 8]])
|
| 699 |
+
assert_equal(compress_rowcols(x, 1), [[0, 2], [3, 5], [6, 8]])
|
| 700 |
+
x = array(x._data, mask=[[1, 0, 0], [0, 1, 0], [0, 0, 0]])
|
| 701 |
+
assert_equal(compress_rowcols(x), [[8]])
|
| 702 |
+
assert_equal(compress_rowcols(x, 0), [[6, 7, 8]])
|
| 703 |
+
assert_equal(compress_rowcols(x, 1,), [[2], [5], [8]])
|
| 704 |
+
x = array(x._data, mask=[[1, 0, 0], [0, 1, 0], [0, 0, 1]])
|
| 705 |
+
assert_equal(compress_rowcols(x).size, 0)
|
| 706 |
+
assert_equal(compress_rowcols(x, 0).size, 0)
|
| 707 |
+
assert_equal(compress_rowcols(x, 1).size, 0)
|
| 708 |
+
|
| 709 |
+
def test_mask_rowcols(self):
|
| 710 |
+
# Tests mask_rowcols.
|
| 711 |
+
x = array(np.arange(9).reshape(3, 3),
|
| 712 |
+
mask=[[1, 0, 0], [0, 0, 0], [0, 0, 0]])
|
| 713 |
+
assert_equal(mask_rowcols(x).mask,
|
| 714 |
+
[[1, 1, 1], [1, 0, 0], [1, 0, 0]])
|
| 715 |
+
assert_equal(mask_rowcols(x, 0).mask,
|
| 716 |
+
[[1, 1, 1], [0, 0, 0], [0, 0, 0]])
|
| 717 |
+
assert_equal(mask_rowcols(x, 1).mask,
|
| 718 |
+
[[1, 0, 0], [1, 0, 0], [1, 0, 0]])
|
| 719 |
+
x = array(x._data, mask=[[0, 0, 0], [0, 1, 0], [0, 0, 0]])
|
| 720 |
+
assert_equal(mask_rowcols(x).mask,
|
| 721 |
+
[[0, 1, 0], [1, 1, 1], [0, 1, 0]])
|
| 722 |
+
assert_equal(mask_rowcols(x, 0).mask,
|
| 723 |
+
[[0, 0, 0], [1, 1, 1], [0, 0, 0]])
|
| 724 |
+
assert_equal(mask_rowcols(x, 1).mask,
|
| 725 |
+
[[0, 1, 0], [0, 1, 0], [0, 1, 0]])
|
| 726 |
+
x = array(x._data, mask=[[1, 0, 0], [0, 1, 0], [0, 0, 0]])
|
| 727 |
+
assert_equal(mask_rowcols(x).mask,
|
| 728 |
+
[[1, 1, 1], [1, 1, 1], [1, 1, 0]])
|
| 729 |
+
assert_equal(mask_rowcols(x, 0).mask,
|
| 730 |
+
[[1, 1, 1], [1, 1, 1], [0, 0, 0]])
|
| 731 |
+
assert_equal(mask_rowcols(x, 1,).mask,
|
| 732 |
+
[[1, 1, 0], [1, 1, 0], [1, 1, 0]])
|
| 733 |
+
x = array(x._data, mask=[[1, 0, 0], [0, 1, 0], [0, 0, 1]])
|
| 734 |
+
assert_(mask_rowcols(x).all() is masked)
|
| 735 |
+
assert_(mask_rowcols(x, 0).all() is masked)
|
| 736 |
+
assert_(mask_rowcols(x, 1).all() is masked)
|
| 737 |
+
assert_(mask_rowcols(x).mask.all())
|
| 738 |
+
assert_(mask_rowcols(x, 0).mask.all())
|
| 739 |
+
assert_(mask_rowcols(x, 1).mask.all())
|
| 740 |
+
|
| 741 |
+
@pytest.mark.parametrize("axis", [None, 0, 1])
|
| 742 |
+
@pytest.mark.parametrize(["func", "rowcols_axis"],
|
| 743 |
+
[(np.ma.mask_rows, 0), (np.ma.mask_cols, 1)])
|
| 744 |
+
def test_mask_row_cols_axis_deprecation(self, axis, func, rowcols_axis):
|
| 745 |
+
# Test deprecation of the axis argument to `mask_rows` and `mask_cols`
|
| 746 |
+
x = array(np.arange(9).reshape(3, 3),
|
| 747 |
+
mask=[[1, 0, 0], [0, 0, 0], [0, 0, 0]])
|
| 748 |
+
|
| 749 |
+
with assert_warns(DeprecationWarning):
|
| 750 |
+
res = func(x, axis=axis)
|
| 751 |
+
assert_equal(res, mask_rowcols(x, rowcols_axis))
|
| 752 |
+
|
| 753 |
+
def test_dot(self):
|
| 754 |
+
# Tests dot product
|
| 755 |
+
n = np.arange(1, 7)
|
| 756 |
+
#
|
| 757 |
+
m = [1, 0, 0, 0, 0, 0]
|
| 758 |
+
a = masked_array(n, mask=m).reshape(2, 3)
|
| 759 |
+
b = masked_array(n, mask=m).reshape(3, 2)
|
| 760 |
+
c = dot(a, b, strict=True)
|
| 761 |
+
assert_equal(c.mask, [[1, 1], [1, 0]])
|
| 762 |
+
c = dot(b, a, strict=True)
|
| 763 |
+
assert_equal(c.mask, [[1, 1, 1], [1, 0, 0], [1, 0, 0]])
|
| 764 |
+
c = dot(a, b, strict=False)
|
| 765 |
+
assert_equal(c, np.dot(a.filled(0), b.filled(0)))
|
| 766 |
+
c = dot(b, a, strict=False)
|
| 767 |
+
assert_equal(c, np.dot(b.filled(0), a.filled(0)))
|
| 768 |
+
#
|
| 769 |
+
m = [0, 0, 0, 0, 0, 1]
|
| 770 |
+
a = masked_array(n, mask=m).reshape(2, 3)
|
| 771 |
+
b = masked_array(n, mask=m).reshape(3, 2)
|
| 772 |
+
c = dot(a, b, strict=True)
|
| 773 |
+
assert_equal(c.mask, [[0, 1], [1, 1]])
|
| 774 |
+
c = dot(b, a, strict=True)
|
| 775 |
+
assert_equal(c.mask, [[0, 0, 1], [0, 0, 1], [1, 1, 1]])
|
| 776 |
+
c = dot(a, b, strict=False)
|
| 777 |
+
assert_equal(c, np.dot(a.filled(0), b.filled(0)))
|
| 778 |
+
assert_equal(c, dot(a, b))
|
| 779 |
+
c = dot(b, a, strict=False)
|
| 780 |
+
assert_equal(c, np.dot(b.filled(0), a.filled(0)))
|
| 781 |
+
#
|
| 782 |
+
m = [0, 0, 0, 0, 0, 0]
|
| 783 |
+
a = masked_array(n, mask=m).reshape(2, 3)
|
| 784 |
+
b = masked_array(n, mask=m).reshape(3, 2)
|
| 785 |
+
c = dot(a, b)
|
| 786 |
+
assert_equal(c.mask, nomask)
|
| 787 |
+
c = dot(b, a)
|
| 788 |
+
assert_equal(c.mask, nomask)
|
| 789 |
+
#
|
| 790 |
+
a = masked_array(n, mask=[1, 0, 0, 0, 0, 0]).reshape(2, 3)
|
| 791 |
+
b = masked_array(n, mask=[0, 0, 0, 0, 0, 0]).reshape(3, 2)
|
| 792 |
+
c = dot(a, b, strict=True)
|
| 793 |
+
assert_equal(c.mask, [[1, 1], [0, 0]])
|
| 794 |
+
c = dot(a, b, strict=False)
|
| 795 |
+
assert_equal(c, np.dot(a.filled(0), b.filled(0)))
|
| 796 |
+
c = dot(b, a, strict=True)
|
| 797 |
+
assert_equal(c.mask, [[1, 0, 0], [1, 0, 0], [1, 0, 0]])
|
| 798 |
+
c = dot(b, a, strict=False)
|
| 799 |
+
assert_equal(c, np.dot(b.filled(0), a.filled(0)))
|
| 800 |
+
#
|
| 801 |
+
a = masked_array(n, mask=[0, 0, 0, 0, 0, 1]).reshape(2, 3)
|
| 802 |
+
b = masked_array(n, mask=[0, 0, 0, 0, 0, 0]).reshape(3, 2)
|
| 803 |
+
c = dot(a, b, strict=True)
|
| 804 |
+
assert_equal(c.mask, [[0, 0], [1, 1]])
|
| 805 |
+
c = dot(a, b)
|
| 806 |
+
assert_equal(c, np.dot(a.filled(0), b.filled(0)))
|
| 807 |
+
c = dot(b, a, strict=True)
|
| 808 |
+
assert_equal(c.mask, [[0, 0, 1], [0, 0, 1], [0, 0, 1]])
|
| 809 |
+
c = dot(b, a, strict=False)
|
| 810 |
+
assert_equal(c, np.dot(b.filled(0), a.filled(0)))
|
| 811 |
+
#
|
| 812 |
+
a = masked_array(n, mask=[0, 0, 0, 0, 0, 1]).reshape(2, 3)
|
| 813 |
+
b = masked_array(n, mask=[0, 0, 1, 0, 0, 0]).reshape(3, 2)
|
| 814 |
+
c = dot(a, b, strict=True)
|
| 815 |
+
assert_equal(c.mask, [[1, 0], [1, 1]])
|
| 816 |
+
c = dot(a, b, strict=False)
|
| 817 |
+
assert_equal(c, np.dot(a.filled(0), b.filled(0)))
|
| 818 |
+
c = dot(b, a, strict=True)
|
| 819 |
+
assert_equal(c.mask, [[0, 0, 1], [1, 1, 1], [0, 0, 1]])
|
| 820 |
+
c = dot(b, a, strict=False)
|
| 821 |
+
assert_equal(c, np.dot(b.filled(0), a.filled(0)))
|
| 822 |
+
#
|
| 823 |
+
a = masked_array(np.arange(8).reshape(2, 2, 2),
|
| 824 |
+
mask=[[[1, 0], [0, 0]], [[0, 0], [0, 0]]])
|
| 825 |
+
b = masked_array(np.arange(8).reshape(2, 2, 2),
|
| 826 |
+
mask=[[[0, 0], [0, 0]], [[0, 0], [0, 1]]])
|
| 827 |
+
c = dot(a, b, strict=True)
|
| 828 |
+
assert_equal(c.mask,
|
| 829 |
+
[[[[1, 1], [1, 1]], [[0, 0], [0, 1]]],
|
| 830 |
+
[[[0, 0], [0, 1]], [[0, 0], [0, 1]]]])
|
| 831 |
+
c = dot(a, b, strict=False)
|
| 832 |
+
assert_equal(c.mask,
|
| 833 |
+
[[[[0, 0], [0, 1]], [[0, 0], [0, 0]]],
|
| 834 |
+
[[[0, 0], [0, 0]], [[0, 0], [0, 0]]]])
|
| 835 |
+
c = dot(b, a, strict=True)
|
| 836 |
+
assert_equal(c.mask,
|
| 837 |
+
[[[[1, 0], [0, 0]], [[1, 0], [0, 0]]],
|
| 838 |
+
[[[1, 0], [0, 0]], [[1, 1], [1, 1]]]])
|
| 839 |
+
c = dot(b, a, strict=False)
|
| 840 |
+
assert_equal(c.mask,
|
| 841 |
+
[[[[0, 0], [0, 0]], [[0, 0], [0, 0]]],
|
| 842 |
+
[[[0, 0], [0, 0]], [[1, 0], [0, 0]]]])
|
| 843 |
+
#
|
| 844 |
+
a = masked_array(np.arange(8).reshape(2, 2, 2),
|
| 845 |
+
mask=[[[1, 0], [0, 0]], [[0, 0], [0, 0]]])
|
| 846 |
+
b = 5.
|
| 847 |
+
c = dot(a, b, strict=True)
|
| 848 |
+
assert_equal(c.mask, [[[1, 0], [0, 0]], [[0, 0], [0, 0]]])
|
| 849 |
+
c = dot(a, b, strict=False)
|
| 850 |
+
assert_equal(c.mask, [[[1, 0], [0, 0]], [[0, 0], [0, 0]]])
|
| 851 |
+
c = dot(b, a, strict=True)
|
| 852 |
+
assert_equal(c.mask, [[[1, 0], [0, 0]], [[0, 0], [0, 0]]])
|
| 853 |
+
c = dot(b, a, strict=False)
|
| 854 |
+
assert_equal(c.mask, [[[1, 0], [0, 0]], [[0, 0], [0, 0]]])
|
| 855 |
+
#
|
| 856 |
+
a = masked_array(np.arange(8).reshape(2, 2, 2),
|
| 857 |
+
mask=[[[1, 0], [0, 0]], [[0, 0], [0, 0]]])
|
| 858 |
+
b = masked_array(np.arange(2), mask=[0, 1])
|
| 859 |
+
c = dot(a, b, strict=True)
|
| 860 |
+
assert_equal(c.mask, [[1, 1], [1, 1]])
|
| 861 |
+
c = dot(a, b, strict=False)
|
| 862 |
+
assert_equal(c.mask, [[1, 0], [0, 0]])
|
| 863 |
+
|
| 864 |
+
def test_dot_returns_maskedarray(self):
|
| 865 |
+
# See gh-6611
|
| 866 |
+
a = np.eye(3)
|
| 867 |
+
b = array(a)
|
| 868 |
+
assert_(type(dot(a, a)) is MaskedArray)
|
| 869 |
+
assert_(type(dot(a, b)) is MaskedArray)
|
| 870 |
+
assert_(type(dot(b, a)) is MaskedArray)
|
| 871 |
+
assert_(type(dot(b, b)) is MaskedArray)
|
| 872 |
+
|
| 873 |
+
def test_dot_out(self):
|
| 874 |
+
a = array(np.eye(3))
|
| 875 |
+
out = array(np.zeros((3, 3)))
|
| 876 |
+
res = dot(a, a, out=out)
|
| 877 |
+
assert_(res is out)
|
| 878 |
+
assert_equal(a, res)
|
| 879 |
+
|
| 880 |
+
|
| 881 |
+
class TestApplyAlongAxis:
|
| 882 |
+
# Tests 2D functions
|
| 883 |
+
def test_3d(self):
|
| 884 |
+
a = arange(12.).reshape(2, 2, 3)
|
| 885 |
+
|
| 886 |
+
def myfunc(b):
|
| 887 |
+
return b[1]
|
| 888 |
+
|
| 889 |
+
xa = apply_along_axis(myfunc, 2, a)
|
| 890 |
+
assert_equal(xa, [[1, 4], [7, 10]])
|
| 891 |
+
|
| 892 |
+
# Tests kwargs functions
|
| 893 |
+
def test_3d_kwargs(self):
|
| 894 |
+
a = arange(12).reshape(2, 2, 3)
|
| 895 |
+
|
| 896 |
+
def myfunc(b, offset=0):
|
| 897 |
+
return b[1 + offset]
|
| 898 |
+
|
| 899 |
+
xa = apply_along_axis(myfunc, 2, a, offset=1)
|
| 900 |
+
assert_equal(xa, [[2, 5], [8, 11]])
|
| 901 |
+
|
| 902 |
+
|
| 903 |
+
class TestApplyOverAxes:
|
| 904 |
+
# Tests apply_over_axes
|
| 905 |
+
def test_basic(self):
|
| 906 |
+
a = arange(24).reshape(2, 3, 4)
|
| 907 |
+
test = apply_over_axes(np.sum, a, [0, 2])
|
| 908 |
+
ctrl = np.array([[[60], [92], [124]]])
|
| 909 |
+
assert_equal(test, ctrl)
|
| 910 |
+
a[(a % 2).astype(bool)] = masked
|
| 911 |
+
test = apply_over_axes(np.sum, a, [0, 2])
|
| 912 |
+
ctrl = np.array([[[28], [44], [60]]])
|
| 913 |
+
assert_equal(test, ctrl)
|
| 914 |
+
|
| 915 |
+
|
| 916 |
+
class TestMedian:
|
| 917 |
+
def test_pytype(self):
|
| 918 |
+
r = np.ma.median([[np.inf, np.inf], [np.inf, np.inf]], axis=-1)
|
| 919 |
+
assert_equal(r, np.inf)
|
| 920 |
+
|
| 921 |
+
def test_inf(self):
|
| 922 |
+
# test that even which computes handles inf / x = masked
|
| 923 |
+
r = np.ma.median(np.ma.masked_array([[np.inf, np.inf],
|
| 924 |
+
[np.inf, np.inf]]), axis=-1)
|
| 925 |
+
assert_equal(r, np.inf)
|
| 926 |
+
r = np.ma.median(np.ma.masked_array([[np.inf, np.inf],
|
| 927 |
+
[np.inf, np.inf]]), axis=None)
|
| 928 |
+
assert_equal(r, np.inf)
|
| 929 |
+
# all masked
|
| 930 |
+
r = np.ma.median(np.ma.masked_array([[np.inf, np.inf],
|
| 931 |
+
[np.inf, np.inf]], mask=True),
|
| 932 |
+
axis=-1)
|
| 933 |
+
assert_equal(r.mask, True)
|
| 934 |
+
r = np.ma.median(np.ma.masked_array([[np.inf, np.inf],
|
| 935 |
+
[np.inf, np.inf]], mask=True),
|
| 936 |
+
axis=None)
|
| 937 |
+
assert_equal(r.mask, True)
|
| 938 |
+
|
| 939 |
+
def test_non_masked(self):
|
| 940 |
+
x = np.arange(9)
|
| 941 |
+
assert_equal(np.ma.median(x), 4.)
|
| 942 |
+
assert_(type(np.ma.median(x)) is not MaskedArray)
|
| 943 |
+
x = range(8)
|
| 944 |
+
assert_equal(np.ma.median(x), 3.5)
|
| 945 |
+
assert_(type(np.ma.median(x)) is not MaskedArray)
|
| 946 |
+
x = 5
|
| 947 |
+
assert_equal(np.ma.median(x), 5.)
|
| 948 |
+
assert_(type(np.ma.median(x)) is not MaskedArray)
|
| 949 |
+
# integer
|
| 950 |
+
x = np.arange(9 * 8).reshape(9, 8)
|
| 951 |
+
assert_equal(np.ma.median(x, axis=0), np.median(x, axis=0))
|
| 952 |
+
assert_equal(np.ma.median(x, axis=1), np.median(x, axis=1))
|
| 953 |
+
assert_(np.ma.median(x, axis=1) is not MaskedArray)
|
| 954 |
+
# float
|
| 955 |
+
x = np.arange(9 * 8.).reshape(9, 8)
|
| 956 |
+
assert_equal(np.ma.median(x, axis=0), np.median(x, axis=0))
|
| 957 |
+
assert_equal(np.ma.median(x, axis=1), np.median(x, axis=1))
|
| 958 |
+
assert_(np.ma.median(x, axis=1) is not MaskedArray)
|
| 959 |
+
|
| 960 |
+
def test_docstring_examples(self):
|
| 961 |
+
"test the examples given in the docstring of ma.median"
|
| 962 |
+
x = array(np.arange(8), mask=[0] * 4 + [1] * 4)
|
| 963 |
+
assert_equal(np.ma.median(x), 1.5)
|
| 964 |
+
assert_equal(np.ma.median(x).shape, (), "shape mismatch")
|
| 965 |
+
assert_(type(np.ma.median(x)) is not MaskedArray)
|
| 966 |
+
x = array(np.arange(10).reshape(2, 5), mask=[0] * 6 + [1] * 4)
|
| 967 |
+
assert_equal(np.ma.median(x), 2.5)
|
| 968 |
+
assert_equal(np.ma.median(x).shape, (), "shape mismatch")
|
| 969 |
+
assert_(type(np.ma.median(x)) is not MaskedArray)
|
| 970 |
+
ma_x = np.ma.median(x, axis=-1, overwrite_input=True)
|
| 971 |
+
assert_equal(ma_x, [2., 5.])
|
| 972 |
+
assert_equal(ma_x.shape, (2,), "shape mismatch")
|
| 973 |
+
assert_(type(ma_x) is MaskedArray)
|
| 974 |
+
|
| 975 |
+
def test_axis_argument_errors(self):
|
| 976 |
+
msg = "mask = %s, ndim = %s, axis = %s, overwrite_input = %s"
|
| 977 |
+
for ndmin in range(5):
|
| 978 |
+
for mask in [False, True]:
|
| 979 |
+
x = array(1, ndmin=ndmin, mask=mask)
|
| 980 |
+
|
| 981 |
+
# Valid axis values should not raise exception
|
| 982 |
+
args = itertools.product(range(-ndmin, ndmin), [False, True])
|
| 983 |
+
for axis, over in args:
|
| 984 |
+
try:
|
| 985 |
+
np.ma.median(x, axis=axis, overwrite_input=over)
|
| 986 |
+
except Exception:
|
| 987 |
+
raise AssertionError(msg % (mask, ndmin, axis, over))
|
| 988 |
+
|
| 989 |
+
# Invalid axis values should raise exception
|
| 990 |
+
args = itertools.product([-(ndmin + 1), ndmin], [False, True])
|
| 991 |
+
for axis, over in args:
|
| 992 |
+
try:
|
| 993 |
+
np.ma.median(x, axis=axis, overwrite_input=over)
|
| 994 |
+
except np.exceptions.AxisError:
|
| 995 |
+
pass
|
| 996 |
+
else:
|
| 997 |
+
raise AssertionError(msg % (mask, ndmin, axis, over))
|
| 998 |
+
|
| 999 |
+
def test_masked_0d(self):
|
| 1000 |
+
# Check values
|
| 1001 |
+
x = array(1, mask=False)
|
| 1002 |
+
assert_equal(np.ma.median(x), 1)
|
| 1003 |
+
x = array(1, mask=True)
|
| 1004 |
+
assert_equal(np.ma.median(x), np.ma.masked)
|
| 1005 |
+
|
| 1006 |
+
def test_masked_1d(self):
|
| 1007 |
+
x = array(np.arange(5), mask=True)
|
| 1008 |
+
assert_equal(np.ma.median(x), np.ma.masked)
|
| 1009 |
+
assert_equal(np.ma.median(x).shape, (), "shape mismatch")
|
| 1010 |
+
assert_(type(np.ma.median(x)) is np.ma.core.MaskedConstant)
|
| 1011 |
+
x = array(np.arange(5), mask=False)
|
| 1012 |
+
assert_equal(np.ma.median(x), 2.)
|
| 1013 |
+
assert_equal(np.ma.median(x).shape, (), "shape mismatch")
|
| 1014 |
+
assert_(type(np.ma.median(x)) is not MaskedArray)
|
| 1015 |
+
x = array(np.arange(5), mask=[0, 1, 0, 0, 0])
|
| 1016 |
+
assert_equal(np.ma.median(x), 2.5)
|
| 1017 |
+
assert_equal(np.ma.median(x).shape, (), "shape mismatch")
|
| 1018 |
+
assert_(type(np.ma.median(x)) is not MaskedArray)
|
| 1019 |
+
x = array(np.arange(5), mask=[0, 1, 1, 1, 1])
|
| 1020 |
+
assert_equal(np.ma.median(x), 0.)
|
| 1021 |
+
assert_equal(np.ma.median(x).shape, (), "shape mismatch")
|
| 1022 |
+
assert_(type(np.ma.median(x)) is not MaskedArray)
|
| 1023 |
+
# integer
|
| 1024 |
+
x = array(np.arange(5), mask=[0, 1, 1, 0, 0])
|
| 1025 |
+
assert_equal(np.ma.median(x), 3.)
|
| 1026 |
+
assert_equal(np.ma.median(x).shape, (), "shape mismatch")
|
| 1027 |
+
assert_(type(np.ma.median(x)) is not MaskedArray)
|
| 1028 |
+
# float
|
| 1029 |
+
x = array(np.arange(5.), mask=[0, 1, 1, 0, 0])
|
| 1030 |
+
assert_equal(np.ma.median(x), 3.)
|
| 1031 |
+
assert_equal(np.ma.median(x).shape, (), "shape mismatch")
|
| 1032 |
+
assert_(type(np.ma.median(x)) is not MaskedArray)
|
| 1033 |
+
# integer
|
| 1034 |
+
x = array(np.arange(6), mask=[0, 1, 1, 1, 1, 0])
|
| 1035 |
+
assert_equal(np.ma.median(x), 2.5)
|
| 1036 |
+
assert_equal(np.ma.median(x).shape, (), "shape mismatch")
|
| 1037 |
+
assert_(type(np.ma.median(x)) is not MaskedArray)
|
| 1038 |
+
# float
|
| 1039 |
+
x = array(np.arange(6.), mask=[0, 1, 1, 1, 1, 0])
|
| 1040 |
+
assert_equal(np.ma.median(x), 2.5)
|
| 1041 |
+
assert_equal(np.ma.median(x).shape, (), "shape mismatch")
|
| 1042 |
+
assert_(type(np.ma.median(x)) is not MaskedArray)
|
| 1043 |
+
|
| 1044 |
+
def test_1d_shape_consistency(self):
|
| 1045 |
+
assert_equal(np.ma.median(array([1, 2, 3], mask=[0, 0, 0])).shape,
|
| 1046 |
+
np.ma.median(array([1, 2, 3], mask=[0, 1, 0])).shape)
|
| 1047 |
+
|
| 1048 |
+
def test_2d(self):
|
| 1049 |
+
# Tests median w/ 2D
|
| 1050 |
+
(n, p) = (101, 30)
|
| 1051 |
+
x = masked_array(np.linspace(-1., 1., n),)
|
| 1052 |
+
x[:10] = x[-10:] = masked
|
| 1053 |
+
z = masked_array(np.empty((n, p), dtype=float))
|
| 1054 |
+
z[:, 0] = x[:]
|
| 1055 |
+
idx = np.arange(len(x))
|
| 1056 |
+
for i in range(1, p):
|
| 1057 |
+
np.random.shuffle(idx)
|
| 1058 |
+
z[:, i] = x[idx]
|
| 1059 |
+
assert_equal(median(z[:, 0]), 0)
|
| 1060 |
+
assert_equal(median(z), 0)
|
| 1061 |
+
assert_equal(median(z, axis=0), np.zeros(p))
|
| 1062 |
+
assert_equal(median(z.T, axis=1), np.zeros(p))
|
| 1063 |
+
|
| 1064 |
+
def test_2d_waxis(self):
|
| 1065 |
+
# Tests median w/ 2D arrays and different axis.
|
| 1066 |
+
x = masked_array(np.arange(30).reshape(10, 3))
|
| 1067 |
+
x[:3] = x[-3:] = masked
|
| 1068 |
+
assert_equal(median(x), 14.5)
|
| 1069 |
+
assert_(type(np.ma.median(x)) is not MaskedArray)
|
| 1070 |
+
assert_equal(median(x, axis=0), [13.5, 14.5, 15.5])
|
| 1071 |
+
assert_(type(np.ma.median(x, axis=0)) is MaskedArray)
|
| 1072 |
+
assert_equal(median(x, axis=1), [0, 0, 0, 10, 13, 16, 19, 0, 0, 0])
|
| 1073 |
+
assert_(type(np.ma.median(x, axis=1)) is MaskedArray)
|
| 1074 |
+
assert_equal(median(x, axis=1).mask, [1, 1, 1, 0, 0, 0, 0, 1, 1, 1])
|
| 1075 |
+
|
| 1076 |
+
def test_3d(self):
|
| 1077 |
+
# Tests median w/ 3D
|
| 1078 |
+
x = np.ma.arange(24).reshape(3, 4, 2)
|
| 1079 |
+
x[x % 3 == 0] = masked
|
| 1080 |
+
assert_equal(median(x, 0), [[12, 9], [6, 15], [12, 9], [18, 15]])
|
| 1081 |
+
x.shape = (4, 3, 2)
|
| 1082 |
+
assert_equal(median(x, 0), [[99, 10], [11, 99], [13, 14]])
|
| 1083 |
+
x = np.ma.arange(24).reshape(4, 3, 2)
|
| 1084 |
+
x[x % 5 == 0] = masked
|
| 1085 |
+
assert_equal(median(x, 0), [[12, 10], [8, 9], [16, 17]])
|
| 1086 |
+
|
| 1087 |
+
def test_neg_axis(self):
|
| 1088 |
+
x = masked_array(np.arange(30).reshape(10, 3))
|
| 1089 |
+
x[:3] = x[-3:] = masked
|
| 1090 |
+
assert_equal(median(x, axis=-1), median(x, axis=1))
|
| 1091 |
+
|
| 1092 |
+
def test_out_1d(self):
|
| 1093 |
+
# integer float even odd
|
| 1094 |
+
for v in (30, 30., 31, 31.):
|
| 1095 |
+
x = masked_array(np.arange(v))
|
| 1096 |
+
x[:3] = x[-3:] = masked
|
| 1097 |
+
out = masked_array(np.ones(()))
|
| 1098 |
+
r = median(x, out=out)
|
| 1099 |
+
if v == 30:
|
| 1100 |
+
assert_equal(out, 14.5)
|
| 1101 |
+
else:
|
| 1102 |
+
assert_equal(out, 15.)
|
| 1103 |
+
assert_(r is out)
|
| 1104 |
+
assert_(type(r) is MaskedArray)
|
| 1105 |
+
|
| 1106 |
+
def test_out(self):
|
| 1107 |
+
# integer float even odd
|
| 1108 |
+
for v in (40, 40., 30, 30.):
|
| 1109 |
+
x = masked_array(np.arange(v).reshape(10, -1))
|
| 1110 |
+
x[:3] = x[-3:] = masked
|
| 1111 |
+
out = masked_array(np.ones(10))
|
| 1112 |
+
r = median(x, axis=1, out=out)
|
| 1113 |
+
if v == 30:
|
| 1114 |
+
e = masked_array([0.] * 3 + [10, 13, 16, 19] + [0.] * 3,
|
| 1115 |
+
mask=[True] * 3 + [False] * 4 + [True] * 3)
|
| 1116 |
+
else:
|
| 1117 |
+
e = masked_array([0.] * 3 + [13.5, 17.5, 21.5, 25.5] + [0.] * 3,
|
| 1118 |
+
mask=[True] * 3 + [False] * 4 + [True] * 3)
|
| 1119 |
+
assert_equal(r, e)
|
| 1120 |
+
assert_(r is out)
|
| 1121 |
+
assert_(type(r) is MaskedArray)
|
| 1122 |
+
|
| 1123 |
+
@pytest.mark.parametrize(
|
| 1124 |
+
argnames='axis',
|
| 1125 |
+
argvalues=[
|
| 1126 |
+
None,
|
| 1127 |
+
1,
|
| 1128 |
+
(1, ),
|
| 1129 |
+
(0, 1),
|
| 1130 |
+
(-3, -1),
|
| 1131 |
+
]
|
| 1132 |
+
)
|
| 1133 |
+
def test_keepdims_out(self, axis):
|
| 1134 |
+
mask = np.zeros((3, 5, 7, 11), dtype=bool)
|
| 1135 |
+
# Randomly set some elements to True:
|
| 1136 |
+
w = np.random.random((4, 200)) * np.array(mask.shape)[:, None]
|
| 1137 |
+
w = w.astype(np.intp)
|
| 1138 |
+
mask[tuple(w)] = np.nan
|
| 1139 |
+
d = masked_array(np.ones(mask.shape), mask=mask)
|
| 1140 |
+
if axis is None:
|
| 1141 |
+
shape_out = (1,) * d.ndim
|
| 1142 |
+
else:
|
| 1143 |
+
axis_norm = normalize_axis_tuple(axis, d.ndim)
|
| 1144 |
+
shape_out = tuple(
|
| 1145 |
+
1 if i in axis_norm else d.shape[i] for i in range(d.ndim))
|
| 1146 |
+
out = masked_array(np.empty(shape_out))
|
| 1147 |
+
result = median(d, axis=axis, keepdims=True, out=out)
|
| 1148 |
+
assert result is out
|
| 1149 |
+
assert_equal(result.shape, shape_out)
|
| 1150 |
+
|
| 1151 |
+
def test_single_non_masked_value_on_axis(self):
|
| 1152 |
+
data = [[1., 0.],
|
| 1153 |
+
[0., 3.],
|
| 1154 |
+
[0., 0.]]
|
| 1155 |
+
masked_arr = np.ma.masked_equal(data, 0)
|
| 1156 |
+
expected = [1., 3.]
|
| 1157 |
+
assert_array_equal(np.ma.median(masked_arr, axis=0),
|
| 1158 |
+
expected)
|
| 1159 |
+
|
| 1160 |
+
def test_nan(self):
|
| 1161 |
+
for mask in (False, np.zeros(6, dtype=bool)):
|
| 1162 |
+
dm = np.ma.array([[1, np.nan, 3], [1, 2, 3]])
|
| 1163 |
+
dm.mask = mask
|
| 1164 |
+
|
| 1165 |
+
# scalar result
|
| 1166 |
+
r = np.ma.median(dm, axis=None)
|
| 1167 |
+
assert_(np.isscalar(r))
|
| 1168 |
+
assert_array_equal(r, np.nan)
|
| 1169 |
+
r = np.ma.median(dm.ravel(), axis=0)
|
| 1170 |
+
assert_(np.isscalar(r))
|
| 1171 |
+
assert_array_equal(r, np.nan)
|
| 1172 |
+
|
| 1173 |
+
r = np.ma.median(dm, axis=0)
|
| 1174 |
+
assert_equal(type(r), MaskedArray)
|
| 1175 |
+
assert_array_equal(r, [1, np.nan, 3])
|
| 1176 |
+
r = np.ma.median(dm, axis=1)
|
| 1177 |
+
assert_equal(type(r), MaskedArray)
|
| 1178 |
+
assert_array_equal(r, [np.nan, 2])
|
| 1179 |
+
r = np.ma.median(dm, axis=-1)
|
| 1180 |
+
assert_equal(type(r), MaskedArray)
|
| 1181 |
+
assert_array_equal(r, [np.nan, 2])
|
| 1182 |
+
|
| 1183 |
+
dm = np.ma.array([[1, np.nan, 3], [1, 2, 3]])
|
| 1184 |
+
dm[:, 2] = np.ma.masked
|
| 1185 |
+
assert_array_equal(np.ma.median(dm, axis=None), np.nan)
|
| 1186 |
+
assert_array_equal(np.ma.median(dm, axis=0), [1, np.nan, 3])
|
| 1187 |
+
assert_array_equal(np.ma.median(dm, axis=1), [np.nan, 1.5])
|
| 1188 |
+
|
| 1189 |
+
def test_out_nan(self):
|
| 1190 |
+
o = np.ma.masked_array(np.zeros((4,)))
|
| 1191 |
+
d = np.ma.masked_array(np.ones((3, 4)))
|
| 1192 |
+
d[2, 1] = np.nan
|
| 1193 |
+
d[2, 2] = np.ma.masked
|
| 1194 |
+
assert_equal(np.ma.median(d, 0, out=o), o)
|
| 1195 |
+
o = np.ma.masked_array(np.zeros((3,)))
|
| 1196 |
+
assert_equal(np.ma.median(d, 1, out=o), o)
|
| 1197 |
+
o = np.ma.masked_array(np.zeros(()))
|
| 1198 |
+
assert_equal(np.ma.median(d, out=o), o)
|
| 1199 |
+
|
| 1200 |
+
def test_nan_behavior(self):
|
| 1201 |
+
a = np.ma.masked_array(np.arange(24, dtype=float))
|
| 1202 |
+
a[::3] = np.ma.masked
|
| 1203 |
+
a[2] = np.nan
|
| 1204 |
+
assert_array_equal(np.ma.median(a), np.nan)
|
| 1205 |
+
assert_array_equal(np.ma.median(a, axis=0), np.nan)
|
| 1206 |
+
|
| 1207 |
+
a = np.ma.masked_array(np.arange(24, dtype=float).reshape(2, 3, 4))
|
| 1208 |
+
a.mask = np.arange(a.size) % 2 == 1
|
| 1209 |
+
aorig = a.copy()
|
| 1210 |
+
a[1, 2, 3] = np.nan
|
| 1211 |
+
a[1, 1, 2] = np.nan
|
| 1212 |
+
|
| 1213 |
+
# no axis
|
| 1214 |
+
assert_array_equal(np.ma.median(a), np.nan)
|
| 1215 |
+
assert_(np.isscalar(np.ma.median(a)))
|
| 1216 |
+
|
| 1217 |
+
# axis0
|
| 1218 |
+
b = np.ma.median(aorig, axis=0)
|
| 1219 |
+
b[2, 3] = np.nan
|
| 1220 |
+
b[1, 2] = np.nan
|
| 1221 |
+
assert_equal(np.ma.median(a, 0), b)
|
| 1222 |
+
|
| 1223 |
+
# axis1
|
| 1224 |
+
b = np.ma.median(aorig, axis=1)
|
| 1225 |
+
b[1, 3] = np.nan
|
| 1226 |
+
b[1, 2] = np.nan
|
| 1227 |
+
assert_equal(np.ma.median(a, 1), b)
|
| 1228 |
+
|
| 1229 |
+
# axis02
|
| 1230 |
+
b = np.ma.median(aorig, axis=(0, 2))
|
| 1231 |
+
b[1] = np.nan
|
| 1232 |
+
b[2] = np.nan
|
| 1233 |
+
assert_equal(np.ma.median(a, (0, 2)), b)
|
| 1234 |
+
|
| 1235 |
+
def test_ambigous_fill(self):
|
| 1236 |
+
# 255 is max value, used as filler for sort
|
| 1237 |
+
a = np.array([[3, 3, 255], [3, 3, 255]], dtype=np.uint8)
|
| 1238 |
+
a = np.ma.masked_array(a, mask=a == 3)
|
| 1239 |
+
assert_array_equal(np.ma.median(a, axis=1), 255)
|
| 1240 |
+
assert_array_equal(np.ma.median(a, axis=1).mask, False)
|
| 1241 |
+
assert_array_equal(np.ma.median(a, axis=0), a[0])
|
| 1242 |
+
assert_array_equal(np.ma.median(a), 255)
|
| 1243 |
+
|
| 1244 |
+
def test_special(self):
|
| 1245 |
+
for inf in [np.inf, -np.inf]:
|
| 1246 |
+
a = np.array([[inf, np.nan], [np.nan, np.nan]])
|
| 1247 |
+
a = np.ma.masked_array(a, mask=np.isnan(a))
|
| 1248 |
+
assert_equal(np.ma.median(a, axis=0), [inf, np.nan])
|
| 1249 |
+
assert_equal(np.ma.median(a, axis=1), [inf, np.nan])
|
| 1250 |
+
assert_equal(np.ma.median(a), inf)
|
| 1251 |
+
|
| 1252 |
+
a = np.array([[np.nan, np.nan, inf], [np.nan, np.nan, inf]])
|
| 1253 |
+
a = np.ma.masked_array(a, mask=np.isnan(a))
|
| 1254 |
+
assert_array_equal(np.ma.median(a, axis=1), inf)
|
| 1255 |
+
assert_array_equal(np.ma.median(a, axis=1).mask, False)
|
| 1256 |
+
assert_array_equal(np.ma.median(a, axis=0), a[0])
|
| 1257 |
+
assert_array_equal(np.ma.median(a), inf)
|
| 1258 |
+
|
| 1259 |
+
# no mask
|
| 1260 |
+
a = np.array([[inf, inf], [inf, inf]])
|
| 1261 |
+
assert_equal(np.ma.median(a), inf)
|
| 1262 |
+
assert_equal(np.ma.median(a, axis=0), inf)
|
| 1263 |
+
assert_equal(np.ma.median(a, axis=1), inf)
|
| 1264 |
+
|
| 1265 |
+
a = np.array([[inf, 7, -inf, -9],
|
| 1266 |
+
[-10, np.nan, np.nan, 5],
|
| 1267 |
+
[4, np.nan, np.nan, inf]],
|
| 1268 |
+
dtype=np.float32)
|
| 1269 |
+
a = np.ma.masked_array(a, mask=np.isnan(a))
|
| 1270 |
+
if inf > 0:
|
| 1271 |
+
assert_equal(np.ma.median(a, axis=0), [4., 7., -inf, 5.])
|
| 1272 |
+
assert_equal(np.ma.median(a), 4.5)
|
| 1273 |
+
else:
|
| 1274 |
+
assert_equal(np.ma.median(a, axis=0), [-10., 7., -inf, -9.])
|
| 1275 |
+
assert_equal(np.ma.median(a), -2.5)
|
| 1276 |
+
assert_equal(np.ma.median(a, axis=1), [-1., -2.5, inf])
|
| 1277 |
+
|
| 1278 |
+
for i in range(10):
|
| 1279 |
+
for j in range(1, 10):
|
| 1280 |
+
a = np.array([([np.nan] * i) + ([inf] * j)] * 2)
|
| 1281 |
+
a = np.ma.masked_array(a, mask=np.isnan(a))
|
| 1282 |
+
assert_equal(np.ma.median(a), inf)
|
| 1283 |
+
assert_equal(np.ma.median(a, axis=1), inf)
|
| 1284 |
+
assert_equal(np.ma.median(a, axis=0),
|
| 1285 |
+
([np.nan] * i) + [inf] * j)
|
| 1286 |
+
|
| 1287 |
+
def test_empty(self):
|
| 1288 |
+
# empty arrays
|
| 1289 |
+
a = np.ma.masked_array(np.array([], dtype=float))
|
| 1290 |
+
with suppress_warnings() as w:
|
| 1291 |
+
w.record(RuntimeWarning)
|
| 1292 |
+
assert_array_equal(np.ma.median(a), np.nan)
|
| 1293 |
+
assert_(w.log[0].category is RuntimeWarning)
|
| 1294 |
+
|
| 1295 |
+
# multiple dimensions
|
| 1296 |
+
a = np.ma.masked_array(np.array([], dtype=float, ndmin=3))
|
| 1297 |
+
# no axis
|
| 1298 |
+
with suppress_warnings() as w:
|
| 1299 |
+
w.record(RuntimeWarning)
|
| 1300 |
+
warnings.filterwarnings('always', '', RuntimeWarning)
|
| 1301 |
+
assert_array_equal(np.ma.median(a), np.nan)
|
| 1302 |
+
assert_(w.log[0].category is RuntimeWarning)
|
| 1303 |
+
|
| 1304 |
+
# axis 0 and 1
|
| 1305 |
+
b = np.ma.masked_array(np.array([], dtype=float, ndmin=2))
|
| 1306 |
+
assert_equal(np.ma.median(a, axis=0), b)
|
| 1307 |
+
assert_equal(np.ma.median(a, axis=1), b)
|
| 1308 |
+
|
| 1309 |
+
# axis 2
|
| 1310 |
+
b = np.ma.masked_array(np.array(np.nan, dtype=float, ndmin=2))
|
| 1311 |
+
with warnings.catch_warnings(record=True) as w:
|
| 1312 |
+
warnings.filterwarnings('always', '', RuntimeWarning)
|
| 1313 |
+
assert_equal(np.ma.median(a, axis=2), b)
|
| 1314 |
+
assert_(w[0].category is RuntimeWarning)
|
| 1315 |
+
|
| 1316 |
+
def test_object(self):
|
| 1317 |
+
o = np.ma.masked_array(np.arange(7.))
|
| 1318 |
+
assert_(type(np.ma.median(o.astype(object))), float)
|
| 1319 |
+
o[2] = np.nan
|
| 1320 |
+
assert_(type(np.ma.median(o.astype(object))), float)
|
| 1321 |
+
|
| 1322 |
+
|
| 1323 |
+
class TestCov:
|
| 1324 |
+
|
| 1325 |
+
def setup_method(self):
|
| 1326 |
+
self.data = array(np.random.rand(12))
|
| 1327 |
+
|
| 1328 |
+
def test_covhelper(self):
|
| 1329 |
+
x = self.data
|
| 1330 |
+
# Test not mask output type is a float.
|
| 1331 |
+
assert_(_covhelper(x, rowvar=True)[1].dtype, np.float32)
|
| 1332 |
+
assert_(_covhelper(x, y=x, rowvar=False)[1].dtype, np.float32)
|
| 1333 |
+
# Test not mask output is equal after casting to float.
|
| 1334 |
+
mask = x > 0.5
|
| 1335 |
+
assert_array_equal(
|
| 1336 |
+
_covhelper(
|
| 1337 |
+
np.ma.masked_array(x, mask), rowvar=True
|
| 1338 |
+
)[1].astype(bool),
|
| 1339 |
+
~mask.reshape(1, -1),
|
| 1340 |
+
)
|
| 1341 |
+
assert_array_equal(
|
| 1342 |
+
_covhelper(
|
| 1343 |
+
np.ma.masked_array(x, mask), y=x, rowvar=False
|
| 1344 |
+
)[1].astype(bool),
|
| 1345 |
+
np.vstack((~mask, ~mask)),
|
| 1346 |
+
)
|
| 1347 |
+
|
| 1348 |
+
def test_1d_without_missing(self):
|
| 1349 |
+
# Test cov on 1D variable w/o missing values
|
| 1350 |
+
x = self.data
|
| 1351 |
+
assert_almost_equal(np.cov(x), cov(x))
|
| 1352 |
+
assert_almost_equal(np.cov(x, rowvar=False), cov(x, rowvar=False))
|
| 1353 |
+
assert_almost_equal(np.cov(x, rowvar=False, bias=True),
|
| 1354 |
+
cov(x, rowvar=False, bias=True))
|
| 1355 |
+
|
| 1356 |
+
def test_2d_without_missing(self):
|
| 1357 |
+
# Test cov on 1 2D variable w/o missing values
|
| 1358 |
+
x = self.data.reshape(3, 4)
|
| 1359 |
+
assert_almost_equal(np.cov(x), cov(x))
|
| 1360 |
+
assert_almost_equal(np.cov(x, rowvar=False), cov(x, rowvar=False))
|
| 1361 |
+
assert_almost_equal(np.cov(x, rowvar=False, bias=True),
|
| 1362 |
+
cov(x, rowvar=False, bias=True))
|
| 1363 |
+
|
| 1364 |
+
def test_1d_with_missing(self):
|
| 1365 |
+
# Test cov 1 1D variable w/missing values
|
| 1366 |
+
x = self.data
|
| 1367 |
+
x[-1] = masked
|
| 1368 |
+
x -= x.mean()
|
| 1369 |
+
nx = x.compressed()
|
| 1370 |
+
assert_almost_equal(np.cov(nx), cov(x))
|
| 1371 |
+
assert_almost_equal(np.cov(nx, rowvar=False), cov(x, rowvar=False))
|
| 1372 |
+
assert_almost_equal(np.cov(nx, rowvar=False, bias=True),
|
| 1373 |
+
cov(x, rowvar=False, bias=True))
|
| 1374 |
+
#
|
| 1375 |
+
try:
|
| 1376 |
+
cov(x, allow_masked=False)
|
| 1377 |
+
except ValueError:
|
| 1378 |
+
pass
|
| 1379 |
+
#
|
| 1380 |
+
# 2 1D variables w/ missing values
|
| 1381 |
+
nx = x[1:-1]
|
| 1382 |
+
assert_almost_equal(np.cov(nx, nx[::-1]), cov(x, x[::-1]))
|
| 1383 |
+
assert_almost_equal(np.cov(nx, nx[::-1], rowvar=False),
|
| 1384 |
+
cov(x, x[::-1], rowvar=False))
|
| 1385 |
+
assert_almost_equal(np.cov(nx, nx[::-1], rowvar=False, bias=True),
|
| 1386 |
+
cov(x, x[::-1], rowvar=False, bias=True))
|
| 1387 |
+
|
| 1388 |
+
def test_2d_with_missing(self):
|
| 1389 |
+
# Test cov on 2D variable w/ missing value
|
| 1390 |
+
x = self.data
|
| 1391 |
+
x[-1] = masked
|
| 1392 |
+
x = x.reshape(3, 4)
|
| 1393 |
+
valid = np.logical_not(getmaskarray(x)).astype(int)
|
| 1394 |
+
frac = np.dot(valid, valid.T)
|
| 1395 |
+
xf = (x - x.mean(1)[:, None]).filled(0)
|
| 1396 |
+
assert_almost_equal(cov(x),
|
| 1397 |
+
np.cov(xf) * (x.shape[1] - 1) / (frac - 1.))
|
| 1398 |
+
assert_almost_equal(cov(x, bias=True),
|
| 1399 |
+
np.cov(xf, bias=True) * x.shape[1] / frac)
|
| 1400 |
+
frac = np.dot(valid.T, valid)
|
| 1401 |
+
xf = (x - x.mean(0)).filled(0)
|
| 1402 |
+
assert_almost_equal(cov(x, rowvar=False),
|
| 1403 |
+
(np.cov(xf, rowvar=False) *
|
| 1404 |
+
(x.shape[0] - 1) / (frac - 1.)))
|
| 1405 |
+
assert_almost_equal(cov(x, rowvar=False, bias=True),
|
| 1406 |
+
(np.cov(xf, rowvar=False, bias=True) *
|
| 1407 |
+
x.shape[0] / frac))
|
| 1408 |
+
|
| 1409 |
+
|
| 1410 |
+
class TestCorrcoef:
|
| 1411 |
+
|
| 1412 |
+
def setup_method(self):
|
| 1413 |
+
self.data = array(np.random.rand(12))
|
| 1414 |
+
self.data2 = array(np.random.rand(12))
|
| 1415 |
+
|
| 1416 |
+
def test_ddof(self):
|
| 1417 |
+
# ddof raises DeprecationWarning
|
| 1418 |
+
x, y = self.data, self.data2
|
| 1419 |
+
expected = np.corrcoef(x)
|
| 1420 |
+
expected2 = np.corrcoef(x, y)
|
| 1421 |
+
with suppress_warnings() as sup:
|
| 1422 |
+
warnings.simplefilter("always")
|
| 1423 |
+
assert_warns(DeprecationWarning, corrcoef, x, ddof=-1)
|
| 1424 |
+
sup.filter(DeprecationWarning, "bias and ddof have no effect")
|
| 1425 |
+
# ddof has no or negligible effect on the function
|
| 1426 |
+
assert_almost_equal(np.corrcoef(x, ddof=0), corrcoef(x, ddof=0))
|
| 1427 |
+
assert_almost_equal(corrcoef(x, ddof=-1), expected)
|
| 1428 |
+
assert_almost_equal(corrcoef(x, y, ddof=-1), expected2)
|
| 1429 |
+
assert_almost_equal(corrcoef(x, ddof=3), expected)
|
| 1430 |
+
assert_almost_equal(corrcoef(x, y, ddof=3), expected2)
|
| 1431 |
+
|
| 1432 |
+
def test_bias(self):
|
| 1433 |
+
x, y = self.data, self.data2
|
| 1434 |
+
expected = np.corrcoef(x)
|
| 1435 |
+
# bias raises DeprecationWarning
|
| 1436 |
+
with suppress_warnings() as sup:
|
| 1437 |
+
warnings.simplefilter("always")
|
| 1438 |
+
assert_warns(DeprecationWarning, corrcoef, x, y, True, False)
|
| 1439 |
+
assert_warns(DeprecationWarning, corrcoef, x, y, True, True)
|
| 1440 |
+
assert_warns(DeprecationWarning, corrcoef, x, bias=False)
|
| 1441 |
+
sup.filter(DeprecationWarning, "bias and ddof have no effect")
|
| 1442 |
+
# bias has no or negligible effect on the function
|
| 1443 |
+
assert_almost_equal(corrcoef(x, bias=1), expected)
|
| 1444 |
+
|
| 1445 |
+
def test_1d_without_missing(self):
|
| 1446 |
+
# Test cov on 1D variable w/o missing values
|
| 1447 |
+
x = self.data
|
| 1448 |
+
assert_almost_equal(np.corrcoef(x), corrcoef(x))
|
| 1449 |
+
assert_almost_equal(np.corrcoef(x, rowvar=False),
|
| 1450 |
+
corrcoef(x, rowvar=False))
|
| 1451 |
+
with suppress_warnings() as sup:
|
| 1452 |
+
sup.filter(DeprecationWarning, "bias and ddof have no effect")
|
| 1453 |
+
assert_almost_equal(np.corrcoef(x, rowvar=False, bias=True),
|
| 1454 |
+
corrcoef(x, rowvar=False, bias=True))
|
| 1455 |
+
|
| 1456 |
+
def test_2d_without_missing(self):
|
| 1457 |
+
# Test corrcoef on 1 2D variable w/o missing values
|
| 1458 |
+
x = self.data.reshape(3, 4)
|
| 1459 |
+
assert_almost_equal(np.corrcoef(x), corrcoef(x))
|
| 1460 |
+
assert_almost_equal(np.corrcoef(x, rowvar=False),
|
| 1461 |
+
corrcoef(x, rowvar=False))
|
| 1462 |
+
with suppress_warnings() as sup:
|
| 1463 |
+
sup.filter(DeprecationWarning, "bias and ddof have no effect")
|
| 1464 |
+
assert_almost_equal(np.corrcoef(x, rowvar=False, bias=True),
|
| 1465 |
+
corrcoef(x, rowvar=False, bias=True))
|
| 1466 |
+
|
| 1467 |
+
def test_1d_with_missing(self):
|
| 1468 |
+
# Test corrcoef 1 1D variable w/missing values
|
| 1469 |
+
x = self.data
|
| 1470 |
+
x[-1] = masked
|
| 1471 |
+
x -= x.mean()
|
| 1472 |
+
nx = x.compressed()
|
| 1473 |
+
assert_almost_equal(np.corrcoef(nx), corrcoef(x))
|
| 1474 |
+
assert_almost_equal(np.corrcoef(nx, rowvar=False),
|
| 1475 |
+
corrcoef(x, rowvar=False))
|
| 1476 |
+
with suppress_warnings() as sup:
|
| 1477 |
+
sup.filter(DeprecationWarning, "bias and ddof have no effect")
|
| 1478 |
+
assert_almost_equal(np.corrcoef(nx, rowvar=False, bias=True),
|
| 1479 |
+
corrcoef(x, rowvar=False, bias=True))
|
| 1480 |
+
try:
|
| 1481 |
+
corrcoef(x, allow_masked=False)
|
| 1482 |
+
except ValueError:
|
| 1483 |
+
pass
|
| 1484 |
+
# 2 1D variables w/ missing values
|
| 1485 |
+
nx = x[1:-1]
|
| 1486 |
+
assert_almost_equal(np.corrcoef(nx, nx[::-1]), corrcoef(x, x[::-1]))
|
| 1487 |
+
assert_almost_equal(np.corrcoef(nx, nx[::-1], rowvar=False),
|
| 1488 |
+
corrcoef(x, x[::-1], rowvar=False))
|
| 1489 |
+
with suppress_warnings() as sup:
|
| 1490 |
+
sup.filter(DeprecationWarning, "bias and ddof have no effect")
|
| 1491 |
+
# ddof and bias have no or negligible effect on the function
|
| 1492 |
+
assert_almost_equal(np.corrcoef(nx, nx[::-1]),
|
| 1493 |
+
corrcoef(x, x[::-1], bias=1))
|
| 1494 |
+
assert_almost_equal(np.corrcoef(nx, nx[::-1]),
|
| 1495 |
+
corrcoef(x, x[::-1], ddof=2))
|
| 1496 |
+
|
| 1497 |
+
def test_2d_with_missing(self):
|
| 1498 |
+
# Test corrcoef on 2D variable w/ missing value
|
| 1499 |
+
x = self.data
|
| 1500 |
+
x[-1] = masked
|
| 1501 |
+
x = x.reshape(3, 4)
|
| 1502 |
+
|
| 1503 |
+
test = corrcoef(x)
|
| 1504 |
+
control = np.corrcoef(x)
|
| 1505 |
+
assert_almost_equal(test[:-1, :-1], control[:-1, :-1])
|
| 1506 |
+
with suppress_warnings() as sup:
|
| 1507 |
+
sup.filter(DeprecationWarning, "bias and ddof have no effect")
|
| 1508 |
+
# ddof and bias have no or negligible effect on the function
|
| 1509 |
+
assert_almost_equal(corrcoef(x, ddof=-2)[:-1, :-1],
|
| 1510 |
+
control[:-1, :-1])
|
| 1511 |
+
assert_almost_equal(corrcoef(x, ddof=3)[:-1, :-1],
|
| 1512 |
+
control[:-1, :-1])
|
| 1513 |
+
assert_almost_equal(corrcoef(x, bias=1)[:-1, :-1],
|
| 1514 |
+
control[:-1, :-1])
|
| 1515 |
+
|
| 1516 |
+
|
| 1517 |
+
class TestPolynomial:
|
| 1518 |
+
#
|
| 1519 |
+
def test_polyfit(self):
|
| 1520 |
+
# Tests polyfit
|
| 1521 |
+
# On ndarrays
|
| 1522 |
+
x = np.random.rand(10)
|
| 1523 |
+
y = np.random.rand(20).reshape(-1, 2)
|
| 1524 |
+
assert_almost_equal(polyfit(x, y, 3), np.polyfit(x, y, 3))
|
| 1525 |
+
# ON 1D maskedarrays
|
| 1526 |
+
x = x.view(MaskedArray)
|
| 1527 |
+
x[0] = masked
|
| 1528 |
+
y = y.view(MaskedArray)
|
| 1529 |
+
y[0, 0] = y[-1, -1] = masked
|
| 1530 |
+
#
|
| 1531 |
+
(C, R, K, S, D) = polyfit(x, y[:, 0], 3, full=True)
|
| 1532 |
+
(c, r, k, s, d) = np.polyfit(x[1:], y[1:, 0].compressed(), 3,
|
| 1533 |
+
full=True)
|
| 1534 |
+
for (a, a_) in zip((C, R, K, S, D), (c, r, k, s, d)):
|
| 1535 |
+
assert_almost_equal(a, a_)
|
| 1536 |
+
#
|
| 1537 |
+
(C, R, K, S, D) = polyfit(x, y[:, -1], 3, full=True)
|
| 1538 |
+
(c, r, k, s, d) = np.polyfit(x[1:-1], y[1:-1, -1], 3, full=True)
|
| 1539 |
+
for (a, a_) in zip((C, R, K, S, D), (c, r, k, s, d)):
|
| 1540 |
+
assert_almost_equal(a, a_)
|
| 1541 |
+
#
|
| 1542 |
+
(C, R, K, S, D) = polyfit(x, y, 3, full=True)
|
| 1543 |
+
(c, r, k, s, d) = np.polyfit(x[1:-1], y[1:-1, :], 3, full=True)
|
| 1544 |
+
for (a, a_) in zip((C, R, K, S, D), (c, r, k, s, d)):
|
| 1545 |
+
assert_almost_equal(a, a_)
|
| 1546 |
+
#
|
| 1547 |
+
w = np.random.rand(10) + 1
|
| 1548 |
+
wo = w.copy()
|
| 1549 |
+
xs = x[1:-1]
|
| 1550 |
+
ys = y[1:-1]
|
| 1551 |
+
ws = w[1:-1]
|
| 1552 |
+
(C, R, K, S, D) = polyfit(x, y, 3, full=True, w=w)
|
| 1553 |
+
(c, r, k, s, d) = np.polyfit(xs, ys, 3, full=True, w=ws)
|
| 1554 |
+
assert_equal(w, wo)
|
| 1555 |
+
for (a, a_) in zip((C, R, K, S, D), (c, r, k, s, d)):
|
| 1556 |
+
assert_almost_equal(a, a_)
|
| 1557 |
+
|
| 1558 |
+
def test_polyfit_with_masked_NaNs(self):
|
| 1559 |
+
x = np.random.rand(10)
|
| 1560 |
+
y = np.random.rand(20).reshape(-1, 2)
|
| 1561 |
+
|
| 1562 |
+
x[0] = np.nan
|
| 1563 |
+
y[-1, -1] = np.nan
|
| 1564 |
+
x = x.view(MaskedArray)
|
| 1565 |
+
y = y.view(MaskedArray)
|
| 1566 |
+
x[0] = masked
|
| 1567 |
+
y[-1, -1] = masked
|
| 1568 |
+
|
| 1569 |
+
(C, R, K, S, D) = polyfit(x, y, 3, full=True)
|
| 1570 |
+
(c, r, k, s, d) = np.polyfit(x[1:-1], y[1:-1, :], 3, full=True)
|
| 1571 |
+
for (a, a_) in zip((C, R, K, S, D), (c, r, k, s, d)):
|
| 1572 |
+
assert_almost_equal(a, a_)
|
| 1573 |
+
|
| 1574 |
+
|
| 1575 |
+
class TestArraySetOps:
|
| 1576 |
+
|
| 1577 |
+
def test_unique_onlist(self):
|
| 1578 |
+
# Test unique on list
|
| 1579 |
+
data = [1, 1, 1, 2, 2, 3]
|
| 1580 |
+
test = unique(data, return_index=True, return_inverse=True)
|
| 1581 |
+
assert_(isinstance(test[0], MaskedArray))
|
| 1582 |
+
assert_equal(test[0], masked_array([1, 2, 3], mask=[0, 0, 0]))
|
| 1583 |
+
assert_equal(test[1], [0, 3, 5])
|
| 1584 |
+
assert_equal(test[2], [0, 0, 0, 1, 1, 2])
|
| 1585 |
+
|
| 1586 |
+
def test_unique_onmaskedarray(self):
|
| 1587 |
+
# Test unique on masked data w/use_mask=True
|
| 1588 |
+
data = masked_array([1, 1, 1, 2, 2, 3], mask=[0, 0, 1, 0, 1, 0])
|
| 1589 |
+
test = unique(data, return_index=True, return_inverse=True)
|
| 1590 |
+
assert_equal(test[0], masked_array([1, 2, 3, -1], mask=[0, 0, 0, 1]))
|
| 1591 |
+
assert_equal(test[1], [0, 3, 5, 2])
|
| 1592 |
+
assert_equal(test[2], [0, 0, 3, 1, 3, 2])
|
| 1593 |
+
#
|
| 1594 |
+
data.fill_value = 3
|
| 1595 |
+
data = masked_array(data=[1, 1, 1, 2, 2, 3],
|
| 1596 |
+
mask=[0, 0, 1, 0, 1, 0], fill_value=3)
|
| 1597 |
+
test = unique(data, return_index=True, return_inverse=True)
|
| 1598 |
+
assert_equal(test[0], masked_array([1, 2, 3, -1], mask=[0, 0, 0, 1]))
|
| 1599 |
+
assert_equal(test[1], [0, 3, 5, 2])
|
| 1600 |
+
assert_equal(test[2], [0, 0, 3, 1, 3, 2])
|
| 1601 |
+
|
| 1602 |
+
def test_unique_allmasked(self):
|
| 1603 |
+
# Test all masked
|
| 1604 |
+
data = masked_array([1, 1, 1], mask=True)
|
| 1605 |
+
test = unique(data, return_index=True, return_inverse=True)
|
| 1606 |
+
assert_equal(test[0], masked_array([1, ], mask=[True]))
|
| 1607 |
+
assert_equal(test[1], [0])
|
| 1608 |
+
assert_equal(test[2], [0, 0, 0])
|
| 1609 |
+
#
|
| 1610 |
+
# Test masked
|
| 1611 |
+
data = masked
|
| 1612 |
+
test = unique(data, return_index=True, return_inverse=True)
|
| 1613 |
+
assert_equal(test[0], masked_array(masked))
|
| 1614 |
+
assert_equal(test[1], [0])
|
| 1615 |
+
assert_equal(test[2], [0])
|
| 1616 |
+
|
| 1617 |
+
def test_ediff1d(self):
|
| 1618 |
+
# Tests mediff1d
|
| 1619 |
+
x = masked_array(np.arange(5), mask=[1, 0, 0, 0, 1])
|
| 1620 |
+
control = array([1, 1, 1, 4], mask=[1, 0, 0, 1])
|
| 1621 |
+
test = ediff1d(x)
|
| 1622 |
+
assert_equal(test, control)
|
| 1623 |
+
assert_equal(test.filled(0), control.filled(0))
|
| 1624 |
+
assert_equal(test.mask, control.mask)
|
| 1625 |
+
|
| 1626 |
+
def test_ediff1d_tobegin(self):
|
| 1627 |
+
# Test ediff1d w/ to_begin
|
| 1628 |
+
x = masked_array(np.arange(5), mask=[1, 0, 0, 0, 1])
|
| 1629 |
+
test = ediff1d(x, to_begin=masked)
|
| 1630 |
+
control = array([0, 1, 1, 1, 4], mask=[1, 1, 0, 0, 1])
|
| 1631 |
+
assert_equal(test, control)
|
| 1632 |
+
assert_equal(test.filled(0), control.filled(0))
|
| 1633 |
+
assert_equal(test.mask, control.mask)
|
| 1634 |
+
#
|
| 1635 |
+
test = ediff1d(x, to_begin=[1, 2, 3])
|
| 1636 |
+
control = array([1, 2, 3, 1, 1, 1, 4], mask=[0, 0, 0, 1, 0, 0, 1])
|
| 1637 |
+
assert_equal(test, control)
|
| 1638 |
+
assert_equal(test.filled(0), control.filled(0))
|
| 1639 |
+
assert_equal(test.mask, control.mask)
|
| 1640 |
+
|
| 1641 |
+
def test_ediff1d_toend(self):
|
| 1642 |
+
# Test ediff1d w/ to_end
|
| 1643 |
+
x = masked_array(np.arange(5), mask=[1, 0, 0, 0, 1])
|
| 1644 |
+
test = ediff1d(x, to_end=masked)
|
| 1645 |
+
control = array([1, 1, 1, 4, 0], mask=[1, 0, 0, 1, 1])
|
| 1646 |
+
assert_equal(test, control)
|
| 1647 |
+
assert_equal(test.filled(0), control.filled(0))
|
| 1648 |
+
assert_equal(test.mask, control.mask)
|
| 1649 |
+
#
|
| 1650 |
+
test = ediff1d(x, to_end=[1, 2, 3])
|
| 1651 |
+
control = array([1, 1, 1, 4, 1, 2, 3], mask=[1, 0, 0, 1, 0, 0, 0])
|
| 1652 |
+
assert_equal(test, control)
|
| 1653 |
+
assert_equal(test.filled(0), control.filled(0))
|
| 1654 |
+
assert_equal(test.mask, control.mask)
|
| 1655 |
+
|
| 1656 |
+
def test_ediff1d_tobegin_toend(self):
|
| 1657 |
+
# Test ediff1d w/ to_begin and to_end
|
| 1658 |
+
x = masked_array(np.arange(5), mask=[1, 0, 0, 0, 1])
|
| 1659 |
+
test = ediff1d(x, to_end=masked, to_begin=masked)
|
| 1660 |
+
control = array([0, 1, 1, 1, 4, 0], mask=[1, 1, 0, 0, 1, 1])
|
| 1661 |
+
assert_equal(test, control)
|
| 1662 |
+
assert_equal(test.filled(0), control.filled(0))
|
| 1663 |
+
assert_equal(test.mask, control.mask)
|
| 1664 |
+
#
|
| 1665 |
+
test = ediff1d(x, to_end=[1, 2, 3], to_begin=masked)
|
| 1666 |
+
control = array([0, 1, 1, 1, 4, 1, 2, 3],
|
| 1667 |
+
mask=[1, 1, 0, 0, 1, 0, 0, 0])
|
| 1668 |
+
assert_equal(test, control)
|
| 1669 |
+
assert_equal(test.filled(0), control.filled(0))
|
| 1670 |
+
assert_equal(test.mask, control.mask)
|
| 1671 |
+
|
| 1672 |
+
def test_ediff1d_ndarray(self):
|
| 1673 |
+
# Test ediff1d w/ a ndarray
|
| 1674 |
+
x = np.arange(5)
|
| 1675 |
+
test = ediff1d(x)
|
| 1676 |
+
control = array([1, 1, 1, 1], mask=[0, 0, 0, 0])
|
| 1677 |
+
assert_equal(test, control)
|
| 1678 |
+
assert_(isinstance(test, MaskedArray))
|
| 1679 |
+
assert_equal(test.filled(0), control.filled(0))
|
| 1680 |
+
assert_equal(test.mask, control.mask)
|
| 1681 |
+
#
|
| 1682 |
+
test = ediff1d(x, to_end=masked, to_begin=masked)
|
| 1683 |
+
control = array([0, 1, 1, 1, 1, 0], mask=[1, 0, 0, 0, 0, 1])
|
| 1684 |
+
assert_(isinstance(test, MaskedArray))
|
| 1685 |
+
assert_equal(test.filled(0), control.filled(0))
|
| 1686 |
+
assert_equal(test.mask, control.mask)
|
| 1687 |
+
|
| 1688 |
+
def test_intersect1d(self):
|
| 1689 |
+
# Test intersect1d
|
| 1690 |
+
x = array([1, 3, 3, 3], mask=[0, 0, 0, 1])
|
| 1691 |
+
y = array([3, 1, 1, 1], mask=[0, 0, 0, 1])
|
| 1692 |
+
test = intersect1d(x, y)
|
| 1693 |
+
control = array([1, 3, -1], mask=[0, 0, 1])
|
| 1694 |
+
assert_equal(test, control)
|
| 1695 |
+
|
| 1696 |
+
def test_setxor1d(self):
|
| 1697 |
+
# Test setxor1d
|
| 1698 |
+
a = array([1, 2, 5, 7, -1], mask=[0, 0, 0, 0, 1])
|
| 1699 |
+
b = array([1, 2, 3, 4, 5, -1], mask=[0, 0, 0, 0, 0, 1])
|
| 1700 |
+
test = setxor1d(a, b)
|
| 1701 |
+
assert_equal(test, array([3, 4, 7]))
|
| 1702 |
+
#
|
| 1703 |
+
a = array([1, 2, 5, 7, -1], mask=[0, 0, 0, 0, 1])
|
| 1704 |
+
b = [1, 2, 3, 4, 5]
|
| 1705 |
+
test = setxor1d(a, b)
|
| 1706 |
+
assert_equal(test, array([3, 4, 7, -1], mask=[0, 0, 0, 1]))
|
| 1707 |
+
#
|
| 1708 |
+
a = array([1, 2, 3])
|
| 1709 |
+
b = array([6, 5, 4])
|
| 1710 |
+
test = setxor1d(a, b)
|
| 1711 |
+
assert_(isinstance(test, MaskedArray))
|
| 1712 |
+
assert_equal(test, [1, 2, 3, 4, 5, 6])
|
| 1713 |
+
#
|
| 1714 |
+
a = array([1, 8, 2, 3], mask=[0, 1, 0, 0])
|
| 1715 |
+
b = array([6, 5, 4, 8], mask=[0, 0, 0, 1])
|
| 1716 |
+
test = setxor1d(a, b)
|
| 1717 |
+
assert_(isinstance(test, MaskedArray))
|
| 1718 |
+
assert_equal(test, [1, 2, 3, 4, 5, 6])
|
| 1719 |
+
#
|
| 1720 |
+
assert_array_equal([], setxor1d([], []))
|
| 1721 |
+
|
| 1722 |
+
def test_setxor1d_unique(self):
|
| 1723 |
+
# Test setxor1d with assume_unique=True
|
| 1724 |
+
a = array([1, 2, 5, 7, -1], mask=[0, 0, 0, 0, 1])
|
| 1725 |
+
b = [1, 2, 3, 4, 5]
|
| 1726 |
+
test = setxor1d(a, b, assume_unique=True)
|
| 1727 |
+
assert_equal(test, array([3, 4, 7, -1], mask=[0, 0, 0, 1]))
|
| 1728 |
+
#
|
| 1729 |
+
a = array([1, 8, 2, 3], mask=[0, 1, 0, 0])
|
| 1730 |
+
b = array([6, 5, 4, 8], mask=[0, 0, 0, 1])
|
| 1731 |
+
test = setxor1d(a, b, assume_unique=True)
|
| 1732 |
+
assert_(isinstance(test, MaskedArray))
|
| 1733 |
+
assert_equal(test, [1, 2, 3, 4, 5, 6])
|
| 1734 |
+
#
|
| 1735 |
+
a = array([[1], [8], [2], [3]])
|
| 1736 |
+
b = array([[6, 5], [4, 8]])
|
| 1737 |
+
test = setxor1d(a, b, assume_unique=True)
|
| 1738 |
+
assert_(isinstance(test, MaskedArray))
|
| 1739 |
+
assert_equal(test, [1, 2, 3, 4, 5, 6])
|
| 1740 |
+
|
| 1741 |
+
def test_isin(self):
|
| 1742 |
+
# the tests for in1d cover most of isin's behavior
|
| 1743 |
+
# if in1d is removed, would need to change those tests to test
|
| 1744 |
+
# isin instead.
|
| 1745 |
+
a = np.arange(24).reshape([2, 3, 4])
|
| 1746 |
+
mask = np.zeros([2, 3, 4])
|
| 1747 |
+
mask[1, 2, 0] = 1
|
| 1748 |
+
a = array(a, mask=mask)
|
| 1749 |
+
b = array(data=[0, 10, 20, 30, 1, 3, 11, 22, 33],
|
| 1750 |
+
mask=[0, 1, 0, 1, 0, 1, 0, 1, 0])
|
| 1751 |
+
ec = zeros((2, 3, 4), dtype=bool)
|
| 1752 |
+
ec[0, 0, 0] = True
|
| 1753 |
+
ec[0, 0, 1] = True
|
| 1754 |
+
ec[0, 2, 3] = True
|
| 1755 |
+
c = isin(a, b)
|
| 1756 |
+
assert_(isinstance(c, MaskedArray))
|
| 1757 |
+
assert_array_equal(c, ec)
|
| 1758 |
+
# compare results of np.isin to ma.isin
|
| 1759 |
+
d = np.isin(a, b[~b.mask]) & ~a.mask
|
| 1760 |
+
assert_array_equal(c, d)
|
| 1761 |
+
|
| 1762 |
+
def test_in1d(self):
|
| 1763 |
+
# Test in1d
|
| 1764 |
+
a = array([1, 2, 5, 7, -1], mask=[0, 0, 0, 0, 1])
|
| 1765 |
+
b = array([1, 2, 3, 4, 5, -1], mask=[0, 0, 0, 0, 0, 1])
|
| 1766 |
+
test = in1d(a, b)
|
| 1767 |
+
assert_equal(test, [True, True, True, False, True])
|
| 1768 |
+
#
|
| 1769 |
+
a = array([5, 5, 2, 1, -1], mask=[0, 0, 0, 0, 1])
|
| 1770 |
+
b = array([1, 5, -1], mask=[0, 0, 1])
|
| 1771 |
+
test = in1d(a, b)
|
| 1772 |
+
assert_equal(test, [True, True, False, True, True])
|
| 1773 |
+
#
|
| 1774 |
+
assert_array_equal([], in1d([], []))
|
| 1775 |
+
|
| 1776 |
+
def test_in1d_invert(self):
|
| 1777 |
+
# Test in1d's invert parameter
|
| 1778 |
+
a = array([1, 2, 5, 7, -1], mask=[0, 0, 0, 0, 1])
|
| 1779 |
+
b = array([1, 2, 3, 4, 5, -1], mask=[0, 0, 0, 0, 0, 1])
|
| 1780 |
+
assert_equal(np.invert(in1d(a, b)), in1d(a, b, invert=True))
|
| 1781 |
+
|
| 1782 |
+
a = array([5, 5, 2, 1, -1], mask=[0, 0, 0, 0, 1])
|
| 1783 |
+
b = array([1, 5, -1], mask=[0, 0, 1])
|
| 1784 |
+
assert_equal(np.invert(in1d(a, b)), in1d(a, b, invert=True))
|
| 1785 |
+
|
| 1786 |
+
assert_array_equal([], in1d([], [], invert=True))
|
| 1787 |
+
|
| 1788 |
+
def test_union1d(self):
|
| 1789 |
+
# Test union1d
|
| 1790 |
+
a = array([1, 2, 5, 7, 5, -1], mask=[0, 0, 0, 0, 0, 1])
|
| 1791 |
+
b = array([1, 2, 3, 4, 5, -1], mask=[0, 0, 0, 0, 0, 1])
|
| 1792 |
+
test = union1d(a, b)
|
| 1793 |
+
control = array([1, 2, 3, 4, 5, 7, -1], mask=[0, 0, 0, 0, 0, 0, 1])
|
| 1794 |
+
assert_equal(test, control)
|
| 1795 |
+
|
| 1796 |
+
# Tests gh-10340, arguments to union1d should be
|
| 1797 |
+
# flattened if they are not already 1D
|
| 1798 |
+
x = array([[0, 1, 2], [3, 4, 5]], mask=[[0, 0, 0], [0, 0, 1]])
|
| 1799 |
+
y = array([0, 1, 2, 3, 4], mask=[0, 0, 0, 0, 1])
|
| 1800 |
+
ez = array([0, 1, 2, 3, 4, 5], mask=[0, 0, 0, 0, 0, 1])
|
| 1801 |
+
z = union1d(x, y)
|
| 1802 |
+
assert_equal(z, ez)
|
| 1803 |
+
#
|
| 1804 |
+
assert_array_equal([], union1d([], []))
|
| 1805 |
+
|
| 1806 |
+
def test_setdiff1d(self):
|
| 1807 |
+
# Test setdiff1d
|
| 1808 |
+
a = array([6, 5, 4, 7, 7, 1, 2, 1], mask=[0, 0, 0, 0, 0, 0, 0, 1])
|
| 1809 |
+
b = array([2, 4, 3, 3, 2, 1, 5])
|
| 1810 |
+
test = setdiff1d(a, b)
|
| 1811 |
+
assert_equal(test, array([6, 7, -1], mask=[0, 0, 1]))
|
| 1812 |
+
#
|
| 1813 |
+
a = arange(10)
|
| 1814 |
+
b = arange(8)
|
| 1815 |
+
assert_equal(setdiff1d(a, b), array([8, 9]))
|
| 1816 |
+
a = array([], np.uint32, mask=[])
|
| 1817 |
+
assert_equal(setdiff1d(a, []).dtype, np.uint32)
|
| 1818 |
+
|
| 1819 |
+
def test_setdiff1d_char_array(self):
|
| 1820 |
+
# Test setdiff1d_charray
|
| 1821 |
+
a = np.array(['a', 'b', 'c'])
|
| 1822 |
+
b = np.array(['a', 'b', 's'])
|
| 1823 |
+
assert_array_equal(setdiff1d(a, b), np.array(['c']))
|
| 1824 |
+
|
| 1825 |
+
|
| 1826 |
+
class TestShapeBase:
|
| 1827 |
+
|
| 1828 |
+
def test_atleast_2d(self):
|
| 1829 |
+
# Test atleast_2d
|
| 1830 |
+
a = masked_array([0, 1, 2], mask=[0, 1, 0])
|
| 1831 |
+
b = atleast_2d(a)
|
| 1832 |
+
assert_equal(b.shape, (1, 3))
|
| 1833 |
+
assert_equal(b.mask.shape, b.data.shape)
|
| 1834 |
+
assert_equal(a.shape, (3,))
|
| 1835 |
+
assert_equal(a.mask.shape, a.data.shape)
|
| 1836 |
+
assert_equal(b.mask.shape, b.data.shape)
|
| 1837 |
+
|
| 1838 |
+
def test_shape_scalar(self):
|
| 1839 |
+
# the atleast and diagflat function should work with scalars
|
| 1840 |
+
# GitHub issue #3367
|
| 1841 |
+
# Additionally, the atleast functions should accept multiple scalars
|
| 1842 |
+
# correctly
|
| 1843 |
+
b = atleast_1d(1.0)
|
| 1844 |
+
assert_equal(b.shape, (1,))
|
| 1845 |
+
assert_equal(b.mask.shape, b.shape)
|
| 1846 |
+
assert_equal(b.data.shape, b.shape)
|
| 1847 |
+
|
| 1848 |
+
b = atleast_1d(1.0, 2.0)
|
| 1849 |
+
for a in b:
|
| 1850 |
+
assert_equal(a.shape, (1,))
|
| 1851 |
+
assert_equal(a.mask.shape, a.shape)
|
| 1852 |
+
assert_equal(a.data.shape, a.shape)
|
| 1853 |
+
|
| 1854 |
+
b = atleast_2d(1.0)
|
| 1855 |
+
assert_equal(b.shape, (1, 1))
|
| 1856 |
+
assert_equal(b.mask.shape, b.shape)
|
| 1857 |
+
assert_equal(b.data.shape, b.shape)
|
| 1858 |
+
|
| 1859 |
+
b = atleast_2d(1.0, 2.0)
|
| 1860 |
+
for a in b:
|
| 1861 |
+
assert_equal(a.shape, (1, 1))
|
| 1862 |
+
assert_equal(a.mask.shape, a.shape)
|
| 1863 |
+
assert_equal(a.data.shape, a.shape)
|
| 1864 |
+
|
| 1865 |
+
b = atleast_3d(1.0)
|
| 1866 |
+
assert_equal(b.shape, (1, 1, 1))
|
| 1867 |
+
assert_equal(b.mask.shape, b.shape)
|
| 1868 |
+
assert_equal(b.data.shape, b.shape)
|
| 1869 |
+
|
| 1870 |
+
b = atleast_3d(1.0, 2.0)
|
| 1871 |
+
for a in b:
|
| 1872 |
+
assert_equal(a.shape, (1, 1, 1))
|
| 1873 |
+
assert_equal(a.mask.shape, a.shape)
|
| 1874 |
+
assert_equal(a.data.shape, a.shape)
|
| 1875 |
+
|
| 1876 |
+
b = diagflat(1.0)
|
| 1877 |
+
assert_equal(b.shape, (1, 1))
|
| 1878 |
+
assert_equal(b.mask.shape, b.data.shape)
|
| 1879 |
+
|
| 1880 |
+
|
| 1881 |
+
class TestNDEnumerate:
|
| 1882 |
+
|
| 1883 |
+
def test_ndenumerate_nomasked(self):
|
| 1884 |
+
ordinary = np.arange(6.).reshape((1, 3, 2))
|
| 1885 |
+
empty_mask = np.zeros_like(ordinary, dtype=bool)
|
| 1886 |
+
with_mask = masked_array(ordinary, mask=empty_mask)
|
| 1887 |
+
assert_equal(list(np.ndenumerate(ordinary)),
|
| 1888 |
+
list(ndenumerate(ordinary)))
|
| 1889 |
+
assert_equal(list(ndenumerate(ordinary)),
|
| 1890 |
+
list(ndenumerate(with_mask)))
|
| 1891 |
+
assert_equal(list(ndenumerate(with_mask)),
|
| 1892 |
+
list(ndenumerate(with_mask, compressed=False)))
|
| 1893 |
+
|
| 1894 |
+
def test_ndenumerate_allmasked(self):
|
| 1895 |
+
a = masked_all(())
|
| 1896 |
+
b = masked_all((100,))
|
| 1897 |
+
c = masked_all((2, 3, 4))
|
| 1898 |
+
assert_equal(list(ndenumerate(a)), [])
|
| 1899 |
+
assert_equal(list(ndenumerate(b)), [])
|
| 1900 |
+
assert_equal(list(ndenumerate(b, compressed=False)),
|
| 1901 |
+
list(zip(np.ndindex((100,)), 100 * [masked])))
|
| 1902 |
+
assert_equal(list(ndenumerate(c)), [])
|
| 1903 |
+
assert_equal(list(ndenumerate(c, compressed=False)),
|
| 1904 |
+
list(zip(np.ndindex((2, 3, 4)), 2 * 3 * 4 * [masked])))
|
| 1905 |
+
|
| 1906 |
+
def test_ndenumerate_mixedmasked(self):
|
| 1907 |
+
a = masked_array(np.arange(12).reshape((3, 4)),
|
| 1908 |
+
mask=[[1, 1, 1, 1],
|
| 1909 |
+
[1, 1, 0, 1],
|
| 1910 |
+
[0, 0, 0, 0]])
|
| 1911 |
+
items = [((1, 2), 6),
|
| 1912 |
+
((2, 0), 8), ((2, 1), 9), ((2, 2), 10), ((2, 3), 11)]
|
| 1913 |
+
assert_equal(list(ndenumerate(a)), items)
|
| 1914 |
+
assert_equal(len(list(ndenumerate(a, compressed=False))), a.size)
|
| 1915 |
+
for coordinate, value in ndenumerate(a, compressed=False):
|
| 1916 |
+
assert_equal(a[coordinate], value)
|
| 1917 |
+
|
| 1918 |
+
|
| 1919 |
+
class TestStack:
|
| 1920 |
+
|
| 1921 |
+
def test_stack_1d(self):
|
| 1922 |
+
a = masked_array([0, 1, 2], mask=[0, 1, 0])
|
| 1923 |
+
b = masked_array([9, 8, 7], mask=[1, 0, 0])
|
| 1924 |
+
|
| 1925 |
+
c = stack([a, b], axis=0)
|
| 1926 |
+
assert_equal(c.shape, (2, 3))
|
| 1927 |
+
assert_array_equal(a.mask, c[0].mask)
|
| 1928 |
+
assert_array_equal(b.mask, c[1].mask)
|
| 1929 |
+
|
| 1930 |
+
d = vstack([a, b])
|
| 1931 |
+
assert_array_equal(c.data, d.data)
|
| 1932 |
+
assert_array_equal(c.mask, d.mask)
|
| 1933 |
+
|
| 1934 |
+
c = stack([a, b], axis=1)
|
| 1935 |
+
assert_equal(c.shape, (3, 2))
|
| 1936 |
+
assert_array_equal(a.mask, c[:, 0].mask)
|
| 1937 |
+
assert_array_equal(b.mask, c[:, 1].mask)
|
| 1938 |
+
|
| 1939 |
+
def test_stack_masks(self):
|
| 1940 |
+
a = masked_array([0, 1, 2], mask=True)
|
| 1941 |
+
b = masked_array([9, 8, 7], mask=False)
|
| 1942 |
+
|
| 1943 |
+
c = stack([a, b], axis=0)
|
| 1944 |
+
assert_equal(c.shape, (2, 3))
|
| 1945 |
+
assert_array_equal(a.mask, c[0].mask)
|
| 1946 |
+
assert_array_equal(b.mask, c[1].mask)
|
| 1947 |
+
|
| 1948 |
+
d = vstack([a, b])
|
| 1949 |
+
assert_array_equal(c.data, d.data)
|
| 1950 |
+
assert_array_equal(c.mask, d.mask)
|
| 1951 |
+
|
| 1952 |
+
c = stack([a, b], axis=1)
|
| 1953 |
+
assert_equal(c.shape, (3, 2))
|
| 1954 |
+
assert_array_equal(a.mask, c[:, 0].mask)
|
| 1955 |
+
assert_array_equal(b.mask, c[:, 1].mask)
|
| 1956 |
+
|
| 1957 |
+
def test_stack_nd(self):
|
| 1958 |
+
# 2D
|
| 1959 |
+
shp = (3, 2)
|
| 1960 |
+
d1 = np.random.randint(0, 10, shp)
|
| 1961 |
+
d2 = np.random.randint(0, 10, shp)
|
| 1962 |
+
m1 = np.random.randint(0, 2, shp).astype(bool)
|
| 1963 |
+
m2 = np.random.randint(0, 2, shp).astype(bool)
|
| 1964 |
+
a1 = masked_array(d1, mask=m1)
|
| 1965 |
+
a2 = masked_array(d2, mask=m2)
|
| 1966 |
+
|
| 1967 |
+
c = stack([a1, a2], axis=0)
|
| 1968 |
+
c_shp = (2,) + shp
|
| 1969 |
+
assert_equal(c.shape, c_shp)
|
| 1970 |
+
assert_array_equal(a1.mask, c[0].mask)
|
| 1971 |
+
assert_array_equal(a2.mask, c[1].mask)
|
| 1972 |
+
|
| 1973 |
+
c = stack([a1, a2], axis=-1)
|
| 1974 |
+
c_shp = shp + (2,)
|
| 1975 |
+
assert_equal(c.shape, c_shp)
|
| 1976 |
+
assert_array_equal(a1.mask, c[..., 0].mask)
|
| 1977 |
+
assert_array_equal(a2.mask, c[..., 1].mask)
|
| 1978 |
+
|
| 1979 |
+
# 4D
|
| 1980 |
+
shp = (3, 2, 4, 5,)
|
| 1981 |
+
d1 = np.random.randint(0, 10, shp)
|
| 1982 |
+
d2 = np.random.randint(0, 10, shp)
|
| 1983 |
+
m1 = np.random.randint(0, 2, shp).astype(bool)
|
| 1984 |
+
m2 = np.random.randint(0, 2, shp).astype(bool)
|
| 1985 |
+
a1 = masked_array(d1, mask=m1)
|
| 1986 |
+
a2 = masked_array(d2, mask=m2)
|
| 1987 |
+
|
| 1988 |
+
c = stack([a1, a2], axis=0)
|
| 1989 |
+
c_shp = (2,) + shp
|
| 1990 |
+
assert_equal(c.shape, c_shp)
|
| 1991 |
+
assert_array_equal(a1.mask, c[0].mask)
|
| 1992 |
+
assert_array_equal(a2.mask, c[1].mask)
|
| 1993 |
+
|
| 1994 |
+
c = stack([a1, a2], axis=-1)
|
| 1995 |
+
c_shp = shp + (2,)
|
| 1996 |
+
assert_equal(c.shape, c_shp)
|
| 1997 |
+
assert_array_equal(a1.mask, c[..., 0].mask)
|
| 1998 |
+
assert_array_equal(a2.mask, c[..., 1].mask)
|
venv/lib/python3.13/site-packages/numpy/ma/tests/test_mrecords.py
ADDED
|
@@ -0,0 +1,497 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Tests suite for mrecords.
|
| 2 |
+
|
| 3 |
+
:author: Pierre Gerard-Marchant
|
| 4 |
+
:contact: pierregm_at_uga_dot_edu
|
| 5 |
+
|
| 6 |
+
"""
|
| 7 |
+
import pickle
|
| 8 |
+
|
| 9 |
+
import numpy as np
|
| 10 |
+
import numpy.ma as ma
|
| 11 |
+
from numpy._core.records import fromarrays as recfromarrays
|
| 12 |
+
from numpy._core.records import fromrecords as recfromrecords
|
| 13 |
+
from numpy._core.records import recarray
|
| 14 |
+
from numpy.ma import masked, nomask
|
| 15 |
+
from numpy.ma.mrecords import (
|
| 16 |
+
MaskedRecords,
|
| 17 |
+
addfield,
|
| 18 |
+
fromarrays,
|
| 19 |
+
fromrecords,
|
| 20 |
+
fromtextfile,
|
| 21 |
+
mrecarray,
|
| 22 |
+
)
|
| 23 |
+
from numpy.ma.testutils import (
|
| 24 |
+
assert_,
|
| 25 |
+
assert_equal,
|
| 26 |
+
assert_equal_records,
|
| 27 |
+
)
|
| 28 |
+
from numpy.testing import temppath
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
class TestMRecords:
|
| 32 |
+
|
| 33 |
+
ilist = [1, 2, 3, 4, 5]
|
| 34 |
+
flist = [1.1, 2.2, 3.3, 4.4, 5.5]
|
| 35 |
+
slist = [b'one', b'two', b'three', b'four', b'five']
|
| 36 |
+
ddtype = [('a', int), ('b', float), ('c', '|S8')]
|
| 37 |
+
mask = [0, 1, 0, 0, 1]
|
| 38 |
+
base = ma.array(list(zip(ilist, flist, slist)), mask=mask, dtype=ddtype)
|
| 39 |
+
|
| 40 |
+
def test_byview(self):
|
| 41 |
+
# Test creation by view
|
| 42 |
+
base = self.base
|
| 43 |
+
mbase = base.view(mrecarray)
|
| 44 |
+
assert_equal(mbase.recordmask, base.recordmask)
|
| 45 |
+
assert_equal_records(mbase._mask, base._mask)
|
| 46 |
+
assert_(isinstance(mbase._data, recarray))
|
| 47 |
+
assert_equal_records(mbase._data, base._data.view(recarray))
|
| 48 |
+
for field in ('a', 'b', 'c'):
|
| 49 |
+
assert_equal(base[field], mbase[field])
|
| 50 |
+
assert_equal_records(mbase.view(mrecarray), mbase)
|
| 51 |
+
|
| 52 |
+
def test_get(self):
|
| 53 |
+
# Tests fields retrieval
|
| 54 |
+
base = self.base.copy()
|
| 55 |
+
mbase = base.view(mrecarray)
|
| 56 |
+
# As fields..........
|
| 57 |
+
for field in ('a', 'b', 'c'):
|
| 58 |
+
assert_equal(getattr(mbase, field), mbase[field])
|
| 59 |
+
assert_equal(base[field], mbase[field])
|
| 60 |
+
# as elements .......
|
| 61 |
+
mbase_first = mbase[0]
|
| 62 |
+
assert_(isinstance(mbase_first, mrecarray))
|
| 63 |
+
assert_equal(mbase_first.dtype, mbase.dtype)
|
| 64 |
+
assert_equal(mbase_first.tolist(), (1, 1.1, b'one'))
|
| 65 |
+
# Used to be mask, now it's recordmask
|
| 66 |
+
assert_equal(mbase_first.recordmask, nomask)
|
| 67 |
+
assert_equal(mbase_first._mask.item(), (False, False, False))
|
| 68 |
+
assert_equal(mbase_first['a'], mbase['a'][0])
|
| 69 |
+
mbase_last = mbase[-1]
|
| 70 |
+
assert_(isinstance(mbase_last, mrecarray))
|
| 71 |
+
assert_equal(mbase_last.dtype, mbase.dtype)
|
| 72 |
+
assert_equal(mbase_last.tolist(), (None, None, None))
|
| 73 |
+
# Used to be mask, now it's recordmask
|
| 74 |
+
assert_equal(mbase_last.recordmask, True)
|
| 75 |
+
assert_equal(mbase_last._mask.item(), (True, True, True))
|
| 76 |
+
assert_equal(mbase_last['a'], mbase['a'][-1])
|
| 77 |
+
assert_(mbase_last['a'] is masked)
|
| 78 |
+
# as slice ..........
|
| 79 |
+
mbase_sl = mbase[:2]
|
| 80 |
+
assert_(isinstance(mbase_sl, mrecarray))
|
| 81 |
+
assert_equal(mbase_sl.dtype, mbase.dtype)
|
| 82 |
+
# Used to be mask, now it's recordmask
|
| 83 |
+
assert_equal(mbase_sl.recordmask, [0, 1])
|
| 84 |
+
assert_equal_records(mbase_sl.mask,
|
| 85 |
+
np.array([(False, False, False),
|
| 86 |
+
(True, True, True)],
|
| 87 |
+
dtype=mbase._mask.dtype))
|
| 88 |
+
assert_equal_records(mbase_sl, base[:2].view(mrecarray))
|
| 89 |
+
for field in ('a', 'b', 'c'):
|
| 90 |
+
assert_equal(getattr(mbase_sl, field), base[:2][field])
|
| 91 |
+
|
| 92 |
+
def test_set_fields(self):
|
| 93 |
+
# Tests setting fields.
|
| 94 |
+
base = self.base.copy()
|
| 95 |
+
mbase = base.view(mrecarray)
|
| 96 |
+
mbase = mbase.copy()
|
| 97 |
+
mbase.fill_value = (999999, 1e20, 'N/A')
|
| 98 |
+
# Change the data, the mask should be conserved
|
| 99 |
+
mbase.a._data[:] = 5
|
| 100 |
+
assert_equal(mbase['a']._data, [5, 5, 5, 5, 5])
|
| 101 |
+
assert_equal(mbase['a']._mask, [0, 1, 0, 0, 1])
|
| 102 |
+
# Change the elements, and the mask will follow
|
| 103 |
+
mbase.a = 1
|
| 104 |
+
assert_equal(mbase['a']._data, [1] * 5)
|
| 105 |
+
assert_equal(ma.getmaskarray(mbase['a']), [0] * 5)
|
| 106 |
+
# Use to be _mask, now it's recordmask
|
| 107 |
+
assert_equal(mbase.recordmask, [False] * 5)
|
| 108 |
+
assert_equal(mbase._mask.tolist(),
|
| 109 |
+
np.array([(0, 0, 0),
|
| 110 |
+
(0, 1, 1),
|
| 111 |
+
(0, 0, 0),
|
| 112 |
+
(0, 0, 0),
|
| 113 |
+
(0, 1, 1)],
|
| 114 |
+
dtype=bool))
|
| 115 |
+
# Set a field to mask ........................
|
| 116 |
+
mbase.c = masked
|
| 117 |
+
# Use to be mask, and now it's still mask !
|
| 118 |
+
assert_equal(mbase.c.mask, [1] * 5)
|
| 119 |
+
assert_equal(mbase.c.recordmask, [1] * 5)
|
| 120 |
+
assert_equal(ma.getmaskarray(mbase['c']), [1] * 5)
|
| 121 |
+
assert_equal(ma.getdata(mbase['c']), [b'N/A'] * 5)
|
| 122 |
+
assert_equal(mbase._mask.tolist(),
|
| 123 |
+
np.array([(0, 0, 1),
|
| 124 |
+
(0, 1, 1),
|
| 125 |
+
(0, 0, 1),
|
| 126 |
+
(0, 0, 1),
|
| 127 |
+
(0, 1, 1)],
|
| 128 |
+
dtype=bool))
|
| 129 |
+
# Set fields by slices .......................
|
| 130 |
+
mbase = base.view(mrecarray).copy()
|
| 131 |
+
mbase.a[3:] = 5
|
| 132 |
+
assert_equal(mbase.a, [1, 2, 3, 5, 5])
|
| 133 |
+
assert_equal(mbase.a._mask, [0, 1, 0, 0, 0])
|
| 134 |
+
mbase.b[3:] = masked
|
| 135 |
+
assert_equal(mbase.b, base['b'])
|
| 136 |
+
assert_equal(mbase.b._mask, [0, 1, 0, 1, 1])
|
| 137 |
+
# Set fields globally..........................
|
| 138 |
+
ndtype = [('alpha', '|S1'), ('num', int)]
|
| 139 |
+
data = ma.array([('a', 1), ('b', 2), ('c', 3)], dtype=ndtype)
|
| 140 |
+
rdata = data.view(MaskedRecords)
|
| 141 |
+
val = ma.array([10, 20, 30], mask=[1, 0, 0])
|
| 142 |
+
|
| 143 |
+
rdata['num'] = val
|
| 144 |
+
assert_equal(rdata.num, val)
|
| 145 |
+
assert_equal(rdata.num.mask, [1, 0, 0])
|
| 146 |
+
|
| 147 |
+
def test_set_fields_mask(self):
|
| 148 |
+
# Tests setting the mask of a field.
|
| 149 |
+
base = self.base.copy()
|
| 150 |
+
# This one has already a mask....
|
| 151 |
+
mbase = base.view(mrecarray)
|
| 152 |
+
mbase['a'][-2] = masked
|
| 153 |
+
assert_equal(mbase.a, [1, 2, 3, 4, 5])
|
| 154 |
+
assert_equal(mbase.a._mask, [0, 1, 0, 1, 1])
|
| 155 |
+
# This one has not yet
|
| 156 |
+
mbase = fromarrays([np.arange(5), np.random.rand(5)],
|
| 157 |
+
dtype=[('a', int), ('b', float)])
|
| 158 |
+
mbase['a'][-2] = masked
|
| 159 |
+
assert_equal(mbase.a, [0, 1, 2, 3, 4])
|
| 160 |
+
assert_equal(mbase.a._mask, [0, 0, 0, 1, 0])
|
| 161 |
+
|
| 162 |
+
def test_set_mask(self):
|
| 163 |
+
base = self.base.copy()
|
| 164 |
+
mbase = base.view(mrecarray)
|
| 165 |
+
# Set the mask to True .......................
|
| 166 |
+
mbase.mask = masked
|
| 167 |
+
assert_equal(ma.getmaskarray(mbase['b']), [1] * 5)
|
| 168 |
+
assert_equal(mbase['a']._mask, mbase['b']._mask)
|
| 169 |
+
assert_equal(mbase['a']._mask, mbase['c']._mask)
|
| 170 |
+
assert_equal(mbase._mask.tolist(),
|
| 171 |
+
np.array([(1, 1, 1)] * 5, dtype=bool))
|
| 172 |
+
# Delete the mask ............................
|
| 173 |
+
mbase.mask = nomask
|
| 174 |
+
assert_equal(ma.getmaskarray(mbase['c']), [0] * 5)
|
| 175 |
+
assert_equal(mbase._mask.tolist(),
|
| 176 |
+
np.array([(0, 0, 0)] * 5, dtype=bool))
|
| 177 |
+
|
| 178 |
+
def test_set_mask_fromarray(self):
|
| 179 |
+
base = self.base.copy()
|
| 180 |
+
mbase = base.view(mrecarray)
|
| 181 |
+
# Sets the mask w/ an array
|
| 182 |
+
mbase.mask = [1, 0, 0, 0, 1]
|
| 183 |
+
assert_equal(mbase.a.mask, [1, 0, 0, 0, 1])
|
| 184 |
+
assert_equal(mbase.b.mask, [1, 0, 0, 0, 1])
|
| 185 |
+
assert_equal(mbase.c.mask, [1, 0, 0, 0, 1])
|
| 186 |
+
# Yay, once more !
|
| 187 |
+
mbase.mask = [0, 0, 0, 0, 1]
|
| 188 |
+
assert_equal(mbase.a.mask, [0, 0, 0, 0, 1])
|
| 189 |
+
assert_equal(mbase.b.mask, [0, 0, 0, 0, 1])
|
| 190 |
+
assert_equal(mbase.c.mask, [0, 0, 0, 0, 1])
|
| 191 |
+
|
| 192 |
+
def test_set_mask_fromfields(self):
|
| 193 |
+
mbase = self.base.copy().view(mrecarray)
|
| 194 |
+
|
| 195 |
+
nmask = np.array(
|
| 196 |
+
[(0, 1, 0), (0, 1, 0), (1, 0, 1), (1, 0, 1), (0, 0, 0)],
|
| 197 |
+
dtype=[('a', bool), ('b', bool), ('c', bool)])
|
| 198 |
+
mbase.mask = nmask
|
| 199 |
+
assert_equal(mbase.a.mask, [0, 0, 1, 1, 0])
|
| 200 |
+
assert_equal(mbase.b.mask, [1, 1, 0, 0, 0])
|
| 201 |
+
assert_equal(mbase.c.mask, [0, 0, 1, 1, 0])
|
| 202 |
+
# Reinitialize and redo
|
| 203 |
+
mbase.mask = False
|
| 204 |
+
mbase.fieldmask = nmask
|
| 205 |
+
assert_equal(mbase.a.mask, [0, 0, 1, 1, 0])
|
| 206 |
+
assert_equal(mbase.b.mask, [1, 1, 0, 0, 0])
|
| 207 |
+
assert_equal(mbase.c.mask, [0, 0, 1, 1, 0])
|
| 208 |
+
|
| 209 |
+
def test_set_elements(self):
|
| 210 |
+
base = self.base.copy()
|
| 211 |
+
# Set an element to mask .....................
|
| 212 |
+
mbase = base.view(mrecarray).copy()
|
| 213 |
+
mbase[-2] = masked
|
| 214 |
+
assert_equal(
|
| 215 |
+
mbase._mask.tolist(),
|
| 216 |
+
np.array([(0, 0, 0), (1, 1, 1), (0, 0, 0), (1, 1, 1), (1, 1, 1)],
|
| 217 |
+
dtype=bool))
|
| 218 |
+
# Used to be mask, now it's recordmask!
|
| 219 |
+
assert_equal(mbase.recordmask, [0, 1, 0, 1, 1])
|
| 220 |
+
# Set slices .................................
|
| 221 |
+
mbase = base.view(mrecarray).copy()
|
| 222 |
+
mbase[:2] = (5, 5, 5)
|
| 223 |
+
assert_equal(mbase.a._data, [5, 5, 3, 4, 5])
|
| 224 |
+
assert_equal(mbase.a._mask, [0, 0, 0, 0, 1])
|
| 225 |
+
assert_equal(mbase.b._data, [5., 5., 3.3, 4.4, 5.5])
|
| 226 |
+
assert_equal(mbase.b._mask, [0, 0, 0, 0, 1])
|
| 227 |
+
assert_equal(mbase.c._data,
|
| 228 |
+
[b'5', b'5', b'three', b'four', b'five'])
|
| 229 |
+
assert_equal(mbase.b._mask, [0, 0, 0, 0, 1])
|
| 230 |
+
|
| 231 |
+
mbase = base.view(mrecarray).copy()
|
| 232 |
+
mbase[:2] = masked
|
| 233 |
+
assert_equal(mbase.a._data, [1, 2, 3, 4, 5])
|
| 234 |
+
assert_equal(mbase.a._mask, [1, 1, 0, 0, 1])
|
| 235 |
+
assert_equal(mbase.b._data, [1.1, 2.2, 3.3, 4.4, 5.5])
|
| 236 |
+
assert_equal(mbase.b._mask, [1, 1, 0, 0, 1])
|
| 237 |
+
assert_equal(mbase.c._data,
|
| 238 |
+
[b'one', b'two', b'three', b'four', b'five'])
|
| 239 |
+
assert_equal(mbase.b._mask, [1, 1, 0, 0, 1])
|
| 240 |
+
|
| 241 |
+
def test_setslices_hardmask(self):
|
| 242 |
+
# Tests setting slices w/ hardmask.
|
| 243 |
+
base = self.base.copy()
|
| 244 |
+
mbase = base.view(mrecarray)
|
| 245 |
+
mbase.harden_mask()
|
| 246 |
+
try:
|
| 247 |
+
mbase[-2:] = (5, 5, 5)
|
| 248 |
+
assert_equal(mbase.a._data, [1, 2, 3, 5, 5])
|
| 249 |
+
assert_equal(mbase.b._data, [1.1, 2.2, 3.3, 5, 5.5])
|
| 250 |
+
assert_equal(mbase.c._data,
|
| 251 |
+
[b'one', b'two', b'three', b'5', b'five'])
|
| 252 |
+
assert_equal(mbase.a._mask, [0, 1, 0, 0, 1])
|
| 253 |
+
assert_equal(mbase.b._mask, mbase.a._mask)
|
| 254 |
+
assert_equal(mbase.b._mask, mbase.c._mask)
|
| 255 |
+
except NotImplementedError:
|
| 256 |
+
# OK, not implemented yet...
|
| 257 |
+
pass
|
| 258 |
+
except AssertionError:
|
| 259 |
+
raise
|
| 260 |
+
else:
|
| 261 |
+
raise Exception("Flexible hard masks should be supported !")
|
| 262 |
+
# Not using a tuple should crash
|
| 263 |
+
try:
|
| 264 |
+
mbase[-2:] = 3
|
| 265 |
+
except (NotImplementedError, TypeError):
|
| 266 |
+
pass
|
| 267 |
+
else:
|
| 268 |
+
raise TypeError("Should have expected a readable buffer object!")
|
| 269 |
+
|
| 270 |
+
def test_hardmask(self):
|
| 271 |
+
# Test hardmask
|
| 272 |
+
base = self.base.copy()
|
| 273 |
+
mbase = base.view(mrecarray)
|
| 274 |
+
mbase.harden_mask()
|
| 275 |
+
assert_(mbase._hardmask)
|
| 276 |
+
mbase.mask = nomask
|
| 277 |
+
assert_equal_records(mbase._mask, base._mask)
|
| 278 |
+
mbase.soften_mask()
|
| 279 |
+
assert_(not mbase._hardmask)
|
| 280 |
+
mbase.mask = nomask
|
| 281 |
+
# So, the mask of a field is no longer set to nomask...
|
| 282 |
+
assert_equal_records(mbase._mask,
|
| 283 |
+
ma.make_mask_none(base.shape, base.dtype))
|
| 284 |
+
assert_(ma.make_mask(mbase['b']._mask) is nomask)
|
| 285 |
+
assert_equal(mbase['a']._mask, mbase['b']._mask)
|
| 286 |
+
|
| 287 |
+
def test_pickling(self):
|
| 288 |
+
# Test pickling
|
| 289 |
+
base = self.base.copy()
|
| 290 |
+
mrec = base.view(mrecarray)
|
| 291 |
+
for proto in range(2, pickle.HIGHEST_PROTOCOL + 1):
|
| 292 |
+
_ = pickle.dumps(mrec, protocol=proto)
|
| 293 |
+
mrec_ = pickle.loads(_)
|
| 294 |
+
assert_equal(mrec_.dtype, mrec.dtype)
|
| 295 |
+
assert_equal_records(mrec_._data, mrec._data)
|
| 296 |
+
assert_equal(mrec_._mask, mrec._mask)
|
| 297 |
+
assert_equal_records(mrec_._mask, mrec._mask)
|
| 298 |
+
|
| 299 |
+
def test_filled(self):
|
| 300 |
+
# Test filling the array
|
| 301 |
+
_a = ma.array([1, 2, 3], mask=[0, 0, 1], dtype=int)
|
| 302 |
+
_b = ma.array([1.1, 2.2, 3.3], mask=[0, 0, 1], dtype=float)
|
| 303 |
+
_c = ma.array(['one', 'two', 'three'], mask=[0, 0, 1], dtype='|S8')
|
| 304 |
+
ddtype = [('a', int), ('b', float), ('c', '|S8')]
|
| 305 |
+
mrec = fromarrays([_a, _b, _c], dtype=ddtype,
|
| 306 |
+
fill_value=(99999, 99999., 'N/A'))
|
| 307 |
+
mrecfilled = mrec.filled()
|
| 308 |
+
assert_equal(mrecfilled['a'], np.array((1, 2, 99999), dtype=int))
|
| 309 |
+
assert_equal(mrecfilled['b'], np.array((1.1, 2.2, 99999.),
|
| 310 |
+
dtype=float))
|
| 311 |
+
assert_equal(mrecfilled['c'], np.array(('one', 'two', 'N/A'),
|
| 312 |
+
dtype='|S8'))
|
| 313 |
+
|
| 314 |
+
def test_tolist(self):
|
| 315 |
+
# Test tolist.
|
| 316 |
+
_a = ma.array([1, 2, 3], mask=[0, 0, 1], dtype=int)
|
| 317 |
+
_b = ma.array([1.1, 2.2, 3.3], mask=[0, 0, 1], dtype=float)
|
| 318 |
+
_c = ma.array(['one', 'two', 'three'], mask=[1, 0, 0], dtype='|S8')
|
| 319 |
+
ddtype = [('a', int), ('b', float), ('c', '|S8')]
|
| 320 |
+
mrec = fromarrays([_a, _b, _c], dtype=ddtype,
|
| 321 |
+
fill_value=(99999, 99999., 'N/A'))
|
| 322 |
+
|
| 323 |
+
assert_equal(mrec.tolist(),
|
| 324 |
+
[(1, 1.1, None), (2, 2.2, b'two'),
|
| 325 |
+
(None, None, b'three')])
|
| 326 |
+
|
| 327 |
+
def test_withnames(self):
|
| 328 |
+
# Test the creation w/ format and names
|
| 329 |
+
x = mrecarray(1, formats=float, names='base')
|
| 330 |
+
x[0]['base'] = 10
|
| 331 |
+
assert_equal(x['base'][0], 10)
|
| 332 |
+
|
| 333 |
+
def test_exotic_formats(self):
|
| 334 |
+
# Test that 'exotic' formats are processed properly
|
| 335 |
+
easy = mrecarray(1, dtype=[('i', int), ('s', '|S8'), ('f', float)])
|
| 336 |
+
easy[0] = masked
|
| 337 |
+
assert_equal(easy.filled(1).item(), (1, b'1', 1.))
|
| 338 |
+
|
| 339 |
+
solo = mrecarray(1, dtype=[('f0', '<f8', (2, 2))])
|
| 340 |
+
solo[0] = masked
|
| 341 |
+
assert_equal(solo.filled(1).item(),
|
| 342 |
+
np.array((1,), dtype=solo.dtype).item())
|
| 343 |
+
|
| 344 |
+
mult = mrecarray(2, dtype="i4, (2,3)float, float")
|
| 345 |
+
mult[0] = masked
|
| 346 |
+
mult[1] = (1, 1, 1)
|
| 347 |
+
mult.filled(0)
|
| 348 |
+
assert_equal_records(mult.filled(0),
|
| 349 |
+
np.array([(0, 0, 0), (1, 1, 1)],
|
| 350 |
+
dtype=mult.dtype))
|
| 351 |
+
|
| 352 |
+
|
| 353 |
+
class TestView:
|
| 354 |
+
|
| 355 |
+
def setup_method(self):
|
| 356 |
+
(a, b) = (np.arange(10), np.random.rand(10))
|
| 357 |
+
ndtype = [('a', float), ('b', float)]
|
| 358 |
+
arr = np.array(list(zip(a, b)), dtype=ndtype)
|
| 359 |
+
|
| 360 |
+
mrec = fromarrays([a, b], dtype=ndtype, fill_value=(-9., -99.))
|
| 361 |
+
mrec.mask[3] = (False, True)
|
| 362 |
+
self.data = (mrec, a, b, arr)
|
| 363 |
+
|
| 364 |
+
def test_view_by_itself(self):
|
| 365 |
+
(mrec, a, b, arr) = self.data
|
| 366 |
+
test = mrec.view()
|
| 367 |
+
assert_(isinstance(test, MaskedRecords))
|
| 368 |
+
assert_equal_records(test, mrec)
|
| 369 |
+
assert_equal_records(test._mask, mrec._mask)
|
| 370 |
+
|
| 371 |
+
def test_view_simple_dtype(self):
|
| 372 |
+
(mrec, a, b, arr) = self.data
|
| 373 |
+
ntype = (float, 2)
|
| 374 |
+
test = mrec.view(ntype)
|
| 375 |
+
assert_(isinstance(test, ma.MaskedArray))
|
| 376 |
+
assert_equal(test, np.array(list(zip(a, b)), dtype=float))
|
| 377 |
+
assert_(test[3, 1] is ma.masked)
|
| 378 |
+
|
| 379 |
+
def test_view_flexible_type(self):
|
| 380 |
+
(mrec, a, b, arr) = self.data
|
| 381 |
+
alttype = [('A', float), ('B', float)]
|
| 382 |
+
test = mrec.view(alttype)
|
| 383 |
+
assert_(isinstance(test, MaskedRecords))
|
| 384 |
+
assert_equal_records(test, arr.view(alttype))
|
| 385 |
+
assert_(test['B'][3] is masked)
|
| 386 |
+
assert_equal(test.dtype, np.dtype(alttype))
|
| 387 |
+
assert_(test._fill_value is None)
|
| 388 |
+
|
| 389 |
+
|
| 390 |
+
##############################################################################
|
| 391 |
+
class TestMRecordsImport:
|
| 392 |
+
|
| 393 |
+
_a = ma.array([1, 2, 3], mask=[0, 0, 1], dtype=int)
|
| 394 |
+
_b = ma.array([1.1, 2.2, 3.3], mask=[0, 0, 1], dtype=float)
|
| 395 |
+
_c = ma.array([b'one', b'two', b'three'],
|
| 396 |
+
mask=[0, 0, 1], dtype='|S8')
|
| 397 |
+
ddtype = [('a', int), ('b', float), ('c', '|S8')]
|
| 398 |
+
mrec = fromarrays([_a, _b, _c], dtype=ddtype,
|
| 399 |
+
fill_value=(b'99999', b'99999.',
|
| 400 |
+
b'N/A'))
|
| 401 |
+
nrec = recfromarrays((_a._data, _b._data, _c._data), dtype=ddtype)
|
| 402 |
+
data = (mrec, nrec, ddtype)
|
| 403 |
+
|
| 404 |
+
def test_fromarrays(self):
|
| 405 |
+
_a = ma.array([1, 2, 3], mask=[0, 0, 1], dtype=int)
|
| 406 |
+
_b = ma.array([1.1, 2.2, 3.3], mask=[0, 0, 1], dtype=float)
|
| 407 |
+
_c = ma.array(['one', 'two', 'three'], mask=[0, 0, 1], dtype='|S8')
|
| 408 |
+
(mrec, nrec, _) = self.data
|
| 409 |
+
for (f, l) in zip(('a', 'b', 'c'), (_a, _b, _c)):
|
| 410 |
+
assert_equal(getattr(mrec, f)._mask, l._mask)
|
| 411 |
+
# One record only
|
| 412 |
+
_x = ma.array([1, 1.1, 'one'], mask=[1, 0, 0], dtype=object)
|
| 413 |
+
assert_equal_records(fromarrays(_x, dtype=mrec.dtype), mrec[0])
|
| 414 |
+
|
| 415 |
+
def test_fromrecords(self):
|
| 416 |
+
# Test construction from records.
|
| 417 |
+
(mrec, nrec, ddtype) = self.data
|
| 418 |
+
# ......
|
| 419 |
+
palist = [(1, 'abc', 3.7000002861022949, 0),
|
| 420 |
+
(2, 'xy', 6.6999998092651367, 1),
|
| 421 |
+
(0, ' ', 0.40000000596046448, 0)]
|
| 422 |
+
pa = recfromrecords(palist, names='c1, c2, c3, c4')
|
| 423 |
+
mpa = fromrecords(palist, names='c1, c2, c3, c4')
|
| 424 |
+
assert_equal_records(pa, mpa)
|
| 425 |
+
# .....
|
| 426 |
+
_mrec = fromrecords(nrec)
|
| 427 |
+
assert_equal(_mrec.dtype, mrec.dtype)
|
| 428 |
+
for field in _mrec.dtype.names:
|
| 429 |
+
assert_equal(getattr(_mrec, field), getattr(mrec._data, field))
|
| 430 |
+
|
| 431 |
+
_mrec = fromrecords(nrec.tolist(), names='c1,c2,c3')
|
| 432 |
+
assert_equal(_mrec.dtype, [('c1', int), ('c2', float), ('c3', '|S5')])
|
| 433 |
+
for (f, n) in zip(('c1', 'c2', 'c3'), ('a', 'b', 'c')):
|
| 434 |
+
assert_equal(getattr(_mrec, f), getattr(mrec._data, n))
|
| 435 |
+
|
| 436 |
+
_mrec = fromrecords(mrec)
|
| 437 |
+
assert_equal(_mrec.dtype, mrec.dtype)
|
| 438 |
+
assert_equal_records(_mrec._data, mrec.filled())
|
| 439 |
+
assert_equal_records(_mrec._mask, mrec._mask)
|
| 440 |
+
|
| 441 |
+
def test_fromrecords_wmask(self):
|
| 442 |
+
# Tests construction from records w/ mask.
|
| 443 |
+
(mrec, nrec, ddtype) = self.data
|
| 444 |
+
|
| 445 |
+
_mrec = fromrecords(nrec.tolist(), dtype=ddtype, mask=[0, 1, 0,])
|
| 446 |
+
assert_equal_records(_mrec._data, mrec._data)
|
| 447 |
+
assert_equal(_mrec._mask.tolist(), [(0, 0, 0), (1, 1, 1), (0, 0, 0)])
|
| 448 |
+
|
| 449 |
+
_mrec = fromrecords(nrec.tolist(), dtype=ddtype, mask=True)
|
| 450 |
+
assert_equal_records(_mrec._data, mrec._data)
|
| 451 |
+
assert_equal(_mrec._mask.tolist(), [(1, 1, 1), (1, 1, 1), (1, 1, 1)])
|
| 452 |
+
|
| 453 |
+
_mrec = fromrecords(nrec.tolist(), dtype=ddtype, mask=mrec._mask)
|
| 454 |
+
assert_equal_records(_mrec._data, mrec._data)
|
| 455 |
+
assert_equal(_mrec._mask.tolist(), mrec._mask.tolist())
|
| 456 |
+
|
| 457 |
+
_mrec = fromrecords(nrec.tolist(), dtype=ddtype,
|
| 458 |
+
mask=mrec._mask.tolist())
|
| 459 |
+
assert_equal_records(_mrec._data, mrec._data)
|
| 460 |
+
assert_equal(_mrec._mask.tolist(), mrec._mask.tolist())
|
| 461 |
+
|
| 462 |
+
def test_fromtextfile(self):
|
| 463 |
+
# Tests reading from a text file.
|
| 464 |
+
fcontent = (
|
| 465 |
+
"""#
|
| 466 |
+
'One (S)','Two (I)','Three (F)','Four (M)','Five (-)','Six (C)'
|
| 467 |
+
'strings',1,1.0,'mixed column',,1
|
| 468 |
+
'with embedded "double quotes"',2,2.0,1.0,,1
|
| 469 |
+
'strings',3,3.0E5,3,,1
|
| 470 |
+
'strings',4,-1e-10,,,1
|
| 471 |
+
""")
|
| 472 |
+
with temppath() as path:
|
| 473 |
+
with open(path, 'w') as f:
|
| 474 |
+
f.write(fcontent)
|
| 475 |
+
mrectxt = fromtextfile(path, delimiter=',', varnames='ABCDEFG')
|
| 476 |
+
assert_(isinstance(mrectxt, MaskedRecords))
|
| 477 |
+
assert_equal(mrectxt.F, [1, 1, 1, 1])
|
| 478 |
+
assert_equal(mrectxt.E._mask, [1, 1, 1, 1])
|
| 479 |
+
assert_equal(mrectxt.C, [1, 2, 3.e+5, -1e-10])
|
| 480 |
+
|
| 481 |
+
def test_addfield(self):
|
| 482 |
+
# Tests addfield
|
| 483 |
+
(mrec, nrec, ddtype) = self.data
|
| 484 |
+
(d, m) = ([100, 200, 300], [1, 0, 0])
|
| 485 |
+
mrec = addfield(mrec, ma.array(d, mask=m))
|
| 486 |
+
assert_equal(mrec.f3, d)
|
| 487 |
+
assert_equal(mrec.f3._mask, m)
|
| 488 |
+
|
| 489 |
+
|
| 490 |
+
def test_record_array_with_object_field():
|
| 491 |
+
# Trac #1839
|
| 492 |
+
y = ma.masked_array(
|
| 493 |
+
[(1, '2'), (3, '4')],
|
| 494 |
+
mask=[(0, 0), (0, 1)],
|
| 495 |
+
dtype=[('a', int), ('b', object)])
|
| 496 |
+
# getting an item used to fail
|
| 497 |
+
y[1]
|
venv/lib/python3.13/site-packages/numpy/ma/tests/test_old_ma.py
ADDED
|
@@ -0,0 +1,942 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import pickle
|
| 2 |
+
from functools import reduce
|
| 3 |
+
|
| 4 |
+
import pytest
|
| 5 |
+
|
| 6 |
+
import numpy as np
|
| 7 |
+
import numpy._core.fromnumeric as fromnumeric
|
| 8 |
+
import numpy._core.umath as umath
|
| 9 |
+
from numpy.ma import (
|
| 10 |
+
MaskedArray,
|
| 11 |
+
MaskType,
|
| 12 |
+
absolute,
|
| 13 |
+
add,
|
| 14 |
+
all,
|
| 15 |
+
allclose,
|
| 16 |
+
allequal,
|
| 17 |
+
alltrue,
|
| 18 |
+
arange,
|
| 19 |
+
arccos,
|
| 20 |
+
arcsin,
|
| 21 |
+
arctan,
|
| 22 |
+
arctan2,
|
| 23 |
+
array,
|
| 24 |
+
average,
|
| 25 |
+
choose,
|
| 26 |
+
concatenate,
|
| 27 |
+
conjugate,
|
| 28 |
+
cos,
|
| 29 |
+
cosh,
|
| 30 |
+
count,
|
| 31 |
+
divide,
|
| 32 |
+
equal,
|
| 33 |
+
exp,
|
| 34 |
+
filled,
|
| 35 |
+
getmask,
|
| 36 |
+
greater,
|
| 37 |
+
greater_equal,
|
| 38 |
+
inner,
|
| 39 |
+
isMaskedArray,
|
| 40 |
+
less,
|
| 41 |
+
less_equal,
|
| 42 |
+
log,
|
| 43 |
+
log10,
|
| 44 |
+
make_mask,
|
| 45 |
+
masked,
|
| 46 |
+
masked_array,
|
| 47 |
+
masked_equal,
|
| 48 |
+
masked_greater,
|
| 49 |
+
masked_greater_equal,
|
| 50 |
+
masked_inside,
|
| 51 |
+
masked_less,
|
| 52 |
+
masked_less_equal,
|
| 53 |
+
masked_not_equal,
|
| 54 |
+
masked_outside,
|
| 55 |
+
masked_print_option,
|
| 56 |
+
masked_values,
|
| 57 |
+
masked_where,
|
| 58 |
+
maximum,
|
| 59 |
+
minimum,
|
| 60 |
+
multiply,
|
| 61 |
+
nomask,
|
| 62 |
+
nonzero,
|
| 63 |
+
not_equal,
|
| 64 |
+
ones,
|
| 65 |
+
outer,
|
| 66 |
+
product,
|
| 67 |
+
put,
|
| 68 |
+
ravel,
|
| 69 |
+
repeat,
|
| 70 |
+
resize,
|
| 71 |
+
shape,
|
| 72 |
+
sin,
|
| 73 |
+
sinh,
|
| 74 |
+
sometrue,
|
| 75 |
+
sort,
|
| 76 |
+
sqrt,
|
| 77 |
+
subtract,
|
| 78 |
+
sum,
|
| 79 |
+
take,
|
| 80 |
+
tan,
|
| 81 |
+
tanh,
|
| 82 |
+
transpose,
|
| 83 |
+
where,
|
| 84 |
+
zeros,
|
| 85 |
+
)
|
| 86 |
+
from numpy.testing import (
|
| 87 |
+
assert_,
|
| 88 |
+
assert_equal,
|
| 89 |
+
assert_raises,
|
| 90 |
+
)
|
| 91 |
+
|
| 92 |
+
pi = np.pi
|
| 93 |
+
|
| 94 |
+
|
| 95 |
+
def eq(v, w, msg=''):
|
| 96 |
+
result = allclose(v, w)
|
| 97 |
+
if not result:
|
| 98 |
+
print(f'Not eq:{msg}\n{v}\n----{w}')
|
| 99 |
+
return result
|
| 100 |
+
|
| 101 |
+
|
| 102 |
+
class TestMa:
|
| 103 |
+
|
| 104 |
+
def setup_method(self):
|
| 105 |
+
x = np.array([1., 1., 1., -2., pi / 2.0, 4., 5., -10., 10., 1., 2., 3.])
|
| 106 |
+
y = np.array([5., 0., 3., 2., -1., -4., 0., -10., 10., 1., 0., 3.])
|
| 107 |
+
a10 = 10.
|
| 108 |
+
m1 = [1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0]
|
| 109 |
+
m2 = [0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 0, 1]
|
| 110 |
+
xm = array(x, mask=m1)
|
| 111 |
+
ym = array(y, mask=m2)
|
| 112 |
+
z = np.array([-.5, 0., .5, .8])
|
| 113 |
+
zm = array(z, mask=[0, 1, 0, 0])
|
| 114 |
+
xf = np.where(m1, 1e+20, x)
|
| 115 |
+
s = x.shape
|
| 116 |
+
xm.set_fill_value(1e+20)
|
| 117 |
+
self.d = (x, y, a10, m1, m2, xm, ym, z, zm, xf, s)
|
| 118 |
+
|
| 119 |
+
def test_testBasic1d(self):
|
| 120 |
+
# Test of basic array creation and properties in 1 dimension.
|
| 121 |
+
(x, y, a10, m1, m2, xm, ym, z, zm, xf, s) = self.d
|
| 122 |
+
assert_(not isMaskedArray(x))
|
| 123 |
+
assert_(isMaskedArray(xm))
|
| 124 |
+
assert_equal(shape(xm), s)
|
| 125 |
+
assert_equal(xm.shape, s)
|
| 126 |
+
assert_equal(xm.dtype, x.dtype)
|
| 127 |
+
assert_equal(xm.size, reduce(lambda x, y: x * y, s))
|
| 128 |
+
assert_equal(count(xm), len(m1) - reduce(lambda x, y: x + y, m1))
|
| 129 |
+
assert_(eq(xm, xf))
|
| 130 |
+
assert_(eq(filled(xm, 1.e20), xf))
|
| 131 |
+
assert_(eq(x, xm))
|
| 132 |
+
|
| 133 |
+
@pytest.mark.parametrize("s", [(4, 3), (6, 2)])
|
| 134 |
+
def test_testBasic2d(self, s):
|
| 135 |
+
# Test of basic array creation and properties in 2 dimensions.
|
| 136 |
+
(x, y, a10, m1, m2, xm, ym, z, zm, xf, s) = self.d
|
| 137 |
+
x.shape = s
|
| 138 |
+
y.shape = s
|
| 139 |
+
xm.shape = s
|
| 140 |
+
ym.shape = s
|
| 141 |
+
xf.shape = s
|
| 142 |
+
|
| 143 |
+
assert_(not isMaskedArray(x))
|
| 144 |
+
assert_(isMaskedArray(xm))
|
| 145 |
+
assert_equal(shape(xm), s)
|
| 146 |
+
assert_equal(xm.shape, s)
|
| 147 |
+
assert_equal(xm.size, reduce(lambda x, y: x * y, s))
|
| 148 |
+
assert_equal(count(xm), len(m1) - reduce(lambda x, y: x + y, m1))
|
| 149 |
+
assert_(eq(xm, xf))
|
| 150 |
+
assert_(eq(filled(xm, 1.e20), xf))
|
| 151 |
+
assert_(eq(x, xm))
|
| 152 |
+
|
| 153 |
+
def test_testArithmetic(self):
|
| 154 |
+
# Test of basic arithmetic.
|
| 155 |
+
(x, y, a10, m1, m2, xm, ym, z, zm, xf, s) = self.d
|
| 156 |
+
a2d = array([[1, 2], [0, 4]])
|
| 157 |
+
a2dm = masked_array(a2d, [[0, 0], [1, 0]])
|
| 158 |
+
assert_(eq(a2d * a2d, a2d * a2dm))
|
| 159 |
+
assert_(eq(a2d + a2d, a2d + a2dm))
|
| 160 |
+
assert_(eq(a2d - a2d, a2d - a2dm))
|
| 161 |
+
for s in [(12,), (4, 3), (2, 6)]:
|
| 162 |
+
x = x.reshape(s)
|
| 163 |
+
y = y.reshape(s)
|
| 164 |
+
xm = xm.reshape(s)
|
| 165 |
+
ym = ym.reshape(s)
|
| 166 |
+
xf = xf.reshape(s)
|
| 167 |
+
assert_(eq(-x, -xm))
|
| 168 |
+
assert_(eq(x + y, xm + ym))
|
| 169 |
+
assert_(eq(x - y, xm - ym))
|
| 170 |
+
assert_(eq(x * y, xm * ym))
|
| 171 |
+
with np.errstate(divide='ignore', invalid='ignore'):
|
| 172 |
+
assert_(eq(x / y, xm / ym))
|
| 173 |
+
assert_(eq(a10 + y, a10 + ym))
|
| 174 |
+
assert_(eq(a10 - y, a10 - ym))
|
| 175 |
+
assert_(eq(a10 * y, a10 * ym))
|
| 176 |
+
with np.errstate(divide='ignore', invalid='ignore'):
|
| 177 |
+
assert_(eq(a10 / y, a10 / ym))
|
| 178 |
+
assert_(eq(x + a10, xm + a10))
|
| 179 |
+
assert_(eq(x - a10, xm - a10))
|
| 180 |
+
assert_(eq(x * a10, xm * a10))
|
| 181 |
+
assert_(eq(x / a10, xm / a10))
|
| 182 |
+
assert_(eq(x ** 2, xm ** 2))
|
| 183 |
+
assert_(eq(abs(x) ** 2.5, abs(xm) ** 2.5))
|
| 184 |
+
assert_(eq(x ** y, xm ** ym))
|
| 185 |
+
assert_(eq(np.add(x, y), add(xm, ym)))
|
| 186 |
+
assert_(eq(np.subtract(x, y), subtract(xm, ym)))
|
| 187 |
+
assert_(eq(np.multiply(x, y), multiply(xm, ym)))
|
| 188 |
+
with np.errstate(divide='ignore', invalid='ignore'):
|
| 189 |
+
assert_(eq(np.divide(x, y), divide(xm, ym)))
|
| 190 |
+
|
| 191 |
+
def test_testMixedArithmetic(self):
|
| 192 |
+
na = np.array([1])
|
| 193 |
+
ma = array([1])
|
| 194 |
+
assert_(isinstance(na + ma, MaskedArray))
|
| 195 |
+
assert_(isinstance(ma + na, MaskedArray))
|
| 196 |
+
|
| 197 |
+
def test_testUfuncs1(self):
|
| 198 |
+
# Test various functions such as sin, cos.
|
| 199 |
+
(x, y, a10, m1, m2, xm, ym, z, zm, xf, s) = self.d
|
| 200 |
+
assert_(eq(np.cos(x), cos(xm)))
|
| 201 |
+
assert_(eq(np.cosh(x), cosh(xm)))
|
| 202 |
+
assert_(eq(np.sin(x), sin(xm)))
|
| 203 |
+
assert_(eq(np.sinh(x), sinh(xm)))
|
| 204 |
+
assert_(eq(np.tan(x), tan(xm)))
|
| 205 |
+
assert_(eq(np.tanh(x), tanh(xm)))
|
| 206 |
+
with np.errstate(divide='ignore', invalid='ignore'):
|
| 207 |
+
assert_(eq(np.sqrt(abs(x)), sqrt(xm)))
|
| 208 |
+
assert_(eq(np.log(abs(x)), log(xm)))
|
| 209 |
+
assert_(eq(np.log10(abs(x)), log10(xm)))
|
| 210 |
+
assert_(eq(np.exp(x), exp(xm)))
|
| 211 |
+
assert_(eq(np.arcsin(z), arcsin(zm)))
|
| 212 |
+
assert_(eq(np.arccos(z), arccos(zm)))
|
| 213 |
+
assert_(eq(np.arctan(z), arctan(zm)))
|
| 214 |
+
assert_(eq(np.arctan2(x, y), arctan2(xm, ym)))
|
| 215 |
+
assert_(eq(np.absolute(x), absolute(xm)))
|
| 216 |
+
assert_(eq(np.equal(x, y), equal(xm, ym)))
|
| 217 |
+
assert_(eq(np.not_equal(x, y), not_equal(xm, ym)))
|
| 218 |
+
assert_(eq(np.less(x, y), less(xm, ym)))
|
| 219 |
+
assert_(eq(np.greater(x, y), greater(xm, ym)))
|
| 220 |
+
assert_(eq(np.less_equal(x, y), less_equal(xm, ym)))
|
| 221 |
+
assert_(eq(np.greater_equal(x, y), greater_equal(xm, ym)))
|
| 222 |
+
assert_(eq(np.conjugate(x), conjugate(xm)))
|
| 223 |
+
assert_(eq(np.concatenate((x, y)), concatenate((xm, ym))))
|
| 224 |
+
assert_(eq(np.concatenate((x, y)), concatenate((x, y))))
|
| 225 |
+
assert_(eq(np.concatenate((x, y)), concatenate((xm, y))))
|
| 226 |
+
assert_(eq(np.concatenate((x, y, x)), concatenate((x, ym, x))))
|
| 227 |
+
|
| 228 |
+
def test_xtestCount(self):
|
| 229 |
+
# Test count
|
| 230 |
+
ott = array([0., 1., 2., 3.], mask=[1, 0, 0, 0])
|
| 231 |
+
assert_(count(ott).dtype.type is np.intp)
|
| 232 |
+
assert_equal(3, count(ott))
|
| 233 |
+
assert_equal(1, count(1))
|
| 234 |
+
assert_(eq(0, array(1, mask=[1])))
|
| 235 |
+
ott = ott.reshape((2, 2))
|
| 236 |
+
assert_(count(ott).dtype.type is np.intp)
|
| 237 |
+
assert_(isinstance(count(ott, 0), np.ndarray))
|
| 238 |
+
assert_(count(ott).dtype.type is np.intp)
|
| 239 |
+
assert_(eq(3, count(ott)))
|
| 240 |
+
assert_(getmask(count(ott, 0)) is nomask)
|
| 241 |
+
assert_(eq([1, 2], count(ott, 0)))
|
| 242 |
+
|
| 243 |
+
def test_testMinMax(self):
|
| 244 |
+
# Test minimum and maximum.
|
| 245 |
+
(x, y, a10, m1, m2, xm, ym, z, zm, xf, s) = self.d
|
| 246 |
+
xr = np.ravel(x) # max doesn't work if shaped
|
| 247 |
+
xmr = ravel(xm)
|
| 248 |
+
|
| 249 |
+
# true because of careful selection of data
|
| 250 |
+
assert_(eq(max(xr), maximum.reduce(xmr)))
|
| 251 |
+
assert_(eq(min(xr), minimum.reduce(xmr)))
|
| 252 |
+
|
| 253 |
+
def test_testAddSumProd(self):
|
| 254 |
+
# Test add, sum, product.
|
| 255 |
+
(x, y, a10, m1, m2, xm, ym, z, zm, xf, s) = self.d
|
| 256 |
+
assert_(eq(np.add.reduce(x), add.reduce(x)))
|
| 257 |
+
assert_(eq(np.add.accumulate(x), add.accumulate(x)))
|
| 258 |
+
assert_(eq(4, sum(array(4), axis=0)))
|
| 259 |
+
assert_(eq(4, sum(array(4), axis=0)))
|
| 260 |
+
assert_(eq(np.sum(x, axis=0), sum(x, axis=0)))
|
| 261 |
+
assert_(eq(np.sum(filled(xm, 0), axis=0), sum(xm, axis=0)))
|
| 262 |
+
assert_(eq(np.sum(x, 0), sum(x, 0)))
|
| 263 |
+
assert_(eq(np.prod(x, axis=0), product(x, axis=0)))
|
| 264 |
+
assert_(eq(np.prod(x, 0), product(x, 0)))
|
| 265 |
+
assert_(eq(np.prod(filled(xm, 1), axis=0),
|
| 266 |
+
product(xm, axis=0)))
|
| 267 |
+
if len(s) > 1:
|
| 268 |
+
assert_(eq(np.concatenate((x, y), 1),
|
| 269 |
+
concatenate((xm, ym), 1)))
|
| 270 |
+
assert_(eq(np.add.reduce(x, 1), add.reduce(x, 1)))
|
| 271 |
+
assert_(eq(np.sum(x, 1), sum(x, 1)))
|
| 272 |
+
assert_(eq(np.prod(x, 1), product(x, 1)))
|
| 273 |
+
|
| 274 |
+
def test_testCI(self):
|
| 275 |
+
# Test of conversions and indexing
|
| 276 |
+
x1 = np.array([1, 2, 4, 3])
|
| 277 |
+
x2 = array(x1, mask=[1, 0, 0, 0])
|
| 278 |
+
x3 = array(x1, mask=[0, 1, 0, 1])
|
| 279 |
+
x4 = array(x1)
|
| 280 |
+
# test conversion to strings
|
| 281 |
+
str(x2) # raises?
|
| 282 |
+
repr(x2) # raises?
|
| 283 |
+
assert_(eq(np.sort(x1), sort(x2, fill_value=0)))
|
| 284 |
+
# tests of indexing
|
| 285 |
+
assert_(type(x2[1]) is type(x1[1]))
|
| 286 |
+
assert_(x1[1] == x2[1])
|
| 287 |
+
assert_(x2[0] is masked)
|
| 288 |
+
assert_(eq(x1[2], x2[2]))
|
| 289 |
+
assert_(eq(x1[2:5], x2[2:5]))
|
| 290 |
+
assert_(eq(x1[:], x2[:]))
|
| 291 |
+
assert_(eq(x1[1:], x3[1:]))
|
| 292 |
+
x1[2] = 9
|
| 293 |
+
x2[2] = 9
|
| 294 |
+
assert_(eq(x1, x2))
|
| 295 |
+
x1[1:3] = 99
|
| 296 |
+
x2[1:3] = 99
|
| 297 |
+
assert_(eq(x1, x2))
|
| 298 |
+
x2[1] = masked
|
| 299 |
+
assert_(eq(x1, x2))
|
| 300 |
+
x2[1:3] = masked
|
| 301 |
+
assert_(eq(x1, x2))
|
| 302 |
+
x2[:] = x1
|
| 303 |
+
x2[1] = masked
|
| 304 |
+
assert_(allequal(getmask(x2), array([0, 1, 0, 0])))
|
| 305 |
+
x3[:] = masked_array([1, 2, 3, 4], [0, 1, 1, 0])
|
| 306 |
+
assert_(allequal(getmask(x3), array([0, 1, 1, 0])))
|
| 307 |
+
x4[:] = masked_array([1, 2, 3, 4], [0, 1, 1, 0])
|
| 308 |
+
assert_(allequal(getmask(x4), array([0, 1, 1, 0])))
|
| 309 |
+
assert_(allequal(x4, array([1, 2, 3, 4])))
|
| 310 |
+
x1 = np.arange(5) * 1.0
|
| 311 |
+
x2 = masked_values(x1, 3.0)
|
| 312 |
+
assert_(eq(x1, x2))
|
| 313 |
+
assert_(allequal(array([0, 0, 0, 1, 0], MaskType), x2.mask))
|
| 314 |
+
assert_(eq(3.0, x2.fill_value))
|
| 315 |
+
x1 = array([1, 'hello', 2, 3], object)
|
| 316 |
+
x2 = np.array([1, 'hello', 2, 3], object)
|
| 317 |
+
s1 = x1[1]
|
| 318 |
+
s2 = x2[1]
|
| 319 |
+
assert_equal(type(s2), str)
|
| 320 |
+
assert_equal(type(s1), str)
|
| 321 |
+
assert_equal(s1, s2)
|
| 322 |
+
assert_(x1[1:1].shape == (0,))
|
| 323 |
+
|
| 324 |
+
def test_testCopySize(self):
|
| 325 |
+
# Tests of some subtle points of copying and sizing.
|
| 326 |
+
n = [0, 0, 1, 0, 0]
|
| 327 |
+
m = make_mask(n)
|
| 328 |
+
m2 = make_mask(m)
|
| 329 |
+
assert_(m is m2)
|
| 330 |
+
m3 = make_mask(m, copy=True)
|
| 331 |
+
assert_(m is not m3)
|
| 332 |
+
|
| 333 |
+
x1 = np.arange(5)
|
| 334 |
+
y1 = array(x1, mask=m)
|
| 335 |
+
assert_(y1._data is not x1)
|
| 336 |
+
assert_(allequal(x1, y1._data))
|
| 337 |
+
assert_(y1._mask is m)
|
| 338 |
+
|
| 339 |
+
y1a = array(y1, copy=0)
|
| 340 |
+
# For copy=False, one might expect that the array would just
|
| 341 |
+
# passed on, i.e., that it would be "is" instead of "==".
|
| 342 |
+
# See gh-4043 for discussion.
|
| 343 |
+
assert_(y1a._mask.__array_interface__ ==
|
| 344 |
+
y1._mask.__array_interface__)
|
| 345 |
+
|
| 346 |
+
y2 = array(x1, mask=m3, copy=0)
|
| 347 |
+
assert_(y2._mask is m3)
|
| 348 |
+
assert_(y2[2] is masked)
|
| 349 |
+
y2[2] = 9
|
| 350 |
+
assert_(y2[2] is not masked)
|
| 351 |
+
assert_(y2._mask is m3)
|
| 352 |
+
assert_(allequal(y2.mask, 0))
|
| 353 |
+
|
| 354 |
+
y2a = array(x1, mask=m, copy=1)
|
| 355 |
+
assert_(y2a._mask is not m)
|
| 356 |
+
assert_(y2a[2] is masked)
|
| 357 |
+
y2a[2] = 9
|
| 358 |
+
assert_(y2a[2] is not masked)
|
| 359 |
+
assert_(y2a._mask is not m)
|
| 360 |
+
assert_(allequal(y2a.mask, 0))
|
| 361 |
+
|
| 362 |
+
y3 = array(x1 * 1.0, mask=m)
|
| 363 |
+
assert_(filled(y3).dtype is (x1 * 1.0).dtype)
|
| 364 |
+
|
| 365 |
+
x4 = arange(4)
|
| 366 |
+
x4[2] = masked
|
| 367 |
+
y4 = resize(x4, (8,))
|
| 368 |
+
assert_(eq(concatenate([x4, x4]), y4))
|
| 369 |
+
assert_(eq(getmask(y4), [0, 0, 1, 0, 0, 0, 1, 0]))
|
| 370 |
+
y5 = repeat(x4, (2, 2, 2, 2), axis=0)
|
| 371 |
+
assert_(eq(y5, [0, 0, 1, 1, 2, 2, 3, 3]))
|
| 372 |
+
y6 = repeat(x4, 2, axis=0)
|
| 373 |
+
assert_(eq(y5, y6))
|
| 374 |
+
|
| 375 |
+
def test_testPut(self):
|
| 376 |
+
# Test of put
|
| 377 |
+
d = arange(5)
|
| 378 |
+
n = [0, 0, 0, 1, 1]
|
| 379 |
+
m = make_mask(n)
|
| 380 |
+
m2 = m.copy()
|
| 381 |
+
x = array(d, mask=m)
|
| 382 |
+
assert_(x[3] is masked)
|
| 383 |
+
assert_(x[4] is masked)
|
| 384 |
+
x[[1, 4]] = [10, 40]
|
| 385 |
+
assert_(x._mask is m)
|
| 386 |
+
assert_(x[3] is masked)
|
| 387 |
+
assert_(x[4] is not masked)
|
| 388 |
+
assert_(eq(x, [0, 10, 2, -1, 40]))
|
| 389 |
+
|
| 390 |
+
x = array(d, mask=m2, copy=True)
|
| 391 |
+
x.put([0, 1, 2], [-1, 100, 200])
|
| 392 |
+
assert_(x._mask is not m2)
|
| 393 |
+
assert_(x[3] is masked)
|
| 394 |
+
assert_(x[4] is masked)
|
| 395 |
+
assert_(eq(x, [-1, 100, 200, 0, 0]))
|
| 396 |
+
|
| 397 |
+
def test_testPut2(self):
|
| 398 |
+
# Test of put
|
| 399 |
+
d = arange(5)
|
| 400 |
+
x = array(d, mask=[0, 0, 0, 0, 0])
|
| 401 |
+
z = array([10, 40], mask=[1, 0])
|
| 402 |
+
assert_(x[2] is not masked)
|
| 403 |
+
assert_(x[3] is not masked)
|
| 404 |
+
x[2:4] = z
|
| 405 |
+
assert_(x[2] is masked)
|
| 406 |
+
assert_(x[3] is not masked)
|
| 407 |
+
assert_(eq(x, [0, 1, 10, 40, 4]))
|
| 408 |
+
|
| 409 |
+
d = arange(5)
|
| 410 |
+
x = array(d, mask=[0, 0, 0, 0, 0])
|
| 411 |
+
y = x[2:4]
|
| 412 |
+
z = array([10, 40], mask=[1, 0])
|
| 413 |
+
assert_(x[2] is not masked)
|
| 414 |
+
assert_(x[3] is not masked)
|
| 415 |
+
y[:] = z
|
| 416 |
+
assert_(y[0] is masked)
|
| 417 |
+
assert_(y[1] is not masked)
|
| 418 |
+
assert_(eq(y, [10, 40]))
|
| 419 |
+
assert_(x[2] is masked)
|
| 420 |
+
assert_(x[3] is not masked)
|
| 421 |
+
assert_(eq(x, [0, 1, 10, 40, 4]))
|
| 422 |
+
|
| 423 |
+
def test_testMaPut(self):
|
| 424 |
+
(x, y, a10, m1, m2, xm, ym, z, zm, xf, s) = self.d
|
| 425 |
+
m = [1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1]
|
| 426 |
+
i = np.nonzero(m)[0]
|
| 427 |
+
put(ym, i, zm)
|
| 428 |
+
assert_(all(take(ym, i, axis=0) == zm))
|
| 429 |
+
|
| 430 |
+
def test_testOddFeatures(self):
|
| 431 |
+
# Test of other odd features
|
| 432 |
+
x = arange(20)
|
| 433 |
+
x = x.reshape(4, 5)
|
| 434 |
+
x.flat[5] = 12
|
| 435 |
+
assert_(x[1, 0] == 12)
|
| 436 |
+
z = x + 10j * x
|
| 437 |
+
assert_(eq(z.real, x))
|
| 438 |
+
assert_(eq(z.imag, 10 * x))
|
| 439 |
+
assert_(eq((z * conjugate(z)).real, 101 * x * x))
|
| 440 |
+
z.imag[...] = 0.0
|
| 441 |
+
|
| 442 |
+
x = arange(10)
|
| 443 |
+
x[3] = masked
|
| 444 |
+
assert_(str(x[3]) == str(masked))
|
| 445 |
+
c = x >= 8
|
| 446 |
+
assert_(count(where(c, masked, masked)) == 0)
|
| 447 |
+
assert_(shape(where(c, masked, masked)) == c.shape)
|
| 448 |
+
z = where(c, x, masked)
|
| 449 |
+
assert_(z.dtype is x.dtype)
|
| 450 |
+
assert_(z[3] is masked)
|
| 451 |
+
assert_(z[4] is masked)
|
| 452 |
+
assert_(z[7] is masked)
|
| 453 |
+
assert_(z[8] is not masked)
|
| 454 |
+
assert_(z[9] is not masked)
|
| 455 |
+
assert_(eq(x, z))
|
| 456 |
+
z = where(c, masked, x)
|
| 457 |
+
assert_(z.dtype is x.dtype)
|
| 458 |
+
assert_(z[3] is masked)
|
| 459 |
+
assert_(z[4] is not masked)
|
| 460 |
+
assert_(z[7] is not masked)
|
| 461 |
+
assert_(z[8] is masked)
|
| 462 |
+
assert_(z[9] is masked)
|
| 463 |
+
z = masked_where(c, x)
|
| 464 |
+
assert_(z.dtype is x.dtype)
|
| 465 |
+
assert_(z[3] is masked)
|
| 466 |
+
assert_(z[4] is not masked)
|
| 467 |
+
assert_(z[7] is not masked)
|
| 468 |
+
assert_(z[8] is masked)
|
| 469 |
+
assert_(z[9] is masked)
|
| 470 |
+
assert_(eq(x, z))
|
| 471 |
+
x = array([1., 2., 3., 4., 5.])
|
| 472 |
+
c = array([1, 1, 1, 0, 0])
|
| 473 |
+
x[2] = masked
|
| 474 |
+
z = where(c, x, -x)
|
| 475 |
+
assert_(eq(z, [1., 2., 0., -4., -5]))
|
| 476 |
+
c[0] = masked
|
| 477 |
+
z = where(c, x, -x)
|
| 478 |
+
assert_(eq(z, [1., 2., 0., -4., -5]))
|
| 479 |
+
assert_(z[0] is masked)
|
| 480 |
+
assert_(z[1] is not masked)
|
| 481 |
+
assert_(z[2] is masked)
|
| 482 |
+
assert_(eq(masked_where(greater(x, 2), x), masked_greater(x, 2)))
|
| 483 |
+
assert_(eq(masked_where(greater_equal(x, 2), x),
|
| 484 |
+
masked_greater_equal(x, 2)))
|
| 485 |
+
assert_(eq(masked_where(less(x, 2), x), masked_less(x, 2)))
|
| 486 |
+
assert_(eq(masked_where(less_equal(x, 2), x), masked_less_equal(x, 2)))
|
| 487 |
+
assert_(eq(masked_where(not_equal(x, 2), x), masked_not_equal(x, 2)))
|
| 488 |
+
assert_(eq(masked_where(equal(x, 2), x), masked_equal(x, 2)))
|
| 489 |
+
assert_(eq(masked_where(not_equal(x, 2), x), masked_not_equal(x, 2)))
|
| 490 |
+
assert_(eq(masked_inside(list(range(5)), 1, 3), [0, 199, 199, 199, 4]))
|
| 491 |
+
assert_(eq(masked_outside(list(range(5)), 1, 3), [199, 1, 2, 3, 199]))
|
| 492 |
+
assert_(eq(masked_inside(array(list(range(5)),
|
| 493 |
+
mask=[1, 0, 0, 0, 0]), 1, 3).mask,
|
| 494 |
+
[1, 1, 1, 1, 0]))
|
| 495 |
+
assert_(eq(masked_outside(array(list(range(5)),
|
| 496 |
+
mask=[0, 1, 0, 0, 0]), 1, 3).mask,
|
| 497 |
+
[1, 1, 0, 0, 1]))
|
| 498 |
+
assert_(eq(masked_equal(array(list(range(5)),
|
| 499 |
+
mask=[1, 0, 0, 0, 0]), 2).mask,
|
| 500 |
+
[1, 0, 1, 0, 0]))
|
| 501 |
+
assert_(eq(masked_not_equal(array([2, 2, 1, 2, 1],
|
| 502 |
+
mask=[1, 0, 0, 0, 0]), 2).mask,
|
| 503 |
+
[1, 0, 1, 0, 1]))
|
| 504 |
+
assert_(eq(masked_where([1, 1, 0, 0, 0], [1, 2, 3, 4, 5]),
|
| 505 |
+
[99, 99, 3, 4, 5]))
|
| 506 |
+
atest = ones((10, 10, 10), dtype=np.float32)
|
| 507 |
+
btest = zeros(atest.shape, MaskType)
|
| 508 |
+
ctest = masked_where(btest, atest)
|
| 509 |
+
assert_(eq(atest, ctest))
|
| 510 |
+
z = choose(c, (-x, x))
|
| 511 |
+
assert_(eq(z, [1., 2., 0., -4., -5]))
|
| 512 |
+
assert_(z[0] is masked)
|
| 513 |
+
assert_(z[1] is not masked)
|
| 514 |
+
assert_(z[2] is masked)
|
| 515 |
+
x = arange(6)
|
| 516 |
+
x[5] = masked
|
| 517 |
+
y = arange(6) * 10
|
| 518 |
+
y[2] = masked
|
| 519 |
+
c = array([1, 1, 1, 0, 0, 0], mask=[1, 0, 0, 0, 0, 0])
|
| 520 |
+
cm = c.filled(1)
|
| 521 |
+
z = where(c, x, y)
|
| 522 |
+
zm = where(cm, x, y)
|
| 523 |
+
assert_(eq(z, zm))
|
| 524 |
+
assert_(getmask(zm) is nomask)
|
| 525 |
+
assert_(eq(zm, [0, 1, 2, 30, 40, 50]))
|
| 526 |
+
z = where(c, masked, 1)
|
| 527 |
+
assert_(eq(z, [99, 99, 99, 1, 1, 1]))
|
| 528 |
+
z = where(c, 1, masked)
|
| 529 |
+
assert_(eq(z, [99, 1, 1, 99, 99, 99]))
|
| 530 |
+
|
| 531 |
+
def test_testMinMax2(self):
|
| 532 |
+
# Test of minimum, maximum.
|
| 533 |
+
assert_(eq(minimum([1, 2, 3], [4, 0, 9]), [1, 0, 3]))
|
| 534 |
+
assert_(eq(maximum([1, 2, 3], [4, 0, 9]), [4, 2, 9]))
|
| 535 |
+
x = arange(5)
|
| 536 |
+
y = arange(5) - 2
|
| 537 |
+
x[3] = masked
|
| 538 |
+
y[0] = masked
|
| 539 |
+
assert_(eq(minimum(x, y), where(less(x, y), x, y)))
|
| 540 |
+
assert_(eq(maximum(x, y), where(greater(x, y), x, y)))
|
| 541 |
+
assert_(minimum.reduce(x) == 0)
|
| 542 |
+
assert_(maximum.reduce(x) == 4)
|
| 543 |
+
|
| 544 |
+
def test_testTakeTransposeInnerOuter(self):
|
| 545 |
+
# Test of take, transpose, inner, outer products
|
| 546 |
+
x = arange(24)
|
| 547 |
+
y = np.arange(24)
|
| 548 |
+
x[5:6] = masked
|
| 549 |
+
x = x.reshape(2, 3, 4)
|
| 550 |
+
y = y.reshape(2, 3, 4)
|
| 551 |
+
assert_(eq(np.transpose(y, (2, 0, 1)), transpose(x, (2, 0, 1))))
|
| 552 |
+
assert_(eq(np.take(y, (2, 0, 1), 1), take(x, (2, 0, 1), 1)))
|
| 553 |
+
assert_(eq(np.inner(filled(x, 0), filled(y, 0)),
|
| 554 |
+
inner(x, y)))
|
| 555 |
+
assert_(eq(np.outer(filled(x, 0), filled(y, 0)),
|
| 556 |
+
outer(x, y)))
|
| 557 |
+
y = array(['abc', 1, 'def', 2, 3], object)
|
| 558 |
+
y[2] = masked
|
| 559 |
+
t = take(y, [0, 3, 4])
|
| 560 |
+
assert_(t[0] == 'abc')
|
| 561 |
+
assert_(t[1] == 2)
|
| 562 |
+
assert_(t[2] == 3)
|
| 563 |
+
|
| 564 |
+
def test_testInplace(self):
|
| 565 |
+
# Test of inplace operations and rich comparisons
|
| 566 |
+
y = arange(10)
|
| 567 |
+
|
| 568 |
+
x = arange(10)
|
| 569 |
+
xm = arange(10)
|
| 570 |
+
xm[2] = masked
|
| 571 |
+
x += 1
|
| 572 |
+
assert_(eq(x, y + 1))
|
| 573 |
+
xm += 1
|
| 574 |
+
assert_(eq(x, y + 1))
|
| 575 |
+
|
| 576 |
+
x = arange(10)
|
| 577 |
+
xm = arange(10)
|
| 578 |
+
xm[2] = masked
|
| 579 |
+
x -= 1
|
| 580 |
+
assert_(eq(x, y - 1))
|
| 581 |
+
xm -= 1
|
| 582 |
+
assert_(eq(xm, y - 1))
|
| 583 |
+
|
| 584 |
+
x = arange(10) * 1.0
|
| 585 |
+
xm = arange(10) * 1.0
|
| 586 |
+
xm[2] = masked
|
| 587 |
+
x *= 2.0
|
| 588 |
+
assert_(eq(x, y * 2))
|
| 589 |
+
xm *= 2.0
|
| 590 |
+
assert_(eq(xm, y * 2))
|
| 591 |
+
|
| 592 |
+
x = arange(10) * 2
|
| 593 |
+
xm = arange(10)
|
| 594 |
+
xm[2] = masked
|
| 595 |
+
x //= 2
|
| 596 |
+
assert_(eq(x, y))
|
| 597 |
+
xm //= 2
|
| 598 |
+
assert_(eq(x, y))
|
| 599 |
+
|
| 600 |
+
x = arange(10) * 1.0
|
| 601 |
+
xm = arange(10) * 1.0
|
| 602 |
+
xm[2] = masked
|
| 603 |
+
x /= 2.0
|
| 604 |
+
assert_(eq(x, y / 2.0))
|
| 605 |
+
xm /= arange(10)
|
| 606 |
+
assert_(eq(xm, ones((10,))))
|
| 607 |
+
|
| 608 |
+
x = arange(10).astype(np.float32)
|
| 609 |
+
xm = arange(10)
|
| 610 |
+
xm[2] = masked
|
| 611 |
+
x += 1.
|
| 612 |
+
assert_(eq(x, y + 1.))
|
| 613 |
+
|
| 614 |
+
def test_testPickle(self):
|
| 615 |
+
# Test of pickling
|
| 616 |
+
x = arange(12)
|
| 617 |
+
x[4:10:2] = masked
|
| 618 |
+
x = x.reshape(4, 3)
|
| 619 |
+
for proto in range(2, pickle.HIGHEST_PROTOCOL + 1):
|
| 620 |
+
s = pickle.dumps(x, protocol=proto)
|
| 621 |
+
y = pickle.loads(s)
|
| 622 |
+
assert_(eq(x, y))
|
| 623 |
+
|
| 624 |
+
def test_testMasked(self):
|
| 625 |
+
# Test of masked element
|
| 626 |
+
xx = arange(6)
|
| 627 |
+
xx[1] = masked
|
| 628 |
+
assert_(str(masked) == '--')
|
| 629 |
+
assert_(xx[1] is masked)
|
| 630 |
+
assert_equal(filled(xx[1], 0), 0)
|
| 631 |
+
|
| 632 |
+
def test_testAverage1(self):
|
| 633 |
+
# Test of average.
|
| 634 |
+
ott = array([0., 1., 2., 3.], mask=[1, 0, 0, 0])
|
| 635 |
+
assert_(eq(2.0, average(ott, axis=0)))
|
| 636 |
+
assert_(eq(2.0, average(ott, weights=[1., 1., 2., 1.])))
|
| 637 |
+
result, wts = average(ott, weights=[1., 1., 2., 1.], returned=True)
|
| 638 |
+
assert_(eq(2.0, result))
|
| 639 |
+
assert_(wts == 4.0)
|
| 640 |
+
ott[:] = masked
|
| 641 |
+
assert_(average(ott, axis=0) is masked)
|
| 642 |
+
ott = array([0., 1., 2., 3.], mask=[1, 0, 0, 0])
|
| 643 |
+
ott = ott.reshape(2, 2)
|
| 644 |
+
ott[:, 1] = masked
|
| 645 |
+
assert_(eq(average(ott, axis=0), [2.0, 0.0]))
|
| 646 |
+
assert_(average(ott, axis=1)[0] is masked)
|
| 647 |
+
assert_(eq([2., 0.], average(ott, axis=0)))
|
| 648 |
+
result, wts = average(ott, axis=0, returned=True)
|
| 649 |
+
assert_(eq(wts, [1., 0.]))
|
| 650 |
+
|
| 651 |
+
def test_testAverage2(self):
|
| 652 |
+
# More tests of average.
|
| 653 |
+
w1 = [0, 1, 1, 1, 1, 0]
|
| 654 |
+
w2 = [[0, 1, 1, 1, 1, 0], [1, 0, 0, 0, 0, 1]]
|
| 655 |
+
x = arange(6)
|
| 656 |
+
assert_(allclose(average(x, axis=0), 2.5))
|
| 657 |
+
assert_(allclose(average(x, axis=0, weights=w1), 2.5))
|
| 658 |
+
y = array([arange(6), 2.0 * arange(6)])
|
| 659 |
+
assert_(allclose(average(y, None),
|
| 660 |
+
np.add.reduce(np.arange(6)) * 3. / 12.))
|
| 661 |
+
assert_(allclose(average(y, axis=0), np.arange(6) * 3. / 2.))
|
| 662 |
+
assert_(allclose(average(y, axis=1),
|
| 663 |
+
[average(x, axis=0), average(x, axis=0) * 2.0]))
|
| 664 |
+
assert_(allclose(average(y, None, weights=w2), 20. / 6.))
|
| 665 |
+
assert_(allclose(average(y, axis=0, weights=w2),
|
| 666 |
+
[0., 1., 2., 3., 4., 10.]))
|
| 667 |
+
assert_(allclose(average(y, axis=1),
|
| 668 |
+
[average(x, axis=0), average(x, axis=0) * 2.0]))
|
| 669 |
+
m1 = zeros(6)
|
| 670 |
+
m2 = [0, 0, 1, 1, 0, 0]
|
| 671 |
+
m3 = [[0, 0, 1, 1, 0, 0], [0, 1, 1, 1, 1, 0]]
|
| 672 |
+
m4 = ones(6)
|
| 673 |
+
m5 = [0, 1, 1, 1, 1, 1]
|
| 674 |
+
assert_(allclose(average(masked_array(x, m1), axis=0), 2.5))
|
| 675 |
+
assert_(allclose(average(masked_array(x, m2), axis=0), 2.5))
|
| 676 |
+
assert_(average(masked_array(x, m4), axis=0) is masked)
|
| 677 |
+
assert_equal(average(masked_array(x, m5), axis=0), 0.0)
|
| 678 |
+
assert_equal(count(average(masked_array(x, m4), axis=0)), 0)
|
| 679 |
+
z = masked_array(y, m3)
|
| 680 |
+
assert_(allclose(average(z, None), 20. / 6.))
|
| 681 |
+
assert_(allclose(average(z, axis=0),
|
| 682 |
+
[0., 1., 99., 99., 4.0, 7.5]))
|
| 683 |
+
assert_(allclose(average(z, axis=1), [2.5, 5.0]))
|
| 684 |
+
assert_(allclose(average(z, axis=0, weights=w2),
|
| 685 |
+
[0., 1., 99., 99., 4.0, 10.0]))
|
| 686 |
+
|
| 687 |
+
a = arange(6)
|
| 688 |
+
b = arange(6) * 3
|
| 689 |
+
r1, w1 = average([[a, b], [b, a]], axis=1, returned=True)
|
| 690 |
+
assert_equal(shape(r1), shape(w1))
|
| 691 |
+
assert_equal(r1.shape, w1.shape)
|
| 692 |
+
r2, w2 = average(ones((2, 2, 3)), axis=0, weights=[3, 1], returned=True)
|
| 693 |
+
assert_equal(shape(w2), shape(r2))
|
| 694 |
+
r2, w2 = average(ones((2, 2, 3)), returned=True)
|
| 695 |
+
assert_equal(shape(w2), shape(r2))
|
| 696 |
+
r2, w2 = average(ones((2, 2, 3)), weights=ones((2, 2, 3)), returned=True)
|
| 697 |
+
assert_(shape(w2) == shape(r2))
|
| 698 |
+
a2d = array([[1, 2], [0, 4]], float)
|
| 699 |
+
a2dm = masked_array(a2d, [[0, 0], [1, 0]])
|
| 700 |
+
a2da = average(a2d, axis=0)
|
| 701 |
+
assert_(eq(a2da, [0.5, 3.0]))
|
| 702 |
+
a2dma = average(a2dm, axis=0)
|
| 703 |
+
assert_(eq(a2dma, [1.0, 3.0]))
|
| 704 |
+
a2dma = average(a2dm, axis=None)
|
| 705 |
+
assert_(eq(a2dma, 7. / 3.))
|
| 706 |
+
a2dma = average(a2dm, axis=1)
|
| 707 |
+
assert_(eq(a2dma, [1.5, 4.0]))
|
| 708 |
+
|
| 709 |
+
def test_testToPython(self):
|
| 710 |
+
assert_equal(1, int(array(1)))
|
| 711 |
+
assert_equal(1.0, float(array(1)))
|
| 712 |
+
assert_equal(1, int(array([[[1]]])))
|
| 713 |
+
assert_equal(1.0, float(array([[1]])))
|
| 714 |
+
assert_raises(TypeError, float, array([1, 1]))
|
| 715 |
+
assert_raises(ValueError, bool, array([0, 1]))
|
| 716 |
+
assert_raises(ValueError, bool, array([0, 0], mask=[0, 1]))
|
| 717 |
+
|
| 718 |
+
def test_testScalarArithmetic(self):
|
| 719 |
+
xm = array(0, mask=1)
|
| 720 |
+
# TODO FIXME: Find out what the following raises a warning in r8247
|
| 721 |
+
with np.errstate(divide='ignore'):
|
| 722 |
+
assert_((1 / array(0)).mask)
|
| 723 |
+
assert_((1 + xm).mask)
|
| 724 |
+
assert_((-xm).mask)
|
| 725 |
+
assert_((-xm).mask)
|
| 726 |
+
assert_(maximum(xm, xm).mask)
|
| 727 |
+
assert_(minimum(xm, xm).mask)
|
| 728 |
+
assert_(xm.filled().dtype is xm._data.dtype)
|
| 729 |
+
x = array(0, mask=0)
|
| 730 |
+
assert_(x.filled() == x._data)
|
| 731 |
+
assert_equal(str(xm), str(masked_print_option))
|
| 732 |
+
|
| 733 |
+
def test_testArrayMethods(self):
|
| 734 |
+
a = array([1, 3, 2])
|
| 735 |
+
assert_(eq(a.any(), a._data.any()))
|
| 736 |
+
assert_(eq(a.all(), a._data.all()))
|
| 737 |
+
assert_(eq(a.argmax(), a._data.argmax()))
|
| 738 |
+
assert_(eq(a.argmin(), a._data.argmin()))
|
| 739 |
+
assert_(eq(a.choose(0, 1, 2, 3, 4),
|
| 740 |
+
a._data.choose(0, 1, 2, 3, 4)))
|
| 741 |
+
assert_(eq(a.compress([1, 0, 1]), a._data.compress([1, 0, 1])))
|
| 742 |
+
assert_(eq(a.conj(), a._data.conj()))
|
| 743 |
+
assert_(eq(a.conjugate(), a._data.conjugate()))
|
| 744 |
+
m = array([[1, 2], [3, 4]])
|
| 745 |
+
assert_(eq(m.diagonal(), m._data.diagonal()))
|
| 746 |
+
assert_(eq(a.sum(), a._data.sum()))
|
| 747 |
+
assert_(eq(a.take([1, 2]), a._data.take([1, 2])))
|
| 748 |
+
assert_(eq(m.transpose(), m._data.transpose()))
|
| 749 |
+
|
| 750 |
+
def test_testArrayAttributes(self):
|
| 751 |
+
a = array([1, 3, 2])
|
| 752 |
+
assert_equal(a.ndim, 1)
|
| 753 |
+
|
| 754 |
+
def test_testAPI(self):
|
| 755 |
+
assert_(not [m for m in dir(np.ndarray)
|
| 756 |
+
if m not in dir(MaskedArray) and
|
| 757 |
+
not m.startswith('_')])
|
| 758 |
+
|
| 759 |
+
def test_testSingleElementSubscript(self):
|
| 760 |
+
a = array([1, 3, 2])
|
| 761 |
+
b = array([1, 3, 2], mask=[1, 0, 1])
|
| 762 |
+
assert_equal(a[0].shape, ())
|
| 763 |
+
assert_equal(b[0].shape, ())
|
| 764 |
+
assert_equal(b[1].shape, ())
|
| 765 |
+
|
| 766 |
+
def test_assignment_by_condition(self):
|
| 767 |
+
# Test for gh-18951
|
| 768 |
+
a = array([1, 2, 3, 4], mask=[1, 0, 1, 0])
|
| 769 |
+
c = a >= 3
|
| 770 |
+
a[c] = 5
|
| 771 |
+
assert_(a[2] is masked)
|
| 772 |
+
|
| 773 |
+
def test_assignment_by_condition_2(self):
|
| 774 |
+
# gh-19721
|
| 775 |
+
a = masked_array([0, 1], mask=[False, False])
|
| 776 |
+
b = masked_array([0, 1], mask=[True, True])
|
| 777 |
+
mask = a < 1
|
| 778 |
+
b[mask] = a[mask]
|
| 779 |
+
expected_mask = [False, True]
|
| 780 |
+
assert_equal(b.mask, expected_mask)
|
| 781 |
+
|
| 782 |
+
|
| 783 |
+
class TestUfuncs:
|
| 784 |
+
def setup_method(self):
|
| 785 |
+
self.d = (array([1.0, 0, -1, pi / 2] * 2, mask=[0, 1] + [0] * 6),
|
| 786 |
+
array([1.0, 0, -1, pi / 2] * 2, mask=[1, 0] + [0] * 6),)
|
| 787 |
+
|
| 788 |
+
def test_testUfuncRegression(self):
|
| 789 |
+
f_invalid_ignore = [
|
| 790 |
+
'sqrt', 'arctanh', 'arcsin', 'arccos',
|
| 791 |
+
'arccosh', 'arctanh', 'log', 'log10', 'divide',
|
| 792 |
+
'true_divide', 'floor_divide', 'remainder', 'fmod']
|
| 793 |
+
for f in ['sqrt', 'log', 'log10', 'exp', 'conjugate',
|
| 794 |
+
'sin', 'cos', 'tan',
|
| 795 |
+
'arcsin', 'arccos', 'arctan',
|
| 796 |
+
'sinh', 'cosh', 'tanh',
|
| 797 |
+
'arcsinh',
|
| 798 |
+
'arccosh',
|
| 799 |
+
'arctanh',
|
| 800 |
+
'absolute', 'fabs', 'negative',
|
| 801 |
+
'floor', 'ceil',
|
| 802 |
+
'logical_not',
|
| 803 |
+
'add', 'subtract', 'multiply',
|
| 804 |
+
'divide', 'true_divide', 'floor_divide',
|
| 805 |
+
'remainder', 'fmod', 'hypot', 'arctan2',
|
| 806 |
+
'equal', 'not_equal', 'less_equal', 'greater_equal',
|
| 807 |
+
'less', 'greater',
|
| 808 |
+
'logical_and', 'logical_or', 'logical_xor']:
|
| 809 |
+
try:
|
| 810 |
+
uf = getattr(umath, f)
|
| 811 |
+
except AttributeError:
|
| 812 |
+
uf = getattr(fromnumeric, f)
|
| 813 |
+
mf = getattr(np.ma, f)
|
| 814 |
+
args = self.d[:uf.nin]
|
| 815 |
+
with np.errstate():
|
| 816 |
+
if f in f_invalid_ignore:
|
| 817 |
+
np.seterr(invalid='ignore')
|
| 818 |
+
if f in ['arctanh', 'log', 'log10']:
|
| 819 |
+
np.seterr(divide='ignore')
|
| 820 |
+
ur = uf(*args)
|
| 821 |
+
mr = mf(*args)
|
| 822 |
+
assert_(eq(ur.filled(0), mr.filled(0), f))
|
| 823 |
+
assert_(eqmask(ur.mask, mr.mask))
|
| 824 |
+
|
| 825 |
+
def test_reduce(self):
|
| 826 |
+
a = self.d[0]
|
| 827 |
+
assert_(not alltrue(a, axis=0))
|
| 828 |
+
assert_(sometrue(a, axis=0))
|
| 829 |
+
assert_equal(sum(a[:3], axis=0), 0)
|
| 830 |
+
assert_equal(product(a, axis=0), 0)
|
| 831 |
+
|
| 832 |
+
def test_minmax(self):
|
| 833 |
+
a = arange(1, 13).reshape(3, 4)
|
| 834 |
+
amask = masked_where(a < 5, a)
|
| 835 |
+
assert_equal(amask.max(), a.max())
|
| 836 |
+
assert_equal(amask.min(), 5)
|
| 837 |
+
assert_((amask.max(0) == a.max(0)).all())
|
| 838 |
+
assert_((amask.min(0) == [5, 6, 7, 8]).all())
|
| 839 |
+
assert_(amask.max(1)[0].mask)
|
| 840 |
+
assert_(amask.min(1)[0].mask)
|
| 841 |
+
|
| 842 |
+
def test_nonzero(self):
|
| 843 |
+
for t in "?bhilqpBHILQPfdgFDGO":
|
| 844 |
+
x = array([1, 0, 2, 0], mask=[0, 0, 1, 1])
|
| 845 |
+
assert_(eq(nonzero(x), [0]))
|
| 846 |
+
|
| 847 |
+
|
| 848 |
+
class TestArrayMethods:
|
| 849 |
+
|
| 850 |
+
def setup_method(self):
|
| 851 |
+
x = np.array([8.375, 7.545, 8.828, 8.5, 1.757, 5.928,
|
| 852 |
+
8.43, 7.78, 9.865, 5.878, 8.979, 4.732,
|
| 853 |
+
3.012, 6.022, 5.095, 3.116, 5.238, 3.957,
|
| 854 |
+
6.04, 9.63, 7.712, 3.382, 4.489, 6.479,
|
| 855 |
+
7.189, 9.645, 5.395, 4.961, 9.894, 2.893,
|
| 856 |
+
7.357, 9.828, 6.272, 3.758, 6.693, 0.993])
|
| 857 |
+
X = x.reshape(6, 6)
|
| 858 |
+
XX = x.reshape(3, 2, 2, 3)
|
| 859 |
+
|
| 860 |
+
m = np.array([0, 1, 0, 1, 0, 0,
|
| 861 |
+
1, 0, 1, 1, 0, 1,
|
| 862 |
+
0, 0, 0, 1, 0, 1,
|
| 863 |
+
0, 0, 0, 1, 1, 1,
|
| 864 |
+
1, 0, 0, 1, 0, 0,
|
| 865 |
+
0, 0, 1, 0, 1, 0])
|
| 866 |
+
mx = array(data=x, mask=m)
|
| 867 |
+
mX = array(data=X, mask=m.reshape(X.shape))
|
| 868 |
+
mXX = array(data=XX, mask=m.reshape(XX.shape))
|
| 869 |
+
|
| 870 |
+
self.d = (x, X, XX, m, mx, mX, mXX)
|
| 871 |
+
|
| 872 |
+
def test_trace(self):
|
| 873 |
+
(x, X, XX, m, mx, mX, mXX,) = self.d
|
| 874 |
+
mXdiag = mX.diagonal()
|
| 875 |
+
assert_equal(mX.trace(), mX.diagonal().compressed().sum())
|
| 876 |
+
assert_(eq(mX.trace(),
|
| 877 |
+
X.trace() - sum(mXdiag.mask * X.diagonal(),
|
| 878 |
+
axis=0)))
|
| 879 |
+
|
| 880 |
+
def test_clip(self):
|
| 881 |
+
(x, X, XX, m, mx, mX, mXX,) = self.d
|
| 882 |
+
clipped = mx.clip(2, 8)
|
| 883 |
+
assert_(eq(clipped.mask, mx.mask))
|
| 884 |
+
assert_(eq(clipped._data, x.clip(2, 8)))
|
| 885 |
+
assert_(eq(clipped._data, mx._data.clip(2, 8)))
|
| 886 |
+
|
| 887 |
+
def test_ptp(self):
|
| 888 |
+
(x, X, XX, m, mx, mX, mXX,) = self.d
|
| 889 |
+
(n, m) = X.shape
|
| 890 |
+
# print(type(mx), mx.compressed())
|
| 891 |
+
# raise Exception()
|
| 892 |
+
assert_equal(mx.ptp(), np.ptp(mx.compressed()))
|
| 893 |
+
rows = np.zeros(n, np.float64)
|
| 894 |
+
cols = np.zeros(m, np.float64)
|
| 895 |
+
for k in range(m):
|
| 896 |
+
cols[k] = np.ptp(mX[:, k].compressed())
|
| 897 |
+
for k in range(n):
|
| 898 |
+
rows[k] = np.ptp(mX[k].compressed())
|
| 899 |
+
assert_(eq(mX.ptp(0), cols))
|
| 900 |
+
assert_(eq(mX.ptp(1), rows))
|
| 901 |
+
|
| 902 |
+
def test_swapaxes(self):
|
| 903 |
+
(x, X, XX, m, mx, mX, mXX,) = self.d
|
| 904 |
+
mXswapped = mX.swapaxes(0, 1)
|
| 905 |
+
assert_(eq(mXswapped[-1], mX[:, -1]))
|
| 906 |
+
mXXswapped = mXX.swapaxes(0, 2)
|
| 907 |
+
assert_equal(mXXswapped.shape, (2, 2, 3, 3))
|
| 908 |
+
|
| 909 |
+
def test_cumprod(self):
|
| 910 |
+
(x, X, XX, m, mx, mX, mXX,) = self.d
|
| 911 |
+
mXcp = mX.cumprod(0)
|
| 912 |
+
assert_(eq(mXcp._data, mX.filled(1).cumprod(0)))
|
| 913 |
+
mXcp = mX.cumprod(1)
|
| 914 |
+
assert_(eq(mXcp._data, mX.filled(1).cumprod(1)))
|
| 915 |
+
|
| 916 |
+
def test_cumsum(self):
|
| 917 |
+
(x, X, XX, m, mx, mX, mXX,) = self.d
|
| 918 |
+
mXcp = mX.cumsum(0)
|
| 919 |
+
assert_(eq(mXcp._data, mX.filled(0).cumsum(0)))
|
| 920 |
+
mXcp = mX.cumsum(1)
|
| 921 |
+
assert_(eq(mXcp._data, mX.filled(0).cumsum(1)))
|
| 922 |
+
|
| 923 |
+
def test_varstd(self):
|
| 924 |
+
(x, X, XX, m, mx, mX, mXX,) = self.d
|
| 925 |
+
assert_(eq(mX.var(axis=None), mX.compressed().var()))
|
| 926 |
+
assert_(eq(mX.std(axis=None), mX.compressed().std()))
|
| 927 |
+
assert_(eq(mXX.var(axis=3).shape, XX.var(axis=3).shape))
|
| 928 |
+
assert_(eq(mX.var().shape, X.var().shape))
|
| 929 |
+
(mXvar0, mXvar1) = (mX.var(axis=0), mX.var(axis=1))
|
| 930 |
+
for k in range(6):
|
| 931 |
+
assert_(eq(mXvar1[k], mX[k].compressed().var()))
|
| 932 |
+
assert_(eq(mXvar0[k], mX[:, k].compressed().var()))
|
| 933 |
+
assert_(eq(np.sqrt(mXvar0[k]),
|
| 934 |
+
mX[:, k].compressed().std()))
|
| 935 |
+
|
| 936 |
+
|
| 937 |
+
def eqmask(m1, m2):
|
| 938 |
+
if m1 is nomask:
|
| 939 |
+
return m2 is nomask
|
| 940 |
+
if m2 is nomask:
|
| 941 |
+
return m1 is nomask
|
| 942 |
+
return (m1 == m2).all()
|
venv/lib/python3.13/site-packages/numpy/ma/tests/test_regression.py
ADDED
|
@@ -0,0 +1,100 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
from numpy.testing import (
|
| 3 |
+
assert_,
|
| 4 |
+
assert_allclose,
|
| 5 |
+
assert_array_equal,
|
| 6 |
+
suppress_warnings,
|
| 7 |
+
)
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
class TestRegression:
|
| 11 |
+
def test_masked_array_create(self):
|
| 12 |
+
# Ticket #17
|
| 13 |
+
x = np.ma.masked_array([0, 1, 2, 3, 0, 4, 5, 6],
|
| 14 |
+
mask=[0, 0, 0, 1, 1, 1, 0, 0])
|
| 15 |
+
assert_array_equal(np.ma.nonzero(x), [[1, 2, 6, 7]])
|
| 16 |
+
|
| 17 |
+
def test_masked_array(self):
|
| 18 |
+
# Ticket #61
|
| 19 |
+
np.ma.array(1, mask=[1])
|
| 20 |
+
|
| 21 |
+
def test_mem_masked_where(self):
|
| 22 |
+
# Ticket #62
|
| 23 |
+
from numpy.ma import MaskType, masked_where
|
| 24 |
+
a = np.zeros((1, 1))
|
| 25 |
+
b = np.zeros(a.shape, MaskType)
|
| 26 |
+
c = masked_where(b, a)
|
| 27 |
+
a - c
|
| 28 |
+
|
| 29 |
+
def test_masked_array_multiply(self):
|
| 30 |
+
# Ticket #254
|
| 31 |
+
a = np.ma.zeros((4, 1))
|
| 32 |
+
a[2, 0] = np.ma.masked
|
| 33 |
+
b = np.zeros((4, 2))
|
| 34 |
+
a * b
|
| 35 |
+
b * a
|
| 36 |
+
|
| 37 |
+
def test_masked_array_repeat(self):
|
| 38 |
+
# Ticket #271
|
| 39 |
+
np.ma.array([1], mask=False).repeat(10)
|
| 40 |
+
|
| 41 |
+
def test_masked_array_repr_unicode(self):
|
| 42 |
+
# Ticket #1256
|
| 43 |
+
repr(np.ma.array("Unicode"))
|
| 44 |
+
|
| 45 |
+
def test_atleast_2d(self):
|
| 46 |
+
# Ticket #1559
|
| 47 |
+
a = np.ma.masked_array([0.0, 1.2, 3.5], mask=[False, True, False])
|
| 48 |
+
b = np.atleast_2d(a)
|
| 49 |
+
assert_(a.mask.ndim == 1)
|
| 50 |
+
assert_(b.mask.ndim == 2)
|
| 51 |
+
|
| 52 |
+
def test_set_fill_value_unicode_py3(self):
|
| 53 |
+
# Ticket #2733
|
| 54 |
+
a = np.ma.masked_array(['a', 'b', 'c'], mask=[1, 0, 0])
|
| 55 |
+
a.fill_value = 'X'
|
| 56 |
+
assert_(a.fill_value == 'X')
|
| 57 |
+
|
| 58 |
+
def test_var_sets_maskedarray_scalar(self):
|
| 59 |
+
# Issue gh-2757
|
| 60 |
+
a = np.ma.array(np.arange(5), mask=True)
|
| 61 |
+
mout = np.ma.array(-1, dtype=float)
|
| 62 |
+
a.var(out=mout)
|
| 63 |
+
assert_(mout._data == 0)
|
| 64 |
+
|
| 65 |
+
def test_ddof_corrcoef(self):
|
| 66 |
+
# See gh-3336
|
| 67 |
+
x = np.ma.masked_equal([1, 2, 3, 4, 5], 4)
|
| 68 |
+
y = np.array([2, 2.5, 3.1, 3, 5])
|
| 69 |
+
# this test can be removed after deprecation.
|
| 70 |
+
with suppress_warnings() as sup:
|
| 71 |
+
sup.filter(DeprecationWarning, "bias and ddof have no effect")
|
| 72 |
+
r0 = np.ma.corrcoef(x, y, ddof=0)
|
| 73 |
+
r1 = np.ma.corrcoef(x, y, ddof=1)
|
| 74 |
+
# ddof should not have an effect (it gets cancelled out)
|
| 75 |
+
assert_allclose(r0.data, r1.data)
|
| 76 |
+
|
| 77 |
+
def test_mask_not_backmangled(self):
|
| 78 |
+
# See gh-10314. Test case taken from gh-3140.
|
| 79 |
+
a = np.ma.MaskedArray([1., 2.], mask=[False, False])
|
| 80 |
+
assert_(a.mask.shape == (2,))
|
| 81 |
+
b = np.tile(a, (2, 1))
|
| 82 |
+
# Check that the above no longer changes a.shape to (1, 2)
|
| 83 |
+
assert_(a.mask.shape == (2,))
|
| 84 |
+
assert_(b.shape == (2, 2))
|
| 85 |
+
assert_(b.mask.shape == (2, 2))
|
| 86 |
+
|
| 87 |
+
def test_empty_list_on_structured(self):
|
| 88 |
+
# See gh-12464. Indexing with empty list should give empty result.
|
| 89 |
+
ma = np.ma.MaskedArray([(1, 1.), (2, 2.), (3, 3.)], dtype='i4,f4')
|
| 90 |
+
assert_array_equal(ma[[]], ma[:0])
|
| 91 |
+
|
| 92 |
+
def test_masked_array_tobytes_fortran(self):
|
| 93 |
+
ma = np.ma.arange(4).reshape((2, 2))
|
| 94 |
+
assert_array_equal(ma.tobytes(order='F'), ma.T.tobytes())
|
| 95 |
+
|
| 96 |
+
def test_structured_array(self):
|
| 97 |
+
# see gh-22041
|
| 98 |
+
np.ma.array((1, (b"", b"")),
|
| 99 |
+
dtype=[("x", np.int_),
|
| 100 |
+
("y", [("i", np.void), ("j", np.void)])])
|
venv/lib/python3.13/site-packages/numpy/ma/tests/test_subclassing.py
ADDED
|
@@ -0,0 +1,469 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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2 |
+
|
| 3 |
+
:author: Pierre Gerard-Marchant
|
| 4 |
+
:contact: pierregm_at_uga_dot_edu
|
| 5 |
+
|
| 6 |
+
"""
|
| 7 |
+
import numpy as np
|
| 8 |
+
from numpy.lib.mixins import NDArrayOperatorsMixin
|
| 9 |
+
from numpy.ma.core import (
|
| 10 |
+
MaskedArray,
|
| 11 |
+
add,
|
| 12 |
+
arange,
|
| 13 |
+
array,
|
| 14 |
+
asanyarray,
|
| 15 |
+
asarray,
|
| 16 |
+
divide,
|
| 17 |
+
hypot,
|
| 18 |
+
log,
|
| 19 |
+
masked,
|
| 20 |
+
masked_array,
|
| 21 |
+
nomask,
|
| 22 |
+
)
|
| 23 |
+
from numpy.ma.testutils import assert_equal
|
| 24 |
+
from numpy.testing import assert_, assert_raises
|
| 25 |
+
|
| 26 |
+
# from numpy.ma.core import (
|
| 27 |
+
|
| 28 |
+
def assert_startswith(a, b):
|
| 29 |
+
# produces a better error message than assert_(a.startswith(b))
|
| 30 |
+
assert_equal(a[:len(b)], b)
|
| 31 |
+
|
| 32 |
+
class SubArray(np.ndarray):
|
| 33 |
+
# Defines a generic np.ndarray subclass, that stores some metadata
|
| 34 |
+
# in the dictionary `info`.
|
| 35 |
+
def __new__(cls, arr, info={}):
|
| 36 |
+
x = np.asanyarray(arr).view(cls)
|
| 37 |
+
x.info = info.copy()
|
| 38 |
+
return x
|
| 39 |
+
|
| 40 |
+
def __array_finalize__(self, obj):
|
| 41 |
+
super().__array_finalize__(obj)
|
| 42 |
+
self.info = getattr(obj, 'info', {}).copy()
|
| 43 |
+
|
| 44 |
+
def __add__(self, other):
|
| 45 |
+
result = super().__add__(other)
|
| 46 |
+
result.info['added'] = result.info.get('added', 0) + 1
|
| 47 |
+
return result
|
| 48 |
+
|
| 49 |
+
def __iadd__(self, other):
|
| 50 |
+
result = super().__iadd__(other)
|
| 51 |
+
result.info['iadded'] = result.info.get('iadded', 0) + 1
|
| 52 |
+
return result
|
| 53 |
+
|
| 54 |
+
|
| 55 |
+
subarray = SubArray
|
| 56 |
+
|
| 57 |
+
|
| 58 |
+
class SubMaskedArray(MaskedArray):
|
| 59 |
+
"""Pure subclass of MaskedArray, keeping some info on subclass."""
|
| 60 |
+
def __new__(cls, info=None, **kwargs):
|
| 61 |
+
obj = super().__new__(cls, **kwargs)
|
| 62 |
+
obj._optinfo['info'] = info
|
| 63 |
+
return obj
|
| 64 |
+
|
| 65 |
+
|
| 66 |
+
class MSubArray(SubArray, MaskedArray):
|
| 67 |
+
|
| 68 |
+
def __new__(cls, data, info={}, mask=nomask):
|
| 69 |
+
subarr = SubArray(data, info)
|
| 70 |
+
_data = MaskedArray.__new__(cls, data=subarr, mask=mask)
|
| 71 |
+
_data.info = subarr.info
|
| 72 |
+
return _data
|
| 73 |
+
|
| 74 |
+
@property
|
| 75 |
+
def _series(self):
|
| 76 |
+
_view = self.view(MaskedArray)
|
| 77 |
+
_view._sharedmask = False
|
| 78 |
+
return _view
|
| 79 |
+
|
| 80 |
+
|
| 81 |
+
msubarray = MSubArray
|
| 82 |
+
|
| 83 |
+
|
| 84 |
+
# Also a subclass that overrides __str__, __repr__ and __setitem__, disallowing
|
| 85 |
+
# setting to non-class values (and thus np.ma.core.masked_print_option)
|
| 86 |
+
# and overrides __array_wrap__, updating the info dict, to check that this
|
| 87 |
+
# doesn't get destroyed by MaskedArray._update_from. But this one also needs
|
| 88 |
+
# its own iterator...
|
| 89 |
+
class CSAIterator:
|
| 90 |
+
"""
|
| 91 |
+
Flat iterator object that uses its own setter/getter
|
| 92 |
+
(works around ndarray.flat not propagating subclass setters/getters
|
| 93 |
+
see https://github.com/numpy/numpy/issues/4564)
|
| 94 |
+
roughly following MaskedIterator
|
| 95 |
+
"""
|
| 96 |
+
def __init__(self, a):
|
| 97 |
+
self._original = a
|
| 98 |
+
self._dataiter = a.view(np.ndarray).flat
|
| 99 |
+
|
| 100 |
+
def __iter__(self):
|
| 101 |
+
return self
|
| 102 |
+
|
| 103 |
+
def __getitem__(self, indx):
|
| 104 |
+
out = self._dataiter.__getitem__(indx)
|
| 105 |
+
if not isinstance(out, np.ndarray):
|
| 106 |
+
out = out.__array__()
|
| 107 |
+
out = out.view(type(self._original))
|
| 108 |
+
return out
|
| 109 |
+
|
| 110 |
+
def __setitem__(self, index, value):
|
| 111 |
+
self._dataiter[index] = self._original._validate_input(value)
|
| 112 |
+
|
| 113 |
+
def __next__(self):
|
| 114 |
+
return next(self._dataiter).__array__().view(type(self._original))
|
| 115 |
+
|
| 116 |
+
|
| 117 |
+
class ComplicatedSubArray(SubArray):
|
| 118 |
+
|
| 119 |
+
def __str__(self):
|
| 120 |
+
return f'myprefix {self.view(SubArray)} mypostfix'
|
| 121 |
+
|
| 122 |
+
def __repr__(self):
|
| 123 |
+
# Return a repr that does not start with 'name('
|
| 124 |
+
return f'<{self.__class__.__name__} {self}>'
|
| 125 |
+
|
| 126 |
+
def _validate_input(self, value):
|
| 127 |
+
if not isinstance(value, ComplicatedSubArray):
|
| 128 |
+
raise ValueError("Can only set to MySubArray values")
|
| 129 |
+
return value
|
| 130 |
+
|
| 131 |
+
def __setitem__(self, item, value):
|
| 132 |
+
# validation ensures direct assignment with ndarray or
|
| 133 |
+
# masked_print_option will fail
|
| 134 |
+
super().__setitem__(item, self._validate_input(value))
|
| 135 |
+
|
| 136 |
+
def __getitem__(self, item):
|
| 137 |
+
# ensure getter returns our own class also for scalars
|
| 138 |
+
value = super().__getitem__(item)
|
| 139 |
+
if not isinstance(value, np.ndarray): # scalar
|
| 140 |
+
value = value.__array__().view(ComplicatedSubArray)
|
| 141 |
+
return value
|
| 142 |
+
|
| 143 |
+
@property
|
| 144 |
+
def flat(self):
|
| 145 |
+
return CSAIterator(self)
|
| 146 |
+
|
| 147 |
+
@flat.setter
|
| 148 |
+
def flat(self, value):
|
| 149 |
+
y = self.ravel()
|
| 150 |
+
y[:] = value
|
| 151 |
+
|
| 152 |
+
def __array_wrap__(self, obj, context=None, return_scalar=False):
|
| 153 |
+
obj = super().__array_wrap__(obj, context, return_scalar)
|
| 154 |
+
if context is not None and context[0] is np.multiply:
|
| 155 |
+
obj.info['multiplied'] = obj.info.get('multiplied', 0) + 1
|
| 156 |
+
|
| 157 |
+
return obj
|
| 158 |
+
|
| 159 |
+
|
| 160 |
+
class WrappedArray(NDArrayOperatorsMixin):
|
| 161 |
+
"""
|
| 162 |
+
Wrapping a MaskedArray rather than subclassing to test that
|
| 163 |
+
ufunc deferrals are commutative.
|
| 164 |
+
See: https://github.com/numpy/numpy/issues/15200)
|
| 165 |
+
"""
|
| 166 |
+
__slots__ = ('_array', 'attrs')
|
| 167 |
+
__array_priority__ = 20
|
| 168 |
+
|
| 169 |
+
def __init__(self, array, **attrs):
|
| 170 |
+
self._array = array
|
| 171 |
+
self.attrs = attrs
|
| 172 |
+
|
| 173 |
+
def __repr__(self):
|
| 174 |
+
return f"{self.__class__.__name__}(\n{self._array}\n{self.attrs}\n)"
|
| 175 |
+
|
| 176 |
+
def __array__(self, dtype=None, copy=None):
|
| 177 |
+
return np.asarray(self._array)
|
| 178 |
+
|
| 179 |
+
def __array_ufunc__(self, ufunc, method, *inputs, **kwargs):
|
| 180 |
+
if method == '__call__':
|
| 181 |
+
inputs = [arg._array if isinstance(arg, self.__class__) else arg
|
| 182 |
+
for arg in inputs]
|
| 183 |
+
return self.__class__(ufunc(*inputs, **kwargs), **self.attrs)
|
| 184 |
+
else:
|
| 185 |
+
return NotImplemented
|
| 186 |
+
|
| 187 |
+
|
| 188 |
+
class TestSubclassing:
|
| 189 |
+
# Test suite for masked subclasses of ndarray.
|
| 190 |
+
|
| 191 |
+
def setup_method(self):
|
| 192 |
+
x = np.arange(5, dtype='float')
|
| 193 |
+
mx = msubarray(x, mask=[0, 1, 0, 0, 0])
|
| 194 |
+
self.data = (x, mx)
|
| 195 |
+
|
| 196 |
+
def test_data_subclassing(self):
|
| 197 |
+
# Tests whether the subclass is kept.
|
| 198 |
+
x = np.arange(5)
|
| 199 |
+
m = [0, 0, 1, 0, 0]
|
| 200 |
+
xsub = SubArray(x)
|
| 201 |
+
xmsub = masked_array(xsub, mask=m)
|
| 202 |
+
assert_(isinstance(xmsub, MaskedArray))
|
| 203 |
+
assert_equal(xmsub._data, xsub)
|
| 204 |
+
assert_(isinstance(xmsub._data, SubArray))
|
| 205 |
+
|
| 206 |
+
def test_maskedarray_subclassing(self):
|
| 207 |
+
# Tests subclassing MaskedArray
|
| 208 |
+
(x, mx) = self.data
|
| 209 |
+
assert_(isinstance(mx._data, subarray))
|
| 210 |
+
|
| 211 |
+
def test_masked_unary_operations(self):
|
| 212 |
+
# Tests masked_unary_operation
|
| 213 |
+
(x, mx) = self.data
|
| 214 |
+
with np.errstate(divide='ignore'):
|
| 215 |
+
assert_(isinstance(log(mx), msubarray))
|
| 216 |
+
assert_equal(log(x), np.log(x))
|
| 217 |
+
|
| 218 |
+
def test_masked_binary_operations(self):
|
| 219 |
+
# Tests masked_binary_operation
|
| 220 |
+
(x, mx) = self.data
|
| 221 |
+
# Result should be a msubarray
|
| 222 |
+
assert_(isinstance(add(mx, mx), msubarray))
|
| 223 |
+
assert_(isinstance(add(mx, x), msubarray))
|
| 224 |
+
# Result should work
|
| 225 |
+
assert_equal(add(mx, x), mx + x)
|
| 226 |
+
assert_(isinstance(add(mx, mx)._data, subarray))
|
| 227 |
+
assert_(isinstance(add.outer(mx, mx), msubarray))
|
| 228 |
+
assert_(isinstance(hypot(mx, mx), msubarray))
|
| 229 |
+
assert_(isinstance(hypot(mx, x), msubarray))
|
| 230 |
+
|
| 231 |
+
def test_masked_binary_operations2(self):
|
| 232 |
+
# Tests domained_masked_binary_operation
|
| 233 |
+
(x, mx) = self.data
|
| 234 |
+
xmx = masked_array(mx.data.__array__(), mask=mx.mask)
|
| 235 |
+
assert_(isinstance(divide(mx, mx), msubarray))
|
| 236 |
+
assert_(isinstance(divide(mx, x), msubarray))
|
| 237 |
+
assert_equal(divide(mx, mx), divide(xmx, xmx))
|
| 238 |
+
|
| 239 |
+
def test_attributepropagation(self):
|
| 240 |
+
x = array(arange(5), mask=[0] + [1] * 4)
|
| 241 |
+
my = masked_array(subarray(x))
|
| 242 |
+
ym = msubarray(x)
|
| 243 |
+
#
|
| 244 |
+
z = (my + 1)
|
| 245 |
+
assert_(isinstance(z, MaskedArray))
|
| 246 |
+
assert_(not isinstance(z, MSubArray))
|
| 247 |
+
assert_(isinstance(z._data, SubArray))
|
| 248 |
+
assert_equal(z._data.info, {})
|
| 249 |
+
#
|
| 250 |
+
z = (ym + 1)
|
| 251 |
+
assert_(isinstance(z, MaskedArray))
|
| 252 |
+
assert_(isinstance(z, MSubArray))
|
| 253 |
+
assert_(isinstance(z._data, SubArray))
|
| 254 |
+
assert_(z._data.info['added'] > 0)
|
| 255 |
+
# Test that inplace methods from data get used (gh-4617)
|
| 256 |
+
ym += 1
|
| 257 |
+
assert_(isinstance(ym, MaskedArray))
|
| 258 |
+
assert_(isinstance(ym, MSubArray))
|
| 259 |
+
assert_(isinstance(ym._data, SubArray))
|
| 260 |
+
assert_(ym._data.info['iadded'] > 0)
|
| 261 |
+
#
|
| 262 |
+
ym._set_mask([1, 0, 0, 0, 1])
|
| 263 |
+
assert_equal(ym._mask, [1, 0, 0, 0, 1])
|
| 264 |
+
ym._series._set_mask([0, 0, 0, 0, 1])
|
| 265 |
+
assert_equal(ym._mask, [0, 0, 0, 0, 1])
|
| 266 |
+
#
|
| 267 |
+
xsub = subarray(x, info={'name': 'x'})
|
| 268 |
+
mxsub = masked_array(xsub)
|
| 269 |
+
assert_(hasattr(mxsub, 'info'))
|
| 270 |
+
assert_equal(mxsub.info, xsub.info)
|
| 271 |
+
|
| 272 |
+
def test_subclasspreservation(self):
|
| 273 |
+
# Checks that masked_array(...,subok=True) preserves the class.
|
| 274 |
+
x = np.arange(5)
|
| 275 |
+
m = [0, 0, 1, 0, 0]
|
| 276 |
+
xinfo = list(zip(x, m))
|
| 277 |
+
xsub = MSubArray(x, mask=m, info={'xsub': xinfo})
|
| 278 |
+
#
|
| 279 |
+
mxsub = masked_array(xsub, subok=False)
|
| 280 |
+
assert_(not isinstance(mxsub, MSubArray))
|
| 281 |
+
assert_(isinstance(mxsub, MaskedArray))
|
| 282 |
+
assert_equal(mxsub._mask, m)
|
| 283 |
+
#
|
| 284 |
+
mxsub = asarray(xsub)
|
| 285 |
+
assert_(not isinstance(mxsub, MSubArray))
|
| 286 |
+
assert_(isinstance(mxsub, MaskedArray))
|
| 287 |
+
assert_equal(mxsub._mask, m)
|
| 288 |
+
#
|
| 289 |
+
mxsub = masked_array(xsub, subok=True)
|
| 290 |
+
assert_(isinstance(mxsub, MSubArray))
|
| 291 |
+
assert_equal(mxsub.info, xsub.info)
|
| 292 |
+
assert_equal(mxsub._mask, xsub._mask)
|
| 293 |
+
#
|
| 294 |
+
mxsub = asanyarray(xsub)
|
| 295 |
+
assert_(isinstance(mxsub, MSubArray))
|
| 296 |
+
assert_equal(mxsub.info, xsub.info)
|
| 297 |
+
assert_equal(mxsub._mask, m)
|
| 298 |
+
|
| 299 |
+
def test_subclass_items(self):
|
| 300 |
+
"""test that getter and setter go via baseclass"""
|
| 301 |
+
x = np.arange(5)
|
| 302 |
+
xcsub = ComplicatedSubArray(x)
|
| 303 |
+
mxcsub = masked_array(xcsub, mask=[True, False, True, False, False])
|
| 304 |
+
# getter should return a ComplicatedSubArray, even for single item
|
| 305 |
+
# first check we wrote ComplicatedSubArray correctly
|
| 306 |
+
assert_(isinstance(xcsub[1], ComplicatedSubArray))
|
| 307 |
+
assert_(isinstance(xcsub[1, ...], ComplicatedSubArray))
|
| 308 |
+
assert_(isinstance(xcsub[1:4], ComplicatedSubArray))
|
| 309 |
+
|
| 310 |
+
# now that it propagates inside the MaskedArray
|
| 311 |
+
assert_(isinstance(mxcsub[1], ComplicatedSubArray))
|
| 312 |
+
assert_(isinstance(mxcsub[1, ...].data, ComplicatedSubArray))
|
| 313 |
+
assert_(mxcsub[0] is masked)
|
| 314 |
+
assert_(isinstance(mxcsub[0, ...].data, ComplicatedSubArray))
|
| 315 |
+
assert_(isinstance(mxcsub[1:4].data, ComplicatedSubArray))
|
| 316 |
+
|
| 317 |
+
# also for flattened version (which goes via MaskedIterator)
|
| 318 |
+
assert_(isinstance(mxcsub.flat[1].data, ComplicatedSubArray))
|
| 319 |
+
assert_(mxcsub.flat[0] is masked)
|
| 320 |
+
assert_(isinstance(mxcsub.flat[1:4].base, ComplicatedSubArray))
|
| 321 |
+
|
| 322 |
+
# setter should only work with ComplicatedSubArray input
|
| 323 |
+
# first check we wrote ComplicatedSubArray correctly
|
| 324 |
+
assert_raises(ValueError, xcsub.__setitem__, 1, x[4])
|
| 325 |
+
# now that it propagates inside the MaskedArray
|
| 326 |
+
assert_raises(ValueError, mxcsub.__setitem__, 1, x[4])
|
| 327 |
+
assert_raises(ValueError, mxcsub.__setitem__, slice(1, 4), x[1:4])
|
| 328 |
+
mxcsub[1] = xcsub[4]
|
| 329 |
+
mxcsub[1:4] = xcsub[1:4]
|
| 330 |
+
# also for flattened version (which goes via MaskedIterator)
|
| 331 |
+
assert_raises(ValueError, mxcsub.flat.__setitem__, 1, x[4])
|
| 332 |
+
assert_raises(ValueError, mxcsub.flat.__setitem__, slice(1, 4), x[1:4])
|
| 333 |
+
mxcsub.flat[1] = xcsub[4]
|
| 334 |
+
mxcsub.flat[1:4] = xcsub[1:4]
|
| 335 |
+
|
| 336 |
+
def test_subclass_nomask_items(self):
|
| 337 |
+
x = np.arange(5)
|
| 338 |
+
xcsub = ComplicatedSubArray(x)
|
| 339 |
+
mxcsub_nomask = masked_array(xcsub)
|
| 340 |
+
|
| 341 |
+
assert_(isinstance(mxcsub_nomask[1, ...].data, ComplicatedSubArray))
|
| 342 |
+
assert_(isinstance(mxcsub_nomask[0, ...].data, ComplicatedSubArray))
|
| 343 |
+
|
| 344 |
+
assert_(isinstance(mxcsub_nomask[1], ComplicatedSubArray))
|
| 345 |
+
assert_(isinstance(mxcsub_nomask[0], ComplicatedSubArray))
|
| 346 |
+
|
| 347 |
+
def test_subclass_repr(self):
|
| 348 |
+
"""test that repr uses the name of the subclass
|
| 349 |
+
and 'array' for np.ndarray"""
|
| 350 |
+
x = np.arange(5)
|
| 351 |
+
mx = masked_array(x, mask=[True, False, True, False, False])
|
| 352 |
+
assert_startswith(repr(mx), 'masked_array')
|
| 353 |
+
xsub = SubArray(x)
|
| 354 |
+
mxsub = masked_array(xsub, mask=[True, False, True, False, False])
|
| 355 |
+
assert_startswith(repr(mxsub),
|
| 356 |
+
f'masked_{SubArray.__name__}(data=[--, 1, --, 3, 4]')
|
| 357 |
+
|
| 358 |
+
def test_subclass_str(self):
|
| 359 |
+
"""test str with subclass that has overridden str, setitem"""
|
| 360 |
+
# first without override
|
| 361 |
+
x = np.arange(5)
|
| 362 |
+
xsub = SubArray(x)
|
| 363 |
+
mxsub = masked_array(xsub, mask=[True, False, True, False, False])
|
| 364 |
+
assert_equal(str(mxsub), '[-- 1 -- 3 4]')
|
| 365 |
+
|
| 366 |
+
xcsub = ComplicatedSubArray(x)
|
| 367 |
+
assert_raises(ValueError, xcsub.__setitem__, 0,
|
| 368 |
+
np.ma.core.masked_print_option)
|
| 369 |
+
mxcsub = masked_array(xcsub, mask=[True, False, True, False, False])
|
| 370 |
+
assert_equal(str(mxcsub), 'myprefix [-- 1 -- 3 4] mypostfix')
|
| 371 |
+
|
| 372 |
+
def test_pure_subclass_info_preservation(self):
|
| 373 |
+
# Test that ufuncs and methods conserve extra information consistently;
|
| 374 |
+
# see gh-7122.
|
| 375 |
+
arr1 = SubMaskedArray('test', data=[1, 2, 3, 4, 5, 6])
|
| 376 |
+
arr2 = SubMaskedArray(data=[0, 1, 2, 3, 4, 5])
|
| 377 |
+
diff1 = np.subtract(arr1, arr2)
|
| 378 |
+
assert_('info' in diff1._optinfo)
|
| 379 |
+
assert_(diff1._optinfo['info'] == 'test')
|
| 380 |
+
diff2 = arr1 - arr2
|
| 381 |
+
assert_('info' in diff2._optinfo)
|
| 382 |
+
assert_(diff2._optinfo['info'] == 'test')
|
| 383 |
+
|
| 384 |
+
|
| 385 |
+
class ArrayNoInheritance:
|
| 386 |
+
"""Quantity-like class that does not inherit from ndarray"""
|
| 387 |
+
def __init__(self, data, units):
|
| 388 |
+
self.magnitude = data
|
| 389 |
+
self.units = units
|
| 390 |
+
|
| 391 |
+
def __getattr__(self, attr):
|
| 392 |
+
return getattr(self.magnitude, attr)
|
| 393 |
+
|
| 394 |
+
|
| 395 |
+
def test_array_no_inheritance():
|
| 396 |
+
data_masked = np.ma.array([1, 2, 3], mask=[True, False, True])
|
| 397 |
+
data_masked_units = ArrayNoInheritance(data_masked, 'meters')
|
| 398 |
+
|
| 399 |
+
# Get the masked representation of the Quantity-like class
|
| 400 |
+
new_array = np.ma.array(data_masked_units)
|
| 401 |
+
assert_equal(data_masked.data, new_array.data)
|
| 402 |
+
assert_equal(data_masked.mask, new_array.mask)
|
| 403 |
+
# Test sharing the mask
|
| 404 |
+
data_masked.mask = [True, False, False]
|
| 405 |
+
assert_equal(data_masked.mask, new_array.mask)
|
| 406 |
+
assert_(new_array.sharedmask)
|
| 407 |
+
|
| 408 |
+
# Get the masked representation of the Quantity-like class
|
| 409 |
+
new_array = np.ma.array(data_masked_units, copy=True)
|
| 410 |
+
assert_equal(data_masked.data, new_array.data)
|
| 411 |
+
assert_equal(data_masked.mask, new_array.mask)
|
| 412 |
+
# Test that the mask is not shared when copy=True
|
| 413 |
+
data_masked.mask = [True, False, True]
|
| 414 |
+
assert_equal([True, False, False], new_array.mask)
|
| 415 |
+
assert_(not new_array.sharedmask)
|
| 416 |
+
|
| 417 |
+
# Get the masked representation of the Quantity-like class
|
| 418 |
+
new_array = np.ma.array(data_masked_units, keep_mask=False)
|
| 419 |
+
assert_equal(data_masked.data, new_array.data)
|
| 420 |
+
# The change did not affect the original mask
|
| 421 |
+
assert_equal(data_masked.mask, [True, False, True])
|
| 422 |
+
# Test that the mask is False and not shared when keep_mask=False
|
| 423 |
+
assert_(not new_array.mask)
|
| 424 |
+
assert_(not new_array.sharedmask)
|
| 425 |
+
|
| 426 |
+
|
| 427 |
+
class TestClassWrapping:
|
| 428 |
+
# Test suite for classes that wrap MaskedArrays
|
| 429 |
+
|
| 430 |
+
def setup_method(self):
|
| 431 |
+
m = np.ma.masked_array([1, 3, 5], mask=[False, True, False])
|
| 432 |
+
wm = WrappedArray(m)
|
| 433 |
+
self.data = (m, wm)
|
| 434 |
+
|
| 435 |
+
def test_masked_unary_operations(self):
|
| 436 |
+
# Tests masked_unary_operation
|
| 437 |
+
(m, wm) = self.data
|
| 438 |
+
with np.errstate(divide='ignore'):
|
| 439 |
+
assert_(isinstance(np.log(wm), WrappedArray))
|
| 440 |
+
|
| 441 |
+
def test_masked_binary_operations(self):
|
| 442 |
+
# Tests masked_binary_operation
|
| 443 |
+
(m, wm) = self.data
|
| 444 |
+
# Result should be a WrappedArray
|
| 445 |
+
assert_(isinstance(np.add(wm, wm), WrappedArray))
|
| 446 |
+
assert_(isinstance(np.add(m, wm), WrappedArray))
|
| 447 |
+
assert_(isinstance(np.add(wm, m), WrappedArray))
|
| 448 |
+
# add and '+' should call the same ufunc
|
| 449 |
+
assert_equal(np.add(m, wm), m + wm)
|
| 450 |
+
assert_(isinstance(np.hypot(m, wm), WrappedArray))
|
| 451 |
+
assert_(isinstance(np.hypot(wm, m), WrappedArray))
|
| 452 |
+
# Test domained binary operations
|
| 453 |
+
assert_(isinstance(np.divide(wm, m), WrappedArray))
|
| 454 |
+
assert_(isinstance(np.divide(m, wm), WrappedArray))
|
| 455 |
+
assert_equal(np.divide(wm, m) * m, np.divide(m, m) * wm)
|
| 456 |
+
# Test broadcasting
|
| 457 |
+
m2 = np.stack([m, m])
|
| 458 |
+
assert_(isinstance(np.divide(wm, m2), WrappedArray))
|
| 459 |
+
assert_(isinstance(np.divide(m2, wm), WrappedArray))
|
| 460 |
+
assert_equal(np.divide(m2, wm), np.divide(wm, m2))
|
| 461 |
+
|
| 462 |
+
def test_mixins_have_slots(self):
|
| 463 |
+
mixin = NDArrayOperatorsMixin()
|
| 464 |
+
# Should raise an error
|
| 465 |
+
assert_raises(AttributeError, mixin.__setattr__, "not_a_real_attr", 1)
|
| 466 |
+
|
| 467 |
+
m = np.ma.masked_array([1, 3, 5], mask=[False, True, False])
|
| 468 |
+
wm = WrappedArray(m)
|
| 469 |
+
assert_raises(AttributeError, wm.__setattr__, "not_an_attr", 2)
|
venv/lib/python3.13/site-packages/numpy/matrixlib/__pycache__/__init__.cpython-313.pyc
ADDED
|
Binary file (472 Bytes). View file
|
|
|
venv/lib/python3.13/site-packages/numpy/matrixlib/__pycache__/defmatrix.cpython-313.pyc
ADDED
|
Binary file (31.6 kB). View file
|
|
|
venv/lib/python3.13/site-packages/numpy/matrixlib/tests/__init__.py
ADDED
|
File without changes
|
venv/lib/python3.13/site-packages/numpy/matrixlib/tests/__pycache__/__init__.cpython-313.pyc
ADDED
|
Binary file (184 Bytes). View file
|
|
|
venv/lib/python3.13/site-packages/numpy/matrixlib/tests/__pycache__/test_defmatrix.cpython-313.pyc
ADDED
|
Binary file (33.2 kB). View file
|
|
|
venv/lib/python3.13/site-packages/numpy/matrixlib/tests/__pycache__/test_interaction.cpython-313.pyc
ADDED
|
Binary file (21.5 kB). View file
|
|
|
venv/lib/python3.13/site-packages/numpy/matrixlib/tests/__pycache__/test_masked_matrix.cpython-313.pyc
ADDED
|
Binary file (16.6 kB). View file
|
|
|
venv/lib/python3.13/site-packages/numpy/matrixlib/tests/__pycache__/test_matrix_linalg.cpython-313.pyc
ADDED
|
Binary file (4.57 kB). View file
|
|
|
venv/lib/python3.13/site-packages/numpy/matrixlib/tests/__pycache__/test_multiarray.cpython-313.pyc
ADDED
|
Binary file (1.65 kB). View file
|
|
|
venv/lib/python3.13/site-packages/numpy/matrixlib/tests/__pycache__/test_numeric.cpython-313.pyc
ADDED
|
Binary file (1.48 kB). View file
|
|
|
venv/lib/python3.13/site-packages/numpy/matrixlib/tests/__pycache__/test_regression.cpython-313.pyc
ADDED
|
Binary file (2.88 kB). View file
|
|
|
venv/lib/python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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import collections.abc
|
| 2 |
+
|
| 3 |
+
import numpy as np
|
| 4 |
+
from numpy import asmatrix, bmat, matrix
|
| 5 |
+
from numpy.linalg import matrix_power
|
| 6 |
+
from numpy.testing import (
|
| 7 |
+
assert_,
|
| 8 |
+
assert_almost_equal,
|
| 9 |
+
assert_array_almost_equal,
|
| 10 |
+
assert_array_equal,
|
| 11 |
+
assert_equal,
|
| 12 |
+
assert_raises,
|
| 13 |
+
)
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
class TestCtor:
|
| 17 |
+
def test_basic(self):
|
| 18 |
+
A = np.array([[1, 2], [3, 4]])
|
| 19 |
+
mA = matrix(A)
|
| 20 |
+
assert_(np.all(mA.A == A))
|
| 21 |
+
|
| 22 |
+
B = bmat("A,A;A,A")
|
| 23 |
+
C = bmat([[A, A], [A, A]])
|
| 24 |
+
D = np.array([[1, 2, 1, 2],
|
| 25 |
+
[3, 4, 3, 4],
|
| 26 |
+
[1, 2, 1, 2],
|
| 27 |
+
[3, 4, 3, 4]])
|
| 28 |
+
assert_(np.all(B.A == D))
|
| 29 |
+
assert_(np.all(C.A == D))
|
| 30 |
+
|
| 31 |
+
E = np.array([[5, 6], [7, 8]])
|
| 32 |
+
AEresult = matrix([[1, 2, 5, 6], [3, 4, 7, 8]])
|
| 33 |
+
assert_(np.all(bmat([A, E]) == AEresult))
|
| 34 |
+
|
| 35 |
+
vec = np.arange(5)
|
| 36 |
+
mvec = matrix(vec)
|
| 37 |
+
assert_(mvec.shape == (1, 5))
|
| 38 |
+
|
| 39 |
+
def test_exceptions(self):
|
| 40 |
+
# Check for ValueError when called with invalid string data.
|
| 41 |
+
assert_raises(ValueError, matrix, "invalid")
|
| 42 |
+
|
| 43 |
+
def test_bmat_nondefault_str(self):
|
| 44 |
+
A = np.array([[1, 2], [3, 4]])
|
| 45 |
+
B = np.array([[5, 6], [7, 8]])
|
| 46 |
+
Aresult = np.array([[1, 2, 1, 2],
|
| 47 |
+
[3, 4, 3, 4],
|
| 48 |
+
[1, 2, 1, 2],
|
| 49 |
+
[3, 4, 3, 4]])
|
| 50 |
+
mixresult = np.array([[1, 2, 5, 6],
|
| 51 |
+
[3, 4, 7, 8],
|
| 52 |
+
[5, 6, 1, 2],
|
| 53 |
+
[7, 8, 3, 4]])
|
| 54 |
+
assert_(np.all(bmat("A,A;A,A") == Aresult))
|
| 55 |
+
assert_(np.all(bmat("A,A;A,A", ldict={'A': B}) == Aresult))
|
| 56 |
+
assert_raises(TypeError, bmat, "A,A;A,A", gdict={'A': B})
|
| 57 |
+
assert_(
|
| 58 |
+
np.all(bmat("A,A;A,A", ldict={'A': A}, gdict={'A': B}) == Aresult))
|
| 59 |
+
b2 = bmat("A,B;C,D", ldict={'A': A, 'B': B}, gdict={'C': B, 'D': A})
|
| 60 |
+
assert_(np.all(b2 == mixresult))
|
| 61 |
+
|
| 62 |
+
|
| 63 |
+
class TestProperties:
|
| 64 |
+
def test_sum(self):
|
| 65 |
+
"""Test whether matrix.sum(axis=1) preserves orientation.
|
| 66 |
+
Fails in NumPy <= 0.9.6.2127.
|
| 67 |
+
"""
|
| 68 |
+
M = matrix([[1, 2, 0, 0],
|
| 69 |
+
[3, 4, 0, 0],
|
| 70 |
+
[1, 2, 1, 2],
|
| 71 |
+
[3, 4, 3, 4]])
|
| 72 |
+
sum0 = matrix([8, 12, 4, 6])
|
| 73 |
+
sum1 = matrix([3, 7, 6, 14]).T
|
| 74 |
+
sumall = 30
|
| 75 |
+
assert_array_equal(sum0, M.sum(axis=0))
|
| 76 |
+
assert_array_equal(sum1, M.sum(axis=1))
|
| 77 |
+
assert_equal(sumall, M.sum())
|
| 78 |
+
|
| 79 |
+
assert_array_equal(sum0, np.sum(M, axis=0))
|
| 80 |
+
assert_array_equal(sum1, np.sum(M, axis=1))
|
| 81 |
+
assert_equal(sumall, np.sum(M))
|
| 82 |
+
|
| 83 |
+
def test_prod(self):
|
| 84 |
+
x = matrix([[1, 2, 3], [4, 5, 6]])
|
| 85 |
+
assert_equal(x.prod(), 720)
|
| 86 |
+
assert_equal(x.prod(0), matrix([[4, 10, 18]]))
|
| 87 |
+
assert_equal(x.prod(1), matrix([[6], [120]]))
|
| 88 |
+
|
| 89 |
+
assert_equal(np.prod(x), 720)
|
| 90 |
+
assert_equal(np.prod(x, axis=0), matrix([[4, 10, 18]]))
|
| 91 |
+
assert_equal(np.prod(x, axis=1), matrix([[6], [120]]))
|
| 92 |
+
|
| 93 |
+
y = matrix([0, 1, 3])
|
| 94 |
+
assert_(y.prod() == 0)
|
| 95 |
+
|
| 96 |
+
def test_max(self):
|
| 97 |
+
x = matrix([[1, 2, 3], [4, 5, 6]])
|
| 98 |
+
assert_equal(x.max(), 6)
|
| 99 |
+
assert_equal(x.max(0), matrix([[4, 5, 6]]))
|
| 100 |
+
assert_equal(x.max(1), matrix([[3], [6]]))
|
| 101 |
+
|
| 102 |
+
assert_equal(np.max(x), 6)
|
| 103 |
+
assert_equal(np.max(x, axis=0), matrix([[4, 5, 6]]))
|
| 104 |
+
assert_equal(np.max(x, axis=1), matrix([[3], [6]]))
|
| 105 |
+
|
| 106 |
+
def test_min(self):
|
| 107 |
+
x = matrix([[1, 2, 3], [4, 5, 6]])
|
| 108 |
+
assert_equal(x.min(), 1)
|
| 109 |
+
assert_equal(x.min(0), matrix([[1, 2, 3]]))
|
| 110 |
+
assert_equal(x.min(1), matrix([[1], [4]]))
|
| 111 |
+
|
| 112 |
+
assert_equal(np.min(x), 1)
|
| 113 |
+
assert_equal(np.min(x, axis=0), matrix([[1, 2, 3]]))
|
| 114 |
+
assert_equal(np.min(x, axis=1), matrix([[1], [4]]))
|
| 115 |
+
|
| 116 |
+
def test_ptp(self):
|
| 117 |
+
x = np.arange(4).reshape((2, 2))
|
| 118 |
+
mx = x.view(np.matrix)
|
| 119 |
+
assert_(mx.ptp() == 3)
|
| 120 |
+
assert_(np.all(mx.ptp(0) == np.array([2, 2])))
|
| 121 |
+
assert_(np.all(mx.ptp(1) == np.array([1, 1])))
|
| 122 |
+
|
| 123 |
+
def test_var(self):
|
| 124 |
+
x = np.arange(9).reshape((3, 3))
|
| 125 |
+
mx = x.view(np.matrix)
|
| 126 |
+
assert_equal(x.var(ddof=0), mx.var(ddof=0))
|
| 127 |
+
assert_equal(x.var(ddof=1), mx.var(ddof=1))
|
| 128 |
+
|
| 129 |
+
def test_basic(self):
|
| 130 |
+
import numpy.linalg as linalg
|
| 131 |
+
|
| 132 |
+
A = np.array([[1., 2.],
|
| 133 |
+
[3., 4.]])
|
| 134 |
+
mA = matrix(A)
|
| 135 |
+
assert_(np.allclose(linalg.inv(A), mA.I))
|
| 136 |
+
assert_(np.all(np.array(np.transpose(A) == mA.T)))
|
| 137 |
+
assert_(np.all(np.array(np.transpose(A) == mA.H)))
|
| 138 |
+
assert_(np.all(A == mA.A))
|
| 139 |
+
|
| 140 |
+
B = A + 2j * A
|
| 141 |
+
mB = matrix(B)
|
| 142 |
+
assert_(np.allclose(linalg.inv(B), mB.I))
|
| 143 |
+
assert_(np.all(np.array(np.transpose(B) == mB.T)))
|
| 144 |
+
assert_(np.all(np.array(np.transpose(B).conj() == mB.H)))
|
| 145 |
+
|
| 146 |
+
def test_pinv(self):
|
| 147 |
+
x = matrix(np.arange(6).reshape(2, 3))
|
| 148 |
+
xpinv = matrix([[-0.77777778, 0.27777778],
|
| 149 |
+
[-0.11111111, 0.11111111],
|
| 150 |
+
[ 0.55555556, -0.05555556]])
|
| 151 |
+
assert_almost_equal(x.I, xpinv)
|
| 152 |
+
|
| 153 |
+
def test_comparisons(self):
|
| 154 |
+
A = np.arange(100).reshape(10, 10)
|
| 155 |
+
mA = matrix(A)
|
| 156 |
+
mB = matrix(A) + 0.1
|
| 157 |
+
assert_(np.all(mB == A + 0.1))
|
| 158 |
+
assert_(np.all(mB == matrix(A + 0.1)))
|
| 159 |
+
assert_(not np.any(mB == matrix(A - 0.1)))
|
| 160 |
+
assert_(np.all(mA < mB))
|
| 161 |
+
assert_(np.all(mA <= mB))
|
| 162 |
+
assert_(np.all(mA <= mA))
|
| 163 |
+
assert_(not np.any(mA < mA))
|
| 164 |
+
|
| 165 |
+
assert_(not np.any(mB < mA))
|
| 166 |
+
assert_(np.all(mB >= mA))
|
| 167 |
+
assert_(np.all(mB >= mB))
|
| 168 |
+
assert_(not np.any(mB > mB))
|
| 169 |
+
|
| 170 |
+
assert_(np.all(mA == mA))
|
| 171 |
+
assert_(not np.any(mA == mB))
|
| 172 |
+
assert_(np.all(mB != mA))
|
| 173 |
+
|
| 174 |
+
assert_(not np.all(abs(mA) > 0))
|
| 175 |
+
assert_(np.all(abs(mB > 0)))
|
| 176 |
+
|
| 177 |
+
def test_asmatrix(self):
|
| 178 |
+
A = np.arange(100).reshape(10, 10)
|
| 179 |
+
mA = asmatrix(A)
|
| 180 |
+
A[0, 0] = -10
|
| 181 |
+
assert_(A[0, 0] == mA[0, 0])
|
| 182 |
+
|
| 183 |
+
def test_noaxis(self):
|
| 184 |
+
A = matrix([[1, 0], [0, 1]])
|
| 185 |
+
assert_(A.sum() == matrix(2))
|
| 186 |
+
assert_(A.mean() == matrix(0.5))
|
| 187 |
+
|
| 188 |
+
def test_repr(self):
|
| 189 |
+
A = matrix([[1, 0], [0, 1]])
|
| 190 |
+
assert_(repr(A) == "matrix([[1, 0],\n [0, 1]])")
|
| 191 |
+
|
| 192 |
+
def test_make_bool_matrix_from_str(self):
|
| 193 |
+
A = matrix('True; True; False')
|
| 194 |
+
B = matrix([[True], [True], [False]])
|
| 195 |
+
assert_array_equal(A, B)
|
| 196 |
+
|
| 197 |
+
class TestCasting:
|
| 198 |
+
def test_basic(self):
|
| 199 |
+
A = np.arange(100).reshape(10, 10)
|
| 200 |
+
mA = matrix(A)
|
| 201 |
+
|
| 202 |
+
mB = mA.copy()
|
| 203 |
+
O = np.ones((10, 10), np.float64) * 0.1
|
| 204 |
+
mB = mB + O
|
| 205 |
+
assert_(mB.dtype.type == np.float64)
|
| 206 |
+
assert_(np.all(mA != mB))
|
| 207 |
+
assert_(np.all(mB == mA + 0.1))
|
| 208 |
+
|
| 209 |
+
mC = mA.copy()
|
| 210 |
+
O = np.ones((10, 10), np.complex128)
|
| 211 |
+
mC = mC * O
|
| 212 |
+
assert_(mC.dtype.type == np.complex128)
|
| 213 |
+
assert_(np.all(mA != mB))
|
| 214 |
+
|
| 215 |
+
|
| 216 |
+
class TestAlgebra:
|
| 217 |
+
def test_basic(self):
|
| 218 |
+
import numpy.linalg as linalg
|
| 219 |
+
|
| 220 |
+
A = np.array([[1., 2.], [3., 4.]])
|
| 221 |
+
mA = matrix(A)
|
| 222 |
+
|
| 223 |
+
B = np.identity(2)
|
| 224 |
+
for i in range(6):
|
| 225 |
+
assert_(np.allclose((mA ** i).A, B))
|
| 226 |
+
B = np.dot(B, A)
|
| 227 |
+
|
| 228 |
+
Ainv = linalg.inv(A)
|
| 229 |
+
B = np.identity(2)
|
| 230 |
+
for i in range(6):
|
| 231 |
+
assert_(np.allclose((mA ** -i).A, B))
|
| 232 |
+
B = np.dot(B, Ainv)
|
| 233 |
+
|
| 234 |
+
assert_(np.allclose((mA * mA).A, np.dot(A, A)))
|
| 235 |
+
assert_(np.allclose((mA + mA).A, (A + A)))
|
| 236 |
+
assert_(np.allclose((3 * mA).A, (3 * A)))
|
| 237 |
+
|
| 238 |
+
mA2 = matrix(A)
|
| 239 |
+
mA2 *= 3
|
| 240 |
+
assert_(np.allclose(mA2.A, 3 * A))
|
| 241 |
+
|
| 242 |
+
def test_pow(self):
|
| 243 |
+
"""Test raising a matrix to an integer power works as expected."""
|
| 244 |
+
m = matrix("1. 2.; 3. 4.")
|
| 245 |
+
m2 = m.copy()
|
| 246 |
+
m2 **= 2
|
| 247 |
+
mi = m.copy()
|
| 248 |
+
mi **= -1
|
| 249 |
+
m4 = m2.copy()
|
| 250 |
+
m4 **= 2
|
| 251 |
+
assert_array_almost_equal(m2, m**2)
|
| 252 |
+
assert_array_almost_equal(m4, np.dot(m2, m2))
|
| 253 |
+
assert_array_almost_equal(np.dot(mi, m), np.eye(2))
|
| 254 |
+
|
| 255 |
+
def test_scalar_type_pow(self):
|
| 256 |
+
m = matrix([[1, 2], [3, 4]])
|
| 257 |
+
for scalar_t in [np.int8, np.uint8]:
|
| 258 |
+
two = scalar_t(2)
|
| 259 |
+
assert_array_almost_equal(m ** 2, m ** two)
|
| 260 |
+
|
| 261 |
+
def test_notimplemented(self):
|
| 262 |
+
'''Check that 'not implemented' operations produce a failure.'''
|
| 263 |
+
A = matrix([[1., 2.],
|
| 264 |
+
[3., 4.]])
|
| 265 |
+
|
| 266 |
+
# __rpow__
|
| 267 |
+
with assert_raises(TypeError):
|
| 268 |
+
1.0**A
|
| 269 |
+
|
| 270 |
+
# __mul__ with something not a list, ndarray, tuple, or scalar
|
| 271 |
+
with assert_raises(TypeError):
|
| 272 |
+
A * object()
|
| 273 |
+
|
| 274 |
+
|
| 275 |
+
class TestMatrixReturn:
|
| 276 |
+
def test_instance_methods(self):
|
| 277 |
+
a = matrix([1.0], dtype='f8')
|
| 278 |
+
methodargs = {
|
| 279 |
+
'astype': ('intc',),
|
| 280 |
+
'clip': (0.0, 1.0),
|
| 281 |
+
'compress': ([1],),
|
| 282 |
+
'repeat': (1,),
|
| 283 |
+
'reshape': (1,),
|
| 284 |
+
'swapaxes': (0, 0),
|
| 285 |
+
'dot': np.array([1.0]),
|
| 286 |
+
}
|
| 287 |
+
excluded_methods = [
|
| 288 |
+
'argmin', 'choose', 'dump', 'dumps', 'fill', 'getfield',
|
| 289 |
+
'getA', 'getA1', 'item', 'nonzero', 'put', 'putmask', 'resize',
|
| 290 |
+
'searchsorted', 'setflags', 'setfield', 'sort',
|
| 291 |
+
'partition', 'argpartition', 'newbyteorder', 'to_device',
|
| 292 |
+
'take', 'tofile', 'tolist', 'tobytes', 'all', 'any',
|
| 293 |
+
'sum', 'argmax', 'argmin', 'min', 'max', 'mean', 'var', 'ptp',
|
| 294 |
+
'prod', 'std', 'ctypes', 'itemset', 'bitwise_count',
|
| 295 |
+
]
|
| 296 |
+
for attrib in dir(a):
|
| 297 |
+
if attrib.startswith('_') or attrib in excluded_methods:
|
| 298 |
+
continue
|
| 299 |
+
f = getattr(a, attrib)
|
| 300 |
+
if isinstance(f, collections.abc.Callable):
|
| 301 |
+
# reset contents of a
|
| 302 |
+
a.astype('f8')
|
| 303 |
+
a.fill(1.0)
|
| 304 |
+
args = methodargs.get(attrib, ())
|
| 305 |
+
b = f(*args)
|
| 306 |
+
assert_(type(b) is matrix, f"{attrib}")
|
| 307 |
+
assert_(type(a.real) is matrix)
|
| 308 |
+
assert_(type(a.imag) is matrix)
|
| 309 |
+
c, d = matrix([0.0]).nonzero()
|
| 310 |
+
assert_(type(c) is np.ndarray)
|
| 311 |
+
assert_(type(d) is np.ndarray)
|
| 312 |
+
|
| 313 |
+
|
| 314 |
+
class TestIndexing:
|
| 315 |
+
def test_basic(self):
|
| 316 |
+
x = asmatrix(np.zeros((3, 2), float))
|
| 317 |
+
y = np.zeros((3, 1), float)
|
| 318 |
+
y[:, 0] = [0.8, 0.2, 0.3]
|
| 319 |
+
x[:, 1] = y > 0.5
|
| 320 |
+
assert_equal(x, [[0, 1], [0, 0], [0, 0]])
|
| 321 |
+
|
| 322 |
+
|
| 323 |
+
class TestNewScalarIndexing:
|
| 324 |
+
a = matrix([[1, 2], [3, 4]])
|
| 325 |
+
|
| 326 |
+
def test_dimesions(self):
|
| 327 |
+
a = self.a
|
| 328 |
+
x = a[0]
|
| 329 |
+
assert_equal(x.ndim, 2)
|
| 330 |
+
|
| 331 |
+
def test_array_from_matrix_list(self):
|
| 332 |
+
a = self.a
|
| 333 |
+
x = np.array([a, a])
|
| 334 |
+
assert_equal(x.shape, [2, 2, 2])
|
| 335 |
+
|
| 336 |
+
def test_array_to_list(self):
|
| 337 |
+
a = self.a
|
| 338 |
+
assert_equal(a.tolist(), [[1, 2], [3, 4]])
|
| 339 |
+
|
| 340 |
+
def test_fancy_indexing(self):
|
| 341 |
+
a = self.a
|
| 342 |
+
x = a[1, [0, 1, 0]]
|
| 343 |
+
assert_(isinstance(x, matrix))
|
| 344 |
+
assert_equal(x, matrix([[3, 4, 3]]))
|
| 345 |
+
x = a[[1, 0]]
|
| 346 |
+
assert_(isinstance(x, matrix))
|
| 347 |
+
assert_equal(x, matrix([[3, 4], [1, 2]]))
|
| 348 |
+
x = a[[[1], [0]], [[1, 0], [0, 1]]]
|
| 349 |
+
assert_(isinstance(x, matrix))
|
| 350 |
+
assert_equal(x, matrix([[4, 3], [1, 2]]))
|
| 351 |
+
|
| 352 |
+
def test_matrix_element(self):
|
| 353 |
+
x = matrix([[1, 2, 3], [4, 5, 6]])
|
| 354 |
+
assert_equal(x[0][0], matrix([[1, 2, 3]]))
|
| 355 |
+
assert_equal(x[0][0].shape, (1, 3))
|
| 356 |
+
assert_equal(x[0].shape, (1, 3))
|
| 357 |
+
assert_equal(x[:, 0].shape, (2, 1))
|
| 358 |
+
|
| 359 |
+
x = matrix(0)
|
| 360 |
+
assert_equal(x[0, 0], 0)
|
| 361 |
+
assert_equal(x[0], 0)
|
| 362 |
+
assert_equal(x[:, 0].shape, x.shape)
|
| 363 |
+
|
| 364 |
+
def test_scalar_indexing(self):
|
| 365 |
+
x = asmatrix(np.zeros((3, 2), float))
|
| 366 |
+
assert_equal(x[0, 0], x[0][0])
|
| 367 |
+
|
| 368 |
+
def test_row_column_indexing(self):
|
| 369 |
+
x = asmatrix(np.eye(2))
|
| 370 |
+
assert_array_equal(x[0, :], [[1, 0]])
|
| 371 |
+
assert_array_equal(x[1, :], [[0, 1]])
|
| 372 |
+
assert_array_equal(x[:, 0], [[1], [0]])
|
| 373 |
+
assert_array_equal(x[:, 1], [[0], [1]])
|
| 374 |
+
|
| 375 |
+
def test_boolean_indexing(self):
|
| 376 |
+
A = np.arange(6)
|
| 377 |
+
A.shape = (3, 2)
|
| 378 |
+
x = asmatrix(A)
|
| 379 |
+
assert_array_equal(x[:, np.array([True, False])], x[:, 0])
|
| 380 |
+
assert_array_equal(x[np.array([True, False, False]), :], x[0, :])
|
| 381 |
+
|
| 382 |
+
def test_list_indexing(self):
|
| 383 |
+
A = np.arange(6)
|
| 384 |
+
A.shape = (3, 2)
|
| 385 |
+
x = asmatrix(A)
|
| 386 |
+
assert_array_equal(x[:, [1, 0]], x[:, ::-1])
|
| 387 |
+
assert_array_equal(x[[2, 1, 0], :], x[::-1, :])
|
| 388 |
+
|
| 389 |
+
|
| 390 |
+
class TestPower:
|
| 391 |
+
def test_returntype(self):
|
| 392 |
+
a = np.array([[0, 1], [0, 0]])
|
| 393 |
+
assert_(type(matrix_power(a, 2)) is np.ndarray)
|
| 394 |
+
a = asmatrix(a)
|
| 395 |
+
assert_(type(matrix_power(a, 2)) is matrix)
|
| 396 |
+
|
| 397 |
+
def test_list(self):
|
| 398 |
+
assert_array_equal(matrix_power([[0, 1], [0, 0]], 2), [[0, 0], [0, 0]])
|
| 399 |
+
|
| 400 |
+
|
| 401 |
+
class TestShape:
|
| 402 |
+
|
| 403 |
+
a = np.array([[1], [2]])
|
| 404 |
+
m = matrix([[1], [2]])
|
| 405 |
+
|
| 406 |
+
def test_shape(self):
|
| 407 |
+
assert_equal(self.a.shape, (2, 1))
|
| 408 |
+
assert_equal(self.m.shape, (2, 1))
|
| 409 |
+
|
| 410 |
+
def test_numpy_ravel(self):
|
| 411 |
+
assert_equal(np.ravel(self.a).shape, (2,))
|
| 412 |
+
assert_equal(np.ravel(self.m).shape, (2,))
|
| 413 |
+
|
| 414 |
+
def test_member_ravel(self):
|
| 415 |
+
assert_equal(self.a.ravel().shape, (2,))
|
| 416 |
+
assert_equal(self.m.ravel().shape, (1, 2))
|
| 417 |
+
|
| 418 |
+
def test_member_flatten(self):
|
| 419 |
+
assert_equal(self.a.flatten().shape, (2,))
|
| 420 |
+
assert_equal(self.m.flatten().shape, (1, 2))
|
| 421 |
+
|
| 422 |
+
def test_numpy_ravel_order(self):
|
| 423 |
+
x = np.array([[1, 2, 3], [4, 5, 6]])
|
| 424 |
+
assert_equal(np.ravel(x), [1, 2, 3, 4, 5, 6])
|
| 425 |
+
assert_equal(np.ravel(x, order='F'), [1, 4, 2, 5, 3, 6])
|
| 426 |
+
assert_equal(np.ravel(x.T), [1, 4, 2, 5, 3, 6])
|
| 427 |
+
assert_equal(np.ravel(x.T, order='A'), [1, 2, 3, 4, 5, 6])
|
| 428 |
+
x = matrix([[1, 2, 3], [4, 5, 6]])
|
| 429 |
+
assert_equal(np.ravel(x), [1, 2, 3, 4, 5, 6])
|
| 430 |
+
assert_equal(np.ravel(x, order='F'), [1, 4, 2, 5, 3, 6])
|
| 431 |
+
assert_equal(np.ravel(x.T), [1, 4, 2, 5, 3, 6])
|
| 432 |
+
assert_equal(np.ravel(x.T, order='A'), [1, 2, 3, 4, 5, 6])
|
| 433 |
+
|
| 434 |
+
def test_matrix_ravel_order(self):
|
| 435 |
+
x = matrix([[1, 2, 3], [4, 5, 6]])
|
| 436 |
+
assert_equal(x.ravel(), [[1, 2, 3, 4, 5, 6]])
|
| 437 |
+
assert_equal(x.ravel(order='F'), [[1, 4, 2, 5, 3, 6]])
|
| 438 |
+
assert_equal(x.T.ravel(), [[1, 4, 2, 5, 3, 6]])
|
| 439 |
+
assert_equal(x.T.ravel(order='A'), [[1, 2, 3, 4, 5, 6]])
|
| 440 |
+
|
| 441 |
+
def test_array_memory_sharing(self):
|
| 442 |
+
assert_(np.may_share_memory(self.a, self.a.ravel()))
|
| 443 |
+
assert_(not np.may_share_memory(self.a, self.a.flatten()))
|
| 444 |
+
|
| 445 |
+
def test_matrix_memory_sharing(self):
|
| 446 |
+
assert_(np.may_share_memory(self.m, self.m.ravel()))
|
| 447 |
+
assert_(not np.may_share_memory(self.m, self.m.flatten()))
|
| 448 |
+
|
| 449 |
+
def test_expand_dims_matrix(self):
|
| 450 |
+
# matrices are always 2d - so expand_dims only makes sense when the
|
| 451 |
+
# type is changed away from matrix.
|
| 452 |
+
a = np.arange(10).reshape((2, 5)).view(np.matrix)
|
| 453 |
+
expanded = np.expand_dims(a, axis=1)
|
| 454 |
+
assert_equal(expanded.ndim, 3)
|
| 455 |
+
assert_(not isinstance(expanded, np.matrix))
|
venv/lib/python3.13/site-packages/numpy/matrixlib/tests/test_interaction.py
ADDED
|
@@ -0,0 +1,360 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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
| 3 |
+
Note that tests with MaskedArray and linalg are done in separate files.
|
| 4 |
+
"""
|
| 5 |
+
import textwrap
|
| 6 |
+
import warnings
|
| 7 |
+
|
| 8 |
+
import pytest
|
| 9 |
+
|
| 10 |
+
import numpy as np
|
| 11 |
+
from numpy.testing import (
|
| 12 |
+
assert_,
|
| 13 |
+
assert_almost_equal,
|
| 14 |
+
assert_array_almost_equal,
|
| 15 |
+
assert_array_equal,
|
| 16 |
+
assert_equal,
|
| 17 |
+
assert_raises,
|
| 18 |
+
assert_raises_regex,
|
| 19 |
+
)
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
def test_fancy_indexing():
|
| 23 |
+
# The matrix class messes with the shape. While this is always
|
| 24 |
+
# weird (getitem is not used, it does not have setitem nor knows
|
| 25 |
+
# about fancy indexing), this tests gh-3110
|
| 26 |
+
# 2018-04-29: moved here from core.tests.test_index.
|
| 27 |
+
m = np.matrix([[1, 2], [3, 4]])
|
| 28 |
+
|
| 29 |
+
assert_(isinstance(m[[0, 1, 0], :], np.matrix))
|
| 30 |
+
|
| 31 |
+
# gh-3110. Note the transpose currently because matrices do *not*
|
| 32 |
+
# support dimension fixing for fancy indexing correctly.
|
| 33 |
+
x = np.asmatrix(np.arange(50).reshape(5, 10))
|
| 34 |
+
assert_equal(x[:2, np.array(-1)], x[:2, -1].T)
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
def test_polynomial_mapdomain():
|
| 38 |
+
# test that polynomial preserved matrix subtype.
|
| 39 |
+
# 2018-04-29: moved here from polynomial.tests.polyutils.
|
| 40 |
+
dom1 = [0, 4]
|
| 41 |
+
dom2 = [1, 3]
|
| 42 |
+
x = np.matrix([dom1, dom1])
|
| 43 |
+
res = np.polynomial.polyutils.mapdomain(x, dom1, dom2)
|
| 44 |
+
assert_(isinstance(res, np.matrix))
|
| 45 |
+
|
| 46 |
+
|
| 47 |
+
def test_sort_matrix_none():
|
| 48 |
+
# 2018-04-29: moved here from core.tests.test_multiarray
|
| 49 |
+
a = np.matrix([[2, 1, 0]])
|
| 50 |
+
actual = np.sort(a, axis=None)
|
| 51 |
+
expected = np.matrix([[0, 1, 2]])
|
| 52 |
+
assert_equal(actual, expected)
|
| 53 |
+
assert_(type(expected) is np.matrix)
|
| 54 |
+
|
| 55 |
+
|
| 56 |
+
def test_partition_matrix_none():
|
| 57 |
+
# gh-4301
|
| 58 |
+
# 2018-04-29: moved here from core.tests.test_multiarray
|
| 59 |
+
a = np.matrix([[2, 1, 0]])
|
| 60 |
+
actual = np.partition(a, 1, axis=None)
|
| 61 |
+
expected = np.matrix([[0, 1, 2]])
|
| 62 |
+
assert_equal(actual, expected)
|
| 63 |
+
assert_(type(expected) is np.matrix)
|
| 64 |
+
|
| 65 |
+
|
| 66 |
+
def test_dot_scalar_and_matrix_of_objects():
|
| 67 |
+
# Ticket #2469
|
| 68 |
+
# 2018-04-29: moved here from core.tests.test_multiarray
|
| 69 |
+
arr = np.matrix([1, 2], dtype=object)
|
| 70 |
+
desired = np.matrix([[3, 6]], dtype=object)
|
| 71 |
+
assert_equal(np.dot(arr, 3), desired)
|
| 72 |
+
assert_equal(np.dot(3, arr), desired)
|
| 73 |
+
|
| 74 |
+
|
| 75 |
+
def test_inner_scalar_and_matrix():
|
| 76 |
+
# 2018-04-29: moved here from core.tests.test_multiarray
|
| 77 |
+
for dt in np.typecodes['AllInteger'] + np.typecodes['AllFloat'] + '?':
|
| 78 |
+
sca = np.array(3, dtype=dt)[()]
|
| 79 |
+
arr = np.matrix([[1, 2], [3, 4]], dtype=dt)
|
| 80 |
+
desired = np.matrix([[3, 6], [9, 12]], dtype=dt)
|
| 81 |
+
assert_equal(np.inner(arr, sca), desired)
|
| 82 |
+
assert_equal(np.inner(sca, arr), desired)
|
| 83 |
+
|
| 84 |
+
|
| 85 |
+
def test_inner_scalar_and_matrix_of_objects():
|
| 86 |
+
# Ticket #4482
|
| 87 |
+
# 2018-04-29: moved here from core.tests.test_multiarray
|
| 88 |
+
arr = np.matrix([1, 2], dtype=object)
|
| 89 |
+
desired = np.matrix([[3, 6]], dtype=object)
|
| 90 |
+
assert_equal(np.inner(arr, 3), desired)
|
| 91 |
+
assert_equal(np.inner(3, arr), desired)
|
| 92 |
+
|
| 93 |
+
|
| 94 |
+
def test_iter_allocate_output_subtype():
|
| 95 |
+
# Make sure that the subtype with priority wins
|
| 96 |
+
# 2018-04-29: moved here from core.tests.test_nditer, given the
|
| 97 |
+
# matrix specific shape test.
|
| 98 |
+
|
| 99 |
+
# matrix vs ndarray
|
| 100 |
+
a = np.matrix([[1, 2], [3, 4]])
|
| 101 |
+
b = np.arange(4).reshape(2, 2).T
|
| 102 |
+
i = np.nditer([a, b, None], [],
|
| 103 |
+
[['readonly'], ['readonly'], ['writeonly', 'allocate']])
|
| 104 |
+
assert_(type(i.operands[2]) is np.matrix)
|
| 105 |
+
assert_(type(i.operands[2]) is not np.ndarray)
|
| 106 |
+
assert_equal(i.operands[2].shape, (2, 2))
|
| 107 |
+
|
| 108 |
+
# matrix always wants things to be 2D
|
| 109 |
+
b = np.arange(4).reshape(1, 2, 2)
|
| 110 |
+
assert_raises(RuntimeError, np.nditer, [a, b, None], [],
|
| 111 |
+
[['readonly'], ['readonly'], ['writeonly', 'allocate']])
|
| 112 |
+
# but if subtypes are disabled, the result can still work
|
| 113 |
+
i = np.nditer([a, b, None], [],
|
| 114 |
+
[['readonly'], ['readonly'],
|
| 115 |
+
['writeonly', 'allocate', 'no_subtype']])
|
| 116 |
+
assert_(type(i.operands[2]) is np.ndarray)
|
| 117 |
+
assert_(type(i.operands[2]) is not np.matrix)
|
| 118 |
+
assert_equal(i.operands[2].shape, (1, 2, 2))
|
| 119 |
+
|
| 120 |
+
|
| 121 |
+
def like_function():
|
| 122 |
+
# 2018-04-29: moved here from core.tests.test_numeric
|
| 123 |
+
a = np.matrix([[1, 2], [3, 4]])
|
| 124 |
+
for like_function in np.zeros_like, np.ones_like, np.empty_like:
|
| 125 |
+
b = like_function(a)
|
| 126 |
+
assert_(type(b) is np.matrix)
|
| 127 |
+
|
| 128 |
+
c = like_function(a, subok=False)
|
| 129 |
+
assert_(type(c) is not np.matrix)
|
| 130 |
+
|
| 131 |
+
|
| 132 |
+
def test_array_astype():
|
| 133 |
+
# 2018-04-29: copied here from core.tests.test_api
|
| 134 |
+
# subok=True passes through a matrix
|
| 135 |
+
a = np.matrix([[0, 1, 2], [3, 4, 5]], dtype='f4')
|
| 136 |
+
b = a.astype('f4', subok=True, copy=False)
|
| 137 |
+
assert_(a is b)
|
| 138 |
+
|
| 139 |
+
# subok=True is default, and creates a subtype on a cast
|
| 140 |
+
b = a.astype('i4', copy=False)
|
| 141 |
+
assert_equal(a, b)
|
| 142 |
+
assert_equal(type(b), np.matrix)
|
| 143 |
+
|
| 144 |
+
# subok=False never returns a matrix
|
| 145 |
+
b = a.astype('f4', subok=False, copy=False)
|
| 146 |
+
assert_equal(a, b)
|
| 147 |
+
assert_(not (a is b))
|
| 148 |
+
assert_(type(b) is not np.matrix)
|
| 149 |
+
|
| 150 |
+
|
| 151 |
+
def test_stack():
|
| 152 |
+
# 2018-04-29: copied here from core.tests.test_shape_base
|
| 153 |
+
# check np.matrix cannot be stacked
|
| 154 |
+
m = np.matrix([[1, 2], [3, 4]])
|
| 155 |
+
assert_raises_regex(ValueError, 'shape too large to be a matrix',
|
| 156 |
+
np.stack, [m, m])
|
| 157 |
+
|
| 158 |
+
|
| 159 |
+
def test_object_scalar_multiply():
|
| 160 |
+
# Tickets #2469 and #4482
|
| 161 |
+
# 2018-04-29: moved here from core.tests.test_ufunc
|
| 162 |
+
arr = np.matrix([1, 2], dtype=object)
|
| 163 |
+
desired = np.matrix([[3, 6]], dtype=object)
|
| 164 |
+
assert_equal(np.multiply(arr, 3), desired)
|
| 165 |
+
assert_equal(np.multiply(3, arr), desired)
|
| 166 |
+
|
| 167 |
+
|
| 168 |
+
def test_nanfunctions_matrices():
|
| 169 |
+
# Check that it works and that type and
|
| 170 |
+
# shape are preserved
|
| 171 |
+
# 2018-04-29: moved here from core.tests.test_nanfunctions
|
| 172 |
+
mat = np.matrix(np.eye(3))
|
| 173 |
+
for f in [np.nanmin, np.nanmax]:
|
| 174 |
+
res = f(mat, axis=0)
|
| 175 |
+
assert_(isinstance(res, np.matrix))
|
| 176 |
+
assert_(res.shape == (1, 3))
|
| 177 |
+
res = f(mat, axis=1)
|
| 178 |
+
assert_(isinstance(res, np.matrix))
|
| 179 |
+
assert_(res.shape == (3, 1))
|
| 180 |
+
res = f(mat)
|
| 181 |
+
assert_(np.isscalar(res))
|
| 182 |
+
# check that rows of nan are dealt with for subclasses (#4628)
|
| 183 |
+
mat[1] = np.nan
|
| 184 |
+
for f in [np.nanmin, np.nanmax]:
|
| 185 |
+
with warnings.catch_warnings(record=True) as w:
|
| 186 |
+
warnings.simplefilter('always')
|
| 187 |
+
res = f(mat, axis=0)
|
| 188 |
+
assert_(isinstance(res, np.matrix))
|
| 189 |
+
assert_(not np.any(np.isnan(res)))
|
| 190 |
+
assert_(len(w) == 0)
|
| 191 |
+
|
| 192 |
+
with warnings.catch_warnings(record=True) as w:
|
| 193 |
+
warnings.simplefilter('always')
|
| 194 |
+
res = f(mat, axis=1)
|
| 195 |
+
assert_(isinstance(res, np.matrix))
|
| 196 |
+
assert_(np.isnan(res[1, 0]) and not np.isnan(res[0, 0])
|
| 197 |
+
and not np.isnan(res[2, 0]))
|
| 198 |
+
assert_(len(w) == 1, 'no warning raised')
|
| 199 |
+
assert_(issubclass(w[0].category, RuntimeWarning))
|
| 200 |
+
|
| 201 |
+
with warnings.catch_warnings(record=True) as w:
|
| 202 |
+
warnings.simplefilter('always')
|
| 203 |
+
res = f(mat)
|
| 204 |
+
assert_(np.isscalar(res))
|
| 205 |
+
assert_(res != np.nan)
|
| 206 |
+
assert_(len(w) == 0)
|
| 207 |
+
|
| 208 |
+
|
| 209 |
+
def test_nanfunctions_matrices_general():
|
| 210 |
+
# Check that it works and that type and
|
| 211 |
+
# shape are preserved
|
| 212 |
+
# 2018-04-29: moved here from core.tests.test_nanfunctions
|
| 213 |
+
mat = np.matrix(np.eye(3))
|
| 214 |
+
for f in (np.nanargmin, np.nanargmax, np.nansum, np.nanprod,
|
| 215 |
+
np.nanmean, np.nanvar, np.nanstd):
|
| 216 |
+
res = f(mat, axis=0)
|
| 217 |
+
assert_(isinstance(res, np.matrix))
|
| 218 |
+
assert_(res.shape == (1, 3))
|
| 219 |
+
res = f(mat, axis=1)
|
| 220 |
+
assert_(isinstance(res, np.matrix))
|
| 221 |
+
assert_(res.shape == (3, 1))
|
| 222 |
+
res = f(mat)
|
| 223 |
+
assert_(np.isscalar(res))
|
| 224 |
+
|
| 225 |
+
for f in np.nancumsum, np.nancumprod:
|
| 226 |
+
res = f(mat, axis=0)
|
| 227 |
+
assert_(isinstance(res, np.matrix))
|
| 228 |
+
assert_(res.shape == (3, 3))
|
| 229 |
+
res = f(mat, axis=1)
|
| 230 |
+
assert_(isinstance(res, np.matrix))
|
| 231 |
+
assert_(res.shape == (3, 3))
|
| 232 |
+
res = f(mat)
|
| 233 |
+
assert_(isinstance(res, np.matrix))
|
| 234 |
+
assert_(res.shape == (1, 3 * 3))
|
| 235 |
+
|
| 236 |
+
|
| 237 |
+
def test_average_matrix():
|
| 238 |
+
# 2018-04-29: moved here from core.tests.test_function_base.
|
| 239 |
+
y = np.matrix(np.random.rand(5, 5))
|
| 240 |
+
assert_array_equal(y.mean(0), np.average(y, 0))
|
| 241 |
+
|
| 242 |
+
a = np.matrix([[1, 2], [3, 4]])
|
| 243 |
+
w = np.matrix([[1, 2], [3, 4]])
|
| 244 |
+
|
| 245 |
+
r = np.average(a, axis=0, weights=w)
|
| 246 |
+
assert_equal(type(r), np.matrix)
|
| 247 |
+
assert_equal(r, [[2.5, 10.0 / 3]])
|
| 248 |
+
|
| 249 |
+
|
| 250 |
+
def test_dot_matrix():
|
| 251 |
+
# Test to make sure matrices give the same answer as ndarrays
|
| 252 |
+
# 2018-04-29: moved here from core.tests.test_function_base.
|
| 253 |
+
x = np.linspace(0, 5)
|
| 254 |
+
y = np.linspace(-5, 0)
|
| 255 |
+
mx = np.matrix(x)
|
| 256 |
+
my = np.matrix(y)
|
| 257 |
+
r = np.dot(x, y)
|
| 258 |
+
mr = np.dot(mx, my.T)
|
| 259 |
+
assert_almost_equal(mr, r)
|
| 260 |
+
|
| 261 |
+
|
| 262 |
+
def test_ediff1d_matrix():
|
| 263 |
+
# 2018-04-29: moved here from core.tests.test_arraysetops.
|
| 264 |
+
assert isinstance(np.ediff1d(np.matrix(1)), np.matrix)
|
| 265 |
+
assert isinstance(np.ediff1d(np.matrix(1), to_begin=1), np.matrix)
|
| 266 |
+
|
| 267 |
+
|
| 268 |
+
def test_apply_along_axis_matrix():
|
| 269 |
+
# this test is particularly malicious because matrix
|
| 270 |
+
# refuses to become 1d
|
| 271 |
+
# 2018-04-29: moved here from core.tests.test_shape_base.
|
| 272 |
+
def double(row):
|
| 273 |
+
return row * 2
|
| 274 |
+
|
| 275 |
+
m = np.matrix([[0, 1], [2, 3]])
|
| 276 |
+
expected = np.matrix([[0, 2], [4, 6]])
|
| 277 |
+
|
| 278 |
+
result = np.apply_along_axis(double, 0, m)
|
| 279 |
+
assert_(isinstance(result, np.matrix))
|
| 280 |
+
assert_array_equal(result, expected)
|
| 281 |
+
|
| 282 |
+
result = np.apply_along_axis(double, 1, m)
|
| 283 |
+
assert_(isinstance(result, np.matrix))
|
| 284 |
+
assert_array_equal(result, expected)
|
| 285 |
+
|
| 286 |
+
|
| 287 |
+
def test_kron_matrix():
|
| 288 |
+
# 2018-04-29: moved here from core.tests.test_shape_base.
|
| 289 |
+
a = np.ones([2, 2])
|
| 290 |
+
m = np.asmatrix(a)
|
| 291 |
+
assert_equal(type(np.kron(a, a)), np.ndarray)
|
| 292 |
+
assert_equal(type(np.kron(m, m)), np.matrix)
|
| 293 |
+
assert_equal(type(np.kron(a, m)), np.matrix)
|
| 294 |
+
assert_equal(type(np.kron(m, a)), np.matrix)
|
| 295 |
+
|
| 296 |
+
|
| 297 |
+
class TestConcatenatorMatrix:
|
| 298 |
+
# 2018-04-29: moved here from core.tests.test_index_tricks.
|
| 299 |
+
def test_matrix(self):
|
| 300 |
+
a = [1, 2]
|
| 301 |
+
b = [3, 4]
|
| 302 |
+
|
| 303 |
+
ab_r = np.r_['r', a, b]
|
| 304 |
+
ab_c = np.r_['c', a, b]
|
| 305 |
+
|
| 306 |
+
assert_equal(type(ab_r), np.matrix)
|
| 307 |
+
assert_equal(type(ab_c), np.matrix)
|
| 308 |
+
|
| 309 |
+
assert_equal(np.array(ab_r), [[1, 2, 3, 4]])
|
| 310 |
+
assert_equal(np.array(ab_c), [[1], [2], [3], [4]])
|
| 311 |
+
|
| 312 |
+
assert_raises(ValueError, lambda: np.r_['rc', a, b])
|
| 313 |
+
|
| 314 |
+
def test_matrix_scalar(self):
|
| 315 |
+
r = np.r_['r', [1, 2], 3]
|
| 316 |
+
assert_equal(type(r), np.matrix)
|
| 317 |
+
assert_equal(np.array(r), [[1, 2, 3]])
|
| 318 |
+
|
| 319 |
+
def test_matrix_builder(self):
|
| 320 |
+
a = np.array([1])
|
| 321 |
+
b = np.array([2])
|
| 322 |
+
c = np.array([3])
|
| 323 |
+
d = np.array([4])
|
| 324 |
+
actual = np.r_['a, b; c, d']
|
| 325 |
+
expected = np.bmat([[a, b], [c, d]])
|
| 326 |
+
|
| 327 |
+
assert_equal(actual, expected)
|
| 328 |
+
assert_equal(type(actual), type(expected))
|
| 329 |
+
|
| 330 |
+
|
| 331 |
+
def test_array_equal_error_message_matrix():
|
| 332 |
+
# 2018-04-29: moved here from testing.tests.test_utils.
|
| 333 |
+
with pytest.raises(AssertionError) as exc_info:
|
| 334 |
+
assert_equal(np.array([1, 2]), np.matrix([1, 2]))
|
| 335 |
+
msg = str(exc_info.value)
|
| 336 |
+
msg_reference = textwrap.dedent("""\
|
| 337 |
+
|
| 338 |
+
Arrays are not equal
|
| 339 |
+
|
| 340 |
+
(shapes (2,), (1, 2) mismatch)
|
| 341 |
+
ACTUAL: array([1, 2])
|
| 342 |
+
DESIRED: matrix([[1, 2]])""")
|
| 343 |
+
assert_equal(msg, msg_reference)
|
| 344 |
+
|
| 345 |
+
|
| 346 |
+
def test_array_almost_equal_matrix():
|
| 347 |
+
# Matrix slicing keeps things 2-D, while array does not necessarily.
|
| 348 |
+
# See gh-8452.
|
| 349 |
+
# 2018-04-29: moved here from testing.tests.test_utils.
|
| 350 |
+
m1 = np.matrix([[1., 2.]])
|
| 351 |
+
m2 = np.matrix([[1., np.nan]])
|
| 352 |
+
m3 = np.matrix([[1., -np.inf]])
|
| 353 |
+
m4 = np.matrix([[np.nan, np.inf]])
|
| 354 |
+
m5 = np.matrix([[1., 2.], [np.nan, np.inf]])
|
| 355 |
+
for assert_func in assert_array_almost_equal, assert_almost_equal:
|
| 356 |
+
for m in m1, m2, m3, m4, m5:
|
| 357 |
+
assert_func(m, m)
|
| 358 |
+
a = np.array(m)
|
| 359 |
+
assert_func(a, m)
|
| 360 |
+
assert_func(m, a)
|
venv/lib/python3.13/site-packages/numpy/matrixlib/tests/test_masked_matrix.py
ADDED
|
@@ -0,0 +1,240 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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
| 3 |
+
import numpy as np
|
| 4 |
+
from numpy.ma.core import (
|
| 5 |
+
MaskedArray,
|
| 6 |
+
MaskType,
|
| 7 |
+
add,
|
| 8 |
+
allequal,
|
| 9 |
+
divide,
|
| 10 |
+
getmask,
|
| 11 |
+
hypot,
|
| 12 |
+
log,
|
| 13 |
+
masked,
|
| 14 |
+
masked_array,
|
| 15 |
+
masked_values,
|
| 16 |
+
nomask,
|
| 17 |
+
)
|
| 18 |
+
from numpy.ma.extras import mr_
|
| 19 |
+
from numpy.ma.testutils import assert_, assert_array_equal, assert_equal, assert_raises
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
class MMatrix(MaskedArray, np.matrix,):
|
| 23 |
+
|
| 24 |
+
def __new__(cls, data, mask=nomask):
|
| 25 |
+
mat = np.matrix(data)
|
| 26 |
+
_data = MaskedArray.__new__(cls, data=mat, mask=mask)
|
| 27 |
+
return _data
|
| 28 |
+
|
| 29 |
+
def __array_finalize__(self, obj):
|
| 30 |
+
np.matrix.__array_finalize__(self, obj)
|
| 31 |
+
MaskedArray.__array_finalize__(self, obj)
|
| 32 |
+
|
| 33 |
+
@property
|
| 34 |
+
def _series(self):
|
| 35 |
+
_view = self.view(MaskedArray)
|
| 36 |
+
_view._sharedmask = False
|
| 37 |
+
return _view
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
class TestMaskedMatrix:
|
| 41 |
+
def test_matrix_indexing(self):
|
| 42 |
+
# Tests conversions and indexing
|
| 43 |
+
x1 = np.matrix([[1, 2, 3], [4, 3, 2]])
|
| 44 |
+
x2 = masked_array(x1, mask=[[1, 0, 0], [0, 1, 0]])
|
| 45 |
+
x3 = masked_array(x1, mask=[[0, 1, 0], [1, 0, 0]])
|
| 46 |
+
x4 = masked_array(x1)
|
| 47 |
+
# test conversion to strings
|
| 48 |
+
str(x2) # raises?
|
| 49 |
+
repr(x2) # raises?
|
| 50 |
+
# tests of indexing
|
| 51 |
+
assert_(type(x2[1, 0]) is type(x1[1, 0]))
|
| 52 |
+
assert_(x1[1, 0] == x2[1, 0])
|
| 53 |
+
assert_(x2[1, 1] is masked)
|
| 54 |
+
assert_equal(x1[0, 2], x2[0, 2])
|
| 55 |
+
assert_equal(x1[0, 1:], x2[0, 1:])
|
| 56 |
+
assert_equal(x1[:, 2], x2[:, 2])
|
| 57 |
+
assert_equal(x1[:], x2[:])
|
| 58 |
+
assert_equal(x1[1:], x3[1:])
|
| 59 |
+
x1[0, 2] = 9
|
| 60 |
+
x2[0, 2] = 9
|
| 61 |
+
assert_equal(x1, x2)
|
| 62 |
+
x1[0, 1:] = 99
|
| 63 |
+
x2[0, 1:] = 99
|
| 64 |
+
assert_equal(x1, x2)
|
| 65 |
+
x2[0, 1] = masked
|
| 66 |
+
assert_equal(x1, x2)
|
| 67 |
+
x2[0, 1:] = masked
|
| 68 |
+
assert_equal(x1, x2)
|
| 69 |
+
x2[0, :] = x1[0, :]
|
| 70 |
+
x2[0, 1] = masked
|
| 71 |
+
assert_(allequal(getmask(x2), np.array([[0, 1, 0], [0, 1, 0]])))
|
| 72 |
+
x3[1, :] = masked_array([1, 2, 3], [1, 1, 0])
|
| 73 |
+
assert_(allequal(getmask(x3)[1], masked_array([1, 1, 0])))
|
| 74 |
+
assert_(allequal(getmask(x3[1]), masked_array([1, 1, 0])))
|
| 75 |
+
x4[1, :] = masked_array([1, 2, 3], [1, 1, 0])
|
| 76 |
+
assert_(allequal(getmask(x4[1]), masked_array([1, 1, 0])))
|
| 77 |
+
assert_(allequal(x4[1], masked_array([1, 2, 3])))
|
| 78 |
+
x1 = np.matrix(np.arange(5) * 1.0)
|
| 79 |
+
x2 = masked_values(x1, 3.0)
|
| 80 |
+
assert_equal(x1, x2)
|
| 81 |
+
assert_(allequal(masked_array([0, 0, 0, 1, 0], dtype=MaskType),
|
| 82 |
+
x2.mask))
|
| 83 |
+
assert_equal(3.0, x2.fill_value)
|
| 84 |
+
|
| 85 |
+
def test_pickling_subbaseclass(self):
|
| 86 |
+
# Test pickling w/ a subclass of ndarray
|
| 87 |
+
a = masked_array(np.matrix(list(range(10))), mask=[1, 0, 1, 0, 0] * 2)
|
| 88 |
+
for proto in range(2, pickle.HIGHEST_PROTOCOL + 1):
|
| 89 |
+
a_pickled = pickle.loads(pickle.dumps(a, protocol=proto))
|
| 90 |
+
assert_equal(a_pickled._mask, a._mask)
|
| 91 |
+
assert_equal(a_pickled, a)
|
| 92 |
+
assert_(isinstance(a_pickled._data, np.matrix))
|
| 93 |
+
|
| 94 |
+
def test_count_mean_with_matrix(self):
|
| 95 |
+
m = masked_array(np.matrix([[1, 2], [3, 4]]), mask=np.zeros((2, 2)))
|
| 96 |
+
|
| 97 |
+
assert_equal(m.count(axis=0).shape, (1, 2))
|
| 98 |
+
assert_equal(m.count(axis=1).shape, (2, 1))
|
| 99 |
+
|
| 100 |
+
# Make sure broadcasting inside mean and var work
|
| 101 |
+
assert_equal(m.mean(axis=0), [[2., 3.]])
|
| 102 |
+
assert_equal(m.mean(axis=1), [[1.5], [3.5]])
|
| 103 |
+
|
| 104 |
+
def test_flat(self):
|
| 105 |
+
# Test that flat can return items even for matrices [#4585, #4615]
|
| 106 |
+
# test simple access
|
| 107 |
+
test = masked_array(np.matrix([[1, 2, 3]]), mask=[0, 0, 1])
|
| 108 |
+
assert_equal(test.flat[1], 2)
|
| 109 |
+
assert_equal(test.flat[2], masked)
|
| 110 |
+
assert_(np.all(test.flat[0:2] == test[0, 0:2]))
|
| 111 |
+
# Test flat on masked_matrices
|
| 112 |
+
test = masked_array(np.matrix([[1, 2, 3]]), mask=[0, 0, 1])
|
| 113 |
+
test.flat = masked_array([3, 2, 1], mask=[1, 0, 0])
|
| 114 |
+
control = masked_array(np.matrix([[3, 2, 1]]), mask=[1, 0, 0])
|
| 115 |
+
assert_equal(test, control)
|
| 116 |
+
# Test setting
|
| 117 |
+
test = masked_array(np.matrix([[1, 2, 3]]), mask=[0, 0, 1])
|
| 118 |
+
testflat = test.flat
|
| 119 |
+
testflat[:] = testflat[[2, 1, 0]]
|
| 120 |
+
assert_equal(test, control)
|
| 121 |
+
testflat[0] = 9
|
| 122 |
+
# test that matrices keep the correct shape (#4615)
|
| 123 |
+
a = masked_array(np.matrix(np.eye(2)), mask=0)
|
| 124 |
+
b = a.flat
|
| 125 |
+
b01 = b[:2]
|
| 126 |
+
assert_equal(b01.data, np.array([[1., 0.]]))
|
| 127 |
+
assert_equal(b01.mask, np.array([[False, False]]))
|
| 128 |
+
|
| 129 |
+
def test_allany_onmatrices(self):
|
| 130 |
+
x = np.array([[0.13, 0.26, 0.90],
|
| 131 |
+
[0.28, 0.33, 0.63],
|
| 132 |
+
[0.31, 0.87, 0.70]])
|
| 133 |
+
X = np.matrix(x)
|
| 134 |
+
m = np.array([[True, False, False],
|
| 135 |
+
[False, False, False],
|
| 136 |
+
[True, True, False]], dtype=np.bool)
|
| 137 |
+
mX = masked_array(X, mask=m)
|
| 138 |
+
mXbig = (mX > 0.5)
|
| 139 |
+
mXsmall = (mX < 0.5)
|
| 140 |
+
|
| 141 |
+
assert_(not mXbig.all())
|
| 142 |
+
assert_(mXbig.any())
|
| 143 |
+
assert_equal(mXbig.all(0), np.matrix([False, False, True]))
|
| 144 |
+
assert_equal(mXbig.all(1), np.matrix([False, False, True]).T)
|
| 145 |
+
assert_equal(mXbig.any(0), np.matrix([False, False, True]))
|
| 146 |
+
assert_equal(mXbig.any(1), np.matrix([True, True, True]).T)
|
| 147 |
+
|
| 148 |
+
assert_(not mXsmall.all())
|
| 149 |
+
assert_(mXsmall.any())
|
| 150 |
+
assert_equal(mXsmall.all(0), np.matrix([True, True, False]))
|
| 151 |
+
assert_equal(mXsmall.all(1), np.matrix([False, False, False]).T)
|
| 152 |
+
assert_equal(mXsmall.any(0), np.matrix([True, True, False]))
|
| 153 |
+
assert_equal(mXsmall.any(1), np.matrix([True, True, False]).T)
|
| 154 |
+
|
| 155 |
+
def test_compressed(self):
|
| 156 |
+
a = masked_array(np.matrix([1, 2, 3, 4]), mask=[0, 0, 0, 0])
|
| 157 |
+
b = a.compressed()
|
| 158 |
+
assert_equal(b, a)
|
| 159 |
+
assert_(isinstance(b, np.matrix))
|
| 160 |
+
a[0, 0] = masked
|
| 161 |
+
b = a.compressed()
|
| 162 |
+
assert_equal(b, [[2, 3, 4]])
|
| 163 |
+
|
| 164 |
+
def test_ravel(self):
|
| 165 |
+
a = masked_array(np.matrix([1, 2, 3, 4, 5]), mask=[[0, 1, 0, 0, 0]])
|
| 166 |
+
aravel = a.ravel()
|
| 167 |
+
assert_equal(aravel.shape, (1, 5))
|
| 168 |
+
assert_equal(aravel._mask.shape, a.shape)
|
| 169 |
+
|
| 170 |
+
def test_view(self):
|
| 171 |
+
# Test view w/ flexible dtype
|
| 172 |
+
iterator = list(zip(np.arange(10), np.random.rand(10)))
|
| 173 |
+
data = np.array(iterator)
|
| 174 |
+
a = masked_array(iterator, dtype=[('a', float), ('b', float)])
|
| 175 |
+
a.mask[0] = (1, 0)
|
| 176 |
+
test = a.view((float, 2), np.matrix)
|
| 177 |
+
assert_equal(test, data)
|
| 178 |
+
assert_(isinstance(test, np.matrix))
|
| 179 |
+
assert_(not isinstance(test, MaskedArray))
|
| 180 |
+
|
| 181 |
+
|
| 182 |
+
class TestSubclassing:
|
| 183 |
+
# Test suite for masked subclasses of ndarray.
|
| 184 |
+
|
| 185 |
+
def setup_method(self):
|
| 186 |
+
x = np.arange(5, dtype='float')
|
| 187 |
+
mx = MMatrix(x, mask=[0, 1, 0, 0, 0])
|
| 188 |
+
self.data = (x, mx)
|
| 189 |
+
|
| 190 |
+
def test_maskedarray_subclassing(self):
|
| 191 |
+
# Tests subclassing MaskedArray
|
| 192 |
+
(x, mx) = self.data
|
| 193 |
+
assert_(isinstance(mx._data, np.matrix))
|
| 194 |
+
|
| 195 |
+
def test_masked_unary_operations(self):
|
| 196 |
+
# Tests masked_unary_operation
|
| 197 |
+
(x, mx) = self.data
|
| 198 |
+
with np.errstate(divide='ignore'):
|
| 199 |
+
assert_(isinstance(log(mx), MMatrix))
|
| 200 |
+
assert_equal(log(x), np.log(x))
|
| 201 |
+
|
| 202 |
+
def test_masked_binary_operations(self):
|
| 203 |
+
# Tests masked_binary_operation
|
| 204 |
+
(x, mx) = self.data
|
| 205 |
+
# Result should be a MMatrix
|
| 206 |
+
assert_(isinstance(add(mx, mx), MMatrix))
|
| 207 |
+
assert_(isinstance(add(mx, x), MMatrix))
|
| 208 |
+
# Result should work
|
| 209 |
+
assert_equal(add(mx, x), mx + x)
|
| 210 |
+
assert_(isinstance(add(mx, mx)._data, np.matrix))
|
| 211 |
+
with assert_raises(TypeError):
|
| 212 |
+
add.outer(mx, mx)
|
| 213 |
+
assert_(isinstance(hypot(mx, mx), MMatrix))
|
| 214 |
+
assert_(isinstance(hypot(mx, x), MMatrix))
|
| 215 |
+
|
| 216 |
+
def test_masked_binary_operations2(self):
|
| 217 |
+
# Tests domained_masked_binary_operation
|
| 218 |
+
(x, mx) = self.data
|
| 219 |
+
xmx = masked_array(mx.data.__array__(), mask=mx.mask)
|
| 220 |
+
assert_(isinstance(divide(mx, mx), MMatrix))
|
| 221 |
+
assert_(isinstance(divide(mx, x), MMatrix))
|
| 222 |
+
assert_equal(divide(mx, mx), divide(xmx, xmx))
|
| 223 |
+
|
| 224 |
+
class TestConcatenator:
|
| 225 |
+
# Tests for mr_, the equivalent of r_ for masked arrays.
|
| 226 |
+
|
| 227 |
+
def test_matrix_builder(self):
|
| 228 |
+
assert_raises(np.ma.MAError, lambda: mr_['1, 2; 3, 4'])
|
| 229 |
+
|
| 230 |
+
def test_matrix(self):
|
| 231 |
+
# Test consistency with unmasked version. If we ever deprecate
|
| 232 |
+
# matrix, this test should either still pass, or both actual and
|
| 233 |
+
# expected should fail to be build.
|
| 234 |
+
actual = mr_['r', 1, 2, 3]
|
| 235 |
+
expected = np.ma.array(np.r_['r', 1, 2, 3])
|
| 236 |
+
assert_array_equal(actual, expected)
|
| 237 |
+
|
| 238 |
+
# outer type is masked array, inner type is matrix
|
| 239 |
+
assert_equal(type(actual), type(expected))
|
| 240 |
+
assert_equal(type(actual.data), type(expected.data))
|
venv/lib/python3.13/site-packages/numpy/matrixlib/tests/test_matrix_linalg.py
ADDED
|
@@ -0,0 +1,105 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
""" Test functions for linalg module using the matrix class."""
|
| 2 |
+
import numpy as np
|
| 3 |
+
from numpy.linalg.tests.test_linalg import (
|
| 4 |
+
CondCases,
|
| 5 |
+
DetCases,
|
| 6 |
+
EigCases,
|
| 7 |
+
EigvalsCases,
|
| 8 |
+
InvCases,
|
| 9 |
+
LinalgCase,
|
| 10 |
+
LinalgTestCase,
|
| 11 |
+
LstsqCases,
|
| 12 |
+
PinvCases,
|
| 13 |
+
SolveCases,
|
| 14 |
+
SVDCases,
|
| 15 |
+
_TestNorm2D,
|
| 16 |
+
_TestNormDoubleBase,
|
| 17 |
+
_TestNormInt64Base,
|
| 18 |
+
_TestNormSingleBase,
|
| 19 |
+
apply_tag,
|
| 20 |
+
)
|
| 21 |
+
from numpy.linalg.tests.test_linalg import TestQR as _TestQR
|
| 22 |
+
|
| 23 |
+
CASES = []
|
| 24 |
+
|
| 25 |
+
# square test cases
|
| 26 |
+
CASES += apply_tag('square', [
|
| 27 |
+
LinalgCase("0x0_matrix",
|
| 28 |
+
np.empty((0, 0), dtype=np.double).view(np.matrix),
|
| 29 |
+
np.empty((0, 1), dtype=np.double).view(np.matrix),
|
| 30 |
+
tags={'size-0'}),
|
| 31 |
+
LinalgCase("matrix_b_only",
|
| 32 |
+
np.array([[1., 2.], [3., 4.]]),
|
| 33 |
+
np.matrix([2., 1.]).T),
|
| 34 |
+
LinalgCase("matrix_a_and_b",
|
| 35 |
+
np.matrix([[1., 2.], [3., 4.]]),
|
| 36 |
+
np.matrix([2., 1.]).T),
|
| 37 |
+
])
|
| 38 |
+
|
| 39 |
+
# hermitian test-cases
|
| 40 |
+
CASES += apply_tag('hermitian', [
|
| 41 |
+
LinalgCase("hmatrix_a_and_b",
|
| 42 |
+
np.matrix([[1., 2.], [2., 1.]]),
|
| 43 |
+
None),
|
| 44 |
+
])
|
| 45 |
+
# No need to make generalized or strided cases for matrices.
|
| 46 |
+
|
| 47 |
+
|
| 48 |
+
class MatrixTestCase(LinalgTestCase):
|
| 49 |
+
TEST_CASES = CASES
|
| 50 |
+
|
| 51 |
+
|
| 52 |
+
class TestSolveMatrix(SolveCases, MatrixTestCase):
|
| 53 |
+
pass
|
| 54 |
+
|
| 55 |
+
|
| 56 |
+
class TestInvMatrix(InvCases, MatrixTestCase):
|
| 57 |
+
pass
|
| 58 |
+
|
| 59 |
+
|
| 60 |
+
class TestEigvalsMatrix(EigvalsCases, MatrixTestCase):
|
| 61 |
+
pass
|
| 62 |
+
|
| 63 |
+
|
| 64 |
+
class TestEigMatrix(EigCases, MatrixTestCase):
|
| 65 |
+
pass
|
| 66 |
+
|
| 67 |
+
|
| 68 |
+
class TestSVDMatrix(SVDCases, MatrixTestCase):
|
| 69 |
+
pass
|
| 70 |
+
|
| 71 |
+
|
| 72 |
+
class TestCondMatrix(CondCases, MatrixTestCase):
|
| 73 |
+
pass
|
| 74 |
+
|
| 75 |
+
|
| 76 |
+
class TestPinvMatrix(PinvCases, MatrixTestCase):
|
| 77 |
+
pass
|
| 78 |
+
|
| 79 |
+
|
| 80 |
+
class TestDetMatrix(DetCases, MatrixTestCase):
|
| 81 |
+
pass
|
| 82 |
+
|
| 83 |
+
|
| 84 |
+
class TestLstsqMatrix(LstsqCases, MatrixTestCase):
|
| 85 |
+
pass
|
| 86 |
+
|
| 87 |
+
|
| 88 |
+
class _TestNorm2DMatrix(_TestNorm2D):
|
| 89 |
+
array = np.matrix
|
| 90 |
+
|
| 91 |
+
|
| 92 |
+
class TestNormDoubleMatrix(_TestNorm2DMatrix, _TestNormDoubleBase):
|
| 93 |
+
pass
|
| 94 |
+
|
| 95 |
+
|
| 96 |
+
class TestNormSingleMatrix(_TestNorm2DMatrix, _TestNormSingleBase):
|
| 97 |
+
pass
|
| 98 |
+
|
| 99 |
+
|
| 100 |
+
class TestNormInt64Matrix(_TestNorm2DMatrix, _TestNormInt64Base):
|
| 101 |
+
pass
|
| 102 |
+
|
| 103 |
+
|
| 104 |
+
class TestQRMatrix(_TestQR):
|
| 105 |
+
array = np.matrix
|
venv/lib/python3.13/site-packages/numpy/matrixlib/tests/test_multiarray.py
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
from numpy.testing import assert_, assert_array_equal, assert_equal
|
| 3 |
+
|
| 4 |
+
|
| 5 |
+
class TestView:
|
| 6 |
+
def test_type(self):
|
| 7 |
+
x = np.array([1, 2, 3])
|
| 8 |
+
assert_(isinstance(x.view(np.matrix), np.matrix))
|
| 9 |
+
|
| 10 |
+
def test_keywords(self):
|
| 11 |
+
x = np.array([(1, 2)], dtype=[('a', np.int8), ('b', np.int8)])
|
| 12 |
+
# We must be specific about the endianness here:
|
| 13 |
+
y = x.view(dtype='<i2', type=np.matrix)
|
| 14 |
+
assert_array_equal(y, [[513]])
|
| 15 |
+
|
| 16 |
+
assert_(isinstance(y, np.matrix))
|
| 17 |
+
assert_equal(y.dtype, np.dtype('<i2'))
|
venv/lib/python3.13/site-packages/numpy/matrixlib/tests/test_numeric.py
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
from numpy.testing import assert_equal
|
| 3 |
+
|
| 4 |
+
|
| 5 |
+
class TestDot:
|
| 6 |
+
def test_matscalar(self):
|
| 7 |
+
b1 = np.matrix(np.ones((3, 3), dtype=complex))
|
| 8 |
+
assert_equal(b1 * 1.0, b1)
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
def test_diagonal():
|
| 12 |
+
b1 = np.matrix([[1, 2], [3, 4]])
|
| 13 |
+
diag_b1 = np.matrix([[1, 4]])
|
| 14 |
+
array_b1 = np.array([1, 4])
|
| 15 |
+
|
| 16 |
+
assert_equal(b1.diagonal(), diag_b1)
|
| 17 |
+
assert_equal(np.diagonal(b1), array_b1)
|
| 18 |
+
assert_equal(np.diag(b1), array_b1)
|
venv/lib/python3.13/site-packages/numpy/matrixlib/tests/test_regression.py
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
from numpy.testing import assert_, assert_equal, assert_raises
|
| 3 |
+
|
| 4 |
+
|
| 5 |
+
class TestRegression:
|
| 6 |
+
def test_kron_matrix(self):
|
| 7 |
+
# Ticket #71
|
| 8 |
+
x = np.matrix('[1 0; 1 0]')
|
| 9 |
+
assert_equal(type(np.kron(x, x)), type(x))
|
| 10 |
+
|
| 11 |
+
def test_matrix_properties(self):
|
| 12 |
+
# Ticket #125
|
| 13 |
+
a = np.matrix([1.0], dtype=float)
|
| 14 |
+
assert_(type(a.real) is np.matrix)
|
| 15 |
+
assert_(type(a.imag) is np.matrix)
|
| 16 |
+
c, d = np.matrix([0.0]).nonzero()
|
| 17 |
+
assert_(type(c) is np.ndarray)
|
| 18 |
+
assert_(type(d) is np.ndarray)
|
| 19 |
+
|
| 20 |
+
def test_matrix_multiply_by_1d_vector(self):
|
| 21 |
+
# Ticket #473
|
| 22 |
+
def mul():
|
| 23 |
+
np.asmatrix(np.eye(2)) * np.ones(2)
|
| 24 |
+
|
| 25 |
+
assert_raises(ValueError, mul)
|
| 26 |
+
|
| 27 |
+
def test_matrix_std_argmax(self):
|
| 28 |
+
# Ticket #83
|
| 29 |
+
x = np.asmatrix(np.random.uniform(0, 1, (3, 3)))
|
| 30 |
+
assert_equal(x.std().shape, ())
|
| 31 |
+
assert_equal(x.argmax().shape, ())
|