Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

simplify the signature for optimization wrappers #196

Closed
wants to merge 11 commits into from

4 participants

Denis Laxalde Charles Harris Joonas Paalasmaa Pauli Virtanen
Denis Laxalde
Collaborator

The idea is to have a more compact signature for the optimization wrappers and to ensure that the return value has always the same type, e.g.:

result = minimize(fun, x0, [jac, hess, constraints], options)

So, changes are:

  • The full_output parameter has been dropped from minimize and minimize_scalar.
  • Only result is returned. The solution can be accessed as result.x.
  • The retall parameter has been moved as an option: options['return_all'].

If this receives a positive feedback, I'll update PR #195 accordingly.

Denis Laxalde
Collaborator

Define and use InfoDict — a subclass of dict with attributes accessors — as suggested by @pv in PR #195.

Denis Laxalde
Collaborator

The default value for options is now None as discussed on the ML.

scipy/optimize/optimize.py
((8 lines not shown))
+ return self[name]
+ except KeyError:
+ raise AttributeError(name)
+
+ __setattr__ = dict.__setitem__
+ __delattr__ = dict.__delitem__
+
+ def __repr__(self):
+ if self.keys():
+ m = max(map(len, self.keys())) + 1
+ else:
+ m = 0
+ return '\n'.join([k.rjust(m) + ': ' + repr(v)
+ for k, v in self.iteritems()])
+
+ def __array__(self):

Isn't this unncecessary magic?

Denis Laxalde Collaborator
dlax added a note
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
scipy/optimize/optimize.py
@@ -60,6 +60,29 @@ def derivative(self, x, *args):
self(x, *args)
return self.jac
+class InfoDict(dict):
+ """ dict subclass with attribute accessors """
Pauli Virtanen Owner
pv added a note

Here you can maybe say instead that this class represents information from optimization solver, and list the common attribute names.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
dlax added some commits
Denis Laxalde dlax ENH: replace InfoDict by Result
`Result` is the only return from optimization wrappers. The solution is
available through the `x` attribute (previously named `solution` in
`InfoDict`).

All tests updated.
65766fc
Denis Laxalde dlax DOC: update optimize tutorial b24d915
Denis Laxalde
Collaborator

I've replaced the InfoDict class by Result which is now the only returned value of minimize and minimize_scalar as discussed on the ML. Solution can be accessed through the x attribute (previously named solution).

Charles Harris
Collaborator

Looks like res.keys() is an easy way to see what is available, but that might be worth a comment in the documentation. Alternatively, you could add a show (show_doc?) method or something along those lines that would provide that information if something more specific to the optimization method is needed. Or is a simple res? in IPython sufficient?

Joonas Paalasmaa jpaalasm commented on the diff
scipy/optimize/optimize.py
((26 lines not shown))
+
+ Note
+ ----
+ This is essential a subclass of dict with attribute accessors.
+ """
+ def __getattr__(self, name):
+ try:
+ return self[name]
+ except KeyError:
+ raise AttributeError(name)
+
+ __setattr__ = dict.__setitem__
+ __delattr__ = dict.__delitem__
+
+ def __repr__(self):
+ if self.keys():
Joonas Paalasmaa
jpaalasm added a note

I would refactor this into

    if len(self):
        max_key_len = max(len(key) for key in self.keys())
        key_format_len = max_key_len + 1
        return '\n'.join([k.rjust(key_format_len) + ': ' + repr(v)
                          for k, v in self.iteritems()])
    else:
        return "Result()"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
scipy/optimize/optimize.py
@@ -60,6 +60,54 @@ def derivative(self, x, *args):
self(x, *args)
return self.jac
+class Result(dict):
+ """ Result of optimization solvers.
Joonas Paalasmaa
jpaalasm added a note

The docstring could be clearer. What about:

Represents the optimization result.

Common attributes
-----------------
x : ndarray
    The optimization solution.
success : bool
    Whether or not the optimizer terminated successfully.
status : int
    Termination status of the optimizer. Its value depends on
    the optimizer. Refer to `message` for details.
message : str
    Description of the cause of termination.
fun, jac, hess : ndarray
    Values of objective function, Jacobian and Hessian (if available).
nfev, njev, nhev: int
    The number of evaluations of the objective functions and of its
    Jacobian and Hessian.
nit: int
    The number of iterations performed by the optimizer.

Note
----
This is essentially a subclass of dict with attribute accessors.
Pauli Virtanen Owner
pv added a note

The section title should also be Attributes so that it interacts well with the documentation machinery. You can mention above that there may also be solver-specific attributes there.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Denis Laxalde
Collaborator

4c1eec3 includes the suggested improvements on the documentation.

Denis Laxalde
Collaborator

Anything else or may I merge this?

Joonas Paalasmaa

I still think that having "Result.__array__" return "self.solution" is a bad idea (shouldn't it be self.x, anyway?). It is unpythonic implicit magic in my opinion.

If you need the solution as an array, why not write "result.x"?

Pauli Virtanen
Owner
pv commented

I think I suggested adding the __array__ method. I agree with Joonas that it's a bit strange, in the sense that asarray(solution) behaves as if solution was an array-like object --- although it is not. The additional convenience gained might not be that great either, so from my side, also removing this piece of magick would be OK.

Denis Laxalde
Collaborator
Denis Laxalde
Collaborator

merged

Denis Laxalde dlax closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Apr 18, 2012
  1. Denis Laxalde

    ENH: drop full_output parameter from minimize

    dlax authored
    `minimize` and underlying `_minimize_METHOD` functions always return a
    tuple containing the solution and the `info` dictionnary.
  2. Denis Laxalde
  3. Denis Laxalde
  4. Denis Laxalde
Commits on Apr 19, 2012
  1. Denis Laxalde

    ENH: define and use InfoDict to store results of optimization wrappers

    dlax authored
    `InfoDict` is a subclass of `dict` with attribute accessor.
  2. Denis Laxalde
Commits on Apr 24, 2012
  1. Denis Laxalde

    ENH: replace InfoDict by Result

    dlax authored
    `Result` is the only return from optimization wrappers. The solution is
    available through the `x` attribute (previously named `solution` in
    `InfoDict`).
    
    All tests updated.
  2. Denis Laxalde

    DOC: update optimize tutorial

    dlax authored
Commits on May 6, 2012
  1. Denis Laxalde
Commits on May 10, 2012
  1. Denis Laxalde
Commits on May 11, 2012
  1. Denis Laxalde
Something went wrong with that request. Please try again.