import unittest

import numpy as np
from torch_geometric.data import Data, InMemoryDataset, DataLoader

from clique_finding_models.output_transforms import *

GRAPHS = [
    # A house graph.
    Data(
        x=torch.ones([5, 1]),
        edge_index=torch.tensor([
            [0, 0, 1, 1, 1, 2, 2, 3, 1, 2, 2, 3, 4, 3, 4, 4],
            [1, 2, 2, 3, 4, 3, 4, 4, 0, 0, 1, 1, 1, 2, 2, 3]], dtype=torch.long),
        y=torch.tensor([3, 4, 4, 4, 4], dtype=torch.float),
    ),
    # A line graph and a node
    Data(
        x=torch.ones([7, 1]),
        edge_index=torch.tensor([
            [0, 1, 2, 3, 4, 1, 2, 3, 4, 5],
            [1, 2, 3, 4, 5, 0, 1, 2, 3, 4]], dtype=torch.long),
        y=torch.tensor([2, 2, 2, 2, 2, 2, 1], dtype=torch.float),
    ),
]

def clone(data):
    return Data.from_dict({k: v.clone() for k, v in data})

class TestDataset(InMemoryDataset):
    def __init__(self, root, transform=None, pre_transform=None):
        super(TestDataset, self).__init__(root, transform, pre_transform)
        self.data, self.slices = self.collate(GRAPHS)

    @property
    def raw_file_names(self):
        return []

    @property
    def processed_file_names(self):
        return []

    def download(self):
        pass

    def process(self):
        pass

class TestTransformY(object):
    def setUp(self):
        self.graphs = [clone(g) for g in GRAPHS]
        self.transform_y = None

    def test_reverse(self):
        for i in range(len(GRAPHS)):
            g = self.transform_y(self.graphs[i])
            y = self.transform_y.reverse(g, g.y)
            np.testing.assert_allclose(y.numpy(), GRAPHS[i].y.numpy())

    def test_reverse_batch_1(self):
        data = TestDataset(root="", transform=self.transform_y)
        loader = DataLoader(data, batch_size=1, shuffle=False)
        for i, batch in enumerate(loader):
            y = self.transform_y.reverse(batch, batch.y)
            np.testing.assert_allclose(y.numpy(), GRAPHS[i].y.numpy())

    def test_reverse_batch_2(self):
        data = TestDataset(root="", transform=self.transform_y)
        loader = DataLoader(data, batch_size=2, shuffle=False)
        batch = next(iter(loader))
        y = self.transform_y.reverse(batch, batch.y)
        np.testing.assert_allclose(y.numpy(), torch.cat([GRAPHS[0].y, GRAPHS[1].y]) .numpy())

class TestTransformYNone(TestTransformY, unittest.TestCase):
    def setUp(self):
        super(TestTransformYNone, self).setUp()
        self.transform_y = transform_y_none

    def test_transform(self):
        g0 = transform_y_none(self.graphs[0])
        np.testing.assert_allclose(g0.y.numpy(), GRAPHS[0].y.numpy())

        g1 = transform_y_none(self.graphs[1])
        np.testing.assert_allclose(g1.y.numpy(), GRAPHS[1].y.numpy())

class TestTransformYRelativeToMaxClique(TestTransformY, unittest.TestCase):
    def setUp(self):
        super(TestTransformYRelativeToMaxClique, self).setUp()
        self.transform_y = transform_y_relative_to_max_clique

    def test_transform(self):
        g0 = transform_y_relative_to_max_clique(self.graphs[0])
        np.testing.assert_allclose(g0.y.numpy(), [0.75, 1, 1, 1, 1])

        g1 = transform_y_relative_to_max_clique(self.graphs[1])
        np.testing.assert_allclose(g1.y.numpy(), [1, 1, 1, 1, 1, 1, 0.5])

class TestTransformYRelativeToDegree(TestTransformY, unittest.TestCase):
    def setUp(self):
        super(TestTransformYRelativeToDegree, self).setUp()
        self.transform_y = transform_y_relative_to_degree

    def test_transform(self):
        g0 = transform_y_relative_to_degree(self.graphs[0])
        np.testing.assert_allclose(g0.y.numpy(), [1, 4. / 5, 4. / 5, 1, 1])

        g1 = transform_y_relative_to_degree(self.graphs[1])
        np.testing.assert_allclose(g1.y.numpy(), [1, 2. / 3, 2. / 3, 2. / 3, 2. / 3, 1, 1])

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


from datetime import datetime
import fs.s3fs
myfs = fs.s3fs.S3FS('wantsomedashboards','logfiles',os.environ['AWS_WSDS3_KEY'], os.environ['AWS_WSDS3_SECRET'])

# db.define_table('page_visit_count',
# 	Field('page_name'),
# 	Field('counter', 'integer'),
# 	)

db.define_table('page_visit_data',
	Field('page_name'),
	Field('last_visited','datetime'),
	Field('args', 'text'),
	Field('vars', 'text'),
	Field('filename'),
	Field('log_file','upload'),
	)

db.define_table('debug_tbl',
	Field('error_message','text'),
	Field('other_info'),
	Field('row_text','text'),
	Field('cell_text','text'),
	Field('timestamp_text','text'),
	)

db.define_table('das_config',
	Field('das_id'),
	Field('serial_number'),
	Field('das_location'),
	Field('notes'),
	Field('last_modified','datetime'),
	)

## This info will be stored in dynamo!
## The table will have one key, the serial number.
# db.define_table('das_attrs',
# 	Field('serial_number','reference das_config.serial_number'),
# 	Field)

db.define_table('device_config',
	Field('device_id'),
	Field('das_id'),
	# Field('serial_number'),
	Field('measuring'),
	Field('device_location'),

	Field('device_field_groups'),

	Field('notes'),
	Field('last_modified', 'datetime'),
	)
db.device_config.das_id.requires=IS_IN_DB(db,'das_config.das_id')
db.device_config.last_modified.default=datetime.now()
db.device_config.device_field_groups.requires=IS_IN_DB(db,'device_field_groups.field_group_name')

db.define_table('log_files',
	Field('device_id'),
	Field('log_filename'),
	Field('log_file', 'upload'),
	Field('date_added','datetime'),
	)
db.log_files.log_file.uploadfs=myfs
db.log_files.device_id.requires=IS_IN_DB(db,'device_config.device_id')

## If I store the acquisuite info in dynamo, I don't have to worry about the names of the params,
## I just use the param name and the attribute freely,
## If there is another unit, I just use whatever names and data they store
## That means I only have to keep track of what certain common info names are for each type of unit.
## If there is a request to view info, I can get the serial- use that to get the acquisuite info from
## dynamo, use that to get the config for the unit and pull the time series data for that unit.
## I think that is better than marrying the acquisuite names for the fields to the db tables
## I also think it is better than trying to come up with common field names based only on the acquisuite.

## I still need a DAS config to use as data validation. But I could still actually use dynamo.....
## But then letting other people configure units might be tricky. I'll keep that in the postgre db for now

db.define_table('device_field_groups',
	Field('field_group_name'),
	Field('field_group_type'),
	Field('field_group_columns','list:integer'),
	Field('last_modified','datetime'),
	)

db.device_field_groups.field_group_type.requires=IS_IN_SET(('include','exclude'))

##I don't want to limit this to tenants right now, but I can't think of another name!
db.define_table('arbitrary_field_groups',
	Field('field_group_name'),
	Field('das_id'),
	Field('device_id'),
	Field('field_ids','list:integer'),
	Field('multiplier','double'),
	Field('percentage','double'),
	Field('add_or_subtract'),
	Field('last_modified','datetime'),
	)

## limit to percent
db.arbitrary_field_groups.percentage.requires=IS_FLOAT_IN_RANGE(0,1)
db.arbitrary_field_groups.last_modified.default=datetime.now()
db.arbitrary_field_groups.add_or_subtract.requires=IS_IN_SET(('add','subtract'))

db.arbitrary_field_groups.das_id.requires=IS_IN_DB(db,'das_config.das_id')
db.arbitrary_field_groups.device_id.requires=IS_IN_DB(db,'device_config.device_id')

db.define_table('aws_core_columns',
	Field('table_name'),
	Field('core','boolean'),
	# Field('equipment_type'),
	Field('data'),
	Field('defaultContent'),
	Field('title'),
	Field('column_order','integer'),
	Field('width'),
	)

db.define_table('coned_ra_elec',
	Field('account_number'),
	Field('start_date','datetime'),
	Field('end_date','datetime'),
	Field('electric_usage_kwh'),
	Field('reading_type'),
	Field('kvars'),
	Field('electric_demand_kw'),
	Field('retail_access_amount_usd'),
	)


import common
import re

lines = common.get_lines("day7_data.txt")

bags = {}

for line in lines:
    colour = re.match(r".*(?= bags contain)", line)[0]
    bags[colour] = re.findall(r"(\d+?) (.+?) bags?", line)

def has_shiny(colour: str):
    if colour == "shiny gold":
        return True
    else:
        return any(has_shiny(c) for _, c in bags[colour])

shiny_bag_count = -1
for bag in bags:
    if has_shiny(bag):
        shiny_bag_count += 1

print(f"Part 1: {shiny_bag_count}")

def bag_calculator(colour: str):
    return 1 + sum(list(int(number) * bag_calculator(c) for number, c in bags[colour]))

print(f"Part 2: {bag_calculator('shiny gold') - 1}")


import random as rnd
import numpy as np
import math
import matplotlib.pyplot as plt
import Demode as de
import Symbol as sym
import timeit

# import tensorflow as tf
# real = tf.constant(2.25)
# imag = tf.constant(3.25)
# tf.dtypes.complex(real, imag)

QAM = 4
Count_Total = 1
symbol_num = 1000
SNR = 15
symbol = []
SER = np.zeros(SNR)

# Kmeans에 필요한 데이터
init_center = [complex(1, 1), complex(-1, 1), complex(-1, -1), complex(1, -1)]
dist = [[0]*symbol_num for i in range(4)] # 거리를 담을 4xn 배열
temp_Y = [[0]*symbol_num for i in range(4)] # 임시 심볼 값을 담을 4xn 배열

# Matrix = [[0]*5 for i in range(7)]  열 행
# 인덱스로 접근해서 값 변경

for count in range(Count_Total):

    symbol = sym.Gensymbol(QAM, symbol_num)  # 초기 심볼
    data_s = de.Demode(QAM, symbol)  # 초기 심볼 위치

    for snr in range(SNR):

        symbol_y = sym.Addnoise(snr, symbol)  # 초기 심볼 + 노이즈
        # 초기화 부분
        position = np.zeros(symbol_num, int)  # 심볼의 사분면 위치를 담을 배열
        rsc_TakeCenter = [[0] * 4 for i in range(3)]

        start = timeit.default_timer()
        # Dist 부분
        for i in range(symbol_num):

            # dist 거리 재기
            for j in range(4):
                part_Real = math.pow(symbol_y[i].real - init_center[j].real, 2)
                part_Imag = math.pow(symbol_y[i].imag - init_center[j].imag, 2)
                dist[j][i] = math.sqrt(part_Real + part_Imag)
        stop = timeit.default_timer()

        for i in range(symbol_num):
            # mindist 부분 - 사분면을 구함
            temp = 0
            for j in range(4):
                if j == 0:
                    temp = dist[j][i]
                elif temp > dist[j][i]:
                    temp = dist[j][i]
            for k in range(4):
                if dist[k][i] == temp:
                    position[i] = k
            # center점 수정
            temp_Y[position[i]][i] = symbol_y[i]  # 임시 temp_Y를 만들어주고

            for q in range(4):
                if position[i] == q:
                    rsc_TakeCenter[0][q] += symbol_y[i].real
                    rsc_TakeCenter[1][q] += symbol_y[i].imag
                    rsc_TakeCenter[2][q] += 1

        # 새로운 초기점 생성
        for v in range(4):
            if rsc_TakeCenter[2][v] != 0:
                init_center[v] = complex(rsc_TakeCenter[0][v] / rsc_TakeCenter[2][v],
                                         rsc_TakeCenter[1][v] / rsc_TakeCenter[2][v])

        data_y = de.Demode(QAM, symbol_y)

        SER[snr] += de.Equals(data_s, data_y)

