#!/usr/bin/env python
# -*- coding: utf-8 -*-
'''
 @Time    : 29/5/2017 11:18 AM
 @Author  : Qiang,Q
 @File    : output.py
 @Software: PyCharm Community Edition
'''
import json
import csv
class Outputer(object):
    def __init__(self):
        self.datas=[]
    def collect_data(self,data):
        if data is None:
            return
        self.datas.append(data)

    #json输出.
    def json_output(self,filename):
        #'a' 以追加模式打开
        fout=open('/Users/fiona/Desktop/lianjiadata/%s.json' % filename,'a',encoding='utf-8')
        try:
            dataLines=[json.dumps(line,ensure_ascii=False)+'\n' for line in self.datas]
            fout.writelines(dataLines)
        finally:
            fout.close()

    #csv输出.
    def csv_output(self):
        csvFile=open('/Users/fiona/Desktop/TestData/lianjiadata.csv','a',encoding='utf-8')
        try:
            writer=csv.writer(csvFile)
            dataLines=[]
            for data in self.datas:
                lineTuple=(data['address'], data['flood'], data['following'], data['content'], data['unit_price'],
                           data['total_price'])
                dataLines.append(lineTuple)

            writer.writerows([data for data in dataLines])
        finally:
            csvFile.close()
    #错误信息写入
    def error_log_output(self,spider_type='zufang',url=None):
        error_file=open('/Users/fiona/Desktop/lianjiadata/error.log','a')
        try:
            line='%s %s\n' %(spider_type,url)
            error_file.write(line)
        except:
            print('error log  write error')
        finally:
            error_file.close()


from sqlalchemy.sql import func

from url_app.utils import db
from url_app.utils.utils import ModelCreationMixin

class Analytics(db.Model, ModelCreationMixin):
    __tablename__ = 'analytics'

    id = db.Column(db.Integer, primary_key=True)
    url_code = db.Column(db.String(5), db.ForeignKey('url.url_code'), nullable=False)
    url = db.relationship('Url', backref='url')
    ip_address = db.Column(db.String(150), nullable=True)
    date_created = db.Column(db.DateTime(timezone=True), default=func.now())


# coding=utf-8
from __future__ import absolute_import
from __future__ import division

import os

import cv2
import numpy as np

from text_fcn import coco_utils
from text_fcn.dataset_reader.dataset_reader import BatchDataset

class CocoDataset(BatchDataset):

    def __init__(self,
                 coco_ids,
                 ct,
                 coco_dir,
                 batch_size,
                 image_size,
                 crop=False,
                 pre_saved=False,
                 augment_data=True):
        """
        :param coco_ids:
        :param ct: COCO_Text object instance
        :param coco_dir: directory to coco dataset
        :param batch_size:
        :param image_size: crop window size if pre_saved=False
                           image size if pre_saved=True (0 if variable)
        :param crop: whether to crop images to image_size
        :param pre_saved: whether to read images from storage or generate them on the go
        :param augment_data: wheter to rotate&shuffle_bgr images

        Here's some examples
            - pre_saved = True
                - batch_size = 1, image_size = 0, crop = False
                    Load images from storage and do not crop them.
                - batch_size = X, image_size = Y, crop = False
                    Load images from storage which are asserted to have the same size = image_size.
                - batch_size = X, image_size = Y, crop = True
                    Load images from storage and crop them to image_size.
            - pre_saved = False
                - batch_size = 1, image_size = 0, crop = False
                    Generate images and do not crop them.
                - batch_size = X, image_size = Y, crop = False
                    Generate images which are asserted to have the same size = image_size.
                - batch_size = X, image_size = Y, crop = True
                    Generate images and crop them to image_size.
        """
        # crop only when crop_size if given AND images are not loaded from disk
        crop_fun = self._crop_resize if crop else None
        BatchDataset.__init__(self,
                              coco_ids,
                              batch_size,
                              image_size,
                              image_op=crop_fun,
                              augment_data=augment_data)

        self.ct = ct
        self.coco_dir = coco_dir
        self.pre_saved = pre_saved

        if self.pre_saved:
            self._get_image = self._load_image
        else:
            self._get_image = self._gen_image

    def _gen_image(self, coco_id):
        """
        Generate images using self.ct data
        :param coco_id: image's coco id
        :return: image, its groundtruth w/o illegibles and its weights
        """
        fname = self.ct.imgs[coco_id]['file_name']
        image = cv2.imread(
            os.path.join(self.coco_dir, 'images/', fname)
        )
        annotation = np.zeros(image.shape[:-1], dtype=np.uint8)
        weight = np.ones(annotation.shape, np.float32)

        for ann in self.ct.imgToAnns[coco_id]:
            poly = np.array(self.ct.anns[ann]['polygon'], np.int32).reshape((4,2))
            if self.ct.anns[ann]['legibility'] == 'legible':
                # draw only legible bbox/polygon
                cv2.fillConvexPoly(annotation, poly, 255)
            else:
                # 0 weight if it is illegible
                cv2.fillConvexPoly(weight, poly, 0.0)

        return [image, annotation, weight]

    def _load_image(self, coco_id):
        """
        Load image already saved on the disk
        """
        fname = 'COCO_train2014_%012d.png' % coco_id
        image = cv2.imread(
            os.path.join(self.coco_dir, 'images/', fname))
        annotation = cv2.imread(
            os.path.join(self.coco_dir, 'anns/', fname))
        annotation = cv2.cvtColor(annotation, cv2.COLOR_BGR2GRAY)
        weight = cv2.imread(
            os.path.join(self.coco_dir, 'weights/', fname))
        weight = cv2.cvtColor(weight, cv2.COLOR_BGR2GRAY).astype(np.float32) / 255.

        return [image, annotation, weight]

    def _crop_resize(self, image, annotation, weight, name=None):
        # next level hacks
        assert name is not None
        valid_anns = [
            ann for ann in self.ct.imgToAnns[name]
            if self.ct.anns[ann]['legibility'] == 'legible'
        ]
        ann = np.random.choice(valid_anns)
        # extract bbox => x, y, w, h
        bbox_rect = np.int32(self.ct.anns[ann]['bbox'])
        window = coco_utils.get_window(annotation.shape[:2], bbox_rect)
        return coco_utils.crop_resize([image, annotation, weight], window, self.image_size)


from pathlib import Path
import os
from typing import Callable, Sequence
from collections import defaultdict
from itertools import cycle

from matplotlib import pyplot as plt
import numpy as np
from tqdm import tqdm
from joblib import Parallel, delayed
from functools import partial

from common.log import create_logger
from common.utils import cache, tqdm_joblib
from market.task import OnlineBayesianLinearRegression
from market.data import MarketData
from market.mechanism import OnlineMarket
from analytics.helpers import (
    save_figure,
    build_input,
    nested_defaultdict,
    bootstrap_resample,
    conditional_value_at_risk,
    MarketDesigns,
    get_pyplot_colors,
)
from market.policy import (
    NllShapleyPolicy,
    KldCfModShapleyPolicy,
    KldContributionModShapleyPolicy,
)

def build_data(
    sample_size: int,
    num_features: int,
    interpolant_function: Callable,
    additive_noise_function: Callable,
    heteroskedasticity_function: Callable,
):
    def _build_data():
        X = build_input(sample_size, num_features)
        noise = additive_noise_function(sample_size)
        y = interpolant_function(X)
        y += noise * heteroskedasticity_function(X)
        return MarketData(X[:, [0]], X[:, [1]], X[:, 2:], y)

    return _build_data

def simulate_online_market(
    market_data: MarketData,
    market_designs: dict,
    likelihood_flattening: float,
    burn_in: int,
    train_payment: float,
    test_payment: float,
):
    results = {}
    for market_design, config in market_designs.items():
        task = config["task"](**config["kwargs"])
        market = config["market"](
            market_data,
            task,
            likelihood_flattening=likelihood_flattening,
            burn_in=burn_in,
            train_payment=train_payment,
            test_payment=test_payment,
        )
        output = market.run(config["policy"])
        results[market_design] = output

    return results

def parse_results(
    results: Sequence[dict],
    num_simulations: int,
    num_forgetting_factors: int,
    num_sellers: int,
    sample_size: int,
    stages: Sequence[str],
    metrics: Sequence[str],
    market_designs: Sequence[str],
):
    empty_array = lambda: np.zeros(
        (num_forgetting_factors, num_sellers, sample_size - 1)
    )
    parsed_results = nested_defaultdict(4, empty_array)

    for i in range(num_simulations):
        for j in range(num_forgetting_factors):
            for market_design in market_designs:
                for stage in stages:
                    for metric in metrics:
                        parsed_results[i][market_design][stage][metric][
                            j
                        ] = results[j][i][market_design][stage][metric].T

    stacked_results = partial(defaultdict, partial(defaultdict, dict))()
    for market_design in market_designs:
        for stage in ("train", "test"):
            for metric in metrics:
                stacked_results[market_design][stage][metric] = np.stack(
                    [
                        result[market_design][stage][metric]
                        for result in parsed_results.values()
                    ],
                    axis=3,
                )

    return stacked_results

def plot_coefficients(coefficients: np.ndarray, savedir):
    fig, ax = plt.subplots(figsize=(4, 2.5))
    for i, agent in enumerate(range(coefficients.shape[1])):
        ax.plot(coefficients[:, agent], label=f"w{i}")
    ax.legend()
    ax.set_ylabel("Coefficient Value")
    ax.set_xlabel("Time Step")
    save_figure(fig, savedir, "coefficients")

def plot_metric_boostrap(results, savedir, idx):
    fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(5.7, 2.5), sharey=True)

    pyplot_colors = get_pyplot_colors()
    colors = cycle([pyplot_colors[1], pyplot_colors[2], pyplot_colors[0]])

    metric = "payments"
    for ax, stage in zip((ax1, ax2), ("train", "test")):
        # ax.ticklabel_format(
        #     axis="both", style="scientific", scilimits=(1, 0), useMathText=True
        # )
        for market_design, market_results in results.items():
            color = next(colors)
            metric_results = np.swapaxes(market_results[stage][metric], 0, -1)
            bootstraps = bootstrap_resample(metric_results, 500, sample_size=50)
            expected_value = bootstraps.mean(axis=0)
            expected_shortfall = conditional_value_at_risk(
                bootstraps, axis=0, alpha=0.05
            )

            for seller in (0,):

                def calculate_mean(metric):
                    return metric.mean(axis=0)[seller, :, idx]

                ev_mean = calculate_mean(expected_value)
                es_mean = calculate_mean(expected_shortfall)

                num_runs = np.arange(len(es_mean)) + 1
                ax.plot(
                    num_runs,
                    ev_mean.cumsum(),
                    color=color,
                    ls="-",
                    lw=1,
                    label=market_design.value if seller == 0 else None,
                    zorder=1,
                )
                ax.axhline(y=0, c="gray", zorder=0)
                ax.yaxis.set_tick_params(labelbottom=True)
                ax.set_ylabel("Revenue (EUR)")
                ax.set_xlabel("Time Step")

    ax1.legend(framealpha=0, ncol=1)

    save_figure(fig, savedir, "contributions_risk")

def main():
    logger = create_logger(__name__)
    logger.info("Running online market analysis")

    savedir = Path(__file__).parent / "docs/sim07-online-market"
    os.makedirs(savedir, exist_ok=True)

    config = {
        "num_simulations": 1000,
        "noise_variance": 1,
        "train_payment": 0.95,
        "test_payment": 0.95,
        "regularization": 1e-5,
        "sample_size": 1000,
        "forgetting_factors": [0.94],
        "burn_in": 10,
    }

    sample_size = config["sample_size"]

    experiments = {
        "step_change": np.concatenate(
            (
                np.tile(
                    np.array([0, -0.2, 0.1, 0.3]),
                    (int(sample_size / 2), 1),
                ),
                np.tile(
                    np.array([0, -0.2, 0.6, 0.3]),
                    (
                        sample_size - int(sample_size / 2),
                        1,
                    ),
                ),
            ),
            axis=0,
        ),
        # "smooth": np.hstack(
        #     [
        #         np.linspace(0.3, 0.3, sample_size).reshape(-1, 1),
        #         np.linspace(-0.2, -0.2, sample_size).reshape(-1, 1),
        #         np.linspace(0.6**0.5, 0, sample_size).reshape(-1, 1) ** 2,
        #         np.linspace(0, 0.7, sample_size).reshape(-1, 1),
        #     ]
        # ),
    }

    for experiment_title, coefficients in experiments.items():
        logger.info(f"Running experiment: {experiment_title}")

        experiment_location = savedir / experiment_title
        os.makedirs(experiment_location, exist_ok=True)

        plot_coefficients(coefficients, experiment_location)

        num_simulations = config["num_simulations"]
        noise_variance = config["noise_variance"]
        train_payment = config["train_payment"]
        test_payment = config["test_payment"]
        regularization = config["regularization"]
        sample_size = config["sample_size"]
        forgetting_factors = config["forgetting_factors"]
        burn_in = config["burn_in"]

        interpolant_function = lambda X: np.sum(
            X * coefficients, axis=1, keepdims=True
        )
        additive_noise_function = lambda sample_size: np.random.normal(
            0, np.sqrt(noise_variance), size=(sample_size, 1)
        )
        heteroskedasticity_function = lambda X: 1

        cache_location = experiment_location / "cache"
        os.makedirs(cache_location, exist_ok=True)

        @cache(cache_location)
        def _run_experiment():
            num_forgetting_factors = len(forgetting_factors)

            experiment_results = np.zeros(
                (num_forgetting_factors, num_simulations)
            ).astype(np.object_)

            for i, forgetting_factor in tqdm(
                enumerate(config["forgetting_factors"]),
                position=0,
                desc="Forgetting Factors",
                total=num_forgetting_factors,
            ):
                market_designs = {
                    MarketDesigns.blr_nll: {
                        "task": OnlineBayesianLinearRegression,
                        "kwargs": {
                            "noise_variance": noise_variance,
                            "forgetting": forgetting_factor,
                            "regularization": regularization,
                        },
                        "market": OnlineMarket,
                        "policy": NllShapleyPolicy,
                    },
                    MarketDesigns.blr_kld_m: {
                        "task": OnlineBayesianLinearRegression,
                        "kwargs": {
                            "noise_variance": noise_variance,
                            "forgetting": forgetting_factor,
                            "regularization": regularization,
                        },
                        "market": OnlineMarket,
                        "policy": KldContributionModShapleyPolicy,
                    },
                    MarketDesigns.blr_kld_c: {
                        "task": OnlineBayesianLinearRegression,
                        "kwargs": {
                            "noise_variance": noise_variance,
                            "forgetting": forgetting_factor,
                            "regularization": regularization,
                        },
                        "market": OnlineMarket,
                        "policy": KldCfModShapleyPolicy,
                    },
                }

                iterator = tqdm(desc="Simulations", position=1, leave=False)
                with tqdm_joblib(iterator) as _:
                    experiment_results[i] = Parallel(n_jobs=-1)(
                        delayed(simulate_online_market)(
                            market_data=build_data(
                                sample_size=int(sample_size),
                                num_features=coefficients.shape[1] - 1,
                                interpolant_function=interpolant_function,
                                additive_noise_function=additive_noise_function,
                                heteroskedasticity_function=heteroskedasticity_function,
                            )(),
                            market_designs=market_designs,
                            likelihood_flattening=forgetting_factor,
                            burn_in=burn_in,
                            train_payment=train_payment,
                            test_payment=test_payment,
                        )
                        for _ in range(num_simulations)
                    )
            return parse_results(
                experiment_results,
                num_simulations,
                num_forgetting_factors,
                coefficients.shape[1] - 2,
                sample_size,
                ("train", "test"),
                ("payments", "allocations", "contributions"),
                market_designs.keys(),
            )

        parsed_results = _run_experiment()
        plot_metric_boostrap(parsed_results, experiment_location, idx=0)

if __name__ == "__main__":
    np.random.seed(42)
    main()


from random import randint

print('-='*12+'-')
print('VAMOS JOGAR PAR OU ÍMPAR')
print('-='*12+'-')

total_wins = 0

while True:
    human_value = int(input('Digite um valor inteiro: '))
    side = input('Par ou Ímpar? [P/I] ')
    machine_value = randint(0, 10)
    sum_values = human_value + machine_value

    def what_side(sum_values):
        if sum_values % 2 == 0:
            return 'PAR'
        else:
            return 'IMPAR'

    def is_correct(sum_values, side):
        if (sum_values % 2 == 0 and side == 'p') or (sum_values % 2 != 0 and side == 'i'):
            return True
        else:
            return False

    print('-='*12+'-')
    print('Você jogou {} e a máquina jogou {}. Total de {} DEU {}!'.format(human_value, machine_value, sum_values, what_side(sum_values)))

    if is_correct(sum_values, side) != True:
        break
    else:
        total_wins += 1

print('-='*6+'-')
print('VOCÊ PERDEU!')
print('-='*6+'-')
print('GAME OVER!\nVocê venceu {} vezes.'.format(total_wins))


import cPickle
import os
import texttable
from experiment.Experiment import Experiment
from experiment.real.four_area.helper.MetaPathHelper import getMetaPathAdjacencyData, testAuthors, findMostSimilarNodes
from experiment.real.four_area.helper.PathSimHelper import getAbsNeighborSimScore

__author__ = 'jontedesco'

class AuthorsNeighborSimAbsPPAExperiment(Experiment):
    """
      Runs some experiments with NeighborSim on author similarity for the 'four area' dataset
    """

    def runFor(self, author, adjMatrix, extraData, citationCounts, publicationCounts):
        print("Running for %s..." % author)

        # Find the top 10 most similar nodes to some given node
        mostSimilar, similarityScores = findMostSimilarNodes(adjMatrix, author, extraData, method = getAbsNeighborSimScore)
        self.output('Most Similar to "%s":' % author)
        mostSimilarTable = texttable.Texttable()
        rows = [['Author', 'Score', 'Citations', 'Publications']]
        rows += [[name, score, citationCounts[name], publicationCounts[name]] for name, score in mostSimilar]
        mostSimilarTable.add_rows(rows)
        self.output(mostSimilarTable.draw())

        # Output all similarity scores
        outputPath = os.path.join('../../results', 'authors', 'intermediate', '%s-neighborsim-absppa' % author.replace(' ', ''))
        cPickle.dump(similarityScores, open(outputPath, 'wb'))

def run(citationCounts, publicationCounts):
    experiment = AuthorsNeighborSimAbsPPAExperiment(
        None,
        'Most Similar PPA NeighborSim (Absolute) Authors',
        outputFilePath = os.path.join('../../results','authors','absppaNeighborSim')
    )

    # Compute once, since these never change
    graph, nodeIndex = cPickle.load(open(os.path.join('../../data', 'graphWithCitations')))
    ppaAdjMatrix, extraData = getMetaPathAdjacencyData(graph, nodeIndex, ['term', 'paper', 'paper', 'author'])
    extraData['fromNodes'] = extraData['toNodes']
    extraData['fromNodesIndex'] = extraData['toNodesIndex']

    for testAuthor in testAuthors:
        experiment.runFor(testAuthor, ppaAdjMatrix, extraData, citationCounts, publicationCounts)


"""
Testing developed functionality.

@Developer: Stan Ermokhin
@Version: 0.0.1
"""

import json
from typing import Any

import database_control
import processing

class TestClass:
    with open("cases_for_tests.json") as json_file_io:
        CASES: list[dict] = json.load(json_file_io)

    DATABASE_APP: database_control.DatabaseControl = database_control.DatabaseControl()
    PROCESSING_APP: processing.MainApp = processing.MainApp()

    def generic_test(self, number: int) -> None:
        _input: str = self.CASES[number]["input"]
        _model_dict: dict[str, Any] = self.PROCESSING_APP.convert_request_json_to_valid_dict(_input)

        values: dict[str, Any] = {"dt_from": None, "dt_upto": None, "group_type": None}
        for key in _model_dict:
            values[key] = self.PROCESSING_APP.unpack_value(_model_dict, key)

        result: str = json.dumps(self.DATABASE_APP.get_result(**values))
        output: str = json.dumps(
            self.PROCESSING_APP.convert_response_json_to_valid_dict(
                self.CASES[number]["output"]
            )
        )

        assert result == output, f"Test {number + 1} NOT passed"

    def test_sample0(self) -> None:
        self.generic_test(0)

    def test_sample1(self) -> None:
        self.generic_test(1)

    def test_sample2(self) -> None:
        self.generic_test(2)


__author__ = 'kristukat'

import numpy as np
import wx
from wx.lib import newevent
from wx.lib.floatcanvas import FloatCanvas
from functools import reduce

import logging
logger = logging.getLogger('pom')

SelectEvent, EVT_RECT_SELECT = newevent.NewEvent()
ReorderEvent, EVT_RECT_REORDER = newevent.NewEvent()

class Map(list):
    def __getitem__(self, item):
        if type(item) == tuple:
            for k in self:
                if k.gridpos == item:
                    return k
            raise IndexError(item)
        else:
            return super(Map, self).__getitem__(item)

