import jax
import tensorflow as tf
import numpy as np
from transformers import GPT2TokenizerFast
import itertools

class TFRecordLoader:
    def __init__(self, index_fname, batch_size, parse_fn, map_fn=None, restore_state=None):
        if restore_state is not None:
            self.file_idx = restore_state["file_idx"]
            self.file_idx_init = False
            self.used = restore_state["used"]
        else:
            self.file_idx = 0
            self.file_idx_init = True
            self.used = []

        self.index = open(index_fname).read().splitlines()
        self.clean_index = list(filter(lambda x: x not in self.used, self.index))
        self.bs = batch_size
        # self.seq = sample_size
        self.parse_fn = parse_fn

        if map_fn:
            self.map_fn = map_fn
        else:
            self.map_fn = lambda x: x

        self.sample_fn = self.sample_once()

    def reset(self):
        self.file_idx = 0
        self.file_idx_init = True
        self.used = []

        self.clean_index = list(filter(lambda x: x not in self.used, self.index))
        self.sample_fn = self.sample_once()

    def sample_once(self):
        for i in self.clean_index:
            compression = "ZLIB" if "zstd" in i else ""

            file = tf.data.TFRecordDataset(i, compression_type=compression).map(self.parse_fn, num_parallel_calls=tf.data.AUTOTUNE)
            file = file.apply(tf.data.experimental.dense_to_ragged_batch(np.prod(self.bs), drop_remainder=True))
            file = file.prefetch(10)

            for file_idx, data in enumerate(file):
                data = jax.tree_map(lambda x: x.numpy(), data)
                data = self.map_fn(data)

                if not self.file_idx_init and file_idx <= self.file_idx:
                    if file_idx % 1000 == 0:
                        print(f"skipping to batch {self.file_idx}, currently at {file_idx}")
                    continue
                self.file_idx_init = True
                self.file_idx = file_idx
                yield jax.tree_map(lambda x: x.reshape(self.bs + x.shape[1:]), data)
            self.used.append(i)
            self.file_idx = 0

    # this loops infinitely, use .sample_once to get an iterator for validation
    def get_samples(self):
        try:
            return next(self.sample_fn)
        except StopIteration:
            self.reset()
            return self.get_samples()

    def get_state(self):
        return {
            "used": self.used,
            "file_idx": self.file_idx
        }

class TFRecordNewInputs(TFRecordLoader):
    def __init__(self, index_fname, batch_size, sample_size, restore_state=None):
        def tf_parse(example_proto):
            features = {
                "text": tf.io.VarLenFeature(tf.int64)
            }
            parsed_features = tf.io.parse_single_example(example_proto, features)

            return tf.cast(tf.sparse.to_dense(tf.sparse.reorder(parsed_features["text"])), tf.uint32)

        super().__init__(index_fname, batch_size, tf_parse, restore_state=restore_state)

def _parse_function(example_proto): # https://zhuanlan.zhihu.com/p/552951305  # XD
    feature_desc = {"input_ids": tf.io.VarLenFeature(tf.int64)}
    example = tf.io.parse_single_example(example_proto, feature_desc)
    for name in list(example.keys()):
        t = example[name]
        if t.dtype == tf.int64: t = tf.cast(t, dtype=tf.int32)
        example[name] = tf.sparse.to_dense(t, default_value=0)
        # example[name] = tf.sparse.to_dense(tf.sparse.reorder(t)) # mesh-transformer-jax
    return example

def shard(data, batch_size=None):  # XD
    return jax.tree_map(lambda x: x.numpy().reshape(batch_size + x.shape[1:]), data)  # mtj

def load_tfrecord_dataset(index_fname, batch_size, seq_len, restore_state=None):  # XD
    # adapted from gpt-neo
    fnames = [index_fname] if index_fname.endswith('.tfrecords') else open(index_fname).read().splitlines()
    ds = tf.data.Dataset.from_tensor_slices(fnames)#.repeat()
    ds = ds.apply(tf.data.TFRecordDataset)
    # fp = index_fname; ds = tf.data.TFRecordDataset(fp)
    # # ds = ds.shuffle(buffer_size=min(1000, len(sequences))) # flaxmodels, https://zhuanlan.zhihu.com/p/552951305
    ds = ds.map(_parse_function, num_parallel_calls=tf.data.AUTOTUNE)
    # gradient_accumulation_steps = 8
    # mp_size, dp_size = 8, 1
    # train_mbs_per_replica = 2 # train_micro_batch_size_per_gpu in deepspeed
    # train_batch_size = (gradient_accumulation_steps, train_mbs_per_replica * dp_size)
    # seq_len = 80  # max(len(s) for s in sequences) == 78
    # ds = ds.apply(tf.data.experimental.dense_to_ragged_batch(np.prod(self.bs), drop_remainder=True)) # mtj
    ds = ds.padded_batch(batch_size=np.prod(batch_size), padded_shapes={'input_ids': [seq_len]},
                        padding_values={'input_ids': 0}, drop_remainder=True)
    ds = ds.prefetch(10)  # mesh-transformer-jax
    # ds = ds.repeat()  # gpt-neo/inputs.py
    # map shard directly over ds won't work, getting AttributeError: 'Tensor' object has no attribute 'numpy'
    # because inside tf.function?, see e.g.:
    # 1) https://stackoverflow.com/questions/34097281/convert-a-tensor-to-numpy-array-in-tensorflow
    # 2) https://github.com/tensorflow/tensorflow/issues/27519
    # ds = ds.map(partial(shard, batch_size=batch_size), num_parallel_calls=tf.data.AUTOTUNE)
    # matthias-wright/flaxmodels/training/stylegan2/data_pipeline.py
    return map(lambda x: shard(x, batch_size=batch_size), iter(ds))

class TFRecordWIT(TFRecordLoader):
    def __init__(self, index_fname, batch_size, restore_state=None, text_tokens=256):
        self.tokenizer = GPT2TokenizerFast.from_pretrained("gpt2")
        self.tokenizer.pad_token = "<|endoftext|>"
        self.tokenizer.add_special_tokens({'sep_token': '<|sep|>', 'pad_token': '<|pad|>'})

        def map_fn(example):
            tokenizer = self.tokenizer

            def decode(x):
                return tokenizer(["<|endoftext|>" + i.decode() for i in x])["input_ids"]

            texts = [
                decode(example["context_page_description"]),
                decode(example["context_section_description"]),
                decode(example["caption_reference_description"]),
                decode(example["caption_alt_text_description"]),
                decode(example["caption_attribution_description"]),
            ]

            output = []

            for text, dalle in zip(zip(*texts), example["dalle"]):
                all_text = list(itertools.chain(*text))[-text_tokens+1:]

                all_text += [tokenizer.pad_token_id] * ((text_tokens - 1) - len(all_text))

                assert len(all_text) == text_tokens - 1

                all_tokens = all_text + [tokenizer.sep_token_id] + list(dalle + tokenizer.vocab_size + 1)
                output.append(all_tokens)

            return np.array(output)

        def tf_parse(example_proto):
            features = {
                "page_title": tf.io.FixedLenFeature([], tf.string),
                "section_title": tf.io.FixedLenFeature([], tf.string),
                "hierarchical_section_title": tf.io.FixedLenFeature([], tf.string),
                "caption_reference_description": tf.io.FixedLenFeature([], tf.string),
                "caption_attribution_description": tf.io.FixedLenFeature([], tf.string),
                "caption_alt_text_description": tf.io.FixedLenFeature([], tf.string),
                "mime_type": tf.io.FixedLenFeature([], tf.string),
                "context_page_description": tf.io.FixedLenFeature([], tf.string),
                "context_section_description": tf.io.FixedLenFeature([], tf.string),

                "dalle": tf.io.FixedLenFeature([1024], tf.int64),
            }

            parsed_features = tf.io.parse_single_example(example_proto, features)

            return parsed_features

        super().__init__(index_fname, batch_size, tf_parse, map_fn, restore_state=restore_state)

if __name__ == "__main__":
    # d = TFRecordNewInputs("data/pile.val.index", (8, 32), 2048)
    # for idx, i in enumerate(d.sample_once()):
    #     print(i)
    #     break

    d = TFRecordWIT("data/wit_dalle.train.index", (8, 32))
    for idx, i in enumerate(d.sample_once()):
        print(i)
        break

    print()


import argparse
import numpy as np
import torch
from torchtext.data import Iterator as BatchIter
import torch.nn.functional as F

from Beam import Beam
import causalchains.utils.data_utils as du
from causalchains.utils.data_utils import EOS_TOK, SOS_TOK, PAD_TOK
from causalchains.train.masked_cross_entropy import masked_cross_entropy
from decode_utils import transform, get_tups, get_pred_events

#from data.generate_tups import VERB, SUB, OBJ, PREP
VERB = "__V__"
SUB = "__S__"
OBJ = "__O__"
PREP = "__P__"

def generate(use_cuda=False, device=-1):

    vocab = du.load_vocab(args.vocab)
    eos_id = vocab.stoi[EOS_TOK]
    pad_id = vocab.stoi[PAD_TOK]
    sos_id = vocab.stoi[SOS_TOK]
    tup_id = vocab.stoi[TUP_TOK]

    assert False == (args.perplexity and args.seed and args.ranking), "Only 1 can be True at a time."
    # Batch size during decoding is set to 1
    assert args.batch_size == 1, "Set batch size to 1 during decoding."

    # Load the model.
    with open(args.model, 'rb') as f:
        model = torch.load(f, map_location=lambda s, loc: s)

    # set the eval mode
    model.eval()
    # to decode without cuda
    model.use_cuda = False

    # TEMP FIX to work with old models without this parameter.
    #model.type_emb = None

    # TASK SPECIFIC FUNCTION CALLS
    if args.ranking:
        # HARD only. Easy one has been deactivated.
        do_ranking(model, vocab)
    elif args.perplexity:
        get_perplexity(model, vocab)
    elif args.seed:
        gen_from_seed(model, vocab, eos_id, pad_id, sos_id, tup_id)
    else:
        print("NOT IMPLEMENTED. RETURNING.")
        return

def do_ranking(model, vocab):

    dataset = du.NarrativeClozeDataset(args.data, vocab, src_seq_length=MAX_EVAL_SEQ_LEN, min_seq_length=MIN_EVAL_SEQ_LEN)
    batches = BatchIter(dataset, args.batch_size, sort_key=lambda x:len(x.actual), train=False, device=device)

    ranked_acc = 0.0
    if args.emb_type:
        print("RANKING WITH ROLE EMB")
        vocab2 = du.load_vocab(args.vocab2)
        role_dataset = du.NarrativeClozeDataset(args.role_data, vocab2, src_seq_length=MAX_EVAL_SEQ_LEN, min_seq_length=MIN_EVAL_SEQ_LEN)
        role_batches = BatchIter(role_dataset, args.batch_size, sort_key=lambda x:len(x.actual), train=False, device=device)

        assert len(dataset) == len(role_dataset), "Dataset and Role dataset must be of same length."

        for iteration, (bl, rbl) in enumerate(zip(batches, role_batches)):

            if (iteration+1)%25 == 0:
                print("iteration {}".format(iteration+1))

            ## DATA STEPS
            all_texts = [bl.actual, bl.actual_tgt, bl.dist1, bl.dist1_tgt, bl.dist2, bl.dist2_tgt, bl.dist3, bl.dist3_tgt,
            bl.dist4, bl.dist4_tgt, bl.dist5, bl.dist5_tgt] # each is a tup

            all_roles = [rbl.actual, rbl.dist1, rbl.dist2, rbl.dist3, rbl.dist4, rbl.dist5] # tgts are not needed for role
            assert len(all_roles) == 6, "6 = 6 * 1."

            assert len(all_texts) == 12, "12 = 6 * 2."

            all_texts_vars = []
            all_roles_vars = []

            if use_cuda:
                for tup in all_texts:
                    all_texts_vars.append((Variable(tup[0].cuda(), volatile=True), tup[1]))
                for tup in all_roles:
                    all_roles_vars.append((Variable(tup[0].cuda(), volatile=True), tup[1]))

            else:
                for tup in all_texts:
                    all_texts_vars.append((Variable(tup[0], volatile=True), tup[1]))
                for tup in all_roles:
                    all_roles_vars.append((Variable(tup[0], volatile=True), tup[1]))

            # will itetrate 2 at a time using iterator and next
            vars_iter = iter(all_texts_vars)
            roles_iter = iter(all_roles_vars)

            # run the model and collect ppls for all 6 sentences
            pps = []
            for tup in vars_iter:
                ## INIT AND DECODE before every sentence
                hidden = model.init_hidden(args.batch_size)
                next_tup = next(vars_iter)
                role_tup = next(roles_iter)
                nll = calc_perplexity(args, model, tup[0], vocab, next_tup[0], next_tup[1], hidden, role_tup[0])
                pp = torch.exp(nll)
                #print("NEG-LOSS {} PPL {}".format(nll.data[0], pp.data[0]))
                pps.append(pp.data.numpy()[0])

            # low perplexity == top ranked sentence- correct answer is the first one of course
            assert len(pps) == 6, "6 targets."
            #print("\n")
            all_texts_str = [transform(text[0].data.numpy()[0], vocab.itos) for text in all_texts_vars]
            #print("ALL: {}".format(all_texts_str))
            min_index = np.argmin(pps)
            if min_index == 0:
                ranked_acc += 1
                #print("TARGET: {}".format(transform(all_texts_vars[1][0].data.numpy()[0], vocab.itos)))
                #print("CORRECT: {}".format(transform(all_texts_vars[1][0].data.numpy()[0], vocab.itos)))
            #else:
                # print the ones that are wrong
                #print("TARGET: {}".format(transform(all_texts_vars[1][0].data.numpy()[0], vocab.itos)))
                #print("WRONG: {}".format(transform(all_texts_vars[min_index+2][0].data.numpy()[0], vocab.itos)))

            if (iteration+1) == args.max_decode:
                print("Max decode reached. Exiting.")
                break

        ranked_acc /= (iteration+1) * 1/100 # multiplying to get percent
        print("Average acc(%): {}".format(ranked_acc))
        return ranked_acc

    else: # THIS IS FOR MODEL WITHOUT ROLE EMB

        print("RANKING WITHOUT ROLE EMB.")
        for iteration, bl in enumerate(batches):

            if (iteration+1)%25 == 0:
                print("iteration {}".format(iteration+1))

            ## DATA STEPS
            all_texts = [bl.actual, bl.actual_tgt, bl.dist1, bl.dist1_tgt, bl.dist2, bl.dist2_tgt, bl.dist3, bl.dist3_tgt,
            bl.dist4, bl.dist4_tgt, bl.dist5, bl.dist5_tgt] # each is a tup

            assert len(all_texts) == 12, "12 = 6 * 2."

            all_texts_vars = []
            if use_cuda:
                for tup in all_texts:
                    all_texts_vars.append((Variable(tup[0].cuda(), volatile=True), tup[1]))
            else:
                for tup in all_texts:
                    all_texts_vars.append((Variable(tup[0], volatile=True), tup[1]))

            # will itetrate 2 at a time using iterator and next
            vars_iter = iter(all_texts_vars)

            # run the model for all 6 sentences
            pps = []
            for tup in vars_iter:
                ## INIT AND DECODE before every sentence
                hidden = model.init_hidden(args.batch_size)
                next_tup = next(vars_iter)

                nll = calc_perplexity(args, model, tup[0], vocab, next_tup[0], next_tup[1], hidden)
                pp = torch.exp(nll)
                #print("NEG-LOSS {} PPL {}".format(nll.data[0], pp.data[0]))
                pps.append(pp.data.numpy()[0])

            # low perplexity == top ranked sentence- correct answer is the first one of course
            assert len(pps) == 6, "6 targets."
            #print("\n")
            all_texts_str = [transform(text[0].data.numpy()[0], vocab.itos) for text in all_texts_vars]
            #print("ALL: {}".format(all_texts_str))
            min_index = np.argmin(pps)
            if min_index == 0:
                ranked_acc += 1
                #print("TARGET: {}".format(transform(all_texts_vars[1][0].data.numpy()[0], vocab.itos)))
                #print("CORRECT: {}".format(transform(all_texts_vars[1][0].data.numpy()[0], vocab.itos)))
            #else:
                # print the ones that are wrong
                #print("TARGET: {}".format(transform(all_texts_vars[1][0].data.numpy()[0], vocab.itos)))
                #print("WRONG: {}".format(transform(all_texts_vars[min_index+2][0].data.numpy()[0], vocab.itos)))

            if (iteration+1) == args.max_decode:
                print("Max decode reached. Exiting.")
                break

        ranked_acc /= (iteration+1) * 1/100 # multiplying to get percent
        print("Average acc(%): {}".format(ranked_acc))
        return ranked_acc

def gen_from_seed(model, vocab, eos_id, pad_id, sos_id, tup_id):

    if args.emb_type: # GEN FROM SEED WITH ROLE EMB
        print("GEN SEED WITH ROLE EMB")
        vocab2 = du.load_vocab(args.vocab2)
        # will use this to feed in role ids in beam decode
        ROLES = [vocab2.stoi[TUP_TOK], vocab2.stoi[VERB], vocab2.stoi[SUB], vocab2.stoi[OBJ], vocab2.stoi[PREP]]
        dataset = du.LMRoleSentenceDataset(args.data, vocab, args.role_data, vocab2, src_seq_length=MAX_EVAL_SEQ_LEN, min_seq_length=MIN_EVAL_SEQ_LEN) #put in filter pred later
        dataset = du.LMRoleSentenceDataset(args.data, vocab, args.role_data, vocab2) #put in filter pred later
        batches = BatchIter(dataset, args.batch_size, sort_key=lambda x:len(x.text), train=False, device=device)

        for iteration, bl in enumerate(batches):

            if (iteration+1)%25 == 0:
                print("iteration {}".format(iteration+1))

            ## DATA STEPS
            batch, batch_lens = bl.text
            target, target_lens = bl.target
            role, role_lens = bl.role

            if use_cuda:
                batch = Variable(batch.cuda(), volatile=True)
                role = Variable(role.cuda(), volatile=True)
            else:
                batch = Variable(batch, volatile=True)
                role = Variable(role, volatile=True)

            ## INIT AND DECODE
            hidden = model.init_hidden(args.batch_size)
            #run the model first on t-1 events, except last word. we know corresponding role ids as well.
            seq_len = batch.size(1)
            for i in range(seq_len-1):
                inp = batch[:, i]
                inp = inp.unsqueeze(args.batch_size)
                typ = role[:, i]
                typ = typ.unsqueeze(1)
                _, hidden = model(inp, hidden, typ)

            #print("seq len {}, decode after {} steps".format(seq_len, i+1))
            # beam set current state to last word in the sequence
            beam_inp = batch[:, i+1]
            # do not need this anymore as assuming last sequence role obj is prep.
            #role_inp = role[:, i+1]
 #           print("ROLES LIST: {}".format(ROLES))
 #           print("FIRST ID: {}".format(role[:, i+1]))

            # init beam initializes the beam with the last sequence element. ROLE is a list of roe type ids.
            outputs = beam_decode(model, beam_inp, hidden, args.max_len_decode, args.beam_size, pad_id, sos_id, eos_id, tup_idx=tup_id, init_beam=True, roles=ROLES)
            predicted_events = get_pred_events(outputs, vocab)

            print("CONTEXT: {}".format(transform(batch.data.squeeze(), vocab.itos)))
            print("PRED_t: {}".format(predicted_events)) # n_best stitched together.

            if (iteration+1) == args.max_decode:
                print("Max decode reached. Exiting.")
                break

    else:
        print("GEN SEED WITHOUT ROLE EMB")
        dataset = du.LMSentenceDataset(args.data, vocab, src_seq_length=MAX_EVAL_SEQ_LEN, min_seq_length=MIN_EVAL_SEQ_LEN) #put in filter pred later
        batches = BatchIter(dataset, args.batch_size, sort_key=lambda x:len(x.text), train=False, device=device)
        for iteration, bl in enumerate(batches):

            if (iteration+1)%25 == 0:
                print("iteration {}".format(iteration+1))

            ## DATA STEPS
            batch, batch_lens = bl.text
            target, target_lens = bl.target

            if use_cuda:
                batch = Variable(batch.cuda(), volatile=True)
            else:
                batch = Variable(batch, volatile=True)

            ## INIT AND DECODE
            hidden = model.init_hidden(args.batch_size)

            #run the model first on t-1 events, except last word
            seq_len = batch.size(1)
            for i in range(seq_len-1):
                inp = batch[:, i]
                inp = inp.unsqueeze(args.batch_size)
                _, hidden = model(inp, hidden)

            #print("seq len {}, decode after {} steps".format(seq_len, i+1))
            # beam set current state to last word in the sequence
            beam_inp = batch[:, i+1]

            # init beam initializesthe beam with the last sequence element
            outputs = beam_decode(model, beam_inp, hidden, args.max_len_decode, args.beam_size, pad_id, sos_id, eos_id, tup_idx=tup_id, init_beam=True)
            predicted_events = get_pred_events(outputs, vocab)

            print("CONTEXT: {}".format(transform(batch.data.squeeze(), vocab.itos)))
            print("PRED_t: {}".format(predicted_events)) # n_best stitched together.

            if (iteration+1) == args.max_decode:
                print("Max decode reached. Exiting.")
                break

def get_perplexity(model, vocab):
    total_loss = 0.0
    if args.emb_type: # GET PERPLEXITY WITH ROLE EMB
        print("PERPLEXITY WITH ROLE EMB")
        vocab2 = du.load_vocab(args.vocab2)
        dataset = du.LMRoleSentenceDataset(args.data, vocab, args.role_data, vocab2, src_seq_length=MAX_EVAL_SEQ_LEN, min_seq_length=MIN_EVAL_SEQ_LEN) #put in filter pred later
        batches = BatchIter(dataset, args.batch_size, sort_key=lambda x:len(x.text), train=False, device=device)

        print("DATASET {}".format(len(dataset)))
        for iteration, bl in enumerate(batches):

            if (iteration+1)%25 == 0:
                print("iteration {}".format(iteration+1))

            ## DATA STEPS
            batch, batch_lens = bl.text
            target, target_lens = bl.target
            role, role_lens = bl.role

            if use_cuda:
                batch = Variable(batch.cuda(), volatile=True)
                target = Variable(target.cuda(), volatile=True)
                role = Variable(role.cuda(), volatile=True)
            else:
                batch = Variable(batch, volatile=True)
                target = Variable(target, volatile=True)
                role = Variable(role, volatile=True)

            ## INIT AND DECODE
            hidden = model.init_hidden(args.batch_size)
            ce_loss = calc_perplexity(args, model, batch, vocab, target, target_lens, hidden, role)
            #print("Loss {}".format(ce_loss))
            total_loss = total_loss + ce_loss.data[0]

            if (iteration+1) == args.max_decode:
                print("Max decode reached. Exiting.")
                break

        # after iterating over all examples
        loss = total_loss / (iteration+1)
        print("Average Loss: {}".format(loss))
        return loss

    else:
        print("PERPLEXITY WITHOUT ROLE EMB")
        dataset = du.LMSentenceDataset(args.data, vocab, src_seq_length=MAX_EVAL_SEQ_LEN, min_seq_length=MIN_EVAL_SEQ_LEN) #put in filter pred later
        batches = BatchIter(dataset, args.batch_size, sort_key=lambda x:len(x.text), train=False, device=device)
        for iteration, bl in enumerate(batches):

            if (iteration+1)%25 == 0:
                print("iteration {}".format(iteration+1))

            ## DATA STEPS
            batch, batch_lens = bl.text
            target, target_lens = bl.target

            if use_cuda:
                batch = Variable(batch.cuda(), volatile=True)
                target = Variable(target, volatile=True)
            else:
                batch = Variable(batch, volatile=True)
                target = Variable(target, volatile=True)

            ## INIT AND DECODE
            hidden = model.init_hidden(args.batch_size)
            ce_loss = calc_perplexity(args, model, batch, vocab, target, target_lens, hidden)
            #print("Loss {}".format(ce_loss))
            total_loss = total_loss + ce_loss.data[0]

            if (iteration+1) == args.max_decode:
                print("Max decode reached. Exiting.")
                break

        # after iterating over all examples
        loss = total_loss / (iteration+1)
        print("Average Loss: {}".format(loss))
        return loss

def calc_perplexity(args, model, batch, vocab, target, target_lens, hidden, role=None):

    logits = []
    for i in range(batch.size(1)): #decode input
        inp = batch[:, i]
        inp = inp.unsqueeze(1)
        if args.emb_type:
            typ = role[:, i]
            typ = typ.unsqueeze(1)
            logit, hidden = model(inp, hidden, typ) # add in the role here
        else:
            # keep updating the hidden state accordingly
            logit, hidden = model(inp, hidden)
        logits += [logit]

    logits = torch.stack(logits, dim=1)
    # loss for the sequence
    # making changes to not consider the EOS when calculating the scores.
    target_lens = target_lens - 1
    loss = masked_cross_entropy(logits, target, Variable(target_lens))
    return loss

def beam_decode(model, input, hidden, max_len_decode, beam_size, pad_id, sos_id, eos_id, tup_idx=4, batch_size=1, use_constraints=True, init_beam=False, roles=None):
    # hidden [1, 1, hidden_size]

    assert beam_size > 0 and batch_size == 1, "Beam decoding batch size must be 1 and Beam size greater than 0."

    # Helper functions for working with beams and batches
    def var(a): return Variable(a, volatile=True)

    def bottle(m):
        return m.view(batch_size * beam_size, -1)

    def unbottle(m):
        return m.view(beam_size, batch_size, -1)

    def beam_update(e, idx, positions, beam_size):
        sizes = e.size() # [1, beam_size, hidden_size]
        br = sizes[1]
        if len(sizes) == 3:
            sent_states = e.view(sizes[0], beam_size, br // beam_size,
                                 sizes[2])[:, :, idx]
        else:
            sent_states = e.view(sizes[0], beam_size,
                                 br // beam_size,
                                 sizes[2],
                                 sizes[3])[:, :, idx]

        # [1, beam_size, hidden_size]
        indexed_before = sent_states.data.index_select(1, positions)
        sent_states.data.copy_(
            sent_states.data.index_select(1, positions))
        indexed_after = sent_states.data.index_select(1, positions)

    # 1 beam object as we have batch_size 1 during decoding
    beam = [Beam(beam_size, n_best=args.n_best,
                cuda=use_cuda,
                pad=pad_id,
                eos=eos_id,
                bos=sos_id,
                min_length=10)]

    if init_beam:
        # id of last element in seq to init the beam
        for b in beam:
            b.next_ys[0][0] = np.asscalar(input.data.numpy()[0])

    # [1, beam_size, hidden_size]
    hidden = hidden.repeat(1, beam_size, 1)

    # this comes from the known role id of the last seqence object.
    #if args.emb_type:
        #inp2 = role.repeat(1, beam_size)

    verb_list = [[]]*beam_size #for constraints
    # run the decoder to generate the sequence
    for i in range(max_len_decode):

        # one all beams have EOS break
        if all((b.done() for b in beam)):
            break

        # No need to explicitly set the input to previous output - beam advance does it. Make sure.
        inp = var(torch.stack([b.get_current_state() for b in beam])
                              .t().contiguous().view(-1)) #[beam_size]

        # Tested that the last output is the input in the next time step.
        # Run one step of the decoder
        # dec_out: beam x rnn_size
        inp = inp.unsqueeze(1)

        if args.emb_type:
            curr_idx = i%5
            # this gives the index of the role type: [tup, v, s, o, prep]
            curr_role = roles[curr_idx]
            # wrap into a tensor and make a var. repeat beam times
            inp2 = var(torch.LongTensor([curr_role])).repeat(beam_size, 1)
            logit, hidden = model(inp, hidden, inp2)
        else:
            logit, hidden = model(inp, hidden)

        # [1, beam_size, hidden_size]
        logit = torch.unsqueeze(logit, 0)
        probs = F.log_softmax(logit, dim=2).data
        out = unbottle(probs) # [beam_size, 1, vocab_size]
        out.log()

        # Advance each beam. We have 1 beam object.
        for j, b in enumerate(beam):
            #print("OUT: {}".format(out[:, j])) # [beam_size, vocab_size]
            if use_constraints:
                b.advance(ge.schema_constraint(out[:, j], b.next_ys[-1], verb_list))
            else:
                b.advance(out[:, j])

            beam_update(hidden, j, b.get_current_origin(), beam_size)
            if use_constraints:
                verb_list = ge.update_verb_list(verb_list, b, tup_idx)

    # extract sentences from beam and return
    ret = _from_beam(beam, args.n_best)
    return ret

def _from_beam(beam, n_best=1):
    ret = {"predictions": [],
           "scores": []}
    for b in beam: # Only 1 beam object.
        scores, ks = b.sort_finished(minimum=n_best)
        hyps = []
        for i, (times, k) in enumerate(ks[:n_best]):
            hyp = b.get_hyp(times, k)
            hyps.append(hyp)
        ret["predictions"].append(hyps)
        ret["scores"].append(scores)

    return ret['predictions']

if __name__ == "__main__":
    parser = argparse.ArgumentParser(description='PyTorch Wikitext-2 Language Model')

    # Model parameters.
    parser.add_argument('--data', type=str, help='location of the data corpus')
    parser.add_argument('--vocab', type=str)
    parser.add_argument('--model', type=str, help='model checkpoint to use')
    parser.add_argument('--batch_size', type=int, default=1, metavar='N', help='batch size')
    parser.add_argument('--random_seed', type=int, default=11, help='random seed')
    parser.add_argument('--cuda', action='store_true', help='use CUDA')
    parser.add_argument('-max_len_decode', type=int, default=50, help='Maximum prediction length.')
    # beam related
    parser.add_argument('--beam_size',  type=int, default=-1, help='Beam size')
    parser.add_argument('--n_best', type=int, default=1, help="""outputs the n_best decoded sentences""")
    # types of evaluation
    # THESE NARRATIE CLOZE TASKS ARE NOT USED ANYMORE
    #parser.add_argument('--n_cloze', action='store_true', help="""narrative cloze recall based evaluation""")
    #parser.add_argument('--easy', action='store_true', help="""narrative cloze ranking: easy. predict last event.""")
    parser.add_argument('--perplexity',  action='store_true')
    parser.add_argument('--ranking', action='store_true', help="""narrative cloze ranking""")
    parser.add_argument('--seed', action='store_true', help="""seed based testing""")
    # max decoded
    parser.add_argument('--max_decode', type=int, default=2000, help="""max sentences to be evaluated/decoded.""")
    # role data related
    parser.add_argument('--emb_type', action='store_true')
    parser.add_argument('--role_data', type=str, help='location of the role data corpus')
    parser.add_argument('--vocab2', type=str, help='location of role vocab')
    args = parser.parse_args()

    # Set the random seed manually for reproducibility.
    torch.manual_seed(args.random_seed)
    if torch.cuda.is_available():
        if not args.cuda:
            print("WARNING: You have a CUDA device, so you should probably run with --cuda")

    if args.cuda and torch.cuda.is_available():
        device = 0
        use_cuda = True
    elif args.cuda and not torch.cuda.is_available():
        print("You do not have CUDA, turning cuda off")
        device = -1
        use_cuda = False
    else:
        device = -1
        use_cuda=False

    generate(use_cuda=use_cuda, device=device)


r"""Preprocess code.org data into a form that we can handle."""

import os
import pickle
import getpass
import numpy as np

USER = getpass.getuser()
DATA_ROOT = '/mnt/fs5/{}/generative-grading/data/real/education/'.format(USER)
ZIPF_CLASS = {'head': 0, 'body': 1, 'tail': 2}

def load_data(problem_name):
    problem_id = int(problem_name.split('_')[0].replace('codeorg', ''))
    root = '/mnt/fs5/{}/datasets/code_org_public_dataset_v1/data/p{}'.format(USER, problem_id)
    programs_file = os.path.join(root, 'programs-{}.pickle'.format(problem_id))
    counts_file = os.path.join(root, 'countMap-{}.pickle'.format(problem_id))
    annotations_file = os.path.join(root, 'annotations-{}.pickle'.format(problem_id))

    with open(programs_file, 'rb') as fp:
        programs = pickle.load(fp)

    with open(counts_file, 'rb') as fp:
        counts = pickle.load(fp)

    program2count = {}
    for _id in programs.keys():
        program2count[programs[_id]] = counts[_id]

    ranks = build_rank_map_from_count_map(program2count)
    zipfs = build_zipf_map(program2count, ranks)

    with open(annotations_file, 'rb') as fp:
        annotations = pickle.load(fp)

    countMap = {}
    labelMap = {}
    zipfMap = {}  # program to head/body/tail

    for _id, labels in annotations.items():
        program = programs[_id]
        countMap[program] = counts[_id]
        labelMap[program] = annotations[_id]
        zipfMap[program] = zipfs[program]

    return countMap, labelMap, zipfMap

