Masked Arrays (ma)#

The ma module provides support for arrays with masked (invalid) elements.

Example#

import numpycore as np

# Create a masked array
data = np.array([1, 2, -999, 4, 5])
mask = np.array([False, False, True, False, False])
ma = np.ma.array(data, mask=mask)

# Operations ignore masked values
print(ma.mean())  # 3.0 (ignoring -999)
print(ma.sum())   # 12

# Mask by condition
ma2 = np.ma.masked_greater(data, 3)

Creating Masked Arrays#

Function

Description

Example

array(data, mask)

Create masked array

View

masked_array(data, mask)

Create masked array (alias)

View

masked_where(condition, a)

Mask where condition is True

View

masked_equal(x, value)

Mask where equal to value

View

masked_not_equal(x, value)

Mask where not equal to value

masked_less(x, value)

Mask where less than value

View

masked_less_equal(x, value)

Mask where less than or equal

masked_greater(x, value)

Mask where greater than value

View

masked_greater_equal(x, value)

Mask where greater than or equal

masked_inside(x, v1, v2)

Mask values inside interval

masked_outside(x, v1, v2)

Mask values outside interval

masked_invalid(a)

Mask NaN and Inf values

Masked Array Attributes#

Attribute

Description

data

Underlying data array

mask

Boolean mask array

fill_value

Value used for filled output

count()

Count of unmasked elements

compressed()

Return all unmasked data as 1-D array

Operations#

All standard NumPy operations work with masked arrays, automatically ignoring masked elements in calculations.

Function

Description

Example

filled(a, fill_value)

Return array with masked values filled

getdata(a)

Return data of masked array

View

getmask(a)

Return mask of array

View

is_masked(x)

Check if array has masked values

View

count(a)

Count non-masked elements

fix_invalid(a)

Fix invalid values (NaN, Inf)

Code Examples#

The following examples are extracted from the test suite.

