Git Product home page Git Product logo

pyprofit's Introduction

pyprofit

image

image

image

pyprofit is a python wrapper for libprofit.

Installing

pyprofit is available in PyPI and thus can be easily installed via:

pip install pyprofit

Since version 1.8.1 pre-compiled binary versions of the package are available for Linux distributions. These are offered for convenience and come with OpenMP and FFTW support, but lack features that some users may want to use, like OpenCL, threaded-FFTW and some CPU-specific optimizations.

Compiling

If you need to compile this package (either because a binary is not available in your platform, or because you want to get the last drop of performance) you will need to compile and install libprofit first. For instruction on how to compile and install libprofit please read libprofit's documentation.

If you need to point to a specific libprofit installation set the LIBPROFIT_HOME environment variable to point to it, e.g.:

LIBPROFIT_HOME=/opt/software/libprofit/1.8.0/ pip install pyprofit

Note that most users will need to specify a user-writable installation directory using the -DCMAKE_INSTALL_PREFIX=/my/installation/directory option when invoking cmake as part of libprofit's compilation. This is the same directory that then needs to be specified to pyprofit via its LIBPROFIT_HOME environment variable.

All in all, the process of compiling both libprofit and pyprofit should work similar to this:

$> git clone https://github.com/ICRAR/libprofit
$> mkdir libprofit/build
$> cd libprofit/build
$> cmake .. -DCMAKE_INSTALL_PREFIX=/my/libprofit/installation/directory
$> make
$> make install
$> LIBPROFIT_HOME=/my/libprofit/installation/directory pip install pyprofit

Troubleshooting

If you are experiencing problems with your installation (specially if you are compiling this package) try the following:

DISTUTILS_DEBUG=1 PYPROFIT_NO_MUTE=1 python setup.py -v build

pyprofit's People

Contributors

manodeep avatar paulprice avatar rtobar avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

pyprofit's Issues

Suggestions for installing with C+11 support on Mac OSX

Several of my collaborators have had difficulty installing pyprofit on MacOS (Big Sur and other earlier but recent versions). The Troubleshooting instructions and the error message in setup.py regarding C++11 support do not work for some installation methods on MacOS. When compiling with clang, passing -std=c++11 alone usually will not work, and an additional -stdlib=libc++ flag is needed. The following installation sequence has worked on all MacOS systems we have tried (note the lack of single quotes and spacing):

DISTUTILS_DEBUG=1 PYPROFIT_NO_MUTE=1 PYPROFIT_CXX11=-stdlib=libc++ CFLAGS=-std=c++11 python3 setup.py -v build

python3 -m pip install .

The above is not a unique solution, but it's worked for us.

I decided to submit an issue in case this was of use to anyone else trying to use pyprofit with MacOS. Thank you for making the library available to the community.

Documentation

There is no Pyprofit documentation. Could you please add this? I'm doing research on Galactic evolution and I want to use Pyprofit instead of Galfit.

example files in usage.py

Hello,

I have pyprofit installed and am trying to do the examples so I can decide whether to use the python version or the R version. in the usage.py file you have the following:

Images on which we'll base the optimization

basename = '/home/rtobar/scm/git/ProFit/inst/extdata/G265911'
image = np.array(pyfits.getdata(basename + 'fitim.fits'))
sigim = np.array(pyfits.getdata(basename + 'sigma.fits'))
segim = np.array(pyfits.getdata(basename + 'segim.fits'))
mask = np.array(pyfits.getdata(basename + 'mskim.fits'))
psf = np.array(pyfits.getdata(basename + 'psfim.fits'))

I can't find these files in the folders anywhere.

Many thanks,

Raphael.

PS I had a bit of trouble getting GSL to link properly. Eventually I fixed it by adding the following to my setup.py (my changes marked #RAPH. I think this is because gsl is not in my path or something (python distribution novice)

"""
def has_gsl():
compiler = distutils.ccompiler.new_compiler()
compiler.add_include_dir('/usr/local/include') #RAPH
compiler.add_library_dir('/usr/local/lib') #RAPH
return compiler.has_function('gsl_sf_gamma', libraries=['gsl', 'gslcblas'])
#return compiler.has_function('gsl_sf_gamma', libraries=['/usr/local/lib/gsl', '/usr/local/lib/gslcblas'])

Our module

pyprofit_sources = ['pyprofit.cpp']

libprofit sources

pyprofit_sources += glob.glob('libprofit/src/*.cpp')

include dirs

incdirs = ['libprofit', '/usr/local/include'] #RAPH
#incdirs = ['libprofit', '/usr/local/include']

gsl libs

if not has_gsl():
print("\n\nNo GSL installation found on your system. Install the GSL development package and try again\n\n")
sys.exit(1)

#libs = ['gsl', 'gslcblas']
libdirs = ['/usr/local/lib'] #RAPH
libs = ['gsl', 'gslcblas']

pyprofit_ext = Extension('pyprofit',
depends=glob.glob('libprofit/profit/*.h'),
language='c++',
define_macros = [('HAVE_GSL',1)],
sources = pyprofit_sources,
include_dirs = incdirs,
library_dirs = libdirs, #RAPH
libraries = libs,
extra_compile_args=[stdspec] if stdspec else [])
"""

axrat > 1

Hi,

Apologies for bothering you again. I have installed pyprofit using 'pip install pyprofit' (I nver managed to get it working using the setup.py from github). When I run Usage.py it iterates through a very large number models before breaking with the error pasted at the bottom. For some reason the axrat suddenly changes from 1.0 to 1.0000000069077553. I can't see if we are fitting axrat from:

Initial set of parameters

names = ['%s.%s' % (profile, prop) for prop,profile in itertools.product(('xcen','ycen','mag','re','nser','ang','axrat','box'), ('sersic1','sersic2'))]
model0 = np.array((84.8832, 84.8832, 94.5951, 94.5951, 16.83217, 16.83217, 7.0574, 14.1148, 4.3776, 1.0000, 140.8191, 140.8191, 1., 0.4891, 0, 0))
tofit = np.array((True, False, True, False, True, True, True, True, True, False, True, True, False, True, True, False))
tolog = np.array((False, False, False, False, False, False, True, True, True, True, False, False, True, True, False, False))
sigmas = np.array((2, 2, 2, 2, 5, 5, 1, 1, 1, 1, 30, 30, 0.3, 0.3, 0.3, 0.3))
lowers = np.array((0, 0, 0, 0, 10, 10, 0, 0, -1, -1, -180, -180, -1, -1, -1, -1))
uppers = np.array((1e3, 1e3, 1e3, 1e3, 30, 30, 2, 2, 1.3, 1.3, 360, 360, 0, 0, 1, 1))
priors = np.array([prior_func(s) for s in sigmas])

I have not changed the usage.py at all. It seems very odd that it suddenly changes axrat which is presumably held fixed and breaks.

Any advice would be greatly appreciated.

Best,

Raphael.

{'psf': array([[ 0., 0., 0., ..., 0., 0., 0.],
[ 0., 0., 0., ..., 0., 0., 0.],
[ 0., 0., 0., ..., 0., 0., 0.],
...,
[ 0., 0., 0., ..., 0., 0., 0.],
[ 0., 0., 0., ..., 0., 0., 0.],
[ 0., 0., 0., ..., 0., 0., 0.]], dtype=float32), 'width': 169, 'magzero': 0, 'profiles': {'sersic': [{'box': 1.0, 'ang': -180.0, 'xcen': 0.0, 'axrat': 1.0, 're': 1.0, 'convolve': True, 'mag': 10.0, 'nser': 19.952623149688797, 'ycen': 0.0}, {'box': 0.0, 'ang': -180.0, 'xcen': 84.883200000000002, 'axrat': 1.0000000069077553, 're': 1.0, 'convolve': True, 'mag': 30.0, 'nser': 1.0, 'ycen': 94.595100000000002}]}, 'height': 187}
Traceback (most recent call last):

File "", line 1, in
runfile('/Users/rs548/GitHub/pyprofit/examples/usage.py', wdir='/Users/rs548/GitHub/pyprofit/examples')

File "/Users/rs548/anaconda/lib/python2.7/site-packages/spyder/utils/site/sitecustomize.py", line 866, in runfile
execfile(filename, namespace)

File "/Users/rs548/anaconda/lib/python2.7/site-packages/spyder/utils/site/sitecustomize.py", line 94, in execfile
builtins.execfile(filename, *where)

File "/Users/rs548/GitHub/pyprofit/examples/usage.py", line 122, in
run()

File "/Users/rs548/GitHub/pyprofit/examples/usage.py", line 111, in run
result = optimize.minimize(profit_like_model, data.init, args=(data,), method='L-BFGS-B', bounds=data.bounds, options={'disp':True})

File "/Users/rs548/anaconda/lib/python2.7/site-packages/scipy/optimize/_minimize.py", line 450, in minimize
callback=callback, **options)

