<commit_before>"""
Functions to visualize an Experiment.

"""

__authors__   = "<NAME>"
__copyright__ = "(c) 2010, Universite de Montreal"
__license__   = "3-clause BSD License"
__contact__   = "<NAME> <<EMAIL>>"

import cPickle
import math
import sys

import matplotlib.pyplot as plt
import numpy

import ht_dist2

def main_plot_history(self):
    """
    self - experiment
    """
    # self is an Experiment
    status_colors = {'new':'k', 'running':'g', 'ok':'b', 'fail':'r'}
    Xs = self.trials

    # XXX: show the un-finished or error trials
    Ys, colors = zip(*[(y, status_colors[s])
        for y, s in zip(self.losses(), self.statuses()) if y is not None])
    plt.scatter(range(len(Ys)), Ys, c=colors)
    plt.xlabel('time')
    plt.ylabel('loss')
    try:
        loss_target = self.bandit.loss_target()
        have_losstarget = True
    except NotImplementedError:
        loss_target = numpy.min(Ys)
        have_losstarget = False
    if have_losstarget:
        plt.axhline(loss_target)
        ymin = min(numpy.min(Ys), loss_target)
        ymax = max(numpy.max(Ys), loss_target)
        yrange = ymax - ymin
        ymean = (ymax + ymin) / 2.0
        plt.ylim(
                ymean - 0.53 * yrange,
                ymean + 0.53 * yrange,
                )
    if 1:
        plt.axhline(self.average_best_error(), c='g')
    plt.title('bandit: %s algo: %s' % (
        self.bandit.short_str(),
        self.bandit_algo.short_str()))
    plt.show()


def main_plot_vars(self):
    import montetheano.rstreams
    # this requires a TheanoBanditAlgo, which has the sample history recorded as
    # idxs and vals
    bandit_algo = self.bandit_algo
    bandit = bandit_algo.bandit

    try:
        flat_template = bandit.template.flatten()
        flat_names = [n[1:] for n in bandit.template.flatten_names(prefix="")]
    except:
        flat_names = ['name not found'] * 100


    TBA_ids = set()
    for db_idx in bandit_algo.db_idxs:
        TBA_ids.update(db_idx)
    trial_TBA_ids = set([t['_config_id'] for t in self.trials])
    skip_ids = [t for t in TBA_ids if t not in trial_TBA_ids]
    print 'Skipping %i jobs not in the official trials: %s' % (
            len(skip_ids), str(skip_ids))

    # at which point in `trials` was a given TBA_id inserted?
    TBA_id_timestep = dict([(t['_config_id'], i) for i, t in enumerate(self.trials)])

    results_by_TBA_id = dict([
        (t['_config_id'], bandit.loss(r, config=t))
        for (t, r) in zip(self.trials, self.results)
        ])

    loss_min = min([y for y in self.losses() if y is not None])
    loss_max = max([y for y in self.losses() if y is not None])

    def color_fn(lossval):
        if lossval is None:
            return (1, 1, 1)
        else:
            t = 4 * (lossval - loss_min) / (loss_max - loss_min + .0001)
            if t < 1:
                return t, 0, 0
            if t < 2:
                return 2-t, t-1, 0
            if t < 3:
                return 0, 3-t, t-2
            return 0, 0, 4-t

    def color_fn_bw(lossval):
        if lossval is None:
            return (1, 1, 1)
        else:
            t = (lossval - loss_min) / (loss_max - loss_min + .0001)
            return (t, t, t)

    C = 5
    R = int(numpy.ceil(len(bandit_algo.s_vals) / float(C)))

    for ii, (s_val, db_idx, db_val, template_pos) in enumerate(zip(
            bandit_algo.s_vals,
            bandit_algo.db_idxs,
            bandit_algo.db_vals,
            bandit_algo.all_s_locs)):

        plt.subplot(R, C, ii + 1)

        # hide x ticks
        ticks_num, ticks_txt = plt.xticks()
        plt.xticks(ticks_num, ['' for i in xrange(len(ticks_num))])

        try:
            dist_name = montetheano.rstreams.rv_dist_name(s_val)
        except:
            print >> sys.stderr, 'problem with', s_val
            raise
        if dist_name in ('lognormal', 'quantized_lognormal'):
            if 0:
                print '-----------'
                print flat_names[template_pos]
                print flat_template[template_pos]
                print s_val
                print db_idx
                print db_val

            results_ii = [results_by_TBA_id[i] for i in db_idx
                    if i in trial_TBA_ids]
            coords_ii = numpy.log([v for i, v in zip(db_idx, db_val)
                    if i in trial_TBA_ids])
            time_ii = [TBA_id_timestep[i] for i in db_idx
                    if i in trial_TBA_ids]

            plt.title(flat_names[template_pos], fontsize=10)
            plt.scatter(time_ii, coords_ii,
                    c=map(color_fn_bw, results_ii))
        elif dist_name == 'categorical':
            if 0:
                print '-----------'
                print flat_names[template_pos]
                print flat_template[template_pos]
                print s_val
                print db_idx
                print db_val

            results_ii = [results_by_TBA_id[i] for i in db_idx
                    if i in trial_TBA_ids]
            coords_ii = [v for i, v in zip(db_idx, db_val)
                    if i in trial_TBA_ids]
            time_ii = [TBA_id_timestep[i] for i in db_idx
                    if i in trial_TBA_ids]
            plt.title(flat_names[template_pos], fontsize=10)
            plt.scatter(time_ii, coords_ii,
                    c=map(color_fn_bw, results_ii))

        else:
            print '-----------'
            print flat_names[template_pos]
            print flat_template[template_pos]
            print s_val
            print db_idx
            print db_val
    plt.show()

if __name__ == '__main__':
    cmd = sys.argv[1]
    save_loc = sys.argv[2]
    self = cPickle.load(open(save_loc, 'rb'))<commit_msg>enabled plotting of uniform and normal quantities<commit_after>"""
Functions to visualize an Experiment.

"""

__authors__   = "<NAME>"
__copyright__ = "(c) 2010, Universite de Montreal"
__license__   = "3-clause BSD License"
__contact__   = "<NAME> <<EMAIL>>"

import cPickle
import math
import sys

import matplotlib.pyplot as plt
import numpy

import ht_dist2

def main_plot_history(self):
    """
    self - experiment
    """
    # self is an Experiment
    status_colors = {'new':'k', 'running':'g', 'ok':'b', 'fail':'r'}
    Xs = self.trials

    # XXX: show the un-finished or error trials
    Ys, colors = zip(*[(y, status_colors[s])
        for y, s in zip(self.losses(), self.statuses()) if y is not None])
    plt.scatter(range(len(Ys)), Ys, c=colors)
    plt.xlabel('time')
    plt.ylabel('loss')
    try:
        loss_target = self.bandit.loss_target()
        have_losstarget = True
    except NotImplementedError:
        loss_target = numpy.min(Ys)
        have_losstarget = False
    if have_losstarget:
        plt.axhline(loss_target)
        ymin = min(numpy.min(Ys), loss_target)
        ymax = max(numpy.max(Ys), loss_target)
        yrange = ymax - ymin
        ymean = (ymax + ymin) / 2.0
        plt.ylim(
                ymean - 0.53 * yrange,
                ymean + 0.53 * yrange,
                )
    if 1:
        plt.axhline(self.average_best_error(), c='g')
    plt.title('bandit: %s algo: %s' % (
        self.bandit.short_str(),
        self.bandit_algo.short_str()))
    plt.show()


def main_plot_vars(self):
    import montetheano.rstreams
    # this requires a TheanoBanditAlgo, which has the sample history recorded as
    # idxs and vals
    bandit_algo = self.bandit_algo
    bandit = bandit_algo.bandit

    try:
        flat_template = bandit.template.flatten()
        flat_names = [n[1:] for n in bandit.template.flatten_names(prefix="")]
    except:
        # these are used for printing and labeling subplots
        # the code above doesn't work with GenSON
        # XXX
        flat_template = [None] * 100
        flat_names = ['name not found'] * 100


    TBA_ids = set()
    for db_idx in bandit_algo.db_idxs:
        TBA_ids.update(db_idx)
    trial_TBA_ids = set([t['_config_id'] for t in self.trials])
    skip_ids = [t for t in TBA_ids if t not in trial_TBA_ids]
    print 'Skipping %i jobs not in the official trials: %s' % (
            len(skip_ids), str(skip_ids))

    # at which point in `trials` was a given TBA_id inserted?
    TBA_id_timestep = dict([(t['_config_id'], i) for i, t in enumerate(self.trials)])

    results_by_TBA_id = dict([
        (t['_config_id'], bandit.loss(r, config=t))
        for (t, r) in zip(self.trials, self.results)
        ])

    loss_min = min([y for y in self.losses() if y is not None])
    loss_max = max([y for y in self.losses() if y is not None])

    def color_fn(lossval):
        if lossval is None:
            return (1, 1, 1)
        else:
            t = 4 * (lossval - loss_min) / (loss_max - loss_min + .0001)
            if t < 1:
                return t, 0, 0
            if t < 2:
                return 2-t, t-1, 0
            if t < 3:
                return 0, 3-t, t-2
            return 0, 0, 4-t

    def color_fn_bw(lossval):
        if lossval is None:
            return (1, 1, 1)
        else:
            t = (lossval - loss_min) / (loss_max - loss_min + .0001)
            return (t, t, t)

    C = 5
    R = int(numpy.ceil(len(bandit_algo.s_vals) / float(C)))

    for ii, (s_val, db_idx, db_val, template_pos) in enumerate(zip(
            bandit_algo.s_vals,
            bandit_algo.db_idxs,
            bandit_algo.db_vals,
            bandit_algo.all_s_locs)):

        plt.subplot(R, C, ii + 1)

        # hide x ticks
        ticks_num, ticks_txt = plt.xticks()
        plt.xticks(ticks_num, ['' for i in xrange(len(ticks_num))])

        try:
            dist_name = montetheano.rstreams.rv_dist_name(s_val)
        except:
            print >> sys.stderr, 'problem with', s_val
            raise
        if dist_name in ('lognormal', 'quantized_lognormal'):
            if 0:
                print '-----------'
                print flat_names[template_pos]
                print flat_template[template_pos]
                print s_val
                print db_idx
                print db_val

            results_ii = [results_by_TBA_id[i] for i in db_idx
                    if i in trial_TBA_ids]
            coords_ii = numpy.log([v for i, v in zip(db_idx, db_val)
                    if i in trial_TBA_ids])
            time_ii = [TBA_id_timestep[i] for i in db_idx
                    if i in trial_TBA_ids]

            plt.title(flat_names[template_pos], fontsize=10)
            plt.scatter(time_ii, coords_ii,
                    c=map(color_fn_bw, results_ii))
        elif dist_name in ('categorical', 'uniform', 'normal'):
            if 0:
                print '-----------'
                print flat_names[template_pos]
                print flat_template[template_pos]
                print s_val
                print db_idx
                print db_val

            results_ii = [results_by_TBA_id[i] for i in db_idx
                    if i in trial_TBA_ids]
            coords_ii = [v for i, v in zip(db_idx, db_val)
                    if i in trial_TBA_ids]
            time_ii = [TBA_id_timestep[i] for i in db_idx
                    if i in trial_TBA_ids]
            plt.title(flat_names[template_pos], fontsize=10)
            plt.scatter(time_ii, coords_ii,
                    c=map(color_fn_bw, results_ii))

        else:
            print '-----------'
            print flat_names[template_pos]
            print flat_template[template_pos]
            print s_val
            print db_idx
            print db_val
    plt.show()

if __name__ == '__main__':
    cmd = sys.argv[1]
    save_loc = sys.argv[2]
    self = cPickle.load(open(save_loc, 'rb'))
{'fraction_non_alphanumeric': 0.10908778560384813, 'fraction_numerical': 0.010221611406974746, 'mean_word_length': 1.8198110922741584, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 15, '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': '5132627', 'n_tokens_mistral': 4097, 'n_tokens_neox': 3887, 'n_words': 997}