"""Example of an instruction paired with agent actions."""
from __future__ import annotations

from typing import TYPE_CHECKING

import numpy as np

from agent import util
from agent.data import gameplay_action
from agent.data import partial_observation
from agent.environment import agent_actions
from agent.environment import environment_objects
from agent.environment import state_delta
from agent.environment import terrain
from agent.environment import util as environment_util

if TYPE_CHECKING:
    from typing import Dict, List, Optional, Set, Tuple
    from agent.data import cereal_bar_game
    from agent.environment import card
    from agent.environment import position

def get_changed_cards_along_trajectory(state_sequence: List[state_delta.StateDelta]) -> List[card.Card]:
    unique_visitation_sequence: List[Tuple[position.Position, bool]] = list()
    original_position: position.Position = state_sequence[0].follower.get_position()
    original_card_positions: List[position.Position] = [original_card.get_position() for original_card in
                                                        state_sequence[0].cards]
    has_moved: bool = False

    for i, delta in enumerate(state_sequence[1:]):
        follower_position = delta.follower.get_position()
        if follower_position == original_position and not has_moved:
            continue
        has_moved = True
        # Only keep track of the position if it wasn't the last one it was in
        if not unique_visitation_sequence or unique_visitation_sequence[-1][0] != follower_position:
            # Also check the previous state information to see if there was a card in that location when it
            # stepped there (check the previous information because otherwise this won't count cards that
            # disappeared when the agent took a step). Also can't just check the original state because
            # the agent could return to a location that it had removed a card from.

            # this is previous because i will be starting from the 1th example.
            unique_visitation_sequence.append(
                (follower_position,
                 follower_position in [state_card.get_position() for state_card in state_sequence[i].cards]))

    # Filter by states where there was a card touched, and specifically if it was a card that was only in the
    # original state (if a card appeared later, the instruction couldn't have referred to it, so it's not a
    # required card to touch).
    unique_card_visitations: List[position.Position] = \
        [pos[0] for pos in unique_visitation_sequence if pos[1] and pos[0] in original_card_positions]

    # Iterates through all unique positions reached which have a card, and counts how many times they have
    # been reached.
    card_pos_visitation_dict: Dict[position.Position, int] = \
        {pos2: len([pos for pos in unique_card_visitations if pos == pos2])
         for pos2 in set(unique_card_visitations)}

    # Filters this by odd visitations -- meaning that the selection of the card should have changed by the end
    # of the instruction.
    reached_card_positions: Set[position.Position] = \
        {pos for pos, count in card_pos_visitation_dict.items() if count % 2 == 1}

    reached_cards: List[card.Card] = list()
    for initial_card in state_sequence[0].cards:
        if initial_card.get_position() in reached_card_positions:
            reached_cards.append(initial_card)

    return reached_cards

class InstructionExample:
    def __init__(self,
                 instruction: List[str],
                 target_action_sequence: List[Tuple[state_delta.StateDelta, agent_actions.AgentAction,
                                                    partial_observation.PartialObservation]],
                 paired_game: cereal_bar_game.CerealBarGame,
                 example_idx_in_interaction: int,
                 leader_actions: List[List[gameplay_action.GameplayAction]],
                 index_of_first_leader_turn: int,
                 sets_made_during_instruction: List[Tuple[List[card.Card], List[card.Card]]],
                 number_of_steps_in_first_turn: int,
                 number_of_instructions_when_starting: int):
        self._instruction: List[str] = instruction
        self._target_action_sequence: List[
            Tuple[state_delta.StateDelta, agent_actions.AgentAction,
                  partial_observation.PartialObservation]] = target_action_sequence
        self._paired_game: cereal_bar_game.CerealBarGame = paired_game
        self._example_idx_in_interaction: int = example_idx_in_interaction
        self._leader_actions: List[List[gameplay_action.GameplayAction]] = leader_actions
        self._index_of_first_leader_turn: int = index_of_first_leader_turn
        self._sets_made_during_instruction: List[Tuple[List[card.Card], List[card.Card]]] = sets_made_during_instruction
        self._number_of_steps_in_first_turn: int = number_of_steps_in_first_turn
        self._number_of_instructions_when_starting: int = number_of_instructions_when_starting

        self._static_indices = None

    def hash_representation(self) -> str:
        str_hash: str = self.get_id() + '/' + \
                        ' '.join([str(action) for action in self.get_action_sequence()]) + '/' + \
                        str(self.get_initial_state().follower.get_position()) + '/' + \
                        str(self.get_initial_state().follower.get_rotation()) + '/'
        for initial_card in self.get_initial_state().cards:
            str_hash += str(initial_card) + '/'
        return str_hash

    def get_seed(self) -> int:
        return self._paired_game.get_seed()

    def get_id(self) -> str:
        return self._paired_game.get_id() + '-' + str(self._example_idx_in_interaction)

    def get_instruction(self) -> List[str]:
        return self._instruction

    def get_action_sequence(self) -> List[str]:
        return [str(x[1]) for x in self._target_action_sequence]

    def get_state_deltas(self) -> List[state_delta.StateDelta]:
        return [x[0] for x in self._target_action_sequence]

    def get_initial_cards(self) -> List[card.Card]:
        return self.get_initial_state().cards

    def get_initial_state(self) -> state_delta.StateDelta:
        return self._target_action_sequence[0][0]

    def get_static_indices(self, state_representation) -> np.array:
        if self._static_indices is None:
            self._static_indices = state_representation.static_indices(self)
        return self._static_indices

    def get_objects(self) -> List[environment_objects.EnvironmentObject]:
        return self._paired_game.get_objects()

    def get_obstacle_positions(self) -> List[position.Position]:
        obstacle_positions: List[position.Position] = list()
        for ter, hexp in self.get_hexes():
            if ter in terrain.OBSTACLE_TERRAINS:
                obstacle_positions.append(hexp)
        for obj in self.get_objects():
            assert obj.get_type() != environment_objects.ObjectType.CARD
            obstacle_positions.append(obj.get_position())
        return obstacle_positions

    def get_hexes(self) -> List[Tuple[terrain.Terrain, position.Position]]:
        return self._paired_game.get_hexes()

    def get_visited_positions(self, include_start: bool = True, start_idx: int = 0) -> List[position.Position]:
        """Gets an ordered list of positions visited by the agent along the gold trajectory.

        Args:
            include_start: Whether to include the start position in the list.
            start_idx: The action index to start from.

        """
        if include_start:
            return list(set([delta.follower.get_position() for delta in self.get_state_deltas()[start_idx:]]))

        state: state_delta.StateDelta = self.get_state_deltas()[start_idx]
        original_position: position.Position = state.follower.get_position()
        pos_list: List[position.Position] = []
        has_moved: bool = False
        for delta in self.get_state_deltas()[start_idx:]:
            current_position = delta.follower.get_position()
            if current_position == original_position and not has_moved:
                continue
            else:
                has_moved = True
            pos_list.append(current_position)

        return list(set(pos_list))

    def get_first_partial_observation(self) -> partial_observation.PartialObservation:
        return self._target_action_sequence[0][2]

    def get_partial_observations(self) -> List[partial_observation.PartialObservation]:
        return [x[2] for x in self._target_action_sequence]

    def get_leader_actions(self,
                           limit_to_instruction: bool = True,
                           use_all_actions: bool = False) -> List[List[gameplay_action.GameplayAction]]:
        """Returns the leader actions aligning to this instruction.

        Args:
            limit_to_instruction: Whether to only return actions that occurred during the execution of this instruction.
            use_all_actions: TODO: What does this mean?
        """
        if limit_to_instruction:
            return self._leader_actions
        actions_from_game: List[List[gameplay_action.GameplayAction]] = self._paired_game.get_leader_actions(
            first_turn=self._index_of_first_leader_turn,
            use_all_actions=use_all_actions)
        if use_all_actions:
            return actions_from_game

        for action1, action2 in zip(self._leader_actions, actions_from_game):
            if action1 != action2:
                raise ValueError('Actions not the same!')
        return actions_from_game

    def get_expected_sets(self) -> List[Tuple[List[card.Card], List[card.Card]]]:
        """Gets the sets that should be made during this instruction and afterwards."""
        return self._paired_game.get_expected_sets(first_instruction=self._example_idx_in_interaction)

    def get_number_of_moves_in_first_turn(self) -> int:
        return self._number_of_steps_in_first_turn

    def get_number_of_instructions_when_starting(self) -> int:
        return self._number_of_instructions_when_starting

    def get_final_state(self) -> state_delta.StateDelta:
        if self._target_action_sequence[-1][1] != agent_actions.AgentAction.STOP:
            raise ValueError('Final action was not STOP')

        # The final state is the prior state for the last action, which is the STOP action.
        return self._target_action_sequence[-1][0]

    def get_touched_cards(self,
                          start_idx: int = 0,
                          include_start_position: bool = False,
                          allow_duplicates: bool = True) -> List[card.Card]:
        """Gets all cards touched along the gold trajectory.

        Args:
            start_idx: The first index along the trajectory to consider when computing which cards were touched.
            include_start_position: Whether to include any cards that are in the initial position.
            allow_duplicates: TODO: Not sure what this setting does actually.

        """
        if allow_duplicates:
            state: state_delta.StateDelta = self.get_state_deltas()[start_idx]
            original_card_positions: List[position.Position] = [state_card.get_position() for state_card in state.cards]

            agent_positions: List[position.Position] = self.get_visited_positions(
                include_start=include_start_position, start_idx=start_idx)
            reached_card_positions = set(original_card_positions) & set(agent_positions)

            reached_cards: List[card.Card] = list()
            for state_card in state.cards:
                if state_card.get_position() in reached_card_positions:
                    reached_cards.append(state_card)
            return reached_cards
        else:
            return get_changed_cards_along_trajectory(self.get_state_deltas())

    def get_card_scores(self) -> Dict[position.Position, float]:
        # TODO: Partial observability
        scores: Dict[position.Position, float] = dict()
        for initial_card in self.get_initial_cards():
            scores[initial_card.get_position()] = 1. if initial_card in self.get_touched_cards() else 0.
        return scores

    def get_correct_trajectory_distribution(self,
                                            weight_by_time: bool,
                                            full_observability: bool = True,
                                            observation: partial_observation.PartialObservation = None,
                                            observed_positions: Set[position.Position] = None) -> np.array:

        if full_observability and observation:
            raise ValueError('If using full observability, do not need to provide an observation object.')

        distribution: np.array = np.zeros((environment_util.ENVIRONMENT_WIDTH, environment_util.ENVIRONMENT_DEPTH))
        if weight_by_time:
            path: List[position.Position] = [delta.follower.get_position() for delta in self.get_state_deltas()]

            if not full_observability:
                # Remove things from the path that were not observed at this point.
                if not observed_positions:
                    observed_positions = observation.currently_observed_positions()
                new_path: List[position.Position] = list()
                for pos in path:
                    if pos in observed_positions:
                        new_path.append(pos)
                path = new_path

            # The weight is one over the path length, rather than the number of unique locations.
            # The weight is zero if the path is empty (e.g., if the agent is off the path during inference).
            if len(path) == 0:
                weight_per_hex = 0.
            else:
                weight_per_hex: float = 1. / len(path)

            for location in path:
                # Add the weight rather than setting it.
                distribution[location.x][location.y] += weight_per_hex
        else:
            correct_trajectory: List[position.Position] = self.get_visited_positions()

            if not full_observability:
                # Limit by positions visible _right now_. The distribution won't include hexes that aren't currently
                # visible.
                if not observed_positions:
                    observed_positions = observation.currently_observed_positions()
                correct_trajectory = list(set(correct_trajectory) & observed_positions)

            if len(correct_trajectory) == 0:
                weight_per_hex = 0.
            else:
                weight_per_hex: float = 1. / len(correct_trajectory)

            for location in correct_trajectory:
                distribution[location.x][location.y] = weight_per_hex

        return distribution

def construct_game_examples(game: cereal_bar_game.CerealBarGame, max_instruction_index: int):
    # First, segment the actions by instruction.
    all_sets: List[Tuple[int, List[card.Card], List[card.Card]]] = list()

    # Segments actions into instructions
    segmented_actions: List[List[gameplay_action.GameplayAction]] = list(list())
    current_action_sequence: List[gameplay_action.GameplayAction] = list()
    instructions: List[gameplay_action.InstructionAction] = list()

    for action in game.get_actions():
        current_action_sequence.append(action)
        if isinstance(action, gameplay_action.FinishCommandAction):
            segmented_actions.append(current_action_sequence)
            current_action_sequence = list()
        elif isinstance(action, gameplay_action.InstructionAction) and action.completed():
            instructions.append(action)

    assert len(instructions) == len(segmented_actions) or len(instructions) == len(segmented_actions) - 1

    game_examples: List[InstructionExample] = list()
    current_leader_turn_idx: int = 0

    num_steps_remaining: int = 10
    buffer_size: int = 0

    current_observation: partial_observation.PartialObservation = game.get_first_partial_observation()

    for i, segmented_seq in enumerate(segmented_actions):
        follower_action_sequence: List[Tuple[state_delta.StateDelta, agent_actions.AgentAction,
                                             partial_observation.PartialObservation]] = list()
        current_leader_actions_dict: Dict[int, List[gameplay_action.GameplayAction]] = dict()
        current_sets: List[Tuple[List[card.Card], List[card.Card]]] = list()

        following: bool = False
        initial_num_steps_remaining = num_steps_remaining
        initial_buffer_size = buffer_size

        for action in segmented_seq:
            if isinstance(action, gameplay_action.MovementAction):
                if action.get_agent() == environment_objects.ObjectType.FOLLOWER:
                    follower_action_sequence.append((action.get_prior_game_info(), action.get_action(),
                                                     current_observation))

                    if not following:
                        initial_num_steps_remaining = num_steps_remaining
                        initial_buffer_size = buffer_size

                    following = True
                    num_steps_remaining -= 1
                else:
                    if following:
                        if current_leader_turn_idx not in current_leader_actions_dict:
                            current_leader_actions_dict[current_leader_turn_idx] = list()
                        current_leader_actions_dict[current_leader_turn_idx].append(action)

                set_result: Optional[Tuple[List[card.Card], List[card.Card]]] = \
                    state_delta.set_difference(action.get_prior_game_info().cards,
                                               action.get_posterior_game_info().cards)
                if set_result:
                    set_instr_idx = i if following else i - 1
                    all_sets.append((set_instr_idx, set_result[0], set_result[1]))
                    if following:
                        current_sets.append(set_result)

                # Update the observability
                current_observation = partial_observation.update_observation(current_observation,
                                                                             action.get_posterior_game_info())

            elif isinstance(action, gameplay_action.EndTurnAction):
                if action.get_agent() == environment_objects.ObjectType.FOLLOWER:
                    num_steps_remaining = 10
                else:
                    if following and current_leader_turn_idx not in current_leader_actions_dict:
                        current_leader_actions_dict[current_leader_turn_idx] = list()
                    current_leader_turn_idx += 1
            elif isinstance(action, gameplay_action.InstructionAction) and action.completed():
                if following:
                    if current_leader_turn_idx not in current_leader_actions_dict:
                        current_leader_actions_dict[current_leader_turn_idx] = list()
                    current_leader_actions_dict[current_leader_turn_idx].append(action)
                buffer_size += 1
            elif isinstance(action, gameplay_action.FinishCommandAction):
                if not following:
                    initial_buffer_size = buffer_size
                buffer_size -= 1
                follower_action_sequence.append((action.get_prior_game_info(), agent_actions.AgentAction.STOP,
                                                 current_observation))

        leader_actions_to_pass: List[List[gameplay_action.GameplayAction]] = list()
        if current_leader_actions_dict:
            first_leader_turn_idx: int = min(current_leader_actions_dict.keys())
            for j in range(first_leader_turn_idx, max(current_leader_actions_dict.keys()) + 1):
                if j not in current_leader_actions_dict:
                    leader_actions_to_pass.append(list())
                else:
                    leader_actions_to_pass.append((current_leader_actions_dict[j]))
        else:
            first_leader_turn_idx: int = current_leader_turn_idx

        from_game_actions = \
            game.get_leader_actions()[first_leader_turn_idx:first_leader_turn_idx + len(leader_actions_to_pass)]
        assert from_game_actions == leader_actions_to_pass

        example: InstructionExample = \
            InstructionExample(instructions[i].get_tokenized_instruction(),
                               follower_action_sequence,
                               game,
                               i,
                               leader_actions_to_pass,
                               first_leader_turn_idx,
                               current_sets,
                               initial_num_steps_remaining,
                               initial_buffer_size)

        game_examples.append(example)

        if len(game_examples) >= max_instruction_index >= 0:
            break

    return game_examples, all_sets

def construct_examples(games: Dict[str, cereal_bar_game.CerealBarGame],
                       max_instruction_index: int) -> Dict[str, InstructionExample]:
    examples: Dict[str, InstructionExample] = dict()

    with util.get_progressbar('constructing examples', len(games)) as pbar:
        for game_idx, (game_id, game) in enumerate(games.items()):
            game_examples, all_sets = construct_game_examples(game, max_instruction_index)
            game.set_examples(game_examples)
            game.set_sets_made(all_sets)
            for i, example in enumerate(game_examples):
                examples[game_id + '-' + str(i)] = example
            pbar.update(game_idx)

    return examples

def get_example_action_index_pairs(examples: Dict[str, InstructionExample],
                                   full_observability: bool,
                                   observability_refresh_rate: int) -> List[Tuple[str, int]]:
    if not full_observability:
        if observability_refresh_rate > 1:
            raise ValueError('Refreshing after more than one action is not supported.')
        ids = list()
        for example_id, example in examples.items():
            for i in range(len(example.get_action_sequence())):
                ids.append((example_id, i))
        return ids
    return [(key, -1) for key in examples.keys()]


import numpy as np
import pylab as pl
# Crea un vector (arreglo) con los valores del eje X
x = [6000,7000,8000,9000,10000]
# Crea un vector (arreglo) con los valores en el eje Y para cada valor en el eje X
y = [73,80,85,87,89]
# Grafica el vector x contra el vector y
pl.plot(x,y, 'r*')
pl.plot(x,y)
# Crea un vector (arreglo) con los valores del eje X
x1 = [7000,8000,9000,10000,11000]
# Crea un vector (arreglo) con los valores en el eje Y para cada valor en el eje X
y1 = [80,83,85,86,88]
# Grafica el vector x contra el vector y
pl.axis ([0,100, 5000,12000])
pl.plot(x1,y1)
pl.plot(x1,y1, 'gD')
pl.ylabel('Voltaje [V]')
pl.xlabel('Eficiencia [%]')
#Guarda la grafica en el formato png
pl.savefig('graph.png')
pl.show()


import deck
import unittest

class TestDeck(unittest.TestCase):
	"""
	Test the deck object
	"""
	cardlist = [deck.card(0,1), deck.card(0,2), deck.card(0,3),
		deck.card(1,1), deck.card(1,2), deck.card(1,3),
		deck.card(2,1), deck.card(2,2), deck.card(2,3)]
	def setUp(self):
		"""
		setup an empty deck
		"""
		self.sampleDeck = deck.deck(shuffle=False)
		self.advDeck = deck.deck(cards=self.cardlist, shuffle=False)

	def test_deck_constructor(self):
		"""
		test the an empty deck constructor with default dictionaries
		"""
		self.assertCountEqual(
			[],
			self.sampleDeck.cards)

	def test_deck_constructorAdv(self):
		"""
		test the deck constructor with user submitted dictionaries and multiple decks
		"""
		self.assertCountEqual(
			self.cardlist,
			self.advDeck.cards)
		self.advDeck = deck.deck(cards=2*self.cardlist)
		self.assertCountEqual(
			self.cardlist*2,
			self.advDeck.cards)

	def test_deck_addCards_validOneCard(self):
		"""
		test the deck's ability to add one valid card
		"""
		self.sampleDeck.addCards([deck.card(2,2)])
		self.assertCountEqual(
			[deck.card(2,2)],
			self.sampleDeck.cards)

	def test_deck_addCards_validListCards(self):
		"""
		test the deck's ability to add a list of valid cards
		"""
		sampleList = [deck.card(2,2), deck.card(3,3), deck.card(4,4)]
		self.sampleDeck.addCards(sampleList)
		self.assertCountEqual(
			sampleList,
			self.sampleDeck.cards)

	def test_deck_addCards_emptyList(self):
		"""
		test the deck's ability to add an empty list of cards
		"""
		self.sampleDeck.addCards([])
		self.assertCountEqual(
			[],
			self.sampleDeck.cards)

	def test_deck_dealHand_validSize(self):
		"""
		test the deck's ability to deal cards with a valid size
		"""
		hand = []
		hand.extend(self.advDeck.dealHand(3))
		self.assertCountEqual(
			[deck.card(0,1), deck.card(0,2), deck.card(0,3)],
			hand)
		self.assertCountEqual(
			[deck.card(1,1), deck.card(1,2), deck.card(1,3), deck.card(2,1), deck.card(2,2), deck.card(2,3)],
			self.advDeck.cards
			)

	def test_deck_dealHand_invalidSize(self):
		"""
		test the deck's ability to deal cards with an invalid input
		"""
		with self.assertRaises(IndexError):
			self.advDeck.dealHand(20)

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


import numpy as np
import cv2

# Identify pixels above the threshold
# Threshold of RGB > 160 does a nice job of identifying ground pixels only
def color_thresh(img, rgb_thresh=(160, 160, 160)):
    # Create an array of zeros same xy size as img, but single channel
    color_path = np.zeros_like(img[:,:,0])
    color_obs = np.zeros_like(img[:,:,0])
    # Require that each pixel be above all three threshold values in RGB
    # above_thresh will now contain a boolean array with "True"
    # where threshold was met
    above_thresh = (img[:,:,0] > rgb_thresh[0]) \
                & (img[:,:,1] > rgb_thresh[1]) \
                & (img[:,:,2] > rgb_thresh[2])

    below_thresh = (img[:,:,0] < rgb_thresh[0]) \
                & (img[:,:,1] < rgb_thresh[1]) \
                & (img[:,:,2] < rgb_thresh[2])
    # Index the array of zeros with the boolean array and set to 1
    color_path[above_thresh] = 1
    color_obs[below_thresh] = 1
    # Return the binary image
    return color_path, color_obs

# Define a function to convert from image coords to rover coords
def rover_coords(binary_img):
    # Identify nonzero pixels
    ypos, xpos = binary_img.nonzero()
    # Calculate pixel positions with reference to the rover position being at the
    # center bottom of the image.
    x_pixel = -(ypos - binary_img.shape[0]).astype(np.float)
    y_pixel = -(xpos - binary_img.shape[1]/2 ).astype(np.float)
    return x_pixel, y_pixel

def rover_coords_obs(binary_img):
    # Identify nonzero pixels
    y_pos, x_pos = np.where(binary_img==0)
    # Calculate pixel positions with reference to the rover position being at the
    # center bottom of the image.
    x_pos += -int(binary_img.shape[1])//2
    y_pos += -int(binary_img.shape[0])

    theta = np.pi/2
    x_pixel = x_pos*np.cos(theta)-y_pos*np.sin(theta)
    y_pixel = x_pos*np.sin(theta)+y_pos*np.cos(theta)

    y_pixel = -y_pixel
    return x_pixel, y_pixel

