# diketahui tabel mahasiswa dan mata kuliah
# mhs	makul	mk1	mk2	mk3	mk4	mk5	mk6	mk7	mk8	mk9
# --------------------------------------------------
# mhs1		|	1		1		1		1
# mhs2		|		1		1	1			1
# mhs3		|	1	1		1			1
# mhs4		|			1	1
# mhs5		|					1	1		1
# mhs6		|		1	1
# mhs7		|	1					1
# mhs8		|		1							1
# mhs9		|				1					1
# mhs10		|					1			1	1
# mhs11		|		1		1
# mhs12		|	1	1			1
# mhs13		|		1		1			1
# mhs14		|			1			1		1
# mhs15		|	1				1				1
# mhs16		|	1	1		1
# ==================================================
#				4	3	2	1	0	1	5	4	2
import numpy as np

n_makul = 118

# ubah ke dalam matriks adjacency (ketetanggaan)

#baca insidensi
def BuatInsidensi_Mhs_Makul(NamaFile, n_makul):
	data1dimensi = open(NamaFile).read().splitlines()

	data1dimensi = [ int(i) for i in data1dimensi ]
	pjg_data = len(data1dimensi)

	n_mhs = pjg_data/n_makul		# banyaknya mahasiswa dan dosen

	inc = np.array(data1dimensi).reshape((n_mhs, n_makul))
	return inc

inc = BuatInsidensi_Mhs_Makul("kode incidency.txt", n_makul)

#ubah insidensi ke adjacency matriks makul

#buat adjacency matrix
def BuatAdj(incidency_matriks, n_makul):
	adj = np.zeros((n_makul,n_makul), dtype=np.uint8)
	for baris in incidency_matriks:
		titik_hidup = [ i for i in range(n_makul) if baris[i] == 1 ]
		banyaknya_hidup = len(titik_hidup)
		if banyaknya_hidup > 1:
			for i in range(banyaknya_hidup - 1):
				for j in range(i+1, banyaknya_hidup):
					adj[titik_hidup[i], titik_hidup[j]] = 1
					adj[titik_hidup[j], titik_hidup[i]] = 1
	return adj

adj_tanpa_sks = BuatAdj(inc, n_makul)

#jangan sampai 2sks dan 3sks di satu slot yang sama
#buat sisi antara makul 2sks dengan yang 3sks
def sks2Adj(NamaFile_sks, n_makul, adj):
	sks = open(NamaFile_sks).read().splitlines()
	sks = np.array([ int(i) for i in sks ])

	#~ pilihan_sks = [2,3]
	dua_sks = np.array([i for i in range(n_makul) if sks[i] == 2])
	tiga_sks = np.array([i for i in range(n_makul) if sks[i] == 3])
	n_dua_sks = len(dua_sks)
	n_tiga_sks = len(tiga_sks)

	for i in range(n_dua_sks):
		for j in range(n_tiga_sks):
			adj[dua_sks[i],tiga_sks[j]] = 1
			adj[tiga_sks[j],dua_sks[i]] = 1
	return adj

adj = sks2Adj("sks.txt", 118, adj_tanpa_sks)

#urutkan berdasarkan derajat tiap titiknya

def UrutAdj(adj, n_makul):
	# tambahkan derajat di baris dan kolom baru
	# buat matriks baru yang ukurannya menjadi lebih besar 1 baris dan 1 kolom
	adj_der = np.zeros((n_makul+1, n_makul+1), dtype=np.uint8)
	adj_der[:n_makul,:n_makul] = adj
	#~ print adj_der

	jml = adj.sum(axis=1)
	#~ print jml

	adj_der[:n_makul,n_makul] = jml
	adj_der[n_makul,:n_makul] = jml
	#~ print adj_der

	adj_tanda = np.zeros((n_makul+2, n_makul+2), dtype=np.uint8)
	adj_tanda[:n_makul+1,:n_makul+1] = adj_der
	#~ print adj_tanda
	adj_tanda[:n_makul,n_makul+1] = np.arange(n_makul)
	adj_tanda[n_makul+1,:n_makul] = np.arange(n_makul)
	#~ print adj_tanda

	print ('mengurutkan berdasarkan kolom ke:', n_makul)
	urutan = adj_tanda[:n_makul,n_makul].argsort()[::-1]
	#~ print 'urutan:\n',urutan

	#urutkan baris2 berdasarkan kolom ke n_makul
	adj_urut = adj_tanda
	adj_urut[:n_makul,:n_makul+2] = adj_tanda[urutan]
	#~ print adj_urut

	#urutkan kolom2 berdasarkan baris ke n_makul
	adj_urut[:n_makul+2,:n_makul] = adj_tanda[:,urutan]
	#~ print adj_urut

	adj_baru = adj_urut[:n_makul,:n_makul]
	#~ print adj_baru

	return adj_baru, urutan

adj_baru, urutan = UrutAdj(adj, n_makul)

#########################################
#### ini tambahannya, bit warna graf muks
####-------------------------------------

def listkeint(list):
	b = ''
	for i in list:
		b += str(i)
	return int(b,2)

# Kita namakan dengan variabel "aint", yaitu matriks "adjacency"
## yang tiap barisnya diubah dari bit menjadi integer.
aint = [ listkeint(baris) for baris in adj_baru]

# Terus kita tampilkan deh..
#~ print "aint =", aint

pjgA = len(aint)
c = [ 0 for i in range(pjgA)]
#~ print "c =", c

topeng = 1 << (pjgA - 1)

# Setelah menginisialisasi "topeng" nya,
# di bagian ini lah proses pewarnaan untuk seluruh grafnya
for i in range(pjgA): # untuk kolom di A
	for j in range(pjgA): # ini untuk baris di C
		cek = c[j] & topeng # bitnya hidup ndak ya?
		if cek == 0:
			c[j] = c[j] | aint[i]
			topeng = topeng >> 1
			break

# Akhirnya bisa kita tampilkan di layar dengan script ini.
#~ print "Setelah kutunggu cukup lama, akhirnya ketemu juga.."
#~ print "c =", c

# Itu kalau kita artikan di bahasa manusia seperti ini
print ("\nItu artinya seperti ini:")

h = [ 1<<pjgA | i for i in c if i != 0]
h_list = [ list(bin(i))[3:] for i in h ]
hku = [ map(int,baris) for baris in h_list ]
h_array = np.array(hku)
print (h_array)

print (urutan)
warna = []
for baris in h_array:
	ini = []
	for i in xrange(n_makul):
		if baris[i] == 0:
			ini.append(urutan[i])
	warna.append(ini)

keluaran = open('keluaran.txt','w')
print (warna)
print ('Okay, jadi bisa menggunakan',len(warna),'warna saja')
keluaran.write('Okay, jadi bisa menggunakan %s warna saja\n' %(len(warna)))

i = 0
for baris in warna:
	print ('warna',i,'bisa untuk titik:',baris)
	keluaran.write('warna %s bisa untuk titik: %s\n' %(i, baris))
	i = i + 1

keluaran.close()
#~ npwarna = np.asarray(warna)
#~ print npwarna
	#~ print [urut[i] for i in xrange(n_makul) if baris[i] == 0 ]


# Dado um inteiro positivo n e n triplas compostas por um símbolo
# de operaçao (+, -, * ou /) e dois numeros reais, calcule #

import random

# Gera uma tripla aleatoria
def tripla():
    i = random.randrange(0,4)
    tripla = []
    if i == 0:
        tripla = tripla + ["+"]
    elif i == 1:
        tripla = tripla + ["-"]
    elif i == 2:
        tripla = tripla + ["*"]
    elif i == 3:
        tripla = tripla + ["/"]

    i = int(100 * random.random())
    tripla = tripla + [i]
    i = int(100 * random.random())
    tripla = tripla + [i]
    return tripla

# Gera n triplas
def tripas(n):
    triplas = []
    for i in range(n):
        triplas.append(tripla())
    return triplas

def main(n):
    triplas =tripas(n)
    for tripla in triplas:
        if tripla[0] == "+":
            res = tripla[1] + tripla[2]
        if tripla[0] == "-":
            res = tripla[1] - tripla[2]
        if tripla[0] == "*":
            res = tripla[1] * tripla[2]
        if tripla[0] == "/":
            res = tripla[1] / tripla[2]
        print(tripla, res)

n = int(input('n = '))

main(n)


"""
Author : Robin Singh
Implementation Of  finding Predecessor And Successor on BST using simple iterative method

"""
class SUCC_PRED:

    def __init__(self, data):
        self.dat = data
        self.left = None
        self.right = None

def pandd(root, p, s, dat):
    if root == None:
        return

    while root != None:

        if root.dat == dat:

            if root.right:
                s[0] = root.right
                while s[0].left:
                    s[0] = s[0].left

            if root.left:
                p[0] = root.left
                while p[0].right:
                    p[0] = p[0].right

            return

        elif root.dat < dat:
            p[0] = root
            root = root.right

        else:
            s[0] = root
            root = root.left

def insert(node, dat):
    if node == None:
        return SUCC_PRED(dat)
    if dat < node.dat:
        node.left = insert(node.left, dat)
    else:
        node.right = insert(node.right, dat)
    return node

if __name__ == '__main__':

    print("Insert value of Root Node")
    val = int(input("Entre Value"))
    a = None
    a = insert(a, val)

    while(1):
        p, s = [None], [None]
        print(f"\n1.Insert\t2.Finding Predecessor And Successor\t3.Exit ")
        ch = int(input("Entre Choice"))

        if ch == 3:
            break
        elif ch == 1:
            ele = int(input("Entre Your Value to be inserted"))
            insert(a, ele)

        elif ch ==2:
            key = int(input("Entre Key"))
            pandd(a, p, s, key)
            if p[0] != None:
                print(f"Predecessor :{ p[0].dat}")
            else:
                print("No Value in BST is lesser than key")

            if s[0] != None:
                print(f"Successor :  {s[0].dat}")
            else:
                print("No value in BST is greater than key")
        else:
            print("invalid option")

Data Structures/Trees/PREDECESSOR AND SUCCESSOR.py


from fastapi import APIRouter, HTTPException
import pandas as pd
import os

router = APIRouter()

rental_data = os.path.join(os.path.dirname(__file__), "..", "..", "data", "rental_data_2020.csv")

@router.get('/rental/{city_id}')
async def get_rental_data(city_id: int):
    """
    Returns
            id,
            city,
            county_name,
            area_name,
            state,
            city_state,
            studio - rental rate in dollars for studio apartment,
            1br - rental rate in dollars for one bedroom apartment,
            2br - rental rate in dollars for two bedroom apartment,
            3br - rental rate in dollars for three bedroom apartment,
            4br - rental rate in dollars for four bedroom apartment,
            rental_pct_chg - rental change in percentage compared to last year
            rental_dollar_chg - rental change in dollars compared to last year

    of a city.

    ### Query Parameters:
    - `city_id`: [city_id], unique numeric mapping (ex: 0 returns Anchorage, AK)
    ### Response
    Dictionary object
    """

    rt_dict = {}
    rt_data_dict = {}

    df = pd.read_csv(rental_data, encoding='utf-8')
    dataframe = df[df['city_id']==city_id]
    rt_data = dataframe.to_numpy()

    rt_data_dict['id'] = rt_data[0][0]

    rt_data_dict['city'] = rt_data[0][1]
    rt_data_dict['county_name'] = rt_data[0][10]
    rt_data_dict['area_name'] = rt_data[0][9]
    rt_data_dict['state'] = rt_data[0][2]
    rt_data_dict['city_state'] = rt_data[0][3]

    rt_data_dict['studio'] = rt_data[0][4]
    rt_data_dict['1br'] = rt_data[0][5]
    rt_data_dict['2br'] = rt_data[0][6]
    rt_data_dict['3br'] = rt_data[0][7]
    rt_data_dict['4br'] = rt_data[0][8]

    rt_data_dict['rental_pct_chg'] = rt_data[0][11]
    rt_data_dict['rental_dollar_chg'] = rt_data[0][12]

    rt_dict['data'] = rt_data_dict
    return rt_dict


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

import os
import csv
import itertools

import nltk
import numpy as np

import npdl

def load_data(corpus_path=os.path.join(os.path.dirname(__file__), 'data/lm/reddit-comments-2015-08.csv'),
              vocabulary_size=8000):
    sentence_start_token = "SENTENCE_START"
    sentence_end_token = "SENTENCE_END"
    unknown_token = 'UNKNOWN_TOKEN'

    # Read the data and append SENTENCE_START and SENTENCE_END tokens
    with open(corpus_path, encoding='utf-8') as f:
        reader = csv.reader(f, skipinitialspace=True)
        # Split full comments into sentences
        sentences = [nltk.sent_tokenize(x[0]) for x in reader]
        sentences = itertools.chain(*sentences)
        # Append SENTENCE_START and SENTENCE_END
        sentences = ["%s %s %s" % (sentence_start_token, x, sentence_end_token) for x in sentences]

    # Tokenize the sentences into words
    tokenized_sentences = [nltk.word_tokenize(sentence) for sentence in sentences]

    # Count the word frequencies
    word_freq = nltk.FreqDist(itertools.chain(*tokenized_sentences))
    print("Found %d unique tokens in corpus '%s'." % (word_freq.B(), corpus_path))

    # Get the most common words and build index_to_word and word_to_index vectors
    vocab = word_freq.most_common(vocabulary_size - 1)
    print("The least frequent word in our vocabulary is '%s' and appeared %d times." %
          (vocab[-1][0], vocab[-1][1]))
    index_to_word = [x[0] for x in vocab]
    index_to_word.append(unknown_token)
    word_to_index = dict([(w, i) for i, w in enumerate(index_to_word)])

    # Replace all words not in our vocabulary with the unknown token
    tokenized_sentences = [[word if word in word_to_index else unknown_token for word in sentence]
                           for sentence in tokenized_sentences]
    # Create the training data.
    train_x = np.asarray([[word_to_index[word] for word in sentence[:-1]] for sentence in tokenized_sentences])
    train_y = np.asarray([[word_to_index[word] for word in sentence[1:]] for sentence in tokenized_sentences])

    return index_to_word, word_to_index, train_x, train_y

def main(max_iter, corpus_path=os.path.join(os.path.dirname(__file__), 'data/lm/tiny_shakespeare.txt')):
    raw_text = open(corpus_path, 'r').read()
    chars = list(set(raw_text))
    data_size, vocab_size = len(raw_text), len(chars)
    print("data has %s charactres, % unique." % (data_size, vocab_size))
    char_to_index = {ch: i for i, ch in enumerate(chars)}
    index_to_char = {i: ch for i, ch in enumerate(chars)}

    time_steps, batch_size = 30, 40

    length = batch_size * 20
    text_pointers = np.random.randint(data_size - time_steps - 1, size=length)
    batch_in = np.zeros([length, time_steps, vocab_size])
    batch_out = np.zeros([length, vocab_size], dtype=np.uint8)
    for i in range(length):
        b_ = [char_to_index[c] for c in raw_text[text_pointers[i]:text_pointers[i] + time_steps + 1]]
        batch_in[i, range(time_steps), b_[:-1]] = 1
        batch_out[i, b_[-1]] = 1

    print("Building model ...")
    net = npdl.Model()
    # net.add(model.layers.SimpleRNN(n_out=500, return_sequence=True))
    net.add(npdl.layers.SimpleRNN(n_out=500, n_in=vocab_size))
    net.add(npdl.layers.Softmax(n_out=vocab_size))
    net.compile(loss=npdl.objectives.SCCE(), optimizer=npdl.optimizers.SGD(lr=0.00001, clip=5))

    print("Train model ...")
    net.fit(batch_in, batch_out, max_iter=max_iter, batch_size=batch_size)

if __name__ == '__main__':
    main(100)


import requests
import json
from sklearn.neighbors import DistanceMetric, BallTree
import numpy as np

def get_available_freebikes():
    # Available nextbikes that are not in stations
    urls = "https://gbfs.nextbike.net/maps/gbfs/v1/nextbike_de/en/free_bike_status.json"

    response = requests.get(url=urls)
    bikes = json.loads(response.text.encode('utf8')).get('data').get('bikes')
    return bikes

def get_available_stationbikes():
    """Return two dictionaries:
    1. bikes: dictionary of station id (keys) with number of available bikes as value
    2. stations_coordinates: dictionary of coordinates (keys) with station id as value
    """
    # Available nextbikes that are not in stations
    urls = "https://gbfs.nextbike.net/maps/gbfs/v1/nextbike_de/en/station_status.json"
    response = requests.get(url=urls)
    stations = json.loads(response.text.encode('utf8')).get('data').get('stations')
    bikes = {station.get('station_id'): station.get('num_bikes_available') for station in stations
             if station.get('num_bikes_available') > 0}

    # Get station positions
    urls = "https://gbfs.nextbike.net/maps/gbfs/v1/nextbike_de/en/station_information.json"
    response = requests.get(url=urls)
    stations_info = json.loads(response.text.encode('utf8')).get('data').get('stations')
    stations_coordinates = {(station.get('lon'), station.get('lat')): station.get('station_id') for station
                            in stations_info}

    return bikes, stations_coordinates

def find_nearest_bikestations(lon: float, lat: float):
    """Return a dictionary with coordinate-tuples (keys) and number of available bikes per station within 0.5 of
    (lon, lat) coordinate (value) and list of distances
    """
    bikes, stations = get_available_stationbikes()
    stations_coordinates = list(stations.keys())
    dist = DistanceMetric.get_metric('haversine')
    tree = BallTree(np.array(stations_coordinates), metric=dist)

    # nearest_stations_ix = tree.query_ball_point((lon, lat), 0.5)
    dist, nearest_stations_ix = tree.query(np.array([(lon, lat)]), 5)
    nearest_stations = [stations_coordinates[i] for i in nearest_stations_ix[0]]
    nearest_bikes = {station: bikes.get(stations.get(station), 0) for station in nearest_stations}

    return nearest_bikes, dist

def find_nearest_freebikes(lon: float, lat: float):
    """Return list of coordinates to the nearest free bikes (not locked to a station), and distance"""
    freebikes = get_available_freebikes()
    bikes = [(bike.get('lon'), bike.get('lat')) for bike in freebikes if
             (bike.get('is_reserved') == 0 and bike.get('is_disabled') == 0)]
    dist = DistanceMetric.get_metric('haversine')
    tree = BallTree(np.array(bikes), metric=dist)
    # nearest_bikes_ix = tree.query_ball_point((lon, lat), 0.5)
    dist, nearest_bikes_ix = tree.query(np.array([(lon, lat)]), 5)

    return [bikes[i] for i in nearest_bikes_ix[0]], dist

if __name__ == "__main__":
    print(find_nearest_freebikes(13.384524, 52.453312))
    print(find_nearest_bikestations(13.384524, 52.453312))


#-*- coding:UTF-8 -*-
__author__ = 'Frank Yang <frankyang418@gmail.com>'
__date__ = "13-6-9 上午10:16"
__doc__ = u""

class ObjectDict(dict):
    u"""
    dict使用·属性·访问
    """

    def __getattr__(self, name):
        try:
            return self[name]
        except KeyError:
            return []

    def __setattr__(self, name, value):
        self[name] = value


from masserstein import Spectrum, peptides, estimate_proportions
from copy import deepcopy
import scipy as sp
import numpy as np
from matplotlib import pyplot as plt
from copy import deepcopy

myoglobin_fasta ="""GLSDGEWQLVLNVWGKVEADIPGHGQEVLIRLFKGHPETLEKFDKFKHLKSEDEMKASE
DLKKHGATVLTALGGILKKKGHHEAEIKPLAQSHATKHKIPVKYLEFISECIIQVLQSKH
PGDFGADAQGAMNKALELFRKDMASNYKELGFQG"""
haemoglobinB_fasta = """VHLTPEEKSAVTALWGKVNVDEVGGEALGRLLVVYPWTQRFFESFGDLSTPDAVMGNPK
VKAHGKKVLGAFSDGLAHLDNLKGTFATLSELHCDKLHVDPENFRLLGNVLVCVLAHHFG
KEFTPPVQAAYQKVVAGVANALAHKYH"""
haemoglobinA_fasta = """VLSPADKTNVKAAWGKVGAHAGEYGAEALERMFLSFPTTKTYFPHFDLSHGSAQVKGHG
KKVADALTNAVAHVDDMPNALSALSDLHAHKLRVDPVNFKLLSHCLLVTLAAHLPAEFTP
AVHASLDKFLASVSTVLTSKYR"""

# Obtaining chemical formulas
haemoglobinA_formula = peptides.get_protein_formula(haemoglobinA_fasta)
haemoglobinB_formula = peptides.get_protein_formula(haemoglobinB_fasta)
myoglobin_formula = peptides.get_protein_formula(myoglobin_fasta)

# Generatin individual spectra
hA19 = Spectrum(haemoglobinA_formula, charge=19, adduct='H', label='hA 19+')
hA20 = Spectrum(haemoglobinA_formula, charge=20, adduct='H', label='hA 20+')
hA21 = Spectrum(haemoglobinA_formula, charge=21, adduct='H', label='hA 21+')
hB20 = Spectrum(haemoglobinB_formula, charge=20, adduct='H', label='hB 20+')
hB21 = Spectrum(haemoglobinB_formula, charge=21, adduct='H', label='hB 21+')
hB22 = Spectrum(haemoglobinB_formula, charge=22, adduct='H', label='hB 22+')
m21  = Spectrum(myoglobin_formula, charge=21, adduct='H', label='myo 21+')
m22  = Spectrum(myoglobin_formula, charge=22, adduct='H', label='myo 22+')
m23  = Spectrum(myoglobin_formula, charge=23, adduct='H', label='myo 23+')
m24  = Spectrum(myoglobin_formula, charge=24, adduct='H', label='myo 24+')
spectra = [hA19, hA20, hA21, hB20, hB21, hB22, m21, m22, m23, m24]
k = len(spectra)

# TIC normalization:
for s in spectra:
    s.normalize()

# Visualization:
#Spectrum.plot_all(spectra)

# Wasserstein distance matrix:
wM = np.zeros((k,k))
for i in range(k):
    for j in range(k):
        wM[i,j] = spectra[i].WSDistance(spectra[j])

# Setting example proportions
proportions = [1, 2, 1.2, 0.5, 0.9, 0.6, 0.2, 0.3, 0.4, 0.]
proportions = [p/sum(proportions) for p in proportions]

# Generating a convolved spectrum
convolved = Spectrum('', empty=True, label='Convolved')
for s, p in zip(spectra, proportions):
    convolved += s*p

##convolved.plot()

##est1 = estimate_proportions(convolved, spectra, MTD=0.001)
##print('True:', 'Estimate w/o noise:', sep='\t')
##for e, p in zip(est1['proportions'], proportions):
##    print(p, e, sep='\t')

# Distorting intensity - simulating finite number of molecules
sampled = Spectrum.sample_multinomial(convolved, 1e05, 1, 0.01)
sampled.add_chemical_noise(1000, 0.1)
sampled.normalize()
plt.subplot(211)
convolved.plot(show=False)
plt.subplot(212)
sampled.plot(show=False)
plt.show()

est2 = estimate_proportions(sampled, spectra, MTD=0.01)
print('True:', 'Estimate after sampling and adding noise peaks:', sep='\t')
for e, p in zip(est2['proportions'], proportions):
    print(p, e, sep='\t')

# Simulating finite resolution
lowres  = deepcopy(sampled)
sampled.coarse_bin(3)
lowres.fuzzify_peaks(0.02, 0.001)
sampled.plot(show=False)
lowres.plot(show=False, profile=True)
plt.show()

# Simulating electronic noise
lowres.add_gaussian_noise(0.001)
lowres.normalize()
plt.subplot(211)
sampled.plot(show=False)
lowres.plot(show=False, profile=True)
plt.subplot(212)
lowres.plot(show=False, profile=True)
plt.show()

est3 = estimate_proportions(lowres, spectra, MTD=0.01)
print('True:', 'Estimate with full noise:', sep='\t')
for e, p in zip(est3['proportions'], proportions):
    print(p, e/sum(est3['proportions']), sep='\t')

# Saving the files

##with open('low_res_spectrum.txt', 'w') as h:
##    for x, y in lowres.confs:
##        h.write(str(x)+'\t'+str(y)+'\n')
##
##with open('molecules.txt', 'w') as h:
##    h.write('#True proportions:')
##    h.write(','.join(map(str, [round(p, 4) for p in proportions])) + '\n')
##    h.write(haemoglobinA_formula+'+H19 \n')
##    h.write(haemoglobinA_formula+'+H20 \n')
##    h.write(haemoglobinA_formula+'+H21 \n')
##    h.write(haemoglobinB_formula+'+H20 \n')
##    h.write(haemoglobinB_formula+'+H21 \n')
##    h.write(haemoglobinB_formula+'+H22 \n')
##    h.write(myoglobin_formula+'+H21 \n')
##    h.write(myoglobin_formula+'+H22 \n')
##    h.write(myoglobin_formula+'+H23 \n')
##    h.write(myoglobin_formula+'+H24 \n')
##print('Spectrum and molecules saved')


# Mike Roylance - roylance@uw.edu
import nltk

class TraversableTree:
	def __init__(self, tree, parent, rules):
		self.tree = tree
		self.parent = parent
		self.children = []

		self.items = []
		self.pos = str(self.tree.node)

		self.gender = ""
		self.number = ""
		self.isPronoun = False
		self.hasPronouns = False

		self.setItems()
		self.setIsPronoun(rules)
		self.determinePlurality(rules)
		self.determineGender(rules)

		self.processChildren(rules)

	def processChildren(self, rules):
		for child in self.tree:
			# only want to process other tree
			if isinstance(child, nltk.Tree):
				newChild = TraversableTree(child, self, rules)
				self.children.append(newChild)

	def setItems(self):
		for production in self.tree.productions():
			if production.is_lexical():
				self.items.append(production)

	def setIsPronoun(self, rules):
		if self.pos in rules.acceptablePronouns:
			self.isPronoun = True
			self.hasPronouns = True
			return

		for item in self.items:
			if str(item.lhs()) in rules.acceptablePronouns:
				self.hasPronouns = True
				return

		self.isPronoun = False

	def determinePlurality(self, rules):
		single = "sg"
		plural = "pl"

		if (self.pos in rules.pluralPartsOfSpeech):
			self.number = plural
			return

		for item in self.items:
			# check rhs first
			for terminalProduction in item.rhs():
				key = str(terminalProduction)

				if key in rules.pluralPartsOfSpeech:
					self.number = plural
					return
				elif key in rules.singlePartsOfSpeech:
					self.number = single
					return

			# then lhs
			key = str(item.lhs())
			if key in rules.pluralPartsOfSpeech:
				self.number = plural
				return
			elif key in rules.singlePartsOfSpeech:
				self.number = single
				return

		self.number = single

	def determineGender(self, rules):
		if self.pos in rules.malePartsOfSpeech:
			self.gender = "m"
			return

		for item in self.items:
			for terminalProduction in item.rhs():
				if str(terminalProduction) in rules.malePartsOfSpeech:
					self.gender = "m"
					return

		self.gender = "u"

	def printTree(self):
		return str(self.tree.pprint(margin=500))

	def printItems(self):
		strBuilder = ""
		for item in self.items:
			for terminalProduction in item.rhs():
				strBuilder = strBuilder + " " + str(terminalProduction)

		return strBuilder.strip()


from sqlalchemy.orm import sessionmaker
import pymysql
from sqlalchemy import create_engine, desc, Table, Float, Column, Integer, String, MetaData, ForeignKey
from sqlalchemy.ext.declarative import declarative_base
from datetime import datetime
from sqlalchemy import DateTime

Base = declarative_base()

# 创建一个test_table类
class test_table(Base):
    __tablename__ = 'test'
    user_id = Column(Integer(), primary_key = True)
    user_name = Column(String(25), nullable = False, unique = True)
    age = Column(Integer(), nullable = False)
    birthday = Column(Date(), nullable = False)
    gender = Column(String(25), nullable = False)
    edu_degree = Column(String(25), nullable = False)
    created_on = Column(DateTime(), default = datetime.now)
    updated_on = Column(DateTime(), default = datetime.now,onupdate=datetime.now)
    def __repr__(self):
        return "test_table(user_id = '{self.user_id}', " \
            "user_name = '{self.user_name}', " \
            "age = '{self.age}', " \
            "birthday = '{self.birthday}', " \
            "gender = '{self.gender}', " \
            "edu_degree = '{self.edu_degree}', " \
            "created_on = '{self.created_on}', " \
            "updated_on = '{self.updated_on})'".format(self = self)
#数据库连接
dburl = "mysql+pymysql://{}:{}@{}:{}/{}?charset=utf8mb4".format(
                                                                self.db_config['user_name'],
                                                                self.db_config['password'],
                                                                self.db_config['server'],
                                                                self.db_config['port'],
                                                                self.db_config['database'],
                                                                )
engine = create_engine(dburl, echo=True, encoding="utf-8")
    #进程函数创建
    #def init_session(self):
     #   SessionClass = sessionmaker(bind = self.engine)
      #  session = SessionClass()
       # return session
    #表函数创建
    #def create_table(self):
     #   try:
      #      Base.metadata.create_all(self.engine)
       # except Exception as e:
        #    sys.exit(e)
#插入操作
db = pymysql.connect("localhost","root", "root", "testdb")
try:
    sql = '''INSERT INTO test (user_id, user_name, age, birthday, gender, edu_degree, created_on, updated_on) VALUES (%s, %s, %s, %s, %s, %s, %s,%s)'''
    values = (
        (1, 'Li Ming', 22, '1998-1-2', 'man',
         'University', datetime.now(), datetime.now()),
        (2, 'Li Hua', 25, '1995-2-3', 'men',
         'Master', datetime.now(), datetime.now()),
        (3, 'Li Hong', 30, '1990-3-4', 'women',
         'High School', datetime.now(), datetime.now())
    )
    with db.cursor() as cursor:
        cursor.executemany(sql, values)
    db.commit()
except Exception as e:
    print(f'Insert error: {e}')
#查询操作
try:
    sql = '''SELECT user_id, user_name, age, birthday, gender, edu_degree FROM test'''
    with db.cursor() as cursor:
        cursor.execute(sql)
        results = cursor.fetchall()
        for result in results:
            print(result)
    db.commit()
except Exception as e:
    print(f'SELECT error: {e}')
finally:
    db.close()


from test_repository import TestRepository

class Teste():
    def __init__(self, title, content):
        self.title = title
        self.content = content
    def __repr__(self):
        return str(self.__dict__)

TestRepository.init()

item_1 = {
    'title': 'teste',
    'content':'another one'
}

t1 = Teste('caneca','café')
print(t1)

print(TestRepository.saveTests(t1))
print(TestRepository.getTests()[0])


import numpy as np
from shapely.geometry import Polygon
from scipy.spatial import ConvexHull
import matplotlib.pyplot as plt

from pylinear import h5table
from pylinear.utilities import indices,pool,convexhull

TTYPE='DDT'

def groupFLT(flt,sources,extconf,path,minarea=0.1):

    #print('loading the polygons for {}'.format(flt.dataset))

    # get the polygons for this FLT:
    with h5table.H5Table(flt.dataset,TTYPE,path=path) as h5:
        for detname,detimg in flt:
            h5det=h5[detname]
            detconf=extconf[detname]

            for beam,beamconf in detconf:
                h5beam=h5det[beam]

                ids=[]
                polys=[]

                for segid,src in sources:
                    # read the DDT
                    ddt=h5table.DDT(src.segid)
                    ddt.readH5(h5beam)
                    if len(ddt)!=0:

                        # collect the points accordingly
                        xyg=ddt.xyg.to_numpy
                        xyg=indices.unique(xyg)
                        x,y=indices.one2two(xyg,detimg.naxis)
                        del xyg

                        # get the vertices
                        xy=convexhull.vertices(x,y)

                        # reform to (x,y) pairs
                        xy=list(zip(*xy))

                        # make into a polygon from Shapely
                        poly=Polygon(xy)

                        # save the results
                        ids.append([segid])
                        polys.append(poly)
    # At this point, we've made shapely.Polygons out of a given DDT

    #print('grouping the polygons for {}'.format(flt.dataset))

    # group those sources with Shapely math
    data=list(zip(ids,polys))
    nnew=ndata=len(ids)
    if nnew==0:
        #print('[warn]No objects to group for {}'.format(flt.dataset))
        return []

    while nnew!=0:
        groups=[]

        while len(data)!=0:
            thisid,thispoly=data.pop(0)

            #ids=thisid
            for i,(testid,testpoly) in enumerate(data):
                inter=thispoly.intersection(testpoly)
                #inter=inter.area   # intersection area

                r1=inter.area/testpoly.area
                r2=inter.area/thispoly.area
                if r1 > minarea and r2 > minarea:
                #if area>minarea:
                    data.pop(i)   # it was grouped, so remove it from the list

                    #print(r1,r2)
                    #print(inter.area,testpoly.area,thispoly.area)
                    #fig,ax=plt.subplots(1,1)
                    #ax.plot(*thispoly.exterior.xy, color='#6699cc', alpha=0.7,
                    #        linewidth=3, solid_capstyle='round', zorder=2)
                    #ax.plot(*testpoly.exterior.xy, color='#cccccc', alpha=0.7,
                    #        linewidth=3, solid_capstyle='round', zorder=2)
                    #ax.set_title('Polygon')
                    #plt.show()

                    # update the this
                    thispoly=thispoly.union(testpoly)
                    thisid.extend(testid)
                    #print(i,area,thisid)

            groups.append((thisid,thispoly))
        data=groups
        nnew=ndata-len(data)
        ndata=len(data)

    # get just the IDs
    groups=list(zip(*groups))[0]

    # return a list of sets
    ids=[set(group) for group in groups]
    #print(len(ids))

    return ids

def groupIDs(data):
    print("[info]Grouping the FLT groups")

    # group those IDs
    nnew=ndata=len(data)
    while nnew!=0:
        new=[]
        while len(data)!=0:
            this=data.pop(0)
            for i,test in enumerate(data):
                if this.intersection(test):
                    this=this.union(test)
                    data.pop(i)
            new.append(this)
        data=new
        n=len(data)
        nnew=ndata-n
        ndata=n
    return data

def makeGroups(conf,grisms,sources,extconf,sort=True):
    print("[info]Starting the grouping algorithm")

    path=conf['tables']['path']

    # use the pool to group the FLTs
    p=pool.Pool(ncpu=conf['cpu']['ncpu'])
    ids=p(groupFLT,grisms.values,sources,extconf,path,\
          prefix='Grouping FLTs')

    # convert list of lists of sets to list of sets
    sets=[]
    for i in ids:
        sets.extend(i)
    del ids

    # group those IDs
    data=groupIDs(sets)

    # sort them by reverse order
    if sort:
        n=[len(d) for d in data]
        data=[d for _,d in reversed(sorted(zip(n,data)))]

    # make data for output
    out=[datum for datum in data]

    # print something for something's sake
    print("[info]Done grouping. Found {} groups.\n".format(len(out)))

    return out


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

def modulo (a):
    if a>0:
        a = a
    else:
        a = -a

    return (a)

n = int (input('Digite a quantidade de elementos da lista: '))

while (n<2):
    n = int (input('Digite a quantidade de elementos da lista: '))

a = []

for i in range (0,n,1):
    valor = int(input('Digite o elemento: '))
    a.append (valor)

degraus = []

