Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 23 additions & 23 deletions src/fasttrackpy/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
"f3_f4_heuristic": F3_F4_Sep
}

DEFAULT_CONFIG = str(files("fasttrackpy").joinpath("resources", "config.yml"))
DEFAULT_CONFIG = str(files("fasttrackpy").joinpath("resources").joinpath("config.yml"))

logging.basicConfig(
filename = "fasttrack.log",
Expand Down Expand Up @@ -300,17 +300,17 @@ def fasttrack():
@smoother_options
@heuristic_options
def audio(
file: Union[str, Path] = None,
dir: Union[str,Path] = None,
output: Union[str, Path] = None,
dest: Union[str, Path] = None,
file: str|Path,
dir: str|Path,
output: str|Path,
dest: str|Path,
which_output: str = "winner",
data_output: str = "formants",
smoother_method: str = "dct_smooth_regression",
smoother_order: int = 5,
loss_method: str = "lmse",
xmin:float = 0,
xmax: float = None,
xmax: float|None = None,
min_max_formant:float = 4000,
max_max_formant:float = 7000,
min_duration = 0.05,
Expand Down Expand Up @@ -454,13 +454,13 @@ def audio(
@smoother_options
@heuristic_options
def audio_textgrid(
audio: Union[str, Path] = None,
textgrid: Union[str,Path] = None,
entry_classes: str = None,
target_tier: str = None,
target_labels: str = None,
output: Union[str, Path] = None,
dest: Union[str, Path] = None,
audio: str|Path,
textgrid: str|Path,
entry_classes: str,
target_tier: str,
target_labels: str,
output: str|Path,
dest: str|Path,
which_output: str = "winner",
data_output: str = "formants",
smoother_method: str = "dct_smooth_regression",
Expand Down Expand Up @@ -533,12 +533,12 @@ def audio_textgrid(
if k in heuristic_dict
]

entry_classes = entry_classes.split("|")
entry_classes_list = entry_classes.split("|")

all_candidates = process_audio_textgrid(
audio_path=audio,
textgrid_path=textgrid,
entry_classes=entry_classes,
entry_classes=entry_classes_list,
target_tier=target_tier,
target_labels=target_labels,
min_duration=min_duration,
Expand Down Expand Up @@ -584,13 +584,13 @@ def audio_textgrid(
@smoother_options
@heuristic_options
def corpus(
corpus: str|Path = None,
entry_classes: str = None,
target_tier: str = None,
target_labels: str = None,
output: str|Path = None,
corpus: str|Path,
entry_classes: str,
target_tier: str,
target_labels: str,
output: str|Path,
dest: str|Path,
separate_output: bool = False,
dest: str|Path = None,
which_output: str = "winner",
data_output: str = "formants",
smoother_method: str = "dct_smooth_regression",
Expand Down Expand Up @@ -661,11 +661,11 @@ def corpus(
if k in heuristic_dict
]

entry_classes = entry_classes.split("|")
entry_classes_list = entry_classes.split("|")

all_candidates = process_corpus(
corpus_path = corpus,
entry_classes = entry_classes,
entry_classes = entry_classes_list,
target_tier = target_tier,
target_labels = target_labels,
min_duration = min_duration,
Expand Down
13 changes: 7 additions & 6 deletions src/fasttrackpy/patterns/audio_textgrid.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from collections.abc import Sequence
import parselmouth as pm
from aligned_textgrid import AlignedTextGrid, Word, Phone, SequenceInterval, SequenceTier
import aligned_textgrid
Expand Down Expand Up @@ -66,18 +67,18 @@ def get_candidates(args_dict):

@delayed
@safely(message="There was a problem getting some candidate tracks.")
def get_candidates_delayed(args_dict):
def get_candidates_delayed(args_dict) -> CandidateTracks:
with warnings.catch_warnings():
warnings.simplefilter("ignore")
candidates = CandidateTracks(**args_dict)
if candidates.winner.formants.shape[1] == 1:
warnings.warn("formant tracking error")
return candidates

def run_candidates(arg_list, parallel:bool):
def run_candidates(arg_list, parallel:bool) -> Sequence[CandidateTracks]:
if parallel:
n_jobs = cpu_count()
all_candidates = Parallel(n_jobs=n_jobs)(
all_candidates = Parallel(n_jobs=n_jobs, return_as="list")(
get_candidates_delayed(args_dict=arg) for arg in tqdm(arg_list)
)
return all_candidates
Expand All @@ -103,8 +104,8 @@ def process_audio_textgrid(
smoother: Smoother = Smoother(),
loss_fun: Loss = Loss(),
agg_fun: Agg = Agg(),
heuristics: list[MinMaxHeuristic|SpacingHeuristic] = []
)->list[CandidateTracks]:
heuristics: list[MinMaxHeuristic|SpacingHeuristic|None] = []
)->Sequence[CandidateTracks]:
"""Process an audio and TextGrid file together.

Args:
Expand Down Expand Up @@ -154,7 +155,7 @@ def process_audio_textgrid(

entry_classes = get_interval_classes(textgrid_format=entry_classes)

tg = AlignedTextGrid(textgrid_path=textgrid_path, entry_classes=entry_classes)
tg = AlignedTextGrid(textgrid=textgrid_path, entry_classes=entry_classes)
target_tiers = get_target_tiers(tg, target_tier=target_tier)
target_intervals = get_target_intervals(
target_tiers=target_tiers,
Expand Down
12 changes: 7 additions & 5 deletions src/fasttrackpy/patterns/corpus.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
import os
import sys

from collections.abc import Sequence

CorpusPair = namedtuple("CorpusPair", field_names=["wav", "tg"])

def get_audio_files(
Expand All @@ -32,7 +34,7 @@ def get_audio_files(

def get_corpus(
audio_files:list[Path]
) -> list[tuple[Path, Path]]:
) -> Sequence[CorpusPair]:
wav_tg = [
CorpusPair(wav, wav.with_suffix(".TextGrid"))
for wav in audio_files
Expand All @@ -42,7 +44,7 @@ def get_corpus(

def read_and_associate_tg(
corpus_pair: CorpusPair,
entry_classes:list[SequenceInterval] = [Word, Phone]
entry_classes:Sequence[type[SequenceInterval]] = [Word, Phone]
) -> AlignedTextGrid:
tg = AlignedTextGrid(
textgrid_path=str(corpus_pair.tg),
Expand Down Expand Up @@ -91,8 +93,8 @@ def get_target_intervals(
def get_sound_parts(
intervals: list[SequenceInterval],
window_length: float
):
sound = pm.Sound(str(intervals[0].wav))
) -> Sequence[pm.Sound]:
sound = pm.Sound(str(getattr(intervals[0], "wav")))
sound_parts = [
sound.extract_part(from_time = interval.start-(window_length/2),
to_time = interval.end+(window_length/2))
Expand Down Expand Up @@ -120,7 +122,7 @@ def get_candidates(args_dict):
warnings.warn("formant tracking error")
return candidates

def run_candidates(arg_list, parallel:bool):
def run_candidates(arg_list, parallel:bool) -> Sequence[CandidateTracks]:
if parallel:
n_jobs = cpu_count()
all_candidates = Parallel(n_jobs=n_jobs)(
Expand Down
7 changes: 3 additions & 4 deletions src/fasttrackpy/patterns/just_audio.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import warnings
from pathlib import Path
from typing import Union
from collections.abc import Callable
from collections.abc import Callable, Sequence
import parselmouth as pm
from fasttrackpy import CandidateTracks,\
Smoother,\
Expand Down Expand Up @@ -38,7 +37,7 @@ def is_audio(path: str|Path)->bool:
def process_audio_file(
path: str|Path,
xmin:float = 0,
xmax: float = None,
xmax: float|None = None,
min_max_formant:float = 4000,
max_max_formant:float = 7000,
nstep:int = 20,
Expand Down Expand Up @@ -123,7 +122,7 @@ def get_candidates_delayed(args_dict):
def get_candidates(args_dict):
return process_audio_file(**args_dict)

def run_candidates(arg_list, parallel:bool):
def run_candidates(arg_list, parallel:bool) -> Sequence[CandidateTracks]:
if parallel:
n_jobs = cpu_count()
all_candidates = Parallel(n_jobs=n_jobs)(
Expand Down
9 changes: 5 additions & 4 deletions src/fasttrackpy/processors/aggs.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import numpy as np
import numpy.typing as npt
from typing import Union
from collections.abc import Callable

Expand All @@ -11,19 +12,19 @@ class Agg:

def __init__(
self,
method: Union[str, Callable]= "agg_sum",
method: str|Callable = "agg_sum",
**kwargs
):
self.method = self._get_method(method)
self.method_args = kwargs

def _get_method(
self,
method:Union[str, Callable]
):
method:str|Callable[[npt.NDArray], float]
)->Callable:
if callable(method):
return method
if method == "agg_sum":
else:
return agg_sum

def aggregate(
Expand Down
16 changes: 8 additions & 8 deletions src/fasttrackpy/processors/heuristic.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
from dataclasses import dataclass, field
import numpy as np
import numpy.typing as npt
from collections.abc import Mapping
from typing import TypeVar, TYPE_CHECKING, Literal, Annotated
from collections.abc import Sequence
if TYPE_CHECKING:
from fasttrackpy import OneTrack

TrackType = TypeVar("OneTrack")

@dataclass
class MinMaxHeuristic:
Expand Down Expand Up @@ -40,7 +41,7 @@ class MinMaxHeuristic:
number: int = 1
boundary: float|int|np.floating = 1200

def eval(self, track: TrackType):
def eval(self, track: "OneTrack"):
"""
Evaluate whether or not the track passes the
heuristic
Expand All @@ -63,8 +64,7 @@ def eval(self, track: TrackType):
track.log_parameters[self.number-1,0]*
np.sqrt(2)
)

if self.measure == "bandwidth":
else:
mean_value = np.exp(
track.bandwidth_parameters[self.number-1, 0]
*np.sqrt(2)
Expand Down Expand Up @@ -98,16 +98,16 @@ class SpacingHeuristic:
bottom_diff (float|int|np.floating):
The spacing of the bottom formants
"""
top: list[int] = field(default_factory=lambda: [3])
bottom: list[int] = field(default_factory=lambda: [1,2])
top: Sequence[int]|npt.NDArray[np.int_] = field(default_factory=lambda: [3])
bottom: Sequence[int]|npt.NDArray[np.int_] = field(default_factory=lambda: [1,2])
top_diff: float|int|np.floating = 2000
bottom_diff: float|int|np.floating = 500

def __post_init__(self):
self.top = np.array(self.top)
self.bottom = np.array(self.bottom)

def eval(self, track:TrackType):
def eval(self, track:"OneTrack"):
"""
Evaluate whether or not the track passes
the heuriustic
Expand All @@ -123,7 +123,7 @@ def eval(self, track:TrackType):
"""
nformants = track.n_formants

if nformants < self.top.max():
if nformants < np.array(self.top).max():
return 0

top_values = np.array([
Expand Down
Loading