def sinc(self, filter_type='<STR_LIT>', cutoff_freq=<NUM_LIT>,<EOL>stop_band_attenuation=<NUM_LIT>, transition_bw=None,<EOL>phase_response=None):

filter_types = ['<STR_LIT>', '<STR_LIT>', '<STR_LIT>', '<STR_LIT>']<EOL>if filter_type not in filter_types:<EOL><INDENT>raise ValueError(<EOL>"<STR_LIT>".format('<STR_LIT:U+002CU+0020>'.join(filter_types))<EOL>)<EOL><DEDENT>if not (is_number(cutoff_freq) or isinstance(cutoff_freq, list)):<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT>if filter_type in ['<STR_LIT>', '<STR_LIT>'] and isinstance(cutoff_freq, list):<EOL><INDENT>raise ValueError(<EOL>"<STR_LIT>"<EOL>"<STR_LIT>"<EOL>)<EOL><DEDENT>if filter_type in ['<STR_LIT>', '<STR_LIT>'] and is_number(cutoff_freq):<EOL><INDENT>raise ValueError(<EOL>"<STR_LIT>"<EOL>"<STR_LIT>"<EOL>)<EOL><DEDENT>if is_number(cutoff_freq) and cutoff_freq <= <NUM_LIT:0>:<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT>if isinstance(cutoff_freq, list):<EOL><INDENT>if len(cutoff_freq) != <NUM_LIT:2>:<EOL><INDENT>raise ValueError(<EOL>"<STR_LIT>"<EOL>)<EOL><DEDENT>if any([not is_number(f) or f <= <NUM_LIT:0> for f in cutoff_freq]):<EOL><INDENT>raise ValueError(<EOL>"<STR_LIT>"<EOL>)<EOL><DEDENT>cutoff_freq = sorted(cutoff_freq)<EOL><DEDENT>if not is_number(stop_band_attenuation) or stop_band_attenuation < <NUM_LIT:0>:<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT>if not (is_number(transition_bw) or<EOL>isinstance(transition_bw, list) or transition_bw is None):<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT>if filter_type in ['<STR_LIT>', '<STR_LIT>'] and isinstance(transition_bw, list):<EOL><INDENT>raise ValueError(<EOL>"<STR_LIT>"<EOL>"<STR_LIT>"<EOL>)<EOL><DEDENT>if is_number(transition_bw) and transition_bw <= <NUM_LIT:0>:<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT>if isinstance(transition_bw, list):<EOL><INDENT>if any([not is_number(f) or f <= <NUM_LIT:0> for f in transition_bw]):<EOL><INDENT>raise ValueError(<EOL>"<STR_LIT>"<EOL>)<EOL><DEDENT>if len(transition_bw) != <NUM_LIT:2>:<EOL><INDENT>raise ValueError(<EOL>"<STR_LIT>"<EOL>)<EOL><DEDENT><DEDENT>if phase_response is not None and not is_number(phase_response):<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT>if (is_number(phase_response) and<EOL>(phase_response < <NUM_LIT:0> or phase_response > <NUM_LIT:100>)):<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT>effect_args = ['<STR_LIT>']<EOL>effect_args.extend(['<STR_LIT>', '<STR_LIT>'.format(stop_band_attenuation)])<EOL>if phase_response is not None:<EOL><INDENT>effect_args.extend(['<STR_LIT>', '<STR_LIT>'.format(phase_response)])<EOL><DEDENT>if filter_type == '<STR_LIT>':<EOL><INDENT>if transition_bw is not None:<EOL><INDENT>effect_args.extend(['<STR_LIT>', '<STR_LIT>'.format(transition_bw)])<EOL><DEDENT>effect_args.append('<STR_LIT>'.format(cutoff_freq))<EOL><DEDENT>elif filter_type == '<STR_LIT>':<EOL><INDENT>effect_args.append('<STR_LIT>'.format(cutoff_freq))<EOL>if transition_bw is not None:<EOL><INDENT>effect_args.extend(['<STR_LIT>', '<STR_LIT>'.format(transition_bw)])<EOL><DEDENT><DEDENT>else:<EOL><INDENT>if is_number(transition_bw):<EOL><INDENT>effect_args.extend(['<STR_LIT>', '<STR_LIT>'.format(transition_bw)])<EOL><DEDENT>elif isinstance(transition_bw, list):<EOL><INDENT>effect_args.extend(['<STR_LIT>', '<STR_LIT>'.format(transition_bw[<NUM_LIT:0>])])<EOL><DEDENT><DEDENT>if filter_type == '<STR_LIT>':<EOL><INDENT>effect_args.append(<EOL>'<STR_LIT>'.format(cutoff_freq[<NUM_LIT:0>], cutoff_freq[<NUM_LIT:1>])<EOL>)<EOL><DEDENT>elif filter_type == '<STR_LIT>':<EOL><INDENT>effect_args.append(<EOL>'<STR_LIT>'.format(cutoff_freq[<NUM_LIT:1>], cutoff_freq[<NUM_LIT:0>])<EOL>)<EOL><DEDENT>if isinstance(transition_bw, list):<EOL><INDENT>effect_args.extend(['<STR_LIT>', '<STR_LIT>'.format(transition_bw[<NUM_LIT:1>])])<EOL><DEDENT>self.effects.extend(effect_args)<EOL>self.effects_log.append('<STR_LIT>')<EOL>return self<EOL>

Apply a sinc kaiser-windowed low-pass, high-pass, band-pass, or
        band-reject filter to the signal.

        Parameters
        ----------
        filter_type : str, default='high'
            Type of filter. One of:
                - 'high' for a high-pass filter
                - 'low' for a low-pass filter
                - 'pass' for a band-pass filter
                - 'reject' for a band-reject filter
        cutoff_freq : float or list, default=3000
            A scalar or length 2 list indicating the filter's critical
            frequencies. The critical frequencies are given in Hz and must be
            positive. For a high-pass or low-pass filter, cutoff_freq
            must be a scalar. For a band-pass or band-reject filter, it must be
            a length 2 list.
        stop_band_attenuation : float, default=120
            The stop band attenuation in dB
        transition_bw : float, list or None, default=None
            The transition band-width in Hz.
            If None, sox's default of 5% of the total bandwith is used.
            If a float, the given transition bandwith is used for both the
            upper and lower bands (if applicable).
            If a list, the first argument is used for the lower band and the
            second for the upper band.
        phase_response : float or None
            The filter's phase response between 0 (minimum) and 100 (maximum).
            If None, sox's default phase repsonse is used.

        See Also
        --------
        band, bandpass, bandreject, highpass, lowpass


if not is_number(factor) or factor <= <NUM_LIT:0>:<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT>if factor < <NUM_LIT:0.5> or factor > <NUM_LIT:2>:<EOL><INDENT>logger.warning(<EOL>"<STR_LIT>"<EOL>)<EOL><DEDENT>effect_args = ['<STR_LIT>', '<STR_LIT>'.format(factor)]<EOL>self.effects.extend(effect_args)<EOL>self.effects_log.append('<STR_LIT>')<EOL>return self<EOL>

Adjust the audio speed (pitch and tempo together).

        Technically, the speed effect only changes the sample rate information,
        leaving the samples themselves untouched. The rate effect is invoked
        automatically to resample to the output sample rate, using its default
        quality/speed. For higher quality or higher speed resampling, in
        addition to the speed effect, specify the rate effect with the desired
        quality option.

        Parameters
        ----------
        factor : float
            The ratio of the new speed to the old speed.
            For ex. 1.1 speeds up the audio by 10%; 0.9 slows it down by 10%.
            Note - this argument is the inverse of what is passed to the sox
            stretch effect for consistency with speed.

        See Also
        --------
        rate, tempo, pitch


def stat(self, input_filepath, scale=None, rms=False):

effect_args = ['<STR_LIT>', '<STR_LIT:1>', '<STR_LIT>']<EOL>if scale is not None:<EOL><INDENT>if not is_number(scale) or scale <= <NUM_LIT:0>:<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT>effect_args.extend(['<STR_LIT>', '<STR_LIT>'.format(scale)])<EOL><DEDENT>if rms:<EOL><INDENT>effect_args.append('<STR_LIT>')<EOL><DEDENT>_, _, stat_output = self.build(<EOL>input_filepath, None, extra_args=effect_args, return_output=True<EOL>)<EOL>stat_dict = {}<EOL>lines = stat_output.split('<STR_LIT:\n>')<EOL>for line in lines:<EOL><INDENT>split_line = line.split()<EOL>if len(split_line) == <NUM_LIT:0>:<EOL><INDENT>continue<EOL><DEDENT>value = split_line[-<NUM_LIT:1>]<EOL>key = '<STR_LIT:U+0020>'.join(split_line[:-<NUM_LIT:1>])<EOL>stat_dict[key.strip('<STR_LIT::>')] = value<EOL><DEDENT>return stat_dict<EOL>

Display time and frequency domain statistical information about the
        audio. Audio is passed unmodified through the SoX processing chain.

        Unlike other Transformer methods, this does not modify the transformer
        effects chain. Instead it computes statistics on the output file that
        would be created if the build command were invoked.

        Note: The file is downmixed to mono prior to computation.

        Parameters
        ----------
        input_filepath : str
            Path to input file to compute stats on.
        scale : float or None, default=None
            If not None, scales the input by the given scale factor.
        rms : bool, default=False
            If True, scales all values by the average rms amplitude.

        Returns
        -------
        stat_dict : dict
            Dictionary of statistics.

        See Also
        --------
        stats, power_spectrum, sox.file_info


effect_args = ['<STR_LIT>', '<STR_LIT:1>', '<STR_LIT>', '<STR_LIT>']<EOL>_, _, stat_output = self.build(<EOL>input_filepath, None, extra_args=effect_args, return_output=True<EOL>)<EOL>power_spectrum = []<EOL>lines = stat_output.split('<STR_LIT:\n>')<EOL>for line in lines:<EOL><INDENT>split_line = line.split()<EOL>if len(split_line) != <NUM_LIT:2>:<EOL><INDENT>continue<EOL><DEDENT>freq, amp = split_line<EOL>power_spectrum.append([float(freq), float(amp)])<EOL><DEDENT>return power_spectrum<EOL>

Calculates the power spectrum (4096 point DFT). This method
        internally invokes the stat command with the -freq option.

        Note: The file is downmixed to mono prior to computation.

        Parameters
        ----------
        input_filepath : str
            Path to input file to compute stats on.

        Returns
        -------
        power_spectrum : list
            List of frequency (Hz), amplitude pairs.

        See Also
        --------
        stat, stats, sox.file_info


effect_args = ['<STR_LIT>', '<STR_LIT:1>', '<STR_LIT>']<EOL>_, _, stats_output = self.build(<EOL>input_filepath, None, extra_args=effect_args, return_output=True<EOL>)<EOL>stats_dict = {}<EOL>lines = stats_output.split('<STR_LIT:\n>')<EOL>for line in lines:<EOL><INDENT>split_line = line.split()<EOL>if len(split_line) == <NUM_LIT:0>:<EOL><INDENT>continue<EOL><DEDENT>value = split_line[-<NUM_LIT:1>]<EOL>key = '<STR_LIT:U+0020>'.join(split_line[:-<NUM_LIT:1>])<EOL>stats_dict[key] = value<EOL><DEDENT>return stats_dict<EOL>

Display time domain statistical information about the audio
        channels. Audio is passed unmodified through the SoX processing chain.
        Statistics are calculated and displayed for each audio channel

        Unlike other Transformer methods, this does not modify the transformer
        effects chain. Instead it computes statistics on the output file that
        would be created if the build command were invoked.

        Note: The file is downmixed to mono prior to computation.

        Parameters
        ----------
        input_filepath : str
            Path to input file to compute stats on.

        Returns
        -------
        stats_dict : dict
            List of frequency (Hz), amplitude pairs.

        See Also
        --------
        stat, sox.file_info


def stretch(self, factor, window=<NUM_LIT:20>):

if not is_number(factor) or factor <= <NUM_LIT:0>:<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT>if factor < <NUM_LIT:0.5> or factor > <NUM_LIT:2>:<EOL><INDENT>logger.warning(<EOL>"<STR_LIT>"<EOL>"<STR_LIT>"<EOL>)<EOL><DEDENT>if abs(factor - <NUM_LIT:1.0>) > <NUM_LIT:0.1>:<EOL><INDENT>logger.warning(<EOL>"<STR_LIT>"<EOL>"<STR_LIT>"<EOL>)<EOL><DEDENT>if not is_number(window) or window <= <NUM_LIT:0>:<EOL><INDENT>raise ValueError(<EOL>"<STR_LIT>"<EOL>)<EOL><DEDENT>effect_args = ['<STR_LIT>', '<STR_LIT>'.format(factor), '<STR_LIT>'.format(window)]<EOL>self.effects.extend(effect_args)<EOL>self.effects_log.append('<STR_LIT>')<EOL>return self<EOL>

Change the audio duration (but not its pitch).
        **Unless factor is close to 1, use the tempo effect instead.**

        This effect is broadly equivalent to the tempo effect with search set
        to zero, so in general, its results are comparatively poor; it is
        retained as it can sometimes out-perform tempo for small factors.

        Parameters
        ----------
        factor : float
            The ratio of the new tempo to the old tempo.
            For ex. 1.1 speeds up the tempo by 10%; 0.9 slows it down by 10%.
            Note - this argument is the inverse of what is passed to the sox
            stretch effect for consistency with tempo.
        window : float, default=20
            Window size in miliseconds

        See Also
        --------
        tempo, speed, pitch


effect_args = ['<STR_LIT>']<EOL>self.effects.extend(effect_args)<EOL>self.effects_log.append('<STR_LIT>')<EOL>return self<EOL>

Swap stereo channels. If the input is not stereo, pairs of channels
        are swapped, and a possible odd last channel passed through.

        E.g., for seven channels, the output order will be 2, 1, 4, 3, 6, 5, 7.

        See Also
        ----------
        remix


def tempo(self, factor, audio_type=None, quick=False):

if not is_number(factor) or factor <= <NUM_LIT:0>:<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT>if factor < <NUM_LIT:0.5> or factor > <NUM_LIT:2>:<EOL><INDENT>logger.warning(<EOL>"<STR_LIT>"<EOL>"<STR_LIT>"<EOL>)<EOL><DEDENT>if abs(factor - <NUM_LIT:1.0>) <= <NUM_LIT:0.1>:<EOL><INDENT>logger.warning(<EOL>"<STR_LIT>"<EOL>"<STR_LIT>"<EOL>)<EOL><DEDENT>if audio_type not in [None, '<STR_LIT:m>', '<STR_LIT:s>', '<STR_LIT:l>']:<EOL><INDENT>raise ValueError(<EOL>"<STR_LIT>"<EOL>)<EOL><DEDENT>if not isinstance(quick, bool):<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT>effect_args = ['<STR_LIT>']<EOL>if quick:<EOL><INDENT>effect_args.append('<STR_LIT>')<EOL><DEDENT>if audio_type is not None:<EOL><INDENT>effect_args.append('<STR_LIT>'.format(audio_type))<EOL><DEDENT>effect_args.append('<STR_LIT>'.format(factor))<EOL>self.effects.extend(effect_args)<EOL>self.effects_log.append('<STR_LIT>')<EOL>return self<EOL>

