Exemple #1
0
def make_cubehelix(*args, **kwargs):
    """make_cubehelix(start=0.5, rotation=-1.5, gamma=1.0,
                   start_hue=None, end_hue=None,
                   sat=None, min_sat=1.2, max_sat=1.2,
                   min_light=0., max_light=1.,
                   n=256., reverse=False, name='custom_cubehelix')
    """
    from palettable.cubehelix import Cubehelix
    cmap = Cubehelix.make(*args, **kwargs).mpl_colormap
    register = kwargs.setdefault("register", False)
    if register:
        plt.register_cmap(cmap=cmap)
        plt.rc('image', cmap=cmap.name)
    return cmap
Exemple #2
0
def make_cubehelix(*args, **kwargs):
    """make_cubehelix(start=0.5, rotation=-1.5, gamma=1.0,
                   start_hue=None, end_hue=None,
                   sat=None, min_sat=1.2, max_sat=1.2,
                   min_light=0., max_light=1.,
                   n=256., reverse=False, name='custom_cubehelix')
    """
    from palettable.cubehelix import Cubehelix
    cmap = Cubehelix.make(*args, **kwargs).mpl_colormap
    register = kwargs.setdefault("register", False)
    if register:
        plt.register_cmap(cmap=cmap)
        plt.rc('image', cmap=cmap.name)
    return cmap
Exemple #3
0
    'GOOGLE_ROOT']  # this is system-dependent and could be removed if necessary

# define the font family
mpl.rc('font', family='Arial')

# define the configurations
num_configs = int(
    sys.argv[1])  # input from the "run_kepert_analysis.sh" script
configs = [sys.argv[i] for i in range(2, 2 + num_configs)]

# define a list of colors
custom_colormap = Cubehelix.make(start_hue=240,
                                 end_hue=-300,
                                 min_sat=1,
                                 max_sat=2.5,
                                 gamma=1,
                                 min_light=0.3,
                                 max_light=0.8,
                                 n=num_configs,
                                 reverse=False,
                                 name='custom_colormap')
colors = custom_colormap.hex_colors

# define the variables to be plotted
vars_to_plot_all_configs = []
vars_to_plot_short_names_all_configs = []
vars_to_plot_long_names_all_configs = []
vars_to_plot_units_all_configs = []
vars_to_plot_ticks_all_configs = []

plot_NOAA = True  # plot reference values from NOAA dropsondes?
plot_betacast = False  # plot betacast reference value?
Exemple #4
0
def plot_scatterplot(candidate,
                     ref,
                     candidatename,
                     refname,
                     hexbin_kwargs=None,
                     xmin=0.,
                     xmax=0.7,
                     ymin=0.,
                     ymax=0.7,
                     figsize=4,
                     ax=None,
                     f=None,
                     weights=None,
                     add_criterion_text=False,
                     criterion_text_threshold=None,
                     filename=None,
                     logmessage=True):
    """ Create hexbin between candidate and reference """
    try:
        from palettable.cubehelix import Cubehelix
        palette = Cubehelix.make(start=0.3, rotation=-0.5, n=16, reverse=True)
        import palettable
        cmap = palette.get_mpl_colormap()
        #cmap = palettable.matplotlib.Inferno_20.get_mpl_colormap()
        cmap = plt.cm.jet
        cmap = plt.cm.cubehelix_r
    except ImportError:
        cmap = 'cubehelix_r'
    if hexbin_kwargs is None:
        hexbin_kwargs = {}
    if ax is None:
        f, ax = plt.subplots(1, 1, figsize=(figsize, figsize))
        myax = ax
        outvar = f
    else:
        outvar = ax
        myax = ax
    x = ref.values.flatten()
    y = candidate.values.flatten()
    if not weights is None:
        broadcasted_weights = xr.where(ref, weights, weights)
        #addweights = {'C':np.ones(len(x)),'reduce_C_function':np.sum}
        addweights = {
            'C': broadcasted_weights.values.flatten(),
            'reduce_C_function': np.sum
        }
    else:
        addweights = {}
    if not 'gridsize' in hexbin_kwargs: hexbin_kwargs['gridsize'] = 200
    if not 'bins' in hexbin_kwargs: hexbin_kwargs['bins'] = 'log'
    if not 'cmap' in hexbin_kwargs: hexbin_kwargs['cmap'] = cmap
    im = myax.hexbin(x, y, **addweights, **hexbin_kwargs)
    myax.set_xlim(xmin, xmax)
    myax.set_ylim(xmin, ymax)
    myax.add_line(mlines.Line2D([0., 1.], [0., 1.], color='k'))
    myax.set_ylabel(candidatename)
    myax.set_xlabel(refname)

    f.subplots_adjust(right=0.8)
    cbar_ax = f.add_axes([0.85, 0.15, 0.05, 0.7])
    f.colorbar(im, cax=cbar_ax)

    if add_criterion_text:
        c = my.stats.compute_crit(candidate,
                                  ref,
                                  candidatename,
                                  refname,
                                  multiplied=1.,
                                  weights=weights)
        text = f'{c.candidatename} vs {c.refname} :\nBias={c.bias} RMSE={c.rmsd}\n'  # & {c.r2} & {c.equation[0]} * x + {c.equation[1]}'

        text = f'{c.candidatename} vs {c.refname} :\nBias={c.bias} RMSE={c.rmsd}\n'  # & {c.r2} & {c.equation[0]} * x + {c.equation[1]}'

        if criterion_text_threshold:

            def t(x):
                if x is None: return None
                return x.where(ref < criterion_text_threshold)

            c1 = my.stats.compute_crit(t(candidate),
                                       t(ref),
                                       candidatename,
                                       refname,
                                       multiplied=1.,
                                       weights=t(weights))
            text += f'Bias={c1.bias} RMSE={c1.rmsd} (<{criterion_text_threshold})\n'  # & {c.r2} & {c.equation[0]} * x + {c.equation[1]}'

            def t(x):
                if x is None: return None
                return x.where(ref >= criterion_text_threshold)

            c1 = my.stats.compute_crit(t(candidate),
                                       t(ref),
                                       candidatename,
                                       refname,
                                       multiplied=1.,
                                       weights=t(weights))
            text += f'Bias={c1.bias} RMSE={c1.rmsd} (>{criterion_text_threshold})\n'  # & {c.r2} & {c.equation[0]} * x + {c.equation[1]}'

        try:
            ax.text(0.02, 0.75, text, transform=ax.transAxes)
        except:
            f.text(0.02, 0.75, text, transform=ax.transFigure
                   )  # may work, or may not, if f is figure or ax, maybe
    if filename:
        my.io.ensure_dir(filename)
        f.savefig(filename)
        if logmessage:
            logging.info(f'Saved plot in {filename}')
    return outvar
