aboutsummaryrefslogtreecommitdiff
path: root/frontend/src/app/experiment/experiment.component.ts
blob: 2d0f6ec5ffe3f5740a5a9bfd0429549abbd5dd70 (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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
import { Component, OnInit } from '@angular/core';
import Experiment, { NullValReplacer, NullValueOptions, ReplaceWith, Encoding } from '../_data/Experiment';
import Model,{ProblemType} from '../_data/Model';
import Dataset, { ColumnInfo } from '../_data/Dataset';
import { ModelsService } from '../_services/models.service';
import Shared from '../Shared';
import { ExperimentsService } from '../_services/experiments.service';
import { ColumnEncoding } from '../_data/Experiment';

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

  experiment: Experiment = new Experiment();
  selectedModel?: Model;
  selectedDataset?: Dataset;
  trainingResult: any; // any za sad, promeni kasnije

  NullValueOptions = NullValueOptions;
  ReplaceWith = ReplaceWith;
  Encoding = Encoding;
  ColumnEncoding = ColumnEncoding;
  Object = Object;
  ProblemType=ProblemType;
  selectedColumnsInfoArray: ColumnInfo[] = [];
  selectedNotNullColumnsArray: string[] = [];

  tempTestSetDistribution = 90;

  constructor(private modelsService: ModelsService, private experimentsService: ExperimentsService) {
  }

  ngOnInit(): void {
  }

  updateDataset(dataset: Dataset) {
    this.selectedDataset = dataset;
    this.selectedColumnsInfoArray = this.selectedDataset.columnInfo;
    this.selectedNotNullColumnsArray = [];
    this.experiment.outputColumn = this.selectedDataset.columnInfo[this.selectedDataset.columnInfo.length - 1].columnName;

    this.resetColumnEncodings();
    console.log(this.experiment.encodings);
  }

  resetColumnEncodings() {
    this.experiment.encodings = [];
    for (let i = 0; i < this.selectedColumnsInfoArray.length; i++) {
      this.experiment.encodings.push(new ColumnEncoding(this.selectedColumnsInfoArray[i].columnName, Encoding.Label));
    }
  }

  getInputById(id: string): HTMLInputElement {
    return document.getElementById(id) as HTMLInputElement;
  }

  arrayColumn = (arr: any[][], n: number) => [...this.dropEmptyString(new Set(arr.map(x => x[n])))];

  dropEmptyString(set: Set<any>): Set<string> {
    if (set.has(""))
      set.delete("");
    if (set.has(null))
      set.delete(null);
    if (set.has(undefined))
      set.delete(undefined);
    return set;
  }

  emptyFillTextInput(colName: string) {
    (<HTMLInputElement>document.getElementById("fillText_" + colName)).value = "";
  }

  checkFillColRadio(colName: string) {
    (<HTMLInputElement>document.getElementById("fillCol_" + colName)).checked = true;
  }

  checkedColumnsChanged(checkedColumnInfo: ColumnInfo, buttonType: number) { //0-input,1-output
    let col = this.selectedColumnsInfoArray.find(x => x.columnName == checkedColumnInfo.columnName);
    if (buttonType == 0) { //inputCol
      if (col == undefined)
        this.selectedColumnsInfoArray.push(checkedColumnInfo);
      else
        this.selectedColumnsInfoArray = this.selectedColumnsInfoArray.filter(x => x.columnName != checkedColumnInfo.columnName);
    }
    else { //outputCol
      if (col == undefined) //ako je vec cekiran neki output, samo dodaj sad ovaj, a taj output postaje input i ostaje u nizu
        this.selectedColumnsInfoArray.push(checkedColumnInfo);
    }
    //console.log(this.selectedColumnsInfoArray);
  }

  replace(event: Event, column: ColumnInfo) {
    let option = (<HTMLInputElement>event.target).value;

    const input = (<HTMLInputElement>document.getElementById("fillText_" + column.columnName));
    if (column.isNumber) {
      switch (option) {
        case ReplaceWith.Max:
          input.value = "" + column.max;
          break;
        case ReplaceWith.Min:
          input.value = "" + column.min;
          break;
        case ReplaceWith.Mean:
          input.value = "" + column.mean;
          break;
        case ReplaceWith.Median:
          input.value = "" + column.median;
          break;
        case ReplaceWith.None:
          break;
      }
    } else {
      input.value = option;
    }
  }

  getSelectedColumnsArrayWithoutNullVals(): string[] {
    let colNames: string[] = [];

    for (let i = 0; i < this.selectedColumnsInfoArray.length; i++) {
      let oneColInfo = this.selectedColumnsInfoArray[i];
      if (oneColInfo.numNulls == 0)
        colNames.push(oneColInfo.columnName);
    }
    return colNames;
  }

  getNullValuesReplacersArray(): NullValReplacer[] {
    let array: NullValReplacer[] = [];

    if (this.experiment.nullValues == NullValueOptions.Replace) {

      for (let i = 0; i < this.selectedColumnsInfoArray.length; i++) {
        let oneColInfo = this.selectedColumnsInfoArray[i];

        if (oneColInfo.numNulls > 0) { //ako kolona nema null vrednosti, ne dodajemo je u niz
          if ((<HTMLInputElement>document.getElementById("delCol_" + oneColInfo.columnName)).checked) {
            array.push({
              column: oneColInfo.columnName,
              option: NullValueOptions.DeleteColumns,
              value: ""
            });
          }
          else if ((<HTMLInputElement>document.getElementById("delRows_" + oneColInfo.columnName)).checked) {
            array.push({
              column: oneColInfo.columnName,
              option: NullValueOptions.DeleteRows,
              value: ""
            });
          }
          else if (((<HTMLInputElement>document.getElementById("fillCol_" + oneColInfo.columnName)).checked)) {
            array.push({
              column: oneColInfo.columnName,
              option: NullValueOptions.Replace,
              value: (<HTMLInputElement>document.getElementById("fillText_" + oneColInfo.columnName)).value
            });
          }
        }
      }
    }
    return array;
  }

  saveExperiment() {
    if (this.selectedDataset == undefined) {
      Shared.openDialog("Greška", "Izvor podataka nije izabran!");
      return;
    }
    if (this.experiment.outputColumn == '') {
      Shared.openDialog("Greška", "Molimo Vas da izaberete izlaznu kolonu.");
      return;
    }
    if (this.selectedColumnsInfoArray.length <= 1) { //jer izlazna je izabrana
      Shared.openDialog("Greška", "Molimo Vas da izaberete ulazne kolone.");
      return;
    }

    this.experiment._id = '';
    this.experiment.uploaderId = '';
    this.experiment.datasetId = this.selectedDataset._id;

    let pom = this.selectedColumnsInfoArray.filter(x => x.columnName != this.experiment.outputColumn);
    for (let i = 0; i < pom.length; i++)
      this.experiment.inputColumns.push(pom[i].columnName);

    this.selectedColumnsInfoArray = this.selectedColumnsInfoArray.filter(x => x.numNulls > 0); //obavezno
    this.experiment.nullValuesReplacers = this.getNullValuesReplacersArray();

    this.experiment.randomTestSetDistribution = 1 - Math.round(this.tempTestSetDistribution / 100 * 10) / 10;

    console.log("Eksperiment:", this.experiment);

    this.experimentsService.addExperiment(this.experiment).subscribe((response) => {
      this.experiment = response;

      this.selectedColumnsInfoArray = []; 
      this.selectedNotNullColumnsArray = [];
      this.experiment.encodings = [];

      Shared.openDialog("Obaveštenje", "Eksperiment je uspešno kreiran.");
    }, (error) => {
      if (error.error == "Experiment with this name exists") {
        Shared.openDialog("Greška", "Eksperiment sa unetim nazivom već postoji u Vašoj kolekciji. Unesite neki drugi naziv.");
      }
    });
  }

  countSelectedNullCols(): number {
    let counter: number = 0;

    for (let i = 0; i < this.selectedColumnsInfoArray.length; i++) {
      let oneColInfo = this.selectedColumnsInfoArray[i];
      if (oneColInfo.numNulls > 0)
        ++counter;
    }
    return counter;
  }
}