From afa2660446129ebd4974c7632804b428ad0a9bbb Mon Sep 17 00:00:00 2001 From: JoFrhwld Date: Wed, 26 Jun 2024 09:48:11 -0400 Subject: [PATCH] candidate tracks now indexable --- src/fasttrackpy/tracks.py | 9 ++++++++- tests/test_tracks.py | 12 +++++++++++- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/src/fasttrackpy/tracks.py b/src/fasttrackpy/tracks.py index c2f4378..d3b16d7 100644 --- a/src/fasttrackpy/tracks.py +++ b/src/fasttrackpy/tracks.py @@ -14,6 +14,7 @@ from aligned_textgrid.sequences.tiers import TierGroup import polars as pl +from collections.abc import Sequence from typing import Union, Literal import warnings @@ -381,7 +382,7 @@ def spectrogram(self, **kwargs): -class CandidateTracks(Track): +class CandidateTracks(Track, Sequence): """A class for candidate tracks for a single formant You can provide *either* @@ -510,6 +511,12 @@ def __init__( self.winner_idx = np.argmin(self.smooth_errors) self.winner = self.candidates[self.winner_idx] + + def __getitem__(self, idx:int) -> OneTrack: + return self.candidates[idx] + + def __len__(self) -> int: + return len(self.candidates) @property def file_name(self): diff --git a/tests/test_tracks.py b/tests/test_tracks.py index 10d4bbe..e4ec751 100644 --- a/tests/test_tracks.py +++ b/tests/test_tracks.py @@ -99,4 +99,14 @@ def test_candidate_tracks_default(self): big_df4 = candidates.to_df(which = "all", output="param") assert isinstance(big_df3, pl.DataFrame) - assert isinstance(big_df4, pl.DataFrame) \ No newline at end of file + assert isinstance(big_df4, pl.DataFrame) + + def test_candidate_sequence(self): + candidates = CandidateTracks( + sound = SOUND + ) + + assert len(candidates) == 20 + + one_cand = candidates[0] + assert isinstance(one_cand, OneTrack) \ No newline at end of file