aboutsummaryrefslogtreecommitdiff
path: root/templates/patent.html
blob: 8bf862ee0f6559552c27e6642a608d1d7bba0e8a (plain)
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
  <title>Excel Data Plotter with Pyodide</title>
  <script src="https://cdn.jsdelivr.net/pyodide/v0.26.2/full/pyodide.js"></script>
  <link href="https://bootswatch.com/5/morph/bootstrap.min.css" rel="stylesheet"/>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap/5.3.0/js/bootstrap.bundle.min.js"></script>
</head>
<body>
  <div class="container mt-5">
    <h1 class="mb-4">Plotter for Patent</h1>
    <div class="row">
      <div class="col-md-6">
        <div class="mb-3">
          <textarea
            id="dataInput"
            class="form-control"
            rows="10"
            placeholder="Paste Excel data (tab-delimited) here">
          </textarea>
        </div>
        <button onclick="loadData()" class="btn btn-primary mb-3">Load Data</button>
      </div>

      <div class="col-md-6">
        <div class="mb-3">
          <label for="xSelect" class="form-label">Select X-axis:</label>
          <select id="xSelect" class="form-select"></select>
        </div>
        <div class="mb-3">
          <label for="ySelect" class="form-label">Select Y-axis:</label>
          <select id="ySelect" class="form-select"></select>
        </div>
        <div class="mb-3">
          <label for="errorSelect" class="form-label">Select Error column:</label>
          <select id="errorSelect" class="form-select"></select>
        </div>
      </div>
    </div>

    <!-- Plot configuration -->
    <div class="row mt-3">
      <div class="col-md-3 mb-3">
        <label for="plotTitle" class="form-label">Plot Title:</label>
        <input type="text" id="plotTitle" class="form-control" placeholder="Enter plot title"/>
      </div>
      <div class="col-md-3 mb-3">
        <label for="xAxisLabel" class="form-label">X-axis Label:</label>
        <input type="text" id="xAxisLabel" class="form-control" placeholder="Enter X-axis label"/>
      </div>
      <div class="col-md-3 mb-3">
        <label for="yAxisLabel" class="form-label">Y-axis Label:</label>
        <input type="text" id="yAxisLabel" class="form-control" placeholder="Enter Y-axis label"/>
      </div>
      <div class="col-md-3 mb-3">
        <label for="dataSeriesLabel" class="form-label">Data Series Label:</label>
        <input type="text" id="dataSeriesLabel" class="form-control" placeholder="Enter data series label"/>
      </div>
    </div>

    <!-- Trendline options -->
    <div class="row mt-3">
      <div class="col-md-3">
        <div class="form-check mb-3">
          <input class="form-check-input" type="checkbox" value="" id="addTrendline" />
          <label class="form-check-label" for="addTrendline">Add Trendline</label>
        </div>
      </div>
      <div class="col-md-3">
        <div class="mb-3">
          <label for="trendlineSelect" class="form-label">Trendline Type:</label>
          <select id="trendlineSelect" class="form-select">
            <option value="none" selected>None</option>
            <option value="linear">Linear</option>
            <option value="exponential">Exponential</option>
            <option value="logarithmic">Logarithmic</option>
            <option value="polynomial">Polynomial</option>
            <option value="power">Power</option>
          </select>
        </div>
      </div>
      <div class="col-md-3">
        <div class="mb-3" id="polynomialOrderContainer" style="display: none;">
          <label for="polynomialOrder" class="form-label">Polynomial Order:</label>
          <input type="number" id="polynomialOrder" class="form-control" value="2" min="1"/>
        </div>
      </div>
    </div>

    <!-- Action buttons -->
    <div class="row mt-3">
      <div class="col-md-6">
        <button onclick="plotScatterPlot()" class="btn btn-success me-2">Plot Scatter Plot</button>
        <button onclick="plotBarPlot()" class="btn btn-info">Plot Bar Plot</button>
      </div>
    </div>

    <!-- Plot output -->
    <div id="plotCanvas" class="mt-4"></div>
  </div>

  <script>
    // Initialize Pyodide on page load
    window.onload = async () => {
      await initPyodide();
      // Event listener to show/hide polynomial order
      document.getElementById('trendlineSelect').addEventListener('change', function () {
        const trendlineType = this.value;
        const orderContainer = document.getElementById('polynomialOrderContainer');
        if (trendlineType === 'polynomial') {
          orderContainer.style.display = 'block';
        } else {
          orderContainer.style.display = 'none';
        }
      });
    };

    // Load Pyodide and initialize Python environment
    async function initPyodide() {
      const pyodide = await loadPyodide();
      await pyodide.loadPackage('micropip');
      await pyodide.runPythonAsync(`
        import micropip
        # Install the needed packages
        await micropip.install('matplotlib')
        await micropip.install('pandas')
        await micropip.install('numpy')
      `);
      window.pyodide = pyodide;
    }

    // Function to populate dropdowns based on header row
    function populateDropdowns(headers) {
      const xSelect = document.getElementById('xSelect');
      const ySelect = document.getElementById('ySelect');
      const errorSelect = document.getElementById('errorSelect');

      // Clear previous options
      xSelect.innerHTML = '';
      ySelect.innerHTML = '';
      errorSelect.innerHTML = '';

      headers.forEach(header => {
        const option = document.createElement('option');
        option.value = header;
        option.textContent = header;
        xSelect.appendChild(option.cloneNode(true));
        ySelect.appendChild(option.cloneNode(true));
        errorSelect.appendChild(option.cloneNode(true));
      });
    }

    // Function to load data from textarea, parse headers
    function loadData() {
      const data = document.getElementById('dataInput').value;
      const rows = data.split('\n');
      const headers = rows[0].split('\t');
      populateDropdowns(headers);
      return data;
    }

    // Helper function that returns a Python code snippet for fitting a trendline
    // based on user’s selection in JavaScript.
    function getTrendlineCode(xColumn, yColumn, trendlineType, polynomialOrder) {
      // Return Python code as a string that performs the chosen regression
      // and plots a trendline on the active subplot (`ax`).
      //
      // We'll assume x and y are numeric. You may want to handle exceptions
      // for non-positive X when using log/power, or handle missing data, etc.
      // This snippet is appended directly inside the Python code block.

      return `
import numpy as np

# If user wants a trendline, perform the chosen regression:
if "${trendlineType}" != "none":
    # Extract numeric arrays for fitting
    x_vals = df["${xColumn}"].to_numpy(dtype=float)
    y_vals = df["${yColumn}"].to_numpy(dtype=float)

    # Sort by x for a cleaner line plot
    sort_idx = np.argsort(x_vals)
    x_sorted = x_vals[sort_idx]
    y_sorted = y_vals[sort_idx]

    if "${trendlineType}" == "linear":
        # y = a*x + b
        p = np.polyfit(x_sorted, y_sorted, 1)
        y_fit = np.polyval(p, x_sorted)

    elif "${trendlineType}" == "polynomial":
        # y = a_0 + a_1*x + a_2*x^2 + ...
        order = int("${polynomialOrder}")
        p = np.polyfit(x_sorted, y_sorted, order)
        y_fit = np.polyval(p, x_sorted)

    elif "${trendlineType}" == "exponential":
        # y = a * e^(b*x)
        # ln(y) = ln(a) + b*x => do linear fit
        log_y = np.log(y_sorted)
        p = np.polyfit(x_sorted, log_y, 1)
        # p[0] = b, p[1] = ln(a)
        y_fit = np.exp(p[1]) * np.exp(p[0] * x_sorted)

    elif "${trendlineType}" == "logarithmic":
        # y = a + b * ln(x)
        # We do linear fit with x' = ln(x)
        log_x = np.log(x_sorted)
        p = np.polyfit(log_x, y_sorted, 1)
        # p[0] = b, p[1] = a
        y_fit = p[1] + p[0] * log_x

    elif "${trendlineType}" == "power":
        # y = a * x^b
        # ln(y) = ln(a) + b * ln(x)
        log_x = np.log(x_sorted)
        log_y = np.log(y_sorted)
        p = np.polyfit(log_x, log_y, 1)
        # p[0] = b, p[1] = ln(a)
        y_fit = np.exp(p[1]) * (x_sorted**p[0])

    # Plot the fitted line
    ax.plot(x_sorted, y_fit, label="Trendline", color="red")
`;
    }

    // Function to plot scatter plot
    async function plotScatterPlot() {
      const data = document.getElementById('dataInput').value;
      const xColumn = document.getElementById('xSelect').value;
      const yColumn = document.getElementById('ySelect').value;
      const errorColumn = document.getElementById('errorSelect').value;
      const xLabel = document.getElementById('xAxisLabel').value;
      const yLabel = document.getElementById('yAxisLabel').value;
      const dataSeriesLabel = document.getElementById('dataSeriesLabel').value;
      const plotTitle = document.getElementById('plotTitle').value;

      // Trendline options
      const addTrendline = document.getElementById('addTrendline').checked;
      const trendlineType = document.getElementById('trendlineSelect').value;
      const polynomialOrder = document.getElementById('polynomialOrder').value;

      // Construct Python code for the optional trendline
      const trendlineCode = addTrendline
        ? getTrendlineCode(xColumn, yColumn, trendlineType, polynomialOrder)
        : '';

      try {
        // Run Python code in Pyodide
        await pyodide.runPythonAsync(`
import pandas as pd
from io import StringIO
import matplotlib.pyplot as plt
import base64
from matplotlib.figure import Figure
from matplotlib.backends.backend_agg import FigureCanvasAgg
csv_data = """${data}"""
df = pd.read_csv(StringIO(csv_data), delimiter='\\t')

# Aggregate means for the chosen columns, then reset index
df = df.groupby("${xColumn}").agg({
    "${yColumn}": "mean",
    "${errorColumn}": "mean"
}).reset_index()

# Sort data by x for correct plotting order
df = df.sort_values("${xColumn}")
x = df["${xColumn}"]
y = df["${yColumn}"]
yerr = df["${errorColumn}"] if "${errorColumn}" in df.columns else None

fig = Figure(figsize=(10, 6))
ax = fig.add_subplot(111)

# Plot data with error bars
ax.errorbar(
    x, y, yerr=yerr,
    fmt='o', capsize=5, capthick=2,
    color='grey', ecolor='black', elinewidth=2,
    markerfacecolor='black', markersize=10,
    label='${dataSeriesLabel}'
)

ax.set_title("${plotTitle}")
ax.set_xlabel("${xLabel}")
ax.set_ylabel("${yLabel}")
ax.grid(False)
ax.legend()

# Add trendline code if requested
${trendlineCode}

ax.legend()

# Save plot to PNG
canvas = FigureCanvasAgg(fig)
import io
buf = io.BytesIO()
canvas.print_png(buf)
buf.seek(0)
img_data = base64.b64encode(buf.getvalue()).decode('utf-8')
        `);

        // Retrieve and display the plot
        const imgData = await pyodide.globals.get('img_data');
        document.getElementById('plotCanvas').innerHTML = `<img src="data:image/png;base64,${imgData}" />`;
      } catch (error) {
        console.error('Error in plotScatterPlot:', error);
        alert('An error occurred while plotting the data. Check the console for details.');
      }
    }

    // Function to plot bar plot
    async function plotBarPlot() {
      const data = document.getElementById('dataInput').value;
      const xColumn = document.getElementById('xSelect').value;
      const yColumn = document.getElementById('ySelect').value;
      const errorColumn = document.getElementById('errorSelect').value;
      const xLabel = document.getElementById('xAxisLabel').value;
      const yLabel = document.getElementById('yAxisLabel').value;
      const dataSeriesLabel = document.getElementById('dataSeriesLabel').value;
      const plotTitle = document.getElementById('plotTitle').value;

      // Trendline options
      const addTrendline = document.getElementById('addTrendline').checked;
      const trendlineType = document.getElementById('trendlineSelect').value;
      const polynomialOrder = document.getElementById('polynomialOrder').value;

      // Construct Python code for the optional trendline (on top of bar plot)
      const trendlineCode = addTrendline
        ? getTrendlineCode(xColumn, yColumn, trendlineType, polynomialOrder)
        : '';

      try {
        // Run Python code in Pyodide
        await pyodide.runPythonAsync(`
import pandas as pd
from io import StringIO
import matplotlib.pyplot as plt
import base64
from matplotlib.figure import Figure
from matplotlib.backends.backend_agg import FigureCanvasAgg
import numpy as np

csv_data = """${data}"""
df = pd.read_csv(StringIO(csv_data), delimiter='\\t')

df = df.groupby("${xColumn}").agg({
    "${yColumn}": "mean",
    "${errorColumn}": "mean"
}).reset_index()

# Sort data by x
df = df.sort_values("${xColumn}")
x = df["${xColumn}"].astype(str)
y = df["${yColumn}"]
yerr = df["${errorColumn}"] if "${errorColumn}" in df.columns else None

fig = Figure(figsize=(10, 6))
ax = fig.add_subplot(111)

bar_positions = range(len(x))
ax.bar(
    x, y, yerr=yerr,
    capsize=10, color='gray', alpha=0.5,
    error_kw={'elinewidth': 2, 'capsize':5,'capthick':2},
    label='${dataSeriesLabel}'
)

ax.set_xticks(bar_positions)
ax.set_xticklabels(x)
ax.set_title("${plotTitle}")
ax.set_xlabel("${xLabel}")
ax.set_ylabel("${yLabel}")
ax.grid(False)
ax.legend()
ax.tick_params(axis='x', rotation=0)
fig.tight_layout()

# -- Add the trendline. Here we re-merge x and y as numeric for fitting. --
# We'll re-use the original numeric column from the group-by (not the string version).
# A quick approach: we can do it from df again, but let's be consistent with the group order.

${trendlineCode}

ax.legend()

# Save plot to PNG
canvas = FigureCanvasAgg(fig)
import io
buf = io.BytesIO()
canvas.print_png(buf)
buf.seek(0)
img_data = base64.b64encode(buf.getvalue()).decode('utf-8')
        `);

        // Retrieve and display the plot
        const imgData = await pyodide.globals.get('img_data');
        document.getElementById('plotCanvas').innerHTML = `<img src="data:image/png;base64,${imgData}" />`;
      } catch (error) {
        console.error('Error in plotBarPlot:', error);
        alert('An error occurred while plotting the data. Check the console for details.');
      }
    }
  </script>
</body>
</html>