for i in range (0,n,1):
    if i == 0:
        diferenca = diferenca = a[1]-a[0]
    elif i == len(a):
        diferenca = diferenca = a[len(a)-1]-a[len(a)-2]
    else:
        diferenca = a[i]-a[i-1]
    degrau = modulo (diferenca)
    degraus.append (degrau)

print (max (degraus))


"""
Training of multiple agents with multiple environments
"""

from __future__ import annotations

from agents.rl_agents.agents.dqn import TaskPricingDqnAgent, ResourceWeightingDqnAgent
from agents.rl_agents.neural_networks.dqn_networks import create_lstm_dqn_network
from training.train_agents import setup_tensorboard, multi_env_single_env_training

if __name__ == "__main__":
    folder = 'multi_agents_multi_envs'
    primary_writer, datetime = setup_tensorboard('training/results/logs/', folder)

    save_folder = f'{folder}_{datetime}'

    task_pricing_agents = [
        TaskPricingDqnAgent(agent_num, create_lstm_dqn_network(9, 16), save_folder=save_folder)
        for agent_num in range(3)
    ]
    resource_weighting_agents = [
        ResourceWeightingDqnAgent(agent_num, create_lstm_dqn_network(16, 10), save_folder=save_folder)
        for agent_num in range(2)
    ]

    multi_env_single_env_training(folder, datetime, primary_writer, task_pricing_agents, resource_weighting_agents)


import csv
import io
import traceback
import sys
from django.http import HttpResponse
from rest_framework import status
from rest_framework.authentication import (SessionAuthentication,
                                           TokenAuthentication)
from rest_framework.parsers import MultiPartParser
from rest_framework.permissions import IsAuthenticated
from rest_framework.response import Response
from rest_framework.views import APIView
from healthid.apps.products.models import Product, BatchInfo
from healthid.apps.orders.models.suppliers import \
    (Suppliers, SuppliersContacts, SuppliersMeta)
from healthid.apps.profiles.models import Profile
from healthid.apps.products.serializers import ProductsSerializer
from healthid.utils.orders_utils.add_supplier import AddSupplier
from healthid.utils.product_utils.handle_csv_export import handle_csv_export
from healthid.utils.product_utils.handle_csv_upload import HandleCsvValidations
from healthid.utils.constants.product_constants import (
    PRODUCT_INCLUDE_CSV_FIELDS, BATCH_INFO_CSV_FIELDS)
from healthid.utils.constants.customer_constants import (
    CUSTOMERS_INCLUDE_CSV_FIELDS)
from healthid.utils.constants.suppliers_infor_constants import \
    SUPPLIERS_INCLUDE_CSV_FIELDS
from healthid.utils.csv_export.generate_csv import generate_csv_response
from healthid.utils.customer_utils.handle_customer_csv_upload import\
    HandleCustomerCSVValidation
from healthid.utils.messages.customer_responses import SUCCESS_RESPONSES
from healthid.utils.messages.common_responses import ERROR_RESPONSES
from healthid.utils.messages.products_responses import (
    PRODUCTS_SUCCESS_RESPONSES
)

class HandleCSV(APIView):
    authentication_classes = (SessionAuthentication, TokenAuthentication)
    parser_classes = (MultiPartParser, )
    permission_classes = (IsAuthenticated, )

    def post(self, request, param, format=None):
        """
        Rest API post method that is meant to handle the mass,
        upload of information from csv files
        :param request: Will be used to extract the file type
        :param param: endpoint parameter confirming the nature of info,
                      expected in the csv
        :param format:
        :return: Throws error on validation failure and returns,
                  success on Successful data upload
        """
        handle_csv_object = HandleCsvValidations()
        handle_customer_csv_object = HandleCustomerCSVValidation()
        handle_supplier_csv_object = AddSupplier()
        handle_csv = handle_csv_object.handle_csv_upload
        handle_supplier_csv = handle_supplier_csv_object.handle_csv_upload
        handle_customer_csv_upload =\
            handle_customer_csv_object.handle_customer_csv_upload
        csv_file = request.FILES['file']

        if not csv_file.name.endswith('.csv'):
            message = {"error": "Please upload a csv file"}
            return Response(message, status.HTTP_400_BAD_REQUEST)
        data_set = csv_file.read().decode('UTF-8')

        io_string = io.StringIO(data_set)
        try:
            if param == 'suppliers':
                on_duplication = request.POST.get('on_duplication')
                user = request.user
                res = handle_supplier_csv(user=user,
                                          io_string=io_string,
                                          on_duplication=on_duplication)
                added_suppliers = res['supplier_count']
                message = {
                    "success": "Successfully added supplier(s)",
                    "noOfSuppliersAdded": res['supplier_count'],
                    "duplicatedSuppliers": res['duplicated_suppliers'],
                }
                if added_suppliers == 0:
                    return Response(
                        {
                            "message":
                            "No supplier has been added due to duplication",
                            "duplicatedSuppliers": res['duplicated_suppliers']
                        },
                        status.HTTP_400_BAD_REQUEST
                    )
                return Response(message, status.HTTP_201_CREATED)
            if param == 'products':
                user = request.user
                result = handle_csv(io_string=io_string, user=user)
                message = {
                    "message": ("Products successfully added"
                                if result['product_count']
                                else "No new products added"),
                    "noOfProductsAdded": result['product_count'],
                    "duplicatedProducts": result['duplicated_products'],
                }
                return Response(message,
                                status.HTTP_201_CREATED
                                if result['product_count']
                                else status.HTTP_400_BAD_REQUEST)
            if param == 'customers':
                next(io_string)
                customers_added = handle_customer_csv_upload(io_string)
                message = {
                    "success": SUCCESS_RESPONSES["csv_upload_success"],
                    "noOfCustomersAdded": customers_added,
                }
                return Response(message, status.HTTP_201_CREATED)
            if param == 'batch_info':
                user = request.user
                handle_csv_object.handle_batch_csv_upload(user, io_string)
                message = {
                    "success": PRODUCTS_SUCCESS_RESPONSES[
                        "batch_upload_success"
                    ]
                }
                return Response(message, status.HTTP_201_CREATED)
        except Exception as e:
            traceback.print_exc(file=sys.stdout)
            raise e

class HandleCsvExport(APIView):
    authentication_classes = (SessionAuthentication, TokenAuthentication)
    permission_classes = (IsAuthenticated, )
    serializer_class = ProductsSerializer

    def post(self, request, param):
        if param == 'products':
            limit = request.data.get('limit', None)
            query_set = Product.objects.filter(is_approved=True)[:limit]
            serializer = self.serializer_class(query_set, many=True)
            response = HttpResponse(content_type='text/csv')
            response['Content-Disposition'] = \
                'attachment; filename="products.csv"'
            headers = self.serializer_class.Meta.fields.copy()
            if request.data is not None:
                custom_headers = list()
                for key, value in request.data.items():
                    handle_csv_export.validate_request_data(
                        custom_headers, headers, key, value)
            headers = custom_headers if custom_headers else headers
            headers = \
                [handle_csv_export.capitalize(word) for word in headers]
            writer = csv.DictWriter(
                response,
                fieldnames=headers,
                extrasaction='ignore')
            writer.writeheader()
            for product in serializer.data:
                product = handle_csv_export.write_csv(product, request)
                writer.writerow(product)
            return response

class EmptyCsvFileExport(APIView):
    """Handle the download of empty CSV file"""
    authentication_classes = (SessionAuthentication, TokenAuthentication)
    permission_classes = (IsAuthenticated, )

    def get(self, request, param):
        """Responds to a get request for csv download based on the params
           with either products or customers or suppliers
        Args:
            request(obj): The http request from client.

        Returns:
            The response in a csv format with a status of 200.

        """
        if param == 'products':
            response = generate_csv_response(
                HttpResponse,
                'sample_product.csv',
                [Product],
                PRODUCT_INCLUDE_CSV_FIELDS)
        elif param == 'customers':
            response = generate_csv_response(
                HttpResponse,
                'sample_customers.csv',
                [Profile],
                CUSTOMERS_INCLUDE_CSV_FIELDS)
        elif param == 'batch_info':
            response = generate_csv_response(HttpResponse,
                                             'sample_batch_info.csv',
                                             [BatchInfo],
                                             BATCH_INFO_CSV_FIELDS,
                                             name='batch')
        elif param == 'suppliers':
            response = generate_csv_response(
                HttpResponse,
                'sample_suppliers.csv',
                [Suppliers,
                 SuppliersContacts,
                 SuppliersMeta],
                SUPPLIERS_INCLUDE_CSV_FIELDS)
        else:
            response = Response(ERROR_RESPONSES["wrong_param"],
                                status.HTTP_404_NOT_FOUND)
        return response


import os
import numpy as np
import cv2

img_path = 'Frames/sub'
target_path = 'Frames/sub_stack'
img_list = os.listdir(img_path)

img_H = 400
img_W = 800

window_size = 5

sub_tile_stack = np.zeros((img_H,img_W,window_size), dtype='f')
sub_tile_c = np.zeros((img_H,img_W), dtype='f')
sub_tile = np.zeros((img_H,img_W), dtype='f')

cnt = 0
stack_cnt = 0
is_save = 1

for name in img_list:

    cur_img_path  = os.path.join(img_path,name)

    sub_tile = cv2.imread(cur_img_path, cv2.IMREAD_GRAYSCALE)

    if stack_cnt >= window_size:
        sub_tile_c = sub_tile_c + sub_tile - sub_tile_stack[:,:,stack_cnt%window_size]
        sub_tile_stack[:,:,stack_cnt%window_size] = sub_tile
    else:
        sub_tile_stack[:,:,stack_cnt%window_size] = sub_tile
        sub_tile_c = sub_tile_c + sub_tile

    if is_save == 1:
        save_name = str("frame_sub%.5d.jpg" % cnt)
        save_path = os.path.join(target_path,save_name)
        cv2.imwrite(save_path, sub_tile_c)

    cnt = cnt+1
    stack_cnt = stack_cnt+1

print('Cumulate images done')


import numpy
import time
from sampler.sampler import SGCP_Sampler
from utils.utils import Utils

__author__ = "Christian Donner"

class AdamsSampler(SGCP_Sampler):

    def __init__(self, *args, **kwargs):
        super(AdamsSampler, self).__init__(*args, **kwargs)

        # Position of all events (first N are the actual observed ones)
        self.X_all = numpy.empty([self.N + self.M, self.dim])
        self.X_all[:self.N] = self.observed_events
        self.X_all[self.N:] = numpy.random.rand(self.M, self.dim)
        self.K = self.cov_func(self.X_all, self.X_all)
        self.L = numpy.linalg.cholesky(self.K + self.noise * numpy.eye(self.K.shape[0]))
        self.L_inv = numpy.linalg.solve(self.L, numpy.eye(self.L.shape[0]))
        self.K_inv = self.L_inv.T.dot(self.L_inv)

        L_X = numpy.linalg.cholesky(self.K[:self.N, :self.N] + self.noise * numpy.eye(self.N))
        L_X_inv = numpy.linalg.solve(L_X, numpy.eye(self.N))
        self.K_X_inv = L_X_inv.T.dot(L_X_inv)
        # Initial values of function g
        self.g = numpy.zeros([self.N + self.M])
        # Probability of insertion or deletion proposal
        self.pM = .5
        self.M_iterations = 10

    def run(self):

        print('Starting Adams')

        start = time.time()

        for i in range(self.maxiter):

            if i == 1:
                loop_start = time.time()

            if i == 2:
                print('Approximately %.2f min to go' % (loop * self.maxiter / 60))

            if (i > 0) and (i % 50 == 0):
                print(i)

            self.sample_g()
            self.sample_M()
            self.sample_locations()
            self.sample_upper_bound()

            self.llambdas[i] = self.upper_bound
            self.latent_M[i] = self.M
            self.intensities[i, :] = self.calc_intensities()
            self.events.append(self.X_all)
            self.gaussians.append(self.g)

            if i == 1:
                loop = time.time() - loop_start

        self.time = (time.time() - start) / 60
        print('Done in %.2f min' % self.time)
        self.mean_intensities = numpy.mean(self.intensities[self.burnin:], axis=0)

    def cov_func(self, x, x_prime):
        """ Computes the covariance functions between x and x_prime.
        :param x: numpy.ndarray [num_points x D]
            Contains coordinates for points of x
        :param x_prime: numpy.ndarray [num_points_prime x D]
            Contains coordinates for points of x_prime
        :return: numpy.ndarray [num_points x num_points_prime]
            Kernel matrix.
        """
        return self.cov_function(x, x_prime, self.kernelparameter)

    def sample_M(self):
        """ Samples number of events.
        """

        propose_insertion = numpy.random.rand(self.M_iterations) < self.pM
        rand_numbers_accept = numpy.random.rand(self.M_iterations)

        for i in range(self.M_iterations):
            # Insertion
            if propose_insertion[i]:
                xprime = numpy.random.rand(1, self.dim) * self.diff
                gprime = self.sample_from_cond_GP(xprime)
                a_ins = (1. - self.pM) * self.upper_bound * self.vol / (
                        (self.M + 1.) * self.pM * (1. + numpy.exp(gprime)))
                if rand_numbers_accept[i] < a_ins:
                    self.M += 1
                    self.X_all = numpy.concatenate([self.X_all, xprime])
                    self.g = numpy.concatenate([self.g, gprime])
                    self.update_kernels()
            # Deletion
            elif self.M > 0:
                del_idx = numpy.random.randint(self.N, self.N + self.M)
                gdel = self.g[del_idx]
                a_del = self.M * self.pM * (1. + numpy.exp(gdel)) / ((1. - self.pM) * self.vol * self.upper_bound)
                if rand_numbers_accept[i] < a_del:
                    self.M -= 1
                    self.X_all = numpy.delete(self.X_all, del_idx, 0)
                    self.g = numpy.delete(self.g, del_idx, 0)
                    self.update_kernels()

    def sample_locations(self):

        theta2 = self.kernelparameter[1]
        proposed_X = self.X_all[self.N:] + numpy.random.randn(self.M, self.dim) * theta2[numpy.newaxis, :]

        # proposed_X = self.X_all[self.N:] + numpy.random.randn(self.L, self.dim) * self.kernelparameter

        rand_num_accept = numpy.random.rand(self.M)

        inregion1 = numpy.equal(numpy.sum(proposed_X < 0, axis=1), 0)
        inregion2 = numpy.equal(numpy.sum((self.diff - proposed_X) < 0, axis=1), 0)  ############### !!!! ############
        inregion = numpy.where(numpy.logical_and(inregion1, inregion2))[0]

        for im in inregion:
            xprime = proposed_X[im]
            gprime = self.sample_from_cond_GP(numpy.array([xprime]))
            a_loc = (1. + numpy.exp(self.g[im + self.N])) / (1. + numpy.exp(gprime))
            if rand_num_accept[im] < a_loc:
                self.X_all[im + self.N] = xprime
                self.g[im + self.N] = gprime
                self.update_kernels_loc(im, xprime)

        self.update_kernels()

    def update_kernels_loc(self, im, xprime):
        new_row = self.cov_func(self.X_all, xprime[numpy.newaxis, :])
        self.K[im + self.N] = new_row[:, 0]
        self.K[:, im + self.N] = new_row[:, 0]
        B = self.K[:self.N, self.N:]
        C = self.K[self.N:, self.N:]
        self.K_inv = self.invert_block_matrix(self.K_X_inv, B, C)

    def sample_upper_bound(self):
        self.upper_bound = numpy.random.gamma(self.N + self.M + self.alpha, 1 / (self.vol + self.beta))

    def sample_g(self):
        r_init = numpy.random.randn(self.g.shape[0])
        H_init = self.compute_Hamiltonian(r_init)
        proposed_g, proposed_r = self.leap_frog_integration(r_init)
        H_proposed = self.compute_Hamiltonian(proposed_r, proposed_g)
        a_funcg = numpy.exp(H_init - H_proposed)
        rand_num_accept = numpy.random.rand(1)

        if rand_num_accept < a_funcg:
            self.g = proposed_g

    def compute_Hamiltonian(self, r, g=None):
        if g is None:
            g = self.g
        E = .5 * g.T.dot(self.K_inv.dot(g)) + \
            numpy.sum(numpy.log(1. + numpy.exp(-g[:self.N]))) + \
            numpy.sum(numpy.log(1. + numpy.exp(g[self.N:])))
        K = .5 * numpy.sum(r ** 2)
        return E + K

    def leap_frog_integration(self, r, g=None, step_size=1e-2):
        if g is None:
            g = numpy.copy(self.g)
        gtilde = self.L_inv.dot(g)
        step_size += 1e-4 * numpy.random.randn()

        rand_num = numpy.random.rand(1)
        if rand_num < .5:
            step_size *= -1

        num_steps = int(numpy.ceil(1. / numpy.abs(step_size)))

        for isteps in range(num_steps):
            dgtilde = self.compute_gradient_gtilde(gtilde)
            r += .5 * step_size * dgtilde
            gtilde -= step_size * r
            dgtilde = self.compute_gradient_gtilde(gtilde)
            r += .5 * step_size * dgtilde
        proposed_g = self.L.dot(gtilde)
        return proposed_g, r

    def compute_gradient_gtilde(self, gtilde):
        g = self.L.dot(gtilde)
        v = numpy.zeros([self.M + self.N])
        v[:self.N] = - 1. / (1. + numpy.exp(g[:self.N]))
        v[self.N:] = 1. / (1. + numpy.exp(-g[self.N:]))
        dgtilde = gtilde + v.dot(self.L)
        return dgtilde

    def update_kernels(self):
        self.K = self.cov_func(self.X_all, self.X_all)
        self.L = numpy.linalg.cholesky(self.K + self.noise * numpy.eye(self.K.shape[0]))
        self.L_inv = numpy.linalg.solve(self.L, numpy.eye(self.L.shape[0]))
        self.K_inv = self.L_inv.T.dot(self.L_inv)

    def sample_from_cond_GP(self, xprime):
        k = self.cov_func(self.X_all, xprime)
        mean = k.T.dot(self.K_inv.dot(self.g))
        kprimeprime = self.cov_func(xprime, xprime)
        cov = kprimeprime - k.T.dot(self.K_inv.dot(k))
        if len(cov) == 1:
            var = cov.diagonal()
            gprime = mean + numpy.sqrt(var) * numpy.random.randn(xprime.shape[0])
        else:
            gprime = Utils.sample_gaussian(mean[:, numpy.newaxis], cov + numpy.eye(len(cov)) * self.noise).flatten()
        return gprime

    def invert_block_matrix(self, A_inv, B, C):
        """ Inverts a matrix of the form
                [[A,  B]
                 [B', C]].
        """

        A_dim, C_dim = A_inv.shape[0], C.shape[0]
        Mat_dim = A_dim + C_dim
        M_inv = C - B.T.dot(A_inv.dot(B))
        LM_inv = numpy.linalg.cholesky(M_inv + 1e-3 * numpy.eye(C_dim))
        LM = numpy.linalg.solve(LM_inv, numpy.eye(LM_inv.shape[0]))
        M = LM.T.dot(LM)

        Mat_inv = numpy.empty([Mat_dim, Mat_dim])
        Mat_inv[:A_dim, :A_dim] = A_inv + A_inv.dot(B.dot(M.dot(B.T.dot(A_inv))))
        Mat_inv[:A_dim, A_dim:] = - A_inv.dot(B.dot(M))
        Mat_inv[A_dim:, :A_dim] = Mat_inv[:A_dim, A_dim:].T
        Mat_inv[A_dim:, A_dim:] = M

        return Mat_inv

    def calc_intensities(self):
        g_pred = self.sample_from_cond_GP(self.grid_events)
        return self.upper_bound * SGCP_Sampler.sigmoid(g_pred)


#!/usr/bin/env python
from fingerprint import fingerprint
from subprocess import call

os = fingerprint()

#Gets epm keyword correct
epm_keyword = {"ubuntu":"dpkg", "redhat":"rpm", "SunOS":"pkg", "osx":"osx"}

#try:
#    epm_keyword[os]
#except Exception, err:
#    print err

#if epm_keyword.has_key(os):
if os in epm_keyword:
        platform_cmd = epm_keyword[os]

subprocess.call("epm -f %s helloEPM hello_epm.list" % platform_cmd, shell=True)


import pytest

from copy import deepcopy
from lumapps.client import pop_matches, ApiClient

def test_pop_matches():
    d = {"a": 1, "b": {"c": 2, "d": {"e": 3}}, "z": 33}

    d2 = deepcopy(d)
    pth = "b/d/e"
    pop_matches(pth, d2)
    assert d2 == {"a": 1, "b": {"c": 2, "d": {}}, "z": 33}

    d2 = deepcopy(d)
    pth = "b"
    pop_matches(pth, d2)
    assert d2 == {"a": 1, "z": 33}

    d2 = deepcopy(d)
    pth = ""
    pop_matches(pth, d2)
    assert d2 == {"a": 1, "b": {"c": 2, "d": {"e": 3}}, "z": 33}

def test_api_client_no_auth():
    with pytest.raises(Exception):
        ApiClient()

def test_api_client_token_setter():
    token = "bvazbduioanpdo2"
    client = ApiClient(token=token)
    assert client.creds is not None
    assert client.creds.token == token


# This file is part of the SLURM utility
# Implements a user interface for the data processor
import subprocess
import sys
import os

from ..core import core
from . import fargs

def process_py(func, root_path=".", config_file="config.ini", start=0):
    try:
        proc = get_processor("python", func, root_path=root_path, config_file=config_file)
    except ValueError as e:
        sys.stdout.write("An error occurred: " + str(e) + "\n")
        sys.exit(1)

    proc.run(start=start)

def process_exec(filename):
    try:
        proc = get_processor("external", filename)
    except ValueError as e:
        sys.stdout.write("An error occurred: " + str(e) + "\n")
        sys.exit(1)

    proc.run()

class DataProcessor(core.DataProcessor):
    """Run specified Python function on data."""
    def __init__(self, func, root_path=None, config_file=None):
        super(DataProcessor, self).__init__(root_path=root_path, config_file=config_file)
        #check if func takes three arguments!
        if fargs.get_number_fargs(func) == 3:
            self.process = func
        else:
            raise ValueError("Function must take exactly three arguments (dirname, job_idx, plist).")

    def run(self, start=0):
        self.iterate(start=start)

def get_processor(script_type, func, root_path=".", config_file="config.ini"):
    """Factory function for processors"""
    if script_type.lower() == 'python':
        # this can raise an exception.
        # ok, since we expect this to be run from within an external Python script anyhow
        return DataProcessor(func, root_path, config_file)
    elif script_type.lower() == 'external':
        try:
            return ExternalProcessor(func, root_path=root_path, config_file=config_file)
        except OSError as e:
            sys.stderr.write(str(e))
            sys.exit(1)
        except Exception as e:
            # can only happen if Popen raises some exception. Timeout?
            sys.stderr.write("There has been an error:\n" + str(e))
            sys.exit(1)

    elif script_type.lower() == 'bash':
        try:
            return ShellProcessor(func, root_path=root_path, config_file=config_file)
        except Exception as e:
            sys.stderr.write(str(e))
            sys.exit(1)

    else:
        raise NotImplementedError(script_type)

class ShellProcessor(core.DataProcessor):
    """Run shell script on data."""
    def __init__(self, script_name, root_path=None, config_file=None):
        super(ShellProcessor, self).__init__(root_path=root_path, config_file=config_file)
        self._script_name = os.path.abspath(script_name)

    def process(self, dirname, job_idx, plist):
        # maybe copy the script file to a temporary file in the job_directory
        p = subprocess.Popen(['bash', self._script_name], stderr=subprocess.PIPE, stdout=subprocess.PIPE)
        stdout, stderr = p.communicate()
        sys.stdout.write(stdout.decode('utf-8'))
        sys.stderr.write(stderr.decode('utf-8'))

    def run(self, start=0):
        self.iterate(start=start)

class ExternalProcessor(core.DataProcessor):
    """Run external program on data."""
    def __init__(self, filename, root_path=None, config_file=None):
        super(ExternalProcessor, self).__init__(root_path=root_path, config_file=config_file)
        filename = os.path.join("..", filename) # ParameterIterator cd's into job-dirs
        if not os.access(filename, os.F_OK):
            raise OSError("File {} does not exist.".format(filename))
        if not os.access(filename, os.X_OK):
            raise OSError("File {} is not an executable.".format(filename))
        self._executable = os.path.abspath(filename)

    def process(self, firname, job_idx, plist):
        p = subprocess.Popen([self._executable], stderr=subprocess.PIPE, stdout=subprocess.PIPE)
        stdout, stderr = p.communicate()
        sys.stdout.write(stdout.decode('utf-8'))
        sys.stderr.write(stderr.decode('utf-8'))

    def run(self, start=0):
        self.iterate(start=start)


import sys
from PyQt5.QtWidgets import *
from PyQt5.QtCore import QCoreApplication, Qt
from date_timer import DateTimer
from datetime import datetime

class Window(QMainWindow):
    def __init__(self):
        super().__init__()
        self.timer = DateTimer()
        self.time_list = []
        self.nod_list = []
        self.initUI()

    def initUI(self):
        label0 = QLabel("Push Enter for start", self)
        label0.move(15, 10)
        label0.resize(300, 30)

        label1 = QLabel("Push N for nodding \"はい\"", self)
        label1.move(15, 40)
        label1.resize(300, 30)

        label2 = QLabel("Push M for nodding others", self)
        label2.move(15, 70)
        label2.resize(300, 30)

        label3 = QLabel("Don't push Space!", self)
        label3.move(15, 100)
        label3.resize(300, 30)

        finish_button = QPushButton("finish", self)
        finish_button.clicked.connect(self.finish_button_clicked)
        finish_button.move(100, 150)

        self.statusBar()
        self.setWindowTitle("")
        self.resize(250, 200)
        self.show()

    def keyPressEvent(self, e):
        if e.key() == Qt.Key_Return:
            self.start()
        elif e.key() == Qt.Key_N:
            self.nod()
        elif e.key() == Qt.Key_M:
            self.nod(others=True)

    def nod(self, others=False):
        if self.timer.running:
            self.time_list.append(self.timer.count())
            log = str(self.timer.count()) + "    "
            # print(others)
            if others:
                self.nod_list.append("other")
                log += "other"
            else:
                self.nod_list.append("hai")
                log += "hai"
            print(log)

    def start(self):
        if not self.timer.running:
            self.timer.start()
            print("start")

    def finish_button_clicked(self):
        now = datetime.now().strftime("%Y%m%d%H%M%S")
        with open("time_truck/time_truck{0}.txt".format(now), mode="x") as f:
            write_list = "\n".join(["{0:0<10}\t{0:0<10}\t{1}".format(self.time_list[i], self.nod_list[i])
                                    for i in range(len(self.time_list))])
            f.writelines(write_list)
            print(write_list)
        QCoreApplication.instance().quit()

if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = Window()
    sys.exit(app.exec_())


# test_load_recipe.py
# ---------
# Related to recipe.py

""" Tests loading of recipe file into list
"""

import unittest
from recipe import load_xml_header_list, load_xml_recipe_ingredients_list
from recipe import load_recipe_ingredients_list, load_recipe_list
from load_recipe import load_recipe_header_list

class test_load_recipe(unittest.TestCase):

  def test_load_named_recipe_header_into_list(self):
    recipe_header = load_recipe_header_list(
      "C:\Documents and Settings\All Users\Documents\My BigOven Recipes\Publish",
      "Gordon_Hamersley_s_Boned_Stuffed_Leg_of_Lamb_1.htm", [])
    self.assertEqual.im_class.maxDiff = None
    self.assertEqual(recipe_header.recipe_list,
      [["Gordon Hamersley's Boned Stuffed Leg of Lamb", "", "8 Servings", "",
      """New York Times Magazine - FOOD; NEW ENGLAND FANCY
BY NANCY HARMON JENKINS: Nancy Harmon Jenkins, a frequent contributor to the Food column, lives in the Boston area.
Published: April 2, 1989
[ Wait time: Minimum = 0 hours]""",
      """1.Preheat the oven to 375 degrees. Place the lamb bones in a roasting pan and roast for about 45 minutes. Roughly chop the unpeeled onion, the carrots and the celery and add to the roasting pan with the tomato paste. Mix to distribute the vegetables and coat them with tomato paste and fat from the lamb. Return to the oven and roast an additional 15 minutes.

2.Remove bones and vegetables from the pan and place them in a stock pot that is just large enough to hold them comfortably. Deglaze the roasting pan with the red wine over high heat and add to the stock pot. Then add enough chicken stock or water to just cover the bones in the pan.

Bring to a boil, skim any scum that rises and add a pinch of salt, two cloves of garlic, the bay leaf and peppercorns. Lower the heat to simmering, partially cover the stock pot and simmer for at least two hours. At the end of this time, the stock should have a destinctive lamb flavor.

3.Remove stock from the heat and strain through a coarse sieve. Return strained stock to the pan and cook briskly to reduce and thicken the stock. You should have about four cups of sauce when it is reduced.

4.While the sauce is cooking, clean and rinse the kale, stripping the leaves from the ribs. Discard the ribs. In a saute pan over medium high heat saute the kale in three tablespoons of olive oil until it is softened but still a little crisp to the bite. (You may need to add a bit of water to keep the kale from burning.) Remove from the heat and set aside.

5.Roast the red peppers over a gas flame, an electric grill or in a preheated broiler, turning until the outsides are blackened. Place in a paper bag for about 15 minutes to soften the skins. Then peel the peppers, discard the seeds and inner membranes and cut in thick slices.

6.Roughly chop the olives together with the peeled shallot and remaining garlic cloves. Mix in dried herbs.

7.Spread the butterflied lamb out on a countertop, skin side down. If the butcher has left the nugget of fat on the inside of the leg, remove and discard it. Layer the kale over the lamb, then layer the strips of red pepper over that. Sprinkle the olives, garlic and shallot pieces over the pepper strips, then sprinkle salt and cracked black pepper and about half the one-quarter cup of olive oil over the vegetables.

8.Roll the lamb leg, securing the stuffing, and tie it in several places with butcher's twine, tucking in the loose flaps at both ends. The result will be a long cylinder.

9.Preheat the oven to 400 degrees. Place the lamb roll on a rack in a roasting pan and roast for exactly one hour. Remove from the oven and set aside to rest for 15 minutes. Carve in thick (onehalf to three-quarter-inch) slices and serve on heated plates with a little of the hot sauce. Pass the rest of the sauce in a sauce boat.

Yield:Eight servings.""",
      ]])

  def test_load_second_named_recipe_header_into_list(self):
    recipe_header = load_recipe_header_list(
      "C:\Documents and Settings\All Users\Documents\My BigOven Recipes\Publish",
      "Ham_Biscuits_-_Gourmet_February_1995_Jessica_B__Harris_3.htm", [])
    self.assertEqual(recipe_header.recipe_list,
      [["Ham Biscuits - Gourmet February 1995 Jessica B. Harris", "Biscuits", "20 Servings",
      """This is a surefire Southern way to use leftover ham and either leftover or freshly baked biscuits.
Sometimes the biscuits are large and the ham pieces merely roughly cut chunks. In this recipe the biscuits are made the size of silver dollars, the ham is thinly sliced and neatly trimmed, and the
biscuits are served with tiny dollops of spicy mustard and freshly grated horseradish. Voil\xe0! The "grab \'em and go" breakfast treat that has been imitated by many popular food chains is
transmuted into an elegant hors d\'oeuvre. Large or small, they\'re mighty good eating.
Can be prepared in 45 minutes or less.""", "",
      """Silver dollar - size biscuits are the quintessential African-American breakfast bread. They can be prepared in 45 minutes or less.

Split biscuits in half and spread lightly with mustard and horseradish. Put ham between biscuit halves.

Preheat oven to 400\xb0F.

In a bowl whisk together flour, baking powder, salt and sugar and with a fork or pastry blender blend in lard until mixture resembles course meal. Add milk, a little at a time, stirring until dough
just pulls away from side of bowl.

Roll out dough 1/2 inch thick on a lightly floured surface. Cut out biscuits with a 1 1/2-inch round cutter and transfer to an ungreased baking sheet. Reroll scraps and cut out more biscuits in same
manner.

Bake biscuits in middle of oven until pale golden, 10 to 15 minutes. Makes 20 silver dollar-size biscuits.

Makes about 20 ham biscuits.""",
      ]])

  def test_load_named_recipe_ingredients_into_list(self):
    recipe_ingredients = load_recipe_ingredients_list(
      "C:\Documents and Settings\All Users\Documents\My BigOven Recipes\Publish",
      "Gordon_Hamersley_s_Boned_Stuffed_Leg_of_Lamb_1.htm", [])
    self.assertEqual(recipe_ingredients.recipe_list,[
      ["1", "1", "ea", "8-pound  leg of lamb (weighed with the bone), boned and butterflied"],
      ["2", "6", "ea", "whole peppercorns"],
      ["3", "0", "ea", " Bones from the lamb"],
      ["4", "1", "bunch", "green kale"],
      ["5", "1/2", "large", "onion unpeeled"],
      ["6", "3", "tablespoons", "olive oil"],
      ["7", "1/4", "cup", " olive oil"],
      ["8", "2", "ea", "carrots, scrubbed but unpeeled"],
      ["9", "3", "ea", "sweet red peppers"],
      ["10", "2", "ea", "stalks of celery with leaves"],
      ["11", "3", "ea", "ounces jumbo Kalamata olives pitted (about 18 olives)"],
      ["12", "1 1/2", "tablespoons", "tomato paste"],
      ["13", "1", "ea", "shallot"],
      ["14", "1 1/2", "cups", "dry red wine"],
      ["15", "1", "teaspoon", "dried thyme"],
      ["16", "3", "cups", "water or chicken stock"],
      ["17", "1", "teaspoon", "dried oregano"],
      ["18", "4", "ea", "cloves garlic Approximately"],
      ["19", "0", "ea", " Kosher salt"],
      ["20", "1", "ea", "bay leaf"],
      ["21", "0", "ea", " Cracked black pepper"]
      ])

  def test_load_second_named_recipe_ingredients_into_list(self):
    recipe_ingredients = load_recipe_ingredients_list(
      "C:\Documents and Settings\All Users\Documents\My BigOven Recipes\Publish",
      "Ham_Biscuits_-_Gourmet_February_1995_Jessica_B__Harris_3.htm", [])
    self.assertEqual(recipe_ingredients.recipe_list,[
      ["1", "20", "ea", "silver dollar-size biscuits"],
      ["2", "1", "tablespoon", "baking powder"],
      ["3", "0", "ea", " spicy prepared mustard to taste"],
      ["4", "1", "teaspoon", "salt"],
      ["5", "0", "ea", " freshly grated horseradish to taste"],
      ["6", "1", "tablespoon", "sugar"],
      ["7", "20", "ea", "pieces thinly sliced baked ham"],
      ["8", "1/2", "cup", "milk"],
      ["9", "0", "ea", "SILVER DOLLAR SIZED BISCUITS"],
      ["10", "1/4", "cup", "milk"],
      ["11", "2", "cups", "all-purpose flour"]
      ])

  def assertLoadRecipeTitle(self, input_directory, input_recipe, expected_title):
    recipe_ingredients = load_recipe_list(input_directory,input_recipe, [])
    recipe_list = recipe_ingredients.recipe_list
    if any(expected_title in s for s in recipe_list):
      title_search_result = True
    else:
      title_search_result = False
    self.assertTrue(title_search_result)

  def test_load_named_recipe_into_list(self):
    self.assertLoadRecipeTitle(
      "C:\Documents and Settings\All Users\Documents\My BigOven Recipes\Publish",
      "Gordon_Hamersley_s_Boned_Stuffed_Leg_of_Lamb_1.htm",
      "Gordon Hamersley's Boned Stuffed Leg of Lamb")

  def test_load_second_named_recipe_into_list(self):
    self.assertLoadRecipeTitle(
      "C:\Documents and Settings\All Users\Documents\My BigOven Recipes\Publish",
      "Ham_Biscuits_-_Gourmet_February_1995_Jessica_B__Harris_3.htm",
      "Ham Biscuits - Gourmet February 1995 Jessica B. Harris")

  def test_load_variations(self):
    recipe_header = load_recipe_header_list(
      "C:\Documents and Settings\All Users\Documents\My BigOven Recipes\Publish",
      "Gordon_Hamersley_s_Boned_Stuffed_Leg_of_Lamb_1.htm", [])
    self.assertEqual.im_class.maxDiff = None
    self.assertEqual(recipe_header.recipe_list[0][4],
      """New York Times Magazine - FOOD; NEW ENGLAND FANCY
BY NANCY HARMON JENKINS: Nancy Harmon Jenkins, a frequent contributor to the Food column, lives in the Boston area.
Published: April 2, 1989
[ Wait time: Minimum = 0 hours]""")

  def test_load_subhead(self):
    recipe_header = load_recipe_header_list(
      "C:\Documents and Settings\All Users\Documents\My BigOven Recipes\Publish",
      "Gordon_Hamersley_s_Boned_Stuffed_Leg_of_Lamb_1.htm", [])
    self.assertEqual.im_class.maxDiff = None
    self.assertEqual(recipe_header.recipe_list[0][3], "")

  def test_load_second_subhead(self):
    recipe_header = load_recipe_header_list(
      "C:\Documents and Settings\All Users\Documents\My BigOven Recipes\Publish",
      "Ham_Biscuits_-_Gourmet_February_1995_Jessica_B__Harris_3.htm", [])
    self.assertEqual.im_class.maxDiff = None
    self.assertEqual(recipe_header.recipe_list[0][3],
      """This is a surefire Southern way to use leftover ham and either leftover or freshly baked biscuits.
Sometimes the biscuits are large and the ham pieces merely roughly cut chunks. In this recipe the biscuits are made the size of silver dollars, the ham is thinly sliced and neatly trimmed, and the
biscuits are served with tiny dollops of spicy mustard and freshly grated horseradish. Voil\xe0! The "grab 'em and go" breakfast treat that has been imitated by many popular food chains is
transmuted into an elegant hors d'oeuvre. Large or small, they're mighty good eating.
Can be prepared in 45 minutes or less.""")

  def test_load_named_xml_recipe_header_into_list(self):
   recipe_header = load_xml_header_list(
      "C:\Documents and Settings\All Users\Documents\My BigOven Recipes\Publish",
      "_Victoria's_Pumpkin_Bread_(From__The_Dairy_Hollow_House_Cookbook__by_Crescent_Dragonwagon_with_Jan_Brown,_Macmillan_Publishing_Company).xml",
      [])
   self.assertEqual.im_class.maxDiff = None
   self.assertEqual(recipe_header.recipe_list,
     [[' Victoria\'s Pumpkin Bread (From "The Dairy Hollow House Cookbook" by Crescent Dragonwagon with Jan Brown, Macmillan Publishing Company)',
     '','','',
     "New York Times Magazine - Food; A Timely Tea By Liz LoganPublished: October 28, 1990[ Wait time: Minimum = 0 hours]1. Preheat the oven to 350 degrees.2. In a large bowl, sift together the cinnamon, baking powder, nutmeg, baking soda, salt, ground cloves, ground ginger, allspice and flour. Set aside.3. In a separate bowl, mix together the vegetable oil, yogurt, eggs, sugar and pumpkin puree. Mix until smooth.4. Combine the two mixtures and beat until smooth. Fold in the walnuts.5. Pour the batter into three 8-by-4-inch (or 9-by-5-inch) loaf pans. (The batter should not reach more than two-thirds of the way up each pan.) Bake for about 1 hour, or until the loaves shrink away from the sides of the pan and have a hollow sound when tapped. Test for doneness after 45 minutes.Yield: Three loaves."
     ]])

  def test_load_second_named_xml_recipe_header_into_list(self):
   recipe_header = load_xml_header_list(
      "C:\Documents and Settings\All Users\Documents\My BigOven Recipes\Publish",
      "Beignets_-TechieVampire.xml",
      [])
   self.assertEqual.im_class.maxDiff = None
   self.assertEqual(recipe_header.recipe_list,
     [["Beignets -TechieVampire","","","",
     'Half fill a large, deep saucepan with oil and preheat it to 375 degrees F. Sift together the flour, salt, baking powder, and cinnamon into a large mixing bowl. In a separate bowl, beat the egg well, then beat in the sugar, milk, and vanilla. Pour the egg mixture into the flour and mix slightly until moistened. Turn onto a floured surface and knead lightly. Roll the dough out to a 1/4 inch thickness. Slice into diamond shapes, about 3 inches long. Fry in oil, turning once, until they are golden brown and puffed up. Remove, drain on paper towels, and sprinkle with confectioners\xe2\x80\x99 sugar. Serve immediately.']])

  def test_load_named_xml_recipe_ingredients_into_list(self):
    recipe_ingredients = load_xml_recipe_ingredients_list(
      "C:\Documents and Settings\All Users\Documents\My BigOven Recipes\Publish",
      "_Victoria's_Pumpkin_Bread_(From__The_Dairy_Hollow_House_Cookbook__by_Crescent_Dragonwagon_with_Jan_Brown,_Macmillan_Publishing_Company).xml",
      [])
    self.assertEqual(recipe_ingredients.recipe_list,[
      ["2", "teaspoons", "ground cinnamon"],
      ["2", "teaspoons", "baking powder"],
      ["1", "teaspoon", "nutmeg"],
      ["1", "teaspoon", "baking soda"],
      ["1", "teaspoon", "salt"],
      ["1/2", "teaspoon", "ground cloves"],
      ["1/4", "teaspoon", "ground ginger"],
      ["1", "dash", "allspice"],
      ["6", "cups", "unbleached white flour"],
      ["1", "cup", "mild vegetable oil"],
      ["1/2", "cup", "yogurt"],
      ["4", "", "eggs"],
      ["3", "cups", "sugar"],
      ["2 1/2", "cups", "unsweetened pumpkin puree"],
      ["1", "cup", "chopped black walnuts"]
      ])

  def test_load_second_named_xml_recipe_ingredients_into_list(self):
    recipe_ingredients = load_xml_recipe_ingredients_list(
      "C:\Documents and Settings\All Users\Documents\My BigOven Recipes\Publish",
      "Beignets_-TechieVampire.xml",
      [])
    self.assertEqual(recipe_ingredients.recipe_list,[
      ["2", "cups", "all purpose flour"],
      ["1", "teaspoon", "salt"],
      ["1", "tablespoon", "baking powder"],
      ["1", "teaspoon", "ground cinnamon"],
      ["1", "", "medium sized egg"],
      ["3", "tablespoons", "sugar"],
      ["1", "cup", "milk"],
      ["1/4", "teaspoon", "vanilla extract"],
      ["1", "quart", "oil for deep frying"],
      ["0", "", "Confectioners\xe2\x80\x99 sugar"]
      ])


