示例#1
0
def plot_sizes_by_track(tracks_df, pes=True):
    prefix = 'pes' if pes else 'manus'
    limb_ids = ['lp', 'rp'] if pes else ['lm', 'rm']

    length_measurements = cd.shared.to_measurements(tracks_df, limb_ids, 'l')
    length_dist = mstats.create_distribution(
        [x for x in length_measurements if x.value > 0])
    pop = mstats.distributions.population(length_dist)
    length_median = mstats.ValueUncertainty(
        mstats.distributions.percentile(pop),
        mstats.distributions.weighted_median_average_deviation(pop))

    width_measurements = cd.shared.to_measurements(tracks_df, limb_ids, 'w')
    width_dist = mstats.create_distribution(
        [x for x in width_measurements if x.value > 0])
    pop = mstats.distributions.population(width_dist)
    width_median = mstats.ValueUncertainty(
        mstats.distributions.percentile(pop),
        mstats.distributions.weighted_median_average_deviation(pop))

    label = prefix.capitalize()

    cd.display.plotly(
        [
            cd.shared.create_density_trace(length_dist, 0, 'Length'),
            cd.shared.create_density_trace(width_dist, 1, 'Width'),
        ],
        plotting.create_layout(
            {},
            title='{} Width & Length Distributions (by track)'.format(label),
            x_label='Size (m)',
            y_label='Probability Density'))

    print('Median {} Width: {}m'.format(label, width_median.label))
    print('Median {} Length: {}m'.format(label, length_median.label))
示例#2
0
    def test_no_overlap(self):
        """
        """

        dist = mstats.create_distribution([-2000], [1.0])
        comp = mstats.create_distribution([2000], [1.0])

        result = mstats.distributions.overlap2(dist, comp)
        self.assertAlmostEqual(result.value, 0)
示例#3
0
    def test_perfect_overlap(self):
        """ """

        values = [1, 1]
        uncertainties = [0.5, 0.5]
        measurements = mstats.values.join(values, uncertainties)

        dist = mstats.create_distribution(measurements)
        comp = mstats.create_distribution(
            [mstats.ValueUncertainty(values[0], uncertainties[0])])

        result = mstats.distributions.overlap2(dist, comp)
        self.assertEqual(result.value, 1.0)
示例#4
0
    def test_low_overlap(self):
        """
        """

        values = [-10, 2, 20]
        uncertainties = [0.5, 0.5, 0.5]
        measurements = mstats.values.join(values, uncertainties)

        dist = mstats.create_distribution(measurements)
        comp = mstats.create_distribution(
            [mstats.mean.weighted_mean_and_deviation(measurements)])

        result = mstats.distributions.overlap2(dist, comp)
        self.assertGreater(result.value, 0.3)
    def test_perfect_overlap(self):
        """ """

        values = [1, 1]
        uncertainties = [0.5, 0.5]
        measurements = mstats.values.join(values, uncertainties)

        dist = mstats.create_distribution(measurements)
        comp = mstats.create_distribution(
            [mstats.ValueUncertainty(values[0], uncertainties[0])]
        )

        result = mstats.distributions.overlap2(dist, comp)
        self.assertEqual(result.value, 1.0)
    def test_low_overlap(self):
        """
        """

        values = [-10, 2, 20]
        uncertainties = [0.5, 0.5, 0.5]
        measurements = mstats.values.join(values, uncertainties)

        dist = mstats.create_distribution(measurements)
        comp = mstats.create_distribution(
            [mstats.mean.weighted_mean_and_deviation(measurements)]
        )

        result = mstats.distributions.overlap2(dist, comp)
        self.assertGreater(result.value, 0.3)
def plot_pes_aspects(trackway_df):
    measurements = mstats.values.join(
        trackway_df['pes_aspect'].tolist(),
        trackway_df['pes_daspect'].tolist()
    )

    dist = mstats.create_distribution(
        [x for x in measurements if x.value > 0]
    )

    pop = mstats.distributions.population(dist)
    median = mstats.ValueUncertainty(
        mstats.distributions.percentile(pop),
        mstats.distributions.weighted_median_average_deviation(pop)
    )

    cd.display.plotly(
        cd.shared.create_density_trace(dist),
        plotting.create_layout(
            {},
            title='Pes Aspect Ratio Distribution (by trackway)',
            x_label='Aspect Ratio (width / length)',
            y_label='Probability Density'
        )
    )

    print('Median Pes Aspect Ratio:', median.label)
示例#8
0
def get_weighted_median(quantities) -> mstats.ValueUncertainty:
    dist = mstats.create_distribution(quantities)
    pop = mdist.population(dist)
    return mstats.ValueUncertainty(
        mdist.percentile(pop, 0.5),
        max(0.00001, mdist.weighted_median_average_deviation(pop))
    )
def plot_pes_aspects_by_tracks(tracks_df):
    widths = cd.shared.to_measurements(tracks_df, ['lp', 'rp'], 'w')
    lengths = cd.shared.to_measurements(tracks_df, ['lp', 'rp'], 'l')
    measurements = [l / w for l, w in zip(lengths, widths)]

    dist = mstats.create_distribution(
        [x for x in measurements if x.value > 0]
    )

    pop = mstats.distributions.population(dist)
    median = mstats.ValueUncertainty(
        mstats.distributions.percentile(pop),
        mstats.distributions.weighted_median_average_deviation(pop)
    )

    cd.display.plotly(
        cd.shared.create_density_trace(dist),
        plotting.create_layout(
            {},
            title='Pes Aspect Ratio Distribution (by track)',
            x_label='Aspect Ratio (width / length)',
            y_label='Probability Density'
        )
    )

    print('Median Pes Aspect Ratio:', median.label)
def compute_normality_weighted(
        key: str,
        trackway_df: pd.DataFrame
) -> mstats.ValueUncertainty:
    dist = to_distribution(key, trackway_df)
    mean = mstats.mean.weighted(*dist.measurements)
    comparison = mstats.create_distribution([mean])
    return 1.0 - mstats.distributions.overlap2(dist, comparison)
def plot_sizes(trackway_df, pes=True):
    prefix = 'pes' if pes else 'manus'
    length_measurements = mstats.values.join(
        trackway_df['{}_l'.format(prefix)].tolist(),
        trackway_df['{}_dl'.format(prefix)].tolist()
    )
    length_dist = mstats.create_distribution(
        [x for x in length_measurements if x.value > 0]
    )
    pop = mstats.distributions.population(length_dist)
    length_median = mstats.ValueUncertainty(
        mstats.distributions.percentile(pop),
        mstats.distributions.weighted_median_average_deviation(pop)
    )

    width_measurements = mstats.values.join(
        trackway_df['{}_w'.format(prefix)].tolist(),
        trackway_df['{}_dw'.format(prefix)].tolist()
    )
    width_dist = mstats.create_distribution(
        [x for x in width_measurements if x.value > 0]
    )
    pop = mstats.distributions.population(width_dist)
    width_median = mstats.ValueUncertainty(
        mstats.distributions.percentile(pop),
        mstats.distributions.weighted_median_average_deviation(pop)
    )

    label = prefix.capitalize()

    cd.display.plotly(
        [
            cd.shared.create_density_trace(length_dist, 0, 'Length'),
            cd.shared.create_density_trace(width_dist, 1, 'Width'),
        ],
        plotting.create_layout(
            {},
            title='{} Width & Length Distributions (by trackway)'.format(label),
            x_label='Size (m)',
            y_label='Probability Density'
        )
    )

    print('Median {} Width: {}m'.format(label, width_median.label))
    print('Median {} Length: {}m'.format(label, length_median.label))
示例#12
0
def get_median(
        quantities: typing.List[mstats.ValueUncertainty]
) -> mstats.ValueUncertainty:
    dist = mstats.create_distribution(quantities)
    pop = mdist.population(dist)
    return mstats.ValueUncertainty(
        mdist.percentile(pop, 0.5),
        max(0.00001, mdist.weighted_median_average_deviation(pop))
    )
示例#13
0
def calculate_median(values, uncertainties, color_index=0, plot_name=None):
    """

    :param values:
    :param uncertainties:
    :param color_index:
    :param plot_name:
    :return:
    """

    try:
        values = values.values
    except:
        pass

    try:
        uncertainties = uncertainties.values
    except:
        pass

    measurements = [
        (val, unc) for val, unc in zip(values, uncertainties)
        if not np.isnan(val) and not np.isnan(unc)
    ]

    values, uncertainties = zip(*measurements)
    measurements = mstats.values.join(values, uncertainties)

    dist = mstats.create_distribution(measurements)
    pop = mdist.population(dist)

    median = mstats.ValueUncertainty(
        mdist.percentile(pop),
        mdist.weighted_median_average_deviation(pop)
    )

    x = mdist.adaptive_range(dist, 4)
    y = dist.probabilities_at(x)

    trace = go.Scatter(
        x=x,
        y=y,
        name=plot_name,
        fill='tozeroy',
        mode='lines',
        line=dict(
            color=plotting.get_color(color_index)
        )
    )

    return dict(
        measurements=measurements,
        median=median,
        trace=trace
    )
