#!/usr/bin/env python
import logging
import sys
from concurrent.futures import as_completed, ThreadPoolExecutor

import click
from pycircleci.api import Api, CircleciError

class CircleCIEnvsManage:
    """CircleCI environment variables managent Class"""

    def __init__(self, api_url, token, debug=False):
        """Initialize a client to interact with CircleCI API.

        :param url: CircleCI API URL.
        :param token: CircleCI API access token.
        :param debug: Enable verbose outputs.
        """

        self.circle_client = Api(token=token, url=api_url)

        if debug:
            logging.basicConfig(
                level=logging.DEBUG,
                format="[%(levelname)s] %(module)s:%(funcName)s - %(message)s",
                stream=sys.stdout
            )
        else:
            logging.basicConfig(
                level=logging.INFO,
                format="> %(message)s",
                stream=sys.stdout
            )

        self.logger = logging.getLogger(__class__.__name__)

    def get_context_id(
        self, org, owner_id, owner_type, vcs_type, context
    ):
        """Get CircleCI Project Context ID.

        :param org: Org or user name.
        :param owner_id: Org or user id.
        :param owner_type: Either ``organization`` or ``account``.
        :param vcs_type: VCS type (``github``, ``bitbucket``).
        :param context: Context name.
        """

        try:
            contexts = self.circle_client.get_contexts(
                org, owner_id, owner_type, vcs_type, True
            )
            context_id = list(
                filter(lambda c: True if c["name"] == context else None, contexts)
            )
        except Exception as error:
            raise SystemExit(f"Error: {error}")

        return context_id[0]["id"] if context_id else None

    def manage_context(
        self, org, owner_id, owner_type, vcs_type, context,
        context_id, env, value, create=False, delete=False
    ):
        """Manage CircleCI Contexts.

        :param org: Org or user name.
        :param owner_id: Org or user id.
        :param owner_type: Either ``organization`` or ``account``.
        :param vcs_type: VCS type (``github``, ``bitbucket``).
        :param context: Context name.
        :param context_id: Context ID.
        :param env: Context env var name.
        :param value: Context env var value.
        :param create: Context create (``True``, ``False``). Defaults to``False``.
        :param delete: Context delete (``True``, ``False``). Defaults to``False``.
        """

        try:
            if create:
                create_new_context = input(
                    f'The context named "{context}" was not found. Do you want to create it? [y/n]: '
                )

                if create_new_context.lower() == "y":
                    new_context = self.circle_client.add_context(
                        context, org, owner_id, owner_type, vcs_type
                    )
                    context_id = new_context["id"]

                    self.logger.info(f"Successfully create context: {context}")

                    return context_id

                exit()

            if delete:
                action = "delete"

                if not env:
                    confirm = input(
                        f'Are you sure want to delete the context "{context}"? [y/n]: '
                    )

                    if confirm.lower() == "y":
                        self.circle_client.delete_context(context_id)
                        self.logger.info("Successfully delete context")

                    exit()
                else:
                    self.circle_client.delete_context_envvar(context_id, env)
            else:
                action = "add/update"
                self.circle_client.add_context_envvar(context_id, env, value)

            self.logger.info(f"Successfully {action} context variable: {env}")

            return True
        except Exception as error:
            self.logger.error(f"Error: {error}")

            return False

    def manage_envvars(self, org, vcs_type, project, env, value, delete=False):
        """Manage CircleCI Project Environment Variables.

        :param org: Org or user name.
        :param vcs_type: VCS type (``github``, ``bitbucket``).
        :param project: Project name.
        :param env: Context env var name.
        :param value: Context env var value.
        :param delete: Context delete (``True``, ``False``). Defaults to``False``.
        """

        action = "add/update"

        try:
            if delete:
                action = "delete"
                self.circle_client.delete_envvar(org, project, env, vcs_type)
            else:
                self.circle_client.add_envvar(org, project, env, value, vcs_type)

            self.logger.info(f"Successfully {action} variable: {env}")
            return True
        except Exception as error:
            self.logger.error(f"Error: {error}")
            return False

    def main(self, owner_type, context, project_slug, env, env_file, list_envs, delete):
        """Manage the CircleCI Contexts and Env Vars

        :param owner_type: Either ``organization`` or ``account``.
        :param context: Context name.
        :param project_slug: Project slug. Example: ``github/org_name/project_name``.
        :param env: Env var name and value. Example: ``KEY1=VALUE1``.
        :param env_file: Env vars file. Must be in the format: ``KEY1=VALUE1``, one per line.
        :param delete: Delete envs or the context (``True``, ``False``). Defaults to``False``.
        """

        vars = []

        try:
            vcs_type, org, project_name = project_slug.split("/")
        except ValueError:
            raise SystemExit(
                "> Error: Invalid project slug. Should be in the format: "
                "(github or bitbucket)/org_name/project_name"
            )

        if env:
            vars += list(env)

        if env_file:
            vars += list(set(env_file.read().strip().split("\n")))

        if context:
            project_info = self.circle_client.get_project(project_slug)
            owner_id = project_info["organization_id"]
            context_id = self.get_context_id(
                org, owner_id, owner_type, vcs_type, context
            )

            if not context_id:
                context_id = self.manage_context(
                    org, owner_id, owner_type, vcs_type, context,
                    context_id, None, None, create=True
                )

            if list_envs:
                context_envs = self.circle_client.get_context_envvars(context_id, True)
                self.logger.info("\n"+"\n".join([x["variable"] for x in context_envs]))
                exit()

            if not vars and delete and context_id:
                self.manage_context(
                    org, owner_id, owner_type, vcs_type, context,
                    context_id, None, None, delete=True
                )

        if list_envs:
            try:
                envvars = self.circle_client.list_envvars(org, project_name, vcs_type)
                self.logger.info("\n"+"\n".join([x["name"] for x in envvars]))
                exit()
            except Exception as error:
                self.logger.error(error)

        with ThreadPoolExecutor(max_workers=5) as executor:
            threadpool = []

            for var in vars:
                try:
                    key, value = var.split("=", 1)
                except ValueError:
                    key, value = (var, "")
                except Exception:
                    raise SystemExit(f"Error: failed to proccess variable {var}...")

                if context:
                    threadpool.append(
                        executor.submit(
                            self.manage_context, org, owner_id, owner_type,
                            vcs_type, context, context_id, key.strip(),
                            value.strip(), False, delete
                        )
                    )
                else:
                    threadpool.append(
                        executor.submit(
                            self.manage_envvars, org, vcs_type, project_name,
                            key.strip(), value.strip(), delete
                        )
                    )

            for thread in as_completed(threadpool):
                thread.result()

@click.command()
@click.option(
    "-u", "--api-url",
    metavar="<circleci_api_url>",
    default="https://circleci.com/api",
    show_default=True,
)
@click.option(
    "-t", "--token",
    metavar="<circleci_token>",
    show_default="CIRCLE_TOKEN"
)
@click.option(
    "-c", "--context",
    metavar="<context_name>",
    help="It will ask for create if does not exists",
)
@click.option(
    "-p", "--project",
    metavar="<project_slug>",
    required=True,
    help="Example: github/org-name/project-name",
)
@click.option(
    "-e", "--env",
    multiple=True,
    metavar="<environment_var>"
)
@click.option(
    "-ef", "--env-file",
    metavar="<environment_vars_file>",
    type=click.File("r")
)
@click.option(
    "-l", "--list-envs",
    metavar="<environment_vars_list>",
    is_flag=True
)
@click.option(
    "-d", "--delete",
    metavar="<environment_var_delete>",
    is_flag=True,
    help=("Context only, will delete the context\n\b"
          " Context + vars, will delete the context vars"
          " Vars only, will delete the environment vars")
)
@click.option(
    "-ot", "--owner-type",
    metavar="<owner_type>",
    default="organization",
    show_default=True,
)
@click.option(
    "--debug",
    metavar="<debug>",
    default=False,
    is_flag=True
)
@click.pass_context
def cli(ctx, api_url, token, context, project, env, env_file, list_envs, delete, owner_type, debug):
    """CLI tool for manage CircleCI contexts and environment vars

    All options can be specified as environment variables in the format:
    CIRCLE_<OPTION>. Example: CIRCLE_TOKEN=********
    """

    try:
        ctx.obj = CircleCIEnvsManage(api_url, token, debug)
        ctx.obj.main(owner_type, context, project, env, env_file, list_envs, delete)
    except CircleciError as error:
        raise SystemExit(f"Error: {error}")

if __name__ == "__main__":
    cli(auto_envvar_prefix="CIRCLE")


import logging

import joblib
import numpy as np
import pandas as pd
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import LabelEncoder

logging.basicConfig(format="%(asctime)-15s %(levelname)s %(message)s", level=logging.INFO)
logger = logging.getLogger(__name__)

def load_data(filename="../data/adult.csv", **kwargs):
    logger.info("Loading data")

    df = pd.read_csv(filename, **kwargs).replace({'?': np.nan}).dropna()
    df.to_csv("../data/train.csv", index=False)
    return df

def train_encoder(data):
    logger.info("Training encoder")

    categorical_features = [
        "workclass", "education", "marital-status",
        "occupation", "relationship", "race", "gender",
        "capital-gain", "capital-loss", "native-country", "income"
    ]
    numerical_features = [
        "age", "fnlwgt", "educational-num",
        "capital-gain", "capital-loss", "hours-per-week"
    ]

    encoders = {}
    for column in categorical_features:
        categorical_encoder = LabelEncoder()
        data[column] = categorical_encoder.fit_transform(data[column])
        encoders[column] = categorical_encoder
    return encoders

def train_model(data):
    logger.info("Training model")

    X, y = data.drop('income', axis=1), data['income']
    train_X, test_X, train_y, test_y = train_test_split(
        X, y.astype(int), stratify=y, test_size=0.2, random_state=42)

    clf = RandomForestClassifier(
        n_estimators=20,
        max_depth=10,
        n_jobs=5,
        random_state=42
    ).fit(train_X, train_y)
    return clf

def dump_artifacts(model, encoder):
    logger.info("Dumping artifacts")

    joblib.dump(model, '../models/model/model.joblib')
    joblib.dump(encoder, "../models/model/encoders.joblib")
    joblib.dump(encoder, "../models/metric/encoders.joblib")

if __name__ == "__main__":
    data = load_data()
    encoder = train_encoder(data)
    model = train_model(data)
    dump_artifacts(model, encoder)


# https://school.programmers.co.kr/learn/courses/30/lessons/60058

def separate(p):
    u = ''
    v = ''
    left = 0
    for i in range(0, len(p)):
        u += p[i]
        if p[i] == "(":
            left += 1
        if left == i - left + 1:
            v += p[i + 1:]
            break
    return u, v

def solution(p):
    answer = ''
    u, v = separate(p)
    if len(p) > 0:
        if u[0] == "(" and u[-1] == ")":
            v = solution(v)
            answer = u + v
        else:
            v = solution(v)
            answer = '(' + v + ')'
            for i in u[1:-1]:
                if i == "(":
                    answer += ')'
                elif i == ')':
                    answer += '('
    return answer


import os
from os.path import join, isfile, basename, exists

import torch
import numpy as np
import nibabel as nib
import SimpleITK as sitk

class FolderIterator():
    def __init__(self, input_dir_or_file, output_dir, xform=None):
        self.fileiter_list = []

        if isfile(input_dir_or_file):
            scan_list = [input_dir_or_file]
        else:
            scan_list = [join(input_dir_or_file, f) for f in os.listdir(input_dir_or_file)]

        for scan_path in scan_list:
            if scan_path.endswith('.nii.gz') or scan_path.endswith('.nii'):
                self.fileiter_list.append(NiftiIterator(scan_path, output_dir, xform))
            elif scan_path.endswith('.img'):
                self.fileiter_list.append(MRIIterator(scan_path, output_dir, xform))
            elif scan_path.endswith('.npy'):
                raise NotImplementedError('.npy file support is not implemented yet for the file {}'.format(scan_path))

    def __len__(self):
        return len(self.fileiter_list)

    def __iter__(self):
        for fiter in self.fileiter_list:
            for slce in fiter:
                yield slce, fiter.output_handler
            fiter.save_all()

class SingleSliceIterator():
    def __init__(self, input_file, output_dir, xform=None):
        self.input_path = input_file
        self.patient_name = basename(input_file).split('.')[0]
        self.file_extension = '.'.join(basename(input_file).split('.')[1:])
        self.output_dir = join(output_dir, self.patient_name)
        self.xform = xform

        self.heatmap_dict = {}

    def __iter__(self):
        input_3d = self.get_data_as_np() # [slice, H,W]

        nslices = input_3d.shape[0]
        for i in range(nslices):
            slice_raw = input_3d[i:i+1, ...]
            if self.xform is not None:
                slice_preproc = self.xform(slice_raw.transpose((1,2,0)))
            else:
                slice_preproc = torch.from_numpy(slice_raw)

            # Add dummy batch dimension
            slice_preproc = slice_preproc[np.newaxis,...]

            yield slice_preproc

    def output_handler(self, target, heatmap):
        if target not in self.heatmap_dict:
            self.heatmap_dict[target] = []
        self.heatmap_dict[target].append(heatmap)

    def save_all(self):
        for target, hlist in self.heatmap_dict.items():
            if not exists(self.output_dir):
                os.makedirs(self.output_dir)
            target_filename = join(self.output_dir, '{}.{}'.format(target, self.file_extension))
            np_data = np.array(hlist)
            self.save(np_data, target_filename)

    def save(self, np_data, filename):
        # It should save the raw numpy array in the form of original input format with meta data
        pass

    def get_data_as_np(self):
        # It should return the image as numpy array with shape [slice, height, width]
        pass

class NiftiIterator(SingleSliceIterator):
    def __init__(self, input_file, output_dir, xform=None):
        super(NiftiIterator, self).__init__(input_file, output_dir, xform)
        self.nifti_img = nib.load(input_file)

    def get_data_as_np(self):
        return self.nifti_img.get_fdata().astype(np.float32).transpose((2,0,1))

    def save(self, np_data, filename):
        nifti_out = nib.Nifti1Image(np_data.transpose((1,2,0)), self.nifti_img.affine)
        nib.save(nifti_out, filename)

class MRIIterator(SingleSliceIterator):
    def __init__(self, input_file, output_dir, xform=None):
        super(MRIIterator, self).__init__(input_file, output_dir, xform)
        self.mri_img = sitk.ReadImage(input_file, imageIO="NiftiImageIO")

    def get_data_as_np(self):
        return sitk.GetArrayFromImage(self.mri_img).astype(np.float32)

    def save(self, np_data, filename):
        #mri_orig = sitk.ReadImage(self.input_path, imageIO="NiftiImageIO")
        #mri_img.set
        #sitk.WriteImage(image, filename)
        pass


import numpy as np

import torch
from torch.utils.data import DataLoader
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")

from sklearn.model_selection import train_test_split

from tqdm import tqdm

from utils import utils, load, deepctr_utils

from model import DeepCTR_Model

import argparse

def main():
    data_name_list = ["movielens", "yelp", "douban_book"]
    model_name_list = ["deepfm", "ipnn", "opnn", "pin", "ccpm", "afm", "nfm", "xdeepfm", "fnn", "wd", "dcn"]
    # Argument
    parser = argparse.ArgumentParser()
    parser.add_argument("--dataname", "-d", type=str, nargs='?', choices=data_name_list, help="DataSet name")
    parser.add_argument("--modelname", "-m", type=str, nargs='?', choices=model_name_list, help="Model name")
    args = parser.parse_args()

    # Load dataset and load feature's type dict
    dataset = load.LoadDataSet(args.dataname)

    user_item = dataset["user_item"]
    all_uids = dataset["uids"]
    all_iids = dataset["iids"]
    user_features_dict, item_features_dict = dataset["user_features_dict"], dataset["item_features_dict"]
    user_features_type, item_features_type = dataset["user_features_type"], dataset["item_features_type"]

    # Get user's feature columns and item's feature columns
    features_columns = deepctr_utils.GetFeatColumns(user_features_type) + deepctr_utils.GetFeatColumns(item_features_type)

    train_data, test_data = train_test_split(user_item, test_size=0.2, random_state=1024)
    train_score = train_data[:,2]

    train_model_input = utils.BatchGetModelInput(
        user_features_dict=user_features_dict,
        item_features_dict=item_features_dict,
        batch_user_id=train_data[:,0].astype(int),
        batch_item_id=train_data[:,1].astype(int),
        user_ftype=user_features_type,
        item_ftype=item_features_type,
    )

    train_dataset = utils.RateDataset(
        user_tensor=torch.LongTensor(train_data[:,0].astype(int)),
        item_tensor=torch.LongTensor(train_data[:,1].astype(int)),
        target_tensor=torch.Tensor(train_data[:,2]),
    )

    test_dataset = utils.RateDataset(
        user_tensor=torch.LongTensor(test_data[:,0].astype(int)),
        item_tensor=torch.LongTensor(test_data[:,1].astype(int)),
        target_tensor=torch.Tensor(test_data[:,2]),
    )
    train_loader = DataLoader(train_dataset, batch_size=256, shuffle=True)
    test_loader = DataLoader(test_dataset, batch_size=256, shuffle=False)

    device = 'cpu'
    use_cuda = True
    if use_cuda and torch.cuda.is_available():
        print('cuda ready...')
        device = 'cuda:0'

    model = DeepCTR_Model.BuildModel(args.modelname, features_columns, device)
    model.compile("adam", "mse", metrics=['mse'], )

    history = model.fit(train_model_input, train_score, batch_size=256, epochs=20, verbose=2)

    total_loss = 0
    print("Calculating TrainSet RMSE...")
    with torch.no_grad():
        for batch in tqdm(train_loader):
            u, i, r = batch[0], batch[1], batch[2]
            r = r.float()

            # forward pass
            batch_model_input = utils.BatchGetModelInput(
                user_features_dict=user_features_dict,
                item_features_dict=item_features_dict,
                batch_user_id=u,
                batch_item_id=i,
                user_ftype=user_features_type,
                item_ftype=item_features_type,
            )

            preds = model.predict(batch_model_input, batch_size=256)
            loss = np.sum((preds.reshape(-1) - r.numpy())**2).item()
            total_loss += loss
        total_loss /= train_dataset.__len__()
    print("TrainSet RMSE:", np.sqrt(total_loss))

    total_loss = 0
    print("Calculating TestSet RMSE...")
    with torch.no_grad():
        for batch in tqdm(test_loader):
            u, i, r = batch[0], batch[1], batch[2]
            r = r.float()

            # forward pass
            batch_model_input = utils.BatchGetModelInput(
                user_features_dict=user_features_dict,
                item_features_dict=item_features_dict,
                batch_user_id=u,
                batch_item_id=i,
                user_ftype=user_features_type,
                item_ftype=item_features_type,
            )

            preds = model.predict(batch_model_input, batch_size=256)
            loss = np.sum((preds.reshape(-1) - r.numpy())**2).item()

            total_loss += loss
        total_loss /= train_dataset.__len__()
    print("TestSet RMSE:", np.sqrt(total_loss))

    all_test_data_id = np.unique(test_data[:,0]).astype(int)

    metrics_list = []
    for uid in tqdm(all_test_data_id):
        metrics_list.append(deepctr_utils.Predict(
            Id_info_dict=dict(user_id=uid, item_min_id=all_iids[0], item_max_id=all_iids[-1]),
            data_pair_dict=dict(train=train_data[:,:2].astype(int), test=test_data.astype(int)),
            features_dict_dict=dict(user=user_features_dict, item=item_features_dict),
            features_type_dict=dict(user=user_features_type, item=item_features_type),
            model=model,
        ))

    metrics_arr = np.mean(metrics_list, axis=0)
    print("Mean of Recall@10:", metrics_arr[0])
    print("Mean of NDCG@10:", metrics_arr[1])

    utils.SaveObject("metric_result/" + args.modelname + "_" + args.dataname + "_metrics.pkl", metrics_list)

if __name__ == "__main__":
    main()


import datetime

from delivery.dilivery_core import Client
from delivery.dilivery_core.Client import Configuration, token_verif
import requests
from delivery.dilivery_core.shemas import Delivery
class SDEKDelivery:

    client = Client

    @classmethod
    @token_verif(token=Configuration.token, time=Configuration.time)
    def get_token(cls):
        return {'token':Configuration.token}

    @classmethod
    @token_verif(token=Configuration.token, time=Configuration.time)
    def create_delivery(cls, delivery_obj: Delivery.DeliveryAdd):
        data = delivery_obj.to_json()
        a = requests.post(f'{Configuration.api_url}orders', headers={
            'Authorization': f"Bearer {Configuration.token}",
            'Content-Type': 'application/json',
            "Accept": "application/json",
        },
                      data=data
                      )
        return a

    @classmethod
    @token_verif(token=Configuration.token, time=Configuration.time)
    def get_delivery(cls, uuid):
        a = requests.get(f'{Configuration.api_url}orders/{uuid}/', headers={
            'Authorization': f"Bearer {Configuration.token}",
            'Content-Type': 'application/json',
            "Accept": "application/json",
        },
                      )
        return a

    @classmethod
    @token_verif(token=Configuration.token, time=Configuration.time)
    def get_price_delivery(cls, from_code, to_code):
        a = requests.post(f'{Configuration.api_url}calculator/tariff', headers={
            'Authorization': f"Bearer {Configuration.token}",
            'Content-Type': 'application/json',
            "Accept": "application/json",
        }, data={
    "type": "2",
    "currency": "1",
    "tariff_code": "136",
    "from_location": {
        "code": 44
    },
    "to_location": {
        "code": 44
    },
    "packages": [
        {
            "height": 10,
            "length": 10,
            "weight": 4000,
            "width": 10
        }
    ]
}
                      )
        return a


import torch
import torch.nn.functional as F
import math
import Bio.PDB
from Bio.PDB.DSSP import DSSP
import numpy as np
from torch_geometric.data import Data
from torch_geometric.nn import radius_graph
from aminoacids import aa_charge, ss_mapping, aa_1_mapping, aa_polar

class ProteinGraphBuilder:
    def __init__(self, max_distance=10, max_num_neighbors=128):
        self.max_distance = max_distance
        self.radius_graph = RadiusGraph(max_distance,
                                        loop=False,
                                        max_num_neighbors=max_num_neighbors)

    def build_graph(self, id: str,
                    pdb_file: str,
                    amd: int = -1,
                    label: int = -1, ) -> Data:
        parser = Bio.PDB.PDBParser()
        structure = parser.get_structure(id, pdb_file)

        model = next(structure.get_models())
        chain = list(next(model.get_chains()))
        seq_length = len(chain)

        aa = np.full((seq_length, 1), dtype=np.float32, fill_value=np.nan)
        ss = np.full((seq_length, 1), dtype=np.float32, fill_value=np.nan)

        ca_coordinates = np.full((seq_length, 3), dtype=np.float32, fill_value=np.nan)
        c_coordinates = np.full((seq_length, 3), dtype=np.float32, fill_value=np.nan)
        n_coordinates = np.full((seq_length, 3), dtype=np.float32, fill_value=np.nan)

        sequence = ""

        dssp_feats = DSSP(model, pdb_file, dssp='mkdssp')
        aa_keys = list(dssp_feats.keys())

        for idx in range(seq_length):
            feats = dssp_feats[aa_keys[idx]]

            ca_coordinates[idx] = chain[idx]['CA'].get_vector()[:3]
            c_coordinates[idx] = chain[idx]['C'].get_vector()[:3]
            n_coordinates[idx] = chain[idx]['N'].get_vector()[:3]

            sequence += feats[1]
            aa[idx] = aa_1_mapping[feats[1]]
            ss[idx] = ss_mapping[feats[2]]

        forward_vec, backward_vec = orientations(ca_cords=ca_coordinates)

        side_chain_vec = side_chains(origin=ca_coordinates,
                                     n=n_coordinates,
                                     c=c_coordinates)

        angles = dihedral_angles(np.stack([n_coordinates,
                                           ca_coordinates,
                                           c_coordinates], 1))

        node_v = np.concatenate([forward_vec,
                                 backward_vec,
                                 side_chain_vec], axis=1)

        out = Data(
            id=id,
            seq=sequence,
            ss=torch.from_numpy(ss).long(),
            aa=torch.from_numpy(aa).long(),
            amd=torch.tensor(amd).to(torch.float32),
            x=angles.to(torch.float32),
            v=torch.from_numpy(node_v.reshape((node_v.shape[0], node_v.shape[1] // 3, 3))).to(torch.float32),
            y=torch.tensor(label).to(torch.float32),
            pos=torch.from_numpy(ca_coordinates).to(torch.float32))

        self.radius_graph(out)
        self.add_edge_feats(out)

        return out

    @staticmethod
    def add_edge_feats(data: Data):
        edge_vec = data.pos[data.edge_index[0]] - data.pos[data.edge_index[1]]
        rbf_feats = rbf(edge_vec.norm(dim=-1), D_count=16)
        pos_feats = positional_embeddings(data.edge_index, num_embeddings=16)
        data.edge_s = torch.cat([rbf_feats, pos_feats], dim=1)
        data.edge_v = normalize(edge_vec).view(edge_vec.size(0), edge_vec.size(1) // 3, 3)

        return data

class RadiusGraph(object):
    def __init__(self, r, loop=False, max_num_neighbors=32):
        self.r = r
        self.loop = loop
        self.max_num_neighbors = max_num_neighbors

    def __call__(self, data: Data) -> Data:
        pos = data.pos.clone()
        batch = data.batch if "batch" in data else None
        edge_index = radius_graph(
            pos, self.r, batch, self.loop, self.max_num_neighbors,
        )
        data.edge_index = edge_index

        return data

def normalize(ndarray, dim=-1):
    return np.divide(ndarray, np.linalg.norm(ndarray, axis=dim, keepdims=True))

def orientations(ca_cords):
    forward = normalize(ca_cords[1:] - ca_cords[:-1])
    forward = np.vstack([forward, np.zeros((3,))])
    backward = normalize(ca_cords[:-1] - ca_cords[1:])
    backward = np.vstack([np.zeros((3,)), backward])
    return forward, backward

def dihedral_angles(cords, eps=1e-7):
    X = torch.reshape(torch.from_numpy(cords), [3 * cords.shape[0], 3])
    dX = X[1:] - X[:-1]
    U = normalize(dX, dim=-1)
    u_2 = U[:-2]
    u_1 = U[1:-1]
    u_0 = U[2:]

    # Backbone normals
    n_2 = normalize(torch.cross(u_2, u_1), dim=-1)
    n_1 = normalize(torch.cross(u_1, u_0), dim=-1)

    # Angle between normals
    cosD = torch.sum(n_2 * n_1, -1)
    cosD = torch.clamp(cosD, -1 + eps, 1 - eps)
    D = torch.sign(torch.sum(u_2 * n_1, -1)) * torch.acos(cosD)

    D = F.pad(D, [1, 2])
    D = torch.reshape(D, [-1, 3])
    # Lift angle representations to the circle
    D_features = torch.cat([torch.cos(D), torch.sin(D)], 1)
    return D_features

def side_chains(origin, n, c):
    c, n = normalize(c - origin), normalize(n - origin)
    bisector = normalize(c + n)
    perp = normalize(np.cross(c, n))
    vec = -bisector * math.sqrt(1 / 3) - perp * math.sqrt(2 / 3)
    return vec

def rbf(D, D_min=0., D_max=20., D_count=16):
    D_mu = torch.linspace(D_min, D_max, D_count)
    D_mu = D_mu.view([1, -1])
    D_sigma = (D_max - D_min) / D_count
    D_expand = torch.unsqueeze(D, -1)

    RBF = torch.exp(-((D_expand - D_mu) / D_sigma) ** 2)
    return RBF

def positional_embeddings(edge_index,
                          num_embeddings=None):
    d = edge_index[0] - edge_index[1]
    frequency = torch.exp(
        torch.arange(0, num_embeddings, 2, dtype=torch.float32)
        * -(np.log(10000.0) / num_embeddings)
    )
    angles = d.unsqueeze(-1) * frequency
    pos_encoding = torch.cat((torch.cos(angles), torch.sin(angles)), -1)
    return pos_encoding


import numpy as np
from skimage.draw import rectangle
from allroom.envs.mazelab.generators.utils import save_maze

# Free space is the grid minus boundary
# Passage are free space minus obstacle rectangle
def u_maze(width, height, obstacle_width, obstacle_height):
	x = np.zeros([height, width], dtype=np.uint8)
	# boundary are walls
	x[0, :] = 1
	x[-1, :] = 1
	x[:, 0] = 1
	x[:, -1] = 1

	# [start_r, start_c]
	start = [height//2 - obstacle_height//2, 1]
	rr, cc = rectangle(start, extent=[obstacle_height, obstacle_width], shape=x.shape)
	x[rr, cc] = 1

	# opening is rightwards
	x = np.fliplr(x)

	env_id = 'umaze-v0'

	save_maze(env_id, x)

	return x

if __name__ == "__main__":
	maze = u_maze(12, 9, 8, 2)
	print(maze)


#!/usr/bin/python3

import os
import subprocess
import sys

def cmd_available(name):
    try:
        with open(os.devnull, 'w') as output:
            subprocess.call([name], stdout=output, stderr=output)
    except OSError:
        return False
    return True

def remove_exif_on_file(file):
    print('\t' + file)
    devnull = open(os.devnull, 'w')
    subprocess.call(['exiftool', '-overwrite_original',
                     '-Make=',
                     '-Model=',
                     '-GPS*=',
                     '-Gps*=',
                     '-gps*=',
                     '-CreateDate=',
                     '-DateTimeOriginal=',
                     '-Software=',
                     '-ImageDescription=',
                     '-SubSecCreateDate=',
                     '-SubSecDateTimeOriginal=',
                     '-SubSecModifyDate=',
                     '-ICC_Profile:all=',
                     '-Photoshop:all=',
                     '-XMP:all=',
                     '-IPTC:all=',
                     '-MakerNotes:all=',
                     file], stdout=devnull, stderr=subprocess.STDOUT)

def remove_exif(file_list):
    for file in file_list:
        remove_exif_on_file(file)

    print('Total updated files: ' + str(len(file_list)))

def main():
    if not cmd_available('exiftool'):
        print('\033[91m {}\033[00m'.format('** No exiftool command available on this machine'))
        print('\033[91m {}\033[00m'.format('** Please run \'sudo apt install exiftool\''))
        return

    work_dir = os.getcwd()

    if len(sys.argv) > 1:
        print('Removing exif data on media files')
        for file in sys.argv[1:]:
            if file.endswith('.jpg') or file.endswith('.jpeg') or file.endswith('.png') or file.endswith('.mp4'):
                remove_exif_on_file(file)
    else:
        all_img_files = []
        for dir, subdirs, files in os.walk(work_dir):
            for file in files:
                all_img_files.append(file)

        remove_exif(all_img_files)

if __name__ == '__main__':
    main()


import pytest
from hypothesis import given, settings

import schemathesis

@pytest.fixture()
def graphql_schema(graphql_endpoint):
    return schemathesis.graphql.from_url(graphql_endpoint)

@pytest.fixture
def graphql_strategy(graphql_schema):
    return graphql_schema["/graphql"]["POST"].as_strategy()

def test_raw_schema(graphql_schema):
    assert graphql_schema.raw_schema["__schema"]["types"][1] == {
        "kind": "OBJECT",
        "name": "Patron",
        "fields": [
            {
                "name": "id",
                "args": [],
                "type": {"kind": "SCALAR", "name": "ID", "ofType": None},
                "isDeprecated": False,
                "deprecationReason": None,
            },
            {
                "name": "name",
                "args": [],
                "type": {"kind": "SCALAR", "name": "String", "ofType": None},
                "isDeprecated": False,
                "deprecationReason": None,
            },
            {
                "name": "age",
                "args": [],
                "type": {"kind": "SCALAR", "name": "Int", "ofType": None},
                "isDeprecated": False,
                "deprecationReason": None,
            },
        ],
        "inputFields": None,
        "interfaces": [],
        "enumValues": None,
        "possibleTypes": None,
    }

@pytest.mark.hypothesis_nested
def test_query_strategy(graphql_strategy):
    @given(case=graphql_strategy)
    @settings(max_examples=10)
    def test(case):
        response = case.call()
        assert response.status_code < 500

    test()

@pytest.mark.filterwarnings("ignore:.*method is good for exploring strategies.*")
def test_get_code_to_reproduce(graphql_endpoint, graphql_strategy):
    case = graphql_strategy.example()
    assert case.get_code_to_reproduce() == f"requests.post('{graphql_endpoint}', json={{'query': {repr(case.body)}}})"

@pytest.mark.filterwarnings("ignore:.*method is good for exploring strategies.*")
def test_as_werkzeug_kwargs(graphql_strategy):
    case = graphql_strategy.example()
    assert case.as_werkzeug_kwargs() == {
        "method": "POST",
        "path": "/graphql",
        "query_string": None,
        "json": {"query": case.body},
        "headers": {},
    }


from django.urls import path
from . import views

urlpatterns=[
    path('', views.index, name='category_index'),
    # path('books/<int:pk>/', views.BookDetailView.as_view(), name='book_detail'),
]

# Books UrlConfig
urlpatterns +=[
    path('books/', views.BookListView.as_view(), name='books'),
    path('books/<int:isbn>/', views.BookDetailView.as_view(), name='book_detail'),
    # path('books/<int:isbn>/', views.book_detail, name='book_detail'),
    path('books/create/', views.BookCreate.as_view(), name='book_create'),
    path('books/<int:isbn>/update/', views.BookUpdate.as_view(), name='book_update'),
    path('books/<int:isbn>/delete/', views.BookDelete.as_view(), name='book_delete'),
]

# Authors URLconfig
urlpatterns += [
    path('authors/', views.AuthorListView.as_view(), name='authors'),
    path('authors/<int:pk>/', views.AuthorDetailView.as_view(), name='author_detail'),
    path('authors/create/', views.AuthorCreate.as_view(), name='author_create'),
    path('authors/<int:pk>/update/', views.AuthorUpdate.as_view(), name='author_update'),
    path('authors/<int:pk>/delete/', views.AuthorDelete.as_view(), name='author_delete'),
]

# Super user URLConig
urlpatterns += [
    path('mybooks/', views.LoanedBooksByUserListView.as_view(), name='my_borrowed'),
    path(r'borrowed/', views.LoanedBooksAllListView.as_view(), name='all_borrowed'),
    path('books/<uuid:pk>/renew/', views.renew_book_librarian, name='renew_book_librarian'),

]


import sys
from typing import List, Optional

from src.config.local_paths import LocalPaths
from src.config.program_config import ProgramConfig
from src.config.test_case_config import TestCaseConfig
from src.config.types.mode_type import ModeType
from src.utils.common import log_separator, log_sub_header
from src.utils.logging.log_level import LogLevel
from src.utils.logging.logger import Logger

class TestBenchSettings:
    """Settings for a test bench run"""

    log_level: LogLevel = LogLevel.INFO
    local_paths: Optional[LocalPaths] = None
    programs: List[ProgramConfig] = []
    configs: List[TestCaseConfig] = []
    run_mode: ModeType = ModeType.COMPARE
    config_file: str
    user_name: str
    filter: str
    autocommit: bool = False
    only_post: bool = False
    teamcity: bool = False
    test_bench_root: Optional[str] = None
    test_bench_script_name: Optional[str] = None
    test_bench_startup_dir: Optional[str] = None

    def log_overview(self, logger: Logger):
        """Logs overview of the parameters

        Args:
            logger (Logger): logger to log to
        """
        log_sub_header("Parsed arguments", logger)

        logger.info(f"Version  : {sys.version}")
        logger.info(f"Mode     : {self.run_mode}")
        logger.info(f"Config   : {self.config_file}")
        logger.info(f"Filter   : {self.filter}")
        logger.info(f"LogLevel : {str(self.log_level)}")
        logger.info(f"Username : {self.user_name}")

        log_separator(logger, char="-", with_new_line=True)


@log
def arg_parser():
    parser = argparse.ArgumentParser()
    parser.add_argument('-p', default = DEFAULT_PORT, type=int, nargs='?')
    parser.add_argument('-a', default='', nargs='?')
    namespace = parser.parse_args(sys.argv[1:])
    listen_address = namespace.a
    listen_port = namespace.p
    return listen_address, listen_port

class Server(metaclass=ServerMaker):
    port = Port()
    def __init__(self, listen_address, listen_port):
        self.addr = listen_address
        self.port = listen_port


import glob
import pandas as pd
import math
import re
from sklearn.metrics import f1_score, precision_score, recall_score, confusion_matrix, classification_report
import time

start_time = time.time()

trainentries = glob.glob('/Users/cristinaramos/desktop/lingspam_public/lemm_stop/part1/*.txt')
testentries = glob.glob('/Users/cristinaramos/desktop/lingspam_public/lemm_stop/part10/*.txt')

traindata = []
testdata = []

for fn in trainentries:
    if 'spmsga' in fn:
        cat = 'spam'
    else:
        cat = 'ham'
    with open(fn) as f:
        messages = f.read()
        traindata.append((cat, messages))
        # print(cat, fn)

for fn in testentries:
    if 'spmsgc' in fn:
        cat = 'spam'
    else:
        cat = 'ham'
    with open(fn) as f:
        messages = f.read()
        testdata.append((cat, messages))
        # print(cat, fn)

train = pd.DataFrame(traindata, columns=['Label', 'Message'])

train['Message'] = train['Message'].str.replace('\W', ' ')
train['Message'] = train['Message'].str.replace('\d+', '')
train['Message'] = train['Message'].str.lower()
train['Message'] = train['Message'].str.split()

test_set = pd.DataFrame(testdata, columns=['Label', 'Message'])

totalwords = []
for message in train['Message']:
    for word in message:
        totalwords.append(word)

all_unq_words = list(set(totalwords))

wordcount_dict = {word: [0] * len(train['Message']) for word in all_unq_words}
for i, message, in enumerate(train['Message']):
    for word in message:
        wordcount_dict[word][i] += 1

a = 0.5

word_counts = pd.DataFrame(wordcount_dict)
train_set = pd.concat([train, word_counts], axis=1)

spamdocs = train_set[train_set['Label'] == 'spam']
hamdocs = train_set[train_set['Label'] == 'ham']

prior_spam = math.log(len(spamdocs) / len(train_set))
prior_ham = math.log(len(hamdocs) / len(train_set))

sp_words_message = spamdocs['Message'].apply(len)
spamwordcount = sp_words_message.sum()

hm_words_message = hamdocs['Message'].apply(len)
hamwordcount = hm_words_message.sum()

v = len(all_unq_words)

spam_word_probs = {word: 0 for word in all_unq_words}
ham_word_probs = {word: 0 for word in all_unq_words}

for word in all_unq_words:
    word_spam = spamdocs[word].sum()
    word_spamprob = math.log((word_spam + a) / (spamwordcount + (a * v)))
    spam_word_probs[word] = word_spamprob

    word_ham = hamdocs[word].sum()
    word_hamprob = math.log((word_ham + a) / (hamwordcount + (a * v)))
    ham_word_probs[word] = word_hamprob

def test(message):
    message = re.sub('\W', ' ', message)
    message = re.sub('\d+', '', message)
    message = message.lower().split()
    max_spprob = prior_spam
    max_hmprob = prior_ham
    for word in message:
        if word in spam_word_probs:
            max_spprob += spam_word_probs[word]
        if word in ham_word_probs:
            max_hmprob += ham_word_probs[word]
    if max_spprob > max_hmprob:
        return 'spam'
    elif max_hmprob > max_spprob:
        return 'ham'

test_set['Y'] = test_set['Message'].apply(test)
test_set.head()

mymap = {'ham': 0, 'spam': 1}
test_set['Label'] = test_set['Label'].map(mymap)
test_set['Y'] = test_set['Y'].map(mymap)

x = list(test_set.Label)
y = list(test_set.Y)

print('precision score', precision_score(x, y))
print('recall', recall_score(x, y))
print('f_1', f1_score(x, y))
print('conf matrix', '\n', confusion_matrix(x, y))
print('classification report', '\n', classification_report(x, y, labels=[0, 1]))

print(time.time() - start_time)


# Cargado y limpieza de datos
import sys
import functions as fn
import pandas as pd

# Cargado de datos
data1 = fn.f_leer_archivo("Data1")
data2 = fn.f_leer_archivo("Data2")
data3 = fn.f_leer_archivo("Data3")
data_total = pd.concat([data1,data2,data3]).reset_index()
instruments_pips = fn.f_leer_archivo("instruments_pips")

# Número de pips
parametro = "EURUSD"
num_pips1 = fn.f_pip_size1(parametro, instruments_pips)
num_pips2 = fn.f_pip_size2(parametro)

# Agregado de columnas de tiempo
data1 = fn.f_columnas_tiempos(data1)
data2 = fn.f_columnas_tiempos(data2)
data3 = fn.f_columnas_tiempos(data3)

# Agregado de columnas de pips
data1 = fn.f_columnas_pips(data1, fn.f_pip_size2)
data2 = fn.f_columnas_pips(data2, fn.f_pip_size2)
data3 = fn.f_columnas_pips(data3, fn.f_pip_size2)

# Estadísticas de transacciones
df_1_tabla_data1 = fn.f_estadisticas_ba(data1, fn.f_columnas_pips, fn.f_pip_size2)
df_1_tabla_data2 = fn.f_estadisticas_ba(data2, fn.f_columnas_pips, fn.f_pip_size2)
df_1_tabla_data3 = fn.f_estadisticas_ba(data3, fn.f_columnas_pips, fn.f_pip_size2)

# Estadísticas de transacciones por equipo
df_1_tabla = fn.f_estadisticas_ba2(tabla,fn.f_columnas_pips, fn.f_pip_size2)

# Evolución de capital
evo_capital_data1=fn.f_evolucion_capital(data1)
evo_capital_data2=fn.f_evolucion_capital(data2)
evo_capital_data2=fn.f_evolucion_capital(data3)

#Metricas de Atribución al desempeño

Me_atri_desem1= fn.f_estadisticas_mad(data1,Prices)
Me_atri_desem2= fn.f_estadisticas_mad(data2,Prices)
Me_atri_desem3= fn.f_estadisticas_mad(data3,Prices)


from django.shortcuts import render, HttpResponse, redirect
from django.contrib import messages
from .models import *

def omar1(request):
    all_mod = Show.objects.all()
    context ={
        'all_mod': all_mod

    }
    return render(request,"abdullah.html",context)

def create(request):
    errors = Show.objects.create(request.POST)

    if len(errors) > 0:

        for key, value in errors.items():
            messages.error(request, value)

        return redirect('/shows/new')
    else:
        Show.objects.create(title=request.POST['title'],network=request.POST['network'],reld=request.POST['date'],desc=request.POST['desc'])
        x = Show.objects.last()
        context ={
            'x': x
        }
        return redirect("/shows/" + str(x.id))

def omar(request):
    x = Show.objects.last()
    context ={
        'x': x
    }
    return render(request,"omar.html",context)

def show(request,id):
    i = Show.objects.get(id =id)
    context={
        'id':id,
        'i':i,
    }
    return render(request,"abdullah1.html",context)

def delete(request,id):
    d = Show.objects.get(id=id)
    d.delete()
    return redirect('/shows')

def edit(request,id):
    context={
        'id':id,
        'all':Show.objects.get(id=id),
    }

    return render(request,"omar1.html",context)

def update(request,id):
    errors = Show.objects.edit(request.POST)
    if len(errors) > 0:

        for key, value in errors.items():
            messages.error(request, value)

        return redirect('/shows/'+str(id)+ "/edit")
    else:
        y =id
        c = Show.objects.get(id=id)
        c.title = request.POST['title']
        c.network = request.POST['network']
        c.reld = request.POST['date']
        c.desc = request.POST['desc']
        c.save()
        return redirect(f"/shows/{y}")


import tensorflow.compat.v1 as tf
import numpy as np
import os

class NutritionTableDetector:
    def __init__(self):
        basedir = os.path.dirname(os.path.abspath(__file__))
        model_path = os.path.join(basedir, 'lib', 'models', 'frozen_inference_graph.pb')
        self.detection_graph = tf.Graph()
        with self.detection_graph.as_default():
            od_graph_def = tf.GraphDef()
            with tf.gfile.GFile(model_path, 'rb') as fid:
                serialized_graph = fid.read()
                od_graph_def.ParseFromString(serialized_graph)
                tf.import_graph_def(od_graph_def, name='')
            self.image_tensor = self.detection_graph.get_tensor_by_name('image_tensor:0')
            self.d_boxes = self.detection_graph.get_tensor_by_name('detection_boxes:0')
            self.d_scores = self.detection_graph.get_tensor_by_name('detection_scores:0')
            self.d_classes = self.detection_graph.get_tensor_by_name('detection_classes:0')
            self.num_d = self.detection_graph.get_tensor_by_name('num_detections:0')
        self.sess = tf.Session(graph=self.detection_graph)

    def get_classification(self, img):
        # Bounding Box Detection.
        with self.detection_graph.as_default():
            # Expand dimension since the model expects image to have shape [1, None, None, 3].
            img_expanded = np.expand_dims(img, axis=0)
            (boxes, scores, classes, num) = self.sess.run(
                [self.d_boxes, self.d_scores, self.d_classes, self.num_d],
                feed_dict={self.image_tensor: img_expanded})
        return boxes, scores, classes, num


from unittest import TestCase, mock

from case_management_api.extensions import db
from case_management_api.main import app
from case_management_api.models import Restriction

# Test data
restriction1 = Restriction('BX102', 'CBCR', 'RESTRICTION: No disposition of the registered estate by the proprietor \
                            of the registered estate is to be registered without a written consent signed by the \
                            proprietor for the time being of the Charge dated *CD* in favour of *CP* \
                            referred to in the Charges Register.')
restriction2 = Restriction('BCL03', 'OJPR', 'RESTRICTION: No disposition by a sole proprietor of the registered \
                            estate (except a trust corporation) under which capital money arises is to be registered \
                            unless authorised by an order of the court.')

# Tests the Restriction endpoints
class TestRestriction(TestCase):

    def setUp(self):
        """Sets up the tests."""
        self.app = app.test_client()

    @mock.patch.object(db.Model, 'query')
    def test_001_get_restrictions(self, mock_db_query):
        """Gets a list of all restrictions."""
        mock_db_query.all.return_value = [restriction1, restriction2]

        response = self.app.get('/v1/restrictions', headers={'accept': 'application/json'})

        print(response.get_data().decode())

        self.assertEqual(response.status_code, 200)
        self.assertEqual(len(response.json), 2)

    @mock.patch.object(db.Model, 'query')
    def test_002_get_restrictions_by_type(self, mock_db_query):
        """Gets a list of restrictions with the type specified."""
        mock_db_query.filter_by.return_value.all.return_value = [restriction1]

        response = self.app.get('/v1/restrictions?type=BCL03', headers={'accept': 'application/json'})

        print(response.get_data().decode())

        self.assertEqual(response.status_code, 200)
        self.assertEqual(len(response.json), 1)

    @mock.patch.object(db.Model, 'query')
    def test_003_get_restriction(self, mock_db_query):
        """Gets a specific restriction by its id."""
        mock_db_query.get.return_value = restriction1

        response = self.app.get(
            '/v1/restrictions/' + restriction1.restriction_type,
            headers={'accept': 'application/json'}
        )

        print(response.get_data().decode())

        self.assertEqual(response.status_code, 200)
        self.assertEqual(response.json['restriction_type'], 'CBCR')

    @mock.patch.object(db.Model, 'query')
    def test_004_get_restriction_not_found(self, mock_db_query):
        """The given restriction id does not exist."""
        mock_db_query.get.return_value = None

        response = self.app.get('/v1/restrictions/ABCDEFG', headers={'accept': 'application/json'})

        print(response.get_data().decode())

        self.assertEqual(response.status_code, 404)
        self.assertIn('Restriction not found', response.get_data().decode())


def populateLastLarger(n, heights):
    st = []
    ending = []
    for i in range(n):
        ending.append(i)

    for h in range(n):
        if len(st) > 0 and heights[h] < heights[st[-1]]:
            while len(st) > 0 and heights[h] < heights[st[-1]]:
                ending[st[-1]] = h - 1
                st.pop()
        st.append(h)
    x = st[-1]
    st.pop()
    while len(st) > 0:
        ending[st[-1]] = x
        st.pop()
    return ending

def largestRectangleArea(heights):
    n = len(heights)
    nr = populateLastLarger(n, heights)
    htsrev = heights[::-1]
    nl = populateLastLarger(n, htsrev)
    nl.reverse()
    for i in range(n):
        nl[i] = n - nl[i] - 1
    maxa = 0
    for i in range(n):
        maxa = max(maxa, (heights[i] * (nr[i] - i + 1)) + (heights[i] * (i - nl[i] + 1)) - heights[i])
    return maxa

print(largestRectangleArea([2, 1, 5, 6, 2, 3]))


import tensorflow as tf
import numpy as np
import os
import matplotlib.pyplot as plt
import cv2
import random

def classification_rate(Y, T):
    return Y == T

data_dir = os.path.join(os.getcwd(), 'dataset')

labels=['daisy', 'dandelion', 'rose', 'sunflower', 'tulip']

dataset = []
for dir in os.listdir(data_dir):
    for img in os.listdir(os.path.join(data_dir, dir)):
        img_array = cv2.imread(os.path.join(data_dir,dir,img))
        img_array = cv2.resize(img_array, (200, 200))
        img_array = img_array/255.0
        # img_array = tf.keras.preprocessing.image.random_shear(img_array, intensity=0.2)
        # img_array = tf.keras.preprocessing.image.apply_channel_shift(img_array, intensity=0.4)
        dataset.append([img_array, labels.index(dir)])
    # print(dir)

# for i in range(100):
#     print(dataset[0][0][0][i])

# print(dataset[0])

plt.figure(figsize=(5, 5))
plt.imshow(dataset[0][0])
plt.show()

random.shuffle(dataset)

Xtrain = list(image[0] for image in dataset[:-int(0.4*len(dataset))])
Ytrain = list(image[1] for image in dataset[:-int(0.4*len(dataset))])
Xval = list(image[0] for image in dataset[-int(0.4*len(dataset)):-int(0.2*len(dataset))])
Yval = list(image[1] for image in dataset[-int(0.4*len(dataset)):-int(0.2*len(dataset))])
Xtest = list(image[0] for image in dataset[-int(0.2*len(dataset)):])
Ytest = list(image[1] for image in dataset[-int(0.2*len(dataset)):])

Xtrain = np.array(Xtrain)
Ytrain = np.array(Ytrain)
Xval = np.array(Xval)
Yval = np.array(Yval)
Xtest = np.array(Xtest)
Ytest = np.array(Ytest)

# print(Ytest.shape)
# print(Ytest[1])

# print(Xtrain.shape)
# print(Xval.shape)
# print(set(Ytrain))
model = tf.keras.Sequential()

model.add(tf.keras.layers.Conv2D(32,
                                 (3, 3),
                                 activation='relu',
                                 input_shape=(200, 200, 3)))
model.add(tf.keras.layers.MaxPool2D((2,2)))
model.add(tf.keras.layers.Conv2D(256, 3, activation='relu'))
model.add(tf.keras.layers.MaxPool2D())
model.add(tf.keras.layers.Conv2D(512, 3, activation='relu'))
model.add(tf.keras.layers.MaxPool2D((2, 2)))
model.add(tf.keras.layers.Conv2D(128, 3, activation='relu'))
model.add(tf.keras.layers.MaxPool2D((2, 2)))
model.add(tf.keras.layers.Flatten())
model.add(tf.keras.layers.Dense(128))
model.add(tf.keras.layers.Dense(64))
model.add(tf.keras.layers.Dense(units=5))

model.compile(optimizer=tf.keras.optimizers.Adam(),
              loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
              metrics=['accuracy'])

early_stopping = tf.keras.callbacks.EarlyStopping(monitor='accuracy',
                                                  min_delta=0.01,
                                                  patience=3,
                                                  verbose=1)

model_checkpoint = tf.keras.callbacks.ModelCheckpoint(filepath='model/model_for_blob.h5',
                                                      monitor='accuracy',
                                                      verbose=1,
                                                      save_best_only=True)

cb = [early_stopping, model_checkpoint]

print('starting training')
model.fit(Xtrain, Ytrain, batch_size=64, epochs=2, verbose=2, callbacks=cb, validation_data=(Xval, Yval))
print('finished')
# model.history

correct = []
# print(Xtest[0])
for i in range(Xtest.shape[0]):
    # print(Xtest[i])
    img_array = Xtest[i].reshape((1, 200, 200, 3))
    pred = np.argmax(model.predict(img_array))
    correct.append(classification_rate(pred, Ytest[i]))

print(f'classification rate: {np.mean(correct)}')


from matplotlib.pyplot import plot, legend, show, xlabel, ylabel, title
from numpy import dot, transpose
import numpy
from numpy.linalg import pinv
from pandas import read_csv, get_dummies

file_name = 'kc_house_data.csv'

def pre_processing(filename):
    """
    :param filename: path to the data to analyze
    :return: processed data
    """
    data = read_csv(file_name)
    data.dropna(inplace=True)
    remove_index = []
    for i, row in data.iterrows():
        if row['price'] < 1 or row['sqft_lot15'] < 1:
            remove_index.append(i)
    data.drop(["id", "date"], axis=1, inplace=True)
    data.drop(remove_index, inplace=True)
    data = get_dummies(data, columns=['zipcode'])
    noise = [1 for i in range(len(data))]
    data.insert(loc=0, column="noise", value=noise)
    return data

def error_calculation(original, predicted):
    return numpy.array((original - predicted)**2).mean()

def house_pricing_model():
    processed_data = pre_processing(file_name)
    learn_error = []
    test_error = []
    for i in range(1, 100):
        percent_of_learning_sample = i/100
        learn_data = processed_data.sample(frac=percent_of_learning_sample)
        test_data = processed_data.drop(learn_data.index)

        learn_prices = learn_data['price']
        test_prices = test_data['price']

        learn_data.drop(['price'], 1, inplace=True)
        test_data.drop(['price'], 1, inplace=True)

        x_dagger = pinv(transpose(learn_data.as_matrix()))
        w_hat = dot(transpose(x_dagger), learn_prices)

        learn_data = learn_data.as_matrix()
        test_data = test_data.as_matrix()

        learn_result = dot(learn_data, w_hat)

        learn_error.append(error_calculation(learn_prices, learn_result))
        test_result = dot(test_data, w_hat)
        test_error.append(error_calculation(test_prices, test_result))
    # graph_plotting(learn_error, test_error)
    index = [i for i in range(1, 100)]
    plot(index, learn_error, label="learn error")
    plot(index, test_error, label="test error")
    xlabel("percentage devoted to training")
    ylabel("error value")
    title("House pricing model - error values")
    legend()
    show()

if __name__ == '__main__':
    house_pricing_model()


import copy
def deger_al(oyun_harita):
    #kurala uygun input alınmasını sağlar
    while True:
        inputs = input("Satır (1-{}), sütun (1-{}) ve işlem (N/D/B)  giriniz: ".format(len(oyun_harita),len(oyun_harita))).split()
        if len(inputs)!=3 or \
                (not (0 <= int(inputs[0]) <= len(oyun_harita) and 0 <= int(inputs[1]) <= len(oyun_harita))) or \
                (inputs[2] != "B" and inputs[2] != "D" and inputs[2] != "N"):
            print("Hatalı giriş...")
            continue
        return [int(inputs[0]),int(inputs[1]),inputs[2]]
def harita_goster(harita,durum):
    # harita listesi sayıları , durum listesi N-B-D karakterlerini tutar.
    #bu listelere göre matris oluşturulur.
    sutun_sayisi=len(harita)
    print(" \t ",end="")
    for i in range(sutun_sayisi):
        print(str((i+1))+"  ",end="")
    print("\n")
    for satir in range(sutun_sayisi):
        print(str(satir+1)+"\t",end="")
        for sutun in range(sutun_sayisi):
            durum_bilgisi=durum[satir][sutun]
            if durum_bilgisi=="N":
                print("-{}-".format(harita[satir][sutun]), end="")
            elif durum_bilgisi == "D":
                print("({})".format(harita[satir][sutun]), end="")
            else:
                print("-X-", end="")
        print("")
def harita_degistir(data,durum_bilgileri):
    durum_bilgileri[data[0] - 1][data[1] - 1] = data[2]
def kural_kontrol(bilgi_liste):
    #listedeki bütün B (X) karakterlerinin komşularını kontrol eder
    length=len(bilgi_liste)
    for i in range(length):
        for j in range(length):
            if bilgi_liste[i][j] == 'B':
                if i < 1:
                    pass
                elif bilgi_liste[i - 1][j] == 'B':
                    return False
                if i > length - 2:
                    pass
                elif bilgi_liste[i + 1][j] == 'B':
                    return False
                if  j < 1:
                    pass
                elif bilgi_liste[i][j - 1] == 'B':
                    return False
                if j > length - 2:
                    pass
                elif bilgi_liste[i][j + 1] == 'B':
                    return False
    return True
def kazandi_mi(harita,durum):
    #her sayının sağından ve aşağısından matris sonuna kadar ilerleyip, çakışma olup olmadığını kontrol eder.
    length=len(harita)
    for i in range(length):
        for j in range(length):
            for k in range(length):
                if durum[i][j]=='B':
                    break
                if j + k > length - 2 or durum[i][j + k + 1] == 'B':
                    pass
                elif harita[i][j] == harita[i][j + k + 1]:
                    return False
                if i + k > length - 2 or durum[i + k + 1][j] == 'B':
                    pass
                elif harita[i][j] == harita[i + k + 1][j]:
                    return False
    # çakışma yoksa oyunun kurallara uygun şekilde bitirilip bitirilmediğini kontrol eder.
    if not kural_kontrol(durum):
        return False
    if ada_var_mi(copy.deepcopy(durum)):
        return False
    return True
def ada_var_mi(liste):
    #sayıların 2 gruba veya daha fazlasına bölünüp bölünmediğini kontrol eder
    def virus():
        #listedeki bir adet N veya D karakterini V'ye çevirir.
        for i in range(len(liste)):
            for j in range(len(liste)):
                if liste[i][j]!='B' :
                    liste[i][j]='V'
                    return liste
    def mitoz(liste):
        #her V karakteri,B olmayan (N-D) dört komşusunu da V ye çevirir.
        #dönüştürme yapılamadığında döngü sonlanır
        bolunme=1
        while bolunme>0:
            bolunme=0
            for i in range(len(liste)):
                for j in range(len(liste)):
                    if liste[i][j]=='V':
                        if i+1<=len(liste)-1:
                            if liste[i+1][j]=='D' or liste[i+1][j]=='N':
                                bolunme += 1
                                liste[i+1][j] = 'V'
                        if i-1>=0:
                            if liste[i-1][j] == 'D' or liste[i-1][j] == 'N':
                                bolunme += 1
                                liste[i-1][j] = 'V'
                        if j+1<=len(liste)-1:
                            if liste[i][j+1] == 'D' or liste[i][j+1] == 'N':
                                bolunme += 1
                                liste[i][j+1] = 'V'
                        if j-1>=0:
                            if liste[i][j-1] == 'D' or liste[i][j-1] == 'N':
                                bolunme+=1
                                liste[i][j-1] = 'V'
    mitoz(virus())
    # listede B veya D karakteri kalmışsa, sayılar arasında bağlantı yoktur.
    for i in range(len(liste)):
        for j in range(len(liste)):
            if liste[i][j]=='D' or liste[i][j]=='N':
                return True
    return False
def set_up():
    # oyunun çalıştığı ana fonksiyondur.
    try:
        # txt dosyasından okunan veriler ile sayıları 2 boyutlu listede kayıt eder.
        with open("hitori_bulmaca.txt","r") as dosya:
            oyun_harita=[]
            for satir in dosya:
                oyun_harita.append(satir.split())
            kare_kenar=len(oyun_harita)
            # N-D-B durumlarının tutulması için bir liste daha oluşturup, varsayılan olarak bütün elemanları N yapar.
            durum_bilgileri = []
            for i in range(kare_kenar):
                durum_bilgileri.append([])
                for j in range(kare_kenar):
                    durum_bilgileri[i].append([])
                    durum_bilgileri[i][j] = "N"
    except IOError:
        print("Oyunu oynayabilmek için txt dosyası gereklidir.")
        exit()
    harita_goster(oyun_harita,durum_bilgileri)
    while True:
        # kullanıcıdan kurallara uygun input alır , listeyi değiştirir.
        # bu göngü oyun bitene kadar devam eder.
        harita_degistir(deger_al(oyun_harita),durum_bilgileri)
        if kazandi_mi(oyun_harita,durum_bilgileri):
            break
        harita_goster(oyun_harita,durum_bilgileri)
    print("\n-x-x-x-x-x-x-x-x-x-\nTebrikler...\nOyunun son hâli:")
    harita_goster(oyun_harita,durum_bilgileri)
set_up()


import sys
import os
import getopt

def process_args(argv):
   persist = False
   move = False
   debug=False
   stopCount=10

   try:
      opts, args = getopt.getopt(argv,"hd:c:p:m:",["debug=","stopcount=","persist=","move="])
   except getopt.GetoptError:
      print( 'test.py -d <shouldDebug> -p <shouldPersistLogs> -c <count> -m <shouldMove> ')
      sys.exit(2)
   for opt, arg in opts:
      if opt == '-h':
         print( 'test.py -d <shouldDebug> -p <shouldPersistLogs> -c <count> -m <shouldMove> ')
         sys.exit()
      elif opt in ("-d", "--debug"):
         debug = bool(arg)
      elif opt in ("-c", "--stopcount"):
         stopCount = int(arg)
      elif opt in ("-p", "--persist"):
         persist = bool(arg)
      elif opt in ("-m", "--move"):
         move = bool(arg)
   print(persist ,move ,debug,stopCount)

process_args(sys.argv[1:])


import re
import openpyxl

# Open and read the header file
header_file = open('header_test.h', 'r')
lines = header_file.read()

# Use regular expressions to find function prototypes
myfuncs = re.findall(r"(((void)|(uint\w+))\s+[A-z0-9]+(\s+)?\([A-z0-9\s+\*\,]+\)\;)", lines)

func_list = []
for func in myfuncs:
    func_list.append(func[0])
unique_ids = [f'IDX{i}' for i in range(0, len(func_list))]

# Create a new Excel workbook and select the active sheet
workbook = openpyxl.Workbook()
sheet = workbook.active
data = [func_list, unique_ids]

data_transposed = list(map(list, zip(*data)))

for row in data_transposed:
    sheet.append(row)

workbook.save('sheet.xlsx')
workbook.close()


import willie
import willie.module
from willie.tools import stderr
import random
import functools
from datetime import datetime, timedelta

import os, sys
sys.path.append(os.path.dirname(__file__))

if not 'commands' in globals():
    commands = []

def getWhiteList(bot):
    result = []
    if hasattr(bot, "config") and hasattr(bot.config, "core") and hasattr(bot.config.core, "whitelist"):
        result = bot.config.core.whitelist.split(",")
    result.extend(bot.config.core.admins)
    result.append(bot.config.core.owner)
    return result

def inWhiteList(bot, nick):
    return next((n for n in getWhiteList(bot) if n.lower() == nick.lower()), False)

class Cmd:
    """ Wrapper class that stores the list of commands, main command name (assumed to be first in
        list), and function to call for the command.
    """
    def __init__(self, cmds, func, hide=False, hideAlways=False):
        self.main = cmds[0]
        self.cmds = cmds
        self.func = func
        self.hide = hide
        self.hideAlways = hideAlways

    def randCmd(self):
        return random.choice(self.cmds)

    def __str__(self):
        return self.main

def command(*args, **kwargs):
    """Decorator that just passes it on to the built-in willie command decorator, but also adds it
        to the module's command list (for !list function, etc).

        Use the "hide" keyword argument to prevent the command from being shown on when the list
        command runs (but still actually be on the list)
    """
    global commands

    def passthrough(func):

        originalFunc = func

        def cooldown(bot, trigger):
            if (hasattr(originalFunc, "lastcall")):
                if (datetime.now() - originalFunc.lastcall > timedelta(seconds=kwargs.get("cooldown"))):
                    originalFunc.lastcall = datetime.now()
                    originalFunc(bot, trigger)
                else:
                    info(bot, trigger, "See above")
            else:
                originalFunc.lastcall = datetime.now()
                originalFunc(bot, trigger)

        if ("cooldown" in kwargs):
            func = cooldown

        existingCmds = [c.main for c in commands if c.main == args[0]]
        if (len(existingCmds) == 0):
            commands.append(Cmd(args,func,kwargs.get("hide"), kwargs.get("hideAlways")))

        return willie.module.commands(*args)(func)

    return passthrough

def adminonly(func):
    """Decorator that only allows the function to run if the caller is an admin or owner.
    """
    @functools.wraps(func)
    def wrapperFunc(bot, trigger):

        if (trigger.admin or trigger.owner):
            func(bot, trigger)
        else:
            pass #Don't say anything back to the user, to avoid spam.
    return wrapperFunc

def whitelisted(func):
    """Decorator that only allows the function to run if the caller is on the whitelist.
    """
    @functools.wraps(func)
    def wrapperFunc(bot, trigger):

        if (trigger.admin or trigger.owner or inWhiteList(bot, trigger.nick)):
            func(bot, trigger)
        else:
            pass #Don't say anything back to the user, to avoid spam.
    return wrapperFunc

def adminonly_streamtime(func):
    """Decorator that only allows the function to run if the caller is an admin or owner if the
       stream is currently on.
    """
    @functools.wraps(func)
    def wrapperFunc(bot, trigger):
        import handmade_stream as stream
        streaming = stream.isCurrentlyStreaming()
        if (not streaming or trigger.admin):
            func(bot, trigger)
        else:
            pass #Don't say anything back to the user, to avoid spam.
    return wrapperFunc

def whitelisted_streamtime(func):
    """Decorator that only allows the function to run if the caller is whitelisted if the
       stream is currently on.
    """
    @functools.wraps(func)
    def wrapperFunc(bot, trigger):
        import handmade_stream as stream
        streaming = stream.isCurrentlyStreaming()
        if (not streaming or (trigger.admin or inWhiteList(bot, trigger.nick))):
            func(bot, trigger)
        else:
            pass #Don't say anything back to the user, to avoid spam.
    return wrapperFunc

def info(bot, trigger, text):
    """Handles directed informational text -- will either direct the text to @caller or to @nick
        specified in the first argument. Commands which use this method should put "Info Command"
        in their docstring (and maybe in something user-facing...!infocommands?)
    """
    ###TODO(chronister): Can this be done as a decorator? (would have to give a custom bot
    ###     or something?)

    if (trigger):
        if (trigger.group(2)):

            args = trigger.group(2).split(" ")
            if (args[0][0] == "@"):
                args[0] = args[0][1:]

            if (args[0].lower() != "cmuratori"):
                bot.say("@%s: %s" % (args[0], text))
            else:
                bot.say("@%s: Please do not direct info at Casey." % trigger.nick)
        else:
            bot.say("@%s: %s" % (trigger.nick, text))
    else:
        bot.say(text)

@adminonly_streamtime
@command('isAdmin', 'amIadmin', hide=True)
def isAdmin(bot, trigger):
    """Simple command that simply tells the user whether or not they are an admin. Mostly
        implemented for debugging (double-checking case sensitivity and things)
    """
    if (trigger):
        args = trigger.group(2)
        if (args):
            args = args.split(" ")
            admins = bot.config.core.admins
            for arg in args:
                if (admins and arg in admins):
                    bot.say("%s is an admin!" % arg)
                else:
                    bot.say("%s is not an admin." % arg)
        elif (trigger.admin):
            bot.say("%s, you are an admin!" % trigger.nick)
        else:
            bot.say("%s, you are not an admin." % trigger.nick)

@whitelisted_streamtime
@command('whitelisted', 'amiwhitelisted', 'isWhitelisted', hide=True)
def isWhitelisted(bot, trigger):
    """Simple command that simply tells the user whether or not they are whitelisted. Mostly
        implemented for debugging (double-checking case sensitivity and things)
    """
    if (trigger):
        args = trigger.group(2)
        if (args):
            args = args.split(" ")
            for arg in args:
                if (inWhiteList(bot, arg)):
                    bot.say("%s is whitelisted!" % arg)
                else:
                    bot.say("%s is not whitelisted." % arg)
        elif (inWhiteList(bot, trigger.nick)):
            bot.say("%s, you are whitelisted!" % trigger.nick)
        else:
            bot.say("%s, you are not whitelisted." % trigger.nick)

@whitelisted_streamtime
@command('alias', 'alt', hide=True)
def aliasList(bot, trigger):
    """Command that provides a list of registered aliases for the given command. Must be registered
        in the same decorator, commands that simply call the same function aren't grouped.
        Note that you must use the custom-defined @command decorator instead of the built-in
        Willie module one for this to work.
    """
    arg = trigger.group(2)
    if (arg):
        cmd = next((c for c in commands if arg in c.cmds), None)
        if (cmd):
            bot.say("Aliases of !%s: !%s" % (arg, ", !".join(cmd.cmds)))
            if (len(args) > 1):
                time.sleep(0.300)
        else:
            bot.say("No aliases found for %s!" % arg)

    else:
        bot.say("Please specify a command to list the aliases of.")

@whitelisted_streamtime
@command('help', hide=True)
def helpInfo(bot, trigger):
    info(bot, trigger, "To see a list of all commands, use !list. To see the aliases of a command, use !alias. To check when the next stream will air, use !timer or !when.")

@adminonly
@command('q&a', 'qa', hide=True, hideAlways=True)
def qaInfo(bot, trigger):
    bot.say("Q&A session. Please prefix questions w/ Q:, @cmuratori, @handmade_hero. Your question will have a higher chance of being answered if it is succinct and related to today's or a previous day's code. No need to repeat your question, as the original one will be captured by a plugin.")

@adminonly
@command('prestream', 'preqa', hide=True, hideAlways=True)
def preqaInfo(bot, trigger):
    bot.say("Prestream Q&A. Please prefix questions w/ Q:, @cmuratori, @handmade_hero. Feel free to ask about anything, keeping in mind that some questions may have already been answered in previous streams.")

@whitelisted_streamtime
@command('owner', hide=True, hideAlways=False)
def OwnerInfo(bot, trigger):
    info(bot, trigger, "My owner is %s." % bot.config.core.owner)

@whitelisted_streamtime
@command('list', 'commands', 'commandlist', 'cmds', hide=True, hideAlways=True)
def commandList(bot, trigger):
    """Command that lists all of the (non-hidden) registered commands.
        Note that you must use the custom-defined @command decorator for commands to appear here,
        not the built-in Willie module one.
    """
    global commands
    visibleCommands = [c.main for c in commands if c.hide==False]
    bot.say("Here are the common HH stream commands: !%s" % ", !".join(visibleCommands))

@whitelisted
@command('listmore', 'listhidden', hideAlways=True)
def commandExtras(bot, trigger):
    """Command that lists ALL of the registered commands.
        Note that you must use the custom-defined @command decorator for commands to appear here,
        not the built-in Willie module one.
    """
    global commands
    extraCommands = [c.main for c in commands if (c.hide!= False and c.hideAlways!=True)]
    bot.say("Here are more of the commands I know: !%s" % ", !".join(extraCommands))


"""
Message: Add remaining route
Revision ID: bd391440adb6
Revises: d58a6d99cebc
Create Date: 2021-10-06 16:59:46.318048
"""
import geoalchemy2
import sqlalchemy as sa
from alembic import op

revision = 'bd391440adb6'
down_revision = 'd58a6d99cebc'
branch_labels = None
depends_on = None

def upgrade():
    op.add_column('route', sa.Column('geom_remainder',
                                     geoalchemy2.types.Geometry(geometry_type='LINESTRING', srid=32637, spatial_index=False, from_text='ST_GeomFromEWKT', name='geometry'), nullable=True
                                     ))
    op.create_index('idx_route_geom_remainder', 'route', ['geom_remainder'], unique=False, postgresql_using='gist')

def downgrade():
    op.drop_index('idx_route_geom_remainder', table_name='route', postgresql_using='gist')
    op.drop_column('route', 'geom_remainder')


from common.dijkstra import dijkstra
from functools import cache
import re

with open('day16.txt', 'r') as f:
    text = f.read().splitlines()

# text = '''Valve AA has flow rate=0; tunnels lead to valves DD, II, BB
# Valve BB has flow rate=13; tunnels lead to valves CC, AA
# Valve CC has flow rate=2; tunnels lead to valves DD, BB
# Valve DD has flow rate=20; tunnels lead to valves CC, AA, EE
# Valve EE has flow rate=3; tunnels lead to valves FF, DD
# Valve FF has flow rate=0; tunnels lead to valves EE, GG
# Valve GG has flow rate=0; tunnels lead to valves FF, HH
# Valve HH has flow rate=22; tunnel leads to valve GG
# Valve II has flow rate=0; tunnels lead to valves AA, JJ
# Valve JJ has flow rate=21; tunnel leads to valve II'''.splitlines()

flow = {}
links = {}
for line in text:
    v, rate, leads = re.match('Valve ([A-Z]+) has flow rate=([0-9]+);.*valve[s]* (.*)', line).groups()
    flow[v] = int(rate)
    links[v] = leads.split(', ')

pos = 'AA'
smax = 26
opened = set()

nodes = {k: {d: 1 for d in v} for k, v in links.items()}
flowing = {k for k in nodes if flow[k]}

all_dist = {s: {d: dijkstra(nodes, s, d) for d in nodes} for s in nodes}

def op(s):
    return ','.join(sorted(s))

@cache
def potential(pos, left, ops):
    if sum(left)<=0:
        return 0
    opened = set(ops.split(',')) if ops else set()
    local_pot = [flow[pos[x]] * (left[x] - 1) if left[x] > 1 and pos[x] not in opened else 0 for x in range(2)]
    opened.update(pos)
    cd = [set() for _ in range(2)]
    for i in range(2):
        for dest in flowing.difference(opened):
            if all_dist[pos[i]][dest]<left[i]-1:
                cd[i].add(dest)
        if not cd[i]:
            cd[i].add(pos[i])
    gs = {(d1,d2) for d1 in cd[0] for d2 in cd[1] if d1!=d2}
    if pos==('AA','AA'):
        goto=set()
        for elem in gs:
            if (elem[1],elem[0]) not in goto:
                goto.add(elem)
    else:
        goto=gs
    if goto and any(x>2 for x in left):
        if sum(local_pot) > 0:
            left=(max(left[0]-1, 0), max(left[1]-1,0))
            return sum(local_pot) + max(potential(g, (left[0] - all_dist[pos[0]][g[0]], left[1] - all_dist[pos[1]][g[1]],), op(opened)) for g in goto)
        else:
            if pos in goto:
                return 0
            return max(potential(g, (left[0] - all_dist[pos[0]][g[0]], left[1] - all_dist[pos[1]][g[1]]), op(opened)) for g in goto)
    else:
        return sum(local_pot)

print(potential((pos,pos), (smax,smax), op(opened)))


import sys
from sys import argv

def solve(N):
	if N == 0:
		print("INSOMNIA")
		return
	count = 0
	mul = 0
	array = [0] * 10
	num = 0
	while count < 10:
		mul = mul + 1
		num = N * mul
		s = str(num)
		for c in s:
			if array[int(c)]==0:
				array[int(c)]=1;
				count = count + 1
	print(num)

T = int(input())
for i in range(0, T):
	N = int(input())
	print("Case #",i+1,": ",sep="",end="")
	solve(N)


from django.shortcuts import render
from StudentFormApp.forms import Studentform
# Create your views here.
def Studentview(request):
    form=Studentform()
    check=False
    x=0
    y=0
    z=0
    tot=0
    avg=0
    if request.method=='POST':
        form=Studentform(request.POST)
        if form.is_valid():
            x=form.cleaned_data['Marks_Maths']
            y=form.cleaned_data['Marks_Science']
            z=form.cleaned_data['Marks_Computer']
            tot=x+y+z
            avg="{:.2f}".format(tot/3)
            check=True
    return render(request,'StudentFormApp/index.html',{'form':form,'x':x ,'y':y ,'z':z ,'tot':tot,'avg':avg,'check':check})


import urllib2
import urlparse
import logging

import exifread
import os
from redis import Redis
from rq import Queue
from settings import *

def _getqueue(queue_name):
    """get Queue instance with given name"""
    redis_conn = _getrediscon()
    return Queue(queue_name, connection=redis_conn)

def _getfilepath(key):
    return os.path.join(download_dir, key)

def _getrediscon():
    return Redis(host=db_host, port=db_port, db=0)

def download_image(key):
    """Download the image identified by the key and store it in local dir"""
    image_url = urlparse.urljoin(bucket_url, key)
    file_path = _getfilepath(key)

    if not (DEBUG and os.path.exists(file_path)):
        logging.info("image download started" + key)
        response = urllib2.urlopen(image_url)
        CHUNK = 1024
        with open(file_path, 'wb') as f:
            while True:
                chunk = response.read(CHUNK)
                if not chunk:
                    break
                f.write(chunk)

        logging.info("image download finished" + key)

        f.close()
    q = _getqueue(read_exif_queue)
    q.enqueue(extract_exif_info, key)

    return file_path

def extract_exif_info(key):
    """Extract Exif info from the downloaded file"""
    logging.info("extracing tags :: " + key)
    file_path = os.path.join(download_dir, key)
    f = open(file_path, 'rb')
    tags = exifread.process_file(f)
    logging.info("extraced tags :: " + key)
    q = _getqueue(store_data_in_db_queue)

    q.enqueue(store_tags_in_db, key, tags)

    return tags

def store_tags_in_db(key, tags):
    """save Exif tags in db """
    logging.info("storing tags in db :: " + key)
    redis_conn = _getrediscon()
    if tags:
        redis_conn.hmset(key, tags)
    # if DEBUG:
    #     print(redis_conn.hgetall(key))
    logging.info("store tags in db complete :: " + key)

def cleanup_image(key):
    file_path = _getfilepath(key)
    if not DEBUG and os.path.exists(file_path):
        os.remove(file_path)


import streamlit as st
import pandas as pd
import plotly.express as px
import numpy as np
from PIL import Image
import pickle
import spacy

#Name of App & pages
st.set_page_config(page_title="Sentiment Analyzer", layout="wide")
st.title("Sentiment Analyzer")
data_overview, model_making, model = st.tabs(["Problem & Data Overview","Model Creation", "Model Overview & Testing"])

with data_overview:
    #Creating Problem subheader with problem description and big story chart
    st.subheader("Problem")
    "Physicians are looking for a model to predict the sentiment of patient drug prescription reviews."
    image = Image.open('Picture1.jpg')
    st.image(image, caption='Big Story Chart for Drug Prescription Sentiment Analysis Model')
    # getting data from your firestore database - reddit collection
    st.subheader("Data Overview")
    "The dataset provides patient reviews on various prescription drugs related to different health conditions. Ratings are available based on overall satisfaction"
    #Creating Data Display of ratings
    df = pd.read_csv("train (1).csv")
    st.subheader("Sample Posts")
    st.write(df.head(5))
    #Creating Histogram of ratings
    fig = px.histogram(df, x='rating', nbins=5)
    st.plotly_chart(fig)
    st.caption("number of reviews per rating")

    st.write("The majority of posts have a rating of 6 or higher, so the dataset is imbalanced!")
    #Explaining cleaning data
    st.subheader("Cleaning Data")
    st.write("To clean data, I labeled data with a rating of 4 and lower as negative and the rest as positive using a defined function.")
    rating = st.selectbox("Select a rating", ["1", "2","3", "4", "5", "6", "7", "8", "9", "10"])
    if rating == ["1", "2", "3", "4"]:
        st.writes("The rating is negative.")
    else:
        st.write("The rating is positive.")
    st.write("Determining whether the rating is positive or negative helps train and validate the model by creating a binary prediction!")
    st.subheader("Splitting data into testing and training")
    st.write("data was split into two datasets for training and testing. 70% of the data was used for training, while 30% were used for testing.")
    st.write("I used an entirely different set of data to test the model. The data was structured the same way, but didn't need to be split up into two seperate sets")
with model_making:
    st.subheader("Models Tested")
    st.write("The model I used consisted of a pipeline that had two parts: 1) TfidfVectorizer, 2)LinearSVC")
    st.write("To tune my model, I tested five tokenizers in my pipeline. Click below to see descriptions.")
    #Downloading images of code
    tokenizer1 = Image.open("tokenizer1.jpg")
    tokenizer2 = Image.open("tokenizer2.jpg")
    tokenizer3 = Image.open("tokenizer3.jpg")
    tokenizer4 = Image.open("tokenizer4.jpg")
    tokenizer5 = Image.open("tokenizer5.jpg")
    #Writing selectbox to display different tokenizer descriptions
    tokenizer_desc = st.selectbox("Select a tokenizer", ["Tokenizer 1", "Tokenizer 2", "Tokenizer 3", "Tokenizer 4", "Tokenizer 5"])
    if tokenizer_desc == "Tokenizer 1":
        st.write("The first tokenizer was a replica of the spacy tokenizer used in class.")
        st.image(tokenizer1)
    elif tokenizer_desc == "Tokenizer 2":
        st.write("The second tokenizer was a basic spacey tokenizer that defined tokens.")
        st.image(tokenizer2)
    elif tokenizer_desc == "Tokenizer 3":
        st.write("The third model incorporated POS.")
        st.image(tokenizer3)
    elif tokenizer_desc == "Tokenizer 4":
        st.write("The fourth model includes POS and STOP_words.")
        st.image(tokenizer4)
    else:
        st.write("The fifth model uses POS, STOP_words, and Lexicons.")
        st.image(tokenizer5)

with model:
    st.subheader("Model Performance by tokenizer")
    #writing table to display accuracy and F1 scores
    st.write("Each model provided different accuracy and F1 scores for testing data and training data")
    st.write("Train & Test Dataset Scores")
    scores = {
    'Tokenizer': ['1', '2', '3', '4', '5'],
    'Train Accuracy Score': [77.63, 77.20, 76.23, 78.06, 76.989],
    'Train F1 Score': [86.85, 86.63, 86.03, 87.10, 86.63],
    'Test Accuracy Score': [88.99, 90.15, 88.41, 88.41, 87.54],
    'Test F1 Score': [94.17, 94.82, 93.85, 93.85, 93.36]
    }
    st.table(scores)
    st.write("Based on the testing scores, I chose tokenizer 2 as the most effective model!")
    st.subheader("Our Sentiment Model: Tokenizer 2")
    col1, col2 = st.columns(2)
    col1.metric("Accuracy", "90.15%")
    col2.metric("F1 Score", "94.82")

    st.subheader("Test out the model")
    #load trained model
    nlp = spacy.load('en_core_web_sm')

    def spacy_tokenizer(doc):
      tokens = nlp(doc)
      return [token.text for token in tokens if not token.is_stop and not token.is_punct and not token.is_space]
    with open('pipeline (1).pkl','rb') as f:
      model = pickle.load(f)
    #create a form for the user to input data
    st.subheader("Review Forum")
    benefits_review = st.text_area("benefits_review")
    side_effects_review = st.text_area("side_effects_review")
    comments_review = st.text_area("comments_review")

    if st.button("Predict Sentiment"):
        input_data = ["benefits_review", "side_effects_review", "comments_review"]
        prediction = model.predict(input_data)
        st.write("Model Predicts:", prediction[0])


# -*- coding: utf-8 -*-
import scrapy
from scrapy import Request
import re
import time
import requests
from bs4 import BeautifulSoup
from scrapy import Selector
from scrapy_splash import SplashRequest
# from yuqing100.items import Yuqing_QiushiItem
# from yuqing100.pipelines import Panduan_Qiushi

lua1 = '''
function main(splash)
    splash:go(splash.args.url)
    splash:wait(5)
    splash:runjs("document.getElementsByClassName('foot')[0].scrollIntoView(true)")
    splash:wait(5)
    return splash:html()
    end
 '''

class PiankeSpider(scrapy.Spider):
    name = 'Pianke_spider'
    start_urls = [
        'http://pianke.me/pages/read/readNote.html'
    ]
    headers = {
        "Host": "pianke.me"
    }

    def start_requests(self):
        for url in self.start_urls:
            yield Request(url,
                          callback=self.parse_url,
                          dont_filter=True )

    def parse_url(self, response):
        url = response.url
        yield SplashRequest(url, endpoint='execute',
                            args={'lua_source': lua1,'headers': self.headers},
                            cache_args=['lua_source'])

    def parse(self, response):
        sele = Selector(response)
        urls = sele.xpath('//div[@class="article-title"]/a/@href').extract()
        # urls = set()
        # panduan = Panduan_Qiushi()
        # db_url = panduan.panduan()
        for url in urls:
            print(url)
            url = 'http://pianke.me/pages/read/' + url.replace('./','')
            # url = div.xpath('.//a/@href').extract()[0]
            # print(date_time)
            # print(author)
            # print(url)
            # if url not in urls:
            yield SplashRequest(url=url,
                                callback=self.parse1,
                                # meta={'date_time': date_time,"author":author},
                                # args={'headers': self.headers, 'wait': 2},
                                encoding='utf-8')

    def parse1(self, response):
        sele = Selector(response)
        title = sele.xpath('//title/text()').extract_first()
        if title:
            # 文章正文内容
            Content = ''
            Contents = sele.xpath('//div[@class="typo container"]//text()').extract()
            for body in Contents:
                Content = Content + str(body)
            item = {
                'AuthorID': sele.xpath('//div[@class="article-others"]/a/@href').extract_first().split('uid=',1)[1],
                'AuthorName': sele.xpath('//div[@class="article-others"]/a/text()').extract_first(),
                'ArticleTitle': title,
                'SourceArticleURL': response.url,
                'URL': response.url,
                'PublishTime': sele.xpath('//div[@class="article-others"]/span/text()').extract_first(),
                'Crawler': time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()),
                'ReadCount': '',
                'CommentCount': '',
                'TransmitCount': '',
                'Content': Content,
                'comments': '',
                'AgreeCount': sele.xpath('//div[@class="article-others"]/span/text()').extract_first(),
                'DisagreeCount': '',
                'AskCount': '',
                'ParticipateCount': '',
                'CollectionCount': '',
                'Classification': '',
                'Labels': '',
                'Type': '',
                'RewardCount': ''
            }

            # yield item
            print(item)


#!/usr/bin/env python
import math
import os
import sys
import mysql.connector
import re

serverName = "localhost"
database = "court_documents"
directory = './SearchEngine/Documents-Search-Engine/data/RiderHaggard'
numberDocument = 0

def sort_by_score(a):
    return a[1]

def calculateIDF(listOccurences, dictionary, listFrequency, ids, pdfPath, docType, pdfName):
    position = 0
    listObjects = []
    scores = ''

    for document in ids:
        cumulative = 0
        for i in range(len(dictionary)):
            idf = math.log10(len(ids) / listOccurences[i])
            tfidf = listFrequency[position][i] * idf
            cumulative += tfidf

        # Checking if its the last iteration of the array
        if position == len(ids) - 1:
            scores += '{"id": ' + str(document) + ', "score": ' + str(cumulative) + ', "pdfPath":"' + pdfPath[position] + '", "docType":"' + str(docType[position]) + '", "pdfName":"' + str(pdfName[position]) + '"}'
            listObjects.append([document, cumulative, pdfPath[position]])
            continue
        scores += '{"id": ' + str(document) + ', "score": ' + str(cumulative) + ', "pdfPath":"' + pdfPath[position] + '", "docType":"' + str(docType[position]) + '", "pdfName":"' + str(pdfName[position]) + '"}, '
        listObjects.append([document, cumulative, pdfPath[position]])
        position += 1
        cumulative = 0

    #return sorted(listObjects, key=sort_by_score, reverse=True)
    return "[" + scores + "]"

def calculateWordOccurence(dictionary, sentences):
    wordOccurence = [0] * len(dictionary)
    position = 0
    # Loop through the documents
    for sentence in sentences:
        for word in dictionary:
            # Convert sentence to array of words
            wordList = re.findall('\w+', sentence)
            occurences = wordList.count(word)

            # Adding Frequencies to a list, incrementing occurences
            wordOccurence[position] += occurences
            position += 1
        position = 0

    return wordOccurence

def getDocuments(search):
    mydb = mysql.connector.connect(
            host="localhost",
            user="root",
            passwd="",
            database = database
    )

    query = ''

    # Loop through the words the user provided
    count = 0
    search = re.findall('\w+', search[0])

    for word in search:
        if count == len(search) - 1:
            query += '`metadata` LIKE "%' + word + '%"'
            count += 1
            continue
        #query += ' AND `documents`.`metadata` LIKE "%' + word + '%"'
        query += '`metadata` LIKE "%' + word + '%" AND'
        count += 1

    mycursor = mydb.cursor()
    mycursor.execute("SELECT `term_frequency`.`id`, `document_id`, `word`, `term_frequency`, `documents`.`metadata`, `documents`.`PDF_Path`, `documents`.`doc_category_id`, `documents`.`PDF` FROM `term_frequency` LEFT JOIN `documents`ON `documents`.`ID` = `term_frequency`.`document_id` LEFT JOIN `document_category` ON `document_category`.`category_id` = `documents`.`doc_category_id` WHERE " + query)

    myresult = mycursor.fetchall()

    ids = []
    dictionary = []
    tf = []
    sentences = []
    pdfPath = []
    docType = []
    pdfName = []

    # Looping through the rows returned
    for row in myresult:
        ids.append(row[1])
        dictionary.append(row[2])
        tf.append({"id": row[1], "frequency": row[3], "word": row[2]})
        sentences.append(row[4])
        pdfPath.append(row[5])
        docType.append(row[6])
        pdfName.append(row[7])
    # Build correct formats
    dictionary = list(dict.fromkeys(dictionary))
    ids = list(dict.fromkeys(ids))
    sentences = list(dict.fromkeys(sentences))
    listFrequency = [[0.0] * len(dictionary) for _ in range(len(ids))]

    for obj in tf:
        idIndex = ids.index(obj["id"])
        dictionaryIndex = dictionary.index(obj['word'])
        listFrequency[idIndex][dictionaryIndex] = obj['frequency']

    return [dictionary, ids, sentences, listFrequency, pdfPath, docType, pdfName]

def main(args):
    # Get documents by the search value

    result = getDocuments(args)
    wordOccurence = calculateWordOccurence(result[0], result[2])
    scores = calculateIDF(wordOccurence, result[0], result[3], result[1], result[4], result[5], result[6])
    print(scores)

if __name__ == '__main__':
    args = sys.argv[1:]
    result = main(args)


import cv2
import time
import os
import HandTracking as htm

wCam, hCam = 640, 480

cap = cv2.VideoCapture(0)
cap.set(3, wCam)
cap.set(4, hCam)

folderPath = 'C:/Users/Michele/Documents/FingerImages'
myList = os.listdir(folderPath)
print(myList)
overlayList = []
for imgPath in myList:
    image = cv2.imread(f'{folderPath}/{imgPath}')
    overlayList.append(image)

detector = htm.handDetector(detectionCon=0.75)

tipIds = [4, 8, 12, 16, 20]

while True:
    success, img = cap.read()
    img = detector.findHands(img)
    lmList = detector.findPosition(img, draw=False)
    # print("ciao Michelino, da Gioietta")
    if len(lmList) != 0:
        fingers = []
        if lmList[4][1] > lmList[3][1]:
            fingers.append(1)
        else:
            fingers.append(0)
        if lmList[8][2] < lmList[6][2]:
            fingers.append(1)
        else:
            fingers.append(0)
        for id in range(2,4):
            if lmList[tipIds[id]][2] < lmList[tipIds[id-2]][2]:
                fingers.append(1)
            else:
                fingers.append(0)
        if lmList[20][2] < lmList[18][2]:
            fingers.append(1)
        else:
            fingers.append(0)

        totalFingers = fingers.count(1)
        print(totalFingers)

        h, w, c = overlayList[totalFingers].shape
        img[50:h+50, 50:w+50] = overlayList[totalFingers]
       # cv2.rectangle(img, (20, 225), (130, 325), (0, 255, 0), cv2.FILLED)
        cv2.putText(img, str(totalFingers), (25, 275), cv2.FONT_HERSHEY_PLAIN,
                    10, (0, 0, 0), 10)

    cv2.imshow("Image", img)
    cv2.waitKey(1)