コード例 #1
0
ファイル: test_nowx.py プロジェクト: shntnu/CellProfiler
    def setUp(self):
        from cellprofiler.preferences import set_headless, set_temporary_directory

        set_headless()
        set_temporary_directory(tempfile.gettempdir())
        self.old_import = __builtin__.__import__
        __builtin__.__import__ = import_all_but_wx
コード例 #2
0
    def setUp(self):
        from cellprofiler.preferences import set_headless, set_temporary_directory

        set_headless()
        set_temporary_directory(tempfile.gettempdir())
        self.old_import = builtins.__import__
        builtins.__import__ = import_all_but_wx
コード例 #3
0
def main(args=None):
    '''Run CellProfiler

    args - command-line arguments, e.g. sys.argv
    '''
    if args is None:
        args = sys.argv
    import cellprofiler.preferences as cpprefs
    cpprefs.set_awt_headless(True)
    exit_code = 0
    switches = ('--work-announce', '--knime-bridge-address')
    if any(
        [any([arg.startswith(switch) for switch in switches])
         for arg in args]):
        #
        # Go headless ASAP
        #
        cpprefs.set_headless()
        for i, arg in enumerate(args):
            if arg == "--ij-plugins-directory" and len(args) > i + 1:
                cpprefs.set_ij_plugin_directory(args[i + 1])
                break
        import cellprofiler.worker
        cellprofiler.worker.aw_parse_args()
        cellprofiler.worker.main()
        sys.exit(exit_code)

    options, args = parse_args(args)
    if options.print_version:
        from cellprofiler.utilities.version import \
            dotted_version, version_string, git_hash, version_number
        print "CellProfiler %s" % dotted_version
        print "Git %s" % git_hash
        print "Version %s" % version_number
        print "Built %s" % version_string.split(" ")[0]
        sys.exit(exit_code)
    #
    # Important to go headless ASAP
    #
    if (not options.show_gui) or options.write_schema_and_exit:
        import cellprofiler.preferences as cpprefs
        cpprefs.set_headless()
        # What's there to do but run if you're running headless?
        # Might want to change later if there's some headless setup
        options.run_pipeline = True

    if options.jvm_heap_size is not None:
        from cellprofiler.preferences import set_jvm_heap_mb
        set_jvm_heap_mb(options.jvm_heap_size, False)
    set_log_level(options)

    if options.print_groups_file is not None:
        print_groups(options.print_groups_file)
        return

    if options.batch_commands_file is not None:
        get_batch_commands(options.batch_commands_file)
        return

    if options.add_message_for_user:
        if len(args) != 3:
            sys.stderr.write("Usage: (for add_message-for-user)\n")
            sys.stderr.write(
                "CellProfiler --add-message-for-user <caption> <message> <pipeline-or-project>\n"
            )
            sys.stderr.write("where:\n")
            sys.stderr.write("    <caption> - the message box caption\n")
            sys.stderr.write(
                "    <message> - the message displayed inside the message box\n"
            )
            sys.stderr.write(
                "    <pipeline-or-project> - the path to the pipeline or project file to modify\n"
            )
            return
        caption = args[0]
        message = args[1]
        path = args[2]

        import h5py
        using_hdf5 = h5py.is_hdf5(path)
        if using_hdf5:
            import cellprofiler.measurement as cpmeas
            m = cpmeas.Measurements(filename=path, mode="r+")
            pipeline_text = m[cpmeas.EXPERIMENT, "Pipeline_Pipeline"]
        else:
            with open(path, "r") as fd:
                pipeline_text = fd.read()
        header, body = pipeline_text.split("\n\n", 1)
        pipeline_text = header + \
                        ("\nMessageForUser:%s|%s\n\n" % (caption, message)) + body
        if using_hdf5:
            m[cpmeas.EXPERIMENT, "Pipeline_Pipeline"] = pipeline_text
            m.close()
        else:
            with open(path, "w") as fd:
                fd.write(pipeline_text)
        print "Message added to %s" % path
        return

    # necessary to prevent matplotlib trying to use Tkinter as its backend.
    # has to be done before CellProfilerApp is imported
    from matplotlib import use as mpluse
    mpluse('WXAgg')

    if options.omero_credentials is not None:
        set_omero_credentials_from_string(options.omero_credentials)
    if options.plugins_directory is not None:
        cpprefs.set_plugin_directory(options.plugins_directory, globally=False)
    if options.ij_plugins_directory is not None:
        cpprefs.set_ij_plugin_directory(options.ij_plugins_directory,
                                        globally=False)
    if options.temp_dir is not None:
        if not os.path.exists(options.temp_dir):
            os.makedirs(options.temp_dir)
        cpprefs.set_temporary_directory(options.temp_dir, globally=False)
    if not options.allow_schema_write:
        cpprefs.set_allow_schema_write(False)
    #
    # After the crucial preferences are established, we can start the VM
    #
    from cellprofiler.utilities.cpjvm import cp_start_vm
    cp_start_vm()
    #
    # Not so crucial preferences...
    #
    if options.image_set_file is not None:
        cpprefs.set_image_set_file(options.image_set_file)
    try:
        # ---------------------------------------
        #
        # Handle command-line tasks that that need to load the modules to run
        #
        if options.output_html:
            from cellprofiler.gui.html.manual import generate_html
            webpage_path = options.output_directory if options.output_directory else None
            generate_html(webpage_path)
            return
        if options.print_measurements:
            print_measurements(options)
            return
        if not hasattr(sys, "frozen") and options.code_statistics:
            print_code_statistics()
            return
        if options.write_schema_and_exit:
            write_schema(options.pipeline_filename)
            return
        #
        # ------------------------------------------
        if options.show_gui:
            import wx
            wx.Log.EnableLogging(False)
            from cellprofiler.gui.app import App
            from cellprofiler.workspace import is_workspace_file

            if options.pipeline_filename:
                if is_workspace_file(options.pipeline_filename):
                    workspace_path = os.path.expanduser(
                        options.pipeline_filename)
                    pipeline_path = None
                else:
                    pipeline_path = os.path.expanduser(
                        options.pipeline_filename)
                    workspace_path = None
            elif options.new_project:
                workspace_path = False
                pipeline_path = None
            else:
                workspace_path = None
                pipeline_path = None

            app = App(0,
                      workspace_path=workspace_path,
                      pipeline_path=pipeline_path)

        if options.data_file is not None:
            cpprefs.set_data_file(os.path.abspath(options.data_file))

        from cellprofiler.utilities.version import version_string, version_number
        logging.root.info("Version: %s / %d" %
                          (version_string, version_number))

        if options.run_pipeline and not options.pipeline_filename:
            raise ValueError("You must specify a pipeline filename to run")

        if options.output_directory:
            if not os.path.exists(options.output_directory):
                os.makedirs(options.output_directory)
            cpprefs.set_default_output_directory(options.output_directory)

        if options.image_directory:
            cpprefs.set_default_image_directory(options.image_directory)

        if options.show_gui:
            if options.run_pipeline:
                app.frame.pipeline_controller.do_analyze_images()
            app.MainLoop()
            return

        elif options.run_pipeline:
            run_pipeline_headless(options, args)
    except Exception, e:
        logging.root.fatal("Uncaught exception in CellProfiler.py",
                           exc_info=True)
        exit_code = -1
コード例 #4
0
def main(args):
    '''Run CellProfiler

    args - command-line arguments, e.g. sys.argv
    '''
    if any([arg.startswith('--work-announce') for arg in args]):
        #
        # Go headless ASAP
        #
        import cellprofiler.preferences as cpprefs
        cpprefs.set_headless()
        for i, arg in enumerate(args):
            if arg == "--ij-plugins-directory" and len(args) > i+1:
                cpprefs.set_ij_plugin_directory(args[i+1])
                break
        import cellprofiler.analysis_worker
        cellprofiler.analysis_worker.aw_parse_args()
        cellprofiler.analysis_worker.main()
        sys.exit(0)
        
    options, args = parse_args(args)
    if options.jvm_heap_size != None:
        from cellprofiler.preferences import set_jvm_heap_mb
        set_jvm_heap_mb(options.jvm_heap_size, False)
    set_log_level(options)
    
    if not hasattr(sys, "frozen") and options.code_statistics:
        print_code_statistics()
        return
    
    if options.print_groups_file is not None:
        print_groups(options.print_groups_file)
        return
    
    if options.batch_commands_file is not None:
        get_batch_commands(options.batch_commands_file)
        return
        
    if options.run_ilastik:
        run_ilastik()
        return
    
    if options.add_message_for_user:
        if len(args) != 3:
            sys.stderr.write("Usage: (for add_message-for-user)\n")
            sys.stderr.write("CellProfiler --add-message-for-user <caption> <message> <pipeline-or-project>\n")
            sys.stderr.write("where:\n")
            sys.stderr.write("    <caption> - the message box caption\n")
            sys.stderr.write("    <message> - the message displayed inside the message box\n")
            sys.stderr.write("    <pipeline-or-project> - the path to the pipeline or project file to modify\n")
            return
        caption = args[0]
        message = args[1]
        path = args[2]
        
        import h5py
        using_hdf5 = h5py.is_hdf5(path)
        if using_hdf5:
            import cellprofiler.measurements as cpmeas
            m = cpmeas.Measurements(
                filename = path, mode="r+")
            pipeline_text = m[cpmeas.EXPERIMENT, "Pipeline_Pipeline"]
        else:
            with open(path, "r") as fd:
                pipeline_text = fd.read()
        header, body = pipeline_text.split("\n\n", 1)
        pipeline_text = header + \
            ("\nMessageForUser:%s|%s\n\n" % (caption, message)) + body
        if using_hdf5:
            m[cpmeas.EXPERIMENT, "Pipeline_Pipeline"] = pipeline_text
            m.close()
        else:
            with open(path, "w") as fd:
                fd.write(pipeline_text)
        print "Message added to %s" % path
        return
    
    # necessary to prevent matplotlib trying to use Tkinter as its backend.
    # has to be done before CellProfilerApp is imported
    from matplotlib import use as mpluse
    mpluse('WXAgg')
    
    if (not hasattr(sys, 'frozen')) and options.fetch_external_dependencies:
        import external_dependencies
        external_dependencies.fetch_external_dependencies(options.overwrite_external_dependencies)
    
    if (not hasattr(sys, 'frozen')) and options.build_extensions:
        build_extensions()
        if options.build_and_exit:
            return
    
    if options.output_html:
        from cellprofiler.gui.html.manual import generate_html
        webpage_path = options.output_directory if options.output_directory else None
        generate_html(webpage_path)
        return
    if options.print_measurements:
        print_measurements(options)
        return
    if options.omero_credentials is not None:
        set_omero_credentials_from_string(options.omero_credentials)
    try:
        if options.show_gui:
            import wx
            wx.Log.EnableLogging(False)
            from cellprofiler.cellprofilerapp import CellProfilerApp
            from cellprofiler.workspace import is_workspace_file
            show_splashbox = (options.pipeline_filename is None and
                              (not options.new_project) and
                              options.show_splashbox)
            
            if options.pipeline_filename:
                if is_workspace_file(options.pipeline_filename):
                    workspace_path = os.path.expanduser(options.pipeline_filename)
                    pipeline_path = None
                else:
                    pipeline_path = os.path.expanduser(options.pipeline_filename)
                    workspace_path = None
            elif options.new_project:
                workspace_path = False
                pipeline_path = None
            else:
                workspace_path = None
                pipeline_path = None
            App = CellProfilerApp(
                0, 
                check_for_new_version = (options.pipeline_filename is None),
                show_splashbox = show_splashbox,
                workspace_path = workspace_path,
                pipeline_path = pipeline_path)
    
        #
        # Important to go headless ASAP
        #
        # cellprofiler.preferences can't be imported before we have a chance
        # to initialize the wx app.
        #
        import cellprofiler.preferences as cpprefs
        if not options.show_gui:
            cpprefs.set_headless()
            # What's there to do but run if you're running headless?
            # Might want to change later if there's some headless setup 
            options.run_pipeline = True
    
            
        if options.plugins_directory is not None:
            cpprefs.set_plugin_directory(options.plugins_directory)
        if options.ij_plugins_directory is not None:
            cpprefs.set_ij_plugin_directory(options.ij_plugins_directory)
        if options.temp_dir is not None:
            if not os.path.exists(options.temp_dir):
                os.makedirs(options.temp_dir)
            cpprefs.set_temporary_directory(options.temp_dir)
        if options.data_file is not None:
            cpprefs.set_data_file(os.path.abspath(options.data_file))
        if options.image_set_file is not None:
            cpprefs.set_image_set_file(options.image_set_file, False)
            
        from cellprofiler.utilities.version import version_string, version_number
        logging.root.info("Version: %s / %d" % (version_string, version_number))
    
        if options.run_pipeline and not options.pipeline_filename:
            raise ValueError("You must specify a pipeline filename to run")
    
        if options.output_directory:
            if not os.path.exists(options.output_directory):
                os.makedirs(options.output_directory)
            cpprefs.set_default_output_directory(options.output_directory)
        
        if options.image_directory:
            cpprefs.set_default_image_directory(options.image_directory)
    
        if options.show_gui:
            if options.run_pipeline:
                App.frame.pipeline_controller.do_analyze_images()
            App.MainLoop()
            return
        
        elif options.run_pipeline:
            run_pipeline_headless(options, args)
    except Exception, e:
        logging.root.fatal("Uncaught exception in CellProfiler.py", exc_info=True)
        raise
