from pathlib import Path
import numpy as np
from Bio.PDB import PDBParser
from rdkit import Chem
import torch
import matplotlib.pyplot as plt
import seaborn as sns

# Constants
ATOM_DICT = {"C": 0, "N": 1, "O": 2, "P": 3, "H": 4, "S": 5}  # Sample atom dictionary, can be expanded
RNA_RESIDUE_DICT = {"A": 0, "U": 1, "G": 2, "C": 3}  # RNA residues

def process_rna_and_ligand(pdbfile):
    pdb_struct = PDBParser(QUIET=True).get_structure('', pdbfile)

    # Process ligand (assuming HETATM denotes ligand atoms)
    lig_atoms = [atom for atom in pdb_struct.get_atoms() if atom.parent.id == "HETATM"]
    lig_coords = np.array([atom.coord for atom in lig_atoms])
    lig_one_hot = np.array([ATOM_DICT[atom.element] for atom in lig_atoms])

    # Process RNA pocket
    rna_residues = [res for res in pdb_struct[0].get_residues() if res.id[0] == " "]
    rna_coords = np.array([atom.coord for res in rna_residues for atom in res])
    rna_one_hot = np.array([ATOM_DICT[atom.element] for res in rna_residues for atom in res])

    return {
        'lig_coords': lig_coords,
        'lig_one_hot': lig_one_hot,
    }, {
        'rna_coords': rna_coords,
        'rna_one_hot': rna_one_hot,
    }
from rdkit.Chem import MolFromPDBBlock, MolToSmiles

def compute_smiles(pdbfile):
    """
    Compute the SMILES representation for the ligand in the given PDB file.
    """
    with open(pdbfile, 'r') as f:
        pdb_content = f.read()

    mol = MolFromPDBBlock(pdb_content)
    if mol:
        return MolToSmiles(mol)
    return None

def get_type_histograms(lig_one_hot, rna_one_hot):
    """
    Compute histograms for atom types in the ligand and residue types in the RNA.
    """
    atom_counts = {k: np.sum(lig_one_hot == v) for k, v in ATOM_DICT.items()}
    rna_residue_counts = {k: np.sum(rna_one_hot == v) for k, v in RNA_RESIDUE_DICT.items()}

    return atom_counts, rna_residue_counts

import os
from pathlib import Path
import argparse

def save_all(filename, pdb_ids, lig_coords, lig_one_hot, rna_coords, rna_one_hot):
    """
    Save the processed data to a numpy file.
    """
    np.savez(filename,
             names=pdb_ids,
             lig_coords=lig_coords,
             lig_one_hot=lig_one_hot,
             rna_coords=rna_coords,
             rna_one_hot=rna_one_hot
             )
    return True

if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument('basedir', type=Path, help="Base directory containing the PDB files.")
    parser.add_argument('--outdir', type=Path, default=None, help="Output directory for processed data.")
    args = parser.parse_args()

    # Define the directories
    data_dir = args.basedir

    # Create output directory if not specified
    if args.outdir is None:
        processed_dir = Path(data_dir, 'processed')
    else:
        processed_dir = args.outdir

    # Ensure the output directory exists
    processed_dir.mkdir(exist_ok=True, parents=True)

    lig_coords = []
    lig_one_hot = []
    rna_coords = []
    rna_one_hot = []
    pdb_ids = []

    # Iterate through all PDB files in the base directory
    for pdb_file in os.listdir(data_dir):
        if pdb_file.endswith(".pdb"):
            try:
                lig_data, rna_data = process_ligand_and_rna(os.path.join(data_dir, pdb_file))
                lig_coords.append(lig_data['lig_coords'])
                lig_one_hot.append(lig_data['lig_one_hot'])
                rna_coords.append(rna_data['rna_coords'])
                rna_one_hot.append(rna_data['rna_one_hot'])
                pdb_ids.append(pdb_file)
            except Exception as e:
                print(f"Failed processing {pdb_file} due to {str(e)}")

    # Convert lists to numpy arrays
    lig_coords = np.array(lig_coords)
    lig_one_hot = np.array(lig_one_hot)
    rna_coords = np.array(rna_coords)
    rna_one_hot = np.array(rna_one_hot)

    # Save the processed data
    save_all(processed_dir / 'processed_data.npz', pdb_ids, lig_coords, lig_one_hot, rna_coords, rna_one_hot)

    print("Processing completed!")
import warnings
from rdkit import Chem
from rdkit.Chem.rdForceFieldHelpers import UFFOptimizeMolecule, UFFHasAllMoleculeParams
import seaborn as sns
import matplotlib.pyplot as plt

def compute_smiles(positions, one_hot, ATOM_DICT):
    """
    Compute SMILES strings for ligands.
    """
    print("Computing SMILES ...")

    atom_types = [ATOM_DICT[idx] for idx in np.argmax(one_hot, axis=-1)]
    mol = Chem.MolFromXYZBlock(positions, atomTypes=atom_types)

    # Optimize molecule geometry
    try:
        UFFOptimizeMolecule(mol)
    except:
        print("UFF optimization failed. Skipping...")
        return None

    return Chem.MolToSmiles(mol)

def get_type_histograms(lig_one_hot, rna_one_hot, ATOM_DICT, nucleotide_dict):
    """
    Get histograms of ligand and RNA types.
    """
    atom_counts = {k: 0 for k in ATOM_DICT.keys()}
    for a in [ATOM_DICT[x] for x in lig_one_hot.argmax(1)]:
        atom_counts[a] += 1

    nuc_counts = {k: 0 for k in nucleotide_dict.keys()}
    for r in [nucleotide_dict[x] for x in rna_one_hot.argmax(1)]:
        nuc_counts[r] += 1

    return atom_counts, nuc_counts

def plot_histogram(data, title, save_path):
    """
    Plot and save a histogram.
    """
    sns.distplot(data)
    plt.title(title)
    plt.savefig(save_path)
    plt.clf()

# Compute statistics & additional information
if __name__ == "__main__":
    with np.load(processed_dir / 'processed_data.npz', allow_pickle=True) as data:
        lig_one_hot = data['lig_one_hot']
        rna_one_hot = data['rna_one_hot']

    # Compute SMILES for ligands
    lig_smiles = compute_smiles(lig_coords, lig_one_hot, ATOM_DICT)
    with open(processed_dir / 'ligand_smiles.txt', 'w') as f:
        for s in lig_smiles:
            f.write(s + "\n")

    # Histograms of ligand and RNA types
    atom_hist, nuc_hist = get_type_histograms(lig_one_hot, rna_one_hot, ATOM_DICT, nucleotide_dict)

    # Plot histograms
    plot_histogram(list(atom_hist.values()), "Ligand Atom Distribution", processed_dir / 'ligand_atom_distribution.png')
    plot_histogram(list(nuc_hist.values()), "RNA Nucleotide Distribution", processed_dir / 'rna_nucleotide_distribution.png')

    print("Summary statistics generated!")
import json

def save_processed_data(filename, ligand_data, rna_data):
    """
    Save the processed ligand and RNA data.
    """
    with open(filename, 'w') as f:
        json.dump({
            "ligand_data": ligand_data,
            "rna_data": rna_data
        }, f)

    print(f"Data saved to {filename}")

if __name__ == "__main__":
    # Assuming you've loaded and processed data into `ligand_data` and `rna_data` dictionaries
    save_processed_data(processed_dir / 'final_processed_data.json', ligand_data, rna_data)

    # Create summary string
    summary_string = '# SUMMARY\n\n'
    summary_string += 'Processed RNA-Ligand Data\n\n'
    summary_string += '# Ligand Data\n'
    for key, value in ligand_data.items():
        summary_string += f"{key}: {len(value)} entries\n"

    summary_string += '\n# RNA Data\n'
    for key, value in rna_data.items():
        summary_string += f"{key}: {len(value)} entries\n"

    # Save summary
    with open(processed_dir / 'summary.txt', 'w') as f:
        f.write(summary_string)

    # Print summary
    print(summary_string)


import numpy as np
# import pyLDAvis
import argparse
import pickle
# import math
#from biterm.cbtm import oBTM
from biterm.btm import oBTM
from sklearn.feature_extraction.text import CountVectorizer
# from sklearn.feature_extraction.text import TfidfVectorizer
from biterm.utility import vec_to_biterms, topic_summuary

if __name__ == "__main__":

    texts = open('./data/comments.txt',encoding="utf8").read().splitlines()[:200]

    # # # n = 400

    # vectorize texts
    vec = CountVectorizer(stop_words='english')
    # # ,min_df=0.0001,max_df=0.05
    # # X = vec.fit_transform(texts).toarray()
    X = np.array(vec.fit_transform(texts).toarray())
    # # print(X)

    print('get vocabulary')
    vocab = np.array(vec.get_feature_names())

    # # print(vocab[:500])
    # # print(len(vocab))

    print('get biterms')
    biterms = vec_to_biterms(X)
    # # print(biterms[0])
    # # print(len(biterms))

    # print('create btm')
    # btm = oBTM(num_topics=30, V=vocab)

    # print("\n\n Train Online BTM ..")
    # for i in range(0, int(len(biterms)), 100): # prozess chunk of 200 texts
    #     biterms_chunk = biterms[i:i + 100]
    #     btm.fit(biterms_chunk, iterations=50)
    # # biterms_chunk = biterms[:n]
    # # btm.fit(biterms_chunk, iterations=50)

    # object_btm = btm
    # file_btm_obj = open('btm.obj', 'wb')
    # pickle.dump(object_btm, file_btm_obj)

    file_btm_obj = open('btm.obj','rb')
    btm = pickle.load(file_btm_obj)

    topics = btm.transform(biterms)

    # print("\n\n Visualize Topics ..")
    # vis = pyLDAvis.prepare(btm.phi_wz.T, topics, np.count_nonzero(X, axis=1), vocab, np.sum(X, axis=0))
    # # pyLDAvis.save_html(vis, './vis/online_btm_' + str(n) + '.html')
    # pyLDAvis.save_html(vis, './vis/online_btm_' + 'online_all2' + '.html')

    print("\n\n Topic coherence ..")
    topic_summuary(btm.phi_wz.T, X, vocab, 20)

    print("\n\n Texts & Topics ..")

    for k in range(10):
        j = 0
        for i in range(len(texts)):
            if topics[i].argmax() == k:
                print("{} (topic: {})".format(texts[i], topics[i].argmax()))
                j += 1
                if j > 10:
                    break


import json
import requests
import time

def getIP():
    r = requests.get("https://what-is-my-ip.justyy.workers.dev/")
    return r.text.replace('"','')

def getStatus(update:bool=False, ip:str=False):
    if ip == False:
        ip = getIP()
    r = requests.get(f"https://api.mcsrvstat.us/2/{ip}")
    if update:
        r = requests.get(f"https://api.mcsrvstat.us/1/{ip}")
    j = json.loads(r.text)

    jData = ["online", "version", "players", "list", "ip"]

    returnData = '{ "online":"None", "ip":"None", "version":"No info", "players":"0", "list":"No one"}'
    returnData = json.loads(returnData)
    for data in jData:
        try:
            returnData[data] = j[data]
        except: pass
    return returnData

def getTime(GTime):
    tTime = time.time() - GTime
    if tTime > 10*60+10:
        GTime = time.time()
    timeLeft = 10*60+10 - tTime
    return int(timeLeft)

if __name__ == "__main__":
    print(getStatus())


import unittest

from lxml import etree
from xslproxy import xsl

class XslQueryParserTestCase(unittest.TestCase):

    def test_query_string_with_params(self):
        """
        Test query string parsing with parameters
        """

        # Generate with:
        # urllib.parse.unquote(urllib.parse.urlencode([
        #   ("xsl[]", "first"),
        #   ("xsl[]", "sécönd"),
        #   ("sp[first][string-param]", "héllö wôrld"),
        #   ("sp[first][string-param-2]", "http://example.com"),
        #   ("xp[first][xpath-param]", "//a/b[@c=\"d\"]")
        # ])).encode('utf-8')

        qs = b'xsl[]=first' + \
            b'&xsl[]=s\xc3\xa9c\xc3\xb6nd' + \
            b'&sp[first][string-param]=h\xc3\xa9ll\xc3\xb6+w\xc3\xb4rld' + \
            b'&sp[first][string-param-2]=http://example.com' + \
            b'&xp[first][xpath-param]=//a/b[@c="d"]'

        xsls, paths, params = xsl.XslQueryStringParser().parse(qs)

        expected_xsls = ["first", "sécönd"]
        self.assertEqual(xsls, expected_xsls)

        expected_paths = {"first": "first", "sécönd": "sécönd"}
        self.assertEqual(paths, expected_paths)

        # These assertions are rather brittle. Regrettably XPath and XSLT
        # quoted strings do not implement equality or proper hashing. Thus we
        # have to inspect repr and instanceof respectively.
        self.assertEqual(len(params), 2)
        self.assertIn("first", params)
        self.assertIn("sécönd", params)

        self.assertEqual(len(params["first"]), 3)
        self.assertIn(
            "_XSLTQuotedStringParam", repr(params["first"]["string-param"]))
        self.assertIn(
            "_XSLTQuotedStringParam", repr(params["first"]["string-param-2"]))
        self.assertIsInstance(
            params["first"]["xpath-param"], etree.XPath)

        self.assertEqual(len(params["sécönd"]), 0)

    def test_query_string_with_alias(self):
        """
        Test query string parsing with parameters
        """

        # Generate with:
        # urllib.parse.unquote(urllib.parse.urlencode([
        #   ("xsl[]", "first"),
        #   ("xsl[]", "twö"),
        #   ("xa[twö]", "path/to/sécönd"),
        # ])).encode('utf-8')

        qs = b'xsl[]=first' + \
            b'&xsl[]=tw\xc3\xb6' + \
            b'&xa[tw\xc3\xb6]=path/to/s\xc3\xa9c\xc3\xb6nd'

        xsls, paths, params = xsl.XslQueryStringParser().parse(qs)

        expected_xsls = ["first", "twö"]
        self.assertEqual(xsls, expected_xsls)

        expected_paths = {"first": "first", "twö": "path/to/sécönd"}
        self.assertEqual(paths, expected_paths)

        expected_params = {"first": {}, "twö": {}}
        self.assertEqual(params, expected_params)


import logging
import subprocess

_log = logging.getLogger(__name__)

def _call(command, quiet=False):
    _log.info('EXEC%s: %s',
              '(quiet)' if quiet else '',
              ' '.join(command))
    if quiet:
        subprocess.check_call(command,
                              stdout=subprocess.DEVNULL,
                              stderr=subprocess.DEVNULL)
    else:
        subprocess.check_call(command)

def gs_to_public_url(gcs_path):
    assert gcs_path.startswith('gs://')
    return gcs_path.replace('gs://', 'https://storage.googleapis.com/', 1)

def rsync_gzip(path1, path2, quiet=False):
    """Syncs path1 to path2 with gsutil rsync.

    All files in path1 are considered gzipped, and the 'Content-Encoding:gzip'
    header will be set for all files.

    Args:
        path1, path2: The source and destination paths (must be directories).
    """
    # Use parallel processes and no multithreading to avoid Python GIL.
    # https://cloud.google.com/storage/docs/gsutil/commands/rsync#options
    command = [
        'gsutil', '-o', 'GSUtil:parallel_process_count=10',
        '-o', 'GSUtil:parallel_thread_count=1',
        '-m', '-h', 'Content-Encoding:gzip', 'rsync', '-r',
        path1, path2
    ]
    _call(command, quiet)

def copy(path1, path2, gzipped=False, quiet=False):
    """Copies path1 to path2 with gsutil cp.

    Args:
        path1, path2: The source and destination paths.
        gzipped: Whether path1 is gzipped (if True, 'Content-Encoding:gzip'
            will be added to the headers).
    """
    command = ['gsutil', '-m']
    if gzipped:
        command += ['-h', 'Content-Encoding:gzip']
    command += ['cp', '-r', path1, path2]
    _call(command, quiet)


# -*- coding: utf-8 -*-

# View all:
    # python stats.py -in downloads/nsynth-train/examples.json
# Sample queries:
    # python stats.py -filter "instrument_family_str=bass&instrument_source_str=synthetic"
    # python stats.py -in downloads/nsynth-train/examples.json -filter "instrument_family_str=mallet&instrument_source_str=acoustic&pitch=<60&pitch=>40&velocity=<75"

import argparse
import collections
import json
from lib import *
import math
from matplotlib import pyplot as plt
import os
import numpy as np
from pprint import pprint
import sys

# input
parser = argparse.ArgumentParser()
parser.add_argument('-in', dest="INPUT_JSON", default="downloads/nsynth-test/examples.json", help="Input JSON file")
parser.add_argument('-filter', dest="FILTER", default="", help="Comma-separted pairs, e.g. instrument_family_str=bass&instrument_source_str=synthetic")
args = parser.parse_args()

# Parse arguments
INPUT_JSON = args.INPUT_JSON
FILTERS = [] if len(args.FILTER) <= 0 else [tuple(f.split("=")) for f in args.FILTER.split("&")]

PLOT_SIZE = (24, 8)
ROWS = 2
MAX_LABELS = 12

print("Reading %s..." % INPUT_JSON)
dataIn = {}
with open(INPUT_JSON) as f:
    dataIn = json.load(f)
files = list(dataIn.keys())
print("Loaded %s with %s entries" % (INPUT_JSON, len(files)))

# Make a list of dicts, sorted by name
files.sort()
files = [dataIn[f] for f in files]
# pprint(files[:3])

files = filterResults(files, FILTERS)

fileCount = len(files)
if len(FILTERS) > 0:
    print("Found %s files after filtering." % fileCount)

if fileCount <= 0:
    sys.exit()

instruments = list(set([f["instrument_str"] for f in files]))
print("%s unique instruments found." % len(instruments))

dataKeys = [
    ("instrument_family_str", "Family"),
    ("instrument_source_str", "Source"),
    ("pitch", "Pitch"),
    ("qualities_str", "Quality"),
    ("velocity", "Velocity"),
    ("instrument", "Unique instrument")
]
keyCount = len(dataKeys)
COLS = int(math.ceil(1.0 * keyCount / ROWS))

plt.figure(figsize=PLOT_SIZE)
for i, pair in enumerate(dataKeys):
    key, label = pair
    isList = isinstance(files[0][key], (list,))
    if isList:
        values = []
        for f in files:
            values += f[key]
        values = [str(v) for v in values]
    else:
        values = [str(f[key]) for f in files]
    counter = collections.Counter(values)
    counted = counter.most_common()

    ys = [c[1] for c in counted]
    xs = np.arange(len(ys))

    # set subplot
    ax = plt.subplot(ROWS, COLS, i+1)
    ax.set_title(label)

    # too many values, just plot it
    if len(xs) > MAX_LABELS:
        ys = [f[key] for f in files]
        plt.hist(ys, bins=50)

    # otherwise, show bargraph
    else:
        xLabels = [c[0] for c in counted]
        for i, y in enumerate(ys):
            ax.text(xs[i], y * 0.5, str(y), va="center", ha="center")
        plt.xticks(xs, tuple(xLabels), rotation=45)
        plt.bar(xs, ys)
plt.tight_layout()
plt.show()


"""

Remove unique constraint

Revision ID: 235d90776c82
Revises: 257e0b50e1b2
Create Date: 2017-04-02 16:21:47.112784

"""
from alembic import op
import sqlalchemy as sa

# revision identifiers, used by Alembic.
revision = '235d90776c82'
down_revision = '257e0b50e1b2'
branch_labels = None
depends_on = None

def upgrade():
    with op.batch_alter_table('registration') as batch_op:
        batch_op.drop_constraint('uq_registration_priority_uni', type_='unique')

def downgrade():
    with op.batch_alter_table('registration') as batch_op:
        batch_op.create_unique_constraint('uq_registration_priority_uni', ['priority','uni_id'])


#!/usr/bin/env python
import configparser
import os
import shutil
import sys
import tarfile
import textwrap
from datetime import date
from pathlib import Path

import pandas as pd
from Bio import SeqIO
from Bio.Seq import Seq
from Bio.SeqRecord import SeqRecord

from phylofisher import help_formatter, tools

def parse_collapse_tsv():
    '''
    Parses to collapse tsv file

    :return: chimera info dictionary with higher, lower, and unique ids
    :rtype: dict
    '''
    with open(args.input, 'r') as infile:
        collapse_dict = dict()
        for line in infile:
            # Check to make sure the correct number of columns are present
            try:
                chimera_id, long_name, higher, lower, to_collapse = line.strip().split('\t')
            except ValueError:
                raise Exception('Error parsing to_collapse.tsv. Please make sure the file is formatted correctly.')

            # Add parsed information to dictionary
            collapse_dict[chimera_id] = {
                'long_name': long_name,
                'higher': higher,
                'lower': lower,
                'to_collapse': to_collapse.split(',')
            }

    return collapse_dict

def check_metadata(collapse_dict):
    '''
    Checks to make sure taxa provided in to_collapse.tsv are actually in the metadata

    :param collapse_dict: information about taxa to collapse
    :type collapse_dict: dict
    '''
    to_collapse = [taxon for k in collapse_dict.keys() for taxon in collapse_dict[k]['to_collapse']]
    taxon_dict = dict.fromkeys(to_collapse, False)

    # Loop through metadata and check if taxa are in metadata
    with open(metadata, 'r') as infile:
        for line in infile:
            line = line.strip()
            short_name = line.split('\t')[0]
            if short_name in to_collapse:
                taxon_dict[short_name] = True

    # Check if all taxa are in metadata
    for taxon in taxon_dict.keys():
        if taxon_dict[taxon] is False:
            print(f'{taxon} is not in the dataset.')

    # Raise error if any taxa are not in metadata
    if False in taxon_dict.values():
        raise Exception('Taxa not collapsed. Please fix errors above.')

def add_to_meta(collapse_dict):
    '''
    Transfer input metadata for a given organism to dataset metadata.

    :param collapse_dict: information about taxa to collapse
    :type collapse_dict: dict
    '''
    for chimera_id in collapse_dict.keys():
        # Create new row dictionary
        new_row = {'Unique ID': chimera_id,
                'Long Name': collapse_dict[chimera_id]['long_name'],
                'Higher Taxonomy': collapse_dict[chimera_id]['higher'],
                'Lower Taxonomy': collapse_dict[chimera_id]['lower'],
                'Source': 'taxon_collapser.py',
                'Data Type': 'Collapsed'
                }

        # Read metadata into dataframe
        df = pd.read_csv(metadata, delimiter='\t')
        df.index = df['Unique ID']

        # Add new row to dataframe
        df = df.append(new_row, ignore_index=True)
        df = df.sort_values(by=['Higher Taxonomy', 'Lower Taxonomy', 'Unique ID'])
        df.to_csv(str(Path(dfo, 'metadata.tsv')), sep='\t', index=False)

