### Homemade code
from classes_arduino import tryexceptArduino
from classes_colther import homingZabersConcu, moveAxisTo
from classes_text import printme, scale_reponse, binary_response
from classes_speech import say
from rand_scaling import choosing_next_pos
from local_functions import (
    panicButton,
    iti_zaber_dance_in,
    iti_zaber_dance_away,
    closeEnvelope,
)
from failing import (
    getNames,
    recoverData,
    rewriteRecoveredData,
)
from saving_data import (
    apendAll,
    tempSaving,
    buildDict,
    appendDataDict,
    csvToDictGridIndv,
    apendAll,
    create_temp_name,
)
from scaling import Scaling, Anchoring
import globals

from globals import lowest_height, highest_height, scaling_magnitudes

### Ready-made code
import threading
import keyboard

import simpleaudio as sa
import os
import csv
import random
import numpy as np
from datetime import date

def showing(
    positions_list,
    limit,
    path_videos,
    cam,
    arduino_syringe,
    arduino_pantilt,
    arduino_dimmer,
    zabers,
    beep,
    speaker,
    beep_speech_success,
    platform,
    path_data,
    ROIs,
    PanTilts,
    grid,
    first_time,
):
    showing_counter = 0
    showing_stimulation = False
    showing_mags = [0, 1]
    random.shuffle(showing_mags)

    homingZabersConcu(zabers, {"tactile": ["y", "x", "z"]})
    moveAxisTo(zabers, "camera", "y", 482604)

    while not showing_stimulation:
        while showing_counter < 2:
            if keyboard.is_pressed("q"):
                iti_zaber_dance_away(zabers)
                break

            # Prepare position ZABERS
            p = choosing_next_pos(globals.PanTilts, positions_list, limit)
            cROI = ROIs[p]

            # Feedback closure + TONE
            ev = threading.Event()
            beep_trial = threading.Thread(target=beep.play, args=[ev])
            beep_trial.name = "Beep thread"
            beep_trial.daemon = True
            beep_trial.start()

            # STIMULATION
            random_chosen_showing_mag = showing_mags.pop(0)

            if random_chosen_showing_mag == 0:
                stimulus = 3
                delta_target = 0
                time_out = np.random.randint(1, 5, size=1) + np.random.random_sample(1)
                sentence = "That was how 0 feels like"
                globals.grid["colther"] = csvToDictGridIndv(path_data, highest_height)

            elif random_chosen_showing_mag == 1:
                stimulus = 2
                delta_target = scaling_magnitudes[-1]
                time_out = globals.time_out_ex
                sentence = "That was how 100 feels like"
                globals.grid["colther"] = csvToDictGridIndv(path_data, lowest_height)

            iti_zaber_dance_in(zabers, arduino_pantilt, p, PanTilts, grid)

            tryexceptArduino(arduino_syringe, 6)

            file_path = (
                path_videos
                + "/"
                + f"showing_trial{showing_counter+1}_delta{str(round(delta_target*10))}_pos{p}"
            )

            printme(f"Showing trial: {showing_counter}")
            printme(f"Grid Position: {p}")
            printme(f"Delta: {delta_target}")

            cam.targetTempAutoDiffDelta(
                file_path,
                delta_target,
                cROI,
                globals.size_ROI,
                arduino_syringe,
                stimulus,
                time_out,
                ev,
            )
            globals.delta = 0

            sa.stop_all()

            if cam.shutter_open_time < 0.4:
                cam.failed_trial = True

            if not cam.failed_trial:

                say(sentence)

                beep_speech_success.play()
                globals.stimulus = 4

                showing_counter += 1

            if cam.failed_trial:
                showing_mags.append(random_chosen_showing_mag)

            iti_zaber_dance_away(zabers)

            panicButton()

        os.system("clear")

        # Move to ANCHORING?
        if first_time:
            printme("\nARE WE HAPPY WITH SHOWING? (y/n)  ")
            while True:
                if keyboard.is_pressed("y"):
                    showing_stimulation = True
                    break
                elif keyboard.is_pressed("n"):
                    break
        elif keyboard.is_pressed("r"):
            continue
        else:
            showing_stimulation = True
            break

def anchoring(
    scaling,
    anchoring_trials_per_mag,
    subject_n,
    positions_list,
    limit,
    path_data,
    path_videos,
    path_day_bit,
    path_day,
    path_anal,
    path_figs,
    path_audios,
    cam,
    arduino_syringe,
    arduino_pantilt,
    arduino_dimmer,
    zabers,
    beep,
    speaker,
    beep_speech_success,
    platform,
    ROIs,
    PanTilts,
    grid,
    first_time,
):
    ### DATA ANCHORING
    # Data stuff
    data_anchoring = buildDict(
        "subject",
        "trial",
        "delta_stimulation",
        "response",
        "stimulus_time",
        "position",
        "failed",
        "n_block",
    )
    failed_name = "data_failedanchoring"
    names_data_failed = getNames(path_data, f"{failed_name}.*\.csv")
    name_temp_file = create_temp_name(failed_name)

    anch_temp_data_writer, anch_temp_file, anch_temp_file_name = tempSaving(
        path_data, list(data_anchoring.keys()), temp_file_name=name_temp_file
    )
    printme(names_data_failed)

    data_anchoring = recoverData(names_data_failed, path_data, data_anchoring)

    rewriteRecoveredData(data_anchoring, path_data, anch_temp_file_name)

    ### Anchoring
    anchoring_stimulation = False
    homingZabersConcu(zabers, {"tactile": ["y", "x", "z"]})
    moveAxisTo(zabers, "camera", "y", 482604)
    while not anchoring_stimulation:
        anchoring_trial_counter = 0
        anchoring_mags = [0, 0, 1, 1]
        random.shuffle(anchoring_mags)
        while anchoring_trial_counter < anchoring_trials_per_mag:
            if keyboard.is_pressed("q"):
                iti_zaber_dance_away(zabers)
                break

            # Prepare position ZABERS
            p = choosing_next_pos(PanTilts, positions_list, limit)
            cROI = ROIs[p]

            ev = threading.Event()
            beep_trial = threading.Thread(target=beep.play, args=[ev])
            beep_trial.name = "Beep thread"
            beep_trial.daemon = True
            beep_trial.start()

            # STIMULATION
            stimulus = 2

            randomly_chosen_anchoring_mag = anchoring_mags.pop(0)

            if randomly_chosen_anchoring_mag == 0:
                stimulus = 3
                delta_target = 0
                time_out = np.random.randint(1, 5, size=1) + np.random.random_sample(1)
                globals.grid["colther"] = csvToDictGridIndv(path_data, highest_height)

            elif randomly_chosen_anchoring_mag == 1:
                stimulus = 2
                delta_target = scaling_magnitudes[-1]
                time_out = globals.time_out_ex
                globals.grid["colther"] = csvToDictGridIndv(path_data, lowest_height)

            iti_zaber_dance_in(zabers, arduino_pantilt, p, PanTilts, grid)

            tryexceptArduino(arduino_syringe, 6)

            file_path = (
                path_videos
                + "/"
                + f"anchoring_block{scaling.block}_trial{anchoring_trial_counter+1}_delta{str(round(delta_target*10))}_pos{p}"
            )

            printme(f"Anchoring trial: {anchoring_trial_counter}")
            printme(f"Grid Position: {p}")
            printme(f"Delta: {delta_target}")

            cam.targetTempAutoDiffDelta(
                file_path,
                delta_target,
                cROI,
                globals.size_ROI,
                arduino_syringe,
                stimulus,
                time_out,
                ev,
            )
            globals.delta = 0

            sa.stop_all()

            if cam.shutter_open_time < 0.4:
                cam.failed_trial = True

            if not cam.failed_trial:
                sentence = globals.question_anchoring
                # speak(speaker, qs)
                say(globals.question_anchoring)

                beep_speech_success.play()

                anchoring_response, time_response_end = binary_response(
                    sentence, values={"0": "0", "100": "100"}
                )

                beep_speech_success.play()
                globals.stimulus = 4
                iti_zaber_dance_away(zabers)

                anchoring_trial_counter += 1

            elif cam.failed_trial:
                anchoring_response = 3
                time_response_end = 0
                anchoring_mags.append(randomly_chosen_anchoring_mag)

            ### SAVE RESPONSES
            anchTempRowToWrite = [
                subject_n,
                anchoring_trial_counter,
                delta_target,
                int(anchoring_response),
                cam.shutter_open_time,
                p,
                cam.failed_trial,
                scaling.block,
            ]
            data_anchoring = appendDataDict(data_anchoring, anchTempRowToWrite)
            # print(data_anchoring)

            anch_temp_file = open(f"{path_data}/{anch_temp_file_name}.csv", "a")
            anch_temp_data_writer = csv.writer(anch_temp_file)
            anch_temp_data_writer.writerow(anchTempRowToWrite)
            anch_temp_file.close()

            iti_zaber_dance_away(zabers)

            panicButton()

        apendAll(path_data, 1, data_anchoring, file="data_anchoring")

        closeEnvelope(
            zabers, platform, arduino_syringe, arduino_pantilt, arduino_dimmer
        )

        os.system("clear")
        if first_time:
            printme("\nARE WE HAPPY WITH ANCHORING? (y/n)  ")
            while True:
                if keyboard.is_pressed("y"):
                    anchoring_stimulation = True
                    break
                elif keyboard.is_pressed("n"):
                    break
        elif keyboard.is_pressed("r"):
            continue
        else:
            anchoring_stimulation = True
            break


# coding: utf-8

# In[1]:

import pandas as pd
import numpy as np
from pathlib import Path
import matplotlib
from IPython.display import HTML
import requests
from datetime import datetime

#### Import stations CSV

# My first step is to find interesting stations and one of the requirements is to find some that have a good time range coverage so I will evaluate the files downloaded.

# Reading the stations coordinates CSV

# In[2]:

stations = pd.read_csv(
    '../../data/ncdc/ish-history.csv',
    skiprows=1,
    names=['usaf','wban','stname','ctry','fips','state','call','lat' ,'lon' ,'elev'],
    dtype=object)
print (len(stations))
stations[:3]

# Get correct coordinates dividing the lat/lon by 1000 and the elevation by 10. Then generate a unique index and a column that specficies if the id is from a USAF station or from WBAN. Finally drop any station that doesn't have a location to use.

# In[3]:

stations['lat'] = stations.lat.apply(lambda lat: float(lat)/1000)
stations['lon'] = stations.lon.apply(lambda lon: float(lon)/1000)
stations['elev'] = stations.elev.apply(lambda e: float(e)/10.0)

# In[4]:

stations['id']     = stations.apply(lambda row : "{}-{}".format(row.usaf,row.wban),axis=1)
stations.set_index(['id'],inplace=True)
stations.dropna(how='any',subset=['lat','lon'],inplace=True)
stations.head()

# In[5]:

stations.tail()

#### Read CSV of observations

# Instantiate the path where the NCDC data has been downloaded

# In[6]:

p = Path('../../data/ncdc')

# Check if we have already the stations CSV or generate it from the files. To generate the CSV the name file will be inspected but also a full read of the file will be performed in order to count the number of observations on every station.

# In[7]:

gsodCSV = p.joinpath('gsod.csv')
if not gsodCSV.exists():
    ops = p.joinpath('raw').joinpath('gsod').glob('**/*.op')
    agsod = []

    for op in ops:
        try:
            data = op.name.replace('.op','').split('-')
            data.append(sum(1 for line in op.open( encoding='utf8' ))-1)
            agsod.append(data)
        except UnicodeDecodeError:
          print (op.absolute())
    dfGsod = pd.DataFrame(data = agsod, columns=['usaf','wban','year','obs'])
    dfGsod['id'] = dfGsod.apply(lambda row: "{}-{}".format(row.usaf,row.wban) ,axis=1)
    dfGsod = dfGsod.set_index(['id'])
    dfGsod[['year','obs']].to_csv(str(gsodCSV))

# In[8]:

print ('Reading existing stations per year CSV')
dfGsod = pd.read_csv(str(gsodCSV),index_col=['id'])
print ("{:,} station files".format(len(dfGsod)))
print ("{:,} observations".format(dfGsod.obs.sum()))
dfGsod.head()

#### Year statistics per station

# Now study this dataframe, grouping by id and see the total years recorded, max and min

# In[9]:

year_groups = dfGsod[['year']].groupby(level=0)
year_count  = year_groups.count()
year_max    = year_groups.max()
year_min    = year_groups.min()

years = pd.concat([year_count,year_max,year_min],axis=1)
years.columns = ['count','max','min']
years.head()

#### Count observations per station

# In[10]:

obs_groups = dfGsod[['obs']].groupby(level=0)
obs_count = obs_groups.sum()
obs_count.head()

#### Join stations and observations statistics

# Now we can check if the indexes of both data frames are unique and then join them to retreive only the stations with observations

# In[11]:

stations.index.is_unique and years.index.is_unique and obs_count.index.is_unique

# In[12]:

scdf = pd.concat([stations,years,obs_count],axis=1,join='inner')
scdf.head()

# Finally we can study this dataset and filter the appropriate stations for our study on this first iteration

# In[13]:

scdf.to_csv('stations.csv')

#### Get preferred stations

# Next step is done at [CartoDB](http://cartodb.com) an analysis and mapping service. From the exported `stations.csv`, I've created a map of the 2 stations with more years of observations for every [Köppen-Geiger](http://koeppen-geiger.vu-wien.ac.at/shifts.htm) classification. The map is interactive, you can zoom in and click on stations to check it's attributions.

# In[14]:

HTML('<iframe width="100%" height="520" frameborder="0" src="https://team.cartodb.com/u/jsanz/viz/c9d103fe-6ab1-11e5-b45b-0e674067d321/embed_map" allowfullscreen webkitallowfullscreen mozallowfullscreen oallowfullscreen msallowfullscreen></iframe>')

# To get those stations back to this notebook I will use CartoDB SQL API so I will execute this SQL using the CSV format so it can be read directly into a Pandas DataFrame.
#
# ```
# WITH ranked AS (
#   SELECT
#   s.id, r.gridcode,
#   rank() over (partition by r.gridcode order by s.count desc, s.elev asc) pos
#   FROM stations s
#   JOIN climate_regions r ON ST_Intersects(s.the_geom,r.the_geom)
# ), filtered as (
#   SELECT
#   r.id,k.koppen,
#   rank() over (partition by r.id order by k.koppen) pos
#   FROM ranked r
#   JOIN koppen k ON r.gridcode = k.gridcode
#   WHERE pos < 3
# )
# SELECT id,koppen FROM filtered WHERE POS = 1
# ```

# In[15]:

query = 'https://jsanz.cartodb.com/api/v1/sql?format=csv&q=WITH+ranked+AS+(%0A++SELECT+%0A++s.id,+r.gridcode,%0A++rank()+over+(partition+by+r.gridcode+order+by+s.count+desc,+s.elev+asc)+pos%0A++FROM+stations+s+%0A++JOIN+climate_regions+r+ON+ST_Intersects(s.the_geom,r.the_geom)%0A),+filtered+as+(%0A++SELECT+%0A++r.id,k.koppen%0A++,rank()+over+(partition+by+r.id+order+by+k.koppen)+pos%0A++FROM+ranked+r%0A++JOIN+koppen+k+ON+r.gridcode+%3D+k.gridcode%0A++WHERE+pos+%3C+3+%0A)+%0ASELECT+id,koppen%0AFROM+filtered+WHERE+POS+%3D+1'
selections = pd.read_csv(query,index_col=['id'])

# Check if the index of the imported dataframe is unique and then join it with our stations dataset. This join will keep only data on both data frames using the parameter `join='inner'`.

# In[16]:

selections.index.is_unique

# In[17]:

scdfc = pd.concat([scdf,selections],axis=1,join='inner')

#### Getting the observations for the selected stations

# Filter the observations dataframe (11M records) by the selected stations and generate the files to read from the observations folder

# In[18]:

files_to_read = pd.merge(left=dfGsod,right=scdfc,left_index=True,right_index=True,)[['year']]
files_to_read['id'] = files_to_read.index
files_to_read['path'] = files_to_read.apply(lambda row: Path('../../data/ncdc/raw/gsod').joinpath("{0}/{1}-{0}.op".format(row.year,row.id)),axis=1)
print ("{} files to read".format(len(files_to_read)))

# Read the files defined previously and store the results on a new big data frame and CSV adding the *Köppen* classification

# In[19]:

def getId(stn,wban):
    try:
        istn = int(stn)
        iwban = int(wban)
        return "{:0>6}-{:0>5}".format(istn,iwban)
    except ValueError:
        print("{}/{}".format(stn,wban))

def getStationByStnWban(stn,wban):
    try:
        koppen = scdfc.loc[getId(stn,wban)].koppen
    except KeyError:
        koppen = None
    return koppen

# In[ ]:

observationsCSV = p.joinpath('observations.csv')
if not observationsCSV.exists():
    i = 0
    acc = 0
    for index,row in files_to_read.iterrows():
        path = row['path']
        if path.exists():
            dfObsTemp = pd.read_fwf(
                    str(path),
                    colspecs=[(0,7),(7,13),(14,18),(18,22),(25,30),(31,33),(35,41),
                              (42,44),(46,52),(53,55),(57,63),(64,66),(68,73),(74,76),
                              (78,84),(84,86),(88,93),(95,100),(102,108),(108,109),
                              (110,116),(116,117),(118,123),(123,124),(125,130),(132,138)],
                    skiprows=1,
                    names=['stn','wban','year','monthday','temp','temp_count',
                           'dewp','dewp_count','slp','slp_count','stp','stp_count',
                           'visib','visib_count','wsdp','wsdp_count','mxspd',
                           'gust','max','max_flag','min','min_flag','prcp','prc_flag','sndp','frshtt']
                    )
            dfObsTemp['koppen'] = dfObsTemp.apply(lambda row: getStationByStnWban(row.stn,row.wban),axis=1)
            dfObsTemp.to_csv(str(observationsCSV),mode='a')

            i += 1
            acc += len (dfObsTemp)

            if i % 1000 == 0:
                print("{:>8} obs".format(acc))

# In[ ]:

print ('Reading observations CSV')
dfObs = pd.read_csv(str(observationsCSV),index_col=0)
dfObs = dfObs[(dfObs.stn != 'stn')]
print ("{:,} observations".format(len(dfObs)))
dfObs.head()

#### Performing data management operations on the dataset

# Now that we have the raw dataset, we can start doing management operations. First generate a copy dataframe with only the columns we are interested in.

# In[ ]:

dfObs2 = dfObs.copy()[['stn','wban','year','monthday','temp','max','min','frshtt','koppen']]

##### Management

# Generate an index using the id station and the date

# In[ ]:

def getDateTimeFromRow(row):
    try:
        iyear = int(row.year)
        imonth = int("{:0>4}".format(row.monthday)[0:2])
        iday = int("{:0>4}".format(row.monthday)[2:4])
        return  datetime(iyear,imonth,iday)
    except ValueError:
        return np.nan

dfObs2['id']   = dfObs2.apply(lambda row: getId(row.stn,row.wban) ,axis=1)
dfObs2['date'] = dfObs2.apply(lambda row : getDateTimeFromRow(row),axis=1)
dfObs2.set_index(['id','date'],inplace=True)

# The `frshtt` column needs to be padded with zeros to get all the flags in the correct place. Then is possible to get the occurrence of different weather conditions

# In[ ]:

dfObs2['frshtt']  = dfObs2.apply(lambda row: "{:0>6}".format(row.frshtt),axis=1)
dfObs2['fog']     = dfObs2['frshtt'].apply(lambda row: row[0:1]=='1')
dfObs2['rain']    = dfObs2['frshtt'].apply(lambda row: row[1:2]=='1')
dfObs2['snow']    = dfObs2['frshtt'].apply(lambda row: row[2:3]=='1')
dfObs2['hail']    = dfObs2['frshtt'].apply(lambda row: row[3:4]=='1')
dfObs2['thunder'] = dfObs2['frshtt'].apply(lambda row: row[4:5]=='1')
dfObs2['tornado'] = dfObs2['frshtt'].apply(lambda row: row[5:6]=='1')

# Recode the temperatures columns, replacing the NaN values and afterwards as numerics

# In[ ]:

dfObs2['tempC'] = dfObs2['temp'].replace('99.9', np.nan)
dfObs2['maxC']  = dfObs2['max'].replace('99.9', np.nan)
dfObs2['minC']  = dfObs2['min'].replace('99.9', np.nan)

dfObs2['tempC'] = dfObs2['tempC'].convert_objects(convert_numeric=True) / 10
dfObs2['maxC']  = dfObs2['maxC'].convert_objects(convert_numeric=True) / 10
dfObs2['minC']  = dfObs2['minC'].convert_objects(convert_numeric=True) / 10

##### Frequency tables

# Frequency tables for koppen, thunders, tornados that are categorized

# In[ ]:

dfObs.koppen.value_counts(normalize=True)*100

# In[ ]:

dfObs2.tornado.value_counts(normalize=True)*100

# In[ ]:

dfObs2.thunder.value_counts(normalize=True)*100

# Categorize the temperatures by quantiles and then make the frequency table to confirm the categorization

# In[ ]:

dfObs2['temp4']=pd.qcut(dfObs2.tempC, 4, labels=["1=0%tile","2=25%tile","3=50%tile","4=75%tile"])
dfObs2['temp4'].value_counts(normalize=True)*100

# And to get the cuts, we can group by the categorization column and get the max value for every group

# In[ ]:

dfObs2[['tempC']].max()

# In[ ]:

group_by_year= dfObs2[['year','tempC','koppen']].groupby(['year','koppen'])

# In[ ]:

group_by_year.agg([np.max,np.std])


import torch
import torch.nn as nn
import numpy as np
import sys
sys.path.append("../../")

from general.mutils import get_param_val, create_transformer_mask, create_channel_mask
from layers.flows.flow_model import FlowModel
from layers.flows.activation_normalization import ActNormFlow
from layers.flows.permutation_layers import InvertibleConv
from layers.flows.coupling_layer import CouplingLayer
from layers.flows.mixture_cdf_layer import MixtureCDFCoupling
from layers.categorical_encoding.mutils import create_encoding

class FlowSetModeling(FlowModel):

	def __init__(self, model_params, dataset_class):
		super().__init__(layers=None, name="Set Modeling Flow")
		self.model_params = model_params
		self.dataset_class = dataset_class
		self.set_size = self.model_params["set_size"]
		self.vocab_size = self.dataset_class.get_vocab_size(self.set_size)

		self._create_layers()
		self.print_overview()

	def _create_layers(self):

		self.latent_dim = self.model_params["categ_encoding"]["num_dimensions"]
		model_func = lambda c_out : CouplingTransformerNet(c_in=self.latent_dim,
														   c_out=c_out,
														   num_layers=self.model_params["coupling_hidden_layers"],
														   hidden_size=self.model_params["coupling_hidden_size"])
		self.model_params["categ_encoding"]["flow_config"]["model_func"] = model_func
		self.model_params["categ_encoding"]["flow_config"]["block_type"] = "Transformer"
		self.encoding_layer = create_encoding(self.model_params["categ_encoding"],
											  dataset_class=self.dataset_class,
											  vocab_size=self.vocab_size)

		num_flows = self.model_params["coupling_num_flows"]
		if self.latent_dim > 1:
			coupling_mask = CouplingLayer.create_channel_mask(self.latent_dim,
															  ratio=self.model_params["coupling_mask_ratio"])
			coupling_mask_func = lambda flow_index : coupling_mask
		else:
			coupling_mask = CouplingLayer.create_chess_mask()
			coupling_mask_func = lambda flow_index : coupling_mask if flow_index%2==0 else 1-coupling_mask

		layers = []
		for flow_index in range(num_flows):
			layers += [
				ActNormFlow(self.latent_dim),
				InvertibleConv(self.latent_dim),
				MixtureCDFCoupling(c_in=self.latent_dim,
								   mask=coupling_mask_func(flow_index),
								   model_func=model_func,
								   block_type="Transformer",
								   num_mixtures=self.model_params["coupling_num_mixtures"])
			]

		self.flow_layers = nn.ModuleList([self.encoding_layer] + layers)

	def forward(self, z, ldj=None, reverse=False, length=None, **kwargs):
		if length is not None:
			kwargs["src_key_padding_mask"] = create_transformer_mask(length)
			kwargs["channel_padding_mask"] = create_channel_mask(length)
		return super().forward(z, ldj=ldj, reverse=reverse, length=length, **kwargs)

	def get_inner_activations(self, z, length=None, return_names=False, **kwargs):
		if length is not None:
			kwargs["length"] = length
			kwargs["src_key_padding_mask"] = create_transformer_mask(length)
			kwargs["channel_padding_mask"] = create_channel_mask(length)

		out_per_layer = []
		layer_names = []
		for layer_index, layer in enumerate(self.flow_layers):
			z = self._run_layer(layer, z, reverse=False, **kwargs)[0]
			out_per_layer.append(z.detach())
			layer_names.append(layer.__class__.__name__)

		if not return_names:
			return out_per_layer
		else:
			return out_per_layer, layer_names

	def initialize_data_dependent(self, batch_list):
		# Batch list needs to consist of tuples: (z, kwargs)
		print("Initializing data dependent...")
		with torch.no_grad():
			for batch, kwargs in batch_list:
				kwargs["src_key_padding_mask"] = create_transformer_mask(kwargs["length"])
				kwargs["channel_padding_mask"] = create_channel_mask(kwargs["length"])
			for layer_index, layer in enumerate(self.flow_layers):
				batch_list = FlowModel.run_data_init_layer(batch_list, layer)

class CouplingTransformerNet(nn.Module):

	def __init__(self, c_in, c_out, num_layers, hidden_size):
		super().__init__()
		self.input_layer = nn.Sequential(
				nn.Linear(c_in, hidden_size),
				nn.GELU(),
				nn.Linear(hidden_size, hidden_size)
			)
		self.transformer_layers = nn.ModuleList([
				nn.TransformerEncoderLayer(hidden_size,
										   nhead=4,
										   dim_feedforward=2*hidden_size,
										   dropout=0.0,
										   activation='gelu') for _ in range(num_layers)
			])
		self.output_layer = nn.Sequential(
				nn.LayerNorm(hidden_size),
				nn.Linear(hidden_size, hidden_size),
				nn.GELU(),
				nn.Linear(hidden_size, c_out)
			)

	def forward(self, x, src_key_padding_mask, **kwargs):
		x = x.transpose(0, 1) # Transformer layer expects [Sequence length, Batch size, Hidden size]
		x = self.input_layer(x)
		for transformer in self.transformer_layers:
			x = transformer(x, src_key_padding_mask=src_key_padding_mask)
		x = self.output_layer(x)
		x = x.transpose(0, 1)
		return x


import numpy as np
import rospy
import sys
from std_msgs.msg import String
from moveit_python import MoveGroupInterface
from moveit_msgs.msg import MoveItErrorCodes
from movo_action_clients.gripper_action_client import GripperActionClient

