rg_space.py 29.3 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) 2015 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

"""
    ..                  __   ____   __
    ..                /__/ /   _/ /  /_
    ..      __ ___    __  /  /_  /   _/  __   __
    ..    /   _   | /  / /   _/ /  /   /  / /  /
    ..   /  / /  / /  / /  /   /  /_  /  /_/  /
    ..  /__/ /__/ /__/ /__/    \___/  \___   /  rg
    ..                               /______/

Marco Selig's avatar
Marco Selig committed
31
    NIFTY submodule for regular Cartesian grids.
Marco Selig's avatar
Marco Selig committed
32
33
34

"""
from __future__ import division
Ultimanet's avatar
Ultimanet committed
35

Marco Selig's avatar
Marco Selig committed
36
import numpy as np
37
import os
38

Marco Selig's avatar
Marco Selig committed
39
40
41
import pylab as pl
from matplotlib.colors import LogNorm as ln
from matplotlib.ticker import LogFormatter as lf
Ultimanet's avatar
Ultimanet committed
42

43
44
from d2o import distributed_data_object,\
                STRATEGIES as DISTRIBUTION_STRATEGIES
45

46
47
from nifty.space import Space
from nifty.field import Field
csongor's avatar
csongor committed
48

49
50
51
from nifty.config import about,\
                         nifty_configuration as gc,\
                         dependency_injector as gdi
theos's avatar
theos committed
52
from rg_space_paradict import RGSpaceParadict
Ultima's avatar
Ultima committed
53
import nifty.nifty_utilities as utilities
54

Ultima's avatar
Ultima committed
55
MPI = gdi[gc['mpi_module']]
56
RG_DISTRIBUTION_STRATEGIES = DISTRIBUTION_STRATEGIES['global']
Ultimanet's avatar
Ultimanet committed
57

Marco Selig's avatar
Marco Selig committed
58

59
class RGSpace(Space):
Marco Selig's avatar
Marco Selig committed
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
    """
        ..      _____   _______
        ..    /   __/ /   _   /
        ..   /  /    /  /_/  /
        ..  /__/     \____  /  space class
        ..          /______/

        NIFTY subclass for spaces of regular Cartesian grids.

        Parameters
        ----------
        num : {int, numpy.ndarray}
            Number of gridpoints or numbers of gridpoints along each axis.
        naxes : int, *optional*
            Number of axes (default: None).
        zerocenter : {bool, numpy.ndarray}, *optional*
            Whether the Fourier zero-mode is located in the center of the grid
            (or the center of each axis speparately) or not (default: True).
        hermitian : bool, *optional*
            Whether the fields living in the space follow hermitian symmetry or
            not (default: True).
        purelyreal : bool, *optional*
            Whether the field values are purely real (default: True).
        dist : {float, numpy.ndarray}, *optional*
            Distance between two grid points along each axis (default: None).
        fourier : bool, *optional*
            Whether the space represents a Fourier or a position grid
            (default: False).

        Notes
        -----
        Only even numbers of grid points per axis are supported.
        The basis transformations between position `x` and Fourier mode `k`
        rely on (inverse) fast Fourier transformations using the
        :math:`exp(2 \pi i k^\dagger x)`-formulation.

        Attributes
        ----------
        para : numpy.ndarray
            One-dimensional array containing information on the axes of the
            space in the following form: The first entries give the grid-points
            along each axis in reverse order; the next entry is 0 if the
            fields defined on the space are purely real-valued, 1 if they are
            hermitian and complex, and 2 if they are not hermitian, but
            complex-valued; the last entries hold the information on whether
            the axes are centered on zero or not, containing a one for each
            zero-centered axis and a zero for each other one, in reverse order.
107
        dtype : numpy.dtype
Marco Selig's avatar
Marco Selig committed
108
109
110
111
112
113
114
115
116
117
118
119
120
            Data type of the field values for a field defined on this space,
            either ``numpy.float64`` or ``numpy.complex128``.
        discrete : bool
            Whether or not the underlying space is discrete, always ``False``
            for regular grids.
        vol : numpy.ndarray
            One-dimensional array containing the distances between two grid
            points along each axis, in reverse order. By default, the total
            length of each axis is assumed to be one.
        fourier : bool
            Whether or not the grid represents a Fourier basis.
    """

