code: def select_genes(
      data,
      threshold=0,
      atleast=10,
      yoffset=0.02,
      xoffset=5,
      decay=1,
      n=None,
      plot=True,
      markers=None,
      genes=None,
      figsize=(6, 3.5),
      markeroffsets=None,
      labelsize=10,
      alpha=1,
  ):
      if sp.issparse(data):
          zeroRate = 1 - np.squeeze(
              np.array((data > threshold).mean(axis=0))
          )
          A = data.multiply(data > threshold)
          A.data = np.log2(A.data)
          meanExpr = np.zeros_like(zeroRate) * np.nan
          detected = zeroRate < 1
          meanExpr[detected] = np.squeeze(
              np.array(A[:, detected].mean(axis=0))
          ) / (1 - zeroRate[detected])
      else:
          zeroRate = 1 - np.mean(data > threshold, axis=0)
          meanExpr = np.zeros_like(zeroRate) * np.nan
          detected = zeroRate < 1
          meanExpr[detected] = np.nanmean(
              np.where(
                  data[:, detected] > threshold,
                  np.log2(data[:, detected]),
                  np.nan,
              ),
              axis=0,
          )
  
      lowDetection = (
          np.array(np.sum(data > threshold, axis=0)).squeeze() < atleast
      )
      # lowDetection = (1 - zeroRate) * data.shape[0] < atleast - .00001
      zeroRate[lowDetection] = np.nan
      meanExpr[lowDetection] = np.nan
  
      if n is not None:
          up = 10
          low = 0
          for t in range(100):
              nonan = ~np.isnan(zeroRate)
              selected = np.zeros_like(zeroRate).astype(bool)
              selected[nonan] = (
                  zeroRate[nonan]
                  > np.exp(-decay * (meanExpr[nonan] - xoffset))
                  + yoffset
              )
              if np.sum(selected) == n:
                  break
              elif np.sum(selected) < n:
                  up = xoffset
                  xoffset = (xoffset + low) / 2
              else:
                  low = xoffset
                  xoffset = (xoffset + up) / 2
          print("Chosen offset: {:.2f}".format(xoffset))
      else:
          nonan = ~np.isnan(zeroRate)
          selected = np.zeros_like(zeroRate).astype(bool)
          selected[nonan] = (
              zeroRate[nonan]
              > np.exp(-decay * (meanExpr[nonan] - xoffset)) + yoffset
          )
  
      if plot:
          import matplotlib.pyplot as plt
  
          if figsize is not None:
              plt.figure(figsize=figsize)
          plt.ylim([0, 1])
          if threshold > 0:
              plt.xlim(
                  [np.log2(threshold), np.ceil(np.nanmax(meanExpr))]
              )
          else:
              plt.xlim([0, np.ceil(np.nanmax(meanExpr))])
          x = np.arange(plt.xlim()[0], plt.xlim()[1] + 0.1, 0.1)
          y = np.exp(-decay * (x - xoffset)) + yoffset
          if decay == 1:
              plt.text(
                  0.4,
                  0.2,
                  "{} genes selected\ny = exp(-x+{:.2f})+{:.2f}".format(
                      np.sum(selected), xoffset, yoffset
                  ),
                  color="k",
                  fontsize=labelsize,
                  transform=plt.gca().transAxes,
              )
          else:
              plt.text(
                  0.4,
                  0.2,
                  "{} genes selected\ny = exp(-{:.1f}*(x-{:.2f}))+{:.2f}".format(
                      np.sum(selected), decay, xoffset, yoffset
                  ),
                  color="k",
                  fontsize=labelsize,
                  transform=plt.gca().transAxes,
              )
  
          plt.plot(x, y, linewidth=2)
          xy = np.concatenate(
              (
                  np.concatenate((x[:, None], y[:, None]), axis=1),
                  np.array([[plt.xlim()[1], 1]]),
              )
          )
          t = plt.matplotlib.patches.Polygon(xy, color="r", alpha=0.2)
          plt.gca().add_patch(t)
  
          plt.scatter(
              meanExpr, zeroRate, s=3, alpha=alpha, rasterized=True
          )
          if threshold == 0:
              plt.xlabel("Mean log2 nonzero expression")
              plt.ylabel("Frequency of zero expression")
          else:
              plt.xlabel("Mean log2 nonzero expression")
              plt.ylabel("Frequency of near-zero expression")
          plt.tight_layout()
  
          if markers is not None and genes is not None:
              if markeroffsets is None:
                  markeroffsets = [(0, 0) for g in markers]
              for num, g in enumerate(markers):
                  i = np.where(genes == g)[0]
                  plt.scatter(meanExpr[i], zeroRate[i], s=10, color="k")
                  dx, dy = markeroffsets[num]
                  plt.text(
                      meanExpr[i] + dx + 0.1,
                      zeroRate[i] + dy,
                      g,
                      color="k",
                      fontsize=labelsize,
                  )
  
      return selected
  
file_path: openTSNE/examples/utils.py
imports: from os.path import abspath, dirname, join
  import numpy
  import scipy.sparse
label_desc: Use image node_id 4 for calling a global function with example usage: select_genes(data, threshold, atleast, yoffset, xoffset, decay, n, plot, markers, genes, figsize, markeroffsets, labelsize, alpha) and returns: selected