def build_rank_map_from_count_map(count_map):
    programs = np.array(list(count_map.keys()))
    counts = np.array(list(count_map.values()))
    # NOTE: need to reverse b/c most common = rank 0
    ranks = np.argsort(counts)[::-1]
    programs = np.array(programs)
    programs = programs[ranks]
    return dict(zip(programs, np.arange(len(programs))))

def build_zipf_map(count_map, rank_map):
    programs = np.array(list(count_map.keys()))
    counts = np.array(list(count_map.values()))
    ranks = np.array(list(rank_map.values()))

    # by default, everything is a body
    zipf_classes = np.ones(len(programs)) * ZIPF_CLASS['body']
    # every with less than 3 counts is in the tail
    zipf_classes[counts < 3] = ZIPF_CLASS['tail']
    # every in top 20 is in the head
    zipf_classes[ranks <= 20] = ZIPF_CLASS['head']

    return dict(zip(programs, zipf_classes))

def save_data(out_dir, counts, labels, zipfs):
    with open(os.path.join(out_dir, 'counts.pkl'), 'wb') as fp:
        pickle.dump(counts, fp)

    with open(os.path.join(out_dir, 'labels.pkl'), 'wb') as fp:
        pickle.dump(labels, fp)

    with open(os.path.join(out_dir, 'zipfs.pkl'), 'wb') as fp:
        pickle.dump(zipfs, fp)

if __name__ == '__main__':
    import argparse
    arg_parser = argparse.ArgumentParser()
    arg_parser.add_argument(
        'out_dir',
        type=str,
        help='where to dump raw data')
    arg_parser.add_argument(
        'problem',
        type=str,
        help='codeorg1|codeorg9')
    args = arg_parser.parse_args()

    if not os.path.isdir(args.out_dir):
        os.makedirs(args.out_dir)

    counts, labels, zipfs = load_data(args.problem)
    save_data(args.out_dir, counts, labels, zipfs)


# -*- coding: utf-8 -*-
from __future__ import absolute_import, unicode_literals

from django.conf.urls import url

from . import views

app_name = 'proyecto'
urlpatterns = [
    # URL pattern for the UserListView
    url(
        regex=r'^$',
        view=views.ProyectoListView.as_view(),
        name='list'
    ),

    url(
        regex=r'^(?P<nombre>[\w.@+-]+)/$',
        view=views.ProyectoDetailView.as_view(),
        name='detail'
    ),

    url(
        regex=r'^(?P<nombre>[\w.@+-]+)/kanban/(?P<sprintId>[0-9]+)$',
        view=views.kanban,
        name='kanban'
    ),
    url(
        regex=r'^(?P<nombre>[\w.@+-]+)/tablero/(?P<sprintId>[0-9]+)$',
        view=views.tablero,
        name='tablero'
    ),

    # URL pattern for the UserUpdateView
    url(
        regex=r'^~update/$',
        view=views.ProyectoUpdateView.as_view(),
        name='update'
    ),
]


import os
from pathlib import Path

dir_ = os.path.abspath(os.curdir)

p = Path(f'{dir_}/text2/text3/text4/text5/text6')

p.mkdir(parents=True, exist_ok=True)

def rec():
    for i in os.listdir():
        if Path(i).is_file():
            print('file-',i)
        else:
            print('dir-',i)
            os.chdir(i)
            rec()
    os.chdir('..')
    return

#rec()

for dirpath, dirnames, filenames in os.walk(os.getcwd()):
    print('path', dirpath)
    print('dirs', dirnames)
    print('filenames', filenames)
    print()


from django.urls import path, include

from my_plant_app.web.views import create_profile, show_index, details_profile, edit_profile, delete_profile, \
    show_catalogue, create_plant, details_plant, edit_plant, delete_plant

urlpatterns = (
    path('', show_index, name='show index'),
    path('profile/', include([
        path('create/', create_profile, name='create profile'),
        path('details/', details_profile, name='details profile'),
        path('edit/', edit_profile, name='edit profile'),
        path('delete/', delete_profile, name='delete profile'),
    ])),

    path('catalogue/', show_catalogue, name='show catalogue'),
    path('create/', create_plant, name='create plant'),
    path('details/<pk>/', details_plant, name='details plant'),
    path('edit/<pk>/', edit_plant, name='edit plant'),
    path('delete/<pk>/', delete_plant, name='delete plant'),
)


#################################################################################
# Capstone Project - Career Study Advisor
# By: Kevin Elliott (ellkev004), Ryan Wong (wngrya001) and Zena Kelz (klzzen001)
# 21/07/2014 - 22/09/2014
# This document is for navigation throught the website
#################################################################################
from django.conf.urls import patterns, url

#Imports Career, Search, Home, Category, Category_Index, Institution_Career, Qualification,
#Institution_Index, Institution
from advisor import views

#Regex used to describe url patterns
# + -> 1 or many
# * -> 0 or many
# ? -> 0 or 1
# These symbols apply to the character immediately before which is why you need to use brackets
# for application to more than one character

urlpatterns = patterns('',
  url(r'^search/?$', views.search, name='search'),

  url(r'^career/(?P<career_name>\w+(\s+\w+)*)/liked/?$', views.unlike_career, name='unlike_career'),
  url(r'^career/(?P<career_name>\w+(\s+\w+)*)/like/?$', views.like_career, name='like_career'),
  url(r'^career/(?P<career_name>\w+(\s+\w+)*)/(?P<inst_name>\w[\w\s\(\)]*)/?$', views.institution_career, name='institution_career'),

  url(r'^career/(?P<career_name>\w+(\s+\w+)*)/?$', views.career, name='career'),
  url(r'^career/?$', views.career_index, name='career_index'),

  url(r'^institution/(?P<inst_name>\w[\w\s\(\)]*)/(?P<qualification_name>\w[\w\s\(\),&]*)/liked/?$', views.unlike_qualification, name='unlike_qualification'),
  url(r'^institution/(?P<inst_name>\w[\w\s\(\)]*)/(?P<qualification_name>\w[\w\s\(\),&]*)/like/?$', views.like_qualification, name='like_qualification'),

  url(r'^institution/(?P<inst_name>\w[\w\s\(\)]*)/(?P<qualification_name>\w[\w\s\(\),&]*)/?$', views.qualification, name='qualification'),
  url(r'^institution/(?P<institution_name>\w[\w\s\(\)]*)/?$', views.institution, name='institution'),
  url(r'^institution/?$', views.institution_index, name='institution_index'),
  url(r'^category/(?P<category_name>\w+(\s+\w+)*)/?$', views.category, name='category'),
  url(r'^category/?$', views.category_index, name='category_index'),
  url(r'^qualification/?$', views.qualification_index, name='qualification_index'),

  url(r'^recommend/?$', views.recommend_careers_and_qualifications, name ='recommend_careers_and_qualifications'),

  url(r'^accounts/login/?$', views.login, name ='login'),
  url(r'^accounts/auth/?$', views.auth_view, name ='auth_view'),
  url(r'^accounts/logout/?$', views.logout, name ='logout'),
  url(r'^accounts/loggedin/?$', views.loggedin, name = 'loggedin'),
  url(r'^accounts/register/?$', views.register_user, name = 'register_user'),

  url(r'^', views.home, name='home'),
)


import numpy as np
from scipy.optimize import curve_fit
def logistic_model(t,r,K):
    return K/(1+(K/p0-1)*np.exp(-r*t))
N=int(input().strip())
populations=list(map(int,input().strip().split()))
T=int(input().strip())
years_to_predict=[int(input().strip())for _ in range(T)]
p0=populations[0]
t=np.arange(0,N)
initial_guess=[0.03,400]
params, _ = curve_fit(logistic_model,t,populations,p0=initial_guess,maxfev=10000)
r,K=params
predictions=[]
for year in years_to_predict:
    prediction=logistic_model(year,r,K)
    predictions.append(prediction)
for pred in predictions:
    print(f"{pred:.2f}")

山东大学 数学模型 程序汇总/人口增长的logistic模型.py


import requests
import json
from bs4 import BeautifulSoup
import re
import nltk
nltk.download('stopwords')
from nltk.corpus import stopwords

# Everthing in one file

stop_words = stopwords.words('english')

def scrap_content(url):
    page=requests.get(url)
    page_content= page.text
    content = BeautifulSoup(page_content, 'html.parser')
    # only paragraphs
    para= content.find_all('p')
    text_list = [p.text for p in para]
    complete_text = ' '.join(text_list)
    return complete_text

def clean_text(text):
    #text = remove_stopWords(text)
    text = ' '.join(text.split())
    text = remove_urls(text)
    text = text.lower()
    return text

#not used
def limit_to_2000_words(text):
    words = text.split()
    return ' '.join(words[:2000])

#any text size can fit now
def split_text(text):
    max_chunk_size = 2000
    chunks = []
    current_chunk = ""
    for sentence in text.split("."):
        if len(current_chunk) + len(sentence) < max_chunk_size:
            current_chunk += sentence + "."
        else:
            chunks.append(current_chunk.strip())
            current_chunk = sentence + "."
    if current_chunk:
        chunks.append(current_chunk.strip())
    return chunks

# I dont like links
def remove_urls(text):
    url_pattern = re.compile(r'https?://\S+|www\.\S+')
    return url_pattern.sub(r'', text)

#not used but ok
def remove_stopWords(text):
    splittedText=text.split()
    withoutStopWords=[word for word in splittedText if word not in stop_words]
    return ' '.join(withoutStopWords)

#  the API to the local llm
# the summary is a list of summary response concatenated
# I didnt use langchain but there is a colab notebook for another solution
def get_response(text):
    input_chunks = split_text(text)
    output_chunks = []
    url= "http://localhost:1234/v1/chat/completions"
    headers={
        "content-type": "application/json"
    }
    system_message = {
    "role": "system",
    "content": "### Instruction: You are a helpful assistant. provide a concise summary of the following text with less than 80 words:  "
    }
    messages=[system_message]
    for chunk in input_chunks:
        user_message ={
                       "role":"user",
                       "content": f"{chunk} \n###Response:" }
        messages.append(user_message)
        data={
        "messages":messages,
        "temperature": 0.7,
        "max_tokens": 500,
        "stream": False
        }
        response = requests.post(url,headers=headers, data=json.dumps(data))
        if response.status_code == 200:
            response = response.json()
            summary=response["choices"][0]["message"]["content"]
            output_chunks.append(summary)
        else:
            print(f"Error: {response.status_code}")
    return " ".join(output_chunks)

url=input("Enter a link to summarize content inside: ")

page_text = scrap_content(url)
cleanedText = clean_text(page_text)
print(f"The cleaned text is : {cleanedText}/n /n /n")
with open('cleanedText.txt', 'w',encoding="utf-8") as f:
    f.write(cleanedText)

summary_answer = get_response(cleanedText)
print(f"The Summary is : {summary_answer}")
with open('summary.txt', 'w',encoding="utf-8") as f:
    f.write(summary_answer)


import aiopg

