from abc import abstractmethod
from typing import Dict, Optional
from PIL import Image
import random

from tqdm import trange
import numpy as np

from sklearn.manifold import TSNE

import torch
from torchvision import transforms, utils
from torch.utils.data import DataLoader
from chamferdist import ChamferDistance
from geomloss import SamplesLoss

from .base_trainer import BaseTrainer
from src.data import get_dataset, infinite_loader, GANDataset
from src.data.dataset_updated import MakeDataLoader
from src.models.fid import get_fid_fn, load_patched_inception_v3
from src.models import inception_score
from src.models import vgg16
from src.utils import PathOrStr
from src.utils import tsne_display_tensorboard
from src.utils import calculate_frechet_distance, slerp

torch.backends.cudnn.benchmark = True

class GeneratorTrainer(BaseTrainer):
    """Abstract class for generator trainers"""

    def __init__(self,
                 config_path: PathOrStr,
                 config: Dict):

        super().__init__(config_path, config)

        # mock objects
        self._g_ema = None
        self._encoder = None
        self._classifier = None

        # load sampling dataset, so when sampling we select real labels
        self._sample_ds = self._get_ds()
        self._columns = self._sample_ds.columns

    @abstractmethod
    def train(self):
        """Trains the model
        Main method, used for training the model"""
        pass

    @abstractmethod
    def _load_model(self):
        """Loads model"""
        pass

    @abstractmethod
    def _save_model(self):
        """Saves model to checkpoint"""
        pass

    def evaluate(self) -> None:
        """Evaluates the model by computing:

        - FID score
        - Inception Score
        - Chamfer distance
        - attribute control accuracy

        and exploring:
        - TSNE of the latent space
        - explore generated images
        - traverse z1, ..., zk latent dimensions
        - epsilon input values
        - epsilon and z1, ..., zk latent dimensions
        """

        fid_score = self._compute_fid_score()
        self._writer.add_scalar('FID', fid_score, 0)

        i_score = self._compute_inception_score()
        self._writer.add_scalar('IS', i_score, 0)

        self._display_output_eps()
        self._explore_y()
        chamfer_dist = self._compute_chamfer_distance()
        self._writer.add_scalar('Chamfer', float(chamfer_dist), 0)

        ssl_fid = self._compute_ssl_fid()
        self._writer.add_scalar('SSL_FID', ssl_fid, 0)

        ssl_ppl = self._compute_ssl_ppl()
        self._writer.add_scalar('SSL_PPL', ssl_ppl, 0)

        vgg_ppl = self._compute_vgg16_ppl()
        self._writer.add_scalar('VGG_PPL', vgg_ppl, 0)

        kid_inception = self._compute_inception_kid()
        self._writer.add_scalar('KID_Inception', kid_inception, 0)

        kid_ssl = self._compute_ssl_kid()
        self._writer.add_scalar('KID_SSL', kid_ssl, 0)

        self._attribute_control_accuracy()

        self._traverse_zk()
        self._explore_eps()
        self._explore_eps_zs()

    @torch.no_grad()
    def _compute_ssl_kid(self) -> float:
        """Computes Kernel Inception Distance using features computed using pretrained SimCLR

        Returns:
            float: KID
        """

        loader = self._get_dl()

        bs = self._config['batch_size']
        num_samples = 50_000
        n_batches = int(num_samples / bs) + 1

        real_features = []
        for _ in trange(n_batches):
            img, _ = next(loader)
            img = img.to(self._device)

            with torch.no_grad():
                h, _ = self._encoder(img)
            real_features.extend(h.cpu().numpy())
        real_features = np.array(real_features)

        gen_features = []
        for _ in trange(n_batches):
            label = self._sample_label()

            with torch.no_grad():
                img = self._g_ema(label)
                h, _ = self._encoder(img)
            gen_features.extend(h.cpu().numpy())
        gen_features = np.array(gen_features)

        m = 1000  # max subset size
        num_subsets = 100

        n = real_features.shape[1]
        t = 0
        for _ in range(num_subsets):
            x = gen_features[np.random.choice(gen_features.shape[0], m, replace=False)]
            y = real_features[np.random.choice(real_features.shape[0], m, replace=False)]
            a = (x @ x.T / n + 1) ** 3 + (y @ y.T / n + 1) ** 3
            b = (x @ y.T / n + 1) ** 3
            t += (a.sum() - np.diag(a).sum()) / (m - 1) - b.sum() * 2 / m
        kid = t / num_subsets / m
        return kid

    @torch.no_grad()
    def _compute_inception_kid(self) -> float:
        """Computes Kernel Inception Distance using features computed using pretrained InceptionV3

        Returns:
            float: KID (lower - better)
        """

        encoder = load_patched_inception_v3().to(self._device).eval()

        loader = self._get_dl()

        bs = self._config['batch_size']
        num_samples = 20_000
        n_batches = int(num_samples / bs) + 1

        real_features = []
        for _ in trange(n_batches):
            img, _ = next(loader)
            img = img.to(self._device)

            if img.shape[2] != 299 or img.shape[3] != 299:
                img = torch.nn.functional.interpolate(img, size=(299, 299), mode='bicubic')

            with torch.no_grad():
                feature = encoder(img)[0].flatten(start_dim=1)
                real_features.extend(feature.cpu().numpy())
        real_features = np.array(real_features)

        gen_features = []
        for _ in trange(n_batches):
            label = self._sample_label()

            with torch.no_grad():
                img = self._g_ema(label)

                if img.shape[2] != 299 or img.shape[3] != 299:
                    img = torch.nn.functional.interpolate(img, size=(299, 299), mode='bicubic')

                feature = encoder(img)[0].flatten(start_dim=1)
                gen_features.extend(feature.cpu().numpy())
        gen_features = np.array(gen_features)

        m = 1000  # max subset size
        num_subsets = 100

        n = real_features.shape[1]
        t = 0
        for _ in range(num_subsets):
            x = gen_features[np.random.choice(gen_features.shape[0], m, replace=False)]
            y = real_features[np.random.choice(real_features.shape[0], m, replace=False)]
            a = (x @ x.T / n + 1) ** 3 + (y @ y.T / n + 1) ** 3
            b = (x @ y.T / n + 1) ** 3
            t += (a.sum() - np.diag(a).sum()) / (m - 1) - b.sum() * 2 / m
        kid = t / num_subsets / m
        return float(kid)

    @torch.no_grad()
    def _compute_vgg16_ppl(self) -> float:
        """Computes perceptual path length using features computed using pretrained VGG16

        Returns:
            float: perceptual path length (smaller better)
        """

        encoder = vgg16().to(self._device).eval()

        bs = self._config['batch_size']
        num_samples = 50_000
        epsilon = 1e-4
        n_batches = int(num_samples / bs) + 1

        dist = []
        for _ in trange(n_batches):
            label = self._sample_label()
            labels_cat = torch.cat([label, label])

            t = torch.rand([label.shape[0]], device=label.device)
            eps0 = self._g_ema.sample_eps(bs)
            eps1 = self._g_ema.sample_eps(bs)

            z0 = z1 = self._g_ema.sample_zs(bs)

            epst0 = slerp(eps0, eps1, t.unsqueeze(1))
            epst1 = slerp(eps0, eps1, t.unsqueeze(1) + epsilon)

            with torch.no_grad():
                img = self._g_ema(labels_cat, torch.cat([epst0, epst1]), torch.cat([z0, z1]))
                if img.shape[2] != 256 or img.shape[3] != 256:
                    img = torch.nn.functional.interpolate(img, size=(256, 256), mode='bicubic')

                lpips0, lpips1 = encoder(img).chunk(2)

            d = (lpips0 - lpips1).square().sum(1) / epsilon ** 2
            dist.extend(d.cpu().numpy())

        dist = np.array(dist)
        lo = np.percentile(dist, 1, interpolation='lower')
        hi = np.percentile(dist, 99, interpolation='higher')
        ppl = np.extract(np.logical_and(dist >= lo, dist <= hi), dist).mean()
        return ppl

    @torch.no_grad()
    def _compute_ssl_ppl(self) -> float:
        """Computes perceptual path length using features computed using SimCLR

        Returns:
            float: perceptual path length (smaller better)
        """

        bs = self._config['batch_size']
        num_samples = 50_000
        epsilon = 1e-4
        n_batches = int(num_samples / bs) + 1

        dist = []
        for _ in trange(n_batches):
            label = self._sample_label()
            labels_cat = torch.cat([label, label])

            t = torch.rand([label.shape[0]], device=label.device)
            eps0 = self._g_ema.sample_eps(bs)
            eps1 = self._g_ema.sample_eps(bs)

            z0 = z1 = self._g_ema.sample_zs(bs)

            epst0 = slerp(eps0, eps1, t.unsqueeze(1))
            epst1 = slerp(eps0, eps1, t.unsqueeze(1) + epsilon)

            with torch.no_grad():
                img = self._g_ema(labels_cat, torch.cat([epst0, epst1]), torch.cat([z0, z1]))
                h, _ = self._encoder(img)

            h0, h1 = h.chunk(2)
            d = (h0 - h1).square().sum(1) / epsilon ** 2
            dist.extend(d.cpu().numpy())

        dist = np.array(dist)
        lo = np.percentile(dist, 1, interpolation='lower')
        hi = np.percentile(dist, 99, interpolation='higher')
        ppl = np.extract(np.logical_and(dist >= lo, dist <= hi), dist).mean()

        return ppl

    @torch.no_grad()
    def _compute_ssl_fid(self) -> float:
        """Computes FID on SSL features

        Returns:
            float: FID
        """
        n_samples = 50_000
        bs = self._config['batch_size']
        n_batches = int(n_samples / bs) + 1
        dl = self._get_dl()

        # compute activations
        activations_real = []
        activations_fake = []

        for _ in trange(n_batches):
            img, lbl = next(dl)
            img = img.to(self._device)
            lbl = lbl.to(self._device)

            with torch.no_grad():
                img_gen = self._g_ema(lbl)

                h, _ = self._encoder(img)
                h_gen, _ = self._encoder(img_gen)

            activations_real.extend(h.cpu().numpy())
            activations_fake.extend(h_gen.cpu().numpy())

        activations_real = np.array(activations_real)
        activations_fake = np.array(activations_fake)

        mu_real = np.mean(activations_real, axis=0)
        sigma_real = np.cov(activations_real, rowvar=False)

        mu_fake = np.mean(activations_fake, axis=0)
        sigma_fake = np.cov(activations_fake, rowvar=False)
        fletcher_distance = calculate_frechet_distance(mu_fake, sigma_fake, mu_real, sigma_real)
        return fletcher_distance

    @torch.no_grad()
    def _compute_geometric_distance(self) -> torch.Tensor:

        n_batches = 200

        loss = SamplesLoss("sinkhorn", p=2, blur=0.05, scaling=0.8, backend="tensorized")
        loader = self._get_dl()

        # real data embeddings
        real_embeddings = []
        for _ in trange(n_batches):
            img, _ = next(loader)
            img = img.to(self._device)

            with torch.no_grad():
                h, _ = self._encoder(img)

            real_embeddings.extend(h.cpu())
        real_embeddings = torch.stack(real_embeddings)

        # fake embeddings
        fake_embeddings = []
        for _ in trange(n_batches):
            label_oh = self._sample_label()

            with torch.no_grad():
                img = self._g_ema(label_oh)
                h, _ = self._encoder(img)

            fake_embeddings.extend(h.cpu())
        fake_embeddings = torch.stack(fake_embeddings)
        distance = loss(real_embeddings, fake_embeddings)
        return distance

    @torch.no_grad()
    def _attribute_control_accuracy(self) -> Dict:
        """Computes attribute control accuracy

        Returns:
            Dict: attribute control accuracy for each label
        """

        n_out = self._config['dataset']['n_out']
        bs = self._config['batch_size']
        diffs = []

        for _ in trange(n_out):
            label = self._sample_label(bs)
            label = label.to(self._device)

            with torch.no_grad():
                img = self._g_ema(label)
                h, _ = self._encoder(img)
                pred = self._classifier(h)

            diff = (label - pred) ** 2
            diffs.append(diff.detach().cpu().numpy())

        diffs = np.array(diffs)
        mean_diffs = np.mean(diffs, axis=1)

        result = {}
        for i in n_out:
            result[self._columns[i]] = mean_diffs[i]
        return result

    @torch.no_grad()
    def _compute_chamfer_distance(self) -> float:
        """Computes Chamfer distance between real and generated data

        Returns:
            float: computed Chamfer distance
        """

        n_batches = 20

        loader = self._get_dl()
        embeddings = []
        # real data embeddings
        for _ in trange(n_batches):
            img, _ = next(loader)
            img = img.to(self._device)

            with torch.no_grad():
                h, _ = self._encoder(img)

            embeddings.extend(h.cpu().numpy())

        # generated data embeddings
        for _ in trange(n_batches):
            label = self._sample_label()

            with torch.no_grad():
                img = self._g_ema(label)
                h, _ = self._encoder(img)

            embeddings.extend(h.cpu().numpy())

        tsne_emb = TSNE(n_components=3).fit_transform(embeddings)
        n = len(tsne_emb)

        tsne_real = tsne_emb[:n//2, ]
        tsne_fake = tsne_emb[n//2:, ]

        tsne_real = torch.from_numpy(tsne_real).unsqueeze(0)
        tsne_fake = torch.from_numpy(tsne_fake).unsqueeze(0)

        chamfer_dist = ChamferDistance()
        return chamfer_dist(tsne_real, tsne_fake).detach().item()

    @torch.no_grad()
    def _explore_eps_zs(self) -> None:
        """Explores generated images with fixed epsilon and x1, ..., xk vectors"""

        traverse_samples = 8
        y = self._sample_label(traverse_samples)

        log_folder = self._writer.checkpoint_folder.parent / 'explore_eps_zs'
        log_folder.mkdir(exist_ok=True, parents=True)

        imgs = []

        for i in range(8):
            zs = self._g_ema.sample_zs(1)
            zs = torch.repeat_interleave(zs, traverse_samples, dim=0)

            eps = self._g_ema.sample_eps(1)
            eps = torch.repeat_interleave(eps, traverse_samples, dim=0)

            with torch.no_grad():
                img = self._g_ema(y, eps, zs).cpu()
                img = torch.cat([_img for _img in img], dim=1)

            imgs.append(img)

        imgs = torch.cat(imgs, dim=2)
        imgs = (imgs.permute(1, 2, 0).numpy() * 127.5 + 127.5).astype(np.uint8)

        Image.fromarray(imgs).save(
            log_folder / 'explore_eps_zs.png'
        )

    @torch.no_grad()
    def _explore_eps(self) -> None:
        """Explores generated images with fixed epsilon"""

        traverse_samples = 8
        y = self._sample_label(traverse_samples)

        log_folder = self._writer.checkpoint_folder.parent / 'explore_eps'
        log_folder.mkdir(exist_ok=True, parents=True)

        zs = self._g_ema.sample_zs(traverse_samples)
        imgs = []

        for i in range(traverse_samples):
            eps = self._g_ema.sample_eps(1)
            eps = torch.repeat_interleave(eps, traverse_samples, dim=0)

            with torch.no_grad():
                img = self._g_ema(y, eps, zs).cpu()
                img = torch.cat([_img for _img in img], dim=1)

            imgs.append(img)
        imgs = torch.cat(imgs, dim=2)
        imgs = (imgs.permute(1, 2, 0).numpy() * 127.5 + 127.5).astype(np.uint8)

        Image.fromarray(imgs).save(
            log_folder / 'traverse_eps.png'
        )

    @torch.no_grad()
    def _traverse_zk(self) -> None:
        """Explores generated images by traversing the dimensions in z1, ..., zk vectors"""

        batch_size = self._config['batch_size']

        log_folder = self._writer.checkpoint_folder.parent / 'traverse_zk'
        log_folder.mkdir(exist_ok=True, parents=True)

        traverse_samples = 8
        y = self._sample_label(traverse_samples)

        # generate images
        with torch.no_grad():
            utils.save_image(
                self._g_ema(y),
                log_folder / 'sample.png',
                nrow=int(batch_size ** 0.5),
                normalize=True,
                value_range=(-1, 1),
            )

        traverse_range = 4.0
        intermediate_points = 9
        truncation = 0.7

        zs = self._g_ema.sample_zs(traverse_samples, truncation)
        es = self._g_ema.sample_eps(traverse_samples, truncation)
        _, n_layers, n_dim = zs.shape

        offsets = np.linspace(-traverse_range, traverse_range, intermediate_points)

        for i_layer in range(n_layers):
            for i_dim in range(n_dim):
                imgs = []
                for offset in offsets:
                    _zs = zs.clone()
                    _zs[:, i_layer, i_dim] = offset
                    with torch.no_grad():
                        img = self._g_ema(y, es, _zs).cpu()
                        img = torch.cat([_img for _img in img], dim=1)
                    imgs.append(img)
                imgs = torch.cat(imgs, dim=2)

                imgs = (imgs.permute(1, 2, 0).numpy() * 127.5 + 127.5).astype(np.uint8)
                Image.fromarray(imgs).save(
                    log_folder / f"traverse_L{i_layer}_D{i_dim}.png"
                )

    @torch.no_grad()
    def _explore_y(self) -> None:
        """Generates and saves images from random labels"""

        n = 49
        y = self._sample_label(n)

        zs = self._g_ema.sample_zs(n)
        eps = self._g_ema.sample_eps(n)

        with torch.no_grad():
            imgs = self._g_ema(y, eps, zs).cpu()

        imgs = [imgs[i] for i in range(n)]
        imgs = torch.cat(imgs, dim=2)
        imgs = (imgs.permute(1, 2, 0).numpy() * 127.5 + 127.5).astype(np.uint8)

        log_folder = self._writer.checkpoint_folder.parent / 'explore_y'
        log_folder.mkdir(exist_ok=True, parents=True)
        Image.fromarray(imgs).save(log_folder / 'explore_y.png')

    @torch.no_grad()
    def _display_output_eps(self) -> None:
        """Displays TSNE of the epsilon from real dataset and from generated dataset"""

        n_classes = self._config['dataset']['n_out']
        n_batches = 200

        loader = self._get_dl()
        labels, embeddings = [], []

        # real data embeddings
        for _ in trange(n_batches):
            img, label = next(loader)
            img = img.to(self._device)

            with torch.no_grad():
                h, _ = self._encoder(img)

            labels.extend(label.cpu().numpy().tolist())
            embeddings.extend(h.cpu().numpy())

        # generated data embeddings
        for _ in trange(n_batches):
            label_oh = self._sample_label()

            with torch.no_grad():
                img = self._g_ema(label_oh)
                h, _ = self._encoder(img)

            label = torch.argmax(label_oh, dim=1) + n_classes

            labels.extend(label.cpu().numpy().tolist())
            embeddings.extend(h.cpu().numpy())

        labels = np.array(labels)
        embeddings = np.array(embeddings)

        tsne_emb = TSNE(n_components=2).fit_transform(embeddings)
        img_tsne = tsne_display_tensorboard(tsne_emb, labels, r'T-SNE of the model $\varepsilon$')
        self._writer.add_image('TSNE', img_tsne, 0)

    def _get_fid_data_transform(self):
        """Returns data transform for FID calculation

        Returns:
            data transform
        """

        name = self._config['dataset']['name']
        size = 299  # this size is needed for Inception network

        if name in ['galaxy_zoo']:
            transform = transforms.Compose([
                transforms.RandomHorizontalFlip(),
                transforms.RandomAffine((0, 360), (0.01,) * 2),
                transforms.CenterCrop(207),
                transforms.Resize((size, size)),
                transforms.ConvertImageDtype(torch.float),
                transforms.Normalize(0.5, 0.5),
            ])

        else:
            raise ValueError('Unsupported dataset')

        return transform

    def _get_data_transform(self):
        """Returns transform for the data, based on the config

        Returns:
            data transform
        """

        name = self._config['dataset']['name']
        size = self._config['dataset']['size']

        if name in ['galaxy_zoo']:
            transform = transforms.Compose([
                transforms.CenterCrop(207),
                transforms.Resize((size, size)),
                transforms.ConvertImageDtype(torch.float),
                transforms.Normalize(0.5, 0.5),
            ])

        else:
            raise ValueError('Unsupported dataset')

        return transform

    @torch.no_grad()
    def _compute_fid_score(self) -> float:
        """Computes FID score for the dataset

        Returns:
            float: FID score
        """

        bs = self._config['batch_size']

        path = self._config['dataset']['path']
        anno = self._config['dataset']['anno']
        size = 299

        make_dl = MakeDataLoader(path, anno, size, augmented=False)
        ds = make_dl.dataset_test

        n_samples = len(ds)
        fid_func = get_fid_fn(ds, self._device, bs, n_samples)
        with torch.no_grad():
            fid_score = fid_func(self._g_ema)
        return fid_score

    @torch.no_grad()
    def _compute_inception_score(self) -> float:
        """Computes inception score for the dataset

        Returns:
            float: inception score
        """

        path = self._config['dataset']['path']
        anno = self._config['dataset']['anno']
        size = self._config['dataset']['size']
        make_dl = MakeDataLoader(path, anno, size, augmented=False)

        n_samples = len(make_dl.dataset_test)
        bs = self._config['batch_size']
        dataset = GANDataset(self._g_ema, n=n_samples)

        with torch.no_grad():
            score = inception_score(dataset, batch_size=bs, resize=True)[0]
        return score

    def _sample_label(self, n: Optional[int] = None) -> torch.Tensor:
        """Samples y label for the dataset

        Args:
            n: number of labels to sample

        Returns:
            torch.Tensor: sampled random label
        """

        if n is None:
            batch_size = self._config['batch_size']
            n = batch_size

        labels = []
        for _ in range(n):
            idx = random.randrange(len(self._sample_ds))
            _, label = self._sample_ds[idx]
            labels.append(torch.from_numpy(label))

        labels = torch.stack(labels).to(self._device)
        return labels

    def _get_ds(self):

        name = self._config['dataset']['name']
        path = self._config['dataset']['path']
        anno = self._config['dataset']['anno']
        columns = None if 'columns' not in self._config['dataset'] else self._config['dataset']['columns']

        transform = self._get_data_transform()
        dataset = get_dataset(name, path, anno_file=anno, transform=transform, columns=columns)
        return dataset

    def _get_dl(self):
        batch_size = self._config['batch_size']
        n_workers = self._config['n_workers']

        dataset = self._get_ds()
        loader = infinite_loader(
            DataLoader(
                dataset,
                batch_size=batch_size,
                shuffle=True,
                drop_last=True,
                num_workers=n_workers
            )
        )
        return loader


import os
from tkinter import Tk, Label, Button
import tkinter.font as font
import tkinter.simpledialog as simpledialog
from PIL import Image, ImageTk
from tkinter import messagebox as mbox
import pyttsx3
import cv2
import face_recognition
from numpy import info
from sklearn import svm
import datetime

def test_svm():

    # Training the SVC classifier

    # The training data would be all the face encodings from all the known images and the labels are their names
    encodings = []
    names = []

    engine = pyttsx3.init()
    voices = engine.getProperty('voices')
    engine.setProperty('voice', voices[1].id)
    engine.setProperty('rate', 150)

    train_dir = os.listdir('train/')

    for person in train_dir:
        pix = os.listdir("train/" + person)

        # Loop through each training image for the current person
        for person_img in pix:
            # Get the face encodings for the face in each image file
            face = face_recognition.load_image_file(
                "train/" + person + "/" + person_img)
            face_bounding_boxes = face_recognition.face_locations(face)

            # If training image contains exactly one face
            if len(face_bounding_boxes) == 1:
                face_enc = face_recognition.face_encodings(face)[0]
                # Add face encoding for current image with corresponding label (name) to the training data
                encodings.append(face_enc)
                names.append(person)
            else:

                print(person + "/" + person_img +" was skipped and can't be used for training")
                os.remove("train/" + person + "/" + person_img)

    # Create and train the SVC classifier
    clf = svm.SVC(gamma='scale')
    clf.fit(encodings, names)

    # menghitung acurasi
    accuracy = clf.score(encodings, names)
    print("Accuracy:", accuracy)

    # Load the test image with unknown faces into a numpy array
    test_image = face_recognition.load_image_file('test/test.jpg')

    # Find all the faces in the test image using the default HOG-based model
    face_locations = face_recognition.face_locations(test_image)
    num = len(face_locations)
    print("Number of faces detected: ", num)

    list_names = []
    if num == 0:
        text = "detection face is failed cause nothing face in the picture.  please give me a picture, with the face"
        engine.say(text)
        engine.runAndWait()
        print("Face Not Found")
    else :

        print("Found:")
        for i in range(num):
            test_image_enc = face_recognition.face_encodings(test_image)[i]
            name = clf.predict([test_image_enc])

        if name[0] in names:
            text = "detection is successfuly. i detected a face, and i recognized him, and he was named" + name[0]
            engine.say(text)
            engine.runAndWait()
            print(name)
            list_names.append(*name)
        else:
            print("Undefined Faces")

    # Predict all the faces in the test image using the trained classifier

    display_name(list_names)
    engine.runAndWait()

def test_img_capture():

    window.destroy()

    img_counter = 0

    if(True):
        cam = cv2.VideoCapture(0)
        cv2.namedWindow("Face Recognition")
        while True:
            ret, frame = cam.read()
            if not ret:
                print("failed to grab frame")
                break

            frame = cv2.flip(frame, 1)
            cv2.imshow("Face Recognition", frame)

            k = cv2.waitKey(1)
            if k % 256 == 32:
                # SPACE pressed
                img_name = "test.jpg"
                cv2.imwrite(os.path.join('test', img_name), frame)
                print("{} written!".format(img_name))
                print("Closing now")
                img_counter += 1
                break
    cam.release()
    cv2.destroyAllWindows()
    test_svm()

def train_img_capture():

    cam = cv2.VideoCapture(0)

    cv2.namedWindow("Face Training")
    img_counter = 0

    file_name = ''
    file_name= simpledialog.askstring(title="Face Recognition",prompt="What's your Name?:")
    window = Tk()
    window.withdraw()

    os.mkdir("train/"+file_name)

    while True:
        ret, frame = cam.read()
        if not ret:
            print("failed to grab frame")
            break

        frame = cv2.flip(frame, 1)
        cv2.imshow("Face Training", frame)

        k = cv2.waitKey(1)
        if k%256 == 27:
            # ESC pressed
            print("Escape hit, closing...")
            break
        elif k%256 == 32:
            # SPACE pressed
            img_name = file_name+"_{}.jpg".format(img_counter)
            cv2.imwrite(os.path.join("train/"+file_name, img_name), frame)
            print("{} written!".format(img_name))
            img_counter += 1

    cam.release()

    cv2.destroyAllWindows()
    window.destroy()

def display_name(list_name):
    window=Tk()
    label = Label(window, text="Faces Recognized")
    listbox = Listbox(window, width=50)
    label.pack()
    listbox.pack(fill=BOTH, expand=1)  # adds listbox to window
    for row in list_name:
        listbox.insert(END, row)   # one line for loop
    window.mainloop()

engine = pyttsx3.init()
voices = engine.getProperty('voices')
engine.setProperty('voice', voices[1].id)
engine.setProperty('rate', 180)

waktu = datetime.datetime.now()
jam = waktu.hour

if jam >= 1 and jam <= 9:
    hour = "Morning"
elif jam > 9 and jam <= 15:
    hour = "Afternoon"
elif jam > 15 and jam <= 22:
    hour = "Evening"
else :
    hour = "night"

text = "hello sir, good" +  hour +  ", how are you today?. welcome to the face detection program. display screen now"
engine.say(text)
engine.runAndWait()

window=Tk()
window.config(width=300, height=300,padx=20,pady=50)
label = Label(
    window, text='WELCOME TO FACE RECOGNITION. SELECT AN OPTION BELOW:\n',font=font.Font(size=16))
label.pack()
button = Button(window, text="Train",command=train_img_capture,width=20,bg="red",fg="white",pady=10)
button['font']=font.Font(size=16)
button.pack()
label = Label(window, text='\n')
label.pack()
button = Button(window, text="Test", command=test_img_capture,width=20,bg="#0052cc",fg="white",pady=10)
button['font']=font.Font(size=16)
button.pack()
label=Label(window,text="\nInstructions\n1).In Train Mode, enter your name and then press SPACEBAR to capture images. Hit ESC when done.\n2).In Test Mode, press SPACEBAR to capture image and display detected face",font=font.Font(size=14))
label.pack()
window.mainloop()

text = "thanks very much,  for using this program. don't forget to try again. see you."
engine.say(text)
engine.runAndWait()

Face-Recognition-Using-SVM/Face Recognition using SVM.py


import torch
import pytorch_lightning as pl
from pytorch_lightning.loggers import TensorBoardLogger
from pytorch_lightning.callbacks import ModelCheckpoint
import torch.nn.functional as F
from argparse import ArgumentParser
from torchvision.utils import make_grid
from utils import DiceLoss
import segmentation_models_pytorch as smp

class U2Net(pl.LightningModule):
    def __init__(self,
                 in_ch=3,
                 out_ch=1,
                 lr=1.0e-3,
                 weight_decay=3.0e-6,
                 ratio=0.9,
                 **kwargs,
                 ):
        super().__init__()

        self.save_hyperparameters()

        self.lr = lr
        self.weight_decay = weight_decay

        self.model = smp.Unet(
            encoder_name="efficientnet-b4",  # choose encoder, e.g. mobilenet_v2 or efficientnet-b7
            encoder_weights="imagenet",  # use `imagenet` pre-trained weights for encoder initialization
            in_channels=3,  # model input channels (1 for gray-scale images, 3 for RGB, etc.)
            classes=1,  # model output channels (number of classes in your dataset)
        )

        self.dice_loss = DiceLoss()
        self.ratio = ratio

        self.sample_imgs = torch.zeros(1)
        self.sample_masks = torch.zeros(1)
        self.rand_mask = torch.randint(16, (8,))

    def forward(self, x):
        x_hat = torch.sigmoid(self.model(x))
        return x_hat

    def training_step(self, batch, batch_idx):
        img, mask = batch
        mask_hat = self.forward(img)

        log_loss = F.binary_cross_entropy(mask_hat, mask)
        dice_loss = self.dice_loss(mask_hat, mask)
        train_loss = log_loss+dice_loss

        self.log('train_loss', log_loss, on_epoch=True, on_step=True)
        self.log('train_diceloss', dice_loss, on_epoch=True, on_step=True)
        self.log('train_dicemetric', 1-dice_loss, on_epoch=True, on_step=True)

        return train_loss

    def validation_step(self, batch, batch_idx):
        img, mask = batch
        mask_hat = self.forward(img)

        log_loss = F.binary_cross_entropy(mask_hat, mask)
        dice_loss = self.dice_loss(mask_hat, mask)
        val_loss = log_loss+dice_loss

        self.log('val_loss', val_loss, on_epoch=True, on_step=False)
        self.log('val_diceloss', dice_loss, on_epoch=True, on_step=False)
        self.log('val_dicemetric', 1-dice_loss, on_epoch=True, on_step=False)

        self.sample_imgs = img[self.rand_mask]
        self.sample_masks = torch.cat([mask[self.rand_mask],
                                       mask_hat[self.rand_mask]], dim=0)

    def on_validation_epoch_end(self) -> None:
        mask_grid = make_grid(self.sample_masks, 8)
        img_grid = make_grid(self.sample_imgs, 8)
        self.logger.experiment.add_image('gt and pred masks', mask_grid, self.current_epoch)
        self.logger.experiment.add_image('images', img_grid, self.current_epoch)

    def configure_optimizers(self):
        optimizer = torch.optim.Adam(self.parameters(), lr=self.lr, weight_decay=self.weight_decay)
        return [optimizer]

    @staticmethod
    def add_model_specific_args(parent_parser):
        parser = ArgumentParser(parents=[parent_parser], add_help=False)

        parser.add_argument("--in_ch", type=int, default=3)
        parser.add_argument("--out_ch", type=int, default=1)
        parser.add_argument("--ratio", type=float, default=0.9)

        parser.add_argument("--lr", type=float, default=1.0e-3)
        parser.add_argument("--weight_decay", type=float, default=3e-6)

        parser.add_argument("--data_dir", type=str, default=".")
        parser.add_argument("--image_size", type=int, default=256)
        parser.add_argument("--num_workers", type=int, default=-1)
        parser.add_argument("--batch_size", type=int, default=32)
        parser.add_argument("--pin_memory", action='store_true')
        parser.add_argument("--drop_last", action='store_false')
        parser.add_argument("--split", type=float, default=0.8)
        return parser

def cli_main(args=None):
    from datamodules import HuBMAPDataModule

    parser = ArgumentParser()

    parser.add_argument("--logdir", default="logs", type=str)
    parser.add_argument("--name", default="unet34", type=str)
    parser.add_argument("--chkpt_monitor", default="val_dicemetric", type=str)
    parser.add_argument("--mode", default="max", type=str)
    script_args, _ = parser.parse_known_args(args)

    dm_cls = HuBMAPDataModule

    logger = TensorBoardLogger(script_args.logdir, name=script_args.name)
    checkpoint = ModelCheckpoint(monitor=script_args.chkpt_monitor, mode='max',
                                 filename=script_args.name + '-{epoch:03d}-{' + script_args.chkpt_monitor + ':.5f}')
    parser = U2Net.add_model_specific_args(parser)
    parser = pl.Trainer.add_argparse_args(parser)
    args = parser.parse_args(args)

    dm = dm_cls.from_argparse_args(args)

    model = U2Net(**vars(args))
    # model = U2Net.load_from_checkpoint('./logs/u2net_256/version_0/checkpoints/u2net_256-epoch=305-val_dicemetric=0.96.ckpt')
    trainer = pl.Trainer.from_argparse_args(args, logger=logger, callbacks=[checkpoint])
    trainer.fit(model, datamodule=dm)
    return dm, model, trainer

if __name__ == "__main__":
    dm, model, trainer = cli_main()


import json

import requests

# Search by language
path = "https://datalab.nlpedia.ai/api/fetch_data_for_sdk_language"
data = {"language": "fr"}
r = requests.post(path, json=data)
list_ = json.loads(r.content)
print(len(list_))
print(list_[0])

# Search by task
path = "https://datalab.nlpedia.ai/api/fetch_data_for_sdk_task"
data = {
    "task": "sentiment-classification",
}
r = requests.post(path, json=data)
list_ = json.loads(r.content)
print(len(list_))
print(list_[0])

# Get all languages, tasks and task_category
path = "https://datalab.nlpedia.ai/api/fetch_meta_data_for_sdk_task_language"
r = requests.get(path)
dic_ = json.loads(r.content)
print(dic_["languages"])  # list of all languages
print(dic_["tasks"])  # list of all tasks
print(dic_["task_categories"])  # list of all task_category


from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import logging
from timeit import default_timer as timer

import compas
from compas.geometry import Transformation
from compas.robots import Configuration

if compas.RHINO:
    import clr
    import rhinoscriptsyntax as rs
    import Rhino.Geometry as rg

    from compas_rhino.geometry import RhinoMesh
    from compas_rhino.geometry.transformations import xform_from_transformation

__all__ = [
    "PathVisualizer",
]

LOG = logging.getLogger("compas_fab.ghpython.path_planning")

def vrep_pose_from_plane(plane):
    """Creates a vrep-compatible transformation matrix from a Rhino/Grasshopper
    plane.

    This function might need rework as the source of the 90-deg Y rotation
    need is not entirely clear to me (related to the RFL model mismatch).
    """
    translation_matrix = rs.XformTranslation(((plane[0][0]), (plane[0][1]), plane[0][2]))
    plane_start = rs.PlaneFromFrame(rs.AddPoint(0, 0, 0), rs.AddPoint(1, 0, 0), rs.AddPoint(0, 1, 0))
    plane_end = rs.PlaneFromFrame(
        rs.AddPoint(0, 0, 0),
        rs.AddPoint(plane[1][0], (plane[1][1]), plane[1][2]),
        rs.AddPoint(plane[2][0], plane[2][1], plane[2][2]),
    )
    rotation_matrix = rs.XformRotation1(plane_start, plane_end)
    matrix = rs.XformMultiply(translation_matrix, rotation_matrix)
    return [
        matrix.M00,
        matrix.M01,
        matrix.M02,
        matrix.M03,
        matrix.M10,
        matrix.M11,
        matrix.M12,
        matrix.M13,
        matrix.M20,
        matrix.M21,
        matrix.M22,
        matrix.M23,
    ]

def _transform_to_origin(mesh, xform):
    inverse = clr.StrongBox[rg.Transform]()
    if not xform.TryGetInverse(inverse):
        raise ValueError("Unable to get inverse of matrix")

    mesh.Transform(inverse.Value)

    return mesh

def _create_rhino_mesh(vertices, faces):
    vertices = [vertices[i : i + 3] for i in range(0, len(vertices), 3)]
    faces = [faces[i : i + 3] for i in range(0, len(faces), 3)]
    mesh = rg.Mesh()
    for a, b, c in vertices:
        mesh.Vertices.Add(a, b, c)
    for face in faces:
        mesh.Faces.AddFace(face[0], face[1], face[2])

    return mesh

def _to_xform(m):
    m = m if len(m) == 16 else m + [0, 0, 0, 1]
    return xform_from_transformation(Transformation.from_list(m))

# TODO: This module needs to be heavily refactored
# Currently only compatible with V-REP client and most helper functions
# are not superseded by compas features
class PathVisualizer(object):
    """Handles the generation of meshes to visualize a full path plan
    in Rhino/Grasshopper.
    """

    def __init__(self, simulator, robot, building_member=None, building_member_pickup_config=None):
        self.simulator = simulator
        self.robot = robot
        self.building_member = building_member
        self.building_member_pickup_config = building_member_pickup_config

    def get_frame_meshes(self, path, frame, ctx):
        """Retrieves all meshes required to render a specific frame of a path plan.

        Args:
            path (:obj:`list` of :class:`Configuration`): Represents a collision-free
                path to a goal pose. It is the output of the path planning generated
                by a :class:`Simulator` object.
            frame (:obj:`int`): Frame number to retrieve.
            ctx (:obj:`dict`): A dictionary to keep context. Within Grasshopper, this is
                normally the ``sc.sticky`` object.

        Returns:
            list: list of Rhino meshes that can be used to visualize the selected frame.
        """
        first_start = timer() if self.debug else None
        shape_handles = self.simulator.get_all_visible_handles()
        if self.debug:
            LOG.debug("Execution time: get_all_visible_handles=%.2f", timer() - first_start)

        if "scene_meshes" not in ctx:
            ctx["scene_meshes"] = self._get_scene_meshes(shape_handles)

        frame_config = path[frame]

        start = timer() if self.debug else None
        self.simulator.set_robot_config(self.robot, frame_config)
        if self.debug:
            LOG.debug("Execution time: set_robot_config=%.2f", timer() - start)

        start = timer() if self.debug else None
        meshes = []
        mesh_matrices = self.simulator.get_object_matrices(shape_handles)
        for handle, mesh_matrix in mesh_matrices.iteritems():
            mesh = ctx["scene_meshes"][handle].DuplicateShallow()
            mesh.Transform(_to_xform(mesh_matrix))
            meshes.append(mesh)

        if self.building_member:
            gripping_config = self.building_member_pickup_config if self.building_member_pickup_config else path[0]
            info = self._get_building_member_info(gripping_config)
            mesh = info["mesh"].DuplicateShallow()
            parent_transform = _to_xform(mesh_matrices[info["parent_handle"]])
            relative_transform = info["relative_transform"]

            mesh.Transform(rg.Transform.Multiply(parent_transform, relative_transform))
            meshes.append(mesh)

        if self.debug:
            LOG.debug("Execution time: get all transformed meshes=%.2f", timer() - start)
            LOG.debug("Execution time: total=%.2f", timer() - first_start)

        return meshes

    @property
    def debug(self):
        """Indicates whether the path visualizer is in debug mode or not."""
        return self.simulator.debug

    def _get_scene_meshes(self, shape_handles):
        start = timer() if self.debug else None
        shape_geometry = []
        for handle in shape_handles:
            _, faces, vertices, _, _ = self.simulator.run_child_script("getShapeMesh", [handle], [], [])
            shape_geometry.append((vertices, faces))

        scene_meshes = {}
        _, _, mesh_matrices, _, _ = self.simulator.run_child_script("getShapeMatrices", shape_handles, [], [])
        for i in range(0, len(mesh_matrices), 12):
            handle = shape_handles[i // 12]
            vertices, faces = shape_geometry[i // 12]
            transform = _to_xform(mesh_matrices[i : i + 12])
            mesh = _transform_to_origin(_create_rhino_mesh(vertices, faces), transform)
            scene_meshes[handle] = mesh

        if self.debug:
            LOG.debug("Execution time: create RFL meshes at origin=%.2f", timer() - start)

        return scene_meshes

    def _get_building_member_info(self, gripping_config):
        start = timer() if self.debug else None
        self.simulator.set_robot_config(self.robot, gripping_config)
        mesh = RhinoMesh.from_guid(self.building_member).to_compas()
        handle = self.simulator.add_attached_collision_mesh(mesh, options={"robot_name": self.robot.name})
        matrix = self.simulator.get_object_matrices([handle])[handle]

        parent_handle = self.simulator.get_object_handle("customGripper" + self.robot.name)
        _, _, mesh_matrix, _, _ = self.simulator.run_child_script(
            "getShapeMatrixRelative", [handle, parent_handle], [], []
        )

        relative_transform = _to_xform(mesh_matrix)

        transform = _to_xform(matrix)
        mesh_at_origin = _transform_to_origin(rs.coercemesh(self.building_member), transform)

        if self.debug:
            LOG.debug("Execution time: building member=%.2f", timer() - start)

        return {"mesh": mesh_at_origin, "parent_handle": parent_handle, "relative_transform": relative_transform}

class InputParameterParser(object):
    """Simplifies some tasks related to parsing Grasshopper input parameters
    for path planning."""

    def compact_list(self, list):
        """Compacts a list filtering all `None` values."""
        return filter(None, list)

    def get_pose(self, plane_or_pose):
        """Gets a pose that is compatible with V-REP from a string containing
        comma-separated floats or from a Rhino/Grasshopper plane.

        Args:
            plane_or_pose (comma-separated :obj:`str` of a Rhino :obj:`Plane`):

        Returns:
            pose: :class:`.Pose` instance representing a transformation matrix.
        """
        if not plane_or_pose:
            return None

        try:
            return Pose.from_list(vrep_pose_from_plane(plane_or_pose))
        except (TypeError, IndexError):
            return Pose.from_list(map(float, plane_or_pose.split(","))) if plane_or_pose else None

    def get_config_or_pose(self, config_values_or_plane):
        """Parses multiple input data types and returns a configuration or a pose
        represented as a transformation matrix.

        Args:
            config_values_or_plane (comma-separated :obj:`string`, or :class:`Configuration` instance,
                or a Rhino :obj:`Plane`).
        """
        if not config_values_or_plane:
            return None

        try:
            return Pose.from_list(vrep_pose_from_plane(config_values_or_plane))
        except (TypeError, IndexError):
            try:
                if config_values_or_plane.external_axes and config_values_or_plane.joint_values:
                    return config_values_or_plane
            except AttributeError:
                values = map(float, config_values_or_plane.split(","))
                return Configuration.from_degrees_list(values)


# test_IM_GetTopicApi   查询主题接口    /as_stock_information/api/topic/v1/get
import json
import logging
import random

import allure
import pytest

from Common.getConsoleLogin import getConsoleLogin_token
from Common.get_time_stamp import stampToTime

from Common.sign import get_sign

from Common.requests_library import Requests
from Common.tools.read_write_json import get_json

from glo import BASE_DIR, HTTP, JSON

# @pytest.mark.skip(reason="调试中 ")
@allure.feature('资讯app_查询主题接口')
class TestIMGetTopicApi():
    @classmethod
    def setup_class(cls) -> None:
        cls.session = Requests().get_session()

    def tearDown(self) -> None:
        Requests(self.session).close_session()

    # @pytest.mark.skip(reason="调试中 ")
    @pytest.mark.parametrize('info',
                             get_json(BASE_DIR + r"/TestData/testIMData/test_IM_GetTopicApi.json"))
    def test_IM_GetTopicApi(self, info):
        url = HTTP + "/as_stock_information/api/topic/v1/get"
        header = {}
        header.update(JSON)

        paylo = info
        # print(paylo)
        sign1 = {"sign": get_sign(paylo)}  # 把参数签名后通过sign1传出来
        payload1 = {}
        payload1.update(paylo)
        payload1.update(sign1)
        headers = {}
        headers.update(header)
        # print(token)
        # print(type(token))
        token = {"token": getConsoleLogin_token()}
        headers.update(token)  # 将token更新到headers
        # print(headers)
        payload = json.dumps(dict(payload1))
        # time.sleep(60.01)

        r = Requests(self.session).post(
            url=url, headers=headers, data=payload, title="查询主题接口"
        )

        j = r.json()
        # print(j)
        assert r.status_code == 200
        assert j.get("code") == "000000"
        assert j.get("msg") == "ok"
        if "data" in j:
            if len(j.get("data")) != 0:
                assert "id" in j.get("data")
                assert "categoryId" in j.get("data")
                assert "name" in j.get("data")
                assert "description" in j.get("data")
                if "backgroundImg" in j.get("data"):
                    assert "https://zhuorui-public-test.oss-cn-shenzhen.aliyuncs.com/information/" \
                           in j.get("data").get("backgroundImg")
                elif "headImg" in j.get("data"):
                    assert j.get("data").get("headImg") == \
                           "https://zhuorui-public-test.oss-cn-shenzhen.aliyuncs.com/" \
                           "information/20210621/13bd8850-d25a-11eb-94bb-5904ce8ea223.jpg"
                else:
                    raise AssertionError(j.get("data"))
            else:
                logging.info("data为空")
        else:
            logging.info("data不在j中")


import torch
from torch.utils.data import DataLoader, Dataset
import os
from pathlib import Path
import numpy as np
from PIL import Image

def cal_mean_std(path: str):
    channels_sum, channels_squared_sum, nums = 0, 0, 0
    path_list = os.listdir(path)
    for img_path in path_list:
        image_path = os.path.join(path, img_path)
        # image = torch.from_numpy(np.array(Image.open(image_path)) / 255).permute([2, 0, 1]).float()
        image = torch.from_numpy(np.array(Image.open(image_path))).permute([2, 0, 1]).float()
        channels_sum += torch.mean(image, dim=[1, 2])
        channels_squared_sum += torch.mean(image**2, dim=[1, 2])
        nums += 1
    mean = channels_sum / nums
    std = (channels_squared_sum / nums - mean**2)**0.5
    return mean, std

if __name__ == '__main__':
    path = os.path.abspath("../data1/coco/train2017/JPEGImages")
    mean, std = cal_mean_std(path=path)
    print(f'mean : {mean}, std : {std}')
# mean : tensor([131.5901, 129.8181, 126.6339]), std : tensor([56.4909, 55.3395, 56.3917])


import scrapy
from tutorial.items import DmozItem

class DomzSpider(scrapy.Spider):
    name = 'tencent'
    allowed_domains = ["tencent.com"]
    start_urls = [
        "https://security.tencent.com/index.php/blog",
        ]

    def parse(self, response):
        for sel in response.xpath('//*[@class="mod-itemlist-article"]'):
            item=DmozItem()
            item['title'] = sel.xpath('li/div[@class="item_content"]/div[@class="content_title"]/a/text()').extract()
            #print item['title']
            item['link'] = sel.xpath('li/div[@class="item_content"]/div[@class="content_title"]/a/@href').extract()
            item['dat'] = sel.xpath(
                'li/div[@class="item_content"]/div[@class="content_info"]/span[@class="info_date"]/span/text()').extract()
            item['hot'] = sel.xpath(
                'li/div[@class="item_content"]/div[@class="content_info"]/span[@class="info_watched"]/span/text()').extract()
            item['info'] = sel.xpath(
                'li/div[@class="item_content"]/div[@class="content_brief"]/p/text()').extract()
            #print title,link,desc
            yield item


from typing import Dict, List, Optional
import glob
import json
import logging
import sys
import time

import numpy as np
import pandas as pd

from pyhailing import RidehailEnv

def get_eval_files(the_dir: str) -> List[str]:
    """Provides a list of all evaluation files in the directory."""

    if the_dir is None:
        the_dir = "./"

    if the_dir[-1] != '/':
        the_dir = the_dir + "/"

    return glob.glob(the_dir + "pyhailing_eval_results*.json")

def load_result_file(eval_file) -> Optional[Dict]:
    """Loads the results data from a submitted file.

    If the file cannot be read, returns None.

    """

    try:
        with open(eval_file, 'r') as f:
            result = json.load(f)
            return result

    except Exception:
        return None

def get_instance_category(entry) -> Optional[str]:
    """Determines the instance category for which the entry was submitted.

    If it does not match the config of any instance category, returns None.

    """

    instance_categories = RidehailEnv.DIMACS_CONFIGS.ALL_CONFIGS

    entry_config = entry["config"]
    keys_to_check = list(entry_config.keys())

    try:
        keys_to_check.remove("nickname")
    except:
        return None

    for category_name, category_config in instance_categories.items():
        if all((entry_config[key] == category_config[key] for key in keys_to_check)):
            return category_name

    return None

def get_rwd_upperbound(env) -> float:
    """Provides an upperbound on the reward for the env's current episode."""

    # TODO
    ub = np.inf

    return ub

def check_vehicle_sequences(assignments, env) -> None:
    """Checks that assignments are plausible."""

    # TODO

    return True

def get_entry_performance(entry):
    """Checks the performance of an entry."""

    eps_results = entry["episodes"]

    # Make sure the proper number of episodes were performed
    expected_num_eps = RidehailEnv.DIMACS_NUM_EVAL_EPISODES

    if len(eps_results) != expected_num_eps:
        logging.warning(
            f"Invalid entry. Expected results for {len(eps_results)} episodes but got results for {expected_num_eps}.")
        return None

    # Create an environment to check the solution
    config = entry["config"]
    config["for_evaluation"] = False
    env = RidehailEnv(entry["config"])

    # For each episode...
    for i, eps_result in enumerate(eps_results):

        env.reset()

        # Make sure the claimed reward does not exceed a computable UB
        rwd_upperbound = get_rwd_upperbound(env)

        if eps_result['final_reward'] > rwd_upperbound:
            logging.warning("Invalid reward achieved.")
            return None

        # Get the sequence for each vehicle
        # get the sequence of reqs for each vehicle. for each, make sure it is time feasible (with an easy check)
        seqs_feasible = check_vehicle_sequences(eps_result['assignments'], env)
        if not seqs_feasible:
            logging.warning("Invalid vehicle-to-request assignments.")
            return None

    # All good. Get the mean episode reward
    mean_reward = (
        sum((eps_result["final_reward"] for eps_result in eps_results))
        / expected_num_eps
    )

    return mean_reward

def get_competition_results(eval_files:List[str]) -> pd.DataFrame:

    # Initialize the results of the competition
    competition_results = []

    for eval_file in eval_files:

        # Initialize a results record for this entry
        entry_results = {
            "filename": eval_file,
            "instance_category": None,
            "mean_reward": None,
        }

        # Load the file
        entry = load_result_file(eval_file)

        if entry is None:
            logging.warning(f"Entry file could not be read: {eval_file}")
            competition_results.append(entry_results)
            continue

        # Determine the entry's instance category
        instance_category = get_instance_category(entry)

        if instance_category is None:
            logging.warning(
                f"Entry file's config does not match the config of any instance category: {eval_file}"
            )
            competition_results.append(entry_results)
            continue

        entry_results["instance_category"] = instance_category

        # Get the entry's mean episode reward
        mean_reward = get_entry_performance(entry)

        # Done.
        competition_results.append(entry_results)

    # Combine all results into df and return it
    return pd.DataFrame(competition_results)

if __name__ == "__main__":

    now = time.strftime("%Y%m%d%H%M%S")

    args = sys.argv

    if len(args) != 2:
        logging.warning("Grabbing eval files from the current directory.")
        eval_files_dir = "./"

    else:
        eval_files_dir = args[1]

    eval_files = get_eval_files(eval_files_dir)

    eval_results = get_competition_results(eval_files)

    eval_results.to_csv(f"competition_results_{now}.csv", index=None)


from utils import toCsv
from pathlib import Path

dirs = Path('outDayDataset/unbalanced/2014/MOORE')

for month_dir in dirs.glob('*'):
    print(month_dir.name)
    for file in month_dir.glob('*.arff'):
        with open(file, 'r') as _in:
            c = _in.readlines()
            data = toCsv(c)
            with open(file.parent / f'{file.name[:-5]}.csv', 'w') as out:
                out.writelines(data)


from sympy import primerange, isprime
import time

N = 10**6 # our upper limit
rnge = 1,N//200 # just a heuristic, could probably come up with a programatic estimate using distribution of primes

primes = list(primerange(*rnge)) # all primes in range

# generate all primes that are sums of primes in our range
# not very pretty or efficient, but fast enough
sums = [(j-i,sum(primes[i:j])) for i in range(*rnge) for j in range(*rnge)
        if i<j and j <= len(primes) and isprime(sum(primes[i:j])) and sum(primes[i:j]) < N]

lengths, sums = zip(*sums) # unzip tuples
ind = lengths.index(max(lengths)) # get index of max sum

print(sums[ind]) # max sum


import torch
import torch.nn as nn
from torch.nn import Parameter
import torch.jit as jit
import warnings
from collections import namedtuple
from typing import List, Tuple
from torch import Tensor
import numbers
import warnings
warnings.filterwarnings("ignore", message=r"'layers' was found in ScriptModule constants,  but it is a non-constant submodule. Consider removing it.")

def script_lstm(input_size, hidden_size, num_layers, dropout=False, bidirectional=False):
    '''Returns a ScriptModule that mimics a PyTorch native LSTM.'''

    if bidirectional:
        stack_type = StackedLSTM2
        layer_type = BidirLSTMLayer
        dirs = 2
    elif dropout:
        stack_type = StackedLSTMWithDropout
        layer_type = LSTMLayer
        dirs = 1
    else:
        stack_type = StackedLSTM
        layer_type = LSTMLayer
        dirs = 1

    return stack_type(num_layers, layer_type,
                      first_layer_args=[LSTMCell, input_size, hidden_size],
                      other_layer_args=[LSTMCell, hidden_size * dirs,
                                        hidden_size])

def script_lnlstm(input_size, hidden_size, num_layers, bias=True,
                  batch_first=False, dropout=False, bidirectional=False,
                  decompose_layernorm=False):
    '''Returns a ScriptModule that mimics a PyTorch native LSTM.'''

    # The following are not implemented.
    assert bias
    assert not batch_first
    assert not dropout

    if bidirectional:
        stack_type = StackedLSTM2
        layer_type = BidirLSTMLayer
        dirs = 2
    else:
        stack_type = StackedLSTM
        layer_type = LSTMLayer
        dirs = 1

    return stack_type(num_layers, layer_type,
                      first_layer_args=[LayerNormLSTMCell, input_size, hidden_size,
                                        decompose_layernorm],
                      other_layer_args=[LayerNormLSTMCell, hidden_size * dirs,
                                        hidden_size, decompose_layernorm])

LSTMState = namedtuple('LSTMState', ['hx', 'cx'])

def reverse(lst: List[Tensor]) -> List[Tensor]:
    return lst[::-1]

class LSTMCell(nn.Module):
    def __init__(self, input_size, hidden_size):
        super(LSTMCell, self).__init__()
        self.input_size = input_size
        self.hidden_size = hidden_size
        self.weight_ih = Parameter(torch.randn(4 * hidden_size, input_size))
        self.weight_hh = Parameter(torch.randn(4 * hidden_size, hidden_size))
        self.bias_ih = Parameter(torch.randn(4 * hidden_size))
        self.bias_hh = Parameter(torch.randn(4 * hidden_size))

    def forward(self, input: Tensor, state: Tuple[Tensor, Tensor]) -> Tuple[Tensor, Tuple[Tensor, Tensor]]:
        hx, cx = state
        gates = (torch.mm(input, self.weight_ih.t()) + self.bias_ih +
                 torch.mm(hx, self.weight_hh.t()) + self.bias_hh)
        ingate, forgetgate, cellgate, outgate = gates.chunk(4, 1)

        ingate = torch.sigmoid(ingate)
        forgetgate = torch.sigmoid(forgetgate)
        cellgate = torch.tanh(cellgate)
        outgate = torch.sigmoid(outgate)

        cy = (forgetgate * cx) + (ingate * cellgate)
        hy = outgate * torch.tanh(cy)

        return hy, (hy, cy)

class LayerNorm(nn.Module):
    def __init__(self, normalized_shape):
        super(LayerNorm, self).__init__()
        if isinstance(normalized_shape, numbers.Integral):
            normalized_shape = (normalized_shape,)
        normalized_shape = torch.Size(normalized_shape)

        # XXX: This is true for our LSTM / NLP use case and helps simplify code
        assert len(normalized_shape) == 1

        self.weight = Parameter(torch.ones(normalized_shape))
        self.bias = Parameter(torch.zeros(normalized_shape))
        self.normalized_shape = normalized_shape

    def compute_layernorm_stats(self, input):
        mu = input.mean(-1, keepdim=True)
        sigma = input.std(-1, keepdim=True, unbiased=False)
        return mu, sigma

    def forward(self, input):
        mu, sigma = self.compute_layernorm_stats(input)
        return (input - mu) / sigma * self.weight + self.bias

class LayerNormLSTMCell(nn.Module):
    def __init__(self, input_size, hidden_size, decompose_layernorm=False):
        super(LayerNormLSTMCell, self).__init__()
        self.input_size = input_size
        self.hidden_size = hidden_size
        self.weight_ih = Parameter(torch.randn(4 * hidden_size, input_size))
        self.weight_hh = Parameter(torch.randn(4 * hidden_size, hidden_size))
        # The layernorms provide learnable biases

        if decompose_layernorm:
            ln = LayerNorm
        else:
            ln = nn.LayerNorm

        self.layernorm_i = ln(4 * hidden_size)
        self.layernorm_h = ln(4 * hidden_size)
        self.layernorm_c = ln(hidden_size)

    def forward(self, input: Tensor, state: Tuple[Tensor, Tensor]) -> Tuple[Tensor, Tuple[Tensor, Tensor]]:
        hx, cx = state
        igates = self.layernorm_i(torch.mm(input, self.weight_ih.t()))
        hgates = self.layernorm_h(torch.mm(hx, self.weight_hh.t()))
        gates = igates + hgates
        ingate, forgetgate, cellgate, outgate = gates.chunk(4, 1)

        ingate = torch.sigmoid(ingate)
        forgetgate = torch.sigmoid(forgetgate)
        cellgate = torch.tanh(cellgate)
        outgate = torch.sigmoid(outgate)

        cy = self.layernorm_c((forgetgate * cx) + (ingate * cellgate))
        hy = outgate * torch.tanh(cy)

        return hy, (hy, cy)

class LSTMLayer(nn.Module):
    def __init__(self, cell, *cell_args):
        super(LSTMLayer, self).__init__()
        self.cell = cell(*cell_args)

    def forward(self, input: Tensor, state: Tuple[Tensor, Tensor]) -> Tuple[Tensor, Tuple[Tensor, Tensor]]:
        inputs = input.unbind(0)
        outputs = torch.jit.annotate(List[Tensor], [])
        for i in range(len(inputs)):
            out, state = self.cell(inputs[i], state)
            outputs += [out]
        return torch.stack(outputs), state

class ReverseLSTMLayer(nn.Module):
    def __init__(self, cell, *cell_args):
        super(ReverseLSTMLayer, self).__init__()
        self.cell = cell(*cell_args)

    def forward(self, input: Tensor, state: Tuple[Tensor, Tensor]) -> Tuple[Tensor, Tuple[Tensor, Tensor]]:
        inputs = reverse(input.unbind(0))
        outputs = jit.annotate(List[Tensor], [])
        for i in range(len(inputs)):
            out, state = self.cell(inputs[i], state)
            outputs += [out]
        return torch.stack(reverse(outputs)), state

class BidirLSTMLayer(nn.Module):
    __constants__ = ['directions']

    def __init__(self, cell, *cell_args):
        super(BidirLSTMLayer, self).__init__()
        self.directions = nn.ModuleList([
            LSTMLayer(cell, *cell_args),
            ReverseLSTMLayer(cell, *cell_args),
        ])

    def forward(self, input: Tensor, states: List[Tuple[Tensor, Tensor]]) -> Tuple[Tensor, List[Tuple[Tensor, Tensor]]]:
        # List[LSTMState]: [forward LSTMState, backward LSTMState]
        outputs = jit.annotate(List[Tensor], [])
        output_states = jit.annotate(List[Tuple[Tensor, Tensor]], [])
        # XXX: enumerate https://github.com/pytorch/pytorch/issues/14471
        i = 0
        for direction in self.directions:
            state = states[i]
            out, out_state = direction(input, state)
            outputs += [out]
            output_states += [out_state]
            i += 1
        return torch.cat(outputs, -1), output_states

def init_stacked_lstm(num_layers, layer, first_layer_args, other_layer_args):
    layers = [layer(*first_layer_args)] + [layer(*other_layer_args)
                                           for _ in range(num_layers - 1)]
    return nn.ModuleList(layers)

class StackedLSTM(nn.Module):
    __constants__ = ['layers']  # Necessary for iterating through self.layers

    def __init__(self, num_layers, layer, first_layer_args, other_layer_args):
        super(StackedLSTM, self).__init__()
        self.layers = init_stacked_lstm(num_layers, layer, first_layer_args,
                                        other_layer_args)

    def forward(self, input: Tensor, states: List[Tuple[Tensor, Tensor]]) -> Tuple[Tensor, List[Tuple[Tensor, Tensor]]]:
        # List[LSTMState]: One state per layer
        output_states = jit.annotate(List[Tuple[Tensor, Tensor]], [])
        output = input
        # XXX: enumerate https://github.com/pytorch/pytorch/issues/14471
        i = 0
        for rnn_layer in self.layers:
            state = states[i]
            output, out_state = rnn_layer(output, state)
            output_states += [out_state]
            i += 1
        return output, output_states

# Differs from StackedLSTM in that its forward method takes
# List[List[Tuple[Tensor,Tensor]]]. It would be nice to subclass StackedLSTM
# except we don't support overriding script methods.
# https://github.com/pytorch/pytorch/issues/10733
class StackedLSTM2(nn.Module):
    __constants__ = ['layers']  # Necessary for iterating through self.layers

    def __init__(self, num_layers, layer, first_layer_args, other_layer_args):
        super(StackedLSTM2, self).__init__()
        self.layers = init_stacked_lstm(num_layers, layer, first_layer_args,
                                        other_layer_args)

    def forward(self, input: Tensor, states: List[List[Tuple[Tensor, Tensor]]]) -> Tuple[Tensor, List[List[Tuple[Tensor, Tensor]]]]:
        # List[List[LSTMState]]: The outer list is for layers,
        #                        inner list is for directions.
        output_states = jit.annotate(List[List[Tuple[Tensor, Tensor]]], [])
        output = input
        # XXX: enumerate https://github.com/pytorch/pytorch/issues/14471
        i = 0
        for rnn_layer in self.layers:
            state = states[i]
            output, out_state = rnn_layer(output, state)
            output_states += [out_state]
            i += 1
        return output, output_states

class StackedLSTMWithDropout(nn.Module):
    # Necessary for iterating through self.layers and dropout support
    __constants__ = ['layers', 'num_layers']

    def __init__(self, num_layers, layer, first_layer_args, other_layer_args):
        super(StackedLSTMWithDropout, self).__init__()
        self.layers = init_stacked_lstm(num_layers, layer, first_layer_args,
                                        other_layer_args)
        # Introduces a Dropout layer on the outputs of each LSTM layer except
        # the last layer, with dropout probability = 0.4.
        self.num_layers = num_layers

        if (num_layers == 1):
            warnings.warn("dropout lstm adds dropout layers after all but last "
                          "recurrent layer, it expects num_layers greater than "
                          "1, but got num_layers = 1")

        self.dropout_layer = nn.Dropout(0.4)

    def forward(self, input: Tensor, states: List[Tuple[Tensor, Tensor]]) -> Tuple[Tensor, List[Tuple[Tensor, Tensor]]]:
        # List[LSTMState]: One state per layer
        output_states = jit.annotate(List[Tuple[Tensor, Tensor]], [])
        output = input
        # XXX: enumerate https://github.com/pytorch/pytorch/issues/14471
        i = 0
        for rnn_layer in self.layers:
            state = states[i]
            output, out_state = rnn_layer(output, state)
            # Apply the dropout layer except the last layer
            if i < self.num_layers - 1:
                output = self.dropout_layer(output)
            output_states += [out_state]
            i += 1
        return output, output_states

def test_script_stacked_lnlstm(seq_len, batch, input_size, hidden_size,
                               num_layers):
    inp = torch.randn(seq_len, batch, input_size)
    states = [LSTMState(torch.randn(batch, hidden_size),
                        torch.randn(batch, hidden_size))
              for _ in range(num_layers)]
    rnn = script_lnlstm(input_size, hidden_size, num_layers)

    # just a smoke test
    out, out_state = rnn(inp, states)


########################################################################################################################
# This script is to be used with realtime sticknet obs
# The purpose of this script is to create reflectivity and velocity sticknet station plots,
# and reflectivity and thetaV and thetaE objective analysis plots
#
# This script should be run every 10 MINUTES during a project
#
# Initially developed by A. Hill, Overhauled by J. McDonald 2021
########################################################################################################################

import matplotlib
matplotlib.use('Agg')

import cartopy.crs as ccrs
import cartopy.feature as cfeature
import cartopy
from metpy.plots import USCOUNTIES
from metpy.plots import  StationPlot
from metpy.interpolate import interpolate_to_grid

import matplotlib.pyplot as plt
import matplotlib.cm as cm
from mpl_toolkits.axes_grid1 import make_axes_locatable
import matplotlib.axes as maxes
import matplotlib.patheffects as path_effects
import cmocean

import pandas as pd
from netCDF4 import num2date
import numpy as np
import pyart;
import glob
import pytz
import datetime as dt
from collections import OrderedDict

# IMPORTANT
from info import TZ, savedir, probe_locs

import SNmods as snmods
from functions import calc_dewpoint,calc_thetae,calc_thetav,C_to_F,calc_mslp,convert_wind,parse_currtime
from functions_plotting import plot_logo, scale_bar, drop_nans_var, homeyer_rainbow_ramp
from functions_radar import get_radar_from_aws,_nearestDate, get_radar_ID

# get refl colormap
homeyer_rainbow_ramp()

# read in cartopy information
crs = ccrs.PlateCarree()
# Get data to plot state and province boundaries
states_provinces = cfeature.NaturalEarthFeature(
        category='cultural',
        name='admin_1_states_provinces_lakes',
        scale='10m',
        facecolor='none')

def add_radar(ax, radar, field):
    '''
    returns information for title
    '''
    if field == 'reflectivity':
        vmin = 0
        vmax = 70
        sweep = 0
        cbar_label = 'Reflectivity (dBZ)'
        cmap = 'pyart_HomeyerRainbow_ramp'

    elif field == 'velocity':
        vmin  = -45
        vmax  = 45
        sweep = 1
        cbar_label = 'Radial Velocity (m/s)'
        cmap = cmocean.cm.balance

    display = pyart.graph.RadarMapDisplay(radar)
    lat_0 = display.loc[0]
    lon_0 = display.loc[1]

    ### Grab indeces and times from radar object to make nice title
    index_at_start = radar.sweep_start_ray_index['data'][0]
    time_at_start_of_radar = num2date(radar.time['data'][index_at_start],
                                      radar.time['units'])
    index_at_start = radar.sweep_start_ray_index['data'][0]
    ts = num2date(radar.time['data'][index_at_start], radar.time['units'])

    rdate = dt.datetime(ts.year, ts.month, ts.day, ts.hour, ts.minute, ts.second)
    timezone = pytz.timezone(TZ) #if this errors, replace TZ with "US/Central" or "US/Eastern", etc
    local_time = timezone.fromutc(rdate)
    fancy_date_string = local_time.strftime('%A, %B %d at %I:%M %p %Z')
    fancy_date_string_utc = time_at_start_of_radar.strftime('%Y-%m-%d %H:%M UTC')

    display.plot_ppi_map(field, sweep, cmap=cmap, ax=ax, vmin=vmin, vmax=vmax,
                         colorbar_flag=False, title_flag=False,
                        lat_lines=[0], lon_lines=[0])

    ### gives stylistic control over colorbar
    divider = make_axes_locatable(ax)
    cax = divider.append_axes("right", size="3%", pad=0.15, axes_class=maxes.Axes)
    norm = plt.Normalize(vmin=vmin, vmax=vmax)
    cbar = plt.colorbar(plt.cm.ScalarMappable(norm=norm, cmap=cmap), pad=0.02, cax=cax, orientation='vertical')
    cbar.set_label(label=cbar_label, size=16, weight='bold', labelpad=12)
    cbar.ax.tick_params(labelsize=15)

    ### Mark the radar
    ax.scatter(lon_0, lat_0, marker='o',s=30, c='r', ec='k', label=radar.metadata['instrument_name'])

    return fancy_date_string, fancy_date_string_utc, radar.metadata['instrument_name']

def radar_and_sticknet(lats, lons, data_df, mslp, dew_plot, IDs, u, v, obtime, radar, product='reflectivity'):

    ### find extent of plot
    # exact center of plot
    clat = np.amin(lats)+ abs(np.amax(lats) - np.amin(lats))/2
    clon = np.amin(lons) + abs(np.amin(lons) - np.amax(lons))/2

    # NOTE: change these hardcoded values if you want to change relative domain size
    # larger (smaller) numbers = larger (smaller) domain
    dlat = 0.78 * abs(np.amax(lats) - np.amin(lats))
    dlon = 0.78 * abs(np.amin(lons) - np.amax(lons))

    if dlon < .1:
        dlon = .1
    if dlat < .1:
        dlat = .1

    # find corners using the center and the buffers
    north_lat, south_lat = clat+dlat, clat-dlat
    west_lon, east_lon = clon-dlon, clon+dlon

    ### initialize figure
    fig = plt.figure(figsize = [10,10])
    ax = fig.add_subplot(1,1,1, projection=crs)
    ax.set_extent([west_lon, east_lon, north_lat,south_lat], crs )
    ax.add_feature(states_provinces, edgecolor='k', alpha=0.25, linewidth=1)
    ax.add_feature(USCOUNTIES.with_scale('20m'), alpha=0.4, linewidth=0.2)

    #### RADAR
    rtime, rtime_utc, radarname = add_radar(ax, radar, product)

    ##### STICKNET
    ax.plot(lons,lats,marker='s',color='k',markersize=5, linewidth=0)

    pe= [path_effects.Stroke(linewidth=3, foreground='white'), path_effects.Normal()]

    # use metpy to plot T, Td, MSLP (coded), and the 4-letter identifiers
    stationplot = StationPlot(ax, lons, lats,clip_on=True, transform=crs, fontsize=9, weight='bold')

    stationplot.plot_parameter((-1.5,1), data_df['T'].values, color='#b30000',
                               formatter='0.1f', path_effects=pe)
    stationplot.plot_parameter((-1.5,-1), dew_plot, color='darkgreen',
                               formatter='0.1f', path_effects=pe)
    stationplot.plot_parameter((1.5,1), mslp, formatter=lambda v: format(10 * v, '.0f')[-3:],
                               path_effects=pe)
    stationplot.plot_text((1.5, -.9), IDs, fontsize=8, weight='bold', path_effects=pe)

    # Add wind barbs, dropping bad vals, cuz WS can == -999
    ax.barbs(lons, lats, u, v, length=7.5,sizes={'emptybarb':.18}, lw=1.2,zorder=20)

    ### title
    ax.set_title('{0} {1}\n{2} ({3})\nStickNet Observations at {4}'.format(radarname,
                                product.capitalize(), rtime, rtime_utc, obtime.strftime('%D %H:%M UTC')),
                 fontsize=16, y=1.01)

    ### Plot the TTU logo, have it update location based on shape of base map
    plt.draw()
    plot_logo(fig, ax,alpha=0.7)

    ### Scale Bar
    center = np.average([ax.get_position().x0, ax.get_position().x1])
    scale_len = np.ceil((dlon*10)/3.5)*10
    if scale_len < 10: scale_len = 10
    scale_bar(fig, ax, length=int(scale_len), location=(center, ax.get_position().y0),fontsize=10)

    ### legend
    ax.legend(loc=1, fontsize=11,handletextpad=0.01,borderpad=0.3)

    # save figure
    nametime = parse_currtime()[1] # note that this may be different from last time on plot
                                 # THATS OKAY... it will make data drops more apparent!
    plt.savefig('{0}{1}_station_{2}.png'.format(savedir,product,nametime),dpi=300,bbox_inches = 'tight')
    plt.close()

def radar_and_oban(lats, lons, variable, name, savename, obtime, radar, line_color='C0',dK=1, product='reflectivity'):

    ### find extent of plot
    # exact center of plot
    clat = np.amin(lats)+ abs(np.amax(lats) - np.amin(lats))/2
    clon = np.amin(lons) + abs(np.amin(lons) - np.amax(lons))/2

    # NOTE: change these hardcoded values if you want to change relative domain size
    # larger (smaller) numbers = larger (smaller) domain
    dlat = 0.78 * abs(np.amax(lats) - np.amin(lats))
    dlon = 0.78 * abs(np.amin(lons) - np.amax(lons))

    if dlon < .1:
        dlon = .1
    if dlat < .1:
        dlat = .1

    # find corners using the center and the buffers
    north_lat, south_lat = clat+dlat, clat-dlat
    west_lon, east_lon = clon-dlon, clon+dlon

    ### initialize figure
    fig = plt.figure(figsize = [10,10])
    ax = fig.add_subplot(1,1,1, projection=crs)
    ax.set_extent([west_lon, east_lon, north_lat,south_lat], crs )
    ax.add_feature(states_provinces, edgecolor='k', alpha=0.25, linewidth=1)
    ax.add_feature(USCOUNTIES.with_scale('20m'), alpha=0.4, linewidth=0.2)

    #### RADAR
    rtime, rtime_utc, radarname = add_radar(ax, radar, product)

    ### OBAN
    ### interpolate data. Hres is 0.05 degrees
    varn, latsn, lonsn, FLAG = drop_nans_var(variable, lats, lons)
    gx,gy,img1 = interpolate_to_grid(np.asarray(lonsn),np.asarray(latsn),np.asarray(varn),\
                         interp_type='natural_neighbor',hres=.05)
    levels = np.arange(int(np.floor(np.amin(variable))),int(np.ceil(np.amax(variable)))+1,dK)
    ax.contour(gx, gy, img1, colors='w', levels=levels, linewidths=3)
    c=ax.contour(gx, gy, img1, colors=line_color, levels=levels, linewidths=1.5)

    # labels
    cl = ax.clabel(c, fontsize=10,inline=1, inline_spacing=2,fmt='%i',
              rightside_up=True, use_clabeltext=True)
    for t in cl:
        t.set_weight('bold')
        t.set_path_effects([path_effects.Stroke(linewidth=3, foreground='white'), path_effects.Normal()])

    # add box around the grid for reference
    x0, x1 = np.min(gx), np.max(gx)
    y0, y1 = np.min(gy), np.max(gy)

    ax.plot([x0,x0,x1,x1,x0],[y0,y1, y1, y0,y0], color='k', alpha=0.2, linewidth=3, linestyle='--')

    ### title
    ax.set_title('{0} {1}\n{2} ({3})\nStickNet {4} Analysis at {5}'.format(radarname,
                                product.capitalize(), rtime, rtime_utc, name, obtime.strftime('%D %H:%M UTC')),
                 fontsize=16, y=1.01)

    ### Plot the TTU logo, have it update location based on shape of base map
    plt.draw()
    plot_logo(fig, ax, alpha=0.7)

    ### Scale Bar
    center = np.average([ax.get_position().x0, ax.get_position().x1])
    scale_len = np.ceil((dlon*10)/3.5)*10
    if scale_len < 10: scale_len = 10
    scale_bar(fig, ax, length=int(scale_len), location=(center, ax.get_position().y0),fontsize=10)

    ### legend
    ax.legend(loc=1, fontsize=11,handletextpad=0.01,borderpad=0.3)

    ## warn if variable data is missing!
    if FLAG != 0:
        plt.draw()
        ax.text(center,0.98,  f'{FLAG} StickNets missing, plot may be inaccurate',
                fontsize=10,color='r',ha='center', va='center', transform=ax.transAxes)

    # save figure
    nametime = parse_currtime()[1] # note that this may be different from last time on plot
                                 # THATS OKAY... it will make data drops more apparent!
    plt.savefig('{0}{1}_{2}_{3}.png'.format(savedir,product,savename,nametime),dpi=300,bbox_inches = 'tight')
    plt.close()

################################
# pull in data

analysis_time = dt.datetime(2017,4,30,19,9) #dt.datetime.utcnow()
probes = list(probe_locs.keys())

# Grab the data from files in filedir we want to plot
data_df = snmods.get_sticknet_data(analysis_time,analysis_time,dataset='latest',probes=probes)

if not data_df.empty: # only works if you have data

    # pull sticknet locations and time
    lats =       data_df['Lat'].values
    lons =       data_df['Lon'].values
    IDs =        [ids[1:] for ids in data_df.index.values]
    obtime = data_df['date'][0]

    # calculate variables
    dewp_c = calc_dewpoint(data_df['T'].values,data_df['RH'].values)
    thetae = calc_thetae(data_df['T'].values,dewp_c, data_df['P'].values)
    thetav = calc_thetav(data_df['T'].values,dewp_c, data_df['P'].values)
    mslp = calc_mslp(data_df['T'].values, data_df['P'].values, data_df['Elevation'].values)
    u,v = convert_wind(data_df['WS'].values,data_df['WD'].values)

    # find appropriate radar, and pull in data
    clat = np.amin(lats)+ abs(np.amax(lats) - np.amin(lats))/2
    clon = np.amin(lons) + abs(np.amin(lons) - np.amax(lons))/2
    station, city = get_radar_ID(clat, clon)
    print('Radar: {}, from {}'.format(station, city))

    radar_namelist, radar_list = get_radar_from_aws(station, analysis_time, analysis_time)
    radar = radar_list[0]

    # radar products and station plots - this will be of Lubbock if no data is available
    radar_and_sticknet(lats, lons, data_df, mslp, dewp_c, IDs, u, v, obtime, radar, product='reflectivity')
    radar_and_sticknet(lats, lons, data_df, mslp, dewp_c, IDs, u, v, obtime, radar, product='velocity')

    # reflectivity and oban plots
    if len(data_df) >= 4: # at least 4 pts needed for interpolation
        radar_and_oban(lats, lons, thetav, r'${\theta_v}$','TV', obtime, radar, line_color='#002775', dK=1)
        radar_and_oban(lats, lons, thetae,r'${\theta_e}$', 'TE', obtime, radar, line_color='#014522', dK=1)

else:
    print('No data at {}'.format(analysis_time.strftime('%D %H:%M UTC')))


from instagrapi import Client

cl = Client()
# cl.login("getcomicsx", "Hentaimanga4321")
ww=cl.media_pk_from_url("https://www.instagram.com/reel/ChhsGIMjIJC/?utm_source=ig_web_copy_link")
# xx=cl.photo_download(ww) //pic download without uname, password
# xx=cl.video_download(ww)
# pk = cl.story_pk_from_url("https://www.instagram.com/stories/missamberfields/2987568729286260276/")
# s = cl.story_info(pk)
# we=cl.story_download_by_url(s.video_url)
# xx=cl.story_download(pk)
# we=cl.album_download(ww) #album download
# we=igtv_download() # igtv download
we=cl.clip_download(ww)	 # reel donwload
print(we)


from sys import stdin
from collections import defaultdict

def get_state(board):
  scores = {c:defaultdict(int) for c in 'XO'}
  for i, line in enumerate(board):
    for j, char in enumerate(line):
      for p in scores:
        if char in p+'T':
          scores[p]['row'+str(i)] += 1
          scores[p]['col'+str(j)] += 1
          if i == j:
            scores[p]['ondiag'] += 1
          if i == 3-j:
            scores[p]['offdiag'] += 1
  mscores = [(max(d.values()), p) for p, d in scores.items() if d.values()]

  if mscores and max(mscores)[0] == 4:
    return max(mscores)[1] + ' won'

  if '.' in ''.join(board):
    return "Game has not completed"

  return "Draw"

cases = int(stdin.readline().strip())
for i in range(cases):
  board = [stdin.readline().strip() for j in range(4)]
  print("Case #{}: {}".format(i+1, get_state(board)))
  stdin.readline()


# import the pygame module, so you can use it
import pygame, os.path, random
from pygame.locals import *

main_dir = os.path.split(os.path.abspath(__file__))[0]
transparent_color = (255, 0, 5)
screen_res = (640, 480)
SCREENRECT = pygame.Rect(0, 0, screen_res[0], screen_res[1])
BADGUY_RELOAD = 12

def load_image(file):
	'''loads an image, prepares it for play'''
	file = os.path.join(main_dir, 'images', file)
	surface = pygame.image.load(file)
	surface.set_colorkey(transparent_color)
	return surface.convert()

def load_images(*files):
	'''sends multiple images to load_image'''
	imgs = []
	for file in files:
		imgs.append(load_image(file))
	return imgs

class Player(pygame.sprite.Sprite):
	'''dats u'''
	speed = 10
	bounce = 24
	images = []
	def __init__(self):
		pygame.sprite.Sprite.__init__(self, self.containers)
		self.rect = self.image.get_rect(midbottom=SCREENRECT.midbottom)
		self.reloading = 0
		self.origtop = self.rect.top

	def move(self, direction):
		self.rect.move_ip(direction*self.speed, 0)
		self.rect = self.rect.clamp(SCREENRECT)
		self.rect.top = self.origtop - (self.rect.left//self.bounce%2)

class Badguy(pygame.sprite.Sprite):
	speed = 5
	animcycle = 12
	images = []
	def __init__(self):
		pygame.sprite.Sprite.__init__(self, self.containers)
		# pick a random badguy to be
		self.image = self.images[random.randint(0,4)-1]
		self.rect = self.image.get_rect()
		self.frame = 0
		self.direction = 1

	def update(self):
		self.rect.move_ip(self.direction*self.speed, 0)
		if not SCREENRECT.contains(self.rect):
			# classic centipede motion
			self.rect.top = self.rect.bottom + 5
			self.direction = -1 * self.direction
			self.rect = self.rect.clamp(SCREENRECT)
			if self.rect.bottom >= screen_res[1]:
				self.kill()

def main():
	pygame.init()

	# set resolution, decorate window
	screen = pygame.display.set_mode(screen_res)
	logo = load_image('logo32x32.png')
	pygame.display.set_icon(logo)
	pygame.display.set_caption('SPACE INVEIGLERS')

	running = True

	# load images, assign sprites
	background = load_image('background.png')
	Player.image = load_image('playerlg.png')
	Badguy.images = load_images('badguy1lg.png', 'badguy2lg.png', 'badguy3lg.png', 'badguy4lg.png')

	screen.blit(background, (0, 0))
	pygame.display.flip()

	# init game groups
	badguys = pygame.sprite.Group()
	all = pygame.sprite.RenderUpdates()

	# assign default groups to each sprite class
	Player.containers = all
	Badguy.containers = badguys, all
	badguyreload = BADGUY_RELOAD
	clock = pygame.time.Clock()

	#initialize our starting sprites
	player = Player()
	Badguy() #note, this 'lives' because it goes into a sprite group

	# main loop
	while player.alive():
		screen.blit(background, (0,0))
		#get input
		for event in pygame.event.get():
			if event.type == QUIT or \
				(event.type == KEYDOWN and event.key == K_ESCAPE):
					return
		keystate = pygame.key.get_pressed()

		# clear/erase the last drawn sprites
		all.clear(screen, background)

		#update all the sprites
		all.update()

		#handle player input
		direction = keystate[K_RIGHT] - keystate[K_LEFT]
		player.move(direction)

		# Create new badguy
		if badguyreload:
			badguyreload = badguyreload - 1
		elif not int(random.random() * 50):
			Badguy()
			badguyreload = BADGUY_RELOAD

		#draw the scene
		screen.blit(background, (0,0))
		dirty = all.draw(screen)
		pygame.display.update(dirty)

		#cap the framerate
		clock.tick(40)

# run the main function only if this module is executed as the main script
# (if you import this as a module then nothing is executed)
if __name__=="__main__":
	# call the main function
	main()


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

"""
实现统计一篇英文文章内每个单词的出现频率，并返回出现频率最高的前10个单词及其出现次数

"""

import os
import re
from collections import Counter
from optparse import OptionParser

def word_count(filename, num=10):
    with open(filename, "r") as fd:
        all = Counter()
        for line in fd.readlines():
            count = Counter(re.split(r"[^a-zA-Z0-9-]+", line))
            print(count)
            all.update(count)

        all[''] =0

        return all.most_common(num)

if __name__ == "__main__":
    usage = """
    %prog file_name
    """

    parser = OptionParser(usage=usage)
    parser.add_option("--file", dest="filename", help="file to count the words")

    (options, args) = parser.parse_args()

    if not options.filename or not os.path.exists(options.filename):
        parser.print_help()
        exit(1)

    count = word_count(filename=options.filename)
    for k, v in count:
        print(k, v)