# HG changeset patch # User Amine Sehili # Date 1568230893 -3600 # Node ID 58eb13b551633b17bff739d1db1be434421dba5f # Parent 17865e8aecc9bc65f13c7f1f870785c29c1dcf98 Make split_and_plot return list of regions diff -r 17865e8aecc9 -r 58eb13b55163 auditok/core.py --- a/auditok/core.py Sun Sep 08 20:42:42 2019 +0100 +++ b/auditok/core.py Wed Sep 11 20:41:33 2019 +0100 @@ -13,7 +13,14 @@ from auditok.util import AudioDataSource, DataValidator, AudioEnergyValidator from auditok.io import check_audio_data, to_file, player_for, get_audio_source from auditok.exceptions import TooSamllBlockDuration -from auditok.plotting import plot, plot_detections + +try: + from auditok.plotting import plot, plot_detections + + _WITH_MATPLOTLIB = True +except ImportError: + _WITH_MATPLOTLIB = False + try: from . import signal_numpy as signal @@ -376,6 +383,7 @@ self._sample_width = sample_width self._channels = channels self._samples = None + self.splitp = self.split_and_plot if meta is not None: self._meta = _AudioRegionMetadata(meta) @@ -562,7 +570,10 @@ ) def plot(self, show=True, **plot_kwargs): - plot(self, self.sr, show=show, **plot_kwargs) + if _WITH_MATPLOTLIB: + plot(self, self.sr, show=show, **plot_kwargs) + else: + raise RuntimeWarning("Plotting requires matplotlib") def split_and_plot( self, @@ -575,22 +586,29 @@ plot_kwargs=None, **kwargs ): - """Split region and plot signal and detection. + """Split region and plot signal and detection. Alias: `splitp`. See :auditok.split() for split parameters description. """ - regions = split( - self, - min_dur=min_dur, - max_dur=max_dur, - max_silence=max_silence, - drop_trailing_silence=drop_trailing_silence, - strict_min_dur=strict_min_dur, - **kwargs - ) - detections = ((reg.meta.start, reg.meta.end) for reg in regions) - if plot_kwargs is None: - plot_kwargs = {} - plot_detections(self, self.sr, detections, show=show, **plot_kwargs) + if _WITH_MATPLOTLIB: + regions = split( + self, + min_dur=min_dur, + max_dur=max_dur, + max_silence=max_silence, + drop_trailing_silence=drop_trailing_silence, + strict_min_dur=strict_min_dur, + **kwargs + ) + regions = list(regions) + detections = ((reg.meta.start, reg.meta.end) for reg in regions) + if plot_kwargs is None: + plot_kwargs = {} + plot_detections( + self, self.sr, detections, show=show, **plot_kwargs + ) + return regions + else: + raise RuntimeWarning("Plotting requires matplotlib") def __array__(self): return self.samples