From 6f21054aff813cab731c47e2cbc328cae5ff4ed0 Mon Sep 17 00:00:00 2001 From: zkldi <20380519+zkldi@users.noreply.github.com> Date: Sun, 21 Aug 2022 18:07:56 +0100 Subject: [PATCH] feat: improve sgl output --- sieglinde/.gitignore | 2 +- sieglinde/package.json | 2 +- sieglinde/src/calc/v0.ts | 61 -------------------------------- sieglinde/src/main.ts | 76 +++++++++++++++++----------------------- sieglinde/src/types.ts | 1 + 5 files changed, 35 insertions(+), 107 deletions(-) delete mode 100644 sieglinde/src/calc/v0.ts diff --git a/sieglinde/.gitignore b/sieglinde/.gitignore index adc130484..b96b8595b 100644 --- a/sieglinde/.gitignore +++ b/sieglinde/.gitignore @@ -1,2 +1,2 @@ -out.* +output/ src/cache \ No newline at end of file diff --git a/sieglinde/package.json b/sieglinde/package.json index 886cfab91..c9212afab 100644 --- a/sieglinde/package.json +++ b/sieglinde/package.json @@ -5,7 +5,7 @@ "main": "index.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1", - "calc-v1": "ts-node src/main.ts -v 1 -o out.json" + "calc-v1": "ts-node src/main.ts" }, "author": "zkldi", "license": "MIT", diff --git a/sieglinde/src/calc/v0.ts b/sieglinde/src/calc/v0.ts deleted file mode 100644 index 3a4e08d60..000000000 --- a/sieglinde/src/calc/v0.ts +++ /dev/null @@ -1,61 +0,0 @@ -/* eslint-disable no-await-in-loop */ -import logger from "../logger"; -import { GetBaseline, GetFString, GetScoresForMD5, GetSigmoidalValue, Mean } from "../util"; -import type { TableRes } from "../fetch-tables"; -import type { CalcReturns } from "../types"; - -/** - * Sieglinde V0 calc. This just reads out hardcoded values for each table, with no fuzzing. - */ -export default function SieglindeV0Calc(tableInfo: TableRes): Array { - const dataset = []; - - for (const chart of tableInfo.charts) { - const baseLine = GetBaseline(tableInfo.table, chart.level); - - if (baseLine === null) { - logger.info(`Skipped level ${tableInfo.table.name} ${chart.level}`); - continue; - } - - const str = GetFString(tableInfo.table, chart); - - dataset.push({ - title: chart.title, - md5: chart.md5, - ecRate: 0, - hcRate: 0, - scoreCount: 0, - baseLine, - str, - }); - } - - const returns = []; - - for (const data of dataset) { - const confidence = GetSigmoidalValue(data.scoreCount / 100); - - const ecDiff = 0; // groupAverageECRate - data.ecRate; - const hcDiff = 0; // groupAverageHCRate - data.hcRate; - - const ecValue = Math.max(0, data.baseLine - ecDiff * confidence); - const hcValue = Math.max(0, data.baseLine - hcDiff * confidence); - - const ecPrefix = ecValue < 12 ? "☆" : "🟊"; - const hcPrefix = hcValue < 12 ? "☆" : "🟊"; - - returns.push({ - md5: data.md5, - title: data.title, - ec: ecValue, - ecStr: `${ecPrefix}${ecValue < 12 ? ecValue : ecValue - 12}`, - hc: hcValue, - hcStr: `${hcPrefix}${hcValue < 12 ? hcValue : hcValue - 12}`, - ecMetric: 0, - hcMetric: 0, - }); - } - - return returns; -} diff --git a/sieglinde/src/main.ts b/sieglinde/src/main.ts index 935fd5f70..ce15bd056 100644 --- a/sieglinde/src/main.ts +++ b/sieglinde/src/main.ts @@ -1,52 +1,17 @@ -import SieglindeV0Calc from "./calc/v0"; import GetTableData from "./fetch-tables"; import logger from "./logger"; import SieglindeV1Calc from "calc/v1-jiminp"; -import { Command } from "commander"; import fs from "fs"; +import path from "path"; +import type { CalcReturns } from "types"; if (require.main !== module) { logger.error(`The script main.ts must be invoked directly!`); process.exit(-1); } -const program = new Command(); - -program - .option("-v, --version ") - .option("-o, --out "); - -program.parse(process.argv); -const options: { - out?: string; - version?: string; -} = program.opts(); - -if (!options.out) { - logger.error(`Need to provide an --out parameter for output!`); - process.exit(-1); -} - -const version = Number(options.version); - -if (Number.isNaN(version)) { - throw new Error(`Unknown sieglinde version ${version}.`); -} - -function WriteOut(data: string) { - fs.writeFileSync(options.out!, data); -} - void (async () => { - let calcFn; - - if (version === 0) { - calcFn = SieglindeV0Calc; - } else if (version === 1) { - calcFn = SieglindeV1Calc; - } else { - throw new Error(`Unknown sieglinde version ${version}.`); - } + const calcFn = SieglindeV1Calc; fs.mkdirSync(`${__dirname}/cache`, { recursive: true }); @@ -54,10 +19,10 @@ void (async () => { logger.info(`Starting...`); - const calcData = []; - let i = 1; + fs.mkdirSync(path.join(__dirname, "output"), { recursive: true }); + for (const table of tableInfo) { logger.info(`Running for table ${table.table.name}. ${i}/${tableInfo.length}`); @@ -65,14 +30,37 @@ void (async () => { // the lr2ir runs off of a toaster which attempts to gut you if you make more than one // request a second. // eslint-disable-next-line no-await-in-loop - calcData.push(await calcFn(table)); + const data = await calcFn(table); - WriteOut(JSON.stringify(calcData.flat(1))); + fs.writeFileSync( + path.join(__dirname, `output/${table.table.name}.json`), + JSON.stringify(data) + ); + + fs.writeFileSync(path.join(__dirname, `output/${table.table.name}.csv`), toCSV(data)); i++; } - WriteOut(JSON.stringify(calcData.flat(1))); - logger.info(`Finished!`); })(); + +function toCSV(calcData: Array) { + let str = `"md5","title","baseLevel","ec","ecStr","hc","hcStr"\n`; + + for (const data of calcData) { + const row = [ + data.md5, + data.title.replace(/"/gu, '""'), + data.baseLevel, + data.ec, + data.ecStr, + data.hc, + data.hcStr, + ]; + + str = `${str}"${row.join('","')}"\n`; + } + + return str; +} diff --git a/sieglinde/src/types.ts b/sieglinde/src/types.ts index f37e73732..a3a901cd2 100644 --- a/sieglinde/src/types.ts +++ b/sieglinde/src/types.ts @@ -23,6 +23,7 @@ export interface CalcReturns { hc: number; ecStr: string; hcStr: string; + baseLevel: string; // Internal values for ec/hc metrics; in the case of the v1 calc, these are // sigma values.