code: def get_cca_similarity(
      acts1,
      acts2,
      epsilon=0.0,
      threshold=0.98,
      compute_coefs=True,
      compute_dirns=False,
      verbose=True,
  ):
      """The main function for computing cca similarities.
  
      This function computes the cca similarity between two sets of activations,
      returning a dict with the cca coefficients, a few statistics of the cca
      coefficients, and (optionally) the actual directions.
  
      Args:
                acts1: (num_neurons1, data_points) a 2d numpy array of neurons by
                       datapoints where entry (i,j) is the output of neuron i on
                       datapoint j.
                acts2: (num_neurons2, data_points) same as above, but (potentially)
                       for a different set of neurons. Note that acts1 and acts2
                       can have different numbers of neurons, but must agree on the
                       number of datapoints
  
                epsilon: small float to help stabilize computations
  
                threshold: float between 0, 1 used to get rid of trailing zeros in
                           the cca correlation coefficients to output more accurate
                           summary statistics of correlations.
  
  
                compute_coefs: boolean value determining whether coefficients
                               over neurons are computed. Needed for computing
                               directions
  
                compute_dirns: boolean value determining whether actual cca
                               directions are computed. (For very large neurons and
                               datasets, may be better to compute these on the fly
                               instead of store in memory.)
  
                verbose: Boolean, whether intermediate outputs are printed
  
      Returns:
                return_dict: A dictionary with outputs from the cca computations.
                             Contains neuron coefficients (combinations of neurons
                             that correspond to cca directions), the cca correlation
                             coefficients (how well aligned directions correlate),
                             x and y idxs (for computing cca directions on the fly
                             if compute_dirns=False), and summary statistics. If
                             compute_dirns=True, the cca directions are also
                             computed.
      """
  
      # assert dimensionality equal
      assert acts1.shape[1] == acts2.shape[1], "dimensions don't match"
      # check that acts1, acts2 are transposition
      assert acts1.shape[0] < acts1.shape[1], (
          "input must be number of neurons" "by datapoints"
      )
      return_dict = {}
  
      # compute covariance with numpy function for extra stability
      numx = acts1.shape[0]
      numy = acts2.shape[0]
  
      covariance = np.cov(acts1, acts2)
      sigmaxx = covariance[:numx, :numx]
      sigmaxy = covariance[:numx, numx:]
      sigmayx = covariance[numx:, :numx]
      sigmayy = covariance[numx:, numx:]
  
      # rescale covariance to make cca computation more stable
      xmax = np.max(np.abs(sigmaxx))
      ymax = np.max(np.abs(sigmayy))
      sigmaxx /= xmax
      sigmayy /= ymax
      sigmaxy /= np.sqrt(xmax * ymax)
      sigmayx /= np.sqrt(xmax * ymax)
  
      (
          [u, s, v],
          invsqrt_xx,
          invsqrt_yy,
          x_idxs,
          y_idxs,
      ) = compute_ccas(
          sigmaxx,
          sigmaxy,
          sigmayx,
          sigmayy,
          epsilon=epsilon,
          verbose=verbose,
      )
  
      # if x_idxs or y_idxs is all false, return_dict has zero entries
      if (not np.any(x_idxs)) or (not np.any(y_idxs)):
          return create_zero_dict(compute_dirns, acts1.shape[1])
  
      if compute_coefs:
          # also compute full coefficients over all neurons
          x_mask = np.dot(
              x_idxs.reshape((-1, 1)), x_idxs.reshape((1, -1))
          )
          y_mask = np.dot(
              y_idxs.reshape((-1, 1)), y_idxs.reshape((1, -1))
          )
  
          return_dict["coef_x"] = u.T
          return_dict["invsqrt_xx"] = invsqrt_xx
          return_dict["full_coef_x"] = np.zeros((numx, numx))
          np.place(
              return_dict["full_coef_x"], x_mask, return_dict["coef_x"]
          )
          return_dict["full_invsqrt_xx"] = np.zeros((numx, numx))
          np.place(
              return_dict["full_invsqrt_xx"],
              x_mask,
              return_dict["invsqrt_xx"],
          )
  
          return_dict["coef_y"] = v
          return_dict["invsqrt_yy"] = invsqrt_yy
          return_dict["full_coef_y"] = np.zeros((numy, numy))
          np.place(
              return_dict["full_coef_y"], y_mask, return_dict["coef_y"]
          )
          return_dict["full_invsqrt_yy"] = np.zeros((numy, numy))
          np.place(
              return_dict["full_invsqrt_yy"],
              y_mask,
              return_dict["invsqrt_yy"],
          )
  
          # compute means
          neuron_means1 = np.mean(acts1, axis=1, keepdims=True)
          neuron_means2 = np.mean(acts2, axis=1, keepdims=True)
          return_dict["neuron_means1"] = neuron_means1
          return_dict["neuron_means2"] = neuron_means2
  
      if compute_dirns:
          # orthonormal directions that are CCA directions
          cca_dirns1 = (
              np.dot(
                  np.dot(
                      return_dict["full_coef_x"],
                      return_dict["full_invsqrt_xx"],
                  ),
                  (acts1 - neuron_means1),
              )
              + neuron_means1
          )
          cca_dirns2 = (
              np.dot(
                  np.dot(
                      return_dict["full_coef_y"],
                      return_dict["full_invsqrt_yy"],
                  ),
                  (acts2 - neuron_means2),
              )
              + neuron_means2
          )
  
      # get rid of trailing zeros in the cca coefficients
      idx1 = sum_threshold(s, threshold)
      idx2 = sum_threshold(s, threshold)
  
      return_dict["cca_coef1"] = s
      return_dict["cca_coef2"] = s
      return_dict["x_idxs"] = x_idxs
      return_dict["y_idxs"] = y_idxs
      # summary statistics
      return_dict["mean"] = (np.mean(s[:idx1]), np.mean(s[:idx2]))
      return_dict["sum"] = (np.sum(s), np.sum(s))
  
      if compute_dirns:
          return_dict["cca_dirns1"] = cca_dirns1
          return_dict["cca_dirns2"] = cca_dirns2
  
      return return_dict
  
file_path: ecco/src/ecco/svcca_lib/cca_core.py
imports: from __future__ import absolute_import
  from __future__ import division
  from __future__ import print_function
  import numpy
label_desc: Use image node_id 6 for calling a global function with example usage: get_cca_similarity(acts1, acts2, epsilon, threshold, compute_coefs, compute_dirns, verbose) and returns: return_dict, create_zero_dict