Skip to content

Commit 09fecfe

Browse files
authored
Merge pull request #930 from thewtex/demo-prerun
perf: support demo pre-runs
2 parents d3e6fac + 9ad1dfa commit 09fecfe

21 files changed

+457
-152
lines changed

packages/compare-images/typescript/test/browser/demo-app/compare-images-load-sample-inputs.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,5 @@ export default null
2727

2828
// return model
2929
// }
30+
31+
export const usePreRun = true

packages/compress-stringify/typescript/test/browser/demo-app/compress-stringify-controller.ts

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Generated file. To retain edits, remove this comment.
22

33
import * as compressStringify from '../../../dist/bundles/compress-stringify.js'
4-
import compressStringifyLoadSampleInputs from "./compress-stringify-load-sample-inputs.js"
4+
import compressStringifyLoadSampleInputs, { usePreRun } from "./compress-stringify-load-sample-inputs.js"
55

66
class CompressStringifyModel {
77

@@ -79,6 +79,23 @@ class CompressStringifyController {
7979
}
8080
})
8181

82+
const tabGroup = document.querySelector('sl-tab-group')
83+
tabGroup.addEventListener('sl-tab-show', async (event) => {
84+
if (event.detail.name === 'compressStringify-panel') {
85+
const params = new URLSearchParams(window.location.search)
86+
if (!params.has('functionName') || params.get('functionName') !== 'compressStringify') {
87+
params.set('functionName', 'compressStringify')
88+
const url = new URL(document.location)
89+
url.search = params
90+
window.history.replaceState({ functionName: 'compressStringify' }, '', url)
91+
}
92+
if (!this.webWorker && loadSampleInputs && usePreRun) {
93+
await loadSampleInputs(model, true)
94+
await this.run()
95+
}
96+
}
97+
})
98+
8299
const runButton = document.querySelector('#compressStringifyInputs sl-button[name="run"]')
83100
runButton.addEventListener('click', async (event) => {
84101
event.preventDefault()
@@ -91,16 +108,11 @@ class CompressStringifyController {
91108

92109
try {
93110
runButton.loading = true
94-
const t0 = performance.now()
95-
96-
const { webWorker, output, } = await compressStringify.compressStringify(this.webWorker,
97-
model.inputs.get('input').slice(),
98-
Object.fromEntries(model.options.entries())
99-
)
100111

112+
const t0 = performance.now()
113+
const { output, } = await this.run()
101114
const t1 = performance.now()
102115
globalThis.notify("compressStringify successfully completed", `in ${t1 - t0} milliseconds.`, "success", "rocket-fill")
103-
this.webWorker = webWorker
104116

105117
model.outputs.set("output", output)
106118
outputOutputDownload.variant = "success"
@@ -116,6 +128,16 @@ class CompressStringifyController {
116128
}
117129
})
118130
}
131+
132+
async run() {
133+
const { webWorker, output, } = await compressStringify.compressStringify(this.webWorker,
134+
this.model.inputs.get('input').slice(),
135+
Object.fromEntries(this.model.options.entries())
136+
)
137+
this.webWorker = webWorker
138+
139+
return { output, }
140+
}
119141
}
120142

121143
const compressStringifyController = new CompressStringifyController(compressStringifyLoadSampleInputs)
Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,34 @@
1-
export default async function compressStringifyLoadSampleInputs (model) {
1+
export default async function compressStringifyLoadSampleInputs (model, preRun=false) {
22
const sampleInput = new Uint8Array([222, 173, 190, 239])
33
model.inputs.set("input", sampleInput)
4-
const inputElement = document.getElementById("compressStringify-input-details")
5-
inputElement.innerHTML = `<pre>${globalThis.escapeHtml(sampleInput.toString())}</pre>`
6-
inputElement.disabled = false
4+
if (!preRun) {
5+
const inputElement = document.getElementById("compressStringify-input-details")
6+
inputElement.innerHTML = `<pre>${globalThis.escapeHtml(sampleInput.toString())}</pre>`
7+
inputElement.disabled = false
8+
}
79

810
const stringify = true
911
model.options.set("stringify", stringify)
10-
const stringifyElement = document.querySelector('#compressStringifyInputs sl-checkbox[name=stringify]')
11-
stringifyElement.checked = stringify
12+
if (!preRun) {
13+
const stringifyElement = document.querySelector('#compressStringifyInputs sl-checkbox[name=stringify]')
14+
stringifyElement.checked = stringify
15+
}
1216

1317
const compressionLevel = 5
1418
model.options.set("compressionLevel", compressionLevel)
15-
const compressionLevelElement = document.querySelector('#compressStringifyInputs sl-input[name=compression-level]')
16-
compressionLevelElement.value = compressionLevel
19+
if (!preRun) {
20+
const compressionLevelElement = document.querySelector('#compressStringifyInputs sl-input[name=compression-level]')
21+
compressionLevelElement.value = compressionLevel
22+
}
1723

1824
const dataUrlPrefix = 'data:application/iwi+cbor+zstd;base64,'
1925
model.options.set("dataUrlPrefix", dataUrlPrefix)
20-
const dataUrlPrefixElement = document.querySelector('#compressStringifyInputs sl-input[name=data-url-prefix]')
21-
dataUrlPrefixElement.value = dataUrlPrefix
22-
}
26+
if (!preRun) {
27+
const dataUrlPrefixElement = document.querySelector('#compressStringifyInputs sl-input[name=data-url-prefix]')
28+
dataUrlPrefixElement.value = dataUrlPrefix
29+
}
30+
31+
return model
32+
}
33+
34+
export const usePreRun = true