"""
The following program will be used to recommend based on a Machine Learning approach. The user enters a userid,
The userid is checked from the user-movies file and the corresponding movies are taken and the data is split into two sets:
1. The training data
2. The test data
Movies rated from 2.5 and below are taken as 'not liked' and movies rated >2.5 are rated as 'liked'. For logistic regression the features in this case would simply be the tf(term frequency) values. So for every training of the data a tfidf score would be calculated and accordingly will be used for recommendation. It is also more effective to keep the trained model for a particular user for future calculations.
Accordingly a 2 class classifier is built.
Purpose:
1. Evaluation
For evaluation of the recommender system a simple appraoch would be to simply use 2/3 of the data for training and 1/3rd of the data for testing purpose.
2. Recommending movies not watched by the current user
For movies not watched by the current user we can use the entire data of the user for purposes of training and then on separate movies not watched by the user we can say whether the user would like it or not.
"""
import csv
import math
from scipy import sparse
import numpy as np
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.feature_extraction.text import TfidfTransformer
from sklearn import linear_model
v=CountVectorizer(stop_words=['The','a'])

USER_MOVIE_FILENAME="../../../dataSets/movieLens/user_ratedmovies.csv"
MOVIE_FILENAME="../MovieLens/ResultMovieDataSetClone.csv"
VALID_LIST_INDICES=[24,27,35]

def openFile(fileName):
    csvFile=open(fileName, newline="")
    reader=csv.reader(csvFile)
    return reader

def stringToList(s): # this is a hack: avoid as far as possible

        s=s.strip('[')
        s=s.strip(']')
        # print(s)
        s=s.strip("'")
        # print(s)
        return s.split("', '")

# columns to be used for tf vectorization: actorName(24), genre(27), directorName(35)

class AllUsers:

    def __init__(self):
        self.users=[]
        self.movieID=[]
        self.ratings=[]

    def getAllUserDetails(self):

        csvReader=openFile(USER_MOVIE_FILENAME)
        rowIter=iter(csvReader)
        next(rowIter) # to get rid of the first row
        # users=[]
        while True:
            try:

                row=next(rowIter)
                self.users.append(int(row[0]))
                self.movieID.append(int(row[1]))
                self.ratings.append(float(row[2]))

            except StopIteration:
                # StopIteration exception is reached when the iterator has iterated thorugh all the rows
                # of the csvFile
                break

class User:
    # all user details from the user-movie file

    def __init__(self, users, movieID, ratings):
        self.index=-1

        self.userStartIndex=-1
        self.userEndIndex=-1

        self.users=users
        self.movieID=movieID
        self.ratings=ratings

        # the following are the 2 lists that will be used directly by the other modules
        self.userRatedMovies=[] # contains the allocated movie ids (not the true movie ids)
        self.userRatings=[]

    def getUserApproxIndex(self, userID):

        low=0
        high=len(self.users)-1
        self.index=-1
        # index=-1
        while low <= high:
            mid=int((low+high)/2)
            if self.users[mid]==userID:
                # print("found index:"+str(mid))
                self.index=mid
                break
            elif self.users[mid]>userID:
                high=mid-1
            elif self.users[mid]<userID:
                low=mid+1

    def getUserStartIndex(self, userID):

        if self.index==-1:
            return -1
        else:
            # startIndex of the userID to be found
            i=self.index
            while i>=0:
                if userID!=self.users[i]:
                    self.userStartIndex=i+1
                    return i+1
                i-=1
            return 0

    def getUserEndIndex(self, userID):

        if self.index==-1:
            return -1
        else:
            # startIndex of the userID to be found
            i=self.index
            while i<len(self.users):
                if userID!=self.users[i]:
                    self.userEndIndex=i
                    return i
                i+=1
            return len(self.users)-1

    def getUserDetails(self, userID): # from this class only this function is called
        self.getUserApproxIndex(userID)
        if self.index==-1:
            raise Exception('Unknown user')
        startIndex=self.getUserStartIndex(userID)
        endIndex=self.getUserEndIndex(userID)
        # print("start index is:"+str(startIndex)+"\n end index:"+str(endIndex))
        userRatings=[]
        for i in range(startIndex, endIndex):
            self.userRatedMovies.append(self.movieID[i])
            self.userRatings.append(self.ratings[i])

