Skip to content

ComputationalBioLab/Unfit-Python

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Unfit Python

unfit-logo

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.

made-with-python language-c++ Open Source Love svg1 contributions welcome license-GPLv3.0

Table of Contents

Getting Started


  1. Download & install Code::Blocks (http://www.codeblocks.org/).

Note: if you are on Windows, choose the version with GCC (mingw)

  1. Download & install Boost-Python.

On MacOS, do the following:

brew install boost-python
  1. Download Unfit-Python, e.g:
git clone https://github.com/NicholasCF/Unfit-Python
  1. 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.

  1. Start Code::Blocks and open the Unfit project file (Unfit.cbp) in the Unfit folder
  2. In the Unfit project, choose the Library option, then Build
  3. Open the Unfit-Python project file (UnfitPython.cbp) in the Unfit-Python folder with Code::Blocks
  4. Build the Unfit-Python project with the Debug option.
  5. Change the output file name, which is a *.dylib in MacOS, to libUnfitPython.so
  6. Execute the main.py Python file to run an example.

Usage


Specifying model

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

Solving problem

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.

Running Tests


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.

Development Environment


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.

Contributing


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:

  1. (Just definitions)
  2. 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
  3. You promise you are not going to contribute something, then patent it and come after us for patent infringement
  4. You are allowed to contribute what you are contributing
  5. You are submitting something you have written, not something that belongs to someone else
  6. 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.

License


This project is licensed under the GNU General Public License version 3.0 - see the LICENSE.md file for details