コード例 #5
0
def main(args):
    '''Run CellProfiler

    args - command-line arguments, e.g. sys.argv
    '''
    if any([arg.startswith('--work-announce') for arg in args]):
        #
        # Go headless ASAP
        #
        import cellprofiler.preferences as cpprefs
        cpprefs.set_headless()
        import cellprofiler.analysis_worker
        cellprofiler.analysis_worker.main()
        sys.exit(0)

    options, args = parse_args(args)
    set_log_level(options)

    if not hasattr(sys, "frozen") and options.code_statistics:
        print_code_statistics()
        return

    if options.print_groups_file is not None:
        print_groups(options.print_groups_file)
        return

    if options.batch_commands_file is not None:
        get_batch_commands(options.batch_commands_file)
        return

    if options.run_ilastik:
        run_ilastik()
        return

    # necessary to prevent matplotlib trying to use Tkinter as its backend.
    # has to be done before CellProfilerApp is imported
    from matplotlib import use as mpluse
    mpluse('WXAgg')

    if (not hasattr(sys, 'frozen')) and options.fetch_external_dependencies:
        import external_dependencies
        external_dependencies.fetch_external_dependencies(
            options.overwrite_external_dependencies)

    if (not hasattr(sys, 'frozen')) and options.build_extensions:
        build_extensions()
        if options.build_and_exit:
            return

    if options.output_html:
        from cellprofiler.gui.html.manual import generate_html
        webpage_path = options.output_directory if options.output_directory else None
        generate_html(webpage_path)
        return
    if options.print_measurements:
        print_measurements(options)
        return

    try:
        if options.show_gui:
            import wx
            wx.Log.EnableLogging(False)
            from cellprofiler.cellprofilerapp import CellProfilerApp
            show_splashbox = (options.pipeline_filename is None
                              and options.workspace_filename is None
                              and (not options.new_workspace)
                              and options.show_splashbox)
            if options.workspace_filename:
                workspace_path = os.path.expanduser(options.workspace_filename)
            elif options.new_workspace:
                workspace_path = False
            else:
                workspace_path = None
            App = CellProfilerApp(
                0,
                check_for_new_version=(options.pipeline_filename is None),
                show_splashbox=show_splashbox,
                workspace_path=workspace_path)

        #
        # Important to go headless ASAP
        #
        # cellprofiler.preferences can't be imported before we have a chance
        # to initialize the wx app.
        #
        import cellprofiler.preferences as cpprefs
        if not options.show_gui:
            cpprefs.set_headless()
            # What's there to do but run if you're running headless?
            # Might want to change later if there's some headless setup
            options.run_pipeline = True

        if options.plugins_directory is not None:
            cpprefs.set_plugin_directory(options.plugins_directory)
        if options.ij_plugins_directory is not None:
            cpprefs.set_ij_plugin_directory(options.ij_plugins_directory)
        if options.temp_dir is not None:
            cpprefs.set_temporary_directory(options.temp_dir)
        if options.data_file is not None:
            cpprefs.set_data_file(os.path.abspath(options.data_file))
        if options.image_set_file is not None:
            cpprefs.set_image_set_file(options.image_set_file, False)

        from cellprofiler.utilities.version import version_string, version_number
        logging.root.info("Version: %s / %d" %
                          (version_string, version_number))

        if options.run_pipeline and not options.pipeline_filename:
            raise ValueError("You must specify a pipeline filename to run")

        if options.output_directory:
            cpprefs.set_default_output_directory(options.output_directory)

        if options.image_directory:
            cpprefs.set_default_image_directory(options.image_directory)

        if options.show_gui:
            import cellprofiler.gui.cpframe as cpgframe
            if options.pipeline_filename:
                pipeline_path = os.path.expanduser(options.pipeline_filename)
                try:
                    App.frame.pipeline.load(pipeline_path)
                    if options.run_pipeline:
                        App.frame.Command(cpgframe.ID_FILE_ANALYZE_IMAGES)
                except:
                    import wx
                    wx.MessageBox(
                        'CellProfiler was unable to load the pipeline file, "%s"'
                        % options.pipeline_filename,
                        "Error loading pipeline",
                        style=wx.OK | wx.ICON_ERROR)
                    logging.root.error("Unable to load pipeline",
                                       exc_info=True)
            App.MainLoop()
            return

        elif options.run_pipeline:
            run_pipeline_headless(options, args)
    except Exception, e:
        logging.root.fatal("Uncaught exception in CellProfiler.py",
                           exc_info=True)
        raise
