#! /usr/local/bin/python3
# Coding: latin1
import os
import pandas as pd
import numpy as np
import multiprocessing as mp

###
# This code allows to extract seasonnal means from hourly downscaled timeseries.
# The seasonnal mean computed with this script should be further used in the
# script plot_assessment.R
#
# This code is related to the paper: Climate change scenarios at hourly time-step
# over Switzerland from an enhanced temporal downscaling approach,
# published in the International Journal of Climatology, doi:10.1002/joc.7032
#
# Copyright (C) 2021, Adrien Michel, EPFL
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
###

LOOKUP_SEASON = {
    11: '4Autumn',
    12: '1Winter',
    1: '1Winter',
    2: '1Winter',
    3: '2Spring',
    4: '2Spring',
    5: '2Spring',
    6: '3Summer',
    7: '3Summer',
    8: '3Summer',
    9: '4Autumn',
    10: '4Autumn'}

def get_season(x):
    return(LOOKUP_SEASON[x.month])

class ReadSmet:
  def __init__(self,currDir,station,scenario,period,h,length):
    # Adapt path if needed to find input downscaled data
    data_file=currDir+"/output_"+str(length)+"_"+str(h)+"/"+station+"/"+period+"/"+station+"_"+scenario+"_"+period+".smet"
    # Read header and fields
    print("Reading ",data_file)
    with open(data_file, 'r') as f:
      line = next(f)
      fields = []
      pos_read = 1
      self.__header=[]
      while not line.startswith("[DATA]"):
        line = line.strip()
        if line.startswith("fields"):
          fields = line.split("=")[1].strip().split(" ")
        else:
          self.__header.append(line)
        line = next(f)
        pos_read = pos_read + 1
    self.__header.append("[DATA]")
    data = pd.read_csv(data_file, delimiter='\t', dtype=np.float64,
                       skiprows=pos_read, names=fields[1:], index_col=0, parse_dates=[0])

    cols=data.columns

    data=data.astype(np.float64)
    data.replace(-999, np.NaN, inplace=True)
    means=data.groupby(get_season).mean()

    self.output=[]
    if 'TA' in cols:
      self.output = self.output+ [str(x) for x in means['TA'].values]
    else:
      self.output.append(['NaN','NaN','NaN','NaN'])
    if 'PSUM' in cols:
      self.output = self.output+ [str(x) for x in means['PSUM'].values]
    else:
      self.output = self.output+['NaN','NaN','NaN','NaN']
    if 'RH' in cols:
      self.output = self.output+ [str(x) for x in means['RH'].values]
    else:
      self.output = self.output+['NaN','NaN','NaN','NaN']
    if 'ISWR' in cols:
      self.output = self.output+ [str(x) for x in means['ISWR'].values]
    else:
      self.output = self.output+['NaN','NaN','NaN','NaN']
    if 'VW' in cols:
      self.output = self.output+ [str(x) for x in means['VW'].values]
    else:
      self.output = self.output+['NaN','NaN','NaN','NaN']

    self.output=self.output+[scenario,period,h]

def do_work(station,periods,scenarios,h_table,length):
  currDir = os.path.dirname(os.path.realpath(__file__))
  output='seas_mean_10_ds'
  with open(output+"/"+station+".txt","w") as file:
    file.write("\t".join(["TA_DJF","TA_MAM","TA_JJA","TA_SON", \
                          "PSUM_DJF","PSUM_MAM","PSUM_JJA","PSUM_SON", \
                          "RH_DJF","RH_MAM","RH_JJA","RH_SON", \
                          "ISWR_DJF","ISWR_MAM","ISWR_JJA","ISWR_SON", \
                          "VW_DJF","VW_MAM","VW_JJA","VW_SON", \
                          "scenario","period","h"])+"\n")
  #os.makedirs(output, exist_ok=True)
    for h in h_table:
      print(h_table)
      for s in scenarios:
        for p in periods:
          smet=ReadSmet(currDir,station,s,p,h,length)
          file.write("\t".join(smet.output)+'\n')

def main():
  stations=["ABO","BAS","CDF","CHU","COV","DIS","GVE","KLO","LUG","LUZ","OTL","PAY","ROB","SAM","SBE","SCU","SHA","SIO","WFJ","ZER"]
  h_table=['3','5',"7",'9','11','13','15']
  periods=["1980_2010","2010_2040","2040_2070","2070_2100"]
  length=30

  scenarios=["CLMCOM-CCLM4_ECEARTH_EUR11_RCP45", \
          "CLMCOM-CCLM4_ECEARTH_EUR11_RCP85", \
          "CLMCOM-CCLM4_HADGEM_EUR11_RCP45", \
          "CLMCOM-CCLM4_HADGEM_EUR11_RCP85", \
          "CLMCOM-CCLM4_HADGEM_EUR44_RCP85", \
          "CLMCOM-CCLM4_MPIESM_EUR11_RCP45", \
          "CLMCOM-CCLM4_MPIESM_EUR11_RCP85", \
          "CLMCOM-CCLM4_MPIESM_EUR44_RCP45", \
          "CLMCOM-CCLM4_MPIESM_EUR44_RCP85", \
          "CLMCOM-CCLM5_ECEARTH_EUR44_RCP85", \
          "CLMCOM-CCLM5_HADGEM_EUR44_RCP85", \
          "CLMCOM-CCLM5_MIROC_EUR44_RCP85", \
          "CLMCOM-CCLM5_MPIESM_EUR44_RCP85", \
          "DMI-HIRHAM_ECEARTH_EUR11_RCP26", \
          "DMI-HIRHAM_ECEARTH_EUR11_RCP45", \
          "DMI-HIRHAM_ECEARTH_EUR11_RCP85", \
          "DMI-HIRHAM_ECEARTH_EUR44_RCP45", \
          "DMI-HIRHAM_ECEARTH_EUR44_RCP85", \
          "ICTP-REGCM_HADGEM_EUR44_RCP85", \
          "KNMI-RACMO_ECEARTH_EUR44_RCP45", \
          "KNMI-RACMO_ECEARTH_EUR44_RCP85", \
          "KNMI-RACMO_HADGEM_EUR44_RCP26", \
          "KNMI-RACMO_HADGEM_EUR44_RCP45", \
          "KNMI-RACMO_HADGEM_EUR44_RCP85", \
          "MPICSC-REMO1_MPIESM_EUR11_RCP26", \
          "MPICSC-REMO1_MPIESM_EUR11_RCP45", \
          "MPICSC-REMO1_MPIESM_EUR11_RCP85", \
          "MPICSC-REMO1_MPIESM_EUR44_RCP26", \
          "MPICSC-REMO1_MPIESM_EUR44_RCP45", \
          "MPICSC-REMO1_MPIESM_EUR44_RCP85", \
          "MPICSC-REMO2_MPIESM_EUR11_RCP26", \
          "MPICSC-REMO2_MPIESM_EUR11_RCP45", \
          "MPICSC-REMO2_MPIESM_EUR11_RCP85", \
          "MPICSC-REMO2_MPIESM_EUR44_RCP26", \
          "MPICSC-REMO2_MPIESM_EUR44_RCP45", \
          "MPICSC-REMO2_MPIESM_EUR44_RCP85", \
          "SMHI-RCA_CCCMA_EUR44_RCP45", \
          "SMHI-RCA_CCCMA_EUR44_RCP85", \
          "SMHI-RCA_CSIRO_EUR44_RCP45", \
          "SMHI-RCA_CSIRO_EUR44_RCP85", \
          "SMHI-RCA_ECEARTH_EUR11_RCP26", \
          "SMHI-RCA_ECEARTH_EUR11_RCP45", \
          "SMHI-RCA_ECEARTH_EUR11_RCP85", \
          "SMHI-RCA_ECEARTH_EUR44_RCP26", \
          "SMHI-RCA_ECEARTH_EUR44_RCP45", \
          "SMHI-RCA_ECEARTH_EUR44_RCP85", \
          "SMHI-RCA_GFDL_EUR44_RCP45", \
          "SMHI-RCA_GFDL_EUR44_RCP85", \
          "SMHI-RCA_HADGEM_EUR11_RCP45", \
          "SMHI-RCA_HADGEM_EUR11_RCP85", \
          "SMHI-RCA_HADGEM_EUR44_RCP26", \
          "SMHI-RCA_HADGEM_EUR44_RCP45", \
          "SMHI-RCA_HADGEM_EUR44_RCP85", \
          "SMHI-RCA_IPSL_EUR11_RCP45", \
          "SMHI-RCA_IPSL_EUR11_RCP85", \
          "SMHI-RCA_IPSL_EUR44_RCP45", \
          "SMHI-RCA_IPSL_EUR44_RCP85", \
          "SMHI-RCA_MIROC_EUR44_RCP26", \
          "SMHI-RCA_MIROC_EUR44_RCP45", \
          "SMHI-RCA_MIROC_EUR44_RCP85", \
          "SMHI-RCA_MPIESM_EUR11_RCP45", \
          "SMHI-RCA_MPIESM_EUR11_RCP85", \
          "SMHI-RCA_MPIESM_EUR44_RCP26", \
          "SMHI-RCA_MPIESM_EUR44_RCP45", \
          "SMHI-RCA_MPIESM_EUR44_RCP85", \
          "SMHI-RCA_NORESM_EUR44_RCP26", \
          "SMHI-RCA_NORESM_EUR44_RCP45", \
          "SMHI-RCA_NORESM_EUR44_RCP85"]

  # Loop over scenarios and do the computation
  ### UNCOMMENT LINE BELOW FOR PARALLEL EXECUTION ###
  # pool = mp.Pool(10)
  for station in stations:
    ### COMMENT LINE FOR PARALLEL EXECUTION ###
    do_work(station,periods,scenarios,h_table,length)
    ### UNCOMMENT LINES BELOW FOR PARALLEL EXECUTION ###
    #pool.apply_async(do_work,[station,periods,scenarios,h_table,length])
  #pool.close()
  #pool.join()

if __name__ == "__main__":
  main()


import json
import unittest
import urllib
import urllib2
import geo

class ElevationProvider:
    def __init__(self, conf = {}):
       self.conf = conf

    def getElevationData(self, points):
        raise NotImplementedError('Abstract method')

class ElevationProviderGoogle(ElevationProvider):
    ELEVATION_BASE_URL = 'https://maps.googleapis.com/maps/api/elevation/json'

class ElevationProviderMapQuest(ElevationProvider):
    """MapQuest

    http://open.mapquestapi.com/elevation/
    """

    ELEVATION_BASE_URL = 'http://open.mapquestapi.com/elevation/v1/profile'

    def getElevationData(self, locations):
        """Get elevation data from Map Quest server"""

        if 'key' not in self.conf:
            raise RuntimeError('key parameter is required')

        # convert all points to single sequence of numbers
        reqPoints = []
        for l in locations:
            reqPoints.append(l.lat)
            reqPoints.append(l.lon)

        urlParams = {
            'format': 'json',
        }
        url = self.ELEVATION_BASE_URL + '?key=' + self.conf['key'] + '&' + urllib.urlencode(urlParams)
        body = { 'latLngCollection': reqPoints }
        postData = json.dumps(body)
        headers = {'Content-Type': 'application/json'}
        req = urllib2.Request(url, postData, headers)
        f = urllib2.urlopen(req)
        response = json.load(f)
        f.close()

        resInfo = response['info']
        if resInfo['statuscode'] != 0:
            raise RuntimeError(' '.join(resInfo['messages']))

        eleProfile = response['elevationProfile']

        for ix, loc in enumerate(locations):
            loc.ele = eleProfile[ix]['height']

class ElevationProviderFactory:
    __providers = { 'mapquest': ElevationProviderMapQuest }

    @staticmethod
    def getProviders():
        return ElevationProviderFactory.__providers

    @staticmethod
    def getProvider(id):
        if id in ElevationProviderFactory.__providers:
            return ElevationProviderFactory.__providers[id]
        return None

### Unit Testing #########################################

class UnitTests(unittest.TestCase):
    def testElevationMapQuest(self):
        holedna = geo.GeoLocation(49.1990681, 16.5280778)
        trista = geo.GeoLocation(49.2067019, 16.5137869)
        doma = geo.GeoLocation(49.2203092, 16.5558653)

        ep = ElevationProviderMapQuest()
        self.assertRaises(RuntimeError, ep.getElevationData, [])

        ep = ElevationProviderMapQuest({'key': 'yourkey'})
        ep.getElevationData([holedna, trista, doma])

    def testFactory(self):
        pList = ElevationProviderFactory.getProviders()
        self.assertTrue('mapquest' in pList)
        p = ElevationProviderFactory.getProvider('xxx')
        self.assertTrue(p is None)
        p = ElevationProviderFactory.getProvider('mapquest')
        self.assertTrue(p == ElevationProviderMapQuest)

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


import queue
import logging
import threading
import typing as typ

from loguru import logger

import PIL.Image
import numpy as np
import trafaret as t

from . import utils
from .error import ConfigError, VideoDisplayError

try:
    import cv2
except ImportError:
    cv2 = None

class VideoDisplay:
    """Video display based on OpenCV lib.

    Options:
        - title - window title (default: empty)
        - width - display width size (default: get from frame)
        - height - display height size (default: get from frame))
    """
    _config_schema = t.Dict({
        t.Key('title', default=''): t.String(allow_blank=True),
        t.Key('width', optional=True): t.Int(gte=16),
        t.Key('height', optional=True): t.Int(gte=16),
    }, ignore_extra='*')

    def __init__(self, config: typ.Optional[dict] = None):
        try:
            config = self._config_schema.check(config or {})
        except (t.DataError, ValueError) as err:
            raise ConfigError(
                'Wrong {} configuration: {}'.format(self, err))

        self._config = config

        self._queue = queue.Queue(maxsize=2)
        self._end_event = threading.Event()
        self._display_thread = threading.Thread(target=self._frames_display_loop)

    @property
    def config(self) -> dict:
        return self._config

    def __str__(self) -> str:
        return self.__class__.__name__

    def __repr__(self) -> str:
        return '<{}>'.format(self)

    def __enter__(self):
        self.startup()
        return self

    def __exit__(self, exc_type, exc_val, exc_tb):
        self.shutdown()

    def startup(self) -> None:
        """Start video display thread"""
        logger.info('Starting {self}')
        self._end_event.clear()
        self._display_thread.start()

    @staticmethod
    def _clean_queue(q: queue.Queue):
        while not q.empty():
            try:
                q.get_nowait()
            except queue.Empty:
                break

    def shutdown(self) -> None:
        """Destroy video display thread and close window"""
        self._end_event.set()
        self._clean_queue(self._queue)

        if self._display_thread.is_alive():
            self._display_thread.join(timeout=1)

        if cv2:
            cv2.destroyAllWindows()

        logger.info(f'{self} successfully destroyed')

    def _frames_display_loop(self) -> None:
        """Display frames from queue"""
        while not self._end_event.is_set():
            try:
                draw_frame = self._queue.get(timeout=1)
            except queue.Empty:
                continue

            if cv2:
                cv2.imshow(self.config['title'], cv2.cvtColor(draw_frame, cv2.COLOR_RGB2BGR))
                cv2.waitKey(1)

    def display(self, draw_frame: typ.Union[np.ndarray, PIL.Image.Image]) -> None:
        """Display next video frame"""
        if cv2 is None or self._end_event.is_set():
            return

        draw_frame = np.array(draw_frame).astype(np.uint8)

        if len(draw_frame.shape) != 3:
            raise VideoDisplayError('{} got wrong frame shape {}'.format(self, draw_frame.shape))

        height, width, bpp = draw_frame.shape
        if bpp != 3:
            raise VideoDisplayError('{} supports only RGB pixel format'.format(self))

        display_size = (self.config.get('width'), self.config.get('height'))
        if all(display_size) and (width, height) != display_size:
            draw_frame = utils.resize(draw_frame, display_size)

        self._queue.put(draw_frame)


def count_inver(arr):
    return _count_inver(arr, [0] * len(arr), 0, len(arr) - 1)

def _count_inver(arr, aux, beg, end):
    if beg == end:
        return 0
    mid = beg + (end - beg) // 2
    left_inv = _count_inver(arr, aux, beg, mid)
    right_inv = _count_inver(arr, aux, mid + 1, end)

    aux[beg: end + 1] = arr[beg: end + 1]

    split_inv = 0
    i, k, j = beg, beg, mid + 1

    while k <= end:
        if i == mid + 1:
            arr[k: end + 1] = aux[j: end + 1]
            break
        if j == end + 1:
            arr[k: end + 1] = aux[i: mid + 1]
            break
        if aux[i] <= aux[j]:
            arr[k] = aux[i]
            i += 1
        else:
            arr[k] = aux[j]
            j += 1
            split_inv += (mid - i + 1)
        k += 1

    return left_inv  + right_inv + split_inv

def brute_force_inver(arr):
    num_inv = 0
    for i in range(len(arr) - 1):
        for j in range(i + 1, len(arr)):
            if arr[j] < arr[i]:
                num_inv += 1
    return num_inv

if __name__ == "__main__":
    import random
    length = 1000
    arr = [random.randint(0, 9999) for _ in range(length)]
    arr1 = list(arr)
    print(count_inver(arr))
    assert arr == sorted(arr)
    print(brute_force_inver(list(arr1)))


from carla_environment import CARLAEnvironment
from environment_abstract import AbstractEnvironment

from episode import SupervisedEpisode
from visualization import make_movie

from environment_waymo import WaymoEnvironment
from settings import NBR_MEASURES

import numpy as np
import os

class SupervisedEnvironment(AbstractEnvironment):

    def __init__(self, path, sess, writer, gradBuffer, log, settings, net=None):
        super(SupervisedEnvironment, self).__init__(path, sess, writer, gradBuffer, log, settings, net=net)
        self.init_methods_train = [-1]
        self.init_methods_test = [-1]
        self.init_methods_val=[-1]#[8, 6, 2, 4]

        print("Supervised Env ")

    def init_episode(self, cars_dict_sample, cars_sample, init_frames, init_frames_cars, people_dict_sample,
                     people_sample, pos_x, pos_y, seq_len_pfnn, tensor, training):
        if training:

            episode = SupervisedEpisode(tensor, people_sample, cars_sample, pos_x, pos_y, self.settings.gamma,
                                        self.settings.seq_len_train, self.settings.reward_weights,
                                        agent_size=self.settings.agent_shape, people_dict=people_dict_sample,
                                        cars_dict=cars_dict_sample,
                                        init_frames=init_frames, follow_goal=self.settings.goal_dir,
                                        action_reorder=self.settings.reorder_actions,
                                        threshold_dist=self.settings.threshold_dist, init_frames_cars=init_frames_cars,
                                        temporal=self.settings.temporal, predict_future=self.settings.predict_future,
                                        run_2D=self.settings.run_2D,
                                        velocity_actions=self.settings.velocity or self.settings.continous,
                                        seq_len_pfnn=seq_len_pfnn,
                                        end_collide_ped=self.settings.end_on_bit_by_pedestrians,
                                        stop_on_goal=self.settings.stop_on_goal, defaultSettings=self.settings)
        else:
            while len(cars_sample) < self.settings.seq_len_test:
                if self.settings.carla:
                    cars_sample.append([])
                    people_sample.append([])
                else:
                    cars_sample.append(cars_sample[-1])
                    people_sample.append(people_sample[-1])
            episode = SupervisedEpisode(tensor, people_sample, cars_sample, pos_x, pos_y, self.settings.gamma,
                                        self.settings.seq_len_test, self.settings.reward_weights,
                                        agent_size=self.settings.agent_shape, people_dict=people_dict_sample,
                                        cars_dict=cars_dict_sample,
                                        init_frames=init_frames, follow_goal=self.settings.goal_dir,
                                        action_reorder=self.settings.reorder_actions,
                                        threshold_dist=self.settings.threshold_dist, init_frames_cars=init_frames_cars,
                                        temporal=self.settings.temporal, predict_future=self.settings.predict_future,
                                        run_2D=self.settings.run_2D,
                                        velocity_actions=self.settings.velocity or self.settings.continous,
                                        seq_len_pfnn=seq_len_pfnn,
                                        end_collide_ped=self.settings.end_on_bit_by_pedestrians,
                                        stop_on_goal=self.settings.stop_on_goal, defaultSettings=self.settings)
        return episode
    def agent_initialization(self, agent, episode, itr,  poses_db, training, init_m=-1, set_goal=""):
        init_key = itr
        print(("Init key "+str(init_key)))
        # 1,6,2,4
        init_names = {1: "on ped", 6: "near ped", 2: "by car", 4: "random", -1: "train"}

        if itr >= len(episode.valid_keys) and len(episode.valid_keys) > 0:
            init_key = itr % len(episode.valid_keys)
            print(("Itr "+str(itr)+" init key "+str(init_key)+" "+str(len(episode.valid_keys))))

        if init_m > 0:
            print(("init_m- initialization method "+str(init_m)+" "+str(init_key)+" "+str(training)))
            pos, indx, vel = episode.initial_position(poses_db, training=training, init_key=init_key, initialization=init_m)
        elif len(episode.valid_keys) > 0:
            print(("valid_keys"+" "+str(init_key)+" "+str(training)+" len "+str(len(episode.valid_keys))))
            pos, indx, vel = episode.initial_position(poses_db, training=training, init_key=init_key)
        else:
            print(("training "+str(training)))
            pos, indx, vel = episode.initial_position(poses_db, training=training)

        return pos, indx, vel

    def get_repeat_reps_and_init(self, training,val=-1, validation=False):
        if validation:
            repeat_rep = self.settings.update_frequency_test
            self.init_methods = self.init_methods_val  # self.init_methods_train
            print(("Init methods "+str(self.init_methods)+" "+str(repeat_rep)))
        elif training:
            repeat_rep = min(val, self.settings.update_frequency)
            self.init_methods = self.init_methods_train  # self.init_methods_train
        else:
            repeat_rep = self.settings.update_frequency_test
            self.init_methods=self.init_methods_test  # self.init_methods_train
        return repeat_rep, self.init_methods

    def visualize(self, episode, file_name, statistics, training, poses, agent):
        return
        seq_len = self.settings.seq_len_train
        if not training:
            seq_len = self.settings.seq_len_test
        jointsParents=None
        if self.settings.pfnn:
            jointsParents = agent.PFNN.getJointParents()
        self.frame_counter = make_movie(episode.people,
                                        episode.reconstruction,
                                        [statistics],
                                        self.settings.width,
                                        self.settings.depth,
                                        seq_len,
                                        self.frame_counter,
                                        self.settings.agent_shape,
                                        self.settings.agent_shape_s, episode,
                                        poses,
                                        training=training,
                                        name_movie=self.settings.name_movie,
                                        path_results=self.statistics_dir + "/agent/",
                                        episode_name=os.path.basename(file_name),
                                        action_reorder=self.settings.reorder_actions,
                                        inits_frames=True,
                                        velocity_show=self.settings.velocity,
                                        velocity_sigma=self.settings.velocity_sigmoid,
                                        jointsParents=jointsParents,
                                        continous=self.settings.continous)

    def act_and_learn(self, agent, agent_file, episode, poses_db, training,saved_files_counter,  road_width=0, curriculum_prob=0, time_file=None, pos_init=[], viz=False, set_goal="", viz_frame=-1):
    # (self, agent, agent_file, episode, poses_db, training, saved_files_counter, road_width=0,
    #                   curriculum_prob=0, time_file=None, pos_init=[], viz=False):

        num_episode = 0
        cum_r = 0
        init_methods = []

        repeat_rep, init_methods = self.get_repeat_reps_and_init(training, val=len(episode.valid_keys))
        poses = np.zeros((repeat_rep, episode.seq_len * 60 // episode.frame_rate + episode.seq_len, 93 + 2 + 1 + 1 + 512),
                     dtype=np.float64)
        statistics = np.zeros((repeat_rep, (episode.seq_len - 1), 38 + NBR_MEASURES + 1), dtype=np.float64)
        people_list = np.zeros((repeat_rep, episode.seq_len, 6), dtype=np.float64)
        car_list = np.zeros((repeat_rep, episode.seq_len, 6), dtype=np.int)
        self.counter = 0
        # Go through all episodes in a gradient batch.
        if True:
            for repeat_counter in range(len(episode.valid_keys)):#repeat_rep):

                cum_r = 0
                pos = []

                initialization = self.get_next_initialization(training)
                print(("Init "+str(initialization)))
                #print "Iteration "+str(repeat_counter)+" "+str(len(episode.valid_keys))
                pos, indx, vel = self.agent_initialization(agent, episode, repeat_counter, poses_db, training, init_m=initialization)
                #print "Inititialize of ped " + str(indx)
                if len(pos)>0:
                    #self.save_people_cars(itr, episode, people_list, car_list)
                    agent.initial_position(pos,episode.goal, episode.current_frame)
                    print(("Done with initialization " + str(episode.goal_person_id)))
                    if self.net:
                        self.net.reset_mem()
                    # This is the main loop of what happens at each frame!
                    correct_count=0
                    avg_error=0

                    number_frames=episode.seq_len - 1

                    person_key=episode.valid_keys[indx]
                    if len(episode.valid_people_tracks[person_key])-2<number_frames:
                        number_frames=len(episode.valid_people_tracks[person_key])-2
                    #print "Number of frames "+str(number_frames)+" len: "+str(number_frames)
                    for frame in range(0, number_frames):
                        #print "Enviornment frame "+str(frame)
                        value = agent.next_action(episode, training )#,filename=self.statistics_file_name(agent_file, episode.pos[0], episode.pos[1], training, saved_files_counter) + '.pkl')#,filename_weights=
                                  # self.statistics_file_name(agent_file,  episode.pos[0], episode.pos[1], training, saved_files_counter) + '_weights.pkl'  )

                        desired=np.mean(episode.valid_people_tracks[person_key][agent.frame+1], axis=1)
                        coming_pos=agent.pos_exact+value
                        # print "Agent pos "+str(agent.pos_exact)+" desired pos "+str(desired)+" true pos "+str(coming_pos)+" "
                        # print "Agent takes action "+str(value[1:])+" desired action "+str(desired[1:]-agent.pos_exact[1:])+" error: "+str(coming_pos[1:]-desired[1:])
                        avg_error+=((desired[1:]-coming_pos[1:])/number_frames)
                        if (episode.get_valid_action(agent.frame, person_key)==episode.action[agent.frame]):
                            correct_count+=1
                        # print value
                        if episode.init_method==1 :
                            #print "Init method 1 "
                            agent.perform_action(value, episode, prob=curriculum_prob,person_id=indx, training=True)
                        else:
                            agent.perform_action(value, episode, prob=curriculum_prob, person_id=indx, training=training)

                        #print "Reward "+str(reward)
                        # print str(frame)+" Number non-zero" + str(len(np.nonzero(episode.reconstruction[:, :, :, 4])[0]))
                    number_frames=number_frames+1

                    while number_frames<episode.seq_len:

                        episode.agent[number_frames]= episode.agent[number_frames-1]
                        episode.velocity[number_frames-1] = np.zeros(3, dtype=int)
                        number_frames= number_frames+ 1
                    print(("Loss "+str(np.sum(episode.loss))+ "Error: "+str(avg_error)))#+" Accuracy: "+str(correct_count*1.0/number_frames)
                    #print "Error: "+str(avg_error)
                    for frame in range(0, episode.seq_len - 1):
                        reward = episode.calculate_reward(frame, person_id=indx)  # Return to this!
                    # Calculate discounted reward

                    episode.discounted_reward()
                    # Save all of the gathered statistics.
                    #  statistics, num_episode, poses, ped_seq_len=-1)
                    episode.save(statistics, num_episode, poses)
                    # if self.settings.curriculum_goal:
                    #     self.goal_dist_reached = episode.get_goal_dist_reached()
                    # else:
                    #     self.goal_dist_reached = -1
                    # Increase number of processed episodes.
                    num_episode += 1
                    self.num_sucessful_episodes += 1
                    cum_r += np.sum(np.copy(episode.reward))

                    # print "  Reward: " + str(cum_r / self.settings.update_frequency)

                #statistics = agent.evaluate(statistics, episode, poses)

                # Save the loss of the agent.

                # Save all statistics to a file.
                print(("Statistics file: " + self.statistics_file_name(agent_file, episode.pos[0], episode.pos[1],
                                                                      training,
                                                                      saved_files_counter)))

                np.save(self.statistics_file_name(agent_file, episode.pos[0], episode.pos[1], training, saved_files_counter)+".npy",
                        statistics)

                if np.sum(episode.reconstruction[0, :, :, 3]) > 0:
                    np.save(self.statistics_file_name(agent_file, episode.pos[0], episode.pos[1], training,
                                                      saved_files_counter) + "reconstruction.npy",
                            episode.reconstruction[0, :, :, 3:5])
                saved_files_counter = saved_files_counter + 1
                return statistics, saved_files_counter, people_list, car_list, poses
                #return statistics, saved_files_counter, people_list, car_list
        else:

            statistics = np.zeros((repeat_rep, (episode.seq_len - 1), 38 + NBR_MEASURES + 1), dtype=np.float64)
            poses = np.zeros((repeat_rep, (episode.seq_len - 1), 93 + 2 + 1), dtype=np.float64)
            for itr in range(repeat_rep):
                print(("Repeat "+str(itr)+" of "+str(repeat_rep)))
                if len(episode.valid_keys) > 0:
                    self.save_people_cars(itr, episode, people_list, car_list)
                    frame_observed=12
                    pos, indx = episode.initial_position_n_frames(frame_observed, itr%len(episode.valid_keys))
                    while len(pos) > 0 and frame_observed<450:

                        print(("Initialize on frame " + str(frame_observed)))
                        agent.initial_position(pos, episode.goal, episode.current_frame)
                        agent.frame=frame_observed
                        print(("Done with initialization " + str(episode.goal_person_id)+" on frame "+str(frame_observed)+" "+str(episode.goal_person_id_val)))

                        if self.net:
                            self.net.reset_mem()
                        # This is the main loop of what happens at each frame!
                        correct_count = 0

                        number_frames = self.settings.N
                        if training and len(episode.valid_people_tracks[indx]) - frame_observed-1 < number_frames:
                            number_frames =min( len(episode.valid_people_tracks[indx]) -  frame_observed-1, len(episode.velocity)-frame_observed)
                        print(("Starting frame "+str(agent.frame)+" next: "+str(number_frames)))
                        for frame in range(frame_observed, min(frame_observed+number_frames,episode.ped_seq_len)):

                            value = agent.next_action(episode, training, filename=self.statistics_file_name(agent_file, episode.pos[0], episode.pos[1], training, saved_files_counter) + '.pkl')#,filename_weights=
                                   #self.statistics_file_name(agent_file,  episode.pos[0], episode.pos[1], training, saved_files_counter) + '_weights.pkl'  )
                            # if len(value)>0:
                            #     if (episode.get_valid_action(agent.frame, indx) == episode.action[agent.frame]):
                            #         correct_count += 1

                            agent.perform_action(value, episode, prob=curriculum_prob, person_id=indx,training=False)
                        print(("Loss " + str(np.sum(episode.loss)) + " Accuracy: " + str( correct_count * 1.0 / number_frames)))

                        # print "Reward "+str(reward)
                        # print str(frame)+" Number non-zero" + str(len(np.nonzero(episode.reconstruction[:, :, :, 4])[0]))

                        # while frame in range(frame_observed, min(frame_observed+number_frames,episode.ped_seq_len)):
                        #     episode.time_dependent_evaluation(frame, episode.goal_person_id)  # Agent on Pavement?
                        #     episode.evaluate_measures(frame)  # Return to this!

                        # Calculate discounted reward

                        #episode.discounted_reward()
                        # Save all of the gathered statistics.
                        episode.save(statistics, num_episode, poses, episode.ped_seq_len)

                        # Increase number of processed episodes.
                        num_episode += 1
                        self.num_sucessful_episodes += 1
                        if self.num_sucessful_episodes% repeat_rep==0 and self.num_sucessful_episodes>0:
                            episode.save_loss(statistics)
                            np.save(self.statistics_file_name(agent_file, episode.pos[0], episode.pos[1], training,
                                                              saved_files_counter)+".npy",
                                    statistics)
                            num_episode = 0
                            saved_files_counter = saved_files_counter + 1
                            print(("Statistics file: " + self.statistics_file_name(agent_file, episode.pos[0],
                                                                                episode.pos[1], training,
                                                                                saved_files_counter)))
                        cum_r += np.sum(np.copy(episode.reward))

                        print(("  Reward: " + str(cum_r / self.settings.update_frequency)))
                        frame_observed=frame_observed+1
                        pos, indx = episode.initial_position_n_frames(frame_observed, itr%len(episode.valid_keys))

        saved_files_counter = saved_files_counter + 1
        return statistics, saved_files_counter, people_list, car_list, poses

    def act_and_learn_eval(self, agent, agent_file, episode, poses_db, training, saved_files_counter, road_width=0,
                      curriculum_prob=0, time_file=None, pos_init=[], viz=False):

        print(training)
        num_episode = 0
        cum_r=0
        init_methods=[]
        repeat_rep,_ = self.get_repeat_reps_and_init(training, validation=True)

        statistics = np.zeros((repeat_rep, (episode.seq_len - 1), 38+NBR_MEASURES+1), dtype=np.float64)
        people_list=np.zeros((repeat_rep, episode.seq_len ,6), dtype=np.float64)
        car_list = np.zeros((repeat_rep, episode.seq_len,6), dtype=np.int)
        self.counter=0
        # Go through all episodes in a gradient batch.
        print((episode.seq_len))
        for ep_itr in range(repeat_rep):

            cum_r = 0
            pos=[]
            while len(pos)==0:
                initialization = self.get_next_initialization(training=False)
                pos, indx, vel = self.agent_initialization(agent, episode, ep_itr, poses_db, training, init_m=initialization)

            self.save_people_cars(ep_itr, episode, people_list, car_list)
            #print "Initial position "+str(pos)+" "
            agent.initial_position(pos, episode.goal)
            if self.net:
                self.net.reset_mem()
            # This is the main loop of what happens at each frame!

            for frame in range(0, episode.seq_len-1):
                value = agent.next_action(episode, training=False)
                #print str(frame) + " " + str(value) + " " + str(episode.agent[frame]) + " " + str(episode.agent[frame + 1])+ " "+str(agent.frame)+" "+str(agent.position)
                agent.perform_action(value, episode, training=False)
                #print str(frame) + " " + str(value)+" "+str(episode.agent[frame])+" "+str(episode.agent[frame+1])
            for frame in range(0, episode.seq_len - 1):
                reward= episode.calculate_reward(frame,episode_done=True )  # Return to this!
                #print str(frame)+" Number non-zero" + str(len(np.nonzero(episode.reconstruction[:, :, :, 4])[0]))

            # Calculate discounted reward
            episode.discounted_reward()
            # Save all of the gathered statistics.
            episode.save(statistics, num_episode)
            # if self.settings.curriculum_goal:
            #     self.goal_dist_reached=episode.get_goal_dist_reached()
            # else:
            #     self.goal_dist_reached =-1
            # Increase number of processed episodes.
            num_episode+=1
            self.num_sucessful_episodes += 1
            cum_r+=np.sum(np.copy(episode.reward))

            print(("  Reward: " + str(cum_r/self.settings.update_frequency)))

            # Train the agent or evaluate it.
            if training:
                statistics=agent.train(ep_itr, statistics, episode,self.statistics_file_name(agent_file, episode.pos[0], episode.pos[1], training, saved_files_counter) + '.pkl',
                                       self.statistics_file_name(agent_file,  episode.pos[0], episode.pos[1], training, saved_files_counter) + '_weights.pkl' )
            else:
                statistics=agent.evaluate(ep_itr,statistics, episode)

            # Save the loss of the agent.
            #episode.save_loss(statistics)
            # Save all statistics to a file.
        print(("Statistics file: "+self.statistics_file_name(agent_file, episode.pos[0], episode.pos[1], training, saved_files_counter)))

        np.save(self.statistics_file_name(agent_file, episode.pos[0], episode.pos[1], training, saved_files_counter)+".npy", statistics)

        if np.sum(episode.reconstruction[0,:,:,3])>0:
            np.save(self.statistics_file_name(agent_file, episode.pos[0], episode.pos[1], training, saved_files_counter)+"reconstruction.npy", episode.reconstruction[0,:,:,3:5])
        saved_files_counter = saved_files_counter + 1
        return statistics, saved_files_counter, people_list, car_list

    # def set_up_episode(self, cachePrefix, envPly, pos_x, pos_y, training, useCaching, evaluate=False, time_file=None,
    #                    seq_len_pfnn=-1, datasetOptions=None,supervised=False):
    #     super(SupervisedEnvironment).set_up_episode(cachePrefix, envPly, pos_x, pos_y, training, useCaching,
    #                                                      evaluate,
    #                                                      time_file, seq_len_pfnn, datasetOptions,
    #                                                      supervised=True)

class SupervisedWaymoEnvironment(WaymoEnvironment, SupervisedEnvironment):
    def __init__(self, path, sess, writer, gradBuffer, log, settings, net=None):
        super(SupervisedWaymoEnvironment, self).__init__(path, sess, writer, gradBuffer, log, settings, net=net)
        self.init_methods_train = [-1]
        self.init_methods_test = [-1]
        self.init_methods_val = [-1]  # [8, 6, 2, 4]
        print("Supervised Env ")

class SupervisedCARLAEnvironment(SupervisedEnvironment, CARLAEnvironment):
    def __init__(self, path, sess, writer, gradBuffer, log, settings, net=None):
        super(SupervisedCARLAEnvironment, self).__init__(path, sess, writer, gradBuffer, log, settings, net=net)
        self.init_methods_train = [-1]
        self.init_methods_test = [-1]
        self.init_methods_val = [-1]  # [8, 6, 2, 4]
        print("Supervised Env ")


"""Test the que.py module."""

import pytest

TEST_ENQ = [
    (1, 2, 3),
    (800, 'String', 5),
    ('String', ['list'], 17),
    ([5, 3, 17], [5, 'list two'], 'another string'),
    ('monkies', ['spiders', 'spidermonkey'], ('spi', 'der', 'monk'))
]

TEST_LEN = [
    ([1], 1),
    ((17, 5, 100), 3),
    ('String', 6),
    ([5, 3, 17], 3),
    ((1, 2, 3, 4, 5, 6, 7, 8, 9), 9)
]

@pytest.fixture
def new_que():
    """Init a new doubly linked list."""
    from que import Queue
    return Queue()

def test_que_none(new_que):
    """When a new stack is created with no iterable, head should be None."""
    assert new_que.newDLL.head is None
    assert new_que.newDLL.tail is None
    assert new_que.newDLL.length == 0

@pytest.mark.parametrize('val1, val2, val3', TEST_ENQ)
def test_que_enq(val1, val2, val3, new_que):
    """Push three values and check to see that they're in the DLL."""
    new_que.enqueue(val1)
    new_que.enqueue(val2)
    new_que.enqueue(val3)
    assert new_que.newDLL.head.val == val1
    assert new_que.newDLL.head.next.val == val2
    assert new_que.newDLL.head.next.next.val == val3

@pytest.mark.parametrize('val1, val2, val3', TEST_ENQ)
def test_que_deque(val1, val2, val3, new_que):
    """Append 3 values and check that the right vals shift out."""
    new_que.enqueue(val1)
    new_que.enqueue(val2)
    new_que.enqueue(val3)
    assert new_que.dequeue() == val1
    assert new_que.dequeue() == val2
    assert new_que.dequeue() == val3

@pytest.mark.parametrize('val1, val2, val3', TEST_ENQ)
def test_que_peek(val1, val2, val3, new_que):
    """Append 3 values, peek, and check the val."""
    new_que.enqueue(val1)
    new_que.enqueue(val2)
    new_que.enqueue(val3)
    assert new_que.peek() == val1

@pytest.mark.parametrize('iterable, result', TEST_LEN)
def test_que_len(iterable, result, new_que):
    """Test the size/len of the queue."""
    for idx in range(len(iterable)):
        new_que.enqueue(iterable[idx])
    assert len(new_que) == result
    assert new_que.size() == result


class Solution:
    def nextGreaterElement(self, nums1: List[int], nums2: List[int]) -> List[int]:
        nums1Idx = {n:i for i,n in enumerate(nums1)}
        res = [-1]*len(nums1)

        for i in range(len(nums2)):
            if nums2[i] not in nums1Idx:
                continue
            for j in range(i+1,len(nums2)):
                if nums2[j]>nums2[i]:
                    idx = nums1Idx[nums2[i]]
                    res[idx] = nums2[j]
                    break
        return res


import pymel.core as pm
import maya.OpenMaya

import os
import sys

import v1_core
import v1_shared

from v1_shared.decorators import csharp_error_catcher
from v1_shared.shared_utils import get_first_or_default, get_index_or_default, get_last_or_default

from metadata import meta_network_utils, meta_property_utils
from metadata.network_registry import Network_Registry, Property_Registry

from maya_utils import node_utils
from maya_utils import fbx_wrapper

def get_scene_name():
    '''
    Gets the name of the scene without full path
    '''
    return pm.sceneName().namebase

@csharp_error_catcher
def get_scene_name_csharp(c_object, event_args):
    '''
    get_scene_name(self, c_definition, event_args)
    Event method to get the name of the scene and pass it to C#

    Args:
        c_object (None): Unused
        event_args (StringEventArgs): Used to pass the scene name string back to C#
    '''
    event_args.Value = get_last_or_default(get_scene_name().split("_", 1))

def set_current_frame():
    '''
    Set the current frame to itself to force a scene update
    '''
    # Weird issue, turning off cycle check here prevents warnings of a meaningless cycle
    cycle_check = pm.cycleCheck(q=True, e=True)
    pm.cycleCheck(e=False)

    pm.cycleCheck(e=cycle_check)

def set_current_frame_to_timerange_start():
    pm.currentTime(get_scene_times()[0])

def get_scene_times():
    '''
    Gets all values from the scene time range

    Returns:
        (float, float, float float). The full time range from the scene
    '''
    min_time = pm.playbackOptions(q=True, min=True)
    ast_time = pm.playbackOptions(q=True, ast=True)
    max_time = pm.playbackOptions(q=True, max=True)
    aet_time = pm.playbackOptions(q=True, aet=True)

    return (min_time, ast_time, max_time, aet_time)

def set_scene_times(value_tuple):
    min_time, ast_time, max_time, aet_time = value_tuple
    pm.playbackOptions(min=min_time, ast=ast_time, max=max_time, aet=aet_time)

def get_scene_fps():
    '''
    Gets the FPS that the scene is set to

    Returns:
        float. FPS value
    '''
    time = pm.currentUnit(q=True, t=True)

    if(time == 'game'):
        return 15.0
    elif(time == 'film'):
        return 24.0
    elif(time == 'pal'):
        return 25.0
    elif(time == 'ntsc'):
        return 30.0
    elif(time == 'show'):
        return 48.0
    elif(time == 'palf'):
        return 50.0
    elif(time == 'ntscf'):
        return 60.0
    else:
        print(time)

def set_playback_rate(playback_string):
    '''
    Set the Animation playback rate
    '''
    pm.currentUnit(t=playback_string)

def get_playback_rate():
    '''
    Get the Animation playback rate
    '''
    return pm.currentUnit(q=True, t=True)

def remove_empty_namespaces():
    '''
    Remove all empty namespaces in the scene
    '''
    for ns in pm.system.Namespace(':').listNamespaces():
        if not ns.listNodes():
            ns.remove()

def clean_scene():
    '''
    Clean up a maya scene of orphaned and unused objects
    '''
    # Delete orphaned controller tags
    controller_tags = [x for x in pm.ls(type='controller') if x.exists() and not x.controllerObject.listConnections()]
    if controller_tags:
        pm.delete(controller_tags)
        v1_core.v1_logging.get_logger().info("Scene Cleanup Deleted Controller Tags : \n{0}".format(controller_tags))

    # Delete unused shader nodes
    temp_cube = None
    try:
        # Since Maya 2020 an error is thrown when running Delete Unused Nodes if the StandardSurface default shader is unassigned
        # So we create a temporary object, assign the default StandardSurface material to it, then delete Unused and delete the sphere
        temp_cube = pm.polyCube(name="TEMP_StandardSurface_Assignment")[0]
        temp_standard_shader = pm.sets( renderable=True, noSurfaceShader=True, empty=True, name="standardSurface1SG" )
        standard_material = pm.PyNode("standardSurface1")
        standard_material.outColor >> temp_standard_shader.surfaceShader
        pm.sets(temp_standard_shader, edit=True, forceElement=temp_cube)

        pm.mel.eval('hyperShadePanelMenuCommand("hyperShadePanel1", "deleteUnusedNodes");')
    except Exception as e:
        v1_core.v1_logging.get_logger().info("Failed To Delete Unused Nodes:")
        v1_core.v1_logging.get_logger().info("{0}".format(e))
        pass
    finally:
        if temp_cube:
            pm.delete(temp_cube)

    # Delete empty namespaces
    delete_empty_namespaces()

    # Delete empty Display Layers
    delete_empty_display_layers()

    # Delete RenderLayers that aren't a default layer
    render_layers = [x for x in pm.ls(type='renderLayer') if x != pm.nt.RenderLayer('defaultRenderLayer')]
    if render_layers:
        pm.delete(render_layers)
        v1_core.v1_logging.get_logger().info("Scene Cleanup Deleted RenderLayers : \n{0}".format(render_layers))

    # Delete orphaned groupId nodes
    group_id_nodes = [x for x in pm.ls(type='groupId') if not x.listConnections()]
    if group_id_nodes:
        pm.delete(group_id_nodes)
        v1_core.v1_logging.get_logger().info("Scene Cleanup Deleted GroupID Nodes : \n{0}".format(group_id_nodes))

    # Delete orphaned TimeEditor tracks
    time_editor_tracks = [x for x in pm.ls(type='timeEditorTracks') if not x.listConnections()]
    if time_editor_tracks:
        pm.delete(time_editor_tracks)
        v1_core.v1_logging.get_logger().info("Scene Cleanup Deleted TimeEditorTracks : \n{0}".format(time_editor_tracks))

    # Delete orphaned GraphEditorInfo nodes
    graph_editor_info = [x for x in pm.ls(type='nodeGraphEditorInfo') if not x.listConnections()]
    if graph_editor_info:
        pm.delete(graph_editor_info)
        v1_core.v1_logging.get_logger().info("Scene Cleanup Deleted GraphEditorInfo : \n{0}".format(graph_editor_info))

    # Delete orphaned and unlocked reference nodes
    reference_nodes = [x for x in pm.ls(type='reference') if not x.listConnections() and not x.isLocked()]
    if reference_nodes:
        pm.delete(reference_nodes)
        v1_core.v1_logging.get_logger().info("Scene Cleanup Deleted References : \n{0}".format(reference_nodes))

def delete_empty_namespaces():
    '''
    Remove all empty namespaces from bottom up to remove children namespaces first
    '''
    namespace_list = []
    for ns in pm.listNamespaces( recursive  =True, internal =False):
        namespace_list.append(ns)

    # Reverse Iterate through the contents of the list to remove the deepest layers first
    for ns in reversed(namespace_list):
        if not pm.namespaceInfo(ns, ls=True):
            pm.namespace(removeNamespace = ns, mergeNamespaceWithRoot = True)

def delete_empty_display_layers():
    empty_layer_list = []
    default_display_layer = pm.PyNode('defaultLayer') if pm.objExists('defaultLayer') else None
    for display_layer in pm.ls(type="displayLayer"):
        if display_layer != default_display_layer and not display_layer.listMembers():
            empty_layer_list.append(display_layer)

    pm.delete(empty_layer_list)

def delete_empty_objects():
    pm.delete([x for x in pm.ls(assemblies=True) if not x.getShape() and not x.getChildren()])

def setup_exporter():
    '''
    Quick initial Exporter setup from scene objects.
    '''
    from metadata.network_core import CharacterCore, JointsCore
    from metadata.exporter_properties import ExportDefinition, CharacterAnimationAsset
    character_core_type = Network_Registry().get(CharacterCore)
    joints_core_type = Network_Registry().get(JointsCore)
    export_definition_type = Network_Registry().get(ExportDefinition)
    character_animation_asset_type = Property_Registry().get(CharacterAnimationAsset)

    export_definition_list = meta_network_utils.get_all_network_nodes(export_definition_type)
    current_definition = meta_network_utils.create_from_node(export_definition_list[0]) if export_definition_list else None
    export_definition = current_definition if current_definition else export_definition_type()
    export_definition.set('use_scene_name', True)

    character_list = meta_network_utils.get_all_network_nodes(character_core_type)
    for character_node in character_list:
        character_network = meta_network_utils.create_from_node(character_node)
        joints_network = character_network.get_downstream(joints_core_type)
        root_joint = node_utils.get_root_node(joints_network.get_first_connection(), 'joint')
        character_name = root_joint.namespace().strip(":").upper()

        existing_export_property = meta_property_utils.get_property(root_joint, character_animation_asset_type)
        export_property = existing_export_property if existing_export_property else character_animation_asset_type()
        export_property.set('asset_name', character_name)

        export_property.connect_node(root_joint)
        export_definition.connect_node(export_property.node)

    pm.select(None)

def fix_full_paths():
    '''
    Fixes full paths in references and texture maps so they are relative to Data\
    '''
    nodes = []

    # Creates a variable named fileNodes and is set to list all of the files
    fileNodes = pm.ls(type = "file")
    refNodes = pm.listReferences()

    nodes.extend(fileNodes)
    nodes.extend(refNodes)

    # dataPathNodes is a variable set to an empty array
    dataPathNodes = []

    # looking at a list of nodes within the fileNodes variables
    for node in nodes:
        try:
            # we are getting the fileTextureName from each specific file node and splitting each sub directory in each node
            fileNameArray = node.fileTextureName.get().replace("\\", "/").split("/")
        except:
            fileNameArray = node.unresolvedPath().split("/")

        dataNumb = None

        # look at specific elements inside of the fileName
        for element in fileNameArray:
            # element array is lowercased with .lower()
            # if there is not data found within the elements array skip the file Node
            if element != "Data":
                continue

            dataNumb = fileNameArray.index("Data")

        if not dataNumb:
            continue

        if dataNumb <= 0:
            continue

        newEndPathArray = fileNameArray[dataNumb:]
        newEndPath = "/".join(newEndPathArray)
        newDir = newEndPath

        try:
            node.fileTextureName.set(newDir)
        except:
            node.replaceWith(newDir)

def clean_reference_cameras():
    '''
    Default cameras from referenced scenes can get into animation scenes.  This finds them,
    unflags their default status, and deletes them
    '''
    for cam in pm.ls(type = "camera"):
        try:
            topParent = cam.getParent(generations = -1)
            isStartUp = pm.camera(cam, q = True, startupCamera = True)

            if topParent.isReferenced() and isStartUp:
                pm.camera(cam, e = True, startupCamera = False)
                pm.delete(cam.getParent())
        except:
            continue

def import_file_safe(file_path, fbx_mode = "add", tag_imported = False, keep_scene_time = True, **kwargs):
    '''
    Import FBX files or Maya files safely, and with the ability to return import nodes.

    Note: If an .ma file has plugin attributes from a plugin that the user does not have, the Maya importFile
    command will error and fail to return any values.  If this happens we fall back to comparing pre and post
    import scene object lists to find the imported objects.
    EDGE CASE: This method will fail to return objects if the import process parents the imported nodes into
    existing scene hierarchy

    Args:
        file_path (string): Full path to the file to import
        **kwargs (kwargs): keyword args to pass along to pm.importFile
    '''
    scene_time_tuple = get_scene_times()
    current_time = pm.currentTime()
    current_playback = get_playback_rate()

    import_return = None
    pre_import_list = pm.ls(assemblies = True)
    current_import_mode = fbx_wrapper.FBXImportMode(q=True)
    try:
        filename, extension = os.path.splitext(file_path)
        if extension.lower() == '.ma':
            import_return = pm.importFile(file_path, **kwargs)
        elif extension.lower() == '.fbx':
            fbx_file_path = file_path.replace('\\', '\\\\')

            fbx_wrapper.FBXImportMode(v = fbx_mode)
            fbx_wrapper.FBXImport(f = fbx_file_path)
    except:
        if ".ai_translator" in v1_core.exceptions.get_exception_message():
            v1_core.v1_logging.get_logger().info("Ignoring Maya to Arnold File Import Errors")
        else:
            exception_info = sys.exc_info()
            v1_core.exceptions.except_hook(exception_info[0], exception_info[1], exception_info[2])
    finally:
        fbx_wrapper.FBXImportMode(v = current_import_mode)
        set_playback_rate(current_playback)
        if (keep_scene_time):
            set_scene_times(scene_time_tuple)
            pm.currentTime(current_time)

    if not import_return and kwargs['returnNewNodes'] == True:
        post_import_list = pm.ls(assemblies = True)
        import_parent_list = [x for x in post_import_list if x not in pre_import_list]

        import_return = import_parent_list
        for import_parent in import_parent_list:
            import_return = import_return + import_parent.listRelatives(ad=True)

    if import_return and tag_imported:
        from metadata.network_core import ImportedCore
        imported_core = Network_Registry().get(ImportedCore)()
        relative_path = v1_shared.file_path_utils.full_path_to_relative(file_path)
        imported_core.set('import_path', relative_path)
        imported_core.connect_nodes(import_return)
        import_return.append(imported_core.node)

    return import_return

def export_selected_safe(file_path, **kwargs):
    filename, extension = os.path.splitext(file_path)
    if extension.lower() == '.ma':
        pm.exportSelected(file_path, force = True)
    elif extension.lower() == '.fbx':
        fbx_file_path = file_path.replace("\\", "\\\\")
        fbx_wrapper.FBXExport(f = fbx_file_path, **kwargs)

    v1_core.v1_logging.get_logger().info("export_selected_safe - {0}".format(file_path))


from .models import *
import json
from django.core.exceptions import PermissionDenied
import requests
import os
import base64
import urllib
from .models import OAuthCache
import random
from django.utils import timezone
from django.conf import settings

module_path = os.path.dirname(__file__)

REDIRECT_URI = settings.MY_BASE_URL + '/login/'
ISSUER = 'https://oidc.mit.edu/'
AUTH_CODE_URL = 'https://oidc.mit.edu/authorize'
AUTH_TOKEN_URL = 'https://oidc.mit.edu/token'
AUTH_USER_INFO_URL = 'https://oidc.mit.edu/userinfo'

LOGIN_TIMEOUT = 600
AUTH_SCOPES = ['email', 'openid', 'profile', 'offline_access']
AUTH_RESPONSE_TYPE = 'code'

def get_client_info():
    with open(os.path.join(module_path, 'oidc.txt'), 'r') as file:
        contents = file.read().strip()
        id, secret = contents.split('\n')
    return id, secret

def generate_random_string(length):
    choices = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
    return ''.join(random.choice(choices) for _ in range(length))

def oauth_code_url(request, after_redirect=None):
    """after_redirect is used to redirect to an application site with a
    temporary code AFTER FireRoad has created the user's account. It should be
    None for mobile apps and a string for websites."""

    # Create a state and nonce, and save them
    cache = OAuthCache(state=generate_random_string(48), nonce=generate_random_string(48), redirect_uri=after_redirect)
    sem = request.GET.get('sem', '')
    if len(sem) > 0:
        cache.current_semester = sem
    cache.save()
    return "{}?response_type={}&client_id={}&redirect_uri={}&scope={}&state={}&nonce={}".format(
        AUTH_CODE_URL,
        AUTH_RESPONSE_TYPE,
        get_client_info()[0],
        urllib.quote(REDIRECT_URI),
        urllib.quote(' '.join(AUTH_SCOPES)),
        cache.state,
        cache.nonce)

def get_user_info(request):
    code = request.GET.get('code', None)
    state = request.GET.get('state', None)

    caches = OAuthCache.objects.filter(state=state)
    if caches.count() == 0:
        raise PermissionDenied

    acc_token, info, all_json, status = get_oauth_id_token(request, code, state)
    if acc_token is None:
        return None, status, None

    result, status = get_user_info_with_token(request, acc_token)
    if result is not None:
        if "refresh_token" in all_json:
            result[u'refresh_token'] = all_json["refresh_token"]
    return result, status, info

def get_oauth_id_token(request, code, state, refresh=False):
    id, secret = get_client_info()

    if refresh:
        payload = {
            'grant_type': 'refresh_token',
            'refresh_token': code
        }
    else:
        payload = {
            'grant_type': 'authorization_code',
            'code': code,
            'redirect_uri': REDIRECT_URI
        }
    r = requests.post(AUTH_TOKEN_URL, auth=(id, secret), data=payload)
    if r.status_code != 200:
        return None, None, None, r.status_code

    # Parse token
    r_json = r.json()

    id_token = r_json['id_token']
    header, body, signature = id_token.split('.')
    header_text = base64.b64decode(header)
    body += "=" * ((4 - len(body) % 4) % 4)
    body_text = base64.b64decode(body)

    body = json.loads(body_text)
    if body['iss'] != ISSUER:
        raise PermissionDenied
    nonce = body['nonce']
    caches = OAuthCache.objects.filter(state=state)
    found = False
    info = {}
    for cache in caches:
        if cache.nonce == nonce:
            current_date = timezone.now()
            if (current_date - cache.date).total_seconds() > LOGIN_TIMEOUT:
                return None, None, None, 408
            found = True
            info["sem"] = cache.current_semester
            if cache.redirect_uri is not None:
                info["redirect"] = cache.redirect_uri
            break
    if not found:
        raise PermissionDenied
    caches.delete()

    access_token = r_json["access_token"]
    return access_token, info, r_json, r.status_code

def get_user_info_with_token(request, acc_token):
    headers = {"Authorization":"Bearer {}".format(acc_token)}
    r = requests.get(AUTH_USER_INFO_URL, headers=headers)
    return r.json(), r.status_code


# Linked lists

class Node:
    def __init__(self, value):
        self.value = value
        self.next = None

class LinkedList:
    """add, delete, nth, reverse, __len__"""
    def __init__(self):
        self.head = None

    def add(self, value):
        """Define new node's value and next, then make it the list's head"""
        new_node = Node(value)
        new_node.next = self.head
        self.head = new_node

    def delete(self, to_delete):
        """delete the first occurrence of value"""
        current_node = self.head
        previous_node = None

        while current_node:
            if current_node.value == to_delete:
                break
            previous_node = current_node
            current_node = current_node.next

        if current_node is None:
            print("Item not found")
            return None

        if previous_node is None:
            # special case (head is removed)
            self.head = self.head.next
        else:
            previous_node.next = current_node.next

    def nth(self, n):
        """print nth item in list"""
        current_node = self.head
        for i in range(n):
            current_node = current_node.next
        print(current_node.value)

    def reverse(self):
        """Uses O(n) memory"""
        reversed = LinkedList()
        current_node = self.head

        while current_node:
            # insert current_node in reversed
            reversed.add(current_node.value)
            current_node = current_node.next
        return reversed

    def print_me(self):
        current_node = self.head
        while True:
            if current_node:
                print(current_node.value)
                current_node = current_node.next
            else:
                break

    def __len__(self):
        length = 0
        current_node = self.head
        while current_node:
            length += 1
            current_node = current_node.next
        return length

def test_delete_head():
    alist = LinkedList()
    alist.add(3)
    alist.add(4)
    alist.add(5)
    alist.delete(5)
    alist.print_me()

def test_delete_mid():
    alist = LinkedList()
    alist.add(3)
    alist.add(4)
    alist.add(5)
    alist.delete(4)
    alist.print_me()

def test_delete_last():
    alist = LinkedList()
    alist.add(3)
    alist.add(4)
    alist.add(5)
    alist.delete(3)
    alist.print_me()

def test_delete_not_found():
    alist = LinkedList()
    alist.add(3)
    alist.add(4)
    alist.add(5)
    alist.delete(9)
    alist.print_me()

def test_reverse():
    alist = LinkedList()
    # 5 4 3
    alist.add(3)
    alist.add(4)
    alist.add(5)

    print("original:")
    alist.print_me()

    revlist = alist.reverse()
    print("reversed:")
    revlist.print_me()

def test_nth():
    alist = LinkedList()
    alist.add(3)
    alist.add(4)
    alist.add(5)

    alist.nth(0)
    alist.nth(1)

def test_print_rev():
    alist = LinkedList()
    alist.add(3)
    alist.add(4)
    alist.add(5)

    for i in range(len(alist) - 1, -1, -1):
        alist.nth(i)


# -*- coding: utf-8 -*-
import datetime
import json
import logging
import werkzeug
from werkzeug.exceptions import BadRequest
from odoo import SUPERUSER_ID, api, http, _
from odoo import registry as registry_get
from odoo.addons.auth_oauth.controllers.main import OAuthController as Controller
from odoo.addons.web.controllers.main import (login_and_redirect, ensure_db)
from odoo.exceptions import AccessDenied
from odoo.http import request

_logger = logging.getLogger(__name__)

class OAuthController(Controller):

    @http.route('/web/login/sms', type='http', auth='public', website=True, sitemap=False)
    def web_odoo_sms_login(self, **kw):
        """
        短信登录入口,点击后返回到验证码界面
        :param kw:
        :return:
        """
        if request.session.uid:
            request.session.uid = False
        if request.session.login:
            request.session.login = False
        data = {'code_maxlength': 6}   # 验证码默认最大长度
        return request.render('sms_base.sms_login_signup', data)

    @http.route('/web/sms/send/code', type='http', auth="public", website=True, sitemap=False)
    def web_sms_send_code(self, **kw):
        """
        发送验证码
        :param kw:
        :return:
        """
        values = request.params.copy()
        user_phone = values.get('user_phone')
        if not user_phone:
            return json.dumps({'state': False, 'msg': "手机号码不能为空！"})
        _logger.info("手机号码:{}正在尝试发送验证码".format(user_phone))
        # 获取服务商
        partners = request.env['sms.partner'].sudo().get_partners_priority()
        if not partners:
            return json.dumps({"state": False, 'msg': "系统未找到可用的短信服务商，请联系管理员维护！"})
        # 验证员工是否有此手机号
        domain = [('user_phone', '=', user_phone)]
        user = request.env['res.users'].sudo().search(domain, limit=1)
        if not user:
            # 判断系统是否允许自动注册
            sms_auto_login = request.env['ir.config_parameter'].sudo().get_param('sms_base.default_sms_auto_login')
            if not sms_auto_login:
                return json.dumps({'state': False, 'msg': "抱歉，您的手机号未注册，请联系管理员完善信息！"})
            # 创建用户
            user = request.env['res.users'].sudo().create_user_by_sms_login(user_phone)
            # 发送通知短信
            partners.sudo().send_registration_message(user, user_phone)
        # 使用服务商的发送验证码函数
        result = partners.sudo().send_message_code(user, user_phone, 'login')
        if result.get('state'):
            return json.dumps({"state": True, 'msg': "验证码已发送，请注意查收短信！"})
        return json.dumps({"state": False, 'msg': result.get('msg')})

    @http.route('/web/sms/user/login', type='http', auth="public", website=True, sitemap=False)
    def web_sms_user_login(self, **kw):
        """
        验证登录验证码
        :param kw:
        :return:
        """
        values = request.params.copy()
        user_phone = values.get('phone')
        code = values.get('code')
        if not user_phone or not code:
            return json.dumps({'state': False, 'msg': "手机号和验证码不正确！"})
        # 检查验证码和登录手机有效性
        domain = [('phone', '=', user_phone), ('code', '=', code), ('state', '=', 'normal')]
        records = request.env['sms.verification.record'].sudo().search(domain)
        if not records:
            return json.dumps({'state': False, 'msg': "验证码不存在,请重新输入！"})
        # 检查时效
        for record in records:
            if datetime.datetime.now() > record.end_time:
                record.sudo().write({'state': 'invalid'})
                return json.dumps({'state': False, 'msg': "验证码已失效！请重新获取!"})
        records.sudo().write({'state': 'invalid'})
        # 验证通过执行登录操作
        return self.do_post_login(user_phone)

    def do_post_login(self, user_phone):
        """
        执行登录
        :param user_phone:
        :return:
        """
        if request.session.uid:
            request.session.uid = False
        if request.session.login:
            request.session.login = False
        ensure_db()
        dbname = request.session.db
        if not http.db_filter([dbname]):
            return BadRequest()
        registry = registry_get(dbname)
        with registry.cursor() as cr:
            try:
                env = api.Environment(cr, SUPERUSER_ID, {})
                credentials = env['res.users'].sudo().auth_oauth_sms('sms', user_phone)
                cr.commit()
                url = '/web'
                resp = login_and_redirect(*credentials, redirect_url=url)
                if werkzeug.urls.url_parse(resp.location).path == '/web' and not request.env.user.has_group('base.group_user'):
                    resp.location = '/'
                return json.dumps({'state': True, 'msg': "登录成功"})
            except AttributeError:
                return json.dumps({'state': False, 'msg': "未在数据库'%s'上安装auth_signup：oauth注册已取消" % (dbname)})
            except AccessDenied:
                _logger.info('>>>SMS-OAuth2: 访问被拒绝，在存在有效会话的情况下重定向到主页，而未设置Cookie')
                url = "/web/login?oauth_error=3"
                redirect = werkzeug.utils.redirect(url, 303)
                redirect.autocorrect_location_header = False
                return redirect
            except Exception as e:
                return json.dumps({'state': False, 'msg': "OAuth2: %s" % str(e)})


score_points = {"A": 1, "B": 2, "C": 3, "X": 1, "Y": 2, "Z": 3}
win_lose = {
        "AY": 6,
        "AZ": 0,
        "BX": 0,
        "BZ": 6,
        "CX": 6,
        "CY": 0
        }
# Not in dict = draw

def part_1(data):
    points = 0

    for round in data:
        s = round[0] + round[1]
        points += win_lose.get(s, 3)
        points += score_points[round[1]]

    print("P1 = " + str(points))

def part_2(data):
    to_play = {
            "AX": "Z",
            "AY": "X",
            "AZ": "Y",
            "BX": "X",
            "BY": "Y",
            "BZ": "Z",
            "CX": "Y",
            "CY": "Z",
            "CZ": "X"
            }
    points = 0

    for round in data:
        s = round[0] + round[1]
        pick = to_play[s]
        points += win_lose.get(round[0] + pick, 3)
        points += score_points[pick]

    print("P2 = " + str(points))

with open("input.txt") as t:
    data = [x.split(" ") for x in t.read().splitlines()]
    part_1(data)
    part_2(data)


# File Opening
file_path = "sample_text_file.txt4"
file = open(file_path, 'r')
contents = file.read()

# -----------------( Calculations )-------------------

# Word Count
words = contents.split()
num_words = len(words)

# Line Count
lines = contents.split("\n")
num_lines = len(lines)

# Non-Space Characters, Spaces, Tabs Count
num_characters, num_spaces, num_tabs = 0, 0, 0
for character in contents:
	if character == " ":
		num_spaces += 1
	elif character == "\t":
		num_tabs += 1
	else:
		num_characters += 1

# Paragraph Count
paragraphs = contents.split("\n\t")
num_paragraphs = len(paragraphs)

# --------------( Calculation Ends )----------------

# ---------------------( Output )-----------------------

# Words
print(f"There are {num_words} words in the file {file_path}")

# Lines
print(f"There are {num_lines} lines in the file {file_path}")

# Characters
print(f"There are {num_characters} non-space characters in the file {file_path}")

# Spaces
print(f"There are {num_spaces} spaces in the file {file_path}")

# Tabs
print(f"There are {num_tabs} tabs in the file {file_path}")

# Paragraphs
print(f"There are {num_paragraphs} paragraphs in the file {file_path}")

# ------------------( Output Ends )-----------------

#File Closing
file.close()

# ---------------( Program Ends )-----------------

Assignment 3/Text File Reader/File Reader.py


from __future__ import absolute_import, division, print_function

import keras
from keras.datasets import mnist
from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten
from keras.layers import Conv2D, MaxPooling2D
from keras import backend as K

import tensorflow as tf
from tensorflow import keras

tf.__version__
tf.enable_eager_execution()

import sys
import os
import numpy as np
import time
import pathlib

### Create Dataset 10000 train data; 10000 test data
(train_images, train_labels), (test_images, test_labels) = tf.keras.datasets.mnist.load_data()

train_labels = train_labels[:10000].astype('int64')
test_labels = test_labels[:10000].astype('int64')
train_images = train_images[:10000].reshape(-1, 28 * 28) / 255.0
test_images = test_images[:10000].reshape(-1, 28 * 28) / 255.0

ds = tf.data.Dataset.from_tensor_slices((test_images, test_labels)).batch(1)

# Returns a short sequential model
def create_model():
  model = tf.keras.models.Sequential([
    keras.layers.Dense(512, activation=tf.keras.activations.relu, input_shape=(784,)),
    keras.layers.Dropout(0.2),
    keras.layers.Dense(10, activation=tf.keras.activations.softmax)
  ])
  opt = tf.train.AdamOptimizer()
  model.compile(optimizer=opt,
              loss='sparse_categorical_crossentropy',
              metrics=['accuracy'])
  return model

# Create a basic model instance
model = create_model()
model.summary()
model.fit(train_images, train_labels, epochs=5)

# eval test
start = time.time()
loss, acc = model.evaluate(test_images, test_labels)
end = time.time()
print("time pass =", end-start)
print("eval model, accuracy: {:5.2f}%".format(100*acc))
print('loss=',loss)

def eval_model_original(predictions, mnist_ds):

  total_seen = 0
  num_correct = 0

  for i, (img, label) in enumerate(mnist_ds):
    total_seen += 1
    pred = np.argmax(predictions[i])
    if pred == label.numpy():
      num_correct += 1

    if total_seen % 500 == 0:
        print("Accuracy after %i images: %f" %
              (total_seen, float(num_correct) / float(total_seen)))

  return float(num_correct) / float(total_seen)

# predict test data
start = time.time()
predictions = model.predict(test_images,batch_size=1)
eval_model_original(predictions, ds)
time_elapsed = time.time() - start
print("prediction time_elapsed=",time_elapsed)

# save model
saved_models_root = "./saved_models/first"
print("saved_models_root =", saved_models_root)
tf.contrib.saved_model.save_keras_model(model, saved_models_root)

saved_models_dir = str(sorted(pathlib.Path(saved_models_root).glob("*"))[-1])
print("saved_models_dir =", saved_models_dir)


"""Original minimal designs based around nesting similar shapes."""

from itertools import cycle
import os
from random import random, choice

import matplotlib.pyplot as plt
import matplotlib.patches as mpatches

from numpy import pi, sin, cos

# ---------- Design and output parameters ------------
# ... for container shape, outer to all the other shapes:
outer_size = 0.5
outer_centre = (0.5, 0.5)

# ... for the design of inner shapes:
size_ratio_of_next_inner_shape = 0.09  # 0.07 used for stage one
# For the alternative designs as shown in the README gallery, instead set 0.03
number_inner_shapes = 500

# For stage two i.e. 'edge descend', the main designs
padding_factor_for_edge_descend = 0.35

# ... for the (cycling) colours of the shapes:
background_colour = "black"
# For the alternative designs, instead set the above to "white"
# Choose a set of harmonious 'mod' like colours that are bold but not garish
stage_one_colours = [
    "midnightblue",
    "lavender",
    "crimson",
    "dodgerblue",
    "indianred",
]
# For the alternative designs in the README gallery, instead set the above to:
# ["black", "peru", "darkslategrey", "goldenrod", "teal", "wheat"]
stage_one_colour_selector = cycle(stage_one_colours)
stage_two_colours = [
    "darkslategrey",
    "khaki",
    "lightseagreen",
    "azure",
    "indigo",
    "powderblue",
]
stage_two_colour_selector = cycle(stage_two_colours)  # flake8 error skip?

# ... directories to save designs into:
first_level_dir = "img"  # second_level_dirs live separately under this dir
second_level_dirs = [
    "without-random-edge-alignment",
    "with-random-edge-alignment",
]
# For the alternative designs, add "-alt" to the end of both dirs above

# List all designs to create...
number_sides_to_plot_compound = [1, 4, 3, 5]  # 3 <-> 4 for overall symmetry
number_sides_to_plot_as_single = number_sides_to_plot_compound + [7, 12]

# ... with the (empirically determined) mapping to zoom in to achieve a
# close-up on the container shape without any of the background showing.
# Note that the higher the value, the less zoomed in, up to 0.5 which results
# in no zoom-in relative to the standard designs (with 'closeup=False')
zoom_in_factors = {
    1: 0.27,
    3: 0.18,  # note this is also shifted down by ~0.1 later to re-centralise
    4: 0.215,
    5: 0.24,
    7: 0.25,
    12: 0.27,
}

# -----------------------------------------

def change_centre_variant_2(centre, index):
    """Variant of 'change_centre' function to generate '-2' designs.

    Note the 'index' should be added as an input to change_centre in
    this case to reflect the signature here.

    """
    identical_coor_val = centre[0] + 0.0008 * index
    return (identical_coor_val, identical_coor_val)

def change_centre_variant_3(centre):
    """Variant of 'change_centre' function to generate '-3' designs."""
    return (centre[0] * 0.99, centre[1] * 0.96)

def change_centre(centre, sides, new_size, old_size):
    """Used to generate the main, 'edge descend', designs.

    Note 'new_size' and 'old_size' should be added as an input to
    'change_centre' in this case to reflect the signature here.

    """
    change_in_size = new_size - old_size
    move_centre_by = change_in_size * (1 - padding_factor_for_edge_descend)
    if sides == 1:  # For a circle, the *_size refers to the radii
        # Shift next circle in a random direction by the radius change
        shift_angle_rads = 2 * pi * random()
    else:  # is a RegularPolygon where *_size is distance centre-vertex
        # Possible shift directions are discretised with the number of sides:
        possible_shift_rads = [(2 * pi * N) / sides for N in range(sides)]
        shift_angle_rads = choice(possible_shift_rads)

    new_centre = (
        centre[0] + move_centre_by * cos(shift_angle_rads),
        centre[1] + move_centre_by * sin(shift_angle_rads),
    )
    return new_centre

def make_shape(
    centre, size, sides=1, colour_selector=stage_one_colour_selector
):
    """Make and return a patch defining a positioned polygon to plot."""
    # Could take the (number of) sides -> infinity for sides of a regular
    # polygon to approximate a circle, but better to use actual circular
    # matplotlib patch as a special case:
    if sides == 1:
        size *= 0.9  # make slightly smaller so relatively in size w/ polygons
        return mpatches.Circle(centre, size, facecolor=next(colour_selector))
    elif sides == 2:
        raise ValueError(
            "No two-sided regular polygon, choose another 'sides' value!"
        )
    else:  # regular polygon patch of specified number of sides
        if sides == 3:
            # Make size larger else triangle looks a bit small in relation and
            # also shift downwards to fit larger shape in the axes boundaries
            size *= 1.1
            centre_x, centre_y = centre
            centre = centre_x, centre_y - 0.1
        return mpatches.RegularPolygon(
            centre, sides, size, facecolor=next(colour_selector)
        )

def make_design_patches(sides=1, reposition=False):
    """Return the list of positioned patches defining the design."""
    patch_layers = []

    use_centre = outer_centre  # fixed centre if reposition=False

    # zorder managed naturally via plotting largest first, if did in inverse
    # order would need to use zorder to stop larger shapes covering smaller.
    new_size = outer_size
    for index in range(number_inner_shapes):
        if reposition:
            old_size = new_size
        new_size *= 1 - size_ratio_of_next_inner_shape

        # Process new centre for edge descend:
        if reposition:
            use_centre = change_centre(use_centre, sides, new_size, old_size)

        new_patch = make_shape(use_centre, new_size, sides)
        patch_layers.append(new_patch)

    return patch_layers

def create_design(axes, stage_two, sides=1):
    """Create a design by attaching all underlying patches to axes."""
    for p in make_design_patches(sides=sides, reposition=stage_two):
        axes.add_artist(p)

def plot_and_save(
    use_number_of_sides=1, single=True, closeup=False, stage_two=True
):
    """Plot and save a single or compound 'Edge Descend' design."""
    fig = plt.figure(figsize=(5, 5), facecolor=background_colour)

    if single:
        ax = fig.add_subplot(111, aspect="equal")
        create_design(ax, sides=use_number_of_sides, stage_two=stage_two)
    else:
        # Vary subplot_index prefix appropriate to len(use_number_of_sides)
        for i, set_sides in enumerate(use_number_of_sides):
            subplot_index = 2
            ax = fig.add_subplot(subplot_index, subplot_index, i + 1)
            ax.set_facecolor(background_colour)
            ax.set_axis_off()
            create_design(ax, sides=set_sides, stage_two=stage_two)

    plt.axis("off")

    if single:
        name_prefix = f"single_design_with_{use_number_of_sides}_sides"
    else:
        name_prefix = "compound_design"

    # Create dirs to store the output designs if they do not exist already
    os.makedirs(f"{first_level_dir}", exist_ok=True)
    for directory in second_level_dirs:
        os.makedirs(f"{first_level_dir}/{directory}", exist_ok=True)
        os.makedirs(f"{first_level_dir}/{directory}-closeups", exist_ok=True)

    # Now can plot, save and show the final single or compound design
    use_subdir = second_level_dirs[int(stage_two)]
    directory = f"{first_level_dir}/{use_subdir}"

    if closeup and single:
        # Zoom in on single plots if requested
        zoom_in_factor = zoom_in_factors[use_number_of_sides]
        zoom_in_vals = (0.5 - zoom_in_factor, 0.5 + zoom_in_factor)  # min, max
        if use_number_of_sides == 3:
            shifted_down_zoom_in_vals = (
                0.405 - zoom_in_factor,
                0.405 + zoom_in_factor,
            )
            zoom_in_vals = zoom_in_vals + shifted_down_zoom_in_vals
        else:  # x and y axes min and max are the same
            zoom_in_vals += zoom_in_vals
        ax.axis(zoom_in_vals)

        # For the alternative designs, add "_alt" to the end of the filename
        # before the extension to save to the dirs as in the savde repo state:
        plt.savefig(
            f"{directory}-closeups/{name_prefix}_closeup.png",
            format="png",
            dpi=1000,
            bbox_inches="tight",
        )
        plt.show()
    else:
        #  For the alternative designs, add "_alt" (see above comment)
        plt.savefig(
            f"{directory}/{name_prefix}.png",
            format="png",
            dpi=1000,
        )
        plt.show()

# Create and plot the designs from...
# ...without zooming in:
for number_sides in number_sides_to_plot_as_single:
    plot_and_save(use_number_of_sides=number_sides)
plot_and_save(single=False, use_number_of_sides=number_sides_to_plot_compound)
# ... zooming in to create a close-up where the shapes fill the entire canvas:
for number_sides in number_sides_to_plot_as_single:
    plot_and_save(use_number_of_sides=number_sides, closeup=True)

# TODO: fix triangular 'edge descend' case, where some inner triangles get
# positioned slightly outside the previous larger triangle, but should be
# fully contained. This doesn't seem to be related to the overall shifting
# since it seems to happen even when the shifting is manually disabled...


from .AbstractExporter import AbstractExporter
from ...common.Properties import Properties

from com.sun.star.io import IOException

# An exporter which is configured with a filter name, and
# uses the specified filter to export documents.
class FilterExporter(AbstractExporter):

    filterName = ""
    props = Properties()

    def __init__(self, exporter_):
        super(FilterExporter, self).__init__(exporter_)
        self.filterName = self.getArgument("Filter", exporter_)

    # (non-Javadoc)
    # @see com.sun.star.wizards.web.export.Exporter#export(java.lang.Object, java.io.File, com.sun.star.wizards.web.data.CGSettings, com.sun.star.lang.XMultiServiceFactory)
    def export(self, source, target, xmsf, task):
        result = True
        document = None

        try:
            document = self.openDocument(source, xmsf)
            task.advance(True)
            self.storeToURL1(document, target, self.filterName, self.props.getProperties1())
            task.advance(True)
        except IOException as iox:
            iox.printStackTrace(System.err)
            result = False
            raise iox
        finally:
            self.closeDocument(document, xmsf)
            self.calcFileSize(source, target, xmsf)
            task.advance(True);
        return result


from flask import Flask, jsonify, request
from classifier import classify
from twitter_handler import get_recent_tweets

app = Flask(__name__)

def get_current_mood_percent():
    """Gets the current mood of the internet as a percent"""
    tweets = get_recent_tweets()
    positive_tweets = 0
    negative_tweets = 0
    for t in tweets:
        if classify(t) == 'Positive':
            positive_tweets += 1
        else:
            negative_tweets += 1
    return negative_tweets / ( negative_tweets + positive_tweets )

def convert_to_degress(percent):
    """Converts percent to degrees negativity"""
    return int(percent * 180)

@app.route('/degrees/')
def degrees():
    """View for degrees"""
    return str(convert_to_degress(get_current_mood_percent()))

@app.route('/percent/')
def percent():
    """View for percent"""
    return str(int(get_current_mood_percent() * 100))

@app.route('/')
def index():
    """View for index"""
    return jsonify({'status': 'ok'})

if __name__ == '__main__':
    app.run(debug=False, host='0.0.0.0')


# MELHOR O DESAFIO 61, PERGUNTANDO PARA O USUÁRIO SE ELE QUER MOSTRAR MAIS
# ALGUNS TERMOS. O PROGRAMA ENCERRA QUANDO ELE DISSER QUE QUER MOSTRAR 0 TERMOS.

from time import sleep

print('PROGRESSÃO ARITMÉTICA !')
print('-='*25)
primeiro = int(input('Primeiro termo: '))
razao = int(input('Razão: '))
print('-='*25)
print('Calculando...')
sleep(1)
termo = primeiro
cont = 1
total = 0
mais = 10
while mais != 0:
    total = total + mais
    while cont <= total:
        print('{} '.format(termo), end='-> ')
        termo += razao
        cont += 1
    print('PAUSA')
    mais = int(input('Quantos termos você quer mostar a mais ? '))
print('-='*25)
print('Progressão finalizada com {} termos mostrados'.format(total))


import torch
import torch.nn as nn
import torch.nn.functional as F

class AdversarialLoss(nn.Module):
    def __init__(self, loss_type='hinge', target_real_label=1., target_fake_label=0.):
        super(AdversarialLoss, self).__init__()
        self.loss_type = loss_type
        self.register_buffer('real_label', torch.tensor(target_real_label))
        self.register_buffer('fake_label', torch.tensor(target_fake_label))

        if loss_type == 'nsgan':
            self.criterion = nn.BCELoss()
        elif loss_type == 'lsgan':
            self.criterion = nn.MSELoss()
        elif loss_type == 'hinge':
            self.criterion = nn.ReLU()

    def __call__(self, outputs, is_real: bool, is_discriminator: bool):
        if self.loss_type == 'hinge':
            if is_discriminator:
                if is_real:
                    outputs = -outputs
                return self.criterion(1 + outputs).mean()
            else:
                return (-outputs).mean()
        else:
            labels = (self.real_label if is_real else self.fake_label).expand_as(outputs)
            loss = self.criterion(outputs, labels)
            return loss

class AudioVisualAttentionLoss(nn.Module):
    def __init__(self, avnet):
        super(AudioVisualAttentionLoss, self).__init__()
        for param in avnet.parameters():
            param.requires_grad = False
        self.avnet = avnet
        self.mse_loss = nn.MSELoss()

    def forward(self, image_pred, image_gt, audio):
        av_attmap_pred = self.avnet(image_pred, audio)['av_attmap']
        av_attmap_gt = self.avnet(image_gt, audio)['av_attmap']
        return self.mse_loss(av_attmap_pred, av_attmap_gt)

class L2ContrastiveLoss(nn.Module):
    def __init__(self, margin=1.0):
        super(L2ContrastiveLoss, self).__init__()
        self.margin = margin

    def l2_similarity(self, feature1, feature2):
        feature = feature1.expand(feature1.size(0), feature1.size(0), feature1.size(1)).transpose(0, 1)
        sim_scores = torch.norm(feature - feature2, p=2, dim=2)
        return sim_scores

    def forward(self, feature1, feature2):
        sim_scores = self.l2_similarity(feature1, feature2)
        diagonal_dist = sim_scores.diag()
        cost_s = (self.margin - sim_scores).clamp(min=0)

        # clear diagonals
        mask = torch.eye(sim_scores.size(0)) > .5
        I = mask
        if torch.cuda.is_available():
            I = I.cuda()
        cost_s = cost_s.masked_fill_(I, 0)

        loss = (torch.sum(cost_s ** 2) + torch.sum(diagonal_dist ** 2)) / (2 * feature1.size(0))
        return loss


from src.config.classes import (
    Config,
    DatasetCreatorConfig,
    LoggerConfig,
    TrainConfig, ListenConfig, StaticAudioConfig, RecognizerConfig
)

recognizer_config = RecognizerConfig(
    secs_step=0.2,
    secs_window=1.0,
    save_audio_length=2.2,
    threshold=0.05,
    save_to="data/output/static_audio/",

    path_to_model="data/model/model.pt",
    n_ftt=1023,
    size=(224, 224)
)

config = Config(
    dataset_creator_config=DatasetCreatorConfig(
        raw_audio="data/raw_data/thanos_message.wav",
        hotkey_folder="data/raw_data/stones",
        dataset_size=30000,
        output_folder="data/dataset",
        train_val_split_k=0.7,
        shift_coefficient=0.25
    ),

    train_config=TrainConfig(
        batch_size=1,
        device='cpu',
        n_epochs=3,

        train_path='data/dataset/train.csv',
        val_path='data/dataset/val.csv',

        model_save_folder='data/model',
        model_save_name='model.pt',

        learning_rate=1e-05,
        gamma=0.98,

        n_fft=1023,
        size=(224, 224)
    ),

    listen_config=ListenConfig(
        url="http://radio.maslovka-home.ru/thanosshow",
        start_time="3:59",
        retries=100,
        sleep_time=10,
        recognizer=recognizer_config
    ),

    static_audio_config=StaticAudioConfig(
        path_to_audio="data/raw_data/thanos_message.wav",
        # path_to_audio="data/night_stream.wav",
        recognizer=recognizer_config
    ),

    logger_config=LoggerConfig(
        log_file='data/log_file.log',
        encoding='utf-8',
        level='INFO',
        format="[%(asctime)s] %(levelname)s [%(name)s.%(funcName)s:%(lineno)d] %(message)s",
        date_format="%d/%b/%Y %H:%M:%S"
    )
)


from datetime import date

from oslo_log import log as logging

from trove.common import stream_codecs
from trove.common import utils
from trove.guestagent.common import operating_system
from trove.guestagent.module.drivers import module_driver

LOG = logging.getLogger(__name__)

NR_ADD_LICENSE_CMD = ['nrsysmond-config', '--set', 'license_key=%s']
NR_SRV_CONTROL_CMD = ['/etc/init.d/newrelic-sysmond']

class NewRelicLicenseDriver(module_driver.ModuleDriver):
    """Module to set up the license for the NewRelic service."""

    def get_description(self):
        return "New Relic License Module Driver"

    def get_updated(self):
        return date(2016, 4, 12)

    @module_driver.output(
        log_message='Installing New Relic license key',
        success_message='New Relic license key installed',
        fail_message='New Relic license key not installed')
    def apply(self, name, datastore, ds_version, data_file, admin_module):
        license_key = None
        data = operating_system.read_file(
            data_file, codec=stream_codecs.KeyValueCodec())
        for key, value in data.items():
            if 'license_key' == key.lower():
                license_key = value
                break
        if license_key:
            self._add_license_key(license_key)
            self._server_control('start')
        else:
            return False, "'license_key' not found in contents file"

    def _add_license_key(self, license_key):
        try:
            exec_args = {'timeout': 10,
                         'run_as_root': True,
                         'root_helper': 'sudo'}
            cmd = list(NR_ADD_LICENSE_CMD)
            cmd[-1] = cmd[-1] % license_key
            utils.execute_with_timeout(*cmd, **exec_args)
        except Exception:
            LOG.exception("Could not install license key '%s'",
                          license_key)
            raise

    def _server_control(self, command):
        try:
            exec_args = {'timeout': 10,
                         'run_as_root': True,
                         'root_helper': 'sudo'}
            cmd = list(NR_SRV_CONTROL_CMD)
            cmd.append(command)
            utils.execute_with_timeout(*cmd, **exec_args)
        except Exception:
            LOG.exception("Could not %s New Relic server", command)
            raise

    @module_driver.output(
        log_message='Removing New Relic license key',
        success_message='New Relic license key removed',
        fail_message='New Relic license key not removed')
    def remove(self, name, datastore, ds_version, data_file):
        self._add_license_key("bad_key_that_is_exactly_40_characters_xx")
        self._server_control('stop')


import math
import os
import time

import ffmpeg

def concatFrames():
    os.system("ffmpeg -f concat -safe 0 -i list.txt -c:v 500k -c copy output.webm")
    os.system("ffmpeg -i output.webm -i input.webm -c copy -map 0:v:0 -map 1:a:0 -shortest final.webm")

    os.system('del frames\\*')
    os.system('del frame\\*')
    os.system('del list.txt')

def exportFrames():
    os.system("cd D:\\PyCharm Projects\\Webm trolling")
    frames = os.popen("ffprobe -v error -select_streams v:0 -count_frames -show_entries stream=nb_read_frames input.webm").read()
    frameCount = frames.split('\n')[1].split('=')[1]

    probe = ffmpeg.probe('input.webm')
    video_stream = next((stream for stream in probe['streams'] if stream['codec_type'] == 'video'), None)
    width = int(video_stream['width'])
    height = int(video_stream['height'])
    (
        ffmpeg
            .input('input.webm')
            .output('frames\\frames%d.jpg', vframes=frameCount)
            .run()
    )
    amplitude = 0.01
    (
        ffmpeg
            .input('frames\\frames1.jpg')
            .output('frame\\frame1.webm', vcodec='vp8', crf=10)
            .run()
    )
    for x in range(2,int(frameCount)):
        sin = abs(round(math.sin(x * amplitude),2))
        cos = abs(round(math.cos(x * amplitude),2))
        (
            ffmpeg
            .input('frames\\frames{}.jpg'.format(x))
            .filter('scale',sin*width,cos*height)
            .output('frame\\frame{}.webm'.format(x), vcodec='vp8', crf=10)
            .run()
        )
        amplitude += 0.03

    f = open("list.txt",'a')
    for a in range(1,int(frameCount)):
        f.write("file 'frame\\frame{}.webm' \n".format(a))
    f.close()

    concatFrames()

exportFrames()