class PlotOrderPanel(wx.Panel):
    def __init__(self, parent, **kwargs):
        wx.Panel.__init__(self, parent, **kwargs)

        self.Canvas = FloatCanvas.FloatCanvas(self, BackgroundColor="LIGHT BLUE")
        self.Canvas.InitAll()

        MainSizer = wx.BoxSizer(wx.VERTICAL)
        MainSizer.Add(self.Canvas, 4, wx.EXPAND)
        self.SetSizer(MainSizer)

        self.Bind(wx.EVT_SIZE, self.OnSize)

    def OnSize(self, evt):
        evt.Skip()
        self.Canvas.ZoomToBB()

    def Log(self, text):
        print(text)

    def OnLeftUp(self, event):
        if self.moving is not None:
            frm = self.moving
            self.moving.PutInBackground()
            self.moving.Text.PutInBackground()
            self.moving.SetFillColor('White')
            self.moving = None
            if self.target is not None:
                to = self.target
                self.target.SetFillColor('White')
                self.Canvas.Draw(Force=True)
                self.target = None
                self.reorder(frm, to)
            self.redraw_rects()

    def add_rect(self, row, col):
        xy = np.asarray([col,row])*[1,-1]
        rect = self.Canvas.AddRectangle(xy, (0.8,0.8), LineWidth = 1, FillColor = 'White')
        rect.Name = ''
        rect.gridpos = (row,col)
        self.__rects.append(rect)

        t = self.Canvas.AddText(rect.Name, xy+[0.3,0.4], Size = 9, Position = 'cc')
        rect.Text = t
        rect.Bind(FloatCanvas.EVT_FC_LEFT_DOWN, self.RectGotHitLeft)
        rect.Bind(FloatCanvas.EVT_FC_ENTER_OBJECT, self.RectMouseOver)
        rect.Bind(FloatCanvas.EVT_FC_LEAVE_OBJECT, self.RectMouseLeave)

    def reorder(self, frm, to):
        to.gridpos, frm.gridpos = frm.gridpos, to.gridpos
        wx.PostEvent(self, ReorderEvent(swap=(frm.gridpos, to.gridpos)))

    def redraw_rects(self, removed=[]):
        for r in removed:
            self.Canvas.RemoveObject(r.Text)
            self.Canvas.RemoveObject(r)

        for r in self.__rects:
            xy = np.asarray(r.gridpos[::-1])*[1,-1]
            r.SetPoint(xy)
            r.Text.SetText(r.Name)
            r.Text.SetPoint(xy+[0.3,0.4])
        self.Canvas.Draw(Force=True)

    @property
    def shape(self):
        return self.__shape
    @shape.setter
    def shape(self, shape):
        if np.asarray(shape).sum() < 2:
            shape = (1,1)
        remove = []
        for r in range(self.__shape[0],shape[0]): # only if new shape has more rows than current
            for c in range(self.__shape[1]):
                self.add_rect(r,c)
            self.__shape = shape[0],self.__shape[1]
        for c in range(self.__shape[1], shape[1]):
            for r in range(self.__shape[0]):
                self.add_rect(r,c)
            self.__shape = self.__shape[0],shape[1]
        for n in range(len(self.__rects)-1,-1,-1):
            if self.__rects[n].gridpos[0] >= shape[0]:
                remove.append(self.__rects.pop(n))
                self.__shape = shape[0], self.__shape[1]
        for n in range(len(self.__rects)-1,-1,-1):
            if self.__rects[n].gridpos[1] >= shape[1]:
                remove.append(self.__rects.pop(n))
                self.__shape = self.__shape[0], shape[1]
        if self.selection in remove:
            self.selection = self.__rects[0]
            self.Focus(self.__rects[0])
            wx.PostEvent(self, SelectEvent(pos=self.selection.gridpos, name=self.selection.Name))
        self.redraw_rects(remove)
        self.Canvas.ZoomToBB()

    @property
    def order(self):
        return [(q.gridpos, q.Name) for q in self.__rects if q.Name != '']

    def set_name(self, name, idx=None):
        if idx is None:
            self.selection.Name = name
            self.selection.Text.SetText(name)
        else:
            self.__rects[idx].Name = name
            self.__rects[idx].Text.SetText(name)
        self.Canvas.Draw(Force=True)

    def Init(self, shape=(1,1)):
        self.moving = None
        self.selection = None
        self.target = None

        self.__shape = shape
        self.__rects = Map()

        Canvas = self.Canvas

        for row,col in np.mgrid[0:shape[1],0:shape[0]].reshape((2,reduce(np.multiply, shape))).T:
            xy = np.asarray([col,row])*[1,-1]
            self.add_rect(row, col)

        self.Canvas.Bind(FloatCanvas.EVT_MOTION, self.OnDrag)
        self.Canvas.Bind(FloatCanvas.EVT_LEFT_UP, self.OnLeftUp)

        Canvas.ZoomToBB()
        self.redraw_rects()

        self.selection = self.__rects[0]

    def update_from_model(self, mpmodel):
        self.shape = mpmodel.shape
        for k,v in mpmodel.items():
            #print(k,v.name)
            self.__rects[k].Name = v.name

        self.redraw_rects()

        if self.selection is not None:
            self.Focus(self.__rects[0])
            wx.PostEvent(self, SelectEvent(pos=self.selection.gridpos, name=self.selection.Name))

    def OnDrag(self, evt):
        self.curr_coords = evt.Coords
        if self.moving is not None:
            x, y = evt.Coords
            ox, oy = self.moving.XY+self.offset
            self.moving.SetFillColor('Lime Green')
            self.moving.Move((x-ox,y-oy))
            self.moving.Text.Move((x-ox,y-oy))
            self.Canvas.Draw(Force=True)
            wx.GetApp().Yield(True)

    def RectGotHitLeft(self, Object):
        if self.selection == Object:
            return
        self.offset = self.curr_coords-Object.XY
        self.Focus(Object)
        self.selection = Object
        wx.PostEvent(self, SelectEvent(pos=Object.gridpos, name=Object.Name))
        self.moving = Object
        Object.PutInForeground()
        Object.Text.PutInForeground()

    def Focus(self, Object=None):
        if self.selection is not None:
            self.selection.SetLineColor('Black')
            self.selection.SetLineWidth(1)
        if Object is not None:
            Object.SetLineColor('Light Grey')
            Object.SetLineWidth(4)
        self.Canvas.Draw(Force=True)

    def RectMouseOver(self, Object):
        if Object == self.moving:
            return
        if self.moving is not None:
            Object.SetFillColor('Red')
            self.Canvas.Draw(Force=True)
            self.target = Object

    def RectMouseLeave(self, Object):
        if Object == self.moving:
            return
        if self.moving is not None:
            Object.SetFillColor('White')
            self.Canvas.Draw(Force=True)
            self.target = None

class PlotLayout(wx.Panel):
    def __init__(self, parent, **kwargs):
        super(PlotLayout, self).__init__(parent, **kwargs)

        self.setup_controls()
        self.layout()

        #self.pop.Bind(EVT_RECT_SELECT, self.OnSelect)
        self.ch_plot_pri.Bind(wx.EVT_CHOICE, self.OnChoice)
        self.ch_rows.Bind(wx.EVT_CHOICE, self.OnShape)
        self.ch_cols.Bind(wx.EVT_CHOICE, self.OnShape)

        wx.CallAfter(self.pop.Canvas.ZoomToBB)

    def setup_controls(self):
        self.pop = PlotOrderPanel(self, size=(200,150))
        self.pop.Init((1,1))

        self.ch_rows = wx.Choice(self, choices=[str(q) for q in range(1,6)])
        self.ch_rows.SetSelection(0)
        self.ch_cols = wx.Choice(self, choices=[str(q) for q in range(1,6)])
        self.ch_cols.SetSelection(0)

        self.ch_plot_pri = wx.Choice(self, choices=[''], name='pri')
        self.ch_plot_sec = wx.Choice(self, choices=[''], name='sec')

    def layout(self):
        b = wx.BoxSizer(wx.VERTICAL)
        fg = wx.FlexGridSizer(2, 2, 0, 0)
        fg.Add(wx.StaticText(self, label='Rows'), 0, wx.ALL|wx.ALIGN_CENTRE_VERTICAL, 5)
        fg.Add(self.ch_rows, 0, wx.EXPAND|wx.ALIGN_CENTRE_VERTICAL|wx.ALL, 5)
        fg.Add(wx.StaticText(self, label='Columns'), 0, wx.ALL|wx.ALIGN_CENTRE_VERTICAL, 5)
        fg.Add(self.ch_cols, 0, wx.EXPAND|wx.ALIGN_CENTRE_VERTICAL|wx.ALL, 5)
        hb = wx.BoxSizer(wx.HORIZONTAL)
        hb.Add(fg, 0, wx.EXPAND)
        hb.Add(self.pop, 1, wx.EXPAND)
        b.Add(hb, 1, wx.EXPAND)

        hb = wx.BoxSizer(wx.HORIZONTAL)
        hb.Add(wx.StaticText(self, label='Primary axis'), 0, wx.RIGHT|wx.ALIGN_CENTER_VERTICAL, 5)
        hb.Add(self.ch_plot_pri, 1, wx.EXPAND)
        hb.Add(wx.StaticText(self, label='Secondary axis'), 0, wx.LEFT|wx.ALIGN_CENTER_VERTICAL, 10)
        hb.Add(self.ch_plot_sec, 1, wx.LEFT|wx.EXPAND, 5)
        b.Add(hb, 0, wx.EXPAND|wx.ALL, 5)
        self.SetSizer(b)

    def update_from_model(self, mpmodel):
        print('plotlayout: update from model, mpmodel.shape',mpmodel.shape)
        rows,cols = mpmodel.shape

        self.ch_rows.SetSelection(max(0,rows-1))
        self.ch_cols.SetSelection(max(0,cols-1))

        def select(ctrl, uuid):
            if uuid is None:
                ctrl.Selection = 0
            else:
                for n in range(ctrl.Count):
                    if ctrl.GetClientData(n) == uuid:
                        ctrl.Selection = n

        if mpmodel.selected is not None:
            pd = mpmodel.selected

            select(self.ch_plot_pri, pd.plot_ref)
            select(self.ch_plot_sec, pd.plot_ref_secondary)
            self.ch_plot_sec.Enable(pd.plot_ref is not None)
        else:
            select(self.ch_plot_pri, None)
            select(self.ch_plot_sec, None)
            self.ch_plot_sec.Enable(False)

    def set_plot_choices(self, choices, clientdata):
        for ch in self.ch_plot_pri, self.ch_plot_sec:
            ch.Clear()
            for c,d in zip(choices,clientdata):
                ch.Append(c, d)

    @property
    def selection(self):
        sel = self.pop.selection
        return sel.gridpos if sel is not None else None

    def OnShape(self, evt):
        evt.Skip()
        self.pop.shape = self.ch_rows.GetSelection()+1,self.ch_cols.GetSelection()+1

    def OnSelect(self, evt):
        #TODO: remove
        logger.debug('plotlayout on select should not be called')
        evt.Skip()
        if evt.name != '':
            self.ch_plot_pri.SetSelection(self.ch_plot_pri.FindString(evt.name))
            self.ch_plot_sec.SetSelection(0)
        else:
            self.ch_plot_pri.SetSelection(0)
            self.ch_plot_sec.SetSelection(0)

    def OnChoice(self, evt):
        evt.Skip()
        sel = evt.GetEventObject().GetSelection()
        name = evt.GetEventObject().GetString(sel)
        self.pop.set_name(name if sel != 0 else '')

if __name__ == "__main__":
    class DemoFrame(wx.Frame):
        def __init__(self):
            super(DemoFrame, self).__init__(None)
            self.panel = PlotLayout(self)

    class DemoApp(wx.App):
        def __init__(self, *args, **kwargs):
            wx.App.__init__(self, *args, **kwargs)

        def OnInit(self):
            frame = DemoFrame()
            frame.Show()
            return True

    app = DemoApp(False)
    app.MainLoop()


"""
Created on Jul 30, 2012

@author: hrabe
"""

import unittest
from pytom.gpu.initialize import xp
import numpy as np
import os

