class_docstr: Test the kd-tree resampler.
class_docstr_tok: ["Test","the","kd-tree","resampler","."]
docstr: Test the kd resampler.
code: def test_kd_resampling(
      self, xr_resampler, create_filename, zarr_open, xr_dset
  ):
      """Test the kd resampler."""
      from satpy.resample import KDTreeResampler
  
      (
          data,
          source_area,
          swath_data,
          source_swath,
          target_area,
      ) = get_test_data()
      mock_dset = mock.MagicMock()
      xr_dset.return_value = mock_dset
      resampler = KDTreeResampler(source_swath, target_area)
      resampler.precompute(
          mask=da.arange(5, chunks=5).astype(bool), cache_dir="."
      )
      xr_resampler.assert_called_once()
      resampler.resampler.get_neighbour_info.assert_called()
      # swath definitions should not be cached
      assert len(mock_dset.to_zarr.mock_calls) == 0
      resampler.resampler.reset_mock()
  
      resampler = KDTreeResampler(source_area, target_area)
      resampler.precompute()
      resampler.resampler.get_neighbour_info.assert_called_with(
          mask=None
      )
  
      try:
          the_dir = tempfile.mkdtemp()
          resampler = KDTreeResampler(source_area, target_area)
          create_filename.return_value = os.path.join(
              the_dir, "test_cache.zarr"
          )
          zarr_open.side_effect = ValueError()
          resampler.precompute(cache_dir=the_dir)
          # assert data was saved to the on-disk cache
          assert len(mock_dset.to_zarr.mock_calls) == 1
          # assert that zarr_open was called to try to zarr_open something from disk
          assert len(zarr_open.mock_calls) == 1
          # we should have cached things in-memory
          assert len(resampler._index_caches) == 1
          nbcalls = len(
              resampler.resampler.get_neighbour_info.mock_calls
          )
          # test reusing the resampler
          zarr_open.side_effect = None
          # The kdtree shouldn't be available after saving cache to disk
          assert resampler.resampler.delayed_kdtree is None
  
          class FakeZarr(dict):
              def close(self):
                  pass
  
              def astype(self, dtype):
                  pass
  
          zarr_open.return_value = FakeZarr(
              valid_input_index=1,
              valid_output_index=2,
              index_array=3,
              distance_array=4,
          )
          resampler.precompute(cache_dir=the_dir)
          # we already have things cached in-memory, no need to save again
          assert len(mock_dset.to_zarr.mock_calls) == 1
          # we already have things cached in-memory, don't need to load
          assert len(zarr_open.mock_calls) == 1
          # we should have cached things in-memory
          assert len(resampler._index_caches) == 1
          assert (
              len(resampler.resampler.get_neighbour_info.mock_calls)
              == nbcalls
          )
  
          # test loading saved resampler
          resampler = KDTreeResampler(source_area, target_area)
          resampler.precompute(cache_dir=the_dir)
          assert len(zarr_open.mock_calls) == 4
          assert (
              len(resampler.resampler.get_neighbour_info.mock_calls)
              == nbcalls
          )
          # we should have cached things in-memory now
          assert len(resampler._index_caches) == 1
      finally:
          shutil.rmtree(the_dir)
  
      fill_value = 8
      resampler.compute(data, fill_value=fill_value)
      resampler.resampler.get_sample_from_neighbour_info.assert_called_with(
          data, fill_value
      )
  
imports: import os
  import shutil
  import tempfile
  import unittest
  from unittest import mock
  import dask.array
  import numpy
  import pytest
  import xarray
  from pyproj import CRS
  from satpy.resample import NativeResampler
label_desc: Use image node_id 1 for calling the TestKDTreeResampler obj's underlying member method code with example usage: obj.test_kd_resampling(xr_resampler, create_filename, zarr_open, xr_dset) without return types