<commit_before>import sys

# Per @jmchilton's suggestion, and this SO question:
# http://stackoverflow.com/a/6032023
def load_conflicting_package(name, not_name, local_module):
    import imp

    for i in range(0, 100):
        random_name = 'random_name_%d' % (i,)
        if random_name not in sys.modules:
            break
        else:
            random_name = None
    if random_name is None:
        raise RuntimeError("Couldn't manufacture an unused module name.")
    # NB: This code is unlikely to work for nonstdlib overrides.
    # This will hold the correct sys.path for the REAL argparse
    for path in sys.path:
        try:
            (f, pathname, desc) = imp.find_module(name, [path])
            if not_name not in pathname and desc[2] == imp.PKG_DIRECTORY:
                handle.close()
                break
        except:
            # Many sys.paths won't contain the module of interest
            pass

    module = imp.load_module(random_name, f, pathname, desc)
    f.close()
    return sys.modules[random_name]

ap = load_conflicting_package('argparse', 'gxargparse', sys.modules[load_conflicting_package.__module__])
import galaxyxml.tool as gxt
import galaxyxml.tool.parameters as gxtp
import argparse_translation as at

class ArgumentParser(object):

    def __init__(self, *args, **kwargs):
        self.parser = ap.ArgumentParser(*args, **kwargs)
        self.argument_list = []
        # TODO: support the prefix_chars option
        #print self.parser.prefix_chars

    def add_argument(self, *args, **kwargs):
        result = self.parser.add_argument(*args, **kwargs)
        self.argument_list.append(result)

    #ArgumentParser.add_subparsers([title][, description][, prog][, parser_class][, action][, option_string][, dest][, help][, metavar])
    def add_subparsers(self, *args, **kwargs):
        return self.parser.add_subparsers(*args, **kwargs)

    #ArgumentParser.add_argument_group(title=None, description=None)
    def add_argument_group(self, *args, **kwargs):
        return self.parser.add_argument_group(*args, **kwargs)

    #ArgumentParser.add_mutually_exclusive_group(required=False)
    def add_mutually_exclusive_group(self, *args, **kwargs):
        return self.parser.add_mutually_exclusive_group(*args, **kwargs)

    def set_defaults(self, *args, **kwargs):
        self.parser.set_defaults(*args, **kwargs)

    def get_default(self, *args, **kwargs):
        return self.parser.get_default(*args, **kwargs)

    def print_usage(self, *args, **kwargs):
        self.parser.print_usage(*args, **kwargs)

    def print_help(self, *args, **kwargs):
        self.parser.print_help(*args, **kwargs)

    def format_usage(self, *args, **kwargs):
        return self.parser.format_usage(*args, **kwargs)

    def format_help(self, *args, **kwargs):
        return self.parser.format_help(*args, **kwargs)

    def parse_known_args(self, *args, **kwargs):
        return self.parser.parse_known_args(*args, **kwargs)

    def convert_arg_line_to_args(self, *args, **kwargs):
        return self.parser.convert_arg_line_to_args(*args, **kwargs)

    def parse_args(self, *args, **kwargs):
        if '--generate_galaxy_xml' in sys.argv:
            self.tool = gxt.Tool(
                    self.parser.prog,
                    self.parser.prog,
                    self.parser.print_version(),
                    self.parser.description,
                    self.parser.prog,
                    interpreter='python')

            self.inputs = gxtp.Inputs()
            self.outputs = gxtp.Outputs()

            # TODO: replace with argparse-esque library to do this.
            stdout = gxtp.OutputParameter('default', 'txt')
            stdout.command_line_override = '> $default'
            self.outputs.append(stdout)

            self.at = at.ArgparseTranslation()
            # Only build up arguments if the user actually requests it
            for result in self.argument_list:
                # I am SO thankful they return the argument here. SO useful.
                argument_type = result.__class__.__name__
                # http://stackoverflow.com/a/3071
                methodToCall = getattr(self.at, argument_type)
                gxt_parameter = methodToCall(result, tool=self.tool)
                if gxt_parameter is not None:
                    self.inputs.append(gxt_parameter)

            self.tool.inputs = self.inputs
            self.tool.outputs = self.outputs
            self.tool.help = 'TODO: Bug @erasche until he fixes it or make a PR'
            data = self.tool.export()
            print data
            sys.exit()
        else:
            return self.parser.parse_args(*args, **kwargs)

<commit_msg>This should work a bit better<commit_after>import sys

# Per @jmchilton's suggestion, and this SO question:
# http://stackoverflow.com/a/6032023
def load_conflicting_package(name, not_name, local_module):
    import imp

    for i in range(0, 100):
        random_name = 'random_name_%d' % (i,)
        if random_name not in sys.modules:
            break
        else:
            random_name = None
    if random_name is None:
        raise RuntimeError("Couldn't manufacture an unused module name.")
    # NB: This code is unlikely to work for nonstdlib overrides.
    # This will hold the correct sys.path for the REAL argparse
    for path in sys.path:
        try:
            (f, pathname, desc) = imp.find_module(name, [path])
            if not_name not in pathname and desc[2] == 1:
                module = imp.load_module(random_name, f, pathname, desc)
                f.close()
                return sys.modules[random_name]
        except:
            # Many sys.paths won't contain the module of interest
            pass
    return None

ap = load_conflicting_package('argparse', 'gxargparse', sys.modules[load_conflicting_package.__module__])
import galaxyxml.tool as gxt
import galaxyxml.tool.parameters as gxtp
import argparse_translation as at

class ArgumentParser(object):

    def __init__(self, *args, **kwargs):
        self.parser = ap.ArgumentParser(*args, **kwargs)
        self.argument_list = []
        # TODO: support the prefix_chars option
        #print self.parser.prefix_chars

    def add_argument(self, *args, **kwargs):
        result = self.parser.add_argument(*args, **kwargs)
        self.argument_list.append(result)

    #ArgumentParser.add_subparsers([title][, description][, prog][, parser_class][, action][, option_string][, dest][, help][, metavar])
    def add_subparsers(self, *args, **kwargs):
        return self.parser.add_subparsers(*args, **kwargs)

    #ArgumentParser.add_argument_group(title=None, description=None)
    def add_argument_group(self, *args, **kwargs):
        return self.parser.add_argument_group(*args, **kwargs)

    #ArgumentParser.add_mutually_exclusive_group(required=False)
    def add_mutually_exclusive_group(self, *args, **kwargs):
        return self.parser.add_mutually_exclusive_group(*args, **kwargs)

    def set_defaults(self, *args, **kwargs):
        self.parser.set_defaults(*args, **kwargs)

    def get_default(self, *args, **kwargs):
        return self.parser.get_default(*args, **kwargs)

    def print_usage(self, *args, **kwargs):
        self.parser.print_usage(*args, **kwargs)

    def print_help(self, *args, **kwargs):
        self.parser.print_help(*args, **kwargs)

    def format_usage(self, *args, **kwargs):
        return self.parser.format_usage(*args, **kwargs)

    def format_help(self, *args, **kwargs):
        return self.parser.format_help(*args, **kwargs)

    def parse_known_args(self, *args, **kwargs):
        return self.parser.parse_known_args(*args, **kwargs)

    def convert_arg_line_to_args(self, *args, **kwargs):
        return self.parser.convert_arg_line_to_args(*args, **kwargs)

    def parse_args(self, *args, **kwargs):
        if '--generate_galaxy_xml' in sys.argv:
            self.tool = gxt.Tool(
                    self.parser.prog,
                    self.parser.prog,
                    self.parser.print_version(),
                    self.parser.description,
                    self.parser.prog,
                    interpreter='python')

            self.inputs = gxtp.Inputs()
            self.outputs = gxtp.Outputs()

            # TODO: replace with argparse-esque library to do this.
            stdout = gxtp.OutputParameter('default', 'txt')
            stdout.command_line_override = '> $default'
            self.outputs.append(stdout)

            self.at = at.ArgparseTranslation()
            # Only build up arguments if the user actually requests it
            for result in self.argument_list:
                # I am SO thankful they return the argument here. SO useful.
                argument_type = result.__class__.__name__
                # http://stackoverflow.com/a/3071
                methodToCall = getattr(self.at, argument_type)
                gxt_parameter = methodToCall(result, tool=self.tool)
                if gxt_parameter is not None:
                    self.inputs.append(gxt_parameter)

            self.tool.inputs = self.inputs
            self.tool.outputs = self.outputs
            self.tool.help = 'TODO: Bug @erasche until he fixes it or make a PR'
            data = self.tool.export()
            print data
            sys.exit()
        else:
            return self.parser.parse_args(*args, **kwargs)

<|endoftext|>


<commit_before>import os
import subprocess
import sys

from armaadmin import config, env, errors, log, server

servers = {}

def get(name):
	if not name in servers:
		raise errors.NoServerError

	return servers[name]

def create(name, source):
	if name in servers:
		raise errors.ServerExistsError

	server.create(name, source)
	servers[name] = Server(name)

def destroy(name):
	if not name in servers:
		raise errors.NoServerError

	servers[name].stop()
	server.destroy(name)
	del servers[name]

def poll():
	for server in servers.values():
		if server.server and not server.serverStatus():
			server.server.stdout.write('WARNING: The server did not gracefully quit; now restarting.\n')
			server.stop()
			server.start()
			log.warn(server.name + ' did not gracefully quit and was restarted.')