packages/compress-stringify/typescript/test/browser/demo-app/index.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,12 @@ compressStringify.setPipelineWorkerUrl(pipelineWorkerUrl)
1010

1111
import './compress-stringify-controller.js'
1212
import './parse-string-decompress-controller.js'
13+
14+
const tabGroup = document.querySelector('sl-tab-group')
15+
const params = new URLSearchParams(window.location.search)
16+
if (params.has('functionName')) {
17+
const functionName = params.get('functionName')
18+
tabGroup.show(functionName + '-panel')
19+
} else {
20+
tabGroup.show('compressStringify-panel')
21+
}

packages/compress-stringify/typescript/test/browser/demo-app/parse-string-decompress-controller.ts

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Generated file. To retain edits, remove this comment.
22

33
import * as compressStringify from '../../../dist/bundles/compress-stringify.js'
4-
import parseStringDecompressLoadSampleInputs from "./parse-string-decompress-load-sample-inputs.js"
4+
import parseStringDecompressLoadSampleInputs, { usePreRun } from "./parse-string-decompress-load-sample-inputs.js"
55

66
class ParseStringDecompressModel {
77

@@ -69,6 +69,23 @@ class ParseStringDecompressController {
6969
}
7070
})
7171

72+
const tabGroup = document.querySelector('sl-tab-group')
73+
tabGroup.addEventListener('sl-tab-show', async (event) => {
74+
if (event.detail.name === 'parseStringDecompress-panel') {
75+
const params = new URLSearchParams(window.location.search)
76+
if (!params.has('functionName') || params.get('functionName') !== 'parseStringDecompress') {
77+
params.set('functionName', 'parseStringDecompress')
78+
const url = new URL(document.location)
79+
url.search = params
80+
window.history.replaceState({ functionName: 'parseStringDecompress' }, '', url)
81+
}
82+
if (!this.webWorker && loadSampleInputs && usePreRun) {
83+
await loadSampleInputs(model, true)
84+
await this.run()
85+
}
86+
}
87+
})
88+
7289
const runButton = document.querySelector('#parseStringDecompressInputs sl-button[name="run"]')
7390
runButton.addEventListener('click', async (event) => {
7491
event.preventDefault()
@@ -81,16 +98,11 @@ class ParseStringDecompressController {
8198

8299
try {
83100
runButton.loading = true
84-
const t0 = performance.now()
85-
86-
const { webWorker, output, } = await compressStringify.parseStringDecompress(this.webWorker,
87-
model.inputs.get('input').slice(),
88-
Object.fromEntries(model.options.entries())
89-
)
90101

102+
const t0 = performance.now()
103+
const { output, } = await this.run()
91104
const t1 = performance.now()
92105
globalThis.notify("parseStringDecompress successfully completed", `in ${t1 - t0} milliseconds.`, "success", "rocket-fill")
93-
this.webWorker = webWorker
94106

95107
model.outputs.set("output", output)
96108
outputOutputDownload.variant = "success"
@@ -106,6 +118,16 @@ class ParseStringDecompressController {
106118
}
107119
})
108120
}
121+
122+
async run() {
123+
const { webWorker, output, } = await compressStringify.parseStringDecompress(this.webWorker,
124+
this.model.inputs.get('input').slice(),
125+
Object.fromEntries(this.model.options.entries())
126+
)
127+
this.webWorker = webWorker
128+
129+
return { output, }
130+
}
109131
}
110132

