Skip to content

Commit

Permalink
CHE_201_average_moves_and_distances_inaccurate (#58)
Browse files Browse the repository at this point in the history
* updated average moves and distances to account for skipped games

* minor changes to quotes

* small changes

* Updated changes according to review

---------

Co-authored-by: Benny Rubanov <106097466+bennyrubanov@users.noreply.github.com>
  • Loading branch information
BuggerBugs and bennyrubanov authored Jun 13, 2024
1 parent cda66ce commit f1ba4cd
Showing 1 changed file with 65 additions and 15 deletions.
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] = {
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

0 comments on commit f1ba4cd

Please sign in to comment.