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

import os
import sublime
from glob import glob

from ..platformio.project_recognition import ProjectRecognition
from .quick_panel import quick_panel
from .paths import getBoardsFileDataPath
from .tools import get_setting, save_setting, save_sysetting
from .preferences_bridge import PreferencesBridge
from .I18n import I18n

class QuickMenu(PreferencesBridge):
    def __init__(self):
        super(QuickMenu, self).__init__()
        self.index = 0
        self.quick_list = []
        self.deeper = 0
        self.history = {}
        self.translate = I18n().translate

    def set_list(self, quick_list):
        """Set List

        Set the list with the items to be shown in the
        quick panel

        Arguments:
            quick_list {list} -- list of items
        """
        self.quick_list = quick_list

    def show_quick_panel(self, callback):
        """Quick Panel

        Show the quick panel with the given items, previously setted
        in the quick_list object. The callback can set the index
        object to selected an item when the panel is called.

        Arguments:
            callback {obj} -- callback to call after the selection
        """
        quick_panel(self.quick_list, callback, index=self.index)

    def callback_board(self, selected):
        """Board Callback

        The quick panel returns the index of the option selected,
        this index is used to get the id of the board and the id
        is stored in the setting file.

        last_action is used to run the compilation or upload if was
        the action before call the list of boards

        Arguments:
            selected {int} -- index of the selected board
        """
        if(selected == -1):
            save_sysetting('last_action', None)
            return

        boards_list = self.boards_list()
        board_select = boards_list[selected][-1]
        board_id = board_select.split("|")[-1].strip()

        self.save_selected_board(board_id)
        self.run_last_action()
        self.set_status_information()

    def boards_list(self):
        """Boards List

        PlatformIO returns a JSON list with all information of the boards,
        the quick panel requires a list with a different format. We will only
        show the name (caption), id and vendor.

        Returns:
            list -- boards list
        """
        from .file import File

        selected_boards = self.get_selected_boards()
        boards_path = getBoardsFileDataPath()
        boards_file = File(boards_path)
        boards = boards_file.read_json()
        boards_list = []
        start = ''

        for board in boards:
            id = board['id']
            vendor = board['vendor']

            if(id in selected_boards):
                start = '* '
            else:
                start = ''

            caption = start + board['name']
            extra = "%s | %s" % (vendor, id)
            boards_list.append([caption, extra])

        return boards_list

    def callback_environment(self, selected):
        """Environment Callback

        Callback to store the select environment

        last_action is used to run the compilation or upload if was
        the action before call the list of environments

        Arguments:
            selected {int} -- option selected (index)
        """
        if(selected == -1):
            save_sysetting('last_action', None)
            return

        environments_list = self.environment_list()
        environment_select = environments_list[selected][1]
        environment = environment_select.split("|")[-1].strip()

        self.save_environment(environment)
        self.run_last_action()
        self.set_status_information()

    def environment_list(self):
        """
        gets a list with all selected environments and format it
        to be shown in the quick panel
        """
        from .file import File

        environments_list = []
        boards = self.boards_list()
        environments = self.get_selected_boards()
        environment = self.get_environment()
        new_environments = environments

        index = 0
        total = len(environments)
        count = total

        if(environments):
            for value in boards:
                data = value[1].split("|")
                caption = value[0][2:]
                id = data[1].strip()
                vendor = data[0].strip()

                for listed in environments:
                    if(listed == id):
                        vendor = "%s | %s" % (vendor, id)
                        environments_list.append([caption, vendor])
                        new_environments.remove(listed)
                        count -= 1

                        if(environment == listed):
                            self.index = index

                        index += 1

                if(not count):
                    break

        if(new_environments):
            for board in new_environments:
                caption = board
                vendor = self.translate('unknown') + " | {0}".format(board)
                environments_list.append([caption, vendor])

        return environments_list

    def callback_overwrite_baud(self, selected):
        """Baud rate callback

        Stores the option selected in the preferences file

        Arguments:
            selected {int} -- index of the selected baud rate
        """
        if(selected == -1):
            return

        selected = self.quick_list[selected]
        selected = None if selected == 'None' else selected

        save_setting('upload_speed', selected)

    def overwrite_baud_list(self):
        """Baud rate list

        List of baud rates used to overwrite the upload speed

        Returns:
            list -- list of baud rates
        """
        current = get_setting('upload_speed', "None")
        items = QuickMenu.baudrate_list()
        self.index = items.index(current)

        return items

    def callback_serial_ports(self, selected):
        """Selected Port Callback

        Stores the selected serial port in the preferences file

        Arguments:
            selected {str} -- Port selected ex. 'COM1'
        """
        if(selected <= 0):
            save_sysetting('last_action', None)
            return

        if(selected == 1):
            self.window.run_command('deviot_set_ip')
            return

        if(selected == 2):
            port_selected = "not"
        else:
            port_selected = self.quick_list[selected][2]

        save_setting('port_id', port_selected)

        self.run_last_action()
        self.set_status_information()

    def serial_list(self):
        """Serial Port List

        Gets the list of serial ports and mdns services and
        return it

        Returns:
            list -- available serial ports/mdns services
        """
        index = 2
        header = self.translate("port_list").upper()
        ports_list = self.get_ports_list()
        ports_list.insert(0, [header, self.translate("select_port_list")])
        ports_list.insert(1, [self.translate("menu_add_ip"), self.translate("add_ip_subtitle")])
        ports_list.insert(2, [self.translate("menu_not_used_port"), self.translate("not_used_subtitle")])
        current = get_setting('port_id', None)

        if(len(ports_list) < 2):
            ports_list = [self.translate("menu_no_serial_mdns").upper()]

        for port in ports_list[2:]:
            if(current in port):
                self.index = index
            index += 1

        return ports_list

    def language_list(self):
        """Language List

        Builts the list with the available languages in Deviot

        Returns:
            list -- English language / Language String list
        """

        i18n = I18n()
        index = 0
        language_list = []
        lang_ids = i18n.get_lang_ids()
        current = get_setting('lang_id', 'en')

        for lang_id in lang_ids:
            language = i18n.get_lang_name(lang_id)
            language_list.append([language[1], language[0]])

            if(current == lang_id):
                self.index = index

            index += 1

        return language_list

    def callback_language(self, selected):
        """Language Callback

        Stores the user language selection

        Arguments:
            selected {int} -- user selection
        """
        if(selected == -1):
            return

        from .top_menu import TopMenu

        lang_ids = I18n().get_lang_ids()
        port_selected = lang_ids[selected]

        save_setting('lang_id', port_selected)
        save_setting('compile_lang', True)
        self.window.run_command("deviot_reload")

    def callback_import(self, selected):
        """Import Callback

        After select the library it will be inserted by the insert_libary
        command, it will include the path of the library to includes

        Arguments:
            selected {int} -- user index selection
        """
        if(selected <= 0):
            return

        libraries_list = self.import_list()
        library_import = libraries_list[selected][1]

        window = sublime.active_window()
        window.run_command('deviot_insert_library', {'path': library_import})

    def import_list(self):
        """Import List

        To generate the list of libraries, it search first in the two main folder of the plugin
        the first one in '~/.platformio/packages', that folder contain the libraries includes by default
        by each platform (avr, expressif, etc). The second folder is located in '~/platformio/lib' there
        are stored all the libraries installed by the user from the management libraries

        Returns:
            [list] -- quick panel list with libraries
        """
        from .libraries import get_library_list

        platform = self.get_platform()
        platform = platform if(platform) else "all"

        quick_list = get_library_list(platform=platform)
        quick_list.insert(0, [self.translate("select_library").upper()])

        if(len(quick_list) <= 1):
            quick_list = [[self.translate("menu_no_libraries")]]

        return quick_list

    def quick_libraries(self):
        """List of libraries

        Show the list of libraries availables. The callback will show
        the list of examples.
        """
        from .libraries import get_library_list

        platform = self.get_platform()
        platform = platform if(platform) else "all"

        self.quick_list = get_library_list(example_list=True, platform=platform)
        self.quick_list.insert(0, [self.translate("select_library").upper()])

        if(len(self.quick_list) <= 1):
            self.quick_list = [[self.translate("menu_no_examples")]]

        self.show_quick_panel(self.callback_library)

    def callback_library(self, selected):
        """Show Examples

        After the previous selection of the library, here will be search
        all folders inside of the "example" folder and will be considerated
        an example to open

        Arguments:
            selected {int} -- user index selection
        """
        if(selected <= 0):
            return

        if(selected == 1):
            self.index -= 1

        if(selected == 1 and self.index == 0):
            self.quick_libraries()
            self.index = 0
            self.history = {}
            return

        if(selected == 1 and self.index > 0):
            library_path = self.history[self.index - 1]
            del self.history[len(self.history) - 1]

        if(selected != 1):
            library_path = self.quick_list[selected][1]

            if('examples' not in library_path):
                library_path = os.path.join(library_path, 'examples')

            if(self.open_file(library_path)):
                return

            self.history[self.index] = library_path
            self.index += 1

        library_path = os.path.join(library_path, '*')

        self.quick_list = [[self.translate("select_example").upper()]]
        self.quick_list.append([self.translate("_previous").upper()])

        for files in glob(library_path):
            caption = os.path.basename(files)
            self.quick_list.append([caption, files])

        self.show_quick_panel(self.callback_library)

    def serial_baudrate_list(self):
        """Serial Baudrate

        List of baud rates to use with the serial monitor.
        It check if there is already an option selected and
        set it in the index object.

        Returns:
            [list] -- list of
        """
        current = get_setting('baudrate', "9600")
        items = QuickMenu.baudrate_list()

        try:
            self.index = items.index(current)
        except ValueError:
            self.index = 0

        return items

    def callback_serial_baudrate(self, selected):
        """Serial baud rate callback

        callback to select the baud rate used in the serial
        monitor. The option is stored in the preferences file

        Arguments:
            selected {int} -- index of the selected baud rate
        """
        if(selected == -1):
            return

        selected = self.quick_list[selected]
        selected = None if selected == 'None' else selected

        save_setting('baudrate', selected)

    def line_endings_list(self):
        """Serial ending strings

        List of ending string used in the monitor serial
        """
        items = [
                ["None"],
                ["New Line", "\n"],
                ["Carriage Return", "\r"],
                ["Both NL & CR", "\r\n"]
                ]
        current = get_setting('line_ending', None)

        simplified = [i[1] for i in items if len(i) > 1]
        simplified.insert(0, None)

        self.index = simplified.index(current)

        return items

    def callback_line_endings(self, selected):
        """Callback line endings

        Stores the line ending selected by the user

        Arguments:
            selected {int} -- index user selection
        """
        if(selected == -1):
            return

        try:
            selected = self.quick_list[selected][1]
        except IndexError:
            selected = None

        save_setting('line_ending', selected)

    def display_mode_list(self):
        """Display modes

        List of display modes
        """
        items = [["Text"],["ASCII"],["HEX"],["Mix"]]

        current = get_setting('display_mode', 'Text')
        self.index = items.index([current])

        return items

    def callback_display_mode(self, selected):
        """Display mode callback

        Stores the display mode selected by the user

        Arguments:
            selected {int} -- index user selection
        """
        if(selected == -1):
            return

        selected = self.quick_list[selected][0]
        save_setting('display_mode', selected)

    @staticmethod
    def baudrate_list():
        """Baudrate list

        List of baud rates shown in the monitor serial and upload speed
        quick panels.
        """
        baudrate_list = ["None",
                         "1200",
                         "1800",
                         "2400",
                         "4800",
                         "9600",
                         "19200",
                         "38400",
                         "57600",
                         "74880"
                         "115200",
                         "230400",
                         "460800",
                         "500000",
                         "576000",
                         "921600",
                         "1000000",
                         "1152000"]

        return baudrate_list

    def open_file(self, sketch_path):
        """Open sketch

        search in the given path a ino or pde file extension
        and open it in a new windows when it's found

        Arguments:
            sketch_path {str} -- path (file/folder) where to search
        Returns:
            [bool] -- true if it open file, false if not
        """
        window = sublime.active_window()

        if(sketch_path.endswith(('.ino', '.pde'))):
            window.open_file(sketch_path)
            return True

        for file in os.listdir(sketch_path):
            if(file.endswith(('.ino', '.pde'))):
                file = os.path.join(sketch_path, file)
                window.open_file(file)
                return True
        return False
<commit_msg>added missing comma<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-

import os
import sublime
from glob import glob

from ..platformio.project_recognition import ProjectRecognition
from .quick_panel import quick_panel
from .paths import getBoardsFileDataPath
from .tools import get_setting, save_setting, save_sysetting
from .preferences_bridge import PreferencesBridge
from .I18n import I18n

class QuickMenu(PreferencesBridge):
    def __init__(self):
        super(QuickMenu, self).__init__()
        self.index = 0
        self.quick_list = []
        self.deeper = 0
        self.history = {}
        self.translate = I18n().translate

    def set_list(self, quick_list):
        """Set List

        Set the list with the items to be shown in the
        quick panel

        Arguments:
            quick_list {list} -- list of items
        """
        self.quick_list = quick_list

    def show_quick_panel(self, callback):
        """Quick Panel

        Show the quick panel with the given items, previously setted
        in the quick_list object. The callback can set the index
        object to selected an item when the panel is called.

        Arguments:
            callback {obj} -- callback to call after the selection
        """
        quick_panel(self.quick_list, callback, index=self.index)

    def callback_board(self, selected):
        """Board Callback

        The quick panel returns the index of the option selected,
        this index is used to get the id of the board and the id
        is stored in the setting file.

        last_action is used to run the compilation or upload if was
        the action before call the list of boards

        Arguments:
            selected {int} -- index of the selected board
        """
        if(selected == -1):
            save_sysetting('last_action', None)
            return

        boards_list = self.boards_list()
        board_select = boards_list[selected][-1]
        board_id = board_select.split("|")[-1].strip()

        self.save_selected_board(board_id)
        self.run_last_action()
        self.set_status_information()

    def boards_list(self):
        """Boards List

        PlatformIO returns a JSON list with all information of the boards,
        the quick panel requires a list with a different format. We will only
        show the name (caption), id and vendor.

        Returns:
            list -- boards list
        """
        from .file import File

        selected_boards = self.get_selected_boards()
        boards_path = getBoardsFileDataPath()
        boards_file = File(boards_path)
        boards = boards_file.read_json()
        boards_list = []
        start = ''

        for board in boards:
            id = board['id']
            vendor = board['vendor']

            if(id in selected_boards):
                start = '* '
            else:
                start = ''

            caption = start + board['name']
            extra = "%s | %s" % (vendor, id)
            boards_list.append([caption, extra])

        return boards_list

    def callback_environment(self, selected):
        """Environment Callback

        Callback to store the select environment

        last_action is used to run the compilation or upload if was
        the action before call the list of environments

        Arguments:
            selected {int} -- option selected (index)
        """
        if(selected == -1):
            save_sysetting('last_action', None)
            return

        environments_list = self.environment_list()
        environment_select = environments_list[selected][1]
        environment = environment_select.split("|")[-1].strip()

        self.save_environment(environment)
        self.run_last_action()
        self.set_status_information()

    def environment_list(self):
        """
        gets a list with all selected environments and format it
        to be shown in the quick panel
        """
        from .file import File

        environments_list = []
        boards = self.boards_list()
        environments = self.get_selected_boards()
        environment = self.get_environment()
        new_environments = environments

        index = 0
        total = len(environments)
        count = total

        if(environments):
            for value in boards:
                data = value[1].split("|")
                caption = value[0][2:]
                id = data[1].strip()
                vendor = data[0].strip()

                for listed in environments:
                    if(listed == id):
                        vendor = "%s | %s" % (vendor, id)
                        environments_list.append([caption, vendor])
                        new_environments.remove(listed)
                        count -= 1

                        if(environment == listed):
                            self.index = index

                        index += 1

                if(not count):
                    break

        if(new_environments):
            for board in new_environments:
                caption = board
                vendor = self.translate('unknown') + " | {0}".format(board)
                environments_list.append([caption, vendor])

        return environments_list

    def callback_overwrite_baud(self, selected):
        """Baud rate callback

        Stores the option selected in the preferences file

        Arguments:
            selected {int} -- index of the selected baud rate
        """
        if(selected == -1):
            return

        selected = self.quick_list[selected]
        selected = None if selected == 'None' else selected

        save_setting('upload_speed', selected)

    def overwrite_baud_list(self):
        """Baud rate list

        List of baud rates used to overwrite the upload speed

        Returns:
            list -- list of baud rates
        """
        current = get_setting('upload_speed', "None")
        items = QuickMenu.baudrate_list()
        self.index = items.index(current)

        return items

    def callback_serial_ports(self, selected):
        """Selected Port Callback

        Stores the selected serial port in the preferences file

        Arguments:
            selected {str} -- Port selected ex. 'COM1'
        """
        if(selected <= 0):
            save_sysetting('last_action', None)
            return

        if(selected == 1):
            self.window.run_command('deviot_set_ip')
            return

        if(selected == 2):
            port_selected = "not"
        else:
            port_selected = self.quick_list[selected][2]

        save_setting('port_id', port_selected)

        self.run_last_action()
        self.set_status_information()

    def serial_list(self):
        """Serial Port List

        Gets the list of serial ports and mdns services and
        return it

        Returns:
            list -- available serial ports/mdns services
        """
        index = 2
        header = self.translate("port_list").upper()
        ports_list = self.get_ports_list()
        ports_list.insert(0, [header, self.translate("select_port_list")])
        ports_list.insert(1, [self.translate("menu_add_ip"), self.translate("add_ip_subtitle")])
        ports_list.insert(2, [self.translate("menu_not_used_port"), self.translate("not_used_subtitle")])
        current = get_setting('port_id', None)

        if(len(ports_list) < 2):
            ports_list = [self.translate("menu_no_serial_mdns").upper()]

        for port in ports_list[2:]:
            if(current in port):
                self.index = index
            index += 1

        return ports_list

    def language_list(self):
        """Language List

        Builts the list with the available languages in Deviot

        Returns:
            list -- English language / Language String list
        """

        i18n = I18n()
        index = 0
        language_list = []
        lang_ids = i18n.get_lang_ids()
        current = get_setting('lang_id', 'en')

        for lang_id in lang_ids:
            language = i18n.get_lang_name(lang_id)
            language_list.append([language[1], language[0]])

            if(current == lang_id):
                self.index = index

            index += 1

        return language_list

    def callback_language(self, selected):
        """Language Callback

        Stores the user language selection

        Arguments:
            selected {int} -- user selection
        """
        if(selected == -1):
            return

        from .top_menu import TopMenu

        lang_ids = I18n().get_lang_ids()
        port_selected = lang_ids[selected]

        save_setting('lang_id', port_selected)
        save_setting('compile_lang', True)
        self.window.run_command("deviot_reload")

    def callback_import(self, selected):
        """Import Callback

        After select the library it will be inserted by the insert_libary
        command, it will include the path of the library to includes

        Arguments:
            selected {int} -- user index selection
        """
        if(selected <= 0):
            return

        libraries_list = self.import_list()
        library_import = libraries_list[selected][1]

        window = sublime.active_window()
        window.run_command('deviot_insert_library', {'path': library_import})

    def import_list(self):
        """Import List

        To generate the list of libraries, it search first in the two main folder of the plugin
        the first one in '~/.platformio/packages', that folder contain the libraries includes by default
        by each platform (avr, expressif, etc). The second folder is located in '~/platformio/lib' there
        are stored all the libraries installed by the user from the management libraries

        Returns:
            [list] -- quick panel list with libraries
        """
        from .libraries import get_library_list

        platform = self.get_platform()
        platform = platform if(platform) else "all"

        quick_list = get_library_list(platform=platform)
        quick_list.insert(0, [self.translate("select_library").upper()])

        if(len(quick_list) <= 1):
            quick_list = [[self.translate("menu_no_libraries")]]

        return quick_list

    def quick_libraries(self):
        """List of libraries

        Show the list of libraries availables. The callback will show
        the list of examples.
        """
        from .libraries import get_library_list

        platform = self.get_platform()
        platform = platform if(platform) else "all"

        self.quick_list = get_library_list(example_list=True, platform=platform)
        self.quick_list.insert(0, [self.translate("select_library").upper()])

        if(len(self.quick_list) <= 1):
            self.quick_list = [[self.translate("menu_no_examples")]]

        self.show_quick_panel(self.callback_library)

    def callback_library(self, selected):
        """Show Examples

        After the previous selection of the library, here will be search
        all folders inside of the "example" folder and will be considerated
        an example to open

        Arguments:
            selected {int} -- user index selection
        """
        if(selected <= 0):
            return

        if(selected == 1):
            self.index -= 1

        if(selected == 1 and self.index == 0):
            self.quick_libraries()
            self.index = 0
            self.history = {}
            return

        if(selected == 1 and self.index > 0):
            library_path = self.history[self.index - 1]
            del self.history[len(self.history) - 1]

        if(selected != 1):
            library_path = self.quick_list[selected][1]

            if('examples' not in library_path):
                library_path = os.path.join(library_path, 'examples')

            if(self.open_file(library_path)):
                return

            self.history[self.index] = library_path
            self.index += 1

        library_path = os.path.join(library_path, '*')

        self.quick_list = [[self.translate("select_example").upper()]]
        self.quick_list.append([self.translate("_previous").upper()])

        for files in glob(library_path):
            caption = os.path.basename(files)
            self.quick_list.append([caption, files])

        self.show_quick_panel(self.callback_library)

    def serial_baudrate_list(self):
        """Serial Baudrate

        List of baud rates to use with the serial monitor.
        It check if there is already an option selected and
        set it in the index object.

        Returns:
            [list] -- list of
        """
        current = get_setting('baudrate', "9600")
        items = QuickMenu.baudrate_list()

        try:
            self.index = items.index(current)
        except ValueError:
            self.index = 0

        return items

    def callback_serial_baudrate(self, selected):
        """Serial baud rate callback

        callback to select the baud rate used in the serial
        monitor. The option is stored in the preferences file

        Arguments:
            selected {int} -- index of the selected baud rate
        """
        if(selected == -1):
            return

        selected = self.quick_list[selected]
        selected = None if selected == 'None' else selected

        save_setting('baudrate', selected)

    def line_endings_list(self):
        """Serial ending strings

        List of ending string used in the monitor serial
        """
        items = [
                ["None"],
                ["New Line", "\n"],
                ["Carriage Return", "\r"],
                ["Both NL & CR", "\r\n"]
                ]
        current = get_setting('line_ending', None)

        simplified = [i[1] for i in items if len(i) > 1]
        simplified.insert(0, None)

        self.index = simplified.index(current)

        return items

    def callback_line_endings(self, selected):
        """Callback line endings

        Stores the line ending selected by the user

        Arguments:
            selected {int} -- index user selection
        """
        if(selected == -1):
            return

        try:
            selected = self.quick_list[selected][1]
        except IndexError:
            selected = None

        save_setting('line_ending', selected)

    def display_mode_list(self):
        """Display modes

        List of display modes
        """
        items = [["Text"],["ASCII"],["HEX"],["Mix"]]

        current = get_setting('display_mode', 'Text')
        self.index = items.index([current])

        return items

    def callback_display_mode(self, selected):
        """Display mode callback

        Stores the display mode selected by the user

        Arguments:
            selected {int} -- index user selection
        """
        if(selected == -1):
            return

        selected = self.quick_list[selected][0]
        save_setting('display_mode', selected)

    @staticmethod
    def baudrate_list():
        """Baudrate list

        List of baud rates shown in the monitor serial and upload speed
        quick panels.
        """
        baudrate_list = ["None",
                         "1200",
                         "1800",
                         "2400",
                         "4800",
                         "9600",
                         "19200",
                         "38400",
                         "57600",
                         "74880",
                         "115200",
                         "230400",
                         "460800",
                         "500000",
                         "576000",
                         "921600",
                         "1000000",
                         "1152000"]

        return baudrate_list

    def open_file(self, sketch_path):
        """Open sketch

        search in the given path a ino or pde file extension
        and open it in a new windows when it's found

        Arguments:
            sketch_path {str} -- path (file/folder) where to search
        Returns:
            [bool] -- true if it open file, false if not
        """
        window = sublime.active_window()

        if(sketch_path.endswith(('.ino', '.pde'))):
            window.open_file(sketch_path)
            return True

        for file in os.listdir(sketch_path):
            if(file.endswith(('.ino', '.pde'))):
                file = os.path.join(sketch_path, file)
                window.open_file(file)
                return True
        return False
<|endoftext|>


<commit_before>import os
import argparse
#
import numpy
#
import tabular_predDB.python_utils.data_utils as du
import tabular_predDB.python_utils.general_utils as gu
import tabular_predDB.python_utils.xnet_utils as xu
import tabular_predDB.LocalEngine as LE
import tabular_predDB.HadoopEngine as HE
import tabular_predDB.cython_code.State as State

def get_generative_clustering(M_c, M_r, T,
                              data_inverse_permutation_indices,
                              num_clusters, num_views):
    # NOTE: this function only works because State.p_State doesn't use
    #       column_component_suffstats
    num_rows = len(T)
    num_cols = len(T[0])
    X_D_helper = numpy.repeat(range(num_clusters), (num_rows / num_clusters))
    gen_X_D = [
        X_D_helper[numpy.argsort(data_inverse_permutation_index)]
        for data_inverse_permutation_index in data_inverse_permutation_indices
        ]
    gen_X_L_assignments = numpy.repeat(range(num_views), (num_cols / num_views))
    # initialize to generate an X_L to manipulate
    local_engine = LE.LocalEngine()
    M_c, M_r, bad_X_L, bad_X_D = local_engine.initialize(M_c, M_r, T,
                                                         initialization='apart')
    bad_X_L['column_partition']['assignments'] = gen_X_L_assignments
    # manually constrcut state in in generative configuration
    state = State.p_State(M_c, T, bad_X_L, gen_X_D)
    gen_X_L = state.get_X_L()
    gen_X_D = state.get_X_D()
    # run inference on hyperparameters to leave them in a reasonable state
    kernel_list = (
        'row_partition_hyperparameters',
        'column_hyperparameters',
        'column_partition_hyperparameter',
        )
    gen_X_L, gen_X_D = local_engine.analyze(M_c, T, gen_X_L, gen_X_D, n_steps=1,
                                            kernel_list=kernel_list)
    #
    return gen_X_L, gen_X_D

def generate_clean_state(gen_seed, num_clusters,
                         num_cols, num_rows, num_splits,
                         max_mean=10, max_std=1,
                         plot=False):
    # generate the data
    T, M_r, M_c, data_inverse_permutation_indices = \
        du.gen_factorial_data_objects(gen_seed, num_clusters,
                                      num_cols, num_rows, num_splits,
                                      max_mean=10, max_std=1,
                                      send_data_inverse_permutation_indices=True)
    # recover generative clustering
    X_L, X_D = get_generative_clustering(M_c, M_r, T,
                                         data_inverse_permutation_indices,
                                         num_clusters, num_splits)
    return T, M_c, M_r, X_L, X_D

def generate_hadoop_dicts(which_kernels, X_L, X_D, args_dict):
    for which_kernel in which_kernels:
        kernel_list = (which_kernel, )
        dict_to_write = dict(X_L=X_L, X_D=X_D)
        dict_to_write.update(time_analyze_args_dict)
        # must write kernel_list after update
        dict_to_write['kernel_list'] = kernel_list
        yield dict_to_write

if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument('--gen_seed', type=int, default=0)
    parser.add_argument('--num_clusters', type=int, default=20)
    parser.add_argument('--num_rows', type=int, default=1000)
    parser.add_argument('--num_cols', type=int, default=20)
    parser.add_argument('--num_splits', type=int, default=2)
    parser.add_argument('--max_std', type=float, default=.001)
    parser.add_argument('--n_steps', type=int, default=10)
    parser.add_argument('-do_local', action='store_true')
    parser.add_argument('-do_remote', action='store_true')
    #
    args = parser.parse_args()
    gen_seed = args.gen_seed
    num_clusters = args.num_clusters
    num_cols = args.num_cols
    num_rows = args.num_rows
    num_splits = args.num_splits
    max_std = args.max_std
    n_steps = args.n_steps
    do_local = args.do_local
    do_remote = args.do_remote

    # generate data
    T, M_c, M_r, X_L, X_D = generate_clean_state(gen_seed,
                                                 num_clusters,
                                                 num_cols, num_rows,
                                                 num_splits,
                                                 max_mean=10, max_std=1)

    # some hadoop processing related settings
    table_data_filename = 'table_data.pkl.gz'
    input_filename = 'hadoop_input'
    script_filename = 'hadoop_line_processor.py'
    output_filename = 'hadoop_output'
    SEED = gen_seed

    # prep settings dictionary
    time_analyze_args_dict = xu.default_analyze_args_dict
    time_analyze_args_dict['SEED'] = SEED
    time_analyze_args_dict['command'] = 'time_analyze'
    time_analyze_args_dict['n_steps'] = n_steps

    # write table_data and hadoop input
    table_data = dict(M_c=M_c, M_r=M_r, T=T)
    xu.pickle_table_data(table_data, table_data_filename)
    # one kernel per line
    all_kernels = State.transition_name_to_method_name_and_args.keys()
    with open(input_filename, 'w') as out_fh:
        dict_generator = generate_hadoop_dicts(all_kernels, X_L, X_D, time_analyze_args_dict)
        for dict_to_write in dict_generator:
            xu.write_hadoop_line(out_fh, key=dict_to_write['SEED'], dict_to_write=dict_to_write)

    # actually run
    if do_local:
        xu.run_script_local(input_filename, script_filename, output_filename)
    elif do_remote:
        output_path = HE.output_path
        hadoop_engine = HE.HadoopEngine()
        was_successful = HE.send_hadoop_command(hadoop_engine,
                                                table_data_filename,
                                                input_filename,
                                                output_path, n_tasks=2)
        if was_successful:
            hadoop_output = HE.read_hadoop_output(output_path)
            hadoop_output_filename = HE.get_hadoop_output_filename(output_path)
            os.system('cp %s %s' % (hadoop_output_filename, output_filename))
        else:
            print 'remote hadoop job NOT successful'
    else:
        hadoop_engine = HE.HadoopEngine()
        # print what the command would be
        n_tasks = len(all_kernels)
        print HE.create_hadoop_cmd_str(hadoop_engine, n_tasks=n_tasks)
<commit_msg>remove max_std command line arg, not actually used<commit_after>import os
import argparse
#
import numpy
#
import tabular_predDB.python_utils.data_utils as du
import tabular_predDB.python_utils.general_utils as gu
import tabular_predDB.python_utils.xnet_utils as xu
import tabular_predDB.LocalEngine as LE
import tabular_predDB.HadoopEngine as HE
import tabular_predDB.cython_code.State as State

def get_generative_clustering(M_c, M_r, T,
                              data_inverse_permutation_indices,
                              num_clusters, num_views):
    # NOTE: this function only works because State.p_State doesn't use
    #       column_component_suffstats
    num_rows = len(T)
    num_cols = len(T[0])
    X_D_helper = numpy.repeat(range(num_clusters), (num_rows / num_clusters))
    gen_X_D = [
        X_D_helper[numpy.argsort(data_inverse_permutation_index)]
        for data_inverse_permutation_index in data_inverse_permutation_indices
        ]
    gen_X_L_assignments = numpy.repeat(range(num_views), (num_cols / num_views))
    # initialize to generate an X_L to manipulate
    local_engine = LE.LocalEngine()
    M_c, M_r, bad_X_L, bad_X_D = local_engine.initialize(M_c, M_r, T,
                                                         initialization='apart')
    bad_X_L['column_partition']['assignments'] = gen_X_L_assignments
    # manually constrcut state in in generative configuration
    state = State.p_State(M_c, T, bad_X_L, gen_X_D)
    gen_X_L = state.get_X_L()
    gen_X_D = state.get_X_D()
    # run inference on hyperparameters to leave them in a reasonable state
    kernel_list = (
        'row_partition_hyperparameters',
        'column_hyperparameters',
        'column_partition_hyperparameter',
        )
    gen_X_L, gen_X_D = local_engine.analyze(M_c, T, gen_X_L, gen_X_D, n_steps=1,
                                            kernel_list=kernel_list)
    #
    return gen_X_L, gen_X_D

def generate_clean_state(gen_seed, num_clusters,
                         num_cols, num_rows, num_splits,
                         max_mean=10, max_std=1,
                         plot=False):
    # generate the data
    T, M_r, M_c, data_inverse_permutation_indices = \
        du.gen_factorial_data_objects(gen_seed, num_clusters,
                                      num_cols, num_rows, num_splits,
                                      max_mean=10, max_std=1,
                                      send_data_inverse_permutation_indices=True)
    # recover generative clustering
    X_L, X_D = get_generative_clustering(M_c, M_r, T,
                                         data_inverse_permutation_indices,
                                         num_clusters, num_splits)
    return T, M_c, M_r, X_L, X_D

def generate_hadoop_dicts(which_kernels, X_L, X_D, args_dict):
    for which_kernel in which_kernels:
        kernel_list = (which_kernel, )
        dict_to_write = dict(X_L=X_L, X_D=X_D)
        dict_to_write.update(time_analyze_args_dict)
        # must write kernel_list after update
        dict_to_write['kernel_list'] = kernel_list
        yield dict_to_write

if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument('--gen_seed', type=int, default=0)
    parser.add_argument('--num_clusters', type=int, default=20)
    parser.add_argument('--num_rows', type=int, default=1000)
    parser.add_argument('--num_cols', type=int, default=20)
    parser.add_argument('--num_splits', type=int, default=2)
    parser.add_argument('--n_steps', type=int, default=10)
    parser.add_argument('-do_local', action='store_true')
    parser.add_argument('-do_remote', action='store_true')
    #
    args = parser.parse_args()
    gen_seed = args.gen_seed
    num_clusters = args.num_clusters
    num_cols = args.num_cols
    num_rows = args.num_rows
    num_splits = args.num_splits
    n_steps = args.n_steps
    do_local = args.do_local
    do_remote = args.do_remote

    # generate data
    T, M_c, M_r, X_L, X_D = generate_clean_state(gen_seed,
                                                 num_clusters,
                                                 num_cols, num_rows,
                                                 num_splits,
                                                 max_mean=10, max_std=1)

    # some hadoop processing related settings
    table_data_filename = 'table_data.pkl.gz'
    input_filename = 'hadoop_input'
    script_filename = 'hadoop_line_processor.py'
    output_filename = 'hadoop_output'
    SEED = gen_seed

    # prep settings dictionary
    time_analyze_args_dict = xu.default_analyze_args_dict
    time_analyze_args_dict['SEED'] = SEED
    time_analyze_args_dict['command'] = 'time_analyze'
    time_analyze_args_dict['n_steps'] = n_steps

    # write table_data and hadoop input
    table_data = dict(M_c=M_c, M_r=M_r, T=T)
    xu.pickle_table_data(table_data, table_data_filename)
    # one kernel per line
    all_kernels = State.transition_name_to_method_name_and_args.keys()
    with open(input_filename, 'w') as out_fh:
        dict_generator = generate_hadoop_dicts(all_kernels, X_L, X_D, time_analyze_args_dict)
        for dict_to_write in dict_generator:
            xu.write_hadoop_line(out_fh, key=dict_to_write['SEED'], dict_to_write=dict_to_write)

    # actually run
    if do_local:
        xu.run_script_local(input_filename, script_filename, output_filename)
    elif do_remote:
        output_path = HE.output_path
        hadoop_engine = HE.HadoopEngine()
        was_successful = HE.send_hadoop_command(hadoop_engine,
                                                table_data_filename,
                                                input_filename,
                                                output_path, n_tasks=2)
        if was_successful:
            hadoop_output = HE.read_hadoop_output(output_path)
            hadoop_output_filename = HE.get_hadoop_output_filename(output_path)
            os.system('cp %s %s' % (hadoop_output_filename, output_filename))
        else:
            print 'remote hadoop job NOT successful'
    else:
        hadoop_engine = HE.HadoopEngine()
        # print what the command would be
        n_tasks = len(all_kernels)
        print HE.create_hadoop_cmd_str(hadoop_engine, n_tasks=n_tasks)
<|endoftext|>


<commit_before><commit_msg>Remove the gmock unittest example from the build as it causes link errors in XP Modules.<commit_after><|endoftext|>


<commit_before>from setuptools import setup, find_packages

setup(
    name='relengapi',
    version='0.0',
    description='The code behind https://api.pub.build.mozilla.org',
    author='Dustin J. Mitchell',
    author_email='dustin@mozilla.com',
    url='https://api.pub.build.mozilla.org',
    install_requires=[
        "Flask",
        "Flask-OAuthlib",
    ],
    tests_require=["nose", "mock"],
    packages=find_packages(),
    include_package_data=True,
    test_suite='nose.collector',
    namespace_packages=['relengapi.blueprints'],
    entry_points={
        "relengapi_blueprints": [
            'base = relengapi.blueprints.base:bp',
            'oauth = relengapi.blueprints.oauth:bp',
        ],
        "console_scripts": [
            'relengapi = relengapi.subcommands:main',
        ],
    },
)
<commit_msg>add relengapi as a namespace package<commit_after>from setuptools import setup, find_packages

setup(
    name='relengapi',
    version='0.0',
    description='The code behind https://api.pub.build.mozilla.org',
    author='Dustin J. Mitchell',
    author_email='dustin@mozilla.com',
    url='https://api.pub.build.mozilla.org',
    install_requires=[
        "Flask",
        "Flask-OAuthlib",
    ],
    tests_require=["nose", "mock"],
    packages=find_packages(),
    include_package_data=True,
    test_suite='nose.collector',
    namespace_packages=['relengapi', 'relengapi.blueprints'],
    entry_points={
        "relengapi_blueprints": [
            'base = relengapi.blueprints.base:bp',
            'oauth = relengapi.blueprints.oauth:bp',
        ],
        "console_scripts": [
            'relengapi = relengapi.subcommands:main',
        ],
    },
)
<|endoftext|>


<commit_before># Copyright 2016 The TensorFlow Authors. All Rights Reserved.
#
# 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.
# ==============================================================================
"""Tests for numpy_io."""

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import numpy as np

from tensorflow.python.estimator.inputs import numpy_io
from tensorflow.python.framework import errors
from tensorflow.python.platform import test
from tensorflow.python.training import coordinator
from tensorflow.python.training import queue_runner_impl

