0

I have a .py file with a function that calculates the gradient of a function at a point and returns the value of that gradient at the point. The function takes a np.array([2,]) as input and outputs another np.array([2,]). I am confused as to how I can call the function from the cmd line and run the function with a specified input.

Here is a code snippet:

import numpy as np

def grad(x):
    
    x_1 = x[0]
    x_2 = x[1]
    
    df_dx_1 = 6*x
    df_dx_2 = 8*x_2 
    
    df_dx = np.array([df_dx_1, df_dx_2])
    
    return np.transpose(df_dx)

I would really appreciate your help!

EDIT: This question differs from the popular command line thread because I have a specific issue of not being able to recognise the numpy input

TheQuant
  • 49
  • 5
  • 2
    Does this answer your question? [How to read/process command line arguments?](https://stackoverflow.com/questions/1009860/how-to-read-process-command-line-arguments) – Trenton Trama Oct 25 '20 at 17:27
  • you can add this function to the `main` function of your `.py` file and then run the file from the command line. You can refer [here](https://www.tutorialspoint.com/python/python_command_line_arguments.htm) for the same – void main Oct 25 '20 at 17:28
  • How does the input look like? You won't have a numpy array in the terminal – Wups Oct 25 '20 at 17:33
  • I need to be able to pass my function a np.array([2,]) and output the same. Any solutions? So ideally the input would be something like this grad(np.array([10,10])) – TheQuant Oct 25 '20 at 17:36
  • https://stackoverflow.com/questions/58694092/convert-json-file-to-pandas-dataframe There is an answer here. Maybe it will help you. – Furkan Balıkçı Oct 25 '20 at 17:53
  • @FurkanBalıkçı I am sorry I do not see where... Could you be explicit please – TheQuant Oct 25 '20 at 18:29

3 Answers3

1

First change script to (Here it uses if __name__='__main__' to check if it is running from script, then import sys and pass first argument using sys.argv[0] to the function):

import numpy as np

def grad(x):
    
    x_1 = x[0]
    x_2 = x[1]
    
    df_dx_1 = 6*x
    df_dx_2 = 8*x_2 
    
    df_dx = np.array([df_dx_1, df_dx_2])
    
    return np.transpose(df_dx)

if __name__ == '__main__':
  import sys
  grad(sys.argv[1])

And call it like:

python "YOURSCRIPTPATH.py" argument_1
Wasif
  • 14,755
  • 3
  • 14
  • 34
  • How would this work if my file say had multiple functions? How would I specify which to call? In addition, would the person calling the function be able to specify what np.array they give the function? – TheQuant Oct 25 '20 at 17:27
  • You can modify it in the `if` block, `sys.argv` is the list of your command-line args – Wasif Oct 25 '20 at 17:27
  • 1
    I think you should use `sys.argv[1]`, as the first element of `argv` is just the name of the script. – Eric Hua Oct 25 '20 at 17:29
  • @TheQuant you can use the `sys.argv` list to access multiple arguments. – void main Oct 25 '20 at 17:30
  • This does not seem to work for me when I run `python gradient.py np.array([10, 5])` I get an error saying `zsh: no matches found: np.array([10, 5])` – TheQuant Oct 25 '20 at 17:43
0

You could just something like this in the command line:

$ python -c 'from YOURFILE import grad; print(grad(your_argument))'
Oliver Hnat
  • 797
  • 4
  • 19
0

You can have more than one command line argument:

import sys
import numpy as np

def grad(x):
   # your grad function here


arr = np.array([int(sys.argv[1]), int(sys.argv[2])])
print(grad(arr))

Usage:

python gradient.py 10 5
Wups
  • 2,489
  • 1
  • 6
  • 17
  • I have updated my code to add `if __name__ == "__main__": import sys array = np.array([sys.argv[1], sys.argv[2]]) print(grad_f1(array)) ` but this gives me an error telling me that `TypeError: unsupported operand type(s) for -: 'str' and 'str'` when subtracting `df_dx_1 = 6*x_1 - 4*x_2` – TheQuant Oct 25 '20 at 18:26
  • @TheQuant I forgot to cast the input to integers. I've updated the answer now. – Wups Oct 25 '20 at 18:42