nifty_core.py 165 KB
Newer Older
1
2
# NIFTY (Numerical Information Field Theory) has been developed at the
# Max-Planck-Institute for Astrophysics.
Marco Selig's avatar
Marco Selig committed
3
##
4
# Copyright (C) 2013 Max-Planck-Society
Marco Selig's avatar
Marco Selig committed
5
##
6
7
# Author: Marco Selig
# Project homepage: <http://www.mpa-garching.mpg.de/ift/nifty/>
Marco Selig's avatar
Marco Selig committed
8
##
9
10
11
12
# 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.
Marco Selig's avatar
Marco Selig committed
13
##
14
15
16
17
# 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.
Marco Selig's avatar
Marco Selig committed
18
##
19
20
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
Marco Selig's avatar
Marco Selig committed
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44

"""
    ..                  __   ____   __
    ..                /__/ /   _/ /  /_
    ..      __ ___    __  /  /_  /   _/  __   __
    ..    /   _   | /  / /   _/ /  /   /  / /  /
    ..   /  / /  / /  / /  /   /  /_  /  /_/  /
    ..  /__/ /__/ /__/ /__/    \___/  \___   /  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
Marco Selig's avatar
Marco Selig committed
45
    to rapidly prototype algorithms in 1D and then apply the developed code in
Marco Selig's avatar
Marco Selig committed
46
47
48
49
50
    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.

51
52
53
54
55
56
57
    References
    ----------
    .. [#] Selig et al., "NIFTY -- Numerical Information Field Theory --
        a versatile Python library for signal inference",
        `A&A, vol. 554, id. A26 <http://dx.doi.org/10.1051/0004-6361/201321236>`_,
        2013; `arXiv:1301.4499 <http://www.arxiv.org/abs/1301.4499>`_

Marco Selig's avatar
Marco Selig committed
58
59
60
61
62
63
    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.

64
65
    .. Overview of all (core) classes:
    ..
Marco Selig's avatar
Marco Selig committed
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
    .. - 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

88
89
    Overview of the main classes and functions:

Marco Selig's avatar
Marco Selig committed
90
91
    .. automodule:: nifty

92
93
94
95
96
97
98
99
100
101
102
103
104
105
    - :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`
Marco Selig's avatar
Marco Selig committed
106

107
        .. currentmodule:: nifty.nifty_tools
Marco Selig's avatar
Marco Selig committed
108

109
110
        - :py:class:`invertible_operator`
        - :py:class:`propagator_operator`
Marco Selig's avatar
Marco Selig committed
111

112
        .. currentmodule:: nifty.nifty_explicit
Marco Selig's avatar
Marco Selig committed
113

114
        - :py:class:`explicit_operator`
Marco Selig's avatar
Marco Selig committed
115

116
    .. automodule:: nifty
Marco Selig's avatar
Marco Selig committed
117

118
119
120
    - :py:class:`probing`
        - :py:class:`trace_probing`
        - :py:class:`diagonal_probing`
Marco Selig's avatar
Marco Selig committed
121

122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
        .. 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`
Marco Selig's avatar
Marco Selig committed
141
142
143
144

"""
from __future__ import division
import numpy as np
Marco Selig's avatar
Marco Selig committed
145
import pylab as pl
146

147
from nifty_paradict import space_paradict,\
148
149
    point_space_paradict,\
    nested_space_paradict
Ultimanet's avatar
Ultimanet committed
150

151
152
from keepers import about,\
                    global_configuration
Ultimanet's avatar
Ultimanet committed
153
from nifty_random import random
154
155
from nifty.nifty_mpi_data import distributed_data_object,\
                                 STRATEGIES as DISTRIBUTION_STRATEGIES
156

157
import nifty.nifty_utilities as utilities
Marco Selig's avatar
Marco Selig committed
158

159
POINT_DISTRIBUTION_STRATEGIES = DISTRIBUTION_STRATEGIES['global']
Marco Selig's avatar
Marco Selig committed
160

Marco Selig's avatar
Marco Selig committed
161
pi = 3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679
162

Marco Selig's avatar
Marco Selig committed
163

164
# =============================================================================
Ultimanet's avatar
Ultimanet committed
165
166

class space(object):
Marco Selig's avatar
Marco Selig committed
167
    """
Ultimanet's avatar
Ultimanet committed
168
169
170
171
172
173
174
        ..     _______   ______    ____ __   _______   _______
        ..   /  _____/ /   _   | /   _   / /   ____/ /   __  /
        ..  /_____  / /  /_/  / /  /_/  / /  /____  /  /____/
        .. /_______/ /   ____/  \______|  \______/  \______/  class
        ..          /__/

        NIFTY base class for spaces and their discretizations.
Marco Selig's avatar
Marco Selig committed
175

Ultimanet's avatar
Ultimanet committed
176
177
178
        The base NIFTY space class is an abstract class from which other
        specific space subclasses, including those preimplemented in NIFTY
        (e.g. the regular grid class) must be derived.
Marco Selig's avatar
Marco Selig committed
179
180
181

        Parameters
        ----------
Ultimanet's avatar
Ultimanet committed
182
183
184
185
186
187
        para : {single object, list of objects}, *optional*
            This is a freeform list of parameters that derivatives of the space
            class can use (default: 0).
        datatype : numpy.dtype, *optional*
            Data type of the field values for a field defined on this space
            (default: numpy.float64).
Marco Selig's avatar
Marco Selig committed
188
189
190

        See Also
        --------
Ultimanet's avatar
Ultimanet committed
191
192
193
194
195
196
197
198
        point_space :  A class for unstructured lists of numbers.
        rg_space : A class for regular cartesian grids in arbitrary dimensions.
        hp_space : A class for the HEALPix discretization of the sphere
            [#]_.
        gl_space : A class for the Gauss-Legendre discretization of the sphere
            [#]_.
        lm_space : A class for spherical harmonic components.
        nested_space : A class for product spaces.
Marco Selig's avatar
Marco Selig committed
199

Ultimanet's avatar
Ultimanet committed
200
201
202
203
204
205
206
207
        References
        ----------
        .. [#] K.M. Gorski et al., 2005, "HEALPix: A Framework for
               High-Resolution Discretization and Fast Analysis of Data
               Distributed on the Sphere", *ApJ* 622..759G.
        .. [#] M. Reinecke and D. Sverre Seljebotn, 2013, "Libsharp - spherical
               harmonic transforms revisited";
               `arXiv:1303.4945 <http://www.arxiv.org/abs/1303.4945>`_
Marco Selig's avatar
Marco Selig committed
208
209
210

        Attributes
        ----------
Ultimanet's avatar
Ultimanet committed
211
212
213
214
215
216
217
218
219
220
        para : {single object, list of objects}
            This is a freeform list of parameters that derivatives of the space class can use.
        datatype : numpy.dtype
            Data type of the field values for a field defined on this space.
        discrete : bool
            Whether the space is inherently discrete (true) or a discretization
            of a continuous space (false).
        vol : numpy.ndarray
            An array of pixel volumes, only one component if the pixels all
            have the same volume.
Marco Selig's avatar
Marco Selig committed
221
    """
222

223
    def __init__(self, para=0, datatype=None):
Marco Selig's avatar
Marco Selig committed
224
        """
Ultimanet's avatar
Ultimanet committed
225
            Sets the attributes for a space class instance.
Marco Selig's avatar
Marco Selig committed
226
227
228

            Parameters
            ----------
Ultimanet's avatar
Ultimanet committed
229
230
231
232
233
234
            para : {single object, list of objects}, *optional*
                This is a freeform list of parameters that derivatives of the
                space class can use (default: 0).
            datatype : numpy.dtype, *optional*
                Data type of the field values for a field defined on this space
                (default: numpy.float64).
Marco Selig's avatar
Marco Selig committed
235

Ultimanet's avatar
Ultimanet committed
236
237
238
            Returns
            -------
            None
Marco Selig's avatar
Marco Selig committed
239
        """
240
        self.paradict = space_paradict(default=para)
Marco Selig's avatar
Marco Selig committed
241

242
        # check data type
Ultimanet's avatar
Ultimanet committed
243
244
        if(datatype is None):
            datatype = np.float64
245
        elif(datatype not in [np.int8, np.int16, np.int32, np.int64, np.float16, np.float32, np.float64, np.complex64, np.complex128]):
Ultimanet's avatar
Ultimanet committed
246
247
248
            about.warnings.cprint("WARNING: data type set to default.")
            datatype = np.float64
        self.datatype = datatype
Marco Selig's avatar
Marco Selig committed
249

Ultimanet's avatar
Ultimanet committed
250
        self.discrete = True
251
252
253
254
        self.harmonic = False
        self.vol = np.real(np.array([1], dtype=self.datatype))


Ultimanet's avatar
Ultimanet committed
255
256
257
    @property
    def para(self):
        return self.paradict['default']
258

Ultimanet's avatar
Ultimanet committed
259
260
261
    @para.setter
    def para(self, x):
        self.paradict['default'] = x
Marco Selig's avatar
Marco Selig committed
262

263
    # +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Ultimanet's avatar
Ultimanet committed
264
    def _freeze_config(self, dictionary):
Marco Selig's avatar
Marco Selig committed
265
        """
266
            a helper function which forms a hashable identifying object from
Ultimanet's avatar
Ultimanet committed
267
            a dictionary which can be used as key of a dict
268
        """
Ultimanet's avatar
Ultimanet committed
269
        return frozenset(dictionary.items())
Marco Selig's avatar
Marco Selig committed
270

271
    def copy(self):
272
273
        return space(para=self.para,
                     datatype=self.datatype)
Marco Selig's avatar
Marco Selig committed
274

275
    # +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Ultimanet's avatar
Ultimanet committed
276
    def getitem(self, data, key):
277
        raise NotImplementedError(about._errors.cstring(
Ultimanet's avatar
Ultimanet committed
278
            "ERROR: no generic instance method 'getitem'."))
Marco Selig's avatar
Marco Selig committed
279

280
    # +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Ultimanet's avatar
Ultimanet committed
281
    def setitem(self, data, key):
282
        raise NotImplementedError(about._errors.cstring(
Ultimanet's avatar
Ultimanet committed
283
            "ERROR: no generic instance method 'getitem'."))
284
285

    # +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Ultimanet's avatar
Ultimanet committed
286
    def apply_scalar_function(self, x, function, inplace=False):
287
        raise NotImplementedError(about._errors.cstring(
Ultimanet's avatar
Ultimanet committed
288
            "ERROR: no generic instance method 'apply_scalar_function'."))
Marco Selig's avatar
Marco Selig committed
289

290
    # +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Ultimanet's avatar
Ultimanet committed
291
    def unary_operation(self, x, op=None):
292
        raise NotImplementedError(about._errors.cstring(
Ultimanet's avatar
Ultimanet committed
293
            "ERROR: no generic instance method 'unary_operation'."))
294
295

    # +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Ultimanet's avatar
Ultimanet committed
296
    def binary_operation(self, x, y, op=None):
297
        raise NotImplementedError(about._errors.cstring(
Ultimanet's avatar
Ultimanet committed
298
            "ERROR: no generic instance method 'binary_operation'."))
Marco Selig's avatar
Marco Selig committed
299

300
    # +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
301
    def get_norm(self, x, q):
302
        raise NotImplementedError(about._errors.cstring(
Ultimanet's avatar
Ultimanet committed
303
            "ERROR: no generic instance method 'norm'."))
Marco Selig's avatar
Marco Selig committed
304

305
    def get_shape(self):
306
        raise NotImplementedError(about._errors.cstring(
Ultimanet's avatar
Ultimanet committed
307
            "ERROR: no generic instance method 'shape'."))
Marco Selig's avatar
Marco Selig committed
308

309
310
    # +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    def get_dim(self, split=False):
Marco Selig's avatar
Marco Selig committed
311
        """
Ultimanet's avatar
Ultimanet committed
312
            Computes the dimension of the space, i.e.\  the number of pixels.
Marco Selig's avatar
Marco Selig committed
313
314
315

            Parameters
            ----------
Ultimanet's avatar
Ultimanet committed
316
317
318
            split : bool, *optional*
                Whether to return the dimension split up, i.e. the numbers of
                pixels in each direction, or not (default: False).
Marco Selig's avatar
Marco Selig committed
319

Ultimanet's avatar
Ultimanet committed
320
321
322
323
            Returns
            -------
            dim : {int, numpy.ndarray}
                Dimension(s) of the space.
Marco Selig's avatar
Marco Selig committed
324
        """
325
        raise NotImplementedError(about._errors.cstring(
326
            "ERROR: no generic instance method 'dim'."))
Marco Selig's avatar
Marco Selig committed
327

328
    # +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Marco Selig's avatar
Marco Selig committed
329

330
    def get_dof(self):
Marco Selig's avatar
Marco Selig committed
331
        """
Ultimanet's avatar
Ultimanet committed
332
            Computes the number of degrees of freedom of the space.
Marco Selig's avatar
Marco Selig committed
333
334
335

            Returns
            -------
Ultimanet's avatar
Ultimanet committed
336
337
            dof : int
                Number of degrees of freedom of the space.
Marco Selig's avatar
Marco Selig committed
338
        """
339
        raise NotImplementedError(about._errors.cstring(
340
            "ERROR: no generic instance method 'dof'."))
Marco Selig's avatar
Marco Selig committed
341

342
    # +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Marco Selig's avatar
Marco Selig committed
343

344
    def enforce_power(self, spec, **kwargs):
Marco Selig's avatar
Marco Selig committed
345
        """
Ultimanet's avatar
Ultimanet committed
346
            Provides a valid power spectrum array from a given object.
Marco Selig's avatar
Marco Selig committed
347
348
349

            Parameters
            ----------
Ultimanet's avatar
Ultimanet committed
350
351
352
353
            spec : {scalar, list, numpy.ndarray, nifty.field, function}
                Fiducial power spectrum from which a valid power spectrum is to
                be calculated. Scalars are interpreted as constant power
                spectra.
Marco Selig's avatar
Marco Selig committed
354
355
356

            Returns
            -------
Ultimanet's avatar
Ultimanet committed
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
            spec : numpy.ndarray
                Valid power spectrum.

            Other parameters
            ----------------
            size : int, *optional*
                Number of bands the power spectrum shall have (default: None).
            kindex : numpy.ndarray, *optional*
                Scale of each band.
            codomain : nifty.space, *optional*
                A compatible codomain for power indexing (default: None).
            log : bool, *optional*
                Flag specifying if the spectral binning is performed on logarithmic
                scale or not; if set, the number of used bins is set
                automatically (if not given otherwise); by default no binning
                is done (default: None).
            nbin : integer, *optional*
                Number of used spectral bins; if given `log` is set to ``False``;
                integers below the minimum of 3 induce an automatic setting;
                by default no binning is done (default: None).
            binbounds : {list, array}, *optional*
                User specific inner boundaries of the bins, which are preferred
                over the above parameters; by default no binning is done
                (default: None).            vmin : {scalar, list, ndarray, field}, *optional*
                Lower limit of the uniform distribution if ``random == "uni"``
                (default: 0).
Marco Selig's avatar
Marco Selig committed
383
384

        """
385
        raise NotImplementedError(about._errors.cstring(
386
            "ERROR: no generic instance method 'enforce_power'."))
Marco Selig's avatar
Marco Selig committed
387

388
    # +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Ultimanet's avatar
Ultimanet committed
389

390
    def set_power_indices(self, **kwargs):
Marco Selig's avatar
Marco Selig committed
391
        """
Ultimanet's avatar
Ultimanet committed
392
            Sets the (un)indexing objects for spectral indexing internally.
Marco Selig's avatar
Marco Selig committed
393
394
395

            Parameters
            ----------
Ultimanet's avatar
Ultimanet committed
396
397
398
399
400
401
402
403
404
405
406
407
408
            log : bool
                Flag specifying if the binning is performed on logarithmic
                scale or not; if set, the number of used bins is set
                automatically (if not given otherwise); by default no binning
                is done (default: None).
            nbin : integer
                Number of used bins; if given `log` is set to ``False``;
                integers below the minimum of 3 induce an automatic setting;
                by default no binning is done (default: None).
            binbounds : {list, array}
                User specific inner boundaries of the bins, which are preferred
                over the above parameters; by default no binning is done
                (default: None).
Marco Selig's avatar
Marco Selig committed
409
410
411
412
413

            Returns
            -------
            None

Ultimanet's avatar
Ultimanet committed
414
415
416
417
            See Also
            --------
            get_power_indices

Marco Selig's avatar
Marco Selig committed
418
        """
419
        raise NotImplementedError(about._errors.cstring(
420
            "ERROR: no generic instance method 'set_power_indices'."))
Marco Selig's avatar
Marco Selig committed
421

422
    def get_power_indices(self, **kwargs):
Marco Selig's avatar
Marco Selig committed
423
        """
Ultimanet's avatar
Ultimanet committed
424
425
426
427
428
429
            Provides the (un)indexing objects for spectral indexing.

            Provides one-dimensional arrays containing the scales of the
            spectral bands and the numbers of modes per scale, and an array
            giving for each component of a field the corresponding index of a
            power spectrum as well as an Unindexing array.
Marco Selig's avatar
Marco Selig committed
430
431
432

            Parameters
            ----------
Ultimanet's avatar
Ultimanet committed
433
434
435
436
437
438
439
440
441
442
443
444
445
            log : bool
                Flag specifying if the binning is performed on logarithmic
                scale or not; if set, the number of used bins is set
                automatically (if not given otherwise); by default no binning
                is done (default: None).
            nbin : integer
                Number of used bins; if given `log` is set to ``False``;
                integers below the minimum of 3 induce an automatic setting;
                by default no binning is done (default: None).
            binbounds : {list, array}
                User specific inner boundaries of the bins, which are preferred
                over the above parameters; by default no binning is done
                (default: None).
Marco Selig's avatar
Marco Selig committed
446
447
448

            Returns
            -------
Ultimanet's avatar
Ultimanet committed
449
450
451
452
453
454
455
456
457
            kindex : numpy.ndarray
                Scale of each spectral band.
            rho : numpy.ndarray
                Number of modes per scale represented in the discretization.
            pindex : numpy.ndarray
                Indexing array giving the power spectrum index for each
                represented mode.
            pundex : numpy.ndarray
                Unindexing array undoing power spectrum indexing.
Marco Selig's avatar
Marco Selig committed
458

Ultimanet's avatar
Ultimanet committed
459
460
461
462
463
464
465
            Notes
            -----
            The ``kindex`` and ``rho`` are each one-dimensional arrays.
            The indexing array is of the same shape as a field living in this
            space and contains the indices of the associated bands.
            Indexing with the unindexing array undoes the indexing with the
            indexing array; i.e., ``power == power[pindex].flatten()[pundex]``.
Marco Selig's avatar
Marco Selig committed
466

Ultimanet's avatar
Ultimanet committed
467
468
469
            See Also
            --------
            set_power_indices
Marco Selig's avatar
Marco Selig committed
470
471

        """
472
        raise NotImplementedError(about._errors.cstring(
473
474
475
            "ERROR: no generic instance method 'get_power_indices'."))

    # +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Marco Selig's avatar
Marco Selig committed
476

Ultimanet's avatar
Ultimanet committed
477
    def cast(self, x, verbose=False):
Marco Selig's avatar
Marco Selig committed
478
        """
Ultimanet's avatar
Ultimanet committed
479
            Computes valid field values from a given object, trying
480
481
            to translate the given data into a valid form. Thereby it is as
            benevolent as possible.
Marco Selig's avatar
Marco Selig committed
482
483
484

            Parameters
            ----------
Ultimanet's avatar
Ultimanet committed
485
486
            x : {float, numpy.ndarray, nifty.field}
                Object to be transformed into an array of valid field values.
Marco Selig's avatar
Marco Selig committed
487
488
489

            Returns
            -------
Ultimanet's avatar
Ultimanet committed
490
491
492
            x : numpy.ndarray, distributed_data_object
                Array containing the field values, which are compatible to the
                space.
Marco Selig's avatar
Marco Selig committed
493

Ultimanet's avatar
Ultimanet committed
494
495
496
            Other parameters
            ----------------
            verbose : bool, *optional*
497
                Whether the method should raise a warning if information is
Ultimanet's avatar
Ultimanet committed
498
                lost during casting (default: False).
Marco Selig's avatar
Marco Selig committed
499
        """
Ultimanet's avatar
Ultimanet committed
500
        return self.enforce_values(x, extend=True)
Marco Selig's avatar
Marco Selig committed
501

502
    # +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Marco Selig's avatar
Marco Selig committed
503

504
    def enforce_shape(self, x):
Marco Selig's avatar
Marco Selig committed
505
        """
Ultimanet's avatar
Ultimanet committed
506
507
            Shapes an array of valid field values correctly, according to the
            specifications of the space instance.
Marco Selig's avatar
Marco Selig committed
508

Ultimanet's avatar
Ultimanet committed
509
510
511
512
            Parameters
            ----------
            x : numpy.ndarray
                Array containing the field values to be put into shape.
Marco Selig's avatar
Marco Selig committed
513

Ultimanet's avatar
Ultimanet committed
514
515
516
517
518
            Returns
            -------
            y : numpy.ndarray
                Correctly shaped array.
        """
519
520
        raise NotImplementedError(about._errors.cstring(
            "ERROR: no generic instance method 'enforce_shape'."))
Marco Selig's avatar
Marco Selig committed
521

522
    # +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Marco Selig's avatar
Marco Selig committed
523

524
    def enforce_values(self, x, extend=True):
Ultimanet's avatar
Ultimanet committed
525
526
527
        """
            Computes valid field values from a given object, according to the
            constraints from the space instance.
Marco Selig's avatar
Marco Selig committed
528

Ultimanet's avatar
Ultimanet committed
529
530
531
532
            Parameters
            ----------
            x : {float, numpy.ndarray, nifty.field}
                Object to be transformed into an array of valid field values.
Marco Selig's avatar
Marco Selig committed
533

Ultimanet's avatar
Ultimanet committed
534
535
536
537
            Returns
            -------
            x : numpy.ndarray
                Array containing the valid field values.
Marco Selig's avatar
Marco Selig committed
538

Ultimanet's avatar
Ultimanet committed
539
540
541
542
543
544
            Other parameters
            ----------------
            extend : bool, *optional*
                Whether a scalar is extented to a constant array or not
                (default: True).
        """
545
546
        raise NotImplementedError(about._errors.cstring(
            "ERROR: no generic instance method 'enforce_values'."))
Marco Selig's avatar
Marco Selig committed
547

548
    # +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Marco Selig's avatar
Marco Selig committed
549

550
    def get_random_values(self, **kwargs):
Marco Selig's avatar
Marco Selig committed
551
        """
Ultimanet's avatar
Ultimanet committed
552
553
            Generates random field values according to the specifications given
            by the parameters.
Marco Selig's avatar
Marco Selig committed
554

Ultimanet's avatar
Ultimanet committed
555
556
557
558
559
560
561
            Returns
            -------
            x : numpy.ndarray
                Valid field values.

            Other parameters
            ----------------
Marco Selig's avatar
Marco Selig committed
562
            random : string, *optional*
Ultimanet's avatar
Ultimanet committed
563
564
565
                Specifies the probability distribution from which the random
                numbers are to be drawn.
                Supported distributions are:
Marco Selig's avatar
Marco Selig committed
566
567

                - "pm1" (uniform distribution over {+1,-1} or {+1,+i,-1,-i}
Ultimanet's avatar
Ultimanet committed
568
569
                - "gau" (normal distribution with zero-mean and a given standard
                    deviation or variance)
Marco Selig's avatar
Marco Selig committed
570
571
572
573
                - "syn" (synthesizes from a given power spectrum)
                - "uni" (uniform distribution over [vmin,vmax[)

                (default: None).
Ultimanet's avatar
Ultimanet committed
574
575
576
577
578
579
580
            dev : float, *optional*
                Standard deviation (default: 1).
            var : float, *optional*
                Variance, overriding `dev` if both are specified
                (default: 1).
            spec : {scalar, list, numpy.ndarray, nifty.field, function}, *optional*
                Power spectrum (default: 1).
581
582
583
584
            pindex : numpy.ndarray, *optional*
                Indexing array giving the power spectrum index of each band
                (default: None).
            kindex : numpy.ndarray, *optional*
Ultimanet's avatar
Ultimanet committed
585
                Scale of each band (default: None).
586
            codomain : nifty.space, *optional*
Ultimanet's avatar
Ultimanet committed
587
                A compatible codomain with power indices (default: None).
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
            log : bool, *optional*
                Flag specifying if the spectral binning is performed on logarithmic
                scale or not; if set, the number of used bins is set
                automatically (if not given otherwise); by default no binning
                is done (default: None).
            nbin : integer, *optional*
                Number of used spectral bins; if given `log` is set to ``False``;
                integers below the minimum of 3 induce an automatic setting;
                by default no binning is done (default: None).
            binbounds : {list, array}, *optional*
                User specific inner boundaries of the bins, which are preferred
                over the above parameters; by default no binning is done
                (default: None).            vmin : {scalar, list, ndarray, field}, *optional*
                Lower limit of the uniform distribution if ``random == "uni"``
                (default: 0).
Ultimanet's avatar
Ultimanet committed
603
604
605
606
            vmin : float, *optional*
                Lower limit for a uniform distribution (default: 0).
            vmax : float, *optional*
                Upper limit for a uniform distribution (default: 1).
Marco Selig's avatar
Marco Selig committed
607
        """
