Takes average vector of replicate measurements
514209
def vspec_magic(data):
    
    vdata, Dirdata, step_meth = [], [], ""
    if len(data) == 0:
        return vdata
    treat_init = ["treatment_temp", "treatment_temp_decay_rate", "treatment_temp_dc_on", "treatment_temp_dc_off", "treatment_ac_field", "treatment_ac_field_decay_rate", "treatment_ac_field_dc_on",
                  "treatment_ac_field_dc_off", "treatment_dc_field", "treatment_dc_field_decay_rate", "treatment_dc_field_ac_on", "treatment_dc_field_ac_off", "treatment_dc_field_phi", "treatment_dc_field_theta"]
    treats = []
#
# find keys that are used
#
    for key in treat_init:
        if key in list(data[0].keys()):
            treats.append(key)  # get a list of keys
    stop = {}
    stop["er_specimen_name"] = "stop"
    for key in treats:
        stop[key] = ""  # tells program when to quit and go home
    data.append(stop)
#
# set initial states
#
    DataState0, newstate = {}, 0
    for key in treats:
        DataState0[key] = data[0][key]  # set beginning treatment
    k, R = 1, 0
    for i in range(k, len(data)):
        FDirdata, Dirdata, DataStateCurr, newstate = [], [], {}, 0
        for key in treats:  # check if anything changed
            DataStateCurr[key] = data[i][key]
            if DataStateCurr[key].strip() != DataState0[key].strip():
                newstate = 1  # something changed
        if newstate == 1:
            if i == k:  # sample is unique
                vdata.append(data[i - 1])
            else:  # measurement is not unique
                # print "averaging: records " ,k,i
                for l in range(k - 1, i):
                    if 'orientation' in data[l]['measurement_description']:
                        data[l]['measurement_description'] = ""
                    Dirdata.append([float(data[l]['measurement_dec']), float(
                        data[l]['measurement_inc']), float(data[l]['measurement_magn_moment'])])
                    FDirdata.append(
                        [float(data[l]['measurement_dec']), float(data[l]['measurement_inc'])])
                dir, R = vector_mean(Dirdata)
                Fpars = fisher_mean(FDirdata)
                vrec = data[i - 1]
                vrec['measurement_dec'] = '%7.1f' % (dir[0])
                vrec['measurement_inc'] = '%7.1f' % (dir[1])
                vrec['measurement_magn_moment'] = '%8.3e' % (
                    old_div(R, (i - k + 1)))
                vrec['measurement_csd'] = '%7.1f' % (Fpars['csd'])
                vrec['measurement_positions'] = '%7.1f' % (Fpars['n'])
                vrec['measurement_description'] = 'average of multiple measurements'
                if "magic_method_codes" in list(vrec.keys()):
                    meths = vrec["magic_method_codes"].strip().split(":")
                    if "DE-VM" not in meths:
                        meths.append("DE-VM")
                    methods = ""
                    for meth in meths:
                        methods = methods + meth + ":"
                    vrec["magic_method_codes"] = methods[:-1]
                else:
                    vrec["magic_method_codes"] = "DE-VM"
                vdata.append(vrec)
# reset state to new one
            for key in treats:
                DataState0[key] = data[i][key]  # set beginning treatment
            k = i + 1
            if data[i]["er_specimen_name"] == "stop":
                del data[-1]  # get rid of dummy stop sign
                return vdata, treats
1153434
def validate_average_best_span():
    """"""
    N = 200
    num_trials = 400
    avg = numpy.zeros(N)
    for i in range(num_trials):
        x, y = smoother_friedman82.build_sample_smoother_problem_friedman82(N=N)
        my_smoother = smoother.perform_smooth(
            x, y, smoother_cls=supersmoother.SuperSmoother
        )
        avg += my_smoother._smoothed_best_spans.smooth_result
        if not (i + 1) % 20:
            print(i + 1)
    avg /= num_trials
    plt.plot(my_smoother.x, avg, '.', label='Average JCV')
    finish_plot()
508718
def _batch_norm(self, name, x):
        """"""
        with tf.variable_scope(name):
            params_shape = [x.get_shape()[-1]]

            beta = tf.get_variable(
                "beta",
                params_shape,
                tf.float32,
                initializer=tf.constant_initializer(0.0, tf.float32))
            gamma = tf.get_variable(
                "gamma",
                params_shape,
                tf.float32,
                initializer=tf.constant_initializer(1.0, tf.float32))

            if self.mode == "train":
                mean, variance = tf.nn.moments(x, [0, 1, 2], name="moments")

                moving_mean = tf.get_variable(
                    "moving_mean",
                    params_shape,
                    tf.float32,
                    initializer=tf.constant_initializer(0.0, tf.float32),
                    trainable=False)
                moving_variance = tf.get_variable(
                    "moving_variance",
                    params_shape,
                    tf.float32,
                    initializer=tf.constant_initializer(1.0, tf.float32),
                    trainable=False)

                self._extra_train_ops.append(
                    moving_averages.assign_moving_average(
                        moving_mean, mean, 0.9))
                self._extra_train_ops.append(
                    moving_averages.assign_moving_average(
                        moving_variance, variance, 0.9))
            else:
                mean = tf.get_variable(
                    "moving_mean",
                    params_shape,
                    tf.float32,
                    initializer=tf.constant_initializer(0.0, tf.float32),
                    trainable=False)
                variance = tf.get_variable(
                    "moving_variance",
                    params_shape,
                    tf.float32,
                    initializer=tf.constant_initializer(1.0, tf.float32),
                    trainable=False)
                tf.summary.histogram(mean.op.name, mean)
                tf.summary.histogram(variance.op.name, variance)
            # elipson used to be 1e-5. Maybe 0.001 solves NaN problem in deeper
            # net.
            y = tf.nn.batch_normalization(x, mean, variance, beta, gamma,
                                          0.001)
            y.set_shape(x.get_shape())
            return y
257893
def veq_samples(R_dist,Prot_dist,N=1e4,alpha=0.23,l0=20,sigl=20):
    
    ls = stats.norm(l0,sigl).rvs(N)
    Prots = Prot_dist.rvs(N)
    Prots *= diff_Prot_factor(ls,alpha)
    return R_dist.rvs(N)*2*np.pi*RSUN/(Prots*DAY)/1e5
1188507
def pvalues(self):
        """"""
        self.compute_statistics()

        lml_alts = self.alt_lmls()
        lml_null = self.null_lml()

        lrs = -2 * lml_null + 2 * asarray(lml_alts)

        from scipy.stats import chi2
        chi2 = chi2(df=1)

        return chi2.sf(lrs)
986580
def _combine_qc_samples(samples):
    
    by_bam = collections.defaultdict(list)
    for data in [utils.to_single_data(x) for x in samples]:
        batch = dd.get_batch(data) or dd.get_sample_name(data)
        if not isinstance(batch, (list, tuple)):
            batch = [batch]
        batch = tuple(batch)
        by_bam[(dd.get_align_bam(data) or dd.get_work_bam(data), batch)].append(data)
    out = []
    for data_group in by_bam.values():
        data = data_group[0]
        alg_qc = []
        qc = {}
        metrics = {}
        for d in data_group:
            qc.update(dd.get_summary_qc(d))
            metrics.update(dd.get_summary_metrics(d))
            alg_qc.extend(dd.get_algorithm_qc(d))
        data["config"]["algorithm"]["qc"] = alg_qc
        data["summary"]["qc"] = qc
        data["summary"]["metrics"] = metrics
        out.append([data])
    return out
752212
private VectorClock toVectorClock(List<Entry<String, Long>> replicaLogicalTimestamps) {
        final VectorClock timestamps = new VectorClock();
        for (Entry<String, Long> replicaTimestamp : replicaLogicalTimestamps) {
            timestamps.setReplicaTimestamp(replicaTimestamp.getKey(), replicaTimestamp.getValue());
        }
        return timestamps;
    }