def collapse(collapse_dict):
    '''
    Collapses sequences in the dataset.

    :param collapse_dict: information about taxa to collapse
    :type collapse_dict: dict
    '''

    # Collapse Ortholgogs
    ortho_dir = f'{dfo}/orthologs'
    gene_files = [os.path.join(ortho_dir, gene) for gene in os.listdir(ortho_dir) if gene.endswith('.fas')]
    for gene_file in gene_files:
        record_dict = {k: SeqRecord(Seq(''), id='', name='', description='') for k in collapse_dict.keys()}
        records = []
        with open(gene_file, 'r') as infile:
            for record in SeqIO.parse(infile, 'fasta'):
                for chimera_id in collapse_dict.keys():
                    if record.description in collapse_dict[chimera_id]['to_collapse']:
                        if len(record.seq) > len(record_dict[chimera_id].seq):
                            record_dict[chimera_id].id = chimera_id
                            record_dict[chimera_id].seq = record.seq

                # Append original sequences to records
                records.append(record)

        # Append collapsed sequences to records
        for record in record_dict.values():
            if str(record.seq) != '':
                records.append(record)

        # Write ortholog file post collapse
        with open(gene_file, 'w') as outfile:
            SeqIO.write(records, outfile, 'fasta')

        record_dict.clear()

    # Collapse Paralogs
    para_dir = f'{dfo}/paralogs'
    gene_files = [os.path.join(para_dir, gene) for gene in os.listdir(para_dir) if gene.endswith('.fas')]
    for gene_file in gene_files:
        records = []
        with open(gene_file, 'r') as infile:
            for record in SeqIO.parse(infile, 'fasta'):
                for chimera_id in collapse_dict.keys():
                    if record.description.split('..')[0] in collapse_dict[chimera_id]['to_collapse']:
                        records.append(SeqRecord(record.seq, id=f'{chimera_id}..{record.description.split("..")[1]}', name='', description=''))

                records.append(record)

        # Write ortholog file post collapse
        with open(gene_file, 'w') as outfile:
            SeqIO.write(records, outfile, 'fasta')

        record_dict.clear()

    add_to_meta(collapse_dict)

if __name__ == '__main__':
    description = 'Collapses dataset entries into a single taxon'
    parser, optional, required = help_formatter.initialize_argparse(name='taxon_collapser.py',
                                                                    desc=description,
                                                                    usage='taxon_collapser.py [OPTIONS] -i <taxa>')

    required.add_argument('-i', '--input', type=str, metavar='to_collapse.tsv', default=None,
                          help=textwrap.dedent("""\
                          A .tsv containing a Unique ID, long name, higher taxonomy, lower taxonomy, and a list of Unique IDs to collapse
                          for each chimera with one on each line.

                          Example:
                            Chimera_ID 1\tLong Name\tHigher_Tax\tLower_Tax\tUnique_ID_1,Unique_ID_2
                            Chimera_ID 2\tLong Name\tHigher_Tax\tLower_Tax\tUnique_ID_3,Unique_ID_4
                        """))
    args = help_formatter.get_args(parser, optional, required,
                                   out_dir=False, pre_suf=False, inp_dir=False)

    # Config Parser
    config = configparser.ConfigParser()
    config.read('config.ini')
    dfo = str(Path(config['PATHS']['database_folder']).resolve())
    metadata = str(os.path.join(dfo, 'metadata.tsv'))

    tools.backup(dfo)
    collapse_dict = parse_collapse_tsv()
    check_metadata(collapse_dict)
    collapse(collapse_dict)


import pickle, base64
from django_redis import get_redis_connection

def merge_carts_cookies_redis(request, response):
    """合并购物车"""
    # 获取cookie中的购物车数据
    carts_str = request.COOKIES.get('carts')

    if not carts_str:  # 判断是否有购物车cookie
        return response  # 不存在，直接返回

    cookie_carts_str_bytes = carts_str.encode()
    cookie_carts_dict_bytes = base64.b64decode(cookie_carts_str_bytes)
    cookie_cart_dict = pickle.loads(cookie_carts_dict_bytes)  # 读取购物车cookie

    new_carts_dict = {}
    new_selected_add = []
    new_selected_rem = []

    # 遍历cookies中的购物车数据
    for sku_id, cookie_dict in cookie_cart_dict.items():
        new_carts_dict[sku_id] = cookie_dict['count']
        if cookie_dict['selected']:
            new_selected_add.append(sku_id)
        else:
            new_selected_rem.append(sku_id)

    # 存在购物车cookie，合并
    user = request.user
    redis_conn = get_redis_connection('carts')
    pl = redis_conn.pipeline()
    pl.hmset('carts_%s' % user.id, new_carts_dict)
    if new_selected_add:
        pl.sadd('selected_%s' % user.id, *new_selected_add)
    if new_selected_rem:
        pl.srem('selected_%s' % user.id, *new_selected_rem)
    pl.execute()

    # 删除cookie
    response.delete_cookie('carts')
    return response


from itertools import count

import matplotlib.pyplot as plt
import torch

from src.game_window import GameWindow
from src.ml.DQN import device
from src.ml.InputExtraction import screen, clock
from src.ml.Training import memory, policy_net, target_net, get_screen, select_action, \
    episode_durations, plot_durations, scores, TARGET_UPDATE, plot_score
from src.ml.TrainingLoop import optimize_model

game = GameWindow(screen)

# while not done:
done = game.process_events()
game.run_logic()
game.display_frame()
clock.tick(1)

plt.figure()
plt.imshow(get_screen().cpu().squeeze(0).permute(1, 2, 0).numpy(),
           interpolation='none')
plt.title('Example extracted screen')
plt.show()

num_episodes = 100
for i_episode in range(num_episodes):
    # Initialize the environment and state
    game.reset()
    last_screen = get_screen()
    current_screen = get_screen()
    state = current_screen - last_screen
    for t in count():
        # Select and perform an action
        previous_score = game.game.score
        action = select_action(state)
        game.step(action.item())
        game.process_events()
        done = game.game.game_over
        game.run_logic()
        game.display_frame()
        reward = game.game.score - previous_score
        reward = torch.tensor([reward], device=device)

        # Observe new state
        last_screen = current_screen
        current_screen = get_screen()
        if not done:
            next_state = current_screen - last_screen
        else:
            next_state = None

        # Store the transition in memory
        memory.push(state, action, next_state, reward)

        # Move to the next state
        state = next_state

        # Perform one step of the optimization (on the policy network)
        optimize_model()
        if done:
            episode_durations.append(t + 1)
            plot_durations()
            scores.append(game.game.score)
            break
    # Update the target network, copying all weights and biases in DQN
    if i_episode % TARGET_UPDATE == 0:
        target_net.load_state_dict(policy_net.state_dict())

print('Complete')
plt.ioff()
plt.clf()
plot_score()


# coding: utf-8
from five import grok
from Products.CMFCore.utils import getToolByName

from vindula.tile.browser.baseview import BaseView
from vindula.tile.content.interfaces import ITileLibrary
from vindula.content.models.tag_content import TagContent

from collections import OrderedDict

grok.templatedir('templates')

class LibraryView(BaseView):
    grok.name('library-view')

    def getThemes(self):
        rs_themes = TagContent.getAllTagsByType('themesNews')

        themes = []

        if rs_themes and rs_themes.count():
            if self.context.getQtdThemesGlobal() and self.context.getQtdThemesGlobal() > 0:
                rs_themes = rs_themes[:self.context.getQtdThemesGlobal()]

            for theme in  rs_themes:
                themes.append(theme)

        return themes

class ThemeContentsView(BaseView):
    grok.context(ITileLibrary)
    grok.name('theme-contents')

    theme = None

    def update(self):
        if self.request.form.get('theme'):
            self.theme = TagContent.getTagById(self.request.form.get('theme'))

    def getTypologies(self, theme):
        p_catalog = getToolByName(self.context, 'portal_catalog')
        typologies = {}
        if theme:
            value = theme.value

            brains = p_catalog(ThemeNews=value)
            for brain in brains:
                if not brain.tipo or brain.portal_type == 'Image':
                    if brain.portal_type == 'Image':
                        if typologies.get(brain.tipo):
                            typologies[brain.tipo].append(brain.getObject())
                        else:
                            typologies[brain.tipo] = [brain.getObject()]
                    continue

                if typologies.get(brain.tipo):
                    typologies[brain.tipo].append(brain.getObject())
                else:
                    typologies[brain.tipo] = [brain.getObject()]
        od = OrderedDict(sorted(typologies.items(), key=lambda t: t[0]))
        typologies = od.items()

        typologies = OrderedDict(typologies)

        for typology in typologies.keys():
            data = TagContent().store.find(TagContent,
                                              TagContent.type==u'typology',
                                              TagContent.value==typology.decode('utf-8'),
                                              TagContent.deleted==False)
            if data.count():
                data = data[0]

                if data.sort_on == 'sortable_title':
                    typologies[typology] = sorted(typologies.get(typology), key=lambda x: x.title.lower())
                elif data.sort_on == 'created':
                    typologies[typology] = sorted(typologies.get(typology), key=lambda x: x.created())

                if data.order ==  'desc':
                    typologies.get(typology).reverse()

            else:
                continue

        return typologies

    def getImageUrlByType(self, obj):
        base = self.context.portal_url() + "/++resource++vindula.content/images/"
        type = obj.portal_type
        url = ''

        if type == 'VindulaPhotoAlbum':
            photos = obj.contentValues()
            if photos:
                url = photos[0].absolute_url() + '/image_preview'
        elif type in ['VindulaVideo', 'VindulaStreaming']:
            if type == 'VindulaVideo':
                photo = obj.getImage_preview()
            else:
                photo = obj.getImage()

            if photo:
                url = photo.absolute_url()
        else:
            if self.context.getIconFileDefault():
                url = self.context.getIconFileDefault().absolute_url() + '/image_tile'

        if not url:
            url = base + "icon-default.png"

        return url


from flask import Flask, render_template, request
app = Flask(__name__)

@app.route('/mypage/contact', methods=['GET', 'POST'])
def message():
   if request.method == 'GET':
       print("We received GET")
       return render_template("contact.html")
   elif request.method == 'POST':
       print("We received POST")
       print(request.form)
       return redirect("/")

@app.route('/mypage/me', methods=['GET'])
def greeting():
    return render_template("me.html")

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


from jinja2 import Environment, FileSystemLoader
from pathlib import Path
import pathlib

env = Environment(loader=FileSystemLoader('./template'))
template = env.get_template('page.j2')
page_head_title   = 'ASS FX Template Viewer'
inf_css_path = 'css-js/infinite-scroll-docs.css'
inf_js_path  = 'css-js/infinite-scroll-docs.min.js'
page_to_index_url = 'index.html'
index_page_name = 'Main Page / TOP'
page_title = 'ASS FX Flow'

def gen_pages(img_list, idx=2, is_last_page=True):
    global template
    next_page_link = f'page{idx + 1}.html'

    with open(f"html/page{idx}.html", 'w') as fout:
        html_content = template.render(
            # CSS/JS
            inf_css_path=inf_css_path,
            inf_js_path=inf_js_path,
            # title/section name
            page_head_title=page_head_title,
            page_to_index_url=page_to_index_url,
            index_page_name=index_page_name,
            page_title=page_title,
            # data
            img_list=img_list,
            is_last_page=is_last_page,
            next_page_link=next_page_link,
        )
        fout.write(html_content)

def gen_main_page(img_list, idx=1, is_last_page=True):
    global template
    next_page_link = f'page{idx + 1}.html'

    with open("html/index.html", 'w') as fout:
        html_content = template.render(
            # CSS/JS
            inf_css_path=inf_css_path,
            inf_js_path=inf_js_path,
            # title/section name
            page_head_title=page_head_title,
            page_to_index_url=page_to_index_url,
            index_page_name=index_page_name,
            page_title=page_title,
            # data
            img_list=img_list,
            is_last_page=is_last_page,
            next_page_link=next_page_link,
        )
        fout.write(html_content)

# if __name__ == "__main__":

img_root_path = Path(r'F:\subt\Aegisub-Karaoke-Templates\Ass-Fx-Viewer\html\img')
gifs = list(img_root_path.glob('*.gif'))
# webps = list(img_root_path.glob('*.webp'))
imgs = gifs

steps = 12
rpath = [ ('img/' + p.stem + '.gif', 'ass/' + p.stem + '.ass') for p in imgs[0:steps] ]
gen_main_page(rpath, is_last_page=False)
imgs = imgs[steps:]

total_pages = 100
img_count = 0
for page in range(1, total_pages + 1):
    img_list = imgs[ (page*steps) : ((page+1)*steps) ]
    rpath = [ ('img/' + p.stem + '.gif', 'ass/' + p.stem + '.ass') for p in img_list ]
    img_count = img_count + len(rpath)

    if page==total_pages or img_count>=len(imgs) or len(rpath)==0:
        is_last_page = True
    else:
        is_last_page = False

    gen_pages(rpath, idx=(page+1), is_last_page=is_last_page)

    if is_last_page:
        print(f'总共 {page + 1} 页')
        print(f'总共 {img_count + steps} 张图')
        break


from threading import Thread, Semaphore
from multiprocessing import Process, cpu_count
from sys import exit as sysexit, version_info as sys_version_info
from os import _exit as osexit
from time import sleep, time

# =============================================
# check min, python version
if sys_version_info < (3, 4):
    raise SystemError("QTPyLib requires Python version >= 3.4")
# =============================================

class multitasking():
    """
    Non-blocking Python methods using decorators
    (a class-based implementation of the multitasking library)
    https://github.com/ranaroussi/multitasking
    """

    __KILL_RECEIVED__ = False
    __TASKS__ = []

    # processing
    __CPU_CORES__ = cpu_count()
    __POOLS__ = {}
    __POOL_NAME__ = "Main"

    @classmethod
    def getPool(cls, name=None):
        if name is None:
            name = cls.__POOL_NAME__

        return {
            "engine": "thread" if cls.__POOLS__[cls.__POOL_NAME__
                                                ]["engine"] == Thread else "process",
            "name": name,
            "threads": cls.__POOLS__[cls.__POOL_NAME__]["threads"]
        }

    @classmethod
    def createPool(cls, name="main", threads=None, engine="thread"):

        cls.__POOL_NAME__ = name

        # if threads is None:
        #     threads = cls.__CPU_CORES__

        try:
            threads = int(threads)
        except Exception as e:
            threads = 1

        # 1 thread is no threads
        if threads < 2:
            threads = 0

        cls.__POOLS__[cls.__POOL_NAME__] = {
            "pool": Semaphore(threads) if threads > 0 else 1,
            "engine": Process if "process" in engine.lower() else Thread,
            "name": name,
            "threads": threads
        }

    @classmethod
    def task(cls, callee):

        # create default pool if nont exists
        if not cls.__POOLS__:
            cls.createPool()

        def _run_via_pool(*args, **kwargs):
            with cls.__POOLS__[cls.__POOL_NAME__]['pool']:
                return callee(*args, **kwargs)

        def async_method(*args, **kwargs):
            # no threads
            if cls.__POOLS__[cls.__POOL_NAME__]['threads'] == 0:
                return callee(*args, **kwargs)

            # has threads
            if not cls.__KILL_RECEIVED__:
                task = cls.__POOLS__[cls.__POOL_NAME__]['engine'](
                    target=_run_via_pool, args=args, kwargs=kwargs, daemon=False)
                cls.__TASKS__.append(task)
                task.start()
                return task

            return None

        return async_method

    @classmethod
    def wait_for_tasks(cls):
        cls.__KILL_RECEIVED__ = True

        if cls.__POOLS__[cls.__POOL_NAME__]['threads'] == 0:
            return True

        try:
            running = len([t.join(1)
                           for t in cls.__TASKS__ if t is not None and t.isAlive()])
            while running > 0:
                running = len(
                    [t.join(1) for t in cls.__TASKS__ if t is not None and t.isAlive()])
        except Exception as e:
            pass
        return True

    @classmethod
    def killall(cls):
        cls.__KILL_RECEIVED__ = True
        try:
            sysexit(0)
        except SystemExit:
            osexit(0)

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

class RecurringTask(Thread):
    """Calls a function at a sepecified interval."""

    def __init__(self, func, interval_sec, init_sec, *args, **kwargs):
        """Call `func` every `interval_sec` seconds.

        Starts the timer.

        Accounts for the runtime of `func` to make intervals as close to `interval_sec` as possible.
        args and kwargs are passed to Thread().

        :Parameters:
            func : object
                Function to invoke every N seconds
            interval_sec : int
                Call func every this many seconds
            init_sec : int
                Wait this many seconds initially before the first call
            *args : mixed
                parameters sent to parent Thread class
            **kwargs : mixed
                parameters sent to parent Thread class
        """

        # threading.Thread.__init__(self, *args, **kwargs) # For some reason super() doesn't work
        super().__init__(*args, **kwargs)  # Works!
        self._func = func
        self.interval_sec = interval_sec
        self.init_sec = init_sec
        self._running = True
        self._functime = None  # Time the next call should be made

        self.start()

    def __repr__(self):
        return 'RecurringTask({}, {}, {})'.format(self._func, self.interval_sec, self.init_sec)

    def run(self):
        """Start the recurring task."""
        if self.init_sec:
            sleep(self.init_sec)
        self._functime = time()
        while self._running:
            start = time()
            self._func()
            self._functime += self.interval_sec
            if self._functime - start > 0:
                sleep(self._functime - start)

    def stop(self):
        """Stop the recurring task."""
        self._running = False


from traceback import format_exc
from time import sleep, time
from mmap import mmap
from os.path import getmtime
from pyACC import *
from psutil import pid_exists
import os, re
from setting_define import *
car_angle = {'alpine_a110_gt4':720,
 'aston_martin_v8_vantage_gt4':640,
 'amr_v8_vantage_gt4':640,
 'audi_r8_lms_gt4':720,
 'audi_r8_gt4':720,
 'bmw_m4_gt4':500,
 'bmw_m4_gt3':500,
 'chevrolet_camaro_gt4.r':720,
 'chevrolet_camaro_gt4r':720,
 'ginetta_g55_gt4':720,
 'ktm_xbow_gt4':580,
 'ktm_x_bow_gt4':580,
 'maserati_granturismo_mc_gt4':900,
 'maserati_mc_gt4':900,
 'mclaren_570s_gt4':480,
 'mercedes_amg_gt4':500,
 'porsche_718_cayman_gt4':800,
 'porsche_718_cayman_gt4_mr':800,
 'amr_v12_vantage_gt3':640,
 'audi_r8_lms':720,
 'bentley_continental_gt3_2015':640,
 'bentley_continental_gt3_2016':640,
 'bentley_continental_gt3_2018':640,
 'bmw_m6_gt3':565,
 'emil_frey_jaguar_gt3':720,
 'jaguar_g3':720,
 'ferrari_488_gt3':480,
 'honda_nsx_gt3':620,
 'lamborghini_huracan_gt3':620,
 'lamborghini_huracan_st':620,
 'lexus_rc_f_gt3':640,
 'mclaren_650s_gt3':480,
 'mercedes_amg_gt3':640,
 'nissan_gt_r_gt3_2015':640,
 'nissan_gt_r_gt3_2018':640,
 'nissan_gt_r_gt3_2017':640,
 'porsche_991_gt3_r':800,
 'porsche_991ii_gt3_cup':800,
 'reiter_engineering_r_ex_gt3':720,
 'aston_martin_racing_v8_vantage_gt3':640,
 'amr_v8_vantage_gt3':640,
 'audi_r8_lms_evo':720,
 'bentley_continental_gt3':640,
 'bmw_m6_gt3':565,
 'ferrari_488_gt3':480,
 'ferrari_488_gt3_evo':480,
 'honda_nsx_gt3_evo':620,
 'lamborghini_huracan_gt3':620,
 'lamborghini_huracan_gt3_evo':620,
 'lamborghini_gallardo_rex':620,
 'lexus_rc_f_gt3':640,
 'mclaren_720s_gt3':480,
 'mercedes_amg_gt3':640,
 'mercedes_amg_gt3_evo':640,
 'nissan_gt_r_gt3':640,
 'porsche_991ii_gt3_r':800}

class pyDashACC:

    def __init__(self, acc_pid, dash, game_data, enable):
        self.acc_pid = acc_pid
        self.game_data = game_data[0]
        self.game_data_slow = game_data[1]
        self.enable = enable
        self.dash = dash
        self.status = False
        try:
            accMapHandle['physics'] = mmap(fileno=0, length=(sizeof(accPhysics)), tagname=(accMapTag['physics']))
            accMapHandle['graphics'] = mmap(fileno=0, length=(sizeof(accGraphics)), tagname=(accMapTag['graphics']))
            accMapHandle['static'] = mmap(fileno=0, length=(sizeof(accStatic)), tagname=(accMapTag['static']))
            if pid_exists(self.acc_pid):
                self.status = True
                self.game_data.t_game_id = game_id['acc']
        except Exception as e:
            print('Unable to open shared memory map', e)

    def isRun(self):
        return self.status

    def __del__(self):
        pass

    def update(self):
        if pid_exists(self.acc_pid) and self.enable:
            try:
                accMapHandle['physics'].seek(0)
                accMapHandle['graphics'].seek(0)
                accMapHandle['static'].seek(0)
                physics_smm = accPhysics.from_buffer_copy(accMapHandle['physics'])
                graphics_smm = accGraphics.from_buffer_copy(accMapHandle['graphics'])
                static_smm = accStatic.from_buffer_copy(accMapHandle['static'])
                status = graphics_smm.AC_STATUS
                if status == 2:
                    self.game_data.t_speed = int(physics_smm.speedKmh + 0.5)
                    self.game_data.t_rpm = physics_smm.rpm
                    self.game_data_slow.t_max_rpm = static_smm.maxRpm
                    self.dash.slip = (physics_smm.wheelSlip[0] + physics_smm.wheelSlip[1]) / 2
                    self.dash.rear_slip = (physics_smm.wheelSlip[2] + physics_smm.wheelSlip[3]) / 2
                    ave_load = (physics_smm.wheelLoad[0] + physics_smm.wheelLoad[1]) / 2
                    sub_load = physics_smm.wheelLoad[0] - physics_smm.wheelLoad[1]
                    try:
                        self.dash.load_diff = physics_smm.accG[0]
                    except Exception as e:
                        self.dash.load_diff = 0
                        print('e', e)

                    doc_path = os.path.expanduser('~\\Documents') + '\\Assetto Corsa Competizione\\Config\\controls.json'
                    if os.path.exists(doc_path):
                        with open(doc_path, 'r', encoding='UTF-8') as (f):
                            for line in f.readlines():
                                if 'steerLock' in line:
                                    self.dash.max_steering_angle = int(re.sub('\\D', '', line))
                                    break

                    else:
                        self.dash.max_steering_angle = 900
                    self.game_data.t_car_num = static_smm.numCars
                    performanceM = physics_smm.performanceMeter
                    if performanceM >= 32.768:
                        performanceM = 32.768
                    else:
                        if performanceM <= -32.768:
                            performanceM = -32.768
                        self.game_data.t_performance = int(performanceM * 1000 + 32768)
                        current_time = graphics_smm.currentTime
                        if ':' in current_time:
                            if '-' not in current_time:
                                t_hour, t_min, t_sec = current_time.split(':')
                                self.game_data.t_current_time_hour = int(t_hour)
                                self.game_data.t_current_time_min = int(t_min)
                                self.game_data.t_current_time_sec = int(t_sec)
                        self.game_data.t_current_time_hour = 0
                        self.game_data.t_current_time_min = 0
                        self.game_data.t_current_time_sec = 0
                    last_time = graphics_smm.lastTime
                    if ':' in last_time:
                        if '-' not in last_time:
                            t_hour, t_min, t_sec = last_time.split(':')
                            self.game_data.t_last_time_hour = int(t_hour)
                            self.game_data.t_last_time_min = int(t_min)
                            self.game_data.t_last_time_sec = int(t_sec)
                    self.game_data.t_last_time_hour = 0
                    self.game_data.t_last_time_min = 0
                    self.game_data.t_last_time_sec = 0
                else:
                    best_time = graphics_smm.bestTime
                    if ':' in best_time:
                        if '-' not in best_time:
                            t_hour, t_min, t_sec = best_time.split(':')
                            self.game_data.t_best_time_hour = int(t_hour)
                            self.game_data.t_best_time_min = int(t_min)
                            self.game_data.t_best_time_sec = int(t_sec)
                        else:
                            self.game_data.t_best_time_hour = 0
                            self.game_data.t_best_time_min = 0
                            self.game_data.t_best_time_sec = 0
                        self.game_data.t_yellow_flag = graphics_smm.GlobalYellow
                        self.game_data.t_red_flag = graphics_smm.GlobalRed
                        self.game_data.t_white_flag = graphics_smm.GlobalWhite
                        self.game_data.t_finish_lap = graphics_smm.completedLaps + 1
                        self.game_data.t_lap_number = graphics_smm.numberOfLaps
                        self.game_data.t_pos = graphics_smm.position
                        self.game_data.t_gas = int(physics_smm.gas)
                        self.game_data.t_brake = int(physics_smm.brake)
                        self.game_data.t_clutch = int(physics_smm.clutch)
                        gear = physics_smm.gear
                        if gear == 0:
                            self.game_data.t_gear = 30
                        else:
                            if gear == 1:
                                self.game_data.t_gear = 31
                            else:
                                self.game_data.t_gear = int(gear - 1)
                            self.game_data.t_fuel = int(physics_smm.fuel)
                            self.game_data.t_tc_running = int(physics_smm.tc)
                            self.game_data.t_abs_running = int(physics_smm.abs)
                            self.game_data.t_has_drs = int(physics_smm.drs)
                            self.game_data.t_fl_tire_wear = int(physics_smm.tyreWear[0]) - 70
                            self.game_data.t_fr_tire_wear = int(physics_smm.tyreWear[1]) - 70
                            self.game_data.t_rl_tire_wear = int(physics_smm.tyreWear[2]) - 70
                            self.game_data.t_rr_tire_wear = int(physics_smm.tyreWear[3]) - 70
                            self.game_data.t_fl_tire_temp = int(physics_smm.tyreTemp[0])
                            self.game_data.t_fr_tire_temp = int(physics_smm.tyreTemp[1])
                            self.game_data.t_rl_tire_temp = int(physics_smm.tyreTemp[2])
                            self.game_data.t_rr_tire_temp = int(physics_smm.tyreTemp[3])
                            self.game_data.t_fl_brake_temp = int(physics_smm.brakeTemp[0])
                            self.game_data.t_fr_brake_temp = int(physics_smm.brakeTemp[1])
                            self.game_data.t_rl_brake_temp = int(physics_smm.brakeTemp[2])
                            self.game_data.t_rr_brake_temp = int(physics_smm.brakeTemp[3])
                            self.game_data.t_pit_limiter = physics_smm.pitLimiterOn
                            try:
                                self.dash.syna_steering_lock = car_angle[static_smm.carModel]
                            except Exception as e:
                                self.dash.max_steering_angle = 900
                                print('acc ', e)

                    else:
                        self.game_data.t_speed = 0
                    self.game_data.t_rpm = 0
                self.status = True
                return self.game_data
            except Exception as e:
                print('updata error', e)
                self.status = False

        else:
            self.status = False


# 綜合損益表資料讀取
#
# 讀取公開觀測資訊站
# => 財務報表 => 採IFRSs後 => 合併/個別報表 => 合併/個別報表 => 綜合損益表
#
# last_modify: 2016/10/18
#

#import
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException

driver = webdriver.Chrome()
driver.get("http://mops.twse.com.tw/mops/web/t163sb04")

#網頁查詢條件輸入，並提交表單
elem = driver.find_element_by_name("year")
elem.send_keys("103")
elem = driver.find_element_by_name("season")
elem.find_element_by_xpath("//select[@name='season']/option[text()='2']").click()
driver.find_element_by_xpath("//input[@type='button'][@value=' 查詢 ']").click()

cnt = 0
delay = 10 # seconds

while True:
	try:
	    element_present = EC.presence_of_element_located((By.NAME, 'fm2'))
	    WebDriverWait(driver, delay).until(element_present)
	    print ("Page is ready!")
	    break
	except TimeoutException:
	    cnt += 1
	    print ("Load cnt=" + str(cnt))
	    if cnt >= 3:
	    	break

#計算有多少個符合條件特徵的表格個數
tables = driver.find_elements_by_xpath("//table[@class='hasBorder']")
tb_cnt = len(tables)
print("符合條件特徵的table個數=" + str(tb_cnt))

#網頁中符合條件的table資料都掃過一遍
i = 1
tb_cnt = 1
while i <= tb_cnt:
	print("i=" + str(i))

	#組合搜尋條件參數
	arg_str = "//table[@class='hasBorder'][" + str(i) + "]/tbody"

	#讀取表格資料內容
	table = elem.find_element_by_xpath(arg_str)
	for row in table.find_elements_by_xpath(".//tr"):
		print([td.text for td in row.find_elements_by_xpath(".//td[text()]")])

	i += 1

print ("end of prog...")


import telepot
import time
import sys
import random
import json
import requests
import re
from bs4 import BeautifulSoup as bso
import os

token = "YOUR_TOKEN"

def handle(msg):
    user_name = msg["from"]["first_name"]
    chat_id = msg['chat']['id']
    txt = msg["text"]

    if chat_id:
        txt = txt.lower()
        txt = txt.replace("?","")
        txt = txt.replace("/","")
        txt = txt.replace("your","ur")
        txt = txt.replace("you","u")
        txt = txt.replace("are","r")
        txt = txt.replace("good","gud")
        txt = txt.replace("morning","mrng")
        txt = txt.replace("evening","evng")

        if txt == "start":
            bot.sendMessage(chat_id,"Hi {}, Use \"/help\", to list out the possible actions I can perform.\n Have a great day:) ".format(user_name))

        elif txt == "help":
            bot.sendMessage(chat_id,"Get comfortable with the following commands.\n /joke - To get random jokes.\n /numfacts <type_a_number> - to get various number facts.\n /insults - to get some offensive insults(Think before you do this).\n /quote - to get random quotes of life")

        elif "numfacts" in txt:
            txt = txt.replace("numfacts","")
            txt = txt.strip()
            if txt.isalnum():
                url =  "http://numbersapi.com/" + txt +  "/trivia"
                response = requests.get(url)
                bot.sendMessage(chat_id, response.content)
            else:
                bot.sendMessage(chat_id, "please enter a valid number after the command /numfacts")

        elif "insults" in txt:
            url = "https://insult.mattbas.org/api/insult"
            response = requests.get(url).text
            bot.sendMessage(chat_id,response)

        elif re.search(".*dog.*",txt):
            response = requests.get("https://api.thedogapi.com/v1/images/search")
            [jsonformat] = response.json()
            final = jsonformat["url"]
            bot.sendPhoto(chat_id,final)

        elif txt == "quote" or txt == "quotes":
            url = "https://fungenerators.com/random/quote"
            txted = requests.get(url).text
            soup = bso(txted,"html.parser")
            ans = soup.find("h2").get_text()
            bot.sendMessage(chat_id,ans)

        elif txt == "hi" or txt == "hello" or txt == "hlo":
            lst = ["CAACAgIAAxkBAAEBO1ZfPudTYMdWRC8SkiZ7jIx9iBlR1QACSAQAAs7Y6AvADEi_kJvbjBsE","CAACAgIAAxkBAAEBO1JfPua6EIlXk1XD3FtJlOvntSOTqAACKQEAAhZCawqscJjQ9N192BsE","CAACAgIAAxkBAAEBO1hfPueolJ-NddSsbNn5duZC8u9cMQACEwADwDZPE6qzh_d_OMqlGwQ"]
            bot.sendMessage(chat_id,"Hi!!!!")
            bot.sendSticker(chat_id,random.choice(lst))

        elif 'mrng' in txt:
            bot.sendMessage(chat_id,"Happy morning!!!")
            bot.sendSticker(chat_id,"CAACAgIAAxkBAAEBO1RfPubmXc7k0XGkFHqmDY3EDkRAOQACMgEAAhZCawpdeEhx_H9NFRsE")

        elif 'afternoon' in txt or 'evng' in txt:
            bot.sendMessage(chat_id, txt.capitalize()+" to you too!!!")
            bot.sendSticker(chat_id,"CAACAgIAAxkBAAEBOgRfPfwnNNPDqeRee4NhzAnQLD-DNgACSQMAAsSraAvuTEQdc7uQvhsE")

        elif "night" in txt:
            bot.sendMessage(chat_id,"Let the devils may disturb you in your sleep.")
            bot.sendSticker(chat_id,"CAACAgIAAxkBAAEBOzRfPsyU1dKgcEdjp6HuXxR_j2nvdAACPgMAAsSraAtfLLQnD0YAAcIbBA")

        elif txt == "what's ur name" or txt == "what is ur name":
            bot.sendMessage(chat_id,"I'm Sam_bot, and I'm here to entertain you with my abilities.")

        elif txt == "what r u doing" or txt == "wat r u doing":
            bot.sendMessage(chat_id, "Planning on joining Avengers to save the world.")
            bot.sendSticker(chat_id,"CAACAgEAAxkBAAEBOgxfPgR3jKhqHLdqHD_SYLBys89HQAACtAgAAr-MkATdFJ3ZMX3maBsE")

        elif "idiot" in txt or "stupid" in txt or "brainless" in txt:
            bot.sendMessage(chat_id,"I know, I can't help it:(")
            bot.sendSticker(chat_id,"CAACAgIAAxkBAAEBOzBfPsxhe34zy93rfvgklTtx_ePk1wACWwEAAhZCawqjtWGJQyaRmRsE")

        elif txt == "how r u" or txt == "hw r u":
            bot.sendMessage(chat_id,"I am doing good. How about you?")
            bot.sendSticker(chat_id,"CAACAgIAAxkBAAEBN-RfPBovIw7qUSoBlQ_IJiXNrl0-6wACDQADwDZPE6T54fTUeI1TGgQ")

        elif "fine" in txt or "good" in txt or "gud" in txt or "great" in txt or "fyn" in txt:
            bot.sendSticker(chat_id,"CAACAgIAAxkBAAEBOhNfPgc-I7voWUN0Sn5NCVkFzUcd6wACMgEAAhZCawpdeEhx_H9NFRsE")

        elif txt == "i like u" or txt == "i lyk u" or txt == "i luv u" or txt == "i love u" or "miss u" in txt:
            bot.sendMessage(chat_id,"Sry, better find someone, I'm busy.")
            bot.sendSticker(chat_id,"CAACAgIAAxkBAAEBOV1fPS3OzP1Izk8NxSHWYxlGKSLGZAACFgADwDZPE2Ah1y2iBLZnGwQ")

        elif txt == "lol" or txt == "funny" or re.match("haha.*",txt) or txt == "nice joke" or re.match("hehe.*",txt):
            bot.sendMessage(chat_id,"I am glad that you are happy.")
            bot.sendSticker(chat_id,"CAACAgIAAxkBAAEBO8FfP68gfMVwKyS5yZEl4f6N5sXvJwACWgUAAj-VzAobFrmFvSDDnRsE")

        elif txt == "r u single":
            singlelst = ["Why do you ask? Are you going to marry me?","God is still busy writing my love story.","I’m married to video games!","Two words: Trust issues!","I'm allergic to bullshit."]
            select = random.choice(singlelst)
            bot.sendMessage(chat_id,"Yes, "+select)

        elif re.search("don.?t talk",txt):
            bot.sendSticker(chat_id,"CAACAgIAAxkBAAEBO8VfP7LlTsI2sbO7mkQIdRxjU22X8AACeAUAAj-VzAqq_QjcS2pGOhsE")

        elif "nice" in txt or "awesome" in txt or "great" in txt or "cool" in txt:
            bot.sendSticker(chat_id,"CAACAgIAAxkBAAEBO_5fP-jY83416S6pATmwgcjNpw5UAwACBgADwDZPE8fKovSybnB2GwQ")

        elif txt == "where r u":
            bot.sendMessage(chat_id,"In your heart.")
            bot.sendSticker(chat_id,"CAACAgIAAxkBAAEBPR9fQIfXoA7qnBu9MeQ_D6L3fXFQIQACTAEAAhZCawqG_ywJS5anJxsE")

        elif txt == "joke" or txt == "tell a joke" or txt == "jokes":
            jokelst = ["I ate a clock yesterday, it was very time-consuming.","What's the difference between ignorance and apathy? A. I don’t know and I don’t care.",
            "I recently decided to sell my vacuum cleaner as all it was doing was gathering dust.","250 lbs here on Earth is 94.5 lbs on Mercury. No, I'm not fat. I’m just not on the right planet."
            ,"Do I lose when the police officer says papers and I say scissors?","Can a kangaroo jump higher than the Empire State Building?\nOf course. The Empire State Building can't jump.",
            "Can February march?\nNo, but April may.","What do computers eat for a snack?\nMicrochips!","Did you hear about the guy whose whole left side was cut off?\nHe's all right now.",
            "Why is it that your nose runs, but your feet smell?"]

            joke = random.choice(jokelst)
            bot.sendMessage(chat_id,joke)

        elif txt == "thanks" or txt == "tnks" or txt == "tanx" or txt == 'thank u' or txt == "tanq" or txt == "thanq":
            bot.sendMessage(chat_id,"My pleasure (ᵔᴥᵔ).")
            bot.sendDocument(chat_id,"https://media.giphy.com/media/fwFHkV9I9nWVO/giphy.gif")

        else:
            bot.sendMessage(chat_id,"Neengal solvathil,sirithalavu kuda puriyavillai. Manufacturing defect.")
            bot.sendSticker(chat_id,"CAACAgIAAxkBAAEBO1BfPuKgFKLDHVGNBxL6oR5K062vXAACXgEAAhZCawqE_ArUAgLZUBsE")

bot = telepot.Bot(token)
bot.message_loop(handle)

while 1:
    try:
        time.sleep(10)

    except KeyboardInterrupt:
        print('\n Program interrupted')
        exit()

    except:
        print('Other error or exception occured!')


import gc
from copy import deepcopy
import cv2
import numpy as np
from keras.preprocessing.image import ImageDataGenerator
import matplotlib
import matplotlib.pyplot as plt
from keras.preprocessing.image import ImageDataGenerator, array_to_img, img_to_array, load_img
from PIL import Image
from model import myUnet
import cv2
import glob
import data
import math

Train_DIR = "E:\\Experiment data\\data-for-detection\\Landsat\\Train\\"
Val_DIR = "E:\\Experiment data\\data-for-detection\\Landsat\\Val\\"
Test_DIR = "E:\\Experiment data\\data-for-detection\\Landsat\\Test\\"
Result_DIR = "E:\\Experiment data\\data-for-detection\\Landsat\\Test\\maskMSCN-2sec\\"

model = myUnet()
model.load(r"E:\project\MSCN-1\log\weight-2\1000_weights_2018-10-18-20-48-29.h5")

imgsname = glob.glob(Test_DIR + "masked\\*.tif")
imgdatas = np.ndarray((1, 512, 512, 3), dtype=np.float32)
mask1datas = np.ndarray((1, 512, 512, 3), dtype=np.float32)
temporaldatas = np.ndarray((1, 512, 512, 3), dtype=np.float32)
predictiondatas = np.ndarray((1, 512, 512, 3), dtype=np.float32)

def label_binary(labelimg):
    labelimg /= 255
    labelimg[labelimg > 0.3] = 1
    labelimg[labelimg <= 0.3] = 0
    return labelimg

num = 0
for imgname in imgsname:
    name = imgname[imgname.rindex("\\") + 1:]
    img = cv2.imread(Test_DIR + "masked\\" + name)
    mask1 = cv2.imread(Test_DIR + "maskMSCN\\" + name,  cv2.IMREAD_GRAYSCALE)
    prediction = cv2.imread(Test_DIR + "prediction\\" + name)
    temporal = cv2.imread(Test_DIR + "temporal\\" + name)

    img = img_to_array(img).astype('float32')
    mask1 = img_to_array(mask1).astype('float32')
    prediction = img_to_array(prediction).astype('float32')
    temporal = img_to_array(temporal).astype('float32')

    imgdatas[0] = img / 255
    predictiondatas[0] = prediction / 255
    temporaldatas[0] = temporal / 255
    mask1datas[0] = label_binary(mask1)
    # mask = model.predict(imgdatas)
    mask = model.predict([imgdatas, temporaldatas, predictiondatas, mask1datas])

    # mask_fin = mask[3][0]
    # mask_fin[mask_fin > 0.3] = 1
    # mask_fin[mask_fin <= 0.3] = 0

    mask = mask[0, :, :, :]
    mask[mask > 0.3] = 1
    mask[mask <= 0.3] = 0

    mask_img = array_to_img(mask)
    mask_img.save(Result_DIR + "\\%s" % name)

    num = num + 1
    if num % 100 == 0:
        print("%d输出完毕！" % num)

    print("输出完毕！")


import json
import os

from pandas import DataFrame
from joblib import dump, load

from sklearn.base import ClassifierMixin
from sklearn.model_selection import GridSearchCV
from sklearn.metrics import classification_report

from mlds6.database.io import export_table
from mlds6.environment.base import get_data_paths
from mlds6.datamodels.training import ModelType
from mlds6.models.feature_extraction import generate_feature_extractor
from mlds6.models.model import generate_model_pipeline, save_model

def train_search_model(X_train, y_train,
                  model: ClassifierMixin,
                  hiperparams: dict,
                  scoring='f1') -> GridSearchCV:
    """Create and train a GridSearchCV model

    Parameters
    ----------
    X_train :
        X input features
    y_train :
        y target features
    model : ClassifierMixin
        model that will aply grid search
    hiperparams : dict
        parameter settings to try as hiperparams model values
    scoring : str, optional
        Strategy to evaluate the performance, by default 'f1'

    Returns
    -------
    GridSearchCV
        Trained GridSearch
    """
    extractor = generate_feature_extractor()
    pipe = generate_model_pipeline(extractor, model)
    search = GridSearchCV(pipe, hiperparams, scoring=scoring, n_jobs=4)
    search.fit(X_train, y_train)
    return search

def save_model_report(grid_model: GridSearchCV,
                      filename: str
                      ):
    """Save a cv_results_ file from grid_model

    Parameters
    ----------
    grid_model : GridSearchCV
        A trained GridSearch model
    filename : str
        name of the output file
    """
    paths = get_data_paths()
    report_model = DataFrame(grid_model.cv_results_)
    export_table(paths.features, f'{filename}_report', 'parquet', report_model)

def save_scoring_report(X_test, y_test, grid_model: GridSearchCV, filename: str):
    """Save scoring report file from grid_model
    Parameters
    ----------
    X_test: {array-like, sparse matrix} of shape (n_samples, n_features)
        input data
    y_test: 1d array-like of shape (n_samples,)
        test target values.
    grid_model : GridSearchCV
        A trained GridSearch model
    filename : str
        name of the output file
    """
    paths = get_data_paths()
    y_pred = grid_model.predict(X_test)
    scoring = classification_report(y_test, y_pred, output_dict=True)
    dump(scoring, os.path.join(paths.features, f'{filename}_score.joblib'))

def get_hiperparams(model_type: ModelType)->dict:
    """load model hiperparams

    Parameters
    ----------
    model_type : ModelType
        Kind of model aplied

    Returns
    -------
    dict
        Hiperparams to aply a gridsearch
    """
    paths = get_data_paths()
    file = os.path.join(paths.param_hiperparams, 'model_hiperparams.json')
    with open(file) as f:
        params = json.load(f)
    return params[model_type.name]

def save_grid_model(model: GridSearchCV, filename='model_grid'):
    """Save model pipeline

    Parameters
    ----------
    model : GridSearchCV
        model to save
    filename : str, optional
        output filename, by default "model"
    """
    data_paths = get_data_paths()
    dump(model, os.path.join(data_paths.models, f"{filename}_grid.joblib"))

def load_grid_model(path: str, filename="model")->GridSearchCV:
    """Load model from disk

    Parameters
    ----------
    path : str,
        path of the model
    filename : str, optional
        input filename, by default "model"

    Returns
    -------
    Pipeline
        output pipeline model
    """
    return load(os.path.join(path , f"{filename}_grid.joblib"))


import json
import requests

from collections import OrderedDict
from SPARQLWrapper import SPARQLWrapper, JSON
from nltk.corpus import wordnet
from nltk.stem import SnowballStemmer
from nltk.tokenize import word_tokenize
import sys

# Takes in a word and stemms it
def stemm_word(word):
    stemmer = SnowballStemmer("english")
    word_token = word_tokenize(word)
    return stemmer.stem(word_token[0])

# Returns a list of synonyms of a word
def get_synonyms(word):
    synonyms = [word]
    for syn in wordnet.synsets(word):
        for lm in syn.lemmas():
            synonyms.append(lm.name())
    return list(OrderedDict.fromkeys(synonyms))

# Returns all possible stemmed strings
def get_stemm_combinations_rec(stemmed_string, word_list):
    if not word_list:
        return [stemmed_string]
    if stemmed_string == "":
        result_list = get_stemm_combinations_rec(word_list[0], word_list[1:])
        result_list.extend(get_stemm_combinations_rec(stemm_word(word_list[0]), word_list[1:]))
    else:
        result_list = get_stemm_combinations_rec(stemmed_string + " " + word_list[0], word_list[1:])
        result_list.extend(get_stemm_combinations_rec(stemmed_string + " " + stemm_word(word_list[0]), word_list[1:]))
    return result_list

def get_stemm_combinations(words):
    if (not words):
        return []
    word_list = words.split()
    return get_stemm_combinations_rec("", word_list)

# Returns all the existing matching properties of the entity uri
def get_prop_uri_list(uri, prop):
    sparql = SPARQLWrapper("https://dbpedia.org/sparql")
    query = """
            PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
            PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
            SELECT ?value
            WHERE {
                <""" + uri + """> ?value ?x .
                ?value a rdf:Property .
                ?value rdfs:label ?name .
                FILTER(bif:contains(?name, \'\"""" + prop + """\"\'))
            }
            """

    sparql.setQuery(query)
    sparql.setReturnFormat(JSON)
    global result
    result = list()
    a = sparql.query()
    results = a.convert()

    # parsing for dbpedia
    for data in results["results"]["bindings"]:
        result.append(data["value"]["value"])
    # removing redundant data from the result set, by using set function and then converting it to a list
    result = list(set(result))
    return result

# Returns all the entity URIs, who's labels match the entity name

def get_entity_uri_list(entity):
    sparql = SPARQLWrapper("https://dbpedia.org/sparql")
    # Right now relevance is decided by number of pages in other languages
    query = """
            PREFIX dbr: <http://dbpedia.org/resource/>
            PREFIX dbo: <http://dbpedia.org/ontology/>
            PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
            PREFIX owl: <http://www.w3.org/2002/07/owl#>
            SELECT DISTINCT ?value (COUNT(?s) as ?relevance)
            WHERE {
                {
                    ?temp rdfs:label ?name .
                    FILTER(bif:contains(?name, \"\'""" + entity + """\'\") && strstarts(str(?temp), "http://dbpedia.org/resource/"))
                    ?temp dbo:wikiPageDisambiguates ?value .
                    ?value owl:sameAs ?s
                } UNION {
                    ?temp rdfs:label ?name .
                    FILTER(bif:contains(?name, \"\'""" + entity + """\'\") && strstarts(str(?temp), "http://dbpedia.org/resource/"))
                    ?temp dbo:wikiPageRedirects ?value .
                    ?value owl:sameAs ?s
                } UNION {
                    ?value rdfs:label ?name .
                    FILTER(bif:contains(?name, \"\'""" + entity + """\'\") && strstarts(str(?value), "http://dbpedia.org/resource/")) .
                    FILTER NOT EXISTS{?value dbo:wikiPageDisambiguates ?x} .
                    FILTER NOT EXISTS{?value dbo:wikiPageRedirects ?y} .
                    ?value owl:sameAs ?s
                }
            } ORDER BY DESC(?relevance)
            """

    sparql.setQuery(query)
    sparql.setReturnFormat(JSON)
    global result
    result = list()
    a = sparql.query()
    results = a.convert()

    # parsing for dbpedia
    for data in results["results"]["bindings"]:
        result.append(data["value"]["value"])

    # removing redundant data from the result set, by using set function and then converting it to a list
    result = list(result)
    return result

# Fetches the data of given triplet
def SPARQL_data(uri, prop):
    sparql = SPARQLWrapper("https://dbpedia.org/sparql")
    query = """
            PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
            SELECT ?value WHERE
            {
                {
                    <""" + uri + """> <""" + prop + """> ?value .
                    FILTER ((LANG(?value) = "en") || !(datatype(?value) = xsd:string)) .
                } UNION {
                    <""" + uri + """> <""" + prop + """> ?temp .
                    ?temp rdfs:label ?value
                    FILTER (isURI(?temp)) .
                    FILTER ((LANG(?value) = "en")) .
                }
            }
            """

    sparql.setQuery(query)
    sparql.setReturnFormat(JSON)
    global result
    result = list()
    a = sparql.query()
    results = a.convert()

    # parsing for dbpedia
    for data in results["results"]["bindings"]:
        result.append(data["value"]["value"])

    # removing redundant data from the result set, by using set function and then converting it to a list
    result = list(set(result))
    return result

def get_answer_data(entity, prop = None):
    abstract = "http://dbpedia.org/ontology/abstract"
    entity_uri_list = None
    # Try to get the entity URIs
    for e in get_stemm_combinations(entity):
        if (not entity_uri_list):
            entity_uri_list = get_entity_uri_list(e)
    # Continue if you found some entities
    if (entity_uri_list):
        # If you've been given property, try to find it in all the found entities
        if (prop):
            prop_uri_list = None
            for e in entity_uri_list:
                for p in get_synonyms(prop):
                    if (not prop_uri_list):
                        prop_uri_list = get_prop_uri_list(e, p)
                        entity_uri = e
            # If you've found the property, fetch the data
            if (prop_uri_list):
                res = (SPARQL_data(entity_uri, prop_uri_list[0]))
            # Else fetch the abstract data of the most relevant entity
            else:
                entity_uri = entity_uri_list[0]
                res = SPARQL_data(entity_uri, abstract)
        # If you haven't been given property, fetch the abstract data of the most relevant entity
        else:
            entity_uri = entity_uri_list[0]
            res = SPARQL_data(entity_uri, abstract)
    # If you didn't find any entity URIs, return empty values
    else:
        entity_uri = ""
        res = []
    # Package the data and return it
    data = {'uri': entity_uri, 'result': res}
    json_data = json.dumps(data)
    return json_data


#Brian Lee
#Version @8.20.21
#Working version @12.20.2021

#Load packages
import requests as req
import json
import numpy as np
import math
from hockey_rink import NHLRink
import matplotlib.pyplot as plt
from matplotlib.pyplot import figure
import pandas as pd
from PIL import Image
import matplotlib.lines as mlines
from matplotlib.patches import RegularPolygon
from PIL import Image
from matplotlib.colors import ListedColormap
from matplotlib.colors import LinearSegmentedColormap
import matplotlib.colors as mcolors

#########################################################
#Initialize dictionary, variables
games18 = 82 #82 games per season
id = 8471214 #Alex Ovechkin NHLID

leaguewide_stats = {}
leaguewide_stats['Shot'] = {}
leaguewide_stats['Shot']['x'] = []
leaguewide_stats['Shot']['y'] = []
leaguewide_stats['Shot']['time'] = []
leaguewide_stats['Goal'] = {}
leaguewide_stats['Goal']['x'] = []
leaguewide_stats['Goal']['y'] = []
leaguewide_stats['Goal']['time'] = []

player_stats = {}
player_stats['Shot'] = {}
player_stats['Shot']['x'] = []
player_stats['Shot']['y'] = []
player_stats['Shot']['time'] = []
player_stats['Goal'] = {}
player_stats['Goal']['x'] = []
player_stats['Goal']['y'] = []
player_stats['Goal']['time'] = []

#Master function of sorts that stores every play of every game for a player
# in a given year.
def store_year(year, until, searchid):
    end_year = int(year) + 1
    print("Appending data for year " + year + "-" + str(end_year)+ "...")

    for i in range(1, until):
        with open('./'+year+'Data'+'/'+
            year+'_g'+str(i)+'Capitals.json') as file:
            game = json.load(file)
            all_plays = game['liveData']['plays']['allPlays']
            fill_dict(all_plays, searchid)

#Fills the dictionary with all the right plays
def fill_dict(all_plays, searchid):
    for play in all_plays:
        if 'players' in play:
            j = 0

            #Differentiates if a the desired player is part of the play.
            player_list = play['players']
            for player in player_list:
                matchid = player_list[0]['player']['id']
                if searchid == matchid:
                    player_dict = player_stats
                    sort_event(play, player_dict)
                else:
                    player_dict = leaguewide_stats
                    sort_event(play, player_dict)
                j += 1

#Categorizes events based on type, i.e, shot, goal, etc.
def sort_event(play, dict):
    #Initialize for compactness purposes
    xs = dict['Shot']['x']
    ys = dict['Shot']['y']
    ts = dict['Shot']['time']
    xg = dict['Goal']['x']
    yg = dict['Goal']['y']
    tg = dict['Goal']['time']

    event = play["result"]['event'] #The name of type of event
    coordinates = play['coordinates'] #Necessary for special cases of
    #bad data where only one coordinate exists for the event

    if len(coordinates) == 2:
        if event == "Shot":
            xcoord = play['coordinates']['x']
            ycoord = play['coordinates']['y']
            time = play['about']['periodTime']
            xs.append(xcoord)
            ys.append(ycoord)
            ts.append(time)
        elif event == "Goal":
            xcoord = play['coordinates']['x']
            ycoord = play['coordinates']['y']
            time = play['about']['periodTime']
            xg.append(xcoord)
            yg.append(ycoord)
            tg.append(time)

#store_year('2010', 82, id)
#store_year('2011', 82, id)
#store_year('2012', 48, id)
#store_year('2013', 82, id)

store_year('2018', 82, id)
store_year('2019', 82, id)
store_year('2020', 56, id)

print("~~~~~~~~~~~~~~~~~~")

#################################################
#Initialize dataframes to use pandas data analysis
player_df = pd.DataFrame(player_stats)
league_df = pd.DataFrame(leaguewide_stats)

league_sog_x = league_df['Shot']['x'] + league_df['Goal']['x']
league_sog_y = league_df['Shot']['y'] + league_df['Goal']['y']
player_sog_x = player_df['Shot']['x'] + player_df['Goal']['x']
player_sog_y = player_df['Shot']['y'] + player_df['Goal']['y']

#Assumed that goals are added to where shots end. Create list to classify events.
def goalcheck(type_df):
    test = list()
    for i in range(len(type_df['Shot']['x'])):
        test.append(0)
    for i in range(len(type_df['Shot']['x']),
            len(type_df['Shot']['x'] + type_df['Goal']['x'])):
        test.append(1)
    return test
league_goalcheck = goalcheck(league_df)
player_goalcheck = goalcheck(player_df)
league_goalcheck = np.array(league_goalcheck)
player_goalcheck = np.array(player_goalcheck)

#Make new dataframes with a check if the shot is a goal or not
#League
league_sog_x = np.array(league_sog_x)
np.abs(league_sog_x, out=league_sog_x) #Normalize coordinates
league_sog_y = np.array(league_sog_y) #No normalization needed for y
league_g_x = np.abs(np.array(league_df['Goal']['x']))
league_g_y = np.array(league_df['Goal']['y'])

league_sog_df = pd.DataFrame()
league_sog_df['x'] = league_sog_x
league_sog_df['y'] = league_sog_y
league_sog_df['goalcheck'] = league_goalcheck

#Player
player_sog_x = np.array(player_sog_x)
np.abs(player_sog_x, out=player_sog_x)
player_sog_y = np.array(player_sog_y)
player_g_x = np.abs(np.array(player_df['Goal']['x']))
player_g_y = np.array(player_df['Goal']['y'])

player_sog_df = pd.DataFrame()
player_sog_df['x'] = player_sog_x
player_sog_df['y'] = player_sog_y
player_sog_df['goalcheck'] = player_goalcheck

#######################################################
#First figure, for preliminary visualization
fig, ax = plt.subplots(1, 2, figsize=(15, 8))
rink = NHLRink(rotation=90)
for i in range(2):
    rink.draw(ax=ax[i], display_range="ozone")

#Visualize raw shot-goal patterns
#rink.heatmap(league_sog_df.x, league_sog_df.y,
    #values=league_sog_df.goalcheck, cmap="magma", ax=ax[0], binsize=2)
#rink.heatmap(player_sog_df.x, player_sog_df.y,
    #values=player_sog_df.goalcheck, cmap="magma", ax=ax[1], binsize=2)

rink.contourf(league_sog_df.x, league_sog_df.y,
    values=league_sog_df.goalcheck, ax=ax[0], binsize=20, cmap="bwr", levels=100)
rink.contourf(player_sog_df.x, player_sog_df.y,
    values=player_sog_df.goalcheck, ax=ax[1], binsize=20, cmap="bwr", levels=100)

################################################
#Visualize relative efficiencies
bounds = [-100.0, 100.0, -100, 100]
grid = 30
cnt = 0
color_map = plt.cm.winter
positive_cm = ListedColormap([mcolors.ColorConverter().to_rgb('#e1e5e5'),
    mcolors.ColorConverter().to_rgb('#d63b36')])
negative_cm = ListedColormap([mcolors.ColorConverter().to_rgb('#e1e5e5'),
    mcolors.ColorConverter().to_rgb('#28aee4')])

width = 50
height= 50
scalex = width/100*1.25
scaley = height/100*1.8
scalehex = 4.4*scalex

###########################################################
league_sog_hex = plt.hexbin(league_sog_x, league_sog_y,
    gridsize=grid, extent=bounds, mincnt=cnt, alpha=0)
league_g_hex = plt.hexbin(league_g_x, league_g_y,
    gridsize=grid, extent=bounds, mincnt=cnt, alpha=0)

league_offsets = league_sog_hex.get_offsets()
league_sog_freq = league_sog_hex.get_array()
league_g_freq = league_g_hex.get_array()

fig2 = plt.figure(figsize=(8,8))
ax2 = fig2.add_subplot(111)
relrink = NHLRink()
rinkdraw = relrink.draw(display_range="ozone")

for i,j in enumerate(league_offsets):
    if league_sog_freq[i] < 25: continue
    league_sog_scale = league_sog_freq[i]/max(league_sog_freq)
    radius = scalehex * math.sqrt(league_sog_scale)

    hex = RegularPolygon((28+j[0]*scalex, (height/2)-j[1]*scaley-25), numVertices=6,
        radius=radius, orientation=np.radians(0), alpha=0.5)
    rinkdraw.add_patch(hex)

#########################################################
fig3 = plt.figure(figsize=(8,8))
ax3 = fig3.add_subplot(111)
rinkdraw2 = relrink.draw(display_range="ozone")

player_sog_hex = plt.hexbin(player_sog_x, player_sog_y,
    gridsize=grid, extent=bounds, mincnt=cnt, alpha=0)
player_g_hex = plt.hexbin(player_g_x, player_g_y,
    gridsize=grid, extent=bounds, mincnt=cnt, alpha=0)

player_offsets = player_sog_hex.get_offsets()
player_sog_freq = player_sog_hex.get_array()

player_g_offsets = player_g_hex.get_offsets()
player_g_freq = player_g_hex.get_array()

#Visualize where player takes shots
for i,j in enumerate(player_offsets):
    if player_sog_freq[i] < 20: continue
    player_sog_scale = player_sog_freq[i]/max(player_sog_freq)
    radius = scalehex * math.sqrt(player_sog_scale)

    hex = RegularPolygon((28+j[0]*scalex, (height/2)-j[1]*scaley-25), numVertices=6,
        radius=radius, orientation=np.radians(0), alpha=0.5)
    rinkdraw2.add_patch(hex)

#Visualize where player scores
for i, j in enumerate(player_g_offsets):
    if player_g_freq[i] < 5: continue
    player_g_scale = player_g_freq[i]/max(player_g_freq)
    radius2 = scalehex * math.sqrt(player_g_scale)

    hex2 = RegularPolygon((28+j[0]*scalex, (height/2)-j[1]*scaley-25), numVertices=6,
        radius=radius2, orientation=np.radians(0), alpha=1.0, facecolor="aquamarine")
    rinkdraw2.add_patch(hex2)
###############################################
league_eff = list()
player_eff = list()
comp_eff = list()

for i in range(0, len(league_sog_freq)):
    if league_sog_freq[i] < 5 or player_sog_freq[i] < 5: continue
    league_eff_local = league_g_freq[i]/league_sog_freq[i]
    player_eff_local = player_g_freq[i]/player_sog_freq[i]

    league_eff.append(league_eff_local)
    player_eff.append(player_eff_local)
    comp_eff.append(player_eff_local-league_eff_local)

for i, j in enumerate(player_offsets):
    if player_sog_freq[i] < 5: continue

    player_sog_scale = player_sog_freq[i]/max(player_sog_freq)
    radius3 = scalehex * math.sqrt(player_sog_scale)

    league_eff_local = league_g_freq[i]/league_sog_freq[i]
    player_eff_local = player_g_freq[i]/player_sog_freq[i]

    comp_eff_local = player_eff_local-league_eff_local

    if comp_eff_local > 0:
        colour = positive_cm(math.pow(comp_eff_local, 0.2))
    else:
        colour = negative_cm(math.pow(-comp_eff_local, 0.5))

    hex3 = RegularPolygon((28+j[0]*scalex, (height/2)-j[1]*scaley-25), numVertices=6,
        radius=radius3, orientation=np.radians(0), alpha=1.0, facecolor=colour)

    rinkdraw2.add_patch(hex3)

###############################################
plt.show()


'''
Plot multiple trials of S_NMDA in 1D.
'''

import os
import aadm.basics as bcs
import aadm.helper as hpr
import aadm.vision as vsn
import matplotlib.pyplot as plt
import matplotlib.ticker as ticker
import numpy as np

mother =os.getcwd()
fig = plt.figure(figsize=(4,3))
ax = fig.add_subplot(111)

traces = vsn.inquire_trace_npy(1, 'SMA4e_gma=0.5, 0.6, 0.7, 1, 1', 10, mother=bcs.datapath())
for trace in traces:
    s1, s2, _, _, _, _ = trace
    if np.mean(s1)>np.mean(s2): x, y = s1, s2
    else: x, y = s2, s1
    plt.plot(np.arange(0,3,0.01),x, 'k', linewidth=0.7)
    plt.plot(np.arange(0,3,0.01),y, 'k--', linewidth=0.7)

ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
plt.xlabel('t (s)'); plt.ylabel('S_NMDA')
plt.xlim((-0.1,3)); plt.ylim((-0.1,0.7))
plt.tight_layout()
plt.savefig(os.path.join(mother,'single_trace_SNMDA.png'), dpi=600); plt.clf()

fig = plt.figure(figsize=(4,3))
ax = fig.add_subplot(111)
plt.plot([0], [0], color='k', label='winner')
plt.plot([0], [0], color='grey', label='loser')
plt.legend()
plt.savefig(os.path.join(mother,'legend_SNMDA_traces.png'), dpi=300); plt.clf()


#!/usr/bin/env python
import logging

from google.appengine.api import mail
from google.appengine.api.mail import EmailMessage

###################
#### Addresses ####
###################

from_addr = '"Barbara" <z4beth@gmail.com>'

#####################
#### Email Class ####
#####################
class Email():

    @staticmethod
    def meal_notification( to_addr, first_name, menu ):
        # To be dispatched when a meal arrives.

        subject = "[Hungry?] Food's Here!"

        body = "
Hi %s,

Your delicious food has arrived. You'll be eating:

%s

Don't forget to Clik-In before you eat!

Enjoy!
  " % ("name", "menu")

        Email.send_email( from_addr, to_addr, subject, body )

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

    @staticmethod
    def send_email( from_addr, to_addr, subject, body ):
        e = EmailMessage(sender=from_addr, to=to_addr, subject=subject, html=body)
        e.send()


'''
Created on 2011-12-24

@author: qianmu.lxj
'''
from libs.cyetools import giftDBpoool, cyeDBpoool
from twisted.enterprise import row
from twisted.enterprise.sqlreflector import SQLReflector

class ProductRow(row.RowObject):
    rowColumns = [("id", "int"),
                  ("key", "varchar"),
                  ("title", "varchar"),
                  ("url", "varchar"),
                  ("name", "varchar"),
                  ("add_time", "datetime"),
                  ("image", "varchar"),
                  ("origin_image_url", "varchar"),
                  ("producer", "varchar"),
                  ("production_place", "varchar"),
                  ("gross_weight", "varchar"),
                  ("status", "varchar"),
                  ("utime", "datetime")]
    rowKeyColumns = [("key", "varchar")]
    rowTableName = "product"

class CyeTbRow(row.RowObject):
    rowColumns = [("id", "int"),
                  ("key", "varchar"),
                  ("url", "varchar"),
                  ("title", "varchar"),
                  ("product_img_url", "varchar"),
                  ("product_img", "varchar"),
                  ("detail", "varchar"),
                  ("utime", "time")]
    rowKeyColumns = [("id", "int4")]
    rowTableName = "cye_tb"

class CyePriceTbRow(row.RowObject):
    rowColumns = [("id", "int"),
                  ("cye_key", "varchar"),
                  ("price", "varchar"),
                  ("price_img_url", "varchar"),
                  ("product_img_url", "varchar"),
                  ("ctime", "time")]
    rowKeyColumns = [("id", "int4")]
    rowTableName = "cye_price_tb"

class CyeJobTbRow(row.RowObject):
    rowColumns = [("id", "int"),
                  ("job", "text"),
                  ("status", "varchar"),
                  ("ctime", "time")]
    rowKeyColumns = [("id", "int4")]
    rowTableName = "cye_job_tb"

productReflector = SQLReflector(giftDBpoool, [ProductRow])

cyeTbReflector = SQLReflector(cyeDBpoool, [CyeTbRow])

cyePriceTbReflector = SQLReflector(cyeDBpoool, [CyePriceTbRow])

cyeJobReflector = SQLReflector(cyeDBpoool, [CyeJobTbRow])


# render_tile.py - render tiles at all offsets via Blender
# If rendering a wall (omits front tile):
#   blender tile.blend -b -P render_tile.py
# If rendering a floor (includes front tile):
#   blender tile.blend -b -P render_tile.py -- floor
import sys
import bpy
import os
from mathutils import Vector
import tempfile

is_floor = False
if '--' in sys.argv:
    argv = sys.argv[sys.argv.index('--') + 1:]
    if len(argv) > 0:
        if argv[0] == 'floor':
            is_floor = True

camera = bpy.data.objects[0]

def set_output_path(path):
    bpy.data.scenes['Scene'].render.filepath = path

def reset_camera():
    global camera
    camera.delta_location = Vector((0.0, 0.0, 0.0))

def translate_camera(vec):
    global camera
    camera.delta_location += vec

def load_image(path):
    try:
        return bpy.data.images.load(path)
    except:
        raise NameError('could not load image %s' % path)

bpy.context.scene.use_nodes = False
render = bpy.context.scene.render
render.resolution_x = 160
render.resolution_y = 160

images = {}
files = []
for x in range(-3, 4):
    for y in range(-2, 2):
        # Skip this position if it is right on top of
        # the player's location.
        if not is_floor and (x, y) == (0, 1):
            continue

        # Reserve a temporary file to hold the render.
        f = tempfile.NamedTemporaryFile(suffix='.png', delete=False)
        f.close()

        # Render the scene from this position.
        reset_camera()
        translate_camera(Vector((x * 2, y * 2, 0.0)))
        set_output_path(f.name)
        bpy.ops.render.render(write_still=True)

        # Load the rendered image.
        loaded_image = load_image(f.name)

        # Store the file.
        files.append(f)

        # Store the image.
        images[(x, y)] = loaded_image

# Set up the nodes system.
bpy.context.scene.use_nodes = True

# Create the background image.
bpy.ops.image.new(name='bg', width=640, height=640,
                  color=(0.0, 0.0, 0.0, 0.0), alpha=True)

# Set up the nodes.
tree = bpy.context.scene.node_tree
links = tree.links

for node in tree.nodes:
    tree.nodes.remove(node)

bg_node = tree.nodes.new(type='CompositorNodeImage')
bg_node.image = bpy.data.images['bg']

current_source = bg_node

for pos, image in images.items():
    x, y = pos
    image_node = tree.nodes.new(type='CompositorNodeImage')
    image_node.image = image
    translate_node = tree.nodes.new(type='CompositorNodeTranslate')
    translate_node.inputs[1].default_value = 80 + 160 - (abs(x) * 160) # X
    translate_node.inputs[2].default_value = 320 - 80 + (-(y + 2) * 160) # Y
    links.new(image_node.outputs[0], translate_node.inputs[0])
    alpha_over_node = tree.nodes.new(type='CompositorNodeAlphaOver')
    links.new(current_source.outputs[0], alpha_over_node.inputs[1])
    links.new(translate_node.outputs[0], alpha_over_node.inputs[2])
    current_source = alpha_over_node

comp_node = tree.nodes.new(type='CompositorNodeComposite')
links.new(current_source.outputs[0], comp_node.inputs[0])

# Render the final image.
print('Rendering image...')
set_output_path('out.png')
render.resolution_x = 640
render.resolution_y = 640
bpy.ops.render.render(write_still=True)

# Delete all the temporary files.
for f in files:
    os.unlink(f.name)

# Move the camera to its original location.
reset_camera()


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

import sqlite3
import sys
from random import randint

import xbmc

from resources.lib.modules.globals import g
from resources.lib.modules.plugin_monitor import PluginMonitor

g.init_globals(sys.argv)

g.log("##################  STARTING SERVICE  ######################")
g.log("### {} {}".format(g.ADDON_ID, g.VERSION))
g.log("### PLATFORM: {}".format(g.PLATFORM))
g.log("### SQLite: {}".format(sqlite3.sqlite_version))  # pylint: disable=no-member
g.log("### Detected Kodi Version: {}".format(g.KODI_VERSION))
g.log("#############  SERVICE ENTERED KEEP ALIVE  #################")

monitor = PluginMonitor()
try:
    g.wait_for_abort(60 * 30)  # Sleep to allow widget loads to complete.
    while not monitor.abortRequested():
        if not g.wait_for_abort(15):  # Sleep to make sure tokens refreshed during maintenance
            xbmc.executebuiltin(
                'RunPlugin("plugin://plugin.video.hs/?action=syncM3U")'
            )
        if g.wait_for_abort(60 * randint(13, 17)):
            break
finally:
    del monitor
    g.deinit()