code: def test_sample(space: Space, n_trials: int = 1_000):
      """Test the space sample has the expected distribution with the chi-squared test and KS test.
  
      Example code with scipy.stats.chisquared that should have the same
  
      >>> import scipy.stats
      >>> variance = np.sum(np.square(observed_frequency - expected_frequency) / expected_frequency)
      >>> f'X2 at alpha=0.05 = {scipy.stats.chi2.isf(0.05, df=4)}'
      >>> f'p-value = {scipy.stats.chi2.sf(variance, df=4)}'
      >>> scipy.stats.chisquare(f_obs=observed_frequency)
      """
      space.seed(0)
      samples = np.array([space.sample() for _ in range(n_trials)])
      assert len(samples) == n_trials
  
      if isinstance(space, Box):
          # TODO: Add KS testing for continuous uniform distribution
          pass
      elif isinstance(space, Discrete):
          expected_frequency = np.ones(space.n) * n_trials / space.n
          observed_frequency = np.zeros(space.n)
          for sample in samples:
              observed_frequency[sample - space.start] += 1
          degrees_of_freedom = space.n - 1
  
          assert observed_frequency.shape == expected_frequency.shape
          assert np.sum(observed_frequency) == n_trials
  
          variance = np.sum(
              np.square(expected_frequency - observed_frequency)
              / expected_frequency
          )
          assert variance < CHI_SQUARED[degrees_of_freedom]
      elif isinstance(space, MultiBinary):
          expected_frequency = n_trials / 2
          observed_frequency = np.sum(samples, axis=0)
          assert observed_frequency.shape == space.shape
  
          # As this is a binary space, then we can be lazy in the variance as the np.square is symmetric for the 0 and 1 categories
          variance = (
              2
              * np.square(observed_frequency - expected_frequency)
              / expected_frequency
          )
          assert variance.shape == space.shape
          assert np.all(variance < CHI_SQUARED[1])
      elif isinstance(space, MultiDiscrete):
          # Due to the multi-axis capability of MultiDiscrete, these functions need to be recursive and that the expected / observed numpy are of non-regular shapes
          def _generate_frequency(dim, func):
              if isinstance(dim, np.ndarray):
                  return np.array(
                      [
                          _generate_frequency(sub_dim, func)
                          for sub_dim in dim
                      ],
                      dtype=object,
                  )
              else:
                  return func(dim)
  
          def _update_observed_frequency(obs_sample, obs_freq):
              if isinstance(obs_sample, np.ndarray):
                  for sub_sample, sub_freq in zip(obs_sample, obs_freq):
                      _update_observed_frequency(sub_sample, sub_freq)
              else:
                  obs_freq[obs_sample] += 1
  
          expected_frequency = _generate_frequency(
              space.nvec, lambda dim: np.ones(dim) * n_trials / dim
          )
          observed_frequency = _generate_frequency(
              space.nvec, lambda dim: np.zeros(dim)
          )
          for sample in samples:
              _update_observed_frequency(sample, observed_frequency)
  
          def _chi_squared_test(dim, exp_freq, obs_freq):
              if isinstance(dim, np.ndarray):
                  for sub_dim, sub_exp_freq, sub_obs_freq in zip(
                      dim, exp_freq, obs_freq
                  ):
                      _chi_squared_test(
                          sub_dim, sub_exp_freq, sub_obs_freq
                      )
              else:
                  assert exp_freq.shape == (
                      dim,
                  ) and obs_freq.shape == (dim,)
                  assert np.sum(obs_freq) == n_trials
                  assert np.sum(exp_freq) == n_trials
                  _variance = np.sum(
                      np.square(exp_freq - obs_freq) / exp_freq
                  )
                  _degrees_of_freedom = dim - 1
                  assert _variance < CHI_SQUARED[_degrees_of_freedom]
  
          _chi_squared_test(
              space.nvec, expected_frequency, observed_frequency
          )
      elif isinstance(space, Text):
          expected_frequency = (
              np.ones(len(space.character_set))
              * n_trials
              * (
                  space.min_length
                  + (space.max_length - space.min_length) / 2
              )
              / len(space.character_set)
          )
          observed_frequency = np.zeros(len(space.character_set))
          for sample in samples:
              for x in sample:
                  observed_frequency[space.character_index(x)] += 1
          degrees_of_freedom = len(space.character_set) - 1
  
          assert observed_frequency.shape == expected_frequency.shape
          assert np.sum(observed_frequency) == sum(
              len(sample) for sample in samples
          )
  
          variance = np.sum(
              np.square(expected_frequency - observed_frequency)
              / expected_frequency
          )
          if degrees_of_freedom == 61:
              # scipy.stats.chi2.isf(0.05, df=61)
              assert variance < 80.23209784876272
          else:
              assert variance < CHI_SQUARED[degrees_of_freedom]
      else:
          raise NotImplementedError(
              f"Unknown sample testing for {type(space)}"
          )
  
imports: import copy
  import itertools
  import json
  import pickle
  import tempfile
  from typing import List, Union
  import numpy
  import pytest
  from gym.spaces import Box, Discrete, MultiBinary, MultiDiscrete, Space, Text
  from gym.utils import seeding
  from gym.utils.env_checker import data_equivalence
  from tests.spaces.utils import TESTING_FUNDAMENTAL_SPACES, TESTING_FUNDAMENTAL_SPACES_IDS, TESTING_SPACES, TESTING_SPACES_IDS
label_desc: Use image node_id 3 for calling a global function with example usage: test_sample(space, n_trials) and returns: np, func