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
---------------------------------------------------------------------------
ModuleNotFoundError                       Traceback (most recent call last)
Cell In[1], line 1
----> 1 import chironpy
      3 example = chironpy.examples(path="4078723797.fit")
      4 data = chironpy.read_fit(example.path)

ModuleNotFoundError: No module named 'chironpy'

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

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()

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

activity[:"00:00:10"]

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()

...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