| 31 | } |
| 32 | |
| 33 | function callPyodide(pyodide, pythonSnippet, samples) { |
| 34 | console.debug('Running Python Snippet'); |
| 35 | |
| 36 | // if for some reason it's still not initialized, return samples without modification |
| 37 | if (!pyodide) { |
| 38 | console.debug('Pyodide isnt initialized yet'); |
| 39 | return samples; |
| 40 | } |
| 41 | |
| 42 | // make samples available within python |
| 43 | // trick from https://github.com/pyodide/pyodide/blob/main/docs/usage/faq.md#how-can-i-execute-code-in-a-custom-namespace |
| 44 | let myNamespace = pyodide.toPy({ x: Array.from(samples) }); |
| 45 | |
| 46 | // Add the conversion code to the snippet |
| 47 | pythonSnippet = 'import numpy as np\nx = np.asarray(x)\n' + pythonSnippet + '\nx = x.tolist()'; |
| 48 | |
| 49 | // TODO: print python errors to console somehow, look at https://pyodide.org/en/stable/usage/api/python-api/code.html#pyodide.code.eval_code |
| 50 | pyodide.runPython(pythonSnippet, { globals: myNamespace }); |
| 51 | |
| 52 | samples = myNamespace.toJs().get('x'); // pull out python variable x |
| 53 | |
| 54 | return samples; |
| 55 | } |
| 56 | |
| 57 | export function applyProcessing(samples, taps, pythonSnippet, pyodide) { |
| 58 | if (taps && taps.length !== 1) { |