# ---------------------------------------------------------------
# transformer_croattn_relcaption_head.py
# Set-up time: 2021/3/11 10:39
# Copyright (c) 2020 ICT
# Licensed under The MIT License [see LICENSE for details]
# Written by Kenneth-Wong (Wenbin-Wang) @ VIPL.ICT
# Contact: wenbin.wang@vipl.ict.ac.cn [OR] nkwangwenbin@gmail.com
# ---------------------------------------------------------------

from ..registry import HEADS
import torch
import torch.nn as nn
import torch.nn.functional as F
from .. import builder
from ..losses import accuracy
from mmdet.datasets import build_dataset
import numpy as np
import mmcv

from .relational_caption_head import RelationalCaptionHead
from mmdet.models.captioners.utils import LowRankBilinearEncBlock, LowRankBilinearDecBlock, FeedForwardBlock
from mmdet.models.captioners.utils import activation, expand_tensor, decode_sequence
import nltk
import copy
import math
from .approaches import MultiHeadedAttention, PositionalEncoding, PositionwiseFeedForward, EncoderDecoder, Encoder, \
    EncoderLayer, Decoder, DecoderLayer, Embeddings, Generator, subsequent_mask

@HEADS.register_module
class TransformerCrossAttnRelationalCaptionHead(RelationalCaptionHead):
    def __init__(self, **kwargs):
        super(TransformerCrossAttnRelationalCaptionHead, self).__init__(**kwargs)

        self.att_dim = self.attention_feat_config.att_feats_embed_dim \
            if self.attention_feat_config.att_feats_embed_dim > 0 else self.attention_feat_config.att_feats_dim

        # union feat embed
        sequential = []
        if self.union_feat_config.union_feats_embed_dim > 0:
            sequential.append(
                nn.Linear(self.union_feat_config.union_feats_dim + self.union_feat_config.union_spatial_dim,
                          self.union_feat_config.union_feats_embed_dim))
        sequential.append(activation(self.union_feat_config.union_feats_embed_act,
                                     elu_alpha=self.captioner_config.elu_alpha))
        if self.union_feat_config.dropout_union_embed > 0:
            sequential.append(nn.Dropout(self.union_feat_config.dropout_union_embed))
        if self.union_feat_config.union_feats_norm:
            sequential.append(torch.nn.LayerNorm(self.union_feat_config.union_feats_embed_dim))
        self.union_embed = nn.Sequential(*sequential) if len(sequential) > 0 else None

        # attention feats embed
        sequential = []
        if self.attention_feat_config.att_feats_embed_dim > 0:
            sequential.append(nn.Linear(self.attention_feat_config.att_feats_dim,
                                        self.attention_feat_config.att_feats_embed_dim))
        sequential.append(activation(self.attention_feat_config.att_feats_embed_act,
                                     elu_alpha=self.captioner_config.elu_alpha))
        if self.attention_feat_config.dropout_att_embed > 0:
            sequential.append(nn.Dropout(self.attention_feat_config.dropout_att_embed))
        if self.attention_feat_config.att_feats_norm:
            sequential.append(torch.nn.LayerNorm(self.attention_feat_config.att_feats_embed_dim))
        self.att_embed = nn.Sequential(*sequential) if len(sequential) > 0 else None

        # encoder-decoder
        self.N_enc = self.captioner_config.N_enc
        self.N_dec = self.captioner_config.N_dec
        self.d_model = self.captioner_config.input_encoding_size  # 512
        self.d_ff = self.captioner_config.d_ff  # 512
        self.h = self.captioner_config.num_att_heads
        self.dropout = self.captioner_config.dropout  # 0.1

        self.model = self.make_model(0, self.vocab_size,
                                     N_enc=self.N_enc,
                                     N_dec=self.N_dec,
                                     d_model=self.d_model,
                                     d_ff=self.d_ff,
                                     h=self.h,
                                     dropout=self.dropout)

        # ipt estmiate
        if self.cross_attn:
            self.gather_attention = self.head_config.gather_attention
            self.ipt_estimate = self.head_config.ipt_estimate
            self.mask_obj_word = self.head_config.mask_obj_word
            self.soft_supervised = self.head_config.soft_supervised

            ipt_estm_dim = 512
            if self.ipt_estimate == 'so':
                input_dim = self.attention_feat_config.att_feats_embed_dim * 2
            elif self.ipt_estimate == 'sou':
                input_dim = self.attention_feat_config.att_feats_embed_dim * 2 + self.union_feat_config.union_feats_embed_dim
            elif self.ipt_estimate == 'u':
                input_dim = self.union_feat_config.union_feats_embed_dim
            elif self.ipt_estimate == 'sous':
                input_dim = self.attention_feat_config.att_feats_embed_dim * 2 \
                            + self.union_feat_config.union_feats_embed_dim + \
                            self.union_feat_config.union_feats_semantic_dim * 2
                self.semantic_embedding = nn.Embedding(self.num_classes,
                                                       self.union_feat_config.union_feats_semantic_dim)
            else:
                raise NotImplementedError
            self.global_feat_embed = nn.Sequential(*[
                nn.Linear(self.attention_feat_config.att_feats_embed_dim, ipt_estm_dim),
                activation('CeLU'),
                nn.Dropout(0.5),
                nn.LayerNorm(ipt_estm_dim)])
            self.local_feat_embed = nn.Sequential(*[nn.Linear(input_dim, ipt_estm_dim),
                                                    activation('CeLU'),
                                                    nn.Dropout(0.5),
                                                    nn.LayerNorm(ipt_estm_dim)])

            self.attn_loss_weight = self.head_config.attn_loss_weight

    def make_model(self, src_vocab, tgt_vocab, N_enc=6, N_dec=6,
                   d_model=512, d_ff=2048, h=8, dropout=0.1):
        "Helper: Construct a model from hyperparameters."
        c = copy.deepcopy
        attn = MultiHeadedAttention(h, d_model, dropout)
        ff = PositionwiseFeedForward(d_model, d_ff, dropout)
        position = PositionalEncoding(d_model, dropout)
        model = EncoderDecoder(
            Encoder(EncoderLayer(d_model, c(attn), c(ff), dropout), N_enc),
            Decoder(DecoderLayer(d_model, c(attn), c(attn),
                                 c(ff), dropout), N_dec),
            lambda x: x,
            nn.Sequential(Embeddings(d_model, tgt_vocab), c(position)),
            Generator(d_model, tgt_vocab))

        # This was important from their code.
        # Initialize parameters with Glorot / fan_avg.
        for p in model.parameters():
            if p.dim() > 1:
                nn.init.xavier_uniform_(p)
        return model

    def padded_att(self, roi_feats, num_rois):
        bs = len(num_rois)
        max_num = max(num_rois)
        num_atts = torch.LongTensor(num_rois).to(roi_feats.device)
        split_roi_feats = roi_feats.split(num_rois)
        padded_att_feats = torch.zeros(bs, max_num, roi_feats.size(-1)).to(roi_feats)
        for i in range(bs):
            padded_att_feats[i, :num_rois[i], :] = split_roi_feats[i][:, :]
        att_mask = torch.arange(0, max_num, device=roi_feats.device).long().unsqueeze(0).expand(bs, max_num).lt(
            num_atts.unsqueeze(1)).long()
        return padded_att_feats, att_mask

    def gather_att_region(self, padded_feats, num_rois, dim):
        return padded_feats.split(num_rois, dim=dim)

    def preproj_feats(self, roi_feats, union_feats):
        return self.att_embed(roi_feats), self.union_embed(union_feats)

    def preprocess_seq(self, input_seq, target_seq):
        # process the input_seq, target_seq for training
        input_seq = input_seq.view(-1, input_seq.size(-1))
        target_seq = target_seq.view(-1, target_seq.size(-1))
        seq_mask = (input_seq > 0).long()
        seq_mask[:, 0] += 1
        seq_mask_sum = seq_mask.sum(-1)
        max_len = int(seq_mask_sum.max())
        input_seq = input_seq[:, 0:max_len].contiguous()
        target_seq = target_seq[:, 0:max_len].contiguous()
        seq_mask = seq_mask[:, 0:max_len].contiguous()

        seq_mask = seq_mask.unsqueeze(-2)
        seq_mask = seq_mask & subsequent_mask(input_seq.size(-1)).to(seq_mask)
        return input_seq, target_seq, seq_mask

    def get_logprobs_state(self, state, wt, p_att_feats, att_mask):
        if len(state) == 0:
            ys = wt.unsqueeze(1)
        else:
            ys = torch.cat([state[0][0], wt.unsqueeze(1)], dim=1)
        out = self.model.decode(p_att_feats, att_mask, ys, subsequent_mask(ys.size(1)).to(p_att_feats.device))
        # use the last region-word atttention (ie, src attention) of the last decoder layer in the decoder, average
        # over the multi-heads (dimension 1),
        # the first time step: attn is 1 * num_head * 1 * num_region
        # the t time step: attn is 1 * num_head * t * num_region
        alpha = self.model.decoder.layers[-1].src_attn.attn.mean(1)[:, -1]
        return F.log_softmax(self.model.generator(out[:, -1])), [ys.unsqueeze(0)], alpha

    def init_cap_hidden(self, batch_size, device):
        return []

    def forward(self,
                img,
                img_meta,
                det_result,
                gt_result=None,
                is_testing=False,
                beam_size=3):
        """
        Obtain the relation prediction results based on detection results.
        Args:
            img (Tensor): of shape (N, C, H, W) encoding input images.
                Typically these should be mean centered and std scaled.

            img_meta (list[dict]): list of image info dict where each dict has:
                'img_shape', 'scale_factor', 'flip', and may also contain
                'filename', 'ori_shape', 'pad_shape', and 'img_norm_cfg'.
                For details on the values of these keys see
                `mmdet/datasets/pipelines/formatting.py:Collect`.
            det_result: (Result): Result containing bbox, label, mask, point, rels,
                etc. According to different mode, all the contents have been
                set correctly. Feel free to  use it.
            gt_result : (Result): The ground truth information.
            is_testing:

        Returns:
            det_result with the following newly added keys:
                refine_scores (list[Tensor]): logits of object
                rel_scores (list[Tensor]): logits of relation
                rel_pair_idxes (list[Tensor]): (num_rel, 2) index of subject and object
                relmaps (list[Tensor]): (num_obj, num_obj):
                target_rel_labels (list[Tensor]): the target relation label.
        """
        # roi_feats: N * 4,096; union_feats: N * 512
        roi_feats, union_feats, det_result = self.frontend_features(img, det_result, gt_result)
        if roi_feats.shape[0] == 0:
            return det_result

        num_rels = [r.shape[0] for r in det_result.rel_pair_idxes]
        num_objs = [len(b) for b in det_result.bboxes]

        num_images = img[0].size(0)
        assert num_images == len(det_result.rel_pair_idxes)
        rel_pair_index = []
        acc_obj = 0
        for i, num_obj in enumerate(num_objs):
            rel_pair_idx_i = det_result.rel_pair_idxes[i].clone()
            rel_pair_idx_i[:, 0] += acc_obj
            rel_pair_idx_i[:, 1] += acc_obj
            acc_obj += num_obj
            rel_pair_index.append(rel_pair_idx_i)
        rel_pair_index = torch.cat(rel_pair_index, 0)

        # pre encode: 512
        roi_feats, union_feats = self.preproj_feats(roi_feats, union_feats)

        # get global feats:
        split_roi_feats = roi_feats.detach().split(num_objs)
        if self.cross_attn:
            global_feats = []
            for i, srf in enumerate(split_roi_feats):
                global_feats += [self.global_feat_embed(torch.mean(srf, dim=0, keepdim=True))] * num_rels[i]
            global_feats = torch.cat(global_feats)

        # captioning part
        if self.with_caption:
            # padded feats and forward the encoder: each single roi
            padded_att_feats, att_mask = self.padded_att(roi_feats, num_objs)
            att_mask = att_mask.unsqueeze(-2)

            # training or val:
            if not is_testing:
                # training: each image have 5 captions, expand the visual features
                padded_att_feats = expand_tensor(padded_att_feats, self.seq_per_img)
                att_mask = expand_tensor(att_mask, self.seq_per_img)
                batch_size = padded_att_feats.size(0)

                cap_inputs, cap_targets = gt_result.cap_inputs, gt_result.cap_targets
                cap_inputs = torch.stack(cap_inputs)
                cap_targets = torch.stack(cap_targets)
                cap_inputs, cap_targets, seq_mask = self.preprocess_seq(cap_inputs, cap_targets)
                gt_result.cap_inputs = cap_inputs
                gt_result.cap_targets = cap_targets
                det_result.cap_targets = cap_targets
                out = self.model(padded_att_feats, cap_inputs, att_mask, seq_mask)
                cap_scores = self.model.generator(out)
                alphas = self.model.decoder.layers[-1].src_attn.attn.mean(1)  # do not need softmax
                det_result.cap_scores = cap_scores
            else:
                # beam search
                batch_size = padded_att_feats.size(0)
                # encode:
                memory = self.model.encode(padded_att_feats, att_mask)
                outputs, log_probs, alphas = self.decode_beam(beam_size, batch_size, padded_att_feats.device,
                                                              self.get_logprobs_state,
                                                              p_att_feats=memory,
                                                              att_mask=att_mask)
                det_result.cap_seqs = outputs
                det_result.cap_scores = log_probs

        # Relational Captioning Part
        if self.with_relcaption:
            roi_subj_feats, roi_obj_feats = roi_feats[rel_pair_index[:, 0], :], roi_feats[rel_pair_index[:, 1], :]
            # here the triplet features are used as the att feats in captions

            triplet_att_feats = torch.stack((roi_subj_feats, roi_obj_feats, union_feats), 1)  # N, 3, D
            batch_size = triplet_att_feats.size(0)
            num_rois = [3] * batch_size
            num_atts = torch.LongTensor(num_rois).to(triplet_att_feats.device)
            triplet_att_mask = torch.arange(0, 3, device=triplet_att_feats.device).long().unsqueeze(0).expand(
                batch_size, 3).lt(
                num_atts.unsqueeze(1)).long()
            triplet_att_mask = triplet_att_mask.unsqueeze(-2)

            if not is_testing:
                tgt_rel_inputs, tgt_rel_targets, tgt_rel_ipts = det_result.tgt_rel_cap_inputs, det_result.tgt_rel_cap_targets, det_result.tgt_rel_ipts
                tgt_rel_inputs = torch.cat(tgt_rel_inputs, 0)
                tgt_rel_targets = torch.cat(tgt_rel_targets, 0)

                assert tgt_rel_inputs is not None and tgt_rel_targets is not None
                tgt_rel_inputs, tgt_rel_targets, rel_seq_mask = self.preprocess_seq(tgt_rel_inputs, tgt_rel_targets)
                det_result.tgt_rel_cap_inputs = tgt_rel_inputs
                det_result.tgt_rel_cap_targets = tgt_rel_targets

                out = self.model(triplet_att_feats, tgt_rel_inputs, triplet_att_mask, rel_seq_mask)
                rel_cap_scores = self.model.generator(out)
                det_result.rel_cap_scores = rel_cap_scores

            else:
                # beam search
                # encode:
                triplet_memory = self.model.encode(triplet_att_feats, triplet_att_mask)
                outputs, log_probs, _ = self.decode_beam(beam_size, batch_size, roi_subj_feats.device,
                                                         self.get_logprobs_state,
                                                         att_mask=triplet_att_mask,
                                                         p_att_feats=triplet_memory)
                det_result.rel_cap_scores = log_probs
                det_result.rel_cap_seqs = outputs

        # cross attention part
        if self.cross_attn:
            assert self.with_caption and self.with_relcaption, 'No captioner! or no relational captioner!'

            # use the last hidden state to evaluate the rel_ipt_score:
            if self.ipt_estimate == 'so':
                ipt_estm_feats = torch.cat((roi_subj_feats.detach(), roi_obj_feats.detach()), -1)
            elif self.ipt_estimate == 'sou':
                ipt_estm_feats = torch.cat((roi_subj_feats.detach(), roi_obj_feats.detach(), union_feats.detach()), -1)
            elif self.ipt_estimate == 'u':
                ipt_estm_feats = union_feats.detach()
            elif self.ipt_estimate == 'sous':
                if self.mode == 'predcls':
                    labels = det_result.labels
                    if isinstance(labels, (list, tuple)):
                        labels = torch.cat(labels, 0)
                    else:
                        labels = labels.view(-1)
                    dist = torch.zeros(labels.size(0), self.num_classes).to(labels.device)
                    dist[torch.arange(labels.size(0)).to(dist.device).long(), labels] = 1
                else:
                    dist = torch.cat(det_result.dist, 0)
                semantic_feats = dist @ self.semantic_embedding.weight
                roi_subj_sem_feats, roi_obj_sem_feats = semantic_feats[rel_pair_index[:, 0], :], \
                                                        semantic_feats[rel_pair_index[:, 1]]
                ipt_estm_feats = torch.cat((roi_subj_feats.detach(), roi_obj_feats.detach(),
                                            union_feats.detach(), roi_subj_sem_feats, roi_obj_sem_feats), -1)
            else:
                raise NotImplementedError
            ipt_estm_feats = self.local_feat_embed(ipt_estm_feats)
            factor = np.sqrt(ipt_estm_feats.size(1))
            rel_ipt_scores = torch.sum(torch.mul(ipt_estm_feats, global_feats), -1) / factor
            rel_ipt_scores = rel_ipt_scores.split(num_rels)

            det_result.rel_ipt_scores = rel_ipt_scores

            if self.soft_supervised:
                # randomly select one caption and use its attention
                batch_size = alphas.size(0)  # (5B) * T * Nr (train/val) or 1 * T * Nr (test)
                att_mask = att_mask.squeeze(-2)
                num_img = batch_size // self.seq_per_img if not is_testing else batch_size
                if not is_testing:
                    sel_idx = torch.from_numpy(
                        np.arange(num_img, dtype=int) * self.seq_per_img +
                        np.random.randint(self.seq_per_img, size=num_img)).to(alphas.device).long()
                    alphas = alphas[sel_idx, :]  # B * T * Nr
                    att_mask = att_mask[sel_idx, :]  # B * Nr

                # generate the timestep mask:
                timestep_mask = torch.zeros(num_img, alphas.size(1), 1).to(att_mask)  # B * T * 1
                cap_sentences = decode_sequence(self.vocab, det_result.cap_targets[sel_idx, :]) if not is_testing \
                    else decode_sequence(self.vocab, det_result.cap_seqs)  # B * MAXT
                for i, sent in enumerate(cap_sentences):
                    tokens = sent.split(' ')  # do not use tokenize
                    if self.mask_obj_word:
                        tags = nltk.pos_tag(tokens)
                        for j, (word, tag) in enumerate(tags):
                            if tag in ['NN', 'NNS', 'NNP', 'NNPS']:
                                timestep_mask[i, j, :] = 1
                        if torch.sum(timestep_mask[i]) == 0:
                            timestep_mask[i, :len(tokens)] = 1
                    else:
                        timestep_mask[i, :len(tokens)] = 1

                alphas = alphas * timestep_mask
                # collect this info for visualization
                det_result.word_obj_distribution = alphas

                if self.gather_attention == 'mean':
                    alphas = torch.sum(alphas, dim=1) / torch.sum(timestep_mask, dim=1)
                elif self.gather_attention == 'max':
                    alphas = torch.max(alphas, dim=1)[0]
                else:
                    raise NotImplementedError

                if not is_testing:
                    # perform softmax again and got the final object-wise attention
                    alphas = alphas.masked_fill(att_mask == 0,
                                                -1e9)  # put a large negative value, so that the exp() is almost 0
                # collect this info for visualization
                det_result.obj_distribution = F.softmax(alphas, dim=-1)

                # use the rel pair idx to construct the pair attention
                rel_pair_idxes = det_result.rel_pair_idxes
                alphas_pairs = []
                for i, rel_pair_idx in enumerate(rel_pair_idxes):
                    alphas_pairs.append(
                        F.softmax(alphas[i, rel_pair_idx[:, 0]] + alphas[i, rel_pair_idx[:, 1]], dim=-1))

                det_result.rel_distribution = alphas_pairs
            else:
                if not is_testing:
                    det_result.rel_distribution = tgt_rel_ipts
                else:
                    det_result.rel_distribution = None

        return det_result


from __future__ import annotations

import logging
from pathlib import Path

import pandas as pd

from ...core.mixins import GeoDBMixin
from ...core.structure import Navaid, Route

_log = logging.getLogger(__name__)

class Airways(GeoDBMixin):
    """
    An ATS route is a specified route designed for channelling the flow of
    traffic as necessary for the provision of air traffic services.

    The term “ATS route” is used to mean variously, airway, advisory route,
    controlled or uncontrolled route, arrival or departure route, etc.

    An ATS route is defined by route specifications which include an ATS route
    designator, the track to or from significant points (waypoints), distance
    between significant points, reporting requirements and, as determined by the
    appropriate ATS authority, the lowest safe altitude. (ICAO Annex 11 - Air
    Traffic Services)

    A (deprecated) database of world ATS routes is available as:

    >>> from traffic.data import airways

    Any ATS route can be accessed by the bracket notation:

    >>> airways['Z50']
    Route('Z50', navaids=['EGOBA', 'SOT', 'BULTI', 'AYE', 'AVMON', ...])

    >>> airways.extent((-0.33, 4.85, 42.34, 45.05))["UN869"]
    Route('UN869', navaids=['XOMBO', 'TIVLI', 'AGN', 'NARAK', 'NASEP', ...])

    .. note::
        The following snippet plots the (in)famous `Silk Road Airway (L888)
        <https://flugdienstberater.org/l888>`_ over the Himalaya mountains,
        which requires special qualifications.

    .. jupyter-execute::

        import matplotlib.pyplot as plt

        from traffic.data import airways
        from cartes.crs import Orthographic

        with plt.style.context("traffic"):

            fig, ax = plt.subplots(
                figsize=(7, 7),
                subplot_kw=dict(projection=Orthographic(95, 30)),
            )

            ax.stock_img()
            ax.coastlines()

            airways["L888"].plot(
                ax, linewidth=2, linestyle="solid", color="crimson"
                )

            for navaid in airways["L888"].navaids:
                navaid.plot(
                    ax, s=20, marker=".", color="crimson",
                    text_kw=dict(fontsize=8)
                )

    """

    cache_dir: Path
    alternatives: dict[str, "Airways"] = dict()  # noqa: RUF012
    name: str = "default"

    def __init__(self, data: None | pd.DataFrame = None) -> None:
        self._data = data
        if self.available:
            Airways.alternatives[self.name] = self

    def parse_data(self) -> None:  # coverage: ignore
        cache_file = Path(__file__).parent.parent / "navdata" / "earth_awy.dat"
        assert cache_file.exists()
        self._data = pd.read_csv(cache_file, sep=" ", header=None)
        self._data.columns = ["route", "id", "navaid", "latitude", "longitude"]
        self._data.to_parquet(self.cache_dir / "traffic_airways.parquet")

    @property
    def available(self) -> bool:
        return True

    @property
    def data(self) -> pd.DataFrame:
        if self._data is not None:
            return self._data

        if not (self.cache_dir / "traffic_airways.parquet").exists():
            self.parse_data()
        else:
            _log.info("Loading airways database")
            self._data = pd.read_parquet(
                self.cache_dir / "traffic_airways.parquet"
            )

        if self._data is not None:
            self._data = self._data.rename(
                columns=dict(lat="latitude", lon="longitude")
            )

        return self._data

    def __getitem__(self, name: str) -> None | Route:
        output = self.data.query("route == @name").sort_values("id")
        if output.shape[0] == 0:
            return None
        return Route(
            name,
            list(
                Navaid(
                    x["navaid"],
                    "FIX",
                    x["latitude"],
                    x["longitude"],
                    0,
                    None,
                    None,
                    None,
                )
                for _, x in output.iterrows()
            ),
        )

    def global_get(self, name: str) -> None | Route:
        """Search for a route from all alternative data sources."""
        for _key, value in sorted(
            self.alternatives.items(),
            # lowest priority for the default source of information
            key=lambda key: 1 if key[0] == "default" else 0,
        ):
            alt = value[name]
            if alt is not None:
                return alt
        return None

    def search(self, name: str) -> "Airways":
        """
        Selects the subset of airways matching name in the route name or in the
        passed navigational beacon.

        >>> airways.extent('Switzerland').search("Z50")  # doctest: +SKIP
          route   id   navaid   latitude   longitude
         ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
          Z50     7    GERSA    47.04      8.532
          Z50     8    KELIP    46.96      8.762
          Z50     9    SOPER    46.89      8.944
          Z50     10   PELAD    46.6       9.726
          Z50     11   RESIA    46.48      10.04

        >>> airways.search("NARAK")  # doctest: +SKIP
          route   id   navaid   latitude   longitude
         ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
          N869    88   NARAK    44.3       1.749
          UN859   15   NARAK    44.3       1.749
          UN869   23   NARAK    44.3       1.749
          UT122   15   NARAK    44.3       1.749
          UY155   2    NARAK    44.3       1.749
          UZ365   3    NARAK    44.3       1.749
        """
        output = self.__class__(
            self.data.query("route == @name.upper() or navaid == @name.upper()")
        )
        return output


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

# In[3]:

import numpy as np
import pandas as pd

from sklearn.linear_model import LogisticRegression
from sklearn.neural_network import MLPClassifier
from sklearn.neighbors import KNeighborsClassifier
from sklearn.svm import LinearSVC
from sklearn.svm import SVC
from sklearn.gaussian_process import GaussianProcessClassifier
from sklearn.gaussian_process.kernels import RBF
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import RandomForestClassifier, AdaBoostClassifier
from sklearn.naive_bayes import GaussianNB
from sklearn.discriminant_analysis import QuadraticDiscriminantAnalysis

from sklearn.manifold import Isomap
from sklearn.manifold import LocallyLinearEmbedding
from sklearn.decomposition import TruncatedSVD
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis
from sklearn.decomposition import PCA, KernelPCA

from sklearn.feature_selection import VarianceThreshold
from sklearn.feature_selection import SelectKBest
from sklearn.feature_selection import f_classif
from sklearn.feature_selection import mutual_info_classif
from sklearn.feature_selection import SelectFromModel
from sklearn.ensemble import ExtraTreesClassifier
from sklearn.feature_selection import RFE

from sklearn.model_selection import cross_val_score
from sklearn.model_selection import RepeatedStratifiedKFold
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import StandardScaler

# In[5]:

df = pd.read_csv('train_data.csv')

X = df.drop(['class4', 'class2'], axis=1)
X = X.loc[:, X.columns[range(2, X.shape[1], 2)]]

X_scaled = pd.DataFrame(StandardScaler().fit_transform(X), columns = X.columns)

y_class2 = df['class2']
y_class4 = df['class4']

# In[6]:

classifiers = [
    ('logistic', LogisticRegression()),
    ('kNeighbour', KNeighborsClassifier(3)),
    ('svcLinear', SVC(kernel="linear", C=0.025, probability=True)),
    ('svc', SVC(gamma=2, C=1, probability=True)),
    ('gaussian', GaussianProcessClassifier(1.0 * RBF(1.0))),
    ('decissionTree', DecisionTreeClassifier(max_depth=5)),
    ('rfc', RandomForestClassifier(max_depth=5, n_estimators=10, max_features=1)),
    ('mlp', MLPClassifier(alpha=1, max_iter=1000)),
    ('ada', AdaBoostClassifier()),
    ('gaussianNB', GaussianNB()),
    ('qda', QuadraticDiscriminantAnalysis())]

# In[7]:

p = 0.7
variance = p * (1 - p)

# In[8]:

dimension_reductions_y2 = [
    ('iso', Isomap(n_components=30)),
    ('lle', LocallyLinearEmbedding(n_components=10)),
    ('llemodified', LocallyLinearEmbedding(n_components=10, method='modified', n_neighbors=90)),
    ('svd', TruncatedSVD(n_components=10)),
    ('lda', LinearDiscriminantAnalysis(n_components=1)),
    ('pca', PCA()),
    ('kpca', KernelPCA(kernel="rbf", fit_inverse_transform=True, gamma=1)),
    ('sel', VarianceThreshold(threshold=variance)),
    ('kbest', SelectKBest(f_classif, k=10)),
    ('kbestmutual', SelectKBest(mutual_info_classif, k=10)),
    ('select', SelectFromModel(LinearSVC(penalty="l2"))),
    ('selecttree', SelectFromModel(ExtraTreesClassifier(n_estimators=20))),
    ('rfe', RFE(estimator=DecisionTreeClassifier(), n_features_to_select=20))]

dimension_reductions_y4 = [
    ('iso', Isomap(n_components=10)),
    ('lle', LocallyLinearEmbedding(n_components=10)),
    ('llemodified', LocallyLinearEmbedding(n_components=10, method='modified', n_neighbors=90)),
    ('svd', TruncatedSVD(n_components=5)),
    ('lda', LinearDiscriminantAnalysis(n_components=2)),
    ('pca', PCA()),
    ('kpca', KernelPCA(kernel="rbf", fit_inverse_transform=True, gamma=1)),
    ('sel', VarianceThreshold(threshold=variance)),
    ('kbest', SelectKBest(f_classif, k=10)),
    ('kbestmutual', SelectKBest(mutual_info_classif, k=10)),
    ('select', SelectFromModel(LinearSVC(penalty="l2"))),
    ('selecttree', SelectFromModel(ExtraTreesClassifier(n_estimators=10))),
    ('rfe', RFE(estimator=DecisionTreeClassifier(), n_features_to_select=10))]

# In[11]:

def k_fold_cross_validation(ml_pipeline, X, y, n=5, k=10, score='accuracy'):

    cv = RepeatedStratifiedKFold(n_splits = n,
                                 n_repeats = k,
                                 random_state = 1)
    n_scores = cross_val_score(ml_pipeline, X, y,
                               scoring = score, cv = cv,
                               n_jobs = -1)

    return(np.array([np.mean(n_scores), np.std(n_scores)]))

# In[12]:

columns = ['accuracy_mean', 'accuracy_std',
           'accuracy_scaled_mean', 'accuracy_scaled_std']
statistics_y2 = pd.DataFrame(index = columns)
statistics_y4 = pd.DataFrame(index = columns)

# In[14]:

y = y_class2

for model_used in classifiers:
    model = Pipeline([model_used])

    not_scaled = k_fold_cross_validation(model, X, y)
    scaled = k_fold_cross_validation(model, X_scaled, y)

    data = np.concatenate((not_scaled, scaled))
    statistics_y2[ model_used[0] ] = data
    break
    for feature_selection in dimension_reductions_y2:
        model = Pipeline([feature_selection, model_used])

        not_scaled = k_fold_cross_validation(model, X, y)
        scaled = k_fold_cross_validation(model, X_scaled, y)

        column = model_used[0] + '_' + feature_selection[0]
        data = np.concatenate((not_scaled, scaled))
        statistics_y2[ column ] = data
        break

# In[15]:

statistics_transpose_y2 = statistics_y2.transpose(copy=True)
statistics_transpose_y2

# In[16]:

statistics_transpose_y2.describe()

# In[21]:

y = y_class4

for model_used in classifiers:
    model = Pipeline([model_used])

    not_scaled = k_fold_cross_validation(model, X, y)
    scaled = k_fold_cross_validation(model, X_scaled, y)

    data = np.concatenate((not_scaled, scaled))
    statistics_y4[ model_used[0] ] = data
    break

    for feature_selection in dimension_reductions_y4:
        model = Pipeline([feature_selection, model_used])

        not_scaled = k_fold_cross_validation(model, X, y)
        scaled = k_fold_cross_validation(model, X_scaled, y)

        column = model_used[0] + '_' + feature_selection[0]
        data = np.concatenate((not_scaled, scaled))
        statistics_y4[ column ] = data
        break

# In[24]:

statistics_transpose_y4 = statistics_y4.transpose(copy=True)
statistics_transpose_y4

# In[25]:

statistics_transpose_y4.describe()

# In[ ]:


from nltk.tokenize import TweetTokenizer

def text_analyzer(text):
    """Analyze the incoming text to separate the
    users that were mentionned and the hashtags
    from the current actual text"""
    tokenizer = TweetTokenizer()
    tokens = tokenizer.tokenize(text)
    mentions = filter(lambda x: x.startswith('@'), tokens)
    hashtags = filter(lambda x: x.startswith('#'), tokens)
    return list(mentions), list(hashtags), tokens


import cv2
import utils_stereovision as stereovision

if __name__ == '__main__':
    # Preparing the images
    stereo_pair = cv2.imread('./scenes/photo.png')
    imgL, imgR = stereovision.splitStereoImage(stereo_pair)
    rectifiedL, rectifiedR = stereovision.rectifyImages(imgL, imgR)

    """
    # Using demonstration pictures instead of the project ones
    imgL = cv2.imread('left_test.jpg')
    imgR = cv2.imread('right_test.jpg')
    rectifiedL = cv2.cvtColor(imgL, cv2.COLOR_BGR2GRAY)
    rectifiedR = cv2.cvtColor(imgR, cv2.COLOR_BGR2GRAY)
    imgSize = (imgL.shape[1], imgL.shape[0])
    """

    # Compute disparity map
    matchers = stereovision.Matchers()
    raw_disp_vis = matchers.computeRawDisparityMap(rectifiedL, rectifiedR)
    filtered_disp_vis = matchers.computeFilteredDisparityMap(rectifiedL, rectifiedR)

    # Display results
    confidence = matchers.getConfidenceMap()
    cv2.imshow('Confidence', confidence)
    cv2.imshow('raw Disparity Map', raw_disp_vis)
    cv2.imshow('Filtered Disparity Map Visual', filtered_disp_vis)
    cv2.imshow('left view', imgL)
    cv2.imshow('rectified left', rectifiedL)
    cv2.imshow('rectified right', rectifiedR)
    cv2.waitKey(0)
    cv2.destroyAllWindows()
    cv2.imwrite("disparity_filtered.png", filtered_disp_vis)
    cv2.imwrite("disparity_raw.png", raw_disp_vis)
    print("[INFO] disparity_filtered.png saved")
    print("[INFO] disparity_raw.png saved")

"""
# draft for applying fast bilateral filter (seems not to work)
fbs_spatial = 16.0
fbs_luma = 8.0
fbs_chroma = 8.0
fbs_lambda = 128.0
conf_map = wls_filter.getConfidenceMap()
roi = wls_filter.getROI()

solving_time = time.time()
solved_disp = cv2.ximgproc.fastBilateralSolverFilter(rectifiedL,
                                                     left_disp_resized,
                                                     conf_map,
                                                     sigma_spatial=fbs_spatial,
                                                     sigma_luma=fbs_luma,
                                                     sigma_chroma=fbs_chroma,
                                                     lambda_=fbs_lambda)
solving_time = time.time() - solving_time
solved_filtered_disp = cv2.ximgproc.fastBilateralSolverFilter(rectifiedL,
                                                              filtered_disp,
                                                              conf_map,
                                                              sigma_spatial=fbs_spatial,
                                                              sigma_luma=fbs_luma,
                                                              sigma_chroma=fbs_chroma,
                                                              lambda_=fbs_lambda)
solved_disp_vis = cv2.ximgproc.getDisparityVis(solved_disp)
solved_filtered_disp_vis = cv2.ximgproc.getDisparityVis(solved_filtered_disp)
cv2.imshow('Solved Disparity Map', solved_disp_vis)
cv2.imshow('Solved Filtered Disparity Map', solved_filtered_disp_vis)
"""