1067855
def variance(sample = false)
      a = numerify
      avg = a.average
      sum = a.inject(0) { |sum, value| sum + (value - avg) ** 2}
      (1 / (a.length.to_f - (sample ? 1 : 0)) * sum)
    end
1097799
def pstdev(data):
        """"""
        n = len(data)
        if n < 2:
            return 0
            # raise ValueError('variance requires at least two data points')
        ss = TableExtraction._ss(data)
        pvar = ss/n # the population variance
        return pvar**0.5
741590
def Kdiag(self, X):
        """"""
        K_ff = np.ones(X.shape[0])
        for i,x in enumerate(X):
            for il,l in enumerate(self.lengthscale):
                idx = il*2
                K_ff[i] *= self.k_ff(x[idx],x[idx],l)
        return K_ff * self.variances[0]
1008457
def _get_split_rhat(values, round_to=2):
    """"""
    shape = values.shape
    if len(shape) != 2:
        raise TypeError("Effective sample size calculation requires 2 dimensional arrays.")
    _, num_samples = shape
    num_split = num_samples // 2
    # Calculate split chain mean
    split_chain_mean1 = np.mean(values[:, :num_split], axis=1)
    split_chain_mean2 = np.mean(values[:, num_split:], axis=1)
    split_chain_mean = np.concatenate((split_chain_mean1, split_chain_mean2))
    # Calculate split chain variance
    split_chain_var1 = np.var(values[:, :num_split], axis=1, ddof=1)
    split_chain_var2 = np.var(values[:, num_split:], axis=1, ddof=1)
    split_chain_var = np.concatenate((split_chain_var1, split_chain_var2))
    # Calculate between-chain variance
    between_chain_variance = num_samples / 2 * np.var(split_chain_mean, ddof=1)
    # Calculate within-chain variance
    within_chain_variance = np.mean(split_chain_var)
    # Estimate of marginal posterior variance
    split_rhat = np.sqrt(
        (between_chain_variance / within_chain_variance + num_samples / 2 - 1) / (num_samples / 2)
    )

    return round(split_rhat, round_to)
1220013
def copy(self):
        """"""
        result = Vector3(self.size, self.deriv)
        result.x.v = self.x.v
        result.y.v = self.y.v
        result.z.v = self.z.v
        if self.deriv > 0:
            result.x.d[:] = self.x.d
            result.y.d[:] = self.y.d
            result.z.d[:] = self.z.d
        if self.deriv > 1:
            result.x.dd[:] = self.x.dd
            result.y.dd[:] = self.y.dd
            result.z.dd[:] = self.z.dd
        return result
469730
def _compute_average_correct(input_, labels, per_example_weights, topk=1):
  """"""
  return _compute_sparse_average_correct(
      input_,
      tf.reshape(tf.argmax(labels, 1), [-1, 1]), per_example_weights, topk=topk)
1045573
def average(sequence, key):
    """"""
    return sum(map(key, sequence)) / float(len(sequence))
763014
def Kdiag(self, X):
        """"""
        ret = np.empty(X.shape[0])
        ret[:] = self.variance
        return ret
1267366
def _sample_orthonormal_to(mu):
    """"""
    v = np.random.randn(mu.shape[0])
    proj_mu_v = mu * np.dot(mu, v) / np.linalg.norm(mu)
    orthto = v - proj_mu_v
    return orthto / np.linalg.norm(orthto)
1313529
def _get_warped_variance(self, mean, std, pred_init=None, deg_gauss_hermite=20):
        
        gh_samples, gh_weights = np.polynomial.hermite.hermgauss(deg_gauss_hermite)
        gh_samples = gh_samples[:, None]
        gh_weights = gh_weights[None, :]
        arg1 = gh_weights.dot(self._get_warped_term(mean, std, gh_samples, 
                                                    pred_init=pred_init) ** 2) / np.sqrt(np.pi)
        arg2 = self._get_warped_mean(mean, std, pred_init=pred_init,
                                     deg_gauss_hermite=deg_gauss_hermite)
        return arg1 - (arg2 ** 2)