Skip to content

Commit

Permalink
Fixed linting errors
Browse files Browse the repository at this point in the history
  • Loading branch information
mbsantiago committed Nov 23, 2023
1 parent 8ee0d28 commit d2e0bc8
Show file tree
Hide file tree
Showing 11 changed files with 63 additions and 57 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ lint: ## Run ruff, black, mypy linters.
$(ENV_PREFIX)ruff $(PROJECT_NAME)/
$(ENV_PREFIX)black --check $(PROJECT_NAME)/
$(ENV_PREFIX)black --check tests/
$(ENV_PREFIX)mypy $(PROJECT_NAME)/
$(ENV_PREFIX)mypy $(PROJECT_NAME)/ --config-file pyproject.toml

.PHONY: test-watch
test-watch: ## Run tests and generate coverage report.
Expand Down
10 changes: 5 additions & 5 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,9 @@ name = "soundevent"
version = "0.7.0"
description = "soundevent is an open-source Python package for the computational biocoustic community, providing standardized tools for sound event analysis and data management."
authors = [
{ name = "Santiago Martinez", email = "[email protected]" },
]
dependencies = [
"pydantic>=2.0",
"email-validator>=2.1.0.post1",
{ name = "Santiago Martinez", email = "[email protected]" },
]
dependencies = ["pydantic>=2.0", "email-validator>=2.1.0.post1"]
requires-python = ">=3.8"
readme = "README.md"
license = { text = "MIT" }
Expand Down Expand Up @@ -87,6 +84,9 @@ profile = "black"

[tool.mypy]
ignore_missing_imports = true
show_error_codes = true
show_error_code_links = true
disable_error_code = ["call-overload", "misc"]

[tool.coverage.run]
branch = true
Expand Down
19 changes: 9 additions & 10 deletions src/soundevent/audio/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,17 @@
Currently only supports reading and writing of .wav files.
"""

import os
from pathlib import Path
from typing import Dict, Optional, Tuple

import numpy as np
import soundfile as sf
import xarray as xr

from soundevent import data
from soundevent.audio.chunks import parse_into_chunks
from soundevent.audio.media_info import extract_media_info_from_chunks
from soundevent.audio.raw import RawData
from soundevent.data.clips import Clip
from soundevent.data.recordings import PathLike, Recording

__all__ = [
"load_audio",
Expand All @@ -35,7 +34,7 @@


def load_audio(
path: PathLike,
path: data.PathLike,
offset: int = 0,
samples: Optional[int] = None,
) -> Tuple[np.ndarray, int]:
Expand Down Expand Up @@ -99,8 +98,8 @@ def load_audio(


def load_recording(
recording: Recording,
audio_dir: Optional[PathLike] = None,
recording: data.Recording,
audio_dir: Optional[data.PathLike] = None,
) -> xr.DataArray:
"""Load a recording from a file.
Expand All @@ -121,7 +120,7 @@ def load_recording(
path = recording.path

if audio_dir is not None:
path = os.path.join(audio_dir, path)
path = Path(audio_dir) / path

data, _ = load_audio(path)
return xr.DataArray(
Expand All @@ -147,8 +146,8 @@ def load_recording(


def load_clip(
clip: Clip,
audio_dir: Optional[PathLike] = None,
clip: data.Clip,
audio_dir: Optional[data.PathLike] = None,
) -> xr.DataArray:
"""Load a clip from a file.
Expand Down Expand Up @@ -177,7 +176,7 @@ def load_clip(

path = recording.path
if audio_dir is not None:
path = os.path.join(audio_dir, path)
path = Path(audio_dir) / path

data, _ = load_audio(
path,
Expand Down
9 changes: 6 additions & 3 deletions src/soundevent/io/aoef/annotation_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
class AnnotationProjectObject(AnnotationSetObject):
"""Schema definition for an annotation project object in AOEF format."""

collection_type: Literal["annotation_project"] = "annotation_project"
collection_type: Literal["annotation_project"] = "annotation_project" # type: ignore
name: str
description: Optional[str] = None
instructions: Optional[str] = None
Expand All @@ -32,7 +32,10 @@ def __init__(
)
)

def to_aoef(self, obj: data.AnnotationProject) -> AnnotationProjectObject:
def to_aoef(
self,
obj: data.AnnotationProject, # type: ignore
) -> AnnotationProjectObject:
tasks = [
self.annotation_task_adapter.to_aoef(task)
for task in obj.tasks or []
Expand Down Expand Up @@ -63,7 +66,7 @@ def to_aoef(self, obj: data.AnnotationProject) -> AnnotationProjectObject:

def to_soundevent(
self,
obj: AnnotationProjectObject,
obj: AnnotationProjectObject, # type: ignore
) -> data.AnnotationProject:
annotation_set = super().to_soundevent(obj)

Expand Down
6 changes: 3 additions & 3 deletions src/soundevent/io/aoef/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@
class DatasetObject(RecordingSetObject):
"""Schema definition for a dataset object in AOEF format."""

collection_type: Literal["dataset"] = "dataset"
collection_type: Literal["dataset"] = "dataset" # type: ignore
name: str
description: Optional[str] = None


class DatasetAdapter(RecordingSetAdapter):
def to_aoef(
self,
obj: data.Dataset,
obj: data.Dataset, # type: ignore
) -> DatasetObject:
recording_set = super().to_aoef(obj)
return DatasetObject(
Expand All @@ -31,7 +31,7 @@ def to_aoef(

def to_soundevent(
self,
obj: DatasetObject,
obj: DatasetObject, # type: ignore
) -> data.Dataset:
recording_set = super().to_soundevent(obj)
return data.Dataset(
Expand Down
11 changes: 4 additions & 7 deletions src/soundevent/io/aoef/evaluation.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,19 +193,16 @@ def to_soundevent(
sound_event_annotation
)

for clip_annotations in obj.clip_annotations or []:
self.clip_annotations_adapter.to_soundevent(clip_annotations)
clip_annotations = [
self.clip_annotations_adapter.to_soundevent(clip_predictions)
for clip_predictions in obj.clip_annotations or []
]

for sound_event_prediction in obj.sound_event_predictions or []:
self.sound_event_prediction_adapter.to_soundevent(
sound_event_prediction
)

clip_annotations = [
self.clip_annotations_adapter.to_soundevent(clip_predictions)
for clip_predictions in obj.clip_annotations or []
]

clip_predictions = [
self.clip_predictions_adapter.to_soundevent(evaluated_clip)
for evaluated_clip in obj.clip_predictions or []
Expand Down
12 changes: 9 additions & 3 deletions src/soundevent/io/aoef/evaluation_set.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,17 @@
class EvaluationSetObject(AnnotationSetObject):
"""Schema definition for an evaluation set object in AOEF format."""

collection_type: Literal["evaluation_set"] = "evaluation_set"
collection_type: Literal["evaluation_set"] = "evaluation_set" # type: ignore
name: str
description: Optional[str] = None
evaluation_tags: Optional[List[int]] = None


class EvaluationSetAdapter(AnnotationSetAdapter):
def to_aoef(self, obj: data.EvaluationSet) -> EvaluationSetObject:
def to_aoef(
self,
obj: data.EvaluationSet, # type: ignore
) -> EvaluationSetObject:
annotation_set = super().to_aoef(obj)
return EvaluationSetObject(
uuid=annotation_set.uuid,
Expand All @@ -36,7 +39,10 @@ def to_aoef(self, obj: data.EvaluationSet) -> EvaluationSetObject:
else None,
)

def to_soundevent(self, obj: EvaluationSetObject) -> data.EvaluationSet:
def to_soundevent(
self,
obj: EvaluationSetObject, # type: ignore
) -> data.EvaluationSet:
annotation_set = super().to_soundevent(obj)
return data.EvaluationSet(
**{
Expand Down
6 changes: 3 additions & 3 deletions src/soundevent/io/aoef/model_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@


class ModelRunObject(PredictionSetObject):
collection_type: Literal["model_run"] = "model_run"
collection_type: Literal["model_run"] = "model_run" # type: ignore
name: str
version: Optional[str] = None
description: Optional[str] = None


class ModelRunAdapter(PredictionSetAdapter):
def to_aoef(self, obj: data.ModelRun) -> ModelRunObject:
def to_aoef(self, obj: data.ModelRun) -> ModelRunObject: # type: ignore
prediction_set = super().to_aoef(obj)
return ModelRunObject(
uuid=prediction_set.uuid,
Expand All @@ -30,7 +30,7 @@ def to_aoef(self, obj: data.ModelRun) -> ModelRunObject:
description=obj.description,
)

def to_soundevent(self, obj: ModelRunObject) -> data.ModelRun:
def to_soundevent(self, obj: ModelRunObject) -> data.ModelRun: # type: ignore
prediction_set = super().to_soundevent(obj)
return data.ModelRun(
**dict(prediction_set),
Expand Down
38 changes: 19 additions & 19 deletions src/soundevent/io/load.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,16 @@
if sys.version_info >= (3, 8):
from typing import Literal
else:
from typing_extensions import Literal
from typing_extensions import Literal # pragma: no cover


@overload
def load(
path: data.PathLike,
audio_dir: Optional[data.PathLike] = None,
format: Optional[str] = "aoef",
type: Literal["dataset"] = "dataset",
) -> data.Dataset:
type: Literal["recording_set"] = "recording_set", # type: ignore
) -> data.RecordingSet: # type: ignore
...


Expand All @@ -27,8 +27,8 @@ def load(
path: data.PathLike,
audio_dir: Optional[data.PathLike] = None,
format: Optional[str] = "aoef",
type: Literal["annotation_set"] = "annotation_set",
) -> data.AnnotationSet:
type: Literal["dataset"] = "dataset", # type: ignore
) -> data.Dataset: # type: ignore
...


Expand All @@ -37,8 +37,8 @@ def load(
path: data.PathLike,
audio_dir: Optional[data.PathLike] = None,
format: Optional[str] = "aoef",
type: Literal["annotation_project"] = "annotation_project",
) -> data.AnnotationProject:
type: Literal["annotation_set"] = "annotation_set", # type: ignore
) -> data.AnnotationSet: # type: ignore
...


Expand All @@ -47,8 +47,8 @@ def load(
path: data.PathLike,
audio_dir: Optional[data.PathLike] = None,
format: Optional[str] = "aoef",
type: Literal["prediction_set"] = "prediction_set",
) -> data.PredictionSet:
type: Literal["annotation_project"] = "annotation_project", # type: ignore
) -> data.AnnotationProject: # type: ignore
...


Expand All @@ -57,8 +57,8 @@ def load(
path: data.PathLike,
audio_dir: Optional[data.PathLike] = None,
format: Optional[str] = "aoef",
type: Literal["model_run"] = "model_run",
) -> data.ModelRun:
type: Literal["prediction_set"] = "prediction_set", # type: ignore
) -> data.PredictionSet: # type: ignore
...


Expand All @@ -67,8 +67,8 @@ def load(
path: data.PathLike,
audio_dir: Optional[data.PathLike] = None,
format: Optional[str] = "aoef",
type: Literal["evaluation_set"] = "evaluation_set",
) -> data.EvaluationSet:
type: Literal["model_run"] = "model_run", # type: ignore
) -> data.ModelRun: # type: ignore
...


Expand All @@ -77,8 +77,8 @@ def load(
path: data.PathLike,
audio_dir: Optional[data.PathLike] = None,
format: Optional[str] = "aoef",
type: Literal["evaluation"] = "evaluation",
) -> data.Evaluation:
type: Literal["evaluation_set"] = "evaluation_set", # type: ignore
) -> data.EvaluationSet: # type: ignore
...


Expand All @@ -87,17 +87,17 @@ def load(
path: data.PathLike,
audio_dir: Optional[data.PathLike] = None,
format: Optional[str] = "aoef",
type: Literal["recording_set"] = "recording_set",
) -> data.RecordingSet:
type: Literal["evaluation"] = "evaluation", # type: ignore
) -> data.Evaluation: # type: ignore
...


def load(
path: data.PathLike,
audio_dir: Optional[data.PathLike] = None,
format: Optional[str] = "aoef",
type: Optional[DataType] = None,
) -> DataObject:
type: Optional[DataType] = None, # type: ignore
) -> DataObject: # type: ignore
if format is None:
format = infer_format(path)

Expand Down
6 changes: 3 additions & 3 deletions src/soundevent/plot/tags.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""Functions for plotting tags."""

from itertools import cycle
from typing import Optional
from typing import Dict, Optional, List

import numpy as np
from matplotlib.axes import Axes
Expand All @@ -26,7 +26,7 @@ def __init__(
num_colors: int = 20,
):
"""Initialize color mapper."""
self._tags = {}
self._tags: Dict[data.Tag, str] = {}

colormap = get_cmap(cmap)
self._colors = cycle(
Expand Down Expand Up @@ -75,7 +75,7 @@ def add_tags_legend(
for tag in color_mapper._tags:
color = color_mapper.get_color(tag)
handles.append(ax.scatter([], [], color=color))
labels.append(tag)
labels.append(str(tag))

ax.legend(handles, labels, loc="upper right")

Expand Down
1 change: 1 addition & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ extras = all
deps =
pytest>=6
hypothesis
html5lib
commands =
pytest {tty:--color=yes} {posargs:tests}

Expand Down

0 comments on commit d2e0bc8

Please sign in to comment.