# Copyright 2018 The Simons Foundation, Inc. - All Rights Reserved.

# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at

#    http://www.apache.org/licenses/LICENSE-2.0

# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import numpy as np
import math as m

def load(N,path_to_samples,path_to_bases):
    tsamples = np.loadtxt(path_to_samples)
    fin_bases = open(path_to_bases,'r')
    lines = fin_bases.readlines()
    bases = []
    for b in lines:
        basis = ""
        for j in range(N):
            basis+=b[j]
        bases.append(basis)
    index_list = sorted(range(len(bases)), key=lambda k: bases[k])
    bases.sort()

    training_samples = []
    training_bases = []
    for i in range(len(tsamples)):
        training_samples.append(tsamples[index_list[i]].tolist())

    U_X = 1./(m.sqrt(2))*np.asarray([[1.,1.],[1.,-1.]])
    U_Y = 1./(m.sqrt(2))*np.asarray([[1.,-1j],[1.,1j]])
    U= []
    sites = []

    tmp = 'void'
    b_index = -1
    for b in bases:
        if (b!=tmp):
            tmp = b
            sub_sites = []
            trivial = True
            for j in range(N):
                if (tmp[j] != 'Z'):
                    trivial=False
                    sub_sites.append(j)
                    if (tmp[j] == 'X'):
                        U.append(U_X.tolist())
                    if (tmp[j] == 'Y'):
                        U.append(U_Y.tolist())
            if trivial is True:
                U.append(np.eye(2).tolist())
            sites.append(sub_sites)
            b_index+=1
        training_bases.append(b_index)
    return U,sites,training_samples,training_bases


__author__ = 'wojtekniedzwiecki'

import unittest
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select
#from __builtin__ import classmethod

class HomePageTest(unittest.TestCase):

    @classmethod
    def setUp(cls):
        # create a new Firefox session """
        cls.driver = webdriver.Firefox()
        cls.driver.implicitly_wait(30)
        cls.driver.maximize_window()

        # navigate to the application home page """
        cls.driver.get("http://demo.magentocommerce.com/")

    def test_language_options(self):
        # list of expected values in Language dropdown
        exp_options = ["ENGLISH", "FRENCH", "GERMAN"]

        # empty list for capturing actual options displayed # in the dropdown
        act_options = []

        # get the Your language dropdown as instance of Select class
        select_language = \
            Select(self.driver.find_element_by_id("select-language"))

        # check number of options in dropdown
        self.assertEqual(3, len(select_language.options))

        # get options in a list
        for option in select_language.options:
            act_options.append(option.text)

        # check expected options list with actual options list
        self.assertListEqual(exp_options, act_options)

        # check default selected option is English
        self.assertEqual("ENGLISH", select_language.first_selected_option.text)

        # select an option using select_by_visible text
        select_language.select_by_visible_text("German")

        # check store is now German
        self.assertTrue("store=german&___from_store" in self.driver.current_url)

        # changing language will refresh the page,
        # we need to get find language dropdown once again
        select_language = \
            Select(self.driver.find_element_by_id("select-language"))
        select_language.select_by_index(0)

    @classmethod
    def tearDown(cls):
        # close the browser window
        cls.driver.quit()

if __name__ == '__main__':
    unittest.main(verbosity=2)


"""Logging module"""

import logging
import logger_constants as lc

from kafka_handler import KafkaHandler

# Our main logging class
class NewLogger(object):

    def __init__(self, nameSpace=None, handler=None, handlerArgs=None, logLevel=None):

        # ------------------------------
        # Sanity check the provided args

        if nameSpace == None:
            raise ValueError("Logger must be initialsed with a namespace keyword argument.")

        if handler not in lc.LOGGERS:
            raise ValueError("Logger must be initialsed with a valid logger choice. Got '{}' must be one of:".format(handler) + ",".join(lc.LOGGERS))

        if logLevel not in lc.LOG_LEVELS and logLevel != None:
            raise ValueError("Logger must be initialsed with a valid log level. Got '{}' must be one of".format() + ",".join(lc.LOG_LEVELS))

        if logLevel == None:
            logLevel = lc.DEBUG

        # Convert string log level to the appropriate int
        logLevel = lc.LOG_LEVEL_LOOKUP[logLevel]

        # ----------------------------
        # Initialise the chosen logger

        if handler == lc.KAFKA:
            self.logger = getKafkaHandler(nameSpace, logLevel, handlerArgs)

    # The writeLog mehod is intended to be overwritten to allow logging to a specification.
    def writeLog(self, record):
        return record

    # # Create a debug level log
    def debug(self, record):
        record = self.writeLog(record)
        self.logger.debug(record)

    # Create a warning level log
    def warning(self, record):
        record = self.writeLog(record)
        self.logger.warning(record)

    # Create a error level log
    def error(self, record):
        record = self.writeLog(record)
        self.logger.error(record)

    # Create a ingo level log
    def info(self, record):
        record = self.writeLog(record)
        self.logger.error(record)

    def close(self):
        self.logger.handlers[0].close()

# -----------------------------------------------
# -----------------------------------------------
# Each supported handler follows in its own block

# Kafka
def getKafkaHandler(nameSpace, logLevel, handlerArgs):

    if "kafka_topic" not in handlerArgs.keys():
        raise ValueError("To use a kafka logger, you need to provide a provide a handlerArgs dict which incudes a value for kafka_topic.")

    if "kafka_hostlist" not in handlerArgs.keys():
        raise ValueError("To use a kafka logger, you need to provide a handlerArgs dict which incudes a value for kafka_address.")

    logger = logging.getLogger(nameSpace)
    logger.setLevel(logLevel)

    kh = KafkaHandler(topic=handlerArgs["kafka_topic"], hostlist=handlerArgs["kafka_hostlist"])
    logger.addHandler(kh)

    return logger


"""Scrape the total homicide count from the Philadelphia Police Department's
Crime Stats website."""

from dataclasses import dataclass

import pandas as pd
import requests
from bs4 import BeautifulSoup
from cached_property import cached_property
from loguru import logger

from . import DATA_DIR

@dataclass
class PPDHomicideTotal:
    """Total number of homicides scraped from the Philadelphia Police
    Department's website.

    This provides:
        - Annual totals since 2007 for past years.
        - Year-to-date homicide total for the current year.

    Source
    ------
    https://www.phillypolice.com/crime-maps-stats/
    """

    debug: bool = False

    URL = "https://www.phillypolice.com/crime-maps-stats/"

    def __post_init__(self):
        self.soup = BeautifulSoup(requests.get(self.URL).content, "html.parser")

    @cached_property
    def years(self):
        """The years available on the page. Starts with 2007."""

        return [
            int(td.text)
            for td in self.soup.select("#homicide-stats")[0]
            .find("tr")
            .find_all("th")[1:]
        ]

    @cached_property
    def as_of_date(self):
        """The current "as of" date on the page."""

        date = (
            self.soup.select("#homicide-stats")[0]
            .select("tbody")[0]
            .select_one("td")
            .text.split("\n")[0]
        )
        return pd.to_datetime(date + " 11:59:00")

    @cached_property
    def annual_totals(self):
        """The annual totals for homicides in Philadelphia."""

        # This is for historic data only (doesn't include current year)
        annual_totals = [
            int(td.text)
            for td in self.soup.select("#homicide-stats")[1].find_all("td")[1:]
        ]

        if len(annual_totals) != len(self.years[1:]):
            raise ValueError(
                "Length mismatch between parsed years and annual homicide totals"
            )

        return pd.DataFrame(
            {"year": self.years[1:], "annual": annual_totals}
        ).sort_values("year", ascending=False)

    @cached_property
    def ytd_totals(self):
        """The year-to-date totals for homicides in Philadelphia."""

        # Scrape the table
        table = self.soup.select("#homicide-stats")[0]
        ytd_totals = [table.select("tbody")[0].select(".homicides-count")[0].text]
        ytd_totals += [td.text for td in table.select("tbody")[0].find_all("td")[2:-1]]
        ytd_totals = list(map(int, ytd_totals))

        if len(ytd_totals) != len(self.years):
            raise ValueError("Length mismatch between parsed years and homicides")

        # Return ytd totals, sorted in ascending order
        out = pd.DataFrame({"year": self.years, "ytd": ytd_totals})
        return out.sort_values("year", ascending=False)

    @property
    def path(self):
        return DATA_DIR / "raw" / "homicide_totals_daily.csv"

    def get(self):
        """Get the shooting victims data, either loading
        the currently downloaded version or a fresh copy."""

        # Load the database of daily totals
        df = pd.read_csv(self.path, parse_dates=[0])

        # Make sure it's in ascending order by date
        return df.sort_values("date", ascending=True)

    def update(self):
        """Update the local data via scraping the PPD website."""

        # Load the database
        database = self.get()

        # Latest database date
        latest_database_date = database.iloc[-1]["date"]

        # Update if we need to
        if latest_database_date < self.as_of_date:

            if self.debug:
                logger.debug("Parsing PPD website to update YTD homicides")

            # Merge annual totals (historic) and YTD (current year)
            data = pd.merge(self.annual_totals, self.ytd_totals, on="year", how="outer")

            # Save it
            path = DATA_DIR / "processed" / "homicide_totals.json"
            data.set_index("year").to_json(path, orient="index")

            # Add new row to database
            YTD = self.ytd_totals.iloc[0]["ytd"]
            database.loc[len(database)] = [self.as_of_date, YTD]

            # Save it
            if self.debug:
                logger.debug("Updating PPD homicides data file")
            database.to_csv(self.path, index=False)


'''
Author: Christian Duncan
Edited By: Alex Hutman, David Lepore, Stephen Kern
Date: Spring 2019
Course: CSC350 (Intelligent Systems)

This sample code shows how one can read in our JSON image files in Python3.  It reads in the file and then outputs the two-dimensional array.
It applies a simple threshold test - if value is > threshold then it outputs a '.' otherwise it outputs an 'X'.  More refinement is probably better.
But this is for displaying images not processing or recognizing images.
'''

from os import listdir
from os.path import isfile, join
import re
from keras.models import Sequential
from keras.layers import Dense, Activation
import numpy as np
import sys
import random
from DigitClassifiers import DigitClassifiers

"""
Prints out the given "image" (a 2-dimensional array).
This just replaces any values greater than a threshold with . and otherwise with an X.
"""
def print_image(img, threshold):
	for row in img:
		for pixel in row:
			print('.' if pixel > threshold else 'X', end='')
		print()  # Newline at end of the row

def kCross(k, data):
	num = round(len(data)/k)
	temp_data = data
	groups = []
	for i in range(k-1):
		train = random.sample(temp_data,num)
		temp_data = list(set(temp_data)-set(train))
		groups.append(train)
	groups.append(temp_data)
	return groups

"""
Main entry point.  Assumes all the arguments passed to it are file names.
For each argument, reads in the file and the prints it out.
"""
def main():
	base_path = "C:/DigitProject/DigitDetector/"
	competition = False

	mypath = base_path + "all_digits/"
	files = [f for f in listdir(mypath) if isfile(join(mypath, f))]
	x = [re.search("^input_[0-9]+_[0-9]+_[0-9]+\.json$", i) for i in files]
	y = [i.group(0) for i in x if i]

	if not competition:
		partitions = kCross(10, y)
		DigitClassifiers(partitions, base_path, 'LDA', competition)
	else:
		DigitClassifiers(y, base_path, 'svm', competition)

# This is just used to trigger the Python to run the main() method.  The if statement is used so that if this code
# were imported as a module then everything would load but main() would not be executed.
if __name__ == "__main__":
	main()


