Skip to content

Commit

Permalink
hacky latex.js changes
Browse files Browse the repository at this point in the history
  • Loading branch information
oflatt committed Feb 7, 2025
1 parent 13cdf16 commit 60ab97b
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 7 deletions.
4 changes: 3 additions & 1 deletion infra/nightly-resources/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,9 @@ function addTableTo(element, data, title) {
const copyMacrosButton = document.createElement("button");
copyMacrosButton.innerText = "Copy Latex Macros";
copyMacrosButton.onclick = () => {
const macros = jsonToLatexMacros(data, "runMethod");
const macros = nestedJsonToLatexMacros(data, "name", "executions", "runMethod");
console.log("macros");
console.log(macros);
navigator.clipboard.writeText(macros);
};

Expand Down
61 changes: 55 additions & 6 deletions infra/nightly-resources/latex.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,35 @@
// some hacky functions to convert json to latex tables and macros
// expects json in the same format as `table.js`
// when a string is valid html, gets the text context of the string to display it

// format a number to 2 decimal places as a string
// but only if it is a number
function formatNumber(num) {
function formatLatexTableValue(num) {
if (typeof num === "number") {
return num.toFixed(2);
} else if (typeof num == "string") {
// check if the string is valid html- if so, get the text content
if (num.includes("<")) {
var div = document.createElement("div");
div.innerHTML = num;
return div.textContent;
}
return num;
} else if (typeof num == "object") {
// expect it to have the form {class: ..., value: ...}
// treat it as just value.value
if (
Object.keys(num).length === 2 &&
num.hasOwnProperty("value") &&
num.hasOwnProperty("class")
) {
return num.value;
} else {
throw new Error("Invalid object format");
}
} else {
throw new Error("Invalid type for value");
}
return num;
}

function jsonHeaders(json) {
Expand Down Expand Up @@ -71,7 +96,7 @@ function jsonToLatexTable(json) {
for (var i = 0; i < rows; i++) {
for (var j = 0; j < cols; j++) {
var value = json[i][headers[j]];
res += formatNumber(value);
res += formatLatexTableValue(value);
if (j < cols - 1) {
res += " & ";
}
Expand All @@ -86,6 +111,12 @@ function jsonToLatexTable(json) {

// change dashes to underscores
function convertStringToValidLatexVar(str) {
// if the str is valid html, get the text content
if (str.includes("<")) {
var div = document.createElement("div");
div.innerHTML = str;
str = div.textContent
}
return str.replace(/-/g, "_");
}

Expand All @@ -95,7 +126,8 @@ function convertStringToValidLatexVar(str) {
// example element: { geoMeanNormalized : "0.619", meanEggccCompileTimeSecs : "0.001", meanLlvmCompileTimeSecs : "0.447",
// runMethod : "llvm-O1-O0" }
// rowIndex is the name of the header of the left column, in this case "runMethod"
function jsonToLatexMacros(json, rowIndex) {
function jsonToLatexMacros(json, rowIndex, prefix) {
var prefix = prefix || "";
var names = [];
var res = "";

Expand All @@ -106,12 +138,12 @@ function jsonToLatexMacros(json, rowIndex) {
// for each header, col pair create a macro
for (var i = 0; i < cols.length; i++) {
for (var j = 0; j < headers.length; j++) {
var name = `${convertStringToValidLatexVar(cols[i])}${convertStringToValidLatexVar(headers[j])}`;
var name = `${prefix}${convertStringToValidLatexVar(cols[i])}${convertStringToValidLatexVar(headers[j])}`;
res +=
"\\newcommand{\\" +
name +
"}{" +
formatNumber(json[i][headers[j]]) +
formatLatexTableValue(json[i][headers[j]]) +
"}\n";

names.push(name);
Expand All @@ -126,3 +158,20 @@ function jsonToLatexMacros(json, rowIndex) {

return res;
}


// given a json like the following:
/// [{ "name": "raytrace", "executions": { data: other_table} }] ...
/// Call jsonToLatexMacros on the other_table for each row index with the prefix name
function nestedJsonToLatexMacros(json, rowIndex, tableIndex, nestedRowIndex) {
var res = "";

for (var i = 0; i < json.length; i++) {
var name = convertStringToValidLatexVar(json[i][rowIndex]);
var table = json[i][tableIndex].data;

res += jsonToLatexMacros(table, nestedRowIndex, name);
}

return res;
}

0 comments on commit 60ab97b

Please sign in to comment.