class DbSession(object):
    engine = None
    connection = None
    cursor = None
    status = None

    def __init__(self, engine, *, transactional=True):
        self.transactional = transactional
        self.engine = engine
        self.connection = None
        self.status = 'new'

    @property
    def pool(self):
        return self.engine.pool

    @classmethod
    def new(cls):
        return cls()

    async def commit(self):
        await self.cursor.execute('COMMIT')
        self.status = 'committed'

    async def rollback(self):
        await self.cursor.execute('ROLLBACK')
        self.status = 'rollbacked'

    async def __aenter__(self):
        self.connection = await self.pool.acquire()
        self.cursor = await self.connection.cursor()
        if self.transactional:
            await self.cursor.execute('BEGIN')
        self.status = 'ready'
        return self

    # noinspection PyUnusedLocal
    async def __aexit__(self, exc_type, exc_value, traceback):
        if self.transactional and self.status == 'ready':
            await self.commit()
        self.cursor.close()
        await self.pool.release(self.connection)

    async def execute(self, query, *, params=None):
        query = query.replace('?', '%s')
        try:
            await self.cursor.execute(query, params)
            return self.cursor
        # noinspection PyBroadException
        except:
            if self.transactional:
                await self.rollback()
            raise

class PostgreSqlEngine(object):
    def __init__(self, conf):
        self.conf = conf

    _pool = None

    @property
    def pool(self):
        if self._pool is None:
            raise ValueError('Engine is noy initialized')
        return self._pool

    @property
    def dsn(self):
        return 'dbname=%s user=%s password=%s host=%s port=%s' % (
            self.conf.get('postgres.db'),
            self.conf.get('postgres.user'),
            self.conf.get('postgres.password'),
            self.conf.get('postgres.host'),
            self.conf.get('postgres.port'),
        )

    async def initialize(self):

        if self._pool is not None:
            raise ValueError('Object is already initialized')

        self._pool = await aiopg.create_pool(
            self.dsn,
            minsize=10,
            timeout=60
        )

    async def shutdown(self):
        self.pool.terminate()
        await self.pool.wait_closed()
        self._pool = None

    async def begin(self):
        return DbSession(self, transactional=True)

    async def session(self):
        return DbSession(self, transactional=False)

    async def execute(self, query: str, *, params=None):
        async with await self.session() as session:
            return await session.execute(query, params=params)

    async def __aenter__(self):
        await self.initialize()
        return self

    # noinspection PyUnusedLocal
    async def __aexit__(self, exc_type, exc_value, traceback):
        await self.shutdown()


import argparse
from itertools import islice
import random

def run_martingale_simulation(initial_bankroll, initial_bet):
    bankroll = initial_bankroll
    bet = initial_bet
    results = []
    while bankroll > 0:
        result = simulate_spin()
        if result == 'win':
            bankroll += bet
            bet = initial_bet
        else:
            bankroll -= bet
            bet *= 2
        results.append(bankroll)
    return results

def run_reverse_martingale_simulation(initial_bankroll, initial_bet, win_limit, max_spins):
    bankroll = initial_bankroll
    bet = initial_bet
    spins = 1
    results = []
    win_streak = 0
    while bankroll > 0 and spins <= max_spins:
        result = simulate_spin()
        if result == 'win':
            bankroll += bet
            bet *= 2
            win_streak += 1
            if win_streak >= win_limit:
                bet = initial_bet
        else:
            bankroll -= bet
            bet = initial_bet
            win_streak = 0
        results.append(bankroll)
        spins += 1
    return results

def run_dalembert_simulation(initial_bankroll, initial_bet):
    bankroll = initial_bankroll
    bet = initial_bet
    results = []
    while bankroll > 0 and bet > 0:
        result = simulate_spin()
        if result == 'win':
            bankroll += bet
            bet -= 1
        else:
            bankroll -= bet
            bet += 1
        results.append(bankroll)
    return results

def run_labouchere_simulation(initial_bankroll, sequence, max_spins):

    bankroll = initial_bankroll

    results = []
    while bankroll > 0 and sequence:
        bet = sequence[0] + sequence[-1]
        result = simulate_spin()
        if result == 'win':
            bankroll += bet
            sequence = sequence[1:-1]
        else:
            bankroll -= bet
            sequence.append(bet)
        results.append(bankroll)

    return results

def run_fibonacci_simulation(initial_bankroll, initial_bet):
    bankroll = initial_bankroll
    results = []
    fib_seq = [1, 1]
    idx = 0
    while bankroll > 0:
        bet = fib_seq[idx] * initial_bet
        result = simulate_spin()
        if result == 'win':
            bankroll += bet
            idx = max(0, idx - 2)
        else:
            bankroll -= bet
            idx += 1
            if idx >= len(fib_seq):
                fib_seq.append(fib_seq[-1] + fib_seq[-2])
        results.append(bankroll)
    return results

def run_oscars_grind_simulation(initial_bankroll, initial_bet, max_spins):
    bankroll = initial_bankroll
    bet = initial_bet
    results = []
    profit = 0
    spins = 0  # to keep track of the number of spins

    while bankroll >= initial_bet and spins < max_spins:
        result = simulate_spin()
        spins += 1  # increment the spin count after each spin

        if result == 'win':
            bankroll += bet
            profit += bet
            if profit >= initial_bet:
                profit = 0
                bet = initial_bet
        else:
            bankroll -= bet
            profit = 0
            bet = min(bankroll, bet + initial_bet)

        results.append(bankroll)

        if bankroll < initial_bet or spins >= max_spins:
            break  # break out of the loop if bankroll is less than initial_bet or max_spins is reached

    return results

# Define Simulation Spin
def simulate_spin():
    number = random.randint(0, 36)
    return 'win' if number % 2 == 0 else 'loss'

def run_simulation(strategy_func, *args, **kwargs):
    max_spins = kwargs.get('max_spins', 100)  # You can set a default value or manage it differently

    strategy_results = strategy_func(*args, **kwargs)  # Store the result of strategy_func in a separate variable

    # Initialize results list to store the dictionaries
    results = []
    for spin_count, bankroll in enumerate(strategy_results, 1):
        peek = next(islice(strategy_results, 1, 2), None)
        if spin_count < 5: #First 5 are always printed
            results.append({"spin_count": spin_count, "bankroll": bankroll})
            print(f"Spin {spin_count}: {bankroll}")
        if spin_count % 10 == 0: #Every 10th is printed
            results.append({"spin_count": spin_count, "bankroll": bankroll})
            print(f"Spin {spin_count}: {bankroll}")
        if spin_count == max_spins or bankroll <= 0: #Last one is printed
            results.append({"spin_count": spin_count, "bankroll": bankroll})
            print(f"Spin {spin_count}: {bankroll}")
        if bankroll <= 0 or spin_count == max_spins or not peek:
            return results

def main():
    print("Main Called")
    parser = argparse.ArgumentParser(description='Roulette Strategy Simulation')
    parser.add_argument('-s', '--strategy', type=str, required=True, help='Name of the strategy to simulate')
    parser.add_argument('-b', '--bankroll', type=int, required=True, help='Initial bankroll')
    parser.add_argument('-bet', '--initial_bet', type=int, required=True, help='Initial bet amount')
    parser.add_argument('-m', '--max_spins', type=int, default=100, help='Maximum number of spins')
    parser.add_argument('-w', '--win_limit', type=int, default=3, help='Win limit for Reverse Martingale Strategy')
    parser.add_argument('-seq', '--sequence', nargs='+', type=int, default= [1,2,3], help='List of numbers for Labouchere System')

    args = parser.parse_args()
    strategy = args.strategy.lower()

    # Mapping strategy names to corresponding functions
    strategies = {
        'mart': run_martingale_simulation,
        'rev': run_reverse_martingale_simulation,
        'dale': run_dalembert_simulation,
        'labo': run_labouchere_simulation,
        'fibo': run_fibonacci_simulation,
        'osca': run_oscars_grind_simulation
    }

    strategy_func = strategies.get(strategy)
    if not strategy_func:
        print(f"Invalid strategy: {strategy}")
        return

    # Running the selected strategy with the required parameters
    if strategy.startswith('mart'):
        run_simulation(strategy_func, args.bankroll, args.initial_bet, max_spins=args.max_spins)
    elif strategy.startswith('rev'):
        run_simulation(strategy_func, args.bankroll, args.initial_bet, args.win_limit, max_spins=args.max_spins)
    elif strategy.startswith('dale'):
        run_simulation(strategy_func, args.bankroll, args.initial_bet, max_spins=args.max_spins)
    elif strategy.startswith('labo'):
        if not args.sequence:
            print("Sequence is required for Labouchere System")
            return
        run_simulation(strategy_func, args.bankroll, args.sequence, max_spins=args.max_spins)
    elif strategy.startswith('fibo'):
        run_simulation(strategy_func, args.bankroll, args.initial_bet, max_spins=args.max_spins)
    elif strategy.startswith('osca'):
        run_simulation(strategy_func, args.bankroll, args.initial_bet, max_spins=args.max_spins)
    else:
        print("Something went wrong!")

if __name__ == "__main__":
    main()


from datetime import datetime
import Player

class Result:
    def __init__(self, date: datetime, place: int, player: Player, id: int = None):
        self.id = id
        self.date = date
        self.place = place
        self.player = player


"""
The version of the package can be returned as a single string or a dict.

When a string, it comes from the package __version__.
When a dict, it also has __version__,
as well as versions of other depdency packages.
"""

from typing import Optional

import dls_mainiac_lib.version
import dls_servbase_lib.version
import dls_utilpack.version

from soakdb3_lib import __version__

# ----------------------------------------------------------
def version() -> str:
    """
    Version of the soakdb3 package as a string.
    """

    return __version__

# ----------------------------------------------------------
def meta(given_meta: Optional[dict] = None) -> dict:
    """
    Returns version information from the soakdb3 package
    and its dependencies as a dict.
    Adds version information to a given meta dict if it was provided.
    """

    meta = {}
    meta["soakdb3_lib"] = version()
    meta.update(dls_servbase_lib.version.meta())
    meta.update(dls_mainiac_lib.version.meta())
    meta.update(dls_utilpack.version.meta())

    if given_meta is not None:
        given_meta.update(meta)
    else:
        given_meta = meta
    return given_meta


import pygame, sys, math, random, time, Object
pygame.init()

width = 1000
height = 1000
size = width, height
fps = 60

bgColor = (83,174,232)

screen = pygame.display.set_mode(size)
icon = pygame.image.load('icon.png')
pygame.display.set_caption("Ball Demo")
pygame.display.set_icon(icon)
clock = pygame.time.Clock()
Object.setRes(width, height)
#Object.Object(Surface, xVel, yVel, img, initPos, weight)
ball1 = Object.Object(screen,6,2,"ball.png",(300,300),1)
ball2 = Object.Object(screen,1,5,"ball2.png",(50,50),2)
square1 = Object.Object(screen,6,5,"square1.png",(100,100),3)
square2 = Object.Object(screen,9,4,"square2.png",(500,500),4)
penta1 = Object.Object(screen,3,4,"penta1.png",(600,600),5)

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            exit();

    #Objects
    screen.fill(bgColor)
    objects = Object.getObjects()
    for obj in objects:
        obj.move()

    #Pygame
    clock.tick(fps)
    pygame.display.flip()


