File size: 7,125 Bytes
02baa9b
41e6303
 
18fd890
41e6303
 
 
 
 
 
 
 
 
 
 
 
 
 
18fd890
 
 
 
41e6303
 
 
 
 
 
 
 
 
 
18fd890
 
 
 
 
803a9d0
18fd890
 
 
 
 
 
 
803a9d0
 
 
 
 
 
 
41e6303
02baa9b
41e6303
 
 
18fd890
 
 
 
 
 
 
 
 
41e6303
 
 
 
 
18fd890
02baa9b
 
41e6303
473e865
41e6303
 
 
473e865
 
41e6303
 
 
 
 
473e865
18fd890
41e6303
 
 
473e865
41e6303
 
 
473e865
 
 
41e6303
 
 
473e865
18fd890
41e6303
 
 
473e865
41e6303
 
473e865
 
 
 
41e6303
 
 
473e865
18fd890
41e6303
 
18fd890
41e6303
 
 
 
18fd890
 
 
 
 
41e6303
 
 
 
 
18fd890
 
 
 
 
 
 
 
2c7da06
 
 
 
 
 
 
41e6303
 
 
 
 
 
2c7da06
41e6303
 
2c7da06
41e6303
 
 
 
803a9d0
 
 
41e6303
 
 
 
18fd890
41e6303
 
 
 
 
 
 
 
 
 
 
 
803a9d0
 
 
41e6303
 
 
 
18fd890
41e6303
 
02baa9b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
41e6303
 
 
 
 
 
 
 
 
18fd890
41e6303
 
18fd890
41e6303
 
 
 
 
18fd890
 
 
 
 
 
 
803a9d0
41e6303
18fd890
41e6303
 
 
 
 
 
 
 
 
02baa9b
 
 
41e6303
2c7da06
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
import { useEffect, useState, useRef } from "react";
import * as Comlink from "comlink";

import type { ModelSettings, DataPoints, DecisionBoundarySettings, DecisionBoundaryResult, CsvSettings } from "./types.ts";
import { OKABE_ITO_COLORS, LabelColors } from "./colors.ts";
import usePyodideBackend from "./usePyodideBackend.ts";


const DEFAULT_CSV_SETTINGS: CsvSettings = {
  inputColumns: "1,2",
  outputColumn: "3",
  normalizerType: "None",
  normalNoiseStd: "0.0",
  projectionType: "Coordinates",
  x1Column: "1",
  x2Column: "2",
};

const DEFAULT_RESOLUTION = 500;
const DEFAULT_X_RANGE: [number, number] = [-10, 10];
const DEFAULT_Y_RANGE: [number, number] = [-10, 10];

export default function useAppLogic() {
  const { getBackend, backendReady } = usePyodideBackend();

  const [dataPoints, setDataPoints] = useState<DataPoints>({
    xPoints: [],
    yPoints: [],
    labels: [],
  });
  const labelColors = new LabelColors(dataPoints.labels);

  const [canAddPoints, setCanAddPoints] = useState<boolean>(true);
  const [pointLabel, setPointLabel] = useState<string>(OKABE_ITO_COLORS[0].name);
  const [resolution, _] = useState<number>(DEFAULT_RESOLUTION);  // todo user defined
  const xRangeRef = useRef<[number, number]>(DEFAULT_X_RANGE);
  const yRangeRef = useRef<[number, number]>(DEFAULT_Y_RANGE);
  const [rangeVersion, setRangeVersion] = useState<number>(0);

  function handleRangeChange(xRange: [number, number], yRange: [number, number]) {
    xRangeRef.current = xRange;
    yRangeRef.current = yRange;
    updateDecisionBoundary();
  }

  function handleRangeChangeWithRender(xRange: [number, number], yRange: [number, number]) {
    xRangeRef.current = xRange;
    yRangeRef.current = yRange;
    setRangeVersion((prev) => prev + 1);
    console.log(xRangeRef.current, yRangeRef.current);
  }

  const [modelSettings, setModelSettings] = useState<ModelSettings>({
    type: "LogisticRegression",
    arguments: "",
  });

  async function handleModelSettingsChange(settings: ModelSettings) {
    setModelSettings(settings);
    if (!backendReady) {
      return;
    }
    await getBackend().setModelConfig(settings);
    await updateDecisionBoundary();
  }

  const [csvSettings, setCsvSettings] = useState<CsvSettings>(DEFAULT_CSV_SETTINGS);
  const [csvError, setCsvError] = useState<string | null>(null);

  const [decisionBoundaryResult, setDecisionBoundaryResult] = useState<DecisionBoundaryResult | null>(null);

  // this is needed for svg export
  const plotHandle = useRef<Plotly.PlotlyHTMLElement | null>(null);

  async function handleAddDataPoint(x: number, y: number, label: string) {
    const next = {
      xPoints: [...dataPoints.xPoints, x],
      yPoints: [...dataPoints.yPoints, y],
      labels: [...dataPoints.labels, label],
    }
    setDataPoints(next);

    if (!backendReady) {
      return;
    }

    await getBackend().setDataset2d(next);
    await updateDecisionBoundary();
  }

  async function handleUndoDataPoint() {
    const next = {
      xPoints: dataPoints.xPoints.slice(0, -1),
      yPoints: dataPoints.yPoints.slice(0, -1),
      labels: dataPoints.labels.slice(0, -1),
    }
    setDataPoints(next);

    if (!backendReady) {
      return;
    }
    await getBackend().setDataset2d(next);
    await updateDecisionBoundary();
  }

  async function handleClearDataPoints() {
    const next = {
      xPoints: [],
      yPoints: [],
      labels: [],
    }
    setDataPoints(next);

    if (!backendReady) {
      return;
    }
    await getBackend().setDataset2d(next);
    await updateDecisionBoundary();
  }

  async function updateDecisionBoundary() {
    if (!backendReady) {
      return;
    }
    const settings: DecisionBoundarySettings = {
      xmin: xRangeRef.current[0],
      xmax: xRangeRef.current[1],
      ymin: yRangeRef.current[0],
      ymax: yRangeRef.current[1],
      resolution: resolution,
    };
    const result = await getBackend().getDecisionBoundary(settings);
    setDecisionBoundaryResult(result);
  }

  async function handleGetDecisionBoundary() {
    if (!backendReady) {
      return;
    }
    await getBackend().buildModel();
    await updateDecisionBoundary();
  }

  async function handleCsvUpload(file: File, settings?: CsvSettings) {
    const activeCsvSettings = settings ?? csvSettings;

    if (settings) {
      setCsvSettings(settings);
    }

    if (!backendReady) {
      alert("Python backend is not ready yet.");
      return;
    }

    const buffer = await file.arrayBuffer(); 
    console.log(buffer);
    const result = await getBackend().setDatasetCsv(
      Comlink.transfer(buffer, [buffer]),
      activeCsvSettings,
    );

    if (result.status === "OK") {
      setDataPoints(result.dataPoints);
      if (result.xRange && result.yRange) {
        handleRangeChangeWithRender(result.xRange, result.yRange);
      }
      setCsvError(null);
    } else if (result.status === "CSV_ERROR") {
      setCsvError(result.message);
    }
    await updateDecisionBoundary();
  }

  async function handleCsvSettingsChange(settings: CsvSettings) {
    if (!backendReady) {
      return;
    }

    setCsvSettings(settings);

    const result = await getBackend().setCsvSettings(settings);
    if (result.status === "OK") {
      setDataPoints(result.dataPoints);
      if (result.xRange && result.yRange) {
        handleRangeChangeWithRender(result.xRange, result.yRange);
      }
      setCsvError(null);
    } else if (result.status === "CSV_ERROR") {
      setCsvError(result.message);
    }
    await updateDecisionBoundary();
  }

  function downloadCsv(filename: string, csvText: string) {
    const blob = new Blob([csvText], { type: "text/csv;charset=utf-8" });
    const url = URL.createObjectURL(blob);

    const a = document.createElement("a");
    a.href = url;
    a.download = filename;
    document.body.appendChild(a);
    a.click();
    a.remove();

    URL.revokeObjectURL(url);
  }

  async function getCsvDataset() {
    if (!backendReady) {
      alert("Python backend is not ready yet.");
      return;
    }
    const csvData = await getBackend().getDatasetCsv();
    downloadCsv("dataset.csv", csvData);
  }

  function getPlotSvg() {
    if (!plotHandle.current) {
      return;
    }
    (plotHandle.current as any)?.downloadSvg()
  }

  console.log("rendering app");
  useEffect(() => {
    if (!backendReady) {
      return;
    }

    (async () => {
      await getBackend().setDataset2d(dataPoints);
      await getBackend().setModelConfig(modelSettings);
      await updateDecisionBoundary();
    })();

  }, [backendReady, getBackend]);

  return {
    backendReady,
    dataPoints,
    labelColors,
    canAddPoints,
    setCanAddPoints,
    pointLabel,
    setPointLabel,
    xRangeRef,
    yRangeRef,
    handleRangeChange,
    rangeVersion,
    modelSettings,
    handleModelSettingsChange,
    csvSettings,
    handleCsvSettingsChange,
    csvError,
    decisionBoundaryResult,
    handleAddDataPoint,
    handleUndoDataPoint,
    handleClearDataPoints,
    handleGetDecisionBoundary,
    handleCsvUpload,
    getCsvDataset,
    plotHandle,
    getPlotSvg,
  }
}