Python代码IndexError:列表赋值索引超出范围

6yoyoihd  于 2023-05-21  发布在  Python
关注(0)|答案(2)|浏览(180)

我有一个代码,可以让我的显微镜扫描样品:

pos = microscope.position
print("Current position: " + json.dumps(pos))
print("Homing")
pos['x'] = 0
pos['y'] = 0
pos['z'] = 0

# units in microns
xyStep = 10
xRange = 400
yRange = 400
zRange = 400
#zBase = maxPosition - micronToStep(zRange/2)
#zBase = -((30000 - 15000)/2)
zBase = -1000

saveData = {
    "xyStep": xyStep,
    "xRange": xRange,
    "yRange": yRange,
    "zRange": zRange,
    "zBase": zBase,
    "data": [[0] * ceil(xRange/xyStep) for i in range(ceil(yRange/xyStep))],
}

measurements = 1
pos = microscope.position
print("Current position: " + json.dumps(pos))

starting_pos = microscope.position
#figure(figsize=(20,5))

ix = 0
iy = 0

totalElapsed = 0
stepsTaken = 1
maxSteps = (xRange/xyStep) * (yRange/xyStep)
up = True

ix = 0
for x in range(-micronToStep(xRange/2), micronToStep(xRange/2) - micronToStep(xyStep), micronToStep(xyStep)):
    iy = 0
    for y in range(-micronToStep(yRange/2), micronToStep(yRange/2) - micronToStep(xyStep), micronToStep(xyStep)):
        start = timer()
        values = []
        if (up):
            pos = {}
            pos['x'] = x + 7500 # declination
            pos['y'] = y - 5500 # declination
            pos['z'] = zBase - micronToStep(zRange/2)
            pos['z2'] = zBase + micronToStep(zRange/2)
            print("Scanning up to position:", pos)

            values = microscope.measureZ(microscope, pos)
            values = np.asarray(values)
            thisTime =  timer() - start
            totalElapsed += thisTime
            up = False
        else:
            pos = {}
            pos['x'] = x + 7500 # declination
            pos['y'] = y - 5500 # declination
            pos['z'] = zBase + micronToStep(zRange/2)
            pos['z2'] = zBase - micronToStep(zRange/2)
            print("Scanning to position:", pos)

            values = microscope.measureZ(microscope, pos)
            values = np.asarray(values)
            values = np.flip(values)
            thisTime =  timer() - start
            totalElapsed += thisTime
            up = True

        print(pos)
        saveData['data'][ix][iy] = values

        iy += 1
        timeLeft = (maxSteps - stepsTaken) * (totalElapsed / stepsTaken) / 60 / 60
        print(">>>>>>>>>>>> Estimated time left: [" + str(timeLeft) + "hr], Per Scan: [" + str(totalElapsed / stepsTaken) + "s][" + str(thisTime) + "], Total Elapsed: [" + str(totalElapsed) + "s], Scans taken: [" + str(stepsTaken) + "] out of: [" + str(maxSteps) + "]")
        stepsTaken += 1
        pickle.dump(saveData, open('saveData.bin', 'wb'))

    ix += 1


pickle.dump(saveData, open('saveData.bin', 'wb'))

它适用于xyStep = 10,但当我将此参数更改为xyStep = 1时,在打印扫描后:[200] I get an error:in line 149, saveData['data'][ix][iy] = values: IndexError: list assignment index out of range。函数micronToStep的定义很简单:def micronToStep(v):返回循环((v * 1000)/62)。
我假设问题出在数组维度上。更准确地说,如果需要解决这个问题,我可以添加有关代码的更多信息。
如何纠正这一点?

gj3fmq9x

gj3fmq9x1#

saveData["data"]是一个400 x 400的列表。
你有这些循环:

for x in range(-micronToStep(xRange/2), micronToStep(xRange/2) - micronToStep(xyStep), micronToStep(xyStep)):
    for y in range(-micronToStep(yRange/2), micronToStep(yRange/2) - micronToStep(xyStep), micronToStep(xyStep)):

在执行计算之后,这些回路上的实际范围条件为:

range(-3225, 3209, 16)

...它产生了略多于400次的迭代(我相信是402次)。因此,ix和iy最终会变得太大,并产生超出范围的误差。

yvgpqqbh

yvgpqqbh2#

post中的代码开始于:“我有一个代码:“工程正确的情况下

xyStep = 10
xRange = 400
yRange = 400

并打印:

Current position: {"x": 0, "y": 0, "z": 0}
Homing
Current position: {"x": 0, "y": 0, "z": 0}
Scanning up to position: {'x': -3226, 'y': -3226, 'z': -3226, 'z2': 3226}
{'x': -3226, 'y': -3226, 'z': -3226, 'z2': 3226}
>>>>>>>>>>>> Estimated time left: [3.450211587398544hr], Per Scan: [7.767830966000474s][7.767830966000474], Total Elapsed: [7.767830966000474s], Scans taken: [1] out of: [1600.0]
Scanning to position: {'x': -3226, 'y': -3065, 'z': 3226, 'z2': -3226}
{'x': -3226, 'y': -3065, 'z': 3226, 'z2': -3226}
>>>>>>>>>>>> Estimated time left: [3.308844005120933hr], Per Scan: [7.454216782500225s][7.140602598999976], Total Elapsed: [14.90843356500045s], Scans taken: [2] out of: [1600.0]
Scanning up to position: {'x': -3226, 'y': -2904, 'z': -3226, 'z2': 3226}

但是当xyStep = 1时它就停止工作了,而当xyStep = 0.9,xyStep = 0.8…等等。
原因是在xyStep = 1的情况下编写了略多于400次的迭代。如何更正条件以使程序在以下情况下工作

xyStep = 0.5
xRange = 400
yRange = 400

相关问题