<commit_before>#!/usr/bin/env python
# -*- coding: utf-8 -*-

import glob
import os
import imp
import core

def start_interfaces(interfaces):
    #read config
    #import Interfaces according to config
    for i in interfaces:
        i.start()

def import_plugins(interfaces):

    core.log(interfaces)
    #list files in /plugin folder
    filenames = glob.glob("plugins/*_plugin.py")
    core.log(interfaces, name, "Importing Plugins: \n" + str(filenames))

    for i in range(0,len(filenames)):
        core.log(interfaces)
        core.log(interfaces, name, "Found %s" % (filenames[i]))
        #new_plugin = "samplugin" + str(i)
        try:
            new_plugin = imp.load_source("samplugin" + str(i), filenames[i])
            #import new_plugin
            core.log(interfaces, name, "%s imported successfully." % (filenames[i]))
            try:
                if new_plugin.is_sam_plugin:
                    plugins.append(new_plugin)
                    core.log(interfaces, name, "  Name:\t\t" + new_plugin.name)
                    core.log(interfaces, name, "  Keywords:\t" + str(new_plugin.keywords))
                    new_plugin.initialize(interfaces)
            except AttributeError:
                core.log(interfaces, name, "%s is not a valid Plugin." % (filenames[i]))
        except ImportError:
            core.log(interfaces, name, "%s could not be imported successfully." % (filenames[i]))
        core.log(interfaces)
    return plugins

def output(interfaces, content):
    for i in interfaces:
        i.output(content)

def set_config(interfaces, key, value):
    #read config file
    for l in config_file:
        if key in l:
            pass
            #replace with key:value

if __name__ == "__main__":
    name = "Mainframe"
    interfaces = []
    plugins = []
    core.log(interfaces)
    core.log(interfaces, name, "Starting up!")
    path = os.getcwd()
    #import interfaces
    start_interfaces(interfaces=interfaces)
    plugins = import_plugins(interfaces=interfaces)
    core.log(interfaces, name, "Plugins imported")
    core.log(interfaces)
    core.log(interfaces, name, "Interfaces started")

'''
def set_something(plugin_keyword, object, target_state):
    for p in plugins:
        if keyword in p.keywords and p.has_set:
            result = p.set(object, target_state)
            if result == 1
                pass
            else
                print("Error from {name}: {description}".format(name=p.name, description=result))

def get_something(plugin_keyword, object, target_state):
    for p in plugins:
        if keyword in p.keywords and p.has_set:
            result = p.set(object, target_state)
            if result == -1
                print("Error from {name}: {description}".format(name=p.name, description=result))
            else
                #process Result, could be path to a file, link, etc
                pass

def toggle_something(plugin_keyword, object):
    for p in plugins:
        if keyword in p.keywords and p.has_toggle:
            result = p.toggle(object)
            if result == 1
                pass
            else
                print("Error from {name}: {description}".format(name=p.name, description=result))
'''<commit_msg>cleaned the code<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-

#import libraries
import glob, os, imp
#import Sam-related things
import core
import global_variables as VARS

def start_interfaces(interfaces):
    #read config
    #import Interfaces according to config
    for i in interfaces:
        i.start()

def import_plugins(interfaces):
    """
    Function to import plugins from the /plugins folder. Valid plugins are marked by <name>.is_sam_plugin == 1.
    """
    #list files in /plugin folder
    filenames = glob.glob("plugins/*_plugin.py")
    core.log(interfaces)
    core.log(interfaces, name, "Importing Plugins: \n" + str(filenames))

    #try importing each plugin
    for i in range(0,len(filenames)):
        core.log(interfaces)
        core.log(interfaces, name, "Found %s" % (filenames[i]))
        try:
            new_plugin = imp.load_source("samplugin" + str(i), filenames[i])
            core.log(interfaces, name, "%s imported successfully." % (filenames[i]))
            #Test if the imported file is a valid Plugin
            try:
                if new_plugin.is_sam_plugin:
                    plugins.append(new_plugin)
                    core.log(interfaces, name, "  Name:\t\t" + new_plugin.name)
                    core.log(interfaces, name, "  Keywords:\t" + str(new_plugin.keywords))
                    new_plugin.initialize(interfaces)
            except AttributeError:
                core.log(interfaces, name, "%s is not a valid Plugin." % (filenames[i]))
        except ImportError:
            core.log(interfaces, name, "%s could not be imported successfully." % (filenames[i]))
        core.log(interfaces)
    return plugins

def output(interfaces, content):
    for i in interfaces:
        i.output(content)

def set_config(interfaces, key, value):
    #read config file
    for l in config_file:
        if key in l:
            pass
            #replace with key:value

if __name__ == "__main__":
    name = "Mainframe"
    interfaces = []
    plugins = []

    core.log(interfaces)
    core.log(interfaces, name, "Starting up!")

    #import interfaces
    start_interfaces(interfaces=interfaces)
    plugins = import_plugins(interfaces=interfaces)

    core.log(interfaces, name, "Plugins imported")
    core.log(interfaces)
    core.log(interfaces, name, "Interfaces started")

'''
def set_something(plugin_keyword, object, target_state):
    for p in plugins:
        if keyword in p.keywords and p.has_set:
            result = p.set(object, target_state)
            if result == 1
                pass
            else
                print("Error from {name}: {description}".format(name=p.name, description=result))

def get_something(plugin_keyword, object, target_state):
    for p in plugins:
        if keyword in p.keywords and p.has_set:
            result = p.set(object, target_state)
            if result == -1
                print("Error from {name}: {description}".format(name=p.name, description=result))
            else
                #process Result, could be path to a file, link, etc
                pass

def toggle_something(plugin_keyword, object):
    for p in plugins:
        if keyword in p.keywords and p.has_toggle:
            result = p.toggle(object)
            if result == 1
                pass
            else
                print("Error from {name}: {description}".format(name=p.name, description=result))
'''<|endoftext|>


<commit_before>from scipy.stats import norm
from sklearn.gaussian_process import GaussianProcessRegressor
from sklearn.gaussian_process.kernels import RBF, Matern

from experiment_groups.search_managers.utils import get_random_generator
from polyaxon_schemas.settings import GaussianProcessConfig, UtilityFunctionConfig
from polyaxon_schemas.utils import AcquisitionFunctions, GaussianProcessesKernels

class UtilityFunction(object):
    UCB = 'ucb'
    EI = 'ei'
    POI = 'poi'
    FUNCTIONS = {UCB, EI, POI}

    def __init__(self, config, seed=None):
        if not isinstance(config, UtilityFunctionConfig):
            raise ValueError('Received a non valid configuration.')

        self.config = config
        self.xi = config.xi
        self.kappa = config.kappa
        self.acquisition_function = config.acquisition_function
        self.gaussian_process = self.get_gaussian_process(config=config.gaussian_process, seed=seed)

    @staticmethod
    def get_gaussian_process(config, seed=None):
        if not isinstance(config, GaussianProcessConfig):
            raise ValueError('Received a non valid configuration.')

        random_generator = get_random_generator(seed=seed)

        if GaussianProcessesKernels.is_rbf(config.kernel):
            kernel = RBF(length_scale=config.length_scale)
        else:
            kernel = Matern(length_scale=config.length_scale,
                            nu=config.nu)

        return GaussianProcessRegressor(
            kernel=kernel,
            n_restarts_optimizer=config.n_restarts_optimizer,
            random_state=random_generator
        )

    def compute(self, x, y_max):
        if AcquisitionFunctions.is_ucb(self.acquisition_function):
            return self.compute_ucb(x=x)
        if AcquisitionFunctions.is_ei(self.acquisition_function):
            return self.compute_ei(x=x, y_max=y_max)
        if AcquisitionFunctions.is_poi(self.acquisition_function):
            return self.compute_poi(x=x, y_max=y_max)

    def compute_ucb(self, x):
        mean, std = self.gaussian_process.predict(x, return_std=True)
        return mean + self.kappa * std

    def compute_ei(self, x, y_max):
        mean, std = self.gaussian_process.predict(x, return_std=True)
        z = (mean - y_max - self.xi) / std
        return (mean - y_max - self.xi) * norm.cdf(z) + std * norm.pdf(z)

    def compute_poi(self, x, y_max):
        mean, std = self.gaussian_process.predict(x, return_std=True)
        z = (mean - y_max - self.xi) / std
        return norm.cdf(z)
<commit_msg>Rename xi to eps<commit_after>from scipy.stats import norm
from sklearn.gaussian_process import GaussianProcessRegressor
from sklearn.gaussian_process.kernels import RBF, Matern

from experiment_groups.search_managers.utils import get_random_generator
from polyaxon_schemas.settings import GaussianProcessConfig, UtilityFunctionConfig
from polyaxon_schemas.utils import AcquisitionFunctions, GaussianProcessesKernels

class UtilityFunction(object):
    UCB = 'ucb'
    EI = 'ei'
    POI = 'poi'
    FUNCTIONS = {UCB, EI, POI}

    def __init__(self, config, seed=None):
        if not isinstance(config, UtilityFunctionConfig):
            raise ValueError('Received a non valid configuration.')

        self.config = config
        self.eps = config.eps
        self.kappa = config.kappa
        self.acquisition_function = config.acquisition_function
        self.gaussian_process = self.get_gaussian_process(config=config.gaussian_process, seed=seed)

    @staticmethod
    def get_gaussian_process(config, seed=None):
        if not isinstance(config, GaussianProcessConfig):
            raise ValueError('Received a non valid configuration.')

        random_generator = get_random_generator(seed=seed)

        if GaussianProcessesKernels.is_rbf(config.kernel):
            kernel = RBF(length_scale=config.length_scale)
        else:
            kernel = Matern(length_scale=config.length_scale,
                            nu=config.nu)

        return GaussianProcessRegressor(
            kernel=kernel,
            n_restarts_optimizer=config.n_restarts_optimizer,
            random_state=random_generator
        )

    def compute(self, x, y_max):
        if AcquisitionFunctions.is_ucb(self.acquisition_function):
            return self.compute_ucb(x=x)
        if AcquisitionFunctions.is_ei(self.acquisition_function):
            return self.compute_ei(x=x, y_max=y_max)
        if AcquisitionFunctions.is_poi(self.acquisition_function):
            return self.compute_poi(x=x, y_max=y_max)

    def compute_ucb(self, x):
        mean, std = self.gaussian_process.predict(x, return_std=True)
        return mean + self.kappa * std

    def compute_ei(self, x, y_max):
        mean, std = self.gaussian_process.predict(x, return_std=True)
        z = (mean - y_max - self.eps) / std
        return (mean - y_max - self.eps) * norm.cdf(z) + std * norm.pdf(z)

    def compute_poi(self, x, y_max):
        mean, std = self.gaussian_process.predict(x, return_std=True)
        z = (mean - y_max - self.eps) / std
        return norm.cdf(z)