# Define a function to convert to radial coords in rover space
def to_polar_coords(x_pixel, y_pixel):
    # Convert (x_pixel, y_pixel) to (distance, angle)
    # in polar coordinates in rover space
    # Calculate distance to each pixel
    dist = np.sqrt(x_pixel**2 + y_pixel**2)
    # Calculate angle away from vertical for each pixel
    angles = np.arctan2(y_pixel, x_pixel)
    return dist, angles

# Define a function to map rover space pixels to world space
def rotate_pix(xpix, ypix, yaw):
    # Convert yaw to radians
    yaw_rad = yaw * np.pi / 180
    xpix_rotated = (xpix * np.cos(yaw_rad)) - (ypix * np.sin(yaw_rad))

    ypix_rotated = (xpix * np.sin(yaw_rad)) + (ypix * np.cos(yaw_rad))
    # Return the result
    return xpix_rotated, ypix_rotated

def translate_pix(xpix_rot, ypix_rot, xpos, ypos, scale):
    # Apply a scaling and a translation
    xpix_translated = (xpix_rot / scale) + xpos
    ypix_translated = (ypix_rot / scale) + ypos
    # Return the result
    return xpix_translated, ypix_translated

# Define a function to apply rotation and translation (and clipping)
# Once you define the two functions above this function should work
def pix_to_world(xpix, ypix, xpos, ypos, yaw, world_size, scale):
    # Apply rotation
    xpix_rot, ypix_rot = rotate_pix(xpix, ypix, yaw)
    # Apply translation
    xpix_tran, ypix_tran = translate_pix(xpix_rot, ypix_rot, xpos, ypos, scale)
    # Perform rotation, translation and clipping all at once
    x_pix_world = np.clip(np.int_(xpix_tran), 0, world_size - 1)
    y_pix_world = np.clip(np.int_(ypix_tran), 0, world_size - 1)
    # Return the result
    return x_pix_world, y_pix_world

# Define a function to perform a perspective transform
def perspect_transform(img, src, dst):

    M = cv2.getPerspectiveTransform(src, dst)
    warped = cv2.warpPerspective(img, M, (img.shape[1], img.shape[0]))# keep same size as input image
    outView = cv2.warpPerspective(np.ones_like(img[:,:,0]), M, (img.shape[1], img.shape[0]))
    return warped, outView

def yellow_select(img):
    h_mask = (20, 50)
    s_mask = (100, 255)
    v_mask = (100, 255)
    # 1) Convert to HLS color space
    hsv = cv2.cvtColor(img, cv2.COLOR_RGB2HSV)
    h, s, v = cv2.split(hsv)
    # 2) Apply a threshold to the S channel
    h_ = np.zeros_like(h, dtype=np.uint8)
    h_[(h > h_mask[0]) & (h < h_mask[1]) & (s>s_mask[0]) & (s<s_mask[1]) & (v>v_mask[0]) & (v<v_mask[1]) ] = 1
    # 3) Return a binary image of threshold result
    binary_output = h_
    return binary_output

# Apply the above functions in succession and update the Rover state accordingly
def perception_step(Rover):
    # Perform perception steps to update Rover()
    # TODO:
    # NOTE: camera image is coming to you in Rover.img
    img = Rover.img
    xpos, ypos = Rover.pos
    yaw = Rover.yaw
    # 1) Define source and destination points for perspective transform
    dst_size = 8
    bottom_offset = 6
    source = np.float32([[14, 140], [301 ,140],[200, 96], [118, 96]])
    destination = np.float32([[img.shape[1]/2 - dst_size, img.shape[0] - bottom_offset],
                      [img.shape[1]/2 + dst_size, img.shape[0] - bottom_offset],
                      [img.shape[1]/2 + dst_size, img.shape[0] - 2*dst_size - bottom_offset],
                      [img.shape[1]/2 - dst_size, img.shape[0] - 2*dst_size - bottom_offset],
                      ])
#     # 2) Apply perspective transform
    warped, mask = perspect_transform(img, source, destination)
#     # 3) Apply color threshold to identify navigable terrain/obstacles/rock samples
    threshed, threshed_obs = color_thresh(warped, rgb_thresh=(160, 160, 160))
    threshed_obs = np.absolute(np.float32(threshed_obs))*mask
    rock_threshed = yellow_select(warped)
    # 4) Update Rover.vision_image (this will be displayed on left side of screen)
    # Rover.vision_image[:,:,0] = obstacle
    # Rover.vision_image[:,:,1] = rock_sample
    # Rover.vision_image[:,:,2] = navigable
    Rover.vision_image[:,:,0] = threshed_obs*255
    Rover.vision_image[:,:,1] = rock_threshed*255
    Rover.vision_image[:,:,2] = threshed*255

    xpix, ypix = rover_coords(threshed)
    dist, angles = to_polar_coords(xpix, ypix)
    mean_dir = np.mean(angles)

    xrock, yrock = rover_coords(rock_threshed)
    rock_dist, rock_angles = to_polar_coords(xrock, yrock)

    xobs, yobs = rover_coords(threshed_obs)

    # 5) Convert rover-centric pixel values to world coords
    world_size = 200
    scale = 2*dst_size
    x_pix_world, y_pix_world = pix_to_world(xpix, ypix,
                 xpos,ypos,yaw,
                 world_size, scale)

    x_obs_world, y_obs_world = pix_to_world(xobs, yobs,
                 xpos,ypos,yaw,
                 world_size, scale)

    x_rock_world, y_rock_world = pix_to_world(xrock, yrock,
                 xpos,ypos,yaw,
                 world_size, scale)
    # 7) Update Rover worldmap (to be displayed on right side of screen)
    Rover.worldmap[y_obs_world, x_obs_world, 0] += 1
    Rover.worldmap[y_rock_world, x_rock_world, 1] += 1
    Rover.worldmap[y_pix_world, x_pix_world, 2] += 1
    #Rover.worldmap[y_obs_world, x_obs_world, 0] = 255
    #Rover.worldmap[y_rock_world, x_rock_world, 1] = 255
    #Rover.worldmap[y_pix_world, x_pix_world, 2] = 255
    # 8) Convert rover-centric pixel positions to polar coordinates
    # Update Rover pixel distances and angles
    # Rover.nav_dists = np.mean(dist)
    # Rover.nav_angles = np.mean(angles)
    Rover.nav_dists = dist
    Rover.nav_angles = angles
    Rover.rock_dists = rock_dist
    Rover.rock_angles = rock_angles

    return Rover


import sms_sdk_renderer_python.lib.sms_sdk_renderer as SmsRenderer

form_data = {
    "header" : {"title":"Test Form",
                "titleSize": 4,
                "formId": "test_form_id"},
    "body": [{"textfield":{
                "name":"exmaple-text-field",
                "placeholder": "example-placeholder",
                "required": "true",
                "masked": "true",
                "minlength": 1,
                "maxlength": 128
    }},
            {"textarea":{
                "name":"exmaple-text-area",
                "placeholder": "example-placeholder",
                "required": "true"
            }},
            {"checkbox":{
                "name":"example-name",
                "value":"example-value",
                "checked": "false",
                "text":"Red"
            }},
            {"dropdown_menu" : {
            "name":"dropdown-name",
            "required": "true",
            "options": [{"value":"value1", "selected":"true", "text":"First Option"},
                        {"value":"value2", "selected":"false", "text":"Second Option"},
                        {"value":"value3", "selected":"false", "text":"Third Option"} ]

            }},
            {"personselector":{
                "name":"person-selector-name",
                "placeholder":"example-placeholder",
                "required":"true"
            }},
            {"table_select":{
                    "select":{
                        "position":"right",
                        "type":"checkbox"
                    },
                    "header_list": ["H1", "H2", "H3"],
                    "body": [["A1", "B1", "C1"],
                             ["A2", "B2", "C2"],
                             ["A3", "B3", "C3"]],
                    "footer_list": ["F1", "F2", "F3"]
                    }}],
    "footer" : [
        {"button": {
            "name": "example-button",
            "type": "action",
            "text": "Submit"
        }},
        {"button": {
            "name": "example-button",
            "type": "reset",
            "text": "Cancel"
        }}]
}

print(SmsRenderer.renderForm(form_data, SmsRenderer.smsTypes['FORM']))


# fruit = 'banana'
# letter = fruit[0]
# len(fruit)
# print(len(fruit))

# # index = 0
# fruit = 'banana'
# # while index < len(fruit):
# #     letter = fruit[index]
# #     print(letter)
# #     index = index + 1

# for letters in fruit:
#     print(letters)

# prefixes = 'JKLMNOPQ'
# suffix = 'ack'

# for letters in prefixes:
#     if letters == "O" or letters == "Q":
#         print(letters + "u" + suffix)
#     else:
#         print(letters + suffix)

# fruit = 'banana'
# print(fruit[:] )

# [1, 2, 3, 4]

# cheeses = ['gouda', 'ham', 'parm']

# for cheese in cheeses:
#     print(cheese)

# t = ['a','b','c','a']
# t.sort()
# t = t.sort()
# print(t)

import random

guessesTaken = 0

print('Hello! What is your name?')
myName= input()

number = random.randint(1, 20)
print('Well,' + myName + ', I am thinking of a number between 1 and 20.')

for i in range (0, 7):
    print('Take a guess.')
    guess = input()
    guess = int(guess)

    guessesTaken = guessesTaken + 1

    if guess < number:
        print('Your guess is too low.')

    if guess > number:
        print('Your guess is too high.')

    if guess == number:
        break

if guess == number:
    guessesTaken = str(guessesTaken)
    print('Good job,' + myName + '! You guessed my number in ' + guessesTaken + ' guesses!')

    if guess != number:
        number = str(number)
        print('Nope. The number I was thinking was ' + number)


from twisted.internet import defer
from pysnmp.entity.rfc3413 import ntforg
from pyasn1.compat.octets import null

def __cbFun(snmpEngine, sendRequestHandle, errorIndication,
           errorStatus, errorIndex, varBinds, cbCtx):
    cbCtx.callback(
        (snmpEngine, errorIndication, errorStatus, errorIndex, varBinds)
    )

class NotificationOriginator:
    def __init__(self, snmpContext=None):
        self.snmpContext = snmpContext  # this is deprecated
        self.notificationOriginator = ntforg.NotificationOriginator()

    def sendVarBinds(self,
                     snmpEngine,
                     notificationTarget,
                     snmpContext,
                     contextName,
                     notificationName,
                     instanceIndex,
                     additionalVarBinds=()):
        df = defer.Deferred()
        self.notificationOriginator.sendVarBinds(
            snmpEngine,
            notificationTarget,
            snmpContext,
            contextName,
            notificationName,
            instanceIndex,
            additionalVarBinds,
            __cbFun,
            df
        )
        return df

#
# Obsolete, compatibility interfaces.
#

def __sendReqCbFun(response, outerDf):
    ( snmpEngine,
      errorIndication,
      errorStatus,
      errorIndex,
      varBinds) = response
    outerDf.callback((errorIndication, errorStatus, errorIndex, varBinds))
    # Callback function may return another deferred to indicate
    # it wishes to continue MIB walk.
    if isinstance(outerDf.result, defer.Deferred):
        innerDf = defer.Deferred()
        innerDf.addCallback(__sendReqCbFun, outerDf.result)
        return innerDf

def _sendNotification(self,
                      snmpEngine,
                      notificationTarget,
                      notificationName,
                      additionalVarBinds=None,
                      contextName=null):

    innerDf = self.sendVarBinds(snmpEngine,
                                notificationTarget,
                                self.snmpContext,
                                contextName,
                                notificationName,
                                None,
                                additionalVarBinds)
    outerDf = defer.Deferred()
    innerDf.addCallback(__sendReqCbFun, outerDf)
    return outerDf

