"""
Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number.

An example is the root-to-leaf path 1->2->3 which represents the number 123.

Find the total sum of all root-to-leaf numbers.

For example,

    1
   / \
  2   3
The root-to-leaf path 1->2 represents the number 12.
The root-to-leaf path 1->3 represents the number 13.

Return the sum = 12 + 13 = 25.
"""

# Definition for a binary tree node.

class TreeNode(object):

    def __init__(self, x):
        self.val = x
        self.left = None
        self.right = None

    def __repr__(self):
        return str(self.val)

class Solution(object):

    def sumNumbers(self, root):
        """
        :type root: TreeNode
        :rtype: int
        """
        if root is None:
            return 0

        pathSum, allPaths = 0, []

        def allPathToLeave(root, path):
            if root.left is None and root.right is None:
                allPaths.append(path)
            if root.left:
                allPathToLeave(root.left, path + [root.left])
            if root.right:
                allPathToLeave(root.right, path + [root.right])

        allPathToLeave(root, [root])

        for path in allPaths:
            numStr = ''.join([str(n) for n in path])
            num = int(numStr)
            pathSum += num

        return pathSum

a, b, c, d, e, f, g = (TreeNode(i) for i in range(1, 8))
a.left, a.right = b, c
b.left, b.right = d, e
c.left, c.right = f, g

h = TreeNode(0)

s = Solution()
r = s.sumNumbers(h)
print(r)


#=============================================
# Plot the NN and GAP data
#=============================================
import re
import os
import numpy as np
import matplotlib.pyplot as plt
import ase.io

def get_max_lims(ax_list, factor=0.04):
    """
    Get the maximum lims for the graphs

    Where:
        ax_list: Is a list of the plot/residual axis pairs
    """
    maxs0 = []
    mins1 = []
    maxs1 = []
    for ax in ax_list:
        xlim    = ax[0].get_xlim()
        ylim    = ax[0].get_ylim()
        maxs0.append(max([xlim[1], ylim[1]]))

        ylim    = ax[1].get_ylim()
        mins1.append(ylim[0])
        maxs1.append(ylim[1])

    lim0 = [-factor, max(maxs0) + factor]
    lim1 = [min(mins1) - factor, max(maxs1) + factor]
    return lim0, lim1
#---------------------------------------------

def format_plot(ax, labels, lims, fontsize=16, match_residuals=False, legend_locs=(9,3), fig_letters=("a","b")):
    """
    Format the axes object to some specific standards.

    Where:
        ax:              A subplot list of matplotlib Axes
        labels:          A dictionary for the labels of the graph:
                            title, y_label, y_label_residual, x_label
        lims:            A tuple of the maximum limits for the top/bottom graph
        fontsize:        The font size for all elements (note titles
                            are +2 larger)
        match_residuals: True makes the residual plots share a y axis
        legend_locs:     A tuple containing the legend location for the graphs
        fig_letters:     A tuple containing the letters for the figure
    """
    lim, residual_lim = lims

    #-----------------------------------------
    # Format the Graph
    #-----------------------------------------
    # Draw the lines
    ax[0].plot(lim, lim, color="k") # label="$x = y$"
    ax[1].hlines(0, lim[0], lim[1])

    # Make the top plot square
    ax[0].set_xlim(lim)
    ax[0].set_ylim(lim)

    # Display grids
    ax[0].grid()
    ax[1].grid()

    # Make sure the bottom plot uses the same limits to lign up the data points
    ax[1].set_xlim(lim)
    if match_residuals:
        ax[1].set_ylim(residual_lim)

    # Plot the legends
    ax[0].legend(loc=legend_locs[0], ncol=2, fontsize=fontsize-2, frameon=True, fancybox=True, shadow=True)
    ax[1].legend(loc=legend_locs[1],         fontsize=fontsize-2, frameon=True, fancybox=True, shadow=True)

    if "title" in labels:
        ax[0].set_title(labels["title"],             fontsize=fontsize+2)
    if "y_label" in labels:
        ax[0].set_ylabel(labels["y_label"],          fontsize=fontsize+2)
    if "y_label_residual" in labels:
        ax[1].set_ylabel(labels["y_label_residual"], fontsize=fontsize)
    if "x_label" in labels:
        ax[1].set_xlabel(labels["x_label"],          fontsize=fontsize)

    # Change the tick label size
    ax[0].yaxis.set_tick_params(labelsize=fontsize)
    ax[1].yaxis.set_tick_params(labelsize=fontsize)
    ax[1].xaxis.set_tick_params(labelsize=fontsize)

    llim, ulim = ax[1].get_ylim()
    ax2_height = ulim - llim

    # Draw the figure letter
    ax[0].text(lim[0] + (lim[1] - lim[0]) / 100, lim[1] - abs(lim[0] - lim[1])  / 20, "({})".format(fig_letters[0]), fontsize=26)
    ax[1].text(lim[0] + (lim[1] - lim[0]) / 100, ulim   - ax2_height / 11, "({})".format(fig_letters[1]), fontsize=26)
#---------------------------------------------

def files(regex):
    """
    Returns a list of lists containing filenames  (index 0) and it's associated epoch (index 1).

    Where:
        regex: Is a compiled regular expression object for locating the specific epoch(s) chosen
    """
    files_to_plot = []
    for item in sorted(os.listdir()):
        regex_match = regex.match(item)
        if regex_match:
            filename = regex_match.group(0)
            epoch    = regex_match.group(1)
            files_to_plot.append([filename, int(epoch)])
    return files_to_plot
#---------------------------------------------

def get_min_values(files_to_plot, best_epochs):
    """
    Get the minimum value for each epoch and return a list of them.

    Where:
        files_to_plot: Is the output of the files definition above
        best_epochs:   Is a list of epoch numbers to plot
    """
    list_min_values = []
    for filename, epoch in files_to_plot:
        if epoch in best_epochs:
            data  = np.loadtxt(filename, skiprows=1)
            width = len(data[0,:])

            min_values = [None] * width
            for i in range(width):
                min_values[i] = min(data[:,i])
            list_min_values.append(min_values)
    list_min_values = np.array(list_min_values)

    min_values = []
    for i in range(width):
        min_values.append(min(list_min_values[:,i]))

    return np.array(min_values)
#---------------------------------------------

def plot_NN_performance(best_epochs, ax, min_value, _type, filename, epoch):
    """
    Plot the neural network data.

    Where:
        best_epochs: Is either a number or a list of numbers corresponding to the epochs to plot
        ax:          Is a numpy aray of array of axes objects
        min_value:   A list of size data containing the miminum value of each column in data
        _type:       A string, for the legend of the plot data
    """
    # Sanitise the input of best_epochs
    if type(best_epochs) == int:
        best_epochs = [best_epochs]
    elif type(best_epochs) != list:
        raise TypeError("`best_epochs` should be an integer or a list!")

    if int(epoch) in best_epochs:
        # data: 0 = index; 1 = DFT; 2=NN
        data      = np.loadtxt(filename, skiprows=1)

        # Shift the axis to make them easier to read
        x         = data[:,1] - min_value
        y         = data[:,2] - min_value

        # Convert from Hartree -> eV
        x         = x * ase.units.Hartree
        y         = y * ase.units.Hartree

        # Main data
        ax[0].scatter(x, y, label=_type)

        # Residuals
        residuals = y - x
        ax[1].scatter(x, residuals, marker="o", label="Residuals ({})".format(_type))
#---------------------------------------------

def plot_gap(filename, label, ax, colour, cols, scale=1):
    """
    Plot the GAP data.

    Where:
        filename: Is the path to the saved numpy array containing the x (col 0) and y (col 1) data.
        label:    Is the legend label string
        ax:       Is a numpy aray of array of axes objects
        colour    Is  the colour of the plot points
        cols:     Is a tuple containing the indexes of the two columns to plot
        scale:    Is a number to divide each value by when plotting (i.e 216 atoms, to make a per
                     atom value on the axis)
    """
    x_index, y_index = cols
    # GAP data array
    # 0 = gap | 1 = DFT
    data = np.loadtxt(filename, skiprows=1, delimiter=",")

    # Shift the axis to make them easier to read
    minimum = min([min(data[:,x_index]), min(data[:,y_index])])
    # Divide by the number of atoms in the system
    x         = (data[:,x_index] - minimum) / scale
    y         = (data[:,y_index] - minimum) / scale

    # Main data
    ax[0].scatter(x, y, label=label, color=colour)

    # Residuals
    residuals = y - x
    ax[1].scatter(x, residuals, marker="o", color=colour, label="Residuals ({})".format(label))


import os
import googleapiclient.discovery

# Initialize the YouTube Data API client with an API key
def initialize_youtube_client(api_key):
    return googleapiclient.discovery.build('youtube', 'v3', developerKey=api_key)

def get_video_comments(video_id, max_results=1, api_key=None):
    # Initialize the YouTube Data API client
    if api_key is None:
        raise ValueError("API key is required")
    youtube = initialize_youtube_client(api_key)

    # Fetch comments from the video
    try:
        comments = []
        nextPageToken = None
        while len(comments) < max_results:
            kwargs = {
                'part': 'snippet',
                'videoId': video_id,
                'textFormat': 'plainText',
                'maxResults': max_results - len(comments),
                'pageToken': nextPageToken
            }
            results = youtube.commentThreads().list(**kwargs).execute()
            for item in results.get('items', []):
                comment = item['snippet']['topLevelComment']['snippet']['textDisplay']
                comments.append(comment)
            nextPageToken = results.get('nextPageToken')
            if not nextPageToken:
                break

    except Exception as e:
        # Handle any exceptions that occur during the API request
        print(f"Error fetching comments: {str(e)}")

    return comments


# -*- coding: utf-8 -*-
"""
Created on Sat Sep 14 14:47:00 2019

@author: balam
"""

import tsplib95 as tsp
from heapq import heappush, heappop
import math
import numpy as np
import copy

from display import display

