0

I know nothing about cython, only python.

The function emd_c below (from the pot optimal transport package) has a header whose argument formats I've never seen before, or which I don't think would work under stand-alone python/numpy, but perhaps only if cython is loaded as well.

def emd_c(np.ndarray[double, ndim=1, mode="c"] a, np.ndarray[double, ndim=1, mode="c"] b, np.ndarray[double, ndim=2, mode="c"] M, int max_iter):

As you can see, there are spaces in the arguments themselves ([] a, [] b, [] M) which would normally be illegal.

I wanted to use this function stand-alone rather than installing the entire package it comes from:

Copying the function out of the package it came from, and pasting the function by itself in my own script, Spyder instantly gave me an invalid syntax error when I tried to run my script, even though I was only expecting errors having to do with missing dependencies. My questions here only have to do with trying to understand this strange-looking function.

  1. What explains the strange formatting for the input arguments in this function? Is it because it is not python-in-isolation compliant and requires cython additionally?
  2. How to make this sort of function run properly?
develarist
  • 1,224
  • 1
  • 13
  • 34
  • https://cython.readthedocs.io/en/latest/src/tutorial/numpy.html – ead Nov 02 '20 at 12:43
  • @ead are you referring to the "Efficient Indexing" section? `def naive_convolve(np.ndarray[DTYPE_t, ndim=2] f, np.ndarray[DTYPE_t, ndim=2] g):` looks similar to what I'm asking – develarist Nov 02 '20 at 12:46
  • 1
    Cython requires you to compile your file into an extension module. You cannot just run it using Python. If you want to use Cython syntax (i.e. things that aren't Python-compliant) then the Cython source file needs to be a .pyx file – DavidW Nov 02 '20 at 14:21
  • without seeing the rest of the package or imported dependencies, how else can I identify that a python function is a cython function besides signs of the "efficient indexing" ability? can cython functions in fact appear completely like normal python functions, going unnoticed? – develarist Nov 02 '20 at 14:25
  • 1
    Most Python code should also work as Cython code (ignoring some small incompatibilities) so a Cython function could appear (and be) a normal Python function. However, the easiest thing is probably assume that anything you find in a .pyx file needs Cython to run. If you happen to find stuff in a .pyx that works in Python alone then it's "luck" – DavidW Nov 02 '20 at 14:53

0 Answers0