【发布时间】:2021-08-13 20:20:00
【问题描述】:
此代码旨在根据参考光谱校正薄膜光谱,找到最小值和最大值,并绘制校正后的光谱。
在减法的情况下,它工作正常:
corrected_power = pd.DataFrame(spec_film["Power (W)"] - spec_ref["Power (W)"], columns = ["Power (W)"])
corrected_spec = corrected_power.join(spec_film["Wavelength (nm)"])
###############Find local maxima ###################
local_max = np.array(argrelextrema(corrected_power.values, np.greater_equal, order=10))
#find corresponding power values for the local maxima
local_max = np.array([x*2+400 for x in local_max][0]), np.zeros(len(local_max[0]))
local_maxes = np.transpose(np.array([np.zeros(len(local_max[0])),list(local_max[0])]))
i = 0
# take the power values for the max wavelengths in local_max and put them in a new list
for elem in local_max[0]:
local_maxes[i,0] = corrected_spec.loc[corrected_spec["Wavelength (nm)"] == elem].values[0,0]
i += 1
______
The maxima are:[ 406. 438. 506. 612. 762. 938. 1000.]
The minima are:[420. 472. 554. 670. 830. 966.]
但是,当我除而不是减时,我得到一个超出范围的错误:
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
<ipython-input-17-5d7219828f63> in <module>
47
48 for elem in local_max[0]:
---> 49 local_maxes[i,0] = corrected_spec.loc[corrected_spec["Wavelength (nm)"] == float(elem)].values[0,0]
50 i += 1
51
IndexError: index 0 is out of bounds for axis 0 with size 0
奇怪的是,找到最小值在这两种情况下都有效,这是使用与上面完全相同的代码块完成的:
local_min = np.array(argrelextrema(corrected_power.values, np.less_equal, order=10))
local_min = np.array([x*2+400 for x in local_min][0]), np.zeros(len(local_min[0]))
local_mins = np.transpose(np.array([np.zeros(len(local_min[0])),list(local_min[0])]))
i = 0
# take the power values for the min wavelengths in local_max and put them in a new list
for elem in local_min[0]:
local_mins[i,0] = corrected_spec.loc[corrected_spec["Wavelength (nm)"] == elem].values[0,0]
i += 1
我认为这可能意味着 .loc 没有找到任何值,但我手动尝试了一些并找到了它们。
我在 for elem in local_(max/min)[0] 运行之前检查过,两个数组看起来一样:
local max = (array([400, 438, 506, 612, 762, 986], dtype=int64), array([0., 0., 0., 0., 0., 0.])) length = 2
local maxes = [[ 0. 400.]
[ 0. 438.]
[ 0. 506.]
[ 0. 612.]
[ 0. 762.]
[ 0. 986.]] length = 6
local min = (array([ 414, 470, 554, 674, 862, 1000], dtype=int64), array([0., 0., 0., 0., 0., 0.])) length = 2
local mins = [[ 0. 414.]
[ 0. 470.]
[ 0. 554.]
[ 0. 674.]
[ 0. 862.]
[ 0. 1000.]] length = 6
我在这里缺少什么?为什么除法而不是减法只对找到最大值的部分给出超出范围的错误,而不是找到最小值的部分?
【问题讨论】:
-
欢迎来到 Stack Overflow。每种方法的
corrected_spec是什么样的?在每种情况下,您从corrected_spec.loc[corrected_spec["Wavelength (nm)"] == elem]得到什么结果?请try to debug the program yourself,如果您仍然遇到困难,您可以利用您的发现帮助创建minimal reproducible example。 -
这是我的问题:波长应间隔 2 nm。我们的 labview 代码有一个错误,它偶尔会吐出不正确的指数。我手动修复了它,但将其设为“985”而不是“986”。 "local_mins[i,0] = Corrected_spec.loc[corrected_spec["Wavelength (nm)"] == elem].values[0,0]" 正在寻找 986(因为我使用 [x*2 从索引转换为波长+400 for x in local_min][0]),但没有找到它,所以它返回了一个形状 (0) 列表。
标签: python arrays pandas numpy