code: def test_lm_mask(lang_model, num_tokens, sample_proba, filling):
      """
      Tests the mean number of masked tokens match the expected one.
      """
      # define text
      text = "word " * num_tokens
  
      # define perturbation options
      perturb_opts = {
          "sample_proba": sample_proba,
          "punctuation": "",
          "stopwords": [],
          "filling": filling,
          "frac_mask_templates": 1.0,
      }
  
      # define sampler
      sampler = LanguageModelSampler(
          model=lang_model, perturb_opts=perturb_opts
      )
      sampler.set_text(text)
  
      # create a bunch of masks
      raw, data = sampler.create_mask((), 10000, **perturb_opts)
  
      # hope that law of large number holds
      empirical_mean1 = np.mean(np.sum(data == 0, axis=1))
      theoretical_mean = sample_proba * len(sampler.ids_sample)
  
      # compute number of mask tokens in the strings
      mask = lang_model.mask.replace(lang_model.SUBWORD_PREFIX, "")
      empirical_mean2 = np.mean([str(x).count(mask) for x in raw])
      assert np.abs(empirical_mean1 - theoretical_mean) < 1.0
      assert empirical_mean1 == empirical_mean2
  
imports: import pytest
  from pytest_lazyfixture import lazy_fixture
  import string
  import numpy
  from typing import List
  from alibi.api.defaults import DEFAULT_META_ANCHOR, DEFAULT_DATA_ANCHOR
  from alibi.exceptions import PredictorCallError, PredictorReturnTypeError
  from alibi.explainers import AnchorText
  from alibi.explainers.anchors.text_samplers import Neighbors, load_spacy_lexeme_prob
  from alibi.explainers.anchors.language_model_text_sampler import LanguageModelSampler
  from alibi.explainers.tests.utils import predict_fcn
label_desc: Use image node_id 9 for calling a global function with example usage: test_lm_mask(lang_model, num_tokens, sample_proba, filling) without return types