<jupyter_start><jupyter_text>Masking<jupyter_code>import numpy as np
import scipp as sc<jupyter_output><empty_output><jupyter_text>Creating and manipulating masksMasks are simply variables with `dtype=bool`:<jupyter_code>mask = sc.Variable(dims=['x'], values=[False, False, True])
sc.table(mask)<jupyter_output><empty_output><jupyter_text>Boolean operators can be used to manipulate such variables:<jupyter_code>print(~mask)
print(mask ^ mask)
print(mask & ~mask)
print(mask | ~mask)<jupyter_output><empty_output><jupyter_text>Comparison operators such as `==`, `!=`, `=` (see also the [list of comparison functions](../reference/free-functions.rstcomparison)) are a common method of defining masks:<jupyter_code>var = sc.Variable(dims=['x'], values=np.random.random(5), unit=sc.units.m)
mask2 = var < 0.5 * sc.units.m
mask2<jupyter_output><empty_output><jupyter_text>Masks in data arrays and items of datasetData arrays and equivalently items of dataset can store arbitrary masks.Datasets themselves do not support masks.Masks are accessible using the `masks` keyword-argument and property, which behaves in the same way as `coords`:<jupyter_code>a = sc.DataArray(
    data = sc.Variable(dims=['y', 'x'], values=np.arange(1.0, 7.0).reshape((2, 3))),
    coords={
        'y': sc.Variable(dims=['y'], values=np.arange(2.0), unit=sc.units.m),
        'x': sc.Variable(dims=['x'], values=np.arange(3.0), unit=sc.units.m)},
    masks={
        'x': sc.Variable(dims=['x'], values=[False, False, True])}
    )
sc.show(a)
b = a.copy()
b.masks['x'].values[1] = True
b.masks['y'] = sc.Variable(dims=['y'], values=[False, True])<jupyter_output><empty_output><jupyter_text>Note that setting a mask does *not* affect the data.Masks of dataset items are accessed using the `masks` property of the item:<jupyter_code>ds = sc.Dataset(data={'a':a})
ds['a'].masks['x']<jupyter_output><empty_output><jupyter_text>Operations with masked objects Element-wise binary operationsThe result of operations between data arrays or dataset with masks contains the masks of both inputs.If both inputs contain a mask with the same name, the output mask is the combination of the input masks with an  **OR** operation:<jupyter_code>a + b<jupyter_output><empty_output><jupyter_text>Reduction operationsOperations like `sum` and `mean` over a particular dimension cannot preserve masks that depend on this dimension.If this is the case, the mask is applied during the operation and is not present in the output:<jupyter_code>sc.sum(a, 'x')<jupyter_output><empty_output><jupyter_text>The `mean` operation takes into account that masking is reducing the number of points in the mean, i.e., masked elements are not counted (in contrast to, e.g., treating them as 0):<jupyter_code>sc.mean(a, 'x')<jupyter_output><empty_output><jupyter_text>If a mask does not depend on the dimension used for the `sum` or `mean` operation, it is preserved.Here `b` has two masks, one that is applied and one that is preserved:<jupyter_code>sc.sum(b, 'x')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1299472295514512, 'fraction_numerical': 0.005606860158311346, 'mean_word_length': 6.343825665859564, 'pattern_counts': {'":': 0, '<': 42, '<?xml version=': 0, '>': 41, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '10', 'dir': 'jupyter-structured-clean-dedup', 'id': '267096', 'license': 'BSD-3-Clause', 'n_tokens_mistral': 1000, 'n_tokens_neox': 994, 'n_words': 298, 'path': 'docs/user-guide/masking.ipynb', 'repo_name': 'mlund/scipp', 'size': '3032'}