Exemple #5
0
def cluster(X, y, dim, name):
    print('clustering')
    svd = TruncatedSVD(n_components=50, n_iter=3, random_state=42)
    X1 = svd.fit_transform(X)
    print('finished svd')
    if dim == 2:
        fig, axes = plt.subplots(3, 5)
        for i, perp in enumerate([10, 30, 50]):
            for j, met in enumerate(
                ['euclidean', 'cityblock', 'hamming', 'jaccard', 'cosine']):

                X_embedded = TSNE(n_components=2, perplexity=perp,
                                  metric=met).fit_transform(X1)
                print('finished tsne')
                palette = Cubehelix.make(start_hue=-300.,
                                         end_hue=240.,
                                         min_sat=1.,
                                         max_sat=2.5,
                                         min_light=0.3,
                                         max_light=0.8,
                                         gamma=.9,
                                         n=10).mpl_colors
                colors = [
                    palette[np.nonzero(y[i])[0][0]] for i in range(y.shape[0])
                ]

                #ax.scatter(X1[:,0], X1[:,1], c=colors, alpha=0.5)
                axes[i, j].scatter(X_embedded[:, 0],
                                   X_embedded[:, 1],
                                   c=colors,
                                   alpha=0.5)

                axes[i, j].set_title(met)

                axes[i, j].grid(True)

        fig.tight_layout()

        plt.show()
        plt.savefig(name + '.png')

    if dim == 3:
        X_embedded = TSNE(n_components=3, perplexity=50).fit_transform(X1)
        print('finished tsne')
        palette = Cubehelix.make(start_hue=-300.,
                                 end_hue=240.,
                                 min_sat=1.,
                                 max_sat=2.5,
                                 min_light=0.3,
                                 max_light=0.8,
                                 gamma=.9,
                                 n=10).mpl_colors
        colors = [palette[np.nonzero(y[i])[0][0]] for i in range(y.shape[0])]

        fig = plt.figure()
        ax = fig.add_subplot(111, projection='3d')
        #ax.scatter(X1[:,0], X1[:,1], c=colors, alpha=0.5)
        ax.scatter(X_embedded[:, 0],
                   X_embedded[:, 1],
                   X_embedded[:, 2],
                   c=colors,
                   alpha=0.5)

        ax.set_title('tsne clustering')

        ax.grid(True)
        fig.tight_layout()

        plt.show()
        plt.savefig('tsne_cluster2.png')
        pickle.dump(
            fig, open('FigureObject.fig.pickle', 'wb')
        )  # This is for Python 3 - py2 may need `file` instead of `open`
Exemple #6
0
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
import matplotlib.ticker as mticker
import cartopy as cart
from scipy import signal
from cartopy.mpl.ticker import LongitudeFormatter, LatitudeFormatter
#from cartopy.mpl.gridliner import LONGITUDE_FORMATTER, LATITUDE_FORMATTER
from scipy.stats.stats import pearsonr, linregress
from sklearn import linear_model
from sklearn.preprocessing import StandardScaler
from ocean_atmosphere.misc_fns import an_ave, spatial_ave, calc_AMO, running_mean, calc_NA_globeanom, calc_NA_globeanom3D, detrend_separate, detrend_common
from thermolib.thermo import w_sat, r_star
from palettable.cubehelix import Cubehelix

cx4 = Cubehelix.make(reverse=True, start=0., rotation=0.5)

#fin = '/Users/cpatrizio/data/ECMWF/'
fin = '/Users/cpatrizio/data/MERRA2/'
fout = '/Volumes/GoogleDrive/My Drive/PhD/figures/AMO/'

#fsst = cdms2.open(fin + 'era_interim_moda_SST_1979to2010.nc')
#fTHF = cdms2.open(fin + 'era_interim_moda_THF_1979to2010.nc')

fsst = cdms2.open(fin + 'MERRA2_SST_ocnthf_monthly1980to2017.nc')
fcf3D = cdms2.open(fin + 'MERRA2_cldfrac3D_monthly1980to2017.nc')
fv3D = cdms2.open(fin + 'MERRA2_v3D_monthly1980to2017.nc')
fu3D = cdms2.open(fin + 'MERRA2_u3D_monthly1980to2017.nc')
fomega = cdms2.open(fin + 'MERRA2_omega_monthly1980to2017.nc')
#fqv3D = cdms2.open(fin + 'MERRA2_qv3D_monthly1980to2017.nc')
ft3D = cdms2.open(fin + 'MERRA2_t3D_monthly1980to2017.nc')