From: Thomas Fillon Date: Tue, 1 Oct 2013 12:08:40 +0000 (+0200) Subject: Add/Modify doctest X-Git-Tag: 0.5.0~56 X-Git-Url: https://git.parisson.com/?a=commitdiff_plain;h=e137fb0c3447fe3ae2dc0a7c89600ef718f0ea7d;p=timeside.git Add/Modify doctest --- diff --git a/timeside/analyzer/core.py b/timeside/analyzer/core.py index a694f19..6b2665d 100644 --- a/timeside/analyzer/core.py +++ b/timeside/analyzer/core.py @@ -406,7 +406,7 @@ class newAnalyzerResult(MetadataObject): """ Object that contains the metadata and parameters of an analyzer process - Attributes + Parameters ---------- dataMode : str dataMode describes the type of data : @@ -418,12 +418,19 @@ class newAnalyzerResult(MetadataObject): - 'global' - 'segment' - 'event' - data : AnalyzerData - idMetadata : IdMetadata - audioMetadata : AudioMetadata - frameMetadata : FrameMetadata - labelMetadata : LabelMetadata - parameters : AnalyzerParameters Object + + + Returns + ------- + A new MetadataObject with the following attributes : + - dataMode + - timeMode + - data : :class:`AnalyzerData` + - idMetadata : :class:`IdMetadata` + - audioMetadata : :class:`AudioMetadata` + - frameMetadata : :class:`FrameMetadata` + - labelMetadata : :class:`LabelMetadata` + - parameters : :class:`AnalyzerParameters` Object """ @@ -655,17 +662,18 @@ class AnalyzerResult(object): class AnalyzerResultContainer(object): ''' >>> from timeside.decoder import FileDecoder - >>> #from timeside.analyzer.core import Analyzer - >>> #from timeside.analyzer import AnalyzerResultContainer, newAnalyzerResult - >>> wavFile = 'tests/samples/sweep.wav' + >>> import timeside.analyzer.core as coreA + >>> import os + >>> ModulePath = os.path.dirname(os.path.realpath(coreA.__file__)) + >>> wavFile = os.path.join(ModulePath , '../../tests/samples/sweep.wav') >>> d = FileDecoder(wavFile, start=1) - >>> a = Analyzer() + >>> a = coreA.Analyzer() >>> (d|a).run() #doctest: +ELLIPSIS >>> a.new_result() #doctest: +ELLIPSIS - newAnalyzerResult(dataMode=None, timeMode=None, idMetadata=IdMetadata(id='', name='', unit='', description='', date='...', version='0.4.4', author='TimeSide'), data=AnalyzerData(data=None, time=None, duration=None, dataType=None), audioMetadata=AudioMetadata(uri='file:///home/thomas/code/timeside/TimeSide/tests/samples/sweep.wav', start=1.0, duration=7.0, channels=None, channelsManagement=''), frameMetadata=FrameMetadata(samplerate=None, blocksize=None, stepsize=None), labelMetadata=LabelMetadata(label=None, description=None, labelType='mono'), parameters={}) - >>> resContainer = AnalyzerResultContainer() + newAnalyzerResult(dataMode=None, timeMode=None, idMetadata=IdMetadata(id='', name='', unit='', description='', date='...', version='...', author='TimeSide'), data=AnalyzerData(value=None, label=array([], dtype=int64), time=array([], dtype=float64), duration=array([], dtype=float64)), audioMetadata=AudioMetadata(uri='file:///.../tests/samples/sweep.wav', start=1.0, duration=7.0, channels=None, channelsManagement=''), frameMetadata=FrameMetadata(samplerate=None, blocksize=None, stepsize=None), labelMetadata=LabelMetadata(label=None, description=None, labelType='mono'), parameters={}) + >>> resContainer = coreA.AnalyzerResultContainer() ''' def __init__(self, analyzer_results=None): diff --git a/timeside/core.py b/timeside/core.py index 2b19beb..8cb548b 100644 --- a/timeside/core.py +++ b/timeside/core.py @@ -43,8 +43,15 @@ class MetaProcessor(MetaComponent): if new_class in implementations(IProcessor): id = str(new_class.id()) if _processors.has_key(id): - raise ApiError("%s and %s have the same id: '%s'" - % (new_class.__name__, _processors[id].__name__, id)) + # Doctest test can duplicate a processor + # This can be identify by the conditon "module == '__main__'" + if new_class.__module__ == '__main__': + new_class = _processors[id] + elif _processors[id].__module__ == '__main__': + pass + else: + raise ApiError("%s and %s have the same id: '%s'" + % (new_class.__name__, _processors[id].__name__, id)) if not MetaProcessor.valid_id.match(id): raise ApiError("%s has a malformed id: '%s'" % (new_class.__name__, id)) diff --git a/timeside/grapher/core.py b/timeside/grapher/core.py index fd794e8..1ed8887 100644 --- a/timeside/grapher/core.py +++ b/timeside/grapher/core.py @@ -619,37 +619,56 @@ def downsample(vector, factor): def smooth(x, window_len=10, window='hanning'): - """smooth the data using a window with requested size. + """ + Smooth the data using a window with requested size. This method is based on the convolution of a scaled window with the signal. The signal is prepared by introducing reflected copies of the signal (with the window size) in both ends so that transient parts are minimized in the begining and end part of the output signal. - input: - x: the input signal - window_len: the dimension of the smoothing window - window: the type of window from 'flat', 'hanning', 'hamming', 'bartlett', 'blackman' - flat window will produce a moving average smoothing. - - output: - the smoothed signal + Parameters + ---------- + x : numpy.array + the input signal + window_len : int + the dimension of the smoothing window + window : str + the type of window from 'flat', 'hanning', 'hamming', 'bartlett', 'blackman' + flat window will produce a moving average smoothing. - example: + Returns + ------- + The smoothed signal - import numpy as np - t = numpy.linspace(-2,2,0.1) - x = numpy.sin(t)+numpy.random.randn(len(t))*0.1 - y = smooth(x) - - see also: + See Also + -------- numpy.hanning, numpy.hamming, numpy.bartlett, numpy.blackman, numpy.convolve scipy.signal.lfilter - TODO: the window parameter could be the window itself if an array instead of a string + + Examples + -------- + + >>> import numpy as np + >>> from timeside.grapher import smooth + >>> t = np.arange(-2,2,0.1) + >>> x = np.sin(t)+np.random.randn(len(t))*0.1 + >>> y = smooth(x) + >>> import matplotlib.pyplot as plt + >>> plt.plot(x) # doctest: +ELLIPSIS + [] + >>> plt.plot(y) # doctest: +ELLIPSIS + [] + >>> plt.legend(['Source signal', 'Smoothed signal']) # doctest: +ELLIPSIS + + >>> #plt.show() """ + # TODO: the window parameter could be the window itself if an array instead of a string + + if x.ndim != 1: raise ValueError, "smooth only accepts 1 dimension arrays." @@ -698,3 +717,8 @@ def im_watermark(im, inputtext, font=None, color=None, opacity=.6, margin=(30,30 if opacity != 1: textlayer = reduce_opacity(textlayer,opacity) return Image.composite(textlayer, im, textlayer) + + +if __name__ == "__main__": + import doctest + doctest.testmod() \ No newline at end of file