# uncompyle6 version 3.7.4
# Python bytecode 3.7 (3394)
# Decompiled from: Python 3.6.9 (default, Apr 18 2020, 01:56:04)
# [GCC 8.4.0]
# Embedded file name: D:\Development\django2-propeller\django2_propeller\card.py
# Compiled at: 2019-04-26 08:32:14
# Size of source mod 2**32: 10322 bytes
"""This module contains classes for constructing propeller cards"""
from django.utils.safestring import mark_safe
from .utils import render_tag, add_css_class
from .components import Button, FAB, Image
from .text import text_concat
from .exceptions import PropellerException

class CardTitle(object):
    __doc__ = '\n    Renders a Card Title.\n\n    **Parameters**:\n\n        text\n            The display text for the title.\n\n        size\n            The size for the title as integer. Works with the h-tag, so size=1 is bigger than size=3.\n            Optional. (default=3)\n    '
    text = ''
    size = 3

    def as_html(self):
        """Returns card title as html"""
        tag = 'h%d' % self.size
        attrs = {'class': 'pmd-card-title-text'}
        content = self.text
        return render_tag(tag, attrs=attrs, content=(mark_safe(content)))

class CardSubtitle(object):
    __doc__ = '\n    Renders a Card Subtitle.\n\n    **Parameters**:\n\n        text\n            The display text for the subtitle.\n    '
    text = ''

    def as_html(self):
        """Returns card subtitle as html"""
        tag = 'span'
        attrs = {'class': 'pmd-card-subtitle-text'}
        content = self.text
        return render_tag(tag, attrs=attrs, content=(mark_safe(content)))

class CardBody(object):
    __doc__ = '\n    Renders a Card Body.\n\n    **Parameters**:\n\n        text\n            The text to display in the body.\n    '
    text = ''

    def as_html(self):
        """Returns card body as html"""
        tag = 'div'
        attrs = {'class': 'pmd-card-body'}
        content = self.text
        return render_tag(tag, attrs=attrs, content=(mark_safe(content)))

class CardHeader(object):
    __doc__ = '\n    Renders a Card Header.\n\n    **Parameters**:\n\n        content_left\n            A list of items to display on the left of header. \n            May contain Button, FAB, Image, CardTitle, or CardSubtitle.\n\n        content_middle\n            A list of items to display in the middle of header. \n            May contain Button, FAB, Image, CardTitle, or CardSubtitle.\n    '
    content_left = []
    content_middle = []

    def get_left_content(self):
        """Returns left content of card header as html"""
        tag = 'div'
        attrs = {'class': 'media-left'}
        content = ''
        for itm in self.content_left:
            content = text_concat(content, mark_safe(itm.as_html()))

        return render_tag(tag, attrs=attrs, content=(mark_safe(content)))

    def get_middle_content(self):
        """Returns middle content of card header as html"""
        tag = 'div'
        attrs = {'class': 'media-body media-middle'}
        content = ''
        for itm in self.content_middle:
            content = text_concat(content, mark_safe(itm.as_html()))

        return render_tag(tag, attrs=attrs, content=(mark_safe(content)))

    def as_html(self):
        """Returns card header as html"""
        tag = 'div'
        attrs = {'class': 'pmd-card-title'}
        content = text_concat(self.get_left_content(), self.get_middle_content())
        return render_tag(tag, attrs=attrs, content=(mark_safe(content)))

class CardMediaActions(object):
    __doc__ = '\n    Renders Card Media Actions.\n\n    **Parameters**:\n\n        items\n            A list of items to display in the Card Media Action section. \n            May contain Button or FAB.\n    '
    items = []

    def as_html(self):
        """Returns card media actions as html"""
        tag = 'div'
        attrs = {'class': 'pmd-card-actions'}
        content = ''
        for btn in self.items:
            if isinstance(btn, FAB):
                content = text_concat(content, mark_safe(btn.as_html()))

        return render_tag(tag, attrs=attrs, content=(mark_safe(content)))

class CardActions(object):
    __doc__ = '\n    Renders Card Actions.\n\n    **Parameters**:\n\n        items\n            A list of items to display in the Card Action section. \n            May contain Button or FAB.\n    '
    items = []

    def as_html(self):
        """Returns card actions as html"""
        tag = 'div'
        attrs = {'class': 'pmd-card-actions'}
        content = ''
        for btn in self.items:
            if isinstance(btn, Button):
                content = text_concat(content, mark_safe(btn.as_html()))

        return render_tag(tag, attrs=attrs, content=(mark_safe(content)))

class CardMediaImage(object):
    __doc__ = '\n    Renders a Card Media Image.\n\n    **Parameters**:\n\n        image\n            Must be an instance of an Image.\n    '
    image = None

    def as_html(self):
        """Returns card media image as html"""
        if isinstance(self.image, Image):
            return self.image.as_html()

class CardMedia(object):
    __doc__ = '\n    Renders Card Media\n\n    **Parameters**:\n\n        content\n            if style_inline=True:\n                A list of items to display in the card media section. \n                May contain CardMediaImage, CardTitle, or CardSubtitle.\n            or if style_inline=False: (default)\n                A instance of CardMediaImage\n\n        style_inline\n            Display card with inline style if true. (Default: False)\n    '
    content = None
    style_inline = False

    def get_media_body_inline(self):
        """Returns media body inline as html"""
        tag = 'div'
        attrs = {'class': 'media-body'}
        content = ''
        if self.style_inline:
            if not isinstance(self.content, list):
                raise PropellerException('Propeller Card: content must be a list')
            for itm in self.content:
                if isinstance(itm, (CardTitle, CardSubtitle)):
                    content = text_concat(content, mark_safe(itm.as_html()))

            content = text_concat(content, mark_safe('
'))
            content = text_concat(content, mark_safe('
'))
            for itm in self.content:
                if isinstance(itm, CardMediaImage):
                    content = text_concat(content, mark_safe(itm.as_html()))

        return render_tag(tag, attrs=attrs, content=(mark_safe(content)))

    def get_media_body(self):
        """Returns media body as html"""
        if self.style_inline:
            return self.get_media_body_inline()
        tag = 'div'
        attrs = {'class': 'media-body'}
        content = ''
        if not isinstance(self.content, list):
            raise PropellerException('Propeller Card: content must be a list')
        for itm in self.content:
            if isinstance(itm, CardMediaImage):
                content = text_concat(content, mark_safe(itm.as_html()))

        return render_tag(tag, attrs=attrs, content=(mark_safe(content)))

    def as_html(self):
        """Returns card media as html"""
        tag = 'div'
        attrs = {'class': 'pmd-card-media'}
        content = self.get_media_body()
        return render_tag(tag, attrs=attrs, content=(mark_safe(content)))

class Card(object):
    __doc__ = '\n    Card is a class that generates a Propeller Card.\n\n    **Parameters**:\n\n        primary_title\n            An instance of CardTitle. Optional.\n\n        secondary_title\n            An instance of CardSubtitle. Optional.\n\n        header\n            An instance of CardHeader. Optional.\n\n        media\n            An instance of CardMedia. Optional.\n\n        body\n            An instance of CardBody. Optional.\n\n        actions\n            An instance of CardActions. Optional.\n\n        media_actions\n            An instance of CardMediaActions. Optional.\n\n        style_inverse\n            Display dark style if true. (Default: False)\n\n        style_inline\n            Display card with inline style if true. (Default: False)\n\n        width\n            Width of the card in Bootstrap grid (col-md) as integer. (Default: 4)\n    '
    primary_title = None
    secondary_title = None
    header = None
    media = None
    body = None
    actions = None
    media_actions = None
    style_inverse = False
    style_inline = False
    width = 4

    def get_actions(self):
        """Returns actions of card as html"""
        actions = ''
        if self.media_actions:
            actions = text_concat(actions, self.media_actions.as_html())
        if self.actions:
            actions = text_concat(actions, self.actions.as_html())
        return actions

    def get_content(self):
        """Returns content of card as html"""
        content = ''
        if self.header:
            if not self.style_inline:
                content = text_concat(content, self.header.as_html())
        if self.media:
            content = text_concat(content, self.media.as_html())
        if not self.style_inline:
            if self.primary_title or self.secondary_title:
                content = text_concat(content, '
')
                if self.primary_title:
                    content = text_concat(content, self.primary_title.as_html())
                if self.secondary_title:
                    content = text_concat(content, self.secondary_title.as_html())
                content = text_concat(content, '
')
            if self.body:
                content = text_concat(content, self.body.as_html())
        content = text_concat(content, self.get_actions())
        return content

    def as_html(self):
        """Returns card as html"""
        tag = 'div'
        classes = 'pmd-card'
        if self.style_inline:
            classes = add_css_class(classes, 'pmd-card-media-inline')
        elif self.style_inverse:
            classes = add_css_class(classes, 'pmd-card-inverse')
        else:
            classes = add_css_class(classes, 'pmd-card-default')
        classes = add_css_class(classes, 'pmd-z-depth')
        classes = add_css_class(classes, 'col-md-%d' % self.width)
        attrs = {'class': classes}
        content = self.get_content()
        return render_tag(tag, attrs=attrs, content=(mark_safe(content)))


import pygame
from pygame.locals import *
from snake_agent import SnakeAgent
from board import BoardEnv
import helper
import time

#   This is the SnakeGame class you'll be working with.
#   It first initializes based on the default conditions.
#   Then it trains according to the number of training steps
#       printing the stats along the way
#   Then it tests using the training set and again prints
#        prints the points along the way
#   Then it calls the show_games function which also based on
#       the parameters, shows a number of games being played
#       based on the training done.

class SnakeGame:

    #   This constructor initializes the board according to the conditions mentioned
    #       in the helper file.
    #   It sets the board and initializes the snake agent.
    def __init__(self, args):
        self.args = args
        self.env = BoardEnv(args.snake_head_x, args.snake_head_y, args.food_x, args.food_y)
        self.agent = SnakeAgent(self.env.get_actions(), args.Ne, args.LPC, args.gamma)

    #   This function does the necessary function calls, to do_training() (if necessary)
    #       then the do_testint() then show_games()
    def play(self):
        if self.args.NUM_TRAIN_ITER != 0:
            self.do_training()
        self.do_testing()
        self.show_games()

    #   This is the function that does calls the functions to do reinforcement training
    #       as many times as specified. It also prints the statistics based on the
    #       parameter specfiied
    def do_training(self):
        print("IN TRAINING PHASE: ")
        self.agent.set_train()
        NUM_TO_STAT = self.args.NUM_TO_STAT
        self.points_results = []
        start = time.time()
        self.env.display()
        pygame.event.pump()

        #   This loop will train for required number of times
        #   WRITE YOUR CODE IN THIS LOOP TO CALL THE TRAINING FUNCTION.
        #   AS TRAINING IS HAPPENING THE CODE IN THE LOOP WILL PRINT STATISTICS.
        #   Use self.env.reset() to reset your game after each iteration.
        for game in range(1, self.args.NUM_TRAIN_ITER + 1):
            print("TRAINING NUMBER : " + str(game))
            state = self.env.get_state()
            dead = False
            action = self.agent.agent_action(state, 0, dead)
            count = 0
            while not dead:
                count +=1
                pygame.event.pump()
                state, points, dead = self.env.step(action)
                # Qlearning agent
                action = self.agent.agent_action(state, points, dead)
            #after game, reset
            print("GAME OVER\n\n\n")
            self.env.reset()
            self.points_results.append(points)
            #UNCOMMENT THE CODE BELOW TO PRINT STATISTICS
            if game % self.args.NUM_TO_STAT == 0:
                print(
                    "Played games:", len(self.points_results) - NUM_TO_STAT, "-", len(self.points_results),
                    "Calculated points (Average:", sum(self.points_results[-NUM_TO_STAT:])/NUM_TO_STAT,
                    "Max points so far:", max(self.points_results[-NUM_TO_STAT:]),
                    "Min points so far:", min(self.points_results[-NUM_TO_STAT:]),")",
                )

        print("Training takes", time.time() - start, "seconds")
        #   THIS LINE WILL SAVE THE MODEL TO THE FILE "model.npy"
        self.agent.save_model()

    #   This function will test based on the model you created. It first reads the
    #       "model.npy" file created above and makes moves based on the trained model
    def do_testing(self):
        print("Test Phase:")
        self.agent.set_eval()
        #   This line loads the model
        self.agent.load_model()
        points_results = []
        start = time.time()

        #   This loop runs the test the specified number of times.
        #   This is where you will write your code.
        #   Use self.env.reset() to reset your state everytime a new game begins.
        for game in range(1, self.args.NUM_TEST_ITER + 1):
            print("TESTING NUMBER: " + str(game))
            # YOUR CODE HERE
            # YOUR CODE HERE
            # YOUR CODE HERE
            # YOUR CODE HERE
            # YOUR CODE HERE

        #UNCOMMENT THE CODE BELOW TO PRINT STATISTICS
        #print("Testing takes", time.time() - start, "seconds")
        #print("Number of Games:", len(points_results))
        #print("Average Points:", sum(points_results)/len(points_results))
        #print("Max Points:", max(points_results))
        #print("Min Points:", min(points_results))

    #   This function is the one where the game will be displayed.
    #   This function is already written for you. No changes are necessary
    #       as long as YOU don't change function names or parameters.
    def show_games(self):
        print("Display Games")
        self.env.display()
        pygame.event.pump()
        self.agent.set_eval()
        points_results = []
        end = False
        for game in range(1, self.args.NUM_DISP_ITER + 1):
            state = self.env.get_state()
            dead = False
            action = self.agent.agent_action(state, 0, dead)
            count = 0
            while not dead:
                count +=1
                pygame.event.pump()
                keys = pygame.key.get_pressed()
                if keys[K_ESCAPE] or self.check_quit():
                    end = True
                    break
                state, points, dead = self.env.step(action)
                # Qlearning agent
                action = self.agent.agent_action(state, points, dead)
            if end:
                break
            self.env.reset()
            points_results.append(points)
            print("Game:", str(game)+"/"+str(self.args.NUM_DISP_ITER), "Points:", points)
        if len(points_results) == 0:
            return
        print("Average Points:", sum(points_results)/len(points_results))

    def check_quit(self):
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                return True
        return False

#   This is the main for the program, it generates the default arguemnts and calls the play function
if __name__ == "__main__":

    main_args = helper.make_args()
    print(main_args)
    game1 = SnakeGame(main_args)
    game1.play()


#! /usr/bin/env python3
#
# Gets and retusrns valuse from a single line of temperature sensor data
#
# Author: Cody Sayer
# Version: 2.0
#
from datetime import datetime

class AbstractReading:
    """Maintains the details of each sensor reading
    """

    DATE_TIME_STR = "Date Time"
    SEQUENCE_NUM_STR = "Sequence Number"
    SENSOR_MODEL_STR = "Sensor Model"
    LOW_TEMP_STR = "Low temp"
    AVERAGE_TEMP_STR = "Average temp"
    HIGH_TEMP_STR = "High temp"
    STATUS_STR = "Status"
    STRPTIME = "%Y-%m-%d %H:%M:%S.%f"
    STRFTIME = "%Y/%m/%d %H:%M"
    DEGREE_SIGN = u'\N{DEGREE SIGN}'
    HIGH_READ_ERROR = "HIGH_READ"
    LOW_READ_ERROR = "LOW_READ"

    def __init__(self, date_time, sequence_num, sensor_model, low_temp, avg_temp, high_temp, status):
        """constructor for the SensorReading class

        Arguments:
            date_time {string} -- the date and time from the CSV
            sequence_num {int} -- the sequence number if the sensor reading
            sensor_model {string} -- the model information of the sensor
            low_temp {float} -- the low temp reading from the sensor
            avg_temp {float} -- the avg temp reading from the sensor
            high_temp {float} -- the high temp reading from the sensor
            status {string} -- the status of the sensor during the reading
        """

        AbstractReading._validate_input(AbstractReading.DATE_TIME_STR, date_time)
        AbstractReading._validate_string(AbstractReading.DATE_TIME_STR, date_time)
        self._date_time = date_time

        AbstractReading._validate_input(AbstractReading.SEQUENCE_NUM_STR, sequence_num)
        AbstractReading._validate_int(AbstractReading.SEQUENCE_NUM_STR, sequence_num)
        self._sequence_num = sequence_num

        AbstractReading._validate_input(AbstractReading.SENSOR_MODEL_STR, sensor_model)
        AbstractReading._validate_string(AbstractReading.SENSOR_MODEL_STR, sensor_model)
        self._sensor_model = sensor_model

        AbstractReading._validate_input(AbstractReading.LOW_TEMP_STR, low_temp)
        AbstractReading._validate_float(AbstractReading.LOW_TEMP_STR, low_temp)
        self._low_read = low_temp

        AbstractReading._validate_input(AbstractReading.AVERAGE_TEMP_STR, avg_temp)
        AbstractReading._validate_float(AbstractReading.AVERAGE_TEMP_STR, avg_temp)
        self._average_read = avg_temp

        AbstractReading._validate_input(AbstractReading.HIGH_TEMP_STR, high_temp)
        AbstractReading._validate_float(AbstractReading.HIGH_TEMP_STR, high_temp)
        self._high_read = high_temp

        AbstractReading._validate_input(AbstractReading.STATUS_STR, status)
        AbstractReading._validate_string(AbstractReading.STATUS_STR, status)
        self._status = status

    def format_datetime_string(self):
        """Retrieves the datetime and formats into Y/M/D H:M

        Returns:
            datetime -- the datetime from a sensor reading
        """

        return datetime.strptime(self._date_time, "%Y-%m-%d %H:%M:%S.%f").strftime("%Y/%m/%d %H:%M")

    def format_output_strings(self):
        """Returns a message if error is detected in log

        Returns:
            string -- error message
        """

        if self._status == AbstractReading.HIGH_READ_ERROR:
            status_display = "High reading Error (100%cC)" % AbstractReading.DEGREE_SIGN
            reading_display_datetime = self.format_datetime_string()
            reading_seq_num = self.get_sequence_num()
            return "%s at %s, Sequence: %d" % (status_display, reading_display_datetime, reading_seq_num)

        elif self._status == AbstractReading.LOW_READ_ERROR:
            status_display = "Low reading Error (-50%cC)" % AbstractReading.DEGREE_SIGN
            reading_display_datetime = self.format_datetime_string()
            reading_seq_num = self.get_sequence_num()
            return "%s at %s, Sequence: %d" % (status_display, reading_display_datetime, reading_seq_num)

    def get_sequence_num(self):
        """retrieves and returns the sequence number from the reading line

        Returns:
            int -- sequence number
        """

        return self._sequence_num

    def get_sensor_model(self):
        """retrieves and returns the sensor model from the reading line

        Returns:
            string -- sensor model
        """

        return self._sensor_model

    def get_low_reading(self):
        """retrieves and returns the lowest reading from the reading line

        Returns:
            float -- low reading
        """

        return self._low_read

    def get_average_reading(self):
        """retrieves and returns the average reading from the reading line

        Returns:
            float -- average reading
        """

        return self._average_read

    def get_high_reading(self):
        """retrieves and returns the highest reading from the reading line

        Returns:
            float -- high reading
        """

        return self._high_read

    def get_reading_range(self):
        """calculates and returns the reading range from the reading line

        Returns:
            float -- reading range
        """

        return self._high_read - self._low_read

    def get_status(self):
        """retrieves and returns the status message of a reading line

        Returns:
            string -- sensor status
        """

        return self._status

    def is_error(self):
        """checks if there is an error

        Raises:
            NotImplementedError -- is_error test not implemented
        """

        raise NotImplementedError()

    def get_error_msg(self):
        """retrieves the error message if present

        Raises:
            NotImplementedError -- get_error_message not implemented
        """

        raise NotImplementedError()

    @staticmethod
    def _validate_string(display_name, input_value):
        """validates a string

        Arguments:
            display_name {value name} -- the actual name assigned to the value
            input_value {input} -- the input to be validated

        Raises:
            ValueError -- if it is not a string
        """

        if input_value != str(input_value):
            raise ValueError(display_name + " must be a string type")

    @staticmethod
    def _validate_int(display_name, input_value):
        """validates the input value to make sure it is of int type

        Arguments:
            display_name {value name} -- the given name for a value
            input_value {input} -- the input to be validated

        Raises:
            ValueError -- if it is not an integer
        """

        if input_value != int(input_value):
            raise ValueError(display_name + " must be a integer type")

    @staticmethod
    def _validate_float(display_name, input_value):
        """validates the input value to make sure it is of float type

        Arguments:
            display_name {value name} -- the given name for a value
            input_value {input} -- the input to be validated

        Raises:
            ValueError -- if it is not a float
        """

        if input_value != float(input_value):
            raise ValueError(display_name, " must be a float type")

    @staticmethod
    def _validate_input(display_name, input_value):
        """validates the input to make sure it is not None or empty

        Arguments:
            display_name {value name} -- the given name for a value
            input_value {input} -- the input to be validated

        Raises:
            ValueError -- if input is undefined
            ValueError -- if input is empty
        """

        if input_value is None:
            raise ValueError(display_name + " cannot be undefined.")

        if input_value == "":
            raise ValueError(display_name + " cannot be empty.")


# Copyright 2012 Rackspace

# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#     http://www.apache.org/licenses/LICENSE-2.0

# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import mock

from twisted.trial import unittest
from twisted.python import filepath

import bot

class LogBotTestCase(unittest.TestCase):
    """
    Tests for L{bot.LogBot}
    """

    def setUp(self):
        for logger in ('PyLogger', 'BufferedSearchLogger',
                       'BufferedMultiChannelFileLogger'):
            self.patch(bot.loggers, logger, mock.MagicMock(['log']))

    def _make_mock_logbot(self, channels, log_path=None):
        """
        Initialize a fake logbot (to use as self to functions) and set the
        factory to a fake factory.
        """
        if not log_path:
            log_path = self.mktemp()
            fp = filepath.FilePath(log_path)
            fp.createDirectory()
        self.log_path = log_path

        fake_factory = mock.MagicMock(channels=channels, log_path=log_path)

        fp = filepath.FilePath(self.mktemp())
        fp.createDirectory()

        self.fake_logbot = mock.MagicMock(bot.LogBot,
                                          _user_left_FP=fp,
                                          factory=fake_factory)

    def test_join_all_channels_on_signon(self):
        """
        On signing on to the server, the bot should join all of the channels in
        the channel list
        """
        channels = ['#channel1', '#channel2']
        self._make_mock_logbot(channels)

        # mock calling a LogBot().signedOn(), but with a fake logbot as self
        bot.LogBot.signedOn.im_func(self.fake_logbot)

        expected_calls = [mock.call(name) for name in channels]
        self.assertEqual(expected_calls, self.fake_logbot.join.mock_calls)

    def _run_connection_made(self):
        """
        Fake calls connection made, for the tests to see if a logger is created
        """
        self.channels = ['#channel1', '#channel2']
        self._make_mock_logbot(self.channels)
        # mock calling LogBot().connectionMade()
        bot.LogBot.connectionMade.im_func(self.fake_logbot)

    def test_connection_made_PyLogger(self):
        """
        When a connection is made, a PyLogger should be created
        """
        self._run_connection_made()
        bot.loggers.PyLogger.assert_called_once_with()

    def test_connection_made_BufferedSearchLogger(self):
        """
        When a connection is made, a BufferedSearchLogger should be created
        """
        self._run_connection_made()
        bot.loggers.BufferedSearchLogger.assert_called_once_with()

    def test_connection_made_BufferedMultiChannelFileLogger(self):
        """
        When a connection is made, a BufferedMultiChannelFileLogger should be
        created
        """
        self._run_connection_made()
        bot.loggers.BufferedMultiChannelFileLogger.assert_called_once_with(
            self.log_path, self.channels)

    def test_write_log(self):
        """
        LogBot().writeLog should log the same message to all available loggers
        """
        self.channels = ['#channel1', '#channel2']
        self._make_mock_logbot(self.channels)
        self.fake_logbot.loggers = [
            bot.loggers.PyLogger(),
            bot.loggers.BufferedSearchLogger(),
            bot.loggers.BufferedMultiChannelFileLogger()
        ]
        # mock calling LogBot().writeLog()
        bot.LogBot.writeLog.im_func(
            self.fake_logbot, "message", "user", "channel1")

        calls = None
        for logger in self.fake_logbot.loggers:
            if not calls:
                calls = logger.log.mock_calls
                self.assertEqual(1, len(calls))
            else:
                self.assertEqual(calls, logger.mock_calls)

    def test_user_join_is_logged(self):
        """
        Message should be logged when a user joins the channel
        """
        self._make_mock_logbot(['#channel1'])
        # mock calling LogBot.userJoined()
        bot.LogBot.userJoined.im_func(self.fake_logbot, 'me', '#channel1')
        self.assertEqual(1, self.fake_logbot.writeLog.call_count)
        # mock.call_args[0] = non-keyword arguments ([1] is keyword arguments)
        self.assertEqual((None, '#channel1'),
                         self.fake_logbot.writeLog.call_args[0][1:])

    def test_user_left_is_logged(self):
        """
        Message should be logged when a user leaves the channel
        """
        self._make_mock_logbot(['#channel1'])
        # mock calling LogBot.userLeft()
        bot.LogBot.userLeft.im_func(self.fake_logbot, 'me', '#channel1')
        self.assertEqual(1, self.fake_logbot.writeLog.call_count)
        # mock.call_args[0] = non-keyword arguments ([1] is keyword arguments)
        self.assertEqual((None, '#channel1'),
                         self.fake_logbot.writeLog.call_args[0][1:])

    def test_user_left_channel_time_record(self):
        """
        When the user leaves the channel, the time at which they left, as well
        as their username and the channel name is recorded in such a way that
        this time can be retrieved.
        """
        self._make_mock_logbot(['#channel1', '#channel2'])
        # mock calling LogBot.userLeft()
        bot.LogBot.userLeft.im_func(self.fake_logbot, 'me', '#channel1')
        bot.LogBot.userLeft.im_func(self.fake_logbot, 'me', '#channel2')
        # there should be some way to get the last exit times
        results = bot.LogBot._get_user_last_exit_time(self.fake_logbot, 'me')

        # assert that the correct channels (that the user exited) were recorded
        keys = results.keys()
        keys.sort()
        self.assertEqual(['#channel1', '#channel2'], keys)


import sys
from awsglue.transforms import *
from awsglue.utils import getResolvedOptions
from pyspark.context import SparkContext
from awsglue.context import GlueContext
from awsglue.job import Job

## @params: [JOB_NAME]
## @params: [target_bucket]
args = getResolvedOptions(sys.argv,
                          ['JOB_NAME',
                           'target_bucket'])
print ("JOB_NAME     : ", args['JOB_NAME'])
print ("target_bucket: ", args['target_bucket'])

# こちらを参考にさせていただきました
# https://future-architect.github.io/articles/20180828/
# https://dev.classmethod.jp/cloud/aws/20180528-aws-glue-etl-job-with-spark-sql/

sc = SparkContext()
glueContext = GlueContext(sc)
spark = glueContext.spark_session
job = Job(glueContext)
job.init(args['JOB_NAME'], args)

bucket = args['target_bucket']

df = glueContext.create_dynamic_frame.from_options(
    connection_type="s3",
    connection_options={
        "paths": ["s3://{0}/glue/csv2parquet/csv/test/".format(bucket)]},
    format="csv",
    format_options={"withHeader": True}  # 1行目をスキーマ名として認識True
    )

## @type: DataSink
## @args: [connection_type = "s3", connection_options = "paths": ["s3://{0}/csv2parquet/parquet/test/".format(bucket)], format = "parquet"]
## @return: datasink
## @inputs: [frame = df]
datasink = glueContext.write_dynamic_frame.from_options(
        frame=df,  # 出力するDynamicFrameを指定
        connection_type="s3",
        connection_options={
            "paths": ["s3://{0}/glue/csv2parquet/parquet/test".format(bucket)],
            "compression": "snappy"
        },
        format="parquet",
        transformation_ctx = "datasink"
    )

job.commit()

# 何故か動かない
# 下記エラーログ
#File "script_2019-06-23-11-40-00.py", line 53, in <module>
#transformation_ctx = "datasink"
#File "/mnt/yarn/usercache/root/appcache/application_1561289672599_0002/container_1561289672599_0002_01_000001/PyGlue.zip/awsglue/dynamicframe.py", line 585, in from_options
#File "/mnt/yarn/usercache/root/appcache/application_1561289672599_0002/container_1561289672599_0002_01_000001/PyGlue.zip/awsglue/context.py", line 194, in write_dynamic_frame_from_options
#File "/mnt/yarn/usercache/root/appcache/application_1561289672599_0002/container_1561289672599_0002_01_000001/PyGlue.zip/awsglue/context.py", line 217, in write_from_options
#File "/mnt/yarn/usercache/root/appcache/application_1561289672599_0002/container_1561289672599_0002_01_000001/PyGlue.zip/awsglue/data_sink.py", line 32, in write
#File "/mnt/yarn/usercache/root/appcache/application_1561289672599_0002/container_1561289672599_0002_01_000001/PyGlue.zip/awsglue/data_sink.py", line 28, in writeFrame
#File "/mnt/yarn/usercache/root/appcache/application_1561289672599_0002/container_1561289672599_0002_01_000001/py4j-0.10.4-src.zip/py4j/java_gateway.py", line 1133, in __call__
#File "/mnt/yarn/usercache/root/appcache/application_1561289672599_0002/container_1561289672599_0002_01_000001/pyspark.zip/pyspark/sql/utils.py", line 79, in deco
#pyspark.sql.utils.IllegalArgumentException: u'Expected exactly one path to be specified, but got: '
#End of LogType:stdout


def solution(arrangement):
    """
    스택을 이용해서 풀은 문제
    문제는 스택의 원소가 존재할 때, 존재하지 않을때를 생각해서 짜야 한다는 것이다.
    """
    answer = 0
    st = []
    idx = 0

    for elem in arrangement:
        # 파이프 추가
        if elem == "(":
            st.append(0)
        # 레이저일 경우,
        elif arrangement[idx-1] == "(":
            res = st.pop()

            if st:
                st[len(st)-1] += 1
        # 그냥 막대기일 경우
        else:
            res = st.pop()

            if st:
                st[len(st)-1] += res

            answer += ( res + 1 if res > 0 else 0 )

        idx += 1

    return answer

def solution2(arrangement):
    """
    스택이 아닌 단순히 pipe의 개수를 생각해서 푼 문제
    스택이 비었는지 체크를 안해도 되기 때문에 스택 사용보다 훨씬 빠르다.
    """
    answer = 0

    pipes = 0
    idx = 0

    for i in range(len(arrangement)):
        # 쇠 막대기가 추가된다.
        if arrangement[i] == '(':
            pipes += 1
        # 레이저일 경우 겹치는 파이프 개수 더해줌.
        elif arrangement[i-1] == '(':
            pipes -= 1
            answer += pipes
        # 그냥 막대일 경우는 pipe의 수를 줄이고 1개만 더해줌.
        else:
            pipes -= 1
            answer += 1

    return answer

if __name__ == '__main__':
    arrangement = "()(((()())(())()))(())"
    print(solution(arrangement))


import numpy as np
from process import get_data,get_binary_data

X,Y = get_data()

#randomly initialise the weights of the NN

#Number of hidden layers
M = 5
D = X.shape[1]
#Number of unique values in Y
K = len(set(Y))

W1 = np.random.randn(D,M)
b1 = np.zeros(M)

W2 = np.random.randn(M,K)
b2 = np.zeros(K)

def softmax(a):
    expA = np.exp(a)
    return expA/expA.sum(axis=1,keepdims=True)

def forward(X,W1,b1,W2,b2):
    Z = np.tanh(X.dot(W1)+b1)
    return softmax(Z.dot(W2)+b2)

P_Y_given_X = forward(X, W1, b1, W2, b2)

predictions = np.argmax(P_Y_given_X, axis=1)

def classification_rate(Y,P):
    return np.mean(Y==P)

print("Score: ",classification_rate(Y, predictions))


import argparse, os, time, sys, csv, io, time
import numpy as np
import pandas as pd
from PIL import Image, ImageFile

import torch
import torch.nn as nn
import torchvision.models as models
from torchvision import transforms as trn
from torch.nn import functional as F

### ???
ImageFile.LOAD_TRUNCATED_IMAGES = True

### cuda gpu ###
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")

def feature_extractor():
    resnet50 = models.resnet50(num_classes=1000, pretrained=True)
    modules = list(resnet50.children())[:-1]
    resnet50 = nn.Sequential(*modules)
    return resnet50

def extractor_train(data, tag, folder_dir):
    # load the image transformer
    centre_crop = trn.Compose([
            trn.Resize((256,256)),
            trn.CenterCrop(224),
            trn.ToTensor(),
            trn.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
    ])

    resnet50 = feature_extractor().to(device)

    image_feature, total_label, total_hashtag = [], [], []
    st = time.time()
    for idx, (img, label) in enumerate(zip(data, tag), 1):
        print('{:>5d}/{} {}'.format(idx, len(data), img[0]), end=' '*10+'\r' if idx != len(data) else '\n')
        all_hashtag = label[0].strip()
        label = img[0].split('/')[1]
        tmp_name = os.path.join(folder_dir, img[0])
        img = Image.open(tmp_name).convert('RGB')
        input_img = centre_crop(img).unsqueeze(0)
        ### gray image ###
        if input_img.shape[1] == 1:
            print('hh')
            input_img = input_img.detach().numpy()
            fake_img = np.zeros((1, 3, 224, 224))
            for i in range(3):
                fake_img[0, i, :] = input_img[0, 0, :]
            input_img = torch.from_numpy(fake_img).type(torch.FloatTensor)
        input_img = input_img.to(device)
        logit = resnet50.forward(input_img).squeeze().detach().cpu().numpy()

        image_feature.append(logit)
        total_label.append(label)
        total_hashtag.append(all_hashtag)

    np.savez('image_feature.npz',
             image_feature=image_feature[:-5730],
             total_label=total_label[:-5730],
             total_hashtag=total_hashtag[:-5730],
             val_image_feature=image_feature[-5730:],
             val_total_label=total_label[-5730:],
             val_total_hashtag=total_hashtag[-5730:])
    print('total time: ', time.time()-st)

if __name__ == "__main__":
    ### Path of data ###
    folder_dir = "../Hashtag/HARRISON/"
    filename = "tag_list.txt"
    data = pd.read_csv(folder_dir+'data_list.txt', header=None).values
    tag = pd.read_csv(folder_dir+'tag_list.txt', header=None).values
    extractor_train(data, tag, folder_dir)


n=int(input())
rev=0
t=n
while(n>0):
    temp=n%10
    rev=rev*10+temp
    n=n//10
print(rev)
print(t)
if(t==rev):
    print("palindrome")
else:
    print("no")


import os
from copy import deepcopy

from flask import Flask, make_response
from flask import session
from flask_cors import CORS
from flask_session import Session

from SodukoUtils import init_board_options, find_next_move
from data.sudopy import Sudoku

app = Flask(__name__)
SESSION_TYPE = 'filesystem'
app.secret_key = b'abjdslgjl'
app.config.from_object(__name__)
CORS(app, supports_credentials=True)
Session(app)
app.config.update(
    SESSION_COOKIE_SECURE=True,
    SESSION_COOKIE_HTTPONLY=True,
    SESSION_COOKIE_SAMESITE='None',
)

'''
why I'm doing so :
1. I want to succeed in being a better SW engineer
2. I want to improve my web development skills
3. I want to be better at sudoku
4. show myself that I can handle this code
5. show myself that I can learn how to develop in web
'''

''' next steps:
FE
1. provide a focus and enter a value and pencil marks
2. provide reason solution
'''

@app.route("/greeting")
def greeting():
    return {"greeting": "Hello from Flask!"}

@app.route('/')
@app.route('/index')
def index():
    return 'hi'

@app.route('/get_board/<int:board_id>', methods=['GET'])
def handle_connect(board_id):
    b = Sudoku(board_id).board
    session['board'] = b
    session['options'] = [[] * 9 for _ in range(9)]
    return {'board': b, 'id': 0}

def jsonify_options(options):
    res = deepcopy(options)
    for i in range(9):
        for j in range(9):
            res[i][j] = list(res[i][j])

    return res

@app.route('/get_pencil_marks', methods=['GET'])
def get_options():
    print('got load options')
    options = init_board_options(session['board'])
    session['options'] = options
    options_with_list_instead_of_sets = [[list(options[i][j]) for j in range(9)]for i in range(9)]
    return {'options': options_with_list_instead_of_sets}

@app.route('/get_next_step', methods=['GET'])
def get_next_step():
    print('got load next step!!!!!')
    board = session['board']
    options = session['options']
    options = [[set(options[i][j]) for j in range(9)]for i in range(9)]
    res = find_next_move(board, options)
    return res

def clean_puzzle(puzzle):
    """
    converts input from request.form to a string format readable by Sudoku
    """
    output = ''
    for val in puzzle.values():
        if val == '':
            output += '.'
        elif int(val) in range(1, 10):
            output += val
    return output


from datetime import datetime
from os.path import dirname, join

# import pytest
from city_scrapers_core.constants import NOT_CLASSIFIED
from city_scrapers_core.utils import file_response
from freezegun import freeze_time

from city_scrapers.spiders.pitt_city_planning import PittCityPlanningSpider

test_response = file_response(
    join(dirname(__file__), "files", "pitt_city_planning.html"),
    url="http://pittsburghpa.gov/dcp/notices",
)
spider = PittCityPlanningSpider()

freezer = freeze_time("2019-08-14")
freezer.start()

parsed_items = [item for item in spider.parse(test_response)]

freezer.stop()

# def test_tests():
# assert False
"""
Uncomment below
"""

def test_title():
    assert parsed_items[0][
        "title"] == "CMU IMP Amendment for Scaife Hall and Zone Change Petition (parcel53-B-100)"

def test_description():
    assert parsed_items[0]["description"] == ""

def test_start():
    assert parsed_items[0]["start"] == datetime(2019, 7, 30, 14, 0)

# def test_end():
#     assert parsed_items[0]["end"] == datetime(2019, 1, 1, 0, 0)

def test_time_notes():
    assert parsed_items[0]["time_notes"] == ""

# def test_id():
#     assert parsed_items[0]["id"] == "EXPECTED ID"

# def test_status():
#    assert parsed_items[0]["status"] == "PASSED"

def test_location():
    assert parsed_items[0]["location"] == {
        "name": '',
        "address": '200 Ross Street, 1stFloor Conference Room'
    }

# def test_source():
#     assert parsed_items[0]["source"] == "EXPECTED URL"

def test_links():
    url_begin = 'https://apps.pittsburghpa.gov/redtail/'
    assert parsed_items[0]["links"] == [{
        'href': url_begin + 'images/6132_CMU_IMP_amendment_Application.pdf',
        'title': 'Amendment'
    }, {
        'href':
            url_begin +
            'images/6134_Institutional_Master_Plan_with_draft_amendments_highlighted.pdf',
        'title': 'amendments highlighted'
    }, {
        'href': url_begin + 'images/6133_CMU_Zone_Change_Petition_Application.pdf',
        'title': 'Petition'
    }, {
        'href': 'https://apps.pittsburghpa.gov/redtail/images/6135_ZoningCMUMapChange3.pdf',
        'title': 'Map'
    }]

def test_classification():
    assert parsed_items[0]["classification"] == NOT_CLASSIFIED

# @pytest.mark.parametrize("item", parsed_items)
# def test_all_day(item):
#     assert item["all_day"] is False


import os, time, re, pickle, signal
from selenium import webdriver
import pytesseract
from PIL import Image
from selenium.webdriver.chrome.options import Options
from selenium.common.exceptions import TimeoutException
import scrapy

# Selenium Part
# Setting driver
driver = webdriver.Chrome('/Users/mya/Downloads/chromedriver')
driver.maximize_window()
screenshot_path =  '/Users/mya/DBDCrawler/scrapy_app/scrapy_app/spiders/temp/screenshot.png'
# The start page (login page)
login_page_url = 'https://datawarehouse.dbd.go.th/login'
# Store cookie
cookie_path = '/Users/mya/DBDCrawler/scrapy_app/scrapy_app/spiders/temp/cookie.json'

# Access login page
driver.get(login_page_url)
print(driver.title)

# get and verify captcha, then access 'https://datawarehouse.dbd.go.th/index' page
def getAndStoreCookieAfterLogin():
    for i in range(10):
        time.sleep(10)
        if 'Home' in driver.title:
            # load cookie
            cookies = driver.get_cookies()
            # find token 'JSESSIONID' and store it into cookie_path
            for i in cookies:
                # print(i)
                if i['name'] == 'JSESSIONID':

                    with open(cookie_path, 'wb') as f:
                        pickle.dump(cookies, f)
                    print(i['value'])
                    break
                else:
                    print('no JSESSIONID in this page!')
            break
        else:
            driver.refresh()

# check whether access 'https://datawarehouse.dbd.go.th/index' page successfully
getAndStoreCookieAfterLogin()

# Change language
driver.find_element_by_xpath('//*[@id="lang"]').click()

# Search 'Opencloud' and access 'https://datawarehouse.dbd.go.th/company/profile/5/0105554123553' page
# English Version
# driver.find_element_by_xpath('//*[@id="textStr"]').send_keys('Opencloud')
# driver.find_element_by_xpath('//*[@id="form"]/div/button').click()
# driver.find_element_by_xpath('//*[@id="fixTable"]/tbody/tr').click()


API_URL                             = "https://as.revogi.net/app"

API_LOGIN_PATH                      = "/user/101"
API_DEVICE_LIST_PATH                = "/user/500"

API_DEVICE_DETAILS_PATH             = "/pww/31101"
API_DEVICE_SCHEDULE_DETAILS_PATH    = "/pww/31102"

API_DEVICE_SWITCH_PATH              = "/pww/21101"
API_DEVICE_TIMER_PATH               = "/pww/21102"
API_DEVICE_LED_PATH                 = "/pww/21104"

API_RESET_CLEAN_PUMP_TIMER          = "/pww/21103"
API_RESET_FILTER_CHANGE_TIMER       = "/pww/21105"
API_RESET_WATER_CHANGE_TIMER        = "/pww/21107"

SECONDS_FOUNTAIN_WATER_CHANGE       = 5 * 24 * 60 * 60    #  5 days
SECONDS_FOUNTAIN_FILTER_CHANGE      = 30 * 24 * 60 * 60   # 30 days
SECONDS_FOUNTAIN_CLEAN_PUMP         = 60 * 24 * 60 * 60   # 60 days

Debug                           = 1


import time
from turtle import Screen
from board import Board
from player import Timmy

player = Timmy(-50, -290, "blue")
machine = Timmy(50, -290, "red")
screen = Screen()
screen.tracer(0)
snake_board = Board()
screen.setup(800, 800)
screen.bgpic("snake1.gif")

with open("machine.txt","w") as file:
    file.write("")
with open("players.txt","w") as file:
    file.write("")

while True:
    PLAYER_VALUE = player.xcor()
    MACHINE_VALUE = machine.xcor()

    time.sleep(0.1)
    screen.update()
    screen.textinput("Snake Hai", "Name of first player:")
    # time.sleep(1)
    player.player_time()
    if player.ycor() > 225:
        player.goto(PLAYER_VALUE, 225)

    machine.machine_time()
    if machine.ycor() > 225:
        machine.goto(MACHINE_VALUE, 225)

    player.check()
    machine.check()


import torch
import torch.nn.functional as F
from torch.nn.modules.batchnorm import _BatchNorm
from Clustering import *

import random

import os

import numpy as np

import matplotlib.pyplot as plt
import matplotlib.image as mpimg

def group_norm(input, group, running_mean, running_var, weight=None, bias=None,
               use_input_stats=True, momentum=0.1, eps=1e-5):
    r"""Applies Group Normalization for channels in the same group in each data sample in a
    batch.
    See :class:`~torch.nn.GroupNorm1d`, :class:`~torch.nn.GroupNorm2d`,
    :class:`~torch.nn.GroupNorm3d` for details.
    """
    if not use_input_stats and (running_mean is None or running_var is None):
        raise ValueError('Expected running_mean and running_var to be not None when use_input_stats=False')

    b, c = input.size(0), input.size(1)
    if weight is not None:
        weight = weight.repeat(b)
    if bias is not None:
        bias = bias.repeat(b)

    def _instance_norm(input, group, running_mean=None, running_var=None, weight=None,
                       bias=None, use_input_stats=None, momentum=None, eps=None):
        # Repeat stored stats and affine transform params if necessary
        if running_mean is not None:
            running_mean_orig = running_mean
            running_mean = running_mean_orig.repeat(b)
        if running_var is not None:
            running_var_orig = running_var
            running_var = running_var_orig.repeat(b)
        eps = 1e-5
        G = group
        input_mod = input
        out_final = input
        input_cpu = input.to('cpu')
        x = input_cpu.to('cpu')
        x_np = x.detach().numpy()
        x_np = np.nan_to_num(x_np)
        # At this point we have just stored our input on CPU with Numpy
        print(33333,x_np,x_np.shape)
        Nabs, C, H, W = x_np.shape
        N = Nabs
        # N refers to the __? What the batch size?
        # res_x is the original shape of our input
        res_x = np.zeros((N, C, H, W))
        # this is a looser form where we are just by channel dimensions
        x_np_new = np.zeros((C, N * H * W))
        # we loop through the channels
        for i in range(C):
            # we are converting the raw input data such that there are channel rows of large N*H*W blocks
            # why?
            x_np_new[i, :] = np.reshape(x_np[:, i, :, :], (1, N * H * W))
        # 60 by 10000 trying to cluster in this basis
        # we create a copy of our reformatted data
        image_vector = np.asarray(x_np_new)
        Data = data_preparation(n_cluster=G, data=image_vector[:, :])
        # Up to here we are all correct, as long as we are clustering in the right basis
        count = 0
        Common_mul = 1
        Common_add = 0
        tmp1 = torch.zeros(())
        for val in range(G):
            # this creates all a list of all the indices where our label is valid
            inx = np.argwhere(Data.labels_ == val)
            if len(inx) > 0:
                tmp = torch.zeros((input.shape[0], len(inx), input.shape[2], input.shape[3]))
            for idx, idxx in enumerate(inx):
                if len(inx) == 0:
                    pass
                else:
                    # This is line of code is effectively saying fill into our temp channel equal to
                    # the original index from input for all the matching indices. so we have temp has
                    # number of channels assigned to that cluster
                    tmp[:, idx, :, :] = input[:, idxx[0], :, :]
                    # now we have the correct grouping so we choose to normalize
            # STAT is the function which I think has a bug...
            out_final_tmp = (Stat_torch(tmp)).float().to('cuda')
            for idx, idxx in enumerate(inx):
                if (len(inx) == 0):
                    pass
                else:
                    # here we assign that the the original index of out_final is equal to the temp
                    # index it was given in the normalized group
                    out_final[:, idxx[0], :, :] = out_final_tmp[:, idx, :, :]
        # we reshape it to the original size
        # this is probably fine since code doesnt error...
        # although double check this last line
        return out_final.view(b, c, *input.size()[2:])

    return _instance_norm(input, group, running_mean=running_mean,
                          running_var=running_var, weight=weight, bias=bias,
                          use_input_stats=use_input_stats, momentum=momentum,
                          eps=eps)

class HoogiNorm(_BatchNorm):
    def __init__(self, num_features, num_groups=5, eps=1e-5, momentum=0.1,
                 affine=False, track_running_stats=False):
        self.num_groups = num_groups
        self.track_running_stats = track_running_stats
        super(HoogiNorm, self).__init__(int(num_features / num_groups), eps,
                                        momentum, affine, track_running_stats)

    def _check_input_dim(self, input):
        return NotImplemented

    def forward(self, input):
        self._check_input_dim(input)

        return group_norm(
            input, self.num_groups, self.running_mean, self.running_var, self.weight, self.bias,
            self.training or not self.track_running_stats, self.momentum, self.eps)

# Deprecated Method of Normalization
def Stat(IN):
    tmp = np.zeros((IN.shape[0]))
    tmp2 = 0
    eps = 1e-5
    sigma = np.zeros((IN.shape[0], 1))
    out = IN
    # print('IN is:')
    # print(IN.shape)
    '''for i in range(IN.shape[1]):
        tmp += np.sum(IN[:,i,:,:])'''
    for i in range(IN.shape[0]):
        # for j in range(IN.shape[1]):
        tmp[i] += np.sum(IN[i, :, :, :])

    # tmp = np.sum(IN, where=[False, True, True, True])
    mu = (1 / (IN.shape[1] * IN.shape[2] * IN.shape[3])) * tmp
    for i in range(IN.shape[0]):
        sigma[i] = np.sqrt(
            (1 / (IN.shape[1] * IN.shape[2] * IN.shape[3])) * (np.sum((IN[i, :, :, :] - mu[i]) ** 2) + eps))
    # sigma = np.sqrt((1 / (IN.shape[0] * IN.shape[2] * IN.shape[3])) * tmp2)
    for i in range(IN.shape[0]):
        for j in range(IN.shape[1]):
            out[i, j, :, :] = (1 / sigma[i]) * (IN[i, j, :, :] - mu[i])
    print(out)
    return out

# Current Method of Normalization
def Stat_torch(IN):
    print('I AM WORKING UP TO HERE')
    tmp = torch.zeros((IN.shape[0]))
    tmp2 = 0
    eps = 1e-5
    sigma = torch.zeros((IN.shape[0], 1))
    out = IN
    res = torch.zeros((IN.shape[0], 2, IN.shape[2], IN.shape[3]))
    res[:, 0, :, :], res[:, 1, :, :] = torch.std_mean(IN + eps, dim=1)
    for i in range(IN.shape[0]):
        for j in range(IN.shape[1]):
            out[i, j, :, :] = (1 / res[i, 0]) * (IN[i, j, :, :] - res[i, 1])
    return out


import numpy as np
import matplotlib.pyplot as plt
from matplotlib.patches import Ellipse
import matplotlib.transforms as transforms
from scipy.stats import multivariate_normal, pearsonr, spearmanr, multinomial

def Covariance(variances, correlations):
    covariance = [[] for i in range(len(variances))]
    for i in range(len(variances)):
        for k in range(len(variances)):
            covariance[i].append((variances[i] * variances[k]) * correlations[i][k])
    return covariance

def QuadrantCorrelation(x, y):
    centerRVS = [x - np.mean(x), y - np.mean(y)]
    centerRVS = [[centerRVS[0][i], centerRVS[1][i]] for i in range(len(centerRVS[0]))]
    n1 = len([elem for elem in centerRVS if elem[0] >= 0 and elem[1] >= 0])
    n2 = len([elem for elem in centerRVS if elem[0] <= 0 and elem[1] >= 0])
    n3 = len([elem for elem in centerRVS if elem[0] <= 0 and elem[1] <= 0])
    n4 = len([elem for elem in centerRVS if elem[0] >= 0 and elem[1] <= 0])
    return (n1 + n3 - n2 - n4) / float(len(x))

def mean_2(x):
    return np.mean([elem * elem for elem in x])

def MixedNormalDistribution(weights, means, covariances, size):
    cases = multinomial(1, weights).rvs(size)
    rvs = []
    for case in cases:
        k = case.tolist().index(1)
        rvs.append(multivariate_normal.rvs(means[k], covariances[k]))
    rvs = np.asarray(rvs)
    return [rvs[:, k] for k in range(len(rvs[0]))]

def confidence_ellipse(x, y, ax, n_std=3.0, edgecolor='red', linestyle='-'):
    cov = np.cov(x, y)
    pearson = cov[0, 1] / np.sqrt(cov[0, 0] * cov[1, 1])

    radius_x = np.sqrt(1 + pearson)
    radius_y = np.sqrt(1 - pearson)
    ellipse = Ellipse((0, 0), width=radius_x * 2, height=radius_y * 2, facecolor='none',
                      edgecolor=edgecolor, linestyle=linestyle)

    scale_x = np.sqrt(cov[0, 0]) * n_std
    mean_x = np.mean(x)
    scale_y = np.sqrt(cov[1, 1]) * n_std
    mean_y = np.mean(y)

    transf = transforms.Affine2D() \
        .rotate_deg(45) \
        .scale(scale_x, scale_y) \
        .translate(mean_x, mean_y)

    ellipse.set_transform(transf + ax.transData)
    return ax.add_patch(ellipse)

if __name__ == "__main__":
    sizes = [20, 60, 100]
    count = 1000
    means = [0, 0]
    variances_1, variances_2 = [1, 1], [10, 10]
    correlations_1, correlations_2 = [0, 0.5, 0.9], [0.9, -0.9]
    measuares = [['$E(z)$', np.mean], ['$E(z^2)$', mean_2], ['$D(z)$', np.var]]
    empiricalCorrelations = ['r', 'r_S', 'r_Q']
    variateCorrelations = [[[[0 for l in range(len(empiricalCorrelations))]
                             for k in range(len(measuares))]
                            for j in range(len(sizes))]
                           for i in range(len(correlations_1))]
    mixedVariateCorrelations = [[[0 for l in range(len(empiricalCorrelations))]
                                 for k in range(len(measuares))]
                                for j in range(len(sizes))]

    for i in range(len(correlations_1)):
        covariance = Covariance(variances_1,
                                [[1, correlations_1[i]], [correlations_1[i], 1]])
        for j in range(len(sizes)):
            P, S, Q = [], [], []
            for m in range(count):
                rvs = multivariate_normal.rvs(means, covariance, sizes[i])
                x, y = rvs[:, 0], rvs[:, 1]
                p, tmp = pearsonr(x, y)
                s, tmp = spearmanr(x, y)
                P.append(p)
                S.append(s)
                Q.append(QuadrantCorrelation(x, y))
            empCors = [P, S, Q]
            for k in range(len(measuares)):
                for l in range(len(empCors)):
                    variateCorrelations[i][j][k][l] = measuares[k][1](empCors[l])

    mixedCovariances = [Covariance(variances_1, [[1, correlations_2[0]], [correlations_2[0], 1]]),
                        Covariance(variances_2, [[1, correlations_2[1]], [correlations_2[1], 1]])]
    for j in range(len(sizes)):
        P, S, Q = [], [], []
        for m in range(count):
            rvs = MixedNormalDistribution([0.9, 0.1], [means, means], mixedCovariances, sizes[j])
            p, tmp = pearsonr(rvs[0], rvs[1])
            s, tmp = spearmanr(rvs[0], rvs[1])
            P.append(p)
            S.append(s)
            Q.append(QuadrantCorrelation(rvs[0], rvs[1]))
        empCors = [P, S, Q]
        for k in range(len(measuares)):
            for l in range(len(empCors)):
                mixedVariateCorrelations[j][k][l] = measuares[k][1](empCors[l])

    for i in range(len(correlations_1)):
        with open('pho = ' + str(correlations_1[i]) + ".tex", 'w') as file:
            for j in range(len(sizes)):
                file.write(" $n$=" + str(sizes[j]) + "&$r$" + "&$r_S$" + r"&$r_Q$\\")
                file.write("\hline")
                for k in range(len(measuares)):
                    file.write(measuares[k][0])
                    for m in range(len(empiricalCorrelations)):
                        file.write("&" + str(round(variateCorrelations[i][j][k][m], 4)))
                    file.write(r"\\")
                    file.write("\hline")
                file.write(r" & & & \\")
                file.write("\hline ")

    with open('Mixed.tex', 'w') as file:
        for j in range(len(sizes)):
            file.write(" $n$=" + str(sizes[j]) + "&$r$" + "&$r_S$" + r"&$r_Q$\\")
            file.write("\hline")
            for k in range(len(measuares)):
                file.write(measuares[k][0])
                for m in range(len(empiricalCorrelations)):
                    file.write("&" + str(round(mixedVariateCorrelations[j][k][m], 4)))
                file.write(r"\\")
                file.write("\hline")
            file.write(r" & & & \\")
            file.write("\hline ")

    for i in range(len(correlations_1)):
        covariance = Covariance(variances_1,
                                [[1, correlations_1[i]], [correlations_1[i], 1]])
        for j in range(len(sizes)):
            rvs = multivariate_normal.rvs(means, covariance, sizes[j])
            x, y = rvs[:, 0], rvs[:, 1]
            fig, ax_nstd = plt.subplots()
            ax_nstd.axvline(c='grey', lw=1)
            ax_nstd.axhline(c='grey', lw=1)
            ax_nstd.scatter(x, y, s=0.5)
            confidence_ellipse(x, y, ax_nstd)
            ax_nstd.scatter(0, 0, c='red', s=3)
            ax_nstd.set_xlabel('x')
            ax_nstd.set_ylabel('y')
            ax_nstd.set_title('n = ' + str(sizes[j]) + r', $\rho$ =' + str(correlations_1[i]))
            plt.show()
            fig.savefig('Ellipse ' + str(correlations_1[i]) + ' ' + str(sizes[j]) + '.png')


'''
Reader for LSP output xdr files (.p4's)

'''
import xdrlib as xdr
import numpy as np
import gzip # For reading .p4.gz files
import os # For path splitting
import re # For regular expression searches of the history.p4 text file

# Define a basic function for checking (and get rid of misc.py dependency)
test = lambda d,k: k in d and d[k];

#get basic dtypes
def get_int(file,N=1,forcearray=False,lowlev=False):
    # If forcearray is True, the output will always be a numpy array.

    dtype = np.dtype('>i4')
    if lowlev:
        ret=np.fromfile(file,dtype=dtype,count=N)
    else:
        s = file.read(dtype.itemsize * N)
        ret=np.fromstring(s, dtype=dtype,count=N)

    if N==1 and not forcearray:
        return ret[0];
    return ret;

def read_hist(p4dir):
    """ Read probes in the 'history.p4' or 'history.p4.gz' file contained in the given directory
    Inputs:
        p4dir: string, path to a directory containing a "history.p4" or "history.p4.gz"
    Outputs:
        values: 2D NumPy array: (N x tsteps) If there are N probes, and tsteps time steps in the simulation.
        labels: list, length N, of two-element tuples with each tuple containing ('probe name', 'units')

    Note: Reads the entire file twice into memory. Not efficient if for some reason history.p4 is a huge file.
    Note: Not thoroughly tested. Not a super-duper rigorous function (assumes a certain format, doesn't do as many checks as it should), so don't be surprised if it breaks.
    """

    ## Decide if it is a .gz or .p4
    if os.path.isfile(os.path.join(p4dir, 'history.p4')):
        histpath = os.path.join(p4dir, 'history.p4')
        myopen = open
    elif os.path.isfile(os.path.join(p4dir, 'history.p4.gz')):
        histpath = os.path.join(p4dir, 'history.p4.gz')
        myopen = gzip.open
    else:
        raise Exception("history.p4(.gz) file does not exist in directory")

    ## Read in the entire file as a string to look at the header (inefficient)
    with myopen(histpath, 'r') as f:
        s = f.read()

    ## Look at the header and extract the number of probes and their labels (including their units)
    m1 = re.search(r"^#Number of data items: ([0-9]{1,})$", s, re.MULTILINE) # e.g. reading "Number of data items: 9" and extracting 9
    if not m1:
        raise Exception("Something's wrong with the .p4 file or reader. Couldn't find a 'Number of data items' entry.")
    else:
        nitems = int(m1.group(1)) # This is the number of probes. Assume there are this many header lines, plus four more.

    labels = re.findall(r"^#[0-9]{1,}?: (.*?): (.*?)$", s, re.MULTILINE) # e.g. reading "#0: time: ns", extracting the tuple ('time', 'ns')
    del s

    ## Read in the array of probe data
    values = np.genfromtxt(histpath, skip_header = nitems + 4).swapaxes(0,1)[1:] # Fortunately, genfromtext works natively on .gz files as well

    ## Make sure we didn't screw something up royally, that we have the same number of probe labels as probe dimensions
    if np.abs(len(labels) - values.shape[0]) > 0.5:
        raise Exception("Error reading in the history.p4 file and its header.")

    return values, labels

def get_float(file,N=1,forcearray=False,lowlev=False):
    # If forcearray is True, the output will always be a numpy array.
    dtype = np.dtype('>f4')
    if lowlev:
        ret=np.fromfile(file,dtype=dtype,count=N)
    else:
        s = file.read(dtype.itemsize * N)
        ret=np.fromstring(s, dtype=dtype,count=N)

    if N==1 and not forcearray:
        return ret[0];
    return ret;

def get_str(file):
    l1 = get_int(file);
    l2 = get_int(file);
    if l1 != l2:
        print("warning, string prefixes are not equal...");
        print("{}!={}".format(l1,l2));
    size=l1;
    if l1%4:
        size+=4-l1%4;
    return xdr.Unpacker(file.read(size)).unpack_fstring(l1);

def get_list(file,fmt):
    '''makes a list out of the fmt from the LspOutput f using the format
       i for int
       f for float
       d for double
       s for string'''
    out=[]
    for i in fmt:
        if i == 'i':
            out.append(get_int(file));
        elif i == 'f' or i == 'd':
            out.append(get_float(file));
        elif i == 's':
            out.append(get_str(file));
        else:
            raise ValueError("Unexpected flag '{}'".format(i));
    return out;

def get_dict(file,fmt,keys):
    return dict(
        zip(keys, get_list(file,fmt))
    );

def get_header(file,**kw):
    '''gets the header for the .p4 file, note that this
       Advanced the file position to the end of the header.

       Returns the size of the header, and the size of the header, if the header keyword is true.
    '''
    if type(file) == str:
        #if called with a filename, recall with opened file.
        with open(file,'r') as f:
            return get_header(f,**kw);
    if test(kw, "size"):
        size = file.tell();
    header = get_dict(
        file,'iiss',
        ['dump_type','dversion', 'title','revision']);
    if header['dump_type'] == 2 or header['dump_type'] == 3:
        #this is a fields file or a scalars file.
        d = get_dict(file,'fii',['timestamp','geometry','domains']);
        header.update(d);
        #reading quantities
        n = get_int(file);
        names=[get_str(file) for i in range(n)];
        units=[get_str(file) for i in range(n)];
        #print "Now I'm done reading units!"
        header['quantities'] = zip(names,units);
    elif header['dump_type'] == 6:
        #this is a particle movie file
        d = get_dict(file,
            'iiii',
            ['geometry','sflagsx','sflagsy','sflagsz']);
        header.update(d);
        #reading params
        n = get_int(file);
        flags=[bool(get_int(file)) for i in range(n)];
        units=[get_str(file) for i in range(n)];
        labels=['q','x','y','z','ux','uy','uz','E'];
        if n == 8:
            pass;
        elif n == 7:
            labels = labels[:-1];
        elif n == 11:
            labels+=['xi','yi','zi'];
        else:
            raise NotImplementedError('Not implemented for these number of parameters:{}.'.format(n));
        header['params'] = [
            (label,unit) for (label,unit,flag) in zip(labels,units,flags) if flag
        ];
    elif header['dump_type'] == 10:
        #this is a particle extraction file:
        header['geometry'] = get_int(file);
        #reading quantities
        n = get_int(file);
        header['quantities'] = [get_str(file) for i in range(n)];
    else:
        raise ValueError('Unknown dump_type: {}'.format(header['dump_type']));
    if test(kw,'size'):
        return header, file.tell()-size;
    return header;

def read_flds(file, header, var=None, vector=True):
    if vector:
        size=3;
        readin = set();
        for i in var:#we assume none of the fields end in x
            if i[-1] == 'x' or i[-1] == 'y' or i[-1] == 'z':
                readin.add(i[:-1]);
            else:
                readin.add(i);
    else:
        size=1;
        readin = set(var);
    if not var:
        var = header;
        header = get_header(file);
        if header['dump_type'] != 2 or header['dump_type'] != 3:
            print("warning: reading something that doesn't look like a flds or sclr as such!");
    doms = [];
    qs = [i[0] for i in header['quantities']];
    for i in range(header['domains']):
        iR, jR, kR = get_int(file, N=3);
        #getting grid parameters (real coordinates)
        nI = get_int(file); Ip = get_float(file,N=nI);
        nJ = get_int(file); Jp = get_float(file,N=nJ);
        nK = get_int(file); Kp = get_float(file,N=nK);
        nAll = nI*nJ*nK;
        #self.logprint('Dimensions are {}x{}x{}={}.'.format(nI,nJ,nK,nAll));
        d={}
        #self.logprint('Making points.');
        #the way meshgrid works, it has to be in this order.
        d['y'], d['z'], d['x'] = np.vstack(np.meshgrid(Jp,Kp,Ip)).reshape(3,-1);
        for quantity in qs:
            if quantity not in readin:
                file.seek(nAll*4*size,1);
            else:
                #self.logprint('Reading in {}'.format(quantity));
                d[quantity] = get_float(file,N=nAll*size);
                if size==3:
                    data=d[quantity].reshape(nAll,size).T;
                    d[quantity+'x'],d[quantity+'y'],d[quantity+'z']= data;
                    del data, d[quantity];
        doms.append(d);
    #self.logprint('Done! Stringing together.');
    out = { k : np.concatenate([i[k] for i in doms]) for k in doms[0] };
    #converting to little endian
    for k in out:
        out[k] = out[k].astype('<f4');
    return out;

def read_flds2(fname, flds=None):
    # Re-written by Scott to be more python-syntax oriented. Reads fields files and scalar files.
    # Inputs:
    #   fname: a file path (string) for a fields or scalars .p4 file
    #   flds (optional): a list of vector field variables to read in. e.g. flds = ["E","B"] (do NOT put "Ex", etc.), or scalar variables (for a sclr file). If omitted, all fields available will be read in.
    # Outputs:
    #   doms: the data of the fields, as a list of domains with sub-content of the field arrays
    #   header: the header dict, as generated by "get_header()" subroutine
    # Changelog:
    #   2015-12-28  Fixed the header read-in problem, where it needs to seek forward.
    #   2015-02-29  Wrapped up the function such that it takes in the filename
    #               Changed file open argument to 'rb' rather than 'r' for compatibility with linux and windows binary file reads
    #   2015-12-30  Spun off this function, read_flds3, from read_flds2. Got rid of legacy read-in of data X, Y, Z
    #               Renamed this function read_flds2 to avoid extraneous version jumping.
    #   2016-01-15  Added scalar read functionality back in.
    # Issues:
    #   * Unknown if this function will work for 3D field dumps. Specifically, the order of the reshaping of fields may be wrong.
    _, ext = os.path.splitext(fname)

    if ext == '.gz':
        myopen = gzip.open # If the file is .p4.gz, open using gunzip
    else:
        myopen = open

    with myopen(fname, 'rb') as file:
        # Read in the header, and check that this dump is a vector fields file or scalars file
        header = get_header(file) # Gets the header, and advances the file cursor to after the header
        if header['dump_type'] == 2:
            size = 3 # Fields file type. Each vector has three components.
        elif header['dump_type'] == 3:
            size = 1 # Scalars file type. Each scalar has one component.
        else:
            size = None
            raise TypeError("Invalid fields file: Dump_type of header is not 2 or 3, indicating this is something other than a fields or scalars file.")

        # Check that the requested field variables are available in this file (if any is not, abort.)
        qs = [i[0] for i in header['quantities']];
        if not flds: # If no fields specified at input, read in all available fields.
            flds = qs
        else: # If fields were specified at input, check their validity.
            for fld in flds:
                if qs.count(fld) < 1:
                    raise ValueError("Invalid field request: Field '" + fld + "' is not a stored quantity in this file.")
        #print "All requested fields are available! Reading domains."

        flds_set = set(flds) # A python set can be an easier type than a list when comparing elements

        doms = []

        for i in range(header['domains']): # Iterate over domains
            #print "Domain: " + str(i + 1) + " of " + str(header['domains'])
            iR, jR, kR = get_int(file, N=3)
            #getting grid parameters (real coordinates)
            nI = get_int(file)
            Ip = get_float(file,N=nI,forcearray=True)
            nJ = get_int(file)
            Jp = get_float(file,N=nJ,forcearray=True)
            nK = get_int(file)
            Kp = get_float(file,N=nK,forcearray=True)
            nAll = nI*nJ*nK
            #self.logprint('Dimensions are {}x{}x{}={}.'.format(nI,nJ,nK,nAll));
            d = {} # The domain data storage unit
            #print('Making points.')
            #the way meshgrid works, it has to be in this order.
            d['xgv'] = Ip
            d['ygv'] = Jp
            d['zgv'] = Kp

            for quantity in qs:
                if quantity not in flds_set: # Skip file cursor past unwanted fields/quantities
                    file.seek(nAll*4*size,1)
                else: # Read in the desired fields
                    #self.logprint('Reading in {}'.format(quantity));
                    # COULD PROBABLY DO THIS IN ONE STEP, BUT WHAT'S THE POINT?
                    fld_raw = get_float(file,N=nAll*size)
                    data=fld_raw.reshape(nAll,size).T
                    if size == 1:
                        d[quantity] = data.reshape(nK,nJ,nI)
                    else:
                        fld_x,fld_y,fld_z= data
                        d[quantity+'x'] = fld_x.reshape(nK,nJ,nI) # CHECK THE ORDER FOR 3D!!!
                        d[quantity+'y'] = fld_y.reshape(nK,nJ,nI)
                        d[quantity+'z'] = fld_z.reshape(nK,nJ,nI)
                    del data, fld_raw

            doms.append(d)

    return doms, header

def read_sclr(file,header,var):
    return read_flds(file, header, var, False);

def iseof(file):
    c = file.tell();
    file.read(1);
    if file.tell() == c:
        return True;
    file.seek(c);

def read_movie(file, header):
    params,_  = zip(*header['params']);
    nparams = len(params);
    pbytes = (nparams+1)*4;
    frames=[];
    pos0 = file.tell();
    while not iseof(file):
        d=get_dict(file, 'fii',['t','step','pnum']);
        d['pos']=file.tell();
        file.seek(d['pnum']*pbytes,1);
        frames.append(d);
    for i,d in enumerate(frames):
        N = d['pnum'];
        lt = [('ip','>i4')] + list(zip(params,['>f4']*nparams));
        file.seek(d['pos']);
        arr=np.frombuffer(file.read(N*4*len(lt)),dtype=np.dtype(lt),count=N);
        arr.flags.writeable = True;
        frames[i].update({'data':arr});
        del frames[i]['pos'];
    return frames;

def read_movie2(fname):
    """ Wrapper for read_movie() that takes a filename as input. Gzipped .p4.gz allowed.
    Inputs:
        fname: string, path to a file, e.g. fname = 'pext1.p4'
    Outputs:
        frames: The output from read_movie. A NumPy array with multiple records.
    """

    _, ext = os.path.splitext(fname)

    if ext == '.gz':
        myopen = gzip.open # If the file is .p4.gz, open using gunzip
    else:
        myopen = open

    with myopen(fname, 'rb') as file:
        header = get_header(file)
        frames = read_movie(file, header)
    return frames

def read_pext(file, header, lowlev=False):
    nparams = len(header['quantities'])
    params = ['t','q','x','y','z','ux','uy','uz']
    if nparams == 9:
        params+=['E']
    elif nparams == 11:
        params+=['xi','yi','zi']
    elif nparams == 12:
        params+=['E','xi','yi','zi']
    #it's just floats here on out
    dt = list(zip(params, ['>f4']*len(params)))
    if lowlev: # more efficient, but does not work with .p4.gz file handles
        out = np.fromfile(file,dtype=dt,count=-1)
    else: # works with all file handles
        s = file.read()
        out=np.fromstring(s, dtype=dt,count=-1)
    return out

def read_pext2(fname):
    """ Wrapper for read_pext() that takes a filename as input. Gzipped .p4.gz allowed.
    Inputs:
        fname: string, path to a file, e.g. fname = 'pext1.p4'
    Outputs:
        out: The output from read_pext. A NumPy array with multiple records.
    """

    _, ext = os.path.splitext(fname)

    if ext == '.gz':
        myopen = gzip.open # If the file is .p4.gz, open using gunzip
    else:
        myopen = open

    with myopen(fname, 'rb') as file:
        header = get_header(file)
        out = read_pext(file, header)
    return out

def read(fname,**kw):
    '''reads an lsp output file into an h5 file.'''
    with open(fname,'rb') as file:
        header = get_header(file);
        if not test(kw, 'var'):
            var=[i[0] for i in header['quantities']];
        else:
            var=kw['var'];
        readers = {#overkill
            2: lambda: read_flds(file,header,var),
            3: lambda: read_sclr(file,header,var),
            10:lambda: read_pext(file,header)
        };
        try:
            d = readers[header['dump_type']]();
        except KeyError:
            d = None;
            raise NotImplementedError("Other file types not implemented yet!");
    return d;

def stitch2D(doms, fld_id, divsp=1, splitax="z"):
    """ Stitch a simple 2D lsp sim together, where we have N domains all built up along the Z dimension, and a flat Y dimension.
      Such as with Chris' 2D sims of the back-reflected plasma
      Inputs:
        doms: an output of fld_reader3, which is a list of N items, containing each the fields data for that domain
        fld_id: the string identifying the field component, e.g. "Ex" or "Bz"
        divsp: integer, divisor by which to reduce the spatial resolution (e.g. divsp = 2 reduces field dimensions from 300x200 to 150x100)
      Outputs:
        fld: A 2D array which contains the field component data
     xgv, zgv: 1D arrays of the X or Z coordinate along the axis, in centimeters
     TODO: Make this generalized to 3D
    """

    if splitax == "z": # ZSPLIT lsp option
        fld_cat = np.squeeze(doms[0][fld_id])
        xgv = doms[0]['xgv'][::divsp]
        zgv_cat = doms[0]['zgv']

        for i in range(1,len(doms)): # Domains are concatenated along the z dimension
            fld_tmp = np.squeeze(doms[i][fld_id])[1:,:]
            fld_cat = np.concatenate((fld_cat,fld_tmp),0)

            zgv_tmp = doms[i]['zgv'][1:]
            zgv_cat = np.concatenate((zgv_cat,zgv_tmp),0)

        zgv = zgv_cat[::divsp]
        fld = fld_cat[::divsp,::divsp]
    elif splitax == "x": # XSPLIT lsp option
        fld_cat = np.squeeze(doms[0][fld_id])
        xgv_cat = doms[0]['xgv']
        zgv = doms[0]['zgv'][::divsp]
        for i in range(1,len(doms)): # Domains are concatenated along the x dimension
            fld_tmp = np.squeeze(doms[i][fld_id])[:,1:]
            fld_cat = np.concatenate((fld_cat,fld_tmp),1)

            xgv_tmp = doms[i]['xgv'][1:]
            xgv_cat = np.concatenate((xgv_cat,xgv_tmp),0)

        xgv = xgv_cat[::divsp]
        fld = fld_cat[::divsp,::divsp]
    else:
        raise Exception("Unsupported split axis: " + splitax)
    return fld, xgv, zgv

def pseek(file):
    # Print out the current file seek position ("file.tell")
    print("Current file seek position: " + str(file.tell()))


"This demo illustrates basic plotting."

# Copyright (C) 2007-2008 Anders Logg
#
# This file is part of DOLFIN.
#
# DOLFIN is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# DOLFIN is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with DOLFIN. If not, see <http://www.gnu.org/licenses/>.
#
# First added:  2007-05-29
# Last changed: 2009-10-15

from dolfin import *
from math import sqrt

import sys

# Read and plot mesh from file
mesh = Mesh("dolfin-2.xml.gz")
mesh.order()

# Decide which demos to run
try:
    demos = [int(sys.argv[-1])]
except:
    demos = [0, 1, 2, 3]

# Have some fun with the mesh
if 0 in demos:

    R = 0.15
    H = 0.025
    X = 0.3
    Y = 0.4
    dX = H
    dY = 1.5*H
    coordinates = mesh.coordinates()
    original = coordinates.copy()

    for i in xrange(100):

        if X < H or X > 1.0 - H:
            dX = -dX
        if Y < H or Y > 1.0 - H:
            dY = -dY
        X += dX
        Y += dY

        for j in xrange(mesh.num_vertices()):
            x, y = coordinates[j]
            r = sqrt((x - X)**2 + (y - Y)**2)
            if r < R:
                coordinates[j] = [X + (r/R)**2*(x - X), Y + (r/R)**2*(y - Y)]

        plot(mesh)

        for j in xrange(mesh.num_vertices()):
            coordinates[j] = original[j]

# Plot scalar function
if 1 in demos:
    V = FunctionSpace(mesh, "CG", 1)
    f = Expression("t * 100 * exp(-10.0 * (pow(x[0] - t, 2) + pow(x[1] - t, 2)))", element=V.ufl_element(), t=0.0)
    for i in range(100):
        f.t += 0.01
        plot(f, mesh=mesh, rescale=True, title="Scalar function")

# Plot vector function
if 2 in demos:
    mesh = UnitSquare(16, 16)
    V = VectorFunctionSpace(mesh, "CG", 1)
    f = Expression(("-(x[1] - t)*exp(-10.0*(pow(x[0] - t, 2) + pow(x[1] - t, 2)))",\
                  " (x[0] - t)*exp(-10.0*(pow(x[0] - t, 2) + pow(x[1] - t, 2)))"), \
                   element=V.ufl_element(), t=0.0)
    for i in range(200):
        f.t += 0.005
        plot(f, mesh=mesh, rescale=True, title="Vector function")

if 3 in demos:
    import numpy
    mesh = UnitSquare(10, 10)
    V = VectorFunctionSpace(mesh, "CG", 1)
    f = Expression(("-(x[1] - t)*exp(-10.0*(pow(x[0] - t, 2) + pow(x[1] - t, 2)))",\
                    " (x[0] - t)*exp(-10.0*(pow(x[0] - t, 2) + pow(x[1] - t, 2)))"),\
                   element=V.ufl_element(), t=0.0)

    pts = numpy.array([
        [.24, .24],
        [.24, .74],
        [.74, .24],
        [.74, .74]
        ], dtype='d')

    for i in range(150):
        f.t += 0.005
        plot(f, mesh=mesh, eval_pts=pts, rescale=True, title="Vector function")


# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models, migrations

class Migration(migrations.Migration):

    dependencies = [
        ('trips', '0027_stoptime'),
    ]

    operations = [
        migrations.AlterModelOptions(
            name='stoptime',
            options={'verbose_name': 'Stop Time', 'verbose_name_plural': 'Stop Times'},
        ),
    ]


'''
The following functions have problems that keep them from
completing the task that they have to do.

All the problems are either Logical or Syntactical errors with READ/WRITE.
Focus on the reading and writing and find the problems with the READ/WRITE.

The number of errors are as follows:
readSingle: 3
readAll: 3
writeStuff: 3
writeDouble: 3
writeAppend: 3
'''

'''
This function takes a fileName and reads then prints the first
line of the file.
'''
def readSingle(fileName):
    f = open("fileName", 'r')
    string = f.readline()

    #Prints the read data
    return string
    print(string.strip())
    readSingle("fileOne.txt")
    f.close()

'''
This function takes a fileName and reads and prints ALL of the lines of the
file.
'''
def readAll(fileName):
    file = open(fileName, 'r')
    stringList = fileName.readline()
    fileName.close()

    #Prints all the read data
    x = 0
    while(x < len(stringList)):
        print(stringList[x].strip())
        x += 1
    return stringList

readAll("fileOne.txt")

'''
This function takes a fileName and some content and writes the content on
the file.
'''
def writeStuff(fileName, content):
    f = open("file", 'r')
    f.write(content)
    f.close()
    print("DONE")
    return

writeStuff("fileTwo.txt", "This is for the third function.")

'''
This function takes a fileName and two pieces of content and writes them in the
file.
'''
def writeDouble(fileName, content, contentTwo):
    f.close("fileName", 'w')
    f.wrte(content)
    f.write(contentTwo)
    print("DONE")
    return

    fileName.writeDouble("fileThree.txt", "This is for the forth function.",
            "This is the second sentence.")

'''
This function takes a fileName and content and appends the content to the end
of the file.
'''
def writeAppend(fileName, append):
    f = open(fileName, '')
    f.write(append)
    f.close()
    print("DONE")
    return

writeAppend("fileThree.txt", "This should be appended to the end.")


"""
Utility methods to help in encryption.
"""
lowerLetters = 'abcdefghijklmnopqrstuvwxyz'
upperLetters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'

def alphabet_position(char):
    """
    Return the alphabet position.
    Returns -1 for all invalid input.
    """
    if type(char) != type(''):
        return -1
    if len(char) != 1:
        return -1
    if char.isalpha():
        return lowerLetters.find(char.lower())
    return -1

def rotate_character(char, rot):
    """
    Return the character shifted by rot amount.
    char must be a single character and rot must be a positive integer.
    char is returned AS IS if any errors, including if it is not a character
    """
    if type(char) != type(''):
        return char
    if type(rot) != type(1):
        return char

    if len(char) != 1:
        return char
    if not char.isalpha():
        return char
    letters = lowerLetters
    if char.isupper():
        letters = upperLetters

    pos = letters.find(char)
    pos += rot
    pos = pos % 26

    return letters[pos]


from django.shortcuts import render
from django.contrib.auth.decorators import login_required
from django.db import transaction
from .forms import UserForm,ProfileForm
from django.contrib.auth import logout

def home(request):
    return render(request, 'home.html')

@login_required
@transaction.atomic
def update_profile(request):
    if request.method == 'POST':
        user_form = UserForm(request.POST, instance=request.user)
        profile_form = ProfileForm(request.POST, instance=request.user.profile)
        if user_form.is_valid() and profile_form.is_valid():
            user_form.save()
            profile_form.save()
            return HttpResponseRedirect('/')
        #else:
        #    messages.error(request, _('Please correct the error below.'))
    else:
        user_form = UserForm(instance=request.user)
        profile_form = ProfileForm(instance=request.user.profile)
    return render(request, 'profile.html', {
        'user_form': user_form,
        'profile_form': profile_form
    })

def Logout(request):
    logout()
    return HttpResponseRedirect('/')


__author__ = 'Farhan'

from random import randint
from bisect import bisect_left

from numpy import *
import matplotlib.pyplot as plt

from process_GTEx import Process
from pylab import *

class Histogram:
    '''
        Create a histogram using rankings from output file. If additional parameters are set,
        create a histogram by randomly selecting individuals
            @input: output_file gives rankings and number of individuals per gene
            tissue_file gives tissue data to generate random histogram
    '''

    def __init__(self, output_file, tissue):
        self.file = output_file
        self.rankings = []
        self.gene_number = []
        self.isHigh = []
        self.randRankings = []

        self.genes_new = tissue.genes
        self.data_new = tissue.data

    # process ranking/gene data
    def processData(self):

        for l in self.file:
            w = l.split()
            if len(w) > 0:
                self.gene_number.append(w[0])
                # append ranking (4th element) to rankings array
                if w[4] > 0:
                    self.rankings.append(w[4])

                # ranking is for k_high
                if w[2] > 0:
                    self.isHigh.append(True)
                # ranking is for k_low
                else:
                    self.isHigh.append(False)
       # print self.rankings
        self.rankings = array(map(float, self.rankings))

    def createHistogram(self):
        plt.hist(self.rankings, bins = 10)
        #savefig('data_hist.png')

        plt.show()

    def createRandomHistogram(self):
        random_rankings = []
        counter = -1
        for gene in self.gene_number:
            counter = counter + 1
            index = nonzero(self.genes_new == gene)
            expr_data_gene = self.data_new[index]
            expr_data_gene = expr_data_gene[0]
            # generate random number between 0 and last index in expression data
            rand_num = randint(0, len(expr_data_gene) - 1)

            # append ranking
            self.randRankings.append(rand_num)

        plt.hist(self.randRankings, bins = 10)
        #savefig('rand_hist.png')
        plt.show()

    def index(self, a, x):
        'Locate the leftmost value exactly equal to x using binary search'
        i = bisect_left(a, x)
        if i != len(a) and a[i] == x:
            return i
        else:
            return -1

    def main(self):
        self.processData()
        self.createHistogram()


import unittest

from selection import selection_sort

class SelectionUnittest(unittest.TestCase):
    @staticmethod
    def checkValid(items, reverse=False):
        n = len(items)
        for i in xrange(0, n-1):
            if not reverse and items[i] > items[i+1]:
                return False
            elif reverse and items[i] < items[i+1]:
                return False
        return True

    def test_Sort(self):
        items = [1, 5, 4, 2, 8, 9, 10]
        selection_sort(items)
        self.assertEquals(True, self.checkValid(items))

    def test_ReverseSort(self):
        items = [1, 5, 4, 2, 8, 9, 10]
        selection_sort(items, reverse=True)
        self.assertEquals(True, self.checkValid(items, reverse=True))

if __name__ == "__main__":
    unittest.main()


from Networks import Network
from database_loader import load_data
from Networks import sigmoid, reLu, tanh
from time import time

[trainingData, validationData, testData] = load_data()
trainingData = list(trainingData)
validationData = list(validationData)
testData = list(testData)

accuracy = 0
cont = 0
lamndaList = [0, 1, 0, 1, 0, 1]
learningParameter = [2, 2, 3, 3, 4, 4]
contLearning = 0
while contLearning < len(learningParameter):

    net = Network([784, 30, 10], [None, tanh, tanh])  # La reLu e' baggata!!
    startingTime = time()
    net.training(trainingData, 20, 50, learningParameter[contLearning], lamndaList[contLearning]/len(trainingData), validationData)
    finalTime = time() - startingTime
    contLearning += 1

    print("The testing accuracy is equal to: " + str(net.evaluate(testData)/len(testData)*100))
    print("The requested training time has been: ", finalTime)

#b = net.backprop([0.05, 0.1], [0.01, 0.99])
#print(b)

#net = Network([2, 3, 2], [None, sigmoid, sigmoid])
#net.updateWeights([(np.array([[0.05], [0.1]]), np.array([[0.01], [0.99]]))], 0.5, 0)

# print(net.evaluate([([0.05, 0.1], [0, 1])]))
#trainingDataset = [([0.05, 0.1],[0.01, 0.99])]
#validationData = [([0.05, 0.1],[0, 1])]
#print(net.training(trainingDataset, 5, 1, 0.5, 0, validationData))


from asgiref.sync import async_to_sync
from channels.generic.websocket import WebsocketConsumer
import json

from chat.models import *

class ChatConsumer(WebsocketConsumer):
    def connect(self):
        self.room_name = self.scope['url_route']['kwargs']['room_name']
        self.room_group_name = 'chat_%s' % self.room_name

        # Join room group
        async_to_sync(self.channel_layer.group_add)(
            self.room_group_name,
            self.channel_name
        )

        self.accept()

    def disconnect(self, close_code):
        # Leave room group
        async_to_sync(self.channel_layer.group_discard)(
            self.room_group_name,
            self.channel_name
        )

    # Receive message from WebSocket
    def receive(self, text_data):
        text_data_json = json.loads(text_data)
        print(text_data)
        chat=Chat.objects.get(id=text_data_json['chat'])
        user=User.objects.get(id=int(text_data_json['user']))
        message = text_data_json['message']
        create_msg=Message(chat=chat,autor=user,message=message)
        create_msg.save()

        # Send message to room group
        async_to_sync(self.channel_layer.group_send)(
            self.room_group_name,
            {
                'type': 'chat_message',
                'message':text_data_json['message'] ,
                'author':int(text_data_json['user'])
            }
        )

    # Receive message from room group
    def chat_message(self, event):
        message = event['message']
        author_row= event['author']
        author=User.objects.get(id=int(author_row)).username

         # Send message to WebSocket
        self.send(text_data=json.dumps({
            'message': message,
            'author':author,
            'author_id':author_row
        }))


# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models, migrations

class Migration(migrations.Migration):

    dependencies = [
    ]

    operations = [
        migrations.CreateModel(
            name='Refuel',
            fields=[
                ('id', models.AutoField(auto_created=True, serialize=False, verbose_name='ID', primary_key=True)),
                ('line', models.CharField(max_length=23)),
                ('consumption', models.PositiveIntegerField()),
                ('refuel_time', models.DateTimeField(auto_now_add=True)),
            ],
        ),
        migrations.CreateModel(
            name='User',
            fields=[
                ('id', models.AutoField(auto_created=True, serialize=False, verbose_name='ID', primary_key=True)),
                ('password', models.CharField(verbose_name='password', max_length=128)),
                ('last_login', models.DateTimeField(blank=True, null=True, verbose_name='last login')),
                ('tag_id', models.CharField(unique=True, max_length=50)),
                ('name', models.CharField(max_length=50)),
                ('email', models.EmailField(unique=True, max_length=254)),
                ('plate', models.CharField(max_length=30)),
                ('bank', models.CharField(max_length=30)),
                ('tel', models.CharField(max_length=50)),
                ('is_active', models.BooleanField(default=False)),
                ('is_admin', models.BooleanField(default=False)),
                ('date_joined', models.DateTimeField(auto_now_add=True)),
            ],
            options={
                'abstract': False,
            },
        ),
        migrations.AddField(
            model_name='refuel',
            name='user',
            field=models.ForeignKey(related_name='refuels', to='carreg_app.User'),
        ),
    ]