Skip to content

Commit

Permalink
Merge 0061c93 into 2964ac6
Browse files Browse the repository at this point in the history
  • Loading branch information
dstndstn committed Dec 24, 2019
2 parents 2964ac6 + 0061c93 commit 2250036
Show file tree
Hide file tree
Showing 7 changed files with 921 additions and 20 deletions.
146 changes: 137 additions & 9 deletions setup-cython.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,130 @@
from distutils.core import setup
import os
from distutils.core import setup, Extension
from distutils.dist import Distribution
from Cython.Build import cythonize

setup(
ext_modules = cythonize([
# from http://stackoverflow.com/questions/12491328/python-distutils-not-include-the-swig-generated-module
from distutils.command.build import build
class CustomBuild(build):
sub_commands = [
('build_ext', build.has_ext_modules),
('build_py', build.has_pure_modules),
('build_clib', build.has_c_libraries),
('build_scripts', build.has_scripts),
]

import numpy as np
numpy_inc = [np.get_include()]

if os.environ.get('CC') == 'icc':
compile_args = ['-g', '-xhost', '-axMIC-AVX512']
link_args = ['-g', '-lsvml']
else:
compile_args = ['-g', '-std=c99']
link_args = ['-g']

kwargs = dict(extra_compile_args=compile_args,
extra_link_args=link_args)

module_fourier = Extension('tractor._mp_fourier',
sources = ['tractor/mp_fourier.i'],
include_dirs = numpy_inc,
undef_macros=['NDEBUG'],
swig_opts=['-outdir', 'tractor'],
**kwargs)
module_mix = Extension('tractor._mix',
sources = ['tractor/mix.i'],
include_dirs = numpy_inc,
extra_objects = [],
undef_macros=['NDEBUG'],
swig_opts=['-outdir', 'tractor'],
**kwargs)

module_em = Extension('tractor._emfit',
sources = ['tractor/emfit.i' ],
include_dirs = numpy_inc,
extra_objects = [],
undef_macros=['NDEBUG'],
swig_opts=['-outdir', 'tractor'],
**kwargs)

mods = [module_mix, module_em, module_fourier]
pymods = ['tractor.mix', 'tractor.emfit', 'tractor.mp_fourier']

## Ceres module
import sys
from subprocess import check_output

eigen_inc = os.environ.get('EIGEN_INC', None)
if eigen_inc is None:
try:
eigen_inc = check_output(['pkg-config', '--cflags', 'eigen3']).strip()
# py3
eigen_inc = eigen_inc.decode()
except:
eigen_inc = ''
inc = eigen_inc.split()

ceres_inc = os.environ.get('CERES_INC', None)
ceres_lib = os.environ.get('CERES_LIB', '-lceres')
if ceres_inc is not None:
inc.append(ceres_inc)
link = []
if ceres_lib is not None:
link.append(ceres_lib)

module_ceres = Extension('tractor._ceres',
sources=['tractor/ceres-tractor.cc', 'tractor/ceres.i'],
include_dirs = numpy_inc,
extra_compile_args = compile_args + inc,
extra_link_args = link_args + link,
language = 'c++',
swig_opts=['-c++', '-outdir', 'tractor'],
)
class MyDistribution(Distribution):
display_options = Distribution.display_options + [
('with-ceres', None, 'build Ceres module?'),
]
key = '--with-ceres'
if key in sys.argv:
sys.argv.remove(key)
mods.append(module_ceres)
pymods.append('tractor.ceres')

##end Ceres

nthreads = 4
comdir2 = dict(language_level=3,
profile=True)

# galaxy.py for some reason can't handle infer_types.
cymod2 = cythonize(
['tractor/galaxy.py',],
annotate=True,
compiler_directives=comdir2,
nthreads=nthreads)

comdir1 = dict(language_level=3,
infer_types=True,
profile=True)
cymod1 = cythonize(
[
'tractor/patch.pyx',

'tractor/basics.py',
'tractor/brightness.py',
'tractor/ceres_optimizer.py',
'tractor/constrained_optimizer.py',
'tractor/dense_optimizer.py',
'tractor/ducks.py',
'tractor/ellipses.py',
'tractor/engine.py',
# 'tractor/galaxy.py',
'tractor/image.py',
'tractor/imageutils.py',
'tractor/lsqr_optimizer.py',
'tractor/mixture_profiles.py',
'tractor/motion.py',
'tractor/optimize.py',
'tractor/patch.py',
'tractor/pointsource.py',
'tractor/psf.py',
'tractor/psfex.py',
Expand All @@ -28,8 +136,28 @@
'tractor/tractortime.py',
'tractor/utils.py',
'tractor/wcs.py',
], annotate=True, compiler_directives=dict(language_level=3,
infer_types=True,
#profile=True
))
],
annotate=True,
compiler_directives=comdir1,
nthreads=nthreads)

# Reach into the distutils.core.Extension objects and set the compiler options...
for ext in cymod1 + cymod2:
for k,v in kwargs.items():
setattr(ext, k, v)

os.system('echo "version = \'$(git describe)\'" > tractor/version.py')
pymods.append('tractor.version')

setup(
name="tractor",
distclass=MyDistribution,
cmdclass={'build': CustomBuild},
version="git",
packages=['tractor', 'wise'],
package_dir={'wise':'wise', 'tractor':'tractor'},
package_data={'wise':['wise-psf-avg.fits', 'allsky-atlas.fits']},
ext_modules = mods + cymod1 + cymod2,
py_modules = pymods,
#zip_safe=False, # we hates it
)
22 changes: 18 additions & 4 deletions tractor/constrained_optimizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,27 @@ def __init__(self, *args, **kwargs):
super(ConstrainedOptimizer, self).__init__(*args, **kwargs)
self.stepLimited = False

def optimize_loop(self, tractor, dchisq=0., steps=50, **kwargs):
def optimize_loop(self, tractor, dchisq=0., steps=50,
dchisq_limited=1e-6, **kwargs):
# print()
# print('Optimize_loop:')
# for s in tractor.catalog:
# print(s)
R = {}
self.hitLimit = False
for step in range(steps):
#print('Opt loop step', step)
#print('Optimize_loop: step', step)
self.stepLimited = False
dlnp,_,_ = self.optimize(tractor, **kwargs)
#print('Optimize_loop: step', step, 'dlnp', dlnp, 'hit limit:',
# self.hitLimit, 'step limit:', self.stepLimited)
#for s in tractor.catalog:
# print(s)

if not self.stepLimited and dlnp <= dchisq:
break
if self.stepLimited and dlnp <= dchisq_limited:
break
R.update(steps=step)
R.update(hit_limit=self.hitLimit)
return R
Expand Down Expand Up @@ -61,6 +73,7 @@ def tryUpdates(self, tractor, X, alphas=None):
# print('Parameter', i, 'with initial value', p0[i],
# 'and update', X[i], 'would hit lower limit', l,
# 'with alpha', alpha, '; max alpha', a)
#print('Limiting step size to hit lower limit: param', i, 'limit', l, 'step size->', a)
maxalpha = min(maxalpha, a)
if u is not None and px > u:
# This parameter hits the limit; compute the step size
Expand All @@ -69,21 +82,22 @@ def tryUpdates(self, tractor, X, alphas=None):
# print('Parameter', i, 'with initial value', p0[i],
# 'and update', X[i], 'would hit upper limit', u,
# 'with alpha', alpha, '; max alpha', a)
#print('Limiting step size to hit upper limit: param', i, 'limit', u, 'step size->', a)
maxalpha = min(maxalpha, a)

for i,(d,m) in enumerate(zip(X, maxsteps)):
if m is None:
continue
#print('param', i, '
if alpha * np.abs(d) > m:
self.stepLimited = True
a = m / np.abs(d)
# print('Parameter', i, 'with update', X[i], 'x alpha', alpha, '=',
# X[i]*alpha, 'would exceed max step', m, '; max alpha', a)
maxalpha = min(maxalpha, a)
#print('Limiting step size for param max-step: param', i, 'max-step', m, 'step size->', a)

if maxalpha < 1e-8 and not self.stepLimited:
# print('Tiny maxalpha; bailing out without parameter update')
#print('Tiny maxalpha; bailing out without parameter update')
self.hitLimit = True
break

Expand Down

0 comments on commit 2250036

Please sign in to comment.