from aiohttp import web
import json

async def handle(request):
    response_obj = {'status': 'success'}
    return web.Response(text=json.dumps(response_obj))

async def new_user(request):
    try:
        user = request.query['name']
        print("Creating new user with name: ", user)

        response_obj = {'status': 'success','message':'user succesfully created'}
        return web.Response(text=json.dumps(response_obj), status=200)

    except Exception as e:
        response_obj = {'status': 'failed', 'reason': str(e)}
        return web.Response(text=json.dumps(response_obj), status=500)

app = web.Application()
app.router.add_get('/', handle)
app.router.add_post('/user', new_user)

web.run_app(app)


import requests
from matrix import MatrixAPI
from urllib.parse import urljoin
import json

USER_ID = 'sharky@shark.pm'
HOMESERVER = 'https://' + USER_ID[USER_ID.index('@')+1:]
from settings import ACCESS_TOKEN

print(HOMESERVER)
m = MatrixAPI(access_token=ACCESS_TOKEN, homeserver=HOMESERVER, user_id=USER_ID)
print(m.whoami())

FILTER = {
    'event_format': 'client',
    'room': {
        'rooms': ['!LxbbnChZJRFRySzHIJ:shark.pm'],
        'timeline': {
            'limit': 10,
            'types': ['m.room.message'],
        }
    },
    'presence': {
        'not_types': ['*']
    },
    'account_data': {
        'not_types': ['*']
    }
}

r = m.do('get', urljoin(HOMESERVER, '/_matrix/client/r0/sync'), params=dict(
    filter=json.dumps(FILTER),
    #since
    full_state='false',
    timeout=30000
))
#with open('syncs.json', 'w') as f:
#    f.write(json.dumps(r.json(), indent=4))
#print(json.dumps(r.json(), indent=4))
states = r.json()
with open('history.txt', 'wb') as f:
    for event in states['rooms']['join']['!LxbbnChZJRFRySzHIJ:shark.pm']['timeline']['events']:
        if 'content' in event:
            f.write(
                "{} - <{}> {}\n".format(
                    event['origin_server_ts'],
                    event['sender'],
                    event['content']['body']
                ).encode('utf8'))


from lxml.html import parse
from urllib.request import Request, urlopen
import re

class HTMLParser:
    site = 'http://ice-data.tinro.ru/'
    #site = 'http://192.168.2.33/'

    @staticmethod
    def parse_page():
        req = Request(HTMLParser.site, headers={'User-Agent': 'Mozilla/5.0'})
        page = parse(urlopen(req))
        urls = page.xpath('//a/@href')

        data_urls = {}
        for u in urls:
            if re.match('weekly/(\D*)/data', u):
                sea_name = re.match('weekly/(\D*)/.*', u).group(1)
                if sea_name not in data_urls.keys():
                    data_urls[sea_name] = {'old': HTMLParser.site + u}
                else:
                    data_urls[sea_name]['old'] = HTMLParser.site + u
            elif re.match('daily/(\D*)/data', u):
                sea_name = re.match('daily/(\D*)/.*', u).group(1)
                if sea_name not in data_urls.keys():
                    data_urls[sea_name] = {'new': HTMLParser.site + u}
                else:
                    data_urls[sea_name]['new'] = HTMLParser.site + u

        return data_urls


import argparse
import csv

def parse_arguments():
    info = 'Extracts the residues that were significant hotspots'
    parser = argparse.ArgumentParser(description=info)
    parser.add_argument('-i', '--input',
                        type=str, required=True,
                        help='Full output from hotspot script which includes p-values')
    parser.add_argument('-s', '--significance-level',
                        type=float, required=True,
                        help='The significance level to call it a hotspot residue')
    parser.add_argument('-o', '--output',
                        type=str, default=None,
                        help='Output listing only the residues that were hotspots')
    args = parser.parse_args()
    return vars(args)

def main(opts):
    output = [['Structure', 'Tumor Type', 'Model', 'Chain',
               'Residue Position', 'p-value', 'Density Z-score']]
    with open(opts['input']) as handle:
        # get csv reader
        myreader = csv.reader(handle, delimiter='\t')
        header = next(myreader)

        # define column position from the header info
        struct_ix = header.index('Structure')
        ttype_ix = header.index('Tumor Type')
        model_ix = header.index('Model')
        chain_ix = header.index('Chain')
        res_ix = header.index('Mutation Residues')
        pval_ix = header.index('Hotspot P-value')
        if 'Density Z-score' in header:
            zscore_ix = header.index('Density Z-score')

        # iterate over every structure/tumor type pair
        for line in myreader:
            # skip if no p-values
            if not line[pval_ix]:
                continue

            chains = line[chain_ix].split(',')
            models = line[model_ix].split(',')
            res_pos = line[res_ix].split(',')
            res_pval = map(float, line[pval_ix].split(','))
            if 'Density Z-score' in header:
                res_zscore = map(float, line[zscore_ix].split(','))

            # iterate over p-values
            for i, p in enumerate(res_pval):
                if p <= opts['significance_level']:
                    tmp_list = [line[struct_ix], line[ttype_ix], models[i],
                                chains[i], res_pos[i], p]
                    if 'Density Z-score' in header:
                        tmp_list.append(res_zscore[i])
                    output.append(tmp_list)

    # write output to file
    if opts['output'] is not None:
        with open(opts['output'], 'w') as handle:
            csv.writer(handle, delimiter='\t', lineterminator='\n').writerows(output)
    else:
        return output

if __name__ == '__main__':
    opts = parse_arguments()
    main(opts)


from django.shortcuts import render, redirect,get_object_or_404

# Create your views here.

from .models import *
from django.views.generic.detail import DetailView
from django.views.generic import CreateView
from django.db.models import Q

# from .models import product,collections,categ
# Create your views here.
def shop(request,c_slug=None):
    c_page=None
    prodt=None
    if c_page!=None:
        c_page=get_object_or_404(categ,slug=c_slug)
        prodt=product.objects.filter(category=c_page,available=True)
    else:

     prodt = product.objects.all()

     ct = categ.objects.all()

     obt=collections.objects.all()
    return render(request,"index.html",{'prodt':prodt,'obt':obt,'ct':ct})

class shopdetails(DetailView):
    model = product
    template_name = 'details.html'
    context_object_name = 'i'

def searching(request):
    prodt=None
    query=None

    if 'q' in request.GET:
        query=request.GET.get('q')
        prodt=product.objects.all().filter(Q(slug__contains=query)|Q(disc__contains=query)|Q(price__contains=query))
    return render(request,"search.html",{'qr':query,'prodt':prodt})

class cartview(DetailView):

    model = product
    template_name = 'cart.html'
    context_object_name = 'i'

def orderview(request):
    if request.method == "POST":
        firstname = request.POST['firstname']
        address = request.POST['address']
        email = request.POST['email']
        mobile = request.POST['mobile']
        state = request.POST['state']
        city = request.POST['city']
        zip = request.POST['zip']
        s = placeorder(firstname=firstname, address=address, email=email, mobile=mobile, city=city, state=state,
                       zip=zip)
        s.save()
        return redirect('/')

    return render(request,"order.html")


from pytube import YouTube
import tempfile
import asyncio
import whisper
import os

async def audio_transcriber(path):
    model = whisper.load_model("base")
    audio = whisper.load_audio(path)

    mel = whisper.log_mel_spectrogram(whisper.pad_or_trim(audio)).to(model.device)
    _, probs = model.detect_language(mel)

    result = model.transcribe(audio=audio, language=max(probs, key=probs.get), fp16=False)
    return result['text']

async def mp3_converter():
    print('Enter your youtube video URL:')
    ytURl = input()

    if not 'www.youtube.com' in ytURl:
       return print('Invalid URL')

    with tempfile.TemporaryDirectory() as path:
        YouTube(ytURl).streams.get_lowest_resolution().download(path, 'audio.mp3')
        print('Wait for the transcription process...')
        audio_text = await audio_transcriber(os.path.join(path, 'audio.mp3'))

        return print(audio_text)

asyncio.run(mp3_converter())


x = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

square = lambda x: x * x

print(square(2))

result = list(map(square, x))

print(result)

print(list(map(square, result)))

#########################################################
##Functional
x = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

def productArray(myList):

    result = 1 # multiply elements 1 by 1
    for x in myList:
        result = result * x
    return result

print(productArray(x))

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

from functools import reduce
x = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

result = reduce((lambda x, y: x * y), x)
print(result)


import matplotlib.pyplot as plt
import numpy as np
import os
import matplotlib.ticker as ticker

def load_2_d_array(directory, col_number_y):
    px_axis = np.loadtxt(directory, skiprows=3, usecols=(0,))

    counts = np.loadtxt(directory, skiprows=3, usecols=(col_number_y,))
    return np.transpose(np.stack((px_axis, counts), axis=0))

def mean_std(array):
    mean = np.mean(array, axis=1)
    std = np.std(array, axis=1)
    return mean, std

def label_from_header(file_name):
    return file_name[0:4]

def plot_results(array, label):
    plt.scatter(array[::, 0], array[::, 1])
    plt.xlabel('N')
    plt.ylabel('sigma 1/e divergence [mrad]')
    #plt.legend()

def get_file_list(path_picture):
    tif_files = []
    counter = 0
    for file in os.listdir(path_picture):
        print(file)
        try:
            if file.endswith(".txt"):
                tif_files.append(str(file))
                counter = counter + 1
            else:
                print("only other files found")
        except Exception as e:
            raise e
    return tif_files

directory = "z1600/"
files_1600 = get_file_list(directory)

def batch_files(files, path):
    array_all = np.zeros([15, len(files)])
    count = 0
    for x in files:
        directory = str(path) + str(x)
        print(directory)
        array = load_2_d_array(directory, 1)
        array_all[::, count] = array[::, 1]
        label = str(x[:-4])
        #plot_results(array, label)
        count = count + 1

    return array_all, array[::,0]

print(directory)

fig, ax = plt.subplots()

array_all, array_x = batch_files(files_1600, directory)
mean_array, std_array = mean_std(array_all)
ax.axes.errorbar(array_x,  mean_array, yerr=std_array, xerr=None,fmt='o', label = '1600 um', color='tab:blue')

directory = "z2000/"
files_2000 = get_file_list(directory)
array_all, array_x = batch_files(files_2000, directory)
mean_array, std_array = mean_std(array_all)
ax.axes.errorbar(array_x,  mean_array, yerr=std_array, xerr=None,fmt='s', label = '2000 um', color = 'tab:purple')

directory = "z2800/"
files_2800 = get_file_list(directory)
array_all, array_x = batch_files(files_2800, directory)
mean_array, std_array = mean_std(array_all)
ax.axes.errorbar(array_x,  mean_array, yerr=std_array, xerr=None,fmt='*', label = '2800 um',  color = 'tab:pink')

directory = "z3200/"
files_3200 = get_file_list(directory)
array_all, array_x = batch_files(files_3200, directory)
mean_array, std_array = mean_std(array_all)
ax.axes.errorbar(array_x,  mean_array, yerr=std_array, xerr=None, fmt='P', label = '3200 um', color = 'tab:red')

directory = "z3600/"
files_3600 = get_file_list(directory)
array_all, array_x = batch_files(files_3600, directory)
mean_array, std_array = mean_std(array_all)
ax.axes.errorbar(array_x,  mean_array, yerr=std_array, xerr=None, fmt='v',label = '3600 um', color = 'tab:orange')

def diffraction_limit():
    N_list = np.arange(1, 30, 1)
    N_diffraction_limit = np.zeros([29, 1])
    for x in range(15, 30 - 1):
        # halfangle
        N_list[x] = 1 + x
        N_diffraction_limit[x] = (60. *1E3/ 1500.) / (1 + x)
    return N_list, N_diffraction_limit

#plot_diffraction_limit()
array_x, diffraction_limit = diffraction_limit()
ax.axes.scatter(array_x,  diffraction_limit, label = 'ThetaL/N', alpha = 0.5)

#plt.legend()
ax.axes.set_xlabel('harmonic number [N]')
ax.axes.set_ylabel('w0(z,N) divergence [mrad]')
ax.axes.set_yscale('log')
#plt.axes.axes.set_yscale(1E-3)
ax.axes.set_xlim(16.5,28)
ax.axes.set_ylim(1,11)
minors = np.linspace(1, 10, 10)[1:-1]
ax.yaxis.set_minor_locator(ticker.FixedLocator(minors))
ax.axes.legend()

plt.savefig("div_over_N_best_shots_avg_70" + ".png", bbox_inches="tight", dpi=1000)
plt.show()


import sympy as sm
import numpy as np

print('='*15,'TUGAS BESAR ALOGAIRTMA DAN PEMROGRAMAN','='*15)
print('''
                +--------------------------------+
                |    SYARIIF ABDURRAHMAN BATHIK  |
                |           1202194114           |
                |            SI-43-09            |
                +--------------------------------+
''')

class HillCipher(): # Class untuk Hill Chiper
    def __init__(self, x, y):
        self.x = x
        self.y = y

    def getKey(self): #Fungsi untuk matrix KEY agar merubah character menjadi angka
        k = 0
        for i in range(3):
            for j in range(3):
                keyMatrix[i][j] = ord(self.y[k]) % 65 # American Standart Code for Information Interchange
                k += 1

    def getInversKey(self): #fungsi untuk menginvers KEY matrix
        self.getKey()
        convertSM = sm.Matrix(keyMatrix)
        minorCoFactorKeyMatrix = convertSM.adjugate() #untuk mencari kofaktor
        determinantKeyMatrix = convertSM.det() #untuk mencari determinan

        inverseKeyMatrix = (determinantKeyMatrix * minorCoFactorKeyMatrix % 26) #invers key matriks harus tetap modulus 26
        inverseKeyMatrix = inverseKeyMatrix.tolist()

        return inverseKeyMatrix

    def encryptData(self, messageMatrix): #fungsi encrypt data
        for i in range(3):
            for j in range(1):
                cipherMatrix[i][j] = 0
                for x in range(3):
                    cipherMatrix[i][j] += (keyMatrix[i][x] * messageMatrix[x][j])
                    cipherMatrix[i][j] = cipherMatrix[i][j] % 26

        return cipherMatrix

    def decryptData(self, cipherMatrix): #fungsi decrypt data
        inverseKeyMatrix = self.getInversKey()
        cipherMatrix = self.encryptData(messageMatrix)
        for i in range(3):
            for j in range(1):
                messageMatrix[i][j] = 0
                for x in range(3):
                    messageMatrix[i][j] += (inverseKeyMatrix[i][x] * cipherMatrix[x][j])
                    messageMatrix[i][j] = messageMatrix[i][j] % 26

    def processEncrypt(self):
        self.getKey()
        for i in range(3):
            messageMatrix[i][0] = ord(self.x[i]) % 65 #untuk merubah char ke angka yg merepresentasikannya

        self.encryptData(messageMatrix)

        CipherText = []
        for i in range(3):
            CipherText.append(chr(cipherMatrix[i][0] + 65)) #untuk convert string ke char yang merepresentasikannya
        print("Ciphertext: ", "".join(CipherText))

    def processDecrypt(self):
        self.decryptData(cipherMatrix)
        for i in range(3):
            cipherMatrix[i][0] = ord(self.x[i]) % 65

        CipherText = []
        for i in range(3):
            CipherText.append(chr(cipherMatrix[i][0] + 65))
        print("Decrypted Message: ", "".join(CipherText))

if __name__ == "__main__":
    Message = input('Input Text To Encrypt :')

    keyMatrix = [[0] * 3 for i in range(3)]
    messageMatrix = [[0] for i in range(3)]
    cipherMatrix = [[0] for i in range(3)]

    default_Key = "HILLMAGIC"

output = HillCipher(Message, default_Key)
output.processEncrypt()
decrypt = input('Do You Want To Decrypt The Message ? (y/n) :')
if (decrypt == 'Y' or decrypt == 'y'):
    output.processDecrypt()
    print('Ok Thank You :)')
elif (decrypt == 'N' or decrypt == 'n'):
    print('Ok Thank You :)')
else:
    print('Wrong Input !!!')


from flask import Flask

webapp = Flask(__name__)

#statistic
global T_max_miss
global T_min_miss
global expand
global shrink

control = "http://127.0.0.1:5000/controller"

from AutoScaler import main


import pygame
import math as m
from enum import Enum

class Direct(Enum):
    LEFT = 1
    RIGHT = 2

class Tank:
    def __init__(self, screen, color, land, x=0, y=0, weapon_list=[]):
        self.screen = screen
        self.color = color
        self.x = 0
        self.y = 0
        self.weapon_list = weapon_list
        self.selected_gun = 0
        self.points = 100
        self.angle = 0
        self.power = 50
        self.hitbox = (self.x-5, self.y-3, 10, 6)
        self.land = land
        self.move_limit = 180

    def move(self, direct):
        if direct == Direct.LEFT:
            if self.move_limit > 0 and 14 < self.x < self.land.box_x - 13:
                self.x = self.x - 1
                self.y = self.land.road_map.get(self.x)[1]
                self.move_limit -= 1
            else:
                pass
        elif direct == Direct.RIGHT:
            if self.move_limit > 0 and 13 < self.x < self.land.box_x - 14:
                self.x = self.x + 1
                self.y = self.land.road_map.get(self.x)[1]
                self.move_limit -= 1
            else:
                pass

    def render(self):
        tube_x = 15 * m.cos(m.radians(self.angle))
        tube_y = - 15 * m.sin(m.radians(self.angle))
        # tube_x, tube_y = rotate_coords(x=8, cos=m.cos(m.radians(self.angle)), sin=m.sin(m.radians(self.angle)))

        land_left, land_right = self.land.road_map.get(self.x-7), self.land.road_map.get(self.x+7)

        gip_len = m.sqrt((land_right[0]-land_left[0])**2+(land_right[1]-land_left[1])**2)
        box_cos = (land_left[1]-land_right[1])/gip_len
        box_sin = 14/gip_len

        if self.land.road_map.get(self.x)[1] > self.y+5:
            self.y = self.y + 2

        update_hitbox(self, self.x, self.y)

        pygame.draw.aaline(self.screen, (200, 200, 200), (self.x-1, self.y), (self.x - 1 + tube_x, self.y + tube_y))
        # print(round(rotate_coords(self.x-7, self.y-4, box_cos, box_sin)[0]), round(rotate_coords(self.x-7, self.y-4, box_cos, box_sin)[1]))
        # print("not rot", self.x-7, self.y-4)
        pygame.draw.polygon(self.screen, self.color, [(self.x-7, self.y-4),
                                                      (self.x+7, self.y-4),
                                                      (self.x+10, self.y+4),
                                                      (self.x-10, self.y+4)])

def rotate_coords(x=0, y=0, cos=0.0, sin=0.0):
    x_ret = x + x*cos + y*sin
    y_ret = y - x*sin + y*cos
    return x_ret, y_ret

def update_hitbox(tank, x, y, box_angle=0):
    TANK_LENGTH = 14
    TANK_HEIGHT = 8
    tank.x = x
    tank.y = y

    tank.hitbox = (x-7, y-4, 14, 8)

# FOR TESTING
'''
go = True
resX = 800
resY = 600
pygame.display.init()
color = (100, 60, 60)
screen = pygame.display.set_mode((resX, resY))
tanka = Tank(screen, color)

while go:
    tanka.render(400, 300)
    pygame.display.flip()
    pygame.event.pump()

    pressed_list = pygame.key.get_pressed()
    if pressed_list[pygame.K_ESCAPE]:
        go = False
'''


import fitz, os, time

rut = input('Ubicación del archivo: ')
pdf = input('Nombre del pdf (sin la extensión *.pdf): ') # A mejorar: detectar la terminación *.pdf

ruta = os.path.join(rut, '')

nom_pdf = "{}{}.pdf".format(ruta, pdf)

ruta_imgs = os.path.join('{}{}'.format(ruta, nom_pdf), '')

if not os.path.exists(pdf):
	os.makedirs(pdf)

doc = fitz.open(nom_pdf)
for i in range(len(doc)):
    for img in doc.getPageImageList(i):
        xref = img[0]
        pix = fitz.Pixmap(doc, xref)
        if pix.n < 5:       # this is GRAY or RGB
            pix.writePNG("{}/{}-{}.png".format(pdf, i, xref))
            print("extraida la imagen {}-{}.png".format(i, xref))
        else:               # CMYK: convert to RGB first
            pix1 = fitz.Pixmap(fitz.csRGB, pix)
            pix1.writePNG("{}/{}-{}.png".format(pdf, i, xref))
            print("extraida la imagen {}-{}.png".format(i, xref))
            pix1 = None
        pix = None
doc.close()

print("Ha finalizado la extracción de las imágenes de su PDF")
wait = input("Presione ENTER para salir.")


#! /usr/bin/env python3

import dbm
import datetime
import time
import concurrent.futures
import requests
import send_email

try:
    import tomllib
except ModuleNotFoundError:
    import tomli as tomllib

def check_get(http_endpoint: str) -> bool:
    try:
        r = requests.get(http_endpoint, timeout=30)
    except ConnectionError:
        return False
    return r.status_code == requests.codes.ok

def http_check(http_endpoint) -> tuple[str, str]:
    if check_get(http_endpoint):
        status = "UP"
    else:
        # Recheck after 60 seconds to ensure not a false positive.
        time.sleep(60)
        if check_get(http_endpoint):
            status = "UP"
        else:
            status = "DOWN"
    return status

def test_service(db, config, service_name):
    services = config["services"]
    service = services[service_name]
    status = http_check(service["http_endpoint"])
    try:
        old_status = db[service_name].decode()
    except KeyError:
        old_status = None
    if status != old_status:
        # Status has changed. Update stored value and send notification email.
        subject = f"{service_name} is {status}"
        timestamp = (
            datetime.datetime.now().astimezone().replace(microsecond=0).isoformat()
        )
        body = f"""This is an automated monitoring email.

At {timestamp} the status of {service_name} changed to {status}."""
        send_email.send_email(service["email"], subject, body)
        send_email.send_email(config["to_addresses"], subject, body)
        db[service_name] = status

def main():
    with open("config.toml", "rb") as fp:
        config = tomllib.load(fp)
    # Unix key-value database thing. Interface is dictionary like, but it only
    # stores bytes.
    with dbm.open("status.dbm", "c") as db:
        with concurrent.futures.ThreadPoolExecutor() as executor:
            futures = [
                executor.submit(test_service, db, config, service)
                for service in config["services"]
            ]
            concurrent.futures.wait(futures, return_when="ALL_COMPLETED")

if __name__ == "__main__":
    main()


from conans import ConanFile, CMake, tools

import os

class PocoConan(ConanFile):
    name = "poco"
    version = "1.10.1"
    settings = "os", "compiler", "build_type", "arch"
    options = {"shared": [True, False], "fPIC": [True, False]}
    default_options = {"shared": True, "fPIC": True}
    #requires = "openssl/1.1.1"
    python_requires = "nla_pkg_helper/1.0"
    python_requires_extend = "nla_pkg_helper.ConanPackageHelper"
    pkg_helper = None

    def init(self):
        self.pkg_helper = self.python_requires["nla_pkg_helper"].module.ConanPackageHelper

        self.pkg_helper.clean_conan_cache_by_detected_os_host_and_arch(self, self.name, self.version)

    def requirements(self):
        if self.settings.os == "Windows":
            self.requires("openssl/1.1.1j")
        elif self.settings.os == "Macos":
            self.requires("openssl/1.1.1")

    def config_options(self):
        if self.settings.os == "Windows":
            del self.options.fPIC

    def source(self):
        git = tools.Git()
        git.clone("https://github.com/pocoproject/poco.git", "poco-1.10.1-release")

    def build(self):
        cmake = CMake(self)

        config_args = [f"-DCMAKE_INSTALL_PREFIX={self.pkg_helper.get_bin_export_path(self)}"]

        if self.settings.os == "Macos":
            cmake.configure(args=config_args)

            cmake.build()
            cmake.install()

        elif self.settings.os == "Windows":
            cmake_build = "cmake-build"

            tools.mkdir(cmake_build)

            with tools.chdir(cmake_build):
                config_args.append('-G')
                config_args.append("Visual Studio 16 2019")
                cmake.configure(args=config_args, cache_build_folder=cmake_build)

                cmake.build()
                cmake.install()

    def package(self):
        self.pkg_helper.build_macosx_universal_bins(self)


import requests
import json
import time
# -------------------------
# Jinja2
# -------------------------
from jinja2 import Environment, FileSystemLoader
template_dir = 'Templates/'
env = Environment(loader=FileSystemLoader(template_dir))
xmen_template = env.get_template('XMen.j2')

# -------------------------
# Headers
# -------------------------
headers = {
  'Accept': 'application/json',
  'Content-Type': 'application/json',
}

# -------------------------
# All Characters
# -------------------------

person_template = env.get_template('character.j2')
people = requests.request("GET", "https://xmenapiheroku.herokuapp.com/api/characters", headers=headers)
peopleJSON = people.json()
peopleList = peopleJSON['results']
while peopleJSON['info']['next']:
    people = requests.request("GET", peopleJSON['info']['next'], headers=headers)
    peopleJSON = people.json()
    peopleList.extend(peopleJSON["results"])

peoplePersonList = []
for person in peopleList:
    singlePersonJSON = person
    if singlePersonJSON not in peoplePersonList:
        peoplePersonList.append(singlePersonJSON)

# -------------------------
# Person Template
# -------------------------

    parsed_all_output = person_template.render(
        singlePerson = singlePersonJSON,
        )

# -------------------------
# Save Characters File
# -------------------------

    with open(f"Cerebro/Characters/{ singlePersonJSON['name'] }.md", "w") as fh:
        fh.write(parsed_all_output)
        fh.close()

# -------------------------
# All Episodes
# -------------------------

episode_template = env.get_template('episode.j2')
episode = requests.request("GET", "https://xmenapiheroku.herokuapp.com/api/episodes", headers=headers)
episodeJSON = episode.json()
episodeList = episodeJSON['results']
while episodeJSON['info']['next']:
    episode = requests.request("GET", episodeJSON['info']['next'], headers=headers)
    episodeJSON = episode.json()
    episodeList.extend(episodeJSON["results"])

episodeEpisodeList = []
for episode in episodeList:
    episodeJSON = episode
    if episodeJSON not in episodeEpisodeList:
        episodeEpisodeList.append(episodeJSON)

# -------------------------
# Episode Template
# -------------------------

    parsed_all_output = episode_template.render(
        singleEpisode = episodeJSON,
        )

# -------------------------
# Save Characters File
# -------------------------

    with open(f"Cerebro/Episodes/{ episodeJSON['title'] }.md", "w") as fh:
        fh.write(parsed_all_output)
        fh.close()

# -------------------------
# X-Men TAS Template
# -------------------------

parsed_all_output = xmen_template.render(
    peoplePersonList = peoplePersonList,
    episodeEpisodeList = episodeEpisodeList
    )

# -------------------------
# Save X-Men TAS File
# -------------------------

with open("Cerebro/X-Men The Animated Series.md", "w") as fh:
    fh.write(parsed_all_output)
    fh.close()


from __future__ import (absolute_import, division, print_function)
__metaclass__ = type

from ansible.plugins.lookup import LookupBase
from ansible.module_utils.six import string_types
from ansible.errors import AnsibleError

import os

class LookupModule(LookupBase):

    def run(self, terms, variables=None, **kwargs):

        results = []

        wantstate = kwargs.pop('wantstate', None)

        files          = self._flatten(terms[0])
        filesExclusive = self._flatten(terms[1])
        filesDir       = terms[2]

        itemDefault = {
            'state': 'present'
        }

        # Mark exclusive files as absent
        for file in filesExclusive:
            item = itemDefault.copy()
            item.update({
                'file':  file['path'],
                'state': 'absent'
            })
            results.append(item)

        for file in files:

            items = []

            # Short syntax
            if isinstance(file, string_types):
                item = itemDefault.copy()
                item.update({
                    'url': file
                })
            else:
                # Must be a dict
                if not isinstance(file, dict):
                    raise AnsibleError('Expect a dict')

                if ('file' not in file) and ('url' not in file):
                    raise AnsibleError('Expect "url" or "file" key')

                if (file.get('state') == 'present') and ('url' not in file):
                    raise AnsibleError('Expect "url" key for present state')

                item = itemDefault.copy()
                item.update(file)

            if 'url' in item and 'file' not in item:
                item.update({
                    'file': os.path.basename(item['url'])
                })

            item.update({
                'file': os.path.join(filesDir, item['file'])
            })

            items.append(item)

            # Merge by index key
            for item in items:
                itemFound = False
                for i, result in enumerate(results):
                    if result['file'] == item['file']:
                        results[i] = item
                        itemFound = True
                        break

                if not itemFound:
                    results.append(item)

        # Filter by state
        if wantstate:
            results = [result for result in results if result.get('state') == wantstate]

        return results


import numpy as np
import torch

from .explorer import Explorer
from see_to_touch.utils import OrnsteinUhlenbeckActionNoise

class OUNoise(Explorer):
    def __init__(
        self,
        num_expl_steps,
        sigma=0.8
    ):
        super().__init__(num_expl_steps=num_expl_steps)
        self.sigma = sigma
        self.ou_noise = OrnsteinUhlenbeckActionNoise(
            mu = np.zeros(23), # The mean of the offsets should be 0
            sigma = sigma # It will give bw -1 and 1 - then this gets multiplied by the scale factors ...
        )

    def explore(self, offset_action, global_step, episode_step, device, eval_mode, **kwargs):
        if eval_mode: # If we are evaluating no exploration
            return offset_action

        if episode_step == 0:
            self.ou_noise = OrnsteinUhlenbeckActionNoise(
                mu = np.zeros(23),
                sigma = self.sigma
            )
        if global_step < self.num_expl_steps:
            offset_action = torch.FloatTensor(self.ou_noise()).to(device).unsqueeze(0)

        return offset_action


# Python script to run fIC analysis functions

# Import relevant libraries
import numpy as np
import matplotlib.pyplot as plt
import glob
import os
import sys
import pickle
from scipy.io import savemat
import pandas as pd

# Import fIC analysis functions
sys.path.insert(0,r'C:\Users\adam\OneDrive - University College London\UCL PhD\PhD Year 1\Projects\VERDICT Screening\Code\VERDICT-Screening\VERDICT Fitting\My Functions')
import fIC_analysis #type: ignore

# Read output folder
OutputFolder = str(open('output_folder.txt', 'r').read())

# OutputFolder = r"C:\Users\adam\OneDrive - University College London\UCL PhD\PhD Year 1\Projects\ISMRM Submission\Outputs"

# ===================================================

# Define ROI drawer
ROIdrawer = 'NT'

# Define ROI averaging type
avg_type = 'median'

# Define Model numbers to analyse
ModelNames= ['RDIex903000']#, 2, 11, 15]#[0,0.5,1,11,12,2,15,19,20,21]

# Loop over model numbers
for ModelName in ModelNames:

    # ROI Name
    if ROIdrawer == 'NT':
        ROIName = 'L1_b3000_NT'
    else:
        ROIName = 'L1'

    # Find list of patients
    fnames = glob.glob(f'{OutputFolder}\VERDICT outputs\*')#/Model {ModelNum}/fIC.mat')

    PatNums = [ os.path.basename(fname) for fname in fnames  ]

    # Create list for 'good PatNums' which have a well defined ROI from Natasha
    goodPatNums = []

    # For each patient, save lesion ROI masks and extract fICs
    for indx, PatNum in enumerate(PatNums):

        # print(f'== {PatNum}')

        # # Excluding ROIs
        if PatNum in ['INN_291', 'INN_167']:
            continue

        # If ROI mask exists, try and extract ROI
        try:

            '''Using Natasha's RTstruct ROIs'''
            # # Extract fICs
            if ROIdrawer == 'NT':
                fIC_analysis.extractROIfICs(PatNum,
                                            ROIName = ROIName,
                                            ModelName = ModelName,
                                            parameter = 'fIC',
                                            MaskType = 'numpy',
                                            VERDICT_output_path = rf"{OutputFolder}\VERDICT outputs",
                                            ROI_path = r"C:\Users\adam\OneDrive - University College London\UCL PhD\PhD Year 1\Projects\ISMRM Submission\Outputs\ROIs",
                                            # ROI_path = r"C:\Users\adam\OneDrive - University College London\UCL PhD\PhD Year 1\Projects\VERDICT Screening\Outputs\fIC ROIs",
                                            output_path = rf"{OutputFolder}\fIC results"
                                            )

            # '''Using my ROIs'''
            # if ROIdrawer == 'AP':
            #     fIC_analysis.extractROIfICs(
            #         PatNum,
            #         ROIName = ROIName,
            #         ModelNum = ModelNum,
            #         MaskType = 'Analyze',
            #         ROI_path = r"C:\Users\adam\OneDrive - University College London\UCL PhD\PhD Year 1\INNOVATE\INNOVATE my ROIs"
            #         )

            # Append to goodPatNums list
            goodPatNums.append(PatNum)

        # If no ROI RTstruct file, or error, remove patient number from list
        except:
            print(f'No ROI for {PatNum}')

    PatNums = goodPatNums

    # == Extract average and variance of fICs in ROIs

    # Calculate median fICs
    fIC_analysis.avgROIfICs(
        PatNums = PatNums,
        ROIName = ROIName,
        ModelName = ModelName,
        avg_type = avg_type,
        results_path = rf"{OutputFolder}\fIC results"
        )

    # # fIC variance across ROI
    # fIC_analysis.ROIvariance(PatNums = PatNums, ROIName = ROIName, ModelNum = ModelNum)

    # Apply ROC analysis
    ResultsDF, fpr, tpr, thresholds, roc_auc = fIC_analysis.fIC_ROC(
                                                                    ModelName = ModelName,
                                                                    avg_type = avg_type,
                                                                    results_path = rf"{OutputFolder}\fIC results"
                                                                    )

    with pd.option_context('display.max_rows', None, 'display.max_columns', None):  # more options can be specified also
        print(ResultsDF)

    # == Save results

    # Make folder
    try:
        os.makedirs(f'{OutputFolder}/ROC results/{ROIdrawer}/{ModelName}/{avg_type}')
    except:
        None

    # Save results dataframe as pickle
    with open(f'{OutputFolder}/ROC results/{ROIdrawer}/{ModelName}/{avg_type}/Results_DF.pickle', 'wb') as handle:
        pickle.dump(ResultsDF, handle, protocol=pickle.HIGHEST_PROTOCOL)

    # Save results data frame as matlab structure
    savemat(f'{OutputFolder}/ROC results/{ROIdrawer}/{ModelName}/{avg_type}/ResultsDF.mat', {'ResultsDF': ResultsDF.to_dict(orient = 'list')})

    # Save ROC results
    np.save(f'{OutputFolder}/ROC results/{ROIdrawer}/{ModelName}/{avg_type}/fpr.npy', fpr)
    np.save(f'{OutputFolder}/ROC results/{ROIdrawer}/{ModelName}/{avg_type}/tpr.npy', tpr)
    np.save(f'{OutputFolder}/ROC results/{ROIdrawer}/{ModelName}/{avg_type}/thresholds.npy', thresholds)
    np.save(f'{OutputFolder}/ROC results/{ROIdrawer}/{ModelName}/{avg_type}/roc_auc.npy', roc_auc)

    print(roc_auc)

    Youden_Indices = tpr-fpr

    print(f'ROC AUC: {roc_auc}')
    print(f'Thresholds: {thresholds}')
    print(f' Best threshold: {thresholds[Youden_Indices == np.max(Youden_Indices)]}')

    plt.figure()
    plt.plot(fpr,tpr)
    plt.grid('on')
    plt.show()