"""
Created on Tue Feb 28 11:44:11 2023.

@author: Vishal Philip

This script consists of functions for plotting line charts,
bar charts and pie charts using matplotlib library.
It also reads data from excel files using pandas.

"""

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

def line_plot(x_axis, y_axis, name, col):
    """
    Create Function to plot a line chart.

    Parameters :
        x_axis : The x-axis values for the plot.
        y_axis : The y-axis values for the plot.
        name (str): label the name of the the country to plot.
        color (str): The color of the plot.
    Returns The plot representing the line chart

    """
    return plt.plot(x_axis, y_axis, '.-', label=name, color=col)

def bar_plot(ypos, Year_data, a):
    """
    Create a function to plot bar chart.

    Parameters :
       ypos  : Name of the countries.
       year_data : The height of each bar.
       a : The width of each bar.
    Returns the plot representing the bar chart.

    """
    plt.figure(figsize=(10, 5))
    plt.xticks(ypos, Countries_data)
    return plt.bar(ypos, Year_data, width=a)

def plot_pie(Pie_Data, Pie_label):
    """
    Create a function for plotting pie chart.

    Parameters :
        Pie_data : The values to be plotted as slices of the pie chart.
        Pie_labels : The labels for each slice of the pie chart.

    Returns plot object representing the pie chart.

    """
    plt.figure(figsize=(10, 9))
    return plt.pie(Pie_Data, labels=Pie_label, autopct='%1.1f%%')

# Reading data from excel file

data1 = pd.read_excel("Mobile Cellular Subscription per 100.xlsx")

# dropping unwanted data

data2 = data1.drop(5, axis=0)

# Plotting line chart using the function line_plot

plt.figure()

# Calling function line_plot

line_plot(data2.Year, data2.Afghanistan, "Afghanistan", "red")
line_plot(data2.Year, data2.Bangladesh, "Bangladesh", "blue")
line_plot(data2.Year, data2.Bhutan, "Bhutan", "green")
line_plot(data2.Year, data2.India, "India", "black")
line_plot(data2.Year, data2.Nepal, "Nepal", "yellow")
line_plot(data2.Year, data2.Pakistan, "Pakistan", "orange")
line_plot(data2.Year, data2.SriLanka, "SriLanka", "violet")

# Creating labels

plt.xlabel("Years", size=15)
plt.ylabel("Subscription per 100 people", size=13)

# Creating Title

plt.title("Mobile Cellular Subscription of South Asian Countries")
plt.legend(fontsize=8)

# Saving image

plt.savefig("lineplot.png")
plt.show()

# Plotting bar graph using the function bar_plot
# Reading data from excel file

newdata = pd.read_excel("Mobile Cellular Subscription Copy.xlsx")
Countries_data = newdata.Countries
ypos = np.arange(len(Countries_data))
Year_data = newdata.Year_2005

# Calling Function bar_plot

bar_plot(ypos, Year_data, 0.5)

# Creating labels

plt.xlabel("Countries", size=15)
plt.ylabel("Subscription per 100 People", size=13)

# Creating Title

plt.title("Mobile Cellular Subscription of South Asian Countries in 2005")

# Saving image
plt.savefig("bargraph1.png")
plt.show()

# Plotting pie chart using the function plot_pie

pie_data = pd.read_excel("ForestArea.xlsx")
Countries_name = pie_data.Countries
Forest_Data2015 = pie_data.Year_2015
Forest_Data2005 = pie_data.Year_2005

# Calling Function plot_pie to create pie year for 2005

plot_pie(Forest_Data2015, Countries_name)

# Creating Title

plt.title("Forest Area of South Asian Countries (2015)", size=20)
plt.legend()

# Saveing fig

plt.savefig("piechart1.png")

# Calling Function plot_pie to create pie year for 2015

plot_pie(Forest_Data2005, Countries_name)

# Creating Title

plt.title("Forest Area of South Asian Countries (2005)", size=20)
plt.legend()

# Saving Image

plt.savefig("piechart2.png")
plt.show()


import requests
import json
import re

url = "http://www.juzimi.com/article/%E6%83%85%E4%B9%A6"
headers = {
    "User-Agent": "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.139 Safari/537.36",
    "Referer": "http://www.juzimi.com/search/node/%E5%82%B2%E6%85%A2%E4%B8%8E%E5%81%8F%E8%A7%81%20type%3Asentence",
}

def get_html(params):
    try:
        response = requests.get(url, params=params, headers=headers)
        response.raise_for_status()
        print(response.url)
        print(response.status_code)
    except Exception as e:
        print(e)
        print("connection error")
        return None

    response.encoding = response.apparent_encoding
    return response.text

def parse_html(html):
    data = {}

    sentences = re.findall(r'\"xlistju\">(.+?)', html)
    loveds = re.findall(r"(喜欢\(\d*?\))", html)
    comments = re.findall(r">(评论\(?\d*?\)?)", html)

    with open("love_letter.txt", "a", encoding="utf-8") as ob:
        for i in range(len(sentences)):
            data["sentence"] = sentences[i]
            data["loved"] = loveds[i]
            data["comment"] = comments[i]

            ob.write(json.dumps(data, ensure_ascii=False))
            ob.write("\n")

def main():
    for i in range(4):
        if i:
            params = {"page":i}
        else:
            params ={}
        html = get_html(params)
        if html:
            parse_html(html)
    print("结束")

if __name__ == "__main__":
    main()


import getChannelInfo
from flask import Flask, request, url_for, redirect, render_template

app = Flask(__name__)

@app.route("/", methods=["POST","GET"])
def home():
    if request.method == "POST":
        return redirect(url_for("results", youtubeUrl = request.form["url"].split('.com/')[-1]))
    else:
        return render_template("home.html")

@app.route("/<youtubeUrl>/", methods=["POST","GET"])
def results(youtubeUrl):
    if request.method == "POST":
        return redirect(url_for("results", youtubeUrl = request.form["url"].split('.com/')[-1]))
    elif 'watch?v=' in youtubeUrl:
        channelInfos = getChannelInfo.getChannelInfo(youtubeUrl)
        return render_template("result.html", channelNameToHtml = channelInfos[0], channelViewsToHtml = channelInfos[1],
                                              channelSubsToHtml = channelInfos[2], channelNumOfVideosToHtml = channelInfos[3])
    else:
        return redirect(url_for("wrongUrl", channelNameToHtml = youtubeUrl))

@app.route("/erro/", methods=["POST", "GET"])
def wrongUrl():
    if request.method == "POST":
        return redirect(url_for("results", youtubeUrl = request.form["url"].split('.com/')[-1]))
    else:
        return render_template("erro.html")

if __name__ == "__main__":
    app.run(debug=True)


# Распознавание лиц с помощью нейронных сетей
# Необходимо установить следующие компоненты:
# pip install git+https://github.com/rcmalli/keras-vggface.git
# pip install mtcnn
# pip install keras-applications
# Заменить строку на from keras.utils.layer_utils import get_source_inputs
# В файле keras_vggface/models.py
from mtcnn import MTCNN
from matplotlib import pyplot
from PIL import Image
import numpy as np
from keras_vggface.vggface import VGGFace
from keras_vggface.utils import preprocess_input
from scipy.spatial.distance import cosine

def extract_faces(filename, required_size=(224, 224)):
    # load image from file
    pixels = pyplot.imread(filename)

    # create the detector, using default weights
    detector = MTCNN()
    # detect faces in the image
    results = detector.detect_faces(pixels)
    # extract the bounding box from the first face
    face_array = []
    for result in results:
        x1, y1, width, height = result['box']
        x2, y2 = x1 + width, y1 + height
        # extract the face
        face = pixels[y1:y2, x1:x2]

        # resize pixels to the model size
        image = Image.fromarray(face)
        image = image.resize((224, 224))
        face_array.append(np.asarray(image))
    return face_array

# load the photo and extract the face
pixels = extract_faces('buntov.jpg')
# plot the extracted face
pyplot.imshow(pixels[1])
# show the plot
pyplot.show()

# extract faces and calculate face embeddings for a list of photo files
def get_embeddings(filename):
    # extract faces
    faces = extract_faces(filename)
    # convert into an array of samples
    samples = np.asarray(faces, 'float32')
    # prepare the face for the model, e.g. center pixels
    samples = preprocess_input(samples, version=2)
    # create a vggface model
    model = VGGFace(model='resnet50', include_top=False, input_shape=(224, 224, 3), pooling='avg')
    # perform prediction
    yhat = model.predict(samples)
    return yhat

# determine if a candidate face is a match for a known face
def is_match(known_embedding, candidate_embedding, thresh=0.5):
    # calculate distance between embeddings
    score = cosine(known_embedding, candidate_embedding)
    if score <= thresh:
        print('>face is a Match (%.3f <= %.3f)' % (score, thresh))
    else:
        print('>face is NOT a Match (%.3f > %.3f)' % (score, thresh))

# define filenames
filename = 'buntov.jpg'
# get embeddings file filenames
embeddings = get_embeddings(filename)
# define sharon stone
buntov_id = get_embeddings('gt_db/buntov/Buntov.jpg')[0]
# verify known photos of sharon
print('Tests')
for embedding in embeddings:
    is_match(embedding, buntov_id)


import os
import lxml
import shutil
import requests as re
import astropy.units as u
from astroquery.vizier import Vizier
from astropy.io import fits
from astropy.coordinates import SkyCoord
from bs4 import BeautifulSoup

# Create the download file directory.
data_dir = os.path.expanduser('~/astro_data/')
if not os.path.exists(data_dir):
    os.mkdir(data_dir)

# Create the data dictionary
dic_data = {
    'dss_image': {
        'format': 'fits',
        'type': 'image',
        'filename': 'dss_image.fits',
        'description': 'It will get the DSS image, RA: 01 02 03.3, DEC: +00 01 02.2, height & width: 30 arcmin'
    },
    'm4': {
        'format': 'fits',
        'type': 'catelog',
        'filename': 'm4_tmc.fits',
        'description': 'It will get m4 '
    }
}

def get_dss_image():
    '''
    example of DSS URL
    https://archive.stsci.edu/cgi-bin/dss_search?
        v=poss2ukstu_red&   => type of data (poss2, red for instance)
        r=01%3A02%3A03.3&   => ra in hex decimal
        d=%2B00%3A01%3A02.2& => dec in hex decimal
        e=J2000&             => epoch (J2000 for instance)
        h=30&                => height of the image (30 arcmin)
        w=30&                => width of the image (30 arcmin)
        f=fits&              => types of the image (fits or gif)
        c=none&              => compress
        s=on&
        fov=NONE&
        v3=
    '''
    ## Download digital sky survey fits.
    urlroot = 'https://archive.stsci.edu/cgi-bin/dss_search?'
    v = 'poss2ukstu_red'
    e = 'J2000'
    ra = str(1) + '%3A' + str(2) + '%3A' + str(03.3)
    de = '%2B' + str(0) + '%3A' + str(1) + '%3A' + str(02.2)
    h = str(10)
    w = str(10)
    f = 'fits'
    urlend = '&c=none&s=on&fov=NONE&v3='
    result = re.get(urlroot+'v='+v+'&r='+ra+'&d='+de+'&e='+e+'&h='+h+'&w='+w+'&f='+f+urlend, stream=True)

    with open(data_dir + 'dss_image.fits', 'wb') as out_file:
        shutil.copyfileobj(result.raw, out_file)
    del result

