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

from tensor2tensor.layers import common_attention
from tensor2tensor.layers import common_hparams
from tensor2tensor.layers import common_layers
from tensor2tensor.layers import discretization
from tensor2tensor.layers import latent_layers
from tensor2tensor.layers import modalities
from tensor2tensor.utils import t2t_model

import numpy as np
import tensorflow as tf
import tensorflow_hub as hub
from galflow import convolve

from galaxy2galaxy.layers.image_utils import pack_images

def loglikelihood_fn(xin, yin, features, hparams):
  size = xin.get_shape().as_list()[1]
  if hparams.likelihood_type == 'Fourier':
    # Compute FFT normalization factor
    x = tf.spectral.rfft2d(xin[...,0]) / tf.complex(tf.sqrt(tf.exp(features['ps'])),0.) / size**2 * (2*np.pi)**2
    y = tf.spectral.rfft2d(yin[...,0]) / tf.complex(tf.sqrt(tf.exp(features['ps'])),0.) / size**2 * (2*np.pi)**2

    pz = 0.5 * tf.reduce_sum(tf.abs(x - y)**2, axis=[-1, -2]) #/ size**2
    return -pz
  elif hparams.likelihood_type == 'Pixel':
    # TODO: include per example noise std
    pz = 0.5 * tf.reduce_sum(tf.abs(xin[:,:,:,0] - yin[...,0])**2, axis=[-1, -2]) / hparams.noise_rms**2 #/ size**2
    return -pz
  else:
    raise NotImplementedError

def image_summary(name, image_logits, max_outputs=1, rows=4, cols=4):
  """Helper for image summaries that are safe on TPU."""
  if len(image_logits.get_shape()) != 4:
    tf.logging.info("Not generating image summary, maybe not an image.")
    return
  return tf.summary.image(name, pack_images(image_logits, rows, cols),
      max_outputs=max_outputs)

def autoencoder_body(self, features):
  """ Customized body function for autoencoders acting on continuous images.
  This is based on tensor2tensor.models.research.AutoencoderBasic.body
  and should be compatible with most derived classes.

  The original autoencoder class relies on embedding the channels to a discrete
  vocabulary and defines the loss on that vocab. It's cool and all, but here we
  prefer expressing the reconstruction loss as an actual continuous likelihood
  function.
  """
  hparams = self.hparams
  is_training = hparams.mode == tf.estimator.ModeKeys.TRAIN

  output_activation = tf.nn.softplus if hparams.output_activation == 'softplus' else None
  input_shape =  [None, ] + common_layers.shape_list(features["inputs"])[1:]

  if hparams.mode == tf.estimator.ModeKeys.PREDICT:
    # In predict mode, we also define TensorFlow Hub modules for all pieces of
    # the autoencoder
    if hparams.encode_psf and 'psf' in features:
      psf_shape =  [None, ] + common_layers.shape_list(features["psf"])[1:]
    # First build encoder spec
    def make_model_spec():
      input_layer = tf.placeholder(tf.float32, shape=input_shape)
      x = self.embed(tf.expand_dims(input_layer, -1))
      x, encoder_layers = self.encoder(x)
      b, b_loss = self.bottleneck(x)
      hub.add_signature(inputs=input_layer, outputs=b)

    def make_model_spec_psf():
      input_layer = tf.placeholder(tf.float32, shape=input_shape)
      psf_layer = tf.placeholder(tf.float32, shape=psf_shape)
      x = self.embed(tf.expand_dims(input_layer, -1))

      # If we have access to the PSF, we add this information to the encoder
      if hparams.encode_psf and 'psf' in features:
        psf_image = tf.expand_dims(tf.signal.irfft2d(tf.cast(psf_layer[...,0], tf.complex64)), axis=-1)
        # Roll the image to undo the fftshift, assuming x1 zero padding and x2 subsampling
        psf_image = tf.roll(psf_image, shift=[input_shape[1], input_shape[2]], axis=[1,2])
        psf_image = tf.image.resize_with_crop_or_pad(psf_image, input_shape[1], input_shape[2])
        net_psf = tf.layers.conv2d(psf_image,
                                   hparams.hidden_size // 4, 5,
                                   padding='same', name="psf_embed_1")
        net_psf = common_layers.layer_norm(net_psf, name="psf_norm")
        x, encoder_layers = self.encoder(tf.concat([x, net_psf], axis=-1))
      else:
        x, encoder_layers = self.encoder(x)
      b, b_loss = self.bottleneck(x)
      hub.add_signature(inputs={'input':input_layer, 'psf':psf_layer}, outputs=b)

    spec = hub.create_module_spec(make_model_spec_psf if hparams.encode_psf else make_model_spec, drop_collections=['checkpoints'])
    encoder = hub.Module(spec, name="encoder_module")
    hub.register_module_for_export(encoder, "encoder")

    if hparams.encode_psf:
      code = encoder({'input':features["inputs"], 'psf': features['psf']})
    else:
      code = encoder(features["inputs"])
    b_shape = [None, ] + common_layers.shape_list(code)[1:]
    res_size = self.hparams.hidden_size * 2**self.hparams.num_hidden_layers
    res_size = min(res_size, hparams.max_hidden_size)

    # Second build decoder spec
    def make_model_spec():
      input_layer = tf.placeholder(tf.float32, shape=b_shape)
      x = self.unbottleneck(input_layer, res_size)
      x = self.decoder(x, None)
      reconstr = tf.layers.dense(x, self.num_channels, name="autoencoder_final",
                                 activation=output_activation)
      hub.add_signature(inputs=input_layer, outputs=reconstr)
      hub.attach_message("stamp_size", tf.train.Int64List(value=[hparams.problem_hparams.img_len]))
      hub.attach_message("pixel_size", tf.train.FloatList(value=[hparams.problem_hparams.pixel_scale]))
    spec = hub.create_module_spec(make_model_spec, drop_collections=['checkpoints'])
    decoder = hub.Module(spec, name="decoder_module")
    hub.register_module_for_export(decoder, "decoder")

    reconstr = decoder(code)
    return reconstr , {"bottleneck_loss": 0.0}

  encoder_layers = None
  self.is1d = hparams.sample_width == 1
  if (hparams.mode != tf.estimator.ModeKeys.PREDICT
      or self._encode_on_predict):
    labels = features["targets_raw"]
    labels_shape = common_layers.shape_list(labels)

    shape = common_layers.shape_list(labels)
    with tf.variable_scope('encoder_module'):
      x = self.embed(tf.expand_dims(labels, -1))

    if shape[2] == 1:
      self.is1d = True

    # Run encoder.
    with tf.variable_scope('encoder_module'):
      # If we have access to the PSF, we add this information to the encoder
      # Note that we only support single band images so far...
      if hparams.encode_psf and 'psf' in features:
        psf_image = tf.expand_dims(tf.signal.irfft2d(tf.cast(features['psf'][...,0], tf.complex64)), axis=-1)
        # Roll the image to undo the fftshift, assuming x1 zero padding and x2 subsampling
        psf_image = tf.roll(psf_image, shift=[input_shape[1], input_shape[2]], axis=[1,2])
        psf_image = tf.image.resize_with_crop_or_pad(psf_image, input_shape[1], input_shape[2])
        net_psf = tf.layers.conv2d(psf_image,
                                   hparams.hidden_size // 4, 5,
                                   padding='same', name="psf_embed_1")
        net_psf = common_layers.layer_norm(net_psf, name="psf_norm")
        x, encoder_layers = self.encoder(tf.concat([x, net_psf], axis=-1))
      else:
        x, encoder_layers = self.encoder(x)

    # Bottleneck.
    with tf.variable_scope('encoder_module'):
      b, b_loss = self.bottleneck(x)

    xb_loss = 0.0
    b_shape = common_layers.shape_list(b)
    self._cur_bottleneck_tensor = b
    res_size = common_layers.shape_list(x)[-1]
    with tf.variable_scope('decoder_module'):
      b = self.unbottleneck(b, res_size)
    if not is_training:
      x = b
    else:
      l = 2**hparams.num_hidden_layers
      warm_step = int(hparams.bottleneck_warmup_steps * 0.25 * l)
      nomix_p = common_layers.inverse_lin_decay(warm_step) + 0.01
      if common_layers.should_generate_summaries():
        tf.summary.scalar("nomix_p_bottleneck", nomix_p)
      rand = tf.random_uniform(common_layers.shape_list(x))
      # This is the distance between b and x. Having this as loss helps learn
      # the bottleneck function, but if we back-propagated to x it would be
      # minimized by just setting x=0 and b=0 -- so we don't want too much
      # of the influence of this, and we stop-gradient to not zero-out x.
      x_stop = tf.stop_gradient(x)
      xb_loss = tf.reduce_mean(tf.reduce_sum(
          tf.squared_difference(x_stop, b), axis=-1))
      # To prevent this loss from exploding we clip at 1, but anneal clipping.
      clip_max = 1.0 / common_layers.inverse_exp_decay(
          warm_step, min_value=0.001)
      xb_clip = tf.maximum(tf.stop_gradient(xb_loss), clip_max)
      xb_loss *= clip_max / xb_clip
      x = tf.where(tf.less(rand, nomix_p), b, x)
  else:
    if self._cur_bottleneck_tensor is None:
      b = self.sample()
    else:
      b = self._cur_bottleneck_tensor
    self._cur_bottleneck_tensor = b
    res_size = self.hparams.hidden_size * 2**self.hparams.num_hidden_layers
    res_size = min(res_size, hparams.max_hidden_size)

    with tf.variable_scope('decoder_module'):
      x = self.unbottleneck(b, res_size)

  # Run decoder.
  with tf.variable_scope('decoder_module'):
    x = self.decoder(x, encoder_layers)

  # Cut to the right size and mix before returning.
  res = x
  if hparams.mode != tf.estimator.ModeKeys.PREDICT:
    res = x[:, :shape[1], :shape[2], :]

  with tf.variable_scope('decoder_module'):
    reconstr = tf.layers.dense(res, self.num_channels, name="autoencoder_final",
                               activation=output_activation)

  # We apply an optional apodization of the output before taking the
  if hparams.output_apodization > 0:
    nx = reconstr.get_shape().as_list()[1]
    alpha = 2 * hparams.output_apodization / nx
    from scipy.signal.windows import tukey
    # Create a tukey window
    w = tukey(nx, alpha)
    w = np.outer(w,w).reshape((1, nx, nx,1)).astype('float32')
    # And penalize non zero things at the border
    apo_loss = tf.reduce_mean(tf.reduce_sum(((1.- w)*reconstr)**2, axis=[1,2,3]))
  else:
    w = 1.0
    apo_loss = 0.

  # We apply the window
  reconstr = reconstr * w

  # Optionally regularizes further the output
  # Anisotropic TV:
  tv = tf.reduce_mean(tf.image.total_variation(reconstr))
  # Smoothed Isotropic TV:
  #im_dx, im_dy = tf.image.image_gradients(reconstr)
  #tv = tf.reduce_sum(tf.sqrt(im_dx**2 + im_dy**2 + 1e-6), axis=[1,2,3])
  #tv = tf.reduce_mean(tv)

  # Apply channel-wise convolution with the PSF if requested
  # TODO: Handle multiple bands
  if hparams.apply_psf and 'psf' in features:
    if self.num_channels > 1:
      raise NotImplementedError

    reconstr = convolve(reconstr, tf.cast(features['psf'][...,0], tf.complex64),
                        zero_padding_factor=1)

  # Losses.
  losses = {
      "bottleneck_extra": b_loss,
      "bottleneck_l2": hparams.bottleneck_l2_factor * xb_loss,
      "total_variation": hparams.total_variation_loss * tv,
      "apodization_loss": hparams.apodization_loss * apo_loss,
  }

  loglik = loglikelihood_fn(labels, reconstr, features, hparams)
  targets_loss = tf.reduce_mean(- loglik)

  tf.summary.scalar("negloglik", targets_loss)
  tf.summary.scalar("bottleneck_loss", b_loss)

  # Compute final loss
  losses["training"] = targets_loss + b_loss + hparams.bottleneck_l2_factor * xb_loss + hparams.total_variation_loss * tv +  hparams.apodization_loss * apo_loss
  logits = tf.reshape(reconstr, labels_shape)

  image_summary("ae", reconstr)
  image_summary("input", labels)

  return logits, losses


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

import requests

from bs4 import BeautifulSoup
# 传入URL
r = requests.get('https://www.csdn.net/')

# 解析URL
soup = BeautifulSoup(r.text, 'html.parser')
content_list = soup.find_all('div', attrs={'class': 'title'})
comment_count = soup.find_all('dl', attrs={'class': 'list_userbar'})

for content in content_list:
    print(content.h2.a.text)


import threading
import time
from datetime import datetime

class StartOverException(Exception):
    """Raise to restart the tails thread loop"""
    pass

class FluffyTailsThread(threading.Thread):
    def __init__(self, bot_object, bot):
        threading.Thread.__init__(self)
        # self.new_reminder = threading.Event()
        self.bot = bot
        self.bot_object = bot_object

    def run(self):
        while True:
            try:
                try:
                    time_to_wait, affected_person = self._time_until_expiry()
                except (KeyError, TypeError, IndexError):
                    raise StartOverException
                time.sleep(1)
                for second in range(0, time_to_wait + 1):
                    time.sleep(1)
                    if second >= time_to_wait:
                        self.bot_object._clear_tails_effect(affected_person)
                        raise StartOverException
                raise StartOverException
            except StartOverException as ex:
                time.sleep(1)
                continue

    def _time_until_expiry(self):
        current_time = datetime.now()
        try:
            tail_effects = self.bot_object._Plugin__db_get(['fluffy_tails'])
            affected_person = self._get_earliest_expiry()
            earliest_expiry_time = datetime.strptime(tail_effects[affected_person]['expiration_date'], '%Y-%m-%d %H:%M:%S.%f')
        except (KeyError, IndexError, TypeError):
            return
        if current_time < earliest_expiry_time:
            difference = earliest_expiry_time - current_time
            time_to_wait = int(difference.total_seconds())
        else:
            time_to_wait = 1
        return time_to_wait, affected_person

    def _get_earliest_expiry(self):
        tail_effects = self.bot_object._Plugin__db_get(['fluffy_tails'])
        earliest_expiry = '2200-01-01 00:00:00.000000'
        earliest_username_key = None
        for i, user in enumerate(tail_effects):
            if tail_effects[user]['expiration_date'] < earliest_expiry:
                earliest_username_key = user
                earliest_expiry = tail_effects[user]['expiration_date']
        return earliest_username_key


import pandas as pd
import numpy as np
import os
import warnings
from .dataset import Dataset
from .dataframe_tools import *
from .exceptions import FailedReindexWarning, ReindexMapError

class Endometrial(Dataset):

    def __init__(self, version="latest", no_internet=False):
        """Load all of the endometrial dataframes as values in the self._data dict variable, with names as keys, and format them properly.

        Parameters:
        version (str, optional): The version number to load, or the string "latest" to just load the latest building. Default is "latest".
        no_internet (bool, optional): Whether to skip the index update step because it requires an internet connection. This will be skipped automatically if there is no internet at all, but you may want to manually skip it if you have a spotty internet connection. Default is False.
        """

        # Set some needed variables, and pass them to the parent Dataset class __init__ function

        valid_versions = ["2.1", "2.1.1"] # This keeps a record of all versions that the code is equipped to handle. That way, if there's a new data release but they didn't update their package, it won't try to parse the new data version it isn't equipped to handle.

        data_files = {
            "2.1": [
                "acetylproteomics.cct.gz",
                "clinical.txt",
                "CNA.cct.gz",
                "definitions.txt",
                "miRNA.cct.gz",
                "phosphoproteomics_gene.cct.gz",
                "phosphoproteomics_site.cct.gz",
                "proteomics.cct.gz",
                "somatic_binary.cbt.gz",
                "somatic.maf.gz",
                "transcriptomics_circular.cct.gz",
                "transcriptomics_linear.cct.gz"],
            "2.1.1": [
                "acetylproteomics.cct.gz",
                "clinical.txt",
                "CNA.cct.gz",
                "definitions.txt",
                "miRNA.cct.gz",
                "phosphoproteomics_site.cct.gz",
                "proteomics.cct.gz",
                "somatic_binary.cbt.gz",
                "somatic.maf.gz",
                "transcriptomics_circular.cct.gz",
                "transcriptomics_linear.cct.gz",
                "UCEC_followup_9_12.xlsx"],
        }

        super().__init__(cancer_type="endometrial", version=version, valid_versions=valid_versions, data_files=data_files, no_internet=no_internet)

        # Load the data files into dataframes in the self._data dict
        loading_msg = f"Loading {self.get_cancer_type()} v{self.version()}"
        for file_path in self._data_files_paths:

            # Print a loading message. We add a dot every time, so the user knows it's not frozen.
            loading_msg = loading_msg + "."
            print(loading_msg, end='\r')

            path_elements = file_path.split(os.sep) # Get a list of the levels of the path
            file_name = path_elements[-1] # The last element will be the name of the file
            df_name = file_name.split(".")[0] # Dataframe name will be the first section of file name; i.e. proteomics.txt.gz becomes proteomics

            # Load the file, based on what it is
            if file_name == "clinical.txt":
                # Fix for reading error on clinical.txt:
                with open(file_path, "r", errors="ignore") as clinical_file:
                    df = pd.read_csv(clinical_file, sep="\t", index_col=0)
                df = df.sort_index()
                self._data[df_name] = df # Maps dataframe name to dataframe

            elif file_name == "definitions.txt":
                with open(file_path, "r") as definitions_file:
                    for line in definitions_file.readlines():
                        line = line.strip()
                        line = line.split("\t")
                        term = line[0]
                        definition = line[1]
                        self._definitions[term] = definition

            elif file_name == "somatic.maf.gz":
                # Make changes here:
                # Parse the MAF file into a dataframe
                # Here with the following change, nothing gets removed

                df = pd.read_csv(file_path, sep = "\t")

###################################################################################
                self._maf = df  # Directly put in the read file
###################################################################################

                split_barcode = df["Tumor_Sample_Barcode"].str.split("_", n=1, expand=True) # The first part of the barcode is the patient id, which we need want to make the index
                df["Tumor_Sample_Barcode"] = split_barcode[0]
                df = df[["Tumor_Sample_Barcode","Hugo_Symbol","Variant_Classification","HGVSp_Short"]]
                df = df.rename({"Tumor_Sample_Barcode":"Patient_ID","Hugo_Symbol":"Gene","Variant_Classification":"Mutation","HGVSp_Short":"Location"}, axis='columns')
                df = df.sort_values(by=["Patient_ID", "Gene"])
                df = df.set_index("Patient_ID")
                self._data["somatic_mutation"] = df # Maps dataframe name to dataframe

            elif file_name == "acetylproteomics.cct.gz" or file_name == "phosphoproteomics_site.cct.gz":
                df = pd.read_csv(file_path, sep = "\t", index_col=0)
                df.index = df.index.str.rsplit('-', n=1, expand=True) # Separate the index into a multiindex where the 1st level is the gene, and 2nd is the site
                df.index = df.index.set_names(["Name", "Site"]) # Properly name the levels
                df = df.sort_index()
                df = df.transpose()
                self._data[df_name] = df # Maps dataframe name to dataframe

            elif file_name == 'UCEC_followup_9_12.xlsx' and self._version == "2.1.1":
                df = pd.read_excel(file_path)

                # Replace redundant values for 'not reported' with NaN
                nan_equivalents = ['Not Reported/ Unknown', 'Reported/ Unknown', 'Not Applicable', 'na', 'unknown',
                    'Not Performed', 'Unknown tumor status', 'Unknown', 'Unknown Tumor Status', 'Not specified']

                df = df.replace(nan_equivalents, np.nan)

                # Rename, set, and sort index
                df = df.rename(columns={'Case ID': 'Patient_ID'})
                df = df.set_index("Patient_ID")
                df = df.sort_index()

                self._data["followup"] = df

            else:
                df = pd.read_csv(file_path, sep="\t", index_col=0)
                df = df.transpose()
                df = df.sort_index()
                self._data[df_name] = df # Maps dataframe name to dataframe

        print(' ' * len(loading_msg), end='\r') # Erase the loading message
        formatting_msg = "Formatting dataframes..."
        print(formatting_msg, end='\r')

        # Separate out clinical, derived_molecular, and experimental_design dataframes
        all_clinical = self._data["clinical"]
        clinical = all_clinical[[
            'Proteomics_Participant_ID', 'Case_excluded',  'Proteomics_Tumor_Normal',  'Country',
            'Histologic_Grade_FIGO', 'Myometrial_invasion_Specify', 'Histologic_type', 'Treatment_naive', 'Tumor_purity',
            'Path_Stage_Primary_Tumor-pT', 'Path_Stage_Reg_Lymph_Nodes-pN', 'Clin_Stage_Dist_Mets-cM', 'Path_Stage_Dist_Mets-pM',
            'tumor_Stage-Pathological', 'FIGO_stage', 'LVSI', 'BMI', 'Age', 'Diabetes', 'Race', 'Ethnicity', 'Gender', 'Tumor_Site',
            'Tumor_Site_Other', 'Tumor_Focality', 'Tumor_Size_cm',   'Num_full_term_pregnancies']]
        clinical = clinical.rename(columns={"Proteomics_Participant_ID":"Patient_ID"})
        self._data["clinical"] = clinical

        derived_molecular = all_clinical.drop(['Proteomics_Participant_ID', 'Case_excluded',  'Proteomics_Tumor_Normal',  'Country',
            'Histologic_Grade_FIGO', 'Myometrial_invasion_Specify', 'Histologic_type', 'Treatment_naive', 'Tumor_purity',
            'Path_Stage_Primary_Tumor-pT', 'Path_Stage_Reg_Lymph_Nodes-pN', 'Clin_Stage_Dist_Mets-cM', 'Path_Stage_Dist_Mets-pM',
            'tumor_Stage-Pathological', 'FIGO_stage', 'LVSI', 'BMI', 'Age', 'Diabetes', 'Race', 'Ethnicity', 'Gender', 'Tumor_Site',
            'Tumor_Site_Other', 'Tumor_Focality', 'Tumor_Size_cm',   'Num_full_term_pregnancies',
            'Proteomics_TMT_batch', 'Proteomics_TMT_plex', 'Proteomics_TMT_channel', 'Proteomics_Parent_Sample_IDs',
            'Proteomics_Aliquot_ID', 'Proteomics_OCT', 'WXS_normal_sample_type', 'WXS_normal_filename', 'WXS_normal_UUID', 'WXS_tumor_sample_type', 'WXS_tumor_filename',
            'WXS_tumor_UUID', 'WGS_normal_sample_type', 'WGS_normal_UUID', 'WGS_tumor_sample_type', 'WGS_tumor_UUID', 'RNAseq_R1_sample_type', 'RNAseq_R1_filename', 'RNAseq_R1_UUID',
            'RNAseq_R2_sample_type', 'RNAseq_R2_filename', 'RNAseq_R2_UUID', 'miRNAseq_sample_type', 'miRNAseq_UUID', 'Methylation_available', 'Methylation_quality'], axis=1)
        derived_molecular = derived_molecular.rename(columns={"JAK1_Mutation":"JAK1_Mutation_status"})
        self._data["derived_molecular"] = derived_molecular

        experimental_design = all_clinical[['Proteomics_TMT_batch', 'Proteomics_TMT_plex', 'Proteomics_TMT_channel', 'Proteomics_Parent_Sample_IDs',
            'Proteomics_Aliquot_ID', 'Proteomics_OCT', 'WXS_normal_sample_type', 'WXS_normal_filename', 'WXS_normal_UUID', 'WXS_tumor_sample_type', 'WXS_tumor_filename',
            'WXS_tumor_UUID', 'WGS_normal_sample_type', 'WGS_normal_UUID', 'WGS_tumor_sample_type', 'WGS_tumor_UUID', 'RNAseq_R1_sample_type', 'RNAseq_R1_filename', 'RNAseq_R1_UUID',
            'RNAseq_R2_sample_type', 'RNAseq_R2_filename', 'RNAseq_R2_UUID', 'miRNAseq_sample_type', 'miRNAseq_UUID', 'Methylation_available', 'Methylation_quality']]
        self._data["experimental_design"] = experimental_design

        # Drop all excluded samples from the dataset. They were excluded due to poor sample quality, etc.
        clinical = self._data["clinical"]
        cases_to_drop = clinical[clinical["Case_excluded"] == "Yes"].index.union(clinical[clinical["Case_excluded"] == "Yes"]["Patient_ID"])

        for name in self._data.keys(): # Loop over the keys so we can alter the values without any issues
            df = self._data[name]
            df_filtered = df.drop(index=cases_to_drop, errors="ignore")
            self._data[name] = df_filtered

        # Drop Case_excluded column from clinical, now that we've dropped all excluded cases in the dataset.
        clinical = self._data["clinical"]
        clinical = clinical.drop(columns=["Case_excluded"])

        # Add a Sample_Tumor_Normal column to the clinical dataframe, with just "Tumor" or "Normal" values (unlike the Proteomics_Tumor_Normal column, which gives the different types of normal samples)
        raw_map = clinical["Proteomics_Tumor_Normal"]
        parsed_map = raw_map.where(raw_map == "Tumor", other="Normal") # Replace various types of normal (Adjacent_normal, Myometrium_normal, etc.) with just "Normal"
        clinical.insert(1, "Sample_Tumor_Normal", parsed_map)

        # Mark the Patient_IDs of the normal samples by appending a ".N" to them
        clinical["Patient_ID"] = clinical["Patient_ID"].where(clinical["Sample_Tumor_Normal"] == "Tumor", other=clinical["Patient_ID"] + ".N")

        # Save our new and improved clinical dataframe!
        self._data["clinical"] = clinical

        # Sort CNV dataframe columns alphabetically
        cna = self._data["CNA"]
        cna_sorted = cna.sort_index(axis=1)
        self._data["CNA"] = cna_sorted

        # Fix dataframe names
        rename_dict = { # Keys are old names, values are new names
            "CNA":"CNV",
            "transcriptomics_linear":"transcriptomics",
            "transcriptomics_circular":"circular_RNA",
            "phosphoproteomics_site":"phosphoproteomics",
            "somatic_binary":"somatic_mutation_binary",
            }
        for old, new in rename_dict.items():
            self._data[new] = self._data[old]
            del self._data[old]

        # Call a function from dataframe_tools.py to reindex all the dataframes with sample IDs instead of patient IDs
        # Skip the followup and somatic_mutation dataframes because they're already indexed with Patient_IDs
        sample_id_to_patient_id_map = self._data["clinical"]["Patient_ID"]
        self._data = reindex_all_sample_id_to_patient_id(self._data, sample_id_to_patient_id_map, skip=["followup", "somatic_mutation"])

        # We no longer need the Patient_ID column in the clinical dataframe, because it's in the index. So we'll remove it.
        clinical = self._data["clinical"]
        clinical = clinical.drop(columns="Patient_ID")
        self._data["clinical"] = clinical

        # Get a union of all dataframes' indices, with duplicates removed
        # Exclude the followup dataframe because it has samples from a different cohort that aren't included anywhere else in the dataset
        master_index = unionize_indices(self._data, exclude="followup")

        # Use the master index to reindex the clinical dataframe, so the clinical dataframe has a record of every sample in the dataset.
        clinical = self._data["clinical"]
        clinical = clinical.reindex(master_index)
        self._data['clinical'] = clinical

        # Call function from dataframe_tools.py to sort all tables first by sample status, and then by the index
        self._data = sort_all_rows(self._data)

        # Call function from dataframe_tools.py to standardize the names of the index and column axes
        self._data = standardize_axes_names(self._data)

        print(" " * len(formatting_msg), end='\r') # Erase the formatting message

    def how_to_cite(self):
        return super().how_to_cite(cancer_type='endometrial carcinoma (uterine)', pmid=32059776)


import openai

openai.api_key = "Fill your API key here"

days = 4
place = "Paris"

prompt = f"Make me a travel plan for {days} days to {place}"

response = openai.Completion.create(
    engine="text-davinci-002",
    prompt=prompt,
    max_tokens=4000,
    temperature=0.6,
    n=1,
    stop=None
)

print(str(response["choices"][0]["text"]))


from multiprocessing.managers import BaseManager
import random
import time
import queue

BaseManager.register('get_task_queue')
BaseManager.register('get_result_queue')

server_addr = '127.0.0.1'
manager = BaseManager(address=(server_addr,5000),authkey=b'abc')

manager.connect()

task = manager.get_task_queue()
result = manager.get_result_queue()

for i in range(10):
    try:
        temp = task.get(timeout=1)
        ans = temp * 2
        print("%s *2 = %s" % (temp,ans))
        time.sleep(1)
        result.put(ans)
    except queue.Queue().empty():
        print("task is empty")
print("Test Done....")


from flask import Flask, jsonify, request
import json
import numpy as np
from datetime import datetime

app = Flask(__name__)
@app.route('/', methods=['GET', 'POST'])
def filtering_api():
    if request.method == 'GET':
        with open('data.txt', 'r') as f:
            data = f.read().splitlines()
        data = [json.loads(item) for item in data]
        return jsonify({"response": data}), 200
    elif request.method == 'POST':
         if request.is_json:
            data = json.loads(request.data)
            main, input = data.get('main'), data.get('input')
            x1, y1 = main["x"], main["y"]
            x2 , y2 = x1 + main["width"], y1 + main["height"]
            out_file = open("data.txt", "a")
            for candid in input:
                x, y, w, h = candid["x"], candid["y"], candid["width"], candid["height"]

                if ((1 + np.min([x2, x+w])) - np.max([x1, x]) > 0) and ((1 + np.min([y2,y+h]) - np.max([y1, y])) > 0) :
                    candid["time"] = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
                    out_file.write(json.dumps(candid)+"\n")

            out_file.close()
            return jsonify({"status": "saved"}), 201
         else:
             return "Content type is not supported. It must be json.", 415

if __name__ == '__main__':
    # app.run(debug=True)
    # To make the Flask app accessible from outside the container, you need to modify the app to listen on 0.0.0.0.
    app.run(host='0.0.0.0', debug=True)


import logging
import socket
import threading

from src import (
    parse_args,
    TestObject,
    TestCase,
    TEST_CASE_TO_TEST,
    OBJECTS,
)

def perform_calculations(test_object, test_case):
    test = TEST_CASE_TO_TEST[test_case]()
    test.run(OBJECTS[TestObject(test_object)])
    return str(test.report)

def handle_client_request(args, server_socket, client_address, data):
    try:
        logging.debug(f"Установлено соединение с клиентом {client_address}")
        command = data.split()
        logging.debug(command)

        assert "/get_result" == command[0]
        assert command[1] in [obj.value for obj in TestObject]
        for c in command[2:]:
            if c == "all":
                continue
            assert c in [case.value for case in TestCase]

        tests = (
            [item for item in TestCase]
            if "all" in command[2:]
            else [TestCase(item) for item in command[2:]]
        )

        if TestCase(args.format) not in tests:
            server_socket.sendto("".encode("utf-8"), client_address)
            return
        tests = [TestCase(args.format)]
        logging.info(args.format)

        logging.debug(tests)
        result = []
        for test in tests:
            res = perform_calculations(command[1], test)
            result.append(res)
            logging.debug(res)

        server_socket.sendto("\n".join(result).encode("utf-8"), client_address)

    except Exception as e:
        server_socket.sendto(str(e).encode("utf-8"), client_address)

def start_server(args):
    logging.basicConfig(
        level=logging.INFO,
        format="%(message)s",
    )
    host = f"server-{args.format}"
    server_address = (host, 2924)
    logging.debug(server_address)
    server_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    server_socket.bind(server_address)
    logging.debug(f"Сервер запущен на {server_address[0]}:{server_address[1]}")

    while True:
        data, client_address = server_socket.recvfrom(1024)
        data = data.decode()
        logging.debug(client_address)
        handle_client_request(args, server_socket, client_address, data)
    server_socket.close()


#!/bin/env python
# This is a script to use descriptive statistics calculated previously to produce presentation-quality graphics
# and save them as PNG files for use in a powerpoint presentation
#   written by Justin Meyer
#   last edited 2022-05-04
#

# import required modules
import pandas as pd
import matplotlib.pyplot as plt

import os
os.chdir('c:/Users/justi/OneDrive/Documents/GitHub/final-project-meyer443')

def ReadData( fileName ):
    """This function takes a filename as input, and returns a dataframe with
    raw data read from that file in a Pandas DataFrame."""

    # open and read the file
    DataDF = pd.read_csv( fileName, sep=',' )
    return DataDF

def Plot_Temp(DataDF, outFileName):
    plt.scatter(DataDF["Date"], DataDF["temp"], marker=".") # scatter date vs temp
    plt.xlabel('Date')
    plt.xticks([]) # can't read x axis with overlapping dates
    plt.ylabel('Average Daily Temperature (C)')
    plt.savefig('Daily_Temp.png', dpi=96) # save the plot as PNG with 96 dpi
    plt.close()

# the following condition checks whether we are running as a script, in which
# case run the test code, otherwise functions are being imported so do not.
# put the main routines from your code after this conditional check.

if __name__ == '__main__':

    fileName = "Daily_Temperature_Averages.csv"

    DataDF = ReadData( fileName )

# Figure generation
    outTempName = "Daily_Temp.png"
    Plot_Temp( DataDF, outTempName )


"""
Driver for PES-Learn
"""
import timeit
import sys
import os
import json
from six.moves import input
from collections import OrderedDict
import peslearn
import numpy as np
import pandas as pd

with open('input.dat', 'r') as f:
    input_string = f.read()

input_obj = peslearn.InputProcessor(input_string)

if input_obj.keywords['mode'] == None:
    text = input("Do you want to 'generate' data, 'parse' data, or 'learn'?")
    text = text.strip()

else:
    text = input_obj.keywords['mode']

start = timeit.default_timer()

if text == 'generate' or text == 'g':
    mol = peslearn.datagen.Molecule(input_obj.zmat_string)
    config = peslearn.datagen.ConfigurationSpace(mol, input_obj)
    template_obj = peslearn.datagen.Template("./template.dat")
    config.generate_PES(template_obj)
    print("Data generation finished in {} seconds".format(round((timeit.default_timer() - start),2)))

if text == 'parse' or text == 'p':
    mol = peslearn.datagen.Molecule(input_obj.zmat_string)
    peslearn.utils.parsing_helper.parse(input_obj, mol)

if text == 'learn' or text == 'l':
    if input_obj.keywords['use_pips'] == 'true':
        mol = peslearn.datagen.Molecule(input_obj.zmat_string)
    if input_obj.keywords["ml_model"] == 'gp':
        if input_obj.keywords['use_pips'] == 'true':
            gp = peslearn.ml.GaussianProcess(input_obj.keywords["pes_name"], input_obj, molecule_type=mol.molecule_type)
        else:
            gp = peslearn.ml.GaussianProcess(input_obj.keywords["pes_name"], input_obj)
        gp.optimize_model()

    if input_obj.keywords["ml_model"] == 'nn':
        if input_obj.keywords['use_pips'] == 'true':
            nn = peslearn.ml.NeuralNetwork(input_obj.keywords["pes_name"], input_obj, molecule_type=mol.molecule_type)
        else:
            nn = peslearn.ml.NeuralNetwork(input_obj.keywords["pes_name"], input_obj)
        nn.optimize_model()

stop = timeit.default_timer()
print("Total run time: {} seconds".format(round(stop - start,2)))


import numpy as np
import matplotlib.pyplot as plt

# Color
# https://matplotlib.org/stable/gallery/color/named_colors.html

#https://www.yutaka-note.com/entry/matplotlib_subplots

x = np.linspace(-3,3)
y1 = x**2
y2 = x
fig, ax = plt.subplots(1, 2, squeeze=False,figsize=(8,3),tight_layout=True)
ax[0,0].plot(x, y1,"SteelBlue", linewidth=2)
ax[0,0].plot(x, y2,"LightSteelBlue")
ax[0,0].set_title("A")
ax[0,0].legend(["test1","test2"])
ax[0,0].set_xlabel("x-label")
ax[0,0].set_ylabel("y-label")

ax[0,1].plot(x, y2)
ax[0,1].set_title("B")
ax[0,1].legend(["test2"])
ax[0,1].set_xticks(list(range(-3,8,2)))
ax[0,1].set_xlim(-3, 10)

fig.autofmt_xdate()
plt.show()


# -*- coding: utf-8 -*-
from flask import Flask, render_template, request
from webapp.forms import db, ModelPosts, ModelComments, ModelTags
from read_db import *

# импорт семантического анализа
from semantic import semantic_res

def f_toxic_vals(v0, v1, v2):
    # processing results function for provide into web-page
    # функция предназначена для передачи данных в index.html в виде частей кода с целью отризовки круговых диаграмм
    # эмециональности

    toxic_rslts = [[], [], []]

    v0 = round(v0)
    v1 = round(v1)
    v2 = round(v2)

    over50 = 'progress-circle over50 p'
    under50 = 'progress-circle p'
    toxic_rslts[0] = [v0, over50 + str(v0) if v0 >= 50 else under50 + str(v0)]
    toxic_rslts[1] = [v1, over50 + str(v1) if v1 >= 50 else under50 + str(v1)]
    toxic_rslts[2] = [v2, over50 + str(v2) if v2 >= 50 else under50 + str(v2)]

    return toxic_rslts

def create_app():  # фабрика Flask

    app = Flask(__name__)
    app.config.from_pyfile('config.py')
    db.init_app(app)

    @app.route('/', methods=['POST', 'GET'])
    def index():

        # временные текстовые константы для страницы:
        v0, v1, v2 = 5, 10, 50

        title = 'Введите запрос'
        flag = False
        result = ''
        if request.method == 'POST':
            answer = request.form['text']
            answer = answer.lower()
            # information about answer
            #
            print('#'*100)
            print(type(answer))
            print(answer)
            print('#' * 100)

            result = '----- no answer -----'
            # флаг для вызова отображения результатов на странице html
            flag = True
            try:
                # function for getting data by input value
                # вызов функции для извлечениия и предобработки данных для предоставления ее в семантический анализатор
                result = get_data(answer)

            except:         # использовать без параметров не реккомендуется, но тут для красоты сделано исключение
                result = '----- no answer -----'
            finally:
                flag = True
                # печать результатов в стадии отладки приложения
                # for r in result:
                #     print(type(r))
                #     print(r)

                result_in_digits = semantic_res(result)

                v0 = result_in_digits['neg']
                v1 = result_in_digits['neu']
                v2 = result_in_digits['pos']

                # v0 = 10
                # v1 = 20
                # v2 = 30

        if request.method == 'GET':
            return render_template('index.html', title=title, answer='', flag=flag, toxic_vals=f_toxic_vals(v0, v1, v2))

        return render_template('index.html', title=title, answer=result, flag=flag, toxic_vals=f_toxic_vals(v0, v1, v2))

    return app

############################
if __name__ == '__main__':
    create_app().run(port='5000', debug=True)

#export FLASK_APP=webapp_f && export FLASK_ENV=development && flask run
#-h 172.19.65.99


from aiogram import Bot, Dispatcher,executor,types
from token_auth import token
from script import get_user_info,get_user_subs,get_friends_user,get_user_photo
import json

def telegram_bot(token):
    bot  = Bot(token=token,parse_mode=types.ParseMode.HTML)
    dp = Dispatcher(bot=bot)

    #@dp.message_handler(content_types=["text"])
    async def echo(message: types.Message):
        await message.answer("[ 🔧 ] Бот на разработке, но он обязательно оживёт ")
        await message.answer("🧑💻")
        print(f"[+] LOGGING: '{message.text}' от {message.from_user.full_name} ({message.from_user.id})")

    @dp.message_handler(commands=['start'])
    async def send_message(message: types.Message):
        await message.answer("Важная информация: данный бот находится в разработке, поэтому я жду от Вас фидбэк по работе бота в лс @gifgufgaf")
        await message.answer("Вкратце о боте: данный бот берёт данные исключительно из открытых источников (VK, Instagram(пока не добавлено)), поэтому важно, чтобы цели бота были открытыми профилями.")
        print(f"[+] LOGGING: '{message.text}' от {message.from_user.full_name} ({message.from_user.id})")

    @dp.message_handler(commands=['help'])
    async def send_message(message: types.Message):
        await message.answer("Вводи в любом формате (числовой или буквенный) \n\nНе нагружай бота страницами с 500> фото, а то ему станет плохо🥹 ")
        print(f"[+] LOGGING: '{message.text}' от {message.from_user.full_name} ({message.from_user.id})")

    @dp.message_handler(content_types=["text"])

    async def echo(message: types.Message):
        try:
            print(f"[+] LOGGING: '{message.text}' от {message.from_user.full_name} ({message.from_user.id})")
            user_id = message.text
            await message.answer(f"Информация, найденная по айди: {message.text}")
            get_user_info(user_id)
            with open("account_info.json", "r") as read_file:
                data = json.load(read_file)
                first_name = data["first_name"]
                second_name = data["second_name"]
                birth_date = data["birth_date"]
                interests = data["interests"]
                home_town = data["home_town"]
                city = data["city"]
                mobile_phone = data["mobile_phone"]
            read_file.close()

            await message.answer(f"Вот что удалось найти: \n \t 📋 Имя Фамилия: {first_name} {second_name} \n \t 🎉 День рождения: {birth_date} \n \t 😍 Интересы: {interests} \n \t 🏠 Родной город: {home_town} \n \t 🏙 Город: {city} \n \t 📱 Мобильный телефон: {mobile_phone} \n Подробная информация файлами ниже.")
            await message.answer_document(open('account_info.json', 'rb'))
            get_user_subs(user_id)
            await message.answer_document(open('account_subs.json', 'rb'))
            get_friends_user(user_id)
            await message.answer_document(open('account_friends.json', 'rb'))
            await message.answer(f"Раздел с фотографиями на доработке")
            #get_user_photo(user_id)
            #await message.answer_document(open('account_photos.zip', 'rb'))
            #await message.answer_video(open('success.mp4','rb'))
        except:
            await message.answer("Чёт пошло не так, но это обязательно пофиксится, надо просто подождать, правда ведь? ")
            await message.answer("🥺")
            print(f"[!] WARNING: '{message.text}' от {message.from_user.full_name} ({message.from_user.id})")
    executor.start_polling(dispatcher = dp)
if __name__ == "__main__":
    telegram_bot(token)


import sys
from collections import deque
input = sys.stdin.readline
# bfs와 이분탐색 활용문제
# 처음에는 다리무게를 활용하여 도착지에 갈 수 있는 최소 무게를 탐색
# 나중에는 이분탐색을 활용해 도달할 수 있는 무게의 최대값을 탐색

def bfs(w):
    q = deque()
    q.append(S)
    visited = [0] * (N+1)
    visited[S] = 1
    while q:
        A = q.popleft()
        for B, C in G[A]:
            if not visited[B] and w <= C:
                visited[B] = 1
                q.append(B)
    if visited[E]:
        return True
    return False

N, M = map(int, input().split())
G = [[] for _ in range(N+1)]
for _ in range(M):
    A, B, C = map(int, input().split())
    G[A].append((B, C))
    G[B].append((A, C))
S, E = map(int, input().split())
minW = 1
maxW = 1000000000
ans = 0
while minW <= maxW:
    midW = (maxW + minW) // 2
    if bfs(midW):
        ans = midW
        minW = midW + 1
    else:
        maxW = midW - 1
print(ans)


# -*- coding: utf-8 -*-
"""
Created on Mon Nov 19 11:41:36 2018

@author: Susan
"""
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

# making phi matrix
def base_func(x,j,M,s):
    muj = np.asarray(4*j/M).reshape(1,len(j))   # 各種不同muj
    phi = (np.tile(x,M)-np.tile(muj,(x.shape[0],1)))/s    # sigmoid 內的方程式：(x-muj)/s
    phi = 1/(1+np.exp(-phi))    # sigmoid
    return phi

data = pd.read_csv('E:/Machine Learning/HW2/1_data.csv')

# Bayesian Linear Regression
beta = 1
M = 7
s = 0.1
N = [10,15,30,80]
x = np.asarray(data['x']).reshape(-1,1)
t = np.asarray(data['t']).reshape(-1,1)
j = np.arange(M).reshape(-1,1)
I = np.eye(len(j))
S0 = (10**-6)*I
m0 = 0

''' Q1-1 find mN & SN '''
phi = base_func(x,j,M,s)
SN = np.linalg.inv(S0 + beta*np.matmul(phi.T,phi))
mN = beta * np.matmul(np.matmul(SN,phi.T),t)

''' Q1-2 & 3'''
# con：continuous x in range 0-4 (要連續數值才可以畫出線條)
con = np.arange(0, 4.01, 0.01)
con = base_func(np.asarray(con).reshape(-1,1), j, M, s)
for i in range(4):
    sample = data[:N[i]]
    # transfer sample_x into phi matrix
    sample_x = base_func(np.asarray(sample['x']).reshape(-1,1), j, M, s)
    SN = np.linalg.inv(S0 + beta*np.matmul(sample_x.T,sample_x))
    mN = beta * np.matmul(np.matmul(SN,sample_x.T),sample['t'])
    # sample 5 w
    w = np.random.multivariate_normal(mN.reshape((-1,)),SN,5)
    # make the distribution under sample_x & 5 w
    con_y = con.dot(w.T)
    sample_y = sample_x.dot(w.T)
    ''' Q1-2 plot the distribution with 5 different w'''
    # plot the distribution of what we sampled and set x axis in range(0 - 4,0.01)
    plt.plot(sample['x'],sample['t'],'.')
    plt.plot(np.arange(0, 4.01, 0.01),con_y)
    plt.show()

    ''' Q1-3 plot the distribution with (x,t) & mean curve'''
    mean = mN.reshape(-1,1).T.dot(con.T).reshape((-1,))
    std = 1/beta + np.diag(con.dot(SN).dot(con.T))
    plt.plot(sample['x'],sample['t'],'.')
    plt.plot(np.arange(0, 4.01, 0.01),mean,'b')
    plt.fill_between(np.arange(0, 4.01, 0.01), mean+std ,mean-std,color='lightblue')
    plt.show()

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

''' Q2-1 '''
train = pd.read_csv('E:/Machine Learning/HW2/train.csv',header=None)    # The data does'nt have header
test = pd.read_csv('E:/Machine Learning/HW2/test.csv',header=None)
x = train.iloc[:,3:].values
t = train.iloc[:,0:3].values

w0 = np.zeros((3,7))
ak = x.dot(w0.T)
yk = np.exp(ak) / np.sum(np.exp(ak), axis=1).reshape((-1,1))

def update_w(wo,x,y,t):
    wn = np.zeros((3,x.shape[1]))
    H1, H2, H3 = H(x,y)
    dE1, dE2, dE3 = delta_E(x,y,t)

    wn[0] = wo[0] - H1.dot(dE1).T
    wn[1] = wo[1] - H2.dot(dE2).T
    wn[2] = wo[2] - H3.dot(dE3).T
    y[:,0] = x.dot(wn[0].T)
    y[:,1] = x.dot(wn[1].T)
    y[:,2] = x.dot(wn[2].T)
    y = softmax(y)
    error = cross_entropy(y,t)
    return wn,y,error

def H(x,y):
    R = y*(1-y)
    R1, R2, R3 = np.zeros((180,180)),np.zeros((180,180)),np.zeros((180,180))
    np.fill_diagonal(R1, R[:,0])
    np.fill_diagonal(R2, R[:,1])
    np.fill_diagonal(R3, R[:,2])

    Hfunc1 = np.linalg.pinv(x.T.dot(R1).dot(x))
    Hfunc2 = np.linalg.pinv(x.T.dot(R2).dot(x))
    Hfunc3 = np.linalg.pinv(x.T.dot(R3).dot(x))
    return Hfunc1, Hfunc2, Hfunc3

def softmax(y):
    y = np.exp(y) / np.sum(np.exp(y), axis=1).reshape((-1,1))
    return y

def delta_E(x,y,t):
    dE1 = x.T.dot(y[:,0]-t[:,0])
    dE2 = x.T.dot(y[:,1]-t[:,1])
    dE3 = x.T.dot(y[:,2]-t[:,2])
    return dE1, dE2, dE3

# calculate the cross entropy
def cross_entropy(y,t):
    error = -np.sum(t * np.log(y))
    return error

# calculate the accuracy
def Accuracy(y,t):
    #pre = np.round(y)
    pre = np.zeros_like(y)
    pre[np.arange(len(y)), y.argmax(1)] = 1
    acc = 1-(np.count_nonzero(pre-t)/pre.shape[0])
    return acc

# Initial y & w
y = yk.copy()
wn, y,error = update_w( w0, x, yk, t)
acc = []
err = []
while( error > 0.005):
    wn, y, error = update_w( wn, x, y, t)
    acc.append(Accuracy(y,t))
    err.append(error)
# plot accuracy matrix
fig,ax1 = plt.subplots()
ax2 = ax1.twinx()
ax1.plot(acc,'b')
ax2.plot(err,color='orange')

ax1.set_xlabel('epochs')
ax1.set_ylabel('Accuracy')
ax2.set_ylabel('Loss')
plt.title('Accuracy rate & loss')
fig.tight_layout()
plt.show()

#%%
''' Q2-2 '''
test_x = test.values
predict = np.round(softmax(test_x.dot(wn.T)))

#%%
''' Q2-3 '''
for i in range(x.shape[1]):
    plt.hist(x[:60,i],color = 'salmon',alpha=0.7)
    plt.hist(x[60:120,i],color = 'yellowgreen',alpha=0.7)
    plt.hist(x[120:,i],color = 'lightblue',alpha=0.7)
    plt.show()

#%%
''' Q2-5 '''
plt.plot(x[:60, 0], x[:60, 1], '.')
plt.plot(x[60:120, 0], x[60:120, 1], '.')
plt.plot(x[120:, 0], x[120:, 1], '.')
plt.show()

#%%
''' Q2-6 '''
# Initial y & w
x2 = x[:,:2]
wn2 = np.zeros((3,2))
y2 = softmax(x2.dot(wn2.T))
error2 = 1
acc2 = []
err2 = []
while(error2>0.005):
    wn2, y2, error2 = update_w( wn2, x2, y2, t)
    acc2.append(Accuracy(y2,t))
    err2.append(error2)
    if (len(err2) > 1 and err2[-2]-error2<0.001):
        break
# plot accuracy matrix
fig,ax1 = plt.subplots()
ax2 = ax1.twinx()
ax1.plot(acc2,'b')
ax2.plot(err2,color='orange')

ax1.set_xlabel('epochs')
ax1.set_ylabel('Accuracy')
ax2.set_ylabel('Loss')
plt.title('Accuracy rate & loss')
fig.tight_layout()
plt.show()

test_x = test.values
predict2 = np.round(softmax(test_x.dot(wn.T)))

#%%
''' Q2-7 '''
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

train = pd.read_csv('E:/Machine Learning/HW2/train.csv',header=None)
test = pd.read_csv('E:/Machine Learning/HW2/test.csv',header=None)
x = train.iloc[:,3:].values
t = train.iloc[:,0:3].values

x1 = x[:60]
x2 = x[60:120]
x3 = x[120:]

m = np.mean(x,axis=0)
m1 = np.mean(x1,axis=0)
m2 = np.mean(x2,axis=0)
m3 = np.mean(x3,axis=0)
SW = (x - m1).T.dot((x - m1)) + (x - m2).T.dot((x - m2)) + (x - m3).T.dot((x - m3))

M1 = (m1 - m).reshape((-1,1))
M2 = (m2 - m).reshape((-1,1))
M3 = (m3 - m).reshape((-1,1))

SB = 60*(M1.dot(M1.T)) + 60*(M2.dot(M2.T)) + 60*(M3.dot(M3.T))
ST = SW +SB

eig_vals, eig_vecs = np.linalg.eig(np.linalg.inv(SW).dot(SB))
eig_pairs = []
for i in range(len(eig_vals)):
    eig_pairs.append((np.abs(eig_vals[i]), eig_vecs[:,i]))
eig_pairs = sorted(eig_pairs, key=lambda k: k[0], reverse=True)
W = np.hstack((eig_pairs[0][1].reshape(7,1), eig_pairs[1][1].reshape(7,1)))
y = x.dot(W)

plt.plot(y[:60,0],y[:60,1],'.',color = 'salmon')
plt.plot(y[60:120,0],y[60:120,1],'.',color = 'yellowgreen')
plt.plot(y[120:,0],y[120:,1],'.',color = 'lightblue')

plt.grid()
plt.tight_layout
plt.show()
#%%
''' Q3-1 '''
def Accuracy_Rate(y,t):
    A = 1 - (np.count_nonzero(t-y) / len(y))
    return A

def normalize(x):
    std = np.std(x,axis=0).reshape((-1,1))
    mean = np.mean(x,axis=0).reshape((-1,1))
    normalized_x = (x - mean.T) / std.T
    return normalized_x

def KNN(test,train,target,K):
    test_num = test.shape[0]
    train_num = train.shape[0]
    _test = np.repeat(test, train_num, axis=0)
    _train = np.tile(train,( test_num,1))
    dist = np.sqrt(np.sum(np.square(_test - _train),axis=1)).reshape((60,150)).T

    arg_sort = np.argsort(dist, axis=0)[:K]
    ans = target[arg_sort].astype(np.int32)
    y = np.zeros((test_num, ))
    for i in range(test_num):
        y[i] = np.bincount(ans[:, i]).argmax()
    return y

seeds = pd.read_csv('E:/Machine Learning/HW2/seeds.csv').values
# normalize the data
x = seeds[:,:7]
t = seeds[:,-1].reshape((-1,1))
x_nor = normalize(x)

# split data into trainning data and testing data
data = np.concatenate((x_nor,t),axis=1)
train = np.concatenate((data[:50],data[70:120],data[140:190]),axis=0)
train_x = train[:,:7]
train_y = train[:,-1]

test = np.concatenate((data[50:70],data[120:140],data[190:210]),axis=0)
test_x = test[:,:7]
test_y = test[:,-1]

Ans, Acc = [], []
K = np.arange(1,11)
for k in K:
    Ans.append(KNN(test_x,train_x,train_y,k))
    Acc.append(Accuracy_Rate(Ans[k-1],test_y))
import matplotlib.pyplot as plt
plt.plot(Acc)
plt.show()
#%%
''' Q3-2 '''
def KNN_dist(test,train,target,distence):
    test_num = test.shape[0]
    train_num = train.shape[0]
    _test = np.repeat(test, train_num, axis=0)
    _train = np.tile(train,( test_num,1))
    dist = np.sqrt(np.sum(np.square(_test - _train),axis=1)).reshape((60,150)).T

    y = np.zeros((test_num, ))

    for i in range(test_num):
        pred = target[dist[:, i] < distence].astype(np.int32)
        y[i] = np.bincount(pred).argmax()
    return y

train_x = train[:,:7]
train_y = train[:,-1]
test_x = test[:,:7]
test_y = test[:,-1]

Ans, Acc = [], []
V = np.arange(2,11)

for v in V:
    Ans.append(KNN_dist(test_x,train_x,train_y,v))
    Acc.append(Accuracy_Rate(Ans[v-2],test_y))
import matplotlib.pyplot as plt
plt.plot(Acc)
plt.show()


from tkinter import *
from tkinter import filedialog
from tkinter import messagebox
import PyPDF2
import os

files=Tk()
files.title('Easy Files')
files.geometry(f'400x600')
files.resizable(False,False)
# Add a frame to set the size of the window
frame= Frame(files, relief= 'sunken')
frame.pack(fill= BOTH, expand= True, padx= 10, pady=0)

def browseFiles():
    global filename
    filename = filedialog.askopenfilename(initialdir="/",
                                          title="Select a File",
                                          filetypes=(("Pdf files",
                                                      "*.pdf*"),
                                                     ("all files",
                                                      "*.*")))
    label_file_explorer.config(text=os.path.split(filename)[1])

def convert_pdf():
    global filename
    global name
    name = name_entry.get()
    start=int(Entry2.get())
    end=int(Entry3.get())
    with open(filename, mode='rb') as f:
        reader = PyPDF2.PdfFileReader(f)
        doc_path = os.path.expanduser("~\Desktop") +'\\'+ name + '.doc'
        print(doc_path)
        # Open a file with access mode 'a'
        file_object = open(doc_path, 'a')
        for i in range(start-1,end):
            page = reader.getPage(i)
            # Append page content at the end of file
            file_object.write(page.extractText())
            # Close the file
        file_object.close()

        return messagebox.showinfo('Success', f'File saved in desktop as {name}.doc')

        #Print success
        label_success.pack()

#Create heading
label = Label(
    frame,
    font = "Helvetica 40 bold",
    foreground = "grey", text = "Welcome")
label.pack(pady=30)

#Create a File Explorer button
button1 = Button(frame, text="Browse file", command=browseFiles)
button1.pack(pady=1)

# Create a File Explorer label
label_file_explorer = Label(frame,
                            text="Upload pdf",
                            width=100, height=4,
                            fg="grey")
label_file_explorer.pack(pady=1)

#Frame 2
frame2=Frame(files)
frame2.pack(fill= BOTH, expand= True, padx= 10, pady=0)
#Page Range
Label2 = Label(frame2,width=25)
Label2.configure(text='''                           Enter page range ''')
Label2.grid(row=0,column=0)

Entry2 = Entry(frame2,width=3)
Entry2.grid(row=0,column=1)

Entry3 = Entry(frame2,width=3)
Entry3.grid(row=0,column=3)

Label3 = Label(frame2)
Label3.configure(text='''-''')
Label3.grid(row=0,column=2)

#Frame3
frame3=Frame(files)
frame3.pack(fill= BOTH, expand= True, padx= 10, pady=0)
#Enter name of new file
name_label = Label(frame3, text = 'New file name', font=('calibre',10, 'bold'))
name_label.pack()
name_entry = Entry(frame3, font=('calibre',10,'normal'))
name_entry.pack()

#Create a File Converter button
button2 = Button(frame3, text="Convert File",height=5,bg='grey',fg='white', command= convert_pdf)
button2.pack(pady=50)

# Create a Message label
label_success = Label(frame3,
                            text='Success!!',
                            fg='green',
                            font=("Arial", 15),
                            width=100, height=4)

#   close window after 3 seconds
#files.after(3000, lambda: files.destroy())

files.mainloop()


import sqlite3, datetime
from EmployeeEdit import randserial
from Login import clear
from Inventory import unique
conn = sqlite3.connect('MarksHardware.db')
cursor = conn.cursor()

def uniquesales(ID): #ensures unique Ids
  for id in conn.execute("SELECT SALESID FROM SALES"):
    if id[0] == ID:
      return False
    return True

def addsales(USERID): #Adds sales uses current date time and Id from who logged on to categorize sales
  clear()
  try:
    cashier = conn.execute(f"SELECT NAME from COMPANY where ID = {USERID}").fetchall()[0][0]
    while True:
      SALESID = randserial(6)
      if uniquesales(SALESID):
        break
    for row in conn.execute("SELECT ID, NAME from INVENTORY"):
      print(f'ID: {row[0]} || Name: {row[1]} ')

    while True:
      print('Sales ID:', SALESID)
      print('Cashier:', cashier)
      Sale = int(input('Enter serial #. Enter 0 to cancel: ')) # getsinventory id of item
      if Sale == 0:
        input('Cancel. \nPress ente to continue...')
        return
      if unique(Sale) == False: # decides if the serial number provided is valid
        print("Found!")
        Name = conn.execute(f"SELECT NAME FROM INVENTORY WHERE ID = {Sale}").fetchone()[0] # gets the product namewith the id from the inventory
        date = datetime.datetime.now().date() # Gets date of sale
        unitsleft2 = conn.execute(f"SELECT STOCK FROM INVENTORY WHERE ID={Sale}").fetchone()[0] # gets units left in stock of item
        print('There are', unitsleft2, 'left in stock')
        while True: # loop asks for unts being sold and if the input is invalid loop it
          sold = int(input('Input number of units being sold: '))
          if unitsleft2 >= sold: # checks to see if there is enough in stock to sell requested amount
            break
          else:
            print('Invalid. Not enough stock.')
            continue
        salval = (SALESID, Sale, Name, sold, date, cashier)
        conn.execute("INSERT INTO SALES VALUES (?,?,?,?,?,?)", salval)
        conn.execute(f"UPDATE INVENTORY SET STOCK = {unitsleft2 - sold} where ID = {Sale}")
        conn.commit()
        input('Success. \nPress enter to continue...')
        return
      else:  # if serial Id does not match any in the inventory asks for id again
          input("Serial # does not exist. \nPress enter to continue...")
          continue
  except:
    input('Invalid. \nPress enter to continue...')

def viewsales(): #This function prints out all the past sales
  for row in conn.execute("SELECT * FROM SALES").fetchall():
    print(f'Sale ID: {row[0]} || Item ID: {row[1]}\nItem: {row[2]} || # of items sold: {row[3]}\nDate: {row[4]} || Cashier: {row[5]}\n')
  input('Press enter to continue...')

def main(id): #This function gives you choices to choose from to continue
  while True:
    clear()
    choice = input("""
  -------Sales information-------
  Please choose:
  1 - View Sales
  2 - Add Sales
  3 - Return
  ----------------------------------
  """)
    if choice == "1":
      viewsales()
    elif choice == "2":
      addsales(id)
    elif choice == "3":
      return
    else:
      input("Invalid.\nPress enter to continue:...")


import pandas as pd
from re import split
from tqdm import tqdm

## COUNT GAMES BY INITIAL PLYS

d = pd.read_csv('../data/csv/caissa_clean.csv', index_col=0)

f = lambda pgn: sum([m.strip().split() for m in split(r'\d+\.', pgn) if len(m) > 0],[])

def extract_first_plys(row):
    plys = {}
    plys['Year'] = row.Year
    plys['Result'] = row.Result
    pgn = f(row.pgn)
    for i in range(16):
        if i < len(pgn):
            plys['Ply'+str(i+1)] = pgn[i]
        else:
            plys['Ply'+str(i+1)] = pd.NA

    return plys

ply_rows = []

for _, row in tqdm(d.iterrows()):
    ply_rows.append(extract_first_plys(row))

d_plys = pd.DataFrame.from_records(ply_rows)
d_plys = d_plys[['Year']+list(d_plys.columns[2:])+['Result']]

for i in tqdm(range(12)):
    d_plys_gb = d_plys.groupby(list(d_plys.columns[:i+2])).agg({d_plys.columns[i+2]: 'count', 'Result': 'mean'}).reset_index()
    d_plys_gb.columns = list(d_plys_gb.columns[:-2]) + ['Count', 'Result']
    d_plys_gb['Moves'] = d_plys_gb[d_plys_gb.columns[1:i+2]].agg(' '.join, axis=1)
    d_plys_gb.to_csv('../data/csv/caissa_counts_by_ply'+str(i+1)+'.csv')

## WRITE A TABLE WITH A ROW PER PLAYER IN A GAME (two rows per actual game)

d = pd.read_csv('../data/csv/caissa_clean_only_frequent_players.csv', index_col=0)

player_rows = []

for i, row in tqdm(d.iterrows()):
    player_rows.append({'Player': row.White, 'Game':i, 'Elo': row.WhiteElo, 'Country': row.WhiteCountry, 'Result':  row.Result, 'ECO': row.ECO[:3], 'Year': row.Year, 'FideId': row.WhiteFideId, 'Title': row.WhiteTitle, 'Pieces': 'White', 'pgn': row.pgn})
    player_rows.append({'Player': row.Black, 'Game':i, 'Elo': row.BlackElo, 'Country': row.BlackCountry, 'Result': -row.Result, 'ECO': row.ECO[:3], 'Year': row.Year, 'FideId': row.BlackFideId, 'Title': row.BlackTitle, 'Pieces': 'Black', 'pgn': row.pgn})

d_players = pd.DataFrame.from_records(player_rows)

d_players.to_csv('../data/csv/caissa_player_games.csv')


import torch.optim as optim
import torchvision.transforms as T

# For list of supported models use timm.list_models
MODEL_NAME = "resnet18"
NUM_ClASSES = 10
IN_CHANNELS = 3

USE_TORCHVISION = False  # If you need to use timm models set to False.

# USE_TORCHVISION = True # Should use Torchvision Models or timm models
PRETRAINED = True  # If True -> Fine Tuning else Scratch Training
EPOCHS = 5
TRAIN_BATCH_SIZE = 512  # Training Batch Size
VALID_BATCH_SIZE = 512  # Validation Batch Size
NUM_WORKERS = 4  # Workers for training and validation

EARLY_STOPPING = True  # If you need early stoppoing for validation loss
SAVE_PATH = "{}.pt".format(MODEL_NAME)

# IMG_WIDTH = 224  # Width of the image
# IMG_HEIGHT = 224  # Height of the image
MOMENTUM = 0.8  # Use only for SGD
LEARNING_RATE = 1e-3  # Learning Rate
SEED = 42

LOG_INTERVAL = 300  # Interval to print between epoch

# Train and validation Transforms which you would like
train_transforms = T.Compose([T.ToTensor(), T.Normalize((0.5,), (0.5,))])
valid_transforms = T.Compose([T.ToTensor(), T.Normalize((0.5,), (0.5,))])

# Classes to be detected.
FASHION_MNIST_CLASSES = (
    "T-shirt/top",
    "Trouser",
    "Pullover",
    "Dress",
    "Coat",
    "Sandal",
    "Shirt",
    "Sneaker",
    "Bag",
    "Ankle Boot",
)
# Classes to be detected.
CIFAR10_CLASSES = (
    "airplane",
    "automobile",
    "bird",
    "cat",
    "deer",
    "dog",
    "frog",
    "horse",
    "ship",
    "truck",
)


import pytest
from rest_framework.test import APIClient
from django.urls import reverse
from website.models import Trip
from website.models.profile import UserDetail
import mock
from rest_framework.response import Response
from website.api.v1.main_page.serializers import TripSerializerRetrieve
from django.shortcuts import get_object_or_404
from website.api.v1.main_page import views

@pytest.fixture
def ApiClient():
    # anonymous user
    client = APIClient()
    return client

@pytest.fixture
def CommonBusiness():
    trip = Trip.objects.create(
        pk = 10,
        appear_in_search=False,
    )
    return trip

@pytest.mark.django_db
class TestTripRetrieve():
    url = reverse(
            'website:main-page:retrieve-trip', kwargs={
            "pk": 10,
            })
    def test_trip_retrieve_response_200(self, ApiClient,CommonBusiness):
        with mock.patch('website.api.v1.main_page.views.TripViewSet.retrieve') as mock_trip_retrieve:
            trip = get_object_or_404(Trip, pk=CommonBusiness.pk, appear_in_search=False)
            serializer = TripSerializerRetrieve(trip)
            mock_trip_retrieve.return_value = Response(serializer.data)

            response = ApiClient.get(self.url)
            assert response.status_code == 200


from __future__ import division
import cv2
import numpy as np

# global
# HSV 色值：
GREEN = [40, 65, 13, 80, 255, 255]  # Green
YELLOW = [20, 103, 80, 40, 255, 255]  # Yellow
BLUE = [94, 81, 82, 126, 255, 255]  # Blue
RED = [0, 144, 0, 20, 255, 255]   # Red

# default value: Blue 蓝色
lowHue = BLUE[0]
lowSat = BLUE[1]
lowVal = BLUE[2]
highHue = BLUE[3]
highSat = BLUE[4]
highVal = BLUE[5]

# 排列：green, yellow, blue, red
defaultColor = [0, 0, 1, 0]  # 蓝色
color = "BLUE"  # 现在的颜色
blueColor = (255, 0, 0)  # 蓝色
greenColor = (0, 255, 0)  # 绿色
redColor = (0, 0, 255)  # 红色

# 图像色调和追踪
def frame_mask_contour(image):
    frameHSV = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)

    colorLow = np.array([lowHue, lowSat, lowVal])
    colorHigh = np.array([highHue, highSat, highVal])

    mask = cv2.inRange(frameHSV, colorLow, colorHigh)

    # get contours
    contours, hierarchy = cv2.findContours(mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)
    contour_sizes = [(cv2.contourArea(contour), contour) for contour in contours]

    count = len(contour_sizes)

    found = False
    i=0
    if count > 0:

        for cnt in contours:
            area = cv2.contourArea(cnt)
            if area > 4000:
                x, y, w, h = cv2.boundingRect(cnt)
                i=i+1
                print(i)
                cv2.rectangle(image, (x, y), (x + w, y + h), greenColor, 2)
                print(x,y,w,h)
                found = True
                if i == 1 :
                    square = "x={} , y={} , x+w={} , y+h={}".format(x,y,x+w,y+h)
                    area = "S = {}".format(w*h)
                    cv2.putText(image, square,(50,100),cv2.FONT_HERSHEY_SIMPLEX, 1, redColor, 2)
                    cv2.putText(image, area,(50,150),cv2.FONT_HERSHEY_SIMPLEX, 1, redColor, 2)
                elif i == 2:
                    square = "x={} , y={} , x+w={} , y+h={}".format(x,y,x+w,y+h)
                    area = "S = {}".format(w*h)
                    cv2.putText(image, square,(50,200),cv2.FONT_HERSHEY_SIMPLEX, 1, redColor, 2)
                    cv2.putText(image, area,(50,250),cv2.FONT_HERSHEY_SIMPLEX, 1, redColor, 2)
                elif i == 3:
                    square = "x={} , y={} , x+w={} , y+h={}".format(x,y,x+w,y+h)
                    area = "S = {}".format(w*h)
                    cv2.putText(image, square,(50,300),cv2.FONT_HERSHEY_SIMPLEX, 1, redColor, 2)
                    cv2.putText(image, area,(50,350),cv2.FONT_HERSHEY_SIMPLEX, 1, redColor, 2)
                elif i == 4:
                    square = "x={} , y={} , x+w={} , y+h={}".format(x,y,x+w,y+h)
                    area = "S = {}".format(w*h)
                    cv2.putText(image, square,(50,400),cv2.FONT_HERSHEY_SIMPLEX, 1, redColor, 2)
                    cv2.putText(image, area,(50,450),cv2.FONT_HERSHEY_SIMPLEX, 1, redColor, 2)
                else:
                    continue
    if found:
        text = f'Found {color} object'
        print(text)
        cv2.putText(image, text, (50, 50), cv2.FONT_HERSHEY_SIMPLEX, 1, redColor, 2)
    else:
        text = "Not Found!"
        print("Not Found! 没有找到！")
        cv2.putText(image, text, (50, 50), cv2.FONT_HERSHEY_SIMPLEX, 1, redColor, 2)

    return mask, image