class Server:
	def __init__(self, name):
		self.name = name
		self.dir = config.prefix + '/' + name
		self.server = None
		self.script = None
		if self.exists():
			self.status_msg = 'stopped'
		else:
			self.status_msg = 'nonexistent'

	def exists(self):
		return os.path.exists(self.dir) and os.path.isdir(self.dir)

	def start(self):
		if not self.exists():
			raise errors.NoServerError

		if self.serverStatus():
			raise errors.ServerRunningError

		self.status_msg = 'starting'
		self.server = subprocess.Popen([ self.dir + '/bin/armagetronad-dedicated', '--vardir', self.dir + '/var', '--userdatadir', self.dir + '/user', '--configdir', self.dir + '/config', '--datadir', self.dir + '/data' ], stdin=subprocess.PIPE, stdout=open(self.dir + '/arma.log', 'a'), stderr=open(self.dir + '/error.log', 'w'), preexec_fn=env.demote, env=env.env, cwd=self.dir)
		self.status_msg = 'started'

		self.startScript()

	def startScript(self):
		if os.path.exists(self.dir + '/scripts/script.py') and self.serverStatus() and not self.scriptStatus():
			self.script = subprocess.Popen([ sys.executable, self.dir + '/scripts/script.py' ], stdin=open(self.dir + '/var/ladderlog.txt', 'r'), stdout=self.server.stdin, stderr=open(self.dir + '/script-error.log', 'w'), preexec_fn=env.demote, env=env.env, cwd=self.dir + '/var')

	def stop(self):
		if self.serverStatus():
			self.status_msg = 'stopping'
			self.server.terminate()
			try:
				self.server.wait(5)
			except subprocess.TimeoutExpired:
				self.server.kill()
				self.server.wait()

		self.server = None
		self.status_msg = 'stopped'

		self.stopScript()

	def stopScript(self):
		if self.scriptStatus():
			self.script.terminate()
			try:
				self.script.wait(5)
			except subprocess.TimeoutExpired:
				self.script.kill()
				self.script.wait()

		self.script = None

	def restart(self):
		self.stop()
		self.start()

	def reload(self):
		if self.server:
			self.server.stdin.write('INCLUDE settings.cfg')
			self.server.stdin.write('INCLUDE server_info.cfg')
			self.server.stdin.write('INCLUDE settings_custom.cfg')
			if self.script:
				self.server.stdin.write('INCLUDE script.cfg')
		else:
			raise errors.ServerStoppedError

	def upgrade(self):
		status = self.serverStatus()
		self.stop()
		server.create(self.name, self.getSource())
		if status:
			self.start()

	def serverStatus(self):
		if self.server:
			return self.server.poll() == None
		else:
			return False

	def scriptStatus(self):
		if self.script:
			return self.script.poll() == None
		else:
			return False

	def status(self):
		return self.status_msg

	def sendCommand(self, command):
		if self.server:
			self.server.stdin.write(command)
		else:
			raise errors.ServerStoppedError

	def getLog(self):
		with open(self.dir + '/arma.log', 'r', encoding='latin_1') as file:
			return file.read()

	def getSettings(self):
		with open(self.dir + '/config/settings_custom.cfg', 'r', encoding='latin_1') as file:
			return file.read()

	def updateSettings(self, settings):
		with open(self.dir + '/config/settings_custom.cfg', 'w', encoding='latin_1') as file:
			file.write(settings)

	def getScript(self):
		with open(self.dir + '/scripts/script.py', 'r') as file:
			return file.read()

	def updateScript(self, script):
		with open(self.dir + '/scripts/script.py', 'w') as file:
			file.write(script)

	def getScriptlog(self):
		with open(self.dir + '/script-error.log', 'r') as file:
			return file.read()

	def getSource(self):
		with open(self.dir + '/source', 'r') as file:
			return file.read().split('|')[0]

	def getRevision(self):
		with open(self.dir + '/source', 'r') as file:
			return file.read().split('|')[1]

for dir in os.listdir(config.prefix):
	path = config.prefix + '/' + dir + '/bin/armagetronad-dedicated'
	if os.path.isfile(path) and os.access(path, os.X_OK):
		servers[dir] = Server(dir)
<commit_msg>Check existence by creating a server and using its 'exists' function<commit_after>import os
import subprocess
import sys

from armaadmin import config, env, errors, log, server

servers = {}

def get(name):
	if not name in servers:
		raise errors.NoServerError

	return servers[name]

def create(name, source):
	if name in servers:
		raise errors.ServerExistsError

	server.create(name, source)
	servers[name] = Server(name)

def destroy(name):
	if not name in servers:
		raise errors.NoServerError

	servers[name].stop()
	server.destroy(name)
	del servers[name]

def poll():
	for server in servers.values():
		if server.server and not server.serverStatus():
			server.server.stdout.write('WARNING: The server did not gracefully quit; now restarting.\n')
			server.stop()
			server.start()
			log.warn(server.name + ' did not gracefully quit and was restarted.')

class Server:
	def __init__(self, name):
		self.name = name
		self.dir = config.prefix + '/' + name
		self.server = None
		self.script = None
		if self.exists():
			self.status_msg = 'stopped'
		else:
			self.status_msg = 'nonexistent'

	def exists(self):
		path = self.dir + '/bin/armagetronad-dedicated'
		return os.path.isfile(path) and os.access(path, os.X_OK):

	def start(self):
		if not self.exists():
			raise errors.NoServerError

		if self.serverStatus():
			raise errors.ServerRunningError

		self.status_msg = 'starting'
		self.server = subprocess.Popen([ self.dir + '/bin/armagetronad-dedicated', '--vardir', self.dir + '/var', '--userdatadir', self.dir + '/user', '--configdir', self.dir + '/config', '--datadir', self.dir + '/data' ], stdin=subprocess.PIPE, stdout=open(self.dir + '/arma.log', 'a'), stderr=open(self.dir + '/error.log', 'w'), preexec_fn=env.demote, env=env.env, cwd=self.dir)
		self.status_msg = 'started'

		self.startScript()

	def startScript(self):
		if os.path.exists(self.dir + '/scripts/script.py') and self.serverStatus() and not self.scriptStatus():
			self.script = subprocess.Popen([ sys.executable, self.dir + '/scripts/script.py' ], stdin=open(self.dir + '/var/ladderlog.txt', 'r'), stdout=self.server.stdin, stderr=open(self.dir + '/script-error.log', 'w'), preexec_fn=env.demote, env=env.env, cwd=self.dir + '/var')

	def stop(self):
		if self.serverStatus():
			self.status_msg = 'stopping'
			self.server.terminate()
			try:
				self.server.wait(5)
			except subprocess.TimeoutExpired:
				self.server.kill()
				self.server.wait()

		self.server = None
		self.status_msg = 'stopped'

		self.stopScript()

	def stopScript(self):
		if self.scriptStatus():
			self.script.terminate()
			try:
				self.script.wait(5)
			except subprocess.TimeoutExpired:
				self.script.kill()
				self.script.wait()

		self.script = None

	def restart(self):
		self.stop()
		self.start()

	def reload(self):
		if self.server:
			self.server.stdin.write('INCLUDE settings.cfg')
			self.server.stdin.write('INCLUDE server_info.cfg')
			self.server.stdin.write('INCLUDE settings_custom.cfg')
			if self.script:
				self.server.stdin.write('INCLUDE script.cfg')
		else:
			raise errors.ServerStoppedError

	def upgrade(self):
		status = self.serverStatus()
		self.stop()
		server.create(self.name, self.getSource())
		if status:
			self.start()

	def serverStatus(self):
		if self.server:
			return self.server.poll() == None
		else:
			return False

	def scriptStatus(self):
		if self.script:
			return self.script.poll() == None
		else:
			return False

	def status(self):
		return self.status_msg

	def sendCommand(self, command):
		if self.server:
			self.server.stdin.write(command)
		else:
			raise errors.ServerStoppedError

	def getLog(self):
		with open(self.dir + '/arma.log', 'r', encoding='latin_1') as file:
			return file.read()

	def getSettings(self):
		with open(self.dir + '/config/settings_custom.cfg', 'r', encoding='latin_1') as file:
			return file.read()

	def updateSettings(self, settings):
		with open(self.dir + '/config/settings_custom.cfg', 'w', encoding='latin_1') as file:
			file.write(settings)

	def getScript(self):
		with open(self.dir + '/scripts/script.py', 'r') as file:
			return file.read()

	def updateScript(self, script):
		with open(self.dir + '/scripts/script.py', 'w') as file:
			file.write(script)

	def getScriptlog(self):
		with open(self.dir + '/script-error.log', 'r') as file:
			return file.read()

	def getSource(self):
		with open(self.dir + '/source', 'r') as file:
			return file.read().split('|')[0]

	def getRevision(self):
		with open(self.dir + '/source', 'r') as file:
			return file.read().split('|')[1]

for dir in os.listdir(config.prefix):
	server = Server(dir)
	if server.exists():
		servers[dir] = server
<|endoftext|>


<commit_before>#######################################################
# Copyright (c) 2015, ArrayFire
# All rights reserved.
#
# This file is distributed under 3-clause BSD license.
# The complete license agreement can be obtained at:
# http://arrayfire.com/licenses/BSD-3-Clause
########################################################

"""
module containing enums and other constants from arrayfire library
"""

import platform
import ctypes as ct

try:
    from enum import Enum as _Enum
    def _Enum_Type(v):
        return v
