"""Test class ``atmcirclib.geo.BoundingBox``."""
from __future__ import annotations

# Third-party
import numpy as np
import pytest

# First-party
from atmcirclib.geo import BoundingBox

# pylint: disable=R0201  # no-self-use

class Test_Init:
    """Test initialization."""

    def test_noargs_fail(self) -> None:
        """Creating a new instance w/o arguments fails."""
        with pytest.raises(TypeError):
            BoundingBox()  # type: ignore

    def test_pos(self) -> None:
        """Use only positional arguments."""
        (llx, urx, lly, ury) = (-46, 2, -15, 9)
        bbx = BoundingBox(llx, urx, lly, ury)
        assert bbx.llx == llx
        assert bbx.urx == urx
        assert bbx.lly == lly
        assert bbx.ury == ury

    def test_kw(self) -> None:
        """Use keyword arguments."""
        (llx, urx, lly, ury) = (-46, 2, -15, 9)
        bbx = BoundingBox(
            lly=lly,
            llx=llx,
            ury=ury,
            urx=urx,
        )
        assert bbx.llx == llx
        assert bbx.urx == urx
        assert bbx.lly == lly
        assert bbx.ury == ury

class Test_Derive:
    """Test method ``derive`` to derive a new instance."""

    def test_equal(self) -> None:
        """Derive a new instance with the same coordinates."""
        (llx, urx, lly, ury) = (-46, 2, -15, 9)
        bbx1 = BoundingBox(llx, urx, lly, ury)
        bbx2 = bbx1.derive()
        assert bbx1 is not bbx2
        assert bbx1 == bbx2

    def test_change(self) -> None:
        """Derive a new instance with changed coordinates."""
        (llx, urx, lly, ury) = (-46, 2, -15, 9)
        bbx1 = BoundingBox(llx, urx, lly, ury)
        bbx2 = bbx1.derive(llx=-32, ury=12)
        assert bbx1 is not bbx2
        assert bbx1 != bbx2
        assert bbx2.llx == -32
        assert bbx2.urx == urx
        assert bbx2.lly == lly
        assert bbx2.ury == 12

class Test_FromCoords:
    """Test class method ``from_coords`` to create an instance from coords."""

    def test(self) -> None:
        """Create an instance from coordinate arrays."""
        xs = np.arange(-20, 5.1, 2)
        ys = np.arange(30, 45.1, 2)
        bbx = BoundingBox.from_coords(xs, ys)
        assert bbx.llx == xs[0]
        assert bbx.urx == xs[-1]
        assert bbx.lly == ys[0]
        assert bbx.ury == ys[-1]

class Test_Get:
    """Test methods ``get_*`` that return basic bbox properties."""

    def test_center(self) -> None:
        """Get center of box."""
        (llx, urx, lly, ury) = (-46, 2, -15, 9)
        bbx = BoundingBox(llx, urx, lly, ury)
        cx, cy = bbx.get_center()
        assert np.isclose(cx, 0.5 * (llx + urx))
        assert np.isclose(cy, 0.5 * (lly + ury))

    def test_width(self) -> None:
        """Get width of box."""
        (llx, urx, lly, ury) = (-46, 2, -15, 9)
        bbx = BoundingBox(llx, urx, lly, ury)
        w = bbx.get_width()
        assert np.isclose(w, urx - llx)

    def test_height(self) -> None:
        """Get height of box."""
        (llx, urx, lly, ury) = (-46, 2, -15, 9)
        bbx = BoundingBox(llx, urx, lly, ury)
        h = bbx.get_height()
        assert np.isclose(h, ury - lly)

    def test_aspect(self) -> None:
        """Get aspect ratio of box."""
        (llx, urx, lly, ury) = (-46, 2, -15, 9)
        bbx = BoundingBox(llx, urx, lly, ury)
        a = bbx.get_aspect()
        assert np.isclose(a, (urx - llx) / (ury - lly))

    def test_xlim(self) -> None:
        """Get limits of box in x-direction."""
        (llx, urx, lly, ury) = (-46, 2, -15, 9)
        bbx = BoundingBox(llx, urx, lly, ury)
        xl = bbx.get_xlim()
        assert np.allclose(xl, (llx, urx))

    def test_ylim(self) -> None:
        """Get limits of box in y-direction."""
        (llx, urx, lly, ury) = (-46, 2, -15, 9)
        bbx = BoundingBox(llx, urx, lly, ury)
        yl = bbx.get_ylim()
        assert np.allclose(yl, (lly, ury))

class Test_Shrink:
    """Test method ``shrink`` to reduce the size of the bbox."""

    def test_copy(self) -> None:
        """Check that the original instance is not affected by shrinking."""
        (llx, urx, lly, ury) = (-46, 2, -15, 9)
        bbx1 = BoundingBox(llx, urx, lly, ury)
        bbx2 = bbx1.shrink(0)
        assert bbx1 is not bbx2
        bbx1.shrink(1)
        assert bbx1 == bbx2

    def test_scalar(self) -> None:
        """Shrink by the same amount in all directions."""
        (llx, urx, lly, ury) = (-46, 2, -15, 9)
        bbx = BoundingBox(llx, urx, lly, ury)
        bbx = bbx.shrink(1)
        assert bbx.llx == llx + 1
        assert bbx.urx == urx - 1
        assert bbx.lly == lly + 1
        assert bbx.ury == ury - 1

    def test_tuple(self) -> None:
        """Shrink by different amounts in the x- and y-direction."""
        (llx, urx, lly, ury) = (-46, 2, -15, 9)
        bbx = BoundingBox(llx, urx, lly, ury)
        bbx = bbx.shrink((2, 3))
        assert bbx.llx == llx + 2
        assert bbx.urx == urx - 2
        assert bbx.lly == lly + 3
        assert bbx.ury == ury - 3

    def test_overshrink(self) -> None:
        """Shrinking stops at the center of the box if it becomes too small."""
        (llx, urx, lly, ury) = (-46, 2, -15, 9)
        bbx1 = BoundingBox(llx, urx, lly, ury)
        bbx2 = bbx1.shrink(100)
        assert bbx2.llx == bbx1.get_center()[0]
        assert bbx2.urx == bbx1.get_center()[0]
        assert bbx2.lly == bbx1.get_center()[1]
        assert bbx2.ury == bbx1.get_center()[1]

    def test_grow(self) -> None:
        """Grow in one direction (and overshrink in the other)."""
        (llx, urx, lly, ury) = (-46, 2, -15, 9)
        bbx1 = BoundingBox(llx, urx, lly, ury)
        bbx2 = bbx1.shrink((-5, 100))
        assert bbx2.llx == llx - 5
        assert bbx2.urx == urx + 5
        assert bbx2.lly == bbx1.get_center()[1]
        assert bbx2.ury == bbx1.get_center()[1]


# easy fast
from collections import deque

N, M = map(int, input().split())
kakao_map = [list(map(int, input().split())) for _ in range(N)]

start = False
for i in range(N):
    for j in range(M):
        if kakao_map[i][j] == 2:
            start = (i, j)
            break
    if start:
        break
visited =[[0]*M for _ in range(N)]
dist = [[0]*M for _ in range(N)]
dist[start[0]][start[1]] = 0
Q = deque([start])
visited[start[0]][start[1]] = 1

move = [(1, 0), (0, 1), (-1, 0), (0, -1)]

while Q:
    x, y = Q.popleft()
    for i in range(4):
        nx = x + move[i][0]
        ny = y + move[i][1]
        if nx < 0 or ny < 0 or nx == N or ny == M:
            continue
        if not visited[nx][ny] and kakao_map[nx][ny]:
            visited[nx][ny] = 1
            if not dist[nx][ny]:
                dist[nx][ny] = dist[x][y] + 1
            elif dist[nx][ny] > dist[x][y] + 1:
                dist[nx][ny] = dist[x][y] + 1
            Q.append((nx, ny))

for i in range(N):
    for j in range(M):
        if not visited[i][j] and kakao_map[i][j]:
            dist[i][j] = -1

for i in dist:
    print(*i)


def get_even(n):
    return n//2

for _ in range(int(input())):
    a, b = list(map(int, input().split()))
    less = min(a, b)
    if a == less:
        even = get_even(b)
        odd = b-even
    else:
        even = get_even(a)
        odd = a-even
    # for i in range(1, a+1):
    #     for j in range(1, b+1):
    #         if (i+j) % 2 == 0:
    #             print(i, j)
    #     print("-----")
    # print("=======")
    # ans = 0
    less_even = get_even(less)
    less_odd = less - less_even
    ans = less_even*even + less_odd*odd
    # for i in range(1, less+1):
    #     if i % 2 == 0:
    #         ans += even
    #     else:
    #         ans += odd
    print(ans)


import cv2
import glob
from matplotlib import pyplot as plt

def substImage(path):
    img_src1 = cv2.imread("../SubstImage.jpg", 1)
    img_src2 = cv2.imread(path, 1)

    fgbg = cv2.bgsegm.createBackgroundSubtractorMOG()

    fgmask = fgbg.apply(img_src1)
    fgmask = fgbg.apply(img_src2)

    return fgmask

def binaryCvt(path,thresh):
	image = cv2.imread(path,1)
	gray  = cv2.cvtColor(image,cv2.COLOR_RGB2GRAY)

	ret, binary =cv2.threshold(gray,thresh,255,cv2.THRESH_BINARY)
	# ret, binary =cv2.threshold(gray,thresh,1,cv2.THRESH_OTSU)

	return binary
	# return gray

def cutLamp(binary):
	pWork={
     	'y1':75, 'y2':95,
        'x1':345,'x2':365}
	pPump={
     	'y1'    :175,'y2'    :195,
        '1offx1':330,'1offx2':350,
        '1onx1' :360,'1onx2' :380,
        '2offx1':385,'2offx2':405,
        '2onx1' :410,'2onx2' :430}
	pTanks={
		'fully1':250,"fully2":270,
		"lowy1" :275,'lowy2' :300,
		"Hx1"   :345,'Hx2'   :365,
		'Lx1'	:400,'Lx2'	 :420}

	# print(pWork,pPump,pTanks)

	working=binary[pWork['y1']:pWork['y2'],pWork['x1']	  :pWork['x2']]
	no1_off=binary[pPump['y1']:pPump['y2'],pPump['1offx1']:pPump['1offx2']]
	no1_on =binary[pPump['y1']:pPump['y2'],pPump['1onx1'] :pPump['1onx2'] ]
	no2_off=binary[pPump['y1']:pPump['y2'],pPump['2offx1']:pPump['2offx2']]
	no2_on =binary[pPump['y1']:pPump['y2'],pPump['2onx1'] :pPump['2onx2'] ]
	H_full =binary[pTanks['fully1']:pTanks['fully2'],pTanks['Hx1']:pTanks['Hx2']]
	H_low  =binary[pTanks['lowy1'] :pTanks['lowy2'], pTanks['Hx1']:pTanks['Hx2']]
	L_full =binary[pTanks['fully1']:pTanks['fully2'],pTanks['Lx1']:pTanks['Lx2']]
	L_low  =binary[pTanks['lowy1'] :pTanks['lowy2'], pTanks['Lx1']:pTanks['Lx2']]

	cv2.rectangle(binary,(pWork['x1'],pWork['y1']),(pWork['x2'],pWork['y2']),230,1)
	cv2.rectangle(binary,(pPump['1offx1'],pPump['y1']),(pPump['1offx2'],pPump['y2']),230,1)
	cv2.rectangle(binary,(pPump['1onx1'] ,pPump['y1']),(pPump['1onx2'] ,pPump['y2']),230,1)
	cv2.rectangle(binary,(pPump['2offx1'],pPump['y1']),(pPump['2offx2'],pPump['y2']),230,1)
	cv2.rectangle(binary,(pPump['2onx1'] ,pPump['y1']),(pPump['2onx2'] ,pPump['y2']),230,1)
	cv2.rectangle(binary,(pTanks['Hx1'],pTanks['fully1']),(pTanks['Hx2'],pTanks['fully2']),230,1)
	cv2.rectangle(binary,(pTanks['Hx1'],pTanks['lowy1'] ),(pTanks['Hx2'],pTanks['lowy2'] ),230,1)
	cv2.rectangle(binary,(pTanks['Lx1'],pTanks['fully1']),(pTanks['Lx2'],pTanks['fully2']),230,1)
	cv2.rectangle(binary,(pTanks['Lx1'],pTanks['lowy1'] ),(pTanks['Lx2'],pTanks['lowy2'] ),230,1)

	return [working,no1_off,no1_on,no2_off,no2_on,H_full,H_low,L_full,L_low],binary