# install compatibility wrappers
NotificationOriginator.sendNotification = _sendNotification


import mynlp
from .CasepairClass import CasepairClass

def _extract_self_sufficient_word(words):
    for word in words:
        if word.pos in ["形容詞", "動詞"]:
            return word
    return None

def extract_cp(phs):
    if not phs:
        return []
    cps = []
    for pi, ph in phs.items():
        previous_w = mynlp.WordClass()
        if not ph.parent_id:
            continue
        for word in ph.words:
            if previous_w.pos_detail == "形式名詞":
                continue
            if word.pos_detail == "格助詞" and previous_w.pos == "名詞":
                if ph.parent_id < 0:
                    continue
                parent = phs[ph.parent_id]
                parent_self_w = _extract_self_sufficient_word(parent.words)

                if not parent_self_w:
                    continue

                cp = {}
                cp["noun"] = previous_w.base
                cp["particle"] = word.particle
                cp["predicate"] = parent_self_w.base
                cp["cateogory"] = word.category

                cps.append(cp)
    return cps

def extract_cp_and_embed_class(phs, th_i, p_i, s_i, u_i):
    if not phs:
        return []
    cps = extract_cp(phs)
    cps_class_list = []
    for cp in cps:
        tmp = CasepairClass(th_i=th_i, p_i=p_i, s_i=s_i, u_i=u_i, particle=cp["particle"], \
                            predicate=cp["predicate"], category=cp["category"])
        cps_class_list.append({"noun": cp["noun"], "cp": tmp})
    return cps


"""This file is provided as a starting template for writing your own unit
tests to run and debug your minimax and alphabeta agents locally.  The test
cases used by the project assistant are not public.
"""

import unittest

import isolation
import game_agent

from importlib import reload

class IsolationTest(unittest.TestCase):
    """Unit tests for isolation agents"""

    def setUp(self):
        reload(game_agent)
        self.player1 = "Player1"
        self.player2 = "Player2"
        self.game = isolation.Board(self.player1, self.player2)

    def test_minimax_interface(self):
        """ Test MinimaxPlayer.minimax() interface and output """
        test_depth = 1
        starting_location = (5, 3)
        adversary_location = (0, 0)  # top left corner

        # create a player agent & a game board
        agentMinMax = game_agent.MinimaxPlayer()
        agentMinMax.time_left = lambda: 99  # ignore timeout for fixed-depth search
        board = isolation.Board(agentMinMax, self.player2)

        # place two "players" on the board at arbitrary (but fixed) locations
        board.apply_move(starting_location)
        board.apply_move(adversary_location)

        print(board.print_board())

        for move in board.get_legal_moves():
            next_state = board.forecast_move(move)
            move = agentMinMax.minimax(next_state, test_depth)
            print("Value: {} - Type: {}".format(move, type(move)))
            self.assertIsInstance(move, tuple,
                            ("Minimax function should return a tuple"))

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


import re
import pandas

def read_to_csv(file):
	df = pandas.DataFrame()

	crop = file.split('/')[1].split('_')[0]
	loc = file.split('.')[0].split('_')[-1]

	alldata = []
	cols = []

	with open(file, 'r') as f:
		for line in f.readlines()[0].replace(' ','').replace('"','').replace('}','').split('{')[1:-1]:
			l = [i.split(':') for i in filter(None,line.split(','))]
			if len(cols) == 0: cols = ['{0}_{1}_{2}'.format(crop,loc,j[0]) for j in l]
			items = [j[1] for j in l]
			alldata.append(items)
	df = pandas.DataFrame(alldata,columns=cols)
	df.set_index('{0}_{1}_year'.format(crop,loc),inplace=True)
	df = df.apply(pandas.to_numeric)
	df = df.divide(1.e6)
	df['{0}_{1}_total'.format(crop,loc)] = df.sum(axis=1)
	df.to_csv('{0}.csv'.format(file.split('.')[0]))

files = ['data/corn_data_ewg_us.txt','data/corn_data_ewg_il.txt','data/soy_data_ewg_us.txt','data/soy_data_ewg_il.txt']

for file in files:
	read_to_csv(file)


import matplotlib.pyplot as plt
import joypy
import numpy as np
import pandas as pd

colors = ['#791E94', '#58C9B9', '#519D9E', '#D1B6E1']
data = pd.read_csv('sample_data.csv')
fig, axs = joypy.joyplot(data, by="project", fill=True, legend=True, alpha=.8,
                         range_style='own', xlabelsize=22, ylabelsize=22,
                         grid='both', linewidth=.8, linecolor='k', figsize=(12, 6), color=colors,
                         )

ax = plt.gca()
ax.set_xlim(left=0, right=7)
ax.set_xticks(np.arange(7))
ax.set_xticklabels( ['1', '2', '3', '4', '5', '6','7'])
ax.text(.5,
        1.1,
        "expression comparison",
        transform=ax.transAxes,
        ha='center',
        va='center',
        fontsize=25,
        color='black')
ax.text(.5, 1.03, "3 projects, 4 samples for each project", transform=ax.transAxes,
        ha='center', va='center', fontsize=15, color='black')
ax.text(.90, -.11, '\nBioPython teaching examples, do not represent real research', transform=ax.transAxes,
        ha='center', va='center', fontsize=12, color='black')
plt.savefig(r'sierra.png',
            width=7, height=5, dpi=900, bbox_inches='tight')


#!/usr/bin/python3

from daemons import Daemon, Wallet

import random
import time
import shutil
import os
from os import path
import asyncio
import glob
from datetime import datetime
import uuid

datadirectory="testdata"

def coins(*args):
    if len(args) != 1:
        return tuple(coins(x) for x in args)
    x = args[0]
    if type(x) in (tuple, list):
        return type(x)(coins(i) for i in x)
    return round(x * 1000000000)

def wait_for(callback, timeout=10):
    expires = time.time() + timeout
    while True:
        try:
            if callback():
                return
        except:
            pass
        if time.time() >= expires:
            raise RuntimeError("task timeout expired")
        time.sleep(.25)

verbose = True
def vprint(*args, timestamp=True, **kwargs):
    global verbose
    if verbose:
        if timestamp:
            print(datetime.now(), end=" ")
        print(*args, **kwargs)

class MNNetwork:
    def __init__(self, datadir, *, binpath='../../build/bin', mns=12, nodes=3):
        self.datadir = datadir
        if not os.path.exists(self.datadir):
            os.makedirs(self.datadir)
        self.binpath = binpath

        vprint("Using '{}' for data files and logs".format(datadir))

        nodeopts = dict(master=self.binpath+'/master', datadir=datadir)

        self.mns = [Daemon(master_node=True, **nodeopts) for _ in range(mns)]
        self.nodes = [Daemon(**nodeopts) for _ in range(nodes)]

        self.all_nodes = self.mns + self.nodes

        self.wallets = []
        for name in ('Alice', 'Bob', 'Mike'):
            self.wallets.append(Wallet(
                node=self.nodes[len(self.wallets) % len(self.nodes)],
                name=name,
                rpc_wallet=self.binpath+'/beldex-wallet-rpc',
                datadir=datadir))

        self.alice, self.bob, self.mike = self.wallets

        # Interconnections
        for i in range(len(self.all_nodes)):
            for j in (2, 3, 5, 7, 11):
                k = (i + j) % len(self.all_nodes)
                if i != k:
                    self.all_nodes[i].add_peer(self.all_nodes[k])

        vprint("Starting new master master nodes with RPC on {} ports".format(self.mns[0].listen_ip), end="")
        for mn in self.mns:
            vprint(" {}".format(mn.rpc_port), end="", flush=True, timestamp=False)
            mn.start()
        vprint(timestamp=False)
        vprint("Starting new regular master nodes with RPC on {} ports".format(self.nodes[0].listen_ip), end="")
        for d in self.nodes:
            vprint(" {}".format(d.rpc_port), end="", flush=True, timestamp=False)
            d.start()
        vprint(timestamp=False)

        vprint("Waiting for all master's to get ready")
        for d in self.all_nodes:
            d.wait_for_json_rpc("get_info")

        vprint("Beldexds are ready. Starting wallets")

        for w in self.wallets:
            vprint("Starting new RPC wallet {w.name} at {w.listen_ip}:{w.rpc_port}".format(w=w))
            w.start()
        for w in self.wallets:
            w.ready()
            w.refresh()
            vprint("Wallet {w.name} is ready: {a}".format(w=w, a=w.address()))

        for w in self.wallets:
            w.wait_for_json_rpc("refresh")

        # Mine some blocks; we need 100 per MN registration, and we can nearly 600 on fakenet before
        # it hits HF16 and kills mining rewards.  This lets us submit the first 5 MN registrations a
        # MN (at height 40, which is the earliest we can submit them without getting an occasional
        # spurious "Not enough outputs to use" error).
        # to unlock and the rest to have enough unlocked outputs for mixins), then more some more to
        # earn MN rewards.  We need 100 per MN registration, and each mined block gives us an input
        # of 18.9, which means each registration requires 6 inputs.  Thus we need a bare minimum of
        # 6(N-5) blocks, plus the 30 lock time on coinbase TXes = 6N more blocks (after the initial
        # 5 registrations).
        self.mine(100)
        vprint("Submitting first round of master node registrations: ", end="", flush=True)
        for mn in self.mns[0:5]:
            self.mike.register_mn(mn)
            vprint(".", end="", flush=True, timestamp=False)
        vprint(timestamp=False)
        if len(self.mns) > 5:
            vprint("Going back to mining", flush=True)

            self.mine(6*len(self.mns))

            self.print_wallet_balances()

            vprint("Submitting more master node registrations: ", end="", flush=True)
            for mn in self.mns[5:-1]:
                self.mike.register_mn(mn)
                vprint(".", end="", flush=True, timestamp=False)
            vprint(timestamp=False)
            vprint("Done.")

        self.print_wallet_balances()

        vprint("Mining 40 blocks (registrations + flash quorum lag) and waiting for nodes to sync")
        self.sync_nodes(self.mine(40))

        self.print_wallet_balances()

        vprint("Sending fake belnet/ss pings")
        for mn in self.mns:
            mn.ping()

        all_master_nodes_proofed = lambda mn: all(x['quorumnet_port'] > 0 for x in
                mn.json_rpc("get_n_master_nodes", {"fields":{"quorumnet_port":True}}).json()['result']['master_node_states'])

        vprint("Waiting for proofs to propagate: ", end="", flush=True)
        for mn in self.mns:
            wait_for(lambda: all_master_nodes_proofed(mn), timeout=120)
            vprint(".", end="", flush=True, timestamp=False)
        vprint(timestamp=False)
        for mn in self.mns[-1:]:
            self.mike.register_mn(mn)
            vprint(".", end="", flush=True, timestamp=False)
        self.sync_nodes(self.mine(1))
        time.sleep(10)
        for mn in self.mns:
            mn.send_uptime_proof()
        vprint("Done.")

        vprint("Local Devnet MN network setup complete!")
        vprint("Communicate with daemon on ip: {} port: {}".format(self.mns[0].listen_ip,self.mns[0].rpc_port))
        configfile=self.datadir+'config.py'
        with open(configfile, 'w') as filetowrite:
            filetowrite.write('#!/usr/bin/python3\n# -*- coding: utf-8 -*-\nlisten_ip=\"{}\"\nlisten_port=\"{}\"\nwallet_listen_ip=\"{}\"\nwallet_listen_port=\"{}\"\nwallet_address=\"{}\"\nexternal_address=\"{}\"'.format(self.mns[0].listen_ip,self.mns[0].rpc_port,self.mike.listen_ip,self.mike.rpc_port,self.mike.address(),self.bob.address()))

    def refresh_wallets(self, *, extra=[]):
        vprint("Refreshing wallets")
        for w in self.wallets + extra:
            w.refresh()
        vprint("All wallets refreshed")

    def mine(self, blocks=None, wallet=None, *, sync=False):
        """Mine some blocks to the given wallet (or self.mike if None) on the wallet's daemon.
        Returns the daemon's height after mining the blocks.  If blocks is omitted, mines enough to
        confirm regular transfers (i.e. 10 blocks).  If sync is specified, sync all nodes and then
        refresh all wallets after mining."""
        if wallet is None:
            wallet = self.mike
        if blocks is None:
            blocks = 10
        node = wallet.node
        vprint("Mining {} blocks to wallet {.name}".format(blocks, wallet))
        start_height = node.height()
        end_height = start_height + blocks
        node.mine_blocks(blocks, wallet)
        while node.rpc("/mining_status").json()["active"]:
            height = node.height()
            vprint("Mined {}/{}".format(height, end_height))
            time.sleep(0.05 if height >= end_height else 0.25)
        height = node.height()
        vprint("Mined {}/{}".format(height, end_height))

        if sync:
            self.sync_nodes(height)
            self.refresh_wallets()

        return height

    def sync_nodes(self, height=None, *, extra=[], timeout=10):
        """Waits for all nodes to reach the given height, typically invoked after mine()"""
        nodes = self.all_nodes + extra
        heights = [x.height() for x in nodes]
        if height is None:
            height = max(heights)
        if min(heights) >= height:
            vprint("All nodes already synced to height >= {}".format(height))
            return
        vprint("Waiting for all nodes to sync to height {}".format(height))
        last = None
        expiry = time.time() + timeout
        while nodes and time.time() < expiry:
            if heights[-1] < height:
                heights[-1] = nodes[-1].height()
            if heights[-1] >= height:
                heights.pop()
                nodes.pop()
                last = None
                continue
            if heights[-1] != last:
                vprint("waiting for {} [{} -> {}]".format(nodes[-1].name, heights[-1], height))
                last = heights[-1]
            time.sleep(0.1)
        if nodes:
            raise RuntimeError("Timed out waiting for node syncing")
        vprint("All nodes synced to height {}".format(height))

    def sync(self, extra_nodes=[], extra_wallets=[]):
        """Synchronizes everything: waits for all nodes to sync, then refreshes all wallets.  Can be
        given external wallets/nodes to sync."""
        self.sync_nodes(extra=extra_nodes)
        self.refresh_wallets(extra=extra_wallets)

    def print_wallet_balances(self):
        """Instructs the wallets to refresh and prints their balances (does nothing in non-verbose mode)"""
        global verbose
        if not verbose:
            return
        vprint("Balances:")
        for w in self.wallets:
            b = w.balances(refresh=True)
            vprint("    {:5s}: {:.9f} (total) with {:.9f} (unlocked)".format(
                w.name, b[0] * 1e-9, b[1] * 1e-9))

    def __del__(self):
        for n in self.all_nodes:
            n.terminate()
        for w in self.wallets:
            w.terminate()

mnn = None

def run():
    global mnn, verbose
    if not mnn:
        if path.isdir(datadirectory+'/'):
            shutil.rmtree(datadirectory+'/', ignore_errors=False, onerror=None)
        vprint("new MNN")
        mnn = MNNetwork(datadir=datadirectory+'/')
    else:
        vprint("reusing MNN")
        mnn.alice.new_wallet()
        mnn.bob.new_wallet()

        # Flush pools because some tests leave behind impossible txes
        for n in mnn.all_nodes:
            assert n.json_rpc("flush_txpool").json()['result']['status'] == 'OK'

        # Mine a few to clear out anything in the mempool that can be cleared
        mnn.mine(5, sync=True)

        vprint("Alice has new wallet: {}".format(mnn.alice.address()))
        vprint("Bob   has new wallet: {}".format(mnn.bob.address()))

    input("Use Ctrl-C to exit...")
    loop = asyncio.get_event_loop()
    try:
        loop.run_forever()
    except KeyboardInterrupt:
        print(f'!!! AsyncApplication.run: got KeyboardInterrupt during start')
    finally:
        loop.close()

# Shortcuts for accessing the named wallets
def alice(net):
    return net.alice

def bob(net):
    return net.bob

def mike(net):
    return net.mike

if __name__ == '__main__':
    run()


from bearlibterminal import terminal as t

from actor import Actor
from actions import *

import random

class Enemy(Actor):
    char = 'E'
    fg = 'orange'

    def __init__(self, x, y):
        super().__init__(x, y)

    def act(self, key):
        from world import World
        import math

        player = World.get().player
        dx = player.x - self.x
        dy = player.y - self.y
        dist = math.sqrt(dx*dx+dy*dy)
        if dist <= 0:
            return ['move', 1, -1]
        dx /= dist
        dy /= dist

        delta = random.choices([
            (round(dx), round(dy)),
            (round(-dx), round(-dy)),
            None
        ], [
            8 if dist > 4 else 0.5,
            8 if dist < 2 else 0.5,
            0.1,
        ])[0]
        #if dist > 4:
        #    return ['move', round(dx), round(dy)]
        #if dist < 2:
        #    return ['move', round(-dx), round(-dy)]

        if delta:
            return ['move', *delta]
        else:
            ai_actions = [
                'stab',
                'slash',
                'long_slash',
            ]

            #ai_actions = list(actions.keys())
            return self.do_action(random.choice(ai_actions), player.x, player.y)

        return ['wait', 10]


from django.conf.urls import url
from django.contrib import admin
from app01 import views

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^classes/', views.classes),
    url(r'^add_class/', views.add_class),
    url(r'^del_class/', views.del_class),
    url(r'^edit_class/', views.edit_class),
    url(r'^students/', views.students),
    url(r'^add_student/', views.add_student),
    url(r'^del_student/', views.del_student),
    url(r'^edit_student/', views.edit_student),
    url(r'^modal_add_class/', views.modal_add_class),
    url(r'^modal_edit_class/', views.modal_edit_class),
    url(r'^modal_add_student/', views.modal_add_student),
    url(r'^modal_edit_student/', views.modal_edit_student),
    url(r'^teachers/', views.teachers),
    url(r'^add_teacher/', views.add_teacher),
    url(r'^edit_teacher/', views.edit_teacher),
    url(r'^get_all_class/', views.get_all_class),
    url(r'^modal_add_teacher/', views.modal_add_teacher),
    url(r'^test/', views.test),
    url(r'^layout/', views.layout),
    url(r'^login/', views.login),
]


from tornado.websocket import WebSocketHandler

import silkycoms
from instance import Instance

class ClientConnection(WebSocketHandler):

    number_of_connections = 0

    def initialize(self, session_path):

        self._session_path = session_path
        self._transactions = { }
        self._listeners = [ ]

    def check_origin(self, origin):
        return True

    def open(self):

        ClientConnection.number_of_connections += 1

        print('websocket opened')
        self.set_nodelay(True)

    def on_close(self):

        ClientConnection.number_of_connections -= 1

        print('websocket closed')

    def on_message(self, m_bytes):
        message = silkycoms.ComsMessage.create_from_bytes(m_bytes)
        clas = getattr(silkycoms, message.payloadType)
        request = clas.create_from_bytes(message.payload)
        self._transactions[message.id] = request

        if type(request) == silkycoms.InstanceRequest:
            if 'instanceId' not in message:
                instance = Instance(session_path=self._session_path)  # create new
            else:
                instance = Instance.instances[message.instanceId]
            instance.set_coms(self)
            response = silkycoms.InstanceResponse()
            self.send(response, instance.id, request)
        else:
            instance = Instance.instances[message.instanceId]
            instance.on_request(request)

    def send(self, message=None, instance_id=None, response_to=None, complete=True):

        if message is None and response_to is None:
            return

        m = silkycoms.ComsMessage()

        if instance_id is not None:
            m.instanceId = instance_id

        if response_to is not None:
            for key, value in self._transactions.items():
                if value is response_to:
                    m.id = key
                    if complete:
                        del self._transactions[key]
                    break
        else:
            m.id = 0

        if message is not None:
            m.payload = message.encode_to_bytes()
            m.payloadType = message.__class__.__name__

        if complete:
            m.status = silkycoms.Status.COMPLETE
        else:
            m.status = silkycoms.Status.IN_PROGRESS

        self.write_message(m.encode_to_bytes(), binary=True)

    def send_error(self, message=None, cause=None, instance_id=None, response_to=None):

        if message is None and response_to is None:
            return

        m = silkycoms.ComsMessage()

        if instance_id is not None:
            m.instanceId = instance_id

        if response_to is not None:
            for key, value in self._transactions.items():
                if value is response_to:
                    m.id = key
                    del self._transactions[key]
                    break
        else:
            m.id = 0

        if message is not None:
            m.error.message = message

        if cause is not None:
            m.error.cause = cause

        m.status = silkycoms.Status.ERROR

        self.write_message(m.encode_to_bytes(), binary=True)

    def discard(self, message):
        for key, value in self._transactions.items():
            if value is message:
                del self._transactions[key]
                break


import numpy as np
from tensorflow.python import keras as K

# 2-layer neural network.
model = K.Sequential([
    # 1層目から2層目に値を送る際は活性化関数のactivation="sigmoid"を適用
    K.layers.Dense(units=4, input_shape=((2, )),
                   activation="sigmoid"),
    K.layers.Dense(units=4),
])

# Make batch size = 3 data (dimension of x is 2).
# 伝搬処理は複数件まとめて送ることが多いらしい。3件まとめたのがこんな感じ
# [
#  [x^1_1, x^1_2],
#  [x^2_1, x^2_2],
#  [x^3_1, x^3_2],
# ]
batch = np.random.rand(3, 2)

y = model.predict(batch)
print(y.shape)  # Will be (3, 4)


# -*- coding：UTF-8 -*-
from openpyxl import load_workbook
from pprint import pprint

class ExcelOperate(object):
    '''
        excel的操作相关
    '''

    def get_data(self, file_path, sheet_name):
        '''
        读取excel的数据，并返回
        :param file_path:
        :param sheet_name:
        :return:data[list]
        '''
        wb = load_workbook(file_path)
        a_sheet = wb[sheet_name]
        max_row = a_sheet.max_row
        max_column = a_sheet.max_column
        # 循环读取数据（dict）,并放入data（list）中 ，row 是行
        data = []
        for i in range(2, max_row + 1):
            data_dict = {}
            for j in range(1, max_column + 1):
                title = a_sheet.cell(row=1, column=j).value
                value = a_sheet.cell(i, j).value
                data_dict[title] = value
            data.append(data_dict)
        return data

    def edit_excel(self, file_path, sheet_name, change_list):
        '''
        将编号在change_list中的行的第change_column列
        的值修改为flag
        :param file_path: 文件目录
        :param sheet_name: sheet页名
        :param change_list: 修改列表
        :return:
        '''
        wb = load_workbook(file_path)
        a_sheet = wb[sheet_name]
        # max_row = a_sheet.max_row
        # max_column = a_sheet.max_column
        rows = a_sheet.rows
        # column = a_sheet.columns
        change_column = 7

        # a = a_sheet['A2'].value
        for row in rows:

            # 每一行
            line = [col.value for col in row]
            print(line)
            # print(line[0])

            if line[0] in change_list:
                a_sheet.cell(row=int(line[0]), column=change_column).value = 'flag'
                wb.save(file_path)
        print('更新列表：{}'.format(change_list))

if __name__ == '__main__':
    file_path = 'test1.xlsx'
    sheet_name = 's1'
    change_list = {5, 6}
    eo = ExcelOperate()
    data = eo.get_data(file_path, sheet_name)
    pprint(data)
    # data = eo.edit_excel(file_path, sheet_name, change_list)
    # pprint(data)


#!/usr/bin/env python
from __future__ import print_function, division

from numpy import *

if __name__ == '__main__':

	import sys, os

	def usage():
		print( "Usage:", sys.argv[0], "path/to/model.obj 0.3(lower_bound) 0.9(upper_bound) 0.02(step)", file = sys.stderr )
		sys.exit(-1)

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

	inpath, lower, upper, step = sys.argv[1:]
	lower = float( lower )
	upper = float( upper )
	step = float( step )

	import subprocess
	for ratio in linspace( lower, upper, round((upper-lower)/step) + 1 ):
		outpath = "decimated_output_" + str(int(ratio*100)) + ".obj"
		command = "./commandlineDecimater -M Q -M AR:50.0 -n " + str(ratio) \
				+ " -i " + inpath \
				+ " -o " + outpath

		subprocess.call(command, shell=True)


from django import forms
from django.forms.widgets import *
from bootstrap_datepicker_plus import DatePickerInput
from crispy_forms.helper import FormHelper
from crispy_forms.layout import Layout, Submit, Row, Column, ButtonHolder, MultiWidgetField
from datetime import date,datetime

from cadastros.models import Jogador

class DateInput(forms.DateInput):
    input_type = 'date'

    def __init__(self, **kwargs):
        kwargs["format"] = "%Y-%m-%d"
        super().__init__(**kwargs)

class DadosPessoaisForm(forms.ModelForm):
    def __init__(self, *args, **kwargs):
        super(DadosPessoaisForm, self).__init__(*args, **kwargs)

        self.fields['nome'].disabled = True
        self.fields['apelido'].disabled = True
        self.fields['data_ini'].disabled = True
        self.fields['data_fim'].disabled = True
        self.fields["data_nascimento"].widget = DateInput()

        self.helper = FormHelper()
        self.helper.form_method = 'POST'
        self.helper.layout = Layout(
            Row(
                Column('nome', css_class='form-group col-md-7 mb-0'),
                Column('apelido', css_class='form-group col-md-5 mb-0'),
            ),
            Row(
                Column('email', css_class='form-group col-md-8 mb-0'),
            ),
            Row(
                Column('data_nascimento', css_class='form-group date col-md-4 mb-0'),
            ),
            Row(
                Column('telefone1', css_class='form-group col-md-3 mb-0'),
                Column('telefone2', css_class='form-group col-md-3 mb-0'),
            ),

            Row (
                Column('data_ini', css_class='form-group col-md-3 mb-0'),
                Column('data_fim', css_class='form-group col-md-3 mb-0'),
            ),
            Submit('alterar_dados', 'Salvar', css_class='btn-info')
        )

    class Meta:
        model   = Jogador
        fields  = ('nome', 'apelido', 'email', 'data_nascimento', 'telefone1', 'telefone2', 'data_ini', 'data_fim')


from .single_stage import SingleStageDetector
from ..builder import DETECTORS

@DETECTORS.register_module
class CenterNetFPN(SingleStageDetector):

    def __init__(self,
                 backbone,
                 neck,
                 bbox_head,
                 train_cfg=None,
                 test_cfg=None,
                 pretrained=None):
        super(CenterNetFPN, self).__init__(backbone, neck, bbox_head, train_cfg,
                                   test_cfg, pretrained)

    def forward_test(self, img, img_meta,**kwargs):
        x = self.extract_feat(img)
        outs = self.bbox_head(x)
        bbox_inputs = outs + (img_meta, self.test_cfg)
        result_lists = self.bbox_head.get_bboxes(*bbox_inputs)
        #print("result_lists:", result_lists) #[1]
        return result_lists[0]


import numpy as np
from sklearn.neighbors import KNeighborsClassifier
import pandas as pd
from sklearn.model_selection import train_test_split,cross_val_score
from sklearn.metrics import accuracy_score
from sklearn.linear_model import LogisticRegression

df = pd.read_csv('heart.csv')
print(df.shape)

df.replace('?',-99999,inplace=True)
df.drop(['sex'],axis=1,inplace=True)
#print(df.shape)
#print(df)

x = np.array(df.drop(['target'],1))
y = np.array(df['target'])

x_train, x_test,y_train,y_test = train_test_split(x,y,test_size=0.3,random_state=0)
#print(x_train.shape)
#print(y_train.shape)

knn = KNeighborsClassifier(n_neighbors=30)
knn.fit(x_train,y_train)

y_pred = knn.predict(x_test)

print(accuracy_score(y_test,y_pred))

logreg = LogisticRegression()

logreg.fit(x_train,y_train)

y_pred = logreg.predict(x_test)
print(accuracy_score(y_test,y_pred))


# 1.加载依赖
import pymysql

# 2.打开数据库连接
db=pymysql.connect("localhost","root","123456","homewokr1")

# 3.使用cursor（）方法创建一个游标对象
cursor= db.cursor()

# 4使用execute（） 方法执行sql查询
sql="""

"""
cursor.execute("sql")

# 5.使用fetchone（）方法获取单条数据,获取结果集，并进行封装
data= cursor.fetchone()

# 打印版本号
print("Database version : %s " % data)

# 6.关闭数据库连接
db.close()


from string import ascii_uppercase
alphabet = '0123456789' + ascii_uppercase

def toCountSystem(x, y):
	ans = ''
	while x:
		ans += alphabet[x%y]
		x //= y
	return ans[::-1]

for n in range(2, len(alphabet)):
	res = toCountSystem(87, n)
	if res[-1] == '2' and len(res) <= 2:
		break

print(n)


# -*- coding: utf-8 -*-
"""
Created on Mon Mar 27 18:51:59 2017

@author: Johann Dolivet

Natural Language Processing : Sentiment analysis

The goal is to predict if a critic for a movie is positive or negative.

Datas :
    25000 critics (text with label 1 if positive and 0 if negative)
        are used to train the model.
    25000 critics (text without labels) are used to test the model.

Model :
    The Stochastic Gradient Descent Classifier is used.

Libraries : pandas to use dataframes, read csv, transform in csv
            sklearn to transfrom in vectors and to use SGD Classifier
"""
import os
import re
import pandas as pd
from sklearn.linear_model import SGDClassifier
from sklearn.feature_extraction.text import TfidfVectorizer

# paths for the datas
train_path = "public/aclImdb/train/"    # files are under this directory
test_path = "public/imdb_te.csv"        # test data for grade evaluation

# parameter for the fitting :
MinDf = 2       # discard words appearing in less than MinDf documents
MaxDf = 0.9     # discard words appearing in more than MaxDf of the documents

#==============================================================================
# Process the datas
#   convert the datas in a csv (in lowercase without stopwords)
#==============================================================================

def stop_words(fileName):
    result = []
    fichierManip = open(fileName, 'r')
    for line in fichierManip:
        result.append(line[:-1])
    fichierManip.close()
    return result

def imdb_data_preprocess(inpath, name="imdb_tr.csv"):
    chemin = inpath
    stopList = stop_words("stopwords.en.txt")
    negatif = os.path.join(chemin,"neg")
    positif = os.path.join(chemin,"pos")
    signe = [positif, negatif]
    pattern = re.compile('[^a-z]')
    listText, listPol = [],[]
    for elt in signe:
        listeFile = os.listdir(elt)
        for file in listeFile:
            sample = open(elt+'/'+file,'r')
            for line in sample:
                line = line.replace('

', ' ').lower()
                liste = pattern.split(line)
                liste = [elt for elt in liste if (elt not in stopList and len(elt) > 1)]
                line = ' '.join(liste)
                listText.append(line)
            if elt == positif:
                listPol.append(1)
            else:
                listPol.append(0)
            sample.close()
    df = pd.DataFrame({'text' : listText, 'polarity' : listPol})
    df = df[['text','polarity']]
    df.to_csv(name)
#imdb_data_preprocess(train_path)

def imdb_data_preprocessTest(inpath, name="imdb_te.csv"):
    stopList = stop_words("stopwords.en.txt")
    datas = pd.read_csv(inpath, encoding='latin1')
    listCritiques = []
    pattern = re.compile('[^a-z]')
    for idx, row in datas.iterrows():
        texte = row[1].replace('

', ' ').lower()
        liste = pattern.split(texte)
        liste = [elt for elt in liste if (elt not in stopList and len(elt) > 1)]
        line = ' '.join(liste)
        listCritiques.append(line)
    df = pd.DataFrame({'text' : listCritiques})
    df.to_csv(name)
#imdb_data_preprocessTest(test_path)

#==============================================================================
# Unigram method
#   input : dataframes for train and test
#       training :  vector for X (n samples of list of words)
#                   vector for y (labels)
#       testing :   vector for X (n samples of list of words)
#   write the result (prediction for testing dataframe) in a csv file
#==============================================================================

def unigram(dataTrain, dataTest):
    vectorizer = TfidfVectorizer(min_df=MinDf, max_df = MaxDf)
    train = dataTrain['text']
    test = dataTest['text']
    label = dataTrain['polarity']
    trainX = vectorizer.fit_transform(train)
    testX = vectorizer.transform(test)
    clf = SGDClassifier(loss="hinge", penalty="l1")
    clf.fit(trainX, label)
    result = clf.predict(testX)
    fichier = open("unigram.output.txt", 'w')
    for elt in result:
        fichier.write(str(elt) + '\n')
    fichier.close()
#dataTrain = pd.read_csv(trainFile, encoding='latin1')
#dataTest = pd.read_csv(testFile, encoding='latin1')
#unigram(dataTrain, dataTest)

#==============================================================================
# Bigram method
#   input : dataframes for train and test
#       training :  vector for X (n samples of list of words)
#                   vector for y (labels)
#       testing :   vector for X (n samples of list of words)
#   write the result (prediction for testing dataframe) in a csv file
#==============================================================================

def bigram(dataTrain, dataTest):
    vectorizer = TfidfVectorizer(min_df=MinDf, max_df = MaxDf, ngram_range=(1,2))
    train = dataTrain['text']
    test = dataTest['text']
    label = dataTrain['polarity']
    trainX = vectorizer.fit_transform(train)
    testX = vectorizer.transform(test)
    clf = SGDClassifier(loss="hinge", penalty="l1")
    clf.fit(trainX, label)
    result = clf.predict(testX)
    fichier = open("bigram.output.txt", 'w')
    for elt in result:
        fichier.write(str(elt) + '\n')
    fichier.close()
#dataTrain = pd.read_csv(trainFile, encoding='latin1')
#dataTest = pd.read_csv(testFile, encoding='latin1')
#bigram(dataTrain, dataTest)

#==============================================================================
# Unigram with tf-idf method
#   input : dataframes for train and test
#       training :  vector for X (n samples of list of words)
#                   vector for y (labels)
#       testing :   vector for X (n samples of list of words)
#   write the result (prediction for testing dataframe) in a csv file
#==============================================================================

def unigramtfidf(dataTrain, dataTest):
    vectorizer = TfidfVectorizer(min_df=MinDf, max_df = MaxDf, sublinear_tf=True, use_idf=True)
    train = dataTrain['text']
    test = dataTest['text']
    label = dataTrain['polarity']
    trainX = vectorizer.fit_transform(train)
    testX = vectorizer.transform(test)
    clf = SGDClassifier(loss="hinge", penalty="l1")
    clf.fit(trainX, label)
    result = clf.predict(testX)
    fichier = open("unigramtfidf.output.txt", 'w')
    for elt in result:
        fichier.write(str(elt) + '\n')
    fichier.close()
#dataTrain = pd.read_csv(trainFile, encoding='latin1')
#dataTest = pd.read_csv(testFile, encoding='latin1')
#unigramtfidf(dataTrain, dataTest)

#==============================================================================
# Bigram with tf-idf method
#   input : dataframes for train and test
#       training :  vector for X (n samples of list of words)
#                   vector for y (labels)
#       testing :   vector for X (n samples of list of words)
#   write the result (prediction for testing dataframe) in a csv file
#==============================================================================

def bigramtfidf(dataTrain, dataTest):
    vectorizer = TfidfVectorizer(min_df=MinDf, max_df = MaxDf, ngram_range=(1,2), sublinear_tf=True, use_idf=True)
    train = dataTrain['text']
    test = dataTest['text']
    label = dataTrain['polarity']
    trainX = vectorizer.fit_transform(train)
    testX = vectorizer.transform(test)
    clf = SGDClassifier(loss="hinge", penalty="l1")
    clf.fit(trainX, label)
    result = clf.predict(testX)
    fichier = open("bigramtfidf.output.txt", 'w')
    for elt in result:
        fichier.write(str(elt) + '\n')
    fichier.close()
#dataTrain = pd.read_csv(trainFile, encoding='latin1')
#dataTest = pd.read_csv(testFile, encoding='latin1')
#bigramtfidf(dataTrain, dataTest)

#==============================================================================
# The main function to compile everything
#==============================================================================

def main():
#    # construct the imdb_tr.csv
#    imdb_data_preprocess(train_path)
#    imdb_data_preprocessTest(test_path)
    dataTrain = pd.read_csv('imdb_tr.csv', encoding='latin1')
    dataTest = pd.read_csv('imdb_te.csv', encoding='latin1')
    unigram(dataTrain, dataTest)
    bigram(dataTrain, dataTest)
    unigramtfidf(dataTrain, dataTest)
    bigramtfidf(dataTrain, dataTest)

if (__name__ == '__main__'):
    main()

Python/Artificial Intelligence/Project 5 - Natural Language Processing/driver_3.py


#Trial and Appeal Board

desc = 'trade_t_ab'
data = {

    'ttab' : [
        #global header fields
        ['version_no', 'string'], #version/version-no
        ['version_date', 'string'], #version/version-date
        ['action_key_code', 'string'], #action-key-code
        ['transaction_date', 'string'], #transaction-date
        ['data_available_code', 'string'], #proceeding-information/data-available-code

        # iterate over #proceeding-information/proceeding-entry
        ['number', 'string'], #number
        ['type_code', 'string'], # type-code
        ['ttab_id', 'string'], # number + type_code

        ['filing_date', 'string'], #filing-date
        ['employee_number', 'string'], #employee-number
        ['interlocutory_attorney_name', 'string'], #interlocutory-attorney-name
        ['location_code', 'string'], #location-code
        ['day_in_location', 'string'], #day-in-location
        ['charge_to_location_code', 'string'], #charge-to-location-code
        ['charge_to_employee_name', 'string'], #charge-to-employee-name
        ['status_update_date', 'string'], #status-update-date
        ['status_code', 'string'] #status-code

    ],

    # iterate over prosecution-history/prosecution-entry
    'ttab_prosecution_history' : [
        ['ttab_id', 'string'],
        ['identifier', 'string'] ,#identifier
        ['code', 'string'] ,# code
        ['type_code', 'string'] ,# type-code
        ['due_date', 'string'] ,# due-date
        ['date', 'string'] ,#  date
        ['history_text', 'string'] ,#  history-text

    ],
    # next 3 interate after each other

    # iterate over party-information/party
    'ttab_party' : [
        ['ttab_id', 'string'],
        ['party_id', 'string'], # identifer
        ['identifier', 'string'], #identifier
        ['role_code', 'string'], #role-code
        ['name', 'string'], #name
        ['orgname', 'string']#orgname

    ],
    # iterate over property-information/property
    'ttab_party_property' : [
        ['ttab_id', 'string'],
        ['party_id', 'string'], # identifer
        ['identifier', 'string'], #identifier
        ['serial_number', 'string'], #serial-number
        ['registration_number', 'string'], #registration-number
        ['mark_text', 'string']#mark-text
    ],

    #iterate over  address-information/proceeding-address
    'ttab_party_proceeding_address' : [
        ['ttab_id', 'string'],
        ['party_id', 'string'],
        ['identifier', 'string'] , #identifier
        ['type_code', 'string'] , #type-code
        ['name', 'string'] , #name
        ['orgname', 'string'] , #orgname
        ['address_1', 'string'] ,  #address-1
        ['city', 'string'] , #city
        ['state', 'string'] , #state
        ['country', 'string'] , #country
        ['postcode', 'string']  #postcode
    ]

}


# -*- coding: utf-8 -*-
from ClaseManejadorAlum import ManejadorA
from ClaseMenu import Menu

if __name__=='__main__':

    alum = ManejadorA() #lista camion
    alum.AgregarAl()
    print(alum)

    menu = Menu()

    salir = False
    while not salir:
        print("\n-----------------Menu--------------------")
        print(" 1 - Ver lista de alumnos que superan la inasistencia permitida")
        print(" 2 - Modificar cantidad maxima de inasistencias")
        print(" 3 - Salir")
        op = int(input(' Ingrese una opcion: '))
        menu.opcion(op,alum)
        if op == 3:
            salir = True


from tabulate import tabulate
import numpy as np
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
import tensorflow as tf
tf.compat.v1.logging.set_verbosity(tf.compat.v1.logging.ERROR)
from sklearn import metrics
import random
from random import randint
import time
import os

def load_X(X_path):
    file = open(X_path, 'r')
    X_ = np.array( # _ is for object
        [elem for elem in [row.split(',') for row in file]],  # parsing the text file
        dtype=np.float32
    )
    file.close()
    blocks = int(len(X_) / n_steps)
    X_ = np.array(np.split(X_,blocks)) # shape will be arrays of length 33
    return X_

# Load the networks outputs
def load_y(y_path):
    file = open(y_path, 'r')
    y_ = np.array(
        [elem for elem in [row.replace('  ', ' ').strip().split(' ') for row in file]],
        dtype=np.int32
    )
    file.close()
    # for 0-based indexing
    return y_ - 1

# Preparing dataset:
# Useful Constants
# Output classes to learn how to classify
LABELS = [
    "JUMPING",
    "JUMPING_JACKS",
    "BOXING",
    "WAVING_2HANDS",
    "WAVING_1HAND",
    "CLAPPING_HANDS"
]
DATASET_PATH = "data/HAR_pose_activities/database/"
X_train_path = DATASET_PATH + "X_train.txt"
X_test_path = DATASET_PATH + "X_test.txt"
y_train_path = DATASET_PATH + "Y_train.txt"
y_test_path = DATASET_PATH + "Y_test.txt"
X_val_path = DATASET_PATH + "X_val.txt"
n_steps = 32 # 32 timesteps per series
# A training step is one gradient update.
X_train = load_X(X_train_path)
X_test = load_X(X_test_path)
y_train = load_y(y_train_path)
y_test = load_y(y_test_path)
X_val = load_X(X_val_path)
# ===================== Parameters ======================= #
# Input Data
training_data_count = len(X_train)  # 4519 training series (with 50% overlap between each serie)
test_data_count = len(X_test)  # 1197 test series
n_input = len(X_train[0][0])  # num input parameters per timestep
n_hidden = 34 # Hidden layer num of features
n_classes = 6

#updated for learning-rate decay
# calculated as: decayed_learning_rate = learning_rate * decay_rate ^ (global_step / decay_steps)
decaying_learning_rate = True
learning_rate = 0.0025 #used if decaying_learning_rate set to False
init_learning_rate = 0.0025
decay_rate = 0.96 #the base of the exponential in the decay
decay_steps = 100000 #used in decay every 60000 steps with a base of 0.96
global_step = tf.Variable(0, trainable=False)
lambda_loss_amount = 0.0015
training_iters = training_data_count*1  # Loop 300 times on the dataset, ie 300 epochs
batch_size = 256
display_iter = batch_size*8  # To show test set accuracy during training

# Load the networks inputs

def placeholders_for_input_data():
    '''
        placeholder
        Assigning variable to memory and defining its type and dimensions ie row: none and column and depth
        an example would be an RBG image with [k, m, 3] to accomodate for k x m resolution and 3 colour channel for the pixel
        Note: Differs from Variable as variables are meant to be trained and changed over time and must have an initial often random number
        whereas placeholder can have no initial value and once assigned does not change
    '''
    x = tf.placeholder(tf.float32, [None, n_steps, n_input], name = "x")
    y = tf.placeholder(tf.float32, [None, n_classes], name = "y")
    return x,y

def assign_random_weights_and_biases_as_variables():
    '''
        Variable
        Variables store tensors. Specific ops allow you to read and modify the values of this tensor.
        TensorFlow variables use lazy loading. Therefore, if you want to see the value of var, you have to create a session
        print(sess.run(var))
        When you train a model, you use variables to hold and update parameters.
        Here we are assigning random weights and biases to start with, by default trainable = True
        and it is added to a global variable somewhere
        The get_variable() function gets an existing variable with the given parameters or creates a new one.
    '''
    weights = {
        'hidden': tf.Variable(tf.random_normal([n_input, n_hidden]), name= "weights_hidden"),
        'out': tf.Variable(tf.random_normal([n_hidden, n_classes], mean=1.0, name= "weights_out"))
    }
    biases = {
        'hidden': tf.Variable(tf.random_normal([n_hidden]), name = "biases_hidden"),
        'out': tf.Variable(tf.random_normal([n_classes]), name = "biases_out")
    }
    return weights, biases

# Utility functions for training:
def LSTM_RNN(_X, _weights, _biases):
    # model architecture based on "guillaume-chevalier" and "aymericdamien" under the MIT license.
    _X = tf.transpose(_X, [1, 0, 2])  # permute n_steps and batch_size
    _X = tf.reshape(_X, [-1, n_input])
    # Rectifies Linear Unit activation function used
    _X = tf.nn.relu(tf.matmul(_X, _weights['hidden']) + _biases['hidden'])
    # Split data because rnn cell needs a list of inputs for the RNN inner loop
    _X = tf.split(_X, n_steps, 0)

    # Define two stacked LSTM cells (two recurrent layers deep) with tensorflow
    lstm_cell_1 = tf.contrib.rnn.BasicLSTMCell(n_hidden, forget_bias=1.0, state_is_tuple=True)
    lstm_cell_2 = tf.contrib.rnn.BasicLSTMCell(n_hidden, forget_bias=1.0, state_is_tuple=True)
    lstm_cells = tf.contrib.rnn.MultiRNNCell([lstm_cell_1, lstm_cell_2], state_is_tuple=True)
    outputs, states = tf.contrib.rnn.static_rnn(lstm_cells, _X, dtype=tf.float32)

    # A single output is produced, in style of "many to one" classifier, refer to http://karpathy.github.io/2015/05/21/rnn-effectiveness/ for details
    lstm_last_output = outputs[-1]
    model = tf.matmul(lstm_last_output, _weights['out']) + _biases['out'] # y = W*x + B
    model = tf.identity(model, name="model")
    # Linear activation
    return model

def cost_function():
    '''
        Loss
        Loss is the value the model aims to reduce. If you recall we test the final layer (output) against
        ground truth and the difference squared was the backpropogation input that would correct the network
        by changing weights and biases proportionately
        L1 Loss Function is mean absolute deviation(MAD) |y2-y1|/n
        L2 Loss Function is mean squared error(MSE) (y2-y1)^2/n
        L2 would be bad with outliers as the difference is squared so the corrective movement will be large
        Softmax
        Softmax is a normalization function that reduces input from 0 to 1.
        It is commonly used as standard normalisation just takes into account proportion (1,2) --> (0.33,0.66)
        but softmax also takes size of data into account so (10,20) --> (0.000045, 0.99) which is useful in machine learning
        as outputs are higher when for eg the picture is clearer and more neurons are fired
        Cross entropy
        Cross entropy is another loss function that computes the loss based on the log difference between predicted value and
        true prediction value
        So in this case softmax outputs 0 to 1 and cross entropy computes the loss using that value.
        Logits just means unnormalized log probabilities
        We also add L2 Regularization it penalises the loss function and reduces the weights for
        It works on assumption that smaller weights generate simpler model and thus helps avoid overfitting.

    '''
    l2_regularization = lambda_loss_amount * sum(tf.nn.l2_loss(tf_var) for tf_var in tf.trainable_variables()) # L2 loss prevents this overkill neural network to overfit the data
    cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y, logits=model)) + l2_regularization # Softmax loss
    return cost

