<jupyter_start><jupyter_text>Week 10This week we want to build on our results from previous weeks<jupyter_code>from modules import week8 as w8
from modules import week5 as w5
from modules import week9 as w9
from modules import misc
import xarray as xr
import matplotlib.pyplot as plt

%load_ext autoreload
%autoreload 2
# Loading data from file
SIC, LIC = w8.load_ice_data()
SIC = SIC.sel(time=slice('1979-01-01','2019-12-31')).compute()
LIC = LIC.sel(time=slice('1979-01-01','2019-12-31')).compute()
temperature = w8.load_temp_data().sel(time=slice('1979-01-01','2019-12-31')).compute()

# Landmask
landmask = w8.load_landmask()
landmask = landmask >= .5

# Preprocessing
SIC = (SIC.pipe(w5.yearly_average)).copy()
LIC = (LIC.pipe(w5.yearly_average)).copy()
temperature = (temperature.pipe(w5.yearly_average)).copy()
SIC_anomalous = (SIC.pipe(w5.find_anomalies)).copy()
LIC_anomalous = (LIC.pipe(w5.find_anomalies)).copy()
temperature_anomalous = (temperature.pipe(w5.find_anomalies)).copy()

# Breaking up temporally
SIC_short = SIC_anomalous.sel(time=slice('2002-01-01', '2019-12-31')).copy()
LIC_short = LIC_anomalous.sel(time=slice('2002-01-01', '2019-12-31')).copy()
temperature_short = temperature_anomalous.sel(time=slice('2002-01-01', '2019-12-31')).skt.copy()
SIC_long = SIC_anomalous.sel(time=slice('1979-01-01', '2019-12-31')).copy()
temperature_long = temperature_anomalous.sel(time=slice('1979-01-01', '2019-12-31')).skt.copy()

misc.print_heading("Preprocessing Done")
# Calculations
sic_temp_long, sic_temp_short, lic_temp_short = w8.regressions(SIC_anomalous, LIC_anomalous, temperature_anomalous, landmask)
sic_temp_long
w8.regression_plots(sic_temp_long.where(~landmask),  SIC_long.where(~landmask),  temperature_long.where(~landmask))
w8.regression_plots(sic_temp_short.where(~landmask), SIC_short.where(~landmask), temperature_short.where(~landmask))
# w8.regression_plots(lic_temp_short.where(landmask), LIC_short.where(landmask), temperature_short.where(landmask))<jupyter_output>findfont: Font family ['computer modern'] not found. Falling back to DejaVu Sans.<jupyter_text>Remote regressionsWe want to select the region around the Antarctic Peninsular. For now we will just select the locations with the 5th percentile largest decreases. We also select the points only in that quadrent.<jupyter_code>from modules import week10 as w10
from modules import week9  as w9
import matplotlib.pyplot as plt
from matplotlib.colors import LinearSegmentedColormap, TwoSlopeNorm, LogNorm
import numpy as np
import scipy
import cartopy.crs as ccrs
LIC_short
LIC_mean = LIC.mean(dim='time').copy()
LIC_mean = LIC_mean.where(landmask)
fig = plt.figure(figsize=(5, 5))
ax = fig.add_subplot(1, 1, 1, projection=ccrs.SouthPolarStereo())
plot = ax.contourf(LIC_mean.x, LIC_mean.y, LIC_mean,
                   levels=20, crs=ccrs.SouthPolarStereo())
ax.coastlines()
ax.set_title('Mean Land Ice LWET')
cbar = plt.colorbar(plot)
cbar.set_label(r'Mean Land Ice LWET [cm]')
misc.savefigures(folder='images/week10', filename='mean_lic_distribution')
plt.show()
mask2 = LIC_mean<np.nanpercentile(LIC_mean, 5)
mask2 *= LIC_mean.x <0
np.nanpercentile(LIC_mean, 5)
LIC_mean2 = LIC_mean.where(mask2)
fig = plt.figure(figsize=(5, 5))
ax = fig.add_subplot(1, 1, 1, projection=ccrs.SouthPolarStereo())
plot = ax.contourf(LIC_mean2.x, LIC_mean2.y, LIC_mean2,
                   levels=20, crs=ccrs.SouthPolarStereo())