def calcBW(bw_image,percent):
  image_size = bw_image.size
  whitePixels = cv2.countNonZero(bw_image)
  blackPixels = bw_image.size - whitePixels
  whiteAreaRatio = (whitePixels/image_size)*100#[%]
  blackAreaRatio = (blackPixels/image_size)*100#[%]
  print("White Area [%] : ", whiteAreaRatio)
  print("Black Area [%] : ", blackAreaRatio)
  if whiteAreaRatio > percent:
	  return '1'
  else:
	  return '0'

def draw(image):
	fig = plt.figure()
	ax1 = fig.add_subplot(111)
	ax1.imshow(binary, cmap = 'gray')
	ax1.tick_params(labelbottom = False, bottom = False)
	ax1.tick_params(labelleft = False, left = False)
	fig.tight_layout()
	plt.show()
	plt.close()

def writeFile(path,lamplist,f,percent):
	lampFlag=[]
	lampFlag.append(path[10:20])

	for bw_image in lampList:
		lampFlag.append(calcBW(bw_image,percent))
		# f=open(path[10:14]+'.txt','a')
	print(lampFlag)
	f.writelines(' '.join(lampFlag))
	f.writelines('\n')

pathList = glob.glob('image_*')
print(pathList)
print(pathList[0][10:14])

f=open(pathList[0][10:14]+'.txt',mode='w')
f.write("wrok No1_off No1_on No2_off No2_on HTank_full HTank_low LTank_full LTank_low\n")
f=open(pathList[0][10:14]+'.txt',mode='a')

for path in pathList:
  binary = binaryCvt(path,245)
#   binary = substImage(path)
  lampList,binary = cutLamp(binary)
#   draw(binary)
  writeFile(path,lampList,f,12)

f.close()


# digits_cnn.py

import random
import numpy as np

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

import training
from training import training, batchsize

class Classifier(nn.Module):
    def __init__(self):
        super(Classifier, self).__init__()

        self.conv1 = nn.Conv2d(in_channels=1, out_channels=6, kernel_size=(5,5), stride=1, padding=2)
        self.maxpool1 = nn.MaxPool2d(kernel_size=(2,2), stride=2)
        self.conv2 = nn.Conv2d(in_channels=6, out_channels=16, kernel_size=(5,5), stride=1,padding=0)
        self.maxpool2 = nn.MaxPool2d(kernel_size=(2,2), stride=2)
        self.fc1 = nn.Linear(400, 120)
        self.fc2 = nn.Linear(120, 84)
        self.fc3 = nn.Linear(84, 10)
        self.softmax = nn.Softmax(dim=0)

    def forward(self, x):
        x = x.view(batchsize, 1, 28, 28)
        x = self.maxpool1(F.relu(self.conv1(x)))
        x = self.maxpool2(F.relu(self.conv2(x)))
        x = x.view(batchsize, 1, 400)
        x = F.relu(self.fc1(x))
        x = F.relu(self.fc2(x))
        x = self.fc3(x)

        return x

    def forward_pass(self, x, xnum):
        x = x.view(xnum, 1, 28, 28)
        x = self.maxpool1(F.relu(self.conv1(x)))
        x = self.maxpool2(F.relu(self.conv2(x)))
        x = x.view(xnum, 1, 400)
        x = F.relu(self.fc1(x))
        x = F.relu(self.fc2(x))
        x = self.fc3(x)

        return x

    def num_flat_features(self, x):
        size = x.size()[1:]  # all dimensions except the batch dimension
        num_features = 1
        for s in size:
            num_features *= s
        return num_features

if __name__=="__main__":

    ''' training entity setup '''
    classifier = Classifier()
    lossF = nn.CrossEntropyLoss()
    optimizer = torch.optim.SGD(classifier.parameters(), lr=0.005)
    optimizer.zero_grad()
    ''' training entity setup '''

    training(classifier, lossF, optimizer, "alex_best")


#!/bin/python3

# https://www.hackerrank.com/challenges/migratory-birds

def migratoryBirds(n, ar):
    import operator
    results = {}

    for i in ar:

        try:
            results[i] += 1
            continue
        except:
            results[i] = 1
            continue

    k, v = sorted(results.items(), key=operator.itemgetter(1), reverse=True)[0]

    return k

n = 6
ar = [1, 4, 4, 4, 5, 5, 3]
result = migratoryBirds(n, ar)
print(result)


# 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.

from wsme import types as wtypes

from solum.api.controllers import common_types
from solum.api.controllers.v1.datamodel import types as api_types

class Extension(api_types.Base):
    """The Extension resource represents Provider modifications.

    This may include additional protocol semantics, resource types,
    application lifecycle states, resource attributes, etc. Anything may be
    added, as long as it does not contradict the base functionality offered
    by Solum.
    """

    version = wtypes.text
    "Version of the extension."

    documentation = common_types.Uri
    "Documentation URI to the extension."

    @classmethod
    def sample(cls):
        return cls(uri='http://example.com/v1/extensions/logstash',
                   name='logstash',
                   type='extension',
                   tags=['large'],
                   project_id='1dae5a09ef2b4d8cbf3594b0eb4f6b94',
                   user_id='55f41cf46df74320b9486a35f5d28a11',
                   description='This logstash extension provides a tool for '
                               'managing your application events and logs.',
                   version='2.13',
                   documentation='http://example.com/docs/ext/logstash')


# remember -- dictionaries are MUTABLE

my_dict = {"name": "Eddy", "age": 26}
my_dict["age"] = 27
print(my_dict)

# note --> updating the value is O(1) time complexity (as well as the space complexity)

my_dict["address"] = "London"
print(my_dict)

# adding a pair to the dictionary --> is O(1) time complexity
# amortized O(1) space complexity


# -*- coding: utf-8 -*-
# __author__ = 'shijin'

"""fq文件序列拆分"""

import os
import datetime
import json
import shutil
import re
from biocluster.workflow import Workflow

class FastqSplitWorkflow(Workflow):
    def __init__(self, wsheet_object):
        self._sheet = wsheet_object
        super(FastqSplitWorkflow, self).__init__(wsheet_object)
        options = [
            {"name": "in_fastq", "type": "infile", 'format': "sequence.fastq"}
        ]
        self.add_option(options)
        self.set_options(self._sheet.options())
        self.samples = {}

    def run_fastq_split(self):
        fastq_path = self.option("in_fastq").prop["path"]
        with open(fastq_path,"r") as f:
            for line in f:
                m = re.match("@(.+)_(\d+)", line)
                if not m:
                    self.set_error('fastq文件格式不符合要求', code="12701601")
                sample_name = m.group(1)
                sample = self.return_sample(sample_name)
                sample.add_new_fastq(line, next(f), next(f), next(f))
        for sample in self.samples.values():
            sample.close_all()
        self.logger.info("全部fastq序列处理完毕")
        # self.set_db()
        self.end_this()

    def return_sample(self,sample_name):
        if sample_name in self.samples:
            return self.samples[sample_name]
        sample = Sample(sample_name,self.output_dir)
        self.samples[sample_name] = sample
        return sample

    def run(self):
        self.run_fastq_split()
        super(FastqSplitWorkflow, self).run()

    def end_this(self):
        """
        result_dir = self.add_upload_dir(self.output_dir)
        result_dir.add_relpath_rules([
            [".", "", "结果输出目录"],
            ["venn_table.xls", "xls", "Venn表格"]
        ])
        """
        super(FastqSplitWorkflow, self).end()

class Sample(object):
    def __init__(self, name,dir):
        self.name = name
        self._new_fastq_file = open(dir+ "/" + self.name + '.fastq', 'w')

    def add_new_fastq(self, line1,line2,line3,line4):
        self._new_fastq_file.write(line1)
        self._new_fastq_file.write(line2)
        self._new_fastq_file.write(line3)
        self._new_fastq_file.write(line4)

    def close_all(self):
        self._new_fastq_file.close()


# Z přednášky již znáte Caesarovu šifru. Vigenèrova šifra funguje
# podobně, ale klíčem je řetězec (slovo), nikoliv pouze jediné číslo
# z rozsahu 0–26.

# Šifrujeme (i dešifrujeme) tak, že pod vstupní text si podepíšeme
# klíč, který zopakujeme tolikrát, aby byl stejně dlouhý jako text.
# Každou takovou dvojici pak použijeme jako vstup pro Caesarovou
# šifru (písmeno klíče udává posuv — A = 0, B = 1, …).

# Jako příklad zašifrujeme slovo „PYTHON“ klíčem „BCD“: klíč nejprve
# prodloužíme opakováním na „BCDBCD“, poté spočítáme:
#
#  • ‹caesar('P', 1) = 'Q'›
#  • ‹caesar('Y', 2) = 'A'›
#  • ‹caesar('T', 3) = 'W'›
#  • ‹caesar('H', 1) = 'I'›
#  • ‹caesar('O', 2) = 'Q'›
#  • ‹caesar('N', 3) = 'Q'›

# Napište (čistou) funkci, která na vstupu dostane text
# k zašifrování a klíč. Obojí budou řetězce libovolné délky, přičemž
# klíč je neprázdný a tvořený pouze písmeny anglické abecedy a text
# písmeny anglické abecedy a mezerami. Výstupem bude daný text
# zašifrovaný pomocí daného klíče. Pro zjednodušení budeme vracet
# zašifrovaný text ve velkých písmenech.

def vigenere_encrypt(text, key):
    encrypted = ''
    for i in range(len(text)):
        cipher = ord(key.upper()[i%len(key)]) - ord('A')
        if (ord(text.upper()[i]) + cipher)> 90:
            encrypted += chr(ord(text.upper()[i]) + cipher - 26)
        elif text[i] == ' ':
                encrypted += ' '
        else:
            encrypted += chr(ord(text.upper()[i]) + cipher)
    return encrypted

# Dále napište (opět čistou) funkci, která na vstupu dostane
# zašifrovaný text a odpovídající klíč (obojí ve stejném formátu,
# jako parametry předešlé funkce) a vrátí dešifrovaný text. Pro
# zjednodušení budeme dešifrovaný text opět vracet ve velkých
# písmenech.

def vigenere_decrypt(text, key):
    encrypted = ''
    for i in range(len(text)):
        cipher = ord(key.upper()[i%len(key)]) - ord('A')
        if text[i] == ' ':
                encrypted += ' '
        elif (ord(text.upper()[i]) - cipher) < 65:
            encrypted += chr(ord(text.upper()[i]) - cipher + 26)
        else:
            encrypted += chr(ord(text.upper()[i]) - cipher)
    return encrypted

def main():
    assert vigenere_encrypt("", "abcd") == ""
    assert vigenere_encrypt(" ", "a") == " "
    assert vigenere_encrypt("abcde", "a") == "ABCDE"
    assert vigenere_encrypt("programming", "c") == "RTQITCOOKPI"
    assert vigenere_encrypt("python", "bcd") == "QAWIQQ"
    assert vigenere_encrypt("PYTHON", "PROGRAMMING") == "EPHNFN"
    assert vigenere_encrypt("Python is fun", "NO") == "CMGVBB WF SIA"
    assert vigenere_encrypt("I shall pass this course "
                            "because I love programming", "hopefully") \
        == "P HLFFW NHGH YBTD JCJVXY MCJOJWJ T JVJT ULZRPHABMSA"

    assert vigenere_decrypt("", "abcd") == ""
    assert vigenere_decrypt(" ", "a") == " "
    assert vigenere_decrypt("abcde", "a") == "ABCDE"
    assert vigenere_decrypt("RTQITCOOKPI", "C") == "PROGRAMMING"
    assert vigenere_decrypt("qawiqq", "bcd") == "PYTHON"
    assert vigenere_decrypt("EPHNFN", "programming") == "PYTHON"
    assert vigenere_decrypt("CMGVBB WF SIA", "no") == "PYTHON IS FUN"
    assert vigenere_decrypt("P HLFFW NHGH YBTD JCJVXY "
                            "MCJOJWJ T JVJT ULZRPHABMSA", "HOPEFULLY") \
        == "I SHALL PASS THIS COURSE BECAUSE I LOVE PROGRAMMING"

if __name__ == '__main__':
    main()


import logging
from typing import List

from cmdb.database.managers import DatabaseManagerMongo
from cmdb.database.errors.database_errors import NoDocumentFound
from cmdb.framework.cmdb_base import CmdbManagerBase, ManagerGetError, ManagerInsertError, ManagerUpdateError, \
    ManagerDeleteError
from cmdb.user_management.models.user import UserModel
from cmdb.user_management.models.group import UserGroupModel
from cmdb.user_management.models.right import BaseRight
from cmdb.utils.error import CMDBError
from cmdb.security.security import SecurityManager

LOGGER = logging.getLogger(__name__)

class UserManager(CmdbManagerBase):

    def __init__(self, database_manager: DatabaseManagerMongo):
        self.scm = SecurityManager(database_manager)
        self.rights = self._load_rights()
        super(UserManager, self).__init__(database_manager)

    def get_new_id(self, collection: str) -> int:
        return self.dbm.get_next_public_id(collection)

    def get_users(self) -> List[UserModel]:
        """Get all users"""
        user_list = []
        for founded_user in self._get_many(collection=UserModel.COLLECTION):
            try:
                user_list.append(UserModel.from_data(founded_user))
            except CMDBError:
                continue
        return user_list

    def get_users_by(self, sort='public_id', **requirements) -> List[UserModel]:
        """Get a list of users by requirement"""
        user_list = []
        users_in_database = self._get_many(collection=UserModel.COLLECTION, sort=sort, **requirements)
        for user in users_in_database:
            try:
                user_ = UserModel.from_data(user)
            except CMDBError as err:
                LOGGER.error(f'[UserManager] Error while inserting database user into return list: {err}')
                continue
            user_list.append(user_)
        return user_list

    def get_user_by(self, **requirements) -> UserModel:
        """Get user by requirement"""
        try:
            return UserModel.from_data(self._get_by(collection=UserModel.COLLECTION, **requirements))
        except NoDocumentFound:
            raise UserManagerGetError(f'UserModel not found')

    def get_user_by_name(self, user_name) -> UserModel:
        """Get a user by his user_name"""
        return self.get_user_by(user_name=user_name)

    def update_user(self, public_id, update_params: dict):
        try:
            return self._update(collection=UserModel.COLLECTION, public_id=public_id, data=update_params)
        except (CMDBError, Exception):
            raise UserManagerUpdateError(f'Could not update user with ID: {public_id}')

    def update_users_by(self, query: dict, update: dict):
        try:
            return self._update_many(collection=UserModel.COLLECTION, query=query, update=update)
        except Exception as err:
            raise UserManagerUpdateError(err)

    def delete_user(self, public_id: int) -> bool:
        try:
            return self._delete(collection=UserModel.COLLECTION, public_id=public_id)
        except Exception:
            raise UserManagerDeleteError(f'Could not delete user with ID: {public_id}')

    def delete_users_by(self, query: dict):
        try:
            return self._delete_many(collection=UserModel.COLLECTION, filter_query=query).acknowledged
        except Exception as err:
            raise UserManagerDeleteError(err)

    def get_groups(self) -> list:
        group_list = []
        for founded_group in self._get_many(collection=UserGroupModel.COLLECTION):
            try:
                group_list.append(UserGroupModel(**founded_group))
            except CMDBError:
                LOGGER.debug("Error while group parser: {}".format(founded_group))
                continue
        return group_list

    def get_group(self, public_id: int) -> UserGroupModel:
        try:
            founded_group = self._get(collection=UserGroupModel.COLLECTION, public_id=public_id)
            return UserGroupModel(**founded_group)
        except NoDocumentFound as e:
            raise UserManagerGetError(e)

    def get_group_by(self, **requirements) -> UserGroupModel:
        try:
            return UserGroupModel(**self._get_by(collection=UserGroupModel.COLLECTION, **requirements))
        except NoDocumentFound:
            raise UserManagerGetError(f'Group not found')

    def update_group(self, public_id, update_params: dict) -> bool:
        try:
            ack = self._update(collection=UserGroupModel.COLLECTION, public_id=public_id,
                               data=update_params)
        except (CMDBError, Exception) as e:
            LOGGER.error(e)
            raise UserManagerUpdateError(e)
        return ack

    def insert_group(self, insert_group: UserGroupModel) -> int:
        try:
            return self.dbm.insert(collection=UserGroupModel.COLLECTION, data=insert_group.__dict__)
        except Exception:
            raise UserManagerInsertError(insert_group.name)

    def delete_group(self, public_id, user_action: str = None, options: dict = None) -> bool:
        try:
            delete_group: UserGroupModel = self.get_group(public_id)
        except UserManagerGetError:
            raise UserManagerDeleteError(f'Could not find group with ID: {public_id}')

        try:
            ack = self.dbm.delete(collection=UserGroupModel.COLLECTION, public_id=public_id).acknowledged
        except Exception:
            raise UserManagerDeleteError(f'Could not delete group')

        # Cleanup user
        users_in_group: [UserModel] = self.get_users_by(**{'group_id': delete_group.get_public_id()})
        if len(users_in_group) > 0:
            if user_action == 'move':
                if not options.get('group_id'):
                    raise UserManagerDeleteError(f'Not move group was provided!')
                self.update_users_by(query={'group_id': delete_group.get_public_id()},
                                     update={'$set': {'group_id': int(options.get('group_id'))}})
            elif user_action == 'delete':
                self.delete_users_by({'group_id': delete_group.get_public_id()})
            else:
                raise UserManagerDeleteError(f'No valid user action was provided')
        return ack

    def get_right_names_with_min_level(self, min_level):
        selected_levels = []
        for right in self.rights:
            if right.get_level() <= min_level:
                selected_levels.append(right.get_display_name())
        return selected_levels

    def get_all_rights(self):
        return self.rights

    def _load_rights(self):
        from cmdb.user_management.rights import __all__
        return self._load_right_tree(__all__)

    def _load_right_tree(self, right_list) -> list:
        rights = list()
        for right in right_list:
            if isinstance(right, tuple) or isinstance(right, list):
                rights = rights + self._load_right_tree(right)
            else:
                rights.append(right)
        return rights

    def get_right_by_name(self, name) -> BaseRight:
        try:
            return next(right for right in self.rights if right.name == name)
        except Exception:
            raise UserManagerGetError(name)

    def group_has_right(self, group_id: int, right_name: str) -> bool:
        right_status = False
        try:
            selected_right = self.get_right_by_name(right_name)
            chosen_group = self.get_group(group_id)
        except UserManagerGetError:
            return right_status

        right_status = chosen_group.has_right(right_name=right_name)
        if not right_status:
            right_status = chosen_group.has_extended_right(right_name=right_name)
        return right_status

class UserManagerGetError(ManagerGetError):

    def __init__(self, err):
        super(UserManagerGetError, self).__init__(err)

class UserManagerInsertError(ManagerInsertError):

    def __init__(self, err):
        super(UserManagerInsertError, self).__init__(err)

class UserManagerUpdateError(ManagerUpdateError):

    def __init__(self, err):
        super(UserManagerUpdateError, self).__init__(err)

class UserManagerDeleteError(ManagerDeleteError):

    def __init__(self, err):
        super(UserManagerDeleteError, self).__init__(err)


'''Files Required are
positive_words.txt
negative_words.txt
project_twitter_data.csv

Give this beast these three files it will give you a file resulting_data.csv
'''
punctuation_chars = ["'", '"', ",", ".", "!", ":", ";", '#', '@']
# lists of words to use
positive_words = []
with open("positive_words.txt") as pos_f:
    for lin in pos_f:
        if lin[0] != ';' and lin[0] != '\n':
            positive_words.append(lin.strip())

negative_words = []
with open("negative_words.txt") as pos_f:
    for lin in pos_f:
        if lin[0] != ';' and lin[0] != '\n':
            negative_words.append(lin.strip())

punctuation_chars = ["'", '"', ",", ".", "!", ":", ";", '#', '@']

negative_words = []
with open("negative_words.txt") as pos_f:
    for lin in pos_f:
        if lin[0] != ';' and lin[0] != '\n':
            negative_words.append(lin.strip())

def strip_punctuation(strng):

    for letter in strng:
        if letter in punctuation_chars: #statement to check if letter is present in string
            strng=strng.replace(letter,'')
    return strng

def get_pos(tweet):

    tweet=tweet.replace("/n",' ')   #To remove multiple lines
    pos_count=0
    tweet_wrds=strip_punctuation(tweet).split(" ")
    for wrd in tweet_wrds:
        if wrd in positive_words:
            pos_count=pos_count+1
    return pos_count

def get_neg(tweet):

    neg_count=0
    tweet_wrds=strip_punctuation(tweet).split(" ")
    for wrd in tweet_wrds:
        if wrd in negative_words:
            neg_count=neg_count+1
    return neg_count

with open("resulting_data.csv",'w') as resulting_data:
    resulting_data.write('Number of Retweets, Number of Replies, Positive Score, Negative Score, Net Score')
    resulting_data.write('\n')
twitterdata= open("project_twitter_data.csv","r")
twtlst=twitterdata.readlines()
for i in twtlst:

        twtfinal=twitterdata.readline().split(",")
        tweet=strip_punctuation(twtfinal[0])
        if twtfinal[1]!='retweet_count':
            row_strng = '{},{},{},{},{}'.format(twtfinal[1].strip(),twtfinal[2].strip(), get_neg(tweet),get_pos(tweet),(get_pos(tweet)-get_neg(tweet)))
            resulting_data.write(row_strng)
            resulting_data.write('\n')
print(resulting_data.csv)


from distutils.core import setup
import os
from setuptools import setup

version = open('VERSION.txt').read()

setup(
    name='xml-parser',
    version=version,
    license='MIT',
    description='Parse MySQL XML datadump',
    author='John Nguyen',
    author_email='jolnguyen@ucdavis.edu',
    url='https://github.com/JohnlNguyen/xml-parser',
    packages=['parse'],
    zip_safe=False,
)


import sys # Allows us to know about the state of the OS
print(sys.path[0])

import re # Regular expressions
text = "My phone number is 555 555555, country code is 55 and my lucky number is also 5"
result = re.findall("[0-9]+", text) # Find every number in the string.
print(result)

import time # Date and time
timestamp = time.time() # Time in Unix format
print(timestamp)

