Example #1
0
def windowed_smooth(measurements, size=1, population_size=512):
    """
    Returns a new list of measurements with the same length as the source
    measurements, where each value in the result is calculated as the median
    and weighted MAD of the nearest +/- size measurements and the measurement
    itself.

    For example, if the resulting value for measurement X10 with a size of 2
    would be median +/- the weighted MAD of the measurements
    (X8, X9, X10, X11, X12)

    Edge conditions are handled so that they are smoothed with partial windows

    :param measurements:
    :param size:
        The extend of the smoothing window.
    :param population_size:
    :return:
    """

    window = []
    window_populations = []

    while len(window) < size + 1:
        m = measurements[len(window)]

        window.append(m)
        window_populations.append(
            mdists.population(mdists.Distribution([m]), count=population_size))

    out = []

    for i in range(len(measurements)):
        pop_combined = []
        for p in window_populations:
            pop_combined += p

        out.append(
            ValueUncertainty(
                mdists.percentile(pop_combined),
                mdists.weighted_median_average_deviation(pop_combined)))

        append_index = i + size + 1

        if append_index >= len(measurements):
            window.pop(0)
            window_populations.pop(0)
            continue

        m = measurements[append_index]
        d = mdists.Distribution([m])
        pop = mdists.population(d, population_size)
        window.append(m)
        window_populations.append(pop)

        while len(window) > (2 * size + 1):
            window.pop(0)
            window_populations.pop(0)

    return out
Example #2
0
def from_serialized(serialized):
    """

    :param serialized:
    :return:
    """

    return [
        ValueUncertainty(x.get('raw', x['value']),
                         x.get('raw_uncertainty', x['uncertainty']))
        for x in serialized
    ]
def overlap2(distribution, comparison):
    min_value = values.minimum(distribution.measurements +
                               comparison.measurements)
    max_value = values.maximum(distribution.measurements +
                               comparison.measurements)

    def value_at(x):
        return abs(
            distribution.probability_at(x) - comparison.probability_at(x))

    result = integrate.quad(value_at,
                            min_value.value - 10.0 * min_value.uncertainty,
                            max_value.value + 10.0 * max_value.uncertainty,
                            limit=100)
    return ValueUncertainty(value=1.0 - 0.5 * result[0], uncertainty=result[1])
Example #4
0
def join(values, uncertainties):
    """

    :param values:
    :param uncertainties:
    :return:
    """

    out = []
    for i in range(len(values)):
        if isinstance(uncertainties, (list, tuple)):
            unc = uncertainties[i]
        else:
            unc = uncertainties
        out.append(ValueUncertainty(values[i], unc))

    return out
Example #5
0
def box_smooth(measurements, size=2, population_size=512):
    """

    :param measurements:
    :param size:
    :param population_size:
    :return:
    """

    out = []

    for i in range(0, len(measurements), size):
        d = mdists.Distribution(measurements[i:(i + size)])
        pop = mdists.population(d, count=population_size)
        median = mdists.percentile(pop)
        mad = mdists.weighted_median_average_deviation(pop)
        while len(out) < (i + size):
            out.append(ValueUncertainty(median, mad))

    return out