8000 BUG: Problems with order of masked arrays in version 1.24.3 · Issue #23651 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content
BUG: Problems with order of masked arrays in version 1.24.3 #23651
Closed
@jcrivenaes

Description

@jcrivenaes

Describe the issue:

Confer earlier issue: #22912, apparently solved in #23635.

A new problem has now arisen, demonstrated in the code below.

Reproduce the code example:

#!/usr/bin/env python
import numpy as np

print("Using numpy", np.__version__)


def showcase1():
    """The following works for numpy version < 1.24.0"""
    print("\nShowcase1")
    arr = np.array([[1., 2., 3., 4.], [5., 6., 7., 8.]])
    arr = np.ma.masked_where(arr == 7., arr)

    val = np.ma.filled(arr, fill_value=np.nan)
    val = np.array(val, order="F")
    val = np.ma.masked_invalid(val)
    val1d = val.ravel(order="K")

    expect_here = "[1. 5. 2. 6. 3. -- 4. 8.]"
    print("Expected:", expect_here)

    got = str(val1d).replace("0", "")
    print("Got:", got)
    if got == expect_here:
        print("OK for", np.__version__)
    else:
        print("**** Not OK for numpy version", np.__version__)


def showcase2():
    """The following works for numpy version < 1.24.3; fails in 1.24.3"""
    print("\nShowcase2")
    arr = np.array([[1, 2, 3, 4], [5, 6, 7, 8]])

    # pure numpy ok here
    print("-- pure numpy")
    arrdata_pure_np = np.array(arr, order="F")
    val = np.array(arrdata_pure_np)
    val1d = val.ravel(order="K")
    print("Expected val1d is [1 5 2 6 3 7 4 8]")
    print("Numpy val1d is", str(val1d))
    assert str(val1d) == "[1 5 2 6 3 7 4 8]", "Pure numpy"

    # numpy masked goes wrong for numpy 1.24.3
    print("-- numpy masked")
    arr = np.ma.masked_where(arr == 7, arr)
    arrdata_ma = np.array(arr.data, order="F")
    arrmask_ma = np.array(arr.mask, order="F")
    val = np.ma.array(arrdata_ma, mask=arrmask_ma)
    val1d = val.ravel(order="K")
    print("Expected val1d is [1 5 2 6 3 -- 4 8]")
    print("Numpy masked val1d is", str(val1d))
    ok = str(val1d) == "[1 5 2 6 3 -- 4 8]"
    if ok:
        print("OK for numpy version", np.__version__)
    else:
        print("**** NOT OK for numpy version", np.__version__)


def main():
    showcase1()
    showcase2()

if __name__ == "__main__":
    main()

Error message:

## Numpy version < 1.24:
Using numpy 1.19.1

Showcase1
Expected: [1. 5. 2. 6. 3. -- 4. 8.]
Got: [1. 5. 2. 6. 3. -- 4. 8.]
OK for 1.19.1

Showcase2
-- pure numpy
Expected val1d is [1 5 2 6 3 7 4 8]
Numpy val1d is [1 5 2 6 3 7 4 8]
-- numpy masked
Expected val1d is [1 5 2 6 3 -- 4 8]
Numpy masked val1d is [1 5 2 6 3 -- 4 8]
OK for numpy version 1.19.1


## Numpy version 1.24.1:
Using numpy 1.24.1

Showcase1
Expected: [1. 5. 2. 6. 3. -- 4. 8.]
Got: [1. 5. 2. 6. 3. nan -- 8.]
**** Not OK for numpy version 1.24.1

Showcase2
-- pure numpy
Expected val1d is [1 5 2 6 3 7 4 8]
Numpy val1d is [1 5 2 6 3 7 4 8]
-- numpy masked
Expected val1d is [1 5 2 6 3 -- 4 8]
Numpy masked val1d is [1 5 2 6 3 -- 4 8]
OK for numpy version 1.24.1


## Numpy version 1.24.3
Using numpy 1.24.3

Showcase1
Expected: [1. 5. 2. 6. 3. -- 4. 8.]
Got: [1. 2. 3. 4. 5. 6. -- 8.]
**** Not OK for numpy version 1.24.3

Showcase2
-- pure numpy
Expected val1d is [1 5 2 6 3 7 4 8]
Numpy val1d is [1 5 2 6 3 7 4 8]
-- numpy masked
Expected val1d is [1 5 2 6 3 -- 4 8]
Numpy masked val1d is [1 2 3 4 5 6 -- 8]
**** NOT OK for numpy version 1.24.3

Runtime information:

See code above

Context for the issue:

Makes https://github.com/equinor/xtgeo tests fail.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0