except:
    class _Enum(object):
        pass

    class _Enum_Type(object):
        def __init__(self, v):
            self.value = v

class ERR(_Enum):
    """
    Error values. For internal use only.
    """

    NONE            =   _Enum_Type(0)

    #100-199 Errors in environment
    NO_MEM         = _Enum_Type(101)
    DRIVER         = _Enum_Type(102)
    RUNTIME        = _Enum_Type(103)

    # 200-299 Errors in input parameters
    INVALID_ARRAY  = _Enum_Type(201)
    ARG            = _Enum_Type(202)
    SIZE           = _Enum_Type(203)
    TYPE           = _Enum_Type(204)
    DIFF_TYPE      = _Enum_Type(205)
    BATCH          = _Enum_Type(207)

    # 300-399 Errors for missing software features
    NOT_SUPPORTED  = _Enum_Type(301)
    NOT_CONFIGURED = _Enum_Type(302)
    NONFREE        = _Enum_Type(303)

    # 400-499 Errors for missing hardware features
    NO_DBL         = _Enum_Type(401)
    NO_GFX         = _Enum_Type(402)

    # 500-599 Errors specific to the heterogeneous API
    LOAD_LIB       = _Enum_Type(501)
    LOAD_SYM       = _Enum_Type(502)
    ARR_BKND_MISMATCH = _Enum_Type(503)

    # 900-999 Errors from upstream libraries and runtimes
    INTERNAL       = _Enum_Type(998)
    UNKNOWN        = _Enum_Type(999)

class Dtype(_Enum):
    """
    Error values. For internal use only.
    """
    f32 = _Enum_Type(0)
    c32 = _Enum_Type(1)
    f64 = _Enum_Type(2)
    c64 = _Enum_Type(3)
    b8  = _Enum_Type(4)
    s32 = _Enum_Type(5)
    u32 = _Enum_Type(6)
    u8  = _Enum_Type(7)
    s64 = _Enum_Type(8)
    u64 = _Enum_Type(9)
    s16 = _Enum_Type(10)
    u16 = _Enum_Type(11)

class Source(_Enum):
    """
    Source of the pointer
    """
    device = _Enum_Type(0)
    host   = _Enum_Type(1)

class INTERP(_Enum):
    """
    Interpolation method
    """
    NEAREST   = _Enum_Type(0)
    LINEAR    = _Enum_Type(1)
    BILINEAR  = _Enum_Type(2)
    CUBIC     = _Enum_Type(3)
    LOWER     = _Enum_Type(4)

class PAD(_Enum):
    """
    Edge padding types
    """
    ZERO = _Enum_Type(0)
    SYM  = _Enum_Type(1)

class CONNECTIVITY(_Enum):
    """
    Neighborhood connectivity
    """
    FOUR  = _Enum_Type(4)
    EIGHT = _Enum_Type(8)

class CONV_MODE(_Enum):
    """
    Convolution mode
    """
    DEFAULT = _Enum_Type(0)
    EXPAND  = _Enum_Type(1)

class CONV_DOMAIN(_Enum):
    """
    Convolution domain
    """
    AUTO    = _Enum_Type(0)
    SPATIAL = _Enum_Type(1)
    FREQ    = _Enum_Type(2)

class MATCH(_Enum):
    """
    Match type
    """

    """
    Sum of absolute differences
    """
    SAD  = _Enum_Type(0)

    """
    Zero mean SAD
    """
    ZSAD = _Enum_Type(1)

    """
    Locally scaled SAD
    """
    LSAD = _Enum_Type(2)

    """
    Sum of squared differences
    """
    SSD  = _Enum_Type(3)

    """
    Zero mean SSD
    """
    ZSSD = _Enum_Type(4)

    """
    Locally scaled SSD
    """
    LSSD = _Enum_Type(5)

    """
    Normalized cross correlation
    """
    NCC  = _Enum_Type(6)

    """
    Zero mean NCC
    """
    ZNCC = _Enum_Type(7)

    """
    Sum of hamming distances
    """
    SHD  = _Enum_Type(8)

class YCC_STD(_Enum):
    """
    YCC Standard formats
    """
    BT_601   = _Enum_Type(601)
    BT_709   = _Enum_Type(709)
    BT_2020  = _Enum_Type(2020)

class CSPACE(_Enum):
    """
    Colorspace formats
    """
    GRAY = _Enum_Type(0)
    RGB  = _Enum_Type(1)
    HSV  = _Enum_Type(2)
    YCbCr= _Enum_Type(3)

class MATPROP(_Enum):
    """
    Matrix properties
    """

    """
    None, general.
    """
    NONE       = _Enum_Type(0)

    """
    Transposed.
    """
    TRANS      = _Enum_Type(1)

    """
    Conjugate transposed.
    """
    CTRANS     = _Enum_Type(2)

    """
    Upper triangular matrix.
    """
    UPPER      = _Enum_Type(32)

    """
    Lower triangular matrix.
    """
    LOWER      = _Enum_Type(64)

    """
    Treat diagonal as units.
    """
    DIAG_UNIT  = _Enum_Type(128)

    """
    Symmetric matrix.
    """
    SYM        = _Enum_Type(512)

    """
    Positive definite matrix.
    """
    POSDEF     = _Enum_Type(1024)

    """
    Orthogonal matrix.
    """
    ORTHOG     = _Enum_Type(2048)

    """
    Tri diagonal matrix.
    """
    TRI_DIAG   = _Enum_Type(4096)

    """
    Block diagonal matrix.
    """
    BLOCK_DIAG = _Enum_Type(8192)

class NORM(_Enum):
    """
    Norm types
    """
    VECTOR_1    = _Enum_Type(0)
    VECTOR_INF  = _Enum_Type(1)
    VECTOR_2    = _Enum_Type(2)
    VECTOR_P    = _Enum_Type(3)
    MATRIX_1    = _Enum_Type(4)
    MATRIX_INF  = _Enum_Type(5)
    MATRIX_2    = _Enum_Type(6)
    MATRIX_L_PQ = _Enum_Type(7)
    EUCLID      = VECTOR_2

class COLORMAP(_Enum):
    """
    Colormaps
    """
    DEFAULT  = _Enum_Type(0)
    SPECTRUM = _Enum_Type(1)
    COLORS   = _Enum_Type(2)
    RED      = _Enum_Type(3)
    MOOD     = _Enum_Type(4)
    HEAT     = _Enum_Type(5)
    BLUE     = _Enum_Type(6)

class IMAGE_FORMAT(_Enum):
    """
    Image Formats
    """
    BMP      = _Enum_Type(0)
    ICO      = _Enum_Type(1)
    JPEG     = _Enum_Type(2)
    JNG      = _Enum_Type(3)
    PNG      = _Enum_Type(13)
    PPM      = _Enum_Type(14)
    PPMRAW   = _Enum_Type(15)
    TIFF     = _Enum_Type(18)
    PSD      = _Enum_Type(20)
    HDR      = _Enum_Type(26)
    EXR      = _Enum_Type(29)
    JP2      = _Enum_Type(31)
    RAW      = _Enum_Type(34)

class HOMOGRAPHY(_Enum):
    """
    Homography Types
    """
    RANSAC   = _Enum_Type(0)
    LMEDS    = _Enum_Type(1)

class BACKEND(_Enum):
    """
    Backend libraries
    """
    DEFAULT = _Enum_Type(0)
    CPU     = _Enum_Type(1)
    CUDA    = _Enum_Type(2)
    OPENCL  = _Enum_Type(4)

class _clibrary(object):

    def __libname(self, name):
        platform_name = platform.system()
        assert(len(platform_name) >= 3)

        libname = 'libaf' + name
        if platform_name == 'Linux':
            libname += '.so'
        elif platform_name == 'Darwin':
            libname += '.dylib'
        elif platform_name == "Windows" or platform_name[:3] == "CYG":
            libname += '.dll'
            libname = libname[3:] # remove 'lib'
            if platform_name == "Windows":
                '''
                Supressing crashes caused by missing dlls
                http://stackoverflow.com/questions/8347266/missing-dll-print-message-instead-of-launching-a-popup
                https://msdn.microsoft.com/en-us/library/windows/desktop/ms680621.aspx
                '''
                ct.windll.kernel32.SetErrorMode(0x0001 | 0x0002);
        else:
            raise OSError(platform_name + ' not supported')

        return libname

    def set_unsafe(self, name):
        lib = self.__clibs[name]
        if (lib is None):
            raise RuntimeError("Backend not found")
        self.__name = name

    def __init__(self):
        self.__name = None

        self.__clibs = {'cuda'   : None,
                        'opencl' : None,
                        'cpu'    : None,
                        ''       : None}

        self.__backend_map = {0 : 'default',
                              1 : 'cpu'    ,
                              2 : 'cuda'   ,
                              4 : 'opencl' }

        self.__backend_name_map = {'default' : 0,
                                   'cpu'     : 1,
                                   'cuda'    : 2,
                                   'opencl'  : 4}

        # Iterate in reverse order of preference
        for name in ('cpu', 'opencl', 'cuda', ''):
            try:
                libname = self.__libname(name)
                ct.cdll.LoadLibrary(libname)
                self.__clibs[name] = ct.CDLL(libname)
                self.__name = name
            except:
                pass

        if (self.__name is None):
            raise RuntimeError("Could not load any ArrayFire libraries.\n" +
                               "Please look at https://github.com/arrayfire/arrayfire-python/wiki for more information.")

    def get_id(self, name):
        return self.__backend_name_map[name]

    def get_name(self, bk_id):
        return self.__backend_map[bk_id]

    def get(self):
        return self.__clibs[self.__name]

    def name(self):
        return self.__name

    def is_unified(self):
        return self.__name == ''

    def parse(self, res):
        lst = []
        for key,value in self.__backend_name_map.items():
            if (value & res):
                lst.append(key)
        return tuple(lst)

backend = _clibrary()

def set_backend(name, unsafe=False):
    """
    Set a specific backend by name

    Parameters
    ----------

    name : str.

    unsafe : optional: bool. Default: False.
           If False, does not switch backend if current backend is not unified backend.
    """
    if (backend.is_unified() == False and unsanfe == False):
        raise RuntimeError("Can not change backend after loading %s" % name)

    if (backend.is_unified()):
        safe_call(backend.get().af_set_backend(backend.get_id(name)))
    else:
        backend.set_unsafe(name)
    return

def get_backend_id(A):
    """
    Get backend name of an array

    Parameters
    ----------
    A    : af.Array

    Returns
    ----------

    name : str.
         Backend name
    """
    if (backend.is_unified()):
        backend_id = ct.c_int(BACKEND.DEFAULT.value)
        safe_call(backend.get().af_get_backend_id(ct.pointer(backend_id), A.arr))
        return backend.get_name(backend_id.value)
    else:
        return backend.name()

def get_backend_count():
    """
    Get number of available backends

    Returns
    ----------

    count : int
          Number of available backends
    """
    if (backend.is_unified()):
        count = ct.c_int(0)
        safe_call(backend.get().af_get_backend_count(ct.pointer(count)))
        return count.value
    else:
        return 1

def get_available_backends():
    """
    Get names of available backends

    Returns
    ----------

    names : tuple of strings
          Names of available backends
    """
    if (backend.is_unified()):
        available = ct.c_int(0)
        safe_call(backend.get().af_get_available_backends(ct.pointer(available)))
        return backend.parse(int(available.value))
    else:
        return (backend.name(),)

from .util import safe_call
<commit_msg>Adding default search paths<commit_after>#######################################################
# Copyright (c) 2015, ArrayFire
# All rights reserved.
#
# This file is distributed under 3-clause BSD license.
# The complete license agreement can be obtained at:
# http://arrayfire.com/licenses/BSD-3-Clause
########################################################

"""
module containing enums and other constants from arrayfire library
"""

import platform
import ctypes as ct

try:
    from enum import Enum as _Enum
    def _Enum_Type(v):
        return v
except:
    class _Enum(object):
        pass

    class _Enum_Type(object):
        def __init__(self, v):
            self.value = v

class ERR(_Enum):
    """
    Error values. For internal use only.
    """

    NONE            =   _Enum_Type(0)

    #100-199 Errors in environment
    NO_MEM         = _Enum_Type(101)
    DRIVER         = _Enum_Type(102)
    RUNTIME        = _Enum_Type(103)

    # 200-299 Errors in input parameters
    INVALID_ARRAY  = _Enum_Type(201)
    ARG            = _Enum_Type(202)
    SIZE           = _Enum_Type(203)
    TYPE           = _Enum_Type(204)
    DIFF_TYPE      = _Enum_Type(205)
    BATCH          = _Enum_Type(207)

    # 300-399 Errors for missing software features
    NOT_SUPPORTED  = _Enum_Type(301)
    NOT_CONFIGURED = _Enum_Type(302)
    NONFREE        = _Enum_Type(303)

    # 400-499 Errors for missing hardware features
    NO_DBL         = _Enum_Type(401)
    NO_GFX         = _Enum_Type(402)

    # 500-599 Errors specific to the heterogeneous API
    LOAD_LIB       = _Enum_Type(501)
    LOAD_SYM       = _Enum_Type(502)
    ARR_BKND_MISMATCH = _Enum_Type(503)

    # 900-999 Errors from upstream libraries and runtimes
    INTERNAL       = _Enum_Type(998)
    UNKNOWN        = _Enum_Type(999)

class Dtype(_Enum):
    """
    Error values. For internal use only.
    """
    f32 = _Enum_Type(0)
    c32 = _Enum_Type(1)
    f64 = _Enum_Type(2)
    c64 = _Enum_Type(3)
    b8  = _Enum_Type(4)
    s32 = _Enum_Type(5)
    u32 = _Enum_Type(6)
    u8  = _Enum_Type(7)
    s64 = _Enum_Type(8)
    u64 = _Enum_Type(9)
    s16 = _Enum_Type(10)
    u16 = _Enum_Type(11)

class Source(_Enum):
    """
    Source of the pointer
    """
    device = _Enum_Type(0)
    host   = _Enum_Type(1)

class INTERP(_Enum):
    """
    Interpolation method
    """
    NEAREST   = _Enum_Type(0)
    LINEAR    = _Enum_Type(1)
    BILINEAR  = _Enum_Type(2)
    CUBIC     = _Enum_Type(3)
    LOWER     = _Enum_Type(4)

class PAD(_Enum):
    """
    Edge padding types
    """
    ZERO = _Enum_Type(0)
    SYM  = _Enum_Type(1)

class CONNECTIVITY(_Enum):
    """
    Neighborhood connectivity
    """
    FOUR  = _Enum_Type(4)
    EIGHT = _Enum_Type(8)

class CONV_MODE(_Enum):
    """
    Convolution mode
    """
    DEFAULT = _Enum_Type(0)
    EXPAND  = _Enum_Type(1)

class CONV_DOMAIN(_Enum):
    """
    Convolution domain
    """
    AUTO    = _Enum_Type(0)
    SPATIAL = _Enum_Type(1)
    FREQ    = _Enum_Type(2)

class MATCH(_Enum):
    """
    Match type
    """

    """
    Sum of absolute differences
    """
    SAD  = _Enum_Type(0)

    """
    Zero mean SAD
    """
    ZSAD = _Enum_Type(1)

    """
    Locally scaled SAD
    """
    LSAD = _Enum_Type(2)

    """
    Sum of squared differences
    """
    SSD  = _Enum_Type(3)

    """
    Zero mean SSD
    """
    ZSSD = _Enum_Type(4)

    """
    Locally scaled SSD
    """
    LSSD = _Enum_Type(5)

    """
    Normalized cross correlation
    """
    NCC  = _Enum_Type(6)

    """
    Zero mean NCC
    """
    ZNCC = _Enum_Type(7)

    """
    Sum of hamming distances
    """
    SHD  = _Enum_Type(8)

class YCC_STD(_Enum):
    """
    YCC Standard formats
    """
    BT_601   = _Enum_Type(601)
    BT_709   = _Enum_Type(709)
    BT_2020  = _Enum_Type(2020)

class CSPACE(_Enum):
    """
    Colorspace formats
    """
    GRAY = _Enum_Type(0)
    RGB  = _Enum_Type(1)
    HSV  = _Enum_Type(2)
    YCbCr= _Enum_Type(3)

class MATPROP(_Enum):
    """
    Matrix properties
    """

    """
    None, general.
    """
    NONE       = _Enum_Type(0)

    """
    Transposed.
    """
    TRANS      = _Enum_Type(1)

    """
    Conjugate transposed.
    """
    CTRANS     = _Enum_Type(2)

    """
    Upper triangular matrix.
    """
    UPPER      = _Enum_Type(32)

    """
    Lower triangular matrix.
    """
    LOWER      = _Enum_Type(64)

    """
    Treat diagonal as units.
    """
    DIAG_UNIT  = _Enum_Type(128)

    """
    Symmetric matrix.
    """
    SYM        = _Enum_Type(512)

    """
    Positive definite matrix.
    """
    POSDEF     = _Enum_Type(1024)

    """
    Orthogonal matrix.
    """
    ORTHOG     = _Enum_Type(2048)

    """
    Tri diagonal matrix.
    """
    TRI_DIAG   = _Enum_Type(4096)

    """
    Block diagonal matrix.
    """
    BLOCK_DIAG = _Enum_Type(8192)

class NORM(_Enum):
    """
    Norm types
    """
    VECTOR_1    = _Enum_Type(0)
    VECTOR_INF  = _Enum_Type(1)
    VECTOR_2    = _Enum_Type(2)
    VECTOR_P    = _Enum_Type(3)
    MATRIX_1    = _Enum_Type(4)
    MATRIX_INF  = _Enum_Type(5)
    MATRIX_2    = _Enum_Type(6)
    MATRIX_L_PQ = _Enum_Type(7)
    EUCLID      = VECTOR_2

class COLORMAP(_Enum):
    """
    Colormaps
    """
    DEFAULT  = _Enum_Type(0)
    SPECTRUM = _Enum_Type(1)
    COLORS   = _Enum_Type(2)
    RED      = _Enum_Type(3)
    MOOD     = _Enum_Type(4)
    HEAT     = _Enum_Type(5)
    BLUE     = _Enum_Type(6)

class IMAGE_FORMAT(_Enum):
    """
    Image Formats
    """
    BMP      = _Enum_Type(0)
    ICO      = _Enum_Type(1)
    JPEG     = _Enum_Type(2)
    JNG      = _Enum_Type(3)
    PNG      = _Enum_Type(13)
    PPM      = _Enum_Type(14)
    PPMRAW   = _Enum_Type(15)
    TIFF     = _Enum_Type(18)
    PSD      = _Enum_Type(20)
    HDR      = _Enum_Type(26)
    EXR      = _Enum_Type(29)
    JP2      = _Enum_Type(31)
    RAW      = _Enum_Type(34)

