from copy import deepcopy
from typing import Optional, List
from types import MethodType
from collections import defaultdict
from nltk.tokenize import sent_tokenize
from nltk.corpus import stopwords
import random
import spacy
import torch

from parlai.core.agents import create_agent, create_agent_from_shared
from parlai.core.params import ParlaiParser
from parlai.core.opt import Opt
from parlai.core.message import Message
from parlai.core.agents import Agent
from parlai.core.metrics import F1Metric
from parlai.agents.rag.args import setup_rag_args
from parlai.agents.bart.bart import BartAgent
from parlai.core.metrics import normalize_answer
from parlai.tasks.wizard_of_wikipedia.agents import (
    TOKEN_NOCHOSEN,
)

from parlai.tasks.wizard_of_wikipedia.agents import (
    TOKEN_KNOWLEDGE,
    TOKEN_END_KNOWLEDGE,
)

STOP_WORDS = stopwords.words('english')

def load_opt_from_file(opt_file):
    if not opt_file.endswith('.opt'):
        opt_file += '.opt'
    return Opt.load(opt_file)

def wow_get_batch_context(self, batch, orig_fun=None):
    """
    Set the beam context for n-gram context blocking specific for WoW data.

    For WoW, we don't want to consider the knowledge in the input for the context beam
    blocking. That's why we mask it out here.
    """
    ctxts = orig_fun(batch)
    knowledge_start_id = self.dict.txt2vec(TOKEN_KNOWLEDGE)
    knowledge_end_id = self.dict.txt2vec(TOKEN_END_KNOWLEDGE)

    def mask_ctxttensor_between_sublists(
        ctxts: torch.Tensor, sub1: List[int], sub2: List[int]
    ) -> torch.Tensor:
        """
        Generate a mask that masks out the context between sub1 and sub2.
        """
        mask = []
        for ctxt in ctxts:
            mask_idxs = []
            should_copy = False
            idx_pointer = 0
            id_to_match = sub1
            for j, token in enumerate(ctxt.cpu().numpy()):
                if token == id_to_match[idx_pointer]:
                    idx_pointer += 1
                    if idx_pointer == 1 and id_to_match == sub1:
                        mask_idxs.append([j])
                    elif idx_pointer >= len(id_to_match):
                        should_copy = id_to_match == sub1
                        idx_pointer = 0
                        id_to_match = sub2 if (id_to_match == sub1) else sub1
                        mask_idxs[-1].append(j)
                    else:
                        mask_idxs[-1].append(j)
                elif should_copy:
                    mask_idxs[-1].append(j)
                elif idx_pointer > 0:
                    idx_pointer = 0
                    del mask_idxs[-1]
            mask.append(
                [
                    0 if idx in [i for sl in mask_idxs for i in sl] else 1
                    for idx in range(len(ctxt))
                ]
            )
        return torch.LongTensor(mask).to(ctxts.device)

    ctxts *= mask_ctxttensor_between_sublists(
        ctxts, knowledge_start_id, knowledge_end_id
    )
    return ctxts

def find_supporting_sentence(question: str, answer: str, docs: List[str]) -> str:
    """
    Finds the supporting sentence for the answer in the docs.
    """
    # Remove the title of the documents.
    for i, doc in enumerate(docs):
        if ' | ' in doc:
            docs[i] = '. '.join(doc.split(' | ')[1:])
    concat_docs = '. '.join(docs)
    sentences = sent_tokenize(concat_docs)

    # Sort sentences according to recall with the answer and question.
    sorted_sentences = sorted(
        sentences,
        key=lambda sentence: (
            F1Metric._prec_recall_f1_score(
                normalize_answer(answer).split(), normalize_answer(sentence).split()
            )[0],
            F1Metric._prec_recall_f1_score(
                normalize_answer(question).split(), normalize_answer(sentence).split()
            )[0],
        ),
        reverse=True,
    )

    return sorted_sentences[0]

def extract_entities(
    sentence, pos=('PROPN', 'NOUN'), use_named_entities=True, use_noun_chunks=True
):
    global nlp
    if nlp is None:
        nlp = spacy.load("en_core_web_sm")
    doc = nlp(sentence)
    results = []
    if pos:
        for token in doc:
            if token.pos_ in pos:
                results.append(token)
    if use_named_entities:
        for ent in doc.ents:
            results.append(ent)
    if use_noun_chunks:
        for chunk in doc.noun_chunks:
            if chunk.text.lower() not in STOP_WORDS:
                results.append(chunk)
    results = list(set([r.text for r in results]))
    return results

def extract_knowledge(txt: str) -> List[str]:
    if not txt or not txt.split():
        return []
    entities = extract_entities(txt)
    return [e.lower() for e in (entities if entities else txt.split())]

def knowledge_from_dialogue_response(dialogue_response: str) -> str:
    """
    Get a knowledge response based on the dialogue response.

    We use a random entity from the dialogue response as knowledge. If there is no
    entity present, we use a random word. If there are no words present, we use
    TOKEN_NOCHOSEN.
    """
    knowledge_options = extract_knowledge(dialogue_response)
    if not knowledge_options:
        return TOKEN_NOCHOSEN
    return random.choice(knowledge_options)

class StackedKnowledgeDialogueAgent(Agent):
    """
    Stacked model that generates first the knowledge, and then the dialogue response.
    """

    @classmethod
    def add_cmdline_args(
        cls, parser: ParlaiParser, partial_opt: Optional[Opt] = None
    ) -> ParlaiParser:
        agent = parser.add_argument_group('StackedKnowledgeDialogueAgent options')

        additional_agent_parser = ParlaiParser(add_parlai_args=False)
        BartAgent.add_cmdline_args(additional_agent_parser)
        setup_rag_args(additional_agent_parser)

        for action in additional_agent_parser._actions:
            key = action.option_strings[-1]
            type = action.type

            for prefix in [
                'krm',  # knowledge response model
                'drm',  # dialogue response model
                'drmrw',  # dialogue response model rag-wiki
                'drmnk',  # dialogue response model no-knowledge
            ]:
                agent.add_argument(
                    f'--{prefix}-{key.strip("-")}',
                    type=type,
                    required=False,
                )

        agent.add_argument(
            '--knowledge-response-model-path',
            type=str,
            default=''
            'wow_knowledge_response_generation_sweep_1_Tue_Aug_17_1822/9aa/model',
            help='Model used to generate the knowledge response.',
        )
        agent.add_argument(
            '--dialogue-response-model-path',
            type=str,
            default='',
            help='Model used to generate the dialogue response.',
        )
        agent.add_argument(
            '--dialogue-response-no-knowledge-model-path',
            type=str,
            default='',
            help='Model used to generate the dialogue response without knowledge.',
        )
        agent.add_argument(
            '--dialogue-response-rag-wiki-model-path',
            type=str,
            default='',
            help='Model used to generate the dialogue response with Wiki knowledge.',
        )
        agent.add_argument(
            '--use-supporting-sentence-as-knowledge',
            type=bool,
            default=False,
            help='Instead of using the knowledge response directly to condition the dialogue'
            ' model, we search for the top supporting sentence and use this.',
        )
        agent.add_argument(
            '--beam-filter-for-knowledge-response',
            type=bool,
            default=False,
            help='Try to pick a beam that contains the knowledge response.',
        )
        agent.add_argument(
            '--beam-filter-questions',
            type=bool,
            default=False,
            help='Try to pick a beam that does not contain a question mark.',
        )
        agent.add_argument(
            '--beam-filter-self-references',
            type=bool,
            default=False,
            help='Try to pick a beam that does not contain self references like "I" and "me".',
        )
        agent.add_argument(
            '--beam-disregard-knowledge-for-context-blocking',
            type=bool,
            default=True,
            help='If True disregard the knowledge input for the context blocking.',
        )
        agent.add_argument(
            '--add-fixed-confidence',
            type=int,
            default=-1,
            help='Add a fixed confidence score of the knowledge response.',
        )
        agent.add_argument(
            '--add-confidence-as-str',
            type=bool,
            default=False,
            help='If we add a confidence score to the KRM, we add it as a str.',
        )
        return parser

    def __init__(self, opt, shared=None):
        self.id = 'StackedKnowledgeDialogueAgent'
        self._construct_opts(opt)

        self.knowledge_agent = None
        self.dialogue_agent = None
        self.dialogue_agent_no_knowledge = None
        self.dialogue_agent_rag_wiki = None

        if not shared:
            self._init_knowledge_model()
            self._init_dialogue_models()
        else:
            if 'knowledge_agent_share' in shared:
                self.knowledge_agent = create_agent_from_shared(
                    shared['knowledge_agent_share']
                )
            if 'dialogue_agent_share' in shared:
                self.dialogue_agent = create_agent_from_shared(
                    shared['dialogue_agent_share']
                )
            if 'dialogue_agent_no_knowledge_share' in shared:
                self.dialogue_agent = create_agent_from_shared(
                    shared['dialogue_agent_no_knowledge_share']
                )
            if 'dialogue_agent_rag_wiki_share' in shared:
                self.dialogue_agent = create_agent_from_shared(
                    shared['dialogue_agent_rag_wiki_share']
                )

        self.shared = shared
        super().__init__(opt, shared)

    @property
    def has_no_knowledge_dialogue_model(self):
        return (
            self.opts['init']['dialogue_response_no_knowledge_model_path']
            and self.opts['init']['dialogue_response_no_knowledge_model_path'] != 'None'
        )

    @property
    def has_rag_wiki_dialogue_model(self):
        return (
            self.opts['init']['dialogue_response_rag_wiki_model_path']
            and self.opts['init']['dialogue_response_rag_wiki_model_path'] != 'None'
        )

    def _agent_opt(
        self, filename, specific_override_args, general_override_args, **kwargs
    ):
        opt = load_opt_from_file(filename)
        opt['override'] = {}
        blocklist_general = ['model', 'model_file', 'init_model']
        standard_override_args = {
            'skip_generation': False,
            'inference': 'beam',
            'beam_block_ngram': 3,
            'beam_context_block_ngram': -1,
            'beam_size': 3,
        }
        general_override_args = {
            **general_override_args,
            **standard_override_args,
            **kwargs,
        }

        # Remove the prefix for the model for the specific override args.
        specific_override_args = {
            '_'.join(k.split('_')[1:]): v for k, v in specific_override_args.items()
        }

        # Specific for --indexer-type.
        if 'indexer_type' in specific_override_args:
            # TODO: do we also need to overwrite path_to_index?
            pass

        override_args = {**general_override_args, **specific_override_args}

        for k, v in override_args.items():
            if k not in blocklist_general and k in opt:
                opt['override'][k] = v

        return opt

    def _construct_opts(self, opt):
        self.opts = {}
        self.opts['init'] = opt
        override_opts = defaultdict(dict)
        for k, v in opt['override'].items():
            if k.startswith('krm_'):
                if v is not None:
                    override_opts['knowledge_agent'][k] = v
            elif k.startswith('drm_'):
                if v is not None:
                    override_opts['dialogue_agent'][k] = v
            elif k.startswith('drmnk_'):
                if v is not None:
                    override_opts['dialogue_agent_no_knowledge'][k] = v
            elif k.startswith('drmrw_'):
                if v is not None:
                    override_opts['dialogue_agent_rag_wiki'][k] = v
            else:
                override_opts['general'][k] = v
        self.opts['override'] = override_opts

        if opt['knowledge_response_model_path'] and opt[
            'knowledge_response_model_path'
        ] not in ['oracle']:
            self.opts['knowledge_agent'] = self._agent_opt(
                filename=opt['knowledge_response_model_path'],
                specific_override_args=override_opts['knowledge_agent'],
                general_override_args=override_opts['general'],
            )
        self.opts['dialogue_agent'] = self._agent_opt(
            filename=opt['dialogue_response_model_path'],
            specific_override_args=override_opts['dialogue_agent'],
            general_override_args=override_opts['general'],
        )
        if self.has_no_knowledge_dialogue_model:
            self.opts['dialogue_agent_no_knowledge'] = self._agent_opt(
                filename=opt['dialogue_response_no_knowledge_model_path'],
                specific_override_args=override_opts['dialogue_agent_no_knowledge'],
                general_override_args=override_opts['general'],
            )
        if self.has_rag_wiki_dialogue_model:
            self.opts['dialogue_agent_rag_wiki'] = self._agent_opt(
                filename=opt['dialogue_response_rag_wiki_model_path'],
                specific_override_args=override_opts['dialogue_agent_rag_wiki'],
                general_override_args=override_opts['general'],
            )

    def share(self):
        shared = super().share()
        shared['knowledge_agent_share'] = self.knowledge_agent.share()
        shared['dialogue_agent_share'] = self.dialogue_agent.share()
        if self.has_no_knowledge_dialogue_model:
            shared[
                'dialogue_agent_no_knowledge_share'
            ] = self.dialogue_agent_no_knowledge.share()
        if self.has_rag_wiki_dialogue_model:
            shared[
                'dialogue_agent_rag_wiki_share'
            ] = self.dialogue_agent_rag_wiki.share()
        return shared

    def _init_knowledge_model(self):
        # Initialize knowledge agent.
        if 'knowledge_agent' in self.opts:
            self.knowledge_agent = create_agent(
                self.opts['knowledge_agent'], requireModelExists=True
            )
            print('Options for Knowledge Response Agent')
            self.knowledge_agent.opt.log()
        elif self.opts['init']['knowledge_response_model_path'] == 'oracle':
            self.knowledge_agent = OracleKnowledgeAgent(self.opts['init'])

    def _init_dialogue_models(self):
        ## Init dialogue models.

        # Initialize dialogue agent that uses the predicted knowledge.
        self.dialogue_agent = create_agent(
            self.opts['dialogue_agent'], requireModelExists=True
        )
        # Monkey patch the get_batch_context to ignore the knowledge for
        # beam-context-blocking.
        if self.opts['init']['beam_disregard_knowledge_for_context_blocking']:
            orig_fun = self.dialogue_agent._get_batch_context
            self.dialogue_agent._get_batch_context = MethodType(
                lambda self, batch: wow_get_batch_context(
                    self, batch, orig_fun=orig_fun
                ),
                self.dialogue_agent,
            )

        print('Options for Dialogue Response Agent')
        self.dialogue_agent.opt.log()

        # Initialize dialogue agent that doesn't use knowledge.
        if self.has_no_knowledge_dialogue_model:
            self.dialogue_agent_no_knowledge = create_agent(
                self.opts['dialogue_agent_no_knowledge'], requireModelExists=True
            )

        # Initialize dialogue agent that uses RAG with Wiki.
        if self.has_rag_wiki_dialogue_model:
            self.dialogue_agent_rag_wiki = create_agent(
                self.opts['dialogue_agent_rag_wiki'], requireModelExists=True
            )

    def dialogue_reply(self, agent, observation):
        return self.batch_dialogue_reply(agent, [observation])[0]

    def batch_dialogue_reply(self, agent, observations):
        dialogue_observations = []
        # Observation for the dialogue model.
        for obs in observations:
            dialogue_observation = agent.observe(obs)
            agent.self_observe(dialogue_observation)
            dialogue_observations.append(dialogue_observation)

        return agent.batch_act(dialogue_observations)

    def generate_knowledge_observation(self, knowledges: List[str], observations):
        # Adjust the observation texts.
        knowledge_infused_observations = deepcopy(observations)
        for obs, knowledge in zip(knowledge_infused_observations, knowledges):
            if 'text' not in obs:
                continue
            text = obs.pop('text')

            if self.opts['init']['add_fixed_confidence'] >= 0:
                confidence = self.opts['init']['add_fixed_confidence']
                if self.opts['init']['add_confidence_as_str']:
                    confidence = {
                        0: 'low',
                        5: 'medium',
                        10: 'high',
                    }[confidence] + ' confidence'
                text += f'\n{TOKEN_KNOWLEDGE} {confidence}: {knowledge} {TOKEN_END_KNOWLEDGE}'
            else:
                text += f'\n{TOKEN_KNOWLEDGE} {knowledge} {TOKEN_END_KNOWLEDGE}'
            obs['text'] = text
        return knowledge_infused_observations

    def batch_act(self, observations):
        knowledge_agent_observations = [o['knowledge_agent'] for o in observations]
        raw_observations = [o['raw'] for o in observations]

        # Get the knowledge replies.
        if self.knowledge_agent is None:
            self._init_knowledge_model()
        batch_reply_knowledge = self.knowledge_agent.batch_act(
            knowledge_agent_observations
        )

        if (
            'top_docs' in batch_reply_knowledge[0]
            and self.opts['init']['use_supporting_sentence_as_knowledge']
        ):
            # The knowledge agent is a rag-style model. Instead of the actual knowledge
            # response, we will use the best matching sentence from the retrieved docs
            # as the knowledge conditioning.
            for i, reply_knowledge in enumerate(batch_reply_knowledge):
                reply_knowledge['support_sentence'] = find_supporting_sentence(
                    question=raw_observations[i]['text'],
                    answer=reply_knowledge['text'],
                    docs=reply_knowledge['top_docs'],
                )

        if self.dialogue_agent is None:
            self._init_dialogue_models()

        if self.dialogue_agent_no_knowledge:
            batch_reply_dialogue_no_knowledge = self.batch_dialogue_reply(
                self.dialogue_agent_no_knowledge, raw_observations
            )
        if self.dialogue_agent_rag_wiki:
            batch_reply_dialogue_rag_wiki = self.batch_dialogue_reply(
                self.dialogue_agent_rag_wiki, raw_observations
            )

        knowledge_infused_observations = self.generate_knowledge_observation(
            knowledges=[
                reply_knowledge.get('text', '')
                for reply_knowledge in batch_reply_knowledge
            ],
            observations=raw_observations,
        )
        batch_reply_dialogue = self.batch_dialogue_reply(
            self.dialogue_agent, knowledge_infused_observations
        )

        batch_reply_dialogue_knowledge_sentence = None
        if (
            self.opts['init']['use_supporting_sentence_as_knowledge']
            and 'support_sentence' in batch_reply_knowledge[0]
        ):
            knowledge_sentence_infused_observations = (
                self.generate_knowledge_observation(
                    knowledges=[
                        reply_knowledge.get('support_sentence', '')
                        for reply_knowledge in batch_reply_knowledge
                    ],
                    observations=raw_observations,
                )
            )
            batch_reply_dialogue_knowledge_sentence = self.batch_dialogue_reply(
                self.dialogue_agent, knowledge_sentence_infused_observations
            )

        for i in range(len(batch_reply_dialogue)):
            if batch_reply_knowledge and len(batch_reply_knowledge) > i:
                batch_reply_dialogue[i]['knowledge_response'] = batch_reply_knowledge[
                    i
                ].get('text', '')
                if 'support_sentence' in batch_reply_knowledge[i]:
                    batch_reply_dialogue[i]['support_sentence'] = batch_reply_knowledge[
                        i
                    ].get('support_sentence', '')
            if (
                self.dialogue_agent_no_knowledge
                and batch_reply_dialogue_no_knowledge
                and len(batch_reply_dialogue_no_knowledge) > i
            ):
                batch_reply_dialogue[i][
                    'text_no_knowledge'
                ] = batch_reply_dialogue_no_knowledge[i].get('text', '')

            if (
                self.dialogue_agent_rag_wiki
                and batch_reply_dialogue_rag_wiki
                and len(batch_reply_dialogue_rag_wiki) > i
            ):
                batch_reply_dialogue[i][
                    'text_rag_wiki'
                ] = batch_reply_dialogue_rag_wiki[i].get('text', '')

            if (
                batch_reply_dialogue_knowledge_sentence
                and len(batch_reply_dialogue_knowledge_sentence) > i
            ):
                batch_reply_dialogue[i][
                    'text_knowledge_sentence'
                ] = batch_reply_dialogue_knowledge_sentence[i].get('text', '')

        [
            self._filter_beams(
                reply=reply,
                filter_for_knowledge=self.opts['init'][
                    'beam_filter_for_knowledge_response'
                ],
                filter_questions=self.opts['init']['beam_filter_questions'],
                filter_self_references=self.opts['init']['beam_filter_self_references'],
            )
            for reply in batch_reply_dialogue
        ]

        return batch_reply_dialogue

    def _filter_beams(
        self,
        reply,
        filter_for_knowledge: bool = True,
        filter_questions: bool = False,
        filter_self_references: bool = False,
    ):
        knowledge = normalize_answer(reply['knowledge_response'])
        self_references = [
            'I live',
            'I love',
            ' me ',
            'my favorite',
            'My favorite',
            'do you know',
            'I have',
            'I like ',
            'My ',
        ]
        question_words = [
            'who',
            'when',
            'where',
            'what',
            'do you',
            'are you',
        ]

        def filter_fn(text: str) -> bool:
            normalized_text = normalize_answer(text)
            if filter_for_knowledge and knowledge not in normalized_text:
                return False
            if filter_questions and (
                '?' in text or any([qw in normalized_text for qw in question_words])
            ):
                return False
            if filter_self_references and any([ref in text for ref in self_references]):
                return False
            return True

        if not (
            'text' in reply
            and 'beam_texts' in reply
            and 'knowledge_response' in reply
            and len(reply['beam_texts']) > 1
        ):
            return

        beam_texts = [
            text
            for text, _ in sorted(reply['beam_texts'], key=lambda x: x[1], reverse=True)
        ]
        # print('\t' + '\n\t'.join(beam_texts[:10]))
        for text in [reply['text']] + beam_texts:
            if filter_fn(text):
                reply.force_set('text', text)
                return

    def self_observe(self, self_message: Message) -> None:
        # Hack: Feed back the final dialogue response to the knowledge model.
        # This is why we need to make sure that --mutators flatten.
        self.knowledge_agent.self_observe(self_message)

    def observe(self, observation):
        # Delete unused keys.
        for key in ['label_candidates', 'knowledge']:
            if key in observation:
                del observation[key]

        label_key = 'eval_labels' if 'eval_labels' in observation else 'labels'
        if 'nqopen' in self.opts['init']['task'].lower():
            knowledge_target = observation.get(label_key, '')
            if isinstance(knowledge_target, tuple):
                knowledge_target = '\t'.join(knowledge_target)
            observation['knowledge_target'] = knowledge_target
            observation['dialogue_response'] = ''
        else:
            observation['dialogue_response'] = observation.get(label_key, '')
            observation['knowledge_response'] = observation.get('checked_sentence', '')

        if self.knowledge_agent is None:
            self._init_knowledge_model()

        observations = {
            'raw': deepcopy(observation),
            'knowledge_agent': self.knowledge_agent.observe(observation),
        }
        self.observations = observations
        return observations

    def act(self):
        """
        Call batch_act with the singleton batch.
        """
        response = self.batch_act([self.observations])[0]
        self.self_observe(response)
        return response

class OracleKnowledgeAgent(Agent):
    def __init__(self, opt, shared=None):
        super().__init__(opt)
        self.id = 'OracleKnowledgeAgent'

    def get_knowledge(self, obs):
        labels_kword = 'labels' if 'train' in self.opt['datatype'] else 'eval_labels'
        if 'wizardofwikipedia' in self.opt['task'].lower().replace('_', ''):
            return obs.get('checked_sentence', '')
        if 'wizardofinternet' in self.opt['task'].lower().replace('_', ''):
            knowledge = obs.get('__selected-sentences__', '')
            if isinstance(knowledge, list):
                knowledge = '\n'.join(knowledge)
            return knowledge
        elif (
            'nqopen' in self.opt['task'].lower()
            or 'natural_questions' in self.opt['task'].lower()
        ):
            return obs.get(labels_kword, '')
        elif 'LightTeacherPlus' in self.opt['task']:
            if labels_kword not in obs or not obs[labels_kword]:
                return ''
            labels = obs[labels_kword]
            if not isinstance(labels, str):
                labels = labels[0]
            return knowledge_from_dialogue_response(labels)
        elif 'SummaryQA' in self.opt['task']:
            return obs.get(labels_kword, '')
        else:
            raise NotImplementedError(f'Task "{self.opt["task"]}" is not known.')

    def self_observe(self, obs):
        pass

    def batch_act(self, observations):
        return [self.act(obs) for obs in observations]

    def act(self, obs=None):
        if not obs:
            obs = self.observation
        if obs is None:
            return {'text': 'Nothing to repeat yet.'}
        reply = {}
        reply['id'] = self.getID()
        knowledge = self.get_knowledge(obs)
        if not isinstance(knowledge, str):
            knowledge = random.choice(knowledge)
        reply['text'] = knowledge
        return Message(reply)


from vegetation_index import psri_index
from osgeo import gdal
import numpy as np
import zipfile
import os

# adjust with band data filename
target_band = ["B04_10m.jp2", "B08_10m.jp2"]
print(target_band)

zip_folder_path = "./data/2020"
files = os.listdir(zip_folder_path)

# make folder if not exits
output_folder = f"./index/psri/2020"
if not os.path.exists(output_folder):
    os.makedirs(output_folder)

# shp file
shp_file = './shp/poly_paddy_13.5768_101.717.shp'

for path in files:
    zip_file_path = f'{zip_folder_path}/{path}'
    file_inside_zip = []

    with zipfile.ZipFile(zip_file_path, 'r') as zip_file:
        for file_name in zip_file.namelist():
            for target_fl in target_band:
                if file_name.endswith(target_fl):
                    file_inside_zip.append(file_name)

    # config to accesing the zip
    gdal.VSICurlClearCache()
    gdal.PushErrorHandler('CPLQuietErrorHandler')
    gdal.SetConfigOption('CPL_VSIL_CURL_ALLOWED_EXTENSIONS', 'jp2')
    gdal.SetConfigOption('CPL_VSIL_CURL_ALLOWED_EXTENSIONS', 'jp2')

    # Open Using Gdal (adjust it with target_band)
    b8 = gdal.Open('/vsizip/' + zip_file_path + '/' + file_inside_zip[0])
    b4 = gdal.Open('/vsizip/' + zip_file_path + '/' + file_inside_zip[1])

    geoTransform = b4.GetGeoTransform()
    geoProjection = b4.GetProjection()

    nir = b8.GetRasterBand(1).ReadAsArray().astype(np.float32)
    red = b4.GetRasterBand(1).ReadAsArray().astype(np.float32)

    psri = psri_index(red, nir)

    # Save ram
    b4 = None
    b8 = None
    nir = None
    red = None

    # save psri
    driver = gdal.GetDriverByName('GTiff')
    output_path = f'{output_folder}/{path.split(".")[0]}.tif'
    cols, rows = psri.shape

    lon_start = geoTransform[0]
    lat_start = geoTransform[3]
    pixel_width = geoTransform[1]
    pixel_height = geoTransform[5]

    lon_values = np.arange(lon_start, lon_start +
                           cols * pixel_width, pixel_width)
    lat_values = np.arange(lat_start, lat_start + rows *
                           pixel_height, pixel_height)

    output_dataset = driver.Create(
        output_path, cols, rows, 1, gdal.GDT_Float32)
    output_dataset.GetRasterBand(1).WriteArray(psri)

    output_dataset.SetGeoTransform(geoTransform)
    output_dataset.SetProjection(geoProjection)

    metadata = {
        'Latitude': ' '.join(map(str, lat_values)),
        'Longitude': ' '.join(map(str, lon_values))
    }

    output_dataset.SetMetadata(metadata)

    print(f"Extract {path} : Completed")


# This is a sample Python script.

# Press Shift+F10 to execute it or replace it with your code.
# Press Double Shift to search everywhere for classes, files, tool windows, actions, and settings.

def print_hi(name):
    # Use a breakpoint in the code line below to debug your script.
    print(f'Hi, {name}')  # Press Ctrl+F8 to toggle the breakpoint.

# Press the green button in the gutter to run the script.
if __name__ == '__main__':
    print_hi('PyCharm')
Spisok = [0,1,2,3,4,5,6,7,8,9]
Odd = []
Even = []
def Split(Spisok):
    global Odd
    global Even
    for i in range(len(Spisok)):
        if i%2 == 1:
            Odd.append(Spisok[i])
        else:
            Even.append(Spisok[i])
    return None
Answer = []
Split(Spisok)
print(Odd, Even)
Odd.reverse()
k = 0
x = 0
for i in range(len(Spisok)):
    if i % 2 == 1:
        Answer.append(Odd[k])
        k+=1
    else:
        Answer.append(Even[x])
        x += 1
print(Answer)
# See PyCharm help at https://www.jetbrains.com/help/pycharm/


import gym
import numpy as np
import matplotlib.pyplot as plt

NUM_RUNS = 50

MAX_EPISODE_STEPS = 5000
NUM_EPISODES = 100
NUM_ACTIONS = 3

env = gym.make('MountainCar-v0').env
env._max_episode_steps = MAX_EPISODE_STEPS

POLYNOMIAL_FEATURES = True
POLYNOMIAL_DEGREE = 2

'''
https://github.com/openai/gym/wiki/MountainCar-v0
Summary for me:
    There are 3 different actions (push left, nothing, push right) which
    correspond to (0, 1, 2).
    An state is a pair (position,velocity).
    Position ranges from -1.2 to 0.6 and velocity from -0.07 to 0.07
'''

def convert_to_features(state, action, polynomial = POLYNOMIAL_FEATURES,
                        max_degree = POLYNOMIAL_DEGREE):

    if polynomial:
        '''
        Converts the state to a polynomial of degree max_degree.
        However, we cap the degree of action at 1, since we cannot expect
        higher powers of action to deliver meaningful extra features.
        (Action takes values in (0, 1, 2) )
        '''

        subfeatures = []
        for n in range(max_degree + 1):
            for k in range(n+1):
                subfeatures += [state[0]**(n-k) * state[1]**(k)]

        subfeatures = np.array(subfeatures)
        features = np.zeros(2*len(subfeatures))
        features[:len(subfeatures)] = subfeatures
        features[len(subfeatures):] = action * subfeatures

        return features

    else:
        ''' Directly returns the state and action as a feature '''

        return np.append(state, action)

state = env.reset()

NUM_FEATURES = len(convert_to_features(state, 0))

learning_rate = 1e-4
discount = 0.9

def action_value(state, action, weights):
    ''' Approximate the action value of an state'''
    features = convert_to_features(state, action)
    return features @ weights

def choose_action(state, weights, epsilon = 0.33):
    ''' A random action will be picked with probability epsilon '''
    if epsilon > np.random.uniform():
        action = np.random.randint(0, NUM_ACTIONS)
    else:
        action = np.argmax([action_value(state, action, weights)
                    for action in range(NUM_ACTIONS)])
    return action

def update_weights(previous_state, state, action, next_action,
                   weights, reward, done = False):
    gradient = convert_to_features(previous_state, action)
    if done:
        update = learning_rate * (reward
                        - action_value(previous_state, action, weights))

    else:
        update = learning_rate * (reward
                        + discount * action_value(state, next_action, weights)
                        - action_value(previous_state, action, weights))
    update *= gradient
    weights -= update # Gradient descent
    return weights

''' Calculate average reward per run and per episode '''
average_rewards = np.zeros((NUM_RUNS, NUM_EPISODES))

for j in range(NUM_RUNS):
    ''' Start of another run '''
    print('Run:', j)
    weights = np.random.rand(NUM_FEATURES)

    for k in range(NUM_EPISODES):
        ''' Start of another episode '''
        state = env.reset()
        action = choose_action(state, weights.copy())
        cumulative_reward = 0
        for step in range(MAX_EPISODE_STEPS):
            #env.render()
            previous_state = state
            state, reward, done, info = env.step(action)
            next_action = choose_action(state, weights.copy())
            weights = update_weights(previous_state, state, action,
                                       next_action, weights.copy(), reward, done)

            cumulative_reward += reward
            if done:
                break
            action = next_action
        average_reward = cumulative_reward
        average_rewards[j,k] = average_reward

average_rewards_per_run = average_rewards.sum(axis=0)/NUM_RUNS/MAX_EPISODE_STEPS

plt.plot(average_rewards_per_run)
plt.xlabel('Episode')
plt.ylabel('Average reward per episode and run')
if POLYNOMIAL_FEATURES:
    title = 'Using polynomial features\n'
else:
    title = 'Using states and actions directly as features\n'
title += 'Average of ' + str(NUM_RUNS) + ' runs with ' \
    + str(MAX_EPISODE_STEPS) + ' steps per episode'
plt.title(title)


#
# @lc app=leetcode.cn id=376 lang=python
#
# [376] 摆动序列
#

# @lc code=start
class Solution(object):
    def wiggleMaxLength(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        if len(nums) < 2:
            return len(nums)

        cur_diff = pre_diff = 0
        result = 1
        for i in range(len(nums)-1):
            cur_diff = nums[i] - nums[i+1]  # 记录当前差值
            if (cur_diff > 0 and pre_diff <= 0) or (cur_diff < 0 and pre_diff >= 0):
                result += 1
                pre_diff = cur_diff

        return result

if __name__ == '__main__':
    nums = [1,2,3,4,5,6,7,8,9]
    print(Solution().wiggleMaxLength(nums))

# @lc code=end


"""A Python Pulumi program"""

import pulumi
from pulumi_spacelift import Stack

stack = Stack("my-stack",
  administrative=False,
  autodeploy=False,
  branch="main",
  description="A simple stack",
  name="simple-stack-python-pulumi",
  repository="empty",
  project_root="",
  terraform_version="1.3.0"
)


import fnmatch, os, time
from suite_subprocess import suite_subprocess
from wtscenario import make_scenarios
import wttest

class test_txn05(wttest.WiredTigerTestCase, suite_subprocess):
    logmax = "100K"
    tablename = 'test_txn05'
    uri = 'table:' + tablename
    remove_list = ['true', 'false']
    sync_list = [
        '(method=dsync,enabled)',
        '(method=fsync,enabled)',
        '(method=none,enabled)',
        '(enabled=false)'
    ]

    types = [
        ('row', dict(tabletype='row',
                    create_params = 'key_format=i,value_format=i')),
        ('var', dict(tabletype='var',
                    create_params = 'key_format=r,value_format=i')),
        ('fix', dict(tabletype='fix',
                    create_params = 'key_format=r,value_format=8t')),
    ]
    op1s = [
        ('trunc-all', dict(op1=('all', 0))),
        ('trunc-both', dict(op1=('both', 2))),
        ('trunc-start', dict(op1=('start', 2))),
        ('trunc-stop', dict(op1=('stop', 2))),
    ]
    txn1s = [('t1c', dict(txn1='commit')), ('t1r', dict(txn1='rollback'))]

    scenarios = make_scenarios(types, op1s, txn1s)

    def conn_config(self):
        # Cycle through the different transaction_sync values in a
        # deterministic manner.
        txn_sync = self.sync_list[
            self.scenario_number % len(self.sync_list)]
        # Set remove=false on the home directory.
        return 'log=(enabled,file_max=%s,remove=false),' % self.logmax + \
            'transaction_sync="%s",' % txn_sync

    # Check that a cursor (optionally started in a new transaction), sees the
    # expected values.
    def check(self, session, txn_config, expected):
        if txn_config:
            session.begin_transaction(txn_config)
        c = session.open_cursor(self.uri, None)
        actual = dict((k, v) for k, v in c if v != 0)
        # Search for the expected items as well as iterating
        for k, v in expected.items():
            self.assertEqual(c[k], v)
        c.close()
        if txn_config:
            session.commit_transaction()
        self.assertEqual(actual, expected)

    # Check the state of the system with respect to the current cursor and
    # different isolation levels.
    def check_all(self, current, committed):
        # Transactions see their own changes.
        # Read-uncommitted transactions see all changes.
        # Snapshot and read-committed transactions should not see changes.
        self.check(self.session, None, current)
        self.check(self.session2, "isolation=snapshot", committed)
        self.check(self.session2, "isolation=read-committed", committed)
        self.check(self.session2, "isolation=read-uncommitted", current)

        # Opening a clone of the database home directory should run
        # recovery and see the committed results.  Flush the log because
        # the backup may not get all the log records if we are running
        # without a sync option.  Use sync=off to force a write to the OS.
        self.session.log_flush('sync=off')
        self.backup(self.backup_dir)
        backup_conn_params = 'log=(enabled,file_max=%s)' % self.logmax
        backup_conn = self.wiredtiger_open(self.backup_dir, backup_conn_params)
        try:
            self.check(backup_conn.open_session(), None, committed)
        finally:
            backup_conn.close()

    def check_log(self, committed):
        self.backup(self.backup_dir)
        #
        # Open and close the backup connection a few times to force
        # repeated recovery and log removal even if later recoveries
        # are essentially no-ops. Confirm that the backup contains
        # the committed operations after recovery.
        #
        # Cycle through the different remove values in a deterministic manner.
        self.remove = self.remove_list[
            self.scenario_number % len(self.remove_list)]
        backup_conn_params = \
            'log=(enabled,file_max=%s,remove=%s)' % (self.logmax, self.remove)
        orig_logs = fnmatch.filter(os.listdir(self.backup_dir), "*gerLog*")
        endcount = 2
        count = 0
        while count < endcount:
            backup_conn = self.wiredtiger_open(self.backup_dir,
                                               backup_conn_params)
            try:
                 session = backup_conn.open_session()
            finally:
                self.check(session, None, committed)
                # Sleep long enough so that the removal thread is guaranteed
                # to run before we close the connection.
                time.sleep(1.0)
                if count == 0:
                    first_logs = \
                        fnmatch.filter(os.listdir(self.backup_dir), "*gerLog*")
                backup_conn.close()
            count += 1
        #
        # Check logs after repeated openings. The first log should
        # have been removed if configured. Subsequent openings would not
        # be removed because no checkpoint is written due to no modifications.
        #
        cur_logs = fnmatch.filter(os.listdir(self.backup_dir), "*gerLog*")
        for o in orig_logs:
            # Creating the backup was effectively an unclean shutdown so
            # even after sleeping, we should never remove log files
            # because a checkpoint has not run.  Later opens and runs of
            # recovery will detect a clean shutdown and allow removal.
            self.assertEqual(True, o in first_logs)
            if self.remove == 'true':
                self.assertEqual(False, o in cur_logs)
            else:
                self.assertEqual(True, o in cur_logs)
        #
        # Run printlog and make sure it exits with zero status.
        #
        self.runWt(['-h', self.backup_dir, 'printlog'], outfilename='printlog.out')

    def test_ops(self):
        self.backup_dir = os.path.join(self.home, "WT_BACKUP")
        self.session2 = self.conn.open_session()
        # print "Creating %s with config '%s'" % (self.uri, self.create_params)
        self.session.create(self.uri, self.create_params)
        # Set up the table with entries for 1-5.
        # We then truncate starting or ending in various places.
        c = self.session.open_cursor(self.uri, None)
        current = {1:1, 2:1, 3:1, 4:1, 5:1}
        for k in current:
            c[k] = 1
        committed = current.copy()

        ops = (self.op1, )
        txns = (self.txn1, )
        for i, ot in enumerate(zip(ops, txns)):
            self.session.begin_transaction()
            ok, txn = ot
            # print '%d: %s(%d)[%s]' % (i, ok[0], ok[1], txn)
            op, k = ok

            # print '%d: %s(%d)[%s]' % (i, ok[0], ok[1], txn)
            if op == 'stop':
                c.set_key(k)
                self.session.truncate(None, None, c, None)
                kstart = 1
                kstop = k
            elif op == 'start':
                c.set_key(k)
                self.session.truncate(None, c, None, None)
                kstart = k
                kstop = len(current)
            elif op == 'both':
                c2 = self.session.open_cursor(self.uri, None)
                # For both, the key given is the start key.  Add 2
                # for the stop key.
                kstart = k
                kstop = k + 2
                c.set_key(kstart)
                c2.set_key(kstop)
                self.session.truncate(None, c, c2, None)
                c2.close()
            elif op == 'all':
                c2 = self.session.open_cursor(self.uri, None)
                kstart = 1
                kstop = len(current)
                c.set_key(kstart)
                c2.set_key(kstop)
                self.session.truncate(None, c, c2, None)
                c2.close()

            while (kstart <= kstop):
                del current[kstart]
                kstart += 1

            # print current
            # Check the state after each operation.
            self.check_all(current, committed)

            if txn == 'commit':
                committed = current.copy()
                self.session.commit_transaction()
            elif txn == 'rollback':
                current = committed.copy()
                self.session.rollback_transaction()

            # Check the state after each commit/rollback.
            self.check_all(current, committed)

        # Check the log state after the entire op completes
        # and run recovery.
        if self.scenario_number % (len(test_txn05.scenarios) // 100 + 1) == 0:
            self.check_log(committed)

if __name__ == '__main__':
    wttest.run()


import glob
import pandas as pd

export_path = 'dataset.csv'

def make_data(export_path):
    files = []
    path = '/home/moby/PycharmProjects/data/gpt/csv2/'
    files.extend(glob.glob(path + '*/*/*.csv'))
    path = '/home/moby/PycharmProjects/data/IDMT-SMT-32/'
    files.extend(glob.glob(path + '*/csv2/*.csv'))
    path = '/home/moby/PycharmProjects/data/IDMT-SMT-32/dataset1/'
    files.extend(glob.glob(path + '*/csv2/*.csv'))
    combined_csv = pd.concat([pd.read_csv(f, header=None, skiprows=1) for f in files])
    combined_csv.to_csv(export_path)

def get_data(path, label_row=5, data_rows=tuple(range(134))[6:134]):
    data = pd.read_csv(path, header=None, usecols=data_rows, skiprows=1).to_numpy()
    labels = pd.read_csv(path, header=None, usecols=[label_row], skiprows=1).to_numpy()
    return data, labels


from collections import Counter

class Solution:
    def permuteUnique(self, nums: List[int]) -> List[List[int]]:
        if len(nums) == 1:
            return [nums]

        counter = Counter(nums)
        result = []
        def backtracking(perm):
            if len(perm) == len(nums):
                result.append(perm[:])
                return
            for num in counter:
                if counter[num] <= 0:
                    continue
                counter[num] -= 1
                perm.append(num)
                backtracking(perm)
                counter[num] += 1
                perm.pop()

        backtracking([])
        return result


from djoser.views import UserViewSet
from rest_framework.pagination import LimitOffsetPagination
from rest_framework.permissions import IsAuthenticated

class DjoserUserViewSet(UserViewSet):
    pagination_class = LimitOffsetPagination
    permission_classes = [IsAuthenticated]

    def me(self, request, *args, **kwargs):
        response = super().me(request, *args, **kwargs)
        response.data['is_subscribed'] = False
        return response


# Extract Adobe Analytics data strings from Adobe Assurance (Project Griffon) logs
# 24 Oct 2022 11:18
# Michael Powe
# reminder of what we are looking for
# (json_data['events'][3]['payload']['ACPExtensionEventData']['hitUrl'])

import json
import urllib.parse
from argparse import ArgumentParser
from pprint import pprint

def main():
    parser = ArgumentParser()
    parser.add_argument("-f", "--file", help="file from which to read")
    parser.add_argument("-o", "--output", help="file to which to write results")
    parser.add_argument(
        "-v",
        "--verbose",
        action="store_true",
        help="print output to screen as well as to file",
    )
    args = parser.parse_args()

    # source data file (default if no command line input)
    griffon_data: str = (
        r"c:\Users\micha\Dropbox\src\python\json\data\AssuranceTraining.json"
    )

    # written output file (default if no command line input)
    url_data: str = r"c:\Users\micha\Dropbox\src\python\json\data\assurance-urls.txt"

    inputfile: str = args.file if args.file is not None else griffon_data
    outputfile: str = args.output if args.output is not None else url_data

    context_list: list[str]
    uri_list: list[str]
    uri_list, context_list = process_file(inputfile, pfarg=args.verbose)

    write_data(outputfile, uri_list, context_list)

def process_file(infile: str, pfarg: bool = None) -> tuple[list[str], list[str]]:
    """
    Processes the file of JSON generated by Adobe Assurance and collects the
    URI data string and the variable context data.

    :rtype: object
    :param arg: True to print to screen, default is False.
                Will be set True by the presence of the `-v` command line option
    :type arg: bool
    :param infile: Complete path and filename for the file to be processed
    :type infile: str
    """
    pfarg = True if pfarg is not None and pfarg is not False else False

    f = open(infile, "r")

    # list to collect matching URIs. It will then be written to file at the end
    uri_list: list[str] = []
    # list to collect context data
    context_list: list[str] = []

    json_data: dict = json.load(f)

    length: int = len(json_data["events"])

    for k in range(length):
        try:
            # this is what we use for validating data is correct
            if "hitUrl" in json_data["events"][k]["payload"]["ACPExtensionEventData"]:
                # remove the URI encoding and add to list for printing
                item: str = urllib.parse.unquote(
                    json_data["events"][k]["payload"]["ACPExtensionEventData"].get(
                        "hitUrl"
                    )
                )
                if pfarg:
                    print(item, "\n")
                uri_list.append(item)
            # this is what we use for validating context data variables (for SDR, &c)
            elif (
                "contextdata"
                in json_data["events"][k]["payload"]["ACPExtensionEventData"]
            ):
                item = (json_data["events"][k]["payload"]["ACPExtensionEventData"]).get(
                    "contextdata"
                )
                if pfarg:
                    print()
                    pprint(item)
                    print()
                context_list.append(item)
        # 'key' refers to dictionary key, not a keyboard key
        except KeyError:
            print(">>>>>>>>>> <<<<<<<<<<")
            print(">>>>>>>>>> key error, `ACPExtensionEventData` not found <<<<<<<<<<")
            print(
                ">>>>>>>>>> payload is ",
                json_data["events"][k]["payload"],
                "<<<<<<<<<<",
            )
            print(">>>>>>>>>> <<<<<<<<<<\n")
    f.close()
    return uri_list, context_list

def write_data(url_data: str, uri_list: list[str], context_list: list[str]) -> None:
    """
    Writes the accumulated URI and context data to a file. URI is written to
    the top of the file and context data follows it.

    :param uri_list: list in which data URI are captured from `hitUrl`
    :type uri_list: list
    :param context_list: list in which context data objects are captured
           from `contextdata`
    :type context_list: list
    :param url_data: the JSON file from which the data is to be extracted.
    :type url_data: str
    """
    w = open(url_data, "w")

    # output to file
    for i in uri_list:
        w.write(i + "\n")

    for i in context_list:
        w.write("\n")
        pprint(i, stream=w)

    w.close()

if __name__ == "__main__":
    main()


#User function Template for python3
# https://practice.geeksforgeeks.org/problems/quick-sort/1

class Solution:
    #Function to sort a list using quick sort algorithm.
    def quickSort(self,arr,low,high):
        # code here

        if(low>=high):return

        pi =  self.partition(arr,low,high)

        self.quickSort(arr,low,pi-1)
        self.quickSort(arr,pi+1,high)

    def partition(self,arr,low,high):
        # code here

        pi = high
        l = low
        r = high - 1

        while(l<=r):
            # imaging all element before pi defined is less then the pi , l will reach up to pi index
            # and swap will pi at the end

            while(l<=r and arr[l]<=arr[pi]):
                l += 1

            # imaging all element before pi are greater then arr[pi] in that case r will go upto -1 index
            # from both case we conclude that we need to swap with l with pi

            while(l<=r and arr[r]>arr[pi]):
                r -= 1

            if(l<=r):
                arr[l],arr[r] = arr[r],arr[l]

        arr[l],arr[pi] = arr[pi],arr[l]

        return l

#{
#  Driver Code Starts
#Initial Template for Python 3

if __name__ == "__main__":
    t=int(input())
    for i in range(t):
        n=int(input())
        arr=list(map(int,input().split()))
        Solution().quickSort(arr,0,n-1)
        for i in range(n):
            print(arr[i],end=" ")
        print()

# } Driver Code Ends


import math
from random import randint

def generic_agency_or_account(number_of_digits=4):
    account = list()
    for i in range(0, number_of_digits):
        account.append(randint(0, 9))
    return account

def generic_multiplier(multipliers):
    sum_numbers = 0
    for i, j in enumerate(reversed(multipliers), start=2):
        sum_numbers += i * j
    return sum_numbers

def formatter(list, type, digit=''):
    formatted = ''
    for i in list:
        formatted += str(i)
    if type.upper() in 'AG':
        return f'AG: {formatted} ' if str(digit) in '' else f'AG: {formatted}-{str(digit)} '
    elif type.upper() in 'CC':
        return f'CC: {formatted}-{str(digit)}'
    else:
        return f'AG: {formatted[:4]} CC: {formatted[4:]}-{digit}'

def banco_do_brazil():
    agency = generic_agency_or_account()
    account = generic_agency_or_account(8)

    sum_agency_dv = generic_multiplier(agency)
    sum_account_dv = generic_multiplier(account)

    agency_dv = abs((sum_agency_dv % 11) - 11)
    if agency_dv == 11 or agency_dv == 10:
        agency_dv = 0 if agency_dv == 10 else 'X'

    account_dv = abs((sum_account_dv % 11) - 11)
    if account_dv == 11 or account_dv == 10:
        account_dv = 0 if account_dv == 10 else 'X'

    agency_formatted = formatter(agency, 'A', agency_dv)
    account_formatted = formatter(account, 'CC', account_dv)
    return f'{agency_formatted}{account_formatted}'

def citibank():
    agency = generic_agency_or_account()
    account = generic_agency_or_account(7)

    sum_account_dv = generic_multiplier(account)

    account_dv = abs((sum_account_dv % 11) - 11)
    if account_dv == 11 or account_dv == 10:
        account_dv = 0

    agency_formatted = formatter(agency, 'AG')
    account_formatted = formatter(account, 'CC', str(account_dv))
    return f'{agency_formatted}{account_formatted}'

def itau():
    account = generic_agency_or_account(9)

    sum_account_dv = 0
    for i, j in enumerate(account):
        if i % 2 == 0:
            par = j * 2
            if par > 9:
                sum_account_dv += (par % 10) + math.floor(par / 10)
            else:
                sum_account_dv += par
        else:
            sum_account_dv += j

    account_dv = abs((sum_account_dv % 10) - 10)
    if sum_account_dv % 10 == 0:
        account_dv = 0

    account_formatted = formatter(account, 'ALL', str(account_dv))
    return account_formatted

def caixa():
    agency = generic_agency_or_account()
    account = [0, 0, 1]

    for i in range(0, 12):
        if i < 4:
            account.insert(0, agency[i])
        else:
            account.append(randint(0, 9))

    sum_account_dv = 0
    for i, j in enumerate(reversed(account), start=2):
        if i <= 9:
            sum_account_dv += i * j
        else:
            sum_account_dv += (i - 8) * j

    account_dv = abs((math.floor(((sum_account_dv * 10) / 11)) * 11) - (sum_account_dv * 10))
    if account_dv == 10:
        account_dv = 0

    account_formatted = formatter(account, 'ALL', str(account_dv))
    return account_formatted

def santander():
    multipliers = [9, 7, 3, 1, 9, 7, 1, 3, 1, 9, 7, 3]
    agency = reversed(generic_agency_or_account())
    account = generic_agency_or_account(8)

    for i in agency:
        account.insert(0, i)

    sum_dv = 0
    for i in range(len(multipliers)):
        sum_dv += (multipliers[i] * account[i]) % 10

    account_dv = abs(sum_dv % 10 - 10)
    if sum_dv % 10 == 0:
        account_dv = 0

    account_formatted = formatter(account, 'ALL', str(account_dv))
    return account_formatted

def bradesco():
    agency = generic_agency_or_account()
    account = generic_agency_or_account(6)

    sum_agency_dv = generic_multiplier(agency)
    sum_account_dv = generic_multiplier(account)

    agency_dv = abs(sum_agency_dv % 11 - 11)
    if agency_dv == 10 or agency_dv == 11:
        agency_dv = 'P' if agency_dv == 10 else 0

    account_dv = abs(sum_account_dv % 11 - 11)
    if account_dv == 10 or account_dv == 11:
        account_dv = 'P' if account_dv == 10 else 0

    agency_formatted = formatter(agency, 'AG', agency_dv)
    account_formatted = formatter(account, 'CC', account_dv)
    return f'{agency_formatted}{account_formatted}'

def real():
    agency = generic_agency_or_account()
    account = generic_agency_or_account(7)
    multipliers = [8, 1, 4, 7, 2, 2, 5, 9, 3, 9, 5]

    for i in reversed(agency):
        account.insert(0, i)

    sum_account_dv = 0
    for i in range(len(multipliers)):
        sum_account_dv += account[i] * multipliers[i]

    account_dv = abs(sum_account_dv % 11 - 11)
    if sum_account_dv % 11 <= 1:
        account_dv = 1 if sum_account_dv == 0 else 1

    account_formatted = formatter(account, 'ALL', str(account_dv))
    return f'{account_formatted}'

def hsbc():
    agency = generic_agency_or_account()
    account = generic_agency_or_account(6)
    multipliers = [8, 9, 2, 3, 4, 5, 6, 7, 8, 9]

    for i in reversed(agency):
        account.insert(0, i)

    sum_account_dv = 0
    for i in range(len(multipliers)):
        sum_account_dv += account[i] * multipliers[i]

    account_dv = abs(sum_account_dv % 11)
    if account_dv == 0 or account_dv == 10:
        account_dv = 0

    account_formatted = formatter(account, 'ALL', str(account_dv))
    return f'{account_formatted}'

def select_bank(bank='Indiferente', all_banks=False):
    lista_bancos = ['Banco do Brasil', 'Caixa', 'Santander', 'Itau', 'HSBC', 'Citibank', 'Bradesco', 'Real',
                    'Indiferente']
    if all_banks:
        return lista_bancos

    options = {
        lista_bancos[0]: banco_do_brazil(),
        lista_bancos[1]: santander(),
        lista_bancos[2]: bradesco(),
        lista_bancos[3]: caixa(),
        lista_bancos[4]: itau(),
        lista_bancos[5]: real(),
        lista_bancos[6]: citibank(),
        lista_bancos[7]: hsbc()
    }
    if bank == 'Indiferente':
        selected = randint(0, 7)
    else:
        selected = lista_bancos.index(bank)
    selected_bank = list(options.keys())
    return selected_bank[selected], options[lista_bancos[selected]]

# print(select_bank())
# print(f'Banco do Brasil\n{banco_do_brazil()}\n')
# print(f'Santander\n{santander()}\n')
# print(f'Bradesco\n{bradesco()}\n')
# print(f'Caixa economica federal\n{caixa()}\n')
# print(f'Itaú\n{itau()}\n')
# print(f'Real\n{real()}\n')
# print(f'Citibank\n{citibank()}\n')
# print(f'HSBC\n{hsbc()}\n')


from pathlib import Path

import pytest

from helpers.regression import verify_output

RINOH_PATH = Path(__file__).parent / 'rinoh'
OUTPUT_PATH = Path(__file__).parent / 'output'

def test_version(script_runner):
    ret = script_runner.run('rinoh', '--version')
    assert ret.success
    assert ret.stderr == ''

def collect_tests():
    for rst_path in sorted(RINOH_PATH.glob('*.rst')):
        yield rst_path.stem

@pytest.mark.parametrize('test_name', collect_tests())
def test_rinoh(script_runner, test_name):
    rst_path = Path(test_name + '.rst')
    args = []
    templconf_path = rst_path.with_suffix('.rtt')
    if (RINOH_PATH / templconf_path).exists():
        args += ['--template', str(templconf_path)]
    stylesheet_path = rst_path.with_suffix('.rts')
    if (RINOH_PATH / stylesheet_path).exists():
        args += ['--stylesheet', str(stylesheet_path)]
    output_dir = OUTPUT_PATH / ('rinoh_' + test_name)
    output_dir.mkdir(parents=True, exist_ok=True)
    ret = script_runner.run('rinoh', *args, str(rst_path),
                            '--output', str(output_dir), cwd=RINOH_PATH)
    assert ret.success
    verify_output(test_name, output_dir, RINOH_PATH)


import codecs
import json
import uuid
import re
from scrapy.utils.markup import remove_tags
from scrapy.utils.serialize import ScrapyJSONEncoder
from typing import Union

from scrapper.crowler.data.object import Variant, Step

class JsonWithEncodingPipeline(object):

    def __init__(self):
        self.file = \
            codecs.open(f'scraped_data_{uuid.uuid4()}.json',
                        'w',
                        encoding='utf-8')
        self.parsed_count = 0
        self.delete_spaces = re.compile(r"[\f\n\r\t\v]")

    def delete_tags(self, text: Union[str, None]) -> Union[str, None]:
        if text is not None:
            return remove_tags(text)
        else:
            return None

    def process_step(self, step: Step) -> Step:
        text = self.delete_spaces.sub("", step["text"])
        text = [chunk for chunk in text.split("<p>") if len(chunk) > 2]
        step["text"] = "<p>".join(text)
        return step

    def process_variant(self, variant: Variant) -> Variant:
        variant["variant_name"] = self.delete_tags(variant["variant_name"])
        variant["steps"] = [
            self.process_step(step) for step in variant["steps"]
        ]
        return variant

    def process_item(self, article, spider):
        if (self.parsed_count + 1) % 1000 == 0:
            self.file.close()
            self.file = \
                codecs.open(f'scraped_data_{uuid.uuid4()}.json',
                            'w',
                            encoding='utf-8')

        article["variants"] = [
            self.process_variant(variant) for variant in article["variants"]
        ]
        line = json.dumps(article, cls=ScrapyJSONEncoder) + "\n"
        self.file.write(line)
        self.parsed_count += 1
        return article

    def spider_closed(self, spider):
        self.file.close()


#!/usr/bin/python
# -*- coding: utf-8 -*-
from testcase.BasicTestCase import BasicTestCase
from HelperPage.Helper_Login import Helper_login
from HelperPage.Helper_Logout import Helper_Logout
from HelperPage.Helper_Pulish import Helper_Publish
from HelperPage.Helper_Reply import Helper_Reply
import time

class testLogin(BasicTestCase):
    # 用户登录
    # 默认板块发帖
    # 默认板块回帖
    # 用户退出
    def test_login(self):
        # 用户登录
        Helper_login.login("admin", "password")
        Helper_Publish.clickDefultBlock()
        i = Helper_Publish.returntopicCount()
        # 默认板块发帖
        Helper_Publish.publishQuickly("title:title", "content:content")
        Helper_Publish.clickblock()
        m = Helper_Publish.returnadminreplyCount()
        # 默认板块回帖
        Helper_Reply.replyQuickly("回复内容")
        Helper_Publish.clickblock()
        j = Helper_Publish.returntopicCount()
        if i < j:
            print("发表成功，帖子的数量是" + str(j))
        else:
            print("发表失败")
        n = Helper_Publish.returnadminreplyCount()
        if m < n:
            print("回复成功，回复的数量是" + str(n))
        else:
            print("回复失败")
        # 用户退出
        Helper_Logout.clickLogout()
        time.sleep(2)


import os

def read_file(file_path: str) -> str:
    """Read content of file provided."""
    with open(file_path, "r", encoding="utf-8") as reader:
        return reader.read()

def write_file(to_write: str, file_path: str) -> None:
    """Write input string to file."""
    folder_path = os.path.dirname(file_path)
    if folder_path and not os.path.isdir(folder_path):
        os.makedirs(folder_path)
    with open(file_path, "w+", encoding="utf-8") as writer:
        writer.write(to_write)


# Contest Page: https://www.codechef.com/NOV21C/

def hillSequence(arr_size, arr):

    if arr_size == 1:
        return arr

    arr.sort(reverse=True)
    count = 1

    if arr[0] == arr[1]:    # The max value cannot be repeated
        return -1

    if arr_size == 2:
        return arr

    for i in range(2, arr_size):
        if arr[i] == arr[i - 1]:
            count += 1
        else:
            count = 1

        if count == 2:    # if an item is duplicated move one copy to the front of arr
            temp = arr[i - 1]
            arr.pop(i - 1)
            arr.insert(0, temp)

        if count == 3:    # if an item is triplicated, the problem cannot be solved
            return -1

    return arr

if __name__ == "__main__":

    t = int(input())

    for _ in range(0, t):

        arr_size = int(input())
        arr = [int(x) for x in input().split(" ")]

        result = hillSequence(arr_size, arr)

        if isinstance(result, list):
            print(" ".join([str(x) for x in result]))
        else:
            print(result)


from __future__ import print_function, division
import we
import json
import numpy as np
import sys
if sys.version_info[0] < 3:
    import io
    open = io.open
"""
Hard-debias embedding
Man is to Computer Programmer as Woman is to Homemaker? Debiasing Word Embeddings
Tolga Bolukbasi, Kai-Wei Chang, James Zou, Venkatesh Saligrama, and Adam Kalai
2016
"""

def debias(E, gender_specific_words, definitional, equalize):
    gender_direction = we.doPCA(definitional, E).components_[0]
    #f = open("bolukbasi_debiased_gender_direction.txt", "w")
    #f.write(str(gender_direction))
    #f.close()

    specific_set = set(gender_specific_words)
    for i, w in enumerate(E.words):
        if w not in specific_set:
            E.vecs[i] = we.drop(E.vecs[i], gender_direction)
    E.normalize()
    candidates = {x for e1, e2 in equalize for x in [(e1.lower(), e2.lower()),
                                                     (e1.title(), e2.title()),
                                                     (e1.upper(), e2.upper())]}
    print(candidates)
    for (a, b) in candidates:
        if (a in E.index and b in E.index):
            y = we.drop((E.v(a) + E.v(b)) / 2, gender_direction)
            z = np.sqrt(1 - np.linalg.norm(y)**2)
            if (E.v(a) - E.v(b)).dot(gender_direction) < 0:
                z = -z
            E.vecs[E.index[a]] = z * gender_direction + y
            E.vecs[E.index[b]] = -z * gender_direction + y
    E.normalize()

if __name__ == "__main__":

    embedding_filename = "word2vec_no_reg.txt"
    definitional_filename = "definitional_pairs.json"
    gendered_words_filename = "gendered_words.json"
    equalize_filename = "equalized_pairs.json"
    debiased_filename = "bolukbasi2016_debiased_word2vec.txt"

    with open(definitional_filename, "r") as f:
        defs = json.load(f)
    print("definitional", defs)

    with open(equalize_filename, "r") as f:
        equalize_pairs = json.load(f)

    with open(gendered_words_filename, "r") as f:
        gender_specific_words = json.load(f)
    print("gender specific", len(gender_specific_words), gender_specific_words[:10])

    E = we.WordEmbedding(embedding_filename)

    print("Debiasing...")
    debias(E, gender_specific_words, defs, equalize_pairs)

    print("Saving to file...")
    if embedding_filename[-4:] == debiased_filename[-4:] == ".bin":
        E.save_w2v(debiased_filename)
    else:
        E.save(debiased_filename)

    print("\n\nDone!\n")


# -*- coding: utf-8 -*-
"""
Created on Wed Jun  5 00:48:35 2019

@author: Vamshi Krishna
"""

import matplotlib.pyplot as plt
emp_names=['vamshi','preethi','santhosh','deexita']
emp_salary=[80000,75000,60000,80000]
plt.pie(emp_salary,labels=emp_names,radius=2,autopct='%0.0f%%',shadow=True,explode=[0.2,0,0,0])
plt.show()


#Create a function and ask the user for two inputs and ask what is the operators
def func():
  x=int(input("Enter the num 1 : "))
  y=int(input("Enter the num 2 : "))
  z=input("Choose '+', '-', '*', '/', '%': ")
  if(z=='+'):
    print("Addition :",x+y)
  elif(z=='-'):
    print("Substraction :",x-y)
  elif(z=='*'):
    print("Multiplication :",x*y)
  elif(z=='/'):
    print("Division :",x/y)
  elif(z=='%'):
    print("Remainder :",x%y)

func()


import jingo

from devmo import (SECTION_USAGE, SECTION_ADDONS, SECTION_APPS, SECTION_MOBILE,
                   SECTION_WEB)
from feeder.models import Bundle, Feed

def home(request):
    """Home page."""
    tweets = []
    for section in SECTION_USAGE:
        tweets += Bundle.objects.recent_entries(section.twitter)[:2]
    tweets.sort(key=lambda t: t.last_published, reverse=True)

    updates = []
    for s in SECTION_USAGE:
        updates += Bundle.objects.recent_entries(s.updates)[:1]

    return jingo.render(request, 'landing/home.html', {
        'updates': updates, 'tweets': tweets})

def addons(request):
    """Add-ons landing page."""
    extra = {
        'discussions': Feed.objects.get(
            shortname='amo-forums').entries.all()[:4],
        'comments': Feed.objects.get(
            shortname='amo-blog-comments').entries.all()[:4],
    }
    return common_landing(request, section=SECTION_ADDONS, extra=extra)

def mozilla(request):
    """Mozilla Applications landing page."""
    return common_landing(request, section=SECTION_APPS)

def search(request):
    """Google Custom Search results page."""
    query = request.GET.get('q', '');
    return jingo.render(request, 'landing/searchresults.html', {'query': query})

def mobile(request):
    """Mobile landing page."""
    return common_landing(request, section=SECTION_MOBILE)

def web(request):
    """Web landing page."""
    return common_landing(request, section=SECTION_WEB)

def common_landing(request, section=None, extra=None):
    """Common code for landing pages."""
    if not section:
        raise NotImplementedError

    updates = Bundle.objects.recent_entries(section.updates)[:5]
    tweets = Bundle.objects.recent_entries(section.twitter)[:8]

    data = {'updates': updates, 'tweets': tweets}
    if extra:
        data.update(extra)

    return jingo.render(request, 'landing/%s.html' % section.short, data)


#!/usr/bin/env python3

"""
Extensive database of location and timezone data for nearly every airport and landing strip in the world.
"""
from __future__ import annotations

import csv
from pathlib import Path
from typing import Dict, Literal, TypedDict

__project_name__ = __package__
# Release numbering follows the release date
__version__ = '20221121'
__min_python_version__ = (3, 8)
__author__ = 'Mike Borsetti <mike@borsetti.com>'
__copyright__ = 'Copyright 2020- Mike Borsetti'
__license__ = 'MIT'
__url__ = f'https://github.com/mborsetti/{__project_name__}'

Airport = TypedDict(
    'Airport',
    {
        'icao': str,
        'iata': str,
        'name': str,
        'city': str,
        'subd': str,
        'country': str,
        'elevation': float,
        'lat': float,
        'lon': float,
        'tz': str,
        'lid': str,
    },
)
CodeType = Literal['ICAO', 'IATA', 'LID']

def load(code_type: CodeType = 'ICAO') -> Dict[str, 'Airport']:
    """Loads airport data into a dict

    :param code_type: optional argument defining the key in the dictionary: 'ICAO' (default if omitted),
    'IATA' (for IATA Location Codes) or 'LID' (for U.S. FAA Location Identifiers).

    :return: a dict of dicts, each entry having the following keys:
        'icao': ICAO 4-letter Location Indicator or 4-alphanumeric FAA/TC LID
        'iata': IATA 3-letter Location Code or an empty string
        'name': Official name (latin script)
        'city': City
        'subd': Subdivision (e.g. state, province, region, etc.)
        'country': ISO 3166-1 alpha 2-code (plus 'XK' for Kosovo)
        'elevation': MSL elevation (the highest point of the landing area) in feet
        'lat': Latitude (decimal)
        'lon': Longitude (decimal)
        'tz': Timezone expressed as a tz database name (IANA-compliant) or empty string for country 'AQ' (Antarctica).
            Originally sourced from [TimeZoneDB](https://timezonedb.com)
        'lid': The FAA Location Identifier (for US country only; others is blank)
    """
    # with open(os.path.join(dir, 'airports.json'), encoding='utf8') as f:
    #     airports = json.load(f)
    # if code_type.lower() == 'icao':
    #     return airports
    # else:
    #     return {airport['iata']: airport for airport in dict(airports).values() if airport['iata']}
    #
    #
    key = code_type.lower()
    if key not in ('icao', 'iata', 'lid'):
        raise ValueError(f'code_type must be one of ICAO, IATA or LID; received {code_type}')
    this_dir = Path(__file__).parent
    airports: Dict[str, Airport] = {}
    with this_dir.joinpath('airports.csv').open(encoding='utf8') as f:
        reader = csv.DictReader(f, quoting=csv.QUOTE_NONNUMERIC)
        for row in reader:
            airports[row[key]] = row  # type: ignore[assignment]
    airports.pop('', None)
    return airports

# Python 3.9 code used to save the dict to CSV:
# with open('airports.csv', 'w', newline='') as f:
#     fieldnames = airports[list(airports.keys())[0]].keys()
#     writer = csv.DictWriter(f, fieldnames=fieldnames, quoting=csv.QUOTE_NONNUMERIC)
#     writer.writeheader()
#     for data in airports.values():
#         writer.writerow(data)


import math

# The direction of wolf3d objects in the order their sprites appwear
wolf3d_sprite_directions = [
    "s",     # towards you
    "sw",
    "w",     # facing left
    "nw",
    "n",     # facing away from you
    "ne",
    "e",     # facing right
    "se"
]

# the order in which 0-360 degrees maps to quadrants
real_quadrants = [
    "e",
    "ne",
    "n",
    "nw",
    "w",
    "sw",
    "s",
    "se"
]

quadrant_mapping = {real_quadrants.index(x): wolf3d_sprite_directions.index(x)
                    for x in real_quadrants}

def direction_to_target(a, b):
    '''how many degrees from north is a to b'''
    x1, y1, z1 = a
    x2, y2, z2 = b

    theta = math.atan2(x2-x1, z2-z1)
    angle = (90 - math.degrees(theta)) % 360
    return angle

def angle_to_dir(angle, shift=0, directions = 8):
    angle = (angle + shift)/360
    quadrant = int(angle*directions) % directions
    return int(quadrant)

if __name__ == '__main__':
    for i in range(0, 360, 10):
        print(i, angle_to_dir(i), quadrant_mapping[angle_to_dir(i)])


"""
This script is used to compute neural network embeddings.
"""
import torch
import numpy as np
import sklearn
import pickle
import os
import json
import argparse
from pathlib import Path
from tqdm import tqdm
import librosa

from utils import extract_spectrogram
from models import AudioEncoder

def return_loaded_model(Model, checkpoint):
    model = Model()
    model.load_state_dict(torch.load(checkpoint, map_location=torch.device('cpu')))
    model.eval()
    return model

def extract_audio_embedding(model, filename):
    with torch.no_grad():
        try:
            x = extract_spectrogram(filename)
            x = scaler.transform(x)
            x = torch.unsqueeze(torch.unsqueeze(torch.tensor(x), 0), 0).float()
            embedding, embedding_d = model(x)
            return embedding, embedding_d
        except KeyboardInterrupt:
            return
        except Exception as e:
            print(e, filename)

def extract_audio_embedding_chunks(model, filename):
    with torch.no_grad():
        try:
            x = extract_spectrogram(filename)
            x_chunks = librosa.util.frame(np.asfortranarray(x), frame_length=256, hop_length=256, axis=-1)
            x_chunks = torch.tensor(x_chunks).permute(2, 0, 1)
            x_chunks = torch.unsqueeze(x_chunks, 1)
            embedding_chunks, embedding_d_chunks = model(x_chunks)
            return embedding_chunks, embedding_d_chunks
        except KeyboardInterrupt:
            return
        except Exception as e:
            print(e, filename)

if __name__ == "__main__":
    for MODEL_NAME in [
        'minz_att_4h_w2v_128/audio_encoder_epoch_120',
    ]:
        MODEL_PATH = f'./saved_models/{MODEL_NAME}.pt'

        model = return_loaded_model(AudioEncoder, MODEL_PATH)
        model.eval()

        # GTZAN
        p = Path('./data/GTZAN/genres')
        filenames_gtzan = p.glob('**/*.wav')

        # # US8K
        # p = Path('./data/UrbanSound8K/audio')
        # filenames_us8k = p.glob('**/*.wav')
        #
        # # NSynth
        # p = Path('./data/nsynth/nsynth-train/audio_selected')
        # filenames_nsynth_train = p.glob('*.wav')
        # p = Path('./data/nsynth/nsynth-test/audio')
        # filenames_nsynth_test = p.glob('*.wav')
        #
        # dataset_files = [filenames_gtzan, filenames_us8k, filenames_nsynth_train, filenames_nsynth_test]
        # dataset_names = ['gtzan', 'us8k', 'nsynth/train', 'nsynth/test']

        dataset_files = [filenames_gtzan]
        dataset_names = ['gtzan']

        for filenames, ds_name in zip(dataset_files, dataset_names):

            print(f'\n {ds_name}  {MODEL_NAME}')

            for f in tqdm(filenames):
                try:
                    with torch.no_grad():
                        model_name = MODEL_NAME.split('/')[0] + '_' + MODEL_NAME.split('_epoch_')[-1]
                        folder = f'./data/embeddings/{ds_name}/embeddings_{model_name}'
                        Path(folder).mkdir(parents=True, exist_ok=True)
                        embedding, embedding_d = extract_audio_embedding_chunks(model, str(f))
                        np.save(Path(folder, str(f.stem)+'.npy'), embedding)
                except Exception as e:
                    print(e)
            print('\n')


import numpy as np
import glob
import os
from blimpy import Waterfall
import blimpy as bl
import gc
import time
import matplotlib
import matplotlib.pyplot as plt

start=time.time()

def get_elapsed_time(start=0):
    end = time.time() - start
    time_label = 'seconds'
    if end > 3600:
        end = end/3600
        time_label = 'hours'
    elif end > 60:
        end = end/60
        time_label = 'minutes'
    return end, time_label

def plot_waterfall(wf, source_name=None, f_start=None, f_stop=None, **kwargs):
    # get plot data
    plot_f, plot_data = wf.grab_data(f_start=f_start, f_stop=f_stop)

    # determine extent of the plotting panel for imshow
    extent=(plot_f[0], plot_f[-1], (wf.timestamps[-1]-wf.timestamps[0])*24.*60.*60, 0.0)

    # plot and scale intensity (log vs. linear)
    kwargs['cmap'] = kwargs.get('cmap', 'viridis')
    plot_data = 10.0 * np.log10(plot_data)

    # get normalization parameters
    vmin = plot_data.min()
    vmax = plot_data.max()
    normalized_plot_data = (plot_data - vmin) / (vmax - vmin)

    # display the waterfall plot
    this_plot = plt.imshow(normalized_plot_data,aspect='auto',rasterized=True,interpolation='nearest',extent=extent,**kwargs)

    del plot_f, plot_data

    return this_plot

def one_wf_plot(fil_file_list,f_start=0,f_stop=0,drift_rate=0,source_name_list=[],save_dir=None,save_name=None,extension='png'):
    # set up the sub-plots
    n_plots = len(fil_file_list)
    fig,ax = plt.subplots(n_plots, sharex=True, sharey=True,figsize=(10, 2*n_plots))

    if not f_start or not f_stop:
        print('\nf_start and/or f_stop input error. Try again.\n')
        return None

    # define more plot parameters
    mid_f = np.abs(f_start+f_stop)/2.

    subplots = []

    # Fill in each subplot for the full plot
    for ii, filename in enumerate(fil_file_list):
        # identify panel
        subplot = plt.subplot(n_plots, 1, ii + 1)
        subplots.append(subplot)

        # read in data
        max_load = bl.calcload.calc_max_load(filename)
        wf = bl.Waterfall(filename, f_start=f_start, f_stop=f_stop, max_load=max_load)

        this_plot = plot_waterfall(wf, f_start=f_start, f_stop=f_stop)

        del wf
        gc.collect()

    plt.subplots_adjust(hspace=0,wspace=0)
    [axi.set_axis_off() for axi in ax.ravel()]
    fig.subplots_adjust(left=0, bottom=0, right=1, top=1, wspace=0, hspace=0)

    # save the figures
    if save_dir:
        if save_dir[-1] != "/":
            save_dir += "/"
        if save_name:
            path_png = save_dir+save_name
        else:
            path_png = save_dir + 'freq_' "{:0.6f}".format(mid_f) + '.' + extension
        plt.savefig(path_png, bbox_inches='tight',format=extension, pad_inches=0)

    return fig

blc32_f2_plots=sorted(glob.glob('/storage/home/nxt5197/work/PPO/TOI-216/node_by_node/blc32/Freqs_3904_to_4032/MJDate_59221/plots_TIC55652896_S_f2_snr10.0/*.png'))
os.chdir('/storage/home/nxt5197/scratch/PPO/TOI-216/')
fils=sorted(glob.glob('*h5'))
power_matrix = []
freq_ranges = []
counter_file = '/storage/home/nxt5197/work/589_Machine_Learning/plots/data/icounter.txt'
for i,png in enumerate(blc32_f2_plots):
    f_start = float(png.split('_freq_')[1].split('.png')[0])
    drift_rate = float(png.split('_freq_')[0].split('_dr_')[1])
    if abs(drift_rate) <= 0.10:
        half_f = 250
    elif abs(drift_rate) == 0.11:
        half_f = 272
    elif abs(drift_rate) == 0.13:
        half_f = 304
    elif abs(drift_rate) == 0.14:
        half_f = 337
    elif abs(drift_rate) == 0.32:
        half_f = 769
    elif abs(drift_rate) == 9.14:
        half_f = 21969
    elif abs(drift_rate) == 24.31:
        half_f = 58424
    elif abs(drift_rate) == 36.21:
        half_f = 87026
    else:
        print('You fucked up. You got the drift rate and frequency range wrong. Try again, buddy.')
        break
    f_mid = f_start+half_f*1e-6
    delta_f = 500
    f_stop = f_mid+delta_f/2*1e-6
    f_start = f_mid-delta_f/2*1e-6
    save_dir='/storage/home/nxt5197/work/589_Machine_Learning/plots/imgs/'
    save_name=png.split('/')[-1]
    image = one_wf_plot(fils,f_start=f_start,f_stop=f_stop,save_dir=save_dir,save_name=save_name,extension='png')
    end, time_label = get_elapsed_time(start)
    with open(counter_file, 'w') as f:
        f.write(f'{i+1} of {len(blc32_f2_plots)} for loops completed in {end:.2f} {time_label}.')
os.chdir('/storage/home/nxt5197/work/589_Machine_Learning/plots/data/')
np.save('power_matrix.npy', power_matrix)
np.save('freq_ranges.npy', freq_ranges)


import torch
import torch.nn as nn
from torch.autograd import Variable
import torch.nn.functional as F

def hidden_init(layer):
    fan_in = layer.weight.data.size()[0]
    lim = 1. / np.sqrt(fan_in)
    return (-lim, lim)

class Actor(nn.Module):
    """Initialize parameters and build model.
        Args:
            state_size (int): Dimension of each state
            action_size (int): Dimension of each action
            max_action (float): highest action to take
            seed (int): Random seed
            h1_units (int): Number of nodes in first hidden layer
            h2_units (int): Number of nodes in second hidden layer

        Return:
            action output of network with tanh activation
    """

    def __init__(self, state_dim, action_dim, max_action, seed):
        super(Actor, self).__init__()
        self.seed = torch.manual_seed(seed)

        self.l1 = nn.Linear(state_dim, 400)
        self.l2 = nn.Linear(400, 300)
        self.l3 = nn.Linear(300, action_dim)

        self.max_action = max_action

    def forward(self, x):
        x = F.relu(self.l1(x))
        x = F.relu(self.l2(x))
        x = self.max_action * torch.tanh(self.l3(x))
        return x

class Critic(nn.Module):
    """Initialize parameters and build model.
        Args:
            state_size (int): Dimension of each state
            action_size (int): Dimension of each action
            max_action (float): highest action to take
            seed (int): Random seed
            h1_units (int): Number of nodes in first hidden layer
            h2_units (int): Number of nodes in second hidden layer

        Return:
            value output of network
    """

    def __init__(self, state_dim, action_dim, seed):
        super(Critic, self).__init__()
        self.seed = torch.manual_seed(seed)

        # Q1 architecture
        self.l1 = nn.Linear(state_dim + action_dim, 400)
        self.l2 = nn.Linear(400, 300)
        self.l3 = nn.Linear(300, 1)

        # Q2 architecture
        self.l4 = nn.Linear(state_dim + action_dim, 400)
        self.l5 = nn.Linear(400, 300)
        self.l6 = nn.Linear(300, 1)

    def forward(self, x, u):
        xu = torch.cat([x, u], 1)

        x1 = F.relu(self.l1(xu))

        x1 = F.relu(self.l2(x1))

        x1 = self.l3(x1)

        x2 = F.relu(self.l4(xu))

        x2 = F.relu(self.l5(x2))

        x2 = self.l6(x2)
        return x1, x2

    def Q1(self, x, u):
        xu = torch.cat([x, u], 1)

        x1 = F.relu(self.l1(xu))

        x1 = F.relu(self.l2(x1))

        x1 = self.l3(x1)
        return x1


import gaia.recognition as recognition
import gaia.draw as draw
import gaia.resize as resize
import gaia.utils as cfg
import gaia.people as model

recognizer = recognition.face_recognizer()
recognizer.read('data/trainer.yml')

people = model.all()

def predict(frame):
    global recognizer
    print("Predicting...")
    (gray, faces) = recognition.recognition(frame)
    draw.faces(gray, faces)

    for rect in faces:
        image_temp = resize.compare(gray, rect)
        (id, confidence) = recognizer.predict(image_temp)

        if len(people) >= id:
            label_text = people[id].fullname
            if id > 0:
                draw.face(frame, rect, label_text)

    return frame


# geospatial help functions
import geopandas as gpd
import matplotlib.pyplot as plt

def show_overlaps(data_temp, id_col):
    # checks and plots which areas overlap

    overlaps = []
    for index, row in data_temp.iterrows():
        data_temp1 = data_temp.loc[data_temp[id_col] != row[id_col]] # grab all rows, != this row
        temp_list = data_temp1[data_temp1.geometry.overlaps(row.geometry)][id_col].tolist() # grab rows with overlap
        # get index
        if temp_list != []:
            overlaps += temp_list

    data_overlaps = data_temp.copy()
    data_overlaps = data_overlaps.set_index(id_col)
    overlap_areas = data_overlaps.loc[overlaps]

    ax = data_temp.plot(figsize=size, linewidth=lw, edgecolor="k", color="whitesmoke")
    if len(overlap_areas) > 0:
        overlap_areas.plot(figsize=size, linewidth=lw, edgecolor="k", color="red", ax=ax)
    print("This many rows still overlap:", overlap_areas.shape[0])

def boundary_cut(df_list, cut_df):
    # cuts a list of dfs out of the input df
    for df in df_list:
        cut_df = gpd.overlay(cut_df, df, how="difference")
        return cut_df

def plot_bounds(gdf):
    # quick plot with standard parameters
    gdf.plot(figsize=(24, 20), color="whitesmoke", edgecolor="k", linewidth=0.5)


import logging
from cterasdk import CTERAException

def unsuspend_filer_sync(self=None, device_name=None, tenant_name=None):
    """Unsuspend sync on a device"""
    logging.info("Starting unsuspend sync task.")
    try:
        device = self.devices.device(device_name, tenant_name)
        device.sync.unsuspend()
        logging.info("Unsuspended sync on %s", device.name)
    except Exception as e:
        logging.warning(e)
        logging.error("Error unsuspending sync")


def special_typing():
    # your code here ^_^
    def func(substring, location):
        Length = len(substring)
        if targetLen <= location:
            return Length
        nextList = []
        for i in range(0, Length, 2):
            if substring[i] == target[location]:
                nextList.append(i)

        for head in nextList:
            judge = func(substring[head + 1:], location + 1)
            if judge != None:
                return judge

    num = int(input())
    for i in range(num):
        origin = input()
        target = input()
        targetLen = len(target)
        originLen = len(origin)
        if targetLen > originLen:
            print('NO')
            continue

        storeHead = 0
        while True:
            head = origin.find(target[0], storeHead)
            if head == -1:
                print('NO')
                break

            left = func(origin[head + 1:], 1)
            if left == None or left % 2 != 0:
                storeHead = head + 1
                continue

            print('YES')
            break


import sys

#we want to divide children in groups due to their ages in such way that the
#largest difference in age of any two children in one gorup will be at most 1#
#and we also want to minimize the number of groups
#The task is equal to cover points on the axe with segments of length 1
def MinGroupsNaive(C):
    R = set()
    i = 1
    n = len(C) - 1
    while i <= n:
        (l, r) = (C[i], C[i]+1)
        R.add((l,r))
        i += 1
        while i <= n and C[i] <= r:
                i += 1
    return len(R)

if __name__ == '__main__':
    input = sys.stdin.read()
    C = [float(i) for i in input.split()]
    print(MinGroupsNaive(C))


import wave
import numpy as np
import scipy
from scipy.io.wavfile import read
from scipy.signal import hann
from scipy.fftpack import rfft
import matplotlib.pyplot as plt

def plotFreqSpec(filename,graphpath=None):
    # read audio samples
    framerate,data = read(filename)

    file = wave.open(filename, 'rb')
    nchannels, sampwidth, framerate, nframes = file.getparams()[0:4]

    data = data.astype(np.float, copy=False)

    if nchannels > 1:
        data2 = np.mean(a=data, axis=1)
        data = data2

    windowsize = 1024
    lackingFrames = nframes%windowsize
    data = np.append(data,[0]*lackingFrames)
    window = hann(windowsize)
    data = data/(2**(sampwidth*8))
    audio = data
    for i in range(0,nframes//windowsize):
        # apply a Hanning window
        audio[(i*windowsize):((i+1)*windowsize)] = audio[(i*windowsize):((i+1)*windowsize)]*window
    # fft
    mags = abs(rfft(audio))
    # convert to dB
    mags = 20*scipy.log10(mags)
    # normalise to 0 dB max
    mags -= max(mags)
    # plot
    plt.plot(mags)
    # label the axes
    plt.ylabel("Magnitude (dB)")
    plt.xlabel("Frequency Bin")
    # set the title
    plt.title("Spectrum")
    if graphpath is not None:
        plt.savefig("Spectrum.png")
    else:
        plt.show()

if __name__ == "__main__":
    plotFreqSpec("./static/music/temp.wav")


import webapp2
import random

def rand_fortune():
    fortunes = ["To avoid criticism, do nothing, say nothing, be nothing", "Error 404: Fortune not found", "Optimist believe we live in the best of worlds and pessimists fear this is true.", "Of all our human resources, the most precious is the desire to improve"]
    index = random.randint(0,3)

    return fortunes[index]

class MainHandler(webapp2.RequestHandler):
    def get(self):
        header = "
Fortune Cookie
"

        fortune = rand_fortune()
        final_fortune_sent = "
"'Your fortune is: ' + fortune + "
"

        lucky_num = random.randint(1,100)
        num_sent = 'Your lucky number: ' + str(lucky_num)
        final_num_sent = "
" + num_sent + "
"

        another_cookie = "
<button>another cookie please</button>
"

        da_cookie = header + final_fortune_sent + final_num_sent + another_cookie
        self.response.write(da_cookie)

app = webapp2.WSGIApplication([
    ('/', MainHandler)
], debug=True)


from django.shortcuts import render, redirect, reverse

# Create your views here.

from django.views.decorators.http import require_GET
from django.contrib.auth.decorators import login_required
from goods.models import Goods
from . import models

@require_GET
@login_required
def add(req, count, goods_id):
    goods = Goods.objects.get(pk=goods_id)
    user = req.user

    try:
        shopCart = models.ShopCart.objects.get(user=user, goods=goods)
        shopCart.count += int(count)
        shopCart.allTotal = shopCart.count * goods.price
        shopCart.save()
    except:
        shopCart = models.ShopCart(goods=goods, user=user)
        shopCart.count = int(count)
        shopCart.allTotal = shopCart.count * goods.price
        shopCart.save()

    return redirect(reverse('shopcart:list'))

#购物车的列表
@login_required
def list(req):
    shopcarts = models.ShopCart.objects.filter(user=req.user).order_by('-addTime')

    return render(req, 'shopcart/list.html', {'shopcarts': shopcarts})

#购物车的删除
def delete(req, shopcart_id):
    shopCart = models.ShopCart.objects.get(pk=shopcart_id)
    shopCart.delete()
    return redirect(reverse('shopcart:list'))