def get_m4():
    ## Query on vizier and save to FITS bin table
    # Query setup
    Vizier.ROW_LIMIT = -1
    target = 'M4' # Another name: M4
    r = 0.01

    # Making query
    # 2MASS catalog
    try:
        tmc = Vizier.query_region(target, radius = r*u.deg, catalog = 'II/246/out')
    except:
        payloads = {'-r': (None, 'SNV'), '-name': (None, target)}
        results = re.post('http://cds.u-strasbg.fr/cgi-bin/Sesame', data = payloads)
        soup = BeautifulSoup(results.text, 'lxml')
        pos = SkyCoord(soup.find_all('pre')[0].string.split('±')[0], frame='icrs', unit=(u.hourangle, u.deg))
        tmc = Vizier.query_region(pos, radius=r*u.deg, catalog='II/246/out')

    # PPMXL proper motion
    #ppmxl = Vizier.query_region(target, radius=r*u.deg, catalog='I/317/sample')

    # Get catalog only
    jhk = tmc[0]
    #pm = ppmxl[0]

    # Retrive data only from 2MASS columns [3,4,9,11,13,15,17,19,21]
    col1 = fits.Column(name=jhk['Jmag'].name, format=jhk['Jmag'].dtype, array=jhk['Jmag'])
    col2 = fits.Column(name=jhk['e_Jmag'].name, format=jhk['e_Jmag'].dtype, array=jhk['e_Jmag'])
    col3 = fits.Column(name=jhk['Hmag'].name, format=jhk['Hmag'].dtype, array=jhk['Hmag'])
    col4 = fits.Column(name=jhk['e_Hmag'].name, format=jhk['e_Hmag'].dtype, array=jhk['e_Hmag'])
    col5 = fits.Column(name=jhk['Kmag'].name, format=jhk['Kmag'].dtype, array=jhk['Kmag'])
    col6 = fits.Column(name=jhk['e_Kmag'].name, format=jhk['e_Kmag'].dtype, array=jhk['e_Kmag'])
    col7 = fits.Column(name=jhk['Qflg'].name, format=jhk['Qflg'].dtype, array=jhk['Qflg'])
    cols = fits.ColDefs([col1, col2, col3, col4, col5, col6, col7])
    tmc_hdu = fits.BinTableHDU.from_columns(cols)
    tmc_hdu.writeto(data_dir + target + '_tmc.fits', overwrite=True)


import pickle
import torch
import os

from ood_samplefree.datasets import __DATASETS__, get_transform

from ood_samplefree.architectures import __ARCHITECTURES__, \
    __ARCHITECTURES_224__
from ood_samplefree.experiments import OODStreamerWithSummaries

NUM_WORKER = 4
BATCH_SIZE = 256

def run(dataset, architecture, network_path, base_db, random_state):
    excluded = "in-dms:in-dss:in-dms-aos:in-nota:nota:proj+".split(":")

    # =========================== PATH =============================== #
    latent_folder = os.path.expanduser("~/data/latent/{}_{}_{}_{}"
                                       "".format(base_db, architecture,
                                                 random_state, dataset))

    folder = os.path.expanduser("~/data/ood_samplefree/")
    uniform_path = os.path.join(folder, "{}_{}_{}_uniform_full.npy"
                                        "".format(base_db,
                                                  architecture,
                                                  random_state))

    result_path = os.path.expanduser("~/data/results/{}_{}_{}_{}"
                                     "".format(base_db, architecture,
                                               random_state, dataset))

    # ============================= RAND ================================= #
    cuda = torch.cuda.is_available()
    torch.manual_seed(random_state + 42)
    if cuda:
        torch.cuda.manual_seed(random_state + 101)

    # =========================== ARCHITECTURES =============================== #
    ARCHITECTURES = __ARCHITECTURES__

    if architecture.endswith("_small"):
        ARCHITECTURES = __ARCHITECTURES__
    if architecture.endswith("_large"):
        ARCHITECTURES = __ARCHITECTURES_224__

    # ============================== MODELS ============================== #
    print("Loading model from", network_path)

    id_db_factory = __DATASETS__[base_db]
    print(id_db_factory, id_db_factory.get_default_n_outputs())
    model = ARCHITECTURES[architecture.split("_")[0]](
        n_outputs=id_db_factory.get_default_n_outputs())

    model.load_state_dict(torch.load(network_path, map_location="cpu"))
    model.eval()

    # =========================== DATA =================================== #
    id_db = id_db_factory(shape=model.input_size)

    actual_db = base_db if dataset in {"train", "val", "test"} else dataset
    whole_dataset = __DATASETS__[actual_db].same_as(id_db)

    train_set, val_set, test_set = whole_dataset.get_ls_vs_ts()

    if dataset == "train":
        investigated_set = train_set
    elif dataset == "val":
        investigated_set = val_set
    elif dataset == "test":
        investigated_set = test_set
    else:
        investigated_set = torch.utils.data.ConcatDataset([train_set,
                                                           val_set,
                                                           test_set])

    # ============================= COMPUTATION ================================ #
    streamer = OODStreamerWithSummaries.from_uniform(id_db,
                                                     excluded=excluded,
                                                     ref_save_path=uniform_path,
                                                     num_workers=NUM_WORKER,
                                                     batch_size=BATCH_SIZE)

    whole_cache = None
    for completion, cache in streamer.stream(model, investigated_set,
                                             latent_folder,
                                             batch_size=BATCH_SIZE,
                                             num_workers=NUM_WORKER):
        whole_cache = cache
        # `completion` hook to display progress

    if whole_cache is None:
        raise RuntimeError("Nothing to save.")

    # ============================= RESULTS ================================ #
    collector = {}
    for key, value in whole_cache:
        collector[key] = value

    with open(result_path, "wb") as hdl:
        pickle.dump(collector, hdl, protocol=pickle.HIGHEST_PROTOCOL)

if __name__ == '__main__':
    import argparse
    parser = argparse.ArgumentParser()
    parser.add_argument("dataset", help="OOD dataset")
    parser.add_argument("architecture", help="Name of the architecture")
    parser.add_argument("network_path", help="Path to the trained model")
    parser.add_argument("--base_db", help="ID dataset", default="cifar10")
    parser.add_argument("--random_state", help="ID dataset", default=0, type=int)

    args = parser.parse_args()

    run(args.dataset, args.architecture, args.network_path, args.base_db, args.random_state)


"""create table vulnerabilities

Revision ID: 7c34e9a89bc0
Revises: b21ffaeed8e4
Create Date: 2016-06-29 10:11:36.057723

"""

# revision identifiers, used by Alembic.
revision = '7c34e9a89bc0'
down_revision = 'b21ffaeed8e4'
branch_labels = None
depends_on = None

from alembic import op
import sqlalchemy as sa
import datetime

def _get_date():
  return datetime.datetime.now()

def upgrade():
  op.create_table('vulnerabilities',
                  sa.Column('id', sa.Integer, primary_key=True, nullable=False),
                  sa.Column('name', sa.Text, nullable=False),
                  sa.Column('cvss_score', sa.Float, nullable=False),
                  sa.Column('bug_id', sa.Text),
                  sa.Column('family', sa.Text),
                  sa.Column('cve_id', sa.Text),
                  sa.Column('inventory_host_id', sa.Integer, sa.ForeignKey('inventory_hosts.id', ondelete='cascade')),
                  sa.Column('port', sa.Text),
                  sa.Column('threat_score', sa.Text),
                  sa.Column('severity_score', sa.Float),
                  sa.Column('xrefs', sa.Text),
                  sa.Column('tags', sa.Text),
                  sa.Column('validated', sa.BOOLEAN),
                  sa.Column('created_at', sa.TIMESTAMP(timezone=False), default=_get_date))

def downgrade():
  op.drop_table('vulnerabilities')


'''Created on 2021-11-03

@author: Gabriel Bailey

This code will solve Burger's equation for a given equation using the leapfrog method. It will produce 4 graphs of the equation at different times, and also an animated graph over time if desired.
'''
# Started from laplace.py

from numpy import pi,zeros, sin,linspace
import matplotlib.pyplot as plt

# Constants
epsilon = 1      # Target accuracy
delta_x = 0.02      #
delta_t = 0.005     #
Lx = 2*pi
Tf = 2