#!/usr/bin/env python3

from fileinput import filename
from os.path import exists
import json

from pprint import pprint
from tqdm import tqdm
from Bio import SeqIO
from Bio.Seq import Seq
from Bio.SeqRecord import SeqRecord
import intervals as I

INPUT_FOLDER = 'input_06_07'
TRANSCRIPT_FILENAME = f'{INPUT_FOLDER}/transcript.fasta'
BLAST_FILENAME = f'{INPUT_FOLDER}/blast_to_genome.tsv'

GENOME_FILENAME = 'genome_good.fasta'
ANNOTATION_FILENAME = 'annotation.gff'

# Output
OUTPUT_FOLDER = 'output_06_07_full'
FASTA_FOLDER = f'{OUTPUT_FOLDER}/fasta'
INTERSECTED_TRANSCRIPTS_FILENAME = f'{OUTPUT_FOLDER}/intersected_transcripts.json'
UTR_STATS_FILENAME = f'{OUTPUT_FOLDER}/utr_statistics.json'
ALL_TRANSCRIPTOME_UTRS_FILENAME = f'{OUTPUT_FOLDER}/all_transcriptome_UTRs.fasta'

NO_HIT_DESC = '***no hit found***'
CATEGORIES = {'NO_HITS': 1, 'FULL': 3, 'PARTIAL': 2}
STOP = 'TAA'

def hamming_distance(s1, s2):
    if len(s1) != len(s2):
        raise ValueError("Strand lengths are not equal!")
    return sum(ch1 != ch2 for ch1, ch2 in zip(s1, s2))

def parse_blast_file(path):
    int_fields = ['qlen', 'alen', 'qstart', 'qend', 'sstart', 'send']
    float_fields = ['pident', 'alen_qlen']
    result = {}
    headers = []
    with open(BLAST_FILENAME) as f:
        for i, line in enumerate(f):
            line = line.strip()

            if line == '':
                continue

            if i == 0:
                headers = line.split('\t')
                continue

            splitted = line.split('\t')
            seq_id = splitted[0].split(' ')[0]

            if seq_id not in result:
                result[seq_id] = None

            if NO_HIT_DESC not in line:
                result[seq_id] = dict([[headers[i], s] for i, s in enumerate(splitted)])

                for header, val in result[seq_id].items():
                    if header in int_fields:
                        result[seq_id][header] = int(val)
                    elif header in float_fields:
                        result[seq_id][header] = float(val)

                    if header in ['qframe', 'sframe']:
                        if val == '1':
                            result[seq_id][header] = '+'
                        elif val == '-1':
                            result[seq_id][header] = '-'
                        else:
                            raise(
                                Exception(f'Blast frame can be only 1/-1 (val {val}), hit {line}'))

    return result

def contains(hit_coords, stop_coords):
    hit_coords = sorted(hit_coords)
    stop_coords = sorted(stop_coords)
    return hit_coords[0] <= stop_coords[0] and hit_coords[1] >= stop_coords[1]

REVERSE_TABLE = {
        ("+", "+", "+"): { "reverse_transcript": False, "reverse_genome": False,
                              "sstart": "sstart", "send": "send", "qstart": "qstart", "qend": "qend"},
        ("-", "+", "+"): { "reverse_transcript": True, "reverse_genome": False,
                              "sstart": "sstart", "send": "send", "qstart": "qend", "qend": "qstart"},
        ("+", "-", "+"): { "reverse_transcript": True, "reverse_genome": False,
                              "sstart": "sstart", "send": "send", "qstart": "qend", "qend": "qstart"},
        ("+", "+", "-"): { "reverse_transcript": True, "reverse_genome": True,
                              "sstart": "send", "send": "sstart", "qstart": "qend", "qend": "qstart"},
        ("-", "+", "-"): { "reverse_transcript": False, "reverse_genome": True,
                              "sstart": "send", "send": "sstart", "qstart": "qstart", "qend": "qend"},
        ("+", "-", "-"): { "reverse_transcript": False, "reverse_genome": True,
                              "sstart": "send", "send": "sstart", "qstart": "qstart", "qend": "qend"},

        ("-", "-", "+"): { "reverse_transcript": False, "reverse_genome": False,
                              "sstart": "sstart", "send": "send", "qstart": "qstart", "qend": "qend"},
        ("-", "-", "-"): { "reverse_transcript": True, "reverse_genome": True,
                              "sstart": "send", "send": "sstart", "qstart": "qend", "qend": "qstart"}
    }

def which_reverse_needed(tran_direction, genome_direction, gene_direction):
    key = (tran_direction == "+", genome_direction == "+", gene_direction == "+")
    return REVERSE_TABLE[key]

def main():
    total_counts = {
        'total_transcript': 0,
        'with_hits_on_genome': 0,
        'PARTIAL':{
            'with_more_than_one_gene': 0,
            'without_genes': 0,
            'with_one_gene': 0,

            'starts_with_STOP': 0,
            'starts_not_with_STOP': 0,

            'where_UTR_EQUAL': 0,
            'where_UTR_NOT_EQUAL': 0
        },
        'without_hits_on_genome': 0
    }
    transcript = {}
    blast = {}

    for seq in SeqIO.parse(TRANSCRIPT_FILENAME, 'fasta'):
        transcript[seq.id] = seq

    blast = parse_blast_file(BLAST_FILENAME)

    stats = {CATEGORIES['NO_HITS']: [],
             CATEGORIES['FULL']: [],
             CATEGORIES['PARTIAL']: []}

    for seq_id, rec in transcript.items():
        if seq_id not in blast:
            raise(Exception(f"Something wrong, {seq_id} not in hit file"))

        total_counts['total_transcript'] += 1

        hit = blast[seq_id]

        # if no hits
        if not hit:
            total_counts['without_hits_on_genome'] += 1
            stats[CATEGORIES['NO_HITS']].append(seq_id)
            continue
        else:
            total_counts['with_hits_on_genome'] += 1

        # if full coverage

        if hit['pident'] == 100.0 and hit['qlen'] == hit['alen']:
            stats[CATEGORIES['FULL']].append(seq_id)
            continue

        stats[CATEGORIES['PARTIAL']].append(seq_id)

    for name, cat_id in CATEGORIES.items():
        print(f"{name}: {len(stats[cat_id])}")
    print()

    # getting statistics of transcripts with partial hits

    genome = SeqIO.to_dict(SeqIO.parse(GENOME_FILENAME, "fasta"))
    annotation_by_contig_id = dict([[e, []] for e in genome.keys()])

    with open(ANNOTATION_FILENAME) as f:
        for line in f:
            line = line.strip()
            if line == '':
                continue

            splitted = line.split('\t')

            record = {}

            record['contig_id'] = splitted[0]
            record['gene_id'] = splitted[-1].split(';')[0].split('=')[-1]
            record['left'] = int(splitted[3])
            record['right'] = int(splitted[4])
            record['direction'] = splitted[6]

            if record['direction'] == '-':
                interval = I.closed(
                    record['left']-3, record['left']-1)
            elif record['direction'] == '+':
                interval = I.closed(
                    record['right']+1, record['right']+3)
            else:
                raise(Exception('Wrong annotation line'))

            record['gene_stop_crds'] = [interval.lower, interval.upper]

            # fucking continue man
            if record['contig_id'] == 'Ctg28_length_81690':
                continue

            annotation_by_contig_id[record['contig_id']].append(record)

    transcripts_with_stops = []
    if exists(INTERSECTED_TRANSCRIPTS_FILENAME):
        with open(INTERSECTED_TRANSCRIPTS_FILENAME) as f:
            transcripts_with_stops = json.load(f)
        total_counts['PARTIAL']['without_genes'] = len(stats[CATEGORIES['PARTIAL']]) - len(transcripts_with_stops)
    else:
        for seq_id in tqdm(stats[CATEGORIES['FULL']]):
            hit = blast[seq_id]
            # hit_interval = I.closed(*sorted([hit['sstart'], hit['send']]))
            hit_coords = [hit['sstart'], hit['send']]

            intersecting_stop_genes = []

            for record in annotation_by_contig_id[hit['sseqid']]:
                stop_interval = record['gene_stop_crds']

                if contains(hit_coords, stop_interval):
                    intersecting_stop_genes.append(record)

            if len(intersecting_stop_genes) > 0:
                data = {}
                data['transcript_id'] = seq_id
                data['hit'] = hit
                data['genes'] = intersecting_stop_genes

                transcripts_with_stops.append(data)
            else:
                total_counts['PARTIAL']['without_genes'] += 1

            with open(INTERSECTED_TRANSCRIPTS_FILENAME, 'w') as out_f:
                json.dump(transcripts_with_stops, out_f, sort_keys=False, indent=4)

    # extracting sequences from transcript & gene for each hit
    utr_stats = {'NOT_EQUAL': {'cnt': 0, 'records': []},
                 'EQUAL': {'cnt': 0, 'records': []}}

    reverse_cases_stats = dict([[e, {'trn_with_STOP': 0, 'trn_without_STOP': 0,
    'gnm_with_STOP': 0, 'gnm_without_STOP': 0 }] for e in REVERSE_TABLE])

    cnt = 0
    for transcript_object in transcripts_with_stops:
        if len(transcript_object['genes']) > 1:
            # TODO: remove it, take the last stop
            total_counts['PARTIAL']['with_more_than_one_gene'] += 1
            continue
        total_counts['PARTIAL']['with_one_gene'] += 1

        stats_record = {}

        main_gene = transcript_object['genes'][0]
        hit = transcript_object['hit']

        gene_direction = main_gene['direction']

        genome_seq = genome[hit['sseqid']].seq
        transcript_seq = transcript[transcript_object['transcript_id']].seq

        # MAGIC here
        rule_key = (hit['qframe'], hit['sframe'], gene_direction)
        rules = REVERSE_TABLE[rule_key]

        sstart = hit[rules['sstart']] - 1
        send = hit[rules['send']] - 1
        qstart = hit[rules['qstart']] - 1
        qend = hit[rules['qend']] - 1

        if rules['reverse_transcript']:
            transcript_seq = transcript_seq.reverse_complement()
            qstart = len(transcript_seq) - qstart - 1
            qend = len(transcript_seq) - qend - 1

        gene_stop = sorted(main_gene['gene_stop_crds'])[0] - 1

        if rules['reverse_genome']:
            genome_seq = genome_seq.reverse_complement()
            gene_stop = len(genome_seq) - gene_stop - 3
            sstart = len(genome_seq) - sstart - 1
            send = len(genome_seq) - send - 1

        transcript_utr_start = qend - (send - gene_stop)
        transcript_utr = transcript_seq[transcript_utr_start:]
        transcript_utr_end = transcript_utr_start + len(transcript_utr)

        genome_utr_start = gene_stop
        genome_utr_end = gene_stop + len(transcript_utr)
        genome_utr = genome_seq[genome_utr_start:genome_utr_end]

        stats_record['genome_seq_id'] = main_gene['contig_id']
        stats_record['genome_utr_start'] = genome_utr_start
        stats_record['genome_utr_end'] = genome_utr_end
        stats_record['transcript_seq_id'] = transcript_object['transcript_id']
        stats_record['transcript_utr_start'] = transcript_utr_start
        stats_record['transcript_utr_end'] = transcript_utr_end
        stats_record['annotated_gene'] = main_gene
        stats_record['transcript_direction'] = hit['qframe']
        stats_record['genome_direction'] = hit['sframe']
        stats_record['gene_direction'] = gene_direction
        stats_record['gen_utr'] = str(genome_utr)
        stats_record['trn_utr'] = str(transcript_utr)

        if len(genome_utr) == len(transcript_utr):
            stats_record['similarity'] = (
                1.0-(hamming_distance(str(genome_utr), str(transcript_utr))/len(genome_utr)))*100
            stats_record['similarity'] = round(
                stats_record['similarity'], 1)
        else:
            stats_record['similarity'] = 0

        if genome_utr.startswith(STOP) and transcript_utr.startswith(STOP):
            total_counts['PARTIAL']['starts_with_STOP'] += 1
        else:
            total_counts['PARTIAL']['starts_not_with_STOP'] += 1

        if genome_utr.startswith(STOP):
            reverse_cases_stats[rule_key]['gnm_with_STOP'] += 1
        else:
            reverse_cases_stats[rule_key]['gnm_without_STOP'] += 1

        if transcript_utr.startswith(STOP):
            reverse_cases_stats[rule_key]['trn_with_STOP'] += 1
        else:
            reverse_cases_stats[rule_key]['trn_without_STOP'] += 1

        if genome_utr == transcript_utr:
            total_counts['PARTIAL']['where_UTR_EQUAL'] += 1
            utr_stats['EQUAL']['records'].append(stats_record)
            utr_stats['EQUAL']['cnt'] += 1
        else:
            total_counts['PARTIAL']['where_UTR_NOT_EQUAL'] += 1
            utr_stats['NOT_EQUAL']['records'].append(stats_record)
            utr_stats['NOT_EQUAL']['cnt'] += 1

    # sorting stats by similarity
    sort_approach = lambda x: x['similarity']

    for key in ['NOT_EQUAL', 'EQUAL']:
        utr_stats[key]['records'] = sorted(
            utr_stats[key]['records'], key=sort_approach)

    # extract data from the code of transcript
    # more_than_one = [e for e in transcripts_with_stops if len(e['genes']) == 5]
    with open(UTR_STATS_FILENAME, 'w') as out_f:
        json.dump(utr_stats, out_f, sort_keys=False, indent=4)

    with open(UTR_STATS_FILENAME+'.csv', 'w') as f:
        delimiter = ','
        header = ["genome_seq_id",
                  "genome_utr_start",
                  "genome_utr_end",
                  "transcript_seq_id",
                  "transcript_utr_start",
                  "transcript_utr_end",
                  "transcript_direction",
                  "genome_direction",
                  "gene_direction",
                  "similarity",
                  "gen_utr",
                  "trn_utr"]
        f.write(f'{delimiter.join(header)}\n')

        for record in utr_stats['NOT_EQUAL']['records']:
            vals = [str(record[h]) for h in header]
            f.write(f'{delimiter.join(vals)}\n')

    transcript_utrs = []

    for record in utr_stats['NOT_EQUAL']['records']:
        similarity_str = str(record["similarity"]).replace(".", "_")
        filename = f'{ similarity_str }_{record["transcript_seq_id"]}'
        with open(f"{FASTA_FOLDER}/{filename}.fasta", 'w') as out_f:
            out_f.write(f">GENOME_UTR\n")
            out_f.write(f"{ record['gen_utr'] }\n")
            out_f.write(f">TRANSCRIPTOME_UTR\n")
            out_f.write(f"{ record['trn_utr'] }\n")

        if record["similarity"] and record["similarity"] < 100:
            rec = SeqRecord(Seq(record['trn_utr']), filename)
            rec.description = ''
            transcript_utrs.append(rec)

    SeqIO.write(transcript_utrs, ALL_TRANSCRIPTOME_UTRS_FILENAME, "fasta")

    pprint(total_counts, sort_dicts=False)
    print()
    pprint(reverse_cases_stats, sort_dicts=False)
    print("Done")

