Skip to content

Instantly share code, notes, and snippets.

View lukegre's full-sized avatar
🐍

Luke Gregor lukegre

🐍
View GitHub Profile

Bror's tool belt

Balance between excell and C++ is NB

Work

Find a good scripting language

  • Python -- a bit more flexible than MATLAB for instance
  • Matlab
  • R
@lukegre
lukegre / time_utils.py
Last active April 17, 2017 10:59
This class takes the tm_* attributes from a timetuple and applies them to a numpy.ndarray. Also includes datestr and datetime functions.
import numpy as np
from pylab import num2date
from itertools import ifilter
from time import struct_time
from __future__ import print_function
class TimeTupleArray(np.ndarray):
def __new__(cls, input_array, info=None):
@lukegre
lukegre / LG_xarray_tools.py
Last active December 10, 2020 20:06
function that takes the trend for an xarray.DataArray over the 'time' variable. Returns the slopes and p-values for each location.
def dataset_encoding(xds):
cols = ['source', 'original_shape', 'dtype', 'zlib', 'complevel', 'chunksizes']
info = pd.DataFrame(columns=cols, index=xds.data_vars)
for row in info.index:
var_encoding = xds[row].encoding
for col in info.keys():
info.ix[row, col] = var_encoding.pop(col, '')
return info
@lukegre
lukegre / regex_sublime_grammar.tex
Created March 11, 2017 21:27
This file contains little "find all" snippets that are useful to find grammar mistakes in a document using regex in sublime
\b(\w+)\s+\1\b % repeated words
^[A-Za-z].*[A-Za-Z0-9] % lines that do not end with a full stop
@lukegre
lukegre / alkalinity_Lee2006.py
Created May 19, 2017 07:46
Calculate total alkalinity according to Lee et al. (2006)
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
This script is written to calculate total alkalinity according to Lee et al. (2006).
Please see function help for more details.
Please acknowledge use.
"""
__author__ = "Luke Gregor"
@lukegre
lukegre / time_series_GP_gap_filler.py
Created May 19, 2017 12:48
Fill gaps in a time series using Gaussian Processes with the option of adding realistically scaled noise
#!/usr/bin/env python
"""
A short script that is used to fill gaps in a time series (gap_filler_with_noise).
The method uses gaussian processes (aka Kriging) to get the trend (calls gaussian_smoother).
I recommend that you play around with the theta0 value to find the correct scale for the trend.
Noise is added to the estimated trend by assessing the noise around the trend.
Diagnostic plots can be created with this script.
@lukegre
lukegre / empirical_mode_decomposition.py
Last active December 17, 2018 18:58
Empirical mode decomposition (EMD) for 1D data with plotting function for output IMFs
import numpy as np
### Implementation of our EMD() function
def emd(data, stop_limit=0.001, spline_order=3):
"""
EMD as explained by Scott Cole (https://srcole.github.io/2016/01/18/emd/),
but has been modified by Luke Gregor (https://github.com/luke-gregor) to
automatically stop when there are no more IMFs in the dataset.
Additionally, the residual from the stopping point and the original dataset
is counted as the final IMF.
@lukegre
lukegre / corona_daily_increase.ipynb
Created March 27, 2020 17:28
Plots daily increases of Covid-19 cases for a few countries using the John Hopkins Uni data
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@lukegre
lukegre / check_inputs.py
Created May 18, 2020 07:03
A few functions to help with functions that have limits for the inputs
def input_limits(return_bool=True, return_info=True, **kwargs):
"""
This is a function wrapper for functions that have defined input limits.
The user can define the valid limits of any of the inputs.
Parameters
----------
return_bool: array-bool
returns a boolean array of the original function where valid or not
return_info: array-str
@lukegre
lukegre / simple_coarsen_example.ipynb
Created September 22, 2020 10:29
Difference between interpolation and averaging when coarsening model data.ipynb
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.