Support sparse metric aggregation
This commit is contained in:
1 parent
d4e7cb6e8b
commit
77ba93b94a
4 files changed
+106
-21
No files matched your search
@@ -105,6 +105,52 @@ describe('FeatureExtractorService', () => {
|
||||
expect(feature.confidence).toBe(1);
|
||||
});
|
||||
|
||||
it('leaves registry metrics without raw observations out of normalized values', () => {
|
||||
const actualShared = jest.requireActual<
|
||||
typeof import('@persona-lenz/shared')
|
||||
>('@persona-lenz/shared');
|
||||
const metrics = [
|
||||
...actualShared.METRICS,
|
||||
{
|
||||
id: 'futureTimelineMetric',
|
||||
label: 'Future timeline metric',
|
||||
direction: 'higherIsBetter' as const,
|
||||
kind: 'scalar' as const,
|
||||
includeInVector: true,
|
||||
},
|
||||
];
|
||||
|
||||
let feature:
|
||||
| ReturnType<FeatureExtractorService['extractPlayerMatchFeature']>
|
||||
| undefined;
|
||||
|
||||
jest.isolateModules(() => {
|
||||
jest.doMock('@persona-lenz/shared', () => ({
|
||||
...actualShared,
|
||||
FEATURE_SCHEMA_VERSION: 99,
|
||||
METRICS: metrics,
|
||||
VECTOR_METRICS: metrics.filter((metric) => metric.includeInVector),
|
||||
}));
|
||||
const { FeatureExtractorService: MockedFeatureExtractorService } =
|
||||
jest.requireActual<typeof import('./feature-extractor.service')>(
|
||||
'./feature-extractor.service',
|
||||
);
|
||||
|
||||
feature = new MockedFeatureExtractorService().extractPlayerMatchFeature(
|
||||
createMatchPayload(),
|
||||
'target-puuid',
|
||||
);
|
||||
});
|
||||
|
||||
jest.dontMock('@persona-lenz/shared');
|
||||
|
||||
expect(feature?.features.futureTimelineMetric).toBeUndefined();
|
||||
expect(feature?.normalized.futureTimelineMetric).toBeUndefined();
|
||||
expect(feature?.vector).toHaveLength(metrics.length);
|
||||
expect(feature?.vector.at(-1)).toBe(0);
|
||||
expect(feature?.featureSchemaVersion).toBe(99);
|
||||
});
|
||||
|
||||
it('falls back to manual calculations when challenges are missing', () => {
|
||||
const payload = createMatchPayload({
|
||||
participants: [
|
||||
|
||||
@@ -65,6 +65,12 @@ function asNumber(value: unknown, fallback = 0): number {
|
||||
return typeof value === 'number' && Number.isFinite(value) ? value : fallback;
|
||||
}
|
||||
|
||||
function asFiniteMetricValue(value: number | undefined): number | undefined {
|
||||
return typeof value === 'number' && Number.isFinite(value)
|
||||
? value
|
||||
: undefined;
|
||||
}
|
||||
|
||||
function fromChallenge(
|
||||
challenges: Record<string, unknown>,
|
||||
key: string,
|
||||
@@ -210,10 +216,12 @@ export class FeatureExtractorService {
|
||||
const laneOpponent = resolveLaneOpponent(participant, participants, role);
|
||||
const normalizationRole = resolveNormalizationRole(role);
|
||||
const normalized = Object.fromEntries(
|
||||
METRICS.map((metric) => [
|
||||
metric.id,
|
||||
normalizeMetric(metric, normalizationRole, features[metric.id] ?? 0),
|
||||
]),
|
||||
METRICS.flatMap((metric) => {
|
||||
const rawValue = asFiniteMetricValue(features[metric.id]);
|
||||
return rawValue === undefined
|
||||
? []
|
||||
: [[metric.id, normalizeMetric(metric, normalizationRole, rawValue)]];
|
||||
}),
|
||||
);
|
||||
|
||||
return {
|
||||
@@ -228,7 +236,7 @@ export class FeatureExtractorService {
|
||||
patch: resolvePatch(info.gameVersion),
|
||||
features,
|
||||
normalized,
|
||||
vector: VECTOR_METRICS.map((metric) => normalized[metric.id]),
|
||||
vector: VECTOR_METRICS.map((metric) => normalized[metric.id] ?? 0),
|
||||
featureSchemaVersion: FEATURE_SCHEMA_VERSION,
|
||||
normalizationMethod: 'registry-baseline',
|
||||
confidence: this.resolveConfidence(role, gameDurationSec, laneOpponent),
|
||||
|
||||
@@ -158,6 +158,33 @@ describe('ProfileBuilderService', () => {
|
||||
expect(profile.rareEventRate).toBeCloseTo(3 / 5);
|
||||
});
|
||||
|
||||
it('aggregates scalar metrics only from features with observed normalized values', () => {
|
||||
const metrics: MetricDefinition[] = [
|
||||
{
|
||||
id: 'observedScalar',
|
||||
label: 'Observed scalar',
|
||||
direction: 'higherIsBetter',
|
||||
kind: 'scalar',
|
||||
includeInVector: true,
|
||||
},
|
||||
];
|
||||
const firstFeature = createFeature({
|
||||
normalized: {},
|
||||
confidence: 1,
|
||||
});
|
||||
const secondFeature = createFeature({
|
||||
normalized: { observedScalar: 0.4 },
|
||||
confidence: 0.5,
|
||||
});
|
||||
|
||||
const profile = buildProfileMetricValues(
|
||||
[firstFeature, secondFeature],
|
||||
metrics,
|
||||
);
|
||||
|
||||
expect(profile.observedScalar).toBe(0.4);
|
||||
});
|
||||
|
||||
it('builds profile vectors from included metrics while retaining insight metrics', () => {
|
||||
const actualShared = jest.requireActual<
|
||||
typeof import('@persona-lenz/shared')
|
||||
|
||||
@@ -37,16 +37,25 @@ function toFiniteMetricValue(value: number | undefined): number {
|
||||
function aggregateScalarMetric(
|
||||
features: readonly ProfileSourceFeature[],
|
||||
metricId: string,
|
||||
safeTotalConfidence: number,
|
||||
): number {
|
||||
return (
|
||||
features.reduce(
|
||||
(sum, feature) =>
|
||||
sum +
|
||||
toFiniteMetricValue(feature.normalized[metricId]) * feature.confidence,
|
||||
0,
|
||||
) / safeTotalConfidence
|
||||
);
|
||||
let weightedSum = 0;
|
||||
let confidenceSum = 0;
|
||||
|
||||
for (const feature of features) {
|
||||
const metricValue = feature.normalized[metricId];
|
||||
if (typeof metricValue !== 'number' || !Number.isFinite(metricValue)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
weightedSum += metricValue * feature.confidence;
|
||||
confidenceSum += feature.confidence;
|
||||
}
|
||||
|
||||
if (confidenceSum <= 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return weightedSum / confidenceSum;
|
||||
}
|
||||
|
||||
function aggregateRatioMetric(
|
||||
@@ -76,18 +85,13 @@ export function buildProfileMetricValues(
|
||||
features: readonly ProfileSourceFeature[],
|
||||
metrics: readonly MetricDefinition[],
|
||||
): Record<string, number> {
|
||||
const totalConfidence = features.reduce(
|
||||
(sum, feature) => sum + feature.confidence,
|
||||
0,
|
||||
);
|
||||
const safeTotalConfidence = Math.max(totalConfidence, Number.EPSILON);
|
||||
const profile: Record<string, number> = {};
|
||||
|
||||
for (const metric of metrics) {
|
||||
profile[metric.id] =
|
||||
metric.kind === 'ratio'
|
||||
? aggregateRatioMetric(features, metric.id)
|
||||
: aggregateScalarMetric(features, metric.id, safeTotalConfidence);
|
||||
: aggregateScalarMetric(features, metric.id);
|
||||
}
|
||||
|
||||
return profile;
|
||||
@@ -161,7 +165,7 @@ export class ProfileBuilderService {
|
||||
left.championId - right.championId,
|
||||
),
|
||||
profile,
|
||||
vector: VECTOR_METRICS.map((metric) => profile[metric.id]),
|
||||
vector: VECTOR_METRICS.map((metric) => profile[metric.id] ?? 0),
|
||||
featureSchemaVersion: FEATURE_SCHEMA_VERSION,
|
||||
normalizationMethod: 'registry-baseline',
|
||||
confidence: averageConfidence * sampleSizeConfidenceMultiplier,
|
||||
|
||||
Reference in new issue
Block a user