import logging

import json
import os
import shutil
import tempfile

import filestore
import http_utils

# pylint: disable=no-self-use,unused-argument

class GitlabFilestore(filestore.BaseFilestore):
  """Implementation of BaseFilestore using GitLab.
  Needs a cache to upload and download builds.
  Needs a git repository for corpus and coverage.
  """

  BUILD_PREFIX = 'build-'
  CORPUS_PREFIX = 'corpus-'
  COVERAGE_PREFIX = 'coverage-'
  CRASHES_PREFIX = 'crashes-'

  def __init__(self, config):
    super().__init__(config)
    self.artifacts_dir = self.config.platform_conf.artifacts_dir
    self.cache_dir = self.config.platform_conf.cache_dir
    if self.config.git_store_repo:
      self.git_filestore = filestore.git.GitFilestore(config, None)
    else:
      self.git_filestore = None

  def upload_crashes(self, name, directory):
    """GitLab artifacts implementation of upload_crashes."""
    # Upload crashes as job artifacts.
    if os.listdir(directory):
      dest_dir_artifacts = os.path.join(self.config.project_src_path,
                                        self.artifacts_dir,
                                        self.CRASHES_PREFIX + name)
      logging.info('Uploading artifacts to %s.', dest_dir_artifacts)
      shutil.copytree(directory, dest_dir_artifacts)

  def upload_corpus(self, name, directory, replace=False):
    """GitLab artifacts implementation of upload_corpus."""
    # Use the git filestore if any.
    if self.git_filestore:
      self.git_filestore.upload_corpus(name, directory, replace)
      return
    # Fall back to cache.
    dest_dir_cache = os.path.join(self.config.project_src_path, self.cache_dir,
                                  self.CORPUS_PREFIX + name)
    logging.info('Copying from %s to cache %s.', directory, dest_dir_cache)
    # Remove previous corpus from cache if any.
    shutil.rmtree(dest_dir_cache, ignore_errors=True)
    shutil.copytree(directory, dest_dir_cache, dirs_exist_ok=True)

  def upload_build(self, name, directory):
    """GitLab artifacts implementation of upload_build."""
    # Puts build into the cache.
    dest_dir_cache = os.path.join(self.config.project_src_path, self.cache_dir,
                                  self.BUILD_PREFIX + name)
    logging.info('Copying from %s to cache %s.', directory, dest_dir_cache)
    shutil.copytree(directory, dest_dir_cache, dirs_exist_ok=True)

  def upload_coverage(self, name, directory):
    """GitLab artifacts implementation of upload_coverage."""
    # Use the git filestore.
    if self.git_filestore:
      self.git_filestore.upload_coverage(name, directory)
      return
    # Fall back to cache.
    dest_dir_cache = os.path.join(self.config.project_src_path, self.cache_dir,
                                  self.COVERAGE_PREFIX + name)
    logging.info('Copying from %s to cache %s.', directory, dest_dir_cache)
    shutil.copytree(directory, dest_dir_cache, dirs_exist_ok=True)
    # And also updates coverage reports as artifacts
    # as it should not be too big.
    dest_dir_artifacts = os.path.join(self.config.project_src_path,
                                      self.artifacts_dir,
                                      self.COVERAGE_PREFIX + name)
    logging.info('Uploading artifacts to %s.', dest_dir_artifacts)
    shutil.copytree(directory, dest_dir_artifacts)

  def _copy_from_cache(self, src_dir_cache, dst_directory):
    if not os.path.exists(src_dir_cache):
      logging.info('Cache %s does not exist.', src_dir_cache)
      return False
    logging.info('Copying %s from cache to %s.', src_dir_cache, dst_directory)
    shutil.copytree(src_dir_cache, dst_directory, dirs_exist_ok=True)
    return True

  def download_corpus(self, name, dst_directory):
    """GitLab artifacts implementation of download_corpus."""
    # Use the git filestore if any.
    if self.git_filestore:
      self.git_filestore.download_corpus(name, dst_directory)
      return
    # Fall back to cache.
    src_dir_cache = os.path.join(self.config.project_src_path, self.cache_dir,
                                 self.CORPUS_PREFIX + name)
    self._copy_from_cache(src_dir_cache, dst_directory)

  def download_build(self, name, dst_directory):
    """GitLab artifacts implementation of download_build."""
    # Gets build from the cache.
    src_dir_cache = os.path.join(self.config.project_src_path, self.cache_dir,
                                 self.BUILD_PREFIX + name)
    return self._copy_from_cache(src_dir_cache, dst_directory)

  def download_coverage(self, name, dst_directory):
    """GitLab artifacts implementation of download_coverage."""
    # Use the git filestore if any.
    if self.git_filestore:
      return self.git_filestore.download_coverage(name, dst_directory)
    # Fall back to cache.
    src_dir_cache = os.path.join(self.config.project_src_path, self.cache_dir,
                                 self.COVERAGE_PREFIX + name)
    return self._copy_from_cache(src_dir_cache, dst_directory)


import os
import time

import torch
import argparse
import dataset
from GeoTr import GeoTr
from seg import U2NETP
import torch.nn.init as init
import torch.optim as optim
import torch
from torch.utils.data import DataLoader

def reload_segmodel(model, path=""):
    if not bool(path):
        return model
    else:
        model_dict = model.state_dict()
        pretrained_dict = torch.load(path, map_location='cuda:0')
        print(len(pretrained_dict.keys()))
        pretrained_dict = {k[6:]: v for k, v in pretrained_dict.items() if k[6:] in model_dict}
        print(len(pretrained_dict.keys()))
        model_dict.update(pretrained_dict)
        model.load_state_dict(model_dict)

        return model

def reload_model(model, path=""):
    if not bool(path):
        return model
    else:
        model_dict = model.state_dict()
        pretrained_dict = torch.load(path, map_location='cuda:0')
        print(len(pretrained_dict.keys()))
        pretrained_dict = {k[7:]: v for k, v in pretrained_dict.items() if k[7:] in model_dict}
        print(len(pretrained_dict.keys()))
        model_dict.update(pretrained_dict)
        model.load_state_dict(model_dict)

        return model

def init_weight(model):
    for name, param in model.named_parameters():
        try:
            if 'bias' in name:
                init.constant_(param, 0.0)
            elif 'weight' in name:
                init.kaiming_normal_(param)
        except Exception as e:  # for batchnorm.
            if 'weight' in name:
                param.data.fill_(1)
            continue
    return model

def visualize_data(images, mappings, mask):
    print(images.shape)
    assert False, "stop"

def train(args):

    # segmentation_model = None
    segmentation_model = U2NETP(3, 1)
    reload_segmodel(segmentation_model, args.segmentation_model)
    segmentation_model.to(args.device)

    # GeoTr = None
    GeoTr_model = GeoTr(num_attn_layers=6)
    GeoTr_model = init_weight(GeoTr_model)
    if args.GeoTr_model != "":
         reload_model(GeoTr_model, args.GeoTr_model)
    GeoTr_model = torch.nn.DataParallel(GeoTr_model).to(args.device)
    GeoTr_model.train()

    train_dataset = dataset.GeometricDataset(args.train_data, segmentation_model)
    train_loader = DataLoader(train_dataset, batch_size=args.batch_size)

    criterion = torch.nn.L1Loss()
    optimizer = optim.Adam(GeoTr_model.parameters(), lr=0.0001)

    for epoch in range(50):

        running_loss = 0.0
        for i, data in enumerate(train_loader, 0):
            # get the inputs; data is a list of [inputs, labels]
            start = time.time()
            inputs, mappings = data
            inputs = inputs.to(args.device)
            mappings = mappings.to(args.device)
            # zero the parameter gradients
            optimizer.zero_grad()

            # forward + backward + optimize
            mask, _1,_2,_3,_4,_5,_6 = segmentation_model(inputs.float())
            mask = (mask > 0.5).float()
            # mask_copy = mask.to("cpu")

            # visualize_data()

            inputs = mask * inputs.float()
            outputs = GeoTr_model(inputs)
            outputs = (2 * (outputs / 286.8) - 1) * 0.99
            print()
            loss = criterion(outputs, mappings)
            loss.backward()
            optimizer.step()

            # print statistics
            running_loss += loss.item()
            print("timeeeeeee", time.time()-start)
            if i % 1 == 0:  # print every 1 mini-batches
                print('[%d, %5d] loss: %.3f' %(epoch + 1, i + 1, running_loss))
                running_loss = 0.0
            # torch.save(GeoTr_model.state_dict(), f"{args.saved_path}/{epoch}_{i}.pth")
        torch.save(GeoTr_model.state_dict(), f"{args.saved_path}/{epoch}.pth", _use_new_zipfile_serialization=False)
    print('Finished Training')

if __name__ == '__main__':
    parser = argparse.ArgumentParser()

    parser.add_argument('--train_data', default='./data/training/', help='path to training dataset')
    # parser.add_argument('--valid_data', required=True, help='path to validation dataset')
    parser.add_argument('--segmentation_model', default='./model_pretrained/epoch95_iter3644.pth')
    parser.add_argument('--GeoTr_model', default="", help='path to GeoTr_model model')
    parser.add_argument('--saved_path', default='./images/result')
    parser.add_argument('--batch_size', default=1, help='batch_size')
    parser.add_argument('--device', default="cuda", help='cuda or cpu')

    args = parser.parse_args()

    train(args)


import logging
import time
from sqlalchemy.orm.exc import NoResultFound
from mmredes.com.sda.dashboard.dao.SdaBaseDao import SdaBaseDao

__author__ = 'macbook'
logger = logging.getLogger(__name__)

STATUS_INSTALLED_TEST = 2
STATUS_INSTALLED_PROD = 3

class TicketBoardDao(SdaBaseDao):
    def get_ticket(self, id_ticket, id_status=1):
        try:
            Base = self._Base
            TicketBoard = Base.classes.ticket_board
            return self._session.query(TicketBoard).filter(
                    TicketBoard.id_ticket == id_ticket and TicketBoard.id_status == id_status).one()
        except NoResultFound as e:
            logger.warning(e.message)
            return None

    def get_ticket_code(self, id_ticket):
        Base = self._Base
        TicketBoard = Base.classes.ticket_board
        CatEnvironment = Base.classes.cat_environment
        CatStatusTicket = Base.classes.cat_status_ticket
        return self._session.query(TicketBoard, CatEnvironment, CatStatusTicket).join(CatEnvironment).join(
                CatStatusTicket).filter(
                TicketBoard.id_ticket == id_ticket).one()

    def add(self, dict_ticket_board):
        Base = self._Base
        TicketBoard = Base.classes.ticket_board
        date_requested = time.time()
        id_ticket = dict_ticket_board["id_ticket"]
        id_environment = dict_ticket_board["id_environment"]
        user_request = dict_ticket_board["user_request"]
        ticket_board = TicketBoard(id_ticket=id_ticket, id_environment=id_environment, id_status=1,
                                   user_request=user_request,
                                   date_requested=date_requested)
        self._session.add(ticket_board)

    def update(self, dict_ticket_board, row=None):
        date_requested = time.time()
        user_request = dict_ticket_board["user_request"]
        id_card_ticket = dict_ticket_board["id_card_tracker"]
        id_ticket = dict_ticket_board["id_ticket"]
        id_environment = dict_ticket_board["id_environment"]

        if not row:
            Base = self._Base
            TicketBoard = Base.classes.ticket_board
            row = self._session.query(TicketBoard).filter(
                    TicketBoard.id_ticket == id_ticket, TicketBoard.id_environment == id_environment).one()

        row.date_requested = date_requested
        row.user_request = user_request
        row.id_card_ticket = id_card_ticket

    def update_environment(self, id_ticket, id_environment):
        base = self._Base
        TicketBoard = base.classes.ticket_board
        rows = self._session.query(TicketBoard).filter(TicketBoard.id_ticket == id_ticket).all()

        if len(rows) > 0:
            row = rows[0]
            row.id_environment = id_environment

    def update_status(self, dict_status_ticket):
        date_installed = time.time()
        id_ticket = dict_status_ticket['id_ticket']
        id_status = dict_status_ticket['id_status']
        user_installer = dict_status_ticket['user_installer']
        base = self._Base
        TicketBoard = base.classes.ticket_board
        rows = self._session.query(TicketBoard).filter(TicketBoard.id_ticket == id_ticket).all()

        if len(rows):
            row = rows[0]
            row.id_status = id_status
            row.date_installed = date_installed if id_status == STATUS_INSTALLED_TEST or id_status == STATUS_INSTALLED_PROD else row.date_installed
            row.user_installer = user_installer if id_status == STATUS_INSTALLED_TEST or id_status == STATUS_INSTALLED_PROD else row.user_installer
            return row
        return None


