Skip to content

Commit

Permalink
Dataset functionality now fixed
Browse files Browse the repository at this point in the history
Now supports species datasets

Delete record with BackSpace or Delete keys
Undelete on crtl-Z
  • Loading branch information
Mattk70 committed Nov 9, 2023
1 parent ee1bbb6 commit 1c1be7b
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 82 deletions.
10 changes: 5 additions & 5 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ <h5>Saved Records</h5>
<span class="material-symbols-outlined">unarchive</span> Remove Current File From
Archive
</li>
<li class="dropdown-item d-none" href="#" id="dataset">
<li class="dropdown-item" href="#" id="dataset">
<span class="material-symbols-outlined">dataset</span> Create Dataset
</li>
</ul>
Expand Down Expand Up @@ -912,8 +912,8 @@ <h5 class="modal-title" id="gotoModalLabel">Set Location</h5>
</span> Analyse</a>
<div class="dropdown-divider"></div>
<a class="dropdown-item" id="create-manual-record" href="#"><span class="material-symbols-outlined">
post_add
</span> Create Archive Record</a>
edit_document
</span> Edit Record</a>
<a class="dropdown-item" id="context-create-clip" href="#"><span class="material-symbols-outlined">
music_note
</span> Export Audio Clip</a>
Expand All @@ -940,7 +940,7 @@ <h5 class="modal-title" id="gotoModalLabel">Set Location</h5>
<!-- Controls view -->
<div class="container-fluid p-1 text-bg-dark d-inline-flex" id="controlsWrapper" style="max-height: 50px">
<div id="filename" class="col-auto ps-1 overflow-visible flex-nowrap"></div>
<div id="unsaved-icon" class="material-symbols-outlined ms-2 me-2 text-warning d-none" title="There are unsaved records">warning</div>
<div id="unsaved-icon" class="material-symbols-outlined ms-2 me-2 text-warning d-none" title="There are unsaved edits">warning</div>
<div class="col-auto btn-group bg-dark ms-2 me-2 text-nowrap" role="group">
<button id="playToggle" class="p-1 pe-2 btn btn-outline-secondary">
<span class="material-symbols-outlined ">play_circle</span><span
Expand Down Expand Up @@ -1130,7 +1130,7 @@ <h5 class="modal-title" id="gotoModalLabel">Set Location</h5>
lonTxt = 'customLon';
}
const showMap = (where) => {
if (where === 'customLocationMap'){
if (where === 'customLocationMap' && LOCATIONS.length){
const markerGroup = L.featureGroup(); // Create a feature group to hold the markers

const bounds = L.latLngBounds(); // Create bounds to include all markers
Expand Down
11 changes: 11 additions & 0 deletions js/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,17 @@ class Model {
})
}

//Used by get-spectrogram
normalise_audio = (signal) => {
return tf.tidy(() => {
//signal = tf.tensor1d(signal, 'float32');
const sigMax = tf.max(signal);
const sigMin = tf.min(signal);
const range = sigMax.sub(sigMin);
//return signal.sub(sigMin).div(range).mul(tf.scalar(8192.0, 'float32')).sub(tf.scalar(4095, 'float32'))
return signal.sub(sigMin).div(range).mul(tf.scalar(2)).sub(tf.scalar(1))
})
};
async predictChunk(audioBuffer, start, fileStart, file, threshold, confidence) {
if (DEBUG) console.log('predictCunk begin', tf.memory().numTensors);
audioBuffer = tf.tensor1d(audioBuffer);
Expand Down
119 changes: 63 additions & 56 deletions js/ui.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
let seenTheDarkness = false, shownDaylightBanner = false, LOCATIONS, locationID = undefined;
let labels = [];
let labels = [], DELETE_HISTORY = [];

const STATE = {
mode: 'analyse',
Expand Down Expand Up @@ -1027,7 +1027,7 @@ exploreLink.addEventListener('click', async () => {

const datasetLink = document.getElementById('dataset');
datasetLink.addEventListener('click', async () => {
worker.postMessage({ action: 'create-dataset' });
worker.postMessage({ action: 'create-dataset', species: isSpeciesViewFiltered(true) });
});

const checkWidth = (text) => {
Expand Down Expand Up @@ -1500,7 +1500,7 @@ const setUpWorkerMessaging = () => {
message += '\nWould you like to remove the file from the Archive?';
if (confirm(message)) deleteFile(args.file)
} else {
if (args.savedToDB){
if (args.filter){
worker.postMessage({
action: 'filter',
species: isSpeciesViewFiltered(true),
Expand Down Expand Up @@ -2141,18 +2141,37 @@ const GLOBAL_ACTIONS = { // eslint-disable-line
}
}
},
KeyE: function (e) {
if (e.ctrlKey && region) exportAudio();
KeyC: function (e) {
// Center window on playhead
if (e.ctrlKey && currentBuffer) {
const saveBufferBegin = bufferBegin;
const middle = bufferBegin + wavesurfer.getCurrentTime();
bufferBegin = middle - windowLength / 2;
bufferBegin = Math.max(0, bufferBegin);
bufferBegin = Math.min(bufferBegin, currentFileDuration - windowLength)
// Move the region if needed
let region = getRegion();
if (region){
const shift = saveBufferBegin - bufferBegin;
region.start += shift;
region.end += shift;
if (region.start < 0 || region.end > windowLength) region = undefined;
}
postBufferUpdate({ begin: bufferBegin, position: 0.5, region: region, goToRegion: false})
}
},
KeyD: function (e) {
if (e.ctrlKey && e.shiftKey) worker.postMessage({ action: 'convert-dataset' });
},
KeyG: function (e) {
if (e.ctrlKey) showGoToPosition();
KeyE: function (e) {
if (e.ctrlKey && region) exportAudio();
},
KeyF: function (e) {
if (e.ctrlKey) toggleFullscreen();
},
KeyG: function (e) {
if (e.ctrlKey) showGoToPosition();
},
KeyO: async function (e) {
if (e.ctrlKey) await showOpenDialog();
},
Expand All @@ -2167,6 +2186,9 @@ const GLOBAL_ACTIONS = { // eslint-disable-line
KeyT: function (e) {
if (e.ctrlKey) timelineToggle(true);
},
KeyZ: function (e) {
if (e.ctrlKey && DELETE_HISTORY.length) insertManualRecord(...DELETE_HISTORY.pop());
},
Escape: function () {
if (PREDICTING) {
console.log('Operation aborted');
Expand All @@ -2193,25 +2215,6 @@ const GLOBAL_ACTIONS = { // eslint-disable-line
postBufferUpdate({ begin: bufferBegin, position: 1 })
}
},
KeyC: function (e) {
// Center window on playhead
if (e.ctrlKey && currentBuffer) {
const saveBufferBegin = bufferBegin;
const middle = bufferBegin + wavesurfer.getCurrentTime();
bufferBegin = middle - windowLength / 2;
bufferBegin = Math.max(0, bufferBegin);
bufferBegin = Math.min(bufferBegin, currentFileDuration - windowLength)
// Move the region if needed
let region = getRegion();
if (region){
const shift = saveBufferBegin - bufferBegin;
region.start += shift;
region.end += shift;
if (region.start < 0 || region.end > windowLength) region = undefined;
}
postBufferUpdate({ begin: bufferBegin, position: 0.5, region: region, goToRegion: false})
}
},
PageUp: function () {
if (currentBuffer) {
const position = wavesurfer.getCurrentTime() / windowLength;
Expand Down Expand Up @@ -2327,7 +2330,13 @@ const GLOBAL_ACTIONS = { // eslint-disable-line
activeRow.focus();
if (!activeRow.classList.contains('text-bg-dark')) activeRow.click();
}
}
},
Delete: function () {
if (activeRow) deleteRecord(activeRow);
},
Backspace: function () {
if (activeRow) deleteRecord(activeRow);
},
};

//returns a region object with the start and end of the region supplied
Expand Down Expand Up @@ -2902,7 +2911,8 @@ function setClickedIndex(target) {
}

const deleteRecord = (target) => {
if (target instanceof PointerEvent) target = activeRow;
if (target === activeRow) {}
else if (target instanceof PointerEvent) target = activeRow;
else {
target.forEach(position => {
const [start, end] = position;
Expand All @@ -2923,6 +2933,14 @@ const deleteRecord = (target) => {
const [file, start, end,] = unpackNameAttr(target);
const setting = target.closest('table');
const row = target.closest('tr');
let cname = target.querySelector('.cname').innerText;
let [species, confidence] = cname.split('\n');
confidence = parseInt(confidence.replace('%', '')) * 10;
const comment = target.querySelector('.comment').innerText;
const label = target.querySelector('.label').innerText;
let callCount = target.querySelector('.call-count').innerText;
callCount = callCount.replace('Present', '');
DELETE_HISTORY.push([species, start, end, comment, callCount, label, null, null, null, confidence])

worker.postMessage({
action: 'delete',
Expand Down Expand Up @@ -3712,7 +3730,7 @@ async function createContextMenu(e) {
}
}
if (region === undefined && ! inSummary) return;
const createOrEdit = (['archive', 'explore'].includes(STATE.mode)) && (region?.attributes.label || target.closest('#summary')) ? 'Edit' : 'Create';
//const createOrEdit = (['archive', 'explore'].includes(STATE.mode)) && (region?.attributes.label || target.closest('#summary')) ? 'Edit' : 'Create';

menu.html(`
<a class="dropdown-item play ${hideInSummary}"><span class='material-symbols-outlined'>play_circle</span> Play</a>
Expand All @@ -3721,7 +3739,7 @@ async function createContextMenu(e) {
</a>
<div class="dropdown-divider ${hideInSummary}"></div>
<a class="dropdown-item" id="create-manual-record" href="#">
<span class="material-symbols-outlined">post_add</span> ${createOrEdit} Archive Record${plural}
<span class="material-symbols-outlined">edit_document</span> Edit Record${plural}
</a>
<a class="dropdown-item" id="context-create-clip" href="#">
<span class="material-symbols-outlined">music_note</span> Export Audio Clip${plural}
Expand Down Expand Up @@ -3860,33 +3878,22 @@ recordEntryForm.addEventListener('submit', function (e) {
})


const insertManualRecord = (cname, start, end, comment, count, label, action, batch, originalCname) => {
const insertManualRecord = (cname, start, end, comment, count, label, action, batch, originalCname, confidence) => {
const files = batch ? fileList : currentFile;
const insert = (toDisk) => {
worker.postMessage({
action: 'insert-manual-record',
cname: cname,
originalCname: originalCname,
start: start?.toFixed(3),
end: end?.toFixed(3),
comment: comment,
count: count || null,
file: files,
label: label,
DBaction: action,
batch: batch,
toDisk: toDisk
})

}
if (STATE.mode === 'analyse') {
//Update the record in the memory db
insert(false)
}
// Insert to disk
setTimeout(insert, 500, true)
//insert(true)

worker.postMessage({
action: 'insert-manual-record',
cname: cname,
originalCname: originalCname,
start: start?.toFixed(3),
end: end?.toFixed(3),
comment: comment,
count: count || null,
file: files,
label: label,
DBaction: action,
batch: batch,
confidence: confidence
})
}


Expand Down
Loading

0 comments on commit 1c1be7b

Please sign in to comment.