<commit_before>import numpy as np
import scipy.io.wavfile
import copy
import mir3.modules.tool.wav2spectrogram as wav2spec
import mir3.data.spectrogram as spec
import mir3.data.feature_track as track
import matplotlib.pyplot as plt

import mir3.modules.features.flatness as feat_flat
import mir3.modules.features.energy as feat_energy
import mir3.modules.features.flux as feat_flux
import mir3.modules.features.centroid as feat_centroid
import mir3.modules.features.rolloff as feat_rolloff
import mir3.modules.features.low_energy as feat_lowenergy
import mir3.lib.mir.mfcc as mfcc
import mir3.lib.mir.tdom_features as tdomf

import mir3.modules.features.join as feat_join
import mir3.modules.features.stats as feat_stats


class FrequencyBand:
    def bands(self):
        pass


class LinearBand(FrequencyBand):

    def __init__(self, low=0, high=22050, step=1000, nbands=None):
        self.low = low
        self.high = high
        self.step = step
        self.i = low
        self.nbands = nbands

    def bands(self):
        if self.nbands is not None:
            len = int(self.high / float(self.nbands))
            bands = []
            for i in range(self.nbands-1):
                bands.append((i*len, (i * len) + (len-1)))
            i+=1
            bands.append((i*len, self.high))
            for i in bands:
                yield i
        else:
            for i in range(self.low, self.high, self.step):
                end = i + self.step
                if end > self.high:
                    end = i + (self.step - (end - self.high))
                yield (i, end)


class OneBand(FrequencyBand):

    def __init__(self, low=0, high=22050):
        self.low = low
        self.high = high

    def bands(self):
        yield (self.low, self.high)


def mel_to_hz(mel):
    return 700 * (10**(float(mel) / 2595)-1)


def hz_to_mel(hz):
    return 2595 * np.log10(1 + (float(hz) / 700))


class MelBand(FrequencyBand):

    def __init__(self, low=0, high=22050, step = 300, nbands=None):
        self.low = low
        self.high = high
        self.step = step
        self.low_mel = hz_to_mel(low)
        self.high_mel = hz_to_mel(high)
        self.nbands = nbands

        #print "low hz = %d, low mel = %d, high hz = %d, high mel = %d" % (self.low, self.low_mel, self.high, self.high_mel)


    def bands(self):

        if self.nbands is not None:
            len = int(self.high_mel / float(self.nbands))
            bands = []
            for i in range(self.nbands-1):
                bands.append((i*len, (i * len) + (len-1)))
            i+=1
            bands.append((i*len, self.high_mel))
            for i in bands:
                yield (int(np.floor(mel_to_hz(i[0]))), int(np.floor(mel_to_hz(i[1]))))
        else:
            for i in range(np.int(self.low_mel), np.int(self.high_mel), self.step):
                beg = int(np.floor(mel_to_hz(i)))
                end = int(np.floor(mel_to_hz(i + self.step)))-1
                if end > self.high:
                    end = self.high
                yield (beg, end)



