Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions tests/gui/data/functional.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import puppeteer from 'puppeteer'

/** @type {puppeteer.Browser} */
let browser
beforeAll(async () => {
browser = await puppeteer.launch({ args: ['--no-sandbox'] })
})

afterAll(async () => {
await browser.close()
})

describe('classification', () => {
/** @type {puppeteer.Page} */
let page
beforeEach(async () => {
page = await browser.newPage()
await page.goto(`http://${process.env.SERVER_HOST}/`)
page.on('console', message => console.log(`${message.type().substring(0, 3).toUpperCase()} ${message.text()}`))
.on('pageerror', ({ message }) => console.log(message))
.on('requestfailed', request => console.log(`${request.failure().errorText} ${request.url()}`))
await page.waitForSelector('#data_menu > *')
}, 10000)

test('initialize', async () => {
const dataSelectBox = await page.waitForSelector('#ml_selector dl:first-child dd:nth-child(2) select')
dataSelectBox.select('functional')

const dataMenu = await page.waitForSelector('#ml_selector #data_menu')
const dimensionTextBox = await dataMenu.waitForSelector('input[name=dim]')
const dimension = await (await dimensionTextBox.getProperty('value')).jsonValue()
expect(dimension).toBe('1')
}, 10000)
})
2 changes: 1 addition & 1 deletion tests/lib/model/diffusion_map.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { jest } from '@jest/globals'
jest.retryTimes(5)
jest.retryTimes(10)

import Matrix from '../../../lib/util/matrix.js'
import DiffusionMap from '../../../lib/model/diffusion_map.js'
Expand Down
14 changes: 14 additions & 0 deletions tests/lib/model/growing_cell_structures.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import Matrix from '../../../lib/util/matrix.js'
import GrowingCellStructures from '../../../lib/model/growing_cell_structures.js'

test('clustering', () => {
const model = new GrowingCellStructures()
const n = 50
const x = Matrix.concat(Matrix.randn(n, 2, 0, 0.1), Matrix.randn(n, 2, 5, 0.1)).toArray()

for (let i = 0; i < 100; i++) {
model.fit(x)
}
const y = model.predict(x)
expect(y).toHaveLength(x.length)
})
28 changes: 25 additions & 3 deletions tests/lib/model/kernel_density_estimator.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,30 @@ import KernelDensityEstimator from '../../../lib/model/kernel_density_estimator.

import { correlation } from '../../../lib/evaluate/regression.js'

