<commit_before>"""
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.

Written (W) 2013 <NAME>
"""
from aggregators.ScalarResultAggregator import ScalarResultAggregator
from numpy.ma.core import log, zeros
from ozone.distribution.OzonePosterior import OzonePosterior
from ozone.distribution.OzonePosteriorAverage import OzonePosteriorAverage
from ozone.jobs.OzoneLikelihoodWithoutLogDetJob import \
    OzoneLikelihoodWithoutLogDetJob
from ozone.jobs.OzoneLogDetJob import OzoneLogDetJob
import logging

class OzonePosteriorAverageEngine(OzonePosteriorAverage):
    def __init__(self, computation_engine, num_estimates, prior):
        OzonePosteriorAverage.__init__(self, num_estimates, prior)
        
        self.computation_engine = computation_engine
        
    def precompute_likelihood_estimates(self, tau, kappa):
        logging.debug("Entering")
        
        # submit all jobs for log-determinant Q
        aggregators_Q = []
        for _ in range(self.num_estimates):
            job = OzoneLogDetJob(ScalarResultAggregator(), self, tau, kappa, "Q")
            aggregators_Q.append(self.computation_engine.submit_job(job))
        
        # submit all jobs for log-determinant M
        aggregators_M = []
        for _ in range(self.num_estimates):
            job = OzoneLogDetJob(ScalarResultAggregator(), self, tau, kappa, "M")
            aggregators_M.append(self.computation_engine.submit_job(job))
        
        # submit job for remainder of likelihood
        job = OzoneLikelihoodWithoutLogDetJob(ScalarResultAggregator(), self, tau, kappa)
        aggregator_remainder = self.computation_engine.submit_job(job)
        
        # grab a coffee
        self.computation_engine.wait_for_all()
        
        # collect results from all aggregators
        log_dets_Q = zeros(self.num_estimates)
        log_dets_M = zeros(self.num_estimates)
        for i in range(self.num_estimates):
            aggregators_Q[i].finalize()
            aggregators_M[i].finalize()
            log_dets_Q[i]=aggregators_Q[i].get_final_result().result
            log_dets_M[i]=aggregators_M[i].get_final_result().result
            
        aggregator_remainder.finalize()
        result_remainder = aggregator_remainder.get_final_result().result
            
        # load n since needed for likelihood
        y, _ = OzonePosterior.load_ozone_data()
        n = len(y)
        
        # construct all likelihood estimates
        log_det_parts = 0.5 * log_dets_Q + 0.5 * n * log(tau) - 0.5 * log_dets_M
        estimates = log_det_parts + result_remainder
        
        logging.debug("Leaving")<commit_msg>added new clean up call<commit_after>"""
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.

Written (W) 2013 <NAME>
"""
from aggregators.ScalarResultAggregator import ScalarResultAggregator
from numpy.ma.core import log, zeros
from ozone.distribution.OzonePosterior import OzonePosterior
from ozone.distribution.OzonePosteriorAverage import OzonePosteriorAverage
from ozone.jobs.OzoneLikelihoodWithoutLogDetJob import \
    OzoneLikelihoodWithoutLogDetJob
from ozone.jobs.OzoneLogDetJob import OzoneLogDetJob
import logging

class OzonePosteriorAverageEngine(OzonePosteriorAverage):
    def __init__(self, computation_engine, num_estimates, prior):
        OzonePosteriorAverage.__init__(self, num_estimates, prior)
        
        self.computation_engine = computation_engine
        
    def precompute_likelihood_estimates(self, tau, kappa):
        logging.debug("Entering")
        
        # submit all jobs for log-determinant Q
        aggregators_Q = []
        for _ in range(self.num_estimates):
            job = OzoneLogDetJob(ScalarResultAggregator(), self, tau, kappa, "Q")
            aggregators_Q.append(self.computation_engine.submit_job(job))
        
        # submit all jobs for log-determinant M
        aggregators_M = []
        for _ in range(self.num_estimates):
            job = OzoneLogDetJob(ScalarResultAggregator(), self, tau, kappa, "M")
            aggregators_M.append(self.computation_engine.submit_job(job))
        
        # submit job for remainder of likelihood
        job = OzoneLikelihoodWithoutLogDetJob(ScalarResultAggregator(), self, tau, kappa)
        aggregator_remainder = self.computation_engine.submit_job(job)
        
        # grab a coffee
        self.computation_engine.wait_for_all()
        
        # collect results from all aggregators
        log_dets_Q = zeros(self.num_estimates)
        log_dets_M = zeros(self.num_estimates)
        for i in range(self.num_estimates):
            aggregators_Q[i].finalize()
            aggregators_M[i].finalize()
            log_dets_Q[i]=aggregators_Q[i].get_final_result().result
            log_dets_M[i]=aggregators_M[i].get_final_result().result
            aggregators_Q[i].clean_up()
            aggregators_M[i].clean_up()
            
        aggregator_remainder.finalize()
        result_remainder = aggregator_remainder.get_final_result().result
        aggregator_remainder.clean_up()
            
        # load n since needed for likelihood
        y, _ = OzonePosterior.load_ozone_data()
        n = len(y)
        
        # construct all likelihood estimates
        log_det_parts = 0.5 * log_dets_Q + 0.5 * n * log(tau) - 0.5 * log_dets_M
        estimates = log_det_parts + result_remainder
        
        logging.debug("Leaving")
{'fraction_non_alphanumeric': 0.07504332755632583, 'fraction_numerical': 0.0038128249566724438, 'mean_word_length': 2.982746721877157, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '1209682', 'n_tokens_mistral': 1764, 'n_tokens_neox': 1693, 'n_words': 380}