MaximeVH / EquivalentCircuits.jl

A julia package to either fit the parameters of a specified equivalent electrical circuit to electrochemical impedance data, or to suggest a plausible circuit configuration for a given set of measurements (either through a comparison of circuits from the literature, or through an evolutionary algorithm approach).
MIT License
22 stars 6 forks source link

Performance of impedance.py vs. EquivalentCircuits.jl #5

Closed sindresops closed 1 year ago

sindresops commented 1 year ago

I compared Impedance.py through PyCall.jl to EquivalentCircuits.jl. Naively I thought that Julia would out-perform Python. This quick test showed a ~25X speed advantage for the Python module. Of course the Julia module does not require the initial guess for circuit components.

image

I did a second test with the example data included in the EquivalentCitcuits.jl Readme.md. The time difference is roughly the same.

image

However, if I enter nonsensical values for the initual guess for the Python module, the fit misses the real data completely.

So I suppose the trade-off here is convenience vs. speed?

Is it on the roadmap to include an optional initial guess for the component values?

MaximeVH commented 1 year ago

Thank you for your feedback. An advantage of the optimization methods used in this module is that they should work regardless of initial guesses. I could include an optional argument to allow users to provide initial component parameter guesses. There are also some opportunities to leverage Julia's speed advantage for this package, which I'm considering to implement at a later stage.

sindresops commented 1 year ago

Thanks for the reply! I will be familiarizing myself with the code and see if I can spot some performance traps :)

I think an optional argument for initial guess would be a great addition.

sindresops commented 1 year ago

Definitely the parameter estimation that takes time. BlackBoxOptim.jl dominating the execution time.

image

I tried it with adding an optional initial guess x0. Just modifying the last unit test to take on two different guesses. The last one is the output from the parameter optimization without passing the optional argument.

image

It does not improve by orders of magnitude, but comes a little bit closer to Impedance.py.

Reducing MaxSteps from 170000 to 70000 linearly decreases execution time, without loss in accuracy for the example data in runtests.jl.

image

So I guess this optimization method does not tremendously benefit from the initial guess. I also tried modifying the SearchRange algorithm, but it did not change anything :)

I am curious as to what speed optimizations you were considering?

richinex commented 1 year ago

It is interesting to stumble across this great package. I am definitely gonna try library out since I have just started working with Julia . Meanwhile @sindresops I have built two python packages for multidimensional fitting of impedance spectra (pymultieis and pymultipleis - written in PyTorch and Jax respectively). You might wanna try it out. I also described in the examples how to use it to fit a single spectra.

I had to convert some of my codebase into Julia and noticed some of the concerns you raised. My take is that the optimization libraries in Julia are maybe not as optimized as they should be. I raised this same issue in Julia discourse and worked with some people to optimize my objective function as best as possible. Yet my python code still seems a little faster especially the one written in JAX (we could say that the python is just a wrapper anyway)

Concerning why impedancepy sometimes fail to fit when the initial values are far from optimal, i guess its because Levenberg Macquardt algorithm is sensitive to initial values. Also impedancepy uses scipy curve fit which employs numerical finite differencies. I implemented autodifferentiation and can see that my algorithm fits where impedancepy fails even when you use same initial values for both.

sindresops commented 1 year ago

Thanks for pointing me to those packages @richinex. I will definitely give them a go. I feel like this will end up with me integrating all four.

StefanPofahl commented 1 year ago

It took a while to figure out, how to simulate the impedance, based on the equivalent circuit model and it its parameters. To help others here are the necessary code lines:

circuit_model = "R1-[C2,R3-[C4,R5]]"
circuit_params = parameteroptimisation(circuit_model, file_name_impedance_measurement)
circfunc = EquivalentCircuits.circuitfunction(circuit_model)
impedance_simulated = EquivalentCircuits.simulateimpedance_noiseless(circfunc, circuit_params, frequency_values)
sindresops commented 1 year ago

Thank you for your feedback. An advantage of the optimization methods used in this module is that they should work regardless of initial guesses. I could include an optional argument to allow users to provide initial component parameter guesses. There are also some opportunities to leverage Julia's speed advantage for this package, which I'm considering to implement at a later stage.

PR generated: https://github.com/MaximeVH/EquivalentCircuits.jl/pull/13

It seems to be quite a lot faster if you do a perfect guess (10X). I am sure that might come in handy

sindresops commented 1 year ago

[...] There are also some opportunities to leverage Julia's speed advantage for this package, which I'm considering to implement at a later stage.

You mention speed advantages. If you were to point me in a direction, where would you be looking to optimize first?

MaximeVH commented 1 year ago

The package's circuit_evolution() function uses some string processing (the manipulation and generation of strings), which has been reported as not being a strength of Julia. Suitable alternatives or improvements to this string processing could increase speed, though maybe not by a lot, as the optimization algorithms are the rate-limiting step.

As for the parameter optimization, a new option has been added in this repo (not registered yet) where you can input other optimization algorithms from the BlaxBoxOptim package. You could experiment with different optimization algorithms for the circuits you're fitting and see which trade-off between speed and performance is most suitable.