コード例 #6
0
ファイル: preferencesdlg.py プロジェクト: refack/CellProfiler
 def get_preferences(self):
     '''Get the list of preferences.
     
     Each row in the list has the following form:
     Title - the text that appears to the right of the edit box
     get_function - retrieves the persistent preference value
     set_function - sets the preference value
     display - If this is a list, it represents the valid choices.
               If it is "dirbrowse", put a directory browse button
               to the right of the edit box.
     '''
     cmaps = list(matplotlib.cm.datad.keys())
     cmaps.sort()
     return [["Default Input Folder",
              cpprefs.get_default_image_directory,
              cpprefs.set_default_image_directory,
              DIRBROWSE, cphelp.DEFAULT_IMAGE_FOLDER_HELP],
             ["Default Output Folder",
              cpprefs.get_default_output_directory,
              cpprefs.set_default_output_directory,
              DIRBROWSE, cphelp.DEFAULT_OUTPUT_FOLDER_HELP],
             [ "Title font", 
               self.get_title_font, 
               self.set_title_font, 
               FONT, cphelp.TITLE_FONT_HELP],
             ["Table font", 
              self.get_table_font, 
              self.set_table_font, 
              FONT, cphelp.TABLE_FONT_HELP],
             ["Default colormap", 
              cpprefs.get_default_colormap, 
              cpprefs.set_default_colormap, 
              cmaps, cphelp.DEFAULT_COLORMAP_HELP],
             ["Window background", 
              cpprefs.get_background_color, 
              cpprefs.set_background_color, 
              COLOR, cphelp.WINDOW_BACKGROUND_HELP],
             ["Error color",
              cpprefs.get_error_color,
              cpprefs.set_error_color,
              COLOR, cphelp.ERROR_COLOR_HELP],
             ["Primary outline color",
              cpprefs.get_primary_outline_color,
              cpprefs.set_primary_outline_color,
              COLOR, cphelp.PRIMARY_OUTLINE_COLOR_HELP],
             ["Secondary outline color",
              cpprefs.get_secondary_outline_color,
              cpprefs.set_secondary_outline_color,
              COLOR, cphelp.SECONDARY_OUTLINE_COLOR_HELP],
             ["Tertiary outline color",
              cpprefs.get_tertiary_outline_color,
              cpprefs.set_tertiary_outline_color,
              COLOR, cphelp.TERTIARY_OUTLINE_COLOR_HELP],
             ["Interpolation mode",
              cpprefs.get_interpolation_mode,
              cpprefs.set_interpolation_mode,
              [cpprefs.IM_NEAREST, cpprefs.IM_BILINEAR, cpprefs.IM_BICUBIC],
              cphelp.INTERPOLATION_MODE_HELP],
             ["Intensity normalization",
              cpprefs.get_intensity_mode,
              cpprefs.set_intensity_mode,
              [cpprefs.INTENSITY_MODE_RAW, cpprefs.INTENSITY_MODE_NORMAL,
               cpprefs.INTENSITY_MODE_LOG],
              cphelp.INTENSITY_MODE_HELP],
             ["CellProfiler plugins directory",
              cpprefs.get_plugin_directory,
              cpprefs.set_plugin_directory,
              DIRBROWSE, cphelp.PLUGINS_DIRECTORY_HELP],
             ["ImageJ plugins directory",
              cpprefs.get_ij_plugin_directory,
              cpprefs.set_ij_plugin_directory,
              DIRBROWSE, cphelp.IJ_PLUGINS_DIRECTORY_HELP],
             ["Check for updates", 
              cpprefs.get_check_new_versions, 
              cpprefs.set_check_new_versions, 
              CHOICE, cphelp.CHECK_FOR_UPDATES_HELP],
             ["Display welcome text on startup", 
              cpprefs.get_startup_blurb, 
              cpprefs.set_startup_blurb, 
              CHOICE, cphelp.SHOW_STARTUP_BLURB_HELP],
             ["Warn if Java runtime environment not present",
              cpprefs.get_report_jvm_error,
              cpprefs.set_report_jvm_error,
              CHOICE, cphelp.REPORT_JVM_ERROR_HELP],
             ['Show the "Analysis complete" message at the end of a run',
              cpprefs.get_show_analysis_complete_dlg,
              cpprefs.set_show_analysis_complete_dlg,
              CHOICE, cphelp.SHOW_ANALYSIS_COMPLETE_HELP],
             ['Show the "Exiting test mode" message',
              cpprefs.get_show_exiting_test_mode_dlg,
              cpprefs.set_show_exiting_test_mode_dlg,
              CHOICE, cphelp.SHOW_EXITING_TEST_MODE_HELP],
             ['Warn if images are different sizes',
              cpprefs.get_show_report_bad_sizes_dlg,
              cpprefs.set_show_report_bad_sizes_dlg,
              CHOICE, cphelp.SHOW_REPORT_BAD_SIZES_DLG_HELP],
             ['Show the sampling menu',
              cpprefs.get_show_sampling,
              cpprefs.set_show_sampling,
              CHOICE, """<p>Show the sampling menu </p>
              <p><i>Note that CellProfiler must be restarted after setting.</i></p>
              <p>The sampling menu is an interplace for Paramorama, a plugin for an interactive visualization 
              program for exploring the parameter space of image analysis algorithms.
              will generate a text file, which specifies: (1) all unique combinations of 
              the sampled parameter values; (2) the mapping from each combination of parameter values to 
              one or more output images; and (3) the actual output images.</p>
              <p>More information on how to use the plugin can be found 
              <a href="http://www.comp.leeds.ac.uk/scsajp/applications/paramorama2/">here</a>.</p>
              <p><b>References</b>
              <ul>
              <li>Visualization of parameter space for image analysis. Pretorius AJ, Bray MA, Carpenter AE 
              and Ruddle RA. (2011) IEEE Transactions on Visualization and Computer Graphics, 17(12), 2402-2411.</li>
              </ul>"""],
             ['Warn if a pipeline was saved in an old version of CellProfiler',
              cpprefs.get_warn_about_old_pipeline,
              cpprefs.set_warn_about_old_pipeline,
              CHOICE,
              cphelp.WARN_ABOUT_OLD_PIPELINES_HELP],
             ['Use more figure space',
              cpprefs.get_use_more_figure_space,
              cpprefs.set_use_more_figure_space,
              CHOICE,
              cphelp.USE_MORE_FIGURE_SPACE_HELP
             ],
             ['Maximum number of workers',
              cpprefs.get_max_workers,
              cpprefs.set_max_workers,
              IntegerPreference(1, cpprefs.default_max_workers() * 4),
              cphelp.MAX_WORKERS_HELP],
             ['Temporary folder',
              cpprefs.get_temporary_directory,
              (lambda x:cpprefs.set_temporary_directory(x, globally=True)),
              DIRBROWSE,
              cphelp.TEMP_DIR_HELP],
             ['Maximum memory for Java (MB)',
              cpprefs.get_jvm_heap_mb,
              cpprefs.set_jvm_heap_mb,
              IntegerPreference(128, 64000),
              cphelp.JVM_HEAP_HELP],
             ['Save pipeline and/or file list in addition to project',
              cpprefs.get_save_pipeline_with_project,
              cpprefs.set_save_pipeline_with_project,
              cpprefs.SPP_ALL,
              cphelp.SAVE_PIPELINE_WITH_PROJECT_HELP],
             ['Folder name regular expression guesses',
              cpprefs.get_pathname_re_guess_file,
              cpprefs.set_pathname_re_guess_file,
              FILEBROWSE,
              cphelp.FOLDER_RE_GUESS_HELP],
             ['File name regular expression guesses',
              cpprefs.get_filename_re_guess_file,
              cpprefs.set_filename_re_guess_file,
              FILEBROWSE,
              cphelp.FILE_RE_GUESS_HELP],
             ['Batch Profiler URL',
              cpprefs.get_batchprofiler_url,
              cpprefs.set_batchprofiler_url,
              None,
              cphelp.BATCHPROFILER_URL_HELP]
             ]
