From 4c88db301f39e17f63dcfb07b5bfad9bc3a70703 Mon Sep 17 00:00:00 2001 From: "Engelbart Kai Sven (PEA4-Fe)" Date: Tue, 15 Dec 2020 12:00:16 +0100 Subject: [PATCH] Add CSV prediction export --- package-lock.json | 17 ++++++++++++++--- package.json | 3 ++- src/app/app.module.ts | 16 ++++++++-------- src/app/prediction/prediction.component.html | 4 ++++ src/app/prediction/prediction.component.ts | 20 ++++++++++++-------- 5 files changed, 40 insertions(+), 20 deletions(-) diff --git a/package-lock.json b/package-lock.json index fae6f83..56946ee 100644 --- a/package-lock.json +++ b/package-lock.json @@ -2034,6 +2034,12 @@ "integrity": "sha512-EaObqwIvayI5a8dCzhFrjKzVwKLxjoG9T6Ppd5CEo07LRKfQ8Yokw54r5+Wq7FaBQ+yXRvQAYPrHwya1/UFt9g==", "dev": true }, + "@types/file-saver": { + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/@types/file-saver/-/file-saver-2.0.1.tgz", + "integrity": "sha512-g1QUuhYVVAamfCifK7oB7G3aIl4BbOyzDOqVyUfEr4tfBKrXfeH+M+Tg7HKCXSrbzxYdhyCP7z9WbKo0R2hBCw==", + "dev": true + }, "@types/glob": { "version": "7.1.1", "resolved": "https://registry.npmjs.org/@types/glob/-/glob-7.1.1.tgz", @@ -2067,9 +2073,9 @@ "dev": true }, "@types/node": { - "version": "12.12.41", - "resolved": "https://registry.npmjs.org/@types/node/-/node-12.12.41.tgz", - "integrity": "sha512-Q+eSkdYQJ2XK1AJnr4Ji8Gvk3sRDybEwfTvtL9CA25FFUSD2EgZQewN6VCyWYZCXg5MWZdwogdTNBhlWRcWS1w==", + "version": "12.19.9", + "resolved": "https://registry.npmjs.org/@types/node/-/node-12.19.9.tgz", + "integrity": "sha512-yj0DOaQeUrk3nJ0bd3Y5PeDRJ6W0r+kilosLA+dzF3dola/o9hxhMSg2sFvVcA2UHS5JSOsZp4S0c1OEXc4m1Q==", "dev": true }, "@types/q": { @@ -5515,6 +5521,11 @@ "schema-utils": "^2.6.5" } }, + "file-saver": { + "version": "2.0.5", + "resolved": "https://registry.npmjs.org/file-saver/-/file-saver-2.0.5.tgz", + "integrity": "sha512-P9bmyZ3h/PRG+Nzga+rbdI4OEpNDzAVyy74uVO9ATgzLK6VtAsYybF/+TOCvrc0MO793d6+42lLyZTw7/ArVzA==" + }, "file-uri-to-path": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/file-uri-to-path/-/file-uri-to-path-1.0.0.tgz", diff --git a/package.json b/package.json index 7976391..437f406 100644 --- a/package.json +++ b/package.json @@ -28,6 +28,7 @@ "angular-2-local-storage": "^3.0.2", "chart.js": "^2.9.3", "chartjs-plugin-datalabels": "^0.7.0", + "file-saver": "^2.0.5", "flatpickr": "^4.6.3", "lodash": "^4.17.15", "ng2-charts": "^2.3.2", @@ -42,9 +43,9 @@ "@angular/cli": "~9.1.6", "@angular/compiler-cli": "~9.1.7", "@angular/language-service": "~9.1.7", + "@types/file-saver": "^2.0.1", "@types/jasmine": "~3.3.8", "@types/jasminewd2": "~2.0.3", - "@types/node": "^12.11.1", "codelyzer": "^5.1.2", "jasmine-core": "~3.4.0", "jasmine-spec-reporter": "~4.2.1", diff --git a/src/app/app.module.ts b/src/app/app.module.ts index 37a5f20..e048d84 100644 --- a/src/app/app.module.ts +++ b/src/app/app.module.ts @@ -4,20 +4,20 @@ import { ChartsModule } from 'ng2-charts'; import { AppRoutingModule } from './app-routing.module'; import { AppComponent } from './app.component'; -import {FormFieldsModule, ModalService, RbUiComponentsModule} from '@inst-iot/bosch-angular-ui-components'; -import {LoginComponent} from './login/login.component'; +import { FormFieldsModule, ModalService, RbUiComponentsModule } from '@inst-iot/bosch-angular-ui-components'; +import { LoginComponent } from './login/login.component'; import { HomeComponent } from './home/home.component'; -import {FormsModule, ReactiveFormsModule} from '@angular/forms'; -import {LocalStorageModule} from 'angular-2-local-storage'; -import {HttpClientModule} from '@angular/common/http'; +import { FormsModule, ReactiveFormsModule } from '@angular/forms'; +import { LocalStorageModule } from 'angular-2-local-storage'; +import { HttpClientModule } from '@angular/common/http'; import { SamplesComponent } from './samples/samples.component'; -import {RbCustomInputsModule} from './rb-custom-inputs/rb-custom-inputs.module'; +import { RbCustomInputsModule } from './rb-custom-inputs/rb-custom-inputs.module'; import { SampleComponent } from './sample/sample.component'; import { ValidateDirective } from './validate.directive'; -import {CommonModule} from '@angular/common'; +import { CommonModule } from '@angular/common'; import { ErrorComponent } from './error/error.component'; import { ObjectPipe } from './object.pipe'; -import {BrowserAnimationsModule} from '@angular/platform-browser/animations'; +import { BrowserAnimationsModule } from '@angular/platform-browser/animations'; import { DocumentationComponent } from './documentation/documentation.component'; import { ImgMagnifierComponent } from './img-magnifier/img-magnifier.component'; import { ExistsPipe } from './exists.pipe'; diff --git a/src/app/prediction/prediction.component.html b/src/app/prediction/prediction.component.html index f4ee2f0..e4b01fc 100644 --- a/src/app/prediction/prediction.component.html +++ b/src/app/prediction/prediction.component.html @@ -60,6 +60,10 @@ + + + Export to CSV +
diff --git a/src/app/prediction/prediction.component.ts b/src/app/prediction/prediction.component.ts index 511a126..3afe646 100644 --- a/src/app/prediction/prediction.component.ts +++ b/src/app/prediction/prediction.component.ts @@ -1,11 +1,12 @@ import { Component, OnInit } from '@angular/core'; -import {ChartOptions} from 'chart.js'; -import {ApiService} from '../services/api.service'; -import {animate, style, transition, trigger} from '@angular/animations'; +import { ChartOptions } from 'chart.js'; +import { ApiService } from '../services/api.service'; +import { animate, style, transition, trigger } from '@angular/animations'; import cloneDeep from 'lodash/cloneDeep'; import omit from 'lodash/omit'; -import {DataService} from '../services/data.service'; -import {ModelItemModel} from '../models/model-item.model'; +import { DataService } from '../services/data.service'; +import { ModelItemModel } from '../models/model-item.model'; +import * as FileSaver from 'file-saver' @Component({ @@ -29,7 +30,7 @@ import {ModelItemModel} from '../models/model-item.model'; }) export class PredictionComponent implements OnInit { - result: {predictions: any[], mean: any[]}; // prediction result from python container + result: { predictions: any[], mean: any[] }; // prediction result from python container loading = false; activeGroup: ModelItemModel = new ModelItemModel(); activeModelIndex = 0; @@ -121,7 +122,10 @@ export class PredictionComponent implements OnInit { this.result = undefined; } - clip(str) { // clip spaces at start and end - return str.replace(/^\s*(.*?)\s*$/, '$1'); + exportCSV() { + const zip = (a, b) => a.map((k, i) => [k, b[i]]); + const predictions = zip(this.spectrumNames, this.result.predictions.map(p => p[0].value)); + const csv = predictions.map(line => line.join(";")).join("\n"); + FileSaver.saveAs(new Blob([csv], { type: 'text/csv;charset=utf-8' }), "predictions.csv"); } }