From 3c7156aa6d96a72785c374c250bcbd626c6a3bd7 Mon Sep 17 00:00:00 2001 From: Leon Foks Date: Thu, 10 Oct 2024 21:24:29 -0700 Subject: [PATCH 1/4] Change install instructions --- python/README.md | 13 +++-------- python/gatdaem1d/__init__.py | 9 +++----- python/meson.build | 29 ++++++++++++++++++++++++ python/pyproject.toml | 26 +++++++++++++++++++++ python/setup.py | 44 ------------------------------------ 5 files changed, 61 insertions(+), 60 deletions(-) create mode 100644 python/meson.build create mode 100644 python/pyproject.toml delete mode 100644 python/setup.py diff --git a/python/README.md b/python/README.md index b424f50..bcd84ff 100644 --- a/python/README.md +++ b/python/README.md @@ -4,21 +4,14 @@ The Python (>=v3.5) interface consists of a C/C++ shared library (.so on Linux or .dll on Windows) called gatdaem1d which contains time-domain forward modelling and derivative functions which are called by the Python interpreter. ## Compiling and installing the C/C++ shared libraries -First the shared library needs to be built with CMake. See one of the CMake build scripts in the root directory of the ga-aem source code repository. If you are only interested in the Python interface, you need only build the ***`python_bindings`*** target. - -## Install directory contents -After being built successfully the install directory should contain, -- [ga-aem-install-dir]/python contains the package set up or installation function `setup.py`. -- [ga-aem-install-dir]/python/gatdaem1d contains the file `__init__.py` which is the package's Python classes and function code. It is also where the compiled shared library will reside after compilation. - - On Linux the shared library is [ga-aem-install-dir]/python/gatdaem1d/gatdaem1d.so. - - On Windows the shared library is [ga-aem-install-dir]/python/gatdaem1d/gatdaem1d.dll. -- [ga-aem-install-dir]/python/examples contains example Python usage code. +We are using Meson to automatically build the required C++ library. +Ensure that the GNU C++ compiler is available, and FFTW has been installed. ## PIP install of the Python package - To install as a python package you can then, ```bash cd [ga-aem-install-dir]/python - python -m pip install . + pip install . ``` - Note that **`python`** may need to be **`python3`** on your system. diff --git a/python/gatdaem1d/__init__.py b/python/gatdaem1d/__init__.py index 551e6ae..58ee85e 100644 --- a/python/gatdaem1d/__init__.py +++ b/python/gatdaem1d/__init__.py @@ -11,12 +11,9 @@ #Function to load the shared library def load_library(): - import platform; - if(platform.system() == "Windows"): - ext = '.dll' - else: - ext = '.so' - libname = os.path.join(os.path.dirname(os.path.realpath(__file__)),"gatdaem1d"+ext) + files = os.listdir(os.path.dirname(os.path.realpath(__file__))) + libname = [file for file in files if 'gatdaem1d_ext' in file][0] + libname = os.path.join(os.path.dirname(os.path.realpath(__file__)),libname) lib = ctypes.CDLL(libname) return lib; diff --git a/python/meson.build b/python/meson.build new file mode 100644 index 0000000..832037c --- /dev/null +++ b/python/meson.build @@ -0,0 +1,29 @@ +project('startingmeson', 'cpp', + version: '0.1' +) + +py = import('python').find_installation(pure: false) + +cpp = meson.get_compiler('cpp') + +incdir = include_directories(['../src', '../submodules/cpp-utils/include']) + +fftw = dependency('fftw3') + +module_path = 'gatdaem1d' + +shared_library('gatdaem1d_ext', + ['../src/gatdaem1d.cpp', '../src/gatdaem1d.h'], + include_directories: incdir, + dependencies: fftw, + install: true) + +python_sources = [ + 'gatdaem1d/__init__.py', + ] + +py.install_sources( + python_sources, + subdir: module_path +) + diff --git a/python/pyproject.toml b/python/pyproject.toml new file mode 100644 index 0000000..a9afc4a --- /dev/null +++ b/python/pyproject.toml @@ -0,0 +1,26 @@ +[build-system] +requires = ["meson-python"] +build-backend = "mesonpy" + +[project] +name = "gatdaem1d" +version = "2.0.1" +description = "Time-domain airborne electromagnetic forward modelling" +readme = { file = "README.md", content-type = "text/markdown" } +requires-python = ">=3.10" +authors = [{ name = "Ross C Brodie", email = "ross.c.brodie@ga.gov.au" }] +keywords = ["electromagnetic", "geophysics"] +dependencies = ["numpy"] +classifiers = [ + 'License :: OSI Approved', + 'License :: OSI Approved :: GNU General Public License v2 (GPLv2)', + 'Programming Language :: Python :: 3', + 'Topic :: Scientific/Engineering :: Physics', + 'Topic :: Electromagnetic :: Airborne :: Forward modelling :: Geophysics' +] + +[tool.spin] +package = 'gatdaem1d' + +[tool.setuptools.packages.find] +gatdaem1d = ["."] \ No newline at end of file diff --git a/python/setup.py b/python/setup.py deleted file mode 100644 index b721886..0000000 --- a/python/setup.py +++ /dev/null @@ -1,44 +0,0 @@ -#!/usr/bin/env python -# Setup Script -# Author: Leon Foks -# March 10 2017 -# Updated: Ross C Brodie, March 20 2017 -# Updated: Ross C Brodie, March 21 2024 to Version 2.0 -# Updated: Leon Foks, June 5 2024. Added .dylib -import sys -import os -from os.path import join - -# Test Python's version -major, minor = sys.version_info[0:2] -if (major, minor) < (3, 5): - sys.stderr.write('\nPython 3.5 or later is needed to use this package\n') - sys.exit(1) - -try: - from setuptools import setup -except ImportError: - pass - -setup(name='gatdaem1d', - packages=['gatdaem1d'], - package_dir={'gatdaem1d':'gatdaem1d'}, - package_data={'gatdaem1d':['gatdaem1d.so', 'gatdaem1d.dll', 'gatdaem1d.dylib']}, - scripts=[], - version=2.0, - description='Time-domain airborne electromagnetic forward modelling.', - long_description='Time-domain airborne electromagnetic forward modelling. Python interface to C++ library for forward model and derivative calculations for airborne electromagnetic (AEM) systems used in geophysics.', - classifiers=[ - 'Development Status :: 4 - Beta', - 'License :: OSI Approved :: GNU General Public License v2 (GPLv2)', - 'Programming Language :: Python :: 3.5', - 'Topic :: Scientific/Engineering :: Physics', - 'Topic :: Electromagnetic :: Airborne :: Forward modelling :: Geophysics', - ], - author='Ross C Brodie, Geoscience Australia and Leon Foks', - author_email='ross.c.brodie at ga.gov.au', - install_requires=[ - 'numpy>=1.11', - ], - url='https://github.com/GeoscienceAustralia/ga-aem', - license='GNU General Public License v2 (GPLv2)') From d94ba16ca8f4e6c5f66ef7112c6539eb8bc27e3c Mon Sep 17 00:00:00 2001 From: Leon Foks Date: Sat, 12 Oct 2024 21:15:18 -0700 Subject: [PATCH 2/4] meson does not work. Switching back to basic makefile. --- python/Makefile | 40 ++++++++++++++++++++++++++++++++++++++++ python/README.md | 10 +++++++--- python/meson.build | 29 ----------------------------- python/pyproject.toml | 17 ++++++++++------- 4 files changed, 57 insertions(+), 39 deletions(-) create mode 100755 python/Makefile delete mode 100644 python/meson.build diff --git a/python/Makefile b/python/Makefile new file mode 100755 index 0000000..11d6ea6 --- /dev/null +++ b/python/Makefile @@ -0,0 +1,40 @@ +SHELL = /bin/sh + +.SUFFIXES: +.SUFFIXES: .cpp .o + +cxxflags += -fPIC +ldflags += -shared +bindir = gatdaem1d + +srcdir = ../src +objdir = ./obj +includes = -I$(srcdir) -I$(FFTW_ROOT)/include -I../submodules/cpp-utils/include +libs = -L$(FFTW_ROOT)/lib -lfftw3 +library = $(bindir)/gatdaem1d.so + +all: compile link +allclean: clean compile link + +objects = \ + $(objdir)/gatdaem1d.o \ + +$(objects): $(objdir)/%.o: $(srcdir)/%.cpp + mkdir -p $(objdir) + @echo ' ' + @echo Compiling $< + $(cxx) -c $(includes) $(cxxflags) $< -o $@ + +compile: $(objects) + +link: $(objects) + mkdir -p $(bindir) + @echo ' ' + @echo Creating library + $(cxx) $(ldflags) $(objects) $(libs) -o $(library) + +clean: + @echo ' ' + @echo Cleaning + rm -f $(objects) + rm -f $(library) \ No newline at end of file diff --git a/python/README.md b/python/README.md index bcd84ff..b710b2d 100644 --- a/python/README.md +++ b/python/README.md @@ -4,16 +4,20 @@ The Python (>=v3.5) interface consists of a C/C++ shared library (.so on Linux or .dll on Windows) called gatdaem1d which contains time-domain forward modelling and derivative functions which are called by the Python interpreter. ## Compiling and installing the C/C++ shared libraries -We are using Meson to automatically build the required C++ library. Ensure that the GNU C++ compiler is available, and FFTW has been installed. +Mac OSX currently has issues using cmake and brew installed gcc compilers. So we are stuck using a Makefile. Simply type "make" in the python folder. On Linux, the easiest option is to use "make" also. + +Two environment variables need to be set before compilation using "export cxx=g++" and "export FFTW_ROOT=". + +On Windows, follow the documentation in the root folder. Once the library is compiled, go ahead and pip install. + ## PIP install of the Python package - To install as a python package you can then, ```bash - cd [ga-aem-install-dir]/python pip install . ``` -- Note that **`python`** may need to be **`python3`** on your system. +- Note that **`python`** may need to be **`python3`** on your system, and make sure the correct environment is activated/sourced. ## Examples - The directory [ga-aem-install-dir]/python/examples contains an example of how to use the gatdaem1d package. diff --git a/python/meson.build b/python/meson.build deleted file mode 100644 index 832037c..0000000 --- a/python/meson.build +++ /dev/null @@ -1,29 +0,0 @@ -project('startingmeson', 'cpp', - version: '0.1' -) - -py = import('python').find_installation(pure: false) - -cpp = meson.get_compiler('cpp') - -incdir = include_directories(['../src', '../submodules/cpp-utils/include']) - -fftw = dependency('fftw3') - -module_path = 'gatdaem1d' - -shared_library('gatdaem1d_ext', - ['../src/gatdaem1d.cpp', '../src/gatdaem1d.h'], - include_directories: incdir, - dependencies: fftw, - install: true) - -python_sources = [ - 'gatdaem1d/__init__.py', - ] - -py.install_sources( - python_sources, - subdir: module_path -) - diff --git a/python/pyproject.toml b/python/pyproject.toml index a9afc4a..c8a692f 100644 --- a/python/pyproject.toml +++ b/python/pyproject.toml @@ -1,6 +1,6 @@ [build-system] -requires = ["meson-python"] -build-backend = "mesonpy" +requires = ["setuptools", "wheel"] +build-backend = "setuptools.build_meta" [project] name = "gatdaem1d" @@ -10,7 +10,10 @@ readme = { file = "README.md", content-type = "text/markdown" } requires-python = ">=3.10" authors = [{ name = "Ross C Brodie", email = "ross.c.brodie@ga.gov.au" }] keywords = ["electromagnetic", "geophysics"] -dependencies = ["numpy"] +dependencies = [ + "numpy", + "matplotlib" +] classifiers = [ 'License :: OSI Approved', 'License :: OSI Approved :: GNU General Public License v2 (GPLv2)', @@ -19,8 +22,8 @@ classifiers = [ 'Topic :: Electromagnetic :: Airborne :: Forward modelling :: Geophysics' ] -[tool.spin] -package = 'gatdaem1d' - [tool.setuptools.packages.find] -gatdaem1d = ["."] \ No newline at end of file +where = ["."] + +[tool.setuptools.package-data] +gatdaem1d = ['gatdaem1d.*'] \ No newline at end of file From 93a2535ae2116ff95bda07cf49f47505458c2243 Mon Sep 17 00:00:00 2001 From: Leon Foks Date: Mon, 14 Oct 2024 18:21:01 -0700 Subject: [PATCH 3/4] forgot to change back the library extension name from meson test --- python/gatdaem1d/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/gatdaem1d/__init__.py b/python/gatdaem1d/__init__.py index 58ee85e..15e548e 100644 --- a/python/gatdaem1d/__init__.py +++ b/python/gatdaem1d/__init__.py @@ -12,7 +12,7 @@ #Function to load the shared library def load_library(): files = os.listdir(os.path.dirname(os.path.realpath(__file__))) - libname = [file for file in files if 'gatdaem1d_ext' in file][0] + libname = [file for file in files if 'gatdaem1d' in file][0] libname = os.path.join(os.path.dirname(os.path.realpath(__file__)),libname) lib = ctypes.CDLL(libname) return lib; From 1756152ce2de0aab56cd57e6a065b159a3d09b56 Mon Sep 17 00:00:00 2001 From: Leon Foks Date: Mon, 14 Oct 2024 18:25:38 -0700 Subject: [PATCH 4/4] add back in -O3 --- python/Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/Makefile b/python/Makefile index 11d6ea6..4e53d14 100755 --- a/python/Makefile +++ b/python/Makefile @@ -3,7 +3,7 @@ SHELL = /bin/sh .SUFFIXES: .SUFFIXES: .cpp .o -cxxflags += -fPIC +cxxflags += -fPIC -O3 ldflags += -shared bindir = gatdaem1d