Commit 0d5fe1e4 authored by Philipp Arras's avatar Philipp Arras
Browse files

python3 compatibility and pep8

parent 529a486d
import nifty4 as ift
import numpy as np
from matplotlib import pyplot as plt
from astropy.io import fits
from separation_energy import SeparationEnergy
from matplotlib import pyplot as plt
from nifty4.library.nonlinearities import PositiveTanh
from .separation_energy import SeparationEnergy
def load_data(path):
if path[-5:] == '.fits':
data = fits.open(path)[1].data
else:
data = plt.imread(path)[:,:,0]
data = plt.imread(path)[:, :, 0]
data = data.clip(min=0.001)
data = np.ndarray.astype(data, float)
return data
def build_problem(data, alpha):
s_space = ift.RGSpace(data.shape, distances=len(data.shape) * [1])
h_space = s_space.get_default_codomain()
data = ift.Field(s_space,val=data)
data = ift.Field(s_space, val=data)
FFT = ift.FFTOperator(h_space)
binbounds = ift.PowerSpace.useful_binbounds(h_space, logarithmic = False)
binbounds = ift.PowerSpace.useful_binbounds(h_space, logarithmic=False)
p_space = ift.PowerSpace(h_space, binbounds=binbounds)
initial_spectrum = ift.power_analyze(FFT.inverse_times(ift.log(data)), binbounds=p_space.binbounds)
initial_spectrum = ift.power_analyze(FFT.inverse_times(ift.log(data)),
binbounds=p_space.binbounds)
initial_correlation = ift.create_power_operator(h_space, initial_spectrum)
initial_x = ift.Field(s_space, val=-1.)
alpha = ift.Field(s_space, val=alpha)
......@@ -39,36 +43,40 @@ def build_problem(data, alpha):
separationEnergy = SeparationEnergy(position=initial_x, parameters=parameters)
return separationEnergy
def problem_iteration(energy, iterations=3):
controller = ift.GradientNormController(name="test1", tol_abs_gradnorm=0.00000001, iteration_limit=iterations)
controller = ift.GradientNormController(name="test1",
tol_abs_gradnorm=0.00000001,
iteration_limit=iterations)
minimizer = ift.RelaxedNewton(controller=controller)
energy, convergence = minimizer(energy)
new_position = energy.position
h_space = energy.correlation.domain[0]
FFT = energy.FFT
binbounds = ift.PowerSpace.useful_binbounds(h_space, logarithmic=False)
new_power = ift.power_analyze(FFT.inverse_times(energy.s), binbounds=binbounds)
new_power = ift.power_analyze(FFT.inverse_times(energy.s),
binbounds=binbounds)
new_correlation = ift.create_power_operator(h_space, new_power)
new_parameters = energy.parameters
new_parameters['correlation'] = new_correlation
new_energy = SeparationEnergy(new_position, new_parameters)
return new_energy
def build_multi_problem(data, alpha):
energy_list = []
for i in range(data.shape[-1]):
energy = build_problem(data[...,i],alpha)
energy = build_problem(data[..., i], alpha)
energy_list.append(energy)
return energy_list
def multi_problem_iteration(energy_list):
new_energy = []
for energy in energy_list:
new_energy.append(problem_iteration(energy))
return new_energy
if __name__ == '__main__':
pass
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