Skip to content

2.1 viz h

import sys, os, pathlib
from pyprojroot import here
root = here(project_files=[".here"])
sys.path.append(str(here()))

PROJ_PATH = pathlib.Path(root)

import scipy.io as scio

# NUMPY SETTINGS
import numpy as np
np.set_printoptions(precision=3, suppress=True)

# MATPLOTLIB Settings
import matplotlib as mpl
import matplotlib.pyplot as plt
%matplotlib inline
%config InlineBackend.figure_format = 'retina'

# SEABORN SETTINGS
import seaborn as sns
sns.set_context(context='talk',font_scale=0.7)
# sns.set(rc={'figure.figsize': (12, 9.)})
# sns.set_style("whitegrid")

# PANDAS SETTINGS
import pandas as pd
pd.set_option("display.max_rows", 120)
pd.set_option("display.max_columns", 120)

# LOGGING SETTINGS
import sys
import logging
logging.basicConfig(
    level=logging.INFO, 
    stream=sys.stdout,
    format='%(asctime) s:%(levelname) s:%(message)s '
)
logger = logging.getLogger()
#logger.setLevel(logging.INFO)

%load_ext autoreload
%autoreload 2
DATA_PATH = PROJ_PATH.joinpath("data/spa_temp/info_earth/entropy")
# DATA_NEW_PATH = PROJ_PATH.joinpath("data/spa_temp/entropy/old_results")
FIG_PATH = PROJ_PATH.joinpath("docs/pics/entropy/")


file_name = '2019_CI_FIG_ENTR_RBIG_2001_'
variables = {
    'gpp': 'gross_primary_productivity',
    'lai': 'leaf_area_index',
    'lst': 'land_surface_temperature',
    'precip': 'precipitation',
    'rm': 'root_moisture',
    'sm': 'soil_moisture',

}

rs_dims = [
    1, 4, 9, 16
]

dims = [
    1, 4, 9, 16, 25, 36, 49
]
!ls $DATA_PATH
olde
rs
sm_v0_world_gpp_2010_s200k_rc.csv
v1_world_gpp_2002_2010_s200k_rs1MS.csv
v1_world_gpp_2002_2010_s200k_rs1MS_rc.csv
v1_world_lai_2002_2010_s200k_rs1MS.csv
v1_world_lai_2002_2010_s200k_rs1MS_rc.csv
v1_world_lst_2002_2010_s200k_rs1MS.csv
v1_world_lst_2002_2010_s200k_rs1MS_rc.csv
v1_world_precip_2002_2010_s200k_rs1MS.csv
v1_world_precip_2002_2010_s200k_rs1MS_rc.csv
v1_world_rm_2002_2010_s200k_rs1MS.csv
v1_world_rm_2002_2010_s200k_rs1MS_rc.csv
v1_world_sm_2002_2010_s200k_rs1MS.csv
v1_world_sm_2002_2010_s200k_rs1MS_rc.csv
v2_world_gpp_2002_2010_s200k.csv
v2_world_gpp_2002_2010_s200k_rc.csv
v2_world_lai_2002_2010_s200k.csv
v2_world_lai_2002_2010_s200k_rc.csv
v2_world_lst_2002_2010_s200k.csv
v2_world_lst_2002_2010_s200k_rc.csv
v2_world_precip_2002_2010_s200k.csv
v2_world_precip_2002_2010_s200k_rc.csv
v2_world_rm_2002_2010_s200k.csv
v2_world_rm_2002_2010_s200k_rc.csv
v2_world_sm_2002_2010_s200k.csv
v2_world_sm_2002_2010_s200k_rc.csv
def plot_entropy(results_df, variable, rs=False, save_name=None):
    fig, ax = plt.subplots(figsize=(7,5))
    # sns.lineplot(ax=ax, x="I", y="n_features", data=fmri)
    sub_df = results_df[results_df['variable'] == variable]
#     print(sub_df)

    sns.lineplot(
        ax=ax, x='ratio', y='entropy', 
        data=sub_df, hue=sub_df.n_dimensions.values, 
        marker='o',markersize=10)
    # sns.lineplot(ax=ax, x='nm_features', y='I', data=ndvi_df, label=label_main, linewidth=4,)
    ax.set_xlabel('Ratio', )
    ax.set_ylabel('Entropy')
    plt.legend(rs_dims if rs is True else dims)
    plt.tight_layout()
    if save_name is not None:
        fig.savefig(FIG_PATH.joinpath(f"{save_name}.png"), frameon=False, )
    else:
        plt.show()

Results I - Resampled, Original

results_df = pd.DataFrame()
version = 'v1'
region = 'world'
period = '2002_2010'
n_samples = 200
rs = True
rc = False

for ivariable in variables.keys():
    save_name = f"{version}_{region}_{ivariable}_{period}_s{n_samples}k"
    if rs:
        save_name += "_rs1MS"
    if rc:
        save_name += "_rc"
    print(ivariable)
    results_df = pd.read_csv(DATA_PATH.joinpath(save_name + ".csv"), index_col=0)
    results_df['ratio'] = (results_df['spatial']**2 / results_df['temporal']) / results_df['n_dimensions']
    results_df['entropy'] /= results_df['n_dimensions']
    plot_entropy(results_df, variables[ivariable], rs, save_name)
gpp
2020-05-28 08:34:53,235:INFO:Note: NumExpr detected 56 cores but "NUMEXPR_MAX_THREADS" not set, so enforcing safe limit of 8. 
2020-05-28 08:34:53,236:INFO:NumExpr defaulting to 8 threads. 
<ipython-input-4-794391b2a066>:17: MatplotlibDeprecationWarning: 
The frameon kwarg was deprecated in Matplotlib 3.1 and will be removed in 3.3. Use facecolor instead.
  fig.savefig(FIG_PATH.joinpath(f"{save_name}.png"), frameon=False, )
lai
<ipython-input-4-794391b2a066>:17: MatplotlibDeprecationWarning: 
The frameon kwarg was deprecated in Matplotlib 3.1 and will be removed in 3.3. Use facecolor instead.
  fig.savefig(FIG_PATH.joinpath(f"{save_name}.png"), frameon=False, )
<ipython-input-4-794391b2a066>:17: MatplotlibDeprecationWarning: 
The frameon kwarg was deprecated in Matplotlib 3.1 and will be removed in 3.3. Use facecolor instead.
  fig.savefig(FIG_PATH.joinpath(f"{save_name}.png"), frameon=False, )
lst
precip
<ipython-input-4-794391b2a066>:17: MatplotlibDeprecationWarning: 
The frameon kwarg was deprecated in Matplotlib 3.1 and will be removed in 3.3. Use facecolor instead.
  fig.savefig(FIG_PATH.joinpath(f"{save_name}.png"), frameon=False, )
<ipython-input-4-794391b2a066>:17: MatplotlibDeprecationWarning: 
The frameon kwarg was deprecated in Matplotlib 3.1 and will be removed in 3.3. Use facecolor instead.
  fig.savefig(FIG_PATH.joinpath(f"{save_name}.png"), frameon=False, )
rm
sm
<ipython-input-4-794391b2a066>:17: MatplotlibDeprecationWarning: 
The frameon kwarg was deprecated in Matplotlib 3.1 and will be removed in 3.3. Use facecolor instead.
  fig.savefig(FIG_PATH.joinpath(f"{save_name}.png"), frameon=False, )

Results II - Resampled, Climatology Removed

results_df = pd.DataFrame()
version = 'v1'
region = 'world'
period = '2002_2010'
n_samples = 200
rs = True
rc = True

for ivariable in variables.keys():
    save_name = f"{version}_{region}_{ivariable}_{period}_s{n_samples}k"
    if rs:
        save_name += "_rs1MS"
    if rc:
        save_name += "_rc"
    print(ivariable)
    results_df = pd.read_csv(DATA_PATH.joinpath(save_name + ".csv"), index_col=0)
    results_df['ratio'] = (results_df['spatial']**2 / results_df['temporal']) / results_df['n_dimensions']
    results_df['entropy'] /= results_df['n_dimensions']

    plot_entropy(results_df, variables[ivariable], rs, save_name)
