@webwriter/deep-learning-model 0.0.1 → 1.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (186) hide show
  1. package/.github/workflows/main.yml +8 -0
  2. package/LICENSE +7 -0
  3. package/README.md +290 -0
  4. package/components/dataset-input/dataset-input.styles.ts +20 -0
  5. package/components/dataset-input/dataset-input.ts +196 -0
  6. package/components/inference-fields/class-input.ts +83 -0
  7. package/components/inference-fields/class-output.ts +62 -0
  8. package/components/inference-fields/image-input.ts +182 -0
  9. package/components/inference-fields/image.ts +82 -0
  10. package/components/inference-fields/inference-interfaces.ts +15 -0
  11. package/components/inference-fields/number-input.ts +62 -0
  12. package/components/inference-fields/number-output.ts +56 -0
  13. package/components/model-editor.ts +288 -0
  14. package/components/model-tests/bad-samples.ts +152 -0
  15. package/components/model-tests/loss-distribution.ts +174 -0
  16. package/components/reference-element-select.ts +105 -0
  17. package/custom-elements.json +1470 -0
  18. package/custom.d.ts +180 -0
  19. package/dist/widgets/webwriter-feature-engineering.css +444 -0
  20. package/dist/widgets/webwriter-feature-engineering.js +112763 -0
  21. package/dist/widgets/webwriter-model-designing.css +444 -0
  22. package/dist/widgets/webwriter-model-designing.js +114587 -0
  23. package/dist/widgets/webwriter-model-evaluation.css +444 -0
  24. package/dist/widgets/webwriter-model-evaluation.js +119010 -0
  25. package/dist/widgets/webwriter-model-prediction.css +444 -0
  26. package/dist/widgets/webwriter-model-prediction.js +109251 -0
  27. package/dist/widgets/webwriter-model-training.css +444 -0
  28. package/dist/widgets/webwriter-model-training.js +121465 -0
  29. package/editing-config.json +73 -0
  30. package/icon.svg +1 -0
  31. package/lib/dataset/class.ts +155 -0
  32. package/lib/dataset/dataset.ts +293 -0
  33. package/lib/dataset/feature.ts +110 -0
  34. package/lib/dataset/image.ts +312 -0
  35. package/lib/dataset/linear.ts +120 -0
  36. package/lib/layers/activation.ts +48 -0
  37. package/lib/layers/add.ts +50 -0
  38. package/lib/layers/attention.ts +74 -0
  39. package/lib/layers/concat.ts +50 -0
  40. package/lib/layers/conv.ts +83 -0
  41. package/lib/layers/dropout.ts +44 -0
  42. package/lib/layers/fcl.ts +66 -0
  43. package/lib/layers/flatten.ts +48 -0
  44. package/lib/layers/index.ts +770 -0
  45. package/lib/layers/input.ts +55 -0
  46. package/lib/layers/noise.ts +43 -0
  47. package/lib/layers/output.ts +64 -0
  48. package/lib/layers/reshape.ts +76 -0
  49. package/lib/layers/transposeconv.ts +90 -0
  50. package/lib/tflib.ts +10 -0
  51. package/lit-localize.json +47 -0
  52. package/localization/ar.xlf +553 -0
  53. package/localization/bg.xlf +552 -0
  54. package/localization/cs.xlf +553 -0
  55. package/localization/da.xlf +552 -0
  56. package/localization/de.xlf +552 -0
  57. package/localization/el.xlf +554 -0
  58. package/localization/es.xlf +553 -0
  59. package/localization/et.xlf +553 -0
  60. package/localization/fi.xlf +552 -0
  61. package/localization/fr.xlf +553 -0
  62. package/localization/generated/ar.ts +159 -0
  63. package/localization/generated/bg.ts +158 -0
  64. package/localization/generated/cs.ts +159 -0
  65. package/localization/generated/da.ts +158 -0
  66. package/localization/generated/de.ts +158 -0
  67. package/localization/generated/el.ts +160 -0
  68. package/localization/generated/es.ts +159 -0
  69. package/localization/generated/et.ts +159 -0
  70. package/localization/generated/fi.ts +158 -0
  71. package/localization/generated/fr.ts +159 -0
  72. package/localization/generated/hu.ts +158 -0
  73. package/localization/generated/id.ts +159 -0
  74. package/localization/generated/index.js +3 -0
  75. package/localization/generated/it.ts +159 -0
  76. package/localization/generated/ja.ts +158 -0
  77. package/localization/generated/ko.ts +158 -0
  78. package/localization/generated/locale-codes.js +83 -0
  79. package/localization/generated/lt.ts +158 -0
  80. package/localization/generated/lv.ts +158 -0
  81. package/localization/generated/nb.ts +158 -0
  82. package/localization/generated/nl.ts +158 -0
  83. package/localization/generated/pl.ts +159 -0
  84. package/localization/generated/pt-BR.ts +159 -0
  85. package/localization/generated/pt-PT.ts +159 -0
  86. package/localization/generated/ro.ts +159 -0
  87. package/localization/generated/ru.ts +159 -0
  88. package/localization/generated/sk.ts +159 -0
  89. package/localization/generated/sl.ts +159 -0
  90. package/localization/generated/sv.ts +158 -0
  91. package/localization/generated/tr.ts +158 -0
  92. package/localization/generated/uk.ts +158 -0
  93. package/localization/generated/zh-hans.ts +141 -0
  94. package/localization/generated/zh-hant.ts +141 -0
  95. package/localization/hu.xlf +552 -0
  96. package/localization/id.xlf +553 -0
  97. package/localization/it.xlf +553 -0
  98. package/localization/ja.xlf +552 -0
  99. package/localization/ko.xlf +552 -0
  100. package/localization/lt.xlf +552 -0
  101. package/localization/lv.xlf +552 -0
  102. package/localization/nb.xlf +552 -0
  103. package/localization/nl.xlf +552 -0
  104. package/localization/pkg/ar.xliff +7 -0
  105. package/localization/pkg/bg.xliff +7 -0
  106. package/localization/pkg/cs.xliff +7 -0
  107. package/localization/pkg/da.xliff +7 -0
  108. package/localization/pkg/de.xliff +7 -0
  109. package/localization/pkg/el.xliff +7 -0
  110. package/localization/pkg/es.xliff +7 -0
  111. package/localization/pkg/et.xliff +7 -0
  112. package/localization/pkg/fi.xliff +7 -0
  113. package/localization/pkg/fr.xliff +7 -0
  114. package/localization/pkg/hu.xliff +7 -0
  115. package/localization/pkg/id.xliff +7 -0
  116. package/localization/pkg/it.xliff +7 -0
  117. package/localization/pkg/ja.xliff +7 -0
  118. package/localization/pkg/ko.xliff +7 -0
  119. package/localization/pkg/lt.xliff +7 -0
  120. package/localization/pkg/lv.xliff +7 -0
  121. package/localization/pkg/nb.xliff +7 -0
  122. package/localization/pkg/nl.xliff +7 -0
  123. package/localization/pkg/pl.xliff +7 -0
  124. package/localization/pkg/pt-BR.xliff +7 -0
  125. package/localization/pkg/pt-PT.xliff +7 -0
  126. package/localization/pkg/ro.xliff +7 -0
  127. package/localization/pkg/ru.xliff +7 -0
  128. package/localization/pkg/sk.xliff +7 -0
  129. package/localization/pkg/sl.xliff +7 -0
  130. package/localization/pkg/sv.xliff +7 -0
  131. package/localization/pkg/tr.xliff +7 -0
  132. package/localization/pkg/uk.xliff +7 -0
  133. package/localization/pkg/zh-hans.xliff +7 -0
  134. package/localization/pkg/zh-hant.xliff +7 -0
  135. package/localization/pl.xlf +553 -0
  136. package/localization/pt-BR.xlf +553 -0
  137. package/localization/pt-PT.xlf +553 -0
  138. package/localization/ro.xlf +553 -0
  139. package/localization/ru.xlf +553 -0
  140. package/localization/sk.xlf +553 -0
  141. package/localization/sl.xlf +553 -0
  142. package/localization/snippets/ar.xliff +16 -0
  143. package/localization/snippets/bg.xliff +16 -0
  144. package/localization/snippets/cs.xliff +16 -0
  145. package/localization/snippets/da.xliff +16 -0
  146. package/localization/snippets/de.xliff +16 -0
  147. package/localization/snippets/el.xliff +16 -0
  148. package/localization/snippets/es.xliff +16 -0
  149. package/localization/snippets/et.xliff +16 -0
  150. package/localization/snippets/fi.xliff +16 -0
  151. package/localization/snippets/fr.xliff +16 -0
  152. package/localization/snippets/hu.xliff +16 -0
  153. package/localization/snippets/id.xliff +16 -0
  154. package/localization/snippets/it.xliff +16 -0
  155. package/localization/snippets/ja.xliff +16 -0
  156. package/localization/snippets/ko.xliff +16 -0
  157. package/localization/snippets/lt.xliff +16 -0
  158. package/localization/snippets/lv.xliff +16 -0
  159. package/localization/snippets/nb.xliff +16 -0
  160. package/localization/snippets/nl.xliff +16 -0
  161. package/localization/snippets/pl.xliff +16 -0
  162. package/localization/snippets/pt-BR.xliff +16 -0
  163. package/localization/snippets/pt-PT.xliff +16 -0
  164. package/localization/snippets/ro.xliff +16 -0
  165. package/localization/snippets/ru.xliff +16 -0
  166. package/localization/snippets/sk.xliff +16 -0
  167. package/localization/snippets/sl.xliff +16 -0
  168. package/localization/snippets/sv.xliff +16 -0
  169. package/localization/snippets/tr.xliff +16 -0
  170. package/localization/snippets/uk.xliff +16 -0
  171. package/localization/snippets/zh-hans.xliff +16 -0
  172. package/localization/snippets/zh-hant.xliff +16 -0
  173. package/localization/sv.xlf +552 -0
  174. package/localization/tr.xlf +552 -0
  175. package/localization/uk.xlf +552 -0
  176. package/localization/zh-hans.xlf +552 -0
  177. package/localization/zh-hant.xlf +552 -0
  178. package/package.json +103 -2
  179. package/snippets/MNIST.html +21 -0
  180. package/test.json +63 -0
  181. package/tsconfig.json +8 -0
  182. package/widgets/webwriter-feature-engineering.ts +421 -0
  183. package/widgets/webwriter-model-designing.ts +323 -0
  184. package/widgets/webwriter-model-evaluation.ts +215 -0
  185. package/widgets/webwriter-model-prediction.ts +307 -0
  186. package/widgets/webwriter-model-training.ts +709 -0