class NumpyIoTest(test.TestCase):

  def testNumpyInputFn(self):
    a = np.arange(4) * 1.0
    b = np.arange(32, 36)
    x = {'a': a, 'b': b}
    y = np.arange(-32, -28)

    with self.test_session() as session:
      input_fn = numpy_io.numpy_input_fn(
          x, y, batch_size=2, shuffle=False, num_epochs=1)
      features, target = input_fn()

      coord = coordinator.Coordinator()
      threads = queue_runner_impl.start_queue_runners(session, coord=coord)

      res = session.run([features, target])
      self.assertAllEqual(res[0]['a'], [0, 1])
      self.assertAllEqual(res[0]['b'], [32, 33])
      self.assertAllEqual(res[1], [-32, -31])

      session.run([features, target])
      with self.assertRaises(errors.OutOfRangeError):
        session.run([features, target])

      coord.request_stop()
      coord.join(threads)

  def testNumpyInputFnWithVeryLargeBatchSizeAndMultipleEpochs(self):
    a = np.arange(2) * 1.0
    b = np.arange(32, 34)
    x = {'a': a, 'b': b}
    y = np.arange(-32, -30)

    with self.test_session() as session:
      input_fn = numpy_io.numpy_input_fn(
          x, y, batch_size=128, shuffle=False, num_epochs=2)
      features, target = input_fn()

      coord = coordinator.Coordinator()
      threads = queue_runner_impl.start_queue_runners(session, coord=coord)

      res = session.run([features, target])
      self.assertAllEqual(res[0]['a'], [0, 1, 0, 1])
      self.assertAllEqual(res[0]['b'], [32, 33, 32, 33])
      self.assertAllEqual(res[1], [-32, -31, -32, -31])

      with self.assertRaises(errors.OutOfRangeError):
        session.run([features, target])

      coord.request_stop()
      coord.join(threads)

  def testNumpyInputFnWithZeroEpochs(self):
    a = np.arange(4) * 1.0
    b = np.arange(32, 36)
    x = {'a': a, 'b': b}
    y = np.arange(-32, -28)

    with self.test_session() as session:
      input_fn = numpy_io.numpy_input_fn(
          x, y, batch_size=2, shuffle=False, num_epochs=0)
      features, target = input_fn()

      coord = coordinator.Coordinator()
      threads = queue_runner_impl.start_queue_runners(session, coord=coord)

      with self.assertRaises(errors.OutOfRangeError):
        session.run([features, target])

      coord.request_stop()
      coord.join(threads)

  def testNumpyInputFnWithBatchSizeNotDividedByDataSize(self):
    batch_size = 2
    a = np.arange(5) * 1.0
    b = np.arange(32, 37)
    x = {'a': a, 'b': b}
    y = np.arange(-32, -27)

    with self.test_session() as session:
      input_fn = numpy_io.numpy_input_fn(
          x, y, batch_size=batch_size, shuffle=False, num_epochs=1)
      features, target = input_fn()

      coord = coordinator.Coordinator()
      threads = queue_runner_impl.start_queue_runners(session, coord=coord)

      res = session.run([features, target])
      self.assertAllEqual(res[0]['a'], [0, 1])
      self.assertAllEqual(res[0]['b'], [32, 33])
      self.assertAllEqual(res[1], [-32, -31])

      res = session.run([features, target])
      self.assertAllEqual(res[0]['a'], [2, 3])
      self.assertAllEqual(res[0]['b'], [34, 35])
      self.assertAllEqual(res[1], [-30, -29])

      res = session.run([features, target])
      self.assertAllEqual(res[0]['a'], [4])
      self.assertAllEqual(res[0]['b'], [36])
      self.assertAllEqual(res[1], [-28])

      with self.assertRaises(errors.OutOfRangeError):
        session.run([features, target])

      coord.request_stop()
      coord.join(threads)

  def testNumpyInputFnWithBatchSizeNotDividedByDataSizeAndMultipleEpochs(self):
    batch_size = 2
    a = np.arange(3) * 1.0
    b = np.arange(32, 35)
    x = {'a': a, 'b': b}
    y = np.arange(-32, -29)

    with self.test_session() as session:
      input_fn = numpy_io.numpy_input_fn(
          x, y, batch_size=batch_size, shuffle=False, num_epochs=3)
      features, target = input_fn()

      coord = coordinator.Coordinator()
      threads = queue_runner_impl.start_queue_runners(session, coord=coord)

      res = session.run([features, target])
      self.assertAllEqual(res[0]['a'], [0, 1])
      self.assertAllEqual(res[0]['b'], [32, 33])
      self.assertAllEqual(res[1], [-32, -31])

      res = session.run([features, target])
      self.assertAllEqual(res[0]['a'], [2, 0])
      self.assertAllEqual(res[0]['b'], [34, 32])
      self.assertAllEqual(res[1], [-30, -32])

      res = session.run([features, target])
      self.assertAllEqual(res[0]['a'], [1, 2])
      self.assertAllEqual(res[0]['b'], [33, 34])
      self.assertAllEqual(res[1], [-31, -30])

      res = session.run([features, target])
      self.assertAllEqual(res[0]['a'], [0, 1])
      self.assertAllEqual(res[0]['b'], [32, 33])
      self.assertAllEqual(res[1], [-32, -31])

      res = session.run([features, target])
      self.assertAllEqual(res[0]['a'], [2])
      self.assertAllEqual(res[0]['b'], [34])
      self.assertAllEqual(res[1], [-30])

      with self.assertRaises(errors.OutOfRangeError):
        session.run([features, target])

      coord.request_stop()
      coord.join(threads)

  def testNumpyInputFnWithBatchSizeLargerThanDataSize(self):
    batch_size = 10
    a = np.arange(4) * 1.0
    b = np.arange(32, 36)
    x = {'a': a, 'b': b}
    y = np.arange(-32, -28)

    with self.test_session() as session:
      input_fn = numpy_io.numpy_input_fn(
          x, y, batch_size=batch_size, shuffle=False, num_epochs=1)
      features, target = input_fn()

      coord = coordinator.Coordinator()
      threads = queue_runner_impl.start_queue_runners(session, coord=coord)

      res = session.run([features, target])
      self.assertAllEqual(res[0]['a'], [0, 1, 2, 3])
      self.assertAllEqual(res[0]['b'], [32, 33, 34, 35])
      self.assertAllEqual(res[1], [-32, -31, -30, -29])

      with self.assertRaises(errors.OutOfRangeError):
        session.run([features, target])

      coord.request_stop()
      coord.join(threads)

  def testNumpyInputFnWithDifferentDimensionsOfFeatures(self):
    a = np.array([[1, 2], [3, 4]])
    b = np.array([5, 6])
    x = {'a': a, 'b': b}
    y = np.arange(-32, -30)

    with self.test_session() as session:
      input_fn = numpy_io.numpy_input_fn(
          x, y, batch_size=2, shuffle=False, num_epochs=1)
      features, target = input_fn()

      coord = coordinator.Coordinator()
      threads = queue_runner_impl.start_queue_runners(session, coord=coord)

      res = session.run([features, target])
      self.assertAllEqual(res[0]['a'], [[1, 2], [3, 4]])
      self.assertAllEqual(res[0]['b'], [5, 6])
      self.assertAllEqual(res[1], [-32, -31])

      coord.request_stop()
      coord.join(threads)

  def testNumpyInputFnWithXAsNonDict(self):
    x = np.arange(32, 36)
    y = np.arange(4)
    with self.test_session():
      with self.assertRaisesRegexp(TypeError, 'x must be dict'):
        failing_input_fn = numpy_io.numpy_input_fn(
            x, y, batch_size=2, shuffle=False, num_epochs=1)
        failing_input_fn()

  def testNumpyInputFnWithNonBoolShuffle(self):
    x = np.arange(32, 36)
    y = np.arange(4)
    with self.test_session():
      with self.assertRaisesRegexp(TypeError,
                                   'shuffle must be explicitly set as boolean'):
        # Default shuffle is None.
        numpy_io.numpy_input_fn(x, y)

  def testNumpyInputFnWithTargetKeyAlreadyInX(self):
    array = np.arange(32, 36)
    x = {'__target_key__': array}
    y = np.arange(4)

    with self.test_session():
      input_fn = numpy_io.numpy_input_fn(
          x, y, batch_size=2, shuffle=False, num_epochs=1)
      input_fn()
      self.assertAllEqual(x['__target_key__'], array)
      # The input x should not be mutated.
      self.assertItemsEqual(x.keys(), ['__target_key__'])

  def testNumpyInputFnWithMismatchLengthOfInputs(self):
    a = np.arange(4) * 1.0
    b = np.arange(32, 36)
    x = {'a': a, 'b': b}
    x_mismatch_length = {'a': np.arange(1), 'b': b}
    y_longer_length = np.arange(10)

    with self.test_session():
      with self.assertRaisesRegexp(
          ValueError, 'Length of tensors in x and y is mismatched.'):
        failing_input_fn = numpy_io.numpy_input_fn(
            x, y_longer_length, batch_size=2, shuffle=False, num_epochs=1)
        failing_input_fn()

      with self.assertRaisesRegexp(
          ValueError, 'Length of tensors in x and y is mismatched.'):
        failing_input_fn = numpy_io.numpy_input_fn(
            x=x_mismatch_length,
            y=None,
            batch_size=2,
            shuffle=False,
            num_epochs=1)
        failing_input_fn()

  def testNumpyInputFnWhenLabelIsDictionary(self):
    a = np.arange(4) * 1.0
    b = np.arange(32, 36)
    x = {'a': a, 'b': b}
    y = {'y1': np.arange(-32, -28), 'y2': np.arange(32, 28, -1)}

    with self.test_session() as session:
      input_fn = numpy_io.numpy_input_fn(
        x, y, batch_size=2, shuffle=False, num_epochs=1)
      features, target = input_fn()

      coord = coordinator.Coordinator()
      threads = queue_runner_impl.start_queue_runners(session, coord=coord)

      res = session.run([features, target])
      self.assertAllEqual(res[0]['a'], [0, 1])
      self.assertAllEqual(res[0]['b'], [32, 33])
      self.assertAllEqual(res[1]['y1'], [-32, -31])
      self.assertAllEqual(res[1]['y2'], [32, 31])

      session.run([features, target])
      with self.assertRaises(errors.OutOfRangeError):
        session.run([features, target])

      coord.request_stop()
      coord.join(threads)

if __name__ == '__main__':
  test.main()
<commit_msg>TST: duplicate test case<commit_after># Copyright 2016 The TensorFlow Authors. All Rights Reserved.
#
# 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.
# ==============================================================================
"""Tests for numpy_io."""

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import numpy as np

from tensorflow.python.estimator.inputs import numpy_io
from tensorflow.python.framework import errors
from tensorflow.python.platform import test
from tensorflow.python.training import coordinator
from tensorflow.python.training import queue_runner_impl

class NumpyIoTest(test.TestCase):

  def testNumpyInputFn(self):
    a = np.arange(4) * 1.0
    b = np.arange(32, 36)
    x = {'a': a, 'b': b}
    y = np.arange(-32, -28)

    with self.test_session() as session:
      input_fn = numpy_io.numpy_input_fn(
          x, y, batch_size=2, shuffle=False, num_epochs=1)
      features, target = input_fn()

      coord = coordinator.Coordinator()
      threads = queue_runner_impl.start_queue_runners(session, coord=coord)

      res = session.run([features, target])
      self.assertAllEqual(res[0]['a'], [0, 1])
      self.assertAllEqual(res[0]['b'], [32, 33])
      self.assertAllEqual(res[1], [-32, -31])

      session.run([features, target])
      with self.assertRaises(errors.OutOfRangeError):
        session.run([features, target])

      coord.request_stop()
      coord.join(threads)

  def testNumpyInputFnWithVeryLargeBatchSizeAndMultipleEpochs(self):
    a = np.arange(2) * 1.0
    b = np.arange(32, 34)
    x = {'a': a, 'b': b}
    y = np.arange(-32, -30)

    with self.test_session() as session:
      input_fn = numpy_io.numpy_input_fn(
          x, y, batch_size=128, shuffle=False, num_epochs=2)
      features, target = input_fn()

      coord = coordinator.Coordinator()
      threads = queue_runner_impl.start_queue_runners(session, coord=coord)

      res = session.run([features, target])
      self.assertAllEqual(res[0]['a'], [0, 1, 0, 1])
      self.assertAllEqual(res[0]['b'], [32, 33, 32, 33])
      self.assertAllEqual(res[1], [-32, -31, -32, -31])

      with self.assertRaises(errors.OutOfRangeError):
        session.run([features, target])

      coord.request_stop()
      coord.join(threads)

  def testNumpyInputFnWithZeroEpochs(self):
    a = np.arange(4) * 1.0
    b = np.arange(32, 36)
    x = {'a': a, 'b': b}
    y = np.arange(-32, -28)

    with self.test_session() as session:
      input_fn = numpy_io.numpy_input_fn(
          x, y, batch_size=2, shuffle=False, num_epochs=0)
      features, target = input_fn()

      coord = coordinator.Coordinator()
      threads = queue_runner_impl.start_queue_runners(session, coord=coord)

      with self.assertRaises(errors.OutOfRangeError):
        session.run([features, target])

      coord.request_stop()
      coord.join(threads)

  def testNumpyInputFnWithBatchSizeNotDividedByDataSize(self):
    batch_size = 2
    a = np.arange(5) * 1.0
    b = np.arange(32, 37)
    x = {'a': a, 'b': b}
    y = np.arange(-32, -27)

    with self.test_session() as session:
      input_fn = numpy_io.numpy_input_fn(
          x, y, batch_size=batch_size, shuffle=False, num_epochs=1)
      features, target = input_fn()

      coord = coordinator.Coordinator()
      threads = queue_runner_impl.start_queue_runners(session, coord=coord)

      res = session.run([features, target])
      self.assertAllEqual(res[0]['a'], [0, 1])
      self.assertAllEqual(res[0]['b'], [32, 33])
      self.assertAllEqual(res[1], [-32, -31])

      res = session.run([features, target])
      self.assertAllEqual(res[0]['a'], [2, 3])
      self.assertAllEqual(res[0]['b'], [34, 35])
      self.assertAllEqual(res[1], [-30, -29])

      res = session.run([features, target])
      self.assertAllEqual(res[0]['a'], [4])
      self.assertAllEqual(res[0]['b'], [36])
      self.assertAllEqual(res[1], [-28])

      with self.assertRaises(errors.OutOfRangeError):
        session.run([features, target])

      coord.request_stop()
      coord.join(threads)

  def testNumpyInputFnWithBatchSizeNotDividedByDataSizeAndMultipleEpochs(self):
    batch_size = 2
    a = np.arange(3) * 1.0
    b = np.arange(32, 35)
    x = {'a': a, 'b': b}
    y = np.arange(-32, -29)

    with self.test_session() as session:
      input_fn = numpy_io.numpy_input_fn(
          x, y, batch_size=batch_size, shuffle=False, num_epochs=3)
      features, target = input_fn()

      coord = coordinator.Coordinator()
      threads = queue_runner_impl.start_queue_runners(session, coord=coord)

      res = session.run([features, target])
      self.assertAllEqual(res[0]['a'], [0, 1])
      self.assertAllEqual(res[0]['b'], [32, 33])
      self.assertAllEqual(res[1], [-32, -31])

      res = session.run([features, target])
      self.assertAllEqual(res[0]['a'], [2, 0])
      self.assertAllEqual(res[0]['b'], [34, 32])
      self.assertAllEqual(res[1], [-30, -32])

      res = session.run([features, target])
      self.assertAllEqual(res[0]['a'], [1, 2])
      self.assertAllEqual(res[0]['b'], [33, 34])
      self.assertAllEqual(res[1], [-31, -30])

      res = session.run([features, target])
      self.assertAllEqual(res[0]['a'], [0, 1])
      self.assertAllEqual(res[0]['b'], [32, 33])
      self.assertAllEqual(res[1], [-32, -31])

      res = session.run([features, target])
      self.assertAllEqual(res[0]['a'], [2])
      self.assertAllEqual(res[0]['b'], [34])
      self.assertAllEqual(res[1], [-30])

      with self.assertRaises(errors.OutOfRangeError):
        session.run([features, target])

      coord.request_stop()
      coord.join(threads)

  def testNumpyInputFnWithBatchSizeLargerThanDataSize(self):
    batch_size = 10
    a = np.arange(4) * 1.0
    b = np.arange(32, 36)
    x = {'a': a, 'b': b}
    y = np.arange(-32, -28)

    with self.test_session() as session:
      input_fn = numpy_io.numpy_input_fn(
          x, y, batch_size=batch_size, shuffle=False, num_epochs=1)
      features, target = input_fn()

      coord = coordinator.Coordinator()
      threads = queue_runner_impl.start_queue_runners(session, coord=coord)

      res = session.run([features, target])
      self.assertAllEqual(res[0]['a'], [0, 1, 2, 3])
      self.assertAllEqual(res[0]['b'], [32, 33, 34, 35])
      self.assertAllEqual(res[1], [-32, -31, -30, -29])

      with self.assertRaises(errors.OutOfRangeError):
        session.run([features, target])

      coord.request_stop()
      coord.join(threads)

  def testNumpyInputFnWithDifferentDimensionsOfFeatures(self):
    a = np.array([[1, 2], [3, 4]])
    b = np.array([5, 6])
    x = {'a': a, 'b': b}
    y = np.arange(-32, -30)

    with self.test_session() as session:
      input_fn = numpy_io.numpy_input_fn(
          x, y, batch_size=2, shuffle=False, num_epochs=1)
      features, target = input_fn()

      coord = coordinator.Coordinator()
      threads = queue_runner_impl.start_queue_runners(session, coord=coord)

      res = session.run([features, target])
      self.assertAllEqual(res[0]['a'], [[1, 2], [3, 4]])
      self.assertAllEqual(res[0]['b'], [5, 6])
      self.assertAllEqual(res[1], [-32, -31])

      coord.request_stop()
      coord.join(threads)

  def testNumpyInputFnWithXAsNonDict(self):
    x = np.arange(32, 36)
    y = np.arange(4)
    with self.test_session():
      with self.assertRaisesRegexp(TypeError, 'x must be dict'):
        failing_input_fn = numpy_io.numpy_input_fn(
            x, y, batch_size=2, shuffle=False, num_epochs=1)
        failing_input_fn()

  def testNumpyInputFnWithNonBoolShuffle(self):
    x = np.arange(32, 36)
    y = np.arange(4)
    with self.test_session():
      with self.assertRaisesRegexp(TypeError,
                                   'shuffle must be explicitly set as boolean'):
        # Default shuffle is None.
        numpy_io.numpy_input_fn(x, y)

  def testNumpyInputFnWithTargetKeyAlreadyInX(self):
    array = np.arange(32, 36)
    x = {'__target_key__': array}
    y = np.arange(4)

    with self.test_session():
      input_fn = numpy_io.numpy_input_fn(
          x, y, batch_size=2, shuffle=False, num_epochs=1)
      input_fn()
      self.assertAllEqual(x['__target_key__'], array)
      # The input x should not be mutated.
      self.assertItemsEqual(x.keys(), ['__target_key__'])

  def testNumpyInputFnWithMismatchLengthOfInputs(self):
    a = np.arange(4) * 1.0
    b = np.arange(32, 36)
    x = {'a': a, 'b': b}
    x_mismatch_length = {'a': np.arange(1), 'b': b}
    y_longer_length = np.arange(10)

    with self.test_session():
      with self.assertRaisesRegexp(
          ValueError, 'Length of tensors in x and y is mismatched.'):
        failing_input_fn = numpy_io.numpy_input_fn(
            x, y_longer_length, batch_size=2, shuffle=False, num_epochs=1)
        failing_input_fn()

      with self.assertRaisesRegexp(
          ValueError, 'Length of tensors in x and y is mismatched.'):
        failing_input_fn = numpy_io.numpy_input_fn(
            x=x_mismatch_length,
            y=None,
            batch_size=2,
            shuffle=False,
            num_epochs=1)
        failing_input_fn()

  def testNumpyInputFnWhenLabelIsDictionary(self):
    a = np.arange(4) * 1.0
    b = np.arange(32, 36)
    x = {'a': a, 'b': b}
    y = {'y1': np.arange(-32, -28), 'y2': np.arange(32, 28, -1)}

    with self.test_session() as session:
      input_fn = numpy_io.numpy_input_fn(
        x, y, batch_size=2, shuffle=False, num_epochs=1)
      features, target = input_fn()

      coord = coordinator.Coordinator()
      threads = queue_runner_impl.start_queue_runners(session, coord=coord)

      res = session.run([features, target])
      self.assertAllEqual(res[0]['a'], [0, 1])
      self.assertAllEqual(res[0]['b'], [32, 33])
      self.assertAllEqual(res[1]['y1'], [-32, -31])
      self.assertAllEqual(res[1]['y2'], [32, 31])

      session.run([features, target])
      with self.assertRaises(errors.OutOfRangeError):
        session.run([features, target])

      coord.request_stop()
      coord.join(threads)

  def testNumpyInputFnDuplicateKeysInXandY(self):
    a = np.arange(4) * 1.0
    b = np.arange(32, 36)
    x = {'a': a, 'b': b}
    y = {'y1': np.arange(-32, -28),
         'a': a,
         'y2': np.arange(32, 28, -1),
         'b': b}

    with self.test_session():
      with self.assertRaisesRegexp(
              ValueError, '2 duplicate keys are found in both x and y'):
        failing_input_fn = numpy_io.numpy_input_fn(
          x, y, batch_size=2, shuffle=False, num_epochs=1)
        failing_input_fn()

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


<commit_before># Copyright 2017 The TensorFlow Authors. All Rights Reserved.
#
# 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.
# ================================
"""Imports a protobuf model as a graph in Tensorboard."""

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import argparse
import sys

from tensorflow.core.framework import graph_pb2
from tensorflow.python.client import session
from tensorflow.python.framework import importer
from tensorflow.python.framework import ops
from tensorflow.python.platform import app
from tensorflow.python.platform import gfile
from tensorflow.python.summary import summary

def import_to_tensorboard(model_dir, log_dir):
  """View an imported protobuf model (`.pb` file) as a graph in Tensorboard.

  Args:
    model_dir: The location of the protobuf (`pb`) model to visualize
    log_dir: The location for the Tensorboard log to begin visualization from.

  Usage:
    Call this function with your model location and desired log directory.
    Launch Tensorboard by pointing it to the log directory.
    View your imported `.pb` model as a graph.
  """
  with session.Session(graph=ops.Graph()) as sess:
    with gfile.FastGFile(model_dir, "rb") as f:
      graph_def = graph_pb2.GraphDef()
      graph_def.ParseFromString(f.read())
      importer.import_graph_def(graph_def)

    pb_visual_writer = summary.FileWriter(log_dir)
    pb_visual_writer.add_graph(sess.graph)
    print("Model Imported. Visualize by running: "
          "tensorboard --logdir={}".format(log_dir))

def main(unused_args):
  import_to_tensorboard(FLAGS.model_dir, FLAGS.log_dir)

if __name__ == "__main__":
  parser = argparse.ArgumentParser()
  parser.register("type", "bool", lambda v: v.lower() == "true")
  parser.add_argument(
      "--model_dir",
      type=str,
      default="",
      required=True,
      help="The location of the protobuf (\'pb\') model to visualize.")
  parser.add_argument(
      "--log_dir",
      type=str,
      default="",
      required=True,
      help="The location for the Tensorboard log to begin visualization from.")
  FLAGS, unparsed = parser.parse_known_args()
  app.run(main=main, argv=[sys.argv[0]] + unparsed)
<commit_msg>Import tensorrt if available to import_pb_to_tensorboard.py for displaying TensorRT ops<commit_after># Copyright 2017 The TensorFlow Authors. All Rights Reserved.
#
# 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.
# ================================
"""Imports a protobuf model as a graph in Tensorboard."""

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import argparse
import sys

from tensorflow.core.framework import graph_pb2
from tensorflow.python.client import session
from tensorflow.python.framework import importer
from tensorflow.python.framework import ops
from tensorflow.python.platform import app
from tensorflow.python.platform import gfile
from tensorflow.python.summary import summary

# Try importing TensorRT ops if available
# pylint: disable=unused-import,trailing-whitespace
try:
  import tensorflow.contrib.tensorrt as trt
except ImportError:
  pass
# pylint: enable=unused-import,trailing-whitespace

def import_to_tensorboard(model_dir, log_dir):
  """View an imported protobuf model (`.pb` file) as a graph in Tensorboard.

  Args:
    model_dir: The location of the protobuf (`pb`) model to visualize
    log_dir: The location for the Tensorboard log to begin visualization from.

  Usage:
    Call this function with your model location and desired log directory.
    Launch Tensorboard by pointing it to the log directory.
    View your imported `.pb` model as a graph.
  """
  with session.Session(graph=ops.Graph()) as sess:
    with gfile.FastGFile(model_dir, "rb") as f:
      graph_def = graph_pb2.GraphDef()
      graph_def.ParseFromString(f.read())
      importer.import_graph_def(graph_def)

    pb_visual_writer = summary.FileWriter(log_dir)
    pb_visual_writer.add_graph(sess.graph)
    print("Model Imported. Visualize by running: "
          "tensorboard --logdir={}".format(log_dir))

def main(unused_args):
  import_to_tensorboard(FLAGS.model_dir, FLAGS.log_dir)

