2490    """get_options(args):
 2491    Process our command-line options.
 2492 
 2493    args is a list of options & positional arguments.
 2494    
 2495    default_options, if given, is an instance of optparse.Values created by a
 2496    previous call to this function.
 2497    """
 2498    usage = 'usage: %prog [options] <cellml file or URI>'
 2499    parser = optparse.OptionParser(version="%%prog %s" % __version__,
 2500                                   usage=usage)
 2501    parser.add_option('-q', '--quiet', action='store_true', default=False,
 2502                      help="don't show warning messages, only errors")
 2503    
 2504    parser.add_option('-T', '--translate',
 2505                      dest='translate', action='store_true',
 2506                      default=True,
 2507                      help="output computer code [default]")
 2508    parser.add_option('-C', '--output-cellml',
 2509                      dest='translate', action='store_false',
 2510                      help="output an annotated CellML file instead of translating, on stdout unless -o specified")
 2511    translators = sorted(CellMLTranslator.translators)
 2512    parser.add_option('-t', '--translate-type',
 2513                      type='choice', choices=translators,
 2514                      default='Nektar', metavar='TYPE',
 2515                      help="the type of code to output [default: %default].  "
 2516                      "Choices: " + str(translators))
 2517    parser.add_option('-o', dest='outfilename', metavar='OUTFILE',
 2518                      help="write program code to OUTFILE [default action is to use the input filename with a different extension]")
 2519    
 2520    parser.add_option('--config-file',
 2521                      action='append', default=[],
 2522                      help="pathname of configuration file")
 2523    parser.add_option('-A', '--fully-automatic',
 2524                      action='store_true', default=False,
 2525                      help="if human intervention is required, fail noisily")
 2526    parser.add_option('--assume-valid',
 2527                      action='store_true', default=False,
 2528                      help="skip some of the model validation checks")
 2529    parser.add_option('--warn-on-unit-conversions',
 2530                      action='store_true', default=False,
 2531                      help="generate a warning if unit conversions are required")
 2532    parser.add_option('--Wu', '--warn-on-units-errors',
 2533                      action='store_true', default=False,
 2534                      dest='warn_on_units_errors',
 2535                      help="give a warning instead of an error for dimensional inconsistencies")
 2536    parser.add_option('-V', '--transmembrane-potential', default=None, metavar='POT_VAR',
 2537                      help="POT_VAR is the full name of the variable representing the transmembrane potential."
 2538                      "  If not specified here, the configuration file will be used, which is the prefered method."
 2539                      "  Defaults to 'membrane,V'.")
 2540    parser.add_option('-d', '--debug', action='store_true', default=False,
 2541                      help="output debug info to stderr")
 2542    parser.add_option('-D', '--debug-source', action='append',
 2543                      help="only show debug info from the specified part of the code."
 2544                      "  This option may appear more than once to select multiple sources.  Implies -d.")
 2545    parser.add_option('--profile', action='store_true', default=False,
 2546                      help="turn on profiling of PyCml")
 2547    
 2548    
 2549    
 2550    
 2551    
 2552    
 2553    
 2554    group = optparse.OptionGroup(parser, 'Transformations',
 2555                                 "These options control which transformations (typically optimisations) are applied in the generated code")
 2556    group.add_option('-l', '--lookup-tables',
 2557                     dest='lut', action='store_true', default=False,
 2558                     help="perform a lookup table analysis")
 2559    group.add_option('-p', '--pe', '--partial-evaluation',
 2560                     dest='pe', action='store_true', default=False,
 2561                     help="partially evaluate the model")
 2562    group.add_option('-u', '--units-conversions',
 2563                     action='store_true', default=False,
 2564                     help="add explicit units conversion mathematics")
 2565    group.add_option('-j', '--maple-output',
 2566                     metavar='FILENAME', default=None,
 2567                     help="file containing output from a Maple script generated using -J.  The generated"
 2568                     " code/CellML will then contain a symbolic Jacobian as computed by Maple.")
 2569    group.add_option('-J', '--do-jacobian-analysis',
 2570                     action='store_true', default=False,
 2571                     help="generate code to perform Jacobian analysis for backward Euler & CVODE; implies -t Maple")
 2572    group.add_option('--backward-euler',
 2573                     action='store_true', default=False,
 2574                     help="generate a specialised cell model that solves itself using a decoupled"
 2575                     " backward Euler method.  Not compatible with --rush-larsen.  Implies -t Chaste."
 2576                     "  Requires -j.")
 2577    group.add_option('--rush-larsen',
 2578                     action='store_true', default=False,
 2579                     help="use the Rush-Larsen method to solve Hodgkin-Huxley style gating variable"
 2580                     " equations.  Not compatible with --backward-euler.  Implies -t Chaste.")
 2581    group.add_option('--grl1',
 2582                     action='store_true', default=False,
 2583                     help="use the GRL1 method to solve Hodgkin-Huxley style gating variable"
 2584                     " equations.  Not compatible with the backward Euler transformation."
 2585                     " Implies -t Chaste.")
 2586    group.add_option('--grl2',
 2587                     action='store_true', default=False,
 2588                     help="use the GRL2 method to solve Hodgkin-Huxley style gating variable"
 2589                     " equations.  Not compatible with the backward Euler transformation."
 2590                     " Implies -t Chaste.")
 2591    parser.add_option_group(group)
 2592    
 2593    group = optparse.OptionGroup(parser, 'Generated code options')
 2594    group.add_option('-c', '--class-name', default=None,
 2595                     help="explicitly set the name of the generated class")
 2596    group.add_option('-a', '--augment-class-name',
 2597                     dest='augment_class_name', action='store_true',
 2598                     default=False,
 2599                     help="alter the class name to show what transformations are used")
 2600    group.add_option('--no-timestamp',
 2601                     action='store_true', default=False,
 2602                     help="don't add a timestamp comment to generated files")
 2603    parser.add_option_group(group)
 2604    
 2605    group = optparse.OptionGroup(parser, 'Maple options', "Options specific to Maple code output")
 2606    group.add_option('--dont-omit-constants',
 2607                     dest='omit_constants', action='store_false', default=True,
 2608                     help="when generating Maple code, include assignments of constants")
 2609    group.add_option('--compute-partial-jacobian', dest='compute_full_jacobian',
 2610                     action='store_false', default=True,
 2611                     help="make generated Maple code compute a Jacobian specific to a Newton solve"
 2612                     " of the nonlinear portion of the ODE system, rather than the full system Jacobian")
 2613    parser.add_option_group(group)
 2614    
 2615    group = optparse.OptionGroup(parser, 'Python options', "Options specific to Python code output")
 2616    group.add_option('--no-numba', dest='numba', default=True, action='store_false',
 2617                     help="turn off using Numba to optimise code on-the-fly")
 2618    parser.add_option_group(group)
 2619    
 2620    group = optparse.OptionGroup(parser, 'Chaste options', "Options specific to Chaste code output")
 2621    group.add_option('-y', '--dll', '--dynamically-loadable',
 2622                     dest='dynamically_loadable',
 2623                     action='store_true', default=False,
 2624                     help="add code to allow the model to be compiled to a shared library and dynamically loaded"
 2625                     " (only works if -t Chaste is used)")
 2626    group.add_option('--use-chaste-stimulus',
 2627                     action='store_true', default=False,
 2628                     help="when generating Chaste code, use Chaste's stimulus rather than that defined in the model")
 2629    group.add_option('--no-use-chaste-stimulus', dest='use_chaste_stimulus',
 2630                     action='store_false',
 2631                     help="when generating Chaste code, use the model's stimulus, not Chaste's")
 2632    group.add_option('-i', '--convert-interfaces',
 2633                     action='store_true', default=False,
 2634                     help="perform units conversions at interfaces to Chaste (only works if -t Chaste is used)")
 2635    group.add_option('--use-i-ionic-regexp', dest='use_i_ionic_regexp',
 2636                     action='store_true', default=False,
 2637                     help="determine ionic currents from the regexp specified in the config file"
 2638                     " rather than analysing the voltage derivative equation")
 2639    group.add_option('--include-dt-in-tables',
 2640                     action='store_true', default=False,
 2641                     help="[experimental] allow timestep to be included in lookup tables.  By default"
 2642                     " uses the timestep of the first cell created.  Requires support from external"
 2643                     " code if timestep changes.  Only really useful for backward Euler cells.")
 2644    group.add_option('-m', '--use-modifiers',
 2645                     action='store_true', default=False,
 2646                     help="[experimental] add modifier functions for certain"
 2647                     " metadata-annotated variables for use in sensitivity analysis (only works if -t Chaste is used)")
 2648    group.add_option('--use-data-clamp',
 2649                     action='store_true', default=False,
 2650                     help="[experimental] generate a data clamp subclass of CVODE cells"
 2651                     " which contains data clamp currents for fitting experimental data (only works if -t CVODE is used)")
 2652    group.add_option('--expose-annotated-variables',
 2653                     action='store_true', default=False,
 2654                     help="expose all oxmeta-annotated variables for access via the GetAnyVariable functionality")
 2655    group.add_option('--expose-all-variables',
 2656                     action='store_true', default=False,
 2657                     help="expose all variables for access via the GetAnyVariable functionality")
 2658    parser.add_option_group(group)
 2659    
 2660    group = optparse.OptionGroup(parser, 'Functional Curation options', "Options specific to use by Functional Curation")
 2661    def protocol_callback(option, opt_str, value, parser):
 2662        """
 2663        Protocols don't always produce normal cardiac cell models.
 2664        However, we want to allow a later option to override these changes.
 2665        """
 2666        parser.values.protocol = value
 2667        parser.values.convert_interfaces = False
 2668        parser.values.use_chaste_stimulus = False
 2669    group.add_option('--protocol',
 2670                     action='callback', callback=protocol_callback, type='string', nargs=1,
 2671                     help="specify a simulation protocol to apply to the model prior to translation")
 2672    group.add_option('--protocol-options', action='store', type='string',
 2673                     help="extra options for the protocol")
 2674    group.add_option('--expose-named-parameters',
 2675                     action='store_true', default=False,
 2676                     help="expose all constant variables with 'name' annotations for access as model parameters")
 2677    parser.add_option_group(group)
 2678    
 2679    group = optparse.OptionGroup(parser, 'Lookup tables options', "Options specific to the lookup tables optimisation")
 2680    lookup_type_choices = ['entry-below', 'nearest-neighbour', 'linear-interpolation']
 2681    group.add_option('--lookup-type', choices=lookup_type_choices,
 2682                     default='linear-interpolation',
 2683                     help="the type of table lookup to perform [default: %default]."
 2684                     " Choices: " + str(lookup_type_choices))
 2685    group.add_option('--no-separate-lut-class', dest='separate_lut_class',
 2686                     action='store_false', default=True,
 2687                     help="don't put lookup tables in a separate class")
 2688    group.add_option('--row-lookup-method',
 2689                     action='store_true', default=True,
 2690                     help="add and use a method to look up a whole row of a table")
 2691    group.add_option('--no-row-lookup-method', dest='row_lookup_method',
 2692                     action='store_false',
 2693                     help="don't add and use a method to look up a whole row of a table")
 2694    group.add_option('--combine-commutative-tables',
 2695                     action='store_true', default=False,
 2696                     help="optimise a special corner case to reduce the number of tables."
 2697                     " See documentation for details.")
 2698    group.add_option('--lt-index-uses-floor',
 2699                     action='store_true', default=False,
 2700                     help="use floor() to calculate LT indices, instead of just casting")
 2701    group.add_option('--constrain-table-indices',
 2702                     action='store_true', default=False,
 2703                     help="constrain lookup table index variables to remain within the bounds specified,"
 2704                     " rather than throwing an exception if they go outside the bounds")
 2705    group.add_option('--no-check-lt-bounds', dest='check_lt_bounds',
 2706                     action='store_false', default=True,
 2707                     help="[unsafe] don't check for LT indexes going outside the table bounds")
 2708    parser.add_option_group(group)
 2709    
 2710    group = optparse.OptionGroup(parser, 'Partial evaluation options', "Options specific to the partial evaluation optimisation")
 2711    group.add_option('--pe-convert-power',
 2712                     action='store_true', default=False,
 2713                     help="convert pow(x,3) to x*x*x; similarly for powers 2 & 4.")
 2714    group.add_option('--no-partial-pe-commutative', dest='partial_pe_commutative',
 2715                     action='store_false', default=True,
 2716                     help="don't combine static operands of dynamic commutative associative applys")
 2717    group.add_option('--no-pe-instantiate-tables', dest='pe_instantiate_tables',
 2718                     action='store_false', default=True,
 2719                     help="don't instantiate definitions that will be tables regardless of usage")
 2720    parser.add_option_group(group)
 2721 
 2722    options, args = parser.parse_args(args, values=default_options)
 2723    if len(args) != 1:
 2724        parser.error("exactly one input CellML file must be specified")
 2725 
 2726    
 2727    if options.debug_source:
 2728        options.debug = True
 2729    if options.do_jacobian_analysis:
 2730        options.translate_type = 'Maple'
 2731        options.maple_output = False
 2732        options.rush_larsen = False
 2733        options.backward_euler = False
 2734    if options.backward_euler:
 2735        if not options.maple_output:
 2736            parser.error("Backward Euler code generation requires maple output (-j)")
 2737        options.rush_larsen = False
 2738        options.grl1 = False
 2739        options.grl2 = False
 2740    if options.rush_larsen or options.backward_euler or options.grl1 or options.grl2:
 2741        options.translate_type = 'Chaste'
 2742    if options.use_data_clamp and not options.translate_type=='CVODE':
 2743        parser.error("Data clamp option '--use-data-clamp' also requires CVODE ('-t CVODE'). If you are calling this via ConvertCellModel use '--cvode-data-clamp'.")
 2744    
 2745    if options.numba:
 2746        try:
 2747            import numba
 2748        except:
 2749            options.numba = False
 2750 
 2751    return options, args[0]
 2752 
 2753 
def get_options(args, default_options=None)