111133
const parseStringDecompressController = new ParseStringDecompressController(parseStringDecompressLoadSampleInputs)
Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
1-
export default async function parseStringDecompressLoadSampleInputs (context) {
1+
export default async function parseStringDecompressLoadSampleInputs (model, preRun=false) {
22
// const sampleInput = TextDecoder().decode('data:application/iwi+cbor+zstd;base64,KLUv/SAEIQAA3q2+7w==')
33
const sampleInput = new Uint8Array([100,97,116,97,58,97,112,112,108,105,99,97,116,105,111,110,47,105,119,105,43,99,98,111,114,43,122,115,116,100,59,98,97,115,101,54,52,44,75,76,85,118,47,83,65,69,73,81,65,65,51,113,50,43,55,119,61,61])
4-
context.inputs.set("input", sampleInput)
5-
const inputElement = document.getElementById("parseStringDecompress-input-details")
6-
inputElement.innerHTML = `<pre>${globalThis.escapeHtml(sampleInput.toString())}</pre>`
7-
inputElement.disabled = false
4+
model.inputs.set("input", sampleInput)
5+
if (!preRun) {
6+
const inputElement = document.getElementById("parseStringDecompress-input-details")
7+
inputElement.innerHTML = `<pre>${globalThis.escapeHtml(sampleInput.toString())}</pre>`
8+
inputElement.disabled = false
9+
}
810

911
const parseString = true
10-
context.options.set("parseString", parseString)
11-
const parseStringElement = document.querySelector('#parseStringDecompressInputs sl-checkbox[name=parse-string]')
12-
parseStringElement.checked = parseString
13-
}
12+
model.options.set("parseString", parseString)
13+
if (!preRun) {
14+
const parseStringElement = document.querySelector('#parseStringDecompressInputs sl-checkbox[name=parse-string]')
15+
parseStringElement.checked = parseString
16+
}
17+
}
18+
19+
export const usePreRun = true

packages/dicom/typescript/test/browser/demo-app/apply-presentation-state-to-image-controller.ts

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import { writeImageArrayBuffer, copyImage } from 'itk-wasm'
44
import * as dicom from '../../../dist/bundles/dicom.js'
5-
import applyPresentationStateToImageLoadSampleInputs from "./apply-presentation-state-to-image-load-sample-inputs.js"
5+
import applyPresentationStateToImageLoadSampleInputs, { usePreRun } from "./apply-presentation-state-to-image-load-sample-inputs.js"
66

77
class ApplyPresentationStateToImageModel {
88

@@ -118,6 +118,23 @@ class ApplyPresentationStateToImageController {
118118
}
119119
})
120120

121+
const tabGroup = document.querySelector('sl-tab-group')
122+
tabGroup.addEventListener('sl-tab-show', async (event) => {
123+
if (event.detail.name === 'applyPresentationStateToImage-panel') {
124+
const params = new URLSearchParams(window.location.search)
125+
if (!params.has('functionName') || params.get('functionName') !== 'applyPresentationStateToImage') {
126+
params.set('functionName', 'applyPresentationStateToImage')
127+
const url = new URL(document.location)
128+
url.search = params
129+
window.history.replaceState({ functionName: 'applyPresentationStateToImage' }, '', url)
130+
}
131+
if (!this.webWorker && loadSampleInputs && usePreRun) {
132+
await loadSampleInputs(model, true)
133+
await this.run()
134+
}
135+
}
136+
})
137+
121138
const runButton = document.querySelector('#applyPresentationStateToImageInputs sl-button[name="run"]')
122139
runButton.addEventListener('click', async (event) => {
123140
event.preventDefault()
@@ -134,17 +151,11 @@ class ApplyPresentationStateToImageController {
134151

135152
try {
136153
runButton.loading = true
137-
const t0 = performance.now()
138-
139-
const { webWorker, presentationStateOutStream, outputImage, } = await dicom.applyPresentationStateToImage(this.webWorker,
140-
{ data: model.inputs.get('imageIn').data.slice(), path: model.inputs.get('imageIn').path },
141-
{ data: model.inputs.get('presentationStateFile').data.slice(), path: model.inputs.get('presentationStateFile').path },
142-
Object.fromEntries(model.options.entries())
143-
)
144154

155+
const t0 = performance.now()
156+
const { presentationStateOutStream, outputImage, } = await this.run()
145157
const t1 = performance.now()
146158
globalThis.notify("applyPresentationStateToImage successfully completed", `in ${t1 - t0} milliseconds.`, "success", "rocket-fill")
147-
this.webWorker = webWorker
148159

149160
model.outputs.set("presentationStateOutStream", presentationStateOutStream)
150161
presentationStateOutStreamOutputDownload.variant = "success"
@@ -169,6 +180,17 @@ class ApplyPresentationStateToImageController {
169180
}
170181
})
171182
}
183+
184+
async run() {
185+
const { webWorker, presentationStateOutStream, outputImage, } = await dicom.applyPresentationStateToImage(this.webWorker,
186+
{ data: this.model.inputs.get('imageIn').data.slice(), path: this.model.inputs.get('imageIn').path },
187+
{ data: this.model.inputs.get('presentationStateFile').data.slice(), path: this.model.inputs.get('presentationStateFile').path },
188+
Object.fromEntries(this.model.options.entries())
189+
)
190+
this.webWorker = webWorker
191+
192+
return { presentationStateOutStream, outputImage, }
193+
}
172194
}
173195