if __name__ == "__main__":
  parser = argparse.ArgumentParser()
  parser.register("type", "bool", lambda v: v.lower() == "true")
  parser.add_argument(
      "--model_dir",
      type=str,
      default="",
      required=True,
      help="The location of the protobuf (\'pb\') model to visualize.")
  parser.add_argument(
      "--log_dir",
      type=str,
      default="",
      required=True,
      help="The location for the Tensorboard log to begin visualization from.")
  FLAGS, unparsed = parser.parse_known_args()
  app.run(main=main, argv=[sys.argv[0]] + unparsed)
<|endoftext|>


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

#THIS IS PROJECT ONE, WE'LL FIRST TRY TO MAKE A BATTLESHIP GAME,
#PLEASE MAKE USE OF COMMENT WHEN PULLING REQUESTS

# Importing pygame modules
import random, sys, pygame
from pygame.locals import *
# and other modules needed

# ----------------------------------------------
# Set variables, like screen width and height
# globals
FPS = 30
REVEALSPEED = 8
WINDOWWIDTH = 800
WINDOWHEIGHT = 600
TILESIZE = 40
BUTTONHEIGHT = 20
BUTTONWIDTH = 40
TEXT_HEIGHT = 25
TEXT_LEFT_POSN = 10
BOARDWIDTH = 12
BOARDHEIGHT = 12
DISPLAYWIDTH = 200
XMARGIN = int((WINDOWWIDTH - (BOARDWIDTH * TILESIZE) - (DISPLAYWIDTH + 20)) / 2)
YMARGIN = int((WINDOWHEIGHT - (BOARDHEIGHT * TILESIZE)) / 2)

BLACK   = (  0,   0,   0)
WHITE   = (255, 255, 255)
GREEN   = (  0, 204,   0)
GRAY    = ( 60,  60,  60)
BLUE    = (  0,  50, 255)
YELLOW  = (255, 255,   0)
DARKGRAY =( 40,  40,  40)

BGCOLOR = GRAY
BUTTONCOLOR = GREEN
TEXTCOLOR = WHITE
TILECOLOR = GREEN
BORDERCOLOR = BLUE
TEXTSHADOWCOLOR = BLUE
SHIPCOLOR = YELLOW
HIGHLIGHTCOLOR = BLUE

def main():
    global DISPLAYSURF, FPSCLOCK, BASICFONT, HELP_SURF, HELP_RECT, NEW_SURF, \
           NEW_RECT, BIGFONT
    pygame.init()
    FPSCLOCK = pygame.time.Clock()
    DISPLAYSURF = pygame.display.set_mode((WINDOWWIDTH, WINDOWHEIGHT))
    BASICFONT = pygame.font.Font('freesansbold.ttf', 20)
    BIGFONT = pygame.font.Font('freesansbold.ttf', 100)

    # create buttons
    HELP_SURF = BASICFONT.render("HELP", True, WHITE, GREEN)
    HELP_RECT = HELP_SURF.get_rect()
    HELP_RECT.topleft = (WINDOWWIDTH - 120, WINDOWHEIGHT - 593)
    NEW_SURF = BASICFONT.render("NEW GAME", True, WHITE, GREEN)
    NEW_RECT = NEW_SURF.get_rect()
    NEW_RECT.topleft = (WINDOWWIDTH - 120, WINDOWHEIGHT - 563)

    pygame.display.set_caption('Battleship')

    while True:
        run_game()
        show_text_screen('Game Over')

def run_game():
    revealed_tiles = generate_default_tiles()
    main_board = generate_default_tiles()
    add_ships_to_board(main_board)
    mousex, mousey = 0, 0

    while True:
        DISPLAYSURF.fill(BGCOLOR)
        DISPLAYSURF.blit(HELP_SURF, HELP_RECT)
        DISPLAYSURF.blit(NEW_SURF, NEW_RECT)
        draw_board(main_board, revealed_tiles)
        mouse_clicked = False

        check_for_quit()
        for event in pygame.event.get():
            if event.type == MOUSEBUTTONUP:
                #print (event.pos[0], event.pos[1])
                if HELP_RECT.collidepoint(event.pos):
                    DISPLAYSURF.fill(BGCOLOR)
                    show_help_screen()
                else:
                    mousex, mousey = event.pos
                    mouse_clicked = True
            elif event.type == MOUSEMOTION:
                mousex, mousey = event.pos

        tilex, tiley = get_tile_at_pixel(mousex, mousey)
        if tilex != None and tiley != None:
            if not revealed_tiles[tilex][tiley]:
                draw_highlight_tile(tilex, tiley)
            if not revealed_tiles[tilex][tiley] and mouse_clicked:
                reveal_tiles_animation(main_board, [(tilex, tiley)])
                revealed_tiles[tilex][tiley] = True

        pygame.display.update()
        FPSCLOCK.tick(FPS)

def generate_default_tiles():
    default_tiles = []
    for i in range(BOARDWIDTH):
        default_tiles.append([False] * BOARDHEIGHT)
    return default_tiles

def reveal_tiles_animation(board, tiles_to_reveal):
    for coverage in range(TILESIZE, (-REVEALSPEED) - 1, -REVEALSPEED):
        draw_tile_covers(board, tiles_to_reveal, coverage)

def draw_tile_covers(board, tiles, coverage):
    for tile in tiles:
        left, top = left_top_coords_tile(tile[0], tile[1])
        if board[tile[0]][tile[1]] == True:
            pygame.draw.rect(DISPLAYSURF, SHIPCOLOR, (left, top, TILESIZE,
                                                      TILESIZE))
        else:
            pygame.draw.rect(DISPLAYSURF, BGCOLOR, (left, top, TILESIZE,
                                                    TILESIZE))
        if coverage > 0:
            pygame.draw.rect(DISPLAYSURF, TILECOLOR, (left, top, coverage,
                                                      TILESIZE))

    pygame.display.update()
    FPSCLOCK.tick(FPS)

def check_for_quit():
    for event in pygame.event.get(QUIT):
        pygame.quit()
        sys.exit()

def draw_board(board, revealed):
    for tilex in range(BOARDWIDTH):
        for tiley in range(BOARDHEIGHT):
            left, top = left_top_coords_tile(tilex, tiley)
            if not revealed[tilex][tiley]:
                pygame.draw.rect(DISPLAYSURF, TILECOLOR, (left, top, TILESIZE,
                                                          TILESIZE))
            else:
                if board[tilex][tiley] == True:
                    pygame.draw.rect(DISPLAYSURF, SHIPCOLOR, (left, top,
                                     TILESIZE, TILESIZE))
                else:
                    pygame.draw.rect(DISPLAYSURF, BGCOLOR, (left, top,
                                     TILESIZE, TILESIZE))

    for x in range(0, BOARDWIDTH * TILESIZE, TILESIZE):
        pygame.draw.line(DISPLAYSURF, DARKGRAY, (x + XMARGIN, YMARGIN), \
                        (x + XMARGIN, WINDOWHEIGHT - YMARGIN))
    for y in range(0, BOARDHEIGHT * TILESIZE, TILESIZE):
        pygame.draw.line(DISPLAYSURF, DARKGRAY, (XMARGIN, y + YMARGIN), \
                (WINDOWWIDTH - (DISPLAYWIDTH + 20 + XMARGIN), y + YMARGIN))

def add_ships_to_board(board):
    # this is a stub for the prototype only. needs to be refactored for next
    # milestone
    new_board = board
    board[1][1] = True
    board[1][2] = True
    board[1][8] = True
    board[2][8] = True
    board[3][8] = True
    board[5][8] = True

def left_top_coords_tile(tilex, tiley):
    left = tilex * TILESIZE + XMARGIN
    top = tiley * TILESIZE + YMARGIN
    return (left, top)

def get_tile_at_pixel(x, y):
    for tilex in range(BOARDWIDTH):
        for tiley in range(BOARDHEIGHT):
            left, top = left_top_coords_tile(tilex, tiley)
            tile_rect = pygame.Rect(left, top, TILESIZE, TILESIZE)
            if tile_rect.collidepoint(x, y):
                return (tilex, tiley)
    return (None, None)

def draw_highlight_tile(tilex, tiley):
    left, top = left_top_coords_tile(tilex, tiley)
    pygame.draw.rect(DISPLAYSURF, HIGHLIGHTCOLOR,
                    (left, top, TILESIZE, TILESIZE), 4)

def show_help_screen():
    # display the help screen until a button is pressed
    line1_surf, line1_rect = make_text_objs('Press a key to exit.', BASICFONT,
                                            TEXTCOLOR)
    line1_rect.topleft = (TEXT_LEFT_POSN, TEXT_HEIGHT)
    DISPLAYSURF.blit(line1_surf, line1_rect)

    line2_surf, line2_rect = make_text_objs('Enter instructions here.',
                                            BASICFONT, TEXTCOLOR)
    line2_rect.topleft = (TEXT_LEFT_POSN, TEXT_HEIGHT * 2)
    DISPLAYSURF.blit(line2_surf, line2_rect)

    line3_surf, line3_rect = make_text_objs('Enter instructions here.',
                                            BASICFONT, TEXTCOLOR)
    line3_rect.topleft = (TEXT_LEFT_POSN, TEXT_HEIGHT * 3)
    DISPLAYSURF.blit(line3_surf, line3_rect)

    while check_for_keypress() == None:
        pygame.display.update()
        FPSCLOCK.tick()

def check_for_keypress():
    # pulling out all KEYDOWN and KEYUP events from queue and returning any
    # KEYUP else return None
    for event in pygame.event.get([KEYDOWN, KEYUP]):
        if event.type == KEYDOWN:
            continue
        return event.key
    return None

def make_text_objs(text, font, color):
    surf = font.render(text, True, color)
    return surf, surf.get_rect()

def show_text_screen(text):
    DISPLAYSURF.fill(BGCOLOR)
    titleSurf, titleRect = make_text_objs(text, BIGFONT, TEXTSHADOWCOLOR)
    titleRect.center = (int(WINDOWWIDTH / 2), int(WINDOWHEIGHT / 2))
    DISPLAYSURF.blit(titleSurf, titleRect)

    titleSurf, titleRect = make_text_objs(text, BIGFONT, TEXTCOLOR)
    titleRect.center = (int(WINDOWWIDTH / 2) - 3, int(WINDOWHEIGHT / 2) - 3)
    DISPLAYSURF.blit(titleSurf, titleRect)

    pressKeySurf, pressKeyRect = make_text_objs('Press a key to play.',
                                                BASICFONT, TEXTCOLOR)
    pressKeyRect.center = (int(WINDOWWIDTH / 2), int(WINDOWHEIGHT / 2) + 100)
    DISPLAYSURF.blit(pressKeySurf, pressKeyRect)

    while check_for_keypress() == None:
        pygame.display.update()
        FPSCLOCK.tick()

if __name__ == "__main__": #This calls the game loop
    main()
