Ejemplo n.º 1
0
def test_plot_argus_phosphenes():
    df = pd.DataFrame([
        {
            'subject': 'S1',
            'electrode': 'A1',
            'image': np.random.rand(10, 10),
            'xrange': (-10, 10),
            'yrange': (-10, 10)
        },
        {
            'subject': 'S1',
            'electrode': 'B2',
            'image': np.random.rand(10, 10),
            'xrange': (-10, 10),
            'yrange': (-10, 10)
        },
    ])
    _, ax = plt.subplots()
    plot_argus_phosphenes(df, ArgusI(), ax=ax)
    plot_argus_phosphenes(df, ArgusII(), ax=ax)

    # Add axon map:
    _, ax = plt.subplots()
    plot_argus_phosphenes(df, ArgusI(), ax=ax, axon_map=AxonMapModel())

    # Data must be a DataFrame:
    with pytest.raises(TypeError):
        plot_argus_phosphenes(np.ones(10), ArgusI())
    # DataFrame must have the required columns:
    with pytest.raises(ValueError):
        plot_argus_phosphenes(pd.DataFrame(), ArgusI())
    # Subjects must all be the same:
    with pytest.raises(ValueError):
        dff = pd.DataFrame([{'subject': 'S1'}, {'subject': 'S2'}])
        plot_argus_phosphenes(dff, ArgusI())
    # Works only for Argus:
    with pytest.raises(TypeError):
        plot_argus_phosphenes(df, AlphaAMS())
    # Works only for axon maps:
    with pytest.raises(TypeError):
        plot_argus_phosphenes(df, ArgusI(), ax=ax, axon_map=ScoreboardModel())
    # Manual subject selection
    plot_argus_phosphenes(df[df.electrode == 'B2'], ArgusI(), ax=ax)
    # If no implant given, dataframe must have additional columns:
    with pytest.raises(ValueError):
        plot_argus_phosphenes(df, ax=ax)
    df['implant_type_str'] = 'ArgusII'
    df['implant_x'] = 0
    df['implant_y'] = 0
    df['implant_rot'] = 0
    plot_argus_phosphenes(df, ax=ax)
Ejemplo n.º 2
0
def test_plot_argus_phosphenes():
    df = pd.DataFrame([
        {
            'subject': 'S1',
            'electrode': 'A1',
            'image': np.random.rand(10, 10),
            'img_x_dva': (-10, 10),
            'img_y_dva': (-10, 10)
        },
        {
            'subject': 'S1',
            'electrode': 'B2',
            'image': np.random.rand(10, 10),
            'img_x_dva': (-10, 10),
            'img_y_dva': (-10, 10)
        },
    ])
    _, ax = plt.subplots()
    plot_argus_phosphenes(df, ArgusI(), ax=ax)
    plot_argus_phosphenes(df, ArgusII(), ax=ax)

    # Add axon map:
    _, ax = plt.subplots()
    plot_argus_phosphenes(df, ArgusI(), ax=ax, axon_map=AxonMapModel())

    # Data must be a DataFrame:
    with pytest.raises(TypeError):
        plot_argus_phosphenes(np.ones(10), ArgusI())
    # DataFrame must have the required columns:
    with pytest.raises(ValueError):
        plot_argus_phosphenes(pd.DataFrame(), ArgusI())
    # Subjects must all be the same:
    with pytest.raises(ValueError):
        dff = pd.DataFrame([{'subject': 'S1'}, {'subject': 'S2'}])
        plot_argus_phosphenes(dff, ArgusI())
    # Works only for Argus:
    with pytest.raises(TypeError):
        plot_argus_phosphenes(df, AlphaAMS())
Ejemplo n.º 3
0
# ---------------------------------------------
#
# :py:class:`~pulse2percept.stimuli.ImageStimulus` can be used in
# combination with any :py:meth:`~pulse2percept.implants.ProsthesisSystem`.
# We just have to resize the image first so that the number of pixels in the
# image matches the number of electrodes in the implant.
#
# But let's start from the top. The first two steps are to create a model and
# choose an implant:

# Simulate only what we need (14x14 deg sampled at 0.1 deg):
model = p2p.models.ScoreboardModel(xrange=(-7, 7), yrange=(-7, 7), xystep=0.1)
model.build()

from pulse2percept.implants import AlphaAMS
implant = AlphaAMS()

# Show the visual field we're simulating (dashed lines) atop the implant:
model.plot()
implant.plot()

##############################################################################
# Since :py:class:`~pulse2percept.implants.AlphaAMS` is a 2D electrode grid,
# all we need to do is downscale the image to the size of the grid:

implant.stim = logo_gray.resize(implant.shape)

##############################################################################
# This way, the pixels of the image will be assigned to the electrodes in
# row-by-row order (i.e., we don't need to specify the actual electrode names).
#
Ejemplo n.º 4
0
def test_AlphaAMS(ztype, x, y, r):
    # Convert rotation angle to rad
    rot = np.deg2rad(r)
    # Height `h` can either be a float or a list
    if ztype == 'float':
        alpha = AlphaAMS(x=x, y=y, z=-100, rot=rot)
        for e in alpha.values():
            npt.assert_almost_equal(e.z, -100)
    else:
        alpha = AlphaAMS(x=x, y=y, z=np.arange(1600), rot=rot)
        for i, e in enumerate(alpha.values()):
            npt.assert_almost_equal(e.z, i)

    # Slots:
    npt.assert_equal(hasattr(alpha, '__slots__'), True)
    npt.assert_equal(hasattr(alpha, '__dict__'), False)

    # Rotate coordinates of first electrode:
    xy = np.array([-1365, -1365]).T
    R = np.array([np.cos(rot), -np.sin(rot),
                  np.sin(rot), np.cos(rot)]).reshape((2, 2))
    xy = np.matmul(R, xy)
    # Then off-set: Make sure first electrode is placed
    # correctly
    npt.assert_almost_equal(alpha['A1'].x, xy[0] + x)
    npt.assert_almost_equal(alpha['A1'].y, xy[1] + y)

    # Make sure array center is still (x,y)
    y_center = alpha['AN1'].y + (alpha['A40'].y - alpha['AN1'].y) / 2
    npt.assert_almost_equal(y_center, y)
    x_center = alpha['A1'].x + (alpha['AN40'].x - alpha['A1'].x) / 2
    npt.assert_almost_equal(x_center, x)

    # Check radii of electrodes
    for e in ['A1', 'B2', 'C3']:
        npt.assert_equal(alpha[e].r, 15)

    # `h` must have the right dimensions
    with pytest.raises(ValueError):
        AlphaAMS(x=-100, y=10, z=np.arange(12))

    # Indexing must work for both integers and electrode names
    alpha = AlphaAMS()
    # enumerate returns ((0, alpha.items()[0]), (1, alpha.items()[1]), ...)
    # idx = 0, ... 36. name = A1, ... e37. electrode = DiskElectrode(...)
    for idx, (name, electrode) in enumerate(alpha.items()):
        npt.assert_equal(electrode, alpha[idx])
        npt.assert_equal(electrode, alpha[name])
        npt.assert_equal(alpha["unlikely name for an electrode"], None)

    # Right-eye implant:
    xc, yc = 1600, -1600
    alpha_re = AlphaAMS(eye='RE', x=xc, y=yc)
    npt.assert_equal(alpha_re['A40'].x > alpha_re['A1'].x, True)
    npt.assert_almost_equal(alpha_re['A40'].y, alpha_re['A1'].y)

    # Left-eye implant:
    alpha_le = AlphaAMS(eye='LE', x=xc, y=yc)
    npt.assert_equal(alpha_le['A1'].x > alpha_le['AE40'].x, True)
    npt.assert_almost_equal(alpha_le['A40'].y, alpha_le['A1'].y)

    # In both left and right eyes, rotation with positive angle should be
    # counter-clock-wise (CCW): for (x>0,y>0), decreasing x and increasing y
    for eye, el in zip(['LE', 'RE'], ['A1', 'A40']):
        before = AlphaAMS(eye=eye)
        after = AlphaAMS(eye=eye, rot=np.deg2rad(10))
        npt.assert_equal(after[el].x > before[el].x, True)
        npt.assert_equal(after[el].y > before[el].y, True)

    # Invalid eye string:
    with pytest.raises(TypeError):
        AlphaIMS(eye=[1, 2])
    with pytest.raises(ValueError):
        AlphaIMS(eye='left eye')