コード例 #7
0
def main(args=None):
    '''Run CellProfiler

    args - command-line arguments, e.g. sys.argv
    '''
    if args is None:
        args = sys.argv
    import cellprofiler.preferences as cpprefs
    cpprefs.set_awt_headless(True)
    exit_code = 0
    switches = ('--work-announce', '--knime-bridge-address')
    if any([any([arg.startswith(switch) for switch in switches])
            for arg in args]):
        #
        # Go headless ASAP
        #
        cpprefs.set_headless()
        for i, arg in enumerate(args):
            if arg == "--ij-plugins-directory" and len(args) > i + 1:
                cpprefs.set_ij_plugin_directory(args[i + 1])
                break
        import cellprofiler.analysis_worker
        cellprofiler.analysis_worker.aw_parse_args()
        cellprofiler.analysis_worker.main()
        sys.exit(exit_code)

    options, args = parse_args(args)
    if options.print_version:
        from cellprofiler.utilities.version import \
            dotted_version, version_string, git_hash, version_number
        print "CellProfiler %s" % dotted_version
        print "Git %s" % git_hash
        print "Version %s" % version_number
        print "Built %s" % version_string.split(" ")[0]
        sys.exit(exit_code)
    #
    # Important to go headless ASAP
    #
    if (not options.show_gui) or options.write_schema_and_exit:
        import cellprofiler.preferences as cpprefs
        cpprefs.set_headless()
        # What's there to do but run if you're running headless?
        # Might want to change later if there's some headless setup
        options.run_pipeline = True

    if options.jvm_heap_size is not None:
        from cellprofiler.preferences import set_jvm_heap_mb
        set_jvm_heap_mb(options.jvm_heap_size, False)
    set_log_level(options)

    if options.print_groups_file is not None:
        print_groups(options.print_groups_file)
        return

    if options.batch_commands_file is not None:
        get_batch_commands(options.batch_commands_file)
        return

    if options.run_ilastik:
        run_ilastik()
        return

    if options.add_message_for_user:
        if len(args) != 3:
            sys.stderr.write("Usage: (for add_message-for-user)\n")
            sys.stderr.write("CellProfiler --add-message-for-user <caption> <message> <pipeline-or-project>\n")
            sys.stderr.write("where:\n")
            sys.stderr.write("    <caption> - the message box caption\n")
            sys.stderr.write("    <message> - the message displayed inside the message box\n")
            sys.stderr.write("    <pipeline-or-project> - the path to the pipeline or project file to modify\n")
            return
        caption = args[0]
        message = args[1]
        path = args[2]

        import h5py
        using_hdf5 = h5py.is_hdf5(path)
        if using_hdf5:
            import cellprofiler.measurements as cpmeas
            m = cpmeas.Measurements(
                    filename=path, mode="r+")
            pipeline_text = m[cpmeas.EXPERIMENT, "Pipeline_Pipeline"]
        else:
            with open(path, "r") as fd:
                pipeline_text = fd.read()
        header, body = pipeline_text.split("\n\n", 1)
        pipeline_text = header + \
                        ("\nMessageForUser:%s|%s\n\n" % (caption, message)) + body
        if using_hdf5:
            m[cpmeas.EXPERIMENT, "Pipeline_Pipeline"] = pipeline_text
            m.close()
        else:
            with open(path, "w") as fd:
                fd.write(pipeline_text)
        print "Message added to %s" % path
        return

    # necessary to prevent matplotlib trying to use Tkinter as its backend.
    # has to be done before CellProfilerApp is imported
    from matplotlib import use as mpluse
    mpluse('WXAgg')

    if options.omero_credentials is not None:
        set_omero_credentials_from_string(options.omero_credentials)
    if options.plugins_directory is not None:
        cpprefs.set_plugin_directory(options.plugins_directory,
                                     globally=False)
    if options.ij_plugins_directory is not None:
        cpprefs.set_ij_plugin_directory(options.ij_plugins_directory,
                                        globally=False)
    if options.temp_dir is not None:
        if not os.path.exists(options.temp_dir):
            os.makedirs(options.temp_dir)
        cpprefs.set_temporary_directory(options.temp_dir, globally=False)
    if not options.allow_schema_write:
        cpprefs.set_allow_schema_write(False)
    #
    # After the crucial preferences are established, we can start the VM
    #
    from cellprofiler.utilities.cpjvm import cp_start_vm
    cp_start_vm()
    #
    # Not so crucial preferences...
    #
    if options.image_set_file is not None:
        cpprefs.set_image_set_file(options.image_set_file)
    try:
        # ---------------------------------------
        #
        # Handle command-line tasks that that need to load the modules to run
        #
        if options.output_html:
            from cellprofiler.gui.html.manual import generate_html
            webpage_path = options.output_directory if options.output_directory else None
            generate_html(webpage_path)
            return
        if options.print_measurements:
            print_measurements(options)
            return
        if not hasattr(sys, "frozen") and options.code_statistics:
            print_code_statistics()
            return
        if options.write_schema_and_exit:
            write_schema(options.pipeline_filename)
            return
        #
        # ------------------------------------------
        if options.show_gui:
            import wx
            wx.Log.EnableLogging(False)
            from cellprofiler.gui.app import App
            from cellprofiler.workspace import is_workspace_file

            if options.pipeline_filename:
                if is_workspace_file(options.pipeline_filename):
                    workspace_path = os.path.expanduser(options.pipeline_filename)
                    pipeline_path = None
                else:
                    pipeline_path = os.path.expanduser(options.pipeline_filename)
                    workspace_path = None
            elif options.new_project:
                workspace_path = False
                pipeline_path = None
            else:
                workspace_path = None
                pipeline_path = None

            app = App(0, workspace_path=workspace_path, pipeline_path=pipeline_path)

        if options.data_file is not None:
            cpprefs.set_data_file(os.path.abspath(options.data_file))

        from cellprofiler.utilities.version import version_string, version_number
        logging.root.info("Version: %s / %d" % (version_string, version_number))

        if options.run_pipeline and not options.pipeline_filename:
            raise ValueError("You must specify a pipeline filename to run")

        if options.output_directory:
            if not os.path.exists(options.output_directory):
                os.makedirs(options.output_directory)
            cpprefs.set_default_output_directory(options.output_directory)

        if options.image_directory:
            cpprefs.set_default_image_directory(options.image_directory)

        if options.show_gui:
            if options.run_pipeline:
                app.frame.pipeline_controller.do_analyze_images()
            app.MainLoop()
            return

        elif options.run_pipeline:
            run_pipeline_headless(options, args)
    except Exception, e:
        logging.root.fatal("Uncaught exception in CellProfiler.py", exc_info=True)
        exit_code = -1