local = time.localtime() # Local time
result = time.asctime(local) # Formats the date
print(result)

import collections # Mainly for managing lists
numbers = [1, 1, 2, 1, 2, 1, 4, 5, 3, 3, 21]
counter = collections.Counter(numbers) # Creates a dictionary with the frecuency of each number
print(counter)


# -*- coding: utf-8 -*-
# __author__ = 'qingmei'
# last modify 20180521

from biocluster.agent import Agent
from biocluster.tool import Tool
from biocluster.core.exceptions import OptionError
import os

class GenomeGrenameAgent(Agent):
    """
    带注释基因组的拆分
    """
    def __init__(self, parent):
        super(GenomeGrenameAgent, self).__init__(parent)
        options = [
            {"name": "fasta", "type": "string"},     # 下载基因组的fa文件
            {"name": "gff", "type": "string"},       # 下载基因组的gff文件
            {"name": "chromosomelist", "type": "string"}       # 更名文件
        ]
        self.add_option(options)
        self.step.add_steps('genomegrename')
        self.on('start', self.step_start)
        self.on('end', self.step_end)

    def step_start(self):
        self.step.genomegrename.start()
        self.step.update()

    def step_end(self):
        self.step.genomegrename.finish()
        self.step.update()

    def check_options(self):
        if not self.option("fasta"):
            raise OptionError("请设置fasta", code="34503301")
        if not self.option("gff"):
            raise OptionError("请设置 gff", code="34503302")

    def set_resource(self):
        """
        运行所需资源
        """
        self._cpu = 2
        self._memory = '30G'

    def end(self):
        super(GenomeGrenameAgent, self).end()

class GenomeGrenameTool(Tool):
    def __init__(self, config):
        super(GenomeGrenameTool, self).__init__(config)
        self.grename_path = self.config.PACKAGE_DIR + "/wgs/GRename.pl"
        self.perl_path = 'miniconda2/bin/perl '

    def run_genomegrename(self):
        """
        要重新写下！！！
        :return:
        """
        cmd = "{} {} -i {} -g {} -o {}"\
            .format(self.perl_path, self.grename_path, self.option("fasta"), self.option("gff"),
                    self.output_dir+"/ref")
        if self.option("chromosomelist"):
            cmd += " -f {} ".format(self.option("chromosomelist"))
        self.logger.info(cmd)
        self.logger.info("开始进行GenomeGrename")
        command = self.add_command("genomegrename", cmd).run()  # 必须小写，
        self.wait(command)
        if command.return_code == 0:
            self.logger.info("GenomeGrename完成！")
        else:
            self.set_error("GenomeGrename出错！", code="34503301")

    def run(self):
        super(GenomeGrenameTool, self).run()
        self.run_genomegrename()
        self.end()


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

import os
import socket
import logging
import requests
import certifi

try:
    from urllib.parse import urlparse
except ImportError:
    from urlparse import urlparse

from mathtranslate.tencentcloud.common.exception.tencent_cloud_sdk_exception import TencentCloudSDKException

logger = logging.getLogger("tencentcloud_sdk_common")

def _get_proxy_from_env(host, varname="HTTPS_PROXY"):
    no_proxy = os.environ.get("NO_PROXY") or os.environ.get("no_proxy")
    if no_proxy and host in no_proxy:
        return None
    return os.environ.get(varname.lower()) or os.environ.get(varname.upper())

class ProxyConnection(object):
    def __init__(self, host, timeout=60, proxy=None, certification=None, is_http=False):
        self.request_host = host
        self.certification = certification
        if certification is None:
            self.certification = certifi.where()
        self.timeout = timeout
        self.proxy = None
        if is_http:
            proxy = proxy or _get_proxy_from_env(host, varname="HTTP_PROXY")
        else:
            proxy = proxy or _get_proxy_from_env(host, varname="HTTPS_PROXY")
        if proxy:
            self.proxy = {"http": proxy, "https": proxy}
        self.request_length = 0

    def request(self, method, url, body=None, headers={}):
        self.request_length = 0
        headers.setdefault("Host", self.request_host)
        return requests.request(method=method,
                                url=url,
                                data=body,
                                headers=headers,
                                proxies=self.proxy,
                                verify=self.certification,
                                timeout=self.timeout)

class ApiRequest(object):
    def __init__(self, host, req_timeout=60, debug=False, proxy=None, is_http=False, certification=None):
        self.conn = ProxyConnection(host, timeout=req_timeout, proxy=proxy, certification=certification, is_http=is_http)
        url = urlparse(host)
        if not url.hostname:
            if is_http:
                host = "http://" + host
            else:
                host = "https://" + host
        self.host = host
        self.req_timeout = req_timeout
        self.keep_alive = False
        self.debug = debug
        self.request_size = 0
        self.response_size = 0

    def set_req_timeout(self, req_timeout):
        self.req_timeout = req_timeout

    def is_keep_alive(self):
        return self.keep_alive

    def set_keep_alive(self, flag=True):
        self.keep_alive = flag

    def set_debug(self, debug):
        self.debug = debug

    def _request(self, req_inter):
        if self.keep_alive:
            req_inter.header["Connection"] = "Keep-Alive"
        if self.debug:
            logger.debug("SendRequest %s" % req_inter)
        if req_inter.method == 'GET':
            req_inter_url = '%s?%s' % (self.host, req_inter.data)
            return self.conn.request(req_inter.method, req_inter_url,
                              None, req_inter.header)
        elif req_inter.method == 'POST':
            return self.conn.request(req_inter.method, self.host,
                              req_inter.data, req_inter.header)
        else:
            raise TencentCloudSDKException(
                "ClientParamsError", 'Method only support (GET, POST)')

    def send_request(self, req_inter):
        try:
            http_resp = self._request(req_inter)
            headers = dict(http_resp.headers)
            resp_inter = ResponseInternal(status=http_resp.status_code,
                                          header=headers,
                                          data=http_resp.text)
            self.request_size = self.conn.request_length
            self.response_size = len(resp_inter.data)
            logger.debug("GetResponse %s" % resp_inter)
            return resp_inter
        except Exception as e:
            raise TencentCloudSDKException("ClientNetworkError", str(e))

class RequestInternal(object):
    def __init__(self, host="", method="", uri="", header=None, data=""):
        if header is None:
            header = {}
        self.host = host
        self.method = method
        self.uri = uri
        self.header = header
        self.data = data

    def __str__(self):
        headers = "\n".join("%s: %s" % (k, v) for k, v in self.header.items())
        return ("Host: %s\nMethod: %s\nUri: %s\nHeader: %s\nData: %s\n"
                % (self.host, self.method, self.uri, headers, self.data))

class ResponseInternal(object):
    def __init__(self, status=0, header=None, data=""):
        if header is None:
            header = {}
        self.status = status
        self.header = header
        self.data = data

    def __str__(self):
        headers = "\n".join("%s: %s" % (k, v) for k, v in self.header.items())
        return ("Status: %s\nHeader: %s\nData: %s\n"
                % (self.status, headers, self.data))


import glob
import os
import pickle
import sys

sys.path.append(os.getcwd())
sys.path.append("")

import torch

# if gpu is to be used
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")

import numpy as np

from game.hint_play_game import TwoRoundHintGame
from train_qlearner import obs_to_agent

def sample_games(p1, p2, episodes=10000, verbose=False):
    hp1 = p1.hp
    hp2 = p2.hp
    env = TwoRoundHintGame(hp=hp1)

    rewards = []

    #     print(f'agents are {hp1.agent_type} and {hp2.agent_type}')

    for i_episode in range(episodes):
        # Initialize the environment and state
        obs1, info = env.reset()
        if verbose:
            env.render()
        obs1_a1, obs1_a2 = obs_to_agent(obs1, hp=hp1)
        if hp1.agent_type != 'FF':
            obs1_a1 = obs1_a1.reshape(-1, hp1.nlab1 + hp1.nlab2).T
        # P1 select and perform a hint
        # obs1_a1 = torch.tensor(obs1_a1, device=device)
        a1 = p1.select_action(torch.tensor([obs1_a1], device=device), evaluate=True)
        obs2, _, _, _ = env.step(a1)
        if verbose:
            env.render()
        # P2 plays a card
        obs2_a1, obs2_a2 = obs_to_agent(obs2, hp=hp2)
        if hp2.agent_type != 'FF':
            obs2_a2 = obs2_a2.reshape(-1, hp2.nlab1 + hp2.nlab2).T
        a2 = p2.select_action(torch.tensor([obs2_a2], device=device), evaluate=True)
        _, r, _, _ = env.step(a2)
        r = torch.tensor([r[0]], device=device)
        rewards.append(r.numpy()[0])
        if verbose:
            env.render()
    return np.array(rewards)

if __name__ == "__main__":
    agent1s = []
    agent2s = []
    aid = 11
    for filename in sorted(glob.glob(os.path.join("res/Att3_hs_5_l1_3_l2_3_TrueTrue3000000", "*.pkl"))):
        with open(filename, "rb") as f:
            res = pickle.load(f)
            agent1s += [res['p1']]
            agent2s += [res['p2']]
    print(sample_games(agent1s[aid], agent2s[aid], episodes=10, verbose=True))


import sys
n, m = map(int, sys.stdin.readline().split())
arr = [list(map(int, sys.stdin.readline().rstrip())) for _ in range(n)]
def bitmask():
    global max_num
    for i in range(1 << n * m):
        total = 0
        for row in range(n):
            rowsum = 0
            for col in range(m):
                idx = row * m + col
                if i & (1 << idx) != 0:
                    rowsum = rowsum * 10 + arr[row][col]
                else:
                    total += rowsum
                    rowsum = 0
            total += rowsum
        for col in range(m):
            colsum = 0
            for row in range(n):
                idx = row * m + col
                if i & (1 << idx) == 0:
                    colsum = colsum * 10 + arr[row][col]
                else:
                    total += colsum
                    colsum = 0
            total += colsum
        max_num = max(max_num, total)
max_num = 0
bitmask()
print(max_num)


import numpy as np
from collections import deque

class Graph:

    def __init__(self, n):

        self.n = n
        self.adj_list = [[] for i in range(n)]

    def add_edge(self, u, v):

        self.adj_list[u].append(v)

    def add_node(self):

        self.n += 1
        self.adj_list.append([])

        return self.n - 1

    def compute_indegrees(self):

        indeg_list = [0] * self.n

        for node in range(self.n):
            for out_nbr in self.adj_list[node]:
                indeg_list[out_nbr] += 1

        return indeg_list

    def compute_outdegrees(self):

        return [len(self.adj_list[i]) for i in range(self.n)]

    def compute_indegree_distribution(self):

        indeg_list = self.compute_indegrees()
        indeg_dist = [0] * self.n

        for deg in indeg_list:
            indeg_dist[deg] += 1

        return indeg_dist

    def compute_outdegree_distribution(self):

        outdeg_list = self.compute_outdegrees()
        outdeg_dist = [0] * self.n

        for deg in outdeg_list:
            outdeg_dist[deg] += 1

        return outdeg_dist

    def compute_diameter(self, check_inf=False):

        dp = np.full((self.n, self.n), np.inf)

        for node in range(self.n):
            for nbr in self.adj_list[node]:
                dp[node, nbr] = 1

        for k in range(self.n):
            dp = np.minimum(
                dp, np.expand_dims(dp[:, k], axis=1)
                + np.expand_dims(dp[k, :], axis=0))

        diameter = np.max(dp) if not check_inf else np.max(dp[dp != np.inf])

        return 'infinity' if diameter == np.inf else diameter

    def dfs_rev(self, adj_rev, u, visited, dq):
        visited[u] = True

        for v in adj_rev[u]:
            if not visited[v]:
                self.dfs_rev(adj_rev, v, visited, dq)

        dq.appendleft(u)

    def dfs(self, u, visited, comp):
        visited[u] = True
        comp.append(u)

        for v in self.adj_list[u]:
            if not visited[v]:
                self.dfs(v, visited, comp)

    def scc(self):

        adj_rev = [[] for _ in range(self.n)]

        for node in range(self.n):
            for nbr in self.adj_list[node]:
                adj_rev[nbr].append(node)

        dq = deque()
        visited = [False for _ in range(self.n)]

        for u in range(self.n):
            if not visited[u]:
                self.dfs_rev(adj_rev, u, visited, dq)

        visited = [False for _ in range(self.n)]
        comp_list = []
        for u in list(dq):
            if not visited[u]:
                comp = []
                comp_list.append(comp)
                self.dfs(u, visited, comp)

        return comp_list

    def get_edge_list(self):

        edge_list = []
        for node in range(self.n):
            for nbr in self.adj_list[node]:
                edge_list.append((node, nbr))

        return edge_list


import json
import pandas as pd

total1 =[]
total2 =[]

# 读取json文件内容,返回字典格式
with open('./a.json','r',encoding='utf8')as fp:
    json_data = json.load(fp)
    print('这是读取到文件数据的数据类型：', type(json_data))
    data_list=json_data['data']
    print(len(data_list))

with open('./b.json','r',encoding='utf8')as fp:
    jason_date = json.load(fp)
    data_dict=jason_date['data']['stocks']
    print(len(data_dict))
list1=[]
list2=[]
list3=[]
list4=[]

for i in data_list.values():
    data1=i['symbol'] #用来存放股票代码
    data2=i['stock_chi_name'] #变量用来存放股票名称
    list1.append(data1)
    list2.append(data2)

for data in data_dict:
    data3=data['desc']
    data4=data['symbol']
    list3.append(data3)
    list4.append(data4)

total_data = []

for i in range(len(data_dict)):
    combined_data = []
    combined_data.append(list1[i+1])
    combined_data.append(list2[i+1])
    combined_data.append(list3[i])
    combined_data.append(list4[i])
    total_data.append(combined_data)
    # print(combined_data)

df = pd.DataFrame(total_data)
df.columns =['股票代码', '股票名称',"股票归因","股票代码"]
# print(df)
df.to_csv(r'./res.csv',index=True,encoding='utf_8_sig')


from flask.globals import g
from flask_jwt_extended.view_decorators import jwt_required
from flask_restful import Resource
from flask_restful.reqparse import RequestParser
from app.models import RankingListTest, Task
from app.models.RankingListModel import RankingList
from app.Responses import ApiErrorResponse, ApiResponse

class RankingListsResource(Resource):
    def __init__(self):
        self.reqparse = RequestParser()
        self.reqparse.add_argument('listname', type=str, required=True, location='json')
        self.reqparse.add_argument('shortname', type=str, required=True, location='json')
        self.reqparse.add_argument('resultsValidDays', type=str, required=False, location='json')

    @RankingList.from_request(many=True)
    def get(self):
        return ApiResponse(g.rankinglists).response()

    @jwt_required
    def post(self):
        args = self.reqparse.parse_args()

        rankinglist = RankingList(args['listname'], args['shortname'])

        rankinglist.update(self.reqparse)

        try:
            rankinglist.save()
        except Exception as e:
            return ApiErrorResponse(str(e)).response()

        return ApiResponse(rankinglist).response()

class RankingListResource(Resource):
    def __init__(self):
        self.reqparse = RequestParser()
        self.reqparse.add_argument('listname', type=str, required=False, location='json')
        self.reqparse.add_argument('shortname', type=str, required=False, location='json')
        self.reqparse.add_argument('resultsValidDays', type=str, required=False, location='json')

    @RankingList.from_request
    def get(self, id):
        return ApiResponse(g.rankinglist).response()

    @jwt_required
    @RankingList.from_request
    def patch(self, id):
        g.rankinglist.update(self.reqparse)

        try:
            g.rankinglist.save()
        except Exception as e:
            return ApiErrorResponse(str(e)).response()

        return ApiResponse(g.rankinglist).response()

    @jwt_required
    @RankingList.from_request
    def delete(self, id):
        try:
            g.rankinglist.delete()
        except Exception as e:
            return ApiErrorResponse(str(e)).response()

        return ApiResponse(response_code=204).response()

class RankingListTestsResource(Resource):
    def __init__(self):
        self.reqparse = RequestParser()
        self.reqparse.add_argument('testcode', type=str, required=True, location='json')
        self.reqparse.add_argument('includedMarks', type=int, required=True, location='json')
        self.reqparse.add_argument('order', type=str, required=True, location='json')
        self.reqparse.add_argument('grouping', type=str, required=True, location='json')
        self.reqparse.add_argument('minMark', type=float, required=True, location='json')
        self.reqparse.add_argument('roundingPrecision', type=int, required=True, location='json')
        self.reqparse.add_argument('markType', type=str, required=True, location='json')

    @RankingList.from_request
    def get(self, id):
        try:
            tests = RankingListTest.load_many(g.rankinglist.tests)
        except ApiErrorResponse as e:
            return e.response()

        return ApiResponse(tests).response()

    @jwt_required
    @RankingList.from_request
    def post(self, id):
        test = RankingListTest()
        test.update(self.reqparse)

        try:
            g.rankinglist.add_test(test)
            test.save()
        except ApiErrorResponse as e:
            return e.response()
        except Exception as e:
            return ApiErrorResponse(str(e)).response()

        return ApiResponse(test).response()

class RankingListTasksResource(Resource):
    @RankingList.from_request
    def get(self, id):
        try:
            tasks = Task.load_many(g.rankinglist.tasks)
        except ApiErrorResponse as e:
            return e.response()

        return ApiResponse(tasks).response()


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Thu Sep 19 14:03:41 2019

@author: thomaram
"""

import pandas as pd

path="~/Hackathon2019/Make-me-a-rock/Data/"

# %notebook inline

data_raw = pd.read_csv(path+"data_1.csv", delimiter=",", encoding = 'cp865')

npd_litho = pd.read_csv(path+"npd_litho_strat.csv", delimiter=";",encoding = 'cp865')

xyz = pd.read_csv(path+"xyz_combined.csv", delimiter=";",encoding ='cp865')

data_1 = data_raw[['wellbore','md','kh_kl','kv_kl','well_id','phi_best','clean_litho','grain_density','grain_size','sorting','cement']]
data_1 = data_1.replace(',','.',regex=True)

after_grouping = data_1.copy()

after_grouping = after_grouping.iloc[0:0]

data_1['kh_kl']=pd.to_numeric(data_1['kh_kl'], downcast='float', errors='coerce')

groups = data_1.groupby(by='wellbore', sort=False)

for name, group in groups:

    average_kh = group.loc[group['kh_kl'].isnull()==False,'kh_kl'].mean()

    group.loc[group['kh_kl'].isnull()==True,'kh_kl'] = average_kh

    after_grouping = pd.concat([after_grouping, group])

data_clean = after_grouping.loc[data_1['wellbore'].isnull()==False]

# first_batch = data_clean.copy()

first_batch = data_clean.loc[ (data_clean['kh_kl'].isnull()==False) & (data_clean['phi_best'].isnull()==False)]

first_batch['well_id']=pd.to_numeric(first_batch['well_id'], downcast='integer', errors = 'coerce')

tmp = pd.merge(first_batch, npd_litho, how='inner', left_on='well_id', right_on='npd_id')

# tmp.loc(tmp['md']=='339..90','md')=3399

tmp['md']=pd.to_numeric(tmp['md'], downcast='float',errors = 'coerce')

tmp['md_top']=pd.to_numeric(tmp['md_top'], downcast='float',errors = 'coerce')

tmp['md_bottom']=pd.to_numeric(tmp['md_bottom'], downcast='float',errors = 'coerce')

result = tmp.loc[(tmp['md']>=tmp['md_top'] ) & (tmp['md']<tmp['md_bottom'])]

result = result.drop(columns = ['wellbore_x', 'md_top','md_bottom','npd_id'])

result2 = pd.merge(result, xyz, how='inner', left_on='well_id', right_on='npd_id')

result2['phi_best']=pd.to_numeric(result2['phi_best'], downcast='float',errors = 'coerce')

result2.loc[result2['cement']=='well ','cement']='well cemented'

result2.loc[result2['clean_litho']=='sandstone','clean_litho']='Sandstone'

result2.loc[result2['clean_litho']=='marl (-y)','clean_litho']='Marlstone'

result2.loc[result2['clean_litho']=='siltstone','clean_litho']='Siltstone'

result2.loc[result2['clean_litho']=='Sandstone ','clean_litho']='Sandstone'

second_batch=result2.copy()

second_batch.insert(11, 'cement_grade', 0, allow_duplicates = False)

#pd.unique(second_batch['cement'])

second_batch=second_batch.replace('well well cemented cemented','well cemented', regex=True)

second_batch=second_batch.replace('well well cemented very well cemented','well cemented very well cemented', regex=True)

second_batch=second_batch.replace('well well cemented fair cemented','well cemented fair cemented', regex=True)

second_batch=second_batch.replace('well ','well cemented', regex=True)

second_batch=second_batch.replace('well cementedcemented','well cemented', regex=True)

second_batch=second_batch.replace('fair cemented well cemented','well cemented fair cemented', regex=True)

second_batch['cement_grade'] = second_batch['cement']

second_batch['cement_grade']=second_batch.loc[:,'cement_grade'].replace('very poorly cemented unconsolidated','14', regex=True)

second_batch['cement_grade']=second_batch.loc[:,'cement_grade'].replace('unconsolidated','15', regex=True)

second_batch['cement_grade']=second_batch.loc[:,'cement_grade'].replace('consolidated','1', regex=True)

second_batch['cement_grade']=second_batch.loc[:,'cement_grade'].replace('well cemented very well cemented','3', regex=True)

second_batch['cement_grade']=second_batch.loc[:,'cement_grade'].replace('well cemented fair cemented','5', regex=True)

second_batch['cement_grade']=second_batch.loc[:,'cement_grade'].replace('poorly cemented very poorly cemented','11', regex=True)

second_batch['cement_grade']=second_batch.loc[:,'cement_grade'].replace('very poorly cemented fair cemented','12', regex=True)

second_batch['cement_grade']=second_batch.loc[:,'cement_grade'].replace('very poorly cemented','13', regex=True)

second_batch['cement_grade']=second_batch.loc[:,'cement_grade'].replace('very well cemented','2', regex=True)

second_batch['cement_grade']=second_batch.loc[:,'cement_grade'].replace('fair well cemented','7', regex=True)

second_batch['cement_grade']=second_batch.loc[:,'cement_grade'].replace('well cemented','4', regex=True)

second_batch['cement_grade']=second_batch.loc[:,'cement_grade'].replace('fair cemented','8', regex=True)

second_batch['cement_grade']=second_batch.loc[:,'cement_grade'].replace('poorly cemented','10', regex=True)

second_batch['cement_grade']=second_batch.loc[:,'cement_grade'].replace('cemented','6', regex=True)

second_batch.to_csv(path+"third_batch.csv", encoding = 'cp865')


#!../dependencies/python/Python32/python3
import os
import sys
import shutil
from argparse import ArgumentParser
from analyze_juliet import AnalyzeJuliet

def main():
    """
    Analyze all juliet test cases with pmd.
    :return: None
    """
    # define, parse, and validate arguments
    parser = ArgumentParser('main.py')
    parser.add_argument('--juliet_dir', help='path/to/juliet/test/cases', type=str, default='..\juliet_test_cases')
    parser.add_argument('--cwe_mappings_file', help='path/to/cwe/mapping/file', type=str, default='cwe_mappings.txt')
    parser.add_argument('--ruleset_template_file', help='path/to/ruleset/template/file', type=str,
                        default='ruleset_template.xml')
    parser.add_argument('--pmd_exec', help='path/to/pmd/exec', type=str,
                        default='..\..\pmd\pmd-dist\\target\pmd-bin-6.13.0-SNAPSHOT\\bin\pmd.bat')
    parser.add_argument('--outdir', help='path/to/outdir', type=str, default='outdir')
    parser.add_argument('--mp', help='max number of pmd processes to spawn at one time', type=int, default=25)
    parser.add_argument('--keep_copied_source', help='keep copied source files', action='store_true')
    parser.add_argument('--use_multifile_tests', help='include tests with multiple files', action='store_true')
    parser.add_argument('-q', '--quiet', help='supress terminal output', action='store_true')
    args = validate_args(parser.parse_args())

    # analyze
    AnalyzeJuliet(
        args.juliet_dir,
        parse_cwe_mappings_file(args.cwe_mappings_file),
        args.ruleset_template_file,
        args.pmd_exec,
        args.outdir,
        args.mp,
        not args.keep_copied_source,
        not args.use_multifile_tests,
        args.quiet)

def validate_args(args):
    """
    Validate command line arguments. Calls sys.exit if error is found.

    :param args: ArgumentParser arguments
    :return: None
    """
    check_exists(args.juliet_dir, 'juliet_dir', False, args.quiet)
    if not os.path.isdir(os.path.join(args.juliet_dir, 'src', 'testcases')):
        sys.exit('invalid argument "juliet_dir", make sure it is a path to the juliet test cases')

    check_exists(args.cwe_mappings_file, 'cwe_mappings_file', True, args.quiet)

    check_exists(args.ruleset_template_file, 'ruleset_template_file', True, args.quiet)

    check_exists(args.pmd_exec, 'pmd_exec', True, args.quiet)

    if os.path.isdir(args.outdir):
        shutil.rmtree(args.outdir)
    os.makedirs(args.outdir)  # TODO verify we can make ruleset outdir

    return args

def check_exists(path, desc, check_file=True, quiet=False):
    """
    Check if a path exists as a dir or file. Calls sys.exit if error is found.

    :param path: desired/path
    :param desc: description for printing
    :param check_file: True to check for file, False to check for dir
    :param quiet: supress output
    :return: None
    """
    tmp_str = '"%s" -> "%s"' % (desc, path)
    if os.path.exists(path):
        type_ = 'dir'
        if check_file:
            type_ = 'file'
        if (check_file and os.path.isfile(path)) or (not check_file and os.path.isdir(path)):
            if quiet:
                print('found argument: %s.' % tmp_str)
            return

        sys.exit('invalid argument: %s, should be a %s.' % (tmp_str, type_))
    else:
        sys.exit('invalid argument: %s, doesnt exist.' % tmp_str)

def parse_cwe_mappings_file(cwe_mappings_file):
    """
    Parse the cwe mappings file. Call sys exit if file is invalid

    :param cwe_mappings_file: path/to/cwe/mappings/file
    :return: cwe_mappings list
    """
    cwe_mappings = []
    with open(cwe_mappings_file, 'r') as fin:
        for idx, line in enumerate(fin.readlines()):
            error = False
            line = line.replace(" ", "")
            if len(line) > 0:
                if line[:1] == "#":
                    continue
            line = line.split(',')
            cwe = 0
            priority = 0
            pmd_rule = ''
            if len(line) == 2:
                try:
                    cwe = int(line[0])
                except:
                    error = True
                pmd_rule = line[1]
                if len(pmd_rule) == 0:
                    error = True
            else:
                error = True
            if error:
                sys.exit('error in cwe_mappings file: "%s", line: "%d", must be in the following format:'
                         '"cwe_number,pmd_rule_path,priority"' % (cwe_mappings_file, idx))
            cwe_mappings.append([cwe, pmd_rule])

    if len(cwe_mappings) == 0:
        sys.exit('error in cwe_mappings file: "%s", empty file.' % cwe_mappings_file)

    return cwe_mappings

if __name__ == '__main__':
    main()


#read write append log file based on arguments we pass in
#data persistance
#with statments are cool bc they automatically close the file
import sys
#datetime method from datetime module:
from datetime import datetime
from time import time

global file_name
global action

def read_log():
	"""Reads from the log file"""
	with open(file_name, 'r') as log_messages:
		print(log_messages.read())

def log(message):
	"""log a message to the log file"""
	"""Arguments: message--any string in command line after calling file"""
	#a appends message, w overwrites, r reads
	with open(file_name, 'a') as logger:
		timestamp = time()
		logger.write("{0}: {1}\n".format(
			datetime.fromtimestamp(timestamp).strftime('%Y-%m-%d %H:%M:%S'),
			str(message)
			))

if __name__ == '__main__':
    try:
        file_name = sys.argv[1]
        action = sys.argv[2]
        if action == 'r':
                read_log()
        elif action == 'w' or action == 'a':
                log(" ".join(sys.argv[3:]), action)
                read_log()

    except IndexError:
        pass


"""
isort:skip_file
"""
from __future__ import annotations

from typing import TYPE_CHECKING, Generator

import pytest
import logging
from pathlib import Path

from barney.BarneyApp import Barney
from barney.Utilities.PathMapper import pathToQUrl, qUrlToPath  # noqa: F401

if TYPE_CHECKING:
    from pytestqt import qtbot
    from barney.views.MainWindow import MainWindow

logger = logging.getLogger()

@pytest.fixture(scope="session")
def app() -> Generator[Barney, None, None]:
    barney = Barney()
    yield barney
    barney.qtapp.processEvents()
    logger.removeHandler(barney.qtapp.handler)
    barney.qtapp.quit()

@pytest.fixture
def viewer(app: Barney, qtbot: qtbot) -> MainWindow:
    app.qtapp.startBarney()
    viewer = app.qtapp.mainView
    qtbot.addWidget(viewer)
    qtbot.waitExposed(viewer)
    yield viewer
    viewer.clear()
    viewer._model.fileProxyModel.setSourceModel(None)
    viewer.close()

@pytest.fixture(scope="function")
def dbPath_relativeEntries():
    dbPath = Path(__file__).parents[1] / "data" / "relative_paths.db"
    return pathToQUrl(dbPath)

@pytest.fixture(scope="function")
def wavPath():
    wavPath = Path(__file__).parents[1] / "data" / "speech-mwm.wav"
    return pathToQUrl(wavPath)

@pytest.fixture(scope="function")
def dirPath():
    dirPath = Path(__file__).parents[1]
    return pathToQUrl(dirPath)


from copy import deepcopy
from typing import List

class Solution:
    def findContinuousSequence(self, target: int) -> List[List[int]]:
        left, right = 1, 1
        ans, res, sums = [], [], 0
        while right < target:
            sums += right
            while sums > target:
                sums -= left
                res.pop(0)
                left += 1
            res.append(right)
            right += 1
            if sums == target:  # 如果找到一个符合条件的区间
                ans.append(deepcopy(res))
        return ans

LeetCode/Problems/57 - II. 和为s的连续正数序列/python.py


""" Download as PDF
"""
import os
import logging
import tempfile
from urllib2 import urlparse
from Products.Five.browser import BrowserView
from Products.statusmessages.interfaces import IStatusMessage
from zope.publisher.interfaces import NotFound
from zope.component import queryAdapter, queryUtility, queryMultiAdapter
from eea.converter.interfaces import IPDFOptionsMaker, IHtml2Pdf
from eea.converter.config import TMPDIR

logger = logging.getLogger('eea.converter')

class Pdf(BrowserView):
    """ Download as PDF using @@pdf.cover and @@pdf.body browser views
    """
    def __init__(self, context, request):
        super(Pdf, self).__init__(context, request)
        self._cookies = None

    @property
    def cookies(self):
        """ Cookies
        """
        if self._cookies is None:
            ac_cookie = self.request.cookies.get('__ac', None)
            if not ac_cookie:
                self._cookies = {}
                return self._cookies

            ## There is a bug with wkhtmltopdf --cookie param
            ## Thus we'll use --cookie-jar
            ## EEA ticket #21958. wkhtmltopdf tickets #1870, #1903

            url = urlparse.urlparse(self.context.absolute_url())
            domain = url.hostname

            # Also allow CDN resources
            if domain.startswith(u"www."):
                domain = domain.replace(u"www.", u".", 1)

            cookie = u"__ac={cookie}; domain={domain}; path=/;".format(
                cookie=ac_cookie,
                domain=domain
            )

            with tempfile.NamedTemporaryFile(
                    prefix='eea.converter.', suffix='.cookie.jar',
                    dir=TMPDIR(), delete=False) as ofile:
                ofile.write(cookie)
                self._cookies = ofile.name
        return self._cookies

    def options(self, section=u'', margin=True):
        """ Get options for given section
        """
        return queryAdapter(self.context, IPDFOptionsMaker, name=section)

    def make_cover(self, dry_run=False, **kwargs):
        """
        Unfortunately wkhtmltopdf can't make cover and body with different
        margins, thus generate them separately
        """
        cover = self.options('pdf.cover')
        cover = cover()
        if not cover:
            return None

        options = self.options('')
        options._margin = False
        options._cookies = self.cookies
        timeout = options.timeout

        options = options()
        options.extend(cover)

        cleanup = []
        if isinstance(self.cookies, (str, unicode)):
            cleanup.append(self.cookies)

        html2pdf = queryUtility(IHtml2Pdf)
        return html2pdf(options, timeout, dry_run, cleanup=cleanup)

    def make_back_cover(self, dry_run=False, **kwargs):
        """ Back cover
        """
        cover = self.options('pdf.cover.back')
        cover = cover()
        if not cover:
            return None

        options = self.options('')
        options._margin = False
        options._cookies = self.cookies
        timeout = options.timeout

        options = options()
        options.extend(cover)

        html2pdf = queryUtility(IHtml2Pdf)
        return html2pdf(options, timeout, dry_run)

    def make_disclaimer(self, dry_run=False, **kwargs):
        """
        Generate pdf disclaimer
        """
        disclaimer = self.options('pdf.disclaimer')
        disclaimer = disclaimer()
        if not disclaimer:
            return None

        options = self.options('')
        options._margin = False
        options._cookies = self.cookies
        timeout = options.timeout

        options = options()
        options.extend(disclaimer)

        html2pdf = queryUtility(IHtml2Pdf)
        return html2pdf(options, timeout, dry_run)

    def make_body(self, dry_run=False, **kwargs):
        """ Override pdf converter
        """
        body = self.options('pdf.body')
        toc = body.toc
        toc_links = body.toc_links

        body = body()
        if not body:
            return None

        options = self.options('')
        options._cookies = self.cookies
        options._outline = toc_links
        timeout = options.timeout

        options = options()
        options.extend(body)

        cleanup = [toc] if toc else []
        if isinstance(self.cookies, (str, unicode)):
            cleanup.append(self.cookies)

        html2pdf = queryUtility(IHtml2Pdf)
        return html2pdf(options, timeout, dry_run, cleanup=cleanup)

    def make_concat(self, dependencies, default='', dry_run=False, **kwargs):
        """ Concat pdfs
        """
        timeout = self.options('').timeout
        html2pdf = queryUtility(IHtml2Pdf)
        return html2pdf.concat(dependencies, default=default,
                               timeout=timeout, dry_run=dry_run)

    def make_pdf(self, dry_run=False, **kwargs):
        """ Compute pdf
        """
        dependencies = []

        cover = self.make_cover(dry_run=True)
        if cover:
            dependencies.append(cover)

        disclaimer = self.make_disclaimer(dry_run=True)
        if disclaimer:
            dependencies.append(disclaimer)

        body = self.make_body(dry_run=True)
        if body:
            dependencies.append(body)

        backcover = self.make_back_cover(dry_run=True)
        if backcover:
            dependencies.append(backcover)

        converter = self.make_concat(dependencies, default=body, dry_run=True)

        if dry_run:
            return converter

        data = ''
        if not converter:
            return data

        converter.run()
        output = converter.path
        if output and os.path.exists(output):
            data = open(output, 'rb').read()

        converter.cleanup()
        return data

    @property
    def filename(self):
        """ Generates the name for the PDF file.
        If the context title does not contain non-ascii characters,
        we'll use it.
        Otherwise we'll rewrite it using normalize string.
        """
        try:
            name = self.context.title.encode('ascii')
        except (UnicodeDecodeError, UnicodeEncodeError, ):
            name = self.context.id
        return '%s.pdf' % name

    def __call__(self, **kwargs):

        support = queryMultiAdapter((self.context, self.request),
                                    name='pdf.support')
        if not getattr(support, 'can_download', lambda: False)():
            raise NotFound(self.context, self.__name__, self.request)

        # Cheat condition @@plone_context_state/is_view_template
        self.request['ACTUAL_URL'] = self.context.absolute_url()

        data = self.make_pdf()
        if not data:
            IStatusMessage(self.request).addStatusMessage(
                "An error occurred while downloading your PDF file. "
                "Please try again later.",
                type='error')
            return self.request.response.redirect(self.context.absolute_url())

        self.request.response.setHeader("Content-type", "application/pdf")
        self.request.response.setHeader("X-Robots-Tag", "noindex")
        self.request.response.setHeader('Content-Disposition',
            'attachment; filename="%s"' % self.filename
        )
        return data


import osmnx as ox
import pandas as pd
import matplotlib.pyplot as plt

# Rough map of Rochester
rochester_city = ox.geocode_to_gdf('Rochester, NY, USA')
ax = ox.project_gdf(rochester_city).plot()
_ = ax.axis('off')

# Node visibility
rochester_graph = ox.graph_from_place('Rochester, NY, USA', network_type='drive')
ox.plot_graph(rochester_graph)

# Setting background and edge colours in the map
colors = plt.rcParams['axes.prop_cycle'].by_key()['color']
ox.plot.plot_graph(rochester_graph, bgcolor='#FFFFFF', node_color=colors[0], edge_color='gray', node_size=5)

# Sample bounding box in Rochester, NY - River Bend Shelter - Highland park boxed area
north, south, east, west = 43.12671,43.13403,-77.63630,-77.60385

# create a network from the above bounded box
G = ox.graph_from_bbox(north, south, east, west, network_type="drive_service")
colors = plt.rcParams['axes.prop_cycle'].by_key()['color']
ox.plot.plot_graph(G, bgcolor='#FFFFFF', node_color=colors[0], edge_color='gray', node_size=5)

# Number of nodes within the selected area
count=0
for i in G.nodes:
    count=count+1
print("The number of nodes within the selected location: River Bend Shelter - Highland park boxed area")
print(count)

# Storing nodes in dataframe
cords = pd.DataFrame()
loc= []
for i in G.nodes:
    loc.append(i)

# Create a column name Location in the dataframe for nodes where
loc_data=pd.DataFrame(loc)
loc_data.rename(columns = {0:'Locations'}, inplace = True)

# Appending lats and longitudes
latitude = []
longitude = []
for i in loc:
    latitude.append(G.nodes[i].get('y'))
    longitude.append(G.nodes[i].get('x'))

# create 2 columns in the dataframe latitude and longitude and add the respective lists to the dataframe
loc_data['Latitudes'] = latitude
loc_data['Longitudes'] = longitude

# Print updated dataframe
print(loc_data)


import numpy as np
from sklearn.cluster import KMeans
from numpy.linalg import norm
from scipy.spatial.distance import euclidean
from sklearn.preprocessing import normalize

def simul_m(K, alpha, M=100000):
    X = np.random.dirichlet(np.ones(K)*alpha, M)
    kmeans = KMeans(n_clusters=K, n_jobs=1, n_init=1, max_iter=5000).fit(X)
    centers = kmeans.cluster_centers_

    cent = 1./K * np.ones(K)
    m = np.sqrt(K*(K-1.))/norm(centers - cent, axis=1).sum()
    return np.ones(K)*m

def get_beta(cent, centers, m, lda=False, pois=False):
    betas = np.array([cent + m[x]*(centers[x,:] - cent) for x in range(centers.shape[0])])
    if lda:
        betas[betas<0] = 0
        betas = normalize(betas, 'l1')
    if pois:
        betas[betas<0] = 0

    return betas

def gdm(X, K, n_jobs=1, n_init=10, lda=False, pois=False):
    cent = np.mean(X, axis=0)
    kmeans = KMeans(n_clusters=K, n_jobs=n_jobs, n_init=n_init).fit(X)
    centers = kmeans.cluster_centers_
    labels = kmeans.labels_

    m = []
    for k in range(K):
        k_dist = euclidean(cent, centers[k])
        Rk = max(np.apply_along_axis(lambda x: euclidean(cent, x), 1, X[labels==k,:]))
        m.append(Rk/k_dist)

    beta = get_beta(cent, centers, m, lda=lda, pois=pois)

    return beta

def match_covariance_gaus(X, gam_centers, alpha_range, alpha_m, sigma2_deconv):

    K, D = gam_centers.shape
    x_mean = X.mean(axis=0)
    x_cov = np.dot((X-x_mean).T, X-x_mean)/X.shape[0]
    P = -np.ones((K,K)) + np.eye(K)*K
    obj_value = []
    for a,m in zip(alpha_range,alpha_m):
        alpha_const = 1./(K**2*(K*a+1))
        beta = get_beta(x_mean, gam_centers, np.ones(K)*m, lda=False, pois=False)
        est_cov = alpha_const*np.dot(np.dot(beta.T,P),beta) + np.eye(D)*sigma2_deconv
        obj_value.append(norm(x_cov-est_cov))

    print('Estimated alpha %f\n' % (alpha_range[np.argmin(obj_value)]))
    return alpha_m[np.argmin(obj_value)]*np.ones(K)

def match_covariance_pois(X, gam_centers, alpha_range, alpha_m):

    K, D = gam_centers.shape
    x_mean = X.mean(axis=0)
    x_cov = np.dot((X-x_mean).T, X-x_mean)/X.shape[0]
    P = -np.ones((K,K)) + np.eye(K)*K
    obj_value = []
    for a,m in zip(alpha_range,alpha_m):
        alpha_const = 1./(K**2*(K*a+1))
        beta = get_beta(x_mean, gam_centers, np.ones(K)*m, lda=False, pois=False)
        est_cov = alpha_const*np.dot(np.dot(beta.T,P),beta) - np.diag(beta.mean(axis=0))
        obj_value.append(norm(x_cov-est_cov))

    print('Estimated alpha %f\n' % (alpha_range[np.argmin(obj_value)]))
    return alpha_m[np.argmin(obj_value)]*np.ones(K)

def match_covariance_lda(X, gam_centers, alpha_range, alpha_m, Nm):

    K, D = gam_centers.shape
    x_mean = X.mean(axis=0)
    x_cov = np.dot((X-x_mean).T, X-x_mean)/X.shape[0]
    P = -np.ones((K,K)) + np.eye(K)*K
    obj_value = []
    for a,m in zip(alpha_range,alpha_m):
        alpha_const = 1./(K**2*(K*a+1))
        beta = get_beta(x_mean, gam_centers, np.ones(K)*m, lda=False, pois=False)
        est_cov = alpha_const*np.dot(np.dot(beta.T,P),beta)*(1-1./Nm) + np.diag(beta.mean(axis=0))/Nm - np.outer(beta.mean(axis=0),beta.mean(axis=0))/Nm
        obj_value.append(1e6*norm(x_cov-est_cov))

    print('Estimated alpha %f\n' % (alpha_range[np.argmin(obj_value)]))
    return alpha_m[np.argmin(obj_value)]*np.ones(K)

def vlad(X, K, alpha=None, it=1000, n_jobs=1, n_init=10, deconv=True, lda=False, pois=False, alpha_m_path=None, Nm=None):

    cent = X.mean(axis=0)

    u, s, v = np.linalg.svd(X-cent, full_matrices=False)

    if deconv:
        sigma2_deconv = s[K]**2/X.shape[0]
        s = np.sqrt(s[:K]**2 - s[K]**2)
    else:
        s = s[:K]

    X_svd = u[:,:K]

    v_back = s.reshape(-1,1)*v[:K]

    if lda:
        init_centers = KMeans(n_clusters=K, n_jobs=n_jobs, n_init=n_init, max_iter=10).fit(X-cent).cluster_centers_
        v_to_svd = v[:K].T * 1./s
        init_centers = np.dot(init_centers, v_to_svd)
        u_centers = KMeans(n_clusters=K, init=init_centers, max_iter=it, n_init=1).fit(X_svd).cluster_centers_
    else:
        u_centers = KMeans(n_clusters=K, n_init=n_init, max_iter=it, n_jobs=n_jobs).fit(X_svd).cluster_centers_

    D_centers = np.dot(u_centers, v_back) + cent

    if alpha is None:
        alpha_range = np.arange(0.05,6.01,0.01)
        if alpha_m_path is None:
            print('Make grid for learning alpha - this may take some time. Needs to be done once for a fixed K (independent of data)')
            from joblib import Parallel, delayed
            alpha_m = Parallel(n_jobs=n_jobs)(delayed(simul_m)(K, a) for a in alpha_range)
            alpha_m = [a_m[0] for a_m in alpha_m]
            np.save('alpha_m_%d' % K, np.array(alpha_m))
            print('Done making the grid and saved it - reuse on next run')
        else:
            alpha_m = np.load(alpha_m_path)
        if deconv:
            m = match_covariance_gaus(X, D_centers, alpha_range, alpha_m, sigma2_deconv)
        elif pois:
            m = match_covariance_pois(X, D_centers, alpha_range, alpha_m)
        elif lda:
            m = match_covariance_lda(X, D_centers, alpha_range, alpha_m, Nm)
        else:
            print('Unknown distribution')

    else:
        m = simul_m(K, alpha)

    beta = get_beta(cent, D_centers, m, lda=lda, pois=pois)

    return beta


import numpy as np
from scipy.optimize  import fsolve
from my_de_solver.solver_functions import solve_ode
from my_de_solver.shooting_functions import shooting
from my_de_solver.pde_solver_functions import forward,backward,crank,pde_solver

from scipy.optimize  import fsolve
import matplotlib.pyplot as plt
import sys

# plotting funciton
def plot_func(discretisation,found_inits,params):
    '''
    Uses matplotlib to show result for each different type of discretisation

    Parameters
    ----------
    discretisation  :   function
        The type of discretisation for continuation either shooting, fsolve, pde_solver or solve_ode which type depends on the plot that is shown
    found_inits : number/numpy array
        The inital conditons/solutions/steady_states/roots for the function at all of the values of the parameter varied over
    params_from :   number/numpy array
        The values of the parameter that was varied.
    '''
    if discretisation==shooting: # if type of discretisation
        np.asarray(found_inits)
        for i in range(0,len(found_inits[0])):
            xs=[]
            for array in found_inits:
                xs.append(array[i])
            plt.plot(params, xs,label='initial condition '+str(i+1)) # plotting params against found initial conditions
        # plotting conditons
        plt.xlabel('parameter being varied')
        plt.ylabel('inital conditions found')
        plt.legend()
        plt.show()

    elif discretisation==pde_solver: # if type of discretisation
        # plotting conditons
        plt.plot(params,found_inits,'ro',label='steady states\nfor varied\nboundry conditions') # plotting params against found steady states
        plt.xlabel('varied parameter (a)')
        plt.ylabel('steady states')
        plt.legend(loc='best')
        plt.show()

    elif discretisation==solve_ode: # if type of discretisation
        for set_of_solutions in found_inits:
            # plotting conditons
            plt.plot(set_of_solutions[0], set_of_solutions[1]) # plotting found solutions against time

        plt.xlabel("t")
        plt.ylabel("variable(s)")
        plt.show()

    elif discretisation==fsolve: # if type of discretisation
        plt.plot(params, found_inits) # plotting params against found roots
        # plotting conditons
        plt.xlabel('varied parameter')
        plt.ylabel('roots to quadratic')
        plt.show()

# appending found solutions and params to arrays
def append_inits_params(found_inits,init_guess,params_from,param_from):
    '''
    Simply appends the found solutions and parameter values to arrays so that they can be stored after each interation

    Parameters
    ----------
    found_inits : number/numpy array
        The inital conditons/solutions/steady_states/roots for the function at all of the values of the parameter varied over
    init_guess : float/numpy array
        The inital guess for the intial conditons of the function you are doing natual parameter continuation for
    params_from :   number/numpy array
        The values of the parameter that was varied.
    param_from :   number/numpy array
        The value of the most recently vaired parameter.

    Returns
    -------
    found_inits : number/numpy array
        The inital conditons/solutions/steady_states/roots for the function at all of the values of the parameter varied over
    params_from :   number/numpy array
        The values of the parameter that was varied.
    '''
     # appending
    found_inits.append(init_guess)
    params_from = np.append(params_from,param_from)
    return found_inits, params_from # returning appended arrays

# main continuation function
def continuation_natural(func, discretisation,plotting='off', tt=100,vars=np.array([1,1]),init_guess=0,init_param=None, param_step_size=0.1,param_from=0, param_to=2, step_size=0.05,n=500, rk_e='--runge',main_param=None, L=1, T=1, mt=30, mx=30,bound_conds=np.array([0,0]), method=forward, type_bc='Dirichlet', period_guess=None,**kwargs):
    '''
    Natural parameter continuation function that will vary through a given parameter 'init_param' and finds the inital conditons/solutions/steady_states/roots from param_from to param_to finding all param_step_size's inbetween.

    Depending on the discretisation given the funtion can use natural parameter discretisation, numerical shooting, solve_ode or fsolve.

    Parameters
    ----------
    For all types of discretisation:
        func :   function
            Differnetial equation or system of ordinary/partial differnetial equations. Defined as a function.
        discretisation  :   function
            The type of discretisation for continuation either shooting, fsolve, pde_solver or solve_ode
        plotting    :   string
            Whether user wishes to plot the results of the chosen type of discretisation (for use: plotting='on'). Defualt to 'off'.
        init_param  :   string
            The parameter chosen to do natural parameter continuation for.
        param_step_size :   float
            The step size of how much the parameter will change by.  Defualt to 0.1
        param_from  :   float
            The starting condition of the parameter.  Defualt to 0
        param_to    :   float
            The final value of the parameter you wish to solve for Defualt to 2

    For discretisation=solve_ode:
        vars : float/numpy array
            The intial conditons of the function you are doing natual parameter continuation for
        tt  :  number
            The target value of t that the funtion will solve up to
        step_size   :   float
            This is the size of the 'step' solve_ode will approximate using. Defualt to 0.05
        n : number
            The number of steps you want to take between the inital value and target
            value. Set to 500 by defult.
        rk_e : string
            String '--euler' chooses to compute step using euler method. Otherwise
            will use 4th order Runge-Kutta method. Set to '--runge' by default
        **kwargs    :   dict
            Dictionary of any additional parameters that either pde, function or boundry conditons may need.

    For discretisation=pde_solver:
        main_param  :   string
            The main paramter in PDE when using pde_solver. This is diffusion constant kappa for the heat equation.
        L   :   integer
            Length of spatial domain. Defualt to 1
        T   :   integer
            Total time to solve for. Defualt to 1
        mt  :   integer
            Number of gridpoints in time. Defualt to 30
        mx  :   integer
            Number of gridpoints in space. Defualt to 30
        bound_conds  :   numpy array
            Boundary conditons for PDE. Defualt to np.array([0,0])
        method  :   function
            method used for pde solver (e.g. forward/backward euler or crank-nicolson). Defualt to forward.
        type_bc :   string
            Type of boundary condition for pde. Defualt to 'Dirichlet'

    For discretisation=shooting:
        init_guess : float/numpy array
            The inital guess for the intial conditons of the function you are doing natual parameter continuation for
        tt  :  number
            The target value of t that the funtion will solve up to
        step_size   :   float
            This is the size of the 'step' solve_ode will approximate using. Defualt to 0.05
        rk_e : string
            String '--euler' chooses to compute step using euler method. Otherwise
            will use 4th order Runge-Kutta method. Set to '--runge' by default
        period_guess    :   float
            Guess for the period in shooting. Defualt to None. If not input then shooting will use period guesser function to get a good guess for period by looking at functions peaks.
        **kwargs    :   dict
            Dictionary of any additional parameters that either pde, function or boundry conditons may need.

    For discretisation=fsolve:
        init_guess : float/numpy array
            The inital guess for the intial conditons of the function you are doing natual parameter continuation for
        tt  :  number
            The target value of t that the funtion will solve up to
        step_size   :   float
            This is the size of the 'step' solve_ode will approximate using. Defualt to 0.05
        n : number
            The number of steps you want to take between the inital value and target
            value. Set to 500 by defult.
        rk_e : string
            String '--euler' chooses to compute step using euler method. Otherwise
            will use 4th order Runge-Kutta method. Set to '--runge' by default
        **kwargs    :   dict
            Dictionary of any additional parameters that either pde, function or boundry conditons may need.

    Returns
    -------
    found_inits : number/numpy array
        The inital conditons/solutions/steady_states/roots for the function at all of the values of the parameter varied over for shooting/solve_ode/pde_solver/fsolve respectively
    params_from :   number/numpy array
        The values of the parameter that was varied.

    Examples
    --------
    >>> def hopf_mod(t, u_vals, beta):
        u1 = beta*u_vals[0]-u_vals[1]+u_vals[1]*(u_vals[0]**2 + u_vals[1]**2)-u_vals[0]*((u_vals[0]**2 + u_vals[1]**2)**2)
        u2 = u_vals[0]+beta*u_vals[1]+u_vals[1]*(u_vals[0]**2 + u_vals[1]**2)-u_vals[1]*((u_vals[0]**2 + u_vals[1]**2)**2)
        return np.array([u1,u2])
    >>> continuation_natural(hopf_mod ,discretisation=shooting,init_guess=np.array([0.1,0.1]), tt=100,varied_param='beta',param_step_size=0.1, param_from=-1,param_to=2,step_size=0.01,n=500, rk_e='--runge', beta=0.1)
    [-1. -0.57714843 -0.57735543 ........ -1.50433312 -1.52137971][-1,-0.9,.....,1.8,1.9,2]
    '''
    found_inits = [] # initilising found solutions array
    params_from = np.array([]) # initilising params array
    kwargs[init_param]=param_from # value of parameter that is being varied being set

    #  it simply increments the a parameter by a set amount and attempts to find
    #  the solution for the new parameter value using the last found solution as an initial guess.
    number_of_steps = ((param_to-param_from)/param_step_size) # calculating number of steps
    number_of_steps = round(number_of_steps, 2) # handles error caused by division sometimes
    if not number_of_steps.is_integer() or number_of_steps < 0: # checking for erros in the step_size
        raise Exception("number of steps in continuation must be a natural number please choose whole numbers param_from and param_to and/or a different param_step_size") # sends error message
    for i in range(0,int(number_of_steps)+1):  # for loop interating the change in parameter being varied
        # vars,tt, func,step_size=0.01,n=500, rk_e='--runge', **kwargs
        if discretisation==shooting: # checking type of discretisation
            init_guess, period_guess = shooting(init_guess, tt, func, step_size, rk_e,period_guess=period_guess, **kwargs) # parseing to shooting to find initial conditions
            found_inits,params_from = append_inits_params(found_inits,init_guess,params_from,param_from) # appending found solutions and parameters to arrays

        elif discretisation==pde_solver: # checking type of discretisation
            # pde_solver(u_I,L,T,mx,mt,bound_conds,main_param,varied_param=None, method=forward, type_bc='Dirichlet', **kwargs)
            if not main_param:
                raise Exception("You need to pass the main parameter you are using as a string parameter in continuation_natural (e.g. main_param=\'kappa\')")
            u_j,x,t_steady_states = pde_solver(func,L,T,mx,tt,bound_conds,main_param=main_param,varied_param=init_param, method=method, type_bc='Dirichlet',**kwargs)
            if type(t_steady_states[0]) != str:
                init_guess = t_steady_states[0]
                found_inits,params_from = append_inits_params(found_inits,init_guess,params_from,param_from) # appending found solutions and parameters to arrays
        elif discretisation==solve_ode: # checking type of discretisation
            try:
                func(tt,vars,**kwargs)
            except IndexError:
                raise Exception('The dimensions provided from the intial guess given are smaller than the dimensions required for the ODE. Please try a different inital guess with larger dimensions')
            if np.shape(vars) != np.shape(func(tt,vars,**kwargs)):
                raise Exception('The dimensions provided from the intial guess given are larger than the dimensions required for the ODE. Please try a different inital guess with smaller dimensions')

            t_vals, sols = solve_ode(vars,tt, func,step_size,n, rk_e, **kwargs)
            init_guess = [t_vals, sols]
            found_inits,params_from = append_inits_params(found_inits,init_guess,params_from,param_from) # appending found solutions and parameters to arrays

        elif discretisation==fsolve: # checking type of discretisation
            if not sys.warnoptions:
                import warnings
                warnings.simplefilter("ignore")
            init_guess = fsolve(func,init_guess,args=kwargs[init_param])
            found_inits,params_from = append_inits_params(found_inits,init_guess,params_from,param_from) # appending found solutions and parameters to arrays

        param_from += param_step_size
        kwargs[init_param]=param_from

    if plotting=='on':
        plot_func(discretisation,found_inits, params_from) # parseing vals to plotting so appropriate plots can be shown

    return found_inits, params_from

# def pseudo_arc_length_continuation(init_guess, func,n,**kwargs):
#     vars_array=np.array([])
#     for var in init_guess:
#         vars_array = np.append(vars_array,var)
#
#     var0,var1 = vars_array
#
#     secant = var1 - var0 # Creating secant
#     v_tilda = var1 + secant
#     print(v_tilda)
#     for i in range(n):
#         def rooting_obj_fun(v,**kwargs):
#             b = v[0]
#             U = v[1:]
#             print(v)
#             f = func(0,v,**kwargs)
#             dot_prod = np.dot((v-v_tilda),secant)
#             return np.append(dot_prod,f)
#
#         # fsolve(lambda sols,ODE: shoot(sols, ODE, param_idx,rk_e, **kwargs), inital_guesses,ODE)
#
#         solution = fsolve(lambda v_tilda1: rooting_obj_fun(v_tilda1,**kwargs),v_tilda)
#         v_true = solution
#         np.append(vars_array,v_true)
#         secant = vars_array[-1]-vars_array[-2]
#         v_tilda = vars_array[-1] + secant
#     final_var = np.array(vars_array)
#     return final_var.transpose()