text:
import logging
log = logging.getLogger(__name__)

import itertools
import numpy as np
from collections import OrderedDict
from pycqed.analysis import analysis_toolbox as a_tools
from pycqed.analysis_v3 import helper_functions as hlp_mod
from pycqed.analysis_v3 import processing_pipeline as pp_mod
from pycqed.measurement.calibration.calibration_points import CalibrationPoints

import sys
pp_mod.search_modules.add(sys.modules[__name__])

def filter_data(data_dict, keys_in, keys_out=None, **params):
    """
    Filters data in data_dict for each keys_in according to data_filter
    in params. Creates new keys_out in the data dict for the filtered data.

    To be used for example for filtering:
        - reset readouts
        - data with and without flux pulse/ pi pulse etc.

    :param data_dict: OrderedDict containing data to be processed and where
                    processed data is to be stored
    :param keys_in: list of key names or dictionary keys paths in
                    data_dict for the data to be processed
    :param keys_out: list of key names or dictionary keys paths in
                    data_dict for the processed data to be saved into
    :param params: keyword arguments:
        data_filter (str, default: 'lambda x: x'): filtering condition passed
            as a string that will be evaluated with eval.

    Assumptions:
        - if any keyo in keys_out contains a '.' string, keyo is assumed to
        indicate a path in the data_dict.
        - len(keys_out) == len(keys_in)
    """
    data_to_proc_dict = hlp_mod.get_data_to_process(data_dict, keys_in)
    if keys_out is None:
        keys_out = [k+' filtered' for k in keys_in]
    if len(keys_out) != len(data_to_proc_dict):
        raise ValueError('keys_out and keys_in do not have '
                         'the same length.')
    data_filter_func = hlp_mod.get_param('data_filter', data_dict,
                                  default_value=lambda data: data, **params)
    if hasattr(data_filter_func, '__iter__'):
        data_filter_func = eval(data_filter_func)
    for keyo, keyi in zip(keys_out, list(data_to_proc_dict)):
        hlp_mod.add_param(
            keyo, data_filter_func(data_to_proc_dict[keyi]), data_dict,
            **params)
    return data_dict

def get_std_deviation(data_dict, keys_in, keys_out=None, **params):
    """
    Finds the standard deviation of data in data_dict specified by keys_in
    along averaging_axis.

    To be used for example for:
        - shots
        - RB seeds

    :param data_dict: OrderedDict containing data to be processed and where
                    processed data is to be stored
    :param keys_in: list of key names or dictionary keys paths in
                    data_dict for the data to be processed
    :param keys_out: list of key names or dictionary keys paths in
                    data_dict for the processed data to be saved into
    :param params: keyword arguments:
    :param params: keyword arguments.:
        - shape (tuple of ints): shape into which to reshape array before
            averaging
        - averaging_axis (int): axis along which to average
        - final_shape (tuple of ints): shape into which to cast the array after
            averaging

    Assumptions:
        - if any keyo in keys_out contains a '.' string, keyo is assumed to
        indicate a path in the data_dict.
        - len(keys_out) == len(keys_in)
        - num_bins exists in params
        - num_bins exactly divides data_dict[keyi] for all keyi in keys_in.
    """
    data_to_proc_dict = hlp_mod.get_data_to_process(data_dict, keys_in)
    if keys_out is None:
        keys_out = [f'get_std_deviation.{k}' for k in keys_in]
    shape = hlp_mod.get_param('shape', data_dict, raise_error=True,
                              **params)
    final_shape = hlp_mod.get_param('final_shape', data_dict, **params)
    averaging_axis = hlp_mod.get_param('averaging_axis', data_dict,
                                       default_value=-1, **params)
    if len(keys_out) != len(data_to_proc_dict):
        raise ValueError('keys_out and keys_in do not have '
                         'the same length.')
    for k, keyi in enumerate(data_to_proc_dict):
        if len(data_to_proc_dict[keyi]) % shape[0] != 0:
            raise ValueError(f'{shape[0]} does not exactly divide '
                             f'data from ch {keyi} with length '
                             f'{len(data_to_proc_dict[keyi])}.')
        data_for_std = data_to_proc_dict[keyi] if shape is None else \
            np.reshape(data_to_proc_dict[keyi], shape)
        if final_shape is not None:
            data_for_std = np.reshape(data_for_std, final_shape)
        hlp_mod.add_param(
            keys_out[k], np.std(data_for_std, axis=averaging_axis), data_dict,
            **params)
    return data_dict

def classify_gm(data_dict, keys_in, keys_out=None, clf_params=None,
                thresholded=True, **params):
    """
    Predict gaussian mixture posterior probabilities for single shots
    of different levels of a qudit. Data to be classified expected in the
    shape (n_datapoints, n_channels). So there should be
    len(keys_in) == n_channels.
    :param data_dict: OrderedDict containing data to be processed and where
                    processed data is to be stored
    :param keys_in: list of key names or dictionary keys paths in
                    data_dict for the data to be processed (shots)
    :param keys_out: list of key names or dictionary keys paths in
                    data_dict for the processed data to be saved into
    :param clf_params: dictionary with parameters for
            Gaussian Mixture classifier:
                means_: array of means of each component of the GM
                covariances_: covariance matrix
                covariance_type: type of covariance matrix
                weights_: array of priors of being in each level. (n_levels,)
                precisions_cholesky_: array of precision_cholesky
            For more info see about parameters see :
            https://scikit-learn.org/stable/modules/generated/sklearn.mixture.
            GaussianMixture.html
    :param thresholded: whether to threshold the probabilities for each shot
        into a single state per shot. So [0.9, 0.05, 0.05] -> [1, 0, 0]
    :param params: keyword arguments:
        clf_params: list of dictionaries with parameters for
            Gaussian Mixture classifier:
                means_: array of means of each component of the GM
                covariances_: covariance matrix
                covariance_type: type of covariance matrix
                weights_: array of priors of being in each level. (n_levels,)
                precisions_cholesky_: array of precision_cholesky
            For more info see about parameters see :
            https://scikit-learn.org/stable/modules/generated/sklearn.mixture.
            GaussianMixture.html
    Stores an entry in data_dict for each classified state (path specified
    in keys_out).

    Assumptions:
        - if any keyo in keys_out contains a '.' string, keyo is assumed to
        indicate a path in the data_dict.
        - one meas_obj
    """

    mobjn = hlp_mod.get_measurement_properties(data_dict,
                                               props_to_extract=['mobjn'],
                                               enforce_one_meas_obj=True,
                                               **params)

    if clf_params is None:
        clf_params = hlp_mod.get_param(
            f'{mobjn}.acq_classifier_params', data_dict, raise_error=True,
            **params)

    data_to_proc_dict = hlp_mod.get_data_to_process(data_dict, keys_in)
    clf_data = a_tools.predict_gm_proba_from_clf(
        np.array(list(data_to_proc_dict.values())).T, clf_params)

    if thresholded:
        clf_data = np.array(np.isclose(
            np.repeat([np.arange(clf_data.shape[1])],
                      clf_data.shape[0], axis=0).T,
            np.argmax(clf_data, axis=1)).T, dtype=int)

    if keys_out is None:
        states = ['g', 'e', 'f']
        keys_out = [f'{mobjn}.classify_gm_{states[idx]}'
                    for idx in range(clf_data.shape[1])]

    if len(keys_out) != clf_params['means_'].shape[0]:
        raise ValueError(f'keys_in must have length '
                         f'{clf_params["means_"].shape[0]} but has length '
                         f'{len(keys_out)}.')

    for idx, keyo in enumerate(keys_out):
        hlp_mod.add_param(keyo, clf_data[:, idx], data_dict, **params)

def do_postselection_f_level(data_dict, keys_in, keys_out=None, **params):
    """
    Postselects on qubit being in f-level by setting the preselection bit to 1
    to the e-state shots. Only these are stored.
    This function is intended to  be used before a function that does
    preselection which will then filter out both preselection RO being in e
    and qubit being in f either in the preselection or in the actual measurement
    shots. Used before state tomo analysis for example.
    :param data_dict: OrderedDict containing data to be processed and where
        processed data is to be stored
    :param keys_in: key names or dictionary keys paths in data_dict for shots
        (with preselection) classified into pg, pe, pf
    :param keys_out: list of key names or dictionary keys paths in
        data_dict for the processed data to be saved into
    :param params: keyword arguments

    Assumptions:
     - one meas object
     - measurement must have been done with preselection
     -  len(keys_in) == 3 corresponding to g, e, f shots
     -  len(keys_out) == 1 where the filtered e shots are stored
    """
    if len(keys_in) != 3:
        raise ValueError('keys_in must have length 3.')
    if len(keys_out) != 1:
        raise ValueError('keys_out must have length 1.')

    data_to_proc_dict = hlp_mod.get_data_to_process(data_dict, keys_in)
    pg, pe, pf = list(data_to_proc_dict.values())

    mpre = np.logical_not(np.arange(pe.size) % 2)
    mmeas = (np.arange(pe.size) % 2).astype(np.bool)

    ro = pe.copy().astype(np.bool)

    # make sure segments where preselection readout is not g are filtered out
    ro[mpre] = np.logical_not(pg[mpre])

    # make sure segments where main readout is f are filtered out
    ro[mpre] = np.logical_or(ro[mpre], pf[mmeas])

    # write output
    hlp_mod.add_param(keys_out[0], ro, data_dict, **params)

def do_standard_preselection(data_dict, keys_in, keys_out=None,
                             joint_processing=False, **params):
    """
    Does standard preselection on the data shot arrays in data_dict specified
    by keys_in. Only the data shots for which the preselection readout
    preceding it found the qubit in the 0 state.
    :param data_dict: OrderedDict containing data to be processed and where
                    processed data is to be stored
    :param keys_in: list of key names or dictionary keys paths in
                    data_dict for the data to be processed
    :param keys_out: list of key names or dictionary keys paths in
                    data_dict for the processed data to be saved into
    :param joint_processing: bool specifying whether to preselect on all the
        measurement objects (arrays specified by keys_in) being in ground state
    :param params: keyword arguments

    Assumptions:
        - the data pointed to by keys_in is assumed to be 1D arrays of
            thresholded/classified shots
        - every other shot starting at 0 is assumed to be a preselection readout

    WARNING! The processed data array will not necessarily have the same length
    as the input array.
    """
    data_to_proc_dict = hlp_mod.get_data_to_process(data_dict, keys_in)
    if not joint_processing:
        for k, keyi in enumerate(data_to_proc_dict):
            th_shots = data_to_proc_dict[keyi]
            if not all(e in [0, 1, 2] for e in np.unique(th_shots)):
                raise TypeError(f'The data corresponding to {keyi} does not '
                                f'contain thresholded shots.')
            presel_shots = th_shots[::2]
            data_shots = th_shots[1::2]
            hlp_mod.add_param(
                keys_out[k], data_shots[
                    np.logical_not(np.ma.make_mask(presel_shots))],
                data_dict, **params)
    else:
        all_shots = np.array(list(data_to_proc_dict.values()))
        presel_states = all_shots[:, ::2]
        data_states = all_shots[:, 1::2]
        presel_data = data_states[:, np.where(
            np.all(presel_states.transpose() == (0, 0, 0, 0), axis=1))[0]]
        for k, keyo in enumerate(keys_out):
            hlp_mod.add_param(
                keyo, presel_data[k],
                data_dict, **params)

def do_preselection(data_dict, classified_data, keys_out, **params):
    """
    Keeps only the data for which the preselection readout data in
    classified_data satisfies the preselection condition.
    :param data_dict: OrderedDict containing data to be processed and where
                    processed data is to be stored
    :param classified_data: list of arrays of 0,1 for qubit, and
                    0,1,2 for qutrit, or list of keys pointing to the binary
                    (or trinary) arrays in the data_dict
    :param keys_out: list of key names or dictionary keys paths in
                    data_dict for the processed data to be saved into
    :param params: keyword arguments.:
        presel_ro_idxs (function, default: lambda idx: idx % 2 == 0):
            specifies which (classified) data entry is a preselection ro
        keys_in (list): list of key names or dictionary keys paths in
            data_dict for the data to be processed
        presel_condition (int, default: 0): 0, 1 (, or 2 for qutrit). Keeps
            data for which the data in classified data corresponding to
            preselection readouts satisfies presel_condition.

    Assumptions:
        - if any keyo in keys_out contains a '.' string, keyo is assumed to
        indicate a path in the data_dict.
        - len(keys_out) == len(classified_data)
        - if keys_in are given, len(keys_in) == len(classified_data)
        - classified_data either list of arrays or list of strings
        - if classified_data contains strings, assumes they are keys in
            data_dict
    """
    if len(keys_out) != len(classified_data):
        raise ValueError('classified_data and keys_out do not have '
                         'the same length.')

    keys_in = params.get('keys_in', None)
    presel_ro_idxs = hlp_mod.get_param('presel_ro_idxs', data_dict,
                               default_value=lambda idx: idx % 2 == 0, **params)
    presel_condition = hlp_mod.get_param('presel_condition', data_dict,
                                 default_value=0, **params)
    if keys_in is not None:
        if len(keys_in) != len(classified_data):
            raise ValueError('classified_data and keys_in do not have '
                             'the same length.')
        data_to_proc_dict = hlp_mod.get_data_to_process(data_dict, keys_in)
        for i, keyi in enumerate(data_to_proc_dict):
            # Check if the entry in classified_data is an array or a string
            # denoting a key in the data_dict
            if isinstance(classified_data[i], str):
                if classified_data[i] in data_dict:
                    classif_data = data_dict[classified_data[i]]
                else:
                    raise KeyError(
                        f'{classified_data[i]} not found in data_dict.')
            else:
                classif_data = classified_data[i]

            mask = np.zeros(len(data_to_proc_dict[keyi]))
            val = True
            for idx in range(len(data_to_proc_dict[keyi])):
                if presel_ro_idxs(idx):
                    val = (classif_data[idx] == presel_condition)
                    mask[idx] = False
                else:
                    mask[idx] = val
            preselected_data = data_to_proc_dict[keyi][mask]
            hlp_mod.add_param(keys_out[i], preselected_data, data_dict)
    else:
        for i, keyo in enumerate(keys_out):
            # Check if the entry in classified_data is an array or a string
            # denoting a key in the data_dict
            if isinstance(classified_data[i], str):
                if classified_data[i] in data_dict:
                    classif_data = data_dict[classified_data[i]]
                else:
                    raise KeyError(
                        f'{classified_data[i]} not found in data_dict.')
            else:
                classif_data = classified_data[i]

            mask = np.zeros(len(classif_data))
            val = True
            for idx in range(len(classif_data)):
                if presel_ro_idxs(idx):
                    val = (classif_data[idx] == 0)
                    mask[idx] = False
                else:
                    mask[idx] = val
            hlp_mod.add_param(keyo, classif_data[mask], data_dict)
    return data_dict

def average_data(data_dict, keys_in, keys_out=None, **params):
    """
    Averages data in data_dict specified by keys_in along averaging_axis.
    :param data_dict: OrderedDict containing data to be processed and where
                    processed data is to be stored
    :param keys_in: list of key names or dictionary keys paths in
                    data_dict for the data to be processed
    :param keys_out: list of key names or dictionary keys paths in
                    data_dict for the processed data to be saved into
    :param params: keyword arguments.:
        - shape (tuple of ints): shape into which to reshape array before
            averaging
        - averaging_axis (int): axis along which to average
        - final_shape (tuple of ints): shape into which to cast the array after
            averaging

    Assumptions:
        - if any keyo in keys_out contains a '.' string, keyo is assumed to
        indicate a path in the data_dict.
        - len(keys_out) == len(keys_in)
    """
    data_to_proc_dict = hlp_mod.get_data_to_process(data_dict, keys_in)
    if keys_out is None:
        keys_out = [f'average_data.{k}' for k in keys_in]
    shape = hlp_mod.get_param('shape', data_dict, **params)
    final_shape = hlp_mod.get_param('final_shape', data_dict, **params)
    averaging_axis = hlp_mod.get_param('averaging_axis', data_dict,
                                       default_value=-1, **params)

    if len(keys_out) != len(data_to_proc_dict):
        raise ValueError('keys_out and keys_in do not have '
                         'the same length.')
    for k, keyi in enumerate(data_to_proc_dict):
        if shape is not None and len(data_to_proc_dict[keyi]) % shape[0] != 0:
            raise ValueError(f'{shape[0]} does not exactly divide '
                             f'data from ch {keyi} with length '
                             f'{len(data_to_proc_dict[keyi])}.')
        data_to_avg = data_to_proc_dict[keyi] if shape is None else \
            np.reshape(data_to_proc_dict[keyi], shape)
        avg_data = np.mean(data_to_avg, axis=averaging_axis)
        if final_shape is not None:
            avg_data = np.reshape(avg_data, final_shape)
        hlp_mod.add_param(keys_out[k], avg_data, data_dict, **params)

def transform_data(data_dict, keys_in, keys_out, **params):
    """
    Maps data in data_dict specified by data_keys_in using transform_func
     callable (can be any function).
    :param data_dict: OrderedDict containing data to be processed and where
                    processed data is to be stored
    :param keys_in: list of key names or dictionary keys paths in
                    data_dict for the data to be processed
    :param keys_out: list of key names or dictionary keys paths in
                    data_dict for the processed data to be saved into
    :param params: keyword arguments.:
        transform_func (callable): string form of a callable or callable
            function
        transform_func_kwargs (dict): additional arguments to forward to
            transform_func

    """
    transform_func = hlp_mod.get_param('transform_func',
                                             data_dict, **params)
    tf_kwargs = hlp_mod.get_param('transform_func_kwargs', data_dict,
                                        default_value=dict(), **params)
    if transform_func is None:
        raise ValueError('mapping is not specified.')
    elif isinstance(transform_func, str):
        transform_func = eval(transform_func)
    data_to_proc_dict = hlp_mod.get_data_to_process(data_dict, keys_in)

    if len(keys_out) != len(data_to_proc_dict):
        raise ValueError('keys_out and keys_in do not have the same length.')

    for keyi, keyo in zip(data_to_proc_dict, keys_out):
        hlp_mod.add_param(
            keyo, transform_func(data_to_proc_dict[keyi], **tf_kwargs),
            data_dict)
    return data_dict

def correct_readout(data_dict, keys_in, keys_out=None, state_prob_mtxs=None,
                    **params):
    """
    Correct data of average state probabilities for readout errors using
    correct state assignment probability matrices.
    This function can handle multi-qubit inversion by taking the tensor
    product of the state assignment probability matrices.
    :param data_dict: OrderedDict containing data to be processed and where
                    processed data is to be stored
    :param keys_in: list of key names or dictionary keys paths in
                    data_dict for the data to be processed
    :param keys_out: list of key names or dictionary keys paths in
                    data_dict for the processed data to be saved into
    :param state_prob_mtxs: dict with meas_obj_names as keys and corresponding
                    correct state assignment probability matrix as values
    :param params: keyword arguments.

    Adds corrected data to data_dict as follows:
     - if keys_out is None, an n-d array will be stored under
        f'{",".join(meas_obj_names)}.correct_readout'
     - if len(keys_out) == 1, an n-d array will be stored under keys_out[0]
     - else, it checks that len(keys_out) == len(keys_in), and a separate
        1d array for each state probability will be stored under each
        keyo in keys_out

    Assumptions:
        - keys_in correspond to the data in the correct order with respect to
        the order of the rows and columns in the state_prob_mtx (usually
        'g', 'e', 'f').
        - meas_obj_names must be in either data_dict or params
        - data specified by keys_in is in the order specified by meas_obj_names
            such that the inversion makes sense
    """

    if state_prob_mtxs is None:
        state_prob_mtxs = {}

    meas_obj_names = hlp_mod.get_measurement_properties(
        data_dict, props_to_extract=['mobjn'], enforce_one_meas_obj=False,
        **params)
    data_to_proc_dict = hlp_mod.get_data_to_process(data_dict, keys_in)
    if len(data_to_proc_dict) == 1:
        uncorrected_data = np.array(
            data_to_proc_dict[list(data_to_proc_dict)[0]])
        num_states = len(uncorrected_data)
    else:
        uncorrected_data = np.stack(list(data_to_proc_dict.values()))
        num_states = uncorrected_data.shape[1]

    state_assign_matrix = [[1]]
    for mobjn in meas_obj_names:
        state_prob_mtx = state_prob_mtxs.get(mobjn, None)
        if state_prob_mtx is None:
            state_prob_mtx = hlp_mod.get_param(f'{mobjn}.state_prob_mtx',
                                               data_dict, **params)
            if state_prob_mtx is None:
                log.warning(f'state_prob_mtx was not found for {mobjn}. Setting '
                            f'it to identity.')
                qutrit = num_states == \
                         len(list(itertools.product(['g', 'e', 'f'],
                                                    repeat=len(meas_obj_names))))
                state_prob_mtx = np.identity(3 if qutrit else 2)
        state_assign_matrix = np.kron(state_assign_matrix, state_prob_mtx)

    if state_assign_matrix.shape[1] != uncorrected_data.shape[0]:
        uncorrected_data = uncorrected_data.T
    corrected_data = \
        (np.linalg.inv(state_assign_matrix).T @ uncorrected_data).T

    if keys_out is None:
        hlp_mod.add_param(
            f'{",".join(meas_obj_names)}.correct_readout',
            corrected_data, data_dict, **params)
    elif len(keys_out) == 1:
        hlp_mod.add_param(keys_out[0], corrected_data, data_dict, **params)
    else:
        if len(keys_out) != len(keys_in):
            raise ValueError(f'len(keys_out)={len(keys_out)} and '
                             f'len(keys_in)={len(keys_in)} must be the same.')
        for i, keyo in enumerate(keys_out):
            hlp_mod.add_param(keyo, corrected_data[:, i], data_dict, **params)

def rotate_iq(data_dict, keys_in, keys_out=None, **params):
    """
    Rotates IQ data based on information in the CalibrationPoints objects.
    :param data_dict: OrderedDict containing data to be processed and where
                processed data is to be stored
    :param keys_in: list of length-2 tuples of key names or dictionary
                keys paths in data_dict for the data to be processed
    :param keys_out: list of key names or dictionary keys paths in
                data_dict for the processed data to be saved into
    :param params: keyword arguments.:
        cal_points (CalibrationPoints object or its repr):
            CalibratinonPoints object for the meaj_obj_name, or its repr
        last_ge_pulse (bool): only for a measurement on ef. Whether a ge
            pi-pulse was applied before measurement.
        meas_obj_value_names_map (dict): {meaj_obj_name: [value_names]}.

    Assumptions:
        - if any keyo in keys_out contains a '.' character, keyo is assumed to
        indicate a path in the data_dict.
        - len(keys_in) == 2 must be True; the 2 entries are I and Q data
        - len(keys_out) == 1 must be True.
        - cal_points exists in **params, data_dict, or metadata
        - assumes meas_obj_names is one of the keys of the dicts returned by
        CalibrationPoints.get_indices(), CalibrationPoints.get_rotations()
        - keys_in exist in meas_obj_value_names_map
    """
    data_to_proc_dict = hlp_mod.get_data_to_process(
        data_dict, keys_in)
    keys_in = list(data_to_proc_dict)
    if keys_out is None:
        keys_out = ['rotated datadata [' + ','.join(keys_in)+']']
    if len(keys_in) != 2:
        raise ValueError(f'keys_in must have length two. {len(keys_in)} '
                         f'entries were given.')

    cp = hlp_mod.get_param('cal_points', data_dict, raise_error=True, **params)
    if isinstance(cp, str):
        cp = CalibrationPoints.from_string(cp)
    last_ge_pulse = hlp_mod.get_param('last_ge_pulse', data_dict,
                                      default_value=[], **params)
    mobjn = hlp_mod.get_param('meas_obj_names', data_dict,
                              raise_error=True, **params)
    if isinstance(mobjn, list):
        mobjn = mobjn[0]
    if mobjn not in cp.qb_names:
        raise KeyError(f'{mobjn} not found in cal_points.')

    rotations = cp.get_rotations(last_ge_pulses=last_ge_pulse)
    ordered_cal_states = []
    for ii in range(len(rotations[mobjn])):
        ordered_cal_states += \
            [k for k, idx in rotations[mobjn].items() if idx == ii]
    rotated_data, _, _ = \
        a_tools.rotate_and_normalize_data_IQ(
            data=list(data_to_proc_dict.values()),
            cal_zero_points=None if len(ordered_cal_states) == 0 else
                cp.get_indices()[mobjn][ordered_cal_states[0]],
            cal_one_points=None if len(ordered_cal_states) == 0 else
                cp.get_indices()[mobjn][ordered_cal_states[1]])
    hlp_mod.add_param(keys_out[0], rotated_data, data_dict)
    return data_dict

def rotate_1d_array(data_dict, keys_in, keys_out=None, **params):
    """
    Rotates 1d array based on information in the CalibrationPoints objects.
    The number of CalibrationPoints objects should equal the number of
    key names in keys_in.
    :param data_dict: OrderedDict containing data to be processed and where
                processed data is to be stored
    :param keys_in: list of key names or dictionary keys paths in
                data_dict for the data to be processed
    :param keys_out: list of key names or dictionary keys paths in
                data_dict for the processed data to be saved into
    :param params: keyword arguments.:
        cal_points_list (list): list of CalibrationPoints objects.
        last_ge_pulses (list): list of booleans

    Assumptions:
        - if any keyo in keys_out contains a '.' string, keyo is assumed to
        indicate a path in the data_dict.
        - len(keys_in) == len(keys_out) == 1 must all be True
        - cal_points exists in **params, data_dict, or metadata
        - assumes meas_obj_namess is one of the keys of the dicts returned by
        CalibrationPoints.get_indices(), CalibrationPoints.get_rotations()
        - keys_in exists in meas_obj_value_names_map
    """
    data_to_proc_dict = hlp_mod.get_data_to_process(data_dict, keys_in)
    keys_in = list(data_to_proc_dict)
    if keys_out is None:
        keys_out = [f'rotated data [{keys_in[0]}]']
    if len(keys_in) != 1:
        raise ValueError(f'keys_in must have length one. {len(keys_in)} '
                         f'entries were given.')
    if len(keys_out) != len(data_to_proc_dict):
        raise ValueError('keys_out and keys_in do not have '
                         'the same length.')
    cp = hlp_mod.get_param('cal_points', data_dict, raise_error=True,
                                 **params)
    if isinstance(cp, str):
        cp = CalibrationPoints.from_string(cp)
    last_ge_pulses = hlp_mod.get_param('last_ge_pulses', data_dict,
                                             default_value=[], **params)
    mobjn = hlp_mod.get_param('meas_obj_names', data_dict,
                                    raise_error=True, **params)
    if isinstance(mobjn, list):
        mobjn = mobjn[0]
    if mobjn not in cp.qb_names:
        raise KeyError(f'{mobjn} not found in cal_points.')

    rotations = cp.get_rotations(last_ge_pulses=last_ge_pulses)
    ordered_cal_states = []
    for ii in range(len(rotations[mobjn])):
        ordered_cal_states += \
            [k for k, idx in rotations[mobjn].items() if idx == ii]
    rotated_data = \
        a_tools.rotate_and_normalize_data_1ch(
            data=data_to_proc_dict[keys_in[0]],
            cal_zero_points=None if len(ordered_cal_states) == 0 else
                cp.get_indices()[mobjn][ordered_cal_states[0]],
            cal_one_points=None if len(ordered_cal_states) == 0 else
                cp.get_indices()[mobjn][ordered_cal_states[1]])
    hlp_mod.add_param(keys_out[0], rotated_data, data_dict)
    return data_dict

def classify_data(data_dict, keys_in, threshold_list, keys_out=None, **params):
    """
    Thresholds the data in data_dict specified by keys_in according to the
    threshold_mapping and the threshold values in threshold_list.
    This node will create nr_states entries in the data_dict, where
    nr_states = len(set(threshold_mapping.values())).
    :param data_dict: OrderedDict containing data to be processed and where
                    processed data is to be stored
    :param keys_in: list of key names or dictionary keys paths in
                    data_dict for the data to be processed
    :param keys_out: list of key names or dictionary keys paths in
                    data_dict for the processed data to be saved into
    :param threshold_list: list of values around which to threshold each
        data array corresponding to keys_in.
    :param params: keyword arguments.:
        threshold_map (dict): dict of the form {idx: state_label}.
            Ex: {0: 'e', 1: 'g', 2: 'f', 3: 'g'}. Default value if
            len(threshold_list) == 1 is {0: 'g', 1: 'e'}. Else, None and a
            ValueError will be raise.
        !!! IMPORTANT: quadrants 1 and 2 are reversed compared to the
        threshold_map we save in qubit preparation parameters !!!

    Assumptions:
        - len(threshold_list) == len(keys_in)
        - data arrays corresponding to keys_in must all have the same length
        - the order of the values in threshold_list is important!
        The thresholds are in the same order as the data corresponding to
        the keys_in. See the 3 lines after extraction of threshold_map below.
    """
    if not hasattr(threshold_list, '__iter__'):
        threshold_list = [threshold_list]

    data_to_proc_dict = hlp_mod.get_data_to_process(data_dict, keys_in)
    if len(threshold_list) != len(data_to_proc_dict):
        raise ValueError('threshold_list and keys_in do not have '
                         'the same length.')
    if len(set([arr.size for arr in data_to_proc_dict.values()])) > 1:
        raise ValueError('The data arrays corresponding to keys_in must all '
                         'have the same length.')
    keys_in = list(data_to_proc_dict)
    threshold_map = hlp_mod.get_param('threshold_map', data_dict,
                                      raise_error=False, **params)
    if threshold_map is None:
        if len(threshold_list) == 1:
            threshold_map = {0: 'g', 1: 'e'}
        else:
            raise ValueError(f'threshold_map was not found in either '
                             f'exp_metadata or input params.')
    if keys_out is None:
        keyo = keys_in[0] if len(keys_in) == 1 else ','.join(keys_in)
        keys_out = [f'{keyo} {s}' for s in set(threshold_map.values())]

    # generate boolean array of size (nr_data_pts_per_ch, len(keys_in).
    thresh_data_binary = np.stack(
        [data_to_proc_dict[keyi] >= th for keyi, th in
         zip(keys_in, threshold_list)], axis=1)

    # convert each row of thresh_data_binary into the decimal value whose
    # binary representation is given by the booleans in each row.
    # thresh_data_decimal is a 1d array of size nr_data_pts_per_ch
    thresh_data_decimal = thresh_data_binary.dot(1 << np.arange(
        thresh_data_binary.shape[-1] - 1, -1, -1))

    for k, state in enumerate(set(threshold_map.values())):
        dd = data_dict
        all_keys = keys_out[k].split('.')
        for i in range(len(all_keys)-1):
            if all_keys[i] not in dd:
                dd[all_keys[i]] = OrderedDict()
            dd = dd[all_keys[i]]
        hlp_mod.add_param(all_keys[-1], np.zeros(
            len(list(data_to_proc_dict.values())[0])), dd)

        # get the decimal values corresponding to state from threshold_map.
        state_idxs = [k for k, v in threshold_map.items() if v == state]

        for idx in state_idxs:
            dd[all_keys[-1]] = np.logical_or(
                dd[all_keys[-1]], thresh_data_decimal == idx).astype('int')

    return data_dict

def threshold_data(data_dict, keys_in, keys_out, ro_thresholds=None,
                   mapping=None, **params):
    """
    Thresholds the data in data_dict specified by keys_in about the
    threshold values in threshold_list (one for each keyi in keys_in).
    :param data_dict: OrderedDict containing data to be processed and where
                    processed data is to be stored
    :param keys_in: list of key names or dictionary keys paths in
                    data_dict for the data to be processed
    :param keys_out: list of key names or dictionary keys paths in
                    data_dict for the processed data to be saved into
    :param ro_thresholds: dict with keys meas_obj_names and values specifying
        the thresholds around which the data array for each meas_obj
        should be thresholded.
    :param mapping: either {0: 'g', 1: 'e'} or {0: 'e', 1: 'g'}; default is
        first one. Specifies thresholding condition.
    :param params: keyword arguments.

    Assumptions:
        - this function must be used for one meas_obj only! Meaning:
            - keys_in and keys_out have length 1
            - meas_obj_names exists in either data_dict or params and has one
                entry
    """
    if len(keys_in) != 1:
        raise ValueError('keys_in must have length 1.')
    if mapping is None:
        mapping = {0: 'g', 1: 'e'}

    mobjn = hlp_mod.get_measurement_properties(data_dict,
                                               props_to_extract=['mobjn'],
                                               enforce_one_meas_obj=True,
                                               **params)
    if ro_thresholds is None:
        acq_classifier_params = hlp_mod.get_param(
            f'{mobjn}.acq_classifier_params', data_dict, raise_error=True,
            **params)
        if 'thresholds' not in acq_classifier_params:
            raise KeyError(f'thresholds does not exist in the '
                           f'acq_classifier_params for {mobjn}.')
        ro_thresholds = {mobjn: acq_classifier_params['thresholds'][0]}
        mapping = acq_classifier_params.get('mapping', mapping)

    th_func = lambda dat, th: dat > th if mapping[1] == 'e' else dat < th
    data_to_proc_dict = hlp_mod.get_data_to_process(data_dict, keys_in)
    keys_in = list(data_to_proc_dict)
    if len(keys_out) != len(keys_in):
        raise ValueError('keys_out and keys_in do not have '
                         'the same length.')

    # generate boolean array of size (nr_data_pts_per_ch, len(keys_in).
    if mobjn not in ro_thresholds:
        raise KeyError(f'{mobjn} not found in ro_thresholds={ro_thresholds}.')
    threshold_list = [ro_thresholds[mobjn]]
    thresh_dat = np.stack(
        [th_func(data_to_proc_dict[keyi], th) for keyi, th in
         zip(keys_in, threshold_list)], axis=1)

    for i, keyo, in enumerate(keys_out):
        hlp_mod.add_param(
            keyo, thresh_dat[:, i].astype('int'), data_dict)

def correlate_qubits(data_dict, keys_in, keys_out, **params):
    """
    Calculated the ZZ correlator of the arrays of shots indicated by keys_in
    as follows:
        - correlator = 0 if even number of 0's
        - correlator = 1 if odd number of 0's
    :param data_dict: OrderedDict containing data to be processed and where
                    processed data is to be stored
    :param keys_in: list of key names or dictionary keys paths in
                    data_dict for the data to be processed
    :param keys_out: list with one entry specifying the key name or dictionary
        key path in data_dict for the processed data to be saved into
    :param params: keyword arguments.
    :return:
        Saves in data_dict, under keys_out[0], the np.array of correlated shots
            with the same dimension as one of the arrays indicated by keys_in

    Assumptions:
        - data must be THRESHOLDED single shots (0's and 1's)
        - all data arrays indicated by keys_in will be correlated
    """
    if len(keys_out) != 1:
        raise ValueError('keys_out must have length 1.')
    data_to_proc_dict = hlp_mod.get_data_to_process(data_dict, keys_in)
    all_data_arr = np.array(list(data_to_proc_dict.values()))
    if not np.all(np.logical_or(all_data_arr == 0, all_data_arr == 1)):
        raise ValueError('Not all shots have been thresholded.')
    hlp_mod.add_param(
        keys_out[0], np.sum(all_data_arr, axis=0) % 2, data_dict)

