<commit_before>"""Scripts to migrate legacy objects in existing databases."""
import logging
from functools import wraps
from pyramid.registry import Registry
from pyramid.threadlocal import get_current_registry
from zope.interface.interfaces import IInterface
from zope.interface import alsoProvides
from zope.interface import noLongerProvides
from zope.interface import directlyProvides
from substanced.evolution import add_evolution_step
from substanced.util import find_service
from substanced.interfaces import IFolder
from adhocracy_core.utils import get_sheet
from adhocracy_core.interfaces import IResource
from adhocracy_core.interfaces import search_query
from adhocracy_core.interfaces import ResourceMetadata
from adhocracy_core.sheets.pool import IPool
from adhocracy_core.sheets.title import ITitle
from adhocracy_core.sheets.badge import IHasBadgesPool
from adhocracy_core.sheets.badge import IBadgeable
from adhocracy_core.resources.pool import IBasicPool
from adhocracy_core.resources.asset import IPoolWithAssets
from adhocracy_core.resources.badge import add_badges_service
from adhocracy_core.resources.badge import add_badge_assignments_service
from adhocracy_core.resources.principal import IUser
from adhocracy_core.catalog import ICatalogsService

logger = logging.getLogger(__name__)

def migrate_new_sheet(context: IPool,
                      iresource: IInterface,
                      isheet: IInterface,
                      isheet_old: IInterface=None,
                      remove_isheet_old=False,
                      fields_mapping: [(str, str)]=[]):
    """Add new `isheet` to `iresource` resources and migrate field values.

    :param context: Pool to search for `iresource` resources
    :param iresource: resource type to migrate
    :param isheet: new sheet interface to add
    :param isheet_old: old sheet interface to migrate,
        must not be None if `fields_mapping` or `remove_isheet_old` is set.
    :param remove_isheet_old: remove old sheet interface
    :param fields_mapping: list of (field name, old field name) to
                           migrate field values.
    """
    registry = get_current_registry(context)
    catalogs = find_service(context, 'catalogs')
    interfaces = isheet_old and (isheet_old, iresource) or iresource
    query = search_query._replace(interfaces=interfaces)
    resources = catalogs.search(query).elements
    count = len(resources)
    logger.info('Migrating {0} {1} to new sheet {2}'.format(count, iresource,
                                                            isheet))
    for index, resource in enumerate(resources):
        logger.info('Migrating {0} of {1}: {2}'.format(index + 1, count,
                                                       resource))
        logger.info('Add {0}  sheet'.format(isheet))
        alsoProvides(resource, isheet)
        if fields_mapping:
            _migrate_field_values(registry, resource, isheet, isheet_old,
                                  fields_mapping)
        if remove_isheet_old:
            logger.info('Remove {0} sheet'.format(isheet_old))
            noLongerProvides(resource, isheet_old)

def migrate_new_iresource(context: IResource,
                          old_iresource: IInterface,
                          new_iresource: IInterface):
    """Migrate resources with `old_iresource` interface to `new_iresource`."""
    meta = _get_resource_meta(context, new_iresource)
    catalogs = find_service(context, 'catalogs')
    resources = _search_for_interfaces(catalogs, old_iresource)
    for resource in resources:
        logger.info('Migrate iresource of {0}'.format(resource))
        noLongerProvides(resource, old_iresource)
        directlyProvides(resource, new_iresource)
        for sheet in meta.basic_sheets + meta.extended_sheets:
            alsoProvides(resource, sheet)
        catalogs.reindex_index(resource, 'interfaces')

def _get_resource_meta(context: IResource,
                       iresource: IInterface) -> ResourceMetadata:
    registry = get_current_registry(context)
    meta = registry.content.resources_meta[iresource]
    return meta

def _search_for_interfaces(catalogs: ICatalogsService,
                           interfaces: (IInterface)) -> [IResource]:
    query = search_query._replace(interfaces=interfaces)
    resources = catalogs.search(query).elements
    return resources

def log_migration(func):
    """Decorator for the migration scripts.

    The decorator logs the call to the evolve script.
    """
    logger = logging.getLogger(func.__module__)

    @wraps(func)
    def logger_decorator(*args, **kwargs):
        logger.info('Running evolve step: ' + func.__doc__)
        func(*args, **kwargs)
        logger.info('Finished evolve step: ' + func.__doc__)

    return logger_decorator

def _migrate_field_values(registry: Registry, resource: IResource,
                          isheet: IInterface, isheet_old: IInterface,
                          fields_mapping=[(str, str)]):
    sheet = get_sheet(resource, isheet, registry=registry)
    old_sheet = get_sheet(resource, isheet_old, registry=registry)
    appstruct = {}
    for field, old_field in fields_mapping:
        logger.info('Migrate value for field {0}'.format(field))
        appstruct[field] = old_sheet.get()[old_field]
        old_sheet.delete_field_values([old_field])
    sheet.set(appstruct)

def _get_autonaming_prefixes(registry: Registry) -> [str]:
    """Return all autonaming_prefixes defined in the resources metadata."""
    meta = registry.content.resources_meta.values()
    prefixes = [m.autonaming_prefix for m in meta if m.use_autonaming]
    return list(set(prefixes))

@log_migration
def evolve1_add_title_sheet_to_pools(root: IPool):  # pragma: no cover
    """Add title sheet to basic pools and asset pools."""
    migrate_new_sheet(root, IBasicPool, ITitle)
    migrate_new_sheet(root, IPoolWithAssets, ITitle)

def add_kiezkassen_permissions(root):  # pragma: no cover
    """(disabled) Add permission to use the kiezkassen process."""

@log_migration
def upgrade_catalogs(root):  # pragma: no cover
    """Upgrade catalogs."""
    registry = get_current_registry()
    old_catalogs = root['catalogs']

    old_catalogs.move('system', root, 'old_system_catalog')
    old_catalogs.move('adhocracy', root, 'old_adhocracy_catalog')

    del root['catalogs']

    registry.content.create(ICatalogsService.__identifier__, parent=root)

    catalogs = root['catalogs']
    del catalogs['system']
    del catalogs['adhocracy']
    root.move('old_system_catalog', catalogs, 'system')
    root.move('old_adhocracy_catalog', catalogs, 'adhocracy')

@log_migration
def make_users_badgeable(root):  # pragma: no cover
    """Add badge services and make user badgeable."""
    registry = get_current_registry(root)
    principals = find_service(root, 'principals')
    if not IHasBadgesPool.providedBy(principals):
        logger.info('Add badges service to {0}'.format(principals))
        add_badges_service(principals, registry, {})
        alsoProvides(principals, IHasBadgesPool)
    users = find_service(root, 'principals', 'users')
    assignments = find_service(users, 'badge_assignments')
    if assignments is None:
        logger.info('Add badge assignments service to {0}'.format(users))
        add_badge_assignments_service(users, registry, {})
    migrate_new_sheet(root, IUser, IBadgeable)

@log_migration
def change_pools_autonaming_scheme(root):  # pragma: no cover
    """Change pool autonaming scheme."""
    registry = get_current_registry(root)
    prefixes = _get_autonaming_prefixes(registry)
    catalogs = find_service(root, 'catalogs')
    pools = _search_for_interfaces(catalogs, (IPool, IFolder))
    count = len(pools)
    for index, pool in enumerate(pools):
        logger.info('Migrating {0} of {1}: {2}'.format(index + 1, count, pool))
        if hasattr(pool, '_autoname_last'):
            pool._autoname_lasts = {prefix: pool._autoname_last
                                    for prefix in prefixes}
            del pool._autoname_last

@log_migration
def hide_password_resets(root):  # pragma: no cover
    """Add hide all password reset objects."""
    from adhocracy_core.resources.principal import hide
    from adhocracy_core.resources.principal import deny_view_permission
    registry = get_current_registry(root)
    resets = find_service(root, 'principals', 'resets')
    not_hidden = getattr(resets, 'hidden', False)
    if not_hidden:
        logger.info('Deny view permission for {0}'.format(resets))
        deny_view_permission(resets, registry, {})

        logger.info('Hide {0}'.format(resets))
        hide(resets, registry, {})

def includeme(config):  # pragma: no cover
    """Register evolution utilities and add evolution steps."""
    config.add_directive('add_evolution_step', add_evolution_step)
    config.scan('substanced.evolution.subscribers')
    config.add_evolution_step(upgrade_catalogs)
    config.add_evolution_step(evolve1_add_title_sheet_to_pools)
    config.add_evolution_step(add_kiezkassen_permissions)
    config.add_evolution_step(make_users_badgeable)
<<<<<<< HEAD
    config.add_evolution_step(change_pools_autonaming_scheme)
=======
    config.add_evolution_step(hide_password_resets)
>>>>>>> master
<commit_msg>Fix merge conflict<commit_after>"""Scripts to migrate legacy objects in existing databases."""
import logging
from functools import wraps
from pyramid.registry import Registry
from pyramid.threadlocal import get_current_registry
from zope.interface.interfaces import IInterface
from zope.interface import alsoProvides
from zope.interface import noLongerProvides
from zope.interface import directlyProvides
from substanced.evolution import add_evolution_step
from substanced.util import find_service
from substanced.interfaces import IFolder
from adhocracy_core.utils import get_sheet
from adhocracy_core.interfaces import IResource
from adhocracy_core.interfaces import search_query
from adhocracy_core.interfaces import ResourceMetadata
from adhocracy_core.sheets.pool import IPool
from adhocracy_core.sheets.title import ITitle
from adhocracy_core.sheets.badge import IHasBadgesPool
from adhocracy_core.sheets.badge import IBadgeable
from adhocracy_core.resources.pool import IBasicPool
from adhocracy_core.resources.asset import IPoolWithAssets
from adhocracy_core.resources.badge import add_badges_service
from adhocracy_core.resources.badge import add_badge_assignments_service
from adhocracy_core.resources.principal import IUser
from adhocracy_core.catalog import ICatalogsService

logger = logging.getLogger(__name__)

def migrate_new_sheet(context: IPool,
                      iresource: IInterface,
                      isheet: IInterface,
                      isheet_old: IInterface=None,
                      remove_isheet_old=False,
                      fields_mapping: [(str, str)]=[]):
    """Add new `isheet` to `iresource` resources and migrate field values.

    :param context: Pool to search for `iresource` resources
    :param iresource: resource type to migrate
    :param isheet: new sheet interface to add
    :param isheet_old: old sheet interface to migrate,
        must not be None if `fields_mapping` or `remove_isheet_old` is set.
    :param remove_isheet_old: remove old sheet interface
    :param fields_mapping: list of (field name, old field name) to
                           migrate field values.
    """
    registry = get_current_registry(context)
    catalogs = find_service(context, 'catalogs')
    interfaces = isheet_old and (isheet_old, iresource) or iresource
    query = search_query._replace(interfaces=interfaces)
    resources = catalogs.search(query).elements
    count = len(resources)
    logger.info('Migrating {0} {1} to new sheet {2}'.format(count, iresource,
                                                            isheet))
    for index, resource in enumerate(resources):
        logger.info('Migrating {0} of {1}: {2}'.format(index + 1, count,
                                                       resource))
        logger.info('Add {0}  sheet'.format(isheet))
        alsoProvides(resource, isheet)
        if fields_mapping:
            _migrate_field_values(registry, resource, isheet, isheet_old,
                                  fields_mapping)
        if remove_isheet_old:
            logger.info('Remove {0} sheet'.format(isheet_old))
            noLongerProvides(resource, isheet_old)

def migrate_new_iresource(context: IResource,
                          old_iresource: IInterface,
                          new_iresource: IInterface):
    """Migrate resources with `old_iresource` interface to `new_iresource`."""
    meta = _get_resource_meta(context, new_iresource)
    catalogs = find_service(context, 'catalogs')
    resources = _search_for_interfaces(catalogs, old_iresource)
    for resource in resources:
        logger.info('Migrate iresource of {0}'.format(resource))
        noLongerProvides(resource, old_iresource)
        directlyProvides(resource, new_iresource)
        for sheet in meta.basic_sheets + meta.extended_sheets:
            alsoProvides(resource, sheet)
        catalogs.reindex_index(resource, 'interfaces')

def _get_resource_meta(context: IResource,
                       iresource: IInterface) -> ResourceMetadata:
    registry = get_current_registry(context)
    meta = registry.content.resources_meta[iresource]
    return meta

def _search_for_interfaces(catalogs: ICatalogsService,
                           interfaces: (IInterface)) -> [IResource]:
    query = search_query._replace(interfaces=interfaces)
    resources = catalogs.search(query).elements
    return resources

def log_migration(func):
    """Decorator for the migration scripts.

    The decorator logs the call to the evolve script.
    """
    logger = logging.getLogger(func.__module__)

    @wraps(func)
    def logger_decorator(*args, **kwargs):
        logger.info('Running evolve step: ' + func.__doc__)
        func(*args, **kwargs)
        logger.info('Finished evolve step: ' + func.__doc__)

    return logger_decorator

def _migrate_field_values(registry: Registry, resource: IResource,
                          isheet: IInterface, isheet_old: IInterface,
                          fields_mapping=[(str, str)]):
    sheet = get_sheet(resource, isheet, registry=registry)
    old_sheet = get_sheet(resource, isheet_old, registry=registry)
    appstruct = {}
    for field, old_field in fields_mapping:
        logger.info('Migrate value for field {0}'.format(field))
        appstruct[field] = old_sheet.get()[old_field]
        old_sheet.delete_field_values([old_field])
    sheet.set(appstruct)

def _get_autonaming_prefixes(registry: Registry) -> [str]:
    """Return all autonaming_prefixes defined in the resources metadata."""
    meta = registry.content.resources_meta.values()
    prefixes = [m.autonaming_prefix for m in meta if m.use_autonaming]
    return list(set(prefixes))

@log_migration
def evolve1_add_title_sheet_to_pools(root: IPool):  # pragma: no cover
    """Add title sheet to basic pools and asset pools."""
    migrate_new_sheet(root, IBasicPool, ITitle)
    migrate_new_sheet(root, IPoolWithAssets, ITitle)

def add_kiezkassen_permissions(root):  # pragma: no cover
    """(disabled) Add permission to use the kiezkassen process."""

@log_migration
def upgrade_catalogs(root):  # pragma: no cover
    """Upgrade catalogs."""
    registry = get_current_registry()
    old_catalogs = root['catalogs']

    old_catalogs.move('system', root, 'old_system_catalog')
    old_catalogs.move('adhocracy', root, 'old_adhocracy_catalog')

    del root['catalogs']

    registry.content.create(ICatalogsService.__identifier__, parent=root)

    catalogs = root['catalogs']
    del catalogs['system']
    del catalogs['adhocracy']
    root.move('old_system_catalog', catalogs, 'system')
    root.move('old_adhocracy_catalog', catalogs, 'adhocracy')

@log_migration
def make_users_badgeable(root):  # pragma: no cover
    """Add badge services and make user badgeable."""
    registry = get_current_registry(root)
    principals = find_service(root, 'principals')
    if not IHasBadgesPool.providedBy(principals):
        logger.info('Add badges service to {0}'.format(principals))
        add_badges_service(principals, registry, {})
        alsoProvides(principals, IHasBadgesPool)
    users = find_service(root, 'principals', 'users')
    assignments = find_service(users, 'badge_assignments')
    if assignments is None:
        logger.info('Add badge assignments service to {0}'.format(users))
        add_badge_assignments_service(users, registry, {})
    migrate_new_sheet(root, IUser, IBadgeable)

@log_migration
def change_pools_autonaming_scheme(root):  # pragma: no cover
    """Change pool autonaming scheme."""
    registry = get_current_registry(root)
    prefixes = _get_autonaming_prefixes(registry)
    catalogs = find_service(root, 'catalogs')
    pools = _search_for_interfaces(catalogs, (IPool, IFolder))
    count = len(pools)
    for index, pool in enumerate(pools):
        logger.info('Migrating {0} of {1}: {2}'.format(index + 1, count, pool))
        if hasattr(pool, '_autoname_last'):
            pool._autoname_lasts = {prefix: pool._autoname_last
                                    for prefix in prefixes}
            del pool._autoname_last

@log_migration
def hide_password_resets(root):  # pragma: no cover
    """Add hide all password reset objects."""
    from adhocracy_core.resources.principal import hide
    from adhocracy_core.resources.principal import deny_view_permission
    registry = get_current_registry(root)
    resets = find_service(root, 'principals', 'resets')
    not_hidden = getattr(resets, 'hidden', False)
    if not_hidden:
        logger.info('Deny view permission for {0}'.format(resets))
        deny_view_permission(resets, registry, {})

        logger.info('Hide {0}'.format(resets))
        hide(resets, registry, {})

def includeme(config):  # pragma: no cover
    """Register evolution utilities and add evolution steps."""
    config.add_directive('add_evolution_step', add_evolution_step)
    config.scan('substanced.evolution.subscribers')
    config.add_evolution_step(upgrade_catalogs)
    config.add_evolution_step(evolve1_add_title_sheet_to_pools)
    config.add_evolution_step(add_kiezkassen_permissions)
    config.add_evolution_step(make_users_badgeable)
    config.add_evolution_step(change_pools_autonaming_scheme)
    config.add_evolution_step(hide_password_resets)
<|endoftext|>


<commit_before>import bisect
from collections import namedtuple, Container

# Singleton to mark unmapped ranges
_empty = object()

MappedRange = namedtuple('MappedRange', ('start', 'stop', 'value'))

def _remove_all(mapping, keys):
	'''Remove all keys from mapping.'''
	for key in keys:
		del mapping[key]

class RangeMap(Container):

	def __init__(self, mapping=None, default_value=_empty):
		'''
		Args:
			mapping: A Mapping from range start dates to values. The end of each
				range is the beginning of the next
		'''
		self.ordered_keys = []
		self.key_mapping = {}
		self._left_value = default_value
		if mapping:
			for key, value in sorted(mapping.items()):
				self.set(value, key)

	def __repr__(self):
		return 'RangeMap(%s)' % ', '.join(['({start}, {stop}): {value}'.format(
			start=item.start,
			stop=item.stop,
			value=repr(item.value),
			) for item in self.ranges()])

	@classmethod
	def from_iterable(cls, iterable):
		'''Create a RangeMap from an iterable where each item is a tuple
		(start, stop, value)
		'''
		obj = cls()
		for start, stop, value in iterable:
			obj.set(value, start=start, stop=stop)
		return obj

	def ranges(self):
		'''Generate MappedRanges for all mapped ranges.'''
		if len(self.ordered_keys) == 0:
			if self._left_value is not _empty:
				yield MappedRange(None, None, self._left_value)
		else:
			if self._left_value is not _empty:
				yield MappedRange(None, self.ordered_keys[0], self._left_value)
			for i, start_key in enumerate(self.ordered_keys[:-1]):
				value = self.key_mapping[start_key]
				if value is not _empty:
					yield MappedRange(start_key, self.ordered_keys[i+1], value)
			last_value = self.key_mapping[self.ordered_keys[-1]]
			if last_value is not _empty:
				yield MappedRange(self.ordered_keys[-1], None, last_value)

	def __contains__(self, value):
		return self.__getitem(value) is not _empty

	def __getitem(self, key):
		'''Helper method.'''
		loc = bisect.bisect_left(self.ordered_keys, key)
		if loc == 0 and (len(self.ordered_keys) == 0 or key < self.ordered_keys[loc]):
			return self._left_value
		else:
			if loc < len(self.ordered_keys) and key == self.ordered_keys[loc]:
				return self.key_mapping[self.ordered_keys[loc]]
			else:
				return self.key_mapping[self.ordered_keys[loc-1]]

	def get(self, key, restval=None):
		value = self.__getitem(key)
		if value is _empty:
			return restval
		else:
			return value

	def get_range(self, start=None, stop=None):
		raise NotImplementedError('yet')

	def set(self, value, start=None, stop=None):
		if start is None:
			if stop is None:
				self.ordered_keys = []
				self.key_mapping = {}
			else:
				stop_index = bisect.bisect_left(self.ordered_keys, stop)
				self.key_mapping[stop] = self.__getitem(stop)
				_remove_all(self.key_mapping, self.ordered_keys[:stop_index])
				if stop_index < len(self.ordered_keys) and self.ordered_keys[stop_index] == stop:
					self.ordered_keys[:stop_index+1] = [stop]
				else:
					self.ordered_keys[:stop_index] = [stop]
			self._left_value = value
		else:
			start_index = bisect.bisect_left(self.ordered_keys, start)
			if stop is None:
				stop_index = len(self.ordered_keys)
				new_keys = [start]
			else:
				stop_index = bisect.bisect_left(self.ordered_keys, stop)
				self.key_mapping[stop] = self.__getitem(stop)
				new_keys = [start, stop]
			_remove_all(self.key_mapping, self.ordered_keys[start_index:stop_index])
			if stop_index < len(self.ordered_keys) and self.ordered_keys[stop_index] == stop:
				self.ordered_keys[start_index:stop_index+1] = new_keys
			else:
				self.ordered_keys[start_index:stop_index] = new_keys
			self.key_mapping[start] = value

	def delete(self, start=None, stop=None):
		self.set(_empty, start=start, stop=stop)

	def __eq__(self, other):
		if isinstance(other, RangeMap):
			return (
				self.key_mapping == other.key_mapping and
				self._left_value == other._left_value
				)
		else:
			return NotImplemented

	def __getitem__(self, key):
		if isinstance(key, slice):
			if key.step:
				raise ValueError('Steps aren\'t allowed')
			# return a RangeMap
			return self.get_range(key.start, key.stop)
		else:
			value = self.__getitem(key)
			if value is _empty:
				raise KeyError()
			else:
				return value

	def __setitem__(self, key, value):
		if not isinstance(key, slice):
			raise ValueError('Can only set slices')
		if key.step is not None:
			raise ValueError('Steps aren\'t allowed')
		self.set(value, key.start, key.stop)

	def __delitem__(self, index):
		if not isinstance(index, slice):
			raise ValueError('Can only delete slices')
		if index.step is not None:
			raise ValueError('Steps aren\'t allowed')
		self.delete(index.start, index.stop)

	# Python2 - override slice methods
	def __setslice__(self, i, j, value):
		'''Implement __setslice__ to override behavior in Python 2.
		This is required because empty slices pass integers in python2 as opposed
		to None in python 3.
		'''
		raise SyntaxError('Assigning slices doesn\t work in Python 2, use set')

	def __delslice__(self, i, j):
		raise SyntaxError('Deleting slices doesn\t work in Python 2, use delete')

	def __getslice__(self, i, j):
		raise SyntaxError('Getting slices doesn\t work in Python 2, use get_range.')
<commit_msg>Add underscore to attribute names.<commit_after>import bisect
from collections import namedtuple, Container

# Singleton to mark unmapped ranges
_empty = object()

MappedRange = namedtuple('MappedRange', ('start', 'stop', 'value'))

def _remove_all(mapping, keys):
	'''Remove all keys from mapping.'''
	for key in keys:
		del mapping[key]

class RangeMap(Container):

	def __init__(self, mapping=None, default_value=_empty):
		'''
		Args:
			mapping: A Mapping from range start dates to values. The end of each
				range is the beginning of the next
		'''
		self._ordered_keys = []
		self._key_mapping = {}
		self._left_value = default_value
		if mapping:
			for key, value in sorted(mapping.items()):
				self.set(value, key)

	def __repr__(self):
		return 'RangeMap(%s)' % ', '.join(['({start}, {stop}): {value}'.format(
			start=item.start,
			stop=item.stop,
			value=repr(item.value),
			) for item in self.ranges()])

	@classmethod
	def from_iterable(cls, iterable):
		'''Create a RangeMap from an iterable where each item is a tuple
		(start, stop, value)
		'''
		obj = cls()
		for start, stop, value in iterable:
			obj.set(value, start=start, stop=stop)
		return obj

	def ranges(self):
		'''Generate MappedRanges for all mapped ranges.'''
		if len(self._ordered_keys) == 0:
			if self._left_value is not _empty:
				yield MappedRange(None, None, self._left_value)
		else:
			if self._left_value is not _empty:
				yield MappedRange(None, self._ordered_keys[0], self._left_value)
			for i, start_key in enumerate(self._ordered_keys[:-1]):
				value = self._key_mapping[start_key]
				if value is not _empty:
					yield MappedRange(start_key, self._ordered_keys[i+1], value)
			last_value = self._key_mapping[self._ordered_keys[-1]]
			if last_value is not _empty:
				yield MappedRange(self._ordered_keys[-1], None, last_value)

	def __contains__(self, value):
		return self.__getitem(value) is not _empty

	def __getitem(self, key):
		'''Helper method.'''
		loc = bisect.bisect_left(self._ordered_keys, key)
		if loc == 0 and (len(self._ordered_keys) == 0 or key < self._ordered_keys[loc]):
			return self._left_value
		else:
			if loc < len(self._ordered_keys) and key == self._ordered_keys[loc]:
				return self._key_mapping[self._ordered_keys[loc]]
			else:
				return self._key_mapping[self._ordered_keys[loc-1]]

	def get(self, key, restval=None):
		value = self.__getitem(key)
		if value is _empty:
			return restval
		else:
			return value

	def get_range(self, start=None, stop=None):
		raise NotImplementedError('yet')

	def set(self, value, start=None, stop=None):
		if start is None:
			if stop is None:
				self._ordered_keys = []
				self._key_mapping = {}
			else:
				stop_index = bisect.bisect_left(self._ordered_keys, stop)
				self._key_mapping[stop] = self.__getitem(stop)
				_remove_all(self._key_mapping, self._ordered_keys[:stop_index])
				if stop_index < len(self._ordered_keys) and self._ordered_keys[stop_index] == stop:
					self._ordered_keys[:stop_index+1] = [stop]
				else:
					self._ordered_keys[:stop_index] = [stop]
			self._left_value = value
		else:
			start_index = bisect.bisect_left(self._ordered_keys, start)
			if stop is None:
				stop_index = len(self._ordered_keys)
				new_keys = [start]
			else:
				stop_index = bisect.bisect_left(self._ordered_keys, stop)
				self._key_mapping[stop] = self.__getitem(stop)
				new_keys = [start, stop]
			_remove_all(self._key_mapping, self._ordered_keys[start_index:stop_index])
			if stop_index < len(self._ordered_keys) and self._ordered_keys[stop_index] == stop:
				self._ordered_keys[start_index:stop_index+1] = new_keys
			else:
				self._ordered_keys[start_index:stop_index] = new_keys
			self._key_mapping[start] = value

	def delete(self, start=None, stop=None):
		self.set(_empty, start=start, stop=stop)

	def __eq__(self, other):
		if isinstance(other, RangeMap):
			return (
				self._key_mapping == other._key_mapping and
				self._left_value == other._left_value
				)
		else:
			return NotImplemented

	def __getitem__(self, key):
		if isinstance(key, slice):
			if key.step:
				raise ValueError('Steps aren\'t allowed')
			# return a RangeMap
			return self.get_range(key.start, key.stop)
		else:
			value = self.__getitem(key)
			if value is _empty:
				raise KeyError()
			else:
				return value

	def __setitem__(self, key, value):
		if not isinstance(key, slice):
			raise ValueError('Can only set slices')
		if key.step is not None:
			raise ValueError('Steps aren\'t allowed')
		self.set(value, key.start, key.stop)

	def __delitem__(self, index):
		if not isinstance(index, slice):
			raise ValueError('Can only delete slices')
		if index.step is not None:
			raise ValueError('Steps aren\'t allowed')
		self.delete(index.start, index.stop)

	# Python2 - override slice methods
	def __setslice__(self, i, j, value):
		'''Implement __setslice__ to override behavior in Python 2.
		This is required because empty slices pass integers in python2 as opposed
		to None in python 3.
		'''
		raise SyntaxError('Assigning slices doesn\t work in Python 2, use set')

	def __delslice__(self, i, j):
		raise SyntaxError('Deleting slices doesn\t work in Python 2, use delete')

	def __getslice__(self, i, j):
		raise SyntaxError('Getting slices doesn\t work in Python 2, use get_range.')
<|endoftext|>


<commit_before>#!/usr/bin/env python2
# -*- coding: utf-8 -*-
"""
Created on Fri May 26 12:35:55 2017

@author: Jessica
"""

import matplotlib.pyplot as plt
import numpy as np

import matplotlib.pyplot as plt
import numpy as np
import matplotlib.mlab as mlab
import math

mu = 0
mu2 = 0.5
mu3 = 0.75

variance = 0.5
variance2 = 1
variance3 = 1.5

sigma = math.sqrt(variance)
sigma2 = math.sqrt(variance2)
sigma3 = math.sqrt(variance3)

x = np.linspace(mu-3*variance,mu+3*variance, 40)
x2 = np.linspace(mu2-3*variance2, mu+3*variance2, 40)
x3 = np.linspace(mu2-3*variance3, mu+3*variance3, 40)

A = np.zeros((559,1))
A[20:60] = mlab.normpdf(x, mu, sigma).reshape(40,1)
A[230:270] = mlab.normpdf(x2, mu2, sigma2).reshape(40,1)
A[420:460] = mlab.normpdf(x3, mu3, sigma3).reshape(40,1)
A = A.reshape(559)

B = np.zeros((559,1))
B[23:63] = mlab.normpdf(x, mu, sigma).reshape(40,1)
B[400:440] =  mlab.normpdf(x3, mu3, sigma3).reshape(40,1)
B[470:510] = mlab.normpdf(x2, mu2, sigma2).reshape(40,1)
B = B.reshape(559)

C = np.zeros((559, 1))
C[320:360] = mlab.normpdf(x2, mu2, sigma2).reshape(40,1)
C[433:473] = mlab.normpdf(x, mu, sigma).reshape(40,1)
C[128:168] = mlab.normpdf(x3, mu3, sigma3).reshape(40,1)
C = C.reshape(559)

#initiate spectral and functional image
spectralmatrix = np.zeros((256, 256, 559))
functionalmatrix = np.zeros((256, 256))
xaxis = spectralmatrix.shape[0]
yaxis = spectralmatrix.shape[1]

#generate random coefficients

#need to create matrix with a spectrum at each point.

np.random.seed(122)
a=np.random.rand(1)
b=np.random.rand(1)
c=np.random.rand(1)
spatialfrequency = (2*np.pi)/64
for x in range(xaxis):
    for y in range(yaxis):
        a = abs(np.sin(y*spatialfrequency))
        b = abs(np.sin(x*spatialfrequency) + np.sin(y*spatialfrequency))
        c = np.sin(x*spatialfrequency)**2
    #can make a, b, c as a function of x and y with some random noise
        spectralmatrix[x][y] = a*A + b*B + c*C

#need to make a matrix that is a function of the spectral matrix, which
#should be relatively easy once the spectral matrix is done.

#run peak finding function on the spectra to identify peaks

#run Principal component analysis to find principal components

#run Linear Regression on Principal Components
<commit_msg>functional matrix added alongwith matrices that contain weights of the reference spectra<commit_after>#!/usr/bin/env python2
# -*- coding: utf-8 -*-
"""
Created on Fri May 26 12:35:55 2017

@author: Jessica
"""

import matplotlib.pyplot as plt
import numpy as np

import matplotlib.pyplot as plt
import numpy as np
import matplotlib.mlab as mlab
import math

mu = 0
mu2 = 0.5
mu3 = 0.75

variance = 0.5
variance2 = 1
variance3 = 1.5

sigma = math.sqrt(variance)
sigma2 = math.sqrt(variance2)
sigma3 = math.sqrt(variance3)

x = np.linspace(mu-3*variance,mu+3*variance, 40)
x2 = np.linspace(mu2-3*variance2, mu+3*variance2, 40)
x3 = np.linspace(mu2-3*variance3, mu+3*variance3, 40)

A = np.zeros((559,1))
A[20:60] = mlab.normpdf(x, mu, sigma).reshape(40,1)
A[230:270] = mlab.normpdf(x2, mu2, sigma2).reshape(40,1)
A[420:460] = mlab.normpdf(x3, mu3, sigma3).reshape(40,1)
A = A.reshape(559)

B = np.zeros((559,1))
B[23:63] = mlab.normpdf(x, mu, sigma).reshape(40,1)
B[400:440] =  mlab.normpdf(x3, mu3, sigma3).reshape(40,1)
B[470:510] = mlab.normpdf(x2, mu2, sigma2).reshape(40,1)
B = B.reshape(559)

C = np.zeros((559, 1))
C[320:360] = mlab.normpdf(x2, mu2, sigma2).reshape(40,1)
C[433:473] = mlab.normpdf(x, mu, sigma).reshape(40,1)
C[128:168] = mlab.normpdf(x3, mu3, sigma3).reshape(40,1)
C = C.reshape(559)

#initiate spectral and functional image
spectralmatrix = np.zeros((256, 256, 559))
functionalmatrix = np.zeros((256, 256))
Amatrix = np.zeros((256, 256))
Bmatrix = np.zeros((256, 256))
Cmatrix =np.zeros((256, 256))
xaxis = spectralmatrix.shape[0]
yaxis = spectralmatrix.shape[1]

#generate random coefficients

#need to create matrix with a spectrum at each point.

np.random.seed(122)
a=np.random.rand(1)
b=np.random.rand(1)
c=np.random.rand(1)
spatialfrequency = (2*np.pi)/64
for x in range(xaxis):
    for y in range(yaxis):
        a = abs(np.sin(y*spatialfrequency))
        b = abs(np.sin(x*spatialfrequency) + np.sin(y*spatialfrequency))
        c = np.sin(x*spatialfrequency)**2
    #can make a, b, c as a function of x and y with some random noise
        spectralmatrix[x][y] = a*A + b*B + c*C
        functionalmatrix[x][y] = 2*a + b + 9*c
        Amatrix[x][y] = a
        Bmatrix[x][y] = b
        Cmatrix[x][y] = c

#need to make a matrix that is a function of the spectral matrix, which
#should be relatively easy once the spectral matrix is done.

#run peak finding function on the spectra to identify peaks

#run Principal component analysis to find principal components

#run Linear Regression on Principal Components
<|endoftext|>


<commit_before>{
  'targets': [
    {
      'target_name': 'node_stringprep',
      'cflags_cc!': [ '-fno-exceptions', '-fmax-errors=0' ],
      'include_dirs': [
        '<!(node -e "require(\'nan\')")'
      ],
      'conditions': [
        ['OS=="win"', {
          'conditions': [
            ['"<!@(cmd /C where /Q icu-config || echo n)"!="n"', {
              'sources': [ 'node-stringprep.cc' ],
              'cflags!': [ '-fno-exceptions', '`icu-config --cppflags`' ],
              'libraries': [ '`icu-config --ldflags`' ]
            }]
          ]
        }, { # OS != win
          'conditions': [
            ['"<!@(which icu-config > /dev/null || echo n)"!="n"', {
              'sources': [ 'node-stringprep.cc' ],
              'cflags!': [ '-fno-exceptions', '-fmax-errors=0', '`icu-config --cppflags`' ],
              'libraries': [ '`icu-config --ldflags`' ],
              'conditions': [
                ['OS=="freebsd"', {
                  'include_dirs': [
                      '/usr/local/include'
                  ],
                }],
                ['OS=="mac"', {
                  'include_dirs': [
                      '/opt/local/include', '/usr/local/include'
                  ],
                  'xcode_settings': {
                    'GCC_ENABLE_CPP_EXCEPTIONS': 'YES'
                  }
                }]
              ]
            }]
          ]
        }]
      ]
     }
  ]
}
<commit_msg>Update libraries call<commit_after>{
  'targets': [
    {
      'target_name': 'node_stringprep',
      'cflags_cc!': [ '-fno-exceptions', '-fmax-errors=0' ],
      'include_dirs': [
        '<!(node -e "require(\'nan\')")'
      ],
      'conditions': [
        ['OS=="win"', {
          'conditions': [
            ['"<!@(cmd /C where /Q icu-config || echo n)"!="n"', {
              'sources': [ 'node-stringprep.cc' ],
              'cflags!': [ '-fno-exceptions', '`icu-config --cppflags`' ],
              'libraries': [ '`icu-config --ldflags`' ]
            }]
          ]
        }, { # OS != win
          'conditions': [
            ['"<!@(which icu-config > /dev/null || echo n)"!="n"', {
              'sources': [ 'node-stringprep.cc' ],
              'cflags!': [ '-fno-exceptions', '-fmax-errors=0', '`icu-config --cppflags`' ],
              'libraries': [ '-Wl,-z,relro -Wl,-z,now  -ldl -lm   -L/usr/lib/x86_64-linux-gnu -licui18n -licuuc -licudata  -ldl -lm' ],
              'conditions': [
                ['OS=="freebsd"', {
                  'include_dirs': [
                      '/usr/local/include'
                  ],
                }],
                ['OS=="mac"', {
                  'include_dirs': [
                      '/opt/local/include', '/usr/local/include'
                  ],
                  'xcode_settings': {
                    'GCC_ENABLE_CPP_EXCEPTIONS': 'YES'
                  }
                }]
              ]
            }]
          ]
        }]
      ]
     }
  ]
}
<|endoftext|>


<commit_before>from django.conf import settings
from os import path

class XmlJsonImportModuleException(Exception):
    pass

if not hasattr(settings, 'XSLT_FILES_DIR'):
    raise XmlJsonImportModuleException('Settings must contain XSLT_FILES_DIR parameter')

if not path.exists(settings.XSLT_FILES_DIR):
    raise XmlJsonImportModuleException('Directory specified by XSLT_FILES_DIR does not exist')
<commit_msg>Add exception handling for invalid XSLT files<commit_after>from django.conf import settings
from os import path, listdir
from lxml import etree

class XmlJsonImportModuleException(Exception):
    pass

if not hasattr(settings, 'XSLT_FILES_DIR'):
    raise XmlJsonImportModuleException('Settings must contain XSLT_FILES_DIR parameter')

if not path.exists(settings.XSLT_FILES_DIR):
    raise XmlJsonImportModuleException('Directory specified by XSLT_FILES_DIR does not exist')

for filename in listdir(settings.XSLT_FILES_DIR):
    filepath = path.join(settings.XSLT_FILES_DIR, filename)
    if path.isfile(filepath):
        try:
            xslt_etree = etree.parse(filepath)
        except etree.XMLSyntaxError as er:
            raise XmlJsonImportModuleException('File ' + filepath + ' is not a valid XML file: ' + str(er))
        try:
            transform = etree.XSLT(xslt_etree)
        except etree.XSLTParseError as er:
            raise XmlJsonImportModuleException('File ' + filepath + ' is not a valid XSLT file: ' + str(er))
<|endoftext|>


<commit_before><commit_msg>Signature error reporting<commit_after><|endoftext|>


<commit_before>#!/usr/bin/python
import sys
import os
import subprocess
import shutil
import platform

def usage():
    print("Usage:")
    print("  RemoveModule.py NAME")
    sys.exit(1)

def RemoveMirroredDirectory(moduleDir, omiDir):
    if len(moduleDir) < 15 or len(omiDir) < 10:
        print("Error: The parameters to RemoveMirroredDirectory are too short.  This function can do dangerous things, so this is a sanity check.")
        sys.exit(1)

    for curFile in os.listdir(moduleDir):
        if os.path.islink(moduleDir + "/" + curFile):
            os.path.unlink(moduleDir + "/" + curFile)
        elif os.path.isfile(moduleDir + "/" + curFile):
            os.remove(moduleDir + "/" + curFile)
        elif os.path.isdir(moduleDir + "/" + curFile):
            RemoveMirroredDirectory(moduleDir + "/" + curFile, omiDir + "/" + curFile)
            os.rmdir(moduleDir + "/" + curFile)
            if len(os.listdir(omiDir + "/" + curFile)) == 0:
                os.rmdir(omiDir + "/" + curFile)

        if os.path.islink(omiDir + "/" + curFile):
            os.path.unlink(omiDir + "/" + curFile)
        elif os.path.isfile(omiDir + "/" + curFile):
            os.remove(omiDir + "/" + curFile)

if len(sys.argv) != 2:
    usage()

moduleName = sys.argv[1]
omi_bindir = "<CONFIG_BINDIR>"
omi_libdir = "<CONFIG_LIBDIR>"
omi_sysconfdir = "<CONFIG_SYSCONFDIR>"
baseModulePath = "<DSC_MODULES_PATH>"
baseScriptPath = "<DSC_SCRIPT_PATH>"
modulePath = baseModulePath + "/" + moduleName
omiregister_dir = "/etc/opt/omi/conf/omiregister"

if not os.path.isdir(modulePath):
    print("Error: unable to find installed module " + moduleName + " at " + modulePath)
    sys.exit(1)

print("Removing module " + moduleName)

# special section for nxOMSAutomationWorker module
# the Linux Hybrid worker needs to be killed before the module is removed
# also a good idea to remove the state.conf file
# manually invoking RemoveModule.py will preserve worker.conf file in case the user wants to keep their settings
# an uninstall or purge operation of the OMS agent installer will get rid of worker.conf file
if moduleName == "nxOMSAutomationWorker":
    registration_file_path = "/opt/microsoft/omsconfig/modules/nxOMSAutomationWorker/DSCResources/MSFT_nxOMSAutomationWorkerResource/automationworker/scripts/register_oms.py"
    state_conf_location = "/var/opt/microsoft/omsagent/state/automationworker/state.conf"
    config_state_section = "state"
    config_pid_key = "pid"
    workspace_id_key = "workspace_id"
    automation_user = "nxautomation"
    import signal
    try:
        import ConfigParser
    except ImportError:
        # ConfigParser is named configparser in python 3x
        import configparser as ConfigParser

    # invoke deregister when it becomes available

    if os.path.isfile(state_conf_location):
        state_conf = ConfigParser.ConfigParser()
        try:
            # try to read the PID of the worker and make a best effort attempt to kill it
            # don't fail uninstall if it cannot be killed
            state_conf.read(state_conf_location)
            worker_process_id = state_conf.get(config_state_section, config_pid_key)
            workspace_id = state_conf.get(config_state_section, workspace_id_key)
            os.kill(int(worker_process_id), signal.SIGTERM)
            # Typicallly the above kill should be sufficient becasue we should never reach as state where there are more
            # than 1 workers. Since we introduced a new user to exclusively for worker, we can safely kill all processes
            # that user which were started with the workspace id for additional safety
            subprocess.call(["sudo", "pkill", "-u", automation_user, "--full", workspace_id])
        except ConfigParser.NoSectionError:
            pass
        except ConfigParser.NoOptionError:
            pass
        except OSError:
            pass
        finally:
            try:
                os.remove(state_conf_location)
            except:
                pass

resourcelist = os.listdir(modulePath + "/DSCResources")
for resource in resourcelist:
    print("Removing resource " + resource)
    # Remove omi registration for each class
    namespace_dir = "<DSC_NAMESPACE>".replace("/","-")
    os.remove(omiregister_dir + "/" + namespace_dir + "/" + resource + ".reg")

    # Remove schema/registration for each class
    schemadir = omi_sysconfdir + "/<CONFIG_SYSCONFDIR_DSC>/configuration/schema"
    regdir = omi_sysconfdir + "/<CONFIG_SYSCONFDIR_DSC>/configuration/registration"
    if os.path.isdir(schemadir + "/" + resource):
        shutil.rmtree(schemadir + "/" + resource)
    if os.path.isdir(regdir + "/" + resource):
        shutil.rmtree(regdir + "/" + resource)

    if os.path.isfile(modulePath + "/DSCResources/" + resource + "/" + resource + ".schema.mof"):
        os.remove(modulePath + "/DSCResources/" + resource + "/" + resource + ".schema.mof")

    # Remove all files in libdir (lib for x86, lib64 for x64) for each class
    libdir = "x86"
    arch = platform.architecture()
    if len(arch) != 2:
        print("Error: The python function platform.architecture() failed to return a valid tuple. Cannot detect if this system has x64 or x86 architecture.")
        sys.exit(1)
    if (arch[0] == "64bit"):
        libdir = "x64"

    libdirPath = modulePath + "/DSCResources/" + resource + "/" + libdir
    if not os.path.isdir(libdirPath):
        print("Error: Unable to find directory in module at " + libdirPath + ", unable to remove module.")
        sys.exit(1)

    # In libdirPath, there's .so file(s) and a Script directory.  We want to remove the .so from its omi lib path, and the Scripts inside the Script directory
    filelist = os.listdir(libdirPath)
    for f in filelist:
        if not os.path.isdir(libdirPath + "/" + f):
            os.remove(omi_libdir + "/" + f)
            os.remove(libdirPath + "/" + f)

    # Now remove the scripts from their installed location
    RemoveMirroredDirectory(libdirPath + "/Scripts", baseScriptPath)
    if os.path.isdir(modulePath + "/DSCResources/" + resource + "/x86"):
        shutil.rmtree(modulePath + "/DSCResources/" + resource + "/x86")
    if os.path.isdir(modulePath + "/DSCResources/" + resource + "/x64"):
        shutil.rmtree(modulePath + "/DSCResources/" + resource + "/x64")

shutil.rmtree(modulePath)

retval = subprocess.call([baseScriptPath + "/RegenerateInitFiles.py"])
if retval != 0:
    print("Error: failed to regenerate init files.")
sys.exit(0)

<commit_msg>Removed Finally clause, not supported in python 2.4<commit_after>#!/usr/bin/python
import sys
import os
import subprocess
import shutil
import platform

def usage():
    print("Usage:")
    print("  RemoveModule.py NAME")
    sys.exit(1)

def RemoveMirroredDirectory(moduleDir, omiDir):
    if len(moduleDir) < 15 or len(omiDir) < 10:
        print("Error: The parameters to RemoveMirroredDirectory are too short.  This function can do dangerous things, so this is a sanity check.")
        sys.exit(1)

    for curFile in os.listdir(moduleDir):
        if os.path.islink(moduleDir + "/" + curFile):
            os.path.unlink(moduleDir + "/" + curFile)
        elif os.path.isfile(moduleDir + "/" + curFile):
            os.remove(moduleDir + "/" + curFile)
        elif os.path.isdir(moduleDir + "/" + curFile):
            RemoveMirroredDirectory(moduleDir + "/" + curFile, omiDir + "/" + curFile)
            os.rmdir(moduleDir + "/" + curFile)
            if len(os.listdir(omiDir + "/" + curFile)) == 0:
                os.rmdir(omiDir + "/" + curFile)

        if os.path.islink(omiDir + "/" + curFile):
            os.path.unlink(omiDir + "/" + curFile)
        elif os.path.isfile(omiDir + "/" + curFile):
            os.remove(omiDir + "/" + curFile)

if len(sys.argv) != 2:
    usage()

moduleName = sys.argv[1]
omi_bindir = "<CONFIG_BINDIR>"
omi_libdir = "<CONFIG_LIBDIR>"
omi_sysconfdir = "<CONFIG_SYSCONFDIR>"
baseModulePath = "<DSC_MODULES_PATH>"
baseScriptPath = "<DSC_SCRIPT_PATH>"
modulePath = baseModulePath + "/" + moduleName
omiregister_dir = "/etc/opt/omi/conf/omiregister"

if not os.path.isdir(modulePath):
    print("Error: unable to find installed module " + moduleName + " at " + modulePath)
    sys.exit(1)

print("Removing module " + moduleName)

# special section for nxOMSAutomationWorker module
# the Linux Hybrid worker needs to be killed before the module is removed
# also a good idea to remove the state.conf file
# manually invoking RemoveModule.py will preserve worker.conf file in case the user wants to keep their settings
# an uninstall or purge operation of the OMS agent installer will get rid of worker.conf file
if moduleName == "nxOMSAutomationWorker":
    registration_file_path = "/opt/microsoft/omsconfig/modules/nxOMSAutomationWorker/DSCResources/MSFT_nxOMSAutomationWorkerResource/automationworker/scripts/register_oms.py"
    state_conf_location = "/var/opt/microsoft/omsagent/state/automationworker/state.conf"
    config_state_section = "state"
    config_pid_key = "pid"
    workspace_id_key = "workspace_id"
    automation_user = "nxautomation"
    import signal
    try:
        import ConfigParser
    except ImportError:
        # ConfigParser is named configparser in python 3x
        import configparser as ConfigParser

    # invoke deregister when it becomes available

    if os.path.isfile(state_conf_location):
        state_conf = ConfigParser.ConfigParser()
        try:
            # try to read the PID of the worker and make a best effort attempt to kill it
            # don't fail uninstall if it cannot be killed
            state_conf.read(state_conf_location)
            worker_process_id = state_conf.get(config_state_section, config_pid_key)
            workspace_id = state_conf.get(config_state_section, workspace_id_key)
            os.kill(int(worker_process_id), signal.SIGTERM)
            # Typicallly the above kill should be sufficient becasue we should never reach as state where there are more
            # than 1 workers. Since we introduced a new user to exclusively for worker, we can safely kill all processes
            # that user which were started with the workspace id for additional safety
            subprocess.call(["sudo", "pkill", "-u", automation_user, "--full", workspace_id])
        except ConfigParser.NoSectionError:
            pass
        except ConfigParser.NoOptionError:
            pass
        except OSError:
            pass
        try:
            os.remove(state_conf_location)
        except:
            pass

resourcelist = os.listdir(modulePath + "/DSCResources")
for resource in resourcelist:
    print("Removing resource " + resource)
    # Remove omi registration for each class
    namespace_dir = "<DSC_NAMESPACE>".replace("/","-")
    os.remove(omiregister_dir + "/" + namespace_dir + "/" + resource + ".reg")

    # Remove schema/registration for each class
    schemadir = omi_sysconfdir + "/<CONFIG_SYSCONFDIR_DSC>/configuration/schema"
    regdir = omi_sysconfdir + "/<CONFIG_SYSCONFDIR_DSC>/configuration/registration"
    if os.path.isdir(schemadir + "/" + resource):
        shutil.rmtree(schemadir + "/" + resource)
    if os.path.isdir(regdir + "/" + resource):
        shutil.rmtree(regdir + "/" + resource)

    if os.path.isfile(modulePath + "/DSCResources/" + resource + "/" + resource + ".schema.mof"):
        os.remove(modulePath + "/DSCResources/" + resource + "/" + resource + ".schema.mof")

    # Remove all files in libdir (lib for x86, lib64 for x64) for each class
    libdir = "x86"
    arch = platform.architecture()
    if len(arch) != 2:
        print("Error: The python function platform.architecture() failed to return a valid tuple. Cannot detect if this system has x64 or x86 architecture.")
        sys.exit(1)
    if (arch[0] == "64bit"):
        libdir = "x64"

    libdirPath = modulePath + "/DSCResources/" + resource + "/" + libdir
    if not os.path.isdir(libdirPath):
        print("Error: Unable to find directory in module at " + libdirPath + ", unable to remove module.")
        sys.exit(1)

    # In libdirPath, there's .so file(s) and a Script directory.  We want to remove the .so from its omi lib path, and the Scripts inside the Script directory
    filelist = os.listdir(libdirPath)
    for f in filelist:
        if not os.path.isdir(libdirPath + "/" + f):
            os.remove(omi_libdir + "/" + f)
            os.remove(libdirPath + "/" + f)

    # Now remove the scripts from their installed location
    RemoveMirroredDirectory(libdirPath + "/Scripts", baseScriptPath)
    if os.path.isdir(modulePath + "/DSCResources/" + resource + "/x86"):
        shutil.rmtree(modulePath + "/DSCResources/" + resource + "/x86")
    if os.path.isdir(modulePath + "/DSCResources/" + resource + "/x64"):
        shutil.rmtree(modulePath + "/DSCResources/" + resource + "/x64")

shutil.rmtree(modulePath)

retval = subprocess.call([baseScriptPath + "/RegenerateInitFiles.py"])
if retval != 0:
    print("Error: failed to regenerate init files.")
sys.exit(0)

<|endoftext|>


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

"""
Rec. 2020 Colourspace
=====================

Defines the *Rec. 2020* colourspace:

-   :attr:`REC_2020_COLOURSPACE`.

See Also
--------
`RGB Colourspaces IPython Notebook
<http://nbviewer.ipython.org/github/colour-science/colour-ipython/blob/master/notebooks/models/rgb.ipynb>`_  # noqa

References
----------
.. [1]  `Recommendation ITU-R BT.2020 <http://www.itu.int/rec/R-REC-BT.2020/en>`_
        (Last accessed 13 April 2014)
"""

from __future__ import division, unicode_literals

import numpy as np

from colour.colorimetry import ILLUMINANTS
from colour.models import RGB_Colourspace, normalised_primary_matrix
from colour.utilities import Structure

__author__ = 'Colour Developers'
__copyright__ = 'Copyright (C) 2013 - 2014 - Colour Developers'
__license__ = 'New BSD License - http://opensource.org/licenses/BSD-3-Clause'
__maintainer__ = 'Colour Developers'
__email__ = 'colour-science@googlegroups.com'
__status__ = 'Production'

__all__ = ['REC_2020_PRIMARIES',
           'REC_2020_WHITEPOINT',
           'REC_2020_TO_XYZ_MATRIX',
           'XYZ_TO_REC_2020_MATRIX',
           'REC_2020_CONSTANTS',
           'REC_2020_TRANSFER_FUNCTION',
           'REC_2020_INVERSE_TRANSFER_FUNCTION',
           'REC_2020_COLOURSPACE']

REC_2020_PRIMARIES = np.array(
    [[0.708, 0.292],
     [0.170, 0.797],
     [0.131, 0.046]])
"""
*Rec. 2020* colourspace primaries.

REC_2020_PRIMARIES : ndarray, (3, 2)
"""

REC_2020_WHITEPOINT = ILLUMINANTS.get(
    'CIE 1931 2 Degree Standard Observer').get('D65')
"""
*Rec. 2020* colourspace whitepoint.

REC_2020_WHITEPOINT : tuple
"""

REC_2020_TO_XYZ_MATRIX = normalised_primary_matrix(REC_2020_PRIMARIES,
                                                   REC_2020_WHITEPOINT)
"""
*Rec. 2020* colourspace to *CIE XYZ* colourspace matrix.

REC_2020_TO_XYZ_MATRIX : array_like, (3, 3)
"""

XYZ_TO_REC_2020_MATRIX = np.linalg.inv(REC_2020_TO_XYZ_MATRIX)
"""
*CIE XYZ* colourspace to *Rec. 2020* colourspace matrix.

XYZ_TO_REC_2020_MATRIX : array_like, (3, 3)
"""

REC_2020_CONSTANTS = Structure(alpha=lambda x: 1.099 if x else 1.0993,
                               beta=lambda x: 0.018 if x else 0.0181)
"""
*CIE XYZ* constants.

REC_2020_CONSTANTS : Structure
"""

def _rec_2020_transfer_function(value, is_10_bits_system=True):
    """
    Defines the *Rec. 2020* colourspace transfer function.

    Parameters
    ----------
    value : numeric
        value.
    is_10_bits_system : bool
        *Rec. 709* *alpha* and *beta* constants are used if system is 10 bit.

    Returns
    -------
    :rtype: numeric
    :return: Companded value.

    References
    ----------
    .. [2]  `Recommendation ITU-R BT.2020: Signal Format
            <http://www.itu.int/rec/R-REC-BT.2020/en>`_
            (Last accessed 13 April 2014)
    """

    return (value * 4.5
            if value < REC_2020_CONSTANTS.beta(is_10_bits_system) else
            REC_2020_CONSTANTS.alpha(is_10_bits_system) *
            (value ** 0.45) -
            (REC_2020_CONSTANTS.alpha(is_10_bits_system) - 1))

def _rec_2020_inverse_transfer_function(value, is_10_bits_system=True):
    """
    Defines the *Rec. 2020* colourspace inverse transfer function.

    Parameters
    ----------
    value : numeric
        value.
    is_10_bits_system : bool
        *Rec. 709* *alpha* and *beta* constants are used if system is 10 bit.

    Returns
    -------
    :rtype: numeric
    :return: Companded value.

    References
    ----------
    .. [3]  `Recommendation ITU-R BT.2020: Signal Format
            <http://www.itu.int/rec/R-REC-BT.2020/en>`_
            (Last accessed 13 April 2014)
    """

    return (value / 4.5
            if value < REC_2020_CONSTANTS.beta(is_10_bits_system) else
            ((value + (REC_2020_CONSTANTS.alpha(is_10_bits_system) - 1)) /
             REC_2020_CONSTANTS.alpha(is_10_bits_system)) ** (1 / 0.45))

REC_2020_TRANSFER_FUNCTION = _rec_2020_transfer_function
"""
Transfer function from linear to *Rec. 2020* colourspace.

REC_2020_TRANSFER_FUNCTION : object
"""

REC_2020_INVERSE_TRANSFER_FUNCTION = _rec_2020_inverse_transfer_function
"""
Inverse transfer function from *Rec. 2020* colourspace to linear.

REC_2020_INVERSE_TRANSFER_FUNCTION : object
"""

REC_2020_COLOURSPACE = RGB_Colourspace(
    'Rec. 2020',
    REC_2020_PRIMARIES,
    REC_2020_WHITEPOINT,
    REC_2020_TO_XYZ_MATRIX,
    XYZ_TO_REC_2020_MATRIX,
    REC_2020_TRANSFER_FUNCTION,
    REC_2020_INVERSE_TRANSFER_FUNCTION)
"""
*Rec. 2020* colourspace.

REC_2020_COLOURSPACE : RGB_Colourspace
"""
<commit_msg>Update "Rec. 2020" colourspace transfer functions.<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
Rec. 2020 Colourspace
=====================

Defines the *Rec. 2020* colourspace:

-   :attr:`REC_2020_COLOURSPACE`.

See Also
--------
`RGB Colourspaces IPython Notebook
<http://nbviewer.ipython.org/github/colour-science/colour-ipython/blob/master/notebooks/models/rgb.ipynb>`_  # noqa

References
----------
.. [1]  `Recommendation ITU-R BT.2020 <http://www.itu.int/rec/R-REC-BT.2020/en>`_
        (Last accessed 2 September 2014)
"""

from __future__ import division, unicode_literals

import numpy as np

from colour.colorimetry import ILLUMINANTS
from colour.models import RGB_Colourspace, normalised_primary_matrix
from colour.utilities import Structure

__author__ = 'Colour Developers'
__copyright__ = 'Copyright (C) 2013 - 2014 - Colour Developers'
__license__ = 'New BSD License - http://opensource.org/licenses/BSD-3-Clause'
__maintainer__ = 'Colour Developers'
__email__ = 'colour-science@googlegroups.com'
__status__ = 'Production'

__all__ = ['REC_2020_PRIMARIES',
           'REC_2020_WHITEPOINT',
           'REC_2020_TO_XYZ_MATRIX',
           'XYZ_TO_REC_2020_MATRIX',
           'REC_2020_CONSTANTS',
           'REC_2020_TRANSFER_FUNCTION',
           'REC_2020_INVERSE_TRANSFER_FUNCTION',
           'REC_2020_COLOURSPACE']

REC_2020_PRIMARIES = np.array(
    [[0.708, 0.292],
     [0.170, 0.797],
     [0.131, 0.046]])
"""
*Rec. 2020* colourspace primaries.

REC_2020_PRIMARIES : ndarray, (3, 2)
"""

REC_2020_WHITEPOINT = ILLUMINANTS.get(
    'CIE 1931 2 Degree Standard Observer').get('D65')
"""
*Rec. 2020* colourspace whitepoint.

REC_2020_WHITEPOINT : tuple
"""

REC_2020_TO_XYZ_MATRIX = normalised_primary_matrix(REC_2020_PRIMARIES,
                                                   REC_2020_WHITEPOINT)
"""
*Rec. 2020* colourspace to *CIE XYZ* colourspace matrix.

REC_2020_TO_XYZ_MATRIX : array_like, (3, 3)
"""

XYZ_TO_REC_2020_MATRIX = np.linalg.inv(REC_2020_TO_XYZ_MATRIX)
"""
*CIE XYZ* colourspace to *Rec. 2020* colourspace matrix.

XYZ_TO_REC_2020_MATRIX : array_like, (3, 3)
"""

REC_2020_CONSTANTS = Structure(alpha=lambda x: 1.099 if x else 1.0993,
                               beta=lambda x: 0.018 if x else 0.0181)
"""
*CIE XYZ* constants.

REC_2020_CONSTANTS : Structure
"""

def _rec_2020_transfer_function(value, is_10_bits_system=True):
    """
    Defines the *Rec. 2020* colourspace transfer function.

    Parameters
    ----------
    value : numeric
        value.
    is_10_bits_system : bool
        *Rec. 709* *alpha* and *beta* constants are used if system is 10 bit.

    Returns
    -------
    :rtype: numeric
    :return: Companded value.

    References
    ----------
    .. [2]  `Recommendation ITU-R BT.2020: Signal Format
            <http://www.itu.int/rec/R-REC-BT.2020/en>`_
            (Last accessed 2 September 2014)
    """

    a = REC_2020_CONSTANTS.alpha(is_10_bits_system)
    b = REC_2020_CONSTANTS.beta(is_10_bits_system)
    return (value * 4.5
            if value < b else
            a * (value ** 0.45) - (a - 1))

def _rec_2020_inverse_transfer_function(value, is_10_bits_system=True):
    """
    Defines the *Rec. 2020* colourspace inverse transfer function.

    Parameters
    ----------
    value : numeric
        value.
    is_10_bits_system : bool
        *Rec. 709* *alpha* and *beta* constants are used if system is 10 bit.

    Returns
    -------
    :rtype: numeric
    :return: Companded value.

    References
    ----------
    .. [3]  `Recommendation ITU-R BT.2020: Signal Format
            <http://www.itu.int/rec/R-REC-BT.2020/en>`_
            (Last accessed 2 September 2014)
    """

    a = REC_2020_CONSTANTS.alpha(is_10_bits_system)
    b = REC_2020_CONSTANTS.beta(is_10_bits_system)
    return (value / 4.5
            if value < b else
            ((value + (a - 1)) / a) ** (1 / 0.45))

REC_2020_TRANSFER_FUNCTION = _rec_2020_transfer_function
"""
Transfer function from linear to *Rec. 2020* colourspace.

REC_2020_TRANSFER_FUNCTION : object
"""

REC_2020_INVERSE_TRANSFER_FUNCTION = _rec_2020_inverse_transfer_function
"""
Inverse transfer function from *Rec. 2020* colourspace to linear.

REC_2020_INVERSE_TRANSFER_FUNCTION : object
"""

REC_2020_COLOURSPACE = RGB_Colourspace(
    'Rec. 2020',
    REC_2020_PRIMARIES,
    REC_2020_WHITEPOINT,
    REC_2020_TO_XYZ_MATRIX,
    XYZ_TO_REC_2020_MATRIX,
    REC_2020_TRANSFER_FUNCTION,
    REC_2020_INVERSE_TRANSFER_FUNCTION)
"""
*Rec. 2020* colourspace.

REC_2020_COLOURSPACE : RGB_Colourspace
"""
<|endoftext|>


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

"""
Sony Colourspaces
=================

Defines the *S-Gamut*, *S-Gamut3* and *S-Gamut3.Cine* colourspaces:

-   :attr:`S_GAMUT_COLOURSPACE`.
-   :attr:`S_GAMUT3_COLOURSPACE`.
-   :attr:`S_GAMUT3_CINE_COLOURSPACE`.

See Also
--------
`RGB Colourspaces Jupyter Notebook
<http://nbviewer.jupyter.org/github/colour-science/colour-notebooks/\
blob/master/notebooks/models/rgb.ipynb>`_

References
----------
.. [1]  Gaggioni, H., Dhanendra, P., Yamashita, J., Kawada, N., Endo, K., &
        Clark, C. (n.d.). S-Log: A new LUT for digital production mastering
        and interchange applications. Retrieved from
        http://pro.sony.com/bbsccms/assets/files/mkt/cinema/solutions/\
slog_manual.pdf
.. [2]  Sony Corporation. (n.d.). S-Log Whitepaper. Retrieved from
        http://www.theodoropoulos.info/attachments/076_on S-Log.pdf
.. [3]  Sony Corporation. (n.d.). Technical Summary for
        S-Gamut3.Cine/S-Log3 and S-Gamut3/S-Log3. Retrieved from
        http://community.sony.com/sony/attachments/sony/\
large-sensor-camera-F5-F55/12359/2/\
TechnicalSummary_for_S-Gamut3Cine_S-Gamut3_S-Log3_V1_00.pdf
"""

from __future__ import division, unicode_literals

import numpy as np

from colour.colorimetry import ILLUMINANTS
from colour.models.rgb import (
    RGB_Colourspace,
    log_encoding_SLog2,
    log_decoding_SLog2,
    log_encoding_SLog3,
    log_decoding_SLog3,
    normalised_primary_matrix)

__author__ = 'Colour Developers'
__copyright__ = 'Copyright (C) 2013-2016 - Colour Developers'
__license__ = 'New BSD License - http://opensource.org/licenses/BSD-3-Clause'
__maintainer__ = 'Colour Developers'
__email__ = 'colour-science@googlegroups.com'
__status__ = 'Production'

__all__ = ['S_GAMUT_PRIMARIES',
           'S_GAMUT_ILLUMINANT',
           'S_GAMUT_WHITEPOINT',
           'S_GAMUT_TO_XYZ_MATRIX',
           'XYZ_TO_S_GAMUT_MATRIX',
           'S_GAMUT_COLOURSPACE',
           'S_GAMUT3_COLOURSPACE',
           'S_GAMUT3_CINE_PRIMARIES',
           'S_GAMUT3_CINE_ILLUMINANT',
           'S_GAMUT3_CINE_WHITEPOINT',
           'S_GAMUT3_CINE_TO_XYZ_MATRIX',
           'XYZ_TO_S_GAMUT3_CINE_MATRIX',
           'S_GAMUT3_CINE_COLOURSPACE']

S_GAMUT_PRIMARIES = np.array(
    [[0.730, 0.280],
     [0.140, 0.855],
     [0.100, -0.050]])
"""
*S-Gamut* colourspace primaries.

S_GAMUT_PRIMARIES : ndarray, (3, 2)
"""

S_GAMUT_ILLUMINANT = 'D65'
"""
*S-Gamut* colourspace whitepoint name as illuminant.

S_GAMUT_ILLUMINANT : unicode
"""

S_GAMUT_WHITEPOINT = ILLUMINANTS.get(
    'CIE 1931 2 Degree Standard Observer').get(S_GAMUT_ILLUMINANT)
"""
*S-Gamut* colourspace whitepoint.

S_GAMUT_WHITEPOINT : tuple
"""

S_GAMUT_TO_XYZ_MATRIX = normalised_primary_matrix(S_GAMUT_PRIMARIES,
                                                  S_GAMUT_WHITEPOINT)
"""
*S-Gamut* colourspace to *CIE XYZ* tristimulus values matrix.

S_GAMUT_TO_XYZ_MATRIX : array_like, (3, 3)
"""

XYZ_TO_S_GAMUT_MATRIX = np.linalg.inv(S_GAMUT_TO_XYZ_MATRIX)
"""
*CIE XYZ* tristimulus values to *S-Gamut* colourspace matrix.

XYZ_TO_S_GAMUT_MATRIX : array_like, (3, 3)
"""

S_GAMUT_COLOURSPACE = RGB_Colourspace(
    'S-Gamut',
    S_GAMUT_PRIMARIES,
    S_GAMUT_WHITEPOINT,
    S_GAMUT_ILLUMINANT,
    S_GAMUT_TO_XYZ_MATRIX,
    XYZ_TO_S_GAMUT_MATRIX,
    log_encoding_SLog2,
    log_decoding_SLog2)
"""
*S-Gamut* colourspace.

S_GAMUT_COLOURSPACE : RGB_Colourspace
"""

S_GAMUT3_COLOURSPACE = RGB_Colourspace(
    'S-Gamut3',
    S_GAMUT_PRIMARIES,
    S_GAMUT_WHITEPOINT,
    S_GAMUT_ILLUMINANT,
    S_GAMUT_TO_XYZ_MATRIX,
    XYZ_TO_S_GAMUT_MATRIX,
    log_encoding_SLog3,
    log_decoding_SLog3)
"""
*S-Gamut3* colourspace.

S_GAMUT3_COLOURSPACE : RGB_Colourspace
"""

S_GAMUT3_CINE_PRIMARIES = np.array(
    [[0.76600, 0.27500],
     [0.22500, 0.80000],
     [0.08900, -0.08700]])
"""
*S-Gamut3.Cine* colourspace primaries.

S_GAMUT_PRIMARIES : ndarray, (3, 2)
"""

S_GAMUT3_CINE_ILLUMINANT = S_GAMUT_ILLUMINANT
"""
*S-Gamut3.Cine* colourspace whitepoint name as illuminant.

S_GAMUT3_CINE_ILLUMINANT : unicode
"""

S_GAMUT3_CINE_WHITEPOINT = S_GAMUT_WHITEPOINT
"""
*S-Gamut3.Cine* colourspace whitepoint.

S_GAMUT3_CINE_WHITEPOINT : tuple
"""

S_GAMUT3_CINE_TO_XYZ_MATRIX = normalised_primary_matrix(
    S_GAMUT3_CINE_PRIMARIES,
    S_GAMUT3_CINE_WHITEPOINT)
"""
*S-Gamut3.Cine* colourspace to *CIE XYZ* tristimulus values matrix.

S_GAMUT3_CINE_TO_XYZ_MATRIX : array_like, (3, 3)
"""

XYZ_TO_S_GAMUT3_CINE_MATRIX = np.linalg.inv(S_GAMUT3_CINE_TO_XYZ_MATRIX)
"""
*CIE XYZ* tristimulus values to *S-Gamut3.Cine* colourspace matrix.

XYZ_TO_S_GAMUT3_CINE_MATRIX : array_like, (3, 3)
"""

S_GAMUT3_CINE_COLOURSPACE = RGB_Colourspace(
    'S-Gamut3.Cine',
    S_GAMUT3_CINE_PRIMARIES,
    S_GAMUT3_CINE_WHITEPOINT,
    S_GAMUT3_CINE_ILLUMINANT,
    S_GAMUT3_CINE_TO_XYZ_MATRIX,
    XYZ_TO_S_GAMUT3_CINE_MATRIX,
    log_encoding_SLog3,
    log_decoding_SLog3)
"""
*S-Gamut3.Cine* colourspace.

S_GAMUT3_CINE_COLOURSPACE : RGB_Colourspace
"""
<commit_msg>Add "S-Gamut3" and "S-Gamut3.Cine" transformation matrices as per "Sony Corporation" data.<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
Sony Colourspaces
=================

Defines the *S-Gamut*, *S-Gamut3* and *S-Gamut3.Cine* colourspaces:

-   :attr:`S_GAMUT_COLOURSPACE`.
-   :attr:`S_GAMUT3_COLOURSPACE`.
-   :attr:`S_GAMUT3_CINE_COLOURSPACE`.

See Also
--------
`RGB Colourspaces Jupyter Notebook
<http://nbviewer.jupyter.org/github/colour-science/colour-notebooks/\
blob/master/notebooks/models/rgb.ipynb>`_

References
----------
.. [1]  Gaggioni, H., Dhanendra, P., Yamashita, J., Kawada, N., Endo, K., &
        Clark, C. (n.d.). S-Log: A new LUT for digital production mastering
        and interchange applications. Retrieved from
        http://pro.sony.com/bbsccms/assets/files/mkt/cinema/solutions/\
slog_manual.pdf
.. [2]  Sony Corporation. (n.d.). S-Log Whitepaper. Retrieved from
        http://www.theodoropoulos.info/attachments/076_on S-Log.pdf
.. [3]  Sony Corporation. (n.d.). Technical Summary for
        S-Gamut3.Cine/S-Log3 and S-Gamut3/S-Log3. Retrieved from
        http://community.sony.com/sony/attachments/sony/\
large-sensor-camera-F5-F55/12359/2/\
TechnicalSummary_for_S-Gamut3Cine_S-Gamut3_S-Log3_V1_00.pdf
.. [4]  Sony Corporation. (n.d.). S-Gamut3_S-Gamut3Cine_Matrix.xlsx.
        Retrieved from https://community.sony.com/sony/attachments/sony/\
large-sensor-camera-F5-F55/12359/3/S-Gamut3_S-Gamut3Cine_Matrix.xlsx

"""

from __future__ import division, unicode_literals

import numpy as np

from colour.colorimetry import ILLUMINANTS
from colour.models.rgb import (
    RGB_Colourspace,
    log_encoding_SLog2,
    log_decoding_SLog2,
    log_encoding_SLog3,
    log_decoding_SLog3)

__author__ = 'Colour Developers'
__copyright__ = 'Copyright (C) 2013-2016 - Colour Developers'
__license__ = 'New BSD License - http://opensource.org/licenses/BSD-3-Clause'
__maintainer__ = 'Colour Developers'
__email__ = 'colour-science@googlegroups.com'
__status__ = 'Production'

__all__ = ['S_GAMUT_PRIMARIES',
           'S_GAMUT_ILLUMINANT',
           'S_GAMUT_WHITEPOINT',
           'S_GAMUT_TO_XYZ_MATRIX',
           'XYZ_TO_S_GAMUT_MATRIX',
           'S_GAMUT_COLOURSPACE',
           'S_GAMUT3_COLOURSPACE',
           'S_GAMUT3_CINE_PRIMARIES',
           'S_GAMUT3_CINE_ILLUMINANT',
           'S_GAMUT3_CINE_WHITEPOINT',
           'S_GAMUT3_CINE_TO_XYZ_MATRIX',
           'XYZ_TO_S_GAMUT3_CINE_MATRIX',
           'S_GAMUT3_CINE_COLOURSPACE']

S_GAMUT_PRIMARIES = np.array(
    [[0.730, 0.280],
     [0.140, 0.855],
     [0.100, -0.050]])
"""
*S-Gamut* colourspace primaries.

S_GAMUT_PRIMARIES : ndarray, (3, 2)
"""

S_GAMUT_ILLUMINANT = 'D65'
"""
*S-Gamut* colourspace whitepoint name as illuminant.

S_GAMUT_ILLUMINANT : unicode
"""

S_GAMUT_WHITEPOINT = ILLUMINANTS.get(
    'CIE 1931 2 Degree Standard Observer').get(S_GAMUT_ILLUMINANT)
"""
*S-Gamut* colourspace whitepoint.

S_GAMUT_WHITEPOINT : tuple
"""

S_GAMUT_TO_XYZ_MATRIX = np.array(
    [[0.7064827132, 0.1288010498, 0.1151721641],
     [0.2709796708, 0.7866064112, -0.0575860820],
     [-0.0096778454, 0.0046000375, 1.0941355587]])
"""
*S-Gamut* colourspace to *CIE XYZ* tristimulus values matrix.

S_GAMUT_TO_XYZ_MATRIX : array_like, (3, 3)
"""

XYZ_TO_S_GAMUT_MATRIX = np.array(
    [[1.5073998991, -0.2458221374, -0.1716116808],
     [-0.5181517271, 1.3553912409, 0.1258786682],
     [0.0155116982, -0.0078727714, 0.9119163656]])
"""
*CIE XYZ* tristimulus values to *S-Gamut* colourspace matrix.

XYZ_TO_S_GAMUT_MATRIX : array_like, (3, 3)
"""

S_GAMUT_COLOURSPACE = RGB_Colourspace(
    'S-Gamut',
    S_GAMUT_PRIMARIES,
    S_GAMUT_WHITEPOINT,
    S_GAMUT_ILLUMINANT,
    S_GAMUT_TO_XYZ_MATRIX,
    XYZ_TO_S_GAMUT_MATRIX,
    log_encoding_SLog2,
    log_decoding_SLog2)
"""
*S-Gamut* colourspace.

S_GAMUT_COLOURSPACE : RGB_Colourspace
"""

S_GAMUT3_COLOURSPACE = RGB_Colourspace(
    'S-Gamut3',
    S_GAMUT_PRIMARIES,
    S_GAMUT_WHITEPOINT,
    S_GAMUT_ILLUMINANT,
    S_GAMUT_TO_XYZ_MATRIX,
    XYZ_TO_S_GAMUT_MATRIX,
    log_encoding_SLog3,
    log_decoding_SLog3)