for i in range(SNR):
    SER[i] = SER[i] / (symbol_num * Count_Total)
    print(i)
    print(SER[i])

# 초기점 찍는 기능
# for test in range(QAM):
#     plt.scatter(init_center[test].real, init_center[test].imag)
# 심볼점 찍는 기능
# for i in range(symbol_num):
#     plt.scatter(symbol[i].real, symbol[i].imag)
plt.xlim(-1.5, 1.5)
plt.ylim(-1.5, 1.5)
print(stop - start)

# SER Semilogy
# x = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]
# plt.semilogy(x, SER)
# plt.xlim([0, SNR])
# plt.ylim(pow(10,-5), pow(10,0))

plt.grid()
plt.show()

# 랜덤값
# 0부터 1까지 랜덤값을 반환
# print(rnd.randrange(0,2))

# 정규 분포
# 평균이 0 표준편차가 1인 정규분포 pure.shape만큼 생성
# pure = np.linspace(-1, 1, 100)
# noise = np.random.normal(0,1,pure.shape)
# 정규 분포 평균이 0 표준편차가 1 랜덤 발생
# print(random.gauss(0.0, 1.0))


import copy
import json

import pytest
from spatialdata.transformations.ngff.ngff_coordinate_system import (
    NgffAxis,
    NgffCoordinateSystem,
)

input_dict = {
    "name": "volume_micrometers",
    "axes": [
        {"name": "x", "type": "space", "unit": "micrometer"},
        {"name": "y", "type": "space", "unit": "micrometer"},
        {"name": "z", "type": "space", "unit": "micrometer"},
    ],
}

def test_coordinate_system_instantiation_and_properties():
    coord_sys = NgffCoordinateSystem.from_dict(input_dict)
    assert coord_sys.name == "volume_micrometers"
    assert [ax.name for ax in coord_sys._axes] == ["x", "y", "z"]
    assert coord_sys.axes_names == ("x", "y", "z")

    assert [ax.type for ax in coord_sys._axes] == ["space", "space", "space"]
    assert coord_sys.axes_types == ("space", "space", "space")

    output_dict = coord_sys.to_dict()
    assert input_dict == output_dict

    axes = [
        NgffAxis(name="x", type="space", unit="micrometer"),
        NgffAxis(name="y", type="space", unit="micrometer"),
        NgffAxis(name="z", type="space", unit="micrometer"),
    ]
    coord_manual = NgffCoordinateSystem(
        name="volume_micrometers",
        axes=axes,
    )

    assert coord_manual.to_dict() == coord_sys.to_dict()
    with pytest.raises(ValueError):
        NgffCoordinateSystem(
            name="non unique axes names",
            axes=[
                NgffAxis(name="x", type="space", unit="micrometer"),
                NgffAxis(name="x", type="space", unit="micrometer"),
            ],
        )

def test_coordinate_system_exceptions():
    input_dict1 = copy.deepcopy(input_dict)
    input_dict1["axes"][0].pop("name")
    coord_sys = NgffCoordinateSystem(name="test")
    with pytest.raises(ValueError):
        coord_sys.from_dict(input_dict1)

    input_dict2 = copy.deepcopy(input_dict)
    input_dict2["axes"][0].pop("type")
    coord_sys = NgffCoordinateSystem(name="test")
    with pytest.raises(ValueError):
        coord_sys.from_dict(input_dict2)

    # not testing the cases with axis.type in ['channel', 'array'] as all the more complex checks are handled by the
    # validation schema

def test_coordinate_system_roundtrip():
    input_json = json.dumps(input_dict)
    cs = NgffCoordinateSystem.from_json(input_json)
    output_json = cs.to_json()
    assert input_json == output_json
    cs2 = NgffCoordinateSystem.from_json(output_json)
    assert cs == cs2

def test_repr():
    cs = NgffCoordinateSystem(
        "some coordinate system",
        [
            NgffAxis("X", "space", "micrometers"),
            NgffAxis("Y", "space", "meters"),
            NgffAxis("T", "time"),
        ],
    )
    expected = (
        "NgffCoordinateSystem('some coordinate system',"
        + " [NgffAxis('X', 'space', 'micrometers'),"
        + " NgffAxis('Y', 'space', 'meters'), NgffAxis('T', 'time')])"
    )
    as_str = repr(cs)

    assert as_str == expected

def test_equal_up_to_the_units():
    cs1 = NgffCoordinateSystem(
        "some coordinate system",
        [
            NgffAxis("X", "space", "micrometers"),
            NgffAxis("Y", "space", "meters"),
            NgffAxis("T", "time"),
        ],
    )
    cs2 = NgffCoordinateSystem(
        "some coordinate systema",
        [
            NgffAxis("X", "space", "micrometers"),
            NgffAxis("Y", "space", "meters"),
            NgffAxis("T", "time"),
        ],
    )
    cs3 = NgffCoordinateSystem(
        "some coordinate system",
        [
            NgffAxis("X", "space", "gigameters"),
            NgffAxis("Y", "space", ""),
            NgffAxis("T", "time"),
        ],
    )

    assert cs1.equal_up_to_the_units(cs1)
    assert not cs1.equal_up_to_the_units(cs2)
    assert cs1.equal_up_to_the_units(cs3)

def test_subset_coordinate_system():
    cs = NgffCoordinateSystem(
        "some coordinate system",
        [
            NgffAxis("X", "space", "micrometers"),
            NgffAxis("Y", "space", "meters"),
            NgffAxis("Z", "space", "meters"),
            NgffAxis("T", "time"),
        ],
    )
    cs0 = cs.subset(["X", "Z"])
    cs1 = cs.subset(["X", "Y"], new_name="XY")
    assert cs0 == NgffCoordinateSystem(
        "some coordinate system_subset ['X', 'Z']",
        [
            NgffAxis("X", "space", "micrometers"),
            NgffAxis("Z", "space", "meters"),
        ],
    )
    assert cs1 == NgffCoordinateSystem(
        "XY",
        [
            NgffAxis("X", "space", "micrometers"),
            NgffAxis("Y", "space", "meters"),
        ],
    )

def test_merge_coordinate_systems():
    cs0 = NgffCoordinateSystem(
        "cs0",
        [
            NgffAxis("X", "space", "micrometers"),
            NgffAxis("Y", "space", "meters"),
        ],
    )
    cs1 = NgffCoordinateSystem(
        "cs1",
        [
            NgffAxis("X", "space", "micrometers"),
        ],
    )
    cs2 = NgffCoordinateSystem(
        "cs2",
        [
            NgffAxis("X", "space", "meters"),
            NgffAxis("Y", "space", "meters"),
        ],
    )
    cs3 = NgffCoordinateSystem(
        "cs3",
        [
            NgffAxis("Z", "space", "micrometers"),
        ],
    )
    assert cs0.merge(cs0, cs1) == NgffCoordinateSystem(
        "cs0_merged_cs1",
        [
            NgffAxis("X", "space", "micrometers"),
            NgffAxis("Y", "space", "meters"),
        ],
    )
    with pytest.raises(ValueError):
        NgffCoordinateSystem.merge(cs0, cs2)
    assert NgffCoordinateSystem.merge(cs0, cs3) == NgffCoordinateSystem(
        "cs0_merged_cs3",
        [
            NgffAxis("X", "space", "micrometers"),
            NgffAxis("Y", "space", "meters"),
            NgffAxis("Z", "space", "micrometers"),
        ],
    )


import yaml
import os
import numpy as np
import ruamel.yaml

def load_conf(path: str) -> yaml:
    """
    The goal of this function is to load the
    params

    Arguments:
        -path: str The path of configuration file to be loaded

    Returns:
        -file: yaml file The configuration file loaded
    """
    with open(path, "r") as ymlfile:
        file = yaml.safe_load(ymlfile)
    return file

model_params=load_conf("configs/path_params.yml")

def load_default_params(
    dict_params: dict(), path_default_params="configs/default_params.yml"
) -> dict():
    """
    The goal of this function is, for all parameters of the
    KMeans not entered by the user, to enter instead default
    params stored in a yml file

    Arguments:
        -dict_params: dict(): The dictionnary containing the
        parameters entered by the user
        -path_default_params: The path of the yml file where the
        default parameters are stored

    Returns:
        -dict_params: dict(): The dictionnary of parameters after it
        was updated with default parameters
    """

    default_params = load_conf(path_default_params)
    for key in default_params.keys():
        if key not in dict_params.keys():
            dict_params[key] = default_params[key]

    return dict_params

def updating_parameter(dict_conf, points) -> None:
    """
    The goal of this function is, if the user decides to cluster its own
    data, to modify the yml file of the model so that the pipeline can run
    without problem and appropriate parameters can be applicated

    Arguments:
        -dict_conf: dict(str): The current dictionnary with the parameters
        of the yml file
        -points: np.array(float): The points entered by the user to be clustered

    Returns:
        None
    """
    data = np.load(os.path.join(os.getcwd(), model_params["to_cluster_data_path"]))
    yaml = ruamel.yaml.YAML()
    with open(os.path.join(os.getcwd(), model_params["path_config_file"])) as fp:
        data = yaml.load(fp)
    data["number_dimension"] = points.shape[1]
    with open(model_params["path_config_file"], "w") as file:
        documents = yaml.dump(data, file)
    dict_conf["number_of_individuals"] = points.shape[0]
    dict_conf["number_dimension"] = points.shape[1]
    dict_conf["limit_min"] = points.min()
    dict_conf["limit_max"] = points.max()


from flask import Flask
import socket

app = Flask(__name__)

@app.route("/")
def hello():
    hostname = socket.gethostname()
    return f"Hello from {hostname}"

if __name__ == "__main__":
    app.run(host='0.0.0.0', port=8080)


import sys
import logging
FORMAT = ('%(asctime)-15s %(threadName)-15s'
          ' %(levelname)-8s %(module)-15s:%(lineno)-8s %(message)s')
logging.basicConfig(format=FORMAT)
log = logging.getLogger()
log.setLevel(logging.DEBUG)
#log.setLevel(logging.WARNING)
#log.setLevel(logging.INFO)
from modbus_sniffer import SerialSnooper
if __name__ == "__main__":
    baud = 9600
    try:
        port = sys.argv[1]
    except IndexError:
        print("Usage: python3 {} device [baudrate, default={}]".format(sys.argv[0], baud))
        sys.exit(-1)
    try:
        baud = int(sys.argv[2])
    except (IndexError,ValueError):
        pass
    with SerialSnooper(port, baud) as ss:
        while True:
            data = ss.read_raw()
            if len(data):
                print(data)
            response = ss.process(data)
            #sleep(float(1)/ss.baud)
    sys.exit(0)


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

from sklearn.metrics import accuracy_score
from sklearn.linear_model import LinearRegression
from sklearn.linear_model import LogisticRegression
from sklearn.ensemble import RandomForestClassifier
from sklearn.neighbors import KNeighborsClassifier
from sklearn.svm import SVC

from sklearn import datasets
iris=datasets.load_iris()

iris_data=iris.data
iris_data=pd.DataFrame(iris_data , columns=iris.feature_names)
iris_data['class']=iris.target
iris_data.head

#understanding data
iris.target_names
print(iris_data.shape)
p=iris_data.describe()

#train test split
from sklearn.model_selection import train_test_split
X=iris_data.values[:,0:4]
Y=iris_data.values[:,4]
x_train,x_test,y_train,y_test=train_test_split(X,Y,test_size=0.3,random_state=0)

#applying Different Models
#logistic Regression
model=LogisticRegression()
model.fit(x_train,y_train)
predictionsL=model.predict(x_test)
print(accuracy_score(y_test,predictionsL))

#Random Forest
modelR=RandomForestClassifier(n_estimators=500 , random_state=0)
modelR.fit(x_train,y_train)
predictionsR=modelR.predict(x_test)
print(accuracy_score(y_test,predictionsR))

