Search code examples
pythonsvmlibsvmembedding

Convert data values from Python to C


For this project I am working with libsvm.

I have a python file that is able to output a list of feature vectors and I have a C executable that takes 2 csv files, a list of feature vectors and the svm model, as arguments and outputs a prediction in the form of a csv file.

Now, I would like to change the C file such that it takes in the list output from the python file as its input arguments to make a prediction. This is because I am having to run the python code and C in real time. Therefore, latency will be an issue if I am having to write to a csv file with python and read the file in C.

I have tried searching things like cython, subprocess module and argparse. However, it seems like these are used to execute Python functions in C and C in Python. Can someone please help understand how to transfer data from python to C? Thank you.


Solution

  • It will depends the amount of latency you are accepting as input. I am not familiar with libsvm so I admit you are able to read your feature vector in my solutions:

    First solution (easier but slower) would be to make a little Python to C library as follow:

    #/usr/bin/python
    def print_vector(vec):
      first =  true;
      print("[")
      for i in vec: 
        if first: 
          first = false
        else:
          print(',')
        print(i)
      print("]")
    

    and to parse it from the standard input in C with <string.h> library and using atoi.

    You will then execute your command as follow:

    ./my_python_program | ./my_c_program
    

    Second solution (way faster but way harder) which is implement pipe connection between you python program and C. Or even TCP socket connections.

    You can, for instance, have a look to the Linux Documentation if you are under Linux.