class pytom_IOTest(unittest.TestCase):
    def setUp(self):
        self.fnames = []
        self.epsilon = 1E-4
        self.outfolder = 'IOTest'
        self.rotation_angles = [10.5, 23.5, -32.5]
        self.sx, self.sy, self.sz=10,11,12
        self.tilt_angle = 132.23
        self.pixel_size = 2.62
        if not os.path.exists(self.outfolder):
            os.mkdir(self.outfolder)

    def tearDown(self):
        from helper_functions import remove_tree
        remove_tree(self.outfolder)

    def read(self):
        """
        test general read function
        """
        from pytom.basic.files import read

        v = read(f'./testData/ribo.em')
        self.assertTrue( v.size_x() == 100 and v.size_y() == 100 and v.size_z() == 100)

        v = read(f'./testData/ribo.em',binning=[4,4,4])
        assert v.size_x() == 25 and v.size_y() == 25 and v.size_z() == 25

    def readem(self):
        """
        test readem
        """
        from pytom.basic.files import read_em

        int2_file = f'./testData/int2.em'
        (image, header) = read_em(int2_file)
        inibytes = header.get_1st4bytes()
        self.assertTrue(inibytes[3] == 5,
                        'Datatype number is wrong! should have been changed to 5 from 2 in initial file')
        dims = header.get_dim()

        #now binning
        (image, header) = read_em(int2_file, binning=[4, 4, 1])
        bdims = header.get_dim()
        self.assertTrue(bdims[0]*4 == dims[0],
                        'Dimensions of binned file are wrong!')

    def write_read_MRC(self):
        self.data_write_read('mrc')

    def write_read_EM(self):
        from pytom.agnostic.io import write, read

        fname = f'{self.outfolder}/dummy_reading.em'
        self.fnames.append(fname)
        data = xp.ones((self.sx, self.sy, self.sz))

        write(fname, data)
        self.assertTrue(os.path.exists(fname))

        data2 = read(fname)
        self.assertTrue((data != data2).sum() == 0)

    def write_read_STAR(self):
        from pytom.agnostic.io import write, read

        fname = f'testData/example_starfile.star'

        data2 = read(fname)
        oname = f'{self.outfolder}/dummy_star.star'
        self.fnames.append(oname)
        write(oname, data2)
        self.assertTrue(os.path.exists(oname))

        data2 = read(fname)
        oname = f'{self.outfolder}/dummy_star2.star'
        self.fnames.append(oname)
        write(oname, data2)
        self.assertTrue(os.path.exists(oname))

        fname = 'testData/relion_with_optics_group.star'
        data3 = read(fname)
        data4 = read(fname, read_optics_group=True)

        self.assertTrue(len(data3) == 60,
                        f'Error reading data from {fname}: epected {78} lines, found {len(data3)} lines')
        self.assertTrue(len([data4]) == 1,
                        f'Error reading data from {fname}: epected { 1} lines, found {len([data4])} lines')

    def write_read_REC(self):
        self.data_write_read('rec')

    def write_read_ST(self):
        self.data_write_read('st')

    def read_LOG(self):
        from pytom.agnostic.io import read
        fname = f'testData/taSolution.log'
        a = read(fname)

    def write_read_TXT(self):
        from pytom.agnostic.io import write, read
        from pytom.basic.datatypes import DATATYPE_METAFILE, FMT_METAFILE, HEADER_METAFILE

        fname = f'{self.outfolder}/data.meta'
        # Structured arrays are not yet supported in cupy 12.0
        data = np.zeros((10), dtype=DATATYPE_METAFILE)
        data['TiltAngle'] = range(1, 11)

        for i in range(10):
            data['FileName'][i] = f'sorted_{i:02d}.mrc'

        write(fname, data, fmt=FMT_METAFILE, header=HEADER_METAFILE)
        self.assertTrue(os.path.exists(fname), f'writing {fname} failed')

        data = read(fname, dtype=DATATYPE_METAFILE)
        self.assertTrue(data['TiltAngle'][9] == 10, f'reading {fname} failed')

    def read_size(self):
        from pytom.agnostic.io import read_size

        for d, sized in ( ('x', self.sx), ('y', self.sy), ('z', self.sz)):
            x = read_size(self.fnames[0], d)
            self.assertTrue(x==sized, f'size in {d}-dimension is off: found {x}, expected {sized}')

        x,y,z = read_size(self.fnames[0])
        self.assertTrue(x==self.sx and y==self.sy and z==self.sz,
                        f'Reading sizes failed:\n\texpected {self.sx}.{self.sy},{self.sz}\n\tfound {x},{y},{z}')

    def read_tilt_angle(self):
        from pytom.agnostic.io import read_tilt_angle

        a = read_tilt_angle(self.fnames[1])
        self.assertTrue(xp.abs(a-self.tilt_angle) < self.epsilon,
                        f'Tilt Angle is not correct: found {a}, expected {self.tilt_angle}')

    def read_header(self):
        from pytom.agnostic.io import read_header
        header = read_header(self.fnames[1])
        assert header.any()

    def read_pixelsize(self):
        from pytom.agnostic.io import read_pixelsize

        fname = self.fnames[1]

        a = read_pixelsize(fname)[0]
        self.assertTrue(xp.abs(a - self.pixel_size) < self.epsilon,
                    f'Tilt Angle is not correct: found {a}, expected {self.pixel_size}')

    def data_write_read(self, extension='mrc'):
        from pytom.agnostic.io import write, read

        fname = f'{self.outfolder}/dummy_reading.{extension.lower()}'
        self.fnames.append(fname)
        data = xp.random.random((self.sx,self.sy,self.sz)).astype(xp.float32)

        write(fname, data, pixel_size=self.pixel_size, tilt_angle=self.tilt_angle)
        self.assertTrue(os.path.exists(fname), f'writing {fname} failed')

        data2 = read(fname)

        write(f'{self.outfolder}/testing.mrc', data2 -data)
        write(f'{self.outfolder}/testing2.mrc', data2 )

        self.assertTrue(xp.abs(data - data2).sum() < self.epsilon, f'reading {fname} failed: input and output files are different')

    def test_run(self):
        self.read()
        self.readem()
        self.write_read_EM()
        self.write_read_MRC()
        self.write_read_REC()
        self.write_read_ST()
        self.write_read_TXT()
        self.read_LOG()
        self.write_read_STAR()
        self.read_size()
        self.read_tilt_angle()
        self.read_header()
        self.read_pixelsize()

if __name__ == '__main__':
    unittest.main()


#!/usr/bin/env python3
# Leetcode 42 : Trapping Rain Water
# Given n non-negative integers representing an elevation map where the
# width of each bar is 1, compute how much water it can trap after raining.

def trapBruteForce( height ):
   # time = O( n^2 )
   # space = O( 1 )
   size = len( height )

   trap = 0
   for i in range( size ):
      maxLeft, maxRight = 0, 0

      for j in range( i-1, -1, -1 ):
         if height[ j ] > maxLeft:
            maxLeft = height[ j ]

      for k in range( i+1, size ):
         if height[ k ] > maxRight:
            maxRight = height[ k ]

      curMaxHeight = min( maxLeft, maxRight )
      if curMaxHeight > height[ i ]:
         trap += curMaxHeight - height[ i ]

   return trap

def trapDynamicProgramming( height ):
   # time = O( n )
   # space = O( n )
   # index  : 0 1 2 3 4 5 6 7 8 9 10 11
   # height : 0 1 0 2 1 0 1 3 2 1  2  1
   # left   : 0 0 1 1 2 2 2 3 3 3  3  3
   # right  : 3 3 3 3 3 3 3 2 2 2  1  0
   # curMax : 0 0 1 1 2 2 2 2 2 2  1  0
   size = len( height )
   left, right = [0]*size, [0]*size

   left[ 0 ] = 0
   for i in range( 1, size ):
      left[ i ] = max( left[ i-1 ], height[ i-1] )

   right[ size - 1 ] = 0
   for i in range( size - 2, -1, -1 ):
      right[ i ] = max( right[ i+1 ], height[ i+1 ] )

   trap = 0
   for i in range( size ):
      if left[ i ] != 0 and right[ i ] != 0:
         curMaxHeight = min( left[ i ], right[ i ] )
         if curMaxHeight > height[ i ]:
            trap += curMaxHeight - height[ i ]

   return trap

def trapStack( height ):
   # time = O(n)
   # space = O(n)
   # very very very complicated
   size = len( height )
   stack = []
   trap = 0

   # index  : 0 1 2 3 4 5 6 7 8 9 10 11
   # height : 0 1 0 2 1 0 1 3 2 1  2  1
   # i   stack    pop   distance    curHeight                              stack
   # 0   []                                                                0
   # 1   0        0                                                        1
   # 2   1                                                                 1,2
   # 3   1,2      2     3-1-1=1     min(h[1],h[3])-h[2] = min(1,2)-0 = 1   1
   #     1        1                                                        3
   # 4   3                                                                 3,4
   # 5   3,4                                                               3,4,5
   # 6   3,4,5    5     6-4-1=1     min(h[4],h[6])-h[5] = min(1,1)-0 = 1   3,4,6
   # 7   3,4,6    6     7-4-1=2     min(h[4],h[7])-h[6] = min(1,3)-1 = 0   3,4
   #     3,4      4     7-3-1=3     min(h[3],h[7])-h[4] = min(2,3)-1 = 1   3
   #     3                                                                 7
   # 8   7                                                                 7,8
   # 9   7,8                                                               7,8,9
   # 10  7,8,9    9     10-8-1=1    min(h[10],h[8])-h[9] = min(2,2)-1 = 1  7,8,10
   # 11  7,8,10                                                            7,8,10,11
   for i in range( size ):
      while stack and height[ i ] > height[ stack[ -1 ] ]:
         top = stack.pop( -1 )
         if not stack:
            break
         distance = i - stack[ -1 ] - 1
         curHeight = min( height[ stack[ -1 ] ], height[ i ] ) - height[ top ]
         trap += distance * curHeight
      stack.append( i )

   return trap

def trapTwoPointer( height ):
   # time = O( n )
   # space = O( 1 )
   # There is two borders: left and right. The level of trapped water depends on
   # min( height[ left ], height[ right ] ). If height[ left ] < height[ right ],
   # move left up by 1 and vice versa. Once either left or right reaches the peak,
   # it will never move again. The other end will move toward it.
   size = len( height )
   left, right = 0, size - 1
   leftMax, rightMax = 0, 0

   trap = 0
   while left < right:
      if height[ left ] < height[ right ]:
         if height[ left ] >= leftMax:
            leftMax = height[ left ]
         else:
            trap += leftMax - height[ left ]
         left += 1
      else:
         if height[ right ] >= rightMax:
            rightMax = height[ right ]
         else:
            trap += rightMax - height[ right ]
         right -= 1

   return trap

testCases = [
      {
         'height' : [0,1,0,2,1,0,1,3,2,1,2,1],
         'output' : 6,
      },
      {
         'height' : [4,2,0,3,2,5],
         'output' : 9,
      },
]

for test in testCases:
   h = test[ 'height' ]
   o = test[ 'output' ]
   assert( trapBruteForce( h ) == o )
   assert( trapDynamicProgramming( h ) == o )
   assert( trapStack( h ) == o )
   assert( trapTwoPointer( h ) == o )