<commit_msg>'Shots:' label added and ultra minor changes<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-
#THIS IS PROJECT ONE, WE'LL FIRST TRY TO MAKE A BATTLESHIP GAME,
#PLEASE MAKE USE OF COMMENT WHEN PULLING REQUESTS

# Importing pygame modules
import random, sys, pygame
from pygame.locals import *
# and other modules needed

# Set variables, like screen width and height
# globals
FPS = 30
REVEALSPEED = 8
WINDOWWIDTH = 800
WINDOWHEIGHT = 600
TILESIZE = 40
BUTTONHEIGHT = 20
BUTTONWIDTH = 40
TEXT_HEIGHT = 25
TEXT_LEFT_POSN = 10
BOARDWIDTH = 12
BOARDHEIGHT = 12
DISPLAYWIDTH = 200
XMARGIN = int((WINDOWWIDTH - (BOARDWIDTH * TILESIZE) - (DISPLAYWIDTH + 20)) / 2)
YMARGIN = int((WINDOWHEIGHT - (BOARDHEIGHT * TILESIZE)) / 2)

BLACK   = (  0,   0,   0)
WHITE   = (255, 255, 255)
GREEN   = (  0, 204,   0)
GRAY    = ( 60,  60,  60)
BLUE    = (  0,  50, 255)
YELLOW  = (255, 255,   0)
DARKGRAY =( 40,  40,  40)

BGCOLOR = GRAY
BUTTONCOLOR = GREEN
TEXTCOLOR = WHITE
TILECOLOR = GREEN
BORDERCOLOR = BLUE
TEXTSHADOWCOLOR = BLUE
SHIPCOLOR = YELLOW
HIGHLIGHTCOLOR = BLUE

def main():
    global DISPLAYSURF, FPSCLOCK, BASICFONT, HELP_SURF, HELP_RECT, NEW_SURF, \
           NEW_RECT, SHOTS_SURF, SHOTS_RECT, BIGFONT
    pygame.init()
    FPSCLOCK = pygame.time.Clock()
    DISPLAYSURF = pygame.display.set_mode((WINDOWWIDTH, WINDOWHEIGHT))
    BASICFONT = pygame.font.Font('freesansbold.ttf', 20)
    BIGFONT = pygame.font.Font('freesansbold.ttf', 100)

    # create buttons
    HELP_SURF = BASICFONT.render("HELP", True, WHITE, GREEN)
    HELP_RECT = HELP_SURF.get_rect()
    HELP_RECT.topleft = (WINDOWWIDTH - 120, WINDOWHEIGHT - 593)
    NEW_SURF = BASICFONT.render("NEW GAME", True, WHITE, GREEN)
    NEW_RECT = NEW_SURF.get_rect()
    NEW_RECT.topleft = (WINDOWWIDTH - 120, WINDOWHEIGHT - 563)

    # shot counter
    SHOTS_SURF = BASICFONT.render("Shots: " + add_shot('n'), True, WHITE)
    SHOTS_RECT = SHOTS_SURF.get_rect()
    SHOTS_RECT.topleft = (WINDOWWIDTH - 750, WINDOWHEIGHT - 570)

    pygame.display.set_caption('Battleship')

    while True:
        run_game()
        show_text_screen('Game Over')

def run_game():
    revealed_tiles = generate_default_tiles()
    main_board = generate_default_tiles()
    add_ships_to_board(main_board)
    mousex, mousey = 0, 0

    while True:
        DISPLAYSURF.fill(BGCOLOR)
        DISPLAYSURF.blit(HELP_SURF, HELP_RECT)
        DISPLAYSURF.blit(NEW_SURF, NEW_RECT)
        DISPLAYSURF.blit(SHOTS_SURF, SHOTS_RECT)
        draw_board(main_board, revealed_tiles)
        mouse_clicked = False

        check_for_quit()
        for event in pygame.event.get():
            if event.type == MOUSEBUTTONUP:
                #print (event.pos[0], event.pos[1])
                if HELP_RECT.collidepoint(event.pos):
                    DISPLAYSURF.fill(BGCOLOR)
                    show_help_screen()
                else:
                    mousex, mousey = event.pos
                    mouse_clicked = True
            elif event.type == MOUSEMOTION:
                mousex, mousey = event.pos

        tilex, tiley = get_tile_at_pixel(mousex, mousey)
        if tilex != None and tiley != None:
            if not revealed_tiles[tilex][tiley]:
                draw_highlight_tile(tilex, tiley)
            if not revealed_tiles[tilex][tiley] and mouse_clicked:
                reveal_tiles_animation(main_board, [(tilex, tiley)])
                revealed_tiles[tilex][tiley] = True

        pygame.display.update()
        FPSCLOCK.tick(FPS)

def generate_default_tiles():
    default_tiles = []
    for i in range(BOARDWIDTH):
        default_tiles.append([False] * BOARDHEIGHT)
    return default_tiles

def reveal_tiles_animation(board, tiles_to_reveal):
    for coverage in range(TILESIZE, (-REVEALSPEED) - 1, -REVEALSPEED):
        draw_tile_covers(board, tiles_to_reveal, coverage)

def draw_tile_covers(board, tiles, coverage):
    for tile in tiles:
        left, top = left_top_coords_tile(tile[0], tile[1])
        if board[tile[0]][tile[1]] == True:
            pygame.draw.rect(DISPLAYSURF, SHIPCOLOR, (left, top, TILESIZE,
                                                      TILESIZE))
        else:
            pygame.draw.rect(DISPLAYSURF, BGCOLOR, (left, top, TILESIZE,
                                                    TILESIZE))
        if coverage > 0:
            pygame.draw.rect(DISPLAYSURF, TILECOLOR, (left, top, coverage,
                                                      TILESIZE))

    pygame.display.update()
    FPSCLOCK.tick(FPS)

def check_for_quit():
    for event in pygame.event.get(QUIT):
        pygame.quit()
        sys.exit()

def draw_board(board, revealed):
    for tilex in range(BOARDWIDTH):
        for tiley in range(BOARDHEIGHT):
            left, top = left_top_coords_tile(tilex, tiley)
            if not revealed[tilex][tiley]:
                pygame.draw.rect(DISPLAYSURF, TILECOLOR, (left, top, TILESIZE,
                                                          TILESIZE))
            else:
                if board[tilex][tiley] == True:
                    pygame.draw.rect(DISPLAYSURF, SHIPCOLOR, (left, top,
                                     TILESIZE, TILESIZE))
                else:
                    pygame.draw.rect(DISPLAYSURF, BGCOLOR, (left, top,
                                     TILESIZE, TILESIZE))

    for x in range(0, BOARDWIDTH * TILESIZE, TILESIZE):
        pygame.draw.line(DISPLAYSURF, DARKGRAY, (x + XMARGIN, YMARGIN), \
                        (x + XMARGIN, WINDOWHEIGHT - YMARGIN))
    for y in range(0, BOARDHEIGHT * TILESIZE, TILESIZE):
        pygame.draw.line(DISPLAYSURF, DARKGRAY, (XMARGIN, y + YMARGIN), \
                (WINDOWWIDTH - (DISPLAYWIDTH + 20 + XMARGIN), y + YMARGIN))

def add_ships_to_board(board):
    # this is a stub for the prototype only. needs to be refactored for next
    # milestone
    new_board = board
    board[1][1] = True
    board[1][2] = True
    board[1][8] = True
    board[2][8] = True
    board[3][8] = True
    board[5][8] = True

def left_top_coords_tile(tilex, tiley):
    left = tilex * TILESIZE + XMARGIN
    top = tiley * TILESIZE + YMARGIN
    return (left, top)

def get_tile_at_pixel(x, y):
    for tilex in range(BOARDWIDTH):
        for tiley in range(BOARDHEIGHT):
            left, top = left_top_coords_tile(tilex, tiley)
            tile_rect = pygame.Rect(left, top, TILESIZE, TILESIZE)
            if tile_rect.collidepoint(x, y):
                return (tilex, tiley)
    return (None, None)

def draw_highlight_tile(tilex, tiley):
    left, top = left_top_coords_tile(tilex, tiley)
    pygame.draw.rect(DISPLAYSURF, HIGHLIGHTCOLOR,
                    (left, top, TILESIZE, TILESIZE), 4)

def show_help_screen():
    # display the help screen until a button is pressed
    line1_surf, line1_rect = make_text_objs('Press a key to exit.', BASICFONT,
                                            TEXTCOLOR)
    line1_rect.topleft = (TEXT_LEFT_POSN, TEXT_HEIGHT)
    DISPLAYSURF.blit(line1_surf, line1_rect)

    line2_surf, line2_rect = make_text_objs('Enter instructions here.',
                                            BASICFONT, TEXTCOLOR)
    line2_rect.topleft = (TEXT_LEFT_POSN, TEXT_HEIGHT * 2)
    DISPLAYSURF.blit(line2_surf, line2_rect)

    line3_surf, line3_rect = make_text_objs('Enter instructions here.',
                                            BASICFONT, TEXTCOLOR)
    line3_rect.topleft = (TEXT_LEFT_POSN, TEXT_HEIGHT * 3)
    DISPLAYSURF.blit(line3_surf, line3_rect)

    while check_for_keypress() == None:
        pygame.display.update()
        FPSCLOCK.tick()

def check_for_keypress():
    # pulling out all KEYDOWN and KEYUP events from queue and returning any
    # KEYUP else return None
    for event in pygame.event.get([KEYDOWN, KEYUP]):
        if event.type == KEYDOWN:
            continue
        return event.key
    return None

def make_text_objs(text, font, color):
    surf = font.render(text, True, color)
    return surf, surf.get_rect()

def show_text_screen(text):
    DISPLAYSURF.fill(BGCOLOR)
    titleSurf, titleRect = make_text_objs(text, BIGFONT, TEXTSHADOWCOLOR)
    titleRect.center = (int(WINDOWWIDTH / 2), int(WINDOWHEIGHT / 2))
    DISPLAYSURF.blit(titleSurf, titleRect)

    titleSurf, titleRect = make_text_objs(text, BIGFONT, TEXTCOLOR)
    titleRect.center = (int(WINDOWWIDTH / 2) - 3, int(WINDOWHEIGHT / 2) - 3)
    DISPLAYSURF.blit(titleSurf, titleRect)

    pressKeySurf, pressKeyRect = make_text_objs('Press a key to play.',
                                                BASICFONT, TEXTCOLOR)
    pressKeyRect.center = (int(WINDOWWIDTH / 2), int(WINDOWHEIGHT / 2) + 100)
    DISPLAYSURF.blit(pressKeySurf, pressKeyRect)

    while check_for_keypress() == None:
        pygame.display.update()
        FPSCLOCK.tick()

if __name__ == "__main__": #This calls the game loop
    main()
<|endoftext|>