Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CHE_201_average_moves_and_distances_inaccurate #58

Merged
merged 5 commits into from
Jun 13, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 65 additions & 15 deletions src/aggregate_analysis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ async function aggregateResults(filePath: string) {
let totalDistByPiece = {};
let avgDistByPiece = {};

// objects to ensure the averages for distances/piece level moves are calculated correctly (doesn't include games with analysis errors)
let numGamesAnalyzedPieceLevelMoveInfoMetric = { numGames: 0 };
let numGamesAnalyzedMoveDistanceMetric = { numGames: 0 };

// moves metrics
let gameMostMoves = [];
let gameMostMovesNumMoves = 0;
Expand Down Expand Up @@ -102,6 +106,34 @@ async function aggregateResults(filePath: string) {
analysisCounter++;

const thisAnalysisGamesAnalyzed = analysis['Number of games analyzed'];
// Calculate the number of games not skipped for PieceLevelMoveInfoMetric
for (const key in analysis['PieceLevelMoveInfoMetric'][
'totalMovesByPiece'
]) {
const totalMoves =
analysis['PieceLevelMoveInfoMetric']['totalMovesByPiece'][key].numMoves;
const avgMoves =
analysis['PieceLevelMoveInfoMetric']['averagesMap'][key].avgMoves;
if (totalMoves > 0 && avgMoves > 0) {
numGamesAnalyzedPieceLevelMoveInfoMetric.numGames += Math.round(
totalMoves / avgMoves
);
break; // Exit the loop once a valid key is found
}
}
// Calculate the number of games not skipped for numGamesAnalyzedMoveDistanceMetric
for (const key in analysis['MoveDistanceMetric']['totalDistancesByPiece']) {
const totalMoves =
analysis['MoveDistanceMetric']['totalDistancesByPiece'][key].numMoves;
const avgMoves =
analysis['MoveDistanceMetric']['avgDistancesByPiece'][key].avgMoves;
if (totalMoves > 0 && avgMoves > 0) {
numGamesAnalyzedMoveDistanceMetric.numGames += Math.round(
totalMoves / avgMoves
);
break; // Exit the loop once a valid key is found
}
}

// METADATA METRICS
// ratings weighted average calculations
Expand Down Expand Up @@ -205,7 +237,8 @@ async function aggregateResults(filePath: string) {
totalCollectiveDistGames,
gameMaxCollectiveDist,
totalDistByPiece,
avgDistByPiece
avgDistByPiece,
numGamesAnalyzedMoveDistanceMetric
));

// moves metrics
Expand Down Expand Up @@ -279,7 +312,8 @@ async function aggregateResults(filePath: string) {
averageNumMovesByPiece,
totalGamesAnalyzed,
highestAverageMoves,
pieceHighestAverageMoves
pieceHighestAverageMoves,
numGamesAnalyzedPieceLevelMoveInfoMetric
));

// LOGS FOR THE ENTIRE SET
Expand Down Expand Up @@ -586,17 +620,26 @@ function avgNumMoves(
averageNumMovesByPiece: {},
totalGamesAnalyzed: number,
highestAverageMoves: number,
pieceHighestAverageMoves: any[]
pieceHighestAverageMoves: any[],
numGamesAnalyzedPieceLevelMoveInfoMetric: { numGames: number }
) {
for (const uas in totalMovesByPiece) {
if (!averageNumMovesByPiece[uas]) {
averageNumMovesByPiece[uas] = {
avgNumMoves: totalMovesByPiece[uas].numMoves / totalGamesAnalyzed,
};
if (numGamesAnalyzedPieceLevelMoveInfoMetric.numGames > 0) {
averageNumMovesByPiece[uas] = {
avgNumMoves:
totalMovesByPiece[uas].numMoves /
numGamesAnalyzedPieceLevelMoveInfoMetric.numGames,
};
} else {
averageNumMovesByPiece[uas] = {
BuggerBugs marked this conversation as resolved.
Show resolved Hide resolved
avgNumMoves: 0,
};
}
}
}

for (const uas in averageNumMovesByPiece) {
for (const uas in totalMovesByPiece) {
if (averageNumMovesByPiece[uas].avgNumMoves > highestAverageMoves) {
highestAverageMoves = averageNumMovesByPiece[uas].avgNumMoves;
pieceHighestAverageMoves = [uas as UASymbol];
Expand Down Expand Up @@ -743,7 +786,8 @@ function aggregateDistanceMetrics(
totalCollectiveDistGames: number,
gameMaxCollectiveDist: { distance: number; games: any[] },
totalDistByPiece: {},
avgDistByPiece: {}
avgDistByPiece: {},
numGamesAnalyzedMoveDistanceMetric: { numGames: number }
) {
const thisMaxAvgDistance = analysis['MoveDistanceMetric']['maxAvgDistance'];
const thisPieceMaxAvgDistance =
Expand Down Expand Up @@ -808,16 +852,22 @@ function aggregateDistanceMetrics(
totalDistByPiece[uas].distance += thisTotalDistByPiece[uas].distance;
}

const thisAvgDistByPiece =
analysis['MoveDistanceMetric']['avgDistancesByPiece'];
for (const uas in thisAvgDistByPiece) {
for (const uas in totalDistByPiece) {
if (!avgDistByPiece[uas]) {
avgDistByPiece[uas] = {
avgDistance: thisAvgDistByPiece[uas].avgDistance,
};
if (numGamesAnalyzedMoveDistanceMetric.numGames > 0) {
avgDistByPiece[uas] = {
avgDistance:
totalDistByPiece[uas].distance /
numGamesAnalyzedMoveDistanceMetric.numGames,
};
} else {
avgDistByPiece[uas] = {
avgDistance: 0,
};
}
}
avgDistByPiece[uas].avgDistance += thisAvgDistByPiece[uas].avgDistance;
}

return {
maxAvgDistance,
pieceMaxAvgDist,
Expand Down
Loading