From 6d2807d00788471ce633abe54c43e842f82c8de0 Mon Sep 17 00:00:00 2001 From: Mathieu Dupont <108517594+mathieudpnt@users.noreply.github.com> Date: Mon, 6 Jul 2026 11:10:38 +0200 Subject: [PATCH 1/4] implement timezone in spectro_data.plot() --- src/osekit/core/spectro_data.py | 5 ++-- tests/test_spectro.py | 49 +++++++++++++++++++++++++++++++-- 2 files changed, 49 insertions(+), 5 deletions(-) diff --git a/src/osekit/core/spectro_data.py b/src/osekit/core/spectro_data.py index cfeac06f..631bd490 100644 --- a/src/osekit/core/spectro_data.py +++ b/src/osekit/core/spectro_data.py @@ -17,7 +17,6 @@ import matplotlib.pyplot as plt import numpy as np import pandas as pd -from matplotlib.dates import date2num from pandas import Timedelta from scipy.signal import ShortTimeFFT, welch @@ -466,7 +465,7 @@ def plot( sx = sx if scale is None else scale.rescale(sx_matrix=sx, original_scale=freq) - ax.xaxis_date() + ax.xaxis_date(tz=time.tz) if time.tz else ax.xaxis_date() ax.imshow( sx, vmin=self._v_lim[0], @@ -475,7 +474,7 @@ def plot( origin="lower", aspect="auto", interpolation="none", - extent=(date2num(time[0]), date2num(time[-1]), freq[0], freq[-1]), + extent=(time[0], time[-1], freq[0], freq[-1]), ) def get_db_value(self, sx: np.ndarray | None = None) -> np.ndarray: diff --git a/tests/test_spectro.py b/tests/test_spectro.py index 9fc38ae3..5ca5950f 100644 --- a/tests/test_spectro.py +++ b/tests/test_spectro.py @@ -1,6 +1,7 @@ from __future__ import annotations import contextlib +import datetime import gc from contextlib import nullcontext from pathlib import Path @@ -9,7 +10,6 @@ import numpy as np import pandas as pd import pytest -from matplotlib.dates import num2date from pandas import Timedelta, Timestamp from scipy.signal import ShortTimeFFT from scipy.signal.windows import hamming @@ -1434,7 +1434,6 @@ def mock_imshow( assert plot_kwargs["interpolation"] == "none" t1, t2, f1, f2 = plot_kwargs["extent"] - t1, t2 = map(num2date, (t1, t2)) t1, t2 = map(Timestamp, (t1, t2)) assert t1 == sd.begin assert t2 == sd.end @@ -1442,6 +1441,52 @@ def mock_imshow( assert f2 == sd.fft.f[-1] +@pytest.mark.parametrize( + "audio_files", + [ + {"date_begin": Timestamp("2026-01-01 00:00:00", tz="UTC")}, + {"date_begin": Timestamp("2026-01-01 00:00:00", tz="Europe/Paris")}, + {"date_begin": Timestamp("2026-06-15 12:00:00", tz="America/New_York")}, + {"date_begin": Timestamp("2025-12-31 23:59:59", tz="Asia/Tokyo")}, + {"date_begin": Timestamp("2025-12-31 23:59:59")}, + ], + indirect=True, + ids=["utc", "paris", "new_york", "tokyo", "naive"], +) +def test_plot_timezone( + audio_files: tuple[list[AudioFile], ...], + monkeypatch: pytest.MonkeyPatch, +) -> None: + audio_files, _ = audio_files + ad = AudioData.from_files(audio_files) + sd = SpectroData.from_audio_data( + data=ad, + fft=ShortTimeFFT(hamming(1024), 512, ad.sample_rate), + ) + + called_ax = set() + + def mock_imshow( + self: plt.Axes, + sx: np.ndarray, + **kwargs: str, + ) -> None: + called_ax.add(self) + + monkeypatch.setattr(plt.Axes, "imshow", mock_imshow) + + _, ax = plt.subplots() + sd.plot(ax=ax) + spectro_data_timezone = called_ax.pop() + + if sd.begin.tz: + assert isinstance(spectro_data_timezone.xaxis.units, datetime.tzinfo) + else: + assert spectro_data_timezone.xaxis.units is None + + + + def test_spectro_default_v_lim(audio_files: pytest.fixture) -> None: files, _ = audio_files ad = AudioData.from_files(files) From 1201bd285fd5bd81361be500aa43124086e33dfa Mon Sep 17 00:00:00 2001 From: Mathieu Dupont <108517594+mathieudpnt@users.noreply.github.com> Date: Mon, 6 Jul 2026 16:40:12 +0200 Subject: [PATCH 2/4] hotfix test tz --- tests/test_spectro.py | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/tests/test_spectro.py b/tests/test_spectro.py index 5ca5950f..7c107180 100644 --- a/tests/test_spectro.py +++ b/tests/test_spectro.py @@ -1445,13 +1445,12 @@ def mock_imshow( "audio_files", [ {"date_begin": Timestamp("2026-01-01 00:00:00", tz="UTC")}, - {"date_begin": Timestamp("2026-01-01 00:00:00", tz="Europe/Paris")}, - {"date_begin": Timestamp("2026-06-15 12:00:00", tz="America/New_York")}, + {"date_begin": Timestamp("2026-01-01 00:00:00", tz="Etc/GMT+2")}, {"date_begin": Timestamp("2025-12-31 23:59:59", tz="Asia/Tokyo")}, {"date_begin": Timestamp("2025-12-31 23:59:59")}, ], indirect=True, - ids=["utc", "paris", "new_york", "tokyo", "naive"], + ids=["utc", "%z", "%Z", "naive"], ) def test_plot_timezone( audio_files: tuple[list[AudioFile], ...], @@ -1477,12 +1476,12 @@ def mock_imshow( _, ax = plt.subplots() sd.plot(ax=ax) - spectro_data_timezone = called_ax.pop() + spectro_data_axes = called_ax.pop() if sd.begin.tz: - assert isinstance(spectro_data_timezone.xaxis.units, datetime.tzinfo) + assert isinstance(spectro_data_axes.xaxis.units, datetime.tzinfo) else: - assert spectro_data_timezone.xaxis.units is None + assert spectro_data_axes.xaxis.units is None From e1a7296870f3f00e4361778301a90d5ddf8574f4 Mon Sep 17 00:00:00 2001 From: mathieudpnt <108517594+mathieudpnt@users.noreply.github.com> Date: Tue, 7 Jul 2026 14:01:18 +0200 Subject: [PATCH 3/4] %z fix spectro_data test --- src/osekit/core/audio_file.py | 3 +++ tests/test_spectro.py | 7 +++---- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/src/osekit/core/audio_file.py b/src/osekit/core/audio_file.py index 0c133f93..c7756875 100644 --- a/src/osekit/core/audio_file.py +++ b/src/osekit/core/audio_file.py @@ -69,6 +69,9 @@ def __init__( self.channels = channels self.end = self.begin + Timedelta(seconds=duration) + if not self.duration: + raise ValueError("t'as chié dans la colle frère") + def read(self, start: Timestamp, stop: Timestamp) -> np.ndarray: """Return the audio data between start and stop from the file. diff --git a/tests/test_spectro.py b/tests/test_spectro.py index 7c107180..257ef8f0 100644 --- a/tests/test_spectro.py +++ b/tests/test_spectro.py @@ -1444,13 +1444,12 @@ def mock_imshow( @pytest.mark.parametrize( "audio_files", [ - {"date_begin": Timestamp("2026-01-01 00:00:00", tz="UTC")}, - {"date_begin": Timestamp("2026-01-01 00:00:00", tz="Etc/GMT+2")}, - {"date_begin": Timestamp("2025-12-31 23:59:59", tz="Asia/Tokyo")}, + {"date_begin": Timestamp("2026-01-01 00:00:00", tz="Europe/Paris")}, + {"date_begin": Timestamp("2025-12-31 23:59:59", tz="+0300")}, {"date_begin": Timestamp("2025-12-31 23:59:59")}, ], indirect=True, - ids=["utc", "%z", "%Z", "naive"], + ids=["%Z", "%z", "naive"], ) def test_plot_timezone( audio_files: tuple[list[AudioFile], ...], From 7c9e95e80ab38eb76808fab88676cdb67d1caff8 Mon Sep 17 00:00:00 2001 From: mathieudpnt <108517594+mathieudpnt@users.noreply.github.com> Date: Tue, 7 Jul 2026 14:27:59 +0200 Subject: [PATCH 4/4] oupsi --- src/osekit/core/audio_file.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/osekit/core/audio_file.py b/src/osekit/core/audio_file.py index c7756875..0c133f93 100644 --- a/src/osekit/core/audio_file.py +++ b/src/osekit/core/audio_file.py @@ -69,9 +69,6 @@ def __init__( self.channels = channels self.end = self.begin + Timedelta(seconds=duration) - if not self.duration: - raise ValueError("t'as chié dans la colle frère") - def read(self, start: Timestamp, stop: Timestamp) -> np.ndarray: """Return the audio data between start and stop from the file.