import regex as re
from bs4 import BeautifulSoup
import requests
import requests_futures
import aiohttp
import asyncio
from requests_futures.sessions import FuturesSession
import tree
import json

trees = []

valid_regex = r"^https?\:\/\/([\w\.]+)wikipedia.org\/wiki\/([\w]+\_?)+"
links = dict()

loop = asyncio.get_event_loop()
def check_valid(link):
    if re.split(valid_regex, link):
        respond = requests.get(link)
        if respond.status_code != 200:
            return True
    else:
        return True

def check_valid_int(intvalue):
    if intvalue>=1 and intvalue<=20:
        return True

""" its my bad tryis without good asyncio
async def run_link(link):
    try:
        response = await requests.get(link)
        links = []
        soup = BeautifulSoup(response.text, "html.parser")
        links_ = soup.find(id="bodyContent").find_all("a")
        for link in links_:
            if 'href' in link.attrs:
                if link.attrs['href'].startswith('/wiki/')and check_valid("https://en.wikipedia.org" + link['href']):
                    links.append("https://en.wikipedia.org" + link['href'])
                elif link.attrs['href'].startswith('https') and check_valid(link['href']): #DO IT WITH WIKIIIIIIIII
                    links.append(link['href'])
        return links
    except:
        print("An exception occurred")

    finally:
        return links

def appendaslist(links):
    async_list = []
    for i in links:
        async_list.append(loop.create_task(run_link(i)))
    return async_list

async def main(n,link):
    #action_item =requestsf.get(link, hooks={'link': run_link})
    links[link] = run_link(link)
    async_list = []
    async_list = appendaslist(links[link])
    loop.run_until_complete(asyncio.gather(*async_list))

    #async_list.append(action_item)
    for i in range(1,n):
        # async with aiohttp.ClientSession() as session:
        tasks = []
        for c in list(links.keys()):
            for j in list(links[c]):
                if j not in checkset:
                    appendlink =run_link(j)
                    async_list.append(loop.create_task(run_link(j)))
                    loop.run_until_complete(asyncio.gather(*async_list))
                    links[j] =  appendlink
                    checkset.update(appendlink)
                print(links)

    print(links)

async def check_valida(session,link):
    async with session.get(link) as resp:
        if re.split(valid_regex, link):
            if resp.status_code != 200:
                return False
        else:
            return False
"""
async def run_linkas(session,link):
    async with session.get(link) as resp:
        response = await resp.text()
        links = []
        soup = BeautifulSoup(response, "html.parser")

        links_ = soup.find(id="bodyContent").find_all('a', href=True)

        for link1 in links_:
            if 'href' in link1.attrs:
                if link1.attrs['href'].startswith('/wiki/'):#and check_valida(session,"https://en.wikipedia.org" + link['href']):
                    links.append("https://en.wikipedia.org" + link1['href'])
                elif link1.attrs['href'].startswith('https://en.wikipedia') :#and check_valida(session,link['href']): #DO IT WITH WIKIIIIIIIII
                    links.append(link1['href'])
        return link,links

async def mains(url, n):
    checksett = set()
    checkset = set()
    trees = []
    allofall = {}
    async with aiohttp.ClientSession() as session:
        pokemon_url = [url]
        checksett.add(url)
        allofall[url] = [url]
        for i in range(0,n):
            tasks=[]
            for j in pokemon_url:
                if j not in checkset:
                    tasks.append(asyncio.ensure_future(run_linkas(session, j)))

            original_pokemon = await asyncio.gather(*tasks)

            checkset |= checksett

            for pokemon in original_pokemon:
                pokemon_url =pokemon[1]
                #trees += pokemon[1]
                checksett |= set(pokemon[1])
                allofall[pokemon[0]] = pokemon_url
    # for x in allofall: \\ its to print
    #     print(x)
    #     print(x, ':', allofall[x])
    return allofall

def savejson(namefile, data):
    with open(namefile, 'w+') as fp:
        json.dump(data, fp)

if __name__ == "__main__":
    global checkset
    checkset = set()
    input_link = input("Input Wiki link: ")
    if check_valid(input_link):
        print("Url is not valid, please enter valid url")
    else:
        input_int = int(input("Input Integer link: "))
        if check_valid_int(input_int):
            data = asyncio.run(mains(input_link,input_int))
            savejson(r"D:\testtask\testtask\data.json",data)
        else:
            print("Int is not valid, please enter valid int from 1 to 20")


"""Tests for the config API."""

import unittest

from pygame_assets.exceptions import NoSuchConfigurationParameterError
from pygame_assets.configure import Config, ConfigMeta
from pygame_assets.configure import get_config, config_exists, remove_config
from pygame_assets.configure import get_environ_config, set_environ_config
from .utils import change_config, temp_config

class TestConfigMeta(unittest.TestCase):
    """Unit tests for the config._params API."""

    def test_get_class_attributes(self):
        class Foo:
            a = 1
            b = 2

            __builtins_are_ignored__ = 'bar'

            def functions_are_ignored():
                return 0

        self.assertDictEqual(ConfigMeta.get_class_attributes(Foo),
                             {'a': 1, 'b': 2})

    def test_two_config_instances_are_equal(self):
        config1 = Config()
        config2 = Config()
        self.assertEqual(config1, config2)

    def test_subconfig_inherits_config_meta(self):
        class SubConfig(Config):
            name = 'sub'

        config = Config()
        subclassed = SubConfig()
        for param in config._meta:
            self.assertIn(param, subclassed._meta)
            self.assertEqual(subclassed._meta[param], config._meta[param])

    def test_new_subconfig_parameters_are_saved(self):
        class SubConfig(Config):
            name = 'sub'

            class Meta:
                base = 'mybase'

        subclassed = SubConfig()
        self.assertEqual(subclassed._meta['base'], 'mybase')

    def test_subconfig_inherits_super_meta(self):

        with temp_config('custom', 'sub'):
            class SuperConfig(Config):
                name = 'custom'

                class Meta:
                    base = 'custom/base'

            class SubConfig(SuperConfig):
                name = 'sub'

            superconfig = SuperConfig()
            subconfig = SubConfig()
            self.assertEqual(subconfig.base, superconfig.base)

    def test_not_registered_parameter_raises_exception(self):
        with self.assertRaises(NoSuchConfigurationParameterError):
            class SubConfig(Config):
                name = 'sub'

                class Meta:
                    not_registered = 'will raise an exception'

    def test_config_has_meta_dict(self):
        config = get_config()
        self.assertIsNotNone(config._meta)
        self.assertIsInstance(config._meta, dict)

    def test_meta_contains_all_parameters(self):
        config = get_config()
        for param in ConfigMeta._allowed_params:
            self.assertIn(param, config._meta)

    def test_get_parameter_through_dot_notation(self):
        config = get_config()
        for param in config._meta:
            self.assertEqual(getattr(config, param), config._meta.get(param))

    def test_set_parameter_through_dot_notation(self):
        name = ConfigMeta._allowed_params[0]
        with change_config(name) as config:
            setattr(config, name, 'other')
            self.assertEqual('other', getattr(config, name))

    def test_set_parameter_propagates_accross_get_config(self):
        name = ConfigMeta._allowed_params[0]
        with change_config(name) as config:
            setattr(config, name, 'other')
            self.assertEqual(getattr(get_config(), name), 'other')

    def test_has_default_font_size(self):
        config = get_config()
        self.assertIsNotNone(config.default_font_size)

    def test_set_config_base(self):
        with change_config('base') as config:
            self.assertEqual('./assets', config.base)
            config.base = 'static'
            self.assertEqual('static', config.base)

class TestConfigureUtils(unittest.TestCase):
    """Test configuration management utilities."""

    def test_remove_config(self):
        class FooConfig(Config):
            name = 'foo'

        self.assertTrue(config_exists('foo'))
        remove_config('foo')
        self.assertFalse(config_exists('foo'))

    def test_config_exists(self):
        self.assertFalse(config_exists('foo'))

        class FooConfig(Config):
            name = 'foo'

        self.assertTrue(config_exists('foo'))
        remove_config('foo')

class TestConfigureFromEnv(unittest.TestCase):
    """Test the configuration from OS environment variables."""

    def setUp(self):
        set_environ_config(None)

    def test_set_environ_config(self):
        set_environ_config(None)
        self.assertIsNone(get_environ_config())
        set_environ_config('foo')
        self.assertEqual('foo', get_environ_config())

    def test_set_environ_config_to_none_is_explicit(self):
        with self.assertRaises(TypeError):
            set_environ_config()
        set_environ_config(None)

    def test_get_config_from_env_var(self):
        class MyConfig(Config):
            name = 'myconfig'

        set_environ_config('myconfig')
        config = get_config()
        self.assertEqual('myconfig', config.name)

class TestConfigDirs(unittest.TestCase):
    """Unit tests for the search directory configuration."""

    def test_config_dirs_is_dict_of_loader_names_and_dirs(self):
        config = get_config()
        for loader_name, dirs in config.dirs.items():
            self.assertIsInstance(loader_name, str)
            self.assertIsInstance(dirs, list)

    def test_search_dirs(self):
        with change_config('base', 'dirs') as config:
            config.base = 'assets'
            config.dirs['spritesheet'] = ['spritesheet', 'sheets']
            expected = ['assets/spritesheet', 'assets/sheets']
            actual = config.search_dirs('spritesheet')
            self.assertListEqual(expected, actual)

class TestConfigure(unittest.TestCase):
    """Unit tests for the Config API."""

    def setUp(self):
        set_environ_config(None)

    def test_load_default_config(self):
        explicit_config = get_config('default')
        self.assertEqual(explicit_config.name, 'default')
        no_args_config = get_config()
        self.assertEqual(no_args_config.name, 'default')

    def test_load_test_config(self):
        config = get_config('test')
        self.assertEqual(config.name, 'test')

    def test_new_config_must_have_name(self):
        with self.assertRaises(ValueError):
            class ConfigWithoutName(Config):
                name = ''

    def test_new_config_is_registered_to_get_config(self):
        # assert custom config does not exist already
        with self.assertRaises(KeyError):
            get_config('custom')

        class CustomConfig(Config):
            name = 'custom'
            base = 'custom/path/to/assets'

        # custom config should now be available through get_config()
        config = get_config('custom')
        self.assertEqual('custom', config.name)
        self.assertEqual('custom/path/to/assets', config.base)


import argparse
from dis import dis
import gym
import numpy as np
from itertools import count

import pyximport; pyximport.install()

import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
from torch.distributions import MultivariateNormal
import matplotlib.pyplot as plt
import pybullet
import os

from tensorboardX import SummaryWriter

from config import env_name, log_interval, device, lr, gamma, env_seed, torch_seed, rand_init

from arguments import get_args

def save_state(net, opt, torch_seed, env_seed, fname, np_seed=None, py_seed=None,):
    # save both model state and optimizer state
    states = {
        'state_dict': net.state_dict(),
        'optimizer': opt.state_dict(),
        'torch_seed': torch_seed,
        'env_seed': env_seed,
        'gamma': gamma,
        'lr' : lr
        # 'py_seed': py_seed,
        # 'np_seed': np_seed
    }
    torch.save(states, fname)

class GaussNet(nn.Module):
    def __init__(self):
        super(GaussNet, self).__init__()
        self.affine1 = nn.Linear(8, 32)
        self.linear = nn.Linear(32,32)
        self.affine2 = nn.Linear(32, 2)

    def forward(self, x):
        x = self.affine1(x)
        x = F.relu(x)
        x = self.linear(x)
        x = F.relu(x)
        mu = self.affine2(x)
        x = F.relu(x)
        return mu

def get_action(state, net, device):
    state = torch.from_numpy(state).float().to(device)
    mu = net(state).to(device)
    #calculate the probability
    dist = MultivariateNormal(mu, 0.1*torch.eye(2).to(device))
    action = dist.sample()
    log_prob = dist.log_prob(action)
    return action, log_prob

def update_policy(policy_loss,optimizer, device):
    optimizer.zero_grad()
    policy_loss = torch.cat(policy_loss).sum().to(device)
    policy_loss.backward()
    optimizer.step()

def main():
    args = get_args()
    gym.logger.set_level(40)
    env = gym.make(env_name,rand_init=rand_init)
    env.seed(env_seed)
    torch.manual_seed(torch_seed)

    state= env.reset()

    num_inputs = state.shape[0]
    num_actions = env.action_space.shape[0]
    print('state size:', num_inputs)
    print('action space shape, highs, lows : ', env.action_space.shape," ",env.action_space.high," ",env.action_space.low)

    net = GaussNet().to(device)
    optimizer = optim.Adam(net.parameters(), lr=lr)

    writer = SummaryWriter('logs')

    if type(args.load_train) == list and (args.load_train[0] >-1 and args.load_train[1] >-1) :
        load_path='{}_ep_{}_it_{}_type_{}.pkl'.format(args.rl_alg,args.load_train[0],args.load_train[1],args.alg_type)
        if not os.path.exists(os.path.join(args.model_path,load_path)):
            raise Exception("load path {} doesn't exist".format(load_path))
        os.system('clear')
        print(" Loading ... {}".format(load_path))

        net.load_state_dict(torch.load(os.path.join(args.model_path,load_path))["state_dict"])
        optimizer.load_state_dict(torch.load(os.path.join(args.model_path,load_path))["optimizer"])
        start_iteration = args.load_train[1]
        start_episode = 0
    else:
        start_iteration = 0
        start_episode = 0
    net.train()

    steps=0

    avg_itr_reward = []
    num_iter = args.num_iterations
    num_episodes = args.num_episodes
    for itr in range(start_iteration,start_iteration + num_iter):
        total_ep_reward = 0
        log_probs_itr = []
        policy_loss_itr = []
        returns_itr = []
        for i_episode in range(num_episodes):
            state, ep_reward = env.reset(), 0
            done = False
            rewards = []
            log_probs = []
            returns=[]
            while not done:
                steps=steps+1;
                action, log_prob = get_action(state, net, device)
                state, reward, done, _ = env.step(action.cpu().numpy())
                # print(reward)
                ep_reward +=reward
                rewards.append(reward)
                log_probs.append(log_prob)
            #Compute the returns from the rewards
            R = 0
            for r in rewards[::-1]:
                R = r + gamma * R
                returns.insert(0, R)
            returns = torch.FloatTensor(returns).to(device)
            #Store the log probability and returns to calculate the loss function
            log_probs_itr.append(log_probs)
            returns_itr.append(returns)
            total_ep_reward += ep_reward

        returns_ep = torch.cat(returns_itr)
        # Find the mean and std of the returns over the episodes to compute
        mean  = returns_ep.mean()
        std = returns_ep.std()
        for log_probs, returns in zip(log_probs_itr,returns_itr):
            # Use VANILLA REINFORCE with baseline
            returns = (returns - mean) / std
            policy_loss_ep = []
            for log_prob, return_ in zip(log_probs,returns):
                policy_loss_ep.append(-log_prob * return_)
            policy_loss_ep = torch.stack(policy_loss_ep).to(device)
            policy_loss_itr.append(policy_loss_ep)
        avg_itr_reward.append(total_ep_reward / num_episodes)
        # Update the policy neural network parameter using the Loss function
        update_policy(policy_loss_itr,optimizer,device)
        if itr % log_interval == 0:
            print('iteration: {} | loss: {:.3f} | steps: {} | avg_reward: {:.2f}'.format(
                itr, policy_loss_ep.sum(), steps, avg_itr_reward[-1]))
            writer.add_scalar('log/avg_reward', float(avg_itr_reward[-1]), itr)
            writer.add_scalar('log/loss', float(policy_loss_ep.sum()), itr)
    if type(args.load_train) == list and (args.load_train[0] >-1 and args.load_train[1] >-1) :
        model_path='{}_ep_{}_it_{}_type_{}.pkl'.format(args.rl_alg,args.num_episodes,args.load_train[1]+args.num_iterations,args.alg_type)
    else:
        model_path='{}_ep_{}_it_{}_type_{}.pkl'.format(args.rl_alg,args.num_episodes,args.num_iterations,args.alg_type)

    if not os.path.exists(args.model_path):
        os.makedirs(args.model_path)
    save_state(net, optimizer, torch_seed, env_seed, os.path.join(args.model_path,model_path))

    plt.figure()
    plt.plot(range(itr+1),avg_itr_reward)
    plt.title("Average Cumulative Reward vs iteration")
    plt.xlabel("iteration")
    plt.ylabel("Average Reward")
    if not os.path.exists(args.save_path):
        os.makedirs(args.save_path)
    plt.savefig("{}/{}_plot.png".format(args.save_path,model_path[:-4]))

if __name__ == '__main__':
    main()


"""
英制单位与公制单位互换
1英寸=2.54厘米
"""
a = float(input('请输入长度：'))
unit = input('请输入单位：')
if unit == '英寸':
    print('%d英寸等于%d厘米' % (a, a * 2.54))
elif unit == '厘米':
    print('%d厘米等于%d英寸' % (a, a / 2.54))
else:
    print('请输入正确的单位')

Day3/英制单位与公制单位互换.py

英制单位与公制单位互换.py


from app.visual_detector.workers import (
    FrameReaderThread, TowerDetectorThread, ComponentDetectorThread, DefectDetectorThread,
    DefectTrackingThread, ResultsProcessorThread, TiltDetectorThread, DumperClassifierThread,
    WoodCracksDetectorThread
)
from app.visual_detector.defect_detectors import (
    TowerDetector, ComponentsDetector, DumperClassifier, WoodCrackSegmenter,
    DefectDetector, LineModifier, ConcretePoleHandler, WoodCracksDetector
)
from app.visual_detector.utils import (
    Drawer, ResultSaver, ObjectTracker
)
from typing import List
import queue
import os
import uuid
import time

class MainDetector:

    def __init__(
            self,
            save_path: str,
            batch_size: int,
            detectors_to_run: List[str],
            search_defects: bool = True,
            db=None
    ):
        # Path on the server where processed data gets stored
        self.save_path = save_path
        self.search_defects = search_defects
        # To keep track of file processing
        self.progress = dict()

        if search_defects:
            self.check_defects = True
            # Metadata extractor could be initialized here if required
        else:
            self.check_defects = False

        # Initialize Qs and workers
        try:
            # --- video / image processors ---
            self.files_to_process_Q = queue.Queue()
            self.frame_to_pole_detector = queue.Queue(maxsize=2)
            self.pole_to_comp_detector = queue.Queue(maxsize=2)
            self.comp_to_defect_detector = queue.Queue(maxsize=3)
            self.defect_det_to_defect_tracker = queue.Queue(maxsize=2)
            self.defect_tracker_to_writer = queue.Queue(maxsize=2)

            # --- dumper classifier ---
            self.to_dumper_classifier = queue.Queue(maxsize=3)
            self.from_dumper_classifier = queue.Queue(maxsize=3)
            # --- tilt detector ---
            self.to_tilt_detector = queue.Queue(maxsize=3)
            self.from_tilt_detector = queue.Queue(maxsize=3)
            # --- wood tower cracks ---
            self.to_wood_cracks_detector = queue.Queue(maxsize=3)
            self.from_wood_cracks_detector = queue.Queue(maxsize=3)
            # --- conrete tower cracks ---
            pass
        except Exception as e:
            print(f"Failed during queue initialization. Error: {e}")
            raise e

        # Initialize detectors and auxiliary modules
        try:
            # Object detectors
            self.pole_detector = TowerDetector()
            self.component_detector = ComponentsDetector()

            # Defect detectors
            self.defect_detector = DefectDetector(
                running_detectors=detectors_to_run,
                tilt_detector=(self.to_tilt_detector, self.from_tilt_detector),
                dumpers_defect_detector=(self.to_dumper_classifier, self.from_dumper_classifier),
                wood_crack_detector=(self.to_wood_cracks_detector, self.from_wood_cracks_detector),
                concrete_cracks_detector=None,
                insulators_defect_detector=None
            )

            # Auxiliary modules
            self.drawer = Drawer(save_path=save_path)
            self.result_saver = ResultSaver()
            self.object_tracker = ObjectTracker()

        except Exception as e:
            print(f"Failed during detectors initialization. Error: {e}")
            raise e

        # Initialize threads
        self.threads_to_run_and_join = list()
        try:
            self.frame_reader_thread = FrameReaderThread(
                batch_size=batch_size,
                in_queue=self.files_to_process_Q,
                out_queue=self.frame_to_pole_detector,
                progress=self.progress
            )
            self.threads_to_run_and_join.append(self.frame_reader_thread)

            self.pole_detector_thread = TowerDetectorThread(
                in_queue=self.frame_to_pole_detector,
                out_queue=self.pole_to_comp_detector,
                poles_detector=self.pole_detector,
                progress=self.progress
            )
            self.threads_to_run_and_join.append(self.pole_detector_thread)

            self.component_detector_thread = ComponentDetectorThread(
                in_queue=self.pole_to_comp_detector,
                out_queue=self.comp_to_defect_detector,
                component_detector=self.component_detector,
                progress=self.progress
            )
            self.threads_to_run_and_join.append(self.component_detector_thread)

            self.defect_detector_thread = DefectDetectorThread(
                in_queue=self.comp_to_defect_detector,
                out_queue=self.defect_det_to_defect_tracker,
                defect_detector=self.defect_detector,
                progress=self.progress,
                check_defects=search_defects
            )
            self.threads_to_run_and_join.append(self.defect_detector_thread)

            self.defect_tracker_thread = DefectTrackingThread(
                in_queue=self.defect_det_to_defect_tracker,
                out_queue=self.defect_tracker_to_writer,
                object_tracker=self.object_tracker,
                results_saver=self.result_saver,
                progress=self.progress,
                database=db
            )
            self.threads_to_run_and_join.append(self.defect_tracker_thread)

            self.results_processor_thread = ResultsProcessorThread(
                in_queue=self.defect_tracker_to_writer,
                save_path=save_path,
                drawer=self.drawer,
                progress=self.progress
            )
            self.threads_to_run_and_join.append(self.results_processor_thread)

            # -------------- DEFECT DETECTORS ---------------
            if "dumper" in detectors_to_run:
                dumper_classifier = DumperClassifier()
                self.dumper_classifier_thread = DumperClassifierThread(
                    in_queue=self.to_dumper_classifier,
                    out_queue=self.from_dumper_classifier,
                    dumper_classifier=dumper_classifier,
                    progress=self.progress
                )
                self.threads_to_run_and_join.append(self.dumper_classifier_thread)

            if "pillar" in detectors_to_run:
                self.tilt_detector_thread = TiltDetectorThread(
                    in_queue=self.to_tilt_detector,
                    out_queue=self.from_tilt_detector,
                    tilt_detector=ConcretePoleHandler(line_modifier=LineModifier),
                    progress=self.progress
                )
                self.threads_to_run_and_join.append(self.tilt_detector_thread)

            if "wood" in detectors_to_run:
                wood_tower_segment = WoodCrackSegmenter()
                self.wood_cracks_thread = WoodCracksDetectorThread(
                    in_queue=self.to_wood_cracks_detector,
                    out_queue=self.from_wood_cracks_detector,
                    wood_cracks_detector=WoodCracksDetector,
                    wood_tower_segment=wood_tower_segment,
                    progress=self.progress
                )
                self.threads_to_run_and_join.append(self.wood_cracks_thread)

        except Exception as e:
            print(f"Failed during workers initialization. Error: {e}")
            raise e

        # Start the app by launching all workers
        self.start()

    def predict(
            self,
            request_id: str,
            path_to_data: str,
            pole_number: int
    ) -> dict:
        """
        API endpoint - parses input data, puts files provided to the Q if of appropriate extension
        :param request_id:
        :param path_to_data:
        :param pole_number:
        :return:
        """
        file_IDS = {}

        if os.path.isfile(path_to_data):
            file_id = self.check_type_and_process(
                path_to_file=path_to_data,
                pole_number=pole_number,
                request_id=request_id
            )
            file_IDS[path_to_data] = file_id
            return file_IDS

        elif os.path.isdir(path_to_data):
            for item in os.listdir(path_to_data):
                path_to_file = os.path.join(path_to_data, item)
                file_id = self.check_type_and_process(
                    path_to_file=path_to_file,
                    pole_number=pole_number,
                    request_id=request_id
                )
                file_IDS[path_to_file] = file_id
        else:
            print(f"ERROR: Cannot process the file {path_to_data}, neither a folder nor a file")

        return file_IDS

    def check_type_and_process(
            self,
            path_to_file: str,
            pole_number: int,
            request_id: str
    ) -> str:
        """
        Checks whether a file can be processed
        :return: file's ID if it was put in the Q or "None" if extension is not supported
        """
        filename = os.path.basename(path_to_file)

        if any(filename.endswith(ext) for ext in ["jpg", "JPG", "jpeg", "JPEG", "png", "PNG"]):
            print(f"Added image {filename} to the processing queue")
            return self.process_file(
                path_to_file=path_to_file,
                pole_number=pole_number,
                file_type="image",
                request_id=request_id
            )

        elif any(filename.endswith(ext) for ext in ["avi", "AVI", "MP4", "mp4"]):
            print(f"Added video {filename} to the processing queue")
            return self.process_file(
                path_to_file=path_to_file,
                pole_number=pole_number,
                file_type="video",
                request_id=request_id
            )
        else:
            print(f"ERROR: file {filename}'s extension is not supported")
            return "None"

    def process_file(
            self,
            path_to_file: str,
            pole_number: int,
            file_type: str,
            request_id: str
    ) -> str:
        """

        :param path_to_file:
        :param pole_number:
        :param file_type:
        :param request_id:
        :return:
        """
        # Each file to process gets a unique ID number to track its progress
        file_id = str(uuid.uuid4())

        # Keep track of processing progress
        self.progress[file_id] = {
            "status": "Awaiting processing",
            "request_id": request_id,
            "file_type": file_type,
            "pole_number": pole_number,
            "path_to_file": path_to_file,
            "total_frames": None,
            "now_processing": 0,
            "processed": 0,
            "defects": []
        }
        self.files_to_process_Q.put(file_id)

        return file_id

    def start(self) -> None:
        for thread in self.threads_to_run_and_join:
            thread.start()

    def stop(self) -> None:
        """Wait until all threads complete their job and exit"""
        self.files_to_process_Q.put("STOP")
        for thread in self.threads_to_run_and_join:
            thread.join()


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import os
import re
import sys
import argparse
__author__ = 'menghao'
__mail__ = 'haomeng@genome.cn'

bindir = os.path.abspath(os.path.dirname(__file__))
pat1 = re.compile('^\s*$')
sys.path.append(bindir + '/../lib')
from common import parser_fasta

complement = {'A':'T','T':'A','C':'G','G':'C'}

def main():
    parser = argparse.ArgumentParser(usage='Locating_Restriction_Sites',
                                     description=__doc__,
                                     formatter_class=argparse.RawDescriptionHelpFormatter,
                                     epilog='author:\t{0}\nmail:\t{1}'.format(__author__, __mail__))
    parser.add_argument('-f', '--fasta', help='fasta format file', dest='fasta', required=True)
    args = parser.parse_args()

    Seq = parser_fasta(args.fasta).values()[0]

    rev_com = ''.join([complement[i] for i in Seq])

    for index in range(0,len(Seq)):
        for length in range(4,13):
            sub = Seq[index:index+length]
            if (4<=len(sub)<=13) & (length == len(sub)):
                rev_com = ''.join([complement[i] for i in reversed(sub)])
                if sub == rev_com:
                    print(str(index+1) + ' ' + str(length))

if __name__ == "__main__":
    main()


# 今まで使った文字の中で**最大**の文字+1まで使える

from itertools import zip_longest

N = int(input())

# def convert_to_normal(s):
#     is_visited = [0]*26
#     converted = ["_"]*26
#     x = 0
#     for s_i in s:
#         num = ord(s_i) - ord('a')
#         if is_visited[num]:
#             continue
#         is_visited[num] = 1
#         converted[num] = chr(ord('a') + x)
#         x += 1
#     t = "".join(converted[ord(s_i) - ord('a')] for s_i in s)
#     return t

t = [[0]]
for i in range(1, N):
    q = []
    for t_j in t:
        for k in range(max(t_j) + 2):
            q.append(t_j + [k])
    # print(q)
    t = q

# if len(t) != len(set(map(tuple, t))):
#     print("length is different")

# chars = "abcdefghij"[:N]
# def all_pattern(i):
#     if i == 0:
#         return [""]
#     l = []
#     for p in all_pattern(i-1):
#         for c in chars:
#             l.append(p + c)
#     return (l)

l = ["".join([chr(ord('a') + t_ij) for t_ij in t_i]) for t_i in t]
[print(l_i) for l_i in l]

# a = [p for p in sorted(all_pattern(N)) if p == convert_to_normal(p)]
# [print(a_i) for a_i in a]

# if l != a:
#     print("DIFF")
#     for l_i, a_i in zip_longest(l, a, fillvalue="_"*N):
#         print(l_i, a_i, (l_i != a_i)*"DIFF")

################################

# t = "abcdefghijk"[:N]
# def solve(idx):
#     if idx == 0:
#         return [""]
#     l = []
#     for s in solve(idx-1):
#         for t_i in t:
#             l.append(s + t_i)
#     return (l)

# print(solve(N))
# # for s in solve(N):


import requests
import sys
import json

def post_predict(host:str):
    '''
    Sends an HTTP POST request to the /predict endpoint.
    '''
    proto = ''
    if not host.startswith('http'):
        proto = 'http://'

    with open('manualtests/manual_predict_data.json', 'r') as f:
        data = json.loads(f.read())
        response = requests.post(proto + host, json=data)

    return response

if __name__ == '__main__':
    args = sys.argv
    print('Requesting for a prediction...')
    response = post_predict(host=args[1])
    if response.ok:
        print(' ...✅ OK:', response.json())
    else:
        print(' ...❌ failed:', response.content)


"""
spiel.segmentation.features

Handles featurization of strings for segmenters
"""
import re
from spiel import levenshtein
from spiel.levenshtein import INSERT_SYMBOL
from spiel.util import pad

class FeaturizationException(Exception):
    """Raises for an error in segmentation"""

class Featurizer:
    """
    Used to convert basic instances into training instances for a classifier
    """
    def __init__(self, mode='normal', inside_label='I', pad_token='_',
                 tokenize=None):
        """
        Initializes the featurizer

        :param mode: The labelling mode to use; one of 'basic', 'normal', or
                     'full'
        :type mode: str
        :param inside_label: The label to use for a character that does not
                             begin a label; default 'I'
        :type inside_label: str
        :param pad_token: The token to pad strings with; default '_'
        :type pade_token: str
        :param tokenize: A function to tokenize incoming strings. Defaults to
                         list()
        :type tokenize: callable
        """
        self.inside_label = inside_label
        self.pad_token = pad_token
        self.tokenize = tokenize or list
        self.mode = mode

    def convert_pairs(self, shape, labels):
        """
        Converts a shape and corresponding labels into training instances

        :param shape: The shape to convert
        :type shape: list or str
        :param labels: The labels for each token in the shape
        :type labels: list
        :return: A list of training instances, where the first element is a
                 dict of features, and the second is a trigram label for the
                 instance
        :rtype: list of (dict, str)
        """
        if isinstance(shape, str):
            shape = self.tokenize(shape)

        if not len(shape) == len(labels):
            raise FeaturizationException(f"{len(shape)} tokens in *shape*,\
but {len(labels)} labels provided")

        features = self.convert_features(shape)

        padded_labels = pad(labels, self.pad_token, 4)
        instances = ['-'.join(padded_labels[i-1:i+2])
                     for i in range(3, len(padded_labels) - 3)]

        return list(zip(features, instances))

    def convert_features(self, shape):
        """
        Converts a shape into training instances

        :param shape: The shape to convert
        :type shape: list or str
        :return: A list of training instances, where the first element is a
                 dict of features, and the second is a trigram label for the
                 instance
        :rtype: list of dict
        """
        if not shape:
            return []

        if isinstance(shape, str):
            shape = self.tokenize(shape)

        padded_shape = pad(shape, self.pad_token, 4)
        instances = []

        for i in range(3, len(padded_shape) - 3):
            features = {
                'prefix': ''.join(padded_shape[i-3:i]),
                'focus': padded_shape[i],
                'suffix': ''.join(padded_shape[i+1:i+4])
            }

            instances.append(features)

        return instances

    def analogize(self, shape, annotations):
        """
        Converts the segments of a set of annotations to match a given shape

        Null segments will be removed during the process.

        :param shape: A word to base the new segments off of
        :type shape: str or list of str
        :param annotations: Segment/label pairs
        :type annotations: list of (str, str)
        :return: The annotations with the segments replaced with those from
                 the provided shape
        :rtype: list of (str, str)
        """
        # Get rid of any null slots
        annotations = [annotation for annotation in annotations
                       if not annotation[0] == '∅']
        labels = [label for _, label in annotations]

        # Tokenize the shape if necessary
        if isinstance(shape, str):
            shape = self.tokenize(shape)

        # Get a map of where the segmentations should occur
        seg_map = self.label(shape, annotations)

        # Split up the shape
        segments = []
        last = 0
        for i, label in enumerate(seg_map):
            if i > 0 and not label == 'I':
                segments.append(''.join(shape[last:i]))
                last = i
        segments.append(''.join(shape[last:]))

        return list(zip(segments, labels))

    def label(self, shape, annotations):
        """
        Generates a list of annotations corresponding to each element in
        *shape*

        :param shape: The sequence to annotate
        :type shape: list or str
        :param annotations: A list of tuples, where the first is the shape of
                            the annotations, and the second is its label
        :type annotations: list of (str, str)
        :return: A list of labels
        :rtype: list
        """
        if isinstance(shape, str):
            shape = self.tokenize(shape)
        annotation_string = self.tokenize(concat_annotations(annotations))
        labels = label_annotations(annotations, self.inside_label,
                                   self.tokenize)

        ops = levenshtein.operations(annotation_string, shape)
        labels = levenshtein.annotate(annotation_string, shape, ops, labels)

        for i, label in enumerate(labels):
            # Perform any insertion directives
            matches = re.findall(rf'\+{INSERT_SYMBOL}(.*?)', label)
            for _ in matches:
                labels.insert(i+1, self.inside_label)

        return [self.__format_label(label) for label in labels]

    def __format_label(self, label):
        if self.mode == 'basic' or self.mode == 'normal':
            # Get rid of any directives
            label = re.match(r'^[^+]*', label)[0]

        if self.mode == 'basic':
            label = self.__simplify_label(label)

        return label

    def __simplify_label(self, label):
        if label == self.pad_token:
            return 'O'
        if label == self.inside_label:
            return 'I'
        return 'B'

def concat_annotations(annotations):
    """
    Glues the shapes of a list of annotations together

    :param annotations: A list of tuples, where the first is the shape of the
                        annotations, and the second is its label
    :type annotations: list of (str, str)
    :return: The combined shape of the annotations
    :rtype: str
    """
    return ''.join([shape for shape, _ in annotations])

def label_annotations(annotations, inside_label, tokenize=None):
    """
    Creates a list of labels for each character in a list of annotations

    :param annotations: A list of tuples, where the first is the shape of the
                        annotations, and the second is its label
    :type annotations: list of (str, str)
    :param inside_label: The label to use for characters that do not begin an
                         annotation
    :param tokenize: The function that should be used to tokenize the
                     annotation shapes. Defaults to calling list()
    :type tokenize: callable
    :return: A list of labels for the combined annotations
    :rtype: list of str
    """
    labels = []
    tokenize = tokenize or list

    for shape, label in annotations:
        for i, _ in enumerate(tokenize(shape)):
            if i == 0:
                labels.append(label)
            else:
                labels.append(inside_label)

    return labels


import pandas
from recordlinkage.preprocessing import clean
d = {'col1': ['marry - a', 'kudo::'], 'col2': ['nam vinh', 'okee-']}

df = pandas.DataFrame(data=d)
s = pandas.Series(df['col1'])
df['col1'] = clean(s)
print(df)
df['col1'][0] = "b"
print(df)


import sys

def suffix_array(seq):
    dict = {}
    i = 0
    while len(seq) > 0:
        dict[seq] = i
        i += 1
        seq = seq[1:]
    seqs = list(dict.keys())
    indexes = list(dict.values())
    zipped = sorted(zip(seqs, indexes))
    tuples = zip(*zipped)
    list1, list2 = [list(tuple) for tuple in tuples]
    return list1,list2

if __name__ == '__main__':
    file = sys.argv[1]
    with open(file) as i:
        lines = i.read()
        lines = lines.split('\n')
    string = lines[0] + "$"
    suffixes = suffix_array(string)
    genome_list = []
    for i in range(len(string)):
        first = string[0:i]
        last = string[i:]
        new_string = last + first
        genome_list.append(new_string)

    genome_list = sorted(genome_list)
    string = ""
    for genome in genome_list:
        string += genome[-1]

    lastCol = string
    # print(lastCol)

    patterns = lines[1].split(' ')
    firstCol = ''.join(sorted(lastCol))
    subscripts = [''] * (len(lastCol))
    a_count, g_count, c_count, t_count = 0, 0, 0, 0
    a_count_2, g_count_2, c_count_2, t_count_2 = 0, 0, 0, 0
    final_dict = {}
    firstOccur = {}
    a_first, g_first, c_first, t_first = False, False, False, False
    count = [[0,0,0,0,0]]
    firstColEdit = list(firstCol)
    for i in range(len(firstCol)):
        temp = ''
        temp2 = ''
        char = firstCol[i]
        char2 = lastCol[i]
        if char == "A":
            temp += char + str(a_count)
            a_count += 1
            if not a_first:
                firstOccur["A"] = i
                a_first = True
        elif char == "G":
            temp += char + str(g_count)
            g_count += 1
            if not g_first:
                firstOccur["G"] = i
                g_first = True
        elif char == "C":
            temp += char + str(c_count)
            c_count += 1
            if not c_first:
                firstOccur["C"] = i
                c_first = True
        elif char == "T":
            temp += char + str(t_count)
            t_count += 1
            if not t_first:
                firstOccur["T"] = i
                t_first = True
        else:
            temp = "$"
            firstOccur["$"] = i
        firstColEdit[i] = temp
        tempList = count[i].copy()
        if char2 == "A":
            temp2 += char2 + str(a_count_2)
            subscripts[i] = a_count_2
            tempList[0] += 1
            a_count_2 += 1
        elif char2 == "G":
            temp2 += char2 + str(g_count_2)
            subscripts[i] = g_count_2
            tempList[1] += 1
            g_count_2 += 1
        elif char2 == "C":
            temp2 += char2 + str(c_count_2)
            subscripts[i] = c_count_2
            tempList[2] += 1
            c_count_2 += 1
        elif char2 == "T":
            temp2 += char2 + str(t_count_2)
            subscripts[i] = t_count_2
            tempList[3] += 1
            t_count_2 += 1
        else:
            temp2 = "$"
            tempList[4] += 1
        count.append(tempList)
        final_dict[temp2] = temp
    all_ans = []
    letToNum = {}
    letToNum["A"] = 0
    letToNum["G"] = 1
    letToNum["C"] = 2
    letToNum["T"] = 3
    letToNum["$"] = 4
    for pattern in patterns:
        breaked = False
        patternCopy = pattern
        top = 0
        bottom = len(lastCol) - 1
        ans = 0
        while top <= bottom:
            if len(pattern) > 0:
                symbol = pattern[-1]
                pattern = pattern[:-1]
                for i in range(top, bottom + 1):
                    if lastCol[i] == symbol:
                        top = firstOccur[symbol] + count[top][letToNum[symbol]]
                        bottom = firstOccur[symbol] + count[bottom + 1][letToNum[symbol]] - 1
                        break
                else:
                    all_ans.append(str(0))
                    breaked = True
                    break
            else:
                all_ans.append(str(bottom - top + 1))
                break
        indexList = []
        if not breaked:
            for i in range(top, bottom + 1):
                indexList.append(suffixes[1][i])
            indexList = sorted(indexList)
            indexList2 = []
            for i in indexList:
                indexList2.append(str(i))
            temp = " ".join(indexList2)
            print(patternCopy + ":" + " " + temp)
        else:
            print(patternCopy + ":")


#coding=utf-8
'''
#import urllib
#import urllib.request
#import urllib.parse
import requests

URL_IP = 'http://httpbin.org/ip'
URL_GET = 'http://httpbin.org/get'

def use_simple_requests():
    response = requests.get(URL_IP)
    print('>>>Response Headers:')
    print(response.headers)
    print('>>>Response  Body:')
    print(response.text)

def use_params_requests():
    params = {'param1':'hello','param2':'你好','param3':'test'}
    response = requests.get(URL_GET,params=params)
    print('>>>Response Headers:')
    print(response.headers)
    print('>>>Status Code:')
    print(response.status_code)
    print('>>>Status Code reason:')
    print(response.reason)
    print('>>>Response  Body:')
    #print(response.json)
    print(response.text)

if __name__ == '__main__':
    print ('>>>Use simple urllib2:')
    use_params_requests()

'''
'''
内置高阶函数
一：
def f(x):
    return x*x
print map(f, [1, 2, 3, 4, 5, 6, 7, 8, 9])

二：

例如，编写一个f函数，接收x和y，返回x和y的和：

def f(x, y):
    return x + y

调用 reduce(f, [1, 3, 5, 7, 9])时，reduce函数将做如下计算：

先计算头两个元素：f(1, 3)，结果为4；
再把结果和第3个元素计算：f(4, 5)，结果为9；
再把结果和第4个元素计算：f(9, 7)，结果为16；
再把结果和第5个元素计算：f(16, 9)，结果为25；
由于没有更多的元素了，计算结束，返回结果25。

三：
filter()函数是 Python 内置的另一个有用的高阶函数，filter()函数接收一个函数 f 和一个list，这个函数 f 的作用是对每个元素进行判断，返回 True或 False，filter()根据判断结果自动过滤掉不符合条件的元素，返回由符合条件元素组成的新list。
例如，要从一个list [1, 4, 6, 7, 9, 12, 17]中删除偶数，保留奇数，首先，要编写一个判断奇数的函数：

def is_odd(x):
    return x % 2 == 1

然后，利用filter()过滤掉偶数：

filter(is_odd, [1, 4, 6, 7, 9, 12, 17])

结果：[1, 7, 9, 17]

'''

import json
import requests
from requests import exceptions
URL = 'https://api.github.com'

def build_url(endpoint):
    return '/'.join([URL,endpoint])

def better_print(json_str):
    return json.dumps(json.loads(json_str),indent=4)

def request_method():
    response = requests.get(build_url('user/emails'),auth=('imoocdemo','imoocdemo123'))
    #print(response.text)
    print(better_print(response.text))

def params_request():
    response = requests.get(build_url('users'),params={'since':11})
    print(better_print(response.text))
    print(response.request.headers)
    print(response.url)

def json_request():
    response = requests.patch(build_url('user'),auth=('imoocdemo','imoocdemo123'),json={'name':'baby'})
    print(better_print(response.text))
    print(response.request.headers)
    print(response.request.body)
    print(response.status_code)

def timeout_request():
    try:
        response = requests.get(build_url('user/emails'),timeout=10)
        response.raise_for_status()
    except exceptions.Timeout as e:
        print(e)
    except exceptions.HTTPError as e:
        print(e)
    else:
        print(response.text)
        print(response.status_code)

def hard_requests():
    from requests import Request,Session
    s = Session()
    headers = {'User-Agent':'hys1.3.1'}
    req = Request('GET',build_url('user/emails'),auth=('imoocdemo','imoocdemo123'),headers=headers)
    prepped = req.prepare()
    print(prepped.body)
    print(prepped.headers)

    resp = s.send(prepped,timeout=10)
    print(resp.status_code)
    print(resp.request.headers)
    print(resp.text)

def error_request():
    response = requests.get('http://api.github.com')
    print(response.history)

def download_image():
    URL = 'https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1508684596337&di=85cabd3af35d7b47e65de12719c7564a&imgtype=0&src=http%3A%2F%2Fpic.58pic.com%2F58pic%2F16%2F42%2F96%2F56e58PICAu9_1024.jpg'
    response = requests.get(URL,stream=True)
    print(response.status_code,response.reason)
    print(response.content)
    with open('demo.jpg','wb') as fd:
        for chunk in response.iter_content(128):
            fd.write(chunk)

def download_image_improved():
    URL = 'https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1508684596337&di=85cabd3af35d7b47e65de12719c7564a&imgtype=0&src=http%3A%2F%2Fpic.58pic.com%2F58pic%2F16%2F42%2F96%2F56e58PICAu9_1024.jpg'
    response = requests.get(URL,stream=True)
    from contextlib import closing
    with closing(requests.get(URL,stream=True)) as response:
        with open('demo1.jpg','wb') as fd:
            for chunk in response.iter_content(128):
                fd.write(chunk)

if __name__ == '__main__':
    download_image_improved()


from .backend import backend as bd
import numpy as np
import os
import time
import fdtd_1d as f
import matplotlib.pyplot as plt
from .constants import c0, BLUE, CYAN, TEAL, ORANGE, RED, MAGENTA, GREY
from werkzeug.utils import cached_property
from multiprocessing import Pool

color_spec = [BLUE, CYAN, TEAL, ORANGE, RED, MAGENTA, GREY]

# Implementation of a dielectric slab
def theory_dielectric_slab_complex(grid):
    d = ((grid.materials[0].position[-1] - grid.materials[0].position[0] + 1)) * grid.dx
    omega = grid.sources[0].omega
    n_real = np.sqrt((np.abs(grid.materials[0].epsilon_complex(omega)) + grid.materials[0].epsilon_real(omega)) / 2)
    kappa = np.sqrt((np.abs(grid.materials[0].epsilon_complex(omega)) - grid.materials[0].epsilon_real(omega)) / 2)
    n = n_real + 1j*kappa
    k0 = grid.sources[0].omega / c0
    k = n*k0
    q = ((n - 1) ** 2) / ((n + 1) ** 2) * np.exp(2j * k * d)
    e_inc = grid.sources[0].ampl
    e_tr = e_inc * (2 / (n + 1)) * (2 * n / (n + 1)) * (1 / (1 - q)) * np.exp(1j * (k - k0) * d)
    theo_amplitude = np.abs(e_tr)
    theo_phasenunterschied = np.angle(e_tr)
    return theo_amplitude, theo_phasenunterschied

def theory_dielectric_slab(grid):
    d = ((grid.materials[0].position[-1] - grid.materials[0].position[0] + 1)) * grid.dx
    n = np.sqrt(grid.eps[grid.materials[0].position[1]])
    k0 = grid.sources[0].omega / c0
    k = n * k0
    q = ((n - 1) ** 2) / ((n + 1) ** 2) * np.exp(2j * k * d)
    e_inc = grid.sources[0].ampl
    e_tr = e_inc * (2 / (n + 1)) * (2 * n / (n + 1)) * (1 / (1 - q)) * np.exp(1j * (k - k0) * d)
    theo_amplitude = np.abs(e_tr)
    theo_phasenunterschied = np.angle(e_tr)
    return theo_amplitude, theo_phasenunterschied

class benchmark:
    '''Parent class for all benchmarks. Declares path/directory to store data'''

    def __init__(self, name, benchmark_type):
        self.name = name
        self.benchmark_type = benchmark_type
        self.dir_path = None
        self.grids = []

    def allocate_directory(self):

        self.dir_path = os.path.join(os.path.dirname(__file__), 'saved_data/'+self.benchmark_type+'/'+self.name)
        os.mkdir(path=self.dir_path)

    def store_obs_data(self):
        self.allocate_directory()
        for grid in self.grids:
            grid.store_obs_data(benchmark=True, benchmark_name=self.name)

class Harmonic_Slab_Lorentz_Setup(benchmark):

    def __init__(self,  name, dx, length_grid_in_dx, length_media_in_dx, start_index_media, wavelength, ampl, conductivity, eps_inf, gamma, w0, chi_1, chi_2, chi_3, timesteps, courant=1):
        super().__init__(name=name, benchmark_type='harmonic_lorentz_slab')
        self.start_media = start_index_media
        self.dx = dx
        self.indices = []
        self.grids = []

        for grid in range(len(self.dx)):
            self.indices.append([start_index_media + 2 + i for i in np.arange(0, length_media_in_dx[grid] - 1)])
            self.grids.append('grid_'+str(grid))

        self.eps_inf = eps_inf
        self.Nx = length_grid_in_dx
        self.length_media = np.array(length_media_in_dx)
        self.lamb = wavelength
        self.timesteps = timesteps
        self.courant = courant
        self.N_lambda = np.zeros(len(self.dx))                                                             #[[] for _ in range(len(self.dx))]
        self.ampl = ampl
        self.conductivity = conductivity
        self.gamma = gamma
        self.w0 = w0

        self.chi_1 = chi_1
        self.chi_2 = chi_2
        self.chi_3 = chi_3

        self.eps_real = None
        self.eps_imag = None
        self.eps_complex = None
        self.n_real = None

        self.wo_phase_merged = np.zeros(len(self.dx))
        self.theo_phasenunterschied_merged = np.zeros(shape=(len(self.dx), np.max(self.length_media)-1))
        self.theo_amplitude_merged = np.zeros(shape=(len(self.dx), np.max(self.length_media)-1))
        self.exp_phase_merged = np.zeros(shape=(len(self.dx), np.max(self.length_media)-1))
        self.exp_amplitude_merged = np.zeros(shape=(len(self.dx), np.max(self.length_media)-1))

    def _grid_wo_slab(self):
        position_src = self.start_media - 1
        for grid in range(len(self.dx)):
            position_obs = self.Nx[grid] - 3
            end_mur = self.Nx[grid] - 1
            wo_grid = f.Grid(self.Nx[grid], dx=self.dx[grid], courant=self.courant)
            if wo_grid.courant == 1:
                wo_grid[position_src] = f.ActivatedSinus(name='SinsquaredActivated', wavelength=self.lamb,
                                                         carrier_wavelength=(self.lamb * 30), phase_shift=0,
                                                         amplitude=self.ampl, tfsf=True)
            else:
                wo_grid[position_src] = f.ActivatedSinus(name='SinsquaredActivated', wavelength=self.lamb,
                                                         carrier_wavelength=(self.lamb * 30), phase_shift=0,
                                                         amplitude=self.ampl, tfsf=False)
            wo_grid[position_obs] = f.QuasiHarmonicObserver(name='firstobserver', first_timestep=self.timesteps[grid]-200)

            if wo_grid.courant == 0.5:
                wo_grid[0] = f.LeftSideGridBoundary()
                wo_grid[end_mur] = f.RightSideGridBoundary()
            else:
                wo_grid[0] = f.LeftSideMur()
                wo_grid[end_mur] = f.RightSideMur()

            wo_grid.run_timesteps(self.timesteps[grid], vis=False)
            self.wo_phase_merged[grid] = wo_grid.local_observers[0].phase

    def _grids_w_slab(self):
        position_src = self.start_media - 1
        for grid in range(len(self.dx)):
            position_obs = self.Nx[grid] - 3
            end_mur = self.Nx[grid] - 1
            for ind_media, ind_array in zip(self.indices[grid], range(self.length_media[grid])):
                # Step 1: init grid
                w_grid = 'slab' + str(ind_media)
                w_grid = f.Grid(nx=self.Nx[grid], dx=self.dx[grid], courant=self.courant)

                # Step 2: init media
                w_grid[self.start_media:ind_media] = f.LorentzMedium(name='media', permeability=1, eps_inf=self.eps_inf, conductivity=self.conductivity, gamma=self.gamma, chi_1=self.chi_1, chi_2=self.chi_2, chi_3=self.chi_3, w0=self.w0)

                # Step 3: init source
                if w_grid.courant == 1:
                    w_grid[position_src] = f.ActivatedSinus(name='SinsquaredActivated', wavelength=self.lamb,
                                                             carrier_wavelength=(self.lamb * 30), phase_shift=0,
                                                             amplitude=self.ampl, tfsf=True)
                else:
                    w_grid[position_src] = f.ActivatedSinus(name='SinsquaredActivated', wavelength=self.lamb,
                                                             carrier_wavelength=(self.lamb * 30), phase_shift=0,
                                                             amplitude=self.ampl, tfsf=False)

                # Step 4: add observer
                w_grid[position_obs] = f.QuasiHarmonicObserver(name='firstobserver', first_timestep=self.timesteps[grid]-200)

                # Step 5: add boundaries
                if w_grid.courant == 0.5:
                    w_grid[0] = f.LeftSideGridBoundary()
                    w_grid[end_mur] = f.RightSideGridBoundary()
                else:
                    w_grid[0] = f.LeftSideMur()
                    w_grid[end_mur] = f.RightSideMur()

                # Step 6: run simulation
                w_grid.run_timesteps(timesteps=self.timesteps[grid], vis=False)

                # Step 7: misc
                self.exp_amplitude_merged[grid][ind_array] = w_grid.local_observers[0].amplitude
                self.exp_phase_merged[grid][ind_array] = w_grid.local_observers[0].phase
                self.theo_amplitude_merged[grid][ind_array] = theory_dielectric_slab_complex(w_grid)[0]
                self.theo_phasenunterschied_merged[grid][ind_array] = theory_dielectric_slab_complex(w_grid)[1]

                # if list self.eps_real is empty:
                if self.eps_real is None:
                    self.eps_real = w_grid.materials[0].epsilon_real(w_grid.sources[0].omega)
                    self.eps_imag = w_grid.materials[0].epsilon_imag(w_grid.sources[0].omega)
                    self.eps_complex = w_grid.materials[0].epsilon_complex(w_grid.sources[0].omega)
                    self.n_real = np.sqrt((np.abs(self.eps_complex) + self.eps_real)/2)

    def _visualize(self):
        max_resolution_grid = int(np.argmax(self.N_lambda))

        fig, axes = plt.subplots(2, 2)
        axes[0][0].grid(True, linestyle=(0, (1, 5)), color=GREY, linewidth=1)
        axes[0][1].grid(True, linestyle=(0, (1, 5)), color=GREY, linewidth=1)
        axes[1][0].grid(True, linestyle=(0, (1, 5)), color=GREY, linewidth=1)
        axes[1][1].grid(True, linestyle=(0, (1, 5)), color=GREY, linewidth=1)
        axes[0][0].set_xlabel('width in m', fontsize=14)
        axes[0][1].set_xlabel('width in m', fontsize=14)
        axes[1][0].set_xlabel('width in m', fontsize=14)
        axes[1][1].set_xlabel('width in m', fontsize=14)
        axes[0][0].set_ylabel('transmitted amplitude ' + r'$E_{z,tr}$', fontsize=14)
        axes[1][0].set_ylabel('Phasenunterschied', fontsize=14)
        axes[1][1].set_ylabel(r'$d(\phi_{exp},\phi_{theo})$', fontsize=14)
        axes[0][1].set_ylabel(r'$E_{tr,theo}$ / $E_{tr,FDTD}$', fontsize=14)

        axes[0][0].plot(np.array((np.array(self.indices[max_resolution_grid])) - self.start_media) * self.dx[max_resolution_grid],
                        self.theo_amplitude_merged[max_resolution_grid][0:self.length_media[max_resolution_grid] - 1], label='Theorie', color=ORANGE)
        axes[1][0].plot(np.array((np.array(self.indices[max_resolution_grid])) - self.start_media) * self.dx[max_resolution_grid],
                        self.theo_phasenunterschied_merged[max_resolution_grid][0:self.length_media[max_resolution_grid] - 1], color=ORANGE,
                        label='Theorie')

        for grid in range(len(self.dx)):
            axes[0][0].plot(np.array((np.array(self.indices[grid])) - self.start_media) * self.dx[grid], self.exp_amplitude_merged[grid][0:self.length_media[grid] - 1], color=color_spec[grid], linestyle='dashed', label=r'$N_{\lambda}=$' + '{0:.3}'.format(self.N_lambda[grid]))
            axes[0][1].plot(np.array((np.array(self.indices[grid])) - self.start_media) * self.dx[grid], self.theo_amplitude_merged[grid][0:self.length_media[grid] - 1]/self.exp_amplitude_merged[grid][0:self.length_media[grid] - 1], color=color_spec[grid], linestyle='dashed', label=r'$N_{\lambda}=$' + '{0:.3}'.format(self.N_lambda[grid]))
            axes[1][0].plot(np.array((np.array(self.indices[grid])) - self.start_media) * self.dx[grid], self.get_exp_phasedifference[grid][0:self.length_media[grid] - 1], color= color_spec[grid], linestyle='dashed', label=r'$N_{\lambda}=$' + '{0:.3}'.format(self.N_lambda[grid]))
            axes[1][1].plot(np.array((np.array(self.indices[grid])) - self.start_media) * self.dx[grid], np.abs(self.get_exp_phasedifference[grid][0:self.length_media[grid] - 1] - self.theo_phasenunterschied_merged[grid][0:self.length_media[grid] - 1]), color=color_spec[grid], label=r'$N_{\lambda}=$' + '{0:.3}'.format(self.N_lambda[grid]))

        axes[0][0].legend(loc='best')
        axes[0][1].legend(loc='best')
        axes[1][0].legend(loc='best')
        axes[1][1].legend(loc='best')

        plt.show()

    @cached_property
    def get_exp_phasedifference(self):
        phase_diff = np.zeros(shape=(len(self.dx), np.max(self.length_media)-1))
        for grid in range(len(self.dx)):
            phase_diff[grid][0:self.length_media[grid] - 1] = -np.array(self.exp_phase_merged[grid][0:self.length_media[grid] - 1]) + self.wo_phase_merged[grid]
            mask = (phase_diff[grid][:]) > np.pi
            phase_diff[grid][mask] -= 2*np.pi
        return phase_diff

    def _set_N_lambda(self):
        for grid in range(len(self.dx)):
            self.N_lambda[grid] = self.lamb/(self.dx[grid]*self.n_real)

    def store_obs_data(self):
        self.allocate_directory()
        file_grid_informations = os.path.join(self.dir_path, 'info.npy')
        file_width_in_dx = os.path.join(self.dir_path, 'width.npy')
        file_theory_ampl = os.path.join(self.dir_path, 'theory_ampl.npy')
        file_theory_phase = os.path.join(self.dir_path, 'theory_phase.npy')
        file_exp_ampl = os.path.join(self.dir_path, 'exp_ampl.npy')
        file_exp_phase = os.path.join(self.dir_path, 'exp_phase.npy')

        grid_informations = np.zeros(shape=(len(self.grids), 4))
        width_in_dx = np.zeros(shape=(len(self.grids), np.max(self.length_media)))
        for grid in range(len(self.grids)):
            width_in_dx[grid][0:self.length_media[grid] - 1] = np.array(self.indices[grid]) - self.start_media
            grid_informations[grid][0] = self.dx[grid]
            grid_informations[grid][1] = self.timesteps[grid]
            grid_informations[grid][2] = self.N_lambda[grid]
            grid_informations[grid][3] = self.length_media[grid]

        np.save(file_grid_informations, arr=grid_informations)
        np.save(file_width_in_dx, arr=width_in_dx)
        np.save(file_theory_ampl, arr=self.theo_amplitude_merged)
        np.save(file_theory_phase, arr=self.theo_phasenunterschied_merged)
        np.save(file_exp_ampl, arr=self.exp_amplitude_merged)
        np.save(file_exp_phase, arr=self.get_exp_phasedifference)

    def run_benchmark(self):
        start_time = time.time()
        self._grid_wo_slab()
        self._grids_w_slab()
        self._set_N_lambda()
        print("computed in --- %s seconds ---" % (time.time() - start_time))
        self._visualize()

class QPM_Length_multiple_chi_2(benchmark):
    def __init__(self, number_of_lambdas, timesteps, name, peak_timestep, pulse_duration, number_of_distributed_observer, chi2, multi=True):
        super().__init__(name=name, benchmark_type='qpm_harmonic_length_multiple')
        self.no_of_lambdas = number_of_lambdas
        self.half_qpm = 737
        self.chi_2 = chi2
        self.multi = multi
        self.dx = 4e-09
        self.dt = 0.5*self.dx/c0
        self.timesteps = timesteps
        self.nx = self.no_of_lambdas * (2*self.half_qpm + 1) + 10           # 10 is just a buffer to place boundarys and src
        self.start_media = 5
        self.ending_indices = np.array([self.start_media + i*self.half_qpm for i in range(self.no_of_lambdas*2 + 1)])
        self.peak_timestep = peak_timestep
        self.pulse_duration = pulse_duration
        self.no_observer = number_of_distributed_observer
        self.obs_distance = (self.nx - 10)//self.no_observer
        self.obs_positions = np.array([5 + i*self.obs_distance for i in range(self.no_observer)])

    def _allocate_memory(self):
        self.grid_information = np.array([self.dt, self.dx, self.no_of_lambdas, self.peak_timestep, self.pulse_duration])
        self.relative_observer_pos = self.obs_positions - self.start_media

    def store_obs_data(self):
        start_time = time.time()
        self.allocate_directory()
        self._allocate_memory()

        file_grid_info = os.path.join(self.dir_path, 'info.npy')
        file_relative_pos = os.path.join(self.dir_path, 'relative_ind.npy')
        file_data = os.path.join(self.dir_path, 'E_data.npy')
        file_chi_info = os.path.join(self.dir_path, 'chi2.npy')

        np.save(file_grid_info, arr=self.grid_information)
        np.save(file_relative_pos, arr=self.relative_observer_pos)
        np.save(file_data, arr=self.observed_data)
        np.save(file_chi_info, arr=self.chi_2)
        print("saved in --- %s seconds ---" % (time.time() - start_time))

    def _create_grids(self, chi2):
        print('process initiated')
        qpm_grid = f.Grid(nx=self.nx, dx=self.dx, benchmark='qpm_harmonic_length_multiple', courant=0.5)

        for indices in range(len(self.ending_indices) - 1):
            if indices % 2 == 0:
                    qpm_grid[self.ending_indices[indices]:self.ending_indices[indices + 1]] = f.LorentzMedium(
                        name='Varin', permeability=1, eps_inf=1.0, chi_1=[2.42, 9.65, 1.46], chi_2=[30.e-12, 0, 0],
                        chi_3=[0, 0, 0], conductivity=0, w0=[1.5494e16, 9.776e13, 7.9514e15], gamma=[0, 0, 0])

            else:
                    qpm_grid[self.ending_indices[indices]:self.ending_indices[indices + 1]] = f.LorentzMedium(
                        name='Varin', permeability=1, eps_inf=1.0, chi_1=[2.42, 9.65, 1.46], chi_2=[chi2, 0, 0],
                        chi_3=[0, 0, 0], conductivity=0, w0=[1.5494e16, 9.776e13, 7.9514e15], gamma=[0, 0, 0])

        qpm_grid[3] = f.GaussianImpulseWithFrequency(name='Varin', Intensity=5*10e12, wavelength=1.064e-06, pulse_duration=self.pulse_duration, peak_timestep=self.peak_timestep, tfsf=False)

        for pos in self.obs_positions:
            qpm_grid[int(pos)] = f.E_FFTObserver(name='Varin', first_timestep=0, second_timestep=self.timesteps - 1)

        qpm_grid[0] = f.LeftSideGridBoundary()
        qpm_grid[self.nx - 1] = f.RightSideGridBoundary()

        # step 6: run simulation
        qpm_grid.run_timesteps(timesteps=self.timesteps, vis=False)

        observed_grid_data = bd.zeros(shape=(self.no_observer, self.timesteps))
        for obs in range(self.no_observer):
            observed_grid_data[obs] = qpm_grid.local_observers[obs].observed_E

        return observed_grid_data

    def run_benchmark(self):
        start_time = time.time()
        processpool = Pool()
        self.observed_data = bd.zeros(shape=(len(self.chi_2), self.no_observer, self.timesteps))
        if self.multi:
            self.observed_data = bd.stack(processpool.map(self._create_grids, self.chi_2))
        else:
            for ind_chi, chi in zip(range(len(self.chi_2)), self.chi_2):
                self.observed_data[ind_chi] = self._create_grids(self.chi_2[ind_chi])
        print("computed in --- %s seconds ---" % (time.time() - start_time))

class QPM_Length(benchmark):

    def __init__(self, number_of_lambdas, timesteps, name, peak_timestep, pulse_duration, number_of_distributed_observer):
        super().__init__(name=name, benchmark_type='qpm_harmonic_length')
        self.no_of_lambdas = number_of_lambdas
        self.half_qpm = 737
        self.dx = 4e-09
        self.timesteps = timesteps
        self.nx = self.no_of_lambdas * (2*self.half_qpm + 1) + 10           # 10 is just a buffer to place boundarys and src
        self.start_media = 5
        self.ending_indices = np.array([self.start_media + i*self.half_qpm for i in range(self.no_of_lambdas*2 + 1)])
        self.peak_timestep = peak_timestep
        self.pulse_duration = pulse_duration
        self.no_observer = number_of_distributed_observer
        self.obs_distance = (self.nx - 10)//self.no_observer
        self.obs_positions = np.array([5 + i*self.obs_distance for i in range(self.no_observer)])

    def _create_grid(self):
        qpm_grid = f.Grid(nx=self.nx, dx=self.dx, benchmark='qpm_harmonic_length', courant=0.5)
        self.grids.append(qpm_grid)

        for indices in range(len(self.ending_indices) - 1):
            if indices % 2 == 0:
                    qpm_grid[self.ending_indices[indices]:self.ending_indices[indices + 1]] = f.LorentzMedium(
                        name='Varin', permeability=1, eps_inf=1.0, chi_1=[2.42, 9.65, 1.46], chi_2=[30.e-12, 0, 0],
                        chi_3=[0, 0, 0], conductivity=0, w0=[1.5494e16, 9.776e13, 7.9514e15], gamma=[0, 0, 0])

            else:
                    qpm_grid[self.ending_indices[indices]:self.ending_indices[indices + 1]] = f.LorentzMedium(
                        name='Varin', permeability=1, eps_inf=1.0, chi_1=[2.42, 9.65, 1.46], chi_2=[-30.e-12, 0, 0],
                        chi_3=[0, 0, 0], conductivity=0, w0=[1.5494e16, 9.776e13, 7.9514e15], gamma=[0, 0, 0])

        qpm_grid[3] = f.GaussianImpulseWithFrequency(name='Varin', Intensity=5*10e12, wavelength=1.064e-06, pulse_duration=self.pulse_duration, peak_timestep=self.peak_timestep, tfsf=False)

        for pos in self.obs_positions:
            qpm_grid[int(pos)] = f.E_FFTObserver(name='Varin', first_timestep=0, second_timestep=self.timesteps - 1)

        qpm_grid[0] = f.LeftSideGridBoundary()
        qpm_grid[self.nx - 1] = f.RightSideGridBoundary()

        #qpm_grid[0] = f.LeftSideMur()
        #qpm_grid[self.nx - 1] = f.RightSideMur()

        # step 6: run simulation
        qpm_grid.run_timesteps(timesteps=self.timesteps, vis=False)

    def _create_grid_mono(self):
        qpm_grid_mono = f.Grid(nx=self.nx, dx=self.dx, benchmark='qpm_harmonic_length', courant=0.5)
        self.grids.append(qpm_grid_mono)

        for indices in range(len(self.ending_indices) - 1):
            if indices % 2 == 0:
                    qpm_grid_mono[self.ending_indices[indices]:self.ending_indices[indices + 1]] = f.LorentzMedium(
                        name='Varin', permeability=1, eps_inf=1.0, chi_1=[2.42, 9.65, 1.46], chi_2=[30.e-12, 0, 0],
                        chi_3=[0, 0, 0], conductivity=0, w0=[1.5494e16, 9.776e13, 7.9514e15], gamma=[0, 0, 0])

            else:
                    qpm_grid_mono[self.ending_indices[indices]:self.ending_indices[indices + 1]] = f.LorentzMedium(
                        name='Varin', permeability=1, eps_inf=1.0, chi_1=[2.42, 9.65, 1.46], chi_2=[30.e-12, 0, 0],
                        chi_3=[0, 0, 0], conductivity=0, w0=[1.5494e16, 9.776e13, 7.9514e15], gamma=[0, 0, 0])

        qpm_grid_mono[3] = f.GaussianImpulseWithFrequency(name='Varin', Intensity=5*10e12, wavelength=1.064e-06, pulse_duration=self.pulse_duration, peak_timestep=self.peak_timestep, tfsf=False)

        for pos in self.obs_positions:
            qpm_grid_mono[int(pos)] = f.E_FFTObserver(name='Varin', first_timestep=0, second_timestep=self.timesteps - 1)

        qpm_grid_mono[0] = f.LeftSideGridBoundary()
        qpm_grid_mono[self.nx - 1] = f.RightSideGridBoundary()

        #qpm_grid[0] = f.LeftSideMur()
        #qpm_grid[self.nx - 1] = f.RightSideMur()

        # step 6: run simulation
        qpm_grid_mono.run_timesteps(timesteps=self.timesteps, vis=False)

    def _allocate_memory(self):
        self.observed_data = np.zeros(shape=(self.no_observer, self.timesteps))
        self.observed_data_mono = np.zeros(shape=(self.no_observer, self.timesteps))
        observer_object_list = np.array(self.grids[0].local_observers)
        observer_object_list_mono = np.array(self.grids[1].local_observers)
        for obs_ind in range(self.no_observer):
            self.observed_data[obs_ind][:] = observer_object_list[obs_ind].observed_E[:]
            self.observed_data_mono[obs_ind][:] = observer_object_list_mono[obs_ind].observed_E[:]
        self.grid_information = np.array([self.grids[0].dt, self.grids[0].dx, self.no_of_lambdas, self.peak_timestep, self.pulse_duration])
        self.relative_observer_pos = self.obs_positions - self.start_media

    def store_obs_data(self):
        start_time = time.time()
        self.allocate_directory()
        self._allocate_memory()

        file_grid_info = os.path.join(self.dir_path, 'info.npy')
        file_relative_pos = os.path.join(self.dir_path, 'relative_ind.npy')
        file_data = os.path.join(self.dir_path, 'E_data.npy')
        file_data_mono = os.path.join(self.dir_path, 'E_data_mono.npy')

        np.save(file_grid_info, arr=self.grid_information)
        np.save(file_relative_pos, arr=self.relative_observer_pos)
        np.save(file_data, arr=self.observed_data)
        np.save(file_data_mono, arr=self.observed_data_mono)
        print("saved in --- %s seconds ---" % (time.time() - start_time))

    def run_benchmark(self):
        start_time = time.time()
        self._create_grid()
        self._create_grid_mono()
        print("computed in --- %s seconds ---" % (time.time() - start_time))

class QPM_end_P(benchmark):
    ''' reproduces paper QuasiPhaseMatching from Varin's Paper '''

    def __init__(self, number_of_lambdas, timesteps, name, peak_timestep, pulse_duration):
        super().__init__(name=name, benchmark_type='qpm_harmonic')
        self.no_of_lambdas = number_of_lambdas
        # Varin parameters
        self.half_qpm = 737
        self.dx = 4e-09
        self.timesteps = timesteps
        self.nx = number_of_lambdas * (2*self.half_qpm + 1) + 10
        self.start_media = 5
        self.ending_indices = np.array([self.start_media + i*self.half_qpm for i in range(self.no_of_lambdas*2 + 1)])
        self.peak_timestep = peak_timestep
        self.pulse_duration = pulse_duration

    def _create_grid(self):
        self.position_P_obs = self.nx - 9
        self.position_E_obs = self.nx - 8

        # step 1: init grid
        qpm_grid = f.Grid(nx=self.nx, dx=self.dx, benchmark='qpm_harmonic', courant=0.5)
        self.grids.append(qpm_grid)

        for indices in range(len(self.ending_indices) - 1):
            if indices % 2 == 0:
                qpm_grid[self.ending_indices[indices]:self.ending_indices[indices + 1]] = f.LorentzMedium(
                    name='Varin', permeability=1, eps_inf=1.0, chi_1=[2.42, 9.65, 1.46], chi_2=[30.e-12, 0, 0],
                    chi_3=[0, 0, 0], conductivity=0, w0=[1.5494e16, 9.776e13, 7.9514e15], gamma=[0, 0, 0])

            else:
                qpm_grid[self.ending_indices[indices]:self.ending_indices[indices + 1]] = f.LorentzMedium(
                    name='Varin', permeability=1, eps_inf=1.0, chi_1=[2.42, 9.65, 1.46], chi_2=[-30.e-12, 0, 0],
                    chi_3=[0, 0, 0], conductivity=0, w0=[1.5494e16, 9.776e13, 7.9514e15], gamma=[0, 0, 0])

        qpm_grid[3] = f.GaussianImpulseWithFrequency(name='Varin', Intensity=10e12, wavelength=1.064e-06,
                                                     pulse_duration=self.pulse_duration,
                                                     peak_timestep=self.peak_timestep, tfsf=True)

        qpm_grid[self.position_E_obs] = f.E_FFTObserver(name='Varin', first_timestep=0, second_timestep=self.timesteps - 1)
        qpm_grid[self.position_P_obs] = f.P_FFTObserver(name='Varin', first_timestep=0, second_timestep=self.timesteps - 1)

        qpm_grid[0] = f.LeftSideGridBoundary()
        qpm_grid[self.nx - 1] = f.RightSideGridBoundary()

        #qpm_grid[0] = f.LeftSideMur()
        #qpm_grid[self.nx - 1] = f.RightSideMur()

        # step 6: run simulation
        qpm_grid.run_timesteps(timesteps=self.timesteps, vis=True)

    def _allocate_memory(self):
        self.observed_data = np.zeros(shape=(2, self.timesteps))
        self.observed_data[0][:] = self.grids[0].local_observers[0].observed_E[:]
        self.observed_data[1][:] = self.grids[0].local_observers[1].observed_P[:]
        self.grid_information = np.array([self.grids[0].dt, self.grids[0].dx, self.no_of_lambdas, self.peak_timestep,
                                          self.pulse_duration])

    def store_obs_data(self):
        start_time = time.time()
        self.allocate_directory()
        self._allocate_memory()

        self.relative_observer_pos = np.array([self.position_E_obs - self.start_media, self.position_P_obs - self.start_media])

        file_grid_info = os.path.join(self.dir_path, 'info.npy')
        file_relative_pos = os.path.join(self.dir_path, 'relative_ind.npy')
        file_data = os.path.join(self.dir_path, 'E_P_data.npy')

        np.save(file_grid_info, arr=self.grid_information)
        np.save(file_relative_pos, arr=self.relative_observer_pos)
        np.save(file_data, arr=self.observed_data)
        print("saved in --- %s seconds ---" % (time.time() - start_time))

    def run_benchmark(self):
        start_time = time.time()
        self._create_grid()
        print("computed in --- %s seconds ---" % (time.time() - start_time))

class Soliton(benchmark):

    ''' tries to show the majestic combined effect of GVD and SPM '''

    def __init__(self, name, central_wavelength, pulse_duration, intensities, x_to_snapshot, peak_timestep, frame_width_in_dx, dx, multi=True):
        super().__init__(name=name, benchmark_type='soliton')
        self.central_wavelength = central_wavelength
        self.pulse_duration = pulse_duration
        self.intensities = np.array(intensities)
        self.x_to_snapshot = x_to_snapshot
        self.peak_timestep = peak_timestep
        self.name = name
        self.dx = dx
        self.nx = int(x_to_snapshot[-1]/self.dx) + 5020
        self.frame_width = frame_width_in_dx
        self.multi = multi

    def _allocate_memory(self):
        self.grid_information = np.array([self.peak_timestep, self.pulse_duration, self.dx, self.frame_width])
        self.used_propagations = np.array(self.x_to_snapshot)
        self.used_intensities = np.array(self.intensities)

    def _create_grids(self, intensity):
        soliton_grid = f.Grid(self.nx, dx=self.dx, courant=0.5) #12mm 550000

        # Step 1: init media
        soliton_grid[5:self.nx-10] = f.CentroRamanMedium(name='Varin', chi_1=[0.69617, 0.40794, 0.89748], w0=[2.7537e16, 1.6205e16, 1.9034e14], chi_3=[1.94e-22, 0, 0], alpha=[0.7, 0, 0], wr=[8.7722e13, 0, 0], gamma_K=[0, 0, 0], gamma_R=[3.1250e13, 0, 0], permeability=1, conductivity=0, eps_inf=1)

        # Step 2: init src
        soliton_grid[3] = f.SechEnveloped(name='Varin', wavelength=1.5e-06, pulse_duration=self.pulse_duration, Intensity=intensity, peak_timestep=self.peak_timestep, tfsf=False)

        # Step 3: init frame
        soliton_grid[5:(6+self.frame_width)] = f.MovingFrame(x_to_snapshot=self.x_to_snapshot, central_wavelength=self.central_wavelength)

        # Step 4: init boundaries
        soliton_grid[0] = f.LeftSideGridBoundary()
        soliton_grid[self.nx - 1] = f.RightSideGridBoundary()

        soliton_grid.local_observers[0]._allocate_memory()
        timesteps = soliton_grid.local_observers[0].timesteps_to_store[-1] + 5000

        # Step 5: start benchmark
        soliton_grid.run_timesteps(timesteps, vis=False)

        # due to multiprocessing it is more convenient to store data by process (nobody loves waiting)
        observed_process_data = soliton_grid.local_observers[0].stored_data
        return observed_process_data

    def store_obs_data(self):
        start_time = time.time()
        self._allocate_memory()
        self.allocate_directory()

        file_grid_info = os.path.join(self.dir_path, 'info.npy')
        file_observed_data = os.path.join(self.dir_path, 'Int_Pos_E.npy')
        file_used_propagations = os.path.join(self.dir_path, 'propagations.npy')
        file_used_intensities = os.path.join(self.dir_path, 'intensities.npy')

        np.save(file_grid_info, arr=self.grid_information)
        np.save(file_used_intensities, arr=self.used_intensities)
        np.save(file_observed_data, arr=self.observed_data)
        np.save(file_used_propagations, arr=self.used_propagations)
        print("stored in --- %s seconds ---" % (time.time() - start_time))

    def run_benchmark(self):
        start_time = time.time()
        if self.multi:
            processpool = Pool()
            self.observed_data = np.array(processpool.map(self._create_grids, self.intensities))
        else:
            self.observed_data = np.zeros(shape=(np.size(self.intensities), len(self.x_to_snapshot), self.frame_width + 1))
            for intensity_index, intensity in zip(range(np.size(self.intensities)), self.intensities):
                self.observed_data[intensity_index] = self._create_grids(intensity)
        print("computed in --- %s seconds ---" % (time.time() - start_time))

class TiO2_Si02_Dielectric_Mirror_Setup:

    def __init__(self, N_lambda_media, wavelength_guided_for, wavelength, ampl, timesteps, number_of_layer_pairs, vary_layers=False, vary_inc_wavelength=False):
        self.N_lambda = N_lambda_media
        self.layer_number = [0 + i for i in range(number_of_layer_pairs)]
        self.number_of_layer = number_of_layer_pairs
        self.ampl = ampl
        self.timesteps = timesteps
        self.lamb = wavelength
        self.lamb_guided = wavelength_guided_for
        self.vary_layers = vary_layers
        self.vary_lambda = vary_inc_wavelength
        self.ti_n = 2.519                                                               # refractiveindex.info
        self.si_n = 1.453                                                               # refractiveindex.info
        self.dx = self.lamb/(self.ti_n * self.N_lambda)
        self.d_ti = int(self.lamb_guided / (self.ti_n * 4 * self.dx))                   # huge problem
        self.d_si = int(self.lamb_guided / (self.si_n * 4 * self.dx))                   # huge problem
        self.Nx = self.number_of_layer*(self.d_si + self.d_ti) + 14
        self.starting_locations_ti = [8 + i*(self.d_ti + self.d_si) for i in self.layer_number]
        self.starting_locations_si = np.array(self.starting_locations_ti) + self.d_ti
        self.position_src = 6
        self.position_obs = 3
        self.incident_wavelengths = [self.lamb + (i / 101) * self.lamb_guided for i in np.arange(0, 101, 1)]
        self.refl_ampl = []
        self.theory_R = []

    def _construct_non_vary_grid(self):
        end_mur = self.Nx - 1
        grid_non_vary = f.Grid(dx=self.dx, nx=self.Nx)
        grid_non_vary[0] = f.LeftSideMur()
        grid_non_vary[end_mur] = f.RightSideMur()
        grid_non_vary[self.position_src] = f.ActivatedSinus(name='Laser', wavelength=self.lamb, carrier_wavelength=6000e-09, tfsf=True, amplitude=self.ampl, phase_shift=0)
        grid_non_vary[self.position_obs] = f.QuasiHarmonicObserver(name='Observer', first_timestep=self.timesteps - 200)

        for layer_pair in self.layer_number:
            ti_start = self.starting_locations_ti[layer_pair]
            si_start = self.starting_locations_si[layer_pair]
            grid_non_vary[ti_start:si_start] = f.NonDispersiveMedia('TiO2', permittivity=self.ti_n**2, permeability=1, conductivity=0)
            grid_non_vary[si_start:(si_start+self.d_si)] = f.NonDispersiveMedia('SiO2', permittivity=self.si_n**2, permeability=1, conductivity=0)

        grid_non_vary.run_timesteps(self.timesteps)
        self.refl_ampl.append(grid_non_vary.local_observers[0].amplitude)

    def _construct_vary_grid(self):
        for layer in self.layer_number:
            Nx = (layer + 1) * (self.d_si + self.d_ti) + 12
            end_mur = Nx - 1
            grid_vary = f.Grid(dx=self.dx, nx=Nx)
            grid_vary[end_mur] = f.RightSideMur()
            grid_vary[0] = f.LeftSideMur()
            grid_vary[self.position_obs] = f.QuasiHarmonicObserver(name='Observer', first_timestep=self.timesteps - 300)
            grid_vary[self.position_src] = f.ActivatedSinus(name='Laser', wavelength=self.lamb, carrier_wavelength=6000e-09, tfsf=True, amplitude=self.ampl, phase_shift=0)

            for layers in range(0, layer + 1):
                ti_start = self.starting_locations_ti[layers]
                si_start = self.starting_locations_si[layers]
                grid_vary[ti_start:si_start] = f.NonDispersiveMedia('TiO2', permittivity=self.ti_n**2, permeability=1, conductivity=0)
                grid_vary[si_start:(si_start + self.d_si)] = f.NonDispersiveMedia('SiO2', permittivity=self.si_n**2, permeability=1, conductivity=0)

            grid_vary.run_timesteps(self.timesteps, vis=False)
            self.refl_ampl.append(grid_vary.local_observers[0].amplitude)
            self.theory_R.append(((self.si_n**(2*(layer+1))-self.ti_n**(2*(layer+1)))/(self.si_n**(2*(layer+1))+self.ti_n**(2*(layer+1))))**2)

    def _construct_vary_wavelength_grid(self):
        for wavelength in self.incident_wavelengths:
            end_mur = self.Nx - 1
            grid_vary_lamb = f.Grid(dx=self.dx, nx=self.Nx)
            grid_vary_lamb[0] = f.LeftSideMur()
            grid_vary_lamb[end_mur] = f.RightSideMur()
            grid_vary_lamb[self.position_src] = f.ActivatedSinus('Laser', wavelength=wavelength, carrier_wavelength=6000e-09, tfsf=True, amplitude=self.ampl, phase_shift=0)
            grid_vary_lamb[self.position_obs] = f.QuasiHarmonicObserver(name='Observer', first_timestep=self.timesteps - 200)

            for layer_pair in self.layer_number:
                ti_start = self.starting_locations_ti[layer_pair]
                si_start = self.starting_locations_si[layer_pair]
                grid_vary_lamb[ti_start:si_start] = f.NonDispersiveMedia('TiO2', permittivity=self.ti_n**2, permeability=1, conductivity=0)
                grid_vary_lamb[si_start:(si_start + self.d_si)] = f.NonDispersiveMedia('SiO2', permittivity=self.si_n**2, permeability=1, conductivity=0)

            grid_vary_lamb.run_timesteps(self.timesteps, vis=False)
            self.refl_ampl.append(grid_vary_lamb.local_observers[0].amplitude)

    def _visualize_vary_grid(self):
        fig, axes = plt.subplots(1, 1)
        axes.plot(np.array(self.layer_number)+1, np.array(self.refl_ampl)**2, linestyle='dashed', color='blue', marker='o', alpha=0.5, label='FDTD')
        axes.plot(np.array(self.layer_number)+1, np.array(self.theory_R), linestyle='dashed', color='red', marker='o', alpha=0.5, label='model')
        axes.legend(loc='best')
        axes.grid(True, linestyle=(0, (1, 5)), color='black', linewidth=1)
        axes.set_xlabel('Anzahl der Paare', fontsize=14)
        axes.set_ylabel(r'Reflektionsgrad $\mathcal{R}$', fontsize=14)
        plt.show()

    def _visualize_vary_lamb(self):
        fig, axes = plt.subplots(1, 1)
        axes.plot(np.array(self.incident_wavelengths)*10**9, np.array(self.refl_ampl)**2, linestyle='dashed', color='blue', marker='o',
                  alpha=0.5, label='FDTD')
        axes.grid(True, linestyle=(0, (1, 5)), color='black', linewidth=1)
        axes.set_xlabel(r'$\lambda$ in nm', fontsize=14)
        axes.set_ylabel(r'Reflektionsgrad $\mathcal{R}$', fontsize=14)
        plt.show()

    def run_benchmark(self):
        if self.vary_layers:
            self._construct_vary_grid()
            self._visualize_vary_grid()

        elif self.vary_lambda:
            self._construct_vary_wavelength_grid()
            self._visualize_vary_lamb()

        else:
            self._construct_non_vary_grid()

