import { Injectable } from '@angular/core'; export interface RawData { index: string[]; values: number[]; name?: string; } export interface LshData { candidates: number[][][]; distances: number[][][]; average_candidates: number[]; average_distances: number[]; tables: {[bucket: string]: number[]}[]; average_table: {[bucket: string]: number[]}; samples: number[]; hash_functions: number[][]; parameters?: number[]; } export interface TableInfoData { prototypes: { average: number[][]; min: number[][]; max: number[][]; }[]; distances: number[][]; } export interface Parameters { windowsize: number; hashsize: number; tablesize: number; stepsize: number; } export interface State { lshData: LshData; queryWindow: number[][]; weights: number[]; labels: any; } export interface StateNode { id: number; image: any; state: State; children: number[]; } @Injectable({ providedIn: 'root' }) /** * This service acts as the interface between the client and server side. */ export class ApiService { constructor() { } /** * Read input data. The format is a list of channels, where each channel is an object of type RawData */ async readFile(): Promise { const response = await fetch('http://127.0.0.1:5000/read-data'); return await response.json(); } /** * Split the data into windows (server side) */ async createWindows(parameters: Parameters): Promise { const postData = {parameters}; await fetch('http://127.0.0.1:5000/create-windows', { method: 'POST', headers: { 'Accept': 'application/json', 'Content-Type': 'application/json' }, body: JSON.stringify(postData) }); } /** * Get weights which will be applied to the LSH hash functions */ async getWeights(query: number[][], labels: {[index: number]: boolean}, weights: number[], hash_functions: number[][]): Promise { const response = await fetch('http://127.0.0.1:5000/weights', { method: 'POST', headers: { 'Accept': 'application/json', 'Content-Type': 'application/json' }, body: new Blob( [ JSON.stringify({query, labels, weights, hash_functions}) ], { type: 'text/plain' } ) }); return await response.json(); } /** * Do the first iteration of LSH and return important information */ async lshInitial(query: number[][]): Promise { const response = await fetch('http://127.0.0.1:5000/initialize', { method: 'POST', headers: { 'Accept': 'application/json', 'Content-Type': 'application/json' }, body: new Blob( [ JSON.stringify({query}) ], { type: 'text/plain' } ) }); return await response.json(); } /** * Do another iteration of LSH, with weights, and return important information */ async lshUpdate(query, weights, parameters): Promise { const response = await fetch('http://127.0.0.1:5000/update', { method: 'POST', headers: { 'Accept': 'application/json', 'Content-Type': 'application/json' }, body: new Blob( [ JSON.stringify({query, weights, parameters}) ], { type: 'text/plain' } ) }); return await response.json(); } /** * Get query window based on windows labeled correct */ async getQueryWindow(indices: number | {[index: number]: boolean}, query_size: number, start_index:number): Promise { const response = await fetch('http://127.0.0.1:5000/query', { method: 'POST', headers: { 'Accept': 'application/json', 'Content-Type': 'application/json' }, body: JSON.stringify({indices, query_size, start_index}) }); return await response.json(); } /** * Get data of a window by indices */ async getWindowByIndices(indices: number[]): Promise { const response = await fetch('http://127.0.0.1:5000/window', { method: 'POST', headers: { 'Accept': 'application/json', 'Content-Type': 'application/json' }, body: JSON.stringify({indices}) }); return await response.json(); } /** * Get additional information for a given table */ async getTableInfo(table: number[][]): Promise { const response = await fetch('http://127.0.0.1:5000/table-info', { method: 'POST', headers: { 'Accept': 'application/json', 'Content-Type': 'application/json' }, body: JSON.stringify({table}) }); return await response.json(); } }