Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fast topological features #1252

Merged
merged 20 commits into from
Jan 27, 2024
Merged

Fast topological features #1252

merged 20 commits into from
Jan 27, 2024

Conversation

kasyanovse
Copy link
Collaborator

This is a 🙋 feature or enhancement.

Summary

Ускоренная версия топологических фич (в 30 раз). От обычных топологических фич отличаются достаточно сильно:

  1. Скинул весь код в один класс.
  2. Отказался от использования giotto-tda в пользу giotto-ph.
  3. Изменил расчет фич из топологических фич для максимального ускорения.

Context

Inspired by #1241.

@kasyanovse kasyanovse added enhancement New feature or request time series related to time series processing labels Jan 19, 2024
@kasyanovse kasyanovse self-assigned this Jan 19, 2024
@pep8speaks
Copy link

pep8speaks commented Jan 19, 2024

Hello @kasyanovse! Thanks for updating this PR. We checked the lines you've touched for PEP 8 issues, and found:

There are currently no PEP 8 issues detected in this Pull Request. Cheers! 🍻

Comment last updated at 2024-01-27 18:04:17 UTC

Copy link
Contributor

github-actions bot commented Jan 19, 2024

Code in this pull request still contains PEP8 errors, please write the /fix-pep8 command in the comments below to create commit with automatic fixes.

Comment last updated at

Copy link

codecov bot commented Jan 19, 2024

Codecov Report

Attention: 30 lines in your changes are missing coverage. Please review.

Comparison is base (5e726e9) 80.05% compared to head (8f895c3) 79.84%.

Files Patch % Lines
...erations/topological/fast_topological_extractor.py 30.23% 30 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1252      +/-   ##
==========================================
- Coverage   80.05%   79.84%   -0.21%     
==========================================
  Files         149      150       +1     
  Lines       10278    10322      +44     
==========================================
+ Hits         8228     8242      +14     
- Misses       2050     2080      +30     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@kasyanovse
Copy link
Collaborator Author

/fix-pep8

@valer1435 valer1435 requested a review from v1docq January 24, 2024 08:27
@valer1435
Copy link
Collaborator

Хотелось бы тест на то, что фичи получаются +- те же, что и в обычном

Comment on lines 37 to 42
maxdim=self.max_homology_dimension,
coeff=2,
metric='euclidean',
n_threads=1,
collapse_edges=False)["dgms"]
result = list()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Неплохо бы вынести хотя бы метрику, n_threads, и размерность гомологий в гиперпараметры. Очень сильно будут влиять на итоговые диаграммы

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

a749678
b2c5f3e

Вынес. Распараллеливание сделал на уровне transform метода.

Comment on lines 12 to 18
class FastTopologicalFeaturesImplementation(DataOperationImplementation):
def __init__(self, params: Optional[OperationParameters] = None):
super().__init__(params)
self.points_count = params.get('points_count')
self.max_homology_dimension = 1
self.feature_funs = (lambda x: np.quantile(x, (0.1, 0.25, 0.5, 0.75, 0.9)), )
self.shape = None
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.Поинт клауд реализовался через абстракцию траекторной матрицы что связывало с собой эти классы и позволяло реализовывать над ними алгебру (складывать, вычитать, делить) для федота это офк лишнее, но просто имей ввиду. Человеку который никогда не видел фильтрацию виетори рипса и персистентных диаграмм
сходу придется учить giotta. Поэтому отказ от собственных абстрактных классов в пользу решений из коробки не всегда самое очевидное решение

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Согласен, код обычных топо фич читается лучше, но, надеюсь, в этот код мало кому придется лазить)

@kasyanovse
Copy link
Collaborator Author

kasyanovse commented Jan 24, 2024

Хотелось бы тест на то, что фичи получаются +- те же, что и в обычном

Здесь из топологии генерируются другие фичи, поэтому смысла в этом нет. Сравнение предсказаний для lagged-topo-ridge на картинке. Я бы не сказал, что есть принципиальные отличия, однако можно сказать что fast_topo не уловило низкочастотные составляющие. Это жертва ради скорости, но если нужно, то качество можно улучшить, докинув к квантилям еще и стат фичи.

image

Код для генерации картинки
import logging
from time import perf_counter
import pickle

import numpy as np
from matplotlib import pyplot as plt

from fedot.core.pipelines.node import PipelineNode
from fedot.core.pipelines.pipeline import Pipeline
from fedot.core.repository.tasks import Task, TaskTypesEnum, TsForecastingParams
from fedot.api.main import Fedot
from fedot.core.data.data import InputData
from fedot.core.repository.dataset_types import DataTypesEnum
from fedot.core.data.data_split import train_test_data_setup


RANDOM_SEED = 100


def get_data(data_length=500, test_length=100):
    garmonics = [(0.1, 0.9), (0.1, 1), (0.1, 1.1), (0.05, 2), (0.05, 5), (1, 0.02)]
    time = np.linspace(0, 100, data_length)
    data = time * 0
    for g in garmonics:
        data += g[0] * np.sin(g[1] * 2 * np.pi / time[-1] * 25 * time)

    data = InputData(idx=np.arange(0, data.shape[0]),
                     features=data,
                     target=data,
                     task=Task(TaskTypesEnum.ts_forecasting,
                               TsForecastingParams(forecast_length=test_length)),
                     data_type=DataTypesEnum.ts)
    return train_test_data_setup(data,
                                 validation_blocks=1,
                                 split_ratio=(data_length - test_length) / ((data_length - test_length) + test_length))


def plot_ppl(ppls, train, test, labels):
    _, ax = plt.subplots()
    limits = len(test.target)
    ax.plot(train.idx[-limits:], train.target[-limits:], label='train')
    ax.plot(test.idx, test.target, label='test')
    for label, ppl in zip(labels, ppls):
        predict = ppl.predict(test).predict
        ax.plot(test.idx[-len(predict):], predict, label=label)
    ax.legend()


if __name__ == '__main__':
    train, test = get_data()
    node = PipelineNode('lagged')
    node = PipelineNode('fast_topological_features', nodes_from=[node])
    node = PipelineNode('ridge', nodes_from=[node])
    ppl1 = Pipeline(node)
    t0 = perf_counter()
    ppl1.fit(train)
    ppl1.predict(test)
    print(perf_counter() - t0)

    train, test = get_data()
    node = PipelineNode('lagged')
    node = PipelineNode('topological_features', nodes_from=[node])
    node = PipelineNode('ridge', nodes_from=[node])
    ppl2 = Pipeline(node)
    t0 = perf_counter()
    ppl2.fit(train)
    ppl2.predict(test)
    print(perf_counter() - t0)

    plot_ppl([ppl1, ppl2], train, test, ('fast_topo', 'topo'))

Copy link
Collaborator

@Lopa10ko Lopa10ko left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm :)
я все nit-pick'и исчерпал

@kasyanovse kasyanovse merged commit 5efd0fb into master Jan 27, 2024
9 of 10 checks passed
@kasyanovse kasyanovse deleted the add_topo2_speedup2 branch January 27, 2024 19:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request time series related to time series processing
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants