code: def get_optimal_category_projection_by_rank(
      corpus,
      n_dims=2,
      n_steps=20,
      projector=lambda rank, n_dims: CategoryProjector(
          AssociationCompactorByRank(rank), projector=PCA(n_dims)
      ),
      verbose=False,
  ):
      try:
          from astropy.stats import RipleysKEstimator
      except:
          raise Exception("Please install astropy")
  
      ripley = RipleysKEstimator(
          area=1.0, x_max=1.0, y_max=1.0, x_min=0.0, y_min=0.0
      )
      min_dev = None
      best_rank = None
      best_x = None
      best_y = None
      best_projector = None
      for rank in np.linspace(
          1, TermCategoryRanker().get_max_rank(corpus), n_steps
      ):
          r = np.linspace(0, np.sqrt(2), 100)
          category_projector = projector(rank, n_dims)
          category_projection = category_projector.project(corpus)
          for dim_1 in range(0, n_dims):
              for dim_2 in range(dim_1 + 1, n_dims):
                  proj = category_projection.projection[
                      :, [dim_1, dim_2]
                  ]
                  scaled_proj = np.array(
                      [
                          stretch_0_to_1(proj.T[0]),
                          stretch_0_to_1(proj.T[1]),
                      ]
                  ).T
                  dev = np.sum(
                      np.abs(
                          ripley(scaled_proj, r, mode="ripley")
                          - ripley.poisson(r)
                      )
                  )
                  if min_dev is None or dev < min_dev:
                      min_dev = dev
                      best_rank = rank
                      best_projector = category_projector
                      best_x, best_y = (dim_1, dim_2)
                  if verbose:
                      print(
                          "rank", rank, "dims", dim_1, dim_2, "K", dev
                      )
                      print(
                          "     best rank",
                          best_rank,
                          "dims",
                          best_x,
                          best_y,
                          "K",
                          min_dev,
                      )
      if verbose:
          print(best_rank, best_x, best_y)
      return best_projector.project(corpus, best_x, best_y)
  
file_path: scattertext/scattertext/categoryprojector/OptimalProjection.py
imports: from typing import Optional, Callable
  import numpy
  from sklearn.decomposition import PCA
  from scattertext.Scalers import stretch_0_to_1
  from scattertext.termscoring.RankDifference import RankDifference
  from scattertext.categoryprojector.CategoryProjector import CategoryProjector
  from scattertext.termcompaction.AssociationCompactor import AssociationCompactor, AssociationCompactorByRank, TermCategoryRanker
label_desc: Use image node_id 3 for calling a global function with example usage: get_optimal_category_projection_by_rank(corpus, n_dims, n_steps, projector, verbose) and returns: best_projector