Skip to content

Instantly share code, notes, and snippets.

@heikowissler
Created March 9, 2021 11:19
Show Gist options
  • Save heikowissler/95f0a3758a6a7a1ed8b260cbeed91fd8 to your computer and use it in GitHub Desktop.
Save heikowissler/95f0a3758a6a7a1ed8b260cbeed91fd8 to your computer and use it in GitHub Desktop.
window.webknossos.apiReady(3).then(async(api) => {
const welcomeMessage =
`edgeMode (version 17.02.2021)
[6] Adds "Yes" to current tree name
[5] Adds "Maybe" to current tree name
[4] Adds "No" to current tree name`;
/*
Edge Annotation Tool
Easily label edge tracings with a single key stroke.
Note: User must create a new node for this script to work.
Written by
Alessandro Motta <[email protected]>
Modified by
Sahil Loomba <[email protected]>
Thanks to
Anselm Weber for valuable input
*/
let allTreeIds;
let isTimeoutExceeded = false;
let currentTimeout;
console.log("starting spine task");
api.utils.showToast("info", 'Press "7" to automatically place a comment for unsolved tracing (means: mistracing, glia, unsure) and "9" to finish and get next task', 3000);
async function actionOverwrite(store, call, action) {
if (!isTimeoutExceeded) {
call(action);
}
}
api.utils.registerOverwrite("CREATE_NODE", actionOverwrite);
function resetTimeout() {
if (currentTimeout != null) {
clearTimeout(currentTimeout);
}
isTimeoutExceeded = false;
currentTimeout = setTimeout(() => {
api.utils.showToast("info", 'Time out. Please go to next tree.', 3000);
isTimeoutExceeded = true;
currentTimeout = null;
}, 35000);
}
function updateTree(treeId, nameSuffix, color, treeVisibility, groupId) {
let oldName = api.tracing.getTreeName(treeId);
let newName = oldName + " " + nameSuffix;
api.tracing.setTreeName(newName, treeId);
if (treeVisibility === undefined) { treeVisibility = false; }
api.tracing.setTreeVisibility(treeId, treeVisibility);
if (groupId !== undefined) {
api.tracing.setTreeGroup(treeId, groupId);
}
}
function goToNextTree() {
// Find next unlabled tree
let treeId = allTreeIds.find(curTreeId => {
let curTreeName = api.tracing.getTreeName(curTreeId);
return (curTreeName.match(/.*\(\w+\)$/) === null);
});
if (treeId === undefined) {
alert("Congratulation! It looks like you're done!");
}
api.tracing.setActiveTree(treeId);
allTreeIds.forEach(id => api.tracing.setTreeVisibility(id, id === treeId));
resetTimeout();
}
function doTree(label, color, treeVisibility, groupId) {
let suffix = "(" + label + ")";
let treeId = api.tracing.getActiveTreeId();
updateTree(treeId, suffix, color, treeVisibility, groupId);
goToNextTree();
}
function shuffleArray(array) {
for (let i = array.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[array[i], array[j]] = [array[j], array[i]];
}
return array;
}
/* NOTE(amotta): Shuffle to get rid of attentional biases */
allTreeIds = shuffleArray(Object.keys(api.tracing.getAllTrees()).map(Number));
/* assign desired functions to keys */
api.utils.registerKeyHandler("6", () => {
doTree("Yes", 10); // green
});
api.utils.registerKeyHandler("5", () => {
doTree("Maybe", 0); // black
});
api.utils.registerKeyHandler("4", () => {
doTree("No", 1); // red
});
alert(welcomeMessage);
goToNextTree();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment