"""

This app allows a user to browse through an OPT volume and mark probe track locations.

"""

import sys
from functools import partial
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QGridLayout, QFileDialog, QSlider, QLabel
import PyQt5.QtWidgets as QtWidgets
from PyQt5.QtGui import QIcon, QKeyEvent, QImage, QPixmap, QColor
from PyQt5.QtCore import pyqtSlot, Qt

from PIL import Image, ImageQt

import numpy as np
import pandas as pd

import os

DEFAULT_SLICE = 400
DEFAULT_VIEW = 0

class App(QWidget):

    def __init__(self):
        super().__init__()
        self.title = 'OPT Annotation'
        self.left = 500
        self.top = 100
        self.width = 800
        self.height = 800
        self.initUI()

    def initUI(self):
        self.setWindowTitle(self.title)
        self.setGeometry(self.left, self.top, self.width, self.height)

        grid = QGridLayout()

        self.image = QLabel()
        self.image.setObjectName("image")
        self.image.mousePressEvent = self.clickedOnImage
        im8 = Image.fromarray(np.ones((800,800),dtype='uint8')*255)
        imQt = QImage(ImageQt.ImageQt(im8))
        imQt.convertToFormat(QImage.Format_ARGB32)
        self.image.setPixmap(QPixmap.fromImage(imQt))
        grid.addWidget(self.image, 0, 0)

        self.slider = QSlider(Qt.Horizontal)
        self.slider.setMinimum(0)
        self.slider.setMaximum(1022)
        self.slider.setValue(DEFAULT_SLICE)
        self.slider.setTickPosition(QSlider.TicksBelow)
        self.slider.setTickInterval(100)
        self.slider.valueChanged.connect(self.sliderMoved)
        grid.addWidget(self.slider, 1,0)

        self.slider_values = [DEFAULT_SLICE, DEFAULT_SLICE, DEFAULT_SLICE]

        subgrid = QGridLayout()

        self.probes = ('A1', 'B1', 'C1', 'D1', 'E1', 'F1',
                       'A2', 'B2', 'C2', 'D2', 'E2', 'F2')

        self.probe_map = {'Probe A1': 0, 'Probe B1': 1, 'Probe C1': 2, 'Probe D1' : 3,
                          'Probe E1': 4, 'Probe F1': 5,
                          'Probe A2': 6, 'Probe B2': 7, 'Probe C2': 8, 'Probe D2' : 9,
                          'Probe E2': 10, 'Probe F2': 11,
                     }

        self.color_map = {'Probe A1': 'darkred', 'Probe B1': 'cadetblue',
                     'Probe C1': 'goldenrod', 'Probe D1' : 'darkgreen',
                     'Probe E1': 'darkblue', 'Probe F1': 'blueviolet',
                     'Probe A2': 'red', 'Probe B2': 'darkturquoise',
                     'Probe C2': 'yellow', 'Probe D2' : 'green',
                     'Probe E2': 'blue', 'Probe F2': 'violet'}

        self.probe_buttons = [QPushButton('Probe ' + i) for i in self.probes]

        for i, button in enumerate(self.probe_buttons):
            button.setToolTip('Annotate ' + button.text())
            button.clicked.connect(partial(self.selectProbe, button))
            subgrid.addWidget(button, i//6, i % 6)

        self.coronal_button = QPushButton('Coronal', self)
        self.coronal_button.setToolTip('Switch to coronal view')
        self.coronal_button.clicked.connect(self.viewCoronal)

        self.horizontal_button = QPushButton('Horizontal', self)
        self.horizontal_button.setToolTip('Switch to horizontal view')
        self.horizontal_button.clicked.connect(self.viewHorizontal)

        self.sagittal_button = QPushButton('Sagittal', self)
        self.sagittal_button.setToolTip('Switch to sagittal view')
        self.sagittal_button.clicked.connect(self.viewSagittal)

        self.current_view = DEFAULT_VIEW

        subgrid.addWidget(self.coronal_button,2,0,1,2)
        subgrid.addWidget(self.horizontal_button,2,2,1,2)
        subgrid.addWidget(self.sagittal_button,2,4,1,2)

        save_button = QPushButton('Save', self)
        save_button.setToolTip('Save values as CSV')
        save_button.clicked.connect(self.saveData)

        load_button = QPushButton('Load', self)
        load_button.setToolTip('Load volume data')
        load_button.clicked.connect(self.loadData)

        subgrid.addWidget(save_button,3,4)
        subgrid.addWidget(load_button,3,5)

        grid.addLayout(subgrid,2,0)

        self.current_directory = '/mnt/md0/data/opt/production'

        self.data_loaded = False

        self.selected_probe = None

        self.setLayout(grid)
        self.viewCoronal()
        self.show()

    def keyPressEvent(self, e):

        if e.key() == Qt.Key_A:
            self.selectProbe(self.probe_buttons[0])
        if e.key() == Qt.Key_B:
            self.selectProbe(self.probe_buttons[1])
        if e.key() == Qt.Key_C:
            self.selectProbe(self.probe_buttons[2])
        if e.key() == Qt.Key_D:
            self.selectProbe(self.probe_buttons[3])
        if e.key() == Qt.Key_E:
            self.selectProbe(self.probe_buttons[4])
        if e.key() == Qt.Key_F:
            self.selectProbe(self.probe_buttons[5])
        if e.key() == Qt.Key_1:
            self.selectProbe(self.probe_buttons[6])
        if e.key() == Qt.Key_2:
            self.selectProbe(self.probe_buttons[7])
        if e.key() == Qt.Key_3:
            self.selectProbe(self.probe_buttons[8])
        if e.key() == Qt.Key_4:
            self.selectProbe(self.probe_buttons[9])
        if e.key() == Qt.Key_5:
            self.selectProbe(self.probe_buttons[10])
        if e.key() == Qt.Key_6:
            self.selectProbe(self.probe_buttons[11])
        if e.key() == Qt.Key_Backspace:
            self.deletePoint()

    def deletePoint(self):

        if self.selected_probe is not None:

            if self.current_view == 0:
                matching_index = self.annotations[(self.annotations.AP == self.slider.value()) &
                                                       (self.annotations.probe_name == self.selected_probe)].index.values
            elif self.current_view == 1:
                matching_index = self.annotations[(self.annotations.DV == self.slider.value()) &
                                                       (self.annotations.probe_name == self.selected_probe)].index.values
            elif self.current_view == 2:
                matching_index = self.annotations[(self.annotations.ML == self.slider.value()) &
                                                       (self.annotations.probe_name == self.selected_probe)].index.values

            if len(matching_index) > 0:
                self.annotations = self.annotations.drop(index=matching_index)

                self.saveData()

                self.refreshImage()

    def clickedOnImage(self , event):

        if self.data_loaded:
            x = int(event.pos().x() * 1024 / 800)
            y = int(event.pos().y() * 1024 / 800)

            #print('X: ' + str(x))
            #print('Y: ' + str(y))

            if self.selected_probe is not None:
                #print('updating volume')

                if self.current_view == 0:
                    AP = self.slider.value()
                    DV = y
                    ML = x
                    matching_index = self.annotations[(self.annotations.AP == AP) &
                                                       (self.annotations.probe_name ==
                                                        self.selected_probe)].index.values
                elif self.current_view == 1:
                    AP = y
                    DV = self.slider.value()
                    ML = x
                    matching_index = self.annotations[(self.annotations.DV == DV) &
                                                       (self.annotations.probe_name ==
                                                        self.selected_probe)].index.values
                elif self.current_view == 2:
                    AP = 1023 - x
                    DV = y
                    ML = self.slider.value()
                    matching_index = self.annotations[(self.annotations.ML == ML) &
                                                       (self.annotations.probe_name ==
                                                        self.selected_probe)].index.values

                if len(matching_index) > 0:
                    self.annotations = self.annotations.drop(index=matching_index)

                self.annotations = self.annotations.append(pd.DataFrame(data = {'AP' : [AP],
                                    'ML' : [ML],
                                    'DV': [DV],
                                    'probe_name': [self.selected_probe]}),
                                    ignore_index=True)

                self.saveData()

                self.refreshImage()

    def selectProbe(self, b):

        for button in self.probe_buttons:
            button.setStyleSheet("background-color: white")

        b.setStyleSheet("background-color: " + self.color_map[b.text()])

        self.selected_probe = b.text()

    def sliderMoved(self):

        self.slider_values[self.current_view] = self.slider.value()
        self.refreshImage()

    def viewCoronal(self):

        self.current_view = 0
        self.slider.setValue(self.slider_values[self.current_view])
        self.coronal_button.setStyleSheet("background-color: gray")
        self.horizontal_button.setStyleSheet("background-color: white")
        self.sagittal_button.setStyleSheet("background-color: white")
        self.refreshImage()

    def viewHorizontal(self):

        self.current_view = 1
        self.slider.setValue(self.slider_values[self.current_view])
        self.coronal_button.setStyleSheet("background-color: white")
        self.horizontal_button.setStyleSheet("background-color: gray")
        self.sagittal_button.setStyleSheet("background-color: white")
        self.refreshImage()

    def viewSagittal(self):

        self.current_view = 2
        self.slider.setValue(self.slider_values[self.current_view])
        self.coronal_button.setStyleSheet("background-color: white")
        self.horizontal_button.setStyleSheet("background-color: white")
        self.sagittal_button.setStyleSheet("background-color: gray")
        self.refreshImage()

    def refreshImage(self):

        colors = ('darkred', 'orangered', 'goldenrod',
            'darkgreen', 'darkblue', 'blueviolet',
            'red','orange','yellow','green','blue','violet')

        if self.data_loaded:
            plane = np.take(self.volume,
                 self.slider.value(),
                 axis=self.current_view)
            if self.current_view == 2:
                plane = plane.T
            im8 = Image.fromarray(plane)
        else:
            im8 = Image.fromarray(np.ones((1024,1024),dtype='uint8')*255)

        imQt = QImage(ImageQt.ImageQt(im8))
        imQt = imQt.convertToFormat(QImage.Format_RGB16)

        #print(self.current_view)
        #print(self.slider.value())

        if self.data_loaded:
            for idx, row in self.annotations.iterrows():

                if self.current_view == 0:
                    shouldDraw = row.AP == self.slider.value()
                    x = row.ML
                    y = row.DV
                elif self.current_view == 1:
                    shouldDraw = row.DV == self.slider.value()
                    x = row.ML
                    y = row.AP
                elif self.current_view == 2:
                    shouldDraw = row.ML == self.slider.value()
                    x = 1023 - row.AP
                    y = row.DV

                if shouldDraw:
                    color = QColor(self.color_map[row.probe_name])

                    for j in range(x-10,x+10):
                        for k in range(y-10,y+10):
                            if pow(j-x,2) + pow(k-y,2) < 20:
                                imQt.setPixelColor(j,k,color)

        pxmap = QPixmap.fromImage(imQt).scaledToWidth(800).scaledToHeight(800)
        self.image.setPixmap(pxmap)

    def loadData(self):

        fname, filt = QFileDialog.getOpenFileName(self,
            caption='Select volume file',
            directory=self.current_directory,
            filter='*nc.001')

        print(fname)

        self.current_directory = os.path.dirname(fname)
        self.output_file = os.path.join(self.current_directory, 'probe_annotations.csv')

        if fname.split('.')[-1] == '001':

            self.volume = self.loadVolume(fname)
            self.data_loaded = True
            self.setWindowTitle(os.path.basename(fname))

            if os.path.exists(self.output_file):
                self.annotations = pd.read_csv(self.output_file, index_col=0)
            else:
                self.annotations = pd.DataFrame(columns = ['AP','ML','DV', 'probe_name'])

            self.refreshImage()

        else:
            print('invalid file')

    def saveData(self):

        if self.data_loaded:
            self.annotations.to_csv(self.output_file)

    def loadVolume(self, fname, _dtype='u1', num_slices=1023):

        dtype = np.dtype(_dtype)

        volume = np.fromfile(fname, dtype) # read it in

        z_size = np.sum([volume[1], volume[2] << pow(2,3)])
        x_size = np.sum([(val << pow(2,i+1)) for i, val in enumerate(volume[8:4:-1])])
        y_size = np.sum([(val << pow(2,i+1)) for i, val in enumerate(volume[12:8:-1])])

        fsize = np.array([z_size, x_size, y_size]).astype('int')

        volume = np.reshape(volume[13:], fsize) # remove 13-byte header and reshape

        print("Data loaded.")

        return volume

if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = App()
    sys.exit(app.exec_())


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

from django.db import models, migrations

class Migration(migrations.Migration):

    dependencies = [
    ]

    operations = [
        migrations.CreateModel(
            name='Topic',
            fields=[
                ('id', models.AutoField(serialize=False, primary_key=True, verbose_name='ID', auto_created=True)),
                ('name', models.CharField(max_length=128)),
                ('desc', models.TextField(max_length=8192, null=True, blank=True)),
                ('pic', models.ImageField(upload_to='/topics/pics')),
            ],
        ),
    ]


import requests
from bs4 import BeautifulSoup as BS

def parse_price(url, default='0'):
    r = requests.get(url)
    soup = BS(r.text, 'lxml')
    quotes = soup.find_all('div', class_='b-pmi-col-left')
    result_price = False

    for el in quotes:
        if el:
            res = el.find(class_='b-price')
        if res:
            result_price = res.text[:-4]

    if result_price :
        print(result_price)
        return result_price

    if result_price == False:
        print('We have problem in parse or product is out of stock')
        return default

# parse_price('https://telemart.ua/products/intel-core-i5-12600k-3449ghz-s1700-tray/')

# def result_sum(*links):
#     res = 0
#     for el in links:
#       res += int((parse_price(el)).replace(' ', ''))
#     print(type(res))
#     print(res)
#     return res

# result_sum('https://telemart.ua/products/intel-core-i5-12600k-3449ghz-s1700-tray/', 'https://telemart.ua/products/asus-tuf-gaming-gt501-rgb-bez-bp-90dc0012-b49000-black/', 'https://telemart.ua/products/1stplayer-v3-a-4g6-bez-bp-black/')


# Exercise 6: Display numbers divisible by 5 from a list
n = int(input("Enter number of elements: "))
a = list(map(int,
             input("\nEnter the numbers : ").strip().split()))[:n]

print("\nGiven list is ", a)

for i in a:
    if i % 5 == 0:
        print(i)


def matrixElementsSum(matrix):
    sum = 0
    for i in range(0,len(matrix)-1):
        for j in range(0,len(matrix[i])):
            if matrix[i][j] == 0:
                matrix[i+1][j] = 0
    for row in matrix:
        for element in row:
            sum = sum + element
    return sum


class Solution:
    def getStats(self, s):
        rel = ""
        ctr = 0
        prefl = []
        prefr = []
        for c in s:
            if c != "_":
                rel += c
                if c == "L":
                    prefl.append(ctr)
                else:
                    prefr.append(ctr)
            else:
                ctr += 1
        return [rel, ctr, prefl, prefr]

    def canChange(self, start: str, target: str) -> bool:
        rels, ctrs, sprefl, sprefr = self.getStats(start)
        relt, ctrt, tprefl, tprefr = self.getStats(target)
        if rels != relt or ctrs != ctrt or len(sprefl) != len(tprefl) or len(sprefr) != len(tprefr):
            return False
        m = len(sprefl)
        for i in range(m):
            if tprefl[i] > sprefl[i]:
                return False
        n = len(sprefr)
        for i in range(n):
            if tprefr[i] < sprefr[i]:
                return False
        return True


from __future__ import print_function

import math
import traceback
import xml.etree.ElementTree as ET
import numpy.random as random
import sys

import py_trees

import carla

from agents.navigation.local_planner import RoadOption

# pylint: disable=line-too-long
from srunner.scenarioconfigs.scenario_configuration import ScenarioConfiguration, ActorConfigurationData
# pylint: enable=line-too-long
from srunner.scenariomanager.carla_data_provider import CarlaDataProvider
from srunner.scenariomanager.scenarioatomics.atomic_behaviors import Idle, ScenarioTriggerer
from srunner.scenarios.basic_scenario import BasicScenario
from srunner.tools.route_parser import RouteParser, TRIGGER_THRESHOLD, TRIGGER_ANGLE_THRESHOLD
from srunner.tools.route_manipulation import interpolate_trajectory
from srunner.tools.py_trees_port import oneshot_behavior

from srunner.scenarios.control_loss import ControlLoss
from srunner.scenarios.follow_leading_vehicle import FollowLeadingVehicle
from srunner.scenarios.object_crash_vehicle import DynamicObjectCrossing
from srunner.scenarios.object_crash_intersection import VehicleTurningRoute
from srunner.scenarios.other_leading_vehicle import OtherLeadingVehicle
from srunner.scenarios.maneuver_opposite_direction import ManeuverOppositeDirection
from srunner.scenarios.junction_crossing_route import SignalJunctionCrossingRoute, NoSignalJunctionCrossingRoute

from srunner.scenariomanager.scenarioatomics.atomic_criteria import (CollisionTest,
                                                                     InRouteTest,
                                                                     RouteCompletionTest,
                                                                     OutsideRouteLanesTest,
                                                                     RunningRedLightTest,
                                                                     RunningStopTest,
                                                                     ActorSpeedAboveThresholdTest)

SECONDS_GIVEN_PER_METERS = 0.4

NUMBER_CLASS_TRANSLATION = {
    "Scenario1": ControlLoss,
    "Scenario2": FollowLeadingVehicle,
    "Scenario3": DynamicObjectCrossing,
    "Scenario4": VehicleTurningRoute,
    "Scenario5": OtherLeadingVehicle,
    "Scenario6": ManeuverOppositeDirection,
    "Scenario7": SignalJunctionCrossingRoute,
    "Scenario8": SignalJunctionCrossingRoute,
    "Scenario9": SignalJunctionCrossingRoute,
    "Scenario10": NoSignalJunctionCrossingRoute  # wut :3
}

def convert_json_to_transform(actor_dict):
    """
    Convert a JSON string to a CARLA transform
    """
    return carla.Transform(location=carla.Location(x=float(actor_dict['x']), y=float(actor_dict['y']),
                                                   z=float(actor_dict['z'])),
                           rotation=carla.Rotation(roll=0.0, pitch=0.0, yaw=float(actor_dict['yaw'])))

def convert_json_to_actor(actor_dict):
    """
    Convert a JSON string to an ActorConfigurationData dictionary
    """
    node = ET.Element('waypoint')
    node.set('x', actor_dict['x'])
    node.set('y', actor_dict['y'])
    node.set('z', actor_dict['z'])
    node.set('yaw', actor_dict['yaw'])

    return ActorConfigurationData.parse_from_node(node, 'simulation')  # return object of ActorConfigurationData(model, transform, rolename, speed, autopilot, random_location, color) ActorConfigurationData

def convert_transform_to_location(transform_vec):
    """
    Convert a vector of transforms to a vector of locations
    """
    location_vec = []
    for transform_tuple in transform_vec:
        location_vec.append((transform_tuple[0].location, transform_tuple[1]))

    return location_vec

def compare_scenarios(scenario_choice, existent_scenario):  # if compare_scenarios(scenario_choice, existent_scenario)
    """
    Compare function for scenarios based on distance of the scenario start position
    """
    def transform_to_pos_vec(scenario):
        """
        Convert left/right/front to a meaningful CARLA position
        """
        position_vec = [scenario['trigger_position']]
        if scenario['other_actors'] is not None:
            if 'left' in scenario['other_actors']:
                position_vec += scenario['other_actors']['left']
            if 'front' in scenario['other_actors']:
                position_vec += scenario['other_actors']['front']
            if 'right' in scenario['other_actors']:
                position_vec += scenario['other_actors']['right']

            #print(position_vec)
            #sys.exit("offfff")
            '''
            Preparing scenario: RouteScenario_0
            [{'pitch': '0', 'x': 41.37, 'y': 207.4, 'yaw': 90.0, 'z': 1.21}, {'pitch': '0.0',
             'x': '75.58', 'y': '236.78', 'yaw': '180.000015', 'z': '1.21'}, {'pitch': '0.0',
             'x': '45.82', 'y': '270.45', 'yaw': '270.000031', 'z': '1.21'}]
            '''

        return position_vec

    # put the positions of the scenario choice into a vec of positions to be able to compare

    choice_vec = transform_to_pos_vec(scenario_choice)
    existent_vec = transform_to_pos_vec(existent_scenario)
    for pos_choice in choice_vec:
        for pos_existent in existent_vec:

            dx = float(pos_choice['x']) - float(pos_existent['x'])
            dy = float(pos_choice['y']) - float(pos_existent['y'])
            dz = float(pos_choice['z']) - float(pos_existent['z'])
            dist_position = math.sqrt(dx * dx + dy * dy + dz * dz)
            dyaw = float(pos_choice['yaw']) - float(pos_choice['yaw'])
            dist_angle = math.sqrt(dyaw * dyaw)
            if dist_position < TRIGGER_THRESHOLD and dist_angle < TRIGGER_ANGLE_THRESHOLD:
                return True

    return False

class RouteScenario(BasicScenario):

    """
    Implementation of a RouteScenario, i.e. a scenario that consists of driving along a pre-defined route,
    along which several smaller scenarios are triggered
    """

    def __init__(self, world, config, debug_mode=False, criteria_enable=True, timeout=300):
        """
        Setup all relevant parameters and create scenarios along route
        """

        self.config = config  # weather, json file and waypoints in the route. No ego, no other vehicle. config.agent = NpcAgent though.
        self.route = None
        self.sampled_scenarios_definitions = None

        self._update_route(world, config, debug_mode)
        # we got densly sampled route from this and the sampled scenarios as
        # self.sampled_scenarios_definitions from all towns 1 3 4 JSON file,
        # i.e., the trigger points and othervehicles location transforms and
        # locs directions as left right etc and turning info S4left etc
        # and set_ego_vehicle_route in CarlaDataProvider._ego_vehicle_route = route
        # and self.time_out as time roughly needed to complete scenario as 0.4 sec per meter

        ego_vehicle = self._update_ego_vehicle()
        #  return ego_vehicle  #  vehicle type mentioned here and role Hero also given and spawned at route[0][0]

        self.list_scenarios = self._build_scenario_instances(world,
                                                             ego_vehicle,
                                                             self.sampled_scenarios_definitions,
                                                             scenarios_per_tick=5,
                                                             timeout=self.timeout,
                                                             debug_mode=debug_mode)

        # return scenario_instance_vec

        super(RouteScenario, self).__init__(name=config.name,
                                            ego_vehicles=[ego_vehicle],
                                            config=config,
                                            world=world,
                                            debug_mode=False,
                                            terminate_on_failure=False,
                                            criteria_enable=criteria_enable)

    def _update_route(self, world, config, debug_mode):
        """
        Update the input route, i.e. refine waypoint list, and extract possible scenario locations

        Parameters:
        - world: CARLA world
        - config: Scenario configuration (RouteConfiguration)
        """

        # Transform the scenario file into a dictionary
        world_annotations = RouteParser.parse_annotations_file(config.scenario_file)  # scenario file is the JSON file # return final_dict  # the file has a current maps name that is an one element vec
        """
        Return the annotations of which positions where the scenarios are going to happen.
        :param annotation_filename: the filename for the anotations file
        :return:
        """

        #print(final_dict)

        #print(next(iter(final_dict)))  # prints Town01

        #for keys in final_dict.keys():
            #print(keys)
        '''
            prints
            Town01
            Town02
            Town03
            Town04
            Town05
            Town06
        '''
        '''
        for key, value in final_dict.items():
            print(key)  # All town numbers printed before all the array values of each town were printed.
            print(f"{key}-value are as {value}")  #Town01-value are as, then all the values of that object array, i.e., te available event configurations, transforms, other vehicle transforms, left right front transforms.
        '''

        #return final_dict  # the file has a current maps name that is an one element vec

        # prepare route's trajectory (interpolate and add the GPS route) is the route in the routes_debug.xml an example of a GPS route? Maybe.
        # Given some raw keypoints interpolate a full dense trajectory to be used by the user.  # meaning the route we have in the routes_drbug.xml file has many sparsly populated waypoints throughout the map and we will densly populate them for our AV to drive on with hop_res of 1. :3
        gps_route, route = interpolate_trajectory(world, config.trajectory)  # in config.trajectory we have  # carla location waypoints here from the routes_debug.xml routes> id = 0, town= Town02 xml!

        # following are returned from interpolate_trajectory:
        # print(connection)  # RoadOption.LANEFOLLOW
        # print(gps_point)  # {'lat': -0.0016101365263807565, 'lon': -3.104682434048109e-05, 'z': 0.0}
        # and the route with densly populated waypoints and RoadOptions

        # For this Route scenario, I see we get the densly populated route plus the RoadOptions with each densly populated waypoint. Don't know if we get that in other scenarios. Don't think so. Rather we depend on carla server
        # information so that our ego vehicle controllers can drive the vehicle to target location using saparsely populated waypoints and no RoadOptions given with those waypoints, rather infro from carla server is used by the ego veh controller.

        potential_scenarios_definitions, _ = RouteParser.scan_route_for_scenarios(config.town, route, world_annotations)  # Town02  # route as defined in the method above  # world_annotations is the dictionary of the JSON file with town names as keys for each scenario and each key returns the annotations of which positions where the scenarios are going to happen.

        # We have till "scenario_type": "Scenario4" in Town02 in the all towns 1 3 4 JSON file and other actors only in scenario 4 and I guess after scenario 4
        # and I noticed we have scenario 1, 3, 4 in all towns 1 3 4 JSON file, go figure.

        '''
                        scenario_description = {
                            'name': scenario_name,  # all the scenarios we are parsing
                            'other_actors': other_vehicles,  # all the transforms inside the other vehicles i.e., left right front
                            'trigger_position': waypoint,  # waypoint is the trigger transform  that has matched with a waypoint inside our route traj
                            'scenario_type': scenario_subtype, #  it actually is S4left etc I guess # scenario_name = scenario["scenario_type"]  # "scenario_type": "Scenario1"  # some scenarios have route dependent configs, S4left, etc
                        }
        '''

        #return possible_scenarios, existent_triggers  # existent_triggers have all the trigger points inside the, and possible scenarios have the complete description of those scenarios from all towns 1 3 4 JSOn file compared to our route

        # we dont even want the existent_triggers as we are equating them to _ as that info is also in the possible_scenarios

        """
        Just returns a plain list of possible scenarios that can happen in this route by matching
        the locations from the scenario into the route description

        :return:  A list of scenario definitions with their correspondent parameters
        """

        self.route = route  # interpolated dense traj/route with RoadOptions

        CarlaDataProvider.set_ego_vehicle_route(convert_transform_to_location(self.route))  # convert_transform_to_location(self.route) returns carla location + the RoadOption in an array of the route, instead of the waypoint object and RoadOption
        # def of this function is:
        #   CarlaDataProvider._ego_vehicle_route = route

        config.agent.set_global_plan(gps_route, self.route)  # just resampling them (by some factor) to the min distance we already wanted (of 1)

        # Sample the scenarios to be used for this route instance.
        self.sampled_scenarios_definitions = self._scenario_sampling(potential_scenarios_definitions)  # potential_scenarios_definitions is scenario_description with scenario name scenario type other vehicles trigger locations defined in the All Towns 1 3 4 JSON file
        # return sampled_scenarios

        # Timeout of scenario in seconds
        self.timeout = self._estimate_route_timeout()
        # return int(SECONDS_GIVEN_PER_METERS * route_length)

        # Print route in debug mode  # gotta try this!!!
        if debug_mode:  # drawing waypoints on the new DENSELY populated route  # wow that was fun :3
            self._draw_waypoints(world, self.route, vertical_shift=1.0, persistency=50000.0)

    def _update_ego_vehicle(self):
        """
        Set/Update the start position of the ego_vehicle
        """
        # move ego to correct position
        elevate_transform = self.route[0][0]

        #print(elevate_transform)  # Transform(Location(x=-3.456117, y=179.239578, z=0.000000), Rotation(pitch=0.000000, yaw=-90.012932, roll=0.000000))
        #sys.exit("yeahh")

        elevate_transform.location.z += 0.5

        ego_vehicle = CarlaDataProvider.request_new_actor('vehicle.lincoln.mkz2017',  #  vehicle type mentioned here and role Hero also given
                                                          elevate_transform,
                                                          rolename='hero')

        return ego_vehicle  #  vehicle type mentioned here and role Hero also given

    def _estimate_route_timeout(self):
        """
        Estimate the duration of the route
        """
        route_length = 0.0  # in meters

        prev_point = self.route[0][0]
        for current_point, _ in self.route[1:]:
            dist = current_point.location.distance(prev_point.location)
            route_length += dist
            prev_point = current_point

        return int(SECONDS_GIVEN_PER_METERS * route_length)

    # pylint: disable=no-self-use
    def _draw_waypoints(self, world, waypoints, vertical_shift, persistency=-1):  # self._draw_waypoints(world, self.route, vertical_shift=1.0, persistency=50000.0)
        """
        Draw a list of waypoints at a certain height given in vertical_shift.
        """
        for w in waypoints:
            wp = w[0].location + carla.Location(z=vertical_shift)

            size = 0.2
            if w[1] == RoadOption.LEFT:  # Yellow
                color = carla.Color(255, 255, 0)
            elif w[1] == RoadOption.RIGHT:  # Cyan
                color = carla.Color(0, 255, 255)
            elif w[1] == RoadOption.CHANGELANELEFT:  # Orange
                color = carla.Color(255, 64, 0)
            elif w[1] == RoadOption.CHANGELANERIGHT:  # Dark Cyan
                color = carla.Color(0, 64, 255)
            elif w[1] == RoadOption.STRAIGHT:  # Gray
                color = carla.Color(128, 128, 128)
            else:  # LANEFOLLOW
                color = carla.Color(0, 255, 0)  # Green
                size = 0.1

            world.debug.draw_point(wp, size=size, color=color, life_time=persistency)

        world.debug.draw_point(waypoints[0][0].location + carla.Location(z=vertical_shift), size=0.2,
                               color=carla.Color(0, 0, 255), life_time=persistency)
        world.debug.draw_point(waypoints[-1][0].location + carla.Location(z=vertical_shift), size=0.2,
                               color=carla.Color(255, 0, 0), life_time=persistency)

    def _scenario_sampling(self, potential_scenarios_definitions, random_seed=0):  # self._scenario_sampling(potential_scenarios_definitions)  # potential_scenarios_definitions is scenario_description with scenario name scenario type other vehicles trigger locations defined in the All Towns 1 3 4 JSON file
        """
        The function used to sample the scenarios that are going to happen for this route.
        """

        # fix the random seed for reproducibility
        rng = random.RandomState(random_seed)  #random Class object initialized by the RandomState class using random seed

        def position_sampled(scenario_choice, sampled_scenarios):  # position_sampled(scenario_choice, sampled_scenarios):
            """
            Check if a position was already sampled, i.e. used for another scenario
            """
            for existent_scenario in sampled_scenarios:
                # If the scenarios have equal positions then it is true.
                if compare_scenarios(scenario_choice, existent_scenario):
                    return True

            return False

        #zman_count = 0

        # The idea is to randomly sample a scenario per trigger position.
        sampled_scenarios = []
        for trigger in potential_scenarios_definitions.keys():
            possible_scenarios = potential_scenarios_definitions[trigger]

            #print(f"Possible scenario number {zman_count}")
            #print(possible_scenarios)
            #zman_count += 1
            #if zman_count == 5:
            #    sys.exit("off")

            '''
            Possible scenario number 0
            [{'name': 'Scenario1', 'other_actors': None, 'trigger_position': {'pitch': '0', 'x': 7.63, 'y': 109.91, 'yaw': 359.0, 'z': 1.22}, 'scenario_type': 'valid'},
            {'name': 'Scenario3', 'other_actors': None, 'trigger_position': {'pitch': '0', 'x': 7.63, 'y': 109.91,
            'yaw': 359.0, 'z': 1.22}, 'scenario_type': 'valid'}]
            Possible scenario number 1
            [{'name': 'Scenario1', 'other_actors': None, 'trigger_position': {'pitch': '0', 'x': 41.37, 'y':
            207.4, 'yaw': 90.0, 'z': 1.21}, 'scenario_type': 'valid'}, {'name': 'Scenario4', 'other_actors': {
            'front': [{'pitch': '0.0', 'x': '45.82', 'y': '270.45', 'yaw': '270.000031', 'z': '1.21'}], 'left'
            : [{'pitch': '0.0', 'x': '75.58', 'y': '236.78', 'yaw': '180.000015', 'z': '1.21'}]}, 'trigger_position
            ': {'pitch': '0', 'x': 41.37, 'y': 207.4, 'yaw': 90.0, 'z': 1.21}, 'scenario_type': 'S4left'}]
            '''

            #print(possible_scenarios)
            #sys.exit("off")

            '''
            [{'name': 'Scenario1', 'other_actors': None, 'trigger_position': {'pitch': '0', 'x': 7.63,
             'y': 109.91, 'yaw': 359.0, 'z': 1.22}, 'scenario_type': 'valid'}, {'name': 'Scenario3',
              'other_actors': None, 'trigger_position': {'pitch': '0', 'x': 7.63, 'y': 109.91, 'yaw':
               359.0, 'z': 1.22}, 'scenario_type': 'valid'}]
            '''

            # so a single possible_scenarios has the same trigger point for the multiple scenarios inside it

            scenario_choice = rng.choice(possible_scenarios)

            #print(scenario_choice)
            #sys.exit("off")

            '''
            Preparing scenario: RouteScenario_0
            {'name': 'Scenario1', 'other_actors': None, 'trigger_position': {'pitch': '0',
             'x': 7.63, 'y': 109.91, 'yaw': 359.0, 'z': 1.22}, 'scenario_type': 'valid'}
            '''

            del possible_scenarios[possible_scenarios.index(scenario_choice)]
            # We keep sampling and testing if this position is present on any of the scenarios.
            while position_sampled(scenario_choice, sampled_scenarios):  # returns true if scenario already sampled if I am right
                if possible_scenarios is None or not possible_scenarios:
                    scenario_choice = None
                    break
                scenario_choice = rng.choice(possible_scenarios)
                del possible_scenarios[possible_scenarios.index(scenario_choice)]
                #while loop keeps on going till we find a scenario that hasn't been sampled already

            if scenario_choice is not None:
                sampled_scenarios.append(scenario_choice)  # add to sampled scenarios!

            '''
            Example of one scenario is this
            Preparing scenario: RouteScenario_0
            {'name': 'Scenario1', 'other_actors': None, 'trigger_position': {'pitch': '0',
             'x': 7.63, 'y': 109.91, 'yaw': 359.0, 'z': 1.22}, 'scenario_type': 'valid'}
             scenario_type can be S4left S4right S4opposite, S5, S9, etc.
            '''

        return sampled_scenarios

    def _build_scenario_instances(self, world, ego_vehicle, scenario_definitions,
                                  scenarios_per_tick=5, timeout=300, debug_mode=False):

        '''
        self.list_scenarios = self._build_scenario_instances(world,
                                                             ego_vehicle,
                                                             self.sampled_scenarios_definitions,
                                                             scenarios_per_tick=5,
                                                             timeout=self.timeout,
                                                             debug_mode=debug_mode)

        '''

        """
        Based on the parsed route and possible scenarios, build all the scenario classes.
        """

        scenario_instance_vec = []

        if debug_mode:  # draw the trigger positions in and mentio the scenario number scenario definitions. We had selected only the Town02 hence only saw those triggers and scenario numbers
            for scenario in scenario_definitions:
                loc = carla.Location(scenario['trigger_position']['x'],
                                     scenario['trigger_position']['y'],
                                     scenario['trigger_position']['z']) + carla.Location(z=2.0)
                world.debug.draw_point(loc, size=0.3, color=carla.Color(255, 0, 0), life_time=100000)
                world.debug.draw_string(loc, str(scenario['name']), draw_shadow=False,
                                        color=carla.Color(0, 0, 255), life_time=100000, persistent_lines=True)

        for scenario_number, definition in enumerate(scenario_definitions):
            # Get the class possibilities for this scenario number
            scenario_class = NUMBER_CLASS_TRANSLATION[definition['name']]

            '''
            NUMBER_CLASS_TRANSLATION = {
                "Scenario1": ControlLoss,
                "Scenario2": FollowLeadingVehicle,
                "Scenario3": DynamicObjectCrossing,
                "Scenario4": VehicleTurningRoute,
                "Scenario5": OtherLeadingVehicle,
                "Scenario6": ManeuverOppositeDirection,
                "Scenario7": SignalJunctionCrossingRoute,
                "Scenario8": SignalJunctionCrossingRoute,
                "Scenario9": SignalJunctionCrossingRoute,
                "Scenario10": NoSignalJunctionCrossingRoute  # wut :3
            }

            # SO we define a route, select a town and select the scenario type and that we want,
            # e.g., Scenario10. Then we have our ego run on that route? Or advarserial vehicle as well?
            # I remember that the adversarial had a route as well. Leme look it up. NOPE
            # Only the starting locations of the ego and adversarial vehicles are mentioned.
            # Rest we define the locations of the TriggersRegion objects inside the NoSigJuncCross class
            # and add those objects to the py-tree inside the _create_behavior method.
            # so lets see what is happening here.
            '''

            # Create the other actors that are going to appear
            if definition['other_actors'] is not None:  # FOFF. We got hellova other actors in in Town02

                list_of_actor_conf_instances = self._get_actors_instances(definition['other_actors'])

                #_get_actors_instances -> return list_of_actors
                    # list of actor config objects of the other vehicles!
                    # # return object of ActorConfigurationData(model, transform, rolename, speed, autopilot, random_location, color) ActorConfigurationData

                #print("OFF MAN OFF")  #OFF MAN OFF
                #print(definition['other_actors'])

                '''
                {'front': [{'pitch': '0.0', 'x': '45.82', 'y': '270.45', 'yaw': '270.000031', 'z': '1.21'}],
                'left': [{'pitch': '0.0', 'x': '75.58', 'y': '236.78', 'yaw': '180.000015', 'z': '1.21'}]}
                each other_actors has a transform as well
                '''

                #sys.exit("off")

            else:
                list_of_actor_conf_instances = []
            # Create an actor configuration for the ego-vehicle trigger position

            egoactor_trigger_position = convert_json_to_transform(definition['trigger_position'])
            # trigger positions meant for the ego actor I presume

            #print(definition['trigger_position'])
            # {'pitch': '0', 'x': 7.63, 'y': 109.91, 'yaw': 359.0, 'z': 1.22}
            #sys.exit("ell")

            scenario_configuration = ScenarioConfiguration()

            """
            ScenarioConfiguration()

            This class provides a basic scenario configuration incl.:
            - configurations for all actors
            - town, where the scenario should be executed
            - name of the scenario (e.g. ControlLoss_1)
            - type is the class of scenario (e.g. ControlLoss)

            """

            # for each trigger point we make a new scenario which has other vehicles and
            # uses the trigger location to make different scenarios
            scenario_configuration.other_actors = list_of_actor_conf_instances
            scenario_configuration.trigger_points = [egoactor_trigger_position]
            scenario_configuration.subtype = definition['scenario_type']  #S4left etc? usually is valid. Yep.
            scenario_configuration.ego_vehicles = [ActorConfigurationData('vehicle.lincoln.mkz2017',
                                                                          ego_vehicle.get_transform(),
                                                                          'hero')]  # making ego ActorConfiguration object here rolenaming it Hero
            route_var_name = "ScenarioRouteNumber{}".format(scenario_number)  # scenario1 or scenario4, do this for all the scenarios

            #print(route_var_name)
            '''

            ScenarioRouteNumber0
            ScenarioRouteNumber1
            ScenarioRouteNumber2
            ScenarioRouteNumber3

            '''

            scenario_configuration.route_var_name = route_var_name

            try:  # all the scenario classes are avaialble for this
                scenario_instance = scenario_class(world, [ego_vehicle], scenario_configuration,
                                                   criteria_enable=False, timeout=timeout)

                # for each trigger point we make a new scenario which has other vehicles and
                # uses the trigger location to make different scenarios

                # Do a tick every once in a while to avoid spawning everything at the same time
                if scenario_number % scenarios_per_tick == 0:
                    if CarlaDataProvider.is_sync_mode():
                        world.tick()
                    else:
                        world.wait_for_tick()

                scenario_number += 1
            except Exception as e:      # pylint: disable=broad-except
                if debug_mode:
                    traceback.print_exc()
                print("Skipping scenario '{}' due to setup error: {}".format(definition['name'], e))
                continue

            scenario_instance_vec.append(scenario_instance)

        return scenario_instance_vec

    def _get_actors_instances(self, list_of_antagonist_actors):
        """
        Get the full list of actor instances.
        """

        def get_actors_from_list(list_of_actor_def):
            """
                Receives a list of actor definitions and creates an actual list of ActorConfigurationObjects
            """
            sublist_of_actors = []
            for actor_def in list_of_actor_def:
                sublist_of_actors.append(convert_json_to_actor(actor_def))  # return object of ActorConfigurationData(model, transform, rolename, speed, autopilot, random_location, color) ActorConfigurationData

            return sublist_of_actors

        list_of_actors = []
        # Parse vehicles to the left
        if 'front' in list_of_antagonist_actors:
            list_of_actors += get_actors_from_list(list_of_antagonist_actors['front'])

        if 'left' in list_of_antagonist_actors:
            list_of_actors += get_actors_from_list(list_of_antagonist_actors['left'])

        if 'right' in list_of_antagonist_actors:
            list_of_actors += get_actors_from_list(list_of_antagonist_actors['right'])

        return list_of_actors
        # list of actor config objects of the other vehicles!
        # # return object of ActorConfigurationData(model, transform, rolename, speed, autopilot, random_location, color) ActorConfigurationData

    # pylint: enable=no-self-use

    def _initialize_actors(self, config):
        """
        Set other_actors to the superset of all scenario actors
        """

        # Create the background activity of the route
        town_amount = {
            'Town01': 120,
            'Town02': 100,
            'Town03': 120,
            'Town04': 200,
            'Town05': 120,
            'Town06': 150,
            'Town07': 110,
            'Town08': 180,
            'Town09': 300,
            'Town10': 120,
        }

        amount = town_amount[config.town] if config.town in town_amount else 0

        new_actors = CarlaDataProvider.request_new_batch_actors('vehicle.*',
                                                                amount,
                                                                carla.Transform(),
                                                                autopilot=True,
                                                                random_location=True,
                                                                rolename='background')

        if new_actors is None:
            raise Exception("Error: Unable to add the background activity, all spawn points were occupied")

        for _actor in new_actors:
            self.other_actors.append(_actor)

        # Add all the actors of the specific scenarios to self.other_actors
        for scenario in self.list_scenarios:
            self.other_actors.extend(scenario.other_actors)

    def _create_behavior(self):
        """
        Basic behavior do nothing, i.e. Idle
        """
        scenario_trigger_distance = 1.5  # Max trigger distance between route and scenario

        behavior = py_trees.composites.Parallel(policy=py_trees.common.ParallelPolicy.SUCCESS_ON_ONE)

        subbehavior = py_trees.composites.Parallel(name="Behavior",
                                                   policy=py_trees.common.ParallelPolicy.SUCCESS_ON_ALL)

        scenario_behaviors = []
        blackboard_list = []

        for i, scenario in enumerate(self.list_scenarios):
            if scenario.scenario.behavior is not None:
                route_var_name = scenario.config.route_var_name
                if route_var_name is not None:
                    scenario_behaviors.append(scenario.scenario.behavior)
                    blackboard_list.append([scenario.config.route_var_name,
                                            scenario.config.trigger_points[0].location])
                else:
                    name = "{} - {}".format(i, scenario.scenario.behavior.name)
                    oneshot_idiom = oneshot_behavior(name,
                                                     behaviour=scenario.scenario.behavior,
                                                     name=name)
                    scenario_behaviors.append(oneshot_idiom)

        # Add behavior that manages the scenarios trigger conditions
        scenario_triggerer = ScenarioTriggerer(
            self.ego_vehicles[0],
            self.route,
            blackboard_list,
            scenario_trigger_distance,
            repeat_scenarios=False
        )

        subbehavior.add_child(scenario_triggerer)  # make ScenarioTriggerer the first thing to be checked
        subbehavior.add_children(scenario_behaviors)
        subbehavior.add_child(Idle())  # The behaviours cannot make the route scenario stop
        behavior.add_child(subbehavior)

        return behavior

    def _create_test_criteria(self):
        """
        """

        criteria = []

        route = convert_transform_to_location(self.route)

        collision_criterion = CollisionTest(self.ego_vehicles[0], terminate_on_failure=False)

        route_criterion = InRouteTest(self.ego_vehicles[0],
                                      route=route,
                                      offroad_max=30,
                                      terminate_on_failure=True)

        completion_criterion = RouteCompletionTest(self.ego_vehicles[0], route=route)

        outsidelane_criterion = OutsideRouteLanesTest(self.ego_vehicles[0], route=route)

        red_light_criterion = RunningRedLightTest(self.ego_vehicles[0])

        stop_criterion = RunningStopTest(self.ego_vehicles[0])

        blocked_criterion = ActorSpeedAboveThresholdTest(self.ego_vehicles[0],
                                                         speed_threshold=0.1,
                                                         below_threshold_max_time=90.0,
                                                         terminate_on_failure=True)

        criteria.append(completion_criterion)
        criteria.append(collision_criterion)
        criteria.append(route_criterion)
        criteria.append(outsidelane_criterion)
        criteria.append(red_light_criterion)
        criteria.append(stop_criterion)
        criteria.append(blocked_criterion)

        return criteria

    def __del__(self):
        """
        Remove all actors upon deletion
        """
        self.remove_all_actors()


# Se elabora dos gráficas con los datos almacenados en el archivo . txt , una de las posiciones y
# la otra de las orientaciones expresadas en ángulos de Euler.

#!/usr/bin/env python3
# encoding: utf-8

#Linea 1 - “Shebang”,le indicamos a la máquina con qué programa lo vamos a ejecutar.
#Linea 2 - Python 3 - asume que solo se utiliza ASCII en el código fuente
#para usar utf-8 hay que indicarlo al principio de nuestro script encoding: utf-8

#Importa librerías
import pandas as pd
import matplotlib.pyplot as plt

#Lectura de la base de datos
path = ('x.txt')
data = pd.read_csv(path, header=None, names=['Posición x'])

path = ('y.txt')
data1 = pd.read_csv(path, header=None, names=['Posición y'])

path = ('z.txt')
data2 = pd.read_csv(path, header=None, names=['Orientación'])

#Gráfica de la base de datos
plt.figure(1)
plt.title('Gráfica de odometría')
plt.xlabel('Posicion en x')
plt.ylabel('Posicion en y')
plt.plot(data,data1,'b')
plt.grid()

plt.figure(2)
plt.title('Gráfica de la orientación')
plt.plot(data2,'r')
plt.grid()

plt.show()


import datetime
import unittest
import mock
# import soapServer
# import pytest
import soap_server
from soap_server import DERGroupsPayloadType, HeaderType, EndDeviceGroup, DERFunction, Version, EndDevice, Name, DERGroups
from message import VerbType, ResultType, LevelType
# @mock.patch('soapServer.conn')
mock_GridAPPSD = mock.MagicMock()
soap_server.conn = mock_GridAPPSD

# @pytest.fixture(scope="module", autouse=True)
# def mock_GridAPPSD(mocker):
#     return mocker.patch('soapServer.GridAPPSD', autospec=True)

# # @pytest.mark.usefixtures("mock_GridAPPSD")
# class SoapServerTestCase(unittest.TestCase):
#     # @mock.patch('soapServer.GridAPPSD', autospec=True)
#     # def setUp(self):
#     #     self.mock_GridAPPSD = mock.patch.object(
#     #         soapServer, 'conn', return_value=GridAPPSD()
#     #     )
#     # @mock.patch('soapServer.GridAPPSD', autospec=True)
#     def test_get_DERM_devices(self):
#         mock_GridAPPSD.reset_mock()
#         mock_GridAPPSD.query_data.return_value = 'query_results'
#         soap_server.get_DERM_devices()
#         self.assertEqual(mock_GridAPPSD.query_data.call_count, 3)

# @mock.patch('soapServer.conn')

class GetDevicesServiceTestCase(unittest.TestCase):
    def test_GetDevices(self):
        mock_GridAPPSD.reset_mock()
        # sol = {'name': {'type': 'literal', 'value': '3p_existi'}, 'bus': {'type': 'literal', 'value': 'b4832_sec'}, 'ratedS': {'type': 'literal', 'value': '209000'}, 'ratedU': {'type': 'literal', 'value': '416'}, 'ipu': {'type': 'literal', 'value': '1.1111111'}, 'p': {'type': 'literal', 'value': '1'}, 'q': {'type': 'literal', 'value': '0'}, 'id': {'type': 'literal', 'value': '00A69D4E-EB07-4AF7-8C93-BA7A3924B07A'}, 'fdrid': {'type': 'literal', 'value': '67AB291F-DCCD-31B7-B499-338206B9828F'}, 'phases': {'type': 'literal', 'value': ''}, 'ratedE': {'type': 'literal', 'value': '500000'}, 'storedE': {'type': 'literal', 'value': '500000'}, 'state': {'type': 'literal', 'value': 'Waiting'}}
        # bat = {'name': {'type': 'literal', 'value': 'battery16'}, 'bus': {'type': 'literal', 'value': 'm2001-ess'}, 'ratedS': {'type': 'literal', 'value': '250000'}, 'ratedU': {'type': 'literal', 'value': '124'}, 'ipu': {'type': 'literal', 'value': '1.1111111'}, 'p': {'type': 'literal', 'value': '0'}, 'q': {'type': 'literal', 'value': '0'}, 'id': {'type': 'literal', 'value': 'C138DF63-68E1-4B3C-B280-55E210D4E9FE'}, 'fdrid': {'type': 'literal', 'value': 'AAE94E4A-2465-6F5E-37B1-3E72183A4E44'}, 'phases': {'type': 'literal', 'value': ''}, 'ratedE': {'type': 'literal', 'value': '500000'}, 'storedE': {'type': 'literal', 'value': '500000'}, 'state': {'type': 'literal', 'value': 'Waiting'}}
        mock_GridAPPSD.query_data.return_value = {'data':
                   {'head': {'vars': ['name', 'bus', 'ratedS', 'ratedU', 'ipu', 'ratedE', 'storedE', 'state', 'p', 'q', 'id', 'fdrid', 'phases']},
                    'results': {'bindings': [
                        {'name': {'type': 'literal', 'value': 'endDevice_Bat_WTG'}, 'mrid': {'type': 'literal', 'value': 'a1321228-40ee-4eb6-86a1-2c7c6a642fc4'}, 'issmart': {'type': 'literal', 'value': 'True'}, 'upoint': {'type': 'literal', 'value': 'f9e17abf-babf-466b-8253-86161f0b95da'}},
                        {'name': {'type': 'literal', 'value': 'endDevice_PV_1'}, 'mrid': {'type': 'literal', 'value': '99491fc0-44ad-482f-9319-72c651c0ef5f'}, 'issmart': {'type': 'literal', 'value': 'True'}, 'upoint': {'type': 'literal', 'value': '11345f6b-f706-4c43-957c-ba88a44890bb'}},
                        {'name': {'type': 'literal', 'value': 'endDevice_Rooftop_1'}, 'mrid': {'type': 'literal', 'value': 'd79490ce-a5c1-470c-af3a-93ffdc1944ff'}, 'issmart': {'type': 'literal', 'value': 'True'}, 'upoint': {'type': 'literal', 'value': '58aa7600-fc5e-4753-ab7d-654cb33d2b73'}}]}},
               'responseComplete': True,
               'id': '861322400'}
        real = soap_server.GetDevicesService()
        value = real.GetDevices(None)
        assert len(value) == 3
        assert value[0].name == 'endDevice_Bat_WTG'
        assert value[0].mRID == 'a1321228-40ee-4eb6-86a1-2c7c6a642fc4'
        assert value[0].isSmartInverter == True
        self.assertEqual(mock_GridAPPSD.query_data.call_count, 1)

class CreateDERGroupsServiceTestCase(unittest.TestCase):
    def test_CreateDERGroups(self):
        header = HeaderType(verb=VerbType.CREATE, noun='DERGroups', timestamp=datetime.datetime.now(), messageID='360150a8-4d74-4cd2-8628-79dafc8ac6c1', correlationID='57a597c0-7d74-4796-9bb5-434c9250ddf0')
        # payload = DERGroupsPayloadType(DERGroups=[EndDeviceGroup(mRID='a6af4a25-878e-4703-8bdf-7245dffe67f2', description='1', DERFunction=DERFunction(connectDisconnect=True, frequencyWattCurveFunction=False, maxRealPowerLimiting=False, rampRateControl=False, reactivePowerDispatch=False, realPowerDispatch=True, voltageRegulation=False, voltVarCurveFunction=False, voltWattCurveFunction=False), endDevices=[EndDevice(mRID='99decc0d-6d70-48d6-8e63-a9ee6ccf1514'), EndDevice(mRID='1f2dd132-00b5-4979-9338-75aada4fb80a')], names=[Name(name='1')], version=Version(date=datetime.datetime.now(), major=1, minor=0, revision=0))])
        payload = DERGroupsPayloadType(DERGroups=DERGroups(endDeviceGroup=[EndDeviceGroup(mRID='ff329fde-6a06-4c91-af1f-b321d8b629e1', description='test2', DERFunction=DERFunction(connectDisconnect=False, frequencyWattCurveFunction=False, maxRealPowerLimiting=False, rampRateControl=False, reactivePowerDispatch=False, realPowerDispatch=True, voltageRegulation=False, voltVarCurveFunction=False, voltWattCurveFunction=False), endDevices=[EndDevice(mRID='7357f5b6-86de-49b5-9e3c-42707475ab41'), EndDevice(mRID='a1321228-40ee-4eb6-86a1-2c7c6a642fc4')], names=[Name(name='group2')], version=Version(date=datetime.datetime.now(), major=1, minor=0, revision=0))]))
        # payload = Payload()
        real = soap_server.CreateDERGroupsService()
        value = real.CreateDERGroups(None, header, payload)
        assert value.Header.Verb == VerbType.REPLY
        assert value.Header.Noun == 'DERGroups'
        assert value.Reply.Error.code == '6.1'
        assert value.Reply.Error.level == LevelType.FATAL
        assert value.Reply.Error.reason == 'Request cancelled per business rule'
        assert value.Reply.Result == ResultType.FAILED
        # print(value)
        # self.assertEqual(True, False)

# import os
#
# @mock.patch("os.listdir")
# class Test(unittest.TestCase):
#
#     def test_not_decorated_and_not_tested(self, mock_listdir):
#         soapServer.get_DERM_devices()
#
#     def test3(self, mock_listdir):
#         mock_listdir.return_value = "test3"
#         assert  "test3" == os.listdir()

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


#!/usr/bin/env python3

"""
Module principal pour la découpe de modèles 3D
"""

import argparse
import itertools
from stl import STL
from svg import SVG

def triangle_in_slice(triangle, z_slice):
    """
    Teste si le triangle est dans la tranche
    """
    return sum(1 for point in triangle if point[2] == z_slice) // 3

def segment_in_slice(point0, point1, z_slice):
    """
    Teste si le segment est dans la tranche
    """
    return point0[2] == z_slice and point1[2] == z_slice

def segment_intersects_slice(point0, point1, z_slice):
    """
    Calcule le parametre qui teste si les deux points forment un segment qui intersecte la tranche
    """
    denominateur = point1[2] - point0[2]
    numerateur = z_slice - point0[2]
    if denominateur != 0:
        parameter = numerateur/denominateur
        if parameter >= 0 and parameter <= 1:
            return parameter

def calcul_intersection(point0, point1, parameter):
    """
    Calcul du point d'intersection entre la tranche et le segment formé des deux points
    """
    return [point0[i] + parameter*(point1[i] - point0[i]) for i in range(3)]

def intersection(triangle, z_slice):
    """
    Calcul des points d'intersection du triangle avec la tranche
    """
    points = []
    for point0, point1 in itertools.combinations(triangle.vertices, 2):
        if segment_in_slice(point0, point1, z_slice) and not triangle_in_slice(triangle, z_slice):
            points.append(point0)
            points.append(point1)
        else:
            parameter = segment_intersects_slice(point0, point1, z_slice)
            if parameter:
                points.append(calcul_intersection(point0, point1, parameter))
    return points

def generate_slice(stl, z_slice, svg, origins, scale):
    """
    Generation d'un tranche
    """
    for triangle in stl.triangles:
        points = intersection(triangle, z_slice)
        for point0, point1 in itertools.combinations(points, 2):
            for index in range(2):
                point0[index] = (point0[index] - origins[index])*scale
                point1[index] = (point1[index] - origins[index])*scale
            svg.add_line(point0, point1)

def main():
    """
    Fonction principale
    """
    parser = argparse.ArgumentParser(description="slice a binary STL file")
    parser.add_argument("stl_file", help="name of the binary STL file to slice")
    parser.add_argument("-s", "--slices", type=int, dest="SLICES", default=4,
                        help="how many slices you want (default is 4)")
    parser.add_argument("-g", "--grow", type=int, dest="SCALE", default=1,
                        help="scale up or down the slices (default is 1)")
    args = parser.parse_args()

    stl = STL()
    stl.load(args.stl_file)
    dimension, origins = stl.dimensions()
    height = dimension[2]/args.SLICES

    for index in range(args.SLICES):
        z_slice = origins[2] + height*index
        filename = "slice{}.svg".format(index)
        svg = SVG(dimension[0]*args.SCALE, dimension[1]*args.SCALE)
        generate_slice(stl, z_slice, svg, origins, args.SCALE)
        svg.save(filename)

if __name__ == "__main__":
    main()


from models import Event, EventAmenity
import logging
from event.choices import AmenityType
from common.service import google as googleService

logging.basicConfig()
log = logging.getLogger(__name__)
from contact.service import createAddressFromForm, createContactNumberFromForm, createAddress
from appUser.models import AppUser

def createEvent(eventForm, eventRuleForm, addressForm, contactForm):
    try:
        contact = createContactNumberFromForm(contactForm)
        address = createAddressFromForm(addressForm)
        eventRule = eventRuleForm.save()
        description = eventForm.cleaned_data['description']
        created_by = eventForm.cleaned_data['createdBy']
        if contact and address and eventRule and created_by:
            event = Event.create(description, created_by, address, contact, eventRule)
            createEventAmenities(event)
            return event
    except Exception as e:
        log.error("event errorrr======" + str(e))
        return None

def createEventAmenities(event):
    latitude = event.location.latitude
    longitude = event.location.longitude
    try:
        for choices in AmenityType.googleMapingChoices:
            response = googleService.nearByPlace(choices[1],10000, latitude=latitude, longitude=longitude)
            response = response['results']
            for priority, i in enumerate(response):
                address = createAddress(
                    latitude=i['geometry']['location']['lat'],
                    longitude=i['geometry']['location']['lng'],
                    line1=i['name'],
                    line2=i['vicinity']
                )
                EventAmenity.objects.create(location=address,
                                            type=choices[0],
                                            event=event,
                                            priority=priority)
    except Exception as e :
        log.error(e)

def createEventRuleFromForm(eventRuleForm):
    try:
        eventRule = eventRuleForm.save()
        return eventRule
    except Exception as e:
        log.error(e)
        log.error(eventRuleForm.errors)
        return None


# 漸化式を立ててDP（動的計画法）をしよう
N, L = map(int, input().split())

div = 10**9 + 7

dp = [1] * (N + 1)
for i in range(1, N + 1):
    if i < L:
        dp[i] = dp[i - 1]
    else:
        dp[i] = (dp[i - 1] + dp[i - L]) % div

print(dp[-1])


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Tue Aug  6 13:58:23 2019

@author: burr
"""

from fileutils import readfile, getfilelist, adctoamp, adctovolt, ch0calib, ch1calib
import numpy as np
import pandas as pd
import os
import time

columns = {'apparent_power', 'real_power', 'nonactive_power',
'imag01', 'imag03', 'imag05', 'imag07', 'imag09',
'imag11', 'imag13', 'imag15', 'imag17', 'imag19',
'imag21', 'imag23', 'imag25', 'imag27', 'imag29',
'imag31', 'imag33', 'imag35', 'imag37', 'imag39',
'imag41', 'imag43', 'imag45', 'imag47', 'imag49',
'imag51', 'imag53', 'imag55', 'imag57', 'imag59',
'imag61', 'imag63', 'imag65', 'imag67', 'imag69',
'imag71', 'imag73', 'imag75', 'imag77', 'imag79',
'imag81', 'imag83', 'imag85', 'imag87', 'imag89',
'imag91', 'imag93', 'imag95', 'imag97', 'imag99',
'real01', 'real03', 'real05', 'real07', 'real09',
'real11', 'real13', 'real15', 'real17', 'real19',
'real21', 'real23', 'real25', 'real27', 'real29',
'real31', 'real33', 'real35', 'real37', 'real39',
'real41', 'real43', 'real45', 'real47', 'real49',
'real51', 'real53', 'real55', 'real57', 'real59',
'real61', 'real63', 'real65', 'real67', 'real69',
'real71', 'real73', 'real75', 'real77', 'real79',
'real81', 'real83', 'real85', 'real87', 'real89',
'real91', 'real93', 'real95', 'real97', 'real99',

'label'}

def concatenatedf(path):
    filelist, dirlist, filenamelist = getfilelist(path)

    li = []

    for filename in filelist:
        df = pd.read_csv(filename, index_col=0)
        li.append(df)

    frame = pd.concat(li, axis=0, ignore_index=True)
    frame.to_csv(os.path.join(path, 'all.csv'))
    print ('concatenated to ', os.path.join(path, 'all.csv'))

def savefeaturestocsv(path, saveto):
    filelist, dirlist, namelist = getfilelist(path)

    for file in filelist:
        outputfile = os.path.join(saveto, os.path.basename(os.path.dirname(file)), os.path.splitext(os.path.basename(file))[0] + '.csv')
        data = extractfeatures(file)
        if not os.path.isdir(os.path.join(saveto,os.path.basename(os.path.dirname(file)))):
            os.makedirs(os.path.join(saveto,os.path.basename(os.path.dirname(file))))
        data.to_csv(outputfile)

def extractfeatures(file):
    fs=10000
    data = pd.DataFrame(columns=columns)

    timestamp_arr, ch0_arr, ch1_arr, marker_arr, label_arr, filename = readfile(file)

    voltage = (ch0_arr+ch0calib)*adctovolt
    current = (ch1_arr+ch1calib)*adctoamp
    window = int(fs/10)
    starttime=time.time()

    for i in range(int(len(ch0_arr)/window)):
        currentwindowed = current[i*window:(i+1)*window]
        voltagewindowed = voltage[i*window:(i+1)*window]
        currentrfft = np.fft.rfft(currentwindowed)#*np.hanning(len(currentwindowed)))

        voltagerms = np.sqrt(np.sum(pow(voltagewindowed,2))/len(voltagewindowed))
        currentrms = np.sqrt(np.sum(pow(currentwindowed,2))/len(currentwindowed))

        instpower = np.multiply(currentwindowed,voltagewindowed)
        realpower = np.sum(instpower)/len(instpower)
        apparentpower = voltagerms*currentrms
        nonactivepower=np.sqrt(apparentpower**2/realpower**2)

        data.loc[i] = pd.Series({

                'apparent_power': apparentpower,
                'real_power': realpower,
                'nonactive_power': nonactivepower,

                'real01': np.abs(np.real((currentrfft[ 5]))),
                'real03': np.abs(np.real((currentrfft[15]))),
                'real05': np.abs(np.real((currentrfft[25]))),
                'real07': np.abs(np.real((currentrfft[35]))),
                'real09': np.abs(np.real((currentrfft[45]))),
                'real11': np.abs(np.real((currentrfft[55]))),
                'real13': np.abs(np.real((currentrfft[65]))),
                'real15': np.abs(np.real((currentrfft[75]))),
                'real17': np.abs(np.real((currentrfft[85]))),
                'real19': np.abs(np.real((currentrfft[95]))),
                'real21': np.abs(np.real((currentrfft[105]))),
                'real23': np.abs(np.real((currentrfft[115]))),
                'real25': np.abs(np.real((currentrfft[125]))),
                'real27': np.abs(np.real((currentrfft[135]))),
                'real29': np.abs(np.real((currentrfft[145]))),
                'real31': np.abs(np.real((currentrfft[155]))),
                'real33': np.abs(np.real((currentrfft[165]))),
                'real35': np.abs(np.real((currentrfft[175]))),
                'real37': np.abs(np.real((currentrfft[185]))),
                'real39': np.abs(np.real((currentrfft[195]))),
                'real41': np.abs(np.real((currentrfft[205]))),
                'real43': np.abs(np.real((currentrfft[215]))),
                'real45': np.abs(np.real((currentrfft[225]))),
                'real47': np.abs(np.real((currentrfft[235]))),
                'real49': np.abs(np.real((currentrfft[245]))),
                'real51': np.abs(np.real((currentrfft[255]))),
                'real53': np.abs(np.real((currentrfft[265]))),
                'real55': np.abs(np.real((currentrfft[275]))),
                'real57': np.abs(np.real((currentrfft[285]))),
                'real59': np.abs(np.real((currentrfft[295]))),
                'real61': np.abs(np.real((currentrfft[305]))),
                'real63': np.abs(np.real((currentrfft[315]))),
                'real65': np.abs(np.real((currentrfft[325]))),
                'real67': np.abs(np.real((currentrfft[335]))),
                'real69': np.abs(np.real((currentrfft[345]))),
                'real71': np.abs(np.real((currentrfft[355]))),
                'real73': np.abs(np.real((currentrfft[365]))),
                'real75': np.abs(np.real((currentrfft[375]))),
                'real77': np.abs(np.real((currentrfft[385]))),
                'real79': np.abs(np.real((currentrfft[395]))),
                'real81': np.abs(np.real((currentrfft[405]))),
                'real83': np.abs(np.real((currentrfft[415]))),
                'real85': np.abs(np.real((currentrfft[425]))),
                'real87': np.abs(np.real((currentrfft[435]))),
                'real89': np.abs(np.real((currentrfft[445]))),
                'real91': np.abs(np.real((currentrfft[455]))),
                'real93': np.abs(np.real((currentrfft[465]))),
                'real95': np.abs(np.real((currentrfft[475]))),
                'real97': np.abs(np.real((currentrfft[485]))),
                'real99': np.abs(np.real((currentrfft[495]))),

                'imag01': np.abs(np.imag((currentrfft[ 5]))),
                'imag03': np.abs(np.imag((currentrfft[15]))),
                'imag05': np.abs(np.imag((currentrfft[25]))),
                'imag07': np.abs(np.imag((currentrfft[35]))),
                'imag09': np.abs(np.imag((currentrfft[45]))),
                'imag11': np.abs(np.imag((currentrfft[55]))),
                'imag13': np.abs(np.imag((currentrfft[65]))),
                'imag15': np.abs(np.imag((currentrfft[75]))),
                'imag17': np.abs(np.imag((currentrfft[85]))),
                'imag19': np.abs(np.imag((currentrfft[95]))),
                'imag21': np.abs(np.imag((currentrfft[105]))),
                'imag23': np.abs(np.imag((currentrfft[115]))),
                'imag25': np.abs(np.imag((currentrfft[125]))),
                'imag27': np.abs(np.imag((currentrfft[135]))),
                'imag29': np.abs(np.imag((currentrfft[145]))),
                'imag31': np.abs(np.imag((currentrfft[155]))),
                'imag33': np.abs(np.imag((currentrfft[165]))),
                'imag35': np.abs(np.imag((currentrfft[175]))),
                'imag37': np.abs(np.imag((currentrfft[185]))),
                'imag39': np.abs(np.imag((currentrfft[195]))),
                'imag41': np.abs(np.imag((currentrfft[205]))),
                'imag43': np.abs(np.imag((currentrfft[215]))),
                'imag45': np.abs(np.imag((currentrfft[225]))),
                'imag47': np.abs(np.imag((currentrfft[235]))),
                'imag49': np.abs(np.imag((currentrfft[245]))),
                'imag51': np.abs(np.imag((currentrfft[255]))),
                'imag53': np.abs(np.imag((currentrfft[265]))),
                'imag55': np.abs(np.imag((currentrfft[275]))),
                'imag57': np.abs(np.imag((currentrfft[285]))),
                'imag59': np.abs(np.imag((currentrfft[295]))),
                'imag61': np.abs(np.imag((currentrfft[305]))),
                'imag63': np.abs(np.imag((currentrfft[315]))),
                'imag65': np.abs(np.imag((currentrfft[325]))),
                'imag67': np.abs(np.imag((currentrfft[335]))),
                'imag69': np.abs(np.imag((currentrfft[345]))),
                'imag71': np.abs(np.imag((currentrfft[355]))),
                'imag73': np.abs(np.imag((currentrfft[365]))),
                'imag75': np.abs(np.imag((currentrfft[375]))),
                'imag77': np.abs(np.imag((currentrfft[385]))),
                'imag79': np.abs(np.imag((currentrfft[395]))),
                'imag81': np.abs(np.imag((currentrfft[405]))),
                'imag83': np.abs(np.imag((currentrfft[415]))),
                'imag85': np.abs(np.imag((currentrfft[425]))),
                'imag87': np.abs(np.imag((currentrfft[435]))),
                'imag89': np.abs(np.imag((currentrfft[445]))),
                'imag91': np.abs(np.imag((currentrfft[455]))),
                'imag93': np.abs(np.imag((currentrfft[465]))),
                'imag95': np.abs(np.imag((currentrfft[475]))),
                'imag97': np.abs(np.imag((currentrfft[485]))),
                'imag99': np.abs(np.imag((currentrfft[495]))),

                'label': label_arr[int(i)*window]

               })
    print(file,'converted in %4f'% (time.time()-starttime))
    return data

if __name__ == '__main__':
    savefeaturestocsv(path='labelled/1_WithoutSwitchingEvents', saveto='data/1_WithoutSwitchingEvents/featurescomplete/')

    savefeaturestocsv(path='labelled/2_WithSwitchingEvents', saveto='data/2_WithSwitchingEvents/featurescomplete')

    savefeaturestocsv(path='labelled/3_MultipleDevices_Order00', saveto='data/3_MultipleDevices_Order00/featurescomplete')
    savefeaturestocsv(path='labelled/3_MultipleDevices_Order01', saveto='data/3_MultipleDevices_Order01/featurescomplete')
    savefeaturestocsv(path='labelled/3_MultipleDevices_Order02', saveto='data/3_MultipleDevices_Order02/featurescomplete')
    savefeaturestocsv(path='labelled/3_MultipleDevices_Order10/', saveto = 'data/3_MultipleDevices_Order10/featurescomplete')
    savefeaturestocsv(path='labelled/3_MultipleDevices_Order11/', saveto = 'data/3_MultipleDevices_Order11/featurescomplete')

    savefeaturestocsv(path='labelled/4_LaptopStates/', saveto = 'data/4_LaptopStates/featurescomplete')

    savefeaturestocsv(path='labelled/5_MassiveSwitching/', saveto = 'data/5_MassiveSwitching/featurescomplete')


import arxiv
import sqlite_utils
import os
from .base_loader import BaseLoader
from .pdf_loader import PdfLoader
from nchain import user_dir
from typing import Optional

class ArxivLoader(BaseLoader):

    def __init__(self, db_path: str):
        """
        Initialize the ArxivLoader with a path to the SQLite database.

        :param db_path: Path to the SQLite database file.
        """
        self.db = sqlite_utils.Database(db_path)
        # Ensure the 'papers' table exists
        self.db["papers"].create({
            "paper_id": str,
            "entry_id": str,
            "title": str,
            "authors": str,
            "summary": str,
            "pdf_path": str,
            "url": str,
            "published": str,
            "updated": str
        }, pk="paper_id", if_not_exists=True)

    def load_data(self, source: str, download_dir: Optional[str] = None) -> dict:
        """
        Load and extract metadata and content from an arXiv paper.
        :source: The arXiv paper ID or URL.
        :param paper_id: The arXiv paper ID.
        :param download_dir: Optional directory path to save the downloaded PDF.
        :return: A dictionary with metadata and content of the paper.
        """
        paper_id = source.split("/")[-1]
        paper = next(arxiv.Search(id_list=[paper_id]).results())

        if not download_dir:
            download_dir = str(user_dir() / 'Documents' / 'paper')
        os.makedirs(download_dir, exist_ok=True)
        # Construct the filename based on the paper_id and title
        pdf_filename = f"{paper.entry_id.split('/')[-1]}.{paper.title.replace(' ', '_').replace('/', '_')}.pdf"
        pdf_path = os.path.join(download_dir, pdf_filename)
        if not os.path.exists(pdf_path):
            pdf_path = paper.download_pdf(download_dir)
        else:
            print(f"PDF for {paper_id} already exists at {pdf_path}. Skipping download.")
        metadata = {
            "paper_id": paper_id,
            "entry_id": paper.entry_id,
            "title": paper.title,
            "authors": ", ".join([author.name for author in paper.authors]),
            "summary": paper.summary,
            "pdf_path": str(pdf_path),
            "url": f"https://arxiv.org/abs/{paper_id}",
            "published": paper.published.strftime('%Y-%m-%d %H:%M:%S'),
            "updated": paper.updated.strftime('%Y-%m-%d %H:%M:%S')
        }

        # Save metadata to the database
        self.db["papers"].upsert(metadata, pk="paper_id")

        content = self.extract_pdf_content(pdf_path)

        data = {"content":content, "meta_data":metadata}
        return { "doc_id": paper_id, "data": data}

    def extract_pdf_content(self, pdf_path: str) -> str:
        """
        Use the PdfLoader to extract the actual content of the provided PDF.

        :param pdf_path: Path to the PDF file.
        :return: Extracted text content of the PDF.
        """
        loader = PdfLoader()
        return loader.load_data(pdf_path)

if __name__ == "__main__":
    loader = ArxivLoader("test.db")
    url = "https://arxiv.org/abs/2102.07947"
    res = loader.load_data(url)
    print('metadata:', res['metadata'])
    print('content:', type(res['content']), len(res['content']))
    print('content:', res['content'][:100])


from batch_helper import batches
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
import numpy as np

def print_epoch_stats(epoch_i, sess, batch_features,batch_labels):
    """
    Print cost and validation accuracy of each epochs
    :param epoch_i:
    :param sess:
    :param batch_features:
    :param batch_labels:
    :return:
    """
    current_cost = sess.run(cost, feed_dict={features: batch_features, labels: batch_labels})
    valid_accuracy = sess.run(accuracy, feed_dict={features: batch_features, labels: batch_labels})
    print('Epochs: {:<4} - Cost: {:<8.3} Valid accuracy: {:<5.3}'.format(epoch_i, current_cost, valid_accuracy))

n_inputs=784 # MINIST input data i.e. 28*28 images
n_classes = 10 #MNIST no of classes i.e. 10

# loading mnist database
mnist = input_data.read_data_sets('/datasets/ud730/mnist', one_hot=True)

# features are already scaled and data is shuffled
# define the MNIST train, validation and test features
train_features = mnist.train.images
valid_features = mnist.validation.images
test_features = mnist.test.images

train_labels = mnist.train.labels.astype(np.float32) # MNIST train labels
valid_labels = mnist.validation.labels.astype(np.float32) # MNIST validation labels
test_labels = mnist.test.labels.astype(np.float32) # MNIST test labels

# features and labels
features = tf.placeholder(tf.float32, [None, n_inputs])
labels = tf.placeholder(tf.float32, [None, n_classes])

#Weights and bias
weights = tf.Variable(tf.random_normal([n_inputs, n_classes]))
bias = tf.Variable(tf.random_normal([n_classes]))

#Logits xW + b
logits = tf.add(tf.matmul(features, weights), bias)

# define loss and optimizer
# Learning rate has been made as place holder because we may want to change the value of learning rate in near future
learning_rate = tf.placeholder(tf.float32)
cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=logits, labels=labels))
optimizer = tf.train.GradientDescentOptimizer(learning_rate=learning_rate).minimize(cost)

# calculate accuracy
correct_prediction = tf.equal(tf.argmax(logits, 1), tf.argmax(labels, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))

batch_size = 128
epochs = 10
learn_rate = 0.001
init = tf.global_variables_initializer()
# call batches function from helper class values passed to batch function are training features and training labels
# This will return features and labels in batches
train_batches = batches(batch_size, train_features, train_labels)

with tf.Session() as sess:
    # run the global variable initializer
    sess.run(init)
    for epoch_i in range(epochs):
        for batch_features, batch_labels in train_batches:
            # the batch features and batch labels returned from batch function are feed to features and labels resp.
            # in the same way learning rate is feed through feed_dict
            train_feed_dict = {features: batch_features, labels: batch_labels, learning_rate: learn_rate}
            # inorder to call the optimizer we require learning rate and cost, where cost intern depends upon logits and labels, and logits in tern depends upon features
            # so we provided features, labels and learning rate as feed_dict
            sess.run(optimizer, feed_dict=train_feed_dict)
        print_epoch_stats(epoch_i, sess, batch_features, batch_labels)
    # similarly in case of test_accuracy we require correct_prediction and correct_prediction requires logits and labels , logits require features
    # so we passes test features and test labels to calculate accuracy
    # there is no need to pass test_features and test_labels to batch method as we need to calculate accuracy, we only need batches in case of training
    # because of which now the value of features has been passed as test_features and value of labels as test_labels
    test_accuracy = sess.run(accuracy, feed_dict={features: test_features, labels: test_labels})

print("Test Accuracy {}".format(test_accuracy))

"""
Output:
D:\Anaconda3\envs\tensorflow\python.exe C:/Users/Intruder/PycharmProjects/Tensor_Flow_Pods/epochs_usingTensor.py
Extracting /datasets/ud730/mnist\train-images-idx3-ubyte.gz
Extracting /datasets/ud730/mnist\train-labels-idx1-ubyte.gz
Extracting /datasets/ud730/mnist\t10k-images-idx3-ubyte.gz
Extracting /datasets/ud730/mnist\t10k-labels-idx1-ubyte.gz
2017-09-09 16:47:14.619455: W C:\tf_jenkins\home\workspace\rel-win\M\windows\PY\35\tensorflow\core\platform\cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use AVX instructions, but these are available on your machine and could speed up CPU computations.
2017-09-09 16:47:14.619719: W C:\tf_jenkins\home\workspace\rel-win\M\windows\PY\35\tensorflow\core\platform\cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use AVX2 instructions, but these are available on your machine and could speed up CPU computations.
Epochs: 0    - Cost: 10.0     Valid accuracy: 0.114
Epochs: 1    - Cost: 9.21     Valid accuracy: 0.148
Epochs: 2    - Cost: 8.58     Valid accuracy: 0.159
Epochs: 3    - Cost: 8.06     Valid accuracy: 0.159
Epochs: 4    - Cost: 7.61     Valid accuracy: 0.159
Epochs: 5    - Cost: 7.22     Valid accuracy: 0.159
Epochs: 6    - Cost: 6.86     Valid accuracy: 0.159
Epochs: 7    - Cost: 6.54     Valid accuracy: 0.17
Epochs: 8    - Cost: 6.24     Valid accuracy: 0.193
Epochs: 9    - Cost: 5.96     Valid accuracy: 0.193
Test Accuracy 0.30880001187324524
"""