Last active
February 8, 2023 20:37
-
-
Save GalvinGao/835e1544c3a11e2f0d9eb21eaddc00db to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"use strict"; | |
// ==UserScript== | |
// @name Pulsar Auto Labeler | |
// @namespace https://pulsars.nanosta.rs/* | |
// @version 0.4 | |
// @description Automatically label pulsars as REJECT based on certain criteria | |
// @author Galvin Gao | |
// @match https://pulsars.nanosta.rs/* | |
// @icon https://www.google.com/s2/favicons?sz=64&domain=pulsars.nanosta.rs | |
// @grant none | |
// @updateURL https://gist.github.com/GalvinGao/835e1544c3a11e2f0d9eb21eaddc00db/raw/0ce462cd248c1690e65ba58468d2f331c71013f6/pulsar-autolabeler.user.js | |
// @downloadURL https://gist.github.com/GalvinGao/835e1544c3a11e2f0d9eb21eaddc00db/raw/0ce462cd248c1690e65ba58468d2f331c71013f6/pulsar-autolabeler.user.js | |
// ==/UserScript== | |
var OUTCOME; | |
(function (OUTCOME) { | |
OUTCOME[OUTCOME["REJECT"] = 0] = "REJECT"; | |
OUTCOME[OUTCOME["NEW_CANDIDATE"] = 1] = "NEW_CANDIDATE"; | |
OUTCOME[OUTCOME["KNOWN"] = 2] = "KNOWN"; | |
OUTCOME[OUTCOME["UNKNOWN"] = 3] = "UNKNOWN"; | |
})(OUTCOME || (OUTCOME = {})); | |
const $ = document.querySelector.bind(document); | |
const $$ = document.querySelectorAll.bind(document); | |
const determine = () => { | |
const determinators = [ | |
{ | |
id: "dm_tooLow", | |
name: "DM: too low", | |
fn: () => { | |
const THRESHOLD = 0.01; | |
const dm = $("#ocr_dm"); | |
if (!dm) { | |
console.warn("[dm_tooLow] #ocr_dm not found"); | |
return; | |
} | |
const dmVal = dm.innerHTML; | |
console.debug("[dm_tooLow] dmVal", dmVal); | |
const dmFloat = parseFloat(dmVal); | |
console.debug("[dm_tooLow] dmFloat", dmFloat); | |
return dmFloat < THRESHOLD ? -1 : 0; | |
}, | |
}, | |
{ | |
id: "period_artificial", | |
name: "Period: artificial", | |
fn: () => { | |
const WHOLE_TOLERANCE = 0.01; // absolute tolerance | |
const period = $("#ocr_period"); | |
if (!period) { | |
console.warn("[period_artificial] #ocr_period not found"); | |
return; | |
} | |
const periodVal = period.innerHTML; | |
console.debug("[period_artificial] periodVal", periodVal); | |
const periodFloat = parseFloat(periodVal); | |
console.debug("[period_artificial] periodFloat", periodFloat); | |
const whole = Math.round(periodFloat); | |
// first check: `whole` is divisible by 10 | |
if (whole % 10 !== 0) { | |
return 0; | |
} | |
// second check: `whole` is within tolerance | |
const diff = Math.abs(whole - periodFloat); | |
if (diff > WHOLE_TOLERANCE) { | |
return 0; | |
} | |
return -1; | |
}, | |
}, | |
]; | |
console.info("[PulsarAutoLabeler] evaluating", determinators.length, "rules"); | |
return determinators.map((determinator) => { | |
let evaluation = determinator.fn(); | |
console.info("[PulsarAutoLabeler] evaluated rule", determinator.name, ":", evaluation); | |
if (evaluation === undefined || typeof evaluation !== "number") { | |
console.warn("[PulsarAutoLabeler] rule evaluation received unexpected result"); | |
evaluation = 0; | |
} | |
return Object.assign(Object.assign({}, determinator), { evaluation }); | |
}); | |
}; | |
const addOverlay = (evaluation) => { | |
const overlayHTML = `<div | |
id="overlay" | |
style=" | |
position: fixed; | |
top: 0; | |
left: 0; | |
width: 100%; | |
height: 100%; | |
background: | |
repeating-linear-gradient(-45deg, rgba(0,0,0,0.5), rgba(0,0,0,0.5) 50px, rgba(0,0,0,0.3) 50px, rgba(0,0,0,0.3) 100px); | |
z-index: 9999; | |
" | |
> | |
<div style="display: flex; justify-content: center; align-items: center; height: 100%;"> | |
<div style="background: white; padding: 20px; border-radius: 10px;"> | |
<h1 style="font-size: 2em; margin: 0;">PulsarAutoLabeler</h1> | |
<p style="margin: 0;">This pulsar has been automatically labeled as <strong>REJECT</strong>:</p> | |
<ul style="font-size: 1.5em;"> | |
${evaluation | |
.filter((evaluationItem) => evaluationItem.evaluation === -1) | |
.map((evaluationItem) => `<li>${evaluationItem.name}</li>`) | |
.join("")} | |
</ul> | |
</div> | |
</div> | |
</div> | |
`; | |
const overlay = document.createElement("div"); | |
overlay.innerHTML = overlayHTML; | |
document.body.appendChild(overlay); | |
}; | |
const label = (evaluation) => { | |
const finalScore = evaluation.reduce((acc, curr) => { var _a; return acc + ((_a = curr === null || curr === void 0 ? void 0 : curr.evaluation) !== null && _a !== void 0 ? _a : 0); }, 0); | |
console.info("[PulsarAutoLabeler] final score:", finalScore); | |
let outcome = OUTCOME.UNKNOWN; | |
if (finalScore < 0) { | |
outcome = OUTCOME.REJECT; | |
} | |
if (outcome === OUTCOME.REJECT) { | |
const button = $("#btn_rfi"); | |
if (!button) { | |
console.warn("[label] #btn_rfi not found"); | |
return; | |
} | |
addOverlay(evaluation); | |
button.click(); | |
} | |
}; | |
// const openNextBatch = () => { | |
// if (window.__PAL_I === undefined) { | |
// window.__PAL_I = 0; | |
// } | |
// }; | |
const main = () => { | |
const inViewerPage = !!$("#btn_rfi"); | |
if (!inViewerPage) { | |
console.info("[PulsarAutoLabeler] not in viewer page; skipping"); | |
return; | |
} | |
const alreadyLabeled = $("#latest_rating_results .alert"); | |
if (alreadyLabeled) { | |
console.info("[PulsarAutoLabeler] already labeled; skipping"); | |
return; | |
} | |
const evaluations = determine(); | |
label(evaluations); | |
}; | |
main(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment