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

# Display a white target moving across a textured drum.

########################################################
#  Import various modules from the Vision Egg package  #
########################################################

import math
from VisionEgg.Core import *
from VisionEgg.AppHelper import *
from VisionEgg.MoreStimuli import *
from VisionEgg.Textures import *

# Initialize an OpenGL window
screen = get_default_screen()

############################################
#  Setup the background - a spinning drum  #
############################################

# Create a perspective projection for the spinning drum
perspective = SimplePerspectiveProjection(fov_x=90.0)

# Create a viewport with this projection
perspective_viewport = Viewport(screen,
                                size=screen.size,
                                projection=perspective)

# Now create a spinning drum stimulus
try:
    texture = TextureFromFile("orig.bmp") # try to open a texture file
except:
    texture = Texture(size=(256,16)) # otherwise, generate one
drum = SpinningDrum(texture=texture)

# Add the spinning drum to the viewport
perspective_viewport.add_stimulus(drum)

############################################
#  Setup a small target in the foreground  #
############################################

# Create an orthographic projection so that OpenGL
# object coordinates are equal to window (pixel) coordinates.
pixel_coords = OrthographicProjection(left=0,right=screen.size[0],
                                      bottom=0,top=screen.size[1])

# Create a viewport with this projection
flat_viewport = Viewport(screen,
                         size=screen.size,
                         projection=pixel_coords)

# Create an instance of the Target2D class with appropriate parameters
target = Target2D(size  = (25.0,10.0),
                  color = (1.0,1.0,1.0,1.0), # Set the target color (RGBA) white
                  orientation = 135.0)

# Add the small target to the appropriate viewport
flat_viewport.add_stimulus(target)

##################################################
#  Create an instance of the Presentation class  #
##################################################

# Add flat_viewport last so its stimulus is drawn last. This way the
# target is drawn after the drum and is visible, without worrying
# about depth testing.
p = Presentation(duration=(10.0,'seconds'),
                 viewports=[perspective_viewport,flat_viewport])

#################################
#  Define controller functions  #
#################################

# A few variables used to calculate postion based on screen size
mid_x = screen.size[0]/2.0
mid_y = screen.size[1]/2.0
if screen.size[0] < screen.size[1]:
    max_vel = screen.size[0] * 0.4
else:
    max_vel = screen.size[1] * 0.4

# Control the small target:
def xy_as_function_of_time(t):
    return ( max_vel*sin(0.1*2.0*math.pi*t) + mid_x , # x
             max_vel*sin(0.1*2.0*math.pi*t) + mid_y ) # y

# Control the background:
def angle_as_function_of_time(t):
    return 50.0*math.cos(0.2*2*math.pi*t)

# Control anything that should be off between presentations:
def one_during_experiment(t):
    if t < 0.0:
        return 0.0
    else:
        return 1.0

#############################################################
#  Connect the controllers with the variables they control  #
#############################################################

p.add_realtime_time_controller(target,'center', xy_as_function_of_time)
p.add_realtime_time_controller(drum,'angular_position', angle_as_function_of_time)
p.add_transitional_controller(target,'on', one_during_experiment)
p.add_transitional_controller(drum,'contrast', one_during_experiment)

#######################
#  Run the stimulus!  #
#######################

p.go()

<commit_msg>Removed explicit flat projection setting.<commit_after>#!/usr/bin/env python

# Display a white target moving across a textured drum.

########################################################
#  Import various modules from the Vision Egg package  #
########################################################

import math
from VisionEgg.Core import *
from VisionEgg.AppHelper import *
from VisionEgg.MoreStimuli import *
from VisionEgg.Textures import *

# Initialize an OpenGL window
screen = get_default_screen()

############################################
#  Setup the background - a spinning drum  #
############################################

# Create a perspective projection for the spinning drum
perspective = SimplePerspectiveProjection(fov_x=90.0)

# Create a viewport with this projection
perspective_viewport = Viewport(screen=screen,
                                projection=perspective)

# Now create a spinning drum stimulus
try:
    texture = TextureFromFile("orig.bmp") # try to open a texture file
except:
    texture = Texture(size=(256,16)) # otherwise, generate one
drum = SpinningDrum(texture=texture)

# Add the spinning drum to the viewport
perspective_viewport.add_stimulus(drum)

############################################
#  Setup a small target in the foreground  #
############################################

# Use viewport with pixel coordinate system for projection
flat_viewport = Viewport(screen=screen)

# Create an instance of the Target2D class with appropriate parameters
target = Target2D(size  = (25.0,10.0),
                  color = (1.0,1.0,1.0,1.0), # Set the target color (RGBA) white
                  orientation = 135.0)

# Add the small target to the appropriate viewport
flat_viewport.add_stimulus(target)

##################################################
#  Create an instance of the Presentation class  #
##################################################

# Add flat_viewport last so its stimulus is drawn last. This way the
# target is drawn after the drum and is visible, without worrying
# about depth testing.
p = Presentation(duration=(10.0,'seconds'),
                 viewports=[perspective_viewport,flat_viewport])

#################################
#  Define controller functions  #
#################################

# A few variables used to calculate postion based on screen size
mid_x = screen.size[0]/2.0
mid_y = screen.size[1]/2.0
if screen.size[0] < screen.size[1]:
    max_vel = screen.size[0] * 0.4
else:
    max_vel = screen.size[1] * 0.4

# Control the small target:
def xy_as_function_of_time(t):
    return ( max_vel*sin(0.1*2.0*math.pi*t) + mid_x , # x
             max_vel*sin(0.1*2.0*math.pi*t) + mid_y ) # y

# Control the background:
def angle_as_function_of_time(t):
    return 50.0*math.cos(0.2*2*math.pi*t)

# Control anything that should be off between presentations:
def one_during_experiment(t):
    if t < 0.0:
        return 0.0
    else:
        return 1.0

#############################################################
#  Connect the controllers with the variables they control  #
#############################################################

p.add_realtime_time_controller(target,'center', xy_as_function_of_time)
p.add_realtime_time_controller(drum,'angular_position', angle_as_function_of_time)
p.add_transitional_controller(target,'on', one_during_experiment)
p.add_transitional_controller(drum,'contrast', one_during_experiment)

#######################
#  Run the stimulus!  #
#######################

p.go()

<|endoftext|>


<commit_before># -*- encoding: utf-8 -*-
'''
Loader and primary interface for nova modules

:maintainer: basepi
:maturity: 20160218
:platform: All
:requires: SaltStack

See README for documentation

Configuration:
    - hubblestack.nova.dir
    - hubblestack.nova.saltenv
    - hubblestack.nova.autoload
    - hubblestack.nova.autosync
'''
from __future__ import absolute_import
import logging

log = logging.getLogger(__name__)

import imp
import os
import sys
import six
import inspect

import salt
import salt.utils
from salt.exceptions import CommandExecutionError
from salt.loader import LazyLoader

__nova__ = {}

def audit(modules='',
          tag='*',
          verbose=False,
          show_success=True,
          report_compliance=True):
    '''
    Primary entry point for audit calls.

    modules
        List (comma-separated or python list) of modules/directories to search
        for audit modules. Directories are dot-separated, much in the same way
        as Salt states. For individual module names, leave the .py extension
        off.  If a given path resolves to a python file, it will be treated as
        a single module. Otherwise it will be treated as a directory. All
        modules found in a recursive search of the specified directories will
        be included in the audit.

    tags
        Glob pattern string for tags to include in the audit. This way you can
        give a directory, and tell the system to only run the `CIS*`-tagged
        audit modules, for example.

    verbose
        Whether to show additional information about audits, including
        description, remediation instructions, etc. The data returned depends
        on the audit module. Defaults to False.

    show_success
        Whether to show successful audits in addition to failed audits.
        Defaults to True.

    report_compliance
        Whether to show compliance as a percentage (successful checks divided
        by total checks). Defaults to True.
    '''
    if __salt__['config.get']('hubblestack.nova.autoload', True):
        load()
    if not __nova__:
        return False, 'No nova modules have been loaded.'

    if not isinstance(modules, list):
        # Convert string
        modules = modules.split(',')

    # Convert module list to paths, with leading slashes
    modules = [os.path.join('/', os.path.join(*mod.split('.'))) for mod in modules]

    results = {'Success': [], 'Failure': []}
    already_run = set()
    # This is a pretty terrible way to iterate, performance-wise
    # However, the data sets should be relatively small, so I don't anticipate
    # issues.
    for module in modules:
        for key, func in __nova__._dict.iteritems():
            if key not in already_run and key.startswith(module):
                # Found a match, run the audit
                ret = func(tag, verbose=verbose)

                # Make sure we don't run the same audit twice
                already_run.add(key)

                # Compile the results
                results['Success'].extend(ret.get('Success', []))
                results['Failure'].extend(ret.get('Failure', []))

    if show_compliance:
        compliance = float(len(results['Success']))/(len(results['Success']) +
                                                     len(results['Failure']))
        compliance = int(compliance * 100)
        results['Compliance'] = '{0}%'.format(compliance)

    if not show_success:
        results.pop('Success')

    return results

def sync():
    '''
    Sync the nove audit modules from the saltstack fileserver.

    The modules should be stored in the salt fileserver. By default nova will
    search the base environment for a top level ``hubblestack_nova`` directory,
    unless otherwise specified via pillar or minion config
    (``hubblestack.nova.dir``)

    Modules will just be cached in the normal minion cachedir

    Returns the minion's path to the cached directory

    NOTE: This function will also clean out existing files at the cached
    location, as cp.cache_dir doesn't clean out old files

    CLI Examples:

    .. code-block:: bash

        salt '*' nova.sync
        salt '*' nova.sync saltenv=hubble
    '''
    log.debug('syncing nova modules')
    nova_dir = __salt__['config.get']('hubblestack.nova.dir', 'salt://hubblestack_nova')
    saltenv = __salt__['config.get']('hubblestack.nova.saltenv', 'base')

    # Support optional salt:// in config
    if 'salt://' in nova_dir:
        path = nova_dir
        _, _, nova_dir = nova_dir.partition('salt://')
    else:
        path = 'salt://{0}'.format(nova_dir)

    # Clean previously synced files
    __salt__['file.remove'](_hubble_dir())
    # Sync the files
    cached = __salt__['cp.cache_dir'](path, saltenv=saltenv)

    if cached and isinstance(cached, list):
        # Success! Trim the paths
        cachedir = _hubble_dir()
        ret = [relative.partition(cachedir)[2] for relative in cached]
        return ret
    else:
        if isinstance(cached, list):
            # Nothing was found
            return cached
        else:
            # Something went wrong, there's likely a stacktrace in the output
            # of cache_dir
            raise CommandExecutionError('An error occurred while syncing: {0}'
                                        .format(cached))

def load():
    '''
    Load the synced audit modules.
    '''
    if __salt__['config.get']('hubblestack.nova.autosync', True):
        sync()
    if not os.path.isdir(_hubble_dir()):
        return False, 'No synced nova modules found'

    log.debug('loading nova modules')

    global __nova__
    __nova__ = NovaLazyLoader()

    ret = {'loaded': __nova__._dict.keys(),
           'missing': __nova__.missing_modules}
    return ret

def _hubble_dir():
    '''
    Generate the local minion directory to which nova modules are synced
    '''
    nova_dir = __salt__['config.get']('hubblestack.nova.dir', 'hubblestack_nova')
    saltenv = __salt__['config.get']('hubblestack.nova.saltenv', 'base')
    cachedir = os.path.join(__opts__.get('cachedir'),
                            'files',
                            saltenv,
                            nova_dir)
    return cachedir

class NovaLazyLoader(LazyLoader):
    '''
    Leverage the SaltStack LazyLoader so we don't have to reimplement
    everything. Note that in general, we'll just call _load_all, so this
    will not actually be a lazy loader, but leveraging the existing code is
    worth it.
    '''

    def __init__(self):
        super(NovaLazyLoader, self).__init__([_hubble_dir()],
                                             opts=__opts__,
                                             tag='nova')
        self._load_all()

    def refresh_file_mapping(self):
        '''
        Override the default refresh_file_mapping to look for nova files
        recursively, rather than only in a top-level directory
        '''
        # map of suffix to description for imp
        self.suffix_map = {}
        suffix_order = []  # local list to determine precedence of extensions
        for (suffix, mode, kind) in imp.get_suffixes():
            self.suffix_map[suffix] = (suffix, mode, kind)
            suffix_order.append(suffix)

        # create mapping of filename (without suffix) to (path, suffix)
        self.file_mapping = {}

        for mod_dir in self.module_dirs:
            for dirname, dirs, files in os.walk(mod_dir):
                if '.git' in dirs:
                    dirs.remove('.git')
                for filename in files:
                    try:
                        if filename.startswith('_'):
                            # skip private modules
                            # log messages omitted for obviousness
                            continue
                        _, ext = os.path.splitext(filename)
                        fpath = os.path.join(dirname, filename)
                        f_noext, _ = os.path.splitext(fpath.partition(mod_dir)[-1])
                        # Nova only supports .py
                        if ext not in ['.py']:
                            continue
                        if f_noext in self.disabled:
                            log.trace(
                                'Skipping {0}, it is disabled by configuration'.format(
                                filename
                                )
                            )
                            continue

                        # if we don't have it, we want it
                        elif f_noext not in self.file_mapping:
                            self.file_mapping[f_noext] = (fpath, ext)
                        # if we do, we want it if we have a higher precidence ext
                        else:
                            curr_ext = self.file_mapping[f_noext][1]
                            #log.debug("****** curr_ext={0} ext={1} suffix_order={2}".format(curr_ext, ext, suffix_order))
                            if curr_ext and suffix_order.index(ext) < suffix_order.index(curr_ext):
                                self.file_mapping[f_noext] = (fpath, ext)
                    except OSError:
                        continue

    def _load_module(self, name):
        '''
        Override the module load code
        '''
        mod = None
        fpath, suffix = self.file_mapping[name]
        self.loaded_files.add(name)
        try:
            sys.path.append(os.path.dirname(fpath))
            desc = self.suffix_map[suffix]
            # if it is a directory, we don't open a file
            with salt.utils.fopen(fpath, desc[1]) as fn_:
                mod = imp.load_module(
                    '{0}.{1}.{2}.{3}'.format(
                        self.loaded_base_name,
                        self.mod_type_check(fpath),
                        self.tag,
                        name
                    ), fn_, fpath, desc)

        except IOError:
            raise
        except ImportError as error:
            log.debug(
                'Failed to import {0} {1}:\n'.format(
                    self.tag, name
                ),
                exc_info=True
            )
            self.missing_modules[name] = str(error)
            return False
        except Exception as error:
            log.error(
                'Failed to import {0} {1}, this is due most likely to a '
                'syntax error:\n'.format(
                    self.tag, name
                ),
                exc_info=True
            )
            self.missing_modules[name] = str(error)
            return False
        except SystemExit as error:
            log.error(
                'Failed to import {0} {1} as the module called exit()\n'.format(
                    self.tag, name
                ),
                exc_info=True
            )
            self.missing_modules[name] = str(error)
            return False
        finally:
            sys.path.pop()

        mod.__grains__ = __grains__
        mod.__pillar__ = __pillar__
        mod.__opts__ = __opts__
        mod.__salt__ = __salt__

        # pack whatever other globals we were asked to
        for p_name, p_value in six.iteritems(self.pack):
            setattr(mod, p_name, p_value)

        module_name = name

        # Call a module's initialization method if it exists
        module_init = getattr(mod, '__init__', None)
        if inspect.isfunction(module_init):
            try:
                module_init(self.opts)
            except TypeError as e:
                log.error(e)
            except Exception:
                err_string = '__init__ failed'
                log.debug(
                    'Error loading {0}.{1}: {2}'.format(
                        self.tag,
                        module_name,
                        err_string),
                    exc_info=True)
                self.missing_modules[name] = err_string
                return False

        # if virtual modules are enabled, we need to look for the
        # __virtual__() function inside that module and run it.
        if self.virtual_enable:
            (virtual_ret, module_name, virtual_err) = self.process_virtual(
                mod,
                module_name,
            )
            if virtual_err is not None:
                log.debug('Error loading {0}.{1}: {2}'.format(self.tag,
                                                              module_name,
                                                              virtual_err,
                                                              ))

            # if process_virtual returned a non-True value then we are
            # supposed to not process this module
            if virtual_ret is not True:
                # If a module has information about why it could not be loaded, record it
                self.missing_modules[name] = virtual_err
                return False

        # If this is a proxy minion then MOST modules cannot work. Therefore, require that
        # any module that does work with salt-proxy-minion define __proxyenabled__ as a list
        # containing the names of the proxy types that the module supports.
        #
        # Render modules and state modules are OK though
        if 'proxy' in self.opts:
            if self.tag in ['grains', 'proxy']:
                if not hasattr(mod, '__proxyenabled__') or \
                        (self.opts['proxy']['proxytype'] not in mod.__proxyenabled__ and
                            '*' not in mod.__proxyenabled__):
                    err_string = 'not a proxy_minion enabled module'
                    self.missing_modules[name] = err_string
                    return False

        if getattr(mod, '__load__', False) is not False:
            log.info(
                'The functions from module {0!r} are being loaded from the '
                'provided __load__ attribute'.format(
                    module_name
                )
            )
        mod_dict = salt.utils.odict.OrderedDict()
        # In nova we only care about the audit() function, and we want to
        # store it with directory structure in the name.
        for attr in getattr(mod, '__load__', dir(mod)):
            if attr != 'audit':
                continue
            func = getattr(mod, attr)
            # Save many references for lookups
            self._dict[name] = func
            mod_dict[name] = func

        self.loaded_modules[name] = mod_dict
        return True
<commit_msg>Fix typo in argument name<commit_after># -*- encoding: utf-8 -*-
'''
Loader and primary interface for nova modules

:maintainer: basepi
:maturity: 20160218
:platform: All
:requires: SaltStack

See README for documentation

Configuration:
    - hubblestack.nova.dir
    - hubblestack.nova.saltenv
    - hubblestack.nova.autoload
    - hubblestack.nova.autosync
'''
from __future__ import absolute_import
import logging

log = logging.getLogger(__name__)

import imp
import os
import sys
import six
import inspect

import salt
import salt.utils
from salt.exceptions import CommandExecutionError
from salt.loader import LazyLoader

__nova__ = {}

def audit(modules='',
          tag='*',
          verbose=False,
          show_success=True,
          show_compliance=True):
    '''
    Primary entry point for audit calls.

    modules
        List (comma-separated or python list) of modules/directories to search
        for audit modules. Directories are dot-separated, much in the same way
        as Salt states. For individual module names, leave the .py extension
        off.  If a given path resolves to a python file, it will be treated as
        a single module. Otherwise it will be treated as a directory. All
        modules found in a recursive search of the specified directories will
        be included in the audit.

    tags
        Glob pattern string for tags to include in the audit. This way you can
        give a directory, and tell the system to only run the `CIS*`-tagged
        audit modules, for example.

    verbose
        Whether to show additional information about audits, including
        description, remediation instructions, etc. The data returned depends
        on the audit module. Defaults to False.

    show_success
        Whether to show successful audits in addition to failed audits.
        Defaults to True.

    show_compliance
        Whether to show compliance as a percentage (successful checks divided
        by total checks). Defaults to True.
    '''
    if __salt__['config.get']('hubblestack.nova.autoload', True):
        load()
    if not __nova__:
        return False, 'No nova modules have been loaded.'

    if not isinstance(modules, list):
        # Convert string
        modules = modules.split(',')

    # Convert module list to paths, with leading slashes
    modules = [os.path.join('/', os.path.join(*mod.split('.'))) for mod in modules]

    results = {'Success': [], 'Failure': []}
    already_run = set()
    # This is a pretty terrible way to iterate, performance-wise
    # However, the data sets should be relatively small, so I don't anticipate
    # issues.
    for module in modules:
        for key, func in __nova__._dict.iteritems():
            if key not in already_run and key.startswith(module):
                # Found a match, run the audit
                ret = func(tag, verbose=verbose)

                # Make sure we don't run the same audit twice
                already_run.add(key)

                # Compile the results
                results['Success'].extend(ret.get('Success', []))
                results['Failure'].extend(ret.get('Failure', []))

    if show_compliance:
        compliance = float(len(results['Success']))/(len(results['Success']) +
                                                     len(results['Failure']))
        compliance = int(compliance * 100)
        results['Compliance'] = '{0}%'.format(compliance)

    if not show_success:
        results.pop('Success')

    return results