class Harmonic_Slab_Setup:

    def __init__(self, dx,  length_grid_in_dx, length_media_in_dx, start_index_media, wavelength, epsilon, ampl, timesteps):
        self.indices = [start_index_media + 2 + i for i in np.arange(0, length_media_in_dx - 1)]
        self.start_media = start_index_media
        self.dx = dx
        self.eps = epsilon
        self.Nx = length_grid_in_dx
        self.length_media = length_media_in_dx
        self.lamb = wavelength
        self.ampl = ampl
        self.wo_phase = []
        self.theo_phasenunterschied = []
        self.theo_amplitude = []
        self.exp_phase = []
        self.exp_amplitude = []
        self.timesteps = timesteps

    def _grid_wo_slab(self):
        position_src = self.start_media - 1
        position_obs = self.Nx - 3
        end_mur = self.Nx - 1
        wo_grid = f.Grid(self.Nx, dx=self.dx)
        wo_grid[position_src] = f.ActivatedSinus(name='SinsquaredActivated', wavelength=self.lamb, carrier_wavelength=10000.e-09, phase_shift=0, amplitude=self.ampl, tfsf=True)
        wo_grid[position_obs] = f.QuasiHarmonicObserver(name='firstobserver', first_timestep=self.timesteps-200)
        wo_grid[0] = f.LeftSideMur()
        wo_grid[end_mur] = f.RightSideMur()
        wo_grid.run_timesteps(self.timesteps, vis=False)
        self.wo_phase.append(wo_grid.local_observers[0].phase)

    def _grids_w_slab(self):
        position_src = self.start_media - 1
        position_obs = self.Nx - 3
        end_mur = self.Nx - 1
        for ind in self.indices:
            # Step 1: init grid
            w_grid = 'slab' + str(ind)
            w_grid = f.Grid(nx=self.Nx, dx=self.dx)

            # Step 2: init media
            w_grid[self.start_media:ind] = f.NonDispersiveMedia(name='media', permeability=1, permittivity=self.eps, conductivity=0)

            # Step 3: init source
            w_grid[position_src] = f.ActivatedSinus(name='SinsquaredActivated', wavelength=self.lamb, carrier_wavelength=10000.e-09, phase_shift=0, amplitude=self.ampl, tfsf=True)

            # Step 4: add observer
            w_grid[position_obs] = f.QuasiHarmonicObserver(name='firstobserver', first_timestep=self.timesteps-200)

            # Step 5: add boundaries
            w_grid[0] = f.LeftSideMur()
            w_grid[end_mur] = f.RightSideMur()

            # Step 6: run simulation
            w_grid.run_timesteps(timesteps=self.timesteps, vis=False)

            # Step 7: misc
            self.exp_amplitude.append(w_grid.local_observers[0].amplitude)
            self.exp_phase.append(w_grid.local_observers[0].phase)
            self.theo_amplitude.append(theory_dielectric_slab_complex(w_grid)[0])
            self.theo_phasenunterschied.append(theory_dielectric_slab_complex(w_grid)[1])

    def _visualize(self):
        fig, axes = plt.subplots(2, 2)
        fig.suptitle(r'$\epsilon_r=$ {epsilon}'.format(epsilon=self.eps) + r'   $N_{\lambda_{media}}=$' + '{0:.3}'.format(self.lamb/(self.dx*np.sqrt(self.eps))), fontsize=20)
        axes[0][0].plot(np.array(self.indices) - self.start_media, np.array(self.theo_amplitude), label='theorie', color='blue', marker='o', alpha=0.5)
        axes[0][0].grid(True, linestyle=(0, (1, 5)), color='black', linewidth=1)
        axes[0][0].plot(np.array(self.indices) - self.start_media, np.array(self.exp_amplitude), label='FDTD', linestyle='dashed', color='red', marker='s', alpha=0.5)
        axes[0][0].legend(loc='best')
        axes[0][0].set_xlabel('Breite des Mediums in ' + r'$\Delta_x$', fontsize=14)
        axes[0][0].set_ylabel('Transmittierte Amplitude ' + r'$Ez_{tr}$', fontsize=14)
        axes[0][0].set_xlim([0, self.length_media + 1])
        axes[0][1].plot(np.array(self.indices) - self.start_media, np.array(self.theo_amplitude) / np.array(self.exp_amplitude), color='black')
        axes[0][1].set_ylabel(r'$E_{tr,theo}$ / $E_{tr,FDTD}$', fontsize=14)
        axes[0][1].set_xlabel('Breite des Mediums in ' + r'$\Delta_x$', fontsize=14)
        axes[0][1].grid(True, linestyle=(0, (1, 5)), color='black', linewidth=1)
        axes[0][1].set_xlim([0, self.length_media + 1])
        axes[1][0].set_ylabel('Phasenunterschied', fontsize=14)
        axes[1][0].plot(np.array(self.indices) - self.start_media, self.theo_phasenunterschied, label='theorie', color='blue', alpha=0.5)
        axes[1][0].set_xlabel('Breite des Mediums in ' + r'$\Delta_x$', fontsize=14)
        axes[1][0].grid(True, linestyle=(0, (1, 5)), color='black', linewidth=1)
        axes[1][0].plot(np.array(self.indices) - self.start_media, -np.array(self.exp_phase) + self.wo_phase, color='red', linestyle='dashed',
                        label='FDTD', alpha=0.5)
        axes[1][0].set_xlim([0, self.length_media + 1])
        axes[1][0].legend()
        axes[1][1].set_xlabel('Breite des Mediums in ' + r'$\Delta_x$', fontsize=14)
        axes[1][1].set_ylabel(r'$d(\phi_{exp},\phi_{theo})$', fontsize=14)
        axes[1][1].plot(np.array(self.indices) - self.start_media,
                        np.abs(-np.array(self.exp_phase) + self.wo_phase - np.array(self.theo_phasenunterschied)), color='black')
        axes[1][1].grid(True, linestyle=(0, (1, 5)), color='black', linewidth=1)
        axes[1][1].set_xlim([0, self.length_media + 1])
        plt.show()

    def run_benchmark(self):
        self._grid_wo_slab()
        self._grids_w_slab()
        self._visualize()


import json
import os
import time
from models import Downloader, User

SETTINGS_FILE = "settings.json"

# Create history file if it does not exist
if not os.path.exists("history.txt"):
    with open("history.txt", "w") as f:
        f.write("")

# Check if the JSON file exists
if os.path.exists(SETTINGS_FILE):
    # If the file exists, open it and load its contents
    with open(SETTINGS_FILE, "r") as f:
        data = json.load(f)
else:
    # If the file doesn't exist, create it with default contents
    data = {"domain": None}
    with open(SETTINGS_FILE, "w") as f:
        json.dump(data, f)

if data["domain"] is None:
    print("Uzaktan eğitim websitesi adresini giriniz: (https:// olmadan)")
else:
    print(f"Uzaktan eğitim websitesi adresini giriniz: ({data['domain']} için enter)")

domain = input()
while domain == "" and data["domain"] is None:
    print("Lütfen geçerli bir adres giriniz.")
    domain = input()

if data["domain"] is None and domain != "":
    data["domain"] = domain
    with open(SETTINGS_FILE, "w") as f:
        json.dump(data, f)
elif data["domain"] is not None and domain == "":
    domain = data["domain"]

print("Çerezleri giriniz:")
cookies = input()
while cookies == "":
    print("Çerezler boş bırakılamaz.")
    cookies = input()

print("Ders listesi alınıyor.")
user = User(domain, cookies)
courses = user.get_enrolled_courses()
downloader = Downloader()
selected_activities = []
choose_again = True
previous_downloads = set(line.strip() for line in open("history.txt", encoding="utf-8"))
while len(courses) > 0 and choose_again:
    i = 1
    for course in courses:
        print(str(i) + ". " + course.name + " (" + str(course.activity_count) + ")")
        i += 1
    print("Ders seçiniz: ")
    course_selection = int(input())
    if 0 < course_selection <= len(courses):
        course = courses[course_selection - 1]
        print(course.name + " içeriği yükleniyor.")
        course.activities = []
        course.fetch_activities()
        i = 1
        for activity in course.activities:
            try:
                if activity.id in previous_downloads:
                    print(
                        "%d) Hafta: %d Dosya Adı: %s (Daha önce indirilmiş.)"
                        % (i, activity.weeks[0], activity.slug_name)
                    )
                else:
                    print(
                        "%d) Hafta: %d Dosya Adı: %s"
                        % (i, activity.weeks[0], activity.slug_name)
                    )
            except:
                continue
            i += 1
        print("İçerik aralığı seçiniz (Örn: 1-17): ")
        activity_selection = input().split("-")
        while len(activity_selection) != 2:
            print("Lütfen geçerli bir aralık giriniz.")
            activity_selection = input().split("-")
        if len(activity_selection) == 2:
            start_activity = int(activity_selection[0])
            end_activity = int(activity_selection[1])
            if (
                end_activity - start_activity >= 0
                and start_activity > 0
                and 0 < end_activity <= len(course.activities)
            ):
                for i in range(start_activity - 1, end_activity):
                    activity = course.activities[i]
                    activity.prepare_video(downloader)
                    selected_activities.append(activity)
                print("Başka bir dersten içerik seçmek ister misiniz? (e/h)")
                choose_again = input().lower() == "e"

if selected_activities:
    start = time.time()
    downloader.start_downloads()
    end = time.time()
else:
    print(selected_activities)
    print("İçerik seçilmedi.")


import pygame

class Heart(pygame.sprite.Sprite):
    def __init__(self, imgfile, hp, pos=[]):
        super().__init__()

        self.heart_img = pygame.image.load(imgfile)
        self.rect = self.heart_img.get_rect()
        self.pos = pos
        self.HP = hp
        self.speed = 7
        self.Invincible = 0 #受到攻击后的无敌时间

    # 裂开的特效（待完善） ；return 是否活着
    def is_dead(self):
        if self.HP <= 0:
            return True
        else:
            return False

    # 居中画图
    def target_pos(self, pos):
            pos = (pos[0]-self.rect[-2]//2,pos[1]-self.rect[-1]//2)
            return pos


import keras
from keras.models import Sequential
from keras.layers import Dense
import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.model_selection import StratifiedKFold

#设置随机数种子
np.random.seed(7)

#数据路径
data_file="pima-indians-diabetes.csv"

#导入数据
dataset=np.loadtxt(data_file,encoding='utf-8',delimiter=',')
#分割X和y
X=dataset[:,0:8]
y=dataset[:,8]

#10折,训练集和验证集总数共为10
kfold=StratifiedKFold(n_splits=10,shuffle=True,random_state=7)

cvscores=[]

for train,validation in kfold.split(X,y):
    # 创建模型
    model = Sequential()
    model.add(Dense(12, input_dim=8, activation='relu'))
    model.add(Dense(6, activation='relu'))

    # 最后一层dense，sigmoid输出患病概率
    model.add(Dense(1, activation='sigmoid'))

    # 编译模型
    model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])

    # 训练模型
    model.fit(X, y, epochs=150, batch_size=10, verbose=0)

    # 评估模型
    scores = model.evaluate(X[validation], y[validation],verbose=0)

    #输出评估结果
    print('\n%s : %.2f%%' % (model.metrics_names[1], scores[1] * 100))

    cvscores.append(scores[1]*100)

#输出均值和标准差
print("%.2f%% (+/- %.2f%%)" % (np.mean(cvscores),np.std(cvscores)))


import sys
input = sys.stdin.readline

def dfs(x,y):
  #상 하 좌 우 우상 우하 좌상 좌하
  dy = [1,-1,0,0,1,-1,1,-1]
  dx = [0,0,-1,1,1,1,-1,-1]
  graph[x][y] = 0
  for i in range(8):
    nx = x + dx[i]
    ny = y + dy[i]
    if(0 <= nx < h and 0 <= ny < w and graph[nx][ny] == 1):
      dfs(nx,ny)

while True:
  w, h = map(int, input().split())
  if w == 0 and h == 0:
    break
  graph = [list(map(int, input().split())) for _ in range(h)]
  cnt = 0
  for i in range(h):
    for j in range(w):
      if graph[i][j] == 1:
        dfs(i,j)
        cnt+=1
  print(cnt)


d = dict()
for _ in range(int(input())):
    mot = ''.join(sorted(input()))
    if mot not in d:
        d[mot] = 1
    else:
        d[mot] += 1

d = sorted(d.items(), key = lambda x : x[1], reverse = True)
print(d[0][1])