package/test.json ADDED
@@ -0,0 +1,63 @@
1
+ [
2
+ {
3
+ "id": 1,
4
+ "posX": 63,
5
+ "posY": 208,
6
+ "inputConnections": {},
7
+ "type": "input",
8
+ "select": "image_bytes",
9
+ "dim": [
10
+ 28,
11
+ 28,
12
+ 1
13
+ ]
14
+ },
15
+ {
16
+ "id": 2,
17
+ "posX": 234,
18
+ "posY": 208,
19
+ "inputConnections": {
20
+ "input_1": {
21
+ "node": "1",
22
+ "input": "output_1"
23
+ }
24
+ },
25
+ "type": "fcl",
26
+ "dim": [
27
+ 10
28
+ ]
29
+ },
30
+ {
31
+ "id": 3,
32
+ "posX": 403,
33
+ "posY": 209,
34
+ "inputConnections": {
35
+ "input_1": {
36
+ "node": "2",
37
+ "input": "output_1"
38
+ }
39
+ },
40
+ "type": "activation",
41
+ "dim": [
42
+ 10
43
+ ],
44
+ "act": "Softmax"
45
+ },
46
+ {
47
+ "id": 4,
48
+ "posX": 572,
49
+ "posY": 209,
50
+ "inputConnections": {
51
+ "input_1": {
52
+ "node": "3",
53
+ "input": "output_1"
54
+ }
55
+ },
56
+ "type": "output",
57
+ "select": "label",
58
+ "dim": [
59
+ 10
60
+ ],
61
+ "loss": "BCE"
62
+ }
63
+ ]
package/tsconfig.json ADDED
@@ -0,0 +1,8 @@
1
+ {
2
+ "compilerOptions": {
3
+ "experimentalDecorators": true,
4
+ "target": "es2022",
5
+ "moduleResolution": "Bundler",
6
+ "module": "ES2022"
7
+ }
8
+ }
@@ -0,0 +1,421 @@
1
+ import SlButtonGroup from "@shoelace-style/shoelace/dist/components/button-group/button-group.component.js";
2
+ import SlButton from "@shoelace-style/shoelace/dist/components/button/button.component.js";
3
+ import SlInput from "@shoelace-style/shoelace/dist/components/input/input.component.js";
4
+ import SlOption from "@shoelace-style/shoelace/dist/components/option/option.component.js";
5
+ import SlSelect from "@shoelace-style/shoelace/dist/components/select/select.component.js";
6
+ import SlTabGroup from "@shoelace-style/shoelace/dist/components/tab-group/tab-group.component.js";
7
+ import SlTab from "@shoelace-style/shoelace/dist/components/tab/tab.component.js";
8
+ import SlTooltip from "@shoelace-style/shoelace/dist/components/tooltip/tooltip.component.js";
9
+ import "@shoelace-style/shoelace/dist/themes/light.css";
10
+ import { LitElementWw } from "@webwriter/lit";
11
+ import { css, html, PropertyValues, TemplateResult } from "lit";
12
+ import { customElement, property } from "lit/decorators.js";
13
+ import { DatasetInput } from "../components/dataset-input/dataset-input";
14
+ import "../lib/dataset/class";
15
+ import { Dataset, TransformedDataset, } from "../lib/dataset/dataset";
16
+ import { FeatureTransformation, getFeatureTransformation, getFeatureTransformations } from "../lib/dataset/feature";
17
+ import "../lib/dataset/image";
18
+ import "../lib/dataset/linear";
19
+ import getTF from "../lib/tflib";
20
+ import { msg, str } from "@lit/localize";
21
+ import LOCALIZE from "../localization/generated";
22
+ type Constructor<T> = new (...args: any[]) => T;
23
+
24
+ BigInt.prototype["toJSON"] = function () {
25
+ return this.toString();
26
+ };
27
+ Uint8Array.prototype["toJSON"] = function () {
28
+ // Convert Uint8Array to base64 for efficient storage
29
+ let binary = '';
30
+ const len = this.length;
31
+ for (let i = 0; i < len; i++) {
32
+ binary += String.fromCharCode(this[i]);
33
+ }
34
+ return btoa(binary);
35
+ };
36
+
37
+ type FieldProp = {
38
+ type:"Class Encoding"|"Number Encoding"|"Image Encoding"|"Ignore",
39
+ settings:any
40
+ }
41
+
42
+ /**
43
+ * A widget that allows feature engineering on a dataset
44
+ *
45
+ * @event change - Fired when the dataset changes. Detail contains { dataset: Dataset, tensorDataset: TransformedDataset }
46
+ */
47
+ @customElement("webwriter-feature-engineering")
48
+ export class AiWidgetsFeatures extends LitElementWw {
49
+ /** @internal */
50
+ static scopedElements = {
51
+ "sl-button": SlButton,
52
+ "sl-button-group": SlButtonGroup,
53
+ "dataset-input": DatasetInput,
54
+ "sl-option": SlOption,
55
+ "sl-select": SlSelect,
56
+ "sl-input": SlInput,
57
+ "sl-tab":SlTab,
58
+ "sl-tab-group":SlTabGroup,
59
+ "sl-tooltip":SlTooltip
60
+ }
61
+
62
+ protected localize = LOCALIZE;
63
+
64
+ static styles = css`
65
+ .table-fields {
66
+ width: 100%;
67
+ display: flex;
68
+ flex-direction: column;
69
+ flex-wrap: nowrap;
70
+ align-content: stretch;
71
+ justify-content: center;
72
+ align-items: stretch;
73
+ }
74
+ .table-fields sl-button {
75
+ flex-grow: 2;
76
+ }
77
+ :host {
78
+ display:block;
79
+ }
80
+ :host([contenteditable]) {
81
+ min-height: 100px;
82
+ }
83
+ :host([contenteditable][editmode="hidden"]) {
84
+ min-height: 30px;
85
+ background:lightgray;
86
+ }
87
+ :host div[part='options'] {
88
+ display:none;
89
+ }
90
+ :host([contenteditable]) div[part='options'] {
91
+ display:block;
92
+ }
93
+ :host .tabs {
94
+ margin-bottom: 5px;
95
+ }
96
+ :host div.settings {
97
+ display:flex;
98
+ flex-direction: row;
99
+ flex-wrap: wrap;
100
+ align-content: center;
101
+ align-items: center;
102
+ justify-content: flex-start;
103
+ }
104
+ :host div.settings * {
105
+ flex-grow:1;
106
+ }
107
+ :host div.props table {
108
+ width: 100%;
109
+ position:relative;
110
+ }
111
+ :host div.props table td * {
112
+ overflow:hidden;
113
+ text-overflow: ellipsis;
114
+ width: 390px;
115
+ max-height: 26px;
116
+ white-space: nowrap;
117
+ margin: 0px;
118
+ }
119
+ :host div.props table td:nth-child(2) {
120
+ text-align: right;
121
+ }
122
+ p {
123
+ margin:5px 0px;
124
+ }
125
+ img {
126
+ object-fit: contain;
127
+ }
128
+ `
129
+
130
+ /**
131
+ * The original decompressed data in a json string with format of RawSample[]
132
+ */
133
+ @property({ type: String, attribute: true, reflect: true })
134
+ accessor samplesSave: string;
135
+
136
+ private samples: Array<Record<string, string>>;
137
+
138
+ /**
139
+ * Stores the settings of the feature columns
140
+ */
141
+ @property({ type: Object, attribute: true, reflect: true })
142
+ accessor fieldProps: Record<string, FieldProp>;
143
+
144
+ private rawDataset: Dataset;
145
+ private tensorDataset: TransformedDataset;
146
+
147
+ /**
148
+ * The selected feature column
149
+ */
150
+ private activeHeader = "";
151
+
152
+ /**
153
+ * The number of samples used for the dataset
154
+ */
155
+ @property({ type: Number, attribute: true, reflect: true })
156
+ accessor limit: number = 1000;
157
+
158
+
159
+ @property({ type: Boolean })
160
+ protected accessor isInitiated = false;
161
+
162
+ /**
163
+ * What students will be able to edit in the explorable. Ranging from "edit" = everything, "feat" = only feature settings, "read" = read only, to "hidden" = invisible to the students.
164
+ */
165
+ @property({ type: String, attribute: true, reflect: true })
166
+ accessor editMode: "edit"|"feat"|"read"|"hidden" = "edit";
167
+
168
+ constructor() {
169
+ super();
170
+
171
+ getTF().then((tf) => {
172
+ tf.setBackend("cpu").then(() => {
173
+ tf.ready().then(() => {
174
+ this.isInitiated = true;
175
+ });
176
+ });
177
+ })
178
+ }
179
+
180
+ private renderSelectDataset() {
181
+ return html`
182
+ <dataset-input
183
+ @readDataset=${(e: CustomEvent) => this.handleDataset(e.detail.data, e.detail.errors, e.detail.meta)}
184
+ ></dataset-input>
185
+ `
186
+ }
187
+
188
+ private renderTableHeader() {
189
+ if (this.activeHeader == "")
190
+ this.activeHeader = this.rawDataset.fields[0];
191
+ let tabs = new Array<TemplateResult>();
192
+ for (let header of this.rawDataset.fields) {
193
+ tabs.push(html`<sl-button @click=${this.handleHeaderClick} variant=${header == this.activeHeader ? "primary": "default"}>${header}</sl-button>`);
194
+ }
195
+ return html`<p>${msg("Dataset Feature Colums:")}</p><sl-button-group>${tabs}</sl-button-group>`
196
+ }
197
+
198
+ private renderFieldValues(fieldName: string) {
199
+ if (fieldName == "")
200
+ return html`<div class="props"><p>${msg("Choose a column to format it")}</p></div>`
201
+
202
+ let that = this;
203
+ let rows;
204
+ if (this.fieldProps[fieldName].type == "Ignore") {
205
+ rows = this.samples.slice(0, 10).map(
206
+ function (v) {
207
+ return html`<tr><td><p>${JSON.stringify(v[fieldName])}</p></td></tr>`
208
+ }
209
+ )
210
+ return html`<div class="props"><table><thead><td>${fieldName}</td><td>${msg("Tensor")}</td></thead><tbody>${rows}</tbody></table></div>`
211
+ } else {
212
+ let Transformation = getFeatureTransformation(this.fieldProps[fieldName].type).constructor;
213
+ try {
214
+ let transformation = new Transformation(this.samples.map(v => v[fieldName]), this.fieldProps[fieldName].settings)
215
+ let rows = this.samples.slice(0, 10).map(
216
+ function (v) {
217
+ const value = v[fieldName];
218
+ const displayValue = transformation.renderOriginalValue(value);
219
+ return html`<tr><td><p>${displayValue}</p></td><td><p>${transformation.renderTransformedValue(value)}</p></td></tr>`
220
+ }
221
+ )
222
+ return html`<div class="settings">${transformation.getSettingElement(function (settings) { that.handleFeatureSettingsChange(settings) }, this.editMode)}</div><div class="props"><table><thead><td>${fieldName}</td><td>${msg("Tensor")}</td></thead><tbody>${rows}</tbody></table></div>`
223
+ } catch (e) {
224
+ console.log(e);
225
+ return html`<p style="background: red">${msg("This Transformation is not applicable for the values")}</p><div class="props"><table><thead><td>${fieldName}</td><td>${msg("Tensor")}</td></thead></table></div>`
226
+ }
227
+ }
228
+ }
229
+
230
+ private renderFieldTypeSelector(fieldName: string) {
231
+ if (fieldName == "")
232
+ return html``;
233
+
234
+ let options = new Array<TemplateResult>();
235
+ for (let option of getFeatureTransformations()) {
236
+ let optionName;
237
+ switch (option.name) {
238
+ case "Class Encoding":
239
+ optionName = msg("Class Encoding");
240
+ break;
241
+ case "Number Encoding":
242
+ optionName = msg("Number Encoding");
243
+ break;
244
+ case "Image Encoding":
245
+ optionName = msg("Image Encoding");
246
+ break;
247
+ case "Ignore":
248
+ optionName = msg("Ignore");
249
+ break;
250
+ default:
251
+ optionName = option.name;
252
+ }
253
+ options.push(html`<sl-option value=${option.name.replaceAll(" ", "_")}>${optionName}</sl-option>`)
254
+ }
255
+
256
+ let that = this;
257
+ function onchange(e: Event) {
258
+ let value = (e.target as SlSelect).value as string;
259
+ that.fieldProps[fieldName].type = value.replaceAll("_", " ") as FieldProp["type"];
260
+ //Deep copy for new reference to trigger refreshing the attribute and save ;-;
261
+ that.fieldProps = Object.assign({}, that.fieldProps);
262
+ that.updateTensorDataset();
263
+ that._triggerChanged();
264
+ }
265
+
266
+ return html`<sl-tooltip content=${msg("Select how you want the string to be interpreted.")}><sl-select label=${msg("Data Type")} @sl-change=${onchange} value=${this.fieldProps[fieldName].type.replaceAll(" ", "_")} ?disabled=${!(this.editMode == "edit")}>${options}</sl-select></sl-tooltip>`
267
+ }
268
+
269
+ protected firstUpdated(_changedProperties: PropertyValues): void {
270
+ this._triggerChanged();
271
+ }
272
+
273
+ render() {
274
+ if (!this.isInitiated)
275
+ return html`<p>${msg("Loading Tensorflow...")}</p>`
276
+ if (this.samplesSave && !this.samples)
277
+ this.samples = JSON.parse(this.samplesSave);
278
+ if (this.samples == null)
279
+ return this.renderSelectDataset();
280
+ else {
281
+ if (this.limit == null)
282
+ this.limit = Math.min(this.samples.length, 1000);
283
+
284
+ if (this.rawDataset == null) {
285
+ this.rawDataset = new Dataset(this.samples, 0.7, 0.2, this.limit);
286
+ this.updateTensorDataset();
287
+ }
288
+
289
+ this._triggerChanged();
290
+
291
+ function editModeChanged(e: Event) {
292
+ const select = e.target as HTMLSelectElement;
293
+ this.editMode = select.value;
294
+ }
295
+
296
+ return html`
297
+ <div part="options">
298
+ <sl-button id="reset" variant="warning" @click=${this.deleteDataset}>${msg("Delete dataset")}</sl-button>
299
+ <sl-select label=${msg("Edit Permissions")} value=${this.editMode} @sl-change=${editModeChanged}>
300
+ <sl-option value="hidden">${msg("hide widget")}</sl-option>
301
+ <sl-option value="read">${msg("read only")}</sl-option>
302
+ <sl-option value="feat">${msg("feature settings only")}</sl-option>
303
+ <sl-option value="edit">${msg("edit everything")}</sl-option>
304
+ </sl-select>
305
+ </div>
306
+ <p style=${this.editMode != "hidden" ? "display:none" : ""}>${msg("Feature Engineering Widget hidden from students.")}</p>
307
+ <div id="content" style=${this.editMode == "hidden" ? "display:none" : ""}>
308
+ <p>${msg("Dataset using")} <sl-input type="number" min="1" max=${String(this.samples.length)} value=${this.limit} @sl-change=${function(e:Event) {this.limit = parseInt((e.target as SlInput).value); if (isNaN(this.limit) || this.limit < 1) this.limit = 1000; this.handleSizeChange()}} style="display:inline-block" ?disabled=${this.editMode != "edit"}></sl-input> ${msg(str`of ${this.samples.length} Samples`)}</p>
309
+ ${this.renderTableHeader()}
310
+ ${this.renderFieldTypeSelector(this.activeHeader)}
311
+ ${this.renderFieldValues(this.activeHeader)}
312
+ </div>
313
+ `;
314
+ }
315
+ }
316
+
317
+ /**
318
+ * Called when a new dataset is provided
319
+ * @param data The dataset
320
+ * @param errors Possible errors from loading the dataset
321
+ * @param meta Should at least contain a list of feature column names in meta.fields
322
+ */
323
+ private handleDataset(data: Array<Record<string, string>>, errors: Array<any>, meta: any) {
324
+ console.log("handle")
325
+ this.samplesSave = JSON.stringify(data);
326
+ this.samples = data;
327
+ this.rawDataset = new Dataset(this.samples);
328
+ this.fieldProps = {}
329
+ for (let field of meta.fields) {
330
+ this.fieldProps[field] = { type: "Ignore", settings: null };
331
+ }
332
+ this.updateTensorDataset();
333
+ this._triggerChanged();
334
+ }
335
+
336
+
337
+ /**
338
+ * Returns the current dataset in both raw and tensor form
339
+ */
340
+ getDataset() {
341
+ return { rawDataset: this.rawDataset, tensorDataset: this.tensorDataset }
342
+ }
343
+
344
+ /**
345
+ * Called everytime settings change to generate the new tensor dataset.
346
+ */
347
+ private updateTensorDataset() {
348
+ let transformations: Record<string, Constructor<FeatureTransformation>> = {}
349
+ let settings: Record<string, Constructor<FeatureTransformation>> = {};
350
+ for (let field in this.fieldProps) {
351
+ if (this.fieldProps[field].type != "Ignore") {
352
+ transformations[field] = getFeatureTransformation(this.fieldProps[field].type).constructor;
353
+ settings[field] = this.fieldProps[field].settings;
354
+ }
355
+ }
356
+ if (this.tensorDataset)
357
+ this.tensorDataset.clearCache();
358
+ this.tensorDataset = new TransformedDataset(this.rawDataset, transformations, settings);
359
+ }
360
+
361
+ /**
362
+ * Switches to the clicked tab
363
+ * @param e The event from the button of the tab
364
+ */
365
+ private handleHeaderClick(e: Event) {
366
+ let field = (e.currentTarget as HTMLElement).innerText
367
+ this.activeHeader = field;
368
+ this.requestUpdate();
369
+ }
370
+
371
+ private deleteDataset() {
372
+ this.samplesSave = null;
373
+ this.samples = null;
374
+ this.rawDataset = null;
375
+ this.tensorDataset = null;
376
+ this.requestUpdate();
377
+ this._triggerChanged();
378
+ }
379
+
380
+ /**
381
+ * Called when the limit is chanded
382
+ */
383
+ private handleSizeChange() {
384
+ this.rawDataset = new Dataset(this.samples, 0.7, 0.2, this.limit);
385
+ this.updateTensorDataset();
386
+ this._triggerChanged();
387
+ }
388
+
389
+ /**
390
+ * Called when the user changes the feature settings
391
+ * @param settings New settings
392
+ */
393
+ private handleFeatureSettingsChange(settings: Record<string, any>) {
394
+ this.fieldProps[this.activeHeader] = { ...this.fieldProps[this.activeHeader], settings };
395
+ this.fieldProps = Object.assign({}, this.fieldProps);
396
+ this.tensorDataset.clearCache();
397
+ this.updateTensorDataset();
398
+ this.requestUpdate();
399
+ this._triggerChanged();
400
+ }
401
+
402
+ /**
403
+ * Tells listening widgets when the dataset changed
404
+ */
405
+ private _triggerChanged() {
406
+ this.dispatchEvent(new CustomEvent('change', {
407
+ detail: {
408
+ dataset: this.rawDataset,
409
+ tensorDataset: this.tensorDataset
410
+ }
411
+ }));
412
+ }
413
+
414
+ /**
415
+ * Checks whether the dataset is ready to be used
416
+ * @returns True if both raw and tensor dataset are available
417
+ */
418
+ public isReady(): boolean {
419
+ return this.rawDataset != null && this.tensorDataset != null;
420
+ }
421
+ }