如何使用接口信号遍历numpy矩阵?

How to use interface signal to iterate through numpy matrix?

我在使用接口信号遍历 numpy 矩阵时遇到了问题。 我试图手动投射“addr”,但它不起作用。 这是我的代码:

@gear
    async def ram_model_data_drv(addr: Uint[8],*,
                                 data_type = b'dtype',
                                 num_of_commands = 1) -> Queue['data_type']:
        matrix = np.random.randint(10, size = (num_of_commands, 10))
        async with addr:
            for data, last in quiter(matrix[addr]):
                yield (data, last)

这是我收到的错误消息。

IndexError: only integers, slices (:), ellipsis (...), numpy.newaxis (None) and integer or boolean arrays are valid indices, in the module "/ram_model_drv"

这里的主要问题是您正在使用接口对象 (addr) 对矩阵进行切片。 PyGears 中的接口对象通过函数的位置参数(在您的情况下仅 addr )传递,并用于模块之间的通信。另一方面,keyword-only 参数(在您的情况下为 data_typenum_of_commands)可以携带任何类型的对象,但通常用作编译时参数。

无论如何,虽然你正确地使用async with语句从addr接口获取数据,但你应该将获取的数据放入一个新的变量中,如下所示:async with addr as a,应该读作:“等待数据在 addr 接口上可用,读取它们并将它们存储到名为 a 的变量中”。

旁注,您使用 b'dtype' 作为 data_type 参数的值,但 dtype 既不是参数名称也不是类型模板的名称,因此这里是多余的.我的猜测是,您希望模块的用户提供 data_type 参数作为输出数据类型,因此您应该将其保留为没有默认值。

from pygears import gear
from pygears.typing import Queue
import numpy as np
from pygears.util.utils import quiter


@gear
async def ram_model_data_drv(addr: Uint[8],
                             *,
                             data_type,
                             num_of_commands=1) -> Queue['data_type']:
    matrix = np.random.randint(10, size=(num_of_commands, 10))
    async with addr as a:
        for data, last in quiter(matrix[a]):
            yield (data, last)