from django.shortcuts import render, redirect

# Create your views here.
from .models import Service
from django.template import loader
from .forms import ServiceForm
from datetime import datetime
from django.contrib.auth.models import User

from rest_framework import generics
from .serializers import ServiceSerializer, ServiceCreateSerializer
from rest_framework.decorators import api_view, permission_classes
from rest_framework.response import Response
from django.views.decorators.csrf import csrf_exempt

def index(request):
    services = Service.objects.filter(receiver_id=request.user.id)
    context = {'services': services}
    return render(request, 'services/index.html', context)

def new(request):
    print(request.method)
    if request.method == 'GET':
        form = ServiceForm()
    else:
        form = ServiceForm(request.POST)
        user = request.user
        print("Valid form", form.is_valid())

        if form.is_valid():
            print('valid')
            # title = form.cleaned_data['title']
            # image = form.cleaned_data['image']
            instance = form.save(commit=False)
            instance.creator_id = user.id
            instance.updater_id = user.id
            instance.save()
            # form.save()
            return redirect('/services')
    users = User.objects.all()
    context = {'users': users}
    return render(request, "services/new.html", context)

def show(request):
    services = Service.objects.all()
    return render(request, "services/show.html", {'services': services})

def edit(request, id):
    services = Service.objects.get(id=id)
    return render(request, 'services/edit.html', {'services': services})

def update(request, id):
    services = Service.objects.get(id=id)
    form = ServicesForm(request.POST, instance=services)
    if form.is_valid():
        form.save()
        return redirect("/services")
    return render(request, 'services/edit.html', {'services': services})

def destroy(request, id):
    services = Service.objects.get(id=id)
    services.delete()
    return redirect("/services")

@csrf_exempt
@api_view(["GET"])
def services(request):
    services = Service.objects.filter(creator_id=request.user.id)
    # return Response(latest_post_list, status=HTTP_200_OK)
    # the many param informs the serializer that it will be serializing more than a single article.
    serializer = ServiceSerializer(services, many=True)
    return Response({"services": serializer.data})

@api_view(["POST"])
def create_service(request):
    serializer = ServiceCreateSerializer(data=request.data)
    print("Data", request.data)
    print("User", request.user.id)
    if serializer.is_valid():
        # user = Document.objects.create(serializer.validated_data)
        serializer.save(creator=request.user, updater=request.user)
        return Response({"message": "Document created"})
    else:
        data = {
            "error": True,
            "errors": serializer.errors,
        }
        return Response(data)

class ServiceList(generics.ListCreateAPIView):
    queryset = Service.objects.all()
    serializer_class = ServiceSerializer

class ServiceDetail(generics.RetrieveAPIView):
    queryset = Service.objects.all()
    serializer_class = ServiceSerializer

class ServiceDetail(generics.RetrieveUpdateDestroyAPIView):
    queryset = Service.objects.all()
    serializer_class = ServiceSerializer

def socialVisa(request):
    return render(request, 'socialVisa.html')


import http
from datetime import datetime

from .utils import *

def init_upload_routes(app, socketio):
    @app.route('/upload', methods=['POST'])
    def upload():
        file = request.files['file']
        path = request.form['path']
        # overwrite = request.form['overwrite']
        # print(overwrite)

        if file and allowed_file(file.filename):
            filename = secure_filename(file.filename)

            # if os.path.exists(os.path.join(path, filename)) and overwrite == 'false':
            #     warnings_msg = f"File already exists: {filename}"
            #     app.logger.warning(warnings_msg)
            #     return jsonify(status=http.HTTPStatus.CONFLICT, message=warnings_msg)

            file.save(os.path.join(path, filename))
            app.logger.info(f"File uploaded: {filename}")
            return jsonify(status=http.HTTPStatus.OK)
        elif not allowed_file(file.filename):
            warning_msg = f"File type not allowed: {file.filename}"
            app.logger.warning(warning_msg)
            return jsonify(status=http.HTTPStatus.BAD_REQUEST, message=warning_msg)

    @app.route('/upload-fill-data', methods=['POST'])
    def upload_fill_data():
        try:
            data = request.get_json()['fillData']
            df = pd.DataFrame(data)
            file_path = os.path.join(Config.UPLOAD_TMP_PREDICT_FOLDER, f'fill_data_{datetime.timestamp(datetime.today())}.csv')
            df.to_csv(file_path, index=False)
            # app.logger.info(file_path)
            return jsonify(status=http.HTTPStatus.OK, filePath=file_path)

        except Exception as e:
            error_msg = f"Exception occurred: {str(e)}\n{traceback.format_exc()}"
            app.logger.error(error_msg)
            return jsonify(status=http.HTTPStatus.INTERNAL_SERVER_ERROR, message=error_msg)


import matplotlib.pyplot as plt
import numpy as np
from scipy.optimize import curve_fit
from scipy.signal import find_peaks_cwt
from uncertainties import ufloat
import scipy.constants as c
import math
from lmfit import minimize, Parameter, Model

DA, ErrDA, IA, MA = np.loadtxt('data/Aluminium.txt', unpack=True)
DB, IB, MB = np.loadtxt('data/Blei.txt', unpack=True)
DK, IK, MK = np.loadtxt('data/Kupfer.txt', unpack=True)

NullGamma = int(804/900); NullBeta = int(248/900)
DA *= (10**(-6)*2760); DB *= 0.001; DK *= 0.001;

#Berechnet bereinigte Zählrate
def berechneZaehlrate(Impulszahl, Zeit, Null, laenge):
	Z = [0]*laenge
	for x in range(laenge):
	  Z[x] = Impulszahl[x]/Zeit[x] - Null
	return np.array(Z)

# Berechnet den Fehler der Zählrate
def calcerr(Z, A):
        Zerr= [0]*A
        for x in range(A):
            Zerr[x] = np.sqrt(Z[x])
        return Zerr

#lineraer Fit
def f(x, a, b):
    return np.exp(-a*x + b)

#Fitte durch die Punkte x,y
def fitfunc(x,y,xdeff):
    fx, ferr = curve_fit(f, x, y)
    #fx = np.exp(fx)
    ferr = np.sqrt(np.diag(ferr))
    plt.plot(xdeff, f(xdeff, *fx))
    m = ufloat(fx[0],ferr[0])
    b = ufloat(fx[1],ferr[0])
    print('Steigung', m, 'y-Achseschnitt', b)
    return m, b

print('--------------Aluminium-------------')
#Berechnet Zählrate mit Fehler
ZA = berechneZaehlrate(IA,MA,NullBeta, 11)
ZAerr = calcerr(ZA,11)
print(ZA, ZAerr)
plt.semilogy(DA,ZA,'bx', label='Messwerte')
plt.errorbar(DA, ZA, yerr=ZAerr, fmt='bx')
xdeff = np.linspace(0,2,50)
m1, b1 = fitfunc(DA[:5],ZA[:5],xdeff)
m2, b2 = fitfunc(DA[5:],ZA[5:],xdeff)
s = (b1-b2)/(m1-m2)
Emax = 1.92*(((s*0.10)**2+0.22*(s*0.10))**0.5)
print('Schnittpunkt = ', s, "und Maximale Energie von: ", Emax)
plt.xlim(0.2,1.4)
plt.xlabel(r'Massenbelegung / $\frac{kg}{m^2}$')
plt.ylim(10**(-1),10**2)
plt.ylabel(r'Zählrate / $cps$')
plt.legend(loc='best')
plt.grid()
plt.tight_layout()
plt.savefig('build/Aluminium.pdf')
plt.close()

print('--------------Blei------------------')
ZB = berechneZaehlrate(IB,MB,NullGamma, 15)
ZBerr = calcerr(ZB, 15)
#print(ZB, ZBerr)
plt.semilogy(DB,ZB,'rx')
plt.errorbar(DB,ZB, yerr=ZBerr, fmt='rx')
xdeff = np.linspace(0, 0.065, 50)
fitfunc(DB,ZB,xdeff)
plt.xlim(0,0.065)
plt.xlabel(r'Dicke / m')
plt.ylabel(r'Zaehlrate / $cps$')
plt.tight_layout()
plt.savefig('build/Blei.pdf')
plt.close()

print('--------------Kupfer----------------')
ZK = berechneZaehlrate(IK,MK,NullGamma, 22)
ZKerr = calcerr(ZK,22)
#print(ZK, ZKerr)
plt.semilogy(DK,ZK,'rx')
plt.errorbar(DK, ZK, yerr=ZKerr, fmt='rx')
xdeff= np.linspace(0, 0.065, 50)
fitfunc(DK,ZK, xdeff)
plt.xlim(0,0.065)
plt.xlabel(r'Dicke / m')
plt.ylabel(r'Zaehlrate / $cps$')
plt.tight_layout()
plt.savefig('build/Kupfer.pdf')
plt.close()

V704 - Absorbtion von gamma- und beta-Strahlung/plot.py


from django.http import HttpResponse
from upload.forms import UploadFileForm
from django.shortcuts import render
from upload.models import *
import openpyxl

def upload_file(request):
    def get_last_used_row(sheet):
        row = sheet.max_row
        while row > 0:
            cells = sheet[row]
            if all([cell.value is None for cell in cells]):
                row -= 1
            else:
                break
        if row == 0:
            return 0, 0
        return row

    def get_first_row_by_value(sheet, value):
        for row in sheet.iter_rows(min_row=1, min_col=1, max_row=get_last_used_row(wb.worksheets[0]), max_col=1):
            for cell in row:
                if cell.value == value:
                    return sheet.cell(row=cell.row, column=cell.column).row

    def get_nonempty_rows_in_first_column(sheet):
        filled = 0
        for row in range(get_first_row_by_value(wb.worksheets[0], 1), get_last_used_row(wb.worksheets[0]) + 1):
            if sheet.cell(row=row, column=1).value is not None:
                filled += 1
        return filled

    if request.method == 'POST':
        upload_file_from = UploadFileForm(request.POST, request.FILES)
        if upload_file_from.is_valid():
            file = request.FILES['file']
            # >>>
            wb = openpyxl.load_workbook(file)
            sheet = wb.active
            testName = sheet.cell(row=1, column=1).value
            testDescriptionShort = sheet.cell(row=2, column=1).value
            testDescriptionLong = sheet.cell(row=3, column=1).value
            QuestionsArr = []
            Answers = [[0 for j in range(0)] for i in range(get_nonempty_rows_in_first_column(wb.worksheets[0]))]
            Corrects = [[0 for j in range(0)] for i in range(get_nonempty_rows_in_first_column(wb.worksheets[0]))]

            for row in range(get_first_row_by_value(wb.worksheets[0], 1), get_last_used_row(wb.worksheets[0]) + 1):
                if sheet.cell(row, 1).value is not None:
                    QuestionsArr.append(sheet.cell(row, 2).value)
                else:
                    Answers[len(QuestionsArr) - 1].append(sheet.cell(row, 2).value)
                    if sheet.cell(row, 3).value == 0:
                        Corrects[len(QuestionsArr) - 1].append(False)
                    if sheet.cell(row, 3).value == 1:
                        Corrects[len(QuestionsArr) - 1].append(True)

            db_testName = tests(name = testName, description=testName)
            db_testName.save()
            for Question in range(len(QuestionsArr)):
                answer = Question
                db_questions = questions(test_name = testName, name = QuestionsArr[Question], type = 'NONE', order= Question)
                db_questions.save()
                for answer in range(len(Answers[Question])):
                    db_answer = answers(questions_name = QuestionsArr[Question], answer = Answers[Question][answer], correct = Corrects[Question][answer], comments = '', order = answer)
                    db_answer.save()

            return HttpResponse('Тест успешно загружен', status=200)
            # >>>
    else:
        upload_file_from = UploadFileForm()

    context = {
        'form': upload_file_from
    }
    return render(request, 'upload/upload.html', context=context)


