code improvements

This commit is contained in:
VLE2FE
2020-09-03 15:51:53 +02:00
parent 1440e9a6fc
commit c38d0be457
73 changed files with 276 additions and 1686 deletions

View File

@ -1,4 +1,3 @@
<rb-tab-panel (tabChanged)="groupChange($event)">
<ng-container *ngFor="let group of d.arr.modelGroups; index as i">
<div *rbTabPanelItem="group.group; id: i"></div>

View File

@ -1,47 +1,5 @@
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { PredictionComponent } from './prediction.component';
import {ApiService} from '../services/api.service';
import {RbUiComponentsModule} from '@inst-iot/bosch-angular-ui-components';
import {FormsModule} from '@angular/forms';
import {ValidationService} from '../services/validation.service';
import {DataService} from '../services/data.service';
import {ChartsModule} from 'ng2-charts';
// TODO
let apiServiceSpy: jasmine.SpyObj<ApiService>;
describe('PredictionComponent', () => {
let component: PredictionComponent;
let fixture: ComponentFixture<PredictionComponent>;
beforeEach(async(() => {
const apiSpy = jasmine.createSpyObj('ApiService', ['post']);
TestBed.configureTestingModule({
declarations: [ PredictionComponent ],
imports: [
RbUiComponentsModule,
ChartsModule
],
providers: [
{provide: ApiService, useValue: apiSpy}
]
})
.compileComponents();
apiServiceSpy = TestBed.inject(ApiService) as jasmine.SpyObj<ApiService>;
}));
beforeEach(() => {
fixture = TestBed.createComponent(PredictionComponent);
component = fixture.componentInstance;
component.ngOnInit();
fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeTruthy();
});
});

View File

@ -29,11 +29,12 @@ import {ModelItemModel} from '../models/model-item.model';
})
export class PredictionComponent implements OnInit {
result: {predictions: string[], mean: string};
result: {predictions: string[], mean: string}; // prediction result from python container
loading = false;
activeGroup: ModelItemModel = new ModelItemModel();
activeModelIndex = 0;
multipleSamples = false; // if true, spectra belong to different samples, otherwise multiple spectra from the same sample are given
// if true, spectra belong to different samples, otherwise multiple spectra from the same sample are given
multipleSamples = false;
spectrumNames: string[] = [];
spectrum: string[][] = [[]];
flattenedSpectra = [];
@ -50,7 +51,7 @@ export class PredictionComponent implements OnInit {
readonly chartOptions: ChartOptions = {
scales: {
xAxes: [{ticks: {min: 400, max: 4000, stepSize: 400, reverse: true}}],
yAxes: [{ticks: {min: 0, max: 1}}]
yAxes: [{ticks: {}}]
},
responsive: true,
tooltips: {enabled: false},
@ -82,13 +83,16 @@ export class PredictionComponent implements OnInit {
if (files.hasOwnProperty(i)) {
const fileReader = new FileReader();
fileReader.onload = () => {
// parse to database spectrum representation
this.spectrum = fileReader.result.toString().split('\r\n').map(e => e.split(',').map(el => parseFloat(el)))
.filter(el => el.length === 2) as any;
// flatten to format needed for prediction
this.flattenedSpectra[i] = {labels: this.spectrum.map(e => e[0]), values: this.spectrum.map(e => e[1])};
// add to chart
this.chart[i] = cloneDeep(this.chartInit);
this.chart[i].data = this.spectrum.map(e => ({x: parseFloat(e[0]), y: parseFloat(e[1])}));
load --;
if (load <= 0) {
if (load <= 0) { // all loaded
this.loadPrediction();
}
};
@ -99,24 +103,21 @@ export class PredictionComponent implements OnInit {
loadPrediction() {
this.loading = true;
console.log(this.activeGroup);
console.log(this.activeModelIndex);
this.api.post<any>(this.activeGroup.models[this.activeModelIndex].url, this.flattenedSpectra, data => {
this.result = {
this.result = { // parse result into prediction and mean string
predictions: Object.entries(omit(data, ['mean', 'std', 'label']))
.map((p: any) => p[1].map(e => `${p[0]}: ${e} ${data.label[p[0]]}`))
.reduce((s, e) => s.map((el, i) => this.clip(el) + ', ' + e[i])),
mean: Object.keys(data.mean).map(e =>
this.clip(`${e}: ${data.mean[e]} ${data.label[e]}`) + (data.std[e] !== '' ? ` (standard deviation: ${data.std[e]})` : '')
this.clip(`${e}: ${data.mean[e]} ${data.label[e]}`) +
(data.std[e] !== '' ? ` (standard deviation: ${data.std[e]})` : '')
).join(', ')
};
console.log(this.result);
this.loading = false;
});
}
groupChange(index) {
console.log(index);
groupChange(index) { // group was changed
this.activeGroup = this.d.arr.modelGroups[index];
this.activeModelIndex = 0;
this.result = undefined;