def plot_sizes_by_track(tracks_df, pes=True):
    prefix = 'pes' if pes else 'manus'
    limb_ids = ['lp', 'rp'] if pes else ['lm', 'rm']

    length_measurements = cd.shared.to_measurements(tracks_df, limb_ids, 'l')
    length_dist = mstats.create_distribution(
        [x for x in length_measurements if x.value > 0]
    )
    pop = mstats.distributions.population(length_dist)
    length_median = mstats.ValueUncertainty(
        mstats.distributions.percentile(pop),
        mstats.distributions.weighted_median_average_deviation(pop)
    )

    width_measurements = cd.shared.to_measurements(tracks_df, limb_ids, 'w')
    width_dist = mstats.create_distribution(
        [x for x in width_measurements if x.value > 0]
    )
    pop = mstats.distributions.population(width_dist)
    width_median = mstats.ValueUncertainty(
        mstats.distributions.percentile(pop),
        mstats.distributions.weighted_median_average_deviation(pop)
    )

    label = prefix.capitalize()

    cd.display.plotly(
        [
            cd.shared.create_density_trace(length_dist, 0, 'Length'),
            cd.shared.create_density_trace(width_dist, 1, 'Width'),
        ],
        plotting.create_layout(
            {},
            title='{} Width & Length Distributions (by track)'.format(label),
            x_label='Size (m)',
            y_label='Probability Density'
        )
    )

    print('Median {} Width: {}m'.format(label, width_median.label))
    print('Median {} Length: {}m'.format(label, length_median.label))
def calculate_median_value(values):
    if not values or not len(values):
        return dict(values=[],
                    dist=None,
                    pop=None,
                    median=mstats.ValueUncertainty(0, 0.0001))

    dist = mstats.create_distribution(values)
    pop = mdist.population(dist)
    median = mstats.ValueUncertainty(
        mdist.percentile(pop), mdist.weighted_median_average_deviation(pop))
    return dict(values=values, dist=dist, pop=pop, median=median)
示例#16
0
def plot_sizes(trackway_df, pes=True):
    prefix = 'pes' if pes else 'manus'
    length_measurements = mstats.values.join(
        trackway_df['{}_l'.format(prefix)].tolist(),
        trackway_df['{}_dl'.format(prefix)].tolist())
    length_dist = mstats.create_distribution(
        [x for x in length_measurements if x.value > 0])
    pop = mstats.distributions.population(length_dist)
    length_median = mstats.ValueUncertainty(
        mstats.distributions.percentile(pop),
        mstats.distributions.weighted_median_average_deviation(pop))

    width_measurements = mstats.values.join(
        trackway_df['{}_w'.format(prefix)].tolist(),
        trackway_df['{}_dw'.format(prefix)].tolist())
    width_dist = mstats.create_distribution(
        [x for x in width_measurements if x.value > 0])
    pop = mstats.distributions.population(width_dist)
    width_median = mstats.ValueUncertainty(
        mstats.distributions.percentile(pop),
        mstats.distributions.weighted_median_average_deviation(pop))

    label = prefix.capitalize()

    cd.display.plotly(
        [
            cd.shared.create_density_trace(length_dist, 0, 'Length'),
            cd.shared.create_density_trace(width_dist, 1, 'Width'),
        ],
        plotting.create_layout(
            {},
            title='{} Width & Length Distributions (by trackway)'.format(
                label),
            x_label='Size (m)',
            y_label='Probability Density'))

    print('Median {} Width: {}m'.format(label, width_median.label))
    print('Median {} Length: {}m'.format(label, length_median.label))