Nx = int(Lx//delta_x)
Nt = int(Tf//delta_t)
# Create arrays to hold potential values
u = zeros([Nx, Nt],float)
# Set initial conditions
u[0,:] = 0
u[Nx-1,:] = 0
for x in range(1,Nx-1):
    u[x,0] = sin(x*delta_x)
    # Also forward Euler step:
    u[x,1] = u[x,0] - epsilon*delta_t/(4*delta_x)*(u[x+1,0]**2-u[x-1,0]**2)

# Fill out the rest of the points using leapfrog
beta = epsilon*delta_t/delta_x
for t in range(1,Nt-1):
    for x in range(1,Nx-1):
        u[x,t+1] = u[x,t-1] - beta/2*(u[x+1,t]**2-u[x-1,t]**2)
        '''try:
            u[x,t+1] = u[x,t-1] - beta/2*(u[x+1,t]**2-u[x-1,t]**2)
        except:
            print(t,x,u[x,t-1],u[x+1,t],u[x-1,t])'''

#This takes the array of values at x,t and the target time and produces a plot for that time.
def plot_result(u,time):
    t = int(time/Tf * Nt)
    #print(t)
    fig = plt.figure(figsize=[10,5])
    ax = fig.add_subplot(1,1,1)
    ax.set_xlim(0, Lx)
    ax.set_ylim(-1.6, 1.6)
    x = linspace(0, Lx, Nx)
    ax.plot(x,u[:,t])

    ax.grid(which='both', axis='y')
    ax.set_title('Burger\'s Equation at Time {}s'.format(time))
    ax.set_xlabel('x')
    ax.set_ylabel('u')
    return ax

# Plot the 4 times
plot_result(u,0.0)
plot_result(u,0.5)
plot_result(u,1.0)
plot_result(u,1.5)
plt.show()

# Plot it as an animation:
""" from matplotlib.animation import FuncAnimation, FFMpegWriter

fig, ax = plt.subplots()
ax.grid(which='both', axis='y')
ax.set_title('Burger\'s Equation')
ax.set_xlabel('x')
ax.set_ylabel('Value')
xdata, ydata = [], []
line, = plt.plot([], [])
time_text = ax.text(0.1, 0.1, 'time = ', transform=ax.transAxes)

def init():
    ax.set_xlim(0, 2*pi)
    ax.set_ylim(-2, 2)
    time_text.set_text('time = ')
    return line,

def update(i):
    i = int(i)
    x = linspace(0, Lx, Nx)
    y = u[:,i]
    line.set_data(x, y)
    time_text.set_text('time = {:1.2f}'.format(i*delta_t))
    return line, time_text

ani = FuncAnimation(fig, update, frames=Nt, interval=1,
                    init_func=init, blit=True)

# Save the animation (need ffmpeg)
# f = 'Q3_vid.mp4'
# writervideo = FFMpegWriter(fps=60)
# ani.save(f, writer=writervideo)

plt.show() """


#!/usr/bin/env python                                                          #
#                                                                              #
# Autor: Milena Crnogorcevic                                                   #
# Date created: 7/22/2019                                                      #
# Last edit: 10/20/2020                                                        #
# Use: Fitting the ALP model to the simulated spectra.                         #
#------------------------------------------------------------------------------#
# Example to run the script (adjust the paths to the SED, energy, bkg, fak, and rsp):
# python ALP_fit.py

# Importing the necessary packages
import numpy as np
import argparse
from scipy.interpolate import interp1d

# importing XSPEC
import xspec as xs
xs.Xset.allowPrompting = False #keeps pyxspec from hanging, waiting a response to a prompt
xs.Xset.allowNewAttributes = True

__description__ = 'Fit ALP model for different normalization values for a given response and background.'

N = np.logspace(np.log10(8.4e-8), np.log10(8.4e2), 30) # normalization values, [1e-52 cm^-2]

E_LLE = np.load(args.emev) # LLE energy bins, [MeV]
SED = np.load(args.sed) # the output of Manuel's code, N = 1 cm^-2

f = interp1d(E_LLE*1000, SED/1000, 'cubic', fill_value="extrapolate") # in keV and per keV

# defining Xspec model
def ALP(engs, params, flux):
    energy_binsizes = np.ediff1d(engs)
    # finding the average bin value for energy
    energy = (np.asarray(engs[:-1])+np.asarray(engs[1:]))/2
    flux[:] = f(energy) * energy_binsizes

ALPInfo = ()
xs.AllModels.addPyMod(ALP, ALPInfo, 'add', spectrumDependent=False)

normALP = np.zeros((30,2001))
chi_stat = np.zeros((30,2001))
Fit_st = np.zeros((30,2001))

xs.AllData.clear()   # clear all data, if any.

for j in range(30):
    for i in range(1,2001):
        xs.AllData.clear()
        s=xs.Spectrum("bn150416773_fakeit_%s.fak{%s}" %(j,i))
        s.response = "bn150416773_LAT-LLE_weightedrsp.rsp"
        s.background= ("bn150416773_LAT-LLE_bkgspectra.bak{1}")
        xs.AllModels.clear()
        m=xs.Model("ALP")
        xs.Fit.statMethod = "pgstat"
        #List of value floats [val,delta,min,bot,top,max].
        #m.ALP.norm.values=[N[j], N[j]*0.1, N[j]*0.1, N[j]*0.95, N[j]*1.05, N[j]*1.95]
        m.ALP.norm.frozen = False
        xs.Fit.perform()
        normALP[j,i] = m.ALP.norm.values[0]
        chi_stat[j,i] = xs.Fit.testStatistic
        Fit_st[j,i] = xs.Fit.statistic

    np.save('TS_pgfit_ALP.npy', chi_stat)
    np.save('pgfit_ALP.npy', Fit_st)
    np.save('norm_ALP.npy', normALP)


from functools import reduce
from operator import concat

# ===================================================
# utils
# ===================================================

def select_keys(d: dict, ks: list) -> dict:
    return {k: d[k] for k in ks if k in d}

# ===================================================
# API
# ===================================================

def produce_api_calls(intervals: list) -> list:
    intervals = [select_keys(x, ['vessel', 'start', 'end']) for x in intervals]
    intervals = sorted(intervals, key=lambda x: (x['vessel'], x['start']))
    v_to_intervals = {}
    for interval in intervals:
        vessel, start2, end2 = interval.get('vessel'), interval.get('start'), interval.get('end')
        cur_intervals = v_to_intervals.get(vessel, [])
        if not cur_intervals or cur_intervals[-1].get('end') < start2:
            # empty or no_overlap (end1<start2): add interval
            cur_intervals.append(interval)
            v_to_intervals[vessel] = cur_intervals
        else:
            # overlaps (start2 <= end1): update cur interval with [start1, max(end1, end2)]
            v_to_intervals[vessel][-1]['end'] = max(cur_intervals[-1].get('end'), end2)
    return list(reduce(concat, v_to_intervals.values()))


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Thu May 16 13:29:10 2019

@author: phoebeharmon
"""

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Thu May  9 14:10:19 2019

@author: phoebeharmon
"""

import speech_recognition as sr
print(sr.__version__)

#Recognizer instance recongizes speech from an audio source
#create Recognizer instance
r = sr.Recognizer()

#7 methods to recognize speech from audio source
#only recognize_sphinx() works without internet connection
#default key provided for recognize_google() aka google web speech api
#r.recognize_google()

#AudioFile class initialized with path to an audio file
harvard = sr.AudioFile('harvard.wav')
with harvard as source: #context manager opens file, reads contents, stores data in AudioFile instance called source
    audio = r.record(source) #records data from file into AudioData instance
type(audio)

message = r.recognize_google(audio)
print(message)

#capture portion of speech in file
with harvard as source:
    audio1 = r.record(source, duration=4) #first 4 seconds of file
    audio2 = r.record(source, duration=4) #next 4 seconds of file
with harvard as source:
    audio3 = r.record(source, offset=4, duration=3) #records 3 seconds started at 4 seconds
message_4sec = r.recognize_google(audio)
print(message_4sec)

#deal with noise
with harvard as source:
    r.adjust_for_ambient_noise(source, duration=.5)
    audio = r.record(source)

print(r.recognize_google(audio, show_all = True)) #returns all possible transcripts


import rclpy
from rclpy.node import Node
from sensor_msgs.msg import Image
import cv2
from cv_bridge import CvBridge

class CameraSubscriber(Node):
    def __init__(self):
        super().__init__("camera_subscriber")
        self.create_subscription(Image, "/wamv/sensors/cameras/front_left_camera_sensor/optical/image_raw", self.callback, 10)

    def callback(self, data: Image):
        self.camera_output = CvBridge().imgmsg_to_cv2(data, "bgr8")
        cv2.imshow("boat_output", self.camera_output)
        cv2.waitKey(1)

def main(args=None):
    rclpy.init(args=args)
    cam_sub = CameraSubscriber()
    rclpy.spin(cam_sub)
    cam_sub.destroy_node()
    rclpy.shutdown()

if __name__ == "__main__":
    main()


from itertools import compress

import numpy as np

from .object_intersect import partition, get_ob_2dbboxes, get_vert_co_2d, get_ob_loc_co_2d, do_selection
from .polygon_tests import point_inside_rectangles, points_inside_rectangle, segments_intersect_rectangle

def get_obs_mask_in_selbox(obs, obs_mask_check, depsgraph, region, rv3d, xmin, xmax, ymin, ymax):
    list_of_obs_to_check = compress(obs, obs_mask_check)
    bool_list = []

    for ob in list_of_obs_to_check:
        ob_eval = ob.evaluated_get(depsgraph)
        me = ob_eval.to_mesh(preserve_all_data_layers=False, depsgraph=None)
        vert_co_2d = get_vert_co_2d(me, ob_eval, region, rv3d)
        verts_mask_in_selbox = points_inside_rectangle(vert_co_2d, xmin, xmax, ymin, ymax)
        if np.all(verts_mask_in_selbox):
            bool_list.append(True)
        else:
            bool_list.append(False)
        ob_eval.to_mesh_clear()

    bools = np.fromiter(bool_list, "?", len(bool_list))
    return bools

def select_obs_in_box(context, mode, xmin, xmax, ymin, ymax, behavior):
    region = context.region
    rv3d = context.region_data
    depsgraph = context.evaluated_depsgraph_get()

    selectable_obs = context.selectable_objects

    if behavior == 'CONTAIN':
        mesh_obs, nonmesh_obs = partition(selectable_obs, lambda o: o.type in {'MESH', 'CURVE', 'FONT'})
        mesh_ob_count = len(mesh_obs)

        # Get coordinates of 2d bounding boxes of objects.
        (
            ob_2dbbox_xmin,
            ob_2dbbox_xmax,
            ob_2dbbox_ymin,
            ob_2dbbox_ymax,
            ob_2dbbox_points,
            ob_2dbbox_segments,
            obs_mask_2dbbox_entire_clip,
        ) = get_ob_2dbboxes(mesh_obs, mesh_ob_count, region, rv3d)

        # Check for bounding boxes intersections with selection box.
        # Speed up finding overlaps or intersections by doing polygon tests on bounding boxes.

        # Ob bbox intersects selection box.
        segment_bools = segments_intersect_rectangle(ob_2dbbox_segments, xmin, xmax, ymin, ymax, prefilter=True)
        segment_bools.shape = (mesh_ob_count, 4)
        obs_mask_2dbbox_isect_selbox = np.any(segment_bools, axis=1)

        # Ob bbox bbox entirely inside selection box.
        point_bools = points_inside_rectangle(ob_2dbbox_points, xmin, xmax, ymin, ymax)
        point_bools.shape = (mesh_ob_count, 4)
        obs_mask_2dbbox_entire_in_selbox = np.all(point_bools, axis=1)

        # Cursor is inside ob bbox.
        obs_mask_cursor_in_2dbbox = point_inside_rectangles(
            (xmin, ymin), ob_2dbbox_xmin, ob_2dbbox_xmax, ob_2dbbox_ymin, ob_2dbbox_ymax
        )

        obs_mask_dont_check = obs_mask_2dbbox_entire_in_selbox | obs_mask_2dbbox_entire_clip
        obs_mask_check = (
            obs_mask_2dbbox_isect_selbox | (obs_mask_cursor_in_2dbbox & ~obs_mask_2dbbox_isect_selbox)
        ) & ~obs_mask_dont_check

        mesh_obs_mask_in_selbox = obs_mask_2dbbox_entire_in_selbox
        mesh_obs_mask_in_selbox[obs_mask_check] = get_obs_mask_in_selbox(
            mesh_obs, obs_mask_check, depsgraph, region, rv3d, xmin, xmax, ymin, ymax
        )
        do_selection(mesh_obs_mask_in_selbox, mesh_obs, mode)

        nonmesh_ob_co_2d = get_ob_loc_co_2d(nonmesh_obs, region, rv3d)
        nonmesh_obs_mask_in_selbox = points_inside_rectangle(nonmesh_ob_co_2d, xmin, xmax, ymin, ymax)
        do_selection(nonmesh_obs_mask_in_selbox, nonmesh_obs, mode)

    if behavior == 'ORIGIN':
        ob_co_2d = get_ob_loc_co_2d(selectable_obs, region, rv3d)
        obs_mask_in_selbox = points_inside_rectangle(ob_co_2d, xmin, xmax, ymin, ymax)
        do_selection(obs_mask_in_selbox, selectable_obs, mode)