code: def calc_weighted_event_var(self, D, weights, event_pat):
          
          Dz = stats.zscore(D, axis=1, ddof=1)
          ev_var = np.empty(event_pat.shape[1])
          for e in range(event_pat.shape[1]):
              # Only compute variances for weights > 0.1% of max weight
              nz = weights[:, e] > np.max(weights[:, e])/1000
              sumsq = np.dot(weights[nz, e],
                             np.sum(np.square(Dz[nz, :] -
                                    event_pat[:, e]), axis=1))
              ev_var[e] = sumsq/(np.sum(weights[nz, e]) -
                                 np.sum(np.square(weights[nz, e])) /
                                 np.sum(weights[nz, e]))
          ev_var = ev_var / D.shape[1]
          return ev_var
docs: Computes normalized weighted variance around event pattern
  
          Utility function for computing variance in a training set of weighted
          event examples. For each event, the sum of squared differences for all
          timepoints from the event pattern is computed, and then the weights
          specify how much each of these differences contributes to the
          variance (normalized by the number of voxels).
  
          Parameters
          ----------
          D : timepoint by voxel ndarray
              fMRI data for which to compute event variances
  
          weights : timepoint by event ndarray
              specifies relative weights of timepoints for each event
  
          event_pat : voxel by event ndarray
              mean event patterns to compute variance around
  
          Returns
          -------
          ev_var : ndarray of variances for each event