2

I have a problem.m file that has a function like this: myfun(p,m).

It does some calculation and returns the result. For testing the execution of this function I have a test.m file that looks like this.
myfun(myarg1,myarg2)

If I run this file as: octave test.m Then it returns me the correct result which looks like this : 0.38007

Now, the problem is when calling this function myfun(p,m) using python. I tried to use python library : oct2py

The python code looks like this:

import sys
import subprocess
import oct2py
import requests
from oct2py import octave

def myfun(p,m):
    octave.addpath('/mypath');
    oc = oct2py.Oct2Py()
    #res = octave.myfun(p,m nout=1);#this didn't work, hence comment
    #res = oc.myfun(p, m) #this didn't work, hence comment    
    x = oc.feval("myfun",p,m);
    print(x);

if __name__ == '__main__':
    myfun(sys.argv[1],sys.argv[2])

When i run this code as: python FileName.py arg1 arg2 (same arguments i used in test.m) , it gives me a warning message and an empty list like this :

warning: some elements in list of return values are undefined []

I am not sure what to do about this. As the function seems to be returning correct result in a correct format when using octave. but for some reason oct2py is not working.

STZ
  • 161
  • 2
  • 10
  • Does your Octave function actually return an output or is it just printing it to the command window? Please see [mcve]. – sco1 Oct 16 '17 at 14:19
  • my octave function returns an output, as i can see ans in octave-cli like this: octave:1> mytest timeBSeuCallUI = 2.6464e-04 1.5133e+00 NaN relerrBSeuCallUI = 2.7459e-05 6.2769e-05 NaN ans = 2.6464e-04 octave:2> ans ans = 2.6464e-04 – STZ Oct 17 '17 at 06:34
  • system arguments are always strings. if you need integer inputs you should parse them as integers first – Tasos Papastylianou Oct 18 '17 at 16:23

1 Answers1

4

Octave code:

function result = test(problem,method)    
Methods={'MC','COS','RBF-AD','RBF-MLT'};    
if problem == 1    
    S=[90,100,110]; K=100; T=1.0; r=0.03; sig=0.15;    
    U=[2.758443856146076 7.485087593912603 14.702019669720769];    
    rootpath=pwd;    
    filepathsBSeuCallUI=getfilenames('./','BSeuCallUI_*.m');    
    par={S,K,T,r,sig};    
    [timeBSeuCallUI,relerrBSeuCallUI] = 
    executor(rootpath,filepathsBSeuCallUI,U,par)    

    tBSeuCallUI=NaN(numel(Methods),1); rBSeuCallUI=tBSeuCallUI;    
    for ii=1:numel(Methods)    
        for jj=1:numel(filepathsBSeuCallUI)    
            a=filepathsBSeuCallUI{jj}(3:3+numel(Methods{ii}));    
            b=[Methods{ii},'/'];    
            if strcmp(a,b)    
            tBSeuCallUI(ii)=timeBSeuCallUI(jj);    
            rBSeuCallUI(ii)=relerrBSeuCallUI(jj);    
        end    
    end    
end    

cd(rootpath);    
for el=1:numel(Methods)    
    if strcmp((Methods{el}),method)    
        result = tBSeuCallUI(el);  
    end
 end    
end    

Python code :

import sys    
import subprocess    
import oct2py    
import requests    

def benchop(problem,method):    
    oc = oct2py.Oct2Py()    
    res = oc.test(problem,method)    
    return res    

if __name__ == '__main__':    
    benchop(sys.argv[1],sys.argv[2]) 

The problem in the octave code as when checking the problem, it says problem==1, but python is expecting a string so the list undefined warning with no resulting value. Changing octave code from problem == 1 to problem == "1" solves the problem.

STZ
  • 161
  • 2
  • 10
  • Is there a way for me to call the function that takes arguments and outputs values without using the oct2py? – Wallflower Jan 22 '21 at 12:57