Open databases in the logbook package so the user does not have to pass in a

boltDB pointer.
This commit is contained in:
gmp
2025-05-15 13:08:28 -04:00
parent b39deb1986
commit a6604728d9
6 changed files with 456 additions and 610 deletions
+97 -15
View File
@@ -12,10 +12,15 @@ import (
// Estimator is the main interface for incremental, time-aware strength estimation.
type Estimator interface {
Fit(weight, reps []float64, dates []time.Time) error
Estimate1RM(t time.Time) float64
EstimateReps(t time.Time, targetWeight float64) float64
EstimateMaxWeight(t time.Time, nReps float64) float64
Params(t time.Time) []float64
Estimate1RM(times ...time.Time) float64
EstimateReps(targetWeight float64, times ...time.Time) float64
EstimateMaxWeight(nReps float64, times ...time.Time) float64
Params(times ...time.Time) []float64
TimelineEstimate1RM(interval time.Duration, starts ...time.Time) ([]time.Time, []float64)
TimelineEstimateReps(targetWeight float64, interval time.Duration, starts ...time.Time) ([]time.Time, []float64)
TimelineEstimateMaxWeight(nReps float64, interval time.Duration, starts ...time.Time) ([]time.Time, []float64)
}
// --- Functional Options ---
@@ -117,14 +122,16 @@ func (e *estimatorImpl) Fit(weight, reps []float64, dates []time.Time) error {
return nil
}
// Estimate1RM returns the smoothed 1RM estimate at time t.
func (e *estimatorImpl) Estimate1RM(t time.Time) float64 {
// --- Estimate Functions with Variadic Time Parameter ---
func (e *estimatorImpl) Estimate1RM(times ...time.Time) float64 {
t := e.resolveTime(times...)
a, b := e.smoothedParamsAt(t)
return a * math.Pow(1, b)
}
// EstimateReps returns the predicted number of reps at a given weight and time.
func (e *estimatorImpl) EstimateReps(t time.Time, targetWeight float64) float64 {
func (e *estimatorImpl) EstimateReps(targetWeight float64, times ...time.Time) float64 {
t := e.resolveTime(times...)
a, b := e.smoothedParamsAt(t)
if a == 0 || b == 0 {
return 0
@@ -132,21 +139,98 @@ func (e *estimatorImpl) EstimateReps(t time.Time, targetWeight float64) float64
return math.Pow(targetWeight/a, 1/b)
}
// EstimateMaxWeight returns the predicted max weight for a given number of reps at time t.
func (e *estimatorImpl) EstimateMaxWeight(t time.Time, nReps float64) float64 {
func (e *estimatorImpl) EstimateMaxWeight(nReps float64, times ...time.Time) float64 {
t := e.resolveTime(times...)
a, b := e.smoothedParamsAt(t)
return a * math.Pow(nReps, b)
}
// Params returns the smoothed model parameters at time t.
func (e *estimatorImpl) Params(t time.Time) []float64 {
func (e *estimatorImpl) Params(times ...time.Time) []float64 {
t := e.resolveTime(times...)
a, b := e.smoothedParamsAt(t)
return []float64{a, b}
}
// --- Timeline Functions with Variadic Start Parameter ---
func (e *estimatorImpl) TimelineEstimate1RM(interval time.Duration, starts ...time.Time) ([]time.Time, []float64) {
return e.timelineEstimate(
func(a, b float64) float64 { return a * math.Pow(1, b) },
interval, starts...,
)
}
func (e *estimatorImpl) TimelineEstimateReps(targetWeight float64, interval time.Duration, starts ...time.Time) ([]time.Time, []float64) {
return e.timelineEstimate(
func(a, b float64) float64 {
if a == 0 || b == 0 {
return 0
}
return math.Pow(targetWeight/a, 1/b)
},
interval, starts...,
)
}
func (e *estimatorImpl) TimelineEstimateMaxWeight(nReps float64, interval time.Duration, starts ...time.Time) ([]time.Time, []float64) {
return e.timelineEstimate(
func(a, b float64) float64 { return a * math.Pow(nReps, b) },
interval, starts...,
)
}
func (e *estimatorImpl) timelineEstimate(
f func(a, b float64) float64,
interval time.Duration,
starts ...time.Time,
) ([]time.Time, []float64) {
var times []time.Time
var values []float64
if len(e.data) == 0 {
return times, values
}
start := e.resolveTimelineStart(starts...)
end := e.data[len(e.data)-1].date
for t := start; !t.After(end); t = t.Add(interval) {
a, b := e.smoothedParamsAt(t)
times = append(times, t)
values = append(values, f(a, b))
}
return times, values
}
// --- Internal Helpers ---
// smoothedParamsAt returns the smoothed parameters for the closest time point <= t.
// resolveTime returns the correct time to use based on the variadic argument.
// Panics if more than one time is provided.
func (e *estimatorImpl) resolveTime(times ...time.Time) time.Time {
if len(times) == 0 {
if len(e.data) == 0 {
return time.Time{}
}
return e.data[len(e.data)-1].date
}
if len(times) == 1 {
return times[0]
}
panic("at most one time argument allowed")
}
// resolveTimelineStart returns the timeline start time based on the variadic argument.
// Panics if more than one start time is provided.
func (e *estimatorImpl) resolveTimelineStart(starts ...time.Time) time.Time {
if len(starts) == 0 {
if len(e.data) == 0 {
return time.Time{}
}
return e.data[0].date
}
if len(starts) == 1 {
return starts[0]
}
panic("at most one start time argument allowed")
}
func (e *estimatorImpl) smoothedParamsAt(t time.Time) (float64, float64) {
if len(e.data) == 0 {
return 0, 0
@@ -163,7 +247,6 @@ func (e *estimatorImpl) smoothedParamsAt(t time.Time) (float64, float64) {
return e.smoothedA[idx-1], e.smoothedB[idx-1]
}
// fitPowerLaw fits a power law model to the data.
func fitPowerLaw(weight, reps []float64, dates []time.Time, halfLifeDays float64) (a, b float64) {
now := dates[len(dates)-1]
params := []float64{max(weight), -0.1}
@@ -218,7 +301,6 @@ func extractB(data []timePoint) []float64 {
return out
}
// exponentialSmoothing applies exponential smoothing to a time series.
func exponentialSmoothing(series []float64, alpha float64) []float64 {
if len(series) == 0 {
return nil