コード例 #8
0
 def get_preferences(self):
     '''Get the list of preferences.
     
     Each row in the list has the following form:
     Title - the text that appears to the right of the edit box
     get_function - retrieves the persistent preference value
     set_function - sets the preference value
     display - If this is a list, it represents the valid choices.
               If it is "dirbrowse", put a directory browse button
               to the right of the edit box.
     '''
     cmaps = list(matplotlib.cm.datad.keys())
     cmaps.sort()
     return [
         [
             "Default Input Folder", cpprefs.get_default_image_directory,
             cpprefs.set_default_image_directory, DIRBROWSE,
             cphelp.DEFAULT_IMAGE_FOLDER_HELP
         ],
         [
             "Default Output Folder", cpprefs.get_default_output_directory,
             cpprefs.set_default_output_directory, DIRBROWSE,
             cphelp.DEFAULT_OUTPUT_FOLDER_HELP
         ],
         [
             "Title font", self.get_title_font, self.set_title_font, FONT,
             cphelp.TITLE_FONT_HELP
         ],
         [
             "Table font", self.get_table_font, self.set_table_font, FONT,
             cphelp.TABLE_FONT_HELP
         ],
         [
             "Default colormap", cpprefs.get_default_colormap,
             cpprefs.set_default_colormap, cmaps,
             cphelp.DEFAULT_COLORMAP_HELP
         ],
         [
             "Window background", cpprefs.get_background_color,
             cpprefs.set_background_color, COLOR,
             cphelp.WINDOW_BACKGROUND_HELP
         ],
         [
             "Error color", cpprefs.get_error_color,
             cpprefs.set_error_color, COLOR, cphelp.ERROR_COLOR_HELP
         ],
         [
             "Primary outline color", cpprefs.get_primary_outline_color,
             cpprefs.set_primary_outline_color, COLOR,
             cphelp.PRIMARY_OUTLINE_COLOR_HELP
         ],
         [
             "Secondary outline color", cpprefs.get_secondary_outline_color,
             cpprefs.set_secondary_outline_color, COLOR,
             cphelp.SECONDARY_OUTLINE_COLOR_HELP
         ],
         [
             "Tertiary outline color", cpprefs.get_tertiary_outline_color,
             cpprefs.set_tertiary_outline_color, COLOR,
             cphelp.TERTIARY_OUTLINE_COLOR_HELP
         ],
         [
             "Interpolation mode", cpprefs.get_interpolation_mode,
             cpprefs.set_interpolation_mode,
             [cpprefs.IM_NEAREST, cpprefs.IM_BILINEAR,
              cpprefs.IM_BICUBIC], cphelp.INTERPOLATION_MODE_HELP
         ],
         [
             "Intensity normalization", cpprefs.get_intensity_mode,
             cpprefs.set_intensity_mode,
             [
                 cpprefs.INTENSITY_MODE_RAW, cpprefs.INTENSITY_MODE_NORMAL,
                 cpprefs.INTENSITY_MODE_LOG
             ], cphelp.INTENSITY_MODE_HELP
         ],
         [
             "CellProfiler plugins directory", cpprefs.get_plugin_directory,
             cpprefs.set_plugin_directory, DIRBROWSE,
             cphelp.PLUGINS_DIRECTORY_HELP
         ],
         [
             "ImageJ plugins directory", cpprefs.get_ij_plugin_directory,
             cpprefs.set_ij_plugin_directory, DIRBROWSE,
             cphelp.IJ_PLUGINS_DIRECTORY_HELP
         ],
         [
             "Check for updates", cpprefs.get_check_new_versions,
             cpprefs.set_check_new_versions, CHOICE,
             cphelp.CHECK_FOR_UPDATES_HELP
         ],
         [
             "Display welcome text on startup", cpprefs.get_startup_blurb,
             cpprefs.set_startup_blurb, CHOICE,
             cphelp.SHOW_STARTUP_BLURB_HELP
         ],
         [
             "Warn if Java runtime environment not present",
             cpprefs.get_report_jvm_error, cpprefs.set_report_jvm_error,
             CHOICE, cphelp.REPORT_JVM_ERROR_HELP
         ],
         [
             'Show the "Analysis complete" message at the end of a run',
             cpprefs.get_show_analysis_complete_dlg,
             cpprefs.set_show_analysis_complete_dlg, CHOICE,
             cphelp.SHOW_ANALYSIS_COMPLETE_HELP
         ],
         [
             'Show the "Exiting test mode" message',
             cpprefs.get_show_exiting_test_mode_dlg,
             cpprefs.set_show_exiting_test_mode_dlg, CHOICE,
             cphelp.SHOW_EXITING_TEST_MODE_HELP
         ],
         [
             'Warn if images are different sizes',
             cpprefs.get_show_report_bad_sizes_dlg,
             cpprefs.set_show_report_bad_sizes_dlg, CHOICE,
             cphelp.SHOW_REPORT_BAD_SIZES_DLG_HELP
         ],
         [
             'Show the sampling menu', cpprefs.get_show_sampling,
             cpprefs.set_show_sampling, CHOICE,
             """<p>Show the sampling menu </p>
              <p><i>Note that CellProfiler must be restarted after setting.</i></p>
              <p>The sampling menu is an interplace for Paramorama, a plugin for an interactive visualization 
              program for exploring the parameter space of image analysis algorithms.
              will generate a text file, which specifies: (1) all unique combinations of 
              the sampled parameter values; (2) the mapping from each combination of parameter values to 
              one or more output images; and (3) the actual output images.</p>
              <p>More information on how to use the plugin can be found 
              <a href="http://www.comp.leeds.ac.uk/scsajp/applications/paramorama2/">here</a>.</p>
              <p><b>References</b>
              <ul>
              <li>Visualization of parameter space for image analysis. Pretorius AJ, Bray MA, Carpenter AE 
              and Ruddle RA. (2011) IEEE Transactions on Visualization and Computer Graphics, 17(12), 2402-2411.</li>
              </ul>"""
         ],
         [
             'Warn if a pipeline was saved in an old version of CellProfiler',
             cpprefs.get_warn_about_old_pipeline,
             cpprefs.set_warn_about_old_pipeline, CHOICE,
             cphelp.WARN_ABOUT_OLD_PIPELINES_HELP
         ],
         [
             'Use more figure space', cpprefs.get_use_more_figure_space,
             cpprefs.set_use_more_figure_space, CHOICE,
             cphelp.USE_MORE_FIGURE_SPACE_HELP
         ],
         [
             'Maximum number of workers', cpprefs.get_max_workers,
             cpprefs.set_max_workers,
             IntegerPreference(1,
                               cpprefs.default_max_workers() * 4),
             cphelp.MAX_WORKERS_HELP
         ],
         [
             'Temporary folder', cpprefs.get_temporary_directory,
             (lambda x: cpprefs.set_temporary_directory(x, globally=True)),
             DIRBROWSE, cphelp.TEMP_DIR_HELP
         ],
         [
             'Maximum memory for Java (MB)', cpprefs.get_jvm_heap_mb,
             cpprefs.set_jvm_heap_mb,
             IntegerPreference(128, 64000), cphelp.JVM_HEAP_HELP
         ],
         [
             'Save pipeline and/or file list in addition to project',
             cpprefs.get_save_pipeline_with_project,
             cpprefs.set_save_pipeline_with_project, cpprefs.SPP_ALL,
             cphelp.SAVE_PIPELINE_WITH_PROJECT_HELP
         ],
         [
             'Folder name regular expression guesses',
             cpprefs.get_pathname_re_guess_file,
             cpprefs.set_pathname_re_guess_file, FILEBROWSE,
             cphelp.FOLDER_RE_GUESS_HELP
         ],
         [
             'File name regular expression guesses',
             cpprefs.get_filename_re_guess_file,
             cpprefs.set_filename_re_guess_file, FILEBROWSE,
             cphelp.FILE_RE_GUESS_HELP
         ],
         [
             'Batch Profiler URL', cpprefs.get_batchprofiler_url,
             cpprefs.set_batchprofiler_url, None,
             cphelp.BATCHPROFILER_URL_HELP
         ]
     ]
