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

# Copyright (c) 2011 Christopher D. Lasher
#
# This software is released under the MIT License. Please see
# LICENSE.txt for details.

"""Markov chains for BPN."""

import math
import random

import recorders
import states

import logging
logger = logging.getLogger('bpn.mcmcbpn.chains')

from defaults import NUM_STEPS, BURN_IN, BROADCAST_PERCENT, \
        SUPERDEBUG, SUPERDEBUG_MODE

class MarkovChain(object):
    pass

class PLNMarkovChain(MarkovChain):
    """A class representing the Markov chain for process linkage
    networks.

    """
    def __init__(
            self,
            annotated_interactions,
            active_gene_threshold,
            transition_ratio,
            num_steps=NUM_STEPS,
            burn_in=BURN_IN,
            seed_links=None,
            alpha=None,
            beta=None,
            link_prior=None,
            state_recorder_class=recorders.PLNStateRecorder,
            parameters_state_class=states.PLNParametersState
        ):
        """Create a new instance.

        :Parameters:
        - `annotated_interactions`: an `AnnotatedInteractionsGraph`
          instance
        - `active_gene_threshold`: the threshold at or above which a
          gene is considered "active"
        - `transition_ratio`: a `float` indicating the ratio of link
          transitions to parameter transitions
        - `num_steps`: the number of steps to take in the Markov chain
        - `burn_in`: the number of steps to take before recording state
          information about the Markov chain (state records are
          discarded until complete)
        - `seed_links`: a user-defined seed of links to start as
          selected
        - `alpha`: the false-positive rate; see `PLNParametersState` for
          more information
        - `beta`: the false-negative rate; see `PLNParametersState` for
          more information
        - `link_prior`: the assumed probability we would pick any one
          link as being active; see `PLNParametersState` for more
          information
        - `state_recorder_class`: the class of the state recorder to use
          [default: `recorders.PLNStateRecorder`]
        - `parameters_state_class`: the class of the parameters state to
          use [default: `states.PLNParametersState]`

        """
        self.current_state = states.PLNOverallState(
                annotated_interactions,
                active_gene_threshold,
                transition_ratio,
                seed_links,
                alpha,
                beta,
                link_prior,
                parameters_state_class

        )
        self.state_recorder = state_recorder_class(
                self.current_state.links_state.process_links,
                self.current_state.parameters_state.get_parameter_distributions()
        )
        self.burn_in_steps = burn_in
        self.num_steps = num_steps
        self.burn_in_period = True
        # This attribute will keep track of how we transition through
        # the Markov chain by storing a tuple for the previous
        # transition. The first item for the transition information is
        # the type of transition performed, which is obtained from the
        # PLNOverallState._delta attribute's key. The second item of the
        # tuple is a floating point value representing the log of the
        # transition ratio computed in calc_log_transition_ratio(). The
        # third item in the tuple is either `True`, representing that
        # the transition was rejected, or `False`, representing that the
        # transition was accepted.
        self.last_transition_info = None

    def next_state(self):
        """Move to the next state in the Markov chain.

        This method creates a proposed state for a transition; it then
        assesses the "fitness" of this new state by comparing the
        likelihood of the proposed state to the likelihood of the
        current state as a (log of the) ratio of the two likelihoods.

        If this ratio is greater than 1 (i.e., the log of the ratio is
        positive, we accept the proposed state and transition to it. If,
        instead, the ratio is less than 1 (i.e., the log of the ratio is
        negative), we flip a rejection coin. If the ratio is still
        greater than the rejection coin, we accept the less likely
        proposed state, anyway (a feature which allows us to exit local
        maxima); otherwise we reject the proposed state and continue
        with the current state.

        """
        proposed_state = self.current_state.create_new_state()
        proposed_transition_type = proposed_state._delta[0]
        current_log_likelihood = \
                self.current_state.calc_log_likelihood()
        proposed_log_likelihood = \
                proposed_state.calc_log_likelihood()
        log_transition_ratio = proposed_log_likelihood - \
                current_log_likelihood
        logger.debug("Log of transition ratio: %s" % (
                log_transition_ratio))

        # Flip a coin to see if we'll accept the transition
        log_rejection_coin = math.log10(random.random())
        logger.debug("Log rejection coin: %s" % log_rejection_coin)
        if log_rejection_coin < log_transition_ratio:
            # We accept the proposed state!
            self.current_state = proposed_state
            logger.debug("Accepted proposed state.")
            accepted = True
            log_state_likelihood = proposed_log_likelihood
        else:
            logger.debug("Rejected proposed state.")
            accepted = False
            log_state_likelihood = current_log_likelihood

        logger.debug("Log of state likelihood: %s" % (
                log_state_likelihood))
        self.last_transition_info = (
                proposed_transition_type,
                log_transition_ratio,
                log_state_likelihood,
                accepted
        )

    def run(self):
        """Step through the states of the Markov chain.

        Runs for a total number of iterations equal to `burn_in` +
        `num_steps`.

        """
        logger.info("Beginning burn-in of %d steps." %
                self.burn_in_steps)
        broadcast_percent_complete = 0
        for i in xrange(self.burn_in_steps):
            logger.debug("Burn-in step %d of %d" % (i + 1,
                    self.burn_in_steps))
            self.next_state()
            percent_complete = int(100 * float(i + 1) /
                    self.burn_in_steps)
            if percent_complete >= (broadcast_percent_complete +
                    BROADCAST_PERCENT):
                broadcast_percent_complete = percent_complete
                logger.info("%d%% of burn-in complete." % (
                        percent_complete))

        logger.info("Beginning run of %d steps." % self.num_steps)
        self.burn_in_period = False
        broadcast_percent_complete = 0
        for i in xrange(self.num_steps):
            logger.debug("Step %d of %d" % (i + 1, self.num_steps))
            self.next_state()
            self.state_recorder.record_state(self)
            percent_complete = int(100 * float(i + 1) / self.num_steps)
            if percent_complete >= (broadcast_percent_complete +
                    BROADCAST_PERCENT):
                broadcast_percent_complete = percent_complete
                logger.info("%d%% of steps complete." % (
                        percent_complete))

class ArrayMarkovChain(PLNMarkovChain):
    """Similar to `PLNMarkovChain`, but using `numpy` arrays to track
    state information.

    """
    def __init__(
            self,
            annotated_interactions,
            active_gene_threshold,
            transition_ratio,
            num_steps=NUM_STEPS,
            burn_in=BURN_IN,
            seed_links_indices=None,
            alpha=None,
            beta=None,
            link_prior=None,
            state_recorder_class=recorders.ArrayStateRecorder,
            parameters_state_class=states.PLNParametersState,
            links_state_class=states.ArrayLinksState
        ):
        """Create a new instance.

        :Parameters:
        - `annotated_interactions`: an `AnnotatedInteractionsArray`
          instance
        - `active_gene_threshold`: the threshold at or above which a
          gene is considered "active"
        - `transition_ratio`: a `float` indicating the ratio of link
          transitions to parameter transitions
        - `num_steps`: the number of steps to take in the Markov chain
        - `burn_in`: the number of steps to take before recording state
          information about the Markov chain (state records are
          discarded until complete)
        - `seed_links_indices`: a user-defined seed of indices to
          links to start as selected
        - `alpha`: the false-positive rate; see `PLNParametersState` for
          more information
        - `beta`: the false-negative rate; see `PLNParametersState` for
          more information
        - `link_prior`: the assumed probability we would pick any one
          link as being active; see `PLNParametersState` for more
          information
        - `state_recorder_class`: the class of the state recorder to use
          [default: `recorders.ArrayStateRecorder`]
        - `parameters_state_class`: the class of the parameters state to
          use [default: `states.PLNParametersState`]
        - `links_state_class`: the class of the links state to use
          [default: `states.ArrayLinksState`]

        """
        self.current_state = states.ArrayOverallState(
                annotated_interactions,
                active_gene_threshold,
                transition_ratio,
                seed_links_indices,
                alpha,
                beta,
                link_prior,
                parameters_state_class,
                links_state_class
        )
        self.state_recorder = state_recorder_class(
                annotated_interactions.get_all_links(),
                self.current_state.parameters_state.get_parameter_distributions()
        )
        self.burn_in_steps = burn_in
        self.num_steps = num_steps
        self.burn_in_period = True
        self.last_transition_info = None

class TermsBasedMarkovChain(ArrayMarkovChain):
    """A Markov chain based on states that consider both term and link
    selections.

    """
    def __init__(
            self,
            annotated_interactions,
            active_gene_threshold,
            transition_ratio,
            num_steps=NUM_STEPS,
            burn_in=BURN_IN,
            seed_links_indices=None,
            alpha=None,
            beta=None,
            link_prior=None,
            term_prior=None,
            state_recorder_class=recorders.TermsBasedStateRecorder,
            parameters_state_class=states.TermPriorParametersState,
            links_state_class=states.TermsAndLinksState
        ):
        """Create a new instance.

        :Parameters:
        - `annotated_interactions`: an `AnnotatedInteractionsArray`
          instance
        - `active_gene_threshold`: the threshold at or above which a
          gene is considered "active"
        - `transition_ratio`: a `float` indicating the ratio of link
          transitions to parameter transitions
        - `num_steps`: the number of steps to take in the Markov chain
        - `burn_in`: the number of steps to take before recording state
          information about the Markov chain (state records are
          discarded until complete)
        - `seed_links_indices`: a user-defined seed of indices to
          links to start as selected
        - `alpha`: the false-positive rate; see `PLNParametersState` for
          more information
        - `beta`: the false-negative rate; see `PLNParametersState` for
          more information
        - `link_prior`: the assumed probability we would pick any one
          link as being active; see `PLNParametersState` for more
          information
        - `term_prior`:the assumed probability we would select any one
          term; see `RandomTransitionParametersState` for more
          information
        - `state_recorder_class`: the class of the state recorder to use
          [default: `recorders.TermsStateRecorder`]
        - `parameters_state_class`: the class of the parameters state to
          use [default: `states.TermPriorParametersState`]
        - `links_state_class`: the class of the links state to use
          [default: `states.TermsAndLinksState`]

        """
        self.current_state = states.TermsBasedOverallState(
                annotated_interactions,
                active_gene_threshold,
                transition_ratio,
                seed_links_indices,
                alpha=alpha,
                beta=beta,
                link_prior=link_prior,
                term_prior=term_prior,
                parameters_state_class=parameters_state_class,
                links_state_class=links_state_class
        )
        self.state_recorder = state_recorder_class(
                annotated_interactions,
                self.current_state.parameters_state.get_parameter_distributions()
        )
        self.burn_in_steps = burn_in
        self.num_steps = num_steps
        self.burn_in_period = True
        self.last_transition_info = None

<commit_msg>Adds more comments on state transition logic.<commit_after>#!/usr/bin/env python
# -*- coding: UTF-8 -*-

# Copyright (c) 2011 Christopher D. Lasher
#
# This software is released under the MIT License. Please see
# LICENSE.txt for details.

"""Markov chains for BPN."""

import math
import random

import recorders
import states

import logging
logger = logging.getLogger('bpn.mcmcbpn.chains')

from defaults import NUM_STEPS, BURN_IN, BROADCAST_PERCENT, \
        SUPERDEBUG, SUPERDEBUG_MODE

class MarkovChain(object):
    pass

class PLNMarkovChain(MarkovChain):
    """A class representing the Markov chain for process linkage
    networks.

    """
    def __init__(
            self,
            annotated_interactions,
            active_gene_threshold,
            transition_ratio,
            num_steps=NUM_STEPS,
            burn_in=BURN_IN,
            seed_links=None,
            alpha=None,
            beta=None,
            link_prior=None,
            state_recorder_class=recorders.PLNStateRecorder,
            parameters_state_class=states.PLNParametersState
        ):
        """Create a new instance.

        :Parameters:
        - `annotated_interactions`: an `AnnotatedInteractionsGraph`
          instance
        - `active_gene_threshold`: the threshold at or above which a
          gene is considered "active"
        - `transition_ratio`: a `float` indicating the ratio of link
          transitions to parameter transitions
        - `num_steps`: the number of steps to take in the Markov chain
        - `burn_in`: the number of steps to take before recording state
          information about the Markov chain (state records are
          discarded until complete)
        - `seed_links`: a user-defined seed of links to start as
          selected
        - `alpha`: the false-positive rate; see `PLNParametersState` for
          more information
        - `beta`: the false-negative rate; see `PLNParametersState` for
          more information
        - `link_prior`: the assumed probability we would pick any one
          link as being active; see `PLNParametersState` for more
          information
        - `state_recorder_class`: the class of the state recorder to use
          [default: `recorders.PLNStateRecorder`]
        - `parameters_state_class`: the class of the parameters state to
          use [default: `states.PLNParametersState]`

        """
        self.current_state = states.PLNOverallState(
                annotated_interactions,
                active_gene_threshold,
                transition_ratio,
                seed_links,
                alpha,
                beta,
                link_prior,
                parameters_state_class

        )
        self.state_recorder = state_recorder_class(
                self.current_state.links_state.process_links,
                self.current_state.parameters_state.get_parameter_distributions()
        )
        self.burn_in_steps = burn_in
        self.num_steps = num_steps
        self.burn_in_period = True
        # This attribute will keep track of how we transition through
        # the Markov chain by storing a tuple for the previous
        # transition. The first item for the transition information is
        # the type of transition performed, which is obtained from the
        # PLNOverallState._delta attribute's key. The second item of the
        # tuple is a floating point value representing the log of the
        # transition ratio computed in calc_log_transition_ratio(). The
        # third item in the tuple is either `True`, representing that
        # the transition was rejected, or `False`, representing that the
        # transition was accepted.
        self.last_transition_info = None

    def next_state(self):
        """Move to the next state in the Markov chain.

        This method creates a proposed state for a transition; it then
        assesses the "fitness" of this new state by comparing the
        likelihood of the proposed state to the likelihood of the
        current state as a (log of the) ratio of the two likelihoods.

        If this ratio is greater than 1 (i.e., the log of the ratio is
        positive, we accept the proposed state and transition to it. If,
        instead, the ratio is less than 1 (i.e., the log of the ratio is
        negative), we flip a rejection coin. If the ratio is still
        greater than the rejection coin, we accept the less likely
        proposed state, anyway (a feature which allows us to exit local
        maxima); otherwise we reject the proposed state and continue
        with the current state.

        """
        proposed_state = self.current_state.create_new_state()
        proposed_transition_type = proposed_state._delta[0]
        current_log_likelihood = (
                self.current_state.calc_log_likelihood())
        proposed_log_likelihood = (
                proposed_state.calc_log_likelihood())
        log_transition_ratio = (proposed_log_likelihood -
                current_log_likelihood)
        logger.debug("Log of transition ratio: %s" % (
                log_transition_ratio))
        # Remember, a very favorable state has a likelihood close to 1
        # and its log-likelihood close to 0 (i.e., very small negative
        # value); a very unfavorable state has a likelihood close to 0,
        # and a log-likelihood with a very negative value (large
        # magnitude).

        # If the proposed state is more likely than the current state,
        # the proposed state's log-likelihood will be a smaller negative
        # number than the current state's; thus log_transition_ratio
        # will be a positive value. If the proposed state is less likely
        # than the current state, the proposed state's log-likelihood
        # will be a larger negative number than the current state's;
        # thus the log_transition_ratio will be a negative value.
        #
        # If the proposed state is more likely, we want to go ahead and
        # accept it.
        if log_transition_ratio > 0:
            self.current_state = proposed_state
            logger.debug("Accepted proposed state.")
            accepted = True
            log_state_likelihood = proposed_log_likelihood
        else:
            # Otherwise, the proposed state is equally or less likely
            # than the current state, however, we may still choose it,
            # even though this may be unfavorable, to avoid local
            # maxima. We will do this by drawing a number uniformly at
            # random in the range [0, 1], as the probability we will
            # reject the state. If the log of the log-transition ratio
            # is greater than the log of the rejection probability,
            # we'll still accept the less favorable proposed state.
            log_rejection_prob = math.log10(random.random())
            logger.debug("Log rejection probability: %s" % log_rejection_prob)
            if log_transition_ratio > log_rejection_prob:
                self.current_state = proposed_state
                logger.debug("Accepted proposed state.")
                accepted = True
                log_state_likelihood = proposed_log_likelihood
            else:
                logger.debug("Rejected proposed state.")
                accepted = False
                log_state_likelihood = current_log_likelihood

        logger.debug("Log of state likelihood: %s" % (
                log_state_likelihood))
        self.last_transition_info = (
                proposed_transition_type,
                log_transition_ratio,
                log_state_likelihood,
                accepted
        )

    def run(self):
        """Step through the states of the Markov chain.

        Runs for a total number of iterations equal to `burn_in` +
        `num_steps`.

        """
        logger.info("Beginning burn-in of %d steps." %
                self.burn_in_steps)
        broadcast_percent_complete = 0
        for i in xrange(self.burn_in_steps):
            logger.debug("Burn-in step %d of %d" % (i + 1,
                    self.burn_in_steps))
            self.next_state()
            percent_complete = int(100 * float(i + 1) /
                    self.burn_in_steps)
            if percent_complete >= (broadcast_percent_complete +
                    BROADCAST_PERCENT):
                broadcast_percent_complete = percent_complete
                logger.info("%d%% of burn-in complete." % (
                        percent_complete))

        logger.info("Beginning run of %d steps." % self.num_steps)
        self.burn_in_period = False
        broadcast_percent_complete = 0
        for i in xrange(self.num_steps):
            logger.debug("Step %d of %d" % (i + 1, self.num_steps))
            self.next_state()
            self.state_recorder.record_state(self)
            percent_complete = int(100 * float(i + 1) / self.num_steps)
            if percent_complete >= (broadcast_percent_complete +
                    BROADCAST_PERCENT):
                broadcast_percent_complete = percent_complete
                logger.info("%d%% of steps complete." % (
                        percent_complete))

class ArrayMarkovChain(PLNMarkovChain):
    """Similar to `PLNMarkovChain`, but using `numpy` arrays to track
    state information.

    """
    def __init__(
            self,
            annotated_interactions,
            active_gene_threshold,
            transition_ratio,
            num_steps=NUM_STEPS,
            burn_in=BURN_IN,
            seed_links_indices=None,
            alpha=None,
            beta=None,
            link_prior=None,
            state_recorder_class=recorders.ArrayStateRecorder,
            parameters_state_class=states.PLNParametersState,
            links_state_class=states.ArrayLinksState
        ):
        """Create a new instance.

        :Parameters:
        - `annotated_interactions`: an `AnnotatedInteractionsArray`
          instance
        - `active_gene_threshold`: the threshold at or above which a
          gene is considered "active"
        - `transition_ratio`: a `float` indicating the ratio of link
          transitions to parameter transitions
        - `num_steps`: the number of steps to take in the Markov chain
        - `burn_in`: the number of steps to take before recording state
          information about the Markov chain (state records are
          discarded until complete)
        - `seed_links_indices`: a user-defined seed of indices to
          links to start as selected
        - `alpha`: the false-positive rate; see `PLNParametersState` for
          more information
        - `beta`: the false-negative rate; see `PLNParametersState` for
          more information
        - `link_prior`: the assumed probability we would pick any one
          link as being active; see `PLNParametersState` for more
          information
        - `state_recorder_class`: the class of the state recorder to use
          [default: `recorders.ArrayStateRecorder`]
        - `parameters_state_class`: the class of the parameters state to
          use [default: `states.PLNParametersState`]
        - `links_state_class`: the class of the links state to use
          [default: `states.ArrayLinksState`]

        """
        self.current_state = states.ArrayOverallState(
                annotated_interactions,
                active_gene_threshold,
                transition_ratio,
                seed_links_indices,
                alpha,
                beta,
                link_prior,
                parameters_state_class,
                links_state_class
        )
        self.state_recorder = state_recorder_class(
                annotated_interactions.get_all_links(),
                self.current_state.parameters_state.get_parameter_distributions()
        )
        self.burn_in_steps = burn_in
        self.num_steps = num_steps
        self.burn_in_period = True
        self.last_transition_info = None

class TermsBasedMarkovChain(ArrayMarkovChain):
    """A Markov chain based on states that consider both term and link
    selections.

    """
    def __init__(
            self,
            annotated_interactions,
            active_gene_threshold,
            transition_ratio,
            num_steps=NUM_STEPS,
            burn_in=BURN_IN,
            seed_links_indices=None,
            alpha=None,
            beta=None,
            link_prior=None,
            term_prior=None,
            state_recorder_class=recorders.TermsBasedStateRecorder,
            parameters_state_class=states.TermPriorParametersState,
            links_state_class=states.TermsAndLinksState
        ):
        """Create a new instance.

        :Parameters:
        - `annotated_interactions`: an `AnnotatedInteractionsArray`
          instance
        - `active_gene_threshold`: the threshold at or above which a
          gene is considered "active"
        - `transition_ratio`: a `float` indicating the ratio of link
          transitions to parameter transitions
        - `num_steps`: the number of steps to take in the Markov chain
        - `burn_in`: the number of steps to take before recording state
          information about the Markov chain (state records are
          discarded until complete)
        - `seed_links_indices`: a user-defined seed of indices to
          links to start as selected
        - `alpha`: the false-positive rate; see `PLNParametersState` for
          more information
        - `beta`: the false-negative rate; see `PLNParametersState` for
          more information
        - `link_prior`: the assumed probability we would pick any one
          link as being active; see `PLNParametersState` for more
          information
        - `term_prior`:the assumed probability we would select any one
          term; see `RandomTransitionParametersState` for more
          information
        - `state_recorder_class`: the class of the state recorder to use
          [default: `recorders.TermsStateRecorder`]
        - `parameters_state_class`: the class of the parameters state to
          use [default: `states.TermPriorParametersState`]
        - `links_state_class`: the class of the links state to use
          [default: `states.TermsAndLinksState`]

        """
        self.current_state = states.TermsBasedOverallState(
                annotated_interactions,
                active_gene_threshold,
                transition_ratio,
                seed_links_indices,
                alpha=alpha,
                beta=beta,
                link_prior=link_prior,
                term_prior=term_prior,
                parameters_state_class=parameters_state_class,
                links_state_class=links_state_class
        )
        self.state_recorder = state_recorder_class(
                annotated_interactions,
                self.current_state.parameters_state.get_parameter_distributions()
        )
        self.burn_in_steps = burn_in
        self.num_steps = num_steps
        self.burn_in_period = True
        self.last_transition_info = None

<|endoftext|>


<commit_before>"""Top-level init file for brainx package.
"""

def patch_nx():
    """Temporary fix for NX's watts_strogatz routine, which has a bug in versions 1.1-1.3
    """

    import networkx as nx

    # Quick test to see if we get the broken version
    g = nx.watts_strogatz_graph(2, 0, 0)

    if g.number_of_nodes() != 2:
        # Buggy version detected.  Create a patched version and apply it to nx

        nx._watts_strogatz_graph_ori = nx.watts_strogatz_graph

        def patched_ws(n, k, p, create_using=None, seed=None):
            if k<2:
                g = nx.Graph()
                g.add_nodes_from(range(n))
                return g
            else:
                return nx._watts_strogatz_graph_ori(n, k, p, create_using, seed)

        patched_ws.__doc__ = nx._watts_strogatz_graph_ori.__doc__

        # Applying monkeypatch now
        import warnings
        warnings.warn("Monkeypatching NetworkX's Watts-Strogatz routine")

        nx.watts_strogatz_graph = patched_ws

patch_nx()
<commit_msg>Update watts-strogatz call to match new NetworkX API<commit_after>"""Top-level init file for brainx package.
"""

def patch_nx():
    """Temporary fix for NX's watts_strogatz routine, which has a bug in versions 1.1-1.3
    """

    import networkx as nx

    # Quick test to see if we get the broken version
    g = nx.watts_strogatz_graph(2, 0, 0)

    if g.number_of_nodes() != 2:
        # Buggy version detected.  Create a patched version and apply it to nx

        nx._watts_strogatz_graph_ori = nx.watts_strogatz_graph

        def patched_ws(n, k, p, seed=None):
            if k<2:
                g = nx.Graph()
                g.add_nodes_from(range(n))
                return g
            else:
                return nx._watts_strogatz_graph_ori(n, k, p, seed)

        patched_ws.__doc__ = nx._watts_strogatz_graph_ori.__doc__

        # Applying monkeypatch now
        import warnings
        warnings.warn("Monkeypatching NetworkX's Watts-Strogatz routine")

        nx.watts_strogatz_graph = patched_ws

patch_nx()
<|endoftext|>


<commit_before># Copyright (c) 2016, Michael Sonntag (sonntag@bio.lmu.de)
#
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted under the terms of the BSD License. See
# LICENSE file in the root of the project.

import os
import imghdr
import yaml

# TODO Add start and end
# TODO Add logfile
# TODO Add commandline support - add cmdline parser
# TODO Outsource program settings to JSON file, parse and read that in.

class BulkRename:

    def __init__(self, main_dir_path, base_name="baseName"):

        # Set defaults
        self.include_main_dir = False
        self.add_directory_name = True
        self.name_separator = "__"
        self.replace_white_space = True
        self.white_space_separator = "_"
        self.batch_start_index = 1
        self.rename_all_file_types = True
        # TODO Current file type checks are really slow...
        self.rename_file_types = ['JPG', 'JPEG', 'PNG', 'GIF']
        self.exclude_dirs = []

        # Set semiconfigs
        self.main_dir_path = main_dir_path
        self.base_name = base_name

    def run(self):
        """
        Script used to batch rename files in a directory tree. At the moment only files
        in the first layer of subfolders will be taken into account.
        Files will be renamed in the following fashion:
        '[Subdirectory name]_[specified batch name]_[progressing index]__[part of the
        previous name after separator]'.
        The index will be reset for each subfolder, the starting index can be defined.
        It can be defined whether all files should be renamed, the renaming can also be
        restricted to specified file types only.
        Subdirectories can be excluded by name.
        """

        print("[Info] Starting ...")
        print("[Info] Handling folder %s; included: %s" %
              (self.main_dir_path, self.include_main_dir))
        print("[Info] MainName: %s; Add Directory Name: %s; batch start index: %s" %
              (self.base_name, self.add_directory_name, self.batch_start_index))
        print("[Info] Separator: '%s'; ws separator: '%s'; replace ws: '%s'" %
              (self.name_separator, self.white_space_separator, self.replace_white_space))
        print("[Info] Rename file types: %s" % self.rename_file_types)

        for path, _, files in os.walk(self.main_dir_path):
            if path != self.main_dir_path or self.include_main_dir:

                # TODO add better check for exclude_dirs at this point
                exclude_dir = False
                for curr_dir in self.exclude_dirs:
                    if path.find(curr_dir) > -1:
                        print("[Info] Directory '%s' will be excluded" % curr_dir)
                        exclude_dir = True

                if not exclude_dir:
                    split_dirs = [os.path.basename(path.rstrip("\\"))]
                    if path != self.main_dir_path:
                        split_path = os.path.abspath(path).replace(self.main_dir_path, "")
                        split_dirs = split_path.split("\\")

                    print("[Info] Processing directory '%s'" % path)
                    i = self.batch_start_index
                    for curr in files:
                        self.do_stuff(path, curr, split_dirs, i)
                        i += 1

    def do_stuff(self, path, curr_file, split_dirs, idx):
        original_file = os.path.join(path, curr_file)

        fpc = str(imghdr.what(original_file)).upper()
        if self.rename_all_file_types or self.rename_file_types.__contains__(fpc):
            if curr_file.find(self.name_separator) == -1:
                print("[Warning] File '%s' does not contain separator" % curr_file)

                # TODO Nicer filename integration into the print string.
                # TODO Check if this is a good way to handle the file
                # TODO ending when working with a missing name separator.

                split_parts = curr_file.rsplit(".", 1)
                split_parts[1] = ".%s" % split_parts[1]
            else:
                split_parts = curr_file.split(self.name_separator)

            file_name = ""

            # Add directory name to te current file name if set.
            # Make sure only one white space separator is used at the
            # end of the directory name.
            if self.add_directory_name:
                file_name += split_dirs[0].rstrip(self.white_space_separator)
                file_name += self.white_space_separator

            # TODO Check if inserting the main name into the first
            # directory and any subdirectory can be done in a better way.
            file_name += self.base_name + self.white_space_separator

            if self.add_directory_name:
                for curr_dir in split_dirs[1:]:
                    file_name += curr_dir.rstrip(self.white_space_separator)
                    file_name += self.white_space_separator

            file_name += "{:03d}".format(idx)
            end_name = split_parts[1]
            if split_parts.__len__() > 2:
                for part in split_parts[2:]:
                    end_name += self.white_space_separator + part

            file_name.strip()
            end_name.strip()

            if self.replace_white_space & (file_name.find(" ") > 0):
                print("[Info] Replace white space in %s" % file_name)
                file_name = file_name.replace(' ', self.white_space_separator)
                file_name = file_name.replace(self.name_separator,
                                              self.white_space_separator)
            if self.replace_white_space & (end_name.find(" ") > 0):
                print("[Info] Replace white space in %s" % end_name)
                end_name = end_name.replace(" ", self.white_space_separator)
                end_name = end_name.replace(self.name_separator,
                                            self.white_space_separator)

            file_name += self.name_separator + end_name

            formatted_name = os.path.join(path, file_name)

            os.rename(original_file, formatted_name)
<commit_msg>[BulkRename] parse config file<commit_after># Copyright (c) 2016, Michael Sonntag (sonntag@bio.lmu.de)
#
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted under the terms of the BSD License. See
# LICENSE file in the root of the project.

import os
import imghdr
import yaml

# TODO Add start and end
# TODO Add logfile
# TODO Add commandline support - add cmdline parser
# TODO Outsource program settings to JSON file, parse and read that in.

CONFIG_TERMS = ["include_main_dir", "add_directory_name", "name_separator",
                "replace_white_space", "white_space_separator", "batch_start_index",
                "rename_all_file_types", "rename_file_types", "exclude_dirs"]