#KNN
modelK=KNeighborsClassifier()
modelK.fit(x_train,y_train)
predictionsK=modelK.predict(x_test)
print(accuracy_score(y_test,predictionsK))

#SVM
modelS=SVC()
modelS.fit(x_train,y_train)
predictionsS=modelS.predict(x_test)
print(accuracy_score(y_test,predictionsS))


# -*- coding: utf-8 -*-

from django.conf import settings
from django.contrib.auth import get_user_model
from django.core.management.base import BaseCommand, CommandError

from guardian.utils import get_anonymous_user

User = get_user_model()

class Command(BaseCommand):
    help = "Set the user profile settings of every user to the defaults"

    def add_arguments(self, parser):
        parser.add_argument('--update-anon-user', dest='update-anon-user',
            default=False, action='store_true',
            help='Update also the profile of the anonymous user')

    def handle(self, *args, **options):
        update_anon_user = options['update-anon-user']
        anon_user = get_anonymous_user()
        for u in User.objects.all():
            # Ignore the anonymous user by default
            if u == anon_user and not update_anon_user:
                continue
            up = u.userprofile
            # Expect user profiles to be there and add all default settings
            up.independent_ontology_workspace_is_default = \
                settings.PROFILE_INDEPENDENT_ONTOLOGY_WORKSPACE_IS_DEFAULT
            up.show_text_label_tool = settings.PROFILE_SHOW_TEXT_LABEL_TOOL
            up.show_tagging_tool = settings.PROFILE_SHOW_TAGGING_TOOL
            up.show_cropping_tool = settings.PROFILE_SHOW_CROPPING_TOOL
            up.show_tracing_tool = settings.PROFILE_SHOW_TRACING_TOOL
            up.show_ontology_tool = settings.PROFILE_SHOW_ONTOLOGY_TOOL
            # Save the changes
            up.save()


from datetime import timedelta

GENESIS_REWARD = 1000
""" The reward that is available for the first `REWARD_HALF_LIFE` blocks, starting with the genesis block. """

REWARD_HALF_LIFE = 10000
""" The number of blocks until the block reward is halved. """

BLOCK_REQUEST_RETRY_INTERVAL = timedelta(seconds=30)
""" The approximate interval after which a block request will be retried. """
BLOCK_REQUEST_RETRY_COUNT = 3
""" The number of failed requests of a block until we give up and delete the depending partial chains. """

GENESIS_TARGET = (1 << 256) - 1
"""
The target of the genesis block.

It is directly used for comparison with the hash and thus we start with the smallest possible target which is the maximal possible hash value.
Thus, everything bellow is a valid proof of work
"""

DIFFICULTY_BLOCK_INTERVAL = 10
""" The number of blocks between target changes. """

DIFFICULTY_TIMEDELTA = timedelta(seconds=6)
""" The time span that it should approximately take to mine `DIFFICULTY_BLOCK_INTERVAL` blocks.  """


import gradio as gr

from transformers import pipeline

fill_mask = pipeline("fill-mask", model="./QuijoBERT", tokenizer = './QuijoBERT')

def predict(text):

    res_dict = {}
    x = fill_mask(text)
    print('x')
    for i in range(len(x)):
        k = x[i]['sequence']
        e = x[i]['score']
        print(k, e)
        if e >= 0.05:
            res_dict[k] = e
    print (res_dict)
    return res_dict
    #return {x[0]["sequence"], x[0]["score"]}

# texto = 'en un lugar de la <mask>'
# print(predict(texto))

iface = gr.Interface(
    fn=predict,
    inputs='text',
    outputs ='label',
    examples=['En un lugar de la <mask>', 'En verdad, <mask> Sancho', 'Cómo has estado, bien mío, <mask> de mis ojos, compañero mío']
)

iface.launch()


import json
import pickle
import numpy as np

# Variable:
df_columns = None
locations = None
model = None

# Price Predict funtion:
def Price_Predict(locationOut, SqFt, Bath, Bed):
    locationsIn = locationOut.lower()
    location_index = df_columns.index(locationsIn)
    SqFt = float(SqFt)
    Bath = int(Bath)
    Bed = int(Bed)
    print('\n\n [2]Price predict called from utilities')
    print(SqFt)
    print(Bath)
    print(Bed)
    print(location_index)
    size = len(df_columns)
    x = np.zeros(size)
    x[0] = Bath
    x[1] = Bed
    x[2] = SqFt
    x[location_index] = 1

    price = model.predict([x])[0]  # 2d array converted to 1D
    print('Price is: ')
    price = price.round(2)
    price = float(price)
    print(price)
    return price

# Dummy function: START ---
def ask(Belt, Bad):
    Belt = int(Belt)
    Bad = int(Bad)
    print(Belt)
    print(Bad)
    print(" <- Bed number.")
    total = Belt + Bad
    return total
# Dummy END ---

''' Dummy function: START --- [Works for Sure]
def ask(Belt, Bad):
    Belt = int(Belt)
    Bad = int(Bad)
    print(Belt)
    print(Bad)
    print(" <- Bed number.")
    total = Belt + Bad
    return total
# Dummy END --- '''

def location_names():
    return locations

def load__model_and_column_names():
    global locations
    global df_columns
    global model
    # Get column names
    with open('Column_Names.json', 'r') as L:
        df_columns = json.load(L)['Columns']  # ['Columns'] is the key name in the Column_Names.json file
        locations = df_columns[3:]

    # Get model
    with open('Price_prediction_banglore_homes_in_USD', 'rb') as L:
        model = pickle.load(L)
        print('Model loaded')

if __name__ == 'utilities':
    load__model_and_column_names()
    print('\n\n[1] Location names are \n\n:', location_names())
    print('\n\n[2] The price is: ', Price_Predict('1st Phase JP Nagar', 1000, 2, 2))


import asyncio
import uvloop

import aiowebsockets

async def connect_client():
    async with aiowebsockets.Connect('ws://localhost:2053') as context:
        context.send(b'{"The":"Quick","Brown":"Fox","Jumped":"Over","The":"Lazy","Dog":"."}')

        async for message in context:
            context.send(message)

if __name__ == '__main__':
    asyncio.set_event_loop_policy(uvloop.EventLoopPolicy())
    tasks = [connect_client() for i in range(1024)]

    asyncio.get_event_loop().run_until_complete(
        asyncio.wait(tasks))


import torch.nn as nn
import torchaudio
import torch.autograd.variable
import numpy as np
from torch.autograd.variable import *

from utils.stft_istft import STFT

class NET_Wrapper(nn.Module):
    def __init__(self,win_len,win_offset):
        super(NET_Wrapper, self).__init__()

        self.win_len = win_len
        self.win_offset = win_offset
        self.STFT = STFT(self.win_len, self.win_offset).cuda()
        self.d_model = 256
        self.d_f = 64
        self.k = 3
        self.max_d_rate = 16
        self.lstm_input_size = 64
        self.lstm_layers = 2
        self.layer_list = []
        self.dila_list = nn.ModuleList([])
        for i in range(5):
            self.dila_list.append(nn.Conv1d(in_channels=self.d_f, out_channels=self.d_f, kernel_size=self.k
                       , padding=int(2 ** i), dilation=int(2 ** i)))
        self.conv1_outp = nn.Conv1d(in_channels=self.d_model, out_channels=64, kernel_size=1)
        self.conv1_inp = nn.Conv1d(in_channels=161, out_channels=self.d_model, kernel_size=1)
        self.conv1_dm = nn.Conv1d(in_channels=self.d_model, out_channels=self.d_f, kernel_size=1)
        self.conv1_df = nn.Conv1d(in_channels=self.d_f, out_channels=self.d_model, kernel_size=1)

        self.BN_dm = nn.BatchNorm1d(self.d_model)
        self.BN_df = nn.BatchNorm1d(self.d_f)

        self.sigmoid = nn.Sigmoid()
        self.relu = nn.ReLU()

        self.lstm = nn.LSTM(input_size=self.lstm_input_size,
                            hidden_size=self.lstm_input_size,
                            num_layers=self.lstm_layers,
                            batch_first=True,
                            bidirectional=True)

        # self.Aver_pooling = nn.AvgPool2d((1, 128))
        self.linear_layer = nn.Sequential(nn.Linear(128, 4),
                                          nn.Dropout(0.5),
                                          nn.LeakyReLU())
        self.softmax = nn.Softmax(dim=1)

        self.Spec = torchaudio.transforms.Spectrogram(n_fft=self.win_len, power=None)

    def forward(self, input_data_c1):
        spec_feature = self.Spec(input_data_c1)
        input_feature = torch.cat([spec_feature[:, :, :, 0], spec_feature[:, :, :, 0]], dim=-1)

        conv = self.conv1_inp(input_feature)
        conv = self.BN_dm(conv)
        # conv = nn.LayerNorm(normalized_shape=conv.shape[-1], eps=1e-6)(conv)
        conv = self.relu(conv)
        self.layer_list.append(conv)

        # feedforward = self.relu(self.BN_dm(self.conv1_inp(input_feature)))
        for i in range(20):
            #self.layer_list.append(self.block(self.layer_list[-1], int(2**(i%(np.log2(self.max_d_rate)+1)))))
            residual = self.layer_list[-1]
            # conv1 = self.conv1_dm(self.relu(self.BN_dm(self.layer_list[-1])))
            conv1 = self.BN_dm(self.layer_list[-1])
            conv1 = self.relu(conv1)
            conv1 = self.conv1_dm(conv1)

            conv2 = self.BN_df(conv1)
            conv2 = self.relu(conv2)
            conv2 = self.dila_list[i % 5](conv2)

            # conv3 = self.conv1_df(self.relu(self.BN_df(conv2)))
            conv3 = self.BN_df(conv2)
            conv3 = self.relu(conv3)
            conv3 = self.conv1_df(conv3)

            self.layer_list.append(residual + conv3)

        ResNep_outp = self.conv1_outp(self.layer_list[-1])
        self.layer_list.clear()
        ResNep_outp = ResNep_outp.permute(0,2,1)

        lstm_out, _ = self.lstm(ResNep_outp)   # B:16 T  F`:128
        outp = self.linear_layer(lstm_out)

        outp = torch.mean(outp, dim=1)
        outp = outp.squeeze()
        outp = self.softmax(outp)

        return outp


from pyspark import SparkContext,SparkConf,SparkFiles
from pyspark.sql import SparkSession
from datetime import datetime
UTC_FORMAT = "%Y-%m-%dT%H:%M:%S.%fZ"
from fmm import Network,NetworkGraph,STMATCH,STMATCHConfig
#network = Network("../data/edges.shp")
import os
import utm
os.environ["PYSPARK_PYTHON"]='/root/anaconda3/envs/py37/bin/python'
os.environ['GDAL_DATA']='/root/anaconda3/envs/py37/share/gdal/gata-data'

def sort_split(x):
    x = sorted(x, key=lambda y: y[5])
    res = []
    tmp = [x[0]]
    for item in x[1:]:
        if (datetime.strptime(item[5], UTC_FORMAT) - datetime.strptime(tmp[-1][5], UTC_FORMAT)).seconds < 180:
            tmp.append(item)
        else:
            if len(tmp) >= 3:
                res.append(tmp)
            tmp = [item]
    if len(tmp) >= 3:
        res.append(tmp)
    #    print("sort_split ok!")
    return res

def match_coor(x):
    res = []
    # with Network(SparkFiles.get("my_connected_path4.shp")) as network:
    # graph = NetworkGraph(netwk.value)
    # model = STMATCH(netwk.value, graph)
    network = Network("./data/my_connected_path4.shp")
    graph = NetworkGraph(network)
    model = STMATCH(network, graph)
    config = STMATCHConfig()
    config.k = 4
    config.gps_error = 0.5
    # config.radius = 0.4
    config.radius = 100
    config.vmax = 60
    config.factor = 1.5

    for traj in x:
        tmp = []
        for item in traj[1]:
            y,x = float(item[4]), float(item[3])
            if x>180 or x<-180 or y>80 or y<-80:
                continue
            trans_coor = utm.from_latlon(y,x, force_zone_number=49)
            tmp.append(str(trans_coor[0])+" "+str(trans_coor[1]))
        if len(tmp) == 0:
            continue
        wkt = "LINESTRING("+",".join(tmp)+")"
        result = model.match_wkt(wkt,config)
        opath = list(result.opath)
        x2 = list(zip(traj[1],opath))
        res.append(x2)

    return iter(res)