ax.coastlines()
ax.set_title('Mean Land Ice LIELWT')
cbar = plt.colorbar(plot)
cbar.set_label(r'Mean Land Ice LIELWT [cm]')
misc.savefigures(folder='images/week10', filename='decreasing_mean_lic_distribution')
plt.show()
LIC_peninsular_decrease = LIC.where(mask2)
LIC_peninsular_decrease_timeseries = LIC_peninsular_decrease.mean(dim=('x','y'))
LIC_peninsular_decrease_timeseries.plot()
plt.title('Mean LIELWT over the decreasing region of the Antarctic Peninsula')
plt.ylabel('Mean LIELWT [cm]')
plt.axhline(0,color='k', alpha = 0.5)
misc.savefigures(folder='images/week10', filename='decreasing_mean_lic_timeseries')
plt.show()
dependant = LIC_peninsular_decrease_timeseries
independant = temperature.sel(time=slice('2002-01-01', '2019-12-31')).skt.copy()
dependant, independant = xr.broadcast(dependant, independant)
ds = xr.Dataset(dict(dependant=dependant, independant=independant))
ds
regression_results = w5.multiple_fast_regression(ds, 'dependant', ['independant'])
regression_results
out = w9.fast_single_regression(dependant, independant)
out
sig_threshold = 0.1
for sig_threshold in [1, 0.1, 0.05]:
    regression_results_sig = out.where(out.pvalues<=sig_threshold)

    fig = plt.figure(figsize=(5, 5))
    ax = fig.add_subplot(1, 1, 1, projection=ccrs.SouthPolarStereo())

    divnorm = TwoSlopeNorm(vmin=-1, vcenter=0, vmax=1)

    plot = ax.contourf(regression_results_sig.x, regression_results_sig.y, regression_results_sig.correlation,
                       levels=20, crs=ccrs.SouthPolarStereo(), norm = divnorm, cmap = 'RdBu_r')
    ax.coastlines()
    ax.set_title(f'Correlation of SST with LIELWT [p$\leq${sig_threshold:.2f}]')
    cbar = plt.colorbar(plot, shrink=0.8)
    cbar.set_label(r'Correlation Coefficient')
    misc.savefigures(folder='images/week10', filename=f'correlations_{sig_threshold}')
    plt.show()

    fig = plt.figure(figsize=(5, 5))
    ax = fig.add_subplot(1, 1, 1, projection=ccrs.SouthPolarStereo())

    vmin, vmax = (-1000,1000)
    divnorm = TwoSlopeNorm(vmin=vmin, vcenter=0, vmax=vmax)

    plot = ax.contourf(regression_results_sig.x, regression_results_sig.y, regression_results_sig.regression,
                       levels=np.arange(vmin,vmax,100), crs=ccrs.SouthPolarStereo(), norm = divnorm, cmap = 'RdBu_r')
    ax.coastlines()
    ax.set_title(f'Regression of SST with LIELWT [p$\leq${sig_threshold:.2f}]')
    cbar = plt.colorbar(plot, shrink=0.8)
    cbar.set_label(r'Regression Coefficient [cm K$^{-1}$]')
    misc.savefigures(folder='images/week10', filename=f'regressions_{sig_threshold}')
    plt.show()
sig = 0.05
LIC_peninsular_decrease_timeseries = LIC_peninsular_decrease.mean(dim=('x','y'))
LIC_peninsular_decrease_timeseries.plot(label='Measured value')
out.prediction.where(out.pvalues<=sig).mean(dim=('x','y')).plot(label=f'Prediction [p<{sig:.2f}]')
plt.title('Mean LIELWT over the decreasing region of the Antarctic Peninsula')
plt.ylabel('Mean LIELWT [cm]')
plt.axhline(LIC_peninsular_decrease_timeseries.mean(), label ='Mean timeseries value')
plt.legend(bbox_to_anchor=(1.01, 0.85), ncol=1, loc='upper left')
plt.axhline(0,color='k', alpha = 0.5)
misc.savefigures(folder='images/week10', filename='decreasing_mean_lic_timeseries_with_prediction')
plt.show()
sig = 0.05

for sig in [0.1,0.05,0.01,0.001]:
    mask3 = out.pvalues<=sig
    mask3 *= out.x <0
    mask3 *= out.y <0
#     mask3 *= out.regression<0


    LIC_peninsular_decrease_timeseries = LIC_peninsular_decrease.mean(dim=('x','y'))
    LIC_peninsular_decrease_timeseries.plot(label='Measured value')
    out.prediction.where(mask3).mean(dim=('x','y')).plot(label=f'Prediction [p<{sig:.2f}]')
    plt.title('Mean LIELWT over the decreasing region of the Antarctic Peninsula')
    plt.ylabel('Mean LIELWT [cm]')
    plt.axhline(LIC_peninsular_decrease_timeseries.mean(), label ='Mean timeseries value')
    plt.legend(bbox_to_anchor=(1.01, 0.85), ncol=1, loc='upper left')
    plt.axhline(0,color='k', alpha = 0.5)

    ax = plt.gcf().add_subplot(2, 2, 3, projection=ccrs.SouthPolarStereo()) 
    ax.contourf(mask3.x, mask3.y, mask3.values, transform = ccrs.SouthPolarStereo())
    ax.coastlines()

    misc.savefigures(folder='images/week10', filename=f'decreasing_mean_lic_timeseries_with_prediction_with_mask_{sig:.3f}')
    plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1440227949747442, 'fraction_numerical': 0.04079782411604715, 'mean_word_length': 6.084403669724771, 'pattern_counts': {'":': 0, '<': 18, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '647698', 'license': 'MIT', 'n_tokens_mistral': 3138, 'n_tokens_neox': 2883, 'n_words': 498, 'path': 'Week 10.ipynb', 'repo_name': 'hjelleyman/Antarctica-M.2', 'size': '7721'}