Files
Robot_Arm/ml/prep/npy.cjs
T

14 lines
848 B
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/* 最小 .npy 写出(float32 / int32,C 序) */
'use strict';
const fs = require('fs');
function writeNpy(file, arr, shape, dtype) {
const descr = dtype === 'int32' ? '<i4' : '<f4';
let hdr = `{'descr': '${descr}', 'fortran_order': False, 'shape': (${shape.join(', ')}${shape.length === 1 ? ',' : ''}), }`;
const pre = 10, total = Math.ceil((pre + hdr.length + 1) / 64) * 64;
hdr = hdr + ' '.repeat(total - pre - hdr.length - 1) + '\n';
const h = Buffer.alloc(pre); h.write('\x93NUMPY', 0, 'latin1'); h[6] = 1; h[7] = 0; h.writeUInt16LE(hdr.length, 8);
const ta = dtype === 'int32' ? Int32Array.from(arr) : (arr instanceof Float32Array ? arr : Float32Array.from(arr));
fs.writeFileSync(file, Buffer.concat([h, Buffer.from(hdr, 'latin1'), Buffer.from(ta.buffer, ta.byteOffset, ta.byteLength)]));
}
module.exports = { writeNpy };