<commit_before># -*- coding: utf8 -*-

# Imports. {{{1

import sys

# Try to load the required modules from Python's standard library.
try:
    import os
    import traceback
    import argparse
    import time
    import hashlib
    from dedupsqlfs.lib import constants
    from dedupsqlfs.db import check_engines
    from dedupsqlfs.log import logging
except ImportError as e:
    msg = "Error: Failed to load one of the required Python modules! (%s)\n"
    sys.stderr.write(msg % str(e))
    sys.exit(1)

def fuse_mount(options, compression_methods=None, hash_functions=None):
    from dedupsqlfs.fuse.dedupfs import DedupFS
    from dedupsqlfs.fuse.operations import DedupOperations

    ops = None
    try:
        ops = DedupOperations()

        _fuse = DedupFS(
            ops, options.mountpoint,
            options,
            fsname="dedupsqlfs", allow_root=True)

        _fuse.saveCompressionMethods(compression_methods)

        for modname in compression_methods:
            _fuse.appendCompression(modname)

        ret = _fuse.main()
    except Exception as e:
        import traceback
        print(traceback.format_exc())
        ret = -1
    if ops:
        ops.getManager().close()

    return ret

def main(): # {{{1
    """
    This function enables using dedupsqlfs.py as a shell script that creates FUSE
    mount points. Execute "dedupsqlfs -h" for a list of valid command line options.
    """

    logger = logging.getLogger("mount.dedupsqlfs/main")
    logger.setLevel(logging.INFO)
    logger.addHandler(logging.StreamHandler(sys.stderr))

    parser = argparse.ArgumentParser(conflict_handler="resolve")

    # Register some custom command line options with the option parser.
    option_stored_in_db = " (this option is only useful when creating a new database, because your choice is stored in the database and can't be changed after that)"

    parser.add_argument('-h', '--help', action='help', help="show this help message followed by the command line options defined by the Python FUSE binding and exit")
    parser.add_argument('-v', '--verbose', action='count', dest='verbosity', default=0, help="increase verbosity")
    parser.add_argument('--log-file', dest='log_file', help="specify log file location")
    parser.add_argument('--data', dest='data', metavar='DIRECTORY', default="~/data", help="Specify the base location for the files in which metadata and blocks data is stored. Defaults to ~/data")
    parser.add_argument('--name', dest='name', metavar='DATABASE', default="dedupsqlfs", help="Specify the name for the database directory in which metadata and blocks data is stored. Defaults to dedupsqlfs")
    parser.add_argument('--temp', dest='temp', metavar='DIRECTORY', help="Specify the location for the files in which temporary data is stored. By default honour TMPDIR environment variable value.")
    parser.add_argument('--block-size', dest='block_size', metavar='BYTES', default=1024*128, type=int, help="Specify the maximum block size in bytes" + option_stored_in_db + ". Defaults to 128kB.")
    parser.add_argument('--mount-snapshot', dest='snapshot_mount', metavar='NAME', default=None, help="Use shapshot NAME as root fs.")
    parser.add_argument('--raw-root', dest='disable_subvolumes', action="store_true", help="Disable use of all snapshots and subvolumes. Unhide them into root of FS.")

    parser.add_argument('--memory-limit', dest='memory_limit', action='store_true', help="Use some lower values for less memory consumption.")

    engines, msg = check_engines()
    if not engines:
        logger.error("No storage engines available! Please install sqlite or pymysql python module!")
        return 1

    parser.add_argument('--storage-engine', dest='storage_engine', metavar='ENGINE', choices=engines, default=engines[0],
                        help=msg)

    if "mysql" in engines:
        tengines = ('MyISAM', 'InnoDB', 'Aria')
        msg = "One of MySQL table engines: "+", ".join(tengines)+". Default: MyISAM. Aria engine can be used only with MariaDB server."
        parser.add_argument('--table-engine', dest='table_engine', metavar='ENGINE', choices=tengines, default=tengines[0],
                        help=msg)

    parser.add_argument('--no-cache', dest='use_cache', action='store_false', help="Don't use cache in memory and delayed write to storage files.")
    parser.add_argument('--cache-meta-timeout', dest='cache_meta_timeout', metavar='NUMBER', type=int, default=20, help="Delay flush expired metadata for NUMBER of seconds. Defaults to 20 seconds.")
    parser.add_argument('--cache-block-write-timeout', dest='cache_block_write_timeout', metavar='NUMBER', type=int, default=5, help="Delay flush expired data from memory to storage for NUMBER of seconds. Defaults to 5 seconds.")
    parser.add_argument('--cache-block-write-size', dest='cache_block_write_size', metavar='BYTES', type=int,
                        default=256*1024*1024, help="Blocks write cache potential size in BYTES. Defaults to 256 MB.")
    parser.add_argument('--cache-block-read-timeout', dest='cache_block_read_timeout', metavar='NUMBER', type=int, default=5, help="Delay flush expired data from memory for NUMBER of seconds. Defaults to 5 seconds.")
    parser.add_argument('--cache-block-read-size', dest='cache_block_read_size', metavar='BYTES', type=int,
                        default=256*1024*1024, help="Blocks read cache potential size in BYTES. Defaults to 256 MB.")

    parser.add_argument('--no-transactions', dest='use_transactions', action='store_false', help="Don't use transactions when making multiple related changes, this might make the file system faster or slower (?).")
    parser.add_argument('--nosync', dest='synchronous', action='store_false', help="Disable SQLite's normal synchronous behavior which guarantees that data is written to disk immediately, because it slows down the file system too much (this means you might lose data when the mount point isn't cleanly unmounted).")

    parser.add_argument('--nogc-on-umount', dest='gc_umount_enabled', action='store_false', help="Disable garbage collection on umount operation (only do this when you've got disk space to waste or you know that nothing will be be deleted from the file system, which means little to no garbage will be produced).")
    parser.add_argument('--gc', dest='gc_enabled', action='store_true', help="Enable the periodic garbage collection. It degrades performance. Only do this when you don't have disk space to waste or you know that alot of data will be be deleted from the file system.")
    parser.add_argument('--gc-vacuum', dest='gc_vacuum_enabled', action='store_true', help="Enable data vacuum after the periodic garbage collection.")
    parser.add_argument('--gc-interval', dest='gc_interval', metavar="N", type=int, default=60, help="Call garbage callector after Nth seconds on FUSE operations, if GC enabled. Defaults to 60.")

    # Dynamically check for supported hashing algorithms.
    msg = "Specify the hashing algorithm that will be used to recognize duplicate data blocks: one of %s"
    hash_functions = list({}.fromkeys([h.lower() for h in hashlib.algorithms_available]).keys())
    hash_functions.sort()
    msg %= ', '.join('%r' % fun for fun in hash_functions)
    msg += ". Defaults to 'sha1'."
    parser.add_argument('--hash', dest='hash_function', metavar='FUNCTION', choices=hash_functions, default='sha1', help=msg)

    parser.add_argument('--collision-check', dest='collision_check_enabled', action='store_true', help="Check for hash collision on writed data.")

    # Dynamically check for supported compression methods.
    compression_methods = [constants.COMPRESSION_TYPE_NONE]
    for modname in constants.COMPRESSION_SUPPORTED:
        try:
            module = __import__(modname)
            if hasattr(module, 'compress') and hasattr(module, 'decompress'):
                compression_methods.append(modname)
        except ImportError:
            pass
    if len(compression_methods) > 1:
        compression_methods.append(constants.COMPRESSION_TYPE_BEST)
        compression_methods.append(constants.COMPRESSION_TYPE_CUSTOM)

    msg = "Enable compression of data blocks using one of the supported compression methods: one of %s"
    msg %= ', '.join('%r' % mth for mth in compression_methods)
    msg += ". Defaults to %r." % constants.COMPRESSION_TYPE_NONE
    if len(compression_methods) > 1:
        msg += " %r will try all compression methods and choose one with smaller result data." % constants.COMPRESSION_TYPE_BEST
        msg += " %r will try selected compression methods (--custom-compress) and choose one with smaller result data." % constants.COMPRESSION_TYPE_CUSTOM

    parser.add_argument('--compress', dest='compression_method', metavar='METHOD', choices=compression_methods, default=constants.COMPRESSION_TYPE_NONE, help=msg)

    msg = "Enable compression of data blocks using one or more of the supported compression methods: %s"
    msg %= ', '.join('%r' % mth for mth in compression_methods[:-2])
    msg += ". To use two or more methods select this option in command line for each compression method."

    parser.add_argument('--custom-compress', dest='compression_custom', metavar='METHOD', choices=compression_methods[:-2], action="append", help=msg)
    parser.add_argument('--force-compress', dest='compression_forced', action="store_true", help="Force compression even if resulting data is bigger than original.")
    parser.add_argument('--minimal-compress-size', dest='compression_minimal_size', metavar='BYTES', type=int, default=-1, help="Minimal block data size for compression. Defaults to -1 bytes (auto). Do not do compression if not forced to.")
    parser.add_argument('--compression-level', dest='compression_level', metavar="LEVEL", default=constants.COMPRESSION_LEVEL_DEFAULT,
                        choices=(constants.COMPRESSION_LEVEL_DEFAULT, constants.COMPRESSION_LEVEL_FAST, constants.COMPRESSION_LEVEL_NORM, constants.COMPRESSION_LEVEL_BEST),
                        help="Compression level ratio: one of %s. Defaults to %r. Not all methods support this option." % (
                            ', '.join((constants.COMPRESSION_LEVEL_DEFAULT, constants.COMPRESSION_LEVEL_FAST, constants.COMPRESSION_LEVEL_NORM, constants.COMPRESSION_LEVEL_BEST)), constants.COMPRESSION_LEVEL_DEFAULT
                        ))
    # Do not want 'best' after help setup

    # Dynamically check for profiling support.
    try:
        # Using __import__() here because of pyflakes.
        for p in 'cProfile', 'pstats': __import__(p)
        parser.add_argument('--profile', action='store_true', default=False, help="Use the Python modules cProfile and pstats to create a profile of time spent in various function calls and print out a table of the slowest functions at exit (of course this slows everything down but it can nevertheless give a good indication of the hot spots).")
    except ImportError:
        logger.warning("No profiling support available, --profile option disabled.")
        logger.warning("If you're on Ubuntu try 'sudo apt-get install python-profiler'.")

    parser.add_argument('-o', '--mountoption', help="specify mount option", action="append")

    parser.add_argument('--mountpoint', help="specify mount point")

    args = parser.parse_args()

    # Pop auto_best and custom methods
    compression_methods.pop()
    compression_methods.pop()

    if args.profile:
        sys.stderr.write("Enabling profiling..\n")
        import cProfile, pstats
        profile = '.dedupsqlfs.cprofile-%i' % time.time()
        cProfile.run('fuse_mount(args, compression_methods, hash_functions)', profile)
        sys.stderr.write("\n Profiling statistics:\n\n")
        s = pstats.Stats(profile)
        s.sort_stats('time')
        s.print_stats(0.1)
        os.unlink(profile)
    else:
        return fuse_mount(args, compression_methods, hash_functions)

    return 0

if __name__ == '__main__':
    main()

# vim: ts=2 sw=2 et
<commit_msg>Update help for cmd options<commit_after># -*- coding: utf8 -*-

# Imports. {{{1

import sys

# Try to load the required modules from Python's standard library.
try:
    import os
    import traceback
    import argparse
    import time
    import hashlib
    from dedupsqlfs.lib import constants
    from dedupsqlfs.db import check_engines
    from dedupsqlfs.log import logging
except ImportError as e:
    msg = "Error: Failed to load one of the required Python modules! (%s)\n"
    sys.stderr.write(msg % str(e))
    sys.exit(1)

def fuse_mount(options, compression_methods=None, hash_functions=None):
    from dedupsqlfs.fuse.dedupfs import DedupFS
    from dedupsqlfs.fuse.operations import DedupOperations

    ops = None
    try:
        ops = DedupOperations()

        _fuse = DedupFS(
            ops, options.mountpoint,
            options,
            fsname="dedupsqlfs", allow_root=True)

        _fuse.saveCompressionMethods(compression_methods)

        for modname in compression_methods:
            _fuse.appendCompression(modname)

        ret = _fuse.main()
    except Exception as e:
        import traceback
        print(traceback.format_exc())
        ret = -1
    if ops:
        ops.getManager().close()

    return ret

def main(): # {{{1
    """
    This function enables using dedupsqlfs.py as a shell script that creates FUSE
    mount points. Execute "dedupsqlfs -h" for a list of valid command line options.
    """

    logger = logging.getLogger("mount.dedupsqlfs/main")
    logger.setLevel(logging.INFO)
    logger.addHandler(logging.StreamHandler(sys.stderr))

    parser = argparse.ArgumentParser(conflict_handler="resolve")

    # Register some custom command line options with the option parser.
    option_stored_in_db = " (this option is only useful when creating a new database, because your choice is stored in the database and can't be changed after that)"

    parser.add_argument('-h', '--help', action='help', help="show this help message followed by the command line options defined by the Python FUSE binding and exit")
    parser.add_argument('-v', '--verbose', action='count', dest='verbosity', default=0, help="increase verbosity")
    parser.add_argument('--log-file', dest='log_file', help="specify log file location")
    parser.add_argument('--data', dest='data', metavar='DIRECTORY', default="~/data", help="Specify the base location for the files in which metadata and blocks data is stored. Defaults to ~/data")
    parser.add_argument('--name', dest='name', metavar='DATABASE', default="dedupsqlfs", help="Specify the name for the database directory in which metadata and blocks data is stored. Defaults to dedupsqlfs")
    parser.add_argument('--temp', dest='temp', metavar='DIRECTORY', help="Specify the location for the files in which temporary data is stored. By default honour TMPDIR environment variable value.")
    parser.add_argument('--block-size', dest='block_size', metavar='BYTES', default=1024*128, type=int, help="Specify the maximum block size in bytes" + option_stored_in_db + ". Defaults to 128kB.")
    parser.add_argument('--mount-snapshot', dest='snapshot_mount', metavar='NAME', default=None, help="Use shapshot NAME as root fs.")
    parser.add_argument('--raw-root', dest='disable_subvolumes', action="store_true", help="Disable use of all snapshots and subvolumes. Unhide them into root of FS.")

    parser.add_argument('--memory-limit', dest='memory_limit', action='store_true', help="Use some lower values for less memory consumption.")

    engines, msg = check_engines()
    if not engines:
        logger.error("No storage engines available! Please install sqlite or pymysql python module!")
        return 1

    parser.add_argument('--storage-engine', dest='storage_engine', metavar='ENGINE', choices=engines, default=engines[0],
                        help=msg)

    if "mysql" in engines:
        tengines = ('MyISAM', 'InnoDB', 'Aria')
        msg = "One of MySQL table engines: "+", ".join(tengines)+". Default: MyISAM. Aria engine can be used only with MariaDB server."
        parser.add_argument('--table-engine', dest='table_engine', metavar='ENGINE', choices=tengines, default=tengines[0],
                        help=msg)

    parser.add_argument('--no-cache', dest='use_cache', action='store_false', help="Don't use cache in memory and delayed write to storage files.")
    parser.add_argument('--cache-meta-timeout', dest='cache_meta_timeout', metavar='NUMBER', type=int, default=20, help="Delay flush expired metadata for NUMBER of seconds. Defaults to 20 seconds.")
    parser.add_argument('--cache-block-write-timeout', dest='cache_block_write_timeout', metavar='NUMBER', type=int, default=5, help="Delay flush expired data from memory to storage for NUMBER of seconds. Defaults to 5 seconds.")
    parser.add_argument('--cache-block-write-size', dest='cache_block_write_size', metavar='BYTES', type=int,
                        default=256*1024*1024, help="Blocks write cache potential size in BYTES. Defaults to 256 MB.")
    parser.add_argument('--cache-block-read-timeout', dest='cache_block_read_timeout', metavar='NUMBER', type=int, default=5, help="Delay flush expired data from memory for NUMBER of seconds. Defaults to 5 seconds.")
    parser.add_argument('--cache-block-read-size', dest='cache_block_read_size', metavar='BYTES', type=int,
                        default=256*1024*1024, help="Blocks read cache potential size in BYTES. Defaults to 256 MB.")

    parser.add_argument('--no-transactions', dest='use_transactions', action='store_false', help="Don't use transactions when making multiple related changes, this might make the file system faster or slower (?).")
    parser.add_argument('--nosync', dest='synchronous', action='store_false', help="Disable SQLite's normal synchronous behavior which guarantees that data is written to disk immediately, because it slows down the file system too much (this means you might lose data when the mount point isn't cleanly unmounted).")

    parser.add_argument('--nogc-on-umount', dest='gc_umount_enabled', action='store_false', help="Disable garbage collection on umount operation (only do this when you've got disk space to waste or you know that nothing will be be deleted from the file system, which means little to no garbage will be produced).")
    parser.add_argument('--gc', dest='gc_enabled', action='store_true', help="Enable the periodic garbage collection. It degrades performance. Only do this when you don't have disk space to waste or you know that alot of data will be be deleted from the file system.")
    parser.add_argument('--gc-vacuum', dest='gc_vacuum_enabled', action='store_true', help="Enable data vacuum after the periodic garbage collection.")
    parser.add_argument('--gc-interval', dest='gc_interval', metavar="N", type=int, default=60, help="Call garbage callector after Nth seconds on FUSE operations, if GC enabled. Defaults to 60.")

    # Dynamically check for supported hashing algorithms.
    msg = "Specify the hashing algorithm that will be used to recognize duplicate data blocks: one of %s"
    hash_functions = list({}.fromkeys([h.lower() for h in hashlib.algorithms_available]).keys())
    hash_functions.sort()
    msg %= ', '.join('%r' % fun for fun in hash_functions)
    msg += ". Defaults to 'sha1'."
    parser.add_argument('--hash', dest='hash_function', metavar='FUNCTION', choices=hash_functions, default='sha1', help=msg)

    parser.add_argument('--collision-check', dest='collision_check_enabled', action='store_true', help="Check for hash collision on writed data.")

    # Dynamically check for supported compression methods.
    compression_methods = [constants.COMPRESSION_TYPE_NONE]
    for modname in constants.COMPRESSION_SUPPORTED:
        try:
            module = __import__(modname)
            if hasattr(module, 'compress') and hasattr(module, 'decompress'):
                compression_methods.append(modname)
        except ImportError:
            pass
    if len(compression_methods) > 1:
        compression_methods.append(constants.COMPRESSION_TYPE_BEST)
        compression_methods.append(constants.COMPRESSION_TYPE_CUSTOM)

    msg = "Enable compression of data blocks using one of the supported compression methods: one of %s"
    msg %= ', '.join('%r' % mth for mth in compression_methods)
    msg += ". Defaults to %r." % constants.COMPRESSION_TYPE_NONE
    if len(compression_methods) > 1:
        msg += " %r will try all compression methods and choose one with smaller result data." % constants.COMPRESSION_TYPE_BEST
        msg += " %r will try selected compression methods (--custom-compress) and choose one with smaller result data." % constants.COMPRESSION_TYPE_CUSTOM

    parser.add_argument('--compress', dest='compression_method', metavar='METHOD', choices=compression_methods, default=constants.COMPRESSION_TYPE_NONE, help=msg)

    msg = "Enable compression of data blocks using one or more of the supported compression methods: %s"
    msg %= ', '.join('%r' % mth for mth in compression_methods[:-2])
    msg += ". To use two or more methods select this option in command line for each compression method."

    parser.add_argument('--custom-compress', dest='compression_custom', metavar='METHOD', choices=compression_methods[:-2], action="append", help=msg)
    parser.add_argument('--force-compress', dest='compression_forced', action="store_true", help="Force compression even if resulting data is bigger than original.")
    parser.add_argument('--minimal-compress-size', dest='compression_minimal_size', metavar='BYTES', type=int, default=-1, help="Minimal block data size for compression. Defaults to -1 bytes (auto). Not compress if data size is less then BYTES long. If not forced to.")
    parser.add_argument('--compression-level', dest='compression_level', metavar="LEVEL", default=constants.COMPRESSION_LEVEL_DEFAULT,
                        choices=(constants.COMPRESSION_LEVEL_DEFAULT, constants.COMPRESSION_LEVEL_FAST, constants.COMPRESSION_LEVEL_NORM, constants.COMPRESSION_LEVEL_BEST),
                        help="Compression level ratio: one of %s. Defaults to %r. Not all methods support this option." % (
                            ', '.join((constants.COMPRESSION_LEVEL_DEFAULT, constants.COMPRESSION_LEVEL_FAST, constants.COMPRESSION_LEVEL_NORM, constants.COMPRESSION_LEVEL_BEST)), constants.COMPRESSION_LEVEL_DEFAULT
                        ))
    # Do not want 'best' after help setup

    # Dynamically check for profiling support.
    try:
        # Using __import__() here because of pyflakes.
        for p in 'cProfile', 'pstats': __import__(p)
        parser.add_argument('--profile', action='store_true', default=False, help="Use the Python modules cProfile and pstats to create a profile of time spent in various function calls and print out a table of the slowest functions at exit (of course this slows everything down but it can nevertheless give a good indication of the hot spots).")
    except ImportError:
        logger.warning("No profiling support available, --profile option disabled.")
        logger.warning("If you're on Ubuntu try 'sudo apt-get install python-profiler'.")

    parser.add_argument('-o', '--mountoption', help="specify mount option", action="append")

    parser.add_argument('--mountpoint', help="specify mount point")

    args = parser.parse_args()

    # Pop auto_best and custom methods
    compression_methods.pop()
    compression_methods.pop()

    if args.profile:
        sys.stderr.write("Enabling profiling..\n")
        import cProfile, pstats
        profile = '.dedupsqlfs.cprofile-%i' % time.time()
        cProfile.run('fuse_mount(args, compression_methods, hash_functions)', profile)
        sys.stderr.write("\n Profiling statistics:\n\n")
        s = pstats.Stats(profile)
        s.sort_stats('time')
        s.print_stats(0.1)
        os.unlink(profile)
    else:
        return fuse_mount(args, compression_methods, hash_functions)

    return 0

if __name__ == '__main__':
    main()

# vim: ts=2 sw=2 et
<|endoftext|>


<commit_before>#
# THIS IS AN IMPLEMENTATION OF Q LEARNING ALGORITHM TO PLAY TIC TAC TOE
#
# COPYRIGHT BELONGS TO THE AUTHOR OF THIS CODE
#
# AUTHOR : LAKSHMAN KUMAR
# AFFILIATION : UNIVERSITY OF MARYLAND, MARYLAND ROBOTICS CENTER
# EMAIL : LKUMAR93@UMD.EDU
# LINKEDIN : WWW.LINKEDIN.COM/IN/LAKSHMANKUMAR1993
#
# THE WORK (AS DEFINED BELOW) IS PROVIDED UNDER THE TERMS OF THE MIT LICENSE
# THE WORK IS PROTECTED BY COPYRIGHT AND/OR OTHER APPLICABLE LAW. ANY USE OF
# THE WORK OTHER THAN AS AUTHORIZED UNDER THIS LICENSE OR COPYRIGHT LAW IS PROHIBITED.
#
# BY EXERCISING ANY RIGHTS TO THE WORK PROVIDED HERE, YOU ACCEPT AND AGREE TO
# BE BOUND BY THE TERMS OF THIS LICENSE. THE LICENSOR GRANTS YOU THE RIGHTS
# CONTAINED HERE IN CONSIDERATION OF YOUR ACCEPTANCE OF SUCH TERMS AND
# CONDITIONS.
#

###########################################
##
##	LIBRARIES
##
###########################################

import pygame, sys, getopt
from pygame.locals import QUIT, MOUSEBUTTONUP
import time

from QLearning import QLearner
from TicTacToe import Board

###########################################
##
##	VARIABLES
##
###########################################

wins = 0.0
losses = 0.0
draws = 0.0

func_approx_flag = False
exp_replay_flag = None

###########################################
##
##	MAIN FUNCTION
##
###########################################

if __name__ == '__main__':

	#Initialize pygame gui
	pygame.init()

	#Initialize pygame clock
	clock = pygame.time.Clock()

	#Parse command line arguments to check if the user wants to enable function approximation or experience replay

	argv = sys.argv[1:]

   	try:
      		opts, args = getopt.getopt(argv,"f:e:r:",["func_approx=","exp_replay="])

   	except getopt.GetoptError:
      		print 'Usage: python Play.py -f <bool> -e <bool>'
      		sys.exit(2)

	for opt, arg in opts:

     		if opt in ("-f", "--func_approx"):

			if arg == 'True' :
				func_approx_flag = True

			elif arg == 'False' :
				func_approx_flag = False

			else :
				print 'Usage: python Play.py -f <bool> -e <bool>'

				sys.exit(2)

		elif opt in ("-e", "--exp_replay") :

			if arg == 'True' :
				exp_replay_flag = True

			elif arg == 'False' :
				exp_replay_flag = False

			else :
				print 'Usage: python Play.py -f <bool>  -e <bool>'

				sys.exit(2)

	#Initialize the game
	TicTacToeBoard = Board(grid_size=3, box_size=100, border=50, line_width=10)

	#Initialize Q Learning Player
	qlearner = QLearner(TicTacToeBoard, q_id = 1, function_approximation = func_approx_flag,epsilon = 0.0,replay = exp_replay_flag)

	#Loop Forever
	while True:

		# While the game is not over
		while TicTacToeBoard.check_game_over() is False :

			# Ask the qlearner to play first
			qlearner.play()

			#Initialize a flag for checking whether the user has played or not
			user_played = False

			#Update Display
			pygame.display.update()

			#Wait till the user has played
			while user_played is False and TicTacToeBoard.check_game_over() is False :

				#Check for user actions
				for event in pygame.event.get():
					if event.type == QUIT :
					    qlearner.save_policy()
					    print "Exiting Game"
					    pygame.quit()
					    sys.exit()
					elif event.type == MOUSEBUTTONUP:
					    x, y = event.pos
					    user_played = TicTacToeBoard.process_click(x, y)
					    pygame.event.clear()
	   			    	    break

			#Update qlearner's policy
			qlearner.update_policy()

			#Update Display
			pygame.display.update()

			#Set clock frequency
			clock.tick(100)

		#Keep track of number of total wins, losses and draws

		if TicTacToeBoard.check_for_winner() == 1:
			wins = wins + 1

		elif TicTacToeBoard.check_for_winner() == 2:
			losses = losses + 1

		else :
			draws = draws + 1

		#Calculate total number of games played
		total = wins+losses+draws

		#Print the information
		statement = "Number Of Games : " + str(total) + " Computer Score : " + str(wins) + " Your Score : " + str(losses) + " Draws : " + str(draws)
		print statement

		#Once the game is over sleep for a while
		time.sleep(1)

		#Reset the game
		TicTacToeBoard.reset()

<commit_msg>Remove the wrong file<commit_after><|endoftext|>


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

import re
import logging
import urlparse

from flask import request, url_for

from website import settings

# Keep me: Makes rubeus importable from website.util
from . import rubeus  # noqa

logger = logging.getLogger(__name__)

guid_url_node_pattern = re.compile('^/project/[a-zA-Z0-9]{5,}/node(?=/[a-zA-Z0-9]{5,})')
guid_url_project_pattern = re.compile('^/project(?=/[a-zA-Z0-9]{5,})')
guid_url_profile_pattern = re.compile('^/profile(?=/[a-zA-Z0-9]{5,})')

def _get_guid_url_for(url):
    """URL Post-processor transforms specific `/project/<pid>` or `/project/<pid>/node/<nid>`
    urls into guid urls. Ex: `<nid>/wiki/home`.
    """
    guid_url = guid_url_node_pattern.sub('', url, count=1)
    guid_url = guid_url_project_pattern.sub('', guid_url, count=1)
    guid_url = guid_url_profile_pattern.sub('', guid_url, count=1)
    return guid_url

def api_url_for(view_name, _absolute=False, _offload=False, _xml=False, *args, **kwargs):
    """Reverse URL lookup for API routes (that use the JSONRenderer or XMLRenderer).
    Takes the same arguments as Flask's url_for, with the addition of
    `_absolute`, which will make an absolute URL with the correct HTTP scheme
    based on whether the app is in debug mode. The _xml flag sets the renderer to use.
    """
    renderer = 'XMLRenderer' if _xml else 'JSONRenderer'

    url = url_for('{0}__{1}'.format(renderer, view_name), *args, **kwargs)

    if _absolute:
        # We do NOT use the url_for's _external kwarg because app.config['SERVER_NAME'] alters
        # behavior in an unknown way (currently breaks tests). /sloria /jspies
        domain = settings.OFFLOAD_DOMAIN if _offload else settings.DOMAIN
        return urlparse.urljoin(domain, url)
    return url

def web_url_for(view_name, _absolute=False, _offload=False, _guid=False, *args, **kwargs):
    """Reverse URL lookup for web routes (those that use the OsfWebRenderer).
    Takes the same arguments as Flask's url_for, with the addition of
    `_absolute`, which will make an absolute URL with the correct HTTP scheme
    based on whether the app is in debug mode.
    """
    url = url_for('OsfWebRenderer__{0}'.format(view_name), *args, **kwargs)
    if _guid:
        url = _get_guid_url_for(url)

    if _absolute:
        # We do NOT use the url_for's _external kwarg because app.config['SERVER_NAME'] alters
        # behavior in an unknown way (currently breaks tests). /sloria /jspies
        domain = settings.OFFLOAD_DOMAIN if _offload else settings.DOMAIN
        return urlparse.urljoin(domain, url)
    return url

def is_json_request():
    """Return True if the current request is a JSON/AJAX request."""
    content_type = request.content_type
    return content_type and ('application/json' in content_type)
<commit_msg>Add helper function to convert camelCase to snake_case<commit_after># -*- coding: utf-8 -*-

import re
import logging
import urlparse

from flask import request, url_for

from website import settings

# Keep me: Makes rubeus importable from website.util
from . import rubeus  # noqa

logger = logging.getLogger(__name__)

guid_url_node_pattern = re.compile('^/project/[a-zA-Z0-9]{5,}/node(?=/[a-zA-Z0-9]{5,})')
guid_url_project_pattern = re.compile('^/project(?=/[a-zA-Z0-9]{5,})')
guid_url_profile_pattern = re.compile('^/profile(?=/[a-zA-Z0-9]{5,})')

def _get_guid_url_for(url):
    """URL Post-processor transforms specific `/project/<pid>` or `/project/<pid>/node/<nid>`
    urls into guid urls. Ex: `<nid>/wiki/home`.
    """
    guid_url = guid_url_node_pattern.sub('', url, count=1)
    guid_url = guid_url_project_pattern.sub('', guid_url, count=1)
    guid_url = guid_url_profile_pattern.sub('', guid_url, count=1)
    return guid_url

def api_url_for(view_name, _absolute=False, _offload=False, _xml=False, *args, **kwargs):
    """Reverse URL lookup for API routes (that use the JSONRenderer or XMLRenderer).
    Takes the same arguments as Flask's url_for, with the addition of
    `_absolute`, which will make an absolute URL with the correct HTTP scheme
    based on whether the app is in debug mode. The _xml flag sets the renderer to use.
    """
    renderer = 'XMLRenderer' if _xml else 'JSONRenderer'

    url = url_for('{0}__{1}'.format(renderer, view_name), *args, **kwargs)

    if _absolute:
        # We do NOT use the url_for's _external kwarg because app.config['SERVER_NAME'] alters
        # behavior in an unknown way (currently breaks tests). /sloria /jspies
        domain = settings.OFFLOAD_DOMAIN if _offload else settings.DOMAIN
        return urlparse.urljoin(domain, url)
    return url

def web_url_for(view_name, _absolute=False, _offload=False, _guid=False, *args, **kwargs):
    """Reverse URL lookup for web routes (those that use the OsfWebRenderer).
    Takes the same arguments as Flask's url_for, with the addition of
    `_absolute`, which will make an absolute URL with the correct HTTP scheme
    based on whether the app is in debug mode.
    """
    url = url_for('OsfWebRenderer__{0}'.format(view_name), *args, **kwargs)
    if _guid:
        url = _get_guid_url_for(url)

    if _absolute:
        # We do NOT use the url_for's _external kwarg because app.config['SERVER_NAME'] alters
        # behavior in an unknown way (currently breaks tests). /sloria /jspies
        domain = settings.OFFLOAD_DOMAIN if _offload else settings.DOMAIN
        return urlparse.urljoin(domain, url)
    return url

def is_json_request():
    """Return True if the current request is a JSON/AJAX request."""
    content_type = request.content_type
    return content_type and ('application/json' in content_type)

# from http://stackoverflow.com/questions/1175208/elegant-python-function-to-convert-camelcase-to-camel-case
def _camel_to_snake(key):
    s1 = re.sub('(.)([A-Z][a-z]+)', r'\1_\2', key)
    return re.sub('([a-z0-9])([A-Z])', r'\1_\2', s1).lower()

def js_to_python(obj):
    ret = {}
    for key, value in obj.iteritems():
        ret[_camel_to_snake(key)] = value
    return ret

<|endoftext|>


<commit_before>from django.test import TestCase
from django.core.urlresolvers import reverse

import json

class POIViewTestCase(TestCase):
    fixtures = ['test_fixtures']

    def setUp(self):
        self.expected_poi = """
{
  "error": {
    "status": false,
    "name": null,
    "text": null,
    "debug": null,
    "level": null
  },
  "id": 1,
  "name": "Newport Lighthouse",
  "alt_name": "",
  "description": "A pretty nice lighthouse",
  "history": "It was built at some time in the past",
  "facts": "It's a lighthouse",
  "location": "POINT (-124.1053399999999982 43.9668739999999971)",
  "street": "123 Fake St",
  "city": "Newport",
  "state": "Oregon",
  "location_description": "out on the cape over there",
  "zip": "11234",
  "website": "",
  "email": "",
  "phone": "",
  "hazards": [1],
  "categories": [1],
  "images": [1],
  "videos": [1],
  "created": "2014-08-08 23:27:05.568395+00:00",
  "modified": "2014-08-08 23:27:05.568395+00:00"
}"""

        self.expected_not_found = """
{
  "error": {
    "status": true,
    "text": "POI id 999 was not found.",
    "name": "POI Not Found",
    "debug": "DoesNotExist: POI matching query does not exist.",
    "level": "Error"
  }
}"""

    def test_url_endpoint(self):
        url = reverse('poi-details', kwargs={'id': '1'})
        self.assertEqual(url, '/1/poi/1')

    def test_known_poi(self):
        response = self.client.get(
            reverse('poi-details', kwargs={'id': '1'}))

        parsed_answer = json.loads(response.content)
        self.assertEqual(response.status_code, 200)

        expected_answer = json.loads(self.expected_poi)
        self.maxDiff = None

        self.assertEqual(parsed_answer, expected_answer)

    def test_poi_not_found(self):
        response = self.client.get(
            reverse('poi-details', kwargs={'id': '999'}))
        parsed_answer = json.loads(response.content)
        self.assertEqual(response.status_code, 404)

        expected_answer = json.loads(self.expected_not_found)
        self.maxDiff = None

        self.assertEqual(parsed_answer, expected_answer)