def sync():
    '''
    Sync the nove audit modules from the saltstack fileserver.

    The modules should be stored in the salt fileserver. By default nova will
    search the base environment for a top level ``hubblestack_nova`` directory,
    unless otherwise specified via pillar or minion config
    (``hubblestack.nova.dir``)

    Modules will just be cached in the normal minion cachedir

    Returns the minion's path to the cached directory

    NOTE: This function will also clean out existing files at the cached
    location, as cp.cache_dir doesn't clean out old files

    CLI Examples:

    .. code-block:: bash

        salt '*' nova.sync
        salt '*' nova.sync saltenv=hubble
    '''
    log.debug('syncing nova modules')
    nova_dir = __salt__['config.get']('hubblestack.nova.dir', 'salt://hubblestack_nova')
    saltenv = __salt__['config.get']('hubblestack.nova.saltenv', 'base')

    # Support optional salt:// in config
    if 'salt://' in nova_dir:
        path = nova_dir
        _, _, nova_dir = nova_dir.partition('salt://')
    else:
        path = 'salt://{0}'.format(nova_dir)

    # Clean previously synced files
    __salt__['file.remove'](_hubble_dir())
    # Sync the files
    cached = __salt__['cp.cache_dir'](path, saltenv=saltenv)

    if cached and isinstance(cached, list):
        # Success! Trim the paths
        cachedir = _hubble_dir()
        ret = [relative.partition(cachedir)[2] for relative in cached]
        return ret
    else:
        if isinstance(cached, list):
            # Nothing was found
            return cached
        else:
            # Something went wrong, there's likely a stacktrace in the output
            # of cache_dir
            raise CommandExecutionError('An error occurred while syncing: {0}'
                                        .format(cached))

def load():
    '''
    Load the synced audit modules.
    '''
    if __salt__['config.get']('hubblestack.nova.autosync', True):
        sync()
    if not os.path.isdir(_hubble_dir()):
        return False, 'No synced nova modules found'

    log.debug('loading nova modules')

    global __nova__
    __nova__ = NovaLazyLoader()

    ret = {'loaded': __nova__._dict.keys(),
           'missing': __nova__.missing_modules}
    return ret

def _hubble_dir():
    '''
    Generate the local minion directory to which nova modules are synced
    '''
    nova_dir = __salt__['config.get']('hubblestack.nova.dir', 'hubblestack_nova')
    saltenv = __salt__['config.get']('hubblestack.nova.saltenv', 'base')
    cachedir = os.path.join(__opts__.get('cachedir'),
                            'files',
                            saltenv,
                            nova_dir)
    return cachedir

class NovaLazyLoader(LazyLoader):
    '''
    Leverage the SaltStack LazyLoader so we don't have to reimplement
    everything. Note that in general, we'll just call _load_all, so this
    will not actually be a lazy loader, but leveraging the existing code is
    worth it.
    '''

    def __init__(self):
        super(NovaLazyLoader, self).__init__([_hubble_dir()],
                                             opts=__opts__,
                                             tag='nova')
        self._load_all()

    def refresh_file_mapping(self):
        '''
        Override the default refresh_file_mapping to look for nova files
        recursively, rather than only in a top-level directory
        '''
        # map of suffix to description for imp
        self.suffix_map = {}
        suffix_order = []  # local list to determine precedence of extensions
        for (suffix, mode, kind) in imp.get_suffixes():
            self.suffix_map[suffix] = (suffix, mode, kind)
            suffix_order.append(suffix)

        # create mapping of filename (without suffix) to (path, suffix)
        self.file_mapping = {}

        for mod_dir in self.module_dirs:
            for dirname, dirs, files in os.walk(mod_dir):
                if '.git' in dirs:
                    dirs.remove('.git')
                for filename in files:
                    try:
                        if filename.startswith('_'):
                            # skip private modules
                            # log messages omitted for obviousness
                            continue
                        _, ext = os.path.splitext(filename)
                        fpath = os.path.join(dirname, filename)
                        f_noext, _ = os.path.splitext(fpath.partition(mod_dir)[-1])
                        # Nova only supports .py
                        if ext not in ['.py']:
                            continue
                        if f_noext in self.disabled:
                            log.trace(
                                'Skipping {0}, it is disabled by configuration'.format(
                                filename
                                )
                            )
                            continue

                        # if we don't have it, we want it
                        elif f_noext not in self.file_mapping:
                            self.file_mapping[f_noext] = (fpath, ext)
                        # if we do, we want it if we have a higher precidence ext
                        else:
                            curr_ext = self.file_mapping[f_noext][1]
                            #log.debug("****** curr_ext={0} ext={1} suffix_order={2}".format(curr_ext, ext, suffix_order))
                            if curr_ext and suffix_order.index(ext) < suffix_order.index(curr_ext):
                                self.file_mapping[f_noext] = (fpath, ext)
                    except OSError:
                        continue

    def _load_module(self, name):
        '''
        Override the module load code
        '''
        mod = None
        fpath, suffix = self.file_mapping[name]
        self.loaded_files.add(name)
        try:
            sys.path.append(os.path.dirname(fpath))
            desc = self.suffix_map[suffix]
            # if it is a directory, we don't open a file
            with salt.utils.fopen(fpath, desc[1]) as fn_:
                mod = imp.load_module(
                    '{0}.{1}.{2}.{3}'.format(
                        self.loaded_base_name,
                        self.mod_type_check(fpath),
                        self.tag,
                        name
                    ), fn_, fpath, desc)

        except IOError:
            raise
        except ImportError as error:
            log.debug(
                'Failed to import {0} {1}:\n'.format(
                    self.tag, name
                ),
                exc_info=True
            )
            self.missing_modules[name] = str(error)
            return False
        except Exception as error:
            log.error(
                'Failed to import {0} {1}, this is due most likely to a '
                'syntax error:\n'.format(
                    self.tag, name
                ),
                exc_info=True
            )
            self.missing_modules[name] = str(error)
            return False
        except SystemExit as error:
            log.error(
                'Failed to import {0} {1} as the module called exit()\n'.format(
                    self.tag, name
                ),
                exc_info=True
            )
            self.missing_modules[name] = str(error)
            return False
        finally:
            sys.path.pop()

        mod.__grains__ = __grains__
        mod.__pillar__ = __pillar__
        mod.__opts__ = __opts__
        mod.__salt__ = __salt__

        # pack whatever other globals we were asked to
        for p_name, p_value in six.iteritems(self.pack):
            setattr(mod, p_name, p_value)

        module_name = name

        # Call a module's initialization method if it exists
        module_init = getattr(mod, '__init__', None)
        if inspect.isfunction(module_init):
            try:
                module_init(self.opts)
            except TypeError as e:
                log.error(e)
            except Exception:
                err_string = '__init__ failed'
                log.debug(
                    'Error loading {0}.{1}: {2}'.format(
                        self.tag,
                        module_name,
                        err_string),
                    exc_info=True)
                self.missing_modules[name] = err_string
                return False

        # if virtual modules are enabled, we need to look for the
        # __virtual__() function inside that module and run it.
        if self.virtual_enable:
            (virtual_ret, module_name, virtual_err) = self.process_virtual(
                mod,
                module_name,
            )
            if virtual_err is not None:
                log.debug('Error loading {0}.{1}: {2}'.format(self.tag,
                                                              module_name,
                                                              virtual_err,
                                                              ))

            # if process_virtual returned a non-True value then we are
            # supposed to not process this module
            if virtual_ret is not True:
                # If a module has information about why it could not be loaded, record it
                self.missing_modules[name] = virtual_err
                return False

        # If this is a proxy minion then MOST modules cannot work. Therefore, require that
        # any module that does work with salt-proxy-minion define __proxyenabled__ as a list
        # containing the names of the proxy types that the module supports.
        #
        # Render modules and state modules are OK though
        if 'proxy' in self.opts:
            if self.tag in ['grains', 'proxy']:
                if not hasattr(mod, '__proxyenabled__') or \
                        (self.opts['proxy']['proxytype'] not in mod.__proxyenabled__ and
                            '*' not in mod.__proxyenabled__):
                    err_string = 'not a proxy_minion enabled module'
                    self.missing_modules[name] = err_string
                    return False

        if getattr(mod, '__load__', False) is not False:
            log.info(
                'The functions from module {0!r} are being loaded from the '
                'provided __load__ attribute'.format(
                    module_name
                )
            )
        mod_dict = salt.utils.odict.OrderedDict()
        # In nova we only care about the audit() function, and we want to
        # store it with directory structure in the name.
        for attr in getattr(mod, '__load__', dir(mod)):
            if attr != 'audit':
                continue
            func = getattr(mod, attr)
            # Save many references for lookups
            self._dict[name] = func
            mod_dict[name] = func

        self.loaded_modules[name] = mod_dict
        return True
<|endoftext|>


<commit_before>from mw.api import Session

from revscores import APIExtractor
from revscores.feature_extractors import (added_badwords_ratio,
                                          added_misspellings_ratio,
                                          badwords_added, bytes_changed,
                                          chars_added, day_of_week_in_utc,
                                          hour_of_day_in_utc, is_custom_comment,
                                          is_mainspace, is_previous_user_same,
                                          is_section_comment,
                                          longest_repeated_char_added,
                                          longest_token_added,
                                          misspellings_added,
                                          numeric_chars_added,
                                          page_age_in_seconds, prev_badwords,
                                          prev_misspellings, prev_words,
                                          proportion_of_badwords_added,
                                          proportion_of_markup_added,
                                          proportion_of_misspellings_added,
                                          proportion_of_numeric_added,
                                          proportion_of_prev_badwords,
                                          proportion_of_prev_misspellings,
                                          proportion_of_symbolic_added,
                                          proportion_of_uppercase_added,
                                          seconds_since_last_page_edit,
                                          seconds_since_last_user_edit,
                                          segments_added, segments_removed,
                                          symbolic_chars_added,
                                          uppercase_chars_added,
                                          user_age_in_seconds, user_is_anon,
                                          user_is_bot, words_added,
                                          words_removed)
