0

I have Node v18.13.0, I have run npm install -D "pyodide@>=0.21.0-alpha.2", and am running the following code (put it in a file called test.js and run node test.js):

var {loadPyodide} = require("pyodide");

(async function main() {
  // setup

  var pyodide = await loadPyodide();
  await pyodide.loadPackage("micropip")
  const micropip = pyodide.pyimport("micropip");
  await micropip.install('scipy')
  var scipy = await pyodide.loadPackage("scipy");
  var numpy = await pyodide.loadPackage("numpy");

  // imports
  pyodide.runPython('import numpy as np')
  pyodide.runPython('import scipy.special')
  pyodide.runPython('import scipy.special as sp')
  pyodide.runPython('from scipy.special import betaln, beta as betafn')

  // testing with JavaScript arguments
  var x = pyodide.globals.get('np').ones(pyodide.toPy([3, 3])).toJs()
  console.log(x) // works!
  var x = pyodide.globals.get('np').exp(pyodide.toPy([3])).toJs()
  console.log(x) // works!
  
  // don't work?
  try {
    var x = pyodide.globals.get('sp').beta(pyodide.toPy([3.1, 3.1])).toJs()
    console.log(x)
  } catch (e) { console.log('thats a nope') }
  
  try {
    var x = pyodide.globals.get('scipy.special').beta(pyodide.toPy([3, 2])).toJs()
    console.log(x)
  } catch (e) { console.log('thats a nope') }

  try {
    var x = pyodide.globals.get('betaln')(pyodide.toPy([3, 2])).toJs()
    console.log(x)
  } catch (e) { console.log('thats a nope') }

  // testing inside Python works fine
  pyodide.runPython('print(sp.beta(3, 2))')
  pyodide.runPython('print(scipy.special.beta(3, 2))')
  pyodide.runPython('print(betaln(3, 2))')
})()

A lot of this script works! After printing some preliminary stuff like

Loading micropip, pyparsing, packaging
Loaded pyparsing, packaging, micropip
Loading scipy, numpy, clapack
Loaded clapack, numpy, scipy
scipy already loaded from default channel
No new packages to load
numpy already loaded from default channel
No new packages to load

I see the call to np.ones and np.exp working:

[
  Float64Array(3) [ 1, 1, 1 ],
  Float64Array(3) [ 1, 1, 1 ],
  Float64Array(3) [ 1, 1, 1 ]
]
Float64Array(1) [ 20.085536923187668 ]

So far so good, I can call Numpy functions. But I encounter exceptions when calling the Scipy special functions beta or betaln. All three of my attempts fail, throwing an exception:

  • pyodide.globals.get('sp').beta(pyodide.toPy([3, 2])).toJs()
  • pyodide.globals.get('scipy.special').beta(pyodide.toPy([3, 2])).toJs()
  • pyodide.globals.get('betaln')(pyodide.toPy([3, 2])).toJs()

I know all three of these should work because I can run them inside pyodide.runPython at the end of my function just fine. So I'm just unable to transfer the examples from Numpy to Scipy.

(My eventual goal is to have an anonymous function in JavaScript to spicy.special.beta, which ordinary JavaScript code can call with JavaScript numbers.)

How can I adapt the above code to successfully call these Scipy functions? Where am I going wrong?

Ahmed Fasih
  • 6,458
  • 7
  • 54
  • 95

0 Answers0