Skip to content

Commit

Permalink
Python 3.6 migration, adding setup.py, and Travis integration (#1)
Browse files Browse the repository at this point in the history
* Move everything in a subfolder and add a setup.pu

* Use some pycharm magik

* Fixing install_requires and switch to find_packages

* Move init to pymb

* Adding an __all__ to stuff

* Add scikit-sparse as deps for cholesky

* Fixing imports, and added scikit-sparse to reqs

* Some more formatting changes

* Change file( to open(

* Change xrange to range

* iteritems to items

* Adding native rpy2 calls to pick up R version

* Adding a Dockerfile to set up conda environment

* Adding a travis yml

* Use Miniconda docker

* Add exec permish to bin 01

* Add a print statement in 01

* Adding miniconda in travis

* Only keep py 3.6

* Remove -- frmo apt-get

* Move apt packages to block

* Add binaries for dependency installation

* remove intel packages

* Why is stage not continuing..

* Move conda installation to 01 shell

* Move all the non R conda installs to use conda-forge

* Remove before install block

* Adding R and Python versions in jobs

* Use R as travis base

* Add DESCRIPT

* Move 01 and 02 to install block and add a 03 tmb run check

* Remove R-base from Conda install

* Add more Python checks

* Just have checks for now

* Reset path thing?

* Move apt-get installation inside 01

* try install again ugh

* Remove second install true?

* Try double scripting

* Caching all directories in Travis

* brek cache

* break... more

* Bad yml, fixing

* Move back conda to before inst

* install in ~/miniconda3

* Add some false installs

* A lot of refactor

* Add yes flag

* - Adding build status on README
- Squash build and test in a single stage
- Move the R package installation in the install block

* Adding apt-get installs of gcc and build ess

* Oops missed the folder nav to PyMB

* Switch to sourcing python instead of ipython

* Move PyMB install to install block and make sure that rpy2 imports

* Bad R link

* Pip install rpy2

* Try and get libRblas to pick up

* Update ld to include properly

* Add rpy2.rinterface import

* Test if we can run example without activating env in shell file

* Adding RcppEigen dependency for TMB

* Trying to test where TMB is breaking

* Add libeigen3 as installing

* Add RcppEigen include flag in linreg.py

* let's build TMB from source and not include the Eigen flag

* Bad CD

* Run example inline
  • Loading branch information
Nafis Sadat authored Aug 6, 2018
1 parent 85de1d9 commit 17731b3
Show file tree
Hide file tree
Showing 11 changed files with 358 additions and 102 deletions.
38 changes: 38 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
sudo: required

language: r

os:
- linux


# Setup anaconda
before_install:
- wget http://repo.continuum.io/miniconda/Miniconda3-latest-Linux-x86_64.sh -O miniconda.sh
- chmod +x miniconda.sh
- ./miniconda.sh -b -p /home/travis/miniconda3
- export PATH=/home/travis/miniconda3/bin:$PATH
- export LD_LIBRARY_PATH=/home/travis/R-bin/lib/R/lib:/home/travis/miniconda3/lib:$LD_LIBRARY_PATH
- R -e 'install.packages(c("Matrix", "RcppEigen", "numDeriv"), repos="http://cran.us.r-project.org", dependencies=T)'

## Cache R packages
cache: packages

## Set up conda environment, TMB and PyMB
install:
- conda create -c conda-forge -n pymb_test -y python=3.6 numpy scipy libiconv libxml2 lxml scikit-sparse
- source activate pymb_test
- which -a python && pip install rpy2 && python -c "import rpy2.rinterface as rinterface"
- git clone https://github.com/kaskr/adcomp.git && cd adcomp && make install-metis-full && cd ..
- cd ../PyMB && python setup.py install

jobs:
include:
- stage: Run PyMB tests
script:
- python Examples/linreg.py

notifications:
email:
on_success: never
on_failure: never
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Package: PyMB
44 changes: 44 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
FROM continuumio/miniconda3

## Get all the Linux dependencies
RUN apt-get update --fix-missing && \
apt-get install --yes \
wget \
curl \
bzip2 \
ca-certificates \
libglib2.0-0 \
libxext6 \
libsm6 \
libxrender1 \
git \
gcc \
g++ \
libreadline-dev \
libeigen3-dev \
tar \
build-essential && \
apt-get purge && \
apt-get clean

## Install Python dependencies
## NOTE: DO NOT install rpy2 from conda-forge, got issues
RUN conda install -c conda-forge -y numpy scipy && \
scikit-sparse libiconv libxml2 lxml

## Install R and rpy2
RUN conda install -c r -y \
r-base=3.5.0 && \
pip install rpy2


## Build TMB and it's dependencies
RUN R -e 'install.packages(c("Matrix", "numDeriv", "RcppEigen"), repos="http://cran.us.r-project.org", dependencies=T)' && \
cd /opt && \
git clone https://github.com/kaskr/adcomp.git && cd adcomp && \
make install-metis-full

## Install PyMB
RUN cd /opt && \
git clone https://github.com/kforeman/PyMB.git && \
cd PyMB && python setup.py install
68 changes: 68 additions & 0 deletions Examples/linreg.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import numpy as np
import os


import PyMB
import rpy2.robjects as ro
import rpy2.rinterface as rin

# create an empty model
m = PyMB.model(name='linreg_code')

# code for a simple linear regression
linreg_code = '''
#include <TMB.hpp>
template<class Type>
Type objective_function<Type>::operator() (){
// DATA
DATA_VECTOR(Y);
DATA_VECTOR(x);
// PARAMETERS
PARAMETER(alpha);
PARAMETER(Beta);
PARAMETER(logSigma);
// MODEL
vector<Type> Y_hat = alpha + Beta*x;
REPORT(Y_hat);
Type nll = -sum(dnorm(Y, Y_hat, exp(logSigma), true));
return nll;
}
'''

# Get the necessary paths to compile TMB code
tmbinclude = ro.r('paste0(find.package("TMB"), "/include")')[0]
# eigeninclude = ro.r('paste0(find.package("RcppEigen"), "/include")')[0]
# tmbinclude = tmbinclude + ' -I' + eigeninclude
rinclude = rin.R_HOME + "/include"
rlib = rin.R_HOME + "/lib"

# rpy2.rinterface.R_HOME

# compile the model
m.compile(codestr=linreg_code,
cc='g++',
R=rinclude,
TMB=tmbinclude,
LR=rlib,
verbose=True,
load=True)

# simulate data
N = 100
m.data['x'] = np.arange(N)
m.data['Y'] = m.data['x'] + 0.5 + np.random.rand(N)

# set initial parameter values
m.init['alpha'] = 0.
m.init['Beta'] = 0.
m.init['logSigma'] = 0.

# set random parameters
m.random = ['alpha', 'Beta']

# fit the model
m.optimize(draws=500)
print(m.report('Y_hat'))
m.print_parameters()
1 change: 1 addition & 0 deletions PyMB/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from PyMB.model import *
3 changes: 2 additions & 1 deletion magic/PyMB_magic.py → PyMB/magic/PyMB_magic.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,8 @@ def PyMB(self, line, cell):
code += '}\n'

# compile model
model.compile(codestr=code, output_dir=args.OUTPUT_DIR, cc=args.CCOMPILER, R=args.R_DIR, TMB=args.TMB_DIR, LR=args.R_LIB, verbose=args.VERBOSE)
model.compile(codestr=code, output_dir=args.OUTPUT_DIR, cc=args.CCOMPILER, R=args.R_DIR,
TMB=args.TMB_DIR, LR=args.R_LIB, verbose=args.VERBOSE)

# print if verbose
if args.VERBOSE == True:
Expand Down
File renamed without changes.
Loading

0 comments on commit 17731b3

Please sign in to comment.