-
-
Notifications
You must be signed in to change notification settings - Fork 10
refactor: Refactor sync modifiers to use shared Data1D processing logic #193
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 1 commit
e3093ce
d13436e
47e8f17
f9fc7ef
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,10 +1,95 @@ | ||||||||||||||||||||||||||||
| import { ModifierInput, ModifierOutput } from "./types"; | ||||||||||||||||||||||||||||
| import { LMPModifier, Data1D } from "../types"; | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| interface ModifierProps { | ||||||||||||||||||||||||||||
| name: string; | ||||||||||||||||||||||||||||
| active: boolean; | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||
| * Shared function to process Data1D from LMPModifier. | ||||||||||||||||||||||||||||
| * Handles Data1D extraction, processing, and WASM memory cleanup. | ||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||
| export function processData1D( | ||||||||||||||||||||||||||||
| lmpModifier: LMPModifier, | ||||||||||||||||||||||||||||
| data1D: Data1D | undefined, | ||||||||||||||||||||||||||||
| input: ModifierInput, | ||||||||||||||||||||||||||||
| everything: boolean, | ||||||||||||||||||||||||||||
| syncDataPoints: boolean, | ||||||||||||||||||||||||||||
| clearPerSync: boolean, | ||||||||||||||||||||||||||||
| ): { data1D: Data1D | undefined; hasData1D: boolean } { | ||||||||||||||||||||||||||||
| // Get data1DNamesWrapper and extract size, then delete immediately | ||||||||||||||||||||||||||||
| const data1DNamesWrapper = lmpModifier.getData1DNames(); | ||||||||||||||||||||||||||||
| const hasData1D = data1DNamesWrapper.size() > 0; | ||||||||||||||||||||||||||||
| const data1DNamesSize = data1DNamesWrapper.size(); | ||||||||||||||||||||||||||||
| data1DNamesWrapper.delete(); // Delete WASM wrapper to prevent memory leak | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| if (data1DNamesSize === 0) { | ||||||||||||||||||||||||||||
| return { data1D, hasData1D: false }; | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| // Initialize data1D if needed | ||||||||||||||||||||||||||||
| let currentData1D = data1D; | ||||||||||||||||||||||||||||
| if (currentData1D == null) { | ||||||||||||||||||||||||||||
| currentData1D = { | ||||||||||||||||||||||||||||
| data: [], | ||||||||||||||||||||||||||||
| labels: [], | ||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| if ((everything || syncDataPoints) && currentData1D) { | ||||||||||||||||||||||||||||
andeplane marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||||||||||||||||||||||||
| // Data points is only for plotting figures | ||||||||||||||||||||||||||||
| if (clearPerSync) { | ||||||||||||||||||||||||||||
| // For histograms (compute rdf etc) we don't have time as x axis, so we clear every time | ||||||||||||||||||||||||||||
| currentData1D.data = []; | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| const lengthBeforeWeStart = currentData1D.data.length; // Used to avoid copying all data every time | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| if (currentData1D.labels.length === 0) { | ||||||||||||||||||||||||||||
| // First label is never visible | ||||||||||||||||||||||||||||
| currentData1D.labels.push("x"); | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| // Get data1DVector once before the loop for better performance | ||||||||||||||||||||||||||||
| const data1DVector = lmpModifier.getData1D(); | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| for (let j = 0; j < data1DNamesSize; j++) { | ||||||||||||||||||||||||||||
| const lmpData = data1DVector.get(j); | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| if (currentData1D.labels.length - 1 === j) { | ||||||||||||||||||||||||||||
| // Add missing labels | ||||||||||||||||||||||||||||
| currentData1D.labels.push(lmpData.getLabel()); | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| const xValuesPointer = lmpData.getXValuesPointer() / 4; | ||||||||||||||||||||||||||||
| const yValuesPointer = lmpData.getYValuesPointer() / 4; | ||||||||||||||||||||||||||||
andeplane marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||||||||||||||||||||||||
| const xValues = input.wasm.HEAPF32.subarray( | ||||||||||||||||||||||||||||
| xValuesPointer, | ||||||||||||||||||||||||||||
| xValuesPointer + lmpData.getNumPoints(), | ||||||||||||||||||||||||||||
| ) as Float32Array; | ||||||||||||||||||||||||||||
| const yValues = input.wasm.HEAPF32.subarray( | ||||||||||||||||||||||||||||
| yValuesPointer, | ||||||||||||||||||||||||||||
| yValuesPointer + lmpData.getNumPoints(), | ||||||||||||||||||||||||||||
| ) as Float32Array; | ||||||||||||||||||||||||||||
| for (let k = lengthBeforeWeStart; k < xValues.length; k++) { | ||||||||||||||||||||||||||||
| if (j === 0) { | ||||||||||||||||||||||||||||
| currentData1D.data.push([xValues[k]]); | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
| currentData1D.data[k].push(yValues[k]); | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
|
Comment on lines
75
to
81
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The current logic for populating
Suggested change
|
||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| // Delete the Data1D copy to prevent memory leak | ||||||||||||||||||||||||||||
| lmpData.delete(); | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| // Delete the vector wrapper after the loop to prevent memory leak | ||||||||||||||||||||||||||||
| data1DVector.delete(); | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| return { data1D: currentData1D, hasData1D }; | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| class Modifier { | ||||||||||||||||||||||||||||
| public name: string; | ||||||||||||||||||||||||||||
| public key: string; | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
Uh oh!
There was an error while loading. Please reload this page.