hex93
hex93

Reputation: 359

Feeding multiple values into a function from array, but only for a single argument

This is a bit of a newbie question which I'm sure has been answered somewhere already but unfortunately I could not find it anywhere.

I have a function,findDX, that takes in an Nx2 numpy array, D, and a single float X (definition provided below). I want to evaluate this function on a fixedD with multiple values of X, i.e. find_DX(D, 1),find_DX(D, 2),find_DX(D, 3) where the array of X inputs is [1,2,3]

Is there a way to pass in multiple values of X while keeping D fixed (other than changing the function definition)? I have tried

find_DX([mcm],[1,2,3])

but this predictably gives a ValueError: operands could not be broadcast together with shapes

def find_DX(D, X):
    """
    Parameters
    ----------
    D : numpy array
        Input cumulative DVH.
    X : Float
       We're finding the dose recieved by at least X% of tissues.

    Returns
    -------
    DX

    """
    idx = (np.abs(D[:,1] - X)).argmin()
    return D[idx,0]

EDIT: I just wrote a loop for it. I was hoping to avoid having to do so but it does the job. I have posted my new function below if anyone wants to use it:

def find_DX(cDVH, X_values):
    """
    Parameters
    ----------
    cDVH : numpy array
        Input array
    X : numpy array
       Values of X to evaluate.

    """
    X_values=np.float64(X_values) #force to float
    Out=np.zeros_like(X_values)
    for i in range(0,len(X_values)):
        idx = np.abs(cDVH[:,1] - X_values[i]).argmin()
        Out[i]=cDVH[idx,0]

    return tuple(Out)

Upvotes: 0

Views: 398

Answers (1)

Pinindu Chethiya
Pinindu Chethiya

Reputation: 36

You can try this,

import numpy as np

def find_DX(D, X):
    """
    Parameters
    ----------
    D : numpy array
        Input cumulative DVH.
    X : Float
       We're finding the dose recieved by at least X% of tissues.

    Returns
    -------
    DX

    """
    tmpArr=[]
    for i in X:
        idx = (np.abs(D[:,1] - i)).argmin()
        tmpArr.append(idx)
    return tmpArr

a = [[1,2],[2,3]]   #sample array
a= np.array(a)

print(find_DX(a,[1,2,3]))

Upvotes: 1

Related Questions