示例#1
0
def main(args=None):
    """Entry point for ``render``.

    Creates and renders an OpenGL scene, and saves it to a file, according
    to the specified command line arguments (which default to
    ``sys.argv[1:]``).
    """

    if args is None:
        args = sys.argv[1:]

    # Create a GL context
    fslgl.getGLContext(offscreen=True, createApp=True)

    # Initialise FSLeyes and implement hacks
    fsleyes.initialise()
    fsleyesmain.hacksAndWorkarounds()

    # Initialise colour maps module
    fslcm.init()

    # Parse arguments, and
    # configure logging/debugging
    namespace = parseArgs(args)
    fsleyes.configLogging(namespace)

    # Initialise the fsleyes.gl modules
    fslgl.bootstrap(namespace.glversion)

    # Create a description of the scene
    overlayList, displayCtx, sceneOpts = makeDisplayContext(namespace)

    import matplotlib.image as mplimg

    # Render that scene, and save it to file
    bitmap = render(namespace, overlayList, displayCtx, sceneOpts)
    mplimg.imsave(namespace.outfile, bitmap)
示例#2
0
    def init(splash):

        # Parse command line arguments. If the
        # user has asked for help (see above),
        # the parseargs module will raise
        # SystemExit. Hence we make sure the
        # splash screen is shown only after
        # arguments have been parsed.
        try:
            namespace[0] = parseArgs(args)

        # But the wx.App.MainLoop eats SystemExit
        # exceptions for unknown reasons, and
        # and causes the application to exit
        # immediately. This makes testing FSLeyes
        # (e.g. code coverage) impossible. So I'm
        # catching SystemExit here, and then
        # telling the wx.App to exit gracefully.
        except SystemExit as e:
            app.ExitMainLoop()
            exitCode[0] = e.code
            return

        # See FSLeyesSplash.Show
        # for horribleness.
        splash.Show()

        # Configure logging (this has to be done
        # after cli arguments have been parsed,
        # but before initialise is called).
        fsleyes.configLogging(namespace[0])

        # Initialise sub-modules/packages. The
        # buildGui function is passed through
        # as a callback, which gets called when
        # initialisation is complete.
        initialise(splash, namespace[0], buildGui)
示例#3
0
def run_with_fsleyes(func, *args, **kwargs):
    """Create a ``FSLeyesFrame`` and run the given function. """

    from fsl.utils.platform import platform as fslplatform
    import fsleyes_widgets.utils.status as status

    fsleyes.configLogging()

    gc.collect()
    idle.idleLoop.reset()
    idle.idleLoop.allowErrors = True

    propagateRaise = kwargs.pop('propagateRaise', True)
    startingDelay = kwargs.pop('startingDelay', 500)
    finishingDelay = kwargs.pop('finishingDelay', 5)
    callAfterApp = kwargs.pop('callAfterApp', None)

    class State(object):
        pass

    state = State()
    state.result = None
    state.raised = None
    state.frame = None
    state.app = None
    state.dummy = None
    state.panel = None

    glver = os.environ.get('FSLEYES_TEST_GL', '2.1')
    glver = [int(v) for v in glver.split('.')]

    def init():
        fsleyes.initialise()
        props.initGUI()
        colourmaps.init()
        initialised[0] = True
        fslgl.bootstrap(glver)
        wx.CallAfter(run)

    def finish():
        state.frame.Close(askUnsaved=False, askLayout=False)
        state.dummy.Close()
        waitUntilIdle()
        realYield(100)
        state.app.ExitMainLoop()

    def run():

        overlayList = fsloverlay.OverlayList()
        displayCtx = dc.DisplayContext(overlayList)
        state.frame = fslframe.FSLeyesFrame(None, overlayList, displayCtx)

        state.app.SetOverlayListAndDisplayContext(overlayList, displayCtx)
        state.app.SetTopWindow(state.frame)

        state.frame.Show()

        try:
            if func is not None:
                state.result = func(state.frame, overlayList, displayCtx,
                                    *args, **kwargs)

        except Exception as e:
            traceback.print_exc()
            state.raised = e

        finally:
            wx.CallLater(finishingDelay, finish)

    state.app = fslmain.FSLeyesApp()
    state.dummy = wx.Frame(None)
    state.panel = wx.Panel(state.dummy)
    state.sizer = wx.BoxSizer(wx.HORIZONTAL)
    state.sizer.Add(state.panel, flag=wx.EXPAND, proportion=1)
    state.dummy.SetSizer(state.sizer)

    if callAfterApp is not None:
        callAfterApp()

    state.dummy.SetSize((100, 100))
    state.dummy.Layout()
    state.dummy.Show()

    if not initialised[0]:

        # gl already initialised
        if fslplatform.glVersion is not None:
            wx.CallLater(startingDelay, init)
        else:
            wx.CallLater(startingDelay,
                         fslgl.getGLContext,
                         ready=init,
                         raiseErrors=True)
    else:
        wx.CallLater(startingDelay, run)

    with exitMainLoopOnError(state.app) as err:
        state.app.MainLoop()

    status.setTarget(None)
    if status._clearThread is not None:
        status._clearThread.die()
        status._clearThread.clear(0.01)
        status._clearThread.join()
        status._clearThread = None

    raised = state.raised
    result = state.result

    if err[0] is not None:
        raise err[0]

    time.sleep(1)

    if raised and propagateRaise:
        raise raised

    return result