608
609
        raise NotImplementedError(about._errors.cstring(
            "ERROR: no generic instance method 'get_random_values'."))
Marco Selig's avatar
Marco Selig committed
610

611
    # +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Marco Selig's avatar
Marco Selig committed
612

613
    def check_codomain(self, codomain):
Marco Selig's avatar
Marco Selig committed
614
        """
Ultimanet's avatar
Ultimanet committed
615
            Checks whether a given codomain is compatible to the space or not.
Marco Selig's avatar
Marco Selig committed
616
617
618

            Parameters
            ----------
Ultimanet's avatar
Ultimanet committed
619
620
            codomain : nifty.space
                Space to be checked for compatibility.
Marco Selig's avatar
Marco Selig committed
621
622
623

            Returns
            -------
Ultimanet's avatar
Ultimanet committed
624
625
            check : bool
                Whether or not the given codomain is compatible to the space.
Marco Selig's avatar
Marco Selig committed
626
        """
627
628
        raise NotImplementedError(about._errors.cstring(
            "ERROR: no generic instance method 'check_codomain'."))
Marco Selig's avatar
Marco Selig committed
629

630
    # +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Marco Selig's avatar
Marco Selig committed
631

632
    def get_codomain(self, **kwargs):
Marco Selig's avatar
Marco Selig committed
633
        """
Ultimanet's avatar
Ultimanet committed
634
635
636
            Generates a compatible codomain to which transformations are
            reasonable, usually either the position basis or the basis of
            harmonic eigenmodes.
Marco Selig's avatar
Marco Selig committed
637
638
639

            Parameters
            ----------
Ultimanet's avatar
Ultimanet committed
640
641
642
643
644
645
646
647
648
            coname : string, *optional*
                String specifying a desired codomain (default: None).
            cozerocenter : {bool, numpy.ndarray}, *optional*
                Whether or not the grid is zerocentered for each axis or not
                (default: None).
            conest : list, *optional*
                List of nested spaces of the codomain (default: None).
            coorder : list, *optional*
                Permutation of the list of nested spaces (default: None).
Marco Selig's avatar
Marco Selig committed
649
650
651

            Returns
            -------
Ultimanet's avatar
Ultimanet committed
652
653
654
            codomain : nifty.space
                A compatible codomain.
        """
655
656
        raise NotImplementedError(about._errors.cstring(
            "ERROR: no generic instance method 'get_codomain'."))
Marco Selig's avatar
Marco Selig committed
657

658
    # +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Marco Selig's avatar
Marco Selig committed
659

660
    def get_meta_volume(self, total=False):
Marco Selig's avatar
Marco Selig committed
661
        """
Ultimanet's avatar
Ultimanet committed
662
            Calculates the meta volumes.
Marco Selig's avatar
Marco Selig committed
663

Ultimanet's avatar
Ultimanet committed
664
665
666
667
            The meta volumes are the volumes associated with each component of
            a field, taking into account field components that are not
            explicitly included in the array of field values but are determined
            by symmetry conditions.
Marco Selig's avatar
Marco Selig committed
668

Ultimanet's avatar
Ultimanet committed
669
670
671
672
673
            Parameters
            ----------
            total : bool, *optional*
                Whether to return the total meta volume of the space or the
                individual ones of each field component (default: False).
Marco Selig's avatar
Marco Selig committed
674

Ultimanet's avatar
Ultimanet committed
675
676
677
678
679
            Returns
            -------
            mol : {numpy.ndarray, float}
                Meta volume of the field components or the complete space.
        """
680
681
        raise NotImplementedError(about._errors.cstring(
            "ERROR: no generic instance method 'get_meta_volume'."))
Marco Selig's avatar
Marco Selig committed
682

683
    # +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Marco Selig's avatar
Marco Selig committed
684

685
    def calc_weight(self, x, power=1):
Marco Selig's avatar
Marco Selig committed
686
        """
Ultimanet's avatar
Ultimanet committed
687
688
            Weights a given array of field values with the pixel volumes (not
            the meta volumes) to a given power.
Marco Selig's avatar
Marco Selig committed
689
690
691

            Parameters
            ----------
Ultimanet's avatar
Ultimanet committed
692
693
694
695
            x : numpy.ndarray
                Array to be weighted.
            power : float, *optional*
                Power of the pixel volumes to be used (default: 1).
Marco Selig's avatar
Marco Selig committed
696
697
698

            Returns
            -------
Ultimanet's avatar
Ultimanet committed
699
700
701
            y : numpy.ndarray
                Weighted array.
        """
702
703
        raise NotImplementedError(about._errors.cstring(
            "ERROR: no generic instance method 'calc_weight'."))
Marco Selig's avatar
Marco Selig committed
704

705
    def get_weight(self, power=1):
706
707
        raise NotImplementedError(about._errors.cstring(
            "ERROR: no generic instance method 'get_weight'."))
Marco Selig's avatar
Marco Selig committed
708

709
710
711
    # +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

    def calc_dot(self, x, y):
Ultimanet's avatar
Ultimanet committed
712
713
714
        """
            Computes the discrete inner product of two given arrays of field
            values.
Marco Selig's avatar
Marco Selig committed
715

Ultimanet's avatar
Ultimanet committed
716
717
718
719
720
721
            Parameters
            ----------
            x : numpy.ndarray
                First array
            y : numpy.ndarray
                Second array
Marco Selig's avatar
Marco Selig committed
722

Ultimanet's avatar
Ultimanet committed
723
724
725
726
727
            Returns
            -------
            dot : scalar
                Inner product of the two arrays.
        """
728
        raise NotImplementedError(about._errors.cstring(
Ultimanet's avatar
Ultimanet committed
729
            "ERROR: no generic instance method 'dot'."))
Marco Selig's avatar
Marco Selig committed
730

731
    # +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Marco Selig's avatar
Marco Selig committed
732

733
    def calc_transform(self, x, codomain=None, **kwargs):
Ultimanet's avatar
Ultimanet committed
734
735
        """
            Computes the transform of a given array of field values.
Marco Selig's avatar
Marco Selig committed
736
737
738

            Parameters
            ----------
Ultimanet's avatar
Ultimanet committed
739
740
741
            x : numpy.ndarray
                Array to be transformed.
            codomain : nifty.space, *optional*
742
                codomain space to which the transformation shall map
Ultimanet's avatar
Ultimanet committed
743
                (default: self).
Marco Selig's avatar
Marco Selig committed
744
745
746

            Returns
            -------
Ultimanet's avatar
Ultimanet committed
747
748
            Tx : numpy.ndarray
                Transformed array
749

Ultimanet's avatar
Ultimanet committed
750
751
752
753
            Other parameters
            ----------------
            iter : int, *optional*
                Number of iterations performed in specific transformations.
754
        """
755
756
        raise NotImplementedError(about._errors.cstring(
            "ERROR: no generic instance method 'calc_transform'."))
Marco Selig's avatar
Marco Selig committed
757

758
    # +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
759

760
    def calc_smooth(self, x, sigma=0, **kwargs):
Marco Selig's avatar
Marco Selig committed
761
        """
Ultimanet's avatar
Ultimanet committed
762
763
            Smoothes an array of field values by convolution with a Gaussian
            kernel.
Marco Selig's avatar
Marco Selig committed
764
765
766

            Parameters
            ----------
Ultimanet's avatar
Ultimanet committed
767
768
769
770
771
            x : numpy.ndarray
                Array of field values to be smoothed.
            sigma : float, *optional*
                Standard deviation of the Gaussian kernel, specified in units
                of length in position space (default: 0).
Marco Selig's avatar
Marco Selig committed
772
773
774

            Returns
            -------
Ultimanet's avatar
Ultimanet committed
775
776
            Gx : numpy.ndarray
                Smoothed array.
Marco Selig's avatar
Marco Selig committed
777

Ultimanet's avatar
Ultimanet committed
778
779
780
781
            Other parameters
            ----------------
            iter : int, *optional*
                Number of iterations (default: 0).
Marco Selig's avatar
Marco Selig committed
782
        """
783
784
        raise NotImplementedError(about._errors.cstring(
            "ERROR: no generic instance method 'calc_smooth'."))
Marco Selig's avatar
Marco Selig committed
785

786
    # +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Marco Selig's avatar
Marco Selig committed
787

788
    def calc_power(self, x, **kwargs):
Marco Selig's avatar
Marco Selig committed
789
        """
Ultimanet's avatar
Ultimanet committed
790
            Computes the power of an array of field values.
Marco Selig's avatar
Marco Selig committed
791
792
793

            Parameters
            ----------
Ultimanet's avatar
Ultimanet committed
794
795
796
            x : numpy.ndarray
                Array containing the field values of which the power is to be
                calculated.
Marco Selig's avatar
Marco Selig committed
797
798
799
800

            Returns
            -------
            spec : numpy.ndarray
Ultimanet's avatar
Ultimanet committed
801
                Power contained in the input array.
Marco Selig's avatar
Marco Selig committed
802
803
804

            Other parameters
            ----------------
Ultimanet's avatar
Ultimanet committed
805
806
807
            pindex : numpy.ndarray, *optional*
                Indexing array assigning the input array components to
                components of the power spectrum (default: None).
808
            kindex : numpy.ndarray, *optional*
Ultimanet's avatar
Ultimanet committed
809
810
811
812
                Scale corresponding to each band in the power spectrum
                (default: None).
            rho : numpy.ndarray, *optional*
                Number of degrees of freedom per band (default: None).
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
            codomain : nifty.space, *optional*
                A compatible codomain for power indexing (default: None).
            log : bool, *optional*
                Flag specifying if the spectral binning is performed on logarithmic
                scale or not; if set, the number of used bins is set
                automatically (if not given otherwise); by default no binning
                is done (default: None).
            nbin : integer, *optional*
                Number of used spectral bins; if given `log` is set to ``False``;
                integers below the minimum of 3 induce an automatic setting;
                by default no binning is done (default: None).
            binbounds : {list, array}, *optional*
                User specific inner boundaries of the bins, which are preferred
                over the above parameters; by default no binning is done
                (default: None).            vmin : {scalar, list, ndarray, field}, *optional*
                Lower limit of the uniform distribution if ``random == "uni"``
                (default: 0).
830

Marco Selig's avatar
Marco Selig committed
831
        """
832
833
        raise NotImplementedError(about._errors.cstring(
            "ERROR: no generic instance method 'calc_power'."))
Marco Selig's avatar
Marco Selig committed
834

835
    # +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Marco Selig's avatar
Marco Selig committed
836

837
    def get_plot(self, x, **kwargs):