示例#17
0
def add_to_group(
        segment: Segment,
        groups: SegmentGroupList,
        trackway_data: dict
) -> SegmentGroupList:
    """

    :param segment:
    :param groups:
    :param trackway_data:
    :return:
    """

    deviations = [deviation_between(g, segment) for g in groups]

    if not deviations or min(deviations) > 2:
        return groups + [SegmentGroup(
            index=len(groups),
            segments=[segment],
            median=segment.median,
            normalized_median=segment.median / trackway_data['width_median']
        )]

    group_index = deviations.index(min(deviations))
    group = groups[group_index]

    cauldron.step.breathe()

    dist = mstats.create_distribution([s.median for s in group.segments])
    pop = mdist.population(dist)
    median = mstats.ValueUncertainty(
        mdist.percentile(pop, 0.5),
        max(0.00001, mdist.weighted_median_average_deviation(pop))
    )

    normalized = median / trackway_data['width_median']

    replacement_group = SegmentGroup(
        index=group.index,
        median=median,
        segments=group.segments + [segment],
        normalized_median=normalized
    )

    return (
        groups[:group_index] +
        [replacement_group] +
        groups[(group_index + 1):]
    )
示例#18
0
def calculate_widths(name, df, progress):
    cd.display.status('Calculating Width Values', progress=progress)
    values = mstats.values.join(df['w'].tolist(), df['dw'].tolist())
    dist = mstats.create_distribution(values)

    mean_weighted = mstats.mean.weighted(values)

    return dict(trackway=name,
                mean=mstats.mean.unweighted(values).raw,
                mean_weighted=mean_weighted.value,
                mean_weighted_unc=mean_weighted.uncertainty,
                median=np.median(df['w']),
                median_weighted=mstats.distributions.percentile(dist),
                median_weighted_unc=mstats.distributions.
                weighted_median_average_deviation(dist))
示例#19
0
def calculate_widths(name, df, progress):
    cd.display.status('Calculating Width Values', progress=progress)
    values = mstats.values.join(df['w'].tolist(), df['dw'].tolist())
    dist = mstats.create_distribution(values)

    mean_weighted = mstats.mean.weighted(values)

    return dict(
        trackway=name,
        mean=mstats.mean.unweighted(values).raw,
        mean_weighted=mean_weighted.value,
        mean_weighted_unc=mean_weighted.uncertainty,
        median=np.median(df['w']),
        median_weighted=mstats.distributions.percentile(dist),
        median_weighted_unc=mstats.distributions.weighted_median_average_deviation(dist)
    )
def to_distribution(key: str,
                    trackway_df: pd.DataFrame) -> mstats.Distribution:
    """
    Converts a trackway data frame to a measurement distribution for the
    specified key

    :param key:
        Lookup key within the trackway data frame. Assumes that key exists
        as a column in the data frame and that the uncertainties for those
        values are stored in a column named d{key}.
    :param trackway_df:
    :return:
        A measurement distribution for the selected measurements
    """

    return mstats.create_distribution(trackway_df[key].tolist(),
                                      trackway_df['d{}'.format(key)].tolist())
def to_distribution(key: str, trackway_df: pd.DataFrame) -> mstats.Distribution:
    """
    Converts a trackway data frame to a measurement distribution for the
    specified key

    :param key:
        Lookup key within the trackway data frame. Assumes that key exists
        as a column in the data frame and that the uncertainties for those
        values are stored in a column named d{key}.
    :param trackway_df:
    :return:
        A measurement distribution for the selected measurements
    """

    return mstats.create_distribution(
        trackway_df[key].tolist(),
        trackway_df['d{}'.format(key)].tolist()
    )
示例#22
0
def calculate_median(values, uncertainties, color_index=0, plot_name=None):
    """

    :param values:
    :param uncertainties:
    :param color_index:
    :param plot_name:
    :return:
    """

    try:
        values = values.values
    except:
        pass

    try:
        uncertainties = uncertainties.values
    except:
        pass

    measurements = [(val, unc) for val, unc in zip(values, uncertainties)
                    if not np.isnan(val) and not np.isnan(unc)]

    values, uncertainties = zip(*measurements)
    measurements = mstats.values.join(values, uncertainties)

    dist = mstats.create_distribution(measurements)
    pop = mdist.population(dist)

    median = mstats.ValueUncertainty(
        mdist.percentile(pop), mdist.weighted_median_average_deviation(pop))

    x = mdist.adaptive_range(dist, 4)
    y = dist.probabilities_at(x)

    trace = go.Scatter(x=x,
                       y=y,
                       name=plot_name,
                       fill='tozeroy',
                       mode='lines',
                       line=dict(color=plotting.get_color(color_index)))

    return dict(measurements=measurements, median=median, trace=trace)