class HOMOGRAPHY(_Enum):
    """
    Homography Types
    """
    RANSAC   = _Enum_Type(0)
    LMEDS    = _Enum_Type(1)

class BACKEND(_Enum):
    """
    Backend libraries
    """
    DEFAULT = _Enum_Type(0)
    CPU     = _Enum_Type(1)
    CUDA    = _Enum_Type(2)
    OPENCL  = _Enum_Type(4)

def _setup():
    import platform
    import os

    platform_name = platform.system()

    try:
        AF_SEARCH_PATH = os.environ['AF_PATH']
    except:
        AF_SEARCH_PATH = None
        pass

    try:
        CUDA_PATH = os.environ['CUDA_PATH']
    except:
        CUDA_PATH= None
        pass

    CUDA_EXISTS = False

    assert(len(platform_name) >= 3)
    if platform_name == 'Windows' or platform_name[:3] == 'CYG':

        ## Windows specific setup
        pre = ''
        post = '.dll'
        if platform_name == "Windows":
            '''
            Supressing crashes caused by missing dlls
            http://stackoverflow.com/questions/8347266/missing-dll-print-message-instead-of-launching-a-popup
            https://msdn.microsoft.com/en-us/library/windows/desktop/ms680621.aspx
            '''
            ct.windll.kernel32.SetErrorMode(0x0001 | 0x0002)

        if AF_SEARCH_PATH is None:
            AF_SEARCH_PATH="C:/Program Files/ArrayFire/v3/"

        if CUDA_PATH is not None:
            CUDA_EXISTS = os.path.isdir(CUDA_PATH + '/bin') and os.path.isdir(CUDA_PATH + '/nvvm/bin/')

    elif platform_name == 'Darwin':

        ## OSX specific setup
        pre = 'lib'
        post = '.dylib'

        if AF_SEARCH_PATH is None:
            AF_SEARCH_PATH='/usr/local/'

        if CUDA_PATH is None:
            CUDA_PATH='/usr/local/cuda/'

        CUDA_EXISTS = os.path.isdir(CUDA_PATH + '/lib') and os.path.isdir(CUDA_PATH + '/nvvm/lib')

    elif platform_name == 'Linux':
        pre = 'lib'
        post = '.so'

        if AF_SEARCH_PATH is None:
            AF_SEARCH_PATH='/opt/arrayfire-3/'

        if CUDA_PATH is None:
            CUDA_PATH='/usr/local/cuda/'

        if platform.architecture()[0][:2] == 64:
            CUDA_EXISTS = os.path.isdir(CUDA_PATH + '/lib64') and os.path.isdir(CUDA_PATH + '/nvvm/lib64')
        else:
            CUDA_EXISTS = os.path.isdir(CUDA_PATH + '/lib') and os.path.isdir(CUDA_PATH + '/nvvm/lib')
    else:
        raise OSError(platform_name + ' not supported')

    return pre, post, AF_SEARCH_PATH, CUDA_EXISTS

class _clibrary(object):

    def __libname(self, name, head='af'):
        libname = self.__pre + head + name + self.__post
        libname_full = self.AF_SEARCH_PATH + '/lib/' + libname
        return (libname, libname_full)

    def set_unsafe(self, name):
        lib = self.__clibs[name]
        if (lib is None):
            raise RuntimeError("Backend not found")
        self.__name = name

    def __init__(self):

        more_info_str = "Please look at https://github.com/arrayfire/arrayfire-python/wiki for more information."

        pre, post, AF_SEARCH_PATH, CUDA_EXISTS = _setup()

        self.__pre = pre
        self.__post = post
        self.AF_SEARCH_PATH = AF_SEARCH_PATH

        self.__name = None

        self.__clibs = {'cuda'   : None,
                        'opencl' : None,
                        'cpu'    : None,
                        ''       : None}

        self.__backend_map = {0 : 'default',
                              1 : 'cpu'    ,
                              2 : 'cuda'   ,
                              4 : 'opencl' }

        self.__backend_name_map = {'default' : 0,
                                   'cpu'     : 1,
                                   'cuda'    : 2,
                                   'opencl'  : 4}

        # Try to pre-load forge library if it exists
        libnames = self.__libname('forge', '')
        for libname in libnames:
            try:
                ct.cdll.LoadLibrary(libname)
            except:
                pass

        # Iterate in reverse order of preference
        for name in ('cpu', 'opencl', 'cuda', ''):
            libnames = self.__libname(name)
            for libname in libnames:
                try:
                    ct.cdll.LoadLibrary(libname)
                    self.__clibs[name] = ct.CDLL(libname)
                    self.__name = name
                    break;
                except:
                    pass

        if (self.__name is None):
            raise RuntimeError("Could not load any ArrayFire libraries.\n" +
                               more_info_str)

    def get_id(self, name):
        return self.__backend_name_map[name]

    def get_name(self, bk_id):
        return self.__backend_map[bk_id]

    def get(self):
        return self.__clibs[self.__name]

    def name(self):
        return self.__name

    def is_unified(self):
        return self.__name == ''

    def parse(self, res):
        lst = []
        for key,value in self.__backend_name_map.items():
            if (value & res):
                lst.append(key)
        return tuple(lst)

backend = _clibrary()

def set_backend(name, unsafe=False):
    """
    Set a specific backend by name

    Parameters
    ----------

    name : str.

    unsafe : optional: bool. Default: False.
           If False, does not switch backend if current backend is not unified backend.
    """
    if (backend.is_unified() == False and unsanfe == False):
        raise RuntimeError("Can not change backend after loading %s" % name)

    if (backend.is_unified()):
        safe_call(backend.get().af_set_backend(backend.get_id(name)))
    else:
        backend.set_unsafe(name)
    return

def get_backend_id(A):
    """
    Get backend name of an array

    Parameters
    ----------
    A    : af.Array

    Returns
    ----------

    name : str.
         Backend name
    """
    if (backend.is_unified()):
        backend_id = ct.c_int(BACKEND.DEFAULT.value)
        safe_call(backend.get().af_get_backend_id(ct.pointer(backend_id), A.arr))
        return backend.get_name(backend_id.value)
    else:
        return backend.name()

def get_backend_count():
    """
    Get number of available backends

    Returns
    ----------

    count : int
          Number of available backends
    """
    if (backend.is_unified()):
        count = ct.c_int(0)
        safe_call(backend.get().af_get_backend_count(ct.pointer(count)))
        return count.value
    else:
        return 1

def get_available_backends():
    """
    Get names of available backends

    Returns
    ----------

    names : tuple of strings
          Names of available backends
    """
    if (backend.is_unified()):
        available = ct.c_int(0)
        safe_call(backend.get().af_get_available_backends(ct.pointer(available)))
        return backend.parse(int(available.value))
    else:
        return (backend.name(),)

from .util import safe_call
<|endoftext|>


<commit_before>#!/usr/bin/env python
from argparse import ArgumentParser
import os
import re
import subprocess
from textwrap import dedent
from utility import s3_cmd
import yaml

def win_test(script_dir, address, juju_home, revision_build):
    host = 'Administrator@{}'.format(address)
    private_key = os.path.join(juju_home, 'staging-juju-rsa')
    revision_build_url = (
        's3://juju-qa-data/juju-ci/products/version-{}'.format(revision_build))
    win_client_url = '{}/build-win-client/'.format(revision_build_url)
    output = s3_cmd(['ls', '-r', win_client_url])
    urls = sorted(l.split()[3] for l in output.splitlines())
    installer = [u for u in urls if re.search('juju-setup-.*\.exe', u)][-1]
    s3_cmd(['sync', installer, '.'])
    install_file = installer.split('/')[-1]
    with open('run-file', 'w') as run_file:
        run_file.write(dedent("""
            ci/$1 /verysilent
            juju version
            juju destroy-environment --force -y win-client-deploy
            mkdir logs
            python ci\\\\deploy_job.py parallel-win-client \
                'c:\\Program Files (x86)\\Juju\\juju.exe' \
                logs win-client-deploy --series trusty \
                --agent-stream revision-build-{revision_build}
            """.format(revision_build=revision_build)))

    ci = [os.path.join(script_dir, f) for f in [
        'deploy_stack.py', 'deploy_job.py', 'jujupy.py', 'jujuconfig.py',
        'remote.py', 'substrate.py', 'utility.py', 'get_ami.py', 'chaos.py',
        'timeout.py', 'jujucharm.py',
        ]]
    ci.extend([install_file, 'run-file'])
    with open('foo.yaml', 'w') as config:
        yaml.dump({
            'install': {'ci': ci},
            'command': ['ci/run-file', install_file],
            }, config)
    subprocess.check_call(['workspace-run', '-v', 'foo.yaml', host, '-i',
                           private_key])

def main():
    parser = ArgumentParser()
    parser.add_argument('address',
                        help='The IP or DNS address the windows test machine.')
    parser.add_argument('revision_build',
                        help='Revision-build to test.')
    parser.add_argument(
        '--juju-home', default=os.environ.get('JUJU_HOME'),
        help='The location of cloud-city and staging-juju-rsa.')
    script_dir = os.path.dirname(__file__)
    win_test(script_dir=script_dir, **parser.parse_args().__dict__)

