Esempio n. 1
0
def fsleyes_embed(parent=None, make_fsleyesframe=True, **kwargs):
    """Initialise FSLeyes and create a :class:`.FSLeyesFrame`, when
    running within another application.
    .. note:: If a ``wx.App`` does not exist, one is created.
    :arg parent: ``wx`` parent object
    :arg make_fsleyesframe: bool, default is True to make a new :class:`.FSLeyesFrame`
    :returns:    A tuple containing:
                    - The :class:`.OverlayList`
                    - The master :class:`.DisplayContext`
                    - The :class:`.FSLeyesFrame` or None if make_fsleyesframe=False
    All other arguments are passed to :meth:`.FSLeyesFrame.__init__`.
    """

    import fsleyes_props as props
    import fsleyes.gl as fslgl
    import fsleyes.frame as fslframe
    import fsleyes.overlay as fsloverlay
    import fsleyes.displaycontext as fsldc

    app = wx.GetApp()
    ownapp = app is None
    if ownapp:
        app = FSLeyesApp()

    fsleyes.initialise()
    colourmaps.init()
    props.initGUI()

    called = [False]
    ret = [None]

    def until():
        return called[0]

    def ready():
        frame = None
        fslgl.bootstrap()

        overlayList = fsloverlay.OverlayList()
        displayCtx = fsldc.DisplayContext(overlayList)
        if make_fsleyesframe:
            frame = fslframe.FSLeyesFrame(parent, overlayList, displayCtx,
                                          **kwargs)

        if ownapp:
            app.SetOverlayListAndDisplayContext(overlayList, displayCtx)
            # Keep a ref to prevent the app from being GC'd
            if make_fsleyesframe:
                frame._embed_app = app

        called[0] = True
        ret[0] = (overlayList, displayCtx, frame)

    fslgl.getGLContext(ready=ready, raiseErrors=True)
    idle.block(10, until=until)

    if ret[0] is None:
        raise RuntimeError('Failed to start FSLeyes')
    return ret[0]
Esempio n. 2
0
def _test_block_until():
    ev = threading.Event()

    def task():
        time.sleep(1)
        ev.set()

    threading.Thread(target=task).start()

    start = time.time()
    idle.block(3, until=ev.is_set)
    end = time.time()

    assert end - start < 3
Esempio n. 3
0
def _test_block():

    called = [False]

    if fslplatform.haveGui:
        import wx
        def idlefunc():
            called[0] = True
        wx.CallLater(1000, idlefunc)

    start = time.time()

    idle.block(2)
    end = time.time()

    # Be relaxed about precision - timing
    # can sometimes be pretty sloppy when
    # running in a docker container.
    assert abs((end - start) - 2) < 0.05

    if fslplatform.haveGui:
        assert called[0]
def _test_editable(ortho, overlayList, displayCtx):

    # must be an image
    # must have one value per voxel (e.g. no RGB)
    # must be displayed as volume, mask, or label
    img = Image(op.join(datadir, '3d'))
    surf = VTKMesh(op.join(datadir, 'mesh_l_thal.vtk'))
    dti = Image(op.join(datadir, 'dti', 'dti_V1'))
    rgb = Bitmap(op.join(datadir, 'test_screenshot_3d.png')).asImage()

    editable = [(img, 'volume'), (img, 'mask'), (img, 'label')]
    notEditable = [(img, 'mip'), (surf, 'mesh'), (dti, 'rgbvector'),
                   (rgb, 'volume')]

    def setprof(profile):
        ortho.profile = profile

    savedprof = [None]

    def saveprof():
        savedprof[0] = ortho.profile

    for ovl, ot in editable:
        idle.idle(setprof, 'edit')
        idle.idle(overlayList.append, ovl, overlayType=ot)
        idle.idle(displayCtx.selectOverlay, ovl)
        idle.block(3)
        idle.idle(saveprof)
        idle.idle(overlayList.clear)
        idle.block(3)
        assert savedprof[0] == 'edit'

    for ovl, ot in notEditable:
        idle.idle(setprof, 'edit')
        idle.idle(overlayList.append, ovl, overlayType=ot)
        idle.idle(displayCtx.selectOverlay, ovl)
        idle.block(3)
        idle.idle(saveprof)
        idle.idle(overlayList.clear)
        idle.block(3)
        assert savedprof[0] == 'view'
