stressfactor 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,24 @@
1
+ module Stressfactor
2
+ class AveragePaceAccumulator
3
+ attr_accessor :intervals
4
+
5
+ def initialize(intervals)
6
+ @intervals = intervals
7
+ end
8
+
9
+ def average_pace(strategy: :raw)
10
+ strategy_klass = strategy == :raw ? RawPaceStrategy : GradeAdjustedPaceStrategy
11
+ intervals.inject(0) do |acc, interval|
12
+ weighted_interval = (interval.distance / total_distance) * strategy_klass.calculate_for_interval(interval)
13
+ acc + weighted_interval
14
+ end
15
+ end
16
+ private
17
+ # Total elapsed time in minutes
18
+ def total_distance
19
+ @total_distance ||= intervals.inject(0) do |acc, interval|
20
+ acc + interval.distance
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,39 @@
1
+ module Stressfactor
2
+ class GpxLoader
3
+ attr_reader :gpx
4
+
5
+ def initialize(gpx_file_path)
6
+ @gpx = GPX::GPXFile.new(gpx_file: gpx_file_path)
7
+ end
8
+
9
+ # units: seconds
10
+ def total_time
11
+ intervals.inject(0) do |acc, i|
12
+ acc + i.time
13
+ end
14
+ end
15
+
16
+ # An array of instantaneous pace times in min/km from comparing
17
+ # two trackpoints
18
+ def intervals
19
+ @intervals ||= begin
20
+ points.map do |p1|
21
+ idx = points.index(p1)
22
+ p2 = points[idx+1]
23
+
24
+ if p2
25
+ Interval.new(p1, p2)
26
+ else
27
+ nil
28
+ end
29
+ end
30
+ end
31
+ @intervals.compact
32
+ end
33
+
34
+ # An array of GPX::TrackPoint objects.
35
+ def points
36
+ @points ||= gpx.tracks.flat_map(&:points)
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,24 @@
1
+ # Inspired by the work CTM Davies' research, as discussed by the Strava
2
+ # blog: http://engineering.strava.com/improving-grade-adjusted-pace/
3
+ module Stressfactor
4
+ class GradeAdjustedPaceStrategy < PaceStrategy
5
+ attr_accessor :interval
6
+
7
+ def self.calculate_for_interval(interval)
8
+ observed_pace = interval.time(units: :minutes) / interval.distance
9
+ interval.grade > 0 ? incline_pace(interval, observed_pace) : decline_pace(interval, observed_pace)
10
+ end
11
+
12
+ def self.decline_pace(interval, observed_pace)
13
+ coefficient_per_grade_point = 0.01815
14
+ grade = interval.grade
15
+ observed_pace / (1 - (coefficient_per_grade_point * grade))
16
+ end
17
+
18
+ def self.incline_pace(interval, observed_pace)
19
+ coefficient_per_grade_point = 0.033
20
+ grade = interval.grade
21
+ observed_pace / (1 + (coefficient_per_grade_point * grade))
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,35 @@
1
+ module Stressfactor
2
+ class Interval
3
+ attr_accessor :start_point, :end_point
4
+
5
+ def initialize(start_point, end_point)
6
+ @start_point = start_point
7
+ @end_point = end_point
8
+ end
9
+
10
+ # time in seconds
11
+ def time(options={})
12
+ delta = end_point.time - start_point.time
13
+ delta /= 60.0 if options[:units] == :minutes
14
+ delta
15
+ end
16
+
17
+ # distance in km
18
+ def distance
19
+ end_point.haversine_distance_from(start_point)
20
+ end
21
+
22
+ # grade in percent incline/decline
23
+ def grade
24
+ elevation_change = end_point.elevation - start_point.elevation
25
+ # Normalize to meters
26
+ distance_change = distance * 1000
27
+ radians = Math.atan(elevation_change / distance_change)
28
+ 180 * radians / Math::PI
29
+ end
30
+
31
+ def to_s
32
+ "i: #{time}s | #{distance * 1000}m | #{grade}%"
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,24 @@
1
+ require "ostruct"
2
+
3
+ module Stressfactor
4
+ class PaceCalculator
5
+ attr_accessor :gpx_loader
6
+
7
+ def initialize(gpx_loader)
8
+ @gpx_loader = gpx_loader
9
+ end
10
+
11
+ def calculate(strategy: :grade_adjusted, units: :metric)
12
+ pace = AveragePaceAccumulator.new(intervals).average_pace(strategy: strategy)
13
+ pace *= (1/0.621371) if units == :english
14
+ pace
15
+ end
16
+
17
+ private
18
+
19
+ def intervals
20
+ gpx_loader.intervals
21
+ end
22
+ end
23
+ end
24
+
@@ -0,0 +1,11 @@
1
+ # Abstract base class for pace strategies.
2
+ module Stressfactor
3
+ class PaceStrategy
4
+ attr_reader :intervals
5
+
6
+ def initialize(intervals)
7
+ @intervals = intervals
8
+ end
9
+
10
+ end
11
+ end
@@ -0,0 +1,14 @@
1
+ # Take the exact average over all paces over the set of
2
+ # run intervals.
3
+ module Stressfactor
4
+ class RawPaceStrategy < PaceStrategy
5
+
6
+ def self.calculate_for_interval(interval)
7
+ interval.time(units: :minutes) / interval.distance
8
+ end
9
+
10
+ def calculate
11
+ AveragePaceAccumulator.new(intervals).average_pace(strategy: :raw)
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,29 @@
1
+ # Implements the actual TSS algorithm.
2
+ module Stressfactor
3
+ class StressCalculator
4
+ attr_reader :threshold_pace, :loader
5
+
6
+ def initialize(threshold_pace: threshold_pace, loader: loader)
7
+ @threshold_pace = threshold_pace
8
+ @loader = loader
9
+ end
10
+
11
+ def calculate
12
+ (total_time * normalized_graded_pace * intensity_factor * 100) / (threshold_pace * 3600)
13
+ end
14
+
15
+ private
16
+
17
+ def total_time
18
+ loader.total_time
19
+ end
20
+
21
+ def intensity_factor
22
+ normalized_graded_pace / threshold_pace
23
+ end
24
+
25
+ def normalized_graded_pace
26
+ @normalized_graded_pace ||= PaceCalculator.new(loader).calculate(strategy: :grade_adjusted, units: :metric)
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,3 @@
1
+ module Stressfactor
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,7 @@
1
+ require_relative "../lib/stressfactor"
2
+ require "pry"
3
+ file = GPX::GPXFile.new(:gpx_file => "examples/data/sample.gpx")
4
+ pc = Stressfactor::PaceCalculator.new(file)
5
+ pace = pc.calculate
6
+ puts pace
7
+ binding.pry
@@ -0,0 +1,91 @@
1
+ require_relative "../lib/stressfactor.rb"
2
+
3
+ # This file was generated by the `rspec --init` command. Conventionally, all
4
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
5
+ # The generated `.rspec` file contains `--require spec_helper` which will cause this
6
+ # file to always be loaded, without a need to explicitly require it in any files.
7
+ #
8
+ # Given that it is always loaded, you are encouraged to keep this file as
9
+ # light-weight as possible. Requiring heavyweight dependencies from this file
10
+ # will add to the boot time of your test suite on EVERY test run, even for an
11
+ # individual file that may not need all of that loaded. Instead, consider making
12
+ # a separate helper file that requires the additional dependencies and performs
13
+ # the additional setup, and require it from the spec files that actually need it.
14
+ #
15
+ # The `.rspec` file also contains a few flags that are not defaults but that
16
+ # users commonly want.
17
+ #
18
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
19
+ RSpec.configure do |config|
20
+ # rspec-expectations config goes here. You can use an alternate
21
+ # assertion/expectation library such as wrong or the stdlib/minitest
22
+ # assertions if you prefer.
23
+ config.expect_with :rspec do |expectations|
24
+ # This option will default to `true` in RSpec 4. It makes the `description`
25
+ # and `failure_message` of custom matchers include text for helper methods
26
+ # defined using `chain`, e.g.:
27
+ # be_bigger_than(2).and_smaller_than(4).description
28
+ # # => "be bigger than 2 and smaller than 4"
29
+ # ...rather than:
30
+ # # => "be bigger than 2"
31
+ expectations.include_chain_clauses_in_custom_matcher_descriptions = true
32
+ end
33
+
34
+ # rspec-mocks config goes here. You can use an alternate test double
35
+ # library (such as bogus or mocha) by changing the `mock_with` option here.
36
+ config.mock_with :rspec do |mocks|
37
+ # Prevents you from mocking or stubbing a method that does not exist on
38
+ # a real object. This is generally recommended, and will default to
39
+ # `true` in RSpec 4.
40
+ mocks.verify_partial_doubles = true
41
+ end
42
+
43
+ # The settings below are suggested to provide a good initial experience
44
+ # with RSpec, but feel free to customize to your heart's content.
45
+ =begin
46
+ # These two settings work together to allow you to limit a spec run
47
+ # to individual examples or groups you care about by tagging them with
48
+ # `:focus` metadata. When nothing is tagged with `:focus`, all examples
49
+ # get run.
50
+ config.filter_run :focus
51
+ config.run_all_when_everything_filtered = true
52
+
53
+ # Limits the available syntax to the non-monkey patched syntax that is recommended.
54
+ # For more details, see:
55
+ # - http://myronmars.to/n/dev-blog/2012/06/rspecs-new-expectation-syntax
56
+ # - http://teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
57
+ # - http://myronmars.to/n/dev-blog/2014/05/notable-changes-in-rspec-3#new__config_option_to_disable_rspeccore_monkey_patching
58
+ config.disable_monkey_patching!
59
+
60
+ # This setting enables warnings. It's recommended, but in some cases may
61
+ # be too noisy due to issues in dependencies.
62
+ config.warnings = true
63
+
64
+ # Many RSpec users commonly either run the entire suite or an individual
65
+ # file, and it's useful to allow more verbose output when running an
66
+ # individual spec file.
67
+ if config.files_to_run.one?
68
+ # Use the documentation formatter for detailed output,
69
+ # unless a formatter has already been configured
70
+ # (e.g. via a command-line flag).
71
+ config.default_formatter = 'doc'
72
+ end
73
+
74
+ # Print the 10 slowest examples and example groups at the
75
+ # end of the spec run, to help surface which specs are running
76
+ # particularly slow.
77
+ config.profile_examples = 10
78
+
79
+ # Run specs in random order to surface order dependencies. If you find an
80
+ # order dependency and want to debug it, you can fix the order by providing
81
+ # the seed, which is printed after each run.
82
+ # --seed 1234
83
+ config.order = :random
84
+
85
+ # Seed global randomization in this process using the `--seed` CLI option.
86
+ # Setting this allows you to use `--seed` to deterministically reproduce
87
+ # test failures related to randomization by passing the same `--seed` value
88
+ # as the one that triggered the failure.
89
+ Kernel.srand config.seed
90
+ =end
91
+ end
@@ -0,0 +1,59 @@
1
+ require "spec_helper"
2
+
3
+ describe Stressfactor::GpxLoader do
4
+ let(:intervals) do
5
+ [Stressfactor::Interval.new(p1, p2),
6
+ Stressfactor::Interval.new(p2, p3)]
7
+ end
8
+
9
+ let(:end_time) { Time.now }
10
+ let(:p1) do
11
+ GPX::TrackPoint.new(
12
+ :lat => 37.7985474,
13
+ :lon => -122.2554386,
14
+ :elevation => 10.0,
15
+ :time => end_time - 10
16
+ )
17
+ end
18
+
19
+ let(:p2) do
20
+ GPX::TrackPoint.new(
21
+ :lat => 37.7985583,
22
+ :lon => -122.2554564,
23
+ :elevation => 15.0,
24
+ :time => end_time - 3
25
+ )
26
+ end
27
+
28
+ let(:p3) do
29
+ GPX::TrackPoint.new(
30
+ :lat => 37.7986548,
31
+ :lon => -122.2555806,
32
+ :elevation => 10.0,
33
+ :time => end_time
34
+ )
35
+ end
36
+
37
+ let(:path) { "path/to/gpx.gpx" }
38
+ let(:points) { [p1, p2, p3] }
39
+ let(:gpx) { double("gpx") }
40
+ let(:track) { double("track", points: points) }
41
+ subject { Stressfactor::GpxLoader.new(path) }
42
+
43
+ before do
44
+ expect(GPX::GPXFile).to receive(:new).and_return(gpx)
45
+ expect(gpx).to receive(:tracks).and_return([track])
46
+ end
47
+
48
+ describe "#intervals" do
49
+ it "returns two intervals" do
50
+ expect(subject.intervals).to be_all{|i| i.is_a?(Stressfactor::Interval)}
51
+ end
52
+ end
53
+
54
+ describe "#total_time" do
55
+ it "returns correct elapsed time" do
56
+ expect(subject.total_time).to eq 10
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,55 @@
1
+ require "spec_helper"
2
+
3
+ describe Stressfactor::GradeAdjustedPaceStrategy do
4
+ let(:end_time) { Time.now }
5
+ let(:p1) do
6
+ GPX::TrackPoint.new(
7
+ :lat => 37.7985474,
8
+ :lon => -122.2554386,
9
+ :elevation => 10.0,
10
+ :time => end_time - 10
11
+ )
12
+ end
13
+ let(:p2) do
14
+ GPX::TrackPoint.new(
15
+ :lat => 37.7985583,
16
+ :lon => -122.2554564,
17
+ :elevation => 15.0,
18
+ :time => end_time - 3
19
+ )
20
+ end
21
+ let(:p3) do
22
+ GPX::TrackPoint.new(
23
+ :lat => 37.7986548,
24
+ :lon => -122.2555806,
25
+ :elevation => 10.0,
26
+ :time => end_time
27
+ )
28
+ end
29
+
30
+ let(:intervals) do
31
+ [interval]
32
+ end
33
+
34
+ let(:interval) { Stressfactor::Interval.new(p1, p2) }
35
+
36
+ subject { described_class.new(intervals) }
37
+
38
+ describe ".calculate_for_interval" do
39
+ subject { described_class.calculate_for_interval(interval) }
40
+
41
+ it "compensates for the negative coefficient of 3.3% for every 1% incline" do
42
+ expected_pace = 18.100632981302162
43
+ expect(subject).to eq expected_pace
44
+ end
45
+
46
+ context "for declines" do
47
+ let(:interval) { Stressfactor::Interval.new(p2, p3) }
48
+
49
+ it "compensates for the positive coefficient of 1.815% for every 1% decline" do
50
+ expected_pace = 2.459421791214623
51
+ expect(subject).to eq expected_pace
52
+ end
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,51 @@
1
+ require "spec_helper"
2
+
3
+ describe Stressfactor::Interval do
4
+ subject { described_class.new(p1, p2) }
5
+ let(:t1) { Time.now }
6
+ let(:t2) { t1 + 3 }
7
+ let(:t3) { t1 + 6 }
8
+ let(:p1) { GPX::TrackPoint.new(:time => t1, :lat => 0.00001, :lon => 0.00001, :elevation => 10) }
9
+ let(:p2) { GPX::TrackPoint.new(:time => t2, :lat => 0.00002, :lon => 0.00002, :elevation => 15) }
10
+ let(:p3) { GPX::TrackPoint.new(:time => t3, :elevation => 5) }
11
+
12
+ describe "#time" do
13
+ context "for adjacent points" do
14
+ it "calculates the time between the two points" do
15
+ expect(subject.time).to eq(3)
16
+ end
17
+ end
18
+
19
+ it "calculates time in minutes" do
20
+ expect(subject.time(units: :minutes)).to eq(0.05)
21
+ end
22
+
23
+ context "for a different set of points" do
24
+ subject { described_class.new(p1, p3) }
25
+
26
+ it "calculates the time between a second set of points" do
27
+ expect(subject.time).to eq(6)
28
+ end
29
+ end
30
+ end
31
+
32
+ describe "#distance" do
33
+ it "returns haversine distance from one point to the other" do
34
+ expect(p2).to receive(:haversine_distance_from).with(p1).and_return(10)
35
+ expect(subject.distance).to eq(10)
36
+ end
37
+ end
38
+
39
+ describe "#grade" do
40
+ it "returns the integer angle in radians of the elevation change" do
41
+ expected_grade = 72.54128654632675
42
+ expect(subject.grade).to eq(expected_grade)
43
+ end
44
+ end
45
+
46
+ describe "#to_s" do
47
+ it "returns a string with debug info" do
48
+ expect(subject.to_s).to be_instance_of String
49
+ end
50
+ end
51
+ end