#     for idims, (ratio, entropy) in zip(dims, data_mat['RES'][0]):
#         for iratio, ientropy in zip(np.nditer(ratio), np.nditer(entropy)):
#     #         print(type(idims))
#     #         print(type(iratio))
#     #         print(type(ientropy))
#             results_df = results_df.append({
#                 'variable': ivariable,
#                 'ratio': float(iratio),
#                 'h': float(ientropy),
#                 'dims': float(idims)
#             }, ignore_index=True)

# results_df.to_csv(DATA_NEW_PATH.joinpath("old_results.csv"))

# len(precip_mat['RES'][0])
gpp
lai
<ipython-input-4-794391b2a066>:17: MatplotlibDeprecationWarning: 
The frameon kwarg was deprecated in Matplotlib 3.1 and will be removed in 3.3. Use facecolor instead.
  fig.savefig(FIG_PATH.joinpath(f"{save_name}.png"), frameon=False, )
<ipython-input-4-794391b2a066>:17: MatplotlibDeprecationWarning: 
The frameon kwarg was deprecated in Matplotlib 3.1 and will be removed in 3.3. Use facecolor instead.
  fig.savefig(FIG_PATH.joinpath(f"{save_name}.png"), frameon=False, )
lst
precip
<ipython-input-4-794391b2a066>:17: MatplotlibDeprecationWarning: 
The frameon kwarg was deprecated in Matplotlib 3.1 and will be removed in 3.3. Use facecolor instead.
  fig.savefig(FIG_PATH.joinpath(f"{save_name}.png"), frameon=False, )
<ipython-input-4-794391b2a066>:17: MatplotlibDeprecationWarning: 
The frameon kwarg was deprecated in Matplotlib 3.1 and will be removed in 3.3. Use facecolor instead.
  fig.savefig(FIG_PATH.joinpath(f"{save_name}.png"), frameon=False, )
rm
sm
<ipython-input-4-794391b2a066>:17: MatplotlibDeprecationWarning: 
The frameon kwarg was deprecated in Matplotlib 3.1 and will be removed in 3.3. Use facecolor instead.
  fig.savefig(FIG_PATH.joinpath(f"{save_name}.png"), frameon=False, )
<ipython-input-4-794391b2a066>:17: MatplotlibDeprecationWarning: 
The frameon kwarg was deprecated in Matplotlib 3.1 and will be removed in 3.3. Use facecolor instead.
  fig.savefig(FIG_PATH.joinpath(f"{save_name}.png"), frameon=False, )

Results III - Full

results_df = pd.DataFrame()
version = 'v2'
region = 'world'
period = '2002_2010'
n_samples = 200
rs = False
rc = False

for ivariable in variables.keys():
    save_name = f"{version}_{region}_{ivariable}_{period}_s{n_samples}k"
    if rs:
        save_name += "_rs1MS"
    if rc:
        save_name += "_rc"
    print(ivariable)
    results_df = pd.read_csv(DATA_PATH.joinpath(save_name + ".csv"), index_col=0)
    results_df['ratio'] = (results_df['spatial']**2 / results_df['temporal']) / results_df['n_dimensions']
    results_df['entropy'] /= results_df['n_dimensions']

    plot_entropy(results_df, variables[ivariable], rs, save_name)
gpp
lai
<ipython-input-4-794391b2a066>:17: MatplotlibDeprecationWarning: 
The frameon kwarg was deprecated in Matplotlib 3.1 and will be removed in 3.3. Use facecolor instead.
  fig.savefig(FIG_PATH.joinpath(f"{save_name}.png"), frameon=False, )
<ipython-input-4-794391b2a066>:17: MatplotlibDeprecationWarning: 
The frameon kwarg was deprecated in Matplotlib 3.1 and will be removed in 3.3. Use facecolor instead.
  fig.savefig(FIG_PATH.joinpath(f"{save_name}.png"), frameon=False, )
