aboutsummaryrefslogtreecommitdiff
path: root/frontend/src/app/_elements/model-load/model-load.component.ts
blob: fb4b3fd070fa85d861fe177730a8ac00d6bd4daf (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import { Component, OnInit, ViewChild, Output, EventEmitter, Input } from '@angular/core';
import Shared from 'src/app/Shared';
import Experiment from 'src/app/_data/Experiment';
import Model, { ActivationFunction, LossFunction, LossFunctionBinaryClassification, LossFunctionMultiClassification, LossFunctionRegression, Metrics, MetricsBinaryClassification, MetricsMultiClassification, MetricsRegression, NullValueOptions, Optimizer, ProblemType } from 'src/app/_data/Model';
import { AuthService } from 'src/app/_services/auth.service';
import { ModelsService } from 'src/app/_services/models.service';
import { SignalRService } from 'src/app/_services/signal-r.service';
import { GraphComponent } from '../graph/graph.component';


@Component({
  selector: 'app-model-load',
  templateUrl: './model-load.component.html',
  styleUrls: ['./model-load.component.css']
})
export class ModelLoadComponent implements OnInit {

  @ViewChild(GraphComponent) graph!: GraphComponent;
  @Input() forExperiment?: Experiment;
  @Output() selectedModelChangeEvent = new EventEmitter<Model>();

  newModel: Model = new Model();
  myModels?: Model[];
  selectedModel?: Model;

  ProblemType = ProblemType;
  ActivationFunction = ActivationFunction;
  metrics: any = Metrics;
  LossFunction = LossFunction;
  Optimizer = Optimizer;
  Object = Object;
  document = document;
  shared = Shared;

  term: string = "";
  selectedMetrics = [];
  lossFunction: any = LossFunction;

  showMyModels: boolean = true;

  batchSizePower: number = 2;

  constructor(private modelsService: ModelsService, private authService: AuthService) {
    //console.log("forExperiment = ", this.forExperiment);
    this.fetchModels();

    this.authService.loggedInEvent.subscribe(_ => {
      this.fetchModels();
    })
  }

  fetchModels(andSelectWithId: string | null = '') {
    //if (this.forExperiment == undefined) {
    this.modelsService.getMyModels().subscribe((models) => {
      this.myModels = models.reverse();
      this.selectThisModel(this.myModels.filter(x => x._id == andSelectWithId)[0]);
    });
    /*}
    else {
      this.modelsService.getMyModelsByType(ProblemType.Regression).subscribe((models) => {
        this.myModels = models;
        //console.log("modeli po tipu: ", this.myModels);
      });
    }*/
  }

  ngOnInit(): void {
  }

  updateBatchSize() {
    this.newModel.batchSize = 2 ** this.batchSizePower;
  }

  updateGraph() {
    this.graph.update();
  }

  getMetrics() {
    this.newModel.metrics = [];
    let cb = document.getElementsByName("cbmetrics");

    for (let i = 0; i < cb.length; i++) {
      let chb = <HTMLInputElement>cb[i];
      if (chb.checked == true)
        this.newModel.metrics.push(chb.value);
    }
  }

  uploadModel() {
    this.getMetrics();

    this.newModel.uploaderId = Shared.userId;

    this.modelsService.addModel(this.newModel).subscribe((response) => {
      console.log(this.newModel);
      //Shared.openDialog('Model dodat', 'Model je uspešno dodat u bazu.');

      Shared.openYesNoDialog("Model dodat", "Model je uspešno dodat u bazu. Da li želite da nastavite treniranje sa dodatim modelom?", () => {
        this.fetchModels(response._id);
        this.showMyModels = true;
      });
      this.fetchModels();
    }, (error) => {
      Shared.openDialog('Greška', 'Model sa unetim nazivom već postoji u Vašoj kolekciji. Promenite naziv modela i nastavite sa kreiranim datasetom.');
    });
  }

  filterOptions() {
    switch (this.newModel.type) {
      case 'regresioni':
        this.lossFunction = LossFunctionRegression;
        this.metrics = MetricsRegression;
        break;
      case 'binarni-klasifikacioni':
        this.lossFunction = LossFunctionBinaryClassification;
        this.metrics = MetricsBinaryClassification;
        break;
      case 'multi-klasifikacioni':
        this.lossFunction = LossFunctionMultiClassification;
        this.metrics = MetricsMultiClassification;
        break;
      default:
        break;
    }
  }

  viewMyModelsForm() {
    this.showMyModels = true;
  }
  viewNewModelForm() {
    this.showMyModels = false;
  }

  selectThisModel(model: Model) {
    this.selectedModel = model;
    this.selectedModelChangeEvent.emit(this.selectedModel);
  }

}