174196
const applyPresentationStateToImageController = new ApplyPresentationStateToImageController(applyPresentationStateToImageLoadSampleInputs)
Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,33 @@
1-
export default async function applyPresentationStateToImageLoadSampleInputs (model) {
1+
export default async function applyPresentationStateToImageLoadSampleInputs (model, preRun=false) {
22
const imageInButton = document.querySelector('#applyPresentationStateToImageInputs sl-button[name=image-in-file-button]')
3-
imageInButton.loading = true
3+
if (!preRun) {
4+
imageInButton.loading = true
5+
}
46
const imageInReponse = await fetch('https://bafybeihmnqsufckyjxt2z3yunppqggtq2rle7fta27rbetmf7fgviytghi.ipfs.w3s.link/ipfs/bafybeihmnqsufckyjxt2z3yunppqggtq2rle7fta27rbetmf7fgviytghi/input/gsps-pstate-test-input-image.dcm')
57
const imageInData = new Uint8Array(await imageInReponse.arrayBuffer())
68
model.inputs.set('imageIn', { data: imageInData, path: 'gsps-pstate-test-input-image.dcm' })
79
const imageInElement = document.getElementById('applyPresentationStateToImage-image-in-details')
8-
imageInElement.innerHTML = `<pre>${globalThis.escapeHtml(imageInData.subarray(0, 50).toString())}</pre>`
9-
imageInElement.disabled = false
10-
imageInButton.loading = false
10+
if (!preRun) {
11+
imageInElement.innerHTML = `<pre>${globalThis.escapeHtml(imageInData.subarray(0, 50).toString())}</pre>`
12+
imageInElement.disabled = false
13+
imageInButton.loading = false
14+
}
1115

1216
const pstateButton = document.querySelector('#applyPresentationStateToImageInputs sl-button[name=presentation-state-file-file-button]')
13-
pstateButton.loading = true
17+
if (!preRun) {
18+
pstateButton.loading = true
19+
}
1420
const pstateReponse = await fetch('https://bafybeihmnqsufckyjxt2z3yunppqggtq2rle7fta27rbetmf7fgviytghi.ipfs.w3s.link/ipfs/bafybeihmnqsufckyjxt2z3yunppqggtq2rle7fta27rbetmf7fgviytghi/input/gsps-pstate-test-input-pstate.dcm')
1521
const pstateData = new Uint8Array(await pstateReponse.arrayBuffer())
1622
model.inputs.set('presentationStateFile', { data: pstateData, path: 'gsps-pstate-test-input-pstate.dcm' })
17-
const pstateElement = document.getElementById('applyPresentationStateToImage-presentation-state-file-details')
18-
pstateElement.innerHTML = `<pre>${globalThis.escapeHtml(pstateData.subarray(0, 50).toString())}</pre>`
19-
pstateElement.disabled = false
20-
pstateButton.loading = false
23+
if (!preRun) {
24+
const pstateElement = document.getElementById('applyPresentationStateToImage-presentation-state-file-details')
25+
pstateElement.innerHTML = `<pre>${globalThis.escapeHtml(pstateData.subarray(0, 50).toString())}</pre>`
26+
pstateElement.disabled = false
27+
pstateButton.loading = false
28+
}
2129

2230
return model
2331
}
32+
33+
export const usePreRun = true

packages/dicom/typescript/test/browser/demo-app/index.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,12 @@ import './structured-report-to-html-controller.js'
1414
import './structured-report-to-text-controller.js'
1515
import './read-dicom-tags-controller.js'
1616
import './read-image-dicom-file-series-controller.js'
17+
18+
const tabGroup = document.querySelector('sl-tab-group')
19+
const params = new URLSearchParams(window.location.search)
20+
if (params.has('functionName')) {
21+
const functionName = params.get('functionName')
22+
tabGroup.show(functionName + '-panel')
23+
} else {
24+
tabGroup.show('applyPresentationStateToImage-panel')
25+
}

0 commit comments

Comments
 (0)