示例#23
0
def plot_pes_aspects(trackway_df):
    measurements = mstats.values.join(trackway_df['pes_aspect'].tolist(),
                                      trackway_df['pes_daspect'].tolist())

    dist = mstats.create_distribution([x for x in measurements if x.value > 0])

    pop = mstats.distributions.population(dist)
    median = mstats.ValueUncertainty(
        mstats.distributions.percentile(pop),
        mstats.distributions.weighted_median_average_deviation(pop))

    cd.display.plotly(
        cd.shared.create_density_trace(dist),
        plotting.create_layout(
            {},
            title='Pes Aspect Ratio Distribution (by trackway)',
            x_label='Aspect Ratio (width / length)',
            y_label='Probability Density'))

    print('Median Pes Aspect Ratio:', median.label)
示例#24
0
def plot_pes_aspects_by_tracks(tracks_df):
    widths = cd.shared.to_measurements(tracks_df, ['lp', 'rp'], 'w')
    lengths = cd.shared.to_measurements(tracks_df, ['lp', 'rp'], 'l')
    measurements = [l / w for l, w in zip(lengths, widths)]

    dist = mstats.create_distribution([x for x in measurements if x.value > 0])

    pop = mstats.distributions.population(dist)
    median = mstats.ValueUncertainty(
        mstats.distributions.percentile(pop),
        mstats.distributions.weighted_median_average_deviation(pop))

    cd.display.plotly(
        cd.shared.create_density_trace(dist),
        plotting.create_layout(
            {},
            title='Pes Aspect Ratio Distribution (by track)',
            x_label='Aspect Ratio (width / length)',
            y_label='Probability Density'))

    print('Median Pes Aspect Ratio:', median.label)
def calculate_median_value(values):
    if not values or not len(values):
        return dict(
            values=[],
            dist=None,
            pop=None,
            median=mstats.ValueUncertainty(0, 0.0001)
        )

    dist = mstats.create_distribution(values)
    pop = mdist.population(dist)
    median = mstats.ValueUncertainty(
        mdist.percentile(pop),
        mdist.weighted_median_average_deviation(pop)
    )
    return dict(
        values=values,
        dist=dist,
        pop=pop,
        median=median
    )
def statistics(coupling_positions: typing.Dict[str, list]) -> dict:
    """

    :param coupling_positions:
    :return:
    """

    lengths = coupling_positions['lengths']
    lengths = [e.value for e in lengths]

    d = mstats.create_distribution(lengths)
    bounds = boxes.weighted_two(d)
    median = bounds[2]
    mad = mstats.distributions.weighted_median_average_deviation(d)

    min_value = d.minimum_boundary(3)
    max_value = d.maximum_boundary(3)

    x_values = mstats.ops.linear_space(min_value, max_value, 250)

    rmsd = calculate_rmsd(median, lengths)
    swing = calculate_swing(median, lengths)
    fitness = math.sqrt(rmsd ** 2 + swing ** 2)

    return dict(
        value=mstats.value.ValueUncertainty(median, mad),
        rmsd=rmsd,
        swing=swing,
        fitness=fitness,
        bounds=bounds,
        distribution_profile={
            'x': x_values,
            'y': d.probabilities_at(x_values)
        },
        population=mstats.distributions.population(d, 256),
    )
示例#27
0
import plotly.graph_objs as go
import numpy as np

from cauldron import plotting
from cauldron import project

import measurement_stats as mstats

df = project.shared.measurements

normalized_values = []
traces = []

for key in ['Width-Box', 'Height-Box']:
    d = mstats.create_distribution(measurements=df[key].tolist(),
                                   uncertainties=5)
    median = mstats.distributions.percentile(d)

    normalized = [100.0 * (v / median - 1.0) for v in d.measurements]
    normalized_values += normalized

    x_values = list(np.arange(1, len(normalized), 1))
    y_values, y_uncertainties = mstats.values.unzip(normalized)
    traces.append(
        go.Bar(x=x_values,
               y=y_values,
               error_y=dict(type='data', array=y_uncertainties, visible=True),
               name=key.split('-')[0],
               marker=dict(color=plotting.get_color(len(traces), 0.5))))

d = mstats.create_distribution(normalized_values)
from _Gauge_Quantization import plotting

tracks = cd.shared.tracks  # type: pd.DataFrame
trackway_name = cd.shared.TRACKWAY_NAME
unweighted = cd.shared.unweighted
pes_tracks = tracks[tracks['pes'] == 1]