import string
import sqlite3

filename = r"E:\work\pycode\util\text\data\Application_of_Artificial_Intelligence_Technology_.txt"
fout = open(r"out.txt", 'w')
conn = sqlite3.connect("mydict.db")
c = conn.cursor()

def extract_word_from_article(filename):
	fin = open(filename)
	words = []
	count = 0
	for line in fin:
		line = line.replace('-',' ')
		for word in line.split():
			word = word.strip(string.punctuation + string.whitespace)
			word = word.lower()
			if word not in words:
				words.append(word)
				sql = "select chinese from DICT WHERE english = \"{0}\"".format(word)
				cursor = c.execute(sql)
				ret = c.fetchone()
				if ret:
					ret=ret[0]
					count = count + 1
					print("%s : %s"%(word, ret))
					fout.write(word)
					# ret = ret.replace("\"", "\\*")
					fout.write("\n")
					fout.write("\t")
					fout.write(ret)
					fout.write("\n")
	fout.close()
	conn.close()
	print("total number: ", count)

extract_word_from_article(filename)


# helper tool to analyse phase spectra which go into delay solver. It uses the output of delaysolver
# (file avgspectrum.dat). It is intended for debugging only and requires matplotlib and numpy to be installed
# checked in to our code tree for convenience
#
import numpy
import matplotlib
import matplotlib.pyplot as plt

data = numpy.loadtxt('avgspectrum.dat').transpose()

ylabelx = -0.07
plt.rc('legend',fontsize=10)
counter = 1
antenna_names = ['AK06','AK01','AK03','AK15','AK08','AK09']

xaxes = [None,None,None]
yaxes = [None,None,None,None,None]

def plotBaseline(counter,data, ant1, ant2):
   xindex = (counter - 1) % 3
   yindex = (counter - 1) / 3
   ax = plt.subplot(5,3,counter, sharex=xaxes[xindex], sharey=yaxes[yindex])
   if xaxes[xindex] == None:
      xaxes[xindex] = ax
   plt.xlim(-1,305)
   if yaxes[yindex] == None:
      yaxes[yindex] = ax
   plt.ylim(-199,199)
   ax.locator_params(tight=True, nbins=5)
   ax.yaxis.set_label_coords(ylabelx,0.2)
   if xindex == 0:
      if yindex == 2:
         plt.ylabel("   Phase, deg")
         plt.setp(ax.get_yticklabels(),visible=True)
   else:
      plt.setp(ax.get_yticklabels(),visible=False)
   if yindex == 4:
      if xindex == 1:
         plt.xlabel("Channel")
         plt.setp(ax.get_xticklabels(),visible=True)
   else:
      plt.setp(ax.get_xticklabels(),visible=False)
   xdata = [data[2,i] for i in range(len(data[2,:])) if data[0,i] == ant1 and data[1,i] == ant2]
   ydata = [data[3,i] for i in range(len(data[3,:])) if data[0,i] == ant1 and data[1,i] == ant2]
   if len(xdata) == 0 or len(ydata) == 0:
      plt.text(100,-20,"excluded")
   plt.plot(xdata,ydata,"r", label="%s-%s" % (antenna_names[ant1],antenna_names[ant2]))
   plt.text(160,110,"%s-%s" % (antenna_names[ant1],antenna_names[ant2]))
   #plt.legend()

for ant1 in range(6):
   for ant2 in range(ant1,6):
       if ant1 == ant2:
          continue
       plotBaseline(counter, data,ant1,ant2)
       counter = counter + 1

plt.gcf().subplots_adjust(hspace=0,wspace=0)
plt.savefig("out.png",transparent=False)


import asyncio
import re
import aiohttp
import aiomysql
from pyquery import PyQuery

start_url = 'http://www.jobbole.com/'

stopping = False
waiting_urls = []
seen_urls = set()
sem = asyncio.Semaphore(1)

async def fetch(url,session):
    async with sem:
        await asyncio.sleep(1)
        try:
            headers = {"Cookie":"Hm_lvt_42a9b1b1d382d464c04bb82b4916af4d=1581668505; security_session_verify=712d12d780df1ff2c077fafa3137644b; security_session_mid_verify=243f0bdca763b3c9a02fa0a603a7e756; Hm_lpvt_42a9b1b1d382d464c04bb82b4916af4d=1581676108",
}
            async with session.get(url,headers=headers) as resp:
                print('url status: {}'.format(resp.status))
                if resp.status in [200,201]:
                    data = await resp.text()
                    print(data)
                    return data
        except Exception as e:
            print(e)

async def init_urls(url,session):
    html = await fetch(url, session)
    seen_urls.add(url)
    extract_urls(html)

async def article_handler(url,session, pool):
    html = await fetch(url,session)
    seen_urls.add(url)
    extract_urls(html)
    pq = PyQuery(html)
    title = pq('title').text()
    async with pool.acquire() as conn:
        async with conn.cursor() as cur:
            insert_sql = 'insert into article_test(title) values("{}")'.format(title)
            await cur.execute(insert_sql)

def extract_urls(html):
    urls = []
    pq = PyQuery(html)
    for link in pq.items("a"):
        url = link.attr("href")
        if url and url.startswith('http') and url not in seen_urls:
            urls.append(url)
            waiting_urls.append(url)

async def consumer(pool):
    async with aiohttp.ClientSession() as session:
        while not stopping:
            if len(waiting_urls) == 0:
                await asyncio.sleep(0.5)
                continue
            url = waiting_urls.pop()
            print('start get url: {}'.format(url))
            if re.match('http://.*?jobbole.com/\d+/',url):
                if url not in seen_urls:
                    asyncio.ensure_future(article_handler(url,session,pool))
                    await asyncio.sleep(30)
            # else:
            #     if url not in seen_urls:
            #         asyncio.ensure_future(init_urls(url, session))

async def main(loop):
    # 等待mysql连接建立好
    pool = await aiomysql.create_pool(host='localhost',port=3306,user='root',
                                      password='',db='aiomysql_test',loop=loop,
                                      charset='utf8',autocommit=True)
    async with aiohttp.ClientSession() as session:
        html = await fetch(start_url, session)
        seen_urls.add(start_url)
        extract_urls(html)
    asyncio.ensure_future(consumer(pool))

if __name__ == '__main__':
    loop = asyncio.get_event_loop()
    asyncio.ensure_future(main(loop))
    loop.run_forever()


import torch
from torch import nn
from torch.nn import functional as F
import zounds
from config.experiment import Experiment
from fft_basis import morlet_filter_bank
from fft_shift import fft_shift
from modules.matchingpursuit import sparse_code, sparse_code_to_differentiable_key_points, sparse_feature_map
from modules.normalization import max_norm, unit_norm
from modules.overfitraw import OverfitRawAudio
from modules.pos_encode import pos_encode_feature
from modules.softmax import hard_softmax
from modules.sparse import soft_dirac
from time_distance import optimizer
from train.experiment_runner import BaseExperimentRunner
from upsample import ConvUpsample
from util import device
from util.readmedocs import readme

exp = Experiment(
    samplerate=zounds.SR22050(),
    n_samples=2**15,
    weight_init=0.1,
    model_dim=128,
    kernel_size=512)

d_size = 256
kernel_size = 256
sparse_coding_iterations = 16

band = zounds.FrequencyBand(20, 2000)
scale = zounds.MelScale(band, d_size)

d = morlet_filter_bank(exp.samplerate, kernel_size, scale, 0.1, normalize=True).real
d = torch.from_numpy(d).float().to(device)
d = unit_norm(d, dim=-1)

def generate(batch_size):
    total_events = batch_size * sparse_coding_iterations
    amps = torch.zeros(total_events, device=device).uniform_(0.9, 1)
    positions = torch.zeros(total_events, device=device).uniform_(0, 1)
    atom_indices = (torch.zeros(total_events).uniform_(0, 1) * d_size).long()

    output = _inner_generate(
        batch_size, total_events, amps, positions, atom_indices)

    output = max_norm(output)
    return output

def _inner_generate(batch_size, total_events, amps, positions, atom_indices):
    output = torch.zeros(total_events, exp.n_samples, device=device)
    for i in range(total_events):
        index = atom_indices[i]
        pos = positions[i]
        amp = amps[i]
        signal = torch.zeros(exp.n_samples, device=device)
        signal[:kernel_size] = unit_norm(d[index]) * amp
        signal = fft_shift(signal, pos)[..., :exp.n_samples]
        output[i] = signal

    output = output.view(batch_size, sparse_coding_iterations, exp.n_samples)
    output = torch.sum(output, dim=1, keepdim=True)
    return output

class ConvModel(nn.Module):
    def __init__(self):
        super().__init__()
        self.latent = nn.Parameter(torch.zeros(1, 128).uniform_(-1, 1))
        self.net = ConvUpsample(128, 128, 8, exp.n_samples, 'nearest', out_channels=1, batch_norm=True)
        self.apply(lambda x: exp.init_weights(x))

    def forward(self, x):
        result = self.net(self.latent)
        return result

model = ConvModel().to(device)
# model = OverfitRawAudio((1, 1, exp.n_samples), std=0.01, normalize=False).to(device)
optim = optimizer(model, lr=1e-3)

def train(batch, i):
    pass

def sample_loss(a, b):
    """
    This is our baseline.  It works, obviously
    """
    return F.mse_loss(a, b)

def extract_keypoints(a, n_steps=sparse_coding_iterations):
    points = []

    def visit(fm, atom_index, position, atom):
        hard = soft_dirac(fm.view(-1), dim=-1).view(fm.shape)

        time = (hard.sum(dim=0) @ torch.linspace(0, 1, exp.n_samples, device=device)).view(1).repeat(256).view(256)

        points.append(torch.cat([
            atom,
            time
        ])[None, ...])

    fm, _, residual = sparse_code(
        a, d, n_steps=n_steps, device=a.device, flatten=True, visit_key_point=visit, return_residual=True)

    return torch.cat(points, dim=0), residual

def local_keypoint_loss(a, b):
    akp, a_res = extract_keypoints(a, n_steps=sparse_coding_iterations)
    bkp, b_res = extract_keypoints(b, n_steps=sparse_coding_iterations)
    return torch.abs(akp - bkp).sum() + torch.abs(torch.norm(a_res) - torch.norm(b_res)).sum()

def extract_sparse_feature_map(a):
    a, res = sparse_feature_map(a, d, n_steps=sparse_coding_iterations, device=device, return_residual=True)

    return torch.cat([
        a.unfold(-1, 16384, 8192).sum(dim=-1).reshape(-1),
        a.unfold(-1, 8192, 4096).sum(dim=-1).reshape(-1),
        # a.unfold(-1, 2048, 1024).sum(dim=-1).reshape(-1),
        # a.unfold(-1, 512, 256).sum(dim=-1).reshape(-1),
        # a.unfold(-1, 128, 64).sum(dim=-1).reshape(-1),
        # a.reshape(-1),
    ]), res