<commit_msg>Fixed JSON<commit_after>from django.test import TestCase
from django.core.urlresolvers import reverse

import json

class POIViewTestCase(TestCase):
    fixtures = ['test_fixtures']

    def setUp(self):
        self.expected_poi = """
{
  "error": {
    "status": false,
    "name": null,
    "text": null,
    "debug": null,
    "level": null
  },
   "pointsofinterest": [
        {
            "street": "123 Fake St",
            "alt_name": "",
            "contact_name": "",
            "facts": "It's a lighthouse",
            "lng": -124.10534,
            "id": 1,
            "city": "Newport",
            "zip": "11234",
            "hazards": [
                {
                    "name": "Falling Rocks",
                    "description": "If these fall on you, you're dead.",
                    "id": 1
                }
            ],
            "ext": {
            },
            "state": "Oregon",
            "email": "",
            "website": "",
            "description": "A pretty nice lighthouse",
            "phone": null,
            "lat": 43.966874,
            "categories": [
                {
                    "category": "Cool Stuff",
                    "id": 1
                }
            ],
            "videos": [
                {
                    "caption": "Traveling at the speed of light!",
                    "link": "http://www.youtube.com/watch?v=efgDdSWDg0g",
                    "name": "A Starship"
                }
            ],
            "images": [
                {
                    "caption": "Woof!",
                    "link": "/media/dog.jpg",
                    "name": "A dog"
                }
            ],
            "name": "Newport Lighthouse",
            "created": "2014-08-08T23:27:05.568Z",
            "modified": "2014-08-08T23:27:05.568Z",
            "location_description": "out on the cape over there",
            "history": "It was built at some time in the past"
        },
        {
            "name": "Haystack Rock",
            "alt_name": "",
            "contact_name": "",
            "description": "A pretty nice rock",
            "history": "Nature made it",
            "facts": "It's a nature habitat place, no climbing!",
            "street": "123 Fake St",
            "city": "Canon Beach",
            "state": "Oregon",
            "location_description": "",
            "zip": "11234",
            "website": "http://hatstackrock.com",
            "email": "rock@haystackrock.com",
            "phone": "+1 555 123 4567",
            "created": "2014-08-08T23:27:05.568Z",
            "modified": "2014-08-08T23:27:05.568Z",
            "lng": -124.10534,
            "id": 2,
            "ext": {
            },
            "lat": 43.966874,
            "hazards": [
                {
                    "name": "Alien Abduction",
                    "description": "This site is liable to result \
                    in you being abducted by aliens.",
                    "id": 2
                }
            ],
            "categories": [
                {
                    "category": "Uncool Stuff",
                    "id": 2
                }
            ],
            "videos": [
                {
                    "link": "http://www.youtube.com/watch?v=M-nlAuCW7WY",
                    "caption": "Princely",
                    "name": "Princely"
                }
            ],
            "images": [
                {
                    "link": "/media/cat.jpg",
                    "caption": "Meow!",
                    "name": "A cat"
                }
            ]
        }
    ]
}"""

        self.expected_not_found = """
{
  "error": {
    "status": true,
    "text": "POI id 999 was not found.",
    "name": "POI Not Found",
    "debug": "DoesNotExist: POI matching query does not exist.",
    "level": "Error"
  }
}"""

    def test_url_endpoint(self):
        url = reverse('poi-details', kwargs={'id': '1'})
        self.assertEqual(url, '/1/pois/1')

    def test_known_poi(self):
        response = self.client.get(
            reverse('poi-details', kwargs={'id': '1'}))

        parsed_answer = json.loads(response.content)
        self.assertEqual(response.status_code, 200)

        expected_answer = json.loads(self.expected_poi)
        self.maxDiff = None

        self.assertEqual(parsed_answer, expected_answer)

    def test_poi_not_found(self):
        response = self.client.get(
            reverse('poi-details', kwargs={'id': '999'}))
        parsed_answer = json.loads(response.content)
        self.assertEqual(response.status_code, 404)

        expected_answer = json.loads(self.expected_not_found)
        self.maxDiff = None

        self.assertEqual(parsed_answer, expected_answer)
<|endoftext|>


<commit_before>
__author__ = 'Lothilius'

import csv
import os
import numpy as np
import matplotlib.pyplot as plt
import skimage as ski
import pylab as P

from skimage import data, filter, exposure
from skimage.color import rgb2gray
from skimage.transform import hough_circle
from skimage.feature import peak_local_max
from skimage.draw import circle_perimeter

#Pull data from CSV file
def arrayFromFile(filename):
    """Given an external file containing numbers,
            create an array from those numbers."""
    dataArray = []
    with open(filename, 'r+') as csvfile:
        spamreader = csv.reader(csvfile, delimiter=',', quotechar='"')
        for row in spamreader:
            dataArray.append(row)
    return dataArray[1565]

#Reduce data to a few peak points
def peaks(data):
    opStack = MyQue.Stack()
    indx = MyQue.Stack()
    i = 0
    last = 0
    lasti = 0
    vlast = 0

    for pixel in data[1565]:
        if len(pixel) == 2:
            pixel = pixel.replace("0.","0")
        if opStack.isEmpty():
            opStack.push(pixel)
            indx.push(i)
            i = i + 1
        elif last > pixel:
            if vlast <= last and last != opStack.peek() and i - indx.peek() <= 3:
                indx.push(lasti)
                opStack.push(last)
                i = i + 1
            else:
                i = i + 1
        elif last < pixel: #and i - indx.peek() >= 10:
            if i - indx.peek() <= 10:
                indx.pop()
                opStack.pop()
                indx.push(i)
                opStack.push(pixel)
                i = i + 1
            else:
                indx.push(i)
                opStack.push(pixel)
                i = i + 1
        else:
            i = i + 1
        last = pixel
        lasti = i
        vlast = last

    thePeaks = [0]*4752
    for i, d in zip(indx, opStack):
        thePeaks[i] = d

    return thePeaks

# Return Radius given two peaks on the same ring
def radius(peakL, peakR):
   rad = (peakR - peakL)
   return rad

#write an array to a file.
def arrayTofile(dataArray, fileNum):
    fileName = "/Users/admin/Dropbox/School/Spring-2014/PHY-474/Labs/Zeeman/play_data/dataManipulation/ImageData"+fileNum+"_a.csv"
    print(fileName)
    with open(fileName, 'w+', newline='') as csvfile:
        linewriter = csv.writer(csvfile, delimiter= ",")
        for each in dataArray:
            linewriter.writerow([each])
    print("done")

#Create file from array with finaldata.csv as the name and append.
def dataTofile(dataArray):
    fileName = "Finaldata2.csv"
    with open(fileName, 'a', newline='') as csvfile:
        linewriter = csv.writer(csvfile, delimiter= ",")
        linewriter.writerow(dataArray)

#Create array of data for creating the Delta enregy function.
def dataArray(peakL1, peakR1, peakL2, peakI, peakO):
    rad1 = radius(peakL1,peakR1) / 2
    halfPoint = peakL1 + rad1
    rad2 = radius(peakL2, halfPoint)
    radI = radius(peakI, halfPoint)
    radO = radius(peakO, halfPoint)
    radArray = [rad1, rad2, radI, radO]
    return radArray

#Ternary Search
def ternarySearch(f, left, right, absolutePrecision):
    #left and right are the current bounds; the maximum is between them
    if (right - left) < absolutePrecision:
        return (left + right)/2

    leftThird = (2*left + right)/3
    rightThird = (left + 2*right)/3

    if f[leftThird] < f[rightThird]:
        return ternarySearch(f, leftThird, right, absolutePrecision)
    else:
        return ternarySearch(f, left, rightThird, absolutePrecision)

#Processes jpg file
def jpg_to_array(file_path, file_name):
    full_path = file_path + file_name
    image = data.load(full_path)
    #plt.imshow(image)
    #plt.show()
    return image

#Remove all but one color
def strip_color(image_rgb1, color_of_interest):
    image_rgb = ski.img_as_ubyte(image_rgb1)

    if color_of_interest == 0:
        colorow = np.array([[1.0, 0, 0]] * len(image_rgb[0]))
    elif color_of_interest == 1:
        colorow = np.array([[0, 1.0, 0]] * len(image_rgb[0]))
    elif color_of_interest == 2:
        colorow = np.array([[0, 0, 1.0]] * len(image_rgb[0]))

    image_stripped = np.array([colorow] * len(image_rgb))

    image_stripped = image_stripped * image_rgb

    image_stripped = rgb2gray(image_stripped)

    image_proc = ski.filter.canny(image_stripped, sigma=7, low_threshold=0, high_threshold=4)

    img, radii = get_center(image_proc, image_rgb)

    print('The red and blue have been stripped from image.')

    return image_stripped, image_proc, radii

#Detect radius
def get_center(edges, image_rgb1):
    #image = ski.img_as_ubyte(edges[977:2277, 1650:3150])
    image = edges

    hough_radii = np.arange(400, 565, 5)
    hough_res = hough_circle(edges, hough_radii)

    centers = []
    accums = []
    radii = []

    for radius, h in zip(hough_radii, hough_res):
        # For each radius, extract two circles
        peaks = peak_local_max(h, num_peaks=2)
        centers.extend(peaks)
        accums.extend(h[peaks[:, 0], peaks[:, 1]])
        radii.extend([radius, radius])

    # Draw the most prominent 15 circles
    center_array = []
    image = ski.color.gray2rgb(image)
    for idx in np.argsort(accums)[::-1][:15]:
        center_x, center_y = centers[idx]
        radius = radii[idx]
        center_array.append([center_x, center_y])
        cx, cy = circle_perimeter(center_y, center_x, radius)
        image[cy, cx] = (220, 0, 0)

    return image, center_array

#plot data
def plotevents(datalist):
    x = range(0, len(datalist))
    x = np.array(x)
    y = datalist

    plt.plot(y)
    #fig, ax = plt.subplots()
    #plt.scatter(x, y, s=20, marker='.', c='blue')

    #plt.show()
    return "done"

#Create histogram plot for center of circles.
def histo_plot(image, center):
    h = np.array([0] * len(image[0]))
    hist = np.array([h] * len(image))

    for each in center:
        hist[each[0]][each[1]] += 1
    H, xedges, yedges = np.histogram2d(center[:,0], center[:,1], bins=25)
    plt.pcolor(xedges, yedges, H)
    plt.show()

    return hist

#Get edges from processed image
def getedge(center, slice):
    r1 =[]
    for i, item in enumerate(slice[center:]):
        if item != False:
            r1.append(i)
    return r1 + center

def main():
    #dtype={'names': ['amps', 'rowT', 'colR', 'rowB', 'colL'], 'formats': ['f2', 'i1', 'i1', 'i1', 'i1']})
    for file in os.listdir("/Users/"):
        if file == 'martin':
            print("Welcome Martin")
            inputFileDer = "/Volumes/Ket/Dropbox/School/Summer-2014/SSC-479R/comp-cert/as_images/"
            break
        elif file == 'admin':
            print("Welcome Admin")
            inputFileDer = "/Users/admin/Dropbox/School/Summer-2014/SSC-479R/comp-cert/as_images/"
            break

    file_name = raw_input("Type in file name: ")
    #while file_name != "done":

    #data = arrayFromFile(inputFileDer+fileNum)
    org_image = jpg_to_array(inputFileDer, file_name)

    color_of_interest = raw_input("What color is of interest? ")

    if ('Red' in color_of_interest) or ('red' in color_of_interest):
        color_of_interest = 0
    elif ('Green' in color_of_interest) or ('green' in color_of_interest):
        color_of_interest = 1
    elif ('Blue' in color_of_interest) or ('blue' in color_of_interest):
        color_of_interest = 2

    image_stripped, image_proc, center = strip_color(org_image, color_of_interest)
    print("Showing image")

    #Show detected rings using canny algo
    plt.imshow(image_proc, origin='lower')
    plt.gray()

    #Show the stripped image
    plt.imshow(image_stripped, origin='lower', alpha=.5)
    plt.gray()

    #Print events in the average of the radius in the y access.
    avrg_y = np.mean(center, axis=0)[0]
    print('Average y value for center: ' + str(avrg_y))
    uncertanty_y = np.std(center, axis=0)[0]
    print('Uncertainty in y: ' + str(uncertanty_y))

    avrg_x = np.mean(center, axis=0)[1]
    print('Average x value for center: ' + str(avrg_x))
    uncertanty_x = np.std(center, axis=0)[1]
    print('Uncertainty in x: ' + str(uncertanty_x))

    #y value will give horizontal slice.
    base_line = avrg_y - org_image[avrg_y][avrg_x][1]
    plotevents(org_image[avrg_y][:, 1] + base_line)
    l = plt.axhline(y=avrg_y, color='r')
    plotevents(image_stripped[avrg_y] + base_line)
    l = plt.axvline(x=avrg_x, color='r')
    plt.margins(0)
    os.system("afplay woohoo.wav")
    plt.show()

 #Get first right peak
    edges_array = getedge(avrg_x, image_proc[avrg_y])
    peakPrec = uncertanty_x
    for item in edges_array:
        peakL1 = ternarySearch(image_stripped[avrg_y], item-15, item+15, peakPrec)
        l = plt.axvline(x=peakL1, color='r')

    field_image = np.array([image_stripped[avrg_y]] * 300)
    field_image_proc = np.array([image_proc[avrg_y]] * 300)
    plt.imshow(field_image_proc, origin='lower')
    plt.imshow(field_image, origin='lower', alpha=.5)
    plotevents(org_image[avrg_y][:, 1])
    plotevents(image_stripped[avrg_y])
    l = plt.axvline(x=avrg_x, color='r')
    plt.margins(0)
    os.system("afplay woohoo.wav")
    plt.show()

    center = np.array(center)

    H = histo_plot(image_proc, center)
    N , Bins, Patches = plt.hist(center[:, 0], 15)

    n , bins, patches = plt.hist(center[:, 1], 15)

    plt.show()

    peakL1 = int(raw_input("Type in potential n peak pixel bin: "))
    peakPrec = int(raw_input("Type in precision: "))
    peakL1 = ternarySearch(image[1622], peakL1-15, peakL1+15, peakPrec)
    print(peakL1)

main()<commit_msg>remove and limit the printing after the processing<commit_after>
__author__ = 'Lothilius'

import csv
import os
import numpy as np
import matplotlib.pyplot as plt
import skimage as ski
import pylab as P

from skimage import data, filter, exposure
from skimage.color import rgb2gray
from skimage.transform import hough_circle
from skimage.feature import peak_local_max
from skimage.draw import circle_perimeter

#Pull data from CSV file
def arrayFromFile(filename):
    """Given an external file containing numbers,
            create an array from those numbers."""
    dataArray = []
    with open(filename, 'r+') as csvfile:
        spamreader = csv.reader(csvfile, delimiter=',', quotechar='"')
        for row in spamreader:
            dataArray.append(row)
    return dataArray[1565]

#Reduce data to a few peak points
def peaks(data):
    opStack = MyQue.Stack()
    indx = MyQue.Stack()
    i = 0
    last = 0
    lasti = 0
    vlast = 0

    for pixel in data[1565]:
        if len(pixel) == 2:
            pixel = pixel.replace("0.","0")
        if opStack.isEmpty():
            opStack.push(pixel)
            indx.push(i)
            i = i + 1
        elif last > pixel:
            if vlast <= last and last != opStack.peek() and i - indx.peek() <= 3:
                indx.push(lasti)
                opStack.push(last)
                i = i + 1
            else:
                i = i + 1
        elif last < pixel: #and i - indx.peek() >= 10:
            if i - indx.peek() <= 10:
                indx.pop()
                opStack.pop()
                indx.push(i)
                opStack.push(pixel)
                i = i + 1
            else:
                indx.push(i)
                opStack.push(pixel)
                i = i + 1
        else:
            i = i + 1
        last = pixel
        lasti = i
        vlast = last

    thePeaks = [0]*4752
    for i, d in zip(indx, opStack):
        thePeaks[i] = d

    return thePeaks

# Return Radius given two peaks on the same ring
def radius(peakL, peakR):
   rad = (peakR - peakL)
   return rad

#write an array to a file.
def arrayTofile(dataArray, fileNum):
    fileName = "/Users/admin/Dropbox/School/Spring-2014/PHY-474/Labs/Zeeman/play_data/dataManipulation/ImageData"+fileNum+"_a.csv"
    print(fileName)
    with open(fileName, 'w+', newline='') as csvfile:
        linewriter = csv.writer(csvfile, delimiter= ",")
        for each in dataArray:
            linewriter.writerow([each])
    print("done")

#Create file from array with finaldata.csv as the name and append.
def dataTofile(dataArray):
    fileName = "Finaldata2.csv"
    with open(fileName, 'a', newline='') as csvfile:
        linewriter = csv.writer(csvfile, delimiter= ",")
        linewriter.writerow(dataArray)

#Create array of data for creating the Delta enregy function.
def dataArray(peakL1, peakR1, peakL2, peakI, peakO):
    rad1 = radius(peakL1,peakR1) / 2
    halfPoint = peakL1 + rad1
    rad2 = radius(peakL2, halfPoint)
    radI = radius(peakI, halfPoint)
    radO = radius(peakO, halfPoint)
    radArray = [rad1, rad2, radI, radO]
    return radArray

#Ternary Search
def ternarySearch(f, left, right, absolutePrecision):
    #left and right are the current bounds; the maximum is between them
    if (right - left) < absolutePrecision:
        return (left + right)/2

    leftThird = (2*left + right)/3
    rightThird = (left + 2*right)/3

    if f[leftThird] < f[rightThird]:
        return ternarySearch(f, leftThird, right, absolutePrecision)
    else:
        return ternarySearch(f, left, rightThird, absolutePrecision)

#Processes jpg file
def jpg_to_array(file_path, file_name):
    full_path = file_path + file_name
    image = data.load(full_path)
    #plt.imshow(image)
    #plt.show()
    return image

#Remove all but one color
def strip_color(image_rgb1, color_of_interest):
    image_rgb = ski.img_as_ubyte(image_rgb1)

    if color_of_interest == 0:
        colorow = np.array([[1.0, 0, 0]] * len(image_rgb[0]))
    elif color_of_interest == 1:
        colorow = np.array([[0, 1.0, 0]] * len(image_rgb[0]))
    elif color_of_interest == 2:
        colorow = np.array([[0, 0, 1.0]] * len(image_rgb[0]))

    image_stripped = np.array([colorow] * len(image_rgb))

    image_stripped = image_stripped * image_rgb

    image_stripped = rgb2gray(image_stripped)

    image_proc = ski.filter.canny(image_stripped, sigma=7, low_threshold=0, high_threshold=4)

    img, radii = get_center(image_proc, image_rgb)

    print('The red and blue have been stripped from image.')

    return image_stripped, image_proc, radii

#Detect radius
def get_center(edges, image_rgb1):
    #image = ski.img_as_ubyte(edges[977:2277, 1650:3150])
    image = edges

    hough_radii = np.arange(400, 565, 5)
    hough_res = hough_circle(edges, hough_radii)

    centers = []
    accums = []
    radii = []

    for radius, h in zip(hough_radii, hough_res):
        # For each radius, extract two circles
        peaks = peak_local_max(h, num_peaks=2)
        centers.extend(peaks)
        accums.extend(h[peaks[:, 0], peaks[:, 1]])
        radii.extend([radius, radius])

    # Draw the most prominent 15 circles
    center_array = []
    image = ski.color.gray2rgb(image)
    for idx in np.argsort(accums)[::-1][:15]:
        center_x, center_y = centers[idx]
        radius = radii[idx]
        center_array.append([center_x, center_y])
        cx, cy = circle_perimeter(center_y, center_x, radius)
        image[cy, cx] = (220, 0, 0)

    return image, center_array

#plot data
def plotevents(datalist):
    x = range(0, len(datalist))
    x = np.array(x)
    y = datalist

    plt.plot(y)
    #fig, ax = plt.subplots()
    #plt.scatter(x, y, s=20, marker='.', c='blue')

    #plt.show()
    return "done"

#Create histogram plot for center of circles.
def histo_plot(image, center):
    h = np.array([0] * len(image[0]))
    hist = np.array([h] * len(image))

    for each in center:
        hist[each[0]][each[1]] += 1
    H, xedges, yedges = np.histogram2d(center[:,0], center[:,1], bins=25)
    plt.pcolor(xedges, yedges, H)
    plt.show()

    return hist

#Get edges from processed image
def getedge(center, slice):
    r1 =[]
    for i, item in enumerate(slice[center:]):
        if item != False:
            r1.append(i)
    return r1 + center

def main():
    #dtype={'names': ['amps', 'rowT', 'colR', 'rowB', 'colL'], 'formats': ['f2', 'i1', 'i1', 'i1', 'i1']})
    for file in os.listdir("/Users/"):
        if file == 'martin':
            print("Welcome Martin")
            inputFileDer = "/Volumes/Ket/Dropbox/School/Summer-2014/SSC-479R/comp-cert/as_images/"
            break
        elif file == 'admin':
            print("Welcome Admin")
            inputFileDer = "/Users/admin/Dropbox/School/Summer-2014/SSC-479R/comp-cert/as_images/"
            break

    file_name = raw_input("Type in file name: ")
    #while file_name != "done":

    #data = arrayFromFile(inputFileDer+fileNum)
    org_image = jpg_to_array(inputFileDer, file_name)

    color_of_interest = raw_input("What color is of interest? ")

    if ('Red' in color_of_interest) or ('red' in color_of_interest):
        color_of_interest = 0
    elif ('Green' in color_of_interest) or ('green' in color_of_interest):
        color_of_interest = 1
    elif ('Blue' in color_of_interest) or ('blue' in color_of_interest):
        color_of_interest = 2

    image_stripped, image_proc, center = strip_color(org_image, color_of_interest)
    print("Showing image")

    #Show detected rings using canny algo
    plt.imshow(image_proc, origin='lower')
    plt.gray()

    #Show the stripped image
    plt.imshow(image_stripped, origin='lower', alpha=.5)
    plt.gray()

    #Print events in the average of the radius in the y access.
    avrg_y = np.mean(center, axis=0)[0]
    print('Average y value for center: ' + str(avrg_y))
    uncertanty_y = np.std(center, axis=0)[0]
    print('Uncertainty in y: ' + str(uncertanty_y))

    avrg_x = np.mean(center, axis=0)[1]
    print('Average x value for center: ' + str(avrg_x))
    uncertanty_x = np.std(center, axis=0)[1]
    print('Uncertainty in x: ' + str(uncertanty_x))

    #y value will give horizontal slice.
    base_line = avrg_y - org_image[avrg_y][avrg_x][1]
    plotevents(org_image[avrg_y][:, 1] + base_line)
    l = plt.axhline(y=avrg_y, color='r')
    plotevents(image_stripped[avrg_y] + base_line)
    l = plt.axvline(x=avrg_x, color='r')
    plt.margins(0)
    os.system("afplay woohoo.wav")
    plt.show()

 #Get first right peak
    edges_array = getedge(avrg_x, image_proc[avrg_y])
    peakPrec = uncertanty_x
    for item in edges_array:
        peakL1 = ternarySearch(image_stripped[avrg_y], item-15, item+15, peakPrec)
        l = plt.axvline(x=peakL1, color='r')

    field_image = np.array([image_stripped[avrg_y]] * 300)
    field_image_proc = np.array([image_proc[avrg_y]] * 300)
    plt.imshow(field_image_proc, origin='lower')
    plt.imshow(field_image, origin='lower', alpha=.5)
    plotevents(org_image[avrg_y][:, 1])
    plotevents(image_stripped[avrg_y])
    l = plt.axvline(x=avrg_x, color='r')
    plt.margins(0)
    os.system("afplay woohoo.wav")
    plt.show()

    center = np.array(center)

    #H = histo_plot(image_proc, center)

    #N , Bins, Patches = plt.hist(center[:, 0], 15)
    #n , bins, patches = plt.hist(center[:, 1], 15)

    #plt.show()

