import logging
import os
import shutil
from pathlib import Path

from PIL import Image

# import statistics
from scipy import spatial
from tqdm import tqdm

from ..models.slide_classifier import inference
from .helpers import make_dir_if_not_exist

logger = logging.getLogger(__name__)

class SegmentCluster:
    """Iterates through frames in order and splits based on large visual differences
    (measured by the cosine difference between the feature vectors from the slide classifier)"""

    def __init__(self, slides_dir, model_path="model_best.ckpt"):
        self.slides_dir = Path(slides_dir)
        self.slides_list = sorted(os.listdir(self.slides_dir))
        self.model = inference.load_model(model_path)
        self.change_indexes = None

    def extract_and_add_features(self, gamma=1.08):
        """Extracts features from the images in ``slides_dir`` and saves feature vectors"""
        num_slides = len(self.slides_list)

        all_features = []
        logger.info("Extracting features from " + str(num_slides) + " slides")
        for idx, slide in tqdm(
            enumerate(self.slides_list),
            total=num_slides,
            desc="> AI Segmenting Engine: Feature extraction",
        ):
            current_slide_path = os.path.join(self.slides_dir, slide)
            _, _, _, extracted_features = inference.get_prediction(
                self.model,
                Image.open(current_slide_path),
            )
            all_features.append(extracted_features)

        # spatial.distance.cosine computes the cosine difference, a larger value means more different
        # and a smaller value means more similar
        similarities = [
            spatial.distance.cosine(all_features[i - 1], all_features[i])
            for i in range(1, len(all_features))
        ]
        mean_similarities = sum(similarities) / len(similarities)
        # std_similarities = statistics.pstdev(similarities)

        # A larger `sim_compare_value` = less segments
        # A larger divisor = more segments
        sim_compare_value = mean_similarities * gamma

        change_indexes = [
            i
            for i in range(0, len(similarities))
            if similarities[i] > sim_compare_value
        ]

        self.change_indexes = change_indexes
        return change_indexes

    def transfer_to_filesystem(self, copy=True, create_best_samples_folder=True):
        """Takes all images in directory ``slides_dir`` and saves each cluster to a subfolder in ``cluster_dir`` (directory in parent of ``slides_dir``)"""
        cluster_dir = (
            self.slides_dir.parents[0] / "slide_clusters"
        )  # cluster_dir = up one directory from slides_dir then into "slide_clusters"

        if create_best_samples_folder:
            best_samples_path = cluster_dir / "best_samples"
            make_dir_if_not_exist(best_samples_path)

        current_folder = 0
        current_cluster_path = cluster_dir / str(current_folder)
        for idx, slide in enumerate(self.slides_list):
            current_slide_path = os.path.join(self.slides_dir, slide)
            if idx in self.change_indexes:
                current_folder += 1
                if create_best_samples_folder:
                    if copy:
                        shutil.copy(str(current_slide_path), str(best_samples_path))
                    else:
                        shutil.move(str(current_slide_path), str(best_samples_path))
            current_cluster_path = cluster_dir / str(current_folder)

            make_dir_if_not_exist(current_cluster_path)

            if copy:
                shutil.copy(str(current_slide_path), str(current_cluster_path))
            else:
                shutil.move(str(current_slide_path), str(current_cluster_path))

        return cluster_dir, best_samples_path

# seg = SegmentCluster("slide")
# seg.extract_and_add_features()
# seg.transfer_to_filesystem()

# TODO
# 1. Matrix of similarity/difference scores to remove the most similar images (in the case that the presenter switches back and forth between two things, thus producing duplicates)
# 2. Test if model features for same slide as screen capture vs camera are the same
# 3. If in the above step the model features are difference then change the dataset so that all screen captures are in the slides class and all cameras are in the presenter_slide class


# -*- coding:utf-8 -*-
import logging
import os
import sys
import time
import threading

import requests

# 任务执行间隔
delay = ''
# 目标 URL
url = ''
# 日志文件名
log = 'common.log'

# 单独线程请求 api
def request_api():
    try:
        logging.info(requests.get(url, timeout=5).text)
    except requests.exceptions.ConnectTimeout as timeout:
        logging.info('this request timeout.')
    except Exception as e:
        logging.info(e)

# 任务服务器
# -p 持续时间
# -t 任务执行间隔
# -s 开始时间
# -u 目标 url
# -d 日志文件名称
if __name__ == '__main__':
    # 循环读取参数
    for count in range(0, len(sys.argv)):
        arg = sys.argv[count]
        if arg == '-t':
            delay = sys.argv[count + 1]
        if arg == '-u':
            url = sys.argv[count + 1]
        if arg == '-d':
            log = sys.argv[count + 1]

    # 自动创建目录文件
    if not os.path.exists('./log'):
        os.makedirs('./log')
    # 日志配置
    logging.basicConfig(level=logging.DEBUG, filename=('./log/' + log + '.log'), filemode="w",
                        format="%(asctime)s - %(message)s", datefmt="%Y-%m-%d %H:%M:%S")
    # 任务进行中
    while True:
        threading.Thread(target=request_api).start()
        time.sleep(float(delay))


from evaluation import confusion_matrix_util, roc_util
from sklearn.metrics import *
import pandas as pd

def evaluate_by_files(y_test_path, y_pred_path, y_pred_proba_path, class_names, target, save_plots_path_prefix):
    y_test_df = pd.read_csv(y_test_path)
    y_pred_df = pd.read_csv(y_pred_path)
    confusion_matrix_util.plot_confusion_matrix(y_test_path, y_pred_path, classes=class_names,
                                                save_path=save_plots_path_prefix + "lstm_confusion_m.png",
                                                title='helpers confusion matrix, without normalization ' + target)

    # Plot normalized confusion matrix
    confusion_matrix_util.plot_confusion_matrix(y_test_path, y_pred_path, classes=class_names,
                                                save_path=save_plots_path_prefix + "lstm_confusion_m_norm.png",
                                                normalize=True,
                                                title='helpers normalized confusion matrix ' + target)

    # calc accuracy
    accuracy = accuracy_score(y_test_df, y_pred_df)
    balanced_accuracy = balanced_accuracy_score(y_test_df, y_pred_df)
    print("Accuracy: %.2f%%" % (accuracy * 100.0))
    print("Balanced Accuracy: %.2f%%" % (balanced_accuracy * 100.0))
    score_df = pd.DataFrame([])
    score_df['accuracy'] = accuracy
    score_df['balanced_accuracy'] = balanced_accuracy
    score_df.to_csv(save_plots_path_prefix + 'scores_' + target + '.csv',
                    index=False)

    # calc ROC and AUC
    roc_util.calc_and_plot_roc(y_test_path, y_pred_path, y_pred_proba_path,
                               save_path=save_plots_path_prefix + "lstm_roc_curves.png",
                               model_name_title="helpers " + target)


#
# @lc app=leetcode id=147 lang=python3
#
# [147] Insertion Sort List
#

# @lc code=start
# Definition for singly-linked list.
class ListNode:
    def __init__(self, val=0, next=None):
        self.val = val
        self.next = next
class Solution:
    def insertionSortList(self, head):
        dummy=ListNode(0,head)
        previous , current= head , head.next
        while current :
            if current.val>=previous.val:
                previous,current=current,current.next
                continue
            temp=dummy
            while current.val > temp.next.val:
                temp=temp.next
            previous.next=current.next
            current.next=temp.next
            temp.next=current
            current=previous.next
        return dummy.next

# @lc code=end


#!/usr/bin/env python2
# encoding: utf-8

# Created by Brian Cherinka on 2016-06-09 16:21:28
# Licensed under a 3-clause BSD license.

# Revision History:
#     Initial Version: 2016-06-09 16:21:28 by Brian Cherinka
#     Last Modified On: 2016-06-09 16:21:28 by Brian

from __future__ import print_function, division, absolute_import
import unittest

from actorcore import Actor
from opscore.actor import Model, KeyVarDispatcher
from actorcore import TestHelper

import sopTester

from sopActor import SopActor

logDirBase = 'temp/'

''' unit tests for SopActor '''

class SopActorTester(unittest.TestCase):
    """Parent class for tests that actually need a proper sopActor instance."""
    @classmethod
    def setUpClass(cls):
        # can only configure the dispatcher once.
        if Model.dispatcher is None:
            Model.setDispatcher(KeyVarDispatcher())
        Actor.setupRootLogger = TestHelper.setupRootLogger

    def setUp(self):
        super(SopActorTester, self).setUp()
        self.addCleanup(self._close_port)

    def _close_port(self):
        """
        Close the connection: requires handling the deferred. Details here:
            https://jml.io/pages/how-to-disconnect-in-twisted-really.html
        """
        if getattr(self, 'sop', None) is not None:
            deferred = self.sop.commandSources.port.stopListening()
            deferred.callback(None)

    def tearDown(self):
        # have to clear any actors that were registered previously.
        Model._registeredActors = set()
        super(SopActorTester, self).tearDown()

class TestSopActor(SopActorTester):
    def test_init_fails(self):
        with self.assertRaises(KeyError):
            self.sop = SopActor.SopActor.newActor(location='nonsense', makeCmdrConnection=False)

    def test_init_apo(self):
        self.sop = SopActor.SopActor.newActor(location='apo', makeCmdrConnection=False)
        self.assertIsInstance(self.sop, SopActor.SopActorAPO)
        self.assertEqual(TestHelper.logBuffer.basedir, '/data/logs/actors/sop')
        logged = TestHelper.logBuffer.getvalue()
        self.assertIsInstance(self.sop.actorState, Actor.ActorState)
        self.assertIn('attaching command set SopCmd', logged)
        self.assertIn('attaching command set SopCmd_APO', logged)

    def test_init_lco(self):
        self.sop = SopActor.SopActor.newActor(location='lco', makeCmdrConnection=False)
        self.assertIsInstance(self.sop, SopActor.SopActorLCO)
        self.assertIsInstance(self.sop.actorState, Actor.ActorState)
        self.assertEqual(TestHelper.logBuffer.basedir, '/data/logs/actors/sop')
        logged = TestHelper.logBuffer.getvalue()
        self.assertIn('attaching command set SopCmd', logged)
        self.assertIn('attaching command set SopCmd_LCO', logged)

    def test_init_local(self):
        self.sop = SopActor.SopActor.newActor(location='local', makeCmdrConnection=False)
        self.assertIsInstance(self.sop, SopActor.SopActorLocal)
        self.assertIsInstance(self.sop.actorState, Actor.ActorState)
        self.assertEqual(TestHelper.logBuffer.basedir, '/data/logs/actors/sop')
        logged = TestHelper.logBuffer.getvalue()
        self.assertIn('attaching command set SopCmd', logged)
        self.assertIn('attaching command set SopCmd_LOCAL', logged)

if __name__ == '__main__':
    verbosity = 2

    unittest.main(verbosity=verbosity)


from django.shortcuts import render
from django.views import generic
from django.core.paginator import PageNotAnInteger, Paginator, EmptyPage

from .models import Dytt8

class IndexView(generic.ListView):
    template_name = 'movie/index.html'
    context_object_name = 'movie_list'
    paginate_by = 20

    def get_queryset(self):
        return Dytt8.objects.exclude(link__isnull=True).order_by('-name')

def search(request):
    q = request.GET.get('q')
    error_msg = ''
    if not q:
        error_msg = '请输入关键词'
        return render(request, 'movie/index.html', {'error_msg': error_msg})
    movie_list = Dytt8.objects.filter(name__icontains=q)
    return render(request, 'movie/index.html', {'error_msg': error_msg,
                                                  'movie_list': movie_list})

def index(request):
    movie = Dytt8.objects.exclude(link__isnull=True)
    paginator = Paginator(movie, 20)
    page = request.GET.get('page')
    try:
        movie = paginator.page(page)
    except PageNotAnInteger:
        movie = paginator.page(1)
    except EmptyPage:
        movie = paginator.page(paginator.num_pages)
    return render(request, 'movie/index1.html', {'movie': movie})


import pygame as pg
import pytmx
from settings import WIDTH, HEIGHT

class TiledMap:
    def __init__(self, filename):
        self.tmxdata = pytmx.load_pygame(filename, pixelalpha=True)
        self.width = self.tmxdata.width * self.tmxdata.tilewidth
        self.height = self.tmxdata.height * self.tmxdata.tileheight

    def render(self, surface):
        ti = self.tmxdata.get_tile_image_by_gid
        for layer in self.tmxdata.visible_layers:
            if isinstance(layer, pytmx.TiledTileLayer):
                for x, y, gid, in layer:
                    tile = ti(gid)
                    if tile:
                        surface.blit(tile, (x * self.tmxdata.tilewidth, y * self.tmxdata.tileheight))

    def make_map(self):
        temp_surface = pg.Surface((self.width, self.height))
        self.render(temp_surface)
        return temp_surface

class Camera:
    def __init__(self, width, height):
        """
        Creates a camera object
        :param width: The width of the cameras view
        :param height: The height of the cameras view
        """
        self.camera = pg.Rect(0, 0, width, height)
        self.width = width
        self.height = height

    def apply(self, entity):
        """
        Applies the camera's focus on the target entity
        :param entity: The focus of the camera
        :return: The entity's rect moved according to the camera position
        """
        # Applies the camera offset to the entity
        return entity.rect.move(self.camera.topleft)

    def apply_to_point(self, point):
        return [point[0] + self.camera.x, point[1] + self.camera.y]

    def apply_rect(self, rect, copy=False):
        """
        Applies the camera's focus on the target rectangle
        :param rect: The focus of the camera
        :return: The moved rectangle according to the camera's position
        """
        # Applies the camera offset to
        # a rectangle
        if not copy:
            return rect.move(self.camera.topleft)
        else:
            x = rect.copy()
            return x.move(self.camera.topleft)

    def update(self, target):
        """
        Updates the location of the camera relative to the target
        :param target: The focus of the camera
        :return: None
        """
        # Get how much the target moved in the x & y
        # Objects that the pass by the target move in the opposite direction of the target
        x = -target.rect.centerx + (WIDTH // 2)
        y = -target.rect.centery + (HEIGHT // 2)

        # Limit the scrolling on all four sides of the screen
        x = min(0, x)  # left
        y = min(0, y)  # top
        x = max(-(self.width - WIDTH), x)  # right
        y = max(-(self.height - HEIGHT), y)  # bottom

        # Update the camera
        self.camera = pg.Rect(x, y, self.width, self.height)


from django.shortcuts import render
# Create your views here.

from django.core.files.storage import FileSystemStorage

from keras.models import load_model
from keras.preprocessing import image
import tensorflow as tf
import json
from tensorflow import Graph
import numpy as np

img_height, img_width=224,224
with open('./models/imagenet_classes.json','r') as model:
    modelLabel=model.read()

modelLabel=json.loads(modelLabel)

model_graph = Graph()
with model_graph.as_default():
    tf_session = tf.compat.v1.Session()
    with tf_session.as_default():
        model=load_model('./models/MobileNetModelImagenet.h5')

def index(request):
    context={'a':1}
    return render(request,'index.html',context)

def predictImage(request):
    print (request)
    print (request.POST.dict())
    file=request.FILES['filePath']
    fileSave=FileSystemStorage()
    filePathName=fileSave.save(file.name,file)
    filePathName=fileSave.url(filePathName)
    imageSetup ='.'+filePathName
    img = image.load_img(imageSetup, target_size=(img_height, img_width))
    imgOutput  = image.img_to_array(img)
    imgOutput=imgOutput/255
    imgOutput=imgOutput.reshape(1,img_height, img_width,3)
    with model_graph.as_default():
        with tf_session.as_default():
            prediction=model.predict(imgOutput)

    predictedImage=modelLabel[str(np.argmax(prediction[0]))]

    context={'filePathName':filePathName,'predictedLabel':predictedImage[1]}
    return render(request,'index.html',context)


#User function Template for python3

class Solution:
    def matrixMultiplication(self, N, arr):
        dp = {}
        for i in range(N):
            dp[(i,i)] = 0

        for i in range(N-1,0,-1):
            for j in range(i+1,N,1):
                mini = float("inf")
                for k in range(i,j):
                    steps = arr[i-1]*arr[k]*arr[j] + dp[(i,k)]+dp[(k+1,j)]
                    mini = min(steps,mini)
                dp[(i,j)] = mini
        return dp[(1,N-1)]

        # def helper(i,j):
        #     if i == j: return 0
        #     if (i,j) in dp: return dp[(i,j)]
        #     mini = float("inf")
        #     for k in range(i,j):
        #         steps = arr[i-1]*arr[k]*arr[j] + helper(i,k)+helper(k+1,j)
        #         mini = min(steps,mini)
        #         dp[(i,j)] = mini
        #     return dp[(i,j)]
        # return helper(1,N-1)

#{
 # Driver Code Starts
#Initial Template for Python 3

if __name__ == '__main__':
    t = int(input())
    for _ in range(t):
        N = int(input())
        arr = input().split()
        for i in range(N):
            arr[i] = int(arr[i])

        ob = Solution()
        print(ob.matrixMultiplication(N, arr))
# } Driver Code Ends

Matrix Chain Multiplication - GFG/matrix-chain-multiplication.py


"""Parses SAM given a template"""
import logging

from samcli.commands.local.cli_common.user_exceptions import InvalidSamTemplateException
from samcli.commands.local.lib.cfn_base_api_provider import CfnBaseApiProvider

LOG = logging.getLogger(__name__)

class CfnApiProvider(CfnBaseApiProvider):
    APIGATEWAY_RESTAPI = "AWS::ApiGateway::RestApi"
    APIGATEWAY_STAGE = "AWS::ApiGateway::Stage"
    TYPES = [
        APIGATEWAY_RESTAPI,
        APIGATEWAY_STAGE
    ]

    def extract_resources(self, resources, collector, cwd=None):
        """
        Extract the Route Object from a given resource and adds it to the RouteCollector.

        Parameters
        ----------
        resources: dict
            The dictionary containing the different resources within the template

        collector: samcli.commands.local.lib.route_collector.RouteCollector
            Instance of the API collector that where we will save the API information

        cwd : str
            Optional working directory with respect to which we will resolve relative path to Swagger file

        Return
        -------
        Returns a list of routes
        """
        for logical_id, resource in resources.items():
            resource_type = resource.get(CfnBaseApiProvider.RESOURCE_TYPE)
            if resource_type == CfnApiProvider.APIGATEWAY_RESTAPI:
                self._extract_cloud_formation_route(logical_id, resource, collector, cwd=cwd)

            if resource_type == CfnApiProvider.APIGATEWAY_STAGE:
                self._extract_cloud_formation_stage(resources, resource, collector)

    def _extract_cloud_formation_route(self, logical_id, api_resource, collector, cwd=None):
        """
        Extract APIs from AWS::ApiGateway::RestApi resource by reading and parsing Swagger documents. The result is
        added to the collector.

        Parameters
        ----------
        logical_id : str
            Logical ID of the resource

        api_resource : dict
            Resource definition, including its properties

        collector : ApiCollector
            Instance of the API collector that where we will save the API information
        """
        properties = api_resource.get("Properties", {})
        body = properties.get("Body")
        body_s3_location = properties.get("BodyS3Location")
        binary_media = properties.get("BinaryMediaTypes", [])

        if not body and not body_s3_location:
            # Swagger is not found anywhere.
            LOG.debug("Skipping resource '%s'. Swagger document not found in Body and BodyS3Location",
                      logical_id)
            return
        self.extract_swagger_route(logical_id, body, body_s3_location, binary_media, collector, cwd)

    @staticmethod
    def _extract_cloud_formation_stage(resources, stage_resource, collector):
        """
        Extract the stage from AWS::ApiGateway::Stage resource by reading and adds it to the collector.
        Parameters
       ----------
        resources: dict
            All Resource definition, including its properties

        stage_resource : dict
            Stage Resource definition, including its properties

        collector : ApiCollector
            Instance of the API collector that where we will save the API information
        """
        properties = stage_resource.get("Properties", {})
        stage_name = properties.get("StageName")
        stage_variables = properties.get("Variables")

        # Currently, we aren't resolving any Refs or other intrinsic properties that come with it
        # A separate pr will need to fully resolve intrinsics
        logical_id = properties.get("RestApiId")
        if not logical_id:
            raise InvalidSamTemplateException("The AWS::ApiGateway::Stage must have a RestApiId property")

        rest_api_resource_type = resources.get(logical_id, {}).get("Type")
        if rest_api_resource_type != CfnApiProvider.APIGATEWAY_RESTAPI:
            raise InvalidSamTemplateException(
                "The AWS::ApiGateway::Stage must have a valid RestApiId that points to RestApi resource {}".format(
                    logical_id))

        collector.stage_name = stage_name
        collector.stage_variables = stage_variables


import os
import logging

from collections import OrderedDict

from cmd import command
from cmd.cmd import Arg
from command_container import CommandContainer
from storage import NumbersStorage

class NumsBasicCommands(NumbersStorage, CommandContainer):
    @staticmethod
    @command('list',
             'выводит содержимое на экран через заданный разделеитель',
             Arg('separator', str, '\t'))
    def list(separator: str):
        print(f'{separator}'.join(map(str, NumbersStorage.storage)))

    @staticmethod
    @command('clear', descr='удаляет все из набора')
    def clear():
        NumsBasicCommands.storage.clear()

    @staticmethod
    @command('add', 'Добавляет значение в набор',
             Arg('value', int, required=True))
    def add(value: int):
        NumsBasicCommands.storage.append(value)

    @staticmethod
    @command('del', 'Удаляет из набора заданный элемент или элементы в '
                    'указанном диопазоне',
             Arg('start_index', int, required=True), Arg('stop_index', int))
    def del_(start: int, stop: int):
        if stop is None:
            del NumsBasicCommands.storage[start]
        else:
            del NumsBasicCommands.storage[slice(start, stop)]

    @staticmethod
    @command('find', 'выполняет поиск значения в наборе',
             Arg('value', int, required=True))
    def find(value: int):
        try:
            index = NumsBasicCommands.storage.index(value)
            print(f'Значение {value} найдено в позиции {index}')
        except ValueError:
            print(f'Значение {value} не найдено')

    @staticmethod
    @command('set', 'устанавливает значение в указанную позицию',
             Arg('index', int, required=True),
             Arg('value', int, required=True))
    def set(index: int, value: int):
        NumsBasicCommands.storage.insert(index, value)

    @staticmethod
    @command('get', 'читает значение в указанной позиции',
             Arg('index', int, required=True))
    def get(index: int):
        try:
            print(
                f'В позиции {index} находится значение '
                f'{NumsBasicCommands.storage[index]}'
            )
        except IndexError:
            print(f'По индексу {index} значение ещё не добавлено')

    @staticmethod
    @command('unique', 'удаляет дубликаты в наборе')
    def unique():
        NumsBasicCommands.storage = list(
            OrderedDict.fromkeys(NumsBasicCommands.storage)
        )

    @staticmethod
    @command('load', 'загружает набор из текстового файла',
             Arg('path', str, required=True), Arg('separator', str, '\t'))
    def load(path: str, separator: str):
        if os.path.exists(path):
            try:
                with open(path, encoding='utf8') as inp:
                    NumsBasicCommands.storage.extend(
                        map(int, inp.read().split(separator))
                    )
            except ValueError:
                print('Указан неправильный разделитель или файл содержит не '
                      'только цифры')
            except Exception as err:
                logging.getLogger('main').exception(err)
                print('При загрузке файла возникла ошибка')
        else:
            print('Такого файла не существует')

    @staticmethod
    @command('count', 'отображает количество чисел в наборе')
    def count():
        print(len(NumsBasicCommands.storage))


#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Created on Wed Sep 20 10:03:14 2017

@author: tkoller
"""

import argparse

from safe_exploration.utils_config import loadConfig, create_env, create_solver, get_model_options_from_conf
from safe_exploration.exploration_runner import run_exploration
from safe_exploration.episode_runner import run_episodic
from safe_exploration.uncertainty_propagation_runner import run_uncertainty_propagation

def create_parser():
    """ Create the argparser """

    parser = argparse.ArgumentParser(description=""" Library for MPC-base Safe Exploration
    of unknown Dynamic Systems using Gaussian Processes \n\n

    Default configurations for the following scenarios exist:

    """)
    parser.add_argument("--scenario_config",
                        default= "journal_experiment_configs/dynamic_expl_inv_pend_nsafe=2_nperf=0_r=1_beta_safe=2.py", type = str,
                        help= """ Create your own scenario by copying one of the scenarios
                        in the example_configs/ directory and changing the default options.\n
                        Default scenario is dynamic_expl_inv_pend_nsafe=2_nperf=0_r=1_beta_safe=2.py (see above for explanation) """)
    return parser

def check_config_conflicts(conf):
    """ Check if there are conflicting options in the Config

    Parameters
    ----------
    conf: Config
        The config file

    Returns
    -------
    has_conflict: Bool
        True, if there is a conflict in the config
    conflict_str: String
        The error message

    """

    has_conflict = False
    conflict_str = ""
    if conf.task == "exploration" and not conf.solver_type == "safempc":
        return True, "Exploration task only allowed with safempc solver"
    elif conf.task == "uncertainty_propagation" and not conf.solver_type == "safempc":
        return True, "Uncertainty propagation task only allowed with safempc solver"

    return has_conflict, conflict_str

def run_scenario(args):
    """ Run the specified scenario

    Parameters
    ----------
    args:
        The parsed arguments (see create_parser for details)
    """
    config_path = args.scenario_config
    conf = loadConfig(config_path)

    conflict, conflict_str = check_config_conflicts(conf)
    if conflict:
        raise ValueError("There are conflicting settings: {}".format(conflict_str))

    env = create_env(conf.env_name, conf.env_options)

    solver, safe_policy = create_solver(conf, env)

    task = conf.task
    if task == "exploration":
        run_exploration(conf, conf.visualize)#,conf.static_exploration,conf.n_iterations,
                        #conf.n_restarts_optimizer,conf.visualize,conf.save_vis,conf.save_path,conf.verify_safety,conf.n_experiments)
    elif task == "episode_setting":
        run_episodic(conf)

    elif task == "uncertainty_propagation":
        run_uncertainty_propagation(env, solver, conf)

if __name__ == "__main__":
    parser = create_parser()
    args = parser.parse_args()
    run_scenario(args)


import threading
import socket
host = "127.0.0.1"
port = 54321

# Basic internet TCP server
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind((host, port))
server.listen()

# Arrays for clients, names and channels
clients = []
names = []
ch1 = []
ch2 = []
ch3 = []

# Sends received messages to all clients
def send_message(msg):
    for c in clients:
        c.send(msg)

# Sends received messages to clients in channel 1
def send_ch1(msg):
    for client in ch1:
        client.send(msg)

# Sends received messages to clients in channel 2
def send_ch2(msg):
    for client in ch2:
        client.send(msg)

# Sends received messages to clients in channel 3
def send_ch3(msg):
    for client in ch3:
        client.send(msg)

# Handles received messages from one client
def handle_client(client):
    while True:
        try:
            msg = client.recv(1024)
            decoded = msg.decode('utf-8')
            i = clients.index(client)
            client_name = names[i]

            # ----------      Channel switching      ---------- #
            # Switch to channel 1
            if '/ch 1' in decoded:
                if client not in ch1:
                    ch1.append(client)      # Add to channel
                    if client in ch2:
                        ch2.remove(client)  # Remove from other channels
                    elif client in ch3:
                        ch3.remove(client)
                    print(f"Switched client {client_name} to channel 1.")
                    client.send("Switched to channel 1.".encode('utf-8'))
                    continue

            # Switch to channel 2
            elif '/ch 2' in decoded:
                if client not in ch2:
                    ch2.append(client)      # Add to channel
                    if client in ch1:
                        ch1.remove(client)  # Remove from other channels
                    elif client in ch3:
                        ch3.remove(client)
                    print(f"Switched client {client_name} to channel 2")
                    client.send("Switched to channel 2.".encode('utf-8'))
                    continue

            # Switch to channel 3
            elif '/ch 3' in decoded:
                if client not in ch3:
                    ch3.append(client)      # Add to channel
                    if client in ch1:
                        ch1.remove(client)  # Remove from other channels
                    elif client in ch2:
                        ch2.remove(client)
                    print(f"Switched client {client_name} to channel 3")
                    client.send("Switched to channel 3.".encode('utf-8'))
                    continue

            # Leave both channels
            elif '/main' in decoded:
                if client in ch1:
                    ch1.remove(client)
                if client in ch2:
                    ch2.remove(client)
                if client in ch3:
                    ch3.remove(client)
                client.send("Left from channels.".encode('utf-8'))

            # Disconnect client
            elif '/exit' in decoded:
                i = clients.index(client)
                clients.remove(client)
                client.close()
                client_name = names[i]
                send_message(f'{client_name} has left the server.'.encode('utf-8'))
                print(f'{client_name} has left the server.')
                names.remove(client_name)
                break

            # Send the message to appropriate channels
            else:
                if client in ch1:
                    send_ch1(msg)
                elif client in ch2:
                    send_ch2(msg)
                elif client in ch3:
                    send_ch3(msg)
                else:
                    send_message(msg)

        # This except block is executed when the client isn't connected anymore (try-block gives an error)
        # The client and its name is removed from lists and a message is broadcast to the other clients.
        except:
            i = clients.index(client)
            clients.remove(client)
            if client in ch1:
                ch1.remove(client)
            if client in ch2:
                ch2.remove(client)

            client.close()
            client_name = names[i]
            send_message(f'{client_name} has left the server.'.encode('utf-8'))
            names.remove(client_name)
            break

def receive():
    while True:
        client, address = server.accept()  # All clients are accepted

        print(f"A client from address {str(address)} connected")
        client.send('NAME_REQUEST'.encode('utf-8'))  # This will be a codeword to the client to send a name

        name = client.recv(1024).decode('utf-8')
        clients.append(client)
        names.append(name)
        print(f"Their name is: {name}")

        send_message(f"{name} has joined the server!".encode("utf-8"))  # Broadcast to clients that a client has joined

        # Multithreading to handle each connected client
        thread = threading.Thread(target=handle_client, args=(client,))
        thread.start()

print("Server started!")
receive()


import turtle
import pandas

score = 0
screen = turtle.Screen()
screen.title("US States Game")
image = "blank_states_img.gif"
screen.addshape(image)
turtle.shape(image)

states_data = pandas.read_csv("50_states.csv")

#screen.update()
guessed_correctly = []
while len(guessed_correctly) < 50:
    answer_state = screen.textinput(title=f"{len(guessed_correctly)}/50 States Correct",
                                    prompt="What's another state's name?").title()

    if answer_state == "Exit":
        break

    if (answer_state in states_data.state.tolist()) and (answer_state not in guessed_correctly):
        t = turtle.Turtle()
        t.hideturtle()
        t.penup()
        state_data = states_data[states_data.state == answer_state]
        t.setpos(int(state_data.iloc[0].x), int(state_data.iloc[0].y))
        t.write(answer_state)
        guessed_correctly.append(answer_state)

states_to_learn = list(set(states_data.state.tolist()) - set(guessed_correctly))
print(states_to_learn)
df = pandas.DataFrame(states_to_learn)
df.to_csv("states_to_learn.csv")


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

"""
This module provides class and methods for dealing with RDKit Conformer.
"""

from typing import Sequence, Union

import numpy as np
from rdkit.Chem import rdMolTransforms as rdMT
from rdkit.Chem.rdchem import Conformer

from rdmc.utils import find_internal_torsions, set_rdconf_coordinates

class RDKitConf(object):
    """
    A wrapper for rdchem.Conformer.

    The method nomenclature follows the Camel style to be consistent with RDKit.
    """

    def __init__(self, conf):
        self._conf = conf
        self._owning_mol = conf.GetOwningMol()
        for attr in dir(conf):
            if not attr.startswith('_') and not hasattr(self, attr):
                setattr(self, attr, getattr(self._conf, attr,))

    @classmethod
    def FromConformer(cls,
                      conf: Conformer,
                      ) -> 'RDKitConf':
        """
        Convert a RDKit Chem.rdchem.Conformer to a RDKitConf. This allows a more
        capable and flexible Conformer class.

        Args:
            conf (Chem.rdchem.Conformer): A RDKit Conformer instance to be converted.

        Returns:
            RDKitConf: The conformer corresponding to the RDKit Conformer in RDKitConf
        """
        return cls(conf)

    @classmethod
    def FromMol(cls,
                mol: Union['RWMol', 'Mol'],
                id: int = 0,
                ) -> 'RDkitConf':
        """
        Get a RDKitConf instance from a Chem.rdchem.Mol/RWMol instance.

        Args:
            mol (Union[RWMol, Mol]): a Molecule in RDKit Default format.
            id (int): The id of the conformer to be extracted from the molecule.

        Returns:
            RDKitConf: A Conformer in RDKitConf of the given molecule
        """
        return cls(mol.GetConformer(id))

    @classmethod
    def FromRDKitMol(cls,
                     rdkitmol: 'RDKitMol',
                     id: int = 0,
                     ) -> 'RDkitConf':
        """
        Get a RDKitConf instance from a RDKitMol instance. The owning molecule
        of the generated conformer is RDKitMol instead of Chem.rdchem.Mol.

        Args:
            rdkitmol (RDKitMol): a Molecule in RDKitMol.
            id (int): The id of the conformer to be extracted from the molecule.

        Returns:
            RDKitConf: A Conformer in RDKitConf of the given molecule
        """
        return rdkitmol.GetConformer(id)

    def GetBondLength(self,
                      atomIds: Sequence[int],
                     ) -> float:
        """
        Get the bond length between atoms in Angstrom.

        Args:
            atomIds (Sequence): A 3-element sequence object containing atom indexes.

        Returns:
            float: Bond length in Angstrom.
        """
        assert len(atomIds) == 2, ValueError(f'Invalid atomIds. It should be a sequence with a length of 2. Got {atomIds}')
        return rdMT.GetBondLength(self._conf, *atomIds)

    def GetAngleDeg(self,
                    atomIds: Sequence[int],
                    ) -> float:
        """
        Get the angle between atoms in degrees.

        Args:
            atomIds (Sequence): A 3-element sequence object containing atom indexes.

        Returns:
            float: Angle value in degrees.
        """
        assert len(atomIds) == 3, ValueError(f'Invalid atomIds. It should be a sequence with a length of 3. Got {atomIds}.')
        return rdMT.GetAngleDeg(self._conf, *atomIds)

    def GetAngleRad(self,
                    atomIds: Sequence[int],
                    ) -> float:
        """
        Get the angle between atoms in rads.

        Args:
            atomIds (Sequence): A 3-element sequence object containing atom indexes.

        Returns:
            float: Angle value in rads.
        """
        assert len(atomIds) == 3, ValueError(f'Invalid atomIds. It should be a sequence with a length of 3. Got {atomIds}')
        return rdMT.GetAngleRad(self._conf, *atomIds)

    def GetAllTorsionsDeg(self) -> list:
        """
        Get the dihedral angles of all torsional modes (rotors) of the Conformer. The sequence of the
        values are corresponding to the torsions of the molecule (``GetTorsionalModes``).

        Returns:
            list: A list of dihedral angles of all torsional modes.
        """
        return [self.GetTorsionDeg(tor) for tor in self.GetTorsionalModes()]

    def GetOwningMol(self):
        """
        Get the owning molecule of the conformer.

        Returns:
            Union[Mol, RWMol, RDKitMol]: The owning molecule
        """
        return self._owning_mol

    def GetTorsionDeg(self,
                      torsion: list,
                      ) -> float:
        """
        Get the dihedral angle of the torsion in degrees. The torsion can be defined
        by any atoms in the molecule (not necessarily bonded atoms.)

        Args:
            torsion (list): A list of four atom indexes.

        Returns:
            float: The dihedral angle of the torsion.
        """
        return rdMT.GetDihedralDeg(self._conf, *torsion)

    def GetTorsionalModes(self,
                          indexed1: bool = False):
        """
        Get all of the torsional modes (rotors) of the Conformer. This information
        is obtained from its owning molecule.

        Args:
            indexed1: The atom index in RDKit starts from 0. If you want to have
                       indexed 1 atom indexes, please set this argument to ``True``.

        Returns:
            Optinal[list]: A list of four-atom-indice to indicating the torsional modes.
        """
        try:
            return self._torsions if not not indexed1 \
                else [[ind + 1 for ind in tor] for tor in self._torions]
        except AttributeError:
            self._torsions = find_internal_torsions(self._owning_mol)
            return self._torsions

    def HasOwningMol(self):
        """
        Whether the conformer has a owning molecule.

        Returns:
            bool: ``True`` if the conformer has a owning molecule.
        """
        if self._owning_mol:
            return True
        return False

    def SetOwningMol(self,
                     owningMol: Union['RDKitMol',
                                      'Mol',
                                      'RWMol']):
        """
        Set the owning molecule of the conformer. It can be either RDKitMol
        or Chem.rdchem.Mol.

        Args:
            owningMol: Union[RDKitMol, Chem.rdchem.Mol] The owning molecule of the conformer.

        Raises:
            ValueError: Not a valid ``owning_mol`` input, when giving something else.
        """
        if not hasattr(owningMol, 'GetConformer'):
            raise ValueError('Provided an invalid molecule object.')
        self._owning_mol = owningMol

    def SetPositions(self,
                     coords: Union[tuple, list]):
        """
        Set the Positions of atoms of the conformer.

        Args:
            coords: a list of tuple of atom coordinates.
        """
        set_rdconf_coordinates(self._conf, coords)

    def SetBondLength(self,
                      atomIds: Sequence[int],
                      value: Union[int, float],
                     ) -> float:
        """
        Set the bond length between atoms in Angstrom.

        Args:
            atomIds (Sequence): A 3-element sequence object containing atom indexes.
            value (int or float, optional): Bond length in Angstrom.
        """
        assert len(atomIds) == 2, ValueError(f'Invalid atomIds. It should be a sequence with a length of 2. Got {atomIds}')
        return rdMT.SetBondLength(self._conf, *atomIds, value)

    def SetAngleDeg(self,
                    atomIds: Sequence[int],
                    value: Union[int, float],
                    ) -> float:
        """
        Set the angle between atoms in degrees.

        Args:
            atomIds (Sequence): A 3-element sequence object containing atom indexes.
            value (int or float, optional): Bond angle in degrees.
        """
        assert len(atomIds) == 3, ValueError(f'Invalid atomIds. It should be a sequence with a length of 3. Got {atomIds}.')
        return rdMT.SetAngleDeg(self._conf, *atomIds, value)

    def SetAngleRad(self,
                    atomIds: Sequence[int],
                    value: Union[int, float],
                    ) -> float:
        """
        Set the angle between atoms in rads.

        Args:
            atomIds (Sequence): A 3-element sequence object containing atom indexes.
            value (int or float, optional): Bond angle in rads.
        """
        assert len(atomIds) == 3, ValueError(f'Invalid atomIds. It should be a sequence with a length of 3. Got {atomIds}')
        return rdMT.SetAngleRad(self._conf, *atomIds, value)

    def SetTorsionDeg(self,
                      torsion: list,
                      degree: Union[float, int]):
        """
        Set the dihedral angle of the torsion in degrees. The torsion can only be defined
        by a chain of bonded atoms.

        Args:
            torsion (list): A list of four atom indexes.
            degree (float, int): The dihedral angle of the torsion.
        """
        rdMT.SetDihedralDeg(self._conf, *torsion, degree)

    def SetAllTorsionsDeg(self, angles: list):
        """
        Set the dihedral angles of all torsional modes (rotors) of the Conformer. The sequence of the
        values are corresponding to the torsions of the molecule (``GetTorsionalModes``).

        Args:
            angles (list): A list of dihedral angles of all torsional modes.
        """
        if len(angles) != len(self.GetTorsionalModes()):
            raise ValueError(
                'The length of angles is not equal to the length of torsional modes')
        for angle, tor in zip(angles, self.GetTorsionalModes()):
            self.SetTorsionDeg(tor, angle)

    def SetTorsionalModes(self,
                          torsions: Union[list, tuple]):
        """
        Set the torsional modes (rotors) of the Conformer. This is useful when the
        default torsion is not correct.

        Args:
            torsions (Union[list, tuple]): A list of four-atom-lists indicating the torsional modes.

        Raises:
            ValueError: The torsional mode used is not valid.
        """
        if isinstance(torsions, (list, tuple)):
            self._torsions = torsions
        else:
            raise ValueError('Invalid torsional mode input.')

    def ToConformer(self) -> 'Conformer':
        """
        Get its backend RDKit Conformer object.

        Returns:
            Conformer: The backend conformer
        """
        return self._conf


# -*- coding: utf-8 -*-
"""
Created on Sun Jan 16 01:41:20 2022

@author: Greta
"""

# Importing needed libraries
import numpy as np
import pandas as pd
from sklearn.feature_extraction.text import CountVectorizer
from googletrans import Translator
translator = Translator()
# NLTK tools for text processing
import re, nltk
from nltk import word_tokenize
from nltk.corpus import stopwords
nltk.download('stopwords')
nltk.download('punkt')
from nltk import pos_tag
nltk.download('wordnet')
from nltk.corpus import wordnet
from nltk.stem import WordNetLemmatizer
wordnet_lemmatizer = WordNetLemmatizer()
from nltk.stem import PorterStemmer
ps = PorterStemmer()
from textblob import TextBlob
from IPython.core.interactiveshell import InteractiveShell
InteractiveShell.ast_node_interactivity = 'all'

# Charger les données
df_pt = pd.read_csv(r'C:\Users\Greta\Desktop\Projet Brazil\olist_order_reviews_dataset.csv',sep=',',encoding='utf-8')

#Séparer reviews et titre, supprimer titres
review_data_title = df_pt['review_comment_title']
review_data = df_pt.drop(['review_comment_title'],axis=1)

# Drop valeurs nulles
reviews = review_data.dropna(subset=['review_comment_message'])

# Reset index
review_data = review_data.reset_index(drop=True)

# Traitement des reviews sous forme de liste
criticas = list(reviews['review_comment_message'].values)

# Regex pour enlever les retours à la ligne
criticas_temp = []
criticas_pos_quebra = []
for c in criticas:
    c = re.sub(r'\n', ' ', c)
    criticas_temp.append(c)
for c in criticas_temp:
    c = re.sub(r'\r', ' ', c)
    criticas_pos_quebra.append(c)

# Regex pour enlever les liens url
criticas_pos_hyperlink = []
for c in criticas_pos_quebra:
    urls = re.findall('(http|ftp|https)://([\w_-]+(?:(?:\.[\w_-]+)+))([\w.,@?^=%&:/~+#-]*[\w@?^=%&/~+#-])?', c)
    if len(urls) == 0:
        pass
    else:
        for url in urls:
            for link in url:
                c = c.replace(link, '')
        c = c.replace(':', '')
        c = c.replace('/', '')
    criticas_pos_hyperlink.append(c)

#Regex pour enlever les nombres
criticas_pos_num = []
for c in criticas_pos_hyperlink:
    c = re.sub(r'\d+(?:\.\d*(?:[eE]\d+))?', 'numero', c)
    criticas_pos_num.append(c)

# Regex pour tout en alphanumerique
criticas_sem_alphanumerico = []
for c in criticas_pos_num:
    c = re.sub(r'R\$', ' ', c)
    c = re.sub(r'\W', ' ', c)
    criticas_sem_alphanumerico.append(c)

# Regex pour enlever les espaces en trop
criticas_sem_espacos_adicionais = []
for c in criticas_sem_alphanumerico:
    c = re.sub(r'\s+', ' ', c)
    criticas_sem_espacos_adicionais.append(c)

# Sauvegarde de la df traitée après regex
processed_reviews = reviews.copy()
processed_reviews['review_after_regex'] = criticas_sem_espacos_adicionais
processed_reviews = processed_reviews.iloc[:, np.r_[4, -1, 2]]
processed_reviews.head()

# Transforme les reviews en enlevant les stopwords, tout mettre en lower case
comments = []
stopwords = nltk.corpus.stopwords.words('portuguese')

#Retrait des stopword et tout en lower case
for words in processed_reviews['review_after_regex']:
    only_letters = re.sub("[^a-zA-Z]", " ",words)
    tokens = nltk.word_tokenize(only_letters) #tokenize the sentences
    lower_case = [l.lower() for l in tokens] #convert all letters to lower case
    filtered_result = list(filter(lambda l: l not in stopwords, lower_case)) #Remove stopwords from the comments
    comments.append(' '.join(filtered_result))

# Frequence des trigrams
co = CountVectorizer(ngram_range=(3,3))
counts = co.fit_transform(comments)
important_trigrams = pd.DataFrame(counts.sum(axis=0),columns=co.get_feature_names()).T.sort_values(0,ascending=False).head(200)

# Rename colonnes
important_trigrams=important_trigrams.reset_index()
important_trigrams.rename(columns={'index':'trigrams',0:'frequency'},inplace=True)

#Prochaine etape Traduction des trigrams en anglais
important_trigrams['trigrams_traduit'] =important_trigrams['trigrams'].map(lambda x: translator.translate(x, src="pt", dest="en").text)

#  Definition des noms, verbes, adverbes
pos_dict = {'J':wordnet.ADJ, 'V':wordnet.VERB, 'N':wordnet.NOUN, 'R':wordnet.ADV}
def token_stop_pos(text):
    tags = pos_tag(word_tokenize(text))
    newlist = []
    for word, tag in tags:
        if word.lower() not in stopwords:
            newlist.append(tuple([word, pos_dict.get(tag[0])]))
    return newlist

important_trigrams['POS tagged'] = important_trigrams['trigrams_traduit'].apply(token_stop_pos)
important_trigrams.head()

# Definition de la racine du mot
def lemmatize(pos_data):
    lemma_rew = " "
    for word, pos in pos_data:
        if not pos:
            lemma = word
            lemma_rew = lemma_rew + " " + lemma
        else:
                lemma = wordnet_lemmatizer.lemmatize(word, pos=pos)
                lemma_rew = lemma_rew + " " + lemma
    return lemma_rew

important_trigrams['Lemma'] = important_trigrams['POS tagged'].apply(lemmatize)
important_trigrams.head()

from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer
analyzer = SentimentIntensityAnalyzer()

# function to calculate vader sentiment
def vadersentimentanalysis(review):
    vs = analyzer.polarity_scores(review)
    return vs['compound']

important_trigrams['Vader Sentiment'] = important_trigrams['Lemma'].apply(vadersentimentanalysis)

#Fonction pour analyser les reviews : Negatif, neutre et positif
def vader_analysis(compound):
    if compound >= 0.5:
        return 'Positive'
    elif compound <= -0.3 :
        return 'Negative'
    else:
        return 'Neutral'
important_trigrams['Vader Analysis'] = important_trigrams['Vader Sentiment'].apply(vader_analysis)
important_trigrams.head()

file_name= "trigrams_200"
important_trigrams.to_csv(file_name,index=False, encoding="utf-8", sep=";")

vader_counts = important_trigrams['Vader Analysis'].value_counts()
print(vader_counts)

import matplotlib.pyplot as plt
plt.pie(vader_counts.values, labels = vader_counts.index, autopct='%1.1f%%', shadow=False)


from PlayerState import *
from PlayerNormalState import *
from Helpers.Artist import *

class PlayerDieState(PlayerState):

    def __init__(self, player):
        # This game is object oriented and the class extends from
        # PlayerState so its required
        # to call the super class by using the super() method.
        super(PlayerDieState, self).__init__(player)

    def run(self):
        # This line of code changes the variable dead in player to True.
        # If this variable is True, then the collider stops calculating
        # the collision.
        self.player.dead = True

        # This line of code changes the jumps_remaining to 1.
        # It is needed so the player can jump when he dies.
        self.player.jumps_remaining = 1

        # Call the jump method so the player jumps when he dies.
        self.player.jump()

    def update(self):
        # The state called the basic_movement and gravity method,
        # so he can move on both axis.
        self.player.basic_movement()
        self.player.gravity()

        # If the player's bottom (y coordinate) is bigger then 960.
        # So it's down on the screen.
        # Let the player die. And open or reset a level.
        if self.player.rect.bottom >= 960:
            # If the player don't have lives anymore.
            # Open level 1.
            if self.player.lives[1] == self.player.health_image_empty:
                # Set the player's health back to 3.
                self.player.level_state_manager.player_health = 3

                # Set the level to 1.
                self.player.level_state_manager.level = 1

                # Open level 1.
                self.player.level_state_manager.open_level1()

            # If the do have lives left,
            # after the player died.
            # reset the level.
            else:
                self.player.level_state_manager.reset_level()


import pandas as pd
import nltk
from nltk.tokenize import RegexpTokenizer

def calculate_words(element, result):
    results = element.split(' ')
    for key in results:
        if key in result:
            result[key] += 1
        else:
            result[key] = 1
    return result

def nltk_calculate_words(series: pd.Series, top = 100):
    string = series.str.lower().str.cat(sep=' ')
    tokenizer = RegexpTokenizer(r'\w+')
    words = tokenizer.tokenize(string)
    word_dist = nltk.FreqDist(words)
    return pd.DataFrame(word_dist.most_common(top), columns=['Word', 'Frequency'])

def nltk_word_frequencies(series: pd.Series, words=None, freq="15T"):
    if words is None:
        words = []

    def calc(df, column_name):
        string = df[column_name].str.lower().str.cat(sep=' ')
        tokenizer = RegexpTokenizer(r'\w+')
        words_distribution = nltk.FreqDist(tokenizer.tokenize(string))
        result = pd.Series()
        for word in words:
            result[word] = words_distribution[word]
        return result

    rv = series.groupby(pd.Grouper(freq=freq)).apply(calc, 'text')
    norm = series['id'].groupby(pd.Grouper(freq=freq)).count()
    for word in words:
        rv[word] = rv[word] / norm
        rv[word] = rv[word] / rv[word].max()
    return rv


import face_recognition
from tkinter import *
from PIL import Image, ImageTk

# config variables
IMAGE_LOCATION_KNOWN = "images/almaz_office.jpg"
# IMAGE_LOCATION_UNKNOWN = "images/almaz_bike.jpg"
IMAGE_LOCATION_UNKNOWN = "images/almazer.jpg"
IMAGE_SCALE = 200

known_image = face_recognition.load_image_file(IMAGE_LOCATION_KNOWN)
unknown_image = face_recognition.load_image_file(IMAGE_LOCATION_UNKNOWN)

known_encoding = face_recognition.face_encodings(known_image)
unknown_encoding = face_recognition.face_encodings(unknown_image)

face_locations_known = face_recognition.face_locations(
    face_recognition.load_image_file(IMAGE_LOCATION_KNOWN)
)
face_locations_unknown = face_recognition.face_locations(
    face_recognition.load_image_file(IMAGE_LOCATION_UNKNOWN)
)

results = face_recognition.compare_faces([known_encoding[0]], unknown_encoding[0], 0.5)

class Window(Frame):
    def __init__(self, master=None):
        Frame.__init__(self, master)
        self.master = master
        self.pack(fill=BOTH, expand=1)

        # Tkinter loads same image on its own
        image_known = Image.open(IMAGE_LOCATION_KNOWN)
        image_unknown = Image.open(IMAGE_LOCATION_UNKNOWN)

        top1, right1, bottom1, left1 = face_locations_known[0]
        top2, right2, bottom2, left2 = face_locations_unknown[0]

        cropped_image_known = image_known.crop(
            [left1 - 20, top1 - 20, right1 + 20, bottom1 + 20]
        )
        cropped_image_unknown = image_unknown.crop(
            [left2 - 20, top2 - 20, right2 + 20, bottom2 + 20]
        )

        cropped_image_known = cropped_image_known.resize(
            (IMAGE_SCALE, IMAGE_SCALE), Image.ANTIALIAS
        )
        cropped_image_unknown = cropped_image_unknown.resize(
            (IMAGE_SCALE, IMAGE_SCALE), Image.ANTIALIAS
        )

        render_known = ImageTk.PhotoImage(cropped_image_known)
        render_unknown = ImageTk.PhotoImage(cropped_image_unknown)

        label_known = Label(self, image=render_known)
        label_known.image = render_known

        label_unknown = Label(self, image=render_unknown)
        label_unknown.image = render_unknown

        label_known.place(x=50, y=50)
        label_unknown.place(x=300, y=50)

        text = Label(
            width=30,
            height=2,
            text="SAME PERSON" if results[0] else "NOT SAME PERSON",
            font="Arial 25",
            padx=0,
            pady=0,
        )
        text.pack()

# Rendering window with size based on grid
root = Tk()
app = Window(root)
root.wm_title("Check Person")
root.geometry("550x350")
root.mainloop()


#!/usr/bin/python3

import argparse
from typing import List, Tuple

from corrade import containers
from magnum import *
from magnum import math, trade

parser = argparse.ArgumentParser()
parser.add_argument('input')
parser.add_argument('output')
parser.add_argument('--offset', type=int, default=0)
parser.add_argument('--count', type=int, default=4)
args = parser.parse_args()

importer_manager = trade.ImporterManager()
importer_manager.metadata('DdsImporter').configuration['assumeYUpZBackward'] = True
importer_manager.metadata('KtxImporter').configuration['assumeOrientation'] = 'ruo'
importer = importer_manager.load_and_instantiate('AnyImageImporter')
importer.open_file(args.input)

image = importer.image2d(0)

assert image.is_compressed, "expected a compressed image"

print(f"format: {image.compressed_format}")

block_size = image.compressed_format.block_size
assert image.size % block_size.xy == Vector2i(0), "expected whole blocks"

block_data_size = image.compressed_format.block_data_size
# TODO use image.blocks once it exists
blocks = containers.StridedArrayView1D(image.data).expanded(0, (
    image.size.y//block_size.y,
    image.size.x//block_size.x,
    block_data_size
))

if image.compressed_format.name.startswith('BC'):
    decoder_name = 'BcDecImageConverter'
elif image.compressed_format.name.startswith('ETC') or \
     image.compressed_format.name.startswith('EAC'):
    decoder_name = 'EtcDecImageConverter'
else: assert False, f"unsupported format {image.compressed_format}"

converter_manager = trade.ImageConverterManager()
decoder = converter_manager.load_and_instantiate(decoder_name)
decoded = decoder.convert(image)
assert decoded.size == image.size
# TODO some better way to get this directly from pixels? i.e., want to access
#   the bytes and not the high-level type
# assert decoded.pixels.is_contiguous
decoded_pixel_size = decoded.format.size
decoded_pixel_data = containers.StridedArrayView1D(decoded.data).expanded(0, (
    image.size.y,
    image.size.x,
    decoded_pixel_size
))

block_count = decoded.size/block_size.xy

pixels = decoded.pixels

if decoded.format.channel_count == 1:
    default_min = math.inf
    default_max = -math.inf
    sum_ = lambda i: i
elif decoded.format.channel_count == 2:
    default_min = Vector2(math.inf)
    default_max = Vector2(-math.inf)
    sum_ = lambda i: i.sum()
elif decoded.format.channel_count == 3:
    default_min = Vector3(math.inf)
    default_max = Vector3(-math.inf)
    sum_ = lambda i: i.sum()
elif decoded.format.channel_count == 4:
    default_min = Vector4(math.inf)
    default_max = Vector4(-math.inf)
    sum_ = lambda i: i.sum()

block_ranges: List[Tuple[int, int, float]] = []

# This is extremely slow. Pybind11 ugh what are you doing?!
for y in range(block_count.y):
    oy = y*block_size.y
    for x in range(block_count.x):
        ox = x*block_size.x
        min_ = default_min
        max_ = default_max
        for by in range(block_size.y):
            for bx in range(block_size.x):
                min_ = math.min(pixels[oy + by, ox + bx], min_)
                max_ = math.max(pixels[oy + by, ox + bx], max_)

        block_ranges += [(x, y, sum_(max_ - min_))]

# Get most interesting blocks
most_interesting_blocks = list(reversed(sorted(block_ranges, key=lambda block: block[2])))[args.offset:(args.offset + args.count)]

print("    char blocks[]{")
for x, y, range_ in most_interesting_blocks:
    print("        /* [{}, {}], {:1.3f} */\n        {},".format(
        x, y, range_,
        ', '.join(["'\\x{:02x}'".format(ord(blocks[y, x, i])) for i in range(block_data_size)])
    ))
print("    };")

# Combine the decoded pixels from the most interesting blocks
data = bytearray(b'\0'*(args.count*block_size.x*block_size.y*decoded_pixel_size))
offset = 0
for x, y, range_ in most_interesting_blocks:
    oy = y*block_size.y
    ox = x*block_size.x
    for by in range(block_size.y):
        for bx in range(block_size.x):
            for i in range(decoded_pixel_size):
                data[offset] = ord(decoded_pixel_data[oy + by, ox + bx, i])
                offset += 1
assert offset == len(data)

# Write them to a file
converter = converter_manager.load_and_instantiate('AnyImageConverter')
converter.convert_to_file(ImageView2D(
    decoded.format,
    (block_size.x, block_size.y*args.count),
    data), args.output)


#!/usr/local/bin/python3

def part1(strings):
    def nice(string):
        vowel_count = len([c for c in string if c in 'aeiou'])

        if vowel_count < 3:
            return False

        for i, j in zip(string[:-1], string[1:]):
            if i == j:
                break
        else:
            return False

        naughties = ['ab', 'cd', 'pq', 'xy']
        for naughty in naughties:
            if naughty in string:
                return False

        return True

    return len([string for string in strings if nice(string)])

def part2(strings):
    def nice(string):
        for pair, rest in [(string[i:i + 2], string[i + 2:]) for i in range(len(string))]:
            if pair in rest:
                break
        else:
            return False

        for sub in [string[i:i + 3] for i in range(len(string) - 2)]:
            if sub[0] == sub[2]:
                break
        else:
            return False

        return True

    return len([string for string in strings if nice(string)])

def main():
    with open('day5.txt') as f:
        strings = [line.strip() for line in f]

    print(part1(strings))
    print(part2(strings))

if __name__ == '__main__':
    main()


from django.http import HttpResponse
from django.shortcuts import render
from tutorials.books.models import Book, BookLog
from tutorials.books.tasks import create_log

def index(request):
    books = Book.objects.all()
    return render(request, 'books/index.html', { 'books': books })

def book_log(request):
    logs = BookLog.objects.all()
    return render(request, 'books/book_log.html', { 'logs': logs })

def mark_read(request, book_id=None):
    if book_id:
        book = Book.objects.get(id=book_id)
        create_log.delay(request.user, book)
        return HttpResponse('Sent off for Processing')
    else:
        return HttpResponse('No ID match')


import cv2 as cv
import numpy as np
from operator import add
import matplotlib.pyplot as plt
from sklearn.cluster import AgglomerativeClustering

TT100K_CLASSES = (
    'p11', 'pl5', 'pne', 'il60', 'pl80', 'pl100', 'il80', 'po', 'w55',
    'pl40', 'pn', 'pm55', 'w32', 'pl20', 'p27', 'p26', 'p12', 'i5',
    'pl120', 'pl60', 'pl30', 'pl70', 'pl50', 'ip', 'pg', 'p10', 'io',
    'pr40', 'p5', 'p3', 'i2', 'i4', 'ph4', 'wo', 'pm30', 'ph5', 'p23',
    'pm20', 'w57', 'w13', 'p19', 'w59', 'il100', 'p6', 'ph4.5')

def region_cluster(regions, mask_shape):
    """
    层次聚类
    """
    regions = np.array(regions)
    centers = (regions[:, [2, 3]] + regions[:, [0, 1]]) / 2.0

    model = AgglomerativeClustering(
                n_clusters=None,
                linkage='complete',
                distance_threshold=min(mask_shape) * 0.1,
                compute_full_tree=True)

    labels = model.fit_predict(centers)

    cluster_regions = []
    for idx in np.unique(labels):
        boxes = regions[labels == idx]
        new_box = [min(boxes[:, 0]), min(boxes[:, 1]),
                   max(boxes[:, 2]), max(boxes[:, 3])]
        cluster_regions.append(new_box)

    return cluster_regions

def generate_box_from_mask(mask):
    """
    Args:
        mask: 0/1 array
    """
    box_all = []
    contours, _ = cv.findContours(mask, cv.RETR_TREE, cv.CHAIN_APPROX_NONE)
    for i in range(len(contours)):
        x, y, w, h = cv.boundingRect(contours[i])
#if w < 2 and h < 2:
#           continue
        box_all.append([x, y, x+w, y+h])
    return np.array(box_all)

def crop(mask_box, image_size):
    """
    Args:
        mask_box: list of box, [xmin, ymin, xmax, ymax]
        image_size: (width, height)
    Returns:
        chips: list of box
    """
    width, height = image_size

    chip_list = []
    for box in mask_box:
        box_w = box[2] - box[0]
        box_h = box[3] - box[1]
        box_cx = box[0] + box_w / 2
        box_cy = box[1] + box_h / 2

        if box_w < 100 and box_h < 100:
            chip_size = max(box_w, box_h)+150
        elif box_w < 150 and box_h < 150:
            chip_size = max(box_w, box_h)+50
        elif box_w < 200 and box_h < 200:
            chip_size = max(box_w, box_h)+100
        elif box_w < 300 and box_h < 300:
            chip_size = max(box_w, box_h)+100
        else:
            chip_size = max(box_w, box_h)

        chip = [box_cx - chip_size / 2, box_cy - chip_size / 2,
                box_cx + chip_size / 2, box_cy + chip_size / 2]

        shift_x = max(0, 0 - chip[0]) + min(0, width-1 - chip[2])
        shift_y = max(0, 0 - chip[1]) + min(0, height-1 - chip[3])

        chip = list(map(add, chip, [shift_x, shift_y]*2))
        chip_list.append([int(x) for x in chip])
    return chip_list

def region_box_generation(mask, image_size):
    regions = generate_box_from_mask(mask)
    if len(regions) > 1:
        regions = region_cluster(regions, mask.shape)
    regions = list(map(resize_box, regions,
                    [mask.shape[0]]*len(regions), [image_size[0]]*len(regions)))
    return crop(regions, image_size)

def enlarge_box(mask_box, image_size, ratio=2):
    """
    Args:
        mask_box: list of box
        image_size: (width, height)
        ratio: int
    """
    new_mask_box = []
    for box in mask_box:
        w = box[2] - box[0]
        h = box[3] - box[1]
        center_x = w / 2 + box[0]
        center_y = h / 2 + box[1]
        w = w * ratio / 2
        h = h * ratio / 2
        new_box = [center_x-w if center_x-w > 0 else 0,
                    center_y-h if center_y-h > 0 else 0,
                    center_x+w if center_x+w < image_size[0] else image_size[0]-1,
                    center_y+h if center_y+h < image_size[1] else image_size[1]-1]
        new_box = [int(x) for x in new_box]
        new_mask_box.append(new_box)
    return new_mask_box

def resize_box(box, original_size, dest_size):
    """
    Args:
        box: [xmin, ymin, xmax, ymax]
        original_size: int
        dest_size: int
    """
    ratio = dest_size / original_size
    box = np.array(box) * ratio
    return list(np.clip(box, 0, dest_size-1).astype(np.int32))

def overlap(box1, box2, thresh = 0.75):
    """ (box1 \cup box2) / box2
    Args:
        box1: [xmin, ymin, xmax, ymax]
        box2: [xmin, ymin, xmax, ymax]
    """
    matric = np.array([box1, box2])
    u_xmin = np.max(matric[:,0])
    u_ymin = np.max(matric[:,1])
    u_xmax = np.min(matric[:,2])
    u_ymax = np.min(matric[:,3])
    u_w = u_xmax - u_xmin
    u_h = u_ymax - u_ymin
    if u_w <= 0 or u_h <= 0:
        return False
    u_area = u_w * u_h
    box2_area = (box2[2] - box2[0]) * (box2[3] - box2[1])
    if u_area / box2_area < thresh:
        return False
    else:
        return True

def _boxvis(img, box_list, origin_img=None, binary=True):
    # if binary:
    #     ret, img = cv.threshold(img, 0, 255, cv.THRESH_BINARY)
    for box in box_list:
        cv.rectangle(img, (box[0], box[1]), (box[2], box[3]), 255, 4)
    plt.subplot(1, 2, 1); plt.imshow(img, cmap='gray')
    if not origin_img is None:
        for box in box_list:
            cv.rectangle(origin_img, (box[0], box[1]), (box[2], box[3]), 255, 4)
        plt.subplot(1, 2, 2); plt.imshow(origin_img[:, :, [2,1,0]])
    plt.show()
    # cv.namedWindow('a', cv.WINDOW_AUTOSIZE)
    # cv.imshow('a', binary)
    # key = cv.waitKey(0)
    # sys.exit(0)

def iou_calc1(boxes1, boxes2):
    """
    array
    :param boxes1: boxes1和boxes2的shape可以不相同，但是需要满足广播机制
    :param boxes2: 且需要保证最后一维为坐标维，以及坐标的存储结构为(xmin, ymin, xmax, ymax)
    :return: 返回boxes1和boxes2的IOU，IOU的shape为boxes1和boxes2广播后的shape[:-1]
    """
    boxes1 = np.array(boxes1)
    boxes2 = np.array(boxes2)

    boxes1_area = (boxes1[..., 2] - boxes1[..., 0]) * (boxes1[..., 3] - boxes1[..., 1])
    boxes2_area = (boxes2[..., 2] - boxes2[..., 0]) * (boxes2[..., 3] - boxes2[..., 1])

    # 计算出boxes1和boxes2相交部分的左上角坐标、右下角坐标
    left_up = np.maximum(boxes1[..., :2], boxes2[..., :2])
    right_down = np.minimum(boxes1[..., 2:], boxes2[..., 2:])

    # 计算出boxes1和boxes2相交部分的宽、高
    # 因为两个boxes没有交集时，(right_down - left_up) < 0，所以maximum可以保证当两个boxes没有交集时，它们之间的iou为0
    inter_section = np.maximum(right_down - left_up, 0.0)
    inter_area = inter_section[..., 0] * inter_section[..., 1]
    union_area = boxes1_area + 1e-16 + boxes2_area - inter_area
    IOU = 1.0 * inter_area / union_area
    return IOU

def iou_calc2(boxes1, boxes2):
    """
    array
    :param boxes1: boxes1和boxes2的shape可以不相同，但是需要满足广播机制
    :param boxes2: 且需要保证最后一维为坐标维，以及坐标的存储结构为(xmin, ymin, xmax, ymax)
    :return: 返回boxes1和boxes2的IOU，IOU的shape为boxes1和boxes2广播后的shape[:-1]
    """
    boxes1 = np.array(boxes1)
    boxes2 = np.array(boxes2)

    boxes1_area = (boxes1[..., 2] - boxes1[..., 0]) * (boxes1[..., 3] - boxes1[..., 1])
    boxes2_area = (boxes2[..., 2] - boxes2[..., 0]) * (boxes2[..., 3] - boxes2[..., 1])

    # 计算出boxes1和boxes2相交部分的左上角坐标、右下角坐标
    left_up = np.maximum(boxes1[..., :2], boxes2[..., :2])
    right_down = np.minimum(boxes1[..., 2:], boxes2[..., 2:])

    # 计算出boxes1和boxes2相交部分的宽、高
    # 因为两个boxes没有交集时，(right_down - left_up) < 0，所以maximum可以保证当两个boxes没有交集时，它们之间的iou为0
    inter_section = np.maximum(right_down - left_up, 0.0)
    inter_area = inter_section[..., 0] * inter_section[..., 1]
    IOU = 1.0 * inter_area / boxes2_area
    return IOU

def nms(prediction, score_threshold=0.005, iou_threshold=0.5, overlap_threshold=0.95):
    """
    :param prediction:
    (x, y, w, h, conf, cls)
    :return: best_bboxes
    """
    prediction = np.array(prediction)
    detections = prediction[(-prediction[:,4]).argsort()]
    # Iterate through all predicted classes
    unique_labels = np.unique(detections[:, -1])

    best_bboxes = []
    for cls in unique_labels:
        cls_mask = (detections[:, 5] == cls)
        cls_bboxes = detections[cls_mask]

        # python code
        while len(cls_bboxes) > 0:
            best_bbox = cls_bboxes[0]
            best_bboxes.append(best_bbox)
            cls_bboxes = cls_bboxes[1:]
            # iou
            iou = iou_calc1(best_bbox[np.newaxis, :4], cls_bboxes[:, :4])
            iou_mask = iou > iou_threshold
             # overlap
            overlap = iou_calc2(best_bbox[np.newaxis, :4], cls_bboxes[:, :4])
            overlap_mask = overlap > overlap_threshold

            weight = np.ones((len(iou),), dtype=np.float32)
            weight[iou_mask] = 0.0
            weight[overlap_mask] = 0.0

            cls_bboxes[:, 4] = cls_bboxes[:, 4] * weight
            score_mask = cls_bboxes[:, 4] > score_threshold
            cls_bboxes = cls_bboxes[score_mask]
    best_bboxes = np.array(best_bboxes)
    return best_bboxes


from datetime import datetime
from dateutil.parser import parse

def compareTimeDifference(first_timeRaw, timePeriodSeconds = 0, timePeriodDays = 0):
	#first_timeRaw - first time
	#timePeriod - time period witch need to be passed in order to return True
	#Function is true if time has past margin
	first_time = first_timeRaw
	second_time = datetime.now()
	first_time = datetime.strptime(str(first_time), '%Y-%m-%d %H:%M:%S.%f')#convert string to datetime object
	difference = second_time - first_time
	#print(difference.seconds, '    ', difference.days)
	if timePeriodDays != 0:
		if difference.seconds > timePeriodSeconds and difference.days >= timePeriodDays:
			return True
	else:
		if difference.seconds > timePeriodSeconds:
			return True
	return False

def unique(it):#filter unique data - entry will be registered only once all other same values will be discarded
#filter out only unique data, example: [1,1,4,3,6,3] --> [1,4,3,6]
	s = []
	for el in it:
		if el not in s:
			s.append(el)
	return s

def filterIfContain(dataList,keyWord):
	#print('--------keyWord--------->', keyWord)
	filteredTableNames = [table for table in dataList if not keyWord in table]
	print(filteredTableNames)
	return filteredTableNames

def is_date(string, fuzzy=False):
	#this is not reliable for date detection
    """
    Return whether the string can be interpreted as a date.

    :param string: str, string to check for date
    :param fuzzy: bool, ignore unknown tokens in string if True
    """
    try:
        parse(string, fuzzy=fuzzy)
        return True

    except ValueError:
        return False

def validateDate(date_text):
	#detect date based on provided format
	try:
		#datetime.strptime(date_text, '%Y-%m-%d')
		#28/6/2021
		#datetime.strptime(date_text, '%d/%m/%Y')
		datetime.strptime(date_text, '%m/%d/%Y')
		return True
	except ValueError:
		return False

def changeDateFormat(itemList):
	dateItemsIndexes = []
	for item in range(len(itemList[0])):
		if validateDate(itemList[0][item]) == True: dateItemsIndexes.append(item)
	newList = itemList

	for currlist in range(len(itemList)):
		for index in dateItemsIndexes:
			try:
				#print("------------------itemList[currlist][index]----------------->",itemList[currlist][index])
				item = datetime.strptime(itemList[currlist][index], '%m/%d/%Y')
				#item = datetime.strptime(itemList[currlist][index], '%d/%m/%Y')
				newList[currlist][index] = item.strftime('%Y-%m-%d')
			except:
				print('-------------------------->>>>-------------does not match format--<<<<------------------------------')
				print('item at index: ', itemList[currlist][index])
				print('index: ', index)
				print('liste: ', itemList[currlist])
	return newList

def isInt(s):
	try:
		int(s)
		return True
	except ValueError:
		return False

def isFloat(s):
	try:
		float(s)
		return True
	except ValueError:
		return False


from Bio import SeqIO
import pandas as pd
import os

def list_files(path):
    path_to_files = []
    for root, dir, files in os.walk(path):
        for f in files:
            if 'japan' in f:
                path_to_files.append(os.path.join(root, f))
    return path_to_files

def get_basename(path_to_file):
    filepath = os.path.splitext(path_to_file)
    base = filepath[0].split('/')[-1]
    return base

df = pd.read_csv('/data/san/data1/users/linda/crAss_DB/circular_genomes/japan4D/genbank_to_contig_map.csv',sep='\t',header=None)
df.columns = ['ncbi_accn','contig']
print(df.head())

japan_opt_translations = list_files('/data/san/data1/users/linda/crAss_DB/optimal_translations_a_i_jap_mon')
for fiLe in japan_opt_translations:
    fiLeName = get_basename(fiLe)
    corrected = f'/data/san/data1/users/linda/crAss_DB/circular_genomes/japan4D/corrected_transl_headers/{fiLeName}.faa'
    with open(fiLe) as original, open(corrected, 'w') as corrected:
        records = SeqIO.parse(fiLe, 'fasta')
        for record in records:
            if record.id:
                try:
                    print('\n')
                    print(record.id)
                    protein_no = record.id.rsplit('_',1)[1]
                    print(protein_no)
                    seq_to_search=(record.id.replace('_k141',''))
                    search_string=seq_to_search.replace('japan4D_','')
                    search_string=search_string.rsplit('_',1)[0]
                    print(search_string)
                    accn = (df.loc[df.contig.str.contains(search_string)]['ncbi_accn'].values[0])
                    print(accn)
                    record.id=f'{accn}_japan4D_{search_string}_{protein_no}'
                    record.description=record.id
                    SeqIO.write(record, corrected, 'fasta')
                except Exception as e:
                    print('printing ERROR')
                    print(f"{e}")
                    record.id=f'{accn}_japan4D_{search_string}'
                    print(record.id)


# -*- coding: utf-8 -*-
import os
import sys
import math
import logging
import numpy as np
from numpy import exp
from copy import deepcopy

import rmgpy.constants as constants
from rmgpy.statmech.vibration import HarmonicOscillator

from ape.sampling import SamplingJob
from ape.FitPES import from_sampling_result, cubic_spline_interpolations
from ape.schrodinger import SetAnharmonicH

class Statmech(object):
    """
    A class to solve shrodinger equation, evaluate partition function and related properties of 1-D PES by using statistical thermodynamics
    """
    def __init__(self, label, input_file, output_directory, Tlist=[298.15], P=100000, frequency_scale_factor=1, ncpus=None):
        self.label = label
        self.input_file = input_file
        self.output_directory = output_directory
        self.Tlist = Tlist
        self.P = P
        self.frequency_scale_factor = frequency_scale_factor
        self.ncpus = ncpus
        self.result_info = list()

    def load_save(self):
        self.csv_path = os.path.join(self.output_directory, '{}_samping_result.csv'.format(self.label))
        self.mode_dict, self.energy_dict, self.min_elect, self.rotors, job_keys = from_sampling_result(self.csv_path)
        n_rotors, n_vibs = 0, 0
        for mode in self.mode_dict.values():
            if mode['mode'] == 'tors': n_rotors += 1
            else: n_vibs += 1
        self.sampling = SamplingJob(self.label, self.input_file, output_directory=self.output_directory, ncpus=self.ncpus, rotors=self.rotors, **job_keys)
        logging.disable(50)
        self.sampling.parse()
        self.sampling.sampling(save_result=False)
        logging.disable(logging.NOTSET)
        self.is_QM_MM_INTERFACE = self.sampling.is_QM_MM_INTERFACE
        self.conformer = self.sampling.conformer
        self.raw_conformer = self.sampling.raw_conformer
        self.only_vib_conformer = self.sampling.only_vib_conformer
        self.zpe_of_Hohf = self.sampling.zpe
        e0 = self.min_elect * constants.E_h * constants.Na + self.sampling.zpe
        self.conformer.E0 = (e0, "J/mol")
        for mode in self.conformer.modes:
            if isinstance(mode, HarmonicOscillator):
                frequencies = mode.frequencies.value_si
                mode.frequencies = (frequencies * self.frequency_scale_factor, "cm^-1")
        self.spin_multiplicity = self.conformer.spin_multiplicity
        self.optical_isomers =self.conformer.optical_isomers
        self.symbols = self.sampling.symbols

        # Solve SE of 1-D PES and calculate E S G Cp
        self.polynomial_dict = cubic_spline_interpolations(self.energy_dict, self.mode_dict)

        # Extract whether this system is QM/MM system or not
        self.is_QM_MM_INTERFACE = self.sampling.is_QM_MM_INTERFACE

    def calcThermoOfEachMode(self, eig, N, mode, T):
        beta = 1/(constants.kB*T) * constants.E_h
        Q = 0
        Q_vib = 0
        E = 0
        dQ = 0
        ddQ = 0
        for i in range(N):
            Ei = eig[i]
            Q += exp(-beta*Ei)
            dQ += Ei*exp(-beta*Ei)*beta/T
            ddQ += -2*Ei*exp(-beta*Ei)*beta/pow(T,2) + pow(Ei,2)*exp(-beta*Ei)*pow(beta,2)/pow(T,2)
            E += Ei*exp(-beta*Ei)
            if i == 0:
                zpve = Ei
            # Measuring energy relative to the zero point vibration frequency
            dE = Ei - zpve
            Q_vib += exp(-beta*dE)
        E /= Q
        is_tors = True if self.mode_dict[mode]['mode'] == 'tors' else False
        if is_tors:
            omega = self.mode_dict[mode]['symmetry_number']
            Q /= omega
            Q_vib /= omega
            dQ /= omega
            ddQ /= omega

        E0 = eig[0]
        v = (eig[1]-eig[0]) * constants.E_h / constants.h / (constants.c * 100)
        #print(Q)

        F = -math.log(Q)/beta
        S = (E - F)/T
        Cv = (2/Q*dQ - T*pow(dQ/Q,2) + T/Q*ddQ)/beta

        return v, E0, E, S, F, Q, Q_vib, Cv

    def SolvEig(self, mode, T):
        Nbasis = 50
        Nbasis_prev = 0
        H_prev = None
        Qold =  np.log(sys.float_info[0])
        vold =  np.log(sys.float_info[0])
        converge = False
        while not converge:
            Nbasis += 1
            H = SetAnharmonicH(self.polynomial_dict, self.mode_dict, self.energy_dict, mode, Nbasis, N_prev=Nbasis_prev, H_prev=H_prev)
            Nbasis_prev = Nbasis
            H_prev = deepcopy(H)
            eig, v =np.linalg.eigh(H)
            v, E0, E, S, F, Q, Q_vib, Cv = self.calcThermoOfEachMode(eig, Nbasis, mode, T)

            if Qold == np.log(sys.float_info[0]):
                self.result_info.append("# \n# \t %d \t\t-\t\t-" % Nbasis) #first run
                logging.debug("# \t {} \t\t-\t\t-".format(Nbasis))
            else:
                self.result_info.append("# \n# \t %d \t\t %.10f \t\t %.10f" % (Nbasis, abs(Q-Qold), abs(v-vold)))
                logging.debug("# \t {:d} \t\t {:.10f} \t\t {:.10f}".format(Nbasis, abs(Q-Qold), abs(v-vold)))

            if ((abs(Q-Qold)<1e-4) and (abs(v-vold)<1e-2)):
                self.result_info.append("# Convergence criterion met")
                self.result_info.append("# ------------------------------------")
                converge = True
                self.result_info.append("# Frequency (cm-1): %.10f" % v)
                self.result_info.append("# Zero point vibrational energy (hartree): %.10f" % E0)
                self.result_info.append("# Energy (hartree): %.10f" % E )
                self.result_info.append("# Entropy (hartree/K): %.10f" % S)
                self.result_info.append("# Free energy (hartree): %.10f" % F)
                self.result_info.append("# Partition function: %.10f" % Q)
                hartree2kcalmol = constants.E_h * constants.Na / 4184
                E0 *= hartree2kcalmol
                E *= hartree2kcalmol
                S *= hartree2kcalmol * 1000
                F *= hartree2kcalmol
                Cv *= hartree2kcalmol * 1000
                '''
                print("Frequency (cm-1): ",v)
                print("Zero point vibrational energy (kcal/mol): ",E0)
                print("Energy (kcal/mol): ",E )
                print("Entropy (cal/mol/K): ",S)
                print("Free energy (kcal/mol): ",F)
                print("Partition function: ",Q)
                '''

            Qold = Q
            vold = v
        return v, E0, E, S, F, Q, Q_vib, Cv


import RPi.GPIO as GPIO
import smbus
import signal
import sys
import time
import threading
from enum import Enum

class TachometerState(Enum):
    SPLASH_SCREEN = 0
    MENU_OPTIONS = 1
    MEASURE_FREQUENCY = 2
    INPUT_FREQUENCY = 3

def event_callback(channel):
    global lt, count, time_difference
    count = (count + 1) % 50
    t = time.time_ns()

    if count == 0:
        flash()
    time_difference = t - lt
    lt = t

def flash():
    lock.acquire()
    GPIO.output(STROBE_TRIGGER, GPIO.LOW)
    GPIO.output(STROBE_TRIGGER, GPIO.HIGH)
    lock.release()

def read():
    print(bus.read_byte_data(0x30, 0))

def locking_read_i2c_THREAD(address, byte):
    global I2C_READ_BYTE_GLOBAL

    lock.acquire()
    I2C_READ_BYTE_GLOBAL = bus.read_byte_data(address, byte)
    lock.release()

def read_i2c(address, byte):
    t = threading.Thread(target=locking_read_i2c_THREAD, args=(address, byte), daemon=True)
    t.start()
    t.join()

    return I2C_READ_BYTE_GLOBAL

def check_input():
    # key = read_i2c(keyboard_address, 0)
    t = threading.Thread(target=locking_read_i2c_THREAD, args=(keyboard_address, 0), daemon=True)
    t.start()
    t.join()

    key = I2C_READ_BYTE_GLOBAL

    c = None
    if key != 0:
        c = chr(key)

    return c

def debounced_check():
    global last_char

    k = check_input()
    if k == last_char:
        last_char = k
        return None

    last_char = k
    return k

def wait_for_input():
    global last_char
    k = check_input()

    while k == None or k == last_char:
        last_char = k
        k = check_input()

    last_char = k

    return k

def signal_handler(signal, frame):
    GPIO.cleanup()
    sys.exit(0)

I2C_READ_BYTE_GLOBAL = 0
last_char = None

state = TachometerState.SPLASH_SCREEN
PHOTOGATE_GPIO = 22
STROBE_TRIGGER = 6
keyboard_address = 0x30
bus = smbus.SMBus(1)

lock = threading.Lock() # The talking stick.

lt = time.time_ns()
count = 0
time_difference = sys.maxsize

GPIO.setmode(GPIO.BCM)
GPIO.setup(PHOTOGATE_GPIO, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(STROBE_TRIGGER, GPIO.OUT)

GPIO.add_event_detect(PHOTOGATE_GPIO, GPIO.FALLING, callback=event_callback)

signal.signal(signal.SIGINT, signal_handler)

try:
    while True:

        if state == TachometerState.SPLASH_SCREEN:
            print("Loading")
            time.sleep(1)
            state = TachometerState.MENU_OPTIONS
        elif state == TachometerState.MENU_OPTIONS:
            print("Menu options")

            k = debounced_check()
            print(I2C_READ_BYTE_GLOBAL)
            if k == '1':
                state = TachometerState.MEASURE_FREQUENCY
            elif k == '2':
                state = TachometerState.INPUT_FREQUENCY

        elif state == TachometerState.MEASURE_FREQUENCY:
            print("Measure frequency")
            time.sleep(1)
        elif state == TachometerState.INPUT_FREQUENCY:
            print("Input frequency")
            time.sleep(1)

        time.sleep(0.2)
except Exception as e:
    print(str(e))
    GPIO.cleanup()
    sys.exit(0)


# number_of_rows = int(input())
# board = list()
# count = 0
# boards = list()
# for row in range(number_of_rows):
#     board.append(input().split(' '))
# for row, col in enumerate(board):
#     new_board = list()
#     for i in range(len(col)):
#         if col[i] == '.':
#             new_board.append(i)
#     boards.append(new_board)
#
# visited = []
# for row, col in enumerate(boards):
#     for i in range(len(col)):
#         for ind in range(1, number_of_rows-1):
#             if col[i]+1 in row[ind]:
#
#     # print(f"{row}{col}")

n = int(input())
matrix_main = []

for _ in range(n):
    matrix_main.append(input().split())

m = len(matrix_main[0])

# stores information about  which cell
# are already visited in a particular BFS
visited = [[0 for j in range(m)] for i in range(n)]

# result stores the final result grid
result = [[0 for j in range(m)] for i in range(n)]

# stores the count of cells in the largest
# connected component
COUNT = 0

# Function checks if a cell is valid i.e it
# is inside the grid and equal to the key
def is_valid(x, y, key, matrix_main):
    if (x < n and y < m and x >= 0 and y >= 0):
        if (visited[x][y] == 0 and matrix_main[x][y] == key):
            return True
        else:
            return False

    else:
        return False

# BFS to find all cells in
# connection with key = input[i][j]
def BFS(x, y, i, j, matrix_main):
    global COUNT

    # terminating case for BFS
    if (x != y):
        return

    if x != ".":
        return

    visited[i][j] = 1
    COUNT += 1

    # x_move and y_move arrays
    # are the possible movements
    # in x or y direction
    x_move = [0, 0, 1, -1]
    y_move = [1, -1, 0, 0]

    # checks all four points connected with input[i][j]
    for u in range(4):

        if (is_valid(i + y_move[u], j + x_move[u], x, matrix_main)):
            BFS(x, y, i + y_move[u], j + x_move[u], matrix_main)

# called every time before a BFS
# so that visited array is reset to zero
def reset_visited():
    for i in range(n):
        for j in range(m):
            visited[i][j] = 0

# If a larger connected component
# is found this function is called
# to store information about that component.
def reset_result(key, matrix_main):
    for i in range(n):
        for j in range(m):
            if (visited[i][j] != 0 and matrix_main[i][j] == key):
                result[i][j] = visited[i][j]
            else:
                result[i][j] = 0

# function to print the result
def print_result(res):
    print(res)

# function to calculate the largest connected
# component
def computeLargestConnectedGrid(matrix_main):
    global COUNT
    current_max = -10000000000

    for i in range(n):
        for j in range(m):
            reset_visited()
            COUNT = 0

            # checking cell to the right
            if (j + 1 < m):
                BFS(matrix_main[i][j], matrix_main[i][j + 1], i, j, matrix_main)

            # updating result
            if (COUNT >= current_max):
                current_max = COUNT
                reset_result(matrix_main[i][j], matrix_main)

            reset_visited();
            COUNT = 0;

            # checking cell downwards
            if (i + 1 < n):
                BFS(matrix_main[i][j], matrix_main[i + 1][j], i, j, matrix_main)

            # updating result
            if (COUNT >= current_max):
                current_max = COUNT
                reset_result(matrix_main[i][j], matrix_main)

    print_result(current_max)

computeLargestConnectedGrid(matrix_main)

Fundamentals/Lists Advanced - More Exercises/dots.py


#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""Tension webapp.

Usage:
  tension_webapp [--port=<port>] [--localhost]
  tension_webapp -h | --help
  tension_webapp --version

Options:
  --localhost   Only allow connections from this computer.
  -h --help     Show this screen.
  --version     Show version.

"""
from docopt import docopt
from tension.webapp import tension_app

def main():
    args = docopt(__doc__, version="Tension webapp 1.0")
    port = int(args.get("--port", False) or 5000)
    host = "localhost" if args.get("--localhost", False) else "0.0.0.0"

    print("tension webapp started on {}:{}".format(host, port))

    tension_app.run(host=host, port=port, debug=True)

if __name__ == "__main__":
    main()


import numpy
import scipy.special
import matplotlib.pyplot

class neuralNetwork():

    def __init__(self,inputnodes,hiddennodes,outputnodes,learningrate):
        self.inodes = inputnodes
        self.hnodes = hiddennodes
        self.onodes = outputnodes
        # 学习率
        self.lr = learningrate

        # 链接权重矩阵
        self.wih = (numpy.random.rand(self.hnodes, self.inodes) - 0.5)
        self.who = (numpy.random.rand(self.onodes, self.hnodes) - 0.5)

        # 使用正态分布采样权重
        # self.wih = numpy.random.normal(0.0,pow(self.hnodes,-0.5),(self.hnodes,self.inodes))
        # self.who = numpy.random.normal(0.0,pow(self.onodes,-0.5),(self.onodes,self.hnodes))

        # 激活函数
        self.activation_function = lambda x: scipy.special.expit(x)

    def train(self,inputs_list,targets_list):

        # 初始化为array
        inputs = numpy.array(inputs_list, ndmin=2).T
        targets = numpy.array(targets_list, ndmin=2).T

        hidden_inputs = numpy.dot(self.wih, inputs)
        hidden_outputs = self.activation_function(hidden_inputs)
        final_inputs = numpy.dot(self.who, hidden_outputs)
        final_outputs = self.activation_function(final_inputs)

        # 误差
        output_errors = targets - final_outputs
        hidden_errors = numpy.dot(self.who.T,output_errors)

        # 更新权重
        # 隐藏层与输出层
        self.who += self.lr * numpy.dot((output_errors * final_outputs * (1.0 - final_outputs)),numpy.transpose(hidden_outputs))
        # 输入层与隐藏层
        self.wih += self.lr * numpy.dot((hidden_errors * hidden_outputs * (1.0 - hidden_outputs)),numpy.transpose(inputs))

    def query(self,inputs_list):

        inputs = numpy.array(inputs_list,ndmin=2).T
        hidden_inputs = numpy.dot(self.wih,inputs)
        hidden_outputs = self.activation_function(hidden_inputs)
        final_inputs = numpy.dot(self.who,hidden_outputs)
        final_outputs = self.activation_function(final_inputs)
        return final_outputs

if __name__ == '__main__':

    input_nodes = 784 # 28x28
    hidden_nodes = 100
    output_nodes = 10
    learning_rate = 0.3

    n = neuralNetwork(input_nodes,hidden_nodes,output_nodes,learning_rate)

    with open('mnist_train_100.csv','r') as f:
        train_list = f.readlines()

    for record in train_list:

        all_values = record.split(',')
        inputs = (numpy.asfarray(all_values[1:])/255.0 * 0.99)+0.01
        targets = numpy.zeros(output_nodes) + 0.01
        targets[int(all_values[0])] = 0.99
        n.train(inputs,targets)

    with open('mnist_test_10.csv', 'r') as f:
        test_list = f.readlines()

    for record in test_list:

        all_values = record.split(',')
        image_array = numpy.asfarray(all_values[1:]).reshape((28,28))
        matplotlib.pyplot.imshow(image_array,cmap='Greys',interpolation='None')
        matplotlib.pyplot.show()

        result = n.query((numpy.asfarray(all_values[1:])/255.0 * 0.99)+0.01)
        print(str(numpy.argmax(result)),str(all_values[0]))


import sys
from io import StringIO

test1 = '''2
1, 2, 3
4, 5, 6'''
test2 = '''3
10, 2, 21, 4
5, 20, 41, 9
6, 2, 4, 99'''

sys.stdin = StringIO(test2)

rows = int(input())
matrix = []
for _ in range(rows):
    line = [int(x) for x in input().split(', ')]
    matrix.extend(line)
print(matrix)

Python Advanced/Multidimensional Lists - Lab/flattening_matrix.py