<commit_before>'''Service Layer Module.
Contains high-level code from which calls to the logic layer can be made.
'''
import logging
import db_layer
import os
#import custom RFCx modules from modules folder
from domain_modules import load_sound
from domain_modules import spectral_analysis
from domain_modules import fingerprinting
from domain_modules import sound_classification
from domain_modules import anomaly_detection
from domain_modules import alerts

# Todo: replace logging level by config file or command line options

class Service(object):
    # standard dependencies for services
    def __init__(self, logger):
        self.logger = logger

class AcquireAudio(Service):
    def read(self, fs, meta_data):
        self.logger.info("""%s - Reading sound file""" % (meta_data["audio_id"]))
        try:
            sound = load_sound.read_sound(fs, meta_data)
        except Exception, e:
            self.logger.error("""%s - Read-in failed\n%s""" % (meta_data["audio_id"], e))
            exit(1)
        else:
            self.logger.info("%s - Read-in successful""" % (meta_data["audio_id"]))
            return sound

class AnalyzeSound(Service):
    def __init__(self, logger):
        super(AnalyzeSound, self).__init__(logger)

    def analyze(self, sound):
        '''Analyze: Processes a wav file into a spectrum, profile it,
        and trigger results.
        '''

        audio_id = sound.meta_data.get('audio_id') or 'unknown'

        # (1) spectral analysis
        spectrum = spectral_analysis.Spectrum(sound)
        self.logger.info("""%s - Completed spectrum generation""" % (audio_id,))
        self.logger.info("""%s - Analyzing""" % (audio_id,))

        # (2) create an audio finger print
        profile = fingerprinting.Profile(spectrum)
        self.logger.info("""%s - Created a fingerprinter""" % (audio_id,))
        profile.get_harmonic_sound_bounds()
        self.logger.info("""%s - Found %i areas of interest""" % (audio_id, len(profile.interest_fingerprints)))

        # (3) explicit detection and classification of sound against known sound sources
        prof_final = sound_classification.SoundClassifier(profile).classify_interest_areas()
        self.logger.info("""%s - Completed classification""" % (audio_id,))
        self.logger.info("""%s - Found %s""" % (audio_id, ','.join(prof_final)))
        
# temporarily disabling machine learning for troubleshooting      
        # (4) use ML to determine whether the sound is an anomaly
        # Todo: add requirements for anomaly detection, then add these lines  
#        repo = db_layer.AnomalyDetectionRepo()
#        anomaly_detection.AnomalyDetector(self.logger, repo).determine_anomaly(profile)
#        self.logger.info("""%s - Completed ML analysis""" % (audio_id,))

        # (5) send alerts if necessary based on interest areas found in fingerprinting module
        alert_sender = alerts.AlertSender(self.logger, profile)
        alert_sender.push_alerts()
        self.logger.info("""%s - Completed sending alerts""" % (audio_id,))
        self.logger.info("""%s - Sent %i alerts out""" % (audio_id, alert_sender.sent))


class UpdateSoundProfile(object):
    def __init__(self, profile, settings):
        '''Update Sound Profile Class: Updates or creates sound profile
        based on ML framework.
        '''
        pass

    def validate(profile, settings):
        '''Validate: Check JSON string against valid profile object properties'''
        pass

    def update(self):
        '''Update. Adds or edits sound profiles in the database.'''
        pass<commit_msg>less logging<commit_after>'''Service Layer Module.
Contains high-level code from which calls to the logic layer can be made.
'''
import logging
import db_layer
import os
#import custom RFCx modules from modules folder
from domain_modules import load_sound
from domain_modules import spectral_analysis
from domain_modules import fingerprinting
from domain_modules import sound_classification
from domain_modules import anomaly_detection
from domain_modules import alerts

# Todo: replace logging level by config file or command line options

class Service(object):
    # standard dependencies for services
    def __init__(self, logger):
        self.logger = logger

class AcquireAudio(Service):
    def read(self, fs, meta_data):
#        self.logger.info("""%s - Reading sound file""" % (meta_data["audio_id"]))
        try:
            sound = load_sound.read_sound(fs, meta_data)
        except Exception, e:
            self.logger.error("""%s - Read-in failed\n%s""" % (meta_data["audio_id"], e))
            exit(1)
        else:
#            self.logger.info("%s - Read-in successful""" % (meta_data["audio_id"]))
            return sound

class AnalyzeSound(Service):
    def __init__(self, logger):
        super(AnalyzeSound, self).__init__(logger)

    def analyze(self, sound):
        '''Analyze: Processes a wav file into a spectrum, profile it,
        and trigger results.
        '''

        audio_id = sound.meta_data.get('audio_id') or 'unknown'

        # (1) spectral analysis
        spectrum = spectral_analysis.Spectrum(sound)
#        self.logger.info("""%s - Completed spectrum generation""" % (audio_id,))
#        self.logger.info("""%s - Analyzing""" % (audio_id,))

        # (2) create an audio finger print
        profile = fingerprinting.Profile(spectrum)
#        self.logger.info("""%s - Created a fingerprinter""" % (audio_id,))
        profile.get_harmonic_sound_bounds()
#        self.logger.info("""%s - Found %i areas of interest""" % (audio_id, len(profile.interest_fingerprints)))

        # (3) explicit detection and classification of sound against known sound sources
        prof_final = sound_classification.SoundClassifier(profile).classify_interest_areas()
#        self.logger.info("""%s - Completed classification""" % (audio_id,))
        self.logger.info("""%s - Found %s""" % (audio_id, ','.join(prof_final)))
        
# temporarily disabling machine learning for troubleshooting      
        # (4) use ML to determine whether the sound is an anomaly
        # Todo: add requirements for anomaly detection, then add these lines  
#        repo = db_layer.AnomalyDetectionRepo()
#        anomaly_detection.AnomalyDetector(self.logger, repo).determine_anomaly(profile)
#        self.logger.info("""%s - Completed ML analysis""" % (audio_id,))

        # (5) send alerts if necessary based on interest areas found in fingerprinting module
        alert_sender = alerts.AlertSender(self.logger, profile)
        alert_sender.push_alerts()
        self.logger.info("""%s - Completed sending alerts""" % (audio_id))


class UpdateSoundProfile(object):
    def __init__(self, profile, settings):
        '''Update Sound Profile Class: Updates or creates sound profile
        based on ML framework.
        '''
        pass

    def validate(profile, settings):
        '''Validate: Check JSON string against valid profile object properties'''
        pass

    def update(self):
        '''Update. Adds or edits sound profiles in the database.'''
        pass
{'fraction_non_alphanumeric': 0.10978184377199156, 'fraction_numerical': 0.001688951442646024, 'mean_word_length': 3.1775426219870666, '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': '2193393', 'n_tokens_mistral': 1980, 'n_tokens_neox': 1909, 'n_words': 593}