"""
*S-Gamut3* colourspace.

S_GAMUT3_COLOURSPACE : RGB_Colourspace
"""

S_GAMUT3_CINE_PRIMARIES = np.array(
    [[0.76600, 0.27500],
     [0.22500, 0.80000],
     [0.08900, -0.08700]])
"""
*S-Gamut3.Cine* colourspace primaries.

S_GAMUT_PRIMARIES : ndarray, (3, 2)
"""

S_GAMUT3_CINE_ILLUMINANT = S_GAMUT_ILLUMINANT
"""
*S-Gamut3.Cine* colourspace whitepoint name as illuminant.

S_GAMUT3_CINE_ILLUMINANT : unicode
"""

S_GAMUT3_CINE_WHITEPOINT = S_GAMUT_WHITEPOINT
"""
*S-Gamut3.Cine* colourspace whitepoint.

S_GAMUT3_CINE_WHITEPOINT : tuple
"""

S_GAMUT3_CINE_TO_XYZ_MATRIX = np.array(
    [[0.5990839208, 0.2489255161, 0.1024464902],
     [0.2150758201, 0.8850685017, -0.1001443219],
     [-0.0320658495, -0.0276583907, 1.1487819910]])
"""
*S-Gamut3.Cine* colourspace to *CIE XYZ* tristimulus values matrix.

S_GAMUT3_CINE_TO_XYZ_MATRIX : array_like, (3, 3)
"""

XYZ_TO_S_GAMUT3_CINE_MATRIX = np.array(
    [[1.8467789693, -0.5259861230, -0.2105452114],
     [-0.4441532629, 1.2594429028, 0.1493999729],
     [0.0408554212, 0.0156408893, 0.8682072487]])
"""
*CIE XYZ* tristimulus values to *S-Gamut3.Cine* colourspace matrix.

XYZ_TO_S_GAMUT3_CINE_MATRIX : array_like, (3, 3)
"""

S_GAMUT3_CINE_COLOURSPACE = RGB_Colourspace(
    'S-Gamut3.Cine',
    S_GAMUT3_CINE_PRIMARIES,
    S_GAMUT3_CINE_WHITEPOINT,
    S_GAMUT3_CINE_ILLUMINANT,
    S_GAMUT3_CINE_TO_XYZ_MATRIX,
    XYZ_TO_S_GAMUT3_CINE_MATRIX,
    log_encoding_SLog3,
    log_decoding_SLog3)
"""
*S-Gamut3.Cine* colourspace.

S_GAMUT3_CINE_COLOURSPACE : RGB_Colourspace
"""
<|endoftext|>


<commit_before>{
  'targets': [
    {
      'target_name': 'node-stringprep',
      'sources': [ 'node-stringprep.cc' ],
      'conditions': [
        ['OS=="mac"', {
          'include_dirs': [
              '/opt/local/include'
          ],
          'xcode_settings': {
            'GCC_ENABLE_CPP_EXCEPTIONS': 'YES'
          }
        }]
      ]
     }
  ]
}<commit_msg>add exception handling support for other Linux.<commit_after>{
  'targets': [
    {
      'target_name': 'node-stringprep',
      'sources': [ 'node-stringprep.cc' ],
      'cflags!': [ '-fno-exceptions' ],
      'cflags_cc!': [ '-fno-exceptions' ],
      'conditions': [
        ['OS=="mac"', {
          'include_dirs': [
              '/opt/local/include'
          ],
          'xcode_settings': {
            'GCC_ENABLE_CPP_EXCEPTIONS': 'YES'
          }
        }]
      ]
     }
  ]
}<|endoftext|>


<commit_before>from autobahn.asyncio.wamp import ApplicationSession

class QuestionComponent(ApplicationSession):
    """
    An application component providing procedures with different kinds
    of arguments.
    """
    def get_channel_from_event(self,event):
        return event.topic
    def convert_to_intent(self, sentence):

        if sentence == "Wo ist Klasse xyz?":

            return {
                    "eventName": "SEARCH_FOR_CLASS",
                    "className": "xyz"
                    }

    async def onJoin(self, details):

        def got_message(incomming_msg,obj,event):
            incoming_text = incomming_msg['text']
            channel = self.get_channel_from_event(event)
            print("incomming: {0}".format(incomming_msg))
            self.publish(u'sofia.channel.{0}.messages.OUTGOING_MESSAGE'.format(channel), {
                "text": u'The force will be with you always',
                "channel": u'GENERAL'
                                                              })

        await self.subscribe(got_message, u'sofia.channel..messages.INCOMING_MESSAGE',{'match': 'wildcard'})
        print("Registered methods; ready for actions. Give me some... ")
<commit_msg>- add support for pattern based receiving of messages<commit_after>from autobahn.asyncio.wamp import ApplicationSession
from autobahn.wamp.types import SubscribeOptions
import functools

class QuestionComponent(ApplicationSession):
    """
    An application component providing procedures with different kinds
    of arguments.
    """
    def get_channel_from_details(self,details):
        arr = details.topic.split(".")
        indexOfMessage = arr.index("channel")
        return arr[indexOfMessage+1]

    def convert_to_intent(self, sentence):

        if sentence == "Wo ist Klasse xyz?":

            return {
                    "eventName": "SEARCH_FOR_CLASS",
                    "className": "xyz"
                    }

    def onIncommingMessage(self,message, details):
        print("{0}".format(message))
        channel_id = self.get_channel_from_details(details)

        print("got message {0}".format(channel_id))
        self.publish(u'sofia.channel.{0}.messages.OUTGOING_MESSAGE'.format(channel_id), {
            "text": u'The force will be with you always',
            "channel": channel_id})

    async def onJoin(self, details):

        await self.subscribe(self.onIncommingMessage,"sofia.channel..messages.INCOMING_MESSAGE",options=SubscribeOptions(match='wildcard',details_arg='details'))
        print("Registered methods; ready for actions. Give me some... ")
<|endoftext|>


<commit_before>{
  'includes': [ 'common.gypi' ],
  'variables': {
      'runtime_link%':'shared',
  },
  'conditions': [
      ['OS=="win"', {
        'variables': {
          'PROTOBUF_INCLUDES%':'C:/dev2/protobuf/vsprojects/include',
          'PROTOBUF_LIBS%':'C:/dev2/protobuf/vsprojects/Release',
          'PROTOBUF_LIBRARY%':'libprotobuf-lite.lib',
        }
      }]
  ],
  'targets': [
    {
      'target_name': '_mapnik',
      'sources': [
          "src/node_mapnik.cpp",
          "src/mapnik_map.cpp",
          "src/mapnik_color.cpp",
          "src/mapnik_geometry.cpp",
          "src/mapnik_feature.cpp",
          "src/mapnik_image.cpp",
          "src/mapnik_image_view.cpp",
          "src/mapnik_grid.cpp",
          "src/mapnik_grid_view.cpp",
          "src/mapnik_js_datasource.cpp",
          "src/mapnik_memory_datasource.cpp",
          "src/mapnik_palette.cpp",
          "src/mapnik_projection.cpp",
          "src/mapnik_proj_transform.cpp",
          "src/mapnik_layer.cpp",
          "src/mapnik_datasource.cpp",
          "src/mapnik_featureset.cpp",
          "src/mapnik_expression.cpp",
          "src/mapnik_query.cpp",
          "src/mapnik_cairo_surface.cpp",
          "src/mapnik_vector_tile.cpp",
          "node_modules/mapnik-vector-tile/src/vector_tile.pb.cc"
      ],
      'include_dirs': [
          './node_modules/mapnik-vector-tile/src/',
          './src'
      ],
      'conditions': [
        ['OS=="win"', {
            'include_dirs':[
                '<!@(mapnik-config --includes)',
                '<!@(mapnik-config --dep-includes)',
                '<@(PROTOBUF_INCLUDES)'
              ],
            'defines': ['NOMINMAX','<!@(mapnik-config --defines)'],
            'libraries': [
                '<!@(mapnik-config --libs)',
                '<!@(mapnik-config --dep-libs)',
                '<@(PROTOBUF_LIBRARY)'
            ],
            'msvs_disabled_warnings': [ 4244,4005,4506,4345,4804 ],
            'msvs_settings': {
            'VCCLCompilerTool': {
                # note: not respected, need to change in C:\Users\mapnik\.node-gyp\*\common.gypi
                'ExceptionHandling': 1,
                'RuntimeTypeInfo':'true',
                'RuntimeLibrary': '2'  # /MD http://stackoverflow.com/questions/757418/should-i-compile-with-md-or-mt
            },
            'VCLinkerTool': {
                'AdditionalOptions': [
                    # https://github.com/mapnik/node-mapnik/issues/74
                    '/FORCE:MULTIPLE'
                ],
                'AdditionalLibraryDirectories': [
                    '<!@(mapnik-config --ldflags)',
                    '<@(PROTOBUF_LIBS)'
                ],
            },
          }
        },{
            'cflags_cc!': ['-fno-rtti', '-fno-exceptions'],
            'cflags_cc' : [
                '<!@(mapnik-config --cflags)',
                '<!@(pkg-config protobuf --cflags)'
            ],
            'libraries':[
                '<!@(mapnik-config --libs)',
                '<!@(pkg-config protobuf --libs-only-L)',
                '-lprotobuf-lite'
            ],
            'conditions': [
              ['runtime_link == "static"', {
                  'libraries': [
                    '<!@(mapnik-config --ldflags)',
                    '<!@(mapnik-config --dep-libs)'
                   ]
              }]
            ],
            'xcode_settings': {
              'OTHER_CPLUSPLUSFLAGS':[
                  '<!@(mapnik-config --cflags)'
              ],
              'OTHER_CFLAGS':[
                  '<!@(mapnik-config --cflags)',
                  '<!@(pkg-config protobuf --cflags)'
              ],
              'GCC_ENABLE_CPP_RTTI': 'YES',
              'GCC_ENABLE_CPP_EXCEPTIONS': 'YES'
            }
        }]
      ]
    },
    {
      'target_name': 'action_after_build',
      'type': 'none',
      'dependencies': [ '_mapnik' ],
      'actions': [
          {
            'action_name': 'generate_setting',
            'inputs': [
              'gen_settings.py'
            ],
            'outputs': [
              'lib/mapnik_settings.js'
            ],
            'action': ['python', 'gen_settings.py']
          },
      ],
      'copies': [
          {
            'files': [ '<(PRODUCT_DIR)/_mapnik.node' ],
            'destination': './lib/'
          }
      ]
    }
  ]
}<commit_msg>always use ldflags from mapnik in case they are customized<commit_after>{
  'includes': [ 'common.gypi' ],
  'variables': {
      'runtime_link%':'shared',
  },
  'conditions': [
      ['OS=="win"', {
        'variables': {
          'PROTOBUF_INCLUDES%':'C:/dev2/protobuf/vsprojects/include',
          'PROTOBUF_LIBS%':'C:/dev2/protobuf/vsprojects/Release',
          'PROTOBUF_LIBRARY%':'libprotobuf-lite.lib',
        }
      }]
  ],
  'targets': [
    {
      'target_name': '_mapnik',
      'sources': [
          "src/node_mapnik.cpp",
          "src/mapnik_map.cpp",
          "src/mapnik_color.cpp",
          "src/mapnik_geometry.cpp",
          "src/mapnik_feature.cpp",
          "src/mapnik_image.cpp",
          "src/mapnik_image_view.cpp",
          "src/mapnik_grid.cpp",
          "src/mapnik_grid_view.cpp",
          "src/mapnik_js_datasource.cpp",
          "src/mapnik_memory_datasource.cpp",
          "src/mapnik_palette.cpp",
          "src/mapnik_projection.cpp",
          "src/mapnik_proj_transform.cpp",
          "src/mapnik_layer.cpp",
          "src/mapnik_datasource.cpp",
          "src/mapnik_featureset.cpp",
          "src/mapnik_expression.cpp",
          "src/mapnik_query.cpp",
          "src/mapnik_cairo_surface.cpp",
          "src/mapnik_vector_tile.cpp",
          "node_modules/mapnik-vector-tile/src/vector_tile.pb.cc"
      ],
      'include_dirs': [
          './node_modules/mapnik-vector-tile/src/',
          './src'
      ],
      'conditions': [
        ['OS=="win"', {
            'include_dirs':[
                '<!@(mapnik-config --includes)',
                '<!@(mapnik-config --dep-includes)',
                '<@(PROTOBUF_INCLUDES)'
              ],
            'defines': ['NOMINMAX','<!@(mapnik-config --defines)'],
            'libraries': [
                '<!@(mapnik-config --libs)',
                '<!@(mapnik-config --dep-libs)',
                '<@(PROTOBUF_LIBRARY)'
            ],
            'msvs_disabled_warnings': [ 4244,4005,4506,4345,4804 ],
            'msvs_settings': {
            'VCCLCompilerTool': {
                # note: not respected, need to change in C:\Users\mapnik\.node-gyp\*\common.gypi
                'ExceptionHandling': 1,
                'RuntimeTypeInfo':'true',
                'RuntimeLibrary': '2'  # /MD http://stackoverflow.com/questions/757418/should-i-compile-with-md-or-mt
            },
            'VCLinkerTool': {
                'AdditionalOptions': [
                    # https://github.com/mapnik/node-mapnik/issues/74
                    '/FORCE:MULTIPLE'
                ],
                'AdditionalLibraryDirectories': [
                    '<!@(mapnik-config --ldflags)',
                    '<@(PROTOBUF_LIBS)'
                ],
            },
          }
        },{
            'cflags_cc!': ['-fno-rtti', '-fno-exceptions'],
            'cflags_cc' : [
                '<!@(mapnik-config --cflags)',
                '<!@(pkg-config protobuf --cflags)'
            ],
            'libraries':[
                '<!@(mapnik-config --libs)',
                '<!@(mapnik-config --ldflags)',
                '<!@(pkg-config protobuf --libs-only-L)',
                '-lprotobuf-lite'
            ],
            'conditions': [
              ['runtime_link == "static"', {
                  'libraries': [
                    '<!@(mapnik-config --dep-libs)'
                   ]
              }]
            ],
            'xcode_settings': {
              'OTHER_CPLUSPLUSFLAGS':[
                  '<!@(mapnik-config --cflags)'
              ],
              'OTHER_CFLAGS':[
                  '<!@(mapnik-config --cflags)',
                  '<!@(pkg-config protobuf --cflags)'
              ],
              'GCC_ENABLE_CPP_RTTI': 'YES',
              'GCC_ENABLE_CPP_EXCEPTIONS': 'YES'
            }
        }]
      ]
    },
    {
      'target_name': 'action_after_build',
      'type': 'none',
      'dependencies': [ '_mapnik' ],
      'actions': [
          {
            'action_name': 'generate_setting',
            'inputs': [
              'gen_settings.py'
            ],
            'outputs': [
              'lib/mapnik_settings.js'
            ],
            'action': ['python', 'gen_settings.py']
          },
      ],
      'copies': [
          {
            'files': [ '<(PRODUCT_DIR)/_mapnik.node' ],
            'destination': './lib/'
          }
      ]
    }
  ]
}<|endoftext|>