class BandwiseFeatures:
    """
    :type infile: string
    :type dft_len: int
    :type window: string
    :type window_len: int
    :type window_step: int
    :type db_spec: boolean
    :type band_features: np.ndarray
    :type spectrogram mir3.data.Spectrogram
    """

    def __init__(self, infile, dft_len=2048, window='hanning', window_len=2048, window_step=1024, db_spec = True):
        self.infile = infile
        self.dft_len = dft_len
        self.window_len = window_len
        self.window = window
        self.window_step = window_step
        self.db_spec = db_spec
        self.features_per_band = 0


        #load the auio file and compute its spectrum
        audio_file = open(infile, 'rb')
        self.spectrogram = wav2spec.Wav2Spectrogram().convert(audio_file, dft_length=dft_len,\
                                                              window_step=window_step,\
                                                              window_length=window_len)

        #keeping the time-domain data for computing time-domain features
        rate, audio_data = scipy.io.wavfile.read(infile)
        audio_data = audio_data.astype(np.float)
        audio_data /= np.max(np.abs(audio_data)) # Normalization to -1/+1 range
        self.audio_data = np.copy(audio_data)
        self.samplingrate = rate
        audio_file.close()

        if db_spec:
            self.spectrogram.data = 20 * np.log10(self.spectrogram.data + np.finfo(np.float).eps)

        self.band_features = np.array([])
        self.joined_features = None
        self.cropped = None

    def spec_to_db(self):
        self.spectrogram.data = 20 * np.log10(self.spectrogram.data + np.finfo(np.float).eps)

    def calculate_features_per_band(self, frequency_band, discard_bin_zero=False):
        """
        :param frequency_band: FrequencyBand
        :return: list[FeatureTrack]
        """

        flatness = feat_flat.Flatness()
        energy = feat_energy.Energy()
        flux = feat_flux.Flux()
        centroid = feat_centroid.Centroid()
        rolloff = feat_rolloff.Rolloff()
        lowenergy = feat_lowenergy.LowEnergy()

        for b in frequency_band.bands():
            lowbin = self.spectrogram.freq_bin(b[0])
            if lowbin == 0:
                if discard_bin_zero:
                    lowbin = 1
            highbin = self.spectrogram.freq_bin(b[1])
            print "calculating features for band in bin range: ", lowbin, highbin

            features = []

            flatness_feature = flatness.calc_track_band(self.spectrogram, lowbin, highbin)
            flatness_feature.metadata.feature += ("_" + str(b[0]))
            features.append(flatness_feature)

            energy_feature = energy.calc_track_band(self.spectrogram, lowbin, highbin)
            energy_feature.metadata.feature += ("_" + str(b[0]))
            features.append(energy_feature)

            flux_feature = flux.calc_track_band(self.spectrogram, lowbin, highbin)
            flux_feature.metadata.feature += ("_" + str(b[0]))
            features.append(flux_feature)

            centroid_feature = centroid.calc_track_band(self.spectrogram, lowbin, highbin)
            centroid_feature.metadata.feature += ("_" + str(b[0]))
            features.append(centroid_feature)<commit_msg>Removed some prints.<commit_after>import numpy as np
import scipy.io.wavfile
import copy
import mir3.modules.tool.wav2spectrogram as wav2spec
import mir3.data.spectrogram as spec
import mir3.data.feature_track as track
import matplotlib.pyplot as plt

import mir3.modules.features.flatness as feat_flat
import mir3.modules.features.energy as feat_energy
import mir3.modules.features.flux as feat_flux
import mir3.modules.features.centroid as feat_centroid
import mir3.modules.features.rolloff as feat_rolloff
import mir3.modules.features.low_energy as feat_lowenergy
import mir3.lib.mir.mfcc as mfcc
import mir3.lib.mir.tdom_features as tdomf

import mir3.modules.features.join as feat_join
import mir3.modules.features.stats as feat_stats


class FrequencyBand:
    def bands(self):
        pass


class LinearBand(FrequencyBand):

    def __init__(self, low=0, high=22050, step=1000, nbands=None):
        self.low = low
        self.high = high
        self.step = step
        self.i = low
        self.nbands = nbands

    def bands(self):
        if self.nbands is not None:
            len = int(self.high / float(self.nbands))
            bands = []
            for i in range(self.nbands-1):
                bands.append((i*len, (i * len) + (len-1)))
            i+=1
            bands.append((i*len, self.high))
            for i in bands:
                yield i
        else:
            for i in range(self.low, self.high, self.step):
                end = i + self.step
                if end > self.high:
                    end = i + (self.step - (end - self.high))
                yield (i, end)


class OneBand(FrequencyBand):

    def __init__(self, low=0, high=22050):
        self.low = low
        self.high = high

    def bands(self):
        yield (self.low, self.high)


def mel_to_hz(mel):
    return 700 * (10**(float(mel) / 2595)-1)


def hz_to_mel(hz):
    return 2595 * np.log10(1 + (float(hz) / 700))


class MelBand(FrequencyBand):

    def __init__(self, low=0, high=22050, step = 300, nbands=None):
        self.low = low
        self.high = high
        self.step = step
        self.low_mel = hz_to_mel(low)
        self.high_mel = hz_to_mel(high)
        self.nbands = nbands

        #print "low hz = %d, low mel = %d, high hz = %d, high mel = %d" % (self.low, self.low_mel, self.high, self.high_mel)


    def bands(self):

        if self.nbands is not None:
            len = int(self.high_mel / float(self.nbands))
            bands = []
            for i in range(self.nbands-1):
                bands.append((i*len, (i * len) + (len-1)))
            i+=1
            bands.append((i*len, self.high_mel))
            for i in bands:
                yield (int(np.floor(mel_to_hz(i[0]))), int(np.floor(mel_to_hz(i[1]))))
        else:
            for i in range(np.int(self.low_mel), np.int(self.high_mel), self.step):
                beg = int(np.floor(mel_to_hz(i)))
                end = int(np.floor(mel_to_hz(i + self.step)))-1
                if end > self.high:
                    end = self.high
                yield (beg, end)



class BandwiseFeatures:
    """
    :type infile: string
    :type dft_len: int
    :type window: string
    :type window_len: int
    :type window_step: int
    :type db_spec: boolean
    :type band_features: np.ndarray
    :type spectrogram mir3.data.Spectrogram
    """

    def __init__(self, infile, dft_len=2048, window='hanning', window_len=2048, window_step=1024, db_spec = True):
        self.infile = infile
        self.dft_len = dft_len
        self.window_len = window_len
        self.window = window
        self.window_step = window_step
        self.db_spec = db_spec
        self.features_per_band = 0


        #load the auio file and compute its spectrum
        audio_file = open(infile, 'rb')
        self.spectrogram = wav2spec.Wav2Spectrogram().convert(audio_file, dft_length=dft_len,\
                                                              window_step=window_step,\
                                                              window_length=window_len)

        #keeping the time-domain data for computing time-domain features
        rate, audio_data = scipy.io.wavfile.read(infile)
        audio_data = audio_data.astype(np.float)
        audio_data /= np.max(np.abs(audio_data)) # Normalization to -1/+1 range
        self.audio_data = np.copy(audio_data)
        self.samplingrate = rate
        audio_file.close()

        if db_spec:
            self.spectrogram.data = 20 * np.log10(self.spectrogram.data + np.finfo(np.float).eps)

        self.band_features = np.array([])
        self.joined_features = None
        self.cropped = None

    def spec_to_db(self):
        self.spectrogram.data = 20 * np.log10(self.spectrogram.data + np.finfo(np.float).eps)

    def calculate_features_per_band(self, frequency_band, discard_bin_zero=False):
        """
        :param frequency_band: FrequencyBand
        :return: list[FeatureTrack]
        """

        flatness = feat_flat.Flatness()
        energy = feat_energy.Energy()
        flux = feat_flux.Flux()
        centroid = feat_centroid.Centroid()
        rolloff = feat_rolloff.Rolloff()
        lowenergy = feat_lowenergy.LowEnergy()

        for b in frequency_band.bands():
            lowbin = self.spectrogram.freq_bin(b[0])
            if lowbin == 0:
                if discard_bin_zero:
                    lowbin = 1
            highbin = self.spectrogram.freq_bin(b[1])
            #print "calculating features for band in bin range: ", lowbin, highbin

            features = []

            flatness_feature = flatness.calc_track_band(self.spectrogram, lowbin, highbin)
            flatness_feature.metadata.feature += ("_" + str(b[0]))
            features.append(flatness_feature)

            energy_feature = energy.calc_track_band(self.spectrogram, lowbin, highbin)
            energy_feature.metadata.feature += ("_" + str(b[0]))
            features.append(energy_feature)

            flux_feature = flux.calc_track_band(self.spectrogram, lowbin, highbin)
            flux_feature.metadata.feature += ("_" + str(b[0]))
            features.append(flux_feature)

            centroid_feature = centroid.calc_track_band(self.spectrogram, lowbin, highbin)
            centroid_feature.metadata.feature += ("_" + str(b[0]))
            features.append(centroid_feature)
{'fraction_non_alphanumeric': 0.1073730545511712, 'fraction_numerical': 0.016192422575066815, 'mean_word_length': 2.1183823529411763, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '352705', 'n_tokens_mistral': 4223, 'n_tokens_neox': 3985, 'n_words': 867}