if __name__ == '__main__':
    main()
<commit_msg>Added missing dep for win deploy tests.<commit_after>#!/usr/bin/env python
from argparse import ArgumentParser
import os
import re
import subprocess
from textwrap import dedent
from utility import s3_cmd
import yaml

def win_test(script_dir, address, juju_home, revision_build):
    host = 'Administrator@{}'.format(address)
    private_key = os.path.join(juju_home, 'staging-juju-rsa')
    revision_build_url = (
        's3://juju-qa-data/juju-ci/products/version-{}'.format(revision_build))
    win_client_url = '{}/build-win-client/'.format(revision_build_url)
    output = s3_cmd(['ls', '-r', win_client_url])
    urls = sorted(l.split()[3] for l in output.splitlines())
    installer = [u for u in urls if re.search('juju-setup-.*\.exe', u)][-1]
    s3_cmd(['sync', installer, '.'])
    install_file = installer.split('/')[-1]
    with open('run-file', 'w') as run_file:
        run_file.write(dedent("""
            ci/$1 /verysilent
            juju version
            juju destroy-environment --force -y win-client-deploy
            mkdir logs
            python ci\\\\deploy_job.py parallel-win-client \
                'c:\\Program Files (x86)\\Juju\\juju.exe' \
                logs win-client-deploy --series trusty \
                --agent-stream revision-build-{revision_build}
            """.format(revision_build=revision_build)))

    ci = [os.path.join(script_dir, f) for f in [
        'deploy_stack.py', 'deploy_job.py', 'jujupy.py', 'jujuconfig.py',
        'remote.py', 'substrate.py', 'utility.py', 'get_ami.py', 'chaos.py',
        'timeout.py', 'jujucharm.py', 'tests/test_jujupy.py',
        ]]
    ci.extend([install_file, 'run-file'])
    with open('foo.yaml', 'w') as config:
        yaml.dump({
            'install': {'ci': ci},
            'command': ['ci/run-file', install_file],
            }, config)
    subprocess.check_call(['workspace-run', '-v', 'foo.yaml', host, '-i',
                           private_key])

def main():
    parser = ArgumentParser()
    parser.add_argument('address',
                        help='The IP or DNS address the windows test machine.')
    parser.add_argument('revision_build',
                        help='Revision-build to test.')
    parser.add_argument(
        '--juju-home', default=os.environ.get('JUJU_HOME'),
        help='The location of cloud-city and staging-juju-rsa.')
    script_dir = os.path.dirname(__file__)
    win_test(script_dir=script_dir, **parser.parse_args().__dict__)

if __name__ == '__main__':
    main()
<|endoftext|>


<commit_before>"""Schevo database repair functions.

For copyright, license, and warranty, see bottom of file.
"""

import sys
from schevo.lib import optimize

import schevo.database

def repairs_needed(db, db_filename):
    """Return a list of repair types that should be performed in order to
    repair the given database."""
    needed = []
    for repair_type in [
        EntityFieldIdsRepair,
        ]:
        repair = repair_type(db, db_filename)
        if repair.is_needed:
            needed.append(repair)
    return needed

class EntityFieldIdsRepair(object):

    description = 'Remove entity_field_ids extraneous field IDs. (No data loss)'

    def __init__(self, db, db_filename):
        self.db = db
        self.db_filename = db_filename
        self._determine_if_needed()

    def perform(self):
        # Database starts out closed.
        db_filename = self.db_filename
        db = schevo.database.open(db_filename)
        try:
            try:
                items = self._extents_extraneous.items()
                for (extent_name, extraneous_field_ids) in items:
                    extent_map = db._extent_map(extent_name)
                    # For all formats, remove extraneouse field IDs from
                    # entity_field_ids.
                    entity_field_ids = set(extent_map['entity_field_ids'])
                    entity_field_ids -= extraneous_field_ids
                    extent_map['entity_field_ids'] = tuple(entity_field_ids)
                    # For format 2, also iterate over each entity in the
                    # extent and remove extraneous related_entities sets.
                    if db.format == 2:
                        for entity_map in extent_map['entities'].itervalues():
                            related_entities = entity_map['related_entities']
                            for field_id in extraneous_field_ids:
                                del related_entities[field_id]
                db._commit()
            except:
                db._rollback()
                raise
        finally:
            db.close()

    def _determine_if_needed(self):
        db = self.db
        self.is_needed = False
        extents_extraneous = self._extents_extraneous = {
            # extent_name: set([extraneous, field, ids]),
            }
        for extent in db.extents():
            extent_map = db._extent_map(extent.name)
            field_id_name = extent_map['field_id_name']
            entity_field_ids = extent_map['entity_field_ids']
            for entity_field_id in entity_field_ids:
                field_name = field_id_name[entity_field_id]
                FieldClass = extent.f[field_name]
                if FieldClass.fget is not None:
                    # Calculated field found in entity_field_ids,
                    # database needs repair.
                    self.is_needed = True
                    extraneous_field_ids = extents_extraneous.setdefault(
                        extent.name, set())
                    extraneous_field_ids.add(entity_field_id)

optimize.bind_all(sys.modules[__name__])  # Last line of module.

# Copyright (C) 2001-2007 Orbtech, L.L.C.
#
# Schevo
# http://schevo.org/
#
# Orbtech
# Saint Louis, MO
# http://orbtech.com/
#
# This toolkit is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This toolkit is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
<commit_msg>Skip over fields where no related entity information had been kept.<commit_after>"""Schevo database repair functions.

For copyright, license, and warranty, see bottom of file.
"""

import sys
from schevo.lib import optimize

import schevo.database

def repairs_needed(db, db_filename):
    """Return a list of repair types that should be performed in order to
    repair the given database."""
    needed = []
    for repair_type in [
        EntityFieldIdsRepair,
        ]:
        repair = repair_type(db, db_filename)
        if repair.is_needed:
            needed.append(repair)
    return needed

class EntityFieldIdsRepair(object):

    description = 'Remove entity_field_ids extraneous field IDs. (No data loss)'

    def __init__(self, db, db_filename):
        self.db = db
        self.db_filename = db_filename
        self._determine_if_needed()

    def perform(self):
        # Database starts out closed.
        db_filename = self.db_filename
        db = schevo.database.open(db_filename)
        try:
            try:
                items = self._extents_extraneous.items()
                for (extent_name, extraneous_field_ids) in items:
                    extent_map = db._extent_map(extent_name)
                    # For all formats, remove extraneouse field IDs from
                    # entity_field_ids.
                    entity_field_ids = set(extent_map['entity_field_ids'])
                    entity_field_ids -= extraneous_field_ids
                    extent_map['entity_field_ids'] = tuple(entity_field_ids)
                    # For format 2, also iterate over each entity in the
                    # extent and remove extraneous related_entities sets.
                    if db.format == 2:
                        for entity_map in extent_map['entities'].itervalues():
                            related_entities = entity_map['related_entities']
                            for field_id in extraneous_field_ids:
                                if field_id in related_entities:
                                    del related_entities[field_id]
                db._commit()
            except:
                db._rollback()
                raise
        finally:
            db.close()

    def _determine_if_needed(self):
        db = self.db
        self.is_needed = False
        extents_extraneous = self._extents_extraneous = {
            # extent_name: set([extraneous, field, ids]),
            }
        for extent in db.extents():
            extent_map = db._extent_map(extent.name)
            field_id_name = extent_map['field_id_name']
            entity_field_ids = extent_map['entity_field_ids']
            for entity_field_id in entity_field_ids:
                field_name = field_id_name[entity_field_id]
                FieldClass = extent.f[field_name]
                if FieldClass.fget is not None:
                    # Calculated field found in entity_field_ids,
                    # database needs repair.
                    self.is_needed = True
                    extraneous_field_ids = extents_extraneous.setdefault(
                        extent.name, set())
                    extraneous_field_ids.add(entity_field_id)

optimize.bind_all(sys.modules[__name__])  # Last line of module.

# Copyright (C) 2001-2007 Orbtech, L.L.C.
#
# Schevo
# http://schevo.org/
#
# Orbtech
# Saint Louis, MO
# http://orbtech.com/
#
# This toolkit is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This toolkit is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
<|endoftext|>


<commit_before>def is_hyper_scientific(number):
    """ Determines if an answer is hyper-scientific
    Args:
        number (String)
    Returns:
        bool: True if is hyper-scientific, False otherwise
    Example:
    >>> is_hyper_scientific("1.00e2")
    True
    >>> is_hyper_scientific("100")
    False
    >>> is_hyper_scientific("1.234e5")
    False
    """
    if convert_to_scientific_notation(float(number)) == number:
        return True
    return False

def convert_to_scientific_notation(number):
    """ Converts a number to scientific notation
    Args:
         number (float)

    Returns:
         String

    Example:
    >>> convert_to_scientific_notation(5)
    '5.00e0'
    >>> convert_to_scientific_notation(10)
    '1.00e1'
    >>> convert_to_scientific_notation(-100)
    '-1.00e2'
    >>> convert_to_scientific_notation(0.01)
    '1.00e-2'
    """
    try:
        float(number)
    except TypeError as te:
        raise TypeError("That was not a number")
    number = "%.2e" % number
    if "+" in number:
        positive = True
        number, exponent = number.split("+")
    else:
        positive = False
        number, exponent = number.split("-")

    exponent = str(int(exponent))  # Removes leading zeros

    if positive:
        return number + exponent
    else:
        return number + "-" + exponent

def is_acceptable_answer(correct_answer, response):
    """ Calculates if an answer is an acceptable distance from the correct answer
    Args:
        correct_answer (float)
        response (String)

    Returns:
        bool: True of the answer is an acceptable answer, false otherwise

    Examples:
    >>> is_acceptable_answer(100, '1.00e2')
    True
    >>> is_acceptable_answer(100, '1.01e2')
    True
    >>> is_acceptable_answer(100, '1.02e2')
    False
    """

    try:
        correct_answer = convert_to_scientific_notation(correct_answer)
    except TypeError as te:
        print(te.args)
    number, exponent = correct_answer.split("e")
    number = float(number)
    max_number = number + .01
    min_number = number - .01
    max_number = str(max_number) + "e" + exponent
    min_number = str(min_number) + "e" + exponent

    min_number = float(min_number)
    correct_answer = float(correct_answer)
    max_number = float(max_number)

    if min_number <= float(response) <= max_number:
        return True

    return False
<commit_msg>Replace TypeError with ValueError<commit_after>def is_hyper_scientific(number):
    """ Determines if an answer is hyper-scientific
    Args:
        number (String)
    Returns:
        bool: True if is hyper-scientific, False otherwise
    Example:
    >>> is_hyper_scientific("1.00e2")
    True
    >>> is_hyper_scientific("100")
    False
    >>> is_hyper_scientific("1.234e5")
    False
    """
    if convert_to_scientific_notation(float(number)) == number:
        return True
    return False

def convert_to_scientific_notation(number):
    """ Converts a number to scientific notation
    Args:
         number (float)

    Returns:
         String

    Example:
    >>> convert_to_scientific_notation(5)
    '5.00e0'
    >>> convert_to_scientific_notation(10)
    '1.00e1'
    >>> convert_to_scientific_notation(-100)
    '-1.00e2'
    >>> convert_to_scientific_notation(0.01)
    '1.00e-2'
    """
    try:
        float(number)
    except ValueError:
        raise ValueError("That was not a number")

    number = "%.2e" % number
    if "+" in number:
        positive = True
        number, exponent = number.split("+")
    else:
        positive = False
        number, exponent = number.split("-")

    exponent = str(int(exponent))  # Removes leading zeros

    if positive:
        return number + exponent
    else:
        return number + "-" + exponent

def is_acceptable_answer(correct_answer, response):
    """ Calculates if an answer is an acceptable distance from the correct answer
    Args:
        correct_answer (float)
        response (String)

    Returns:
        bool: True of the answer is an acceptable answer, false otherwise

    Examples:
    >>> is_acceptable_answer(100, '1.00e2')
    True
    >>> is_acceptable_answer(100, '1.01e2')
    True
    >>> is_acceptable_answer(100, '1.02e2')
    False
    """

    # TODO Handle errors better with loop
    try:
        correct_answer = convert_to_scientific_notation(correct_answer)
    except ValueError as ve:
        print(ve.args)

    number, exponent = correct_answer.split("e")
    number = float(number)
    max_number = number + .01
    min_number = number - .01
    max_number = str(max_number) + "e" + exponent
    min_number = str(min_number) + "e" + exponent

    min_number = float(min_number)
    correct_answer = float(correct_answer)
    max_number = float(max_number)

    if min_number <= float(response) <= max_number:
        return True

    return False
<|endoftext|>


<commit_before>import logging

import os
import numpy as np
import tempfile
from rdkit import Chem
from rdkit.Chem import AllChem
from pdbfixer import PDBFixer
from simtk.openmm.app import PDBFile

class MoleculeLoadException(Exception):

  def __init__(self, *args, **kwargs):
    Exception.__init__(*args, **kwargs)

def get_xyz_from_mol(mol):
  """
  returns an m x 3 np array of 3d coords
  of given rdkit molecule
  """
  xyz = np.zeros((mol.GetNumAtoms(), 3))
  conf = mol.GetConformer()
  for i in range(conf.GetNumAtoms()):
    position = conf.GetAtomPosition(i)
    xyz[i, 0] = position.x
    xyz[i, 1] = position.y
    xyz[i, 2] = position.z
  return (xyz)

def add_hydrogens_f(mol):
  molecule_file = str(tempfile.NamedTemporaryFile().name)
  Chem.MolToPDBFile(mol, molecule_file)
  try:
    fixer = PDBFixer(filename=molecule_file)
    fixer.addMissingHydrogens(7.4)
    PDBFile.writeFile(fixer.topology, fixer.positions, open(molecule_file, 'w'))
  except ValueError as e:
    print(e)
    raise MoleculeLoadException(e)
  return Chem.MolFromPDBFile(str(molecule_file), sanitize=False, removeHs=False)

def compute_charges(mol):
  try:
    AllChem.ComputeGasteigerCharges(mol)
  except Exception as e:
    logging.exception("Unable to compute charges for mol")
    raise MoleculeLoadException(e)
  return mol

def load_molecule(molecule_file, add_hydrogens=True, calc_charges=True):
  """Converts molecule file to (xyz-coords, obmol object)

  Given molecule_file, returns a tuple of xyz coords of molecule
  and an rdkit object representing that molecule
  """
  if ".mol2" in molecule_file or ".sdf" in molecule_file:
    suppl = Chem.SDMolSupplier(str(molecule_file), sanitize=False)
    my_mol = suppl[0]
  elif ".pdbqt" in molecule_file:
    raise MoleculeLoadException("Don't support pdbqt files yet")
  elif ".pdb" in molecule_file:
    my_mol = Chem.MolFromPDBFile(
        str(molecule_file), sanitize=False, removeHs=False)
  else:
    raise ValueError("Unrecognized file type")

  if my_mol is None:
    raise ValueError("Unable to read non None Molecule Object")

  if calc_charges:
    my_mol = add_hydrogens_f(my_mol)
    compute_charges(my_mol)
  elif add_hydrogens:
    my_mol = add_hydrogens_f(my_mol)
    compute_charges(my_mol)

  xyz = get_xyz_from_mol(my_mol)

  return xyz, my_mol

def write_molecule(mol, outfile):
  if ".pdbqt" in outfile:
    # TODO (LESWING) create writer for pdbqt which includes charges
    writer = Chem.PDBWriter(outfile)
    writer.write(mol)
    writer.close()
    pass
  elif ".pdb" in outfile:
    writer = Chem.PDBWriter(outfile)
    writer.write(mol)
    writer.close()
  else:
    raise ValueError("Unsupported Format")

def pdbqt_to_pdb(filename):
  base_filename = os.path.splitext(filename)[0]
  pdb_filename = base_filename + ".pdb"
  pdbqt_data = open(filename).readlines()
  with open(pdb_filename, 'w') as fout:
    for line in pdbqt_data:
      fout.write("%s\n" % line[:66])
  return pdb_filename
<commit_msg>Renames and PR cleanups<commit_after>import logging

import os
import numpy as np
import tempfile
import shutil
from rdkit import Chem
from rdkit.Chem import AllChem
from pdbfixer import PDBFixer
from simtk.openmm.app import PDBFile

class MoleculeLoadException(Exception):

  def __init__(self, *args, **kwargs):
    Exception.__init__(*args, **kwargs)

def get_xyz_from_mol(mol):
  """
  returns an m x 3 np array of 3d coords
  of given rdkit molecule
  """
  xyz = np.zeros((mol.GetNumAtoms(), 3))
  conf = mol.GetConformer()
  for i in range(conf.GetNumAtoms()):
    position = conf.GetAtomPosition(i)
    xyz[i, 0] = position.x
    xyz[i, 1] = position.y
    xyz[i, 2] = position.z
  return (xyz)

def add_hydrogens_to_mol(mol):
  molecule_file = None
  try:
    molecule_file = str(tempfile.NamedTemporaryFile().name)
    Chem.MolToPDBFile(mol, molecule_file)
    fixer = PDBFixer(filename=molecule_file)
    fixer.addMissingHydrogens(7.4)
    PDBFile.writeFile(fixer.topology, fixer.positions, open(molecule_file, 'w'))
    return Chem.MolFromPDBFile(
        str(molecule_file), sanitize=False, removeHs=False)
  except ValueError as e:
    logging.warning("Unable to add hydrogens", e)
    raise MoleculeLoadException(e)
  finally:
    try:
      os.remove(molecule_file)
    except (OSError, TypeError):
      pass

def compute_charges(mol):
  try:
    AllChem.ComputeGasteigerCharges(mol)
  except Exception as e:
    logging.exception("Unable to compute charges for mol")
    raise MoleculeLoadException(e)
  return mol

def load_molecule(molecule_file, add_hydrogens=True, calc_charges=True):
  """Converts molecule file to (xyz-coords, obmol object)

  Given molecule_file, returns a tuple of xyz coords of molecule
  and an rdkit object representing that molecule
  """
  if ".mol2" in molecule_file or ".sdf" in molecule_file:
    suppl = Chem.SDMolSupplier(str(molecule_file), sanitize=False)
    my_mol = suppl[0]
  elif ".pdbqt" in molecule_file:
    raise MoleculeLoadException("Don't support pdbqt files yet")
  elif ".pdb" in molecule_file:
    my_mol = Chem.MolFromPDBFile(
        str(molecule_file), sanitize=False, removeHs=False)
  else:
    raise ValueError("Unrecognized file type")

  if my_mol is None:
    raise ValueError("Unable to read non None Molecule Object")

  if calc_charges:
    my_mol = add_hydrogens_to_mol(my_mol)
    compute_charges(my_mol)
  elif add_hydrogens:
    my_mol = add_hydrogens_to_mol(my_mol)
    compute_charges(my_mol)

  xyz = get_xyz_from_mol(my_mol)

  return xyz, my_mol

