Skip to content

Instantly share code, notes, and snippets.

View cbur24's full-sized avatar

Chad Burton cbur24

View GitHub Profile
@cbur24
cbur24 / test_landsat_collections.py
Last active August 5, 2020 01:42
Test SR between landsat collections
import datacube
import matplotlib.pyplot as plt
from matplotlib.pyplot import cm
from matplotlib.patches import Patch
import numpy as np
import sys
import xarray as xr
sys.path.append('../Scripts')
from deafrica_datahandling import load_ard
@cbur24
cbur24 / custom_feature_layer_extract.py
Last active August 5, 2020 06:00
custom reduce function for `collect_training_data`
from datacube.utils.geometry import assign_crs
import datacube
import numpy as np
import sys
import xarray as xr
sys.path.append('../Scripts')
from deafrica_bandindices import calculate_indices
from deafrica_temporal_statistics import temporal_statistics
from deafrica_classificationtools import HiddenPrints
@cbur24
cbur24 / random_sampling.py
Last active January 11, 2023 00:35
Python function for generating random samples across an xarray.DataArray
def random_sampling(da,
n,
sampling='stratified_random',
manual_class_ratios=None,
out_fname=None
):
"""
Creates randomly sampled points for post-classification
accuracy assessment.
@cbur24
cbur24 / plot_places.py
Created August 10, 2020 03:27
Plot the locations and names of towns/cities on a given geo-axes.
import cartopy.crs as ccrs
def plot_towns(ax, lats, lons, resolution='10m', transform=ccrs.PlateCarree(), zorder=3):
"""
This function will download the 'populated_places' shapefile from
NaturalEarth, trim the shapefile based on the limits of the provided
lat & long coords, and then plot the locations and names of the towns
on a given GeoAxes.
@cbur24
cbur24 / significance_test.py
Created August 10, 2020 03:30
Per-pixel, xarray significance tests
import numpy as np
import xarray as xr
from scipy import stats
def significance_tests(xarray_a, xarray_b, t_test=False, levene_test=False,
equal_variance = False, nan_policy= 'omit', mask_not_sig = False,
level_of_sig = 0.05, center='mean'):
"""
This function operates on a per-pixel basis and contains two types of significance tests:
@cbur24
cbur24 / predict_xr.py
Last active September 22, 2020 07:29
import xarray as xr
import numpy as np
import joblib
import dask.array as da
from datacube.utils.geometry import assign_crs
from dask_ml.wrappers import ParallelPostFit
def predict_xr(model,
input_xr,
chunk_size=None,
@cbur24
cbur24 / mask_dc_data.py
Created September 22, 2020 04:44
Looping through geopandas df, loading datacube data, masking with geometry
import sys
import datacube
import xarray as xr
import geopandas as gpd
from datacube.utils import geometry
sys.path.append('../Scripts')
from deafrica_datahandling import load_ard
from deafrica_spatialtools import xr_rasterize
@cbur24
cbur24 / crop_mask_feature_layer.py
Last active April 19, 2021 04:43
Feature layer function for production run of eastern crop-mask
import datacube
import numpy as np
import xarray as xr
from odc.algo import xr_reproject
from pyproj import Proj, transform
from datacube.utils.geometry import assign_crs
from datacube.testutils.io import rio_slurp_xarray
from deafrica_tools.bandindices import calculate_indices
def common_ops(ds, era):
@cbur24
cbur24 / mask_xarray.py
Last active May 27, 2021 03:01
Create xarray mask from vector
import xarray as xr
import geopandas as gpd
from dea_tools.spatial import xr_rasterize
#read shapefile
mask_vector = gpd.read_file('mask.shp')
#import xarray ds
ds = xr.open_dataset('testing.nc')
@cbur24
cbur24 / xr_geomad.py
Last active February 23, 2021 04:07
Calculate geomedian and TMADs on xarray, numpy, or dask arrays
import xarray as xr
import numpy as np
import dask
import hdstats
from odc.algo import randomize, reshape_for_geomedian
from odc.algo._dask import reshape_yxbt
from datacube.utils.geometry import assign_crs
def xr_geomad(ds, axis='time', where=None, **kw):
"""