Commit 1297585d authored by Theo Steininger's avatar Theo Steininger
Browse files

Merge branch 'python3' into 'master'

Add Python3 compatibility

See merge request !156
parents da29a8f4 a476b875
Pipeline #17025 passed with stages
in 24 minutes and 49 seconds
# -*- coding: utf-8 -*-
from critical_power_curvature import CriticalPowerCurvature
from critical_power_energy import CriticalPowerEnergy
from .critical_power_curvature import CriticalPowerCurvature
from .critical_power_energy import CriticalPowerEnergy
# -*- coding: utf-8 -*-
from log_normal_wiener_filter_curvature import LogNormalWienerFilterCurvature
from log_normal_wiener_filter_energy import LogNormalWienerFilterEnergy
from .log_normal_wiener_filter_curvature import LogNormalWienerFilterCurvature
from .log_normal_wiener_filter_energy import LogNormalWienerFilterEnergy
# -*- coding: utf-8 -*-
from wiener_filter_curvature import WienerFilterCurvature
from wiener_filter_energy import WienerFilterEnergy
from .wiener_filter_curvature import WienerFilterCurvature
from .wiener_filter_energy import WienerFilterEnergy
......@@ -16,9 +16,9 @@
# NIFTy is being developed at the Max-Planck-Institut fuer Astrophysik
# and financially supported by the Studienstiftung des deutschen Volkes.
from line_searching import *
from conjugate_gradient import ConjugateGradient
from descent_minimizer import DescentMinimizer
from steepest_descent import SteepestDescent
from vl_bfgs import VL_BFGS
from relaxed_newton import RelaxedNewton
from .line_searching import *
from .conjugate_gradient import ConjugateGradient
from .descent_minimizer import DescentMinimizer
from .steepest_descent import SteepestDescent
from .vl_bfgs import VL_BFGS
from .relaxed_newton import RelaxedNewton
......@@ -16,6 +16,7 @@
# NIFTy is being developed at the Max-Planck-Institut fuer Astrophysik
# and financially supported by the Studienstiftung des deutschen Volkes.
from __future__ import division
import abc
from nifty.nifty_meta import NiftyMeta
......@@ -24,9 +25,10 @@ import numpy as np
from keepers import Loggable
from .line_searching import LineSearchStrongWolfe
from future.utils import with_metaclass
class DescentMinimizer(Loggable, object):
class DescentMinimizer(with_metaclass(NiftyMeta, type('NewBase', (Loggable, object), {}))):
""" A base class used by gradient methods to find a local minimum.
Descent minimization methods are used to find a local minimum of a scalar
......@@ -77,8 +79,6 @@ class DescentMinimizer(Loggable, object):
"""
__metaclass__ = NiftyMeta
def __init__(self, line_searcher=LineSearchStrongWolfe(), callback=None,
convergence_tolerance=1E-4, convergence_level=3,
iteration_limit=None):
......
......@@ -16,5 +16,5 @@
# NIFTy is being developed at the Max-Planck-Institut fuer Astrophysik
# and financially supported by the Studienstiftung des deutschen Volkes.
from line_search import LineSearch
from line_search_strong_wolfe import LineSearchStrongWolfe
from .line_search import LineSearch
from .line_search_strong_wolfe import LineSearchStrongWolfe
......@@ -21,9 +21,10 @@ import abc
from keepers import Loggable
from nifty import LineEnergy
from future.utils import with_metaclass
class LineSearch(Loggable, object):
class LineSearch(with_metaclass(abc.ABCMeta, type('NewBase', (Loggable, object), {}))):
"""Class for determining the optimal step size along some descent direction.
Initialize the line search procedure which can be used by a specific line
......
......@@ -16,6 +16,9 @@
# NIFTy is being developed at the Max-Planck-Institut fuer Astrophysik
# and financially supported by the Studienstiftung des deutschen Volkes.
from __future__ import print_function
from __future__ import division
from builtins import range
import numpy as np
from .line_search import LineSearch
......@@ -124,7 +127,7 @@ class LineSearchStrongWolfe(LineSearch):
alpha1 = 1.0/pk.norm()
# start the minimization loop
for i in xrange(self.max_iterations):
for i in range(self.max_iterations):
if alpha1 == 0:
self.logger.warn("Increment size became 0.")
return le_0.energy
......@@ -152,7 +155,7 @@ class LineSearchStrongWolfe(LineSearch):
# update alphas
alpha0, alpha1 = alpha1, min(2*alpha1, self.max_step_size)
if alpha1 == self.max_step_size:
print "reached max step size, bailing out"
print ("reached max step size, bailing out")
return le_alpha1.energy
phi_alpha0 = phi_alpha1
......@@ -207,7 +210,7 @@ class LineSearchStrongWolfe(LineSearch):
assert phi_lo <= phi_0 + self.c1*alpha_lo*phiprime_0
assert phiprime_lo*(alpha_hi-alpha_lo) < 0.
for i in xrange(self.max_zoom_iterations):
for i in range(self.max_zoom_iterations):
# assert phi_lo <= phi_0 + self.c1*alpha_lo*phiprime_0
# assert phiprime_lo*(alpha_hi-alpha_lo)<0.
delta_alpha = alpha_hi - alpha_lo
......
from __future__ import division
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
......@@ -16,6 +17,7 @@
# NIFTy is being developed at the Max-Planck-Institut fuer Astrophysik
# and financially supported by the Studienstiftung des deutschen Volkes.
from __future__ import division
from .descent_minimizer import DescentMinimizer
......
......@@ -16,6 +16,9 @@
# NIFTy is being developed at the Max-Planck-Institut fuer Astrophysik
# and financially supported by the Studienstiftung des deutschen Volkes.
from __future__ import division
from builtins import range
from builtins import object
import numpy as np
from .descent_minimizer import DescentMinimizer
......@@ -81,7 +84,7 @@ class VL_BFGS(DescentMinimizer):
delta = self._information_store.delta
descent_direction = delta[0] * b[0]
for i in xrange(1, len(delta)):
for i in range(1, len(delta)):
descent_direction += delta[i] * b[i]
return descent_direction
......@@ -154,14 +157,15 @@ class InformationStore(object):
result = []
m = self.history_length
mmax = self.max_history_length
k = self.k
k = self.k
s = self.s
for i in xrange(m):
for i in range(m):
result.append(s[(k-m+i) % mmax])
y = self.y
for i in xrange(m):
for i in range(m):
result.append(y[(k-m+i) % mmax])
result.append(self.last_gradient)
......@@ -188,18 +192,18 @@ class InformationStore(object):
# update the stores
k1 = (k-1) % mmax
for i in xrange(m):
for i in range(m):
kmi = (k-m+i) % mmax
self.ss[kmi, k1] = self.ss[k1, kmi] = self.s[kmi].vdot(self.s[k1])
self.yy[kmi, k1] = self.yy[k1, kmi] = self.y[kmi].vdot(self.y[k1])
self.sy[kmi, k1] = self.s[kmi].vdot(self.y[k1])
for j in xrange(m-1):
for j in range(m-1):
kmj = (k-m+j) % mmax
self.sy[k1, kmj] = self.s[k1].vdot(self.y[kmj])
for i in xrange(m):
for i in range(m):
kmi = (k-m+i) % mmax
for j in xrange(m):
for j in range(m):
kmj = (k-m+j) % mmax
result[i, j] = self.ss[kmi, kmj]
result[i, m+j] = result[m+j, i] = self.sy[kmi, kmj]
......@@ -233,16 +237,16 @@ class InformationStore(object):
alpha = np.empty(m, dtype=np.float)
for j in xrange(m-1, -1, -1):
delta_b_b = sum([delta[l] * b_dot_b[l, j] for l in xrange(2*m+1)])
for j in range(m-1, -1, -1):
delta_b_b = sum([delta[l] * b_dot_b[l, j] for l in range(2*m+1)])
alpha[j] = delta_b_b/b_dot_b[j, m+j]
delta[m+j] -= alpha[j]
for i in xrange(2*m+1):
for i in range(2*m+1):
delta[i] *= b_dot_b[m-1, 2*m-1]/b_dot_b[2*m-1, 2*m-1]
for j in xrange(m):
delta_b_b = sum([delta[l]*b_dot_b[m+j, l] for l in xrange(2*m+1)])
for j in range(m):
delta_b_b = sum([delta[l]*b_dot_b[m+j, l] for l in range(2*m+1)])
beta = delta_b_b/b_dot_b[j, m+j]
delta[j] += (alpha[j] - beta)
......
......@@ -17,7 +17,7 @@ class DocStringInheritor(type):
if doc:
clsdict['__doc__'] = doc
break
for attr, attribute in clsdict.items():
for attr, attribute in list(clsdict.items()):
if not attribute.__doc__:
for mro_cls in (mro_cls for base in bases
for mro_cls in base.mro()
......
......@@ -16,6 +16,8 @@
# NIFTy is being developed at the Max-Planck-Institut fuer Astrophysik
# and financially supported by the Studienstiftung des deutschen Volkes.
from builtins import next
from builtins import range
import numpy as np
from itertools import product
......@@ -53,7 +55,7 @@ def get_slice_list(shape, axes):
raise ValueError("axes(axis) does not match shape.")
axes_select = [0 if x in axes else 1 for x, y in enumerate(shape)]
axes_iterables = \
[range(y) for x, y in enumerate(shape) if x not in axes]
[list(range(y)) for x, y in enumerate(shape) if x not in axes]
for index in product(*axes_iterables):
it_iter = iter(index)
slice_list = [
......
......@@ -18,24 +18,24 @@
from __future__ import division
from linear_operator import LinearOperator
from .linear_operator import LinearOperator
from diagonal_operator import DiagonalOperator
from .diagonal_operator import DiagonalOperator
from endomorphic_operator import EndomorphicOperator
from .endomorphic_operator import EndomorphicOperator
from smoothing_operator import *
from .smoothing_operator import *
from fft_operator import *
from .fft_operator import *
from invertible_operator_mixin import InvertibleOperatorMixin
from .invertible_operator_mixin import InvertibleOperatorMixin
from projection_operator import ProjectionOperator
from .projection_operator import ProjectionOperator
from composed_operator import ComposedOperator
from .composed_operator import ComposedOperator
from response_operator import ResponseOperator
from .response_operator import ResponseOperator
from laplace_operator import LaplaceOperator
from .laplace_operator import LaplaceOperator
from smoothness_operator import SmoothnessOperator
from .smoothness_operator import SmoothnessOperator
......@@ -16,4 +16,4 @@
# NIFTy is being developed at the Max-Planck-Institut fuer Astrophysik
# and financially supported by the Studienstiftung des deutschen Volkes.
from composed_operator import ComposedOperator
from .composed_operator import ComposedOperator
......@@ -16,7 +16,8 @@
# NIFTy is being developed at the Max-Planck-Institut fuer Astrophysik
# and financially supported by the Studienstiftung des deutschen Volkes.
from nifty.operators.linear_operator import LinearOperator
from builtins import range
from ..linear_operator import LinearOperator
class ComposedOperator(LinearOperator):
......@@ -136,7 +137,7 @@ class ComposedOperator(LinearOperator):
def _times_helper(self, x, spaces, func):
space_index = 0
if spaces is None:
spaces = range(len(self.domain))
spaces = list(range(len(self.domain)))
for op in self._operator_store:
active_spaces = spaces[space_index:space_index+len(op.domain)]
space_index += len(op.domain)
......@@ -147,7 +148,7 @@ class ComposedOperator(LinearOperator):
def _inverse_times_helper(self, x, spaces, func):
space_index = 0
if spaces is None:
spaces = range(len(self.target))
spaces = list(range(len(self.target)))
rev_spaces = spaces[::-1]
for op in reversed(self._operator_store):
active_spaces = rev_spaces[space_index:space_index+len(op.target)]
......
......@@ -16,4 +16,4 @@
# NIFTy is being developed at the Max-Planck-Institut fuer Astrophysik
# and financially supported by the Studienstiftung des deutschen Volkes.
from diagonal_operator import DiagonalOperator
from .diagonal_operator import DiagonalOperator
......@@ -16,14 +16,16 @@
# NIFTy is being developed at the Max-Planck-Institut fuer Astrophysik
# and financially supported by the Studienstiftung des deutschen Volkes.
from __future__ import division
from builtins import range
import numpy as np
from d2o import distributed_data_object,\
STRATEGIES as DISTRIBUTION_STRATEGIES
from nifty.config import nifty_configuration as gc
from nifty.field import Field
from nifty.operators.endomorphic_operator import EndomorphicOperator
from ...config import nifty_configuration as gc
from ...field import Field
from ..endomorphic_operator import EndomorphicOperator
class DiagonalOperator(EndomorphicOperator):
......@@ -125,11 +127,11 @@ class DiagonalOperator(EndomorphicOperator):
operation=lambda z: z.adjoint().__mul__)
def _inverse_times(self, x, spaces):
return self._times_helper(x, spaces, operation=lambda z: z.__rdiv__)
return self._times_helper(x, spaces, operation=lambda z: z.__rtruediv__)
def _adjoint_inverse_times(self, x, spaces):
return self._times_helper(x, spaces,
operation=lambda z: z.adjoint().__rdiv__)
operation=lambda z: z.adjoint().__rtruediv__)
def diagonal(self, bare=False, copy=True):
""" Returns the diagonal of the Operator.
......@@ -269,7 +271,7 @@ class DiagonalOperator(EndomorphicOperator):
# the one of x, reshape the local data of self and apply it directly
active_axes = []
if spaces is None:
active_axes = range(len(x.shape))
active_axes = list(range(len(x.shape)))
else:
for space_index in spaces:
active_axes += x.domain_axes[space_index]
......@@ -287,7 +289,7 @@ class DiagonalOperator(EndomorphicOperator):
local_diagonal = redistr_diagonal_val.get_local_data(copy=False)
reshaper = [x.val.data.shape[i] if i in active_axes else 1
for i in xrange(len(x.shape))]
for i in range(len(x.shape))]
reshaped_local_diagonal = np.reshape(local_diagonal, reshaper)
# here the actual multiplication takes place
......
......@@ -16,4 +16,4 @@
# NIFTy is being developed at the Max-Planck-Institut fuer Astrophysik
# and financially supported by the Studienstiftung des deutschen Volkes.
from endomorphic_operator import EndomorphicOperator
from .endomorphic_operator import EndomorphicOperator
......@@ -18,7 +18,7 @@
import abc
from nifty.operators.linear_operator import LinearOperator
from ..linear_operator import LinearOperator
class EndomorphicOperator(LinearOperator):
......
......@@ -16,5 +16,5 @@
# NIFTy is being developed at the Max-Planck-Institut fuer Astrophysik
# and financially supported by the Studienstiftung des deutschen Volkes.
from transformations import *
from .transformations import *
from .fft_operator import FFTOperator
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment