Ejemplo n.º 1
0
def test_strava_power_model_auto_compute():
    # check that the acceleration and the elevation will be auto-computed
    power_auto = strava_power_model(activity, cyclist_weight=70)

    activity_ele_acc = activity.copy()
    activity_ele_acc = gradient_elevation(activity)
    activity_ele_acc = acceleration(activity_ele_acc)
    power_ele_acc = strava_power_model(activity_ele_acc, cyclist_weight=70)

    assert_series_equal(power_auto, power_ele_acc)
Ejemplo n.º 2
0
# License: BSD 3 clause

###############################################################################
# We can first grab a file and read all data available

from skcycling.datasets import load_fit
from skcycling.io import bikeread

ride = bikeread(load_fit()[0], drop_nan='columns')

###############################################################################
# We can use a physical model to infer the power.

from skcycling.model import strava_power_model

power = strava_power_model(ride, cyclist_weight=72)

###############################################################################
# We can plot the measured and estimated power to observe the difference. We
# can also compute the median absolute error to asses the quality of the
# estimation. To ease the interpretation, we will first resample the data.

import matplotlib.pyplot as plt
from sklearn.metrics import median_absolute_error

resampling_rate = '10S'
measured_power = (ride['power'].resample(resampling_rate)
                               .mean()
                               .rename('Measured Power'))
estimated_power = (power.resample(resampling_rate)
                        .mean()
Ejemplo n.º 3
0
groups = []
for group_idx, activity in enumerate(data):
    groups += [group_idx] * activity.shape[0]
groups = np.array(groups)

weight_user = {'user_1': 86., 'user_2': 72., 'user_3': 61., 'user_5': 72.}
weight_groups = np.zeros_like(groups)
for idx, filename in enumerate(valid_filenames):
    mask = groups == idx
    weight = [value for key, value in weight_user.items() if key in filename]
    weight_groups[mask] = weight[0]

y_pred = np.zeros_like(y.values)
for idx_activity in range(len(valid_filenames)):
    mask = groups == idx_activity
    y_pred[mask] = strava_power_model(X.iloc[mask, :],
                                      np.unique(weight_groups[mask]),
                                      use_acceleration=True).values

# Store the prediction for visualization
path_results = os.path.join('results', 'mathematical_model')
if not os.path.exists(path_results):
    os.makedirs(path_results)
f = os.path.join(path_results, 'y_pred.csv')
pd.Series(y_pred, index=y.index).to_csv(f)
f = os.path.join(path_results, 'y_true.csv')
y.to_csv(f)
np.save(os.path.join(path_results, 'groups.npy'), groups)
f = os.path.join(path_results, 'altitude.csv')
X['elevation'].to_csv(f)
Ejemplo n.º 4
0
# License: BSD 3 clause

###############################################################################
# We can first grab a file and read all data available

from skcycling.datasets import load_fit
from skcycling.io import bikeread

ride = bikeread(load_fit()[0], drop_nan='columns')

###############################################################################
# We can use a physical model to infer the power.

from skcycling.model import strava_power_model

power = strava_power_model(ride, cyclist_weight=72)

###############################################################################
# We can plot the measured and estimated power to observe the difference. We
# can also compute the median absolute error to asses the quality of the
# estimation. To ease the interpretation, we will first resample the data.

import matplotlib.pyplot as plt
from sklearn.metrics import median_absolute_error

resampling_rate = '10S'
measured_power = (
    ride['power'].resample(resampling_rate).mean().rename('Measured Power'))
estimated_power = (
    power.resample(resampling_rate).mean().rename('Estimate Power'))
Ejemplo n.º 5
0
def test_strava_power_model():
    # at constant speed the acceleration should not have any influence
    power_without_acc = strava_power_model(activity,
                                           cyclist_weight=78,
                                           use_acceleration=False)
    power_with_acc = strava_power_model(activity,
                                        cyclist_weight=78,
                                        use_acceleration=True)
    assert_allclose(power_without_acc, power_with_acc)

    # increase cyclist weight should increase power
    power_initial = strava_power_model(activity, cyclist_weight=70)
    power_increase_weight = strava_power_model(activity, cyclist_weight=78)
    assert_array_less(power_initial, power_increase_weight)

    # increase bike weight should increase power
    power_initial = strava_power_model(activity,
                                       cyclist_weight=70,
                                       bike_weight=7)
    power_increase_weight = strava_power_model(activity,
                                               cyclist_weight=70,
                                               bike_weight=8)
    assert_array_less(power_initial, power_increase_weight)

    # increase the rolling coefficient should increase power
    power_initial = strava_power_model(activity,
                                       cyclist_weight=70,
                                       coef_roll_res=0.0045)
    power_increase_cr = strava_power_model(activity,
                                           cyclist_weight=70,
                                           coef_roll_res=0.006)
    assert_array_less(power_initial, power_increase_cr)

    # increase of the pressure should increase power
    power_initial = strava_power_model(activity,
                                       cyclist_weight=70,
                                       pressure=101325)
    power_increase_pressure = strava_power_model(activity,
                                                 cyclist_weight=70,
                                                 pressure=110000)
    assert_array_less(power_initial, power_increase_pressure)

    # decrease the temperature should increase the power
    power_initial = strava_power_model(activity,
                                       cyclist_weight=70,
                                       temperature=15.0)
    power_decrease_temperature = strava_power_model(activity,
                                                    cyclist_weight=70,
                                                    temperature=10.0)
    assert_array_less(power_initial, power_decrease_temperature)

    # increase the drag coefficient should increase the power
    power_initial = strava_power_model(activity,
                                       cyclist_weight=70,
                                       coef_drag=0.5)
    power_increase_coef_drag = strava_power_model(activity,
                                                  cyclist_weight=70,
                                                  coef_drag=1.0)
    assert_array_less(power_initial, power_increase_coef_drag)

    # increase the rider frontal surface should increase the power
    power_initial = strava_power_model(activity,
                                       cyclist_weight=70,
                                       surface_rider=0.32)
    power_increase_surface_rider = strava_power_model(activity,
                                                      cyclist_weight=70,
                                                      surface_rider=0.5)
    assert_array_less(power_initial, power_increase_surface_rider)
Ejemplo n.º 6
0
def test_strava_power_model_error(activity_corrupted, use_acceleration):
    with pytest.raises(MissingDataError):
        strava_power_model(activity_corrupted,
                           cyclist_weight=70,
                           use_acceleration=use_acceleration)