import dascore as dc
import numpy as np
= dc.get_example_patch()
patch # Select a smaller subset for examples
= patch.select(distance=(0, 50))
patch
# Example 1: Absolute mode - align traces to specific times
# Each trace will start at the specified absolute time value
= patch.get_coord("time")
time = patch.get_array("distance")
distance # Specify exact start times for each trace (as datetime64)
= np.timedelta64(1, 'ms') # 1 millisecond
dt = time.min() + np.arange(len(distance)) * dt
start_times = patch.update_coords(abs_times=("distance", start_times))
patch_abs # Traces will now start at specified absolute times
= patch_abs.align_to_coord(time="abs_times", mode="full")
aligned_abs
# Example 2: Round-trip alignment with reverse parameter
= np.arange(len(distance)) # shifts in samples
shifts = patch.update_coords(my_shifts=("distance", shifts))
patch_shift # Forward alignment
= patch_shift.align_to_coord(
aligned ="my_shifts", samples=True, mode="full"
time
)# Reverse to get back original (after dropping NaN padding)
= aligned.align_to_coord(
reversed_patch ="my_shifts", samples=True, mode="full", reverse=True
time
)= reversed_patch.dropna("time")
original assert original.equals(patch_shift)
# Example 3: Use 'valid' mode to extract only overlapping region
= patch_shift.align_to_coord(
valid_aligned ="my_shifts", samples=True, mode="valid"
time
)# Result contains no NaN values, only the overlapping data
assert not np.isnan(valid_aligned.data).any()
align_to_coord
align_to_coord(
patch: Patch ,
mode: Literal[‘full’, ‘valid’, ‘same’] = same,
relative: bool = False,
samples: bool = False,
reverse: bool = False,
fill_value: float = nan,
**kwargs ,
)-> ‘PatchType’
Align patches based on values in a non-dimension coordinate.
Parameters
Parameter | Description |
---|---|
mode : str |
Determines the output shape of the patch. Options are: “full” - Regardless of shift, all original data are preserved. This can result in patches with many fill values along the aligned dimension. “same” - The patch will retain its shape, however, only one trace (and traces that weren’t shifted) will remain complete. Parts of shifted traces will be discarded. “valid” - The output patch will likely be smaller than the input patch, as only completely overlapped valid data are kept. This means no fill_values will occur in the patch. |
relative |
If True, the values in the alignment coordinate specify offsets from the original start position (in the dimension’s units). If False and samples is False (absolute mode), the values specify the exact absolute positions where each trace should start. |
samples |
If True, the values in the alignment coordinate specify offsets in sample counts from the original start position. If False and relative is False (absolute mode), the values specify exact absolute positions in the dimension’s units. |
reverse |
If True, multiply the alignment coordinate values by -1 to reverse a previous alignment operation. |
fill_value | The value to insert in areas lacking data. |
**kwargs |
Used to specify the dimension which should shift and the coordinate to shift it to. The shift coordinate should not depend on the specified dimension. |
Returns
A patch with aligned coordinates.
Examples
**Alignment Meaning
The relative
and samples
parameters control how shift values are interpreted:
Absolute mode (relative=False, samples=False): Shift coordinate values specify the exact absolute position where each trace should start. For example, if the shift coordinate contains [2.0, 2.5, 3.0], traces will start at times 2.0, 2.5, and 3.0 respectively.
Relative mode (relative=True): Shift coordinate values specify offsets from the original start position in the dimension’s units. For example, if the original start is 0.0 and shifts are [0.0, 0.5, 1.0], traces will start at 0.0, 0.5, and 1.0.
Samples mode (samples=True): Shift coordinate values specify offsets in sample counts. For example, shifts of [0, 1, 2] with step=0.1 will offset traces by 0, 0.1, and 0.2 in the dimension’s units.
Mode Parameter:
To understand the mode argument, consider a 2D patch with two traces, an alignment dimension length of 10, and a relative shift of 5 samples. For mode==“full” the output looks like this: —–aaaaaaaaaa bbbbbbbbbb—– For mode==“same” the patch will look like this: aaaaaaaaaa bbbbb—– For mode==“valid”: aaaaa bbbbb where a and b represent values in first and second trace, respectively, and the - represents the fill values.