main()<|endoftext|>


<commit_before>import re
import sys

errors_encounter = 0
pattern = re.compile(r"_\(([\"']{,3})(?P<message>((?!\1).)*)\1(\s*,\s*context\s*=\s*([\"'])(?P<py_context>((?!\5).)*)\5)*(\s*,\s*(.)*?\s*(,\s*([\"'])(?P<js_context>((?!\11).)*)\11)*)*\)")
start_pattern = re.compile(r"_{1,2}\([\"'`]{1,3}.*[a-zA-Z]")

# skip first argument
files = sys.argv[1:]
files_to_scan = [_file for _file in files if _file.endswith(('.py', '.js'))]

for _file in files_to_scan:
	with open(_file, 'r') as f:
		print(f'Checking: {_file}')
		file_lines = f.readlines()
		for line_number, line in enumerate(file_lines, 1):
			start_matches = start_pattern.search(line)
			if start_matches:
				match = pattern.search(line)
				if not match and line.endswith(',\n'):
					# concat remaining text to validate multiline pattern
					line = "".join(file_lines[line_number - 1:])
					line = line[start_matches.start() + 1:]
					match = pattern.match(line)

				if not match:
					errors_encounter += 1
					print(f'\nTranslation syntax error at line number: {line_number + 1}\n{line.strip()[:100]}')

if errors_encounter > 0:
	print('\nYou can visit "https://frappeframework.com/docs/user/en/translations" to resolve this error.')
	sys.exit(1)
else:
	print('\nGood To Go!')
<commit_msg>fix: add test strings<commit_after>import re
import sys

errors_encounter = 0
pattern = re.compile(r"_\(([\"']{,3})(?P<message>((?!\1).)*)\1(\s*,\s*context\s*=\s*([\"'])(?P<py_context>((?!\5).)*)\5)*(\s*,\s*(.)*?\s*(,\s*([\"'])(?P<js_context>((?!\11).)*)\11)*)*\)")
start_pattern = re.compile(r"_{1,2}\([\"'`]{1,3}.*[a-zA-Z]")

# _('this is valid')
# _('{0} {1}')
# _("{0} {1}")
# _("{0} valid")
# _("valid {0}")

# skip first argument
files = sys.argv[1:]
files_to_scan = [_file for _file in files if _file.endswith(('.py', '.js'))]

for _file in files_to_scan:
	with open(_file, 'r') as f:
		print(f'Checking: {_file}')
		file_lines = f.readlines()
		for line_number, line in enumerate(file_lines, 1):
			start_matches = start_pattern.search(line)
			if start_matches:
				match = pattern.search(line)
				if not match and line.endswith(',\n'):
					# concat remaining text to validate multiline pattern
					line = "".join(file_lines[line_number - 1:])
					line = line[start_matches.start() + 1:]
					match = pattern.match(line)

				if not match:
					errors_encounter += 1
					print(f'\nTranslation syntax error at line number: {line_number + 1}\n{line.strip()[:100]}')

if errors_encounter > 0:
	print('\nYou can visit "https://frappeframework.com/docs/user/en/translations" to resolve this error.')
	sys.exit(1)
else:
	print('\nGood To Go!')
<|endoftext|>


<commit_before>from dolo.symbolic.symbolic import *
from dolo.symbolic.model import *

import sympy
import re
import inspect

line_regex = re.compile(
    "(\s*)$|"
    +"\s*//#(.*)$|"
    +"\s*@#(.*)$|"
    +"\s*//[^#](.*)$|"
    +"\s*\[(.*)\]\s*$|"
    +"(\s*[^\s].*)$"
)
tag_regex = re.compile("\s*(\w+)\s*=\s*'(.*)'")

def parse_dynare_text(txt,add_model=True,full_output=False,names_dict = {}):
    '''
    Imports the content of a modfile into the current interpreter scope
    '''
    # here we call "instruction group", a string finishing by a semicolon
    # an "instruction group" can have several lines
    # a line can be
    # - a comment //...
    # - an old-style tag //$...
    # - a new-style tag [key1='value1',..]
    # - macro-instruction @#...
    # A Modfile contains several blocks (in this order) :
    # - an initblock defining variables, exovariables, parameters, initialization
    #   inside the initblock the order of declaration doesn't matter
    # - a model block with two special lines (model; end;)
    # - optional blocks (like endval, shocks)
    #    seperated by free matlab instructions in any order;
    # - all other instructions are ignored

    otxt = txt
    otxt = otxt.replace("\r\n","\n")
    otxt = otxt.replace("^","**")

    # first, we remove end-of-line comments : they are definitely lost
    regex = re.compile("(.+)//[^#](.*)")
    def remove_end_comment(line):
        res = regex.search(line)
        if res:
            l = res.groups(1)[0]
            return(l)
        else:
            return line
    txt = str.join("\n",map(remove_end_comment,otxt.split("\n")))

    name_regex = re.compile("//\s*fname\s*=\s*'(.*)'")
    m = name_regex.search(txt)
    if m:
        fname = m.group(1)
    else:
        fname = None

    instruction_groups = [Instruction_group(s) for s in txt.split(";")]

    instructions = [ig.instruction for ig in instruction_groups]

    try:
        imodel = [re.compile('model(\(.*\)|)').match(e) is not None for e in instructions]
        imodel = imodel.index(True)
        #imodel = instructions.index("model") #this doesn't work for "MODEL"
        iend = instructions.index("end")
        model_block = instruction_groups[imodel:(iend+1)]
        init_block = instruction_groups[0:imodel]
    except:
        raise Exception('Model block could not be found.')

    next_instructions = instructions[(iend+1):]
    next_instruction_groups = instruction_groups[(iend+1):]

    if 'initval' in next_instructions:
        iinitval = next_instructions.index('initval')
        iend = next_instructions.index('end',iinitval)
        matlab_block_1 = next_instruction_groups[0:iinitval]
        initval_block = next_instruction_groups[iinitval:(iend+1)]
        next_instruction_groups = next_instruction_groups[(iend+1):]
        next_instructions = next_instructions[(iend+1):]
    else:
        initval_block = None
        matlab_block_1 = None

    if 'endval' in next_instructions:
        iendval = next_instructions.index('endval')
        iend = next_instructions.index('end',iendval)
        matlab_block_2 = next_instruction_groups[0:iendval]
        endval_block = next_instruction_groups[iendval:(iend+1)]
        next_instruction_groups = next_instruction_groups[(iend+1):]
        next_instructions = next_instructions[(iend+1):]
    else:
        endval_block = None
        matlab_block_2 = None

    # TODO : currently shocks block needs to follow initval, this restriction should be removed
    if 'shocks' in next_instructions:
        ishocks = next_instructions.index('shocks')
        iend = next_instructions.index('end',ishocks)
        matlab_block_3 = next_instruction_groups[0:ishocks]
        shocks_block = next_instruction_groups[ishocks:(iend+1)]
        next_instruction_groups = next_instruction_groups[(iend+1):]
        next_instructions = next_instructions[(iend+1):]
    else:
        shocks_block = None
        matlab_block_3 = None

    try:
        init_regex = re.compile("(parameters |var |varexo |)(.*)")
        var_names = []
        varexo_names = []
        parameters_names = []
        declarations = {}
        for ig in init_block:
            if ig.instruction != '':
                m = init_regex.match(ig.instruction)
                if not m:
                    raise Exception("Unexpected instruction in init block : " + str(ig.instruction))
                if m.group(1) == '':
                    [lhs,rhs] = m.group(2).split("=")
                    lhs = lhs.strip()
                    rhs = rhs.strip()
                    declarations[lhs] = rhs
                else:
                    arg = m.group(2).replace(","," ")
                    names = [vn.strip() for vn in arg.split()]
                    if m.group(1).strip() == 'var':
                        dest = var_names
                    elif m.group(1).strip() == 'varexo':
                        dest = varexo_names
                    elif m.group(1).strip() == 'parameters':
                        dest = parameters_names
                    for n in names:
                        if not n in dest:
                            dest.append(n)
                        else:
                            raise Exception("symbol %s has already been defined".format(n))
    except Exception as e:
        raise Exception('Initval could not be read : ' + str(e) )
    # the following instruction set the variables "variables","shocks","parameters"

    variables = []
    for vn in var_names:
        if vn in names_dict:
            latex_name = names_dict[vn]
        else:
            latex_name = None
        v = Variable(vn,0)
        variables.append(v)

    shocks = []
    for vn in varexo_names:
        if vn in names_dict:
            latex_name = names_dict[vn]
        else:
            latex_name = None
        s = Shock(vn,0)
        shocks.append(s)

    parameters = []
    for vn in parameters_names:
        if vn in names_dict:
            latex_name = names_dict[vn]
        else:
            latex_name = None
        p = Parameter(vn)
        parameters.append(p)

    parse_dict = dict()
    for v in variables + shocks + parameters:
        parse_dict[v.name] = v

    special_symbols = [sympy.exp,sympy.log,sympy.sin,sympy.cos, sympy.atan, sympy.tan]
    for s in special_symbols:
        parse_dict[str(s)] = s

    # Read parameters values
    parameters_values = {}
    for p in declarations:
        try:
            rhs = eval(declarations[p], parse_dict)
        except Exception as e:
            Exception("Impossible to evaluate parameter value : " + str(e))
        try:
            lhs = eval(p,parse_dict)
        except Exception as e:
            # here we could declare p
            raise e
        parameters_values[lhs] = rhs

    # Now we read the model block
    model_tags = model_block[0].tags
    equations = []
    for ig in model_block[1:-1]:
        if ig.instruction != '':
            teq = ig.instruction.replace('^',"**")
            if '=' in teq:
                teqlhs,teqrhs = teq.split("=")
            else:
                teqlhs = teq
                teqrhs = '0'
            eqlhs = eval(teqlhs, parse_dict)
            eqrhs = eval(teqrhs, parse_dict)
            eq = Equation(eqlhs,eqrhs)
            eq.tags.update(ig.tags)
    #        if eq.tags.has_key('name'):
    #            eq.tags[] = ig.tags['name']
            equations.append(eq)

    # Now we read the initval block
    init_values = {}
    if initval_block != None:
        for ig in initval_block[1:-1]:
            [lhs,rhs] = ig.instruction.split("=")
            init_values[eval(lhs,parse_dict)] = eval(rhs,parse_dict)

    # Now we read the endval block
    # I don't really care about the endval block !

    end_values = {}
    if endval_block != None:
        for ig in endval_block[1:-1]:
            [lhs,rhs] = ig.instruction.split("=")
            end_values[eval(lhs)] = eval(rhs)

    # Now we read the shocks block
    covariances = None
    if shocks_block != None:
        covariances = sympy.zeros(len(shocks))
        regex1 = re.compile("var (.*?),(.*?)=(.*)|var (.*?)=(.*)")
        for ig in shocks_block[1:-1]:
            m = regex1.match(ig.instruction)
            if not m:
                raise Exception("unrecognized instruction in block shocks : " + str(ig.instruction))
            if m.group(1) != None:
                varname1 = m.group(1).strip()
                varname2 = m.group(2).strip()
                value = m.group(3).strip().replace("^","**")
            elif m.group(4) != None:
                varname1 = m.group(4).strip()
                varname2 = varname1
                value = m.group(5).strip().replace("^","**")
            i = varexo_names.index(varname1)
            j = varexo_names.index(varname2)
            covariances[i,j] = eval(value,parse_dict)
            covariances[j,i] = eval(value,parse_dict)

    resp = dict()
    resp['variables_ordering'] = variables
    resp['parameters_ordering'] = parameters
    resp['shocks_ordering'] = shocks
    resp['equations'] = equations
    resp['parameters_values'] = parameters_values
    resp['init_values'] = init_values
    resp['covariances'] = covariances
    if fname:
        resp['name'] = fname

    model = Model(**resp)
    return model