File "/Users/rs548/anaconda/lib/python2.7/site-packages/scipy/optimize/lbfgsb.py", line 328, in _minimize_lbfgsb
f, g = func_and_grad(x)

File "/Users/rs548/anaconda/lib/python2.7/site-packages/scipy/optimize/lbfgsb.py", line 274, in func_and_grad
g = _approx_fprime_helper(x, fun, epsilon, args=args, f0=f)

File "/Users/rs548/anaconda/lib/python2.7/site-packages/scipy/optimize/optimize.py", line 628, in _approx_fprime_helper
grad[k] = (f(*((xk + d,) + args)) - f0) / d[k]

File "/Users/rs548/anaconda/lib/python2.7/site-packages/scipy/optimize/optimize.py", line 292, in function_wrapper
return function(*(wrapper_args + args))

File "profit_optim.py", line 82, in profit_like_model
allparams, modelim = to_pyprofit_image(params, data)

File "profit_optim.py", line 70, in to_pyprofit_image
return allparams, np.array(pyprofit.make_model(profit_model))

error: axrat > 1, must have axrat <= 1

Python package

Hello,

Thank you very much for making this software available. I am trying to use the software with a view to applying it to galaxy deblending problems we are working on at the University of Sussex Astronomy Centre.

I am struggling to install the python package using 'python setup.py install' which throws the following error:

