<commit_before>__author__ = 'nmearl'

import numpy as np
import pylab


class Analyzer(object):

    def __init__(self, optimizer=None, path=None):
        self.optimizier = optimizer

        # burnin = int(0.5 * len(self.optimizier.chain))
        self.samples = self.optimizier.chain

        if path and not optimizer.chain:
            self.samples = np.load(path)

        self.results = map(lambda v: (v[1], v[2] - v[1], v[1] - v[0]),
                           zip(*np.percentile(self.samples[:, 1:], [16, 50, 84],
                                              axis=0)))

    def report(self):
        print("{0:12s} {1:12s} {2:12s} {3:12s}".format('Name', 'Value', 'Err-', 'Err+'))

        j = 0
        with open("report_" + self.optimizier.output_prefix + ".out", 'w') as f:
            for i in range(len(self.optimizier.params.all())):
                param = self.optimizier.params.all()[i]

                if param.vary:
                    print("{0:12s} {1:12g} {2:12g} {3:12g}".format(param.name, self.results[j][0],
                                                                   self.results[j][1], self.results[j][2]))

                    f.write("{0:12s} {1:12g} {2:12g} {3:12g}\n".format(param.name, self.results[j][0],
                                                                       self.results[j][1], self.results[j][2]))
                    j += 1
                else:
                    print("{0:12s} {1:12g}".format(param.name, param.value))

                    f.write("{0:12s} {1:12g}\n".format(param.name, param.value))

    def show(self, method, param_list=None):
        if method == 'histogram':
            self.histogram(param_list=param_list, show=True)
        elif method == 'chi':
            self.chi(param_list=param_list, show=True)

    def save(self, method, param_list=None, prefix='hist'):
        if method == 'histogram':
            self.histogram(save=True, show=False, param_list=param_list, prefix=prefix)

    def chi(self, param_list=None, show=True, save=False):

        for i in range(1, len(self.optimizier.params.all(True))):
            pylab.plot(self.samples[:, i], self.samples[:, 0], '+')
            pylab.show()

    def histogram(self, param_list=None, save=False, show=True, prefix='hist'):

        for i in range(1, len(self.optimizier.params.all(True))):
            # pylab.figure()

            param = self.optimizier.params.all(True)[i]

            if param_list and param.name not in param_list:
                continue

            ax = pylab.subplot('111')#.format(res, j+1))
            ax.hist(self.samples[:, i], 100, color="k", alpha=0.5, histtype="step")
            ax.axvline(self.results[i][0], color="r", lw=2)
            ax.axvline(self.results[i][0] + self.results[i][1], color='k', alpha=0.6)
            ax.axvline(self.results[i][0] - self.results[i][2], color='k', alpha=0.6)
            ax.set_title("{0:s} Parameter Distribution".format(param.name))
            ax.set_xlim(self.results[i][0] * 0.95, self.results[i][0] * 1.05)
            pylab.tight_layout()

            if save:
                pylab.savefig('{0}_{1:s}.png'.format(prefix, param.name))
            if show:
                pylab.show()
            # pylab.close()

    def step(self, param_list=None, save=False, show=True, prefix='step'):

        for i in range(1, len(self.optimizier.params.all(True))):
            pylab.figure()

            param = self.optimizier.params.all(True)[i]

            if param_list and param.name not in param_list:
                continue

            ax = pylab.subplot('111')
            ax.plot(range(len(self.samples)), self.samples[:, i], color="k", alpha=0.5)
            ax.axhline(self.results[i][0], color="r", lw=2)
            ax.axhline(self.results[i][0] + self.results[i][1], color='k', alpha=0.6)
            ax.axhline(self.results[i][0] - self.results[i][2], color='k', alpha=0.6)
            ax.set_title("{0:s} Step".format(param.name))
            # ax.set_xlim(results[k][0] * 0.95, results[k][0] * 1.05)
            pylab.tight_layout()

            if save:
                pylab.savefig('{0}_{1:s}.png'.format(prefix, param.name))
            if show:
                pylab.show()

            pylab.close()

    def plot(self, save=False, show=True, prefix='plot'):
        mod_flux, mod_rv = self.optimizier.model()

        pylab.plot(self.optimizier.photo_data[0], self.optimizier.photo_data[1], 'k.')
        pylab.plot(self.optimizier.photo_data[0], mod_flux, 'r')

        if save:
            pylab.savefig('{0}.png'.format(prefix))

        pylab.show()<commit_msg>Improved plotting.<commit_after>__author__ = 'nmearl'

import numpy as np
import pylab


class Analyzer(object):

    def __init__(self, optimizer=None, path=None):
        self.optimizier = optimizer

        # burnin = int(0.5 * len(self.optimizier.chain))
        self.samples = self.optimizier.chain

        if path and not optimizer.chain:
            self.samples = np.load(path)

        self.results = map(lambda v: (v[1], v[2] - v[1], v[1] - v[0]),
                           zip(*np.percentile(self.samples[:, 1:], [16, 50, 84],
                                              axis=0)))

    def report(self):
        print("{0:12s} {1:12s} {2:12s} {3:12s}".format('Name', 'Value', 'Err-', 'Err+'))

        j = 0
        with open("report_" + self.optimizier.output_prefix + ".out", 'w') as f:
            for i in range(len(self.optimizier.params.all())):
                param = self.optimizier.params.all()[i]

                if param.vary:
                    print("{0:12s} {1:12g} {2:12g} {3:12g}".format(param.name, self.results[j][0],
                                                                   self.results[j][1], self.results[j][2]))

                    f.write("{0:12s} {1:12g} {2:12g} {3:12g}\n".format(param.name, self.results[j][0],
                                                                       self.results[j][1], self.results[j][2]))
                    j += 1
                else:
                    print("{0:12s} {1:12g}".format(param.name, param.value))

                    f.write("{0:12s} {1:12g}\n".format(param.name, param.value))

    def show(self, method, param_list=None):
        if method == 'histogram':
            self.histogram(param_list=param_list, show=True)
        elif method == 'plot':
            self.plot()
        elif method == 'chi':
            self.chi(param_list=param_list, show=True)

    def save(self, method, param_list=None, prefix='hist'):
        if method == 'histogram':
            self.histogram(save=True, show=False, param_list=param_list, prefix=prefix)
        elif method == 'plot':
            self.plot(save=True, show=False)
        elif method == 'chi':
            self.chi(param_list=param_list, save=True, show=False)

    def chi(self, param_list=None, show=True, save=False):

        for i in range(1, len(self.optimizier.params.all(True))):
            pylab.plot(self.samples[:, i], self.samples[:, 0], '+')
            pylab.show()

    def histogram(self, param_list=None, save=False, show=True, prefix='hist'):

        for i in range(1, len(self.optimizier.params.all(True))):
            param = self.optimizier.params.all(True)[i]

            if param_list and param.name not in param_list:
                continue

            ax = pylab.subplot('111')#.format(res, j+1))
            ax.hist(self.samples[:, i], 100, color="k", alpha=0.5, histtype="step")
            ax.axvline(self.results[i][0], color="r", lw=2)
            ax.axvline(self.results[i][0] + self.results[i][1], color='k', alpha=0.6)
            ax.axvline(self.results[i][0] - self.results[i][2], color='k', alpha=0.6)
            ax.set_title("{0:s} Parameter Distribution".format(param.name))
            ax.set_xlim(self.results[i][0] * 0.95, self.results[i][0] * 1.05)
            pylab.tight_layout()

            if save:
                pylab.savefig('{0}_{1:s}.png'.format(prefix, param.name))
            if show:
                pylab.show()

            pylab.close()

    def step(self, param_list=None, save=False, show=True, prefix='step'):

        for i in range(1, len(self.optimizier.params.all(True))):
            pylab.figure()

            param = self.optimizier.params.all(True)[i]

            if param_list and param.name not in param_list:
                continue

            ax = pylab.subplot('111')
            ax.plot(range(len(self.samples)), self.samples[:, i], color="k", alpha=0.5)
            ax.axhline(self.results[i][0], color="r", lw=2)
            ax.axhline(self.results[i][0] + self.results[i][1], color='k', alpha=0.6)
            ax.axhline(self.results[i][0] - self.results[i][2], color='k', alpha=0.6)
            ax.set_title("{0:s} Step".format(param.name))
            # ax.set_xlim(results[k][0] * 0.95, results[k][0] * 1.05)
            pylab.tight_layout()

            if save:
                pylab.savefig('{0}_{1:s}.png'.format(prefix, param.name))
            if show:
                pylab.show()

            pylab.close()

    def plot(self, save=False, show=True, prefix='plot'):
        mod_flux, mod_rv = self.optimizier.model()

        pylab.plot(self.optimizier.photo_data[0], self.optimizier.photo_data[1], 'k.')
        pylab.plot(self.optimizier.photo_data[0], mod_flux, 'r')

        if save:
            pylab.savefig('{0}.png'.format(prefix))
        if show:
            pylab.show()

        pylab.close()
{'fraction_non_alphanumeric': 0.16766719325961033, 'fraction_numerical': 0.02843601895734597, 'mean_word_length': 1.9768025078369906, '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': 14, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3175563', 'n_tokens_mistral': 3332, 'n_tokens_neox': 3141, 'n_words': 543}