Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add convenience functions for multiple Parameter/Variable creation #27

Closed
tBuLi opened this issue May 7, 2015 · 4 comments
Closed

Add convenience functions for multiple Parameter/Variable creation #27

tBuLi opened this issue May 7, 2015 · 4 comments

Comments

@tBuLi
Copy link
Owner

tBuLi commented May 7, 2015

sympy support the following to make multiple symbols:

x, y, z, t = symbols('x y z t')

Although I'm not a big fan of determining the names from the string x y z t, symfit should have something similar:

x, y = variables('x y')
a, b = parameters('a b')

Or if you use the inspect for DRY philosophy used in single Argument definitions,

x, y = variables()
a, b = parameters()

The function itself would be a oneliner in the first case:

def variables(names):
    return [Variable(name=name) for name in names.split()]
@tBuLi tBuLi changed the title Add convenience functions Add convenience functions for multiple Parameter/Variable creation May 7, 2015
@tBuLi
Copy link
Owner Author

tBuLi commented May 7, 2015

After thinking about it some more,

x, y = variables()
a, b = parameters()

looks weird to me. As a user I would not understand how this could possibly work. How does it know I want two? At the very least I would expect to have to give an int as argument for the number I want. So I guess in this case the sympy way is better.

Just as an aside, does the following look good?

(x, y), (a, b) = variables('x y'), parameters('a b')

I think it looks rather nice, and greatly reduces the number of lines needed! I'm thinking of adopting this as the official way of defining vars and params in the docs.

@tBuLi
Copy link
Owner Author

tBuLi commented May 7, 2015

Some other cool ideas for the parameter factory, given that you also should have some method of specifying default values and ranges:

# value will be used as initial guess
a, b = parameters(a=1.0, b=2.0)

# Parameter will be initiated from the dict
c, d = parameters(c={'value': 1.0, 'min': 0.0}, d={'value': 2.0, 'fixed': True})

# The range of the param. Must be increasing in size. Does not support fixed kw nicely
a, e = parameters(a=(0.0, 1.0), e=(0.0, 1.1, 5.0))

# Or, taking even more inspiration from django:
c, d = parameters(c=1.0, c__min=0.0, d=2.0, d__fixed=True)

In all of these the name of the keyword argument is used as the name of the parameter.

I like a combination of the first two most: if only a number is supplied use it as an initial guess, if dict then use that instead. The Djangoesque way is also quite nice, but only if you've seen it before I imagine.

# Next level with djangoesque syntax to keep similarity with variables():
c, d = parameters('c d', c__min=0.0, d__value=2.0, d__fixed=True)

@Eljee
Copy link

Eljee commented May 8, 2015

Though some of these might reduce the numbers of lines of code, the main choice in what would be possible is not convenience rather than readability, since the main idea (as it strikes me) is to produce readable fitting,

With that in mind I would say that the Variables object will have to have more input and not use introspection (?) to find the number of variables to be defined. The string added, like sympy, would be the better option, though the formatting bothers me. I would want to see (compulsory?):

x, y = Variables('x, y') 

Note the added comma, which in PEP8 is the best way to represent multiple variables. It might be however, that some fuzziness should be allowed (PEP8 is a style guide) and I'm not sure on the implementation of that.

As for the Django-esque syntax, though it might be beautiful, I would at the very least not introduce it as standard. If it were possible for Django users and not be the norm, I would not object encourage it.

@tBuLi
Copy link
Owner Author

tBuLi commented May 10, 2015

@Eljee, I agree with you that using a space as a separator is not nice. It's my other big annoyance about the way symbols are defined in sympy. (Apart from the violation of DRY)

After talking to a mutual friend of ours I decided that parameter creation shouldn't be done with optional extra's. Instead, if other default guesses or ranges are required, you can always set them separately:

a, b = parameters('a, b')
a.value = 2.0
a.min, a.max = 0.0, 10.0

I got a bit carried away earlier ;).

I still don't like the violation of DRY though... But it is the least surprising to users, which is important.

@tBuLi tBuLi closed this as completed in 9961baa May 17, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants