# Author: Leyla Noroozbabaee
# Date: 12/12/2021
# To reproduce Figure 1 from original paper, the python file 'Fig1_sim.py' should be run.

import matplotlib.pyplot as plt
import pandas as pd
from sklearn import preprocessing
import numpy as np
# Figure name
prefilename = 'Fig1'

# Set figure dimension (width, height) in inches.
fw, fh = 15, 10
# Set subplots
subpRow, subpCol = 3, 2
ax, lns = {}, {}
# This gives list with the colors from the cycle, which you can use to iterate over.
cycle = plt.rcParams['axes.prop_cycle'].by_key()['color']
# Set subplots
lfontsize, labelfontsize = 10, 15  # legend, label fontsize
fig, axs = plt.subplots(subpRow, subpCol, figsize=(fw, fh), facecolor='w', edgecolor='k')
fig.subplots_adjust(hspace=.3, wspace=.3)
axs = axs.ravel()
var_name = ['Time','fss','dss','f1tc','f2tc','dtc','ical','v','I_Ca_tot']
# Set ylable
tit = ['Steady state','Steady state','Time constant (ms)','Time constant (ms)','I (normalised)','I (normalised)']
# Set Color
cycle = plt.rcParams [ 'axes.prop_cycle' ].by_key() [ 'color' ]

filename = '%s.csv' % (prefilename)
data = pd.read_csv(filename)
print('filename', filename)
data = pd.read_csv(filename)
time = data [ var_name[0] ]
fss_data = data [var_name[1]]
dss_data = data [var_name[2]]
f1tc_data = data [var_name[3]]
f2tc_data = data [var_name[4]]
dtc_data = data [var_name[5]]
ical_data = data [var_name[6]]
v_data = data [var_name[7]]
axs[0].plot( v_data, pow(dss_data,2), 'b')
axs[1].plot( v_data, fss_data, 'b')
axs[2].plot( v_data, dtc_data, 'b')
axs[3].plot( v_data, f1tc_data, 'b',  v_data, f2tc_data, 'b')

I_V = []
prefilename = 'Fig1_5'
filename5 = '%s_%s.csv' % (prefilename, 5)
data5 = pd.read_csv(filename5)
max_ical_data5 = max(abs(data5 ['ical']))
for i in range(11):
    filename = '%s_%s.csv' % (prefilename, i)
    data = pd.read_csv(filename)
    time = data ['Time']
    ical_data = data ['ical']
    max_ical_data = max(data ['ical'])
    print(' max_ical_data ', max_ical_data5)
    if  5<=i<11:
         axs [4].plot( time, ical_data/max_ical_data5, color=cycle [i % 4])
    if max_ical_data  < 0:
        MAX_I_V = (min(ical_data / max_ical_data5))
    else:
        MAX_I_V = (max(ical_data / max_ical_data5))
    I_V.append(MAX_I_V )

V = [50, 40, 30, 20, 10, 0, -10, -20, -30, -40, -50]
print(I_V)
axs[5].plot(V, I_V, '-b')

# To add the extracted data from original paper to your plot, modify the path to have access to the
# "Extracted_data"
prefilename = 'Fig1'
for i in range(6):
    prefilename = 'Extracted_Data/Fig1'
    filename = '%s_%s.csv' % (prefilename, i)
    data = pd.read_csv(filename)
    if i == 3 :
        x_name2 = 'x'
        y_name2 = 'Curve2'
        y_data2 = data [ y_name2 ]
        x_data2 = data [ x_name2 ]
        axs [ i ].plot(x_data2, y_data2, 'k^')
    elif i== 4:
        for j in range(1,6):
            filename = 'Extracted_Data/Fig1_4.csv'
            data = pd.read_csv(filename)
            x_name = 'x'
            y_name = 'Curve%s' %(j)
            x_data = data [ x_name ]
            y_data = data [ y_name ]
            axs [ i ].plot(x_data, y_data, '^')

    x_name = 'x'
    y_name = 'Curve1'
    x_data = data [ x_name ]
    y_data = data [ y_name ]

    axs [ i ].set_ylabel('%s' % (tit [ i ]), fontsize=labelfontsize)
    if i!=4:
       axs [ i ].set_xlim([ -80, 50 ])
       axs [ i ].set_xlabel('V (mV)', fontsize=labelfontsize)
    else:
        axs [ i ].set_xlim([ 0, 60 ])
        axs [ i ].set_xlabel('Time (ms)', fontsize=labelfontsize)
    axs [ i ].plot(x_data, y_data, 'k^')

figfiles = 'Figure_1.png'
plt.savefig(figfiles)
plt.show()


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

import json
from utils.price_comparison import PriceComparison
from exchanges_util.coin_exchange_utils import CoinExchangeUtils
from exchanges_util.trade_satoshi_utils import TradeSatoshiUtils
from exchanges_util.binance_utils import BinanceUtils
from exchanges_util.bittrex_utils import BittrexUtils

# 独自クラスインスタンス化
price_comparison = PriceComparison()
trade_satoshi = TradeSatoshiUtils()
coin_exchange = CoinExchangeUtils()
binance = BinanceUtils()
bittrex = BittrexUtils()

# configファイル読み込み
arbitrage_parameter_file = 'config/arbitrage_parameter.json'
arbitrage_parameter = json.load(open(arbitrage_parameter_file, 'r'))
RATIO = arbitrage_parameter['output_limit_ratio']

if __name__ == '__main__':

    # 価格情報を格納するlist
    price_list = []

    # satoshi価格取得-----------------------
    satoshi_json = trade_satoshi.get_market_price()
    price_list.append(satoshi_json)

    # coin_exchange価格取得------------------
    coin_exchange_json = coin_exchange.get_market_price()
    price_list.append(coin_exchange_json)

    # binance価格取得------------------
    binance_json = binance.get_market_price()
    price_list.append(binance_json)

    # bittrex価格取得------------------
    bittrex_json = bittrex.get_market_price()
    price_list.append(bittrex_json)

    # 指定した倍率以上の通貨ペアと取引所を表示-----------------------------
    price_comparison.print_arbitrage_list(RATIO, price_list)


# Numa eleição existem três candidatos. Faça um programa que peça o número total de eleitores. Peça para cada eleitor votar e ao final mostrar o número de votos de cada candidato.

c1 = 0
c2 = 0
c3 = 0

n = int(input("Digite o número total de eleitores: "))

for i in range(n):
    voto = input("Digite em qual candidato votar: (c1,c2,c3) ")
    while voto != "c1" and voto != "c2" and voto != "c3":
        voto = input("Voto inválido.\nDigite em qual candidato votar: (c1,c2,c3) ")
    if voto == "c1":
        c1 +=1
    elif voto == "c2":
        c2 +=1
    elif voto == "c3":
        c3 +=1
print("Total de votos para c1:%d\nTotal de votos para c2:%d\nTotal de votos para c3:%d" % (c1,c2,c3))

Lista de exercícios pybrasil/Repetição/26.py


import re

import zope.interface
import zope.schema

from pmr2.app.schema.interfaces import InvalidObjectId
from pmr2.app.schema.interfaces import IObjectId, IWorkspaceList, ICurationDict
from pmr2.app.schema.interfaces import ITextLineList

_isobjectid = re.compile(r'^[a-zA-Z0-9_\.\-]*$').match

class ObjectId(zope.schema.BytesLine):
    """\
    This is implemented because zope.schema.Id does not work as
    advertised.
    """

    zope.interface.implements(IObjectId)

    def _validate(self, value):
        """\
        >>> id = ObjectId(__name__='test')
        >>> id.validate("foo")
        >>> id.validate("foo.bar")
        >>> id.validate("foo/bar")
        Traceback (most recent call last):
        ...
        InvalidObjectId: foo/bar
        >>> id.validate("foo bar")
        Traceback (most recent call last):
        ...
        InvalidObjectId: foo bar
        >>> id.validate("http://www.example.org/foo/bar")
        Traceback (most recent call last):
        ...
        InvalidObjectId: http://www.example.org/foo/bar
        """

        super(ObjectId, self)._validate(value)
        if _isobjectid(value):
            return

        raise InvalidObjectId(value)

class WorkspaceList(zope.schema.List):
    """\
    Workspace listing field.
    """

    zope.interface.implements(IWorkspaceList)

class CurationDict(zope.schema.Dict):
    """\
    Curation dictionary.
    """

    zope.interface.implements(
        ICurationDict,
        zope.schema.interfaces.IFromUnicode,
    )

    key_type = zope.schema.TextLine(
        title=u'Key',
    )
    value_type = zope.schema.List(
        title=u'Value',
        value_type=zope.schema.TextLine(title=u'Values',),
    )

    def fromUnicode(self, u):
        """\
        >>> d = CurationDict()
        >>> d.fromUnicode(u'key:value')
        {u'key': [u'value']}
        >>> result = d.fromUnicode(u'key:value\\nkey2:type2\\nkey:value2')
        >>> result[u'key']
        [u'value', u'value2']
        >>> result[u'key2']
        [u'type2']
        """

        result = {}
        for i in u.splitlines():
            k, v = i.split(u':', 1)
            if k not in result:
                result[k] = []
            result[k].append(v)
        return result

class TextLineList(zope.schema.List):
    """\
    Workspace listing field.
    """

    zope.interface.implements(
        ITextLineList,
        zope.schema.interfaces.IFromUnicode,
    )
    value_type = zope.schema.TextLine(title=u'Values',),

    def fromUnicode(self, u):
        """\
        >>> d = TextLineList()
        >>> d.fromUnicode(u'')
        []
        >>> d.fromUnicode(u'test')
        [u'test']
        >>> d.fromUnicode(u'test\\ntest2')
        [u'test', u'test2']
        """

        return u.splitlines()


#座間市HP ハザードマップPDFをDL href要素のテキスト表示部分を抜き出して保存
#座間市HP　https://www.city.zama.kanagawa.jp/www/contents/1613713435129/index.html
#1.ハザードマップ（洪水浸水想定区域凡例等）(647KB)(PDF文書)　だったら、
#1.ハザードマップ（洪水浸水想定区域凡例等）(647KB)(PDF文書).pdfで保存する
import requests
from bs4 import BeautifulSoup
from urllib.parse import urljoin
from pathlib import Path
import time

url = "https://www.city.zama.kanagawa.jp/www/contents/1613713435129/index.html"
request = requests.get(url)
print(request.status_code)

#ハザードマップはa hrefに保存されている
soup = BeautifulSoup(request.content,"html.parser")
a_elments = soup.select("a")
print(a_elments)

#hrefのリンクから、pdfファイルのリンクを抜き出す
pdf_url_list = []
#href文書を抜き出す
dic_txtname = {}

#https://www.twilio.com/blog/web-scraping-and-parsing-html-in-python-with-beautiful-soup-jp
#soup.find_all()はWebスクレイピングで最もよく使われるメソッドです。これによりページのすべてのハイパーリンクを反復してURLを出力できます。
for link in soup.find_all("a"):
    href = link.get("href")
    if href.endswith(".pdf"):
        #print(href)
        pdf_url = urljoin(url,href)
        print(pdf_url)
        pdf_url_list.append(pdf_url)
        lntxt = link.getText()
        dic_txtname[pdf_url] = lntxt

print(dic_txtname)
save_folder_path = Path("../learning/Download_Zama")
save_folder_path.mkdir(exist_ok=True)

for r in pdf_url_list:
    r_file = requests.get(r)
    if r_file.status_code == 200:
        print("yes")
        for k,v in dic_txtname.items():
            if k == r :
                save_file = Path(v + ".pdf")
                print(save_file)
                save_path = save_folder_path / save_file
                with save_path.open("wb") as f:
                    f.write(r_file.content)
    else:
        print("no")

    time.sleep(3)


from pydantic.error_wrappers import ValidationError

from db.database import Database
from models.base_record_manager import BaseRecordManager
from models.service_account.service_account_model import (
    SERVICE_ACCOUNT_MODEL_NAME, ServiceAccount, ServiceAccountCreate,
    ServiceAccountPartial)

class ServiceAccountManager(BaseRecordManager):
    """ServiceAccountManager to handle CRUD functionality"""

    model = ServiceAccount
    model_name = SERVICE_ACCOUNT_MODEL_NAME

    @classmethod
    def create(cls, db: Database, record: ServiceAccountCreate) -> ServiceAccount:
        """Creates a new ServiceAccount after validating subjects.

        Arguments:
            db {Database} -- Database connection
            record {ServiceAccountCreate} -- New ServiceAccount data

        Returns:
            ServiceAccount -- newly created service_account
        """
        existing_service_account = ServiceAccountManager.find_by_name(
            db, record.metadata.name)
        if existing_service_account:
            raise ValidationError(
                "ServiceAccount with name [%s] already exists" % record.metadata.name)

        return super(ServiceAccountManager, cls).create(db, record)

    @classmethod
    def update(cls, db: Database, record_uuid: str, record: ServiceAccountPartial) -> ServiceAccount:
        """Updates the existing ServiceAccount after validating data

        Arguments:
            db {Database} -- Database connection
            record_uuid {str} -- unique record uuid
            record {BaseModel} -- updating record

        Returns:
            BaseRecord -- Updated record
        """
        existing_service_account = cls.find_by_uuid(db, record_uuid)
        updated_record = cls.model(**record.dict(), uuid=record_uuid)
        if updated_record.metadata.name != existing_service_account.metadata.name:
            if ServiceAccountManager.find_by_name(db, updated_record.metadata.name):
                raise ValidationError(
                    "ServiceAccount with name [%s] already exists" % record.metadata.name)

        return super(ServiceAccountManager, cls).update(db, record_uuid, record)


#!/usr/bin/env python3

import logging
logger = logging.getLogger()
logger.setLevel(level=logging.ERROR) #we set this to error before importing vpype to ignore the nasty output "WARNING:root:!!! `vpype.Length` is deprecated, use `vpype.LengthType` instead."

import sys
import os
from lxml import etree

import inkex
from inkex import transforms, bezier, PathElement
from inkex.paths import CubicSuperPath, Path
from inkex.command import inkscape

import vpype
import vpype_viewer
from vpype_viewer import ViewMode
from vpype_cli import execute

logger = logging.getLogger()
logger.setLevel(level=logging.WARNING) #after importing vpype we enabled logging again

import warnings # we import this to suppress moderngl warnings from vpype_viewer

from shapely.geometry import LineString, Point

"""
Extension for InkScape 1.X
Author: Mario Voigt / FabLab Chemnitz
Mail: mario.voigt@stadtfabrikanten.org
Date: 02.04.2021
Last patch: 06.06.2021
License: GNU GPL v3

This piece of spaghetti-code, called "vpypetools", is a wrapper to pass (pipe) line elements from InkScape selection (or complete canvas) to vpype.
It allows to run basic commands on the geometry. The converted lines are getting pushed back into InkScape.
vpypetools allows to enable some important adjusters and debugging settings to get the best out of it.

vpypetools is based on
 - Aaron Spike's "Flatten Bezier" extension, licensed by GPL v2
 - a lot of other extensions to rip off the required code pieces ;-)
 - used (tested) version of vpype: commit id https://github.com/abey79/vpype/commit/0b0dc8dd7e32998dbef639f9db578c3bff02690b (29.03.2021)
 - used (tested) version of vpype occult: commit id https://github.com/LoicGoulefert/occult/commit/2d04ca57d69078755c340066c226fd6cd927d41e (04.02.2021)

CLI / API docs:
- https://vpype.readthedocs.io/en/stable/api/vpype_cli.html#module-vpype_cli
- https://vpype.readthedocs.io/en/stable/api/vpype.html#module-vpype

Todo's
- find some python code to auto-convert strokes and objects to paths (for input and for output again)
- remove fill property of converted lines (because there is no fill anymore) without crashing Inkscape ...
- as we use flatten() we modify the original path. rewrite to avoid modifications to original path
"""

class vpypetools (inkex.EffectExtension):

    def add_arguments(self, pars):

        # Line Sorting
        pars.add_argument("--linesort", type=inkex.Boolean, default=False)
        pars.add_argument("--linesort_no_flip", type=inkex.Boolean, default=False, help="Disable reversing stroke direction for optimization")

        # Line Merging
        pars.add_argument("--linemerge", type=inkex.Boolean, default=False)
        pars.add_argument("--linemerge_tolerance", type=float, default=0.500, help="Maximum distance between two line endings that should be merged (default 0.500 mm)")
        pars.add_argument("--linemerge_no_flip", type=inkex.Boolean, default=False, help="Disable reversing stroke direction for merging")

        # Trimming
        pars.add_argument("--trim", type=inkex.Boolean, default=False)
        pars.add_argument("--trim_x_margin", type=float, default=0.000, help="trim margin - x direction (mm)") # keep default at 0.000 to keep clean bbox
        pars.add_argument("--trim_y_margin", type=float, default=0.000, help="trim margin - y direction (mm)") # keep default at 0.000 to keep clean bbox

        # Relooping
        pars.add_argument("--reloop", type=inkex.Boolean, default=False)
        pars.add_argument("--reloop_tolerance", type=float, default=0.500, help="Controls how close the path beginning and end must be to consider it closed (default 0.500 mm)")

        # Multipass
        pars.add_argument("--multipass", type=inkex.Boolean, default=False)
        pars.add_argument("--multipass_count", type=int, default=2, help="How many passes for each line (default 2)")

        # Filter
        pars.add_argument("--filter", type=inkex.Boolean, default=False)
        pars.add_argument("--filter_tolerance", type=float, default=0.050, help="Tolerance used to determined if a line is closed or not (default 0.050 mm)")
        pars.add_argument("--filter_closed", type=inkex.Boolean, default=False, help="Keep closed lines")
        pars.add_argument("--filter_not_closed", type=inkex.Boolean, default=False, help="Keep open lines")
        pars.add_argument("--filter_min_length_enabled", type=inkex.Boolean, default=False, help="filter by min length")
        pars.add_argument("--filter_min_length", type=float, default=0.000, help="Keep lines whose length isn't shorter than value")
        pars.add_argument("--filter_max_length_enabled", type=inkex.Boolean, default=False, help="filter by max length")
        pars.add_argument("--filter_max_length", type=float, default=0.000, help="Keep lines whose length isn't greater than value")

        # Split All
        pars.add_argument("--splitall", type=inkex.Boolean, default=False)

        # Plugin Occult
        pars.add_argument("--plugin_occult", type=inkex.Boolean, default=False)
        pars.add_argument("--plugin_occult_tolerance", type=float, default=0.01, help="Max distance between start and end point to consider a path closed (default 0.01 mm)")
        pars.add_argument("--plugin_occult_keepseparatelayer", type=inkex.Boolean, default=False, help="Put occulted lines to separate layer")

        # Plugin Deduplicate
        pars.add_argument("--plugin_deduplicate", type=inkex.Boolean, default=False)
        pars.add_argument("--plugin_deduplicate_tolerance", type=float, default=0.01, help="Max distance between points to consider them equal (default 0.01 mm)")
        pars.add_argument("--plugin_deduplicate_keepseparatelayer", type=inkex.Boolean, default=False, help="Put duplicate lines to separate layer")

        # Free Mode
        pars.add_argument("--tab")
        pars.add_argument("--freemode", type=inkex.Boolean, default=False)
        pars.add_argument("--freemode_cmd1", default="")
        pars.add_argument("--freemode_cmd1_enabled", type=inkex.Boolean, default=True)
        pars.add_argument("--freemode_cmd2", default="")
        pars.add_argument("--freemode_cmd2_enabled", type=inkex.Boolean, default=False)
        pars.add_argument("--freemode_cmd3", default="")
        pars.add_argument("--freemode_cmd3_enabled", type=inkex.Boolean, default=False)
        pars.add_argument("--freemode_cmd4", default="")
        pars.add_argument("--freemode_cmd4_enabled", type=inkex.Boolean, default=False)
        pars.add_argument("--freemode_cmd5", default="")
        pars.add_argument("--freemode_cmd5_enabled", type=inkex.Boolean, default=False)
        pars.add_argument("--freemode_show_cmd", type=inkex.Boolean, default=False)

        # General Settings
        pars.add_argument("--input_handling", default="paths", help="Input handling")
        pars.add_argument("--flattenbezier", type=inkex.Boolean, default=False, help="Flatten bezier curves to polylines")
        pars.add_argument("--flatness", type=float, default=0.1, help="Minimum flatness = 0.1. The smaller the value the more fine segments you will get (quantization).")
        pars.add_argument("--decimals", type=int, default=3, help="Accuracy for imported lines' coordinates into vpype. Does not work for 'Multilayer/document'")
        pars.add_argument("--simplify", type=inkex.Boolean, default=False, help="Reduces significantly the number of segments used to approximate the curve while still guaranteeing an accurate conversion, but may increase the execution time. Does not work for 'Singlelayer/paths'")
        pars.add_argument("--parallel", type=inkex.Boolean, default=False, help="Enables multiprocessing for the SVG conversion. This is recommended ONLY when using 'Simplify geometry' on large SVG files with many curved elements. Does not work for 'Singlelayer/paths'")
        pars.add_argument("--output_show", type=inkex.Boolean, default=False, help="This will open a separate window showing the finished SVG data. If enabled, output is not applied to InkScape canvas (only for preview)!")
        pars.add_argument("--output_show_points", type=inkex.Boolean, default=False, help="Enable point display in viewer")
        pars.add_argument("--output_stats", type=inkex.Boolean, default=False, help="Show output statistics before/after conversion")
        pars.add_argument("--output_trajectories", type=inkex.Boolean, default=False, help="Add paths for the travel trajectories")
        pars.add_argument("--keep_objects", type=inkex.Boolean, default=False, help="If false, selected paths will be removed")
        pars.add_argument("--strokes_to_paths", type=inkex.Boolean, default=True, help="Recommended option. Performs 'Path' > 'Stroke to Path' (CTRL + ALT + C) to convert vpype converted lines back to regular path objects")
        pars.add_argument("--use_style_of_first_element", type=inkex.Boolean, default=True, help="If enabled the first element in selection is scanned and we apply it's style to all imported vpype lines (but not for trajectories). Does not work for 'Multilayer/document'")
        pars.add_argument("--lines_stroke_width", type=float, default=1.0, help="Stroke width of tooling lines (px). Gets overwritten if 'Use style of first selected element' is enabled")
        pars.add_argument("--trajectories_stroke_width", type=float, default=1.0, help="Stroke width of trajectory lines (px). Gets overwritten if 'Use style of first selected element' is enabled")

    def effect(self):
        lc = vpype.LineCollection() # create a new array of LineStrings consisting of Points. We convert selected paths to polylines and grab their points
        elementsToWork = [] # we make an array of all collected nodes to get the boundingbox of that array. We need it to place the vpype converted stuff to the correct XY coordinates

        def flatten(node):
            #path = node.path.to_superpath()
            path = node.path.transform(node.composed_transform()).to_superpath()
            bezier.cspsubdiv(path, self.options.flatness)
            newpath = []
            for subpath in path:
                first = True
                for csp in subpath:
                    cmd = 'L'
                    if first:
                        cmd = 'M'
                    first = False
                    newpath.append([cmd, [csp[1][0], csp[1][1]]])
            node.path = newpath

        # flatten the node's path to linearize, split up the path to it's subpaths (break apart) and add all points to the vpype lines collection
        def convertPath(node, nodes = None):
            if nodes is None:
                nodes = []
            if node.tag == inkex.addNS('path','svg'):
                nodes.append(node)
                if self.options.flattenbezier is True:
                    flatten(node)

                raw = node.path.to_arrays()
                subPaths, prev = [], 0
                for i in range(len(raw)): # Breaks compound paths into simple paths
                    if raw[i][0] == 'M' and i != 0:
                        subPaths.append(raw[prev:i])
                        prev = i
                subPaths.append(raw[prev:])
                for subPath in subPaths:
                    points = []
                    for csp in subPath:
                        if len(csp[1]) > 0: #we need exactly two points per straight line segment
                            points.append(Point(round(csp[1][0], self.options.decimals), round(csp[1][1], self.options.decimals)))
                    if  len(subPath) > 2 and (subPath[-1][0] == 'Z' or subPath[0][1] == subPath[-1][1]):  #check if path has more than 2 points and is closed by Z or first pont == last point
                        points.append(Point(round(subPath[0][1][0], self.options.decimals), round(subPath[0][1][1], self.options.decimals))) #if closed, we add the first point again
                    lc.append(LineString(points))

            children = node.getchildren()
            if children is not None:
                for child in children:
                    convertPath(child, nodes)
            return nodes

        doc = None #create a vpype document

        '''
        if 'paths' we process paths only. Objects like rectangles or strokes like polygon have to be converted before accessing them
        if 'layers' we can process all layers in the complete document
        '''
        if self.options.input_handling == "paths":
            # getting the bounding box of the current selection. We use to calculate the offset XY from top-left corner of the canvas. This helps us placing back the elements
            input_bbox = None
            if len(self.svg.selected) == 0:
                elementsToWork = convertPath(self.document.getroot())
                for element in elementsToWork:
                    input_bbox += element.bounding_box()
            else:
                elementsToWork = None
                for element in self.svg.selected.values():
                    elementsToWork = convertPath(element, elementsToWork)
                #input_bbox = inkex.elements._selected.ElementList.bounding_box(self.svg.selected) # get BoundingBox for selection
                input_bbox = self.svg.selection.bounding_box() # get BoundingBox for selection
            if len(lc) == 0:
                self.msg('Selection appears to be empty or does not contain any valid svg:path nodes. Try to cast your objects to paths using CTRL + SHIFT + C or strokes to paths using CTRL + ALT+ C')
                return
            # find the first object in selection which has a style attribute (skips groups and other things which have no style)
            firstElementStyle = None
            for element in elementsToWork:
                if element.attrib.has_key('style'):
                    firstElementStyle = element.get('style')
            doc = vpype.Document(page_size=(input_bbox.width + input_bbox.left, input_bbox.height + input_bbox.top)) #create new vpype document
            doc.add(lc, layer_id=None) # we add the lineCollection (converted selection) to the vpype document

        elif self.options.input_handling == "layers":
            doc = vpype.read_multilayer_svg(self.options.input_file, quantization = self.options.flatness, crop = False, simplify = self.options.simplify, parallel = self.options.parallel, default_width = self.document.getroot().get('width'), default_height = self.document.getroot().get('height'))

            for element in self.document.getroot().xpath("//svg:g", namespaces=inkex.NSS): #all groups/layers
                elementsToWork.append(element)

        tooling_length_before = doc.length()
        traveling_length_before = doc.pen_up_length()

        # build and execute the conversion command
        # the following code block is not intended to sum up the commands to build a series (pipe) of commands!
        ##########################################

        # Line Sorting
        if self.options.linesort is True:
            command = "linesort "
            if self.options.linesort_no_flip is True:
                command += " --no-flip"

        # Line Merging
        if self.options.linemerge is True:
            command = "linemerge --tolerance " + str(self.options.linemerge_tolerance)
            if self.options.linemerge_no_flip is True:
                command += " --no-flip"

        # Trimming
        if self.options.trim is True:
            command = "trim " + str(self.options.trim_x_margin) + " " + str(self.options.trim_y_margin)

        # Relooping
        if self.options.reloop is True:
            command = "reloop --tolerance " + str(self.options.reloop_tolerance)

        # Multipass
        if self.options.multipass is True:
            command = "multipass --count " + str(self.options.multipass_count)

        # Filter
        if self.options.filter is True:
            command = "filter --tolerance " + str(self.options.filter_tolerance)
            if self.options.filter_min_length_enabled is True:
                command += " --min-length " + str(self.options.filter_min_length)
            if self.options.filter_max_length_enabled is True:
                command += " --max-length " + str(self.options.filter_max_length)
            if self.options.filter_closed is True and self.options.filter_not_closed is False:
                command += " --closed"
            if self.options.filter_not_closed is True and self.options.filter_closed is False:
                command += " --not-closed"
            if self.options.filter_closed is False and \
                self.options.filter_not_closed is False and \
                self.options.filter_min_length_enabled is False and \
                self.options.filter_max_length_enabled is False:
                self.msg('No filters to apply. Please select at least one filter.')
                return

        # Plugin Occult
        if self.options.plugin_occult is True:
            command = "occult --tolerance " + str(self.options.plugin_occult_tolerance)
            if self.options.plugin_occult_keepseparatelayer is True:
                command += " --keep-occulted"

        # Plugin Deduplicate
        if self.options.plugin_deduplicate is True:
            command = "deduplicate --tolerance " + str(self.options.plugin_deduplicate_tolerance)
            if self.options.plugin_deduplicate_keepseparatelayer is True:
                command += " --keep-duplicates"

        # Split All
        if self.options.splitall is True:
            command = " splitall"

        # Free Mode
        if self.options.freemode is True:
            command = ""
            if self.options.freemode_cmd1_enabled is True:
                command += " " + self.options.freemode_cmd1.strip()
            if self.options.freemode_cmd2_enabled is True:
                command += " " + self.options.freemode_cmd2.strip()
            if self.options.freemode_cmd3_enabled is True:
                command += " " + self.options.freemode_cmd3.strip()
            if self.options.freemode_cmd4_enabled is True:
                command += " " + self.options.freemode_cmd4.strip()
            if self.options.freemode_cmd5_enabled is True:
                command += " " + self.options.freemode_cmd5.strip()
            if self.options.freemode_cmd1_enabled is False and \
                self.options.freemode_cmd2_enabled is False and \
                self.options.freemode_cmd3_enabled is False and \
                self.options.freemode_cmd4_enabled is False and \
                self.options.freemode_cmd5_enabled is False:
                self.msg("Warning: empty vpype pipeline. With this you are just getting read-write layerset/lineset.")
            else:
                if self.options.freemode_show_cmd is True:
                    self.msg("Your command pipe will be the following:")
                    self.msg(command)

        # self.msg(command)
        try:
            doc = execute(command, doc)
        except Exception as e:
            self.msg("Error in vpype:" + str(e))
            return

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

        tooling_length_after = doc.length()
        traveling_length_after = doc.pen_up_length()
        if tooling_length_before > 0:
            tooling_length_saving = (1.0 - tooling_length_after / tooling_length_before) * 100.0
        else:
            tooling_length_saving = 0.0
        if traveling_length_before > 0:
            traveling_length_saving = (1.0 - traveling_length_after / traveling_length_before) * 100.0
        else:
            traveling_length_saving = 0.0
        if self.options.output_stats is True:
            self.msg('Total tooling length before vpype conversion: '   + str('{:0.2f}'.format(tooling_length_before))   + ' mm')
            self.msg('Total traveling length before vpype conversion: ' + str('{:0.2f}'.format(traveling_length_before)) + ' mm')
            self.msg('Total tooling length after vpype conversion: '    + str('{:0.2f}'.format(tooling_length_after))    + ' mm')
            self.msg('Total traveling length after vpype conversion: '  + str('{:0.2f}'.format(traveling_length_after))  + ' mm')
            self.msg('Total tooling length optimized: '   + str('{:0.2f}'.format(tooling_length_saving))   + ' %')
            self.msg('Total traveling length optimized: ' + str('{:0.2f}'.format(traveling_length_saving)) + ' %')

        if tooling_length_after == 0:
            self.msg('No lines left after vpype conversion. Conversion result is empty. Cannot continue. Check your document about containing any svg:path elements. You will need to convert objects and strokes to paths first! Vpype command chain was:')
            self.msg(command)
            return

        # show the vpype document visually
        if self.options.output_show:
            warnings.filterwarnings("ignore") # workaround to suppress annoying DeprecationWarning
            # vpype_viewer.show(doc, view_mode=ViewMode.PREVIEW, show_pen_up=self.options.output_trajectories, show_points=self.options.output_show_points, pen_width=0.1, pen_opacity=1.0, argv=None)
            vpype_viewer.show(doc, view_mode=ViewMode.PREVIEW, show_pen_up=self.options.output_trajectories, show_points=self.options.output_show_points, argv=None) # https://vpype.readthedocs.io/en/stable/api/vpype_viewer.ViewMode.html
            warnings.filterwarnings("default") # reset warning filter
            exit(0) #we leave the code loop because we only want to preview. We don't want to import the geometry

        # save the vpype document to new svg file and close it afterwards
        output_file = self.options.input_file + ".vpype.svg"
        output_fileIO = open(output_file, "w", encoding="utf-8")
        #vpype.write_svg(output_fileIO, doc, page_size=None, center=False, source_string='', layer_label_format='%d', show_pen_up=self.options.output_trajectories, color_mode='layer', single_path = True)
        vpype.write_svg(output_fileIO, doc, page_size=None, center=False, source_string='', layer_label_format='%d', show_pen_up=self.options.output_trajectories, color_mode='layer')
        #vpype.write_svg(output_fileIO, doc, page_size=(self.svg.unittouu(self.document.getroot().get('width')), self.svg.unittouu(self.document.getroot().get('height'))), center=False, source_string='', layer_label_format='%d', show_pen_up=self.options.output_trajectories, color_mode='layer')
        output_fileIO.close()

        # parse the SVG file
        try:
            stream = open(output_file, 'r')
        except FileNotFoundError as e:
            self.msg("There was no SVG output generated by vpype. Cannot continue")
            exit(1)
        p = etree.XMLParser(huge_tree=True)
        import_doc = etree.parse(stream, parser=etree.XMLParser(huge_tree=True))
        stream.close()

        # handle pen_up trajectories (travel lines)
        trajectoriesLayer = import_doc.getroot().xpath("//svg:g[@id='pen_up_trajectories']", namespaces=inkex.NSS)
        if self.options.output_trajectories is True:
            if len(trajectoriesLayer) > 0:
                trajectoriesLayer[0].set('style', 'stroke:#0000ff;stroke-width:{:0.2f}px;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1;fill:none'.format(self.options.trajectories_stroke_width))
                trajectoriesLayer[0].attrib.pop('stroke') # remove unneccesary stroke attribute
                trajectoriesLayer[0].attrib.pop('fill') # remove unneccesary fill attribute
        else:
            if len(trajectoriesLayer) > 0:
                trajectoriesLayer[0].delete()

        lineLayers = import_doc.getroot().xpath("//svg:g[not(@id='pen_up_trajectories')]", namespaces=inkex.NSS) #all layer except the pen_up trajectories layer
        if self.options.use_style_of_first_element is True and self.options.input_handling == "paths" and firstElementStyle is not None:

            # if we remove the fill property and use "Use style of first element in layer" the conversion will just crash with an unknown reason
            #declarations = firstElementStyle.split(';')
            #for i, decl in enumerate(declarations):
            #    parts = decl.split(':', 2)
            #    if len(parts) == 2:
            #        (prop, val) = parts
            #        prop = prop.strip().lower()
            #        #if prop == 'fill':
            #        #   declarations[i] = prop + ':none'
            for lineLayer in lineLayers:
                #lineLayer.set('style', ';'.join(declarations))
                lineLayer.set('style', firstElementStyle)
                lineLayer.attrib.pop('stroke') # remove unneccesary stroke attribute
                lineLayer.attrib.pop('fill') # remove unneccesary fill attribute

        else:
            for lineLayer in lineLayers:
                if lineLayer.attrib.has_key('stroke'):
                    color = lineLayer.get('stroke')
                    lineLayer.set('style', 'stroke:' + color + ';stroke-width:{:0.2f}px;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1;fill:none'.format(self.options.lines_stroke_width))
                    lineLayer.attrib.pop('stroke') # remove unneccesary stroke attribute
                    lineLayer.attrib.pop('fill') # remove unneccesary fill attribute

        import_viewBox = import_doc.getroot().get('viewBox').split(" ")
        self_viewBox = self.document.getroot().get('viewBox')

        if self_viewBox is not None: #some SVG files do not have this attribute
            self_viewBoxValues = self_viewBox.split(" ")
            scaleX = self.svg.unittouu(self_viewBoxValues[2]) / self.svg.unittouu(import_viewBox[2])
            scaleY = self.svg.unittouu(self_viewBoxValues[3]) / self.svg.unittouu(import_viewBox[3])

        for element in import_doc.getroot().iter("{http://www.w3.org/2000/svg}g"):
            e = self.document.getroot().append(element)
            if self.options.input_handling == "layers":
                if self_viewBox is not None:
                    element.set('transform', 'scale(' + str(scaleX) + ',' + str(scaleY) + ')') #imported groups need to be transformed. Or they have wrong size. Reason: different viewBox sizes/units in namedview definitions

            # convert vpype polylines/lines/polygons to regular paths again (objects to paths)
            if self.options.strokes_to_paths is True:
                for line in element.iter("{http://www.w3.org/2000/svg}line"):
                    newLine = PathElement()
                    newLine.path = Path("M {},{} L {},{}".format(line.attrib['x1'], line.attrib['y1'], line.attrib['x2'], line.attrib['y2']))
                    element.append(newLine)
                    line.delete()

                for polyline in element.iter("{http://www.w3.org/2000/svg}polyline"):
                    newPolyLine = PathElement()
                    newPolyLine.path = Path('M' + polyline.attrib['points'])
                    element.append(newPolyLine)
                    polyline.delete()

                for polygon in element.iter("{http://www.w3.org/2000/svg}polygon"):
                    newPolygon = PathElement()
                    #newPolygon.path = Path('M' + " ".join(polygon.attrib['points'].split(' ')[:-1]) + ' Z') #remove the last point of the points string by splitting at whitespace, converting to array and removing the last item. then converting back to string
                    newPolygon.path = Path('M' + " ".join(polygon.attrib['points'].split(' ')) + ' Z')
                    element.append(newPolygon)
                    polygon.delete()

        # Delete the temporary file again because we do not need it anymore
        if os.path.exists(output_file):
            os.remove(output_file)

        # Remove selection objects to do a real replace with new objects from vpype document
        if self.options.keep_objects is False:
            for element in elementsToWork:
                element.delete()

if __name__ == '__main__':
    vpypetools().run()


from pymongo import MongoClient
from tkinter import *

root = Tk()
root.geometry("300x300")
client = MongoClient("localhost", 27017)

# insert
def HandleInsert():
    # db
    database = client["student"]
    # collection
    col = database["student_info"]
    name = e1.get()
    col.insert_one({'name': name})
    e1.delete(0, END)
    print("Date successfully inserted...")

l = Label(root, text="Enter Name")
l.grid(row=0, column=1)
e1 = Entry(root)
e1.grid(row=0, column=2)
b1 = Button(root, text="Submit", command=HandleInsert)
b1.grid(row=1, column=2)

root.mainloop()


import tensorflow as tf
import keras
import numpy as np
import matplotlib.pyplot as plt
from tqdm import tqdm

model = keras.models.load_model('model.h5')

(x_train, y_train), (x_test, y_test) = tf.keras.datasets.mnist.load_data(path="mnist.npz")

xs_test = []
ys_test = []
for image in tqdm(x_test):
    x = []
    y = []
    for line in image:
        if len(x) < 14:
            x.append(line)
        else:
            y.append(line)
    xs_test.append(x)
    ys_test.append(y)
xs_test = np.array(xs_test).reshape((10000, 14, 28, 1))/255
ys_test = np.array(ys_test).reshape((10000, 14, 28, 1))/255

for i in range(30):
    top = xs_test[i].reshape(14, 28)
    bottom_true = ys_test[i].reshape(14, 28)

    x = np.array([top.reshape(14, 28, 1)])
    y = model.predict(x)[0].reshape(14, 28)

    img1 = np.array([*top, *y])
    img2 = np.array([*top, *bottom_true])

    plt.imshow(img1)
    plt.show()
    plt.imshow(img2)
    plt.show()


#!/usr/bin/env python2

import numpy as np
import scipy.misc, sys

def munge(im):
    sz  = min(im.shape[:2])                             # Get shorter extent.
    ofs = [(int(x) - sz) // 2 for x in im.shape[:2]]    # Calculate crop corner pixel.
    im  = im[ofs[0]:ofs[0]+sz, ofs[1]:ofs[1]+sz, :]     # Crop to a square.
    im  = scipy.misc.imresize(im, (224, 224))           # Resize to 224 x 224.
    im  = im.astype(np.float32)                         # Convert to FP32.
    im -= [[[103.939, 116.779, 123.68]]]                # Subtract VGG training set mean from every pixel.
    im  = im.transpose((2,0,1))                         # Transpose to channel-major order.
    return im

if __name__ == "__main__":
    if len(sys.argv) != 3:
        print("Usage: python imgconv.py <input file> <output file>")
        exit(1)

    im = scipy.misc.imread(sys.argv[1], mode='RGB')
    im = munge(im)
    im.tofile(sys.argv[2])


import pandas as pd
import numpy as np
from sklearn import preprocessing
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.neural_network import MLPClassifier
from sklearn.metrics import classification_report, confusion_matrix
from sklearn.model_selection import GridSearchCV

dataUrl = "./resources/setInicial.csv"

columnsName = ['ciudad_destino_anterior_compra', 'frecuencia_viajes_x_anio', 'ingresos_a_la_pagina_x_anio',
               'dias_anticipacion_anterior_compra', 'cantidad_pasajeros_anterior_compra',
               'duracion_viaje_anterior_compra', 'ciudad_destino']

data = pd.read_csv(dataUrl, names=columnsName, sep='|')

x = data.iloc[:, 0:5]

y = data.iloc[:, 6:7]

le = preprocessing.LabelEncoder()

y = le.fit_transform(y.values.ravel())

x.ciudad_destino_anterior_compra = le.fit_transform(np.ravel(x.ciudad_destino_anterior_compra))
x.frecuencia_viajes_x_anio = le.fit_transform(np.ravel(x.frecuencia_viajes_x_anio))
x.ingresos_a_la_pagina_x_anio = le.fit_transform(np.ravel(x.ingresos_a_la_pagina_x_anio))

X_train, X_test, y_train, y_test = train_test_split(x, y, test_size=0.02)

scaler = StandardScaler()
scaler.fit(X_train)

X_train = scaler.transform(X_train)
X_test = scaler.transform(X_test)

mlp = MLPClassifier(max_iter=100000)

# mlp.fit(X_test, y_test)

# predictions = mlp.predict(X_test)

parameter_space = {
    'hidden_layer_sizes': [(50,50,50), (50,100,50), (100,),(10,10,10)],
    'activation': ['tanh', 'relu'],
    'solver': ['sgd', 'adam'],
    'alpha': [0.0001, 0.05],
    'learning_rate': ['constant','adaptive'],
}

clf = GridSearchCV(mlp, parameter_space, n_jobs=-1, cv=3)
clf.fit(X_test, y_test)

# All results
means = clf.cv_results_['mean_test_score']
stds = clf.cv_results_['std_test_score']
for mean, std, params in zip(means, stds, clf.cv_results_['params']):
    print("%0.3f (+/-%0.03f) for %r" % (mean, std * 2, params))

# print(confusion_matrix(y_test, predictions))
# print(classification_report(y_test, predictions))


# coding=utf-8
from datetime import datetime
from model import db

class User(db.Model):
    """

    """
    __tablename__ = 'users_'
    cid = db.Column(db.Integer, primary_key=True)
    place = db.Column(db.String, nullable=False)
    status = db.Column(db.String(255), nullable=False)
    created_at = db.Column(db.DateTime, nullable=False)

    @staticmethod
    def set_config(cid, place, status):
        """Guarda un valor

        Args:
            :param cid: Id. del chat
            :param place: lugar del usuario
            :param status: Valor del estado del usuario

        Returns:
            :return: Instancia del usuario con los el valor almacenado
        """
        record = db.session.query(User).filter_by(cid=cid).first()

        if record is None:
            record = User(cid=cid, place=place, status=status, created_at=datetime.now())
            db.session.add(record)
        else:
            record.status = status
            record.place = place
            record.created_at = datetime.now()

        db.session.commit()
        db.session.close()

        return record

    @staticmethod
    def get_config(cid):
        """ Recupera un valor

        Args:
            :param cid: Id. del chat

        Returns:
            :return: Instancia de Chat que coincide con la clave o None si no existe
        """

        record = db.session.query(User).filter_by(cid=cid).first()

        if record is None:
            record = User(cid=cid, place="none", status="none", created_at=datetime.now())
            db.session.add(record)

        db.session.close()

        return record

    @staticmethod
    def get_checked_in_at(place):
        """ Recupera un valor

        Args:
            :param place: lugar del usuario

        Returns:
            :return: array de usuarios
        """
        record = db.session.query(User).filter_by(place=place,status="checked in").all()
        db.session.close()

        return record

    @staticmethod
    def get_all():
        """
        Returns:
            :return: array de usuarios
        """
        record = db.session.query(User).all()
        db.session.close()

        return record


# 导入需要的库
import imutils
import cv2

def preprocess(image, width, height):
    # 获得图像尺寸，然后初始化填充值
    (h, w) = image.shape[:2]

    # 如果宽度大于高度，那么按照宽度调整大小
    if w > h:
        image = imutils.resize(image, width=height)

    # 否则，如果高度大于宽度那么按照高度进行大小调整
    else:
        image = imutils.resize(image, height=height)

    # 确定高度和宽度需要的填充
    padW = int((width - image.shape[1]) / 2.0)
    padH = int((height - image.shape[0]) / 2.0)

    # 填充图像，然后应用更多的resize来解决四舍五入问题
    image = cv2.copyMakeBorder(image, padH, padH, padW, padW, cv2.BORDER_REPLICATE)
    image = cv2.resize(image, (width, height))

    # 返回一个预处理图片
    return image


__author__ = 'Guilhem Faure'
__description__ = '''SRA.py download SRR files from GSM or SRX list  '''

import os
import configparser
import subprocess
import optparse
import copy
import sys

def download_sra(command):
    '''
    Download SRR file in workdir
    :param command: command to download all SRR from GSM number
    :return: stdout of download_sra
    '''

    print ('Downloading', sample_gsm)
    log_download_sra = subprocess.getoutput(command)

    return log_download_sra

def gsm2srr(command):
    '''

    :param command_gsm2srr: command to grab the SRR from the GSM
    :return: srr_id
    '''

    print ('Getting the SRR identification')
    srr_id = subprocess.getoutput(command)

    return srr_id

def clip_adapter(command):
    '''
    Clip adapter
    :param command: command to clip adapter from gzip fastq
    :return: log clip adapter
    '''

    print ('Clipping adapter', sample_gsm)

    log_clip_adapter = subprocess.getoutput(command)

    return log_clip_adapter

if __name__ == '__main__':

    usage = "usage: %prog [-h -w <workdir>] -g <GSEsample>"
    parser = optparse.OptionParser(usage = usage)
    parser.add_option('-g', '--gse', dest='gse', help='GSE sample generate by GSE.py')
    parser.add_option('-w', '--workdir', dest='workdir', help='Workdir output', default = None)
    parser.add_option('-a', '--adapter', dest='adapter', help='adapter', default = 'CTGTAGGCACCATCAAT')
    parser.add_option('-c', '--clip', dest='clip', action='store_true',
                      help='clip adapter; use -a to specify adapter (default CTGTAGGCACCATCAAT)')
                                                                             #CTGTAGGCACCATCAAT
    (options, args) = parser.parse_args()

    if options.gse == None:
        parser.error("You should provide a GSEsample file with -g or --gse, -h for help")

    ## Loading dependency and command lines
    p_d_script = os.path.dirname(os.path.realpath(__file__))
    Config_dependancy = configparser.ConfigParser()
    Config_dependancy.read(os.path.join( p_d_script, "dependency.config"))

    Config_command = configparser.ConfigParser()
    Config_command.read(os.path.join( p_d_script, "command.config"))

    d_options = copy.copy(Config_dependancy['EUTILS'])
    d_options.update(Config_dependancy['SRATOOLKIT'])

    ## Output path
    p_out = os.path.basename(options.gse).split('.')[0]+'.log'

    p_root = os.getcwd()

    # Read GSE.sample and download selected samples
    with open(options.gse) as f:

        # if workdir is enter by the user
        if options.workdir and not os.path.exists(options.workdir):
            os.mkdir(options.workdir)
            os.chdir(options.workdir)

        # Read sample to download
        for line in f:
            if line.startswith('#'):
                continue
            sp = line.strip().split('\t')
            sample_gsm = sp[0]
            d_options['gsm'] = sample_gsm

            # Create GSM directory
            if not os.path.exists(sample_gsm):
                os.mkdir(sample_gsm)
            os.chdir(sample_gsm)

            # Log file in each GSM directory
            with open(sample_gsm, 'w') as fout:
                fout.write('#Log '+ ' '.join(sys.argv)+'\n')

                # Get SRR number
                command_gsm2srr = Config_command['SRA']['gsm2srr'].format(**d_options)
                fout.write(command_gsm2srr+'\n')
                srr_id = gsm2srr(command_gsm2srr)
                d_options['srr'] = srr_id
                fout.write('GSM2SRR {0} {1}\n'.format(sample_gsm, srr_id))

                # Download fastq.gz
                command_download_sra = Config_command['SRA']['download_sra'].format(**d_options)
                fout.write(command_download_sra+'\n')
                log_download_sra = download_sra(command_download_sra)
                fout.write(log_download_sra+'\n')

                if options.clip is True:
                    d_options.update(Config_dependancy['FASTX'])
                    d_options['min_read_size'] = '25'
                    d_options['adapter'] = options.adapter
                    d_options['input_fastqgz'] = srr_id+'.fastq.gz'
                    d_options['output_clip'] = srr_id+'_clip.fastq.gz'
                    command_clip_adapter = Config_command['FASTX']['clip_adapter'].format(**d_options)
                    fout.write(command_clip_adapter+'\n')
                    log_clip_adapter = clip_adapter(command_clip_adapter)
                    fout.write(log_clip_adapter+'\n')

            # Coming back to root directory
            os.chdir(p_root)

    print ( 'Log file', p_root )


#在一个长度为n的数组里的所有数字都在0到n-1的范围内。
# 数组中某些数字是重复的，但不知道有几个数字是重复的。
# 也不知道每个数字重复几次。请找出数组中任意一个重复的数字。
#  例如，如果输入长度为7的数组{2,3,1,0,2,5,3}，那么对应的输出是第一个重复的数字2。

#用字典就解决了。
def duplicate( numbers, duplication):
    dic = {}
    flag = 0
    for num in numbers:

        if num not in dic:
            dic[num] = 1
        else:
            duplicate[0] = num
            flag = 1
            break
    if flag == 0 :
        return False

    else:
        return True

剑指Offer/数组中重复的数字.py


import cv2
import matplotlib
import numpy as np
from matplotlib import pyplot as plt

def display(dataset, result, iteration, number_generation, new_best_individual, number_step_to_actualize_view):
    if new_best_individual:
        display_on_screen(number_generation, dataset, result)
    elif round(iteration % number_step_to_actualize_view) == 0:
        display_on_screen(number_generation, dataset, result)

def display_on_screen(number_generation, dataset, result):
    matplotlib.use('Agg')

    new_width = 500
    font = cv2.FONT_HERSHEY_SIMPLEX
    bottom_left_corner_of_text = (100, 40)
    font_scale = 1
    font_color = (0, 0, 0)
    thickness = 1
    line_type = 0

    text_to_display = "Score: " + str(round(result[-1][1], 2))
    best_individual = result[-1][2]
    x_values = []
    y_values = []

    figure1 = plt.figure(figsize=(6, 4))
    for gene in best_individual:
        x_values.append(dataset[gene][0])
        y_values.append(dataset[gene][1])
    plt.scatter(x_values, y_values, color='blue', zorder=2)

    x_values.append(dataset[best_individual[0]][0])
    y_values.append(dataset[best_individual[0]][1])
    plt.plot(x_values, y_values, color='deepskyblue', zorder=1)

    for i, label in enumerate(dataset):
        plt.text(label[0], label[1], i)

    plt.xlim([0, round(max(x_values) * 1.1)])
    plt.ylim([0, round(max(y_values) * 1.1)])

    plt.gca().set_aspect('equal', adjustable='box')

    image1 = generate_image(figure1, new_width)

    figure2 = plt.figure(figsize=(6, 2.5))
    if len(result) > 1:
        x = [i[0] for i in result]
        y = [i[1] for i in result]
        plt.plot(x, y)

    plt.xscale('log')
    plt.xlim([1, number_generation])
    plt.ylim([0, result[0][1]])

    image2 = generate_image(figure2, new_width)

    image_to_display = np.concatenate((image1, image2), axis=0)
    cv2.putText(image_to_display, text_to_display, bottom_left_corner_of_text, font, font_scale, font_color, thickness,
                line_type)

    image_to_display = cv2.cvtColor(image_to_display, cv2.COLOR_RGB2BGR)
    plt.close('all')
    cv2.startWindowThread()
    cv2.imshow("Genetic algorithm", image_to_display)
    cv2.waitKey(1)

def generate_image(fig, new_width):
    fig.canvas.draw()
    img = np.fromstring(fig.canvas.tostring_rgb(), dtype=np.uint8, sep='')
    img = img.reshape(fig.canvas.get_width_height()[::-1] + (3,))
    percent_size = (new_width / img.shape[1])
    hsize = int(img.shape[0] * percent_size)
    img = cv2.resize(img, (new_width, hsize))
    return img


from __future__ import annotations

import torch
import torch.nn as nn

from compressai.entropy_models import EntropyBottleneck
from compressai.latent_codecs import EntropyBottleneckLatentCodec
from compressai.models import CompressionModel
from compressai.registry import register_model
from src.layers.layers import Gain, Interleave, Reshape, Transpose
from src.layers.pc_pointnet2 import PointNetSetAbstraction
from src.layers.pcc import GAIN
from src.layers.pcc_reconstruction.pointnet2 import UpsampleBlock

@register_model("um-pcc-rec-pointnet2-ssg")
@register_model("um-pcc-rec-pointnet2")
class PointNet2SsgReconstructionPccModel(CompressionModel):
    def __init__(
        self,
        num_points=1024,
        num_classes=40,
        D=(0, 128, 256, 512),
        P=(1024, 256, 64, 1),
        S=(None, 4, 4, 64),
        R=(None, 0.2, 0.4, None),
        E=(3, 32, 32, 32, 0),
        M=(64, 64, 64, 64),
        normal_channel=False,
    ):
        """
        Args:
            num_points: Number of input points. [unused]
            num_classes: Number of object classes. [unused]
            D: Number of input feature channels.
            P: Number of output points.
            S: Number of samples per centroid.
            R: Radius of the ball to query points within.
            E: Number of output feature channels after each upsample.
            M: Number of latent channels for the bottleneck.
            normal_channel: Whether the input includes normals.
        """
        super().__init__()

        self.num_points = num_points
        self.num_classes = num_classes
        self.D = D
        self.P = P
        self.S = S
        self.R = R
        self.E = E
        self.M = M
        self.normal_channel = bool(normal_channel)

        # Original PointNet++ architecture:
        # D = [3 * self.normal_channel, 128, 256, 1024]
        # P = [None, 512, 128, 1]
        # S = [None, 32, 64, 128]
        # R = [None, 0.2, 0.4, None]

        # NOTE: P[0] is only used to determine the number of output points.
        # assert P[0] == num_points

        assert P[0] == P[1] * S[1]
        assert P[1] == P[2] * S[2]
        assert P[2] == P[3] * S[3]

        self.levels = 4

        self.down = nn.ModuleDict(
            {
                "_1": PointNetSetAbstraction(
                    npoint=P[1],
                    radius=R[1],
                    nsample=S[1],
                    in_channel=D[0] + 3,
                    mlp=[D[1] // 2, D[1] // 2, D[1]],
                    group_all=False,
                ),
                "_2": PointNetSetAbstraction(
                    npoint=P[2],
                    radius=R[2],
                    nsample=S[2],
                    in_channel=D[1] + 3,
                    mlp=[D[1], D[1], D[2]],
                    group_all=False,
                ),
                "_3": PointNetSetAbstraction(
                    npoint=None,
                    radius=None,
                    nsample=None,
                    in_channel=D[2] + 3,
                    mlp=[D[2], D[3], D[3]],
                    group_all=True,
                ),
            }
        )

        i_final = self.levels - 1
        groups_h_final = 1 if D[i_final] * M[i_final] <= 2**16 else 4

        self.h_a = nn.ModuleDict(
            {
                **{
                    f"_{i}": nn.Sequential(
                        Reshape((D[i] + 3, P[i + 1] * S[i + 1])),
                        nn.Conv1d(D[i] + 3, M[i], 1),
                        Gain((M[i], 1), factor=GAIN),
                    )
                    for i in range(self.levels - 1)
                    if M[i] > 0
                },
                f"_{i_final}": nn.Sequential(
                    Reshape((D[i_final], 1)),
                    nn.Conv1d(D[i_final], M[i_final], 1, groups=groups_h_final),
                    Interleave(groups=groups_h_final),
                    Gain((M[i_final], 1), factor=GAIN),
                ),
            }
        )

        self.h_s = nn.ModuleDict(
            {
                **{
                    f"_{i}": nn.Sequential(
                        Gain((M[i], 1), factor=1 / GAIN),
                        nn.Conv1d(M[i], D[i] + 3, 1),
                    )
                    for i in range(self.levels - 1)
                    if M[i] > 0
                },
                f"_{i_final}": nn.Sequential(
                    Gain((M[i_final], 1), factor=1 / GAIN),
                    nn.Conv1d(M[i_final], D[i_final], 1, groups=groups_h_final),
                    Interleave(groups=groups_h_final),
                ),
            }
        )

        self.up = nn.ModuleDict(
            {
                "_0": nn.Sequential(
                    nn.Conv1d(E[1] + D[0] + 3 * bool(M[0]), E[1], 1),
                    # nn.BatchNorm1d(E[1]),
                    nn.ReLU(inplace=True),
                    nn.Conv1d(E[1], E[0], 1),
                    Reshape((E[0], P[0])),
                    Transpose(-2, -1),
                ),
                "_1": UpsampleBlock(D, E, M, P, S, i=1, extra_in_ch=3, groups=(1, 4)),
                "_2": UpsampleBlock(D, E, M, P, S, i=2, extra_in_ch=3, groups=(1, 4)),
                "_3": UpsampleBlock(D, E, M, P, S, i=3, extra_in_ch=0, groups=(1, 4)),
            }
        )

        self.latent_codec = nn.ModuleDict(
            {
                f"_{i}": EntropyBottleneckLatentCodec(
                    entropy_bottleneck=EntropyBottleneck(M[i], tail_mass=1e-4),
                )
                for i in range(self.levels)
                if M[i] > 0
            }
        )

    def forward(self, input):
        xyz, norm = self._get_inputs(input)
        y_out_, u_, uu_ = self._compress(xyz, norm, mode="forward")
        x_hat, y_hat_, v_ = self._decompress(y_out_, mode="forward")

        return {
            "x_hat": x_hat,
            "likelihoods": {
                f"y_{i}": y_out_[i]["likelihoods"]["y"]
                for i in range(self.levels)
                if "likelihoods" in y_out_[i]
            },
            # Additional outputs:
            "debug_outputs": {
                **{f"u_{i}": v for i, v in u_.items() if v is not None},
                **{f"uu_{i}": v for i, v in uu_.items()},
                **{f"y_hat_{i}": v for i, v in y_hat_.items()},
                **{f"v_{i}": v for i, v in v_.items() if v.numel() > 0},
            },
        }

    def compress(self, input):
        xyz, norm = self._get_inputs(input)
        y_out_, _, _ = self._compress(xyz, norm, mode="compress")

        return {
            # "strings": {f"y_{i}": y_out_[i]["strings"] for i in range(self.levels)},
            # Flatten nested structure into list[list[str]]:
            "strings": [
                ss for level in range(self.levels) for ss in y_out_[level]["strings"]
            ],
            "shape": {f"y_{i}": y_out_[i]["shape"] for i in range(self.levels)},
        }

    def decompress(self, strings, shape):
        y_inputs_ = {
            i: {
                "strings": [strings[i]],
                "shape": shape[f"y_{i}"],
            }
            for i in range(self.levels)
        }

        x_hat, _, _ = self._decompress(y_inputs_, mode="decompress")

        return {
            "x_hat": x_hat,
        }

    def _get_inputs(self, input):
        points = input["points"].transpose(-2, -1)
        if self.normal_channel:
            xyz = points[:, :3, :]
            norm = points[:, 3:, :]
        else:
            xyz = points
            norm = None
        return xyz, norm

    def _compress(self, xyz, norm, *, mode):
        lc_func = {"forward": lambda lc: lc, "compress": lambda lc: lc.compress}[mode]

        B, _, _ = xyz.shape

        xyz_ = {0: xyz}
        u_ = {0: norm}
        uu_ = {}
        y_ = {}
        y_out_ = {}

        for i in range(1, self.levels):
            down_out_i = self.down[f"_{i}"](xyz_[i - 1], u_[i - 1])
            xyz_[i] = down_out_i["new_xyz"]
            u_[i] = down_out_i["new_features"]
            uu_[i - 1] = down_out_i["grouped_features"]

        uu_[self.levels - 1] = u_[self.levels - 1][:, :, None, :]

        for i in reversed(range(0, self.levels)):
            if self.M[i] == 0:
                y_out_[i] = {"strings": [[b""] * B], "shape": ()}
                continue

            y_[i] = self.h_a[f"_{i}"](uu_[i])
            # NOTE: Reshape 1D -> 2D since latent codecs expect 2D inputs.
            y_out_[i] = lc_func(self.latent_codec[f"_{i}"])(y_[i][..., None])

        return y_out_, u_, uu_

    def _decompress(self, y_inputs_, *, mode):
        y_hat_ = {}
        y_out_ = {}
        uu_hat_ = {}
        v_ = {}

        for i in reversed(range(0, self.levels)):
            if self.M[i] == 0:
                continue
            if mode == "forward":
                y_out_[i] = y_inputs_[i]
            elif mode == "decompress":
                y_out_[i] = self.latent_codec[f"_{i}"].decompress(
                    y_inputs_[i]["strings"], shape=y_inputs_[i]["shape"]
                )
            # NOTE: Reshape 2D -> 1D since latent codecs return 2D outputs.
            y_hat_[i] = y_out_[i]["y_hat"].squeeze(-1)
            uu_hat_[i] = self.h_s[f"_{i}"](y_hat_[i])

        B, _, *tail = uu_hat_[self.levels - 1].shape
        v_[self.levels] = uu_hat_[self.levels - 1].new_zeros((B, 0, *tail))

        for i in reversed(range(0, self.levels)):
            v_[i] = self.up[f"_{i}"](
                v_[i + 1]
                if self.M[i] == 0
                else torch.cat([v_[i + 1], uu_hat_[i]], dim=1)
            )

        x_hat = v_[0]

        return x_hat, y_hat_, v_


# lista completa de nombres
nombres = ["Harry Houdini", "Newton", "David Blaine", "Hawking", "Messi", "Teller", "Einstein", "Pele", "Juanes"]

# separar los nombres en categorías
magos = ["Harry Houdini", "David Blaine", "Teller"]
cientificos = ["Newton", "Hawking", "Einstein"]
otros=[]

for item in nombres:
    if item not in magos and item not in cientificos:
        otros.append(item)

# función para hacer grandiosos a los magos
def hacer_grandioso(lista_magos):
    for i in range(len(lista_magos)):
        lista_magos[i] = "El gran " + lista_magos[i]

# función para imprimir nombres
def imprimir_nombres(lista_nombres):
    for nombre in lista_nombres:
        print(nombre)

# imprimir la lista completa de nombres
print("Lista completa de nombres:")
imprimir_nombres(nombres)
print("")

# hacer grandiosos a los magos
hacer_grandioso(magos)

# imprimir los nombres de los magos grandiosos
print("Nombres de los magos grandiosos:")
imprimir_nombres(magos)
print("")

# imprimir los nombres de los científicos
print("Nombres de los científicos:")
imprimir_nombres(cientificos)
print("")

# imprimir los nombres de los restantes
print("Nombres de los restantes:")
imprimir_nombres(otros)


# -*- coding: utf-8 -*-
"""
Created on Fri Jun 01 23:14:27 2018

@author: lwang
"""

class MedianFinder(object):

    def __init__(self):
        """
        initialize your data structure here.
        """
        self.array = []

    def addNum(self, num):
        """
        :type num: int
        :rtype: void
        """
        if not self.array:
            self.array.append(num)
        else:
            lo, hi = 0, len(self.array)-1
            while lo<=hi:
                mid = (lo+hi)/2
                if self.array[mid]<num:
                    lo = mid+1
                elif self.array[mid]>num:
                    hi = mid-1
                else:
                    self.array.insert(mid, num)
                    return
            self.array.insert(min(lo, hi)+1,num)

    def findMedian(self):
        """
        :rtype: float
        """
        if not self.array:
            return None
        else:
            l = len(self.array)
            return self.array[l/2] if l%2==1 else sum(self.array[l/2-1:l/2+1])/2.

# Your MedianFinder object will be instantiated and called as such:
# obj = MedianFinder()
# obj.addNum(num)
# param_2 = obj.findMedian()


import json

from kafka import KafkaProducer

from config import REDPANDA_BROKERS, SYMBOLS
from data.providers import ALPACA, DATA_PROVIDER_KEY
from utils import alpaca_utils

# configs
REDPANDA_TOPIC = "market-news"

# Pull live data from Alpaca
print(f"Pulling live news data for symbols: {SYMBOLS}")

# Get a Redpanda client instance
redpanda_client = KafkaProducer(
    bootstrap_servers=REDPANDA_BROKERS,
    key_serializer=str.encode,
    value_serializer=lambda m: json.dumps(m).encode("utf-8"),
)

# An async handler that gets called whenever new data is seen
async def news_callback(row):
    # The SDK returns a NewsV2 object. row._raw allows us to access the record
    # in dictionary form
    article = row._raw

    # Covert the timestamp to milliseconds
    timestamp_ms = int(row.created_at.timestamp() * 1000)
    article["timestamp_ms"] = timestamp_ms

    # Add an identifier for the data provider
    article[DATA_PROVIDER_KEY] = ALPACA

    # The article may relate to multiple symbols. Produce a separate record
    # each matched search symbol.
    article_symbols = article.pop("symbols")

    for search_symbol in SYMBOLS:
        if not search_symbol in article_symbols:
            continue

        article["symbol"] = search_symbol

        # Produce the news article to Redpanda
        try:
            future = redpanda_client.send(
                REDPANDA_TOPIC,
                key=search_symbol,
                value=article,
                timestamp_ms=timestamp_ms,
            )

            # Block until the message is sent (or timeout).
            _ = future.get(timeout=10)

        except Exception as e:
            print(e)

stream = alpaca_utils.get_legacy_stream_client()
stream.subscribe_news(news_callback, *SYMBOLS)

stream.run()


import discord
from discord.ext import commands
import os
import json
import random
import asyncio
import requests
import datetime
import time

client = commands.Bot(command_prefix = ".", intents = discord.Intents.all(), activity = discord.Activity(type=discord.ActivityType.listening, name= " .help"))

@client.command()
async def joke(ctx):
  response = requests.get("https://icanhazdadjoke.com", headers = {"Accept": "text/plain"})
  embed = discord.Embed(title = "Joke", description = response.text, color = discord.Colour.blue())
  await ctx.send(embed = embed)

@client.command()
async def invite(ctx):
  embed = discord.Embed(title = "Thanks for considering!", description = f"[Click Here](link) to invite me to your server! \n Currently, I'm in {len(client.guilds)} servers!", color = discord.Color.green())
  embed.set_footer(text = "Invite me", icon_url = client.user.avatar_url)
  await ctx.send(embed = embed)

@client.command()
@commands.is_owner()
async def load(ctx, extension):
  client.load_extension(f"cogs.{extension}")
  await ctx.send(f"Loaded ``{extension}`` successfully")

@client.command()
async def server(ctx):
  all_roles = ""
  for role in ctx.guild.roles:
    all_roles += f"{role.mention} "

  guild = ctx.guild
  embed = discord.Embed(title = ctx.guild.name, decription = f"Information on {ctx.guild}", color = client.ecolor)
  embed.add_field(name = "Members", value = guild.member_count, inline = False)
  embed.add_field(name = "Channels", value = f"All channels: {len(guild.channels)} \n Text Chanels: {len(guild.text_channels)} \n Voice Channels: {len(guild.voice_channels)}", inline = False)
  embed.add_field(name = "ID", value = guild.id, inline = False)
  embed.add_field(name = "Owner", value = guild.owner, inline = False)
  embed.add_field(name = f"Roles ({len(guild.roles)})", value = all_roles, inline = False)
  embed.set_thumbnail(url = ctx.guild.icon_url)
  await ctx.send(embed = embed)

@client.command()
@commands.is_owner()
async def unload(ctx, extension):
  client.unload_extension(f"cogs.{extension}")
  await ctx.send(f"Unloaded ``{extension}`` successfully")

@client.command()
@commands.is_owner()
async def loadall(ctx):
  for filename in os.listdir("./cogs"):
    if filename.endswith('.py'):
      client.load_extension(f"cogs.{filename[:-3]}")
      await ctx.send(f"Loaded ``{filename[:-3]}`` successfully")

@client.event
async def on_ready():
  for filename in os.listdir("./cogs"):
    if filename.endswith('.py') and not filename.startswith('Automod'):
      client.load_extension(f"cogs.{filename[:-3]}")
      print(f"Loaded ``{filename[:-3]}`` successfully")
  client.load_extension('jishaku')
  print("Ready")

@client.command()
async def ping(ctx):
  start = time.perf_counter()
  a = await ctx.send("Pinging...")
  end = time.perf_counter()
  dur = (end-start) * 1000
  embed = discord.Embed(title = "Pong!", description = f"**Response Time** \n ```{dur:.2f}ms``` \n **Websocket Latency** \n ```{round(((client.latency) * 1000), 2)}ms```", color = 0xb3b3ff)
  await a.edit(embed = embed)

@client.command(aliases = ["bug"])
async def suggest(ctx, *, suggestion):
  you = client.get_user(your_id)

  await you.send(f"New Suggestion by {ctx.author} \n {suggestion}")
  await ctx.send("Thank you for your suggestion / bug report! It helps a lot!")

snipe_message_content = None
snipe_message_author = None
snipe_message_id = None
snipe_message = None

@client.event
async def on_message_delete(message):
  global snipe_message_content
  global snipe_message_author
  global snipe_message_id
  global snipe_message

  snipe_message_content = message.content
  snipe_message_author = message.author
  snipe_message_id = message.id
  snipe_message = message
  await asyncio.sleep(60)

  if message.id == snipe_message_id:
      snipe_message_author = None
      snipe_message_content = None
      snipe_message_id = None
      snipe_message = None

@client.command()
async def snipe(ctx):
    if snipe_message_content==None:
        await ctx.send("There's nothing to snipe!")
    else:
        embed = discord.Embed(title = f"{snipe_message_author.name}#{snipe_message_author.discriminator}", description = f"{snipe_message_content}", color = 0xb3b3ff)
        created_time = snipe_message.created_at
        str_time = created_time.strftime('%m/%d/%Y, %H:%M:%S')
        embed.set_footer(text =f"Deleted at {str_time}")
        await ctx.send(embed=embed)
        return

client.run(os.getenv("TOKEN"))


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

@author: Simone Boglio
"""

import os
import numpy as np
import scipy.sparse as sps
import Data_manager.Utility as ut

from Data_manager.load_and_save_data import save_data_dict_zip, load_data_dict_zip
from Data_manager.IncrementalSparseMatrix import IncrementalSparseMatrix
from Data_manager.split_functions.split_train_validation import split_train_validation_leave_one_out_user_wise

class TafengReader:

    URM_DICT = {}
    ICM_DICT = {}

    CONFERENCE = "IJCAI"
    MODEL = "CoupledCF"
    DATASET_NAME = "Tafeng"

    def __init__(self, pre_splitted_path, type='original'):
        assert type in ["original", "ours"]

        pre_splitted_path += "data_split/"
        pre_splitted_filename = "splitted_data_"

        # If directory does not exist, create
        if not os.path.exists(pre_splitted_path):
            os.makedirs(pre_splitted_path)

        try:
            print("Dataset_{}: Attempting to load pre-splitted data".format(self.DATASET_NAME))

            for attrib_name, attrib_object in load_data_dict_zip(pre_splitted_path, pre_splitted_filename).items():
                self.__setattr__(attrib_name, attrib_object)

        except FileNotFoundError:

            print("Dataset_{}: Pre-splitted data not found, building new one".format(self.DATASET_NAME))

            from Conferences.IJCAI.CoupledCF_original import LoadTafengDataCnn as DatareaderOriginal
            path = "Conferences/IJCAI/CoupledCF_original/tafeng/"

            n_users, user_attributes_mat = DatareaderOriginal.load_user_attributes(path=path)
            n_items, items_genres_mat = DatareaderOriginal.load_itemGenres_as_matrix(path=path)
            ratings = DatareaderOriginal.load_rating_train_as_matrix(path=path)
            testRatings = DatareaderOriginal.load_rating_file_as_list(path=path)
            testNegatives = DatareaderOriginal.load_negative_file(path=path)

            URM_all = ratings.tocsr()

            UCM_all = sps.csc_matrix(user_attributes_mat)
            UCM_age = UCM_all[:, 0:11].tocsr()
            UCM_region = UCM_all[:, 11:19].tocsr()
            UCM_all = UCM_all.tocsr()

            # col: 0->category, 2->asset(0-1), 1->price(0-1)
            ICM_original = sps.csc_matrix(items_genres_mat)

            # category could be used as matrix, not single row
            ICM_sub_class = ICM_original[:, 0:1].tocsr()
            max = ICM_sub_class.shape[0]
            rows, cols, data = [], [], []
            for idx in range(max):
                # we have only index 0 as col
                data_vect = ICM_sub_class.data[ICM_sub_class.indptr[idx]:ICM_sub_class.indptr[idx+1]]
                if len(data_vect) == 0:
                    # handle category value 0 that in a csr matrix is not present
                    cols.append(int(0))
                else:
                    cols.append(int(data_vect[0]))
                rows.append(idx)
                data.append(1.0)

            ICM_sub_class = sps.csr_matrix((data, (rows, cols)))
            ICM_asset = ICM_original[:, 1:2].tocsr()
            ICM_price = ICM_original[:, 2:3].tocsr()

            ICM_original = ICM_original.tocsc()
            ICM_all = sps.hstack((ICM_sub_class, ICM_asset,ICM_price))

            testRatings = np.array(testRatings).T
            URM_test_builder = IncrementalSparseMatrix(n_rows=n_users+1, n_cols=n_items+1)
            URM_test_builder.add_data_lists(testRatings[0], testRatings[1], np.ones(len(testRatings[0])))
            URM_test = URM_test_builder.get_SparseMatrix()

            URM_test_negatives_builder = IncrementalSparseMatrix(n_rows=n_users+1, n_cols=n_items+1)

            # care here, the test negative start from index 0 but it refer to user index 1 (user index start from 1)
            n_negative_samples = 99
            for index in range(len(testNegatives)):
                user_test_items = testNegatives[index]
                if len(user_test_items) != n_negative_samples:
                    print("user id: {} has {} negative items instead {}".format(index+1, len(user_test_items), n_negative_samples))
                URM_test_negatives_builder.add_single_row(index + 1, user_test_items, data=1.0)

            URM_test_negative = URM_test_negatives_builder.get_SparseMatrix()
            URM_test_negative.data = np.ones_like(URM_test_negative.data)

            if type == 'original':
                URM_test = URM_test
                URM_train, URM_validation = split_train_validation_leave_one_out_user_wise(URM_all.copy(), verbose=False)
            else:  # redo the split
                URM_full = URM_all + URM_test
                URM_temp, URM_test = split_train_validation_leave_one_out_user_wise(URM_full.copy(), verbose=False)
                URM_train, URM_validation = split_train_validation_leave_one_out_user_wise(URM_temp.copy(), verbose=False)

            self.ICM_DICT = {
                "UCM_age": UCM_age,
                "UCM_region": UCM_region,
                "UCM_all": UCM_all,
                "ICM_all": ICM_all,
                "ICM_original": ICM_original,
                "ICM_sub_class": ICM_sub_class,
                "ICM_asset": ICM_asset,
                "ICM_price": ICM_price,
            }

            self.URM_DICT = {
                "URM_train": URM_train,
                "URM_test": URM_test,
                "URM_validation": URM_validation,
                "URM_test_negative": URM_test_negative,
            }

            save_data_dict_zip(self.URM_DICT, self.ICM_DICT, pre_splitted_path, pre_splitted_filename)

        print("{}: Dataset loaded".format(self.DATASET_NAME))

        ut.print_stat_datareader(self)


class Solution(object):
    def findWords(self, board, words):
        """
        :type board: List[List[str]]
        :type words: List[str]
        :rtype: List[str]
        """
        self.ROWS = len(board)
        self.COLS = len(board[0])
        self.board = board

        words_found = []
        for word in words:
            for row in range(self.ROWS):
                for col in range(self.COLS):
                    if self.backtrack(row, col, word):
                        words_found.append(word)
        return words_found

    def backtrack(self, row, col, suffix):
        if len(suffix) == 0:
            return True

        if row < 0 or row == self.ROWS or col < 0 or col == self.COLS or \
                self.board[row][col] != suffix[0]:
            return False

        ret = False
        self.board[row][col] = '#'

        for row_offset, col_offset in [(0, 1), (1, 0), (0, -1), (-1, 0)]:
            ret = self.backtrack(row+row_offset, col + col_offset, suffix[1:])
            if ret:
                return True

        self.board[row][col] = suffix[0]

        return ret

board = [	["o", "a", "a", "n"],
					["e", "t", "a", "e"],
					["i", "h", "k", "r"],
					["i", "n", "i", "a"]
				]
words = ["oath", "pea", "eat", "rain"]

solution = Solution()

print("is there a solution for", board, "with word ",
      words, " result:", solution.findWords(board, words))


from . import tictactoe_client, tictactoe_server

def run(client_or_serv="input"):
    if client_or_serv == "input":
        user_input = input("Are you connecting to or hosting a tictactoe match? (C/H) ")

        if user_input.lower() == "c":
            tictactoe_client.run()
        elif user_input.lower() == "h":
            tictactoe_server.run()
        else:
            print("No option selected, aborting...")
    elif client_or_serv.lower() == "client":
        tictactoe_client.run()
    elif client_or_serv.lower() == "server":
        tictactoe_server.run()


import time
import os
import argparse
import sys
from tensorflow.keras.models import Model as ModelTF2
from tensorflow.keras.layers import Input as InputTF2
from tensorflow.keras.layers import Dropout as DropoutTF2
from tensorflow.keras.layers import Conv2D as Conv2DTF2
from tensorflow.keras.layers import MaxPooling2D as MaxPooling2DTF2
from tensorflow.keras.layers import BatchNormalization as BatchNormalizationTF2
from tensorflow.keras.layers import Flatten as FlattenTF2
from tensorflow.keras.layers import Dense as DenseTF2
from tensorflow.keras.regularizers import l2
import cv2
import numpy as np
from fer import FaceEmotionRecognition
import matplotlib.pyplot as plt

def main(args):
    print("Predicting emotions...")
    start = time.time()

    cropped_faces_folder = args.cropped_faces_folder
    output_folder = args.output_folder

    print("Loading FER model...")
    model = create_model()
    print("Model loaded! Loading weights...")
    model.load_weights('models/model1-fer-weights.h5')
    print("Model weights loaded!")

    FER = FaceEmotionRecognition()
    n_actors = len(os.listdir(cropped_faces_folder))

    print("Starting classification...")
    if not os.path.exists(output_folder):
        os.makedirs(output_folder)
    with open(output_folder + '/results.csv', mode='a+') as file:

        for folder in sorted(os.listdir(cropped_faces_folder)):
            done = 0

            n_images = len(next(os.walk(cropped_faces_folder + '/' + folder))[2])
            print(n_images)

            for path in sorted(os.listdir(cropped_faces_folder + '/' + folder)):
                image_path = cropped_faces_folder + '/' + folder + '/' + path
                image_name = os.path.splitext(path)[0]

                image = cv2.imread(image_path)
                correct_class = image_name[6:8]

                predictions = []
                angry, disgust, fear, happy, sad, surprise, neutral = [], [], [], [], [], [], []

                for face in FER.extract_features_from_face(FER.face_detector(image)):

                    to_predict = np.reshape(face.flatten(), (1, 48, 48, 1))
                    predicted_emotions = model.predict(to_predict)

                    angry.append(float('{:f}'.format(predicted_emotions[0][0])))
                    disgust.append(float('{:f}'.format(predicted_emotions[0][1])))
                    fear.append(float('{:f}'.format(predicted_emotions[0][2])))
                    happy.append(float('{:f}'.format(predicted_emotions[0][3])))
                    sad.append(float('{:f}'.format(predicted_emotions[0][4])))
                    surprise.append(float('{:f}'.format(predicted_emotions[0][5])))
                    neutral.append(float('{:f}'.format(predicted_emotions[0][6])))

                done += 1
                print("Processing {} of {}. Done {} of {} frames - {}%".format(folder, n_actors, done, n_images, round((done / n_images) * 100, 2)))

                if len(angry) and len(disgust) and len(fear) and len(happy) and len(sad) and len(surprise) and len(neutral):
                    # angry,disgust,fear,happy,sad,surprise,neutra,image_name,correct_class
                    results = "{},{},{},{},{},{},{},{},{}"\
                        .format(
                            (sum(angry) / len(angry)),
                            (sum(disgust) / len(disgust)),
                            (sum(fear) / len(fear)),
                            (sum(happy) / len(happy)),
                            (sum(sad) / len(sad)),
                            (sum(surprise) / len(surprise)),
                            (sum(neutral) / len(neutral)),
                            image_name,
                            correct_class
                        )

                    file.write(results)
                    file.write("\n")

    end = time.time()
    print("Classification finished. Total time: {}".format(end - start))

def create_model():
    num_features = 64
    width = height = 48
    input_shape = InputTF2(shape=(48, 48, 1))

    x = Conv2DTF2(num_features, kernel_size=(3, 3), activation='relu', input_shape=(width, height, 1), data_format='channels_last', kernel_regularizer=l2(0.01))(input_shape)
    x = Conv2DTF2(num_features, kernel_size=(3, 3), activation='relu', padding='same')(x)
    x = BatchNormalizationTF2()(x)
    x = MaxPooling2DTF2(pool_size=(2, 2), strides=(2, 2))(x)
    x = DropoutTF2(0.5)(x)

    x = Conv2DTF2(2*num_features, kernel_size=(3, 3), activation='relu', padding='same')(x)
    x = BatchNormalizationTF2()(x)
    x = Conv2DTF2(2*num_features, kernel_size=(3, 3), activation='relu', padding='same')(x)
    x = BatchNormalizationTF2()(x)
    x = MaxPooling2DTF2(pool_size=(2, 2), strides=(2, 2))(x)
    x = DropoutTF2(0.5)(x)

    x = Conv2DTF2(2*2*num_features, kernel_size=(3, 3), activation='relu', padding='same')(x)
    x = BatchNormalizationTF2()(x)
    x = Conv2DTF2(2*2*num_features, kernel_size=(3, 3), activation='relu', padding='same')(x)
    x = BatchNormalizationTF2()(x)
    x = MaxPooling2DTF2(pool_size=(2, 2), strides=(2, 2))(x)
    x = DropoutTF2(0.5)(x)

    x = Conv2DTF2(2*2*2*num_features, kernel_size=(3, 3), activation='relu', padding='same')(x)
    x = BatchNormalizationTF2()(x)
    x = Conv2DTF2(2*2*2*num_features, kernel_size=(3, 3), activation='relu', padding='same')(x)
    x = BatchNormalizationTF2()(x)
    x = MaxPooling2DTF2(pool_size=(2, 2), strides=(2, 2))(x)
    x = DropoutTF2(0.5)(x)

    x = FlattenTF2()(x)

    x = DenseTF2(2*2*2*num_features, activation='relu')(x)
    x = DropoutTF2(0.4)(x)
    x = DenseTF2(2*2*num_features, activation='relu')(x)
    x = DropoutTF2(0.4)(x)
    x = DenseTF2(2*num_features, activation='relu')(x)
    x = DropoutTF2(0.5)(x)

    x = DenseTF2(7, activation='softmax')(x)

    return ModelTF2(inputs=input_shape, outputs=x)

def parse_arguments(argv):
    parser = argparse.ArgumentParser()
    parser.add_argument('cropped_faces_folder', type=str, help='The path to the cropped faces folder')
    parser.add_argument('output_folder', type=str, help='The path where the .csv file will be saved')
    return parser.parse_args(argv)

if __name__ == "__main__":
    main(parse_arguments(sys.argv[1:]))


import matplotlib.pyplot as plt
from matplotlib.ticker import FormatStrFormatter
import pickle
import numpy as np
import json
import pandas as pd
from tabulate import tabulate

import sys, os
sys.path.append('../')

from function_p import get_cms_relative
import utils
from utils import ApplyConfiguration

if __name__ == "__main__":
    with open(os.path.join(utils.ROOT_DIR, 'functions_output/configs.json'), 'r') as fp:
        configs = json.load(fp)

    nth = configs['nth']

    th2s = configs['th2s']
    configs = configs['configs']

    configs = [
        ApplyConfiguration.from_dict(configs[hash])
        for hash in configs
    ]

    configs_done = []
    for i, config in enumerate(configs):
        _path_csv = os.path.join(utils.ROOT_DIR, f'./functions_output/f/{config.__hash__()}.csv')
        _path_pkl = os.path.join(utils.ROOT_DIR, f'./functions_output/f/{config.__hash__()}.pickle')

        df_f = pd.read_csv(_path_csv)

        with open(_path_pkl, 'rb') as f:
            cms_ths = pickle.load(f)

        ths = np.linspace(df_f.wvalue.min(), df_f.wvalue.max(), num=nth, dtype=np.float32)

        ths = pd.Index(ths, name='th')

        with open('/tmp/csvtmp.csv', 'w') as fp:
            str_ = pd.DataFrame(cms_ths, index=ths, columns=[ 'TN', 'FP', 'FN', 'TP']).reset_index().to_string()
            fp.write(str_)

        cms_ths_rel = get_cms_relative(cms_ths)

        with open('/tmp/csvtmp_relative.csv', 'w') as fp:
            df = pd.DataFrame()
            df['ths'] = ths
            df['tn'] = cms_ths_rel[0]
            df['tp'] = [ [ '%0.2f' % round(cms_ths_rel[3][d][i], 2) for d in utils.DGAS0 ] for i in range(cms_ths_rel[3][0].shape[0])  ]
            for d in utils.DGAS0:
                df['tp_%d' % d] = cms_ths_rel[3][d]

            fp.write(df.round(2).to_latex(index=False))

        plt.rcParams.update({
            "text.usetex": True,
            "font.family": "sans-serif",
            "font.sans-serif": ["Helvetica"]
        })

        fig, axs = plt.subplots(len(utils.DGAS0), sharex=True)

        tabs = [ [0] for _ in utils.DGAS0 ]

        for dga in utils.DGAS0:

            tp = 'tp$^{\\omega_{%d}}$' % dga

            df = df.rename(columns={ 'ths': 'th', f'tp_{dga}': tp})

            df['tn + tp'] = df.tn + df[tp]

            ax = df.plot(x='th', y=['tn', tp], figsize=(10,6), color=[ 'CornflowerBlue', 'OrangeRed', 'green'], ax=axs[dga])
            plt.tight_layout()

            max_ms_idx = df['tn + tp'].idxmax()

            max_ms_th = df.th[max_ms_idx]

            max_ms_tn = df.tn[max_ms_idx]
            max_ms_tp = df[tp][max_ms_idx]

            tabs[dga][0] = f'{max_ms_tn:0.2f}, {max_ms_tp:0.2f}'

            axs[dga].set_ylabel(f'$\\omega^{dga}$', color='g')

            # axs[dga].text(max_ms_th, 1, 'max(tn + tp$^{\\omega_0}$)', ha='center')

            axs[dga].scatter([max_ms_th, max_ms_th], [max_ms_tn, max_ms_tp], c=['CornflowerBlue', 'OrangeRed'])

            gap_tp = 0.04 if abs(max_ms_tn - max_ms_tp) < 0.05 else 0
            sign_tp = 1 if max_ms_tn > max_ms_tp else -1

            axs[dga].annotate(f'{max_ms_tn:.2f}', xy=(df.th.min(), max_ms_tn + sign_tp*gap_tp), color='CornflowerBlue', fontsize=10, horizontalalignment='right', verticalalignment='center')
            axs[dga].annotate(f'{max_ms_tp:.2f}', xy=(df.th.min(), max_ms_tp - sign_tp*gap_tp), color='OrangeRed', fontsize=10, horizontalalignment='right', verticalalignment='center')

            axs[dga].plot([max_ms_th, max_ms_th], [-0.05, max_ms_tp if max_ms_tp > max_ms_tn else max_ms_tn], color='grey', linestyle='--', linewidth=0.5)
            axs[dga].plot([df.th.iloc[0], max_ms_th], [max_ms_tn, max_ms_tn], color='CornflowerBlue', linestyle='--', linewidth=0.5)
            axs[dga].plot([df.th.iloc[0], max_ms_th], [max_ms_tp, max_ms_tp], color='OrangeRed', linestyle='--', linewidth=0.5)

            th2s = [ 0.25, 0.5, 0.75 ]
            for j, th2 in enumerate(th2s):

                s = (df['tn'] > th2) & (df[tp] > th2)
                sa = s.cumsum()-s.cumsum().where(~s).ffill().fillna(0).astype(int)

                c = 0
                start = 0
                maxg = 0
                group = []
                for i, a in s.iteritems():
                    if c == 0 and a != 0:
                        start = i
                    if c != 0 and a == 0:
                        if maxg < c:
                            group = [ start, i ]
                        c = 0
                    c = a + c

                if group != []:
                    th_start = df.th[group[0] - 1 if group[0] > 0 else group[0]]
                    th_end = df.th[group[1] + 1 if group[1] > nth else group[1]]

                    tabs[dga].append((th_end-th_start) / (df.th.max() - df.th.min()))

                    axs[dga].annotate('', xy=(th_start, th2), xytext=(th_end, th2), arrowprops=dict(arrowstyle='<->', linewidth=0.5))
                    axs[dga].annotate(f'$th_2={th2:.2f}$', xy=(-36000, th2), horizontalalignment='center', verticalalignment='top')
                    axs[dga].annotate(f'$m_s({th2})$', xy=(th_start + (th_end - th_start) / 2, th2 - 0.07), fontsize=8, horizontalalignment='center', verticalalignment='center')

                pass

            # plt.legend(bbox_to_anchor=(0.95, 0.95), prop={'size': 12})

            yticks = [ 0 ] + th2s
            axs[dga].yaxis.set_major_formatter(FormatStrFormatter('%.2f'))
            axs[dga].set_yticks(yticks)

            plt.setp(axs[dga].get_yticklabels(), rotation=30, horizontalalignment='right')

            axs[dga].set_xlim(df.th.iloc[0], df.th.iloc[-1])
            axs[dga].set_ylim(-0.005, 1.2)

            pass

        print(tabulate(tabs))

        print(tabulate(tabs, tablefmt='latex'))

        plt.xticks([ x for x in list(plt.xticks()[0]) if x >= df.th.iloc[0] and x != -20000 ] + [ max_ms_th ])

        plt.subplots_adjust(left=0.1,
                        bottom=0.1,
                        right=0.9,
                        top=0.9,
                        wspace=0.05,
                        hspace=0.05)

        plt.savefig(f'/tmp/plot_metric_dga_{dga}.png')

        plt.show()

        pass

    pass