if __name__ == "__main__":
    rospy.init_node('follow_me_pose')

    voice_pub = rospy.Publisher("/movo/voice/text", String, queue_size=1, latch=True)
    voice_cmd = String()
    voice_cmd.data = "Do you want to dance with me?"
    # voice_cmd.data = "Long fei, stop wasting time on me. It will not work, unless you say I like movo."
    voice_pub.publish(voice_cmd)

    dof = rospy.get_param('~jaco_dof')

    rmove_group = MoveGroupInterface("right_arm", "base_link")
    lmove_group = MoveGroupInterface("left_arm", "base_link")
    lmove_group.setPlannerId("RRTConnectkConfigDefault")
    rmove_group.setPlannerId("RRTConnectkConfigDefault")
    lgripper = GripperActionClient('left')
    rgripper = GripperActionClient('right')

    if "6dof" == dof:
        right_arm_joints = ["right_shoulder_pan_joint",
                              "right_shoulder_lift_joint",
                              "right_elbow_joint",
                              "right_wrist_1_joint",
                              "right_wrist_2_joint",
                              "right_wrist_3_joint"]
        left_arm_joints = ["left_shoulder_pan_joint",
                             "left_shoulder_lift_joint",
                             "left_elbow_joint",
                             "left_wrist_1_joint",
                             "left_wrist_2_joint",
                             "left_wrist_3_joint"]
        rarm_pick = [np.radians(x) for x in [-75.0, -15.0, -84.0, 0.0, -120.0, 30.0]]
        larm_pick = [np.radians(x) for x in [84.8, 84.8, 160.4, 0.0, 0.0, -90.0]]

    elif "7dof" == dof:
        right_arm_joints = ["right_shoulder_pan_joint",
                                  "right_shoulder_lift_joint",
                                  "right_arm_half_joint",
                                  "right_elbow_joint",
                                  "right_wrist_spherical_1_joint",
                                  "right_wrist_spherical_2_joint",
                                  "right_wrist_3_joint"]
        left_arm_joints = ["left_shoulder_pan_joint",
                                 "left_shoulder_lift_joint",
                                 "left_arm_half_joint",
                                 "left_elbow_joint",
                                 "left_wrist_spherical_1_joint",
                                 "left_wrist_spherical_2_joint",
                                 "left_wrist_3_joint"]
        larm_pick = [2.6, -2.0, 0.0, -2.0, 0.0, 0.0, 1.0]
        rarm_pick = [-2.6, 2.0, 0.0, 2.0, 0.0, 0.0, -1.0]
        rospy.logwarn("follow_me pose for 7dof is not defined yet.")
    else:
        rospy.logerr("DoF needs to be set 6 or 7, aborting demo")
        sys.exit()

    # This is not simulation so need to adjust gripper parameters
    gripper_closed = 0.01
    gripper_open = 0.165

    success = False
    while not rospy.is_shutdown() and not success:
        l_result = lmove_group.moveToJointPosition(left_arm_joints, larm_pick, 0.05, planning_time=120.0)
        r_result = rmove_group.moveToJointPosition(right_arm_joints, rarm_pick, 0.05, planning_time=120.0)
        if (r_result.error_code.val == MoveItErrorCodes.SUCCESS) and (l_result.error_code.val == MoveItErrorCodes.SUCCESS):
            success = True
    lgripper.command(gripper_closed)
    rgripper.command(gripper_closed)

    rospy.spin()


import tensorflow as tf
import os
import numpy as np
import logging
import io
import PIL.Image
import hashlib
import random

from object_detection.utils import dataset_util
import cv2 as cv
from utils.utils import read_config
from object_detection.dataset_tools import tf_record_creation_util
from object_detection.utils import label_map_util
import hashlib

config = read_config()
save_dir = config['TFRecord']['save_dir']
IMAGE_DIR = config['TFRecord']['IMAGE_DIR']
TF_REC_DIR = config['TFRecord']['TF_REC_DIR']
data_dir = config['TFRecord']['data_dir']

flags = tf.app.flags
flags.DEFINE_string('output_dir', TF_REC_DIR, 'Path to output TFRecord')
FLAGS = flags.FLAGS