121
122
    def __init__(self, shape=(1,), zerocenter=False, distances=None,
                 harmonic=False, dtype=np.dtype('float'), ):
Marco Selig's avatar
Marco Selig committed
123
124
125
126
127
128
129
130
131
132
133
134
        """
            Sets the attributes for an rg_space class instance.

            Parameters
            ----------
            num : {int, numpy.ndarray}
                Number of gridpoints or numbers of gridpoints along each axis.
            naxes : int, *optional*
                Number of axes (default: None).
            zerocenter : {bool, numpy.ndarray}, *optional*
                Whether the Fourier zero-mode is located in the center of the
                grid (or the center of each axis speparately) or not
Ultimanet's avatar
Ultimanet committed
135
                (default: False).
Marco Selig's avatar
Marco Selig committed
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
            hermitian : bool, *optional*
                Whether the fields living in the space follow hermitian
                symmetry or not (default: True).
            purelyreal : bool, *optional*
                Whether the field values are purely real (default: True).
            dist : {float, numpy.ndarray}, *optional*
                Distance between two grid points along each axis
                (default: None).
            fourier : bool, *optional*
                Whether the space represents a Fourier or a position grid
                (default: False).

            Returns
            -------
            None
        """
152

theos's avatar
theos committed
153
154
155
156
        self.paradict = RGSpaceParadict(shape=shape,
                                        zerocenter=zerocenter,
                                        distances=distances,
                                        harmonic=harmonic)
157
        self.dtype = np.dtype(dtype)
theos's avatar
theos committed
158
159
160
161

    @property
    def harmonic(self):
        return self.paradict['harmonic']
162

163
    def copy(self):
164
165
        return RGSpace(dtype=self.dtype, harmonic=self.harmonic,
                       **self.paradict.parameters)
166

167
168
    @property
    def shape(self):
169
        return self.paradict['shape']
Marco Selig's avatar
Marco Selig committed
170

171
172
173
    @property
    def dim(self):
        return reduce(lambda x, y: x*y, self.shape)
174

175
176
177
    @property
    def total_volume(self):
        return self.dim * reduce(lambda x, y: x*y, self.paradict['distances'])
Marco Selig's avatar
Marco Selig committed
178

179
180
181
    def weight(self, x, power=1, axes=None):
        weight = reduce(lambda x, y: x*y, self.paradict['distances'])**power
        return x * weight
Marco Selig's avatar
Marco Selig committed
182

183
    def compute_k_array(self, distribution_strategy):
Marco Selig's avatar
Marco Selig committed
184
        """
185
186
            Calculates an n-dimensional array with its entries being the
            lengths of the k-vectors from the zero point of the grid.
Marco Selig's avatar
Marco Selig committed
187
188
189

            Parameters
            ----------
190
            None : All information is taken from the parent object.
Marco Selig's avatar
Marco Selig committed
191
192
193

            Returns
            -------
194
            nkdict : distributed_data_object
Marco Selig's avatar
Marco Selig committed
195
        """
196
197
198
199
200
201
202
203
204
205
206
207
208
        shape = self.shape
        # prepare the distributed_data_object
        nkdict = distributed_data_object(
                        global_shape=shape,
                        dtype=np.float128,
                        distribution_strategy=distribution_strategy)

        if distribution_strategy in DISTRIBUTION_STRATEGIES['slicing']:
            # get the node's individual slice of the first dimension
            slice_of_first_dimension = slice(
                                    *nkdict.distributor.local_slice[0:2])
        elif distribution_strategy in DISTRIBUTION_STRATEGIES['not']:
            slice_of_first_dimension = slice(0, shape[0])
Ultimanet's avatar
Ultimanet committed
209
        else:
