"""Export and plot results as networkx objects."""
import itertools as it
import networkx as nx
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from .multivariate_te import Multivariate_te

VERBOSE = False

def generate_network_graph(res):
    """Generate graph object for the full network.

    Generate a graph object from the network of (multivariate)
    interactions (e.g., multivariate TE), using the networkx
    class for directed graphs (DiGraph).

    Args:
        res : dict
            output of multivariate_te.analyse_network()

    Returns:
        instance of a directed graph class from the networkx
        package (DiGraph)
    """
    graph = nx.DiGraph()
    # add all targets as vertices
    graph.add_nodes_from(list(res.keys()))
    # Add edges as significant sources.
    for n in res.keys():
        s = np.array(res[n]['selected_vars_sources'],
                     dtype=[('sources', np.int), ('lags', np.int)])
        s = np.unique(s['sources'])
        if s.size > 0:
            edges = [x for x in it.product(s, [n])]
            graph.add_nodes_from(s)
            graph.add_edges_from(edges)
            if VERBOSE:
                print(edges)
                print(n)
                print(s)
    return graph

def generate_source_graph(res, sign_sources=True):
    """Generate graph object for a target process and single variables.

    Generate a graph object from the network of (multivariate)
    interactions (e.g., multivariate TE) between single samples and
    a target sample (current value), using the networkx class for
    directed graphs (DiGraph).

    Args:
        res : dict
            output of multivariate_te.analyse_single_target()
        sign_sources : bool
            add only sources significant information contribution
            (default=True)

    Returns:
        instance of a directed graph class from the networkx
        package (DiGraph)
    """
    try:
        target = (res['current_value'][0], 0)
    except KeyError:
        KeyError('Input res should be result of analyse_single_target() '
                 'method.')
    graph = nx.DiGraph()
    graph.add_node(target)
    # Add either all tested candidates or only significant ones
    # to the graph.
    if sign_sources:
        graph.add_nodes_from(res['selected_vars_full'][:])
    else:
        procs = res['sources_tested']
        samples = np.arange(res['current_value'][1] - res['min_lag_sources'],
                            res['current_value'][1] - res['max_lag_sources'],
                            -res['tau_sources'])
        define_candidates = Multivariate_te._define_candidates
        nodes = define_candidates(_, procs, samples)
        graph.add_nodes_from(nodes)

    for v in range(len(res['selected_vars_full'])):
        graph.add_edge(res['selected_vars_full'][v], target)  # here, I could also add additional info
    if VERBOSE:
        print(graph.node)
        print(graph.edge)
        graph.number_of_edges()
    return graph

def plot_network(res):
    """Plot network of multivariate TE between processes.

    Plot graph of the network of (multivariate) interactions between
    processes (e.g., multivariate TE). The function uses  the
    networkx class for directed graphs (DiGraph) internally.
    Plots a network and adjacency matrix.

    Args:
        res : dict
            output of multivariate_te.analyse_network()

    Returns:
        instance of a directed graph class from the networkx
        package (DiGraph)
    """
    try:
        res = res['fdr']
    except KeyError:
        print('plotting non-corrected network!')

    graph = generate_network_graph(res)
    print(graph.node)
    f, (ax1, ax2) = plt.subplots(1, 2)
    adj_matrix = nx.to_numpy_matrix(graph)
    cmap = sns.light_palette('cadetblue', n_colors=2, as_cmap=True)
    sns.heatmap(adj_matrix, cmap=cmap, cbar=False, ax=ax1,
                square=True, linewidths=1, xticklabels=graph.nodes(),
                yticklabels=graph.nodes())
    ax1.xaxis.tick_top()
    plt.setp(ax1.yaxis.get_majorticklabels(), rotation=0)
    nx.draw_circular(graph, with_labels=True, node_size=300, alpha=1.0, ax=ax2,
                     node_color='cadetblue', hold=True, font_weight='bold')
    plt.show()
    return graph

def plot_selected_vars(res, sign_sources=True):
    """Plot network of a target process and single variables.

    Plot graph of the network of (multivariate) interactions between
    source variables and the current value. The function uses the
    networkx class for directed graphs (DiGraph) internally.
    Plots a network and reduced adjacency matrix.

    Args:
        res : dict
            output of multivariate_te.analyse_single_target()
        sign_sources : bool
            add only sources significant information contribution
            (default=True)

    Returns:
        instance of a directed graph class from the networkx
        package (DiGraph)
    """
    graph = generate_source_graph(res, sign_sources)
    n = np.array(graph.nodes(),
                 dtype=[('procs', np.int), ('lags', np.int)])
    target = tuple(n[n['lags'] == 0][0])
    max_lag = max(res['max_lag_sources'], res['max_lag_target'])
    ind = 0
    color = ['lavender' for c in range(graph.number_of_nodes())]
    pos = nx.spring_layout(graph)
    for n in graph.node:
        if n == target:  # current value
            pos[n] = np.array([max_lag, 0])
        elif n[0] == target[0]:  # target history
            pos[n] = np.array([max_lag - n[1], 0])
        elif n[0] < target[0]:  # sources with proc. number < target
            pos[n] = np.array([max_lag - n[1], n[0] + 1])
        else:  # sources with proc. number > target
            pos[n] = np.array([max_lag - n[1], n[0]])

        if n in res['selected_vars_sources']:
            color[ind] = 'cadetblue'
        elif n in res['selected_vars_target']:
            color[ind] = 'tomato'
        elif n == target:
            color[ind] = 'red'
        ind += 1

    if VERBOSE:
        print(graph.node)
        print(color)
    plt.figure()
    nx.draw(graph, pos=pos, with_labels=True, font_weight='bold',
            node_size=900, alpha=0.7, node_shape='s', node_color=color,
            hold=True)
    plt.plot([-0.5, max_lag + 0.5], [0.5, 0.5],
             linestyle='--', linewidth=1, color='0.5')
    plt.show()
    return graph

def plot_mute_graph():
    """Plot MuTE example network.

    Network of 5 AR-processes, which is used as an example the paper
    on the MuTE toolbox (Montalto, PLOS ONE, 2014, eq. 14). The
    network consists of five autoregressive (AR) processes with model
    orders 2 and les and the following (non-linear) couplings:
        0 -> 1, u = 2
        0 -> 2, u = 3
        0 -> 3, u = 2 (non-linear)
        3 -> 4, u = 1
        4 -> 3, u = 1
    """
    graph = nx.DiGraph()
    graph.add_nodes_from(np.arange(5))
    # graph.add_edges_from([(0, 1), (0, 2), (0, 3), (3, 4), (4, 3)])
    graph.add_weighted_edges_from([(0, 1, 2), (0, 2, 3), (0, 3, 2), (3, 4, 1),
                                   (4, 3, 1)], weight='delay')
    pos = {
        0: np.array([1, 1]),
        1: np.array([0, 2]),
        2: np.array([0, 0]),
        3: np.array([2, 1]),
        4: np.array([3, 1]),
    }
    plt.figure()
    nx.draw(graph, pos=pos, with_labels=True, node_size=900, alpha=1.0,
            node_color='cadetblue', font_weight='bold',
            edge_color=['k', 'k', 'r', 'k', 'k'], hold=True)
    nx.draw_networkx_edge_labels(graph, pos=pos)
    plt.text(2, 0.1, 'non-linear interaction in red')
    plt.show()
    # see here for an example on how to plot edge labels:
    # http://stackoverflow.com/questions/10104700/how-to-set-networkx-edge-labels-offset-to-avoid-label-overlap


import requests
from bs4 import BeautifulSoup

URL = "https://www.instagram.com/{}/"

def scrape(username):
    full_url = URL.format(username)  # make full URL
    r = requests.get(full_url)  # send GET requests
    s = BeautifulSoup(r.text, "lxml")  # make soup object

    tag = s.find("meta", attrs={"name": "description"})
    text = tag.attrs['content']  # grab the content text
    main_text = text.split("-")[0]  # required text

    return main_text

USERNAME = "3richkey"
data = scrape(USERNAME)
print(data)


import os
from PIL import Image
import numpy as np
from tqdm import tqdm

class FaceDataset:
    '''
    采用批量加载数据的方法实现图片数据的加载
    '''
    def __init__(self, path):
        self.path = path
        self._path_list = self.__get_path()
        self._i = 0
        self._n = len(self._path_list)

    def __iter__(self):
        return self

    def __next__(self):
        if self._i < self._n:
            ndarray = self.__converter(self._path_list[self._i])
            self._i += 1
            return ndarray
        else:
            raise StopIteration(f"迭代完毕，共{self._n}次")

    def __get_path(self):
        '''
        接收图片路径列表
        '''
        iter = os.walk(self.path)
        path_list = []
        while True:
            try:
                ls = next(iter)
                if len(ls[1]) != 0:
                    continue
                for p in ls[2]:
                    abs_path = os.path.join(ls[0], p)
                    path_list.append(abs_path)
            except StopIteration:
                print("全部路径获取成功！")
                break
        return path_list

    def __converter(self, image):
        '''
        将一张图片数据以ndarray的形式返回
        '''
        im = Image.open(image)
        return np.array(im)

if __name__ == "__main__":
    fd = FaceDataset("D:\\vscode py\\现代程设\\week 9\\originalPics")
    with tqdm(total=fd._n) as bar:
        for x in fd:
            bar.update(1)
            print(x)
            break


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

import string
import warnings

import numpy as np
import matplotlib.pyplot as mpl
from matplotlib.font_manager import FontProperties

from . import wcs_util
from . import angle_util as au
from .decorators import auto_refresh, fixdocstring

class TickLabels(object):

    def __init__(self, parent):

        # Store references to axes
        self._ax1 = parent._ax1
        self._ax2 = parent._ax2
        self._wcs = parent._wcs
        self._figure = parent._figure

        # Save plotting parameters (required for @auto_refresh)
        self._parameters = parent._parameters

        # Set font
        self._label_fontproperties = FontProperties()

        self.set_style('plain')

        system, equinox, units = wcs_util.system(self._wcs)

        # Set default label format
        if self._wcs.xaxis_coord_type in ['longitude', 'latitude']:
            if system['name'] == 'equatorial':
                if self._wcs.xaxis_coord_type == 'longitude':
                    self.set_xformat("hh:mm:ss.ss")
                else:
                    self.set_xformat("dd:mm:ss.s")
            else:
                self.set_xformat("ddd.dddd")
        else:
            self.set_xformat('%g')

        if self._wcs.yaxis_coord_type in ['longitude', 'latitude']:
            if system['name'] == 'equatorial':
                if self._wcs.yaxis_coord_type == 'longitude':
                    self.set_yformat("hh:mm:ss.ss")
                else:
                    self.set_yformat("dd:mm:ss.s")
            else:
                self.set_yformat("ddd.dddd")
        else:
            self.set_yformat('%g')

        # Set major tick formatters
        fx1 = WCSFormatter(wcs=self._wcs, coord='x')
        fy1 = WCSFormatter(wcs=self._wcs, coord='y')
        self._ax1.xaxis.set_major_formatter(fx1)
        self._ax1.yaxis.set_major_formatter(fy1)

        fx2 = mpl.NullFormatter()
        fy2 = mpl.NullFormatter()
        self._ax2.xaxis.set_major_formatter(fx2)
        self._ax2.yaxis.set_major_formatter(fy2)

        # Cursor display
        self._ax1._cursor_world = True
        self._figure.canvas.mpl_connect('key_press_event', self._set_cursor_prefs)

    @auto_refresh
    def set_xformat(self, format):
        '''
        Set the format of the x-axis tick labels. If the x-axis type is
        ``longitude`` or ``latitude``, then the options are:

            * ``ddd.ddddd`` - decimal degrees, where the number of decimal places can be varied
            * ``hh`` or ``dd`` - hours (or degrees)
            * ``hh:mm`` or ``dd:mm`` - hours and minutes (or degrees and arcminutes)
            * ``hh:mm:ss`` or ``dd:mm:ss`` - hours, minutes, and seconds (or degrees, arcminutes, and arcseconds)
            * ``hh:mm:ss.ss`` or ``dd:mm:ss.ss`` - hours, minutes, and seconds (or degrees, arcminutes, and arcseconds), where the number of decimal places can be varied.

        If the x-axis type is ``scalar``, then the format should be a valid
        python string format beginning with a ``%``.

        If one of these arguments is not specified, the format for that axis
        is left unchanged.
        '''
        if self._wcs.xaxis_coord_type in ['longitude', 'latitude']:
            if format.startswith('%'):
                raise Exception("Cannot specify Python format for longitude or latitude")
            try:
                if not self._ax1.xaxis.apl_auto_tick_spacing:
                    au._check_format_spacing_consistency(format, self._ax1.xaxis.apl_tick_spacing)
            except au.InconsistentSpacing:
                warnings.warn("WARNING: Requested label format is not accurate enough to display ticks. The label format will not be changed.")
                return
        else:
            if not format.startswith('%'):
                raise Exception("For scalar tick labels, format should be a Python format beginning with %")

        self._ax1.xaxis.apl_label_form = format
        self._ax2.xaxis.apl_label_form = format

    @auto_refresh
    def set_yformat(self, format):
        '''
        Set the format of the y-axis tick labels. If the y-axis type is
        ``longitude`` or ``latitude``, then the options are:

            * ``ddd.ddddd`` - decimal degrees, where the number of decimal places can be varied
            * ``hh`` or ``dd`` - hours (or degrees)
            * ``hh:mm`` or ``dd:mm`` - hours and minutes (or degrees and arcminutes)
            * ``hh:mm:ss`` or ``dd:mm:ss`` - hours, minutes, and seconds (or degrees, arcminutes, and arcseconds)
            * ``hh:mm:ss.ss`` or ``dd:mm:ss.ss`` - hours, minutes, and seconds (or degrees, arcminutes, and arcseconds), where the number of decimal places can be varied.

        If the y-axis type is ``scalar``, then the format should be a valid
        python string format beginning with a ``%``.

        If one of these arguments is not specified, the format for that axis
        is left unchanged.
        '''
        if self._wcs.yaxis_coord_type in ['longitude', 'latitude']:
            if format.startswith('%'):
                raise Exception("Cannot specify Python format for longitude or latitude")
            try:
                if not self._ax1.yaxis.apl_auto_tick_spacing:
                    au._check_format_spacing_consistency(format, self._ax1.yaxis.apl_tick_spacing)
            except au.InconsistentSpacing:
                warnings.warn("WARNING: Requested label format is not accurate enough to display ticks. The label format will not be changed.")
                return
        else:
            if not format.startswith('%'):
                raise Exception("For scalar tick labels, format should be a Python format beginning with %")

        self._ax1.yaxis.apl_label_form = format
        self._ax2.yaxis.apl_label_form = format

    @auto_refresh
    def set_style(self, style):
        """
        Set the format of the x-axis tick labels. This can be 'colons' or 'plain':

            * 'colons' uses colons as separators, for example 31:41:59.26 +27:18:28.1
            * 'plain' uses letters and symbols as separators, for example 31h41m59.26s +27º18'28.1"
        """

        if style == 'latex':
            warnings.warn("latex has now been merged with plain - whether or not to use LaTeX is controled through set_system_latex")
            style = 'plain'

        if not style in ['colons', 'plain']:
            raise Exception("Label style should be one of colons/plain")

        self._ax1.xaxis.apl_labels_style = style
        self._ax1.yaxis.apl_labels_style = style
        self._ax2.xaxis.apl_labels_style = style
        self._ax2.yaxis.apl_labels_style = style

    @auto_refresh
    @fixdocstring
    def set_font(self, family=None, style=None, variant=None, stretch=None, weight=None, size=None, fontproperties=None):
        """
        Set the font of the tick labels

        Optional Keyword Arguments:

        common: family, style, variant, stretch, weight, size, fontproperties

        Default values are set by matplotlib or previously set values if
        set_font has already been called. Global default values can be set by
        editing the matplotlibrc file.
        """

        if family:
            self._label_fontproperties.set_family(family)

        if style:
            self._label_fontproperties.set_style(style)

        if variant:
            self._label_fontproperties.set_variant(variant)

        if stretch:
            self._label_fontproperties.set_stretch(stretch)

        if weight:
            self._label_fontproperties.set_weight(weight)

        if size:
            self._label_fontproperties.set_size(size)

        if fontproperties:
            self._label_fontproperties = fontproperties

        for tick in self._ax1.get_xticklabels():
            tick.set_fontproperties(self._label_fontproperties)
        for tick in self._ax1.get_yticklabels():
            tick.set_fontproperties(self._label_fontproperties)
        for tick in self._ax2.get_xticklabels():
            tick.set_fontproperties(self._label_fontproperties)
        for tick in self._ax2.get_yticklabels():
            tick.set_fontproperties(self._label_fontproperties)

    @auto_refresh
    def show(self):
        """
        Show the x- and y-axis tick labels
        """
        self.show_x()
        self.show_y()

    @auto_refresh
    def hide(self):
        """
        Hide the x- and y-axis tick labels
        """
        self.hide_x()
        self.hide_y()

    @auto_refresh
    def show_x(self):
        """
        Show the x-axis tick labels
        """

        for tick in self._ax1.get_xticklabels():
            tick.set_visible(True)
        for tick in self._ax2.get_xticklabels():
            tick.set_visible(True)

    @auto_refresh
    def hide_x(self):
        """
        Hide the x-axis tick labels
        """

        for tick in self._ax1.get_xticklabels():
            tick.set_visible(False)
        for tick in self._ax2.get_xticklabels():
            tick.set_visible(False)

    @auto_refresh
    def show_y(self):
        """
        Show the y-axis tick labels
        """

        for tick in self._ax1.get_yticklabels():
            tick.set_visible(True)
        for tick in self._ax2.get_yticklabels():
            tick.set_visible(True)

    @auto_refresh
    def hide_y(self):
        """
        Hide the y-axis tick labels
        """

        for tick in self._ax1.get_yticklabels():
            tick.set_visible(False)
        for tick in self._ax2.get_yticklabels():
            tick.set_visible(False)

    @auto_refresh
    def set_xposition(self, position):
        "Set the position of the x-axis tick labels ('top' or 'bottom')"
        if position == 'bottom':
            fx1 = WCSFormatter(wcs=self._wcs, coord='x')
            self._ax1.xaxis.set_major_formatter(fx1)
            fx2 = mpl.NullFormatter()
            self._ax2.xaxis.set_major_formatter(fx2)
        elif position == 'top':
            fx1 = mpl.NullFormatter()
            self._ax1.xaxis.set_major_formatter(fx1)
            fx2 = WCSFormatter(wcs=self._wcs, coord='x')
            self._ax2.xaxis.set_major_formatter(fx2)
        else:
            raise ValueError("position should be one of 'top' or 'bottom'")

    @auto_refresh
    def set_yposition(self, position):
        "Set the position of the y-axis tick labels ('left' or 'right')"
        if position == 'left':
            fy1 = WCSFormatter(wcs=self._wcs, coord='y')
            self._ax1.yaxis.set_major_formatter(fy1)
            fy2 = mpl.NullFormatter()
            self._ax2.yaxis.set_major_formatter(fy2)
        elif position == 'right':
            fy1 = mpl.NullFormatter()
            self._ax1.yaxis.set_major_formatter(fy1)
            fy2 = WCSFormatter(wcs=self._wcs, coord='y')
            self._ax2.yaxis.set_major_formatter(fy2)
        else:
            raise ValueError("position should be one of 'left' or 'right'")

    def _set_cursor_prefs(self, event, **kwargs):
        if event.key == 'c':
            self._ax1._cursor_world = not self._ax1._cursor_world

    def _cursor_position(self, x, y):

        xaxis = self._ax1.xaxis
        yaxis = self._ax1.yaxis

        if self._ax1._cursor_world:

            xw, yw = wcs_util.pix2world(self._wcs, x, y)

            if self._wcs.xaxis_coord_type in ['longitude', 'latitude']:

                xw = au.Angle(degrees=xw, latitude=self._wcs.xaxis_coord_type == 'latitude')

                hours = 'h' in xaxis.apl_label_form

                if hours:
                    xw = xw.tohours()

                if xaxis.apl_labels_style in ['plain', 'latex']:
                    sep = ('d', 'm', 's')
                    if hours:
                        sep = ('h', 'm', 's')
                elif xaxis.apl_labels_style == 'colons':
                    sep = (':', ':', '')

                xlabel = xw.tostringlist(format=xaxis.apl_label_form, sep=sep)
                xlabel = string.join(xlabel, "")

            else:

                xlabel = xaxis.apl_label_form % xw

            if self._wcs.yaxis_coord_type in ['longitude', 'latitude']:

                yw = au.Angle(degrees=yw, latitude=self._wcs.yaxis_coord_type == 'latitude')

                hours = 'h' in yaxis.apl_label_form

                if hours:
                    yw = yw.tohours()

                if yaxis.apl_labels_style in ['plain', 'latex']:
                    sep = ('d', 'm', 's')
                    if hours:
                        sep = ('h', 'm', 's')
                elif yaxis.apl_labels_style == 'colons':
                    sep = (':', ':', '')

                ylabel = yw.tostringlist(format=yaxis.apl_label_form, sep=sep)
                ylabel = string.join(ylabel, "")

            else:

                ylabel = yaxis.apl_label_form % yw

            return  "%s %s (world)" % (xlabel, ylabel)

        else:

            return "%g %g (pixel)" % (x, y)

class WCSFormatter(mpl.Formatter):

    def __init__(self, wcs=False, coord='x'):
        self._wcs = wcs
        self.coord = coord

    def __call__(self, x, pos=None):
        'Return the format for tick val x at position pos; pos=None indicated unspecified'

        self.coord_type = self._wcs.xaxis_coord_type if self.coord == 'x' else self._wcs.yaxis_coord_type

        if self.coord_type in ['longitude', 'latitude']:

            au._check_format_spacing_consistency(self.axis.apl_label_form, self.axis.apl_tick_spacing)

            hours = 'h' in self.axis.apl_label_form

            if self.axis.apl_labels_style == 'plain':
                if mpl.rcParams['text.usetex']:
                    label_style = 'plain_tex'
                else:
                    label_style = 'plain_notex'
            else:
                label_style = self.axis.apl_labels_style

            if label_style == 'plain_notex':
                sep = (u'\u00b0', u"'", u'"')
                if hours:
                    sep = ('h', 'm', 's')
            elif label_style == 'colons':
                sep = (':', ':', '')
            elif label_style == 'plain_tex':
                if hours:
                    sep = ('^{h}', '^{m}', '^{s}')
                else:
                    sep = ('^{\circ}', '^{\prime}', '^{\prime\prime}')

            ipos = np.argmin(np.abs(self.axis.apl_tick_positions_pix - x))

            label = self.axis.apl_tick_spacing * self.axis.apl_tick_positions_world[ipos]
            if hours:
                label = label.tohours()
            label = label.tostringlist(format=self.axis.apl_label_form, sep=sep)

            if self.coord == x or self.axis.apl_tick_positions_world[ipos] > 0:
                comp_ipos = ipos - 1
            else:
                comp_ipos = ipos + 1

            if comp_ipos >= 0 and comp_ipos <= len(self.axis.apl_tick_positions_pix) - 1:

                comp_label = self.axis.apl_tick_spacing * self.axis.apl_tick_positions_world[comp_ipos]
                if hours:
                    comp_label = comp_label.tohours()
                comp_label = comp_label.tostringlist(format=self.axis.apl_label_form, sep=sep)

                for iter in range(len(label)):
                    if comp_label[0] == label[0]:
                        label.pop(0)
                        comp_label.pop(0)
                    else:
                        break

        else:

            ipos = np.argmin(np.abs(self.axis.apl_tick_positions_pix - x))
            label = self.axis.apl_tick_spacing * self.axis.apl_tick_positions_world[ipos]
            label = self.axis.apl_label_form % label

        if mpl.rcParams['text.usetex']:
            return "$" + string.join(label, "") + "$"
        else:
            return string.join(label, "")


import boto3

dynamodb = boto3.resource(
    'dynamodb',
    endpoint_url='http://localhost:18000',
    region_name='us-east-2',
    aws_access_key_id='ACCESS_ID',
    aws_secret_access_key='ACCESS_KEY'
)

table = dynamodb.Table('scores')

with table.batch_writer() as batch:
    batch.put_item(Item={"question_id": "q1",  "answer": "3", "score": 10})
    batch.put_item(Item={"question_id": "q2",  "answer": "1", "score": 20})
    batch.put_item(Item={"question_id": "q3",  "answer": "2", "score": 20})


import numpy as np

numbers1 = np.random.randint(10,100,6)
numbers2 = np.random.randint(10,100,6)

print(numbers1,numbers2)

result = numbers1 + numbers2#İki dizi arasındaki indisi aynı olan elemanları toplar
result = numbers1 + 10#Dizinin tüm indislerine 10 ekler

result = np.sin(numbers1)#Dizi içerisindeki tüm elemanların sinüsünü alır
result = np.sqrt(numbers1)#Dizideki tüm elemanların karakökünü alır
result = np.log(numbers1)

multi_numbers1 = numbers1.reshape(2,3)
multi_numbers2 = numbers2.reshape(2,3)
print(multi_numbers1)
print(multi_numbers2)

result = np.vstack((multi_numbers1,multi_numbers2))#2 Diziyi dikey bir şekilde birleştirir
result2 = np.hstack((multi_numbers1,multi_numbers2))#2 Diziyi yatay bir şekilde birleştirir

print(result)
print(result2)
"""
---> Vertical
[[54 35 90]     --->Denklem sayısı artar
 [52 18 92]
 [20 85 75]
 [58 59 86]]
---> Horizonal
[[54 35 90 20 85 75] --->Bilinmeyen sayısı artar
 [52 18 92 58 59 86]]
"""

test = np.arange(10,100,6)

yazdir = numbers1 >= 5#Sayıları kontrol eder 5'e göre bool türünde değer döndürür ---> Bu işlem % // gibi işlemler ile de yapılabilir
print(yazdir[1])
print(yazdir)


import serial
import time
import requests

try:
    arduino=serial.Serial('COM3', 9600)
except:
    arduino = serial.Serial('/dev/ttyACM0', 9600)
time.sleep(2)
while 1:
    msg = arduino.readline()
    #print(msg.decode('utf-8'))
    string = msg.decode('utf-8')
    extract = string.split("\n")[0].strip()
    print(extract)
    data = {'rfid_id': extract}
    response = requests.put('http://127.0.0.1:5000/entry_exit', json=data)
    if response.status_code == 200:
        print(response.json())


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

'''
Script to load raw LISST profiling data from CSV files to NetCDF files
 - Set for randomly shaped model files

p_lisst_to_nc.py

### CODE AUTHOR ###
 * William Edge

### CODE UPDATE ###
 * 20180717: Creation of code
 * 20180821: QAQC from manual added
 * 20190520: Multiple fieldtrip handling added
'''

### IMPORT PACKAGES ###
import os
import glob
import pandas as pd
import numpy as np
import sys
sys.path.insert(0, os.path.abspath(os.curdir)[:-9])
from fielddata import extra
from fielddata import lisst as ls

##fieldtrip = 'RS2019'
##sn = '2031'
##bg = 'D4'
fieldtrip = 'KISSME2017'
sn = '2009'
bg = 'DI2'

### CODE INPUT ###
##def main():
if 1==1:

    # Specify work locations
    workDir = os.path.abspath(os.curdir)[:-12]

    # Define work directories and find LISST data
    rawLoc = os.path.join(workDir,'data','field',fieldtrip,'raw_data','profiling',\
                          'calibration_data','LISST')

    datatype = 'Profiling'

    # Source and save folders
    rawDir = os.path.join(rawLoc, sn, bg)
    saveDir = rawDir.replace('raw_data','processed_data')
    # Check is save dir exists and make if not
    if not os.path.isdir(saveDir):
        os.makedirs(saveDir)

    # Particle shape model
    model = 'rs'

    if model=='rs':
        # Find all Randomly shaped CSV files
        rsFileSearch = os.path.join(rawDir, '*rs.csv')
        csv_files = glob.glob(rsFileSearch)

    fields, units, dtype, bins_lower, bins_median = ls.load_LISST_info(workDir)

    # Read CTD logbook
    logFileName = fieldtrip + '_CTD_Logbook.xlsx'
    logbook = os.path.join(workDir,'fieldwork',fieldtrip,logFileName)
    df_check = pd.read_excel(logbook)

    ## Loop through file list and append together
    df_master = pd.DataFrame([])
    for files in csv_files:

        # Read file
        df = pd.read_csv(files, header=None)

        # Convert LISST time to days since 1970
        time_series = ls.convert_LISST_time(df)

        # Set nc filename
        nc_file, nc_full = extra.set_NC_filename(files, saveDir)

        df = ls.add_LISST_flags(df)

        # Check if valid filename in logbook
        if model=='rs':
            df_check_temp = df_check[df_check['LISST Raw Filename'].str.match(nc_file[:-3], na=False)]
        else:
            df_check_temp = df_check[df_check['LISST Raw Filename'].str.match(nc_file, na=False)]

        ## Check if LISST file needs to be split to match CTD a file
        if len(df_check_temp) > 1:

            # Work out split times
            splitStart, splitEnd = ls.calc_LISST_splits(df_check_temp)

            # Split dataset at splitTimes
            count = 0
            for start, end in zip(splitStart, splitEnd):

                ind = np.where(np.logical_and(time_series>start, time_series<end))
                df_new = df.iloc[ind]

                # Create xarray dataset from subset dataframe
                ds_new = ls.parse_LISST_csv(df_new, fields, units, dtype, bins_lower)

                # Name new split
                file_new = ls.name_split_file(nc_file, count, model)

                # Create NetCDF
                ls.create_LISST_netCDF(file_new, saveDir, ds_new, datatype, sn)
                count += 1

        else:
            # Create NetCDF
            ds_new = ls.parse_LISST_csv(df, fields, units, dtype, bins_lower)
            ls.create_LISST_netCDF(nc_file, saveDir, ds_new, datatype, sn)

        df_master = df_master.append(df)

    # Create xarray dataset from subset dataframe
    ds = ls.parse_LISST_csv(df_master, fields, units, dtype, bins_lower)

    # Create NetCDF
    ls.create_LISST_netCDF((fieldtrip + '_allcasts'), saveDir, ds, datatype, sn)

source_py_code/edge lisst and CTD code/p_lisst_to_nc.py


from VBOFKit import StringTrick
import numpy as np
from viranet.info import metDict, ntpsDict, aaDict, miscDict, ntpsMets, aaMets, N_A, k_atp, k_ppi
# Total AA calculation from the aggregate protein sequence object created by Stringtrick function
def TotalAACalc(protein_copy_number_dict, virus_file):
    protein_sequence_dict = {}
    # Create protein_sequence_dictionary
    for protein in list(protein_copy_number_dict.keys()):
        protein_sequence_dict[protein] = StringTrick.StringTrick(virus_file, protein)
    # Multiply the protein sequence by its assigned copy number
    totalaa = ''
    for i in range(len(protein_copy_number_dict)):
        protein = list(protein_copy_number_dict.keys())[i]
        totalaa += protein_sequence_dict[protein]*protein_copy_number_dict[protein]
    totalaa_array = np.zeros((20,1))
    for aa in range(len(aaMets)):
        totalaa_array[aa,0] = totalaa.count(aaMets[aa])
    return protein_sequence_dict, totalaa_array


import pathlib
from django.db.models.signals import post_save
from django.dispatch import receiver
from allauth.account.signals import user_signed_up
from django.contrib.auth.models import Group
from io import BytesIO
from wagtail.users.models import UserProfile
from custom_storages import s3_media_storage
from users.models import CustomUserProfile, CustomUser
from PIL import Image

@receiver(user_signed_up)
def group_and_profiles(sender, request, user, **kwargs):
	""" add new user to "customers" group, create
	wagtail and user profile fields with a default
	language setting. """
	if user.is_anonymous:
		pass
	else:
		group = Group.objects.get(name='Customers')
		user.groups.add(group)
		wagtail_userprofile_defaults = {
			'submitted_notifications': False,
			'approved_notifications': False,
			'rejected_notifications': False,
			'preferred_language': 'en',
		}

		base_userprofile_defaults = {
			'user_id': user.id,
		}
		user.save()
		UserProfile.objects.get_or_create(user_id=user.id, defaults=wagtail_userprofile_defaults)
		CustomUserProfile.objects.get_or_create(user_id=user.id, defaults=base_userprofile_defaults)

@receiver(post_save, sender=CustomUser)
def default_groups(sender, instance, **kwargs):
	if instance.is_superuser and kwargs.get('created'):
		Group.objects.get_or_create(name='Contributors')
		Group.objects.get_or_create(name='Authors')
		Group.objects.get_or_create(name='Customers')
		user = CustomUser.objects.get(pk=instance.id)
		groups = Group.objects.all()
		user.first_name = 'Admin'
		user.last_name = 'McAdmin'
		user.is_smssubscribed = True
		user.is_mailsubscribed = True
		user.is_active = True
		for group in groups:
			group.user_set.add(user)
		user.save()

		wagtail_userprofile_defaults = {
			'submitted_notifications': True,
			'approved_notifications': True,
			'rejected_notifications': True,
			'updated_comments_notifications': True,
			'preferred_language': 'en',
		}

		base_userprofile_defaults = {
			'user_id': user.id,
		}

		UserProfile.objects.get_or_create(user_id=user.id, defaults=wagtail_userprofile_defaults)
		CustomUserProfile.objects.get_or_create(user_id=user.id, defaults=base_userprofile_defaults)
	else:
		pass

def crop_center(img, crop_width, crop_height):
	width, height = img.size
	return img.crop(((width - crop_width) // 2,
					(height - crop_height) // 2,
					(width + crop_width) // 2,
					(height + crop_height) // 2))

def crop_max_square(img):
    return crop_center(img, min(img.size), min(img.size))

@receiver(post_save, sender=UserProfile)
def resize_avatar(sender, instance, **kwargs):
	if instance.avatar:
		memfile = BytesIO()
		try:
			img = Image.open(instance.avatar.file)
		except:
			img = None

		if img:
			width, height = img.size
			newsize = 100, 100
			try:
				imgformat = img.format
			except:
				imgformat = pathlib.Path(instance.avatar.file.name).suffix.split('.')[1].upper()
			img = crop_max_square(img).resize((newsize), Image.LANCZOS)
			img.save(memfile, imgformat)
			s3_media_storage.save(instance.avatar.name, memfile)
			img.close()
			memfile.close()


import pytest
from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
import re

@pytest.fixture
def driver(request):
    wd = webdriver.Chrome()
    request.addfinalizer(wd.quit)
    return wd

def test_geo_zones_order(driver):
    driver.implicitly_wait(10)
    driver.get('http://localhost/litecart/admin/?app=geo_zones&doc=geo_zones')
    driver.find_element_by_name("username").send_keys("admin")
    driver.find_element_by_name("password").send_keys("admin")
    driver.find_element_by_css_selector('button[name="login"]').click()

    # ждем появления кнопок после всех стран. Для гарантии, что все страны загрузились
    WebDriverWait(driver,10).until(EC.presence_of_element_located((By.CSS_SELECTOR, "table.table tfoot")))

    # находим все строки таблицы
    rows = driver.find_elements_by_css_selector("table.table tbody tr")

    # иницализируем список ссылок за пределами цикла for
    countries_links = []

    for row in rows:
        country_link = row.find_element_by_css_selector("a:not([title=Edit])")
        countries_links.append(country_link.get_attribute("href"))

    # печатать будем с новой строки
    print()

    # для каждой страны
    for country_link in countries_links:
        # Печатаем ссылку
        print (country_link)
        driver.get(country_link)  # загружаем страницу с зонами каждой такой страны по сохраненным ранее ссылкам
        # ждем появления кнопки "Add zone" после списка зон. Для гарантии, что все зоны загрузились
        WebDriverWait(driver,10).until(EC.presence_of_element_located((By.CSS_SELECTOR, "a.add")))

        zones = driver.find_elements_by_css_selector("table.table tbody tr td:nth-child(3)")  # 3-я колонка в таблице
        for i in range(0, len(zones)-1):
            zone_name = zones[i].get_attribute("textContent")
            next_zone_name = zones[i+1].get_attribute("textContent")
            print (zone_name)  # Печатаем название зоны
            if zone_name > next_zone_name:
                raise Exception("Зоны стоят не по порядку")
            if zone_name == next_zone_name:
                raise Exception("Две одинаковых зоны подряд")
        # Печатаем название последней зоны
        zone_name = zones[len(zones)-1].get_attribute("textContent")
        print (zone_name)


import os

import numpy as np
import tensorflow as tf
import torch

from lolip.variables import get_file_name

def set_random_seed(auto_var):
    random_seed = auto_var.get_var("random_seed")

    torch.manual_seed(random_seed)
    tf.random.set_seed(random_seed)
    np.random.seed(random_seed)

    random_state = np.random.RandomState(auto_var.get_var("random_seed"))
    auto_var.set_intermidiate_variable("random_state", random_state)

    return random_state

def load_model(auto_var, trnX, trny, tstX, tsty, n_channels, model_dir="./models", device=None):
    model = auto_var.get_var("model", trnX=trnX, trny=trny, n_channels=n_channels, device=device)
    if tstX is not None and tsty is not None:
        model.tst_ds = (tstX, tsty)
    model_path = get_file_name(auto_var).split("-")
    model_path[0] = 'pgd'
    model_path = '-'.join(model_path)
    if os.path.exists(os.path.join(model_dir, model_path + '-ep%04d.pt') % model.epochs):
        model_path = os.path.join(model_dir, model_path + '-ep%04d.pt') % model.epochs
    else:
        model_path = os.path.join(model_dir, model_path + '.pt')

    model.load(model_path)
    model.model.cuda()
    return model_path, model


from abc import ABC, abstractmethod
from collections import defaultdict
import random
from tkinter import EW
from typing import List, Dict, DefaultDict
from gym.spaces import Space
from gym.spaces.utils import flatdim

class Agent(ABC):
    """Base class for Q-Learning agent

    """

    def __init__(
        self,
        action_space: Space,
        obs_space: Space,
        gamma: float,
        epsilon: float,
        **kwargs
    ):
        """Constructor of base agent for Q-Learning

        Initializes basic variables of the Q-Learning agent
        namely the epsilon, learning rate and discount rate.

        :param action_space (int): action space of the environment
        :param obs_space (int): observation space of the environment
        :param gamma (float): discount factor (gamma)
        :param epsilon (float): epsilon for epsilon-greedy action selection

        :attr n_acts (int): number of actions
        :attr q_table (DefaultDict): table for Q-values mapping (OBS, ACT) pairs of observations
            and actions to respective Q-values
        """

        self.action_space = action_space
        self.obs_space = obs_space
        self.n_acts = flatdim(action_space)

        self.epsilon: float = epsilon
        self.gamma: float = gamma

        self.q_table: DefaultDict = defaultdict(lambda: 0)

    def act(self, obs: int) -> int:
        import numpy as np
        """Implement the epsilon-greedy action selection here

        :param obs (int): received observation representing the current environmental state
        :return (int): index of selected action
        """

        q_vector = np.zeros(self.action_space.n)

        for (i, j), value in self.q_table.items():
            if i == obs:
                q_vector[j] += value

        if random.uniform(0,1) < self.epsilon:
            action = self.action_space.sample()
        else:
            action=np.argmax(q_vector)

        return action

    @abstractmethod
    def schedule_hyperparameters(self, timestep: int, max_timestep: int):
        """Updates the hyperparameters

        This function is called before every episode and allows you to schedule your
        hyperparameters.

        :param timestep (int): current timestep at the beginning of the episode
        :param max_timestep (int): maximum timesteps that the training loop will run for
        """
        ...

    @abstractmethod
    def learn(self):
        ...

class QLearningAgent(Agent):
    """
    Agent using the Q-Learning algorithm

    """

    def __init__(self, alpha: float, **kwargs):
        """Constructor of QLearningAgent

        Initializes some variables of the Q-Learning agent, namely the epsilon, discount rate
        and learning rate alpha.

        :param alpha (float): learning rate alpha for Q-learning updates
        """

        super().__init__(**kwargs)
        self.alpha: float = alpha

    def learn(
        self, obs: int, action: int, reward: float, n_obs: int, done: bool
    ) -> float:
        """Updates the Q-table based on agent experience

        :param obs (int): received observation representing the current environmental state
        :param action (int): index of applied action
        :param reward (float): received reward
        :param n_obs (int): received observation representing the next environmental state
        :param done (bool): flag indicating whether a terminal state has been reached
        :return (float): updated Q-value for current observation-action pair
        """

        import numpy as np

        q_vector_n_obs = np.zeros(self.action_space.n)

        for (i, j), value in self.q_table.items():
            if i == n_obs:
                q_vector_n_obs[j] += value
        best_qn = np.max(q_vector_n_obs)

        new_q_value = self.q_table[(obs,action)]+self.alpha*(reward+self.gamma*best_qn-self.q_table[(obs,action)])
        self.q_table[(obs,action)] = new_q_value

        return self.q_table[(obs, action)]

    def schedule_hyperparameters(self, timestep: int, max_timestep: int):
        """Updates the hyperparameters

        This function is called before every episode and allows you to schedule your
        hyperparameters.

        :param timestep (int): current timestep at the beginning of the episode
        :param max_timestep (int): maximum timesteps that the training loop will run for
        """

        import numpy as np
        alpha_min = 0
        alpha_max = 1

        #Cosine annealing
        self.alpha = alpha_min + 0.5*(alpha_max-alpha_min)*(1+np.cos(timestep/max_timestep*np.pi))

        steepness = 2
        self.alpha = (2*alpha_max)/(np.exp(steepness*timestep/max_timestep)+1)

        self.epsilon = 0.05/(timestep//1000+1)

class MonteCarloAgent(Agent):
    """
    Agent using the Monte-Carlo algorithm for training

    """

    def __init__(self, **kwargs):
        """Constructor of MonteCarloAgent

        Initializes some variables of the Monte-Carlo agent, namely epsilon,
        discount rate and an empty observation-action pair dictionary.

        :attr sa_counts (Dict[(Obs, Act), int]): dictionary to count occurrences observation-action pairs
        """
        super().__init__(**kwargs)
        self.sa_counts = {}

    def learn(
        self, obses: List[int], actions: List[int], rewards: List[float]
    ) -> Dict:
        """Updates the Q-table based on agent experience

        :param obses (List(int)): list of received observations representing environmental states
            of trajectory (in the order they were encountered)
        :param actions (List[int]): list of indices of applied actions in trajectory (in the
            order they were applied)
        :param rewards (List[float]): list of received rewards during trajectory (in the order
            they were received)
        :return (Dict): A dictionary containing the updated Q-value of all the updated state-action pairs
            indexed by the state action pair.
        """
        updated_values = {}
        import numpy as np
        g = 0

        pairs=[(i,j)for i,j in zip(obses,actions)]

        for s in reversed(range(len(obses))):

            g = self.gamma*g + rewards[s]

            if (obses[s],actions[s]) not in pairs[:s]:

                if (obses[s],actions[s]) not in self.sa_counts.keys():
                    self.sa_counts[(obses[s],actions[s])]=0
                else:
                    self.sa_counts[(obses[s],actions[s])]+=1

                if (obses[s],actions[s]) in self.q_table.keys():
                    temp_v = self.q_table[(obses[s],actions[s])]
                else:
                    temp_v = 0

                new_val = (temp_v*self.sa_counts[(obses[s],actions[s])]+g)/(self.sa_counts[(obses[s],actions[s])]+1)
                updated_values[(obses[s],actions[s])] = new_val
                self.q_table[(obses[s],actions[s])] = new_val

        return updated_values

    def schedule_hyperparameters(self, timestep: int, max_timestep: int):
        """Updates the hyperparameters

        This function is called before every episode and allows you to schedule your
        hyperparameters.

        :param timestep (int): current timestep at the beginning of the episode
        :param max_timestep (int): maximum timesteps that the training loop will run for
        """
        import numpy as np
        init_eps = 0.65
        steepness = 5
        self.epsilon = (2*init_eps)/(np.exp(steepness*timestep/max_timestep)+1)


import logging
import unittest

import torch

from openspeech.criterion import Perplexity, PerplexityLossConfigs
from openspeech.lm.transformer_lm import TransformerForLanguageModel
from openspeech.tokenizers.ksponspeech.character import KsponSpeechCharacterTokenizer
from openspeech.utils import DUMMY_INPUT_LENGTHS, DUMMY_LM_INPUTS, DUMMY_TARGETS, build_dummy_configs

logger = logging.getLogger(__name__)

class TestTransformerForLanguageModel(unittest.TestCase):
    def test_forward(self):
        configs = build_dummy_configs(
            criterion_configs=PerplexityLossConfigs(),
        )
        vocab = KsponSpeechCharacterTokenizer(configs)

        model = TransformerForLanguageModel(
            num_classes=4,
            max_length=32,
            d_model=64,
            num_attention_heads=4,
            d_ff=128,
            pad_id=0,
            sos_id=1,
            eos_id=2,
            num_layers=3,
        )

        criterion = Perplexity(configs, vocab)
        optimizer = torch.optim.Adam(model.parameters(), lr=1e-04)

        for i in range(3):
            logits = model(DUMMY_LM_INPUTS, DUMMY_INPUT_LENGTHS)

            loss = criterion(logits, DUMMY_TARGETS)
            loss.backward()
            optimizer.step()
            assert type(loss.item()) == float

    def test_forward_step(self):
        configs = build_dummy_configs(
            criterion_configs=PerplexityLossConfigs(),
        )
        vocab = KsponSpeechCharacterTokenizer(configs)

        model = TransformerForLanguageModel(
            num_classes=4,
            max_length=32,
            d_model=64,
            num_attention_heads=4,
            d_ff=128,
            pad_id=0,
            sos_id=1,
            eos_id=2,
            num_layers=3,
        )

        criterion = Perplexity(configs, vocab)
        optimizer = torch.optim.Adam(model.parameters(), lr=1e-04)

        for i in range(1, 4):
            input_lengths = torch.IntTensor([i] * 3)
            logits = model.forward_step(DUMMY_LM_INPUTS[:i], input_lengths)
            loss = criterion(logits, DUMMY_TARGETS[:i])
            loss.backward()
            optimizer.step()
            assert type(loss.item()) == float

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


#!/usr/bin/python3
'''
@File : wheelpaintwidget.py

@Time : 2020/6/8

@Author : Boholder

@Function : Paint given emotion's position on emotion wheel picture,
            then display it in a widget.
'''

import sys

from PyQt5 import QtGui
from PyQt5.QtCore import pyqtSignal
from PyQt5.QtGui import QPixmap, QPainter
from PyQt5.QtWidgets import (QApplication, QWidget)

import jellygolem.jg_emotion.emotionwheelprocess as emotionproc

_RESOURCE_PATH = '../resource/'
_WHEEL_PIC_PATH = _RESOURCE_PATH + 'emotion-wheel.png'
_HEART_PIC_PATH = _RESOURCE_PATH + 'heart.png'

class WheelPaintWidget(QWidget):
    value_changed_signal = pyqtSignal(float)

    def __init__(self, emotion: tuple = (0, 0), *args,
                 **kwargs):
        super(WheelPaintWidget, self).__init__(*args, **kwargs)

        self.emotion = emotion
        self.wheel_pixmap = QPixmap(_WHEEL_PIC_PATH)
        self.heart_pixmap = QPixmap(_HEART_PIC_PATH)
        # if not so, when use this widget, it has no default space to show.
        self.setMinimumSize(self.wheel_pixmap.size())

    def paintEvent(self, event: QtGui.QPaintEvent):
        super(WheelPaintWidget, self).paintEvent(event)
        self.setGeometry(self.geometry().x(), self.geometry().y(), 650, 650)
        (x, y) = emotionproc.calc_paint_position(*self.emotion)
        painter = QPainter(self)
        painter.drawPixmap(self.rect(), self.wheel_pixmap)
        painter.drawPixmap(x, y, 16, 16, self.heart_pixmap)

    def repaint_wheel(self, emotion: tuple):
        self.emotion = emotion
        self.repaint()

# if __name__ == '__main__':
#     app = QApplication([])
#     ex = WheelPaintWidget()
#     ex.show()
#     sys.exit(app.exec_())


import discord
from discord.ext import commands

pre = "!"
token = "<YOUR DISCORD BOT TOKEN>"

bot = commands.Bot(command_prefix=f"{pre}", case_intensive=True, intents=discord.Intents.all())
bot.remove_command('help')
bot.launch_time = datetime.utcnow()

@bot.event
async def on_ready():
    print(f'{bot.user.name}')
    print(f'{bot.user.id}')
    print("Online")
    print("-------------")
    await bot.load_extension("cogs.bank")

bot.run(token)


import mysql.connector                                                                                #Import nötig für MySQL in Python

def connect(dbname):                                                                                #connect-Funktion stellt Verbindung zur übergebenen DB her
    mydb = mysql.connector.connect(
	host = "localhost",                                                                                #Standard-Einlogdaten für DB
	user = "root",
	password = "",
	database= dbname
    )
    return mydb                                                                                #Rückgabewert ist die Schnittstelle zur DB
    mydb.close()                                                                                #Da schon als Kopie an Hauptprogramm zurückgegeben, kann diese Instanz geschlossen werden

def disconnect(db):
    db.close()                                                                                #Kappt Verbindung zur DB

def send(table,time,cpu,ram,db):                                                                                #Sendet übergebene Daten an spezifizierte DB und Tabelle
    mycursor = db.cursor()                                                                                #Der auf die angegebene DB gerichtete Cursor führt SQL Aktionen an DB aus
    mycursor.execute("INSERT INTO %s (Timestamp,CPU,RAM) VALUES('%s', '%s', '%s');"% (table,time,cpu,ram))                      #Timestamp,CPU und RAM Daten werden in genannte Tabelle eingetragen
    db.commit()                                                                                #commit für tatsächliche Anpassung in Tabelle
    mycursor.close()                                                                                #Cursor kann geschlossen werden

def stats(db,table,interval):                                                                                #stats-Funktion liest Daten aus gegebener DB/Tabelle aus und erstellt eine Statistik, welche Daten aus einem angegebenen Zeitintervall nutzt
    print("\n","MIN_CPU \t MAX_CPU \t AVG_CPU \t MIN_RAM \t MAX_RAM \t AVG_RAM")                                                #Überschrift
    mycursor = db.cursor()                                                                                #Cursor wird wieder auf angegebene DB gerichtet

    mycursor.execute("SELECT MIN(CPU) FROM %s WHERE Timestamp >= DATE_SUB(NOW(), INTERVAL %s);"% (table,interval))              #Der kleinste CPU-Wert in der übergebenen Tabelle mit Zeitsempel größer gleich die aktuelle Zeit minus ein ausgewähltes Zeitintervall wird ausgelesen
    min_cpu = mycursor.fetchone()[0]                                                                                #Der Wert wird in Variable gspeichert

    mycursor.execute("SELECT MAX(CPU) FROM %s WHERE Timestamp >= DATE_SUB(NOW(), INTERVAL %s);"% (table,interval))              #das gleiche mit max CPU Wert
    max_cpu = mycursor.fetchone()[0]

    mycursor.execute("SELECT AVG(CPU) FROM %s WHERE Timestamp >= DATE_SUB(NOW(), INTERVAL %s);"% (table,interval))              #das gleiche mit durchschnittlichem CPU Wert
    avg_cpu = mycursor.fetchone()[0]

    mycursor.execute("SELECT MIN(RAM) FROM %s WHERE Timestamp >= DATE_SUB(NOW(), INTERVAL %s);"% (table,interval))              #ab hier mit RAM
    min_ram = mycursor.fetchone()[0]

    mycursor.execute("SELECT MAX(RAM) FROM %s WHERE Timestamp >= DATE_SUB(NOW(), INTERVAL %s);"% (table,interval))
    max_ram = mycursor.fetchone()[0]

    mycursor.execute("SELECT AVG(RAM) FROM %s WHERE Timestamp >= DATE_SUB(NOW(), INTERVAL %s);"% (table,interval))
    avg_ram = mycursor.fetchone()[0]

    try:
        print("",min_cpu,"\t\t",max_cpu,"\t\t",round(avg_cpu,2) ,"\t\t",min_ram,"\t\t",max_ram,"\t\t",round(avg_ram,2),"\n")    #Ausgabe der Werte unter der Überschrift mit gerundeten Durchschnittswerten
    except:
        print("",min_cpu,"\t\t",max_cpu,"\t\t",avg_cpu ,"\t\t",min_ram,"\t\t",max_ram,"\t\t",avg_ram,"\n")                      #try-excep nötig ohne Rundungen falls im angegebenen Zeitraum keine Werte stehen, da round Funktion sonst Fehler ausgibt

    mycursor.close()

def checkdb(dbname):                                                                                #Funktion checkdb prüft ob DB vorhanden -> falls nicht wird sie erstellt
    try:
        mydb = mysql.connector.connect(
            host = "localhost",
            user = "root",
            password = "",
            database= dbname                                                                                #wenn diese Zeile keinen Fehler generiert, exisitiert die DB
        )
        print("\n\nDatenbank '" + dbname + "' existiert bereits!\n")
    except:
        mydb = mysql.connector.connect(                                                                                #Except Teil wird ausgelöst wenn Fehler generiert wurde
            host = "localhost",
            user = "root",
            password = "",
        )
        mycursor = mydb.cursor()
        mycursor.execute("CREATE DATABASE %s;"% (dbname))                                                                       #Datenbank mit gewähltem Namen wird erstellt
        mycursor.close()
        print("\n\nDatenbank '" + dbname + "' wurde erstellt.\n")
        mydb.close()

def listdb():                                                                                #Funktion listet Datenbanken auf
    mydb = mysql.connector.connect(
            host = "localhost",
            user = "root",
            password = ""
    )
    mycursor = mydb.cursor()

    mycursor.execute("SHOW DATABASES")

    for x in mycursor:
        print(x)

    mycursor.close()
    mydb.close()

def checktable(db,table):                                                                                #Funktion prüft ob Tabelle vorhanden ist
    mycursor = db.cursor()

    mycursor.execute("SHOW TABLES LIKE '%s'"% (table))                                                                          #listet Tabellennamen auf, die wie der übergebene Name sind

    for x in mycursor:
        if table in x:                                                                                #Tabellennamen muss nur in x vorkommen, da mit "SHOW TABLES" Tabellennamen so ausgelesen werden : "(tabellenname);". Fehler dadurch können nicht vorkommen, da mir "SHOW TABLES LIKE ..." schon gefiltert wurde
            return True
        else:
            return False

def add_device(db,tablename):                                                                                #Erstellt neue Tabelle
    mycursor = db.cursor()

    mycursor.execute("CREATE TABLE %s (Timestamp TIMESTAMP, CPU FLOAT, RAM FLOAT);"% (tablename))                               #Erstellt Tabelle mit gewähltem Namen mit Timestamp, CPU und RAM Spalte
    print("\nGerät '" + tablename + "' wurde erstellt.\n")
    mycursor.close()

def listtable(db):                                                                                #Funktion listet alle Tabellen einer spezifizierten DB auf
    mycursor = db.cursor()

    mycursor.execute("SHOW TABLES")

    for x in mycursor:
        print(x)

    mycursor.close()


import os
import torch
import numpy as np
import pandas as pd
import random
import logging
import copy
from .metrics import Metrics

class EarlyStopping:
    """Early stops the training if validation loss doesn't improve after a given patience."""
    def __init__(self, args, delta=1e-6):
        """
        Args:
            patience (int): How long to wait after last time validation loss improved.
            delta (float): Minimum change in the monitored quantity to qualify as an improvement.
        """
        self.patience = args.wait_patience
        self.logger = logging.getLogger(args.logger_name)
        self.monitor = args.eval_monitor
        self.counter = 0
        self.best_score = 1e8 if self.monitor == 'loss' else 1e-6
        self.early_stop = False
        self.delta = delta
        self.best_model = None

    def __call__(self, score, model):

        better_flag = score <= (self.best_score - self.delta) if self.monitor == 'loss' else score >= (self.best_score + self.delta)

        if better_flag:
            self.counter = 0
            self.best_model = copy.deepcopy(model)
            self.best_score = score

        else:
            self.counter += 1
            self.logger.info(f'EarlyStopping counter: {self.counter} out of {self.patience}')

            if self.counter >= self.patience:
                self.early_stop = True

def set_torch_seed(seed):
    random.seed(seed)
    np.random.seed(seed)
    torch.manual_seed(seed)
    torch.cuda.manual_seed(seed)
    torch.cuda.manual_seed_all(seed) # if you are using multi-GPU.
    torch.backends.cudnn.deterministic = True
    torch.backends.cudnn.benchmark = False
    os.environ['PYTHONHASHSEED'] = str(seed)

def set_output_path(args):

    if not os.path.exists(args.output_path):
        os.makedirs(args.output_path)

    pred_output_path = os.path.join(args.output_path, args.logger_name)
    if not os.path.exists(pred_output_path):
        os.makedirs(pred_output_path)

    model_path = os.path.join(pred_output_path, args.model_path)
    if not os.path.exists(model_path):
        os.makedirs(model_path)

    return pred_output_path, model_path

def save_npy(npy_file, path, file_name):
    npy_path = os.path.join(path, file_name)
    np.save(npy_path, npy_file)

def load_npy(path, file_name):
    npy_path = os.path.join(path, file_name)
    npy_file = np.load(npy_path)
    return npy_file

def save_model(model, model_dir):

    save_model = model.module if hasattr(model, 'module') else model
    model_file = os.path.join(model_dir, 'pytorch_model.bin')
    model_config_file = os.path.join(model_dir, 'config.json')
    torch.save(save_model.state_dict(), model_file)
    with open(model_config_file, "w") as f:
        f.write(save_model.config.to_json_string())

def restore_model(model, model_dir):
    output_model_file = os.path.join(model_dir, 'pytorch_model.bin')
    model.load_state_dict(torch.load(output_model_file))
    return model

def save_results(args, test_results, debug_args = None):

    pred_labels_path = os.path.join(args.pred_output_path, 'y_pred.npy')
    np.save(pred_labels_path, test_results['y_pred'])

    true_labels_path = os.path.join(args.pred_output_path, 'y_true.npy')
    np.save(true_labels_path, test_results['y_true'])

    if 'features' in test_results.keys():
        features_path = os.path.join(args.pred_output_path, 'features.npy')
        np.save(features_path, test_results['features'])

    if not os.path.exists(args.results_path):
        os.makedirs(args.results_path)

    results = {}
    metrics = Metrics(args)
    for key in metrics.eval_metrics:
        results[key] = round(test_results[key] * 100, 2)

    eval_key = 'eval_' + args.eval_monitor
    results.update({eval_key: test_results['best_eval_score']})

    _vars = [args.dataset, args.method, args.text_backbone, args.seed, args.logger_name]
    _names = ['dataset',  'method', 'text_backbone', 'seed', 'logger_name']

    if debug_args is not None:
        _vars.extend([args[key] for key in debug_args.keys()])
        _names.extend(debug_args.keys())

    vars_dict = {k:v for k,v in zip(_names, _vars)}
    results = dict(results,**vars_dict)

    keys = list(results.keys())
    values = list(results.values())

    results_path = os.path.join(args.results_path, args.results_file_name)

    if not os.path.exists(results_path) or os.path.getsize(results_path) == 0:
        ori = []
        ori.append(values)
        df1 = pd.DataFrame(ori,columns = keys)
        df1.to_csv(results_path,index=False)
    else:
        df1 = pd.read_csv(results_path)
        new = pd.DataFrame(results,index=[1])
        df1 = df1.append(new,ignore_index=True)
        df1.to_csv(results_path,index=False)
    data_diagram = pd.read_csv(results_path)

    print('test_results', data_diagram)


#!/usr/bin/env python
# coding=utf-8

from __future__ import unicode_literals

from pytreex.core.block import Block
from pytreex.core.exception import LoadingException

__author__ = "Silvie Cinková"
__date__ = "2015"

# OD's note: this seems really complicated -- why not just relabel all RSTRs to mod ??
# I think that it would work the same in 99.9% cases

class WhiteMarble(Block):

    """
    This class creates this AMR structure:
    (m / marble
        :mod (w / white))
    from "white marble". We ignore the case "marble that is white".
    Also, this time we ignore adjectival attributes in coordinations and
    #look only for attributes as direct children of the governing noun,
    #since coordinated attributes are already covered by
    #the Coordination class!
    #This class served as the base for the HisBoat class, so if one
    #gets an update/correction, the other ought to as well.
    """

    def __init__(self, scenario, args):
        "Constructor, just checking the argument values"
        Block.__init__(self, scenario, args)
        if self.language is None:
            raise LoadingException('Language must be defined!')

    def process_amrnode(self, amrnode):
        tnode = amrnode.src_tnode
        if tnode is None:
            return
        if not tnode.formeme.startswith('n:'):
            return
        else:
            # is really OK, ignore attr in coord
            tchildren = tnode.get_children()
            # detect all descendants of amrnode, not just children
            # because the relevant amrchildren could have been already
            # relocated by other rules, but unlikely somewhere upwards
            amrdescendants = amrnode.get_descendants()
            # detect all RSTR functored tchildren of tnode
            rstr_tnodes = set()
            for tchild in tchildren:
                if (tchild.functor == 'RSTR' and
                        tchild.formeme.startswith('adj')):
                    # have all RSTR nodes
                    rstr_tnodes.add(tchild)

            for amrdescendant in amrdescendants:  # find amrnode
                # descendants with the same id as their corresponding t-nodes
                if amrdescendant.src_tnode in rstr_tnodes:
                    amrdescendant.modifier = 'mod'
                    # make sure that this amrmoddescendant is
                    # amrmod's child:
                    amrdescendant.parent = amrnode


from urllib.request import urlopen
from finder import LinkFinder
from domain import *
from file_utils import *

class WebSpider:
    folder_name = ''
    base_url = ''
    domain_name = ''
    frontier_file = ''
    fetched_file = ''
    frontier = set()
    fetched = set()

    def __init__(self, project_name, base_url, domain_name):
        WebSpider.folder_name = project_name
        WebSpider.base_url = base_url
        WebSpider.domain_name = domain_name
        WebSpider.frontier_file = WebSpider.folder_name + '/frontier.txt'
        WebSpider.fetched_file = WebSpider.folder_name + '/downloadList.txt'
        self.init()
        self.fetch_page('First spider', WebSpider.base_url)

    # Creates directory and files for project on first run and starts the spider
    @staticmethod
    def init():
        create_project_directory(WebSpider.folder_name)
        create_data_files(WebSpider.folder_name, WebSpider.base_url)
        WebSpider.frontier = file_to_set(WebSpider.frontier_file)
        WebSpider.fetched = file_to_set(WebSpider.fetched_file)

    # Updates user display, fills frontier and updates files
    @staticmethod
    def fetch_page(thread_name, page_url):
        if page_url not in WebSpider.fetched:
            print(thread_name + ' now crawling ' + page_url)
            print('Frontier ' + str(len(WebSpider.frontier)) + ' page(s) | Fetched ' + str(len(WebSpider.fetched)) + " page(s).")
            links = WebSpider.gather_links(page_url, len(WebSpider.fetched))  # Parse html and Gather links first
            WebSpider.add_links_to_queue(links, False)  # Then add to frontier
            WebSpider.frontier.remove(page_url)
            WebSpider.fetched.add(page_url)
            WebSpider.update_files()

    # Converts raw response data into readable information and checks for proper html formatting
    @staticmethod
    def gather_links(page_url, file_index):
        html_string = ''
        try:
            response = urlopen(page_url)
            if 'text/html' in response.getheader('Content-Type'):
                html_bytes = response.read()
                html_string = html_bytes.decode("utf-8")
                # Write to file
                write_file_in_folder(WebSpider.folder_name, str(file_index + 1) + ".html", html_string, True)
            finder = LinkFinder(WebSpider.base_url, page_url)
            finder.feed(html_string)
        except Exception as e:
            print(str(e))
            return set()
        return finder.page_links()  # bring to inside try catch

    # Saves frontier data to project files
    @staticmethod
    def add_links_to_queue(links, is_crawl_other_sites):
        for url in links:
            if (url in WebSpider.frontier) or (url in WebSpider.fetched):
                continue
            if WebSpider.domain_name != get_domain_name(url) and not is_crawl_other_sites:
                continue
            WebSpider.frontier.add(url)

    @staticmethod
    def update_files():
        set_to_file(WebSpider.frontier, WebSpider.frontier_file)
        set_to_file(WebSpider.fetched, WebSpider.fetched_file)


import keras
from keras import backend as K
from keras.models import Sequential, Model
from keras.layers import Input, Reshape, merge
from keras.layers import Activation
from keras.layers.core import Flatten, Dense, Dropout, Lambda
from keras.regularizers import l2, activity_l2, l1, activity_l1
from keras.layers.normalization import BatchNormalization
from keras.optimizers import SGD, RMSprop, Adam
from keras.metrics import categorical_crossentropy, categorical_accuracy
from keras.layers.convolutional import *
from keras.layers.pooling import GlobalAveragePooling2D

BOUND_SIZE = 32
def get_model():
    model = Sequential([
        Lambda(norm_input, input_shape=(1,BOUND_SIZE,BOUND_SIZE)),
        Convolution2D(64,2,2, activation='relu'), #31
        BatchNormalization(axis=1),
        Convolution2D(64,2,2, activation='relu'), #30
        BatchNormalization(axis=1),
        Convolution2D(64,2,2, subsample=(2, 2), activation='relu'), #15
        BatchNormalization(axis=1),
        Convolution2D(128,2,2, activation='relu'), #14
        BatchNormalization(axis=1),
        Dropout(0.3),
        Convolution2D(128,2,2, activation='relu'), #13
        BatchNormalization(axis=1),
        Dropout(0.3),
        Convolution2D(128,2,2, subsample=(2, 2), activation='relu'), #6
        BatchNormalization(axis=1),
        Dropout(0.3),
        Convolution2D(192,2,2, activation='relu'), #5
        BatchNormalization(axis=1),
        Dropout(0.4),
        Convolution2D(192,2,2, activation='relu'), #4
        BatchNormalization(axis=1),
        Dropout(0.4),
        Convolution2D(192,2,2, subsample=(2, 2), activation='relu'), #2
        BatchNormalization(axis=1),
        Dropout(0.4),
        Convolution2D(256,2,2, activation='relu'), #1
        BatchNormalization(axis=1),
        Dropout(0.5),
        Convolution2D(256,1,1, activation='relu'), #1
        BatchNormalization(axis=1),
        Dropout(0.5),
        Convolution2D(2,1,1, activation='relu'), #1
        GlobalAveragePooling2D(),
        Activation('softmax')
        ])
    model.compile(Adam(), loss='categorical_crossentropy', metrics=['accuracy'])
    return model


#!/usr/bin/env python

import sys
sys.path.append('../pytorch_load_image/')
import torch
from torch.autograd import Variable
import numpy as np
import torch.nn.functional as F
import matplotlib
from img_load import *
from RFF import *
from sklearn import linear_model
import collections
import pickle
import time

np.set_printoptions(precision=4)
np.set_printoptions(threshold=np.nan)
np.set_printoptions(linewidth=300)
np.set_printoptions(suppress=True)

class cnn_kernel_net(torch.nn.Module):
	def __init__(self, db, learning_rate=0.001):
		super(cnn_kernel_net, self).__init__()
		self.db = db
		self.filter_len = 5
		self.num_output_channels = 128
		[H,W] = self.extract_HW(db)

		self.conv1 = nn.Conv2d(1,16,	self.filter_len,stride=2)
		self.conv2 = nn.Conv2d(16,32,	self.filter_len,stride=2)
		self.conv3 = nn.Conv2d(32,self.num_output_channels,	self.filter_len,stride=2)
		final_layer_size = self.num_output_channels*H*W

		self.l1 = torch.nn.Linear(final_layer_size , 10, bias=True)
		self.l2 = torch.nn.Linear(10, final_layer_size , bias=True)
		self.conv4 = nn.ConvTranspose2d(self.num_output_channels, 32, self.filter_len, stride=2)
		self.conv5 = nn.ConvTranspose2d(32, 16, self.filter_len, stride=2)
		self.conv6 = nn.ConvTranspose2d(16, 1, self.filter_len, stride=2)

		self.criterion = torch.nn.MSELoss(size_average=False)
		self.learning_rate = learning_rate

		self.initialize()

	def initialize(self):
		for param in self.parameters():
			if(len(param.data.numpy().shape)) > 1:
				torch.nn.init.kaiming_normal(param.data , a=0, mode='fan_in')
			else:
				param.data = torch.zeros(param.data.size())

	def initialize_RFF(self, db, sigma):
		self.rff = RFF(30000)
		self.rff.manual_init(db['batch_size'], db['output_dim'], sigma, db['dataType'])

	def set_Y(self, Y_matrix):
		Y_matrix = torch.from_numpy(Y_matrix)
		self.Y = Variable(Y_matrix.type(self.db['dataType']), requires_grad=False)

	def get_optimizer(self):
		return torch.optim.Adam(self.parameters(), lr=self.learning_rate)

	def extract_HW(self, db):
		H = db['img_height']
		W = db['img_width']
		for m in range(3):
			H = (H - self.filter_len)/2 + 1
			W = (W - self.filter_len)/2 + 1
		return [H,W]

	def encoder(self, y0):
		y1 = F.relu(self.conv1(y0))
		y2 = F.relu(self.conv2(y1))
		y3 = F.relu(self.conv3(y2))
		return y3

	def CAE_compute_loss(self, y0, indices=None):
		y_pred = self.CAE_forward(y0)

		#print(y0.shape)
		#print(y_pred.shape)
		#import pdb; pdb.set_trace()
		#return torch.sum((y_pred - y0) ** 2)
		return self.criterion(y_pred, y0)

	def CAE_forward(self, y0):
		y1 = self.encoder(y0)
		y2 = y1.view(y0.shape[0],-1)

		y3 = self.l1(y2)
		y4 = F.relu(self.l2(y3))
		y5 = y4.view( y1.size() )

		y6 = F.relu(self.conv4(y5))
		y7 = F.relu(self.conv5(y6))
		y8 = self.conv6(y7)

		return y8

	def gaussian_kernel(self, x):			#Each row is a sample
		bs = x.shape[0]
		s = self.sigma

		if bs < 50:	# Compute raw RBF kernel
			K = self.db['dataType'](bs, bs)
			K = Variable(K.type(self.db['dataType']), requires_grad=False)

			for i in range(bs):
				for j in range(bs):
					tmpY = (x[i,:] - x[j,:]).unsqueeze(0)
					eVal = -(torch.mm(tmpY, tmpY.transpose(0,1)))/(2*s*s)
					K[i,j] = torch.exp(eVal)
		else: # use RFF
			K = self.rff.get_rbf(x, s, True)

		return K

	def compute_loss(self, x, indices):
		x_out = self.forward(x)
		rbk = self.gaussian_kernel(x_out)

		PP = self.Y[indices, :]
		Ysmall = PP[:, indices]
		cost = -torch.sum(rbk*Ysmall)

#		print rbk
#		print Ysmall
#		print self.db['dataset'].y[indices]
#		print indices
#		import pdb; pdb.set_trace()
		return cost

	def kernel_net_output(self, y0):
		return self.forward(y0)

	def forward(self, y0):
		y1 = self.encoder(y0)
		y2 = y1.view(y0.shape[0],-1)
		y3 = self.l1(y2)
		return y3

if __name__ == '__main__':
	def loss_optimization_printout(epoch, avgLoss, avgGrad, epoc_loop, slope):
			sys.stdout.write("\r\t\t%d/%d, MaxLoss : %f, AvgGra : %f, progress slope : %f" % (epoch, epoc_loop, avgLoss, avgGrad, slope))
			sys.stdout.flush()

	def get_slope(y_axis):
		y_axis = np.array(list(y_axis))

		n = len(y_axis)
		LR = linear_model.LinearRegression()
		X = np.array(range(n))
		X = X.reshape((n,1))
		y_axis = y_axis.reshape((n,1))
		LR.fit(X, y_axis)
		#print LR.intercept_

		return LR.coef_

	def get_loss(ckernel_net, data_loader):
		#	Compute final average loss
		loss_sum = 0
		for idx, data in enumerate(data_loader):
			data = Variable(data.type(dtype), requires_grad=False)
			try:
				loss = ckernel_net.CAE_compute_loss(data)
			except:
				import pdb; pdb.set_trace()
			loss_sum += loss

		avgL = loss_sum/idx
		return avgL.cpu().data.numpy()[0]

	start_time = time.time()

	if torch.cuda.is_available(): dtype = torch.cuda.FloatTensor
	else: dtype = torch.FloatTensor
	exit_loss=0.001
	face_data = image_datasets('../../dataset/faces/', 'face_img')
	data_loader = DataLoader(face_data, batch_size=5, shuffle=True, drop_last=True)

	db = {}
	db['img_height'] = 29
	db['img_width'] = 29
	db['batch_size'] = 5

	epoc_loop = 5000

	if torch.cuda.is_available(): ckernel_net = cnn_kernel_net(db).cuda()
	else: ckernel_net = cnn_kernel_net(db)

	learning_rate = 1e-3
	optimizer = torch.optim.Adam(ckernel_net.parameters(), lr=learning_rate, weight_decay=1e-5)
	avgLoss_cue = collections.deque([], 400)

	avgLoss = get_loss(ckernel_net, data_loader)
	print('Starting avg loss %.3f.'%avgLoss)

	for epoch in range(epoc_loop):
		running_avg = []
		running_avg_grad = []

		for idx, data in enumerate(data_loader):
			data = Variable(data.type(dtype), requires_grad=False)

			optimizer.zero_grad()
			loss = ckernel_net.CAE_compute_loss(data)
			loss.backward()
			optimizer.step()

			grad_norm = 0
			for param in ckernel_net.parameters():
				grad_norm += param.grad.data.norm()

			running_avg_grad.append(grad_norm)
			running_avg.append(loss.data[0])

		maxLoss = np.max(np.array(running_avg))		#/db['num_of_output']
		avgGrad = np.mean(np.array(running_avg_grad))
		avgLoss_cue.append(maxLoss)
		progression_slope = get_slope(avgLoss_cue)

		loss_optimization_printout(epoch, maxLoss, avgGrad, epoc_loop, progression_slope)

		if maxLoss < exit_loss: break;
		if len(avgLoss_cue) > 300 and progression_slope > 0: break;

	avgLoss = get_loss(ckernel_net, data_loader)
	print('\nEnding avg loss %.3f.'%avgLoss)

	try:
		prev_result = pickle.load( open( "face.p", "rb" ) )
	except:
		prev_result = {}
		prev_result['avgLoss'] = 1000000

	if prev_result['avgLoss'] > avgLoss:
		result = {}
		result['avgLoss'] = avgLoss
		result['kernel_net'] = ckernel_net
		pickle.dump( result, open( "face.p", "wb" ) )

	print("\n--- Run Time : %s seconds ---\n" % (time.time() - start_time))


import numpy as np
import random
from functions import BUILD_FUNCTIONS
from PIL import Image, ImageDraw, ImageFont
from os.path import join, isfile
import config
import markovify
import os

def random_sequence_length(max_length, min_length):
    if max_length is None and min_length is not None:
        sequence_length = min_length
    elif max_length is not None and min_length is not None:
        sequence_length = max_length
    elif max_length is not None and min_length is not None:
        sequence_length = np.random.randint(min_length, max_length + 1)
    else:
        sequence_length = 1
    return sequence_length

def make_text(quotes_path, max_length=None, min_length=None, state_size=2, seed=42):
    with open(quotes_path, encoding='utf8') as quote_file:
       text = quote_file.read()

    text_model = markovify.NewlineText(text, state_size=state_size)
    sequence_length = random_sequence_length(max_length, min_length)

    random.seed(seed)
    return text_model.make_short_sentence(sequence_length)

def log_tree_to_file(func, depth, log_filepath="tree.txt"):
    if log_filepath is not None:
        mode = "a" if isfile(log_filepath) else "w"
        with open(log_filepath, mode) as log_file:
            log_file.write("|\t"*depth + func.__name__ + "\n")

# Generate x and y images, with 3D shape so operations will correctly broadcast.
def get_random_function(depth=0, min_depth=5, max_depth=15, p=None):

    if p is None:
        p = np.ones(len(BUILD_FUNCTIONS))

    funcs, weights = list(), list()

    for (n_args, function), w in zip(BUILD_FUNCTIONS, p):
        if (n_args > 0 and depth < max_depth) or (n_args == 0 and depth >= min_depth):
            funcs.append((n_args, function))
            weights.append(w)

    weights = list(np.array(weights)/sum(weights))
    idx = np.random.choice(range(len(funcs)), p=weights)
    n_args, func = funcs[idx]

    return n_args, func

def build_img(min_depth=5, max_depth=15, dx=100, dy=100, weights=None, log_filepath="tree.txt", seed=42):

    np.random.seed(seed % (2**32 - 1))

    def _build_img(depth=0):
        n_args, func = get_random_function(depth, p=weights, min_depth=min_depth, max_depth=max_depth)
        log_tree_to_file(func, depth, log_filepath=log_filepath)
        args = [_build_img(depth + 1) for i in range(n_args)]
        kwargs = dict(dx=dx, dy=dy) if n_args == 0 else {}
        try:
            return func(*args, **kwargs)
        except Exception as e:
            print(func.__name__, str(e))
            raise e
    return _build_img(depth=0)

def make_background(dx, dy, min_depth=5, max_depth=15, seed=42, save_filepath=None, log_path=None, personality=None):

    log_filepath = join(log_path, f"tree_{seed}.txt") if log_path is not None else None

    fail_counter = 0
    while True:
        img = build_img(min_depth=min_depth, max_depth=max_depth, dx=dx, dy=dy, weights=personality, log_filepath=log_filepath, seed=seed)
        # Ensure it has the right dimensions
        if img.shape == (dy, dx, 3):
            break
        else:
            fail_counter += 1
            if fail_counter > 10:
                raise Exception(f"Too many failures when building image (got {img.shape})")
            print(f"Failed build image (wrong dimensions). Trying again with seed {seed+1} [{fail_counter}/{10}]")
            seed += 1

    # Convert to 8-bit, send to PIL and save
    img_8bit = np.rint(img.clip(0.0, 1.0)* 255.0).astype(np.uint8)

    if save_filepath is not None:
        Image.fromarray(img_8bit).save(save_filepath)
        print('Seed {}; wrote output to {}'.format(seed, save_filepath))
    return img_8bit

# This is being worked on, as wrapped text won't draw an outline for some reason
# as such, the max text size is 50 chars so it does not have to wrap
def text_wrap(text, font, max_width):

    lines = []

    #if the width is smaller than the image width, add to lines array
    if font.getsize(text)[0] <= max_width:
        lines.append(text)
    else:
        words = text.split(' ')
        i = 0
        while i < len(words):
            line = ''
            while i < len(words) and font.getsize(line + words[i])[0] <= max_width:
                line = line + words[i] + " "
                i += 1
            if not line:
                line = words[i]
                i += 1
            #when the line is longer than the max width, add line to array
            lines.append(line)
    return lines

def combine_image(text, fontsize=40, background_path="./background.png", output_path="./output.png", font_path="./zalgo.ttf"):

    #make the new image
    img = Image.open(background_path)
    width, height = img.size
    #setup d for drawing
    drawing = ImageDraw.Draw(img)
    #set image size
    image_size = (width, height)
    #set the colour of text & outline
    textcolour = (0, 0, 0)
    text_outline = (255, 255, 255)
    #create the font and set text size
    fnt = ImageFont.truetype(font_path, fontsize, encoding="unic")

    draw = ImageDraw.Draw(img)
    #Set outline amount, sets how many times we loop
    outline_amount = 3

    #set the height of each line
    line_height = fnt.getsize('hg')[1]
    x = random.randint(0, 50)
    y = random.randint(0, 150)

    # Make outline by re-drawing the text in every direction
    for adj in range(outline_amount):
        #move right
        draw.text((x-adj, y), text, font=fnt, fill=text_outline)
        #move left
        draw.text((x+adj, y), text, font=fnt, fill=text_outline)
        #move up
        draw.text((x, y+adj), text, font=fnt, fill=text_outline)
        #move down
        draw.text((x, y-adj), text, font=fnt, fill=text_outline)
        #diagnal left up
        draw.text((x-adj, y+adj), text, font=fnt, fill=text_outline)
        #diagnal right up
        draw.text((x+adj, y+adj), text, font=fnt, fill=text_outline)
        #diagnal left down
        draw.text((x-adj, y-adj), text, font=fnt, fill=text_outline)
        #diagnal right down
        draw.text((x+adj, y-adj), text, font=fnt, fill=text_outline)

    #check text's length to see if we need to wrap
    if len(text) >= 55:

        wrapped_text = text_wrap(text, fnt, image_size[0])

        for lines in wrapped_text:

            if lines == None:
                break
            else:
                #draw the lines on the image
                drawing.text((x,y), lines, fill=textcolour, font=fnt)
                y = y + line_height

    else:
        drawing.text((x,y), text, font=fnt, fill=textcolour)

    img.save(output_path, optimize=True)
    print('Text written')


import threading
import time

def sleeper(n, name):
    print('Hi Iam {}.\nIam going to sleep for 5 seconds.\n'.format(name))
    time.sleep(n)
    print('Hey, {} just got up'.format(name))

t = threading.Thread(target = sleeper, name = 'thread1', args = (5,'thread1'))

t.start()

learning threads, queues, locks/threads1 sleeper moon.py


import datetime

from django.contrib.auth.decorators import login_required
from django.shortcuts import render, redirect
from .forms import *
from .models import *

@login_required()
def Menu_form(request):
    form = menu_form()  # form called from forms.py
    if request.method == "POST":
        form = menu_form(request.POST or None, request.FILES)
        if form.is_valid():
            menu = form.save(commit=False)
            menu.restaurant = request.user
            menu.save()
            return redirect("/menu/view")
        else:
            print(form.errors)
    return render(request, "menu/menu_form.html", {"form": form})

@login_required()
def menu_view(request):
    # content = menu_details.objects.filter(restaurant_name=request.user.restaurant.restaurant_name)
    content = menu_details.objects.filter(restaurant=request.user)
    return render(request, "menu/menu_view.html", {'content': content})

@login_required()
def menu_edit(request, id):
    content = menu_details.objects.get(id=id)
    form = menu_form(request.POST or None, instance=content)
    return render(request, 'menu/menu_update.html', {'form': form, 'content': content})

@login_required()
def menu_update(request, id):
    content = menu_details.objects.get(id=id)
    form = menu_form(request.POST or None, request.FILES, instance=content)
    if form.is_valid():
        form.save()
        return redirect("/menu/view/")
    else:
        print(form.errors)
    return render(request, 'menu/menu_update.html', {'form': form, 'content': content})

@login_required()
def menu_delete(request, id):
    content = menu_details.objects.get(id=id)
    content.delete()
    return redirect("/menu/view/")

def menu_global_view(request):
    content = menu_details.objects.all()
    return render(request, "menu/menu_global_view.html", {'content': content})

@login_required()
def place_order(request, id):
    content = menu_details.objects.get(id=id)  #for order details
    menu_content = menu_details.objects.all()  #for showing data in global menu

    cust_name = request.user.username
    r_name = content.restaurant.restaurant.restaurant_name
    dish_name = content.dish_name
    dish_type = content.dish_type
    price = content.price
    description = content.description

    data = order_details(customer_name=cust_name, restaurant_name=r_name, description=description,
                         dish_name=dish_name, dish_type=dish_type, price=price)
    data.save()
    return render(request, 'menu/menu_global_view.html', {'content': menu_content})

@login_required()
def view_orders_restaurant(request):
    content = order_details.objects.filter(restaurant_name=request.user.restaurant.restaurant_name)
    title = 'Orders Received'
    return render(request, "order/order_view.html", {'content': content, 'title': title})

@login_required()
def view_orders_customer(request):
    content = order_details.objects.filter(customer_name=request.user)
    title = 'Orders Placed'
    return render(request, "order/order_view.html", {'content': content, 'title': title})


import streamlit as st
import time
import numpy as np
import pandas as pd

st.set_page_config(
    page_title="База данных",
    page_icon="📈"
)

st.markdown("# Данные о растениях")

# ========== READ THE DATA ==========

# species
f = open("data/atlas_species.txt", "r")
species = [x.replace("\n", "") for x in f.readlines()]

# collected CSV
daf_path = "data/result.csv"
daf = pd.read_csv(daf_path)

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

search_query = st.text_input("Поиск по базе данных", placeholder="Поиск по базе данных")
for ix, specie in enumerate([x for x in species if search_query.lower() in x.lower()]):
    row = daf[daf['name'] == specie]
    if row.shape[0] == 0:
        continue
    row = row.iloc[0]

    # st.markdown(f"**{specie}**")
    with st.expander(f"**{specie}**"):
        st.markdown(f"**Поизрастает:** {row['area']}")
        st.markdown(f"**Почва:** {row['soil']}")
        st.markdown(f"**Время:** {row['timings']}")
        st.markdown(f"**Время:** {row['chemicals']}")
        st.markdown(f"**Время:** {row['medicine']}")


import vtk
import numpy as np
from typing import Tuple, Union
from vtk.util.numpy_support import numpy_to_vtk, vtk_to_numpy
import vtk_util
import math

PolyDataLike = Union[vtk.vtkPolyData, Tuple[np.ndarray, np.ndarray]]

class SurfaceRenderer:
    renderer = None

    def __init__(self, offscreen=True):
        cls = SurfaceRenderer
        if cls.renderer is None:
            cls.renderer = vtk.vtkRenderer()
            self.renderer = cls.renderer
        else:
            self.renderer = cls.renderer

        self.camera = vtk.vtkCamera()
        self.render_window = vtk.vtkRenderWindow()
        self.render_window.SetUseOffScreenBuffers(offscreen)
        self.render_window.AddRenderer(self.renderer)

        self.window_to_image_filter = vtk.vtkWindowToImageFilter()

        self.actors = []

    def set_camera_parameter(self, geometry):
        vtk_extrinsic = vtk_util.convert_numpy_matrix4x4(geometry.extrinsic)
        self.camera.SetModelTransformMatrix(vtk_extrinsic)

        self.camera.SetPosition(0, 0, 0)
        self.camera.SetFocalPoint(0, 0, -1)
        self.camera.SetClippingRange(100, geometry.SDD)

        detector_size = (np.array(geometry.image_size) *
                         np.array(geometry.pixel_spacing))

        rad_angle = math.atan2(detector_size[1] / 2, geometry.SDD)
        self.camera.SetViewAngle(2 * rad_angle * 180 / math.pi)

    def setup_render(self):
        for actor in self.actors:
            self.renderer.RemoveActor(actor)

        self.actors = []
        self.renderer.SetActiveCamera(self.camera)
        self.renderer.SetBackground(0.0, 0.0, 0.0)

    def render(self, poly_data: PolyDataLike, pose=np.zeros(6)) -> None:
        if isinstance(poly_data, tuple):
            poly_data = self.convert_ply2poly_data(*poly_data)

        actor = None
        if (not isinstance(poly_data, vtk.vtkActor) and
                not issubclass(type(poly_data), vtk.vtkActor)):
            actor = self.setup_actor(poly_data)
        else:
            actor = poly_data

        self.actors.append(actor)
        self.renderer.AddActor(actor)

    def flush(self):
        self.render_window.Render()

    def capture(self):
        self.window_to_image_filter.SetInput(self.render_window)
        self.window_to_image_filter.SetMagnification(
            self.image_size[0] / self.render_window.GetSize()[0]
        )
        self.window_to_image_filter.SetInputBufferTypeToRGB()
        self.window_to_image_filter.ReadFrontBufferOff()
        self.window_to_image_filter.Update()

        vtk_image = self.window_to_image_filter.GetOutput()
        rows, cols, _ = vtk_image.GetDimensions()
        vtk_pointer = vtk_image.GetPointData().GetScalars()
        a = vtk_to_numpy(vtk_pointer)
        if a.ndim == 2:
            return None
        a = a.reshape(rows, cols, -1)
        return a

    def setup_actor(self, poly_obj):
        mapper = vtk.vtkPolyDataMapper()
        mapper.SetInputData(poly_obj)
        mapper.Update()

        actor = vtk.vtkActor()
        actor.SetMapper(mapper)

        return actor

    def convert_ply2poly_data(self, vertices, faces):
        # setup points
        if not isinstance(faces, vtk.vtkPoints):
            vtkArray = numpy_to_vtk(vertices, deep=1)
            points = vtk.vtkPoints()
            points.SetData(vtkArray)
        else:
            points = vertices

        # setup traiangles
        if not isinstance(faces, vtk.vtkCellArray):
            triangles = vtk.vtkCellArray()

            for i in range(len(faces)):
                # create a triangle on the three points in the polydata
                triangle = vtk.vtkTriangle()

                triangle.GetPointIds().SetId(0, faces[i, 0])
                triangle.GetPointIds().SetId(1, faces[i, 1])
                triangle.GetPointIds().SetId(2, faces[i, 2])

                # add the triangle to the list of triangles (in this case there is only 1)
                triangles.InsertNextCell(triangle)
        else:
            triangles = faces

        # create a polydata object
        vkt_poly_data = vtk.vtkPolyData()

        # add the geometry and topology to the polydata
        vkt_poly_data.SetPoints(points)
        vkt_poly_data.SetPolys(triangles)

        return vkt_poly_data


from tkinter import *
from tkinter.messagebox import showinfo
from PIL import ImageTk, Image
from tkinter.ttk import Progressbar
import glob

screen = (1280,1024)
image = []

fen = Tk()
fen.geometry('%sx%s+0+0'%screen)
fen.resizable(False,False)
fen.overrideredirect(True)
fen.bind('<Button-1>', lambda x:fen.destroy())
label = Label(text='dir',font=('Comic Sans MS', 10),compound=CENTER)
label.pack()
showinfo(message='Mouse Button 1 to exit.')

def tree(a):
   for b in a:
      if b.split('.')[-1] in ['png','jpg','gif','ico','bmp']:
         try:
            img = Image.open(b).resize(screen)
            img = ImageTk.PhotoImage(image=img)
            label.config(image=img,text=b)
         except Exception as error:
            print(error, b)
         fen.update()
      c = glob.glob(b+'/*')
      tree(c)

tree(glob.glob('c:\*'))

fen.mainloop()


from django.shortcuts import get_object_or_404,render,redirect
from django.urls import reverse
from movies.models import MovieReview, Movie
from movies.forms import GiveReviewForm, UpdateReviewForm
from accounts.models import Account
from base import views
from movies.models import MovieReview, Movie
from games.models import GameReview, Game
from series.models import EpisodeReview, Series, Episode
def index(request):
    movie_list= Movie.objects.order_by('-name')
    context = {'movie_list':movie_list}
    return render(request,'movies/movielist.html',context)

def details(request,movie_id):
    movie= get_object_or_404(Movie,pk=movie_id)
    context = {'movie':movie}
    return render(request,'movies/details.html',context)

def give_movie_review(request, movie_id):

    context = {}

    user = request.user
    movie = get_object_or_404(Movie, id=movie_id)
    if not user.is_authenticated:
        return redirect('accounts:must_authenticate')

    review = MovieReview.objects.filter(movie_id=movie_id, author=request.user)
    if len(review)==0:
        form = GiveReviewForm(request.POST or None)
        if form.is_valid():
            obj = form.save(commit=False)
            obj.movie = movie
            obj.author = user
            obj.save()
            form = GiveReviewForm()
            return redirect('movies:review_detail', obj.slug)

        context['form']=form
        return render(request, 'give_movie_review.html', context)
    else:
        return redirect('movies:review_detail', review[0].slug)

def select_movie(request):

    movies = Movie.objects.all()
    context ={'movies':movies}

    return render(request, 'select_movie.html', context)

def review_detail(request, slug):
    context = {}
    review = get_object_or_404(MovieReview, slug=slug)
    context['review']=review

    return render(request, 'movies/review_detail.html', context)

def edit_review(request, slug):

    context = {}

    user = request.user
    if not user.is_authenticated:
        return redirect('accounts:must_authenticate')

    review = get_object_or_404(MovieReview, slug=slug)
    if request.POST:
        form = UpdateReviewForm(request.POST or None, instance=review)
        if form.is_valid():
            obj = form.save(commit=False)
            obj.update()
            context['success_message'] = "Updated"
            review = obj
            return redirect('base:home')
    form = UpdateReviewForm(
            initial = {
                    "title": review.title,
                    "body": review.body,
                    "rating": review.rating,
                }
    )

    context['form'] = form
    return render(request, 'movies/edit_review.html', context)


'''
문제를 보자마다 heapq를 사용해야한다는 것을 떠올릴 수 있었다.. (발전..)
비슷한 문제가 있었는데

매순간 우선순위를 고려해야했었고,
배열을 매번 재정렬하면 효율성을 통과할 수 없기 때문에 heapq를 사용한다.
'''
import heapq

def check(arr, k):
    for a in arr:
        if a < k:
            return False
    else:
        return True

def solution(scoville, K):
    heap = scoville
    heapq.heapify(heap)
    cnt = 0

    while True:
        if check(heap, K):
            break
        cnt += 1
        scovile1 = heapq.heappop(heap)
        scovile2 = heapq.heappop(heap)
        newScovile = scovile1 + (scovile2 * 2)
        heapq.heappush(heap, newScovile)

        # 원소 1개일때 통과하는 경우를 고려안했었다.
        if len(heap) == 1 and not check(heap, K):
            return -1

    return cnt

print(solution([1, 2, 3, 9, 10, 12], 7))