if __name__ == '__main__':
    main()

    # TODO
    # total_transcript: bla
    #   with_hits_on_genome: bla
    #       with_genes: bla
    #           with_STOP_codons: bla
    #               where_UTR_EQUAL: bla
    #               where_UTR_NOT_EQUAL: bla
    #           without_STOP_codons: bla
    #       without_genes: bla
    #   without_hit_on_genome: bla


# -*- coding: utf-8 -*-
"""
.. invisible:
     _   _ _____ _     _____ _____
    | | | |  ___| |   |  ___/  ___|
    | | | | |__ | |   | |__ \ `--.
    | | | |  __|| |   |  __| `--. \
    \ \_/ / |___| |___| |___/\__/ /
     \___/\____/\_____|____/\____/

Created on Nov 7, 2013

Unit test for convolutional layer back propagation.

███████████████████████████████████████████████████████████████████████████████

Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements.  See the NOTICE file
distributed with this work for additional information
regarding copyright ownership.  The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License.  You may obtain a copy of the License at

  http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied.  See the License for the
specific language governing permissions and limitations
under the License.

███████████████████████████████████████████████████████████████████████████████
"""

import numpy
from veles.backends import NumpyDevice

from veles.config import root
from veles.dummy import DummyUnit
from veles.memory import assert_addr, Array
import veles.opencl_types as opencl_types
from veles.tests import AcceleratedTest, assign_backend
from veles.znicz.gd_conv import GradientDescentConv, GDRELUConv, \
    GDStrictRELUConv, GDTanhConv, GDSigmoidConv
import veles.znicz.conv as conv
import veles.prng as prng
from veles.znicz.tests.unit.gd_numdiff import GDNumDiff
from veles.znicz.tests.unit.test_gd import PatchedGradientDescentBase

class PatchedGradientDescentConv(GradientDescentConv,
                                 PatchedGradientDescentBase):
    pass

class PatchedGDRELUConv(GDRELUConv, PatchedGradientDescentBase):
    pass

class PatchedGDStrictRELUConv(GDStrictRELUConv, PatchedGradientDescentBase):
    pass

class PatchedGDTanhConv(GDTanhConv, PatchedGradientDescentBase):
    pass

class PatchedGDSigmoidConv(GDSigmoidConv, PatchedGradientDescentBase):
    pass

class TestGDConv(AcceleratedTest, GDNumDiff):
    ABSTRACT = True

    def setUp(self):
        super(TestGDConv, self).setUp()
        self.precision_threshold = {
            numpy.float64: 1.0e-8,
            numpy.float32: 1.0e-4,
            numpy.float16: 1.0e-2}[self.dtype]

    def test_err_h_gpu(self):
        self._test_err_h(self.device)

    def test_err_h_cpu(self):
        self._test_err_h(NumpyDevice())

    def _test_err_h(self, device):
        self.info("Will test convolutional layer back propagation")

        dtype = opencl_types.dtypes[root.common.engine.precision_type]
        inp = numpy.array([[[-1, 0, 2, 0, 3],
                            [0, 1, -2, 1, 2],
                            [2, 0, 1, 1, 0],
                            [-1, 1, 1, 0, 2],
                            [1, 0, 1, 0, 1]],

                           [[-1, 0, 2, 0, 3],
                            [0, 1, -2, 1, 2],
                            [2, 0, 1, 1, 0],
                            [-1, 1, 1, 0, 2],
                            [1, 0, 1, 0, 1]]], dtype=dtype)
        inp.shape = inp.shape + (1,)

        a = numpy.array([[-1, 0, 2, 0, 1, -2, 2, 0, 1],
                         [0, 2, 0, 1, -2, 1, 0, 1, 1],
                         [2, 0, 3, -2, 1, 2, 1, 1, 0],
                         [0, 1, -2, 2, 0, 1, -1, 1, 1],
                         [1, -2, 1, 0, 1, 1, 1, 1, 0],
                         [-2, 1, 2, 1, 1, 0, 1, 0, 2],
                         [2, 0, 1, -1, 1, 1, 1, 0, 1],
                         [0, 1, 1, 1, 1, 0, 0, 1, 0],
                         [1, 1, 0, 1, 0, 2, 1, 0, 1],

                         [-1, 0, 2, 0, 1, -2, 2, 0, 1],
                         [0, 2, 0, 1, -2, 1, 0, 1, 1],
                         [2, 0, 3, -2, 1, 2, 1, 1, 0],
                         [0, 1, -2, 2, 0, 1, -1, 1, 1],
                         [1, -2, 1, 0, 1, 1, 1, 1, 0],
                         [-2, 1, 2, 1, 1, 0, 1, 0, 2],
                         [2, 0, 1, -1, 1, 1, 1, 0, 1],
                         [0, 1, 1, 1, 1, 0, 0, 1, 0],
                         [1, 1, 0, 1, 0, 2, 1, 0, 1]], dtype=dtype)

        weights = numpy.array([[-1, -1, 4, 1, 8, -1, -1, 3, 2],
                               [2, 1, -1, 3, 0, 1, 4, 1, 3]], dtype=dtype)

        bias = numpy.array([10, -10], dtype=dtype)

        err_output = numpy.array([[[[-1, 3], [8, 2], [0, 1]],
                                   [[4, -1], [-1, 2], [0, 1]],
                                   [[-2, 3], [1, 2], [1, 1]]],

                                  [[[-1, 3], [8, 2], [0, 1]],
                                   [[4, -1], [-1, 2], [0, 1]],
                                   [[-2, 3], [1, 2], [1, 1]]]], dtype=dtype)

        c = PatchedGradientDescentConv(self.parent, gradient_moment=0.9)
        u = DummyUnit(kx=3, ky=3, n_kernels=2,
                      padding=(0, 0, 0, 0), sliding=(1, 1),
                      err_output=Array(err_output.copy()),
                      input=Array(inp.copy()),
                      weights=Array(weights.copy()),
                      bias=Array(bias.copy()),
                      output=Array(err_output.copy()),
                      unpack_size=1, unpack_data=Array())
        c.link_conv_attrs(u)
        c.link_attrs(u, "err_output", "input", "output", "weights", "bias")

        batch_size = c.err_output.shape[0]
        b = c.err_output.mem.reshape(9 * batch_size, 2)
        gradient_weights = numpy.dot(b.transpose(), a)
        weights_derivative = gradient_weights.copy()
        gradient_weights *= -c.learning_rate
        gradient_weights += weights * (-1) * (c.learning_rate *
                                              c.weights_decay)
        weights_new = weights + gradient_weights
        bias_derivative = b.sum(axis=0)
        gradient_bias = bias_derivative * (-c.learning_rate_bias)
        bias_new = bias + gradient_bias

        c.initialize(device=device)
        c.run()
        c.err_input.map_read()
        c.weights.map_read()
        c.bias.map_read()

        t = numpy.array([[[7, 0, -11, 31, -1],
                          [2, 6, 93, -11, 0],
                          [22, 45, 18, 28, 7],
                          [-1, 11, 25, 14, 3],
                          [14, 4, 13, 12, 5]],

                         [[7, 0, -11, 31, -1],
                          [2, 6, 93, -11, 0],
                          [22, 45, 18, 28, 7],
                          [-1, 11, 25, 14, 3],
                          [14, 4, 13, 12, 5]]], dtype=dtype)
        max_diff = numpy.fabs(t.ravel() - c.err_input.mem.ravel()).max()
        self.assertLess(max_diff, self.precision_threshold,
                        "Err_input differs by %.6f" % (max_diff))
        self.info("Err_input Ok")

        max_diff = numpy.fabs(weights_new.ravel() -
                              c.weights.mem.ravel()).max()
        self.assertLess(max_diff, self.precision_threshold,
                        "Weights differ by %.6f" % (max_diff))
        self.info("Weights Ok")

        max_diff = numpy.fabs(bias_new.ravel() - c.bias.mem.ravel()).max()
        self.assertLess(max_diff, self.precision_threshold,
                        "Bias differs by %.6f" % (max_diff))
        self.info("Bias Ok")

        err_input = c.err_input.mem.ravel()
        forward = conv.Conv(self.parent, n_kernels=2, kx=3, ky=3)
        target = self._numdiff_init_forward(forward, inp, weights, bias,
                                            err_output)

        self.numdiff_check_gd(forward, inp, weights, bias, target,
                              err_input, weights_derivative, bias_derivative,
                              self.info, self.assertLess,
                              mean=False)

    def _numdiff_init_forward(self, forward, inp, weights, bias, err_output):
        forward.input = Array()
        forward.input.mem = inp.copy()
        forward.initialize(device=self.device)
        forward.weights.map_invalidate()
        forward.weights.mem[:] = weights[:]
        forward.bias.map_invalidate()
        forward.bias.mem[:] = bias[:]
        forward.run()
        forward.output.map_read()
        target = forward.output.mem.ravel() - err_output.ravel()
        return target

    def test_padding_sliding_gpu(self):
        self._test_padding_sliding(self.device)

    def test_padding_sliding_cpu(self):
        self._test_padding_sliding(NumpyDevice())

    def _test_padding_sliding(self, device):
        self.info("Will test convolutional layer back propagation")

        dtype = opencl_types.dtypes[root.common.engine.precision_type]
        inp = numpy.array([[[1, 2, 3, 2, 1],
                            [0, 1, 2, 1, 0],
                            [0, 1, 0, 1, 0],
                            [2, 0, 1, 0, 2],
                            [1, 0, 1, 0, 1]]], dtype=dtype)
        inp.shape = inp.shape + (1,)

        a = numpy.array([[0, 0, 0, 0, 0, 0, 0, 1, 2],
                         [0, 0, 0, 0, 0, 0, 2, 3, 2],
                         [0, 0, 0, 0, 0, 0, 2, 1, 0],
                         [0, 0, 0, 0, 0, 0, 0, 0, 0],
                         [0, 0, 1, 0, 0, 1, 0, 2, 0],
                         [1, 2, 1, 1, 0, 1, 0, 1, 0],
                         [1, 0, 0, 1, 0, 0, 0, 2, 0],
                         [0, 0, 0, 0, 0, 0, 0, 0, 0],
                         [0, 1, 0, 0, 0, 0, 0, 0, 0],
                         [0, 1, 0, 0, 0, 0, 0, 0, 0],
                         [0, 1, 0, 0, 0, 0, 0, 0, 0],
                         [0, 0, 0, 0, 0, 0, 0, 0, 0]], dtype=dtype)

        weights = numpy.array([[-1, -1, -1, -1, 8, -1, -1, -1, -1],
                               [1.1, 2.1, 3.1, -1.1, -0.5, 1.3, 1.7, -1.4,
                                0.05]], dtype=dtype)

        bias = numpy.array([10, -10], dtype=dtype)

        err_output = numpy.array([[[[-1, 3], [8, 2], [0, 1], [4, -1]],
                                   [[-1, 2], [0, 1], [-2, 3], [1, 2]],
                                   [[1, 1], [1, -2], [0, 5], [2, 3]]]],
                                 dtype=dtype)

        c = PatchedGradientDescentConv(self.parent, gradient_moment=0.9)
        u = DummyUnit(n_kernels=2, kx=3, ky=3,
                      padding=(1, 2, 3, 4), sliding=(2, 3),
                      err_output=Array(err_output.copy()),
                      input=Array(inp.copy()),
                      weights=Array(weights.copy()),
                      bias=Array(bias.copy()),
                      output=Array(err_output.copy()),
                      unpack_size=1, unpack_data=Array())
        c.link_conv_attrs(u)
        c.link_attrs(u, "err_output", "input", "output", "weights", "bias")

        batch_size = c.err_output.shape[0]
        b = c.err_output.mem.reshape(12 * batch_size, 2)
        gradient_weights = numpy.dot(b.transpose(), a)
        weights_derivative = gradient_weights.copy()
        gradient_weights *= -c.learning_rate
        gradient_weights += weights * (-1) * (c.learning_rate *
                                              c.weights_decay)
        weights_new = weights + gradient_weights
        bias_derivative = b.sum(axis=0)
        gradient_bias = bias_derivative * (-c.learning_rate_bias)
        bias_new = bias + gradient_bias

        c.initialize(device=device)
        c.run()
        c.err_input.map_read()
        c.weights.map_read()
        c.bias.map_read()

        t = numpy.array([[[-3.2, -3.45, -10.8, -6.2, -1.4],
                          [5.2, 8.3, 2.1, 8.4, 8.3],
                          [-9, 2.5, -0.5, 0, -17.5],
                          [-1.8, 2.8, -1.4, 7.15, -2.2],
                          [1.1, -1.1, -5.2, -1.7, 10.5]]], dtype=dtype)
        max_diff = numpy.fabs(t.ravel() - c.err_input.mem.ravel()).max()
        self.assertLess(max_diff, self.precision_threshold,
                        "Err_input differs by %.6f\nTarget is:\n%s\nGot:\n%s" %
                        (max_diff, " ".join("%.2f" % x for x in t.ravel()),
                         " ".join("%.2f" % x for x in
                                  c.err_input.mem.ravel())))
        self.info("Err_input Ok")

        max_diff = numpy.fabs(weights_new.ravel() -
                              c.weights.mem.ravel()).max()
        self.assertLess(max_diff, self.precision_threshold,
                        "Weights differ by %.6f" % (max_diff))
        self.info("Weights Ok")

        max_diff = numpy.fabs(bias_new.ravel() - c.bias.mem.ravel()).max()
        self.assertLess(max_diff, self.precision_threshold,
                        "Bias differs by %.6f" % (max_diff))
        self.info("Bias Ok")

        err_input = c.err_input.mem.ravel()
        forward = conv.Conv(self.parent, n_kernels=2, kx=3, ky=3,
                            padding=(1, 2, 3, 4), sliding=(2, 3))
        target = self._numdiff_init_forward(forward, inp, weights, bias,
                                            err_output)

        self.numdiff_check_gd(forward, inp, weights, bias, target,
                              err_input, weights_derivative, bias_derivative,
                              self.info, self.assertLess,
                              mean=False)

    def test_random_numeric_gpu(self):
        self._test_random_numeric(self.device, conv.Conv,
                                  PatchedGradientDescentConv)

    def test_random_numeric_gpu_tanh(self):
        self._test_random_numeric(self.device, conv.ConvTanh,
                                  PatchedGDTanhConv)

    def test_random_numeric_gpu_sigmoid(self):
        self._test_random_numeric(self.device, conv.ConvSigmoid,
                                  PatchedGDSigmoidConv)

    def test_random_numeric_gpu_relu(self):
        self._test_random_numeric(self.device, conv.ConvRELU,
                                  PatchedGDRELUConv)

    def test_random_numeric_cpu(self):
        self._test_random_numeric(NumpyDevice(), conv.Conv,
                                  PatchedGradientDescentConv)

    def test_random_numeric_cpu_tanh(self):
        self._test_random_numeric(NumpyDevice(), conv.ConvTanh,
                                  PatchedGDTanhConv)

    def test_random_numeric_cpu_sigmoid(self):
        self._test_random_numeric(NumpyDevice(), conv.ConvSigmoid,
                                  PatchedGDSigmoidConv)

    def test_random_numeric_cpu_relu(self):
        self._test_random_numeric(NumpyDevice(), conv.ConvRELU,
                                  PatchedGDRELUConv)

    def _test_random_numeric(self, device, Forward, GD):
        self._test_random_numeric_transposed(device, Forward, GD, False)
        self._test_random_numeric_transposed(device, Forward, GD, True)

    def _test_random_numeric_transposed(self, device, Forward, GD,
                                        weights_transposed):
        self.info("Will test convolutional layer forward-backward "
                  "via numeric differentiation (weights_transposed = %s)",
                  str(weights_transposed))

        dtype = opencl_types.dtypes[root.common.engine.precision_type]
        inp = numpy.zeros([2, 5, 5, 3], dtype=dtype)
        prng.get().fill(inp)
        forward = Forward(self.parent, n_kernels=2, kx=3, ky=3,
                          padding=(1, 2, 3, 4), sliding=(2, 3),
                          weights_transposed=weights_transposed)
        sh = list(inp.shape)
        sh[0] <<= 1
        forward.input = Array(numpy.zeros(sh, dtype=dtype))
        forward.input.initialize(device)
        forward.input.map_write()
        forward.input.unit_test_mem = forward.input.mem
        sh[0] >>= 1
        forward.input.mem = forward.input.unit_test_mem[:sh[0]]
        assert_addr(forward.input.mem, forward.input.unit_test_mem)
        forward.input.mem[:] = inp[:]
        forward.input.unit_test_mem[sh[0]:] = numpy.nan
        forward.initialize(device=device)
        forward.run()

        forward.output.map_read()
        target = numpy.zeros_like(forward.output.mem)
        prng.get().fill(target)
        out = forward.output.mem.copy()
        err_output = out - target
        forward.weights.map_read()
        weights = forward.weights.mem.copy()
        forward.bias.map_read()
        bias = forward.bias.mem.copy()

        c = GD(
            self.parent,
            gradient_moment=0, gradient_moment_bias=0,
            learning_rate=-1, weights_decay=0,
            learning_rate_bias=-1, weights_decay_bias=0,
            weights_transposed=weights_transposed)
        c.link_conv_attrs(forward)
        c.link_attrs(forward, "input", "output", "weights", "bias")
        sh = list(err_output.shape)
        sh[0] <<= 1
        c.err_output = Array(numpy.zeros(sh, dtype=dtype))
        c.err_output.initialize(device)
        c.err_output.map_write()
        c.err_output.unit_test_mem = c.err_output.mem
        sh[0] >>= 1
        c.err_output.mem = c.err_output.unit_test_mem[:sh[0]]
        assert_addr(c.err_output.mem, c.err_output.unit_test_mem)
        c.err_output.mem[:] = err_output[:]
        c.err_output.unit_test_mem[sh[0]:] = numpy.nan
        c.initialize(device)
        c.run()
        c.err_input.map_read()
        c.weights.map_read()
        c.bias.map_read()

        nz = numpy.count_nonzero(numpy.isnan(c.err_input.mem))
        self.assertEqual(nz, 0, "NaNs encountered in err_input")

        nz = numpy.count_nonzero(numpy.isnan(c.weights.mem))
        self.assertEqual(nz, 0, "NaNs encountered in weights")

        nz = numpy.count_nonzero(numpy.isnan(c.bias.mem))
        self.assertEqual(nz, 0, "NaNs encountered in bias")

        err_input = c.err_input.mem.ravel().copy()
        weights_derivative = c.weights.mem - weights
        bias_derivative = c.bias.mem - bias

        c.err_input_alpha = 0.21436598
        c.err_input_beta = 0.123456789
        c.weights.map_invalidate()
        c.weights.mem[:] = weights
        c.bias.map_invalidate()
        c.bias.mem[:] = bias
        c.err_output.map_invalidate()
        c.err_output.mem[:] = err_output
        c.run()
        c.err_input.map_read()
        max_diff = numpy.fabs(
            c.err_input.mem.ravel() -
            (err_input * c.err_input_beta +
             err_input * c.err_input_alpha)).max()
        self.assertLess(max_diff, self.precision_threshold,
                        "err_input alpha/beta test failed")

        self.numdiff_check_gd(forward, inp, weights, bias, target,
                              err_input, weights_derivative, bias_derivative,
                              self.info, self.assertLess, mean=False)

@assign_backend("ocl")
class OpenCLTestGDConv(TestGDConv):
    pass

@assign_backend("cuda")
class CUDATestGDConv(TestGDConv):
    pass

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


a=int(input())
d=int(input())
li = []
for b in range(a,d+1):
    flag = True
    if b == 1:
        continue
    if b == 2 :
        li.append(b)
        continue
    for j in range(2,b):
        if b % j == 0:
            flag = False
            break
    if flag:
        li.append(b)
print(sum(li))
print(min(li))


def solution(scores):
    answer = ''
    avg = []
    for i in range(len(scores[0])):
        demo = []
        for j in range(len(scores)):
            demo.append(scores[j][i])
        maxx = max(demo)
        minn = min(demo)
        if demo.count(maxx) == 1:
            if demo.index(maxx) == i:
                demo.remove(maxx)

        if demo.count(minn) == 1:
            if demo.index(minn) == i:
                demo.remove(minn)

        avg.append(sum(demo) / len(demo))

    for i in avg:
        if i >= 90:
            answer += 'A'
        elif i >= 80:
            answer += 'B'
        elif i >= 70:
            answer += 'C'
        elif i >= 50:
            answer += 'D'
        else:
            answer += 'F'

    return answer

11주차 완전탐색/위클리/상호평가/김승욱.py


decipherthis = "MYXQBKDEVKDSYXC"
alphabet= "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
shift = int(input("Shift value, like 16: "))
length = len(decipherthis)
deciphered = ""
for i in range(length):
    num = decipherthis[i]
    lookforletter = alphabet.find(num) #Need to determine where the letter is since we can't just shift it to the location. Needs to match.
    looking = (lookforletter + shift)%26
    deciphered += alphabet[looking]
print("Consider this deciphered: ", deciphered)


from graphs import Graph, Vertex

class DFSGraph(Graph):
    def __init__(self):
        super().__init__()
        self.time = 0

    def dfs(self):
        for aVertex in self:
            aVertex.setColor('white')
            aVertex.setPred(-1)
            self.dfsvisit(aVertex)

    def dfsvisit(self, startVertex):
        print("vertex {}: color {}".format(startVertex, startVertex.getColor()))
        print("setting color gray")
        startVertex.setColor('gray')
        print("vertex {}: color {}".format(startVertex, startVertex.getColor()))
        self.time += 1
        startVertex.setDiscovery(self.time)
        print("setting discovery time {} for vertex, {}".format(startVertex.getDiscovery(),startVertex ))
        for nextVertex in startVertex.getConnections():
            print("connections for vertex {} are {}. ".format(startVertex, startVertex.getConnections()))
            print("     LOOP: vertex {}: color {}".format(nextVertex, nextVertex.getColor()))
            if nextVertex.getColor() == 'white':
                nextVertex.setPred(startVertex)
                print("RECURSION: making recursive call on vertex {}".format(nextVertex))
                self.dfsvisit(nextVertex)
        print("setting color black")
        startVertex.setColor('black')
        print("vertex {}: color {}".format(startVertex, startVertex.getColor()))
        self.time += 1
        startVertex.setFinish(self.time)
        print("setting finish time {} for vertex, {}".format(startVertex.getFinish(), startVertex ))

if __name__ == '__main__':

    g = DFSGraph()
    g.addVertex('a')
    g.addVertex('b')
    g.addVertex('c')
    g.addVertex('d')
    g.addVertex('e')
    g.addVertex('f')

    a = g.getVertex('a')
    b = g.getVertex('b')
    c = g.getVertex('c')
    d = g.getVertex('d')
    e = g.getVertex('e')
    f = g.getVertex('f')

    g.addEdge(a,b)
    g.addEdge(a,d)
    g.addEdge(b,c)
    g.addEdge(b,d)
    g.addEdge(d,e)
    g.addEdge(e,b)
    g.addEdge(e,f)
    g.addEdge(f,c)
    g.dfs()


#the simplest code possible for fizzbuzz (most-possibly) in python
#Author: Rajat Bansal (@rajat_1401)

for num in range(1,101):
    strr= ""
    if(num%3== 0):
        strr+= "Fizz"
    if(num%5== 0):
        strr+= "Buzz"
    if(strr== ""):
        strr= num
    print (strr)


'''
Created on Oct 24, 2014

@author: chengwen.li
'''

import httplib, urllib, threading, time, socket, errno

class UrlMonitor(threading.Thread):
    def __init__(self, url, duration, logger, finish_callback):
        threading.Thread.__init__(self)
        self.url = url
        self.duration = int(duration)
        self.logger = logger
        self.finish_callback = finish_callback
        self.terminated = False

    def run(self):
        protocol, temp = urllib.splittype(self.url)  # @UnusedVariable
        host, uri =  urllib.splithost(temp)
        host, port = urllib.splitport(host)
        if port is None:
            port = 80
        response = None
        self.logger.info('monitor url:' + self.url)
        self.start_time = time.time()
        while True:
            if self.should_stop():
                break
            try:
                httpclient = httplib.HTTPConnection(host, port, timeout=3)
                #httpclient = httplib.HTTPConnection(host, port, duration=self.duration)
                httpclient.request('GET', uri)
                response = httpclient.getresponse()
                break
            except socket.timeout:
                if self.should_stop():
                    self.logger.error('request ' + self.url + ' failed!')
                    break
#                 self.logger.warn('request[' + self.url + '] timeout, wait 2 seconds.')
                time.sleep(2)
            except socket.error as err:
                if self.should_stop():
                    self.logger.error('cannot connect to ' + self.url)
                    break
                if err.errno != errno.ECONNREFUSED:
                    self.logger.error(err)
                    break
                self.logger.warn('connection[%s:%s] refused, wait 1 second.' % (host, port))
                time.sleep(1)

        if response is None:
            self.logger.error('monitor url failed!')
        else:
            if response.status >= 200 and response.status < 400:
                self.logger.info('start successfully.')
            else:
                self.logger.error('start failed!')
        self.finish_callback()

    def stop(self):
        self.terminated = True

    def should_stop(self):
        return self.terminated or (time.time() - self.start_time >= self.duration)


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

"""Bottle Server Module."""

import logging
import os

import bottle

LOG = logging.getLogger(__name__)

class EventletLogFilter(object):  # pylint: disable=R0903

    """Receives eventlet log.write() calls and routes them.

    Thread and WSGI messages only get written in debug mode. Eventlet writes
    these out starting with a thread id in parens or "wsgi" messages:
    - "(46722) wsgi starting up on http://127.0.0.1:8080/"
    - "(47001) accepted ('127.0.0.1', 53046)"
    - "wsgi exiting"

    All other calls are assumed to be apache log-style API calls and we want
    these written as INFO and to an access_log if provided.

    API calls are assumed to start with the IP address:
    - "127.0.0.1 - - [07/Jul/2015 16:16:31] "GET /version HTTP/1.1" 200 ..."

    An instance of this class can be passed in to a bottle.run command using
    the `log` keyword.
    """

    def __init__(self, log, access_log=None):
        """Initialize with config and optional access_log.

        :param log: logger instance (ex. logging.getLogger()).
        :keyword access_log: a file handle to an access log that should receive
            apache-style entries for each call and response.
        """
        self.log = log
        self.access_log = access_log

    def write(self, text):
        """Write to appropriate target."""
        if text:
            if text[0] in '(w':
                # write thread and wsgi messages to debug only
                self.log.debug(text[:-1])
                return
            if self.access_log:
                self.access_log.write(text)
            self.log.info(text[:-1])

class XEventletServer(bottle.ServerAdapter):

    r"""Eventlet Bottle Server Adapter with extensions.

    Supports SSL. Accepts additional tuning parameters:

    * `backlog` adjust the eventlet backlog parameter which is the maximum
      number of queued connections. Should be at least 1; the maximum
      value is system-dependent.
    * `family`: (default is 2) socket family, optional. See socket
      documentation for available families.
    * `**kwargs`: directly map to python's ssl.wrap_socket arguments from
      https://docs.python.org/2/library/ssl.html#ssl.wrap_socket and
      wsgi.server arguments from
      http://eventlet.net/doc/modules/wsgi.html#wsgi-wsgi-server

    To create a self-signed key and start the eventlet server using SSL::

      openssl genrsa -des3 -out server.orig.key 2048
      openssl rsa -in server.orig.key -out test.key
      openssl req -new -key test.key -out server.csr
      openssl x509 -req -days 365 -in server.csr -signkey test.key -out \
      test.crt

      bottle.run(server='eventlet', keyfile='test.key', certfile='test.crt')
    """

    def get_socket(self):
        """Create listener socket based on bottle server parameters."""
        import eventlet

        # Separate out socket.listen arguments
        socket_args = {}
        for arg in ('backlog', 'family'):
            try:
                socket_args[arg] = self.options.pop(arg)
            except KeyError:
                pass
        # Separate out wrap_ssl arguments
        ssl_args = {}
        for arg in ('keyfile', 'certfile', 'server_side', 'cert_reqs',
                    'ssl_version', 'ca_certs', 'do_handshake_on_connect',
                    'suppress_ragged_eofs', 'ciphers'):
            try:
                ssl_args[arg] = self.options.pop(arg)
            except KeyError:
                pass
        address = (self.host, self.port)
        try:
            sock = eventlet.listen(address, **socket_args)
        except TypeError:
            # Fallback, if we have old version of eventlet
            sock = eventlet.listen(address)
        if ssl_args:
            sock = eventlet.wrap_ssl(sock, **ssl_args)
        return sock

    def run(self, handler):
        """Start bottle server."""
        import eventlet.patcher
        if not eventlet.patcher.is_monkey_patched(os):
            msg = ("%s requires eventlet.monkey_patch() (before "
                   "import)" % self.__class__.__name__)
            raise RuntimeError(msg)

        # Separate out wsgi.server arguments
        wsgi_args = {}
        for arg in ('log', 'environ', 'max_size', 'max_http_version',
                    'protocol', 'server_event', 'minimum_chunk_size',
                    'log_x_forwarded_for', 'custom_pool', 'keepalive',
                    'log_output', 'log_format', 'url_length_limit', 'debug',
                    'socket_timeout', 'capitalize_response_headers'):
            try:
                wsgi_args[arg] = self.options.pop(arg)
            except KeyError:
                pass
        if 'log_output' not in wsgi_args:
            wsgi_args['log_output'] = not self.quiet

        import eventlet.wsgi
        sock = self.options.pop('shared_socket', None) or self.get_socket()
        eventlet.wsgi.server(sock, handler, **wsgi_args)

    def __repr__(self):
        """Show class name, even if subclassed."""
        return self.__class__.__name__

bottle.server_names['xeventlet'] = XEventletServer

class XTornadoServer(bottle.ServerAdapter):  # pylint: disable=R0903

    """The Tornado Server Adapter with xheaders enabled."""

    def run(self, handler):
        """Start up the server."""
        import tornado.httpserver
        import tornado.ioloop
        import tornado.wsgi
        container = tornado.wsgi.WSGIContainer(handler)
        server = tornado.httpserver.HTTPServer(container, xheaders=True)
        server.listen(port=self.port, address=self.host)
        tornado.ioloop.IOLoop.instance().start()

bottle.server_names['xtornado'] = XTornadoServer


import math
import numpy as np
import random
import matplotlib.pyplot as plt

def real_to_binary(vector, precision_array, left_array, right_array):
    l = np.ceil(np.log2(right_array - left_array) + precision_array * np.log2(10))
    l = l.astype(np.int64)
    ints = np.rint((vector - left_array) / (1 / np.power(10, precision_array)))
    ints = ints.astype(np.int64)
    binary = np.zeros(np.sum(l, dtype=np.int64), dtype=np.int64)

    i = 0
    l_accum = 0
    for integer in ints:
        quotient = integer
        bin = []
        while quotient != 0:
            remainder = np.rint(quotient % 2).astype(np.int64)
            quotient = quotient // 2
            bin.append(remainder)

        idx = l[i] - 1 + l_accum
        k = 0
        for j in range(len(bin)):
            binary[idx] = bin[k]
            idx -= 1
            k += 1
        l_accum += l[i]
        i += 1
    return binary, l

def binary_to_real(binary_vector, precision_array, left_array, l):
    accum_vector = np.zeros(l.shape[0])
    idx = 0
    for j in range(l.shape[0]):
        for i in reversed(range(l[j])):
            accum_vector[j] += (2 ** i) * binary_vector[idx]
            idx += 1
    real_vector = left_array + (accum_vector * (1 / np.power(10, precision_array)))
    return real_vector

def make_aptitude_positive(A, bias):
    a_min = A[0]
    idx_min = 0
    for i in range(A.shape[0] - 1):
        if (A[i+1] < a_min):
            idx_min = i+1
            a_min = A[i+1]
    if a_min < 0:
        A = A - a_min
        A = A + bias
    return A

def roulette_selection(A, X, n, sel_population):
    B = np.zeros(A.shape[0])
    B[0] = A[0]
    for i in range(A.shape[0] - 1):
        B[i + 1] = A[i + 1] + B[i]

    for i in range(n):
        u = B[A.shape[0] - 1] * random.uniform(0, 1)
        j = 0
        while u > B[j]:
            j += 1
        sel_population = np.append(sel_population, X[j])

    return sel_population

def stochastic_leftover_selection(A, X, n, sel_population):
    b_m = np.sum(A)
    m = A.shape[0]
    E = m * (A / b_m)

    int_part = np.trunc(E)
    int_part = int_part.astype(np.int64)
    leftover = E - int_part
    for i in range(int_part.shape[0]):
        for j in range(int_part[i]):
            sel_population = np.append(sel_population, X[i])

    size_solution = X[0].shape[0]
    sel_population = roulette_selection(leftover, X, n - sel_population.shape[0]//size_solution, sel_population)

    return sel_population

def one_point_cross(A, B):
    size_parent = A.shape[0]
    u = random.randrange(size_parent - 1)
    children1 = np.zeros(size_parent)
    children2 = np.zeros(size_parent)
    i = 0
    while i < u + 1:
        children1[i] = A[i]
        children2[i] = B[i]
        i += 1
    while i < size_parent:
        children1[i] = B[i]
        children2[i] = A[i]
        i += 1

    return children1, children2

def two_point_cross(A, B):
    size_parent = A.shape[0]
    u1 = random.randrange(size_parent - 1)
    u2 = random.randrange(size_parent - 1)
    while(u2 == u1):
        u2 = random.randrange(size_parent - 1)

    if u1 > u2:
        aux = u1
        u1 = u2
        u2 = aux

    children1 = np.zeros(size_parent)
    children2 = np.zeros(size_parent)
    i = 0
    while i < u1 + 1:
        children1[i] = A[i]
        children2[i] = B[i]
        i += 1
    while i < u2 + 1:
        children1[i] = B[i]
        children2[i] = A[i]
        i += 1
    while i < size_parent:
        children1[i] = A[i]
        children2[i] = B[i]
        i += 1

    return children1, children2

def uniform_cross(A, B):
    size_parent = A.shape[0]
    children1 = np.zeros(size_parent)
    children2 = np.zeros(size_parent)
    for i in range(size_parent):
        u = random.uniform(0, 1)
        if u < 0.5:
            children1[i] = A[i]
            children2[i] = B[i]
        else:
            children1[i] = B[i]
            children2[i] = A[i]

    return children1, children2

def linear_normalization(X, A, v_min, v_max):
    m = A.shape[0]
    idx_sorted = np.argsort(A)
    A_new = np.zeros(m)
    for i in range(m):
        A_new[idx_sorted[i]] = v_min + ((v_max - v_min) / (m-1)) * i
    return A_new

def get_best_individual(A):
    a_best = -1
    idx_best = -1
    for i in range(A.shape[0]):
        if A[i] > a_best:
            a_best = A[i]
            idx_best = i

    return idx_best

def mutation(x_bin):
    size_x_bin = x_bin.shape[0]
    u = random.randrange(size_x_bin)
    if (x_bin[u] == 1):
        x_bin[u] = 0
    else:
        x_bin[u] = 1
    return x_bin

def f1(x1, x2):
    return 0.5 - ((math.sin(math.sqrt(x1**2 + x2**2))**2 )-0.5 / (1.0+0.001*(x1**2 + x2**2))**2)

def evaluate_f1(X):
    size_pop = X.shape[0]
    A = np.zeros(size_pop)
    for i in range(size_pop):
        A[i] = f1(X[i][0], X[i][1])
    return A

def traditional_genetic_algorithm(eval, max_gen, size_population, left_array, right_array, precision_array,
                                  cross_prob, mutation_prob, elitism, cross, selection, linear_norm, bias):
    t = 0
    A_best_generations = np.zeros(max_gen)
    A_mean_generations = np.zeros(max_gen)
    size_solution = left_array.shape[0]
    X = np.zeros((size_population, size_solution))
    for i in range(size_population):
        for j in range(size_solution):
            X[i, j] = random.uniform(left_array[j], right_array[j])
    A = eval(X)
    while t < max_gen:
        idx_best = get_best_individual(A)
        A_best_generations[t] = A[idx_best]
        A_mean_generations[t] = np.mean(A)

        if linear_norm[0] == False:
            A = make_aptitude_positive(A, bias)
        else:
            A = linear_normalization(X, A, linear_norm[1], linear_norm[2])
        n = 0
        if A.shape[0] % 2 == 0:
            n = A.shape[0]
        else:
            n = A.shape[0] - 1
        sel_population = np.array([])
        if selection == "roulette":
            sel_population = roulette_selection(A, X, n, sel_population)
        else: # == "stochastic leftover"
            sel_population = stochastic_leftover_selection(A, X, n, sel_population)
        j = 0
        k = 0
        if elitism == True:
            new_population = np.zeros((n + 1, size_solution))
            idx_best = get_best_individual(A)
            new_population[k] = X[idx_best]
            k += 1
        else:
            new_population = np.zeros((n, size_solution))
        for i in range(n//2):
            parent_binary1, l = real_to_binary(sel_population[j], precision_array, left_array, right_array)
            j += 1
            parent_binary2, l = real_to_binary(sel_population[j], precision_array, left_array, right_array)
            j += 1
            r_cross = random.uniform(0, 1)
            if r_cross < cross_prob:
                if cross == "one point cross":
                    children1, children2 = one_point_cross(parent_binary1, parent_binary2)
                elif cross == "two point cross":
                    children1, children2 = two_point_cross(parent_binary1, parent_binary2)
                else:    # == "uniform cross"
                    children1, children2 = uniform_cross(parent_binary1, parent_binary2)
            else:
                children1, children2 = parent_binary1, parent_binary2

            r_mutation = random.uniform(0, 1)
            if r_mutation < mutation_prob:
                children1 = mutation(children1)
            r_mutation = random.uniform(0, 1)
            if r_mutation < mutation_prob:
                children2 = mutation(children2)

            ch1_real = binary_to_real(children1, precision_array, left_array, l)
            ch2_real = binary_to_real(children2, precision_array, left_array, l)
            new_population[k] = ch1_real
            k += 1
            new_population[k] = ch2_real
            k += 1

        X = new_population
        A = eval(X)
        #print("X:\n", X)
        #print("A:\n", A)
        t += 1
    return X, A, A_best_generations, A_mean_generations

def get_accum_mean(vec):
    vec_accum = np.zeros(vec.shape[0])
    vec_accum[0] = vec[0]
    for i in range(vec.shape[0] - 1):
        vec_accum[i+1] = vec[i+1] + vec_accum[i]
    div = np.arange(vec.shape[0]) + 1
    vec_accum = vec_accum / div
    return vec_accum

def graph_monitor_genetic_alg(A_best_generations, A_best_generations_accum, A_mean_generations_accum, title):
    fig, ax = plt.subplots()

    ax.plot(np.arange(A_best_generations.shape[0]), A_best_generations, 'b', label='Curva de mejores elementos')
    ax.plot(np.arange(A_best_generations_accum.shape[0]), A_best_generations_accum, 'g', label='Curva offline')
    ax.plot(np.arange(A_mean_generations_accum.shape[0]), A_mean_generations_accum, 'r', label='Curva online')
    plt.ylabel('aptitud')
    plt.xlabel('generacion')
    legend = plt.legend(loc=0, shadow=True, fontsize='x-large')
    plt.title(title)
    major_ticks_y = np.arange(0, 1.05, 0.2)
    minor_ticks_y = np.arange(0, 1.05, 0.05)
    major_ticks_x = np.arange(0, 41, 5)
    minor_ticks_x = np.arange(0, 41, 1)

    ax.set_xticks(major_ticks_x)
    ax.set_xticks(minor_ticks_x, minor=True)
    ax.set_yticks(major_ticks_y)
    ax.set_yticks(minor_ticks_y, minor=True)

    # And a corresponding grid
    ax.grid(which='both')

    # Or if you want different settings for the grids:
    ax.grid(which='minor', alpha=0.2)
    ax.grid(which='major', alpha=0.5)
    #ax.grid(True)

    plt.show()

def experiment1(num_executions):
    max_gen = 40
    size_population = 100
    left_array = np.array([-100, -100])
    right_array = np.array([100, 100])
    precision_array = np.array([7, 8])
    cross_prob = 0.65
    mutation_prob = 0.008
    elitism = True
    cross_vec = ["one point cross", "two point cross", "uniform cross"]
    selection = "roulette"
    linear_norm = [False, 0, 0]
    bias = 5 # para make_aptitude_positive()

    for cross in cross_vec:
        A_best_generations_avg = np.zeros(max_gen)
        A_best_generations_accum_avg = np.zeros(max_gen)
        A_mean_generations_accum_avg = np.zeros(max_gen)
        for i in range(num_executions):
            X, A, A_best_generations, A_mean_generations = traditional_genetic_algorithm(evaluate_f1, max_gen, size_population, left_array, right_array, precision_array,
                                                                                cross_prob, mutation_prob, elitism, cross, selection, linear_norm, bias)

            print("A_best_generations:\n", A_best_generations)
            print("A_mean_generations:\n", A_mean_generations)

            A_best_generations_avg += A_best_generations
            A_best_generations_accum_avg += get_accum_mean(A_best_generations)
            A_mean_generations_accum_avg += get_accum_mean(A_mean_generations)
        A_best_generations_avg /= num_executions
        A_best_generations_accum_avg /= num_executions
        A_mean_generations_accum_avg /= num_executions
        graph_monitor_genetic_alg(A_best_generations_avg, A_best_generations_accum_avg, A_mean_generations_accum_avg, cross)

def experiment2(num_executions):
    max_gen = 40
    size_population = 100
    left_array = np.array([-100, -100])
    right_array = np.array([100, 100])
    precision_array = np.array([7, 8])
    params = np.array([[0.65, 0.008],
                       [0.85, 0.008],
                       [0.55, 0.008],
                       [0.25, 0.008],
                       [0.65, 0.004],
                       [0.65, 0.01],
                       [0.65, 0.02],
                       [0.65, 0.05]])
    elitism = True
    cross = "uniform cross"
    selection = "stochastic leftover"
    linear_norm = [False, 0, 0]
    bias = 5 # para make_aptitude_positive()

    for i in range(params.shape[0]):
        A_best_generations_avg = np.zeros(max_gen)
        A_best_generations_accum_avg = np.zeros(max_gen)
        A_mean_generations_accum_avg = np.zeros(max_gen)
        for j in range(num_executions):
            X, A, A_best_generations, A_mean_generations = traditional_genetic_algorithm(evaluate_f1, max_gen, size_population, left_array, right_array, precision_array,
                                                                                params[i, 0], params[i, 1], elitism, cross, selection, linear_norm, bias)
            print("A_best_generations:\n", A_best_generations)
            print("A_mean_generations:\n", A_mean_generations)

            A_best_generations_avg += A_best_generations
            A_best_generations_accum_avg += get_accum_mean(A_best_generations)
            A_mean_generations_accum_avg += get_accum_mean(A_mean_generations)
        A_best_generations_avg /= num_executions
        A_best_generations_accum_avg /= num_executions
        A_mean_generations_accum_avg /= num_executions
        graph_monitor_genetic_alg(A_best_generations_avg, A_best_generations_accum_avg, A_mean_generations_accum_avg, "prob_cruce=%f, prob_mut=%f" % (params[i, 0], params[i, 1]))

def experiment3(num_executions):
    max_gen = 40
    size_population = 100
    left_array = np.array([-100, -100])
    right_array = np.array([100, 100])
    precision_array = np.array([7, 8])
    cross_prob = 0.25
    mutation_prob = 0.008
    elitism = False
    cross = "uniform cross"
    selection = "stochastic leftover"
    linear_norm = [False, 0, 0]
    bias = 5 # para make_aptitude_positive()

    A_best_generations_avg = np.zeros(max_gen)
    A_best_generations_accum_avg = np.zeros(max_gen)
    A_mean_generations_accum_avg = np.zeros(max_gen)

    for j in range(num_executions):
        X, A, A_best_generations, A_mean_generations = traditional_genetic_algorithm(evaluate_f1, max_gen, size_population, left_array, right_array, precision_array,
                                                                                cross_prob, mutation_prob, elitism, cross, selection, linear_norm, bias)
        print("A_best_generations:\n", A_best_generations)
        print("A_mean_generations:\n", A_mean_generations)

        A_best_generations_avg += A_best_generations
        A_best_generations_accum_avg += get_accum_mean(A_best_generations)
        A_mean_generations_accum_avg += get_accum_mean(A_mean_generations)
    A_best_generations_avg /= num_executions
    A_best_generations_accum_avg /= num_executions
    A_mean_generations_accum_avg /= num_executions
    graph_monitor_genetic_alg(A_best_generations_avg, A_best_generations_accum_avg, A_mean_generations_accum_avg, "Ni normalizacion lineal ni elitismo")

    elitism = True

    A_best_generations_avg = np.zeros(max_gen)
    A_best_generations_accum_avg = np.zeros(max_gen)
    A_mean_generations_accum_avg = np.zeros(max_gen)

    for j in range(num_executions):
        X, A, A_best_generations, A_mean_generations = traditional_genetic_algorithm(evaluate_f1, max_gen, size_population, left_array, right_array, precision_array,
                                                                                cross_prob, mutation_prob, elitism, cross, selection, linear_norm, bias)
        print("A_best_generations:\n", A_best_generations)
        print("A_mean_generations:\n", A_mean_generations)

        A_best_generations_avg += A_best_generations
        A_best_generations_accum_avg += get_accum_mean(A_best_generations)
        A_mean_generations_accum_avg += get_accum_mean(A_mean_generations)
    A_best_generations_avg /= num_executions
    A_best_generations_accum_avg /= num_executions
    A_mean_generations_accum_avg /= num_executions
    graph_monitor_genetic_alg(A_best_generations_avg, A_best_generations_accum_avg, A_mean_generations_accum_avg, "Solo elitismo")

    linear_norm = [True, 10, 60]

    A_best_generations_avg = np.zeros(max_gen)
    A_best_generations_accum_avg = np.zeros(max_gen)
    A_mean_generations_accum_avg = np.zeros(max_gen)

    for j in range(num_executions):
        X, A, A_best_generations, A_mean_generations = traditional_genetic_algorithm(evaluate_f1, max_gen, size_population, left_array, right_array, precision_array,
                                                                                cross_prob, mutation_prob, elitism, cross, selection, linear_norm, bias)
        print("A_best_generations:\n", A_best_generations)
        print("A_mean_generations:\n", A_mean_generations)

        A_best_generations_avg += A_best_generations
        A_best_generations_accum_avg += get_accum_mean(A_best_generations)
        A_mean_generations_accum_avg += get_accum_mean(A_mean_generations)
    A_best_generations_avg /= num_executions
    A_best_generations_accum_avg /= num_executions
    A_mean_generations_accum_avg /= num_executions
    graph_monitor_genetic_alg(A_best_generations_avg, A_best_generations_accum_avg, A_mean_generations_accum_avg, "Norm Lineal 10 - 60 y elitismo")

    linear_norm = [True, 1, 200]

    A_best_generations_avg = np.zeros(max_gen)
    A_best_generations_accum_avg = np.zeros(max_gen)
    A_mean_generations_accum_avg = np.zeros(max_gen)

    for j in range(num_executions):
        X, A, A_best_generations, A_mean_generations = traditional_genetic_algorithm(evaluate_f1, max_gen, size_population, left_array, right_array, precision_array,
                                                                                cross_prob, mutation_prob, elitism, cross, selection, linear_norm, bias)
        print("A_best_generations:\n", A_best_generations)
        print("A_mean_generations:\n", A_mean_generations)

        A_best_generations_avg += A_best_generations
        A_best_generations_accum_avg += get_accum_mean(A_best_generations)
        A_mean_generations_accum_avg += get_accum_mean(A_mean_generations)
    A_best_generations_avg /= num_executions
    A_best_generations_accum_avg /= num_executions
    A_mean_generations_accum_avg /= num_executions
    graph_monitor_genetic_alg(A_best_generations_avg, A_best_generations_accum_avg, A_mean_generations_accum_avg, "Norm Lineal 1 - 200 y elitismo")

def main():
    max_gen = 40
    size_population = 100
    left_array = np.array([-100, -100])
    right_array = np.array([100, 100])
    precision_array = np.array([6, 8])
    cross_prob = 0.25
    mutation_prob = 0.008
    elitism = True
    #cross = "one point cross"
    cross = "two point cross"
    #cross = "uniform cross"
    #selection = "roulette"
    selection = "stochastic leftover"
    #linear_norm = [False, 0, 0]
    linear_norm = [True, 5, 100]
    bias = 5 # para make_aptitude_positive()

    X, A, A_best_generations, A_mean_generations = traditional_genetic_algorithm(evaluate_f1, max_gen, size_population, left_array, right_array, precision_array,
                                                                                cross_prob, mutation_prob, elitism, cross, selection, linear_norm, bias)

    print("A_best_generations:\n", A_best_generations)
    print("A_mean_generations:\n", A_mean_generations)

    A_best_generations_accum = get_accum_mean(A_best_generations)
    A_mean_generations_accum = get_accum_mean(A_mean_generations)
    graph_monitor_genetic_alg(A_best_generations, A_best_generations_accum, A_mean_generations_accum, 'Prueba')

    num_executions = 20
    #experiment1(num_executions)
    #experiment2(num_executions)
    experiment3(num_executions)

main()


from question.models import *
from question.forms import *
from django.shortcuts import *
import datetime

def edit(request):
    if request.method == 'POST':
        form = SingleChoiceQuestionForm(request.POST)
        if form.is_valid():
            scq = form.save(commit = False)
            scq.save()

            question = Question()
            question.status = 2
            question.content_object = scq
            question.author = request.user
            question.last_modify = datetime.datetime.now()
            question.save()
            return HttpResponseRedirect("/question/edit/scq/" + str(scq.id))

    else:
        form = SingleChoiceQuestionForm()
        return render_to_response("question/add/scq.html", locals(), context_instance = RequestContext(request))


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

import subprocess

def test_noqa_fixture_disabled(absolute_path):
    """End-to-End test to check that all violations are present."""
    process = subprocess.Popen(
        [
            'flake8',
            '--disable-noqa',
            '--select',
            'Z',
            absolute_path('fixtures', 'noqa.py'),
        ],
        stdout=subprocess.PIPE,
        stderr=subprocess.PIPE,
    )
    stdout, _ = process.communicate()
    output = stdout.decode('utf8')

    counts = {
        'Z110': 2,
        'Z111': 1,
        'Z112': 1,
        'Z113': 1,

        'Z220': 1,

        'Z300': 1,
        'Z302': 1,
        'Z303': 1,
        'Z304': 1,
        'Z305': 1,
        'Z306': 2,
        'Z307': 1,
        'Z308': 1,
        'Z309': 1,
        'Z310': 4,

        'Z410': 1,
        'Z420': 1,
        'Z421': 1,
        'Z422': 1,
        'Z423': 1,
        'Z430': 1,
        'Z431': 2,
        'Z432': 2,
        'Z433': 2,
        'Z434': 1,
        'Z435': 1,
        'Z436': 1,
    }

    for error in counts:
        assert output.count(error) == counts[error], error

def test_noqa_fixture(absolute_path):
    """End-to-End test to check that `noqa` works."""
    process = subprocess.Popen(
        ['flake8', '--select', 'Z', absolute_path('fixtures', 'noqa.py')],
        stdout=subprocess.PIPE,
        stderr=subprocess.PIPE,
    )
    stdout, _ = process.communicate()

    assert stdout.count(b'Z') == 0


import jinja2
import webapp2
import json

from utils import maps

JINJA_ENVIRONMENT = jinja2.Environment(
    loader=jinja2.FileSystemLoader('templates'),
    extensions=['jinja2.ext.autoescape'])

class MainHandler(webapp2.RequestHandler):
  def get(self):

    # Render to templates
    template_values = {
      'message': 'Hello'
    }
    template = JINJA_ENVIRONMENT.get_template('index.html')
    self.response.write(template.render(template_values))

class GetSectionsHandler(webapp2.RequestHandler):
  def get(self):

    # result
    result = {
      'success': False,
    }

    # query
    sw_lat = self.request.get('sw_lat')
    ne_lat = self.request.get('ne_lat')
    sw_lng = self.request.get('sw_lng')
    ne_lng = self.request.get('ne_lng')
    if not (sw_lat and ne_lat and sw_lng and ne_lng):
      result['message'] = 'ERROR: Please specify bounds'
      self.response.write(json.dumps(result))
      return

    # floatify
    sw_lat = float(sw_lat)
    ne_lat = float(ne_lat)
    sw_lng = float(sw_lng)
    ne_lng = float(ne_lng)

    # Get sections
    sections = maps.pick_sections(self.request.host_url, sw_lat, sw_lng, ne_lat, ne_lng)
    if sections:
      result['success'] = True
      result['sections'] = sections

    self.response.write(json.dumps(result))


import cv2
import numpy as np
import os
from create_base import *
import time

def get_contours(filename,pic_path):
    #pic_path = "./tmp/tmp.jpg"
    y,sr = get_cqt_pic(filename,pic_path)
    rms = librosa.feature.rmse(y=y)[0]
    length = len(rms)
    img = cv2.imread(pic_path)
    # kernel = np.ones((2, 2), np.uint8)*np.max(img)  # 全为1的过滤数组
    # # 膨胀(去黑小点)
    # img = cv2.dilate(img, kernel)  # 膨胀

    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    ret,binary = cv2.threshold(gray, 100, 255, cv2.THRESH_BINARY)

    _,contours, hierarchy = cv2.findContours(binary, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_TC89_KCOS)

    # draw_img0 = cv2.drawContours(img.copy(),contours,0,(0,255,255),3)
    # draw_img1 = cv2.drawContours(img.copy(),contours,1,(255,0,255),3)
    # draw_img2 = cv2.drawContours(img.copy(),contours,2,(255,255,0),3)
    #draw_img3 = cv2.drawContours(img.copy(), contours, -1, (0, 0, 255), 1)
    draw_img3 = img.copy()
    contours_dict = {}
    right_lines = []
    right_points = []
    for i in range(0,len(contours)):
        x, y, w, h = cv2.boundingRect(contours[i])
        right_points.append(x + w)
    for i in range(0,len(contours)):
        x, y, w, h = cv2.boundingRect(contours[i])
        #cv2.rectangle(draw_img3, (x, y), (x + w, y + h), (0, 0, 255), 2)
        if w * h > 30:
            cv2.rectangle(draw_img3, (x,y), (x+w,y+h), (0, 0, 255), 2)
            font = cv2.FONT_HERSHEY_SIMPLEX
            cv2.putText(draw_img3, str(x) + ',' + str(y), (x,y), font, 0.4, (255, 255, 255), 1)
            #print("START x,w is {},{}".format(x, x + w))
            #根据左边线判断
            is_exist = False
            tmp = []
            for j in range(x-5,x+5):
                if j in contours_dict.keys():
                    #print("1. x,w is {},{}".format(x,x+w))
                    tmp.append(contours_dict.get(j))
                    tmp.append([x, y, w, h])
                    contours_dict.update({int((x + j)/2):tmp})
                    #添加右边线
                    if w > 10 and h > 10 and check_right_line(right_points,x + w):
                        right_lines.append(x + w)
                    is_exist = True
                    break
            if is_exist is False:
                tmp.append([x, y, w, h])
                contours_dict.update({x:tmp})
                #print("2. x,w is {},{}".format(x, x + w))
            # 根据右边线判断
            # is_exist = False
            # tmp = []
            # for j in range(x + w - 3, x + w + 3):
            #     if j in contours_dict.keys():
            #         tmp.append(contours_dict.get(j))
            #         tmp.append(contours[i])
            #         contours_dict.update({int((x + w + j) / 2): tmp})
            #         is_exist = True
            #         break
    find_notes = find_note_by_contours(contours_dict)
    for x in find_notes:
        cv2.line(draw_img3, (x, 10), (x, 200), (0, 255, 0), 2)  # 绿色，3个像素宽度
    right_lines.sort()
    if len(right_lines)>0:
        right_lines = del_overrate(right_lines)
        right_lines = check_with_start(find_notes, right_lines)
    for x in right_lines:
        cv2.line(draw_img3, (x, 10), (x, 200), (255, 255, 0), 2)  # 绿色，3个像素宽度
        find_notes.append(x)
    find_notes.sort()
    return draw_img3,img,find_notes,length

def get_note_lines(img,find_notes,lenght):
    width = img.shape[1]
    result = []
    for x in find_notes:
        tmp = int(x * lenght /width)
        result.append(tmp)
    return result

def find_note_by_contours(contours_dict):
    result = []
    for key in contours_dict.keys():
        tmp = contours_dict.get(key)
        if len(tmp)>=2:
            result.append(key)
        elif tmp[0][2]*tmp[0][3] > 50 and tmp[0][2]*tmp[0][3] < 10000:
            result.append(key)
    result.sort()
    result = del_overrate(result)
    return result

def del_overrate(result):
    new_result = [result[0]]
    last = result[0]
    for i in range(1, len(result)):
        if result[i] - last > 3:
            new_result.append(result[i])
            last = result[i]
    return new_result

def check_with_start(start_nots,right_lines):
    result = []
    for r in right_lines:
        tmp = [np.abs(r - x) for x in start_nots]
        if np.min(tmp) > 5:
            result.append(r)
    return result

def check_right_line(right_points,right):
    offset = [x - right for x in right_points]
    find_nearly = [x for x in offset if x<=1]
    if len(find_nearly)>3:
        return True
    else:
        return False

def get_cqt_pic(filename,pic_path):
    _,y,sr = draw_on_cqt(filename,pic_path, False)
    plt.close()
    return y,sr

if __name__ == "__main__":
    filename = 'F:/项目/花城音乐项目/样式数据/3.06MP3/旋律/旋8录音4(93).wav'
    filename = 'F:/项目/花城音乐项目/样式数据/3.06MP3/旋律/旋律八（2）（60）.wav'
    start_time = time.time()
    #get_cqt_pic(filename)
    end_time = time.time()
    print("runing is {}".format(end_time - start_time))
    # filename = 'E:/test_image/n/A/50.jpg'
    # filename = 'e:/test_image/n/D/4.jpg'
    # filename = 'e:/test_image/n/C/54.jpg'
    pic_path = "./tmp/tmp.jpg"
    draw_img3,img,note_lines,length = get_contours(filename,pic_path)
    #cv2.imshow("img", img)
    # cv2.imshow("draw_img0", draw_img0)
    # cv2.imshow("draw_img1", draw_img1)
    # cv2.imshow("draw_img2", draw_img2)
    cv2.imshow("draw_img3", draw_img3)
    print("width,height is {},{}".format(img.shape[1],img.shape[0]))

    cv2.waitKey(0)
    cv2.destroyAllWindows()
    cv2.imwrite( "f:/test1111..jpg",draw_img3)

    dir_list = ['F:/项目/花城音乐项目/样式数据/3.06MP3/旋律/']
    dir_list = ['e:/test_image/n/B/']
    dir_list = []
    total_accuracy = 0
    total_num = 0
    result_path = 'e:/test_image/c/'
    # clear_dir(result_path)
    # 要测试的数量
    test_num = 100
    score = 0
    for dir in dir_list:
        file_list = os.listdir(dir)
        # shuffle(file_list)  # 将语音文件随机排列
        # file_list = ['视唱1-01（95）.wav']
        for filename in file_list:
            # clear_dir(image_dir)
            # wavname = re.findall(pattern,filename)[0]
            print(dir + filename)
            # plt = draw_start_end_time(dir + filename)
            draw_img3, img = get_contours(dir + filename)

            grade = 'B'
            cv2.imwrite(result_path + grade + "/" + filename, draw_img3)


#!/bin/env python2
from __future__ import print_function

import sys, os, random, time, atexit
import findpygly
from pygly.GlycanImage import GlycanImage
from pygly.GlycanResource import GlyTouCan, GlyCosmos
from pygly.GNOme import GNOme

batch = 10
iterations = 100
scale_options = [ 0.5, 1.0, 2.0, 4.0, ]
redend_options = [ True, False ]
orient_options = [ "RL", "LR", "TB", "BT" ]
notation_options = [ "snfg", "cfg" ]
display_options = [ "normal", "normalinfo", "compact" ]
opaque_options = [ True, False ]

print("GlyCosmos archived...",file=sys.stderr)
start = time.time()
gco = GlyCosmos(usecache=False)
archived = set(map(lambda d: d['accession'],gco.archived()))
print("GlyCosmos archived complete. (%s secs.)"%(time.time()-start,),file=sys.stderr)

print("GlyTouCan accessions...",file=sys.stderr)
start = time.time()
gtc = GlyTouCan(verbose=False,usecache=False,prefetch=True)
accs = list(filter(lambda acc: acc not in archived,gtc.allaccessions()))
dummy = gtc.getseq('G00912UN','wurcs')
print("GlyTouCan accessions complete. (%s secs.)"%(time.time()-start,),file=sys.stderr)

print("GNOme setup...",file=sys.stderr)
start = time.time()
gnome = GNOme()
print("GNOme setup complete. (%s secs.)"%(time.time()-start,),file=sys.stderr)

import subprocess, atexit
xvfbproc = subprocess.Popen(["Xvfb",":1"])
os.environ["DISPLAY"] = "localhost:1.0"
def killxvfb(proc):
    try:
        proc.terminate()
    except OSError:
        pass
atexit.register(killxvfb,xvfbproc)

seen = set()
for j in range(iterations):
    imageWriter = GlycanImage()
    imageWriter.set('scale',random.choice(scale_options))
    imageWriter.set('reducing_end',random.choice(redend_options))
    imageWriter.set('orientation',random.choice(orient_options))
    imageWriter.set('notation',random.choice(notation_options))
    imageWriter.set('display',random.choice(display_options))
    imageWriter.set('opaque',random.choice(opaque_options))
    imageWriter.force(True)
    # imageWriter.verbose(True)

    count = 0
    while count < batch:
        acc = random.choice(accs)
        if acc in seen:
            continue
        seen.add(acc)
        outfile = acc + ".png"
        if os.path.exists(outfile):
            continue
        gly = gtc.getGlycan(acc,format='wurcs')
        if not gly:
            continue
        seq = gtc.getseq(acc,format='wurcs')
        if not seq:
            continue
        if gly.undetermined():
            continue
        if not gly.has_root():
            continue
        if gly.repeated():
            continue
        topoacc = gnome.get_topology(acc)
        if not topoacc:
            continue
        comp = gly.iupac_composition(floating_substituents=False,
                                     aggregate_basecomposition=False)
        if comp['Count'] < 3:
            continue
        bad = False
        for k,v in comp.items():
            if k in ('Glc','Gal','Man','NeuAc','NeuGc','Fuc','GlcNAc','GalNAc','Count'):
                continue
            if v <= 0:
                continue
            bad = True
            break
        if bad:
            continue
        print(acc,file=sys.stderr)
        imageWriter.writeImage(seq,outfile)
        wh = open(acc + ".log",'w')
        for k in ('scale','reducing_end','orientation','notation','display','opaque'):
            print(k+":",imageWriter.get(k),file=wh)
        print("composition:",comp,file=wh)
        print("topology:",topoacc,file=wh)
        wh.close()
        count += 1


#
# @lc app=leetcode.cn id=115 lang=python3
#
# [115] 不同的子序列
#

# @lc code=start
class Solution:
    def numDistinct(self, s: str, t: str) -> int:
        # dp[i][j] 表示s[0: i - 1]中t[0: j - 1]出现的个数
        # dp[i][j] = dp[i - 1][j - 1] + dp[i - 1][j] if s[i - 1] == t[j - 1]
        # dp[i][j] = dp[i - 1][j] if s[i - 1] != t[j - 1]
        # base case
        # dp[:][0] = 1
        m, n = len(s) + 1, len(t) + 1

        dp = [[0] * n for _ in range(m)]
        for i in range(m):
            dp[i][0] = 1
        for i in range(1, m):
            for j in range(1, n):
                dp[i][j] = dp[i - 1][j] + dp[i - 1][j - 1] * (s[i - 1] == t[j - 1])
        return dp[-1][-1]

# @lc code=end