<|endoftext|>


<commit_before>import os
from django.conf import settings
from django.db import models
from django.utils.translation import ugettext_lazy as _
from django.utils import simplejson as json
from django.utils.datastructures import MultiValueDict
from django.contrib.auth.models import User

class PackageInfoField(models.Field):
    description = u'Python Package Information Field'
    __metaclass__ = models.SubfieldBase

    def __init__(self, *args, **kwargs):
        kwargs['editable'] = False
        super(PackageInfoField,self).__init__(*args, **kwargs)

    def to_python(self, value):
        if isinstance(value, basestring):
            if value:
                return MultiValueDict(json.loads(value))
            else:
                return MultiValueDict()
        if isinstance(value, dict):
            return MultiValueDict(value)
        if isinstance(value,MultiValueDict):
            return value
        raise ValueError('Unexpected value encountered when converting data to python')

    def get_prep_value(self, value):
        if isinstance(value,MultiValueDict):
            return json.dumps(dict(value.iterlists()))
        if isinstance(value, dict):
            return json.dumps(value)
        if isinstance(value, basestring) or value is None:
            return value

        raise ValueError('Unexpected value encountered when preparing for database')

    def get_internal_type(self):
        return 'TextField'

class Classifier(models.Model):
    name = models.CharField(max_length=255, primary_key=True)

    class Meta:
        verbose_name = _(u"classifier")
        verbose_name_plural = _(u"classifiers")
        ordering = ('name',)

    def __unicode__(self):
        return self.name

class Package(models.Model):
    name = models.CharField(max_length=255, unique=True, primary_key=True,
                            editable=False)
    auto_hide = models.BooleanField(default=True, blank=False)
    allow_comments = models.BooleanField(default=True, blank=False)
    owners = models.ManyToManyField(User, blank=True,
                                    related_name="packages_owned")
    maintainers = models.ManyToManyField(User, blank=True,
                                         related_name="packages_maintained")

    class Meta:
        verbose_name = _(u"package")
        verbose_name_plural = _(u"packages")

    def __unicode__(self):
        return self.name

    @models.permalink
    def get_absolute_url(self):
        return ('djangopypi-package', (), {'package': self.name})

    @property
    def latest(self):
        try:
            return self.releases.latest()
        except Release.DoesNotExist:
            return None

    def get_release(self, version):
        """Return the release object for version, or None"""
        try:
            return self.releases.get(version=version)
        except Release.DoesNotExist:
            return None

class Release(models.Model):
    package = models.ForeignKey(Package, related_name="releases", editable=False)
    version = models.CharField(max_length=128, editable=False)
    metadata_version = models.CharField(max_length=64, default='1.0')
    package_info = PackageInfoField(blank=False)
    hidden = models.BooleanField(default=False)
    created = models.DateTimeField(auto_now_add=True, editable=False)

    class Meta:
        verbose_name = _(u"release")
        verbose_name_plural = _(u"releases")
        unique_together = ("package", "version")
        get_latest_by = 'created'
        ordering = ['-created']

    def __unicode__(self):
        return self.release_name

    @property
    def release_name(self):
        return u"%s-%s" % (self.package.name, self.version)

    @property
    def summary(self):
        return self.package_info.get('summary', u'')

    @property
    def description(self):
        return self.package_info.get('description', u'')

    @property
    def classifiers(self):
        return self.package_info.getlist('classifier')

    @models.permalink
    def get_absolute_url(self):
        return ('djangopypi-release', (), {'package': self.package.name,
                                           'version': self.version})

class Distribution(models.Model):
    release = models.ForeignKey(Release, related_name="distributions",
                                editable=False)
    content = models.FileField(upload_to=settings.DJANGOPYPI_RELEASE_UPLOAD_TO)
    md5_digest = models.CharField(max_length=32, blank=True, editable=False)
    filetype = models.CharField(max_length=32, blank=False,
                                choices=settings.DJANGOPYPI_DIST_FILE_TYPES)
    pyversion = models.CharField(max_length=16, blank=True,
                                 choices=settings.DJANGOPYPI_PYTHON_VERSIONS)
    comment = models.CharField(max_length=255, blank=True)
    signature = models.TextField(blank=True)
    created = models.DateTimeField(auto_now_add=True, editable=False)
    uploader = models.ForeignKey(User, editable=False)

    @property
    def filename(self):
        return os.path.basename(self.content.name)

    @property
    def display_filetype(self):
        for key,value in settings.DJANGOPYPI_DIST_FILE_TYPES:
            if key == self.filetype:
                return value
        return self.filetype

    @property
    def path(self):
        return self.content.name

    def get_absolute_url(self):
        return "%s#md5=%s" % (self.content.url, self.md5_digest)

    class Meta:
        verbose_name = _(u"distribution")
        verbose_name_plural = _(u"distributions")
        unique_together = ("release", "filetype", "pyversion")

    def __unicode__(self):
        return self.filename

class Review(models.Model):
    release = models.ForeignKey(Release, related_name="reviews")
    rating = models.PositiveSmallIntegerField(blank=True)
    comment = models.TextField(blank=True)

    class Meta:
        verbose_name = _(u'release review')
        verbose_name_plural = _(u'release reviews')

try:
    from south.modelsinspector import add_introspection_rules
    add_introspection_rules([], ["^djangopypi\.models\.PackageInfoField"])
except ImportError:
    pass
<commit_msg>Added some ordering to the package model and set the get latest by<commit_after>import os
from django.conf import settings
from django.db import models
from django.utils.translation import ugettext_lazy as _
from django.utils import simplejson as json
from django.utils.datastructures import MultiValueDict
from django.contrib.auth.models import User

class PackageInfoField(models.Field):
    description = u'Python Package Information Field'
    __metaclass__ = models.SubfieldBase

    def __init__(self, *args, **kwargs):
        kwargs['editable'] = False
        super(PackageInfoField,self).__init__(*args, **kwargs)

    def to_python(self, value):
        if isinstance(value, basestring):
            if value:
                return MultiValueDict(json.loads(value))
            else:
                return MultiValueDict()
        if isinstance(value, dict):
            return MultiValueDict(value)
        if isinstance(value,MultiValueDict):
            return value
        raise ValueError('Unexpected value encountered when converting data to python')

    def get_prep_value(self, value):
        if isinstance(value,MultiValueDict):
            return json.dumps(dict(value.iterlists()))
        if isinstance(value, dict):
            return json.dumps(value)
        if isinstance(value, basestring) or value is None:
            return value

        raise ValueError('Unexpected value encountered when preparing for database')

    def get_internal_type(self):
        return 'TextField'

class Classifier(models.Model):
    name = models.CharField(max_length=255, primary_key=True)

    class Meta:
        verbose_name = _(u"classifier")
        verbose_name_plural = _(u"classifiers")
        ordering = ('name',)

    def __unicode__(self):
        return self.name

class Package(models.Model):
    name = models.CharField(max_length=255, unique=True, primary_key=True,
                            editable=False)
    auto_hide = models.BooleanField(default=True, blank=False)
    allow_comments = models.BooleanField(default=True, blank=False)
    owners = models.ManyToManyField(User, blank=True,
                                    related_name="packages_owned")
    maintainers = models.ManyToManyField(User, blank=True,
                                         related_name="packages_maintained")

    class Meta:
        verbose_name = _(u"package")
        verbose_name_plural = _(u"packages")
        get_latest_by = "releases__latest"
        ordering = ['name',]

    def __unicode__(self):
        return self.name

    @models.permalink
    def get_absolute_url(self):
        return ('djangopypi-package', (), {'package': self.name})

    @property
    def latest(self):
        try:
            return self.releases.latest()
        except Release.DoesNotExist:
            return None

    def get_release(self, version):
        """Return the release object for version, or None"""
        try:
            return self.releases.get(version=version)
        except Release.DoesNotExist:
            return None

class Release(models.Model):
    package = models.ForeignKey(Package, related_name="releases", editable=False)
    version = models.CharField(max_length=128, editable=False)
    metadata_version = models.CharField(max_length=64, default='1.0')
    package_info = PackageInfoField(blank=False)
    hidden = models.BooleanField(default=False)
    created = models.DateTimeField(auto_now_add=True, editable=False)

    class Meta:
        verbose_name = _(u"release")
        verbose_name_plural = _(u"releases")
        unique_together = ("package", "version")
        get_latest_by = 'created'
        ordering = ['-created']

    def __unicode__(self):
        return self.release_name

    @property
    def release_name(self):
        return u"%s-%s" % (self.package.name, self.version)

    @property
    def summary(self):
        return self.package_info.get('summary', u'')

    @property
    def description(self):
        return self.package_info.get('description', u'')

    @property
    def classifiers(self):
        return self.package_info.getlist('classifier')

    @models.permalink
    def get_absolute_url(self):
        return ('djangopypi-release', (), {'package': self.package.name,
                                           'version': self.version})

