# NIFTY (Numerical Information Field Theory) has been developed at the
# Max-Planck-Institute for Astrophysics.
##
# Copyright (C) 2013 Max-Planck-Society
##
# Author: Marco Selig
# Project homepage:
##
# 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
# (at your option) any later version.
##
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the GNU General Public License for more details.
##
# You should have received a copy of the GNU General Public License
# along with this program. If not, see .
"""
.. __ ____ __
.. /__/ / _/ / /_
.. __ ___ __ / /_ / _/ __ __
.. / _ | / / / _/ / / / / / /
.. / / / / / / / / / /_ / /_/ /
.. /__/ /__/ /__/ /__/ \___/ \___ / core
.. /______/
.. The NIFTY project homepage is http://www.mpa-garching.mpg.de/ift/nifty/
NIFTY [#]_, "Numerical Information Field Theory", is a versatile
library designed to enable the development of signal inference algorithms
that operate regardless of the underlying spatial grid and its resolution.
Its object-oriented framework is written in Python, although it accesses
libraries written in Cython, C++, and C for efficiency.
NIFTY offers a toolkit that abstracts discretized representations of
continuous spaces, fields in these spaces, and operators acting on fields
into classes. Thereby, the correct normalization of operations on fields is
taken care of automatically without concerning the user. This allows for an
abstract formulation and programming of inference algorithms, including
those derived within information field theory. Thus, NIFTY permits its user
to rapidly prototype algorithms in 1D and then apply the developed code in
higher-dimensional settings of real world problems. The set of spaces on
which NIFTY operates comprises point sets, n-dimensional regular grids,
spherical spaces, their harmonic counterparts, and product spaces
constructed as combinations of those.
References
----------
.. [#] Selig et al., "NIFTY -- Numerical Information Field Theory --
a versatile Python library for signal inference",
`A&A, vol. 554, id. A26 `_,
2013; `arXiv:1301.4499 `_
Class & Feature Overview
------------------------
The NIFTY library features three main classes: **spaces** that represent
certain grids, **fields** that are defined on spaces, and **operators**
that apply to fields.
.. Overview of all (core) classes:
..
.. - switch
.. - notification
.. - _about
.. - random
.. - space
.. - point_space
.. - rg_space
.. - lm_space
.. - gl_space
.. - hp_space
.. - nested_space
.. - field
.. - operator
.. - diagonal_operator
.. - power_operator
.. - projection_operator
.. - vecvec_operator
.. - response_operator
.. - probing
.. - trace_probing
.. - diagonal_probing
Overview of the main classes and functions:
.. automodule:: nifty
- :py:class:`space`
- :py:class:`point_space`
- :py:class:`rg_space`
- :py:class:`lm_space`
- :py:class:`gl_space`
- :py:class:`hp_space`
- :py:class:`nested_space`
- :py:class:`field`
- :py:class:`operator`
- :py:class:`diagonal_operator`
- :py:class:`power_operator`
- :py:class:`projection_operator`
- :py:class:`vecvec_operator`
- :py:class:`response_operator`
.. currentmodule:: nifty.nifty_tools
- :py:class:`invertible_operator`
- :py:class:`propagator_operator`
.. currentmodule:: nifty.nifty_explicit
- :py:class:`explicit_operator`
.. automodule:: nifty
- :py:class:`probing`
- :py:class:`trace_probing`
- :py:class:`diagonal_probing`
.. currentmodule:: nifty.nifty_explicit
- :py:class:`explicit_probing`
.. currentmodule:: nifty.nifty_tools
- :py:class:`conjugate_gradient`
- :py:class:`steepest_descent`
.. currentmodule:: nifty.nifty_explicit
- :py:func:`explicify`
.. currentmodule:: nifty.nifty_power
- :py:func:`weight_power`,
:py:func:`smooth_power`,
:py:func:`infer_power`,
:py:func:`interpolate_power`
"""
from __future__ import division
import abc
import numpy as np
from keepers import Loggable,\
Versionable
class Space(Versionable, Loggable, Plottable, object):
"""
.. __ __
.. /__/ / /_
.. ______ ______ __ __ ___ / _/
.. / _ | / _ | / / / _ | / /
.. / /_/ / / /_/ / / / / / / / / /_
.. / ____/ \______/ /__/ /__/ /__/ \___/ space class
.. /__/
NIFTY subclass for unstructured spaces.
Unstructured spaces are lists of values without any geometrical
information.
Parameters
----------
num : int
Number of points.
dtype : numpy.dtype, *optional*
Data type of the field values (default: None).
Attributes
----------
para : numpy.ndarray
Array containing the number of points.
dtype : numpy.dtype
Data type of the field values.
discrete : bool
Parameter captioning the fact that a :py:class:`point_space` is
always discrete.
vol : numpy.ndarray
Pixel volume of the :py:class:`point_space`, which is always 1.
"""
__metaclass__ = abc.ABCMeta
def __init__(self, dtype=np.dtype('float')):
"""
Sets the attributes for a point_space class instance.
Parameters
----------
num : int
Number of points.
dtype : numpy.dtype, *optional*
Data type of the field values (default: numpy.float64).
Returns
-------
None.
"""
# parse dtype
casted_dtype = np.result_type(dtype, np.float64)
if casted_dtype != dtype:
self.Logger.warning("Input dtype reset to: %s" % str(casted_dtype))
self.dtype = casted_dtype
self._ignore_for_hash = ['_global_id']
def __hash__(self):
# Extract the identifying parts from the vars(self) dict.
result_hash = 0
for key in sorted(vars(self).keys()):
item = vars(self)[key]
if key in self._ignore_for_hash or key == '_ignore_for_hash':
continue
result_hash ^= item.__hash__() ^ int(hash(key)/117)
return result_hash
def __eq__(self, x):
if isinstance(x, type(self)):
return hash(self) == hash(x)
else:
return False
def __ne__(self, x):
return not self.__eq__(x)
@abc.abstractproperty
def harmonic(self):
raise NotImplementedError
@abc.abstractproperty
def shape(self):
raise NotImplementedError(
"There is no generic shape for the Space base class.")
@abc.abstractproperty
def dim(self):
raise NotImplementedError(
"There is no generic dim for the Space base class.")
@abc.abstractproperty
def total_volume(self):
raise NotImplementedError(
"There is no generic volume for the Space base class.")
@abc.abstractmethod
def copy(self):
return self.__class__(dtype=self.dtype)
@abc.abstractmethod
def weight(self, x, power=1, axes=None, inplace=False):
"""
Weights a given array of field values with the pixel volumes (not
the meta volumes) to a given power.
Parameters
----------
x : numpy.ndarray
Array to be weighted.
power : float, *optional*
Power of the pixel volumes to be used (default: 1).
Returns
-------
y : numpy.ndarray
Weighted array.
"""
raise NotImplementedError
def pre_cast(self, x, axes=None):
return x
def post_cast(self, x, axes=None):
return x
def get_distance_array(self, distribution_strategy):
raise NotImplementedError(
"There is no generic distance structure for Space base class.")
def get_fft_smoothing_kernel_function(self, sigma):
raise NotImplementedError(
"There is no generic co-smoothing kernel for Space base class.")
def hermitian_decomposition(self, x, axes=None):
raise NotImplementedError
def __repr__(self):
string = ""
string += str(type(self)) + "\n"
string += "dtype: " + str(self.dtype) + "\n"
return string
# ---Serialization---
def _to_hdf5(self, hdf5_group):
hdf5_group.attrs['dtype'] = self.dtype.name
return None
@classmethod
def _from_hdf5(cls, hdf5_group, repository):
result = cls(dtype=np.dtype(hdf5_group.attrs['dtype']))
return result