Time stretch audio without changing pitch.

        This effect uses the WSOLA algorithm. The audio is chopped up into
        segments which are then shifted in the time domain and overlapped
        (cross-faded) at points where their waveforms are most similar as
        determined by measurement of least squares.

        Parameters
        ----------
        factor : float
            The ratio of new tempo to the old tempo.
            For ex. 1.1 speeds up the tempo by 10%; 0.9 slows it down by 10%.
        audio_type : str
            Type of audio, which optimizes algorithm parameters. One of:
             * m : Music,
             * s : Speech,
             * l : Linear (useful when factor is close to 1),
        quick : bool, default=False
            If True, this effect will run faster but with lower sound quality.

        See Also
        --------
        stretch, speed, pitch


def treble(self, gain_db, frequency=<NUM_LIT>, slope=<NUM_LIT:0.5>):

if not is_number(gain_db):<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT>if not is_number(frequency) or frequency <= <NUM_LIT:0>:<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT>if not is_number(slope) or slope <= <NUM_LIT:0> or slope > <NUM_LIT:1.0>:<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT>effect_args = [<EOL>'<STR_LIT>', '<STR_LIT>'.format(gain_db), '<STR_LIT>'.format(frequency),<EOL>'<STR_LIT>'.format(slope)<EOL>]<EOL>self.effects.extend(effect_args)<EOL>self.effects_log.append('<STR_LIT>')<EOL>return self<EOL>

Boost or cut the treble (lower) frequencies of the audio using a
        two-pole shelving filter with a response similar to that of a standard
        hi-fi’s tone-controls. This is also known as shelving equalisation.

        The filters are described in detail in
        http://musicdsp.org/files/Audio-EQ-Cookbook.txt

        Parameters
        ----------
        gain_db : float
            The gain at the Nyquist frequency.
            For a large cut use -20, for a large boost use 20.
        frequency : float, default=100.0
            The filter's cutoff frequency in Hz.
        slope : float, default=0.5
            The steepness of the filter's shelf transition.
            For a gentle slope use 0.3, and use 1.0 for a steep slope.

        See Also
        --------
        bass, equalizer


def tremolo(self, speed=<NUM_LIT>, depth=<NUM_LIT>):

if not is_number(speed) or speed <= <NUM_LIT:0>:<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT>if not is_number(depth) or depth <= <NUM_LIT:0> or depth > <NUM_LIT:100>:<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT>effect_args = [<EOL>'<STR_LIT>',<EOL>'<STR_LIT>'.format(speed),<EOL>'<STR_LIT>'.format(depth)<EOL>]<EOL>self.effects.extend(effect_args)<EOL>self.effects_log.append('<STR_LIT>')<EOL>return self<EOL>

Apply a tremolo (low frequency amplitude modulation) effect to the
        audio. The tremolo frequency in Hz is giv en by speed, and the depth
        as a percentage by depth (default 40).

        Parameters
        ----------
        speed : float
            Tremolo speed in Hz.
        depth : float
            Tremolo depth as a percentage of the total amplitude.

        See Also
        --------
        flanger

        Examples
        --------
        >>> tfm = sox.Transformer()

        For a growl-type effect

        >>> tfm.tremolo(speed=100.0)


def trim(self, start_time, end_time=None):

if not is_number(start_time) or start_time < <NUM_LIT:0>:<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT>effect_args = [<EOL>'<STR_LIT>',<EOL>'<STR_LIT>'.format(start_time)<EOL>]<EOL>if end_time is not None:<EOL><INDENT>if not is_number(end_time) or end_time < <NUM_LIT:0>:<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT>if start_time >= end_time:<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT>effect_args.append('<STR_LIT>'.format(end_time - start_time))<EOL><DEDENT>self.effects.extend(effect_args)<EOL>self.effects_log.append('<STR_LIT>')<EOL>return self<EOL>

Excerpt a clip from an audio file, given the start timestamp and end timestamp of the clip within the file, expressed in seconds. If the end timestamp is set to `None` or left unspecified, it defaults to the duration of the audio file.

        Parameters
        ----------
        start_time : float
            Start time of the clip (seconds)
        end_time : float or None, default=None
            End time of the clip (seconds)


if not isinstance(factor, int) or factor < <NUM_LIT:1>:<EOL><INDENT>raise ValueError('<STR_LIT>')<EOL><DEDENT>effect_args = ['<STR_LIT>', '<STR_LIT:{}>'.format(factor)]<EOL>self.effects.extend(effect_args)<EOL>self.effects_log.append('<STR_LIT>')<EOL>return self<EOL>

Upsample the signal by an integer factor: zero-value samples are
        inserted between each pair of input samples. As a result, the original
        spectrum is replicated into the new frequency space (imaging) and
        attenuated. The upsample effect is typically used in combination with
        filtering effects.

        Parameters
        ----------
        factor : int, default=2
            Integer upsampling factor.

        See Also
        --------
        rate, downsample


def vad(self, location=<NUM_LIT:1>, normalize=True, activity_threshold=<NUM_LIT>,<EOL>min_activity_duration=<NUM_LIT>, initial_search_buffer=<NUM_LIT:1.0>,<EOL>max_gap=<NUM_LIT>, initial_pad=<NUM_LIT:0.0>):

if location not in [-<NUM_LIT:1>, <NUM_LIT:1>]:<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT>if not isinstance(normalize, bool):<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT>if not is_number(activity_threshold):<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT>if not is_number(min_activity_duration) or min_activity_duration < <NUM_LIT:0>:<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT>if not is_number(initial_search_buffer) or initial_search_buffer < <NUM_LIT:0>:<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT>if not is_number(max_gap) or max_gap < <NUM_LIT:0>:<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT>if not is_number(initial_pad) or initial_pad < <NUM_LIT:0>:<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT>effect_args = []<EOL>if normalize:<EOL><INDENT>effect_args.append('<STR_LIT>')<EOL><DEDENT>if location == -<NUM_LIT:1>:<EOL><INDENT>effect_args.append('<STR_LIT>')<EOL><DEDENT>effect_args.extend([<EOL>'<STR_LIT>',<EOL>'<STR_LIT>', '<STR_LIT>'.format(activity_threshold),<EOL>'<STR_LIT>', '<STR_LIT>'.format(min_activity_duration),<EOL>'<STR_LIT>', '<STR_LIT>'.format(initial_search_buffer),<EOL>'<STR_LIT>', '<STR_LIT>'.format(max_gap),<EOL>'<STR_LIT>', '<STR_LIT>'.format(initial_pad)<EOL>])<EOL>if location == -<NUM_LIT:1>:<EOL><INDENT>effect_args.append('<STR_LIT>')<EOL><DEDENT>self.effects.extend(effect_args)<EOL>self.effects_log.append('<STR_LIT>')<EOL>return self<EOL>

Voice Activity Detector. Attempts to trim silence and quiet
        background sounds from the ends of recordings of speech. The algorithm
        currently uses a simple cepstral power measurement to detect voice, so
        may be fooled by other things, especially music.

        The effect can trim only from the front of the audio, so in order to
        trim from the back, the reverse effect must also be used.

        Parameters
        ----------
        location : 1 or -1, default=1
            If 1, trims silence from the beginning
            If -1, trims silence from the end
        normalize : bool, default=True
            If true, normalizes audio before processing.
        activity_threshold : float, default=7.0
            The measurement level used to trigger activity detection. This may
            need to be cahnged depending on the noise level, signal level, and
            other characteristics of the input audio.
        min_activity_duration : float, default=0.25
            The time constant (in seconds) used to help ignore short bursts of
            sound.
        initial_search_buffer : float, default=1.0
            The amount of audio (in seconds) to search for quieter/shorter
            bursts of audio to include prior to the detected trigger point.
        max_gap : float, default=0.25
            The allowed gap (in seconds) between quiteter/shorter bursts of
            audio to include prior to the detected trigger point
        initial_pad : float, default=0.0
            The amount of audio (in seconds) to preserve before the trigger
            point and any found quieter/shorter bursts.

        See Also
        --------
        silence

        Examples
        --------
        >>> tfm = sox.Transformer()

        Remove silence from the beginning of speech

        >>> tfm.vad(initial_pad=0.3)

        Remove silence from the end of speech

        >>> tfm.vad(location=-1, initial_pad=0.2)


def vol(self, gain, gain_type='<STR_LIT>', limiter_gain=None):

if not is_number(gain):<EOL><INDENT>raise ValueError('<STR_LIT>')<EOL><DEDENT>if limiter_gain is not None:<EOL><INDENT>if (not is_number(limiter_gain) or<EOL>limiter_gain <= <NUM_LIT:0> or limiter_gain >= <NUM_LIT:1>):<EOL><INDENT>raise ValueError(<EOL>'<STR_LIT>'<EOL>)<EOL><DEDENT><DEDENT>if gain_type in ['<STR_LIT>', '<STR_LIT>'] and gain < <NUM_LIT:0>:<EOL><INDENT>raise ValueError(<EOL>"<STR_LIT>"<EOL>)<EOL><DEDENT>effect_args = ['<STR_LIT>']<EOL>effect_args.append('<STR_LIT>'.format(gain))<EOL>if gain_type == '<STR_LIT>':<EOL><INDENT>effect_args.append('<STR_LIT>')<EOL><DEDENT>elif gain_type == '<STR_LIT>':<EOL><INDENT>effect_args.append('<STR_LIT>')<EOL><DEDENT>elif gain_type == '<STR_LIT>':<EOL><INDENT>effect_args.append('<STR_LIT>')<EOL><DEDENT>else:<EOL><INDENT>raise ValueError('<STR_LIT>')<EOL><DEDENT>if limiter_gain is not None:<EOL><INDENT>if gain_type in ['<STR_LIT>', '<STR_LIT>'] and gain > <NUM_LIT:1>:<EOL><INDENT>effect_args.append('<STR_LIT>'.format(limiter_gain))<EOL><DEDENT>elif gain_type == '<STR_LIT>' and gain > <NUM_LIT:0>:<EOL><INDENT>effect_args.append('<STR_LIT>'.format(limiter_gain))<EOL><DEDENT><DEDENT>self.effects.extend(effect_args)<EOL>self.effects_log.append('<STR_LIT>')<EOL>return self<EOL>

