import cv2
import numpy as np
def noVal(val):
    pass
identity_kernel = np.array([[0,0,0],[0,1,0],[0,0,0]])
sharpen_kernel = np.array([[0,-1,0],[-1,5,-1],[0,-1,0]])
edge_kernel = np.array([[1,1,1],[1,-8,1],[1,1,1]])
#gaussian_kernel = np.array([[1,2,1],[2,4,2],[1,2,1]] , np.float32) / 16
gaussian_kernel1 = cv2.getGaussianKernel(3, 0)
gaussian_kernel2 = cv2.getGaussianKernel(5, 0)
emboss_kernel = np.array([[-2,-1,0],[-1,1,1],[0,1,2]])
multi_kernel = np.array([[-4,8,0],[0,-80,0],[0,8,-4]])*2
kernels = [identity_kernel, gaussian_kernel1, gaussian_kernel2, multi_kernel, emboss_kernel, sharpen_kernel, edge_kernel]
color_original = cv2.imread('test.jpg')
color_modified = color_original.copy()
gray_original = cv2.cvtColor(color_original, cv2.COLOR_BGR2GRAY)
gray_modified = gray_original.copy()
cv2.namedWindow('pKaras - Filter Application')
cv2.createTrackbar('Kontrast', 'pKaras - Filter Application', 1, 100, noVal)
cv2.createTrackbar('Jasnosc', 'pKaras - Filter Application', 50, 100, noVal)
cv2.createTrackbar('Rodzaj Filtru', 'pKaras - Filter Application', 0, len(kernels)-1, noVal)
cv2.createTrackbar('Monochrom', 'pKaras - Filter Application', 0, 1, noVal)
counter = 1
while True:
    grayscale = cv2.getTrackbarPos('Monochrom', 'pKaras - Filter Application')
    if grayscale == 0:
        cv2.imshow('pKaras - Filter Application', color_modified)
    else:
        cv2.imshow('pKaras - Filter Application', gray_modified)
    k = cv2.waitKey(1) & 0xFF
    if k == ord('q'):
        break
    if k == ord('s'):
        if grayscale == 0:
            cv2.imwrite('output%d.png'% counter, color_modified)
        else:
            cv2.imwrite('output%d.png'% counter, gray_modified)
    counter += 1
    contrast = cv2.getTrackbarPos('Kontrast', 'pKaras - Filter Application')
    brightness = cv2.getTrackbarPos('Jasnosc', 'pKaras - Filter Application')
    kernel = cv2.getTrackbarPos('Rodzaj Filtru','pKaras - Filter Application')
    if(grayscale == 0):
        color_modified = cv2.filter2D(color_original, -1, kernels[kernel])
        color_modified = cv2.addWeighted(color_modified,contrast, np.zeros(color_original.shape, dtype=color_original.dtype), 0, brightness-50)
    else:
        gray_modified = cv2.filter2D(gray_original, -1, kernels[kernel])
        gray_modified = cv2.addWeighted(gray_modified,contrast, np.zeros(gray_original.shape, dtype=gray_original.dtype), 0, brightness-50)
    color_modified = cv2.filter2D(color_original, -1, kernels[kernel])
    color_modified = cv2.addWeighted(color_modified,contrast, np.zeros(color_original.shape, dtype=color_original.dtype), 0, brightness-50)
cv2.destroyAllWindows()


import sys
import google.auth
import google.cloud.logging
from .composer import Composer
from .compute_engine import delete_polling_instance
from .utils import wait_for_state, notify_slack
from .env import ENVIRONMENT_NAME, DAG_NAME_TO_RUN
from timeout_decorator import timeout, TimeoutError

MAXIMUM_HOURS = 3

@timeout(60 * 60 * MAXIMUM_HOURS)
def _create_composer():
    if Composer.environment_exists(ENVIRONMENT_NAME):
        raise Exception('Composer Already exists!')

    # Composer作成
    composer = Composer(ENVIRONMENT_NAME)
    notify_slack('create composer')

    # Composer PyPI更新
    wait_for_state(composer.update_pypi)
    notify_slack('update composer PyPI')

    # Upload Airflow dag
    wait_for_state(composer.upload_airflow_dags)
    notify_slack('upload dags')

@timeout(60 * 60 * MAXIMUM_HOURS)
def _delete_composer():
    if not Composer.environment_exists(ENVIRONMENT_NAME):
        raise Exception('Composer does not exists!')

    # Composer取得
    composer = Composer(ENVIRONMENT_NAME)

    # Composer削除
    wait_for_state(composer.delete)
    notify_slack('delete composer')

    # Composer紐付きDisk削除
    wait_for_state(composer.delete_disk)
    notify_slack('delete Composer disk not in use')

@timeout(60 * 60 * MAXIMUM_HOURS)
def _run_composer_once():
    _create_composer()

    # Run Airflow dag
    composer = Composer(ENVIRONMENT_NAME)
    wait_for_state(lambda: composer.run_airflow_dag(DAG_NAME_TO_RUN))
    notify_slack('run dag')

    _delete_composer()

def main():
    try:
        client = google.cloud.logging.Client()
        client.setup_logging()

        manipulate_type = sys.argv[1]
        if manipulate_type == 'create':
            _create_composer()
        elif manipulate_type == 'delete':
            _delete_composer()
        if manipulate_type == 'run_once':
            _run_composer_once()
    except TimeoutError:
        notify_slack('one time composer operation timeout!')
    except Exception as e:
        notify_slack(e.message)
    finally:
        # Polling GCE削除
        delete_polling_instance()
        notify_slack('delete polling instance')

if __name__ == '__main__':
    main()


from __future__ import absolute_import, division, print_function, unicode_literals

import tensorflow as tf

class CustomSchedule(tf.keras.optimizers.schedules.LearningRateSchedule):
    def __init__(self, d_model, warmup_steps=4000):
        super(CustomSchedule, self).__init__()

        self.d_model = d_model
        self.d_model = tf.cast(self.d_model, tf.float32)

        self.warmup_steps = warmup_steps

    def __call__(self, step):
        arg1 = tf.math.rsqrt(step)
        arg2 = step * (self.warmup_steps ** -1.5)

        return tf.math.rsqrt(self.d_model) * tf.math.minimum(arg1, arg2)

if __name__ == '__main__':
    step = tf.constant(1.)
    warmup_steps = 2000
    d_model = 256.

    for i in range(0, 10000, 100):
        step = tf.constant(i, dtype=tf.float32)
        arg1 = tf.math.rsqrt(step)
        arg2 = step * (warmup_steps ** -1.5)
        print(i, tf.math.rsqrt(d_model) * tf.math.minimum(arg1, arg2))


import tkinter as tk
import random
import nameGenerator as nameGen
#Name: NPC
#Params: list package
#Returns: none
#Description: holds data about each npc created on the map
class NPC():
    #Name: __init__
    #Params: list package
    #Returns: none
    #Description: initializes variables for the villagers from the package given to them
    def __init__(self,package):
        self.img = tk.PhotoImage(file = "./Images/NPC/" + package[0] + "/down1.png")
        self.x = 0
        self.y = 0
        self.type = package[1]
        self.name = nameGen.characterNameGenerator()
        self.moveTimer = 15
        self.moveVectorTime = 0
        self.moveVector = random.randint(0,3)
        self.pos = 0
        self.optionMap = package[2]
        self.talkOptions = ["Welcome to the store!","Lovely weather today, is it not?", "There have been a lot of bat attacks on the outermost towns of late!"]
        self.cTO = 0
#Holds data for the only type of npc that we have currently
npcBase = [["TestNPC","shop", {1: "Shop", 2: "Buy",3:"Talk",4:"Sell"}]]


import sublime
from sublime import *
from sublime_plugin import *
import difflib

def diff_view_with_disk(view):
  old_s = open(view.file_name()).read()
  new_s = view.substr(Region(0, view.size()))
  return diff(old_s, new_s)

def diff(old_s, new_s):
  """Returns operations necessary to transform old_s into new_s.
  Note: We optimize for the (hypothetically)common case where edits will
  be localized to one small area of a large file.
  """
  limit = min(len(old_s),len(new_s))

  # Find first index (counting from the start) where the
  # strings differ.
  i = 0
  while i < limit:
    if new_s[i] != old_s[i]:
      break
    i += 1

  i = max(i-1, 0)

  # Find first index (counting from the end) where the
  # strings differ.
  j = 1
  while j < (limit - i):  # Cursors should not overlap
    if new_s[-j] != old_s[-j]:
      break
    j += 1

  j = j-1

  # Do diff, only over the modified window.
  d = difflib.SequenceMatcher(isjunk=None,
                              a=old_s[i:len(old_s) - j],
                              b=new_s[i:len(new_s) - j])

  ops = []
  for (op,i1,i2,j1,j2) in d.get_opcodes():
      # Re-add the window offset.
      k1 = i1 + i
      k2 = i2 + i
      l1 = j1 + i
      l2 = j2 + i
      if op == 'delete':
          ops.append(['-', k1, k2])
      elif op == 'insert':
          ops.append(['+', k1, new_s[l1:l2]])
      elif op == 'replace':
          ops.append(['*', k1, k2, new_s[l1:l2]])

  return ops

def apply_operations(input, ops):
  newLen = len(input) + net_length_change(ops)
  result = ""
  offset = 0
  src_cursor = 0
  for op in ops:
    i = op[1]
    copy_len = i - src_cursor
    result += input[src_cursor:src_cursor + copy_len]
    src_cursor += copy_len
    if op[0] == '+':
      [plus, i, text] = op
      result += text
      offset += len(text)
    elif op[0] == '*':
      [mult, i, j, text] = op
      result += text
      offset += len(text) - (j - i)
      src_cursor += (j - i)
    elif op[0] == '-':
      [minus, i, j] = op
      offset -= (j - i)
      src_cursor += (j - i)
  copy_len = len(input) - src_cursor
  result += input[src_cursor:src_cursor + copy_len]
  return result

def net_length_change(ops):
  offset = 0
  for op in ops:
    if op[0] == '+':
      [plus, i, text] = op
      offset += len(text)
    elif op[0] == '*':
      [mult, i, j, text] = op
      offset += len(text) - (j - i)
    elif op[0] == '-':
      [minus, i, j] = op
      offset -= (j - i)
  return offset

def _check(old, new):
  assert apply_operations(old, diff(old, new)) == new

if __name__ == "__main__":
  _check("abc", "abc")
  _check("abc", "qbc")
  _check("abc", "qabc")
  _check("abc", "abcd")
  _check("abc", "aqbqcq")
  _check("abc", "qqqqqbc")
  _check("abc", "")
  _check("abc\n   def", "abc   def")
  _check("", "abcdef")
  _check("abcdef", "abcabcdef")
  _check("abcdef", "abcdefabcabcdef")
  _check("abcdef", "abc")
  _check("abcde", "abcabcde")


##############################################################################
# Copyright by The HDF Group.                                                #
# All rights reserved.                                                       #
#                                                                            #
# This file is part of HSDS (HDF5 Scalable Data Service), Libraries and      #
# Utilities.  The full HSDS copyright notice, including                      #
# terms governing use, modification, and redistribution, is contained in     #
# the file COPYING, which can be found at the root of the source code        #
# distribution tree.  If you do not have access to this file, you may        #
# request a copy from help@hdfgroup.org.                                     #
##############################################################################
import asyncio
import sys
from collections import namedtuple
from aiobotocore import get_session
from aiohttp.client_exceptions import ClientOSError
from aiohttp.web import StreamResponse
from aiohttp.http_writer import StreamWriter

sys.path.append('../../hsds')
sys.path.append('../../hsds/util')
from util.storUtil import releaseStorageClient
from util.idUtil import isValidChunkId
from util.lruCache import LruCache
from chunk_dn import GET_Chunk
import config

# This is a utility to dump a JSON obj (group, dataset, ctype) given the
# the objects UUID

# Note - this is not quite working for streaming responses

HttpVersion = namedtuple('HttpVersion', ['major', 'minor'])
HttpVersion11 = HttpVersion(1, 1)

