pyhctsa.calculator.FeatureCalculator

class pyhctsa.calculator.FeatureCalculator(config_path=None)

A configuration-driven feature extraction calculator for time series data.

config

The parsed YAML configuration containing feature definitions.

Type:

dict

feature_funcs

A dictionary mapping feature labels to their corresponding callable functions.

Type:

dict

Parameters:
config_path : str or None, optional

Path to the YAML configuration file. If None, uses the default ‘feature_config.yaml’ configuration file in the package configurations directory.

Notes

Besides the operation’s own arguments, each entry of a feature’s configs list may carry these meta keys (not passed to the function):

  • zscore: True: z-score the series first (label gets no suffix; without it, _raw);

  • abs: True: take the absolute value last (suffix _abs);

  • preprocess: one more input transform, applied to the (z-scored) series before abs; its label suffix follows _abs. The values:

    • decimate_ac1e (suffix _dec): with tau the floored 1/e autocorrelation time, the series is decimated to one sample per tau from each of the tau starting phases (x_z[p::tau], p = 0, ..., tau - 1), each subseries z-scored again (with zscore: True). The function is computed on every subseries that has at least 3 samples and at least the operation’s min_length, and its outputs are averaged over them (field by field, ignoring NaN). The feature is NaN if tau cannot be determined (e.g. the ACF never falls to 1/e) or no subseries qualifies. See pyhctsa.utils.decimation_phases();

    • decimate_ac1e-rank (_dec-rank): the same, with tau set from the ranks of the series (get_tau(x_z, 'ac1e-rank')), for features that depend on the data only through their order;

    • detrend (_lindt): the least-squares line removed and the residual z-scored again (with zscore: True); NaN if the series is a line up to round-off;

    • diff1 (_diff1): diff(x_z);

    • zscore_abs (_mag): zscore(abs(x_z)); zscore_sign (_sign): zscore(sign(x_z)). (abs: True alone is abs(x_z).)

  • select / exclude: keep or drop keys of a dict-valued output.

A feature’s entry (alongside configs) may also give min_length: the shortest series on which it is defined (an int, or an expression in its parameters such as "tau + 1"; see _MIN_LENGTH_NAMESPACE for the helpers it may use). It applies to the series the function receives (after zscore/preprocess); for a shorter series every output of that feature is NaN, so the set of feature columns never depends on the length. The minima are listed in min_lengths. field_min_length (a mapping {field: int or expression}; a field name may contain {k}, an integer the expression can use) gives single fields of a dict output a larger minimum: below it that field alone is NaN (field_min_lengths).

Examples

>>> fc = FeatureCalculator()  # Load default configuration
>>> x = np.random.randn(1000)
>>> df = fc.extract(x)

Methods

extract(data[, labels, verbose, distributor])

Run the configured feature extractor over one or more time series and return a single tidy pandas.DataFrame.

save(path, values[, meta])

Write values (the DataFrame the last extract() returned) together with its quality, errors and calc_time to path.

extract(data, labels=None, verbose=False, distributor=None)

Run the configured feature extractor over one or more time series and return a single tidy pandas.DataFrame.

Parameters:
data : ArrayLike or list[ArrayLike]

The input time series data. Two forms are accepted:

  • Single series: a 1-D array, a pandas Series, or a list or tuple of real numbers.

  • Multiple series: a 2-D array with one series per row, or a list or tuple of series (arrays, pandas Series, lists or tuples), which may differ in length.

A pandas DataFrame is not accepted, since it does not say whether its rows or its columns are the series: pass df.to_numpy() (rows) or df.to_numpy().T (columns).

labels : ArrayLike, list, str, int, or None, optional

Labels for each time series. The order of labels is assumed to match the order of the time series as passed in the data argument.

verbose : bool, optional

Whether to print progress and timing information. Default is False.

distributor : object, optional

Optional distributor for parallel computation.

Returns:

A DataFrame with one row per input series and one column per feature. Every value is a float: a real, finite number, or NaN. Why a value is NaN is in quality, which this call also sets, with errors and calc_time:

  • quality: a uint8 DataFrame of the same shape, one Quality code per value (0 good, 1 error, 2 NaN, 3 Inf, 4 -Inf, 5 complex, 6 empty, 7 link error, 8 non-scalar, 9 rejected input; see pyhctsa.quality).

  • errors: one row per feature function that raised (instance, feature, error_type, message), and one per series rejected by the input check (feature is None). The user’s home directory is replaced by ~ in message.

  • calc_time: seconds per series and feature function.

A series that fails the input check (too short, constant, or containing NaN or Inf) is not computed, and its row is coded Quality.REJECTED. If every series fails it, a ValueError is raised.

A UserWarning (one per call) says how many series consist almost entirely of repeated values, whose rank-, bin- and symbol-based features can change when the series is rescaled or shifted (see “Series with many repeated values” in the usage guide). It changes no value.

Return type:

pd.DataFrame

save(path, values, meta=None)

Write values (the DataFrame the last extract() returned) together with its quality, errors and calc_time to path.

The format follows the suffix: .npz (no extra dependency) or .parquet (needs pyarrow). Read it back with pyhctsa.load_results().

The file also records which feature set produced the values (meta of load_results()): the pyhctsa version, the time of saving, the configuration (config_name, its file name only, and config_sha256, the SHA-256 of its text, which changes with any edit), the number of feature functions, whether feature_funcs was changed after the configuration built it (feature_functions_modified), and the operations skipped for missing optional dependencies (skipped_operations, missing_dependencies, n_skipped_feature_functions). meta adds to these, or overrides them.

A file you share therefore shows the series labels (the index), the configuration’s file name and hash, the pyhctsa version, when it was saved and which optional packages were missing, besides the values, quality codes, errors and timings. pyhctsa records no file paths (not even config_path), user names or host names. The message column of errors holds each exception’s message, with the user’s home directory replaced by ~ (/Users/<name>/... becomes ~/...). A message can still contain a path outside the home directory, or other text that the failing package or custom feature function chose to include.

Parameters:
values : DataFrame

meta : dict | None

Return type:

Path