import json

import numpy as np
from flask_pymongo.wrappers import Collection
import bson
import pickle

import sys
sys.path.insert(0, "../artemis/src/compare")
sys.path.insert(0, "../color_histogram")

import image_extraction as ex
import color_histogram as clr
import os
import connection as cn
from PIL import Image
import cv2

def read_data():
    data_list = np.genfromtxt("data.csv", delimiter=",", dtype='U')
    return data_list

def put_to_db(collection: Collection, data_list):
    db_list = []
    for i in range(len(data_list)):
        print(i)
        data = data_list[i]
        image_str = data[1]
        image_id = image_str[7:]
        img = Image.open(f"./sub1_out/image{image_id}sub1.png")
        gray = img.convert('LA')
        norm = gray.convert('RGB')
        img2 = np.asarray(img)
        vector = ex.extract_resnet(img)
        vector_colorless = ex.extract_resnet(norm)
        colour, perc = clr.get_image_color_features(img2)
        price_with_dollars = data[2]
        price = price_with_dollars[1:]
        db_list.append({
            "Pid": str(image_id),
            "Pname" : data[0],
            "Pprice" : price,
            "Pdescription": data[3],
            "resnet50": bson.binary.Binary(pickle.dumps(vector)),
            "renet50-colorless": bson.binary.Binary(pickle.dumps(vector_colorless)),
            "color": bson.binary.Binary(pickle.dumps(colour)),
            "percentage": bson.binary.Binary(pickle.dumps(perc))
        })
    collection.insert_many(db_list)

def crop_image(img):
    h = 750
    w = 1244
    x = 42
    y = 450
    #img = cv2.imread(img)
    return img[y:y + h, x:x + w]

def read_product_data_and_put_to_db():
    data_list = read_data()
    db = cn.connectToDB()
    products_collection: Collection = db.get_collection('Products')
    put_to_db(products_collection, data_list)

read_product_data_and_put_to_db()


import numpy as np
from decision_tree import DecisionTree
from sklearn.model_selection import train_test_split
from sklearn import datasets

def accuracy(y_true, y_predicted):
    accuracy = np,sum(y_true == y_predicted) / len(y_true)
    return accuracy

data = datasets.load_breast_cancer()
X, y = data.data, data.target

X_train, X_test, y_train, y_test = train_test_split(
    X, y, test_size=0.25, random_state=42
)

model = DecisionTree(max_depth = 10)
model.fit(X_train, y_train)

y_predicted= model.predict(X_test)
accu = accuracy(y_test, y_predicted)

print("Accuracy: ", accu)


#!/usr/bin/env python
from http.server import CGIHTTPRequestHandler, HTTPServer

# Set the port number for the server
PORT = 8000

# Create a CGI handler object and set the directory for CGI scripts
handler = CGIHTTPRequestHandler
handler.cgi_directories = ["/cgi-bin"]

# Start the server
httpd = HTTPServer(("", PORT), handler)
print("CGI server running on port", PORT)
httpd.serve_forever()


# DFS
from collections import defaultdict

N, M = map(int, input().split())

relation = defaultdict(list)
visited = [False] * N
for _ in range(M):
    a, b = map(int, input().split())
    relation[a].append(b)
    relation[b].append(a)

def dfs(v, depth):
    global answer
    if depth == 4:
        answer = True
        return
    for i in relation[v]:
        if not visited[i]:
            visited[v] = True
            dfs(i, depth+1)
            visited[v] = False

answer = False
for i in range(N):
    visited[i] = True
    dfs(i, 0)
    visited[i] = False
    if answer:
        break

if answer:
    print(1)
else:
    print(0)


# import libraries
import os
import matplotlib.pyplot as plt
import torch
import torchvision
from torch.nn import functional as F
from torch.utils.data import DataLoader
import pytorch_lightning as pl
from pytorch_lightning import Trainer, loggers
from pytorch_lightning.callbacks import EarlyStopping
from multiprocessing import Process
from loss import RarityWeightedLoss, L2Loss
import numpy as np
#import misc.npy_loader.loader as npy

import data_loader as dl

import torch.nn as nn
import warnings

warnings.filterwarnings('ignore')

import argparse

parser = argparse.ArgumentParser()
parser.add_argument('--lr', default=1e-2, type=float, help='learning rate') #TODO test initial lr of 1e-2 w cosine annealing
parser.add_argument('--betas', default = (0.9, 0.999), help='betas for ADAM')
parser.add_argument('--loss', default='RarityWeighted', help='loss function')
opt = parser.parse_args()

"""
 Code found from: https://github.com/richzhang/colorization
"""

weight_mix = np.load('../npy/weight_distribution_mix_with_uniform_distribution_reduced_normalized.npy')

class Colorization_model(pl.LightningModule):
	def __init__(self, norm_layer=nn.BatchNorm2d, num_bins=len(weight_mix), loss=opt.loss):
		super(Colorization_model, self).__init__()
		if loss == 'RarityWeighted':
			self.data_loaders = dl.return_loaders(soft_encoding=True)
			self.loss_criterion = RarityWeightedLoss(weight_mix)
		elif loss == 'L2':
			self.data_loaders = dl.return_loaders(soft_encoding=False)
			self.loss_criterion = L2Loss()
		model1=[nn.Conv2d(1, 64, kernel_size=3, stride=1, padding=1, bias=True),]
		model1+=[nn.ReLU(True),]
		model1+=[nn.Conv2d(64, 64, kernel_size=3, stride=2, padding=1, bias=True),]
		model1+=[nn.ReLU(True),]
		model1+=[norm_layer(64),]

		model2=[nn.Conv2d(64, 128, kernel_size=3, stride=1, padding=1, bias=True),]
		model2+=[nn.ReLU(True),]
		model2+=[nn.Conv2d(128, 128, kernel_size=3, stride=2, padding=1, bias=True),]
		model2+=[nn.ReLU(True),]
		model2+=[norm_layer(128),]

		model3=[nn.Conv2d(128, 256, kernel_size=3, stride=1, padding=1, bias=True),]
		model3+=[nn.ReLU(True),]
		model3+=[nn.Conv2d(256, 256, kernel_size=3, stride=1, padding=1, bias=True),]
		model3+=[nn.ReLU(True),]
		model3+=[nn.Conv2d(256, 256, kernel_size=3, stride=2, padding=1, bias=True),]
		model3+=[nn.ReLU(True),]
		model3+=[norm_layer(256),]

		model4=[nn.Conv2d(256, 512, kernel_size=3, stride=1, padding=1, bias=True),]
		model4+=[nn.ReLU(True),]
		model4+=[nn.Conv2d(512, 512, kernel_size=3, stride=1, padding=1, bias=True),]
		model4+=[nn.ReLU(True),]
		model4+=[nn.Conv2d(512, 512, kernel_size=3, stride=1, padding=1, bias=True),]
		model4+=[nn.ReLU(True),]
		model4+=[norm_layer(512),]

		model5=[nn.Conv2d(512, 512, kernel_size=3, dilation=2, stride=1, padding=2, bias=True),]
		model5+=[nn.ReLU(True),]
		model5+=[nn.Conv2d(512, 512, kernel_size=3, dilation=2, stride=1, padding=2, bias=True),]
		model5+=[nn.ReLU(True),]
		model5+=[nn.Conv2d(512, 512, kernel_size=3, dilation=2, stride=1, padding=2, bias=True),]
		model5+=[nn.ReLU(True),]
		model5+=[norm_layer(512),]

		model6=[nn.Conv2d(512, 512, kernel_size=3, dilation=2, stride=1, padding=2, bias=True),]
		model6+=[nn.ReLU(True),]
		model6+=[nn.Conv2d(512, 512, kernel_size=3, dilation=2, stride=1, padding=2, bias=True),]
		model6+=[nn.ReLU(True),]
		model6+=[nn.Conv2d(512, 512, kernel_size=3, dilation=2, stride=1, padding=2, bias=True),]
		model6+=[nn.ReLU(True),]
		model6+=[norm_layer(512),]

		model7=[nn.Conv2d(512, 512, kernel_size=3, stride=1, padding=1, bias=True),]
		model7+=[nn.ReLU(True),]
		model7+=[nn.Conv2d(512, 512, kernel_size=3, stride=1, padding=1, bias=True),]
		model7+=[nn.ReLU(True),]
		model7+=[nn.Conv2d(512, 512, kernel_size=3, stride=1, padding=1, bias=True),]
		model7+=[nn.ReLU(True),]
		model7+=[norm_layer(512),]

		model8=[nn.ConvTranspose2d(512, 256, kernel_size=4, stride=2, padding=1, bias=True),]
		model8+=[nn.ReLU(True),]
		model8+=[nn.Conv2d(256, 256, kernel_size=3, stride=1, padding=1, bias=True),]
		model8+=[nn.ReLU(True),]
		model8+=[nn.Conv2d(256, 256, kernel_size=3, stride=1, padding=1, bias=True),]
		model8+=[nn.ReLU(True),]

		model8+=[nn.Conv2d(256, num_bins, kernel_size=1, stride=1, padding=0, bias=True),]

		self.model1 = nn.Sequential(*model1)
		self.model2 = nn.Sequential(*model2)
		self.model3 = nn.Sequential(*model3)
		self.model4 = nn.Sequential(*model4)
		self.model5 = nn.Sequential(*model5)
		self.model6 = nn.Sequential(*model6)
		self.model7 = nn.Sequential(*model7)
		self.model8 = nn.Sequential(*model8)

		self.softmax = nn.LogSoftmax(dim=1)
		if loss == 'RarityWeighted':
			self.model9 = nn.Upsample(scale_factor=4, mode='bilinear')
		elif loss == 'L2':
			self.model9 = nn.Sequential(nn.Conv2d(num_bins, 2, kernel_size=1, padding=0, dilation=1, stride=1, bias=False),
										   nn.Upsample(scale_factor=4, mode='bilinear'))
		#self.model_out = nn.Conv2d(num_bins, 2, kernel_size=1, padding=0, dilation=1, stride=1, bias=False)
		#self.upsample4 = nn.Upsample(scale_factor=4, mode='bilinear')

	def forward(self, X):
		conv1_2 = self.model1(X)
		conv2_2 = self.model2(conv1_2)
		conv3_3 = self.model3(conv2_2)
		conv4_3 = self.model4(conv3_3)
		conv5_3 = self.model5(conv4_3)
		conv6_3 = self.model6(conv5_3)
		conv7_3 = self.model7(conv6_3)
		conv8_3 = self.model8(conv7_3)
		'''try returning num bins to loss function'''
		upsampled = self.model9(conv8_3)
		out_reg = self.softmax(upsampled)
		#out_reg = self.upsample4(self.softmax(conv8_3))
		#out = self.upsample4(out_reg)
		return out_reg

	def training_step(self, batch, batch_idx):
		X, y = batch
		output = self.forward(X)
		loss = self.loss_criterion(output, y)
		self.log('train_loss', loss, prog_bar=True, logger=True, on_step=True, on_epoch=False)
		return loss

	def validation_step(self,batch,batch_idx):
		X, y = batch
		output = self.forward(X)
		loss = self.loss_criterion(output, y)
		self.log('val_loss', loss, prog_bar=True, logger=True, on_step=True, on_epoch=False)
		return loss

	def configure_optimizers(self):
		optimizer = torch.optim.Adam(self.parameters(), lr=opt.lr, betas=opt.betas, weight_decay=1e-5)
		# T_max should be number of cycles to vary the learning rate, i set to 3 (12,000 steps if batch size is 25)
		scheduler = torch.optim.lr_scheduler.CosineAnnealingLR(optimizer=optimizer, eta_min=1e-6, T_max=12000)  #TODO comment out if you don't want to mess with
		return [optimizer], [scheduler]

	def predict_step(self, batch: int, batch_idx: int, dataloader_idx: int = None):
		return self(batch)

	def on_epoch_end(self):
		global_step = self.global_step
		for name, param in self.named_parameters():
			self.logger.experiment.add_histogram(name, param.grad, global_step)

	# @pl.data_loader
	def train_dataloader(self):
		return self.data_loaders['train']

	# @pl.data_loader
	def test_dataloader(self):
		return self.data_loaders['test']

	def val_dataloader(self):
		return self.data_loaders['validation']

	#def configure_optimizers(self):
		# Dummy optimizer for testing
	#	return torch.optim.Adam(self.parameters(), lr=1e-5)

def run_trainer():
	early_stop_call_back = EarlyStopping(
		monitor='val_loss',
		min_delta=0.00,
		patience=5,
		verbose=False,
		mode='max'
	)
	'''log learning rate'''
	lr_callback = pl.callbacks.LearningRateMonitor(logging_interval='epoch')
	model = Colorization_model(loss=opt.loss) #TODO set loss as RarityWeighted or L2, default: L2
	logger = loggers.TensorBoardLogger(save_dir = 'logs/')
	print("using GPU", torch.cuda.is_available())
	trainer = Trainer(max_epochs=300,
					  #gpus=1,
					  logger=logger, #use default tensorboard
					  log_every_n_steps=20, #log every update step for debugging
					  limit_train_batches=1.0,
					  limit_val_batches=1.0,
					  check_val_every_n_epoch=2,
					  callbacks=[early_stop_call_back, lr_callback])
	trainer.fit(model)
	'''we may not need the below. lightning model can be loaded from last checkpoint'''
	os.makedirs('trained_models', exist_ok=True)
	name = 'ColorizationModelOverfitTest.pth'
	torch.save(model, os.path.join('trained_models', name))

if __name__ == '__main__':
	# start trainer
	run_trainer()


#coding=utf-8
# 网络图片的爬取和存储--结合os库和文件操作的使用

import requests
import os

url = "http://tc.sinaimg.cn/maxwidth.800/tc.service.weibo.com/p3_pstatp_com/6da229b421faf86ca9ba406190b6f06e.jpg"
root = "//root//桌面//zmp//"
path = root + url.split('/')[-1]
try:
    if not os.path.exists(root):
        os.mkdir(root)
    if not os.path.exists(path):
        r = requests.get(url)
        with open(path, 'wb') as f:
            f.write(r.content)
            f.close()
            print("文件保存成功")
    else:
        print("文件已存在")
except:
    print("爬取失败")


# improvement over harris corner detection

import cv2
import numpy as np

# load the image

image = cv2.imread('../images/sudoku.jpg')
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

# we specify the top 50 corners

corners = cv2.goodFeaturesToTrack(gray,50,0.01,15)

# 0.01 -- minimum accepted quality of image corners

for corner in corners:
    x, y = corner[0]
    x = int(x)
    y = int(y)
    cv2.rectangle(image, (x-10,y-10),(x+10,y+10), (0,255,0), 2)

cv2.imshow("Corners found", image)
cv2.waitKey(0)
cv2.destroyAllWindows()


import pandas as pd
import datetime as dt
import numpy as np

# this is the dataset。
icd = pd.read_csv('datasetWithTitle/icd.csv')    # icd.csv has 7176 X 5, the 1st row is title.
patient = pd.read_csv('datasetWithTitle/patient.csv')
labresult = pd.read_csv('datasetWithTitle/lab.csv')
vitalsign = pd.read_csv('datasetWithTitle/vitalsign.csv')    # this is not important, so do not consider this info.
TestNumberThreshold = 3000

# ------- Define isTestNameDigital() ----------
def isTestNameDigital(testname):
    try:
        float(testname)
        return True
    except ValueError:
        return False
# ---------------------------------------------

# xudong: merge them in tables.
tables = [icd, patient, labresult, vitalsign]
for t in tables:
    print (list(t.columns.values), t.shape, type(t))

# Xudong: delete some columns in icd and labresult.
icd.drop(['ICDName','CodeSystem'],inplace=True,axis=1)
# icd.drop_duplicates(['PatientID', 'Date', 'ICD'],inplace = True) #Return Series with duplicate values removed.

labresult.drop(['LOINCNum','Unit'],inplace=True,axis=1)

# --- the original excel file contain duplicate rows, so need to drop_cuplicates ------
# labresult.drop_duplicates(['PatientID', 'ServiceDate', 'TestName', 'Value'],inplace = True) #Return Series with duplicate values removed.
labresult.drop_duplicates(['PatientID', 'ServiceDate', 'TestName'],inplace = True) #Return Series with duplicate values removed. ***** Very important to invoid having 4.34.2 (same patient, have same test at same day but different value)
labresult = labresult[ labresult['Value'].apply(isTestNameDigital)]    # drop the row if no date info.
# print labresult

vitalsign.drop(['HR', 'RR', 'FS', 'Temp', 'SPO2', 'PainLevel', 'Pulse'],inplace=True,axis=1)

for t in tables:
    print (list(t.columns.values), t.shape, type(t))

"""
['PatientID', 'Date', 'ICD'] (7175, 3)
['PatientID', 'PatientAge', 'PatientGender'] (583, 3)
['PatientID', 'ServiceDate', 'TestName', 'Value'] (105599, 4)
['PatientID', 'VisitDate', 'HT', 'WT', 'BP', 'RR', 'Temp', 'PainLevel'] (5095, 8)
"""
haveSlash = lambda x: '/' in x

def validate(date_text):
    # print type(date_text), date_text
    if not haveSlash(date_text):
        # print "0 -> "
        return False

    # temp = date_text
    try:
        # global i
        # print i
        # print 'try'
        dt.datetime.strptime(date_text, '%m/%d/%Y')
        # i += 1
        # print "-> 1"
        # return True
    except ValueError:

        dt.datetime.strptime(date_text, '%m/%d/%y')
        # print "2 -> "
        return True
    # except ValueError as b:
    except:
        # print "3 -> "
        return False
    else:
        # print "1 -> "
        return True

# def validate(date_text):
#     try:
#         dt.datetime.strptime(date_text, '%m/%d/%Y')
#         return True
#     except ValueError:
#         return False

icd = icd[ icd['Date'].apply(lambda x: validate(x))]    # drop the row if no date info.
labresult = labresult[ labresult['ServiceDate'].apply(lambda x: validate(x)) ]

# df.groupby("A").filter(lambda x: len(x) > 1)  # this is just a example for filter data.
# df = labresult.groupby(['TestName']).filter(lambda x: len(x) >= 3000)
df = labresult.groupby(['TestName']).filter(lambda x: len(x) >= TestNumberThreshold) # if # of testName is larger than 10, then have it.
#but some patient may not have it, even it is a popular testName.
# df.to_csv('xudong_output_df.csv')
table = pd.pivot_table(df, values='Value', index=['PatientID', 'ServiceDate'], columns=['TestName'], aggfunc=np.sum)
# table.drop(['BUN/CREATININE RATIO'],inplace=True,axis=1)

table.reset_index(level=0, inplace=True) # add PatientID back to columns
# table.reset_index(level=1, inplace=True) # add ServiceDate back to columns
table.reset_index(level=0, inplace=True) # add ServiceDate back to columns

vitalsign = vitalsign[ vitalsign['VisitDate'].apply(lambda x: validate(x)) ]
vitalsign = vitalsign[vitalsign['BP'].apply(lambda x: '/' in x)] # filter blood pressure
tmp = vitalsign['BP'].apply(lambda x: x.split('/'))
vitalsign['high'] = tmp.apply(lambda x: x[0])
vitalsign['low'] = tmp.apply(lambda x: x[1])
print (vitalsign.columns.values, vitalsign.shape)
vitalsign.drop(['BP'],inplace=True,axis=1)
print (vitalsign.columns.values, vitalsign.shape)

print ("merge by date......")
# time column are still in the type "object"
icd = icd.copy()
# table = table.copy()
# vitalsign = vitalsign.copy()
icd['Date'] = pd.to_datetime(icd['Date'])
table['ServiceDate'] = pd.to_datetime(table['ServiceDate'])
vitalsign['VisitDate'] = pd.to_datetime(vitalsign['VisitDate'])

res = icd
res = pd.merge(res, table, left_on=['Date', 'PatientID'], right_on=['ServiceDate', 'PatientID'], how='inner')
res.drop(['PatientID', 'ServiceDate', 'Date'],inplace=True,axis=1)

# res.drop(['ALBUMIN','PROTEIN,TOTAL'],inplace=True,axis=1)   # the value of test named 'ALBUMIN' contain wrong value as 4.224.66.
# res.drop(['PatientID', 'Date', 'VisitDate', 'ServiceDate'],inplace=True,axis=1)
np.save('y_icd.npy', res['ICD'].values[:])
np.save('trainingData_icd.npy', res)

def discretize(x, y):
    # find index of each y in list x
    # http://stackoverflow.com/questions/8251541/numpy-for-every-element-in-one-array-find-the-index-in-another-array
    xsorted = np.argsort(x)
    ypos = np.searchsorted(x[xsorted], y)
    indices = xsorted[ypos]
    return indices

icd_code = res['ICD'].unique()

#icd_code contains the different icd code from 0 to Nmax.
res['ICD'].values[:] = discretize(icd_code, res['ICD'].values[:])   # change the icd code to index in res.
np.save('y_index.npy', res['ICD'].values[:])
np.save('trainingData_index.npy', res)
res.to_csv('trainingData_index.csv')
# print res['ICD'].values[:]

res.drop(['ICD'],inplace=True,axis=1)
res.drop_duplicates()   #Return Series with duplicate values removed.
np.save('x.npy', res)
np.save('x_without_none.npy', res.dropna())
res.to_csv('xudong_output2.csv')


import os
import tempfile
import unittest

import astropy.units as u
import numpy as np
from pixell import utils, enmap, curvedsky

try:
    # Import sotodlib toast module first, which sets global toast defaults
    import sotodlib.toast as sotoast
    import sotodlib.toast.ops as so_ops
    import toast
    from toast.observation import default_values as defaults
    toast_available = True
except ImportError as e:
    toast_available = False

if toast_available:
    import healpy as hp

from ._helpers import calibration_schedule, close_data_and_comm, simulation_test_data