class Hiku():
	""" This class lets you write a hiku and save it on text file"""
	def __init__(self,name,date):
		"""initelizing"""
		self.name = name #the name of the hiku
		self.date = date
		self.hiku = []
	def write_hiku(self):
		"""reciving each indevidual line of the hiku from the user"""
		count = 0
		while(count<3):
			line = input("Line {0}:".format(count+1))
			line = line.split()
			syllables = 0
			for i in line:
				syllables += self.syllable_count(i)
			if((count == 0 or count == 2) and syllables !=5):#chcking if the first and lasst lines have 5 syllables
				print("You must have 5 syllables.\nYouu only have {0} syllables".format(syllables))
				continue
			elif(count == 1 and syllables != 7):#checking if the secod line has 7 syllabels
				print("You must have 7 syllables.\nYouu only have {0} syllables".format(syllables))
				continue
			" ".join(line)
			self.hiku.append(" ".join(line))
			count +=1

	def syllable_count(self,word):
		"""counting the number of syllabels in indevidual words"""
		word = word.lower()
		count = 0
		vowels = "aeiouy"
		if word[0] in vowels:
			count += 1
		for index in range(1, len(word)):
			if word[index] in vowels and word[index - 1] not in vowels:
				count += 1
		if word.endswith("e"):
			count -= 1
		if count == 0:
			count += 1
		return count
	def save_list(self,fileName,listName):
		"""saving an inputed list in to a text file along with the name and date"""
		F = open(fileName+".txt","w+")
		F.write(self.date+"\n")
		F.write(fileName+"\n")
		for i in listName:
			print(i)
			F.write(i+"\n")
		F.close()
hiku = Hiku(input("What is the name of your hiku\n"),input("What is the date?\n"))
hiku.write_hiku()
hiku.save_list(hiku.name,hiku.hiku)

#made by Fizzy Bubalech


import numpy as np
import scipy.optimize
import molsturm
import molsturm.posthf

def optimize_geometry(geometryfctn, geometry_params_guess, conv_tol,
                      level="hf", **params):
    # Start from hcore guess
    recent_hf_res = "hcore"
    recent_hf_res_for_args = None

    # The function to minimise
    def objective_function(args):
        nonlocal recent_hf_res
        nonlocal recent_hf_res_for_args

        # Update geometry and run calculation:
        system = geometryfctn(args)
        res = molsturm.hartree_fock(system, conv_tol=conv_tol / 10,
                                    guess=recent_hf_res, **params)
        print("arguments: ", args, "niter:", res["n_iter"])

        # Update the stored recent_hf_res but only if it differs enough
        # from the one we currently have stored
        if recent_hf_res_for_args is None or \
           np.max(np.abs(np.array(recent_hf_res_for_args) - np.array(args))) > conv_tol:
            recent_hf_res_for_args = args
            recent_hf_res = res
            print("  -> guess update")

        if level == "hf":
            return res["energy_ground_state"]
        elif level == "mp2":
            res = molsturm.posthf.mp2(res)
            return res["energy_ground_state"]
        else:
            raise NotImplementedError("Level of theory not yet implemented")

    res = scipy.optimize.minimize(objective_function, geometry_params_guess, tol=conv_tol)
    print(res)

def optimize_h2o(rHO_guess, angHO_guess, conv_tol, level="hf", **params):
    def construct_system(r_theta):
        r, theta = r_theta
        return molsturm.MolecularSystem(
            atoms=["O", "H", "H"],
            coords=[
                (0, 0, 0),
                (r, 0, 0),
                (r * np.cos(theta), r * np.sin(theta), 0)
            ]
        )
    return optimize_geometry(construct_system, (rHO_guess, angHO_guess), conv_tol,
                             level, **params)

if __name__ == "__main__":
    optimize_h2o(1.6, 105, conv_tol=1e-7, level="hf", basis_type="gaussian",
                 basis_set_name="def2-sv(p)")


lst = ['a','e','i','o','u']

while True:
    cnt = 0
    n = list(input().lower())

    if n[0] == '#':
        break

    for i in n:
        if i in lst:
            cnt += 1
    print(cnt)


# Описать функцию InvertDigits(K), меняющую порядок следования цифр целого положительного числа К на обратный
# (К-параметр целого типа, являющийся одновременно входным и выходным).
# С помощью этой функции поменять порядок следования цифр на обратный для каждого из пяти данных целых чисел.

def invert(k):                          # задание функции
    b = 0
    while k > 0:
        digit = k % 10
        k = k // 10
        b = b * 10 + digit
    return b

k = int(input("Введите число: "))
print("Обратное число: ", invert(k))


class node:
    def __init__(self,data):
        self.data = data
        self.next = None
        self.down = None
class Snode:
    def __init__(self,data):
        self.data = data
        self.down = None
class link:
    def __init__(self):
        self.head = node(None)

    def next_node(self,data):
        if self.search(data.data):
            return
        p = self.head
        while p.next is not None:
            p = p.next
        p.next = data

    def search(self,data):
        p = self.head
        while p and p.data != data:
            p = p.next
        return p

    def next_secondary_node(self,n,data):
        p = self.search(n)
        if p != self.head:
            while p.down is not None:
                p = p.down
            p.down = data

    def show_all(self):
        p = self.head.next
        while p is not None:
            print(f'{p.data} : ',end='')
            p_down_node = p.down
            while p_down_node is not None:
                print(f'{p_down_node.data},',end='')
                p_down_node = p_down_node.down
            print()
            p = p.next

inp = input("input : ").split(",")
l = link()
for i in inp:
    u = i.split(" ")
    if u[0] == "ADN":
        l.next_node(node(u[1]))
    elif u[0] == "ADSN":
        h = u[1].split("-")
        l.next_secondary_node(h[0],Snode(h[1]))
l.show_all()


import requests
from rgx import clean_company_name

def connect_functions():
    response = requests.get('http://127.0.0.1:5000/companies')
    companies = response.json()['companies']  # get the list of companies
    clean_companies = []

    for company in companies:

        name = company['name']
        purified_name = clean_company_name(name)

        post_data = {
            purified_name: {
                     'country iso': company['country iso'],
                     'city': company['city'],
                     'nace': company['nace'],
                     'website': company['website']}}

        post_response = requests.post('http://127.0.0.1:5000/cleanedCompanies', json=post_data)

        if post_response.status_code != 200:
            print(f'Error posting {name}: {post_response.text}')
        else:
            print(f'Successfully posted {name}!')

        print(f'Total {len(clean_companies)} companies successfully posted.')

connect_functions()


from hls_images_matcher import HlsImagesMatcher
from hls_image_matcher import HlsImageMatcher

def main():
    temp = 'cat_test2.jpg'
    sources = ['cat_test.jpeg', 'hoodie cat.jpg', 'surprised cat.jpg']
    matcher = HlsImagesMatcher(temp, sources)
    matcher.match_and_display_images()

    # Lightness and Saturation matching only
    matcher.match_and_display_images(hue=False)

    template= 'cat_test2.jpg'
    source = 'cat_test.jpeg'
    matcher2 = HlsImageMatcher(template, source)
    matcher2.graph_histogram()

    # Lightness and Saturation matching only
    matcher2.graph_histogram(hue=False)

if __name__ == '__main__':
    main()


# 나이트, 비숍, 룩 중에 선택해서 1에 놓는다
# 1초에 할 수 있는 행동은 움직이거나, 다른 말로 변경한다.
# 방문했던 곳을 다시 방문할 수 도 있고, 차례가 아닌 다른 숫자라도 방문할 수 있다.
# 0 나이트, 1 비숍, 2 룩 이라고 가정
import sys
from collections import deque

answer = -1
n = int(input())
a = [list(map(int, sys.stdin.readline().split())) for _ in range(n)]
d = [[[[-1] * (n * n) for _ in range(3)] for _ in range(n)] for _ in range(n)]
dx1 = [-1, -2, -2, -1, 1, 2, 2, 1]
dy1 = [-2, -1, 1, 2, 2, 1, -1, -2]
dx2 = [-1, -1, 1, 1]
dy2 = [-1, 1, 1, -1]
dx3 = [1, -1, 0, 0]
dy3 = [0, 0, 1, -1]
queue = deque()

for i in range(n):
    for j in range(n):
        a[i][j] -= 1

        if a[i][j] == 0:
            for k in range(3):
                queue.append((i, j, k, 0))
                d[i][j][k][0] = 0

while queue:
    x, y, h, s = queue.popleft()

    if s == n * n - 1:
        if answer == -1 or answer > d[x][y][h][s]:
            answer = d[x][y][h][s]
        continue

    # 말 바꾸기
    for i in range(3):
        if i == h:
            continue

        if d[x][y][i][s] == -1:
            queue.append((x, y, i, s))
            d[x][y][i][s] = d[x][y][h][s] + 1

    if h == 0:  # 나이트
        for i in range(8):
            nx, ny, ns = x + dx1[i], y + dy1[i], s

            if 0 <= nx < n and 0 <= ny < n:
                if a[nx][ny] == s + 1:
                    ns = s + 1

                if d[nx][ny][h][ns] == -1:
                    queue.append((nx, ny, h, ns))
                    d[nx][ny][h][ns] = d[x][y][h][s] + 1
    elif h == 1:  # 비숍
        for i in range(4):
            step = 1

            while True:
                nx, ny, ns = x + (dx2[i] * step), y + (dy2[i] * step), s

                if 0 <= nx < n and 0 <= ny < n:
                    if a[nx][ny] == s + 1:
                        ns = s + 1

                    if d[nx][ny][h][ns] == -1:
                        queue.append((nx, ny, h, ns))
                        d[nx][ny][h][ns] = d[x][y][h][s] + 1

                    step += 1
                else:
                    break
    else:  # 룩
        for i in range(4):
            step = 1

            while True:
                nx, ny, ns = x + (dx3[i] * step), y + (dy3[i] * step), s

                if 0 <= nx < n and 0 <= ny < n:
                    if a[nx][ny] == s + 1:
                        ns = s + 1

                    if d[nx][ny][h][ns] == -1:
                        queue.append((nx, ny, h, ns))
                        d[nx][ny][h][ns] = d[x][y][h][s] + 1

                    step += 1
                else:
                    break

print(answer)


from django.http import Http404
from django.shortcuts import render

# Create your views here.
from rest_framework import permissions, status
from rest_framework.views import APIView
from rest_framework.response import Response

from devices.models import Device
from devices.serializers import SerDeviceList, SerDeviceDetail

class DeviceList(APIView):

    def get(self, request, format=None):
        returndata = [['ID', 'Name', 'Diameter', 'Height']]
        temparray = []
        device = Device.objects.all()
        serializer = SerDeviceList(device, many=True)
        for content in serializer.data:
            for k, v in content.items():
                temparray.append(v)
            returndata.append(temparray)
            temparray = []
        return Response(returndata)

class DeviceDetail(APIView):

    def get_object(self, pk):
        try:
            return Device.objects.get(pk=pk)
        except Device.DoesNotExist:
            raise Http404

    def get(self, request, pk, format=None):
        snippet = self.get_object(pk)
        serializer = SerDeviceDetail(snippet)
        return Response(serializer.data)

    def put(self, request, pk, format=None):
        tank = Device.objects.get(pk=pk)
        serializer = SerDeviceDetail(tank, data=request.data["payload"])
        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data, status=status.HTTP_201_CREATED)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

    def perform_create(self, serializer):
        serializer.save(owner=self.request.user)

    def delete(self, request, pk, format=None):
        snippet = self.get_object(pk)
        snippet.delete()
        return Response(status=status.HTTP_204_NO_CONTENT)


# coding=utf-8
# lifxlan.py
# Author: Meghan Clark

from random import randint
from socket import AF_INET, SOCK_DGRAM, SOL_SOCKET, SO_BROADCAST, SO_REUSEADDR, socket, timeout
from time import sleep, time
import os

from .device import DEFAULT_ATTEMPTS, DEFAULT_TIMEOUT, Device, UDP_BROADCAST_IP_ADDRS, UDP_BROADCAST_PORT
from .errors import InvalidParameterException, WorkflowException
from .light import Light
from .message import BROADCAST_MAC
from .msgtypes import Acknowledgement, GetService, LightGet, LightGetPower, LightSetColor, LightSetPower, \
    LightSetWaveform, LightState, LightStatePower, StateService
from .multizonelight import MultiZoneLight
from .unpack import unpack_lifx_message
from .group import Group

class LifxLAN:
    def __init__(self, num_lights=None, verbose=False):
        self.source_id = os.getpid()
        self.num_devices = num_lights
        self.num_lights = num_lights
        self.devices = None
        self.lights = None
        self.verbose = verbose

    ############################################################################
    #                                                                          #
    #                         LAN (Broadcast) API Methods                      #
    #                                                                          #
    ############################################################################

    def get_devices(self):
        if self.devices == None:
            self.discover_devices()
        return self.devices

    def get_lights(self):
        if self.lights == None:
            self.discover_devices()
        return self.lights

    # more of an internal helper function
    # forces a refresh of the internal list of available devices
    def discover_devices(self):
        self.lights = []
        self.devices = []
        if self.num_lights == None:
            responses = self.discover()
        else:
            try:
                responses = self.broadcast_with_resp(GetService, StateService)
            except WorkflowException as e:
                raise
                #return None
        for r in responses:
            device = Device(r.target_addr, r.ip_addr, r.service, r.port, self.source_id, self.verbose)
            if device.is_light():
                if device.supports_multizone():
                    device = MultiZoneLight(r.target_addr, r.ip_addr, r.service, r.port, self.source_id, self.verbose)
                else:
                    device = Light(r.target_addr, r.ip_addr, r.service, r.port, self.source_id, self.verbose)
                self.lights.append(device)
            self.devices.append(device)
        self.num_lights = len(self.lights)
        self.num_devices = len(self.lights)

    def get_multizone_lights(self):
        multizone_lights = []
        all_lights = self.get_lights()
        for l in all_lights:
            if l.supports_multizone():
                multizone_lights.append(l)
        return multizone_lights

    def get_infrared_lights(self):
        infrared_lights = []
        all_lights = self.get_lights()
        for l in all_lights:
            if l.supports_infrared():
                infrared_lights.append(l)
        return infrared_lights

    def get_color_lights(self):
        color_lights = []
        all_lights = self.get_lights()
        for l in all_lights:
            if l.supports_color():
                color_lights.append(l)
        return color_lights

    def get_device_by_name(self, name):
        device = None
        all_devices = self.get_devices()
        for d in all_devices:
            if d.get_label() == name:
                device = d
        if device == None:               # didn't find it?
            self.discover_devices()      # update list in case it is out of date
            all_devices = self.get_devices()
            for d in all_devices:            # and try again
                if d.get_label() == name:
                    device = d
        return device

    # takes in list of strings, returns Group of devices
    def get_devices_by_name(self, names):
        devices = []
        all_devices = self.get_devices()
        for d in all_devices:
            if d.get_label() in names:
                devices.append(d)
        if len(devices) != len(names):  # didn't find everything?
            self.discover_devices()     # update list in case it is out of date
            all_devices = self.get_devices()
            for d in all_devices:       # and try again
                if d.get_label() in names:
                    devices.append(d)
        return Group(devices)

    def get_devices_by_group(self, group):
        devices = []
        all_devices = self.get_devices()
        for d in all_devices:
            if d.get_group() == group:
                devices.append(d)
        return Group(devices)

    def get_devices_by_location(self, location):
        devices = []
        all_devices = self.get_devices()
        for d in all_devices:
            if d.get_location() == location:
                devices.append(d)
        return Group(devices)

    # returns dict of Light: power_level pairs
    def get_power_all_lights(self):
        responses = self.broadcast_with_resp(LightGetPower, LightStatePower)
        power_states = {}
        for light in self.lights:
            for response in responses:
                if light.mac_addr == response.target_addr:
                    power_states[light] = response.power_level
        return power_states

    def set_power_all_lights(self, power_level, duration=0, rapid=False):
        on = [True, 1, "on", 65535]
        off = [False, 0, "off"]
        try:
            if power_level in on and not rapid:
                self.broadcast_with_ack(LightSetPower, {"power_level": 65535, "duration": duration})
            elif power_level in on and rapid:
                self.broadcast_fire_and_forget(LightSetPower, {"power_level": 65535, "duration": duration}, num_repeats=5)
            elif power_level in off and not rapid:
                self.broadcast_with_ack(LightSetPower, {"power_level": 0, "duration": duration})
            elif power_level in off and rapid:
                self.broadcast_fire_and_forget(LightSetPower, {"power_level": 0, "duration": duration}, num_repeats=5)
            else:
                raise InvalidParameterException("{} is not a valid power level.".format(power_level))
        except WorkflowException as e:
            raise

    def get_color_all_lights(self):
        responses = self.broadcast_with_resp(LightGet, LightState)
        colors = {}
        for light in self.lights:
            for response in responses:
                if light.mac_addr == response.target_addr:
                    colors[light] = response.color
        return colors

    def set_color_all_lights(self, color, duration=0, rapid=False):
        if len(color) == 4:
            try:
                if rapid:
                    self.broadcast_fire_and_forget(LightSetColor, {"color": color, "duration": duration}, num_repeats=5)
                else:
                    self.broadcast_with_ack(LightSetColor, {"color": color, "duration": duration})
            except WorkflowException as e:
                raise
        else:
            raise InvalidParameterException("{} is not a valid color.".format(color))

    def set_waveform_all_lights(self, is_transient, color, period, cycles, duty_cycle, waveform, rapid=False):
        if len(color) == 4:
            try:
                if rapid:
                    self.broadcast_fire_and_forget(LightSetWaveform, {"transient": is_transient, "color": color, "period": period, "cycles": cycles, "duty_cycle": duty_cycle, "waveform": waveform}, num_repeats=5)
                else:
                    self.broadcast_with_ack(LightSetWaveform, {"transient": is_transient, "color": color, "period": period, "cycles": cycles, "duty_cycle": duty_cycle, "waveform": waveform})
            except WorkflowException as e:
                raise
        else:
            raise InvalidParameterException("{} is not a valid color.".format(color))

    ############################################################################
    #                                                                          #
    #                            Workflow Methods                              #
    #                                                                          #
    ############################################################################

    def discover(self, timeout_secs=DEFAULT_TIMEOUT, num_repeats=3):
        self.initialize_socket(timeout_secs)
        msg = GetService(BROADCAST_MAC, self.source_id, seq_num=0, payload={}, ack_requested=False, response_requested=True)
        responses = []
        addr_seen = []
        num_devices_seen = 0
        attempts = 0
        while attempts < num_repeats:
            sent = False
            start_time = time()
            timedout = False
            while not timedout:
                if not sent:
                    for ip_addr in UDP_BROADCAST_IP_ADDRS:
                        self.sock.sendto(msg.packed_message, (ip_addr, UDP_BROADCAST_PORT))
                    sent = True
                    if self.verbose:
                        print("SEND: " + str(msg))
                try:
                    data, (ip_addr, port) = self.sock.recvfrom(1024)
                    response = unpack_lifx_message(data)
                    response.ip_addr = ip_addr
                    if self.verbose:
                        print("RECV: " + str(response))
                    if type(response) == StateService and response.source_id == self.source_id:
                        if response.target_addr not in addr_seen and response.target_addr != BROADCAST_MAC:
                            addr_seen.append(response.target_addr)
                            num_devices_seen += 1
                            responses.append(response)
                except timeout:
                    pass
                elapsed_time = time() - start_time
                timedout = True if elapsed_time > timeout_secs else False
            attempts += 1
        self.close_socket()
        return responses

    def broadcast_fire_and_forget(self, msg_type, payload={}, timeout_secs=DEFAULT_TIMEOUT, num_repeats=DEFAULT_ATTEMPTS):
        self.initialize_socket(timeout_secs)
        msg = msg_type(BROADCAST_MAC, self.source_id, seq_num=0, payload=payload, ack_requested=False, response_requested=False)
        sent_msg_count = 0
        sleep_interval = 0.05 if num_repeats > 20 else 0
        while(sent_msg_count < num_repeats):
            for ip_addr in UDP_BROADCAST_IP_ADDRS:
                self.sock.sendto(msg.packed_message, (ip_addr, UDP_BROADCAST_PORT))
            if self.verbose:
                print("SEND: " + str(msg))
            sent_msg_count += 1
            sleep(sleep_interval) # Max num of messages device can handle is 20 per second.
        self.close_socket()

    def broadcast_with_resp(self, msg_type, response_type, payload={}, timeout_secs=DEFAULT_TIMEOUT, max_attempts=DEFAULT_ATTEMPTS):
        if self.lights == None:
            self.get_lights()
        success = False
        self.initialize_socket(timeout_secs)
        if response_type == Acknowledgement:
            msg = msg_type(BROADCAST_MAC, self.source_id, seq_num=0, payload=payload, ack_requested=True, response_requested=False)
        else:
            msg = msg_type(BROADCAST_MAC, self.source_id, seq_num=0, payload=payload, ack_requested=False, response_requested=True)
        responses = []
        addr_seen = []
        num_devices_seen = 0
        attempts = 0
        while num_devices_seen < self.num_devices and attempts < max_attempts:
            sent = False
            start_time = time()
            timedout = False
            while num_devices_seen < self.num_devices and not timedout:
                if not sent:
                    for ip_addr in UDP_BROADCAST_IP_ADDRS:
                        self.sock.sendto(msg.packed_message, (ip_addr, UDP_BROADCAST_PORT))
                    sent = True
                    if self.verbose:
                        print("SEND: " + str(msg))
                try:
                    data, (ip_addr, port) = self.sock.recvfrom(1024)
                    response = unpack_lifx_message(data)
                    response.ip_addr = ip_addr
                    if self.verbose:
                        print("RECV: " + str(response))
                    if type(response) == response_type and response.source_id == self.source_id:
                        if response.target_addr not in addr_seen and response.target_addr != BROADCAST_MAC:
                            addr_seen.append(response.target_addr)
                            num_devices_seen += 1
                            responses.append(response)
                            if num_devices_seen >= self.num_devices:
                                success = True
                except timeout:
                    pass
                elapsed_time = time() - start_time
                timedout = True if elapsed_time > timeout_secs else False
            attempts += 1
        if success == False:
            self.close_socket()
            raise WorkflowException("Did not receive {} in response to {}".format(str(response_type), str(msg_type)))
        else:
            self.close_socket()
        return responses

    def broadcast_with_ack(self, msg_type, payload={}, timeout_secs=DEFAULT_TIMEOUT+0.5, max_attempts=DEFAULT_ATTEMPTS):
        self.broadcast_with_resp(msg_type, Acknowledgement, payload, timeout_secs, max_attempts)

    # Not currently implemented, although the LIFX LAN protocol supports this kind of workflow natively
    def broadcast_with_ack_resp(self, msg_type, response_type, payload={}, timeout_secs=DEFAULT_TIMEOUT+0.5, max_attempts=DEFAULT_ATTEMPTS):
        pass

    ############################################################################
    #                                                                          #
    #                              Socket Methods                              #
    #                                                                          #
    ############################################################################

    def initialize_socket(self, timeout):
        self.sock = socket(AF_INET, SOCK_DGRAM)
        self.sock.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
        self.sock.setsockopt(SOL_SOCKET, SO_BROADCAST, 1)
        self.sock.settimeout(timeout)
        try:
            self.sock.bind(("", 0))  # allow OS to assign next available source port
        except Exception as err:
            raise WorkflowException("WorkflowException: error {} while trying to open socket".format(str(err)))

    def close_socket(self):
        self.sock.close()

def test():
    pass

if __name__=="__main__":
    test()


import tensorflow as tf
from tensorflow.python.keras.preprocessing.image import ImageDataGenerator, img_to_array
from tensorflow.python.keras.preprocessing.image import load_img
from tensorflow.python.keras.models import load_model

import cv2
import numpy as np

img_name_arr = []
predict_arr=[]
answer_arr=[]
img_arr = []
label_idx = []

PREDICT_MODE = 'test'
def get_key(dict, value):
    return [k for k, v in dict.items() if v==value]

if PREDICT_MODE == 'train':
    with open('txt/training_labels.txt') as f:
            for line in f.readlines():
                line = line.strip('\n')
                answer_arr.append(line)
                line = line.split(' ')
                image_name = line[0]
                class_name = line[1][4:]

                img_name_arr.append(image_name)
else:
    with open('txt/testing_img_order.txt', 'r') as f:
        for line in f:
            img_name_arr.append(line.strip('\n').split(',')[0])

train_gen = ImageDataGenerator(

)
train_ds = train_gen.flow_from_directory(
    'dataset/train',
    target_size=(480,480),
    batch_size=32,
    class_mode='categorical'
)

model = load_model('bestModelPath', compile=True)
model.summary()

label_idx = train_ds.class_indices

with open('answer/answer.txt', 'w') as f:
    for i in range(len(img_name_arr)):
        path ='src' + '/' + PREDICT_MODE + '/' + img_name_arr[i]
        #path = 'dataset/ttt' + '/' + img_name_arr[i]
        img = cv2.imread(path)
        img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
        img = cv2.resize(img, (480,480))
        img = img /255.
        img = img.astype('float32')
        img = tf.reshape(img, (-1,480,480,3))
        result_class = np.argmax(model.predict(img))
        res_str = get_key(label_idx, result_class)
        answer_str = img_name_arr[i] + ' ' + res_str[0]
        f.write(answer_str + '\n')
        predict_arr.append(answer_str)
        print(answer_str)

if PREDICT_MODE == 'train':
    correct_num =0.
    error_num=0.
    for i in range(len(predict_arr)):
        if predict_arr[i] == answer_arr[i]:
            print("correct!")
            correct_num+=1
        else:
            print("wrong!")
            error_num+=1

    print(correct_num / (correct_num+error_num))


import os
import time
import random
import codecs
import numpy as np
import tensorflow as tf
from tensorflow.keras.utils import to_categorical
import keras
from keras import backend as K
from keras.layers import Input
from sklearn.model_selection import StratifiedKFold
from sklearn.metrics import confusion_matrix
import matplotlib.pyplot as plt
from matplotlib.font_manager import FontProperties

from create_data import get_ns

def set_seed(seed):
    os.environ['PYTHONHASHSEED'] = str(seed)
    os.environ['TF_DETERMINISTIC_OPS'] = '1'
    random.seed(seed)
    np.random.seed(seed)
    tf.random.set_seed(seed)

def get_folds_data(num_people, feat_time, kfold_num, img_rows, img_cols):
    folds_data = {}
    folds_data_person = {}
    for nb in range(num_people):
        one_falx_1 = falx[nb * 3:nb * 3 + 3]
        one_falx_1 = one_falx_1.reshape((-1, feat_time, img_rows, img_cols, 5))

        # ###============= random select ============####
        # permutation = np.random.permutation(one_y_1.shape[0])
        # one_falx_2 = one_falx_1[permutation, :]
        # one_falx = one_falx_2[0:3400]
        # one_y_2 = one_y_1[permutation, :]
        # one_y = one_y_2[0:3400]
        # ###============= random select ============####

        one_y = one_y_1
        one_falx = one_falx_1[:, :, :, :, 1:5]

        # print(one_y.shape)
        # print(one_falx.shape)
        # x_train, x_test, y_train, y_test = train_test_split(one_falx, one_y, test_size=0.25)
        kfold = StratifiedKFold(n_splits=kfold_num, shuffle=True, random_state=seed)
        for fi, (train, test) in enumerate(kfold.split(one_falx, one_y.argmax(1))):
            # x_train = one_falx[train]
            # y_train = one_y[train]
            x_test = one_falx[test]
            y_test = one_y[test]

            if fi in folds_data:
                # folds_data[fi]['x_train'] = np.concatenate([folds_data[fi]['x_train'], x_train], axis=0)
                # folds_data[fi]['y_train'] = np.concatenate([folds_data[fi]['y_train'], y_train], axis=0)
                folds_data[fi]['x_test'] = np.concatenate([folds_data[fi]['x_test'], x_test], axis=0)
                folds_data[fi]['y_test'] = np.concatenate([folds_data[fi]['y_test'], y_test], axis=0)
            else:
                folds_data[fi] = {}
                # folds_data[fi]['x_train'] = x_train
                # folds_data[fi]['y_train'] = y_train
                folds_data[fi]['x_test'] = x_test
                folds_data[fi]['y_test'] = y_test

            # folds_data_person[fi][nb]['x_train'] = x_train
            # folds_data_person[fi][nb]['y_train'] = y_train
            folds_data_person[fi] = {nb: {}}
            folds_data_person[fi][nb]['x_test'] = x_test
            folds_data_person[fi][nb]['y_test'] = y_test

    return folds_data, folds_data_person

def plot_confusion_matrix(output_path, y_true, y_pred, labels=["0", "1", "-1"]):
    cm = confusion_matrix(y_true, y_pred)
    cm_norm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]
    cm_norm = cm_norm * 100
    cm_norm = np.around(cm_norm, decimals=2)  # 保留两位小数

    plt.matshow(cm_norm, cmap=plt.cm.Reds)  # 根据最下面的图按自己需求更改颜色
    # plt.colorbar()

    for i in range(len(cm_norm)):
        for j in range(len(cm_norm)):
            plt.annotate('%s' % cm_norm[j, i], xy=(i, j), horizontalalignment='center', verticalalignment='center')

    # plt.tick_params(labelsize=15) # 设置左边和上面的label类别如0,1,2,3,4的字体大小。

    font_label = FontProperties(fname='./data/SimHei.ttf', size=13)
    plt.ylabel('实际类别', fontproperties=font_label)
    plt.xlabel('预测类别', fontproperties=font_label)

    font_ticks = FontProperties(fname='./data/SimHei.ttf', size=11)
    xlocations = np.array(range(len(labels)))
    plt.xticks(xlocations, labels, fontproperties=font_ticks)
    plt.yticks(xlocations, labels, fontproperties=font_ticks)

    # plt.show()
    plt.savefig('%s/confusion_matrix.png' % output_path)
    plt.close('all')
    print('=================================================================')
    print('=================================================================')
    print('=================================================================')
    print('=================================================================')

if __name__ == '__main__':
    # os.environ['CUDA_VISIBLE_DEVICES'] = '1'

    data_path = './data/model/'
    output_path = './runtime_all/'
    num_people = 1
    num_classes = 3
    kfold_num = 5
    img_rows, img_cols, num_chan = 8, 9, 4
    model_type = 'cnn'  # cnn, densenet, resnet
    feat_time = 1
    se_head = False
    se_tail = False
    seed = 1
    labels_name = ['中性', '积极', '消极']  # 0, 1, -1

    set_seed(seed)

    runtime_path = '%s/t%s/%s' % (output_path, feat_time, model_type)
    if se_head:
        runtime_path += '_se_head'
    if se_tail:
        runtime_path += '_se_tail'

    img_size = (img_rows, img_cols, num_chan)
    falx = np.load('%s/X_89_t%s.npy' % (data_path, feat_time))
    y = np.load('%s/y_89_t%s.npy' % (data_path, feat_time))

    one_y_1 = np.array([y[:get_ns(feat_time)]] * 3).reshape((-1,))
    one_y_1 = to_categorical(one_y_1, num_classes)

    people_acc_list = {}
    label_gold = []
    label_pred = []
    folds_data, folds_data_person = get_folds_data(num_people, feat_time, kfold_num, img_rows, img_cols)
    with codecs.open('%s/test.log' % runtime_path, 'w', 'utf-8') as fout:
        for fi in folds_data:
            start = time.time()
            K.clear_session()

            x_test = folds_data[fi]['x_test']
            y_test = folds_data[fi]['y_test']

            fold_path = '%s/f%s' % (runtime_path, fi)

            input_list = [Input(shape=img_size) for _ in range(feat_time)]
            if os.path.exists('%s/best.h5' % fold_path):
                model = keras.models.load_model('%s/best.h5' % fold_path)
            else:
                model = keras.models.load_model('%s/last.h5' % fold_path)

            # evaluate the model
            scores = model.evaluate(
                [x_test[:, nf] for nf in range(feat_time)],
                y_test, verbose=1
            )

            accuracy = (scores[1] * 100)
            print("%.2f%%" % accuracy)
            fout.write('fold acc： %s\n' % accuracy)

            # 画图
            y_pred = model([x_test[:, nf] for nf in range(feat_time)])
            y_pred = K.eval(tf.argmax(y_pred, axis=1, output_type=tf.int32)).tolist()
            y_true = np.argmax(y_test, axis=1).tolist()
            plot_confusion_matrix(fold_path, y_true, y_pred, labels=labels_name)
            label_gold.extend(y_true)
            label_pred.extend(y_pred)

            people_acc = {}
            for nb in folds_data_person[fi]:
                person_x = folds_data_person[fi][nb]['x_test']
                person_y = folds_data_person[fi][nb]['y_test']
                person_y_pred = model([person_x[:, nf] for nf in range(feat_time)])
                person_y_pred = K.eval(tf.argmax(person_y_pred, axis=1, output_type=tf.int32)).tolist()
                person_y_true = np.argmax(person_y, axis=1).tolist()
                person_acc = np.mean(np.equal(np.array(person_y_pred), np.array(person_y_true)))
                people_acc[nb] = person_acc

                if nb in people_acc_list:
                    people_acc_list[nb].append(person_acc)
                else:
                    people_acc_list[nb] = [person_acc]

            with codecs.open('%s/people_accuracy.log' % fold_path, 'w', 'utf-8') as fout_per:
                for nb in people_acc:
                    fout_per.write('%s: %s\n' % (nb, people_acc[nb]))

            # print("all acc: {}".format(all_acc))
            # print('mean acc: %s, std: %s' % (np.mean(all_acc), np.std(all_acc)))
            # fout.write('mean acc: %s, std: %s\n' % (np.mean(all_acc), np.std(all_acc)))
            # acc_list.append(np.mean(all_acc))
            # std_list.append(np.std(all_acc))
            # print("进度： {}".format(nb))
            # all_acc = []
            end = time.time()
            print("%.2f" % (end - start))  # run time

        plot_confusion_matrix(runtime_path, label_gold, label_pred, labels=labels_name)
        with codecs.open('%s/people_accuracy.log' % runtime_path, 'w', 'utf-8') as fout_peo:
            for nb in people_acc_list:
                fout_peo.write('%s: %s\n' % (nb, people_acc_list[nb]))


from sklearn.metrics import accuracy_score, precision_score, recall_score, confusion_matrix, classification_report
from sklearn.linear_model import LogisticRegression, LinearRegression
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import RandomForestClassifier
from sklearn.neighbors import KNeighborsClassifier
import pandas as pd

######################### Train Logistic Regression #################################

def logistic_regression(X_train, y_train):
    '''
    This function takes in X_train (features using for model) and y_train (target 'win') and performs logistic
    regression giving us accuracy of the model and the classification report
    '''
    # Calling out funtion
    logit = LogisticRegression()

    # Fit the training data set
    logit = logit.fit(X_train, y_train)

    # Make predictions
    y_pred = logit.predict(X_train)

    #Accuracy of model
    score = logit.score(X_train, y_train)

    print(f'The logistic regression models accuracy is {round(score * 100,2)}%\n')
    print(f'----------------------')
    print(f'Confusion Matrix\n\n {pd.crosstab(y_train, y_pred)}\n')
    print(f'----------------------')

    # Coefficients for each feature
    coef_df = pd.DataFrame(logit.coef_)

    # List comprehension for columns in X_train
    names = [column for column in X_train.columns]

    # Renaming columns to their names for coef_df
    coef_df.columns = names

    print(f'Classification Report\n {classification_report(y_train, y_pred)}')
    return coef_df, logit

    ######################## Decision Tree ############################
def decision_tree(X_train, y_train, k):
    '''
    This function requires X_train, y_train and k (max_depth). A confusion matrix, models accuracy and
    classification report are outputed
    '''
    # Creating the decision tree object
    clf = DecisionTreeClassifier(max_depth=k, random_state=123)

    # Fitting the data to the trained data
    clf = clf.fit(X_train, y_train)

    # Array of the win/loss
    y_pred = clf.predict(X_train)

    # Estimate the probaility of win
    y_pred_proba = clf.predict_proba(X_train)

    print('The decision tree classifier accuracy : {:.2f}\n'
        .format(clf.score(X_train, y_train)))
    print(f'----------------------')
    # Confusion matrix
    print(f'Confusion Matrix: \n\n {pd.crosstab(y_train, y_pred)}\n' )
    print(f'----------------------')
    print("Decision Tree Model Classification Report:\n", classification_report(y_train, y_pred))

    return clf

    ####################### Random Forest ###################################
def random_forest(X_train, y_train, k):
    # Random forest object
    rf = RandomForestClassifier(n_estimators=100, max_depth=k, random_state=123)

    # Fitting the model
    rf = rf.fit(X_train, y_train)

    # Make predictions
    y_pred = rf.predict(X_train)

    # Estimate the probability of a passenger surviving, using the training data
    y_pred_proba = rf.predict_proba(X_train)

    # Accuracy
    rf.score(X_train, y_train)

    print('The random forest accuracy : {:.2f}\n'
             .format(rf.score(X_train, y_train)))
    print(f'----------------------')
    # Confusion matrix
    print(f'Confusion Matrix: \n\n {pd.crosstab(y_train, y_pred)}\n' )
    print(f'----------------------')
    print("Random Forest Model Classification Report:\n", classification_report(y_train, y_pred))

    return rf

    ########################## KNN ###################################
def knn(X_train, y_train, k):
    # KNN object
    knn = KNeighborsClassifier(n_neighbors=k, weights='uniform')

    # Fit the model
    knn = knn.fit(X_train, y_train)

    # Make predictions
    y_pred = knn.predict(X_train)

    # Estimate the probability
    y_pred_proba = knn.predict_proba(X_train)

    print('The k-neareast neighbor accuracy : {:.2f}\n'
                 .format(knn.score(X_train, y_train)))
    print(f'----------------------')
    # Confusion matrix
    print(f'Confusion Matrix: \n\n {pd.crosstab(y_train, y_pred)}\n' )
    print(f'----------------------')
    print("K-Nearest Neighbor Classification Report:\n", classification_report(y_train, y_pred))

    return knn


# Faça um Programa que pergunte quanto você ganha por hora e o número de horas trabalhadas no mês.
# Calcule e mostre o total do seu salário no referido mês.

salario_hora = int(input("Quanto vc ganha por hora: "))
horas_mes = int(input("Quantas horas trabalhou no mês: "))

salario_total = salario_hora*horas_mes

print("Seu salário: R$", salario_total)

input("Pressione Enter para continuar")

Lista de Exercicios - Wiki_python/Estrutura Sequencial/08.py


import os
from dotenv import load_dotenv
from aiogram import Bot, Dispatcher
from aiogram.enums import ParseMode
from aiogram.fsm.storage.memory import MemoryStorage

def get_apikey():
    env_path = os.path.join("TOKEN.env")
    load_dotenv(env_path)
    API_TOKEN = os.getenv("API_TOKEN")
    return str(API_TOKEN)

def get_admin():
    env_path = os.path.join("TOKEN.env")
    load_dotenv(env_path)
    ADMIN_TOKEN = os.getenv("ADMIN_TOKEN")
    return int(ADMIN_TOKEN)

bot = Bot(get_apikey(), parse_mode=ParseMode.HTML)
dp = Dispatcher(storage=MemoryStorage())


import configparser
from datetime import datetime
import os
import sys
from pyspark.sql import SparkSession
from pyspark.sql.functions import udf, col, monotonically_increasing_id
from pyspark.sql.functions import year, month, dayofmonth, hour, weekofyear, date_format
from pyspark.sql.types import TimestampType, IntegerType
import pandas as pd

config = configparser.ConfigParser()
config.read('dl.cfg')

os.environ['AWS_ACCESS_KEY_ID']=config['AWS']['AWS_ACCESS_KEY_ID']
os.environ['AWS_SECRET_ACCESS_KEY']=config['AWS']['AWS_SECRET_ACCESS_KEY']

def create_spark_session():
    """ This function is created spark session """
    spark = SparkSession \
        .builder \
        .config("spark.jars.packages", "org.apache.hadoop:hadoop-aws:2.7.0") \
        .getOrCreate()
    return spark

def process_song_data(spark, input_data, output_data):
    """ This function is help to extract song and artists json files from S3 and
        save them in a dataframe"""
    # get filepath to song data file
    song_data = os.path.join(input_data, "song_data/*/*/*/*.json")

    # read song data file
    df = spark.read.json(song_data)

    # extract columns to create songs table
    songs_table = df.select(col('song_id').alias('song_id'),
                            col('title').alias('title'),
                            col('artist_id').alias('artist_id'),
                            col('year').alias('year'),
                            col('duration').alias('duration')).dropDuplicates(subset = ['song_id'])
    print ('songs_table:',songs_table.count(),'rows')
    songs_table.printSchema()
    songs_table.show(1)

    # write songs table to parquet files partitioned by year and artist
    songs_table.write.partitionBy('year', 'artist_id').parquet('{}songs/'.format(output_data),'overwrite')

    # extract columns to create artists table
    artists_table = df.select(['artist_id', 'artist_name','artist_location','artist_latitude',
                               'artist_longitude']).dropDuplicates(subset = ['artist_id'])
    artists_table.printSchema()
    artists_table.show(1)

    # write artists table to parquet files
    artists_table.write.parquet('{}artists/'.format(output_data),'overwrite')

def process_log_data(spark, input_data, output_data):
    """ This function set the log data Json files from S3 and extract user and time data.
        And join log and song files."""

    # get filepath to log data file
    #log_data =os.path.join(input_data, "log_data/*/*/*.json")
    log_data =os.path.join(input_data, "log_data/*.json")

    # read log data file
    df = spark.read.json(log_data)

    # filter by actions for song plays
    df = df.filter(df['page'] == 'NextSong')

    # extract columns for users table
    users_table =  df.select(['userID', 'firstName', 'LastName', 'gender', 'level']).dropDuplicates(subset = ['userID'])
    users_table.printSchema()
    users_table.show(1)

    # write users table to parquet files
    users_table.write.parquet('{}users/'.format(output_data),'overwrite')

    # create timestamp column from original timestamp column
    #
    get_timestamp = udf(lambda x : datetime.fromtimestamp(x/1000).strftime('%Y-%m-%d %H:%M:%S'))
    df = df.withColumn('start_time',get_timestamp(df['ts']))

    # extract columns to create time table
    time_table = df.select(col('start_time').alias('start_time'),
                           hour('start_time').alias('hour'),
                           dayofmonth('start_time').alias('day'),
                           weekofyear('start_time').alias('week'),
                           month('start_time').alias('month'),
                           year('start_time').alias('year'),
                           date_format('start_time','EEEE').alias('weekday')).dropDuplicates()

    # write time table to parquet files partitioned by year and month
    time_table.write.partitionBy(['year','month']).parquet('{}time/'.format(output_data),'overwrite')

    time_table.printSchema()
    time_table.show(1)

    # read in song data to use for songplays table
    #song_df = spark.read.parquet('{}songs/*/*/*/*'.format(output_data))
    song_df = spark.read.parquet('{}songs/*/*/*'.format(output_data))
    artists_t = spark.read.parquet('{}artists/*'.format(output_data))

    df.createOrReplaceTempView("logs")
    song_df.createOrReplaceTempView("song")
    artists_t.createOrReplaceTempView("artist")
    time_table.createOrReplaceTempView('time')

    spark.sql("""
        SELECT * FROM song
    """).show(500)

    # extract columns from joined song and log datasets to create songplays table
    songplays_table = spark.sql(""" SELECT logs.ts as start_time, time.year, time.month,
                                        logs.userId, logs.level,
                                        song.song_id, artist.artist_id, logs.sessionId,
                                        artist.artist_location, logs.userAgent
                                    FROM logs
                                        JOIN song on
                                            logs.song = song.title AND
                                            logs.length=song.duration
                                        JOIN time on
                                            logs.start_time = time.start_time
                                        JOIN artist on
                                            logs.artist = artist.artist_name
                                         """)

    # set songplay_id as serial
    songplays_table=songplays_table.withColumn('songplay_id',monotonically_increasing_id())
    print(songplays_table.limit(100).toPandas())

    # write songplays table to parquet files partitioned by year and month
    songplays_table.write.partitionBy('year', 'month').parquet('{}songplays/'.format(output_data),'overwrite')

def main():
    """ Main function calls spark session, input data and output data"""
    spark = create_spark_session()
    input_data = "data"
    output_data = ""

    process_song_data(spark, input_data, output_data)
    process_log_data(spark, input_data, output_data)

if __name__ == "__main__":
    main()