def calculate_probability_table(data_dict, keys_in, keys_out=None, **params):
    """
    Creates a general table of normalized counts averaging out all but
        specified set of correlations.
    :param data_dict: OrderedDict containing data to be processed and where
        processed data is to be stored
    :param keys_in: list of key names or dictionary keys paths in
        data_dict for the data to be processed (thresholded shots)
    :param observables: List of observables. Observable is a dictionary with
        name of the qubit as key and boolean value indicating if it is
        selecting exited states. If the qubit is missing from the list
        of states it is averaged out. Instead of just the qubit name, a
        tuple of qubit name and a shift value can be passed, where the
        shift value specifies the relative readout index for which the
        state is checked.
        Example qb2-qb4 state tomo with preselection:
                {'pre': {('qb2', -1): False,
                        ('qb4', -1): False}, # preselection conditions
                 '$\\| gg\\rangle$': {'qb2': False,
                                      'qb4': False,
                                      ('qb2', -1): False,
                                      ('qb4', -1): False},
                 '$\\| ge\\rangle$': {'qb2': False,
                                      'qb4': True,
                                      ('qb2', -1): False,
                                      ('qb4', -1): False},
                 '$\\| eg\\rangle$': {'qb2': True,
                                      'qb4': False,
                                      ('qb2', -1): False,
                                      ('qb4', -1): False},
                 '$\\| ee\\rangle$': {'qb2': True,
                                      'qb4': True,
                                      ('qb2', -1): False,
                                      ('qb4', -1): False}}
    :param n_readouts: Assumed to be the period in the list of shots between
        experiments with the same prepared state. If shots_of_qubits
        includes preselection readout results or if there was several
        readouts for a single readout then n_readouts has to include them.
    :param params: keyword arguments: used if get_observables is called
        - preselection_shift (int, default: -1)
        - do_preselection (bool, default: False)
        - return_counts (bool, default: False): whether to return raw counts
            for each state (True), or normalize the counts by
            n_readouts/n_shots (False)
    :return adds to data_dict, under keys_out, a dict with observables as keys
        and np.array of normalized counts with size n_readouts as values

    Assumptions:
        - len(keys_out) == 1 -> one probability table is calculated
        - !!! This function returns a dict not an array like the static method
        probability_table in readout_analysis.py/MultiQubit_SingleShot_Analysis
    """

    # Get shots_of_qubits: Dictionary of np.arrays of thresholded shots for
    # each qubit.
    data_to_proc_dict = hlp_mod.get_data_to_process(data_dict, keys_in)
    n_readouts = hlp_mod.get_param('n_readouts', data_dict, raise_error=True,
                                   **params)
    observables = hlp_mod.get_param('observables', data_dict,
                                    raise_error=True, **params)

    return_counts = hlp_mod.get_param('return_counts', data_dict,
                                      default_value=False, **params)

    n_shots = next(iter(data_to_proc_dict.values())).shape[0]
    table = OrderedDict({obs: np.zeros(n_readouts) for obs in observables})
    res_e = {}
    res_g = {}
    for keyi, results in data_to_proc_dict.items():
        mobjn = keyi.split('.')[0]
        res_e[mobjn] = np.array(results).reshape((n_readouts, -1),
                                                 order='F')
        # This makes copy, but allows faster AND later
        res_g[mobjn] = np.logical_not(
            np.array(results)).reshape((n_readouts, -1), order='F')

    for readout_n in range(n_readouts):
        # first result all ground
        for obs, states_of_mobjs in observables.items():
            mask = np.ones((n_shots//n_readouts), dtype=np.bool)
            # slow qubit is the first in channel_map list
            for mobjn, state in states_of_mobjs.items():
                if isinstance(mobjn, tuple):
                    seg = (readout_n+mobjn[1]) % n_readouts
                    mobjn = mobjn[0]
                else:
                    seg = readout_n
                if state:
                    mask = np.logical_and(mask, res_e[mobjn][seg])
                else:
                    mask = np.logical_and(mask, res_g[mobjn][seg])
            table[obs][readout_n] = np.count_nonzero(mask) * (
                    n_readouts/n_shots if not return_counts else 1)

    if keys_out is not None:
        if len(keys_out) != 1:
            raise ValueError(f'keys_out must have length one. {len(keys_out)} '
                             f'entries were given.')
        hlp_mod.add_param(keys_out[0], table, data_dict, **params)
    else:
        return table

def calculate_meas_ops_and_covariations(
        data_dict, observables, keys_out=None, meas_obj_names=None,
        **params):
    """
    Calculates the measurement operators corresponding to each observable and
        the expected covariance matrix between the operators from the
        observables and meas_obj_names.
    :param data_dict: OrderedDict containing data to be processed and where
        processed data is to be stored
    :param observables: measurement observables, see docstring of hlp_mod.
        get_observables. If not provided, it will default to hlp_mod.
        get_observables. See required input params there.
    :param keys_out: list of key names or dictionary keys paths in
        data_dict for the processed data to be saved into
    :param meas_obj_names: list of measurement object names
    :param params: keyword arguments: meas_obj_names can be passed here
    :return: adds to data_dict:
        - the measurement operators corresponding to each observable and the
            expected covariance matrix between the operators under keys_out
        - if keys_out is None, it will saves the aforementioned quantities
            under  'measurement_ops' and 'cov_matrix_meas_obs'
    Assumptions:
     - len(keys_out) == 2
     - order in keys_out corresponds to [measurement_operators, covar_matrix]
    """
    correct_readout = hlp_mod.get_param('correct_readout', data_dict, **params)
    if correct_readout is None:
        correct_readout = False

    if keys_out is None:
        keys_out = ['measurement_ops', 'cov_matrix_meas_obs']
        keys_out_container = hlp_mod.get_param('keys_out_container', data_dict,
                                               **params)
        if keys_out_container is not None:
            keys_out = [f'{keys_out_container}.{keyo}' for keyo in keys_out]
    if len(keys_out) != 2:
        raise ValueError(f'keys_out must have length 2. {len(keys_out)} '
                         f'entries were given.')

    if meas_obj_names is None:
        meas_obj_names = hlp_mod.get_measurement_properties(
            data_dict, props_to_extract=['mobjn'], enforce_one_meas_obj=False,
            **params)

    Fs = []
    if not correct_readout:
        Fsingle = {mobjn: {None: np.array([[1, 0], [0, 1]]),
                           True: np.array([[0, 0], [0, 1]]),
                           False: np.array([[1, 0], [0, 0]])}
                    for mobjn in meas_obj_names}
    else:
        state_prob_mtxs = hlp_mod.get_param('state_prob_mtxs', data_dict,
                                            **params,
                                            default_value={})
        Fsingle = {}
        for mobjn in meas_obj_names:
            state_prob_mtx = state_prob_mtxs.get(mobjn, None)
            if state_prob_mtx is None:
                state_prob_mtx = hlp_mod.get_param(f'{mobjn}.state_prob_mtx',
                                                   data_dict,
                                                   raise_error=True, **params)
            state_prob_mtx = state_prob_mtx[:2, :2]
            state_prob_mtx = state_prob_mtx/state_prob_mtx.sum(axis=1, keepdims=True)
            Fsingle[mobjn] = {
                None: np.array([[1, 0], [0, 1]]),
                False: np.diag(state_prob_mtx[:, 0]),
                True: np.diag(state_prob_mtx[:, 1]),
            }

    Omega = []
    for obs_name, obs in observables.items():
        if obs_name == 'pre':
            continue
        F = np.array([[1]])
        nr_meas = 0
        for qb in meas_obj_names:
            # TODO: does not handle conditions on previous readouts
            Fqb = Fsingle[qb][obs.get(qb, None)]
            # Kronecker product convention - assumed the same as QuTiP
            F = np.kron(F, Fqb)
            if qb in obs:
                nr_meas += 1
        Fs.append(F)
        # The variation is proportional to the number of qubits we have
        # a condition on, assuming that all readout errors are small
        # and equal.
        Omega.append(nr_meas)
    Omega = np.array(Omega)

    hlp_mod.add_param(keys_out[0], Fs, data_dict, **params)
    hlp_mod.add_param(keys_out[1], Omega, data_dict, **params)

def calculate_meas_ops_and_covariations_cal_points(
        data_dict, keys_in, observables, keys_out=None, **params):
    """
    Calculates the measurement operators corresponding to each observable and
        the expected covariance matrix between the operators from the
        observables and calibration points.
    Applies readout correction based on calibration segments.

    :param data_dict: OrderedDict containing data to be processed and where
        processed data is to be stored
    :param keys_in: list of key names or dictionary keys paths in data_dict
        for the data to be processed (thresholded shots)
    :param observables: measurement observables, see docstring of hlp_mod.
        get_observables. If not provided, it will default to hlp_mod.
        get_observables. See required input params there.
    :param keys_out: list of key names or dictionary keys paths in
        data_dict for the processed data to be saved into
    :param params: keyword arguments:
        Expects to find either in data_dict or in params:
            - cal_points:  repr of instance of CalibrationPoints
            - preparation_params: preparation parameters as stored in
                QuDev_transmon or Device. Used in CalibrationPoints.get_indices()
            - meas_obj_names: list of measurement object names
            - probability_table if it is not in data_dict; see
                calculate_probability_table for details.
            - n_readouts: the total number of readouts including
                preselection.
    :return: adds to data_dict:
        - the measurement operators corresponding to each observable and the
            expected covariance matrix between the operators under keys_out
        - if keys_out is None, it will saves the aforementioned quantities
            under 'measurement_ops' and 'cov_matrix_meas_obs'

    Assumptions:
        - all qubits in CalibrationPoints have the same cal point indices
        - There must be a calibration segment for each of the computational
        basis states of the Hilbert space.
    """
    if keys_out is None:
        keys_out = ['measurement_ops', 'cov_matrix_meas_obs']
        keys_out_container = hlp_mod.get_param('keys_out_container', data_dict,
                                               **params)
        if keys_out_container is not None:
            keys_out = [f'{keys_out_container}.{keyo}' for keyo in keys_out]
    if len(keys_out) != 2:
        raise ValueError(f'keys_out must have length 2. {len(keys_out)} '
                         f'entries were given.')

    prob_table = hlp_mod.get_param('probability_table', data_dict,
                                   raise_error=True, **params)
    prob_table = np.array(list(prob_table.values())).T
    cp = hlp_mod.get_measurement_properties(data_dict, props_to_extract=['cp'],
                                            **params)
    meas_obj_names = hlp_mod.get_measurement_properties(
        data_dict, props_to_extract=['mobjn'], enforce_one_meas_obj=False,
        **params)
    prep_params = hlp_mod.get_param('preparation_params', data_dict, **params)

    try:
        preselection_obs_idx = list(observables.keys()).index('pre')
    except ValueError:
        preselection_obs_idx = None
    observable_idxs = [i for i in range(len(observables))
                       if i != preselection_obs_idx]

    # calculate the mean for each reference state and each observable
    cp_indices = cp.get_indices(meas_obj_names, prep_params)
    cal_readouts = hlp_mod.flatten_list(
        [cp_indices[list(cp_indices)[0]].get(state, [])
         for state in ['g', 'e', 'f', 'h']])
    means = np.array([np.mean([prob_table[cal_idx][observable_idxs]], axis=0)
                      for cal_idx in cal_readouts])
    # normalize the assignment matrix
    for r in range(means.shape[0]):
        means[r] /= means[r].sum()
    Fs = [np.diag(ms) for ms in means.T]

    # find the means for all the products of the operators and the average
    # covariation of the operators
    observables_data = {k: v for k, v in observables.items() if k != 'pre'}
    n_readouts = hlp_mod.get_param('n_readouts', data_dict, raise_error=True,
                                   **params)
    prod_obss = OrderedDict()
    prod_obs_idxs = {}
    obs_products = np.zeros([n_readouts] + [len(observables_data)]*2)
    for i, obs in enumerate(observables_data):
        obsi = observables_data[obs]
        for j, obsj in enumerate(observables_data.values()):
            if i > j:
                continue
            obsp = hlp_mod.observable_product(obsi, obsj)
            if obsp is None:
                obs_products[:, i, j] = 0
                obs_products[:, j, i] = 0
            else:
                prod_obs_idxs[(i, j)] = len(prod_obss)
                prod_obs_idxs[(j, i)] = len(prod_obss)
                prod_obss[obs] = obsp

    prod_prob_table = calculate_probability_table(data_dict, keys_in=keys_in,
                                                  observables=prod_obss,
                                                  n_readouts=n_readouts)
    prod_prob_table = np.array(list(prod_prob_table.values())).T
    for (i, j), k in prod_obs_idxs.items():
        obs_products[:, i, j] = prod_prob_table[:, k]
    Omega = -np.array([np.outer(ro, ro) for ro in
                        prob_table[:, observable_idxs]]) + obs_products
    Omega = np.mean(Omega[list(cal_readouts)], 0)

    hlp_mod.add_param(keys_out[0], Fs, data_dict, **params)
    hlp_mod.add_param(keys_out[1], Omega, data_dict, **params)

def count_states(data_dict, keys_in, keys_out, states=None, n_meas_objs=None,
                 **params):
    """
    Averages data in data_dict specified by keys_in into num_bins.
    :param data_dict: OrderedDict containing data to be processed and where
                    processed data is to be stored
    :param keys_in: list of key names or dictionary keys paths in
                    data_dict for the data to be processed
    :param keys_out: list of key names or dictionary keys paths in
                    data_dict for the processed data to be saved into
    :param states: list of tuples of 0, 1, (2 if qutrits) denoting qubit/qutrit
        basis states to match
    :param n_meas_objs: number of measurement objects. If this is given and
        states is None, all possible basis states for n_meas_objs qubits will
        be generated
    :param params: keyword arguments

    Assumptions:
        - each array pointed to by keys_in is assumed to correspond to one of
            the qubits/qutrits in the basis states. IMPORTANT TO SPECIFY
            KEYS_IN IN THE CORRECT ORDER, SUCH THAT ARRAYS CORRESPOND TO YOUR
            STATE
        - the data pointed to by keys_in is assumed to be 1D arrays of
            thresholded/classified shots
        - len(keys_out) == 1
    """

    if len(keys_out) != 1:
        raise ValueError(f'keys_out must have length one. {len(keys_out)} '
                         f'entries were given.')
    if states is None:
        if n_meas_objs is None:
            raise ValueError('Please specify either states or n_meas_objs.')
        states = itertools.product((0, 1), repeat=n_meas_objs)

    data_to_proc_dict = hlp_mod.get_data_to_process(data_dict, keys_in)
    shots = np.array(list(data_to_proc_dict.values()))
    state_counts = np.array([
        np.count_nonzero(np.all(shots.transpose() == state, axis=1))
        for state in states])
    hlp_mod.add_param(keys_out[0], state_counts, data_dict, **params)

def data_from_analysis_v2(data_dict, class_name, class_params,
                          keys_in=None, keys_out=None, **params):
    """
    Instantiates an analysis class with name class_name and input parameters
    class_params from either timedomain_analysis.py or readout_analysis.py.
    :param data_dict: OrderedDict where the data from the analysis class will
        be stored.
    :param class_name: string specifying the name of the analysis class to run
    :param class_params: keyword input parameters to the analysis class
    :param keys_in: list of paths inside the __dict__ of the analysis instance
        pointing to the parameters that should be extracted and stored in
        data_dict
    :param keys_out: list of paths inside data_dict where the value of the
        parameters corresponding to keys_in are to be stored.
    :param params: keyword arguments passed to hlp_mod.add_param
    """
    from pycqed.analysis_v2 import timedomain_analysis as tda
    from pycqed.analysis_v2 import readout_analysis as ra
    try:
        ana = getattr(tda, class_name)
    except AttributeError:
        try:
            ana = getattr(ra, class_name)
        except AttributeError:
            raise AttributeError(
                f'{class_name} was not found in either '
                f'timedomain_analysis.py or readout_analysis.py.')

    # run analysis
    options_dict = class_params.get('options_dict', {})
    options_dict['delegate_plotting'] = options_dict.get('delegate_plotting',
                                                         False)
    class_params['options_dict'] = options_dict
    ana = ana(**class_params)

    if keys_in is None:
        keys_in = ['proc_data_dict']
        if keys_out is None:
            keys_out = [f'proc_data_dict_{class_name}']

    if keys_out is None:
        keys_out = [f'{keyi}_{class_name}' for keyi in keys_in]

    params['add_param_method'] = params.get('add_param_method', 'replace')
    for keyi, keyo in zip(keys_in, keys_out):
        data = hlp_mod.get_param(keyi, ana.__dict__)
        if data is None:
            log.warning(f'{keyi} was not found in {class_name} instance.')
            continue
        hlp_mod.add_param(keyo, data, data_dict, **params)

def extract_leakage_classified_shots(data_dict, keys_in, keys_out=None,
                                     **params):
    """
    Extracts leakage from classified shots with preselection when either of the
    qubits was in f and when all qubits were in f.
    :param data_dict: OrderedDict containing data to be processed and where
        processed data is to be stored
    :param keys_in: key names or dictionary keys paths in data_dict for shots
        (with preselection) classified into pg, pe, pf
    :param keys_out: list of key names or dictionary keys paths in
        data_dict for the processed data to be saved into
    :param state_prob_mtxs: dict with keys meas_obj_names and values specifying
        the readout correction matrix.
    :param params: keyword arguments

    Adds corrected data to data_dict as follows:
     - if keys_out is None, the probabilities of each qubit leaking and of
        either qubit leaking are stored under
        [f'extract_leakage_classified_shots.{name}_leaked' for name in
        [f'{n}_leaked' for n in meas_obj_names + ['any']]]
     - else, checks that len(keys_out) == 1, and stores the dict with keys
        [f'{n}_leaked' for n in meas_obj_names + ['any']]] under keys_out[0]

    Assumptions:
        - meas_obj_names must exist in either data_dict or params and
            len(meas_obj_names) == 2
        - data specified by keys_in is in the order specified by meas_obj_names
            - if len(keys_in) == 1, then the array it points to must have the
            last axis equal to the total number of state probabilities
            (for example as returned by calculate_flat_mutliqubit_shots)
            in the order that would result from doing tensor product on the
            qubits as specified by the order in meas_obj_names
            - else, checks that len(keys_in) equals the total number of
            state probabilities
    """
    meas_obj_names = hlp_mod.get_measurement_properties(
        data_dict, props_to_extract=['mobjn'], enforce_one_meas_obj=False,
        **params)

    data_to_proc_dict = hlp_mod.get_data_to_process(data_dict, keys_in)
    if len(data_to_proc_dict) == 1:
        ps_mean_corr = np.array(data_to_proc_dict[list(data_to_proc_dict)[0]])
    else:
        ps_mean_corr = np.stack(list(data_to_proc_dict.values())).T

    # extract leakage probabilities for individual qubits
    masks_f = {}
    for mobjn in meas_obj_names:
        res = [1]
        for mobjn2 in meas_obj_names:
            res = np.kron(res, [0, 0, 1] if mobjn == mobjn2 else [1, 1, 1])
        masks_f[mobjn] = res

    # check that the size of the last axis in the data array matches the
    # length of res
    if ps_mean_corr.shape[-1] != len(masks_f[mobjn]):
        raise ValueError(f'No axes of data array with shape '
                         f'{ps_mean_corr.shape} matches the expected '
                         f'length of {len(masks_f[mobjn])} based on '
                         f'meas_obj_names.')

    # extract probability of each qubit leaked
    proba_f = {f'{mobjn}_leaked': np.sum(ps_mean_corr*masks_f[mobjn],
                                         axis=-1)
               for mobjn in meas_obj_names}

    # extract probability of any qubit leaked
    proba_f['any_leaked'] = np.sum(ps_mean_corr*(np.sum(
        list(masks_f.values()), axis=0) > 0), axis=-1)

    keys_out_container = hlp_mod.get_param(
        'keys_out_container', data_dict,
        default_value='extract_leakage_classified_shots', **params)
    if keys_out is None:
        for mobjn in meas_obj_names:
            hlp_mod.add_param(
                f'{keys_out_container}.{mobjn}_leaked',
                proba_f[f'{mobjn}_leaked'], data_dict, **params)

        hlp_mod.add_param(f'{keys_out_container}.any_leaked',
                          proba_f['any_leaked'], data_dict, **params)
    else:
        if len(keys_out) != 1:
            raise ValueError(f'keys_out must have length 1 but has '
                             f'length {len(keys_out)}')
        hlp_mod.add_param(keys_out[0], proba_f, data_dict, **params)

def calculate_flat_multiqubit_shots(data_dict, keys_in, keys_out=None,
                                    **params):
    """
    Calculates the full len(meas_obj_names)-qubit correlator between all
    states provided (given by data shape)
    :param data_dict: OrderedDict containing data to be processed and where
        processed data is to be stored
    :param keys_in: key names or dictionary keys paths in data_dict for shots
        (with preselection) classified into pg, pe, pf
    :param keys_out: list of key names or dictionary keys paths in
        data_dict for the processed data to be saved into
    :param params: keyword arguments
        - do_preselection (bool, default: True): whether every other shot is a
        preselection readout

    Adds array of shape (nr_shots_excluding_preselection, nr_correlated_states)
    to data_dict as follows:
     - if keys_out is None, adds under the path
        f'{",".join(meas_obj_names)}.calculate_flat_multiqubit_shots',
     - else, checks that len(keys_out) == 1, adds under keys_out[0]

    Assumptions:
        - meas_obj_names must exist in either data_dict or params
        - the order of the calculated correlator states is given by the order
            of the qubits in meas_obj_names!!!
        - data specified by keys_in corresponds to classified and thresholded
            shots such that for each shot, only one state is 1, the others 0.
    """
    meas_obj_names = hlp_mod.get_measurement_properties(
        data_dict, props_to_extract=['mobjn'], enforce_one_meas_obj=False,
        **params)

    data_to_proc_dict = hlp_mod.get_data_to_process(data_dict, keys_in)

    do_preselection = hlp_mod.get_param('do_preselection', data_dict,
                                        default_value=True, **params)

    # extract probabilities for the transmon states: pg, pe, pf for each qubit
    ps = {}
    is_empty = False
    for mobjn in meas_obj_names:
        ps[mobjn] = np.array(
            [v for k, v in data_to_proc_dict.items() if mobjn in k])
        is_empty = is_empty or (len(ps[mobjn]) == 0)

    if is_empty:
        log.warning('Unable to determine which keys_in correspond to which '
                    'measurement object. Creating the data array from the '
                    'order specified by keys_in.')
        for i, mobjn in enumerate(meas_obj_names):
            ps[mobjn] = np.array(list(data_to_proc_dict.values())[i])

    nr_shots = ps[meas_obj_names[0]][0].size
    presel_mask = np.ones(nr_shots)
    if do_preselection:
        nr_shots //= 2
        # distinguish between measurement and preselection shots
        presel_segs = np.logical_not(np.arange(2*nr_shots) % 2)

        # calculate preselection mask: True when the shots are to be kept
        presel_mask = np.ones(nr_shots)
        for mobjn in meas_obj_names:
            presel_mask = np.logical_and(presel_mask, ps[mobjn][0][presel_segs])
    presel_mask = presel_mask.astype(bool)

    # Calculate flat multiqubit shots
    # e.g. [gg, ge, gf, eg, ee, ef, fg, fe, ff] for two qubits
    # Use the Einstein summation function numpy.einsum, which, when reshaped,
    # implements a Kronecker product of a set of arrays.

    # First create the subscripts for summation string
    # e.g. 'na,nb->nab' for 2 arrays (2 qubits),
    # or 'na,nb,nc->nabc' for 3 arrays (3 qubits)
    # Define the letter of the alphabet
    alphabet = list(map(chr, range(97, 123)))
    # Create the comma-separated string on the left hand side of "->"
    s = ','.join(['n'+alphabet[i] for i in range(len(meas_obj_names))])
    # Append "->" + the string on the right hand side of it
    s += '->n'+''.join([alphabet[i] for i in range(len(meas_obj_names))])
    # Create the list of arrays to be Einstein-summed
    if do_preselection:
        # Exclude preselection shots: keep every other second shot
        data_arr_list = [ps[mobjn].T[1::2][list(presel_mask)]
                         for mobjn in meas_obj_names]
    else:
        # Take all shots
        data_arr_list = [ps[mobjn].T[list(presel_mask)]
                         for mobjn in meas_obj_names]
    # Perform Einstein summation. The output will have shape
    # (len(data_arr_list), np.sqrt(nr_states**n), np.sqrt(nr_states**n))
    # with n = len(meas_obj_names), so we need to reshape it to the final shape
    # (len(data_arr_list), nr_states**n).
    ps_flat = np.einsum(s, *data_arr_list).reshape(data_arr_list[0].shape[0], -1)

    # Add ps_flat to data_dict
    if keys_out is None:
        hlp_mod.add_param(
            f'{",".join(meas_obj_names)}.calculate_flat_multiqubit_shots',
            ps_flat, data_dict, **params)
    else:
        if len(keys_out) != 1:
            raise ValueError(f'keys_out must have length 1 but has '
                             f'length {len(keys_out)}')
        hlp_mod.add_param(keys_out[0], ps_flat, data_dict, **params)


text:
namespace std {
template <typename>
class initializer_list{};
int a;
auto c = a, &d = {a}; // expected-error {{'auto' deduced as 'int'}} \
                         expected-error {{non-const lvalue reference to type}}
} // namespace std

redpajama_set_name: RedPajamaGithub


text:
<?php

namespace League\Uri\Modifiers\Filters;

use InvalidArgumentException;

/**
 * Hierarchical Component index trait
 *
 * @package League.uri
 * @author  Ignace Nyamagana Butera <nyamsprod@gmail.com>
 * @since   4.0.0
 * @internal
 */
trait Offset
{
    /**
     * Offset key to replace the content
     *
     * @var int
     */
    protected $offset;

    /**
     * Return a new modifier with a new offset key
     *
     * DEPRECATION WARNING! This method will be removed in the next major point release
     *
     * @deprecated deprecated since version 4.1
     *
     * @param int $offset
     *
     * @return $this
     */
    public function withOffset($offset)
    {
        $clone = clone $this;
        $clone->offset = $this->filterOffset($offset);

        return $clone;
    }

    /**
     * Filter and validate the offset key
     *
     * @param int $offset
     *
     * @throws InvalidArgumentException if the Offset key is invalid
     *
     * @return int
     */
    protected function filterOffset($offset)
    {
        $offset = filter_var($offset, FILTER_VALIDATE_INT, ['options' => ['min_range' => 0]]);
        if (false === $offset) {
            throw new InvalidArgumentException('The submitted index is invalid');
        }

        return $offset;
    }
}


text:
<?xml version="1.0" encoding="UTF-8"?>
<assessmentItem xmlns="http://www.imsglobal.org/xsd/imsqti_item_v2p0"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://www.imsglobal.org/xsd/imsqti_item_v2p0 ../imsqti_item_v2p0.xsd"
  identifier="upload" title="File Upload" adaptive="false" timeDependent="false">
  <responseDeclaration identifier="RESPONSE" cardinality="single" baseType="file"/>
  <outcomeDeclaration identifier="SCORE" cardinality="single" baseType="integer"/>
  <itemBody>
    ITEM_TEXT<!-- SET -->
    <uploadInteraction responseIdentifier="RESPONSE">
    FILE_UPLOAD_COMPONENT<!-- SET -->
    </uploadInteraction>
  </itemBody>
</assessmentItem>


text:
<?xml version="1.0" encoding="utf-8" standalone="yes" ?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>W.S. | R. Calen Walshe</title>
    <link>/authors/w.s./</link>
      <atom:link href="/authors/w.s./index.xml" rel="self" type="application/rss+xml" />
    <description>W.S.</description>
    <generator>Source Themes Academic (https://sourcethemes.com/academic/)</generator><language>en-us</language>
    <image>
      <url>/img/icon-192.png</url>
      <title>W.S.</title>
      <link>/authors/w.s./</link>
    </image>

    <item>
      <title>Detection of Occluding Targets in Natural Images</title>
      <link>/publication_bak/jov_2020/</link>
      <pubDate>Thu, 31 Dec 2020 00:00:00 +0000</pubDate>
      <guid>/publication_bak/jov_2020/</guid>
      <description></description>
    </item>

  </channel>
</rss>


text:
require 'rails_helper'

RSpec.describe "topics/index", type: :view do
  before(:each) do
    assign(:topics, [
      Topic.create!(
        :description => "Description",
        :status => "active",
        :topic => nil
      ),
      Topic.create!(
        :description => "Description",
        :status => "active",
        :topic => nil
      )
    ])
  end

  it "renders a list of topics" do
    @total = 2
    @limit = 10
    @page = 0
    render
    assert_select "ul>li", :text => /Description/, :count => 2
  end
end


text:
package net.wolfgangwerner.kata.set;

import org.junit.Test;

import static org.junit.Assert.assertTrue;

public class SetEqualityTest {

    @Test
    public void setsWithSameCardsInDifferentOrderAreConsideredEqual() {
        Card c0 = Card.of(Shape.SQUIGGLE, Shading.OUTLINED, Color.GREEN, Count.TWO);
        Card c1 = Card.of(Shape.SQUIGGLE, Shading.STRIPED, Color.RED, Count.ONE);
        Card c2 = Card.of(Shape.OVAL, Shading.OUTLINED, Color.RED, Count.ONE);

        CardSet one = new CardSet(c0, c1, c2);
        CardSet two = new CardSet(c2, c0, c1);

        assertTrue(one.equals(two));
    }

    @Test
    public void setsWithSameCardsInDifferentOrderProduceSameHashcode() {
        Card c0 = Card.of(Shape.SQUIGGLE, Shading.OUTLINED, Color.GREEN, Count.TWO);
        Card c1 = Card.of(Shape.SQUIGGLE, Shading.STRIPED, Color.RED, Count.ONE);
        Card c2 = Card.of(Shape.OVAL, Shading.OUTLINED, Color.RED, Count.ONE);

        CardSet one = new CardSet(c0, c1, c2);
        CardSet two = new CardSet(c1, c2, c0);
        assertTrue(one.hashCode() == two.hashCode());
    }
}


text:
package com.bluelinelabs.logansquare.processor;

import com.bluelinelabs.logansquare.JsonMapper;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParser;
import java.io.IOException;
import java.lang.Override;
import java.lang.String;
import java.lang.SuppressWarnings;

@SuppressWarnings("unsafe,unchecked")
public final class EnumModel$$JsonObjectMapper extends JsonMapper<EnumModel> {
    protected static final EnumModel.EnumTypeConverter COM_BLUELINELABS_LOGANSQUARE_PROCESSOR_ENUMMODEL_ENUMTYPECONVERTER = new EnumModel.EnumTypeConverter();

    @Override
    public EnumModel parse(JsonParser jsonParser) throws IOException {
        return null;
    }

    @Override
    public void parseField(EnumModel instance, String fieldName, JsonParser jsonParser) throws IOException {
        if ("enum".equals(fieldName)) {
            instance.myEnum = COM_BLUELINELABS_LOGANSQUARE_PROCESSOR_ENUMMODEL_ENUMTYPECONVERTER.parse(jsonParser);
        }
    }

    @Override
    public void serialize(EnumModel object, JsonGenerator jsonGenerator, boolean writeStartAndEnd) throws IOException {
        if (writeStartAndEnd) {
            jsonGenerator.writeStartObject();
        }
        COM_BLUELINELABS_LOGANSQUARE_PROCESSOR_ENUMMODEL_ENUMTYPECONVERTER.serialize(object.myEnum, "enum", true, jsonGenerator);
        if (writeStartAndEnd) {
            jsonGenerator.writeEndObject();
        }
    }
}


text:
package org.sakaiproject.jsf2.tag;

import javax.faces.component.UIComponent;
import javax.faces.webapp.UIComponentTag;

import lombok.Data;

import org.sakaiproject.jsf2.util.TagUtil;

/**
 *

 *
Description:

 * This class is the tag handler that evaluates the timerBar
 * custom tag.

 */

 @Data
public class TimerBarTag
  extends UIComponentTag
{

  private String id = null;
  private String expireScript;
  private String expireMessage;

  private String elapsed;
  private String height;
  private String wait;
  private String width;
  private String elapsedColor;
  private String remainingColor;
  private String expireHandling;

  public String getComponentType()
  {
    return ("javax.faces.Output");
  }

  public String getRendererType()
  {
    return "org.sakaiproject.TimerBar";
  }

  protected void setProperties(UIComponent component)
  {
    super.setProperties(component);

    TagUtil.setInteger(component, "height", height);
    TagUtil.setInteger(component, "width", width);
    TagUtil.setInteger(component, "wait", wait);
    TagUtil.setInteger(component, "elapsed", elapsed);
    TagUtil.setString(component, "expireScript", expireScript);
    TagUtil.setString(component, "expireMessage", expireMessage);
    TagUtil.setString(component, "elapsedColor", elapsedColor);
    TagUtil.setString(component, "remainingColor", remainingColor);
    TagUtil.setString(component, "expireHandling", expireHandling);
  }
}


text:
package android.app.backup;

import android.os.ParcelFileDescriptor;

import java.io.FileDescriptor;
import java.io.IOException;

/**
 * Provides the structured interface through which a {@link BackupAgent} commits
 * information to the backup data set, via its {@link
 * BackupAgent#onBackup(ParcelFileDescriptor,BackupDataOutput,ParcelFileDescriptor)
 * onBackup()} method.  Data written for backup is presented
 * as a set of "entities," key/value pairs in which each binary data record "value" is
 * named with a string "key."
 *

 * To commit a data record to the backup transport, the agent's
 * {@link BackupAgent#onBackup(ParcelFileDescriptor,BackupDataOutput,ParcelFileDescriptor)
 * onBackup()} method first writes an "entity header" that supplies the key string for the record
 * and the total size of the binary value for the record.  After the header has been
 * written, the agent then writes the binary entity value itself.  The entity value can
 * be written in multiple chunks if desired, as long as the total count of bytes written
 * matches what was supplied to {@link #writeEntityHeader(String, int) writeEntityHeader()}.
 *

 * Entity key strings are considered to be unique within a given application's backup
 * data set. If a backup agent writes a new entity under an existing key string, its value will
 * replace any previous value in the transport's remote data store.  You can remove a record
 * entirely from the remote data set by writing a new entity header using the
 * existing record's key, but supplying a negative dataSize parameter.
 * When you do so, the agent does not need to call {@link #writeEntityData(byte[], int)}.
 *
Example

 *

 * Here is an example illustrating a way to back up the value of a String variable
 * called mStringToBackUp:
 *

 * static final String MY_STRING_KEY = "storedstring";
 *
 * public void {@link BackupAgent#onBackup(ParcelFileDescriptor, BackupDataOutput, ParcelFileDescriptor) onBackup(ParcelFileDescriptor oldState, BackupDataOutput data, ParcelFileDescriptor newState)}
 *         throws IOException {
 *     ...
 *     byte[] stringBytes = mStringToBackUp.getBytes();
 *     data.writeEntityHeader(MY_STRING_KEY, stringBytes.length);
 *     data.writeEntityData(stringBytes, stringBytes.length);
 *     ...
 * }

 *
 * @see BackupAgent
 */
public class BackupDataOutput {
    long mBackupWriter;

    /** @hide */
    public BackupDataOutput(FileDescriptor fd) {
        if (fd == null) throw new NullPointerException();
        mBackupWriter = ctor(fd);
        if (mBackupWriter == 0) {
            throw new RuntimeException("Native initialization failed with fd=" + fd);
        }
    }

    /**
     * Mark the beginning of one record in the backup data stream. This must be called before
     * {@link #writeEntityData}.
     * @param key A string key that uniquely identifies the data record within the application
     * @param dataSize The size in bytes of this record's data.  Passing a dataSize
     *    of -1 indicates that the record under this key should be deleted.
     * @return The number of bytes written to the backup stream
     * @throws IOException if the write failed
     */
    public int writeEntityHeader(String key, int dataSize) throws IOException {
        int result = writeEntityHeader_native(mBackupWriter, key, dataSize);
        if (result >= 0) {
            return result;
        } else {
            throw new IOException("result=0x" + Integer.toHexString(result));
        }
    }

    /**
     * Write a chunk of data under the current entity to the backup transport.
     * @param data A raw data buffer to send
     * @param size The number of bytes to be sent in this chunk
     * @return the number of bytes written
     * @throws IOException if the write failed
     */
    public int writeEntityData(byte[] data, int size) throws IOException {
        int result = writeEntityData_native(mBackupWriter, data, size);
        if (result >= 0) {
            return result;
        } else {
            throw new IOException("result=0x" + Integer.toHexString(result));
        }
    }

    /** @hide */
    public void setKeyPrefix(String keyPrefix) {
        setKeyPrefix_native(mBackupWriter, keyPrefix);
    }

    /** @hide */
    protected void finalize() throws Throwable {
        try {
            dtor(mBackupWriter);
        } finally {
            super.finalize();
        }
    }

    private native static long ctor(FileDescriptor fd);
    private native static void dtor(long mBackupWriter);

    private native static int writeEntityHeader_native(long mBackupWriter, String key, int dataSize);
    private native static int writeEntityData_native(long mBackupWriter, byte[] data, int size);
    private native static void setKeyPrefix_native(long mBackupWriter, String keyPrefix);
}


text:
class Model
  def initialize(hash={})
    @attributes = hash
  end

  def read_attribute_for_serialization(name)
    if name == :id || name == 'id'
      object_id
    else
      @attributes[name]
    end
  end
end

###
## Models
###
class User < Model
  def profile
    @profile ||= Profile.new(name: 'N1', description: 'D1')
  end
end

class Profile < Model
end

class Post < Model
  def comments
    @comments ||= [Comment.new(content: 'C1'),
                   Comment.new(content: 'C2')]
  end
end

class Comment < Model
end

class Desk < Model
end

###
## Serializers
###
class UserSerializer < ActiveModel::Serializer
  attributes :name, :email

  has_one :profile
end

class ProfileSerializer < ActiveModel::Serializer
  def description
    description = object.read_attribute_for_serialization(:description)
    scope ? "#{description} - #{scope}" : description
  end

  attributes :name, :description
end

class PostSerializer < ActiveModel::Serializer
  attributes :title, :body

  has_many :comments
end

class CommentSerializer < ActiveModel::Serializer
  attributes :content
end

class DeskSerializer < ActiveModel::Serializer
  attributes :drawer, :lamp
  hidden_attributes :secret_document
end


text:
/* $NetBSD: amigatypes.h,v 1.3 1999/02/16 23:34:11 is Exp $ */

/*-
 * Copyright (c) 1996 The NetBSD Foundation, Inc.
 * All rights reserved.
 *
 * This code is derived from software contributed to The NetBSD Foundation
 * by Ignatios Souvatzis.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *        This product includes software developed by the NetBSD
 *        Foundation, Inc. and its contributors.
 * 4. Neither the name of The NetBSD Foundation nor the names of its
 *    contributors may be used to endorse or promote products derived
 *    from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#ifndef _AMIGA_TYPES_H_
#define _AMIGA_TYPES_H_

/* Dummy structs, used only as abstract pointers */

struct Library;
struct TextAttr;
struct Gadget;
struct BitMap;
struct NewScreen;
struct MemNode;

/* real structs */

struct TagItem {u_int32_t item; void * data;};

struct Library {
	u_int8_t Dmy1[20];
	u_int16_t Version, Revision;
	u_int8_t Dmy2[34-24];
};

struct MemHead {
	struct MemHead *next;
	u_int8_t Dmy1[  9-  4];
	u_int8_t Pri;
	u_int8_t Dmy2[ 14- 10];
	u_int16_t Attribs;
	u_int32_t First, Lower, Upper, Free;
};

struct ExecBase {
	struct Library LibNode;
	u_int8_t Dmy1[296-34];
	u_int16_t AttnFlags;	/* 296 */
	u_int8_t Dmy2[300-298];	/* 298 */
	void *ResModules;	/* 300 */
	u_int8_t Dmy3[322-304];	/* 304 */
	struct MemHead *MemLst;	/* 322 */
	/*
	 * XXX: actually, its a longer List base, but we only need to
	 * search it once.
	 */
	u_int8_t Dmy4[568-326];	/* 326 */
	u_int32_t EClockFreq;	/* 330 */
	u_int8_t Dmy5[632-334];
};

#endif /* _AMIGA_TYPES_H */


text:
export const mutations = {

};


text:
"""
simple cube fitting example
"""
from __future__ import print_function

import numpy as np
import pyspeckit
from spectral_cube import SpectralCube
from astropy import wcs
from astropy import units as u

# Create a new WCS object so we can instantiate the SpectralCube
mywcs = wcs.WCS(naxis=3)

# Set up a tangent projection
# You would normally read this from a file!!
mywcs.wcs.crpix = [1,2,21.0]
mywcs.wcs.cdelt = np.array([-0.066667, 0.066667, 500])
mywcs.wcs.crval = [290.9250, 14.5092, 60000]
mywcs.wcs.ctype = ["RA---TAN", "DEC--TAN", 'VELO']
mywcs.wcs.cunit = ['deg', 'deg', 'm/s']

# Create a synthetic X-dimension in km/s
xarr = np.linspace(50, 70, 41) # km/s

# Define a line width, which will vary across our image
# It will increase from 1 km/s to 4 km/s over the X-direction (RA)
sigma = np.outer(np.linspace(1,1.5,2), np.ones(4)).T

# Define a line center, which will vary in the opposite direction,
# along increasing Y-direction (declination)
centroid = np.outer(np.ones(2), np.linspace(58, 62, 4)).T

data = np.exp(-(np.tile(xarr, (2, 4, 1)).T - centroid)**2 / (2.*sigma**2))
cube = SpectralCube(data=data, wcs=mywcs)

# Sanity checks: do the moments accurately recover the inputs?
assert (np.abs(cube.moment1().to(u.km/u.s).value - centroid).max()) < 1e-5
assert (np.abs(cube.moment2().to(u.km**2/u.s**2).value - sigma**2).max()) < 1e-5

# Create a pyspeckit cube
pcube = pyspeckit.Cube(cube=cube)

# For convenience, convert the X-axis to km/s
# (WCSLIB automatically converts to m/s even if you give it km/s)
pcube.xarr.convert_to_unit(u.km/u.s)

# Set up the fitter by doing a preliminary fit
pcube.specfit(fittype='gaussian', guesses='moments')

# Fit each spectrum with a gaussian
# First, assemble the guesses:
guesses = np.array([cube.max(axis=0).value,
                    cube.moment1(axis=0).to(u.km/u.s).value,
                    (cube.moment2(axis=0)**0.5).to(u.km/u.s).value])
# (the second moment is in m^2/s^2, but we want km/s

# Do the fit!
pcube.fiteach(guesses=guesses, # pass in the guess array
              # tell it where to start the fitting (center pixel in this case)
              start_from_point=(5,5),
              # Paralellize the fits?
              multicore=4,
              fittype='gaussian',
             )

# Then you can access the fits via parcube:
assert np.all(pcube.parcube[0,:,:] == 1)
assert np.all(pcube.parcube[1,:,:] == centroid)
assert np.all(pcube.parcube[2,:,:] == sigma)

# Can also fit non-parallelized
pcube.fiteach(guesses=guesses, # pass in the guess array
              # tell it where to start the fitting (center pixel in this case)
              start_from_point=(5,5),
              # Paralellize the fits?
              multicore=1,
              fittype='gaussian',
             )

assert np.all(pcube.parcube[0,:,:] == 1)
assert np.all(pcube.parcube[1,:,:] == centroid)
assert np.all(pcube.parcube[2,:,:] == sigma)


text:
package com.testapps.movieslist.presenters;

import android.os.AsyncTask;

import com.testapps.movieslist.models.Movie;
import com.testapps.movieslist.views.IDetailFragmentView;

import javax.inject.Inject;

public class DetailFragmentPresenterImpl implements IDetailFragmentPresenter {

    private IDetailFragmentView view;

    @Inject
    public DetailFragmentPresenterImpl() {
    }

    @Override
    public void init(IDetailFragmentView view) {
        this.view = view;
    }

    @Override
    public void onResume(long id) {
        sendRequest(id);
    }

    @Override
    public void onPause() {
    }

    private void sendRequest(long id) {
        getSingleMovieById(id);
        view.showProgressDialog();
    }

    private void getSingleMovieById(long id) {
        new getMovieFromDb().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, id);
    }

    // ----- Inner Class for RequestListener

    public class getMovieFromDb extends AsyncTask<Long, Void, Movie> {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
        }

        @Override
        protected Movie doInBackground(Long... params) {
            Long incId = params[0];
            return view.getMovie(incId);
        }

        @Override
        protected void onPostExecute(Movie result) {
            super.onPostExecute(result);
            view.hideProgressDialog();
            view.updateViews(result);
        }
    }
}


text: I18n.enforce_available_locales = false


text:
function N = minus(N1, N2)
%MINUS   Minus for CHEBOP2 objects.
%
% N = MINUS(N1, N2) is the same as N = N1 - N2.

% Copyright 2014 by The University of Oxford and The Chebfun Developers.
% See http://www.chebfun.org/ for Chebfun information.

N = plus(N1, uminus(N2));

end


text:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/1999/REC-html401-19991224/loose.dtd">

    <meta http-equiv="Cache-Control" content="no-cache, no-store, must-revalidate" />
    <meta http-equiv="Pragma" content="no-cache" />
    <meta http-equiv="Expires" content="0" />
<title>Volunteer State Community College Login - powered by SunGard Higher Education</title>

<link rel="stylesheet" href="https://portal.volstate.edu/misc/style.css" type="text/css">
<link rel="stylesheet" href="https://portal.volstate.edu/custom/classic/custom.css" type="text/css">
<link rel="stylesheet" href="/cp/content/style.css" type="text/css">

      ALERT

Secure Access Login

	      <form name="userid" onSubmit="xferFocus(this); return false;">
		Username:

		<input class="textform" id="user" name="user" tabindex="1" onFocus="hadFocus(true)" type="text" size="15"/>
	      </form>

	      <form name="cplogin" action="https://portal.volstate.edu/cp/home/login" onSubmit="login(); return false;" method="post">
		Password:

		<input class="textform" id="pass" name="pass" tabindex="2" onFocus="hadFocus(true)" type="password" size="15"/>
		<input type="hidden" name="user" value=""/>
		<input type="hidden" name="uuid" value=""/>
	      </form>

	      <input class="btn" accesskey="l" id="login_btn" value="  Login  " type="button" onClick="login();" tabindex="3"/>
	      <input class="btn" accesskey="c" id="cancel_btn" value="Cancel" type="button" onClick="doCancel();" tabindex="4"/>

Change or Activate your password

Need Help?

STUDENTS

If you do not have a Vnumber, please contact our Records Office at (615) 230-3466.

FACULTY/STAFF

If you do not have a Vnumber, please check your ID card or contact Human Resources.

STUDENTS, FACULTY & STAFF

If you have your Vnumber, but do not have your username, please contact the IT Help Desk at (615) 230-3302

Login Help

Having problems logging in? Click here.

Welcome to My Volstate 2.5

This secure site provides students, faculty, and administrative staff with world-class Intranet and Internet services. This is where you can check email, register for courses, and access your online classes.

What's Inside?

My Vol State provides access to several tools you already use. Once you sign in to My Vol State you will automatically be signed in to these tools as well.

eLearn

Student Email

RODP Classes


text:
var mongoose = require('mongoose');
var UserExtAccount = mongoose.model('UserExtAccount');

var link = function( req, profile, provider, accessToken, refreshToken, done ) {
  var options = {
    critedia: { 'uid': req.session.appuid }
    , select: 'uid ' + provider
  };
  UserExtAccount.load(options, function(err, data) {
    if (err) { return done(err); }
    if (data)
    {
      {
        data[provider] = {};
        data[provider].id = profile.id;
        if ( accessToken ) { data[provider].accessToken = accessToken; }
        if ( refreshToken ) { data[provider].refreshToken = refreshToken; }
        UserExtAccount.linkExtAccount( req.session.appuid, provider, data[provider], function(err,numberAffected) {
          if (err) { return done(err); }
          return done(null, data);
        });
      }
    }
    else
    {
      data = {};
      {
        data[provider] = {};
        data[provider].id = profile.id;
        if ( accessToken ) { data[provider].accessToken = accessToken; }
        if ( refreshToken ) { data[provider].refreshToken = refreshToken; }

        var doc = {}
        doc.uid = req.session.appuid;
        doc[provider] = data[provider];
        data = new UserExtAccount(doc);
        data.save( function(err) {
          if (err) { return done(err); }
          return done(null, data);
        });
      }
    }
  });
};

var recover = function( req, profile, provider, accessToken, refreshToken, done ) {
  var options = {
    criteria: {}
    , select: 'uid ' + provider
  };
  options.criteria[provider+'.id'] = profile.id;

  UserExtAccount.load(options, function(err, data) {
    if (err) { return done(err); }
    if (data)
    {
      if ( data[provider] && data[provider].id )
      {
        req.session.appuid = profile.uid;
        data[provider] = {};
        data[provider].id = profile.id;
        if ( accessToken ) { data[provider].accessToken = accessToken; }
        if ( refreshToken ) { data[provider].refreshToken = refreshToken; }
        UserExtAccount.linkExtAccount( req.session.appuid, provider, data[provider], function(err,numberAffected) {
          if (err) { return done(err); }
          return done(null, data);
        });
      }
      else
      {
        return done(err, data);
      }
    }
    else
    {
      console.log('passportCallback.recover: doesn\'t found account. ' + provider + ' ' + profile.id);
      return done(null);
    }
  });
};

var passportCallback = function( req, profile, provider, accessToken, refreshToken, done ) {
  var mode = req.session.ext_account_mode;
  if ( 'link' === mode )
  {
    return link(req, profile, provider, accessToken, refreshToken, done);
  }
  else
  if ( 'recovery' === mode )
  {
    return recover(req, profile, provider, accessToken, refreshToken, done);
  }
  else
  {
    done(new Error('unknown mode: ' + mode));
  }
};

exports.invoke = passportCallback;


text:
namespace crash_reporter {

// Returns whether the user has consented to collecting stats.
bool GetClientCollectStatsConsent();

#if defined(OS_POSIX) && !defined(OS_MACOSX)
// Returns a textual description of the product type, version and channel
// to include in crash reports.
// TODO(https://crbug.com/986178): Implement this for other platforms.
void GetClientProductNameAndVersion(std::string* product,
                                    std::string* version,
                                    std::string* channel);
#endif

}  // namespace crash_reporter

#endif  // COMPONENTS_CRASH_CORE_APP_CLIENT_UPLOAD_INFO_H_


text:
import test from 'tape';
import { END } from '../../src'
import proc from '../../src/internal/proc'
import { deferred, arrayOfDeffered } from '../../src/utils'
import * as io from '../../src/effects'

test('processor array of effects handling', assert => {
  assert.plan(1);

  let actual;
  const def = deferred()

  let cpsCb = {}
  const cps = (val, cb) => cpsCb = {val, cb}

  const input = cb => {
    Promise.resolve(1)
      .then(() => def.resolve(1))
      .then(() => cpsCb.cb(null, cpsCb.val))
      .then(() => cb({type: 'action'}))
    return () => {}
  }

  function* genFn() {
    actual = yield io.all([
      def.promise,
      io.cps(cps, 2),
      io.take('action')
    ])
  }

  proc(genFn(), input).done.catch(err => assert.fail(err))

  const expected = [1,2, {type: 'action'}];

  setTimeout(() => {
    assert.deepEqual(actual, expected,
      "processor must fullfill parallel effects"
    );
    assert.end();
  })

});

test('processor empty array', assert => {
  assert.plan(1);

  let actual;

  const input = () => {
    return () => {}
  }

  function* genFn() {
    actual = yield io.all([])
  }

  proc(genFn(), input).done.catch(err => assert.fail(err))

  const expected = [];

  setTimeout(() => {
    assert.deepEqual(actual, expected,
      "processor must fullfill empty parallel effects with an empty array"
    );
    assert.end();
  })

});

test('processor array of effect: handling errors', assert => {
  assert.plan(1);

  let actual;
  const defs = arrayOfDeffered(2)

  Promise.resolve(1)
    .then(() => defs[0].reject('error'))
    .then(() => defs[1].resolve(1))

  function* genFn() {
    try {
      actual = yield io.all([
        defs[0].promise,
        defs[1].promise
      ])
    } catch(err) {
      actual = [err]
    }
  }

  proc(genFn()).done.catch(err => assert.fail(err))

  const expected = ['error'];

  setTimeout(() => {
    assert.deepEqual(actual, expected,
      "processor must catch the first error in parallel effects"
    );
    assert.end();
  })

});

test('processor array of effect: handling END', assert => {
  assert.plan(1);

  let actual;
  const def = deferred()
  const input = (cb) => {
    Promise.resolve(1)
      .then(() => def.resolve(1))
      .then(() => cb(END))

    return () => {}
  }

  function* genFn() {
    try {
      actual = yield io.all([
        def.promise,
        io.take('action')
      ])
    } finally {
      actual = 'end'
    }

  }

  proc(genFn(), input).done.catch(err => assert.fail(err))

  setTimeout(() => {
    assert.deepEqual(actual, 'end',
      "processor must end Parallel Effect if one of the effects resolve with END"
    );
    assert.end();
  })

});

test('processor array of effect: named effects', assert => {
  assert.plan(1);

  let actual;
  const def = deferred()
  const input = (cb) => {
    Promise.resolve(1)
      .then(() => def.resolve(1))
      .then(() => cb({ type: 'action' }))

    return () => {}
  }

  function* genFn() {
    actual = yield io.all({
      ac: io.take('action'),
      prom: def.promise
    })
  }

  proc(genFn(), input).done.catch(err => assert.fail(err))

  setTimeout(() => {
    const expected = { ac: { type: 'action' }, prom: 1 }
    assert.deepEqual(actual, expected,
      "processor must handle parallel named effects"
    );
    assert.end();
  })

});