class BulkRename:

    def __init__(self, main_dir_path, base_name="baseName", config_file=None):

        # Set defaults
        self.include_main_dir = False
        self.add_directory_name = True
        self.name_separator = "__"
        self.replace_white_space = True
        self.white_space_separator = "_"
        self.batch_start_index = 1
        self.rename_all_file_types = True
        # TODO Current file type checks are really slow...
        self.rename_file_types = ['JPG', 'JPEG', 'PNG', 'GIF']
        self.exclude_dirs = []

        # Set semi-configs
        self.main_dir_path = main_dir_path
        self.base_name = base_name

        # Set config if available
        print("[Info] Using config file: %s (found: %s)" %
              (config_file, os.path.isfile(config_file)))

        if config_file and os.path.isfile(config_file):
            with open(config_file) as yaml_raw:
                config = yaml.load(yaml_raw)
                if "include_main_dir" in config and config["include_main_dir"]:
                    self.include_main_dir = True if config["include_main_dir"] == 1 else False
                if "add_directory_name" in config and config["add_directory_name"]:
                    self.add_directory_name = True if config["add_directory_name"] == 1 else False
                if "name_separator" in config and config["name_separator"]:
                    self.name_separator = config["name_separator"]
                if "replace_white_space" in config and config["replace_white_space"]:
                    self.replace_white_space = config["replace_white_space"]
                if "white_space_separator" in config and config["white_space_separator"]:
                    self.white_space_separator = True if config["white_space_separator"] == 1 else False
                if "batch_start_index" in config and config["batch_start_index"]:
                    self.batch_start_index = config["batch_start_index"]
                if "rename_all_file_types" in config and config["rename_all_file_types"]:
                    self.rename_all_file_types = True if config["rename_all_file_types"] == 1 else False
                if "rename_file_types" in config and config["rename_file_types"]:
                    self.rename_file_types = config["rename_file_types"]
                if "exclude_dirs" in config and config["exclude_dirs"]:
                    self.exclude_dirs = config["exclude_dirs"]

    def run(self):
        """
        Script used to batch rename files in a directory tree. At the moment only files
        in the first layer of subfolders will be taken into account.
        Files will be renamed in the following fashion:
        '[Subdirectory name]_[specified batch name]_[progressing index]__[part of the
        previous name after separator]'.
        The index will be reset for each subfolder, the starting index can be defined.
        It can be defined whether all files should be renamed, the renaming can also be
        restricted to specified file types only.
        Subdirectories can be excluded by name.
        """

        print("[Info] Starting ...")
        print("[Info] Handling folder %s; included: %s" %
              (self.main_dir_path, self.include_main_dir))
        print("[Info] MainName: %s; Add Directory Name: %s; batch start index: %s" %
              (self.base_name, self.add_directory_name, self.batch_start_index))
        print("[Info] Separator: '%s'; ws separator: '%s'; replace ws: '%s'" %
              (self.name_separator, self.white_space_separator, self.replace_white_space))
        print("[Info] Rename file types: %s" % self.rename_file_types)

        for path, _, files in os.walk(self.main_dir_path):
            if path != self.main_dir_path or self.include_main_dir:

                # TODO add better check for exclude_dirs at this point
                exclude_dir = False
                for curr_dir in self.exclude_dirs:
                    if path.find(curr_dir) > -1:
                        print("[Info] Directory '%s' will be excluded" % curr_dir)
                        exclude_dir = True

                if not exclude_dir:
                    split_dirs = [os.path.basename(path.rstrip("\\"))]
                    if path != self.main_dir_path:
                        split_path = os.path.abspath(path).replace(self.main_dir_path, "")
                        split_dirs = split_path.split("\\")

                    print("[Info] Processing directory '%s'" % path)
                    i = self.batch_start_index
                    for curr in files:
                        self.do_stuff(path, curr, split_dirs, i)
                        i += 1

    def do_stuff(self, path, curr_file, split_dirs, idx):
        original_file = os.path.join(path, curr_file)

        fpc = str(imghdr.what(original_file)).upper()
        if self.rename_all_file_types or self.rename_file_types.__contains__(fpc):
            if curr_file.find(self.name_separator) == -1:
                print("[Warning] File '%s' does not contain separator" % curr_file)

                # TODO Nicer filename integration into the print string.
                # TODO Check if this is a good way to handle the file
                # TODO ending when working with a missing name separator.

                split_parts = curr_file.rsplit(".", 1)
                split_parts[1] = ".%s" % split_parts[1]
            else:
                split_parts = curr_file.split(self.name_separator)

            file_name = ""

            # Add directory name to te current file name if set.
            # Make sure only one white space separator is used at the
            # end of the directory name.
            if self.add_directory_name:
                file_name += split_dirs[0].rstrip(self.white_space_separator)
                file_name += self.white_space_separator

            # TODO Check if inserting the main name into the first
            # directory and any subdirectory can be done in a better way.
            file_name += self.base_name + self.white_space_separator

            if self.add_directory_name:
                for curr_dir in split_dirs[1:]:
                    file_name += curr_dir.rstrip(self.white_space_separator)
                    file_name += self.white_space_separator

            file_name += "{:03d}".format(idx)
            end_name = split_parts[1]
            if split_parts.__len__() > 2:
                for part in split_parts[2:]:
                    end_name += self.white_space_separator + part

            file_name.strip()
            end_name.strip()

            if self.replace_white_space & (file_name.find(" ") > 0):
                print("[Info] Replace white space in %s" % file_name)
                file_name = file_name.replace(' ', self.white_space_separator)
                file_name = file_name.replace(self.name_separator,
                                              self.white_space_separator)
            if self.replace_white_space & (end_name.find(" ") > 0):
                print("[Info] Replace white space in %s" % end_name)
                end_name = end_name.replace(" ", self.white_space_separator)
                end_name = end_name.replace(self.name_separator,
                                            self.white_space_separator)

            file_name += self.name_separator + end_name

            formatted_name = os.path.join(path, file_name)

            os.rename(original_file, formatted_name)
<|endoftext|>


<commit_before>"""Brooks.

A simulator for Brooks' Law: "Adding manpower to a late software project makes it later." (Brooks, 1975)

Usage:
    brooks <schedule> [--output=<output-file>] [--] <attribute>...

Arguments:
    schedule    A Python module defining initial(), step() and complete() functions to control the simulation.
    attribute   The name(s) of one or more a model state attribute(s) to be logged at each step.

Options:
  --output=<output-file>  Output file: [default: stdout]
  -h --help               Show this screen.
  --version               Show version
"""

import sys
import importlib.machinery
from contextlib import contextmanager

from docopt import docopt

from brooks.simulation import simulate
from brooks.brooks_law import step

def main(argv=None):
    if argv is None:
        argv = sys.argv

    arguments = docopt(__doc__, version="Brooks 0.5")

    schedule_file_path = arguments['<schedule>']
    loader = importlib.machinery.SourceFileLoader("schedule", schedule_file_path)
    schedule = loader.load_module()

    attributes = arguments['<attribute>']

    with open_stream(arguments['--output']) as output_stream:
        simulate(schedule, step, output_stream, attributes)

    return 0

@contextmanager
def open_stream(filepath):
    if filepath == 'stdout':
        yield sys.stdout
    else:
        f = open(filepath)
        yield f
        f.close()

if __name__ == '__main__':
    sys.exit(main())

<commit_msg>Corrects argument handling.<commit_after>"""Brooks.

A simulator for Brooks' Law: "Adding manpower to a late software project makes it later." (Brooks, 1975)

Usage:
    brooks <schedule> [--output=<output-file>] [--] <attribute>...

Arguments:
    schedule    A Python module defining initial(), step() and complete() functions to control the simulation.
    attribute   The name(s) of one or more a model state attribute(s) to be logged at each step.

Options:
  --output=<output-file>  Output file: [default: stdout]
  -h --help               Show this screen.
  --version               Show version
"""

import sys
import importlib.machinery
from contextlib import contextmanager

from docopt import docopt

from brooks.simulation import simulate
from brooks.brooks_law import step

def main(argv=None):
    if argv is None:
        argv = sys.argv[1:]

    arguments = docopt(__doc__, argv=argv, version="Brooks 0.5")

    schedule_file_path = arguments['<schedule>']
    loader = importlib.machinery.SourceFileLoader("schedule", schedule_file_path)
    schedule = loader.load_module()

    attributes = arguments['<attribute>']

    with open_out_stream(arguments['--output']) as output_stream:
        simulate(schedule, step, output_stream, attributes)

    return 0

@contextmanager
def open_out_stream(filepath):
    if filepath == 'stdout':
        yield sys.stdout
    else:
        f = open(filepath, 'wt')
        yield f
        f.close()

if __name__ == '__main__':
    sys.exit(main())

<|endoftext|>


<commit_before>#!/usr/bin/env python
#A simple script that prints the letter or whatever of the day (randomly)
#Written by: Adam Walsh (adamwalsh14@gmail.com)
#Maintained @ https://github.com/walshie4/Geeklets

from __future__ import printfunction
import random

#CONFIG
NUMBER = True
#END CONFIG

numbers = range(-200,201) #all integers [-200, 200]

if NUMBER:
    print(numbers[random.random()*len(numbers)])
else:
    print('No data sources enabled in config')
<commit_msg>Finished implementation of number of the day<commit_after>#!/usr/bin/env python
#A simple script that prints the letter or whatever of the day (randomly)
#Written by: Adam Walsh (adamwalsh14@gmail.com)
#Maintained @ https://github.com/walshie4/Geeklets

from __future__ import print_function
import random

#CONFIG
NUMBER = True
#END CONFIG

numbers = range(-200,201) #all integers [-200, 200]

if NUMBER:
    print("The number of the day is:", numbers[int(random.random()*len(numbers))])
else:
    print('No data sources enabled in config')
<|endoftext|>


<commit_before># Copyright 2021 The Kubric Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import logging

import shutil

import kubric as kb
from kubric.renderer.blender import Blender as KubricRenderer

logging.basicConfig(level="INFO")

# --- create scene and attach a renderer to it
scene = kb.Scene(resolution=(7, 7), frame_end=2)
shutil.rmtree("output_tmp")
renderer = KubricRenderer(scene, scratch_dir="output_tmp")

# --- populate the scene with objects, lights, cameras
scene += kb.Cube(name="floor", scale=(10, 10, 0.1), position=(0, 0, -0.1))
ball_horiz = kb.Sphere(name="ball_horiz", scale=1, position=(0, 1, 1.))
ball_vert = kb.Sphere(name="ball_vert", scale=1, position=(1, 0, 1.))
scene += ball_horiz
scene += ball_vert
scene += kb.DirectionalLight(name="sun", position=(-1, -0.5, 3), look_at=(0, 0, 0), intensity=1.5)
scene += kb.PerspectiveCamera(name="camera", position=(1e-5, 0, 20), look_at=(0, 0, 0))

ball_horiz.keyframe_insert("position", 0)
ball_horiz.position = (0, 10, 1)
ball_horiz.keyframe_insert("position", 5)

ball_vert.keyframe_insert("position", 0)
ball_vert.position = (10, 0, 1)
ball_vert.keyframe_insert("position", 5)

# --- render (and save the blender file)
renderer.save_state("output/helloworld.blend")
frames = renderer.render()
kb.write_image_dict(frames, "output/")

#
# # --- save the output as pngs
# kb.write_png(frame["rgba"], "output/helloworld.png")
# kb.write_palette_png(frame["segmentation"], "output/helloworld_segmentation.png")
# scale = kb.write_scaled_png(frame["depth"], "output/helloworld_depth.png")
# logging.info("Depth scale: %s", scale)
<commit_msg>deleted flow_test (which is now part of test_postprocessing)<commit_after><|endoftext|>


<commit_before>import matplotlib
matplotlib.use('agg')
import pytest
import DataHandling
import numpy as np
from matplotlib.testing.decorators import image_comparison

def test_nothing():
    print("Nothing Tested")
    print(dir(DataHandling))

def test_LoadData():
    data = DataHandling.LoadData("testData.raw")
    assert type(data) == DataHandling.DataHandling.DataObject
    assert data.filename == "testData.raw"
    assert data.time[1]-data.time[0] == pytest.approx(data.SampleFreq, 0.0001*data.SampleFreq) # approx equal to within 0.01%
    assert max(data.freqs) == pytest.approx(data.SampleFreq/2, 0.00001*data.SampleFreq/2) # max freq in PSD is approx equal to Nyquist frequency within 0.001%
    t, V = data.getTimeData()
    np.testing.assert_array_equal(t, data.time)
    np.testing.assert_array_equal(V, data.Voltage)

    return None

@pytest.mark.mpl_image_compare # this decorator compares the figure object returned by the following function to the baseline png image stored in tests/baseline
def test_plotPSD():
    data = DataHandling.LoadData("testData.raw")
    fig, ax = data.plotPSD([0, 400e3], ShowFig=False)
    return fig
<commit_msg>attempting to force python to use 'agg' backend<commit_after>import matplotlib
matplotlib.use('agg', warn=False, force=True)
import pytest
import DataHandling
import numpy as np
from matplotlib.testing.decorators import image_comparison

def test_nothing():
    print("Nothing Tested")
    print(dir(DataHandling))

