Support method chaining in compute methods

Issue #147 resolved
Matthew Spellings created an issue

Currently, most freud objects require a couple of lines of initialization before users get to the actual results that they care about. For example,

rdf = freud.density.RDF(5, .1)
rdf.compute(box, positions)
pyplot.plot(rdf.getR(), rdf.getRDF())

hex = freud.order.HexOrderParameter(6)
hex.compute(box, positions)
pyplot.hist(numpy.abs(hex.getPsi()))

At least the line overhead of the constructor could be avoided by utilizing method chaining in compute functions, as in:

rdf = freud.density.RDF(5, .1).compute(box, positions)
pyplot.plot(rdf.getR(), rdf.getRDF())

psi = freud.order.HexOrderParameter(6).compute(box, positions).getPsi()
pyplot.hist(numpy.abs(psi))

Since (as far as I know, at least) nothing is using the return value of compute methods, the normal API would still be usable as-is. This may be particularly helpful for the neighbor list-based API proposed in PR #99.

Tagging @joaander and @harperic for opinions.

Comments (9)

  1. Joshua Anderson

    Sounds like a great idea!

    Combined with my proposal for property based outputs (#143) and the API can be really succinct.

  2. Matthew Spellings reporter

    As a heads-up, this should be completed as far as I recall, unless we are missing it on something that was being developed in parallel with the neighbor list revamp.

  3. Bradley Dice

    @mspells I was going to review all the current code to be sure it's there across all modules before marking it as resolved. Thanks for the heads-up!

  4. Log in to comment