Skip to content

Commit

Permalink
Fix for pybind11 and updates in github actions (#478)
Browse files Browse the repository at this point in the history
* parent 94fe3d6
author Subhasis Ray <ray.subhasis@gmail.com> 1715590080 +0530
committer Subhasis Ray <ray.subhasis@gmail.com> 1715619586 +0530

parent 94fe3d6
author Subhasis Ray <ray.subhasis@gmail.com> 1715590080 +0530
committer Subhasis Ray <ray.subhasis@gmail.com> 1715618717 +0530

- Updated includes in sourcecode for pybind11
- Fixed cmake file for finding system pybind11
- Updated github workflow to use micromamba setup from github marketplace

* Added simple test of importing moose
  • Loading branch information
subhacom committed May 15, 2024
1 parent 94fe3d6 commit a6db169
Show file tree
Hide file tree
Showing 9 changed files with 94 additions and 75 deletions.
61 changes: 40 additions & 21 deletions .github/workflows/pymoose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,43 @@ jobs:
- os: macos-14
brew: 20
steps:
- uses: actions/checkout@v2
- if: ${{ matrix.apt }}
run: sudo apt-get install libhdf5-dev libgsl0-dev
- if: ${{ matrix.brew }}
run: |
brew install gsl
brew install hdf5
- name: Set up Python ${{ matrix.python-version }}
shell: bash
run: |
"${SHELL}" <(curl -L micro.mamba.pm/install.sh)
micromamba self-update
micromamba create -n moose python=${{ matrix.python-version }} graphviz lxml cmake numpy matplotlib vpython hdf5 pytables doxygen -c conda-forge
eval "$(micromamba shell hook --shell bash)"
micromamba activate moose
pip install python-libsbml
pip install pyneuroml
- name: Build
run: |
pip install .
- name: mamba-setup
uses: mamba-org/setup-micromamba@v1
with:
environment-name: moose
cache-environment: true
cache-downloads: true
create-args: >-
python=${{ matrix.python-version }}
hdf5
graphviz
pytables
numpy
matplotlib
vpython
lxml
doxygen
setuptools
wheel
- name: Display Python version
run: |
python -c "import sys; print(sys.version)"
- if: ${{ matrix.apt }}
run: sudo apt-get install libhdf5-dev libgsl0-dev graphviz-dev python3-lxml cmake doxygen
- if: ${{ matrix.brew }}
run: |
brew install gsl
brew install hdf5
brew install graphviz
brew install cmake
brew install doxygen
- name: install python module dependencies via pip
run: |
pip install pybind11[global]
pip install python-libsbml
- name: checkout
uses: actions/checkout@v4
- name: build and install
run: |
pip install .
python -c "import moose; moose.le()"
22 changes: 6 additions & 16 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,9 @@ include(CheckCXXCompiler.cmake)
include(CheckIncludeFileCXX)

# We find python executable here. Though mainly used inside pymoose.
# FIXME: When cmake 3.12 is widely available use the following:
# find_package(Python3 COMPONENTS Interpreter Numpy)
# set(PYTHON_EXECUTABLE ${Python3_EXECUTABLE})
set(Python_ADDITIONAL_VERSIONS 2.7)
find_package(PythonInterp 3.5)
if(NOT PYTHONINTERP_FOUND)
find_package(PythonInterp 2.7)
endif()
find_package(Python3 COMPONENTS Interpreter Numpy)
set(PYTHON_EXECUTABLE ${Python3_EXECUTABLE})
find_package(PythonInterp 3.8)

set(CMAKE_MACOSX_RPATH OFF)

Expand Down Expand Up @@ -321,14 +316,9 @@ endif( WITH_BOOST )
# It can be easily done with `pip install pybind11`
# See: https://pybind11.readthedocs.io/en/stable/installing.html
# - Subha, Mon Apr 22 14:26:58 IST 2024

# if(NOT WITH_LEGACY_BINDING)
# add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/external/pybind11)
# add_subdirectory(pybind11)
# else()
# message(STATUS "Building legacy python binding.")
# add_subdirectory(pymoose)
# endif()
execute_process(COMMAND ${CMAKE_COMMAND} -E pybind11-config --cmakedir OUTPUT_VARIABLE pybind11_DIR OUTPUT_STRIP_TRAILING_WHITESPACE)
find_package(pybind11 REQUIRED HINTS "${Python3_SITELIB}")
add_subdirectory(pybind11)


# always override debian default installation directory. It will be installed in
Expand Down
21 changes: 19 additions & 2 deletions INSTALL.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@ followings are installed.

- gsl-1.16 or higher.
- python-numpy
- pybind11 (if the setup fails to find pybind11, try running `pip install pybind11[global]`)

On Ubuntu-16.04 or higher, these dependencies can be installed with:

```
sudo apt-get install python-pip python-numpy cmake libgsl-dev g++
sudo apt-get install python-pip python-numpy cmake libgsl-dev g++ pybind11
```

Now use `pip` to download and install `pymoose` from the [github repository](https://github.com/BhallaLab/moose-core).
Expand Down Expand Up @@ -83,10 +84,26 @@ Now you can import moose in a Python script or interpreter with the statement:
>>> import moose
>>> moose.test() # will take time. Not all tests will pass.

## Uninstall

To uninstall moose, run

$ pip uninstall pymoose

If you are building moose from source, make sure to get out of the source directory, or you may encounter a message like this:

Found existing installation: pymoose {version}
Can't uninstall 'pymoose'. No files were found to uninstall.




# Notes

SBML support is enabled by installing
- SBML support is enabled by installing
[python-libsbml](http://sbml.org/Software/libSBML/docs/python-api/libsbml-installation.html).
Alternatively, it can be installed by using `python-pip`

$ sudo pip install python-libsbml


8 changes: 4 additions & 4 deletions pybind11/Finfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
//
// =====================================================================================

#include "../external/pybind11/include/pybind11/pybind11.h"
#include "../external/pybind11/include/pybind11/stl.h"
#include "../external/pybind11/include/pybind11/numpy.h"
#include "../external/pybind11/include/pybind11/functional.h"
#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
#include <pybind11/numpy.h>
#include <pybind11/functional.h>

namespace py = pybind11;

Expand Down
6 changes: 3 additions & 3 deletions pybind11/MooseVec.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@

using namespace std;

#include "../external/pybind11/include/pybind11/pybind11.h"
#include "../external/pybind11/include/pybind11/numpy.h"
#include "../external/pybind11/include/pybind11/stl.h"
#include <pybind11/pybind11.h>
#include <pybind11/numpy.h>
#include <pybind11/stl.h>

namespace py = pybind11;

Expand Down
4 changes: 2 additions & 2 deletions pybind11/MooseVec.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
#ifndef MOOSE_VEC_H
#define MOOSE_VEC_H

#include "../external/pybind11/include/pybind11/pybind11.h"
#include "../external/pybind11/include/pybind11/numpy.h"
#include <pybind11/pybind11.h>
#include <pybind11/numpy.h>
namespace py = pybind11;

class MooseVec
Expand Down
10 changes: 5 additions & 5 deletions pybind11/helper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,16 @@
#include <stdexcept>
#include <csignal>

#include "../external/pybind11/include/pybind11/functional.h"
#include "../external/pybind11/include/pybind11/numpy.h"
#include "../external/pybind11/include/pybind11/pybind11.h"
#include "../external/pybind11/include/pybind11/stl.h"
#include <pybind11/functional.h>
#include <pybind11/numpy.h>
#include <pybind11/pybind11.h>
#include <pybind11/stl.h>

namespace py = pybind11;

// See
// https://pybind11.readthedocs.io/en/stable/advanced/cast/stl.html#binding-stl-containers
// #include "../external/pybind11/include/pybind11/stl_bind.h"
// #include <pybind11/stl_bind.h>

#include "../basecode/header.h"
#include "../basecode/global.h"
Expand Down
6 changes: 3 additions & 3 deletions pybind11/pymoose.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@
#include <functional>
#include <chrono>

#include "../external/pybind11/include/pybind11/pybind11.h"
#include "../external/pybind11/include/pybind11/stl.h"
#include "../external/pybind11/include/pybind11/numpy.h"
#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
#include <pybind11/numpy.h>

namespace py = pybind11;
using namespace std;
Expand Down
31 changes: 12 additions & 19 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
# See https://docs.python.org/3/library/distutils.html
# setuptools is preferred over distutils. And we are supporting python3 only.
from setuptools import setup, Extension, Command
from setuptools.command.build_ext import build_ext as _build_ext
from setuptools.command.build_ext import build_ext
import subprocess

# Global variables.
Expand All @@ -39,6 +39,7 @@

if not os.path.exists(builddir_):
os.makedirs(builddir_)


numCores_ = multiprocessing.cpu_count()

Expand All @@ -49,18 +50,10 @@


class CMakeExtension(Extension):
# Reference: https://martinopilia.com/posts/2018/09/15/building-python-extension.html
def __init__(self, name, **kwargs):
# don't invoke the original build_ext for this special extension
import tempfile

# Create a temp file to create a dummy target. This build raises an
# exception because sources are empty. With python3 we can fix it by
# passing `optional=True` to the argument. With python2 there is no
# getaway from it.
f = tempfile.NamedTemporaryFile(suffix='.cpp', delete=False)
f.write(b'int main() { return 1; }')
Extension.__init__(self, name, sources=[f.name], **kwargs)
f.close()
Extension.__init__(self, name, sources=[], **kwargs)


class TestCommand(Command):
Expand All @@ -79,14 +72,14 @@ def run(self):
os.chdir(sdir_)


class build_ext(_build_ext):
class cmake_build_ext(build_ext):
user_options = [
('with-boost', None, 'Use Boost Libraries (OFF)'),
('with-gsl', None, 'Use Gnu Scienfific Library (ON)'),
('with-gsl-static', None, 'Use GNU Scientific Library (static library) (OFF)'),
('debug', None, 'Build moose in debugging mode (OFF)'),
('no-build', None, 'DO NOT BUILD. (for debugging/development)'),
] + _build_ext.user_options
] + build_ext.user_options

def initialize_options(self):
# Initialize options.
Expand All @@ -97,12 +90,12 @@ def initialize_options(self):
self.no_build = 0
self.cmake_options = {}
# super().initialize_options()
_build_ext.initialize_options(self)
build_ext.initialize_options(self)

def finalize_options(self):
# Finalize options.
# super().finalize_options()
_build_ext.finalize_options(self)
build_ext.finalize_options(self)
self.cmake_options['PYTHON_EXECUTABLE'] = os.path.realpath(sys.executable)
self.cmake_options['VERSION_MOOSE'] = version_
if self.with_boost:
Expand All @@ -122,7 +115,7 @@ def run(self):
for ext in self.extensions:
self.build_cmake(ext)
# super().run()
_build_ext.run(self)
build_ext.run(self)

def build_cmake(self, ext):
global numCores_
Expand Down Expand Up @@ -175,7 +168,7 @@ def build_cmake(self, ext):
]
},
install_requires=['numpy', 'matplotlib', 'vpython', 'pybind11'],
extra_requires={'dev': ['coverage', 'pytest', 'pytest-cov']},
ext_modules=[CMakeExtension('dummy', optional=True)],
cmdclass={'build_ext': build_ext, 'test': TestCommand},
extra_require={'dev': ['coverage', 'pytest', 'pytest-cov']},
ext_modules=[CMakeExtension('_moose', optional=True)],
cmdclass={'build_ext': cmake_build_ext, 'test': TestCommand},
)

0 comments on commit a6db169

Please sign in to comment.