def test_LoadData():
    data = DataHandling.LoadData("testData.raw")
    assert type(data) == DataHandling.DataHandling.DataObject
    assert data.filename == "testData.raw"
    assert data.time[1]-data.time[0] == pytest.approx(data.SampleFreq, 0.0001*data.SampleFreq) # approx equal to within 0.01%
    assert max(data.freqs) == pytest.approx(data.SampleFreq/2, 0.00001*data.SampleFreq/2) # max freq in PSD is approx equal to Nyquist frequency within 0.001%
    t, V = data.getTimeData()
    np.testing.assert_array_equal(t, data.time)
    np.testing.assert_array_equal(V, data.Voltage)

    return None

@pytest.mark.mpl_image_compare # this decorator compares the figure object returned by the following function to the baseline png image stored in tests/baseline
def test_plotPSD():
    data = DataHandling.LoadData("testData.raw")
    fig, ax = data.plotPSD([0, 400e3], ShowFig=False)
    return fig
<|endoftext|>


<commit_before># -*- coding: utf-8 -*-

import unittest
from unittest import mock

import nikola
from nikola.plugins.command.init import SAMPLE_CONF
from nikola.plugins.command.init import format_default_translations_config

class CommandInitCallTest(unittest.TestCase):
    def setUp(self):
        self.ask_questions = mock.MagicMock()
        self.copy_sample_site = mock.MagicMock()
        self.create_configuration = mock.MagicMock()
        self.create_empty_site = mock.MagicMock()
        ask_questions_patch = mock.patch(
            'nikola.plugins.command.init.CommandInit.ask_questions', self.ask_questions)
        copy_sample_site_patch = mock.patch(
            'nikola.plugins.command.init.CommandInit.copy_sample_site', self.copy_sample_site)
        create_configuration_patch = mock.patch(
            'nikola.plugins.command.init.CommandInit.create_configuration',
            self.create_configuration)
        create_empty_site_patch = mock.patch(
            'nikola.plugins.command.init.CommandInit.create_empty_site', self.create_empty_site)

        self.patches = [ask_questions_patch, copy_sample_site_patch,
                        create_configuration_patch, create_empty_site_patch]
        for patch in self.patches:
            patch.start()

        self.init_command = nikola.plugins.command.init.CommandInit()

    def tearDown(self):
        for patch in self.patches:
            patch.stop()
        del self.patches

        del self.copy_sample_site
        del self.create_configuration
        del self.create_empty_site

    def test_init_default(self):
        self.init_command.execute()

        self.assertTrue(self.ask_questions.called)
        self.assertTrue(self.create_configuration.called)
        self.assertFalse(self.copy_sample_site.called)
        self.assertTrue(self.create_empty_site.called)

    def test_init_args(self):
        arguments = dict(
            options={'demo': True, 'quiet': True},
            args=['destination'])
        self.init_command.execute(**arguments)

        self.assertFalse(self.ask_questions.called)
        self.assertTrue(self.create_configuration.called)
        self.assertTrue(self.copy_sample_site.called)
        self.assertFalse(self.create_empty_site.called)

    def test_init_called_without_target_quiet(self):
        self.init_command.execute(**dict(options={'quiet': True}))

        self.assertFalse(self.ask_questions.called)
        self.assertFalse(self.create_configuration.called)
        self.assertFalse(self.copy_sample_site.called)
        self.assertFalse(self.create_empty_site.called)

    def test_init_empty_dir(self):
        self.init_command.execute(args=['destination'])

        self.assertTrue(self.ask_questions.called)
        self.assertTrue(self.create_configuration.called)
        self.assertFalse(self.copy_sample_site.called)
        self.assertTrue(self.create_empty_site.called)

def test_configure_translations_without_additional_languages():
    """
    Testing the configuration of the translation when no additional language has been found.
    """
    translations_cfg = format_default_translations_config(set())
    assert SAMPLE_CONF["TRANSLATIONS"] == translations_cfg

def test_configure_translations_with_2_additional_languages():
    """
    Testing the configuration of the translation when two additional languages are given.
    """
    translations_cfg = format_default_translations_config(set(["es", "en"]))
    assert translations_cfg == """{
    DEFAULT_LANG: "",
    "en": "./en",
    "es": "./es",
}"""
<commit_msg>Migrate tests to pytest.<commit_after># -*- coding: utf-8 -*-

from unittest import mock

from nikola.plugins.command.init import (
    SAMPLE_CONF, CommandInit, format_default_translations_config)

import pytest
from .base import cd

@pytest.fixture
def ask_questions():
    return mock.MagicMock()

@pytest.fixture
def copy_sample_site():
    return mock.MagicMock()

@pytest.fixture
def create_configuration():
    return mock.MagicMock()

@pytest.fixture
def create_empty_site():
    return mock.MagicMock()

@pytest.fixture
def init_command(tmpdir, ask_questions, copy_sample_site, create_configuration, create_empty_site):
    with mock.patch('nikola.plugins.command.init.CommandInit.ask_questions', ask_questions):
        with mock.patch('nikola.plugins.command.init.CommandInit.copy_sample_site', copy_sample_site):
            with mock.patch('nikola.plugins.command.init.CommandInit.create_configuration', create_configuration):
                with mock.patch('nikola.plugins.command.init.CommandInit.create_empty_site', create_empty_site):
                    with cd(tmpdir):
                        yield CommandInit()

def test_command_init_with_defaults(init_command, ask_questions, copy_sample_site, create_configuration, create_empty_site):
    init_command.execute()

    assert ask_questions.called
    assert create_configuration.called
    assert not copy_sample_site.called
    assert create_empty_site.called

def test_command_init_with_arguments(init_command, ask_questions, copy_sample_site, create_configuration, create_empty_site):
    arguments = dict(
        options={'demo': True, 'quiet': True},
        args=['destination'])
    init_command.execute(**arguments)

    assert not ask_questions.called
    assert create_configuration.called
    assert copy_sample_site.called
    assert not create_empty_site.called

def test_init_called_without_target_quiet(init_command, ask_questions, copy_sample_site, create_configuration, create_empty_site):
    init_command.execute(**{"options": {'quiet': True}})

    assert not ask_questions.called
    assert not create_configuration.called
    assert not copy_sample_site.called
    assert not create_empty_site.called

def test_command_init_with_empty_dir(init_command, ask_questions, copy_sample_site, create_configuration, create_empty_site):
    init_command.execute(args=['destination'])

    assert ask_questions.called
    assert create_configuration.called
    assert not copy_sample_site.called
    assert create_empty_site.called

def test_configure_translations_without_additional_languages():
    """
    Testing the configuration of the translation when no additional language has been found.
    """
    translations_cfg = format_default_translations_config(set())
    assert SAMPLE_CONF["TRANSLATIONS"] == translations_cfg

def test_configure_translations_with_2_additional_languages():
    """
    Testing the configuration of the translation when two additional languages are given.
    """
    translations_cfg = format_default_translations_config(set(["es", "en"]))
    assert translations_cfg == """{
    DEFAULT_LANG: "",
    "en": "./en",
    "es": "./es",
}"""
<|endoftext|>


<commit_before>import numpy as np
import os
import pytest

import hcnn.common.utils as utils

def __eq(a, b):
    assert a == b

def test_create_directory(workspace):
    dname = os.path.join(workspace, "oh_hello")
    assert not os.path.exists(dname)
    assert utils.create_directory(dname)
    assert utils.create_directory(dname)

def test_filebase():
    fnames = ['y', 'y.z', 'x/y.z', 'x.y.z']
    results = ['y', 'y', 'y', 'x.y']
    for fn, res in zip(fnames, results):
        yield __eq, utils.filebase(fn), res

@pytest.mark.skipif(True, reason='todo')
def test_fold_array():
    assert False

@pytest.mark.skipif(True, reason='todo')
def test_map_io():
    assert False

def test_unzip():
    zip_files = [os.path.join(os.path.dirname(__file__), "zipped_folder.zip")]
    unzipped_folders = utils.unzip_files(zip_files)
    for dir_path in unzipped_folders:
        assert os.path.exists(dir_path) and os.path.isdir(dir_path)

@pytest.mark.skipif(True, reason='todo')
def test_slice_ndarray():
    assert False

@pytest.mark.skipif(True, reason='todo')
def test_colored():
    assert False

def test_iter_from_params_filepath():
    tests = [
        ("foo/bar/params/params0500.npz", "0500"),
        ("foo/bar/params/params10505.npz", "10505"),
        ("foo/bar/params/params0000.npz", "0000"),
        ("foo/bar/params/final.npz", "final"),
    ]

    def __test(value, expected):
        assert value == expected

    for test_input, expected in tests:
        yield __test, utils.iter_from_params_filepath(test_input), expected

def test_filter_df(tinyds):
    datasets_df = tinyds.to_df()
    # Test filtering on different Instruments
    filtered_df = utils.filter_df(
        datasets_df, instrument="bassoon")
    assert filtered_df["instrument"].unique() == ["bassoon"]

    filtered_df = utils.filter_df(
        datasets_df, instrument="trumpet")
    assert filtered_df["instrument"].unique() == ["trumpet"]

    # Test filtering on different datasets and dataset combinations.
    filtered_df = utils.filter_df(
        datasets_df, datasets=["rwc"])
    assert filtered_df["dataset"].unique() == ["rwc"]

    filtered_df = utils.filter_df(
        datasets_df, datasets=["philharmonia"])
    assert filtered_df["dataset"].unique() == ["philharmonia"]

    filtered_df = utils.filter_df(
        datasets_df, datasets=["philharmonia", "rwc"])
    assert set(filtered_df["dataset"].unique()) == \
           set(["philharmonia", "rwc"])

@pytest.fixture(scope="module", params=[
    (1, 1, 10), (1, 2, 10),
    (1, 4, 10), (1, 8, 10),
    (1, 100, 10)])
def noise_shape(request):
    param = request.param
    return param

def test_backfill_noise(noise_shape):
    noise = np.random.random(noise_shape)
    for t_len in [1, 2, 3, 8, 10]:
        backfilled = utils.backfill_noise(noise, t_len)
        assert backfilled.shape[1] >= t_len
<commit_msg>making sure to test case with different shapes, because it applies for wcqt/hcqt<commit_after>import numpy as np
import os
import pytest

import hcnn.common.utils as utils

def __eq(a, b):
    assert a == b

def test_create_directory(workspace):
    dname = os.path.join(workspace, "oh_hello")
    assert not os.path.exists(dname)
    assert utils.create_directory(dname)
    assert utils.create_directory(dname)

def test_filebase():
    fnames = ['y', 'y.z', 'x/y.z', 'x.y.z']
    results = ['y', 'y', 'y', 'x.y']
    for fn, res in zip(fnames, results):
        yield __eq, utils.filebase(fn), res

@pytest.mark.skipif(True, reason='todo')
def test_fold_array():
    assert False

@pytest.mark.skipif(True, reason='todo')
def test_map_io():
    assert False

def test_unzip():
    zip_files = [os.path.join(os.path.dirname(__file__), "zipped_folder.zip")]
    unzipped_folders = utils.unzip_files(zip_files)
    for dir_path in unzipped_folders:
        assert os.path.exists(dir_path) and os.path.isdir(dir_path)

@pytest.mark.skipif(True, reason='todo')
def test_slice_ndarray():
    assert False

@pytest.mark.skipif(True, reason='todo')
def test_colored():
    assert False

def test_iter_from_params_filepath():
    tests = [
        ("foo/bar/params/params0500.npz", "0500"),
        ("foo/bar/params/params10505.npz", "10505"),
        ("foo/bar/params/params0000.npz", "0000"),
        ("foo/bar/params/final.npz", "final"),
    ]

    def __test(value, expected):
        assert value == expected

    for test_input, expected in tests:
        yield __test, utils.iter_from_params_filepath(test_input), expected

def test_filter_df(tinyds):
    datasets_df = tinyds.to_df()
    # Test filtering on different Instruments
    filtered_df = utils.filter_df(
        datasets_df, instrument="bassoon")
    assert filtered_df["instrument"].unique() == ["bassoon"]

    filtered_df = utils.filter_df(
        datasets_df, instrument="trumpet")
    assert filtered_df["instrument"].unique() == ["trumpet"]

    # Test filtering on different datasets and dataset combinations.
    filtered_df = utils.filter_df(
        datasets_df, datasets=["rwc"])
    assert filtered_df["dataset"].unique() == ["rwc"]

    filtered_df = utils.filter_df(
        datasets_df, datasets=["philharmonia"])
    assert filtered_df["dataset"].unique() == ["philharmonia"]

    filtered_df = utils.filter_df(
        datasets_df, datasets=["philharmonia", "rwc"])
    assert set(filtered_df["dataset"].unique()) == \
           set(["philharmonia", "rwc"])

@pytest.fixture(scope="module", params=[
    (1, 1, 10, 100), (1, 2, 10),
    (1, 4, 10, 100, 5), (1, 8, 10),
    (1, 100, 10)])
def noise_shape(request):
    param = request.param
    return param

def test_backfill_noise(noise_shape):
    noise = np.random.random(noise_shape)
    for t_len in [1, 2, 3, 8, 10]:
        backfilled = utils.backfill_noise(noise, t_len)
        assert backfilled.shape[1] >= t_len
        assert noise.shape[2:] == backfilled.shape[2:]
<|endoftext|>


<commit_before>from zope.interface.verify import verifyClass, verifyObject
from ironman.communicator import Jarvis, SimpleIO
from ironman.interfaces import ICommunicationSlave
from ironman.hardware import HardwareManager
from ironman.globals import TESTPACKETS
from ironman.packet import IPBusPacket
from twisted.internet.defer import inlineCallbacks, returnValue

import pytest

hwmanager = HardwareManager()

class TestJarvis:
    @pytest.fixture(autouse=True)
    def set_up(self):
        # this checks a simple communication that works
        class FakeManager:
            def parse_address(self, addr):
                return 'file'

        self.j = Jarvis()
        self.j.set_hardware_manager(FakeManager())

    def test_jarvis_create(self):
        assert self.j is not None

    def test_jarvis_class_iface(self):
        # Assure the class implements the declared interface
        assert verifyClass(ICommunicationSlave, Jarvis)

    def test_jarvis_instance_iface(self):
        # Assure instances of the class provide the declared interface
        assert verifyObject(ICommunicationSlave, self.j)

    def test_jarvis_packet(self, tmpdir):
        f = tmpdir.mkdir("sub").join("hello.txt")
        f.write_binary("helloworld")

        @self.j.register('file')
        class TestIO(SimpleIO):
            __f__ = f.strpath

        p = IPBusPacket(TESTPACKETS['big-endian'])
        self.j(p)

        self.j.unregister('file')

    def test_jarvis_no_read(self):
        with pytest.raises(AttributeError) as excinfo:
            @self.j.register('bad-method')
            class Bad(object):
                def write(self): pass
        assert "has no attribute 'read'" in str(excinfo.value)

    def test_jarvis_no_write(self):
        with pytest.raises(AttributeError) as excinfo:
            @self.j.register('bad-method')
            class Bad(object):
                def read(self): pass
        assert "has no attribute 'write'" in str(excinfo.value)

    def test_jarvis_ducktyping(self):
        @self.j.register('ok-method')
        class Ok(object):
            def read(self): pass
            def write(self): pass
        assert 1  # we should have no error
<commit_msg>fix the tests<commit_after>from zope.interface.verify import verifyClass, verifyObject
from ironman.communicator import Jarvis, SimpleIO
from ironman.interfaces import ICommunicationSlave
from ironman.hardware import HardwareManager
from ironman.globals import TESTPACKETS
from ironman.packet import IPBusPacket
from twisted.internet.defer import inlineCallbacks, returnValue

import pytest

hwmanager = HardwareManager()

class TestJarvis:
    @pytest.fixture(autouse=True)
    def set_up(self):
        # this checks a simple communication that works
        class FakeManager:
            def get_route(self, addr):
                return 'file'

        self.j = Jarvis()
        self.j.set_hardware_manager(FakeManager())

    def test_jarvis_create(self):
        assert self.j is not None

    def test_jarvis_class_iface(self):
        # Assure the class implements the declared interface
        assert verifyClass(ICommunicationSlave, Jarvis)

    def test_jarvis_instance_iface(self):
        # Assure instances of the class provide the declared interface
        assert verifyObject(ICommunicationSlave, self.j)

    def test_jarvis_packet(self, tmpdir):
        f = tmpdir.mkdir("sub").join("hello.txt")
        f.write_binary("helloworld")

        @self.j.register('file')
        class TestIO(SimpleIO):
            __f__ = f.strpath

        p = IPBusPacket(TESTPACKETS['big-endian'])
        self.j(p)

        self.j.unregister('file')

    def test_jarvis_no_read(self):
        with pytest.raises(AttributeError) as excinfo:
            @self.j.register('bad-method')
            class Bad(object):
                def write(self): pass
        assert "has no attribute 'read'" in str(excinfo.value)

    def test_jarvis_no_write(self):
        with pytest.raises(AttributeError) as excinfo:
            @self.j.register('bad-method')
            class Bad(object):
                def read(self): pass
        assert "has no attribute 'write'" in str(excinfo.value)

    def test_jarvis_ducktyping(self):
        @self.j.register('ok-method')
        class Ok(object):
            def read(self): pass
            def write(self): pass
        assert 1  # we should have no error
<|endoftext|>


<commit_before># -*- coding: utf-8 -*-

import contextlib
import os
import tempfile
import textwrap

import mock
import py.test

from configloader import ConfigLoader

class test_obj:
    setting0 = 1
    SETTING0 = 2
    SETTING1 = 'blah'

test_obj_py = {
    'SETTING0': 2,
    'SETTING1': 'blah',
}

test_yaml = textwrap.dedent("""
    SETTING1: x
    SETTING2:
      - 1
      - 2
    SETTING3:
      foo: bar
""").strip()

test_yaml_py = {
    'SETTING1': 'x',
    'SETTING2': [1, 2],
    'SETTING3': {'foo': 'bar'},
}

test_json = textwrap.dedent("""
    {
        "SETTING3": "x",
        "SETTING4": [1, 2],
        "SETTING5": {"foo": "bar"}
    }
""").strip()

test_json_py = {
    'SETTING3': 'x',
    'SETTING4': [1, 2],
    'SETTING5': {'foo': 'bar'},
}

test_env = {
    'APP_SETTING5': 'x',
    'APP_SETTING6': 'y',
    'APP_SETTING7': 'z',
}

test_env_py = {
    'SETTING5': 'x',
    'SETTING6': 'y',
    'SETTING7': 'z',
}

test_combined_py = {
    'SETTING0': 2,
    'SETTING1': 'x',
    'SETTING2': [1, 2],
    'SETTING3': 'x',
    'SETTING4': [1, 2],
    'SETTING5': 'x',
    'SETTING6': 'y',
    'SETTING7': 'z',
}

@py.test.fixture
def config_loader():
    return ConfigLoader()

@contextlib.contextmanager
def temp_config_file(test_data):
    with tempfile.NamedTemporaryFile('wt') as configfile:
        configfile.write(test_data)
        configfile.seek(0)
        yield configfile.name

class TestConfigLoader:

    def test_init(self):
        config = ConfigLoader(logger=1)
        assert config.logger == 1

    @mock.patch('configloader.ConfigLoader.update_from_obj')
    @mock.patch('configloader.ConfigLoader.update_from_yaml_env')
    @mock.patch('configloader.ConfigLoader.update_from_yaml_file')
    @mock.patch('configloader.ConfigLoader.update_from_json_env')
    @mock.patch('configloader.ConfigLoader.update_from_json_file')
    @mock.patch('configloader.ConfigLoader.update_from_env_namespace')
    def test_update_from_calls(
            self,
            mock_ufen,
            mock_ufjf,
            mock_ufje,
            mock_ufyf,
            mock_ufye,
            mock_ufo,
            config_loader
            ):
        """Check that the expected functions are called."""
        config_loader.update_from(
            obj=1,
            yaml_env=2,
            yaml_file=3,
            json_env=4,
            json_file=5,
            env_namespace=6,
        )
        mock_ufo.assert_called_with(1)
        mock_ufye.assert_called_with(2)
        mock_ufyf.assert_called_with(3)
        mock_ufje.assert_called_with(4)
        mock_ufjf.assert_called_with(5)
        mock_ufen.assert_called_with(6)

    def test_update_from_merge(self, config_loader, monkeypatch):
        """Check that config chunks are applied in the expected order."""
        for key, value in test_env.items():
            monkeypatch.setenv(key, value)
        with temp_config_file(test_yaml) as yaml_filename, \
                temp_config_file(test_json) as json_filename:
            config_loader.update_from(
                obj=test_obj,
                yaml_file=yaml_filename,
                json_file=json_filename,
                env_namespace='APP'
            )
        assert config_loader == test_combined_py

    def test_update_from_obj(self, config_loader):
        config_loader.update_from_obj(test_obj)
        assert config_loader == test_obj_py

    def test_update_from_yaml_env(self, config_loader, monkeypatch):
        with temp_config_file(test_yaml) as yaml_filename:
            monkeypatch.setenv('CONFIG_YAML', yaml_filename)
            config_loader.update_from_yaml_env('CONFIG_YAML')
        assert config_loader == test_yaml_py

    def test_update_from_yaml_file(self, config_loader):
        with temp_config_file(test_yaml) as yaml_filename:
            config_loader.update_from_yaml_file(yaml_filename)
        assert config_loader == test_yaml_py

    def test_update_from_json_env(self, config_loader, monkeypatch):
        with temp_config_file(test_json) as json_filename:
            monkeypatch.setenv('CONFIG_JSON', json_filename)
            config_loader.update_from_json_env('CONFIG_JSON')
        assert config_loader == test_json_py

    def test_update_from_json_file(self, config_loader):
        with temp_config_file(test_json) as json_filename:
            config_loader.update_from_json_file(json_filename)
        assert config_loader == test_json_py

    def test_update_from_env_namespace(self):
        config = ConfigLoader()
        os.environ.update(test_env)
        config.update_from_env_namespace('APP')
        for key in test_env:
            del os.environ[key]
        assert config == test_env_py

    def test_namespace(self):
        config = ConfigLoader(
            PART1_SETTING1='x',
            PART1_SETTING2='y',
            PART2_SETTING1='z',
        )
        assert config.namespace('PART1') == dict(
            SETTING1='x',
            SETTING2='y',
        )
<commit_msg>test criterion argument for update_from_obj<commit_after># -*- coding: utf-8 -*-

import contextlib
import os
import tempfile
import textwrap

import mock
import py.test

from configloader import ConfigLoader

class test_obj:
    setting0 = 1
    SETTING0 = 2
    SETTING1 = 'blah'

test_obj_py = {
    'SETTING0': 2,
    'SETTING1': 'blah',
}

test_obj_py_no_criterion = {
    'setting0': 1,
    'SETTING0': 2,
    'SETTING1': 'blah',
}

test_yaml = textwrap.dedent("""
    SETTING1: x
    SETTING2:
      - 1
      - 2
    SETTING3:
      foo: bar
""").strip()

test_yaml_py = {
    'SETTING1': 'x',
    'SETTING2': [1, 2],
    'SETTING3': {'foo': 'bar'},
}

test_json = textwrap.dedent("""
    {
        "SETTING3": "x",
        "SETTING4": [1, 2],
        "SETTING5": {"foo": "bar"}
    }
""").strip()

test_json_py = {
    'SETTING3': 'x',
    'SETTING4': [1, 2],
    'SETTING5': {'foo': 'bar'},
}

test_env = {
    'APP_SETTING5': 'x',
    'APP_SETTING6': 'y',
    'APP_SETTING7': 'z',
}

test_env_py = {
    'SETTING5': 'x',
    'SETTING6': 'y',
    'SETTING7': 'z',
}

test_combined_py = {
    'SETTING0': 2,
    'SETTING1': 'x',
    'SETTING2': [1, 2],
    'SETTING3': 'x',
    'SETTING4': [1, 2],
    'SETTING5': 'x',
    'SETTING6': 'y',
    'SETTING7': 'z',
}

@py.test.fixture
def config_loader():
    return ConfigLoader()

@contextlib.contextmanager
def temp_config_file(test_data):
    with tempfile.NamedTemporaryFile('wt') as configfile:
        configfile.write(test_data)
        configfile.seek(0)
        yield configfile.name

class TestConfigLoader:

    def test_init(self):
        config = ConfigLoader(logger=1)
        assert config.logger == 1

    @mock.patch('configloader.ConfigLoader.update_from_obj')
    @mock.patch('configloader.ConfigLoader.update_from_yaml_env')
    @mock.patch('configloader.ConfigLoader.update_from_yaml_file')
    @mock.patch('configloader.ConfigLoader.update_from_json_env')
    @mock.patch('configloader.ConfigLoader.update_from_json_file')
    @mock.patch('configloader.ConfigLoader.update_from_env_namespace')
    def test_update_from_calls(
            self,
            mock_ufen,
            mock_ufjf,
            mock_ufje,
            mock_ufyf,
            mock_ufye,
            mock_ufo,
            config_loader
            ):
        """Check that the expected functions are called."""
        config_loader.update_from(
            obj=1,
            yaml_env=2,
            yaml_file=3,
            json_env=4,
            json_file=5,
            env_namespace=6,
        )
        mock_ufo.assert_called_with(1)
        mock_ufye.assert_called_with(2)
        mock_ufyf.assert_called_with(3)
        mock_ufje.assert_called_with(4)
        mock_ufjf.assert_called_with(5)
        mock_ufen.assert_called_with(6)

    def test_update_from_merge(self, config_loader, monkeypatch):
        """Check that config chunks are applied in the expected order."""
        for key, value in test_env.items():
            monkeypatch.setenv(key, value)
        with temp_config_file(test_yaml) as yaml_filename, \
                temp_config_file(test_json) as json_filename:
            config_loader.update_from(
                obj=test_obj,
                yaml_file=yaml_filename,
                json_file=json_filename,
                env_namespace='APP'
            )
        assert config_loader == test_combined_py

    def test_update_from_obj(self, config_loader):
        config_loader.update_from_obj(test_obj)
        assert config_loader == test_obj_py

    def test_update_from_obj_criterion(self, config_loader):
        config_loader.update_from_obj(
            test_obj,
            criterion=lambda key: key[:1] != '_'
        )
        assert config_loader == test_obj_py_no_criterion

    def test_update_from_yaml_env(self, config_loader, monkeypatch):
        with temp_config_file(test_yaml) as yaml_filename:
            monkeypatch.setenv('CONFIG_YAML', yaml_filename)
            config_loader.update_from_yaml_env('CONFIG_YAML')
        assert config_loader == test_yaml_py

    def test_update_from_yaml_file(self, config_loader):
        with temp_config_file(test_yaml) as yaml_filename:
            config_loader.update_from_yaml_file(yaml_filename)
        assert config_loader == test_yaml_py

    def test_update_from_json_env(self, config_loader, monkeypatch):
        with temp_config_file(test_json) as json_filename:
            monkeypatch.setenv('CONFIG_JSON', json_filename)
            config_loader.update_from_json_env('CONFIG_JSON')
        assert config_loader == test_json_py

    def test_update_from_json_file(self, config_loader):
        with temp_config_file(test_json) as json_filename:
            config_loader.update_from_json_file(json_filename)
        assert config_loader == test_json_py

    def test_update_from_env_namespace(self):
        config = ConfigLoader()
        os.environ.update(test_env)
        config.update_from_env_namespace('APP')
        for key in test_env:
            del os.environ[key]
        assert config == test_env_py

    def test_namespace(self):
        config = ConfigLoader(
            PART1_SETTING1='x',
            PART1_SETTING2='y',
            PART2_SETTING1='z',
        )
        assert config.namespace('PART1') == dict(
            SETTING1='x',
            SETTING2='y',
        )
<|endoftext|>


<commit_before>import unittest
import six

from . import settings
from tests.data import valid_data
from tests.data import invalid_data

from craftai.client import CraftAIClient
from craftai import errors as craft_err

class TestCreateAgentSuccess(unittest.TestCase):
    """Checks that the client succeeds when creating an agent with OK input"""

    @classmethod
    def setUpClass(self):
        self.client = CraftAIClient(settings.CRAFT_CFG)

    def setUp(self):
        # Makes sure that no agent with the same ID already exists
        self.client.delete_agent(valid_data.VALID_ID)

    def clean_up_agent(self, aid):
        # Makes sure that no agent with the standard ID remains
        self.client.delete_agent(aid)

    def test_create_agent_with_generated_agent_id(self):
        """create_agent should succeed when given an empty `id` field

        It should give a proper JSON response with `id` and
        `model` fields being strings.
        """
        resp = self.client.create_agent(valid_data.VALID_MODEL)
        self.assertIsInstance(resp.get("id"), six.string_types)
        self.addCleanup(self.clean_up_agent, resp.get("id"))

    def test_create_agent_given_agent_id(self):
        """create_agent should succeed when given a string ID

        It should give a proper JSON response with `id` and
        `model` fields being strings and `id` being the same as the one
        given as a parameter.
        """
        resp = self.client.create_agent(
            valid_data.VALID_MODEL,
            valid_data.VALID_ID)
        self.assertEqual(resp.get("id"), valid_data.VALID_ID)
        self.addCleanup(self.clean_up_agent, valid_data.VALID_ID)

class TestCreateAgentFailure(unittest.TestCase):
    """Checks that the client fails when creating an agent with bad input"""

    @classmethod
    def setUpClass(self):
        self.client = CraftAIClient(settings.CRAFT_CFG)

    def setUp(self):
        # Makes sure that no agent with the same ID already exists
        self.client.delete_agent(valid_data.VALID_ID)

    def test_create_agent_with_invalid_given_agent_id(self):
        """create_agent should fail when given a non-string/empty string ID

        It should raise an error upon request for creation of
        an agent with an ID that is not of type string, since agent IDs
        should always be strings.
        """
        for inv_id in invalid_data.INVALID_IDS:
            self.assertRaises(
                craft_err.CraftAIBadRequestError,
                self.client.create_agent,
                valid_data.VALID_MODEL,
                invalid_data.INVALID_IDS[inv_id])

    def test_create_agent_with_existing_agent_id(self):
        """create_agent should fail when given an ID that already exists

        It should raise an error upon request for creation of
        an agent with an ID that already exists, since agent IDs
        should always be unique.
        """
        # Calling create_agent a first time
        self.client.create_agent(valid_data.VALID_MODEL, valid_data.VALID_ID)
        # Asserting that an error is risen the second time
        self.assertRaises(
            craft_err.CraftAIBadRequestError,
            self.client.create_agent,
            valid_data.VALID_MODEL,
            valid_data.VALID_ID)

    def test_create_agent_with_invalid_context(self):
        """create_agent should fail when given an invalid or no context

        It should raise an error upon request for creation of
        an agent with no context or a context that is invalid.
        """
        for inv_context in invalid_data.INVALID_CONTEXTS:
            model = {
                "context": invalid_data.INVALID_CONTEXTS[inv_context],
                "output": ["lightbulbColor"],
                "time_quantum": 100
            }
            print(model)
            self.assertRaises(
                craft_err.CraftAIBadRequestError,
                self.client.create_agent,
                model,
                valid_data.VALID_ID)

    def test_create_agent_with_undefined_model(self):
        """create_agent should fail when given no model key in the request body

        It should raise an error upon request for creation of an agent with
        no model key in the request body, since it is a mandatory field to
        create an agent.
        """

        # Testing all non dict model cases
        for empty_model in invalid_data.UNDEFINED_KEY:
            if not (isinstance(invalid_data.UNDEFINED_KEY[empty_model],
                               (dict, list))):
                self.assertRaises(
                    craft_err.CraftAIBadRequestError,
                    self.client.create_agent,
                    invalid_data.UNDEFINED_KEY[empty_model],
                    valid_data.VALID_ID)

    def test_create_agent_with_unknown_model(self):
        """create_agent should fail when given an unknown model

        It should raise an error upon request for creation of an agent with
        no valid model in the request body, since it is a mandatory field to
        create an agent.
        """
        # Testing all dict model cases
        for empty_model in invalid_data.UNDEFINED_KEY:
            if isinstance(invalid_data.UNDEFINED_KEY[empty_model],
                          (dict, list)):
                self.assertRaises(
                    craft_err.CraftAINotFoundError,
                    self.client.create_agent,
                    invalid_data.UNDEFINED_KEY[empty_model],
                    valid_data.VALID_ID)

    def test_create_agent_with_invalid_time_quantum(self):
        """create_agent should fail when given an invalid time quantum

        It should raise an error upon request for creation of an agent with
        an incorrect time quantum in the model, since it is essential to
        perform any action with craft ai.
        """
        for inv_tq in invalid_data.INVALID_TIME_QUANTA:
            print(invalid_data.INVALID_TIME_QUANTA[inv_tq])
            model = {
                "context": valid_data.VALID_CONTEXT,
                "output": valid_data.VALID_OUTPUT,
                "time_quantum": invalid_data.INVALID_TIME_QUANTA[inv_tq]
            }
            self.assertRaises(
                craft_err.CraftAIBadRequestError,
                self.client.create_agent,
                model,
                valid_data.VALID_ID)
<commit_msg>Added security to create_agent tests<commit_after>import unittest
import six

from . import settings
from tests.data import valid_data
from tests.data import invalid_data

from craftai.client import CraftAIClient
from craftai import errors as craft_err

class TestCreateAgentSuccess(unittest.TestCase):
    """Checks that the client succeeds when creating an agent with OK input"""

    @classmethod
    def setUpClass(self):
        self.client = CraftAIClient(settings.CRAFT_CFG)

    def setUp(self):
        # Makes sure that no agent with the same ID already exists
        self.client.delete_agent(valid_data.VALID_ID)

    def clean_up_agent(self, aid):
        # Makes sure that no agent with the standard ID remains
        self.client.delete_agent(aid)

    def test_create_agent_with_generated_agent_id(self):
        """create_agent should succeed when given an empty `id` field

        It should give a proper JSON response with `id` and
        `model` fields being strings.
        """
        resp = self.client.create_agent(valid_data.VALID_MODEL)
        self.assertIsInstance(resp.get("id"), six.string_types)
        self.addCleanup(self.clean_up_agent, resp.get("id"))

    def test_create_agent_given_agent_id(self):
        """create_agent should succeed when given a string ID

        It should give a proper JSON response with `id` and
        `model` fields being strings and `id` being the same as the one
        given as a parameter.
        """
        resp = self.client.create_agent(
            valid_data.VALID_MODEL,
            valid_data.VALID_ID)
        self.assertEqual(resp.get("id"), valid_data.VALID_ID)
        self.addCleanup(self.clean_up_agent, valid_data.VALID_ID)

class TestCreateAgentFailure(unittest.TestCase):
    """Checks that the client fails when creating an agent with bad input"""

    @classmethod
    def setUpClass(self):
        self.client = CraftAIClient(settings.CRAFT_CFG)

    def setUp(self):
        # Makes sure that no agent with the same ID already exists
        self.client.delete_agent(valid_data.VALID_ID)

    def clean_up_agent(self, aid):
        print(aid)
        # Makes sure that no agent with the standard ID remains
        self.client.delete_agent(aid)

    def test_create_agent_with_invalid_given_agent_id(self):
        """create_agent should fail when given a non-string/empty string ID

        It should raise an error upon request for creation of
        an agent with an ID that is not of type string, since agent IDs
        should always be strings.
        """
        for inv_id_name in invalid_data.INVALID_IDS:
            inv_id = invalid_data.INVALID_IDS[inv_id_name]
            self.assertRaises(
                craft_err.CraftAIBadRequestError,
                self.client.create_agent,
                valid_data.VALID_MODEL,
                inv_id)
            if isinstance(inv_id, six.string_types):
                self.addCleanup(
                    self.clean_up_agent,
                    inv_id
                )

    def test_create_agent_with_existing_agent_id(self):
        """create_agent should fail when given an ID that already exists

        It should raise an error upon request for creation of
        an agent with an ID that already exists, since agent IDs
        should always be unique.
        """
        # Calling create_agent a first time
        self.client.create_agent(valid_data.VALID_MODEL, valid_data.VALID_ID)
        # Asserting that an error is risen the second time
        self.assertRaises(
            craft_err.CraftAIBadRequestError,
            self.client.create_agent,
            valid_data.VALID_MODEL,
            valid_data.VALID_ID)
        self.addCleanup(
            self.clean_up_agent,
            valid_data.VALID_ID
        )

    def test_create_agent_with_invalid_context(self):
        """create_agent should fail when given an invalid or no context

        It should raise an error upon request for creation of
        an agent with no context or a context that is invalid.
        """
        for inv_context in invalid_data.INVALID_CONTEXTS:
            model = {
                "context": invalid_data.INVALID_CONTEXTS[inv_context],
                "output": ["lightbulbColor"],
                "time_quantum": 100
            }
            print(model)
            self.assertRaises(
                craft_err.CraftAIBadRequestError,
                self.client.create_agent,
                model,
                valid_data.VALID_ID)
            self.addCleanup(
                self.clean_up_agent,
                valid_data.VALID_ID
            )

    def test_create_agent_with_undefined_model(self):
        """create_agent should fail when given no model key in the request body

        It should raise an error upon request for creation of an agent with
        no model key in the request body, since it is a mandatory field to
        create an agent.
        """

        # Testing all non dict model cases
        for empty_model in invalid_data.UNDEFINED_KEY:
            if not (isinstance(invalid_data.UNDEFINED_KEY[empty_model],
                               (dict, list))):
                self.assertRaises(
                    craft_err.CraftAIBadRequestError,
                    self.client.create_agent,
                    invalid_data.UNDEFINED_KEY[empty_model],
                    valid_data.VALID_ID)
                self.addCleanup(
                    self.clean_up_agent,
                    valid_data.VALID_ID
                )

    def test_create_agent_with_unknown_model(self):
        """create_agent should fail when given an unknown model

        It should raise an error upon request for creation of an agent with
        no valid model in the request body, since it is a mandatory field to
        create an agent.
        """
        # Testing all dict model cases
        for empty_model in invalid_data.UNDEFINED_KEY:
            if isinstance(invalid_data.UNDEFINED_KEY[empty_model],
                          (dict, list)):
                self.assertRaises(
                    craft_err.CraftAINotFoundError,
                    self.client.create_agent,
                    invalid_data.UNDEFINED_KEY[empty_model],
                    valid_data.VALID_ID)
                self.addCleanup(
                    self.clean_up_agent,
                    valid_data.VALID_ID
                )

    def test_create_agent_with_invalid_time_quantum(self):
        """create_agent should fail when given an invalid time quantum

        It should raise an error upon request for creation of an agent with
        an incorrect time quantum in the model, since it is essential to
        perform any action with craft ai.
        """
        for inv_tq in invalid_data.INVALID_TIME_QUANTA:
            print(invalid_data.INVALID_TIME_QUANTA[inv_tq])
            model = {
                "context": valid_data.VALID_CONTEXT,
                "output": valid_data.VALID_OUTPUT,
                "time_quantum": invalid_data.INVALID_TIME_QUANTA[inv_tq]
            }
            self.assertRaises(
                craft_err.CraftAIBadRequestError,
                self.client.create_agent,
                model,
                valid_data.VALID_ID)
            self.addCleanup(
                self.clean_up_agent,
                valid_data.VALID_ID
            )
<|endoftext|>


<commit_before>#!/usr/bin/env python

import errno
import os
import platform
import sys

BASE_URL = 'http://gh-contractor-zcbenz.s3.amazonaws.com/libchromiumcontent'
LIBCHROMIUMCONTENT_COMMIT = '0fb4fbe55f5a967b960644f0fdc2013668d5a46a'

PLATFORM = {
  'cygwin': 'win32',
  'darwin': 'darwin',
  'linux2': 'linux',
  'win32': 'win32',
}[sys.platform]

verbose_mode = False

def get_target_arch():
  # Always build 64bit on OS X.
  if PLATFORM == 'darwin':
    return 'x64'
  # Only build for host's arch on Linux.
  elif PLATFORM == 'linux':
    if platform.architecture()[0] == '32bit':
      return 'ia32'
    else:
      return 'x64'
  # On Windows it depends on user.
  elif PLATFORM == 'win32':
    try:
      target_arch_path = os.path.join(__file__, '..', '..', '..', 'vendor',
                                      'brightray', 'vendor', 'download',
                                      'libchromiumcontent', '.target_arch')
      with open(os.path.normpath(target_arch_path)) as f:
        return f.read().strip()
    except IOError as e:
      if e.errno != errno.ENOENT:
        raise
    # Build 32bit by default.
    return 'ia32'
  # Maybe we will support other platforms in future.
  else:
    return 'x64'

def s3_config():
  config = (os.environ.get('ATOM_SHELL_S3_BUCKET', ''),
            os.environ.get('ATOM_SHELL_S3_ACCESS_KEY', ''),
            os.environ.get('ATOM_SHELL_S3_SECRET_KEY', ''))
  message = ('Error: Please set the $ATOM_SHELL_S3_BUCKET, '
             '$ATOM_SHELL_S3_ACCESS_KEY, and '
             '$ATOM_SHELL_S3_SECRET_KEY environment variables')
  assert all(len(c) for c in config), message
  return config

def enable_verbose_mode():
  print 'Running in verbose mode'
  global verbose_mode
  verbose_mode = True

def is_verbose_mode():
  return verbose_mode
<commit_msg>Update libchromiumcontent which includes pdf and ppapi libs<commit_after>#!/usr/bin/env python

import errno
import os
import platform
import sys

BASE_URL = 'http://gh-contractor-zcbenz.s3.amazonaws.com/libchromiumcontent'
LIBCHROMIUMCONTENT_COMMIT = '07a73a610496e4a1b4f3abc3c2fb0516187ec460'

PLATFORM = {
  'cygwin': 'win32',
  'darwin': 'darwin',
  'linux2': 'linux',
  'win32': 'win32',
}[sys.platform]

verbose_mode = False

def get_target_arch():
  # Always build 64bit on OS X.
  if PLATFORM == 'darwin':
    return 'x64'
  # Only build for host's arch on Linux.
  elif PLATFORM == 'linux':
    if platform.architecture()[0] == '32bit':
      return 'ia32'
    else:
      return 'x64'
  # On Windows it depends on user.
  elif PLATFORM == 'win32':
    try:
      target_arch_path = os.path.join(__file__, '..', '..', '..', 'vendor',
                                      'brightray', 'vendor', 'download',
                                      'libchromiumcontent', '.target_arch')
      with open(os.path.normpath(target_arch_path)) as f:
        return f.read().strip()
    except IOError as e:
      if e.errno != errno.ENOENT:
        raise
    # Build 32bit by default.
    return 'ia32'
  # Maybe we will support other platforms in future.
  else:
    return 'x64'

def s3_config():
  config = (os.environ.get('ATOM_SHELL_S3_BUCKET', ''),
            os.environ.get('ATOM_SHELL_S3_ACCESS_KEY', ''),
            os.environ.get('ATOM_SHELL_S3_SECRET_KEY', ''))
  message = ('Error: Please set the $ATOM_SHELL_S3_BUCKET, '
             '$ATOM_SHELL_S3_ACCESS_KEY, and '
             '$ATOM_SHELL_S3_SECRET_KEY environment variables')
  assert all(len(c) for c in config), message
  return config

def enable_verbose_mode():
  print 'Running in verbose mode'
  global verbose_mode
  verbose_mode = True

def is_verbose_mode():
  return verbose_mode
<|endoftext|>