class Distribution(models.Model):
    release = models.ForeignKey(Release, related_name="distributions",
                                editable=False)
    content = models.FileField(upload_to=settings.DJANGOPYPI_RELEASE_UPLOAD_TO)
    md5_digest = models.CharField(max_length=32, blank=True, editable=False)
    filetype = models.CharField(max_length=32, blank=False,
                                choices=settings.DJANGOPYPI_DIST_FILE_TYPES)
    pyversion = models.CharField(max_length=16, blank=True,
                                 choices=settings.DJANGOPYPI_PYTHON_VERSIONS)
    comment = models.CharField(max_length=255, blank=True)
    signature = models.TextField(blank=True)
    created = models.DateTimeField(auto_now_add=True, editable=False)
    uploader = models.ForeignKey(User, editable=False)

    @property
    def filename(self):
        return os.path.basename(self.content.name)

    @property
    def display_filetype(self):
        for key,value in settings.DJANGOPYPI_DIST_FILE_TYPES:
            if key == self.filetype:
                return value
        return self.filetype

    @property
    def path(self):
        return self.content.name

    def get_absolute_url(self):
        return "%s#md5=%s" % (self.content.url, self.md5_digest)

    class Meta:
        verbose_name = _(u"distribution")
        verbose_name_plural = _(u"distributions")
        unique_together = ("release", "filetype", "pyversion")

    def __unicode__(self):
        return self.filename

class Review(models.Model):
    release = models.ForeignKey(Release, related_name="reviews")
    rating = models.PositiveSmallIntegerField(blank=True)
    comment = models.TextField(blank=True)

    class Meta:
        verbose_name = _(u'release review')
        verbose_name_plural = _(u'release reviews')

try:
    from south.modelsinspector import add_introspection_rules
    add_introspection_rules([], ["^djangopypi\.models\.PackageInfoField"])
except ImportError:
    pass
<|endoftext|>


<commit_before>import copy
import os
import warnings

import chainer

from chainercv.visualizations.vis_bbox import vis_bbox

try:
    from matplotlib import pyplot as plot
    _available = True

except ImportError:
    _available = False

def _check_available():
    if not _available:
        warnings.warn('matplotlib is not installed on your environment, '
                      'so nothing will be plotted at this time. '
                      'Please install matplotlib to plot figures.\n\n'
                      '  $ pip install matplotlib\n')

class DetectionVisReport(chainer.training.extension.Extension):

    """An extension that visualizes output of a detection model.

    This extension visualizes the predicted bounding boxes together with the
    ground truth bounding boxes.

    Internally, this extension takes examples from an iterator,
    predict bounding boxes from the images in the examples,
    and visualizes them using :meth:`chainercv.visualizations.vis_bbox`.
    The process can be illustrated in the following code.

    .. code:: python

        batch = next(iterator)
        # Convert batch -> imgs, gt_bboxes, gt_labels
        pred_bboxes, pred_labels, pred_scores = target.predict(imgs)
        # Visualization code
        for img, gt_bbox, gt_label, pred_bbox, pred_label, pred_score \\
                in zip(imgs, gt_boxes, gt_labels,
                       pred_bboxes, pred_labels, pred_scores):
            # the ground truth
            vis_bbox(img, gt_bbox, gt_label)
            # the prediction
            vis_bbox(img, pred_bbox, pred_label, pred_score)

    .. note::
        :obj:`gt_bbox` and :obj:`pred_bbox` are float arrays
        of shape :math:`(R, 4)`, where :math:`R` is the number of
        bounding boxes in the image. Each bounding box is organized
        by :obj:`(x_min, y_min, x_max, y_max)` in the second axis.

        :obj:`gt_label` and :obj:`pred_label` are intenger arrays
        of shape :math:`(R,)`. Each label indicates the class of
        the bounding box.

        :obj:`pred_score` is a float array of shape :math:`(R,)`.
        Each score indicates how confident the prediction is.

    Args:
        iterator: Iterator object that produces images and ground truth.
        target: Link object used for detection.
        label_names (iterable of str): Name of labels ordered according
            to label ids. If this is :obj:`None`, labels will be skipped.
        filename (str): Basename for the saved image. It can contain two
            keywords, :obj:`'{iteration}'` and :obj:`'{index}'`. They are
            replaced with the iteration of the trainer and the index of
            the sample when this extension save an image. The default value is
            :obj:`'detection_iter={iteration}_idx={index}.jpg'`.
    """

    invoke_before_training = False

    def __init__(
            self, iterator, target, label_names=None,
            filename='detection_iter={iteration}_idx={index}.jpg'):
        _check_available()

        self.iterator = iterator
        self.target = target
        self.label_names = label_names
        self.filename = filename

    @staticmethod
    def available():
        _check_available()
        return _available

    def __call__(self, trainer):
        if not _available:
            return

        if hasattr(self.iterator, 'reset'):
            self.iterator.reset()
            it = self.iterator
        else:
            it = copy.copy(self.iterator)

        idx = 0
        while True:
            try:
                batch = next(it)
            except StopIteration:
                break

            imgs = [img for img, _, _ in batch]
            pred_bboxes, pred_labels, pred_scores = self.target.predict(imgs)

            for (img, gt_bbox, gt_label), pred_bbox, pred_label, pred_score \
                    in zip(batch, pred_bboxes, pred_labels, pred_scores):

                pred_bbox = chainer.cuda.to_cpu(pred_bbox)
                pred_label = chainer.cuda.to_cpu(pred_label)
                pred_score = chainer.cuda.to_cpu(pred_score)

                out_file = self.filename.format(
                    index=idx, iteration=trainer.updater.iteration)
                out_file = os.path.join(trainer.out, out_file)

                fig = plot.figure()

                ax_gt = fig.add_subplot(2, 1, 1)
                ax_gt.set_title('ground truth')
                vis_bbox(
                    img, gt_bbox, gt_label,
                    label_names=self.label_names, ax=ax_gt)

                ax_pred = fig.add_subplot(2, 1, 2)
                ax_pred.set_title('prediction')
                vis_bbox(
                    img, pred_bbox, pred_label, pred_score,
                    label_names=self.label_names, ax=ax_pred)

                plot.savefig(out_file)
                plot.close()

                idx += 1
<commit_msg>remove invoke_before_training<commit_after>import copy
import os
import warnings

import chainer

from chainercv.visualizations.vis_bbox import vis_bbox

try:
    from matplotlib import pyplot as plot
    _available = True

except ImportError:
    _available = False

def _check_available():
    if not _available:
        warnings.warn('matplotlib is not installed on your environment, '
                      'so nothing will be plotted at this time. '
                      'Please install matplotlib to plot figures.\n\n'
                      '  $ pip install matplotlib\n')

class DetectionVisReport(chainer.training.extension.Extension):

    """An extension that visualizes output of a detection model.

    This extension visualizes the predicted bounding boxes together with the
    ground truth bounding boxes.

    Internally, this extension takes examples from an iterator,
    predict bounding boxes from the images in the examples,
    and visualizes them using :meth:`chainercv.visualizations.vis_bbox`.
    The process can be illustrated in the following code.

    .. code:: python

        batch = next(iterator)
        # Convert batch -> imgs, gt_bboxes, gt_labels
        pred_bboxes, pred_labels, pred_scores = target.predict(imgs)
        # Visualization code
        for img, gt_bbox, gt_label, pred_bbox, pred_label, pred_score \\
                in zip(imgs, gt_boxes, gt_labels,
                       pred_bboxes, pred_labels, pred_scores):
            # the ground truth
            vis_bbox(img, gt_bbox, gt_label)
            # the prediction
            vis_bbox(img, pred_bbox, pred_label, pred_score)

    .. note::
        :obj:`gt_bbox` and :obj:`pred_bbox` are float arrays
        of shape :math:`(R, 4)`, where :math:`R` is the number of
        bounding boxes in the image. Each bounding box is organized
        by :obj:`(x_min, y_min, x_max, y_max)` in the second axis.

        :obj:`gt_label` and :obj:`pred_label` are intenger arrays
        of shape :math:`(R,)`. Each label indicates the class of
        the bounding box.

        :obj:`pred_score` is a float array of shape :math:`(R,)`.
        Each score indicates how confident the prediction is.

    Args:
        iterator: Iterator object that produces images and ground truth.
        target: Link object used for detection.
        label_names (iterable of str): Name of labels ordered according
            to label ids. If this is :obj:`None`, labels will be skipped.
        filename (str): Basename for the saved image. It can contain two
            keywords, :obj:`'{iteration}'` and :obj:`'{index}'`. They are
            replaced with the iteration of the trainer and the index of
            the sample when this extension save an image. The default value is
            :obj:`'detection_iter={iteration}_idx={index}.jpg'`.
    """

    def __init__(
            self, iterator, target, label_names=None,
            filename='detection_iter={iteration}_idx={index}.jpg'):
        _check_available()

        self.iterator = iterator
        self.target = target
        self.label_names = label_names
        self.filename = filename

    @staticmethod
    def available():
        _check_available()
        return _available

    def __call__(self, trainer):
        if not _available:
            return

        if hasattr(self.iterator, 'reset'):
            self.iterator.reset()
            it = self.iterator
        else:
            it = copy.copy(self.iterator)

        idx = 0
        while True:
            try:
                batch = next(it)
            except StopIteration:
                break

            imgs = [img for img, _, _ in batch]
            pred_bboxes, pred_labels, pred_scores = self.target.predict(imgs)

            for (img, gt_bbox, gt_label), pred_bbox, pred_label, pred_score \
                    in zip(batch, pred_bboxes, pred_labels, pred_scores):

                pred_bbox = chainer.cuda.to_cpu(pred_bbox)
                pred_label = chainer.cuda.to_cpu(pred_label)
                pred_score = chainer.cuda.to_cpu(pred_score)

                out_file = self.filename.format(
                    index=idx, iteration=trainer.updater.iteration)
                out_file = os.path.join(trainer.out, out_file)

                fig = plot.figure()

                ax_gt = fig.add_subplot(2, 1, 1)
                ax_gt.set_title('ground truth')
                vis_bbox(
                    img, gt_bbox, gt_label,
                    label_names=self.label_names, ax=ax_gt)

                ax_pred = fig.add_subplot(2, 1, 2)
                ax_pred.set_title('prediction')
                vis_bbox(
                    img, pred_bbox, pred_label, pred_score,
                    label_names=self.label_names, ax=ax_pred)

                plot.savefig(out_file)
                plot.close()

                idx += 1
<|endoftext|>


<commit_before>#
# ========================================================
# Copyright (c) 2012 Whamcloud, Inc.  All rights reserved.
# ========================================================

from selenium.common.exceptions import (
    NoSuchElementException, StaleElementReferenceException
)
from selenium.webdriver.support.ui import WebDriverWait

from tests.selenium.base import (
    element_visible, find_visible_element_by_css_selector,
    wait_for_any_element, wait_for_transition
)
from tests.selenium.base_view import BaseView

class EditFilesystem(BaseView):
    """
    Page Object for editing file system
    """
    def __init__(self, driver):
        super(EditFilesystem, self).__init__(driver)

        # Initialise elements on this page
        self.advanced_button = "div#filesystem_detail button.advanced"
        self.conf_param_apply_button = "button.conf_param_apply_button"
        self.conf_param_close_button = "button.conf_param_close_button"
        # Conf param apply button on pop-up dialog for MDT and OST
        self.target_conf_param_apply_button = "button.conf_param_apply"
        self.popup_container = "#popup_container"
        self.popup_message = "#popup_message"
        self.popup_ok = "#popup_ok"
        self.mdt_table = "mdt"
        self.ost_table = "ost"
        self.ost_data_table = "ost_content"
        self.mdt_data_table = "mdt_content"
        self.mgt_data_table = "example_content"
        self.popup_dialog_close_button = "button.close"

        self.config_param_tab = ""

    def set_state(self, state):
        button_box = self.driver.find_element_by_css_selector('td#fs_actions')
        self.click_command_button(button_box, state)
        self.quiesce()

    def open_target_conf_params(self, target_name):
        link = WebDriverWait(self.driver, self.standard_wait).until(lambda driver: driver.find_element_by_link_text(target_name))
        link.click()
        self.quiesce()
        self.config_param_tab = self.driver.find_element_by_css_selector('div.target_detail a[href="#target_dialog_config_param_tab"]')
        self.config_param_tab.click()

    def apply_conf_params(self):
        """Given that a conf param dialog is visible, click its apply button"""
        self.get_visible_element_by_css_selector(self.conf_param_apply_button).click()
        self.quiesce()

    def close_conf_params(self):
        """Given that a conf param dialog is visible, click its apply button"""
        self.get_visible_element_by_css_selector(self.conf_param_close_button).click()

    def apply_target_conf_params(self):
        """Given that a target detail dialog is visible, and on the conf params tab click its apply button"""
        self.get_visible_element_by_css_selector("div.target_detail button.conf_param_apply").click()
        self.quiesce()

    def close_target_conf_params(self):
        """Given that a target detail dialog is visible, and on the conf params tab click its apply button"""
        self.get_visible_element_by_css_selector("div.ui-dialog-buttonset button.close").click()
        # Closing the dialog is a history.back() operation so wait for the reload
        self.quiesce()

    def conf_param_dialog_visible(self):
        element = find_visible_element_by_css_selector(self.driver, self.conf_param_apply_button)
        return bool(element)

    def open_fs_conf_param_dialog(self):
        self.driver.find_element_by_css_selector("div#filesystem_detail button.advanced").click()
        wait_for_any_element(self.driver, self.conf_param_apply_button, self.medium_wait)

    def add_ost(self, primary_server_address, volume_name):
        """Click button to add new OST and select an OST/s from ost chooser"""
        # Open the ost chooser pop-up
        self.driver.find_element_by_css_selector('#btnNewOST').click()
        self.wait_for_element('#new_ost_chooser_table')

        # Click on the row that has the given volume name and primary server address
        try:
            self.volume_chooser_select('new_ost_chooser', primary_server_address, volume_name, True)
        except (StaleElementReferenceException, NoSuchElementException):
            # Hate doing this, but volume chooser could be reloaded right in the middle of this action,
            # and havent found a good way to detect this, so if it fails in a specific way, we try again.
            self.volume_chooser_select('new_ost_chooser', primary_server_address, volume_name, True)

        # Submit and wait for the add to complete
        self.driver.find_element_by_css_selector('#ost_ok_button').click()
        self.quiesce()
        wait_for_transition(self.driver, self.long_wait)

    def ost_set_state(self, target_name, state):
        return self._target_set_state('ost', target_name, state)

    def mdt_set_state(self, target_name, state):
        return self._target_set_state('mdt', target_name, state)

    def mgt_set_state(self, target_name, state):
        return self._target_set_state('mgt_configuration_view', target_name, state)

    def _target_set_state(self, table_id, target_name, state):
        self.log.info("Setting state %s on target %s in %s" % (state, target_name, table_id))
        row = self.find_row_by_column_text(
            self.driver.find_element_by_css_selector('table#%s' % table_id),
                {0: target_name})
        self.click_command_button(row, state)

    @property
    def filesystem_name(self):
        return self.driver.find_element_by_css_selector('span#fs_name').text

    def _get_target_volumes(self, table_id):
        return self.get_table_text(
            self.driver.find_element_by_css_selector("#%s" % table_id),
            [1, 2]
        )

    @property
    def mgt_volumes(self):
        return self._get_target_volumes('mgt_configuration_view')

    @property
    def mdt_volumes(self):
        return self._get_target_volumes('mdt')

    @property
    def ost_volumes(self):
        return self._get_target_volumes('ost')

    @property
    def visible(self):
        return element_visible(self.driver, 'div#filesystem-tab-detail')
<commit_msg>Selenium: Better fix for volume reload when adding an ost.<commit_after>#
# ========================================================
# Copyright (c) 2012 Whamcloud, Inc.  All rights reserved.
# ========================================================

from selenium.webdriver.support.ui import WebDriverWait

from tests.selenium.base import (
    element_visible, find_visible_element_by_css_selector,
    wait_for_any_element, wait_for_transition
)
from tests.selenium.base_view import BaseView

class EditFilesystem(BaseView):
    """
    Page Object for editing file system
    """
    def __init__(self, driver):
        super(EditFilesystem, self).__init__(driver)

        # Initialise elements on this page
        self.advanced_button = "div#filesystem_detail button.advanced"
        self.conf_param_apply_button = "button.conf_param_apply_button"
        self.conf_param_close_button = "button.conf_param_close_button"
        # Conf param apply button on pop-up dialog for MDT and OST
        self.target_conf_param_apply_button = "button.conf_param_apply"
        self.popup_container = "#popup_container"
        self.popup_message = "#popup_message"
        self.popup_ok = "#popup_ok"
        self.mdt_table = "mdt"
        self.ost_table = "ost"
        self.ost_data_table = "ost_content"
        self.mdt_data_table = "mdt_content"
        self.mgt_data_table = "example_content"
        self.popup_dialog_close_button = "button.close"

        self.config_param_tab = ""

    def set_state(self, state):
        button_box = self.driver.find_element_by_css_selector('td#fs_actions')
        self.click_command_button(button_box, state)
        self.quiesce()

    def open_target_conf_params(self, target_name):
        link = WebDriverWait(self.driver, self.standard_wait).until(lambda driver: driver.find_element_by_link_text(target_name))
        link.click()
        self.quiesce()
        self.config_param_tab = self.driver.find_element_by_css_selector('div.target_detail a[href="#target_dialog_config_param_tab"]')
        self.config_param_tab.click()

    def apply_conf_params(self):
        """Given that a conf param dialog is visible, click its apply button"""
        self.get_visible_element_by_css_selector(self.conf_param_apply_button).click()
        self.quiesce()

    def close_conf_params(self):
        """Given that a conf param dialog is visible, click its apply button"""
        self.get_visible_element_by_css_selector(self.conf_param_close_button).click()

    def apply_target_conf_params(self):
        """Given that a target detail dialog is visible, and on the conf params tab click its apply button"""
        self.get_visible_element_by_css_selector("div.target_detail button.conf_param_apply").click()
        self.quiesce()

    def close_target_conf_params(self):
        """Given that a target detail dialog is visible, and on the conf params tab click its apply button"""
        self.get_visible_element_by_css_selector("div.ui-dialog-buttonset button.close").click()
        # Closing the dialog is a history.back() operation so wait for the reload
        self.quiesce()

    def conf_param_dialog_visible(self):
        element = find_visible_element_by_css_selector(self.driver, self.conf_param_apply_button)
        return bool(element)

    def open_fs_conf_param_dialog(self):
        self.driver.find_element_by_css_selector("div#filesystem_detail button.advanced").click()
        wait_for_any_element(self.driver, self.conf_param_apply_button, self.medium_wait)

    def add_ost(self, primary_server_address, volume_name):
        """Click button to add new OST and select an OST/s from ost chooser"""
        # Open the ost chooser pop-up
        self.driver.find_element_by_css_selector('#btnNewOST').click()
        self.quiesce()

        # Click on the row that has the given volume name and primary server address
        self.volume_chooser_select('new_ost_chooser', primary_server_address, volume_name, True)

        # Submit and wait for the add to complete
        self.driver.find_element_by_css_selector('#ost_ok_button').click()
        self.quiesce()
        wait_for_transition(self.driver, self.long_wait)

    def ost_set_state(self, target_name, state):
        return self._target_set_state('ost', target_name, state)

    def mdt_set_state(self, target_name, state):
        return self._target_set_state('mdt', target_name, state)

    def mgt_set_state(self, target_name, state):
        return self._target_set_state('mgt_configuration_view', target_name, state)

    def _target_set_state(self, table_id, target_name, state):
        self.log.info("Setting state %s on target %s in %s" % (state, target_name, table_id))
        row = self.find_row_by_column_text(
            self.driver.find_element_by_css_selector('table#%s' % table_id),
                {0: target_name})
        self.click_command_button(row, state)

    @property
    def filesystem_name(self):
        return self.driver.find_element_by_css_selector('span#fs_name').text

    def _get_target_volumes(self, table_id):
        return self.get_table_text(
            self.driver.find_element_by_css_selector("#%s" % table_id),
            [1, 2]
        )

    @property
    def mgt_volumes(self):
        return self._get_target_volumes('mgt_configuration_view')

    @property
    def mdt_volumes(self):
        return self._get_target_volumes('mdt')

    @property
    def ost_volumes(self):
        return self._get_target_volumes('ost')

    @property
    def visible(self):
        return element_visible(self.driver, 'div#filesystem-tab-detail')