Marco Selig's avatar
Marco Selig committed
838
        """
Ultimanet's avatar
Ultimanet committed
839
840
            Creates a plot of field values according to the specifications
            given by the parameters.
841
842
843

            Parameters
            ----------
Ultimanet's avatar
Ultimanet committed
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
            x : numpy.ndarray
                Array containing the field values.

            Returns
            -------
            None

            Other parameters
            ----------------
            title : string, *optional*
                Title of the plot (default: "").
            vmin : float, *optional*
                Minimum value to be displayed (default: ``min(x)``).
            vmax : float, *optional*
                Maximum value to be displayed (default: ``max(x)``).
            power : bool, *optional*
                Whether to plot the power contained in the field or the field
                values themselves (default: False).
            unit : string, *optional*
                Unit of the field values (default: "").
            norm : string, *optional*
                Scaling of the field values before plotting (default: None).
            cmap : matplotlib.colors.LinearSegmentedColormap, *optional*
                Color map to be used for two-dimensional plots (default: None).
            cbar : bool, *optional*
                Whether to show the color bar or not (default: True).
            other : {single object, tuple of objects}, *optional*
                Object or tuple of objects to be added, where objects can be
                scalars, arrays, or fields (default: None).
            legend : bool, *optional*
                Whether to show the legend or not (default: False).
            mono : bool, *optional*
                Whether to plot the monopole or not (default: True).
            save : string, *optional*
                Valid file name where the figure is to be stored, by default
                the figure is not saved (default: False).
            error : {float, numpy.ndarray, nifty.field}, *optional*
                Object indicating some confidence interval to be plotted
                (default: None).
            kindex : numpy.ndarray, *optional*
                Scale corresponding to each band in the power spectrum
                (default: None).
            codomain : nifty.space, *optional*
                A compatible codomain for power indexing (default: None).
            log : bool, *optional*
                Flag specifying if the spectral binning is performed on logarithmic
890
891
892
                scale or not; if set, the number of used bins is set
                automatically (if not given otherwise); by default no binning
                is done (default: None).
Ultimanet's avatar
Ultimanet committed
893
894
            nbin : integer, *optional*
                Number of used spectral bins; if given `log` is set to ``False``;
895
                integers below the minimum of 3 induce an automatic setting;
896
                by default no binning is done (default: None).
Ultimanet's avatar
Ultimanet committed
897
            binbounds : {list, array}, *optional*
898
899
                User specific inner boundaries of the bins, which are preferred
                over the above parameters; by default no binning is done
Ultimanet's avatar
Ultimanet committed
900
901
902
903
904
                (default: None).            vmin : {scalar, list, ndarray, field}, *optional*
                Lower limit of the uniform distribution if ``random == "uni"``
                (default: 0).
            iter : int, *optional*
                Number of iterations (default: 0).
Marco Selig's avatar
Marco Selig committed
905
906

        """
907
908
        raise NotImplementedError(about._errors.cstring(
            "ERROR: no generic instance method 'get_plot'."))
Marco Selig's avatar
Marco Selig committed
909

910
    # +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Marco Selig's avatar
Marco Selig committed
911

Ultimanet's avatar
Ultimanet committed
912
913
    def __repr__(self):
        return "<nifty_core.space>"
Marco Selig's avatar
Marco Selig committed
914

Ultimanet's avatar
Ultimanet committed
915
    def __str__(self):
916
        return "nifty_core.space instance\n- para     = " + str(self.para) + "\n- datatype = numpy." + str(np.result_type(self.datatype))
Marco Selig's avatar
Marco Selig committed
917

918
    # +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Marco Selig's avatar
Marco Selig committed
919

Ultimanet's avatar
Ultimanet committed
920
    def __len__(self):
921
        return int(self.get_dim(split=False))
Marco Selig's avatar
Marco Selig committed
922

923
924
925
    # _identiftier returns an object which contains all information needed
    # to uniquely idetnify a space. It returns a (immutable) tuple which therefore
    # can be compored.
926
    def _identifier(self):
Ultimanet's avatar
Ultimanet committed
927
        return tuple(sorted(vars(self).items()))
Marco Selig's avatar
Marco Selig committed
928

929
    # +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Marco Selig's avatar
Marco Selig committed
930

931
932
933
934
    def _meta_vars(self):  # > captures all nonstandard properties
        mars = np.array([ii[1] for ii in vars(self).iteritems() if ii[0] not in [
                        "para", "datatype", "discrete", "vol", "power_indices"]], dtype=np.object)
        if(np.size(mars) == 0):
Ultimanet's avatar
Ultimanet committed
935
936
937
            return None
        else:
            return mars
Marco Selig's avatar
Marco Selig committed
938

939
    def __eq__(self, x):  # __eq__ : self == x
940
941
942
943
        if isinstance(x, type(self)):
            return self._identifier() == x._identifier()
        else:
            return False
944

Ultima's avatar
Ultima committed
945
946
    def __ne__(self, x):
        return not self.__eq__(x)
Marco Selig's avatar
Marco Selig committed
947

948
949
950
951
952
953
    def __lt__(self, x):  # __lt__ : self < x
        if(isinstance(x, space)):
            if(not isinstance(x, type(self)))or(np.size(self.para) != np.size(x.para))or(np.size(self.vol) != np.size(x.vol)):
                raise ValueError(about._errors.cstring(
                    "ERROR: incomparable spaces."))
            elif(self.discrete == x.discrete):  # data types are ignored
Ultimanet's avatar
Ultimanet committed
954
                for ii in xrange(np.size(self.para)):
955
                    if(self.para[ii] < x.para[ii]):
Ultimanet's avatar
Ultimanet committed
956
                        return True
957
                    elif(self.para[ii] > x.para[ii]):
Ultimanet's avatar
Ultimanet committed
958
959
                        return False
                for ii in xrange(np.size(self.vol)):
960
                    if(self.vol[ii] < x.vol[ii]):
Ultimanet's avatar
Ultimanet committed
961
                        return True
962
                    elif(self.vol[ii] > x.vol[ii]):
Ultimanet's avatar
Ultimanet committed
963
964
965
966
                        return False
                s_mars = self._meta_vars()
                x_mars = x._meta_vars()
                for ii in xrange(np.size(s_mars)):
967
                    if(np.all(s_mars[ii] < x_mars[ii])):
Ultimanet's avatar
Ultimanet committed
968
                        return True
969
                    elif(np.any(s_mars[ii] > x_mars[ii])):
Ultimanet's avatar
Ultimanet committed
970
971
                        break
        return False
Marco Selig's avatar
Marco Selig committed
972

