From 0c44d4648b3de13bb6b53dc6169e4db228ca3e12 Mon Sep 17 00:00:00 2001 From: aranega Date: Tue, 13 Aug 2024 11:03:42 -0600 Subject: [PATCH] Fix formatting --- .../backend/visualizer/settings/common.py | 2 +- .../components/viewers/EM/SceneControls.tsx | 7 +- .../frontend/src/helpers/slidingRing.ts | 307 +++++++++--------- 3 files changed, 156 insertions(+), 160 deletions(-) diff --git a/applications/visualizer/backend/visualizer/settings/common.py b/applications/visualizer/backend/visualizer/settings/common.py index ff76b0d8..d29bc555 100644 --- a/applications/visualizer/backend/visualizer/settings/common.py +++ b/applications/visualizer/backend/visualizer/settings/common.py @@ -146,7 +146,7 @@ NINJA_PAGINATION_PER_PAGE = 100 # Fragments for various artifacts -# In this format: {var} is changed by the backend, while {{var}} is something that need to be changed by the frontend +# In this format: {var} is changed by the backend, while the first {{var}} is something that will be changed by the frontend # NEURON_REPRESENTATION_3D_URL_FORMAT = "resources/{{dataset}}/3d-model/{name}" # DATASET_NEURON_REPRESENTATION_3D_URL_FORMAT = "resources/{dataset}/3d-model/{{name}}" # DATASET_EMDATA_URL_FORMAT = "resources/{dataset}/em-data/tiles/{{index}}" diff --git a/applications/visualizer/frontend/src/components/viewers/EM/SceneControls.tsx b/applications/visualizer/frontend/src/components/viewers/EM/SceneControls.tsx index c6139e66..c43a7d81 100644 --- a/applications/visualizer/frontend/src/components/viewers/EM/SceneControls.tsx +++ b/applications/visualizer/frontend/src/components/viewers/EM/SceneControls.tsx @@ -11,12 +11,7 @@ interface ScaleControlsHandlers { onPrint: () => void; } -function SceneControls({ - onZoomIn, - onResetView, - onZoomOut, - onPrint, -}: ScaleControlsHandlers) { +function SceneControls({ onZoomIn, onResetView, onZoomOut, onPrint }: ScaleControlsHandlers) { return ( { - onPush: (n: number) => T; // called any time a new object is pushed into the ring - onSelected: (n: number, o: T) => void; // called when the selected object changes - onUnselected: (n: number, o: T) => void; // called when an object is unselected - onEvict: (n: number, o: T) => void; // called when an object is evicted from the ring + onPush: (n: number) => T; // called any time a new object is pushed into the ring + onSelected: (n: number, o: T) => void; // called when the selected object changes + onUnselected: (n: number, o: T) => void; // called when an object is unselected + onEvict: (n: number, o: T) => void; // called when an object is evicted from the ring } export interface SlidingRingOptions extends SlidingRingCb { - cacheSize: number; - startAt: number; - extent: [number, number]; + cacheSize: number; + startAt: number; + extent: [number, number]; } /** @@ -29,169 +29,170 @@ export interface SlidingRingOptions extends SlidingRingCb { * Going back on the previous element follows the same logic. */ export class SlidingRing { - private extent: [number, number]; - - private ring: Array<{ - n: number; // position within extent - o: T; - }>; - - private pos: number; // current buffer position - private tail: number; // buffer tail - private head: number; // buffer head - - private cb: SlidingRingCb - - constructor(options: SlidingRingOptions) { - if (options.cacheSize < 3) { - throw Error("cache should be greater that 3") - } - - const [min, max] = options.extent - - if (min >= max) { - throw Error("extent should be [min,max], where min < max") - } - - if (options.startAt > max || options.startAt < min) { - throw Error("startAt must be within extent") - } - - const extentSize = max - min - if (extentSize < 3) { - throw Error("extent size is too small, should be greater than 3") - } - - if (extentSize < options.cacheSize) { - options.cacheSize = max - min - } - - this.ring = new Array(options.cacheSize) - this.extent = options.extent - - this.cb = { - onPush: options.onPush, - onSelected: options.onSelected, - onUnselected: options.onUnselected, - onEvict: options.onEvict, - } - - // initialize ring - const halfSize = Math.floor(this.ring.length / 2) - let tailN = options.startAt - halfSize - let headN = options.startAt + halfSize - - // the ring may start near the extent - // this account for that adjustment - if (tailN < min) tailN = min - if (headN > max) { - headN = max - tailN = headN - this.ring.length - 1 - } - - // populate the ring - for (let i = 0; i < this.ring.length; i++) { - const n = tailN + i - const o = this.cb.onPush(n) - - this.ring[i] = {n, o} - } - - this.pos = options.startAt - tailN - this.tail = 0 - this.head = this.ring.length - 1 - - this.cb.onSelected(options.startAt, this.ring[this.pos].o) + private extent: [number, number]; + + private ring: Array<{ + n: number; // position within extent + o: T; + }>; + + private pos: number; // current buffer position + private tail: number; // buffer tail + private head: number; // buffer head + + private cb: SlidingRingCb; + + constructor(options: SlidingRingOptions) { + if (options.cacheSize < 3) { + throw Error("cache should be greater that 3"); + } + + const [min, max] = options.extent; + + if (min >= max) { + throw Error("extent should be [min,max], where min < max"); + } + + if (options.startAt > max || options.startAt < min) { + throw Error("startAt must be within extent"); + } + + const extentSize = max - min; + if (extentSize < 3) { + throw Error("extent size is too small, should be greater than 3"); + } + + if (extentSize < options.cacheSize) { + options.cacheSize = max - min; + } + + this.ring = new Array(options.cacheSize); + this.extent = options.extent; + + this.cb = { + onPush: options.onPush, + onSelected: options.onSelected, + onUnselected: options.onUnselected, + onEvict: options.onEvict, + }; + + // initialize ring + const halfSize = Math.floor(this.ring.length / 2); + let tailN = options.startAt - halfSize; + let headN = options.startAt + halfSize; + + // the ring may start near the extent + // this account for that adjustment + if (tailN < min) tailN = min; + if (headN > max) { + headN = max; + tailN = headN - this.ring.length - 1; } - next() { - const [_, max] = this.extent - const nextN = this.ring[this.pos].n + 1 - if (nextN > max) return + // populate the ring + for (let i = 0; i < this.ring.length; i++) { + const n = tailN + i; + const o = this.cb.onPush(n); - // update selected - const nextPos = (this.pos + this.ring.length + 1) % this.ring.length - this.cb.onSelected(this.ring[nextPos].n, this.ring[nextPos].o) - this.cb.onUnselected(this.ring[this.pos].n, this.ring[this.pos].o) + this.ring[i] = { n, o }; + } + + this.pos = options.startAt - tailN; + this.tail = 0; + this.head = this.ring.length - 1; + + this.cb.onSelected(options.startAt, this.ring[this.pos].o); + } - // update sliding window - const nextHeadN = this.ring[this.head].n + 1 - const halfSize = Math.floor(this.ring.length / 2) // TODO: to avoid calc should we save as class property? - const canSlideFurther = nextN - this.ring[this.tail].n > halfSize + next() { + const [_, max] = this.extent; + const nextN = this.ring[this.pos].n + 1; + if (nextN > max) return; - if (nextHeadN <= max && canSlideFurther) { - const tail = this.ring[this.tail] - this.cb.onEvict(tail.n, tail.o) + // update selected + const nextPos = (this.pos + this.ring.length + 1) % this.ring.length; + this.cb.onSelected(this.ring[nextPos].n, this.ring[nextPos].o); + this.cb.onUnselected(this.ring[this.pos].n, this.ring[this.pos].o); - this.ring[this.tail] = { - n: nextHeadN, - o: this.cb.onPush(nextHeadN) - } + // update sliding window + const nextHeadN = this.ring[this.head].n + 1; + const halfSize = Math.floor(this.ring.length / 2); // TODO: to avoid calc should we save as class property? + const canSlideFurther = nextN - this.ring[this.tail].n > halfSize; - this.head = this.tail - this.tail = (this.tail + this.ring.length + 1) % this.ring.length - } + if (nextHeadN <= max && canSlideFurther) { + const tail = this.ring[this.tail]; + this.cb.onEvict(tail.n, tail.o); - this.pos = nextPos + this.ring[this.tail] = { + n: nextHeadN, + o: this.cb.onPush(nextHeadN), + }; + + this.head = this.tail; + this.tail = (this.tail + this.ring.length + 1) % this.ring.length; } - prev() { - const [min, _] = this.extent - const prevN = this.ring[this.pos].n - 1 - if (prevN < min) return + this.pos = nextPos; + } - // update selected - const prevPos = (this.pos + this.ring.length - 1) % this.ring.length - this.cb.onSelected(this.ring[prevPos].n, this.ring[prevPos].o) - this.cb.onUnselected(this.ring[this.pos].n, this.ring[this.pos].o) + prev() { + const [min, _] = this.extent; + const prevN = this.ring[this.pos].n - 1; + if (prevN < min) return; - // update sliding window - const nextTailN = this.ring[this.tail].n - 1 - const halfSize = Math.floor(this.ring.length / 2) // TODO: to avoid calc should we save as class property? - const canSlideBackwards = this.ring[this.head].n - prevN > halfSize + // update selected + const prevPos = (this.pos + this.ring.length - 1) % this.ring.length; + this.cb.onSelected(this.ring[prevPos].n, this.ring[prevPos].o); + this.cb.onUnselected(this.ring[this.pos].n, this.ring[this.pos].o); - if (nextTailN >= min && canSlideBackwards) { - const head = this.ring[this.head] - this.cb.onEvict(head.n, head.o) + // update sliding window + const nextTailN = this.ring[this.tail].n - 1; + const halfSize = Math.floor(this.ring.length / 2); // TODO: to avoid calc should we save as class property? + const canSlideBackwards = this.ring[this.head].n - prevN > halfSize; - this.ring[this.head] = { - n: nextTailN, - o: this.cb.onPush(nextTailN) - } + if (nextTailN >= min && canSlideBackwards) { + const head = this.ring[this.head]; + this.cb.onEvict(head.n, head.o); - this.tail = this.head - this.head = (this.tail + this.ring.length - 1) % this.ring.length - } + this.ring[this.head] = { + n: nextTailN, + o: this.cb.onPush(nextTailN), + }; - this.pos = prevPos + this.tail = this.head; + this.head = (this.tail + this.ring.length - 1) % this.ring.length; } - debug() { - let text = '[' - - for (let i = 0; i < this.ring.length; i++) { - if (this.ring[i] === undefined) { // should not happen - text = text. concat('?') - continue - } - - switch (true) { - case i === this.pos: - text = text.concat('*') - break - case i === this.tail: - text = text.concat("-") - break - case i === this.head: - text = text.concat("+") - break - } - - text = text.concat(`${this.ring[i].n}`) - if (i !== this.ring.length - 1) text = text.concat(', ') - } - text = text.concat(']') - - console.debug(text) + this.pos = prevPos; + } + + debug() { + let text = "["; + + for (let i = 0; i < this.ring.length; i++) { + if (this.ring[i] === undefined) { + // should not happen + text = text.concat("?"); + continue; + } + + switch (true) { + case i === this.pos: + text = text.concat("*"); + break; + case i === this.tail: + text = text.concat("-"); + break; + case i === this.head: + text = text.concat("+"); + break; + } + + text = text.concat(`${this.ring[i].n}`); + if (i !== this.ring.length - 1) text = text.concat(", "); } + text = text.concat("]"); + + console.debug(text); + } }