commit miss, recover server.ts
This commit is contained in:
parent
1b49ae20fe
commit
0aa6248a9b
1 changed files with 1423 additions and 1799 deletions
500
server.ts
500
server.ts
|
|
@ -1,6 +1,6 @@
|
|||
// server.ts - Simplified main server file
|
||||
// package.json dependencies needed:
|
||||
// npm install express mathjs lodash date-fns swagger-jsdoc swagger-ui-express js-yaml
|
||||
// npm install express mathjs lodash date-fns
|
||||
// npm install -D @types/express @types/node @types/lodash typescript ts-node
|
||||
|
||||
import express from 'express';
|
||||
|
|
@ -8,25 +8,9 @@ import swaggerJsdoc from 'swagger-jsdoc';
|
|||
import swaggerUi from 'swagger-ui-express';
|
||||
import * as math from 'mathjs';
|
||||
import * as _ from 'lodash';
|
||||
|
||||
// These imports assume the files exist in the same directory
|
||||
// import { KMeans, KMeansOptions } from './kmeans';
|
||||
// import { getWeekNumber, getSameWeekDayLastYear } from './time-helper';
|
||||
// import { calculateLinearRegression, generateForecast, calculatePredictionIntervals, ForecastResult } from './prediction';
|
||||
import { SignalProcessor, SmoothingOptions, EdgeDetectionOptions } from './signal_processing_convolution';
|
||||
import { convolve1D, ConvolutionKernels } from './convolution'; // Direct import for new functions
|
||||
|
||||
interface KMeansOptions {}
|
||||
class KMeans {
|
||||
constructor(p: any, n: any, o: any) {}
|
||||
run = () => ({ clusters: [] })
|
||||
}
|
||||
const getWeekNumber = (d: string) => 1;
|
||||
const getSameWeekDayLastYear = (d: string) => new Date().toISOString();
|
||||
interface ForecastResult {}
|
||||
const calculateLinearRegression = (v: any) => ({slope: 1, intercept: 0});
|
||||
const generateForecast = (m: any, l: any, p: any) => [];
|
||||
const calculatePredictionIntervals = (v: any, m: any, f: any) => [];
|
||||
import { KMeans, KMeansOptions } from './kmeans';
|
||||
import { getWeekNumber, getSameWeekDayLastYear } from './time-helper';
|
||||
import { calculateLinearRegression, generateForecast, calculatePredictionIntervals, ForecastResult } from './prediction';
|
||||
|
||||
const app = express();
|
||||
app.use(express.json());
|
||||
|
|
@ -46,7 +30,8 @@ const swaggerOptions = {
|
|||
},
|
||||
],
|
||||
},
|
||||
apis: ["./server.ts"], // Pointing to this file for Swagger docs
|
||||
// Paths to files containing OpenAPI definitions
|
||||
apis: ["./*.ts"], // Make sure this path is correct
|
||||
};
|
||||
|
||||
const swaggerSpec = swaggerJsdoc(swaggerOptions);
|
||||
|
|
@ -274,8 +259,8 @@ class AnalyticsEngine {
|
|||
const kmeans = new KMeans(points, nClusters, options);
|
||||
const result = kmeans.run();
|
||||
|
||||
const centroids = result.clusters.map(c => (c as any).centroid);
|
||||
const clusters = result.clusters.map(c => (c as any).points);
|
||||
const centroids = result.clusters.map(c => c.centroid);
|
||||
const clusters = result.clusters.map(c => c.points);
|
||||
|
||||
return { clusters, centroids };
|
||||
}
|
||||
|
|
@ -360,9 +345,8 @@ const analytics = new AnalyticsEngine();
|
|||
* get:
|
||||
* summary: Health check endpoint
|
||||
* description: Returns the health status of the API
|
||||
* tags: [Health]
|
||||
* responses:
|
||||
* '200':
|
||||
* 200:
|
||||
* description: API is healthy
|
||||
* content:
|
||||
* application/json:
|
||||
|
|
@ -386,7 +370,6 @@ app.get('/api/health', (req, res) => {
|
|||
* post:
|
||||
* summary: Get unique values from a data series
|
||||
* description: Returns an array of unique values from the provided data series
|
||||
* tags: [Statistics]
|
||||
* requestBody:
|
||||
* required: true
|
||||
* content:
|
||||
|
|
@ -397,9 +380,9 @@ app.get('/api/health', (req, res) => {
|
|||
* series:
|
||||
* $ref: '#/components/schemas/DataSeries'
|
||||
* responses:
|
||||
* '200':
|
||||
* 200:
|
||||
* description: Unique values calculated successfully
|
||||
* '400':
|
||||
* 400:
|
||||
* description: Invalid input data
|
||||
*/
|
||||
app.post('/api/unique', (req, res) => {
|
||||
|
|
@ -418,7 +401,6 @@ app.post('/api/unique', (req, res) => {
|
|||
* post:
|
||||
* summary: Calculate mean of a data series
|
||||
* description: Returns the arithmetic mean of the provided data series, optionally filtered by conditions
|
||||
* tags: [Statistics]
|
||||
* requestBody:
|
||||
* required: true
|
||||
* content:
|
||||
|
|
@ -433,9 +415,9 @@ app.post('/api/unique', (req, res) => {
|
|||
* items:
|
||||
* $ref: '#/components/schemas/Condition'
|
||||
* responses:
|
||||
* '200':
|
||||
* 200:
|
||||
* description: Mean calculated successfully
|
||||
* '400':
|
||||
* 400:
|
||||
* description: Invalid input data
|
||||
*/
|
||||
app.post('/api/mean', (req, res) => {
|
||||
|
|
@ -454,7 +436,6 @@ app.post('/api/mean', (req, res) => {
|
|||
* post:
|
||||
* summary: Count data points in a series
|
||||
* description: Returns the count of data points in the series, optionally filtered by conditions
|
||||
* tags: [Statistics]
|
||||
* requestBody:
|
||||
* required: true
|
||||
* content:
|
||||
|
|
@ -469,9 +450,9 @@ app.post('/api/mean', (req, res) => {
|
|||
* items:
|
||||
* $ref: '#/components/schemas/Condition'
|
||||
* responses:
|
||||
* '200':
|
||||
* 200:
|
||||
* description: Count calculated successfully
|
||||
* '400':
|
||||
* 400:
|
||||
* description: Invalid input data
|
||||
*/
|
||||
app.post('/api/count', (req, res) => {
|
||||
|
|
@ -490,7 +471,6 @@ app.post('/api/count', (req, res) => {
|
|||
* post:
|
||||
* summary: Calculate variance of a data series
|
||||
* description: Returns the variance of the provided data series
|
||||
* tags: [Statistics]
|
||||
* requestBody:
|
||||
* required: true
|
||||
* content:
|
||||
|
|
@ -505,9 +485,9 @@ app.post('/api/count', (req, res) => {
|
|||
* items:
|
||||
* $ref: '#/components/schemas/Condition'
|
||||
* responses:
|
||||
* '200':
|
||||
* 200:
|
||||
* description: Variance calculated successfully
|
||||
* '400':
|
||||
* 400:
|
||||
* description: Invalid input data
|
||||
*/
|
||||
app.post('/api/variance', (req, res) => {
|
||||
|
|
@ -526,7 +506,6 @@ app.post('/api/variance', (req, res) => {
|
|||
* post:
|
||||
* summary: Calculate standard deviation of a data series
|
||||
* description: Returns the standard deviation of the provided data series
|
||||
* tags: [Statistics]
|
||||
* requestBody:
|
||||
* required: true
|
||||
* content:
|
||||
|
|
@ -541,9 +520,9 @@ app.post('/api/variance', (req, res) => {
|
|||
* items:
|
||||
* $ref: '#/components/schemas/Condition'
|
||||
* responses:
|
||||
* '200':
|
||||
* 200:
|
||||
* description: Standard deviation calculated successfully
|
||||
* '400':
|
||||
* 400:
|
||||
* description: Invalid input data
|
||||
*/
|
||||
app.post('/api/std', (req, res) => {
|
||||
|
|
@ -562,7 +541,6 @@ app.post('/api/std', (req, res) => {
|
|||
* post:
|
||||
* summary: Calculate percentile of a data series
|
||||
* description: Returns the specified percentile of the provided data series
|
||||
* tags: [Statistics]
|
||||
* requestBody:
|
||||
* required: true
|
||||
* content:
|
||||
|
|
@ -585,9 +563,9 @@ app.post('/api/std', (req, res) => {
|
|||
* items:
|
||||
* $ref: '#/components/schemas/Condition'
|
||||
* responses:
|
||||
* '200':
|
||||
* 200:
|
||||
* description: Percentile calculated successfully
|
||||
* '400':
|
||||
* 400:
|
||||
* description: Invalid input data
|
||||
*/
|
||||
app.post('/api/percentile', (req, res) => {
|
||||
|
|
@ -606,7 +584,6 @@ app.post('/api/percentile', (req, res) => {
|
|||
* post:
|
||||
* summary: Calculate median of a data series
|
||||
* description: Returns the median (50th percentile) of the provided data series
|
||||
* tags: [Statistics]
|
||||
* requestBody:
|
||||
* required: true
|
||||
* content:
|
||||
|
|
@ -621,9 +598,9 @@ app.post('/api/percentile', (req, res) => {
|
|||
* items:
|
||||
* $ref: '#/components/schemas/Condition'
|
||||
* responses:
|
||||
* '200':
|
||||
* 200:
|
||||
* description: Median calculated successfully
|
||||
* '400':
|
||||
* 400:
|
||||
* description: Invalid input data
|
||||
*/
|
||||
app.post('/api/median', (req, res) => {
|
||||
|
|
@ -642,7 +619,6 @@ app.post('/api/median', (req, res) => {
|
|||
* post:
|
||||
* summary: Calculate mode of a data series
|
||||
* description: Returns the mode (most frequent values) of the provided data series
|
||||
* tags: [Statistics]
|
||||
* requestBody:
|
||||
* required: true
|
||||
* content:
|
||||
|
|
@ -657,9 +633,9 @@ app.post('/api/median', (req, res) => {
|
|||
* items:
|
||||
* $ref: '#/components/schemas/Condition'
|
||||
* responses:
|
||||
* '200':
|
||||
* 200:
|
||||
* description: Mode calculated successfully
|
||||
* '400':
|
||||
* 400:
|
||||
* description: Invalid input data
|
||||
*/
|
||||
app.post('/api/mode', (req, res) => {
|
||||
|
|
@ -678,7 +654,6 @@ app.post('/api/mode', (req, res) => {
|
|||
* post:
|
||||
* summary: Find maximum value in a data series
|
||||
* description: Returns the maximum value from the provided data series
|
||||
* tags: [Statistics]
|
||||
* requestBody:
|
||||
* required: true
|
||||
* content:
|
||||
|
|
@ -693,9 +668,9 @@ app.post('/api/mode', (req, res) => {
|
|||
* items:
|
||||
* $ref: '#/components/schemas/Condition'
|
||||
* responses:
|
||||
* '200':
|
||||
* 200:
|
||||
* description: Maximum value found successfully
|
||||
* '400':
|
||||
* 400:
|
||||
* description: Invalid input data
|
||||
*/
|
||||
app.post('/api/max', (req, res) => {
|
||||
|
|
@ -714,7 +689,6 @@ app.post('/api/max', (req, res) => {
|
|||
* post:
|
||||
* summary: Find minimum value in a data series
|
||||
* description: Returns the minimum value from the provided data series
|
||||
* tags: [Statistics]
|
||||
* requestBody:
|
||||
* required: true
|
||||
* content:
|
||||
|
|
@ -729,9 +703,9 @@ app.post('/api/max', (req, res) => {
|
|||
* items:
|
||||
* $ref: '#/components/schemas/Condition'
|
||||
* responses:
|
||||
* '200':
|
||||
* 200:
|
||||
* description: Minimum value found successfully
|
||||
* '400':
|
||||
* 400:
|
||||
* description: Invalid input data
|
||||
*/
|
||||
app.post('/api/min', (req, res) => {
|
||||
|
|
@ -750,7 +724,6 @@ app.post('/api/min', (req, res) => {
|
|||
* post:
|
||||
* summary: Calculate correlation between two data series
|
||||
* description: Returns the Pearson correlation coefficient between two data series
|
||||
* tags: [Statistics]
|
||||
* requestBody:
|
||||
* required: true
|
||||
* content:
|
||||
|
|
@ -763,9 +736,9 @@ app.post('/api/min', (req, res) => {
|
|||
* series2:
|
||||
* $ref: '#/components/schemas/DataSeries'
|
||||
* responses:
|
||||
* '200':
|
||||
* 200:
|
||||
* description: Correlation calculated successfully
|
||||
* '400':
|
||||
* 400:
|
||||
* description: Invalid input data or series have different lengths
|
||||
*/
|
||||
app.post('/api/correlation', (req, res) => {
|
||||
|
|
@ -784,7 +757,6 @@ app.post('/api/correlation', (req, res) => {
|
|||
* post:
|
||||
* summary: Calculate moving average of a data series
|
||||
* description: Returns the moving average of the provided data series with specified window size
|
||||
* tags: [Series Operations]
|
||||
* requestBody:
|
||||
* required: true
|
||||
* content:
|
||||
|
|
@ -800,9 +772,9 @@ app.post('/api/correlation', (req, res) => {
|
|||
* minimum: 1
|
||||
* example: 5
|
||||
* responses:
|
||||
* '200':
|
||||
* 200:
|
||||
* description: Moving average calculated successfully
|
||||
* '400':
|
||||
* 400:
|
||||
* description: Invalid input data or window size
|
||||
*/
|
||||
app.post('/api/series/moving-average', (req, res) => {
|
||||
|
|
@ -822,7 +794,6 @@ app.post('/api/series/moving-average', (req, res) => {
|
|||
* post:
|
||||
* summary: Get rolling windows of a data series
|
||||
* description: Returns rolling windows of the provided data series with specified window size
|
||||
* tags: [Series Operations]
|
||||
* requestBody:
|
||||
* required: true
|
||||
* content:
|
||||
|
|
@ -838,9 +809,9 @@ app.post('/api/series/moving-average', (req, res) => {
|
|||
* minimum: 1
|
||||
* example: 3
|
||||
* responses:
|
||||
* '200':
|
||||
* 200:
|
||||
* description: Rolling windows calculated successfully
|
||||
* '400':
|
||||
* 400:
|
||||
* description: Invalid input data or window size
|
||||
*/
|
||||
app.post('/api/series/rolling', (req, res) => {
|
||||
|
|
@ -860,7 +831,6 @@ app.post('/api/series/rolling', (req, res) => {
|
|||
* post:
|
||||
* summary: Perform K-means clustering
|
||||
* description: Performs K-means clustering on the provided data matrix
|
||||
* tags: [Machine Learning]
|
||||
* requestBody:
|
||||
* required: true
|
||||
* content:
|
||||
|
|
@ -879,9 +849,9 @@ app.post('/api/series/rolling', (req, res) => {
|
|||
* type: object
|
||||
* description: K-means options
|
||||
* responses:
|
||||
* '200':
|
||||
* 200:
|
||||
* description: K-means clustering completed successfully
|
||||
* '400':
|
||||
* 400:
|
||||
* description: Invalid input data
|
||||
*/
|
||||
app.post('/api/ml/kmeans', (req, res) => {
|
||||
|
|
@ -900,7 +870,6 @@ app.post('/api/ml/kmeans', (req, res) => {
|
|||
* post:
|
||||
* summary: Get week number from date
|
||||
* description: Returns the ISO week number for the provided date string
|
||||
* tags: [Time]
|
||||
* requestBody:
|
||||
* required: true
|
||||
* content:
|
||||
|
|
@ -914,9 +883,9 @@ app.post('/api/ml/kmeans', (req, res) => {
|
|||
* description: Date string in ISO format
|
||||
* example: "2024-03-15"
|
||||
* responses:
|
||||
* '200':
|
||||
* 200:
|
||||
* description: Week number calculated successfully
|
||||
* '400':
|
||||
* 400:
|
||||
* description: Invalid date format
|
||||
*/
|
||||
app.post('/api/time/week-number', (req, res) => {
|
||||
|
|
@ -936,7 +905,6 @@ app.post('/api/time/week-number', (req, res) => {
|
|||
* post:
|
||||
* summary: Get same day of week from last year
|
||||
* description: Returns the date string for the same day of the week from the previous year
|
||||
* tags: [Time]
|
||||
* requestBody:
|
||||
* required: true
|
||||
* content:
|
||||
|
|
@ -950,9 +918,9 @@ app.post('/api/time/week-number', (req, res) => {
|
|||
* description: Date string in ISO format
|
||||
* example: "2024-03-15"
|
||||
* responses:
|
||||
* '200':
|
||||
* 200:
|
||||
* description: Same day last year calculated successfully
|
||||
* '400':
|
||||
* 400:
|
||||
* description: Invalid date format
|
||||
*/
|
||||
app.post('/api/time/same-day-last-year', (req, res) => {
|
||||
|
|
@ -972,7 +940,6 @@ app.post('/api/time/same-day-last-year', (req, res) => {
|
|||
* post:
|
||||
* summary: Calculate purchase rate
|
||||
* description: Calculates the purchase rate as a percentage of product purchases over total transactions
|
||||
* tags: [Retail]
|
||||
* requestBody:
|
||||
* required: true
|
||||
* content:
|
||||
|
|
@ -989,9 +956,9 @@ app.post('/api/time/same-day-last-year', (req, res) => {
|
|||
* description: Total number of transactions
|
||||
* example: 1000
|
||||
* responses:
|
||||
* '200':
|
||||
* 200:
|
||||
* description: Purchase rate calculated successfully
|
||||
* '400':
|
||||
* 400:
|
||||
* description: Invalid input data or division by zero
|
||||
*/
|
||||
app.post('/api/retail/purchase-rate', (req, res) => {
|
||||
|
|
@ -1010,7 +977,6 @@ app.post('/api/retail/purchase-rate', (req, res) => {
|
|||
* post:
|
||||
* summary: Calculate lift value
|
||||
* description: Calculates the lift value for market basket analysis
|
||||
* tags: [Retail]
|
||||
* requestBody:
|
||||
* required: true
|
||||
* content:
|
||||
|
|
@ -1031,9 +997,9 @@ app.post('/api/retail/purchase-rate', (req, res) => {
|
|||
* description: Purchase rate of product B
|
||||
* example: 0.3
|
||||
* responses:
|
||||
* '200':
|
||||
* 200:
|
||||
* description: Lift value calculated successfully
|
||||
* '400':
|
||||
* 400:
|
||||
* description: Invalid input data or division by zero
|
||||
*/
|
||||
app.post('/api/retail/lift-value', (req, res) => {
|
||||
|
|
@ -1052,7 +1018,6 @@ app.post('/api/retail/lift-value', (req, res) => {
|
|||
* post:
|
||||
* summary: Calculate cost ratio
|
||||
* description: Calculates the cost ratio (cost divided by sale price)
|
||||
* tags: [Retail]
|
||||
* requestBody:
|
||||
* required: true
|
||||
* content:
|
||||
|
|
@ -1069,9 +1034,9 @@ app.post('/api/retail/lift-value', (req, res) => {
|
|||
* description: Sale price of the product
|
||||
* example: 100
|
||||
* responses:
|
||||
* '200':
|
||||
* 200:
|
||||
* description: Cost ratio calculated successfully
|
||||
* '400':
|
||||
* 400:
|
||||
* description: Invalid input data or division by zero
|
||||
*/
|
||||
app.post('/api/retail/cost-ratio', (req, res) => {
|
||||
|
|
@ -1090,7 +1055,6 @@ app.post('/api/retail/cost-ratio', (req, res) => {
|
|||
* post:
|
||||
* summary: Calculate gross margin rate
|
||||
* description: Calculates the gross margin rate as a percentage
|
||||
* tags: [Retail]
|
||||
* requestBody:
|
||||
* required: true
|
||||
* content:
|
||||
|
|
@ -1107,9 +1071,9 @@ app.post('/api/retail/cost-ratio', (req, res) => {
|
|||
* description: Cost of the product
|
||||
* example: 60
|
||||
* responses:
|
||||
* '200':
|
||||
* 200:
|
||||
* description: Gross margin rate calculated successfully
|
||||
* '400':
|
||||
* 400:
|
||||
* description: Invalid input data or division by zero
|
||||
*/
|
||||
app.post('/api/retail/gross-margin', (req, res) => {
|
||||
|
|
@ -1128,7 +1092,6 @@ app.post('/api/retail/gross-margin', (req, res) => {
|
|||
* post:
|
||||
* summary: Calculate average spend per customer
|
||||
* description: Calculates the average amount spent per customer
|
||||
* tags: [Retail]
|
||||
* requestBody:
|
||||
* required: true
|
||||
* content:
|
||||
|
|
@ -1145,9 +1108,9 @@ app.post('/api/retail/gross-margin', (req, res) => {
|
|||
* description: Number of customers
|
||||
* example: 500
|
||||
* responses:
|
||||
* '200':
|
||||
* 200:
|
||||
* description: Average spend calculated successfully
|
||||
* '400':
|
||||
* 400:
|
||||
* description: Invalid input data or division by zero
|
||||
*/
|
||||
app.post('/api/retail/average-spend', (req, res) => {
|
||||
|
|
@ -1167,7 +1130,6 @@ app.post('/api/retail/average-spend', (req, res) => {
|
|||
* post:
|
||||
* summary: Calculate purchase index
|
||||
* description: Calculates the purchase index (items per 1000 customers)
|
||||
* tags: [Retail]
|
||||
* requestBody:
|
||||
* required: true
|
||||
* content:
|
||||
|
|
@ -1184,9 +1146,9 @@ app.post('/api/retail/average-spend', (req, res) => {
|
|||
* description: Number of customers
|
||||
* example: 1000
|
||||
* responses:
|
||||
* '200':
|
||||
* 200:
|
||||
* description: Purchase index calculated successfully
|
||||
* '400':
|
||||
* 400:
|
||||
* description: Invalid input data or division by zero
|
||||
*/
|
||||
app.post('/api/retail/purchase-index', (req, res) => {
|
||||
|
|
@ -1206,7 +1168,6 @@ app.post('/api/retail/purchase-index', (req, res) => {
|
|||
* post:
|
||||
* summary: Generate time series forecast
|
||||
* description: Generates a forecast for time series data using linear regression
|
||||
* tags: [Prediction]
|
||||
* requestBody:
|
||||
* required: true
|
||||
* content:
|
||||
|
|
@ -1222,9 +1183,9 @@ app.post('/api/retail/purchase-index', (req, res) => {
|
|||
* minimum: 1
|
||||
* example: 10
|
||||
* responses:
|
||||
* '200':
|
||||
* 200:
|
||||
* description: Forecast generated successfully
|
||||
* '400':
|
||||
* 400:
|
||||
* description: Invalid input data
|
||||
*/
|
||||
app.post('/api/predict/forecast', (req, res) => {
|
||||
|
|
@ -1238,316 +1199,6 @@ app.post('/api/predict/forecast', (req, res) => {
|
|||
}
|
||||
});
|
||||
|
||||
// ========================================
|
||||
// NEW SIGNAL & IMAGE PROCESSING ROUTES
|
||||
// ========================================
|
||||
|
||||
/**
|
||||
* @swagger
|
||||
* /api/signal/smooth:
|
||||
* post:
|
||||
* summary: Smooth a 1D data series
|
||||
* description: Applies a smoothing filter (Gaussian or Moving Average) to a 1D data series to reduce noise.
|
||||
* tags: [Signal Processing]
|
||||
* requestBody:
|
||||
* required: true
|
||||
* content:
|
||||
* application/json:
|
||||
* schema:
|
||||
* type: object
|
||||
* properties:
|
||||
* series:
|
||||
* $ref: '#/components/schemas/DataSeries'
|
||||
* options:
|
||||
* $ref: '#/components/schemas/SmoothingOptions'
|
||||
* responses:
|
||||
* '200':
|
||||
* description: The smoothed data series
|
||||
* content:
|
||||
* application/json:
|
||||
* schema:
|
||||
* $ref: '#/components/schemas/ApiResponse'
|
||||
* '400':
|
||||
* description: Invalid input data
|
||||
*/
|
||||
app.post('/api/signal/smooth', (req, res) => {
|
||||
try {
|
||||
const { series, options } = req.body;
|
||||
validateSeries(series);
|
||||
const result = SignalProcessor.smooth(series.values, options);
|
||||
res.status(200).json({ success: true, data: result } as ApiResponse<number[]>);
|
||||
} catch (error) {
|
||||
const errorMessage = handleError(error);
|
||||
res.status(400).json({ success: false, error: errorMessage } as ApiResponse<number[]>);
|
||||
}
|
||||
});
|
||||
|
||||
/**
|
||||
* @swagger
|
||||
* /api/signal/detect-peaks:
|
||||
* post:
|
||||
* summary: Detect peaks in a 1D data series
|
||||
* description: Identifies local maxima (peaks) in a 1D data series. More robust and accurate logic.
|
||||
* tags: [Signal Processing]
|
||||
* requestBody:
|
||||
* required: true
|
||||
* content:
|
||||
* application/json:
|
||||
* schema:
|
||||
* type: object
|
||||
* properties:
|
||||
* series:
|
||||
* $ref: '#/components/schemas/DataSeries'
|
||||
* options:
|
||||
* type: object
|
||||
* properties:
|
||||
* smoothWindow:
|
||||
* type: integer
|
||||
* description: Optional window size for Gaussian smoothing to reduce noise before peak detection.
|
||||
* example: 3
|
||||
* minDistance:
|
||||
* type: integer
|
||||
* description: The minimum number of data points between two peaks.
|
||||
* example: 1
|
||||
* threshold:
|
||||
* type: number
|
||||
* description: The minimum value for a data point to be considered a peak.
|
||||
* example: 0.5
|
||||
* responses:
|
||||
* '200':
|
||||
* description: An array of detected peak objects, each with an index and value.
|
||||
* '400':
|
||||
* description: Invalid input data
|
||||
*/
|
||||
app.post('/api/signal/detect-peaks', (req, res) => {
|
||||
try {
|
||||
const { series, options } = req.body;
|
||||
validateSeries(series);
|
||||
const result = SignalProcessor.detectPeaksConvolution(series.values, options);
|
||||
res.status(200).json({ success: true, data: result } as ApiResponse<any>);
|
||||
} catch (error) {
|
||||
const errorMessage = handleError(error);
|
||||
res.status(400).json({ success: false, error: errorMessage } as ApiResponse<any>);
|
||||
}
|
||||
});
|
||||
|
||||
/**
|
||||
* @swagger
|
||||
* /api/signal/detect-valleys:
|
||||
* post:
|
||||
* summary: Detect valleys in a 1D data series
|
||||
* description: Identifies local minima (valleys) in a 1D data series. More robust and accurate logic.
|
||||
* tags: [Signal Processing]
|
||||
* requestBody:
|
||||
* required: true
|
||||
* content:
|
||||
* application/json:
|
||||
* schema:
|
||||
* type: object
|
||||
* properties:
|
||||
* series:
|
||||
* $ref: '#/components/schemas/DataSeries'
|
||||
* options:
|
||||
* type: object
|
||||
* properties:
|
||||
* smoothWindow:
|
||||
* type: integer
|
||||
* description: Optional window size for Gaussian smoothing to reduce noise before valley detection.
|
||||
* example: 3
|
||||
* minDistance:
|
||||
* type: integer
|
||||
* description: The minimum number of data points between two valleys.
|
||||
* example: 1
|
||||
* threshold:
|
||||
* type: number
|
||||
* description: The maximum value for a data point to be considered a valley.
|
||||
* example: -0.5
|
||||
* responses:
|
||||
* '200':
|
||||
* description: An array of detected valley objects, each with an index and value.
|
||||
* '400':
|
||||
* description: Invalid input data
|
||||
*/
|
||||
app.post('/api/signal/detect-valleys', (req, res) => {
|
||||
try {
|
||||
const { series, options } = req.body;
|
||||
validateSeries(series);
|
||||
const result = SignalProcessor.detectValleysConvolution(series.values, options);
|
||||
res.status(200).json({ success: true, data: result } as ApiResponse<any>);
|
||||
} catch (error) {
|
||||
const errorMessage = handleError(error);
|
||||
res.status(400).json({ success: false, error: errorMessage } as ApiResponse<any>);
|
||||
}
|
||||
});
|
||||
|
||||
/**
|
||||
* @swagger
|
||||
* /api/signal/detect-outliers:
|
||||
* post:
|
||||
* summary: Detect outliers in a 1D data series
|
||||
* description: Identifies outliers in a 1D data series using statistically sound methods.
|
||||
* tags: [Signal Processing]
|
||||
* requestBody:
|
||||
* required: true
|
||||
* content:
|
||||
* application/json:
|
||||
* schema:
|
||||
* type: object
|
||||
* properties:
|
||||
* series:
|
||||
* $ref: '#/components/schemas/DataSeries'
|
||||
* options:
|
||||
* type: object
|
||||
* properties:
|
||||
* method:
|
||||
* type: string
|
||||
* enum: [local_deviation, mean_diff]
|
||||
* default: local_deviation
|
||||
* windowSize:
|
||||
* type: integer
|
||||
* default: 7
|
||||
* threshold:
|
||||
* type: number
|
||||
* description: "The sensitivity threshold. For 'local_deviation', this is the number of standard deviations (Z-score)."
|
||||
* default: 3.0
|
||||
* responses:
|
||||
* '200':
|
||||
* description: An array of detected outlier objects.
|
||||
* content:
|
||||
* application/json:
|
||||
* schema:
|
||||
* $ref: '#/components/schemas/ApiResponse'
|
||||
* '400':
|
||||
* description: Invalid input data
|
||||
*/
|
||||
app.post('/api/signal/detect-outliers', (req, res) => {
|
||||
try {
|
||||
const { series, options } = req.body;
|
||||
validateSeries(series);
|
||||
const result = SignalProcessor.detectOutliersConvolution(series.values, options);
|
||||
res.status(200).json({ success: true, data: result } as ApiResponse<any>);
|
||||
} catch (error) {
|
||||
const errorMessage = handleError(error);
|
||||
res.status(400).json({ success: false, error: errorMessage } as ApiResponse<any>);
|
||||
}
|
||||
});
|
||||
|
||||
/**
|
||||
* @swagger
|
||||
* /api/signal/detect-vertices:
|
||||
* post:
|
||||
* summary: Detect trend vertices (turning points) in a 1D series
|
||||
* description: Identifies all significant peaks and valleys in a data series trend using a robust local maxima/minima search.
|
||||
* tags: [Signal Processing]
|
||||
* requestBody:
|
||||
* required: true
|
||||
* content:
|
||||
* application/json:
|
||||
* schema:
|
||||
* type: object
|
||||
* properties:
|
||||
* series:
|
||||
* $ref: '#/components/schemas/DataSeries'
|
||||
* options:
|
||||
* type: object
|
||||
* properties:
|
||||
* smoothingWindow:
|
||||
* type: integer
|
||||
* default: 5
|
||||
* description: Window size for an initial Gaussian smoothing pass to reduce noise.
|
||||
* threshold:
|
||||
* type: number
|
||||
* description: The absolute value a peak/valley must exceed to be counted.
|
||||
* default: 0
|
||||
* minDistance:
|
||||
* type: integer
|
||||
* default: 3
|
||||
* description: Minimum number of data points between any two vertices.
|
||||
* responses:
|
||||
* '200':
|
||||
* description: An array of detected vertex objects, labeled as 'peak' or 'valley'.
|
||||
* content:
|
||||
* application/json:
|
||||
* schema:
|
||||
* $ref: '#/components/schemas/ApiResponse'
|
||||
* '400':
|
||||
* description: Invalid input data
|
||||
*/
|
||||
app.post('/api/signal/detect-vertices', (req, res) => {
|
||||
try {
|
||||
const { series, options } = req.body;
|
||||
validateSeries(series);
|
||||
const result = SignalProcessor.detectTrendVertices(series.values, options);
|
||||
res.status(200).json({ success: true, data: result } as ApiResponse<any>);
|
||||
} catch (error) {
|
||||
const errorMessage = handleError(error);
|
||||
res.status(400).json({ success: false, error: errorMessage } as ApiResponse<any>);
|
||||
}
|
||||
});
|
||||
|
||||
/**
|
||||
* @swagger
|
||||
* /api/kernels/{name}:
|
||||
* get:
|
||||
* summary: Get a pre-defined convolution kernel
|
||||
* description: Retrieves a standard 1D or 2D convolution kernel by its name.
|
||||
* tags: [Kernels]
|
||||
* parameters:
|
||||
* - in: path
|
||||
* name: name
|
||||
* required: true
|
||||
* schema:
|
||||
* type: string
|
||||
* enum: [sobel-x, sobel-y, laplacian, difference1d, average1d]
|
||||
* description: The name of the kernel to retrieve.
|
||||
* - in: query
|
||||
* name: size
|
||||
* schema:
|
||||
* type: integer
|
||||
* default: 3
|
||||
* description: The size of the kernel (for kernels like 'average1d').
|
||||
* responses:
|
||||
* '200':
|
||||
* description: The requested kernel as a 1D or 2D array.
|
||||
* content:
|
||||
* application/json:
|
||||
* schema:
|
||||
* $ref: '#/components/schemas/ApiResponse'
|
||||
* '400':
|
||||
* description: Unknown kernel name or invalid options.
|
||||
*/
|
||||
app.get('/api/kernels/:name', (req, res) => {
|
||||
try {
|
||||
const kernelName = req.params.name;
|
||||
const size = req.query.size ? parseInt(req.query.size as string, 10) : 3;
|
||||
let kernel: number[] | number[][];
|
||||
|
||||
switch (kernelName) {
|
||||
case 'sobel-x':
|
||||
kernel = ConvolutionKernels.sobel('x');
|
||||
break;
|
||||
case 'sobel-y':
|
||||
kernel = ConvolutionKernels.sobel('y');
|
||||
break;
|
||||
case 'laplacian':
|
||||
kernel = ConvolutionKernels.laplacian();
|
||||
break;
|
||||
case 'difference1d':
|
||||
kernel = ConvolutionKernels.difference1D();
|
||||
break;
|
||||
case 'average1d':
|
||||
kernel = ConvolutionKernels.average1D(size);
|
||||
break;
|
||||
default:
|
||||
throw new Error(`Unknown kernel name: ${kernelName}`);
|
||||
}
|
||||
res.status(200).json({ success: true, data: kernel } as ApiResponse<any>);
|
||||
} catch (error) {
|
||||
const errorMessage = handleError(error);
|
||||
res.status(400).json({ success: false, error: errorMessage } as ApiResponse<any>);
|
||||
}
|
||||
});
|
||||
|
||||
// ========================================
|
||||
// SWAGGER COMPONENTS
|
||||
// ========================================
|
||||
|
|
@ -1573,6 +1224,7 @@ app.get('/api/kernels/:name', (req, res) => {
|
|||
* type: string
|
||||
* description: Optional labels for the values
|
||||
* example: ["Jan", "Feb", "Mar", "Apr", "May"]
|
||||
*
|
||||
* DataMatrix:
|
||||
* type: object
|
||||
* required:
|
||||
|
|
@ -1598,6 +1250,7 @@ app.get('/api/kernels/:name', (req, res) => {
|
|||
* type: string
|
||||
* description: Optional row names
|
||||
* example: ["row1", "row2", "row3"]
|
||||
*
|
||||
* Condition:
|
||||
* type: object
|
||||
* required:
|
||||
|
|
@ -1620,34 +1273,7 @@ app.get('/api/kernels/:name', (req, res) => {
|
|||
* - type: string
|
||||
* description: Value to compare against
|
||||
* example: 10
|
||||
* SmoothingOptions:
|
||||
* type: object
|
||||
* properties:
|
||||
* method:
|
||||
* type: string
|
||||
* enum: [gaussian, moving_average]
|
||||
* default: gaussian
|
||||
* description: The smoothing method to use.
|
||||
* windowSize:
|
||||
* type: integer
|
||||
* default: 5
|
||||
* description: The size of the window for the filter. Must be an odd number for Gaussian.
|
||||
* sigma:
|
||||
* type: number
|
||||
* default: 1.0
|
||||
* description: The standard deviation for the Gaussian filter.
|
||||
* EdgeDetectionOptions:
|
||||
* type: object
|
||||
* properties:
|
||||
* method:
|
||||
* type: string
|
||||
* enum: [sobel, laplacian]
|
||||
* default: sobel
|
||||
* description: The edge detection algorithm to use.
|
||||
* threshold:
|
||||
* type: number
|
||||
* default: 0.1
|
||||
* description: The sensitivity threshold for detecting an edge. Values below this will be set to 0.
|
||||
*
|
||||
* ApiResponse:
|
||||
* type: object
|
||||
* properties:
|
||||
|
|
@ -1667,9 +1293,8 @@ app.get('/api/kernels/:name', (req, res) => {
|
|||
* get:
|
||||
* summary: Export API documentation as JSON
|
||||
* description: Returns the complete OpenAPI specification in JSON format
|
||||
* tags: [Documentation]
|
||||
* responses:
|
||||
* '200':
|
||||
* 200:
|
||||
* description: OpenAPI specification in JSON format
|
||||
* content:
|
||||
* application/json:
|
||||
|
|
@ -1688,9 +1313,8 @@ app.get('/api/docs/export/json', (req, res) => {
|
|||
* get:
|
||||
* summary: Export API documentation as YAML
|
||||
* description: Returns the complete OpenAPI specification in YAML format
|
||||
* tags: [Documentation]
|
||||
* responses:
|
||||
* '200':
|
||||
* 200:
|
||||
* description: OpenAPI specification in YAML format
|
||||
* content:
|
||||
* text/yaml:
|
||||
|
|
@ -1712,9 +1336,8 @@ app.get('/api/docs/export/yaml', (req, res) => {
|
|||
* get:
|
||||
* summary: Export API documentation as HTML
|
||||
* description: Returns a standalone HTML file with the complete API documentation
|
||||
* tags: [Documentation]
|
||||
* responses:
|
||||
* '200':
|
||||
* 200:
|
||||
* description: Standalone HTML documentation
|
||||
* content:
|
||||
* text/html:
|
||||
|
|
@ -1774,6 +1397,7 @@ app.get('/api/docs/export/html', (req, res) => {
|
|||
res.send(htmlTemplate);
|
||||
});
|
||||
|
||||
|
||||
// ========================================
|
||||
// ERROR HANDLING
|
||||
// ========================================
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue