it(`should encode/decode variant ${objecType}`, () => { const dataType = data[objecType] dataType.forEach((value) => { const encoded = GdCom.putVar(value) const decoded = GdCom.getVar(encoded) if (/Float/i.test(objecType)) { expect(decoded.value).to.be.closeTo(value, 0.00001) } else { expect(decoded.value).to.deep.equal(value) } }) })
it('Float32', () => { const buf = Buffer.alloc(16); serialize.Array.float32([0.00000012, 19875664,-2456654,-0.011], buf, 0, 4); const bufferOffset = [0]; expect(deserialize.float32(buf, bufferOffset)).to.be.closeTo(0.00000012, 1e-7); expect(bufferOffset[0]).to.equal(4); expect(deserialize.float32(buf, bufferOffset)).to.be.closeTo(19875664, 1e-7); expect(bufferOffset[0]).to.equal(8); expect(deserialize.float32(buf, bufferOffset)).to.be.closeTo(-2456654, 1e-7); expect(bufferOffset[0]).to.equal(12); expect(deserialize.float32(buf, bufferOffset)).to.be.closeTo(-0.011, 1e-7); expect(bufferOffset[0]).to.equal(16); });
it('should encode/decode variant Vector2', () => { const dataType = [ { x: 1, y: 2 }, { x: -1, y: -2 }, { x: -42.4, y: 0.15 } ] dataType.forEach((value) => { const encoded = GdCom.putVar(value, GdCom.TYPE.VECTOR2) const decoded = GdCom.getVar(encoded) expect(decoded.value.x).to.be.closeTo(value.x, 0.00001) expect(decoded.value.y).to.be.closeTo(value.y, 0.00001) }) })
describe('BLE', function () { it('should multiply each channel by the proper scale value', function () { let chanArr = k.channelSettingsArrayInit(k.OBCINumberOfChannelsCytonBLE); // Not in daisy mode let scaleFactor = 4.5 / 24 / (Math.pow(2, 23) - 1); // Call the function under test let valueArray = openBCIUtilities.getChannelDataArray({ rawDataPacket: sampleBuf, channelSettings: chanArr, protocol: k.OBCIProtocolBLE }); for (let j = 0; j < k.OBCINumberOfChannelsCytonBLE; j++) { // console.log(`channel data ${j + 1}: ${valueArray[j]} : actual ${scaleFactor * (j + 1)}`); expect(valueArray[j]).to.be.closeTo(scaleFactor * (j + 1), 0.0001); } }); });
it('Float32 Array', () => { const buf = Buffer.alloc(28); serialize.Array.float32([0.00000012, 19875664], buf, 0, 2); serialize.Array.float32([-2456654], buf, 8, -1); serialize.Array.float32([-0.011, 12], buf, 16, -1); const bufferOffset = [0]; let arr = deserialize.Array.float32(buf, bufferOffset, 2); expect(bufferOffset[0]).to.equal(8); expect(arr.length).to.equal(2); expect(arr[0]).to.be.closeTo(0.00000012, 1e-7); expect(arr[1]).to.be.closeTo(19875664, 1e-7); arr = deserialize.Array.float32(buf, bufferOffset, -1); expect(bufferOffset[0]).to.equal(16); expect(arr.length).to.equal(1); expect(arr[0]).to.be.closeTo(-2456654, 1e-7); arr = deserialize.Array.float32(buf, bufferOffset, -1); expect(bufferOffset[0]).to.equal(28); expect(arr.length).to.equal(2); expect(arr[0]).to.be.closeTo(-0.011, 1e-7); expect(arr[1]).to.be.closeTo(12, 1e-7); });
describe('network.propagate()', function () { it('network.propagate(rate, momentum, update, target_output) => {undefined}', function () { const upper_test_epoch_limit = 2000; // will attempt to propagate this many times const test_network = createUsedNetwork(); // train the network to output all 1s. const input_size = test_network.input_nodes.size; const output_size = test_network.output_nodes.size; const ideal_output = Array(output_size).fill(1); for (let i = 0; i < upper_test_epoch_limit; i++) { const random_input = Array(input_size).fill(0).map(() => Math.random()); test_network.activate(random_input); test_network.propagate(0.25, 0.01, true, ideal_output); } const random_input = Array(input_size).fill(0).map(() => Math.random()); const test_output = test_network.activate(random_input); const epsilon = 0.08; test_output.forEach((value, index) => { expect(value).to.be.closeTo(ideal_output[index], epsilon); }); }) })
it('Float64', () => { const buf = Buffer.alloc(32); serialize.Array.float64([0.00000012, -2456654,2.23e-16,19875665], buf, 0, 4); const bufferOffset = [0]; expect(deserialize.float64(buf, bufferOffset)).to.be.closeTo(0.00000012, Number.EPSILON); expect(bufferOffset[0]).to.equal(8); expect(deserialize.float64(buf, bufferOffset)).to.be.closeTo(-2456654, Number.EPSILON); expect(bufferOffset[0]).to.equal(16); expect(deserialize.float64(buf, bufferOffset)).to.be.closeTo(2.23e-16, Number.EPSILON); expect(bufferOffset[0]).to.equal(24); expect(deserialize.float64(buf, bufferOffset)).to.be.closeTo(19875665, Number.EPSILON); expect(bufferOffset[0]).to.equal(32); });
describe('gd-com binary serializer', () => { for (let objecType in data) { it(`should encode/decode ${objecType}`, () => { let dataType = data[objecType] return dataType.reduce((promise, value) => { return promise .then(() => gdCom.encode(value)) .then((encoded) => gdCom.decode(encoded)) .then((decoded) => { if (/Float/i.test(objecType)) { expect(decoded).to.be.closeTo(value, 0.00001) } else { expect(decoded).to.deep.equal(value) } }) }, Promise.resolve()) }) } })
it('should encode/decode variant AABB', () => { const dataType = [ { x_coordinate: 1, y_coordinate: 2, z_coordinate: 3, x_size: 4, y_size: 5, z_size: 6 }, { x_coordinate: -1, y_coordinate: -2, z_coordinate: -3, x_size: -4, y_size: -5, z_size: -6 }, { x_coordinate: 1.05, y_coordinate: -42.852, z_coordinate: 3.52, x_size: 4, y_size: 5, z_size: 6 } ] dataType.forEach((value) => { const encoded = GdCom.putVar(value, GdCom.TYPE.AABB) const decoded = GdCom.getVar(encoded) expect(decoded.value.x_coordinate).to.be.closeTo(value.x_coordinate, 0.00001) expect(decoded.value.y_coordinate).to.be.closeTo(value.y_coordinate, 0.00001) expect(decoded.value.z_coordinate).to.be.closeTo(value.z_coordinate, 0.00001) expect(decoded.value.x_size).to.be.closeTo(value.x_size, 0.00001) expect(decoded.value.y_size).to.be.closeTo(value.y_size, 0.00001) expect(decoded.value.z_size).to.be.closeTo(value.z_size, 0.00001) }) })
it('should encode/decode variant Color', () => { const dataType = [ { r: 0.1, g: 1, b: 0.5, a: 1 }, { r: 1, g: 0, b: 0.5, a: 1 }, { r: 0, g: 0.5, b: 1, a: 1 } ] dataType.forEach((value) => { const encoded = GdCom.putVar(value, GdCom.TYPE.COLOR) const decoded = GdCom.getVar(encoded) expect(decoded.value.r).to.be.closeTo(value.r, 0.00001) expect(decoded.value.g).to.be.closeTo(value.g, 0.00001) expect(decoded.value.b).to.be.closeTo(value.b, 0.00001) expect(decoded.value.a).to.be.closeTo(value.a, 0.00001) }) })
it('should encode/decode variant Rect2', () => { const dataType = [ { x_coordinate: 1, y_coordinate: 2, x_size: 3, y_size: 4 }, { x_coordinate: -1, y_coordinate: -2, x_size: -3, y_size: -4 }, { x_coordinate: -42.54, y_coordinate: 0.520, x_size: 0.520, y_size: -42.54 } ] dataType.forEach((value) => { const encoded = GdCom.putVar(value, GdCom.TYPE.RECT2) const decoded = GdCom.getVar(encoded) expect(decoded.value.x_coordinate).to.be.closeTo(value.x_coordinate, 0.00001) expect(decoded.value.y_coordinate).to.be.closeTo(value.y_coordinate, 0.00001) expect(decoded.value.x_size).to.be.closeTo(value.x_size, 0.00001) expect(decoded.value.y_size).to.be.closeTo(value.y_size, 0.00001) }) })
it('should encode/decode variant Vector3', () => { const dataType = [ { x: 1, y: 2, z: 3 }, { x: -1, y: -2, z: -0.520 }, { x: -42.54, y: 0.520, z: 0.520 } ] dataType.forEach((value) => { const encoded = GdCom.putVar(value, GdCom.TYPE.VECTOR3) const decoded = GdCom.getVar(encoded) expect(decoded.value.x).to.be.closeTo(value.x, 0.00001) expect(decoded.value.y).to.be.closeTo(value.y, 0.00001) expect(decoded.value.z).to.be.closeTo(value.z, 0.00001) }) })
it('should encode/decode variant Quat', () => { const dataType = [ { x: 1, y: 2, z: 3, w: 2 }, { x: -1, y: -2, z: -0.520, w: 2 }, { x: -42.54, y: 0.520, z: 0.520, w: 2 } ] dataType.forEach((value) => { const encoded = GdCom.putVar(value, GdCom.TYPE.QUATERNION) const decoded = GdCom.getVar(encoded) expect(decoded.value.x).to.be.closeTo(value.x, 0.00001) expect(decoded.value.y).to.be.closeTo(value.y, 0.00001) expect(decoded.value.z).to.be.closeTo(value.z, 0.00001) expect(decoded.value.w).to.be.closeTo(value.w, 0.00001) }) })
it('should encode/decode variant Plane', () => { const dataType = [ { x: 1, y: 2, z: 3, distance: 2 }, { x: -1, y: -2, z: -0.520, distance: 2 }, { x: -42.54, y: 0.520, z: 0.520, distance: 2 } ] dataType.forEach((value) => { const encoded = GdCom.putVar(value, GdCom.TYPE.PLANE) const decoded = GdCom.getVar(encoded) expect(decoded.value.x).to.be.closeTo(value.x, 0.00001) expect(decoded.value.y).to.be.closeTo(value.y, 0.00001) expect(decoded.value.z).to.be.closeTo(value.z, 0.00001) expect(decoded.value.distance).to.be.closeTo(value.distance, 0.00001) }) })