<|endoftext|>


<commit_before># coding=utf-8
from __future__ import unicode_literals

from ...symbols import ORTH, LEMMA

TOKENIZER_EXCEPTIONS = {}

ABBREVIATIONS = {
    "ডঃ": [
        {ORTH: "ডঃ", LEMMA: "ডক্টর"},
    ],
    "ডাঃ": [
        {ORTH: "ডাঃ", LEMMA: "ডাক্তার"},
    ],
    "ড.": [
        {ORTH: "ড.", LEMMA: "ডক্টর"},
    ],
    "ডা.": [
        {ORTH: "ডা.", LEMMA: "ডাক্তার"},
    ],
    "মোঃ": [
        {ORTH: "মোঃ", LEMMA: "মোহাম্মদ"},
    ],
    "মো.": [
        {ORTH: "মো.", LEMMA: "মোহাম্মদ"},
    ],
    "সে.": [
        {ORTH: "সে.", LEMMA: "সেলসিয়াস"},
    ],
    "কি.মি.": [
        {ORTH: "কি.মি.", LEMMA: "কিলোমিটার"},
    ],
    "কি.মি": [
            {ORTH: "কি.মি", LEMMA: "কিলোমিটার"},
    ],
    "সে.মি.": [
        {ORTH: "সে.মি.", LEMMA: "সেন্টিমিটার"},
    ],
    "সে.মি": [
            {ORTH: "সে.মি", LEMMA: "সেন্টিমিটার"},
    ],
    "মি.লি.": [
        {ORTH: "মি.লি.", LEMMA: "মিলিলিটার"},
    ]
}

TOKENIZER_EXCEPTIONS.update(ABBREVIATIONS)
<commit_msg>Tidy up Bengali tokenizer exceptions<commit_after># coding=utf-8
from __future__ import unicode_literals

from ...symbols import ORTH, LEMMA

_exc = {}

for exc_data in [
    {ORTH: "ডঃ", LEMMA: "ডক্টর"},
    {ORTH: "ডাঃ", LEMMA: "ডাক্তার"},
    {ORTH: "ড.", LEMMA: "ডক্টর"},
    {ORTH: "ডা.", LEMMA: "ডাক্তার"},
    {ORTH: "মোঃ", LEMMA: "মোহাম্মদ"},
    {ORTH: "মো.", LEMMA: "মোহাম্মদ"},
    {ORTH: "সে.", LEMMA: "সেলসিয়াস"},
    {ORTH: "কি.মি.", LEMMA: "কিলোমিটার"},
    {ORTH: "কি.মি", LEMMA: "কিলোমিটার"},
    {ORTH: "সে.মি.", LEMMA: "সেন্টিমিটার"},
    {ORTH: "সে.মি", LEMMA: "সেন্টিমিটার"},
    {ORTH: "মি.লি.", LEMMA: "মিলিলিটার"}]:
    _exc[exc_data[ORTH]] = [dict(exc_data)]

TOKENIZER_EXCEPTIONS = dict(_exc)
<|endoftext|>


<commit_before>"""
Standalone wrapper for the cheat.sh server.
"""

import sys
import textwrap

import cheat_wrapper

def show_usage():
    """
    Show how to use the program in the standalone mode
    """

    print textwrap.dedent("""
        Usage:

            lib/standalone.py [OPTIONS] QUERY

        For OPTIONS see :help
    """)[1:-1]

def parse_cmdline(args):
    """
    Parses command line arguments and returns
    query and request_options
    """

    if not args:
        show_usage()
        sys.exit(0)

    request_options = {}
    query = args[0]
    query = query.lstrip("/")
    if not query:
        query = ":firstpage"
    return query, request_options

def main(args):
    """
    standalone wrapper for cheat_wrapper()
    """

    query, request_options = parse_cmdline(args)
    answer, _ = cheat_wrapper.cheat_wrapper(query, request_options=request_options)
    sys.stdout.write(answer)

if __name__ == '__main__':
    main(sys.argv[1:])
<commit_msg>lib/standalone.py: output is UTF-8<commit_after>"""
Standalone wrapper for the cheat.sh server.
"""

import sys
import textwrap

import cheat_wrapper

def show_usage():
    """
    Show how to use the program in the standalone mode
    """

    print textwrap.dedent("""
        Usage:

            lib/standalone.py [OPTIONS] QUERY

        For OPTIONS see :help
    """)[1:-1]

def parse_cmdline(args):
    """
    Parses command line arguments and returns
    query and request_options
    """

    if not args:
        show_usage()
        sys.exit(0)

    request_options = {}
    query = args[0]
    query = query.lstrip("/")
    if not query:
        query = ":firstpage"
    return query, request_options

def main(args):
    """
    standalone wrapper for cheat_wrapper()
    """

    query, request_options = parse_cmdline(args)
    answer, _ = cheat_wrapper.cheat_wrapper(query, request_options=request_options)
    sys.stdout.write(answer.encode("utf-8"))

if __name__ == '__main__':
    main(sys.argv[1:])
<|endoftext|>


<commit_before>import numpy as np
import csv
import timeit

import chippr
from chippr import defaults as d
from chippr import utils as u
from chippr import sim_utils as su
from chippr import gauss

class catalog(object):

    def __init__(self, params=None, vb=True):
        """
        Object containing catalog of photo-z interim posteriors

        Parameters
        ----------
        params: dict or string, optional
            dictionary containing parameter values for catalog creation or string containing location of parameter file
        vb: boolean, optional
            True to print progress messages to stdout, False to suppress
        """
        if params is None:
            self.params = {}
        elif type(params) == str:
            self.params = su.ingest(params)
        else:
            self.params = params

        self.params = d.check_sim_params(self.params)

        if vb:
            print self.params

    def proc_bins(self, bins, limits=(d.min_x, d.max_x), vb=True):
        """
        Function to process binning

        Parameters
        ----------
        bins: int
            number of evenly spaced bins
        limits: tuple or list or numpy.ndarray, float, optional
            endpoints over which binning will be defined
        vb: boolean, optional
            True to print progress messages to stdout, False to suppress
        """
        if type(bins) == int:
            self.n_coarse = bins
        else:
            self.n_coarse = 10
        x_min = limits[0]#np.min(self.obs_samps)
        x_max = limits[-1]#np.max(self.obs_samps)
        self.n_fine = self.n_coarse
        self.n_tot = self.n_coarse * self.n_fine
        x_range = x_max-x_min

        self.dx_coarse = x_range / self.n_coarse
        self.dx_fine = x_range / self.n_tot

        self.x_coarse = np.arange(x_min+0.5*self.dx_coarse, x_max, self.dx_coarse)
        self.x_fine = np.arange(x_min+0.5*self.dx_fine, x_max, self.dx_fine)

        self.bin_ends = np.arange(x_min, x_max+self.dx_coarse, self.dx_coarse)

        return

    def coarsify(self, fine):
        """
        Function to bin function evaluated on fine grid

        Parameters
        ----------
        fine: numpy.ndarray, float
            vector of values of function on fine grid

        Returns
        -------
        coarse: numpy.ndarray, float
            vector of binned values of function
        """
        coarse = fine / (np.sum(fine) * self.dx_fine)
        coarse = np.array([np.sum(coarse[k * self.n_fine : (k+1) * self.n_fine]) * self.dx_fine for k in range(self.n_coarse)])
        coarse /= self.dx_coarse

        return coarse

    def create(self, truth, int_pr, bins=d.n_bins, vb=True):
        """
        Function creating a catalog of interim posterior probability distributions, will split this up into helper functions

        Parameters
        ----------
        truth: numpy.ndarray, float
            vector of true redshifts
        int_pr: chippr.gmix object or chippr.gauss object or chippr.binned object
            interim prior distribution object
        bins: int, optional
            number of evenly spaced bins

        Returns
        -------
        self.cat: dict
            dictionary comprising catalog information
        """
        self.true_samps = truth
        self.n_items = len(self.true_samps)
        self.samp_range = range(self.n_items)

        self.obs_samps = self.sample_obs()

        self.int_pr = int_pr
        self.proc_bins(bins, limits=(self.int_pr.bin_ends[0], self.int_pr.bin_ends[-1]))

        self.obs_lfs = self.evaluate_lfs()

        int_pr_fine = int_pr.evaluate(self.x_fine)
        int_pr_coarse = self.coarsify(int_pr_fine)

        pfs = np.zeros((self.n_items, self.n_coarse))
        for n in self.samp_range:
            pf = int_pr_fine * self.obs_lfs[n]
            pf = self.coarsify(pf)
            pfs[n] += pf

        self.cat = {}
        self.cat['bin_ends'] = self.bin_ends
        self.cat['log_interim_prior'] = u.safe_log(int_pr_coarse)
        self.cat['log_interim_posteriors'] = u.safe_log(pfs)

        return self.cat

    def sample_obs(self):
        """
        Samples observed values from true values

        Returns
        -------
        obs_samps: numpy.ndarray, float
            observed values
        """
        if not self.params['variable_sigma']:
            true_lfs = [gauss(self.true_samps[n], self.params['constant_sigma']**2) for n in self.samp_range]
        if self.params['catastrophic_outliers']:
            outlier_lf = gauss(self.params['outlier_mean'], self.params['outlier_sigma']**2)
        obs_samps = np.zeros(self.n_items)
        for n in self.samp_range:
            if np.random.uniform() < self.params['outlier_fraction']:
                obs_samps[n] = outlier_lf.sample_one()
            else:
                obs_samps[n] = true_lfs[n].sample_one()
        return obs_samps

    def evaluate_lfs(self):
        """
        Evaluates likelihoods based on observed sample values

        Returns
        -------
        obs_lfs: numpy.ndarray, float
            array of likelihood values for each item as a function of fine binning
        """
        if not self.params['variable_sigma']:
            lfs_fine = [gauss(self.x_fine[kk], self.params['constant_sigma']**2) for kk in range(self.n_tot)]
            obs_lfs = (1.-self.params['outlier_fraction']) * np.array([lfs_fine[kk].evaluate(self.obs_samps) for kk in range(self.n_tot)])
        if self.params['catastrophic_outliers']:
            outlier_lf = gauss(self.params['outlier_mean'], self.params['outlier_sigma']**2)
            obs_lfs += self.params['outlier_fraction'] * outlier_lf.evaluate(self.obs_samps)
        return obs_lfs.T

    def write(self, loc):
        """
        Function to write newly-created catalog to file

        Parameters
        ----------
        loc: string
            location into which to save catalog files
        """
        with open(loc, 'wb') as csvfile:
            out = csv.writer(csvfile, delimiter=' ')
            out.writerow(self.cat['bin_ends'])
            out.writerow(self.cat['log_interim_prior'])
            for line in self.cat['log_interim_posteriors']:
                out.writerow(line)
        return
