@qe-libs/rena-wasm 0.1.11 → 0.1.12
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +12 -3
- package/dist/libena.js +2 -0
- package/dist/libena.wasm +0 -0
- package/package.json +5 -2
- package/src/data.js +48 -5
- package/src/index.js +94 -12
- package/src/tensor.js +47 -3
package/README.md
CHANGED
|
@@ -1,11 +1,13 @@
|
|
|
1
1
|
# @qe-libs/rena-wasm
|
|
2
2
|
|
|
3
|
-
JavaScript/WebAssembly ENA pipeline
|
|
4
|
-
[`@qe-libs/libqe-wasm`](../../libqe/wasm/README.md).
|
|
3
|
+
JavaScript/WebAssembly ENA pipeline.
|
|
5
4
|
|
|
6
5
|
Handles data parsing, unit/conversation grouping, and the full
|
|
7
6
|
accumulate → normalize → center → rotate → project → node-positions pipeline.
|
|
8
|
-
|
|
7
|
+
The math runs in WebAssembly: the ENA model code (rotations, node positions,
|
|
8
|
+
window estimation) is libena, rENA's C++ layer, compiled into this package
|
|
9
|
+
(`dist/libena.*`), and generic numerics and accumulation come from
|
|
10
|
+
[`@qe-libs/libqe-wasm`](../../libqe/wasm/README.md).
|
|
9
11
|
|
|
10
12
|
---
|
|
11
13
|
|
|
@@ -324,9 +326,16 @@ automatically unless `factorLevels` is provided explicitly.
|
|
|
324
326
|
|
|
325
327
|
```bash
|
|
326
328
|
npm install
|
|
329
|
+
npm run build # compiles dist/libena.* — needs emscripten (emcc) and conan
|
|
327
330
|
npm test
|
|
328
331
|
```
|
|
329
332
|
|
|
333
|
+
`npm run build` (`scripts/build.sh`) vendors the libena and libqe headers into
|
|
334
|
+
`include/` (`../scripts/sync-headers.sh`: libena from `inst/include`, libqe from
|
|
335
|
+
Conan), gets Armadillo from Conan, and builds with `emcmake`.
|
|
336
|
+
`test/libena.test.js` checks that this module and libqe-wasm's own copy of the
|
|
337
|
+
ENA functions give identical results.
|
|
338
|
+
|
|
330
339
|
Tests cover the simple windowed pipeline (`test/ena.test.js`) and the
|
|
331
340
|
context-tensor path (`test/tensor.test.js`), and check against R: infinite and
|
|
332
341
|
time-based windows, flexible horizons and custom rotations
|
package/dist/libena.js
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
async function LibENA(moduleArg={}){var Module=moduleArg;var ENVIRONMENT_IS_WEB=!!globalThis.window;var ENVIRONMENT_IS_WORKER=!!globalThis.WorkerGlobalScope;var ENVIRONMENT_IS_NODE=globalThis.process?.versions?.node&&globalThis.process?.type!="renderer";if(ENVIRONMENT_IS_NODE){const{createRequire}=await(import("node:module"));var require=createRequire(import.meta.url)}var programArgs=[];var thisProgram="./this.program";var quit_=(status,toThrow)=>{throw toThrow};var _scriptName=import.meta.url;var scriptDirectory="";function locateFile(path){if(Module["locateFile"]){return Module["locateFile"](path,scriptDirectory)}return scriptDirectory+path}var readAsync,readBinary;if(ENVIRONMENT_IS_NODE){var fs=require("node:fs");if(_scriptName.startsWith("file:")){scriptDirectory=require("node:path").dirname(require("node:url").fileURLToPath(_scriptName))+"/"}readBinary=filename=>{filename=isFileURI(filename)?new URL(filename):filename;var ret=fs.readFileSync(filename);return ret};readAsync=async(filename,binary=true)=>{filename=isFileURI(filename)?new URL(filename):filename;var ret=fs.readFileSync(filename,binary?undefined:"utf8");return ret};if(process.argv.length>1){thisProgram=process.argv[1].replace(/\\/g,"/")}programArgs=process.argv.slice(2);quit_=(status,toThrow)=>{process.exitCode=status;throw toThrow}}else if(ENVIRONMENT_IS_WEB||ENVIRONMENT_IS_WORKER){try{scriptDirectory=new URL(".",_scriptName).href}catch{}{readAsync=async url=>{var response=await fetch(url,{credentials:"same-origin"});if(response.ok){return response.arrayBuffer()}throw new Error(response.status+" : "+response.url)}}}else{}var out=console.log.bind(console);var err=console.error.bind(console);var wasmBinary;var ABORT=false;var isFileURI=filename=>filename.startsWith("file://");class EmscriptenEH{}class EmscriptenSjLj extends EmscriptenEH{}var runtimeInitialized=false;function getMemoryBuffer(){return wasmMemory.buffer}function updateMemoryViews(){if(HEAP8?.buffer?.resizable)return;var b=getMemoryBuffer();HEAP8=new Int8Array(b);HEAP16=new Int16Array(b);HEAPU8=new Uint8Array(b);HEAPU16=new Uint16Array(b);HEAP32=new Int32Array(b);HEAPU32=new Uint32Array(b);HEAPF32=new Float32Array(b);HEAPF64=new Float64Array(b);HEAP64=new BigInt64Array(b);HEAPU64=new BigUint64Array(b)}function preRun(){var preRun=Module["preRun"];if(preRun){if(typeof preRun=="function")preRun=[preRun];onPreRuns.push(...preRun)}callRuntimeCallbacks(onPreRuns)}function initRuntime(){runtimeInitialized=true;wasmExports["C"]()}function postRun(){var postRun=Module["postRun"];if(postRun){if(typeof postRun=="function")postRun=[postRun];onPostRuns.push(...postRun)}callRuntimeCallbacks(onPostRuns)}function abort(what){Module["onAbort"]?.(what);what=`Aborted(${what})`;err(what);ABORT=true;what+=". Build with -sASSERTIONS for more info.";var e=new WebAssembly.RuntimeError(what);throw e}var wasmBinaryFile;function findWasmBinary(){if(Module["locateFile"]){return locateFile("libena.wasm")}return new URL("libena.wasm",import.meta.url).href}function getBinarySync(file){if(readBinary){return readBinary(file)}throw"both async and sync fetching of the wasm failed"}async function getWasmBinary(binaryFile){if(!wasmBinary){try{var response=await readAsync(binaryFile);return new Uint8Array(response)}catch{}}return getBinarySync(binaryFile)}async function instantiateArrayBuffer(binaryFile,imports){try{var binary=await getWasmBinary(binaryFile);var instance=await WebAssembly.instantiate(binary,imports);return instance}catch(reason){err(`failed to asynchronously prepare wasm: ${reason}`);abort(reason)}}async function instantiateAsync(binary,binaryFile,imports){if(!binary&&!ENVIRONMENT_IS_NODE){try{var response=fetch(binaryFile,{credentials:"same-origin"});var instantiationResult=await WebAssembly.instantiateStreaming(response,imports);return instantiationResult}catch(reason){err(`wasm streaming compile failed: ${reason}`);err("falling back to ArrayBuffer instantiation")}}return instantiateArrayBuffer(binaryFile,imports)}function getWasmImports(){var imports={a:wasmImports};return imports}async function createWasm(){function receiveInstance(instance){wasmExports=instance.exports;assignWasmExports(wasmExports);updateMemoryViews();return wasmExports}function receiveInstantiationResult(result){return receiveInstance(result["instance"])}var info=getWasmImports();var instantiateWasm=Module["instantiateWasm"];if(instantiateWasm){return new Promise(resolve=>{instantiateWasm(info,inst=>resolve(receiveInstance(inst)))})}wasmBinaryFile??=findWasmBinary();var result=await instantiateAsync(wasmBinary,wasmBinaryFile,info);var exports=receiveInstantiationResult(result);return exports}class ExitStatus{name="ExitStatus";constructor(status){this.message=`Program terminated with exit(${status})`;this.status=status}}var HEAP8;var callRuntimeCallbacks=callbacks=>{while(callbacks.length>0){callbacks.shift()(Module)}};var onPostRuns=[];var onPreRuns=[];var noExitRuntime=true;var stackRestore=val=>__emscripten_stack_restore(val);var stackSave=()=>_emscripten_stack_get_current();var HEAPU32;class ExceptionInfo{constructor(excPtr){this.excPtr=excPtr;this.ptr=excPtr-24}set_type(type){HEAPU32[this.ptr+4>>2]=type}get_type(){return HEAPU32[this.ptr+4>>2]}set_destructor(destructor){HEAPU32[this.ptr+8>>2]=destructor}get_destructor(){return HEAPU32[this.ptr+8>>2]}set_caught(caught){caught=caught?1:0;HEAP8[this.ptr+12]=caught}get_caught(){return HEAP8[this.ptr+12]!=0}set_rethrown(rethrown){rethrown=rethrown?1:0;HEAP8[this.ptr+13]=rethrown}get_rethrown(){return HEAP8[this.ptr+13]!=0}init(type,destructor){this.set_adjusted_ptr(0);this.set_type(type);this.set_destructor(destructor)}set_adjusted_ptr(adjustedPtr){HEAPU32[this.ptr+16>>2]=adjustedPtr}get_adjusted_ptr(){return HEAPU32[this.ptr+16>>2]}}var uncaughtExceptionCount=0;var __Unwind_RaiseException=ex=>{abort()};var ___cxa_throw=(ptr,type,destructor)=>{var info=new ExceptionInfo(ptr);info.init(type,destructor);uncaughtExceptionCount++;__Unwind_RaiseException(ptr)};var __abort_js=()=>abort("");var HEAPU8;var AsciiToString=ptr=>{var str="";while(1){var ch=HEAPU8[ptr++];if(!ch)return str;str+=String.fromCharCode(ch)}};var awaitingDependencies={};var registeredTypes={};var typeDependencies={};class BindingError extends Error{constructor(message){super(message);this.name="BindingError"}}var throwBindingError=message=>{throw new BindingError(message)};function sharedRegisterType(rawType,registeredInstance,options={}){var name=registeredInstance.name;if(!rawType){throwBindingError(`type "${name}" must have a positive integer typeid pointer`)}if(registeredTypes.hasOwnProperty(rawType)){if(options.ignoreDuplicateRegistrations){return}else{throwBindingError(`Cannot register type '${name}' twice`)}}registeredTypes[rawType]=registeredInstance;delete typeDependencies[rawType];if(awaitingDependencies.hasOwnProperty(rawType)){var callbacks=awaitingDependencies[rawType];delete awaitingDependencies[rawType];callbacks.forEach(cb=>cb())}}function registerType(rawType,registeredInstance,options={}){return sharedRegisterType(rawType,registeredInstance,options)}var HEAP16;var HEAPU16;var HEAP32;var HEAP64;var HEAPU64;var integerReadValueFromPointer=(name,width,signed)=>{switch(width){case 1:return signed?pointer=>HEAP8[pointer]:pointer=>HEAPU8[pointer];case 2:return signed?pointer=>HEAP16[pointer>>1]:pointer=>HEAPU16[pointer>>1];case 4:return signed?pointer=>HEAP32[pointer>>2]:pointer=>HEAPU32[pointer>>2];case 8:return signed?pointer=>HEAP64[pointer>>3]:pointer=>HEAPU64[pointer>>3];default:throw new TypeError(`invalid integer width (${width}): ${name}`)}};var __embind_register_bigint=(primitiveType,name,size,minRange,maxRange)=>{name=AsciiToString(name);const isUnsignedType=minRange===0n;let fromWireType=value=>value;if(isUnsignedType){const bitSize=size*8;fromWireType=value=>BigInt.asUintN(bitSize,value);maxRange=fromWireType(maxRange)}registerType(primitiveType,{name,fromWireType,toWireType:(destructors,value)=>{if(typeof value=="number"){value=BigInt(value)}return value},readValueFromPointer:integerReadValueFromPointer(name,size,!isUnsignedType),destructorFunction:null})};var __embind_register_bool=(rawType,name,trueValue,falseValue)=>{name=AsciiToString(name);registerType(rawType,{name,fromWireType:function(wt){return!!wt},toWireType:function(destructors,o){return o?trueValue:falseValue},readValueFromPointer:function(pointer){return this.fromWireType(HEAPU8[pointer])},destructorFunction:null})};var emval_freelist=[];var emval_handles=[0,1,,1,null,1,true,1,false,1];var __emval_decref=handle=>{if(handle>9&&0===--emval_handles[handle+1]){var value=emval_handles[handle];emval_handles[handle]=undefined;emval_freelist.push(handle)}};var Emval={toValue:handle=>{if(!handle){throwBindingError(`Cannot use deleted val. handle = ${handle}`)}return emval_handles[handle]},toHandle:value=>{switch(value){case undefined:return 2;case null:return 4;case true:return 6;case false:return 8;default:{const handle=emval_freelist.pop()||emval_handles.length;emval_handles[handle]=value;emval_handles[handle+1]=1;return handle}}}};function readPointer(pointer){return this.fromWireType(HEAPU32[pointer>>2])}var EmValType={name:"emscripten::val",fromWireType:handle=>{var rv=Emval.toValue(handle);__emval_decref(handle);return rv},toWireType:(destructors,value)=>Emval.toHandle(value),readValueFromPointer:readPointer,destructorFunction:null};var __embind_register_emval=rawType=>registerType(rawType,EmValType);var HEAPF32;var HEAPF64;var floatReadValueFromPointer=(name,width)=>{switch(width){case 4:return function(pointer){return this.fromWireType(HEAPF32[pointer>>2])};case 8:return function(pointer){return this.fromWireType(HEAPF64[pointer>>3])};default:throw new TypeError(`invalid float width (${width}): ${name}`)}};var __embind_register_float=(rawType,name,size)=>{name=AsciiToString(name);registerType(rawType,{name,fromWireType:value=>value,toWireType:(destructors,value)=>value,readValueFromPointer:floatReadValueFromPointer(name,size),destructorFunction:null})};var createNamedFunction=(name,func)=>Object.defineProperty(func,"name",{value:name});var runDestructors=destructors=>{while(destructors.length){var ptr=destructors.pop();var del=destructors.pop();del(ptr)}};function usesDestructorStack(argTypes){for(var i=1;i<argTypes.length;++i){if(argTypes[i]!==null&&argTypes[i].destructorFunction===undefined){return true}}return false}function argsUseStackAlloc(argTypes){for(var i=1;i<argTypes.length;++i){if(argTypes[i]!==null&&argTypes[i].argStackAlloc){return true}}return false}function createJsInvoker(argTypes,isClassMethodFunc,returns,isAsync){var needsDestructorStack=usesDestructorStack(argTypes);var argsNeedStack=argsUseStackAlloc(argTypes);var useStackFrame=argsNeedStack&&!isAsync&&!needsDestructorStack;if(argsNeedStack&&!useStackFrame){needsDestructorStack=true}var argCount=argTypes.length-2;var argsList=[];var argsListWired=["fn"];if(isClassMethodFunc){argsListWired.push("thisWired")}for(var i=0;i<argCount;++i){argsList.push(`arg${i}`);argsListWired.push(`arg${i}Wired`)}argsList=argsList.join();argsListWired=argsListWired.join();var invokerFnBody=`return function (${argsList}) {\n`;if(needsDestructorStack){invokerFnBody+="var destructors = [];\n"}if(useStackFrame){invokerFnBody+="var sp = stackSave();\ntry {\n"}var dtorStack=needsDestructorStack?"destructors":"null";var args1=["humanName","throwBindingError","invoker","fn","runDestructors","fromRetWire","toClassParamWire"];if(useStackFrame){args1.push("stackSave","stackRestore")}if(isClassMethodFunc){invokerFnBody+=`var thisWired = toClassParamWire(${dtorStack}, this);\n`}for(var i=0;i<argCount;++i){var argName=`toArg${i}Wire`;invokerFnBody+=`var arg${i}Wired = ${argName}(${dtorStack}, arg${i});\n`;args1.push(argName)}invokerFnBody+=(returns||isAsync?"var rv = ":"")+`invoker(${argsListWired});\n`;if(useStackFrame){invokerFnBody+="} finally {\nstackRestore(sp);\n}\n"}if(needsDestructorStack){invokerFnBody+="runDestructors(destructors);\n"}else{for(var i=isClassMethodFunc?1:2;i<argTypes.length;++i){var paramName=i===1?"thisWired":`arg${i-2}Wired`;if(argTypes[i].destructorFunction!==null){invokerFnBody+=`${paramName}_dtor(${paramName});\n`;args1.push(`${paramName}_dtor`)}}}if(returns){invokerFnBody+="var ret = fromRetWire(rv);\n"+"return ret;\n"}else{}invokerFnBody+="}\n";return new Function(args1,invokerFnBody)}function craftInvokerFunction(humanName,argTypes,classType,cppInvokerFunc,cppTargetFunc,isAsync){var argCount=argTypes.length;if(argCount<2){throwBindingError("argTypes array size mismatch! Must at least get return value and receiver (this) types!")}var isClassMethodFunc=argTypes[1]!==null&&classType!==null;var needsDestructorStack=usesDestructorStack(argTypes);var argsNeedStack=argsUseStackAlloc(argTypes);var useStackFrame=argsNeedStack&&!isAsync&&!needsDestructorStack;if(argsNeedStack&&!useStackFrame){needsDestructorStack=true}var returns=!argTypes[0].isVoid;var retType=argTypes[0];var instType=argTypes[1];var closureArgs=[humanName,throwBindingError,cppInvokerFunc,cppTargetFunc,runDestructors,retType.fromWireType.bind(retType),instType?.toWireType.bind(instType)];if(useStackFrame){closureArgs.push(stackSave,stackRestore)}for(var i=2;i<argCount;++i){var argType=argTypes[i];closureArgs.push(argType.toWireType.bind(argType))}if(!needsDestructorStack){for(var i=isClassMethodFunc?1:2;i<argTypes.length;++i){if(argTypes[i].destructorFunction!==null){closureArgs.push(argTypes[i].destructorFunction)}}}let invokerFactory=createJsInvoker(argTypes,isClassMethodFunc,returns,isAsync);var invokerFn=invokerFactory(...closureArgs);return createNamedFunction(humanName,invokerFn)}var ensureOverloadTable=(proto,methodName,humanName)=>{if(undefined===proto[methodName].overloadTable){var prevFunc=proto[methodName];proto[methodName]=function(...args){if(!proto[methodName].overloadTable.hasOwnProperty(args.length)){throwBindingError(`Function '${humanName}' called with an invalid number of arguments (${args.length}) - expects one of (${proto[methodName].overloadTable})!`)}return proto[methodName].overloadTable[args.length].apply(this,args)};proto[methodName].overloadTable=[];proto[methodName].overloadTable[prevFunc.argCount]=prevFunc}};var exposePublicSymbol=(name,value,numArguments)=>{if(Module.hasOwnProperty(name)){if(undefined===numArguments||undefined!==Module[name].overloadTable&&undefined!==Module[name].overloadTable[numArguments]){throwBindingError(`Cannot register public name '${name}' twice`)}ensureOverloadTable(Module,name,name);if(Module[name].overloadTable.hasOwnProperty(numArguments)){throwBindingError(`Cannot register multiple overloads of a function with the same number of arguments (${numArguments})!`)}Module[name].overloadTable[numArguments]=value}else{Module[name]=value;Module[name].argCount=numArguments}};var heap32VectorToArray=(count,firstElement)=>{var array=[];for(var i=0;i<count;i++){array.push(HEAPU32[firstElement+i*4>>2])}return array};class InternalError extends Error{constructor(message){super(message);this.name="InternalError"}}var throwInternalError=message=>{throw new InternalError(message)};var replacePublicSymbol=(name,value,numArguments)=>{if(!Module.hasOwnProperty(name)){throwInternalError("Replacing nonexistent public symbol")}if(undefined!==Module[name].overloadTable&&undefined!==numArguments){Module[name].overloadTable[numArguments]=value}else{Module[name]=value;Module[name].argCount=numArguments}};var wasmTableMirror=[];var getWasmTableEntry=funcPtr=>{var func=wasmTableMirror[funcPtr];if(!func){wasmTableMirror[funcPtr]=func=wasmTable.get(funcPtr)}return func};var embind__requireFunction=(signature,rawFunction,isAsync=false)=>{signature=AsciiToString(signature);function makeDynCaller(){var rtn=getWasmTableEntry(rawFunction);return rtn}var fp=makeDynCaller();if(typeof fp!="function"){throwBindingError(`unknown function pointer with signature ${signature}: ${rawFunction}`)}return fp};class UnboundTypeError extends Error{}var getTypeName=type=>{var ptr=___getTypeName(type);var rv=AsciiToString(ptr);_free(ptr);return rv};var throwUnboundTypeError=(message,types)=>{var unboundTypes=[];var seen={};function visit(type){if(seen[type]){return}if(registeredTypes[type]){return}if(typeDependencies[type]){typeDependencies[type].forEach(visit);return}unboundTypes.push(type);seen[type]=true}types.forEach(visit);throw new UnboundTypeError(`${message}: `+unboundTypes.map(getTypeName).join([", "]))};var whenDependentTypesAreResolved=(myTypes,dependentTypes,getTypeConverters)=>{myTypes.forEach(type=>typeDependencies[type]=dependentTypes);function onComplete(typeConverters){var myTypeConverters=getTypeConverters(typeConverters);if(myTypeConverters.length!==myTypes.length){throwInternalError("Mismatched type converter count")}for(var i=0;i<myTypes.length;++i){registerType(myTypes[i],myTypeConverters[i])}}var typeConverters=new Array(dependentTypes.length);var unregisteredTypes=[];var registered=0;for(let[i,dt]of dependentTypes.entries()){if(registeredTypes.hasOwnProperty(dt)){typeConverters[i]=registeredTypes[dt]}else{unregisteredTypes.push(dt);if(!awaitingDependencies.hasOwnProperty(dt)){awaitingDependencies[dt]=[]}awaitingDependencies[dt].push(()=>{typeConverters[i]=registeredTypes[dt];++registered;if(registered===unregisteredTypes.length){onComplete(typeConverters)}})}}if(0===unregisteredTypes.length){onComplete(typeConverters)}};var getFunctionName=signature=>{signature=signature.trim();const argsIndex=signature.indexOf("(");if(argsIndex===-1)return signature;return signature.slice(0,argsIndex)};var __embind_register_function=(name,argCount,rawArgTypesAddr,signature,rawInvoker,fn,isAsync,isNonnullReturn)=>{var argTypes=heap32VectorToArray(argCount,rawArgTypesAddr);name=AsciiToString(name);name=getFunctionName(name);rawInvoker=embind__requireFunction(signature,rawInvoker,isAsync);exposePublicSymbol(name,function(){throwUnboundTypeError(`Cannot call ${name} due to unbound types`,argTypes)},argCount-1);whenDependentTypesAreResolved([],argTypes,argTypes=>{var invokerArgsArray=[argTypes[0],null].concat(argTypes.slice(1));replacePublicSymbol(name,craftInvokerFunction(name,invokerArgsArray,null,rawInvoker,fn,isAsync),argCount-1);return[]})};var __embind_register_integer=(primitiveType,name,size,minRange,maxRange)=>{name=AsciiToString(name);const isUnsignedType=minRange===0;let fromWireType=value=>value;if(isUnsignedType){var bitshift=32-8*size;fromWireType=value=>value<<bitshift>>>bitshift;maxRange=fromWireType(maxRange)}registerType(primitiveType,{name,fromWireType,toWireType:(destructors,value)=>value,readValueFromPointer:integerReadValueFromPointer(name,size,minRange!==0),destructorFunction:null})};var __embind_register_memory_view=(rawType,dataTypeIndex,name)=>{var typeMapping=[Int8Array,Uint8Array,Int16Array,Uint16Array,Int32Array,Uint32Array,Float32Array,Float64Array,BigInt64Array,BigUint64Array];var TA=typeMapping[dataTypeIndex];function decodeMemoryView(handle){var size=HEAPU32[handle>>2];var data=HEAPU32[handle+4>>2];return new TA(HEAP8.buffer,data,size)}name=AsciiToString(name);registerType(rawType,{name,fromWireType:decodeMemoryView,readValueFromPointer:decodeMemoryView},{ignoreDuplicateRegistrations:true})};var stringToUTF8Array=(str,heap,outIdx,maxBytesToWrite)=>{if(!(maxBytesToWrite>0))return 0;var startIdx=outIdx;var endIdx=outIdx+maxBytesToWrite-1;for(var i=0;i<str.length;++i){var u=str.codePointAt(i);if(u<=127){if(outIdx>=endIdx)break;heap[outIdx++]=u}else if(u<=2047){if(outIdx+1>=endIdx)break;heap[outIdx++]=192|u>>6;heap[outIdx++]=128|u&63}else if(u<=65535){if(outIdx+2>=endIdx)break;heap[outIdx++]=224|u>>12;heap[outIdx++]=128|u>>6&63;heap[outIdx++]=128|u&63}else{if(outIdx+3>=endIdx)break;heap[outIdx++]=240|u>>18;heap[outIdx++]=128|u>>12&63;heap[outIdx++]=128|u>>6&63;heap[outIdx++]=128|u&63;i++}}heap[outIdx]=0;return outIdx-startIdx};var stringToUTF8=(str,outPtr,maxBytesToWrite)=>stringToUTF8Array(str,HEAPU8,outPtr,maxBytesToWrite);var lengthBytesUTF8=str=>{var len=0;for(var i=0;i<str.length;++i){var c=str.charCodeAt(i);if(c<=127){len++}else if(c<=2047){len+=2}else if(c>=55296&&c<=57343){len+=4;++i}else{len+=3}}return len};var UTF8Decoder=globalThis.TextDecoder&&new TextDecoder;var findStringEnd=(heapOrArray,idx,maxBytesToRead,ignoreNul)=>{var maxIdx=idx+maxBytesToRead;if(ignoreNul)return maxIdx;while(heapOrArray[idx]&&!(idx>=maxIdx))++idx;return idx};var UTF8ArrayToString=(heapOrArray,idx=0,maxBytesToRead,ignoreNul)=>{var endPtr=findStringEnd(heapOrArray,idx,maxBytesToRead,ignoreNul);if(endPtr-idx>16&&heapOrArray.buffer&&UTF8Decoder){return UTF8Decoder.decode(heapOrArray.subarray(idx,endPtr))}var str="";while(idx<endPtr){var u0=heapOrArray[idx++];if(!(u0&128)){str+=String.fromCharCode(u0);continue}var u1=heapOrArray[idx++]&63;if((u0&224)==192){str+=String.fromCharCode((u0&31)<<6|u1);continue}var u2=heapOrArray[idx++]&63;if((u0&240)==224){u0=(u0&15)<<12|u1<<6|u2}else{u0=(u0&7)<<18|u1<<12|u2<<6|heapOrArray[idx++]&63}if(u0<65536){str+=String.fromCharCode(u0)}else{var ch=u0-65536;str+=String.fromCharCode(55296|ch>>10,56320|ch&1023)}}return str};var UTF8ToString=(ptr,maxBytesToRead,ignoreNul)=>ptr?UTF8ArrayToString(HEAPU8,ptr,maxBytesToRead,ignoreNul):"";var __embind_register_std_string=(rawType,name)=>{name=AsciiToString(name);var stdStringIsUTF8=true;registerType(rawType,{name,fromWireType(value){var length=HEAPU32[value>>2];var payload=value+4;var str;if(stdStringIsUTF8){str=UTF8ToString(payload,length,true)}else{str="";for(var i=0;i<length;++i){str+=String.fromCharCode(HEAPU8[payload+i])}}_free(value);return str},toWireType(destructors,value){if(value instanceof ArrayBuffer){value=new Uint8Array(value)}var length;var valueIsOfTypeString=typeof value=="string";if(!(valueIsOfTypeString||ArrayBuffer.isView(value)&&value.BYTES_PER_ELEMENT==1)){throwBindingError("Cannot pass non-string to std::string")}if(stdStringIsUTF8&&valueIsOfTypeString){length=lengthBytesUTF8(value)}else{length=value.length}var base=_malloc(4+length+1);var ptr=base+4;HEAPU32[base>>2]=length;if(valueIsOfTypeString){if(stdStringIsUTF8){stringToUTF8(value,ptr,length+1)}else{for(var i=0;i<length;++i){var charCode=value.charCodeAt(i);if(charCode>255){_free(base);throwBindingError("String has UTF-16 code units that do not fit in 8 bits")}HEAPU8[ptr+i]=charCode}}}else{HEAPU8.set(value,ptr)}if(destructors!==null){destructors.push(_free,base)}return base},readValueFromPointer:readPointer,destructorFunction(ptr){_free(ptr)}})};var UTF16Decoder=globalThis.TextDecoder?new TextDecoder("utf-16le"):undefined;var UTF16ToString=(ptr,maxBytesToRead,ignoreNul)=>{var idx=ptr>>1;var endIdx=findStringEnd(HEAPU16,idx,maxBytesToRead/2,ignoreNul);if(endIdx-idx>16&&UTF16Decoder)return UTF16Decoder.decode(HEAPU16.subarray(idx,endIdx));var str="";for(var i=idx;i<endIdx;++i){var codeUnit=HEAPU16[i];str+=String.fromCharCode(codeUnit)}return str};var stringToUTF16=(str,outPtr,maxBytesToWrite=2147483647)=>{if(maxBytesToWrite<2)return 0;maxBytesToWrite-=2;var startPtr=outPtr;var numCharsToWrite=maxBytesToWrite<str.length*2?maxBytesToWrite/2:str.length;for(var i=0;i<numCharsToWrite;++i){var codeUnit=str.charCodeAt(i);HEAP16[outPtr>>1]=codeUnit;outPtr+=2}HEAP16[outPtr>>1]=0;return outPtr-startPtr};var lengthBytesUTF16=str=>str.length*2;var UTF32ToString=(ptr,maxBytesToRead,ignoreNul)=>{var str="";var startIdx=ptr>>2;for(var i=0;!(i>=maxBytesToRead/4);i++){var utf32=HEAPU32[startIdx+i];if(!utf32&&!ignoreNul)break;str+=String.fromCodePoint(utf32)}return str};var stringToUTF32=(str,outPtr,maxBytesToWrite=2147483647)=>{if(maxBytesToWrite<4)return 0;var startPtr=outPtr;var endPtr=startPtr+maxBytesToWrite-4;for(var i=0;i<str.length;++i){var codePoint=str.codePointAt(i);if(codePoint>65535){i++}HEAP32[outPtr>>2]=codePoint;outPtr+=4;if(outPtr+4>endPtr)break}HEAP32[outPtr>>2]=0;return outPtr-startPtr};var lengthBytesUTF32=str=>{var len=0;for(var i=0;i<str.length;++i){var codePoint=str.codePointAt(i);if(codePoint>65535){i++}len+=4}return len};var __embind_register_std_wstring=(rawType,charSize,name)=>{name=AsciiToString(name);var decodeString,encodeString,lengthBytesUTF;if(charSize===2){decodeString=UTF16ToString;encodeString=stringToUTF16;lengthBytesUTF=lengthBytesUTF16}else{decodeString=UTF32ToString;encodeString=stringToUTF32;lengthBytesUTF=lengthBytesUTF32}registerType(rawType,{name,fromWireType:value=>{var length=HEAPU32[value>>2];var str=decodeString(value+4,length*charSize,true);_free(value);return str},toWireType:(destructors,value)=>{if(!(typeof value=="string")){throwBindingError(`Cannot pass non-string to C++ string type ${name}`)}var length=lengthBytesUTF(value);var ptr=_malloc(4+length+charSize);HEAPU32[ptr>>2]=length/charSize;encodeString(value,ptr+4,length+charSize);if(destructors!==null){destructors.push(_free,ptr)}return ptr},readValueFromPointer:readPointer,destructorFunction(ptr){_free(ptr)}})};var __embind_register_void=(rawType,name)=>{name=AsciiToString(name);registerType(rawType,{isVoid:true,name,fromWireType:()=>undefined,toWireType:(destructors,o)=>undefined})};var emval_methodCallers=[];var emval_addMethodCaller=caller=>{var id=emval_methodCallers.length;emval_methodCallers.push(caller);return id};var requireRegisteredType=(rawType,humanName)=>{var impl=registeredTypes[rawType];if(undefined===impl){throwBindingError(`${humanName} has unknown type ${getTypeName(rawType)}`)}return impl};var emval_lookupTypes=(argCount,argTypes)=>{var a=new Array(argCount);for(var i=0;i<argCount;++i){a[i]=requireRegisteredType(HEAPU32[argTypes+i*4>>2],`parameter ${i}`)}return a};var emval_returnValue=(toReturnWire,destructorsRef,handle)=>{var destructors=[];var result=toReturnWire(destructors,handle);if(destructors.length){HEAPU32[destructorsRef>>2]=Emval.toHandle(destructors)}return result};var emval_symbols={};var getStringOrSymbol=address=>{var symbol=emval_symbols[address];if(symbol===undefined){return AsciiToString(address)}return symbol};var __emval_create_invoker=(argCount,argTypesPtr,kind)=>{var GenericWireTypeSize=8;var[retType,...argTypes]=emval_lookupTypes(argCount,argTypesPtr);var toReturnWire=retType.toWireType.bind(retType);var argFromPtr=argTypes.map(type=>type.readValueFromPointer.bind(type));argCount--;var captures={toValue:Emval.toValue};var args=argFromPtr.map((argFromPtr,i)=>{var captureName=`argFromPtr${i}`;captures[captureName]=argFromPtr;return`${captureName}(args${i?"+"+i*GenericWireTypeSize:""})`});var functionBody;switch(kind){case 0:functionBody="toValue(handle)";break;case 2:functionBody="new (toValue(handle))";break;case 3:functionBody="";break;case 1:captures["getStringOrSymbol"]=getStringOrSymbol;functionBody="toValue(handle)[getStringOrSymbol(methodName)]";break}functionBody+=`(${args})`;if(!retType.isVoid){captures["toReturnWire"]=toReturnWire;captures["emval_returnValue"]=emval_returnValue;functionBody=`return emval_returnValue(toReturnWire, destructorsRef, ${functionBody})`}functionBody=`return function (handle, methodName, destructorsRef, args) {\n${functionBody}\n}`;var invokerFunction=new Function(Object.keys(captures),functionBody)(...Object.values(captures));var functionName=`methodCaller<(${argTypes.map(t=>t.name)}) => ${retType.name}>`;return emval_addMethodCaller(createNamedFunction(functionName,invokerFunction))};var __emval_get_property=(handle,key)=>{handle=Emval.toValue(handle);key=Emval.toValue(key);return Emval.toHandle(handle[key])};var __emval_incref=handle=>{if(handle>9){emval_handles[handle+1]+=1}};var __emval_invoke=(caller,handle,methodName,destructorsRef,args)=>emval_methodCallers[caller](handle,methodName,destructorsRef,args);var __emval_is_string=handle=>{handle=Emval.toValue(handle);return typeof handle=="string"};var __emval_new_array=()=>Emval.toHandle([]);var __emval_new_cstring=v=>Emval.toHandle(getStringOrSymbol(v));var __emval_new_object=()=>Emval.toHandle({});var __emval_run_destructors=handle=>{var destructors=Emval.toValue(handle);runDestructors(destructors);__emval_decref(handle)};var __emval_set_property=(handle,key,value)=>{handle=Emval.toValue(handle);key=Emval.toValue(key);value=Emval.toValue(value);handle[key]=value};var __tzset_js=(timezone,daylight,std_name,dst_name)=>{var currentYear=(new Date).getFullYear();var winter=new Date(currentYear,0,1);var summer=new Date(currentYear,6,1);var winterOffset=winter.getTimezoneOffset();var summerOffset=summer.getTimezoneOffset();var stdTimezoneOffset=Math.max(winterOffset,summerOffset);HEAPU32[timezone>>2]=stdTimezoneOffset*60;HEAP32[daylight>>2]=Number(winterOffset!=summerOffset);var extractZone=timezoneOffset=>{var sign=timezoneOffset>=0?"-":"+";var absOffset=Math.abs(timezoneOffset);var hours=String(Math.floor(absOffset/60)).padStart(2,"0");var minutes=String(absOffset%60).padStart(2,"0");return`UTC${sign}${hours}${minutes}`};var winterName=extractZone(winterOffset);var summerName=extractZone(summerOffset);if(summerOffset<winterOffset){stringToUTF8(winterName,std_name,17);stringToUTF8(summerName,dst_name,17)}else{stringToUTF8(winterName,dst_name,17);stringToUTF8(summerName,std_name,17)}};var getHeapMax=()=>2147483648;var alignMemory=(size,alignment)=>Math.ceil(size/alignment)*alignment;var growMemory=size=>{var oldHeapSize=wasmMemory.buffer.byteLength;var pages=(size-oldHeapSize+65535)/65536|0;try{wasmMemory.grow(pages);updateMemoryViews();return 1}catch(e){}};var _emscripten_resize_heap=requestedSize=>{var oldSize=HEAPU8.length;requestedSize>>>=0;var maxHeapSize=getHeapMax();if(requestedSize>maxHeapSize){return false}for(var cutDown=1;cutDown<=4;cutDown*=2){var overGrownHeapSize=oldSize*(1+.2/cutDown);overGrownHeapSize=Math.min(overGrownHeapSize,requestedSize+100663296);var newSize=Math.min(maxHeapSize,alignMemory(Math.max(requestedSize,overGrownHeapSize),65536));var replacement=growMemory(newSize);if(replacement){return true}}return false};var ENV={};var getExecutableName=()=>thisProgram;var getEnvStrings=()=>{if(!getEnvStrings.strings){var lang=(globalThis.navigator?.language??"C").replace("-","_")+".UTF-8";var env={USER:"web_user",LOGNAME:"web_user",PATH:"/",PWD:"/",HOME:"/home/web_user",LANG:lang,_:getExecutableName()};for(var x in ENV){if(ENV[x]===undefined)delete env[x];else env[x]=ENV[x]}var strings=[];for(var x in env){strings.push(`${x}=${env[x]}`)}getEnvStrings.strings=strings}return getEnvStrings.strings};var _environ_get=(__environ,environ_buf)=>{var bufSize=0;var envp=0;for(var string of getEnvStrings()){var ptr=environ_buf+bufSize;HEAPU32[__environ+envp>>2]=ptr;bufSize+=stringToUTF8(string,ptr,Infinity)+1;envp+=4}return 0};var _environ_sizes_get=(penviron_count,penviron_buf_size)=>{var strings=getEnvStrings();HEAPU32[penviron_count>>2]=strings.length;var bufSize=0;for(var string of strings){bufSize+=lengthBytesUTF8(string)+1}HEAPU32[penviron_buf_size>>2]=bufSize;return 0};{if(Module["noExitRuntime"])noExitRuntime=Module["noExitRuntime"];if(Module["print"])out=Module["print"];if(Module["printErr"])err=Module["printErr"];if(Module["arguments"])programArgs=Module["arguments"];if(Module["thisProgram"])thisProgram=Module["thisProgram"];var preInit=Module["preInit"];if(preInit){if(typeof preInit=="function")Module["preInit"]=preInit=[preInit];while(preInit.length>0){preInit.shift()()}}}var ___getTypeName,_free,_malloc,__emscripten_stack_restore,_emscripten_stack_get_current,memory,__indirect_function_table,wasmMemory,wasmTable;function assignWasmExports(wasmExports){___getTypeName=wasmExports["D"];_free=wasmExports["E"];_malloc=wasmExports["G"];__emscripten_stack_restore=wasmExports["H"];_emscripten_stack_get_current=wasmExports["I"];memory=wasmMemory=wasmExports["B"];__indirect_function_table=wasmTable=wasmExports["F"]}var wasmImports={b:___cxa_throw,u:__abort_js,q:__embind_register_bigint,y:__embind_register_bool,w:__embind_register_emval,p:__embind_register_float,g:__embind_register_function,l:__embind_register_integer,h:__embind_register_memory_view,x:__embind_register_std_string,o:__embind_register_std_wstring,z:__embind_register_void,f:__emval_create_invoker,a:__emval_decref,m:__emval_get_property,k:__emval_incref,e:__emval_invoke,A:__emval_is_string,j:__emval_new_array,c:__emval_new_cstring,n:__emval_new_object,d:__emval_run_destructors,i:__emval_set_property,t:__tzset_js,v:_emscripten_resize_heap,r:_environ_get,s:_environ_sizes_get};async function run(){preRun();var setStatus=Module["setStatus"];if(setStatus){setStatus("Running...");await new Promise(resolve=>setTimeout(resolve,1));setTimeout(setStatus,1,"")}if(ABORT)return;initRuntime();Module["onRuntimeInitialized"]?.();postRun()}var wasmExports;wasmExports=await createWasm();await run();
|
|
2
|
+
;return Module}export default LibENA;
|
package/dist/libena.wasm
ADDED
|
Binary file
|
package/package.json
CHANGED
|
@@ -1,13 +1,16 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@qe-libs/rena-wasm",
|
|
3
|
-
"version": "0.1.
|
|
4
|
-
"description": "JavaScript/WebAssembly ENA pipeline —
|
|
3
|
+
"version": "0.1.12",
|
|
4
|
+
"description": "JavaScript/WebAssembly ENA pipeline — libena (ENA rotations, node positions) compiled to WASM, over @qe-libs/libqe-wasm",
|
|
5
5
|
"main": "src/index.js",
|
|
6
6
|
"type": "module",
|
|
7
7
|
"files": [
|
|
8
|
+
"dist/libena.js",
|
|
9
|
+
"dist/libena.wasm",
|
|
8
10
|
"src/"
|
|
9
11
|
],
|
|
10
12
|
"scripts": {
|
|
13
|
+
"build": "sh scripts/build.sh",
|
|
11
14
|
"test": "node --experimental-vm-modules node_modules/.bin/jest"
|
|
12
15
|
},
|
|
13
16
|
"publishConfig": {
|
package/src/data.js
CHANGED
|
@@ -17,6 +17,32 @@ export function rowKey(row, cols) {
|
|
|
17
17
|
return cols.map(c => row[c]).join('__');
|
|
18
18
|
}
|
|
19
19
|
|
|
20
|
+
// rowKey() is the label callers see (and pass back as unitsUsed), but joining
|
|
21
|
+
// with '__' is ambiguous: {a: 'x__y', b: 'z'} and {a: 'x', b: 'y__z'} both give
|
|
22
|
+
// 'x__y__z'. Track the exact values behind each label and refuse to merge
|
|
23
|
+
// different ones under the same label.
|
|
24
|
+
function checkedKey(seen, row, cols, what) {
|
|
25
|
+
const key = rowKey(row, cols);
|
|
26
|
+
const exact = JSON.stringify(cols.map(c => row[c] ?? null));
|
|
27
|
+
const prev = seen.get(key);
|
|
28
|
+
if (prev === undefined) seen.set(key, exact);
|
|
29
|
+
else if (prev !== exact) {
|
|
30
|
+
throw new Error(`Different ${what} values both map to "${key}" because a value ` +
|
|
31
|
+
`contains "__"; rename the values or use different ${what} columns.`);
|
|
32
|
+
}
|
|
33
|
+
return key;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function requireColumns(kind, cols, available) {
|
|
37
|
+
if (!Array.isArray(cols) || cols.length === 0 || !cols.every(c => typeof c === 'string')) {
|
|
38
|
+
throw new Error(`${kind} must be a non-empty array of column names`);
|
|
39
|
+
}
|
|
40
|
+
const missing = cols.filter(c => !available.has(c));
|
|
41
|
+
if (missing.length) {
|
|
42
|
+
throw new Error(`${kind}: column(s) not found in the data: ${missing.join(', ')}`);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
20
46
|
/**
|
|
21
47
|
* Parse tabular data into ENA-ready structures.
|
|
22
48
|
*
|
|
@@ -55,9 +81,19 @@ export function parseData(rows, codes, unitCols, convoCols, unitsUsed = null) {
|
|
|
55
81
|
const convoGroups = new Map(); // convoIdx → [rowIdx, ...]
|
|
56
82
|
|
|
57
83
|
// Metadata columns: every column that is NOT a code, unit, or convo column.
|
|
58
|
-
const excludedCols = new Set([...codes, ...unitCols, ...convoCols]);
|
|
59
84
|
const allCols = nRows > 0 ? Object.keys(rows[0]) : [];
|
|
85
|
+
// A misspelled column used to read as undefined everywhere: all-zero codes,
|
|
86
|
+
// or every row in one unit called "undefined".
|
|
87
|
+
if (nRows > 0) {
|
|
88
|
+
const available = new Set(allCols);
|
|
89
|
+
requireColumns('codes', codes, available);
|
|
90
|
+
requireColumns('units', unitCols, available);
|
|
91
|
+
requireColumns('conversations', convoCols, available);
|
|
92
|
+
}
|
|
93
|
+
const excludedCols = new Set([...codes, ...unitCols, ...convoCols]);
|
|
60
94
|
const metaCols = allCols.filter(c => !excludedCols.has(c));
|
|
95
|
+
const unitSeen = new Map(); // label → exact values (see checkedKey)
|
|
96
|
+
const convoSeen = new Map();
|
|
61
97
|
|
|
62
98
|
// Per-unit metadata rows — populated on first occurrence of each unit.
|
|
63
99
|
const metaData = [];
|
|
@@ -67,13 +103,20 @@ export function parseData(rows, codes, unitCols, convoCols, unitsUsed = null) {
|
|
|
67
103
|
for (let i = 0; i < nRows; i++) {
|
|
68
104
|
const row = rows[i];
|
|
69
105
|
|
|
70
|
-
// Code values
|
|
106
|
+
// Code values: numbers, numeric strings or booleans. A missing or
|
|
107
|
+
// non-numeric value is an error rather than a silent 0.
|
|
71
108
|
for (let c = 0; c < nCodes; c++) {
|
|
72
|
-
|
|
109
|
+
const raw = row[codes[c]];
|
|
110
|
+
const v = typeof raw === 'string' && raw.trim() === '' ? NaN : Number(raw);
|
|
111
|
+
if (!Number.isFinite(v)) {
|
|
112
|
+
throw new Error(`Code "${codes[c]}" has a missing or non-numeric value ` +
|
|
113
|
+
`(${JSON.stringify(raw ?? null)}) in row ${i + 1}`);
|
|
114
|
+
}
|
|
115
|
+
codeMatrix[i * nCodes + c] = v;
|
|
73
116
|
}
|
|
74
117
|
|
|
75
118
|
// Unit index
|
|
76
|
-
const uKey =
|
|
119
|
+
const uKey = checkedKey(unitSeen, row, unitCols, 'unit');
|
|
77
120
|
if (used && !used.has(uKey)) {
|
|
78
121
|
unitOf[i] = -1;
|
|
79
122
|
} else if (!unitIndex.has(uKey)) {
|
|
@@ -87,7 +130,7 @@ export function parseData(rows, codes, unitCols, convoCols, unitsUsed = null) {
|
|
|
87
130
|
if (unitOf[i] !== -1) unitOf[i] = unitIndex.get(uKey);
|
|
88
131
|
|
|
89
132
|
// Conversation index
|
|
90
|
-
const cKey =
|
|
133
|
+
const cKey = checkedKey(convoSeen, row, convoCols, 'conversation');
|
|
91
134
|
if (!convoIndex.has(cKey)) convoIndex.set(cKey, convoIndex.size);
|
|
92
135
|
const cIdx = convoIndex.get(cKey);
|
|
93
136
|
convoOf[i] = cIdx;
|
package/src/index.js
CHANGED
|
@@ -2,9 +2,11 @@
|
|
|
2
2
|
* @qe-libs/rena-wasm
|
|
3
3
|
*
|
|
4
4
|
* JavaScript/WebAssembly ENA pipeline.
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
-
*
|
|
5
|
+
* Handles data parsing, unit/conversation grouping, and the full
|
|
6
|
+
* accumulate→normalize→center→rotate→project→node-positions pipeline. The
|
|
7
|
+
* ENA math (rotations, node positions, CCD) is libena, compiled into this
|
|
8
|
+
* package (dist/libena.*) with tma's accumulation (libtma); generic numerics
|
|
9
|
+
* come from @qe-libs/libqe-wasm.
|
|
8
10
|
*
|
|
9
11
|
* Output structure mirrors R's ena.set object (without R-specific S3 class
|
|
10
12
|
* attributes and without metadata columns prepended to every matrix).
|
|
@@ -41,6 +43,7 @@
|
|
|
41
43
|
*/
|
|
42
44
|
|
|
43
45
|
import loadLibQE from '@qe-libs/libqe-wasm';
|
|
46
|
+
import createLibENA from '../dist/libena.js';
|
|
44
47
|
import { parseData } from './data.js';
|
|
45
48
|
import {
|
|
46
49
|
sphereNorm, scaleNetworks, center,
|
|
@@ -294,18 +297,42 @@ function applyCodeMask(networks, codeMask, nCodes, nUnits, nConnections, ordered
|
|
|
294
297
|
|
|
295
298
|
// Rows of a matrix given as an array of row arrays, or as flat row-major data
|
|
296
299
|
// with `cols` columns.
|
|
297
|
-
function matrixRows(m, cols) {
|
|
300
|
+
function matrixRows(m, cols, name = 'rotation matrix') {
|
|
298
301
|
if (m == null) return null;
|
|
302
|
+
let rows;
|
|
299
303
|
if (Array.isArray(m) && (m.length === 0 || Array.isArray(m[0]) || ArrayBuffer.isView(m[0]))) {
|
|
300
|
-
|
|
304
|
+
rows = m.map(r => Array.from(r, Number));
|
|
305
|
+
} else {
|
|
306
|
+
const flat = Array.from(m, Number);
|
|
307
|
+
if (!cols) throw new Error(`a flat ${name} needs its column count`);
|
|
308
|
+
if (flat.length % cols !== 0) {
|
|
309
|
+
throw new Error(`${name} has ${flat.length} values, not a multiple of ${cols} columns`);
|
|
310
|
+
}
|
|
311
|
+
rows = [];
|
|
312
|
+
for (let i = 0; i < flat.length; i += cols) rows.push(flat.slice(i, i + cols));
|
|
313
|
+
}
|
|
314
|
+
// Ragged rows used to be laid out at a fixed stride: a long row overwrote
|
|
315
|
+
// the start of the next one, a short one left zeros.
|
|
316
|
+
const width = cols || (rows[0] ? rows[0].length : 0);
|
|
317
|
+
for (const r of rows) {
|
|
318
|
+
if (r.length !== width) throw new Error(`${name} rows must all have ${width} values`);
|
|
319
|
+
if (!r.every(Number.isFinite)) throw new Error(`${name} contains a non-numeric value`);
|
|
301
320
|
}
|
|
302
|
-
const flat = Array.from(m, Number);
|
|
303
|
-
if (!cols) throw new Error('a flat rotation matrix needs its column count');
|
|
304
|
-
const rows = [];
|
|
305
|
-
for (let i = 0; i < flat.length; i += cols) rows.push(flat.slice(i, i + cols));
|
|
306
321
|
return rows;
|
|
307
322
|
}
|
|
308
323
|
|
|
324
|
+
// The libqe statistics read nRows × dims values from `points`; a mismatch
|
|
325
|
+
// used to read past the array (leftover WASM heap memory) — libqe now throws,
|
|
326
|
+
// this gives the reason.
|
|
327
|
+
function checkPoints(name, points, nRows, dims) {
|
|
328
|
+
for (const [what, n] of [['row count', nRows], ['dims', dims]]) {
|
|
329
|
+
if (!Number.isInteger(n) || n < 0) throw new Error(`${name}: ${what} must be a non-negative integer`);
|
|
330
|
+
}
|
|
331
|
+
if (points == null || points.length !== nRows * dims) {
|
|
332
|
+
throw new Error(`${name}: expected ${nRows} × ${dims} = ${nRows * dims} values, got ${points?.length ?? 0}`);
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
|
|
309
336
|
/**
|
|
310
337
|
* Validate a supplied rotation set (= R's ena.rotation.set, e.g. another
|
|
311
338
|
* model's rotation) against this model's codes, and reorder it to this
|
|
@@ -330,7 +357,7 @@ function resolveRotationSet(rs, codes, nConnections, ordered) {
|
|
|
330
357
|
);
|
|
331
358
|
}
|
|
332
359
|
const rotCols = rot[0].length;
|
|
333
|
-
const nodes = matrixRows(rs.nodes, rotCols);
|
|
360
|
+
const nodes = matrixRows(rs.nodes, rotCols, 'rotationSet.nodes');
|
|
334
361
|
const center = rs.centerVec ? Array.from(rs.centerVec, Number) : null;
|
|
335
362
|
if (!nodes || nodes.length !== codes.length) {
|
|
336
363
|
throw new Error('rotationSet.nodes must have one row per code');
|
|
@@ -585,6 +612,53 @@ function runCustomRotation(qe, rawNetworks, lineWeights, ownCentered, ownCenterV
|
|
|
585
612
|
});
|
|
586
613
|
}
|
|
587
614
|
|
|
615
|
+
// ── WASM modules ─────────────────────────────────────────────────────────────
|
|
616
|
+
|
|
617
|
+
// Functions compiled into dist/libena.*: libena's (inst/include/libena; moved
|
|
618
|
+
// out of libqe in libqe's phase 4a split) and libtma's accumulation (tma's
|
|
619
|
+
// inst/include/libtma; phase 5). libqe-wasm still carries its own copies
|
|
620
|
+
// until libqe 0.2.0, so these names must route to dist/libena.
|
|
621
|
+
const LIBENA_FUNCTIONS = new Set([
|
|
622
|
+
'ena_correlation',
|
|
623
|
+
'node_positions',
|
|
624
|
+
'directed_node_positions',
|
|
625
|
+
'directed_node_positions_combine_pairs',
|
|
626
|
+
'ena_svd',
|
|
627
|
+
'deflate',
|
|
628
|
+
'orthogonal_svd',
|
|
629
|
+
'complete_rotation',
|
|
630
|
+
'means_rotation',
|
|
631
|
+
'generalized_means_rotation',
|
|
632
|
+
'ccd_window',
|
|
633
|
+
// libtma
|
|
634
|
+
'connection_matrix',
|
|
635
|
+
'accumulate_stanza',
|
|
636
|
+
'row_connections',
|
|
637
|
+
'rolling_window_sum',
|
|
638
|
+
'flat_index',
|
|
639
|
+
'accumulate_unit',
|
|
640
|
+
'accumulate_unit_with_rows',
|
|
641
|
+
'accumulate_tensor_unit',
|
|
642
|
+
'aggregate_row_connections',
|
|
643
|
+
'finalize_row_connections',
|
|
644
|
+
]);
|
|
645
|
+
|
|
646
|
+
/**
|
|
647
|
+
* Load libqe-wasm and libena and present them as one module object: the
|
|
648
|
+
* pipeline (pipeline.js, tensor.js, ...) takes a single `qe`, so libena's
|
|
649
|
+
* functions are routed to libena and everything else to libqe-wasm.
|
|
650
|
+
*/
|
|
651
|
+
async function loadModules() {
|
|
652
|
+
const [libqe, libena] = await Promise.all([loadLibQE(), createLibENA()]);
|
|
653
|
+
return new Proxy(libqe, {
|
|
654
|
+
get(target, prop, receiver) {
|
|
655
|
+
return LIBENA_FUNCTIONS.has(prop)
|
|
656
|
+
? libena[prop]
|
|
657
|
+
: Reflect.get(target, prop, receiver);
|
|
658
|
+
},
|
|
659
|
+
});
|
|
660
|
+
}
|
|
661
|
+
|
|
588
662
|
// ── main factory ─────────────────────────────────────────────────────────────
|
|
589
663
|
|
|
590
664
|
/**
|
|
@@ -593,7 +667,7 @@ function runCustomRotation(qe, rawNetworks, lineWeights, ownCentered, ownCenterV
|
|
|
593
667
|
* @returns {Promise<{fit: Function, accumulate: Function}>}
|
|
594
668
|
*/
|
|
595
669
|
export default async function loadENA() {
|
|
596
|
-
const qe = await
|
|
670
|
+
const qe = await loadModules();
|
|
597
671
|
|
|
598
672
|
const api = {
|
|
599
673
|
/**
|
|
@@ -902,6 +976,7 @@ export default async function loadENA() {
|
|
|
902
976
|
* rows=dims, cols=3 — columns: mean, lower CI, upper CI
|
|
903
977
|
*/
|
|
904
978
|
confInts(points, nUnits, dims, confLevel = 0.95) {
|
|
979
|
+
checkPoints('confInts', points, nUnits, dims);
|
|
905
980
|
return qe.mean_ci(points, nUnits, dims, confLevel);
|
|
906
981
|
},
|
|
907
982
|
|
|
@@ -917,6 +992,7 @@ export default async function loadENA() {
|
|
|
917
992
|
* rows=dims, cols=2 — columns: lower fence, upper fence
|
|
918
993
|
*/
|
|
919
994
|
outlierInts(points, nUnits, dims, iqrFactor = 1.5) {
|
|
995
|
+
checkPoints('outlierInts', points, nUnits, dims);
|
|
920
996
|
return qe.outlier_ci(points, nUnits, dims, iqrFactor);
|
|
921
997
|
},
|
|
922
998
|
|
|
@@ -933,6 +1009,8 @@ export default async function loadENA() {
|
|
|
933
1009
|
* U, pvalue_u, effect_r, medians }}
|
|
934
1010
|
*/
|
|
935
1011
|
compareGroups(g1Points, nG1, g2Points, nG2, dims) {
|
|
1012
|
+
checkPoints('compareGroups (group 1)', g1Points, nG1, dims);
|
|
1013
|
+
checkPoints('compareGroups (group 2)', g2Points, nG2, dims);
|
|
936
1014
|
return qe.group_stats(g1Points, nG1, dims, g2Points, nG2, dims);
|
|
937
1015
|
},
|
|
938
1016
|
|
|
@@ -1124,7 +1202,11 @@ export default async function loadENA() {
|
|
|
1124
1202
|
*/
|
|
1125
1203
|
defaultTensor,
|
|
1126
1204
|
|
|
1127
|
-
/**
|
|
1205
|
+
/**
|
|
1206
|
+
* The underlying WASM functions, for custom low-level pipelines:
|
|
1207
|
+
* libqe-wasm's, with libena's (rotations, node positions, CCD) from
|
|
1208
|
+
* this package.
|
|
1209
|
+
*/
|
|
1128
1210
|
qe,
|
|
1129
1211
|
};
|
|
1130
1212
|
|
package/src/tensor.js
CHANGED
|
@@ -82,8 +82,16 @@ export function buildContextLookup(rows, rowIndices, factors, factorLevels) {
|
|
|
82
82
|
const row = rows[rowIndices[i]];
|
|
83
83
|
times[i] = i; // default: row index as time
|
|
84
84
|
for (let f = 0; f < nFactor; f++) {
|
|
85
|
-
const col
|
|
86
|
-
|
|
85
|
+
const col = factors[f];
|
|
86
|
+
const levels = factorLevels[col];
|
|
87
|
+
// Own properties only: a value like "constructor" must not resolve
|
|
88
|
+
// to an inherited Object.prototype member. A value with no level
|
|
89
|
+
// used to fall back to level 0 silently.
|
|
90
|
+
if (!levels || !Object.prototype.hasOwnProperty.call(levels, row[col])) {
|
|
91
|
+
throw new Error(`tensor factor "${col}" has no level for value ` +
|
|
92
|
+
`${JSON.stringify(row[col] ?? null)}`);
|
|
93
|
+
}
|
|
94
|
+
contextLookup[i * nFactor + f] = levels[row[col]];
|
|
87
95
|
}
|
|
88
96
|
}
|
|
89
97
|
|
|
@@ -221,7 +229,9 @@ export function parseTimes(rows, col, endCol = null) {
|
|
|
221
229
|
export function inferFactorLevels(rows, factors) {
|
|
222
230
|
const levels = {};
|
|
223
231
|
for (const col of factors) {
|
|
224
|
-
|
|
232
|
+
// No prototype: values such as "constructor" or "__proto__" are
|
|
233
|
+
// ordinary keys here.
|
|
234
|
+
levels[col] = Object.create(null);
|
|
225
235
|
let idx = 0;
|
|
226
236
|
for (const row of rows) {
|
|
227
237
|
const v = row[col];
|
|
@@ -233,6 +243,39 @@ export function inferFactorLevels(rows, factors) {
|
|
|
233
243
|
return levels;
|
|
234
244
|
}
|
|
235
245
|
|
|
246
|
+
/**
|
|
247
|
+
* Check a tensor definition before it reaches libqe, which indexes the
|
|
248
|
+
* tensor with these values (an index out of range used to read past it).
|
|
249
|
+
*/
|
|
250
|
+
export function validateTensor(dims, tensorData, factors, factorLevels) {
|
|
251
|
+
if (!Array.isArray(dims) && !ArrayBuffer.isView(dims)) {
|
|
252
|
+
throw new Error('tensor dims must be an array');
|
|
253
|
+
}
|
|
254
|
+
const d = Array.from(dims);
|
|
255
|
+
if (d.length === 0 || !d.every(x => Number.isInteger(x) && x > 0)) {
|
|
256
|
+
throw new Error('tensor dims must be positive integers');
|
|
257
|
+
}
|
|
258
|
+
const size = d.reduce((a, b) => a * b, 1);
|
|
259
|
+
if (size !== tensorData.length) {
|
|
260
|
+
throw new Error(`tensor data has ${tensorData.length} values but dims ` +
|
|
261
|
+
`[${d.join(', ')}] need ${size}`);
|
|
262
|
+
}
|
|
263
|
+
if (factors.length > 0 && d.length !== factors.length + 1) {
|
|
264
|
+
throw new Error(`tensor dims need one axis per factor (${factors.length}) ` +
|
|
265
|
+
`plus the weight/window axis; got ${d.length}`);
|
|
266
|
+
}
|
|
267
|
+
factors.forEach((col, f) => {
|
|
268
|
+
const levels = factorLevels[col];
|
|
269
|
+
if (!levels) throw new Error(`no factor levels for tensor factor "${col}"`);
|
|
270
|
+
for (const [value, level] of Object.entries(levels)) {
|
|
271
|
+
if (!Number.isInteger(level) || level < 0 || level >= d[f]) {
|
|
272
|
+
throw new Error(`level ${level} of "${col}" = ${JSON.stringify(value)} ` +
|
|
273
|
+
`is outside its axis (size ${d[f]})`);
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
});
|
|
277
|
+
}
|
|
278
|
+
|
|
236
279
|
// libqe's apply_tensor_unit reads the IS_DEFAULT window as an int, so an
|
|
237
280
|
// infinite window (or one too large for int32) is clamped to INT32_MAX — still
|
|
238
281
|
// longer than any conversation, in lines or in seconds.
|
|
@@ -354,6 +397,7 @@ export function accumulateTensor(qe, rows, codeMatrix, nRows, nCodes, nUnits,
|
|
|
354
397
|
|
|
355
398
|
// Resolve factor levels (infer if not provided)
|
|
356
399
|
const factorLevels = tensorDef.factorLevels ?? inferFactorLevels(rows, factors);
|
|
400
|
+
validateTensor(dims, tensorData, factors, factorLevels);
|
|
357
401
|
const allTimes = timesCol ? parseTimes(rows, timesCol, timesEndCol || null) : null;
|
|
358
402
|
|
|
359
403
|
const nConnections = ordered
|