def optimise(args, **kwargs):
    """
    Optimise the model parameters.
    """

    expected_output_files = kwargs.pop("expected_output_files", None)
    if not expected_output_files:
        expected_output_files = ["optimised.pkl"]
        if args.plotting:
            expected_output_files.extend([
                "projection-estimate.{}".format(args.plot_format),
                "projection-optimised.{}".format(args.plot_format)
            ])
        
    # Estimate the model parameters, unless they are already specified.
    model = sick.models.Model(args.model)
    initial_theta = model._configuration.get("initial_theta", {})
    if len(set(model.parameters).difference(initial_theta)) == 0:
        model, data, metadata = _pre_solving(args, expected_output_files)

    else:
        model, data, metadata, initial_theta = estimate(args, 
            expected_output_files=expected_output_files, __return_result=True)

    try:
        theta, chisq, dof, model_fluxes = model.optimise(data, 
            initial_theta=initial_theta, full_output=True, debug=args.debug)

    except:
        logger.exception("Failed to optimise model parameters")
        raise

    metadata["optimised"] = {
        "theta": theta,
        "chi_sq": chisq,
        "dof": dof,
        "r_chi_sq": chisq/dof
    }

    logger.info("Optimised model parameters are:")
    _announce_theta(theta)
    logger.info("With a chi-sq value of {0:.1f} (reduced {1:.1f}; DOF {2:.1f})"\
        .format(chisq, chisq/dof, dof))

    if args.plotting:
        fig = sick.plot.spectrum(data, model_flux=model_fluxes)
        filename = _prefix(args, "projection-optimised.{}".format(
            args.plot_format))
        fig.savefig(filename)
        logger.info("Created figure {}".format(filename))

    if kwargs.pop("__return_result", False):
        return (model, data, metadata, theta)

    # Write the results to file.
    _write_output(_prefix(args, "optimised.pkl"), metadata)
    return None
def optimise ( args , ** kwargs ) : expected_output_files = kwargs . pop ( "expected_output_files" , None ) if not expected_output_files : expected_output_files = [ "optimised.pkl" ] if args . plotting : expected_output_files . extend ( [ "projection-estimate.{}" . format ( args . plot_format ) , "projection-optimised.{}" . format ( args . plot_format ) ] ) model = sick . models . Model ( args . model ) initial_theta = model . _configuration . get ( "initial_theta" , { } ) if len ( set ( model . parameters ) . difference ( initial_theta ) ) == 0 : model , data , metadata = _pre_solving ( args , expected_output_files ) else : model , data , metadata , initial_theta = estimate ( args , expected_output_files = expected_output_files , __return_result = True ) try : theta , chisq , dof , model_fluxes = model . optimise ( data , initial_theta = initial_theta , full_output = True , debug = args . debug ) except : logger . exception ( "Failed to optimise model parameters" ) raise metadata [ "optimised" ] = { "theta" : theta , "chi_sq" : chisq , "dof" : dof , "r_chi_sq" : chisq / dof } logger . info ( "Optimised model parameters are:" ) _announce_theta ( theta ) logger . info ( "With a chi-sq value of {0:.1f} (reduced {1:.1f}; DOF {2:.1f})" . format ( chisq , chisq / dof , dof ) ) if args . plotting : fig = sick . plot . spectrum ( data , model_flux = model_fluxes ) filename = _prefix ( args , "projection-optimised.{}" . format ( args . plot_format ) ) fig . savefig ( filename ) logger . info ( "Created figure {}" . format ( filename ) ) if kwargs . pop ( "__return_result" , False ) : return ( model , data , metadata , theta ) _write_output ( _prefix ( args , "optimised.pkl" ) , metadata ) return None
Optimise the model parameters.