def get_optimizer():
    '''
        Given an algorithm f(x), an optimization algorithm help in either minimizing or maximizing the value of f(x)
        We use the Adam optimiser here instead of stochastic gradient descent
    '''
    optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(cost,global_step=global_step) # Adam Optimizer
    return optimizer

def get_accuracy(model):
    '''
        If equal return true
        Argmax returns index of biggest element in tensor, argmax(array,axis) axis of 1 is columns, axis of 0 is rows
        For example tensor returned is [3,4,1] then equal() is run and produces tensor of (1,1,1) if all is equal
    '''
    correct_pred = tf.equal(tf.argmax(model,1), tf.argmax(y,1))
    accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32)) # cast changes datatype
    return accuracy

def extract_batch_size(_train, _labels, _unsampled, batch_size):
    # Fetch a "batch_size" amount of data and labels from "(X|y)_train" data.
    # Elements of each batch are chosen randomly, without replacement, from X_train with corresponding label from Y_train
    # unsampled_indices keeps track of sampled data ensuring non-replacement. Resets when remaining datapoints < batch_size
    shape = list(_train.shape)
    shape[0] = batch_size
    batch_s = np.empty(shape)
    batch_labels = np.empty((batch_size,1))
    _unsampled = list(_unsampled)
    # TODO replace this with library function
    for i in range(batch_size):
        index = random.choice(_unsampled)
        batch_s[i] = _train[index]
        batch_labels[i] = _labels[index]
        _unsampled.remove(index) # remove indexes to be sampled
    return batch_s, batch_labels, _unsampled

def train_model():
    test_losses = []
    test_accuracies = []
    train_losses = []
    train_accuracies = []
    sess = tf.InteractiveSession(config=tf.ConfigProto(log_device_placement=True))
    # interactive session allows you to stop and evaluate and change the graph at any time
    # this delayed execution is one of the advantages of tensorflow
    init = tf.global_variables_initializer()
    # Variable need to be "initialised" in tensorflow, creation is not enough
    sess.run(init)
    # Perform Training steps with "batch_size" amount of data at each loop.
    # Elements of each batch are chosen randomly, without replacement, from X_train,
    # restarting when remaining datapoints < batch_size
    step = 1
    unsampled_indices = range(0,len(X_train))

    while step * batch_size <= training_iters:
        #print (sess.run(learning_rate)) #decaying learning rate
        #print (sess.run(global_step)) # global number of iterations
        if len(unsampled_indices) < batch_size:
            unsampled_indices = range(0,len(X_train))
        batch_xs, raw_labels, unsampled_indicies = extract_batch_size(X_train, y_train, unsampled_indices, batch_size)
        # print(raw_labels) # in index 1-6
        batch_ys = one_hot(raw_labels)
        # print(batch_ys) # in array of arrays of 0s with 1 at the index
        # check that encoded output is same length as num_classes, if not, pad it
        # (?) must i encode the labels
        if len(batch_ys[0]) < n_classes:
            temp_ys = np.zeros((batch_size, n_classes)) # (256, 6)
            temp_ys[:batch_ys.shape[0],:batch_ys.shape[1]] = batch_ys
            # [:] is slice operator [:,:] is slice row, slice column, replaces those values with batch_ys
            batch_ys = temp_ys
        # Fit training using batch data
        '''
            session.run(fetches=,feed_dict=)
            fetches are operations, the things that need to be computed, so for example if W is a constant and x a Variable,
            Wx = tf.multiply(W,x) is an operation/node that is to be computed. So sess.run(Wx, feed_dict={x: [3,33]})
            will return Wx.
            underscore is the optimizer but it is not needed.
            cost function returns the loss
            accuracy returns the accuracy
            apparently these functions(nodes) are global or at least appended to the graph already
            Inside the run function we are computing 3 operations, optimizer, cost and accuracy
            cost computes the loss based on the model and ground truth (can replace accuracy?)
            optimizer works to reduce cost
            accuracy evaluates the accuracy based on the difference between predicted value and ground truth
            feed dict provides input to the placeholders x and y
            W and B gets changed because first cost function produces loss from the last layer of the model y = Wx+B
            Then that is fed into the optimizer which reduces the loss by changing the weights and bias from the last layer of the model
            and then it backpropogates until all the nodes are visited through the concept of automatic differentiation which simply
            and probably inaccurately explained is that the gradient of each sequential small operation like + or - can be obtained
            and combined via chain rule.
        '''
        _, loss, acc = sess.run(
            [optimizer, cost, accuracy],
            feed_dict={
                # y = W*x + B
                x: batch_xs,
                y: batch_ys
            }
        )
        train_losses.append(loss)
        train_accuracies.append(acc)

        # Evaluate network only at some steps for faster training:
        if (step*batch_size % display_iter == 0) or (step == 1) or (step * batch_size > training_iters):
            # To not spam console, show training accuracy/loss in this "if"
            print("Iter no." + str(step) + ":  Learning rate = " + "{:.6f}".format(sess.run(learning_rate)) + ":   Batch Loss = " + "{:.6f}".format(loss) + ", Accuracy = {}".format(acc))
            # Evaluation on the test set (no learning made here - just evaluation for diagnosis)
            loss, acc = sess.run(
                [cost, accuracy],
                feed_dict={
                    x: X_test,
                    y: one_hot(y_test)
                }
            )
            test_losses.append(loss)
            test_accuracies.append(acc)
            print("Performance on test set:" + "Batch Loss = {}".format(loss) + ", Accuracy = {}".format(acc))
        step += 1
    print("Optimization Finished!")
    return sess, test_accuracies, test_losses, train_accuracies, train_losses

def one_hot(y_):
    # One hot encoding of the network outputs
    # e.g.: [[5], [0], [3]] --> [[0, 0, 0, 0, 0, 1], [1, 0, 0, 0, 0, 0], [0, 0, 0, 1, 0, 0]]
    y_ = y_.reshape(len(y_))
    n_values = int(np.max(y_)) + 1
    return np.eye(n_values)[np.array(y_, dtype=np.int32)]  # Returns FLOATS

def plot_training_graph(train_accuracies,train_losses,width,height):
    font = {
        'family' : 'Bitstream Vera Sans',
        'weight' : 'bold',
        'size'   : 18
    }
    matplotlib.rc('font', **font)
    plt.figure(figsize=(width, height))
    indep_train_axis = np.array(range(batch_size, (len(train_losses)+1)*batch_size, batch_size))
    #plt.plot(indep_train_axis, np.array(train_losses),     "b--", label="Train losses")
    plt.plot(indep_train_axis, np.array(train_accuracies), "g--", label="Train accuracies")
    indep_test_axis = np.append(
        np.array(range(batch_size, len(test_losses)*display_iter, display_iter)[:-1]),
        [training_iters]
    )
    #plt.plot(indep_test_axis, np.array(test_losses), "b-", linewidth=2.0, label="Test losses")
    plt.plot(indep_test_axis, np.array(test_accuracies), "b-", linewidth=2.0, label="Test accuracies")
    plt.title("Training session's Accuracy over Iterations")
    plt.legend(loc='lower right', shadow=True)
    plt.ylabel('Training Accuracy')
    plt.xlabel('Training Iteration')
    plt.savefig("graph.png")

def plot_confusion_matrix(data, width, height):
    #cmap = plt.cm.Blues
    plt.title("Confusion matrix \n(normalised to % of total test data)")
    plt.matshow(normalised_confusion_matrix)
    plt.colorbar()
    tick_marks = np.arange(n_classes)
    plt.xticks(tick_marks, LABELS, rotation=90)
    plt.yticks(tick_marks, LABELS)
    plt.tight_layout()
    plt.ylabel('True label')
    plt.xlabel('Predicted label')
    plt.savefig("confusion_matrix.png")

def predict(sess):
    # Now, you run this with fine-tuning data in sess.run()
    # preds = sess.run(
    #     [model],
    #     feed_dict={
    #         x: X_val
    #     }
    # )
    prediction=tf.argmax(model,1)
    prediction = prediction.eval(feed_dict={x: X_val},session=sess)
    prediction = LABELS[prediction[0]]
    print ("Prediction is: {}".format(prediction))

if __name__ == "__main__":
    # Replace labelled classes with random classes to train on
    # for i in range(len(y_train)):
    #     y_train[i] = randint(0, 5)
    print(tabulate([[X_train.shape, y_test.shape, np.mean(X_test), np.std(X_test)]],
    headers=['X shape', 'y shape','X mean', 'X standard deviation' ]))
    # with tf.device('/gpu:0'):
    # Graph input/output
    x,y = placeholders_for_input_data()
    # Graph weights
    weights, biases = assign_random_weights_and_biases_as_variables()
    model = LSTM_RNN(x, weights, biases)
    # Loss, optimizer and evaluation
    cost = cost_function()
    if decaying_learning_rate:
        learning_rate = tf.train.exponential_decay(init_learning_rate, global_step*batch_size, decay_steps, decay_rate, staircase=True)
    optimizer = get_optimizer()
    accuracy = get_accuracy(model)
    # Train the network:
    time_start = time.time()
    sess, test_accuracies, test_losses, train_accuracies, train_losses = train_model()
    time_stop = time.time()
    print("Total time:  {}".format(time_stop - time_start))
    # ======== Save model ========== #
    saver = tf.train.Saver()
    saver.save(sess, 'model_steps/model.ckpt')
    # Accuracy for test data
    one_hot_predictions, accuracy, final_loss = sess.run(
        [model, accuracy, cost],
        feed_dict={
            x: X_test,
            y: one_hot(y_test)
        }
    )
    test_losses.append(final_loss)
    test_accuracies.append(accuracy)
    # Results
    print("Final result: " + "Batch Loss = {}".format(final_loss) + ", Accuracy = {}".format(accuracy))
    predictions = one_hot_predictions.argmax(1)
    print(metrics.classification_report(y_test, predictions, target_names=LABELS))
    # Plot Results:
    plot_training_graph(train_accuracies, train_losses,16,16)
    confusion_matrix = metrics.confusion_matrix(y_test, predictions)
    normalised_confusion_matrix = np.array(confusion_matrix, dtype=np.float32)/np.sum(confusion_matrix)*100
    plot_confusion_matrix(normalised_confusion_matrix, 16, 16)
    prediction=tf.argmax(model,1)
    predict(sess) # must feed in the same sess to get the same result as above
    sess.close()


# -*- coding: utf-8 -*-
from genomix.fields import DisplayChoiceField
from rest_framework import serializers

from . import choices, models

class FileSerializer(serializers.ModelSerializer):
    """Serializer for Files."""

    type = DisplayChoiceField(choices=choices.FILE_TYPES)

    class Meta:
        model = models.File
        fields = (
            'id', 'name', 'description', 'url', 'path',
            'type', 'relative_path',
            'active', 'created', 'modified',
        )