Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: use QR, add demo, add tests, add benchmark #12

Closed
wants to merge 4 commits into from
Closed
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,4 @@ jspm_packages
lib

lib-esm
*.cpuprofile
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
src/__tests__/data/*.json
34 changes: 34 additions & 0 deletions benchmark/cubic.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/**
* Data produced with
const yVal =
((6.02 * (i / 100)) ^ 3) +
3.01 * (i / 100) ** 2 +
2.55 * i/100 +
5 +
Math.random();
*/

import { readFileSync } from 'fs';
import { join } from 'path';

import { PolynomialRegression } from '../lib/index.js';

const __dirname = new URL('.', import.meta.url).pathname;
const data = JSON.parse(
readFileSync(
join(__dirname, '..', 'src', '__tests__', 'data', 'large_cubic.json'),
'utf8',
),
);

/**
* const yVal =
6.3 * (i / 5000) ** 3 +
3.01 * (i / 1000) ** 2 +
(2.55 / 100) * i +
5 +
Math.random();
*/
const result = new PolynomialRegression(data.x, data.y, 3);

console.log(result.coefficients);
68 changes: 68 additions & 0 deletions demo/example.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import React from 'react';
import { PolynomialRegression } from '../src/index';
import {
Plot,
LineSeries,
Axis,
Legend,
Heading,
SeriesPoint,
} from 'react-plot';

import { x, y } from '../src/__tests__/data/tricky.data';

const data = x.map((x, i) => ({ x, y: y[i] }));

const calculations: SeriesPoint[][] = [];
const degree: number[] = [];
for (let i = 2; i <= 7; i++) {
const r = new PolynomialRegression(x, y, i);
const plot = r.predict(x).map((y, i) => ({ x: x[i], y }));
degree.push(r.degree);
calculations.push(plot);
}

export const Example = () => (
<Plot
width={1000}
height={1000}
margin={{ bottom: 50, left: 90, top: 50, right: 100 }}
>
<Heading
title="Electrical characterization"
subtitle="Current vs Voltage"
/>
<LineSeries
data={data}
xAxis="x"
yAxis="y"
lineStyle={{ strokeWidth: 3 }}
label="Raw Data"
displayMarkers={false}
/>
{calculations.map((plot, i) => (
<LineSeries
key={i}
data={plot}
xAxis="x"
yAxis="y"
label={`${degree[i]} degree polynomial`}
/>
))}
<Axis
id="x"
position="bottom"
label="Drain voltage [V]"
displayPrimaryGridLines
// max={Math.max(...x) * 1.1}
/>
<Axis
id="y"
position="left"
label="Drain current [mA]"
displayPrimaryGridLines
// max={Math.max(...y) * 1.1}
/>
<Legend position="right" />
</Plot>
);
34 changes: 34 additions & 0 deletions demo/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>1D Spectra</title>
<style>
* {
padding: 0.5rem;
margin: 0;
box-sizing: border-box;
}
body {
padding-top: 2rem;
}
label {
border-radius: 50% 25% 40% 10%;
border: 1.5px solid #ccc;
display: inline-block;
padding: 1rem;
margin: 1rem;
}
label:hover {
cursor: pointer;
background-color: bisque;
}
</style>
</head>
<body>
<div id="root"></div>
<script type="module" src="./main.tsx"></script>
</body>
</html>
9 changes: 9 additions & 0 deletions demo/main.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import React from 'react'
import ReactDOM from 'react-dom/client'
import { Example } from './example'

ReactDOM.createRoot(document.getElementById('root') as HTMLElement).render(
<React.StrictMode>
<Example />
</React.StrictMode>,
)
16 changes: 11 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@
"lib-esm"
],
"scripts": {
"benchmark": "npm run tsc && node --cpu-prof ./benchmark/cubic.mjs",
"check-types": "tsc --noEmit",
"clean": "rimraf lib lib-esm",
"demo": "vite demo --open --base ./",
"eslint": "eslint src",
"eslint-fix": "npm run eslint -- --fix",
"prepack": "npm run tsc",
Expand All @@ -36,17 +38,21 @@
},
"homepage": "https://github.com/mljs/regression-polynomial#readme",
"devDependencies": {
"@vitest/coverage-v8": "^0.34.5",
"eslint": "^8.50.0",
"@vitejs/plugin-react": "^4.1.0",
"@vitest/coverage-v8": "^0.34.6",
"eslint": "^8.51.0",
"eslint-config-cheminfo-typescript": "^12.0.4",
"prettier": "^3.0.3",
"react": "^18.2.0",
"react-plot": "^1.4.2",
"rimraf": "^5.0.5",
"typescript": "^5.2.2",
"vitest": "^0.34.5"
"vite": "^4.5.0",
"vitest": "^0.34.6"
},
"dependencies": {
"cheminfo-types": "^1.7.2",
"ml-matrix": "^6.10.5",
"ml-matrix": "^6.10.7",
"ml-regression-base": "^3.0.0"
}
}
}
6 changes: 6 additions & 0 deletions src/__tests__/data/degree5.data.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export const x = [
50, 50, 50, 70, 70, 70, 80, 80, 80, 90, 90, 90, 100, 100, 100,
];
export const y = [
3.3, 2.8, 2.9, 2.3, 2.6, 2.1, 2.5, 2.9, 2.4, 3.0, 3.1, 2.8, 3.3, 3.5, 3.0,
];
Loading