Improve interpolation querying

This commit is contained in:
Dominic Ferrando
2025-09-16 01:07:54 -04:00
parent 6dd0f9333c
commit 189f606b07
2 changed files with 114 additions and 89 deletions
+108 -83
View File
@@ -1,4 +1,4 @@
import { Activity, Attribute, Player, Gender, ActivityPerformance } from "./util"; import { Activity, Attribute, Player, Gender, ActivityPerformance, lbToKg } from "./util";
// SOURCES // SOURCES
// Squat, Bench, Dead Lift: // Squat, Bench, Dead Lift:
@@ -28,15 +28,15 @@ export interface Metrics {
gender: Gender; gender: Gender;
}; };
type DataMetric = Extract<keyof Metrics, "age" | "weight">; type NumberMetric = Extract<keyof Metrics, "age" | "weight">;
export interface Standard { export interface Standard {
metrics: Metrics; metrics: Metrics;
levels: Levels; levels: Levels;
} }
type StandardsGeneratorOptions = { type Generator = {
metric: DataMetric, metric: NumberMetric,
spread: number, spread: number,
step: number, step: number,
ratio: "normal" | "inverse" ratio: "normal" | "inverse"
@@ -45,7 +45,7 @@ type StandardsGeneratorOptions = {
export type ActivityStandards = Record<Activity, { export type ActivityStandards = Record<Activity, {
metadata: { metadata: {
attribute: Attribute, attribute: Attribute,
standardsGeneratorOptions: StandardsGeneratorOptions[] generators: Generator[]
}, },
standards: Standard[] standards: Standard[]
}>; }>;
@@ -145,7 +145,10 @@ export class LevelCalculator {
} }
const activityLevels = activityPerformances.map((p) => { const activityLevels = activityPerformances.map((p) => {
const interpolatedStandard = this.getInterpolatedStandard(p.activity, player.metrics); const interpolatedStandard = this.standards
.byActivity(p.activity)
.byMetrics(player.metrics)
.getInterpolated();
return this.findLevel(interpolatedStandard, p.performance); return this.findLevel(interpolatedStandard, p.performance);
}); });
@@ -156,73 +159,6 @@ export class LevelCalculator {
return activityLevelsAvg; return activityLevelsAvg;
} }
// -------------------------------------------------------------------------------------------------
// Standards interpolation (heavy math bits)
// -------------------------------------------------------------------------------------------------
private getInterpolatedStandard(
activity: Activity,
metrics: Metrics,
): Standard {
const interpolateByWeight = (targetAge: number): Levels => {
const { lower: lowerAgeWeightStandard, upper: upperAgeWeightStandard } = this.standards
.byActivity(activity)
.byGender(metrics.gender)
.byAge(targetAge)
.getNearest("weight", metrics.weight);
const weightLower = lowerAgeWeightStandard.metrics.weight;
const weightUpper = upperAgeWeightStandard.metrics.weight;
let weightRatio = weightUpper === weightLower ? 1 : (metrics.weight - weightLower) / (weightUpper - weightLower);
weightRatio = Math.max(0, Math.min(1, weightRatio));
return this.interpolateLevels(lowerAgeWeightStandard.levels, upperAgeWeightStandard.levels, weightRatio);
}
// Find nearest age standards
const { lower: lowerAgeStandard, upper: upperAgeStandard } = this.standards
.byActivity(activity)
.byGender(metrics.gender)
.getNearest("age", metrics.age);
const ageLower = lowerAgeStandard.metrics.age;
const ageUpper = upperAgeStandard.metrics.age;
// Interpolate by age and weight
let ageRatio = ageUpper === ageLower ? 1 : (metrics.age - ageLower) / (ageUpper - ageLower);
ageRatio = Math.max(0, Math.min(1, ageRatio));
const interpolatedLevels = this.interpolateLevels(
interpolateByWeight(ageLower),
interpolateByWeight(ageUpper),
ageRatio
);
return {
metrics: metrics,
levels: interpolatedLevels
};
}
private interpolateLevels(
lower: Levels,
upper: Levels,
ratio: number,
): Levels {
if (Object.keys(lower).length !== Object.keys(upper).length)
throw new Error("Cannot interpolate between varying number of levels");
const lerp = (lvl: string) => (lower[lvl] as number) + ((upper[lvl] as number) - (lower[lvl] as number)) * ratio;
const interpolatedLevels = {};
for (const lvl in lower) {
interpolatedLevels[lvl] = lerp(lvl);
}
return interpolatedLevels;
}
// ------------------------------------------------------------------------------------------------- // -------------------------------------------------------------------------------------------------
// Level utilities (helpers) // Level utilities (helpers)
// ------------------------------------------------------------------------------------------------- // -------------------------------------------------------------------------------------------------
@@ -246,27 +182,49 @@ export class Standards {
private activityStandards: ActivityStandards; private activityStandards: ActivityStandards;
constructor(activityStandards: ActivityStandards) { constructor(activityStandards: ActivityStandards) {
// prepare data
this.activityStandards = activityStandards; this.activityStandards = activityStandards;
for (const activity of Object.keys(this.activityStandards) as Activity[]) { for (const activity of Object.keys(this.activityStandards) as Activity[]) {
// expand/compress
for (const standard of this.activityStandards[activity].standards) { for (const standard of this.activityStandards[activity].standards) {
const expandedLevels = this.expandLevels(standard.levels, 5); const expandedLevels = this.expandLevels(standard.levels, 5);
const compressedLevels = this.compressLevels(expandedLevels, 100); const compressedLevels = this.compressLevels(expandedLevels, 100);
standard.levels = compressedLevels; standard.levels = compressedLevels;
} }
// generate data
// const allGenerators = this.activityStandards[activity].metadata.generators;
//
// const ageGenerators = allGenerators.filter(g => g.metric === "age");
// for (const ageGenerator of ageGenerators) {
// }
//
// const weightGenerators = allGenerators.filter(g => g.metric === "weight");
// for (const weightGenerator of weightGenerators) {
// for (const gender of Object.values(Gender)) {
// const referenceWeight = gender === Gender.Male ?
// lbToKg(170) :
// lbToKg(137);
//
// const standardsByGender = this.byActivity(activity).byGender(gender).getAll();
// const ages = [...new Set(standardsByGender.map(s => s.metrics.age))];
//
// for (const age of ages) {
// const levelCalculator = new LevelCalculator(this);
// const referenceStandard =
// }
// }
// }
} }
} }
public byActivity(activity: Activity) { public byActivity(activity: Activity) {
const self = this; const self = this;
const state: { const state: Partial<Metrics> = {};
gender?: Gender;
age?: number;
weight?: number;
} = {};
const execMethods = { const execMethods = {
getAll: function() { getAll: function(): Standard[] {
let filtered = [...self.activityStandards[activity].standards]; let filtered = [...self.activityStandards[activity].standards];
if (state.gender) if (state.gender)
filtered = filtered.filter((s) => s.metrics.gender === state.gender); filtered = filtered.filter((s) => s.metrics.gender === state.gender);
@@ -276,10 +234,10 @@ export class Standards {
filtered = filtered.filter((s) => s.metrics.weight === state.weight); filtered = filtered.filter((s) => s.metrics.weight === state.weight);
return filtered; return filtered;
}, },
getOne: function() { getOne: function(): Standard {
return execMethods.getAll()[0]; return execMethods.getAll()[0];
}, },
getNearest(metric: DataMetric, target: number) { getNearest(metric: NumberMetric, target: number): { lower: Standard, upper: Standard } {
const standards = execMethods.getAll(); const standards = execMethods.getAll();
const lower = [...standards] const lower = [...standards]
.sort((a, b) => a.metrics[metric] - b.metrics[metric]) .sort((a, b) => a.metrics[metric] - b.metrics[metric])
@@ -289,7 +247,51 @@ export class Standards {
.sort((a, b) => a.metrics[metric] - b.metrics[metric]) .sort((a, b) => a.metrics[metric] - b.metrics[metric])
.find((s) => s.metrics[metric] >= target) ?? standards.at(-1)!; .find((s) => s.metrics[metric] >= target) ?? standards.at(-1)!;
return { lower, upper }; return { lower, upper };
},
};
const getInterpolated = (): Standard => {
const metrics: Metrics = state as Metrics;
const interpolateByWeight = (targetAge: number): Levels => {
const { lower: lowerAgeWeightStandard, upper: upperAgeWeightStandard } = this
.byActivity(activity)
.byGender(metrics.gender)
.byAge(targetAge)
.getNearest("weight", metrics.weight);
const weightLower = lowerAgeWeightStandard.metrics.weight;
const weightUpper = upperAgeWeightStandard.metrics.weight;
let weightRatio = weightUpper === weightLower ? 1 : (metrics.weight - weightLower) / (weightUpper - weightLower);
weightRatio = Math.max(0, Math.min(1, weightRatio));
return this.interpolateLevels(lowerAgeWeightStandard.levels, upperAgeWeightStandard.levels, weightRatio);
} }
// Find nearest age standards
const { lower: lowerAgeStandard, upper: upperAgeStandard } = this
.byActivity(activity)
.byGender(metrics.gender)
.getNearest("age", metrics.age);
const ageLower = lowerAgeStandard.metrics.age;
const ageUpper = upperAgeStandard.metrics.age;
// Interpolate by age and weight
let ageRatio = ageUpper === ageLower ? 1 : (metrics.age - ageLower) / (ageUpper - ageLower);
ageRatio = Math.max(0, Math.min(1, ageRatio));
const interpolatedLevels = this.interpolateLevels(
interpolateByWeight(ageLower),
interpolateByWeight(ageUpper),
ageRatio
);
return {
metrics,
levels: interpolatedLevels
};
}; };
const byGender = (gender: Gender) => { const byGender = (gender: Gender) => {
@@ -304,10 +306,15 @@ export class Standards {
const byWeight = (weight: number) => { const byWeight = (weight: number) => {
state.weight = weight; state.weight = weight;
return { ...execMethods }; return { ...execMethods, getInterpolated };
} }
return { byGender, ...execMethods } const byMetrics = (metrics: Metrics) => {
Object.assign(state, metrics);
return { ...execMethods, getInterpolated };
}
return { byGender, byMetrics, ...execMethods }
} }
getActivityAttribute(activity: Activity): Attribute { getActivityAttribute(activity: Activity): Attribute {
@@ -377,4 +384,22 @@ export class Standards {
return compressedLevels; return compressedLevels;
} }
private interpolateLevels(
lower: Levels,
upper: Levels,
ratio: number,
): Levels {
if (Object.keys(lower).length !== Object.keys(upper).length)
throw new Error("Cannot interpolate between varying number of levels");
const lerp = (lvl: string) => (lower[lvl] as number) + ((upper[lvl] as number) - (lower[lvl] as number)) * ratio;
const interpolatedLevels = {};
for (const lvl in lower) {
interpolatedLevels[lvl] = lerp(lvl);
}
return interpolatedLevels;
}
} }
+6 -6
View File
@@ -2,7 +2,7 @@
"BackSquat": { "BackSquat": {
"metadata": { "metadata": {
"attribute": "Strength", "attribute": "Strength",
"standardsGeneratorOptions": [] "generators": []
}, },
"standards": [ "standards": [
{ {
@@ -1578,7 +1578,7 @@
"Deadlift": { "Deadlift": {
"metadata": { "metadata": {
"attribute": "Strength", "attribute": "Strength",
"standardsGeneratorOptions": [] "generators": []
}, },
"standards": [ "standards": [
{ {
@@ -3154,7 +3154,7 @@
"BenchPress": { "BenchPress": {
"metadata": { "metadata": {
"attribute": "Strength", "attribute": "Strength",
"standardsGeneratorOptions": [] "generators": []
}, },
"standards": [ "standards": [
{ {
@@ -4730,7 +4730,7 @@
"Run": { "Run": {
"metadata": { "metadata": {
"attribute": "Endurance", "attribute": "Endurance",
"standardsGeneratorOptions": [ "generators": [
{ {
"metric": "weight", "metric": "weight",
"spread": 50, "spread": 50,
@@ -5221,7 +5221,7 @@
"BroadJump": { "BroadJump": {
"metadata": { "metadata": {
"attribute": "Power", "attribute": "Power",
"standardsGeneratorOptions": [] "generators": []
}, },
"standards": [ "standards": [
{ {
@@ -5257,7 +5257,7 @@
"ConeDrill": { "ConeDrill": {
"metadata": { "metadata": {
"attribute": "Agility", "attribute": "Agility",
"standardsGeneratorOptions": [] "generators": []
}, },
"standards": [ "standards": [
{ {