def sparse_feature_map_loss(a, b):
    """
    This learns some random, out-of-order version of the original
    """
    a, a_res = extract_sparse_feature_map(a)
    b, b_res = extract_sparse_feature_map(b)
    return torch.abs(a - b).sum() + torch.abs(torch.norm(a_res) - torch.norm(b_res)).sum()

def specific_sparse_feature_map_loss(a, b):
    batch = a.shape[0]

    a, a_res = extract_sparse_feature_map(a)
    b, b_res = extract_sparse_feature_map(b)

    a = a.view(batch, -1)
    b = b.view(batch, -1)

    values, indices = torch.topk(a, k=sparse_coding_iterations, dim=-1)

    other = torch.gather(b, dim=-1, index=indices)
    return torch.abs(values - other).sum()

def atom_comparison_loss(recon, target):
    """
    This learns a noisy, but precisely-timed reconstruction
    """
    events, scatter, t_res = sparse_code(
        target, d, n_steps=sparse_coding_iterations, device=device, flatten=True, return_residual=True)

    # _, scatter, r_res = sparse_code(
    #     recon, d, n_steps=sparse_coding_iterations, device=device, flatten=True, return_residual=True)

    loss = 0
    for ai, j, p, a in events:
        start = p
        stop = min(exp.n_samples, p + kernel_size)
        size = stop - start
        r = recon[j, :, start: start + size]
        at = a[:, :size]
        loss = loss + torch.abs(r - at).sum()
    return loss #+ torch.abs(torch.norm(t_res) - torch.norm(r_res)).sum()

def experiment_loss(a, b):
    # return sample_loss(a, b)
    # return sparse_feature_map_loss(a, b)
    # return local_keypoint_loss(a, b)
    return atom_comparison_loss(a, b)
    # return specific_sparse_feature_map_loss(a, b)

@readme
class DenseToSparse(BaseExperimentRunner):
    def __init__(self, stream, port=None):
        super().__init__(stream, train, exp, port=port)

    def run(self):
        item = generate(1)
        self.real = item

        i = 0
        while True:
            optim.zero_grad()
            recon = model.forward(None)
            self.fake = recon
            loss = experiment_loss(recon, item)
            loss.backward()
            optim.step()
            print(i, loss.item())

            self.after_training_iteration(loss)
            i += 1


import sys
from Parser.ParserFile import ParserClass
from interpreter import convertor
from interpreter.errors import ErrorHandler
from Parser.Node import Node
from interpreter.errors import ErrType
from interpreter import errors
from interpreter.VariableClass import Variable
from interpreter.VariableClass import ArrVariable

cells = {
    ' ': 'empty',  # четные
    'E': 'exit',
    '#': 'wall'
}

back_cells = {
    'empty': ' ',
    'exit': 'E',
    'wall': '#'
}
'''
example:
 ▲ ▼ ᩔ 🤖 🏰
    ᩔᩔᩔᩔᩔᩔᩔᩔ
    ᩔ▼▲▼▲▼▲ᩔ
    ᩔ▲▼▲🤖▲▼ᩔ
    ᩔ▼🏰▼▲▼▲ᩔ
    ᩔ▲▼▲▼▲▼ᩔ
    ᩔᩔᩔᩔᩔᩔᩔᩔᩔ
'''

class Cell:
    def __init__(self, type):
        self.type = type

    def __repr__(self):
        return f'{self.type}'

class Robot:
    def __init__(self, x, y, map, map_int):
        self.map_int = map_int
        self.x = x
        self.y = y
        self.scan_right = True
        self.map = map
        self.counte = 1

    def __repr__(self):
        if self.scan_right:
            return f'x = {self.x}, y = {self.y}; side scan = right\n'
        else:
            return f'x = {self.x}, y = {self.y}; side scan = left\n'

    def show(self):
        print(self)
        for i in range(len(self.map)):
            for j in range(len(self.map[0])):
                if i == self.y and j == self.x:
                    print('🤖', end='')
                else:
                    if back_cells[self.map[i][j].type] == ' ':
                        if (i+j) % 2 == 0:
                            print('▼', end='')
                        else:
                            print('▲', end='')
                    elif back_cells[self.map[i][j].type] == '#':
                        print('ᩔ', end='')
                    else:
                        print('🏰', end='')
            print()
        print()

    def move(self, direction):
        if direction == 'move':
            if (self.x+self.y) % 2 == 0:
                return self.down()
            else:
                return self.up()
        elif direction == 'left':
            return self.left()
        elif direction == 'right':
            return self.right()
        else:
            return Variable(0, 'int')

    def up(self):
        if self.y <= 0:
            return Variable(0, 'int')
        if (self.x + self.y) % 2 == 0:
            return Variable(0, 'int')
        else:
            if self.map[self.y - 1][self.x].type != 'wall':
                if self.map_int[self.y][self.x] == 0:
                    self.map_int[self.y][self.x] = self.counte
                    self.counte += 1
                self.y -= 1
                return Variable(1, 'int')
        return Variable(0, 'int')

    def down(self):
        if self.y >= len(self.map):
            return Variable(0, 'int')
        if (self.x + self.y) % 2 == 0:
            if self.map[self.y + 1][self.x].type != 'wall':
                if self.map_int[self.y][self.x] == 0:
                    self.map_int[self.y][self.x] = self.counte
                    self.counte += 1
                self.y += 1
                return Variable(-1, 'int')
        return Variable(0, 'int')

    def left(self):
        if self.x <= 0:
            return Variable(0, 'int')
        else:
            if self.map[self.y][self.x - 1].type != 'wall':
                if self.map_int[self.y][self.x] == 0:
                    self.map_int[self.y][self.x] = self.counte
                    self.counte += 1
                self.x -= 1
                return Variable(-1, 'int')
        return Variable(0, 'int')

    def right(self):
        if self.x >= len(self.map[0]):
            return Variable(0, 'int')
        else:
            if self.map[self.y][self.x + 1].type != 'wall':
                if self.map_int[self.y][self.x] == 0:
                    self.map_int[self.y][self.x] = self.counte
                    self.counte += 1
                self.x += 1
                return Variable(1, 'int')
        return Variable(0, 'int')

    def exit(self):
        if self.map[self.y][self.x].type == 'exit':
            return True
        return False

    def lms(self):
        dist = 1
        radius = 5
        if self.scan_right:
            self.scan_right = False
            while self.map[self.y][self.x+dist].type == 'empty':
                if radius == 0:
                    return Variable(0, 'int')
                dist += 1
                radius -= 1
            if self.map[self.y][self.x+dist].type == 'exit':
                return Variable(-dist, 'int')
            elif self.map[self.y][self.x+dist].type == 'wall':
                return Variable(dist, 'int')
        else:  # left side
            self.scan_right = True
            while self.map[self.y][self.x-dist].type == 'empty':
                if radius == 0:
                    return Variable(0, 'int')
                dist += 1
                radius -= 1
            if self.map[self.y][self.x-dist].type == 'exit':
                return Variable(dist, 'int')
            elif self.map[self.y][self.x-dist].type == 'wall':
                return Variable(-dist, 'int')

class VariableList:
    def __init__(self):
        self.pre = None
        self.variables = {}
        self.next = None