<commit_msg>opening up option for different file types<commit_after>import numpy as np
import csv
import timeit

import chippr
from chippr import defaults as d
from chippr import utils as u
from chippr import sim_utils as su
from chippr import gauss

class catalog(object):

    def __init__(self, params=None, vb=True):
        """
        Object containing catalog of photo-z interim posteriors

        Parameters
        ----------
        params: dict or string, optional
            dictionary containing parameter values for catalog creation or string containing location of parameter file
        vb: boolean, optional
            True to print progress messages to stdout, False to suppress
        """
        if params is None:
            self.params = {}
        elif type(params) == str:
            self.params = su.ingest(params)
        else:
            self.params = params

        self.params = d.check_sim_params(self.params)

        if vb:
            print self.params

    def proc_bins(self, bins, limits=(d.min_x, d.max_x), vb=True):
        """
        Function to process binning

        Parameters
        ----------
        bins: int
            number of evenly spaced bins
        limits: tuple or list or numpy.ndarray, float, optional
            endpoints over which binning will be defined
        vb: boolean, optional
            True to print progress messages to stdout, False to suppress
        """
        if type(bins) == int:
            self.n_coarse = bins
        else:
            self.n_coarse = 10
        x_min = limits[0]#np.min(self.obs_samps)
        x_max = limits[-1]#np.max(self.obs_samps)
        self.n_fine = self.n_coarse
        self.n_tot = self.n_coarse * self.n_fine
        x_range = x_max-x_min

        self.dx_coarse = x_range / self.n_coarse
        self.dx_fine = x_range / self.n_tot

        self.x_coarse = np.arange(x_min+0.5*self.dx_coarse, x_max, self.dx_coarse)
        self.x_fine = np.arange(x_min+0.5*self.dx_fine, x_max, self.dx_fine)

        self.bin_ends = np.arange(x_min, x_max+self.dx_coarse, self.dx_coarse)

        return

    def coarsify(self, fine):
        """
        Function to bin function evaluated on fine grid

        Parameters
        ----------
        fine: numpy.ndarray, float
            vector of values of function on fine grid

        Returns
        -------
        coarse: numpy.ndarray, float
            vector of binned values of function
        """
        coarse = fine / (np.sum(fine) * self.dx_fine)
        coarse = np.array([np.sum(coarse[k * self.n_fine : (k+1) * self.n_fine]) * self.dx_fine for k in range(self.n_coarse)])
        coarse /= self.dx_coarse

        return coarse

    def create(self, truth, int_pr, bins=d.n_bins, vb=True):
        """
        Function creating a catalog of interim posterior probability distributions, will split this up into helper functions

        Parameters
        ----------
        truth: numpy.ndarray, float
            vector of true redshifts
        int_pr: chippr.gmix object or chippr.gauss object or chippr.binned object
            interim prior distribution object
        bins: int, optional
            number of evenly spaced bins

        Returns
        -------
        self.cat: dict
            dictionary comprising catalog information
        """
        self.true_samps = truth
        self.n_items = len(self.true_samps)
        self.samp_range = range(self.n_items)

        self.obs_samps = self.sample_obs()

        self.int_pr = int_pr
        self.proc_bins(bins, limits=(self.int_pr.bin_ends[0], self.int_pr.bin_ends[-1]))

        self.obs_lfs = self.evaluate_lfs()

        int_pr_fine = int_pr.evaluate(self.x_fine)
        int_pr_coarse = self.coarsify(int_pr_fine)

        pfs = np.zeros((self.n_items, self.n_coarse))
        for n in self.samp_range:
            pf = int_pr_fine * self.obs_lfs[n]
            pf = self.coarsify(pf)
            pfs[n] += pf

        self.cat = {}
        self.cat['bin_ends'] = self.bin_ends
        self.cat['log_interim_prior'] = u.safe_log(int_pr_coarse)
        self.cat['log_interim_posteriors'] = u.safe_log(pfs)

        return self.cat

    def sample_obs(self):
        """
        Samples observed values from true values

        Returns
        -------
        obs_samps: numpy.ndarray, float
            observed values
        """
        if not self.params['variable_sigma']:
            true_lfs = [gauss(self.true_samps[n], self.params['constant_sigma']**2) for n in self.samp_range]
        if self.params['catastrophic_outliers']:
            outlier_lf = gauss(self.params['outlier_mean'], self.params['outlier_sigma']**2)
        obs_samps = np.zeros(self.n_items)
        for n in self.samp_range:
            if np.random.uniform() < self.params['outlier_fraction']:
                obs_samps[n] = outlier_lf.sample_one()
            else:
                obs_samps[n] = true_lfs[n].sample_one()
        return obs_samps

    def evaluate_lfs(self):
        """
        Evaluates likelihoods based on observed sample values

        Returns
        -------
        obs_lfs: numpy.ndarray, float
            array of likelihood values for each item as a function of fine binning
        """
        if not self.params['variable_sigma']:
            lfs_fine = [gauss(self.x_fine[kk], self.params['constant_sigma']**2) for kk in range(self.n_tot)]
            obs_lfs = (1.-self.params['outlier_fraction']) * np.array([lfs_fine[kk].evaluate(self.obs_samps) for kk in range(self.n_tot)])
        if self.params['catastrophic_outliers']:
            outlier_lf = gauss(self.params['outlier_mean'], self.params['outlier_sigma']**2)
            obs_lfs += self.params['outlier_fraction'] * outlier_lf.evaluate(self.obs_samps)
        return obs_lfs.T

    def write(self, loc, style='plaintext'):
        """
        Function to write newly-created catalog to file

        Parameters
        ----------
        loc: string
            location into which to save catalog files
        style: string, optional
            file format in which to save the catalog
        """
        if style == 'plaintext':
            with open(loc, 'wb') as csvfile:
                out = csv.writer(csvfile, delimiter=' ')
                out.writerow(self.cat['bin_ends'])
                out.writerow(self.cat['log_interim_prior'])
                for line in self.cat['log_interim_posteriors']:
                    out.writerow(line)
        return
<|endoftext|>


<commit_before>#!/usr/bin/env python
import json
import logging
import os.path
import random
import time
from urllib import urlencode

import praw
from praw.errors import NotFound
from pyparsing import ParseException

import db
from config import Config
from db import DB, Battle, Region, User, MarchingOrder, Processed
from parser import parse
from commands import (Command, Context, failable, InvadeCommand,
                      SkirmishCommand, StatusCommand)
from utils import (base36decode, extract_command, num_to_team, name_to_id, now,
                   timestr, version)