class MapmakerPointingTest(unittest.TestCase):
    def test_mapmaker_pointing(self):
        if not toast_available:
            print("toast cannot be imported- skipping unit tests", flush=True)
            return

        log = toast.utils.Logger.get()

        nnz = 3
        nside = 1024
        wpix = hp.nside2pixarea(nside, degrees=True)**.5 * 60
        npix = 12 * nside**2
        testdir = tempfile.TemporaryDirectory()

        comm, procs, rank = toast.get_world()
        data = simulation_test_data(
            comm,
            telescope_name=None,
            wafer_slot="w00",
            bands="LAT_f230",
            sample_rate=10.0 * u.Hz,
            thin_fp=16,
            cal_schedule=False,
        )

        pointing = toast.ops.PointingDetectorSimple(
            name="det_pointing_radec",
            quats="quats_radec",
            boresight=defaults.boresight_radec,
            shared_flag_mask=0,
        )
        pixels = toast.ops.PixelsHealpix(
            nside=nside,
            create_dist="pixel_dist",
            detector_pointing=pointing,
        )
        weights = toast.ops.StokesWeights(
            name="weights_radec",
            weights="weights_radec",
            mode="IQU",
            detector_pointing=pointing,
        )

        input_map_file = os.path.join(
            testdir.name,
            "pointing_test_map.fits",
        )
        if rank == 0:
            input_map = np.zeros([nnz, npix], dtype=np.float32)
            input_map[1] = 1
            hp.write_map(
                input_map_file, input_map, nest=pixels.nest, column_units="K"
            )
        log.info_rank(f"Wrote test map to {input_map_file}", comm=comm)

        if data.comm.comm_world is not None:
            data.comm.comm_world.Barrier()

        # Scan map into timestreams
        scan_hpix = toast.ops.ScanHealpixMap(
            file=input_map_file,
            det_data=defaults.det_data,
            pixel_pointing=pixels,
            stokes_weights=weights,
        )
        scan_hpix.apply(data)

        # Bin the map using TOAST
        toast.ops.DefaultNoiseModel(noise_model="noise_model").apply(data)
        binner = toast.ops.BinMap(
            pixel_dist="pixel_dist",
            pixel_pointing=pixels,
            stokes_weights=weights,
            noise_model="noise_model",
            shared_flag_mask=0,
        )
        toast.ops.MapMaker(
            name="mapmaker",
            det_data=defaults.det_data,
            binning=binner,
            write_hits=True,
            write_binmap=True,
            write_rcond=False,
            write_invcov=False,
            write_cov=False,
            write_map=False,
            output_dir=testdir.name,
        ).apply(data)
        file_hits_toast = os.path.join(testdir.name, "mapmaker_hits.fits")
        file_map_toast = os.path.join(testdir.name, "mapmaker_binmap.fits")

        # Create an area file
        areafile = os.path.join(testdir.name, "area_file.fits")
        box = utils.parse_box("-51:-35,35:49") * utils.degree
        res = wpix * utils.arcmin
        shape, wcs = enmap.geometry(box, res=res, proj="car")
        wcs.wcs.crpix[1] -= 0.5
        enmap.write_map(areafile, enmap.zeros(shape, wcs, np.uint8))

        mapmaker = so_ops.MLMapmaker(
            area=areafile,
            name="mlmapmaker",
            out_dir=testdir.name,
            comps="TQU",
            nmat_type="Nmat",
            maxiter=3,
            truncate_tod=False,
            write_hits=True,
            write_rhs=False,
            write_div=False,
            write_bin=True,
            deslope=False,
        )
        mapmaker.apply(data)

        if rank == 0:
            # Direct comparison of pointing
            obs = data.obs[0]
            pmap = mapmaker._signal_map.data[obs.name].pmap
            det_quats = pmap._get_asm().dets
            coords = np.array(pmap.sight.coords(det_quats))
            dets = mapmaker._mapmaker.data[0].dets
            ndet = len(dets)

            pointing.apply(data)
            dlats_mean = np.zeros(ndet)
            dlats_rms = np.zeros(ndet)
            dlons_mean = np.zeros(ndet)
            dlons_rms = np.zeros(ndet)
            dgammas_mean = np.zeros(ndet)
            dgammas_rms = np.zeros(ndet)
            for idet in range(ndet):
                lon, lat, cosgamma, singamma = coords[idet].T
                gamma = np.arctan2(singamma, cosgamma)
                theta, toast_lon, toast_gamma = toast.qarray.to_iso_angles(
                    obs.detdata["quats_radec"][idet]
                )
                toast_lat = np.pi / 2 - theta
                toast_dets = obs.local_detectors
                dlon = np.degrees(lon - toast_lon) * 3600
                dlat = np.degrees(lat - toast_lat) * 3600
                dgamma = np.degrees(np.unwrap(gamma - toast_gamma)) * 3600
                mean_dlon = np.mean(dlon)
                rms_dlon = np.std(dlon)
                mean_dlat = np.mean(dlat)
                rms_dlat = np.std(dlat)
                mean_dgamma = np.mean(dgamma)
                rms_dgamma = np.std(dgamma)
                #log.info(f"dlat = {mean_dlat:.3f} +- {rms_dlat:.3f} arcsec")
                #log.info(f"dlon = {mean_dlon:.3f} +- {rms_dlon:.3f} arcsec")
                #log.info(f"dgamma = {mean_dgamma:.3f} +- {rms_dgamma:.3f} arcsec")
                dlats_mean[idet] = mean_dlat
                dlats_rms[idet] = rms_dlat
                dlons_mean[idet] = mean_dlon
                dlons_rms[idet] = rms_dlon
                dgammas_mean[idet] = mean_dgamma
                dgammas_rms[idet] = rms_dgamma
            log.info(f"dlat = {np.mean(dlats_mean):.3f} +- {np.std(dlats_mean):.3f} arcsec")
            log.info(f"dlon = {np.mean(dlons_mean):.3f} +- {np.std(dlons_mean):.3f} arcsec")
            log.info(f"dgamma = {np.mean(dgammas_mean):.3f} +- {np.std(dgammas_mean):.3f} arcsec")
            tol = 1.0
            if rms_dlon > tol:
                raise RuntimeError(f"RA differs systematically over {tol} arc seconds")
            if rms_dlat > tol:
                raise RuntimeError(f"Dec differs systematically over {tol} arc seconds")
            if rms_dgamma > tol:
                raise RuntimeError(f"PA differs systematically over {tol} arc seconds")

            # Compare binned maps

            file_hits = os.path.join(testdir.name, "mlmapmaker_sky_hits.fits")
            file_map = os.path.join(testdir.name, "mlmapmaker_sky_map.fits")
            hits = enmap.read_map(file_hits).astype(int)
            sky = enmap.read_map(file_map)
            good = hits != 0
            ind = sky[1] != 0

            toast_hits = hp.read_map(file_hits_toast)
            toast_sky = hp.read_map(file_map_toast, None)
            toast_good = toast_hits != 0
            toast_ind = toast_sky[1] != 0

            rms = []
            means = []
            toast_rms = []
            toast_means = []
            log.info(f"            {'MLMapmaker':16}            {'TOAST':16}")
            log.info(f"Hits        {np.sum(hits):8}       {np.sum(toast_hits):16}")
            log.info(f"Hit pixels  {np.sum(good):8}       {np.sum(toast_good):16}")
            log.info(f"After cut   {np.sum(ind):8}       {np.sum(toast_ind):16}")
            for i in range(3):
                rms.append(np.std(sky[i][ind].ravel()))
                means.append(np.mean(sky[i][ind].ravel()))
                toast_rms.append(np.std(toast_sky[i][toast_ind].ravel()))
                toast_means.append(np.mean(toast_sky[i][toast_ind].ravel()))
                stokes = "IQU"[i]
                log.info(
                    f"{stokes:3} {means[i]:12.6f} +- {rms[i]:12.6f}, "
                    f"{toast_means[i]:12.6f} +- {toast_rms[i]:12.6f}"
                )

            tol = 1e-3
            if np.abs(means[0]) > tol:
                raise RuntimeError("Found non-zero I")

            if np.abs(means[1] - 1) > tol:
                raise RuntimeError("Found non-unit Q")

            if np.abs(means[2]) > tol:
                raise RuntimeError("Found non-zero U")

        close_data_and_comm(data)

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


from intake.container.base import RemoteSource, get_partition
from intake.source.base import Schema

class RemoteSequenceSource(RemoteSource):
    """Sequence-of-things source on an Intake server"""

    name = "remote_sequence"
    container = "python"

    def __init__(self, url, headers, **kwargs):
        self.url = url
        self.npartitions = kwargs.get("npartition", 1)
        self.partition_access = self.npartitions > 1
        self.headers = headers
        self.metadata = kwargs.get("metadata", {})
        self._schema = Schema(npartitions=self.npartitions, extra_metadata=self.metadata)
        self.bag = None
        super(RemoteSequenceSource, self).__init__(url, headers, **kwargs)

    def _load_metadata(self):
        import dask
        import dask.bag as db

        if self.bag is None:
            self.parts = [dask.delayed(get_partition)(self.url, self.headers, self._source_id, self.container, i) for i in range(self.npartitions)]
            self.bag = db.from_delayed(self.parts)
        return self._schema

    def _get_partition(self, i):
        self._load_metadata()
        return self.parts[i].compute()

    def read(self):
        self._load_metadata()
        return self.bag.compute()

    def to_dask(self):
        self._load_metadata()
        return self.bag

    def _close(self):
        self.bag = None

    @staticmethod
    def _persist(source, path, encoder=None, **kwargs):
        """Save list to files using encoding

        encoder : None or one of str|json|pickle
            None is equivalent to str
        """
        import json
        import pickle

        encoder = {None: str, "str": str, "json": json.dumps, "pickle": pickle.dumps}[encoder]
        try:
            b = source.to_dask()
        except NotImplementedError:
            b = source.read()
        return RemoteSequenceSource._data_to_source(b, path, encoder, **kwargs)

    @staticmethod
    def _data_to_source(b, path, encoder=None, storage_options=None, **kwargs):
        import json
        import pickle
        import posixpath

        import dask
        import dask.bag as db
        from fsspec import open_files

        from intake.source.textfiles import TextFilesSource

        encoder = {None: str, "str": str, "json": json.dumps, "pickle": pickle.dumps}.get(encoder, encoder)

        if not hasattr(b, "to_textfiles"):
            try:
                b = db.from_sequence(b, npartitions=1)
            except TypeError:
                raise NotImplementedError

        files = open_files(posixpath.join(path, "part.*"), mode="wt", num=b.npartitions, **(storage_options or {}))
        dwrite = dask.delayed(write_file)
        out = [dwrite(part, f, encoder) for part, f in zip(b.to_delayed(), files)]
        dask.compute(out)
        s = TextFilesSource(posixpath.join(path, "part.*"), storage_options=storage_options)
        return s

def write_file(data, fo, encoder):
    with fo as f:
        for d in data:
            f.write(encoder(d))

{'dask': 'dask', 'db': 'dask.bag', 'json': 'json', 'pickle': 'pickle'}._data_to_source


import common, itertools
def solve():
	for n in itertools.count(1):
		if (divisorCount(n) + 1) // 2 > 1000:
			return str(n)

def divisorCount(n):
	count = 1
	end = common.sqrt(n)
	for i in itertools.count(2):
		if i > end:
			break
		if n % i == 0:
			j = 0
			while True:
				n //= i
				j += 1
				if n % i != 0:
					break
			count *= j * 2 + 1
			end = common.sqrt(n)
	if n != 1:
		count *= 3
	return count

if __name__ == "__main__":
	print(solve())


from datetime import datetime, date
from person_class import Person

# Employee (наследуемся от Person) (добавляются свойства: должность, опыт работы, зарплата)
# ** (только для продвинутых) Можете в конструкторе проверить, что в опыт работы и зарплата не отрицательные 😊
# Реализовать новые методы:
# возвращает должность с приставкой, которая зависит от опыта работы: Junior — менее 3 лет, Middle — от 3 до 6 лет,
# Senior — больше 6 лет.
# Т.е. метод должен вернуть позицию с приставкой Junior/Middle/Senior <position>.
# Если, например у вас объект имел должность “programmer” с опытом 2 года, метод должен вернуть “Junior programmer”
# метод, который увеличивает зарплату на сумму, которую вы передаёте аргументом.

class Employee(Person):
    """full_name= str must be two words name and surname like 'Ivan Durak', raise ValueError if not\n
        birth_date= datetime.datetime.date obj , year must be in range 1900<year<current_year,
        raise ValueError if not\n
        position='' position str\n
        experience=0 years of experience int raise ValueError if <0\n
        salary=0 current salary of employee int raise ValueError if <0"""

    def __init__(self, full_name='', birth_date=date(datetime.now().year, datetime.now().month, datetime.now().day),
                 position='', experience=0, salary=0):
        super().__init__(full_name, birth_date)
        self._position = position
        if experience >= 0:
            self.experience = experience
        else:
            raise ValueError(f'experience={experience} can not be less 0')
        if salary >= 0:
            self.salary = salary
        else:
            raise ValueError(f'experience={salary} can not be less 0')

    def full_position(self) -> str:
        """return str of full position based on experience and position\n
        0..2 Junior; 3..6 Middle; 6 and more Senior"""
        status = ("Junior" if self.experience < 3 else
                  "Middle" if 3 <= self.experience < 6 else
                  "Senior")
        return f'{status} {self._position}'

    def change_salary(self, delta=0):
        """change salary of employee to delta\n
        delta=0 must be int, can be minus
        but
        delta in minus can not be more salary raise ValueError
        """
        if self.salary + delta >= 0:
            self.salary += delta
        else:
            raise ValueError(f'delta={delta} , sum of salary={self.salary} and delta can\'t be less 0')

    def __str__(self):
        return f'{super().__str__()}, {self._position}, {self.experience}, {self.salary}'

if __name__ == '__main__':
    per = Employee('Ivan Durak', date(1985, 10, 27), 'programmer', 5, 2500)
    print(per)


import statsmodels.api as sm

# In[4]:

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
sns.set()

# In[5]:

url = 'https://en.wikipedia.org/wiki/Estimates_of_historical_world_population'
dataset = pd.read_html(url, header = 0)

# In[6]:

dataset = dataset[2]

# In[7]:

dataset.head()

# In[8]:

dataset.to_excel('World_Population_Estimate.xlsx')

# In[9]:

dataset_for_analysis = dataset[['Year', 'United States Census Bureau (2017)[29]']]

# In[10]:

dataset_for_analysis.head()

# In[11]:

dataset_for_analysis = dataset_for_analysis.dropna()

# In[12]:

dataset_for_analysis.describe()

# ## Data Exploration

# In[13]:

y = dataset_for_analysis['United States Census Bureau (2017)[29]'] # Defining the independent and dependent variables
x1 = dataset_for_analysis['Year']

# In[16]:

fig, ax = plt.subplots(figsize = (14,7))
plt.scatter(x1,y)
plt.title('World Population', fontsize=45)
plt.xlabel('Year', fontsize=40)
plt.ylabel('Census', fontsize=40)
plt.show()

# ## Regression Model

# In[28]:

y = dataset_for_analysis['United States Census Bureau (2017)[29]'] # Defining the independent and dependent variables
x1 = dataset_for_analysis['Year']

# ### Using Ordinary Least Squares

# In[29]:

x = sm.add_constant(x1)
results = sm.OLS(y,x).fit()
results.summary()

# ## Explanation of table
# ## With our R- squared value of 0.997, this shows our model explains the variability of the data to a large extent

# In[15]:

fig, ax = plt.subplots(figsize = (14,8))
plt.scatter(x1,y)
yhat = 75290000*x1 - 144500000000 # census = 75290000 * x1 + (-144500000000)
fig = plt.plot(x1, yhat, lw=4, c='yellow', label = 'regression line')
plt.title('World Population Growth', fontsize=45)
plt.xlabel('Year', fontsize=40)
plt.ylabel('Census', fontsize=40)
plt.show()

#
# # Interpretation of Regression Analysis
#
# ## From the analysis, we calculated the values for our constants in the regression equation:
# ### yhat = -144500000000 + 75290000 * x1
# ### Where;
# ### x1 = Year
# ### yhat = Census
# ## This equation can be use to predict the World's population
#

# In[ ]:


from datetime import datetime

trabalhador = {}
trabalhador['nome'] = input('Digite o nome:')
trabalhador['idade'] = datetime.now().year - int(input('Digite o ano de nascimento:'))
trabalhador['ctps'] = int(input('Digite o número da carteira (0 não tem):'))
if trabalhador['ctps'] != 0:
    trabalhador['anoContratacao'] = int(input('Digite o ano de contratação:'))
    trabalhador['salario'] = float(input('Digite o salário:'))
    trabalhador['aposentadoria'] = trabalhador['anoContratacao'] + 35
print(trabalhador)
for k, v in trabalhador.items():
    print(f'{k} tem o valor {v}')


import smtplib
import speech_recognition as sr
import pyttsx3
from email.message import EmailMessage

listener = sr.Recognizer()
engine = pyttsx3.init()

def talk(text):
    engine.say(text)
    engine.runAndWait()

def get_info():
    try:
        with sr.Microphone() as source:
            print('listening...')
            voice = listener.listen(source)
            info = listener.recognize_google(voice)
            print(info)
            return info.lower()
    except:
        pass

def send_email(receiver, subject, message):
    server = smtplib.SMTP('smtp.gmail.com', 587)
    server.starttls()

    server.login('appbanana2701@gmail.com', '********')
    email = EmailMessage()
    email['From'] = 'Sender_Email'
    email['To'] = receiver
    email['Subject'] = subject
    email.set_content(message)
    server.send_message(email)

email_list = {
    'pratyush': '*****************',
    'guru': '***********',

}

def get_email_info():
    talk('Hi Sir I am your assistant for today, To Whom you want to send email')
    name = get_info()
    receiver = email_list[name]
    print(receiver)
    talk('What is the subject of your email?')
    subject = get_info()
    talk('Tell me the text in your email')
    message = get_info()
    send_email(receiver, subject, message)
    talk('Thankyou sir for using me. Your email has been send')
    talk('Do you want to send more email?')
    send_more = get_info()
    if 'yes' in send_more:
        get_email_info()

get_email_info()


#!/usr/bin/env python
"""
Compute nye tensor around dislocation core.
The INPUT should be a LAMMPS dump file.

Usage:
  nye_tensor.py dumpconf
  nye_tensor.py calc INPUT [CONFIG]
  nye_tensor.py plot [options] [CONFIG]

Options:
  -h,--help   Show this message and exit.
  --component COMPONENT
              Specifies a component to be plot. [default: 33]
  -x X        Origin of x. [default: 0.0]
  -y Y        Origin of y. [default: 0.0]
  --width W   Half width in unit of Burgers vector. [default: 10]
  --height H  Half height in unit of Burgers vector. [default: 8]
"""
from __future__ import print_function

import os,sys,math,json
from docopt import docopt
import numpy as np
import numba

from ase.io import read

__author__ = "RYO KOBAYASHI"
__version__ = "160930"

########################################################################
#...parameters to be set to match the system
########################################################################
a0 = 4.045
c_o_a = 1.0
phi_min = 27.
iwindow = 1
xmin = 150.  # in Angstrom
xmax = 200.
ymin = 150.
ymax = 200.
nei_perf = np.zeros((12,3),dtype=float)
nei_perf[:,:] = [
    [ .5,  .5,  0.],
    [ .5, -.5,  0.],
    [-.5,  .5,  0.],
    [-.5, -.5,  0.],
    [ .5,  0.,  .5],
    [ .5,  0., -.5],
    [-.5,  0.,  .5],
    [-.5,  0., -.5],
    [ 0.,  .5,  .5],
    [ 0., -.5,  .5],
    [ 0.,  .5, -.5],
    [ 0., -.5, -.5]
]
#...rotation matrix from reference to deformed system
#...to be normalized in the script
block = np.array([[ 1.,-2., 1.],
                   [-1.,-1.,-1.],
                   [ 1., 0.,-1.]])

#...max number of nearest neighbors
maxnn = 20

def nye_tensor(atoms):
    """
    Compute Nye tensor around z-axis of given structure.
    """
    global block,maxnn,nei_perf,a0,\
        xmin,xmax,ymin,ymax,iwindow,phi_min

    phi_min = math.cos(phi_min*math.pi/180)
    pos = atoms.get_positions()
    cot = [ [i,pos[i,0],pos[i,1],pos[i,2]] for i in range(len(atoms)) ]
    co1 = np.array(cot)
    cell = atoms.get_cell()
    #...assuming that cell[2,2] is z-period
    period = cell[2,2]
    ntot = len(atoms)
    print(' num of atoms = ',ntot)

    xmin0 = np.min(co1[:,1])
    xmax0 = np.max(co1[:,1])
    ymin0 = np.min(co1[:,2])
    ymax0 = np.max(co1[:,2])
    zmin0 = np.min(co1[:,3])
    zmax0 = np.max(co1[:,3])
    zmin = zmin0 - 0.05
    zmax = zmax0 + 0.05

    for mm in range(3):
        block[mm,:] /= np.linalg.norm(block[mm,:])
    block = block.T
    print(' block after modification:')
    for mm in range(3):
        print(' {0:8.2f} {1:8.2f} {2:8.2f}'.format(block[mm,0],
                                                   block[mm,1],
                                                   block[mm,2]))

    nei_perf *= a0
    b = nei_perf[0,:]
    r1 = np.sqrt(np.dot(b,b))
    rc = (r1 +a0)/2
    rc2 = rc*rc
    print(' r1 = ',r1)
    print(' rc,rc2 = ',rc,rc2)

    #...Rotate the lattice vectors to the block system (what is block system?)
    # print(' nei_perf before rotation:')
    # for i in range(len(nei_perf)):
    #     print('   i,nei_perf =',i,nei_perf[i,:])
    for i in range(len(nei_perf)):
        nei_perf[i,:] = np.dot(nei_perf[i,:],block)
    # print(' nei_perf after rotation:')
    # for i in range(len(nei_perf)):
    #     print('   i,nei_perf =',i,nei_perf[i,:])

    #...select atoms within the window
    if iwindow > 0:
        tmp = []
        for i in range(ntot):
            if (xmin < co1[i,1] < xmax) and (ymin < co1[i,2] < ymax) and (zmin < co1[i,3] < zmax):
                tmp.append(co1[i,:])
        nfreew = len(tmp)
        co = np.array(tmp)

    else:
        xmin = xmin0
        xmax = xmax0
        ymin = ymin0
        ymax = ymax0
        zmin = zmax0
        zmax = zmax0
        nfreew = ntot
        co = copy.deepcopy(co1)
    print(' nfreew = ',nfreew)

    print(' >>> make_neighbors...')
    lsnn,lspr,lssft = make_neighbors_fast(co,co1,maxnn,
                                          rc,period)
    #check_neighbors(lsnn,lspr,lssft,co,co1,nei_perf)
    print(' >>> get_lattice_correspondance...')
    emat = get_lattice_correspondance(co,co1,
                                      lsnn,lspr,lssft,
                                      nei_perf,maxnn,
                                      period,r1,phi_min)
    print(' >>> compute_nye...')
    nye,inner = compute_nye(co,co1,lsnn,lspr,lssft,
                            emat,rc,period,
                            xmin,xmax,ymin,ymax,zmin,zmax)

    #...write to file
    f = open('dat.nye','w')
    for jj in range(len(inner)):
        i = inner[jj]
        f.write(' {0:8.3f} {1:8.3f} {2:8.3f}'.format(co[i,1],co[i,2],co[i,3]))
        for mm in range(3):
            f.write(' {0:8.4f} {1:8.4f} {2:8.4f}'.format(nye[jj,mm,0],
                                                        nye[jj,mm,1],
                                                        nye[jj,mm,2]))
        f.write('\n')
    f.close()
    print(' Nye tensor was written to file: dat.nye')
    return None

#@numba.jit
def make_neighbors(co,co1,maxnn,rc2,period):
    """
    Make neighbors of atoms in the window.
    Note that the neighbors can be outside the window.
    """
    #...indentify neighbors within the window
    nfreew = len(co)
    nnmax = 0
    nnmin= 20
    ntemp = np.zeros(maxnn,dtype=int)
    nshift= np.zeros(maxnn,dtype=int)
    lsnn = np.zeros(nfreew,dtype=int)
    lspr = np.zeros((nfreew,maxnn),dtype=int)
    lssft= np.zeros((nfreew,maxnn),dtype=int)
    for i in range(nfreew):
        ri = co[i,1:4]
        nn = 0
        for j in range(len(co1)):
            rj = co1[j,1:4]
            for k in (-1,0,1):
                rij = rj[:] -ri[:]
                rij[2] += period*k
                #r2 = np.dot(rij,rij)
                r2 = rij[0]*rij[0] +rij[1]*rij[1] +rij[2]*rij[2]
                if 0.001 < r2 < rc2:
                    ntemp[nn] = j
                    nshift[nn]= k
                    nn += 1
                    if nn >= maxnn:
                        raise RuntimeError('nn >= maxnn, nn =',nn)

        lsnn[i] = nn
        lspr[i,0:nn] = ntemp[0:nn]
        lssft[i,0:nn] = nshift[0:nn]
        if nn > nnmax:
            nnmax = nn
        if nn < nnmin:
            nnmin = nn
    print(' nnmax = ',nnmax)
    print(' nnmin = ',nnmin)
    return lsnn,lspr,lssft

def make_neighbors_fast(co,co1,maxnn,rc,period):
    """
    Make neighbors of atoms in the window using cell list method,
    which is much faster than Brute-force method.
    Assuming that the PERIOD, which is z-lengh, is short enough
    not to use cell list method.
    Note that the neighbors can be outside the window.
    """
    tiny = 1.e-3
    xmin1 = np.min(co1[:,1])-tiny
    xmax1 = np.max(co1[:,1])+tiny
    ymin1 = np.min(co1[:,2])-tiny
    ymax1 = np.max(co1[:,2])+tiny
    zmin1 = np.min(co1[:,3])-tiny
    zmax1 = np.max(co1[:,3])+tiny
    wx = xmax1 - xmin1
    wy = ymax1 - ymin1
    print(' xmin1,xmax1,wx =',xmin1,xmax1,wx)
    print(' ymin1,ymax1,wy =',ymin1,ymax1,wy)
    lcx = int(wx/rc)
    lcy = int(wy/rc)
    lcxy= lcx*lcy
    print(' lcx,lcy,lcxy = ',lcx,lcy,lcxy)
    rcx = wx /lcx
    rcy = wy /lcy
    rcxi= 1.0 /rcx
    rcyi= 1.0 /rcy
    lscl= np.zeros(len(co1),dtype=int)
    lshd= np.zeros(lcxy,dtype=int)
    lscl[:] = -1
    lshd[:] = -1

    for i in range(len(co1)):
        #...assign a vector cell index
        mx = int((co1[i,1]-xmin1)*rcxi)
        my = int((co1[i,2]-ymin1)*rcyi)
        m = mx*lcy + my
        try:
            lscl[i] = lshd[m]
        except:
            print(' i,mx,my,m =',i,mx,my,m)
            print(' co1 =',co1[i,:])
            raise
        lshd[m] = i

    natm = len(co)
    natm1= len(co1)
    lsnn = np.zeros(natm,dtype=int)
    lspr = np.zeros((natm,maxnn),dtype=int)
    lssft= np.zeros((natm,maxnn),dtype=int)
    lspr[:] = -1

    rc2 = rc*rc
    for ia in range(len(co)):
        ri = co[ia,1:4]
        mx = int((ri[0]-xmin1)*rcxi)
        my = int((ri[1]-ymin1)*rcyi)
        m = mx*lcy + my
        for kuy in (-1,0,1):
            m1y = my + kuy
            if m1y < 0 or m1y >= lcy: continue
            for kux in (-1,0,1):
                m1x = mx + kux
                if m1x < 0 or m1x >= lcx: continue
                m1 = m1x*lcy + m1y
                ja = lshd[m1]
                if ja == -1: continue
                scan_j_in_cell(ia,ri,ja,lscl,rc2,co1,
                               lsnn,lspr,lssft,maxnn,
                               period)

    print(' nnmax = ',np.max(lsnn))
    print(' nnmin = ',np.min(lsnn))
    return lsnn,lspr,lssft

def scan_j_in_cell(ia,ri,ja,lscl,rc2,co1,
                   lsnn,lspr,lssft,maxnn,period):
    if ja == ia: ja = lscl[ja]
    if ja == -1: return 0

    if not ja in lspr[ia]:
        rj= co1[ja,1:4]
        for k in (-1,0,1):
            rij= rj[:] - ri[:]
            rij[2] += period*k
            r2 = rij[0]*rij[0] +rij[1]*rij[1] +rij[2]*rij[2]
            if 0.001 < r2 < rc2:
                n = lsnn[ia]
                lspr[ia,n] = ja
                lssft[ia,n]= k
                lsnn[ia] += 1
                # if lsnn[ia] > 12:
                #     print(' lsnn[ia] > 12, ia =',ia)
                #     print('   lspr = ',lspr[ia,:])
                #     print('   lssft= ',lssft[ia,:])
                if lsnn[ia] >= maxnn:
                    raise RuntimeError(' lsnn >= maxnn,'+
                                       ' ia =',ia)

    ja= lscl[ja]
    scan_j_in_cell(ia,ri,ja,lscl,rc2,co1,
                   lsnn,lspr,lssft,maxnn,period)

def check_neighbors(lsnn,lspr,lssft,co,co1,nei_perf):
    #...check neighbor list and rotated nei_perf
    print(' >>> check_neighbors...')
    idx = lsnn.argmax()
    print(' idx =',idx)
    ri = co[idx,1:4]
    print('Neighbor vectors in deformed system:')
    for jj in range(lsnn[idx]):
        j = lspr[idx,jj]
        rj = co1[j,1:4]
        rij = rj[:] -ri[:]
        rij[2] += period*lssft[idx,jj]
        print('jj,j,rij=',jj,j,rij)

    for j in range(len(nei_perf)):
        print('j,nei_perf =',j,nei_perf[j,:])
    return None

# @numba.jit
def get_lattice_correspondance(co,co1,lsnn,lspr,lssft,nei_perf,
                               maxnn,period,r1,phi_min):
    """
    Compute lattice correspondance between deformed and reference lattices.
    Local strain is also computed in the same loop as well.

    Returns
    --------
    emat : ndarray
           Local correspondance strain matrices for each atom.

    """

    nfreew = len(co)
    iden = np.identity(3)
    isym = 0
    iunique = 0
    iiu = 0
    npr = 0
    rr = np.zeros(maxnn)
    best = np.zeros(maxnn)
    km = [ 0 for i in range(maxnn) ]
    igood = np.zeros((nfreew,maxnn))
    nsym = np.zeros(nfreew)
    best_fit = np.ones(nfreew)
    emat = np.zeros((nfreew,3,3))
    angle = np.zeros(len(nei_perf))
    #...loop for atoms in deformed system
    for i in range(nfreew):
        ri = co[i,1:4]
        b = np.zeros((3,3))
        a = np.zeros((3,3))
        isym1 = isym
        iunique1 = iunique
        #...loop for neighbors of atom-i in deformed system
        for jj in range(lsnn[i]):
            j = lspr[i,jj]
            rj = co1[j,1:4]
            rij = rj[:] - ri[:]
            rij[2] += period*lssft[i,jj]
            drij= np.linalg.norm(rij)
            rr[jj] = abs(drij-r1)
            erij = rij[:]/drij
            #...check angle of bond from reference structure
            for k in range(len(nei_perf)):
                rn = nei_perf[k,0:3]
                ern = rn[:]/r1
                angle[k] = np.dot(erij,ern)
            best[jj] = np.max(angle)
            kmax = [ k for k in range(len(angle)) if abs(angle[k]-best[jj]) < 1.e-5 ]

            if not kmax:
                print('i,jj,j,kmax=',i,jj,j,kmax)
                print('ri,rj=',ri,rj)
                print('angle =',angle)
                raise ValueError('kmax is [] which should not happen.')

            if len(kmax) > 1:
                if isym == 0:
                    print(' problems with symmetry: isym==0, len(kmax)>1')
                    print('     i,jj,j =',i,jj,j)
                isym = isym + 1
                igood[i,jj] = 0
            else:
                igood[i,jj] = 1
                if best[jj] < best_fit[jj]:
                    best_fit[jj] = best[jj]
            if igood[i,jj] == 1: # if the deformed vector unique
                km[jj] = kmax
                repeat = ismember(km[0:jj],km[jj])
                if np.sum(repeat) > 0:
                    try:
                        jjj = km[0:jj].index(km[jj])
                    except:
                        print('kmax = ',kmax)
                        print('km = ',km)
                        raise
                    dr1 = rr[jjj]
                    dr2 = rr[jj]
                    #...choose deformed vector with smaller stretch
                    if dr2 > dr1:
                        iunique = iunique + 1
                        igood[i,jj] = 0
                    elif dr2 < dr1:
                        igood[i,jjj] = 0
                        iunique = iunique + 1
                    else:
                        if iiu == 0:
                            print('Lattice correspondance is not unique.')
                            iiu = 1
                        iunique = iunique + 1
            if igood[i,jj] == 1 and best[jj] < phi_min:
                igood[i,jj] = 0
            if igood[i,jj] == 1:
                p = nei_perf[kmax,:]
                b = b + np.outer(rij,p)
                a = a + np.outer(rij,rij)
        if isym > isym1:
            npr = npr + 1
            nsym[npr] = i
        try:
            ee = np.dot(np.linalg.inv(a),b) # local correspondence strain
        except:
            print('i = ',i)
            print('a  =',a)
            raise
        strn = (iden - np.dot(ee,ee)) /2 # finite local strain
        small= ( (iden - ee) + (iden -ee.T) ) /2 # local small strain
        inv1 = np.trace(small) # 1st invariant of strain
        inv2 = (np.sum(small*small) -inv1*inv1) /2 # 2nd invariant of strain
        rot = ( (iden - ee) - (iden - ee).T) /2 # local spin
        vspin = [ -rot[1,2], rot[0,2], -rot[0,1]] # local angular velocity
        emat[i,:,:] = ee[:,:]

    if npr > 0:
        print(' correspondence is not unique.')
        print(' number of problematic atoms =',npr)
    else:
        print(' no problematic atoms.')

    return emat

@numba.jit()
def compute_nye(co,co1,lsnn,lspr,lssft,emat,rc,period,
                xmin,xmax,ymin,ymax,zmin,zmax):
    """
    Compute Nye tensor as curl of G.

    Returns
    -------
    nye : ndarray

    """
    rcc = 1.1*rc
    # inner = [ i for i in range(len(co)) \
    #     if xmin+rcc < co[i,1] < xmax-rcc and \
    #        ymin+rcc < co[i,2] < ymax-rcc and \
    #        zmin-rcc/1000 < co[i,3] < zmax+rcc/1000 ]
    n=0
    for i in range(len(co)):
        if xmin+rcc < co[i,1] < xmax-rcc and \
           ymin+rcc < co[i,2] < ymax-rcc and \
           zmin-rcc/1000 < co[i,3] < zmax+rcc/1000:
            n += 1
    inner = np.zeros(n,dtype=int)
    n= 0
    for i in range(len(co)):
        if xmin+rcc < co[i,1] < xmax-rcc and \
           ymin+rcc < co[i,2] < ymax-rcc and \
           zmin-rcc/1000 < co[i,3] < zmax+rcc/1000:
            inner[n] = i
            n += 1

    mi = len(inner)
    # co2 = np.zeros((mi,4))
    # for i in range(mi):
    #     co2[i,:] = co[inner[i],0:4]

    grd = np.zeros((mi,3,3,3))
    for i1 in range(3):
        for j1 in range(3):
            for jj in range(mi):
                n = inner[jj]
                r = co[n,1:4]
                a = np.zeros((3,3))
                c = np.zeros((3))
                e0 = emat[n,i1,j1]
                for j in range(lsnn[n]):
                    nb = lspr[n,j]
                    nnb = find_idx_in_co(nb,co)
                    rn = co1[nb,1:4]
                    dr = rn[:] -r[:]
                    dr[2] += period*lssft[n,j]
                    a = a + np.outer(dr,dr)
                    de = emat[nnb,i1,j1] - e0
                    c = c + de*dr
                aa = np.dot(c,np.linalg.inv(a))
                grd[jj,i1,j1,:] = aa[:]

    #...compute curl and get Nye tensor
    nye = np.zeros((mi,3,3))
    for jj in range(mi):
        for mm in range(3):
            nye[jj,0,mm] = -grd[jj,2,mm,1] + grd[jj,1,mm,2]
            nye[jj,1,mm] = -grd[jj,0,mm,2] + grd[jj,2,mm,0]
            nye[jj,2,mm] = -grd[jj,1,mm,0] + grd[jj,0,mm,1]

    return nye,inner

#@numba.jit
def ismember(a,b):
    """
    Mimics ismember function in Matlab.
    Returns an array of the same length as A in which elements are 0 or 1.
    If an element of A exists in the array B, the element of returning array is 1,
    otherwise it is 0.
    """
    arr = np.zeros((len(a)),dtype=int)
    for i in range(len(a)):
        if a[i] in b:
            arr[i] = 1
    return arr

@numba.jit
def find_idx_in_co(idx,co):
    """
    Find the index such that co[index,0]==idx.
    """
    for i in range(len(co)):
        if co[i,0] == idx:
            return i
    return None