import cv2 as cv
import numpy as np
import mediapipe as mp
import math
from PIL import Image
from numpy.lib.function_base import average

LEFT_EYE = [362, 382, 381, 380, 374, 373, 390, 249, 263, 466, 388, 387, 386, 385, 384, 398]
RIGHT_EYE = [33, 7, 163, 144, 145, 153, 154, 155, 133, 173, 157, 158, 159, 160, 161, 246]
# points of iris
RIGHT_IRIS = [474, 475, 476, 477]
LEFT_IRIS = [469, 470, 471, 472]

L_H_LEFT = [33]  # right eye right most landmark
L_H_RIGHT = [133]  # right eye left most landmark
R_H_LEFT = [362]  # left eye right most landmark
R_H_RIGHT = [263]  # left eye left most landmark

R_U_landmark = [386]  # right eye up most landmark
R_D_landmark = [374]  # right eye down most landmark
L_U_landmark = [159]  # left eye up most landmark
L_D_landmark = [145]  # left eye down most landmark

def euclidean_distance(point1, point2):
    """
    calculate euclidean distance between two points
    input: point1, point2 : x and y for each point
    output: distance between point1 and point2

    """
    x1, y1 = point1.ravel()
    x2, y2 = point2.ravel()
    distance = math.sqrt((x2 - x1) ** 2 + (y2 - y1) ** 2)
    return distance

# thresholds represent ratio of distance between center of iris and point
right_thre = 0.42
left_thre = 0.57
up_thre = 0.25
down_thre = 0.58

def iris_position_in_eye(iris_center, right_point, left_point, up_point, down_point):
    """
    iris position :
    is return position of iris center and ratio_horizontal,ratio_vertical
    """

    center_to_right_dist = euclidean_distance(iris_center, right_point)
    center_to_up_dist = euclidean_distance(iris_center, up_point)

    total_distance_horizontal = euclidean_distance(right_point, left_point)
    total_distance_vertical = euclidean_distance(up_point, down_point)

    ratio_horizontal = center_to_right_dist / total_distance_horizontal

    if total_distance_vertical == 0:
        ratio_vertical = 0.1
    else:
        ratio_vertical = center_to_up_dist / total_distance_vertical

    iris_position = ""
    # iris in center horizontal
    if right_thre < ratio_horizontal <= left_thre:
        if ratio_vertical <= up_thre:
            iris_position = "up"
        elif up_thre < ratio_vertical <= down_thre:
            iris_position = "center"
        else:
            iris_position = "down"

    # iris in center vertical
    elif up_thre < ratio_vertical <= down_thre:
        if ratio_horizontal <= right_thre:
            iris_position = "right"
        elif right_thre < ratio_horizontal <= left_thre:
            iris_position = "center"
        else:
            iris_position = "left"

    # iris in right horizontal
    elif ratio_horizontal <= right_thre:
        if ratio_vertical <= up_thre:
            iris_position = "right _ up"
        elif ratio_vertical > down_thre:
            iris_position = "right _ down"

    # iris in left horizontal
    else:
        if ratio_vertical <= up_thre:
            iris_position = "left _ up"
        elif ratio_vertical > down_thre:
            iris_position = "left _ down"

    return iris_position, ratio_horizontal, ratio_vertical

def crop_face_from_frame(face_detection_model, frame):
    height, width, channels = frame.shape
    results = face_detection_model.process(frame)

    if results.detections:
        for detection in results.detections:
            results.detections[0].location_data.relative_bounding_box.xmin
            l, t = normaliz_pixel(detection.location_data.relative_bounding_box.xmin,
                                  detection.location_data.relative_bounding_box.ymin, width, height)
            r = detection.location_data.relative_bounding_box.width * width + l
            b = detection.location_data.relative_bounding_box.height * height + t

            l -= (r - l) * 0.3
            r += (r - l) * 0.3
            t -= (b - t) * 0.3
            b += (b - t) * 0.3

            bbox = ([l, t, r, b])
        frame = Image.fromarray(frame)
        face = frame.crop(bbox)
        frame = np.asarray(face)

    return frame

def detect_iris_on_frame(face_mesh_model, frame):
    with face_mesh_model.FaceMesh(
            max_num_faces=1,
            refine_landmarks=True,
            min_detection_confidence=0.5,
            min_tracking_confidence=0.5
    ) as face_mesh:
        img_h, img_w = frame.shape[:2]
        results = face_mesh.process(frame)
        if results.multi_face_landmarks:
            mesh_points = np.array([np.multiply([p.x, p.y], [img_w, img_h]).astype(int) for p in
                                    results.multi_face_landmarks[0].landmark])
            (l_cx, l_cy), l_radius = cv.minEnclosingCircle(mesh_points[LEFT_IRIS])
            (r_cx, r_cy), r_radius = cv.minEnclosingCircle(mesh_points[RIGHT_IRIS])

            center_left = np.array([l_cx, l_cy], dtype=np.int32)
            center_right = np.array([r_cx, r_cy], dtype=np.int32)

            cv.circle(frame, center_left, int(l_radius), (255, 0, 255), 1, cv.LINE_AA)
            cv.circle(frame, center_right, int(r_radius), (255, 0, 255), 1, cv.LINE_AA)
            cv.circle(frame, mesh_points[R_H_LEFT][0], 1, (255, 255, 255), -1, cv.LINE_AA)
            cv.circle(frame, mesh_points[R_H_RIGHT][0], 1, (0, 255, 255), -1, cv.LINE_AA)
            cv.circle(frame, mesh_points[R_U_landmark][0], 1, (255, 255, 255), -1, cv.LINE_AA)
            cv.circle(frame, mesh_points[R_D_landmark][0], 1, (0, 255, 255), -1, cv.LINE_AA)

            #             time.sleep(0.2)
            #             print("right eye: the most right",mesh_points[R_H_RIGHT][0][1])
            #             print("right eye: the most left",mesh_points[R_H_LEFT][0][1])
            #             print("right eye: the center of iris",center_right[1])

            iris_pos, ratio_h, ratio_v = iris_position_in_eye(center_right, mesh_points[R_H_RIGHT][0],
                                                              mesh_points[R_H_LEFT][0],
                                                              mesh_points[R_U_landmark][0],
                                                              mesh_points[R_D_landmark][0])
            #             print(iris_pos)

            cv.putText(frame, f"Iris pos: {iris_pos} {ratio_h:.2f} {ratio_v:.2f} ", (30, 30),
                       cv.FONT_HERSHEY_PLAIN, 1.2, (0, 255, 0), 1, cv.LINE_AA)
        return frame

def normaliz_pixel(normalized_x, normalized_y, image_width, image_height):
    """
    input:
    x ,y :  both normalized to [0.0, 1.0] by the image width and the image height
    image_width, image_height: represent width and height of the image

    output:
    x , y: represent point in the image by pixels
    """
    x_px = min(math.floor(normalized_x * image_width), image_width - 1)
    y_px = min(math.floor(normalized_y * image_height), image_height - 1)
    return x_px, y_px

def detect_eye_contact_unsupervised():
    """
    # the main method:
    """
    # face detection
    mp_face_detection = mp.solutions.face_detection
    # face mesh
    mp_face_mesh = mp.solutions.face_mesh

    cap = cv.VideoCapture(0)

    with mp_face_detection.FaceDetection(
            model_selection=1,
            min_detection_confidence=0.5) as face_detection:
        while True:
            # Capture frame by frame
            ret, frame = cap.read()
            if not ret:
                break
            # may be using the lane to take frame vertically or horizontally
            # frame = cv.flip(frame,1)

            frame = crop_face_from_frame(face_detection, frame)
            frame = detect_iris_on_frame(mp_face_mesh, frame)

            cv.imshow('img', frame)
            key = cv.waitKey(1)
            if key == ord('q'):
                break

    cap.release()
    cv.destroyAllWindows()

if __name__ == '__main__':
    detect_eye_contact_unsupervised(

    )


#PIC16
#Assignment10M
#Keith Atienza
#Li, Chak Yin (Anders) 704950093

import sys
import socket
from PyQt5.QtCore import Qt, QTimer
from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QTextEdit, QVBoxLayout, QSizePolicy, QLineEdit, QMessageBox
# from PyQt5.QtGui import QPainter, QColor

class ChatWindow(QWidget):

    def __init__(self):
        super(ChatWindow, self).__init__()
        self.initUi()
        self.setServer()

    def initUi(self):
        self.setWindowTitle('Chat App')
        self.resize(350,130)

        self.textLabel = QLabel('started') # to display the message
        self.textBox = QLineEdit(self) #so enter can be pressed

        ###### for appearance only when window is resized #####
        self.layout = QVBoxLayout()
        self.textBox.setMaximumHeight(30)
        self.textBox.setSizePolicy(QSizePolicy(QSizePolicy.Preferred, QSizePolicy.Maximum))
        self.layout.addWidget(self.textLabel)
        self.layout.addWidget(self.textBox)
        self.setLayout(self.layout)
        ##########################################################

        self.timer = QTimer()
        self.timer.setInterval(2000)
        self.timer.timeout.connect(self.receive)
        self.timer.start()

        self.textBox.returnPressed.connect(self.send)

    def setServer(self):
        self.mySocket = socket.socket()
        self.host = '127.0.0.1'
        self.port = 5001

        try:
            self.mySocket.connect((self.host, self.port))
            self.textLabel.setText('client <connected>')
            self.timer.start()

        except:
            self.mySocket = socket.socket()
            self.mySocket.bind((self.host,self.port))
            self.mySocket.listen(1)
            self.mySocket, addr = self.mySocket.accept()
            self.textLabel.setText('server <connected>')
            self.timer.start()

    def receive(self):
        self.mySocket.settimeout(0.25)
        try:
            data = self.mySocket.recv(1024)
            if data == '<connection closed>':
                self.timer.stop()
                self.textBox.returnPressed.disconnect(self.send)
            self.textLabel.setText(data)
        except:
            pass

    def send(self):
        try:
            self.mySocket.send(self.textBox.text())
        except:
            pass

    def closeEvent(self, event):
        self.mySocket.send('<connection closed>')
        self.mySocket.close()

def main():

    app = QApplication(sys.argv)
    ex = ChatWindow()
    ex.show()
    app.exec_()

if __name__ == "__main__":
    main()


import numpy as np
import tensorflow as tf
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
import parameter_initialization as pi
import activation_func as af

NLAYER = 5
NUNIT = [100, 80, 60, 40, 10]
EPOCH = 10
LEARNING_RATE = 0.05
ACTIVATION_FUNC = {'Step': af.step, 'SimpleReturn': af.simplereturn, 'ReLU': af.ReLU, 'Approximated_ReLU': af.Approximated_ReLU, 'Parametric_ReLU': af.Parametric_ReLU, 'ELU': af.ELU, 'sigmoid': af.sigmoid, 'softsign': af.softsign, 'tanh': af.tanh, 'tanhplus': af.tanhplus, 'softsignplus': af.softsignplus}
USE_FUNC = 'ReLU'
PARAMETER_INIT = {'he': pi.he, 'randn': pi.randn, 'xavier': pi.xavier, 'normalized_initialization': pi.ni}
USE_INIT = 'he'
y_lim = (0, 7000)

x = np.random.randn(1000,100)
activations = {}
for i in range(NLAYER):
    if i != 0:
        x = activations[i - 1]

    w = pi.he(NUNIT[i], NUNIT[i+1])
    z = tf.cast(tf.matmul(x, w), tf.float32)
    a = ACTIVATION_FUNC[USE_FUNC](z)
    activations[i] = a

plt.figure(figsize=(18, 4))
for i, a in activations.items():
    plt.subplot(1, len(activations), i + 1)
    plt.title(str(i + 1) + "-layer")
    plt.hist(a.flatten(), 30, range=(0, 1))
    plt.ylim(y_lim)
plt.show()
plt.savefig('../image/' + USE_FUNC + '_' + USE_INIT + '_figure.png')