class Bot(object):
    def __init__(self, config, reddit):
        self.config = config
        self.reddit = reddit
        self.db = DB(config)
        self.db.create_all()
        self.session = self.db.session()

    @failable
    def check_battles(self):
        session = self.session
        battles = session.query(Battle).all()
        for battle in battles:
            post = self.reddit.get_submission(
                comment_limit=None,
                submission_id=name_to_id(battle.submission_id))
            if post:
                self.process_post_for_battle(post, battle, session)

    @failable
    def check_hq(self):
        hq = self.reddit.get_subreddit(self.config.headquarters)
        submissions = hq.get_new()
        for submission in submissions:
            if "[recruitment]" in submission.title.lower():
                self.recruit_from_post(submission)
                break  # Only recruit from the first one

    @failable
    def check_messages(self):
        unread = reddit.get_unread(True, True)
        session = self.session
        for comment in unread:
            # Only PMs, we deal with comment replies in process_post_for_battle
            if not comment.was_comment:
                seen = (session.query(Processed).
                    filter_by(id36=comment.name).
                    count())
                if seen:
                    continue
                player = self.find_player(comment, session)
                if player:
                    cmds = extract_command(comment.body)
                    if not cmds:
                        cmds = [comment.body]
                    context = Context(player, self.config, session,
                                      comment, self.reddit)
                    for cmd in cmds:
                        self.command(cmd, context)
                session.add(Processed(id36=comment.name))
                session.commit()
            comment.mark_as_read()

    @failable
    def command(self, text, context):
        text = text.lower()
        logging.info("Processing command: '%s' by %s" %
                     (text, context.player.name))
        try:
            parsed = parse(text)
            parsed.execute(context)
        except ParseException as pe:
            result = (
                "I'm sorry, I couldn't understand your command:"
                "\n\n"
                "> %s\n"
                "\nThe parsing error is below:\n\n"
                "    %s") % (text, pe)
            context.reply(result)

    def find_player(self, comment, session):
        if comment.author:  # Some messages (mod invites) don't have authors
            player = session.query(User).filter_by(
            name=comment.author.name.lower()).first()
            if not player and getattr(comment, 'was_comment', None):
                comment.reply(Command.FAIL_NOT_PLAYER %
                                  self.config.headquarters)
            return player
        return None

    @failable
    def generate_markdown_report(self, loop_start):
        """
        Separate from the others as this logs to a sidebar rather than
        a file
        """
        s = self.session

        land_report = StatusCommand.lands_status_for(s, self.config)
        hq = self.reddit.get_subreddit(self.config.headquarters)

        cur = now()
        elapsed = (cur - loop_start) + self.config["bot"]["sleep"]
        version_str = version(self.config)

        bot_report = ("Bot Status:\n\n"
                      "* Last run at %s\n\n"
                      "* Seconds per Frame: %d\n\n"
                      "* Version: %s") % (timestr(cur), elapsed, version_str)

        report = "%s\n\n%s" % (land_report, bot_report)

        # This is apparently not immediately done, or there's some caching.
        # Keep an eye on it.
        hq.update_settings(description=report)

    def generate_reports(self, loop_start):
        logging.info("Generating reports")
        self.generate_markdown_report(loop_start)
        rdir = self.config["bot"].get("report_dir")
        if not rdir:
            return
        s = self.session
        regions = s.query(Region).all()
        with open(os.path.join(rdir, "report.txt"), 'w') as url:
            urldict = {}
            for r in regions:
                if r.owner is not None:
                    owner = r.owner
                else:
                    owner = -1
                urldict[r.name] = owner
            url.write(urlencode(urldict))

        with open(os.path.join(rdir, "report.json"), 'w') as j:
            jdict = {}
            for r in regions:
                rdict = {}
                rdict['name'] = r.name
                rdict['srname'] = r.srname
                if r.owner is not None:
                    rdict['owner'] = r.owner
                else:
                    rdict['owner'] = -1

                if r.battle:
                    if r.battle.has_started():
                        rdict['battle'] = 'underway'
                    else:
                        rdict['battle'] = 'preparing'
                else:
                    rdict['battle'] = 'none'
                jdict[r.name] = rdict
            print "Dumping %s" % jdict
            j.write(json.dumps(jdict))

    def process_post_for_battle(self, post, battle, sess):
        p = sess.query(Processed).filter_by(battle=battle).all()
        seen = [entry.id36 for entry in p]

        replaced = post.replace_more_comments(limit=None, threshold=0)
        if replaced:
            logging.info("Comments that went un-replaced: %s" % replaced)
        flat_comments = praw.helpers.flatten_tree(
            post.comments)

        for comment in flat_comments:
            if comment.name in seen:
                continue
            if not comment.author:  # Deleted comments don't have an author
                continue
            if comment.author.name.lower() == self.config.username.lower():
                continue
            cmds = extract_command(comment.body)
            if cmds:
                player = self.find_player(comment, sess)
                if player:
                    context = Context(player, self.config, sess,
                                          comment, self.reddit)
                    for cmd in cmds:
                        self.command(cmd, context)
            sess.add(Processed(id36=comment.name, battle=battle))
            sess.commit()

    @failable
    def recruit_from_post(self, post):
        post.replace_more_comments(threshold=0)
        flat_comments = praw.helpers.flatten_tree(post.comments)
        for comment in flat_comments:
            self.recruit_from_comment(comment)

    @failable
    def recruit_from_comment(self, comment):
        session = self.session
        if not comment.author:  # Deleted comments don't have an author
            return
        name = comment.author.name.lower()
        if name == self.config.username.lower():
            return

        # Is this author already one of us?
        found = session.query(User).filter_by(
            name=name).first()
        if not found:
            # Getting the author ID triggers a lookup on the userpage.  In the
            # case of banned users, this will 404.  @failable would normally
            # catch that just fine, but I want to check it here so that in the
            # future, I can do things like e.g. add to an 'ignored' list and
            # save myself the lookup
            try:
                author_id = comment.author.id
            except NotFound:
                logging.warn("Ignored banned user %s" % name)
                return

            team = 0
            assignment = self.config['game']['assignment']
            if assignment == 'uid':
                base10_id = base36decode(author_id)
                team = base10_id % 2
            elif assignment == "random":
                team = random.randint(0, 1)
            is_leader = name in self.config["game"]["leaders"]
            newbie = User(name=name,
                          team=team,
                          loyalists=100,
                          leader=is_leader)
            session.add(newbie)

            cap = Region.capital_for(newbie.team, session)
            if not cap:
                logging.fatal("Could not find capital for %d" %
                              newbie.team)
            newbie.region = cap

            session.commit()
            logging.info("Created combatant %s", newbie)

            reply = ("Welcome to Chroma!  You are now a %s "
                     "in the %s army, commanding a force of loyalists "
                     "%d people strong. You are currently encamped at %s"
            ) % (newbie.rank,
                 num_to_team(newbie.team, self.config),
                 newbie.loyalists,
                 cap.markdown())
            comment.reply(reply)
        else:
            #logging.info("Already registered %s", comment.author.name)
            pass

    @failable
    def update_skirmish_summaries(self, skirmishes):
        c = Context(player=None,
                    config=self.config,
                    session=self.session,
                    comment=None,
                    reddit=self.reddit)
        for s in skirmishes:
            SkirmishCommand.update_summary(c, s)

    @failable
    def update_game(self):
        session = self.session
        MarchingOrder.update_all(session, self.config)

        results = Region.update_all(session, self.config)
        to_add = []
        for newternal in results['new_eternal']:
            title = "The Eternal Battle Rages On"
            post = InvadeCommand.post_invasion(title, newternal, self.reddit)
            if post:
                newternal.submission_id = post.name
                to_add.append(newternal)
            else:
                logging.warn("Couldn't submit eternal battle thread")
                session.rollback()
        if to_add:
            session.add_all(to_add)
            session.commit()

        results = Battle.update_all(session, self.config)

        for ready in results['begin']:
            ready.display_ends = (ready.begins +
                self.config["game"]["battle_time"])

            # Actual ending is within battle_lockout of the end
            chooserange = self.config["game"]["battle_lockout"]
            chosen = random.randint(0, chooserange)
            ready.ends = ready.display_ends - (chooserange / 2) + chosen

            text = ("War is now at your doorstep!  Mobilize your armies! "
                    "The battle has begun now, and will end at %s.\n\n"
                    "> Enter your commands in this thread, prefixed with "
                    "'>'") % ready.ends_str()
            post = self.reddit.get_submission(
                submission_id=name_to_id(ready.submission_id))
            post.edit(text)
            session.commit()

        self.update_skirmish_summaries(results['skirmish_ended'])

        for done in results['ended']:
            report = ["The battle is complete...\n"]
            report += done.report(self.config)

            report.append("")

            if done.old_buffs:
                report.append("Buffs in effect for Team %s\n" %
                              num_to_team(done.old_owner, self.config))
                for buff in done.old_buffs:
                    report.append("  * %s" % buff.name)
                report.append("")

            team0_name = num_to_team(0, self.config)
            team1_name = num_to_team(1, self.config)

            # Homeland buffs?
            if done.homeland_buffs:
                report.append("Homeland buffs in effect:")
                for team in range(0, 2):
                    name = num_to_team(team, self.config)
                    report.append("%s: %d%%" %
                                  (name, done.homeland_buffs[team]))

            report.append(("## Final Score:  Team %s: %d "
                           "Team %s: %d") % (team0_name, done.score0,
                                             team1_name, done.score1))
            if done.victor is not None:
                report.append("\n# The Victor:  Team %s" %
                              num_to_team(done.victor, self.config))
            else:
                report.append("# TIE")

            text = "\n".join(report)
            post = self.reddit.get_submission(
                submission_id=name_to_id(done.submission_id))
            post.edit(text)

            # Update all the skirmish summaries
            self.update_skirmish_summaries(done.toplevel_skirmishes())

            session.delete(done)
            session.commit()
        db.Buff.update_all(session)

    @failable
    def login(self):
        reddit.login(c.username, c.password)
        return True

    def run(self):
        logging.info("Bot started up")
        if self.config.bot.get("verbose_logging"):
            logging.info("Verbose logging enabled")
        logged_in = self.login()
        while(logged_in):
            loop_start = now()
            self.config.refresh()
            logging.info("Checking headquarters")
            self.check_hq()
            logging.info("Checking Messages")
            self.check_messages()
            logging.info("Checking Battles")
            self.check_battles()
            logging.info("Updating game state")
            self.update_game()
            # generate_reports logs itself
            self.generate_reports(loop_start)
            logging.info("Sleeping")
            time.sleep(self.config["bot"]["sleep"])
        logging.fatal("Unable to log into bot; shutting down")

if __name__ == '__main__':
    fmt = "%(asctime)s: %(levelname)s %(message)s"
    logging.basicConfig(level=logging.INFO, format=fmt)
    c = Config()
    reddit = c.praw()

    bot = Bot(c, reddit)
    bot.run()
<commit_msg>JSON reports now include user information<commit_after>#!/usr/bin/env python
import json
import logging
import os.path
import random
import time
from urllib import urlencode

import praw
from praw.errors import NotFound
from pyparsing import ParseException

import db
from config import Config
from db import DB, Battle, Region, User, MarchingOrder, Processed
from parser import parse
from commands import (Command, Context, failable, InvadeCommand,
                      SkirmishCommand, StatusCommand)
from utils import (base36decode, extract_command, num_to_team, name_to_id, now,
                   timestr, version)