def dumpconf():
    global a0,c_o_a,phi_min,iwindow,xmin,xmax,ymin,ymax, \
        nei_perf,block,maxnn

    confname = 'nye_config.json'
    dic = {
        "a0": a0,
        "c_o_a": c_o_a,
        "phi_min": phi_min,
        "iwindow": iwindow,
        "xmin": xmin,
        "xmax": xmax,
        "ymin": ymin,
        "ymax": ymax,
        "nei_perf": nei_perf.tolist(),
        "block": block.tolist(),
        "maxnn":maxnn,
    }
    with open(confname,'w') as f:
        json.dump(dic,f,indent=2,sort_keys=True)
    print(' config file '+confname+' was written.')
    return None

def loadconf(confname):
    global a0,c_o_a,phi_min,iwindow,xmin,xmax,ymin,ymax, \
        nei_perf,block,maxnn

    with open(confname,'r') as f:
        js = json.load(f)
    a0 = js['a0']
    c_o_a = js['c_o_a']
    phi_min = js['phi_min']
    iwindow = js['iwindow']
    xmin = js['xmin']
    xmax = js['xmax']
    ymin = js['ymin']
    ymax = js['ymax']
    nei_perf = np.array(js['nei_perf'])
    block = np.array(js['block'])
    maxnn = js['maxnn']
    return None

def read_dat_nye(fname='dat.nye',):
    with open(fname,'r') as f:
        lines = f.readlines()
        natm = len(lines)
        pos = np.zeros((natm,3),dtype=float)
        nye = np.zeros((natm,3,3),dtype=float)
        for il,l in enumerate(lines):
            ds = l.split()
            pos[il,:] = [ float(ds[i]) for i in range(3) ]
            k = 3
            for i in range(3):
                for j in range(3):
                    nye[il,i,j] = float(ds[k])
                    k += 1
    return pos,nye

if __name__ == "__main__":

    args = docopt(__doc__)
    fname = args['INPUT']
    confname = args['CONFIG']
    print(args)

    exit

    if args['dumpconf']:
        dumpconf()

    elif args['calc']:
        if confname:
            loadconf(confname)
        #...output for checking
        print(' block:')
        for i in range(3):
            print(' {0:8.2f} {1:8.2f} {2:8.2f}'.format(block[i,0],
                                                       block[i,1],
                                                       block[i,2]))
        print(' nei_perf:')
        for i in range(len(nei_perf)):
            print(' {0:4d}:'.format(i)+
                  ' {0:8.2f} {1:8.2f} {2:8.2f}'.format(nei_perf[i,0],
                                                       nei_perf[i,1],
                                                       nei_perf[i,2]))
        atoms = read(fname,format='lammps-dump')
        nye_tensor(atoms)

    elif args['plot']:
        #...See http://matplotlib.org/examples/pylab_examples/griddata_demo.html
        #...for contour plot example.
        from matplotlib.mlab import griddata
        import matplotlib.pyplot as plt
        if confname:
            loadconf(confname)
        comp = args['--component']
        xo = float(args['-x'])
        yo = float(args['-y'])
        w = int(args['--width'])
        h = int(args['--height'])
        if len(comp) != 2:
            raise ValueError('len(comp) != 2')
        cmpnnt = [ int(comp[0])-1, int(comp[1])-1 ]
        pos,nye = read_dat_nye(fname='dat.nye')
        x = pos[:,0] -xo
        y = pos[:,1] -yo
        z = nye[:,cmpnnt[0],cmpnnt[1]]
        nei_perf *= a0
        b = nei_perf[0,:]
        b = np.sqrt(np.dot(b,b))
        print(' b = ',b)
        print(' xmin,xmax = ',x.min(),x.max())
        print(' ymin,ymax = ',y.min(),y.max())
        xi = np.linspace(x.min(),x.max(),100)
        yi = np.linspace(y.min(),y.max(),100)
        zi = griddata(x,y,z,xi,yi,interp='linear')
        cs = plt.contourf(xi,yi,zi, 15, cmap=plt.cm.rainbow,
                          vmax=abs(zi).max(),
                          vmin=-abs(zi).max())
        plt.colorbar(shrink=float(h)/w,aspect=20.*float(h)/w)
        plt.scatter(x,y,marker='o',s=40,zorder=10,
                    facecolors='white',edgecolors='black')
        print(' -w*b,w*b = ',-w*b,w*b)
        print(' -h*b,h*b = ',-h*b,h*b)
        plt.xlim(-w*b,w*b)
        plt.ylim(-h*b,h*b)
        xtics = [ i*b for i in range(-w,w+1,1) ]
        xticss = [ '{0:d}b'.format(i) for i in range(-w,w+1,1) ]
        ytics = [ i*b for i in range(-h,h+1,1) ]
        yticss = [ '{0:d}b'.format(i) for i in range(-h,h+1,1) ]
        plt.xticks( xtics, xticss, fontname='sans-serif',
                   fontsize=16 )
        plt.yticks( ytics, yticss, fontname='sans-serif',
                   fontsize=16 )
        plt.xlabel('[-1 1 0] -->', fontname='sans-serif',
                   fontsize=16)
        plt.ylabel('[ 1 1 1] -->', fontname='sans-serif',
                   fontsize=16)
        plt.title('Nye component '+comp)
        plt.axes().set_aspect(1.0)
        plt.axes().tick_params(axis='x',
                               which='major',
                               pad=10)
        plt.axes().tick_params(axis='y',
                               which='major',
                               pad=10)
        plt.show()


import stomp
import time

try:
  conexao = stomp.Connection()
  conexao.connect('','', wait=True)

  conexao.send(body='TESTE', destination ='/queue/DIA-NAMORADOS', idade = 50)
  print('Enviou msg - ')
#  time.sleep(2)
  conexao.disconnect()

except Exception as e:
  print('error')

print('FIM DA EXECUCAO')


import cv2
import numpy as np

cap = cv2.VideoCapture(0)  # Webcam Capture

totalNoOfImages = 2
onlyOnce = False
alreadyDetected = [False,False,False,False,False]

while (True):

    ret, frame = cap.read()
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

    for i in range(1,totalNoOfImages+1):
        template = cv2.imread('images/pic'+str(i)+'.png', 0)
        w, h = template.shape[::-1]

        if alreadyDetected[i] is False :

            res = cv2.matchTemplate(gray, template, cv2.TM_CCOEFF_NORMED)
            threshold = 0.6

            loc = np.where(res >= threshold)

            for pt in zip(*loc[::-1]):
                top_left = pt
                bottom_right = (top_left[0] + w, top_left[1] + h)
                demo = cv2.rectangle(frame, top_left, bottom_right, (0, 0, 255), 3)
                if i is 1 :
                    cv2.putText(frame, 'Mobile' , (top_left[0], top_left[1] - 10), cv2.FONT_HERSHEY_PLAIN,1.4, (0, 255, 0) , 2)
                elif i is 2 :
                    cv2.putText(frame, 'Vasu', (top_left[0], top_left[1] - 10), cv2.FONT_HERSHEY_PLAIN, 1.4,(0, 255, 0) , 2)

                if demo is not None :
                    if onlyOnce is True :
                        alreadyDetected[i] = True
                    print('Object' , i , 'Found')
                break

    cv2.imshow('Object Tracker', frame)

    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

cap.release()
cv2.destroyAllWindows()


'''
 @Author    Ray
 @Date      2022-07-25
 @Func      ts片段爬取
 @Version   v1.0
 @Note      无
'''

import os
import time
import copy
import requests
import concurrent.futures

from functools import partial
from misstv.config import crawl_header, sleep_time, worker_num

def scrape(ci, folder_path, download_list, urls):
    os.path.split(urls)
    fileName = urls.split('/')[-1][0:-3]
    saveName = os.path.join(folder_path, fileName + ".mp4")
    if os.path.exists(saveName):
        # 跳过已下载
        download_list.remove(urls)
    else:
        response = requests.get(urls, headers=crawl_header, timeout=100)
        if response.status_code == 200:
            content_ts = response.content
            if ci:
                content_ts = ci.decrypt(content_ts)  # 解碼
            with open(saveName, 'ab') as f:
                f.write(content_ts)
            download_list.remove(urls)
        elif response.status_code == 429:
            time.sleep(sleep_time)
        # 输出进度
        print('\r  -PROCESS: 正在下载 {0}, 剩余 {1} 个, status code: {2} \t'.format(
            urls.split('/')[-1], len(download_list), response.status_code), end='', flush=True)

def crawl(ci, folder_path, download_list):
    round = 0
    while(download_list != []):
        with concurrent.futures.ThreadPoolExecutor(max_workers=worker_num) as executor:
            executor.map(partial(scrape, ci, folder_path,
                                 download_list), download_list)
        round += 1
        print(f', round {round}')

def get_video_file(ci, folder_path, ts_list):
    download_list = copy.deepcopy(ts_list)
    start_time = time.time()
    print(' NOTICE: 开始下载 ' + str(len(download_list)) + ' 份文件, ', end='')
    print('预计等待时间 {0:.2f} 分钟(视影片长度和网络速度而定)'.format(len(download_list) / 120))

    crawl(ci, folder_path, download_list)   #开始爬取

    end_time = time.time()
    print(' NOTICE: 下载成功, 共计 {0:.2f} 分钟'.format((end_time - start_time) / 60))


from dimension_reduction import Reducer
from feature_selection import Selector
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error
from math import sqrt
import matplotlib.pyplot as plt

if __name__ == '__main__':
    # reducer = Reducer()
    reducer = Selector()
    input = []
    target = []
    for d in reducer.getLearnData():
        input.append(d[0])
        target.append(d[1][0])

    regressor = LinearRegression()
    regressor.fit(input, target)
    print(regressor.coef_)

    mult = 1#reducer.provider.multiplier

    results = regressor.predict(input)
    print(results)
    w = open('output2_1_s.txt', 'w')
    for i, line in enumerate(results):
        w.write('%f;%f\n' % (target[i]*mult, line*mult))
    w.close()

    input = []
    target = []
    for d in reducer.getTestData():
        input.append(d[0])
        target.append(d[1][0])
    results = regressor.predict(input)
    w = open('output2_2_s.txt', 'w')
    for i, line in enumerate(results):
        w.write('%f;%f\n' % (target[i]*mult, line*mult))
    w.close()
    w = open('linear_mse.txt', 'w')
    w.write('%f;\n' % sqrt(mean_squared_error(target, results)))
    w.close()
    plt.plot(target, 'b', results, 'r')
    plt.ylabel('Reikšmė')
    plt.xlabel('Masyvo elementas')
    plt.title('Multiple linear regression grafikas')
    plt.show()


from django.forms import (
    ModelForm,
    HiddenInput,
)
from gbe.models import ActBidEvaluation

class ActBidEvaluationForm(ModelForm):
    required_css_class = 'required'
    error_css_class = 'error'

    class Meta:
        model = ActBidEvaluation
        fields = ['notes', 'evaluator', 'bid']
        widgets = {'evaluator': HiddenInput(),
                   'bid': HiddenInput()}


from django.shortcuts import render, get_object_or_404
from .models import Car
from django.core.paginator import EmptyPage, PageNotAnInteger, Paginator
from django.http import JsonResponse

# Create your views here.

def cars(request):
    cars = Car.objects.order_by('-created_date')
    paginator = Paginator(cars, 4)
    page = request.GET.get('page')
    paged_cars = paginator.get_page(page)

    title_search = Car.objects.values_list('car_title', flat=True).distinct()
    model_search = Car.objects.values_list('car_model', flat=True).distinct()
    year_search = Car.objects.values_list('year', flat=True).distinct()
    city_search = Car.objects.values_list('city', flat=True).distinct()

    data = {
        'cars': paged_cars,
        'title_search': title_search,
        'model_search': model_search,
        'year_search': year_search,
        'city_search': city_search,
    }
    return render(request, 'cars/cars.html', data)

def car_detail(request, id):
    single_car = get_object_or_404(Car, pk=id)
    data = {
        'single_car': single_car,
    }
    return render(request, 'cars/car_detail.html', data)

def get_json_car_Data(request):
    qs_val = list(Car.objects.values('car_title').distinct())
    return JsonResponse({'data':qs_val})

def get_json_model_data(request, *args, **kwargs):
    selected_car = kwargs.get('car_title')
    obj_models = list(Car.objects.values('car_model').filter(car_title__icontains=selected_car).values().distinct())
    return JsonResponse({'data': obj_models})

def search(request):
    cars = Car.objects.order_by('-created_date')

    title_search = Car.objects.values_list('car_title', flat=True).distinct()
    model_search = Car.objects.values_list('car_model', flat=True).distinct()
    year_search = Car.objects.values_list('year', flat=True).distinct()
    city_search = Car.objects.values_list('city', flat=True).distinct()

    if 'keyword' in request.GET:
        keyword = request.GET['keyword']
        if keyword:
            cars = cars.filter(description__icontains=keyword)

    if 'title' in request.GET:
        title = request.GET['title']
        if title:
            cars = cars.filter(car_title__iexact=title)

    if 'model' in request.GET:
        model = request.GET['model']
        if model:
            cars = cars.filter(car_model__iexact=model)

    if 'location' in request.GET:
        location = request.GET['location']
        if location:
            cars = cars.filter(city__iexact=location)

    if 'year-min' in request.GET:
        yearmin = request.GET['year-min']
        if yearmin:
            cars = cars.filter(year__gte=yearmin)

    if 'year-max' in request.GET:
        yearmax = request.GET['year-max']
        if yearmax:
            cars = cars.filter(year__lte=yearmax)

    if 'min_price' in request.GET:
        min_price = request.GET['min_price']
        max_price = request.GET['max_price']
        if max_price:
           cars = cars.filter(price__gte=min_price, price__lte=max_price)

    data = {
        'cars': cars,
        'title_search': title_search,
        'model_search': model_search,
        'year_search': year_search,
        'city_search': city_search,
    }
    return render(request, 'cars/search.html', data)


import config
import telebot
import random

from pars import Pars
import time

bot = telebot.TeleBot(config.token)

@bot.message_handler(content_types=["text"])
def repeat_all_messages(message):
    m = bot.send_message(message.chat.id, "обработка... ")
    bot.edit_message_text(chat_id=m.chat.id, message_id=m.message_id, text=f"{m.text} {random.randint(1, 20)}%")

    time.sleep(2)
    bot.edit_message_text(chat_id=m.chat.id, message_id=m.message_id, text=f"{m.text} {random.randint(20, 50)}%")
    # ищем meme png
    id = message.text
    pars = Pars(id)
    pars0 = pars.str1
    bot.edit_message_text(chat_id=m.chat.id, message_id=m.message_id, text=f"{m.text} {random.randint(50, 80)}%")
    par = 'bot.send_media_group(message.chat.id,[' + pars0 + '])'
    bot.edit_message_text(chat_id=m.chat.id, message_id=m.message_id, text=f"{m.text} {random.randint(80, 99)}%")
    # задержка
    time.sleep(3)
    bot.edit_message_text(chat_id=m.chat.id, message_id=m.message_id, text=f"Готово! 100%")
    # кнопки
    m = telebot.types.ReplyKeyboardMarkup(resize_keyboard=True)
    btn1 = telebot.types.KeyboardButton("Собака")
    btn2 = telebot.types.KeyboardButton("Кот")
    btn3 = telebot.types.KeyboardButton(message.text)
    m.add(btn1, btn2, btn3)

    # приветствие
    bot.send_message(message.chat.id, text="Привет {0.first_name}!".format(message.from_user), reply_markup=m)
    bot.send_message(message.chat.id, message.text)
    # картинки meme
    if pars0 != 'err':
        #print(pars0)
        try:
            eval(par)
        except telebot.apihelper.ApiTelegramException:
            bot.edit_message_text(chat_id=m.chat.id, message_id=m.message_id, text=f"=( мне бан")
    else:
        bot.send_message(message.chat.id, text="_=( нет meme_", parse_mode="Markdown")
    # картинки meme

if __name__ == '__main__':
     bot.infinity_polling()


from transformers import AutoTokenizer, AutoModelForTokenClassification
from transformers import pipeline
import statistics
import re
from typing import List, Dict

# TODO: fix entity output from NERPipeline:
# TODO: no ''
# TODO: ammend output that is 'B' and '##ad' - revisit grouped_entities setting.

class NERPipeline(object):
    """
    attributes:
    self.tokenizer AutoTokenizer from_pretrained "APOLLO/bert-base-NER"
    self.model = AutoModelForTokenClassification from_pretrained "APOLLO/bert-base-NER"
    self.nlp transformers pipeline of the tokenizer and model
    self.tokens = []
    """
    def __init__(self):
        self.tokenizer = AutoTokenizer.from_pretrained("APOLLO/bert-base-NER", local_files_only=True)
        self.model = AutoModelForTokenClassification.from_pretrained("APOLLO/bert-base-NER", local_files_only=True)
        self.nlp = pipeline("ner",model=self.model,tokenizer=self.tokenizer)  #, grouped_entities=True)
        self.tokens = []
        self.entities = []

    def flatten(self, lol:List) -> List:
        """
        take any list of lists (of lists), return a single list with all values
        :param lol: List of lists
        :return: a flat list
        """
        if isinstance(lol, list):
            return [a for i in lol for a in self.flatten(i)]
        else:
            return [lol]

    # def _store_tokens(self, txt:str):
    #     """
    #     update list of encoded tokens including start and stop tokens.
    #     """
    #     self.tokens.append(self.tokenizer(txt)['input_ids'])
    def _store_tokens(self, tokens:List):
        """
        store tokens in object attribute
        :param tokens : List of tokens
        :type tokens: list
        :return: None.
        """
        self.tokens.append(tokens)

    def _store_entities(self, entities:List):
        """
        store entites in object attribute
        :param tokens : List of tokens
        :type tokens: list
        :return: None.
        """
        self.entities.append(entities)

    def delete_tokens(self):
        """
        erase collected tokens list
        """
        self.tokens = []

    def delete_entities(self):
        """
        erase collected tokens list
        """
        self.entities = []

    def get_tokens(self, txt:str) -> List:
        """
        :param txt: arbitary str
        :type txt: str
        :return: List of encoded tokens
        :return type: List
        """
        return self.tokenizer(txt)['input_ids']

    def get_decoded_tokens(self) -> List:
        """
        decode tokens to original words
        :return: List [[token_text[], [token_text], ...]
        :return type: List
        """
        decoded_tokens = []
        for tokens in self.tokens:
            for token in tokens:
                decoded_tokens.append(self.tokenizer.decode(token))
        return decoded_tokens

    def get_token_stats(self) -> Dict:
        """
        returns Dict of token statistics
        """
        token_stats={}

        document_lengths = []
        doc_tokens = []
        for tokens in self.tokens:
            document_lengths.append(len(tokens))
            doc_tokens.append(tokens)

        token_stats['total_doc_count'] = len(document_lengths)
        token_stats['total tokens'] = sum(document_lengths)
        token_stats['total unique tokens'] = len(set(self.flatten(doc_tokens)))
        median = statistics.median(document_lengths)
        LQ = statistics.median([x for x in document_lengths if x <= median])
        UQ = statistics.median([x for x in document_lengths if x >= median])
        token_stats['token count median'] = median
        token_stats['token count lower quartile'] = LQ
        token_stats['token count upper quartile'] = UQ
        token_stats['token count IQR'] = UQ-LQ
        return token_stats

    def nerfunc(self, txt):
        ner_res=self.nlp(txt)
        # return list of dicts [ {'word': 'China', 'score': 0.9999999, 'entity': 'B-LOC', index: 45 start: 74 end: 79}, {etc}]
        output=[]
        scores=[]
        types=[]
        tmp=''  #store word to build up sequence.
        s=0  # score tracking
        t=''  # entity types
        counter=1
        # print(f'ner_res: {ner_res}')
        for word_count, rec in enumerate(ner_res):
            # print(f'rec :{rec}')
            if rec['word'][0] == '#':
                tmp+=rec['word'][2:]
                if word_count == len(ner_res)-1:
                    # print(f'final tmp: {tmp}')
                    output.append(tmp)
                    scores.append(s/counter)
                    types.append(t)
                    tmp=rec['word']
                    s=rec['score']
                    if rec['entity']=='B-ORG':
                        t='Organization'
                    if rec['entity']=='B-PER':
                        t='Person'
                    if rec['entity']=='B-LOC':
                        t='Location'
                    if rec['entity']=='B-MISC':
                        t='Miscellaneous'
                continue

            if 'B-' in rec['entity']:
                # print(f'intermediate tmp: {tmp}')
                output.append(tmp)
                scores.append(s/counter)
                types.append(t)
                tmp=rec['word']
                s=rec['score']
                if rec['entity']=='B-ORG':
                    t='Organization'
                if rec['entity']=='B-PER':
                    t='Person'
                if rec['entity']=='B-LOC':
                    t='Location'
                if rec['entity']=='B-MISC':
                    t='Miscellaneous'
            else:
                a=rec['word']
                s+=rec['score']
                if a[0]=='#':
                    a=a[2:]
                    tmp+=a
                    # print(f'# add tmp: {tmp}')
                else:
                    tmp+=' '
                    tmp+=a
                counter+=1
            if word_count == len(ner_res)-1:
                # print(f'final tmp: {tmp}')
                output.append(tmp)
                scores.append(s/counter)
                types.append(t)
                tmp=rec['word']
                s=rec['score']
                if rec['entity']=='B-ORG':
                    t='Organization'
                if rec['entity']=='B-PER':
                    t='Person'
                if rec['entity']=='B-LOC':
                    t='Location'
                if rec['entity']=='B-MISC':
                    t='Miscellaneous'

        return(output,types,scores, self.get_tokens(txt))

    @staticmethod
    def _chunk_out_text(text:str, chunk_size:int = 512) -> List:
        """
        :param text: any string
        :param chunk_size: any positive int
        split up test a list of 0-to-n chunk_size strings and one string with the remaining <chunk_size chunk
        """
        i = 0
        chunks = []
        while i < len(text):
            if i+chunk_size < len(text):
                chunks.append(text[i:i+chunk_size])
            else:
                chunks.append(text[i:len(text)])
            i += chunk_size
        return chunks

    def ner_over_chunks(self, actual_text:str) -> List:
        """
        Take a string of any length, cut it into 512 char sections, run NER on each section then reassemble the
        results into one list.
        :param actual_text: any string - here all concatenated text associated with an incident_id_column_name
        :return: List of three lists [[entities], [entity types], [scores]]
        """
        actual_text = re.sub(r"[|]+", self.tokenizer.sep_token, actual_text)  # aggregated source text is separated by |'s.
        chunks = self._chunk_out_text(actual_text)
        aggregated_results = [[],[],[],[]]
        chunk_ners = []
        for c in chunks:
            # NERPipeline returns 3 value list
            # chunk_ners is a list of three value lists
            res = self.nerfunc(c)
            chunk_ners.append(res)

        for c in chunk_ners:
            for j in range(4):
                aggregated_results[j].extend(c[j])

        entities = aggregated_results[0]
        types = aggregated_results[1]
        scores = aggregated_results[2]
        tokens = aggregated_results[3]

        self._store_entities(entities)

        self._store_tokens(tokens)

        return entities, types, scores

if __name__=="__main__":
    np = NERPipeline()
    texts = [
        "Bucky's a badger. that lives in Madison, WI| Bubbles is a clown"
        ]
    for txt in texts:
        output,types,scores=np.ner_over_chunks(txt)
        print(f'output: {output}')
        print(f'types: {types}')
        print(f'scores: {scores}')
        print(f'bert tokens: {np.tokens}')
        print(f'tokens: {np.get_decoded_tokens()}')
        print(f'token_stats: {np.get_token_stats()}')
        print(f'entities: {np.entities}')


from mongoengine.document import Document, EmbeddedDocument
from mongoengine.fields import StringField, ListField, EmbeddedDocumentField

# NOC modules
from noc.sa.models.managedobjectprofile import ManagedObjectProfile
from .firmware import Firmware
from .platform import Platform
from noc.core.mongo.fields import ForeignKeyField, PlainReferenceField
from noc.core.text import alnum_key