/var/folders/zx/nzb1wy_d0yj1g8wznhz1ns68000930/T/tmplanbqx8y.cpp:4:21: error: cannot deduce type of initializer list because
std::initializer_list was not found; include <initializer_list>
for(auto i: {0,1,2}) {
^
1 error generated.
/var/folders/zx/nzb1wy_d0yj1g8wznhz1ns68000930/T/tmplanbqx8y.cpp:4:21: error: cannot deduce type of initializer list because
std::initializer_list was not found; include <initializer_list>
for(auto i: {0,1,2}) {
^
1 error generated.
/var/folders/zx/nzb1wy_d0yj1g8wznhz1ns68000930/T/tmplanbqx8y.cpp:4:13: warning: 'auto' type specifier is a C++11 extension
[-Wc++11-extensions]
for(auto i: {0,1,2}) {
^
/var/folders/zx/nzb1wy_d0yj1g8wznhz1ns68000930/T/tmplanbqx8y.cpp:4:19: warning: range-based for loop is a C++11 extension
[-Wc++11-extensions]
for(auto i: {0,1,2}) {
^
/var/folders/zx/nzb1wy_d0yj1g8wznhz1ns68000930/T/tmplanbqx8y.cpp:4:21: error: cannot deduce type of initializer list because
std::initializer_list was not found; include <initializer_list>
for(auto i: {0,1,2}) {
^
2 warnings and 1 error generated.
No C/C++ compiler with C++11 support found. Use the CC environment variable to specify a different compiler if you have one
Using None to enable C++11 support

It is clearly a c++ compiler problem. I seem to be able to compile the test code in the setup.py file using g++ on a mac with 'g++ -std=c++ main.cpp' but when that is run in setup.py it throws the error after setting the CC envirnoment variable to g++.

Do you have any suggestions for fixing this problem. I get the same errors when I try to use pip.

Sorry if I'm doing something stupid.

Best regards,

Dr Raphael Shirley
University of Sussex
[email protected]

pyprofit fails to load on python 3

From a report in #1, originally reported by @raphaelshirley:

Traceback (most recent call last):
File "usage.py", line 36, in 
from profit_optim import profit_setup_data, profit_like_model, to_pyprofit_image
File "/Users/rs548/GitHub/pyprofit/examples/profit_optim.py", line 30, in 
import pyprofit
SystemError: PyState_AddModule called on module with slots
Bus error: 10

Problems finding GSL

(Originally reported by @raphaelshirley in #3)

I had a bit of trouble getting GSL to link properly. Eventually I fixed it by adding the following to my setup.py (my changes marked #RAPH. I think this is because gsl is not in my path or something (python distribution novice)

def has_gsl():
    compiler = distutils.ccompiler.new_compiler()
    compiler.add_include_dir('/usr/local/include') #RAPH
    compiler.add_library_dir('/usr/local/lib') #RAPH
    return compiler.has_function('gsl_sf_gamma', libraries=['gsl', 'gslcblas'])
    #return compiler.has_function('gsl_sf_gamma', libraries=['/usr/local/lib/gsl', '/usr/local/lib/gslcblas'])

# Our module
pyprofit_sources = ['pyprofit.cpp']

# libprofit sources
pyprofit_sources += glob.glob('libprofit/src/*.cpp')

# include dirs
incdirs = ['libprofit', '/usr/local/include'] #RAPH
#incdirs = ['libprofit', '/usr/local/include']

# gsl libs
if not has_gsl():
    print("\n\nNo GSL installation found on your system. Install the GSL development package and try again\n\n")
    sys.exit(1)

#libs = ['gsl', 'gslcblas']
libdirs = ['/usr/local/lib'] #RAPH
libs = ['gsl', 'gslcblas']

pyprofit_ext = Extension('pyprofit',
                       depends=glob.glob('libprofit/profit/*.h'),
                       language='c++',
                       define_macros = [('HAVE_GSL',1)],
                       sources = pyprofit_sources,
                       include_dirs = incdirs,
                       library_dirs = libdirs, #RAPH
                       libraries = libs,
                       extra_compile_args=[stdspec] if stdspec else [])

Pyprofit Import Error on MacOX M1

Hi, I am having trouble importing pyprofit from Jupyter notebook. Here is the trouble shooting:

---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
/Users/changhaosmac/Desktop/academy/physics/PHD/freshman/proposal/code/GALFITs.ipynb Cell 2 in <cell line: 1>()
----> 1 import pyprofit

ImportError: dlopen(/Users/changhaosmac/opt/anaconda3/lib/python3.9/site-packages/pyprofit.cpython-39-darwin.so, 0x0002): symbol not found in flat namespace (__ZTIN6profit12opencl_errorE)

My pyprofit is installed using pip, I have also tried the following:

LIBPROFIT_HOME=~/my/installation/directory pip install pyprofit

but same problem persists.
I am using the newest MacBook Pro with M1 chip, any advices ?
(My colleague using Mac with intel framework doesn't seem to have this problem)

pyprofit does not find headers

Hi, I am trying to get the python wrapper to compile on a Scientific Linux 7 machine (Red Hat clone).
I am running anaconda python 3.5.5.
I installed libprofit successfully and have copied the profit headers to /usr/include/profit and
libprofit.so to /usr/lib64. Following the steps described in a previous issue I ran:

LIBPROFIT_HOME=/home/cnaw/libprofit PYPROFIT_CXX11="-std c++" python setup.py

and got this:

running build
running build_ext
running configure
-- Using '-std c++' to enable C++11 support
-- no suitable libprofit headers not found
error: No libprofit installation found on your system.

Supported versions are: 1.7.0, 1.7.1, 1.7.2, 1.7.3, 1.7.4, 1.8.0-dev, 1.8.0

You can specify a libprofit installation directory via the LIBPROFIT_HOME environment variable.
Additionally, you can also use the LIBPROFIT_INCDIR and LIBPROFIT_LIBDIR environment variables
to point separately to the headers and library directories respectivelly

For example:

LIBPROFIT_HOME=~/local python setup.py install

In the subroutine def has_libprofit in the setup.py , I see it is checking for config.h while the headers directory has config,h.in If I change this to config.h.in I get the following exceptions:

[cnaw@tangara pyprofit-1.8.0]$ LIBPROFIT_HOME=/home/cnaw/libprofit PYPROFIT_CXX11="-std c++" python setup.py build
running build
running build_ext
running configure
-- Using '-std c++' to enable C++11 support
Traceback (most recent call last):
File "setup.py", line 348, in
'build_ext': _build_ext,
File "/home/cnaw/anaconda3/lib/python3.5/site-packages/setuptools/init.py", line 140, in setup
return distutils.core.setup(**attrs)
File "/home/cnaw/anaconda3/lib/python3.5/distutils/core.py", line 148, in setup
dist.run_commands()
File "/home/cnaw/anaconda3/lib/python3.5/distutils/dist.py", line 955, in run_commands
self.run_command(cmd)
File "/home/cnaw/anaconda3/lib/python3.5/distutils/dist.py", line 974, in run_command
cmd_obj.run()
File "/home/cnaw/anaconda3/lib/python3.5/distutils/command/build.py", line 135, in run
self.run_command(cmd_name)
File "/home/cnaw/anaconda3/lib/python3.5/distutils/cmd.py", line 313, in run_command
self.distribution.run_command(command)
File "/home/cnaw/anaconda3/lib/python3.5/distutils/dist.py", line 974, in run_command
cmd_obj.run()
File "setup.py", line 317, in run
self.run_command('configure')
File "/home/cnaw/anaconda3/lib/python3.5/distutils/cmd.py", line 313, in run_command
self.distribution.run_command(command)
File "/home/cnaw/anaconda3/lib/python3.5/distutils/dist.py", line 974, in run_command
cmd_obj.run()
File "setup.py", line 282, in run
info = has_libprofit(user_incdirs, user_libdirs, extra_compile_args)
File "setup.py", line 189, in has_libprofit
distutils.log.debug("-- Found libprofit version %s in %s" % (version_as_str(version), header))
File "setup.py", line 163, in version_as_str
ver_str = '.'.join(map(str, version[:3]))
TypeError: 'NoneType' object is not subscriptable

How can I solve this ?

Thanks !

Christopher Willmer
Steward Observatory, University of Arizona

getting usage.py to work

Hi @rtobar

Thank you for including the usage.py example script. This makes testing exceedingly easier!

I had to make a few changes on the script so it would run on Python 3.5.5.
The first one was to modify
import pyfits
to
import astropy.io.fits
Apparently pyfits is not being actively supported by STScI . At least there was no easy way to get hold of a pyfits version that would run under 3.5.5 and not create a conflict with other packages.
By using the astropy version another change must be made:
from
return np.array(pyfits.getdata(fname))
to
return np.array(astropy.io.fits.getdata(fname))

After fixing this, the fit completed, but the script crashed when making the plots:
Traceback (most recent call last):
File "usage_v1.py", line 124, in
run()
File "usage_v1.py", line 119, in run
plot_image_comparison(data.image, modelim0, data.sigim, data.region)
File "usage_v1.py", line 60, in plot_image_comparison
modplot.imshow(modelim, cmap='gray', norm=mpl.colors.LogNorm())
File "/home/cnaw/anaconda3/lib/python3.5/site-packages/matplotlib/init.py", line 1867, in inner
return func(ax, *args, **kwargs)
File "/home/cnaw/anaconda3/lib/python3.5/site-packages/matplotlib/axes/_axes.py", line 5501, in imshow
im.set_data(X)
File "/home/cnaw/anaconda3/lib/python3.5/site-packages/matplotlib/image.py", line 650, in set_data
raise TypeError("Image data cannot be converted to float")
TypeError: Image data cannot be converted to float

The solution I found was to do this:

# Now result.x contains the optimal set of parameters
# Plot the initial and final set of parameters to see the difference
_, modelim0 = to_pyprofit_image(data.init, data, use_mask=False)

this is the fix - add these lines:
array0 = np.array(modelim0[0])
array0.shape
array0.size

all_params, modelim  = to_pyprofit_image(result.x, data, use_mask=False)

add
array = np.array(modelim[0])
array.shape
array.size

change
plot_image_comparison(data.image, modelim0, data.sigim, data.region)
plot_image_comparison(data.image, modelim, data.sigim, data.region)
to:
plot_image_comparison(data.image, array0, data.sigim, data.region)
plot_image_comparison(data.image, array, data.sigim, data.region)

Once that is done I have a figure!
By the way I also had to follow the advice given in one of the previous issues in
setting an upper bound to log10(axrat) = -0.01 so it would not raise an exception.

Two things that would be helpful in the examples directory are a table showing the output of
the model fit and the corresponding plot. This would give (me at least!) confidence that the
fit is running as it should.
Thanks!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.