Apply an amplification or an attenuation to the audio signal.

        Parameters
        ----------
        gain : float
            Interpreted according to the given `gain_type`.
            If `gain_type' = 'amplitude', `gain' is a positive amplitude ratio.
            If `gain_type' = 'power', `gain' is a power (voltage squared).
            If `gain_type' = 'db', `gain' is in decibels.
        gain_type : string, default='amplitude'
            Type of gain. One of:
                - 'amplitude'
                - 'power'
                - 'db'
        limiter_gain : float or None, default=None
            If specified, a limiter is invoked on peaks greater than
            `limiter_gain' to prevent clipping.
            `limiter_gain` should be a positive value much less than 1.

        See Also
        --------
        gain, compand


validate_input_file(input_filepath)<EOL>output = soxi(input_filepath, '<STR_LIT:b>')<EOL>if output == '<STR_LIT:0>':<EOL><INDENT>logger.warning("<STR_LIT>", input_filepath)<EOL><DEDENT>return int(output)<EOL>

Number of bits per sample (0 if not applicable).

Parameters
----------
input_filepath : str
    Path to audio file.

Returns
-------
bitrate : int
    number of bits per sample
    returns 0 if not applicable


validate_input_file(input_filepath)<EOL>output = soxi(input_filepath, '<STR_LIT:c>')<EOL>return int(output)<EOL>

Show number of channels.

Parameters
----------
input_filepath : str
    Path to audio file.

Returns
-------
channels : int
    number of channels


validate_input_file(input_filepath)<EOL>output = soxi(input_filepath, '<STR_LIT:a>')<EOL>return str(output)<EOL>

Show file comments (annotations) if available.

Parameters
----------
input_filepath : str
    Path to audio file.

Returns
-------
comments : str
    File comments from header.
    If no comments are present, returns an empty string.


validate_input_file(input_filepath)<EOL>output = soxi(input_filepath, '<STR_LIT:D>')<EOL>if output == '<STR_LIT:0>':<EOL><INDENT>logger.warning("<STR_LIT>", input_filepath)<EOL><DEDENT>return float(output)<EOL>

Show duration in seconds (0 if unavailable).

Parameters
----------
input_filepath : str
    Path to audio file.

Returns
-------
duration : float
    Duration of audio file in seconds.
    If unavailable or empty, returns 0.


validate_input_file(input_filepath)<EOL>output = soxi(input_filepath, '<STR_LIT:e>')<EOL>return str(output)<EOL>

Show the name of the audio encoding.

Parameters
----------
input_filepath : str
    Path to audio file.

Returns
-------
encoding : str
    audio encoding type


validate_input_file(input_filepath)<EOL>output = soxi(input_filepath, '<STR_LIT:t>')<EOL>return str(output)<EOL>

Show detected file-type.

Parameters
----------
input_filepath : str
    Path to audio file.

Returns
-------
file_type : str
    file format type (ex. 'wav')


validate_input_file(input_filepath)<EOL>output = soxi(input_filepath, '<STR_LIT:s>')<EOL>if output == '<STR_LIT:0>':<EOL><INDENT>logger.warning("<STR_LIT>", input_filepath)<EOL><DEDENT>return int(output)<EOL>

Show number of samples (0 if unavailable).

Parameters
----------
input_filepath : str
    Path to audio file.

Returns
-------
n_samples : int
    total number of samples in audio file.
    Returns 0 if empty or unavailable


validate_input_file(input_filepath)<EOL>output = soxi(input_filepath, '<STR_LIT:r>')<EOL>return float(output)<EOL>

Show sample-rate.

Parameters
----------
input_filepath : str
    Path to audio file.

Returns
-------
samplerate : float
    number of samples/second


validate_input_file(input_filepath)<EOL>stat_dictionary = stat(input_filepath)<EOL>mean_norm = stat_dictionary['<STR_LIT>']<EOL>if mean_norm is not float('<STR_LIT>'):<EOL><INDENT>if mean_norm >= threshold:<EOL><INDENT>return False<EOL><DEDENT>else:<EOL><INDENT>return True<EOL><DEDENT><DEDENT>else:<EOL><INDENT>return True<EOL><DEDENT>

Determine if an input file is silent.

Parameters
----------
input_filepath : str
    The input filepath.
threshold : float
    Threshold for determining silence

Returns
-------
is_silent : bool
    True if file is determined silent.


if not os.path.exists(input_filepath):<EOL><INDENT>raise IOError(<EOL>"<STR_LIT>".format(input_filepath)<EOL>)<EOL><DEDENT>ext = file_extension(input_filepath)<EOL>if ext not in VALID_FORMATS:<EOL><INDENT>logger.info("<STR_LIT>", "<STR_LIT:U+0020>".join(VALID_FORMATS))<EOL>logger.warning(<EOL>"<STR_LIT>".format(ext)<EOL>)<EOL><DEDENT>

Input file validation function. Checks that file exists and can be
    processed by SoX.

    Parameters
    ----------
    input_filepath : str
        The input filepath.


if not isinstance(input_filepath_list, list):<EOL><INDENT>raise TypeError("<STR_LIT>")<EOL><DEDENT>elif len(input_filepath_list) < <NUM_LIT:2>:<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT>for input_filepath in input_filepath_list:<EOL><INDENT>validate_input_file(input_filepath)<EOL><DEDENT>

Input file list validation function. Checks that object is a list and
    contains valid filepaths that can be processed by SoX.

    Parameters
    ----------
    input_filepath_list : list
        A list of filepaths.


nowrite_conditions = [<EOL>bool(os.path.dirname(output_filepath)) ornot os.access(os.getcwd(), os.W_OK),<EOL>not os.access(os.path.dirname(output_filepath), os.W_OK)]<EOL>if all(nowrite_conditions):<EOL><INDENT>raise IOError(<EOL>"<STR_LIT>".format(output_filepath)<EOL>)<EOL><DEDENT>ext = file_extension(output_filepath)<EOL>if ext not in VALID_FORMATS:<EOL><INDENT>logger.info("<STR_LIT>", "<STR_LIT:U+0020>".join(VALID_FORMATS))<EOL>logger.warning(<EOL>"<STR_LIT>".format(ext)<EOL>)<EOL><DEDENT>if os.path.exists(output_filepath):<EOL><INDENT>logger.warning(<EOL>'<STR_LIT>',<EOL>output_filepath<EOL>)<EOL><DEDENT>

Output file validation function. Checks that file can be written, and
    has a valid file extension. Throws a warning if the path already exists,
    as it will be overwritten on build.

    Parameters
    ----------
    output_filepath : str
        The output filepath.

    Returns:
    --------
    output_filepath : str
        The output filepath.


return os.path.splitext(filepath)[<NUM_LIT:1>][<NUM_LIT:1>:]<EOL>

Get the extension of a filepath.

    Parameters
    ----------
    filepath : str
        File path.

    Returns:
    --------
    extension : str
        The file's extension


info_dictionary = {<EOL>'<STR_LIT>': channels(filepath),<EOL>'<STR_LIT>': sample_rate(filepath),<EOL>'<STR_LIT>': bitrate(filepath),<EOL>'<STR_LIT>': duration(filepath),<EOL>'<STR_LIT>': num_samples(filepath),<EOL>'<STR_LIT>': encoding(filepath),<EOL>'<STR_LIT>': silent(filepath)<EOL>}<EOL>return info_dictionary<EOL>

Get a dictionary of file information

    Parameters
    ----------
    filepath : str
        File path.

    Returns:
    --------
    info_dictionary : dict
        Dictionary of file information. Fields are:
            * channels
            * sample_rate
            * bitrate
            * duration
            * num_samples
            * encoding
            * silent


stat_output = _stat_call(filepath)<EOL>stat_dictionary = _parse_stat(stat_output)<EOL>return stat_dictionary<EOL>

Returns a dictionary of audio statistics.

    Parameters
    ----------
    filepath : str
        File path.

    Returns
    -------
    stat_dictionary : dict
        Dictionary of audio statistics.


validate_input_file(filepath)<EOL>args = ['<STR_LIT>', filepath, '<STR_LIT>', '<STR_LIT>']<EOL>_, _, stat_output = sox(args)<EOL>return stat_output<EOL>

Call sox's stat function.

    Parameters
    ----------
    filepath : str
        File path.

    Returns
    -------
    stat_output : str
        Sox output from stderr.


lines = stat_output.split('<STR_LIT:\n>')<EOL>stat_dict = {}<EOL>for line in lines:<EOL><INDENT>split_line = line.split('<STR_LIT::>')<EOL>if len(split_line) == <NUM_LIT:2>:<EOL><INDENT>key = split_line[<NUM_LIT:0>]<EOL>val = split_line[<NUM_LIT:1>].strip('<STR_LIT:U+0020>')<EOL>try:<EOL><INDENT>val = float(val)<EOL><DEDENT>except ValueError:<EOL><INDENT>val = None<EOL><DEDENT>stat_dict[key] = val<EOL><DEDENT><DEDENT>return stat_dict<EOL>

Parse the string output from sox's stat function

    Parameters
    ----------
    stat_output : str
        Sox output from stderr.

    Returns
    -------
    stat_dictionary : dict
        Dictionary of audio statistics.


if args[<NUM_LIT:0>].lower() != "<STR_LIT>":<EOL><INDENT>args.insert(<NUM_LIT:0>, "<STR_LIT>")<EOL><DEDENT>else:<EOL><INDENT>args[<NUM_LIT:0>] = "<STR_LIT>"<EOL><DEDENT>try:<EOL><INDENT>logger.info("<STR_LIT>", '<STR_LIT:U+0020>'.join(args))<EOL>process_handle = subprocess.Popen(<EOL>args, stdout=subprocess.PIPE, stderr=subprocess.PIPE<EOL>)<EOL>out, err = process_handle.communicate()<EOL>out = out.decode("<STR_LIT:utf-8>")<EOL>err = err.decode("<STR_LIT:utf-8>")<EOL>status = process_handle.returncode<EOL>return status, out, err<EOL><DEDENT>except OSError as error_msg:<EOL><INDENT>logger.error("<STR_LIT>", error_msg)<EOL><DEDENT>except TypeError as error_msg:<EOL><INDENT>logger.error("<STR_LIT>", error_msg)<EOL><DEDENT>return <NUM_LIT:1>, None, None<EOL>

Pass an argument list to SoX.

    Parameters
    ----------
    args : iterable
        Argument list for SoX. The first item can, but does not
        need to, be 'sox'.

    Returns:
    --------
    status : bool
        True on success.


if NO_SOX:<EOL><INDENT>return []<EOL><DEDENT>so = subprocess.check_output(['<STR_LIT>', '<STR_LIT>'])<EOL>if type(so) is not str:<EOL><INDENT>so = str(so, encoding='<STR_LIT>')<EOL><DEDENT>so = so.split('<STR_LIT:\n>')<EOL>idx = [i for i in range(len(so)) if '<STR_LIT>' in so[i]][<NUM_LIT:0>]<EOL>formats = so[idx].split('<STR_LIT:U+0020>')[<NUM_LIT:3>:]<EOL>return formats<EOL>

Calls SoX help for a lists of audio formats available with the current
    install of SoX.

    Returns:
    --------
    formats : list
        List of audio file extensions that SoX can process.


if argument not in SOXI_ARGS:<EOL><INDENT>raise ValueError("<STR_LIT>".format(argument))<EOL><DEDENT>args = ['<STR_LIT>', '<STR_LIT>']<EOL>args.append("<STR_LIT>".format(argument))<EOL>args.append(filepath)<EOL>try:<EOL><INDENT>shell_output = subprocess.check_output(<EOL>args,<EOL>stderr=subprocess.PIPE<EOL>)<EOL><DEDENT>except CalledProcessError as cpe:<EOL><INDENT>logger.info("<STR_LIT>".format(cpe.output))<EOL>raise SoxiError("<STR_LIT>".format(cpe.returncode))<EOL><DEDENT>shell_output = shell_output.decode("<STR_LIT:utf-8>")<EOL>return str(shell_output).strip('<STR_LIT:\n>')<EOL>

Base call to SoXI.

    Parameters
    ----------
    filepath : str
        Path to audio file.

    argument : str
        Argument to pass to SoXI.

    Returns
    -------
    shell_output : str
        Command line output of SoXI


if args[<NUM_LIT:0>].lower() != "<STR_LIT>":<EOL><INDENT>args.insert(<NUM_LIT:0>, "<STR_LIT>")<EOL><DEDENT>else:<EOL><INDENT>args[<NUM_LIT:0>] = "<STR_LIT>"<EOL><DEDENT>try:<EOL><INDENT>logger.info("<STR_LIT>", "<STR_LIT:U+0020>".join(args))<EOL>process_handle = subprocess.Popen(<EOL>args, stdout=subprocess.PIPE, stderr=subprocess.PIPE<EOL>)<EOL>status = process_handle.wait()<EOL>if process_handle.stderr is not None:<EOL><INDENT>logger.info(process_handle.stderr)<EOL><DEDENT>if status == <NUM_LIT:0>:<EOL><INDENT>return True<EOL><DEDENT>else:<EOL><INDENT>logger.info("<STR_LIT>", status)<EOL>return False<EOL><DEDENT><DEDENT>except OSError as error_msg:<EOL><INDENT>logger.error("<STR_LIT>", error_msg)<EOL><DEDENT>except TypeError as error_msg:<EOL><INDENT>logger.error("<STR_LIT>", error_msg)<EOL><DEDENT>return False<EOL>

Pass an argument list to play.

    Parameters
    ----------
    args : iterable
        Argument list for play. The first item can, but does not
        need to, be 'play'.

    Returns:
    --------
    status : bool
        True on success.


try:<EOL><INDENT>float(var)<EOL>return True<EOL><DEDENT>except ValueError:<EOL><INDENT>return False<EOL><DEDENT>except TypeError:<EOL><INDENT>return False<EOL><DEDENT>

Check if variable is a numeric value.

    Parameters
    ----------
    var : object

    Returns:
    --------
    bool
        True if var is numeric, False otherwise.


return len(set(list_of_things)) <= <NUM_LIT:1><EOL>

Check if a list contains identical elements.

    Parameters
    ----------
    list_of_things : list
        list of objects

    Returns:
    --------
    bool
        True if all list elements are the same.


pass<EOL>


@app.route("<STR_LIT>", methods=["<STR_LIT:GET>", "<STR_LIT>"])<EOL>def list_users(request):

pass<EOL>

responses:
  200:
    description: A list of users.
    examples:
      [{"username": "tom"}, {"username": "lucy"}]


pass<EOL>

responses:
  200:
    description: A user.
    examples:
      {"username": "tom"}


pass<EOL>

This a regular docstring example (not included in schema)

---

responses:
  200:
    description: This is included in the schema.


pass<EOL>

This a regular docstring example (not included in schema)


pass<EOL>

responses:
  200:
    description: This endpoint is part of a subapp.


pass<EOL>

responses:
  200:
    description: A list of organisations.
    examples:
      [{"name": "Foo Corp."}, {"name": "Acme Ltd."}]


pass<EOL>

responses:
  200:
    description: An organisation.
    examples:
      {"name": "Foo Corp."}


with open(os.path.join(package, "<STR_LIT>")) as f:<EOL><INDENT>return re.search("<STR_LIT>", f.read()).group(<NUM_LIT:1>)<EOL><DEDENT>

Return package version as listed in `__version__` in `init.py`.


with open("<STR_LIT>", encoding="<STR_LIT:utf8>") as f:<EOL><INDENT>return f.read()<EOL><DEDENT>


return [<EOL>dirpath<EOL>for dirpath, dirnames, filenames in os.walk(package)<EOL>if os.path.exists(os.path.join(dirpath, "<STR_LIT>"))<EOL>]<EOL>

Return root package and all sub-packages.


async def on_connect(self, websocket: WebSocket) -> None:

await websocket.accept()<EOL>

Override to handle an incoming websocket connection


async def on_receive(self, websocket: WebSocket, data: typing.Any) -> None:

Override to handle an incoming websocket message


async def on_disconnect(self, websocket: WebSocket, close_code: int) -> None:

Override to handle a disconnecting websocket


def request_response(func: typing.Callable) -> ASGIApp:

is_coroutine = asyncio.iscoroutinefunction(func)<EOL>async def app(scope: Scope, receive: Receive, send: Send) -> None:<EOL><INDENT>request = Request(scope, receive=receive)<EOL>if is_coroutine:<EOL><INDENT>response = await func(request)<EOL><DEDENT>else:<EOL><INDENT>response = await run_in_threadpool(func, request)<EOL><DEDENT>await response(scope, receive, send)<EOL><DEDENT>return app<EOL>

Takes a function or coroutine `func(request) -> response`,
and returns an ASGI application.


def websocket_session(func: typing.Callable) -> ASGIApp:

<EOL>async def app(scope: Scope, receive: Receive, send: Send) -> None:<EOL><INDENT>session = WebSocket(scope, receive=receive, send=send)<EOL>await func(session)<EOL><DEDENT>return app<EOL>

Takes a coroutine `func(session)`, and returns an ASGI application.


def compile_path(<EOL>path: str<EOL>) -> typing.Tuple[typing.Pattern, str, typing.Dict[str, Convertor]]:

path_regex = "<STR_LIT>"<EOL>path_format = "<STR_LIT>"<EOL>idx = <NUM_LIT:0><EOL>param_convertors = {}<EOL>for match in PARAM_REGEX.finditer(path):<EOL><INDENT>param_name, convertor_type = match.groups("<STR_LIT:str>")<EOL>convertor_type = convertor_type.lstrip("<STR_LIT::>")<EOL>assert (<EOL>convertor_type in CONVERTOR_TYPES<EOL>), f"<STR_LIT>"<EOL>convertor = CONVERTOR_TYPES[convertor_type]<EOL>path_regex += path[idx : match.start()]<EOL>path_regex += f"<STR_LIT>"<EOL>path_format += path[idx : match.start()]<EOL>path_format += "<STR_LIT>" % param_name<EOL>param_convertors[param_name] = convertor<EOL>idx = match.end()<EOL><DEDENT>path_regex += path[idx:] + "<STR_LIT:$>"<EOL>path_format += path[idx:]<EOL>return re.compile(path_regex), path_format, param_convertors<EOL>

Given a path string, like: "/{username:str}", return a three-tuple
of (regex, format, {param_name:convertor}).

regex:      "/(?P<username>[^/]+)"
format:     "/{username}"
convertors: {"username": StringConvertor()}


def build_environ(scope: Scope, body: bytes) -> dict:

environ = {<EOL>"<STR_LIT>": scope["<STR_LIT>"],<EOL>"<STR_LIT>": scope.get("<STR_LIT>", "<STR_LIT>"),<EOL>"<STR_LIT>": scope["<STR_LIT:path>"],<EOL>"<STR_LIT>": scope["<STR_LIT>"].decode("<STR_LIT:ascii>"),<EOL>"<STR_LIT>": f"<STR_LIT>",<EOL>"<STR_LIT>": (<NUM_LIT:1>, <NUM_LIT:0>),<EOL>"<STR_LIT>": scope.get("<STR_LIT>", "<STR_LIT:http>"),<EOL>"<STR_LIT>": io.BytesIO(body),<EOL>"<STR_LIT>": sys.stdout,<EOL>"<STR_LIT>": True,<EOL>"<STR_LIT>": True,<EOL>"<STR_LIT>": False,<EOL>}<EOL>server = scope.get("<STR_LIT>") or ("<STR_LIT:localhost>", <NUM_LIT>)<EOL>environ["<STR_LIT>"] = server[<NUM_LIT:0>]<EOL>environ["<STR_LIT>"] = server[<NUM_LIT:1>]<EOL>if scope.get("<STR_LIT>"):<EOL><INDENT>environ["<STR_LIT>"] = scope["<STR_LIT>"][<NUM_LIT:0>]<EOL><DEDENT>for name, value in scope.get("<STR_LIT>", []):<EOL><INDENT>name = name.decode("<STR_LIT>")<EOL>if name == "<STR_LIT>":<EOL><INDENT>corrected_name = "<STR_LIT>"<EOL><DEDENT>elif name == "<STR_LIT>":<EOL><INDENT>corrected_name = "<STR_LIT>"<EOL><DEDENT>else:<EOL><INDENT>corrected_name = f"<STR_LIT>".upper().replace("<STR_LIT:->", "<STR_LIT:_>")<EOL><DEDENT>value = value.decode("<STR_LIT>")<EOL>if corrected_name in environ:<EOL><INDENT>value = environ[corrected_name] + "<STR_LIT:U+002C>" + value<EOL><DEDENT>environ[corrected_name] = value<EOL><DEDENT>return environ<EOL>

Builds a scope and request body into a WSGI environ object.


def __setitem__(self, key: str, value: str) -> None:

set_key = key.lower().encode("<STR_LIT>")<EOL>set_value = value.encode("<STR_LIT>")<EOL>found_indexes = []<EOL>for idx, (item_key, item_value) in enumerate(self._list):<EOL><INDENT>if item_key == set_key:<EOL><INDENT>found_indexes.append(idx)<EOL><DEDENT><DEDENT>for idx in reversed(found_indexes[<NUM_LIT:1>:]):<EOL><INDENT>del self._list[idx]<EOL><DEDENT>if found_indexes:<EOL><INDENT>idx = found_indexes[<NUM_LIT:0>]<EOL>self._list[idx] = (set_key, set_value)<EOL><DEDENT>else:<EOL><INDENT>self._list.append((set_key, set_value))<EOL><DEDENT>

Set the header `key` to `value`, removing any duplicate entries.
Retains insertion order.


def __delitem__(self, key: str) -> None:

del_key = key.lower().encode("<STR_LIT>")<EOL>pop_indexes = []<EOL>for idx, (item_key, item_value) in enumerate(self._list):<EOL><INDENT>if item_key == del_key:<EOL><INDENT>pop_indexes.append(idx)<EOL><DEDENT><DEDENT>for idx in reversed(pop_indexes):<EOL><INDENT>del self._list[idx]<EOL><DEDENT>


def setdefault(self, key: str, value: str) -> str:

set_key = key.lower().encode("<STR_LIT>")<EOL>set_value = value.encode("<STR_LIT>")<EOL>for idx, (item_key, item_value) in enumerate(self._list):<EOL><INDENT>if item_key == set_key:<EOL><INDENT>return item_value.decode("<STR_LIT>")<EOL><DEDENT><DEDENT>self._list.append((set_key, set_value))<EOL>return value<EOL>

If the header `key` does not exist, then set it to `value`.
Returns the header value.


def append(self, key: str, value: str) -> None:

append_key = key.lower().encode("<STR_LIT>")<EOL>append_value = value.encode("<STR_LIT>")<EOL>self._list.append((append_key, append_value))<EOL>

Append a header, preserving any duplicate entries.


def get_endpoints(<EOL>self, routes: typing.List[BaseRoute]<EOL>) -> typing.List[EndpointInfo]:

endpoints_info: list = []<EOL>for route in routes:<EOL><INDENT>if isinstance(route, Mount):<EOL><INDENT>routes = route.routes or []<EOL>sub_endpoints = [<EOL>EndpointInfo(<EOL>path="<STR_LIT>".join((route.path, sub_endpoint.path)),<EOL>http_method=sub_endpoint.http_method,<EOL>func=sub_endpoint.func,<EOL>)<EOL>for sub_endpoint in self.get_endpoints(routes)<EOL>]<EOL>endpoints_info.extend(sub_endpoints)<EOL><DEDENT>elif not isinstance(route, Route) or not route.include_in_schema:<EOL><INDENT>continue<EOL><DEDENT>elif inspect.isfunction(route.endpoint) or inspect.ismethod(route.endpoint):<EOL><INDENT>for method in route.methods or ["<STR_LIT:GET>"]:<EOL><INDENT>if method == "<STR_LIT>":<EOL><INDENT>continue<EOL><DEDENT>endpoints_info.append(<EOL>EndpointInfo(route.path, method.lower(), route.endpoint)<EOL>)<EOL><DEDENT><DEDENT>else:<EOL><INDENT>for method in ["<STR_LIT>", "<STR_LIT>", "<STR_LIT>", "<STR_LIT>", "<STR_LIT>", "<STR_LIT>"]:<EOL><INDENT>if not hasattr(route.endpoint, method):<EOL><INDENT>continue<EOL><DEDENT>func = getattr(route.endpoint, method)<EOL>endpoints_info.append(<EOL>EndpointInfo(route.path, method.lower(), func)<EOL>)<EOL><DEDENT><DEDENT><DEDENT>return endpoints_info<EOL>

Given the routes, yields the following information:

- path
    eg: /users/
- http_method
    one of 'get', 'post', 'put', 'patch', 'delete', 'options'
- func
    method ready to extract the docstring


def parse_docstring(self, func_or_method: typing.Callable) -> dict:

docstring = func_or_method.__doc__<EOL>if not docstring:<EOL><INDENT>return {}<EOL><DEDENT>docstring = docstring.split("<STR_LIT>")[-<NUM_LIT:1>]<EOL>parsed = yaml.safe_load(docstring)<EOL>if not isinstance(parsed, dict):<EOL><INDENT>return {}<EOL><DEDENT>return parsed<EOL>

Given a function, parse the docstring as YAML and return a dictionary of info.


def get_directories(<EOL>self, directory: str = None, packages: typing.List[str] = None<EOL>) -> typing.List[str]:

directories = []<EOL>if directory is not None:<EOL><INDENT>directories.append(directory)<EOL><DEDENT>for package in packages or []:<EOL><INDENT>spec = importlib.util.find_spec(package)<EOL>assert spec is not None, f"<STR_LIT>"<EOL>assert (<EOL>spec.origin is not None<EOL>), "<STR_LIT>"<EOL>directory = os.path.normpath(os.path.join(spec.origin, "<STR_LIT:..>", "<STR_LIT>"))<EOL>assert os.path.isdir(<EOL>directory<EOL>), "<STR_LIT>"<EOL>directories.append(directory)<EOL><DEDENT>return directories<EOL>

Given `directory` and `packages` arugments, return a list of all the
directories that should be used for serving static files from.


async def __call__(self, scope: Scope, receive: Receive, send: Send) -> None:

assert scope["<STR_LIT:type>"] == "<STR_LIT:http>"<EOL>if not self.config_checked:<EOL><INDENT>await self.check_config()<EOL>self.config_checked = True<EOL><DEDENT>path = self.get_path(scope)<EOL>response = await self.get_response(path, scope)<EOL>await response(scope, receive, send)<EOL>


def get_path(self, scope: Scope) -> str:

return os.path.normpath(os.path.join(*scope["<STR_LIT:path>"].split("<STR_LIT:/>")))<EOL>

Given the ASGI scope, return the `path` string to serve up,
with OS specific path seperators, and any '..', '.' components removed.


async def get_response(self, path: str, scope: Scope) -> Response:

if scope["<STR_LIT>"] not in ("<STR_LIT:GET>", "<STR_LIT>"):<EOL><INDENT>return PlainTextResponse("<STR_LIT>", status_code=<NUM_LIT>)<EOL><DEDENT>if path.startswith("<STR_LIT:..>"):<EOL><INDENT>return PlainTextResponse("<STR_LIT>", status_code=<NUM_LIT>)<EOL><DEDENT>full_path, stat_result = await self.lookup_path(path)<EOL>if stat_result and stat.S_ISREG(stat_result.st_mode):<EOL><INDENT>return self.file_response(full_path, stat_result, scope)<EOL><DEDENT>elif stat_result and stat.S_ISDIR(stat_result.st_mode) and self.html:<EOL><INDENT>index_path = os.path.join(path, "<STR_LIT>")<EOL>full_path, stat_result = await self.lookup_path(index_path)<EOL>if stat_result is not None and stat.S_ISREG(stat_result.st_mode):<EOL><INDENT>if not scope["<STR_LIT:path>"].endswith("<STR_LIT:/>"):<EOL><INDENT>url = URL(scope=scope)<EOL>url = url.replace(path=url.path + "<STR_LIT:/>")<EOL>return RedirectResponse(url=url)<EOL><DEDENT>return self.file_response(full_path, stat_result, scope)<EOL><DEDENT><DEDENT>if self.html:<EOL><INDENT>full_path, stat_result = await self.lookup_path("<STR_LIT>")<EOL>if stat_result is not None and stat.S_ISREG(stat_result.st_mode):<EOL><INDENT>return self.file_response(<EOL>full_path, stat_result, scope, status_code=<NUM_LIT><EOL>)<EOL><DEDENT><DEDENT>return PlainTextResponse("<STR_LIT>", status_code=<NUM_LIT>)<EOL>

Returns an HTTP response, given the incoming path, method and request headers.


if self.directory is None:<EOL><INDENT>return<EOL><DEDENT>try:<EOL><INDENT>stat_result = await aio_stat(self.directory)<EOL><DEDENT>except FileNotFoundError:<EOL><INDENT>raise RuntimeError(<EOL>f"<STR_LIT>"<EOL>)<EOL><DEDENT>if not (stat.S_ISDIR(stat_result.st_mode) or stat.S_ISLNK(stat_result.st_mode)):<EOL><INDENT>raise RuntimeError(<EOL>f"<STR_LIT>"<EOL>)<EOL><DEDENT>

Perform a one-off configuration check that StaticFiles is actually
pointed at a directory, so that we can raise loud errors rather than
just returning 404 responses.


def is_not_modified(<EOL>self, response_headers: Headers, request_headers: Headers<EOL>) -> bool:

try:<EOL><INDENT>if_none_match = request_headers["<STR_LIT>"]<EOL>etag = response_headers["<STR_LIT>"]<EOL>if if_none_match == etag:<EOL><INDENT>return True<EOL><DEDENT><DEDENT>except KeyError:<EOL><INDENT>pass<EOL><DEDENT>try:<EOL><INDENT>if_modified_since = parsedate(request_headers["<STR_LIT>"])<EOL>last_modified = parsedate(response_headers["<STR_LIT>"])<EOL>if (<EOL>if_modified_since is not None<EOL>and last_modified is not None<EOL>and if_modified_since >= last_modified<EOL>):<EOL><INDENT>return True<EOL><DEDENT><DEDENT>except KeyError:<EOL><INDENT>pass<EOL><DEDENT>return False<EOL>

Given the request and response headers, return `True` if an HTTP
"Not Modified" response could be returned instead.


scope = self.scope<EOL>receive = self._asgi_receive<EOL>send = self._asgi_send<EOL>try:<EOL><INDENT>self._loop.run_until_complete(self.app(scope, receive, send))<EOL><DEDENT>except BaseException as exc:<EOL><INDENT>self._send_queue.put(exc)<EOL><DEDENT>

The sub-thread in which the websocket session runs.


if self.client_state == WebSocketState.CONNECTING:<EOL><INDENT>message = await self._receive()<EOL>message_type = message["<STR_LIT:type>"]<EOL>assert message_type == "<STR_LIT>"<EOL>self.client_state = WebSocketState.CONNECTED<EOL>return message<EOL><DEDENT>elif self.client_state == WebSocketState.CONNECTED:<EOL><INDENT>message = await self._receive()<EOL>message_type = message["<STR_LIT:type>"]<EOL>assert message_type in {"<STR_LIT>", "<STR_LIT>"}<EOL>if message_type == "<STR_LIT>":<EOL><INDENT>self.client_state = WebSocketState.DISCONNECTED<EOL><DEDENT>return message<EOL><DEDENT>else:<EOL><INDENT>raise RuntimeError(<EOL>'<STR_LIT>'<EOL>)<EOL><DEDENT>

Receive ASGI websocket messages, ensuring valid state transitions.


async def send(self, message: Message) -> None:

if self.application_state == WebSocketState.CONNECTING:<EOL><INDENT>message_type = message["<STR_LIT:type>"]<EOL>assert message_type in {"<STR_LIT>", "<STR_LIT>"}<EOL>if message_type == "<STR_LIT>":<EOL><INDENT>self.application_state = WebSocketState.DISCONNECTED<EOL><DEDENT>else:<EOL><INDENT>self.application_state = WebSocketState.CONNECTED<EOL><DEDENT>await self._send(message)<EOL><DEDENT>elif self.application_state == WebSocketState.CONNECTED:<EOL><INDENT>message_type = message["<STR_LIT:type>"]<EOL>assert message_type in {"<STR_LIT>", "<STR_LIT>"}<EOL>if message_type == "<STR_LIT>":<EOL><INDENT>self.application_state = WebSocketState.DISCONNECTED<EOL><DEDENT>await self._send(message)<EOL><DEDENT>else:<EOL><INDENT>raise RuntimeError('<STR_LIT>')<EOL><DEDENT>

Send ASGI websocket messages, ensuring valid state transitions.


log_base = np.log(base)<EOL>sign = np.sign(y)<EOL>logs = np.log(np.abs(y) / log_base)<EOL>return sign * logs<EOL>

Symmetrical logarithmic scale.

    Optional arguments:

    *base*:
        The base of the logarithm.


def filter_savitzky_golay(y, window_size=<NUM_LIT:5>, order=<NUM_LIT:2>, deriv=<NUM_LIT:0>, rate=<NUM_LIT:1>):

try:<EOL><INDENT>window_size = np.abs(np.int(window_size))<EOL>order = np.abs(np.int(order))<EOL><DEDENT>except ValueError:<EOL><INDENT>raise ValueError('<STR_LIT>')<EOL><DEDENT>if window_size % <NUM_LIT:2> != <NUM_LIT:1> or window_size < <NUM_LIT:1>:<EOL><INDENT>raise ValueError('<STR_LIT>')<EOL><DEDENT>if window_size < order + <NUM_LIT:2>:<EOL><INDENT>raise ValueError('<STR_LIT>')<EOL><DEDENT>order_range = range(order + <NUM_LIT:1>)<EOL>half_window = (window_size - <NUM_LIT:1>) // <NUM_LIT:2><EOL>minimum = np.min(y)<EOL>maximum = np.max(y)<EOL>b = np.mat([<EOL>[k ** i for i in order_range]<EOL>for k in range(-half_window, half_window + <NUM_LIT:1>)<EOL>])<EOL>m = np.linalg.pinv(b).A[deriv] * rate ** deriv * math.factorial(deriv)<EOL>firstvals = y[<NUM_LIT:0>] - np.abs(y[<NUM_LIT:1>:half_window+<NUM_LIT:1>][::-<NUM_LIT:1>] - y[<NUM_LIT:0>])<EOL>lastvals = y[-<NUM_LIT:1>] + np.abs(y[-half_window-<NUM_LIT:1>:-<NUM_LIT:1>][::-<NUM_LIT:1>] - y[-<NUM_LIT:1>])<EOL>y = np.concatenate((firstvals, y, lastvals))<EOL>return np.clip(<EOL>np.convolve(m[::-<NUM_LIT:1>], y, mode='<STR_LIT>'),<EOL>minimum,<EOL>maximum,<EOL>)<EOL>

Smooth (and optionally differentiate) with a Savitzky-Golay filter.


parser.print_usage()<EOL>print('<STR_LIT>')<EOL>print('<STR_LIT>')<EOL>for function in sorted(FUNCTION):<EOL><INDENT>doc = FUNCTION[function].__doc__.strip().splitlines()[<NUM_LIT:0>]<EOL>print('<STR_LIT>' % (function + '<STR_LIT::>', doc))<EOL><DEDENT>return <NUM_LIT:0><EOL>

Show usage and available curve functions.


parser.print_usage()<EOL>print('<STR_LIT>')<EOL>print('<STR_LIT>')<EOL>for palette in sorted(PALETTE):<EOL><INDENT>print('<STR_LIT>' % (palette,))<EOL><DEDENT>return <NUM_LIT:0><EOL>


import argparse<EOL>parser = argparse.ArgumentParser(<EOL>description=(<EOL>'<STR_LIT>'<EOL>),<EOL>epilog=

Main entrypoint if invoked via the command line.


curses.setupterm()<EOL>


number = curses.tigetnum('<STR_LIT>') or <NUM_LIT:0><EOL>return <NUM_LIT:16> if number == <NUM_LIT:8> else number<EOL>

Get the number of colors supported by this terminal.


_, encoding = locale.getdefaultlocale()<EOL>return encoding<EOL>


return self.size[<NUM_LIT:1>]<EOL>


for fd in range(<NUM_LIT:3>):<EOL><INDENT>cr = self._ioctl_GWINSZ(fd)<EOL>if cr:<EOL><INDENT>break<EOL><DEDENT><DEDENT>if not cr:<EOL><INDENT>try:<EOL><INDENT>fd = os.open(os.ctermid(), os.O_RDONLY)<EOL>cr = self._ioctl_GWINSZ(fd)<EOL>os.close(fd)<EOL><DEDENT>except Exception:<EOL><INDENT>pass<EOL><DEDENT><DEDENT>if not cr:<EOL><INDENT>env = os.environ<EOL>cr = (env.get('<STR_LIT>', <NUM_LIT>), env.get('<STR_LIT>', <NUM_LIT>))<EOL><DEDENT>return int(cr[<NUM_LIT:1>]), int(cr[<NUM_LIT:0>])<EOL>


return self.size[<NUM_LIT:0>]<EOL>


try:<EOL><INDENT>import fcntl<EOL>import termios<EOL>import struct<EOL>return struct.unpack(<EOL>'<STR_LIT>',<EOL>fcntl.ioctl(fd, termios.TIOCGWINSZ, '<STR_LIT>')<EOL>)<EOL><DEDENT>except Exception:<EOL><INDENT>return None<EOL><DEDENT>

Get terminal size using ``TIOCGWINSZ``.

        Internal function that will try to request the ``TIOCGWINSZ`` against
        the selected file descriptor ``fd``.


if self.colors == <NUM_LIT:16>:<EOL><INDENT>if index >= <NUM_LIT:8>:<EOL><INDENT>return self.csi('<STR_LIT>') + self.csi('<STR_LIT>', index - <NUM_LIT:8>)<EOL><DEDENT>else:<EOL><INDENT>return self.csi('<STR_LIT>') + self.csi('<STR_LIT>', index)<EOL><DEDENT><DEDENT>else:<EOL><INDENT>return self.csi('<STR_LIT>', index)<EOL><DEDENT>

Get the escape sequence for indexed color ``index``.

        The ``index`` is a color index in the 256 color space. The color space
        consists of:

        * 0x00-0x0f: default EGA colors
        * 0x10-0xe7: 6x6x6 RGB cubes
        * 0xe8-0xff: gray scale ramp


value = curses.tigetstr(capname)<EOL>if value is None:<EOL><INDENT>return b'<STR_LIT>'<EOL><DEDENT>else:<EOL><INDENT>return curses.tparm(value, *args)<EOL><DEDENT>

Return the escape sequence for the selected Control Sequence.


def csi_wrap(self, value, capname, *args):

if isinstance(value, str):<EOL><INDENT>value = value.encode('<STR_LIT:utf-8>')<EOL><DEDENT>return b'<STR_LIT>'.join([<EOL>self.csi(capname, *args),<EOL>value,<EOL>self.csi('<STR_LIT>'),<EOL>])<EOL>

Return a value wrapped in the selected CSI and does a reset.


self.x = coordinates[<NUM_LIT:0>]<EOL>self.y = coordinates[<NUM_LIT:1>]<EOL>


return Point((self.x, self.y))<EOL>

Return a fresh copy of the current point.


return self.size.x<EOL>


return self.size.y<EOL>


if not isinstance(point, Point):<EOL><INDENT>point = Point(point)<EOL><DEDENT>if point.y not in self.canvas:<EOL><INDENT>return False<EOL><DEDENT>else:<EOL><INDENT>return point.x in self.canvas[point.y]<EOL><DEDENT>


if not isinstance(point, Point):<EOL><INDENT>point = Point(point)<EOL><DEDENT>if point.y > self.size.y:<EOL><INDENT>if self.extend_y:<EOL><INDENT>self.size.y = point.y<EOL><DEDENT>else:<EOL><INDENT>raise OverflowError('<STR_LIT>' % (<EOL>self,<EOL>point.y,<EOL>self.size.y,<EOL>))<EOL><DEDENT><DEDENT>if point.x > self.size.x:<EOL><INDENT>if self.extend_x:<EOL><INDENT>self.size.x = point.x<EOL><DEDENT>else:<EOL><INDENT>raise OverflowError('<STR_LIT>' % (<EOL>self,<EOL>point.x,<EOL>self.size.x,<EOL>))<EOL><DEDENT><DEDENT>self.canvas[point.y][point.x] = value<EOL>


if not isinstance(point, Point):<EOL><INDENT>point = Point(point)<EOL><DEDENT>return self.canvas[point.y][point.x]<EOL>


def consume(self, istream, ostream, batch=False):

datapoints = []  <EOL>if batch:<EOL><INDENT>sleep = max(<NUM_LIT>, self.option.sleep)<EOL>fd = istream.fileno()<EOL>while True:<EOL><INDENT>try:<EOL><INDENT>if select.select([fd], [], [], sleep):<EOL><INDENT>try:<EOL><INDENT>line = istream.readline()<EOL>if line == '<STR_LIT>':<EOL><INDENT>break<EOL><DEDENT>datapoints.append(self.consume_line(line))<EOL><DEDENT>except ValueError:<EOL><INDENT>continue<EOL><DEDENT>if self.option.sort_by_column:<EOL><INDENT>datapoints = sorted(datapoints, key=itemgetter(self.option.sort_by_column - <NUM_LIT:1>))<EOL><DEDENT>if len(datapoints) > <NUM_LIT:1>:<EOL><INDENT>datapoints = datapoints[-self.maximum_points:]<EOL>self.update([dp[<NUM_LIT:0>] for dp in datapoints], [dp[<NUM_LIT:1>] for dp in datapoints])<EOL>self.render(ostream)<EOL><DEDENT>time.sleep(sleep)<EOL><DEDENT><DEDENT>except KeyboardInterrupt:<EOL><INDENT>break<EOL><DEDENT><DEDENT><DEDENT>else:<EOL><INDENT>for line in istream:<EOL><INDENT>try:<EOL><INDENT>datapoints.append(self.consume_line(line))<EOL><DEDENT>except ValueError:<EOL><INDENT>pass<EOL><DEDENT><DEDENT>if self.option.sort_by_column:<EOL><INDENT>datapoints = sorted(datapoints, key=itemgetter(self.option.sort_by_column - <NUM_LIT:1>))<EOL><DEDENT>self.update([dp[<NUM_LIT:0>] for dp in datapoints], [dp[<NUM_LIT:1>] for dp in datapoints])<EOL>self.render(ostream)<EOL><DEDENT>

Read points from istream and output to ostream.


data = RE_VALUE_KEY.split(line.strip(), <NUM_LIT:1>)<EOL>if len(data) == <NUM_LIT:1>:<EOL><INDENT>return float(data[<NUM_LIT:0>]), None<EOL><DEDENT>else:<EOL><INDENT>return float(data[<NUM_LIT:0>]), data[<NUM_LIT:1>].strip()<EOL><DEDENT>


return <NUM_LIT:1><EOL>


self.values = values or [None] * len(points)<EOL>if np is None:<EOL><INDENT>if self.option.function:<EOL><INDENT>warnings.warn('<STR_LIT>')<EOL><DEDENT>self.points = points<EOL>self.minimum = min(self.points)<EOL>self.maximum = max(self.points)<EOL>self.current = self.points[-<NUM_LIT:1>]<EOL><DEDENT>else:<EOL><INDENT>self.points = self.apply_function(points)<EOL>self.minimum = np.min(self.points)<EOL>self.maximum = np.max(self.points)<EOL>self.current = self.points[-<NUM_LIT:1>]<EOL><DEDENT>if self.maximum == self.minimum:<EOL><INDENT>self.extents = <NUM_LIT:1><EOL><DEDENT>else:<EOL><INDENT>self.extents = (self.maximum - self.minimum)<EOL>self.extents = (self.maximum - self.minimum)<EOL><DEDENT>


color = PALETTE.get(self.option.palette, {})<EOL>color = color.get(self.term.colors, None)<EOL>color_ramp = []<EOL>if color is not None:<EOL><INDENT>ratio = len(color) / float(size)<EOL>for i in range(int(size)):<EOL><INDENT>color_ramp.append(self.term.color(color[int(ratio * i)]))<EOL><DEDENT><DEDENT>return color_ramp<EOL>

Generate a color ramp for the current screen height.


def human(self, size, base=<NUM_LIT:1000>, units='<STR_LIT>'):

sign = '<STR_LIT:+>' if size >= <NUM_LIT:0> else '<STR_LIT:->'<EOL>size = abs(size)<EOL>if size < <NUM_LIT:1000>:<EOL><INDENT>return '<STR_LIT>' % (sign, size)<EOL><DEDENT>for i, suffix in enumerate(units):<EOL><INDENT>unit = <NUM_LIT:1000> ** (i + <NUM_LIT:1>)<EOL>if size < unit:<EOL><INDENT>return ('<STR_LIT>' % (<EOL>sign,<EOL>size / float(unit) * base,<EOL>suffix,<EOL>)).strip()<EOL><DEDENT><DEDENT>raise OverflowError<EOL>

Convert the input ``size`` to human readable, short form.


if not self.option.function:<EOL><INDENT>return points<EOL><DEDENT>if np is None:<EOL><INDENT>raise ImportError('<STR_LIT>')<EOL><DEDENT>if '<STR_LIT::>' in self.option.function:<EOL><INDENT>function, arguments = self.option.function.split('<STR_LIT::>', <NUM_LIT:1>)<EOL>arguments = arguments.split('<STR_LIT:U+002C>')<EOL><DEDENT>else:<EOL><INDENT>function = self.option.function<EOL>arguments = []<EOL><DEDENT>arguments = list(map(self._function_argument, arguments))<EOL>filter_function = FUNCTION.get(function)<EOL>if filter_function is None:<EOL><INDENT>raise TypeError('<STR_LIT>' % (function,))<EOL><DEDENT>else:<EOL><INDENT>return filter_function(np.array(list(points)), *arguments)<EOL><DEDENT>

Run the filter function on the provided points.


if value in FUNCTION_CONSTANT:<EOL><INDENT>return FUNCTION_CONSTANT[value]<EOL><DEDENT>else:<EOL><INDENT>return float(value)<EOL><DEDENT>

Resolve function, convert to float if not found.


def line(self, p1, p2, resolution=<NUM_LIT:1>):

xdiff = max(p1.x, p2.x) - min(p1.x, p2.x)<EOL>ydiff = max(p1.y, p2.y) - min(p1.y, p2.y)<EOL>xdir = [-<NUM_LIT:1>, <NUM_LIT:1>][int(p1.x <= p2.x)]<EOL>ydir = [-<NUM_LIT:1>, <NUM_LIT:1>][int(p1.y <= p2.y)]<EOL>r = int(round(max(xdiff, ydiff)))<EOL>if r == <NUM_LIT:0>:<EOL><INDENT>return<EOL><DEDENT>for i in range((r + <NUM_LIT:1>) * resolution):<EOL><INDENT>x = p1.x<EOL>y = p1.y<EOL>if xdiff:<EOL><INDENT>x += (float(i) * xdiff) / r * xdir / resolution<EOL><DEDENT>if ydiff:<EOL><INDENT>y += (float(i) * ydiff) / r * ydir / resolution<EOL><DEDENT>yield Point((x, y))<EOL><DEDENT>

Resolve the points to make a line between two points.


raise NotImplementedError()<EOL>


raise NotImplementedError()<EOL>

Render the graph to the selected output stream.


return int(value)<EOL>

Get an integer value for the input value.


if not self.option.legend:<EOL><INDENT>return<EOL><DEDENT>if not isinstance(point, Point):<EOL><INDENT>point = Point(point)<EOL><DEDENT>for offset, char in enumerate(str(text)):<EOL><INDENT>self.screen.canvas[point.y][point.x + offset] = char<EOL><DEDENT>


return self.size.x * <NUM_LIT:2><EOL>


return self.size.y * <NUM_LIT:4><EOL>


encoding = self.option.encoding or self.term.encoding or "<STR_LIT:utf8>"<EOL>if self.option.color:<EOL><INDENT>ramp = self.color_ramp(self.size.y)[::-<NUM_LIT:1>]<EOL><DEDENT>else:<EOL><INDENT>ramp = None<EOL><DEDENT>if self.cycle >= <NUM_LIT:1> and self.lines:<EOL><INDENT>stream.write(self.term.csi('<STR_LIT>', self.lines))<EOL><DEDENT>zero = int(self.null / <NUM_LIT:4>)  <EOL>lines = <NUM_LIT:0><EOL>for y in range(self.screen.size.y):<EOL><INDENT>if y == zero and self.size.y > <NUM_LIT:1>:<EOL><INDENT>stream.write(self.term.csi('<STR_LIT>'))<EOL><DEDENT>if ramp:<EOL><INDENT>stream.write(ramp[y])<EOL><DEDENT>for x in range(self.screen.size.x):<EOL><INDENT>point = Point((x, y))<EOL>if point in self.screen:<EOL><INDENT>value = self.screen[point]<EOL>if isinstance(value, int):<EOL><INDENT>stream.write(chr(self.base + value).encode(encoding))<EOL><DEDENT>else:<EOL><INDENT>stream.write(self.term.csi('<STR_LIT>'))<EOL>stream.write(self.term.csi_wrap(<EOL>value.encode(encoding),<EOL>'<STR_LIT>'<EOL>))<EOL>if y == zero and self.size.y > <NUM_LIT:1>:<EOL><INDENT>stream.write(self.term.csi('<STR_LIT>'))<EOL><DEDENT>if ramp:<EOL><INDENT>stream.write(ramp[y])<EOL><DEDENT><DEDENT><DEDENT>else:<EOL><INDENT>stream.write(b'<STR_LIT:U+0020>')<EOL><DEDENT><DEDENT>if y == zero and self.size.y > <NUM_LIT:1>:<EOL><INDENT>stream.write(self.term.csi('<STR_LIT>'))<EOL><DEDENT>if ramp:<EOL><INDENT>stream.write(self.term.csi('<STR_LIT>'))<EOL><DEDENT>stream.write(b'<STR_LIT:\n>')<EOL>lines += <NUM_LIT:1><EOL><DEDENT>stream.flush()<EOL>self.cycle = self.cycle + <NUM_LIT:1><EOL>self.lines = lines<EOL>


if np is None:<EOL><INDENT>return self._normalised_python()<EOL><DEDENT>else:<EOL><INDENT>return self._normalised_numpy()<EOL><DEDENT>


dx = (self.screen.width / float(len(self.points)))<EOL>oy = (self.screen.height)<EOL>points = np.array(self.points) - self.minimum<EOL>points = points * <NUM_LIT> / self.extents * self.size.y<EOL>for x, y in enumerate(points):<EOL><INDENT>yield Point((<EOL>dx * x,<EOL>min(oy, oy - y),<EOL>))<EOL><DEDENT>


dx = (self.screen.width / float(len(self.points)))<EOL>oy = (self.screen.height)<EOL>for x, point in enumerate(self.points):<EOL><INDENT>y = (point - self.minimum) * <NUM_LIT> / self.extents * self.size.y<EOL>yield Point((<EOL>dx * x,<EOL>min(oy, oy - y),<EOL>))<EOL><DEDENT>

Normalised data points using pure Python.


return self.size.x<EOL>


if not self.option.axis:<EOL><INDENT>return -<NUM_LIT:1><EOL><DEDENT>else:<EOL><INDENT>return self.screen.height - (<EOL>-self.minimum * <NUM_LIT> / self.extents * self.size.y<EOL>)<EOL><DEDENT>


if not isinstance(point, Point):<EOL><INDENT>point = Point(point)<EOL><DEDENT>rx = self.round(point.x)<EOL>ry = self.round(point.y)<EOL>item = Point((rx >> <NUM_LIT:1>, min(ry >> <NUM_LIT:2>, self.size.y)))<EOL>self.screen[item] |= self.pixels[ry & <NUM_LIT:3>][rx & <NUM_LIT:1>]<EOL>


if not isinstance(point, Point):<EOL><INDENT>point = Point(point)<EOL><DEDENT>x, y = self.round(point.x) >> <NUM_LIT:1>, self.round(point.y) >> <NUM_LIT:2><EOL>if (x, y) not in self.screen:<EOL><INDENT>return<EOL><DEDENT>if isinstance(self.screen[y][x], int):<EOL><INDENT>self.screen[(x, y)] &= ~self.pixels[y & <NUM_LIT:3>][x & <NUM_LIT:1>]<EOL><DEDENT>else:<EOL><INDENT>del self.screen[(x, y)]<EOL><DEDENT>if not self.screen.canvas.get(y):<EOL><INDENT>del self.screen[y]<EOL><DEDENT>


def __init__(self, dg_option=None, ostream=None, data=None):

<EOL>self.dg_option = dg_option<EOL>if self.dg_option == None:<EOL><INDENT>self.dg_option = DOption()<EOL><DEDENT>self.ostream = ostream<EOL>if self.ostream == None:<EOL><INDENT>try:<EOL><INDENT>self.ostream = sys.stdout.buffer<EOL><DEDENT>except AttributeError:<EOL><INDENT>self.ostream = sys.stdout<EOL><DEDENT><DEDENT>if self.dg_option.mode == '<STR_LIT:h>':<EOL><INDENT>self.dg = HorizontalBarGraph(self.dg_option.size,<EOL>self.dg_option)<EOL><DEDENT>elif self.dg_option.mode == '<STR_LIT:v>':<EOL><INDENT>self.dg = VerticalBarGraph(self.dg_option.size,<EOL>self.dg_option)<EOL><DEDENT>else:<EOL><INDENT>self.dg = AxisGraph(self.dg_option.size,<EOL>self.dg_option)<EOL><DEDENT>self.dg.update(data[<NUM_LIT:0>], data[<NUM_LIT:1>])<EOL>

Handle some of the setup functions for the graph in the
        diagram package. Specifically hide all of the requirements that
        are computed in run() inside diagram.py.


self.dg.render(self.ostream)<EOL>


deputies = DeputiesDataset()<EOL>df = deputies.fetch()<EOL>save_to_csv(df, data_dir, "<STR_LIT>")<EOL>holders = df.condition == '<STR_LIT>'<EOL>substitutes = df.condition == '<STR_LIT>'<EOL>log.info("<STR_LIT>", len(df))<EOL>log.info("<STR_LIT>", len(df[holders]))<EOL>log.info("<STR_LIT>", len(df[substitutes]))<EOL>return df<EOL>

:param data_dir: (str) directory in which the output file will be saved


xml = urllib.request.urlopen(self.URL)<EOL>tree = ET.ElementTree(file=xml)<EOL>records = self._parse_deputies(tree.getroot())<EOL>df = pd.DataFrame(records, columns=(<EOL>'<STR_LIT>',<EOL>'<STR_LIT>',<EOL>'<STR_LIT>',<EOL>'<STR_LIT>',<EOL>'<STR_LIT>',<EOL>'<STR_LIT>',<EOL>'<STR_LIT>',<EOL>'<STR_LIT>',<EOL>'<STR_LIT:state>',<EOL>'<STR_LIT>',<EOL>'<STR_LIT>',<EOL>'<STR_LIT:email>'<EOL>))<EOL>return self._translate(df)<EOL>

Fetches the list of deputies for the current term.


def fetch_speeches(data_dir, range_start, range_end):

speeches = SpeechesDataset()<EOL>df = speeches.fetch(range_start, range_end)<EOL>save_to_csv(df, data_dir, "<STR_LIT>")<EOL>return df<EOL>

:param data_dir: (str) directory in which the output file will be saved
:param range_start: (str) date in the format dd/mm/yyyy
:param range_end: (str) date in the format dd/mm/yyyy


def fetch(self, range_start, range_end):

range_dates = {'<STR_LIT>': range_start, '<STR_LIT>': range_end}<EOL>url = self.URL.format(**range_dates)<EOL>xml = urllib.request.urlopen(url)<EOL>tree = ET.ElementTree(file=xml)<EOL>records = self._parse_speeches(tree.getroot())<EOL>return pd.DataFrame(records, columns=[<EOL>'<STR_LIT>',<EOL>'<STR_LIT>',<EOL>'<STR_LIT>',<EOL>'<STR_LIT>',<EOL>'<STR_LIT>',<EOL>'<STR_LIT>',<EOL>'<STR_LIT>',<EOL>'<STR_LIT>',<EOL>'<STR_LIT>',<EOL>'<STR_LIT>',<EOL>'<STR_LIT>',<EOL>'<STR_LIT>'<EOL>])<EOL>

Fetches speeches from the ListarDiscursosPlenario endpoint of the
SessoesReunioes (SessionsReunions) API.

The date range provided should be specified as a string using the
format supported by the API (%d/%m/%Y)


def fetch_session_start_times(data_dir, pivot, session_dates):

session_start_times = SessionStartTimesDataset()<EOL>df = session_start_times.fetch(pivot, session_dates)<EOL>save_to_csv(df, data_dir, "<STR_LIT>")<EOL>log.info("<STR_LIT>", len(session_dates))<EOL>found = pd.to_datetime(df['<STR_LIT:date>'], format="<STR_LIT>").dt.date.unique()<EOL>log.info("<STR_LIT>", len(found))<EOL>return df<EOL>

:param data_dir: (str) directory in which the output file will be saved
:param pivot: (int) congressperson document to use as a pivot for scraping the data
:param session_dates: (list) datetime objects to fetch the start times for


records = self._all_start_times(pivot, session_dates)<EOL>return pd.DataFrame(records, columns=(<EOL>'<STR_LIT:date>',<EOL>'<STR_LIT>',<EOL>'<STR_LIT>'<EOL>))<EOL>

:param pivot: (int) a congressperson document to use as a pivot for scraping the data
:param session_dates: (list) datetime objects to fetch the start times for


def fetch_official_missions(data_dir, start_date, end_date):

official_missions = OfficialMissionsDataset()<EOL>df = official_missions.fetch(start_date, end_date)<EOL>save_to_csv(df, data_dir, "<STR_LIT>")<EOL>return df<EOL>

:param data_dir: (str) directory in which the output file will be saved
:param start_date: (datetime) first date of the range to be scraped
:param end_date: (datetime) last date of the range to be scraped


records = []<EOL>for two_months_range in self._generate_ranges(start_date, end_date):<EOL><INDENT>log.debug(two_months_range)<EOL>for record in self._fetch_missions_for_range(two_months_range[<NUM_LIT:0>], two_months_range[<NUM_LIT:1>]):<EOL><INDENT>records.append(record)<EOL><DEDENT><DEDENT>df = pd.DataFrame(records, columns=[<EOL>'<STR_LIT>',<EOL>'<STR_LIT>',<EOL>'<STR_LIT>',<EOL>'<STR_LIT:start>',<EOL>'<STR_LIT:end>',<EOL>'<STR_LIT>',<EOL>'<STR_LIT>',<EOL>'<STR_LIT>'<EOL>])<EOL>translate_column(df, '<STR_LIT>', {<EOL>'<STR_LIT>': '<STR_LIT>',<EOL>'<STR_LIT>': '<STR_LIT>',<EOL>'<STR_LIT>': '<STR_LIT>',<EOL>'<STR_LIT>': '<STR_LIT>'<EOL>})<EOL>translate_column(df, '<STR_LIT>', {<EOL>'<STR_LIT>': '<STR_LIT>',<EOL>'<STR_LIT>': '<STR_LIT>'<EOL>})<EOL>return df.drop_duplicates()<EOL>

Fetches official missions within the given date range


range_start = start_date<EOL>while range_start < end_date:<EOL><INDENT>range_end = range_start + timedelta(days=<NUM_LIT>)<EOL>yield (<EOL>range_start.strftime("<STR_LIT>"),<EOL>range_end.strftime("<STR_LIT>")<EOL>)<EOL>range_start += timedelta(days=<NUM_LIT:30>)<EOL><DEDENT>

Generate a list of 2 month ranges for the range requested with an
intersection between months. This is necessary because we can't search
for ranges longer than 3 months and the period searched has to encompass
the whole period of the mission.


def fetch_presences(data_dir, deputies, date_start, date_end):

presences = PresencesDataset()<EOL>df = presences.fetch(deputies, date_start, date_end)<EOL>save_to_csv(df, data_dir, "<STR_LIT>")<EOL>log.info("<STR_LIT>", len(df))<EOL>log.info("<STR_LIT>", len(df[df.presence == '<STR_LIT>']))<EOL>log.info("<STR_LIT>", len(df[df.presence == '<STR_LIT>']))<EOL>return df<EOL>

:param data_dir: (str) directory in which the output file will be saved
:param deputies: (pandas.DataFrame) a dataframe with deputies data
:param date_start: (str) a date in the format dd/mm/yyyy
:param date_end: (str) a date in the format dd/mm/yyyy


def fetch(self, deputies, start_date, end_date):

log.debug("<STR_LIT>".format(len(deputies), start_date, end_date))<EOL>records = self._all_presences(deputies, start_date, end_date)<EOL>df = pd.DataFrame(records, columns=(<EOL>'<STR_LIT>',<EOL>'<STR_LIT>',<EOL>'<STR_LIT>',<EOL>'<STR_LIT>',<EOL>'<STR_LIT:state>',<EOL>'<STR_LIT:date>',<EOL>'<STR_LIT>',<EOL>'<STR_LIT>',<EOL>'<STR_LIT>',<EOL>'<STR_LIT>'<EOL>))<EOL>return self._translate(df)<EOL>

:param deputies: (pandas.DataFrame) a dataframe with deputies data
:param date_start: (str) date in the format dd/mm/yyyy
:param date_end: (str) date in the format dd/mm/yyyy


text = node.find(xpath).text<EOL>if text is not None:<EOL><INDENT>text = text.strip()<EOL><DEDENT>return text<EOL>

:param node: the node to be queried
:param xpath: the path to fetch the child node that has the wanted text


def xml_extract_date(node, xpath, date_format='<STR_LIT>'):

return datetime.strptime(xml_extract_text(node, xpath), date_format)<EOL>

:param node: the node to be queried
:param xpath: the path to fetch the child node that has the wanted date


def xml_extract_datetime(node, xpath, datetime_format='<STR_LIT>'):

return datetime.strptime(xml_extract_text(node, xpath), datetime_format)<EOL>

:param node: the node to be queried
:param xpath: the path to fetch the child node that has the wanted datetime


def translate_column(df, column, translations):

df[column] = df[column].astype('<STR_LIT>')<EOL>translations = [translations[cat]<EOL>for cat in df[column].cat.categories]<EOL>df[column].cat.rename_categories(translations, inplace=True)<EOL>

:param df: (pandas.Dataframe) the dataframe to be translated
:param column: (str) the column to be translated
:param translations: (dict) a dictionary of the strings to be categorized and translated


self.obj = obj<EOL>attrs = '<STR_LIT:U+0020>'.join([<EOL>'<STR_LIT>' % (attr_name, attr.resolve(obj))<EOL>if isinstance(attr, Accessor)<EOL>else '<STR_LIT>' % (attr_name, attr)<EOL>for attr_name, attr in self.attrs.items()<EOL>])<EOL>return mark_safe(u'<STR_LIT>' % (attrs, self.text))<EOL>


return str(self.get_days_span(index))<EOL>

Get `colspan` value for <th> tag.
It will render as <th colspan="VALUE"><th>


is_first_month = month_index == <NUM_LIT:0><EOL>is_last_month = month_index == self.__len__() - <NUM_LIT:1><EOL>y = int(self.start_date.year + (self.start_date.month + month_index) / <NUM_LIT>)<EOL>m = int((self.start_date.month + month_index) % <NUM_LIT:12> or <NUM_LIT:12>)<EOL>total = calendar.monthrange(y, m)[<NUM_LIT:1>]<EOL>if is_first_month and is_last_month:<EOL><INDENT>return (self.end_date - self.start_date).days + <NUM_LIT:1><EOL><DEDENT>else:<EOL><INDENT>if is_first_month:<EOL><INDENT>return total - self.start_date.day + <NUM_LIT:1><EOL><DEDENT>elif is_last_month:<EOL><INDENT>return self.end_date.day<EOL><DEDENT>else:<EOL><INDENT>return total<EOL><DEDENT><DEDENT>

Calculate how many days the month spans.


def render_to_json_response(self, context, **response_kwargs):

return HttpResponse(<EOL>self.convert_context_to_json(context),<EOL>content_type='<STR_LIT:application/json>',<EOL>**response_kwargs<EOL>)<EOL>

Returns a JSON response, transforming 'context' to make the payload.


return json.dumps(context, cls=DjangoJSONEncoder)<EOL>

Convert the context dictionary into a JSON object.


sEcho = self.query_data["<STR_LIT>"]<EOL>context = super(BaseListView, self).get_context_data(**kwargs)<EOL>queryset = context["<STR_LIT>"]<EOL>if queryset is not None:<EOL><INDENT>total_length = self.get_queryset_length(queryset)<EOL>queryset = self.filter_queryset(queryset)<EOL>display_length = self.get_queryset_length(queryset)<EOL>queryset = self.sort_queryset(queryset)<EOL>queryset = self.paging_queryset(queryset)<EOL>values_list = self.convert_queryset_to_values_list(queryset)<EOL>context = {<EOL>"<STR_LIT>": sEcho,<EOL>"<STR_LIT>": total_length,<EOL>"<STR_LIT>": display_length,<EOL>"<STR_LIT>": values_list,<EOL>}<EOL><DEDENT>else:<EOL><INDENT>context = {<EOL>"<STR_LIT>": sEcho,<EOL>"<STR_LIT>": <NUM_LIT:0>,<EOL>"<STR_LIT>": <NUM_LIT:0>,<EOL>"<STR_LIT>": [],<EOL>}<EOL><DEDENT>return context<EOL>

Get context data for datatable server-side response.
See http://www.datatables.net/usage/server-side


<EOL>header_rows = []<EOL>headers = [col.header for col in self.columns]<EOL>for header in headers:<EOL><INDENT>if len(header_rows) <= header.row_order:<EOL><INDENT>header_rows.append([])<EOL><DEDENT>header_rows[header.row_order].append(header)<EOL><DEDENT>return header_rows<EOL>


try:<EOL><INDENT>obj = context<EOL>for level in self.levels:<EOL><INDENT>if isinstance(obj, dict):<EOL><INDENT>obj = obj[level]<EOL><DEDENT>elif isinstance(obj, list) or isinstance(obj, tuple):<EOL><INDENT>obj = obj[int(level)]<EOL><DEDENT>else:<EOL><INDENT>if callable(getattr(obj, level)):<EOL><INDENT>try:<EOL><INDENT>obj = getattr(obj, level)()<EOL><DEDENT>except KeyError:<EOL><INDENT>obj = getattr(obj, level)<EOL><DEDENT><DEDENT>else:<EOL><INDENT>display = '<STR_LIT>' % level<EOL>obj = getattr(obj, display)() if hasattr(obj, display) else getattr(obj, level)<EOL><DEDENT><DEDENT>if not obj:<EOL><INDENT>break<EOL><DEDENT><DEDENT>return obj<EOL><DEDENT>except Exception as e:<EOL><INDENT>if quiet:<EOL><INDENT>return '<STR_LIT>'<EOL><DEDENT>else:<EOL><INDENT>raise e<EOL><DEDENT><DEDENT>

Return an object described by the accessor by traversing the attributes
of context.


rendition = image.get_rendition(filter='<STR_LIT>')<EOL>return request.build_absolute_uri(rendition.url)<EOL>

Resize an image for metadata tags, and return an absolute URL to it.


raise NotImplementedError()<EOL>

The full URL to this object, including protocol and domain.


return None<EOL>

Get the image to use for this object.
Can be None if there is no relevant image.


if self.get_meta_image() is not None:<EOL><INDENT>return '<STR_LIT>'<EOL><DEDENT>else:<EOL><INDENT>return '<STR_LIT>'<EOL><DEDENT>

Get the Twitter card type for this object.
See https://dev.twitter.com/cards/types.
Defaults to 'summary_large_image' if the object has an image,
otherwise 'summary'.


def train_encoder(X, y, fold_count, encoder):

kf = StratifiedKFold(n_splits=fold_count, shuffle=True, random_state=<NUM_LIT>)<EOL>encoder = deepcopy(encoder)  <EOL>imputer = SimpleImputer(strategy='<STR_LIT>')<EOL>scaler = StandardScaler()<EOL>folds = []<EOL>fit_encoder_time = <NUM_LIT:0><EOL>score_encoder_time = <NUM_LIT:0><EOL>for train_index, test_index in kf.split(X, y):<EOL><INDENT>X_train, X_test = X.iloc[train_index, :].reset_index(drop=True), X.iloc[test_index, :].reset_index(drop=True)<EOL>y_train, y_test = y[train_index].reset_index(drop=True), y[test_index].reset_index(drop=True)<EOL>start_time = time.time()<EOL>X_train = encoder.fit_transform(X_train, y_train)<EOL>fit_encoder_time += time.time() - start_time<EOL>X_train = imputer.fit_transform(X_train)<EOL>X_train = scaler.fit_transform(X_train)<EOL>start_time = time.time()<EOL>X_test = encoder.transform(X_test)<EOL>score_encoder_time += time.time() - start_time<EOL>X_test = imputer.transform(X_test)<EOL>X_test = scaler.transform(X_test)<EOL>folds.append([X_train, y_train, X_test, y_test])<EOL><DEDENT>return folds, fit_encoder_time/fold_count, score_encoder_time/fold_count<EOL>

Defines folds and performs the data preprocessing (categorical encoding, NaN imputation, normalization)
Returns a list with {X_train, y_train, X_test, y_test}, average fit_encoder_time and average score_encoder_time

Note: We normalize all features (not only numerical features) because otherwise SVM would
    get stuck for hours on ordinal encoded cylinder.bands.arff dataset due to presence of
    unproportionally high values.

Note: The fold count is variable because there are datasets, which have less than 10 samples in the minority class.

Note: We do not use pipelines because of:
    https://github.com/scikit-learn/scikit-learn/issues/11832


scores = []<EOL>fit_model_time = <NUM_LIT:0>      <EOL>score_model_time = <NUM_LIT:0>    <EOL>for X_train, y_train, X_test, y_test in folds:<EOL><INDENT>start_time = time.time()<EOL>with ignore_warnings(category=ConvergenceWarning):  <EOL><INDENT>model.fit(X_train, y_train)<EOL><DEDENT>fit_model_time += time.time() - start_time<EOL>prediction_train_proba = model.predict_proba(X_train)[:, <NUM_LIT:1>]<EOL>prediction_train = (prediction_train_proba >= <NUM_LIT:0.5>).astype('<STR_LIT>')<EOL>start_time = time.time()<EOL>prediction_test_proba = model.predict_proba(X_test)[:, <NUM_LIT:1>]<EOL>score_model_time += time.time() - start_time<EOL>prediction_test = (prediction_test_proba >= <NUM_LIT:0.5>).astype('<STR_LIT>')<EOL>with warnings.catch_warnings():<EOL><INDENT>warnings.simplefilter("<STR_LIT:ignore>")<EOL>scores.append([<EOL>sklearn.metrics.matthews_corrcoef(y_test, prediction_test),<EOL>sklearn.metrics.matthews_corrcoef(y_train, prediction_train),<EOL>sklearn.metrics.roc_auc_score(y_test, prediction_test_proba),<EOL>sklearn.metrics.roc_auc_score(y_train, prediction_train_proba),<EOL>sklearn.metrics.brier_score_loss(y_test, prediction_test_proba),<EOL>sklearn.metrics.brier_score_loss(y_train, prediction_train_proba)<EOL>])<EOL><DEDENT><DEDENT>return np.mean(scores, axis=<NUM_LIT:0>), fit_model_time/len(folds), score_model_time/len(folds)<EOL>

Evaluation with:
  Matthews correlation coefficient: represents thresholding measures
  AUC: represents ranking measures
  Brier score: represents calibration measures


def score_models(clf, X, y, encoder, runs=<NUM_LIT:1>):

scores = []<EOL>X_test = None<EOL>for _ in range(runs):<EOL><INDENT>X_test = encoder().fit_transform(X, y)<EOL>X_test = StandardScaler().fit_transform(X_test)<EOL>scores.append(cross_validate(clf, X_test, y, n_jobs=<NUM_LIT:1>, cv=<NUM_LIT:5>)['<STR_LIT>'])<EOL>gc.collect()<EOL><DEDENT>scores = [y for z in [x for x in scores] for y in z]<EOL>return float(np.mean(scores)), float(np.std(scores)), scores, X_test.shape[<NUM_LIT:1>]<EOL>

Takes in a classifier that supports multiclass classification, and X and a y, and returns a cross validation score.


scores = []<EOL>raw_scores_ds = {}<EOL>X, y, mapping = loader()<EOL>clf = linear_model.LogisticRegression(solver='<STR_LIT>', multi_class='<STR_LIT>', max_iter=<NUM_LIT:200>, random_state=<NUM_LIT:0>)<EOL>encoders = (set(category_encoders.__all__) - {'<STR_LIT>'})  <EOL>for encoder_name in encoders:<EOL><INDENT>encoder = getattr(category_encoders, encoder_name)<EOL>start_time = time.time()<EOL>score, stds, raw_scores, dim = score_models(clf, X, y, encoder)<EOL>scores.append([encoder_name, name, dim, score, stds, time.time() - start_time])<EOL>raw_scores_ds[encoder_name] = raw_scores<EOL>gc.collect()<EOL><DEDENT>results = pd.DataFrame(scores, columns=['<STR_LIT>', '<STR_LIT>', '<STR_LIT>', '<STR_LIT>', '<STR_LIT>', '<STR_LIT>'])<EOL>raw = pd.DataFrame.from_dict(raw_scores_ds)<EOL>ax = raw.plot(kind='<STR_LIT>', return_type='<STR_LIT>')<EOL>plt.title('<STR_LIT>' % (name,))<EOL>plt.ylabel('<STR_LIT>')<EOL>for tick in ax.get_xticklabels():<EOL><INDENT>tick.set_rotation(<NUM_LIT>)<EOL><DEDENT>plt.grid()<EOL>plt.tight_layout()<EOL>plt.show()<EOL>return results, raw<EOL>

Here we iterate through the datasets and score them with a classifier using different encodings.


df = pd.read_csv('<STR_LIT>')<EOL>X = df.reindex(columns=[x for x in df.columns.values if x != '<STR_LIT:class>'])<EOL>y = df.reindex(columns=['<STR_LIT:class>'])<EOL>y = preprocessing.LabelEncoder().fit_transform(y.values.reshape(-<NUM_LIT:1>, ))<EOL>mapping = [<EOL>{'<STR_LIT>': '<STR_LIT>', '<STR_LIT>': [('<STR_LIT>', <NUM_LIT:0>), ('<STR_LIT>', <NUM_LIT:1>), ('<STR_LIT>', <NUM_LIT:2>), ('<STR_LIT>', <NUM_LIT:3>)]},<EOL>{'<STR_LIT>': '<STR_LIT>', '<STR_LIT>': [('<STR_LIT>', <NUM_LIT:0>), ('<STR_LIT>', <NUM_LIT:1>), ('<STR_LIT>', <NUM_LIT:2>), ('<STR_LIT>', <NUM_LIT:3>)]},<EOL>{'<STR_LIT>': '<STR_LIT>', '<STR_LIT>': [('<STR_LIT:2>', <NUM_LIT:0>), ('<STR_LIT:3>', <NUM_LIT:1>), ('<STR_LIT:4>', <NUM_LIT:2>), ('<STR_LIT>', <NUM_LIT:3>)]},<EOL>{'<STR_LIT>': '<STR_LIT>', '<STR_LIT>': [('<STR_LIT:2>', <NUM_LIT:0>), ('<STR_LIT:4>', <NUM_LIT:1>), ('<STR_LIT>', <NUM_LIT:2>)]},<EOL>{'<STR_LIT>': '<STR_LIT>', '<STR_LIT>': [('<STR_LIT>', <NUM_LIT:0>), ('<STR_LIT>', <NUM_LIT:1>), ('<STR_LIT>', <NUM_LIT:2>)]},<EOL>{'<STR_LIT>': '<STR_LIT>', '<STR_LIT>': [('<STR_LIT>', <NUM_LIT:0>), ('<STR_LIT>', <NUM_LIT:1>), ('<STR_LIT>', <NUM_LIT:2>)]},<EOL>]<EOL>return X, y, mapping<EOL>

Load the cars dataset, split it into X and y, and then call the label encoder to get an integer y column.

:return:


df = pd.read_csv('<STR_LIT>')<EOL>X = df.reindex(columns=[x for x in df.columns.values if x != '<STR_LIT:class>'])<EOL>y = df.reindex(columns=['<STR_LIT:class>'])<EOL>y = preprocessing.LabelEncoder().fit_transform(y.values.reshape(-<NUM_LIT:1>, ))<EOL>mapping = None<EOL>return X, y, mapping<EOL>

Load the mushroom dataset, split it into X and y, and then call the label encoder to get an integer y column.

:return:


df = pd.read_csv('<STR_LIT>')<EOL>X = df.reindex(columns=[x for x in df.columns.values if x != '<STR_LIT:class>'])<EOL>X['<STR_LIT>'] = X['<STR_LIT>'].map(lambda x: list(str(x).strip()))<EOL>for idx in range(<NUM_LIT>):<EOL><INDENT>X['<STR_LIT>' % (idx, )] = X['<STR_LIT>'].map(lambda x: x[idx])<EOL><DEDENT>del X['<STR_LIT>']<EOL>y = df.reindex(columns=['<STR_LIT:class>'])<EOL>y = preprocessing.LabelEncoder().fit_transform(y.values.reshape(-<NUM_LIT:1>, ))<EOL>mapping = None<EOL>return X, y, mapping<EOL>


def create_array(n_rows=<NUM_LIT:1000>, extras=False, has_none=True):

ds = [[<EOL>random.random(),<EOL>random.random(),<EOL>random.choice(['<STR_LIT:A>', '<STR_LIT:B>', '<STR_LIT:C>']),<EOL>random.choice(['<STR_LIT:A>', '<STR_LIT:B>', '<STR_LIT:C>', '<STR_LIT:D>']) if extras else random.choice(['<STR_LIT:A>', '<STR_LIT:B>', '<STR_LIT:C>']),<EOL>random.choice(['<STR_LIT:A>', '<STR_LIT:B>', '<STR_LIT:C>', None, np.nan]) if has_none else random.choice(['<STR_LIT:A>', '<STR_LIT:B>', '<STR_LIT:C>']),<EOL>random.choice(['<STR_LIT:A>'])<EOL>] for _ in range(n_rows)]<EOL>return np.array(ds)<EOL>

Creates a numpy dataset with some categorical variables.


def create_dataset(n_rows=<NUM_LIT:1000>, extras=False, has_none=True):

random.seed(<NUM_LIT>)<EOL>ds = [[<EOL>random.random(),                                                                        <EOL>random.choice([float('<STR_LIT>'), float('<STR_LIT>'), float('<STR_LIT>'), -<NUM_LIT:0>, <NUM_LIT:0>, <NUM_LIT:1>, -<NUM_LIT:1>, math.pi]),      <EOL>row,                                                                                <EOL>str(row),                                                                               <EOL>random.choice(['<STR_LIT:A>', '<STR_LIT:B>']) if extras else '<STR_LIT:A>',                                           <EOL>random.choice(['<STR_LIT:A>', '<STR_LIT>', '<STR_LIT>']),                                                   <EOL>random.choice(['<STR_LIT:A>', '<STR_LIT:B>', '<STR_LIT:C>', None]) if has_none else random.choice(['<STR_LIT:A>', '<STR_LIT:B>', '<STR_LIT:C>']),   <EOL>random.choice(['<STR_LIT:A>', '<STR_LIT:B>', '<STR_LIT:C>', '<STR_LIT:D>']) if extras else random.choice(['<STR_LIT:A>', '<STR_LIT:B>', '<STR_LIT:C>']),      <EOL>random.choice([<NUM_LIT:12>, <NUM_LIT>, -<NUM_LIT:32>]),                                                           <EOL>random.choice(['<STR_LIT:A>', '<STR_LIT:B>', '<STR_LIT:C>']),                                                         <EOL>random.choice(['<STR_LIT:A>', '<STR_LIT:B>', '<STR_LIT:C>', np.nan])                                                  <EOL>] for row in range(n_rows)]<EOL>df = pd.DataFrame(ds, columns=['<STR_LIT:float>', '<STR_LIT>', '<STR_LIT>', '<STR_LIT>', '<STR_LIT>', '<STR_LIT>', '<STR_LIT:none>', '<STR_LIT>', <NUM_LIT>, '<STR_LIT>', '<STR_LIT>'])<EOL>df['<STR_LIT>'] = pd.Categorical(df['<STR_LIT>'], categories=['<STR_LIT:A>', '<STR_LIT:B>', '<STR_LIT:C>'])<EOL>df['<STR_LIT>'] = pd.Categorical(df['<STR_LIT>'], categories=['<STR_LIT:A>', '<STR_LIT:B>', '<STR_LIT:C>'])<EOL>return df<EOL>

Creates a dataset with some categorical variables.


assert x.equals(x_inv)<EOL>

Verify x is equal to x_inv. The test returns true for NaN.equals(NaN) as it should.


return [[round(val, ndigits) for val in sublst] for sublst in A]<EOL>

Rounds numbers in a list of lists. Useful for approximate equality testing.


<EOL>X = util.convert_input(X)<EOL>self._dim = X.shape[<NUM_LIT:1>]<EOL>if self.cols is None:<EOL><INDENT>self.cols = util.get_obj_cols(X)<EOL><DEDENT>else:<EOL><INDENT>self.cols = util.convert_cols_to_list(self.cols)<EOL><DEDENT>if self.handle_missing == '<STR_LIT:error>':<EOL><INDENT>if X[self.cols].isnull().any().bool():<EOL><INDENT>raise ValueError('<STR_LIT>')<EOL><DEDENT><DEDENT>_, categories = self.ordinal_encoding(<EOL>X,<EOL>mapping=self.mapping,<EOL>cols=self.cols,<EOL>handle_unknown=self.handle_unknown,<EOL>handle_missing=self.handle_missing<EOL>)<EOL>self.mapping = categories<EOL>X_temp = self.transform(X, override_return_df=True)<EOL>self.feature_names = X_temp.columns.tolist()<EOL>if self.drop_invariant:<EOL><INDENT>self.drop_cols = []<EOL>generated_cols = util.get_generated_cols(X, X_temp, self.cols)<EOL>self.drop_cols = [x for x in generated_cols if X_temp[x].var() <= <NUM_LIT>]<EOL>try:<EOL><INDENT>[self.feature_names.remove(x) for x in self.drop_cols]<EOL><DEDENT>except KeyError as e:<EOL><INDENT>if self.verbose > <NUM_LIT:0>:<EOL><INDENT>print("<STR_LIT>"<EOL>"<STR_LIT>".format(e))<EOL><DEDENT><DEDENT><DEDENT>return self<EOL>

Fit encoder according to X and y.

        Parameters
        ----------

        X : array-like, shape = [n_samples, n_features]
            Training vectors, where n_samples is the number of samples
            and n_features is the number of features.
        y : array-like, shape = [n_samples]
            Target values.

        Returns
        -------

        self : encoder
            Returns self.


def transform(self, X, override_return_df=False):

if self.handle_missing == '<STR_LIT:error>':<EOL><INDENT>if X[self.cols].isnull().any().bool():<EOL><INDENT>raise ValueError('<STR_LIT>')<EOL><DEDENT><DEDENT>if self._dim is None:<EOL><INDENT>raise ValueError(<EOL>'<STR_LIT>')<EOL><DEDENT>X = util.convert_input(X)<EOL>if X.shape[<NUM_LIT:1>] != self._dim:<EOL><INDENT>raise ValueError('<STR_LIT>' % (X.shape[<NUM_LIT:1>], self._dim,))<EOL><DEDENT>if not self.cols:<EOL><INDENT>return X if self.return_df else X.values<EOL><DEDENT>X, _ = self.ordinal_encoding(<EOL>X,<EOL>mapping=self.mapping,<EOL>cols=self.cols,<EOL>handle_unknown=self.handle_unknown,<EOL>handle_missing=self.handle_missing<EOL>)<EOL>if self.drop_invariant:<EOL><INDENT>for col in self.drop_cols:<EOL><INDENT>X.drop(col, <NUM_LIT:1>, inplace=True)<EOL><DEDENT><DEDENT>if self.return_df or override_return_df:<EOL><INDENT>return X<EOL><DEDENT>else:<EOL><INDENT>return X.values<EOL><DEDENT>

Perform the transformation to new categorical data.

        Will use the mapping (if available) and the column list (if available, otherwise every column) to encode the
        data ordinarily.

        Parameters
        ----------

        X : array-like, shape = [n_samples, n_features]

        Returns
        -------

        p : array, shape = [n_samples, n_numeric + N]
            Transformed values with encoding applied.


X = X_in.copy(deep=True)<EOL>X = util.convert_input(X)<EOL>if self._dim is None:<EOL><INDENT>raise ValueError(<EOL>'<STR_LIT>')<EOL><DEDENT>if X.shape[<NUM_LIT:1>] != self._dim:<EOL><INDENT>if self.drop_invariant:<EOL><INDENT>raise ValueError("<STR_LIT>"<EOL>"<STR_LIT>" % (X.shape[<NUM_LIT:1>],))<EOL><DEDENT>else:<EOL><INDENT>raise ValueError('<STR_LIT>' % (X.shape[<NUM_LIT:1>], self._dim,))<EOL><DEDENT><DEDENT>if not self.cols:<EOL><INDENT>return X if self.return_df else X.values<EOL><DEDENT>if self.handle_unknown == '<STR_LIT:value>':<EOL><INDENT>for col in self.cols:<EOL><INDENT>if any(X[col] == -<NUM_LIT:1>):<EOL><INDENT>warnings.warn("<STR_LIT>"<EOL>"<STR_LIT>" % (col,))<EOL><DEDENT><DEDENT><DEDENT>if self.handle_unknown == '<STR_LIT>' and self.handle_missing == '<STR_LIT>':<EOL><INDENT>for col in self.cols:<EOL><INDENT>if X[col].isnull().any():<EOL><INDENT>warnings.warn("<STR_LIT>"<EOL>"<STR_LIT>" % (col,))<EOL><DEDENT><DEDENT><DEDENT>for switch in self.mapping:<EOL><INDENT>column_mapping = switch.get('<STR_LIT>')<EOL>inverse = pd.Series(data=column_mapping.index, index=column_mapping.get_values())<EOL>X[switch.get('<STR_LIT>')] = X[switch.get('<STR_LIT>')].map(inverse).astype(switch.get('<STR_LIT>'))<EOL><DEDENT>return X if self.return_df else X.values<EOL>

Perform the inverse transformation to encoded data. Will attempt best case reconstruction, which means
it will return nan for handle_missing and handle_unknown settings that break the bijection. We issue
warnings when some of those cases occur.

Parameters
----------
X_in : array-like, shape = [n_samples, n_features]

Returns
-------
p: array, the same size of X_in


@staticmethod<EOL><INDENT>def ordinal_encoding(X_in, mapping=None, cols=None, handle_unknown='<STR_LIT:value>', handle_missing='<STR_LIT:value>'):<DEDENT>

return_nan_series = pd.Series(data=[np.nan], index=[-<NUM_LIT:2>])<EOL>X = X_in.copy(deep=True)<EOL>if cols is None:<EOL><INDENT>cols = X.columns.values<EOL><DEDENT>if mapping is not None:<EOL><INDENT>mapping_out = mapping<EOL>for switch in mapping:<EOL><INDENT>column = switch.get('<STR_LIT>')<EOL>X[column] = X[column].map(switch['<STR_LIT>'])<EOL>try:<EOL><INDENT>X[column] = X[column].astype(int)<EOL><DEDENT>except ValueError as e:<EOL><INDENT>X[column] = X[column].astype(float)<EOL><DEDENT>if handle_unknown == '<STR_LIT:value>':<EOL><INDENT>X[column].fillna(-<NUM_LIT:1>, inplace=True)<EOL><DEDENT>elif handle_unknown == '<STR_LIT:error>':<EOL><INDENT>missing = X[column].isnull()<EOL>if any(missing):<EOL><INDENT>raise ValueError('<STR_LIT>' % column)<EOL><DEDENT><DEDENT>if handle_missing == '<STR_LIT>':<EOL><INDENT>X[column] = X[column].map(return_nan_series).where(X[column] == -<NUM_LIT:2>, X[column])<EOL><DEDENT><DEDENT><DEDENT>else:<EOL><INDENT>mapping_out = []<EOL>for col in cols:<EOL><INDENT>nan_identity = np.nan<EOL>if util.is_category(X[col].dtype):<EOL><INDENT>categories = X[col].cat.categories<EOL><DEDENT>else:<EOL><INDENT>categories = X[col].unique()<EOL><DEDENT>index = pd.Series(categories).fillna(nan_identity).unique()<EOL>data = pd.Series(index=index, data=range(<NUM_LIT:1>, len(index) + <NUM_LIT:1>))<EOL>if handle_missing == '<STR_LIT:value>' and ~data.index.isnull().any():<EOL><INDENT>data.loc[nan_identity] = -<NUM_LIT:2><EOL><DEDENT>elif handle_missing == '<STR_LIT>':<EOL><INDENT>data.loc[nan_identity] = -<NUM_LIT:2><EOL><DEDENT>mapping_out.append({'<STR_LIT>': col, '<STR_LIT>': data, '<STR_LIT>': X[col].dtype}, )<EOL><DEDENT><DEDENT>return X, mapping_out<EOL>

Ordinal encoding uses a single column of integers to represent the classes. An optional mapping dict can be passed
in, in this case we use the knowledge that there is some true order to the classes themselves. Otherwise, the classes
are assumed to have no true order and integers are selected at random.


if not isinstance(self.feature_names, list):<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT>else:<EOL><INDENT>return self.feature_names<EOL><DEDENT>

Returns the names of all transformed / added columns.

Returns
-------
feature_names: list
    A list with all feature names transformed or added.
    Note: potentially dropped features are not included!


<EOL>X = util.convert_input(X)<EOL>y = util.convert_input_vector(y, X.index)<EOL>if X.shape[<NUM_LIT:0>] != y.shape[<NUM_LIT:0>]:<EOL><INDENT>raise ValueError("<STR_LIT>" + str(X.shape[<NUM_LIT:0>]) + "<STR_LIT>" + str(y.shape[<NUM_LIT:0>]) + "<STR_LIT:.>")<EOL><DEDENT>self._dim = X.shape[<NUM_LIT:1>]<EOL>if self.cols is None:<EOL><INDENT>self.cols = util.get_obj_cols(X)<EOL><DEDENT>else:<EOL><INDENT>self.cols = util.convert_cols_to_list(self.cols)<EOL><DEDENT>if self.handle_missing == '<STR_LIT:error>':<EOL><INDENT>if X[self.cols].isnull().any().bool():<EOL><INDENT>raise ValueError('<STR_LIT>')<EOL><DEDENT><DEDENT>self.ordinal_encoder = OrdinalEncoder(<EOL>verbose=self.verbose,<EOL>cols=self.cols,<EOL>handle_unknown='<STR_LIT:value>',<EOL>handle_missing='<STR_LIT:value>'<EOL>)<EOL>self.ordinal_encoder = self.ordinal_encoder.fit(X)<EOL>X_ordinal = self.ordinal_encoder.transform(X)<EOL>self.mapping = self.fit_target_encoding(X_ordinal, y)<EOL>X_temp = self.transform(X, override_return_df=True)<EOL>self.feature_names = list(X_temp.columns)<EOL>if self.drop_invariant:<EOL><INDENT>self.drop_cols = []<EOL>X_temp = self.transform(X)<EOL>generated_cols = util.get_generated_cols(X, X_temp, self.cols)<EOL>self.drop_cols = [x for x in generated_cols if X_temp[x].var() <= <NUM_LIT>]<EOL>try:<EOL><INDENT>[self.feature_names.remove(x) for x in self.drop_cols]<EOL><DEDENT>except KeyError as e:<EOL><INDENT>if self.verbose > <NUM_LIT:0>:<EOL><INDENT>print("<STR_LIT>"<EOL>"<STR_LIT>".format(e))<EOL><DEDENT><DEDENT><DEDENT>return self<EOL>

Fit encoder according to X and y.

        Parameters
        ----------
        X : array-like, shape = [n_samples, n_features]
            Training vectors, where n_samples is the number of samples
            and n_features is the number of features.
        y : array-like, shape = [n_samples]
            Target values.

        Returns
        -------
        self : encoder
            Returns self.


def transform(self, X, y=None, override_return_df=False):

if self.handle_missing == '<STR_LIT:error>':<EOL><INDENT>if X[self.cols].isnull().any().bool():<EOL><INDENT>raise ValueError('<STR_LIT>')<EOL><DEDENT><DEDENT>if self._dim is None:<EOL><INDENT>raise ValueError('<STR_LIT>')<EOL><DEDENT>X = util.convert_input(X)<EOL>if X.shape[<NUM_LIT:1>] != self._dim:<EOL><INDENT>raise ValueError('<STR_LIT>' % (X.shape[<NUM_LIT:1>], self._dim,))<EOL><DEDENT>if y is not None:<EOL><INDENT>y = util.convert_input_vector(y, X.index)<EOL>if X.shape[<NUM_LIT:0>] != y.shape[<NUM_LIT:0>]:<EOL><INDENT>raise ValueError("<STR_LIT>" + str(X.shape[<NUM_LIT:0>]) + "<STR_LIT>" + str(y.shape[<NUM_LIT:0>]) + "<STR_LIT:.>")<EOL><DEDENT><DEDENT>if not self.cols:<EOL><INDENT>return X<EOL><DEDENT>X = self.ordinal_encoder.transform(X)<EOL>if self.handle_unknown == '<STR_LIT:error>':<EOL><INDENT>if X[self.cols].isin([-<NUM_LIT:1>]).any().any():<EOL><INDENT>raise ValueError('<STR_LIT>')<EOL><DEDENT><DEDENT>X = self.target_encode(X)<EOL>if self.drop_invariant:<EOL><INDENT>for col in self.drop_cols:<EOL><INDENT>X.drop(col, <NUM_LIT:1>, inplace=True)<EOL><DEDENT><DEDENT>if self.return_df or override_return_df:<EOL><INDENT>return X<EOL><DEDENT>else:<EOL><INDENT>return X.values<EOL><DEDENT>

Perform the transformation to new categorical data.

        Parameters
        ----------
        X : array-like, shape = [n_samples, n_features]
        y : array-like, shape = [n_samples] when transform by leave one out
            None, when transform without target info (such as transform test set)

        Returns
        -------
        p : array, shape = [n_samples, n_numeric + N]
            Transformed values with encoding applied.


def fit_transform(self, X, y=None, **fit_params):

return self.fit(X, y, **fit_params).transform(X, y)<EOL>

Encoders that utilize the target must make sure that the training data are transformed with:
     transform(X, y)
and not with:
    transform(X)