test('density estimation', () => {
const model = new KernelDensityEstimator()
test.each([undefined, 'gaussian', 'triangular', 'epanechnikov', 'biweight', 'triweight'])(
'density estimation %s',
kernel => {
const model = new KernelDensityEstimator(kernel)
const n = 500
const x = Matrix.concat(Matrix.randn(n, 2, 0, 0.1), Matrix.randn(n, 2, 5, 0.1)).toArray()

model.fit(x)
const y = model.predict(x)
expect(y).toHaveLength(x.length)

const p = []
for (let i = 0; i < x.length; i++) {
const p1 = Math.exp(-x[i].reduce((s, v) => s + v ** 2, 0) / (2 * 0.1)) / (2 * Math.PI * 0.1)
const p2 = Math.exp(-x[i].reduce((s, v) => s + (v - 5) ** 2, 0) / (2 * 0.1)) / (2 * Math.PI * 0.1)
p[i] = (p1 + p2) / 2
}
const corr = correlation(y, p)
expect(corr).toBeGreaterThan(0.9)
}
)

test.each(['rectangular'])('density estimation %s', kernel => {
const model = new KernelDensityEstimator(kernel)
const n = 500
const x = Matrix.concat(Matrix.randn(n, 2, 0, 0.1), Matrix.randn(n, 2, 5, 0.1)).toArray()

Expand All @@ -22,5 +44,5 @@ test('density estimation', () => {
p[i] = (p1 + p2) / 2
}
const corr = correlation(y, p)
expect(corr).toBeGreaterThan(0.9)
expect(corr).toBeGreaterThan(0.8)
})
95 changes: 77 additions & 18 deletions tests/lib/model/maxabs.test.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,90 @@
import Matrix from '../../../lib/util/matrix.js'
import MaxAbsScaler from '../../../lib/model/maxabs.js'

test('fit', () => {
test('mat mat', () => {
const model = new MaxAbsScaler()
const x = Matrix.randn(50, 2, 1, 0.2)
const xabsmax = Matrix.map(x, Math.abs).max(0).value
model.fit(x.toArray())
const y = model.predict(x.toArray())

const min = Array(2).fill(Infinity)
const max = Array(2).fill(-Infinity)
const absmax = Array(2).fill(0)
for (let i = 0; i < x.rows; i++) {
for (let k = 0; k < x.cols; k++) {
expect(y[i][k]).toBeCloseTo(x.at(i, k) / xabsmax[k])
min[k] = Math.min(min[k], y[i][k])
max[k] = Math.max(max[k], y[i][k])
absmax[k] = Math.max(max[k], Math.abs(y[i][k]))

const x1 = Matrix.randn(50, 2, 0, 0.2)
const y = model.predict(x1.toArray())

for (let i = 0; i < x1.rows; i++) {
for (let j = 0; j < x1.cols; j++) {
expect(y[i][j]).toBeCloseTo(x1.at(i, j) / xabsmax[j])
}
}
for (let k = 0; k < min.length; k++) {
expect(min[k]).toBeGreaterThanOrEqual(-1)
})

test('mat arr', () => {
const model = new MaxAbsScaler()
const x = Matrix.randn(50, 2, 1, 0.2)
const xabsmax = Matrix.map(x, Math.abs).max(0).value
model.fit(x.toArray())

const x1 = Matrix.randn(50, 1, 0, 0.2)
const y = model.predict(x1.value)

for (let i = 0; i < x1.rows; i++) {
expect(y[i]).toBeCloseTo(x1.at(i, 0) / xabsmax[0])
}
for (let k = 0; k < max.length; k++) {
expect(max[k]).toBeLessThanOrEqual(1)
})

test('mat 0', () => {
const model = new MaxAbsScaler()
const x = Matrix.zeros(50, 2)
model.fit(x.toArray())

const x1 = Matrix.randn(50, 2, 0, 0.2)
const y = model.predict(x1.toArray())

for (let i = 0; i < x1.rows; i++) {
for (let j = 0; j < x1.cols; j++) {
expect(y[i][j]).toBeCloseTo(x1.at(i, j))
}
}
for (let k = 0; k < absmax.length; k++) {
expect(absmax[k]).toBeCloseTo(1)
})

test('arr mat', () => {
const model = new MaxAbsScaler()
const x = Matrix.randn(50, 1, 1, 0.2)
const xabsmax = Matrix.map(x, Math.abs).max()
model.fit(x.value)

const x1 = Matrix.randn(50, 2, 0, 0.2)
const y = model.predict(x1.toArray())

for (let i = 0; i < x1.rows; i++) {
for (let j = 0; j < x1.cols; j++) {
expect(y[i][j]).toBeCloseTo(x1.at(i, j) / xabsmax)
}
}
})

test('arr arr', () => {
const model = new MaxAbsScaler()
const x = Matrix.randn(50, 1, 1, 0.2)
const xabsmax = Matrix.map(x, Math.abs).max()
model.fit(x.value)

const x1 = Matrix.randn(50, 1, 0, 0.2)
const y = model.predict(x1.value)

for (let i = 0; i < x1.rows; i++) {
expect(y[i]).toBeCloseTo(x1.at(i, 0) / xabsmax)
}
})

test('arr 0', () => {
const model = new MaxAbsScaler()
const x = Matrix.zeros(50, 1, 1, 0.2)
model.fit(x.value)

const x1 = Matrix.randn(50, 1, 0, 0.2)
const y = model.predict(x1.value)

for (let i = 0; i < x1.rows; i++) {
expect(y[i]).toBeCloseTo(x1.at(i, 0))
}
})
95 changes: 82 additions & 13 deletions tests/lib/model/minmax.test.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,96 @@
import Matrix from '../../../lib/util/matrix.js'
import MinmaxNormalization from '../../../lib/model/minmax.js'

test('fit', () => {
test('mat mat', () => {
const model = new MinmaxNormalization()
const x = Matrix.randn(50, 2, 1, 0.2)
model.fit(x.toArray())
const y = model.predict(x.toArray())

const x1 = Matrix.randn(50, 2, 0, 0.2)
const y = model.predict(x1.toArray())

const xmin = x.min(0).value
const xmax = x.max(0).value
for (let i = 0; i < x1.rows; i++) {
for (let k = 0; k < x1.cols; k++) {
expect(y[i][k]).toBeCloseTo((x1.at(i, k) - xmin[k]) / (xmax[k] - xmin[k]))
}
}
})

test('mat arr', () => {
const model = new MinmaxNormalization()
const x = Matrix.randn(50, 2, 1, 0.2)
model.fit(x.toArray())

const x1 = Matrix.randn(50, 1, 0, 0.2)
const y = model.predict(x1.value)

const xmin = x.min(0).value
const xmax = x.max(0).value
const min = Array(2).fill(Infinity)
const max = Array(2).fill(-Infinity)
for (let i = 0; i < x.rows; i++) {
for (let k = 0; k < x.cols; k++) {
expect(y[i][k]).toBeCloseTo((x.at(i, k) - xmin[k]) / (xmax[k] - xmin[k]))
min[k] = Math.min(min[k], y[i][k])
max[k] = Math.max(max[k], y[i][k])
for (let i = 0; i < x1.rows; i++) {
expect(y[i]).toBeCloseTo((x1.at(i, 0) - xmin[0]) / (xmax[0] - xmin[0]))
}
})

test('mat same', () => {
const model = new MinmaxNormalization()
const r = Math.random()
const x = new Matrix(50, 2, r)
model.fit(x.toArray())

const x1 = Matrix.randn(50, 2, 0, 0.2)
const y = model.predict(x1.toArray())

for (let i = 0; i < x1.rows; i++) {
for (let k = 0; k < x1.cols; k++) {
expect(y[i][k]).toBeCloseTo(x1.at(i, k) - r)
}
}
for (let k = 0; k < min.length; k++) {
expect(min[k]).toBeCloseTo(0)
})

test('arr mat', () => {
const model = new MinmaxNormalization()
const x = Matrix.randn(50, 1, 1, 0.2)
model.fit(x.value)

const x1 = Matrix.randn(50, 2, 0, 0.2)
const y = model.predict(x1.toArray())

const xmin = x.min()
const xmax = x.max()
for (let i = 0; i < x1.rows; i++) {
for (let k = 0; k < x1.cols; k++) {
expect(y[i][k]).toBeCloseTo((x1.at(i, k) - xmin) / (xmax - xmin))
}
}
})

test('arr arr', () => {
const model = new MinmaxNormalization()
const x = Matrix.randn(50, 1, 1, 0.2)
model.fit(x.value)

const x1 = Matrix.randn(50, 1, 0, 0.2)
const y = model.predict(x1.value)

const xmin = x.min()
const xmax = x.max()
for (let i = 0; i < x1.rows; i++) {
expect(y[i]).toBeCloseTo((x1.at(i, 0) - xmin) / (xmax - xmin))
}
for (let k = 0; k < max.length; k++) {
expect(max[k]).toBeCloseTo(1)
})

test('arr same', () => {
const model = new MinmaxNormalization()
const r = Math.random()
const x = new Matrix(50, 1, r)
model.fit(x.value)

const x1 = Matrix.randn(50, 1, 0, 0.2)
const y = model.predict(x1.value)

for (let i = 0; i < x1.rows; i++) {
expect(y[i]).toBeCloseTo(x1.at(i, 0) - r)
}
})
30 changes: 30 additions & 0 deletions tests/lib/model/nns/optimizer.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import NeuralNetwork from '../../../../lib/model/neuralnetwork.js'
import Matrix from '../../../../lib/util/matrix.js'

describe('nn', () => {
test.each(['sgd', 'momentum', 'rmsprop', 'adam'])('%s', optimizer => {
const net = NeuralNetwork.fromObject(
[
{ type: 'input', name: 'in' },
{ type: 'full', out_size: 5, activation: 'sigmoid' },
{ type: 'full', out_size: 3 },
],
'mse',
optimizer
)
const x = Matrix.randn(1, 10)
const t = Matrix.randn(1, 3)

for (let i = 0; i < 1000; i++) {
const loss = net.fit(x, t, 1000, 0.01)
if (loss[0] < 1.0e-8) {
break
}
}

const y = net.calc(x)
for (let i = 0; i < 3; i++) {
expect(y.at(0, i)).toBeCloseTo(t.at(0, i))
}
})
})
Loading