lst
precip
<ipython-input-4-794391b2a066>:17: MatplotlibDeprecationWarning: 
The frameon kwarg was deprecated in Matplotlib 3.1 and will be removed in 3.3. Use facecolor instead.
  fig.savefig(FIG_PATH.joinpath(f"{save_name}.png"), frameon=False, )
<ipython-input-4-794391b2a066>:17: MatplotlibDeprecationWarning: 
The frameon kwarg was deprecated in Matplotlib 3.1 and will be removed in 3.3. Use facecolor instead.
  fig.savefig(FIG_PATH.joinpath(f"{save_name}.png"), frameon=False, )
rm
sm
<ipython-input-4-794391b2a066>:17: MatplotlibDeprecationWarning: 
The frameon kwarg was deprecated in Matplotlib 3.1 and will be removed in 3.3. Use facecolor instead.
  fig.savefig(FIG_PATH.joinpath(f"{save_name}.png"), frameon=False, )
<ipython-input-4-794391b2a066>:17: MatplotlibDeprecationWarning: 
The frameon kwarg was deprecated in Matplotlib 3.1 and will be removed in 3.3. Use facecolor instead.
  fig.savefig(FIG_PATH.joinpath(f"{save_name}.png"), frameon=False, )

Results IV - Full, Climatology Removed

results_df = pd.DataFrame()
version = 'v2'
region = 'world'
period = '2002_2010'
n_samples = 200
rs = False
rc = True

for ivariable in variables.keys():
    save_name = f"{version}_{region}_{ivariable}_{period}_s{n_samples}k"
    if rs:
        save_name += "_rs1MS"
    if rc:
        save_name += "_rc"
    print(ivariable)
    results_df = pd.read_csv(DATA_PATH.joinpath(save_name + ".csv"), index_col=0)
    results_df['ratio'] = (results_df['spatial']**2 / results_df['temporal']) / results_df['n_dimensions']
    results_df['entropy'] /= results_df['n_dimensions']

    plot_entropy(results_df, variables[ivariable], rs, save_name)
gpp
lai
<ipython-input-4-794391b2a066>:17: MatplotlibDeprecationWarning: 
The frameon kwarg was deprecated in Matplotlib 3.1 and will be removed in 3.3. Use facecolor instead.
  fig.savefig(FIG_PATH.joinpath(f"{save_name}.png"), frameon=False, )
<ipython-input-4-794391b2a066>:17: MatplotlibDeprecationWarning: 
The frameon kwarg was deprecated in Matplotlib 3.1 and will be removed in 3.3. Use facecolor instead.
  fig.savefig(FIG_PATH.joinpath(f"{save_name}.png"), frameon=False, )
lst
precip
<ipython-input-4-794391b2a066>:17: MatplotlibDeprecationWarning: 
The frameon kwarg was deprecated in Matplotlib 3.1 and will be removed in 3.3. Use facecolor instead.
  fig.savefig(FIG_PATH.joinpath(f"{save_name}.png"), frameon=False, )
<ipython-input-4-794391b2a066>:17: MatplotlibDeprecationWarning: 
The frameon kwarg was deprecated in Matplotlib 3.1 and will be removed in 3.3. Use facecolor instead.
  fig.savefig(FIG_PATH.joinpath(f"{save_name}.png"), frameon=False, )
rm
<ipython-input-4-794391b2a066>:17: MatplotlibDeprecationWarning: 
The frameon kwarg was deprecated in Matplotlib 3.1 and will be removed in 3.3. Use facecolor instead.
  fig.savefig(FIG_PATH.joinpath(f"{save_name}.png"), frameon=False, )
<ipython-input-4-794391b2a066>:17: MatplotlibDeprecationWarning: 
The frameon kwarg was deprecated in Matplotlib 3.1 and will be removed in 3.3. Use facecolor instead.
  fig.savefig(FIG_PATH.joinpath(f"{save_name}.png"), frameon=False, )
sm