Skip to content

Instantly share code, notes, and snippets.

View dvgodoy's full-sized avatar

Daniel Voigt Godoy dvgodoy

View GitHub Profile
@dvgodoy
dvgodoy / surface.py
Last active November 24, 2022 16:54
#!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],
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
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
#!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])
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,
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)):
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
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()
@dvgodoy
dvgodoy / data.py
Last active November 24, 2022 16:45
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)
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