class Instruction_group():

    def __init__(self,s):
        self.string = s
        self.span = 1 + s.count("\n")
        self.tags = {}
        self.instruction = ""
        self.process_lines()
        return None
    def __repr__(self):
        return str([self.instruction,self.tags])
    def process_lines(self):
        lines = []
        entire_instruction = ""
        for l in self.string.split("\n"):
            g = line_regex.match(l).groups()
            matches = [i for i in range(len(g)) if g[i]!=None]
            if len(matches) !=1 :
                raise Exception( "Parsing error" )
            else:
                i = matches[0]
            if i == 0:
                # this line is blank, just ignore it
                lines.append(["blank",g[i]])
            if i == 1:
                # this line contains tag, read them immediately
                lines.append(["tags",g[i]])
                keys_tags = [kt.split(":") for kt in g[i].split('#')]
                for kt in keys_tags:
                    if len(kt) > 1:
                        self.tags[kt[0].strip()] = kt[1].strip()
            if i == 2:
                # this line contains macro instructions
                lines.append(["macro",g[i]])
            if i == 3:
                # there are regular comments on this line
                lines.append(["comment",g[i]])
            if i == 4:
                # new style tags
                lines.append(["tags",g[i]])
                pairs = [tag_regex.match(kt).groups() for kt in g[i].split(',')]
                for p in pairs:
                    if len(p)>1:
                        self.tags[p[0]] = p[1].strip()
            if i == 5:
                entire_instruction += (" " + g[i])
                lines.append(("instruction",g[i]))
        self.instruction = entire_instruction.strip()
        self.lines = lines

def dynare_import(filename,names_dict={},full_output=False):
    '''Imports model defined in specified file'''
    import os
    basename = os.path.basename(filename)
    fname = re.compile('(.*)\.mod').match(basename).group(1)
    f = file(filename)
    txt = f.read()
    model = parse_dynare_text(txt,names_dict=names_dict,full_output=full_output)
    model['name'] = fname
    return model

def undeclare_variables_not_in_equations():
    frame = inspect.currentframe().f_back
    equations = frame.f_globals['equations']
    variables = frame.f_globals['variables']
    init_values = frame.f_globals['init_values']
    l = set()
    for eq in equations:
        l=l.union(  [v.P for v in eq.variables] )
    absent_variables = [v for v in variables if not v in l]
    for v in absent_variables:
        variables.remove(v)
        if v in init_values:
            init_values.pop(v)
    del(frame)<commit_msg>Bugfix in modfile import.<commit_after>from dolo.symbolic.symbolic import *
from dolo.symbolic.model import *

import sympy
import re
import inspect

line_regex = re.compile(
    "(\s*)$|"
    +"\s*//#(.*)$|"
    +"\s*@#(.*)$|"
    +"\s*//[^#](.*)$|"
    +"\s*\[(.*)\]\s*$|"
    +"(\s*[^\s].*)$"
)
tag_regex = re.compile("\s*(\w+)\s*=\s*'(.*)'")

def parse_dynare_text(txt,add_model=True,full_output=False,names_dict = {}):
    '''
    Imports the content of a modfile into the current interpreter scope
    '''
    # here we call "instruction group", a string finishing by a semicolon
    # an "instruction group" can have several lines
    # a line can be
    # - a comment //...
    # - an old-style tag //$...
    # - a new-style tag [key1='value1',..]
    # - macro-instruction @#...
    # A Modfile contains several blocks (in this order) :
    # - an initblock defining variables, exovariables, parameters, initialization
    #   inside the initblock the order of declaration doesn't matter
    # - a model block with two special lines (model; end;)
    # - optional blocks (like endval, shocks)
    #    seperated by free matlab instructions in any order;
    # - all other instructions are ignored

    otxt = txt
    otxt = otxt.replace("\r\n","\n")
    otxt = otxt.replace("^","**")

    # first, we remove end-of-line comments : they are definitely lost
    regex = re.compile("(.+)//[^#](.*)")
    def remove_end_comment(line):
        res = regex.search(line)
        if res:
            l = res.groups(1)[0]
            return(l)
        else:
            return line
    txt = str.join("\n",map(remove_end_comment,otxt.split("\n")))

    name_regex = re.compile("//\s*fname\s*=\s*'(.*)'")
    m = name_regex.search(txt)
    if m:
        fname = m.group(1)
    else:
        fname = None

    instruction_groups = [Instruction_group(s) for s in txt.split(";")]

    instructions = [ig.instruction for ig in instruction_groups]

    try:
        imodel = [re.compile('model(\(.*\)|)').match(e) is not None for e in instructions]
        imodel = imodel.index(True)
        #imodel = instructions.index("model") #this doesn't work for "MODEL"
        iend = instructions.index("end")
        model_block = instruction_groups[imodel:(iend+1)]
        init_block = instruction_groups[0:imodel]
    except:
        raise Exception('Model block could not be found.')

    next_instructions = instructions[(iend+1):]
    next_instruction_groups = instruction_groups[(iend+1):]

    if 'initval' in next_instructions:
        iinitval = next_instructions.index('initval')
        iend = next_instructions.index('end',iinitval)
        matlab_block_1 = next_instruction_groups[0:iinitval]
        initval_block = next_instruction_groups[iinitval:(iend+1)]
        next_instruction_groups = next_instruction_groups[(iend+1):]
        next_instructions = next_instructions[(iend+1):]
    else:
        initval_block = None
        matlab_block_1 = None

    if 'endval' in next_instructions:
        iendval = next_instructions.index('endval')
        iend = next_instructions.index('end',iendval)
        matlab_block_2 = next_instruction_groups[0:iendval]
        endval_block = next_instruction_groups[iendval:(iend+1)]
        next_instruction_groups = next_instruction_groups[(iend+1):]
        next_instructions = next_instructions[(iend+1):]
    else:
        endval_block = None
        matlab_block_2 = None

    # TODO : currently shocks block needs to follow initval, this restriction should be removed
    if 'shocks' in next_instructions:
        ishocks = next_instructions.index('shocks')
        iend = next_instructions.index('end',ishocks)
        matlab_block_3 = next_instruction_groups[0:ishocks]
        shocks_block = next_instruction_groups[ishocks:(iend+1)]
        next_instruction_groups = next_instruction_groups[(iend+1):]
        next_instructions = next_instructions[(iend+1):]
    else:
        shocks_block = None
        matlab_block_3 = None

    try:
        init_regex = re.compile("(parameters |var |varexo |)(.*)")
        var_names = []
        varexo_names = []
        parameters_names = []
        declarations = {}
        for ig in init_block:
            if ig.instruction != '':
                m = init_regex.match(ig.instruction)
                if not m:
                    raise Exception("Unexpected instruction in init block : " + str(ig.instruction))
                if m.group(1) == '':
                    [lhs,rhs] = m.group(2).split("=")
                    lhs = lhs.strip()
                    rhs = rhs.strip()
                    declarations[lhs] = rhs
                else:
                    arg = m.group(2).replace(","," ")
                    names = [vn.strip() for vn in arg.split()]
                    if m.group(1).strip() == 'var':
                        dest = var_names
                    elif m.group(1).strip() == 'varexo':
                        dest = varexo_names
                    elif m.group(1).strip() == 'parameters':
                        dest = parameters_names
                    for n in names:
                        if not n in dest:
                            dest.append(n)
                        else:
                            raise Exception("symbol %s has already been defined".format(n))
    except Exception as e:
        raise Exception('Initval could not be read : ' + str(e) )
    # the following instruction set the variables "variables","shocks","parameters"

    variables = []
    for vn in var_names:
        if vn in names_dict:
            latex_name = names_dict[vn]
        else:
            latex_name = None
        v = Variable(vn,0)
        variables.append(v)

    shocks = []
    for vn in varexo_names:
        if vn in names_dict:
            latex_name = names_dict[vn]
        else:
            latex_name = None
        s = Shock(vn,0)
        shocks.append(s)

    parameters = []
    for vn in parameters_names:
        if vn in names_dict:
            latex_name = names_dict[vn]
        else:
            latex_name = None
        p = Parameter(vn)
        parameters.append(p)

    parse_dict = dict()
    for v in variables + shocks + parameters:
        parse_dict[v.name] = v

    special_symbols = [sympy.exp,sympy.log,sympy.sin,sympy.cos, sympy.atan, sympy.tan]
    for s in special_symbols:
        parse_dict[str(s)] = s

    # Read parameters values
    parameters_values = {}
    for p in declarations:
        try:
            rhs = eval(declarations[p], parse_dict)
        except Exception as e:
            Exception("Impossible to evaluate parameter value : " + str(e))
        try:
            lhs = eval(p,parse_dict)
        except Exception as e:
            # here we could declare p
            raise e
        parameters_values[lhs] = rhs

    # Now we read the model block
    model_tags = model_block[0].tags
    equations = []
    for ig in model_block[1:-1]:
        if ig.instruction != '':
            teq = ig.instruction.replace('^',"**")
            if '=' in teq:
                teqlhs,teqrhs = teq.split("=")
            else:
                teqlhs = teq
                teqrhs = '0'
            eqlhs = eval(teqlhs, parse_dict)
            eqrhs = eval(teqrhs, parse_dict)
            eq = Equation(eqlhs,eqrhs)
            eq.tags.update(ig.tags)
    #        if eq.tags.has_key('name'):
    #            eq.tags[] = ig.tags['name']
            equations.append(eq)

    # Now we read the initval block
    init_values = {}
    if initval_block != None:
        for ig in initval_block[1:-1]:
            [lhs,rhs] = ig.instruction.split("=")
            init_values[eval(lhs,parse_dict)] = eval(rhs,parse_dict)

    # Now we read the endval block
    # I don't really care about the endval block !

    end_values = {}
    if endval_block != None:
        for ig in endval_block[1:-1]:
            [lhs,rhs] = ig.instruction.split("=")
            end_values[eval(lhs)] = eval(rhs)

    # Now we read the shocks block
    covariances = None
    if shocks_block != None:
        covariances = sympy.zeros(len(shocks))
        regex1 = re.compile("var (.*?),(.*?)=(.*)|var (.*?)=(.*)")
        for ig in shocks_block[1:-1]:
            m = regex1.match(ig.instruction)
            if not m:
                raise Exception("unrecognized instruction in block shocks : " + str(ig.instruction))
            if m.group(1) != None:
                varname1 = m.group(1).strip()
                varname2 = m.group(2).strip()
                value = m.group(3).strip().replace("^","**")
            elif m.group(4) != None:
                varname1 = m.group(4).strip()
                varname2 = varname1
                value = m.group(5).strip().replace("^","**")
            i = varexo_names.index(varname1)
            j = varexo_names.index(varname2)
            covariances[i,j] = eval(value,parse_dict)
            covariances[j,i] = eval(value,parse_dict)

    resp = dict()
    resp['variables_ordering'] = variables
    resp['parameters_ordering'] = parameters
    resp['shocks_ordering'] = shocks
    resp['equations'] = equations
    resp['parameters_values'] = parameters_values
    resp['init_values'] = init_values
    resp['covariances'] = covariances
    if fname:
        resp['name'] = fname

    model = Model(**resp)
    return model