def _test_SelectionActions(ortho, overlayList, displayCtx):
    img1 = Image(np.random.randint(1, 65536, (20, 20, 20)))
    img2 = Image(np.random.randint(1, 65536, (20, 20, 20)))
    overlayList[:] = [img1, img2]
    displayCtx.selectOverlay(img1)
    idle.block(2)
    ortho.profile = 'edit'
    idle.block(2)
    profile = ortho.getCurrentProfile()
    profile.mode = 'sel'
    profile.drawMode = False
    idle.block(2)
    ed = profile.editor(img1)

    # clear
    ed.getSelection().addToSelection(np.ones((4, 4, 4)), offset=(8, 8, 8))
    idle.block(2)
    profile.clearSelection()
    assert np.all(ed.getSelection().getSelection() == 0)

    # fill
    ed.getSelection().addToSelection(np.ones((4, 4, 4)), offset=(8, 8, 8))
    idle.block(2)
    profile.fillValue = 999
    profile.fillSelection()
    exp = np.copy(img1[:])
    exp[8:12, 8:12, 8:12] = 999
    assert np.all(img1[:] == exp)

    # erase
    profile.clearSelection()
    ed.getSelection().addToSelection(np.ones((4, 4, 4)), offset=(3, 3, 3))
    idle.block(2)
    profile.eraseValue = -999
    profile.eraseSelection()
    exp = np.copy(img1[:])
    exp[3:7, 3:7, 3:7] = -999
    assert np.all(img1[:] == exp)

    # invert
    ed.getSelection().addToSelection(np.ones((4, 4, 4)), offset=(8, 8, 8))
    idle.block(2)
    profile.invertSelection()
    exp = np.ones(img1.shape)
    exp[8:12, 8:12, 8:12] = 0
    assert np.all(ed.getSelection().getSelection() == exp)

    # copy+paste
    profile.clearSelection()
    ed.getSelection().addToSelection(np.ones((4, 4, 4)), offset=(8, 8, 8))
    displayCtx.selectOverlay(img2)
    profile.copySelection()
    displayCtx.selectOverlay(img1)
    idle.block(2)
    profile.pasteSelection()
    idle.block(2)
    exp = np.copy(img1[:])
    exp[8:12, 8:12, 8:12] = img2[8:12, 8:12, 8:12]
    assert np.all(img1[:] == exp)
def _test_newMask(ortho, overlayList, displayCtx):
    img = Image(np.random.randint(1, 65536, (20, 20, 20)))
    overlayList[:] = [img]
    idle.block(2.5)
    ortho.profile = 'edit'
    idle.block(2.5)

    profile = ortho.getCurrentProfile()

    profile.createMask()
    idle.block(2.5)
    assert len(overlayList) == 2
    mask = overlayList[1]
    assert mask.sameSpace(img)
    assert np.all(mask[:] == 0)
    overlayList.remove(mask)
    idle.block(2.5)

    profile.mode = 'sel'
    idle.block(2.5)
    ed = profile.editor(img)
    ed.getSelection().addToSelection(np.ones((4, 4, 4)), offset=(8, 8, 8))
    idle.block(2.5)
    profile.createMask()
    idle.block(2.5)
    assert len(overlayList) == 2
    mask = overlayList[1]
    assert mask.sameSpace(img)
    exp = np.zeros(mask.shape)
    exp[8:12, 8:12, 8:12] = 1
    assert np.all(mask[:] == exp)
    overlayList[:] = []
    idle.block(2.5)
Esempio n. 7
0
def embed(mkFrame=True, **kwargs):
    """Initialise FSLeyes and create a :class:`.FSLeyesFrame`, when
    running within another application.

    .. note:: In most cases, this function must be called from the
              ``wx.MainLoop``.

    :arg mkFrame: Defaults to ``True``. If ``False``, FSLeyes is
                  initialised, but a :class:`.FSLeyesFrame` is not created.
                  If you set this to ``False``, you must ensure that a
                  ``wx.App`` object exists before calling this function.

    :returns:     A tuple containing:

                   - The :class:`.OverlayList`
                   - The master :class:`.DisplayContext`
                   - The :class:`.FSLeyesFrame` (or ``None``, if
                     ``makeFrame is False``).

    All other arguments are passed to :meth:`.FSLeyesFrame.__init__`.
    """

    import fsleyes_props as props
    import fsleyes.gl as fslgl
    import fsleyes.frame as fslframe
    import fsleyes.overlay as fsloverlay
    import fsleyes.displaycontext as fsldc

    # initialise must be called before
    # a FSLeyesApp gets created, as it
    # tries to access app_icon.png
    fsleyes.initialise()

    app = wx.GetApp()
    ownapp = app is None

    if ownapp and (mkFrame is False):
        raise RuntimeError('If mkFrame is False, you '
                           'must create a wx.App before '
                           'calling fsleyes.main.embed')
    if ownapp:
        app = FSLeyesApp()

    colourmaps.init()
    props.initGUI()

    called = [False]
    ret = [None]

    def until():
        return called[0]

    def ready():
        fslgl.bootstrap()

        overlayList = fsloverlay.OverlayList()
        displayCtx = fsldc.DisplayContext(overlayList)

        if mkFrame:
            frame = fslframe.FSLeyesFrame(None, overlayList, displayCtx,
                                          **kwargs)
        else:
            frame = None

        if ownapp:
            app.SetOverlayListAndDisplayContext(overlayList, displayCtx)
            # Keep a ref to prevent the app from being GC'd
            frame._embed_app = app

        called[0] = True
        ret[0] = (overlayList, displayCtx, frame)

    fslgl.getGLContext(ready=ready, raiseErrors=True)
    idle.block(10, until=until)

    if ret[0] is None:
        raise RuntimeError('Failed to start FSLeyes')
    return ret[0]