Skip to content

Metrics

When you import chironpy with import chironpy all pandas data frames and series have a chironpy accessor available that allows you to do things like:

df.chironpy.mean_max("column name")

...or

df["power"].chironpy.mean_max()

See below for more examples. The chiron accessor will raise an AttributeError when the data frame or series content is not valid.

Please note that the chiron accessor for data frames and series are similar but not identical.

Most methods on the chiron accessor are also available for usage outside data frames.

Mean max

import chironpy

example = chironpy.examples(path="4078723797.fit")
data = chironpy.read_fit(example.path)

mmp = data["power"].chironpy.mean_max()
mmp
0 days 00:00:01    1022.000000
0 days 00:00:02     980.500000
0 days 00:00:03     928.666667
0 days 00:00:04     847.750000
0 days 00:00:05     721.600000
                      ...     
0 days 02:19:12     219.201748
0 days 02:19:13     219.175506
0 days 02:19:14     219.149270
0 days 02:19:15     219.123040
0 days 02:19:16     219.096817
Name: mean_max_power, Length: 8356, dtype: float64

Or for multiple columns at once:

import chironpy

example = chironpy.examples(path="4078723797.fit")
data = chironpy.read_fit(example.path)

mean_max = data.chironpy.mean_max(["power", "heartrate"])
mean_max
mean_max_power mean_max_heartrate
0 days 00:00:01 1022.000000 190.000000
0 days 00:00:02 980.500000 190.000000
0 days 00:00:03 928.666667 190.000000
0 days 00:00:04 847.750000 190.000000
0 days 00:00:05 721.600000 190.000000
... ... ...
0 days 02:19:12 219.201748 164.746049
0 days 02:19:13 219.175506 164.738178
0 days 02:19:14 219.149270 164.730668
0 days 02:19:15 219.123040 164.723639
0 days 02:19:16 219.096817 164.716850

8356 rows × 2 columns

The mean_max() methods accept a monotic boolean argument that can be used to force a monotonically decreasing mean max curve. The default is False.

import chironpy

example = chironpy.examples(path="4078723797.fit")
data = chironpy.read_fit(example.path)

mean_max = data.chironpy.mean_max(["power", "heartrate"], monotonic=True)

The mean_max() function is also available as chironpy.metrics.core.mean_max().

Timedelta index

The data frames returned by the read_*() functions have a pandas.DatetimeIndex by default. Sometimes it is usefull to have a relative pandas.TimedeltaIndex:

import chironpy

example = chironpy.examples(path="4078723797.fit")
activity = chironpy.read_fit(example.path)[["power", "heartrate"]]

activity = activity.chironpy.to_timedelta_index()
activity.head()
power heartrate
datetime
0 days 00:00:00 0 111
0 days 00:00:01 0 108
0 days 00:00:02 0 106
0 days 00:00:03 0 102
0 days 00:00:04 0 99

Then you can do things like slice for the first 10 seconds of an activity:

activity[:"00:00:10"]
power heartrate
datetime
0 days 00:00:00 0 111
0 days 00:00:01 0 108
0 days 00:00:02 0 106
0 days 00:00:03 0 102
0 days 00:00:04 0 99
0 days 00:00:05 0 95
0 days 00:00:06 0 94
0 days 00:00:07 0 94
0 days 00:00:08 0 97
0 days 00:00:09 0 97
0 days 00:00:10 0 101

The chironpy.to_timedelta_index() method is available on both data frames and series.

Training zones

Working with training zones is easy in chironpy. To add a column with the heart rate zone label to the data frame:

import chironpy

example = chironpy.examples(path="4078723797.fit")
activity = chironpy.read_fit(example.path)[["power", "heartrate"]]

activity["heartrate_zone"] = activity["heartrate"].chironpy.calculate_zones(
    bins=[0, 100, 140, 160, 999],
    labels=["rest", "D1", "D2", "D3"])
activity["heartrate_zone"].head()
datetime
2019-09-20 14:10:03+00:00      D1
2019-09-20 14:10:04+00:00      D1
2019-09-20 14:10:05+00:00      D1
2019-09-20 14:10:06+00:00      D1
2019-09-20 14:10:07+00:00    rest
Name: heartrate_zone, dtype: category
Categories (4, object): ['rest' < 'D1' < 'D2' < 'D3']

...where the bins argument contains the left and right bounds for each training zone and the labels argument the zone labels.

To calculate the time in zone:

import chironpy

example = chironpy.examples(path="4078723797.fit")
activity = chironpy.read_fit(example.path)[["power", "heartrate"]]

time_in_zone = activity["power"].chironpy.time_in_zone(
    bins=[-9999, 150, 230, 320, 9999],
    labels=["rest", "D1", "D2", "D3"])
time_in_zone
D2     0 days 00:58:06
rest   0 days 00:36:25
D3     0 days 00:25:40
D1     0 days 00:19:06
Name: power, dtype: timedelta64[ns]