File size: 9,539 Bytes
dab4b62
c17048f
9b05cbd
c17048f
a6c2420
 
 
 
 
 
 
 
 
 
9b05cbd
 
 
 
de4a32f
 
2c7da06
 
9b05cbd
 
 
 
477f2fe
9b05cbd
 
 
 
de4a32f
9b05cbd
2c7da06
de4a32f
ee30be1
02baa9b
 
9b05cbd
 
 
 
 
477f2fe
9b05cbd
 
 
 
de4a32f
9b05cbd
746b70a
de4a32f
ee30be1
02baa9b
 
9b05cbd
7e363e6
9b05cbd
 
b3827b7
5ea577f
 
c972103
b3827b7
 
2c7da06
 
b3827b7
 
 
 
 
 
2c7da06
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
dab4b62
 
5ea577f
b3827b7
dab4b62
9b05cbd
af85f3e
9b05cbd
 
 
af85f3e
 
 
 
 
 
 
5ea577f
af85f3e
 
 
5ea577f
af85f3e
 
 
 
 
 
de4a32f
1bfbf9b
af85f3e
 
 
 
 
 
 
 
 
 
 
5ea577f
af85f3e
 
5ea577f
af85f3e
 
1bfbf9b
af85f3e
 
 
1bfbf9b
 
af85f3e
 
 
1bfbf9b
 
ee30be1
 
af85f3e
 
5ea577f
af85f3e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9b05cbd
af85f3e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e07a0b4
af85f3e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
77ed52e
9b05cbd
7e363e6
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 } from "react";
import ReactMarkdown from "react-markdown";

import usageMarkdown from "./usage.md?raw";

import {
  Button,
  Card,
  Dropdown,
  FileUploader,
  InputField,
  InputSection,
  Tabs,
} from "@elvis/ui";

import { OKABE_ITO_COLORS } from "./colors";
import { CLASSIFIERS } from "./classifiers";

import type { CsvSettings, ModelSettings } from "./types";

import { DATASETS } from "./datasets";


interface SidebarProps {
  pointLabel: string;
  setPointLabel: (label: string) => void;
  setCanAddPoints: (canAddPoints: boolean) => void;
  undoDataPoint: () => void;
  clearDataPoints: () => void;
  modelSettings: ModelSettings;
  setModelSettings: (settings: ModelSettings) => void;
  csvSettings: CsvSettings;
  onGetDecisionBoundary: () => void;
  onCsvUpload: (file: File, settings?: CsvSettings) => void;
  onCsvSettingsChange: (settings: CsvSettings) => void;
  csvError: string | null;
  onCsvExport: () => void;
  onSvgExport: () => void;
}