class InterpreterClass:
    def __init__(self, program, robot=None):
        self.parser = ParserClass()
        self.program = program
        self.tree = []
        self.robot = robot
        self.functions = None
        self.has_errors = False
        self.errorHandler = ErrorHandler()
        self.err_type = ErrType
        self.curFunc = None
        self.variables = VariableList()
        self.exit = Variable('false', 'bool', 'exit')
        self.variables.variables['exit'] = self.exit
        self.steps = 0

    def interprete(self):
        self.tree, self.functions, self.has_errors = self.parser.parse(self.program)
        if not self.has_errors:
            if 'work' not in self.functions.keys():
                self.errorHandler.raise_err(ErrType.MissingWorkError.value)
            else:
                self.curFunc = 'work'
                self.nodeHandle(self.functions['work'].children[1])
                return_value = self.expression(self.functions['work'].children[2]).value
                return return_value
        else:
            self.errorHandler.raise_err(self.err_type.SyntaxError.value)

    def nodeHandle(self, node):
        node_type = node.type
        try:
            if node_type == None:
                return
            elif node_type == 'program':
                self.nodeHandle(node.children[0])
            elif node_type == 'statement list':
                self.nodeHandle(node.children[0])
                self.nodeHandle(node.children[1])
            elif node_type == 'End of program':
                return
            elif node_type == 'declaration':
                var_type = node.children[0]
                var_var = node.children[1]
                try:
                    if var_type.data != 'vector of':
                        self.declaration(var_type, var_var, False)
                    else:
                        self.declaration(var_type, var_var, True)
                except Exception as err:
                    print("Something wrong in declaration")
                    raise err
            elif node_type == 'statement group':
                self.nodeHandle(node.children[0])
            elif node_type == 'assign':
                variable1 = node.children[0]
                variable2 = node.children[1]
                try:
                    self.assign(variable1, variable2)
                except Exception as err:
                    print(f"Something wrong in Assign")
                    raise err
            elif node_type == 'expression':
                self.nodeHandle(node.children[1])
            elif node_type == 'expr comma':
                pass
            elif node_type == 'if then else':
                try:
                    self.if_statement(node.children[0], node.children[1], node.children[2])
                except Exception as err:
                    self.returnEnv()
                    print('Something wrong in IF statement')
                    raise err
            elif node_type == 'call function':
                try:
                    self.callfunc(node)
                except Exception as err:

                    self.returnEnv()
                    print('Something wrong in Call function')
                    raise err
            elif node_type == 'do while':
                try:
                    self.dowhile_statement(node)
                except Exception as err:
                    self.returnEnv()
                    print('Something wrong in DO WHILE statement')
                    raise err
            elif node.type == 'Robot command':
                if self.robot is None:
                    raise errors.EmptyRobotError
                if node.data == 'lms':
                    return self.robot.lms()
                else:
                    if self.robot.exit():
                        self.exit = True
                        exit = self.get_variable(Node('', data='exit'))
                        exit.set_value('true')
                        return Variable('true', 'bool', 'exit')
                    self.steps += 1
                    self.robot.show()
                    return self.robot.move(node.data)
            else:
                raise errors.UnexpectedError(node)
        except errors.MyKeyError as err:
            self.errorHandler.raise_err(self.err_type.KeyError.value, [err.args[0]])
        except errors.UnexpectedError as err:
            self.errorHandler.raise_err(self.err_type.UnexpectedError.value, [err.args[0]])
        except errors.TypeError as err:
            self.errorHandler.raise_err(self.err_type.TypeError.value, [err.args[0], err.args[1], err.args[2]])
        except errors.KeyErrorFunc as err:
            self.errorHandler.raise_err(self.err_type.KeyErrorFunc.value, [err.args[0], err.args[1]])
        except errors.RepeatingParam as err:
            self.errorHandler.raise_err(self.err_type.RepeatingParam.value, [err.args[0], err.args[1]])
        except errors.MyRuntimeError as err:
            self.errorHandler.raise_err((self.err_type.RuntimeError.value))
            return False
        except errors.SizeOfError as err:
            self.errorHandler.raise_err(self.err_type.SizeOfError.value, [err.args[0], err.args[1]])
        except errors.FunctionVarlistError as err:
            self.errorHandler.raise_err(self.err_type.FunctionVarlistError.value, [err.args[0], err.args[1]])
        except errors.WrongArrayDeclaration as err:
            self.errorHandler.raise_err(self.err_type.WrongArrayDeclaration.value, [err.args[0]])
        except errors.EmptyRobotError as err:
            self.errorHandler.raise_err(self.err_type.EmptyRobotError.value)

    def get_indexes_arr(self, node, sizelist):
        for child in node.children:
            if child.type == 'double index':
                self.get_indexes_arr(child, sizelist)
            elif child.type == 'squared bracket':
                value = self.expression(child.children[0])
                value = convertor.convert_type(value, 'int').value
                sizelist.append(value)

    def get_values(self, node, values):
        if node.type == 'expr comma':
            for child in node.children:
                if child.type != 'expr comma':
                    values.append(self.expression(child).value)
                elif child.type == 'expr comma':
                    self.get_values(child, values)
        elif node.type == 'arr comma':
            for child in node.children:
                self.get_values(child, values)
        elif node.type == 'arr count':
            self.get_values(node.children[0], values)
        else:
            errors.UnexpectedError(node)

    def get_sizes(self, node):
        pass

    def declaration(self, type, var, flag_arr):
        if not flag_arr:
            if var.type == 'variable':
                variable = Variable(0, type.data, var.data)  # (value, type, name)
                self.variables.variables[var.data] = variable
            elif var.type == 'assign':
                variable = Variable(0, type.data, var.children[0].data)  # (value, type, name)
                self.variables.variables[var.children[0].data] = variable
                self.assign(var.children[0], var.children[1])
        else:
            scope_count, arr_type = self.arr_count(type, 0)
            if var.type == 'array variable':
                name = var.data
                sizelist = []
                self.get_indexes_arr(var, sizelist)
                if len(sizelist) != scope_count:
                    raise errors.WrongArrayDeclaration(var)
                variable = ArrVariable(sizelist, arr_type, name=name)
                self.variables.variables[name] = variable
            elif var.type == 'assign array':
                name = var.children[0].data
                values = []
                sizelist = []
                self.get_indexes_arr(var.children[0], sizelist)
                self.get_values(var.children[1].children[0], values)
                variable = ArrVariable(sizelist, arr_type, values, name=name)
                self.variables.variables[name] = variable
            elif var.type == 'assign':
                name = var.children[0].data
                if var.children[1].type == 'call function':
                    variable = self.callfunc(var.children[1])
                    if isinstance(variable, ArrVariable):
                        variable.name = name
                        self.variables.variables[name] = variable
                    else:
                        raise errors.WrongArrayDeclaration(var)

    def expression(self, node):
        if node.type == 'variable':
            return self.get_variable(node)
        elif node.type == 'array variable':
            return self.get_Variable_arr(self.get_variable(node), node)
        elif node.type == 'math expression':
            return self.math_expression(node)
        elif node.type == 'digit':
            return Variable(node.data, 'int')
        elif node.type == 'bool':
            return Variable(node.data, 'bool')
        elif node.type == 'call function':
            return self.callfunc(node)
        elif node.type == 'size of':
            return Variable(self.sizeof(node.children[0]), 'int')
        elif node.type == 'brackets expression':
            return self.expression(node.children[0])
        elif node.type == 'Robot command':
            return self.nodeHandle(node)
        else:
            raise errors.UnexpectedError(node)

    def arr_count(self, node, count):
        if node.type == 'type':
            return count, node.data
        else:
            count += 1
            return self.arr_count(node.children[0], count)

    def sizeof(self, node):
        if node.type == 'variable':
            data = self.get_variable(node).type
        else:
            data = node.data
        if data == 'int':
            return 4
        elif data == 'bool':
            return 1
        elif data == 'short':
            return 2
        else:
            raise errors.SizeOfError(data, node)

    def createNewEnv(self):
        current = VariableList()
        current.pre = self.variables
        self.variables.next = current
        self.variables = self.variables.next

    def returnEnv(self):
        self.variables = self.variables.pre

    def varlist(self, node, varlist):
        variable_1 = node.children[0]
        if variable_1.type == 'variable':
            variable_1 = self.get_variable(variable_1)
            if isinstance(variable_1, Variable):
                variable_1 = variable_1.value
            elif isinstance(variable_1, ArrVariable):
                arr = []
                for value in variable_1.values:
                    arr.append(value.value)
                variable_1 = arr
        elif variable_1.type == 'size of':
            variable_1 = self.sizeof(variable_1)
        else:
            variable_1 = variable_1.data
        varlist.append(variable_1)
        if len(node.children) > 1:
            variable_2 = node.children[1]
            if variable_2.type == 'double varlist':
                self.varlist(variable_2, varlist)
                return
            elif variable_2.type == 'variable':
                variable_2 = self.get_variable(variable_2.data).value
            elif variable_2.type == 'size of':
                variable_2 = self.sizeof(variable_2)
            else:
                variable_2 = variable_2.data
            varlist.append(variable_2)

    def callfunc(self, node):
        func_name = node.data
        try:
            func = self.functions[func_name]
        except KeyError as err:
            self.createNewEnv()
            raise errors.KeyErrorFunc(err, node)
        varlist = []
        if node.children[0].type != 'none variable':
            if node.children[0].type == 'double varlist':
                self.varlist(node.children[0], varlist)
            else:
                self.varlist(node, varlist)
        self.createNewEnv()
        self.comma_variables(func.children[0], varlist, node)
        self.nodeHandle(func.children[1])
        return_value = func.children[2]
        value = self.expression(return_value)
        self.returnEnv()
        return value

    def if_statement(self, expression, then_statement, else_statement):
        self.createNewEnv()
        condition = self.expression(expression)
        condition = convertor.convert_type(condition, 'bool').value
        if condition == 'true':
            self.nodeHandle(then_statement)
        elif condition == 'false':
            self.nodeHandle(else_statement)
        self.returnEnv()

    def dowhile_statement(self, node):
        body = node.children[0]
        expression = node.children[1]
        self.createNewEnv()
        counter = 0
        condition = 'true'
        while condition == 'true':
            counter += 1
            self.nodeHandle(body)
            condition = self.expression(expression)
            condition = convertor.convert_type(condition, 'bool').value
            if counter > 1000:
                raise errors.MyRuntimeError()
        self.returnEnv()

    def get_variable(self, node):
        name = node.data
        current = self.variables
        variable = None
        while True:
            try:
                variable = current.variables[name]
                break
            except KeyError:
                current = current.pre
                if current == None:
                    break
        if not(variable):
            raise errors.MyKeyError(node)
        return variable

    def get_Variable_arr(self, variable, node):
        indexes = []
        self.get_indexes_arr(node, indexes)
        if len(indexes) == 1:
            return variable.values[indexes[0]]
        if len(indexes) == 2:
            return variable.values[indexes[0]][indexes[1]]

    def assign(self, name, value):
        # self.errorHandler.set_node(name)
        # raise errors.MyKeyError
        variable = self.get_variable(name)
        if isinstance(variable, Variable):
            cur_value = self.expression(value)
            cur_value = convertor.convert_type(cur_value, variable.type).value
            variable.set_value(cur_value)
        elif isinstance(variable, ArrVariable):
            cur_value = self.expression(value)
            if not isinstance(cur_value, ArrVariable):
                cur_value = convertor.convert_type(cur_value, variable.type).value
            var = self.get_Variable_arr(variable, name)
            var.set_value(cur_value)

    def math_expression(self, value):
        flag = False
        if value.data == '==' or value.data == 'first larger' or value.data == 'first smaller' or value.data == 'second larger' or value.data == 'second smaller':
            value_1 = self.expression(value.children[0])
            value_2 = self.expression(value.children[1])
            if value.data == '==':
                if value_1.type != value_2.type:
                    raise errors.TypeError(value_1, value_2, value)
                if value_1.value == value_2.value:
                    return Variable('true', 'bool')
                else:
                    return Variable('false', 'bool')
            elif value.data == 'first larger' or value.data == 'second smaller':
                value_1 = convertor.convert_type(value_1, value_2.type)
                if value_1.value > value_2.value:
                    return Variable('true', 'bool')
                else:
                    return Variable('false', 'bool')
            elif value.data == 'first smaller' or value.data == 'second larger':
                value_1 = convertor.convert_type(value_1, value_2.type)
                if value_1.value < value_2.value:
                    return Variable('true', 'bool')
                else:
                    return Variable('false', 'bool')
        if value.data == 'add' or value.data == 'sub':
            if value.children[1].type == 'math expression':
                # if second child is math expression we create new math expression
                # where first element is sum of value.children[0].data and value.children[1].children[0].data
                # and second is value.children[1].children[1].data
                value_1 = self.expression(value.children[1].children[0])
                value_1 = convertor.convert_type(value_1, 'int').value
                flag = True
                # value_1 = self.math_expression(value.children[1])
            elif value.children[1].type == 'variable':
                variable = self.get_variable(value.children[1])
                value_1 = variable.value
                if variable.type == 'bool':
                    value_1 = convertor.convert_type(Variable(value_1, 'bool'), 'int').value
            elif value.children[1].type == 'array variable':
                return self.get_Variable_arr(self.get_variable(value.children[1]), value.children[1].children[0])
            elif value.children[1].type == 'brackets expression':
                value_1 = self.expression(value.children[1])
                value_1 = convertor.convert_type(value_1, 'int').value
            elif value.children[1].type == 'call function':
                value_1 = self.expression(value.children[1])
                value_1 = convertor.convert_type(value_1, 'int').value
            else:
                value_1 = value.children[1].data
                if value.children[1].type == 'bool':
                    value_1 = convertor.convert_type(Variable(value_1, 'bool'), 'int').value
                    # convert_type returns tuple (value, type) so we take convert_type[0]
            value_2 = self.expression(value.children[0])
            value_2 = convertor.convert_type(value_2, 'int').value
            if value.data == 'add':
                result = value_2 + value_1
            elif value.data == 'sub':
                result = value_2 - value_1
            else:
                raise errors.UnexpectedError(value)
            if flag:
                new_node = Node('math expression',
                     data=value.children[1].data,
                     children=[Node('digit', data=result, lineno=value.children[1].lineno),
                               value.children[1].children[1]],
                     lineno=value.children[1].lineno)
                result = self.math_expression(new_node).value
            return Variable(result, 'int')
        elif value.data == 'and' or value.data == 'or' or value.data == 'not and' or value.data == 'not or':
            if value.children[1].type == 'math expression':
                # if second child is math expression we create new math expression
                # where first element is sum of value.children[0].data and value.children[1].children[0].data
                # and second is value.children[1].children[1].data
                value_1 = self.expression(value.children[1].children[0])
                value_1 = convertor.convert_type(value_1, 'bool').value
                flag = True
                # value_1 = self.math_expression(value.children[1])
            elif value.children[1].type == 'variable':
                variable = self.get_variable(value.children[1])
                value_1 = convertor.convert_type(variable, 'bool').value
            elif value.children[1].type == 'array variable':
                value_1 = self.get_Variable_arr(self.get_variable(value.children[1]), value.children[1].children[0])
                value_1 = convertor.convert_type(value_1, 'bool').value
            elif value.children[1].type == 'brackets expression':
                value_1 = self.expression(value.children[1])
                value_1 = convertor.convert_type(value_1, 'bool').value
            else:
                value_1 = value.children[1].data
                if value.children[1].type == 'digit':
                    value_1 = convertor.convert_type(Variable(value_1, 'int'), 'bool').value
                    # convert_type returns tuple (value, type) so we take convert_type[0]
            value_2 = self.expression(value.children[0])
            value_2 = convertor.convert_type(value_2, 'bool').value
            if value.data == 'and':
                if value_1 == 'false' or value_2 == 'false':
                    result = 'false'
                elif value_1 == 'undefined' or value_2 == 'undefined':
                    result = 'undefined'
                else:
                    result = 'true'
            elif value.data == 'or':
                if value_1 == 'true' or value_2 == 'true':
                    result = 'true'
                elif value_1 == 'undefined' or value_2 == 'undefined':
                    result = 'undefined'
                else:
                    result = 'false'
            elif value.data == 'not and':
                if value_1 == 'false' or value_2 == 'false':
                    result = 'true'
                elif value_1 == 'undefined' or value_2 == 'undefined':
                    result = 'undefined'
                else:
                    result = 'false'
            elif value.data == 'not or':
                if value_1 == 'true' or value_2 == 'true':
                    result = 'false'
                elif value_1 == 'undefined' or value_2 == 'undefined':
                    result = 'undefined'
                else:
                    result = 'true'
            else:
                raise errors.UnexpectedError(value)
            if flag:
                new_node = Node('math expression',
                                data=value.children[1].data,
                                children=[Node('bool', data=result, lineno=value.children[1].lineno),
                                          value.children[1].children[1]],
                                lineno=value.children[1].lineno)
                result = self.math_expression(new_node).value
            return Variable(result, 'bool')

    def comma_variables(self, node, varlist, name):
        if node.type == 'none variable':
            if len(varlist) == 0:
                return
            else:
                raise errors.FunctionVarlistError(name.data, name)
        if node.type == 'variable':
            current = Node('placeholder', 'placeholder',[node], lineno=node.lineno)
            node = current
        for child in node.children:
            if child.type == 'variable':
                try:
                    self.variables.variables[child.data]
                    raise errors.RepeatingParam(child.data, child)
                except KeyError:
                    if len(varlist) != 0:
                        value = varlist.pop(0)
                    else:
                        raise errors.FunctionVarlistError(name.data, name)
                    if child.children[0].type == 'Vector of':
                        name = child.data
                        size = len(value)
                        sizelist = []
                        scope_count, arr_type = self.arr_count(child.children[0], 0)
                        variable = ArrVariable([size], arr_type, value, name=name)
                    else:
                        variable = Variable(value, child.children[0].data, child.data)
                    self.variables.variables[child.data] = variable
            elif child.type == 'comma variables':
                self.comma_variables(child, varlist, name)
            else:
                raise errors.UnexpectedError(node)
        if len(varlist) != 0:
            raise errors.FunctionVarlistError(name.data, name)

def create_robot():
    with open('map_big.txt') as file:
        text = file.read()
    text = text.split('\n')
    robot_info = text.pop(0).split(' ')
    map_size = text.pop(0).split(' ')
    x = int(robot_info[0])
    y = int(robot_info[1])
    map_numbers = [0] * int(map_size[0])
    map = ['empty'] * int(map_size[0])

    for i in range(int(map_size[0])):
        map[i] = ['empty']*int(map_size[1])
        map_numbers[i] = [0] * int(map_size[1])
    index = 0
    while len(text) > 0:
        line = list(text.pop(0))
        new_line = []
        for i, char in enumerate(line):
            new_line.append(Cell(cells[char]))
            if char == '#':
                map_numbers[index][i] = '#'
            if char == 'E':
                map_numbers[index][i] = 'E'
        map[index] = new_line
        index += 1
    return Robot(x, y, map, map_numbers)

def print_map_int(map):
    for i in range(len(map)):
        for j in range(len(map[0])):
            print(map[i][j], end='\t')
        print()
    print()

if __name__ == '__main__':
    f = open('robot_righthand.txt', 'r')
    data = f.read()
    robot = create_robot()
    my_interpreter = InterpreterClass(data, robot)
    result = my_interpreter.interprete()
    if result == 'true':
        print(f"Robot find exit;\nNumber of steps:{my_interpreter.steps}")
        print_map_int(my_interpreter.robot.map_int)
    else:
        print(f"Robot can't find exit;\nNumber of steps:{my_interpreter.steps}")


import asyncio
from bleak import BleakScanner
from bleak import BleakClient
import asyncio
import platform
import binascii
import sys
import time

i=0
t1=0
t2=0
rr=0
class ritmo:
	band=False
	def prueba(self):
		print("Hola")
	def bandera(self):
		return self.band
	def notification_handler(self, sender, data):
		global i,t1,t2,rr
		datos=data.hex()
		hr=int(datos[2]+datos[3],16)
		print("==============")
		print("HR: ",hr)
		rr = int(60000/hr)
		print("RR: ",rr)
		print("==============")
		f=open("data.txt","w")
		f.write(str(hr)+"\n"+str(rr))
		f.close()

	async def run(self, address, loop, char_uid):
		try:
			self.band=False
			async with BleakClient(address, loop=loop) as client:
				x=await client.is_connected()
				print(f"X es {x}")
				await client.start_notify(char_uid, self.notification_handler)
				i=1
				while i>0:
					await asyncio.sleep(1)
					i+=1
				await client.stop_notify(char_uid)
				self.band=True

		except:
			self.band=False


import torch
import torch.nn as nn
import torch.nn.functional as F
from ssd import SSD