<commit_before>{
    "targets": [
        {
            "target_name": "superstring",
            "dependencies": [
                "superstring_core"
            ],
            "sources": [
                "src/bindings/bindings.cc",
                "src/bindings/marker-index-wrapper.cc",
                "src/bindings/patch-wrapper.cc",
                "src/bindings/point-wrapper.cc",
                "src/bindings/buffer-offset-index-wrapper.cc",
            ],
            "include_dirs": [
              "src/core",
              "<!(node -e \"require('nan')\")"
            ],
        },
        {
            "target_name": "superstring_core",
            "type": "static_library",
            "sources": [
                "src/core/patch.cc",
                "src/core/point.cc",
                "src/core/text.cc",
                "src/core/marker-index.cc",
                "src/core/buffer-offset-index.cc"
            ]
        },
    ],

    "variables": {
        "tests": 0
    },

    "conditions": [
        # If --tests is passed to node-gyp configure, we'll build a standalone
        # executable that runs tests on the patch.
        ['tests != 0', {
            "targets": [{
                "target_name": "tests",
                "type": "executable",
                "cflags_cc!": ["-fno-exceptions"],
                "defines": [
                    "CATCH_CONFIG_CPP11_NO_IS_ENUM"
                ],
                "sources": [
                    "test/native/patch-test.cc",
                    "test/native/tests.cc",
                ],
                "include_dirs": [
                    "vendor",
                    "src/core",
                ],
                "dependencies": [
                    "superstring_core"
                ],
                "conditions": [
                    ['OS=="mac"', {
                        "xcode_settings": {
                            "GCC_ENABLE_CPP_EXCEPTIONS": "YES",
                        }
                    }]
                ]
            }]
        }]
    ],

    "target_defaults": {
        "cflags_cc": ["-std=c++11"],
        "conditions": [
            ['OS=="mac"', {
                "xcode_settings": {
                    'CLANG_CXX_LIBRARY': 'libc++',
                    'CLANG_CXX_LANGUAGE_STANDARD':'c++11',
                }
            }],
            ['OS=="win"', {
                "link_settings": {
                    "libraries": ["ws2_32.lib"]
                }
                "defines": [
                    "NOMINMAX"
                ]
            }]
        ]
    }
}
<commit_msg>Fix syntax error in binding.gyp<commit_after>{
    "targets": [
        {
            "target_name": "superstring",
            "dependencies": [
                "superstring_core"
            ],
            "sources": [
                "src/bindings/bindings.cc",
                "src/bindings/marker-index-wrapper.cc",
                "src/bindings/patch-wrapper.cc",
                "src/bindings/point-wrapper.cc",
                "src/bindings/buffer-offset-index-wrapper.cc",
            ],
            "include_dirs": [
              "src/core",
              "<!(node -e \"require('nan')\")"
            ],
        },
        {
            "target_name": "superstring_core",
            "type": "static_library",
            "sources": [
                "src/core/patch.cc",
                "src/core/point.cc",
                "src/core/text.cc",
                "src/core/marker-index.cc",
                "src/core/buffer-offset-index.cc"
            ]
        },
    ],

    "variables": {
        "tests": 0
    },

    "conditions": [
        # If --tests is passed to node-gyp configure, we'll build a standalone
        # executable that runs tests on the patch.
        ['tests != 0', {
            "targets": [{
                "target_name": "tests",
                "type": "executable",
                "cflags_cc!": ["-fno-exceptions"],
                "defines": [
                    "CATCH_CONFIG_CPP11_NO_IS_ENUM"
                ],
                "sources": [
                    "test/native/patch-test.cc",
                    "test/native/tests.cc",
                ],
                "include_dirs": [
                    "vendor",
                    "src/core",
                ],
                "dependencies": [
                    "superstring_core"
                ],
                "conditions": [
                    ['OS=="mac"', {
                        "xcode_settings": {
                            "GCC_ENABLE_CPP_EXCEPTIONS": "YES",
                        }
                    }]
                ]
            }]
        }]
    ],

    "target_defaults": {
        "cflags_cc": ["-std=c++11"],
        "conditions": [
            ['OS=="mac"', {
                "xcode_settings": {
                    'CLANG_CXX_LIBRARY': 'libc++',
                    'CLANG_CXX_LANGUAGE_STANDARD':'c++11',
                }
            }],
            ['OS=="win"', {
                "link_settings": {
                    "libraries": ["ws2_32.lib"]
                },
                "defines": [
                    "NOMINMAX"
                ],
            }]
        ]
    }
}
<|endoftext|>


<commit_before>from __future__ import division

# LIBTBX_SET_DISPATCHER_NAME dev.dials.duelling_profiles

import iotbx.phil

phil_scope = iotbx.phil.parse("""\
  method = *example nonsense flat
    .type = choice
""", process_includes=True)

help_message = '''

Examples::

  dev.dials.duelling_profiles experiments.json integrated.pickle

'''

def model_background(shoebox, mean_bg):
  from scitbx.random import variate, poisson_distribution
  dz, dy, dx = shoebox.focus()
  g = variate(poisson_distribution(mean = mean_bg))
  for k in range(dz):
    for j in range(dy):
      for i in range(dx):
        shoebox[k, j, i] += g.next()
  return

def model_reflection_example(reflection, experiment):
  hkl = reflection['miller_index']
  i0 = reflection['intensity.sum.value'] / reflection['dqe']
  s1 = reflection['s1']
  xyz = reflection['xyzcal.px']
  pixels = reflection['shoebox']
  mean_bg = reflection['background.mean']
  crystal = experiment.crystal
  profile = experiment.profile
  Amat = crystal.get_A_at_scan_point(int(xyz[2]))
  return

def model_reflection_nonsense(reflection, experiment):
  return

def model_reflection_flat(reflection, experiment):
  pixels = reflection['shoebox']
  pixels.flatten()
  data = pixels.data
  dz, dy, dx = data.focus()
  return

def main(reflections, experiment, method):
  nref0 = len(reflections)

  if 'intensity.prf.variance' in reflections:
    selection = reflections.get_flags(
      reflections.flags.integrated,
      all=True)
  else:
    selection = reflections.get_flags(
      reflections.flags.integrated_sum)
  reflections = reflections.select(selection)

  nref1 = len(reflections)

  print 'Removed %d invalid reflections, %d remain' % (nref0 - nref1, nref1)

  for j, reflection in enumerate(reflections):
    globals()['model_reflection_%s' % method](reflection, experiment)

  return

def run(args):
  from dials.util.options import OptionParser
  from dials.util.options import flatten_experiments
  from dials.util.options import flatten_datablocks
  from dials.util.options import flatten_reflections
  import libtbx.load_env

  usage = "%s [options] integrated.pickle experiments.json" % (
    libtbx.env.dispatcher_name)

  parser = OptionParser(
    usage=usage,
    phil=phil_scope,
    read_experiments=True,
    read_reflections=True,
    check_format=False,
    epilog=help_message)

  params, options = parser.parse_args(show_diff_phil=False)
  experiments = flatten_experiments(params.input.experiments)
  reflections = flatten_reflections(params.input.reflections)

  if len(experiments) != 1 or len(reflections) != 1:
    parser.print_help()
    exit()

  if not 'shoebox' in reflections[0]:
    print 'Please add shoeboxes to reflection pickle'
    exit()

  main(reflections[0], experiments[0], params.method)

if __name__ == '__main__':
  import sys
  run(sys.argv[1:])
<commit_msg>Tool for Sm / F model<commit_after>from __future__ import division

# LIBTBX_SET_DISPATCHER_NAME dev.dials.duelling_profiles

import iotbx.phil

phil_scope = iotbx.phil.parse("""\
  method = *example nonsense flat
    .type = choice
""", process_includes=True)

help_message = '''

Examples::

  dev.dials.duelling_profiles experiments.json integrated.pickle

'''

def model_background(shoebox, mean_bg):
  from scitbx.random import variate, poisson_distribution
  dz, dy, dx = shoebox.focus()
  g = variate(poisson_distribution(mean = mean_bg))
  for k in range(dz):
    for j in range(dy):
      for i in range(dx):
        shoebox[k, j, i] += g.next()
  return

def random_vector_cone(vector, sd=0.5):
  import random
  o0 = vector.ortho()
  o1 = vector.cross(o0)
  return vector.rotate(o0, random.gauss(0, sd),  deg = True).rotate(
    o1, random.gauss(0, sd),  deg = True)

def model_reflection_example(reflection, experiment):
  hkl = reflection['miller_index']
  i0 = reflection['intensity.sum.value'] / reflection['dqe']
  s1 = reflection['s1']
  xyz = reflection['xyzcal.px']
  pixels = reflection['shoebox']
  mean_bg = reflection['background.mean']
  crystal = experiment.crystal
  profile = experiment.profile
  Amat = crystal.get_A_at_scan_point(int(xyz[2]))
  return

def model_reflection_nonsense(reflection, experiment):
  return

def model_reflection_flat(reflection, experiment):
  pixels = reflection['shoebox']
  pixels.flatten()
  data = pixels.data
  dz, dy, dx = data.focus()
  return

def main(reflections, experiment, method):
  nref0 = len(reflections)

  if 'intensity.prf.variance' in reflections:
    selection = reflections.get_flags(
      reflections.flags.integrated,
      all=True)
  else:
    selection = reflections.get_flags(
      reflections.flags.integrated_sum)
  reflections = reflections.select(selection)

  nref1 = len(reflections)

  print 'Removed %d invalid reflections, %d remain' % (nref0 - nref1, nref1)

  for j, reflection in enumerate(reflections):
    globals()['model_reflection_%s' % method](reflection, experiment)

  return

def run(args):
  from dials.util.options import OptionParser
  from dials.util.options import flatten_experiments
  from dials.util.options import flatten_datablocks
  from dials.util.options import flatten_reflections
  import libtbx.load_env

  usage = "%s [options] integrated.pickle experiments.json" % (
    libtbx.env.dispatcher_name)

  parser = OptionParser(
    usage=usage,
    phil=phil_scope,
    read_experiments=True,
    read_reflections=True,
    check_format=False,
    epilog=help_message)

  params, options = parser.parse_args(show_diff_phil=False)
  experiments = flatten_experiments(params.input.experiments)
  reflections = flatten_reflections(params.input.reflections)

  if len(experiments) != 1 or len(reflections) != 1:
    parser.print_help()
    exit()

  if not 'shoebox' in reflections[0]:
    print 'Please add shoeboxes to reflection pickle'
    exit()

  main(reflections[0], experiments[0], params.method)

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