export default function Sidebar({ 
  pointLabel, 
  setPointLabel, 
  setCanAddPoints,
  undoDataPoint,
  clearDataPoints,
  modelSettings, 
  setModelSettings,
  csvSettings,
  onGetDecisionBoundary,
  onCsvUpload,
  onCsvSettingsChange,
  csvError,
  onCsvExport,
  onSvgExport,
}: SidebarProps) {
  const tabs = ["Data", "Model", "Export", "Usage"] as const;
  const [activeTab, setActiveTab] = useState<(typeof tabs)[number]>("Data");

  const presetOptions = DATASETS.map((dataset) => dataset.name) as string[];
  const customDatasetOptions = ["Manual...", "Upload..."] as string[];
  const [datasetOption, setDatasetOption] = useState<string>("Manual...");

  function isPresetOption(option: string) {
    return presetOptions.includes(option);
  }

  function onDatasetOptionChange(option: string) {
    setDatasetOption(option);
    if (isPresetOption(option)) {
      loadPresetDataset(option);
    }
  }

  async function loadPresetDataset(datasetName: string) {
    const dataset = DATASETS.find((d) => d.name === datasetName);
    if (!dataset) {
      return;
    }

    const response = await fetch(dataset.url);
    const blob = await response.blob();
    const file = new File([blob], `${dataset.name}.csv`, { type: "text/csv" });

    const presetCsvSettings = {
      ...csvSettings,
      inputColumns: dataset.inputColumns,
      outputColumn: dataset.outputColumn,
    };

    onCsvUpload(file, presetCsvSettings);
  }

  useEffect(() => {
    clearDataPoints();
    setCanAddPoints(datasetOption === "Manual...");
  }, [datasetOption]);

  return (
    <Card className="bg-white flex flex-col h-full min-w-0 min-h-0 overflow-auto border border-gray-200 rounded p-6 gap-4">
      <Tabs tabs={tabs} activeTab={activeTab} onChange={setActiveTab} />

      { /* Tab content */}
      <div className="flex flex-col gap-8 min-h-0 overflow-auto">
        { activeTab === "Data" && (
          <>
            <InputSection label="Dataset">
              <Dropdown
                label="Input"
                options={customDatasetOptions}
                groupedOptions={[{ label: "Built-in...", options: presetOptions }]}
                activeOption={datasetOption}
                onChange={onDatasetOptionChange}
              />
              { datasetOption === "Manual..." && (
                <>
                  <Dropdown
                    label="Point Label"
                    options={OKABE_ITO_COLORS.map(color => color.name)}
                    activeOption={pointLabel}
                    onChange={(option: string) => setPointLabel(option)}
                  />
                  <div className="grid grid-cols-2 gap-2">
                    <Button
                      label="Undo"
                      onClick={undoDataPoint}
                    />
                    <Button
                      label="Clear"
                      onClick={clearDataPoints}
                    />
                  </div>
                </>
              )}
              { datasetOption === "Upload..." && (
                <FileUploader label="Upload CSV" onFileUpload={onCsvUpload} />
              )}
              { (datasetOption === "Upload..." || isPresetOption(datasetOption)) && (
                <>
                  <div className="grid grid-cols-[minmax(0,2fr)_minmax(0,1fr)] gap-2">
                    <InputField
                      label="Input Columns"
                      value={csvSettings.inputColumns}
                      onChange={(value: string) => {onCsvSettingsChange({ ...csvSettings, inputColumns: value })}}
                    />
                    <InputField
                      label="Output Column"
                      value={csvSettings.outputColumn}
                      onChange={(value: string) => {onCsvSettingsChange({ ...csvSettings, outputColumn: value })}}
                    />
                  </div>
                </>
              )}
            </InputSection>

            { (datasetOption === "Upload..." || isPresetOption(datasetOption)) && (
              <>
                <InputSection label="Preprocessing">
                  <Dropdown
                    label="Normalizer"
                    options={["None", "MinMax", "Standard"]}
                    activeOption={csvSettings.normalizerType}
                    onChange={(option: string) => {onCsvSettingsChange({ ...csvSettings, normalizerType: option })}}
                  />
                  <InputField
                    label="Normal noise standard deviation"
                    value={csvSettings.normalNoiseStd}
                    onChange={(value: string) => {onCsvSettingsChange({ ...csvSettings, normalNoiseStd: value })}}
                  />
                </InputSection>

                <InputSection label="Plotting">
                  <Dropdown
                    label="Projection"
                    options={["Coordinates", "PCA"]}
                    activeOption={csvSettings.projectionType}
                    onChange={(option: string) => {onCsvSettingsChange({ ...csvSettings, projectionType: option })}}
                  />
                  {csvSettings.projectionType === "Coordinates" && (
                    <div className="grid grid-cols-2 gap-2">
                      <InputField
                        label="X1 Column"
                        value={csvSettings.x1Column || ""}
                        onChange={(value: string) => {onCsvSettingsChange({ ...csvSettings, x1Column: value })}}
                      />
                      <InputField
                        label="X2 Column"
                        value={csvSettings.x2Column || ""}
                        onChange={(value: string) => {onCsvSettingsChange({ ...csvSettings, x2Column: value })}}
                      />
                    </div>
                  )}
                </InputSection>

                {csvError && (
                  <InputSection label="Error">
                    <p className="text-red-600 font-semibold">CSV Error: {csvError}</p>
                  </InputSection>
                )}
              </>
            )}
          </>
        )}

        { activeTab === "Model" && (
          <>
            <InputSection label="Classifier Options">
              <Dropdown
                label="Type"
                options={CLASSIFIERS}
                activeOption={modelSettings.type}
                onChange={(option: string) => {
                  setModelSettings({ ...modelSettings, type: option });
                }}
              />
              <InputField
                label="Arguments"
                value={modelSettings.arguments}
                onChange={(value: string) => {
                  setModelSettings({ ...modelSettings, arguments: value });
                }}
              />
            </InputSection>
            <Button
              label="Get Decision Boundary"
              onClick={onGetDecisionBoundary}
            />
          </>
        )}
        { activeTab === "Export" && (
          <div className="flex flex-col gap-4">
            <Button
              label="Dataset as CSV"
              onClick={onCsvExport}
            />
            <Button
              label="Plot as SVG"
              onClick={onSvgExport}
            />
            {/* <Button
              label="Python code"
            /> */}
          </div>
        )}
        { activeTab === "Usage" && (
            <div className="prose leading-loose overflow-auto">
              <ReactMarkdown
                components={{
                  h1: ({ children }) => <h1 className="text-2xl font-bold mt-12 mb-2 first:mt-4">{children}</h1>,
                  h2: ({ children }) => <h2 className="text-xl font-semibold mt-12 mb-2 first:mt-4">{children}</h2>,
                  h3: ({ children }) => <h3 className="text-lg font-semibold mt-12 mb-2 first:mt-4">{children}</h3>,
                  p: ({ children }) => <p className="leading-6 mb-3 last:mb-0">{children}</p>,
                  ul: ({ children }) => <ul className="list-disc pl-5 mb-3">{children}</ul>,
                  ol: ({ children }) => <ol className="list-decimal pl-5 mb-3">{children}</ol>,
                  li: ({ children }) => <li className="mb-1">{children}</li>,
                  code: ({ children }) => <code className="bg-gray-200 px-1 py-0.5 rounded">{children}</code>
                }}
              >
                {usageMarkdown}
              </ReactMarkdown>
            </div>
        )}
      </div>
    </Card>
  )
}