# 绿色控制
def greenCtrl(val):
    global defaultColor, color
    color = 'GREEN'
    defaultColor = [1, 0, 0, 0]
    assignColor(GREEN)
# 黄色控制
def yellowCtrl(val):
    global defaultColor, color
    color = 'YELLOW'
    defaultColor = [0, 1, 0, 0]
    assignColor(YELLOW)
# 蓝色控制
def blueCtrl(val):
    global defaultColor, color
    color = 'BLUE'
    defaultColor = [0, 0, 1, 0]
    assignColor(BLUE)
# 红色控制
def redCtrl(val):
    global defaultColor, color
    color = 'RED'
    defaultColor = [0, 0, 0, 1]
    assignColor(RED)

vidCapture = cv2.VideoCapture(0)

vidCapture.set(cv2.CAP_PROP_FRAME_WIDTH,800)#FRAME_WIDTH = 800
vidCapture.set(cv2.CAP_PROP_FRAME_HEIGHT,600)#FRAME_HEIGHT = 600

video_win = 'Frame'
cv2.namedWindow(video_win, cv2.WINDOW_AUTOSIZE)
cv2.moveWindow(video_win, 855, 0)
'''
# =====================================
# Window 窗口： Demo
# =====================================
demo_win = 'Demo'
cv2.namedWindow(demo_win)
cv2.moveWindow(demo_win, 205, 0)
'''

while True:

    # Get webcam frame.获取网络摄像头帧。
    _, frame = vidCapture.read()

    # Show the original image.显示原始图像。
    demo, frame_contour = frame_mask_contour(frame)
    cv2.imshow(video_win, frame_contour)
    #cv2.imshow(demo_win, demo)

    key = cv2.waitKey(10)

vidCapture.release()  # release()释放摄像头
cv2.destroyAllWindows()  # 调用destroyAllWindows()关闭所有图像窗口。


import datetime
from typing import Any, Dict, List, Optional, Type, TypeVar, Union, cast

import attr
from dateutil.parser import isoparse

from ..types import UNSET, Unset

T = TypeVar("T", bound="PatchedStepInvocation")

@attr.s(auto_attribs=True)
class PatchedStepInvocation:
    """Dynamically removes fields from serializer.
    https://stackoverflow.com/questions/27935558/dynamically-exclude-or-include-a-field-in-django-rest-framework-serializer"""

    url: Union[Unset, str] = UNSET
    id: Union[Unset, int] = UNSET
    step: Union[Unset, str] = UNSET
    release: Union[Unset, str] = UNSET
    invoker: Union[Unset, List[str]] = UNSET
    origin: Union[Unset, Optional[str]] = UNSET
    created: Union[Unset, datetime.datetime] = UNSET
    additional_properties: Dict[str, Any] = attr.ib(init=False, factory=dict)

    def to_dict(self) -> Dict[str, Any]:
        url = self.url
        id = self.id
        step = self.step
        release = self.release
        invoker: Union[Unset, List[Any]] = UNSET
        if not isinstance(self.invoker, Unset):
            invoker = self.invoker

        origin = self.origin
        created: Union[Unset, str] = UNSET
        if not isinstance(self.created, Unset):
            created = self.created.isoformat()

        field_dict: Dict[str, Any] = {}
        field_dict.update(self.additional_properties)
        field_dict.update({})
        if url is not UNSET:
            field_dict["url"] = url
        if id is not UNSET:
            field_dict["id"] = id
        if step is not UNSET:
            field_dict["step"] = step
        if release is not UNSET:
            field_dict["release"] = release
        if invoker is not UNSET:
            field_dict["invoker"] = invoker
        if origin is not UNSET:
            field_dict["origin"] = origin
        if created is not UNSET:
            field_dict["created"] = created

        return field_dict

    @classmethod
    def from_dict(cls: Type[T], src_dict: Dict[str, Any]) -> T:
        d = src_dict.copy()
        url = d.pop("url", UNSET)

        id = d.pop("id", UNSET)

        step = d.pop("step", UNSET)

        release = d.pop("release", UNSET)

        invoker = cast(List[str], d.pop("invoker", UNSET))

        origin = d.pop("origin", UNSET)

        created: Union[Unset, datetime.datetime] = UNSET
        _created = d.pop("created", UNSET)
        if not isinstance(_created, Unset):
            created = isoparse(_created)

        patched_step_invocation = cls(
            url=url,
            id=id,
            step=step,
            release=release,
            invoker=invoker,
            origin=origin,
            created=created,
        )

        patched_step_invocation.additional_properties = d
        return patched_step_invocation

    @property
    def additional_keys(self) -> List[str]:
        return list(self.additional_properties.keys())

    def __getitem__(self, key: str) -> Any:
        return self.additional_properties[key]

    def __setitem__(self, key: str, value: Any) -> None:
        self.additional_properties[key] = value

    def __delitem__(self, key: str) -> None:
        del self.additional_properties[key]

    def __contains__(self, key: str) -> bool:
        return key in self.additional_properties


import pandas as pd
import numpy as np
import pdfplumber
import re
import zipfile
import os

def unzipper(zip):
    """
    unzips zipfile and stores content in tempfolder on same level directory.
    returns list of directories of pdf files in temp folder.
    ______
    takes in the zipfile.
    """
    path = 'data'
    with zipfile.ZipFile(zip, 'r') as zip_ref:
        zip_ref.extractall(path)

    files = os.listdir(path)
    dirs = [os.path.join(path, file) for file in files if file.endswith('.pdf') or file.endswith('.PDF')]

    return dirs

def pdf_parser(files):
    """
    Returns a df filled with buy and sell information of the input PDF files.
    ________
    Takes in files, a list of directories or bytes files (pdfplumber).
    """
    print('pdf_parser was called.')

    df = pd.DataFrame()
    for file in files:
        with pdfplumber.open(file) as pdf:
            pages = [page.extract_text() for page in pdf.pages]

        match = [p.start() for p in re.finditer('Nominale', pages[0])]
        trans = pages[0][match[0]:].split('\n')
        action = re.findall('Wertpapier Abrechnung (\w+)', pages[0])

        if action:
            # if 'Abrechnung' was found
            val = re.findall(r'Stück (\d+)', trans[1])[0]
            wkn = trans[1].replace(')', '').split('(')[1]
            date = re.findall(r'Datum (\d+.\d+.\d+)', pages[0])[0]

            # assemble df
            data = [val, wkn, date]
            columns = [action[0], 'WKN', 'Datum']

            # scan 2nd page
            if action[0] == 'Verkauf':
                buy = [p.start() for p in re.finditer('Ber\wcksichtigte Anschaffungsgesch\wfte', pages[1])][0]
    #             columns2 = columns + pages[1][buy:].split('\n')[1].split()
                columns2 = columns + ['Geschäft',
                                      'Auftragsnr.',
                                      'Ausführ.-tag',
                                      'Whr./St.',
                                      'Nennwert/Stück',
                                      'AS-Kosten',
                                      'Erlös', 'ant.Ergebnis',
                                      'Land']
                data2 = data + pages[1][buy:].split('\n')[2].split()
                try:
                    new_df = pd.DataFrame([data2], columns=columns2)
                except:
                    new_df = (pd.DataFrame([data], columns=columns))
                    print(f"couldn't process 2nd page of file: {file}")

            else:
                rate = re.findall(r'Ausf\whrungskurs(\d+,\d+)', pages[0])[0]
                data.append(rate)
                columns.append('Kurs')
                new_df = pd.DataFrame([data], columns=columns)

            df = pd.concat([df, new_df])

    return df


from django.conf import settings
from django.contrib.auth import (
    login as auth_login,
    logout as auth_logout,
    REDIRECT_FIELD_NAME
)
from django.core.paginator import EmptyPage, PageNotAnInteger, Paginator
from django.shortcuts import redirect, render, resolve_url, reverse
from django.utils.http import is_safe_url
from django.utils.translation import ugettext_lazy as _
from django.views import View

from clublink.cms.forms import LoginForm
from clublink.clubs.models import Club, Department
from clublink.users.models import User

from clublink.cms.views import CMSView

def login(request):
    redirect_to = request.GET.get(REDIRECT_FIELD_NAME, '')

    # Ensure the user-originating redirection URL is safe.
    if not is_safe_url(url=redirect_to, host=request.get_host()):
        redirect_to = resolve_url(reverse('home'))
    form = LoginForm()

    if request.user.is_authenticated:
        if redirect_to == request.path:
            return redirect(resolve_url(settings.LOGIN_REDIRECT_URL))
        return redirect(redirect_to)
    elif request.method == 'POST':
        form = LoginForm(request.POST)
        if form.is_valid():
            auth_login(request, form.get_user())
            return redirect(redirect_to)

    return render(request, 'cms/login.jinja', {'form': form})

def logout(request):
    auth_logout(request)
    return redirect(reverse('home'))

class HomeView(CMSView):
    def get(self, request, *args, **kwargs):
        return render(request, 'cms/dashboard/home.jinja')

class ListView(View):
    title = ''
    queryset = None
    list_fields = None
    per_page = 10
    actions = (
        ('delete_items', _('Delete selected')),
    )

    def get_queryset(self, request):
        return self.queryset

    def delete_items(self, request):
        item_pks = request.POST.get('items', [])
        items = self.get_queryset(request).filter(pk__in=item_pks)
        items.delete()

    def get(self, request):
        per_page = request.GET.get('per_page', self.per_page)
        paginator = Paginator(self.get_queryset(request), per_page)

        page = request.GET.get('page')
        try:
            items = paginator.page(page)
        except PageNotAnInteger:
            items = paginator.page(1)
        except EmptyPage:
            page = paginator.num_pages
            items = paginator.page(page)

        return render(request, 'cms/list.jinja', {
            'items': items, 'list_fields': self.list_fields, 'page': page, 'per_page': per_page,
            'title': self.title})

    def post(self, request):
        action = request.POST.get('action')

        for a in self.actions:
            if a[0] == action:
                fn = getattr(self, a[1])
                if callable(fn):
                    fn(request)

        return self.get(request)

class UserListView(ListView):
    title = _('User Accounts')
    list_fields = (
        ('username', _('Username')),
        ('first_name', _('First Name')),
        ('last_name', _('Last Name')),
        ('membership_number', _('Membership Number')),
    )
    queryset = User.objects.all()

class ClubListView(ListView):
    title = _('Clubs')
    list_fields = (
        ('name', _('Name')),
        ('slug', _('Slug')),
        ('code', _('Club Number')),
    )
    queryset = Club.objects.all()

class DepartmentListView(ListView):
    title = _('Departments')
    list_fields = (
        ('name', _('Name')),
        ('number', _('Department Number')),
        ('hidden', _('Hidden')),
    )
    queryset = Department.objects.all()


import pandas as pd
import streamlit as st
import plotly.express as px

st.set_page_config(page_title="Tickets Dashboard ðŸ“Š ", layout="wide" )

df = pd.read_csv('customer_support_tickets.csv')
print(df)

st.title("ðŸ“Š Tickets Dashboard")
st.markdown("##")

st.sidebar.header("Please Filter Here:")
# ticket_ID = st.sidebar.multiselect(
#     "select the ticket id:",
#     options = df["Ticket ID"].unique(),
#     default = df["Ticket ID"].unique()
# )
customer_Gender = st.sidebar.multiselect(
    "select the Customer Gender:",
    options = df["Customer Gender"].unique(),
    default = df["Customer Gender"].unique()
)
ticket_Priority = st.sidebar.multiselect(
    "select the Ticket Priority:",
    options = df["Ticket Priority"].unique(),
    default = df["Ticket Priority"].unique()
)

df_selection = pd.read_csv("customer_support_tickets.csv")

st.dataframe(df_selection)

total_tickets = int(df_selection["Ticket ID"].sum())
Customer_Satisfaction_Rating = round(df_selection["Customer Satisfaction Rating"].mean(), 1)
star_rating = ":star" * int(round(Customer_Satisfaction_Rating, 0))

left_column, right_column = st.columns(2)
with left_column:
    st.subheader("Total Number of Tickets:")
    st.subheader(f"{total_tickets:}")
with right_column:
    st.subheader("Average Customer Rating:")
    st.subheader(f"{Customer_Satisfaction_Rating}{star_rating}")

st.markdown("---")

Ticket_Type_Count = df_selection["Ticket Type"].value_counts().reset_index()
Ticket_Type_Count.columns = ["Ticket Type", "Count"]

fig_product_sales = px.bar(
    Ticket_Type_Count,
    x="Ticket Type",
    y="Count",
    orientation="v",
    title="<b>Ticket Type Occurrence</b>",
    template="plotly_white",
)

st.plotly_chart(fig_product_sales)


import cv2
import numpy as np
import glob
import re

img_array = []
files = sorted(glob.glob('C:\\Users\\Rodrigo\\Documents\\GitHub\\computerVision\\images\\output\\brighter\\*.png'))
files = sorted(files, key=lambda x:float(re.findall("(\d+)",x)[0]))
outputPath = 'C:\\Users\\Rodrigo\\Documents\\GitHub\\computerVision\\images\\output\\videos\\'

for filename in files:
    print(filename)
    img = cv2.imread(filename)
    height, width, layers = img.shape
    size = (width,height)
    img_array.append(img)

out = cv2.VideoWriter('project.avi',cv2.VideoWriter_fourcc(*'DIVX'), 60, size)

for i in range(len(img_array)):
    out.write(img_array[i])
out.release()


'''This program is run separately and manually to add new entries in the
csv file that is not in the database, to the database.
'''

import csv
import sqlite3

db_connection = sqlite3.connect('events.db')
db_cursor = db_connection.cursor()
db_cursor.execute('''DROP TABLE IF EXISTS events''')
db_cursor.execute('''CREATE TABLE events (
serial int NOT NULL PRIMARY KEY,
eventdate date,
name varchar,
event varchar,
priority int)'''
                  )

with open('events.csv', newline='') as csvfile:
    csvobj = csv.reader(csvfile)
    serial_nums = [x[0]
                   for x in db_cursor.execute('SELECT serial FROM events')]
    for item in csvobj:
        if int(item[0]) not in serial_nums:
            db_cursor.execute(
                "INSERT INTO events VALUES (?, ?, ?, ?, ?)", item)

db_connection.commit()
db_connection.close()


import aiohttp

API_URL = "http://gfapi.mlogcn.com/weather/v001/hour"
API_KEY = ""  # 请替换为您的API密钥

class WeatherForecast:
    def __init__(self, db_manager):
        self.db_manager = db_manager

    async def get_unique_district_codes(self):
        async with self.db_manager.pool.acquire() as conn:
            async with conn.cursor() as cur:
                await cur.execute("SELECT DISTINCT district_code FROM stations")
                result = await cur.fetchall()
                return [row[0] for row in result]

    async def fetch_weather_data(self, district_code):
        params = {"areacode": district_code, "hours": 24, "key": API_KEY}
        async with aiohttp.ClientSession() as session:
            async with session.get(API_URL, params=params) as response:
                if response.status == 200:
                    return await response.json()
                else:
                    print(f"Error fetching data for district_code: {district_code}")
                    return None

    async def get_forecasts(self):
        district_codes = await self.get_unique_district_codes()
        forecasts = {}
        for district_code in district_codes:
            forecast_data = await self.fetch_weather_data(district_code)
            if forecast_data:
                forecasts[district_code] = forecast_data
        return forecasts

    async def store_forecasts(self, forecasts):
        for district_code, forecast in forecasts.items():
            if forecast["status"] == 0:  # 确保状态码为0，表示数据有效
                await self.db_manager.insert_weather_forecast(
                    str(district_code), forecast["result"]
                )


import bpy
from bpy.props import StringProperty, BoolProperty
from ... utils.collection import get_groups_collection, get_scene_collections

class CreateCollection(bpy.types.Operator):
    bl_idname = "machin3.create_collection"
    bl_label = "MACHIN3: Create Collection"
    bl_description = "description"
    bl_options = {'REGISTER', 'UNDO'}

    def update_name(self, context):
        name = self.name.strip()
        col = bpy.data.collections.get(name)

        if col:
            self.isduplicate = True
        else:
            self.isduplicate = False

    name: StringProperty("Collection Name", default="", update=update_name)

    isduplicate: BoolProperty("is duplicate name")

    def draw(self, context):
        layout = self.layout

        column = layout.column()

        column.prop(self, "name", text="Name")
        if self.isduplicate:
            column.label(text="Collection '%s' exists already" % (self.name.strip()), icon='ERROR')

    def invoke(self, context, event):
        wm = context.window_manager

        return wm.invoke_props_dialog(self, width=300)

    def execute(self, context):
        name = self.name.strip()

        col = bpy.data.collections.new(name=name)

        acol = context.view_layer.active_layer_collection.collection
        acol.children.link(col)

        self.name = ''

        return {'FINISHED'}

class RemoveFromCollection(bpy.types.Operator):
    bl_idname = "machin3.remove_from_collection"
    bl_label = "MACHIN3: Remove from Collection"
    bl_description = "Remove Selection from a Collection"
    bl_options = {'REGISTER', 'UNDO'}

    @classmethod
    def poll(cls, context):
        view = context.space_data
        return view.type == 'VIEW_3D' and context.selected_objects

    def execute(self, context):

        if context.active_object not in context.selected_objects:
            context.view_layer.objects.active = context.selected_objects[0]

        bpy.ops.collection.objects_remove('INVOKE_DEFAULT')

        return {'FINISHED'}

class AddToCollection(bpy.types.Operator):
    bl_idname = "machin3.add_to_collection"
    bl_label = "MACHIN3: Add to Collection"
    bl_description = "Add Selection to a Collection"
    bl_options = {'REGISTER', 'UNDO'}

    @classmethod
    def poll(cls, context):
        view = context.space_data

        if view.type == 'VIEW_3D' and context.selected_objects:

            if view.local_view:
                for area in context.screen.areas:
                    if area.type == 'OUTLINER':
                        return True
            else:
                return True

    def execute(self, context):
        view = context.space_data

        if view.local_view:
            for area in context.screen.areas:
                if area.type == 'OUTLINER':
                    override = {'area': area}
                    break

            bpy.ops.object.link_to_collection(override, 'INVOKE_DEFAULT')

        else:
            bpy.ops.object.link_to_collection('INVOKE_DEFAULT')

        return {'FINISHED'}

class MoveToCollection(bpy.types.Operator):
    bl_idname = "machin3.move_to_collection"
    bl_label = "MACHIN3: Move to Collection"
    bl_description = "Move Selection to a Collection"
    bl_options = {'REGISTER', 'UNDO'}

    @classmethod
    def poll(cls, context):
        view = context.space_data

        if view.type == 'VIEW_3D' and context.selected_objects:

            if view.local_view:
                for area in context.screen.areas:
                    if area.type == 'OUTLINER':
                        return True
            else:
                return True

    def execute(self, context):
        view = context.space_data

        if view.local_view:
            for area in context.screen.areas:
                if area.type == 'OUTLINER':
                    override = {'area': area}
                    break

            bpy.ops.object.move_to_collection(override, 'INVOKE_DEFAULT')

        else:
            bpy.ops.object.move_to_collection('INVOKE_DEFAULT')

        return {'FINISHED'}

class Purge(bpy.types.Operator):
    bl_idname = "machin3.purge_collections"
    bl_label = "MACHIN3: Purge Collections"
    bl_description = "Remove empty Collections"
    bl_options = {'REGISTER', 'UNDO'}

    def execute(self, context):
        for col in get_scene_collections(context.scene):
            if not any([col.children, col.objects]):
                print("Removing collection '%s'." % (col.name))
                bpy.data.collections.remove(col, do_unlink=True)

        return {'FINISHED'}

class Select(bpy.types.Operator):
    bl_idname = "machin3.select_collection"
    bl_label = "MACHIN3: (De)Select Collection"
    bl_description = "Select Collection Objects\nSHIFT: Select all Collection Objects\nALT: Deselect Collection Objects\nSHIFT+ALT: Deselect all Collection Objects\nCTRL: Toggle Viewport Selection of Collection Objects"
    bl_options = {'REGISTER'}

    name: StringProperty()
    force_all: BoolProperty()

    def invoke(self, context, event):
        col = bpy.data.collections.get(self.name, context.scene.collection)

        objects = col.all_objects if event.shift or self.force_all else col.objects

        if objects:
            hideselect = objects[0].hide_select

            if col:
                for obj in objects:
                    if event.alt:
                        obj.select_set(False)

                    elif event.ctrl:
                        if obj.name in col.objects:
                            obj.hide_select = not hideselect

                    else:
                        obj.select_set(True)

        self.force_all = False
        return {'FINISHED'}


from collections import deque

def solution(board: list[str]):
    answer = 0
    dx = [1,0,-1,0]
    dy = [0,1,0,-1]
    n = len(board)
    m = len(board[0])

    q = deque()
    matrix = [[0 for _ in range(len(board[0]))] for _ in range(len(board))]

    for idx in range(len(board)):
        for jdx in range(len(board[0])):
            if board[idx][jdx] == 'R':
                q.append((idx,jdx))
                matrix[idx][jdx] = 1

    while q:
        x,y = q.popleft()
        if board[x][y] == 'G':
            return matrix[x][y]-1

        for i in range(4):
            nx = x
            ny = y
            while True:
                nx += dx[i]
                ny += dy[i]
                if nx < 0 or nx >= n or ny < 0 or ny >= m:
                    nx -= dx[i]
                    ny -= dy[i]
                    break
                if 0<=nx<n and 0<=ny<m and board[nx][ny] == 'D':
                    nx -= dx[i]
                    ny -= dy[i]
                    break

            if matrix[nx][ny] == 0:
                matrix[nx][ny] = matrix[x][y]+1
                q.append((nx,ny))
    return -1

print(solution(["...D..R", ".D.G...", "....D.D", "D....D.", "..D...."]))
print(solution([".D.R", "....", ".G..", "...D"]))