def create_tf_example(data):
  img_path = os.path.join(IMAGE_DIR, data['filename'])
  with tf.gfile.GFile(img_path, 'rb') as fid:
    encoded_jpg = fid.read()
  encoded_jpg_io = io.BytesIO(encoded_jpg)

  image = PIL.Image.open(encoded_jpg_io)
  if image.format != 'JPEG':
    raise ValueError('Image format not JPEG')

  key = hashlib.sha256(encoded_jpg).hexdigest()

  width = int(data['size']['width'])
  height = int(data['size']['height'])

  xmins = []
  ymins = []
  xmaxs = []
  ymaxs = []
  classes = []
  classes_text = []

  if 'object' in data:
    for obj in data['object']:
      xmins.append(obj[0] / width)
      ymins.append(obj[1] / height)
      xmaxs.append(obj[2] / width)
      ymaxs.append(obj[3] / height)
      class_name = 'PCB'
      classes_text.append(class_name.encode('utf8'))
      classes.append(1)

  feature_dict = {
      'image/height': dataset_util.int64_feature(height),
      'image/width': dataset_util.int64_feature(width),
      'image/filename': dataset_util.bytes_feature(
          data['filename'].encode('utf8')),
      'image/source_id': dataset_util.bytes_feature(
          data['filename'].encode('utf8')),
      'image/key/sha256': dataset_util.bytes_feature(key.encode('utf8')),
      'image/encoded': dataset_util.bytes_feature(encoded_jpg),
      'image/format': dataset_util.bytes_feature('jpeg'.encode('utf8')),
      'image/object/bbox/xmin': dataset_util.float_list_feature(xmins),
      'image/object/bbox/xmax': dataset_util.float_list_feature(xmaxs),
      'image/object/bbox/ymin': dataset_util.float_list_feature(ymins),
      'image/object/bbox/ymax': dataset_util.float_list_feature(ymaxs),
      'image/object/class/text': dataset_util.bytes_list_feature(classes_text),
      'image/object/class/label': dataset_util.int64_list_feature(classes),
  }
  example = tf.train.Example(features=tf.train.Features(feature=feature_dict))
  return example

def main()->None:
    #writer = tf.python_io.TFRecordWriter(FLAGS.output_path)
  if not(os.path.exists(IMAGE_DIR)):
    os.mkdir(IMAGE_DIR)
  if not(os.path.exists(TF_REC_DIR)):
    os.mkdir(TF_REC_DIR)

  # TODO(user): Write code to read in your dataset to examples variable
  count=0
  examples = []
  for file in os.listdir(save_dir):
    example = {}
    file_path = (os.path.join(save_dir, file))
    data = np.load(file_path,allow_pickle=True)
    # print(data.keys())
    image = np.array(data['image'],dtype=np.float32)
    filename = (os.path.join(str(count)+'.jpg'))
    cv.imwrite(os.path.join(IMAGE_DIR,str(count)+'.jpg'),image*255)
    count+=1
    example['filename'] = filename
    example['size'] = {}
    example['size']['width'] = 512
    example['size']['height'] = 512
    example['object'] = data['boxes']
    examples.append(example)
      # tf_example = create_tf_example(example)
      # writer.write(tf_example.SerializeToString())

      # writer.close()
  image_dir = os.path.join(data_dir, 'images')
  annotations_dir = os.path.join(data_dir, 'annotations')
  examples_list = list(range(1,len(examples)))
  random.shuffle(examples_list)

  # Test images are not included in the downloaded data set, so we shall perform
  # our own split.

  num_examples = len(examples_list)
  num_train = int(0.9 * num_examples)
  train_examples = examples_list[:num_train]
  val_examples = examples_list[num_train:]
  logging.info('%d training and %d validation examples.',
               len(train_examples), len(val_examples))

  train_output_path = os.path.join(FLAGS.output_dir, 'pcb_train.record')
  val_output_path = os.path.join(FLAGS.output_dir, 'pcb_val.record')

  writer = tf.python_io.TFRecordWriter(train_output_path)
  for idx in train_examples:
      example = examples[idx]
      tf_example = create_tf_example(example)
      writer.write(tf_example.SerializeToString())

  writer.close()

  writer = tf.python_io.TFRecordWriter(val_output_path)
  for idx in val_examples:
      example = examples[idx]
      tf_example = create_tf_example(example)
      writer.write(tf_example.SerializeToString())

if __name__ == '__main__':
  tf.app.run()


import random

months = ['jan','feb','mar','apr']
print(random.randint(0,3))
word = months[random.randint(0,3)]

def check(user_input):

        if user_input == word:
                print('You were right')
        else:
                print('You were wrong')

user_input = input('Tell me what i picked  jan, mar, apr : ')
check(user_input)


# Input
x = float(input("Vneste x: "))
y = float(input("Vneste y: "))
z = float(input("Vneste z: "))

# Provera da li moze da se konstruise trougao
# Ovo su tri uslova za validnost trougla:
# 1.a + b > c
# 2.a + c > b
# 3.b + c > a
if ((x + y > z) and (x + z > y) and (y + z > x)):
    # Trougao je validan
    if (x == y == z):
        # Trougao je jednakostranicni
        print("trougao je jednakostranicni")
    elif ((x == y) or (x == z) or (y == z)):
        # Trougao je jednakokraki
        print("the bitch is jednakokraki")
    else:
        # Trougao je raznostranicni
        print("trougao je raznostranicni")
else:
    # Trougao belongs to dem streets
    print("the bitch aint valid she belong to the streets5")