code: def prepare(
      topic_term_dists,
      doc_topic_dists,
      doc_lengths,
      vocab,
      term_frequency,
      R=30,
      lambda_step=0.01,
      mds=js_PCoA,
      n_jobs=-1,
      plot_opts=None,
      sort_topics=True,
      start_index=1,
  ):
      """Transforms the topic model distributions and related corpus data into
      the data structures needed for the visualization.
  
      Parameters
      ----------
      topic_term_dists : array-like, shape (`n_topics`, `n_terms`)
          Matrix of topic-term probabilities. Where `n_terms` is `len(vocab)`.
      doc_topic_dists : array-like, shape (`n_docs`, `n_topics`)
          Matrix of document-topic probabilities.
      doc_lengths : array-like, shape `n_docs`
          The length of each document, i.e. the number of words in each document.
          The order of the numbers should be consistent with the ordering of the
          docs in `doc_topic_dists`.
      vocab : array-like, shape `n_terms`
          List of all the words in the corpus used to train the model.
      term_frequency : array-like, shape `n_terms`
          The count of each particular term over the entire corpus. The ordering
          of these counts should correspond with `vocab` and `topic_term_dists`.
      R : int
          The number of terms to display in the barcharts of the visualization.
          Default is 30. Recommended to be roughly between 10 and 50.
      lambda_step : float, between 0 and 1
          Determines the interstep distance in the grid of lambda values over
          which to iterate when computing relevance.
          Default is 0.01. Recommended to be between 0.01 and 0.1.
      mds : function or a string representation of function
          A function that takes `topic_term_dists` as an input and outputs a
          `n_topics` by `2`  distance matrix. The output approximates the distance
          between topics. See :func:`js_PCoA` for details on the default function.
          A string representation currently accepts `pcoa` (or upper case variant),
          `mmds` (or upper case variant) and `tsne` (or upper case variant),
          if `sklearn` package is installed for the latter two.
      n_jobs : int
          The number of cores to be used to do the computations. The regular
          joblib conventions are followed so `-1`, which is the default, will
          use all cores.
      plot_opts : dict, with keys 'xlab' and `ylab`
          Dictionary of plotting options, right now only used for the axis labels.
      sort_topics : sort topics by topic proportion (percentage of tokens covered). Set to false to
          to keep original topic order.
      start_index: how to number topics for prepared data. Defaults to one-based indexing.
          Set to 0 for zero-based indexing.
  
      Returns
      -------
      prepared_data : PreparedData
          A named tuple containing all the data structures required to create
          the visualization. To be passed on to functions like :func:`display`.
          This named tuple can be represented as json or a python dictionary.
          There is a helper function 'sorted_terms' that can be used to get
          the terms of a topic using lambda to rank their relevance.
  
  
      Notes
      -----
      This implements the method of `Sievert, C. and Shirley, K. (2014):
      LDAvis: A Method for Visualizing and Interpreting Topics, ACL Workshop on
      Interactive Language Learning, Visualization, and Interfaces.`
  
      http://nlp.stanford.edu/events/illvi2014/papers/sievert-illvi2014.pdf
  
      See Also
      --------
      :func:`save_json`: save json representation of a figure to file
      :func:`save_html` : save html representation of a figure to file
      :func:`show` : launch a local server and show a figure in a browser
      :func:`display` : embed figure within the IPython notebook
      :func:`enable_notebook` : automatically embed visualizations in IPython notebook
      """
      if plot_opts is None:
          plot_opts = {"xlab": "PC1", "ylab": "PC2"}
  
      # parse mds
      if isinstance(mds, str):
          mds = mds.lower()
          if mds == "pcoa":
              mds = js_PCoA
          elif mds in ("mmds", "tsne"):
              mds_opts = {"mmds": js_MMDS, "tsne": js_TSNE}
              mds = mds_opts[mds]
          else:
              logging.warning("Unknown mds `%s`, switch to PCoA" % mds)
              mds = js_PCoA
  
      # Conceptually, the items in `topic_term_dists` end up as individual rows in the
      # DataFrame, but we can speed up ingestion by treating them as columns and
      # transposing at the end. (This is especially true when the number of terms far
      # exceeds the number of topics.)
      topic_term_dist_cols = [
          pd.Series(topic_term_dist, dtype="float64")
          for topic_term_dist in topic_term_dists
      ]
      topic_term_dists = pd.concat(topic_term_dist_cols, axis=1).T
  
      topic_term_dists = _df_with_names(
          topic_term_dists, "topic", "term"
      )
      doc_topic_dists = _df_with_names(doc_topic_dists, "doc", "topic")
      term_frequency = _series_with_name(
          term_frequency, "term_frequency"
      )
      doc_lengths = _series_with_name(doc_lengths, "doc_length")
      vocab = _series_with_name(vocab, "vocab")
      _input_validate(
          topic_term_dists,
          doc_topic_dists,
          doc_lengths,
          vocab,
          term_frequency,
      )
      R = min(R, len(vocab))
  
      topic_freq = doc_topic_dists.mul(doc_lengths, axis="index").sum()
      # topic_freq       = np.dot(doc_topic_dists.T, doc_lengths)
      if sort_topics:
          topic_proportion = (
              topic_freq / topic_freq.sum()
          ).sort_values(ascending=False)
      else:
          topic_proportion = topic_freq / topic_freq.sum()
  
      topic_order = topic_proportion.index
      # reorder all data based on new ordering of topics
      topic_freq = topic_freq[topic_order]
      topic_term_dists = topic_term_dists.iloc[topic_order]
      # Unused: doc_topic_dists = doc_topic_dists[topic_order]
  
      # token counts for each term-topic combination (widths of red bars)
      term_topic_freq = (topic_term_dists.T * topic_freq).T
      # Quick fix for red bar width bug.  We calculate the
      # term frequencies internally, using the topic term distributions and the
      # topic frequencies, rather than using the user-supplied term frequencies.
      # For a detailed discussion, see: https://github.com/cpsievert/LDAvis/pull/41
      term_frequency = np.sum(term_topic_freq, axis=0)
  
      topic_info = _topic_info(
          topic_term_dists,
          topic_proportion,
          term_frequency,
          term_topic_freq,
          vocab,
          lambda_step,
          R,
          n_jobs,
          start_index,
      )
      token_table = _token_table(
          topic_info,
          term_topic_freq,
          vocab,
          term_frequency,
          start_index,
      )
      topic_coordinates = _topic_coordinates(
          mds, topic_term_dists, topic_proportion, start_index
      )
      client_topic_order = [x + start_index for x in topic_order]
  
      return PreparedData(
          topic_coordinates,
          topic_info,
          token_table,
          R,
          lambda_step,
          plot_opts,
          client_topic_order,
      )
  
imports: import json
  import logging
  import numpy
  import pandas
  from collections import namedtuple
  from joblib import Parallel, delayed, cpu_count
  from scipy.stats import entropy
  from scipy.spatial.distance import pdist, squareform
  from sklearn.manifold import MDS, TSNE
  from pyLDAvis.utils import NumPyEncoder
label_desc: Use image node_id 18 for calling a global function with example usage: prepare(topic_term_dists, doc_topic_dists, doc_lengths, vocab, term_frequency, R, lambda_step, mds, n_jobs, plot_opts, sort_topics, start_index) and returns: PreparedData