def v_num(x):
    total_v = 0.0
    cnt = 0
    for i in x:
        total_v += float(i)
        cnt += 1
    return total_v / cnt, cnt

if __name__=="__main__":

    # network = Network("./data/my_connected_path4.shp")

    # result = model.match_wkt(wkt, config)
    cof = SparkConf().setMaster("local[*]").setExecutorEnv("/root/anaconda3/envs/py37/bin/python")
    # cof =SparkConf().setMaster("local[*]").setAppName("myfmmapp")
    sc = SparkContext(conf=cof)
    # path = os.path.join("./data","my_connected_path4.shp")
    # sc.addFile(path)
    # sc = SparkContext("spark://172.16.101.5:9090", "yarn")
    # sc = SparkSession.builder.master("spark://172.16.101.5:9090").getOrCreate()
    # mod = sc.broadcast(model)
    # netwk = sc.broadcast(network)
    # con = sc.broadcast(config)
    # rdd = sc.textFile("hdfs://compute-5-2:8020/user/zhaojuanjuan/hxj/testfiles/*.gz")
    # rdd = sc.textFile("./data/2017-05-08.gz/*.gz")
    rdd = sc.textFile("./data/2017-07/2017-07-*/part*.gz").filter(lambda x:x!=None)
    # print(rdd.getNumPartitions())
    rdd2 = rdd.map(lambda x:[i for i in x.split(",")]).filter(lambda x:x[-2]!='0').map(lambda x:(x[0],x[:])).groupByKey()
    # print(rdd.count())
    rdd3 = rdd2.mapValues(sort_split).flatMapValues(lambda x:x).mapPartitions(match_coor).flatMap(lambda x:x)
    # print(rdd3.first())
    # (['粤B011YU', '红的', '深圳市国贸汽车实业有限公司', '114.076897', '22.540199', '2017-05-08T08:48:18.000Z', '1453571', '23', '270',
    #   '0', '', '', '1', '蓝色'], 9782)
    # sc.stop()

    rdd4 = rdd3.map(lambda x:((x[1],int(datetime.strptime(x[0][5], UTC_FORMAT).timestamp()/300)),x[0][7]))
    rdd5 = rdd4.groupByKey().mapValues(v_num)
    rdd5.saveAsTextFile("./res_2017_07")
    sc.stop()


from numba import jit, f8, i2
import numpy as np

@jit(f8[:](f8[:], f8, i2[:]))
def _proximal_operator(coef, threshold, group_ids):
    """
    Proximal operator.
    """
    result = np.zeros_like(coef).astype(np.float64)
    unique_group_ids = np.unique(group_ids)
    for group_id in unique_group_ids:
        target_idx = group_ids == group_id
        target_coef = coef[target_idx]
        group_norm = np.linalg.norm(target_coef, 2)
        multiplier = 0 if group_norm == 0 else max(0, 1 - threshold / group_norm)
        result[target_idx] = multiplier * target_coef

    return result

@jit(f8(f8, f8[:], i2[:]))
def _group_lasso_penalty(alpha, coef, group_ids):
    penalty = 0.0
    unique_group_ids = np.unique(group_ids)
    for group_id in unique_group_ids:
        penalty += np.linalg.norm(coef[group_ids == group_id], 2)

    return penalty * alpha


from functools import reduce as R
import sys,math
input=sys.stdin.readline
U=lambda:map(int,input().split())
Q=lambda a:(a>0)-(a<0)
T=lambda p,q,r:Q((q[0]-p[0])*(r[1]-p[1])-(r[0]-p[0])*(q[1]-p[1]))
def L(H,r):
    while len(H)>1 and T(H[-2],H[-1],r)<1:H.pop()
    if not len(H) or H[-1]!=r:H.append(r)
    return H
def c(P):
    P.sort()
    l,u=R(L,P,[]),R(L,P[::-1],[])
    return l+[u[i] for i in range(1,len(u)-1)] or l
n,r=U()
C=c([[*U()]for _ in range(n)])
print(sum(math.dist(C[i-1],C[i])for i in range(len(C)))+math.pi*2*r)


import argparse
import csv
import wnhier
import wnNounSim
import wnSemSim

def handle_csv(file):
    """
    Handles csv files by parsing them into a list.
    Requires the delimiter to be a semi-colon.

    Params:
        file: a csv filename
    Returns:
        a list with the csv file contents
    """
    with open(file, newline='', encoding="utf8") as f:
        reader = csv.reader(f, delimiter=';')
        data = list(reader)
    return data

def run_script(mode, data, output_file=None):
    """
    Runs either the hierarchical reasoning or the Wu-Palmer similarity script

    Params:
        mode: Either hierarchical or wu-palmer
        data: a two dimensional list,
            where sentence pairs are the first and second element of a row
        output_file: filename where the results will be appended
    Returns:
        result_list: list of results if no output file is specified.
    """
    results = []
    for i in range(0, len(data)):
        S1 = data[i][0]
        S2 = data[i][1]
        if mode == "hierarchical":
            results.append(wnhier.Sim(S1, S2))
        elif mode == "wu-palmer":
            results.append(wnNounSim.Similarity(S1, S2))
        elif mode == "idf":
            results.append(wnSemSim.Similarity(S1, S2))
    if output_file:
        with open(output_file, 'a', newline='') as f:
            writer = csv.writer(f, delimiter=";")
            for i, row in enumerate(results):
                writer.writerow([data[i][0], data[i][1], results[i]])
    else:
        result_list = []
        print("Results:")
        for i, row in enumerate(results):
            result_list.append([data[i][0], data[i][1], results[i]])
            print(f"Sentence 1: {data[i][0]}, Sentence 2: {data[i][1]}, Similarity: {results[i]}")
        return result_list

def main():
    """
    A Command Line Interface for running either the hierarchical or wu-palmer similarity script
    Help with usage: python main.py --help
    """
    parser = argparse.ArgumentParser(description="Run semantic similarity scripts on csv:s, text files or sentences")
    subparsers = parser.add_subparsers(help="Sub-programs: ")
    parser_test = subparsers.add_parser("test", help="For testing the programs scripts against the STSS-131 dataset. Use 'test -h', for help.")
    parser_test.add_argument("test", help="Run a test of the chosen similarity script", default=True)

    parser_use = subparsers.add_parser("use", help="For actual usage of the program. Use 'use -h', for help.")
    input_group = parser_use.add_mutually_exclusive_group(required=True)
    input_group.add_argument("-c", "--csv", help="Input csv file, must have sentences as the first and second element of each row")
    input_group.add_argument("-s", "--sentences", help="semi-colon separated sentences, e.g. 'The weather is sunny;The moon is not present'")
    parser_use.add_argument("-m", "--mode", help="Choose the mode of similarity measurement", choices=["hierarchical", "wu-palmer", "idf"], required=True)
    parser_use.add_argument("-o", "--output", help="Name of the output file, if empty results will be printed and returned")
    args = parser.parse_args()

    if hasattr(args, "test"):
        print("Running The Inverse Document Frequency based semantic similarity script...")
        wnSemSim.main()
        print("Running Wu-Palmer Noun derivation based semantic similarity script...")
        wnNounSim.main()
        print("Running Hierarchical reasoning semantic similarity test script...")
        wnhier.test_script()
    else:
        if args.csv:
            data = handle_csv(args.csv)
        elif args.sentences:
            data = [args.sentences.split(";")]
        else:
            raise RuntimeError("No input data given")
        result = run_script(args.mode, data, args.output)
        if result:
            return result
        else:
            print(f"Results saved to {args.output}")

if __name__ == "__main__":
    main()


import streamlit as st
from streamlit import session_state as s_state

from pathlib import Path
from tools.io import logo_load, load_databases
from tools.workflow import sidebar_widgets, user_logging

text_format = 'style="font-size: 25px;"'
nirspec_link = 'NIRSpec'
ceers_link = 'website'
reduction_link = 'Pablo et al. (2023)'
publications_link = 'publications'
mail_link = 'author'
limelogo_link = ('')
dr07_link = 'link'

INTRODUCTION_TEXT = (f'
 '
                     f'- On this site you can visualize the {nirspec_link} observations from the CEERs survey.
'
                     f'- You may check the latest CEERs products from its {ceers_link} and the {publications_link}.
'
                     f'- The spectra on this site have been reduced and calibrated using the methodology described in {reduction_link}.
'
                     f'- The line fluxes have been measured using {limelogo_link}. You may contact the {mail_link}, regarding any issue with the measurements or the site operation.'
                     f'
')

RELEASE_TEXT = (f'
 '
                f'- The available data belongs to the CEERs Data release 0.7. You can download these spectra from this {dr07_link}.
'
                f'
')

def run():

    # Sidebar
    menu_items = {'About': '## Learn more about the CEERs survey at https://ceers.github.io/',
                  'Report a bug': "https://github.com/Vital-Fernandez/lime",
                  'Get help': "https://lime-stable.readthedocs.io/en/latest/"}
    st.set_page_config(page_title="CEERs data", layout='wide', menu_items=menu_items)

    # Check for logged-user interface
    force_id = False
    user_logging(check_user=force_id)

    if s_state['auth_status_hold'] is False and force_id is True:
        st.error(f'Username/password is incorrect')

    elif s_state['auth_status_hold'] is None and force_id is True:
        st.warning(f'Please enter you username and password')

    else:

        # Website-page selection message
        st.sidebar.success("Access the data from the sections above")

        # CEERs logo and welcome
        col_logo, col_welcome = st.columns([0.3, 0.7], gap='large')

        with col_logo:
            logo_address = Path(f'format/CEERS_white.png')
            image = logo_load(logo_address)
            st.image(image, width=300)

        with col_welcome:
            welcome_message = f'# Welcome to the CEERs data release'
            if s_state["user"] is not None:
                welcome_message += f',\n# {s_state["user"]}'
            for i in range(8):
                st.write("\n")
            st.markdown(welcome_message)

        # Introduction text
        st.markdown("***")
        st.markdown(INTRODUCTION_TEXT, unsafe_allow_html=True)

        # Load the database
        files_sample, lines_sample = load_databases()

        # Sidebar to select the data
        _idcs_files, _idcs_lines = sidebar_widgets(files_sample, lines_sample)

        # The current
        st.markdown("***")
        st.markdown(RELEASE_TEXT, unsafe_allow_html=True)
        st.dataframe(files_sample.loc[_idcs_files], width=1600)

if __name__ == "__main__":
    run()


# ---LICENSE-BEGIN - DO NOT CHANGE OR MOVE THIS HEADER
# This file is part of the Neurorobotics Platform software
# Copyright (C) 2014,2015,2016,2017 Human Brain Project
# https://www.humanbrainproject.eu
#
# The Human Brain Project is a European Commission funded project
# in the frame of the Horizon2020 FET Flagship plan.
# http://ec.europa.eu/programmes/horizon2020/en/h2020-section/fet-flagships
#
# 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 2
# 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, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
# ---LICENSE-END
from hbp_nrp_cle.brainsim.nest.devices.__NestFixedSpikeGenerator import NestFixedSpikeGenerator as FixedSpikeGenerator
import unittest
from mock import patch, Mock

__author__ = 'LorenzoVannucci'

class TestNestFixedSpikeGenerator(unittest.TestCase):

    def setUp(self):
        self.maxDiff = None

    @patch("hbp_nrp_cle.brainsim.nest.devices.__NestFixedSpikeGenerator.nest")
    def test_default_config(self, sim_mock):
        dev = FixedSpikeGenerator()
        dev.connect(Mock())
        self.assertTrue(sim_mock.Create.called)
        self.assertTrue(sim_mock.SetStatus.called)
        self.assertDictEqual(dev._parameters, {
            'initial_rate': 0.0,
            'C_m': 1000.0,
            'tau_m': 1000.0,
            't_ref': FixedSpikeGenerator.default_parameters['t_ref'],
            'V_th': -50.0,
            'V_reset': -100.0,
            'E_L': -100.0,
            'connector': 'all_to_all',
            'weight': 0.15,
            'delay': 0.1,
            'receptor_type': 'excitatory',
            'synapse_type': {'model': 'static_synapse'}
        })

    @patch("hbp_nrp_cle.brainsim.nest.devices.__NestFixedSpikeGenerator.nest")
    def test_connector(self, sim_mock):
        dev = FixedSpikeGenerator(connector={'weight': 2, 'delay': 4, 'mode': 'OneToOne'}, receptor_type="inhibitory")
        dev.connect(Mock())
        self.assertTrue(sim_mock.Create.called)
        self.assertTrue(sim_mock.SetStatus.called)
        self.assertDictEqual(dev._parameters, {
            'initial_rate': 0.0,
            'C_m': 1000.0,
            'tau_m': 1000.0,
            't_ref': FixedSpikeGenerator.default_parameters['t_ref'],
            'V_th': -50.0,
            'V_reset': -100.0,
            'E_L': -100.0,
            'connector': 'one_to_one',
            'weight': -2,
            'delay': 4,
            'receptor_type': 'inhibitory',
            'synapse_type': {'model': 'static_synapse'}
        })

    @patch("hbp_nrp_cle.brainsim.nest.devices.__NestFixedSpikeGenerator.nest")
    def test_connector_no_delays(self, sim_mock):
        dev = FixedSpikeGenerator(connector={'weight': 1, 'mode': 'Fixed', 'n': 1})
        dev.connect(Mock())
        self.assertTrue(sim_mock.Create.called)
        self.assertTrue(sim_mock.SetStatus.called)
        self.assertDictEqual(dev._parameters, {
            'initial_rate': 0.0,
            'C_m': 1000.0,
            'tau_m': 1000.0,
            't_ref': FixedSpikeGenerator.default_parameters['t_ref'],
            'V_th': -50.0,
            'V_reset': -100.0,
            'E_L': -100.0,
            'connector': {'rule': 'fixed_indegree', 'indegree': 1},
            'weight': 1,
            'delay': 0.1,
            'receptor_type': 'excitatory',
            'synapse_type': {'model': 'static_synapse'}
        })

    @patch("hbp_nrp_cle.brainsim.nest.devices.__NestFixedSpikeGenerator.nest")
    def test_manual_weight_overrides_connector(self, sim_mock):
        dev = FixedSpikeGenerator(connector={'weight': 2, 'delay': 4, 'mode': 'AllToAll'}, weight=42)
        dev.connect(Mock())
        self.assertTrue(sim_mock.Create.called)
        self.assertTrue(sim_mock.SetStatus.called)
        self.assertDictEqual(dev._parameters, {
            'initial_rate': 0.0,
            'C_m': 1000.0,
            'tau_m': 1000.0,
            't_ref': FixedSpikeGenerator.default_parameters['t_ref'],
            'V_th': -50.0,
            'V_reset': -100.0,
            'E_L': -100.0,
            'connector': 'all_to_all',
            'weight': 42,
            'delay': 4,
            'receptor_type': 'excitatory',
            'synapse_type': {'model': 'static_synapse'}
        })

    @patch("hbp_nrp_cle.brainsim.nest.devices.__NestFixedSpikeGenerator.nest")
    def test_synapse_dynamics(self, sim_mock):
        dev = FixedSpikeGenerator(synapse_type={'type': 'TsodyksMarkram', 'U': 0, 'tau_rec': 8, 'tau_facil': 15})
        dev.connect(Mock())
        self.assertTrue(sim_mock.Create.called)
        self.assertTrue(sim_mock.SetStatus.called)
        self.assertDictEqual(dev._parameters["synapse_type"], {
            'model': 'tsodyks_synapse',
            'U': 0,
            'tau_rec': 8,
            'tau_fac': 15
        })

    @patch("hbp_nrp_cle.brainsim.nest.devices.__NestFixedSpikeGenerator.nest")
    def test_invalid_connector_raises(self, _):
        connector = {'weight': 2, 'delay': 4, 'mode': 'invalid'}
        self.assertRaises(Exception, FixedSpikeGenerator, connector=connector)
        del connector['mode']
        self.assertRaises(Exception, FixedSpikeGenerator, connector=connector)

    @patch("hbp_nrp_cle.brainsim.nest.devices.__NestFixedSpikeGenerator.nest")
    def test_activation(self, _):
        dev = FixedSpikeGenerator()
        dev.connect(Mock())

        self.assertTrue(dev.active)

        dev.active = False
        self.assertFalse(dev.active)

        dev.active = True
        self.assertTrue(dev.active)


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

from django.db import models, migrations

class Migration(migrations.Migration):

    dependencies = [
        ('book', '0001_initial'),
    ]

    operations = [
        migrations.AlterField(
            model_name='sequencebook',
            name='number',
            field=models.CharField(max_length=10, null=True, verbose_name='\u041d\u043e\u043c\u0435\u0440 \u0432 \u0441\u0435\u0440\u0438\u0438', blank=True),
            preserve_default=True,
        ),
    ]


from game_data import data
import random
import art
from replit import clear

print(art.logo)
# create two accounts to compare
choice_A = random.choice(data)
# print(choice_A)
choice_B = random.choice(data)
# print(choice_B)
score = 0
if choice_A == choice_B:
  choice_B = random.choice(data)
# print(choice_B)
# print(choice_A['name'])
#print one account vs the other in the console
continue_game = True
while continue_game:
  print(f"Compare A: {choice_A['name']}, a {choice_A['description']}, from {choice_A['country']} ")
  print(art.vs)
  print(f"Compare B: {choice_B['name']}, a {choice_B['description']}, from {choice_B['country']} ")

  # ask the player to choose who is higher
  def winner():
    if choice_A['follower_count'] > choice_B['follower_count']:
      return 'a'
    else:
      return 'b'
  answer = input("Who has more followers? Type 'A' or 'B':").lower()

  #check if their answer
  if answer == winner():
     score += 1
     clear()
     print(art.logo)
     choice_A = choice_B
     choice_B = random.choice(data)
     if choice_A == choice_B:
       choice_B = random.choice(data)
     print(F"You're right! Current score: {score}.")
  else:
    clear()
    print(art.logo)
    print(f"Sorry, that's wrong. Final score: {score}.")
    continue_game = False


#coding:utf-8
import cv2
import numpy as np
import glob

def draw_chease_board(height = 1200, width = 1800, length = 200):
    image = np.zeros((height, width), dtype=np.uint8)
    print(image.shape[0], image.shape[1])

    for j in range(width):
        for i in range(height):
            if (int(i / length) + int(j / length)) % 2:
                image[i, j] = 255
        # 白边
        # image[:, 0:94] = 255
        # image[:, width - 100:width] = 255
        # image[0:94, :] = 255
        # image[height - 94:height, :] = 255

    cv2.imwrite("chess_board.png", image)
    cv2.imshow("chess", image)
    cv2.waitKey(0)
# draw_chease_board()

root_dir = "../data/kinect2/rgb/"
calib_result_intrinsic_fname = root_dir + "intrinsic_mtx.npy"
tvecs_fname = root_dir + "tvecs.npy"
rvecs_fname = root_dir + "rvecs.npy"
images = glob.glob(root_dir + '*.png')
print("list file complete.")
# 找棋盘格角点
# 阈值
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 30, 0.001)
#棋盘格模板规格
w = 8
h = 5
len = 2.87 #黑白格长度
# 世界坐标系中的棋盘格点,例如(0,0,0), (1,0,0), (2,0,0) ....,(8,5,0)，去掉Z坐标，记为二维矩阵
objp = np.zeros((w*h,3), np.float32)
objp[:, :2] = np.mgrid[0:w*len:len,0:h*len:len].T.reshape(-1, 2)
# 储存棋盘格角点的世界坐标和图像坐标对
objpoints = [] # 在世界坐标系中的三维点
imgpoints = [] # 在图像平面的二维点

i = 0
j = 0
for fname in images:
    # i += 1
    # if i%50:
    #     continue
    # j += 1
    print(fname)
    img = cv2.imread(fname)
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    cv2.imshow('gray', gray)
    cv2.waitKey(1)
    # 找到棋盘格角点
    ret, corners = cv2.findChessboardCorners(gray, (w,h),None)
    # 如果找到足够点对，将其存储起来
    if ret == True:
        cv2.cornerSubPix(gray, corners, (11, 11), (-1, -1), criteria)
        objpoints.append(objp)
        imgpoints.append(corners)
        # 将角点在图像上显示
        cv2.drawChessboardCorners(img, (w, h), corners, ret)
        cv2.imshow('findCorners', img)
        cv2.waitKey(1)
cv2.destroyAllWindows()
# print("有多少张图：", j)

# 标定
ret, mtx, dist, rvecs, tvecs = cv2.calibrateCamera(objpoints, imgpoints, gray.shape[::-1], None, None)
print("校准完成，内参：")
print(mtx)
save_dict = {'intrinsic_mtx': mtx, 'distort': dist}
np.save(calib_result_intrinsic_fname, save_dict)
np.save(rvecs_fname, rvecs)
np.save(tvecs_fname, tvecs)

extrinsic_mtxs = []
R_mtxs = []
for fname, rvec, tvec in zip(images, rvecs, tvecs):
    rotation_mtx = cv2.Rodrigues(rvec)[0]
    extrinsic_mtx = np.hstack((rotation_mtx, tvec))
    extrinsic_mtx = np.vstack((extrinsic_mtx, np.array([0, 0, 0, 1])))
    R_mtxs.append(rotation_mtx)
    extrinsic_mtxs.append(extrinsic_mtx)
    print("外参：")
    print(extrinsic_mtx)
    calib_result_extrinsic_fname = fname.replace('.png', 'extrinsic_mtx.npy')
    save_dict = {'tvec': tvec, 'rvec': rvec, 'extrinsic_mtx': extrinsic_mtx, 'rotation_mtx': rotation_mtx}
    np.save(calib_result_extrinsic_fname, save_dict)

# print("done")
# 去畸变
img2 = cv2.imread(root_dir + '115.png')
h,  w = img2.shape[:2]
dst = cv2.undistort(img2, mtx, dist)
cv2.imshow("distort", dst)
# cv2.imwrite('calibresult.png',dst)

# rgb相机去畸
rgb_image = img2
cv2.imshow("rgb", rgb_image)
newcameramtx, roi = cv2.getOptimalNewCameraMatrix(mtx, dist, rgb_image.shape[1::-1], 1)
map1, map2 = cv2.initUndistortRectifyMap(mtx, dist, None, newcameramtx, rgb_image.shape[1::-1], cv2.CV_32FC1)
rgb_image_undistort = cv2.remap(rgb_image, map1, map2, cv2.INTER_NEAREST)
rgb_intrinsic_mtx = newcameramtx
print("new rgb_camera mtx:\n", newcameramtx)
cv2.imshow("rgb_undistort", rgb_image_undistort)
cv2.waitKey(0)


from botoy import Botoy, Action, GroupMsg
from botoy import decorators as deco
import os
import json
import requests, time

from utils.fileio import read_json, write_json
from utils.twitter_manager import TwitterManager

resource_path = "./resources/twitter_timeline_alarm"

bot = Action(
    qq = int(os.getenv('BOTQQ'))
)

tm = TwitterManager()

@deco.ignore_botself
def receive_group_msg(ctx: GroupMsg):
    if ctx.MsgType == "TextMsg":
        content_list = ctx.Content.strip().split()
        if content_list and content_list[0] == '推特关注':
            subscribes = read_json(os.path.join(resource_path, "subscribes.json"))
            content_list = content_list[1:]
            t = ''
            if content_list:
                uid = content_list[0]
                res = tm.get_user_info(uid)
                if res.status_code != 200:
                    bot.sendGroupText(ctx.FromGroupId, f"error in get_twitch_streams\nuser_login: {uid}\nres: {res.text}")
                    return
                user_info = tm.parse_user_info(res.json())
                if not user_info:
                    bot.sendGroupText(ctx.FromGroupId, f"找不到用户")
                    return
                if uid not in subscribes:
                    subscribes[uid] = {'groups': []}
                if ctx.FromGroupId not in subscribes[uid]['groups']:
                    subscribes[uid]['groups'].append(ctx.FromGroupId)
                    t += f"已关注 {user_info['name']}@{uid}\n\n"
                    write_json(os.path.join(resource_path, "subscribes.json"), subscribes)
            sub_list = []
            for uid in subscribes:
                if ctx.FromGroupId in subscribes[uid]['groups']:
                    sub_list.append(uid)
            if sub_list:
                t += "你群目前关注以下用户:\n"
                for sub in sub_list:
                    t += sub + "\n"
                t = t[:-1]
                bot.sendGroupText(ctx.FromGroupId, t)
            else:
                bot.sendGroupText(ctx.FromGroupId, "啥也没有")
        if content_list and content_list[0] == '取消推特关注':
            content_list = content_list[1:]
            if not content_list:
                return
            subscribes = read_json(os.path.join(resource_path, "subscribes.json"))
            t = ''
            uid = content_list[0]
            if uid not in subscribes or ctx.FromGroupId not in subscribes[uid]["groups"]:
                t += '未找到关注\n\n'
            else:
                del subscribes[uid]["groups"][subscribes[uid]["groups"].index(ctx.FromGroupId)]
                if not subscribes[uid]["groups"]:
                    del subscribes[uid]
                t = f"你已取消关注{uid}的推特通知\n\n"
                write_json(os.path.join(resource_path, "subscribes.json"), subscribes)
            sub_list = []
            for uid in subscribes:
                if ctx.FromGroupId in subscribes[uid]['groups']:
                    sub_list.append(uid)
            if sub_list:
                t += "你群关注了以下用户：\n"
                for sub in sub_list:
                    t += sub + "\n"
                t = t[:-1]
                bot.sendGroupText(ctx.FromGroupId, t)
            else:
                bot.sendGroupText(ctx.FromGroupId, "当前没有关注")


import random
from django.core.management.base import BaseCommand
from django_seed import Seed
from reviews import models as models_reviews
from places import models as models_places
from users import models as models_users

class Command(BaseCommand):
    help = "This command created many reviews"

    def add_arguments(self, parser):
        parser.add_argument(
            "--number",
            default=1,
            type=int,
            help="How many reviews do you want to create?",
        )

    def handle(self, *args, **options):
        number = options.get("number")
        seeder = Seed.seeder()
        users = models_users.User.objects.all()
        places = models_places.Place.objects.all()
        seeder.add_entity(
            models_reviews.Review,
            number,
            {
                "safety": lambda x: random.randint(0, 5),
                "accuracy": lambda x: random.randint(0, 5),
                "communication": lambda x: random.randint(0, 5),
                "location": lambda x: random.randint(0, 5),
                "value": lambda x: random.randint(0, 5),
                "place": lambda x: random.choice(places),
                "user": lambda x: random.choice(users),
            },
        )
        seeder.execute()
        self.stdout.write(self.style.SUCCESS(f"{number} reivews created!"))


from flask import Flask, render_template, request, redirect, flash
from flask_mysqldb import MySQL
from scripts.scraper import getReddit
import yaml

app = Flask(__name__)

# Configure db
db = yaml.safe_load(open("db.yaml"))
app.config['MYSQL_HOST'] = db['mysql_host']
app.config['MYSQL_USER'] = db['mysql_user']
app.config['MYSQL_PASSWORD'] = db['mysql_password']
app.config['MYSQL_DB'] = db['mysql_db']

# app.config['MYSQL_HOST'] = 'aws-lab.cu3ottuyfwbk.us-east-1.rds.amazonaws.com'
# app.config['MYSQL_USER'] = 'admin'
# app.config['MYSQL_PASSWORD'] = '29!nt$XELNE1'
# app.config['MYSQL_DB'] = 'flaskapp'

mysql = MySQL(app)

@app.route('/')
def index():
        # data = getReddit() #getting back the pandas dataframe of json data
        # if data != None:
        #     return render_template('index.html', data=data)
        # else:
        #     return render_template("error.html")

        data = getReddit() #getting back the pandas dataframe of json data
        return render_template('index.html', data=data)
        # if request.method == 'POST':
        #     # Fetch form data
        #     userDetails = request.form
        #     if userDetails['name'] != '' and userDetails['email'] != '': #if both fields are not empty
        #         name = userDetails['name']
        #         email = userDetails['email']
        #         cur = mysql.connection.cursor()
        #         cur.execute("INSERT INTO users(name, email) VALUES(%s, %s)",(name, email))
        #         mysql.connection.commit()
        #         cur.close()
        #         return "
Success
"
        # return render_template('index.html')

@app.route('/newsletter', methods=['GET', 'POST'])
def newsletter():
        if request.method == 'POST':
            # Fetch form data
            userDetails = request.form
            if userDetails['name'] != '' and userDetails['email'] != '': #if both fields are not empty
                name = userDetails['name']
                email = userDetails['email']
                cur = mysql.connection.cursor()
                cur.execute("INSERT INTO users(name, email) VALUES(%s, %s)",(name, email))
                mysql.connection.commit()
                cur.close()
                # flash("You have signed up for the newsletter! Thank you!")
                return redirect(request.url)

        return render_template('newsletter.html')

if __name__ == "__main__":
        app.run()


from world import World
import json
import cProfile

unit_testing = False

def main():

    if unit_testing:
        world = World(30, 1, 1)
        world.unit_test()
    else:
        with open("species.json", "w") as speciesFile:
            speciesFile.write("{}")
        world = World(50, 1, 1000)
        while True:
            if world.make_moves():
                continue
            else:
                generations = {}
                with open("species.json", "r") as speciesFile:
                    generations = json.load(speciesFile)
                    generations[f"generation{world.generation}"] = world.current_generation_best_move_history[f"generation{world.generation}"]

                with open("species.json", "w") as speciesFile:
                    json.dump(generations, speciesFile, indent=4)

#main()

cProfile.run("main()", sort="cumtime")


from pytube import Playlist, YouTube
import re
from datetime import timedelta
from tqdm import tqdm

# Ask user for YouTube playlist URL
playlist_url = input("Enter YouTube playlist URL: ")

# Extract playlist ID from URL
playlist_id = re.findall(r"(?<=list=)[^&$]+", playlist_url)[0]

# Initialize playlist object
playlist = Playlist(f"https://www.youtube.com/playlist?list={playlist_id}")

print("Processing playlist...")

# Initialize variables
total_duration = 0
num_processed = 0

# Loop through each video in playlist
for url in tqdm(playlist.video_urls):
    yt = YouTube(url)
    if yt:
        duration = yt.length
        total_duration += duration
        num_processed += 1
    else:
        print(f"Could not process video: {url}")

# Format total duration
total_duration = str(timedelta(seconds=total_duration))

# Print total duration
print(f"Total duration: {total_duration}")


from fractions import Fraction

# Fungsi untuk mencari bentuk baris eselon tereduksi/tak tereduksi dari matriks
def getRowEchelon(mat):
    row, col = len(mat), len(mat[0])
    lead = 0

    for i in range(row):
        # Mencari posisi nilai terdepan
        while lead < col and mat[i][lead] == 0:
            for j in range(i, row):
                if mat[j][lead] == 0:
                    continue

                if i != j:
                    mat[i], mat[j] = mat[j], mat[i]

                lead -= 1
                break

            lead += 1

        # Membagi semua elemen dalam i dengan nilai terdepan
        for j in range(col - 1, lead - 1, -1):
            mat[i][j] /= mat[i][lead]

        # Menjadikan mat[i][lead] sebagai pivot dan melakukan OBE pada baris lainnya
        for j in range(row):
            if j == i:
                continue

            for k in range(col - 1, lead - 1, -1):
                mat[j][k] -= mat[j][lead] * mat[i][k]

        lead += 1

    return mat

# Input dengan matriks augmented dalam bentuk desimal atau pecahan
def matrixAugmentedInput(row, decimal = False):
    matrix = [list(map(float if decimal else Fraction, input().split())) for i in range(row)]
    return matrix

# Main Function
def main():
    row = int(input("Jumlah Baris: "))
    col = int(input("Jumlah Kolom: "))

    tipeInput = input("Input desimal atau bukan(ya/tidak): ")

    matrix = getRowEchelon(matrixAugmentedInput(row, tipeInput == "ya"))
    print(matrix)
    print()

    # Output desimal
    for i in matrix:
        print(i)

    # Output pecahan
    for i in matrix:
        res = ''

        for j in i:
            res += str(j) + '\t'

        print(res)

if __name__ == "__main__":
    main()


import torch.nn as nn
import torch

class BCE(nn.Module):
	def __init__(self, user_size, item_size, dim):
		super().__init__()
		self.user_embd = nn.Embedding(user_size, dim)
		self.item_embd = nn.Embedding(item_size, dim)
		# self.user_bias = nn.Embedding(user_size, 1)
		# self.item_bias = nn.Embedding(item_size, 1)

		nn.init.xavier_normal_(self.user_embd.weight)
		nn.init.xavier_normal_(self.item_embd.weight)
		# self.user_bias.weight.data.fill_(0.)
		# self.item_bias.weight.data.fill_(0.)

	def forward(self, u, i):
		user = self.user_embd(u)
		item = self.item_embd(i)
		# u_bias = self.user_bias(u)
		# i_bias = self.item_bias(i)
		# prediction = u_bias + i_bias + (user * item).sum(dim = -1)
		prediction = (user * item).sum(dim = -1)
		return prediction.squeeze()

class BPR(nn.Module):
	def __init__(self, user_size, item_size, dim):
		super().__init__()
		self.user_embd = nn.Embedding(user_size, dim)
		self.item_embd = nn.Embedding(item_size, dim)
		nn.init.xavier_normal_(self.user_embd.weight)
		nn.init.xavier_normal_(self.item_embd.weight)

	def forward(self, u, i, j):
		user = self.user_embd(u)
		item_i = self.item_embd(i)
		item_j = self.item_embd(j)

		prediction_i = (user * item_i).sum(dim = -1)
		prediction_j = (user * item_j).sum(dim = -1)

		return prediction_i, prediction_j


import asyncio
import aiofiles
import json
from os.path import exists

from ..exceptions import WiserExtraConfigError

class _WiserExtraConfig:
    def __init__(self, config_file: str, hub_name: str):
        self._config_file = config_file + "_" + hub_name
        self._config = {}

    async def async_load_config(self):
        if exists(self._config_file):
            async with aiofiles.open(self._config_file, mode="r") as config_file:
                try:
                    contents = await config_file.read()
                    if contents:
                        self._config = json.loads(contents)
                    config_file.close()

                except (
                    OSError,
                    EOFError,
                    TypeError,
                    AttributeError,
                    json.JSONDecodeError,
                ) as ex:
                    raise WiserExtraConfigError("Error loading extra config file")
        else:
            await self.async_update_config("Info", "Version", "1.0.0")
        return

    async def async_update_config(self, section: str, key: str, value):
        # If section doesn't exist
        if section not in self._config:
            self._config[section] = {}

        # If key not in section
        if key and value:
            if self._config[section].get(key):
                self._config[section][key].update(value)
            else:
                self._config[section][key] = value

        await self.async_write_config()

    async def async_remove_config(self, section: str, key: str):
        if key and self._config[section].get(key):
            del self._config[section][key]

        await self.async_write_config()

    async def async_write_config(self):
        # Write to config file
        async with aiofiles.open(self._config_file, mode="w") as config_file:
            try:
                await config_file.write(json.dumps(self._config, indent=2))
                config_file.close()
            except (
                OSError,
                EOFError,
                TypeError,
                AttributeError,
                json.JSONDecodeError,
            ) as ex:
                raise WiserExtraConfigError("Error writing to extra config file")

    # @property
    def config(self, section: str = None, key: str = None):
        try:
            if section:
                if key:
                    return self._config[section][key]
                else:
                    return self._config[section]
            else:
                return self._config
        except:
            return None


# Импорт необходимых библиотек и модулей
import pytest
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import email_config  # Импорт конфигурационного файла с электронной почтой

@pytest.fixture
def driver():
    # Инициализация веб-драйвера
    driver = webdriver.Chrome()
    yield driver
    driver.quit()  # Закрыть браузер после завершения теста

def test_password_recovery(driver):
    # Открыть сайт Ростелеком
    driver.get("https://lk.rt.ru")

    # Ожидание появления кнопки "Войти с паролем" и клик по ней
    WebDriverWait(driver, 10).until(
        EC.element_to_be_clickable((By.CSS_SELECTOR, "#standard_auth_btn"))
    ).click()

    # Ожидание появления кнопки "Забыл пароль" и клик по ней
    WebDriverWait(driver, 10).until(
        EC.element_to_be_clickable((By.CSS_SELECTOR, "#forgot_password"))
    ).click()

    # Клик по кнопке для восстановления пароля по почте
    WebDriverWait(driver, 10).until(
        EC.element_to_be_clickable((By.CSS_SELECTOR, "#t-btn-tab-mail"))
    ).click()

    # Ожидание появления поля ввода почты, ввод почты
    WebDriverWait(driver, 10).until(
        EC.visibility_of_element_located((By.CSS_SELECTOR, "#username"))
    ).send_keys(email_config.email)  # Использование электронной почты из конфигурационного файла

    # Ожидание появления кнопки "Продолжить" и клик по ней
    WebDriverWait(driver, 10).until(
        EC.element_to_be_clickable((By.CSS_SELECTOR, "#reset"))
    ).click()

    # Ожидание в 20 секунд для ручного ввода кода из почты
    print("Ожидание ввода кода из почты...")
    driver.implicitly_wait(20)  # Пауза в 20 секунд

    # Попытка найти сообщение об ошибке
    error_message_element = EC.presence_of_element_located((By.CSS_SELECTOR, "#form-error-message"))
    success_window_element = EC.presence_of_element_located((By.CSS_SELECTOR, "#page-right > div"))

    if WebDriverWait(driver, 10).until(error_message_element):
        error_message = driver.find_element(By.CSS_SELECTOR, "#form-error-message").text
        assert error_message == "Этот пароль уже использовался, укажите другой пароль", f"Ошибка: {error_message}"
    elif WebDriverWait(driver, 10).until(success_window_element):
        print("Процесс восстановления пароля прошел успешно")
    elif driver.current_url == "https://lk.rt.ru":
        print("Процесс восстановления пароля прошел успешно, пользователь перенаправлен на страницу авторизации")
    else:
        print("Неизвестное состояние, тест не удался")


# -*- coding: utf-8 -*-
"""
Created on Tue Feb 16 11:29:36 2021

@author: DELL
"""

import speech_recognition as sr
AUDIO_FILE=("Downloads/Dil_Todi_Na_Song.wav")
#use audio file as the source

r=sr.Recognizer()
#initialise the recogniser

with sr.AudioFile(AUDIO_FILE) as source:
    audio=r.record(source)
    #reads the audio file

try:
    print("audio file contains " +r.recognize_google(audio))

except sr.UnknownValueError:
    print("Google Speech Recognition could not understand audio.")
except sr.RequestError:
    print("Could not get the results from google Speec Recognition")


'''
Write a function called list_manipulator which receives a list of numbers as first parameter and different amount of other parameters. The second parameter might be "add" or "remove". The third parameter might be "beginning" or "end". There might or might not be any other parameters (numbers):
 - In case of "add" and "beginning",
    add the given numbers to the beginning of the given list of numbers
    and return the new list

 - In case of "add" and "end",
    add the given numbers to the end of the given list of numbers
    and return the new list

 - In case of "remove" and "beginning"
    - If there is another parameter (number), remove that amount of numbers from
        the beginning of the list of numbers.
    - If there are no other parameters, remove only the first element of the list.
    - Finaly, return the new list

 - In case of "remove" and "end"
    - If there is another parameter (number), remove that amount of numbers from
    the end of the list of numbers.
    - Otherwise if there are no other parameters, remove only the last element of the list.
    - Finaly, return the new list
'''

from collections import deque

def list_manipulator(nums, *args):
    numbers = deque(nums)
    parameters = len(args)
    action = args[0]
    direction = args[1]

    if action == 'add' and direction == 'beginning':
        if parameters > 2:
            items = []
            for a in args[2:]:       # get all extra numbers
                items.append(a)

            for i in items[::-1]:    # add them to front starting from the last one
                numbers.appendleft(i)
        return list(numbers)

    elif action == 'add' and direction == 'end':
        if parameters > 2:
            items = []
            for a in args[2:]:      # get all extra numbers
                items.append(a)

            for i in items:
                numbers.append(i)   # add them to original list one by one
        return list(numbers)

    elif action == 'remove' and direction == 'beginning':
        if parameters > 2:
            items = args[2]
            for _ in range(items):
                numbers.popleft()
        else:
            numbers.popleft()
        return list(numbers)

    elif action == 'remove' and direction == 'end':
        if parameters > 2:
            items = args[2]
            for _ in range(items):
                numbers.pop()
        else:
            numbers.pop()
        return list(numbers)

print(list_manipulator([1,2,3], "remove", "end"))
print(list_manipulator([1,2,3], "remove", "beginning"))

print(list_manipulator([1,2,3], "add", "beginning", 20))

print(list_manipulator([1,2,3], "add", "end", 30))

print(list_manipulator([1,2,3], "remove", "end", 2))

print(list_manipulator([1,2,3], "remove", "beginning", 2))

print(list_manipulator([1,2,3], "add", "beginning", 20, 30, 40))
print(list_manipulator([1,2,3], "add", "end", 30, 40, 50))


import pandas as pd
import requests
from pandas.io.json import json_normalize
import datetime
from dateutil import parser
import co2_converter_common as ccc
from dataiku.customrecipe import get_recipe_config

# Get input parameters:
input_df, output_dataset, columns_names = ccc.get_input_output()
coordinates = ccc.get_coordinates(input_df)
date_column_name = ccc.get_date_column_name(input_df)
extracted_geopoint, extracted_longitude, extracted_latitude = ccc.get_geopoint_column_names(columns_names)
user_selected_columns = get_recipe_config().get('user_selected_columns')

API_ENDPOINT = 'https://api.electricitymap.org/v3/power-breakdown/past-range'
API_TOKEN = ccc.get_api_token()

# Date is not in the future:
input_df[date_column_name] = pd.to_datetime(input_df[date_column_name], format="%Y-%m-%dT%H:%M:%S.%fZ", utc=True)
now = datetime.datetime.utcnow()
if max(input_df[date_column_name]).timestamp() > now.timestamp():
    raise Exception("Date is in the future. Please check your input dataset or use the CO2 forecast component.")

# Parse Geopoint to longitude and latitude:
input_df[extracted_geopoint] = input_df[coordinates].apply(lambda point: ccc.parse_wkt_point(point))
input_df[extracted_longitude] = input_df[extracted_geopoint].apply(lambda point: point[0])
input_df[extracted_latitude] = input_df[extracted_geopoint].apply(lambda point: point[1])

# setup request
session = requests.session()

# GroupBy latitude, longitude to retrieve only one API call per coordinates:
dates_per_unique_latitude_longitudes = input_df.groupby([extracted_longitude, extracted_latitude])[date_column_name].unique()
data = pd.DataFrame()

# for each unique location location and with 10 days chunks (to avoid API limit):

for index_latitude_longitude, dates in enumerate(dates_per_unique_latitude_longitudes):

    # Parse dates:
    now = datetime.datetime.utcnow()
    max_date = parser.parse(str(dates.max()))
    min_date = parser.parse(str(dates.min()))

    # Get only the day from the dates:
    min_date_day = min_date.strftime("%Y-%m-%d")
    max_date_day = max_date.strftime("%Y-%m-%d")

    # As the API is limited to 10 days, I create chunks of dates:
    chunked_dates = ccc.date_chunk(min_date_day, max_date_day, 10)

    for index_chunked_dates in range(len(chunked_dates)):
        params = {
            'lat': dates_per_unique_latitude_longitudes.index[index_latitude_longitude][1],
            'lon': dates_per_unique_latitude_longitudes.index[index_latitude_longitude][0],
            'start': chunked_dates[index_chunked_dates][0],
            'end': chunked_dates[index_chunked_dates][-1]
        }

        # make request and create df dataframe with response from API:
        response = session.get(API_ENDPOINT, params=params, auth=('auth-token', API_TOKEN))
        response_json = response.json()
        chunked_data = json_normalize(response_json['data'])
        chunked_data['latitude'] = dates_per_unique_latitude_longitudes.index[index_latitude_longitude][1]
        chunked_data['longitude'] = dates_per_unique_latitude_longitudes.index[index_latitude_longitude][0]
        data = data.append(chunked_data, ignore_index=True)

    # Filtering on the columns selected by the user:
    data_to_return = data[['datetime', 'latitude', 'longitude']]
    for user_selected_column in user_selected_columns:
        column_list = [column for column in data.columns if column.startswith(user_selected_column)]
        user_selected_data = data[column_list]
        data_to_return = pd.concat((data_to_return, user_selected_data), axis=1)

    # rename datetime in e-mix_date_time:
    data_to_return = data_to_return.rename(columns={
        "datetime": "e-mix_date_time", "latitude": extracted_latitude, "longitude": extracted_longitude}
    )

    # join on date with input_df:
    # convert emix_date_time to datetime format:
    data_to_return['e-mix_date_time'] = pd.to_datetime(data_to_return['e-mix_date_time'])

    # convert DateColName to datetime format:
    input_df[date_column_name] = pd.to_datetime(input_df[date_column_name])

    output_df = ccc.merge_w_nearest_keys(input_df, data_to_return, date_column_name, 'e-mix_date_time', by=[extracted_latitude, extracted_longitude])

    # drop extracted columns (not needed):
    output_df.drop([extracted_geopoint, extracted_latitude, extracted_longitude], axis=1, inplace=True)

# ##################################### output ######################################

# Write output
output_dataset.write_with_schema(output_df)


# -*- coding: utf-8 -*-
"""
Created on Mon Oct 28 18:27:01 2019

@author: Paulo
"""

import pandas as pd
import numpy as np
from PIL import Image
from nightmares.images_defines import GDOT_SAVE_FOLDER
from os.path import join, isdir
from os import listdir as ls

def create_info_cols(df):
    """ Create width and height columns in a dataframe.
        Arguments:
            df: Dataframe to put the columns.
        Returns:
            df: Modified dataframe.
    """
    df["width"] = np.nan
    df["height"] = np.nan
    return df

def put_image_info(df, name, width=np.nan, height=np.nan):
    """ Put width and height information into a dataframe.
        Arguments:
            df: Dataframe to put width and height information.
            name: Name of the image to put the information.
            width: Width of the image.
            height: Height of the image.
        Returns:
            df: Modified dataframe.
    """
    df.loc[df.name == name, "width"] = width
    df.loc[df.name == name, "height"] = height
    return df

def join_per_year(root_folder=GDOT_SAVE_FOLDER):
    """ Uses all years images informations to make a info file with all csv.
        The function will read the images.csv files, join then and put the new
        file in the root_folder.s
        Arguments:
            root_folder: Folder with the year/month folders.
    """
    infos = []
    for year_folder in ls(root_folder):
        print(year_folder)
        year_folder = join(root_folder, year_folder)
        if not isdir(year_folder): continue
        image_info_file = join(year_folder, "images.csv")
        try:
            info = pd.read_csv(image_info_file)
            info = create_info_cols(info)
        except:
            print("image.csv from {} does not exists!".format(year_folder))
            continue
        for month_folder in ls(year_folder):
            month_folder = join(year_folder, month_folder)
            if not isdir(month_folder): continue
            images = ls(month_folder)
            for image in images:
                image_filepath = join(month_folder, image)
                with Image.open(image_filepath) as img:
                    width, height = img.size
                    info = put_image_info(info, image, width=width, height=height)
        info.to_csv(image_info_file, index=False)
        infos.append(info)
    infos = pd.concat(infos, axis=0)
    infos.to_csv(join(root_folder, "images.csv"), index=False)


from datetime import datetime
from chalicelib import s3
from chalicelib.constants import EVENT_ARRIVAL, EVENT_DEPARTURE

import itertools

DATE_FORMAT_MASSDOT = "%Y-%m-%d %H:%M:%S"
DATE_FORMAT_OUT = "%Y-%m-%dT%H:%M:%S"

def pairwise(iterable):
    # an itertools recipe from the docs
    # "s -> (s0,s1), (s1,s2), (s2, s3), ..."
    a, b = itertools.tee(iterable)
    next(b, None)
    return zip(a, b)

def unique_everseen(iterable, key=None):
    # an itertools recipe from the docs
    # "List unique elements, preserving order. Remember all elements ever seen."
    # unique_everseen('AAAABBBCCDAABBB') --> A B C D
    # unique_everseen('ABBCcAD', str.lower) --> A B C D
    seen = set()
    seen_add = seen.add
    if key is None:
        for element in itertools.filterfalse(seen.__contains__, iterable):
            seen_add(element)
            yield element
    else:
        for element in iterable:
            k = key(element)
            if k not in seen:
                seen_add(k)
                yield element

def dwells(stop_ids, sdate, edate):
    rows_by_time = s3.download_events(sdate, edate, stop_ids)

    dwells = []
    for maybe_an_arrival, maybe_a_departure in pairwise(rows_by_time):
        # Look for all ARR/DEP pairs for same trip id
        if (
            maybe_an_arrival["event_type"] in EVENT_ARRIVAL
            and maybe_a_departure["event_type"] in EVENT_DEPARTURE
            and maybe_an_arrival["trip_id"] == maybe_a_departure["trip_id"]
        ):
            dep_dt = datetime.strptime(maybe_a_departure["event_time"], DATE_FORMAT_MASSDOT)
            arr_dt = datetime.strptime(maybe_an_arrival["event_time"], DATE_FORMAT_MASSDOT)
            delta = dep_dt - arr_dt
            dwells.append(
                {
                    "route_id": maybe_a_departure["route_id"],
                    "direction": int(maybe_a_departure["direction_id"]),
                    "arr_dt": arr_dt.strftime(DATE_FORMAT_OUT),
                    "dep_dt": dep_dt.strftime(DATE_FORMAT_OUT),
                    "dwell_time_sec": delta.total_seconds(),
                }
            )

    return dwells

def headways(stop_ids, sdate, edate):
    rows_by_time = s3.download_events(sdate, edate, stop_ids)

    only_departures = filter(lambda row: row["event_type"] in EVENT_DEPARTURE, rows_by_time)

    headways = []
    for prev, this in pairwise(only_departures):
        if this["trip_id"] == prev["trip_id"] != "":
            # in rare cases, same train can arrive and depart twice
            # Here, we should skip the headway
            # (though if trip_id is empty, we don't know).
            continue
        this_dt = datetime.strptime(this["event_time"], DATE_FORMAT_MASSDOT)
        prev_dt = datetime.strptime(prev["event_time"], DATE_FORMAT_MASSDOT)
        delta = this_dt - prev_dt
        headway_time_sec = delta.total_seconds()

        # Throw out any headways > 120 min
        if headway_time_sec > 120 * 60:
            continue

        benchmark_headway = this.get("scheduled_headway")
        if benchmark_headway == "":
            benchmark_headway = None

        headways.append(
            {
                "route_id": this["route_id"],
                "direction": this["direction_id"],
                "current_dep_dt": this["event_time"],
                "headway_time_sec": headway_time_sec,
                "benchmark_headway_time_sec": benchmark_headway,
            }
        )

    return headways

def travel_times(stops_a, stops_b, sdate, edate):
    rows_by_time_a = s3.download_events(sdate, edate, stops_a)
    rows_by_time_b = s3.download_events(sdate, edate, stops_b)

    departures = filter(lambda event: event["event_type"] in EVENT_DEPARTURE, rows_by_time_a)
    # we reverse arrivals so that if the same train arrives twice (this can happen),
    # we get the earlier time.
    arrivals = {
        (event["service_date"], event["trip_id"]): event
        for event in reversed(list(rows_by_time_b))
        if event["event_type"] in EVENT_ARRIVAL and event["trip_id"] != ""
    }

    travel_times = []
    for departure in unique_everseen(departures, key=lambda x: (x["service_date"], x["trip_id"])):
        arrival = arrivals.get((departure["service_date"], departure["trip_id"]))
        if arrival is None:
            continue
        dep = departure["event_time"]
        arr = arrival["event_time"]

        dep_dt = datetime.strptime(dep, DATE_FORMAT_MASSDOT)
        arr_dt = datetime.strptime(arr, DATE_FORMAT_MASSDOT)
        delta = arr_dt - dep_dt
        travel_time_sec = delta.total_seconds()

        if travel_time_sec < 0:
            continue

        # benchmark calculation:
        # not every file will have the scheduled_tt field, so we use get.
        sched_arr = arrival.get("scheduled_tt")
        sched_dep = departure.get("scheduled_tt")
        try:
            # sched values may be None or ''
            benchmark = float(sched_arr) - float(sched_dep)
        except (TypeError, ValueError):
            benchmark = None

        travel_times.append(
            {
                "route_id": departure["route_id"],
                "direction": int(departure["direction_id"]),
                "dep_dt": dep_dt.strftime(DATE_FORMAT_OUT),
                "arr_dt": arr_dt.strftime(DATE_FORMAT_OUT),
                "travel_time_sec": travel_time_sec,
                "benchmark_travel_time_sec": benchmark,
            }
        )

    return travel_times


import numpy as np
import pandas as pd
from sdt_analysis import *
import matplotlib.pyplot as plt
import os
import argparse
import random

mc = "black"
plt.rcParams.update(
    {
        "font.size": 40,
        "axes.labelcolor": "{}".format(mc),
        "xtick.color": "{}".format(mc),
        "ytick.color": "{}".format(mc),
        "font.family": "sans-serif",
    }
)

if __name__ == "__main__":

    parser = argparse.ArgumentParser(description="Folder name")
    parser.add_argument("-f", type=str)
    args = parser.parse_args()
    folder_name = args.f

    path = os.path.realpath(__file__)
    root_path = path.rsplit("/", 3)[0]

    table_data = pd.read_csv(f"{root_path}/data/{folder_name}/data/data_all.csv")
    nofailed_table_data = table_data.loc[table_data["failed"] == False]
    cleaned_table_data = nofailed_table_data.loc[
        nofailed_table_data["stimulus_time"] > 0.4
    ]

    tables_per_position = {}
    percs_poses = []

    for i, cond in enumerate(cleaned_table_data["position"].unique()):
        tables_per_position[cond] = {
            "table": cleaned_table_data.loc[cleaned_table_data["position"] == cond]
        }
        tables_per_position[cond]["responses"] = (
            tables_per_position[cond]["table"]["responses"].value_counts().to_dict()
        )
        try:
            perc_no = round(
                tables_per_position[cond]["responses"][0]
                / sum(tables_per_position[cond]["responses"].values())
                * 100
            )
        except:
            perc_no = 0
        percs_poses.append(perc_no)

    fig = plt.figure(figsize=(35, 20))
    ax = fig.add_subplot(111)
    random.shuffle(percs_poses)

    for i, perc_no in enumerate(percs_poses):
        ax.bar(i, perc_no, color="k")
        ax.text(i, perc_no + 0.1, str(round(perc_no, 2)))

    ax.set_ylabel("Perc NO")
    ax.set_xlabel("Positions")
    ax.set_ylim([0, 100])

    plt.tick_params(axis="x", which="both", bottom=False, top=False, labelbottom=False)

    plt.savefig(f"{root_path}/data/{folder_name}/figures/perc_no_pos_sdt.png")


# To add a new cell, type '# %%'
# To add a new markdown cell, type '# %% [markdown]'
# %%
#

# %% [markdown]
# # Initial Bringup of the Sovol-SO1
#
# Much of these are taken from: https://github.com/Springwald/GCodePlotter/blob/main/code/Plotter/SovolS01Hardware.cs
#
# It was learnt that the Sovol-SO1 does not require a \r, only \n is used
#
# Shows up as a USB to serial device, with a 115200 baud rate.

# %%
# Requires pyserial library to be installed
import serial
serial_port = "/dev/ttyUSB0" # Set this to whatever tty serial dev
baudrate = 115200
serial = serial.Serial(serial_port, baudrate)

# %%
serial.close()

# %%
# Clear the buffer
serial.write(b"\n\r")
time.sleep(1.0)
res = serial.read_all()
print(res)

# %%
# Get some basic settings set
serial.write(b"G90\n\r") # Set to absolute mode
serial.write(b"G21\n\r") # Set to mm

# %%
# Pen Down
serial.write(b"M280P0S0\n\r")

# %%
# Pen Up (HIGH)

serial.write(b"M280P0S90\n\r")

# %%
# Pen Up (Normal)

serial.write(b"M280P0S30\n\r")

# %%
# Pause
serial.write(b"M280P0S0\n\r") # pen down
serial.write(b"G4 P2000\n\r") # P50 is the pause length in ms
serial.write(b"M280P0S30\n\r") # pen up

# %%
# Auto home
serial.write(b"G28\n\r")

# %%
# Go to position
x = 100
y = 120.0

max_y = 300 #mm
max_x = 300 #mm
sendstr = f"G1 X{x:0.3f} Y{y:0.3f}\n\r".encode('utf-8')
print(sendstr)
serial.write(sendstr)

# %%
# Travel speed
serial.write(b"G1 F10000\n\r")

# %%
#Paint Speed
# Travel speed
serial.write(b"G1 F3000\n\r")

# %%
import time
with SovolSO1("/dev/ttyUSB0") as plotter:
    plotter.autoHome()
    plotter.setTravelSpeed()
    plotter.setPen(PenState.HIGH_UP)
    plotter.pause(50)
    plotter.moveTo((75.0,75.0))
    plotter.setDrawingSpeed()
    plotter.setPen(PenState.DOWN)
    plotter.pause(50)
    plotter.moveTo((100.0,100.0))
    plotter.moveTo((50.0,100.0))
    plotter.moveTo((75.0,75.0))
    plotter.setPen(PenState.HIGH_UP)
    plotter.pause(50)
    plotter.moveTo((50.0,100.0))
    plotter.setPen(PenState.DOWN)
    plotter.pause(50)
    plotter.moveTo((50.0,50.0))
    plotter.moveTo((75.0,75.0))
    plotter.setPen(PenState.HIGH_UP)
    plotter.pause(50)
    plotter.moveTo((50.0,50.0))
    plotter.setPen(PenState.DOWN)
    plotter.pause(50)
    plotter.moveTo((100.0,50.0))
    plotter.moveTo((75.0,75.0))
    plotter.setPen(PenState.HIGH_UP)
    plotter.pause(50)
    plotter.moveTo((100.0,50.0))
    plotter.setPen(PenState.DOWN)
    plotter.pause(50)
    plotter.moveTo((100.0,100.0))
    plotter.arcTo((50.0,100.0), (-25.0,-25.0), Rotation.COUNTER_CLOCKWISE)
    plotter.arcTo((50.0,50.0), (25.0,-25.0), Rotation.COUNTER_CLOCKWISE)
    plotter.arcTo((100.0,50.0), (25.0,25.0), Rotation.COUNTER_CLOCKWISE)
    plotter.arcTo((100.0,100.0), (-25.0,25.0), Rotation.COUNTER_CLOCKWISE)
    plotter.setPen(PenState.HIGH_UP)
    plotter.pause(50)
    plotter.setTravelSpeed()
    plotter.moveTo((0.0,0.0))