layout = go.Layout(
    title='{} Pes Gauge Values Along Trackway'.format(trackway_name),
    xaxis={'title': 'Trackway Position (m)'},
    yaxis={'title': 'Gauge (m)'}
)

dist = mstats.create_distribution(
    pes_tracks['simpleGauge'].tolist(),
    pes_tracks['simpleGaugeUnc'].tolist()
)
pop = mstats.distributions.population(dist)
median_value = mstats.distributions.percentile(pop)
mad = mstats.distributions.weighted_median_average_deviation(pop)
median = mstats.ValueUncertainty(median_value, mad)

scatter_trace = plotting.create_scatter(
    pes_tracks['curvePosition'],
    pes_tracks['simpleGauge'],
    pes_tracks['simpleGaugeUnc'],
    name='Gauges'
)

median_traces = plotting.make_ranged_quantity_traces(
    x_start=pes_tracks['curvePosition'].min(),
variations = df['raw_mad'] / df['raw_median']

cd.display.plotly(
    data=go.Histogram(
        x=variations
    ),
    layout=plotting.create_layout(
        title='Coupling Length Fractional Deviations',
        x_label='Fractional Deviation',
        y_label='Frequency (#)'
    )
)

dist = mstats.create_distribution(
    measurements=variations.tolist(),
    uncertainties=0.01
)

x = mstats.distributions.uniform_range(dist, 3)
y = dist.probabilities_at(x)


cd.display.plotly(
    data=go.Scatter(
        x=x,
        y=y,
        mode='lines',
        fill='tozeroy'
    ),
    layout=plotting.create_layout(
        title='Coupling Length KDE',
def compute_normality_weighted(
        key: str, trackway_df: pd.DataFrame) -> mstats.ValueUncertainty:
    dist = to_distribution(key, trackway_df)
    mean = mstats.mean.weighted(*dist.measurements)
    comparison = mstats.create_distribution([mean])
    return 1.0 - mstats.distributions.overlap2(dist, comparison)
示例#31
0
def distribution(
        df: pd.DataFrame,
        segment_min: float = 0,
        segment_max: float = 100,
        scale_factor: float = 1.0
) -> dict:
    """

    :param df:
    :param segment_min:
    :param segment_max:
    :param scale_factor:
    :return:
    """

    measurements = mstats.values.join(
        df['coupling_length'].tolist(),
        df['uncertainty'].tolist()
    )

    dist = mstats.create_distribution(measurements)
    x_values = mstats.distributions.adaptive_range(dist, 4)
    y_values = dist.heighted_probabilities_at(x_values, df['fitness'])

    max_value = functools.reduce(
        lambda a, b: (a if a[1] > b[1] else b),
        zip(x_values, y_values),
        (0, 0)
    )

    points = [
        p for p in zip(x_values, y_values)
        if segment_min < p[0] < segment_max
        ]
    x_segment_values, y_segment_values = zip(*points)

    population = mstats.distributions.population(dist, 4096)
    population = [x for x in population if segment_min < x < segment_max]

    coupling_length = mstats.ValueUncertainty(
        np.median(population),
        mstats.distributions.weighted_median_average_deviation(population)
    )

    gaussian_fit = mstats.create_distribution([coupling_length])

    y_gauss_values = gaussian_fit.probabilities_at(x_values)
    y_gauss_values = [scale_factor * y for y in y_gauss_values]

    return dict(
        dist=dist,
        max=max_value,
        coupling_length=coupling_length,
        values=dict(
            x=x_values,
            y=y_values
        ),
        segment=dict(
            x=x_segment_values,
            y=y_segment_values
        ),
        gauss=dict(
            x=x_values,
            y=y_gauss_values
        )
    )
def positions(
        foot_positions: limb.Property,
        times: dict
) -> typing.Dict[str, list]:
    """
    Iterates over the foot positions lists and computes the coupling length for
    each entry in the lists (a time step of the simulation) as well as the
    positions of the rear and forward couplers.

    :param foot_positions:
        The foot positions for each limb at each simulated time step

    :param times:

    :return:
        A dictionary with the following keys:
            * lengths: The coupling length at each simulated time step
            * rear: The rear coupler position at each simulated time step
            * forward: The forward coupler position at each simulated time step
    """

    coupling_events = []
    pes_coupler_positions = []
    manus_coupler_positions = []
    midpoint_positions = []

    for i, time in enumerate(times['cycles']):
        pes_pos = foot_positions.left_pes[i].midpoint_between(
            foot_positions.right_pes[i]
        )
        pes_coupler_positions.append(pes_pos)

        manus_pos = foot_positions.left_manus[i].midpoint_between(
            foot_positions.right_manus[i]
        )
        manus_coupler_positions.append(manus_pos)

        midpoint_pos = pes_pos.midpoint_between(manus_pos)
        midpoint_positions.append(midpoint_pos)

        sample_time = 2 * time
        if not mstats.value.equivalent(sample_time - int(sample_time), 0):
            continue

        coupling_events.append(events.Event(
            time=time,
            index=i,
            value=pes_pos.distance_between(manus_pos)
        ))

    lengths = [v.value for v in coupling_events]
    d = mstats.create_distribution(lengths)
    median = mstats.distributions.percentile(d, 0.5)
    median_deviations = []

    for cl in lengths:
        median_deviations.append(cl - median)

    return dict(
        lengths=coupling_events,
        rear=pes_coupler_positions,
        forward=manus_coupler_positions,
        midpoints=midpoint_positions,
        deviations=median_deviations
    )
示例#33
0
import cauldron as cd
import plotly.graph_objs as go
from cauldron import plotting
import measurement_stats as mstats

df = cd.shared.couplings_df

variations = df['raw_mad'] / df['raw_median']

cd.display.plotly(data=go.Histogram(x=variations),
                  layout=plotting.create_layout(
                      title='Coupling Length Fractional Deviations',
                      x_label='Fractional Deviation',
                      y_label='Frequency (#)'))

dist = mstats.create_distribution(measurements=variations.tolist(),
                                  uncertainties=0.01)

x = mstats.distributions.uniform_range(dist, 3)
y = dist.probabilities_at(x)

cd.display.plotly(data=go.Scatter(x=x, y=y, mode='lines', fill='tozeroy'),
                  layout=plotting.create_layout(title='Coupling Length KDE',
                                                x_label='Coupling Lengths (m)',
                                                y_label='Expectation Value'))
示例#34
0
    To include uncertainty each fitness value needs to be represented as a
    Gaussian kernel function with a width based on the uncertainty in coupling
    length for that trial. Each Gaussian is weighted according to it fitness
    such that a low fitness value produces a correspondingly smaller Gaussian.

    The sum over all Gaussian kernels leads to a fitness distribution by
    coupling length.
    """
)

measurements = mstats.values.join(
    df['coupling_length'].tolist(),
    df['uncertainty'].tolist()
)

distribution = mstats.create_distribution(measurements)
x_values = mstats.distributions.adaptive_range(distribution, 4)
y_values = distribution.heighted_probabilities_at(x_values, df['fitness'])

distribution_trace = go.Scatter(
    x=x_values,
    y=y_values,
    name='Distribution',
    mode='lines',
    fill='tozeroy'
)

cd.display.plotly(
    distribution_trace,
    layout=plotting.create_layout(
        title='Kernel Density Solution Fitness',
import plotly.graph_objs as go
import numpy as np

from cauldron import plotting
from cauldron import project

import measurement_stats as mstats

df = project.shared.measurements

normalized_values = []
traces = []

for key in ['Width-Box', 'Height-Box']:
    d = mstats.create_distribution(
        measurements=df[key].tolist(),
        uncertainties=5
    )
    median = mstats.distributions.percentile(d)

    normalized = [100.0 * (v / median - 1.0) for v in d.measurements]
    normalized_values += normalized

    x_values = list(np.arange(1, len(normalized), 1))
    y_values, y_uncertainties = mstats.values.unzip(normalized)
    traces.append(go.Bar(
        x=x_values,
        y=y_values,
        error_y=dict(
            type='data',
            array=y_uncertainties,
            visible=True
import measurement_stats as mstats
from cauldron import project
from cauldron import plotting

import plotly.graph_objs as go

d = mstats.create_distribution(project.shared.normalized_values)

x_values = mstats.distributions.adaptive_range(d, 3.0, max_delta=0.1)
y_values = d.probabilities_at(x_values)

data = go.Scatter(x=x_values, y=y_values, fill='tozeroy')

layout = plotting.create_layout(
    title='Combined Measurement Deviations Cumulative Distribution',
    x_label='Median Deviation (%)',
    y_label='Expectation Value (au)')

project.display.markdown("""
    Cumulative Distribution
    -----------------------

    In this final plot, the median-deviation values for every previous
    measurement were combined into a single weighted distribution and plotted
    to inspect the structure of the variance. An important conclusion to take
    away from this distribution is that the small humps, particularly in the
    lower half of the graph, are the result of singular or small numbers of
    measurements. This indicates that the measurement distribution is not
    robustly sampled and that more samples should be added for additional
    statistical validation of the result.
    """)
normalized_lengths = []
length_traces = []

normalized_angles = []
angle_traces = []

for key_data in project.shared.measurement_keys:
    if key_data['type'] == 'length':
        combined_normalized = normalized_lengths
        combined_traces = length_traces
    else:
        combined_normalized = normalized_angles
        combined_traces = angle_traces

    d = mstats.create_distribution(
        measurements=df[key_data['keys'][0]].tolist(),
        uncertainties=df[key_data['keys'][1]].tolist())

    median = mstats.distributions.percentile(d)
    normalized = [100.0 * (v / median - 1.0) for v in d.measurements]
    combined_normalized += normalized

    x_values = list(np.arange(1, len(normalized), 1))
    y_values, y_uncertainties = mstats.values.unzip(normalized)

    combined_traces.append(
        go.Bar(
            x=x_values,
            y=y_values,
            error_y=dict(type='data', array=y_uncertainties, visible=True),
            name=key_data['label'],
示例#38
0
        x=np.arange(0, len(aisle_capacity), 1) / 60.0,
        y=aisle_capacity,
        mode='lines',
        fill='tozeroy',
        line={'color': 'purple'}
    ),
    layout=plotting.create_layout(
        title='Aisle Capacity',
        x_label='Boarding Time (minutes)',
        y_label='Capacity',
        y_bounds=[0, 100]
    )
)

distribution = mstats.create_distribution(
    measurements=aisle_capacity,
    uncertainties=50.0 / aisle_count
)
x_values = mstats.distributions.uniform_range(distribution, 3, 1024)

project.display.plotly(
    data=go.Scatter(
        x=x_values,
        y=distribution.probabilities_at(x_values),
        mode='lines',
        fill='tozeroy',
        line={'color': 'purple'}
    ),
    layout=plotting.create_layout(
        title='Aisle Capacity KDE Distribution',
        x_label='Aisle Capacity (%)',
        y_label='Expectation Value (au)'
import measurement_stats as mstats
from cauldron import project
from cauldron import plotting

import plotly.graph_objs as go

d = mstats.create_distribution(project.shared.normalized_values)

x_values = mstats.distributions.adaptive_range(d, 3.0, max_delta=0.1)
y_values = d.probabilities_at(x_values)


data = go.Scatter(
    x=x_values,
    y=y_values,
    fill='tozeroy'
)

layout = plotting.create_layout(
    title='Combined Measurement Deviations Cumulative Distribution',
    x_label='Median Deviation (%)',
    y_label='Expectation Value (au)'
)

project.display.markdown(
    """
    Cumulative Distribution
    -----------------------

    In this final plot, the median-deviation values for every previous
    measurement were combined into a single weighted distribution and plotted
示例#40
0
import pandas as pd
import plotly.graph_objs as go

from _Gauge_Quantization import plotting

tracks = cd.shared.tracks  # type: pd.DataFrame
trackway_name = cd.shared.TRACKWAY_NAME
unweighted = cd.shared.unweighted
pes_tracks = tracks[tracks['pes'] == 1]

layout = go.Layout(
    title='{} Pes Gauge Values Along Trackway'.format(trackway_name),
    xaxis={'title': 'Trackway Position (m)'},
    yaxis={'title': 'Gauge (m)'})

dist = mstats.create_distribution(pes_tracks['simpleGauge'].tolist(),
                                  pes_tracks['simpleGaugeUnc'].tolist())
pop = mstats.distributions.population(dist)
median_value = mstats.distributions.percentile(pop)
mad = mstats.distributions.weighted_median_average_deviation(pop)
median = mstats.ValueUncertainty(median_value, mad)

scatter_trace = plotting.create_scatter(pes_tracks['curvePosition'],
                                        pes_tracks['simpleGauge'],
                                        pes_tracks['simpleGaugeUnc'],
                                        name='Gauges')

median_traces = plotting.make_ranged_quantity_traces(
    x_start=pes_tracks['curvePosition'].min(),
    x_end=pes_tracks['curvePosition'].max(),
    quantity=median,
    name='Median',