示例#4
0
def main(args=None):
    """*FSLeyes* entry point. Shows a :class:`.FSLeyesSplash` screen, parses
    command line arguments, and shows a :class:`.FSLeyesFrame`. Returns
    an exit code.
    """

    if args is None:
        args = sys.argv[1:]

    # Hack to allow render to
    # be called via fsleyes.main
    if len(args) >= 1 and args[0] == 'render':
        import fsleyes.render as render
        render.main(args[1:])
        sys.exit(0)

    # the fsleyes.initialise function figures
    # out the path to asset files (e.g. cmaps)
    fsleyes.initialise()

    # Hook which allows us to run a jupyter
    # notebook server from a frozen version
    # of FSLeyes
    if len(args) >= 1 and args[0] == 'notebook':
        from fsleyes.actions.notebook import nbmain
        fsleyes.configLogging()
        sys.exit(nbmain(args))

    # initialise colour maps - this must be
    # done before parsing arguments, as if
    # the user asks for help, available
    # colourmaps/luts will be listed.
    colourmaps.init()

    # Function to bootstrap the GUI - keep
    # reading below.
    def initgui():

        # First thing's first. Create a wx.App,
        # and initialise the FSLeyes package.
        app = FSLeyesApp()

        # Create a splash screen frame
        splash = fslsplash.FSLeyesSplash(None)
        return app, splash

    # If it looks like the user is asking for
    # help, or using cliserver to pass arguments
    # to an existing FSLeyes instance, then we
    # parse command line arguments before
    # creating a wx.App and showing the splash
    # screen. This means that FSLeyes help/
    # version information can be retrieved
    # without a display, and hopefully fairly
    # quickly.
    #
    # Otherwise we create the app and splash
    # screen first, so the splash screen gets
    # shown as soon as possible. Arguments
    # will get parsed in the init function below.
    #
    # The argparse.Namespace object is kept in a
    # list so it can be shared between the sub-
    # functions below
    #
    # If argument parsing bombs out, we put the
    # exit code here and return it at the bottom.
    namespace = [None]
    exitCode = [0]

    # user asking for help - parse args first
    if (len(args) > 0) and (args[0]
                            in ('-V', '-h', '-fh', '-cs', '--version',
                                '--help', '--fullhelp', '--cliserver')):
        namespace = [parseArgs(args)]
        app, splash = initgui()

    # otherwise parse arguments on wx.MainLoop
    # below
    else:
        app, splash = initgui()

    # We are going do all processing on the
    # wx.MainLoop, so the GUI can be shown
    # as soon as possible, and because it is
    # difficult to force immediate GUI
    # refreshes when not running on the main
    # loop - this is important for FSLeyes,
    # which displays status updates to the
    # user while it is loading overlays and
    # setting up the interface.
    #
    # All of the work is defined in a series
    # of functions, which are chained together
    # via ugly callbacks, but which are
    # ultimately scheduled and executed on the
    # wx main loop.

    def init(splash):

        # See FSLeyesSplash.Show
        # for horribleness.
        splash.Show()

        # Parse command line arguments if necessary.
        # If arguments are invalid, the parseargs
        # module will raise SystemExit.
        try:
            if namespace[0] is None:

                errmsg = strings.messages['main.parseArgs.error']
                errtitle = strings.titles['main.parseArgs.error']
                with status.reportIfError(errtitle, errmsg, raiseError=True):
                    namespace[0] = parseArgs(args)

        # But the wx.App.MainLoop eats SystemExit
        # exceptions for unknown reasons, and
        # causes the application to exit
        # immediately. This makes testing FSLeyes
        # (e.g. code coverage) impossible. So I'm
        # catching SystemExit here, and then
        # telling the wx.App to exit gracefully.
        except (SystemExit, Exception) as e:
            app.ExitMainLoop()
            exitCode[0] = getattr(e, 'code', 1)
            return

        # Configure logging (this has to be done
        # after cli arguments have been parsed,
        # but before initialise is called).
        fsleyes.configLogging(namespace[0].verbose, namespace[0].noisy)

        # Initialise sub-modules/packages. The
        # buildGui function is passed through
        # as a callback, which gets called when
        # initialisation is complete.
        initialise(splash, namespace[0], buildGui)

    def buildGui():

        # Now the main stuff - create the overlay
        # list and the master display context,
        # and then create the FSLeyesFrame.
        overlayList, displayCtx = makeDisplayContext(namespace[0], splash)
        app.SetOverlayListAndDisplayContext(overlayList, displayCtx)
        frame = makeFrame(namespace[0], displayCtx, overlayList, splash)

        app.SetTopWindow(frame)
        frame.Show()

        # Check that $FSLDIR is set, complain
        # to the user if it isn't
        if not namespace[0].skipfslcheck:
            wx.CallAfter(fslDirWarning, frame)

        # Check for updates. Ignore point
        # releases, otherwise users might
        # get swamped with update notifications.
        if namespace[0].updatecheck:
            import fsleyes.actions.updatecheck as updatecheck
            wx.CallAfter(updatecheck.UpdateCheckAction(),
                         showUpToDateMessage=False,
                         showErrorMessage=False,
                         ignorePoint=False)

        # start notebook server
        if namespace[0].notebookFile is not None:
            namespace[0].notebook = True
            namespace[0].notebookFile = op.abspath(namespace[0].notebookFile)
        if namespace[0].notebook:
            from fsleyes.actions.notebook import NotebookAction
            frame.menuActions[NotebookAction](namespace[0].notebookFile)

        # start CLI server
        if namespace[0].cliserver:
            cliserver.runserver(overlayList, displayCtx)

    # Shut down cleanly on sigint/sigterm.
    # We do this so that any functions
    # registered with atexit will actually
    # get called.
    nsignals = [0]

    def sigHandler(signo, frame):
        log.debug('Signal received - FSLeyes is shutting down...')

        # first signal - try to exit cleanly
        if nsignals[0] == 0:
            nsignals[0] += 1
            exitCode[0] = signo

            # kill any modal windows
            # that are open
            for mdlg in app.modals:
                mdlg.EndModal(wx.ID_CANCEL)

            wx.CallAfter(app.ExitMainLoop)

        # subsequent signals - exit immediately
        else:
            sys.exit(signo)

    signal.signal(signal.SIGINT, sigHandler)
    signal.signal(signal.SIGTERM, sigHandler)

    # Note: If no wx.Frame is created, the
    # wx.MainLoop call will exit immediately,
    # even if we have scheduled something via
    # wx.CallAfter. In this case, we have
    # already created the splash screen, so
    # all is well.
    wx.CallAfter(init, splash)

    # under mac, use appnope to make sure
    # we don't get put to sleep. This is
    # primarily for the jupyter notebook
    # integration - if the user is working
    # with a notebook in the web browser,
    # macos might put FSLeyes to sleep,
    # causing the kernel to become
    # unresponsive.
    try:
        import appnope
        appnope.nope()
    except ImportError:
        pass
    app.MainLoop()
    shutdown()
    return exitCode[0]