class Bot(object):
    def __init__(self, config, reddit):
        self.config = config
        self.reddit = reddit
        self.db = DB(config)
        self.db.create_all()
        self.session = self.db.session()

    @failable
    def check_battles(self):
        session = self.session
        battles = session.query(Battle).all()
        for battle in battles:
            post = self.reddit.get_submission(
                comment_limit=None,
                submission_id=name_to_id(battle.submission_id))
            if post:
                self.process_post_for_battle(post, battle, session)

    @failable
    def check_hq(self):
        hq = self.reddit.get_subreddit(self.config.headquarters)
        submissions = hq.get_new()
        for submission in submissions:
            if "[recruitment]" in submission.title.lower():
                self.recruit_from_post(submission)
                break  # Only recruit from the first one

    @failable
    def check_messages(self):
        unread = reddit.get_unread(True, True)
        session = self.session
        for comment in unread:
            # Only PMs, we deal with comment replies in process_post_for_battle
            if not comment.was_comment:
                seen = (session.query(Processed).
                    filter_by(id36=comment.name).
                    count())
                if seen:
                    continue
                player = self.find_player(comment, session)
                if player:
                    cmds = extract_command(comment.body)
                    if not cmds:
                        cmds = [comment.body]
                    context = Context(player, self.config, session,
                                      comment, self.reddit)
                    for cmd in cmds:
                        self.command(cmd, context)
                session.add(Processed(id36=comment.name))
                session.commit()
            comment.mark_as_read()

    @failable
    def command(self, text, context):
        text = text.lower()
        logging.info("Processing command: '%s' by %s" %
                     (text, context.player.name))
        try:
            parsed = parse(text)
            parsed.execute(context)
        except ParseException as pe:
            result = (
                "I'm sorry, I couldn't understand your command:"
                "\n\n"
                "> %s\n"
                "\nThe parsing error is below:\n\n"
                "    %s") % (text, pe)
            context.reply(result)

    def find_player(self, comment, session):
        if comment.author:  # Some messages (mod invites) don't have authors
            player = session.query(User).filter_by(
            name=comment.author.name.lower()).first()
            if not player and getattr(comment, 'was_comment', None):
                comment.reply(Command.FAIL_NOT_PLAYER %
                                  self.config.headquarters)
            return player
        return None

    @failable
    def generate_markdown_report(self, loop_start):
        """
        Separate from the others as this logs to a sidebar rather than
        a file
        """
        s = self.session

        land_report = StatusCommand.lands_status_for(s, self.config)
        hq = self.reddit.get_subreddit(self.config.headquarters)

        cur = now()
        elapsed = (cur - loop_start) + self.config["bot"]["sleep"]
        version_str = version(self.config)

        bot_report = ("Bot Status:\n\n"
                      "* Last run at %s\n\n"
                      "* Seconds per Frame: %d\n\n"
                      "* Version: %s") % (timestr(cur), elapsed, version_str)

        report = "%s\n\n%s" % (land_report, bot_report)

        # This is apparently not immediately done, or there's some caching.
        # Keep an eye on it.
        hq.update_settings(description=report)

    def generate_reports(self, loop_start):
        logging.info("Generating reports")
        self.generate_markdown_report(loop_start)
        rdir = self.config["bot"].get("report_dir")
        if not rdir:
            return
        s = self.session
        regions = s.query(Region).all()
        with open(os.path.join(rdir, "report.txt"), 'w') as url:
            urldict = {}
            for r in regions:
                if r.owner is not None:
                    owner = r.owner
                else:
                    owner = -1
                urldict[r.name] = owner
            url.write(urlencode(urldict))

        with open(os.path.join(rdir, "report.json"), 'w') as j:
            jdict = {}
            jdict['regions'] = {}
            for r in regions:
                rdict = {}
                rdict['name'] = r.name
                rdict['srname'] = r.srname
                if r.owner is not None:
                    rdict['owner'] = r.owner
                else:
                    rdict['owner'] = -1

                if r.battle:
                    if r.battle.has_started():
                        rdict['battle'] = 'underway'
                    else:
                        rdict['battle'] = 'preparing'
                else:
                    rdict['battle'] = 'none'
                jdict['regions'][r.name] = rdict

            users = {}
            for u in s.query(User).all():
                udict = {}
                udict['team'] = u.team
                udict['leader'] = u.leader
                users[u.name] = udict
            jdict['users'] = users
            j.write(json.dumps(jdict))

    def process_post_for_battle(self, post, battle, sess):
        p = sess.query(Processed).filter_by(battle=battle).all()
        seen = [entry.id36 for entry in p]

        replaced = post.replace_more_comments(limit=None, threshold=0)
        if replaced:
            logging.info("Comments that went un-replaced: %s" % replaced)
        flat_comments = praw.helpers.flatten_tree(
            post.comments)

        for comment in flat_comments:
            if comment.name in seen:
                continue
            if not comment.author:  # Deleted comments don't have an author
                continue
            if comment.author.name.lower() == self.config.username.lower():
                continue
            cmds = extract_command(comment.body)
            if cmds:
                player = self.find_player(comment, sess)
                if player:
                    context = Context(player, self.config, sess,
                                          comment, self.reddit)
                    for cmd in cmds:
                        self.command(cmd, context)
            sess.add(Processed(id36=comment.name, battle=battle))
            sess.commit()

    @failable
    def recruit_from_post(self, post):
        post.replace_more_comments(threshold=0)
        flat_comments = praw.helpers.flatten_tree(post.comments)
        for comment in flat_comments:
            self.recruit_from_comment(comment)

    @failable
    def recruit_from_comment(self, comment):
        session = self.session
        if not comment.author:  # Deleted comments don't have an author
            return
        name = comment.author.name.lower()
        if name == self.config.username.lower():
            return

        # Is this author already one of us?
        found = session.query(User).filter_by(
            name=name).first()
        if not found:
            # Getting the author ID triggers a lookup on the userpage.  In the
            # case of banned users, this will 404.  @failable would normally
            # catch that just fine, but I want to check it here so that in the
            # future, I can do things like e.g. add to an 'ignored' list and
            # save myself the lookup
            try:
                author_id = comment.author.id
            except NotFound:
                logging.warn("Ignored banned user %s" % name)
                return

            team = 0
            assignment = self.config['game']['assignment']
            if assignment == 'uid':
                base10_id = base36decode(author_id)
                team = base10_id % 2
            elif assignment == "random":
                team = random.randint(0, 1)
            is_leader = name in self.config["game"]["leaders"]
            newbie = User(name=name,
                          team=team,
                          loyalists=100,
                          leader=is_leader)
            session.add(newbie)

            cap = Region.capital_for(newbie.team, session)
            if not cap:
                logging.fatal("Could not find capital for %d" %
                              newbie.team)
            newbie.region = cap

            session.commit()
            logging.info("Created combatant %s", newbie)

            reply = ("Welcome to Chroma!  You are now a %s "
                     "in the %s army, commanding a force of loyalists "
                     "%d people strong. You are currently encamped at %s"
            ) % (newbie.rank,
                 num_to_team(newbie.team, self.config),
                 newbie.loyalists,
                 cap.markdown())
            comment.reply(reply)
        else:
            #logging.info("Already registered %s", comment.author.name)
            pass

    @failable
    def update_skirmish_summaries(self, skirmishes):
        c = Context(player=None,
                    config=self.config,
                    session=self.session,
                    comment=None,
                    reddit=self.reddit)
        for s in skirmishes:
            SkirmishCommand.update_summary(c, s)

    @failable
    def update_game(self):
        session = self.session
        MarchingOrder.update_all(session, self.config)

        results = Region.update_all(session, self.config)
        to_add = []
        for newternal in results['new_eternal']:
            title = "The Eternal Battle Rages On"
            post = InvadeCommand.post_invasion(title, newternal, self.reddit)
            if post:
                newternal.submission_id = post.name
                to_add.append(newternal)
            else:
                logging.warn("Couldn't submit eternal battle thread")
                session.rollback()
        if to_add:
            session.add_all(to_add)
            session.commit()

        results = Battle.update_all(session, self.config)

        for ready in results['begin']:
            ready.display_ends = (ready.begins +
                self.config["game"]["battle_time"])

            # Actual ending is within battle_lockout of the end
            chooserange = self.config["game"]["battle_lockout"]
            chosen = random.randint(0, chooserange)
            ready.ends = ready.display_ends - (chooserange / 2) + chosen

            text = ("War is now at your doorstep!  Mobilize your armies! "
                    "The battle has begun now, and will end at %s.\n\n"
                    "> Enter your commands in this thread, prefixed with "
                    "'>'") % ready.ends_str()
            post = self.reddit.get_submission(
                submission_id=name_to_id(ready.submission_id))
            post.edit(text)
            session.commit()

        self.update_skirmish_summaries(results['skirmish_ended'])

        for done in results['ended']:
            report = ["The battle is complete...\n"]
            report += done.report(self.config)

            report.append("")

            if done.old_buffs:
                report.append("Buffs in effect for Team %s\n" %
                              num_to_team(done.old_owner, self.config))
                for buff in done.old_buffs:
                    report.append("  * %s" % buff.name)
                report.append("")

            team0_name = num_to_team(0, self.config)
            team1_name = num_to_team(1, self.config)

            # Homeland buffs?
            if done.homeland_buffs:
                report.append("Homeland buffs in effect:")
                for team in range(0, 2):
                    name = num_to_team(team, self.config)
                    report.append("%s: %d%%" %
                                  (name, done.homeland_buffs[team]))

            report.append(("## Final Score:  Team %s: %d "
                           "Team %s: %d") % (team0_name, done.score0,
                                             team1_name, done.score1))
            if done.victor is not None:
                report.append("\n# The Victor:  Team %s" %
                              num_to_team(done.victor, self.config))
            else:
                report.append("# TIE")

            text = "\n".join(report)
            post = self.reddit.get_submission(
                submission_id=name_to_id(done.submission_id))
            post.edit(text)

            # Update all the skirmish summaries
            self.update_skirmish_summaries(done.toplevel_skirmishes())

            session.delete(done)
            session.commit()
        db.Buff.update_all(session)

    @failable
    def login(self):
        reddit.login(c.username, c.password)
        return True

    def run(self):
        logging.info("Bot started up")
        if self.config.bot.get("verbose_logging"):
            logging.info("Verbose logging enabled")
        logged_in = self.login()
        while(logged_in):
            loop_start = now()
            self.config.refresh()
            logging.info("Checking headquarters")
            self.check_hq()
            logging.info("Checking Messages")
            self.check_messages()
            logging.info("Checking Battles")
            self.check_battles()
            logging.info("Updating game state")
            self.update_game()
            # generate_reports logs itself
            self.generate_reports(loop_start)
            logging.info("Sleeping")
            time.sleep(self.config["bot"]["sleep"])
        logging.fatal("Unable to log into bot; shutting down")

if __name__ == '__main__':
    fmt = "%(asctime)s: %(levelname)s %(message)s"
    logging.basicConfig(level=logging.INFO, format=fmt)
    c = Config()
    reddit = c.praw()

    bot = Bot(c, reddit)
    bot.run()
<|endoftext|>