Skip to content

[ENH] Port Catch22 to the catch22 v1.0 feature definitions - #4052

Draft
MatthewMiddlehurst wants to merge 17 commits into
aeon-toolkit:mainfrom
MatthewMiddlehurst:mm/catch22-v060
Draft

MatthewMiddlehurst wants to merge 17 commits into
aeon-toolkit:mainfrom
MatthewMiddlehurst:mm/catch22-v060

Conversation

@MatthewMiddlehurst

Copy link
Copy Markdown
Member

What does this implement/fix? Explain your changes.

catch22 v1.0 re-implements every feature so that it reproduces its hctsa v3.0.0 definition exactly, and freezes the definitions there. pycatch22 1.0 ships it. aeon's numba Catch22 reproduced catch22 v0.5, including its off-by-one bugs, which catch22 v0.6.0 fixed. This PR ports every kernel to the catch22 v1.2.0 C code as one change, rather than moving to v0.6.0 first and then changing values again.

aeon's structure is kept: the feature order, the batched FFT and autocorrelation caches, and the compiled dispatch loop.

Changes by feature:

  • mode_5, mode_10, ami2: hctsa's bin edges and histcounts binning. ami2 adds the Panzeri-Treves bias correction.
  • acf_timescale: interpolated crossing within the series' lags, and n − 1 when there is none.
  • low_freq_power, median_freq: the fft periodogram. median_freq (SP_Summaries_fft_wq_50) replaces centroid_freq, as in catch22.
  • whiten_timescale: interpolated zero crossings of both autocorrelations.
  • entropy_pairs: Miller-Madow bias correction.
  • transition_variance: tau from the first negative autocorrelation, and anti-aliased resampling.
  • embedding_dist: hctsa's automatic binning and density normalisation.
  • rs_range, dfa: hctsa's two-regime fit and minimum lengths.
  • Outlier timing: hctsa's trimming.
  • periodicity, ami_timescale, stretch_high, stretch_decreasing: the catch22 v0.6.0 corrections.

API:

  • Masking. A feature that cannot be estimated is set to catch22's mask value, as catch22 does by default. Examples are any feature of a constant series, and dfa on a series shorter than 79 values. mask_nans=False returns NaN instead.
  • Names. Feature names follow catch22 v1.0, e.g. CO_FirstCrossing_acf_1e_pointOfCrossing and transition_variance. The v0.x names still select the same positions, with a FutureWarning, and are removed in v1.8.0. This also fixes aeon's swapped low_freq_power/centroid_freq short names.
  • catch24. The standard deviation is the sample standard deviation, as in catch22, and mean/std are accepted as names.
  • Normalisation. A new normalise=False option z-normalises each series with aeon's numba z-normalisation (z_normalise_series_3d) before computing the features, as catch22 recommends. outlier_norm uses the same function, as it did before, so the outlier features are the same with either option. aeon divides by the population standard deviation where catch22 uses the sample standard deviation, so the scale-dependent features differ slightly from catch22's on short series. Catch22Classifier, Catch22Regressor and Catch22Clusterer pass normalise through. catch24's mean and standard deviation stay those of the series as given. aeon's z-normalisation is wrong beyond about 1e154 and for constant series far from zero; that will be fixed in a separate PR.
  • Extreme scales. The autocorrelation scales each series by a power of two first. That is exact, so values on ordinary series are unchanged, and it keeps its sums of products from under- or overflowing on series of scale around 1e±200, where acf_timescale raised a ZeroDivisionError. periodicity no longer fails on series of length 2.

Values change for most features, so outputs change for every estimator built on Catch22: Catch22Classifier, Catch22Regressor, Catch22Clusterer, CanonicalIntervalForest, DrCIF, RIST, and HIVECOTEV2 through DrCIF.

Validation:

  • On 267 z-normalised series (lengths 3 to 2000, including noise, walks, sines, AR, logistic map, ties, ramps, spikes and constants), all 22 features match pycatch22 1.0.2 to 1e-6, masked and unmasked, with outlier_norm=False (the series are already normalised as catch22 does). The exceptions are 4 rounding-level ties on degenerate series: two stretch_high ties at the mean on short ramps, and two whiten_timescale values from near-constant residuals.
  • New tests check:
    • against stored pycatch22 1.0.2 values;
    • the mask values of a constant series;
    • that every short, full and v0.x name selects the right feature;
    • normalise, and the autocorrelation features at extreme scales.
  • On Linux (Docker, Python 3.12, CI's numba settings), the full estimator checks pass for Catch22 and every estimator built on it, as do the dependent test suites.

Does your contribution introduce a new dependency? If yes, which one?

No.

Any other comments?

  • Expected results were regenerated on Linux (Docker, Python 3.12, CI's numba CPU settings) with the expected-results writer's data and parameters. In the same environment, main's stored results all reproduce. Fourteen entries change, each on both datasets: Catch22Classifier, CanonicalIntervalForestClassifier, DrCIFClassifier, RISTClassifier, HIVECOTEV2, Catch22Regressor and RISTRegressor.
  • Speed. Within 5% of the previous implementation on the benchmarks, although hctsa's definitions do more work. The kernels use numba's fastmath without its no-NaN and no-inf assumptions, and loops over offset windows index slices, so that numba vectorises them. Values can therefore differ from catch22's C in the last bits. transition_variance's low-pass filter is built once per downsampling factor for a batch, rather than for every series. embedding_dist is still about 1.4× slower: hctsa's automatic binning sorts the distances.
  • Raw input. By default aeon still computes features on the series as given, apart from the outlier features with outlier_norm, so existing pipelines keep their behaviour. catch22 defines every feature on z-normalised series.
    • On raw input, 14 features match pycatch22 anyway.
    • transition_variance resamples the mean-centred series to stay offset-invariant; this is identical on z-normalised input.
    • The scale-dependent features differ: mode_5, mode_10, trev, high_fluctuation, periodicity, embedding_dist, low_freq_power and forecast_error. With normalise=True, they differ only through the ratio of the two standard deviations, √(n/(n−1)): by 2.5% at length 20 and 0.13% at 400 for the features linear in scale, and 2–3 times that for trev and low_freq_power. high_fluctuation and periodicity compare against fixed thresholds, so they change on a few percent of series.
    • Those two thresholds (0.04 and 0.01) only make sense on z-normalised data. Whether to make normalise=True the default, here or in the interval forests, needs an accuracy comparison first.
  • Feature order. aeon's order is kept rather than catch22's, so selection by index is unchanged.
  • Tests. test_catch22_optimisations.py is removed. Its checks that single features, channels, unequal-length input and _transform_features masking agree with the full transform are now in test_catch22.py; its checks of the internal caches are dropped.

PR checklist

For all contributions
  • I've added myself to the list of contributors. Alternatively, you can use the @all-contributors bot to do this for you after the PR has been merged.
  • The PR title starts with either [ENH], [MNT], [DOC], [BUG], [REF], [DEP] or [GOV] indicating whether the PR topic is related to enhancement, maintenance, documentation, bugs, refactoring, deprecation or governance.

This pull request includes code written with the assistance of AI.
The code has not yet been reviewed by a human.

feature_names_short mapped SP_Summaries_welch_rect_area_5_1 to
centroid_freq and SP_Summaries_welch_rect_centroid to low_freq_power, the
reverse of catch22's featureList.txt. Selecting either feature by short
name returned the other one.

The pycatch22 reference test listed low_freq_power, which under the old
mapping checked the centroid feature; it now lists centroid_freq so it
keeps checking the same feature.
catch22 v0.6.0 corrected four features whose C implementations differed
from their hctsa definitions, and aeon's numba Catch22 reproduced the old C
behaviour:

- PD_PeriodicityWang_th0_01 and IN_AutoMutualInfoStats_40_gaussian_fmmi
  returned the lag minus one; they now return the lag.
- SB_BinaryStats_mean_longstretch1 ignored the last point and counted runs
  not at the start one too long; it now returns the exact longest run.
- SB_BinaryStats_diff_longstretch0 counted runs not at the start one too
  long and treated zero differences as increases; it now returns the exact
  longest run of non-increasing steps.

Values of these features change. Regenerate the stored basic-motions arrays
(only the four feature columns), the clustering test labels and two
docstring examples, and require pycatch22>=0.6.0 so the reference
comparisons use the corrected C code.
# Conflicts:
#	aeon/transformations/collection/feature_based/tests/test_catch22.py
#	pyproject.toml
# Conflicts:
#	aeon/transformations/collection/feature_based/tests/test_catch22.py
#	aeon/transformations/collection/feature_based/tests/test_catch22_optimisations.py
Parametrise the short-name selection test over all 22 catch22 short/full
name pairs, written out from catch22's feature list rather than taken from
the module, so a mismatch in the module's lists is caught.
catch22 v1.0 re-implements its features to reproduce their hctsa v3.0.0
definitions exactly. Port every kernel to the catch22 v1.2.0 C code, keeping
aeon's feature order, batched FFT and autocorrelation caches and compiled
dispatch:

- histogram mode and ami2: hctsa bin edges and histcounts binning; ami2 adds
  the Panzeri-Treves bias correction
- acf_timescale: interpolated crossing within the series' lags, n - 1 when
  none
- low_freq_power and median_freq (replacing centroid_freq): the fft
  periodogram with a 1e-12 floor
- whiten_timescale: interpolated zero crossings of both autocorrelations
- entropy_pairs: Miller-Madow bias correction
- transition_variance: tau from the first negative autocorrelation, with
  anti-aliased resampling of the mean-centred series
- embedding_dist: hctsa's automatic binning and density normalisation
- rs_range and dfa: hctsa's two-regime fit and minimum lengths
- outlier timing: hctsa's trimming, and outlier_norm z-normalises with the
  sample standard deviation as catch22
- forecast_error, ami_timescale, trev, pnn40: catch22's guards for short
  series

Features that cannot be estimated (e.g. any feature of a constant series)
are masked to catch22's values unless mask_nans=False. Feature names follow
catch22 v1.0; the v0.x names still select the same positions with a
deprecation warning. catch24's standard deviation is the sample standard
deviation, as in catch22.

On z-normalised series every feature matches pycatch22 1.0.2 to 1e-6, except
for rounding-level ties on degenerate series. A test compares against stored
pycatch22 1.0.2 values. Kernels that can return NaN are compiled without
fastmath, which makes the transform about 1.4x slower than before.
Regenerate the stored results that change with the new Catch22 features:
Catch22Classifier, CanonicalIntervalForestClassifier, DrCIFClassifier and
RISTClassifier on both datasets, and Catch22Regressor on CardanoSentiment.

Generated on Linux (Python 3.12, CI's numba CPU settings) with the
expected-results writer's data and parameters, in an environment where
main's stored results all reproduce.
The classifier, regressor and clusterer docstrings still listed the catch22
v0.x feature names. Also rename the FFT cache from welch to spectrum, since
the spectral features now use the fft periodogram.
…zero

z_normalise_series, z_normalise_series_with_mean, z_normalise_series_2d and
z_normalise_series_3d handle the two cases where the mean and standard
deviation they compute are wrong:
- beyond about 1e154 the sum of squares overflowed and every value came out
  0; such series are now scaled by a power of two first;
- when the standard deviation is within rounding of the mean, the mean's
  rounding error could exceed the series' variation, so a constant series
  far from zero normalised to ones and near-constant residuals were not
  centred; such series are now centred a second time, and constant series
  give zeros.

Ordinary series take the same arithmetic as before.
- Use numba's fastmath without its no-NaN and no-inf assumptions, index
  slices in loops over offset windows, and build transition_variance's
  low-pass filters once per downsampling factor for a batch.
- Add normalise, which z-normalises each series with the numba
  z-normalisation before computing the features, as catch22 recommends.
  outlier_norm uses the same function. Catch22Classifier, Catch22Regressor
  and Catch22Clusterer pass normalise through.
- Scale each series by a power of two before the autocorrelation, so its
  sums of products do not under- or overflow at scales around 1e±200.
- Return 0 for periodicity on series of length 2.
- Regenerate the expected results that change.
- Move the useful tests of test_catch22_optimisations.py into
  test_catch22.py and drop the ones of internals.
Revert "[BUG] Fix the numba z-normalisation for overflow and series far
from zero" (7032a1d), which is fixed separately with Normalizer. Catch22
keeps its own copy of _pow2_scale for the autocorrelation, and the
normalise scale-invariance test only checks ordinary scales.
@review-notebook-app

Copy link
Copy Markdown

Review Notebooks

Notebooks changed:

Review visual diffs and comment on any notebook cell. All comments sync back to this PR.


Powered by ReviewNB

@aeon-actions-bot aeon-actions-bot Bot added the enhancement New feature, improvement request or other non-bug code enhancement label Nov 27, 2026
@aeon-actions-bot

Copy link
Copy Markdown
Contributor

Thank you for contributing to aeon

I have added the following labels to this PR based on the title: enhancement.
This PR changes too many different packages (>3) for automatic addition of labels, please manually add package labels if relevant.

The Checks tab will show the status of our automated tests. You can click on individual test runs in the tab or "Details" in the panel below to see more information if there is a failure.

If our pre-commit code quality check fails, please run pre-commit locally and push the fixes to your PR branch.

Don't hesitate to ask questions on the aeon Discord channel if you have any.

PR CI actions

These checkboxes will add labels to enable or disable CI functionality for this PR. This may not take effect immediately, and a new commit may be required to run the new configuration.

  • Run pre-commit checks for all files
  • Run mypy typecheck tests
  • Run all pytest tests and configurations
  • Run all notebook example tests
  • Run numba-disabled codecov tests
  • Disable numba cache loading
  • Regenerate expected results for testing
  • Push an empty commit to re-run CI checks

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature, improvement request or other non-bug code enhancement

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant