This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!pip install area | |
from area import area | |
from functools import partial | |
def make_polygon(coords, size): | |
left_long, bottom_lat = coords | |
return np.array([[[left_long, bottom_lat], | |
[left_long, bottom_lat + size], | |
[left_long + size, bottom_lat + size], | |
[left_long + size, bottom_lat], |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def series_changes(filled, model='l1', pen=3): | |
# Applies the change_points function over every filled series | |
changes = np.apply_along_axis(func1d=lambda s: change_points(s, model, pen), | |
arr=filled.reshape(filled.shape[0], -1).T, | |
axis=1) | |
changes = changes.reshape(*filled.shape[1:], 2) | |
return changes | |
def series_evolution(gridded, changes, offset=True, keep_missing=True): | |
# Applies the change_evolution function over every grid box |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def change_evolution(changes, offset=True): | |
breaks, averages = changes | |
# Creates a full series using the break points and using the average | |
# of each regime | |
avg_series = np.concatenate([[averages[i]] * (breaks[i + 1] - breaks[i]) | |
for i in range(len(averages))]) | |
# If offset, shifts the starting point to zero | |
if offset: | |
avg_series -= avg_series[0] | |
# If the first break is other than zero, concatenates empty data at |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!pip install ruptures | |
import ruptures as rpt | |
from ruptures.exceptions import BadSegmentationParameters | |
def change_points(data, model='l1', pen=3): | |
signal = data.copy() | |
# If there are no valid data points, returns one regime | |
# that starts at 0, ends at the full length, and is nan | |
if np.all(np.isnan(signal)): | |
return np.array([0, len(signal)]), np.array([np.nan]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def fill_series(gridded, | |
max_contiguous_na=3, | |
periods_for_extrapolation=5, | |
method='slinear'): | |
# Applies the fill_values function over every grid box | |
filled = np.apply_along_axis(func1d=lambda s: fill_values(s, | |
max_contiguous_na, | |
periods_for_extrapolation, | |
method), | |
arr=gridded.reshape(gridded.shape[0], -1).T, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from scipy import interpolate | |
def fill_values(data, | |
max_contiguous_na=5, | |
periods_for_extrapolation=5, | |
method='slinear'): | |
time = np.arange(len(data)) | |
signal = data.copy() | |
# If there are no valid data points, there's nothing to fill | |
if np.all(np.isnan(signal)): |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def bounds(data, max_contiguous_na=5): | |
# Returns the start and end indices of the longest | |
# valid sequence, that is, containing up to a given | |
# number of contiguous missing points | |
# Gets the indices of the non-null points | |
idxs = np.arange(len(data))[~np.isnan(data)] | |
max_size = 0 | |
max_ini = 0 | |
size = 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def surface_stat(data, surface_perc, stat='mean'): | |
# Average anomaly weighted by surface area, not counting missing data | |
data_mean = np.array([np.nansum(ev * surface_perc) / | |
(~np.isnan(ev) * surface_perc).sum() | |
for ev in data]) | |
if stat == 'mean': | |
return data_mean | |
elif stat == 'std': | |
data_var = [np.nansum((ev - ev_mean) ** 2 * surface_perc) / | |
(~np.isnan(ev) * surface_perc).sum() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
import netCDF4 as nc | |
import requests | |
import os | |
def download(url=None, cached_etag=None): | |
try: | |
if url is None: | |
base = b'https://www.ncei.noaa.gov/data/noaa-global-surface-temperature/v5/access/gridded/' | |
resp = requests.get(base) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import torch | |
import torch.nn as nn | |
from torchviz import make_dot | |
device = 'cuda' | |
# sending tensor to device at creation time | |
a = torch.randn(1, requires_grad=True, dtype=torch.float, device=device) | |
plot1 = make_dot(a) | |
# sending tensor to device immediately after creating it |
NewerOlder