FS_RECOMMENDED = "r"
FS_ACCEPTABLE = "a"
FS_NOT_RECOMMENDED = "n"
FS_DENIED = "d"

class ManagementPolicy(EmbeddedDocument):
    protocol = StringField(choices=[("cli", "CLI"), ("snmp", "SNMP"), ("http", "HTTP")])

class FirmwarePolicy(Document):
    meta = {
        "collection": "noc.firmwarepolicy",
        "strict": False,
        "auto_create_index": False,
        "indexes": ["platform", "firmware"],
    }
    # Platform (Matched with get_version)
    platform = PlainReferenceField(Platform)
    #
    description = StringField()
    #
    object_profile = ForeignKeyField(ManagedObjectProfile)
    #
    firmware = PlainReferenceField(Firmware)
    status = StringField(
        choices=[
            (FS_RECOMMENDED, "Recommended"),
            (FS_ACCEPTABLE, "Acceptable"),
            (FS_NOT_RECOMMENDED, "Not recommended"),
            (FS_DENIED, "Denied"),
        ]
    )
    #
    management = ListField(EmbeddedDocumentField(ManagementPolicy))

    @classmethod
    def get_status(cls, platform, version):
        if not platform or not version:
            return None
        fp = FirmwarePolicy.objects.filter(platform=platform.id, firmware=version.id).first()
        if fp:
            return fp.status
        else:
            return None

    @classmethod
    def get_recommended_version(cls, platform):
        """
        Get recommended version for  platform

        :param platform: Platform instance
        :return: Version string or None
        """
        if not platform:
            return None
        fp = FirmwarePolicy.objects.filter(platform=platform.id, status=FS_RECOMMENDED).first()
        if fp:
            return fp.firmware.version
        versions = []
        for fp in FirmwarePolicy.objects.filter(platform=platform.id, status=FS_ACCEPTABLE):
            versions += [fp.firmware.version]
        if versions:
            # Get latest acceptable version
            return list(sorted(versions, key=lambda x: alnum_key(x)))[-1]
        return None


"""qtbot should work for testing!"""
from threading import Thread, current_thread, main_thread
from typing import TYPE_CHECKING, Any, Callable, Tuple
from unittest.mock import Mock

import pytest
from typing_extensions import Literal

from psygnal import Signal
from psygnal._signal import _guess_qtsignal_signature

pytest.importorskip("pytestqt")
if TYPE_CHECKING:
    from pytestqt.qtbot import QtBot

def _equals(*val: Any) -> Callable[[Tuple[Any, ...]], bool]:
    def _inner(*other: Any) -> bool:
        return other == val

    return _inner

def test_wait_signals(qtbot: "QtBot") -> None:
    class Emitter:
        sig1 = Signal()
        sig2 = Signal(int)
        sig3 = Signal(int, int)

    e = Emitter()

    with qtbot.waitSignal(e.sig2, check_params_cb=_equals(1)):
        e.sig2.emit(1)

    with qtbot.waitSignal(e.sig3, check_params_cb=_equals(2, 3)):
        e.sig3.emit(2, 3)

    with qtbot.waitSignals([e.sig3], check_params_cbs=[_equals(2, 3)]):
        e.sig3.emit(2, 3)

    signals = [e.sig1, e.sig2, e.sig3, e.sig1]
    checks = [_equals(), _equals(1), _equals(2, 3), _equals()]
    with qtbot.waitSignals(signals, check_params_cbs=checks, order="strict"):
        e.sig1.emit()
        e.sig2.emit(1)
        e.sig3.emit(2, 3)
        e.sig1.emit()

def test_guess_signal_sig(qtbot: "QtBot") -> None:
    from qtpy import QtCore

    class QtObject(QtCore.QObject):
        qsig1 = QtCore.Signal()
        qsig2 = QtCore.Signal(int)
        qsig3 = QtCore.Signal(int, str)

    q_obj = QtObject()
    assert "qsig1()" in _guess_qtsignal_signature(q_obj.qsig1)
    assert "qsig1()" in _guess_qtsignal_signature(q_obj.qsig1.emit)
    assert "qsig2(int)" in _guess_qtsignal_signature(q_obj.qsig2)
    assert "qsig2(int)" in _guess_qtsignal_signature(q_obj.qsig2.emit)
    assert "qsig3(int,QString)" in _guess_qtsignal_signature(q_obj.qsig3)
    assert "qsig3(int,QString)" in _guess_qtsignal_signature(q_obj.qsig3.emit)

def test_connect_qt_signal_instance(qtbot: "QtBot") -> None:
    from qtpy import QtCore

    class Emitter:
        sig1 = Signal()
        sig2 = Signal(int)
        sig3 = Signal(int, int)

    class QtObject(QtCore.QObject):
        qsig1 = QtCore.Signal()
        qsig2 = QtCore.Signal(int)

    q_obj = QtObject()
    e = Emitter()

    # the hard case: signal.emit takes less args than we emit
    def test_receives_1(value: int) -> bool:
        # making sure that qsig2.emit only receives and emits 1 value
        return value == 1

    e.sig3.connect(q_obj.qsig2.emit)
    with qtbot.waitSignal(q_obj.qsig2, check_params_cb=test_receives_1):
        e.sig3.emit(1, 2)  # too many

    # the "standard" cases, where params match
    e.sig1.connect(q_obj.qsig1.emit)
    with qtbot.waitSignal(q_obj.qsig1):
        e.sig1.emit()

    e.sig2.connect(q_obj.qsig2.emit)
    with qtbot.waitSignal(q_obj.qsig2):
        e.sig2.emit(1)

    # the flip case: signal.emit takes more args than we emit
    with pytest.raises(ValueError):
        e.sig1.connect(q_obj.qsig2.emit)
    e.sig1.emit()

@pytest.mark.parametrize("thread", [None, "main"])
def test_q_main_thread_emit(
    thread: Literal["main", None], qtbot: "QtBot", qapp
) -> None:
    """Test using signal.emit(..., queue=True)

    ... and receiving it on the main thread with a QTimer connected to `emit_queued`
    """
    from psygnal.qt import start_emitting_from_queue, stop_emitting_from_queue

    class C:
        sig = Signal(int)

    obj = C()
    mock = Mock()

    @obj.sig.connect(thread=thread)
    def _some_slot(val: int) -> None:
        mock(val)
        assert (current_thread() == main_thread()) == (thread == "main")

    def _emit_from_thread() -> None:
        assert current_thread() != main_thread()
        obj.sig.emit(1)

    with qtbot.waitSignal(obj.sig, timeout=1000):
        t = Thread(target=_emit_from_thread)
        t.start()
        t.join()

    qapp.processEvents()
    if thread is None:
        mock.assert_called_once_with(1)
    else:
        mock.assert_not_called()
        start_emitting_from_queue()
        qapp.processEvents()
        mock.assert_called_once_with(1)

        start_emitting_from_queue(10)  # just for test coverage
        stop_emitting_from_queue()


import config
from time import sleep
import time
import subprocess
import threading #Allow multithreading functionality
from MCP25625_api import MCP25625_api, Message #import CAN HAL
import subprocess
import os
#Allow time dependant interrupt controller
flag1 = threading.Event()

#Prevent Send and Recieve from accessing mcp at same time
mcp_lock = threading.Lock()

cwd = os.getcwd()
if config.dynamicpathing:
    SINEFILEPATH = cwd
else:
    SINEFILEPATH = config.SINEFILEPATH_Default # SET THIS # SET THIS

pythonx_radio_process = None

def RF_KILL_formal():
    global pythonx_radio_process
    if pythonx_radio_process is None:
        return
    pythonx_radio_process.terminate()
    if pythonx_radio_process.returncode is None:
        pythonx_radio_process.kill()
        pythonx_radio_process = None
    return
# A dictionary join for simplification of reference, n+1 dicrionary overwrites n dictionary
commandMap = {**config.py2commandMap, **config.py3commandMap, **config.COM2commandMacroMap}

def run_pyx_file(name, pytype):
    global pythonx_radio_process
    pythonx_command = pytype +" {}/{} ".format(SINEFILEPATH, name)  # launch your python3 script using bash
    pythonx_radio_process = subprocess.Popen(pythonx_command.split(), stdout=subprocess.PIPE)
    # if not pythonx_radio_process is None:
    #       output, error = pythonx_radio_process.communicate()  # receive output from the python3 script
    return

def get_byte_temp():
    cpu_temp_object = open('/sys/class/thermal/thermal_zone0/temp')
    try:
        raw_temp = int(cpu_temp_object.read())
    except: #Data Insurance
        print('temp is NAN')
    cpu_temp_object.close() #Range output temp is (-40, 215)
    b10_temp =round(raw_temp / 1000 + 40) #Supposed to convert int to bytes for sending via buffer
    print("Temp: " + str(raw_temp/1000) + " Hex: " + hex(b10_temp) )
    return b10_temp

#CAN controller Initialization
#There needs to be some kind of event listener daemon? on tap so that this puppy
#can appropriately wait for CAN packets...
##CAN_control()

#Thread 2
#Ten second interrupt
#There is drift in this function, not meant for real time calculations
#Set up object to output
sender = MCP25625_api()
sender.SetFilterIdF0(0x120801F1)
sender.Initialize()
sender.SetNormalMode()
def interrupt_10s():
    #print("Interrupt 10s")
# the temperature data to send
    global mcp_lock
    mcp_lock.acquire()
    msg = Message(config.CAN_ID_output_telemetry, [get_byte_temp()])  # , extended_id=False)
    try:
        sender.Send(msg, timeoutMilliseconds=1000)
        print("    SEND: {}".format(msg))
    except TimeoutError as e:
        print("    SEND: Timeout sending message. <{}>".format(e))
    finally:
        mcp_lock.release()
        threading.Timer(10, interrupt_10s).start()

#Thread 3
#Ten minutes interrupt
##Interrupt behavior needs to be defined.%%
def interrupt_10min():
    print("Interrupt 10min")
    global flag1
    flag1.set()
    #thread1.cancl()

def listener():#Target of CAN control thread 1
    # initializing CAN message data object and reader
    reader = MCP25625_api()
    reader.SetFilterIdF0(0x120801F1)
    reader.Initialize()
    reader.SetNormalMode()
    global mcp_lock
    while(True):
        try:
            mcp_lock.acquire()
            recvMsg = reader.Recv(timeoutMilliseconds=5000)
        except TimeoutError:
            print("Loop Timeout:  Restarting Loop")
        else:
            parsePacket(recvMsg)
        finally:
            mcp_lock.release()
            time.sleep(.005) #other thread to aquire lock

def parsePacket(msg):
    m_id = msg.arbitration_id
    if m_id == config.CAN_ID_commands:
        try:#RADIO Message "transceiver" command subroutine
            possiblecommand = msg.data[0]
            commandActual = commandMap[possiblecommand]
            RF_KILL_formal()
            print("Command " + str(possiblecommand) + ": " + str(commandActual) )
            if possiblecommand < config.PYTHON_2_LENGTH:
                run_pyx_file(commandActual, "python2")
                return #None case: command is macro and is executed
            elif possiblecommand < config.PYTHON_3_LENGTH:
                run_pyx_file(commandActual, "python3")
                 #maybe py2 commandlength should be config variable?
            elif possiblecommand < config.MACRO_LENGTH:
                commandActual()

            #This code assumes all m_id=1 events correspond to a python 2.7 file or python 3.6X file or Subsystem Command
        except KeyError:
            print("Invalid Data")
    elif m_id == config.CAN_ID_pictures:
        print("Picture data here")
    else:
        print("Incorrect filter")

#Timer class
##WARNING TIMERS ARE NOT CLOCK BASED, ACCURACY IS NOT GUARANTEED
thread2 = threading.Timer(600, interrupt_10min)

##Repeating 10.
thread3 = threading.Timer(10, interrupt_10s)
thread3.daemon = True

#Init Block
thread2.start()
thread3.start()
thread1 = threading.Thread(name='observer', target=listener())
thread1.start()
print("CAN controller initialized")