首页 > 解决方案 > 功能错误。ValueError:使用序列设置数组元素

问题描述

我编写了一个函数来创建一个域来收集进入探测器的光强度。当我使用单个值作为起点时很好,但是当我设置一个点数组时(因为我需要描述一个表面并将强度积分到这个表面上)它会引发 ValueError: setting an array element with a sequence .

我试图验证不同数组的形状和它(3,)和(3,),所以理论上没有不一致......这里的函数

def probe_intensity(cone_FOV, n_circles):
    probeI = 0
    # probe location information
    #probe_angle = 5.0/4.0*np.pi
    #tvar=np.linspace(4.09,4.186,30)
    ray_cone_angle = cone_FOV/(2.0*n_circles)
   # T0 = np.array([0, 0, 1])
    r = 6.2
    x = 5.5
    probe_angle = np.linspace(1.2334*np.pi-1,1.2334*np.pi+1,30)
    probe_direction_angle = probe_angle - np.pi
    #R1=np.ones(len(probe_angle))
    # probe direction
    for k in probe_angle:

# getting the estimated cone for a single ray
#ray_cone_angle = cone_FOV/(2.0*n_circles)

# probe position information
#r = 6.2
#x = 5.5

        R0 = np.array([5.5,
               r*np.sin(probe_angle)+0.0001,
               r*np.cos(probe_angle)+0.0001])*0.001

        gamma = probe_direction_angle

        ROT1 = np.matrix([[1, 0, 0],
                  [0, np.cos(gamma), np.sin(gamma)],
                  [0, -np.sin(gamma), np.cos(gamma)]])


# vector T0 before any rotation
        T0 = np.array([0, 0, 1])

# vector T1 - probe axis vector
        T1 = np.array(ROT1*np.matrix(T0).T).flatten()

    # rotation for the cone_half_angle
        for ray_carrier_angle in [(i + 0.5)*ray_cone_angle for i in range(n_circles)]:

            alpha = ray_carrier_angle

            ROT2 = np.matrix([[np.cos(alpha), 0, np.sin(alpha)],
                          [0, 1, 0],
                          [-np.sin(alpha), 0, np.cos(alpha)]])

        # rotating to make a full circle of the cone
            for beta in np.linspace(0, 2*np.pi, int(np.round(n_circles*np.pi))):
                ROT3 = np.matrix([[np.cos(beta), np.sin(beta), 0],
                              [-np.sin(beta), np.cos(beta), 0],
                              [0, 0, 1]])

            # second rotaiton for the cone after the probe direction rotation
                T3 = np.array(ROT1*ROT3*ROT2*np.matrix(T0).T).flatten()
                print R0
                print R0.shape
                print T3
                print T3.shape
                T3=list(T3)
                R0=list(R0)


                ray_solution = ray_trace_solve_ivp(R0, T3, 0.0005)
                t, I = field.integrate_trace(ray_solution, 0.0005)
            #s,e,alpha,I = field.getspectra

        # Add the intensity of the ray to the intensity gathered at the probe. Dot product takes care of the projection
            probeI += I[-1]*np.dot(T0, T3)
            print probeI

return probeI

另一个被调用的函数是

def ray_trace_solve_ivp(R0, T0, optical=False, dt=np.inf, atol=1e-6, rtol=1e-2):
    y0 = np.r_[R0, T0]

    if optical:
        differential_equation = eikonalODE1system_optical
    else:
        differential_equation = eikonalODE1system_physical

    sol = solve_ivp(differential_equation, [0, 0.5], y0,
                    events=limit_functions,
                    dense_output=True,
                    max_step=dt,
                    atol=atol,
                    rtol=rtol)
    return sol

我收到以下错误消息:

  运行文件中的文件“/home/tont_fe/anaconda2/lib/python2.7/site-packages/spyder_kernels/customize/spydercustomize.py”,第 827 行
    execfile(文件名,命名空间)

  文件“/home/tont_fe/anaconda2/lib/python2.7/site-packages/spyder_kernels/customize/spydercustomize.py”,第 102 行,在 execfile
    builtins.execfile(文件名,*位置)

  文件“/home/tont_fe/data/cfd/OH_analysis/OHstar_ray_tracing/oh_ray_trace_candidate_cone_federica.py”,第 1109 行,在
    打印(“探针强度=”,probe_intensity(np.pi/20.0,10))

  文件“/home/tont_fe/data/cfd/OH_analysis/OHstar_ray_tracing/oh_ray_trace_candidate_cone_federica.py”,第 878 行,在 probe_intensity
    ray_solution = ray_trace_solve_ivp(R0, T3, 0.0005)

  文件“/home/tont_fe/data/cfd/OH_analysis/OHstar_ray_tracing/oh_ray_trace_candidate_cone_federica.py”,第 690 行,位于 ray_trace_solve_ivp
    rtol=rtol)

  文件“/home/tont_fe/anaconda2/lib/python2.7/site-packages/scipy/integrate/_ivp/ivp.py”,第456行,在solve_ivp
    求解器 = 方法(有趣,t0,y0,tf,矢量化=矢量化,**选项)

  文件“/home/tont_fe/anaconda2/lib/python2.7/site-packages/scipy/integrate/_ivp/rk.py”,第 96 行,在 __init__
    support_complex=真)

  文件“/home/tont_fe/anaconda2/lib/python2.7/site-packages/scipy/integrate/_ivp/base.py”,第 120 行,在 __init__
    self._fun, self.y = check_arguments(fun, y0, support_complex)

  文件“/home/tont_fe/anaconda2/lib/python2.7/site-packages/scipy/integrate/_ivp/base.py”,第 15 行,在 check_arguments
    y0 = y0.astype(dtype, 复制=假)

标签: pythonnumpysequencereturn-valueuser-defined-functions

解决方案


所以你的代码的相关部分是:

probe_angle = np.linspace(1.2334*np.pi-1,1.2334*np.pi+1,30)
R0 = np.array([5.5,
               r*np.sin(probe_angle)+0.0001,
               r*np.cos(probe_angle)+0.0001])*0.001
T0 = np.array([0, 0, 1])
y0 = np.r_[R0, T0]

或简化一点:

In [96]: R0 = np.array([1, np.linspace(0,1,3), np.linspace(0,3,3)])             
In [97]: R0                                                                     
Out[97]: array([1, array([0. , 0.5, 1. ]), array([0. , 1.5, 3. ])], dtype=object)
In [98]: T0 = np.array([0,0,1])                                                 
In [99]: np.r_[R0,T0]                                                           
Out[99]: 
array([1, array([0. , 0.5, 1. ]), array([0. , 1.5, 3. ]), 0, 0, 1],
      dtype=object)
In [100]: _.astype(float)                                                       
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-100-b12cc607d111> in <module>
----> 1 _.astype(float)

ValueError: setting an array element with a sequence.

你的R0,然后y0是构造函数,创建一个对象 dtype 数组 - 一个包含数组的数组。

solve_ivp需要一个 (n,) 浮点数组,其中ndifferential_equation产生的值的数量。

直接的问题是从那个创建一个数字数组y0。由于我不知道您需要或想要什么,因此我不会提出解决方案。

另外我对differential_equation. 我不知道它返回了多少价值。只有一个,三个?或者它是否随y0输入的大小而变化?

退后一步,解决一个更简单的问题。确保您了解solve_ivp功能和y0.

当您带着新问题回来时,请让帮助变得更容易!


推荐阅读