The following shows some simple examples of patch processing. See the proc module for a list of all processing functions.
Basic
There are several “basic” processing functions which manipulate the patch metadata, shape, etc. Many of these are covered in the patch tutorial, but here are a few that aren’t:
Transpose
The transpose patch function patch function simply transposes the dimensions of the patch, either by rotating the dimensions or to a new specified dimension.
import dascore as dcpatch = dc.get_example_patch()print(f"dims before transpose: {patch.dims}")transposed = patch.transpose()print(f"dims after transpose: {transposed.dims}")# Dimension order can be manually specified as well.transposed = patch.transpose("time", "distance")
dims before transpose: ('distance', 'time')
dims after transpose: ('time', 'distance')
Squeeze
squeeze removes dimensions which have a single value (see also numpy.squeeze).
import dascore as dcpatch = dc.get_example_patch()# Select first distance value to make distance dim flat.flat_patch = patch.select(distance=0, samples=True)print(f"Pre-squeeze dims: {flat_patch.shape}")squeezed = flat_patch.squeeze()print(f"Post-squeeze dims: {squeezed.shape}")
import numpy as npimport dascore as dcpatch = dc.get_example_patch()# Create a patch with nan valuesdata = np.array(patch.data)data[:, 0] = np.NaNpatch_with_nan = patch.new(data=data)# Drop Nan along axis 1dim = patch_with_nan.dims[1]no_na_patch = patch_with_nan.dropna(dim)assertnot np.any(np.isnan(no_na_patch.data))
Decimate
decimate decimates a Patch along a given axis while by default performing low-pass filtering to avoid aliasing.
Data creation
First, we create a patch composed of two sine waves; one above the new decimation frequency and one below.
import dascore as dcpatch = dc.examples.get_example_patch("sin_wav", sample_rate=1000, frequency=[200, 10], channel_count=2,)patch.viz.wiggle(show=True);
The rolling patch function implements moving window operators similar to pandas rolling. It is useful for smoothing, calculating aggregated statistics, etc.
Here is an example of using a rolling mean to smooth along the time axis:
import dascore as dcpatch = dc.get_example_patch("example_event_1")# Apply moving mean window over every 10 samplesdt = patch.get_coord('time').stepsmoothed = patch.rolling(time=50*dt).mean()smoothed.viz.waterfall(scale=.1);
Notice the NaN values at the start of the time axis. These can be trimmed with Patch.dropna.