array (test_phase13_masked_adapters.py:259)
249        print(f"[SKIP] ma.array not available in numpycore")
250        return True
251
252    passed = True
253
254    # Test: Create masked array using array()
255    data = test_bind.np.array([1.0, 2.0, 3.0, 4.0])
256    mask = test_bind.np.array([False, True, False, False])
257
258    np_ma = test_bind.np.ma.array(data, mask=mask)
259    nc_ma = test_bind.numpycore.ma.array(data, mask=mask)
260
261    if not assert_masked_arrays_equal(np_ma, nc_ma, "array: create with mask"):
262        passed = False
263
264    if passed:
265        print(f"\n[OK] ma.array() tests passed")
266    return passed
267
268def test_asanyarray():
269    """Test np.ma.asanyarray() - convert to masked array"""
masked_array (test_masked_array_wrappers.py:30)
20)
21
22# ============================================================================
23# Helper Functions
24# ============================================================================
25
26def create_masked_array(data, mask, dtype='float64'):
27    """Create a numpycore MaskedArray"""
28    np_data = test_bind.np.array(data, dtype=dtype)
29    np_mask = test_bind.np.array(mask, dtype=bool)
30    return test_bind.numpycore.ma.masked_array(np_data, np_mask)
31
32def assert_array_equal(result, expected, test_name, rtol=1e-10, atol=1e-10):
33    """Assert that result matches expected"""
34    result_arr = test_bind.np.asarray(result)
35    expected_arr = test_bind.np.asarray(expected)
36
37    if not test_bind.np.allclose(result_arr, expected_arr, rtol=rtol, atol=atol, equal_nan=True):
38        print(f"[FAIL] {test_name}: Arrays not equal")
39        print(f"  Result: {result_arr}")
40        print(f"  Expected: {expected_arr}")
masked_where (test_masked_arrays.py:203)
193    print("="*80)
194
195    # Test: Mask where condition array is True
196    np_data = test_bind.np.array([1.0, 2.0, 3.0, 4.0, 5.0])
197    np_condition = np_data > 3.0
198
199    nc_data = test_bind.np.array([1.0, 2.0, 3.0, 4.0, 5.0])
200    nc_condition = nc_data > 3.0
201
202    np_masked = test_bind.np.ma.masked_where(np_condition, np_data)
203    nc_masked = test_bind.numpycore.ma.masked_where(nc_condition, nc_data)
204
205    if not assert_masked_arrays_equal(np_masked, nc_masked, "masked_where: condition > 3"):
206        return False
207
208    print(f"\n[OK] ma.masked_where() tests passed")
209    return True
210
211
212# ============================================================================
213# Utility Functions Tests
masked_equal (test_masked_arrays.py:140)
130    """Test np.ma.masked_equal() - mask where equal to value"""
131    print("\n" + "="*80)
132    print("Testing ma.masked_equal()")
133    print("="*80)
134
135    # Test: Mask where values equal 2
136    np_data = test_bind.np.array([1.0, 2.0, 3.0, 2.0, 5.0])
137    nc_data = test_bind.np.array([1.0, 2.0, 3.0, 2.0, 5.0])
138
139    np_masked = test_bind.np.ma.masked_equal(np_data, 2.0)
140    nc_masked = test_bind.numpycore.ma.masked_equal(nc_data, 2.0)
141
142    if not assert_masked_arrays_equal(np_masked, nc_masked, "masked_equal: value 2.0"):
143        return False
144
145    print(f"\n[OK] ma.masked_equal() tests passed")
146    return True
147
148
149def test_masked_greater():
150    """Test np.ma.masked_greater() - mask where greater than value"""
masked_less (test_masked_arrays.py:180)
170    """Test np.ma.masked_less() - mask where less than value"""
171    print("\n" + "="*80)
172    print("Testing ma.masked_less()")
173    print("="*80)
174
175    # Test: Mask where values < 3
176    np_data = test_bind.np.array([1.0, 2.0, 3.0, 4.0, 5.0])
177    nc_data = test_bind.np.array([1.0, 2.0, 3.0, 4.0, 5.0])
178
179    np_masked = test_bind.np.ma.masked_less(np_data, 3.0)
180    nc_masked = test_bind.numpycore.ma.masked_less(nc_data, 3.0)
181
182    if not assert_masked_arrays_equal(np_masked, nc_masked, "masked_less: < 3.0"):
183        return False
184
185    print(f"\n[OK] ma.masked_less() tests passed")
186    return True
187
188
189def test_masked_where():
190    """Test np.ma.masked_where() - mask where condition is True"""
masked_greater (test_masked_arrays.py:160)
150    """Test np.ma.masked_greater() - mask where greater than value"""
151    print("\n" + "="*80)
152    print("Testing ma.masked_greater()")
153    print("="*80)
154
155    # Test: Mask where values > 3
156    np_data = test_bind.np.array([1.0, 2.0, 3.0, 4.0, 5.0])
157    nc_data = test_bind.np.array([1.0, 2.0, 3.0, 4.0, 5.0])
158
159    np_masked = test_bind.np.ma.masked_greater(np_data, 3.0)
160    nc_masked = test_bind.numpycore.ma.masked_greater(nc_data, 3.0)
161
162    if not assert_masked_arrays_equal(np_masked, nc_masked, "masked_greater: > 3.0"):
163        return False
164
165    print(f"\n[OK] ma.masked_greater() tests passed")
166    return True
167
168
169def test_masked_less():
170    """Test np.ma.masked_less() - mask where less than value"""
getdata (test_masked_arrays.py:234)
224    np_mask = test_bind.np.array([False, True, False, True, False])
225
226    nc_data = test_bind.np.array([1.0, 2.0, 3.0, 4.0, 5.0])
227    nc_mask = test_bind.np.array([False, True, False, True, False])
228
229    np_masked = test_bind.np.ma.masked_array(np_data, mask=np_mask)
230    nc_masked = test_bind.numpycore.ma.masked_array(nc_data, mask=nc_mask)
231
232    # Extract data
233    np_extracted = test_bind.np.ma.getdata(np_masked)
234    nc_extracted = test_bind.numpycore.ma.getdata(nc_masked)
235
236    assert_arrays_equal(np_extracted, nc_extracted, "getdata: extract data")
237
238    print(f"\n[OK] ma.getdata() tests passed")
239    return True
240
241
242def test_getmask():
243    """Test np.ma.getmask() - extract mask array"""
244    print("\n" + "="*80)
getmask (test_masked_arrays.py:260)
250    np_mask = test_bind.np.array([False, True, False, True, False])
251
252    nc_data = test_bind.np.array([1.0, 2.0, 3.0, 4.0, 5.0])
253    nc_mask = test_bind.np.array([False, True, False, True, False])
254
255    np_masked = test_bind.np.ma.masked_array(np_data, mask=np_mask)
256    nc_masked = test_bind.numpycore.ma.masked_array(nc_data, mask=nc_mask)
257
258    # Extract mask
259    np_extracted_mask = test_bind.np.ma.getmask(np_masked)
260    nc_extracted_mask = test_bind.numpycore.ma.getmask(nc_masked)
261
262    assert_arrays_equal(np_extracted_mask, nc_extracted_mask, "getmask: extract mask")
263
264    print(f"\n[OK] ma.getmask() tests passed")
265    return True
266
267
268# ============================================================================
269# Math Functions Tests
270# ============================================================================
is_masked (test_masked_week16_utilities.py:31)
21def test_is_masked():
22    """Test ma.is_masked module function"""
23    print("\n[TEST] ma.is_masked()")
24
25    # Test with masked data
26    data = np.array([1.0, 2.0, 3.0, 4.0], dtype=np.float64)
27    mask = np.array([False, True, False, False], dtype=bool)
28    nc_ma = test_bind.numpycore.ma.masked_array(data, mask=mask)
29
30    result = test_bind.numpycore.ma.is_masked(nc_ma)
31    assert result == True, "Array with masked elements should return True"
32
33    # Test with no masked data
34    mask_none = np.array([False, False, False, False], dtype=bool)
35    nc_ma_none = test_bind.numpycore.ma.masked_array(data, mask=mask_none)
36
37    result_none = test_bind.numpycore.ma.is_masked(nc_ma_none)
38    assert result_none == False, "Array with no masked elements should return False"
39
40    f_print_success("ma.is_masked() passed")