This allows use of Unfit through Python. Solve cost function problems with 6 different optimizers. Aims to smoothly interface the Unfit from C++ to Python.
- Download & install Code::Blocks (http://www.codeblocks.org/).
Note: if you are on Windows, choose the version with GCC (mingw)
- Download & install Boost-Python.
On MacOS, do the following:
brew install boost-python
- Download Unfit-Python, e.g:
git clone https://github.com/NicholasCF/Unfit-Python
- Download Unfit in the same directory, e.g:
git clone https://github.com/ComputationalBioLab/Unfit.git
The last known commit to work is 637521d on the master branch.
- Start Code::Blocks and open the Unfit project file (Unfit.cbp) in the Unfit folder
- In the Unfit project, choose the Library option, then Build
- Open the Unfit-Python project file (UnfitPython.cbp) in the Unfit-Python folder with Code::Blocks
- Build the Unfit-Python project with the Debug option.
- Change the output file name, which is a *.dylib in MacOS, to libUnfitPython.so
- Execute the main.py Python file to run an example.
Derive a class from the GenericCostFunction class in libUnfitPython module, as demonstrated in mycostfunction.py, changing the model in the following segment as needed:
def __call__(self, x):
r = libUnfitPython.std_vector_double()
r[:] = self.y[:]
for i, ti in enumerate(self.t):
model = x[0] + x[1] * ti #change this
r[i] -= model
return r
Do the following syntax, and the results will be stored in initial_guess:
initial_guess = libUnfitPython.std_vector_double()
initial_guess[:] = [guess1, guess2, ...] #do not put initial_guess = [guess1, guess2, ...]
cf = CostFunction(x_data, y_data) #your cost function
#x_data and y_data are the data to be put into cost function
op = libUnfitPython.Optimizer() #replace 'Optimizer' with any of the optimizer names
op.FindMin(cf, initial_guess)
Additionally, the SetBounds function needs to be called, using either of the following ways, for the optimizers except NelderMead and LevenbergMarquardt before calling FindMin:
op.bounds.SetBounds(index, lower_bound, upper_bound)
op.bounds.SetBounds(lower_bounds, upper_bounds) #The bounds are std_vector_double objects
For further modifications of the optimization process, check the options and bounds members of the optimizers, which is documented in the original Unfit.
There are two sets of tests, in the TestExamples and Unittest folders, made with Python unittest. These tests can be run to check whether the build is successful.
The TestExamples tests serves as an example for users to follow in using Unfit, and check that access of Unfit through Python does not affect results. To run all tests, exectute the TestExamples module. To run individual tests, execute the relevant modules.
The Unittest tests ensures that the functions and objects behave in the same way as their C++ counterparts, and no compatibility issues arise between C++ and Python. The code can also be viewed as an example on how to call the functions, especially the Bounds and Options classes. Similarly, to run all tests, exectute the Unittest module. To run individual tests, execute the relevant modules.
This project is built with:
- C++11
- CodeBlocks
- Python 2.7
- Python unittest
- Boost.Python
Development is mainly done in MacOS environment, but this is also ocassionally tested in Windows and Linux environment.
We are serious about this being around for a long time and hopefully becoming useful to more and more people. After much study, we have decided to require contributing license agreements (CLAs) from our contributors to keep our work open. Our CLA is adapted directly from the one used by the Apache Foundation, and can be found here. If you are interested in how a CLA protects you as well as us, a good explanation is given here. In case you can't be bothered to read the whole thing, here is our summary of the key points of the CLA:
- (Just definitions)
- You still own what you contribute, and can do whatever you want with it, but you allow us to use it forever, so we don't have to worry that you will change your mind and take it back
- You promise you are not going to contribute something, then patent it and come after us for patent infringement
- You are allowed to contribute what you are contributing
- You are submitting something you have written, not something that belongs to someone else
- You don't have to provide support unless you choose to do so
If you just want to use our work, you don't have to sign anything - just use it. If you want to contribute please sign, scan and email it back to us. You only have to do this once to be able to contribute to any of our projects.
This project is licensed under the GNU General Public License version 3.0 - see the LICENSE.md file for details