<commit_before>from main.experiments.ExperimentAggregator import ExperimentAggregator
from matplotlib.pyplot import plot, fill_between, savefig, show
from numpy.linalg.linalg import norm
from numpy.ma.core import arange, zeros, mean, std, allclose, sqrt

class SingleChainExperimentAggregator(ExperimentAggregator):
    def __init__(self, folders, ref_quantiles=arange(0.1, 1, 0.1)):
        ExperimentAggregator.__init__(self, folders)
        self.ref_quantiles = ref_quantiles
    
    def __process_results__(self):
        lines = []
        if len(self.experiments) == 0:
            lines.append("no experiments to process")
            return
        
        # burnin is the same for all chains
        burnin = self.experiments[0].mcmc_chain.mcmc_params.burnin
        
        quantiles = zeros((len(self.experiments), len(self.ref_quantiles)))
        norm_of_means = zeros(len(self.experiments))
        acceptance_rates = zeros(len(self.experiments))
        
        for i in range(len(self.experiments)):
            burned_in = self.experiments[i].mcmc_chain.samples[burnin:, :]
            
            # use precomputed quantiles if they match with the provided ones
            if hasattr(self.experiments[i], "ref_quantiles") and \
               allclose(self.ref_quantiles, self.experiments[i].ref_quantiles):
                quantiles[i, :] = self.experiments[i].quantiles
            else:
                quantiles[i, :] = self.experiments[i].mcmc_chain.mcmc_sampler.distribution.emp_quantiles(\
                                  burned_in, self.ref_quantiles)
                
            norm_of_means[i] = norm(mean(burned_in, 0))
            acceptance_rates[i] = mean(self.experiments[i].mcmc_chain.accepteds[burnin:])

        mean_quantiles = mean(quantiles, 0)
        std_quantiles = std(quantiles, 0)
        
        lines.append("quantiles:")
        for i in range(len(self.ref_quantiles)):
            lines.append(str(mean_quantiles[i]) + " +- " + str(std_quantiles[i]))
        
        lines.append("norm of means:")
        lines.append(str(mean(norm_of_means)) + " +- " + str(std(norm_of_means)))
        
        lines.append("acceptance rate:")
        lines.append(str(mean(acceptance_rates)))
        
        
        
        # mean as a function of iterations
        step = 1000
        iterations = arange(self.experiments[0].mcmc_chain.mcmc_params.num_iterations+step, step=step)
        
        running_means = zeros(len(iterations))
        running_errors = zeros(len(iterations))
        for i in arange(len(iterations)):
            # norm of mean of chain up to current iterations
            norm_of_means_yet = zeros(len(self.experiments))
            for j in range(len(self.experiments)):
                samples_yet = self.experiments[j].mcmc_chain.samples[:(iterations[i] + 1),:]
                norm_of_means_yet[j] = norm(mean(samples_yet, 0))
            
            running_means[i] = mean(norm_of_means_yet)
            error_level = 1.96
            running_errors[i] = error_level*std(norm_of_means_yet) / sqrt(i + 1)
            
        plot(iterations, running_means)
        fill_between(iterations, running_means - running_errors, \
                     running_means + running_errors, hold=True, color="gray")
        savefig(self.experiments[0].experiment_dir + self.experiments[0].name + "_running_mean.png")
#        show()
        
        
        
        
        
        
        # add latex table line
        latex_lines = []
        latex_lines.append("& STM & ADM & ADML & KAM\\\\")
        
        # add latex table line
        latex_lines = []
        latex_lines.append("Sampler & Acceptance & Norm(mean) & ")
        for i in range(len(self.ref_quantiles)):
            latex_lines.append('%.1f' % self.ref_quantiles[i] + "-quantile")
            if i < len(self.ref_quantiles) - 1:
                latex_lines.append(" & ")
        latex_lines.append("\\\\")<commit_msg>fix y axis in mean plots<commit_after>from main.experiments.ExperimentAggregator import ExperimentAggregator
from matplotlib.pyplot import plot, fill_between, savefig, show, ylim
from numpy.linalg.linalg import norm
from numpy.ma.core import arange, zeros, mean, std, allclose, sqrt

class SingleChainExperimentAggregator(ExperimentAggregator):
    def __init__(self, folders, ref_quantiles=arange(0.1, 1, 0.1)):
        ExperimentAggregator.__init__(self, folders)
        self.ref_quantiles = ref_quantiles
    
    def __process_results__(self):
        lines = []
        if len(self.experiments) == 0:
            lines.append("no experiments to process")
            return
        
        # burnin is the same for all chains
        burnin = self.experiments[0].mcmc_chain.mcmc_params.burnin
        
        quantiles = zeros((len(self.experiments), len(self.ref_quantiles)))
        norm_of_means = zeros(len(self.experiments))
        acceptance_rates = zeros(len(self.experiments))
        
        for i in range(len(self.experiments)):
            burned_in = self.experiments[i].mcmc_chain.samples[burnin:, :]
            
            # use precomputed quantiles if they match with the provided ones
            if hasattr(self.experiments[i], "ref_quantiles") and \
               allclose(self.ref_quantiles, self.experiments[i].ref_quantiles):
                quantiles[i, :] = self.experiments[i].quantiles
            else:
                quantiles[i, :] = self.experiments[i].mcmc_chain.mcmc_sampler.distribution.emp_quantiles(\
                                  burned_in, self.ref_quantiles)
                
            norm_of_means[i] = norm(mean(burned_in, 0))
            acceptance_rates[i] = mean(self.experiments[i].mcmc_chain.accepteds[burnin:])

        mean_quantiles = mean(quantiles, 0)
        std_quantiles = std(quantiles, 0)
        
        lines.append("quantiles:")
        for i in range(len(self.ref_quantiles)):
            lines.append(str(mean_quantiles[i]) + " +- " + str(std_quantiles[i]))
        
        lines.append("norm of means:")
        lines.append(str(mean(norm_of_means)) + " +- " + str(std(norm_of_means)))
        
        lines.append("acceptance rate:")
        lines.append(str(mean(acceptance_rates)))
        
        
        
        # mean as a function of iterations
        step = 1000
        iterations = arange(self.experiments[0].mcmc_chain.mcmc_params.num_iterations+step, step=step)
        
        running_means = zeros(len(iterations))
        running_errors = zeros(len(iterations))
        for i in arange(len(iterations)):
            # norm of mean of chain up to current iterations
            norm_of_means_yet = zeros(len(self.experiments))
            for j in range(len(self.experiments)):
                samples_yet = self.experiments[j].mcmc_chain.samples[:(iterations[i] + 1),:]
                norm_of_means_yet[j] = norm(mean(samples_yet, 0))
            
            running_means[i] = mean(norm_of_means_yet)
            error_level = 1.96
            running_errors[i] = error_level*std(norm_of_means_yet) / sqrt(i + 1)
            
        plot(iterations, running_means)
        fill_between(iterations, running_means - running_errors, \
                     running_means + running_errors, hold=True, color="gray")
        savefig(self.experiments[0].experiment_dir + self.experiments[0].name + "_running_mean.png")
        ylim(0,10)
#        show()
        
        
        
        
        
        
        # add latex table line
        latex_lines = []
        latex_lines.append("& STM & ADM & ADML & KAM\\\\")
        
        # add latex table line
        latex_lines = []
        latex_lines.append("Sampler & Acceptance & Norm(mean) & ")
        for i in range(len(self.ref_quantiles)):
            latex_lines.append('%.1f' % self.ref_quantiles[i] + "-quantile")
            if i < len(self.ref_quantiles) - 1:
                latex_lines.append(" & ")
        latex_lines.append("\\\\")
{'fraction_non_alphanumeric': 0.10955126598144899, 'fraction_numerical': 0.006643268989721735, 'mean_word_length': 2.2147461724415796, 'pattern_counts': {'":': 0, '<': 5, '<?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': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7615', 'n_tokens_mistral': 2540, 'n_tokens_neox': 2415, 'n_words': 426}