File size: 7,148 Bytes
2e0eaa2
f586f77
 
2e0eaa2
f586f77
 
 
2e0eaa2
 
 
 
 
f586f77
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2e0eaa2
 
f586f77
2e0eaa2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f586f77
 
2e0eaa2
 
f586f77
 
 
 
 
6c17bbb
f586f77
 
 
 
 
 
 
 
 
2e0eaa2
 
f586f77
 
 
 
2e0eaa2
 
 
f586f77
 
 
 
2e0eaa2
 
 
f586f77
 
 
 
 
 
 
2e0eaa2
f586f77
 
 
2e0eaa2
 
f586f77
 
 
 
 
2e0eaa2
 
f586f77
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2e0eaa2
 
f586f77
 
 
 
 
 
 
 
 
 
 
2e0eaa2
 
 
 
 
 
 
 
 
 
 
f586f77
 
 
2e0eaa2
 
 
f586f77
 
 
 
2e0eaa2
 
f586f77
2e0eaa2
 
 
f586f77
2e0eaa2
 
 
f586f77
2e0eaa2
 
 
f586f77
 
2e0eaa2
f586f77
 
 
2e0eaa2
 
f586f77
2e0eaa2
 
 
 
 
 
 
f586f77
 
 
 
2e0eaa2
f586f77
2e0eaa2
 
f586f77
2e0eaa2
 
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
import { useEffect, useState, useRef } from "react";
import { usePythonStatus, type Result } from "@elvis/pyodide";
import type { Dataset, ModelResults } from "@elvis/ui";

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


const DEFAULT_RESOLUTION = 500;
const DEFAULT_X_RANGE: [number, number] = [-10, 10];
const DEFAULT_Y_RANGE: [number, number] = [-10, 10];
// With auto-fit on, wait this long after a model setting stops changing
const AUTO_FIT_DELAY_MS = 500;

// The axis range covering values, with 10% padding on each side
function paddedRange(values: number[]): [number, number] {
  const min = Math.min(...values);
  const max = Math.max(...values);
  const pad = max > min ? 0.1 * (max - min) : 1;
  return [min - pad, max + pad];
}

// Returns the call's data, or null on error. User errors go to showUserError
// if given; everything else is logged.
function dataOf<T>(result: Result<T>, showUserError?: (message: string) => void): T | null {
  if (result.ok) {
    return result.data;
  }
  if (result.error.kind === "user" && showUserError) {
    showUserError(result.error.message);
  } else {
    console.error(`Python ${result.error.kind} error: ${result.error.message}`);
  }
  return null;
}

export default function useAppLogic() {
  const pythonStatus = usePythonStatus(python);

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

  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);

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

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

  const [modelError, setModelError] = useState<string | null>(null);
  // From the last fit; cleared when the data or model changes without a refit
  const [modelResults, setModelResults] = useState<ModelResults | null>(null);

  // Refit whenever the data or model changes, instead of waiting for the button
  const [autoFit, setAutoFit] = useState(true);
  const autoFitTimer = useRef<ReturnType<typeof setTimeout> | null>(null);
  // Counts dataset changes, so only the newest one triggers a refit
  const datasetVersion = useRef(0);

  // After the data or model changes: refit if auto-fit is on and there are
  // two classes to separate, otherwise just redraw (an unfitted model draws nothing)
  async function refresh(labels: string[]) {
    if (autoFit && new Set(labels).size >= 2) {
      await handleGetDecisionBoundary();
      return;
    }
    if (autoFit) {
      setModelError(null);
    }
    setModelResults(null);
    await updateDecisionBoundary();
  }

  function handleAutoFitChange(on: boolean) {
    setAutoFit(on);
    if (on && new Set(dataPoints.labels).size >= 2) {
      handleGetDecisionBoundary();
    }
  }

  async function handleModelSettingsChange(settings: ModelSettings) {
    // A setting changes with each keystroke; a new model doesn't
    const settingChanged = settings.type === modelSettings.type;
    setModelSettings(settings);
    // The old results no longer describe the model; with auto-fit they stay until the refit
    if (!autoFit) {
      setModelResults(null);
    }
    dataOf(await python.call("handle_set_model_config", settings));
    if (!autoFit) {
      await updateDecisionBoundary();
      return;
    }
    // Wait for a pause so half-typed values don't show errors
    if (autoFitTimer.current) {
      clearTimeout(autoFitTimer.current);
    }
    autoFitTimer.current = setTimeout(() => refresh(dataPoints.labels), settingChanged ? AUTO_FIT_DELAY_MS : 0);
  }

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

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

  // The dataset from the picker: plot its projected points and send the full
  // inputs to Python. fitView zooms to the points; an empty plot resets the view.
  async function handleSetDataset(dataset: Dataset | null, fitView: boolean) {
    if (!autoFit) {
      setModelResults(null);
    }
    const labels = dataset ? dataset.targets.map(String) : [];
    const next = { xPoints: dataset?.points.x ?? [], yPoints: dataset?.points.y ?? [], labels };
    if (labels.length === 0) {
      xRangeRef.current = DEFAULT_X_RANGE;
      yRangeRef.current = DEFAULT_Y_RANGE;
    } else if (fitView) {
      xRangeRef.current = paddedRange(next.xPoints);
      yRangeRef.current = paddedRange(next.yPoints);
    }
    setDataPoints(next);
    // A new table's labels don't match the old boundary, so don't show it meanwhile
    if (fitView) {
      setDecisionBoundaryResult(null);
    }
    const version = ++datasetVersion.current;
    dataOf(await python.call("handle_set_dataset", dataset
      ? { inputs: dataset.inputs, outputs: labels, input_names: dataset.inputNames, projection: dataset.projection }
      : { inputs: [], outputs: [] }));
    // A newer dataset is already on its way; let it do the refit
    if (version === datasetVersion.current) {
      await refresh(labels);
    }
  }

  async function updateDecisionBoundary() {
    const settings: DecisionBoundarySettings = {
      xmin: xRangeRef.current[0],
      xmax: xRangeRef.current[1],
      ymin: yRangeRef.current[0],
      ymax: yRangeRef.current[1],
      resolution: resolution,
    };
    const result = dataOf(await python.call("handle_get_decision_boundary", settings), setModelError);
    if (result) {
      setDecisionBoundaryResult(result);
    }
  }

  async function handleGetDecisionBoundary() {
    const built = await python.call("handle_build_model");
    setModelResults(dataOf(built, setModelError));
    if (!built.ok) {
      return;
    }
    setModelError(null);
    await updateDecisionBoundary();
  }

  function getPlotImage(format: "svg" | "png") {
    if (!plotHandle.current) {
      return;
    }
    (plotHandle.current as any)?.downloadImage(format)
  }

  console.log("rendering app");
  // Python runs calls in the order they are made, so send these together
  // rather than awaiting each: user actions made while Python loads queue after them
  useEffect(() => {
    python.call("handle_set_model_config", modelSettings).then((result) => dataOf(result));
    updateDecisionBoundary();
  }, []);

  return {
    pythonStatus,
    dataPoints,
    labelColors,
    xRangeRef,
    yRangeRef,
    handleRangeChange,
    modelSettings,
    handleModelSettingsChange,
    modelError,
    modelResults,
    autoFit,
    handleAutoFitChange,
    decisionBoundaryResult,
    handleSetDataset,
    handleGetDecisionBoundary,
    plotHandle,
    getPlotImage,
  }
}