123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117 |
- import assert from "assert";
- import {promises as fs} from "fs";
- import path from "path";
- import http from "http";
- import N from "../index.js";
- describe("npyjs parser", function () {
- it("should correctly parse npy files", async function () {
- const server = http.createServer(async function (req, res) {
- const fpath = path.resolve(req.url.slice(1));
- const data = await fs.readFile(fpath);
- res.writeHead(200);
- res.end(data);
- });
- server.listen();
- const {port} = server.address()
- const records = JSON.parse(await fs.readFile("test/records.json"));
- const n = new N();
- for (const fname in records) {
- const fpath = path.join("test", `${fname}.npy`)
- const data = await n.load(`http://localhost:${port}/${fpath}`)
-
- // Get the last 5 values for comparison
- const resultValues = Array.prototype.slice.call(
- data.data.slice(-5)
- );
-
- // Compare with expected values
- resultValues.forEach((actual, j) => {
- const expected = records[fname][j];
- // Use approximate equality for floating point comparisons
- if (data.dtype.includes('float')) {
- assert.ok(
- Math.abs(actual - expected) < 1e-5,
- `${fname}: Expected ${expected} but got ${actual} at index ${j}`
- );
- } else if (data.dtype.includes('int64') || data.dtype.includes('uint64')) {
- // Convert BigInt to string for comparison
- assert.strictEqual(
- actual.toString(),
- expected.toString(),
- `${fname}: Expected ${expected} but got ${actual} at index ${j}`
- );
- } else {
- assert.strictEqual(
- actual,
- expected,
- `${fname}: Expected ${expected} but got ${actual} at index ${j}`
- );
- }
- });
- }
- server.close();
- });
- // Add specific test for float16 conversion
- it("should correctly convert float16 to float32", function() {
- const n = new N();
-
- // Test some known float16 to float32 conversions
- const testCases = [
- { input: 0x0000, expected: 0 }, // Zero
- { input: 0x8000, expected: -0 }, // Negative zero
- { input: 0x3C00, expected: 1 }, // One
- { input: 0xBC00, expected: -1 }, // Negative one
- { input: 0x7C00, expected: Infinity }, // Infinity
- { input: 0xFC00, expected: -Infinity }, // Negative infinity
- { input: 0x7E00, expected: NaN }, // NaN
- { input: 0x3200, expected: 0.1875 } // 1.5 * 2^-9
- ];
- testCases.forEach(({input, expected}) => {
- const result = N.float16ToFloat32(input);
- if (Number.isNaN(expected)) {
- assert.ok(Number.isNaN(result), `Expected NaN for input 0x${input.toString(16)}`);
- } else {
- assert.strictEqual(
- result,
- expected,
- `Failed converting 0x${input.toString(16)}: expected ${expected}, got ${result}`
- );
- }
- });
- });
- it("should handle float16 data based on conversion flag", async function() {
- const server = http.createServer(async function (req, res) {
- const fpath = path.resolve(req.url.slice(1));
- const data = await fs.readFile(fpath);
- res.writeHead(200);
- res.end(data);
- });
- server.listen();
- const {port} = server.address();
- // Test with conversion enabled (default)
- const nWithConversion = new N();
- const dataConverted = await nWithConversion.load(
- `http://localhost:${port}/test/data/10-float16.npy`
- );
- assert.ok(dataConverted.data instanceof Float32Array,
- "With conversion enabled, should return Float32Array");
- // Test with conversion disabled
- const nWithoutConversion = new N({ convertFloat16: false });
- const dataRaw = await nWithoutConversion.load(
- `http://localhost:${port}/test/data/10-float16.npy`
- );
- assert.ok(dataRaw.data instanceof Uint16Array,
- "With conversion disabled, should return Uint16Array");
- server.close();
- });
- });
|