<commit_before>import unittest2
import numpy as np
from duration import Seconds
from samplerate import SR44100, SR11025, SampleRate, Stride
from zounds.timeseries import TimeDimension, TimeSlice
from zounds.core import IdentityDimension
from audiosamples import AudioSamples
from zounds.synthesize import SineSynthesizer


class AudioSamplesTest(unittest2.TestCase):

    def test_sliding_window(self):
        samples = AudioSamples.silence(SR11025(), Seconds(30))
        sr = samples.samplerate * Stride(frequency=16, duration=512)
        windowed = samples.sliding_window(sr)
        self.assertEqual((512,), windowed.shape[1:])
        long_sr = SampleRate(
            frequency=sr.frequency * 2, duration=sr.frequency * 32)
        frequency = TimeSlice(duration=long_sr.frequency)
        duration = TimeSlice(duration=long_sr.duration)
        _, long_windowed = windowed.sliding_window_with_leftovers(
            windowsize=duration, stepsize=frequency, dopad=True)
        self.assertEqual((32, 512), long_windowed.shape[1:])
        self.assertEqual(3, long_windowed.ndim)

    def test_sliding_window_dimensions(self):
        samplerate = SR11025()
        samples = AudioSamples.silence(SR11025(), Seconds(10))
        window_sr = SampleRate(
            duration=samplerate.frequency * 512,
            frequency=samplerate.frequency * 512 - 25)
        print window_sr
        windowed = samples.sliding_window(window_sr)
        self.assertEqual(window_sr, windowed.dimensions[0].samplerate)

    def test_expanding_first_dimension_should_create_identity_dimension(self):
        silence = AudioSamples.silence(SR11025(), Seconds(10))
        expanded = silence[None, ...]
        print expanded.dimensions
        self.assertEqual(2, len(expanded.dimensions))
        self.assertIsInstance(expanded.dimensions[0], IdentityDimension)
        self.assertEqual(silence.dimensions[0], expanded.dimensions[1])

    def test_time_slice_should_return_audio_samples(self):
        silence = AudioSamples.silence(SR11025(), Seconds(10))
        ts = TimeSlice(duration=Seconds(1))
        sliced = silence[ts]
        self.assertIsInstance(sliced, AudioSamples)
        self.assertEqual(int(SR11025()), len(sliced))
        self.assertEqual(SR11025(), sliced.samplerate)

    def test_integer_slice_should_return_scalar(self):
        silence = AudioSamples.silence(SR11025(), Seconds(10))
        sliced = silence[10]
        self.assertIsInstance(sliced, np.float32)

    def test_stereo(self):<commit_msg>Fix order of operations issue in sliding window test<commit_after>import unittest2
import numpy as np
from duration import Seconds
from samplerate import SR44100, SR11025, SampleRate, Stride
from zounds.timeseries import TimeDimension, TimeSlice
from zounds.core import IdentityDimension
from audiosamples import AudioSamples
from zounds.synthesize import SineSynthesizer


class AudioSamplesTest(unittest2.TestCase):

    def test_sliding_window(self):
        samples = AudioSamples.silence(SR11025(), Seconds(30))
        sr = samples.samplerate * Stride(frequency=16, duration=512)
        windowed = samples.sliding_window(sr)
        self.assertEqual((512,), windowed.shape[1:])
        long_sr = SampleRate(
            frequency=sr.frequency * 2, duration=sr.frequency * 32)
        frequency = TimeSlice(duration=long_sr.frequency)
        duration = TimeSlice(duration=long_sr.duration)
        _, long_windowed = windowed.sliding_window_with_leftovers(
            windowsize=duration, stepsize=frequency, dopad=True)
        self.assertEqual((32, 512), long_windowed.shape[1:])
        self.assertEqual(3, long_windowed.ndim)

    def test_sliding_window_dimensions(self):
        samplerate = SR11025()
        samples = AudioSamples.silence(SR11025(), Seconds(10))
        window_sr = SampleRate(
            duration=samplerate.frequency * 512,
            frequency=samplerate.frequency * (512 - 25))
        windowed = samples.sliding_window(window_sr)
        self.assertEqual(window_sr, windowed.dimensions[0].samplerate)

    def test_expanding_first_dimension_should_create_identity_dimension(self):
        silence = AudioSamples.silence(SR11025(), Seconds(10))
        expanded = silence[None, ...]
        self.assertEqual(2, len(expanded.dimensions))
        self.assertIsInstance(expanded.dimensions[0], IdentityDimension)
        self.assertEqual(silence.dimensions[0], expanded.dimensions[1])

    def test_time_slice_should_return_audio_samples(self):
        silence = AudioSamples.silence(SR11025(), Seconds(10))
        ts = TimeSlice(duration=Seconds(1))
        sliced = silence[ts]
        self.assertIsInstance(sliced, AudioSamples)
        self.assertEqual(int(SR11025()), len(sliced))
        self.assertEqual(SR11025(), sliced.samplerate)

    def test_integer_slice_should_return_scalar(self):
        silence = AudioSamples.silence(SR11025(), Seconds(10))
        sliced = silence[10]
        self.assertIsInstance(sliced, np.float32)

    def test_stereo(self):
{'fraction_non_alphanumeric': 0.09655584312164045, 'fraction_numerical': 0.03941867409914394, 'mean_word_length': 3.803059273422562, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1674476', 'n_tokens_mistral': 1620, 'n_tokens_neox': 1476, 'n_words': 263}