コード例 #9
0
ファイル: CellProfiler.py プロジェクト: JDWarner/CellProfiler
def main(args):
    '''Run CellProfiler

    args - command-line arguments, e.g. sys.argv
    '''
    if any([arg.startswith('--work-announce') for arg in args]):
        #
        # Go headless ASAP
        #
        import cellprofiler.preferences as cpprefs
        cpprefs.set_headless()
        import cellprofiler.analysis_worker
        cellprofiler.analysis_worker.main()
        sys.exit(0)
        
    options, args = parse_args(args)
    set_log_level(options)
    
    if not hasattr(sys, "frozen") and options.code_statistics:
        print_code_statistics()
        return
    
    if options.print_groups_file is not None:
        print_groups(options.print_groups_file)
        return
    
    if options.batch_commands_file is not None:
        get_batch_commands(options.batch_commands_file)
        return
        
    if options.run_ilastik:
        run_ilastik()
        return
    
    # necessary to prevent matplotlib trying to use Tkinter as its backend.
    # has to be done before CellProfilerApp is imported
    from matplotlib import use as mpluse
    mpluse('WXAgg')
    
    if (not hasattr(sys, 'frozen')) and options.fetch_external_dependencies:
        import external_dependencies
        external_dependencies.fetch_external_dependencies(options.overwrite_external_dependencies)
    
    if (not hasattr(sys, 'frozen')) and options.build_extensions:
        build_extensions()
        if options.build_and_exit:
            return
    
    if options.output_html:
        from cellprofiler.gui.html.manual import generate_html
        webpage_path = options.output_directory if options.output_directory else None
        generate_html(webpage_path)
        return
    if options.print_measurements:
        print_measurements(options)
        return
    
    try:
        if options.show_gui:
            import wx
            wx.Log.EnableLogging(False)
            from cellprofiler.cellprofilerapp import CellProfilerApp
            show_splashbox = (options.pipeline_filename is None and
                              options.workspace_filename is None and
                              (not options.new_workspace) and
                              options.show_splashbox)
            if options.workspace_filename:
                workspace_path = os.path.expanduser(options.workspace_filename)
            elif options.new_workspace:
                workspace_path = False
            else:
                workspace_path = None
            App = CellProfilerApp(
                0, 
                check_for_new_version = (options.pipeline_filename is None),
                show_splashbox = show_splashbox,
                workspace_path = workspace_path)
    
        #
        # Important to go headless ASAP
        #
        # cellprofiler.preferences can't be imported before we have a chance
        # to initialize the wx app.
        #
        import cellprofiler.preferences as cpprefs
        if not options.show_gui:
            cpprefs.set_headless()
            # What's there to do but run if you're running headless?
            # Might want to change later if there's some headless setup 
            options.run_pipeline = True
    
            
        if options.plugins_directory is not None:
            cpprefs.set_plugin_directory(options.plugins_directory)
        if options.ij_plugins_directory is not None:
            cpprefs.set_ij_plugin_directory(options.ij_plugins_directory)
        if options.temp_dir is not None:
            cpprefs.set_temporary_directory(options.temp_dir)
        if options.data_file is not None:
            cpprefs.set_data_file(os.path.abspath(options.data_file))
        if options.image_set_file is not None:
            cpprefs.set_image_set_file(options.image_set_file, False)
            
        from cellprofiler.utilities.version import version_string, version_number
        logging.root.info("Version: %s / %d" % (version_string, version_number))
    
        if options.run_pipeline and not options.pipeline_filename:
            raise ValueError("You must specify a pipeline filename to run")
    
        if options.output_directory:
            cpprefs.set_default_output_directory(options.output_directory)
        
        if options.image_directory:
            cpprefs.set_default_image_directory(options.image_directory)
    
        if options.show_gui:
            import cellprofiler.gui.cpframe as cpgframe
            if options.pipeline_filename:
                pipeline_path = os.path.expanduser(options.pipeline_filename)
                try:
                    App.frame.pipeline.load(pipeline_path)
                    if options.run_pipeline:
                        App.frame.Command(cpgframe.ID_FILE_ANALYZE_IMAGES)
                except:
                    import wx
                    wx.MessageBox(
                        'CellProfiler was unable to load the pipeline file, "%s"' %
                        options.pipeline_filename, "Error loading pipeline",
                        style = wx.OK | wx.ICON_ERROR)
                    logging.root.error("Unable to load pipeline", exc_info=True)
            App.MainLoop()
            return
        
        elif options.run_pipeline:
            run_pipeline_headless(options, args)
    except Exception, e:
        logging.root.fatal("Uncaught exception in CellProfiler.py", exc_info=True)
        raise