class Document:
    # this class returns the documents for the tf vectorizer
    def __init__(self, userRatedMovies):

        self.userRatedMovies=userRatedMovies
        # list of allocated ids of the movies that the user had rated.
        self.documents={}

    def makeDocuments(self):
        reader=openFile(MOVIE_FILENAME)
        next(reader)
        # to skip the very first line
        for row in reader:
            countCol=0
            allocatedID=int (row[1])
            if allocatedID not in self.userRatedMovies:
                continue
            id=row[0]
            self.documents[id]=''

            # returns one row from table at a time
            for value in row:

                if countCol in VALID_LIST_INDICES:
                    # the value is actaully a string that needs to be converted to a list
                    l=stringToList(value)

                    if countCol==24:
                        for index in range(len(l)):
                            l[index]=l[index].replace(' ','')
                    for element in l:
                        self.documents[id]+=' '+element

                countCol+=1

class TfVectorizer:

    def __init__(self, documents, userRatings):

        self.documents=documents
        self.ratings=userRatings # a list that stores all the user ratings serially
        self.labels=None
        self.trainModel=None
        self.termDocMatrix=None

    def vectorize(self):

        """
        term frequency vectorization is done by method
        """

        trainSet=[]

        check=0
        for key in self.documents:

            # if check==0:
            #     check+=1
            #     print(self.documents[key])

            trainSet.append(self.documents[key])

        # trainSet prepared

        # converting to term document matrix

        self.trainModel=v.fit(trainSet)

        termDocMatrix=v.transform(trainSet)

        self.termDocMatrix=termDocMatrix
        # termDocMatrix is a scipy sparse matrix

    """
    This function defines the labels for the data if the rating is >2.5 it is assigned
    """
    def defineLabels(self):

        for rating in self.ratings:
            if rating>2.5:
                # user has liked the movie
                self.labels.append(1)
            else:
                # user has not liked the movie
                self.labels.append(0)
        # the following is an important step
        self.labels=np.array(self.labels)

class SampleTfVectorizer:

    def __init__(self, document, trainModel):
        self.document=document
        self.trainModel=trainModel

    def vectorizer(self):
        trainSet=[]

        check=0
        for key in self.documents:

            # if check==0:
            #     check+=1
            #     print(self.documents[key])

            trainSet.append(self.documents[key])

        # trainSet prepared

        # converting to term document matrix

        trainedModel=v.fit(trainSet)

        termDocMatrix=v.transform(trainSet)

        # here we need to make changes to the termDocMatrix

        sampleFeatureNames=trainedModel.get_feature_names()
        trainFeatureNames=self.trainModel.get_feature_names()
        modifiedTermDocMatrix=[]
        for feature in trainFeatureNames:
            if feature not in sampleFeatureNames:
                modifiedTermDocMatrix.append(0)
            else:
                featureIndex=sampleFeatureNames.index(feature)
                modifiedTermDocMatrix.append(termDocMatrix[featureIndex])
        termDocMatrix=np.array(modifiedTermDocMatrix)

        return termDocMatrix
        # termDocMatrix is a scipy sparse matrix

class Recommend:

    # here both featureMatrix is a scipy sparse matrix and the labels is simply a numpy lists
    def __init__(self, featureMatrix, labels):
        self.featureMatrix=featureMatrix
        self.labels=labels
        self.logr=None

    def trainModel(self):
        """
        computer the logistic regression model
        """
        lg=linear_model.LogisticRegression()

        """
        the fit function of sklearn.linear_model.LogisticRegression has 2 parameters. The first one is an array-like/sparse matrix of all the features of the training data. The second parameter is an array-like (basically a list) of all the labels
        """
        # fitting a curve y=f(x) where Y is the set of labels and X is the set of features
        self.logr=lg.fit(self.featureMatrix,self.labels)

    def predict(self, sample):
        """
        predicts for a sample X
        """
        return self.logr.predict(sample)

def main():

    allUsers=AllUsers()
    allUsers.getAllUserDetails()
    print ("all user details cached...")
    while True:
        print ("Enter a user id to recommend to/ -1 to quit:")
        userid=int(input())
        if user==-1:
            return -1
        user=User(allUsers.users, allUsers.movieID, allUsers.ratings)
        print ("getting user details...")
        user.getUserDetails(userid)
        document=Document(user.userRatedMovies)
        print ("making documents...")
        document.makeDocuments()
        tfvectorizer=TfVectorizer(document.documents, user.userRatings)
        print ("vectorizing...")
        tfvectorizer.vectorize()
        print ("training model...")
        recommend=Recommend(tfvectorizer.termDocMatrix,tfvectorizer.labels)
        print ("model trained...")

if __name__=="__main__":
    main()


import sys
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
paper_rc = {'lines.linewidth': 10}
sns.set_context("paper", rc = paper_rc,font_scale=5)

### USAGE ###
# python organize_and_filter_genes.py output_folder filtered_insert_ratio_file.poolcounts
#adds rel_loc
#splits tech reps and converts poolcount file formatted for this into .fastq_pooled_read_clean for downstream rh-seq pipeline

##PARAMETERS##
filename=sys.argv[1]
#filename='YBR061C_poolCount_cvfilt3pt3.txt'
maxCount=20000000
figname='poolCount_Hist_maxCt'+str(maxCount)+'.png'

##FUNCTIONS##

def parse_poolcount(filename, sep='\t',maxCount=1000):
    '''parses the poolcount, looks for the maximum value'''
    scer_ins=[]
    spar_ins=[]
    f=open(filename)
    next(f)
    for line in f:
        row_data=line.strip().split(sep)
        allele=row_data[1][:2]
        brs=list(map(float, row_data[8:]))
        #print(brs)
        n=np.max(brs)
        if n<=maxCount:
            if allele=='sc':
                scer_ins.append(n)
            elif allele=='sp':
                spar_ins.append(n)
    f.close()
    #print(scer_ins,spar_ins)
    return scer_ins,spar_ins

def PlotHist(figName,scer_ins,spar_ins):
   # print(spar_ins)

    fig=plt.figure(figsize=(20,15))

    sns.kdeplot(spar_ins, bw='silverman', kernel='gau',label= 'Insert in paradoxus',color='#08A5CD')
    sns.kdeplot(scer_ins, bw='silverman', kernel='gau',label= 'Insert in cerevisiae',color='#F1B629')

    #plt.title(geneName,fontsize=80,loc='center')
    plt.ylabel('Density of Inserts', fontsize=80)
    plt.xlabel('PoolCount Max Count for Ins', fontsize=80)
    plt.rc('xtick',labelsize=80)
    plt.rc('ytick',labelsize=80)
    #plt.xscale('log')

    #plt.vlines(0,0,1,colors='grey',linestyles='dashed',linewidth=5)

    #plt.xlim(-5,5)
    plt.savefig(figName, bbox_inches='tight', format='png')
    print("done plotting histogram: "+figName)

##START
if __name__ == '__main__':
    print('...parsing poolcount...')
    scer_ins,spar_ins=parse_poolcount(filename,maxCount=maxCount)
    print('...plotting...')
    PlotHist(figname,scer_ins,spar_ins)


import requests

from dal.biostudies.biostudies_transaction import retrieve_gxa_studies, insert_gxa_link
from settings import *

__author__ = 'Ahmed G. Ali'

def get_atlas_accessions():
    r = requests.get(ATLAS_API).json()
    # print(r.text)
    atlas_accs = [i.get('experimentAccession', None) for i in r.get('experiments', [])]
    r = requests.get(ATLAS_SC_API).json()
    sc_atlas_accs = [i.get('experimentAccession', None) for i in r.get('experiments', [])]
    return atlas_accs, sc_atlas_accs

def construct_result(res):
    experiments = {}
    for record in res:
        acc = record['accNo']
        if acc not in experiments.keys():
            experiments[acc] = {}
        experiments[acc][record['link_id']] = {
            'section_id': record['section_id'],
            'type': record['value']
        }

    return experiments

def main():
    atlas_accs, sc_atlas_accs = get_atlas_accessions()
    experiments = construct_result(retrieve_gxa_studies())

    for k, v in experiments.items():
        if k in atlas_accs:
            link_found, section_id = check_link_exists(v)
            if not link_found:
                insert_gxa_link(acc=k, section_id=section_id, link_type='gxa', dscr='Expression Atlas')
        elif k in sc_atlas_accs:
            link_found, section_id = check_link_exists(v, 'gxa-sc')
            if not link_found:
                insert_gxa_link(acc=k, section_id=section_id, link_type='gxa-sc', dscr='Single Cell Expression Atlas')
        # break

def check_link_exists(v, link_type='gxa'):
    link_found = False
    section_id = None
    for l_id, link in v.items():
        section_id = link['section_id']
        if link['type'] == link_type:
            link_found = True
            break
    return link_found, section_id

if __name__ == '__main__':
    main()


import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
import torchvision.transforms as transforms
from torchvision.datasets import CIFAR10
from torch.utils.data import DataLoader

import math
import statistics

# モデルを用意
class CNN_model(nn.Module):
    def __init__(self):
        super().__init__()
        self.conv1 = nn.Conv2d(3, 10, 3)
        self.conv2 = nn.Conv2d(10, 20, 3)
        self.l1 = nn.Linear(20*6*6, 120)
        self.l2 = nn.Linear(120, 10)
        self.bn1 = nn.BatchNorm2d(10)
        self.bn2 = nn.BatchNorm2d(20)
        self.pool = nn.MaxPool2d(2, 2)

    def forward(self, x):
        # (batch_size, 3, 32, 32) -> (batch_size, 16, 15, 15)
        x = self.conv1(x)
        x = self.bn1(x)
        x = F.leaky_relu(x)
        x = self.pool(x)

        # (batch_size, 16, 15, 15) -> (batch_size, 16, 6, 6)
        x = self.conv2(x)
        x = self.bn2(x)
        x = F.leaky_relu(x)
        x = self.pool(x)

        # (batch_size, 16, 5, 5) -> (batch_size, 10)
        x = x.view(-1, 20*6*6)
        x = self.l1(x)
        x = F.leaky_relu(x)
        x = self.l2(x)
        return x

# モデルのイニシャライズ
model = CNN_model()

# 訓練データとテストデータを用意
train_data = CIFAR10('~/tmp/cifar10',
                  train=True, download=True,
                  transform=transforms.ToTensor())
train_loader = DataLoader(train_data,
                         batch_size=16,
                         shuffle=True)
test_data = CIFAR10('~/tmp/cifar10',
                 train=False, download=True,
                 transform=transforms.ToTensor())
test_loader = DataLoader(test_data,
                         batch_size=16,
                         shuffle=False)

# GPUの設定
device = 'cuda' if torch.cuda.is_available() else 'cpu'
model = model.to(device)

# Lossの計算関数を設定
criterion = nn.CrossEntropyLoss()

# 値の更新関数を設定
optimizer = optim.Adam(model.parameters(), lr=0.01)

# test時に実行される関数
def test():
    correct = 0
    total = 0
    for inputs, labels in test_loader:
        inputs, labels = inputs.to(device), labels.to(device)
        outputs = model(inputs)
        _, predicted = torch.max(outputs.data, 1)
        total += labels.size(0)
        correct += (predicted == labels).sum()
    # print(outputs, predicted, labels)
    print('Accuracy {} / {} = {}\n'
         .format(correct, total, float(correct) / total))

# 以下を実行しtrainingする
num_epochs = 10
for epoch in range(num_epochs):
    running_loss = []
    for i, (inputs, labels) in enumerate(train_loader):
        inputs, labels = inputs.to(device), labels.to(device)

        # 勾配情報をリセット
        optimizer.zero_grad()

        # 順伝播
        outputs = model(inputs)

        # コスト関数を使ってロスを計算する
        loss = criterion(outputs, labels)

        # 逆伝播
        loss.backward()

        # パラメータの更新
        optimizer.step()

        # ロスを保存
        running_loss.append(loss.item())

        # 中間結果を表示
        cut_number = 200
        if i % cut_number == 0:
            print('{} {} loss: {}'
            .format(epoch + 1, i, statistics.mean(running_loss)))
            running_loss = []
    test()

print('Finished Training')