code: def get_range_search_metric_function(range_metric, D, R):
      if D is not None:
          assert R is not None
          assert D.shape == R.shape
      if isinstance(range_metric, list):
          aradius, ascore, aradius_from, aradius_to = [], [], [], []
          radius_to = 0
          for rsd in range_metric:
              assert isinstance(rsd, list)
              if len(rsd) == 3:
                  radius_from, radius_to, score = rsd
              elif len(rsd) == 2:
                  radius_from = radius_to
                  radius_to, score = rsd
              else:
                  raise AssertionError(
                      f"invalid range definition {rsd}"
                  )
              # radius_from and radius_to are compressed distances,
              # we need to convert them to real embedding distances.
              if D is not None:
                  sample_idxs = np.argwhere(
                      (D <= radius_to) & (D > radius_from)
                  )
                  assert len(sample_idxs) > 0
                  real_radius = np.mean(R[sample_idxs]).item()
              else:
                  real_radius = mean([radius_from, radius_to])
              logger.info(
                  f"range_search_metric_function {radius_from=} {radius_to=} {real_radius=} {score=}"
              )
              aradius.append(real_radius)
              ascore.append(score)
              aradius_from.append(radius_from)
              aradius_to.append(radius_to)
  
          def sigmoid(x, a, b, c):
              return a / (1 + np.exp(b * x - c))
  
          cutoff = max(aradius)
          popt, _ = curve_fit(sigmoid, aradius, ascore, [1, 5, 5])
  
          for r in np.arange(0, cutoff + 0.05, 0.05):
              logger.info(
                  f"range_search_metric_function {r=} {sigmoid(r, *popt)=}"
              )
  
          assert isinstance(cutoff, float)
          return (
              cutoff,
              lambda x: np.where(x < cutoff, sigmoid(x, *popt), 0),
              popt.tolist(),
              list(
                  zip(
                      aradius,
                      ascore,
                      aradius_from,
                      aradius_to,
                      strict=True,
                  )
              ),
          )
      else:
          # Assuming that the range_metric is a float,
          # so the range is [0..range_metric).
          # D is the result of a range_search with a radius of range_metric,
          # but both range_metric and D may be compressed distances.
          # We approximate the real embedding distance as max(R).
          if R is not None:
              real_range = np.max(R).item()
          else:
              real_range = range_metric
          logger.info(
              f"range_search_metric_function {range_metric=} {real_range=}"
          )
          assert isinstance(real_range, float)
          return (
              real_range * 2,
              lambda x: np.where(x < real_range, 1, 0),
              [],
              [],
          )
  
imports: import logging
  from copy import copy
  from dataclasses import dataclass
  from operator import itemgetter
  from statistics import median, mean
  from typing import Any, Dict, List, Optional
  from .utils import dict_merge
  from .index import Index, IndexFromCodec, IndexFromFactory
  from .descriptors import DatasetDescriptor, IndexDescriptor
  import faiss
  import numpy
  from scipy.optimize import curve_fit
label_desc: Use image node_id 3 for calling a global function with example usage: get_range_search_metric_function(range_metric, D, R) and returns: cutoff,  function,  popt,  list, unknown,  function,  list,  list, unknown