From cb8589c7957b856cd8532daad2bef01b9f7ec72e Mon Sep 17 00:00:00 2001 From: theos <theo.steininger@ultimanet.de> Date: Fri, 19 Aug 2016 23:42:43 +0200 Subject: [PATCH] Fixed a lot of small bugs. Removed the plotting routines from most of the spaces. Fixed furthermore: - Let's undo the changes in the RGSpace.__init__ (see the comment on gitlab) - The HPSpaceParadict still has `distances` in the list. - HPSpace.copy: Is this method really necessary? The one from Space should be sufficient, right? - HPSpace.total_volume is missing - Unnecessary imports of `random`, `LMSpace`,... in hp_space.py - I removed the plotting functionality from the space classes. - GLSpace.copy should also not be necessary - GLSpace.dim is missing - GLSpace.vol should be called `total_volume`. Also its implementation is defect: there is no self.distances for GLSpace. The total volume is just 4*pi. - Use nifty_utilities.cast_axis_to_tuple in order to sanitize `axes` input in GLSpace.weight - typo in `new_shape = np.ones(x.shape)` --- nifty/field.py | 1 - nifty/spaces/gl_space/gl_space.py | 238 ++------------ nifty/spaces/hp_space/hp_space.py | 205 +----------- nifty/spaces/hp_space/hp_space_paradict.py | 2 +- nifty/spaces/rg_space/rg_space.py | 354 +-------------------- 5 files changed, 34 insertions(+), 766 deletions(-) diff --git a/nifty/field.py b/nifty/field.py index 041b28236..61bb4f937 100644 --- a/nifty/field.py +++ b/nifty/field.py @@ -15,7 +15,6 @@ from nifty.field_types import FieldType,\ from nifty.spaces.space import Space import nifty.nifty_utilities as utilities -from nifty_random import random POINT_DISTRIBUTION_STRATEGIES = DISTRIBUTION_STRATEGIES['global'] COMM = getattr(gdi[gc['mpi_module']], gc['default_comm']) diff --git a/nifty/spaces/gl_space/gl_space.py b/nifty/spaces/gl_space/gl_space.py index 9d5f1ae7b..5c82b082d 100644 --- a/nifty/spaces/gl_space/gl_space.py +++ b/nifty/spaces/gl_space/gl_space.py @@ -2,19 +2,14 @@ from __future__ import division import itertools import numpy as np -import pylab as pl -from matplotlib.colors import LogNorm as ln -from matplotlib.ticker import LogFormatter as lf from d2o import STRATEGIES as DISTRIBUTION_STRATEGIES -from nifty.spaces.lm_space import LMSpace - from nifty.spaces.space import Space from nifty.config import about, nifty_configuration as gc,\ dependency_injector as gdi from gl_space_paradict import GLSpaceParadict -from nifty.nifty_random import random +import nifty.nifty_utilities as utilities gl = gdi.get('libsharp_wrapper_gl') @@ -74,7 +69,7 @@ class GLSpace(Space): An array containing the pixel sizes. """ - def __init__(self, nlat, nlon=None, dtype=np.dtype('float64')): + def __init__(self, nlat, nlon=None, dtype=np.dtype('float')): """ Sets the attributes for a gl_space class instance. @@ -107,45 +102,38 @@ class GLSpace(Space): # setup paradict self.paradict = GLSpaceParadict(nlat=nlat, nlon=nlon) - # check and set data type - dtype = np.dtype(dtype) - if dtype not in [np.dtype('float32'), np.dtype('float64')]: - about.warnings.cprint("WARNING: data type set to default.") - dtype = np.dtype('float') - self.dtype = dtype + # setup dtype + self.dtype = np.dtype(dtype) # GLSpace is not harmonic self._harmonic = False - def copy(self): - return GLSpace(nlat=self.paradict['nlat'], - nlon=self.paradict['nlon'], - dtype=self.dtype) - @property def shape(self): return (np.int((self.paradict['nlat'] * self.paradict['nlon'])),) @property - def vol(self): - return np.sum(self.paradict['nlon'] * np.array(self.distances[0])) + def dim(self): + return np.int((self.paradict['nlat'] * self.paradict['nlon'])) + + @property + def total_volume(self): + return 4 * np.pi def weight(self, x, power=1, axes=None, inplace=False): - # check if the axes provided are valid given the input shape - if axes is not None and \ - not all(axis in range(len(x.shape)) for axis in axes): - raise ValueError("ERROR: Provided axes does not match array shape") + axes = utilities.cast_axis_to_tuple(axes, length=1) - weight = np.array(list( - itertools.chain.from_iterable( - itertools.repeat(x ** power, self.paradict['nlon']) - for x in gl.vol(self.paradict['nlat']) - ) - )) + nlon = self.paradict['nlon'] + nlat = self.paradict['nlat'] + + weight = np.array(list(itertools.chain.from_iterable( + itertools.repeat(x ** power, nlon) + for x in gl.vol(nlat)) + )) if axes is not None: # reshape the weight array to match the input shape - new_shape = np.ones(x.shape) + new_shape = np.ones(len(x.shape), dtype=np.int) for index in range(len(axes)): new_shape[index] = len(weight) weight = weight.reshape(new_shape) @@ -157,191 +145,3 @@ class GLSpace(Space): result_x = x * weight return result_x - - def get_plot(self, x, title="", vmin=None, vmax=None, power=False, - unit="", norm=None, cmap=None, cbar=True, other=None, - legend=False, mono=True, **kwargs): - """ - 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). - - """ - from nifty.field import Field - - try: - x = x.get_full_data() - except AttributeError: - pass - - if (not pl.isinteractive()) and (not bool(kwargs.get("save", False))): - about.warnings.cprint("WARNING: interactive mode off.") - - if (power): - x = self.calc_power(x) - - 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]) - - xaxes = np.arange(self.para[0], dtype=np.int) - if (vmin is None): - vmin = np.min(x[:mono].tolist( - ) + (xaxes * (2 * xaxes + 1) * x)[1:].tolist(), axis=None, - out=None) - if (vmax is None): - vmax = np.max(x[:mono].tolist( - ) + (xaxes * (2 * xaxes + 1) * x)[1:].tolist(), axis=None, - out=None) - ax0.loglog(xaxes[1:], (xaxes * (2 * xaxes + 1) * 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]) - elif (isinstance(other, Field)): - other = [other.power(**kwargs)] - else: - other = [self.enforce_power(other)] - imax = max(1, len(other) - 1) - for ii in xrange(len(other)): - ax0.loglog(xaxes[1:], - (xaxes * (2 * xaxes + 1) * 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"$l$") - ax0.set_ylim(vmin, vmax) - ax0.set_ylabel(r"$l(2l+1) C_l$") - ax0.set_title(title) - - else: - x = self.cast(x) - 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).")) - - fig = pl.figure(num=None, figsize=(8.5, 5.4), dpi=None, - facecolor="none", - edgecolor="none", frameon=False, - FigureClass=pl.Figure) - ax0 = fig.add_axes([0.02, 0.05, 0.96, 0.9]) - - lon, lat = gl.bounds(self.para[0], nlon=self.para[1]) - lon = (lon - np.pi) * 180 / np.pi - lat = (lat - np.pi / 2) * 180 / np.pi - if (norm == "log"): - n_ = ln(vmin=vmin, vmax=vmax) - else: - n_ = None - sub = ax0.pcolormesh(lon, lat, np.roll( - np.array(x).reshape((self.para[0], self.para[1]), order='C'), - self.para[ - 1] // 2, axis=1)[::-1, ::-1], cmap=cmap, norm=n_, vmin=vmin, - vmax=vmax) - ax0.set_xlim(-180, 180) - ax0.set_ylim(-90, 90) - ax0.set_aspect("equal") - ax0.axis("off") - 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.5, - aspect=25, 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) - - 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() diff --git a/nifty/spaces/hp_space/hp_space.py b/nifty/spaces/hp_space/hp_space.py index 50be5e4f6..0ebb9cfd3 100644 --- a/nifty/spaces/hp_space/hp_space.py +++ b/nifty/spaces/hp_space/hp_space.py @@ -33,25 +33,16 @@ """ from __future__ import division -import itertools import numpy as np -import pylab as pl - -from d2o import STRATEGIES as DISTRIBUTION_STRATEGIES - -from nifty.spaces.lm_space import LMSpace from nifty.spaces.space import Space -from nifty.config import about, nifty_configuration as gc, \ +from nifty.config import nifty_configuration as gc, \ dependency_injector as gdi from hp_space_paradict import HPSpaceParadict -from nifty.nifty_random import random hp = gdi.get('healpy') -HP_DISTRIBUTION_STRATEGIES = DISTRIBUTION_STRATEGIES['global'] - class HPSpace(Space): """ @@ -103,7 +94,7 @@ class HPSpace(Space): An array with one element containing the pixel size. """ - def __init__(self, nside): + def __init__(self, nside=2, dtype=np.dtype('float')): """ Sets the attributes for a hp_space class instance. @@ -133,12 +124,10 @@ class HPSpace(Space): self.paradict = HPSpaceParadict(nside=nside) # setup dtype - self.dtype = np.dtype('float64') - # HPSpace is never harmonic - self._harmonic = False + self.dtype = np.dtype(dtype) - def copy(self): - return HPSpace(nside=self.paradict['nside']) + # HPSpace is not harmonic + self._harmonic = False @property def shape(self): @@ -148,27 +137,12 @@ class HPSpace(Space): def dim(self): return np.int(12 * self.paradict['nside'] ** 2) - def weight(self, x, power=1, axes=None, inplace=False): - # check if the axes provided are valid given the input shape - if axes is not None and \ - not all(axis in range(len(x.shape)) for axis in axes): - raise ValueError("ERROR: Provided axes does not match array shape") - - weight = np.array(list( - itertools.chain.from_iterable( - itertools.repeat( - (4 * np.pi / 12 * self.paradict['nside'] ** 2) ** power, - 12 * self.paradict['nside'] ** 2 - ) - ) - )) + @property + def total_volume(self): + return 4 * np.pi - if axes is not None: - # reshape the weight array to match the input shape - new_shape = np.ones(x.shape) - for index in range(len(axes)): - new_shape[index] = len(weight) - weight = weight.reshape(new_shape) + def weight(self, x, power=1, axes=None, inplace=False): + weight = ((4*np.pi) / (12 * self.paradict['nside']**2)) ** power if inplace: x *= weight @@ -177,162 +151,3 @@ class HPSpace(Space): result_x = x * weight return result_x - - def get_plot(self, x, title="", vmin=None, vmax=None, power=False, unit="", - norm=None, cmap=None, cbar=True, other=None, legend=False, - mono=True, **kwargs): - """ - 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). - iter : int, *optional* - Number of iterations performed in the HEALPix basis - transformation. - """ - from nifty.field import Field - - try: - x = x.get_full_data() - except AttributeError: - pass - - if (not pl.isinteractive()) and (not bool(kwargs.get("save", False))): - about.warnings.cprint("WARNING: interactive mode off.") - - if (power): - x = self.calc_power(x, **kwargs) - - 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]) - - xaxes = np.arange(3 * self.para[0], dtype=np.int) - if (vmin is None): - vmin = np.min(x[:mono].tolist( - ) + (xaxes * (2 * xaxes + 1) * x)[1:].tolist(), axis=None, - out=None) - if (vmax is None): - vmax = np.max(x[:mono].tolist( - ) + (xaxes * (2 * xaxes + 1) * x)[1:].tolist(), axis=None, - out=None) - ax0.loglog(xaxes[1:], (xaxes * (2 * xaxes + 1) * 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]) - elif (isinstance(other, Field)): - other = [other.power(**kwargs)] - else: - other = [self.enforce_power(other)] - imax = max(1, len(other) - 1) - for ii in xrange(len(other)): - ax0.loglog(xaxes[1:], - (xaxes * (2 * xaxes + 1) * 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"$\ell$") - ax0.set_ylim(vmin, vmax) - ax0.set_ylabel(r"$\ell(2\ell+1) C_\ell$") - ax0.set_title(title) - - else: - if (norm == "log"): - if (vmin is not None): - if (vmin <= 0): - raise ValueError(about._errors.cstring( - "ERROR: nonpositive value(s).")) - elif (np.min(x, axis=None, out=None) <= 0): - raise ValueError(about._errors.cstring( - "ERROR: nonpositive value(s).")) - if (cmap is None): - cmap = pl.cm.jet # default - cmap.set_under(color='k', alpha=0.0) # transparent box - hp.mollview(x, fig=None, rot=None, coord=None, unit=unit, xsize=800, - title=title, nest=False, min=vmin, max=vmax, - flip="astro", remove_dip=False, - remove_mono=False, gal_cut=0, format="%g", format2="%g", - cbar=cbar, cmap=cmap, notext=False, norm=norm, - hold=False, margins=None, sub=None) - fig = pl.gcf() - - 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() diff --git a/nifty/spaces/hp_space/hp_space_paradict.py b/nifty/spaces/hp_space/hp_space_paradict.py index b9287002f..d676a1ab5 100644 --- a/nifty/spaces/hp_space/hp_space_paradict.py +++ b/nifty/spaces/hp_space/hp_space_paradict.py @@ -12,7 +12,7 @@ class HPSpaceParadict(SpaceParadict): SpaceParadict.__init__(self, nside=nside) def __setitem__(self, key, arg): - if key not in ['nside', 'distances']: + if key not in ['nside']: raise ValueError(about._errors.cstring( "ERROR: Unsupported hp_space parameter")) diff --git a/nifty/spaces/rg_space/rg_space.py b/nifty/spaces/rg_space/rg_space.py index 503a1c5cd..d2c600389 100644 --- a/nifty/spaces/rg_space/rg_space.py +++ b/nifty/spaces/rg_space/rg_space.py @@ -34,25 +34,15 @@ from __future__ import division import numpy as np -import os - -import pylab as pl -from matplotlib.colors import LogNorm as ln -from matplotlib.ticker import LogFormatter as lf from d2o import distributed_data_object,\ STRATEGIES as DISTRIBUTION_STRATEGIES from nifty.spaces.space import Space -from nifty.config import about,\ - nifty_configuration as gc,\ - dependency_injector as gdi -from rg_space_paradict import RGSpaceParadict -import nifty.nifty_utilities as utilities +from nifty.config import about -MPI = gdi[gc['mpi_module']] -RG_DISTRIBUTION_STRATEGIES = DISTRIBUTION_STRATEGIES['global'] +from rg_space_paradict import RGSpaceParadict class RGSpace(Space): @@ -118,7 +108,7 @@ class RGSpace(Space): """ def __init__(self, shape=(1,), zerocenter=False, distances=None, - harmonic=False, dtype=np.dtype('float'), ): + harmonic=False, dtype=np.dtype('float')): """ Sets the attributes for an rg_space class instance. @@ -149,12 +139,11 @@ class RGSpace(Space): None """ - super(RGSpace, self).__init__(dtype=np.dtype('float64')) - self.paradict = RGSpaceParadict(shape=shape, zerocenter=zerocenter, distances=distances, harmonic=harmonic) + self.dtype = np.dtype(dtype) @property def harmonic(self): @@ -242,338 +231,3 @@ class RGSpace(Space): dists = dists + temp dists = np.sqrt(dists) return dists - - 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): - """ - 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). - - """ - from nifty.field import Field - - 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) - try: - x = x.get_full_data() - except AttributeError: - pass - - 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]) - - ## explicit kindex - xaxes = kwargs.get("kindex",None) - ## implicit kindex - if(xaxes is None): - try: - self.power_indices - kindex_supply_space = self - except: - kindex_supply_space = self.get_codomain() - - xaxes = kindex_supply_space.power_indices.get_index_dict( - **kwargs)['kindex'] - - -# 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") - - 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: - try: - x = x.get_full_data() - except AttributeError: - pass - 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]) - - xaxes = (np.arange(self.para[0],dtype=np.int)+self.para[2]*(self.para[0]//2))*self.distances - 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)): - other = [self._enforce_values(xx,extend=True) for xx in other] - else: - other = [self._enforce_values(other,extend=True)] - 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): - error = self._enforce_values(np.absolute(kwargs.get("error")),extend=True) - 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): - error = self._enforce_values(np.absolute(kwargs.get("error")),extend=True) - 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).")) - - 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))]) - 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]]) - - 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] - 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: - 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). - """ - from nifty.field import Field - - about.warnings.cflush( - "WARNING: _enforce_values is deprecated function. Please use self.cast") - if(isinstance(x, Field)): - 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( - x) * np.ones(self.dim_split, dtype=self.dtype, order='C') - 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 -- GitLab