def test_to_image():
    from PIL.Image import Image

    print(
        type(
            Spectrogram(np.zeros((5, 10)), np.linspace(0, 100, 5),
                        np.linspace(0, 10, 10)).to_image()))
    assert isinstance(
        Spectrogram(np.zeros((5, 10)), np.linspace(0, 100, 5),
                    np.linspace(0, 10, 10)).to_image(),
        Image,
    )
Exemplo n.º 2
0
def pulse_finder_file(file,
                      freq_range,
                      pulse_rate_range,
                      window_len,
                      rejection_bands=None,
                      plot=False):
    """a wrapper for pulse_finder with takes an audio file path as an argument
    
    creates the audio object and spectrogram within the function
    
    Args:
        file: path to an audio file
        freq_range: range to bandpass the spectrogram, in Hz
        pulse_rate_range: how many pulses per second? (where to look in the fft of the smoothed-amplitude), in Hz
        rejection_bands: list of frequency bands to subtract from the desired freq_range
        plot=False : if True, plot figures
    
    Returns:
        array of pulse_score: pulse score (float) for each time window
        array of time: start time of each window
    
    """
    # make spectrogram from file path
    audio = Audio(file)
    spec = Spectrogram.from_audio(audio)

    pulse_scores, window_start_times = pulse_finder(spec, freq_range,
                                                    pulse_rate_range,
                                                    window_len,
                                                    rejection_bands, plot)

    return pulse_scores, window_start_times
Exemplo n.º 3
0
def test_bandpass_spectrogram_bad_limits():
    with pytest.raises(ValueError):
        Spectrogram(
            np.zeros((5, 10)),
            np.linspace(0, 100, 5),
            np.linspace(0, 10, 10),
            (-100, -20),
        ).bandpass(4, 2)
Exemplo n.º 4
0
def test_spectrogram_shape_of_veryshort(veryshort_wav_str):
    audio = Audio.from_file(veryshort_wav_str, sample_rate=22050)
    spec = Spectrogram.from_audio(audio, overlap_samples=384)
    assert spec.spectrogram.shape == (257, 21)
    assert spec.frequencies.shape == (257, )
    assert spec.times.shape == (21, )
    assert isclose(spec.window_length(), 0.02321995465, abs_tol=1e-4)
    assert isclose(spec.window_step(), 0.005804988662, abs_tol=1e-4)
    assert isclose(spec.duration(), audio.duration(), abs_tol=1e-2)
    assert isclose(spec.window_start_times()[0], 0, abs_tol=1e-4)
Exemplo n.º 5
0
def test_ribbit():
    path = "./tests/audio/silence_10s.mp3"
    audio = Audio.from_file(path, sample_rate=22050)
    spec = Spectrogram.from_audio(audio)

    scores, times = ribbit.ribbit(
        spec,
        pulse_rate_range=[5, 10],
        signal_band=[1000, 2000],
        window_len=5.0,
        noise_bands=[[0, 200]],
        plot=True,
    )
    assert len(scores) > 0
def test_pulse_finder():
    path = "./tests/silence_10s.mp3"
    audio = Audio(path)
    spec = Spectrogram.from_audio(audio)

    scores, times = pulse_finder(
        spec,
        pulse_rate_range=[5, 10],
        freq_range=[1000, 2000],
        window_len=5.0,
        rejection_bands=[[0, 200]],
        plot=True,
    )
    assert len(scores) > 0
Exemplo n.º 7
0
def test_summarize_top_scores(gpt_path):
    df = pd.DataFrame(columns=[
        "species",
        "pulse_rate_low",
        "pulse_rate_high",
        "low_f",
        "high_f",
        "reject_low",
        "reject_high",
        "window_length",
    ])
    df.at[0, :] = ["sp1", 5, 10, 1000, 2000, 0, 500, 1.0]
    df.at[1, :] = ["sp2", 10, 15, 1000, 2000, 0, 500, 1.0]
    audio = Audio.from_file(gpt_path, sample_rate=32000)
    spec = Spectrogram.from_audio(audio, overlap_samples=256)
    df = ribbit.pulse_finder_species_set(spec, df)

    ribbit.summarize_top_scores(["1", "2"], [df, df], scale_factor=10.0)
Exemplo n.º 8
0
def test_ribbit_short_audio(veryshort_wav_str):
    audio = Audio.from_file(veryshort_wav_str, sample_rate=22050)
    spec = Spectrogram.from_audio(audio,
                                  window_samples=512,
                                  overlap_samples=256,
                                  decibel_limits=(-100, -20))

    df = ribbit.ribbit(
        spec,
        pulse_rate_range=[5, 10],
        signal_band=[1000, 2000],
        clip_duration=5.0,
        clip_overlap=2.5,
        final_clip=None,
        noise_bands=[[0, 200]],
        plot=False,
    )
    assert len(df) == 0
Exemplo n.º 9
0
def test_pulsefinder_species_set(gpt_path):
    df = pd.DataFrame(columns=[
        "species",
        "pulse_rate_low",
        "pulse_rate_high",
        "low_f",
        "high_f",
        "reject_low",
        "reject_high",
        "window_length",
    ])
    df.at[0, :] = ["sp1", 5, 10, 1000, 2000, 0, 500, 1.0]
    df.at[1, :] = ["sp2", 10, 15, 1000, 2000, 0, 500, 1.0]

    audio = Audio.from_file(gpt_path, sample_rate=32000)
    spec = Spectrogram.from_audio(audio, overlap_samples=256)

    df = ribbit.pulse_finder_species_set(spec, df)

    assert type(df) == pd.DataFrame
Exemplo n.º 10
0
def test_ribbit_high_spec_overlap(gpt_path):
    """spec params should not effect number of clips in results"""
    audio = Audio.from_file(gpt_path, sample_rate=22050).trim(0, 16)
    spec = Spectrogram.from_audio(audio,
                                  window_samples=512,
                                  overlap_samples=500,
                                  decibel_limits=(-100, -20))

    df = ribbit.ribbit(
        spec,
        pulse_rate_range=[5, 10],
        signal_band=[1000, 2000],
        clip_duration=5.0,
        clip_overlap=0,
        final_clip=None,
        noise_bands=[[0, 200]],
        plot=False,
    )
    assert len(df) == 3
    assert isclose(max(df["start_time"]), 10.0, abs_tol=1e-4)
Exemplo n.º 11
0
def test_ribbit(gpt_path):
    audio = Audio.from_file(gpt_path, sample_rate=22050).trim(0, 16)

    spec = Spectrogram.from_audio(audio,
                                  window_samples=512,
                                  overlap_samples=256,
                                  decibel_limits=(-100, -20))

    df = ribbit.ribbit(
        spec,
        pulse_rate_range=[5, 10],
        signal_band=[1000, 2000],
        clip_duration=5.0,
        clip_overlap=0,
        final_clip=None,
        noise_bands=[[0, 200]],
        plot=False,
    )

    assert len(df) == 3
    assert isclose(max(df["score"]), 0.0392323, abs_tol=1e-4)
Exemplo n.º 12
0
def test_pulsefinder_species_set():
    path = "./tests/great_plains_toad.wav"
    df = pd.DataFrame(columns=[
        "species",
        "pulse_rate_low",
        "pulse_rate_high",
        "low_f",
        "high_f",
        "reject_low",
        "reject_high",
        "window_length",
    ])
    df.at[0, :] = ["sp1", 5, 10, 1000, 2000, 0, 500, 1.0]
    df.at[1, :] = ["sp2", 10, 15, 1000, 2000, 0, 500, 1.0]

    audio = Audio(path, sample_rate=32000)
    spec = Spectrogram.from_audio(audio, overlap_samples=256)

    df = pulse_finder_species_set(spec, df)

    assert type(df) == pd.DataFrame
Exemplo n.º 13
0
def test_spectrogram_raises_typeerror():
    with pytest.raises(TypeError):
        Spectrogram.from_audio("not samples")
Exemplo n.º 14
0
def test_construct_spectrogram_decibel_limits_incorrect_dimensions_raises():
    with pytest.raises(TypeError):
        Spectrogram(np.zeros((5, 10)), np.zeros((5)), np.zeros((10)), (-100))
Exemplo n.º 15
0
def test_construct_spectrogram_no_decibel_limits_raises():
    with pytest.raises(TypeError):
        Spectrogram(np.zeros((5, 10)), np.zeros((5)), np.zeros((10)))
Exemplo n.º 16
0
def test_construct_spectrogram_dimensions_mismatch_raises_one():
    with pytest.raises(TypeError):
        Spectrogram(np.zeros((5, 10)), np.zeros((5)), np.zeros((7)),
                    (-100, -20))