class Instruction_group():

    def __init__(self,s):
        self.string = s
        self.span = 1 + s.count("\n")
        self.tags = {}
        self.instruction = ""
        self.process_lines()
        return None
    def __repr__(self):
        return str([self.instruction,self.tags])
    def process_lines(self):
        lines = []
        entire_instruction = ""
        for l in self.string.split("\n"):
            g = line_regex.match(l).groups()
            matches = [i for i in range(len(g)) if g[i]!=None]
            if len(matches) !=1 :
                raise Exception( "Parsing error" )
            else:
                i = matches[0]
            if i == 0:
                # this line is blank, just ignore it
                lines.append(["blank",g[i]])
            if i == 1:
                # this line contains tag, read them immediately
                lines.append(["tags",g[i]])
                keys_tags = [kt.split(":") for kt in g[i].split('#')]
                for kt in keys_tags:
                    if len(kt) > 1:
                        self.tags[kt[0].strip()] = kt[1].strip()
            if i == 2:
                # this line contains macro instructions
                lines.append(["macro",g[i]])
            if i == 3:
                # there are regular comments on this line
                lines.append(["comment",g[i]])
            if i == 4:
                # new style tags
                lines.append(["tags",g[i]])
                pairs = [tag_regex.match(kt).groups() for kt in g[i].split(',')]
                for p in pairs:
                    if len(p)>1:
                        self.tags[p[0]] = p[1].strip()
            if i == 5:
                entire_instruction += (" " + g[i])
                lines.append(("instruction",g[i]))
        self.instruction = entire_instruction.strip()
        self.lines = lines

def dynare_import(filename,names_dict={},full_output=False):
    '''Imports model defined in specified file'''
    import os
    basename = os.path.basename(filename)
    fname = re.compile('(.*)\.mod').match(basename).group(1)
    f = file(filename)
    txt = f.read()
    model = parse_dynare_text(txt,names_dict=names_dict,full_output=full_output)
    model['name'] = fname
    return model

def undeclare_variables_not_in_equations():
    frame = inspect.currentframe().f_back
    equations = frame.f_globals['equations']
    variables = frame.f_globals['variables']
    init_values = frame.f_globals['init_values']
    l = set()
    for eq in equations:
        l=l.union(  [v.P for v in eq.variables] )
    absent_variables = [v for v in variables if not v in l]
    for v in absent_variables:
        variables.remove(v)
        if v in init_values:
            init_values.pop(v)
    del(frame)
<|endoftext|>


<commit_before><commit_msg>cannot send order when strategy is hold<commit_after><|endoftext|>


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

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

import argparse
import wrenet
from wrenet.wrenet import get_interfaces

def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('path', help='Windows system registry file')
    parser.add_argument('--version', action='version',
                        version='%(prog)s ' + wrenet.__version__)
    parser.add_argument('-r', '--root', action='store_true',
                        help='Use the Windows mount point as path')
    args = parser.parse_args()

    if args.root == True:
        args.path = args.path + "/Windows/System32/config/SYSTEM"

    for interface in get_interfaces(args.path):
        interface.print_all()
        print()

if __name__ == "__main__":
    main()
<commit_msg>Error Handling<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-

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

import argparse
import wrenet
import Registry
from wrenet.wrenet import get_interfaces

parser = argparse.ArgumentParser()
parser.add_argument('path', help='Windows system registry file')
parser.add_argument('--version', action='version',
                    version='%(prog)s ' + wrenet.__version__)
parser.add_argument('-r', '--root', action='store_true',
                    help='Use the Windows mount point as path')
args = parser.parse_args()

def main():
    if args.root == True:
        args.path = args.path + "/Windows/System32/config/SYSTEM"
    try:
        interfaces = get_interfaces(args.path)
        for interface in interfaces:
            interface.print_all()
            print()
    except Registry.RegistryParse.ParseException as e:
        print("%s: %s: Invalid registry file" % (parser.prog, args.path))
    except Exception as e:
        print("%s: %s: %s" % (parser.prog, args.path, e.args[1]))

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


<commit_before>import unittest

import numpy as np

import cupy
from cupy import testing

@testing.gpu
class TestArrayUfunc(unittest.TestCase):

    @testing.for_all_dtypes()
    def test_unary_op(self, dtype):
        a = cupy.array(np.array([0, 1, 2]), dtype=dtype)
        outa = np.sin(a)
        # numpy operation produced a cupy array
        assert isinstance(outa, cupy.ndarray)
        b = a.get()
        outb = np.sin(b)
        assert np.allclose(outa.get(), outb)

    @testing.for_all_dtypes()
    def test_unary_op_out(self, dtype):
        a = cupy.array(np.array([0, 1, 2]), dtype=dtype)
        b = a.get()
        outb = np.sin(b)
        # pre-make output with same type as input
        outa = cupy.array(np.array([0, 1, 2]), dtype=outb.dtype)
        np.sin(a, out=outa)
        assert np.allclose(outa.get(), outb)

    @testing.for_all_dtypes()
    def test_binary_op(self, dtype):
        a1 = cupy.array(np.array([0, 1, 2]), dtype=dtype)
        a2 = cupy.array(np.array([0, 1, 2]), dtype=dtype)
        outa = np.add(a1, a2)
        # numpy operation produced a cupy array
        assert isinstance(outa, cupy.ndarray)
        b1 = a1.get()
        b2 = a2.get()
        outb = np.add(b1, b2)
        assert np.allclose(outa.get(), outb)

    @testing.for_all_dtypes()
    def test_binary_op_out(self, dtype):
        a1 = cupy.array(np.array([0, 1, 2]), dtype=dtype)
        a2 = cupy.array(np.array([0, 1, 2]), dtype=dtype)
        outa = cupy.array(np.array([0, 1, 2]), dtype=dtype)
        np.add(a1, a2, out=outa)
        b1 = a1.get()
        b2 = a2.get()
        outb = np.add(b1, b2)
        assert np.allclose(outa.get(), outb)

    @testing.for_all_dtypes()
    def test_binary_mixed_op(self, dtype):
        a1 = cupy.array(np.array([0, 1, 2]), dtype=dtype)
        a2 = cupy.array(np.array([0, 1, 2]), dtype=dtype).get()
        with self.assertRaises(TypeError):
            # attempt to add cupy and numpy arrays
            np.add(a1, a2)
        with self.assertRaises(TypeError):
            # check reverse order
            np.add(a2, a1)
        with self.assertRaises(TypeError):
            # reject numpy output from cupy
            np.add(a1, a1, out=a2)
        with self.assertRaises(TypeError):
            # reject cupy output from numpy
            np.add(a2, a2, out=a1)
        with self.assertRaises(ValueError):
            # bad form for out=
            # this is also an error with numpy array
            np.sin(a1, out=())
        with self.assertRaises(ValueError):
            # bad form for out=
            # this is also an error with numpy array
            np.sin(a1, out=(a1, a1))

    @testing.numpy_cupy_array_equal()
    def test_indexing(self, xp):
        a = cupy.testing.shaped_arange((3, 1), xp)[:, :, None]
        b = cupy.testing.shaped_arange((3, 2), xp)[:, None, :]
        return a * b

    @testing.numpy_cupy_array_equal()
    def test_shares_memory(self, xp):
        a = cupy.testing.shaped_arange((1000, 1000), xp, 'int64')
        b = xp.transpose(a)
        a += b
        return a
<commit_msg>Test `types` attr of ufunc<commit_after>import unittest

import numpy as np
import pytest

import cupy
from cupy import testing

@testing.gpu
class TestArrayUfunc(unittest.TestCase):

    @testing.for_all_dtypes()
    def test_unary_op(self, dtype):
        a = cupy.array(np.array([0, 1, 2]), dtype=dtype)
        outa = np.sin(a)
        # numpy operation produced a cupy array
        assert isinstance(outa, cupy.ndarray)
        b = a.get()
        outb = np.sin(b)
        assert np.allclose(outa.get(), outb)

    @testing.for_all_dtypes()
    def test_unary_op_out(self, dtype):
        a = cupy.array(np.array([0, 1, 2]), dtype=dtype)
        b = a.get()
        outb = np.sin(b)
        # pre-make output with same type as input
        outa = cupy.array(np.array([0, 1, 2]), dtype=outb.dtype)
        np.sin(a, out=outa)
        assert np.allclose(outa.get(), outb)

    @testing.for_all_dtypes()
    def test_binary_op(self, dtype):
        a1 = cupy.array(np.array([0, 1, 2]), dtype=dtype)
        a2 = cupy.array(np.array([0, 1, 2]), dtype=dtype)
        outa = np.add(a1, a2)
        # numpy operation produced a cupy array
        assert isinstance(outa, cupy.ndarray)
        b1 = a1.get()
        b2 = a2.get()
        outb = np.add(b1, b2)
        assert np.allclose(outa.get(), outb)

    @testing.for_all_dtypes()
    def test_binary_op_out(self, dtype):
        a1 = cupy.array(np.array([0, 1, 2]), dtype=dtype)
        a2 = cupy.array(np.array([0, 1, 2]), dtype=dtype)
        outa = cupy.array(np.array([0, 1, 2]), dtype=dtype)
        np.add(a1, a2, out=outa)
        b1 = a1.get()
        b2 = a2.get()
        outb = np.add(b1, b2)
        assert np.allclose(outa.get(), outb)

    @testing.for_all_dtypes()
    def test_binary_mixed_op(self, dtype):
        a1 = cupy.array(np.array([0, 1, 2]), dtype=dtype)
        a2 = cupy.array(np.array([0, 1, 2]), dtype=dtype).get()
        with self.assertRaises(TypeError):
            # attempt to add cupy and numpy arrays
            np.add(a1, a2)
        with self.assertRaises(TypeError):
            # check reverse order
            np.add(a2, a1)
        with self.assertRaises(TypeError):
            # reject numpy output from cupy
            np.add(a1, a1, out=a2)
        with self.assertRaises(TypeError):
            # reject cupy output from numpy
            np.add(a2, a2, out=a1)
        with self.assertRaises(ValueError):
            # bad form for out=
            # this is also an error with numpy array
            np.sin(a1, out=())
        with self.assertRaises(ValueError):
            # bad form for out=
            # this is also an error with numpy array
            np.sin(a1, out=(a1, a1))

    @testing.numpy_cupy_array_equal()
    def test_indexing(self, xp):
        a = cupy.testing.shaped_arange((3, 1), xp)[:, :, None]
        b = cupy.testing.shaped_arange((3, 2), xp)[:, None, :]
        return a * b

    @testing.numpy_cupy_array_equal()
    def test_shares_memory(self, xp):
        a = cupy.testing.shaped_arange((1000, 1000), xp, 'int64')
        b = xp.transpose(a)
        a += b
        return a

class TestUfunc:
    @pytest.mark.parametrize('ufunc', [
        'add',
        'sin',
    ])
    @testing.numpy_cupy_equal()
    def test_types(self, xp, ufunc):
        types = getattr(xp, ufunc).types
        if xp == np:
            assert isinstance(types, list)
            types = list(dict.fromkeys(  # remove dups: numpy/numpy#7897
                sig for sig in types
                # CuPy does not support the following dtypes:
                # (c)longdouble, datetime, timedelta, and object.
                if not any(t in sig for t in 'GgMmO')
            ))
        return types
<|endoftext|>