210
            raise ValueError(about._errors.cstring(
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
                "ERROR: Unsupported distribution strategy"))
        dists = self._compute_k_array_helper(slice_of_first_dimension)
        nkdict.set_local_data(dists)

        return nkdict

    def _compute_k_array_helper(self, slice_of_first_dimension):
        dk = self.paradict['distances']
        shape = self.shape

        inds = []
        for a in shape:
            inds += [slice(0, a)]

        cords = np.ogrid[inds]

        dists = ((np.float128(0) + cords[0] - shape[0] // 2) * dk[0])**2
        # apply zerocenterQ shift
        if self.paradict['zerocenter'][0] == False:
            dists = np.fft.fftshift(dists)
        # only save the individual slice
        dists = dists[slice_of_first_dimension]
        for ii in range(1, len(shape)):
            temp = ((cords[ii] - shape[ii] // 2) * dk[ii])**2
            if self.paradict['zerocenter'][ii] == False:
                temp = np.fft.fftshift(temp)
            dists = dists + temp
        dists = np.sqrt(dists)
        return dists

    def smooth(self, x, sigma=0, codomain=None, axes=None):
Marco Selig's avatar
Marco Selig committed
242
243
244
245
246
247
248
249
250
251
252
253
        """
            Smoothes an array of field values by convolution with a Gaussian
            kernel.

            Parameters
            ----------
            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; for testing: a sigma of -1 will be
                reset to a reasonable value (default: 0).
254
255
            axes: None, tuple
                Axes which should be smoothed
Marco Selig's avatar
Marco Selig committed
256
257
258
259
260
261
262

            Returns
            -------
            Gx : numpy.ndarray
                Smoothed array.
        """

263
        # Check sigma
Ultimanet's avatar
Ultimanet committed
264
        if sigma == 0:
265
            return x.copy()
Ultimanet's avatar
Ultimanet committed
266
267
268
        elif sigma == -1:
            about.infos.cprint(
                "INFO: Resetting sigma to sqrt(2)*max(dist).")
269
            sigma = np.sqrt(2) * np.max(self.distances)
270
        elif(sigma < 0):
Marco Selig's avatar
Marco Selig committed
271
            raise ValueError(about._errors.cstring("ERROR: invalid sigma."))
Ultimanet's avatar
Ultimanet committed
272

273
        # if a codomain was given...
274
        if codomain is not None:
275
            # ...check if it was suitable
Ultimanet's avatar
Ultimanet committed
276
277
            if not self.check_codomain(codomain):
                raise ValueError(about._errors.cstring(
278
279
                    "ERROR: the given codomain is not a compatible!"))
        else:
Ultimanet's avatar
Ultimanet committed
280
281
            codomain = self.get_codomain()

282
283
284
        # TODO: Use the Fourier Transformation Operator for the switch into
        # hormonic space.
        raise NotImplementedError
285
286
287
        x = self.calc_transform(x, codomain=codomain, axes=axes)
        x = codomain._calc_smooth_helper(x, sigma, axes=axes)
        x = codomain.calc_transform(x, codomain=self, axes=axes)
288
        return x
289

290
    def _calc_smooth_helper(self, x, sigma, axes=None):
291
        # multiply the gaussian kernel, etc...
292
293
        if axes is None:
            axes = range(len(x.shape))
294
295

        # if x is hermitian it remains hermitian during smoothing
csongor's avatar
csongor committed
296
297
        # TODO look at this later
        # if self.datamodel in RG_DISTRIBUTION_STRATEGIES:
298
        remember_hermitianQ = x.hermitian
Ultimanet's avatar
Ultimanet committed
299

300
301
302
303
        # Define the Gaussian kernel function
        gaussian = lambda x: np.exp(-2. * np.pi**2 * x**2 * sigma**2)

        # Define the variables in the dialect of the legacy smoothing.py
304
        nx = np.array(self.shape)
305
        dx = 1 / nx / self.distances
306
        # Multiply the data along each axis with suitable the gaussian kernel
Ultimanet's avatar
Ultimanet committed
307
        for i in range(len(nx)):
308
309
            # Prepare the exponent
            dk = 1. / nx[i] / dx[i]
Ultimanet's avatar
Ultimanet committed
310
            nk = nx[i]
311
            k = -0.5 * nk * dk + np.arange(nk) * dk
Ultimanet's avatar
Ultimanet committed
312
313
            if self.paradict['zerocenter'][i] == False:
                k = np.fft.fftshift(k)
314
            # compute the actual kernel vector
Ultimanet's avatar
Ultimanet committed
315
            gaussian_kernel_vector = gaussian(k)
316
            # blow up the vector to an array of shape (1,.,1,len(nk),1,.,1)
317
318
            blown_up_shape = [1, ] * len(x.shape)
            blown_up_shape[axes[i]] = len(gaussian_kernel_vector)
Ultimanet's avatar
Ultimanet committed
319
320
            gaussian_kernel_vector =\
                gaussian_kernel_vector.reshape(blown_up_shape)
321
322
            # apply the blown-up gaussian_kernel_vector
            x = x*gaussian_kernel_vector
323

324
        try:
325
            x.hermitian = remember_hermitianQ
326
327
        except AttributeError:
            pass
328

Ultimanet's avatar
Ultimanet committed
329
        return x
Marco Selig's avatar
Marco Selig committed
330

Ultima's avatar
Ultima committed
331
332
    def get_plot(self,x,title="",vmin=None,vmax=None,power=None,unit="",
                 norm=None,cmap=None,cbar=True,other=None,legend=False,mono=True,**kwargs):
Marco Selig's avatar
Marco Selig committed
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
        """
            Creates a plot of field values according to the specifications
            given by the parameters.

            Parameters
            ----------
            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
                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).

        """
Ultima's avatar
Ultima committed
400

Marco Selig's avatar
Marco Selig committed
401
402
403
404
405
406
407
408
409
        if(not pl.isinteractive())and(not bool(kwargs.get("save",False))):
            about.warnings.cprint("WARNING: interactive mode off.")

        naxes = (np.size(self.para)-1)//2
        if(power is None):
            power = bool(self.para[naxes])

        if(power):
            x = self.calc_power(x,**kwargs)
Ultima's avatar
Ultima committed
410
411
412
413
            try:
                x = x.get_full_data()
            except AttributeError:
                pass
Marco Selig's avatar
Marco Selig committed
414

Ultima's avatar
Ultima committed
415
416
            fig = pl.figure(num=None,figsize=(6.4,4.8),dpi=None,
                            facecolor="none",edgecolor="none",frameon=False,FigureClass=pl.Figure)
Marco Selig's avatar
Marco Selig committed
417
418
419
420
421
422
423
            ax0 = fig.add_axes([0.12,0.12,0.82,0.76])

            ## explicit kindex
            xaxes = kwargs.get("kindex",None)
            ## implicit kindex
            if(xaxes is None):
                try:
Ultima's avatar
Ultima committed
424
425
                    self.power_indices
                    kindex_supply_space = self
Marco Selig's avatar
Marco Selig committed
426
                except:
Ultima's avatar
Ultima committed
427
428
429
                    kindex_supply_space = self.get_codomain()

                xaxes = kindex_supply_space.power_indices.get_index_dict(
Ultima's avatar
Ultima committed
430
                                                **kwargs)['kindex']
Ultima's avatar
Ultima committed
431
432
433
434
435
436
437
438
439
440


#                try:
#                    self.set_power_indices(**kwargs)
#                except:
#                    codomain = kwargs.get("codomain",self.get_codomain())
#                    codomain.set_power_indices(**kwargs)
#                    xaxes = codomain.power_indices.get("kindex")
#                else:
#                    xaxes = self.power_indices.get("kindex")
Marco Selig's avatar
Marco Selig committed
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478

            if(norm is None)or(not isinstance(norm,int)):
                norm = naxes
            if(vmin is None):
                vmin = np.min(x[:mono].tolist()+(xaxes**norm*x)[1:].tolist(),axis=None,out=None)
            if(vmax is None):
                vmax = np.max(x[:mono].tolist()+(xaxes**norm*x)[1:].tolist(),axis=None,out=None)
            ax0.loglog(xaxes[1:],(xaxes**norm*x)[1:],color=[0.0,0.5,0.0],label="graph 0",linestyle='-',linewidth=2.0,zorder=1)
            if(mono):
                ax0.scatter(0.5*(xaxes[1]+xaxes[2]),x[0],s=20,color=[0.0,0.5,0.0],marker='o',cmap=None,norm=None,vmin=None,vmax=None,alpha=None,linewidths=None,verts=None,zorder=1)

            if(other is not None):
                if(isinstance(other,tuple)):
                    other = list(other)
                    for ii in xrange(len(other)):
                        if(isinstance(other[ii],field)):
                            other[ii] = other[ii].power(**kwargs)
                        else:
                            other[ii] = self.enforce_power(other[ii],size=np.size(xaxes),kindex=xaxes)
                elif(isinstance(other,field)):
                    other = [other.power(**kwargs)]
                else:
                    other = [self.enforce_power(other,size=np.size(xaxes),kindex=xaxes)]
                imax = max(1,len(other)-1)
                for ii in xrange(len(other)):
                    ax0.loglog(xaxes[1:],(xaxes**norm*other[ii])[1:],color=[max(0.0,1.0-(2*ii/imax)**2),0.5*((2*ii-imax)/imax)**2,max(0.0,1.0-(2*(ii-imax)/imax)**2)],label="graph "+str(ii+1),linestyle='-',linewidth=1.0,zorder=-ii)
                    if(mono):
                        ax0.scatter(0.5*(xaxes[1]+xaxes[2]),other[ii][0],s=20,color=[max(0.0,1.0-(2*ii/imax)**2),0.5*((2*ii-imax)/imax)**2,max(0.0,1.0-(2*(ii-imax)/imax)**2)],marker='o',cmap=None,norm=None,vmin=None,vmax=None,alpha=None,linewidths=None,verts=None,zorder=-ii)
                if(legend):
                    ax0.legend()

            ax0.set_xlim(xaxes[1],xaxes[-1])
            ax0.set_xlabel(r"$|k|$")
            ax0.set_ylim(vmin,vmax)
            ax0.set_ylabel(r"$|k|^{%i} P_k$"%norm)
            ax0.set_title(title)

        else:
Ultima's avatar
Ultima committed
479
480
481
482
            try:
                x = x.get_full_data()
            except AttributeError:
                pass
Marco Selig's avatar
Marco Selig committed
483
484
485
486
            if(naxes==1):
                fig = pl.figure(num=None,figsize=(6.4,4.8),dpi=None,facecolor="none",edgecolor="none",frameon=False,FigureClass=pl.Figure)
                ax0 = fig.add_axes([0.12,0.12,0.82,0.76])

487
                xaxes = (np.arange(self.para[0],dtype=np.int)+self.para[2]*(self.para[0]//2))*self.distances
Marco Selig's avatar
Marco Selig committed
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
                if(vmin is None):
                    if(np.iscomplexobj(x)):
                        vmin = min(np.min(np.absolute(x),axis=None,out=None),np.min(np.real(x),axis=None,out=None),np.min(np.imag(x),axis=None,out=None))
                    else:
                        vmin = np.min(x,axis=None,out=None)
                if(vmax is None):
                    if(np.iscomplexobj(x)):
                        vmax = max(np.max(np.absolute(x),axis=None,out=None),np.max(np.real(x),axis=None,out=None),np.max(np.imag(x),axis=None,out=None))
                    else:
                        vmax = np.max(x,axis=None,out=None)
                if(norm=="log"):
                    ax0graph = ax0.semilogy
                    if(vmin<=0):
                        raise ValueError(about._errors.cstring("ERROR: nonpositive value(s)."))
                else:
                    ax0graph = ax0.plot

                if(np.iscomplexobj(x)):
                    ax0graph(xaxes,np.absolute(x),color=[0.0,0.5,0.0],label="graph (absolute)",linestyle='-',linewidth=2.0,zorder=1)
                    ax0graph(xaxes,np.real(x),color=[0.0,0.5,0.0],label="graph (real part)",linestyle="--",linewidth=1.0,zorder=0)
                    ax0graph(xaxes,np.imag(x),color=[0.0,0.5,0.0],label="graph (imaginary part)",linestyle=':',linewidth=1.0,zorder=0)
                    if(legend):
                        ax0.legend()
                elif(other is not None):
                    ax0graph(xaxes,x,color=[0.0,0.5,0.0],label="graph 0",linestyle='-',linewidth=2.0,zorder=1)
                    if(isinstance(other,tuple)):
514
                        other = [self._enforce_values(xx,extend=True) for xx in other]
Marco Selig's avatar
Marco Selig committed
515
                    else:
516
                        other = [self._enforce_values(other,extend=True)]
Marco Selig's avatar
Marco Selig committed
517
518
519
520
                    imax = max(1,len(other)-1)
                    for ii in xrange(len(other)):
                        ax0graph(xaxes,other[ii],color=[max(0.0,1.0-(2*ii/imax)**2),0.5*((2*ii-imax)/imax)**2,max(0.0,1.0-(2*(ii-imax)/imax)**2)],label="graph "+str(ii+1),linestyle='-',linewidth=1.0,zorder=-ii)
                    if("error" in kwargs):
521
                        error = self._enforce_values(np.absolute(kwargs.get("error")),extend=True)
Marco Selig's avatar
Marco Selig committed
522
523
524
525
526
527
                        ax0.fill_between(xaxes,x-error,x+error,color=[0.8,0.8,0.8],label="error 0",zorder=-len(other))
                    if(legend):
                        ax0.legend()
                else:
                    ax0graph(xaxes,x,color=[0.0,0.5,0.0],label="graph 0",linestyle='-',linewidth=2.0,zorder=1)
                    if("error" in kwargs):
528
                        error = self._enforce_values(np.absolute(kwargs.get("error")),extend=True)
Marco Selig's avatar
Marco Selig committed
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
                        ax0.fill_between(xaxes,x-error,x+error,color=[0.8,0.8,0.8],label="error 0",zorder=0)

                ax0.set_xlim(xaxes[0],xaxes[-1])
                ax0.set_xlabel("coordinate")
                ax0.set_ylim(vmin,vmax)
                if(unit):
                    unit = " ["+unit+"]"
                ax0.set_ylabel("values"+unit)
                ax0.set_title(title)

            elif(naxes==2):
                if(np.iscomplexobj(x)):
                    about.infos.cprint("INFO: absolute values and phases are plotted.")
                    if(title):
                        title += " "
                    if(bool(kwargs.get("save",False))):
                        save_ = os.path.splitext(os.path.basename(str(kwargs.get("save"))))
                        kwargs.update(save=save_[0]+"_absolute"+save_[1])
                    self.get_plot(np.absolute(x),title=title+"(absolute)",vmin=vmin,vmax=vmax,power=False,unit=unit,norm=norm,cmap=cmap,cbar=cbar,other=None,legend=False,**kwargs)
#                    self.get_plot(np.real(x),title=title+"(real part)",vmin=vmin,vmax=vmax,power=False,unit=unit,norm=norm,cmap=cmap,cbar=cbar,other=None,legend=False,**kwargs)
#                    self.get_plot(np.imag(x),title=title+"(imaginary part)",vmin=vmin,vmax=vmax,power=False,unit=unit,norm=norm,cmap=cmap,cbar=cbar,other=None,legend=False,**kwargs)
                    if(unit):
                        unit = "rad"
                    if(cmap is None):
                        cmap = pl.cm.hsv_r
                    if(bool(kwargs.get("save",False))):
                        kwargs.update(save=save_[0]+"_phase"+save_[1])
                    self.get_plot(np.angle(x,deg=False),title=title+"(phase)",vmin=-3.1416,vmax=3.1416,power=False,unit=unit,norm=None,cmap=cmap,cbar=cbar,other=None,legend=False,**kwargs) ## values in [-pi,pi]
                    return None ## leave method
                else:
                    if(vmin is None):
                        vmin = np.min(x,axis=None,out=None)
                    if(vmax is None):
                        vmax = np.max(x,axis=None,out=None)
                    if(norm=="log")and(vmin<=0):
                        raise ValueError(about._errors.cstring("ERROR: nonpositive value(s)."))

566
                    s_ = np.array([self.para[1]*self.distances[1]/np.max(self.para[:naxes]*self.distances,axis=None,out=None),self.para[0]*self.distances[0]/np.max(self.para[:naxes]*self.distances,axis=None,out=None)*(1.0+0.159*bool(cbar))])
Marco Selig's avatar
Marco Selig committed
567
568
569
                    fig = pl.figure(num=None,figsize=(6.4*s_[0],6.4*s_[1]),dpi=None,facecolor="none",edgecolor="none",frameon=False,FigureClass=pl.Figure)
                    ax0 = fig.add_axes([0.06/s_[0],0.06/s_[1],1.0-0.12/s_[0],1.0-0.12/s_[1]])

570
571
                    xaxes = (np.arange(self.para[1]+1,dtype=np.int)-0.5+self.para[4]*(self.para[1]//2))*self.distances[1]
                    yaxes = (np.arange(self.para[0]+1,dtype=np.int)-0.5+self.para[3]*(self.para[0]//2))*self.distances[0]
Marco Selig's avatar
Marco Selig committed
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
                    if(norm=="log"):
                        n_ = ln(vmin=vmin,vmax=vmax)
                    else:
                        n_ = None
                    sub = ax0.pcolormesh(xaxes,yaxes,x,cmap=cmap,norm=n_,vmin=vmin,vmax=vmax)
                    ax0.set_xlim(xaxes[0],xaxes[-1])
                    ax0.set_xticks([0],minor=False)
                    ax0.set_ylim(yaxes[0],yaxes[-1])
                    ax0.set_yticks([0],minor=False)
                    ax0.set_aspect("equal")
                    if(cbar):
                        if(norm=="log"):
                            f_ = lf(10,labelOnlyBase=False)
                            b_ = sub.norm.inverse(np.linspace(0,1,sub.cmap.N+1))
                            v_ = np.linspace(sub.norm.vmin,sub.norm.vmax,sub.cmap.N)
                        else:
                            f_ = None
                            b_ = None
                            v_ = None
                        cb0 = fig.colorbar(sub,ax=ax0,orientation="horizontal",fraction=0.1,pad=0.05,shrink=0.75,aspect=20,ticks=[vmin,vmax],format=f_,drawedges=False,boundaries=b_,values=v_)
                        cb0.ax.text(0.5,-1.0,unit,fontdict=None,withdash=False,transform=cb0.ax.transAxes,horizontalalignment="center",verticalalignment="center")
                    ax0.set_title(title)

            else:
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
                raise ValueError(about._errors.cstring("ERROR: unsupported number of axes ( "+str(naxes)+" > 2 )."))

        if(bool(kwargs.get("save",False))):
            fig.savefig(str(kwargs.get("save")),dpi=None,facecolor="none",edgecolor="none",orientation="portrait",papertype=None,format=None,transparent=False,bbox_inches=None,pad_inches=0.1)
            pl.close(fig)
        else:
            fig.canvas.draw()


    def _enforce_values(self, x, extend=True):
        """
            Computes valid field values from a given object, taking care of
            data types, shape, and symmetry.

            Parameters
            ----------
            x : {float, numpy.ndarray, nifty.field}
                Object to be transformed into an array of valid field values.

            Returns
            -------
            x : numpy.ndarray
                Array containing the valid field values.

            Other parameters
            ----------------
            extend : bool, *optional*
                Whether a scalar is extented to a constant array or not
                (default: True).
        """
        about.warnings.cflush(
            "WARNING: _enforce_values is deprecated function. Please use self.cast")
628
        if(isinstance(x, Field)):
629
630
631
632
633
634
635
636
637
638
639
640
641
            if(self == x.domain):
                if(self.dtype is not x.domain.dtype):
                    raise TypeError(about._errors.cstring("ERROR: inequal data types ( '" + str(
                        np.result_type(self.dtype)) + "' <> '" + str(np.result_type(x.domain.dtype)) + "' )."))
                else:
                    x = np.copy(x.val)
            else:
                raise ValueError(about._errors.cstring(
                    "ERROR: inequal domains."))
        else:
            if(np.size(x) == 1):
                if(extend):
                    x = self.dtype(
642
                        x) * np.ones(self.dim_split, dtype=self.dtype, order='C')
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
                else:
                    if(np.isscalar(x)):
                        x = np.array([x], dtype=self.dtype)
                    else:
                        x = np.array(x, dtype=self.dtype)
            else:
                x = self.enforce_shape(np.array(x, dtype=self.dtype))

        # hermitianize if ...
        if(about.hermitianize.status)and(np.size(x) != 1)and(self.para[(np.size(self.para) - 1) // 2] == 1):
            #x = gp.nhermitianize_fast(x,self.para[-((np.size(self.para)-1)//2):].astype(np.bool),special=False)
            x = utilities.hermitianize(x)
        # check finiteness
        if(not np.all(np.isfinite(x))):
            about.warnings.cprint("WARNING: infinite value(s).")

        return x