973
974
975
976
977
978
    def __le__(self, x):  # __le__ : self <= x
        if(isinstance(x, space)):
            if(not isinstance(x, type(self)))or(np.size(self.para) != np.size(x.para))or(np.size(self.vol) != np.size(x.vol)):
                raise ValueError(about._errors.cstring(
                    "ERROR: incomparable spaces."))
            elif(self.discrete == x.discrete):  # data types are ignored
Ultimanet's avatar
Ultimanet committed
979
                for ii in xrange(np.size(self.para)):
980
                    if(self.para[ii] < x.para[ii]):
Ultimanet's avatar
Ultimanet committed
981
                        return True
982
                    if(self.para[ii] > x.para[ii]):
Ultimanet's avatar
Ultimanet committed
983
984
                        return False
                for ii in xrange(np.size(self.vol)):
985
                    if(self.vol[ii] < x.vol[ii]):
Ultimanet's avatar
Ultimanet committed
986
                        return True
987
                    if(self.vol[ii] > x.vol[ii]):
Ultimanet's avatar
Ultimanet committed
988
989
990
991
                        return False
                s_mars = self._meta_vars()
                x_mars = x._meta_vars()
                for ii in xrange(np.size(s_mars)):
992
                    if(np.all(s_mars[ii] < x_mars[ii])):
Ultimanet's avatar
Ultimanet committed
993
                        return True
994
                    elif(np.any(s_mars[ii] > x_mars[ii])):
Ultimanet's avatar
Ultimanet committed
995
996
997
                        return False
                return True
        return False
Marco Selig's avatar
Marco Selig committed
998

999
1000
1001
1002
1003
1004
    def __gt__(self, x):  # __gt__ : self > x
        if(isinstance(x, space)):
            if(not isinstance(x, type(self)))or(np.size(self.para) != np.size(x.para))or(np.size(self.vol) != np.size(x.vol)):
                raise ValueError(about._errors.cstring(
                    "ERROR: incomparable spaces."))
            elif(self.discrete == x.discrete):  # data types are ignored
Ultimanet's avatar
Ultimanet committed
1005
                for ii in xrange(np.size(self.para)):
1006
                    if(self.para[ii] > x.para[ii]):
Ultimanet's avatar
Ultimanet committed
1007
                        return True
1008
                    elif(self.para[ii] < x.para[ii]):
Ultimanet's avatar
Ultimanet committed
1009
1010
                        break
                for ii in xrange(np.size(self.vol)):
1011
                    if(self.vol[ii] > x.vol[ii]):
Ultimanet's avatar
Ultimanet committed
1012
                        return True
1013
                    elif(self.vol[ii] < x.vol[ii]):
Ultimanet's avatar
Ultimanet committed
1014
1015
1016
1017
                        break
                s_mars = self._meta_vars()
                x_mars = x._meta_vars()
                for ii in xrange(np.size(s_mars)):
1018
                    if(np.all(s_mars[ii] > x_mars[ii])):
Ultimanet's avatar
Ultimanet committed
1019
                        return True
1020
                    elif(np.any(s_mars[ii] < x_mars[ii])):
Ultimanet's avatar
Ultimanet committed
1021
1022
                        break
        return False
Marco Selig's avatar
Marco Selig committed
1023

1024
1025
1026
1027
1028
1029
    def __ge__(self, x):  # __ge__ : self >= x
        if(isinstance(x, space)):
            if(not isinstance(x, type(self)))or(np.size(self.para) != np.size(x.para))or(np.size(self.vol) != np.size(x.vol)):
                raise ValueError(about._errors.cstring(
                    "ERROR: incomparable spaces."))
            elif(self.discrete == x.discrete):  # data types are ignored
Ultimanet's avatar
Ultimanet committed
1030
                for ii in xrange(np.size(self.para)):
1031
                    if(self.para[ii] > x.para[ii]):
Ultimanet's avatar
Ultimanet committed
1032
                        return True
1033
                    if(self.para[ii] < x.para[ii]):
Ultimanet's avatar
Ultimanet committed
1034
1035
                        return False
                for ii in xrange(np.size(self.vol)):
1036
                    if(self.vol[ii] > x.vol[ii]):
Ultimanet's avatar
Ultimanet committed
1037
                        return True
1038
                    if(self.vol[ii] < x.vol[ii]):
Ultimanet's avatar
Ultimanet committed
1039
1040
1041
1042
                        return False
                s_mars = self._meta_vars()
                x_mars = x._meta_vars()
                for ii in xrange(np.size(s_mars)):
1043
                    if(np.all(s_mars[ii] > x_mars[ii])):
Ultimanet's avatar
Ultimanet committed
1044
                        return True
1045
                    elif(np.any(s_mars[ii] < x_mars[ii])):
Ultimanet's avatar
Ultimanet committed
1046
1047
1048
                        return False
                return True
        return False
Marco Selig's avatar
Marco Selig committed
1049

1050
# =============================================================================
Marco Selig's avatar
Marco Selig committed
1051
1052


1053
# -----------------------------------------------------------------------------
Marco Selig's avatar
Marco Selig committed
1054

Ultimanet's avatar
Ultimanet committed
1055
class point_space(space):
Marco Selig's avatar
Marco Selig committed
1056
    """
Ultimanet's avatar
Ultimanet committed
1057
1058
1059
1060
1061
1062
1063
        ..                            __             __
        ..                          /__/           /  /_
        ..      ______    ______    __   __ ___   /   _/
        ..    /   _   | /   _   | /  / /   _   | /  /
        ..   /  /_/  / /  /_/  / /  / /  / /  / /  /_
        ..  /   ____/  \______/ /__/ /__/ /__/  \___/  space class
        .. /__/
Marco Selig's avatar
Marco Selig committed
1064

Ultimanet's avatar
Ultimanet committed
1065
        NIFTY subclass for unstructured spaces.
Marco Selig's avatar
Marco Selig committed
1066

Ultimanet's avatar
Ultimanet committed
1067
1068
        Unstructured spaces are lists of values without any geometrical
        information.
Marco Selig's avatar
Marco Selig committed
1069
1070
1071

        Parameters
        ----------
Ultimanet's avatar
Ultimanet committed
1072
1073
1074
1075
        num : int
            Number of points.
        datatype : numpy.dtype, *optional*
            Data type of the field values (default: None).
Marco Selig's avatar
Marco Selig committed
1076