class graph:

    def __init__(self, _tspP: tsp.Problem ):
        self.tspP = _tspP
        self.heapGraph = []
        self.v = self.tspP.dimension
        self.e = len(list(self.tspP.get_edges()))
        self.parent = dict()
        self.rank = dict()
        self.result = []

    def initParentRank(self):
        for i in self.tspP.get_nodes():
            self.parent[i] = i
            self.rank[i] = 0

    def initHeapGraph(self):
        self.heapGraph = []
        for edge in self.tspP.get_edges():
            n1 , n2 = edge
            dist = self.tspP.wfunc(n1,n2)
            heappush(self.heapGraph, (dist, n1, n2))

    def findGrandParent(self, node):
        if self.parent[node] == node:
            return node
        else:
            return self.findGrandParent(self.parent[node])

    def union(self, n1, n2):
        n1Gparent = self.findGrandParent(n1)
        n2Gparent = self.findGrandParent(n2)

        if self.rank[n1Gparent] > self.rank[n2Gparent]:
            self.parent[n2Gparent] = n1Gparent
        elif self.rank[n1Gparent] < self.rank[n2Gparent]:
            self.parent[n1Gparent] = n2Gparent
        else:
            self.parent[n2Gparent] = n1Gparent
            self.rank[n1Gparent] += 1

    def kruskalMSTEdges(self):
        self.result = []
        self.mstCost = math.nan
        self.initHeapGraph()
        self.initParentRank()

        while len(self.result) < self.v - 1:
            [w, n1, n2] = heappop(self.heapGraph)
            n1Gparent = self.findGrandParent(n1)
            n2Gparent = self.findGrandParent(n2)
            if n1Gparent != n2Gparent:
                self.result.append((w, n1, n2))
                self.union(n1Gparent,n2Gparent)
        self.mstCost = sum([i[0] for i in self.result])
        print("MST cost : ", self.mstCost)

    def genTree(self):
        stack = []
        self.tree = dict()
        edges = np.asarray([[n1,n2] for w,n1,n2 in self.result])
        nodes = np.asarray(list(self.parent.keys()))
        parents = np.asarray(list(self.parent.values()))
        for i in np.where((nodes == parents) == True)[0]:
            self.rootNode = nodes[i]
            stack.append(nodes[i])
            self.tree[nodes[i]] = nodes[i]
        while len(stack) > 0:
            node = stack.pop(-1)
            childEdges = np.where(edges == node)
            if childEdges[0].size > 0 :
                for childEdge in zip(childEdges[0],childEdges[1]):
                    stack.append( edges [ childEdge[0] ] [ int(not(childEdge[1])) ] )
                    self.tree[ edges [ childEdge[0] ] [ int(not(childEdge[1])) ] ] = node
                    edges[childEdge[0],:] = [-1, -1]

    def getTour_NoHeuristic(self):
        stack = []
        self.tour = []
        nodes = np.asarray(list(self.tree.keys()))
        parents = np.asarray(list(self.tree.values()))
        for i in np.where((nodes == parents) == True)[0]:
            stack.append(nodes[i])
            parents[i] = -1
        while len(stack) > 0:
            node = stack.pop(-1)
            self.tour.append(node)
            for i in np.where((parents == node) == True)[0]:
                stack.append(nodes[i])
        return self.tour

    def getTour_nChildHeuristic(self):
        stack = []
        self.tour = []
        nodes = np.asarray(list(self.tree.keys()))
        parents = np.asarray(list(self.tree.values()))
        for i in np.where((nodes == parents) == True)[0]:
            stack.append(nodes[i])
            parents[i] = -1
        while len(stack) > 0:
            node = stack.pop(-1)
            self.tour.append(node)
            childs = []
            for i in np.where((parents == node) == True)[0]:
                dist = self.tspP.wfunc(node, nodes[i])
                childs.append([dist, nodes[i]])
            for dist, child in sorted(childs, reverse = True):
                stack.append(child)
        return self.tour

    def getTour_nnHeuristic(self):
        stack = []
        self.tour = []
        nodes = np.asarray(list(self.tree.keys()))
        parents = np.asarray(list(self.tree.values()))
        for i in np.where((nodes == parents) == True)[0]:
            stack.append(nodes[i])
            parents[i] = -1
        while len(stack) > 0:
            if len(self.tour) > 0:
                cNode = self.tour[-1]
                stackParents = []
                for i in stack:
                    if self.tree[i] == i:
                        stackParents.append(-1)
                    else:
                        stackParents.append(self.tree[i])
                if np.where(stackParents == self.tree[cNode])[0].size > 0 :
                    nextNodes = []
                    for i in np.where(stackParents == self.tree[cNode])[0]:
                        nextNodes.append(stack[i])
                    nodeDistances = [[self.tspP.wfunc(i,cNode), i] for i in nextNodes]
                    nodeDistances = sorted(nodeDistances)
                    minDistNode = nodeDistances[0][1]
                    node = stack.pop(np.where(stack == minDistNode)[0][0])
                else:
                    node = stack.pop(-1)
            else:
                node = stack.pop(-1)
            self.tour.append(node)
            childs = []
            for i in np.where((parents == node) == True)[0]:
                dist = self.tspP.wfunc(node, nodes[i])
                childs.append([dist, nodes[i]])
            for dist, child in sorted(childs, reverse = True):
                stack.append(child)
        return self.tour

    def getTour_nnAtLeaf(self):
        stack = []
        self.tour = []
        nodes = np.asarray(list(self.tree.keys()))
        parents = np.asarray(list(self.tree.values()))
        for i in np.where((nodes == parents) == True)[0]:
            stack.append(nodes[i])
            parents[i] = -1
        childs = []
        while len(stack) > 0:
            if len(childs) == 0 and len(self.tour)>0:
                cNode = self.tour[-1]
                stackDist = [[self.tspP.wfunc(cNode,i), i] for i in stack]
                stackDist = sorted(stackDist)
                minDistNode = stackDist[0][1]
                node = stack.pop(np.where(stack == minDistNode)[0][0])
            else:
                node = stack.pop(-1)
            self.tour.append(node)
            childs = []
            for i in np.where((parents == node) == True)[0]:
                dist = self.tspP.wfunc(node, nodes[i])
                childs.append([dist, nodes[i]])
            for dist, child in sorted(childs, reverse = True):
                stack.append(child)

        return self.tour

    def getTour_2opt(self):
        best_length = float('inf')
        changed = True
        order = copy.deepcopy(self.tour)
        length = self.calcTourLength_(order)
        while changed:
            changed = False
            for a in range(-1, len(order)):
                for b in range(a+1, len(order)):
                    new_order = order[:a] + order[a:b][::-1] + order[b:]
                    new_length = self.calcTourLength_(new_order)
                    if new_length < length:
                        length = new_length
                        order = new_order
                        changed = True
        if length < best_length:
            best_length = length
            self.tour = order
        return self.tour

    def calcTourLength_(self, tour):
        tourLength = 0
        closedTour = []
        closedTour.extend(tour)
        closedTour.append(tour[0])
        for i in range(len(closedTour)-1):
            tourLength += self.tspP.wfunc(closedTour[i], closedTour[i+1])
        return tourLength

    def calcTourLength(self):
        tourLength = 0
        closedTour = []
        closedTour.extend(self.tour)
        closedTour.append(self.tour[0])
        for i in range(len(closedTour)-1):
            tourLength += self.tspP.wfunc(closedTour[i], closedTour[i+1])
        return tourLength

    def writeTourFile(self,filename):
        header = f'''NAME : {filename}
COMMENT : Optimal tour for {filename} (tourLength = {self.calcTourLength()})
TYPE : TOUR
DIMENSION : {len(self.tour)}
TOUR_SECTION
'''
        with open(f"{filename.split('.')[0]}.out.tour",'w+') as f:
            f.write(header)
            for i in self.tour:
                f.write(f'{i}\n')
            f.write('-1\n')
            f.write('EOF')


"""
This plots an f1-score tree for the DNN model of the project discovery/machine learning paper.
The code depends on networkx as well as matplotlib and numpy.

To run, make sure that the files
tree_level0-{1,2,3,4}, tree_level1-{1,2,3,4}, tree_level2-{1,2,3,4} and hpa_gold_standard_tree.csv is in the same folder
as this program and then run
python3 tree_plot.py

The program will output a pdf called tree_output5.pdf that will contain the result tree.
"""
import textwrap
import logging
import tree_utils
import matplotlib.colors
import csv
import networkx as nx
import matplotlib.pyplot as plt
import pickle
import collections
import numpy as np
import copy
from collections import OrderedDict

CUTOFF_VALUES = collections.defaultdict(lambda: 0.4, {
    # H0
    'Aggresome': 0.060,
    'Cell Junctions': 0.098,
    'Centrosome': 0.094,
    'Cytoplasm': 0.437,
    'Cytoskeleton (Actin filaments)': 0.134,
    'Cytoskeleton (Cytokinetic bridge)': 0.004,
    'Cytoskeleton (Intermediate filaments)': 0.085,
    'Cytoskeleton (Microtubule end)': 0.070,
    'Cytoskeleton (Microtubules)': 0.165,
    'Endoplasmic reticulum': 0.185,
    'Focal Adhesions': 0.126,
    'Golgi apparatus': 0.181,
    'Microtubule organizing center': 0.048,
    'Mitochondria': 0.264,
    'Negative': 0.306,
    'Nuclear bodies': 0.141,
    'Nuclear membrane': 0.125,
    'Nuclear speckles': 0.161,
    'Nucleoli (Fibrillar center)': 0.168,
    'Nucleoli': 0.183,
    'Nucleoplasm': 0.392,
    'Nucleus': 0.238,
    'Plasma membrane': 0.278,
    'Unspecific': 0.443,
    'Vesicles': 0.271,
    'NULL': 1.5,

    # H1
    'Cytoplasmic structures': 0.427,
    'Cytoskeleton_1': 0.213,
    'MTOC': 0.093,
    'Neg/Unspec': 0.307,
    'Nuclear bodies/speckles': 0.172,
    'Nuclear membrane_1': 0.129,
    'Nucleoli_1': 0.201,
    'Nucleus_1': 0.500,
    'Periphery': 0.282,
    'Secretory': 0.386,

    # H2
    'Cytoplasmic': 0.516,
    'Nuclear': 0.478,
    'Cytoskeleton': 0.216,
    'Unspecific/Negative': 0.296,
    })

# Hierarchy is defined as a string containing numbers
# Highest number indicates type, Nucleus:0, Cytoplasm:1, or Secretory:2
# Second highest indicates category within type
# Third indicates class within category
DEFAULT_HIERARCHY = {
        'ROOT': '',

        'Nuclear': '1',

        'Nucleus_1': '10',
        'Nucleus': '101',
        'Nucleoplasm': '102',

        'Nuclear bodies/speckles': '11',
        'Nuclear bodies': '111',
        'Nuclear bodies (many)': '112',
        'Nuclear speckles': '113',

        'Nucleoli_1': '12',
        'Nucleoli': '121',
        'Nucleoli (Fibrillar center)': '123',
        'Nucleoli (fib center)': '123',
        'Nucleoli (rim)': '124',

        'Nuclear membrane_1': '13',
        'Nuclear membrane': '131',

        'Cytoplasmic': '2',

        'Cytoplasmic structures': '20',
        'Cytoplasm': '201',
        'Aggresome': '202',
        'Mitochondria': '203',

        'Cytoskeleton_1': '21',
        'Intermediate filaments': '211',
        'Cytoskeleton (Intermediate filaments)': '211',
        'Microtubule end': '212',
        'Cytoskeleton (Microtubule end)': '212',
        'Microtubules': '213',
        'Cytoskeleton (Microtubules)': '213',
        'Actin filaments': '214',
        'Cytoskeleton (Actin filaments)': '214',
        'Cytokinetic bridge': '215',
        'Cytoskeleton (Cytokinetic bridge)': '215',
        'Cytoskeleton': '',

        'MTOC': '22',
        'Microtubule organizing center': '221',
        'Centrosome': '222',

        'Secretory': '23',
        'Endoplasmic reticulum': '231',
        'Golgi apparatus': '232',
        'Vesicles': '233',
        'Lipid droplets': '234',

        'Periphery': '24',
        'Cell Junctions': '240',
        'Focal Adhesions': '241',
        'Plasma membrane': '242',

        'Unspecific/Negative': '9',
        'Neg/Unspec': '90',
        'Negative': '901',
        'Unspecific': '902',
        'NULL': '903',

        'R&R': '',
        'CCD': '',
        'Periphery_1': '',

        'CCV': '',
        'CCV_2': '',
        ' ': '',
        }
DISP_HIERARCHY = {
        '': 'ROOT',

        '1': 'Nuclear',

        '10': 'Nucleus',
        '101': 'Nucleus',
        '102': 'Nucleoplasm',

        '11': 'Subnuclear structures',
        '111': 'Nuclear bodies',
        '112': 'Nuclear bodies (many)',
        '113': 'Nuclear speckles',

        '12': 'Nucleoli',
        '121': 'Nucleoli',
        '123': 'Nucleoli (Fibrillar center)',
        '124': 'Nucleoli (rim)',

        '13': 'Nuclear membrane',
        '131': 'Nuclear membrane',

        '2': 'Cytoplasmic',

        '20':  'Cytoplasm',
        '201': 'Cytosol',
        '202': 'Aggresome',
        '203': 'Mitochondria',

        '21': 'Cytoskeleton',
        '211': 'Intermediate filaments',
        '212': 'Microtubule end',
        '213': 'Microtubules',
        '214': 'Actin filaments',
        '215': 'Cytokinetic bridge',
        '': 'Cytoskeleton',

        '22': 'MTOC',
        '221': 'Microtubule organizing center',
        '222': 'Centrosome',

        '23': 'Secretory',
        '231': 'Endoplasmic reticulum',
        '232': 'Golgi apparatus',
        '233': 'Vesicles',
        '234': 'Lipid droplets',

        '24': 'Cell Periphery',
        '240': 'Cell Junctions',
        '241': 'Focal Adhesions',
        '242': 'Plasma membrane',

        '': 'Unspecific/Negative',
        '': 'Neg/Unspec',
        '': 'Negative',
        '': 'Unspecific',
        '': 'NULL',

        '': 'R&R',
        '': 'CCD',
        '': 'Periphery_1',

        '': 'CCV',
        '': 'CCV_2',

        }

skip_classes_in_output_l0 = {4, 11, 14}

def read_single_fov_counts_layer(preds, plates, classes, skip_classes, location_string):
    counts = collections.defaultdict(lambda: collections.defaultdict(int))
    for fov, fov_dict in preds.items():
        fov = '_'.join(fov.split('_')[:-1])
        fov_prediction = fov_dict['prediction']
        fov_prediction = [fov_prediction[oc] for oc in range(len(fov_prediction)) if oc not in skip_classes]
        fov_prediction = np.asarray(fov_prediction)

        fov_locations = set(plates[fov][location_string].split(','))
        fov_classes = set()
        for i in range(int(len(classes)/2)):
            c = classes[i]
            if c not in CUTOFF_VALUES:
                print(c, 'not in cutoff values. Using 0.4')
            if fov_prediction[i] >= CUTOFF_VALUES[c]:
                fov_classes.add(c)

        for c in fov_classes:
            code = DEFAULT_HIERARCHY[c]
            if c in fov_locations:
                counts[code]['tp'] += 1
            else:
                counts[code]['fp'] += 1

        for c in fov_locations:
            code = DEFAULT_HIERARCHY[c]
            if c not in fov_classes:
                counts[code]['fn'] += 1
    return counts

def get_if_info_classes(if_info, locations_header):
    classes = set()
    for p in if_info:
        unsplit = if_info[p][locations_header]
        split = unsplit.split(',')
        classes.update(split)
    if locations_header == 'locations_h1':
        classes.add('NULL')
    if locations_header == 'locations_h2':
        classes.add('Neg/Unspec')
    classes = sorted(classes)
    ordered_classes = collections.OrderedDict()
    for i, c in enumerate(classes):
        if isinstance(c, int):
            logging.critical('Class CANNOT be an int')
            raise ValueError('{} is int when it should be str'.format(c))

        ordered_classes[c] = i
        ordered_classes[i] = c
    return ordered_classes

def read_if_file(if_file, include={}, non_include={}, identifiers=['if_plate_id', 'position', 'sample'],
                 split_to_well=True):
    dictreader = csv.DictReader(open(if_file))
    if_info = dict()
    for line in dictreader:
        skipping = False

        for constraint in include:
            actual_line = line[constraint]
            actual_line = actual_line.split(',')

            any_in = [actual in include[constraint] for actual in actual_line]
            if not any(any_in):
                skipping = True
                break

        if skipping:
            continue

        for constraint in non_include:
            actual_line = line[constraint]
            value = actual_line.split(',')

            any_in = [actual in non_include[constraint] for actual in value]
            if any(any_in):
                skipping = True
                break

        if skipping:
            continue

        id_ = ''
        for i in identifiers:
            id_ += line[i] + '_'

        if split_to_well:
            id_ = '_'.join(id_.split('_')[:-2])
        else:
            id_ = '_'.join(id_.split('_')[:-1])
        if_info[id_] = line
    return if_info

if __name__ == '__main__':
    counts = collections.defaultdict(lambda: collections.defaultdict(int))

    print('H0')
    reader = read_if_file('hpa_gold_standard_tree.csv')
    classes = get_if_info_classes(reader, 'locations')
    for f in ['tree_level0-1', 'tree_level0-2', 'tree_level0-3', 'tree_level0-4']:
        h0_preds = pickle.load(open(f, 'rb'))
        h0_counts = read_single_fov_counts_layer(h0_preds, reader, classes, skip_classes_in_output_l0, 'locations')
        for c in h0_counts:
            counts[c]['tp'] += h0_counts[c]['tp']
            counts[c]['fp'] += h0_counts[c]['fp']
            counts[c]['fn'] += h0_counts[c]['fn']

    print('H1')
    reader = read_if_file('hpa_gold_standard_tree.csv')
    classes = get_if_info_classes(reader, 'locations_h1')
    for f in ['tree_level1-1', 'tree_level1-2', 'tree_level1-3', 'tree_level1-4']:
        h1_preds = pickle.load(open(f, 'rb'))
        h1_counts = read_single_fov_counts_layer(h1_preds, reader, classes, [], 'locations_h1')
        for c in h1_counts:
            if c == DEFAULT_HIERARCHY['Nucleus_1']:
                print(c, counts[c]['tp'])
            counts[c]['tp'] += h1_counts[c]['tp']
            counts[c]['fp'] += h1_counts[c]['fp']
            counts[c]['fn'] += h1_counts[c]['fn']

    print('H2')
    reader = read_if_file('hpa_gold_standard_tree.csv')
    classes = get_if_info_classes(reader, 'locations_h2')
    for f in ['tree_level2-1', 'tree_level2-2', 'tree_level2-3', 'tree_level2-4']:
        h2_preds = pickle.load(open(f, 'rb'))
        h2_counts = read_single_fov_counts_layer(h2_preds, reader, classes, [], 'locations_h2')
        for c in h2_counts:
            counts[c]['tp'] += h2_counts[c]['tp']
            counts[c]['fp'] += h2_counts[c]['fp']
            counts[c]['fn'] += h2_counts[c]['fn']

    precisions = {}
    recalls = {}
    lengths = {}
    f1s = {}
    print('making precision_recalls')
    for c in counts:
        tp = counts[c]['tp']
        fp = counts[c]['fp']
        fn = counts[c]['fn']
        precisions[c] = tp/((tp + fp) or 1)
        recalls[c] = tp/((tp + fn) or 1)
        lengths[c] = tp + fn
        f1s[c] = 2 * (precisions[c] * recalls[c])/(
                (precisions[c] + recalls[c]) or 1)

    G = nx.DiGraph()
    nodes = []
    node_sizes = []
    edges = []
    edge_colors = []
    node_colors = []

    labels = {k: v for (k, v) in DISP_HIERARCHY.items()}

    norm = matplotlib.colors.Normalize(0.3, 1.0)
    cmap = plt.cm.ScalarMappable(norm=norm, cmap=plt.cm.plasma)
    print('sorting')
    sort_f1s = OrderedDict(sorted(f1s.items()))
    sort_recalls = OrderedDict(sorted(recalls.items()))
    sort_precisions = OrderedDict(sorted(precisions.items()))
    sort_lengths = OrderedDict(sorted(lengths.items()))
    label_sizes = {}
    for code in sort_precisions:
        if code == '' or code == '9' or code == '90' or code == '901' or code == '902' or code == '903':
            continue
        G.add_edge(code[0:-1], code)
        edges.append((code[0:-1], code))
        edge_colors.append(plt.cm.viridis(sort_recalls[code]))

        node_sizes.append(max(300, np.sqrt(sort_lengths[code]) * 12.7))
        label_sizes[code] = node_sizes[-1]
        print(code)
        nodes.append(code)
        node_colors.append(cmap.to_rgba(sort_f1s[code]))

    pos = tree_utils.hierarchical_tree_pos(G, '')
    print(pos)
    sort_edges = sorted(edges)
    fig, ax = tree_utils.draw_hierarchical_tree(
            G, pos, nodes=nodes, node_size=node_sizes, node_color=node_colors,
            edges=sort_edges, edge_color='k', sm=cmap)
    print(node_sizes)

    labelpos = copy.deepcopy(pos)
    f_tex = "{:.2f}"
    l1 = len('(fibrillar center)')
    l2 = len('bodies/speckles')
    for c in labelpos:
        if c == '':
            continue
        originalpos = labelpos[c]
        if c in f1s:
            if f1s[c] <= 0.45:
                color = 'white'
            else:
                color = 'black'
            ax.text(originalpos[0]-0.010, originalpos[1]-0.0025,
                    f_tex.format(f1s[c]), fontsize=8, color=color)
            if len(c) > 2:
                ax.text(originalpos[0], originalpos[1]-0.032,
                        '\n'.join(textwrap.wrap(labels[c], l1)), rotation=-40, fontsize=8,
                        fontdict={'family': 'Sans-serif'})
            else:
                ax.text(originalpos[0]+0.0250, originalpos[1]-0.005,
                        '\n'.join(textwrap.wrap(labels[c], l2)), rotation=0, fontsize=8,
                        fontdict={'family': 'Sans-serif'})

    fig.savefig('tree_output5.pdf', dpi=100, transparent=True)


import numpy as np
import shapefile
from matplotlib import pyplot as pl

basefname = sys.argv[1]
sf = shapefile.Reader(basefname)

#n = len(sf.records())
#print(n)

for i in range(1000):
    s = sf.shape(i)
    p = np.array(s.points)
    pl.plot(p[:, 0], p[:, 1])

pl.show()


# This script visualizes the Auxiliary Nested PSO optimization for when two non-bonded parameters are being optimized
# Example:
# python3.6 plot_parallel_coord_nbp2.py All_scores.res All_scores.res.png 2 CH_3 CH_2

import matplotlib as mpl
from mpl_toolkits.mplot3d import Axes3D
import numpy
import pandas as pd
import matplotlib.pyplot as plt
from matplotlib import ticker
import sys

score_file = sys.argv[1]
output_figure_filename = sys.argv[2]
NS = int(sys.argv[3])
if NS < 1:
    raise ValueError("Number of site types (NS) should be an integer greater or equal to 1")
for i in range(1, NS+1):
    exec('SiteTypeName' + str(i) + '= sys.argv[3+ ' + str(i) + "]")

if NS == 1:
    cols = ['MBAR_Sig1', 'MBAR_Eps1']
    COLS = ['ref_sig1', 'ref_eps1']
    cols_COLS = [ 'MBAR_Sig1', 'MBAR_Eps1', 'ref_sig1', 'ref_eps1' ]
    ytitles = ['$\sigma_{' + SiteTypeName1 + '}$ [$\mathrm{\AA}$]', '${\epsilon/k_\mathrm{B}}_{' + SiteTypeName1 + '}$ [K]' ]

if NS == 2:
    cols = ['MBAR_Sig1', 'MBAR_Eps1', 'MBAR_Sig2', 'MBAR_Eps2']
    COLS = ['ref_sig1', 'ref_eps1', 'ref_sig2', 'ref_eps2']
    cols_COLS = [ 'MBAR_Sig1', 'MBAR_Eps1', 'MBAR_Sig2', 'MBAR_Eps2', 'ref_sig1', 'ref_eps1', 'ref_sig2', 'ref_eps2' ]
    ytitles = ['$\sigma_{' + SiteTypeName1 + '}$ [$\mathrm{\AA}$]', '${\epsilon/k_\mathrm{B}}_{' + SiteTypeName1 + '}$ [K]', '$\sigma_{' + SiteTypeName2 + '}$ [$\mathrm{\AA}$]', '${\epsilon/k_\mathrm{B}}_{' + SiteTypeName2 + '}$ [K]' ]

df = pd.read_csv(score_file, sep=' ')

NI = df['I'].max()
NP = df['P'].max()

x = [i for i, _ in enumerate(cols)]

if NP == 1:
    PCOLORS = ['red']
if NP == 2:
    PCOLORS = ['red', 'green']
if NP == 3:
    PCOLORS = ['red', 'green', 'blue']
if NP == 4:
    PCOLORS = ['red', 'green', 'blue', 'orange']
if NP == 5:
    PCOLORS = ['red', 'green', 'blue', 'orange', 'purple']
if NP == 6:
    PCOLORS = ['red', 'green', 'blue', 'orange', 'purple', 'cyan']

# Create sublots along x axis
fig, axes = plt.subplots(NI, len(x)-1, sharey=False, figsize=(5*len(x)-1, 5*NI), squeeze=0)

# Get min, max and range for each column, Normalize the data for each column
min_max_range = {}
for col in cols:
    min_max_range[col] = [df[col].min(), df[col].max(), numpy.ptp(df[col])]
    df[col] = numpy.true_divide(df[col] - df[col].min(), numpy.ptp(df[col]))
n=-1
for col in COLS:
    n = n + 1
    min_max_range[col] = [df[cols[n]].min(), df[cols[n]].max(), numpy.ptp(df[cols[n]])]
    df[col] = numpy.true_divide(df[cols[n]] - df[cols[n]].min(), numpy.ptp(df[cols[n]]))

for ITERATION in range(0, NI):
    for iax, ax in enumerate(axes[ITERATION]):
        for idx in df.index:
            I = df.iloc[idx,0]
            P = df.iloc[idx,1]
            if I == ITERATION + 1:
                y = df.loc[idx, cols]
                ax.plot(x, y, color=PCOLORS[P-1], alpha=0.15)    # plot particles

                i = df.iloc[idx,2]
                p = df.iloc[idx,3]
                if i == 1 and p == 1:
                    Y = df.loc[idx, COLS]
                    ax.plot(x, Y, color=PCOLORS[P-1], alpha=1.0, linewidth=5, dashes=[2, 5, 3, 5])   # plot PARTICLES

        ax.set_xlim([x[iax], x[iax+1]])

    # Set the tick positions and labels on y axis for each plot. Tick positions based on normalised data. Tick labels are based on original data
    def set_ticks_for_axis(dim, ax, ticks):
        min_val, max_val, val_range = min_max_range[cols[dim]]
        step = val_range / float(ticks-1)
        tick_labels = [round(min_val + step * i, 2) for i in range(ticks)]
        norm_min = df[cols[dim]].min()
        norm_range = numpy.ptp(df[cols[dim]])
        norm_step = norm_range / float(ticks-1)
        ticks = [round(norm_min + norm_step * i, 3) for i in range(ticks)]
        ax.yaxis.set_ticks(ticks)
        ax.set_yticklabels(tick_labels)

    for dim, ax in enumerate(axes[ITERATION]):
        ax.xaxis.set_major_locator(ticker.FixedLocator([dim]))
        set_ticks_for_axis(dim, ax, ticks=20)
        if ITERATION == NI - 1:
            ax.set_xticklabels([ytitles[dim]])
        else:
            ax.set_xticklabels([""])

    # Move the final axis' ticks to the right-hand side
    ax = plt.twinx(axes[ITERATION, -1])
    dim = len(axes[ITERATION])
    ax.xaxis.set_major_locator(ticker.FixedLocator([x[-2], x[-1]]))
    set_ticks_for_axis(dim, ax, ticks=20)
    if ITERATION == NI - 1:
        ax.set_xticklabels([ytitles[-2], ytitles[-1]])
    else:
        ax.set_xticklabels(["", ""])

    ax.set_ylabel("Iteration " + str(ITERATION + 1))

# Remove space between subplots
plt.subplots_adjust(wspace=0) #, hspace=0)

#plt.show()
plt.savefig(output_figure_filename)
plt.close()


from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import argparse

def write_build_info(filename, is_config_cuda, is_config_rocm, key_value_list):
  """Writes a Python that describes the build.

  Args:
    filename: filename to write to.
    is_config_cuda: Whether this build is using CUDA.
    is_config_rocm: Whether this build is using ROCm.
    key_value_list: A list of "key=value" strings that will be added to the
      module as additional fields.

  Raises:
    ValueError: If `key_value_list` includes the key "is_cuda_build", which
      would clash with one of the default fields.
  """
  module_docstring = "\"\"\"Generates a Python module containing information "
  module_docstring += "about the build.\"\"\""

  build_config_rocm_bool = "False"
  build_config_cuda_bool = "False"

  if is_config_rocm == "True":
    build_config_rocm_bool = "True"
  elif is_config_cuda == "True":
    build_config_cuda_bool = "True"

  key_value_pair_stmts = []
  if key_value_list:
    for arg in key_value_list:
      key, value = arg.split("=")
      if key == "is_cuda_build":
        raise ValueError("The key \"is_cuda_build\" cannot be passed as one of "
                         "the --key_value arguments.")
      if key == "is_rocm_build":
        raise ValueError("The key \"is_rocm_build\" cannot be passed as one of "
                         "the --key_value arguments.")
      key_value_pair_stmts.append("%s = %r" % (key, value))
  key_value_pair_content = "\n".join(key_value_pair_stmts)

  contents = """
# Copyright 2017 The TensorFlow Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
%s
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

is_rocm_build = %s
is_cuda_build = %s

%s
""" % (module_docstring, build_config_rocm_bool, build_config_cuda_bool,
       key_value_pair_content)
  open(filename, "w").write(contents)

parser = argparse.ArgumentParser(
    description="""Build info injection into the PIP package.""")

parser.add_argument(
    "--is_config_cuda",
    type=str,
    help="'True' for CUDA GPU builds, 'False' otherwise.")

parser.add_argument(
    "--is_config_rocm",
    type=str,
    help="'True' for ROCm GPU builds, 'False' otherwise.")

parser.add_argument("--raw_generate", type=str, help="Generate build_info.py")

parser.add_argument(
    "--key_value", type=str, nargs="*", help="List of key=value pairs.")

args = parser.parse_args()

if (args.raw_generate is not None) and (args.is_config_cuda is not None) and (
    args.is_config_rocm is not None):
  write_build_info(args.raw_generate, args.is_config_cuda, args.is_config_rocm,
                   args.key_value)
else:
  raise RuntimeError(
      "--raw_generate, --is_config_cuda and --is_config_rocm must be used")


"""
544 output contest matches
"""
import math

def contestMatches(n):
  if n < 2: return ""
  matches = [i for i in range(1, n+1)]
  # print(f"{matches}")
  # find the total number of iterations which is log2(n)
  for _ in range(int(math.log(n, 2))): # for each iteration, start from the left most and right most pairs until the middle
    matches = [f"({matches[i]}, {matches[len(matches)-i-1]})" for i in range(0, len(matches) // 2)]
    # print(f"matches: {matches}")
  return matches

print(contestMatches(8))
print(contestMatches(4))


import torch
from torch import nn
import torch.nn.functional as F

def load_network(device, path=None, **resnet_params):
    in_channels = resnet_params['in_channels']
    base_filters = resnet_params['base_filters']
    kernel_size = resnet_params['kernel_size']
    stride = resnet_params['stride']
    groups = resnet_params['groups']
    n_block = resnet_params['n_block']
    n_classes = resnet_params['n_classes']
    downsample_gap = resnet_params['downsample_gap']
    increasefilter_gap = resnet_params['increasefilter_gap']
    num_features = resnet_params['num_features']

    # initial a residual network
    model = ResNet(in_channels=in_channels,
                   base_filters=base_filters,
                   kernel_size=kernel_size,
                   stride=stride,
                   groups=groups,
                   n_block=n_block,
                   n_classes=n_classes,
                   downsample_gap=downsample_gap,
                   increasefilter_gap=increasefilter_gap,
                   num_features=num_features)
    model = model.to(device)

    # if exists, load the pre-trained model
    if path is not None:
        model.load_state_dict(torch.load(path, map_location=device))
        model.eval()
    return model

def test_forward_pass():
    device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")

    resnet_params = dict()
    resnet_params['num_features'] = 64
    resnet_params['in_channels'] = 1
    resnet_params['base_filters'] = 64
    resnet_params['kernel_size'] = 16
    resnet_params['stride'] = 2
    resnet_params['groups'] = 32
    resnet_params['n_block'] = 48
    resnet_params['n_classes'] = 4
    resnet_params['downsample_gap'] = 6
    resnet_params['increasefilter_gap'] = 12

    model = load_network(device, **resnet_params)

    signal_length = 6000

    no_channels = 1

    batch_size = 3
    example_source = torch.rand((batch_size, no_channels, signal_length)).to(device)

    features = model.get_features(example_source)
    print(features.size())

    result = model(example_source)
    print(result.size())

def run():
    test_forward_pass()

class Conv1dPadSame(nn.Module):
    """
    Extend nn.Conv1d to support same padding
    Code from: https://github.com/hsd1503/resnet1d/blob/master/resnet1d.py
    """
    def __init__(self, in_channels, out_channels, kernel_size, stride, groups=1):
        super(Conv1dPadSame, self).__init__()

        self.in_channels = in_channels
        self.out_channels = out_channels
        self.kernel_size = kernel_size
        self.stride = stride
        self.groups = groups
        self.conv = nn.Conv1d(in_channels=self.in_channels,
                              out_channels=self.out_channels,
                              kernel_size=self.kernel_size,
                              stride=self.stride,
                              groups=self.groups)

    def forward(self, x):

        # compute padding shape
        in_dim = x.shape[-1]
        out_dim = (in_dim + self.stride - 1) // self.stride
        pad = max(0, (out_dim - 1) * self.stride + self.kernel_size - in_dim)
        pad_left = pad // 2
        pad_right = pad - pad_left

        output = F.pad(x, (pad_left, pad_right), mode="constant", value=0)
        output = self.conv(output)

        return output

class MaxPool1dPadSame(nn.Module):
    """
    Extend nn.MaxPool1d to support same padding
    Code from: https://github.com/hsd1503/resnet1d/blob/master/resnet1d.py
    """
    def __init__(self, kernel_size):
        super(MaxPool1dPadSame, self).__init__()
        self.kernel_size = kernel_size
        self.stride = 1
        self.max_pool = nn.MaxPool1d(kernel_size=self.kernel_size)

    def forward(self, x):

        # compute pad shape
        in_dim = x.shape[-1]
        out_dim = (in_dim + self.stride - 1) // self.stride
        pad = max(0, (out_dim - 1) * self.stride + self.kernel_size - in_dim)
        pad_left = pad // 2
        pad_right = pad - pad_left

        output = F.pad(x, (pad_left, pad_right), mode="constant", value=0)
        output = self.max_pool(output)

        return output

class ResidualBlock(nn.Module):
    """
    First residual block: Conv -> BN -> ReLU -> Dropout -> Conv
    Rest residual block:  BN -> ReLU -> Dropout -> Conv -> BN -> ReLU -> Dropout -> Conv
    Code from: https://github.com/hsd1503/resnet1d/blob/master/resnet1d.py
    """
    def __init__(self, in_channels, out_channels, kernel_size, downsample, stride, groups, is_first_block=False):
        super(ResidualBlock, self).__init__()

        self.in_channels = in_channels
        self.out_channels = out_channels
        self.kernel_size = kernel_size

        self.downsample = downsample  # bool
        self.stride = stride if downsample else 1

        self.groups = groups

        self.is_first_block = is_first_block

        # first convolutional layer
        self.bn1 = nn.BatchNorm1d(in_channels)
        self.relu1 = nn.ReLU()
        self.dropout1 = nn.Dropout(p=0.5)
        self.conv1 = Conv1dPadSame(in_channels=self.in_channels,
                                   out_channels=self.out_channels,
                                   kernel_size=self.kernel_size,
                                   stride=self.stride,
                                   groups=self.groups)

        # second convolutional layer
        self.bn2 = nn.BatchNorm1d(self.out_channels)
        self.relu2 = nn.ReLU()
        self.dropout2 = nn.Dropout(p=0.5)
        self.conv2 = Conv1dPadSame(in_channels=self.out_channels,
                                   out_channels=self.out_channels,
                                   kernel_size=self.kernel_size,
                                   stride=1,
                                   groups=self.groups)

        self.max_pool = MaxPool1dPadSame(kernel_size=self.stride)

    def forward(self, x):

        shortcut = x
        output = x

        if not self.is_first_block:
            output = self.bn1(output)
            output = self.relu1(output)
            output = self.dropout1(output)
        output = self.conv1(output)

        output = self.bn2(output)
        output = self.relu2(output)
        output = self.dropout2(output)
        output = self.conv2(output)

        # if downsample, also downsample shortcut
        if self.downsample:
            shortcut = self.max_pool(shortcut)

        # if expand channel, also pad zeros to shortcut
        if self.out_channels != self.in_channels:
            shortcut = shortcut.transpose(-1, -2)
            channel_1 = (self.out_channels - self.in_channels) // 2
            channel_2 = self.out_channels - self.in_channels - channel_1
            shortcut = F.pad(shortcut, (channel_1, channel_2), mode="constant", value=0)
            shortcut = shortcut.transpose(-1, -2)

        output += shortcut

        return output

class ResNet(nn.Module):
    """
    Input:
        X: (n_samples, n_channel, n_length)
        Y: (n_samples)

    Output:
        output: (n_samples, n_classes)

    Pararmetes:
        in_channels: dim of input, the same as n_channel
        base_filters: number of filters in the first several Conv layer, it will double at every 4 layers
        kernel_size: width of kernel
        stride: stride of kernel moving
        groups: set larget to 1 as ResNeXt
        n_block: number of blocks
        n_classes: number of classes

    Code from: https://github.com/hsd1503/resnet1d/blob/master/resnet1d.py (modify the last several layers)
    """
    def __init__(self, in_channels, base_filters, kernel_size, stride, groups,
                 n_block, n_classes=4, downsample_gap=2, increasefilter_gap=4, num_features=32, verbose=False):
        super(ResNet, self).__init__()

        self.kernel_size = kernel_size
        self.stride = stride
        self.groups = groups
        self.n_block = n_block
        self.num_features = num_features
        self.verbose = verbose

        self.downsample_gap = downsample_gap  # 2 for base model
        self.increasefilter_gap = increasefilter_gap  # 4 for base model

        # first block
        self.first_block_conv = Conv1dPadSame(in_channels=in_channels,
                                              out_channels=base_filters,
                                              kernel_size=kernel_size,
                                              stride=1)
        self.first_block_bn = nn.BatchNorm1d(base_filters)
        self.first_block_relu = nn.ReLU()

        out_channels = base_filters

        # residual blocks
        self.residualblock_list = nn.ModuleList()
        for i_block in range(self.n_block):
            is_first_block = True if i_block == 0 else False

            # downsample at every <self.downsample_gap> blocks
            downsample = True if i_block % self.downsample_gap == 1 else False

            if is_first_block:
                in_channels = base_filters
                out_channels = in_channels
            else:
                # increase filters at every <self.increasefilter_gap> blocks
                in_channels = int(base_filters*2**((i_block-1)//self.increasefilter_gap))
                if (i_block % self.increasefilter_gap == 0) and (i_block != 0):
                    out_channels = in_channels * 2
                else:
                    out_channels = in_channels

            res_block = ResidualBlock(in_channels=in_channels,
                                      out_channels=out_channels,
                                      kernel_size=self.kernel_size,
                                      stride=self.stride,
                                      groups=self.groups,
                                      downsample=downsample,
                                      is_first_block=is_first_block)
            self.residualblock_list.append(res_block)

        # feature extraction
        self.feature_bn = nn.BatchNorm1d(out_channels)
        self.feature_relu = nn.ReLU()
        self.feature_dropout = nn.Dropout(p=0.5)
        self.feature_layer = nn.Linear(out_channels, self.num_features)
        self.feature_sigmoid = nn.Sigmoid()

        # final prediction
        self.final_dropout = nn.Dropout(p=0.5)
        self.final_dense = nn.Linear(self.num_features, n_classes)
        self.final_softmax = nn.Softmax(dim=1)

    def get_features(self, x):

        output = x

        # first block
        if self.verbose:
            print("Input shape:", output.shape)
        output = self.first_block_conv(output)
        if self.verbose:
            print("After first conv:", output.shape)
        output = self.first_block_bn(output)
        output = self.first_block_relu(output)

        # residual blocks, every block has two conv
        for i_block in range(self.n_block):
            res_block = self.residualblock_list[i_block]
            if self.verbose:
                print("i_block: {0}, in_channels: {1}, out_channels: {2}, downsample: {3}".format(
                    i_block, res_block.in_channels, res_block.out_channels, res_block.downsample))
            output = res_block(output)
            if self.verbose:
                print(output.shape)

        # feature extraction
        output = self.feature_bn(output)
        output = self.feature_relu(output)
        output = output.mean(-1)
        if self.verbose:
            print('Final pooling', output.shape)
        output = self.feature_dropout(output)
        output = self.feature_sigmoid(self.feature_layer(output))
        if self.verbose:
            print('Feature shape', output.shape)

        return output

    def forward(self, x):

        # feature extraction
        output = self.get_features(x)

        # final prediction
        output = self.final_dropout(output)
        output = self.final_dense(output)
        if self.verbose:
            print("Dense: ", output.shape)
        output = self.final_softmax(output)
        if self.verbose:
            print('Softmax', output.shape)

        return output

if __name__ == "__main__":
    run()


import cv2
import numpy as np

ref_path = 'Q:/minami/20230913_Ali/Module0/sensor-3/IMAGE00_AREA-1/png/L0_VX0000_VY0001/L0_VX0000_VY0001_1.png'

ref_img = cv2.imread(ref_path, 0)
ref_binary = cv2.adaptiveThreshold(ref_img, 1, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 15, 14)
ref_num = np.count_nonzero(ref_binary)
print(ref_num)

nog_list = []
for i in range(64):
    com_path = 'Q:/minami/20230913_Ali/20230913_oneshot/IMAGE/00_03/ImageFilterWithStream_GPU_0_00000000_0_{:03}.bmp'.format(i)
    com_img = cv2.imread(com_path, 0)
    com_binary = cv2.adaptiveThreshold(com_img, 1, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 15, 14)
    tmp = np.count_nonzero(com_binary)
    nog_list.append(tmp)

print(nog_list)


from django.conf import settings
from django.conf.urls.static import static
from django.urls import path
from .views import *

app_name = 'recipes'
urlpatterns = [
    path('recipes/', RecipeListView.as_view(), name='recipes'),
    path('recipes/<int:pk>', RecipeDetailsView.as_view(), name='recipe-single'),
    path('recipes/<int:pk>/update', RecipeUpdateView.as_view(), name='recipe-update'),
    path('recipes/<int:pk>/steps/update', RecipeInstructionUpdateView.as_view(), name='recipe-steps-update'),
    path('recipes/<int:pk>/ingredient/update', RecipeIngredientsUpdateView.as_view(), name='recipe-ingredient-update'),
    path('recipes/<int:pk>/delete', RecipeDeleteView.as_view(), name='recipe-delete'),
    path('recipes/<int:pk>/pdf/', ExportToPDFView.as_view(), name='recipe-pdf'),
    path('recipes/<int:id>/addcomment', AddCommentView, name='recipe-comment'),
    path('recipes/<int:id>/bookmark', BookmarkRecipeView, name='recipe-bookmark'),
    path('submit-recipe/', SubmitRecipeView, name='submit-recipe'),
    path('recipe-index/', RecipeIndexView, name='recipe-index'),
    path("search", SearchRecipes, name='search'),
]


name = input("Enter your name: ")
#name = input("enter your name: ")
age= int(input("Enter your age: "))

print("hello " + name + "! you are " + str(age) + "years old")

# # Input from the user
# age = int(input("Enter your age: "))  # Converting the input to an integer

# # Output to the console
# print("Hello, " + name + "! You are " + str(age) + " years old.")

a = int(input("enter the nubmer a as value: "))
b = int(input("enter the nubmer b as value: "))

print("the sum is", a+b)
print("the product is", a*b)

a = 5
b = 10
result = a + b
print(result)  # Output: 15

a = 20
b = 7
result = a - b
print(result)  # Output: 13

a = 3
b = 4
result = a * b
print(result)  # Output: 12

a = 15
b = 3
result = a / b
print(result)  # Output: 5.0 (result is a float)

a = 15
b = 4
result = a // b
print(result)  # Output: 3 (result is an integer, rounded down)

a = 17
b = 5
result = a % b
print(result)  # Output: 2 (remainder of 17 divided by 5)

a = 2
b = 3
result = a ** b
print(result)  # Output: 8 (2 raised to the power of 3)

# Calculate the area of a rectangle
length = 5
width = 3
area = length * width
print("Area of the rectangle:", area)

# Calculate the volume of a cube
side_length = 4
volume = side_length ** 3
print("Volume of the cube:", volume)

# Convert temperature from Celsius to Fahrenheit
celsius = 30
fahrenheit = (celsius * 9/5) + 32
print("Temperature in Fahrenheit:", fahrenheit)


from django import forms

from . import models

class StatusForm(forms.ModelForm):

    message = forms.CharField(widget=forms.Textarea)

    def __init__(self, *args, **kwargs):
        self.user = kwargs.pop('user')
        super().__init__(*args, **kwargs)

    def save(self, commit=True):
        instance = super().save(commit=False)
        instance.user = self.user
        if commit:
            instance.save()
        return instance

    class Meta:
        model = models.Status
        fields = ['message']


from turtle import title
import streamlit as st
import numpy as np
import pandas as pd
import plotly.express as px
import matplotlib.pyplot as plt
from utils import process_data
import altair as alt

def plot_position_loc_stats(daily_ped_datas: np.ndarray):
    th_10 = [80- data[2] - data[3] - data[4] for data in daily_ped_datas]
    dates = process_data.convert_date_format(daily_ped_datas[:, 0])

    ped_data_frame = pd.DataFrame({
        'dates': dates,
        'threshold_010': th_10,
        'threshold_030': list(daily_ped_datas[:, 2]),
        'threshold_050': list(daily_ped_datas[:, 3]),
        'threshold_100': list(daily_ped_datas[:, 4]),
    })

    ped_line_data_frame = pd.DataFrame({
        'dates': dates,
        'data_counts': list(daily_ped_datas[:, 1]),
    })

    line = alt.Chart(ped_line_data_frame).mark_line(color='red').encode(
        x='dates:T',
        y=alt.Y('data_counts:Q', title='Data Counts')
    )

    melted_data = pd.melt(ped_data_frame, id_vars=['dates'], value_vars=['threshold_010', 'threshold_030', 'threshold_050', 'threshold_100'], var_name='threshold')

    color_scale = alt.Scale(domain=['threshold_010', 'threshold_030', 'threshold_050', 'threshold_100'],
        range=['lightskyblue', 'lightgreen', 'lightsalmon', 'tomato'])

    bar = alt.Chart(melted_data).mark_bar(size=2000/len(melted_data)).encode(
        x=alt.X('dates:T', sort=dates),
        y=alt.Y('value:Q', title='Location Difference', scale=alt.Scale(domain=[0, 20])),
        color=alt.Color('threshold:N', scale=color_scale),
        tooltip=['value', 'threshold']
    ).properties(
        width=alt.Step(0),
        height=500
    ).interactive().properties(
        title=alt.TitleParams(text="Location Difference Chart", fontSize=20)
    )

    layered_chart = alt.layer(bar, line).resolve_scale(
        y='independent'
    ).properties(width=0)

    st.altair_chart(layered_chart, use_container_width=True) # type: ignore

def plot_daily_ttff(daily_tf_datas: np.ndarray, height: int) -> None:
    dates = [item[0] for item in daily_tf_datas]
    avg_time = [item[1] for item in daily_tf_datas]
    ttff_data_frame = pd.DataFrame({
        'dates': process_data.convert_date_format(np.array(dates)),
        'avg stabilization time': avg_time,
    })
    st.line_chart(ttff_data_frame, x='dates', y='avg stabilization time', height=height, use_container_width=True)

def scatter_avg_ttff(daily_tf_datas: tuple):
    avg_time = [item[1] for item in daily_tf_datas]
    dates = [item[0] for item in daily_tf_datas]
    dates = process_data.convert_date_format(np.array(dates))

    ttff_data_frame = pd.DataFrame({
        'dates': dates,
        'avg time to first fix': avg_time,
    })

    dot_chart = alt.Chart(ttff_data_frame).mark_circle(size=45,
        color='blue'
    ).encode(
        x=alt.X('dates:T', sort=dates),
        y='avg time to first fix:Q'
    ).interactive().properties(
        title=alt.TitleParams(text="Time To First Fix Chart", fontSize=20)
    )

    st.altair_chart(dot_chart, use_container_width=True) # type: ignore

def scatter_avg_ltt(daily_ltt_datas: tuple):
    avg_time = [item[1]/1000 for item in daily_ltt_datas]
    dates = [item[0] for item in daily_ltt_datas]
    dates = process_data.convert_date_format(np.array(dates))

    ltt_data_frame = pd.DataFrame({
        'dates': dates,
        'avg location track time': avg_time,
    })

    line_chart = alt.Chart(ltt_data_frame).mark_line(size=5,
        color='blue'
    ).encode(
        x=alt.X('dates:T', sort=dates),
        y='avg location track time:Q'
    ).interactive().properties(
        title=alt.TitleParams(text="Location Tracking Time", fontSize=20)
    )

    st.altair_chart(line_chart, use_container_width=True) # type: ignore
import datetime
def one_day_ltt(daily_ltt_datas: tuple):
    q_50, q_90, q_95 = daily_ltt_datas[0][2], daily_ltt_datas[0][3], daily_ltt_datas[0][4]

    # 각 원소를 1000으로 나눈 값으로 업데이트
    q_50 = [x / 1000 for x in q_50]
    q_90 = [x / 1000 for x in q_90]
    q_95 = [x / 1000 for x in q_95]

    dates = daily_ltt_datas[0][0]

    # numpy_str = np.array(['2023-08-15'], dtype=np.str_)

    # datetime_obj = datetime.datetime.strptime(numpy_str[0], '%Y-%m-%d')
    # formatted_date = datetime_obj.strftime('%m-%d')
    dates = []
    for i in range(len(q_95)):
        dates.append(str(i))

    ltt_data_frame = pd.DataFrame({
        'dates': dates,
        'quant_50': q_50,
        'quant_90': q_90,
        'quant_95': q_95
    })

    # 데이터를 'long' 형태로 변환하여 모든 퀀타일 값을 하나의 열로 모읍니다.
    ltt_data_frame_long = ltt_data_frame.melt(id_vars=['dates'], value_vars=['quant_50', 'quant_90', 'quant_95'])

    line_chart = alt.Chart(ltt_data_frame_long).mark_line(size=5).encode(
        x=alt.X('dates:T'),
        y=alt.Y('value:Q', title='Avg Location Track Time'),
        color=alt.Color('variable:N', title='Quantile', scale=alt.Scale(domain=['quant_50', 'quant_90', 'quant_95'], range=['blue', 'green', 'red']))
    ).properties(
        title=alt.TitleParams(text="Location Tracking Time", fontSize=20)
    )

    st.altair_chart(line_chart, use_container_width=True)


import numpy as np
from util.approximate import WeightedApproximator
from util.math import abs_diff

# Construct an approximation algorithm that only returns the average
# of the fit points.
class Average(WeightedApproximator):
    def _fit(self, points):
        self.points = points
    def _predict(self, points):
        indices = [list(range(len(self.points)))] * len(points)
        w = 1 / len(self.points)
        weights = [[w]*len(self.points)] * len(points)
        return np.array(indices), np.array(weights)

# ===========================================
#      Simple Nearest Neighbor Regressor
# ===========================================

# Class for computing an interpolation between the nearest n neighbors
class NearestNeighbor(WeightedApproximator):
    def __init__(self, k=1, method=Average, **auto_kwargs):
        self.points = None
        self.method = method
        self.num_neighbors = k
        self.auto_kwargs = auto_kwargs

    # Use fortran code to compute the boxes for the given data
    def _fit(self, control_points, k=None, display=True, **kwargs):
        # from balltree import BallTree
        from sklearn.neighbors import BallTree
        if (k is not None): self.num_neighbors=k
        # Process and store local information
        self.points = control_points.copy()
        self.tree = BallTree(self.points)
        # Update the associated 'y' values if they exist (since points
        # were shuffled on the construction of the tree).
        if (self.y is not None) and hasattr(self.tree, "index_mapping"):
            self.y = [self.y[i] for i in self.tree.index_mapping]
        # Automatically select the value for "k" if appropriate and
        # the response values are available for the points.
        if (self.num_neighbors is None):
            if (self.y is not None):
                self.auto_kwargs.update(kwargs)
                # If "mean" was not provided, pick based on problem type.
                if "mean" not in self.auto_kwargs:
                    self.auto_kwargs["mean"] = not self.classifier
                # Begin the estimation of best value for 'k'.
                end = ("\n" if display else "\r")
                print("Nearest neighbor, estimating best value for 'k'..", end=end, flush=True)
                self.num_neighbors = auto(self.points, self.y, **self.auto_kwargs)
                print(f"  chose k = {self.num_neighbors}", end=end, flush=True)
                print("                                                 ", end=end, flush=True)
            else:
                self.num_neighbors = 1

    # Function that returns the indices of points and the weights that
    # should be used to make associated predictions for each point in
    # "points".
    def _predict(self, points, max_size=2**20, display=True):
        if display: import time
        # Body
        pts = []
        wts = []
        if display:
            update = end = ""
            start = time.time()
        # Cycle through points and make prediction.
        for i,pt in enumerate(points):
            if display and (time.time() - start > 1):
                update = "\b"*len(end)
                end = f" nearest neighbor predicting {i+1} of {len(points)}.."
                print(update, end=end, flush=True)
                start = time.time()
            closest = self.tree.query(pt[None,:], k=self.num_neighbors,
                                      return_distance=False)[0]
            # Use another weighted approximator to handle final prediction.
            model = self.method()
            model.fit(self.points[closest])
            guess_pts, guess_wts = model.predict(pt)
            # Return the indices and weights associated with each.
            pts.append( closest[guess_pts] )
            wts.append( guess_wts )
        if display: print("\b"*len(end), end="", flush=True)
        # Convert into array form
        pts = np.array(pts)
        wts = np.array(wts)
        # Return the appropriate shaped pair of points and weights
        return (pts, wts)

# Automatically select the value for "k" for using nearest neighbor
# based on a random sample of the points provided.
def auto(points, values, metric=abs_diff, max_k=None, samples=100,
         mean=True, k_step=1, model=NearestNeighbor, display=False):
    from util.random import random_range
    # Make the maximum value for "k" the nearest power of 2 that
    # contains less than or equal to half of the provided data.
    if (max_k is None):
        from math import floor, log2
        max_k = 2**floor(log2(len(points)//2))
    # Compute up to 'max_k' nearest neighbors about selected points.
    # Add "+1" to exclude the current active point as a neighbor.
    model = NearestNeighbor(k=max_k+1)
    model.fit(points)
    # Randomly pick a set of points as the "checks".
    indices = [i for i in random_range(len(points), count=samples)]
    neighbors = np.array([i[1:] for (i,w) in model(points[indices])])
    differences = np.array([[metric(values[i1], values[i2]) for i2 in neighbors[i]]
                            for i,i1 in enumerate(indices)])
    k_values = {}
    # Pick the function for identifying the best selection of "k".
    for k_pow in range(0, int(log2(max_k))+1, k_step):
        k = 2**k_pow
        if mean: k_values[k] = np.mean(differences[:,:k])
        else:    k_values[k] = np.mean(np.min(differences[:,:k], axis=1))
    if (2**k_pow != max_k):
        k = 2**int(log2(max_k))
        if mean: k_values[k] = np.mean(differences[:,:k])
        else:    k_values[k] = np.mean(np.min(differences[:,:k], axis=1))
    # Find the k with the lowest mean error.
    best_k = min(k_values.items(), key=lambda i: i[1])[0]
    if display:
        name = "mean" if mean else "minimum"
        from math import log10, ceil
        print('-'*52)
        print(" Estimated "+name+" error for various choices of 'k':")
        for k in sorted(k_values):
            extra = "  <-- chosen 'k'" if k == best_k else ""
            print(f"  k = {k:{ceil(log10(max_k))}d} ~ {k_values[k]:.4e}"+extra)
        print('-'*52)
    # Return the "k" with the minimum mean difference
    return best_k

if __name__ == "__main__":

    # np.random.seed(0)

    # pts = np.random.random(size=(10000,5))
    # values = np.random.random(size=(10000,))
    # test = np.random.random(size=(1000,5))
    # print("Fitting model..", flush=True)
    # m = NearestNeighbor()
    # m.fit(pts, values)
    # print("Evaluating model..", flush=True)
    # m(test)
    # print("done")
    # exit()

    TEST_AUTO = True
    if TEST_AUTO:
        d = 10
        n = 511
        points = np.random.random(size=(n,d))
        values = np.random.random(size=(n,d))
        k = auto(points, values, display=True)

    # from util.approximate import condition
    # m = condition(NearestNeighbor, method="PLR")()
    # m = condition(NearestNeighbor, method="MPCA", dim=1)()
    # m = condition(NearestNeighbor, method="MPCA", dim=2)()
    # m = condition(NearestNeighbor, method="PCA", dim=2)()
    # m = NearestNeighbor()
    from util.approximate.testing import test_plot
    # p,x,y = test_plot(m, N=30, fun=lambda x: x[0]**3, plot_points=4000)
    p,x,y = test_plot(m, N=300)
    p.show()


from collections import namedtuple
from typing import Tuple
from enum import Enum

def main():
    list = [1, 2, 3, 4, 5]
    dict = {"one": 1, "two": 2}
    sets = set([2, 3, 4, 2])
    bool = True or False
    str = "nim_is_awesome"
    integer: int = 1
    floats: float = 1.2
    PI = 3.14

    Point: Tuple[int, int] = namedtuple("Point", ["x", "y"])
    p = Point(x=11, y=22)

    class Person:
        def __init__(self, name: str, age: int):
            self.name: str = name
            self.age: str = age

    person = Person(name="James", age=32)

    class Color(Enum):
        RED = 1
        GREEN = 2

if __name__ == "__main__":
    main()


import random
import time

nahodne_cislo = []
zadane_cislo = []

def main():
    print("Ahoj, vitej ve hre bulls and cows!")
    print("Generuji nahodne ctyrciferne cislo (muze zacinat nulou).")
    vytvor_nahodne_cislo()
    t0 = time.time()
    pokus = 0
    while nahodne_cislo != zadane_cislo:
        pokus += 1
        print(16 * "=")
        print("Pokus cislo: {} .".format(pokus))
        print(16 * "=")
        zadane_cislo.clear()
        zadej_cislo()
        print(41 * "=")
        porovnej_cisla(nahodne_cislo, zadane_cislo)
        print(41 * "=")
        if nahodne_cislo == zadane_cislo:
            break
        ukonceni = input("Pro ukonceni zadej 'exit',pro pokracovani zadej cokoli jineho a stiskni ENTER : ")
        if ukonceni == 'exit':
            break
        else:
            continue
    if zadane_cislo == nahodne_cislo:
        t1 = time.time() - t0
        casy.append(t1)
        pokusy.append(pokus)
        print("Vyborne, uhodl jsi na pocet pokusu: {} , za cas {} vterin.".format(pokus, t1))
    else:
        t1 = time.time() - t0
        casy.append(-t1)
        pokusy.append(-pokus)
        print("Nevadi, snad nekdy priste.")

def vytvor_nahodne_cislo():
    while len(nahodne_cislo) < 4:
        x = random.randint(0, 9)
        if x not in nahodne_cislo:
            nahodne_cislo.append(x)

def zadej_cislo():
    cislo_str = input("Vloz ctyrciferne cislo! : ")
    while len(cislo_str) != 4 or cislo_str.isalpha():
        cislo_str = input("Neplatne zadani! Vloz ctyrciferne cislo! : ")
    for i in range(4):
        zadane_cislo.append(int(cislo_str[i]))

def porovnej_cisla(nahodne_cislo, zadane_cislo):
    bulls = 0
    dohromady = 0
    for i in range(0, 4):
        if nahodne_cislo[i] == zadane_cislo[i]:
            bulls += 1
    for i in nahodne_cislo:
        if i in zadane_cislo:
            dohromady += 1
    cows = dohromady - bulls
    print("Trefil jsi pocet BULLS: {} a pocet COWS: {} ".format(bulls, cows))

pokusy = []
casy = []

while True:
    nahodne_cislo.clear()
    zadane_cislo.clear()
    main()
    konec_hry = input("Pro UKONCENI zadej 'exit', pro NOVOU HRU zadej cokoli : ")
    if konec_hry == 'exit':
        break
    else:
        continue
print("Znamenko minus [-] indikuje NEDOHRANOU hru!")
print("POCET pokusu v jednotlivych hrach: {}.".format(pokusy))
print("CASY jednotlivych her [vteriny]: {}.".format(casy))
print("Ukoncuji, nashledanou...")


import shutil
import time

import numpy as np
import seaborn as sns
import torch
import torch.nn as nn
import torch.nn.functional as F
from scipy.optimize import linear_sum_assignment
from scipy.special import softmax
from sklearn.metrics import confusion_matrix, accuracy_score, f1_score, \
    classification_report
from torch.autograd import Variable

import matplotlib as mpl
import matplotlib.pyplot as plt
from matplotlib import cm
from matplotlib.lines import Line2D

columns = shutil.get_terminal_size().columns
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")

def train_model(model, train_loader, valid_loader, epochs, optimizer, criterion, scheduler=None,
                clip=-1):
    """
    Only works for classifier
    :param model:
    :param train_loader:
    :param valid_loader:
    :param epochs:
    :param optimizer:
    :param criterion:
    :param scheduler:
    :param clip:
    :return:
    """

    avg_train_loss = []
    avg_valid_loss = []
    avg_train_acc = []
    avg_valid_acc = []

    # Set model to train
    model.train()

    try:
        for idx_epoch in range(1, epochs + 1):
            epochstart = time.time()
            train_loss = []
            train_acc = []

            model.train()
            for idx_batch, (data, target) in enumerate(train_loader):
                data = data.to(device)
                target = target.to(device)
                batch_size = data.size(0)

                # Forward
                optimizer.zero_grad()
                yhat = model(data)

                loss = criterion(yhat, target)

                # Backward
                loss.backward()

                # Gradient clip
                if clip > 0:
                    torch.nn.utils.clip_grad_norm_(model.parameters(), clip)
                optimizer.step()

                train_loss.append(loss.data.item())

                proba = F.softmax(yhat, dim=1)
                train_acc.append((torch.argmax(proba, dim=1) == target).sum().item() / batch_size)

            if scheduler is not None:
                scheduler.step()
            # Validate
            valid_loss, valid_acc = eval_model(model, valid_loader, criterion)

            # calculate average loss over an epoch
            train_loss_epoch = np.average(train_loss)
            avg_train_loss.append(train_loss_epoch)
            avg_valid_loss.append(valid_loss)

            train_acc_epoch = 100 * np.average(train_acc)

            avg_train_acc.append(train_acc_epoch)
            avg_valid_acc.append(valid_acc)

            print(
                'Epoch [{}/{}], Time:{:.3f} - TrAcc:{:.3f} - ValAcc:{:.3f} - TrLoss:{:.5f} - ValLoss:{:.5f} - lr:{:.5f}'
                    .format(idx_epoch, epochs, time.time() - epochstart, train_acc_epoch,
                            valid_acc, train_loss_epoch, valid_loss, optimizer.param_groups[0]['lr']))

    except KeyboardInterrupt:
        print('*' * shutil.get_terminal_size().columns)
        print('Exiting from training early')

    return model

def eval_model(model, valid_loader, criterion):
    valid_loss = []
    valid_acc = []

    with torch.no_grad():
        model.eval()
        for data, target in valid_loader:
            data = data.to(device)
            target = target.to(device)
            batch_size = data.size(0)

            yhat = model(data)
            pred = torch.argmax(F.softmax(yhat, dim=1), dim=1)

            loss = criterion(yhat, target)
            valid_loss.append(loss.data.item())
            valid_acc.append((pred == target).sum().item() / batch_size)
    return np.array(valid_loss).mean(), 100 * np.average(valid_acc)

def predict(model, test_data):
    prediction = []
    with torch.no_grad():
        model.eval()
        for data in test_data:
            data = data[0]
            data = data.float().to(device)
            output = model(data)
            prediction.append(output.cpu().numpy())

    prediction = np.concatenate(prediction, axis=0)
    return prediction

def evaluate_model(model, data_loader, y_true, datatype):
    print(f'{datatype} score')
    results_dict = dict()

    yhat = predict(model, data_loader)

    y_hat_proba = softmax(yhat, axis=1)
    y_hat_labels = np.argmax(y_hat_proba, axis=1)

    accuracy = accuracy_score(y_true, y_hat_labels)
    f1_weighted = f1_score(y_true, y_hat_labels, average='weighted')

    #cm = confusion_matrix(y_true, y_hat_labels)
    #cm_norm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]

    report = classification_report(y_true, y_hat_labels)
    print(report)

    results_dict['acc'] = accuracy
    results_dict['f1_weighted'] = f1_weighted
    return results_dict


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

# 예제 내용
# QWizard 사용 예제

import sys

from PyQt5.QtWidgets import QWizard
from PyQt5.QtWidgets import QWizardPage
from PyQt5.QtWidgets import QApplication
from PyQt5.QtCore import Qt

__author__ = "Deokyu Lim <hong18s@gmail.com>"

class Start(QWizardPage):
    def __init__(self):
        QWizardPage.__init__(self)
        self.setTitle("Start Page")
        self.setSubTitle("Sub Start Page")

class Form(QWizard):
    def __init__(self):
        QWizard.__init__(self, flags=Qt.Widget)
        self.init_widget()
        self.init_pages()

    def init_pages(self):
        self.addPage(Start())

    def init_widget(self):
        self.setWindowTitle("Hello World")

if __name__ == "__main__":
    app = QApplication(sys.argv)
    form = Form()
    form.show()
    exit(app.exec_())


import numpy as np
import matplotlib.pyplot as plt

data=np.loadtxt('results2.dat')
x=data[:,0]
theta=data[:,1]
t=data[:,2]
v=data[:,3]

nx=100

for i in range(0,8):
    #Import from results.dat file for every 20 steps to account for all space at each time step
    x=data[i*nx:(i+1)*nx,0]
    theta=data[i*nx:(i+1)*nx,1]
    t=data[i*nx:(i+1)*nx,2]
    v=data[i*nx:(i+1)*nx,3]
    p=str(i*0.2)
    plt.plot(x,v,label="timestep is : "+p )
    plt.legend(loc='best')

plt.plot(x,v)
plt.show()


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

import time
import numpy as np
from numpy import genfromtxt
import matplotlib.pyplot as plt
#from matplotlib.pyplot import imread
import random
#from PIL import Image
#import cv2

#--------------------------------
#All csvs
#--------------------------------
#map_centre='centre.csv'
map_player='player.csv'
map_otherp='otherplayers.csv'
map_loot='loot.csv'
map_egg='eggs.csv'
map_shop='shops.csv'
map_monsters='monsters.csv'
teleport="clickteleport.csv"
#map_align='align.csv'

x_max_Glo=100000
y_max_Glo=100000
x_min_Glo=-100000
y_min_Glo=-100000
point_size_Glo=15

x_max_Loc=10000
y_max_Loc=10000
x_min_Loc=-10000
y_min_Loc=-10000
#play_Loc=[1000,1000]
point_size_Loc=30

#--------------------------------
#Function for Debugging
#Outputs random data to different csvs (currently player position)
#Used to check the ability of the map to update and then for checking where lag came from
#--------------------------------
def update(z):
    try:
        num = random.randint(1,5)
        data = np.zeros(shape=(num, 3))
        for i in range(0, num):
            out1 = random.randint(x_min_Glo,x_max_Glo)
            out2 = random.randint(y_min_Glo,y_max_Glo)
            data[i,0] = out1
            data[i,1] = out2
            data[i,2] = z
            np.savetxt(map_player, data, delimiter=",")
    except:
        print("Exiting")

#--------------------------------
#The teleport function
#When the plots are clicked (Need to check if local works)
#The xdata and ydata are taken and outputted to a csv file to be used by preload
#--------------------------------
def onclick(event):
    print('%s click: button=%d, x=%d, y=%d, xdata=%f, ydata=%f' %
          ('double' if event.dblclick else 'single', event.button,
           event.x, event.y, event.xdata, event.ydata))
    out=np.empty((1,4), dtype=object)
    out[0,0] = "click"
    out[0,1] = event.xdata
    out[0,2] = event.ydata
    out[0,3] = 15500
    print(out)
    np.savetxt(teleport, out, delimiter=",", fmt="%s %f %f %f")

if __name__ == "__main__":
    #try:
        x = 0.0
        y = 0.0
        #All these only need to be generated once as they are static
        #Attempts were made to have interactions be noted by the map
        #Such as when picking up the egg, but unfortunately this was not possible in time given
        data4 = genfromtxt(map_loot,delimiter=',', names=['id','x', 'y', 'z'], dtype=("S20,f8,f8,f8"))
        data5 = genfromtxt(map_egg,delimiter=',', names=['id','x', 'y', 'z'], dtype=("S20,f8,f8,f8"))
        data6 = genfromtxt(map_shop,delimiter=',', names=['id','x', 'y', 'z'], dtype=("S20,f8,f8,f8"))
        #Used to align the image of the map with the plotted data
        #Went to viable location on map (near coast or centre), noted position and then aligned it to plot
        #Had to edit the image seperately in gimp
        #data6 = genfromtxt(map_align,delimiter=',', names=['id','x', 'y', 'z'], dtype=("S20,f8,f8,f8"))

        #Creates one figure with two plots
        plt.ion()
        fig = plt.figure(figsize=(10,10))
        #--------------------------------
        #Global setup
        #--------------------------------
        subGlobal = fig.add_subplot(121)
        subGlobal.set_title('Global Map')
        #subGlobal = fig.add_subplot(111)
        subGlobal.set_xlim([x_min_Glo,x_max_Glo])
        subGlobal.set_ylim([y_min_Glo,y_max_Glo])

        p1, = subGlobal.plot(x, y, 'k.')#, label="Centre")
        p2, = subGlobal.plot(x, y, 'g.', label="Player")
        p3, = subGlobal.plot(x, y, 'b.', label="Other Players")
        p4, = subGlobal.plot(x, y, 'w.', label="Loot")
        p5, = subGlobal.plot(x, y, 'y.', label="Eggs")
        p6, = subGlobal.plot(x, y, 'm.', label="Shops")
        p7, = subGlobal.plot(x, y, 'r.', label="Monsters")
        #p11,= subGlobal.plot(x, y, 'k.', label="Aligner")
        p1.set_markersize(5)
        p2.set_markersize(point_size_Glo)
        p3.set_markersize(point_size_Glo)
        p4.set_markersize(point_size_Glo)
        p5.set_markersize(point_size_Glo)
        p6.set_markersize(point_size_Glo)
        p7.set_markersize(point_size_Glo)
        #p11.set_markersize(point_size_Glo)

        p4.set_xdata(data4['x'])
        p4.set_ydata(data4['y'])
        p5.set_xdata(data5['x'])
        p5.set_ydata(data5['y'])
        p6.set_xdata(data6['x'])
        p6.set_ydata(data6['y'])

        #p11.set_xdata(data6['x'])
        #p11.set_ydata(data6['y'])
        #print(data3['x'].shape[0])
        #subGlobal.annotate(data3['id'], xy=(data3['x'],data3['y']))
        plt.legend(loc="upper left")
        plt.grid()
        #img = Image.open("island_final.png")
        img = plt.imread("island_final.png")
        #img = img.resize((370000,370000))
        plt.imshow(img, extent = [x_min_Glo, x_max_Glo, y_min_Glo, y_max_Glo])#,interpolation='nearest', aspect='auto')#origin='upper'

        #--------------------------------
        #Local setup
        #--------------------------------
        subLocal = fig.add_subplot(122)
        subLocal.set_title('Local Map')
        #subLocal.margins(2,2)
        subLocal.set_xlim([0,x_max_Loc])
        subLocal.set_ylim([0,y_max_Loc])
        p8, = subLocal.plot(x, y, 'k.', label="Centre")
        p9, = subLocal.plot(x, y, 'g.', label="Player")
        p10,= subLocal.plot(x, y, 'b.', label="Other Players")
        p11,= subLocal.plot(x, y, 'w.', label="Loot")
        p12,= subLocal.plot(x, y, 'y.', label="Egg")
        p13,= subLocal.plot(x, y, 'm.', label="Shop")
        p14,= subLocal.plot(x, y, 'r.', label="Monsters")

        p8.set_markersize(point_size_Loc)
        p9.set_markersize(point_size_Loc)
        p10.set_markersize(point_size_Loc)
        p11.set_markersize(point_size_Loc)
        p12.set_markersize(point_size_Loc)
        p13.set_markersize(point_size_Loc)
        p14.set_markersize(point_size_Loc)

        p11.set_xdata(data4['x'])
        p11.set_ydata(data4['y'])
        p12.set_xdata(data5['x'])
        p12.set_ydata(data5['y'])
        p13.set_xdata(data6['x'])
        p13.set_ydata(data6['y'])

        plt.legend(loc="upper right")
        plt.grid()
        #plt.imshow(img,interpolation='nearest', aspect='auto')#origin='upper'
        #img = Image.open("radar.png")
        #img = plt.imread("radar.png")

        #img = img.resize((2000,2000))
        #plt.imshow(img, extent = [x_min_Loc, x_max_Loc,y_min_Loc,y_max_Loc])

        while True:
            #--------------------------------
            #Updating data
            #--------------------------------
            #update(5)
            time.sleep(0.01)
            #plt.close()
            #data  = genfromtxt(map_centre, delimiter=',', names=['id','x', 'y', 'z'])
            data2 = genfromtxt(map_player, delimiter=',', names=['id','x', 'y', 'z'])
           # img = Image.open("radar.png")
            #plt.imshow(img, extent = [(data2['x'] + x_min_Loc), (data2['x'] + x_max_Loc),(data2['y'] + y_min_Loc), (data2['y'] + y_max_Loc)])
            #destroyWindow(img)
            #img.close()
            #img.close()
            data3 = genfromtxt(map_otherp, delimiter=',', names=['id', 'x', 'y', 'z'])
            data7 = genfromtxt(map_monsters, delimiter=',', names=['id', 'x', 'y', 'z'])

            #Sets the local graph parameters depending on the player position
            subLocal.set_xlim([data2['x'] + x_min_Loc, data2['x'] + x_max_Loc])
            subLocal.set_ylim([data2['y'] + y_min_Loc, data2['y'] + y_max_Loc])

            #p1.set_xdata(data['x'])
            #p1.set_ydata(data['y'])
            p2.set_xdata(data2['x'])
            p2.set_ydata(data2['y'])
            p3.set_xdata(data3['x'])
            p3.set_ydata(data3['y'])
            p7.set_xdata(data7['x'])
            p7.set_ydata(data7['y'])

            #p6.set_xdata(data['x'])
            #p6.set_ydata(data['y'])
            p9.set_xdata(data2['x'])
            p9.set_ydata(data2['y'])
            p10.set_xdata(data3['x'])
            p10.set_ydata(data3['y'])
            p14.set_xdata(data7['x'])
            p14.set_ydata(data7['y'])

            cid = fig.canvas.mpl_connect('button_press_event', onclick)

            fig.canvas.draw()
            fig.canvas.flush_events()

#subGlobal.annotate(data3['id'], xy=(data3['x'],data3['y']))
            #for xy in p3:
            #    p3.annotate(data3['id'], xy)
#            p3.set_xdata(data['x'])
#            p3.set_ydata(data['y'])
#            p4.set_xdata(data2['x'])
#            p4.set_ydata(data2['y'])
            #if data['z'] == 5:
            #    p1.set_color('blue')
            #p1.set_gid("Group")
            #fig.savefig('test.png')
            #fig.close()


import os
import time
import cv2
import numpy as np
import tensorflow as tf
from utils.ssd_mobilenet_utils import *

def run_detection(image, interpreter):
    # Run model: start to detect
    # Sets the value of the input tensor.
    interpreter.set_tensor(input_details[0]['index'], image)
    # Invoke the interpreter.
    interpreter.invoke()

    # get results
    boxes = interpreter.get_tensor(output_details[0]['index'])
    classes = interpreter.get_tensor(output_details[1]['index'])
    scores = interpreter.get_tensor(output_details[2]['index'])
    num = interpreter.get_tensor(output_details[3]['index'])

    boxes, scores, classes = np.squeeze(boxes), np.squeeze(scores), np.squeeze(classes + 1).astype(np.int32)
    out_scores, out_boxes, out_classes = non_max_suppression(scores, boxes, classes)

    # Print predictions info
    #print('Found {} boxes for {}'.format(len(out_boxes), 'images/dog.jpg'))

    return out_scores, out_boxes, out_classes

def image_object_detection(interpreter, colors):
    image = cv2.imread('images/dog.jpg')
    image_data = preprocess_image_for_tflite(image, model_image_size=300)
    out_scores, out_boxes, out_classes = run_detection(image_data, interpreter)

    # Draw bounding boxes on the image file
    result = draw_boxes(image, out_scores, out_boxes, out_classes, class_names, colors)
    # Save the predicted bounding box on the image
    cv2.imwrite(os.path.join("out", "ssdlite_mobilenet_v2_dog.jpg"), result, [cv2.IMWRITE_JPEG_QUALITY, 90])

def real_time_object_detection(interpreter, colors):
    camera = cv2.VideoCapture(0)

    while camera.isOpened():
        start = time.time()
        ret, frame = camera.read()

        if ret:
            image_data = preprocess_image_for_tflite(frame, model_image_size=300)
            out_scores, out_boxes, out_classes = run_detection(image_data, interpreter)
            # Draw bounding boxes on the image file
            result = draw_boxes(frame, out_scores, out_boxes, out_classes, class_names, colors)
            end = time.time()

            # fps
            t = end - start
            fps  = "Fps: {:.2f}".format(1 / t)
            cv2.putText(result, fps, (10, 30),
		                cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2, cv2.LINE_AA)

            cv2.imshow("Object detection - ssdlite_mobilenet_v2", frame)
            if cv2.waitKey(1) & 0xFF == ord('q'):
                break

    camera.release()
    cv2.destroyAllWindows()

if __name__ == '__main__':
    # Load TFLite model and allocate tensors.
    interpreter = tf.lite.Interpreter(model_path="model_data/ssdlite_mobilenet_v2.tflite")
    interpreter.allocate_tensors()

    # Get input and output tensors.
    input_details = interpreter.get_input_details()
    output_details = interpreter.get_output_details()

    # label
    class_names = read_classes('model_data/coco_classes.txt')
    # Generate colors for drawing bounding boxes.
    colors = generate_colors(class_names)

    #image_object_detection(interpreter, colors)
    real_time_object_detection(interpreter, colors)


#!/usr/bin/python3

# NOTE: there's a ton of ways to manage bits:
#    numpy, BitVector, bitarray, butstring, numba, plain python...
#    (haven't a preference atm)

import os
import errno
import binascii
from decimal import Decimal as D
import numpy
import io
import logging

_logger = logging.getLogger(__name__)

def makedirs(name, mode=0o777, exist_ok=False):
    try:
        os.makedirs(name)
    except OSError as exc:  # Python >2.5
        if exc.errno == errno.EEXIST and os.path.isdir(name):
            pass
        else:
            raise

def write(data, fname):
    if isinstance(data, str):
        # perhaps we should warn them if they're using a string...
        data = data.encode()
    with open(fname, 'wb') as f:
        f.write(data)

def phexlify(data):
    ''' hexlify data into a printable, readable format '''
    dout = binascii.hexlify(data).upper()
    dout = b' '.join(dout[i:i+2] for i in range(0, len(dout), 2)).strip()
    #dout = ' '.join("{:02x}".format(c) for c in dout)
    return dout.decode('utf-8')

def hz(hzs):
    ''' hz/cycle calculator
    hzs: hz or cycle in seconds
        if hz, returns cycle in seconds, else hz
    '''
    v = 1 / hzs
    if v > 1:
        return round(v, 2)
    return v

def encode_bools(bool_lst):
    # if isinstance(bool_lst, BitVector):
    #     return bool_lst.int_val()
    #     # s = io.BytesIO()
    #     # bool_lst.write_bits_to_stream_object(s)
    #     # return s.getvalue()
    # TODO: test numpy.packbits (if bool_lst is multiple of 8)
    # numpy.packbits(bool_lst).tobytes()
    # numpy version (fast) (list size limit?)
    return numpy.sum(2**numpy.arange(len(bool_lst))*bool_lst)
    # plain python version (pretty darn fast, too)
    res = 0
    for i, bval in enumerate(bool_lst):
        if bval: res += 1 << i
    return res

def decode_bools(intval, bits):
    # return BitVector(intVal=intval, size=bits).reverse()
    # plain python
    res = []
    for bit in range(bits):
        mask = 1 << bit
        res.append((intval & mask) == mask)
    return res

class Singleton(type):
    _instances = {}
    def __call__(cls, *args, **kwargs):
        if cls not in cls._instances:
            cls._instances[cls] = super(Singleton, cls).__call__(*args, **kwargs)
        return cls._instances[cls]

# TODO: test!
from path import Path
class PathIO():
    ''' path-like object using object/item traversal for path traversal
    not really suitable for general usage; it's fine for ConfigFS,
    but not flexible enough for more complex file management (atm)
    '''
    def __init__(self, path):
        super(PathIO, self).__setattr__('path', path)

    def __setattr__(self, key, value):
        fpath = super(PathIO, self).__getattribute__('path')
        path = Path(fpath)
        kpath = path/key
        if isinstance(value, str):
            with kpath.open('w') as f:
                f.write(value)
        elif isinstance(value, PathIO):
            vfpath = super(PathIO, value).__getattribute__('path')
            vpath = Path(vfpath)
            try:
                vpath.symlink(kpath)
            except FileExistsError:
                kpath.unlink()
                vpath.symlink(kpath)
        elif value is None:
            kpath.makedirs()

    def __getattribute__(self, key):
        fpath = super(PathIO, self).__getattribute__('path')
        path = Path(fpath)
        kpath = path/key
        if kpath.isdir():
            return PathIO(kpath)
        elif kpath.isfile():
            return kpath.text() # kpath.bytes()
        elif not kpath.exists():
            return PathIONE(kpath)
        else:
            raise Exception('unsupported type')

    def __delattr__(self, key):
        fpath = super(PathIO, self).__getattribute__('path')
        path = Path(fpath)
        kpath = path/key
        if not kpath.exists():
            pass # warn?
        elif kpath.islink():
            kpath.unlink()
        elif kpath.isdir():
            kpath.rmdir()
        else:
            kpath.unlink()

    # item (array-like) access (for paths that require string values)

    def __getitem__(self, key):
        return getattr(self, key)

    def __setitem__(self, key, value):
        return setattr(self, key, value)

    def __delitem__(self, key):
        return delattr(self, key)

import os
class PathIONE(PathIO):
    ''' a non-existent path
    setting an attribute will trigger path creation
    '''
    def __setattr__(self, key, value):
        # since we've inherited PathIO, we access it's super directly
        path = object.__getattribute__(self, 'path')
        # revert to os for exist_ok
        os.makedirs(path, exist_ok=True)
        super(PathIONE, self).__setattr__(key, value)

gadget_conf_dir = '/sys/kernel/config/usb_gadget/'

class Gadget(object):
    def __init__(self, name):
        self.name = name
        self.gadget = PathIO(gadget_conf_dir)[self.name]

    def add_function(self, function, config):
        if isinstance(self.gadget.functions[function], PathIONE):
            self.gadget.functions[function] = None
        self.gadget.configs[config][function] = self.gadget.functions[function]

    def remove_function(self, function, config):
        del self.gadget.configs[config][function]

    def bind(self, udc):
        _logger.info('Binding to %s', udc)
        self.gadget.UDC = udc

    def remove_gadget(self):
        del PathIO(gadget_conf_dir)[self.name]

if __name__ == '__main__':
    import argparse
    parser = argparse.ArgumentParser()
    parser.add_argument('file', type=str, help='File containing encoded data')
    args = parser.parse_args()
    with open(args.file) as f:
        import codecs
        d = f.read().strip(' "\n') #.encode('utf-8')
        #print(d)
        #d = codecs.escape_decode(bytes(d, 'utf8')) #.decode('utf8'))
        #d = bytes(d, 'raw')
        d = bytes(d, 'utf8').decode('unicode_escape')
        #print(d)
        print(phexlify(bytes(d, 'utf8')))


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon Mar 30 20:44:42 2020

@author: abhijithneilabraham
"""
a=[2,4,2,6,1,7,8,9,2,1]
def candies(n, arr):
    candy_lr = [1]*(len(arr))
    candy_rl = [1]*(len(arr))
    candy = []

    # traverses from left to right and assigns candy to students
    for i in range(1,len(arr)):
        if arr[i]>arr[i-1]:
            candy_lr[i] = candy_lr[i-1]+1

    # traverses from right to left and assigns candy to students
    for i in range(len(arr)-2,-1,-1):
        if arr[i]>arr[i+1]:
            candy_rl[i] = candy_rl[i+1]+1

    #calculates the total candy needed
    for i in range(0,len(arr)):
        candy.append(max(candy_lr[i],candy_rl[i]))
    print(sum(candy))
    return sum(candy)

def candies(n, arr):
    cl=[1]*n
    cr=[1]*n
    s=0

    for i in range(1,n):
        if arr[i-1]<arr[i]:
            cl[i]=cl[i-1]+1

    for i in range(n-2,-1,-1):
        if arr[i+1]<arr[i]:
            cr[i]=cr[i+1]+1
    #for i in range(n):
    for i in range(n):
        s+=max(cl[i],cr[i])
    print(s)
    return s

print(candies(len(y),y))


'''

# Sample Input
6               arr[] size n = 6
-4 3 -9 0 4 1   arr = [-4, 3, -9, 0, 4, 1]

# Sample Output

0.500000
0.333333
0.166667

'''

positive = 0
negative = 0
zero = 0
arr = [-4, 3, -9, 0, 4, 1]
for i in  arr:
    if i == 0:
        zero += 1
    elif i > 0:
        positive += 1
    elif i < 0:
        negative += 1
p = positive/len(arr)
n = negative/len(arr)
z = zero/len(arr)

print('%.6f'%p)
print('%.6f'%n)
print('%.6f'%z)


import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import tushare as ts

# 获取数据
s_pf = '600000'  # 浦发银行
s_gd = '601818'  # 光大银行
sdate = '2016-01-01'
edate = '2016-12-31'
df_pf = ts.get_h_data(s_pf, start=sdate, end=edate).sort_index(axis=0, ascending=True)
df_gd = ts.get_h_data(s_gd, start=sdate, end=edate).sort_index(axis=0, ascending=True)
df = pd.concat([df_pf.close, df_gd.close], axis=1, keys=['pf_close', 'gd_close'])
df.ffill(axis=0, inplace=True)  # 填充数据
df.to_csv('pf_gd.csv')

# df = pd.read_csv('pf_gd.csv') 如果第二次运行 可以不用再次下载数据，直接读取上次保存的数据

corr = df.corr(method='pearson', min_periods=1)
print(corr)
'''
           pf_close  gd_close
pf_close   1.00000   0.69518
gd_close   0.69518   1.00000
'''
# df.plot(figsize=(20, 12))
# plt.savefig('pf_gd.jpg')
# plt.show()

# 将数值归一化
df['pf_one'] = df.pf_close / float(df.pf_close[0]) * 100
df['gd_one'] = df.gd_close / float(df.gd_close[0]) * 100

df.pf_one.plot(figsize=(20, 12))
df.gd_one.plot(figsize=(20, 12))
plt.savefig('pf_gd.jpg')
plt.close()