Skip to main content

Correlation Optimized Warping (COW) algorithm for signal alignment

Project description

📘 Cowarp – Correlation Optimized Warping in Python

Cowarp is a Python implementation of an alignment method called Correlation Optimized Warping (COW). This technique is widely used in spectroscopy, chromatography, and other analytical fields to align signals that may have local time shifts or distortions. COW aligns one signal (sample) to another (reference) by segmenting the signals and warping segments to maximize correlation.

This is the initial release of the package. It currently includes a single implementation of the COW algorithm, in which the sample signal endpoints remain fixed (i.e., not subject to warping).

Version

🛠️ Installation

pip install cowarp

🚀 Basic Usage

import numpy as np
from cowarp import warp

# Example reference and sample signals
reference = np.sin(np.linspace(0, 2 * np.pi, n))
sample = np.sin(np.linspace(0, 2 * np.pi, n) + 0.5)

# Run Correlation Optimized Warping
aligned_sample, corr = warp(reference, sample, num_intervals=5, slack=8)

⚙️ Function Signature

warp(
    reference,
    sample,
    num_intervals=None,
    interval_length=None,
    slack=None,
    min_interval_length=None,
    return_details=False,
    verbose=False
)

📚 Parameters

Parameter Type Description
reference array-like Reference (target) 1D signal to align against.
sample array-like Sample signal to be warped and aligned to the reference. Currently supports only 1D signals.
num_intervals int, optional Number of intervals to divide the signal into. Mutually exclusive with interval_length.
interval_length int, optional Length of each interval. Alternative to num_intervals.
slack int, optional Maximum allowed deviation (shift) in interval boundary positions.
min_interval_length int, optional Minimum length allowed for any interval. Default: 3.
return_details bool, default=False If True, returns additional details (e.g., warp path).
verbose bool, default=False If True, prints internal decisions and computed values.

📤 Returns

Depending on return_details:

  • If False returns a tuple: (warped_sample, correlation)

    • warped_sample : np.ndarray

      The warped (aligned ) version of sample, aligned to the same length as reference.

    • correlation : final_correlation : float

      Pearson correlation coefficient between reference and warped_sample.

  • If True returns a dictionary:

          {
              "warped_sample": np.ndarray,
              "correlation": float,
              "warping_path": list[int],
              "boundaries": list[int],
          }
    
    • warping_path

      List of boundary indices in the sample signal defining the intervals that correspond to the fixed reference intervals. The length of this list is equal to num_intervals + 1.

    • boundaries

      List of fixed interval boundaries in the reference signal. The length of this list is equal to num_intervals + 1.

🧠 Example

"""
Example: Aligning Chromatographic Signals using COW

This example demonstrates how to use the COW (Correlation Optimized Warping)
algorithm to align two chromatographic signals. The sample chromatogram
contains nonlinear time shifts and noise relative to the reference.

"""

import numpy as np
import matplotlib.pyplot as plt
from cowarp import warp


def generate_chromatogram(num_points=400, random_shift=False, noise_level=0.01):
    x = np.linspace(0, 100, num_points)

    # Define ideal peaks
    peaks = [15, 35, 55, 75]
    widths = [2.5, 3.5, 2.5, 3.0]
    amplitudes = [1.0, 0.9, 1.2, 0.8]

    y = np.zeros_like(x)
    for i, (mu, sigma, amp) in enumerate(zip(peaks, widths, amplitudes)):
        if random_shift:
            mu += np.random.uniform(-3.0, 3.0)  # stronger shift
        y += amp * np.exp(-0.5 * ((x - mu) / sigma) ** 2)

    # Add small baseline and noise
    y += 0.05 + noise_level * np.random.randn(num_points)
    return x, y


# --- Generate reference and sample chromatograms ---

# Reference chromatogram
x_ref, reference = generate_chromatogram(num_points=500, random_shift=False, noise_level=0.005)

# Sample chromatogram (more strongly shifted and noisier)
x_samp, sample = generate_chromatogram(num_points=480, random_shift=True, noise_level=0.02)

# --- Perform COW alignment ---
result_dict = warp(
    reference,
    sample,
    num_intervals=10,
    slack=25,
    min_interval_length=4,
    return_details=True,
    verbose=True
)

warped_sample = result_dict['warped_sample']
final_corr = result_dict['correlation']
warping_path = result_dict['warping_path']
boundaries = result_dict['boundaries']

print(f"Final correlation after warping: {final_corr:.4f}")

# --- Plot results ---
plt.figure(figsize=(10, 7))

plt.subplot(2, 1, 1)
plt.plot(x_ref, reference, label="Reference", linewidth=2)
plt.plot(x_samp, sample, label="Sample (before warping)", linestyle="--")
plt.title("Before COW Alignment")
plt.xlabel("Time")
plt.ylabel("Intensity")
plt.legend()
plt.grid(True)

plt.subplot(2, 1, 2)
plt.plot(x_ref, reference, label="Reference", linewidth=2)
plt.plot(np.linspace(0, 100, len(warped_sample)), warped_sample, label="Sample (after warping)", linestyle="--")
plt.title(f"After COW Alignment (Correlation = {final_corr:.4f})")
plt.xlabel("Time")
plt.ylabel("Intensity")
plt.legend()
plt.grid(True)

plt.tight_layout()
plt.show()

The resulting plot is given below.

COW Chromatogram Alignment

🧾 License

This project is licensed under the MIT License.

✍️ Author

Guram Chaganava

Electrical & Electronics Engineer | Machine Learning Enthusiast

LinkedIn

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

cowarp-0.1.6.tar.gz (16.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

cowarp-0.1.6-py3-none-any.whl (14.4 kB view details)

Uploaded Python 3

File details

Details for the file cowarp-0.1.6.tar.gz.

File metadata

  • Download URL: cowarp-0.1.6.tar.gz
  • Upload date:
  • Size: 16.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.9.13

File hashes

Hashes for cowarp-0.1.6.tar.gz
Algorithm Hash digest
SHA256 43a142c303b6d27ad3317ca11249d8a1ecfb49e25bc52ebf6b572530c1cb4c16
MD5 9e8a191cbbd79cde4e0591436ad2716f
BLAKE2b-256 d27142ea688a23575eb422fc65cef0d1aa9ad8354c75a9a9be24e2e0eb9b9ca9

See more details on using hashes here.

File details

Details for the file cowarp-0.1.6-py3-none-any.whl.

File metadata

  • Download URL: cowarp-0.1.6-py3-none-any.whl
  • Upload date:
  • Size: 14.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.9.13

File hashes

Hashes for cowarp-0.1.6-py3-none-any.whl
Algorithm Hash digest
SHA256 e3899858ef141220d0eac2c94e5e12765f8eb2f533da9beed092b892d036f175
MD5 0fd4d5636a88831c80166fd2f1fa530b
BLAKE2b-256 b26b4656784096c62b72b945615f76d16a79cc07254c4ac3287bbe1711ac0c28

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page