def chain_break_frequency(samples_like, embedding):
    """Determine the frequency of chain breaks in the given samples.

    Args:
        samples_like (samples_like/:obj:`dimod.SampleSet`):
            A collection of raw samples. 'samples_like' is an extension of NumPy's array_like.
            See :func:`dimod.as_samples`.

        embedding (dict):
            Mapping from source graph to target graph as a dict of form {s: {t, ...}, ...},
            where s is a source-model variable and t is a target-model variable.

    Returns:
        dict: Frequency of chain breaks as a dict in the form {s: f, ...},  where s
        is a variable in the source graph, and frequency, a float, is the fraction
        of broken chains.

    Examples:
        This example embeds a single source node, 'a', as a chain of two target nodes (0, 1)
        and uses :func:`.chain_break_frequency` to show that out of two synthetic samples,
        one ([-1, +1]) represents a broken chain.

        >>> import dimod
        >>> import numpy as np
        >>> samples = np.array([[-1, +1], [+1, +1]])
        >>> embedding = {'a': {0, 1}}
        >>> print(dimod.chain_break_frequency(samples, embedding)['a'])
        0.5


        This example embeds a single source node (0) as a chain of two target nodes (a, b)
        and uses :func:`.chain_break_frequency` to show that out of two samples in a
        dimod response, one ({'a': 1, 'b': 0}) represents a broken chain.

        >>> import dimod
        ...
        >>> response = dimod.SampleSet.from_samples([{'a': 1, 'b': 0}, {'a': 0, 'b': 0}],
        ...                                        {'energy': [1, 0]}, {}, dimod.BINARY)
        >>> embedding = {0: {'a', 'b'}}
        >>> print(dimod.chain_break_frequency(response, embedding)[0])
        0.5

    """
    if isinstance(samples_like, dimod.SampleSet):
        labels = samples_like.variables
        samples = samples_like.record.sample
        num_occurrences = samples_like.record.num_occurrences
    else:
        samples, labels = dimod.as_samples(samples_like)
        num_occurrences = np.ones(samples.shape[0])

    if not all(v == idx for idx, v in enumerate(labels)):
        labels_to_idx = {v: idx for idx, v in enumerate(labels)}
        embedding = {v: {labels_to_idx[u] for u in chain} for v, chain in embedding.items()}

    if not embedding:
        return {}

    variables, chains = zip(*embedding.items())

    broken = broken_chains(samples, chains)

    return {v: float(np.average(broken[:, cidx], weights=num_occurrences))
            for cidx, v in enumerate(variables)}
def chain_break_frequency ( samples_like , embedding ) : if isinstance ( samples_like , dimod . SampleSet ) : labels = samples_like . variables samples = samples_like . record . sample num_occurrences = samples_like . record . num_occurrences else : samples , labels = dimod . as_samples ( samples_like ) num_occurrences = np . ones ( samples . shape [ 0 ] ) if not all ( v == idx for idx , v in enumerate ( labels ) ) : labels_to_idx = { v : idx for idx , v in enumerate ( labels ) } embedding = { v : { labels_to_idx [ u ] for u in chain } for v , chain in embedding . items ( ) } if not embedding : return { } variables , chains = zip ( * embedding . items ( ) ) broken = broken_chains ( samples , chains ) return { v : float ( np . average ( broken [ : , cidx ] , weights = num_occurrences ) ) for cidx , v in enumerate ( variables ) }
Determine the frequency of chain breaks in the given samples.

    Args:
        samples_like (samples_like/:obj:`dimod.SampleSet`):
            A collection of raw samples. 'samples_like' is an extension of NumPy's array_like.
            See :func:`dimod.as_samples`.

        embedding (dict):
            Mapping from source graph to target graph as a dict of form {s: {t, ...}, ...},
            where s is a source-model variable and t is a target-model variable.

    Returns:
        dict: Frequency of chain breaks as a dict in the form {s: f, ...},  where s
        is a variable in the source graph, and frequency, a float, is the fraction
        of broken chains.

    Examples:
        This example embeds a single source node, 'a', as a chain of two target nodes (0, 1)
        and uses :func:`.chain_break_frequency` to show that out of two synthetic samples,
        one ([-1, +1]) represents a broken chain.

        >>> import dimod
        >>> import numpy as np
        >>> samples = np.array([[-1, +1], [+1, +1]])
        >>> embedding = {'a': {0, 1}}
        >>> print(dimod.chain_break_frequency(samples, embedding)['a'])
        0.5


        This example embeds a single source node (0) as a chain of two target nodes (a, b)
        and uses :func:`.chain_break_frequency` to show that out of two samples in a
        dimod response, one ({'a': 1, 'b': 0}) represents a broken chain.

        >>> import dimod
        ...
        >>> response = dimod.SampleSet.from_samples([{'a': 1, 'b': 0}, {'a': 0, 'b': 0}],
        ...                                        {'energy': [1, 0]}, {}, dimod.BINARY)
        >>> embedding = {0: {'a', 'b'}}
        >>> print(dimod.chain_break_frequency(response, embedding)[0])
        0.5