def write_molecule(mol, outfile):
  if ".pdbqt" in outfile:
    # TODO (LESWING) create writer for pdbqt which includes charges
    writer = Chem.PDBWriter(outfile)
    writer.write(mol)
    writer.close()
    pass
  elif ".pdb" in outfile:
    writer = Chem.PDBWriter(outfile)
    writer.write(mol)
    writer.close()
  else:
    raise ValueError("Unsupported Format")

def pdbqt_to_pdb(filename):
  base_filename = os.path.splitext(filename)[0]
  pdb_filename = base_filename + ".pdb"
  pdbqt_data = open(filename).readlines()
  with open(pdb_filename, 'w') as fout:
    for line in pdbqt_data:
      fout.write("%s\n" % line[:66])
  return pdb_filename
<|endoftext|>


<commit_before>import os

import numpy as np
import tensorflow as tf

IMG_MEAN = np.array((104.00698793,116.66876762,122.67891434), dtype=np.float32)

def read_labeled_image_list(data_dir, data_list):
    """Reads txt file containing paths to images and ground truth masks.

    Args:
      data_dir: path to the directory with images and masks.
      data_list: path to the file with lines of the form '/path/to/image /path/to/mask'.

    Returns:
      Two lists with all file names for images and masks, respectively.
    """
    f = open(data_list, 'r')
    images = []
    masks = []
    for line in f:
        image, mask = line.strip("\n").split(' ')
        images.append(data_dir + image)
        masks.append(data_dir + mask)
    return images, masks

def read_images_from_disk(input_queue, input_size, random_scale):
    """Read one image and its corresponding mask with optional pre-processing.

    Args:
      input_queue: tf queue with paths to the image and its mask.
      input_size: a tuple with (height, width) values.
                  If not given, return images of original size.
      random_scale: whether to randomly scale the images prior
                    to random crop.

    Returns:
      Two tensors: the decoded image and its mask.
    """
    img_contents = tf.read_file(input_queue[0])
    label_contents = tf.read_file(input_queue[1])

    img = tf.image.decode_jpeg(img_contents, channels=3)
    label = tf.image.decode_png(label_contents, channels=1)
    if input_size is not None:
        h, w = input_size
        if random_scale:
            scale = tf.random_uniform([1], minval=0.75, maxval=1.25, dtype=tf.float32, seed=None)
            h_new = tf.to_int32(tf.mul(tf.to_float(tf.shape(img)[1]), scale))
            w_new = tf.to_int32(tf.mul(tf.to_float(tf.shape(img)[1]), scale))
            new_shape = tf.squeeze(tf.pack([h_new, w_new]), squeeze_dims=[1])
            img = tf.image.resize_images(img, new_shape)
            label = tf.image.resize_nearest_neighbor(tf.expand_dims(label, 0), new_shape)
            label = tf.squeeze(label, squeeze_dims=[0]) # resize_image_with_crop_or_pad accepts 3D-tensor.
        img = tf.image.resize_image_with_crop_or_pad(img, h, w)
        label = tf.image.resize_image_with_crop_or_pad(label, h, w)
    # RGB -> BGR.
    img_r, img_g, img_b = tf.split(split_dim=2, num_split=3, value=img)
    img = tf.cast(tf.concat(2, [img_b, img_g, img_r]), dtype=tf.float32)
    # Extract mean.
    img -= IMG_MEAN
    return img, label

class ImageReader(object):
    '''Generic ImageReader which reads images and corresponding segmentation
       masks from the disk, and enqueues them into a TensorFlow queue.
    '''

    def __init__(self, data_dir, data_list, input_size, random_scale, coord):
        '''Initialise an ImageReader.

        Args:
          data_dir: path to the directory with images and masks.
          data_list: path to the file with lines of the form '/path/to/image /path/to/mask'.
          input_size: a tuple with (height, width) values, to which all the images will be resized.
          random_scale: whether to randomly scale the images prior to random crop.
          coord: TensorFlow queue coordinator.
        '''
        self.data_dir = data_dir
        self.data_list = data_list
        self.input_size = input_size
        self.coord = coord

        self.image_list, self.label_list = read_labeled_image_list(self.data_dir, self.data_list)
        self.images = tf.convert_to_tensor(self.image_list, dtype=tf.string)
        self.labels = tf.convert_to_tensor(self.label_list, dtype=tf.string)
        self.queue = tf.train.slice_input_producer([self.images, self.labels],
                                                   shuffle=input_size is not None) # Not shuffling if it is val.
        self.image, self.label = read_images_from_disk(self.queue, self.input_size, random_scale)

    def dequeue(self, num_elements):
        '''Pack images and labels into a batch.

        Args:
          num_elements: the batch size.

        Returns:
          Two tensors of size (batch_size, h, w, {3,1}) for images and masks.'''
        image_batch, label_batch = tf.train.batch([self.image, self.label],
                                                  num_elements)
        return image_batch, label_batch
<commit_msg>Update image_reader.py<commit_after>import os

import numpy as np
import tensorflow as tf

IMG_MEAN = np.array((104.00698793,116.66876762,122.67891434), dtype=np.float32)

def read_labeled_image_list(data_dir, data_list):
    """Reads txt file containing paths to images and ground truth masks.

    Args:
      data_dir: path to the directory with images and masks.
      data_list: path to the file with lines of the form '/path/to/image /path/to/mask'.

    Returns:
      Two lists with all file names for images and masks, respectively.
    """
    f = open(data_list, 'r')
    images = []
    masks = []
    for line in f:
        image, mask = line.strip("\n").split(' ')
        images.append(data_dir + image)
        masks.append(data_dir + mask)
    return images, masks

def read_images_from_disk(input_queue, input_size, random_scale):
    """Read one image and its corresponding mask with optional pre-processing.

    Args:
      input_queue: tf queue with paths to the image and its mask.
      input_size: a tuple with (height, width) values.
                  If not given, return images of original size.
      random_scale: whether to randomly scale the images prior
                    to random crop.

    Returns:
      Two tensors: the decoded image and its mask.
    """
    img_contents = tf.read_file(input_queue[0])
    label_contents = tf.read_file(input_queue[1])

    img = tf.image.decode_jpeg(img_contents, channels=3)
    label = tf.image.decode_png(label_contents, channels=1)
    if input_size is not None:
        h, w = input_size
        if random_scale:
            scale = tf.random_uniform([1], minval=0.75, maxval=1.25, dtype=tf.float32, seed=None)
            h_new = tf.to_int32(tf.mul(tf.to_float(tf.shape(img)[0]), scale))
            w_new = tf.to_int32(tf.mul(tf.to_float(tf.shape(img)[1]), scale))
            new_shape = tf.squeeze(tf.pack([h_new, w_new]), squeeze_dims=[1])
            img = tf.image.resize_images(img, new_shape)
            label = tf.image.resize_nearest_neighbor(tf.expand_dims(label, 0), new_shape)
            label = tf.squeeze(label, squeeze_dims=[0]) # resize_image_with_crop_or_pad accepts 3D-tensor.
        img = tf.image.resize_image_with_crop_or_pad(img, h, w)
        label = tf.image.resize_image_with_crop_or_pad(label, h, w)
    # RGB -> BGR.
    img_r, img_g, img_b = tf.split(split_dim=2, num_split=3, value=img)
    img = tf.cast(tf.concat(2, [img_b, img_g, img_r]), dtype=tf.float32)
    # Extract mean.
    img -= IMG_MEAN
    return img, label

class ImageReader(object):
    '''Generic ImageReader which reads images and corresponding segmentation
       masks from the disk, and enqueues them into a TensorFlow queue.
    '''

    def __init__(self, data_dir, data_list, input_size, random_scale, coord):
        '''Initialise an ImageReader.

        Args:
          data_dir: path to the directory with images and masks.
          data_list: path to the file with lines of the form '/path/to/image /path/to/mask'.
          input_size: a tuple with (height, width) values, to which all the images will be resized.
          random_scale: whether to randomly scale the images prior to random crop.
          coord: TensorFlow queue coordinator.
        '''
        self.data_dir = data_dir
        self.data_list = data_list
        self.input_size = input_size
        self.coord = coord

        self.image_list, self.label_list = read_labeled_image_list(self.data_dir, self.data_list)
        self.images = tf.convert_to_tensor(self.image_list, dtype=tf.string)
        self.labels = tf.convert_to_tensor(self.label_list, dtype=tf.string)
        self.queue = tf.train.slice_input_producer([self.images, self.labels],
                                                   shuffle=input_size is not None) # Not shuffling if it is val.
        self.image, self.label = read_images_from_disk(self.queue, self.input_size, random_scale)

    def dequeue(self, num_elements):
        '''Pack images and labels into a batch.

        Args:
          num_elements: the batch size.

        Returns:
          Two tensors of size (batch_size, h, w, {3,1}) for images and masks.'''
        image_batch, label_batch = tf.train.batch([self.image, self.label],
                                                  num_elements)
        return image_batch, label_batch
<|endoftext|>