class Request:
    # Shim class to replace aiohttp request
    def __init__(self, app, path, method='GET', params={}):
        RelURL = namedtuple('RelURL', 'query')
        self._app = app
        self._method = method
        self._headers = {}
        self._path = path
        self._match_info = params
        self._rel_url = RelURL(params)
        self._payload_writer = None

    @property
    def app(self):
        return self._app

    @property
    def method(self):
        return self._method

    @property
    def headers(self):
        return self._headers

    @property
    def path(self):
        return self._path

    @property
    def match_info(self):
        return self._match_info

    @property
    def rel_url(self):
        return self._rel_url

    @property
    def keep_alive(self):
        """Is keepalive enabled by client?"""
        return False

    @property
    def version(self):
        # HTTP Version
        return HttpVersion11

    async def _prepare_hook(self, response: StreamResponse) -> None:
        return

#
# Print usage and exit
#
def printUsage():
    print("usage: python get_s3json [--bucket_name=<bucket>] [--aws_s3_gateway=<s3_endpoint>] objid ")
    print("  objid: s3 JSON obj to fetch")
    print("  Example: python get_chunk_values.py --aws_s3_gateway=http://192.168.99.100:9000 --bucket_name=hsdsdev --domain=/shared/tall.h5 c-75b88d05-46db146e-a19a-7a403e-f6d7a8_0_0")
    sys.exit()

async def printChunkValues(app, domain, chunk_id):

    params = {"domain": domain, "id": chunk_id}
    path=f"/chunks/{chunk_id}"
    request = Request(app, path, params=params)
    try:
        resp = await GET_Chunk(request)
        print("got resp:", resp)
        # print(json.dumps(json_obj, sort_keys=True, indent=4))
    except ValueError as ve:
        print(f"Got ValueError exception: {ve}")
    except ClientOSError as coe:
        print(f"Got error: {coe}")
    await releaseStorageClient(app)

def main():
    if len(sys.argv) == 1 or sys.argv[1] == "-h" or sys.argv[1] == "--help":
        printUsage()
        sys.exit(1)

    chunk_id = sys.argv[-1]
    if not isValidChunkId(chunk_id):
        print("Invalid chunk id")
        sys.exit(1)

    # we need to setup a asyncio loop to query s3
    loop = asyncio.get_event_loop()
    session = get_session(loop=loop)

    app = {}
    app["session"] = session
    app['bucket_name'] = config.get("bucket_name")
    app['node_count'] = 1
    app['node_number'] = 0
    app['deleted_ids'] = set()
    app['meta_cache'] = {}
    app['pending_s3_read'] = {}
    app['meta_cache'] = LruCache(mem_target=1024*1024, chunk_cache=False)
    app['chunk_cache'] = LruCache(mem_target=64*1024*1024, chunk_cache=True)
    domain = config.get("domain")
    if not domain:
        printUsage()
        sys.exit(-1)
    print("got domain:", domain)

    loop.run_until_complete(printChunkValues(app, domain, chunk_id))

    loop.close()

main()


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

from PyQt4 import QtCore, QtGui
import sys
try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    def _fromUtf8(s):
        return s

try:
    _encoding = QtGui.QApplication.UnicodeUTF8
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig)

class Ui_Contrib(QtGui.QMainWindow):

    def __init__(self, setter):

        QtGui.QMainWindow.__init__(self)
        self.setupUi(self)
        self.setter = setter

    def setupUi(self, Contrib):
        Contrib.setObjectName(_fromUtf8("Contrib"))
        Contrib.resize(800, 600)
        self.centralwidget = QtGui.QWidget(Contrib)
        self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
        self.tableView = QtGui.QTableView(self.centralwidget)
        self.tableView.setGeometry(QtCore.QRect(50, 110, 701, 411))
        self.tableView.setObjectName(_fromUtf8("tableView"))
        self.pushButton = QtGui.QPushButton(self.centralwidget)
        self.pushButton.setGeometry(QtCore.QRect(680, 550, 99, 27))
        self.pushButton.setObjectName(_fromUtf8("pushButton"))
        self.pushButton_2 = QtGui.QPushButton(self.centralwidget)
        self.pushButton_2.setGeometry(QtCore.QRect(550, 550, 99, 27))
        self.pushButton_2.setObjectName(_fromUtf8("pushButton_2"))
        self.label = QtGui.QLabel(self.centralwidget)
        self.label.setGeometry(QtCore.QRect(90, 30, 411, 41))
        font = QtGui.QFont()
        font.setPointSize(19)
        self.label.setFont(font)
        self.label.setObjectName(_fromUtf8("label"))
        Contrib.setCentralWidget(self.centralwidget)

        self.retranslateUi(Contrib)
        QtCore.QMetaObject.connectSlotsByName(Contrib)

    def retranslateUi(self, Contrib):
        Contrib.setWindowTitle(_translate("Contrib", "MainWindow", None))
        self.pushButton.setText(_translate("Contrib", "Envoyer", None))
        self.pushButton_2.setText(_translate("Contrib", "Annuler", None))
        self.label.setText(_translate("Contrib", "Contribuer à notre projet", None))


# encoding=utf-8

import pickle
import numpy as np

embedding_size = 64

with open('./data/biogrid_dict.pkl', 'rb') as file:
    biogrid_dict = pickle.load(file)
biogrid_dict = {value: key for key, value in biogrid_dict.items()}

emb_dict = dict()
with open('./data/biogrid_network_bin2.emb') as file:
    for line in file.readlines()[1:]:
        columns = line.strip().split()
        protein_id = columns[0]
        protein_emb = columns[1:]
        protein_name = biogrid_dict[protein_id]
        emb_dict[protein_name] = protein_emb

key_protein_set = set()
with open('./data/essential_proteins.txt') as file:
    for line in file.readlines():
        key_protein = line.strip()
        key_protein_set.add(key_protein)

protein_matrix = []
protein_label = []
protein_emb = []
with open('./data/GSE3431_T36.txt') as file:
    for line in file.readlines():
        columns = line.strip().split()
        protein_name = columns[0]
        features = columns[1:]
        assert len(features) == 36

        if protein_name in emb_dict:
            protein_matrix.append(features)
            protein_emb.append(emb_dict[protein_name])
            if protein_name in key_protein_set:
                protein_label.append(1)  # 1266
            else:
                protein_label.append(0)  # 5510

protein_matrix = np.array(protein_matrix).reshape(-1, 3, 12)
np.save('./data/protein_matrix.npy', protein_matrix)
print(protein_matrix.shape)

protein_emb = np.array(protein_emb)
np.save('./data/protein_emb.npy', protein_emb)
print(protein_emb.shape)

protein_label = np.array(protein_label).reshape(-1, 1)
np.save('./data/protein_label.npy', protein_label)
print(protein_label.shape)


#-*-coding:utf-8 -*-
import web_news_splits
import sys
sys.path.append('/home/team6/ArticleTitleGenerator/model')
import predict_website
import random
from flask import Flask,request
from flask import jsonify
import utils
import get_article

app = Flask(__name__,static_url_path='',root_path='/home/team6/final_demo_website')
@app.route('/')
def index():
	return app.send_static_file('index.html')

@app.route('/Ajax')
def Ajax():
	url = request.args.get('url')
	article = get_article.get_news_article(url)
	cut_sequence = web_news_splits.splitWords(article)
	#cut_content = utils.cutToWord(content)
	#output = predict_website.decode(cut_content)
	output = predict_website.decode(cut_sequence)
	result = {'predict':output,'article':article}
	return jsonify(result)

@app.route('/dataFromAjax')
def dataFromAjax():
	test = request.args.get('mode')
	#x,y = utils.getRandomXY(0,95,0,91)
	#print(x,y)
	result = {'x':55,'y':72}
	#result = ['aa',5]
	return jsonify(result)

if __name__ == '__main__':
	#app.run()
	app.run(host='0.0.0.0',port=8892,debug=False)


from __future__ import print_function
import ROOT as R
import argparse
import os
import sys
from itertools import product

A_loose = R.TFile("2018_trigger_eff/results_Ele_looseOnly/Ele32_pt_eta/Ele32_pt_eta_2018_EGM_looseid_nominal_efficiency.root")
A_tight = R.TFile("2018_trigger_eff/results_Ele_tight/Ele32_pt_eta/Ele32_pt_eta_2018_EGM_newWPnominal_efficiency.root")
B_loose = R.TFile("2018_trigger_eff/results_Ele_looseOnly/Ele23_Ele12_leg1_pt_eta/Ele23_Ele12_leg1_pt_eta_2018_EGM_looseid_nominal_efficiency.root")
B_tight = R.TFile("2018_trigger_eff/results_Ele_tight/Ele23_Ele12_leg1_pt_eta/Ele23_Ele12_leg1_pt_eta_2018_EGM_newWPnominal_efficiency.root")

print ("Aloose",[p for p in A_loose.c.GetListOfPrimitives()])
print ("Atight",[p for p in A_tight.c.GetListOfPrimitives()])
print ("Bloose",[p for p in B_loose.c.GetListOfPrimitives()])
print ("Btight",[p for p in B_tight.c.GetListOfPrimitives()])

eff_A_L = A_loose.c.GetPrimitive("Ele32_pt_eta_total_clone")
eff_A_T = A_tight.c.GetPrimitive("Ele32_pt_eta_total_clone")
eff_B_L = B_loose.c.GetPrimitive("Ele23_Ele12_leg1_pt_eta_total_clone")
eff_B_T = B_tight.c.GetPrimitive("Ele23_Ele12_leg1_pt_eta_total_clone")

hs = [eff_A_L, eff_A_T, eff_B_L, eff_B_T]
cache =[]
for i,h in enumerate(hs):
    c = R.TCanvas(h.GetTitle()+str(i))
    h.Draw("COLZ")
    c.Draw()
    cache.append(c)

# Get histogram to copy the structure
h_A_L = eff_A_L.GetCopyTotalHisto()
h_A_T = eff_A_T.GetCopyTotalHisto()
h_B_L = eff_B_L.GetCopyTotalHisto()
h_B_T = eff_B_T.GetCopyTotalHisto()

binsX = h_A_L.GetNbinsX()

if h_A_T.GetNbinsX() != binsX: print("ERROR X A_T")
if h_B_L.GetNbinsX() != binsX: print("ERROR X B_L")
if h_B_T.GetNbinsX() != binsX: print("ERROR X B_T")
edX_A = [h_A_L.GetXaxis().GetBinLowEdge(i) for i in range(1, binsX+1)]
edX_B = [h_B_L.GetXaxis().GetBinLowEdge(i) for i in range(1, binsX+1)]
print("edge X A: ", edX_A)
print("edge X B: ", edX_B)

binsY = h_A_L.GetNbinsY()
binsY_B = h_B_L.GetNbinsY()
if h_A_T.GetNbinsY() != binsY: print("ERROR Y A_T")
if h_B_L.GetNbinsY() != binsY: print("ERROR Y B_L")
if h_B_T.GetNbinsY() != binsY: print("ERROR Y B_T")
edY_A = [h_A_L.GetYaxis().GetBinLowEdge(i) for i in range(1, binsY+1)]
edY_B = [h_B_L.GetYaxis().GetBinLowEdge(i) for i in range(1, binsY+1)]
edY_A_up = [h_A_L.GetYaxis().GetBinUpEdge(i) for i in range(1, binsY+1)]
edY_B_up = [h_B_L.GetYaxis().GetBinUpEdge(i) for i in range(1, binsY+1)]
print("edge X A: ", edY_A)
print("edge up X A: ", edY_A_up)
print("edge X B: ", edY_B)
print("edge up X B: ", edY_B_up)
print("N bins A:", binsY, "N bins B: ", binsY_B)

outputfile = R.TFile("output_hist_2018.root", "recreate")
ratio_L = h_A_L.Clone("ratio_L")
ratio_T = h_B_T.Clone("ratio_T")
ratio_L.SetTitle("Eff. Loose trig A/B")
ratio_T.SetTitle("Eff. Tight trig A/B")
ratio_tot = ratio_L.Clone("ratio_tot")
ratio_tot.SetTitle("ratio_tot")
ratio_L.Reset()
ratio_T.Reset()
ratio_tot.Reset()

ratios_pt = {}

for binx in range(1, binsX+1):
    g = R.TGraph(binsY - 2)
    g.SetName("ratio_etabin{}".format(binx))
    g.SetTitle("ratio_etabin{};Pt".format(binx) )
    g.Write()
    ratios_pt[binx] = g

# No overflow and underflow for eta,
# Starting from bin3, 40 GeV in Pt
for binx, biny in product(range(1, binsX+1), range(3, binsY+2)):
    eAL = eff_A_L.GetEfficiency(eff_A_L.GetGlobalBin(binx, biny))
    eBL = eff_B_L.GetEfficiency(eff_B_L.GetGlobalBin(binx, biny))
    eAT = eff_A_T.GetEfficiency(eff_A_T.GetGlobalBin(binx, biny))
    eBT = eff_B_T.GetEfficiency(eff_B_T.GetGlobalBin(binx, biny))

    if eBL == 0 or eBT == 0:
        print(binx, biny,eff_A_L.GetGlobalBin(binx, biny), eAL, eAT, eBL, eBT)
        ratio_L.SetBinContent(binx, biny, 1)
        ratio_T.SetBinContent(binx, biny, 1)
        ratio_tot.SetBinContent(binx, biny, 1)
        ratios_pt[binx].SetPoint(biny-3, h_A_L.GetYaxis().GetBinCenter(biny), eAL)
    else:
        ratio_L.SetBinContent(binx, biny, eAL / eBL)
        ratio_T.SetBinContent(binx, biny, eAT / eBT)
        ratio_tot.SetBinContent(binx, biny, (eAT / eAL) / (eBT / eBL))
        print(h_A_L.GetYaxis().GetBinCenter(biny))
        ratios_pt[binx].SetPoint(biny-3, h_A_L.GetYaxis().GetBinCenter(biny), eAL)

cL = R.TCanvas("cl")
ratio_L.Draw("COLZ")
cL.SetLogy()
cL.Draw()

cT = R.TCanvas("cT")
ratio_T.Draw("COLZ")
cT.SetLogy()
cT.Draw()

cTot = R.TCanvas("cTot")
ratio_tot.Draw("COLZ")
cTot.SetLogy()
cTot.Draw()

outputfile.Write()
# outputfile.Close()


#==============================================================================
# MatrixVecMultLaneBL_test
#==============================================================================

import pytest

from pymtl        import *
from pclib.test   import TestSource, TestMemory
from pclib.ifcs import mem_msgs
from MatrixVecCOP import MatrixVecCOP

#------------------------------------------------------------------------------
# SourceHarness
#------------------------------------------------------------------------------
class SourceHarness( Model ):

  def __init__( s, nlanes, nmul_stages, mem_delay, src_delay, config_msgs,
                test_verilog ):

    cop_addr_nbits = 5
    cop_data_nbits = 32
    mem_addr_nbits = 32
    mem_data_nbits = 32

    memreq_params  = mem_msgs.MemReqParams( mem_addr_nbits, mem_data_nbits )
    memresp_params = mem_msgs.MemRespParams( mem_data_nbits )

    s.src   = TestSource  ( cop_addr_nbits + cop_data_nbits,
                            config_msgs, src_delay )
    s.cop   = MatrixVecCOP( nlanes, nmul_stages,
                            cop_addr_nbits, cop_data_nbits,
                            mem_addr_nbits, mem_data_nbits )
    s.mem   = TestMemory  ( memreq_params, memresp_params,
                            nlanes, mem_delay )

    if test_verilog:
      s.cop = TranslationTool( s.cop )

    assert nlanes > 0
    s.nlanes = nlanes

  def elaborate_logic( s ):

    for i in range( s.nlanes ):
      s.connect( s.src.out,          s.cop.from_cpu )
      s.connect( s.cop.lane_req [i], s.mem.reqs [i] )
      s.connect( s.cop.lane_resp[i], s.mem.resps[i] )

  def done( s ):
    return s.src.done and s.cop.from_cpu.rdy

  def line_trace( s ):
    return "{} || {}".format( s.src.line_trace(), s.mem.line_trace() )

#------------------------------------------------------------------------------
# run_lane_managed_test
#------------------------------------------------------------------------------
def run_lane_managed_test( dump_vcd, model,
                           src_matrix, src_vector, dest_vector ):

  model.vcd_file = dump_vcd
  model.elaborate()

  sim = SimulationTool( model )

  # Load the memory

  model.mem.load_memory( src_matrix  )
  model.mem.load_memory( src_vector  )

  # Run the simulation

  sim.reset()

  print
  while not model.done() and sim.ncycles < 80:
    sim.print_line_trace()
    sim.cycle()

  assert model.done()

  dest_addr   = dest_vector[0]
  for i, dest_value in enumerate( dest_vector[1] ):
    assert model.mem.mem.mem[ dest_addr+i ] == dest_value

  # Add a couple extra ticks so that the VCD dump is nicer

  sim.cycle()
  sim.cycle()
  sim.cycle()

#------------------------------------------------------------------------------
# config_msg
#------------------------------------------------------------------------------
# Utility method for creating config messages
def config_msg( addr, value ):
  return concat( Bits(3, addr), Bits(32, value) )

#------------------------------------------------------------------------------
# mem_array_32bit
#------------------------------------------------------------------------------
# Utility function for creating arrays formatted for memory loading.
from itertools import chain
def mem_array_32bit( base_addr, data ):
  return [base_addr,
          list( chain.from_iterable([ [x,0,0,0] for x in data ] ))
         ]

#------------------------------------------------------------------------------
# test_managed
#------------------------------------------------------------------------------
#  5 1 3   1    16
#  1 1 1 . 2  =  6
#  1 2 1   3     8
#
@pytest.mark.parametrize(
  ('mem_delay','nmul_stages'), [(0,1),(0,4),(5,1),(5,4)]
)
def test_managed_1lane( dump_vcd, test_verilog, mem_delay, nmul_stages ):
  run_lane_managed_test( dump_vcd,
                  SourceHarness( 1, nmul_stages, mem_delay, 0,
                     [ config_msg( 1,   3), # size
                       config_msg( 2,   0), # r_addr
                       config_msg( 3,  80), # v_addr
                       config_msg( 4, 160), # d_addr
                       config_msg( 0,   1), # go
                     ],
                     test_verilog
                   ),
                   # NOTE: C++ has dummy data between rows when you have array**!
                   mem_array_32bit(  0, [ 5, 1 ,3, 99,
                                          1, 1 ,1, 99,
                                          1, 2 ,1] ),
                   mem_array_32bit( 80, [ 1, 2, 3 ]),
                   mem_array_32bit(160, [16]),
                 )

@pytest.mark.parametrize(
  ('mem_delay','nmul_stages'), [(0,1),(0,4),(5,1),(5,4)]
)
def test_managed_3lane( dump_vcd, test_verilog, mem_delay, nmul_stages ):
  run_lane_managed_test( dump_vcd,
                  SourceHarness( 3, nmul_stages, mem_delay, 0,
                     [ config_msg( 1,   3), # size
                       config_msg( 2,   0), # r_addr
                       config_msg( 3,  80), # v_addr
                       config_msg( 4, 160), # d_addr
                       config_msg( 0,   1), # go
                     ],
                     test_verilog
                   ),
                   # NOTE: C++ has dummy data between rows when you have array**!
                   mem_array_32bit(  0, [ 5, 1 ,3, 99,
                                          1, 1 ,1, 99,
                                          1, 2 ,1] ),
                   mem_array_32bit( 80, [ 1, 2, 3 ]),
                   mem_array_32bit(160, [16, 6, 8 ]),
                 )


# -*- coding: utf-8 -*-
"""
Created on Tue Mar 13 14:55:30 2018

@author: Sula Mueller

DESCRIPTION:
    defines functions used by ASGmain
"""

from smAngles import determineAnglesSparseInfo
from smAngles import determineAllAnglesAndCoordinates
from smFacets import FacetizeEntireGrid
from smSTLAfile import createSTLAFile
from smIGESfile import createIGESFile

''' readInputFromText
DESCRIPTION: extracts value/ input from given file-string by designation of
    value
INPUT:
    * inputtext: content of Input_Parameter_File as string
    * valuename: name of value that gets extracted (eg 'FILETYPE' or 'SDD')
OUTPUT:
    * value of designated valuename (eg 'STLA' or '120')
    * output always as string '''
def readInputFromText(inputtext, valuename):
    i = inputtext.find(valuename)  # index of valuename
    i = inputtext.find('=', i)   # index of = after valuename
    substring = inputtext[i:-1]
    value = substring.split()  # returns array of all non-space entries
    return value[1]  # first entry is '=', second entry should be wanted value

''' readAsMatrix
DESCRIPTION: extracts value/ input from given file-string by designation of
    value
INPUT:
    * inputtext: content of Input_Parameter_File as string
    * valuename: name of value that gets extracted (eg 'N_P')
    * OneDgrid: 0 -> 2D grid
                1 -> 1D with leaves along x-axis
                -1 -> 1D with leaves along z-axis
OUTPUT:
    * value of designated valuename (eg [32,32])
    * output always as 1x2 matrix type float
    * if 1D grid: values of OTHER dimension are set to 0 '''
def readAsMatrix(inputtext, valuename, gridtype):
    if gridtype == 1:  # 1D with leaves along x-axis
        val = float(readInputFromText(inputtext, valuename))
        return [0,val]
    if gridtype == -1:  # 1D with leaves along z-axis
        val = float(readInputFromText(inputtext, valuename))
        return [val,0]
    # else: 2D grid
    i = inputtext.find(valuename)  # index of valuename
    i = inputtext.find('[', i)  # index of first [ after valuename
    j = inputtext.find(']', i)
    substring = inputtext[i+1:j]  # extract string between [] (without [])
    if substring == '[]':
        return ('[0,0]')
    substring.replace(';', ',')
    value = substring.split(',')
    valu = value[0]  # first entry
    valu.strip()  # remove ' '
    x = float(valu)
    valu = value[1]  # second entry
    valu.strip()  # remove ' '
    z = float(valu)
    return [x,z]

''' readAsIntMatrix
same as readAsMatrix but output is matrix of integers
if 1D grid: values of OTHER dimension are set to 1
(readAsIntMatrix gets only called by N_E, N_P -> treat as if 1 pixel) '''
def readAsIntMatrix(inputtext, valuename, gridtype):
    if gridtype == 1:  # 1D with leaves along x-axis
        val = int(readInputFromText(inputtext, valuename))
        return [1,val]
    if gridtype == -1:  # 1D with leaves along z-axis
        val = int(readInputFromText(inputtext, valuename))
        return [val,1]
    # else: 2D grid
    i = inputtext.find(valuename)  # index of valuename
    i = inputtext.find('[', i)  # index of first [ after valuename
    j = inputtext.find(']', i)
    substring = inputtext[i+1:j]  # extract string between [] (without [])
    if substring == '[]':
        return ('[0,0]')
    substring.replace(';',',')
    value = substring.split(',')
    valu = value[0]  # first entry
    valu.strip()  # remove ' '
    x = int(valu)
    valu = value[1]  # second entry
    valu.strip()  # remove ' '
    z = int(valu)
    return [x,z]

''' does same as createSTLAfromGridParams with simpler geometry '''
def createSTLAfromGridParams_Sparse(path, name, GR, th_grid, SDD, delta_M, N_E,
                                    N_P, n):
    Angles = determineAnglesSparseInfo(SDD, delta_M, N_E, N_P, n)
    Facets = FacetizeEntireGrid(Angles, GR, th_grid)
    createSTLAFile(path, name, Facets)
    return

def createSTLAfromGridParams(path, name, GR, th_grid, SDD, EAD, size_M,
                             edge_M0, edge_M1, gap_M, edge_E0, edge_E1, gap_E,
                             N_E, N_P, n):
    Angles = determineAllAnglesAndCoordinates(SDD, EAD, size_M, edge_M0,
                                              edge_M1, gap_M, edge_E0,
                                              edge_E1, gap_E, N_E, N_P, n)
    Facets = FacetizeEntireGrid(Angles, GR, th_grid)
    createSTLAFile(path, name, Facets)
    return

def createIGESfromGridParams(path, name, GR, th_grid, SDD, EAD, size_M,
                             edge_M0, edge_M1, gap_M, edge_E0, edge_E1, gap_E,
                             N_E, N_P, n):
    Angles = determineAllAnglesAndCoordinates(SDD, EAD, size_M, edge_M0,
                                              edge_M1, gap_M, edge_E0, edge_E1,
                                              gap_E, N_E, N_P, n)
    createIGESFile(path, name, Angles, GR, th_grid)
    return


import numpy as np
from sympy import *

def calculate_eigenvector(list_input):
	#Step 1: Transform matrix and set up index
	matrix = np.array(list_input)
	x = Symbol('x')
	y = Symbol('y')

	pos1 = matrix[0][0]
	pos2 = matrix[0][1]
	pos3 = matrix[1][0]
	pos4 = matrix[1][1]

	#Step 2: Establish quadratic equation ...
	# ... and find the coefficients
	equation = (pos1 - x) * (pos4 - x) - pos2 * pos3
	equation_domain = Poly(equation,x)
	equation_coeffs = list(
		    map(lambda coeff: int(coeff),
			equation_domain.coeffs()))

	a,b,c = equation_coeffs

	if a == 0:
		eigen_val = ((-1) * c) / b
		pos1 = pos1 - eigen_val
		pos4 = pos4 - eigen_val
		print(solve([pos1*x + pos2*y, pos3*x + pos4*y],[x,y]))

	else:
	#Step 3: Calculate the discriminant,
	#...which is also known as delta ...
	# ... delta according to bullshit high school
		d = int((b**2) - (4*a*c))

	#Step 4: find eigenvalues
		eigen_val1 = int((-b - np.sqrt(d)) / (2*a))
		eigen_val2 = int((-b + np.sqrt(d)) / (2*a))

	#Step 5: find eigenvectors
		for val in [eigen_val1,eigen_val2]:
			pos1 = pos1 - val
			pos4 = pos4 - val
			print("Eigenvalue is: {}".format(val))
			print(
			"Eigenvector is: {}\n".format(
			solve([pos1*x + pos2*y, pos3*x + pos4*y],[x,y])
			     ))

calculate_eigenvector([[7,3],[3,-1]])


# !/usr/bin/env python3
# -*- coding:utf-8 -*- #
# Author: Flyaway - flyaway1217@gmail.com
# Blog: zhouyichu.com
#
# Python release: 3.4.5
#
# Date: 2017-01-16 20:42:15
# Last modified: 2017-01-23 12:40:33

"""
Implementation for Coupon Collectors
"""

import random
import time

import matplotlib.pyplot as plt

def QA(n):
    count = n
    x = [random.randint(1, n) for _ in range(n)]
    s = set(x)
    while True:
        count += 1
        x = random.randint(1, n)
        s.add(x)
        if len(s) == n:
            return count

def QB(m, n):
    results = [QA(n) for i in range(m)]
    # results = sorted(results)
    return results

def density(result):
    """ Calculate the cumulative density.
    """
    unique = list(set(result))
    unique = sorted(unique)
    y = []
    for x in unique:
        count = len([i for i in result if i <= x])
        y.append(count/len(result))
    return unique, y

def plot(x, y):
    plt.plot(x, y)
    plt.xlabel('n', fontsize=40)
    plt.ylabel('Time', fontsize=40)
    plt.show()

def QD(m):
    x = [ i for i in range(200, 20001, 1000)]
    y = []
    for n in x:
        t = time.clock()
        QB(m, n)
        y.append(time.clock()-t)
    return x, y

def write(path, x, y):
    with open(path, 'w', encoding='utf8') as f:
        for a, b in zip(x, y):
            s = ' '.join([str(a), str(b)])
            f.write(s+'\n')

def read(path):
    x = []
    y = []
    with open(path, encoding='utf8') as f:
        for line in f:
            s = line.strip().split()
            x.append(float(s[0]))
            y.append(float(s[1]))
    return x, y

if __name__ == '__main__':
    n = 200
    m = 3000
    # print(QA(n))
    # result = QB(m, n)
    # print(sum(result)/len(result))

    # x, y = density(result)
    # plot(x, y)

    # t = time.clock()
    # QB(m, n)
    # print(time.clock()-t)

    # x, y =QD(5000)
    # write('coupon_5000.txt', x, y)
    x, y = read('coupon_5000.txt')
    plot(x, y)


#!/usr/bin/env python3

__author__ = "neutronics material maker development team"

import json
import os
import unittest

import openmc

import neutronics_material_maker as nmm

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

class test_object_properties(unittest.TestCase):
    def test_zaid_to_isotope(self):
        assert nmm.utils.zaid_to_isotope("3006") == "Li6"
        assert nmm.utils.zaid_to_isotope("03006") == "Li6"
        assert nmm.utils.zaid_to_isotope("003006") == "Li6"

        assert nmm.utils.zaid_to_isotope("8018") == "O18"
        assert nmm.utils.zaid_to_isotope("08018") == "O18"
        assert nmm.utils.zaid_to_isotope("008018") == "O18"

        assert nmm.utils.zaid_to_isotope("26056") == "Fe56"
        assert nmm.utils.zaid_to_isotope("026056") == "Fe56"
        assert nmm.utils.zaid_to_isotope("026056") == "Fe56"

        assert nmm.utils.zaid_to_isotope("092235") == "U235"
        assert nmm.utils.zaid_to_isotope("92235") == "U235"

    def test_isotope_to_zaid(self):
        assert nmm.utils.isotope_to_zaid("Li6") == "003006"

        assert nmm.utils.isotope_to_zaid("O18") == "008018"

        assert nmm.utils.isotope_to_zaid("Fe56") == "026056"

        assert nmm.utils.isotope_to_zaid("U235") == "092235"

    def test_entries_from_each_json_file_get_into_the_internal_dict(self):
        all_mats = nmm.AvailableMaterials().keys()

        assert "A-150 Tissue-Equivalent Plastic (A150TEP)" in all_mats
        assert "Zirconium Hydride (ZrH2)" in all_mats
        assert "Pb842Li158" in all_mats
        assert "FLiNaBe" in all_mats
        assert "WC" in all_mats
        assert "CuCrZr" in all_mats
        assert "DD_plasma" in all_mats
        assert "DT_plasma" in all_mats
        assert "Pb" in all_mats
        assert "Zr5Pb4" in all_mats
        assert "isotropic graphite HPG-59" in all_mats
        assert "Nb3Sn" in all_mats
        assert "ReBCO" in all_mats
        assert "He" in all_mats
        assert "xenon" in all_mats
        assert "Li4SiO4" in all_mats
        assert "Li2TiO3" in all_mats
        assert "Li" in all_mats
        assert "FLiNaK" in all_mats

    def test_number_of_materials_in_dict(self):
        import neutronics_material_maker as nmm_again

        assert len(nmm_again.AvailableMaterials().keys()) >= 418

    def test_dictionary_of_materials_makes_openmc_materials(self):

        for mat in nmm.AvailableMaterials().keys():
            print(mat)
            test_mat = nmm.Material(
                mat, temperature_in_K=300, pressure_in_Pa=5e6)

            assert isinstance(test_mat.openmc_material, openmc.Material)

    def test_dictionary_of_materials_makes_mcnp_materials(self):

        for mat in nmm.AvailableMaterials().keys():
            print(mat)
            test_mat = nmm.Material(
                mat, temperature_in_K=300, pressure_in_Pa=5e6, material_id=1
            )

            assert isinstance(test_mat.mcnp_material, str)

    def test_dictionary_of_materials_makes_fispact_materials(self):

        for mat in nmm.AvailableMaterials().keys():
            print(mat)
            test_mat = nmm.Material(
                mat,
                temperature_in_K=300,
                pressure_in_Pa=5e6,
                volume_in_cm3=1.5)

            assert isinstance(test_mat.fispact_material, str)

    def test_dictionary_of_materials_makes_serpent_materials(self):

        for mat in nmm.AvailableMaterials().keys():
            print(mat)
            test_mat = nmm.Material(
                mat, temperature_in_K=300, pressure_in_Pa=5e6)

            assert isinstance(test_mat.serpent_material, str)

    def test_adding_one_material_AddMaterialFromFile(self):
        test_material_1 = {
            "WC2": {
                "chemical_equation": "WC",
                "density": 18.0,
                "density_unit": "g/cm3",
                "percent_type": "ao",
            }
        }

        with open("extra_material_1.json", "w") as outfile:
            json.dump(test_material_1, outfile)

        number_of_materials = len(nmm.AvailableMaterials())
        nmm.AddMaterialFromFile("extra_material_1.json")

        assert number_of_materials + 1 == len(nmm.AvailableMaterials())
        assert "WC2" in nmm.AvailableMaterials().keys()
        os.system("rm extra_material_1.json")

    def test_adding_two_material_AddMaterialFromFile(self):
        test_material_1 = {
            "WC3": {
                "chemical_equation": "WC",
                "density": 18.0,
                "density_unit": "g/cm3",
                "percent_type": "ao",
            },
            "WB2": {
                "chemical_equation": "WB",
                "density": 15.3,
                "density_unit": "g/cm3",
                "percent_type": "ao",
            },
        }

        with open("extra_material_1.json", "w") as outfile:
            json.dump(test_material_1, outfile)

        number_of_materials = len(nmm.AvailableMaterials())
        nmm.AddMaterialFromFile("extra_material_1.json")

        assert number_of_materials + 2 == len(nmm.AvailableMaterials())
        assert "WC3" in nmm.AvailableMaterials().keys()
        assert "WB2" in nmm.AvailableMaterials().keys()
        os.system("rm extra_material_1.json")

    def test_replacing_material_using_AddMaterialFromFile(self):
        test_material_1 = {
            "Li4SiO4": {
                "chemical_equation": "WC",
                "density": 18.0,
                "density_unit": "g/cm3",
                "percent_type": "ao",
            }
        }

        with open("extra_material_1.json", "w") as outfile:
            json.dump(test_material_1, outfile)

        number_of_materials = len(nmm.AvailableMaterials())
        nmm.AddMaterialFromFile("extra_material_1.json")

        assert number_of_materials == len(nmm.AvailableMaterials())
        assert "Li4SiO4" in nmm.AvailableMaterials().keys()
        os.system("rm extra_material_1.json")

    def test_AddMaterialFromDir(self):
        os.system("mkdir new_materials")

        test_material_1 = {
            "Li4SiO42": {
                "chemical_equation": "WC",
                "density": 18.0,
                "density_unit": "g/cm3",
                "percent_type": "ao",
            }
        }

        with open(
            os.path.join("new_materials", "extra_material_1.json"), "w"
        ) as outfile:
            json.dump(test_material_1, outfile)

        test_material_2 = {
            "Li4SiO43": {
                "chemical_equation": "WC",
                "density": 18.0,
                "density_unit": "g/cm3",
                "percent_type": "ao",
            }
        }

        with open(
            os.path.join("new_materials", "extra_material_2.json"), "w"
        ) as outfile:
            json.dump(test_material_2, outfile)

        number_of_materials = len(nmm.AvailableMaterials())
        nmm.AddMaterialFromDir("new_materials")

        os.system("rm -rf new_materials")
        assert number_of_materials + 2 == len(nmm.AvailableMaterials())
        assert "Li4SiO42" in nmm.AvailableMaterials().keys()
        assert "Li4SiO43" in nmm.AvailableMaterials().keys()


import discord
from discord.ext import commands

ALLOWED_CHARS = {"_", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"}

class sky_cog(commands.Cog):
    def __init__(self, bot):
        self.client = bot

    @commands.command()
    async def sky(self, ctx, target=None):
        if target is None:  # If they don't give any args, use their discord nick
            nick = ctx.author.display_name  #  Set nick to their author of the command's nickname
            target = nick.split("]")[1] if "]" in nick else nick  # If the nickname contains a ], get all the characters after the ]
            target = "".join([char for char in target if char.lower() in ALLOWED_CHARS])  # Create a new string with only the allowed characters (0-9, A-Z, _)

        await ctx.send(f"https://sky.shiiyu.moe/stats/{target}")  # Send the link with the target's name


#########
# Copyright (c) 2019 Cloudify Platform Ltd. All rights reserved
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#       http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
#  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#  * See the License for the specific language governing permissions and
#  * limitations under the License.

# region Service components

SERVICE_COMPONENTS = {
    # The ordering in each service is required as some components depend on
    # the previously installed ones
    # Please do not change the order in which the components of each service
    # will be installed
    "database_service": [
        "postgresql_server"
    ],
    "queue_service": [
        "rabbitmq",
    ],
    "manager_service": [
        "manager",
        "postgresql_client",
        "restservice",
        "manager_ip_setter",
        "nginx",
        "python",
        "amqp_postgres",
        "stage",
        "composer",
        "mgmtworker",
        "cli",
        "usage_collector",
        "patch",
        "sanity"
    ]
}

# endregion

# region Service package names

# Service names constants
DATABASE_SERVICE = 'database_service'
QUEUE_SERVICE = 'queue_service'
MANAGER_SERVICE = 'manager_service'

# endregion

# region Service installation order

# This is to ensure that database is installed first in any kind of
# separated installation
SERVICE_INSTALLATION_ORDER = [
    DATABASE_SERVICE,
    QUEUE_SERVICE,
    MANAGER_SERVICE
]

# endregion


import ahkab
import socket
import sys
# from ahkab import dc_guess
#ahkab --help

# # Create a TCP/IP socket
# s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

# # Connect the socket to the port where the server is listening
# server_address = ('localhost', 1234)
# s.connect(server_address)

mycir = ahkab.Circuit('Simple Circuit')

mycir.add_resistor('R1', 'n1', mycir.gnd, value=5)
mycir.add_vsource('V1', 'n2', 'n1', dc_value=8)
mycir.add_resistor('R2', 'n2', mycir.gnd, value=2)
mycir.add_vsource('V2', 'n3', 'n2', dc_value=4)
mycir.add_resistor('R3', 'n3', mycir.gnd, value=4)
mycir.add_resistor('R4', 'n001', 'n3', value=1)
mycir.add_vsource('V3', 'n4', mycir.gnd, dc_value=10)
mycir.add_resistor('R5', 'n2', 'n4', value=4)
mycir.add_resistor('R6','n4','n001',value=10)

opa = ahkab.new_op() # Assembles an OP analysis and returns the analysis object.
r = ahkab.run(mycir, opa)['op']
#print(r)

# print('---------------------------------------------')
# a = mycir.get_nodes_number()
# print(a)
# print('----------------------------------------------')

#print the output to a file
with open('test_file.txt', 'w') as f:
    print(r, file=f)

with open('test_file.txt', 'r') as f:
    data = f.read()#.replace('\n', '')

print(data)

sock.bind(('', port))
sock.listen(1)
client,clientinfo=sock.accept()
while True:
    var = client.recv(1024)
    s.sendall(var)

#
# #read the voltages from the file
# voltage_list ={}
# o = open('test_file.txt','r')
# o1 = o.readlines()
# for x in o1:
#     print(x[20:30])
#


from music_app.models import UserFavoriteSong, Song
from rest_framework import status, serializers, viewsets, permissions
from rest_framework.response import Response
from music_app import serializers as ser
from django.http import Http404

class UserReadWritePermission(permissions.BasePermission):
    message = 'User can only see and edit his own information'

    def has_object_permission(self, request, view, obj):
        return obj.id == request.user.id or request.user.is_superuser

class UserFavSongViewSet(viewsets.ModelViewSet):
    permission_classes = [UserReadWritePermission]

    queryset = UserFavoriteSong.objects.all()
    serializer_class = ser.UserFavSongSerializer

    def list(self, request):
        user = request.user

        if not user.is_authenticated:
            raise serializers.ValidationError("You can't access this endpoint.")

        user_fav_songs = UserFavoriteSong.objects.filter(user=user)

        serializer = ser.UserFavSongSerializer(user_fav_songs, many=True)

        return Response(serializer.data)

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

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


from flask import Flask, Response,jsonify, request, flash, render_template, current_app, redirect
from baza import Ormar, Stalak_za_obucu
import sqlite3
import secrets
import os

db = sqlite3.connect("baza.sqlite", check_same_thread=False)

app = Flask(__name__)

cur = db.cursor()

@app.route('/')
def index():
	return render_template('index.html')

@app.route('/ormar', methods=['GET', 'POST'])
def ormar():
	if request.method == 'POST':
		naziv = request.form.get('naziv')
		velicina = request.form.get('velicina')
		vlasnik = request.form.get('vlasnik')
		datum = request.form.get('datum')
		db.execute("INSERT INTO Ormar(naziv,velicina,vlasnik, datum) VALUES (:naziv,:velicina,:vlasnik, :datum)", {"naziv":naziv,"velicina":velicina,"vlasnik":vlasnik, "datum":datum})
		db.commit()
		return render_template('ormar.html')
	return render_template('ormar.html')

@app.route('/pokazi', methods=['GET', 'POST'])
def pokazi():
	if request.method == 'POST':
		print("radi")
		ime = request.form.get('ime')

		ime1 = request.form.get('ime1')
		print(ime)
		print(ime1)
		if ime != None:
			db.execute("DELETE FROM Ormar WHERE id="+ime+"")
			db.commit()
		if ime1 != None:
			db.execute("DELETE FROM Stalak_za_obucu WHERE id="+ime1+"")
			db.commit()

	cur.execute("select * from Ormar")
	data = cur.fetchall()
	cur.execute("select * from Stalak_za_obucu")
	data1 = cur.fetchall()
	return render_template('pokazi.html', value=data, value1=data1)

@app.route('/stalak', methods=['GET', 'POST'])
def stalak():
	if request.method == 'POST':
		naziv = request.form.get('naziv')
		velicina = request.form.get('velicina')
		vlasnik = request.form.get('vlasnik')
		datum = request.form.get('datum')
		db.execute("INSERT INTO Stalak_za_obucu(naziv,velicina,vlasnik, datum) VALUES (:naziv,:velicina,:vlasnik, :datum)", {"naziv":naziv,"velicina":velicina,"vlasnik":vlasnik, "datum":datum})
		db.commit()
		return render_template('stalak.html')
	return render_template('stalak.html')
if __name__ == "__main__":
	app.run(debug=True)


#!/usr/bin/env python

import asyncio
import aiohttp

from datetime import datetime
from types import AsyncGeneratorType

from aspider.request import Request
from aspider.utils import get_logger

try:
    import uvloop

    asyncio.set_event_loop_policy(uvloop.EventLoopPolicy())
except ImportError:
    pass

class Spider:
    name = 'aspider'
    request_config = None
    request_queue = asyncio.Queue()

    failed_counts, success_counts = 0, 0
    start_urls, worker_tasks = [], []

    def __init__(self, loop=None):
        if not self.start_urls or not isinstance(self.start_urls, list):
            raise ValueError("Spider must have a param named start_urls, eg: start_urls = ['https://www.github.com']")
        self.logger = get_logger(name=self.name)
        self.loop = loop or asyncio.get_event_loop()

    async def parse(self, res):
        raise NotImplementedError

    async def start_master(self):
        for url in self.start_urls:
            request_ins = Request(url=url,
                                  callback=self.parse,
                                  metadata=getattr(self, 'metadata', None),
                                  headers=getattr(self, 'headers', None),
                                  request_config=getattr(self, 'request_config'),
                                  request_session=getattr(self, 'request_session', None),
                                  res_type=getattr(self, 'res_type', 'text'),
                                  **getattr(self, 'kwargs', {}))
            self.request_queue.put_nowait(request_ins.fetch_callback())
        workers = [asyncio.ensure_future(self.start_worker()) for i in range(2)]
        await self.request_queue.join()
        for work in workers:
            work.cancel()

    async def start_worker(self):
        while True:
            request_item = await self.request_queue.get()
            self.worker_tasks.append(asyncio.ensure_future(request_item))
            if self.request_queue.empty():
                done, pending = await asyncio.wait(self.worker_tasks)
                for task in done:
                    callback_res, res = task.result()
                    if isinstance(callback_res, AsyncGeneratorType):
                        async for each in callback_res:
                            self.request_queue.put_nowait(each.fetch_callback())
                    if res.body is None:
                        self.failed_counts += 1
                    else:
                        self.success_counts += 1
                self.worker_tasks = []
            self.request_queue.task_done()

    def make_request_from_url(self, url):
        yield Request(url=url)

    @classmethod
    def start(cls):
        spider_ins = cls()
        spider_ins.logger.info('Spider started!')
        start_time = datetime.now()
        try:
            spider_ins.loop.run_until_complete(spider_ins.start_master())
        except KeyboardInterrupt:
            for task in asyncio.Task.all_tasks():
                task.cancel()
            spider_ins.loop.stop()
            spider_ins.loop.run_forever()
        finally:
            end_time = datetime.now()
            spider_ins.logger.info(f'Total requests: {spider_ins.failed_counts + spider_ins.success_counts}')
            if spider_ins.failed_counts:
                spider_ins.logger.info(f'Failed requests: {spider_ins.failed_counts}')
            spider_ins.logger.info(f'Time usage: {end_time - start_time}')
            spider_ins.logger.info('Spider finished!')
            spider_ins.loop.close()


#!/usr/bin/python

import requests
import re
import argparse
from bs4 import BeautifulSoup

class Crawler(object):
    #http://www.shopping.com/clothing/batman/products~PG-2?KW=batman  is the url of shopping.com when searched for batman and page 2
    #so , pattern is defined in
    #search_tag  = item we want to search,,,,,,,,,page_tag = page we want to search
    Search = 'http://www.shopping.com/batman/products?CLT=SCH&KW={search_tag}'
    Search_in_page = 'http://www.shopping.com/products~PG-{page_tag}?KW={search_tag}'

    def __init__(self, search, page=1):
        self.search = search
        self.page=1
        if page != 1:
            self.page = page

    #returns data from HTML page

    def data_from_page(self,modified = 0):
        '''
        Each item is class="gridBox term, so fetching items based on that keyword
        '''
        if modified != 0:
            page = requests.get(self.Search_in_page.format(page_tag=modified,search_tag=self.search,))
        else:
            page = requests.get(self.Search_in_page.format(page_tag=self.page, search_tag=self.search, ))

        if page.status_code != 200:
            raise ValueError ("Something Went Wrong, Wrong Request\nAborting...\nAborting ...")

        soup = BeautifulSoup(page.text,'html.parser')
        item_list = soup.select('.gridBox')
        return item_list

    def items_from_page(self):
        #Items having class 'gridItemBtm' are search results.
        items_to_return = []
        item_list = self.data_from_page()

        for item in item_list:
            selected = item.select_one('.gridItemBtm')
            if selected:
                name = selected.select_one('.productName')
                if name:
                    #print ("i am here")
                    f_name = name.attrs.get('title')  or name.select_one('span').attrs.get('title')

                price = selected.select_one('.productPrice')
                if price:
                    f_price = price.string or price.select_one('a').string

                seller = selected.select_one('.newMerchantName')
                if seller:
                    f_seller = seller.string or seller.select_one('a').string

                if f_name and f_price and f_seller:
                    #print (f_name +"     "+f_price.strip)
                    items_to_return.append({'name':f_name,'price':f_price,'seller':f_seller})

        return items_to_return

    #if number of results are mentioned as 1500+ then we use this
    def using_PL_way(self):
        page = requests.get(self.Search.format(search_tag=self.search))
        if page.status_code != 200:
            raise ValueError("Something Went Wrong, Wrong Request\nAborting...\nAborting...")
        soup = BeautifulSoup(page.text, 'html.parser')
        items = soup.findAll("div", {"class": "paginationNew"})
        #print (len(items))
        #print(str(items[0]))
        #finding all digits in this segment
        num_list = re.findall(r'\d+',str(items))
        results = map(int, num_list)
        #print(max(results));

        #each page has 40 items , we have to find the items in last page now, which is the largest number in the list

        #self.page = max(results)
        item_list = self.data_from_page(max(results))
        count = 0
        for item in item_list:
            selected = item.select_one('.gridItemBtm')
            if selected:
                name = selected.select_one('.productName')
                price = selected.select_one('.productPrice')
                seller = selected.select_one('.newMerchantName')
                if name and price and seller:
                    count = count + 1

        return (max(results)-1)*40 + count;

    def number_of_items(self):
        page = requests.get(self.Search.format(search_tag=self.search))
        if page.status_code != 200:
            raise ValueError("Something Went Wrong, Wrong Request\nAborting...")
        #print(page.text);
        soup = BeautifulSoup(page.text, 'html.parser')
        #soup = BeautifulSoup(open(self.Search.format(search_tag=self.search)))
        #print(soup);
        result = None
        try:
            items = soup.findAll("span", {"class": "numTotalResults"})[0].string
            #items = soup.select('span#numTotalResults')[0].text
            item_str = ""
            #print(items);
            for item in items:
                item_str = item_str + str(item)
            #print(item_str)
            delim = "of"
            after_split = item_str.split(delim)
            result = (str(after_split[1]).strip())
            if "1500" in result:
                result = self.using_PL_way()
        except (IndexError, ValueError) as err:
            print("Something Went Wrong, No items present for your search item\nAborting ...")

        return result

    def print_items(self,items):
        print ("- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -")
        for item in items:
            print ("Item  : "+item.get('name').strip()+"\nPrice : "+item.get('price').strip()+"\nSeller: "+item.get('seller'))
            print ("- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -")

if __name__ == '__main__':
    args = argparse.ArgumentParser()
    args.add_argument('search_tag',help="<enter search keyword here>")
    args.add_argument('page_tag', default=None,help="<enter page number here {optional}> ",nargs='?')

    args_list = args.parse_args()

    try:
        if args_list.page_tag and args_list.search_tag:
            crawler = Crawler(args_list.search_tag,args_list.page_tag)
            items = crawler.number_of_items()
            if items:
                print(("No of Items for {} : {}").format(args_list.search_tag,items))
                ans = (crawler.items_from_page())
                if ans:
                    crawler.print_items(ans)
                else:
                    print("This page is not available for this product")
        elif args_list.search_tag:
            crawler = Crawler(args_list.search_tag)
            items = crawler.number_of_items()
            if items:
                print(("No of Items for {} : {}").format(args_list.search_tag, items))
            #else:
            #    print(("No of Items for {} : 0").format(args_list.search_tag))
    except ValueError:
        args.print_help()


# Module to get data stored at MSI at Dryad
from urllib import request
import csv
import numpy as np
from config.config import config, cfg_path

def get_dryad_ss_data(retrieve=True):
    ############################
    # Get Data From DATA DRYAD
    ############################

    # load config
    db_path = cfg_path['db']

    url_data = 'http://datadryad.org/bitstream/handle/10255/dryad.86348/consensus_data.csv?sequence=1'

    output_file = db_path + 'consolidated_annotations.csv'
    if retrieve:
        request.urlretrieve(url_data, output_file)

    ############################
    # Get Data From Local Drive
    ############################

    # read file
    file = open(output_file, "r")
    datareader = csv.reader(file)

    data_dict = dict()
    labels = list()

    counter = 0
    for row in datareader:
        counter += 1
        # capture header
        if counter == 1:
            header = row
            continue
        # filter for one species only
        if not row[6] == '1':
            continue
        # fill data
        info_dict = {name: entry for entry, name in zip(row, header)}
        data_dict[row[0]] = {'y_label': row[7],
                             'info': info_dict}
        labels.append(row[7])
    file.close()

    # get all possible labels
    label_counts = np.unique(labels, return_counts=True)

    label_list = list()
    for l, c in zip(label_counts[0], label_counts[1]):
        print("Label: %s, Counts: %s" % (l, c))
        label_list.append(l)

    return data_dict

if __name__ == "__main__":
    dd = get_dryad_ss_data()


import sqlitedatastore as datastore
from annoutil import find_x_including_y, find_xs_in_y

if __name__ == '__main__':
    datastore.connect()
    anno_name = 'affiliation'

    for doc_id in datastore.get_all_ids(limit=-1):
        row = datastore.get(doc_id, fl=['content'])
        text = row['content']
        sentences = datastore.get_annotation(doc_id, 'sentence')
        tokens = datastore.get_annotation(doc_id, 'token')
        annos = datastore.get_annotation(doc_id, anno_name)
        for sentence in sentences:
            annos_in_sentence = find_xs_in_y(annos, sentence)
            if annos_in_sentence == []:
                continue
            prev = False
            for token in find_xs_in_y(tokens, sentence):
                if find_x_including_y(annos_in_sentence, token) is None:
                    prev = False
                    print('{0}\t{1}\t{2}'.format(
                        text[token['begin']:token['end']], token['POS'], 'O'))
                else:
                    if prev:
                        print('{0}\t{1}\tI-{2}'.format(
                            text[token['begin']:token['end']], token['POS'], anno_name))
                    else:
                        print('{0}\t{1}\tB-{2}'.format(
                            text[token['begin']:token['end']], token['POS'], anno_name))
                        prev = True
            print()  # 文の区切り
    datastore.close()


import  time
from functools import lru_cache

# @lru_cache(maxsize=3)
# def somework(n) :
#     time.sleep(n)
#     return  n
#
# if __name__ == "__main__" :
#     print("now running work done")
#     somework(3)
#     somework(1)
#     somework(2)
#     somework(4)
#     print("Done...calling again")
#     somework(3)
#     print("Called again")

cach_value = int (input("Enter no of previous calls you want to cach :- "))

@lru_cache(maxsize=cach_value)
def playing (n) :
    time.sleep(n)

print("Playing")
playing(3)
playing(4)
print("waited for 2 calls")
playing(2)
print("third call")
playing(4)
print("did't wait for 4 sec")


##
## Imprima una tabla en formato CSV que contenga por cada clave
## de la columna 5, la correspondiente cantidad de registros
## asociados (filas en el archivo)
##
## aaa,13
## bbb,16
## ccc,23
## ddd,23
## eee,15
## fff,20
## ggg,13
## hhh,16
## iii,18
## jjj,18
##
##
### Punto 10
import sys
x = open('data.csv', 'r').readlines()
x = [row[0:-1].split('\t') for row in x]
a = []
c = []
for i in x:
    a.append(i[4].split(","))
for j in a:
    for k in j:
        b = k.split(":")
        c.append(b[0])
uniquepatters = set(c)
cant_registros = [(l, c.count(l)) for l in sorted(uniquepatters)]

for i in cant_registros:
    print("%s,%d" %(i[0], i[1]))


import tweepy
from textblob import TextBlob
import time
import matplotlib.pyplot as plt
import matplotlib.animation as animation
import json
import re

#overriding methods from the twitter api for analysis of real time text

class MyStreamListener(tweepy.StreamListener):

    def on_data(self, data):
    	json_data=json.loads(data)
    	text_data=json_data["text"]
    	#clean the text from url
    	text_data=re.sub(r"https\S+", "", text_data)
    	#text_data=text_data.decode("utf-8")

    	print(text_data)
    	print(json_data["lang"])
    	print(json_data['user']['time_zone'])
		# declaring all the global variables required for graph and pie charts

    	global positive
    	global negative
    	global net
    	global init_time
    	global pos_count
    	global neg_count
    	global neutral_count

    	senti=0
    	t=int(time.time()-init_time)
    	analysis=TextBlob(text_data)
		# translate the tweet if the tweet is not in english
    	if(json_data["lang"] != 'en'):
    		try :
    			analysis.translate(to='en')
    		except :
    			pass

    	#print(analysis.sentiment)
    	for line in analysis.sentences :
    		senti=senti+line.sentiment.polarity
    		if line.sentiment.polarity >=0 :
    			positive=positive+line.sentiment.polarity
    			if line.sentiment.polarity==0:
    				neutral_count=neutral_count+1
    			else :
    				pos_count=pos_count+1

    		else :
    			negative=negative+line.sentiment.polarity
    			neg_count=neg_count+1

    	net=net+senti
    	print(senti)
    	print("positive :"+str(positive))
    	print("negative :"+str(negative))
    	print("net :"+str(net))

    	# finally priniting the graph

    	plt.subplot(1,2,1)
    	plt.title('time v/s sentiment graph')
    	plt.grid(color='c')

    	plt.axis([0,300,-40,40])
    	plt.xlabel("time(seconds)")
    	plt.ylabel("sentiment")
    	plt.plot([t],[positive],'go',[t] ,[negative],'ro',[t],[net],'bo',label='linear')

    	#piechart

    	plt.subplot(1,2,2).cla()
    	#plt.gcf().clear()
    	plt.title('pie chart')
    	labels='positive','negative','neutral'
    	sizes=[(pos_count/(pos_count+neg_count+neutral_count))*100,(neg_count/(pos_count+neg_count+neutral_count))*100,(neutral_count/(pos_count+neg_count+neutral_count))*100]
    	plt.pie(sizes, labels=labels, autopct='%1.1f%%',shadow=True, startangle=90)
    	plt.axis('equal')

		###################################################################
    	plt.show()
    	plt.pause(0.0001)
    	print("")

    def on_error(self,status):
		# print out any error ie. time out, credentials mismatch , system - server time mismatch
    	print (status)

#test 1 : 401 authorization error rectified by changing the time line
#if the system time has error by more than 15 minutes with respect to official time we get 401 error

if __name__ == '__main__':

	# Enter your twitter application credentials here

	consumer_key= '*********************************'
	consumer_secret= '******************************'

	access_token='************************************************'
	access_token_secret='*****************************************'

	#variables for analysis
	positive=0
	negative=0
	net=0
	pos_count=0
	neg_count=0
	neutral_count=0

	init_time=time.time()
	plt.ion()

	#authenticate

	auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
	auth.set_access_token(access_token, access_token_secret)
	api = tweepy.API(auth)
	myStreamListener = MyStreamListener()
	myStream = tweepy.Stream(auth = api.auth, listener=myStreamListener)
	myStream.filter(track=[input("input keyword that you want to search : ")])


import datetime
import pandas as pd
import numpy as np
from jqdata import *
import datetime
import pandas as pd
import numpy as np
from jqfactor import get_factor_values
from jqdata import *
class private_tools():
    '''比较DataFrame'''
    def compare_serice(self, serice_1, serice_2, flag):
        if flag == 'max':
            compare_flag = (serice_1 > serice_2)
            serice_2[compare_flag] = serice_1[compare_flag]
            return serice_2
        if flag == 'min':
            compare_flag = (serice_1 < serice_2)
            serice_2[compare_flag] = serice_1[compare_flag]
            return serice_2
    '''利用采样函数，设定转换周期period_type  转换为周是'W',月'M',季度线'Q',五分钟'5min',12天12D'''
    def get_period_date(self, peroid,start_date, end_date):
        stock_data = get_price('000001.XSHE',start_date,end_date,'daily',fields=['close'])
        #types = stock_data.index.dtype 查看index的数据类型
        stock_data['date']=stock_data.index #resample按照index自然日采样，获取日期临时值
        start_date_0 = stock_data['date'][0].strftime("%Y-%m-%d")
        period_stock_data=stock_data.resample(peroid,how= 'last').dropna() #进行转换，周线的每个变量都等于那一周中最后一个交易日的变量值
        date_list = period_stock_data['date'].apply(lambda s: s.strftime('%Y-%m-%d')).values.tolist() #numpy.arrange 转 list
        date_list.insert(0, start_date_0)
        return date_list
    '''过滤次新股票'''
    def filter_new_stock(self, stocks, currernt_date, delta = 30 * 6):
        stockList=[]
        currernt_date = datetime.datetime.strptime(currernt_date, "%Y-%m-%d")
        for stock in stocks:
            start_date = get_security_info(stock).start_date
            if start_date < (currernt_date - datetime.timedelta(days = delta)).date():
                stockList.append(stock)
        return stockList
    '''
    交易日位移
    shift：位移，为避免未来函数，不提供时间后移功能
    '''
    def shift_trading_day(self, current_date, shift):
        stock_data = get_price('000001.XSHE',count = shift * 2, end_date = current_date, frequency = 'daily',fields=['close'])
        #types = stock_data.index.dtype 查看index的数据类型
        trade_date = stock_data.index.map(lambda x : x.strftime('%Y-%m-%d')).values.tolist()
        trade_date_index = trade_date.index(current_date) - shift
        return trade_date[trade_date_index]
    '''过滤停牌股票'''
    def get_nopaued_stock(self, stock_list, cunrrent_date):
        suspened_info_df = get_price(stock_list, start_date=cunrrent_date, end_date=cunrrent_date, frequency='daily', fields='paused', panel=False)
        suspened_info_df.set_index('code', inplace = True)
        suspened_info_df = suspened_info_df[suspened_info_df['paused'] < 1]
        return suspened_info_df.index.values.tolist()
    '''获取股票池'''
    def filter_st_stock(self, stocks, current_date):
        #剔除ST股
        st_stock_info = get_extras('is_st', stocks, count = 1, end_date = current_date)
        stock_filter = [stock for stock in stocks if not st_stock_info[stock][0]]
        return stock_filter
    '''数据清洗函数工具'''
    '''
    mad中位数去极值法
    series:待处理数据，Series
    n：几个单位的偏离值
    '''
    def filter_extreme_MAD(self, series_pre, n):
        median = series_pre.quantile(0.5)
        new_median = ((series_pre - median).abs()).quantile(0.50)
        max_range = median + n * new_median
        min_range = median - n * new_median
        return np.clip(series_pre, min_range, max_range)

    '''
    方差取极值
    series_pre:待处理数据，数据类型Series
    std:为几倍的标准差，
    have_negative 为布尔值，是否包括负值
    '''
    def filter_extreme_std(series_pre, n=3, have_negative=True):

        series_pre_copy = series_pre.dropna().copy()
        if have_negative == False:
            series_pre_copy = series_pre_copy[series_pre_copy >= 0]
        else:
            pass
        edge_up = series_pre_copy.mean() + n * series_pre_copy.std()
        edge_low = series_pre_copy.mean() - n * series_pre_copy.std()
        series_pre_copy[series_pre_copy > edge_up] = edge_up
        series_pre_copy[series_pre_copy < edge_low] = edge_low
        return series_pre_copy
    '''
    标准化函数：
    series_pre:待处理数据，数据类型Series
    ty：标准化类型:1 MinMax,2 Standard,3 maxabs
    '''
    def standardize(self, series_pre, ty = 2):
        series_pre_copy = series_pre.dropna().copy()
        if int(ty) == 1:
            series_pre_copy = (series_pre_copy - series_pre_copy.min()) / (series_pre_copy.max() - series_pre_copy.min())
        elif ty == 2:
            std = series_pre_copy.std()
            if std == 0:
                std = 1
            re = (series_pre_copy - series_pre_copy.mean()) / std
        elif ty == 3:
            re = series_pre_copy / (10 ** np.ceil(np.log10(series_pre_copy.abs().max())))
        return re
    def standardize_df(self, factor_data, ty = 2):
        result = pd.DataFrame()
        for factor in factor_data.columns:
            result[factor] = self.standardize(factor_data[factor], ty = 2)
        return result

    '''
    缺失值处理，一般情况应该去除drop（how = ‘any’）,但A股数据量本来缺少，最终还是决定以均值替代
    若是行业中因子值为空，则以所有行业均值代替
    某只股票因子值为空，用行业平均值代替，
    依然会有nan，则用所有股票平均值代替
    '''
    def replace_nan_indu(self, factor_data, stockList, industry_code, date):

        i_Constituent_Stocks = {}
        if isinstance(factor_data, pd.DataFrame):
            data_temp = pd.DataFrame(index=industry_code, columns=factor_data.columns)
            for i in industry_code:
                temp = get_industry_stocks(i, date)
                i_Constituent_Stocks[i] = list(set(temp).intersection(set(stockList)))
                data_temp.loc[i] = np.mean(factor_data.loc[i_Constituent_Stocks[i], :])
            for factor in data_temp.columns:
                # 行业缺失值用所有行业平均值代替
                null_industry = list(data_temp.loc[pd.isnull(data_temp[factor]), factor].keys())
                for i in null_industry:
                    data_temp.loc[i, factor] = np.mean(data_temp[factor])
                null_stock = list(factor_data.loc[pd.isnull(factor_data[factor]), factor].keys())
                for i in null_stock:
                    industry = get_key(i_Constituent_Stocks, i)
                    if industry:
                        factor_data.loc[i, factor] = data_temp.loc[industry[0], factor]
                    else:
                        factor_data.loc[i, factor] = np.mean(factor_data[factor])
        return factor_data
    '''
    中性化函数，对需要待中性化的因子进行中性化回归，其残差即为中性化后的因子值
    输入：
    mkt_cap：以股票为index，市值为value的Series,
    factor：以股票code为index，因子值为value的Series,
    输出：
    中性化后的因子值series
    '''
    def neutralize(self, factor, mkt_cap=False, industry=False):
        y = factor
        if type(mkt_cap) == pd.Series:
            # LnMktCap = mkt_cap.apply(lambda x:math.log(x))
            if industry:  # 行业、市值
                dummy_industry = self.get_industry_exposure(factor.index)
                x = pd.concat([mkt_cap, dummy_industry.T], axis=1)
            else:  # 仅市值
                x = mkt_cap
        elif industry:  # 仅行业
            dummy_industry = self.get_industry_exposure(factor.index)
            x = dummy_industry.T
        result = sm.OLS(y.astype(float), x.astype(float)).fit()
        return result.resid

    def neutralize_df(self, factor_df, exclude = None, mkt_cap=False, industry=False):
        stocks = factor_df.index
        result = pd.DataFrame()
        for factor in factor_df.columns:
            if factor == exclude:
                result[factor] = factor_df[factor]
            else:
                y = factor_df[factor]
                if type(mkt_cap) == pd.Series:
                    # LnMktCap = mkt_cap.apply(lambda x:math.log(x))
                    if industry:  # 行业、市值
                        dummy_industry = self.get_industry_exposure(stocks)
                        x = pd.concat([mkt_cap, dummy_industry.T], axis=1)
                    else:  # 仅市值
                        x = mkt_cap
                elif industry:  # 仅行业
                    dummy_industry = self.get_industry_exposure(stocks)
                    x = dummy_industry.T
                result[factor] = sm.OLS(y.astype(float), x.astype(float)).fit().resid
        return result

    '''为股票池添加行业标记'''
    def get_industry_exposure(self, stock_list):
        df = pd.DataFrame(index = (get_industries(name='sw_l1').index), columns=stock_list)
        for stock in stock_list:
            try:
                df[get_industry_code_from_security(stock)][stock] = 1
            except:
                continue
        return df.fillna(0)  # 将NaN赋为0

    '''查询个股所在行业函数代码（申万一级） ,为中性化函数的子函数'''
    def get_industry_code_from_security(self, security, date=None):
        industry_index = get_industries(name='sw_l1').index
        for i in range(0, len(industry_index)):
            try:
                index = get_industry_stocks(industry_index[i], date=date).index(security)
                return industry_index[i]
            except:
                continue
        return u'未找到'


#should be reduced down further to for demo
import os
from flask import Flask, request, url_for, redirect, render_template
from flask_sqlalchemy import SQLAlchemy

app = Flask(__name__)
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False

basedir = os.path.abspath(os.path.dirname(__file__))
app.config['UPLOAD_FOLDER'] = basedir + '/static/'

app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///' + os.path.join(basedir, 'art.sqlite3')
app.config['SECRET_KEY'] = 'RANDOM STRING'

db = SQLAlchemy(app)

class Artwork(db.Model):
    id = db.Column('student_id', db.Integer, primary_key = True)
    title = db.Column(db.String(100))
    image_url = db.Column(db.String(200))

    def __init__(self, title, image_url):
        self.title = title
        self.image_url = image_url

@app.route('/', methods = ['GET', 'POST'])
def new():
    if request.method == 'POST':
        file = request.files['upload']
        filename = file.filename
        file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
        artwork = Artwork(  request.form['title'],
                            str('/static/' + filename)
                                )
        db.session.add(artwork)
        db.session.commit()
        return redirect(url_for('new'))
    return render_template('new.html', artwork=Artwork.query.all())

if __name__ == "__main__":
    db.create_all()
    app.run(debug=True)


from __future__ import division
import torch
from torch.autograd import Variable
from torch.utils import data
# from resnet import FCN
from jg_concatener import FCN
# from upsample import FCN
# from gcn import FCN
from jg_datasets import sunrgbd_rgbd
from loss import CrossEntropy2d, CrossEntropyLoss2d
from visualize import LinePlotter
from transform import ReLabel, ToLabel, ToSP, Scale
from torchvision.transforms import Compose, CenterCrop, Normalize, ToTensor
from tqdm import * #tqdm
from PIL import Image
import numpy as np
import getpass

num_classes = 38
batch_size=10

input_transform = None #Compose([    Scale((224, 224), Image.BILINEAR),    ToTensor(),    Normalize([.485, .456, .406], [.229, .224, .225]),])
target_transform = None #Compose([    Scale((224, 224), Image.NEAREST),    ToSP(224),    ToLabel(),    ReLabel(255, num_classes),])
sample_strategy_train="crop_flip"
sample_strategy_val="crop"

def main():

    trainloader = data.DataLoader(sunrgbd_rgbd(split='train', img_transform=input_transform,label_transform=target_transform,sample_strategy=sample_strategy_train),batch_size=batch_size, shuffle=True, pin_memory=True)
    valloader = data.DataLoader(sunrgbd_rgbd(split='test', img_transform=input_transform,label_transform=target_transform,sample_strategy=sample_strategy_val),batch_size=batch_size, shuffle=True, pin_memory=True)
    model = FCN(num_classes)

    try:
        print('Loading path')
        path = '/home/'+getpass.getuser()+'/THESE.JORIS/pytorch-ss/pth/fcn-deconv-4.pth'
        model.load_state_dict(torch.load(path));
    except:
        print('Fail while loading model weights')

    if torch.cuda.is_available():
        # model = FCN(38) #torch.nn.DataParallel(FCN(22))
        model.cuda()
    else:
        print('Cuda not available.')

    epoches = 1000
    start_epoch=4
    print("Starting at epoch",start_epoch)
    lr = 1e-4
    weight_decay = 2e-5
    momentum = 0.9
    weight = torch.ones(num_classes)

    name_labels = ['unlabelled','wall','floor','cabinet','bed','chair','sofa','table','door','window','bookshelf','picture','counter','blinds','desk','shelves','curtain','dresser','pillow','mirror','floor_mat','clothes','ceiling','books','fridge','tv','paper','towel','shower_curtain','box','whiteboard','person','night_stand','toilet','sink','lamp','bathtub','bag']
    weights_sunrgbd_37_train={0: 0.018636841284528617, 1: 0.022513927371236728, 2: 0.026058448931433355, 3: 0.21119854859406284, 4: 0.14570473485321114, 5: 0.052291865520171586, 6: 0.1838193904491639, 7: 0.081731792393125363, 8: 0.22783170525509927, 9: 0.19251694006259695, 10: 0.98654725370753249,
                                 11: 0.89443838783498675, 12: 0.66288775491547292, 13: 1.5193332239786814, 14: 0.25709320979962919, 15: 1.6497213849433678, 16: 0.59128447349292046, 17: 1.0905173938706694, 18: 0.90291909897243094, 19: 0.91813591292643271, 20: 15.217285287998195,
                                  21: 1.926440761334907, 22: 0.57080214100189564, 23: 1.6118707134757864, 24: 1.9047622621552052, 25: 2.8424458210460255, 26: 1.8183809153964505, 27: 3.3493283711763784, 28: 22.629880069550744, 29: 1.0, 30: 0.93881397655514409,
                                   31: 5.030146351518483, 32: 5.1303906024159716, 33: 1.9712181862289606, 34: 1.3547999182889652, 35: 2.1037979503353244, 36: 2.9668540602129876, 37: 2.7373099781876906}
    weights = [weights_sunrgbd_37_train[i] for i in range(38)]

    weight[0] = 0
    max_iters = 92*epoches
    criterion = CrossEntropyLoss2d(weight.cuda())

    for param in model.parameters():
        param.requires_grad = False
    for name, param in model.named_parameters():
        if 'projecteur' in name or 'hd' in name:
            param.requires_grad = True
    learnable_parameters = filter(lambda p: p.requires_grad, model.parameters())

    optimizer = torch.optim.SGD(learnable_parameters, lr=lr, momentum=momentum,
                                weight_decay=weight_decay)
    # optimizer = torch.optim.SGD(model.parameters(), lr=lr, momentum=momentum,
    #                             weight_decay=weight_decay)
    # myplots={}
    # myplots['loss_epoch'] = LinePlotter('ResNet FCN')
    # myplots['loss_mean_batch'] = LinePlotter('ResNet FCN')
    # myplots['global_accuracy_mean_batch'] = LinePlotter('ResNet FCN')

    myplots = LinePlotter('concatener')
    for epoch in range(epoches):

        if (epoch+1) % 20 == 0:
            lr /= 10
            optimizer = torch.optim.SGD(learnable_parameters, lr=lr, momentum=momentum,
                                    weight_decay=weight_decay)
        if epoch<start_epoch:
            continue
        train(model,trainloader,criterion,optimizer,epoch,myplots)
        validate(model,valloader,criterion,epoch,myplots)
        torch.save(model.state_dict(), "./pth/fcn-deconv-%d.pth" % (4*(epoch//4+1)))
    torch.save(model.state_dict(), "./pth/fcn-deconv.pth")

def train(model,dataloader,criterion,optimizer,epoch,plots):
    model.train()

    running_loss = 0.0
    running_oa = 0.0
    running_moa = 0.0

    avg_loss = AverageMeter()
    avg_accuracy = AverageMeter()
    avg_moa = AverageMeter()

    tq_bar = tqdm(enumerate(dataloader),total=len(dataloader),ncols=80,desc='Training')
    for batch_id, (images, labels) in tq_bar:

        if torch.cuda.is_available():
            images = Variable(images[0].cuda())
            labels = Variable(labels[0].cuda().squeeze_())
        else:
            print('Cuda not available')
            images = Variable(images[0])
            labels = Variable(labels[0].squeeze_())
        net_batch_size = len(images)

        optimizer.zero_grad()

        outputs = model(images)

        accuracy = accuracy_dense(outputs.data, labels.data)
        moa,_ = mAP_dense(outputs.data, labels.data)

        loss = criterion(outputs, labels)
        avg_loss.update(loss.data[0])
        avg_accuracy.update(accuracy, net_batch_size)
        avg_moa.update(moa, net_batch_size)

        ## LOSS COMPUTATION
        loss.backward()
        optimizer.step()

        plots.plot("Total loss (running)", "train", epoch*len(dataloader)+batch_id+1, avg_loss.val)
        plots.plot("OA (running)", "train", epoch*len(dataloader)+batch_id+1, avg_accuracy.val)
        plots.plot("mOA (running)", "train", epoch*len(dataloader)+batch_id+1, avg_moa.val)

    plots.plot("Total loss (final mean of epoch)", "train", epoch+1, avg_loss.val)
    plots.plot("OA (final mean of epoch)", "train", epoch+1, avg_accuracy.val)
    plots.plot("mOA (final mean of epoch)", "train", epoch+1, avg_moa.val)

def validate(model,dataloader,criterion,epoch,plots):
    """Perform validation on the validation set"""
    # switch to evaluate mode
    model.eval()
    running_loss = 0.0
    running_oa = 0.0
    running_moa = 0.0

    avg_loss = AverageMeter()
    avg_accuracy = AverageMeter()
    avg_moa = AverageMeter()

    tq_bar = tqdm(enumerate(dataloader),total=len(dataloader),ncols=80,desc='Testing')
    for batch_id, (images, labels) in tq_bar:

        if torch.cuda.is_available():
            images = Variable(images[0].cuda())
            labels = Variable(labels[0].cuda().squeeze_())
        else:
            print('Cuda not available')
            images = Variable(images[0])
            labels = Variable(labels[0].squeeze_())
        net_batch_size = len(images)

        outputs = model(images)

        accuracy = accuracy_dense(outputs.data, labels.data)
        moa,_ = mAP_dense(outputs.data, labels.data)

        loss = criterion(outputs, labels)
        avg_loss.update(loss.data[0])
        avg_accuracy.update(accuracy, net_batch_size)
        avg_moa.update(moa, net_batch_size)

        plots.plot("Total loss (running)", "val", epoch*len(dataloader)+batch_id+1, avg_loss.val)
        plots.plot("OA (running)", "val", epoch*len(dataloader)+batch_id+1, avg_accuracy.val)
        plots.plot("mOA (running)", "val", epoch*len(dataloader)+batch_id+1, avg_moa.val)

    plots.plot("Total loss (final mean of epoch)", "val", epoch+1, avg_loss.val)
    plots.plot("OA (final mean of epoch)", "val", epoch+1, avg_accuracy.val)
    plots.plot("mOA (final mean of epoch)", "val", epoch+1, avg_moa.val)

class AverageMeter(object):
    """Computes and stores the average and current value"""
    def __init__(self):
        self.reset()
    def reset(self):
        self.val = 0
        self.avg = 0
        self.sum = 0
        self.count = 0
    def update(self, val, n=1.):
        if float(val)>=0. and float(n)>=1.:
            self.val = val
            self.sum += val * n
            self.count += n
            self.avg = self.sum / self.count
        else:
            print('Error in values and number',val,n)

def accuracy_dense(output, target):
    output_np = output.cpu().numpy()
    output_np = np.argmax(output_np, axis=1)
    target_np = target.cpu().numpy().astype(int)
    output_np = output_np[target_np>0]
    target_np = target_np[target_np>0]
    good_classif = (output_np==target_np)
    prediction_good_count=good_classif.sum()
    gt_good_count=target_np.ravel().shape[0]
    return 100.*prediction_good_count/gt_good_count

def mAP_dense(output, target, label_nbr=38):
    histogram_pred={}
    histogram_gt={}
    classes_map={}
    output_np = output.cpu().numpy()
    output_np = np.argmax(output_np, axis=1)
    target_np = target.cpu().numpy().astype(int)
    output_np = output_np[target_np>0]
    target_np = target_np[target_np>0]
    good_classif = (output_np==target_np)
    good_outputs = output_np[good_classif]
    # histogram of good predictions
    for classe,num_occur in enumerate(np.bincount(good_outputs)):
        histogram_pred[classe] = num_occur
    # histrogram of gt labels
    for classe,num_occur in enumerate(np.bincount(target_np)):
        histogram_gt[classe] = num_occur
    global_mAP=0
    for i in range(1,label_nbr):
        if histogram_gt.get(i,0)!=0:
            acc_i=100.*histogram_pred.get(i,0)/histogram_gt[i]
            global_mAP+=acc_i
            classes_map[i]=acc_i
        else:
            classes_map[i]=0
    global_mAP/=label_nbr
    return global_mAP, classes_map

if __name__ == '__main__':
    main()


import base64
from itertools import cycle
import sys
import hashlib

#encrypt/decrypt text files using xor encryption

def encrypt(data, key):
    xored = ''.join(chr(ord(a) ^ ord(b)) for (a,b) in zip(data, cycle(key)))
    xored = base64.encodebytes(bytes(xored, 'utf-8')).strip()
    return xored.decode('utf-8')

def decrypt(data, key):
    xored = base64.decodebytes(bytes(data, 'utf-8')).decode('utf-8')
    xored = ''.join(chr(ord(a) ^ ord(b)) for (a,b) in zip(xored, cycle(key)))
    return xored

if len(sys.argv) != 5:
    print('python encrypt.py encrypt/decrypt key input_file output_file/print')
    sys.exit()

#use sha256 hash of key
key_hash = hashlib.sha256(sys.argv[2].encode()).hexdigest()

if sys.argv[1] == 'encrypt':
    read = open(sys.argv[3], 'r').read()
    encrypted = encrypt(read, key_hash)
    if sys.argv[4] == 'print':
        print(encrypted)
    else:
        open(sys.argv[4], 'w').write(encrypted)

if sys.argv[1] == 'decrypt':
    read = open(sys.argv[3], 'r').read()
    decrypted = decrypt(read, key_hash)
    if sys.argv[4] == 'print':
        print(decrypted)
    else:
        open(sys.argv[4], 'w').write(decrypted)


#!/usr/bin/python
#Rachelle Szymkiewicz

#Libraries
import os
import datetime

#Variables
Root_Folder = "/media/sdc/Bowtie2/"
referenceFasta = "/media/rachelle/Seagate\ Expansion\ Drive/PES_Files/P749_PES_Sample/PES_P749_PacBio_edited.fasta"
SamtoolsFolderPath = "/media/sdc/mpileup/"
sortedBam = ""
timestamp = (datetime.datetime.now())

print ("\n ##########\n samtools mpileup Bowtie2 \n ##########\n")
print (timestamp)

#Make Parent Directory
os.system("mkdir /media/sdc/mpileup/")

for root, dirs, files in os.walk(Root_Folder):
	print ("..........")
	print ("Current Directory : " + root) #root directory we are currently in
	Forward_paired = ""
	Reverse_paired = ""
	for file in files: #loop through all files within this current folder
		#Get Sample Name
		SampleName = file.split('_')
		Sample = SampleName[0]

		#Find the sorted.bam file from the Bowtie2 mapping
		if file.endswith('.sort.bam'):
			sortedBam = file
		else:
			continue
	if sortedBam != "":
		#Make separate directory for the sample
		#print ("mkdir " + SamtoolsFolderPath + "Sample_" + str(Sample))
		os.system("mkdir " + SamtoolsFolderPath + "Sample_" + str(Sample))
		#Change directory to the sample
		os.chdir(SamtoolsFolderPath + "Sample_" + str(Sample))
		print("..........")
		#Sample Index
		print("SAMPLE INDEX")
		IndexBuild = ("samtools faidx " + referenceFasta)
		#Print samtools index code to screen as progress indication for user
		print(IndexBuild)
		os.system(IndexBuild)
		print("..........")
		print("..........")
		#mpileup
		print("MPILEUP")
		#Create mpileup VCF file per sample
		mpileup = ("/home/rachelle/samtools/samtools mpileup -vf " + referenceFasta + " " + os.path.join(root, sortedBam) + " | /home/rachelle/bcftools/bcftools call -vmO z -o " + SamtoolsFolderPath + "Sample_" + str(Sample) + "/" + str(Sample) + "_B2.vcf.gz")
		#Print samtools mpileup code to screen as progress indication for user
		print(mpileup)
		os.system(mpileup)
		print("..........")
timestamp = (datetime.datetime.now())
print ('Script is complete')
print (timestamp)


# -*- mode: python ; coding: utf-8 -*-

import ast
import os
import importlib
from os import environ as env

import warnings
warnings.filterwarnings("ignore", category=ResourceWarning,
                        module='pyinstaller', message="unclosed file")

block_cipher = None

# Pyinstaller removes this, but I want it. Put it back
import sys
sys.path.insert(0, '')

app_names = ast.literal_eval(env['TERRA_APPS'])

# For each app (python import string, like "app.foo.bar"), determine the
# filename of import (prefer __main__ over __init__)
def get_app_paths(app_names):
  for name in app_names:
    # Determine the app file full path
    app = importlib.util.find_spec(name+'.__main__')
    if app is None:
      app = importlib.util.find_spec(name)
    yield app.origin

app_paths = get_app_paths(app_names)

hooks_dirs = [os.path.join(env['TERRA_CWD'], 'freeze')]

try:
  hooks_dirs.append(env['TERRA_PYINSTALLER_HOOKS_DIR'])
except KeyError:
  pass

apps_a = []
for app_path, app_name in zip(app_paths, app_names):
  extra_hidden_imports = env.get(
      f'TERRA_PYINSTALLER_{app_name.upper()}_HIDDEN_IMPORTS', [])
  if extra_hidden_imports:
    extra_hidden_imports = ast.literal_eval(extra_hidden_imports)

  apps_a.append(Analysis([app_path],
                         pathex=[env['TERRA_CWD']],
                         binaries=[],
                         datas=[],
                         hiddenimports=extra_hidden_imports,
                         # setuptools 45 - 49.0.0 needed pkg_resources.py2_warn
                         hookspath=hooks_dirs,
                         runtime_hooks=[],
                         excludes=[],
                         win_no_prefer_redirects=False,
                         win_private_assemblies=False,
                         cipher=block_cipher,
                         noarchive=False))

merge_args = []
for a, name in zip(apps_a, app_names):
  merge_args.append((a, name, name))
# This is broken until pyinstaller 4.0 is released
# https://github.com/pyinstaller/pyinstaller/pull/4303
# MERGE(*merge_args)

for a, name in zip(apps_a, app_names):
  pyz = PYZ(a.pure, a.zipped_data,
            cipher=block_cipher)
  exe = EXE(pyz,
            a.scripts,
            a.binaries,
            a.zipfiles,
            a.datas,
            a.dependencies,
            name=name,
            debug=False,
            bootloader_ignore_signals=False,
            strip=False,
            upx=False,
            console=True)