class Bottleneck(nn.Module):
    def __init__(self, in_planes):
        super(Bottleneck, self).__init__()
        self.conv1 = nn.Conv2d(in_planes, in_planes//4, kernel_size=3, stride=1, padding=1, bias=False)
        self.bn1 = nn.BatchNorm2d(in_planes//4)
        self.conv2 = nn.Conv2d(in_planes//4, in_planes//4, kernel_size=1, stride=1, padding=0, bias=False)
        self.bn2 = nn.BatchNorm2d(in_planes//4)

    def forward(self, x):
        out = F.relu(self.bn1(self.conv1(x)))
        out = F.relu(self.bn2(self.conv2(out)))
        return out

class ActivatedBatchNorm(nn.Module):
    def __init__(self, num_features, activation='relu', slope=0.01, **kwargs):
        super().__init__()
        self.bn = nn.BatchNorm2d(num_features, **kwargs)
        if activation == 'relu':
            self.act = nn.ReLU(inplace=True)
        elif activation == 'leaky_relu':
            self.act = nn.LeakyReLU(negative_slope=slope, inplace=True)
        elif activation == 'elu':
            self.act = nn.ELU(inplace=True)
        else:
            self.act = None

    def forward(self, x):
        x = self.bn(x)
        if self.act:
            x = self.act(x)
        return x

class SCSEBlock(nn.Module):
    def __init__(self, channel, reduction=16):
        super().__init__()
        self.avg_pool = nn.AdaptiveAvgPool2d(1)
        self.channel_excitation = nn.Sequential(nn.Linear(channel, int(channel // reduction)),
                                                nn.ReLU(inplace=True),
                                                nn.Linear(int(channel // reduction), channel))
        self.spatial_se = nn.Conv2d(channel, 1, kernel_size=1,
                                    stride=1, padding=0, bias=False)

    def forward(self, x):
        bahs, chs, _, _ = x.size()

        # Returns a new tensor with the same data as the self tensor but of a different size.
        chn_se = self.avg_pool(x).view(bahs, chs)
        chn_se = torch.sigmoid(self.channel_excitation(chn_se).view(bahs, chs, 1, 1))
        chn_se = torch.mul(x, chn_se)

        spa_se = torch.sigmoid(self.spatial_se(x))
        spa_se = torch.mul(x, spa_se)
        return x + chn_se + spa_se

class DecoderSCSE(nn.Module):
    def __init__(self, in_channels, middle_channels, out_channels):
        super().__init__()
        self.conv1 = nn.Conv2d(in_channels, middle_channels, kernel_size=3, padding=1)
        self.ABN = ActivatedBatchNorm(middle_channels)
        self.SCSEBlock = SCSEBlock(middle_channels)
        self.deconv = nn.ConvTranspose2d(middle_channels, out_channels, kernel_size=4, stride=2, padding=1)

    def forward(self, *args):
        x = torch.cat(args, 1)
        x = self.conv1(x)
        x = self.ABN(x)
        x = self.SCSEBlock(x)
        x = self.deconv(x)
        return x

class Segmentation_Decoder(nn.Module):

    def __init__(self, num_classes=21):
        super(Segmentation_Decoder, self).__init__()
        self.tp_conv1 = nn.Sequential(nn.ConvTranspose2d(64, 32, 3, 2, 1, 1),
                                      nn.BatchNorm2d(32),
                                      nn.ReLU(inplace=True),)
        self.conv2 = nn.Sequential(nn.Conv2d(64, 32, 3, 1, 1),
                                nn.BatchNorm2d(32),
                                nn.ReLU(inplace=True),)
        self.tp_conv2 = nn.ConvTranspose2d(32, num_classes, 3, 1, 1)
        self.lsm = nn.LogSoftmax(dim=1)

        self.bottle_neck1 = Bottleneck(256)
        self.bottle_neck2 = Bottleneck(512)
        self.bottle_neck3 = Bottleneck(1024)

        self.center = DecoderSCSE(512, 256, 256)
        self.decoder5 = DecoderSCSE(768, 512, 256)
        self.decoder4 = DecoderSCSE(512, 256, 128)
        self.decoder3 = DecoderSCSE(256, 128, 64)
        self.decoder2 = DecoderSCSE(128, 64, 64)
        self.decoder1 = DecoderSCSE(128, 64, 64)
        self.pool = nn.MaxPool2d(2, 2)

    def forward(self, encoder):
        x, e1, e2, e3, e4 = encoder[0], self.bottle_neck1(encoder[1]), self.bottle_neck2(encoder[2]), self.bottle_neck3(encoder[3]), encoder[4]
        c = self.center(self.pool(e4))
        d4 = self.decoder5(c, e4)
        d3 = self.decoder4(d4, e3)
        d2 = self.decoder3(d3, e2)
        d1 = self.decoder2(d2, e1)
        x = F.upsample(x, d1.size()[2:], mode='bilinear', align_corners=True)
        d1 = self.decoder1(d1, x)

        y = self.conv2(d1)
        y = self.tp_conv2(y)
        y_seg = self.lsm(y)
        return y_seg

class AP_MTL(nn.Module):
    def __init__(self, num_classes=8, size=1024, ssd_conf=None):
        super(AP_MTL, self).__init__()
        #detection
        self.detection = SSD(num_classes=num_classes, size=size, ssd_conf=ssd_conf)
        # segmentation decoder
        self.seg_decoder = Segmentation_Decoder(num_classes=num_classes)

    def forward(self, x):
        out_bbox, out_encoder = self.detection(x)
        out_seg = self.seg_decoder(out_encoder)
        return out_seg, out_bbox


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

import logging, jieba, os, re
from tqdm import tqdm

def split_sentence(document):
    '''
    this is to split a doc into sentences
    分句之前先把\n，\r，\r\n，\u3000直接洗掉即可
    分句的规则有：1.碰到句号，问号，感叹号，分号还有空格就分，并把标点符号加到前句
    '''
    document = document.strip().replace("\r", "").replace("\n", "").replace("\\n", "").replace("\u3000", "").replace("\\", "")
    # 以结尾标点切分
    split = re.split(r"([？?。!！；…])", document)
    # 标点符号加回句子
    split.append("")
    split = [i+j for (i, j) in zip(split[0::2], split[1::2])]
    return split

def wash_and_split(input_text):
    '''
    主要的数据预处理函数
    输入一篇文章string
    输出分词分句并清洗后的句子列表
    '''

    sentences = split_sentence(input_text)
    cut = list(map(jieba.lcut, sentences))

    stop_words = get_stopwords(path='data/stopwords.txt')

    def remove_stopwords_for_each_sentence(list_of_words):
        return [word for word in list_of_words if word not in stop_words and word != ' ']

    cut = list(map(remove_stopwords_for_each_sentence, cut))

    return [i for i in cut if i not in [[], ["…"]]]

def parse_wiki(file_path, output_path):
    # 过滤掉<doc>
    regex_str = "[^<doc.*>$]|[^</doc>$]"
    with open(file_path, "r", encoding="utf-8") as f_read:
        # 写文件
        with open(output_path, "w+", encoding="utf-8") as f_write:
            content_line = f_read.readline()
            # 获取停用词表
            stopwords = get_stopwords()
            # 定义一个字符串变量，表示一篇文章的分词结果
            article_contents = ""
            while content_line:
                match_obj = re.match(regex_str, content_line)
                content_line = content_line.strip("\n")
                if len(content_line) > 0:
                    if match_obj:
                        words = jieba.cut(content_line, cut_all=False)
                        for word in words:
                            if word not in stopwords:
                                article_contents += word + " "
                    else:
                        if len(article_contents) > 0:
                            f_write.write(article_contents + "\n")
                            article_contents = ""
                content_line = f_read.readline()

def generate_corpus():
    wiki_path = "data/AA"
    save_path = "data/output"
    for i in range(3):
        file_path = os.path.join(wiki_path, str("wiki_0%s" % str(i)))
        parse_wiki(file_path, os.path.join(save_path, "wiki_corpus0%s" % str(i)))
    print(f"Wiki Data Saved into: {save_path}")

def merge_corpus():
    '''
    合并分词后的文件
    '''
    output_path = open("data/output/wiki_sum", "w", encoding="utf-8")
    input_path = "data/output"
    for i in tqdm(range(3)):
        file_path = os.path.join(input_path, str("wiki_corpus0%s" % str(i)))
        with open(file_path, "r", encoding="utf-8") as f_read:
            line = f_read.readline()
            while line:
                output_path.writelines(line)
                line = f_read.readline()
    output_path.close()

def get_stopwords(path='data/stopwords.txt'):
    # 加载停用词表
    stopwords_set = []
    with open(path, 'r', encoding="utf-8") as stopwords:
        for stopword in stopwords:
            if stopword != '':
                stopwords_set.append(stopword.strip("\n").strip())
    stopwords_set = set(stopwords_set)
    return stopwords_set

def remove_stopwords(words):
    return [word.strip() for word in words if word not in get_stopwords() and word != " "]

if __name__ == "__main__":
    # generate_corpus()
    # merge_corpus()

    with open('data/new_data_to_train_word2vec.txt', 'w', encoding='utf-8') as f_write:
        with open("data/output/wiki_sum", "r", encoding="utf-8") as wiki_data:
            print('开始写入wiki数据')
            count1 = 1
            line = wiki_data.readline()
            while line:
                line = line.strip().split(' ')
                line = remove_stopwords(line)
                # a = list(map(remove_stopwords, [line.strip().split(' ') for line in wiki_data]))
                f_write.write(" ".join(line) + '\n')
                if count1 % 100 == 0:
                   print(f'完成{count1}条数据')
                line = wiki_data.readline()
                count1 += 1
            print('完成写入所有数据')


import glob
import h5py
import pathlib
import numpy as np
import matplotlib.pyplot as plt

def plot_training_curves(path: pathlib.Path, show_err: bool = False,
                         std_err: bool = False, show_grid: bool = False,
                         show: bool = True) -> plt.Figure:
    """
        Plot all the hft5 files found within the given path.

        path (pathlib.Path) this must be a folder path.
        show_err (bool): whether to show or not the error bars on each point
        std_err (bool): whether to use the standard deviation as the error
        show_grid (bool): Show grid on the plot.

        Return:
            the created fig
    """
    assert path.is_dir(), "Path must be a folder location"

    fig = plt.figure()
    for fpath in glob.glob(str(path / "*.h5"), recursive=True):
        plot_single_model_training_curves(fpath, fig)

    assert len(fig.get_axes()) > 0, "Nothing was drawn onto the graph! Verify the folder used for this script!"

    plt.title("Neural Teleportation Model Training Comparison")
    plt.xlabel("Epochs")
    plt.ylabel("Accuracy")
    if show_grid:
        plt.grid()
    plt.legend()

    if show:
        plt.show()

    return fig

def plot_single_model_training_curves(path: pathlib.Path, figure: plt.Figure = None, show_err: bool = False,
                                      std_err: bool = False, show_grid: bool = False, show: bool = True):
    """
        Plot a single hft5 file containing the training curves of the models comparison

        path (pathlib.Path) this must be a folder path.
        figure (pyplot.Figure), this is if the user want to put the printed result to an already existing pyplot figure.
        show_err (bool): whether to show or not the error bars on each point
        std_err (bool): whether to use the standard deviation as the error
        show_grid (bool): Show grid on the plot.

        Return:
            the created figure or the passed figure.
    """
    splits = pathlib.Path(path).name.split('_')

    # This preliminary check to not read files that does not fit
    # the format from teleportation_train_comparison script
    if len(splits) != 8:
        return
    name = splits[6]

    fig = figure
    if not figure:
        fig = plt.figure()
        plt.title("Neural Teleportation Model Training Comparison")
        plt.xlabel("Epochs")
        plt.ylabel("Accuracy")
        plt.legend()
        if show_grid:
            plt.grid()
        if show:
            plt.show()
    else:
        fig = plt.figure(figure.number)

    f = h5py.File(path, "r")
    for k in f.keys():
        data = np.array(f[k])
        assert data.ndim == 2, "The open file does not contain the right data format or is "
        x = np.arange(data.shape[1])
        mean = data.mean(axis=0)
        if std_err:
            err = data.std(axis=0)
        else:
            err = -np.amin(data, axis=0) + mean, np.amax(data, axis=0) - mean
        label = name + "_" + k
        if show_err:
            plt.errorbar(x, mean, yerr=err, fmt='-o', ms=3, label=label, capsize=5.0)
        else:
            plt.plot(mean, '-o', ms=3, label=label, )
    f.close()

    return fig


"""============================ MorphVertices.py ==================================
This script updates the coordinates of all vertices in the specified mesh to match
the specified weighted average of two or more meshes.

06/21/2017 - Writen by APM
"""

import bpy
from mathutils import Vector, Matrix

#==================== Calculate vertex destinations
Proportion = 0.5                            # Proportion mix between mesh A and mesh B

#==================== Get vertex data
obj     = bpy.data.objects['Cube1']         # Handle to mesh object
mesh    = obj.data                          # mesh data
vert    = mesh.vertices[0]                  # Vertex 1

mat_world = obj.matrix_world
vec      = Vector((0.0, 0.0, 0.1))
mat_edit = mat_world.inverted() * Matrix.Translate(vec) * mat_world
vert.co  = mat_edit * vert.co


from django.conf import settings
from django.conf.urls.static import static
from django.contrib import admin
from django.urls import path, include
from shop.api.views import *
from rest_framework import routers
from .yasg import urlpatterns as doc_urls

router = routers.DefaultRouter()
router.register(r'users', UserProfileViewSet, basename='users')
router.register(r'products', ProductViewSet, basename='products')
router.register(r'categories', CategoryProductViewSet, basename='categories')
router.register(r'reviews', ReviewViewSet, basename='reviews')
router.register(r'exhibited_goods', ExhibitedGoodsViewSet, basename='exhibited')
router.register(r'sold_goods', SoldGoodsViewSet, basename='sold')
router.register(r'buy_goods', BuyGoodsViewSet, basename='buy')

urlpatterns = [
    path('admin/', admin.site.urls),
    path('api/', include(router.urls)),
    path('api-auth/', include('rest_framework.urls')),
    path('ckeditor/', include('ckeditor_uploader.urls')),
    path('auth/', include('djoser.urls')),
    path('auth/', include('djoser.urls.authtoken')),
    path('auth/', include('djoser.urls.jwt')),

]

urlpatterns+=doc_urls

if settings.DEBUG:
    urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)


#!/usr/bin/env python3

from bs4 import BeautifulSoup
from tqdm import tqdm
records = []

with open("../data/desc2019", "r") as handle:
    record = ""
    for line in handle:
        if line.startswith("<DescriptorRecord"):
            if record:
                records.append(record)
                record = ""
            record = "".join([record, line])
        else:
            record = "".join([record, line])

# Discard first 2 records which have document info
records = records[2:]

desc_records = []
desc_uis = []
desc_names = []
tree_num_lists = []
min_depths = []
distinct_tree_posits = []

# Extract data. tree_num_lists is currently commented out but this might be
# useful to keep in the future
for rec in tqdm(records):
    soup = BeautifulSoup(rec)

    tree_nums = []

    desc_uis.append(soup.descriptorui.string)
    desc_names.append(soup.descriptorname.find('string').string)
    if soup.treenumberlist is not None:
        for tree_num in soup.treenumberlist.find_all('treenumber'):
            tree_nums.append(tree_num.string)
        min_depths.append(len(min([t.split(".") for t in tree_nums], key=len)))
    else:
        min_depths.append(0)
    distinct_tree_posits.append(len(tree_nums))
    tree_num_lists.append(tree_nums)

with open("../data/mesh_data.tab", "w") as out:
    for index in range(len(records)):
        out.write("".join([desc_uis[index], "\t", desc_names[index], "\t"]))
        out.write("".join([str(min_depths[index]), "\t"]))
        out.write("".join([str(distinct_tree_posits[index]), "\t"]))
        out.write(",".join(tree_num_lists[index]))
        out.write("\n")


from django.shortcuts import render, HttpResponse, redirect
from time import gmtime, strftime

# Create your views here.
def index(request):
    context = {
    "time":strftime("%b %m, %Y", gmtime()),
    "timeofday":strftime("%I %M %S", gmtime())
    }
    return render(request, "time_display/index.html", context)


import os
import io
import jieba
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.naive_bayes import MultinomialNB
from sklearn import metrics
import warnings

warnings.filterwarnings('ignore')

train_texts = []
train_labels = []
test_texts = []
test_labels = []

labels = os.listdir(r'N:/text_classification/train')
#print(labels)

# 加载数据
for label in labels:
    train_path = 'N:/text_classification/train/' + label + '/'
    test_path = 'N:/text_classification/test/' + label + '/'
    for file in os.listdir(train_path):
        with open(train_path+file,encoding='gb18030',errors='ignore') as f:
            train_texts.append(f.readline())
            train_labels.append(label)

    for file in os.listdir(test_path):
        with open(test_path+file,encoding='gb18030',errors='ignore') as f:
            test_texts.append(f.readline())
            test_labels.append(label)

# 加载停用词
with open('N:/text_classification/stop/stopword.txt', 'rb') as f:
    STOP_WORDS = [line.strip() for line in f.readlines()]

# 使用jieba分词并计算单词权重
tf = TfidfVectorizer(tokenizer=jieba.cut, stop_words=STOP_WORDS, max_df=0.5)
train_features = tf.fit_transform(train_texts)
#print(train_features.shape)

# 生成朴素贝叶斯分类器
clf = MultinomialNB(alpha=0.001).fit(train_features, train_labels)

# 使用分类器做预测并计算准确率
test_tf = TfidfVectorizer(tokenizer=jieba.cut,stop_words=STOP_WORDS, max_df=0.5, vocabulary=tf.vocabulary_)
test_features=test_tf.fit_transform(test_texts)

#print(test_features.shape)
predicted_labels=clf.predict(test_features)
print('准确率为：',metrics.accuracy_score(test_labels, predicted_labels))


import numpy as np
import torch
import torch.utils.data
import torchvision.transforms as transforms

def one_hot(y, num_classes=10):
    """Change label at end of vectors of size 7 to be a one-hot vector"""
    N = len(y)
    y_where = y[:, :6]
    y_label = y[:, 6].astype(np.int64)
    y_label_one_hot = np.zeros((N, num_classes))
    y_label_one_hot[np.arange(N), y_label] = 1
    return np.concatenate((y_where, y_label_one_hot), axis=1)

def load_data(outfile="mnist_sequence3_sample_8distortions9x9"):
    X_train = np.load(outfile + "/X_train.npy")
    y_train = np.load(outfile + "/y_train.npy")
    X_valid = np.load(outfile + "/X_valid.npy")
    y_valid = np.load(outfile + "/y_valid.npy")
    X_test = np.load(outfile + "/X_test.npy")
    y_test = np.load(outfile + "/y_test.npy")
    #y_train, y_valid, y_test = one_hot(y_train), one_hot(y_valid), one_hot(y_test)
    return X_train, y_train, X_valid, y_valid, X_test, y_test

def prepare_datasets(X_train, y_train, X_valid, y_valid, X_test, y_test,
                     train_batch_size, val_batch_size, test_batch_size):

    X_train, y_train = torch.Tensor(X_train).reshape(-1, 1, 100, 100), torch.Tensor(y_train)
    X_valid, y_valid = torch.Tensor(X_valid).reshape(-1, 1, 100, 100), torch.Tensor(y_valid)
    X_test, y_test = torch.Tensor(X_test).reshape(-1, 1, 100, 100), torch.Tensor(y_test)

    # Normalization

    mu = X_train.mean()
    std = X_train.std()
    X_train = (X_train - mu) / std
    X_valid = (X_valid - mu) / std
    X_test = (X_test - mu) / std

    train_set = torch.utils.data.TensorDataset(X_train, y_train)
    train_loader = torch.utils.data.DataLoader(train_set, batch_size=train_batch_size,
                                               shuffle=True, num_workers=0)
    val_set = torch.utils.data.TensorDataset(X_valid, y_valid)
    val_loader = torch.utils.data.DataLoader(val_set, batch_size=val_batch_size,
                                             shuffle=False, num_workers=0)
    test_set = torch.utils.data.TensorDataset(X_test, y_test)
    test_loader = torch.utils.data.DataLoader(test_set,
                                              batch_size=test_batch_size,
                                              shuffle=False, num_workers=0)
    return train_loader, val_loader, test_loader


#!/usr/bin/env python
# -*- coding: utf-8 -*-

from django import forms
from django.utils.text import slugify

from bricksa.apps.projects.models import Project

class ProjectForm(forms.ModelForm):
    class Meta:
        fields = '__all__'
        model = Project

    def clean_video(self):
        cleaned_data = super(ProjectForm, self).clean()
        video = cleaned_data.get('video')
        if 'embed' in video:
            video_embedded = video
        elif video == '':
            video_embedded = ''
        else:
            try:
                arg = video.split('=')[1]
                video_embedded = 'https://www.youtube.com/embed/%s' % arg
            except:
                raise forms.ValidationError('Inserte un link de la forma https://www.youtube.com/watch?v=IVx6ZlksMJw')
        return video_embedded

    def save(self, commit=True):
        m = super(ProjectForm, self).save(commit=False)
        m.slug = slugify(m.name)
        if commit:
            m.save()
        return m


import os, argparse
import numpy as np
import torch
import torch.nn as nn
import warnings
from beat_tracking.helper_functions import *
from beat_tracking.data_loading import *
from beat_tracking.modules import AudioModule, MyMadmomModule_pr

import pytorch_lightning as pl

from pytorch_lightning.loggers import WandbLogger

warnings.filterwarnings("ignore")

# TRAINING LOOP

def train(args):
    data = Audio_and_pr_DataModule(args)

    dataset = args.dataset
    mode = args.mode
    epochs = args.epochs
    stepsize = args.stepsize
    full_train = args.full_train

    #train_loader = data.train_dataloader()
    #for i,elem in enumerate(train_loader):
    #    print(i,elem[0],elem[3].shape)

    if mode == "audio":
        model = AudioModule(args)
    elif mode == "pianorolls":
        model = MyMadmomModule_pr(args)
    #model = MyMadmomModule.load_from_checkpoint(args=args, checkpoint_path="/home/thomass/beat_tracking/ISMIR-training-all-pretty_midi-step_size_20-only_beats/9pjqc8mu/checkpoints/epoch=20-val_loss=0.09-val_f1=0.00.ckpt")

    wandb_logger = WandbLogger(project="ISMIR-training-"+dataset+"-"+mode+"-full_train_"+str(full_train))

    trainer = pl.Trainer(
        default_root_dir="pl_checkpoints/",
        logger=wandb_logger,
        log_every_n_steps=50,
        reload_dataloaders_every_n_epochs=True,
        max_epochs=int(epochs),
        #gpus=gpus,
        accelerator='gpu',
        devices=[0]
    )

    trainer.fit(model, data)

if __name__ == '__main__':

    parser = argparse.ArgumentParser(description='Train ISMIR.')

    parser.add_argument('--dataset', type=str, help='Which dataset?')
    parser.add_argument('--mode', type=str, default="audio", help='Audio or pianorolls?')
    parser.add_argument('--epochs', type=str, help='How many epochs?')
    parser.add_argument('--stepsize',  type=int, help='Adjust learning rate after ... epochs')
    parser.add_argument('--full_train', default=False, action='store_true', help='Train on the whole dataset')

    args = parser.parse_args()

    train(args)


import wx
from raw_gui import RawSpamFrame
import string
from client import SteveBannonClient
import telegram, config

class SpamFrame(RawSpamFrame):

    def __init__(self, parent=None):
        super().__init__(parent=parent)

        self.Bind(wx.EVT_BUTTON, self.on_send, self.m_button)

        self.post_init()

    def post_init(self):

        config.client.connect()
        if config.client.is_user_authorized():
            self.init_telegram_data()
        else:
            dlg = wx.TextEntryDialog(None, 'Digite seu número de telefone', 'Autenticação')
            if dlg.ShowModal() == wx.ID_OK:
                config.client.send_code_request(dlg.GetValue())
                dlg = wx.TextEntryDialog(None, 'Digite o código recebido', 'Autenticação')
                if dlg.ShowModal() == wx.ID_OK:
                    config.client.sign_in(code=dlg.GetValue())
                    self.post_init()

    def init_telegram_data(self):
        self.groups = telegram.get_all_dialogs()
        print(self.groups)

        for i, group in enumerate(self.groups):
            self.m_list.InsertItem(i, group.name)

    def on_send(self, event):
        selected = [self.m_list.GetFirstSelected()]

        while True:
            next = self.m_list.GetNextSelected(selected[-1])
            if next == -1:
                break
            selected.append(next)

        selected = [self.groups[i] for i in selected]

        typed = self.m_textinput.GetValue()
        times = self.m_spinCtrl1.GetValue()

        for chat in selected:
            telegram.send_message(chat, typed, loop=times)

        self.m_textinput.SetValue('')

        dlg = wx.MessageDialog(self,
                               f'Texto digitado: {typed}',
                               'Mensagem enviada',
                               wx.OK
                               )
        dlg.ShowModal()
        dlg.Destroy()

app = wx.App()

frame = SpamFrame(parent=None)
frame.Show()

app.MainLoop()


import argparse
import bisect
import glob
import logging
import os
import re
import sys

import natsort
import numpy as np
import pandas as pd
import segmentation_models_pytorch as smp
import tifffile
import torch
from torch.utils.data import DataLoader

from .dataio.dataset_input import DataProcessing
from .model.model import SegmentationModel as Network
from .utils.utils import (
    get_experiment_id,
    get_files_list,
    get_seg_mask,
    make_dir,
    save_fig_outputs,
    sorted_alphanumeric,
)

from ..config import load_config, Config

def predict(config: Config) -> str:
    logging.basicConfig(
        format="%(asctime)s %(levelname)s %(message)s",
        level=logging.INFO,
        stream=sys.stdout,
    )

    use_cuda = torch.cuda.is_available()
    device = torch.device("cuda" if use_cuda else "cpu")

    # Create experiment directories
    make_new = False
    timestamp = get_experiment_id(
        make_new,
        config.experiment_dirs.dir_id,
        config.files.data_dir,
    )
    experiment_path = os.path.join(config.files.data_dir, "model_outputs", timestamp)
    model_dir = experiment_path + "/" + config.experiment_dirs.model_dir
    test_output_dir = experiment_path + "/" + config.experiment_dirs.test_output_dir
    make_dir(test_output_dir)

    # Set up the model
    logging.info("Initialising model")

    atlas_exprs = pd.read_csv(config.files.fp_ref, index_col=0)
    n_genes = atlas_exprs.shape[1] - 3
    print("Number of genes: %d" % n_genes)

    if config.model_params.name != "custom":
        model = smp.Unet(
            encoder_name=config.model_params.name,
            encoder_weights=None,
            in_channels=n_genes,
            classes=2,
        )
    else:
        model = Network(n_channels=n_genes)

    model = model.to(device)

    # Get list of model files
    if config.testing_params.test_epoch < 0:
        saved_model_paths, _ = get_files_list(model_dir, [".pth"])
        saved_model_paths = sorted_alphanumeric(saved_model_paths)
        saved_model_names = [
            (os.path.basename(x)).split(".")[0] for x in saved_model_paths
        ]
        saved_model_epochs = [x.split("_")[1] for x in saved_model_names]
        saved_model_steps = [x.split("_")[-1] for x in saved_model_names]
        if config.testing_params.test_epoch is None:
            saved_model_epochs = np.array(saved_model_epochs, dtype="int")
            saved_model_steps = np.array(saved_model_steps, dtype="int")
        elif config.testing_params.test_epoch == -1:
            saved_model_epochs = np.array(saved_model_epochs[-1], dtype="int")
            saved_model_epochs = [saved_model_epochs]
            saved_model_steps = np.array(saved_model_steps[-1], dtype="int")
            saved_model_steps = [saved_model_steps]
    else:
        saved_model_epochs = [config.testing_params.test_epoch]
        saved_model_steps = [config.testing_params.test_step]

    shifts = [0, int(config.model_params.patch_size / 2)]

    for shift_patches in shifts:
        # Dataloader
        logging.info("Preparing data")
        test_dataset = DataProcessing(
            config,
            isTraining=False,
            shift_patches=shift_patches,
        )
        test_loader = DataLoader(
            dataset=test_dataset, batch_size=1, shuffle=False, num_workers=0
        )

        n_test_examples = len(test_loader)
        logging.info("Total number of patches: %d" % n_test_examples)

        logging.info("Begin prediction")

        for epoch_idx, (test_epoch, test_step) in enumerate(
            zip(saved_model_epochs, saved_model_steps)
        ):
            current_dir = (
                test_output_dir
                + "/"
                + "epoch_"
                + str(test_epoch)
                + "_step_"
                + str(test_step)
            )
            make_dir(current_dir)

            # Restore model
            load_path = model_dir + "/epoch_%d_step_%d.pth" % (test_epoch, test_step)
            checkpoint = torch.load(load_path)
            model.load_state_dict(checkpoint["model_state_dict"])
            epoch = checkpoint["epoch"]
            assert epoch == test_epoch
            print("Predict using " + load_path)

            model = model.eval()

            for batch_idx, (
                batch_x313,
                batch_n,
                batch_sa,
                batch_pos,
                batch_neg,
                coords_h1,
                coords_w1,
                nucl_aug,
                expr_aug_sum,
                whole_h,
                whole_w,
                expr_fp,
            ) in enumerate(test_loader):
                if batch_idx == 0:
                    whole_seg = np.zeros((whole_h, whole_w), dtype=np.uint32)

                # Permute channels axis to batch axis
                batch_x313 = batch_x313[0, :, :, :, :].permute(3, 2, 0, 1)
                batch_sa = batch_sa.permute(3, 0, 1, 2)
                batch_n = batch_n.permute(3, 0, 1, 2)

                if batch_x313.shape[0] == 0:
                    seg_patch = np.zeros(
                        (
                            config.model_params.patch_size,
                            config.model_params.patch_size,
                        ),
                        dtype=np.uint32,
                    )

                else:
                    # Transfer to GPU
                    batch_x313 = batch_x313.to(device)
                    batch_sa = batch_sa.to(device)
                    batch_n = batch_n.to(device)

                    # Forward pass
                    seg_pred = model(batch_x313)

                    coords_h1 = coords_h1.detach().cpu().squeeze().numpy()
                    coords_w1 = coords_w1.detach().cpu().squeeze().numpy()
                    sample_seg = seg_pred.detach().cpu().numpy()
                    sample_n = nucl_aug.detach().cpu().numpy()
                    sample_sa = batch_sa.detach().cpu().numpy()
                    sample_expr = expr_aug_sum.detach().cpu().numpy()
                    patch_fp = current_dir + "/%d_%d.png" % (coords_h1, coords_w1)

                    if (batch_idx % config.training_params.sample_freq) == 0:
                        save_fig_outputs(
                            sample_seg, sample_n, sample_sa, sample_expr, patch_fp
                        )

                    seg_patch = get_seg_mask(sample_seg, sample_n)

                # seg_patch_fp = current_dir + '/' + "%d_%d.tif" %(coords_h1, coords_w1)
                # tifffile.imwrite(seg_patch_fp, seg_patch.astype(np.uint32), photometric='minisblack')

                whole_seg[
                    coords_h1 : coords_h1 + config.model_params.patch_size,
                    coords_w1 : coords_w1 + config.model_params.patch_size,
                ] = seg_patch.copy()

            seg_fp = (
                test_output_dir
                + "/"
                + "epoch_%d_step_%d_seg_shift%d.tif"
                % (test_epoch, test_step, shift_patches)
            )

            tifffile.imwrite(
                seg_fp, whole_seg.astype(np.uint32), photometric="minisblack"
            )

    logging.info("Finished")

    return test_output_dir

def gap_coords(coords, patcsize):
    """If gap larger than patcsize -> remove all corresponding locations"""
    starts_diff = np.diff(coords)
    gap_idx = np.where(starts_diff > patcsize)[0] + 1
    # print(gap_idx.shape)
    gap_end = [coords[x] for x in gap_idx]
    gap_start = [coords[bisect.bisect(coords, x - 1) - 1] for x in gap_end]
    # print(gap_start, gap_end)
    gap = []
    for gs, ge in zip(gap_start, gap_end):
        gap.extend(list(range(gs, ge)))
    # print(len(gap))
    return gap

def fill_grid(config: Config, dir_id: str):
    """
    Combine predictions from unshifted and shifted patches to remove
    border effects
    """

    print("Combining predictions")

    patch_size = config.model_params.patch_size
    shift = int(patch_size / 2)

    expr_fp = (
        config.files.data_dir
        + "/"
        + config.files.dir_out_maps
        + "/"
        + config.files.dir_patches
        + str(config.model_params.patch_size)
        + "x"
        + str(config.model_params.patch_size)
        + "_shift_"
        + str(shift)
    )
    expr_fp_ext = ".hdf5"

    dir_id = os.path.join(
        config.files.data_dir,
        "model_outputs",
        dir_id,
        config.experiment_dirs.test_output_dir,
    )

    pred_fp = "%s/epoch_%d_step_%d_seg_shift0.tif" % (
        dir_id,
        config.testing_params.test_epoch,
        config.testing_params.test_step,
    )
    pred_fp_sf = "%s/epoch_%d_step_%d_seg_shift%d.tif" % (
        dir_id,
        config.testing_params.test_epoch,
        config.testing_params.test_step,
        shift,
    )

    output_fp = dir_id + "/" + os.path.basename(pred_fp).replace("_seg_shift0", "")

    pred = tifffile.imread(pred_fp)
    pred_sf = tifffile.imread(pred_fp_sf)

    fp_patches_sf = glob.glob(expr_fp + "/*" + expr_fp_ext)
    fp_patches_sf = natsort.natsorted(fp_patches_sf)

    coords_patches = [re.findall(r"\d+", os.path.basename(x)) for x in fp_patches_sf]
    coords_h1 = [int(x[0]) for x in coords_patches]
    coords_w1 = [int(x[1]) for x in coords_patches]

    # Fill along grid
    h_starts_wide = []
    w_starts_wide = []

    # Middle section of shifted patches
    for i in range(int(patch_size * 0.35), int(patch_size * 0.65)):
        h_starts_wide.extend([x + i for x in coords_h1])
        w_starts_wide.extend([x + i for x in coords_w1])

    # Gap larger than patch_size -> remove all corresponding locations
    h_gap = gap_coords(coords_h1, patch_size)
    w_gap = gap_coords(coords_w1, patch_size)

    fill = np.zeros(pred.shape)
    fill[h_starts_wide, :] = 1
    fill[:, w_starts_wide] = 1

    # Gaps
    fill[h_gap, :] = 0
    fill[:, w_gap] = 0

    tifffile.imwrite(
        dir_id + "/" + "fill.tif", fill.astype(np.uint16), photometric="minisblack"
    )

    result = np.zeros(pred.shape, dtype=np.uint32)
    result = np.where(fill > 0, pred_sf, pred)

    tifffile.imwrite(output_fp, result.astype(np.uint32), photometric="minisblack")

if __name__ == "__main__":
    parser = argparse.ArgumentParser()

    parser.add_argument("--config_dir", type=str, help="path to config")

    args = parser.parse_args()
    config = load_config(args.config_dir)

    test_output_dir = predict(config)

    fill_grid(config, test_output_dir)


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
creates Fig 13
creates spatial plot for irrigation effects on LAI and NPP
Be careful with the unit of NPP, we use gC( of freshmatter)/m2month1
"""

import os

import matplotlib.pyplot as plt
import xarray as xr

from analysis_functions.functions_correcting_time import correct_timedim_mfiles
from analysis_functions.functions_plotting import plot_rotvar, rotated_pole
from analysis_functions.functions_reading_files import read_mfiles

# In[]: select experiment, year and month

year = 2017
month = 0
exp_number_irri = "067016"
exp_number_noirri = "067015"

# In[]: read files

# paths to the data
data_path = "../data"

# background map: irrifrac
remo_dir = str(exp_number_noirri) + "/irrifrac/"
remo_files = "e" + str(exp_number_noirri) + "e_c743_201706.nc"
remo_tfile = xr.open_dataset(str(data_path) + "/" + str(remo_dir) + str(remo_files))
irrifrac = remo_tfile.IRRIFRAC[0]

# In[]: read mfiles
mds_irri = read_mfiles(data_path, exp_number_irri, 2017, 0)
mds_noirri = read_mfiles(data_path, exp_number_noirri, 2017, 0)

mdsirri_extended = correct_timedim_mfiles(xr.merge([mds_irri, irrifrac]))
mdsnoirri_extended = correct_timedim_mfiles(xr.merge([mds_noirri, irrifrac]))

# In[]: define plot directory

dir_working = os.getcwd()
# creates dir in parent directory
dir_out = os.path.join(os.pardir, "Figures")
if not os.path.exists(dir_out):
    os.makedirs(dir_out)
print("Output directory is: ", dir_out)

# In[]: LAI and NPP
# LAI

irrilimit = 0.0

var = "ALAI_PFI"

# monthly for irrigated fraction
data_irri = mdsirri_extended.where(mdsirri_extended.IRRIFRAC > irrilimit)[var][
    :, 0, :, :
].mean(dim=["rlon", "rlat"], skipna=True)
data_noirri = mdsnoirri_extended.where(mdsnoirri_extended.IRRIFRAC > irrilimit)[var][
    :, 0, :, :
].mean(dim=["rlon", "rlat"], skipna=True)
laimonthdiff = data_irri - data_noirri

#
month_list = ["April", "May", "June"]
mdsirri_AMJ = mdsirri_extended.sel(time=mdsirri_extended.time.dt.month.isin([4, 5, 6]))
mdsnoirri_AMJ = mdsnoirri_extended.sel(
    time=mdsnoirri_extended.time.dt.month.isin([4, 5, 6])
)
laispatialdiff = mdsirri_AMJ.ALAI_PFI[:, 0, :, :] - mdsnoirri_AMJ.ALAI_PFI[:, 0, :, :]

# NPP
irrilimit = 0.0
var = "ANPP_ACI"

# monthly for irrigated fraction

data_irri = (
    mdsirri_extended.where(mdsirri_extended.IRRIFRAC > irrilimit)[var][:, 0, :, :].mean(
        dim=["rlon", "rlat"], skipna=True
    )
) * (30 * 24)
data_noirri = (
    mdsnoirri_extended.where(mdsnoirri_extended.IRRIFRAC > irrilimit)[var][
        :, 0, :, :
    ].mean(dim=["rlon", "rlat"], skipna=True)
) * (30 * 24)
nppmonthdiff = data_irri - data_noirri

month_list = ["April", "May", "June"]
mdsirri_AMJ = mdsirri_extended.sel(time=mdsirri_extended.time.dt.month.isin([4, 5, 6]))
mdsnoirri_AMJ = mdsnoirri_extended.sel(
    time=mdsnoirri_extended.time.dt.month.isin([4, 5, 6])
)
nppspatialdiff = ((mdsirri_AMJ[var][:, 0, :, :]) * (30 * 24)) - (
    (mdsnoirri_AMJ[var][:, 0, :, :]) * (30 * 24)
)

# spatial plot
fig = plt.figure(figsize=(18, 10))

params = {
    "legend.fontsize": 18,
    "axes.labelsize": 18,
    "axes.titlesize": 18,
    "xtick.labelsize": 16,
    "ytick.labelsize": 16,
}
plt.rcParams.update(params)

cax1 = fig.add_axes([0.152, 0.56, 0.175, 0.02])
cax2 = fig.add_axes([0.425, 0.54, 0.175, 0.02])
cax3 = fig.add_axes([0.7, 0.54, 0.175, 0.02])
cbaraxes_list = [cax1, cax2, cax3]

for i in range(len(laispatialdiff)):
    ax1 = fig.add_subplot(2, 3, 1 + i, projection=rotated_pole)
    levels = [-1, -0.8, -0.6, -0.4, -0.2, 0.2, 0.4, 0.6, 0.8, 1]
    ticks = [
        -0.8,
        -0.4,
        0.4,
        0.8,
    ]
    rotplot = plot_rotvar(
        fig,
        laispatialdiff[i],
        ax1,
        cbaraxes_list[i],
        label="LAI [Δm$^2$m$^{-2}$]",
        unit="[-]",
        cmap="RdBu_r",
        levels=levels,
        extend_scale="both",
        ticks=ticks,
        cbar_orient="horizontal",
    )
    ax1.set_title(month_list[i])

# NPP
# diff spatial
cax4 = fig.add_axes([0.152, 0.07, 0.175, 0.02])
cax5 = fig.add_axes([0.425, 0.07, 0.175, 0.02])
cax6 = fig.add_axes([0.7, 0.07, 0.175, 0.02])
cbaraxes_list = [cax4, cax5, cax6]

for i in range(len(nppspatialdiff)):
    ax4 = fig.add_subplot(2, 3, 3 + (1 + i), projection=rotated_pole)
    levels = [
        -1200,
        -1100,
        -1000,
        -900,
        -800,
        -700,
        -600,
        -500,
        -400,
        -300,
        -200,
        -100,
        100,
        200,
        300,
        400,
        500,
        600,
        700,
        800,
        900,
        1000,
        1100,
        1200,
    ]
    ticks = [-1200, -400, 400, 1200]
    rotplot = plot_rotvar(
        fig,
        nppspatialdiff[i],
        ax4,
        cbaraxes_list[i],
        label="NPP [ΔgCm$^{-2}$month$^{-1}$]",
        unit="[ΔgCm$^{-2}$month$^{-1}$]",
        cmap="RdBu_r",
        levels=levels,
        extend_scale="both",
        ticks=ticks,
        cbar_orient="horizontal",
    )
    ax4.set_title(" ")
hspace = 0.7
fig.subplots_adjust(hspace=hspace)
plt.savefig(str(dir_out) + "/Fig13.png", dpi=300, bbox_inches="tight")
plt.show()


from collections import deque

class RecentCounter:

    def __init__(self):
        self.request = deque()

    def ping(self, t: int) -> int:

        self.request.append(t)

        while len(self.request) > 0 and self.request[0] < t - 3000:
            self.request.popleft()

        return len(self.request)

recentCounter = RecentCounter()

# Your RecentCounter object will be instantiated and called as such:
# obj = RecentCounter()
# param_1 = obj.ping(t)

recentCounter.ping(1)#;     // requests = [1], range is [-2999,1], return 1
recentCounter.ping(100)#;   // requests = [1, 100], range is [-2900,100], return 2
recentCounter.ping(3001)#;  // requests = [1, 100, 3001], range is [1,3001], return 3
print(recentCounter.ping(3002))#;  // requests = [1, 100, 3001, 3002], range is [2,3002],