from revscores.language import English

api_extractor = APIExtractor(
    Session("https://pt.wikipedia.org/w/api.php"),
    language=English()
)

extractors = [added_badwords_ratio,
              added_misspellings_ratio,
              badwords_added, bytes_changed,
              chars_added, day_of_week_in_utc,
              hour_of_day_in_utc, is_custom_comment,
              is_mainspace, is_previous_user_same,
              is_section_comment,
              longest_repeated_char_added,
              longest_token_added,
              misspellings_added,
              numeric_chars_added,
              page_age_in_seconds, prev_badwords,
              prev_misspellings, prev_words,
              proportion_of_badwords_added,
              proportion_of_markup_added,
              proportion_of_misspellings_added,
              proportion_of_numeric_added,
              proportion_of_prev_misspellings,
              proportion_of_prev_badwords,
              proportion_of_symbolic_added,
              proportion_of_uppercase_added,
              seconds_since_last_page_edit,
              seconds_since_last_user_edit,
              segments_added, segments_removed,
              symbolic_chars_added,
              uppercase_chars_added,
              user_age_in_seconds, user_is_anon,
              user_is_bot, words_added,
              words_removed]

print("Extracting features for "  +
      "http://pt.wikipedia.org/wiki/?oldid=40837209&diff=prev")
features = api_extractor.extract(40837209, extractors)

for extractor, feature in zip(extractors, features):
    print("{0}: {1}".format(extractor, feature))
<commit_msg>Fixes langauge in demo extractor.<commit_after>from mw.api import Session

from revscores import APIExtractor
from revscores.feature_extractors import (added_badwords_ratio,
                                          added_misspellings_ratio,
                                          badwords_added, bytes_changed,
                                          chars_added, day_of_week_in_utc,
                                          hour_of_day_in_utc, is_custom_comment,
                                          is_mainspace, is_previous_user_same,
                                          is_section_comment,
                                          longest_repeated_char_added,
                                          longest_token_added,
                                          misspellings_added,
                                          numeric_chars_added,
                                          page_age_in_seconds, prev_badwords,
                                          prev_misspellings, prev_words,
                                          proportion_of_badwords_added,
                                          proportion_of_markup_added,
                                          proportion_of_misspellings_added,
                                          proportion_of_numeric_added,
                                          proportion_of_prev_badwords,
                                          proportion_of_prev_misspellings,
                                          proportion_of_symbolic_added,
                                          proportion_of_uppercase_added,
                                          seconds_since_last_page_edit,
                                          seconds_since_last_user_edit,
                                          segments_added, segments_removed,
                                          symbolic_chars_added,
                                          uppercase_chars_added,
                                          user_age_in_seconds, user_is_anon,
                                          user_is_bot, words_added,
                                          words_removed)
from revscores.language import Portuguese

api_extractor = APIExtractor(
    Session("https://pt.wikipedia.org/w/api.php"),
    language=Portuguese()
)

extractors = [added_badwords_ratio,
              added_misspellings_ratio,
              badwords_added, bytes_changed,
              chars_added, day_of_week_in_utc,
              hour_of_day_in_utc, is_custom_comment,
              is_mainspace, is_previous_user_same,
              is_section_comment,
              longest_repeated_char_added,
              longest_token_added,
              misspellings_added,
              numeric_chars_added,
              page_age_in_seconds, prev_badwords,
              prev_misspellings, prev_words,
              proportion_of_badwords_added,
              proportion_of_markup_added,
              proportion_of_misspellings_added,
              proportion_of_numeric_added,
              proportion_of_prev_misspellings,
              proportion_of_prev_badwords,
              proportion_of_symbolic_added,
              proportion_of_uppercase_added,
              seconds_since_last_page_edit,
              seconds_since_last_user_edit,
              segments_added, segments_removed,
              symbolic_chars_added,
              uppercase_chars_added,
              user_age_in_seconds, user_is_anon,
              user_is_bot, words_added,
              words_removed]

print("Extracting features for "  +
      "http://pt.wikipedia.org/wiki/?oldid=40837209&diff=prev")
features = api_extractor.extract(40837209, extractors)

for extractor, feature in zip(extractors, features):
    print("{0}: {1}".format(extractor, feature))
<|endoftext|>


<commit_before>import os.path

def absAppPath(argv):
    return os.path.abspath(argv)<commit_msg>PfadBerechner eingebaut, weil ich immer vergesse wie das richtig geht<commit_after>import os.path

def absPathOf(basePath, *relPaths):
    pathDict = [os.path.dirname(basePath)]
    for path in relPaths:
        pathDict.append(path)
    return os.path.abspath(os.path.sep.join(pathDict))<|endoftext|>


<commit_before>import time

from pllm import vision, config, util

class screenlock(object):
    def __init__(self, func):
        self.func = func

    def __call__(self, dom, template):
        with dom.screen_lock:
            res = self.func(dom, template)
        return res

class cachefind(object):
    def __init__(self, func):
        self.func = func
        self.last_screen_id = -1
        self.cache = {}

    def __call__(self, dom, template):
        if self.last_screen_id != dom.screen_id:
            self.cache = {}
            self.last_screen_id = dom.screen_id

        if template in self.cache:
            res = self.cache[template]
        else:
            res = self.func(dom, template)
            self.cache[template] = res

        return res

@screenlock
@cachefind
def find(dom, template):
    res, x, y = vision.template_match(dom.screen, util.load_img(template))
    if res > config.get('treshold'):
        print('+{0}@{1} = {2}'.format(template, dom.screen_id, res))
        ret = True
    else:
        print('-{0}@{1} = {2}'.format(template, dom.screen_id, res))
        ret = False

    return ret

@screenlock
@cachefind
def findxy(dom, template):
    res, x, y = vision.template_match(dom.screen, util.load_img(template))
    if res > config.get('treshold'):
        print('+{0}@{1} = {2}'.format(template, dom.screen_id, res))
        ret = True
    else:
        print('-{0}@{1} = {2}'.format(template, dom.screen_id, res))
        ret = False

    return (ret, x, y)

def expect(dom, items):
    for stage_name, template, callback in items:
        print('Testing stage {0}'.format(stage_name))
        if find(dom, template):
            print('Stage "{0}" found'.format(stage_name))
            callback(dom)

def expect_text(dom, items):
    for stage_name, text, callback in items:
        print('Testing text presence "{0}" for stage "{1}"'
              .format(text, stage_name))

        if text.lower() in dom.text.lower():
            print('Stage "{0}" found'.format(stage_name))
            callback(dom)

def click(dom, template):
    print('Looking for click target')
    ret, x, y = findxy(dom, template)

    if ret:
        dom.clickxy(x, y)
    else:
        print('Click target not found')

    time.sleep(0.5)

def click_text(dom, text):
    print('Looking for click target with text "{0}"'.format(text))

    for segname, data in dom.segments.items():
        rect, ocrd = data
        x, y, w, h = rect

        if text.lower() in ocrd.lower():
            print('Found target at {0}x{1}'.format(x, y))
            dom.clickxy(x + w / 2, y + h / 2)
            return

    print('Click target not found')

def wait(dom, template, timeout_seconds=30):
    start = time.time()
    print('Waiting for target for {0} sec'.format(timeout_seconds))
    while True:
        if find(dom, template):
            print('Wait target found after {:.2} sec'.format(
                time.time() - start))
            return True

        time.sleep(0.1)

        if time.time() > start + timeout_seconds:
            print('!! Timeout on wait')
            return False

def wait_text(dom, text, timeout_seconds=30):
    start = time.time()
    print('Waiting for "{0}" text for {1} sec'.format(text, timeout_seconds))

    while True:
        if text.lower() in dom.text.lower():
            print('Wait target found after {:.2} sec'.format(
                time.time() - start))
            return True

        # check segments
        for segname, data in dom.segments.items():
            rect, ocrd = data
            x, y, w, h = rect

            if text.lower() in ocrd.lower():
                print('Wait target found at segment {0}x{1} after{:.2} sec'
                      .format(x, y, time.time() - start))
                return True

        time.sleep(0.1)

        if time.time() > start + timeout_seconds:
            print('!! Timeout on wait')
            return False

def wait_click(dom, template):
    wait(dom, template)
    click(dom, template)

def wait_click_text(dom, text):
    wait_text(dom, text)
    click_text(dom, text)

def grub(dom):
    dom.key_press('ret')
    # skip media check
    time.sleep(7)
    for i in range(5):
        dom.key_press('esc')
        time.sleep(1)

def anaconda(dom):
    # first click glitch, but why?
    dom.mouse_move(1, 1)
    #dom.click()

    click_text(dom, 'continue')
    if not wait_text(dom, 'installation summary', 5):
        print('First click glitch present')
        click_text(dom, 'continue')

    wait_text(dom, 'installation summary')

    while True:
        wait_click(dom, 'anaconda_storage_incomplete_btn')
        if wait_text(dom, 'local standard disks'):
            break

    wait_click_text(dom, 'done')
    wait_text(dom, 'installation options')
    dom.key_press('alt-m')
    wait_click_text(dom, 'continue')

    def create_partition(mount, size):
        wait_text(dom, 'manual partitioning')

        while not wait_text(dom, 'add a new mount point', 2):
            wait_click(dom, 'anaconda_partitioning_plus_btn')

        dom.write(mount)
        dom.key_press('right')
        dom.key_press('tab')
        dom.key_press('tab')
        dom.write(size)

        while not wait_text(dom, 'manual partitioning', 2):
            wait_click_text(dom, 'add mount point')

    create_partition('/', '5G')
    create_partition('/boot', '100M')
    create_partition('/home', '1G')

    wait_click_text(dom, 'done')
    wait_click_text(dom, 'accept changes')

    wait_click(dom, 'anaconda_software_btn')
    #wait_click(dom, 'anaconda_xfce_choice')
    wait_click_text(dom, 'done')
    wait_click_text(dom, 'begin installation')

    wait_click(dom, 'anaconda_rootpw_btn')
    for _ in [1, 2]:
        for i in range(6):
            dom.key_press(str(i))

        dom.key_down('tab')

    wait_click_text(dom, 'done')
    wait_click_text(dom, 'done')

    '''
    wait_click(dom, 'anaconda_begin_btn')
    click(dom, 'anaconda_done_btn')
    wait_click(dom, 'anaconda_done_btn')
    wait(dom, 'anaconda_reboot_btn', 3600)
    click(dom, 'anaconda_reboot_btn')
    '''

    print('Waiting for domain to shutdown')
    while dom.is_running():
        print('.')
        time.sleep(1)

    print dom.is_running()
    time.sleep(1)
    print('Starting again')
    print dom.is_running()
    dom.start()

def write(dom, text):
    for letter in text:
        dom.key_down(letter)

def firstboot(dom):
    wait_click(dom, 'firstboot_forward_btn')
    wait_click(dom, 'firstboot_forward_btn')
    wait_click(dom, 'firstboot_forward_btn')
    wait(dom, 'firstboot_create_user_label')
    write(dom, 'pllm framework')
    dom.key_down('tab')
    write(dom, 'pllm')
    dom.key_down('tab')
    dom.key_down('tab')
    write(dom, 'a')
    dom.key_down('tab')
    write(dom, 'a')
    wait_click(dom, 'firstboot_forward_btn')
    wait_click(dom, 'firstboot_finish_btn')

def f20(dom):
    expect(dom, [
        ('grub', 'grub_autoboot_label', grub),
    ])

    expect_text(dom, [
        ('anaconda', 'welcome to fedora', anaconda)
    ])

try:
    while True:
        print('f20')
        f20(dom)
        time.sleep(1)
except KeyboardInterrupt:
    pass
<commit_msg>update pseudo<commit_after>import time

from pllm import vision, config, util

class screenlock(object):
    def __init__(self, func):
        self.func = func

    def __call__(self, dom, template):
        with dom.screen_lock:
            res = self.func(dom, template)
        return res

class cachefind(object):
    def __init__(self, func):
        self.func = func
        self.last_screen_id = -1
        self.cache = {}

    def __call__(self, dom, template):
        if self.last_screen_id != dom.screen_id:
            self.cache = {}
            self.last_screen_id = dom.screen_id

        if template in self.cache:
            res = self.cache[template]
        else:
            res = self.func(dom, template)
            self.cache[template] = res

        return res

@screenlock
@cachefind
def find(dom, template):
    res, x, y = vision.template_match(dom.screen, util.load_img(template))
    if res > config.get('treshold'):
        print('+{0}@{1} = {2}'.format(template, dom.screen_id, res))
        ret = True
    else:
        print('-{0}@{1} = {2}'.format(template, dom.screen_id, res))
        ret = False

    return ret

@screenlock
@cachefind
def findxy(dom, template):
    res, x, y = vision.template_match(dom.screen, util.load_img(template))
    if res > config.get('treshold'):
        print('+{0}@{1} = {2}'.format(template, dom.screen_id, res))
        ret = True
    else:
        print('-{0}@{1} = {2}'.format(template, dom.screen_id, res))
        ret = False

    return (ret, x, y)

def expect(dom, items):
    for stage_name, template, callback in items:
        print('Testing stage {0}'.format(stage_name))
        if find(dom, template):
            print('Stage "{0}" found'.format(stage_name))
            callback(dom)

def expect_text(dom, items):
    for stage_name, text, callback in items:
        print('Testing text presence "{0}" for stage "{1}"'
              .format(text, stage_name))

        if text.lower() in dom.text.lower():
            print('Stage "{0}" found'.format(stage_name))
            callback(dom)

def click(dom, template):
    print('Looking for click target')
    ret, x, y = findxy(dom, template)

    if ret:
        dom.clickxy(x, y)
    else:
        print('Click target not found')

    time.sleep(0.5)

def click_text(dom, text):
    print('Looking for click target with text "{0}"'.format(text))

    for segname, data in dom.segments.items():
        rect, ocrd = data
        x, y, w, h = rect

        if text.lower() in ocrd.lower():
            print('Found target at {0}x{1}'.format(x, y))
            dom.clickxy(x + w / 2, y + h / 2)
            return

    print('Click target not found')

def wait(dom, template, timeout_seconds=30):
    start = time.time()
    print('Waiting for target for {0} sec'.format(timeout_seconds))
    while True:
        if find(dom, template):
            print('Wait target found after {:.2} sec'.format(
                time.time() - start))
            return True

        time.sleep(0.1)

        if time.time() > start + timeout_seconds:
            print('!! Timeout on wait')
            return False

def wait_text(dom, text, timeout_seconds=30):
    start = time.time()
    print('Waiting for "{0}" text for {1} sec'.format(text, timeout_seconds))

    while True:
        if text.lower() in dom.text.lower():
            print('Wait target found after {:.2} sec'.format(
                time.time() - start))
            return True

        # check segments
        for segname, data in dom.segments.items():
            rect, ocrd = data
            x, y, w, h = rect

            if text.lower() in ocrd.lower():
                print('Wait target found at segment {0}x{1} after{:.2} sec'
                      .format(x, y, time.time() - start))
                return True

        time.sleep(0.1)

        if time.time() > start + timeout_seconds:
            print('!! Timeout on wait')
            return False

def wait_click(dom, template):
    wait(dom, template)
    click(dom, template)

def wait_click_text(dom, text):
    wait_text(dom, text)
    click_text(dom, text)

def grub(dom):
    dom.key_press('tab')
    # erase part of cmdline
    for _ in "rd.live.check quiet":
        dom.key_press('backspace')
        time.sleep(0.2)

    # disable gelocation so we don't start with random language
    dom.write(' geoloc=0')
    time.sleep(2)  # screenshot
    dom.key_press('ret')

def anaconda(dom):
    # first click glitch, but why?
    dom.mouse_move(1, 1)
    #dom.click()

    click_text(dom, 'continue')
    if not wait_text(dom, 'installation summary', 5):
        print('First click glitch present')
        click_text(dom, 'continue')

    wait_text(dom, 'installation summary')

    while True:
        wait_click(dom, 'anaconda_storage_incomplete_btn')
        if wait_text(dom, 'local standard disks'):
            break

    wait_click_text(dom, 'done')
    wait_text(dom, 'installation options')
    dom.key_press('alt-m')
    wait_click_text(dom, 'continue')

    def create_partition(mount, size):
        wait_text(dom, 'manual partitioning')

        while not wait_text(dom, 'add a new mount point', 2):
            wait_click(dom, 'anaconda_partitioning_plus_btn')

        dom.write(mount)
        dom.key_press('right')
        dom.key_press('tab')
        dom.key_press('tab')
        dom.write(size)

        while not wait_text(dom, 'manual partitioning', 2):
            wait_click_text(dom, 'add mount point')

    create_partition('/', '5G')
    create_partition('/boot', '100M')
    create_partition('/home', '1G')

    wait_click_text(dom, 'done')
    wait_click_text(dom, 'accept changes')

    wait_click(dom, 'anaconda_software_btn')
    #wait_click(dom, 'anaconda_xfce_choice')
    wait_click_text(dom, 'done')
    wait_click_text(dom, 'begin installation')

    wait_click(dom, 'anaconda_rootpw_btn')
    wait_text(dom, 'root password')
    for _ in [1, 2]:
        for i in range(6):
            dom.key_press(str(i))

        dom.key_down('tab')

    # password too simple, need to click two times
    wait_click_text(dom, 'done')
    wait_click_text(dom, 'done')

    time.sleep(2)
    print(dom.text)

    wait_click(dom, 'anaconda_user_creation_btn')
    wait_click_text(dom, 'done')

    time.sleep(2)
    print(dom.text)

    '''
    wait_click(dom, 'anaconda_begin_btn')
    click(dom, 'anaconda_done_btn')
    wait_click(dom, 'anaconda_done_btn')
    wait(dom, 'anaconda_reboot_btn', 3600)
    click(dom, 'anaconda_reboot_btn')
    '''

    print('Waiting for installation to finish')
    while True:
        wait_click_text(dom, 'reboot')

    print('Waiting for domain to shutdown')
    while dom.is_running():
        print('.')
        time.sleep(1)

    print dom.is_running()
    time.sleep(1)
    print('Starting again')
    print dom.is_running()
    dom.start()

def write(dom, text):
    for letter in text:
        dom.key_down(letter)

def firstboot(dom):
    wait_click(dom, 'firstboot_forward_btn')
    wait_click(dom, 'firstboot_forward_btn')
    wait_click(dom, 'firstboot_forward_btn')
    wait(dom, 'firstboot_create_user_label')
    write(dom, 'pllm framework')
    dom.key_down('tab')
    write(dom, 'pllm')
    dom.key_down('tab')
    dom.key_down('tab')
    write(dom, 'a')
    dom.key_down('tab')
    write(dom, 'a')
    wait_click(dom, 'firstboot_forward_btn')
    wait_click(dom, 'firstboot_finish_btn')

def f20(dom):
    expect(dom, [
        ('grub', 'grub_autoboot_label', grub),
    ])

    expect_text(dom, [
        ('anaconda', 'welcome to fedora', anaconda)
    ])

try:
    while True:
        print('f20')
        f20(dom)
        time.sleep(1)
except KeyboardInterrupt:
    pass
<|endoftext|>


<commit_before>#!/usr/bin/python3
# -*- coding: iso-8859-15 -*-
# Paxton main module

# Imports #################################################
import random
import sys

# Data structure ##########################################
class Memory:
    def __init__(self):
        self.characters = dict()
        self.charactersTotal = 0

        self.sizes = dict()
        self.sizesTotal = 0
        self.maxSize = 0

        # list of dict(), one for each position
        self.positions = []
        self.positionsTotal = []

        # a dict of [total, dict()]
        self.twochains = dict()

    # Parsing =============================================
    def parse(self, data, separator="\n"):
        self.parseCharacters(data, separator)
        self.parseSizes(data, separator)
        self.parsePositions(data, self.maxSize, separator)
        self.parseTwochains(data, separator)

    def parseCharacters(self, data, separator="\n"):
        log("Beginning parsing characters.")

        self.charactersTotal = len(data)
        for i in range(len(data)):
            if data[i] == separator:
                self.charactersTotal -= 1
            else:
                if data[i] in self.characters.keys():
                    self.characters[data[i]] += 1
                else:
                    self.characters[data[i]] = 1

        log("Ending parsing characters.")

    def parseSizes(self, data, separator="\n"):
        log("Beginning parsing sizes.")

        datas = data.split(separator)
        self.maxSize = max(map(len, datas))
        self.sizesTotal = len(datas)
        for i in range(len(datas)):
            if len(datas[i]) in self.sizes.keys():
                self.sizes[len(datas[i])] += 1
            else:
                self.sizes[len(datas[i])] = 1

        log("Ending parsing sizes.")

    def parsePositions(self, data, maxSize, separator="\n"):
        log("Beginning parsing positions.")

        datas = data.split(separator)
        for i in range(maxSize):
            self.positions.append(dict())
            self.positionsTotal.append(0)

        for i in range(len(datas)):
            for j in range(len(datas[i])):
                if datas[i][j] in self.positions[j].keys():
                    self.positions[j][datas[i][j]] += 1
                else:
                    self.positions[j][datas[i][j]] = 1
                self.positionsTotal[j] += 1

        log("Ending parsing positions.")

    def parseTwochains(self, data, separator="\n"):
        log("Beginning parsing 2-chains.")

        # The very first character is assumed to have followed a separator.
        self.twochains[separator] = [1, dict()]
        self.twochains[separator][1][data[0]] = 1

        for i in range(1, len(data)):
            if not (data[i - 1] in self.twochains.keys()):
                self.twochains[data[i - 1]] = [0, dict()]

            if data[i] in self.twochains[data[i - 1]][1].keys():
                self.twochains[data[i - 1]][1][data[i]] += 1
            else:
                self.twochains[data[i - 1]][1][data[i]] = 1
            self.twochains[data[i - 1]][0] += 1

        # The very last character is assumed to be followed by a separator
        if separator in self.twochains[data[len(data) - 1]][1].keys():
            self.twochains[data[len(data) - 1]][1][separator] += 1
        else:
            self.twochains[data[len(data) - 1]][1][separator] = 1
        self.twochains[data[len(data) - 1]][0] += 1

        log("Ending parsing 2-chains.")

    # Production ==========================================
    def produce(self, separator="\n"):
        random.seed(1)

        # Choose size
        fu = random.randint(0, self.sizesTotal - 1)
        size = dictChoice(fu, self.sizes)
        assert(size >= 0)
        if size == 0:
            return ""

        # Choose the first element
        fu = random.randint(0, self.charactersTotal - 1)
        value = dictChoice(fu, self.characters)

        # Choose what remains
        for i in range(size - 1):
            fu = random.randint(0, self.charactersTotal - 1)
            value += dictChoice(fu, self.characters)

        return value

    # Save/restore ========================================
    def save(self, filename):
        pass

    def restore(self, filename):
        pass

# Utility functions #######################################
def log(data):
    """
        Log the data given as input.
        Useful for a future change of the logger used.
    """
    print("*** " + data + " ***")

def dictChoice(invalue, dictionary):
    """
        Return the key of a dictionary designated by a given value, where the value is a ponderation to be consumed.
    """
    value = invalue
    for key in dictionary.keys():
        value -= dictionary[key]
        if value < 0:
            return key

    raise "Error: the value asked is out of the dictionary."

def start():
    usage = "Usage: {0} <file>".format(sys.argv[0])

    if len(sys.argv) != 2:
        print(usage)
    else:
        mem = Memory()
        f = open(sys.argv[1], 'r')
        data = f.read()
        mem.parse(data)
        print(mem.produce())

if __name__ == "__main__":
    start()

<commit_msg>Update the first element generation.<commit_after>#!/usr/bin/python3
# -*- coding: iso-8859-15 -*-
# Paxton main module

# Imports #################################################
import random
import sys

# Data structure ##########################################
class Memory:
    def __init__(self):
        self.characters = dict()
        self.charactersTotal = 0

        self.sizes = dict()
        self.sizesTotal = 0
        self.maxSize = 0

        # list of dict(), one for each position
        self.positions = []
        self.positionsTotal = []

        # a dict of [total, dict()]
        self.twochains = dict()

    # Parsing =============================================
    def parse(self, data, separator="\n"):
        self.parseCharacters(data, separator)
        self.parseSizes(data, separator)
        self.parsePositions(data, self.maxSize, separator)
        self.parseTwochains(data, separator)

    def parseCharacters(self, data, separator="\n"):
        log("Beginning parsing characters.")

        self.charactersTotal = len(data)
        for i in range(len(data)):
            if data[i] == separator:
                self.charactersTotal -= 1
            else:
                if data[i] in self.characters.keys():
                    self.characters[data[i]] += 1
                else:
                    self.characters[data[i]] = 1

        log("Ending parsing characters.")

    def parseSizes(self, data, separator="\n"):
        log("Beginning parsing sizes.")

        datas = data.split(separator)
        self.maxSize = max(map(len, datas))
        self.sizesTotal = len(datas)
        for i in range(len(datas)):
            if len(datas[i]) in self.sizes.keys():
                self.sizes[len(datas[i])] += 1
            else:
                self.sizes[len(datas[i])] = 1

        log("Ending parsing sizes.")

    def parsePositions(self, data, maxSize, separator="\n"):
        log("Beginning parsing positions.")

        datas = data.split(separator)
        for i in range(maxSize):
            self.positions.append(dict())
            self.positionsTotal.append(0)

        for i in range(len(datas)):
            for j in range(len(datas[i])):
                if datas[i][j] in self.positions[j].keys():
                    self.positions[j][datas[i][j]] += 1
                else:
                    self.positions[j][datas[i][j]] = 1
                self.positionsTotal[j] += 1

        log("Ending parsing positions.")

    def parseTwochains(self, data, separator="\n"):
        log("Beginning parsing 2-chains.")

        # The very first character is assumed to have followed a separator.
        self.twochains[separator] = [1, dict()]
        self.twochains[separator][1][data[0]] = 1

        for i in range(1, len(data)):
            if not (data[i - 1] in self.twochains.keys()):
                self.twochains[data[i - 1]] = [0, dict()]

            if data[i] in self.twochains[data[i - 1]][1].keys():
                self.twochains[data[i - 1]][1][data[i]] += 1
            else:
                self.twochains[data[i - 1]][1][data[i]] = 1
            self.twochains[data[i - 1]][0] += 1

        # The very last character is assumed to be followed by a separator
        if separator in self.twochains[data[len(data) - 1]][1].keys():
            self.twochains[data[len(data) - 1]][1][separator] += 1
        else:
            self.twochains[data[len(data) - 1]][1][separator] = 1
        self.twochains[data[len(data) - 1]][0] += 1

        log("Ending parsing 2-chains.")

    # Production ==========================================
    def produce(self, separator="\n"):
        random.seed(1)

        # Choose size
        fu = random.randint(0, self.sizesTotal - 1)
        size = dictChoice(fu, self.sizes)
        assert(size >= 0)
        if size == 0:
            return ""

        # Choose the first element
        fu = random.randint(0, self.positionsTotal[0] - 1)
        value = dictChoice(fu, self.positions[0])

        # Choose what remains
        for i in range(size - 1):
            fu = random.randint(0, self.charactersTotal - 1)
            value += dictChoice(fu, self.characters)

        return value

    # Save/restore ========================================
    def save(self, filename):
        pass

    def restore(self, filename):
        pass

# Utility functions #######################################
def log(data):
    """
        Log the data given as input.
        Useful for a future change of the logger used.
    """
    print("*** " + data + " ***")

def dictChoice(invalue, dictionary):
    """
        Return the key of a dictionary designated by a given value, where the value is a ponderation to be consumed.
    """
    value = invalue
    for key in dictionary.keys():
        value -= dictionary[key]
        if value < 0:
            return key

    raise "Error: the value asked is out of the dictionary."

def start():
    usage = "Usage: {0} <file>".format(sys.argv[0])

    if len(sys.argv) != 2:
        print(usage)
    else:
        mem = Memory()
        f = open(sys.argv[1], 'r')
        data = f.read()
        mem.parse(data)
        print(mem.produce())

if __name__ == "__main__":
    start()

<|endoftext|>


<commit_before>"""Models for the ``people`` app."""
from django.db import models
from django.utils.translation import ugettext_lazy as _

from cms.models.pluginmodel import CMSPlugin
from django_libs.models_mixins import SimpleTranslationMixin
from filer.fields.file import FilerFileField
from localized_names.templatetags.localized_names_tags import get_name

from . import settings

# Hack to have these strings translated
mr = _('Mr')
mrs = _('Ms')

GENDER_CHOICES = [
    ('male', _('male')),
    ('female', _('female')),
]

TITLE_CHOICES = [
    ('Dr', _('Dr')),
    ('Prof', _('Prof')),
    ('Prof Dr', _('Prof Dr')),
]

class LinkType(SimpleTranslationMixin, models.Model):
    """
    A link type could be ``Facebook`` or ``Twitter`` or ``Website``.

    This is masterdata that should be created by the admins when the site is
    deployed for the first time.

    For translateable fields see ``LinkTypeTranslation`` model.

    :ordering: Enter numbers here if you want links to be displayed in a
      special order.

    """
    slug = models.SlugField(
        max_length=256,
        verbose_name=_('Slug'),
        help_text=_(
            'Use this field to define a simple identifier that can be used'
            ' to style the different link types (i.e. assign social media'
            ' icons to them)'),
        blank=True,
    )

    ordering = models.PositiveIntegerField(
        verbose_name=_('Ordering'),
        null=True, blank=True,
    )

    class Meta:
        ordering = ['ordering', ]

    def __unicode__(self):
        return self.get_translation().name

class LinkTypeTranslation(models.Model):
    """
    Translateable fields of the ``LinkType`` model.

    """
    name = models.CharField(
        max_length=256,
        verbose_name=_('Name'),
    )

    # needed by simple-translation
    link_type = models.ForeignKey(LinkType)
    language = models.CharField(max_length=16)

class Nationality(SimpleTranslationMixin, models.Model):
    """
    The nationality of a Person.

    For translateable fields see the ``NationalityTranslation`` model.

    """
    def __unicode__(self):
        return self.get_translation().name

class NationalityTranslation(models.Model):
    """
    The translateable fields of the ``Nationality`` model.

    :name: E.g. 'Chinese' or 'Deutsch'

    """
    name = models.CharField(
        max_length=128,
        verbose_name=_('Name'),
    )

    # needed by simple-translation
    nationality = models.ForeignKey(Nationality)
    language = models.CharField(max_length=16)

class Role(SimpleTranslationMixin, models.Model):
    """
    People can have certain roles in an organisation.

    For translateable fields see ``RoleTranslation`` model.

    :name: The name of the role.

    """
    def __unicode__(self):
        return self.get_translation().name

class RoleTranslation(models.Model):
    """
    Translateable fields of the ``Role`` model.

    :name: The name of the role.

    """
    name = models.CharField(
        max_length=256,
        verbose_name=_('Role'),
    )

    role_description = models.TextField(
        max_length=4000,
        verbose_name=_('Role description'),
        blank=True,
    )

    # needed by simple-translation
    role = models.ForeignKey(Role)
    language = models.CharField(max_length=16)

class Person(SimpleTranslationMixin, models.Model):
    """
    A model that holds information about a person.

    For translateable fields see ``PersonTitle`` model.

    :roman_first_name: The first name in roman letters.
    :roman_last_name: The last name in roman letters.
    :non_roman_first_name: The first name in non roman letters.
    :non_roman_last_name: The last name in non roman letters.
    :gender: The gender of the person.
    :title: The title of the person.
    :chosen_name: For asian people, this is the chosen western name.
    :role: Role of the person within the organisation.
    :picture: A picture of the person.
    :phone: Phonenumber of the person.
    :email: Email address of the person.
    :ordering: Enter numbers if you want to order the list of persons on your
      site in a special way.
    :nationality: The nationality of a person.

    """
    roman_first_name = models.CharField(
        max_length=256,
        verbose_name=_('Roman first name'),
        blank=True
    )

    roman_last_name = models.CharField(
        max_length=256,
        verbose_name=_('Roman last name'),
        blank=True,
    )

    non_roman_first_name = models.CharField(
        max_length=256,
        verbose_name=_('Non roman first name'),
        blank=True
    )

    non_roman_last_name = models.CharField(
        max_length=256,
        verbose_name=_('Non roman last name'),
        blank=True,
    )

    gender = models.CharField(
        max_length=16,
        choices=GENDER_CHOICES,
        verbose_name=_('Gender'),
        blank=True,
    )

    title = models.CharField(
        max_length=16,
        choices=TITLE_CHOICES,
        verbose_name=_('Title'),
        blank=True,
    )

    chosen_name = models.CharField(
        max_length=256,
        verbose_name=_('Chosen name'),
        blank=True,
    )

    role = models.ForeignKey(
        Role,
        verbose_name=_('Role'),
        null=True, blank=True,
    )

    picture = FilerFileField(
        verbose_name=_('Picture'),
        null=True, blank=True,
    )

    phone = models.CharField(
        max_length=32,
        verbose_name=_('Phone'),
        blank=True,
    )

    email = models.EmailField(
        verbose_name=_('Email'),
        blank=True,
    )

    ordering = models.PositiveIntegerField(
        verbose_name=_('Ordering'),
        null=True, blank=True,
    )

    nationality = models.ForeignKey(
        Nationality,
        verbose_name=_('Nationality'),
        blank=True, null=True,
    )

    class Meta:
        ordering = ['ordering', ]

    def __unicode__(self):
        trans = self.get_translation()
        return get_name(trans, 'SHORT_NAME_FORMAT')

class PersonTranslation(models.Model):
    """
    Translateable fields of the ``Person`` model.

    :short_bio: A short description of the person.
    :bio: A longer description of the person, could appear after a
      ``read more`` link behind the ``short_bio``.

    """
    short_bio = models.TextField(
        max_length=512,
        verbose_name=_('Short bio'),
        blank=True,
    )

    bio = models.TextField(
        max_length=4000,
        verbose_name=_('Biography'),
        blank=True,
    )

    # needed by simple-translation
    person = models.ForeignKey(Person)
    language = models.CharField(max_length=16)

    def get_gender(self):
        """Returns either 'Mr.' or 'Ms.' depending on the gender."""
        if self.person.gender == 'male':
            return 'Mr'
        elif self.person.gender == 'female':
            return 'Ms'
        return ''

    def get_title(self):
        """Returns the title of the person."""
        return self.person.title

    def get_romanized_first_name(self):
        """Returns the first name in roman letters."""
        return self.person.roman_first_name

    def get_romanized_last_name(self):
        """Returns the first name in roman letters."""
        return self.person.roman_last_name

    def get_non_romanized_first_name(self):
        """Returns the non roman version of the first name."""
        return self.person.non_roman_first_name

    def get_non_romanized_last_name(self):
        """Returns the non roman version of the first name."""
        return self.person.non_roman_last_name

    def get_nickname(self):
        """Returns the nickname of a person in roman letters."""
        return self.person.chosen_name

class PersonPluginModel(CMSPlugin):
    """Model for the ``PersonPlugin`` cms plugin."""
    display_type = models.CharField(
        max_length=256,
        choices=settings.DISPLAY_TYPE_CHOICES,
        verbose_name=_('Display type'),
    )

    person = models.ForeignKey(
        Person,
        verbose_name=_('Person'),
    )

    def copy_relations(self, oldinstance):
        self.person = oldinstance.person

    def __unicode__(self):
        return self.person.__unicode__()

class Link(models.Model):
    """
    A person can have many links.

    """
    person = models.ForeignKey(
        Person,
        verbose_name=_('Person'),
    )

    link_type = models.ForeignKey(
        LinkType,
        verbose_name=_('Link type'),
    )

    url = models.URLField(
        verbose_name=_('URL'),
    )

    def __unicode__(self):
        return self.url
<commit_msg>Add correct English translation for plurals of Person and Nationality.<commit_after>"""Models for the ``people`` app."""
from django.db import models
from django.utils.translation import ugettext_lazy as _

from cms.models.pluginmodel import CMSPlugin
from django_libs.models_mixins import SimpleTranslationMixin
from filer.fields.file import FilerFileField
from localized_names.templatetags.localized_names_tags import get_name

from . import settings

# Hack to have these strings translated
mr = _('Mr')
mrs = _('Ms')

GENDER_CHOICES = [
    ('male', _('male')),
    ('female', _('female')),
]

TITLE_CHOICES = [
    ('Dr', _('Dr')),
    ('Prof', _('Prof')),
    ('Prof Dr', _('Prof Dr')),
]

class LinkType(SimpleTranslationMixin, models.Model):
    """
    A link type could be ``Facebook`` or ``Twitter`` or ``Website``.

    This is masterdata that should be created by the admins when the site is
    deployed for the first time.

    For translateable fields see ``LinkTypeTranslation`` model.

    :ordering: Enter numbers here if you want links to be displayed in a
      special order.

    """
    slug = models.SlugField(
        max_length=256,
        verbose_name=_('Slug'),
        help_text=_(
            'Use this field to define a simple identifier that can be used'
            ' to style the different link types (i.e. assign social media'
            ' icons to them)'),
        blank=True,
    )

    ordering = models.PositiveIntegerField(
        verbose_name=_('Ordering'),
        null=True, blank=True,
    )

    class Meta:
        ordering = ['ordering', ]

    def __unicode__(self):
        return self.get_translation().name

class LinkTypeTranslation(models.Model):
    """
    Translateable fields of the ``LinkType`` model.

    """
    name = models.CharField(
        max_length=256,
        verbose_name=_('Name'),
    )

    # needed by simple-translation
    link_type = models.ForeignKey(LinkType)
    language = models.CharField(max_length=16)

class Nationality(SimpleTranslationMixin, models.Model):
    """
    The nationality of a Person.

    For translateable fields see the ``NationalityTranslation`` model.

    """
    def __unicode__(self):
        return self.get_translation().name

    class Meta:
        verbose_name_plural = _('Nationalities')

class NationalityTranslation(models.Model):
    """
    The translateable fields of the ``Nationality`` model.

    :name: E.g. 'Chinese' or 'Deutsch'

    """
    name = models.CharField(
        max_length=128,
        verbose_name=_('Name'),
    )

    # needed by simple-translation
    nationality = models.ForeignKey(Nationality)
    language = models.CharField(max_length=16)

class Role(SimpleTranslationMixin, models.Model):
    """
    People can have certain roles in an organisation.

    For translateable fields see ``RoleTranslation`` model.

    :name: The name of the role.

    """
    def __unicode__(self):
        return self.get_translation().name

class RoleTranslation(models.Model):
    """
    Translateable fields of the ``Role`` model.

    :name: The name of the role.

    """
    name = models.CharField(
        max_length=256,
        verbose_name=_('Role'),
    )

    role_description = models.TextField(
        max_length=4000,
        verbose_name=_('Role description'),
        blank=True,
    )

    # needed by simple-translation
    role = models.ForeignKey(Role)
    language = models.CharField(max_length=16)

class Person(SimpleTranslationMixin, models.Model):
    """
    A model that holds information about a person.

    For translateable fields see ``PersonTitle`` model.

    :roman_first_name: The first name in roman letters.
    :roman_last_name: The last name in roman letters.
    :non_roman_first_name: The first name in non roman letters.
    :non_roman_last_name: The last name in non roman letters.
    :gender: The gender of the person.
    :title: The title of the person.
    :chosen_name: For asian people, this is the chosen western name.
    :role: Role of the person within the organisation.
    :picture: A picture of the person.
    :phone: Phonenumber of the person.
    :email: Email address of the person.
    :ordering: Enter numbers if you want to order the list of persons on your
      site in a special way.
    :nationality: The nationality of a person.

    """
    roman_first_name = models.CharField(
        max_length=256,
        verbose_name=_('Roman first name'),
        blank=True
    )

    roman_last_name = models.CharField(
        max_length=256,
        verbose_name=_('Roman last name'),
        blank=True,
    )

    non_roman_first_name = models.CharField(
        max_length=256,
        verbose_name=_('Non roman first name'),
        blank=True
    )

    non_roman_last_name = models.CharField(
        max_length=256,
        verbose_name=_('Non roman last name'),
        blank=True,
    )

    gender = models.CharField(
        max_length=16,
        choices=GENDER_CHOICES,
        verbose_name=_('Gender'),
        blank=True,
    )

    title = models.CharField(
        max_length=16,
        choices=TITLE_CHOICES,
        verbose_name=_('Title'),
        blank=True,
    )

    chosen_name = models.CharField(
        max_length=256,
        verbose_name=_('Chosen name'),
        blank=True,
    )

    role = models.ForeignKey(
        Role,
        verbose_name=_('Role'),
        null=True, blank=True,
    )

    picture = FilerFileField(
        verbose_name=_('Picture'),
        null=True, blank=True,
    )

    phone = models.CharField(
        max_length=32,
        verbose_name=_('Phone'),
        blank=True,
    )

    email = models.EmailField(
        verbose_name=_('Email'),
        blank=True,
    )

    ordering = models.PositiveIntegerField(
        verbose_name=_('Ordering'),
        null=True, blank=True,
    )

    nationality = models.ForeignKey(
        Nationality,
        verbose_name=_('Nationality'),
        blank=True, null=True,
    )

    class Meta:
        ordering = ['ordering', ]
        verbose_name_plural = _('People')

    def __unicode__(self):
        trans = self.get_translation()
        return get_name(trans, 'SHORT_NAME_FORMAT')

class PersonTranslation(models.Model):
    """
    Translateable fields of the ``Person`` model.

    :short_bio: A short description of the person.
    :bio: A longer description of the person, could appear after a
      ``read more`` link behind the ``short_bio``.

    """
    short_bio = models.TextField(
        max_length=512,
        verbose_name=_('Short bio'),
        blank=True,
    )

    bio = models.TextField(
        max_length=4000,
        verbose_name=_('Biography'),
        blank=True,
    )

    # needed by simple-translation
    person = models.ForeignKey(Person)
    language = models.CharField(max_length=16)

    def get_gender(self):
        """Returns either 'Mr.' or 'Ms.' depending on the gender."""
        if self.person.gender == 'male':
            return 'Mr'
        elif self.person.gender == 'female':
            return 'Ms'
        return ''

    def get_title(self):
        """Returns the title of the person."""
        return self.person.title

    def get_romanized_first_name(self):
        """Returns the first name in roman letters."""
        return self.person.roman_first_name

    def get_romanized_last_name(self):
        """Returns the first name in roman letters."""
        return self.person.roman_last_name

    def get_non_romanized_first_name(self):
        """Returns the non roman version of the first name."""
        return self.person.non_roman_first_name

    def get_non_romanized_last_name(self):
        """Returns the non roman version of the first name."""
        return self.person.non_roman_last_name

    def get_nickname(self):
        """Returns the nickname of a person in roman letters."""
        return self.person.chosen_name

class PersonPluginModel(CMSPlugin):
    """Model for the ``PersonPlugin`` cms plugin."""
    display_type = models.CharField(
        max_length=256,
        choices=settings.DISPLAY_TYPE_CHOICES,
        verbose_name=_('Display type'),
    )

    person = models.ForeignKey(
        Person,
        verbose_name=_('Person'),
    )

    def copy_relations(self, oldinstance):
        self.person = oldinstance.person

    def __unicode__(self):
        return self.person.__unicode__()

class Link(models.Model):
    """
    A person can have many links.

    """
    person = models.ForeignKey(
        Person,
        verbose_name=_('Person'),
    )

    link_type = models.ForeignKey(
        LinkType,
        verbose_name=_('Link type'),
    )

    url = models.URLField(
        verbose_name=_('URL'),
    )

    def __unicode__(self):
        return self.url
<|endoftext|>


<commit_before>import numpy as np
import rpy2.rinterface as ri
from rpy2.robjects.packages import importr
from rpy2.robjects.numpy2ri import numpy2ri

VINECOPULA = importr('VineCopula')

def check_matrix(value):
    assert isinstance(value, np.ndarray), \
        TypeError('Variable must be a numpy array.')
    assert value.ndim == 2, \
        AttributeError('Matrix should be of dimension 2.')
    assert value.shape[0] == value.shape[1], \
        AttributeError('Matrix should be squared.')

def check_family(matrix):
    d = matrix.shape[0]
    for i in range(d):
        for j in range(i):
            if isinstance(matrix[i, j], str):
                matrix[i, j] = int(VINECOPULA.BiCopName(matrix[i, j], False)[0])
            elif isinstance(matrix[i, j], int):
                pass
            else:
                raise ValueError("Uncorrect Family matrix")

    return matrix
class VineCopula(object):
    """Vine Copula Class."""

    def __init__(self, structure, family, param1, param2=None):
        self.structure = structure
        self.family = family
        self.param1 = param1
        self.param2 = param2

        ri.initr()

        self.build_vine()

    @property
    def structure(self):
        return self._structure

    @structure.setter
    def structure(self, value):
        check_matrix(value)
        self._dim = value.shape[0]
        self._structure = value

    @property
    def family(self):
        return self._family

    @family.setter
    def family(self, value):
        check_matrix(value)
        check_family(value)
        assert value.shape[0] == self._dim, \
            AttributeError('Family matrix should be of dimension == %d' % (self._dim))
        self._family = value

    @property
    def param1(self):
        return self._param1

    @param1.setter
    def param1(self, value):
        check_matrix(value)
        assert value.shape[0] == self._dim, \
            AttributeError('Family matrix should be of dimension == %d' % (self._dim))
        self._param1 = value

    @property
    def param2(self):
        return self._param2

    @param2.setter
    def param2(self, value):
        if value is None:
            value = np.zeros((self._dim, self._dim))
        check_matrix(value)
        assert value.shape[0] == self._dim, \
            AttributeError('Family matrix should be of dimension == %d' % (self._dim))
        self._param2 = value

    def build_vine(self):
        """
        """
        r_structure = numpy2ri(self.structure)
        r_family = numpy2ri(self.family)
        r_par = numpy2ri(self.param1)
        r_par2 = numpy2ri(self.param2)
        self._rvine = VINECOPULA.RVineMatrix(r_structure, r_family, r_par, r_par2)

    def get_sample(self, n_obs):
        """
        """
        assert isinstance(n_obs, int), \
            TypeError("Sample size must be an integer.")
        assert n_obs > 0, \
            ValueError("Sample size must be positive.")
        return np.asarray(VINECOPULA.RVineSim(n_obs, self._rvine))
<commit_msg>Added an attribute to_rebuild in the VineCopula class<commit_after>import numpy as np
import rpy2.rinterface as ri
from rpy2.robjects.packages import importr
from rpy2.robjects.numpy2ri import numpy2ri

VINECOPULA = importr('VineCopula')

def check_matrix(value):
    assert isinstance(value, np.ndarray), \
        TypeError('Variable must be a numpy array.')
    assert value.ndim == 2, \
        AttributeError('Matrix should be of dimension 2.')
    assert value.shape[0] == value.shape[1], \
        AttributeError('Matrix should be squared.')

def check_family(matrix):
    d = matrix.shape[0]
    for i in range(d):
        for j in range(i):
            if isinstance(matrix[i, j], str):
                matrix[i, j] = int(VINECOPULA.BiCopName(matrix[i, j], False)[0])
            elif isinstance(matrix[i, j], int):
                pass
            else:
                raise ValueError("Uncorrect Family matrix")

    return matrix
class VineCopula(object):
    """Vine Copula Class."""

    def __init__(self, structure, family, param1, param2=None):
        self.structure = structure
        self.family = family
        self.param1 = param1
        self.param2 = param2
        ri.initr()
        self.build_vine()

    @property
    def structure(self):
        return self._structure

    @structure.setter
    def structure(self, value):
        check_matrix(value)
        self._dim = value.shape[0]
        self._structure = value
        self._to_rebuild = True

    @property
    def family(self):
        return self._family

    @family.setter
    def family(self, value):
        check_matrix(value)
        check_family(value)
        assert value.shape[0] == self._dim, \
            AttributeError('Family matrix should be of dimension == %d' % (self._dim))
        self._family = value
        self._to_rebuild = True

    @property
    def param1(self):
        return self._param1

    @param1.setter
    def param1(self, value):
        check_matrix(value)
        assert value.shape[0] == self._dim, \
            AttributeError('Family matrix should be of dimension == %d' % (self._dim))
        self._param1 = value
        self._to_rebuild = True

    @property
    def param2(self):
        return self._param2

    @param2.setter
    def param2(self, value):
        if value is None:
            value = np.zeros((self._dim, self._dim))
        check_matrix(value)
        assert value.shape[0] == self._dim, \
            AttributeError('Family matrix should be of dimension == %d' % (self._dim))
        self._param2 = value
        self._to_rebuild = True

    def build_vine(self):
        """
        """
        r_structure = numpy2ri(self.structure)
        r_family = numpy2ri(self.family)
        r_par = numpy2ri(self.param1)
        r_par2 = numpy2ri(self.param2)
        self._rvine = VINECOPULA.RVineMatrix(r_structure, r_family, r_par, r_par2)
        self._to_rebuild = False

    def get_sample(self, n_obs):
        """
        """
        assert isinstance(n_obs, int), \
            TypeError("Sample size must be an integer.")
        assert n_obs > 0, \
            ValueError("Sample size must be positive.")
        if self._to_rebuild:
            self.build_vine()
        return np.asarray(VINECOPULA.RVineSim(n_obs, self._rvine))
<|endoftext|>