import logging
import random
from typing import Set, Generator, Optional

from .location import Location
from .move import Move
from .piece import Color, Piece, Rank
from .board import Board

class MoveSet:
    _brd = None  # type: Board

    @staticmethod
    def set_board(brd: Board) -> None:
        r""" Sets the board for the entire class """
        MoveSet._brd = brd

    r""" Moves available to a player """
    def __init__(self, color: Color):
        r"""
        :param color: Color that is making the moves
        """
        self._avail = dict()  # Available moves
        self._color = color

    @property
    def avail(self) -> dict:
        r""" Accessor for the available moves """
        return self._avail

    @staticmethod
    def build(pieces: Set[Piece], locs: dict, other_locs: dict) -> 'MoveSet':
        r"""
        Factory method used to construct an initial move set.

        :param pieces: All of the players pieces
        :param locs: Location of the player pieces
        :param other_locs: Location of pieces of other player
        :return: Constructed move set
        """
        assert MoveSet._brd is not None, "Board information be present"
        assert pieces, "Piece set can never be empty"
        color = next(iter(pieces)).color
        ms = MoveSet(color)
        for p in pieces:
            ms.add_piece(p, locs, other_locs)
        return ms

    def add_piece(self, piece: Piece, locs: dict, other_locs: dict):
        r"""
        Add a piece's moves to the MoveSet
        :param piece: Piece whose moves (if any) will be added
        :param locs: Location of the player pieces
        :param other_locs: Location of pieces of other player
        """
        self._process_piece(piece, locs, other_locs, add=True)

    def del_piece(self, piece: Piece, locs: dict, other_locs: dict):
        r"""
        Add a piece's moves to the MoveSet
        :param piece: Piece whose moves (if any) will be added
        :param locs: Location of the player pieces
        :param other_locs: Location of pieces of other player
        """
        self._process_piece(piece, locs, other_locs, add=False)

    def _process_piece(self, piece: Piece, locs: dict, other_locs: dict, add: bool):
        r"""
        Standardizes adding/removing a piece since same algorithm with minor change.

        :param piece: Piece to process
        :param locs: Location for pieces of same color as \p Piece
        :param other_locs: Location for other player's pieces
        :param add: If True, add the piece, otherwise remove the piece
        """
        # Verify color is same for all pieces
        assert piece.color == self._color, "Piece set has pieces of different colors"

        # Standard function for either adding or deleting a move
        def _process_func(_p: Piece, _loc: Location):
            if add:
                try: self._add_move(_p, _loc, other_locs[_loc])
                except KeyError: self._add_move(_p, _loc)
            else: self._del_move(_p, _loc)

        # Bombs and flags can be ignored
        if piece.is_immobile(): return
        # Check ordinary pieces
        if piece.rank != Rank.scout():
            for loc in piece.loc.neighbors():
                # Ignore pieces not allowed by board or where piece of same color
                if not self._brd.is_inside(loc) or loc in locs: continue
                _process_func(piece, loc)
        # Check scout pieces specially
        else:
            for direction_list in self._brd.to_edge_lists(piece.loc):
                for loc in direction_list:
                    # If scout blocked by board location or same color, immediately stop
                    if not self._brd.is_inside(loc) or loc in locs: break
                    _process_func(piece, loc)
                    if loc in other_locs: break

    def _add_move(self, p: Piece, other: Location, attacked: Optional[Piece] = None) -> None:
        r""" Add \p piece's move to \p other to the \p MoveSet """
        assert p.is_scout() or p.loc.is_adjacent(other)

        key = self._make_move_key(p.loc, other)
        # assert key not in self._avail
        self._avail[key] = Move(p, p.loc, other, attacked)

    def _del_move(self, p: Piece, other: Location) -> None:
        r"""
        Delete the corresponding move from the \p MoveSet

        :param p: Piece whose move will be deleted
        :param other: Location where \p p will be moved
        """
        assert p.is_scout() or p.loc.is_adjacent(other)

        key = self._make_move_key(p.loc, other)
        del self._avail[key]

    def has_move(self, p: Piece, new_loc: Location) -> bool:
        r""" Returns True if the \p Piece has an availble move to the specified \p Location """
        key = self._make_move_key(p.loc, new_loc)
        return key in self._avail

    def get_move(self, p: Piece, new_loc: Location) -> Optional[Move]:
        r"""
        Gets the move corresponding to the \p Piece and \p Location.  If the corresponding \p Move
        is not found, \p None is returned.
        """
        key = self._make_move_key(p.loc, new_loc)
        try: return self._avail[key]
        except KeyError: return None

    def __len__(self) -> int:
        r""" Return number of moves in the \p MoveSet """
        return len(self._avail)

    def __contains__(self, item: Move) -> bool:
        r""" Adds support for the "in" operator """
        if item.piece is None: return False
        return self.has_move(item.piece, item.new)

    def remove_moves_after_add(self, loc: Location, plyr_locs: dict, other_locs: dict) -> None:
        r"""
        Process the adding of a piece at Location \p loc

        :param loc: Location of added piece
        :param plyr_locs: Location of pieces for same color as \p MoveSet
        :param other_locs: Location of pieces of other \p Player
        """
        self._handle_loc_change(loc, plyr_locs, other_locs, False)

    def add_moves_after_delete(self, loc: Location, plyr_locs: dict, other_locs: dict) -> None:
        r"""
        Process the deletion of a piece that was at Location \p loc

        :param loc: Location of deleted piece
        :param plyr_locs: Location of pieces for same color as \p MoveSet
        :param other_locs: Location of pieces of other \p Player
        """
        self._handle_loc_change(loc, plyr_locs, other_locs, True)

    def _handle_loc_change(self, loc: Location, plyr_locs: dict, other_locs: dict, add: bool):
        r"""
        Process a \p Location's state change by either removing or add moves to the MoveSet.

        :param loc: Location whose state is being changed
        :param plyr_locs: Locations of the implicit player's pieces
        :param other_locs: Location dictionary for the other player
        :param add: If True, add moves to the MoveSet.  Otherwise, remove those locations.
        """
        el = self._brd.to_edge_lists(loc)
        el_groups = [(el.right, el.left), (el.left, el.right), (el.up, el.down), (el.down, el.up)]

        def _add_func(_p: Piece, _loc: Location):
            try: self._add_move(_p, _loc, other_locs[_loc])
            except KeyError: self._add_move(_p, _loc)

        for search, opp in el_groups:
            # Find first piece in search direction (if any)
            p = None
            for srch in search:
                if srch in plyr_locs: p = plyr_locs[srch]
                elif srch in other_locs: p = other_locs[srch]
                if p is not None: break
            # If no piece in search direction
            if p is None or p.is_immobile(): continue
            # Ignore pieces of other color since will be handled in separate function call
            if p.color != self._color: continue
            # If found p is not a scout and not adjacent, move on
            if not p.is_scout() and not p.loc.is_adjacent(loc): continue

            # Delete first since may need to add in next step
            if not add: self._del_move(p, loc)
            # In an add, always add the move.  In a delete, may need to add back if the moved
            # piece is of the other player's color
            if add or loc in other_locs:
                _add_func(p, loc)

            if p.is_scout():
                for srch in opp:
                    if srch in plyr_locs: break

                    if add: _add_func(p, srch)
                    else: self._del_move(p, srch)

                    # Perform second since could still attack
                    if srch in other_locs: break

    @staticmethod
    def _make_move_key(orig: Location, new: Location):
        return orig, new

    def is_empty(self, cyclic_moves: Set[Move] = None) -> bool:
        r""" Returns \p True if the \p MoveSet is empty """
        if cyclic_moves is not None and cyclic_moves:
            avail = set(self.avail.values())
            # If available larger than cyclic, definitely not empty move set
            if len(avail) > len(cyclic_moves): return False
            # Check if each available moves in cyclic.  If any not in there, not empty move set
            for a_m in avail:
                for c_m in cyclic_moves:
                    if Move.is_identical(a_m, c_m):
                        break
                else:
                    return False
            return True
        return not bool(self.avail)

    def __iter__(self):
        return iter(self.avail.values())

class Player:
    r""" Represents one of the two players """
    def __init__(self, color: Color):
        r"""
        :param color: Color of the player
        """
        self._color = color

        # noinspection PyTypeChecker
        self._move_set = None  # type: MoveSet
        self._locs = dict()
        self._pieces = set()

    @property
    def color(self) -> Color:
        r""" Accessor for the \p Player's \p Color. """
        return self._color

    @property
    def num_pieces(self) -> int:
        r""" Accessor for number of pieces the player has """
        return len(self._pieces)

    @property
    def move_set(self) -> MoveSet:
        r""" Accessor for the \p Player's \p MoveSet"""
        return self._move_set

    def add_piece(self, piece: Piece, other: 'Player' = None) -> None:
        r""" Add \p piece to \p Player's set of pieces """
        assert piece not in self._pieces, "Duplicate piece"
        assert piece.loc not in self._locs, "Two pieces in same location"

        self._pieces.add(piece)
        self._locs[piece.loc] = piece

        if other is not None:
            assert self._color != other.color
            self.move_set.add_piece(piece, self._locs, other._locs)

    def delete_piece_info(self, piece: Piece, other: 'Player') -> None:
        r""" Remove \p piece from the \p Player's set of pieces """
        self._pieces.remove(piece)
        del self._locs[piece.loc]
        self.move_set.del_piece(piece, self._locs, other._locs)

    def delete_moveset_info(self, loc: Location, other: 'Player') -> None:
        r""" Update the MoveSet information after deleting a piece at Location \p loc """
        assert self._color != other.color
        self.move_set.add_moves_after_delete(loc, self._locs, other._locs)

    def update_moveset_after_add(self, loc: Location, other: 'Player') -> None:
        r"""
        When adding a piece (i.e., moving it and placing it back down), some previously valid moves
        become blocked.  This method updates \p MoveSet to accomodate that.
        :param loc: \p Location where piece was placed
        :param other: Other player
        """
        assert self._color != other.color
        # pylint: disable=protected-access
        self.move_set.remove_moves_after_add(loc, self._locs, other._locs)

    def has_flag(self) -> bool:
        r""" Returns True if the player has a flag """
        flag = Rank.flag()
        return any(p.rank == flag for p in self._pieces)

    def get_piece_at_loc(self, loc: Location) -> Optional[Piece]:
        r""" Returns the piece at the specified location. If no piece is there, returns None """
        try: return self._locs[loc]
        except KeyError: return None

    def has_move(self, piece: Piece, new_loc: Location) -> bool:
        r""" Returns \p True if the player has a move for the piece ot the specified \p Location """
        assert piece is not None
        return self.move_set.has_move(piece, new_loc)

    def is_valid_next(self, m: Move) -> bool:
        r"""
        Checks whether move \m is in the player's \p MoveSet

        :param m: \p Move to check
        :return: True if \p m is a valid next move.
        """
        return m in self.move_set

    def get_move(self, piece: Piece, new_loc: Location) -> Optional[Move]:
        r""" Returns \p True if the player has a move for the piece ot the specified \p Location """
        assert piece is not None
        return self.move_set.get_move(piece, new_loc)

    def piece_locations(self) -> Set[Location]:
        r""" Location of all of the \p Player's pieces """
        set_locs = set(self._locs.keys())
        assert len(set_locs) == len(self._pieces)
        return set_locs

    def pieces(self) -> Generator[Piece, None, None]:
        r""" Generator that yields the Player's pieces """
        for p in self._pieces:
            yield p

    def build_move_set(self, other: 'Player'):
        r""" Construct the move set of the """
        assert self._color != other.color
        self._move_set = MoveSet.build(self._pieces, self._locs, other._locs)

    def verify_piece_set(self, piece_set: Board.PieceSet) -> bool:
        r"""
        Verify that the player piece information is compliance with the \p Board \p PieceSet
        :param piece_set: Piece set maximum counts
        :return: True if the player's piece set information is in compliance
        """
        pieces_by_rank = dict()
        # Count the number of pieces for each rank
        for p in self._pieces:
            try: pieces_by_rank[p.rank] += 1
            except KeyError: pieces_by_rank[p.rank] = 1
        res = True
        for r in Rank.get_all():
            if r in pieces_by_rank and pieces_by_rank[r] > piece_set.get_rank_count(r):
                logging.warning("Color %s has too many pieces of rank: \"%s\"", self._color.name, r)
                res = False
        return res

    def get_random_move(self) -> Move:
        r"""
        Selects a piece to move uniformly at random.  Then select the move from that piece's
        available moves uniformly at random.
        :return: Randomly selected move
        """
        move_dict = dict()
        keys = []
        for m in self.move_set.avail.values():
            try:
                move_dict[m.piece].append(m)
            except KeyError:
                keys.append(m.piece)
                move_dict[m.piece] = [m]

        key = random.choice(keys)
        return random.choice(move_dict[key])


"""tilltheend URL Configuration

The `urlpatterns` list routes URLs to views. For more information please see:
    https://docs.djangoproject.com/en/3.2/topics/http/urls/
Examples:
Function views
    1. Add an import:  from my_app import views
    2. Add a URL to urlpatterns:  path('', views.home, name='home')
Class-based views
    1. Add an import:  from other_app.views import Home
    2. Add a URL to urlpatterns:  path('', Home.as_view(), name='home')
Including another URLconf
    1. Import the include() function: from django.urls import include, path
    2. Add a URL to urlpatterns:  path('blog/', include('blog.urls'))
"""

from django.urls import path
from forever import views
from django.conf import settings
from django.conf.urls.static import static

urlpatterns = [
    path('login', views.login_request, name='login'),
    path('logout', views.logout_request, name='logout'),
    path('register', views.register, name='register'),
    path('', views.index, name='home'),
    path('todo', views.todoadd, name='todo'),
    path('translate', views.translate, name='translate'),
    path('texttospech', views.texttospech, name='texttospech'),
    path('qrcode', views.qrcode, name='qrcode'),
    path('weather', views.weather, name='weather'),
    path('download', views.download_video, name='download'),
    path('delete/<int:id>', views.delete, name='delete'),
    path('doing/<int:id>', views.doing, name='doing'),
    path('finish/<int:id>', views.finish, name='finish'),
    path('history/<int:id>', views.history, name='history'),
    path('news', views.news, name='news'),
]
if settings.DEBUG:
    urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)


#K-Nearesst Neighbour

#importing the Librares
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns

#set the index value as index_col 0
data=pd.read_csv('./Dataset/Classified Data',index_col=0)

#standardize the values
from sklearn.preprocessing import StandardScaler

#Create a object for StandardScaler
scaler=StandardScaler()

#fit and tranform the value to the standard values
scaler.fit(data.drop('TARGET CLASS',axis=1))
scaled_features=scaler.transform(data.drop('TARGET CLASS',axis=1))

#This scaled data doesn't have index name and column name
#store the scaled feature in a dataset

#columns will fil the columns index
#and also neglecting the target class because that is
#independent feature
df_feat=pd.DataFrame(scaled_features,columns=data.columns[:-1])

#sns.pairplot(data,hue='TARGET CLASS')

#Separate the train and test data
from sklearn.model_selection import train_test_split
#first is independent feature ->input dependent feature -> output
x_train,x_test,y_train,y_test=train_test_split(scaled_features,data['TARGET CLASS'])

#k_nearest Neighbour
from sklearn.neighbors import KNeighborsClassifier
#Giving K=1
knn = KNeighborsClassifier(n_neighbors=1)
knn.fit(x_train,y_train)

pred=knn.predict(x_test)

#use the confusion matrix to get the what are the values correctly perdicted
#and unpredicted

from sklearn.metrics import classification_report,confusion_matrix

#here you can see which are all the values g
print(confusion_matrix(pred,y_test))

#Give reports accuracy and other scores
print(classification_report(pred,y_test))

#Run the error rate find the point falls below and predict the K value

error_rate=[]

for i in range(1,40):
    knn=KNeighborsClassifier(n_neighbors=i)
    knn.fit(x_train,y_train)
    pred=knn.predict(x_test)
    error_rate.append(np.mean(pred!=y_test))

#plot the error rate vs K values for the error rate calculated
plt.figure(figsize=(10,6))

plt.plot(range(1,40),error_rate,linestyle="dashed",marker="o",markersize=10,
         markerfacecolor="red")

plt.title("Error Rate Graph")
plt.xlabel("K-value")
plt.ylabel("Error_Rate")

#here you can see that after k=24 it never touches back so choose that value

knn=KNeighborsClassifier(n_neighbors=24)
knn.fit(x_train,y_train)
pred=knn.predict(x_test)

#Confusion Matric
print(confusion_matrix(y_test,pred))
#classification report
print(classification_report(y_test,pred))

#You can see that accuracy has increased


####################
# Joint distribution of Ask/Bid Qty
####################

import os
import pickle
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from mpl_toolkits.mplot3d import axes3d

data_directory = 'data/xFGBL'
img_directory = 'images/'
data_file = 'xFGBL20130702.pkl'

for fn in os.listdir(data_directory):
    with open(os.path.join(data_directory, fn), 'rb') as input:
        r=pickle.load(input)

        X = r['AskQty']
        Y = r['BidQty']
        bins = np.arange(0, 600, 20)
        hist, xedges, yedges = np.histogram2d(Y, X, bins=bins, normed=True)

        fig = plt.figure()
        fig.suptitle(fn, fontsize=20)

        ax = fig.add_subplot(111, projection='3d')
        elements = (len(xedges) - 1) * (len(yedges) - 1)
        X, Y = np.meshgrid(xedges[:-1]+0.25, yedges[:-1]+0.25)
        ax.plot_wireframe(X, Y, hist)

        # xpos = X.flatten()
        # ypos = Y.flatten()
        # zpos =  np.zeros(elements)
        # dx =  10 * np.ones_like(zpos)
        # dy = dx.copy()
        # dz = hist.flatten()

        #ax.bar3d(xpos, ypos, zpos, dx, dy, dz, color='b', zsort='average')
        #ax.scatter(xpos, ypos, dz)

        #plt.show()
        plt.savefig(os.path.join(img_directory, fn + '.png'))


from django.core.exceptions import ValidationError
from django.core.validators import MaxValueValidator, MinValueValidator
from django.db import models
from django.utils.translation import gettext_lazy as _
from django.contrib.postgres.fields import ArrayField

from udemy.apps.core.models import TimeStampedBase, OrderedModel, CreatorBase
from udemy.apps.course.models import Course
from udemy.apps.module.models import Module
from udemy.apps.quiz.annotations import QuizAnnotations

class Quiz(TimeStampedBase, OrderedModel):
    title = models.CharField(_('Title'), max_length=200)
    description = models.TextField(_('Description'))
    is_published = models.BooleanField(default=False)
    is_draft = models.BooleanField(default=True)
    is_timed = models.BooleanField(default=False)
    pass_percent = models.PositiveIntegerField(validators=[MaxValueValidator(100)])
    module = models.ForeignKey(
        Module,
        related_name='quizzes',
        on_delete=models.CASCADE,
    )
    course = models.ForeignKey(
        Course,
        related_name='quizzes',
        on_delete=models.CASCADE,
    )
    order_in_respect = ('course', 'module')
    annotation_class = QuizAnnotations()

class Question(TimeStampedBase, OrderedModel):
    question = models.TextField()
    feedback = models.TextField()
    answers = ArrayField(models.TextField())
    max_time = models.PositiveIntegerField(default=0)
    quiz = models.ForeignKey(
        Quiz,
        related_name='questions',
        on_delete=models.CASCADE
    )
    course = models.ForeignKey(
        Course,
        related_name='questions_quiz',
        on_delete=models.CASCADE,
    )
    correct_response = models.IntegerField(validators=[MinValueValidator(1)])
    order_in_respect = ('quiz',)

    def save(self, *args, **kwargs):
        if self.correct_response > len(self.answers):
            raise ValidationError({'correct_response': 'invalid response'})
        super().save(*args, **kwargs)

class QuizRelation(CreatorBase, TimeStampedBase):
    quiz = models.ForeignKey(Quiz, on_delete=models.CASCADE)
    done = models.BooleanField(default=False)

    class Meta:
        constraints = [
            models.UniqueConstraint(fields=('creator', 'quiz'), name='unique quiz relation')]


#!/usr/bin/python
# -*- coding: UTF-8 -*-
import re,os,sys
import random
import argparse

parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument('--per', dest='per', type=int, default=10, help='ratio of test set (%)')
parser.add_argument('--file', dest='file', type=str, default='data_from_USPTO_utf8_converted_clear', help='input file')
args = parser.parse_args()
params = vars(args)
print(params)
file = params['file']
test_percent = params['per']

# select n% as test set
def select_file(fin,test_p):
	lines = open(fin,'r+').readlines()[1:] #remove the first title line and the last blank line
	writer1= open(fin+'_train', 'w')
	writer2= open(fin+'_test', 'w')
	all_num = len(lines)
	test_num = int(all_num*(test_p*0.01))

	print('all num: %d' %all_num)
	print('test num: %d' %test_num)
	print('train num: %d' %(all_num-test_num))
	print('slecting...')
	test_set = random.sample(lines, test_num)
	for item in test_set:
		lines.remove(item)
	print('selected')

	writer1.writelines(lines)
	writer2.writelines(test_set)

select_file(file, test_percent)


from string import ascii_lowercase, ascii_uppercase
from utils.data import read_data_as_list

characters = list(ascii_lowercase) + list(ascii_uppercase)
priority_lookup = dict(zip(characters, range(1, len(characters) + 1)))

rucksacks = read_data_as_list(day=3)

# Part 1
total = 0
for rucksack in rucksacks:
    midpoint = len(rucksack) // 2
    compartment_1, compartment_2 = rucksack[:midpoint], rucksack[midpoint:]
    common_item = set(compartment_1).intersection(compartment_2).pop()
    priority = priority_lookup[common_item]
    total += priority
print(f'Part 1 Solution: {total}')

# Part 2
total = 0
for i in range(0, len(rucksacks), 3):
    rucksack_1, rucksack_2, rucksack_3 = rucksacks[i: i+3]
    common_item = set(rucksack_1).intersection(rucksack_2).intersection(rucksack_3).pop()
    priority = priority_lookup[common_item]
    total += priority
print(f'Part 2 Solution: {total}')


# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import migrations

from ..pki.migrate_data import migrate_pki_data

class Migration(migrations.Migration):

    dependencies = [
        ('ssl_pki', '0002_default_config'),
    ]

    operations = [
        migrations.RunPython(migrate_pki_data, migrations.RunPython.noop),
    ]


import time
import os

from richxerox import *
from tinydb import TinyDB, where

HOME_DIR = 'static/db'
# Create directory if it doesn't exist
os.system("mkdir %s" % HOME_DIR)
db = TinyDB('%s/db.json' % HOME_DIR)
currently_found_in_clipboard = paste(format='text')
while True:
    time.sleep(0.1) # one tenth of a second
    if paste(format='text') != currently_found_in_clipboard:
        currently_found_in_clipboard = paste(format='text')
        # When the user hits CMD+C store the clipboard in a file and take a screenshot of the screen
        created_at = time.time()
        entry = { 'content': pasteall(), 'created_at': int(created_at),}
        entry['screenshot'] = '%s/screen%s.png' % (HOME_DIR, created_at)
        os.system("screencapture %s" % entry['screenshot'])
        db.insert(entry)


import numpy as np
import os
import Tools.FilesTool as FilesTool
import imblearn.over_sampling as over_sampling

class DataSetTool:
    # 08版的度量补偿
    # Mij in Target = (Mij in Target * Mean(Mj in Source)) / Mean(Mj) in  Target
    @staticmethod
    def metric_compensation(source, target):
        # 遍历每一个度量属性
        for j in range(target.shape[1]):
            # 计算每个度量属性的均值
            metric_mean_source = np.mean(source[:, j])
            metric_mean_target = np.mean(target[:, j])
            # 遍历每一个样例
            for i in range(target.shape[0]):
                target[i, j] = (target[i, j] * metric_mean_source) / metric_mean_target
        return target

    # 17版进行调整的度量补偿
    # Mij in Source = (Mij in Source * Mean(Mj in Target)) / Mean(Mj) in Source
    @staticmethod
    def metric_compensation_adopt(source, target):
        # 遍历每一个度量属性
        for j in range(source.shape[1]):
            # 计算每个度量属性的均值
            metric_mean_source = np.mean(source[:, j])
            metric_mean_target = np.mean(target[:, j])
            # 遍历每一个样例
            for i in range(source.shape[0]):
                source[i, j] = (source[i, j] * metric_mean_target) / metric_mean_source
        return source

    # 读取文件夹下的所有文件，并返回处理好的数据集
    # metrics_num 度量数目（原始数据中除开标签列的列数）
    # is_sample 是否重采样
    # is_normalized 是否数据归一化
    @staticmethod
    def init_data(folder_path, metrics_num, is_sample=True, is_normalized=True):
        # 获取目录下所有原始文件
        files = os.listdir(folder_path)
        data_list, label_list = [], []
        for file in files:
            # 每一个子文件的真实路径
            file_path = folder_path+file
            # txt文件
            if 'txt' == FilesTool.file_type(file) or 'TXT' == FilesTool.file_type(file):
                # 直接读取文件
                data_file = np.loadtxt(file_path, dtype=float, delimiter=',', usecols=range(0, metrics_num+1))
                label_file = np.loadtxt(file_path, dtype=float, delimiter=',', usecols=metrics_num+1)
                if is_normalized:
                    # 数据归一化
                    data_file -= data_file.min()
                    data_file /= data_file.max()
                    label_file -= label_file.min()
                    label_file /= label_file.max()
                # 加入列表
                data_list.append(data_file)
                label_list.append(label_file)
        # 重采样
        if is_sample:
            for index in range(len(data_list)):
                data_list[index], label_list[index] = over_sampling.SMOTE(kind='regular').fit_sample(data_list[index],
                                                                                label_list[index])
        return data_list, label_list


import torch
import torch.nn as nn
import torch.nn.functional as F
import numpy as np
import math
from detectron2.detectron2.layers import FrozenBatchNorm2d, ShapeSpec, get_norm
_NORM = 'BN'

class Conv2d_BN(nn.Module):
    """Convolution with BN module."""
    def __init__(
            self,
            in_ch,
            out_ch,
            kernel_size=1,
            stride=1,
            pad=0,
            dilation=1,
            groups=1,
            bn_weight_init=1,
            act_layer=None,
    ):
        super().__init__()
        self.in_ch = in_ch
        self.out_ch = out_ch
        self.conv = torch.nn.Conv2d(in_ch,
                                    out_ch,
                                    kernel_size,
                                    stride,
                                    pad,
                                    dilation,
                                    groups,
                                    bias=False
        )
        self.bn = get_norm(_NORM, out_ch)
        torch.nn.init.constant_(self.bn.weight, bn_weight_init)
        torch.nn.init.constant_(self.bn.bias, 0)
        for m in self.modules():
            if isinstance(m, nn.Conv2d):
                # Note that there is no bias due to BN
                fan_out = m.kernel_size[0] * m.kernel_size[1] * m.out_channels
                m.weight.data.normal_(mean=0.0, std=np.sqrt(2.0 / fan_out))

        self.act_layer = act_layer() if act_layer is not None else nn.Identity()

    def forward(self, x):
        # ain = self.in_ch
        # aout = self.out_ch

        x = self.conv(x)
        x = self.bn(x)
        x = self.act_layer(x)

        return x

class Dila_PRM(nn.Module):
    def __init__(self,in_embed,out_embed,kernel_size=4,downsample_ratio=1,dilations=[2,4,6],
                 fusion='cat'):
        super().__init__()

        self.dilations = dilations
        self.in_embed = in_embed
        # self.in_embeds=[self.in_embed,self.in_embed//2,self.in_embed//4]
        # self.out_embeds=[self.in_embed,self.in_embed//2,self.in_embed//4]
        self.out_embed = out_embed
        self.fusion = fusion
        self.kernel_size = kernel_size
        self.stride = downsample_ratio
        #self.out_size = img_size//downsample_ratio

        self.convs = nn.ModuleList(
            [
                nn.Sequential(
                    nn.Conv2d(
                        in_channels=self.in_embed,
                        out_channels=self.in_embed,
                        kernel_size=self.kernel_size,
                        stride=self.stride,
                        # padding=math.ceil(((self.kernel_size-1)*self.dilations[idx] + 1 - self.stride) / 2),
                        padding=math.ceil(((self.kernel_size-1)*self.dilations[idx])/2),
                        dilation=self.dilations[idx]),
                    # nn.BatchNorm2d(self.in_embed),
                    nn.GELU()
                ) for idx in range(len(self.dilations))
            ]
        )

        if self.fusion == 'cat':
            self.outchans = self.in_embed * len(self.dilations)
            '''这里可以改一改，不同尺度的特征维度尽量少'''
            #self.aggerate = Conv2d_BN(self.in_embed*len(self.dilations),self.in_embed,act_layer=nn.Hardswish)
            self.aggerate = Conv2d_BN(self.in_embed*len(self.dilations),self.out_embed,act_layer=nn.Hardswish)

    def forward(self,x):
        B,C,H,W = x.shape   #1,3,320,320

        out = self.convs[0](x).unsqueeze(dim=-1)
        for i in range(1,len(self.dilations)):
            cur_out = self.convs[i](x).unsqueeze(dim=-1)

            out = torch.cat((cur_out,out),dim=-1)

        B, C, W, H, N = out.shape
        #cur_size = (W,H)
        if self.fusion=='cat':
            out = out.permute(0,4,1,2,3).reshape(B,N*C,W,H)
            out = self.aggerate(out)
            # out = out.flatten(2).transpose(1,2) #B,N,C

        return out


"""
Tests for the server.

Before running them, the server database should be restarted.

Run as: python server/tests/test_all.py (don't use py.test as it does not pass env variables easily)
"""
import os
import shutil
from pathlib import Path
from typing import List

import pytest
from requests.exceptions import HTTPError

import numpy as np

from src.al_loop import LeadCompound
from server.app import WORKSHOP_ORACLES
from solutions.task1.random_loop import RandomLoop
from src.server_wrapper import FlaskAppClient

from rich.console import Console

console = Console()

PORT = int(os.environ.get("PORT", "5000"))
BASE_URL = "http://127.0.0.1:" + str(PORT)
BASE_URL = "http://mlinpl23.ngrok.io"
TEST_TOKEN_PREFIX = 'test-' # test-0, test-1, ...

def test_submitting_compounds_to_workshop_oracles():
    """Submits three simple molecules to the server using token test-0, to all workshop oracles."""
    client = FlaskAppClient(base_url=BASE_URL)
    token = TEST_TOKEN_PREFIX + '0'

    # Example for scoring compounds

    for oracle in WORKSHOP_ORACLES:
        compounds = ["CCCCCCCCC", "CCCCCCCC", "CCCCCC=O"]
        response = client.score_compounds_and_update_leaderboard(compounds, oracle, token)
        print(response)
        assert "metrics" in response
        assert "compound_scores" in response
        assert "compound_sas_scores" in response

def _run_random_exploration(protein, token="test-1", steps=10):
    """Simple random exploration of ZINC. Should get above >0.5 score on each oracle."""
    base_dir = Path("tmp")
    shutil.rmtree(base_dir, ignore_errors=True)
    client = FlaskAppClient(base_url=BASE_URL)
    loop = RandomLoop(base_dir=base_dir,
                          user_token=token,
                          target=protein)
    all_result: List[LeadCompound] = []
    budget_per_step = 100
    for step in range(steps):
        console.print(f"[red]Step {step}[/red]")
        candidates = loop.propose_candidates(budget_per_step)
        loop.test_in_lab_and_save(candidates, client=client)
        result: List[LeadCompound] = loop.load(iteration_id=step)
        all_result += result
        all_result_sorted = sorted(all_result, key=lambda x: x.activity, reverse=True)
        metrics = {"top10": np.mean([x.activity for x in all_result_sorted[:10]]),
                        "top10_synth": np.mean([x.synth_score for x in all_result_sorted[:10]])}
        console.log(metrics)

    return metrics

def test_random_exploration_gets_reasonable_score():
    for protein in [ 'GSK3β', 'DRD2_server', 'JNK3']:
        console.log("Testing: " + protein)
        metrics = _run_random_exploration(protein=protein)
        assert metrics['top10'] > 0.1, "Random search should identify reasonable compounds"

def test_leaderboard_ordering_and_user_names():
    _run_random_exploration('DRD2_server', 'test-2', steps=1)
    _run_random_exploration('DRD2_server', 'test-3', steps=1)
    client = FlaskAppClient(base_url=BASE_URL)
    all_results = client.all_results()
    users = [r['user'] for r in all_results]
    print(users)
    assert 'user-2' in users
    assert 'user-3' in users
    all_proteins = ['DRD2', 'JNK3', 'GSK3β']
    sums = [sum([all_results[0]['metrics'][p + "_top_10"]  for p in all_proteins]) for r in all_results]
    assert sums[0] == max(sums), "First result in the leaderboard should be the maximum sum of top10 scores"

def test_call_limits():
    base_dir = Path("tmp")
    token = 'test-10'
    shutil.rmtree(base_dir, ignore_errors=True)
    loop = RandomLoop(base_dir=base_dir,
                      user_token=token,
                      target='DRD2')
    client = FlaskAppClient(base_url=BASE_URL)
    # exhaust limit
    candidates = loop.propose_candidates(1000)
    loop.test_in_lab_and_save(candidates, client=client)
    # run one time more
    candidates = loop.propose_candidates(100)

    with pytest.raises(RuntimeError):
        client.score_compounds_and_update_leaderboard([c.smiles for c in candidates], user_token=token, oracle_name='DRD2')

def test_get_all_scores():
    base_dir = Path("tmp")
    token = 'test-40'
    shutil.rmtree(base_dir, ignore_errors=True)
    loop = RandomLoop(base_dir=base_dir,
                      user_token=token,
                      target='GSK3β_server')
    client = FlaskAppClient(base_url=BASE_URL)
    # exhaust limit
    candidates = loop.propose_candidates(100)
    candidates = loop.test_in_lab_and_save(candidates, client=client)
    # run one time more
    console.log(client.all_scores(token))
    assert len(client.all_scores(token)['compound_sas_scores']['GSK3β']) == len(candidates)
    assert len(client.all_scores(token)['compound_scores']['GSK3β']) == len(candidates)

if __name__ == "__main__":
    test_submitting_compounds_to_workshop_oracles()
    test_random_exploration_gets_reasonable_score()
    test_leaderboard_ordering_and_user_names()
    test_call_limits()
    test_get_all_scores()
    console.log("[green] Tests passed [/green]")


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

url = "http://localhost:6332"
headers = {'content-type': 'application/json'}

def get_result(payload):
    response = requests.post(
        url, data=json.dumps(payload), headers=headers).json()
    return json.dumps(response)

def get_all_address():
    payload = {
        "method": "getalladdress",
        "params": {},
        "jsonrpc": "2.0",
        "id": 1,
    }
    content = json.loads(get_result(payload))
    content=content["result"]
    address_arr=[]
    for (v) in content:
        address=v.split("-")[2]
        address_arr.append(address)
    return json.dumps(address_arr)

def get_balance():
    payload = {
        "method": "getbalance",
        "params": {},
        "jsonrpc": "2.0",
        "id": 1,
    }
    content = json.loads(get_result(payload))
    pending=(content["result"]["base"]["pending"])/1000000
    stable=(content["result"]["base"]["stable"])/1000000
    balance=pending+stable
    return json.dumps({"balance":balance,"pending":pending,"stable":stable})

def check_address(address):
    payload = {
        "method": "checkAddress",
        "params": [address],
        "jsonrpc": "2.0",
        "id": 1,
    }
    return get_result(payload)

def pay(address,amount,msg):
    if not msg:
        payload = {
            "method": "sendtoaddress",
            "params": [address,amount*1000000],
            "jsonrpc": "2.0",
            "id": 1,
        }
    else:
        payload = {
            "method": "sendtoaddresswithmessage",
            "params": [address,amount*1000000,msg],
            "jsonrpc": "2.0",
            "id": 1,
        }
    return get_result(payload)


from six.moves.urllib import parse
import tarfile

from lxml import etree

from atrope import exception

SPECS = {
    'http://www.vmware.com/interfaces/specifications/vmdk.html': 'vmdk',
    'https://people.gnome.org/~markmc/qcow-image-format.html': 'qcow',
}

def _get_tarfile(ova):
    if not tarfile.is_tarfile(ova):
        raise exception.CannotOpenFile(reason="not a valid 'tar' file")

    return tarfile.open(ova)

def extract_file(ova, filename):
    tf = _get_tarfile(ova)
    fd = tf.extractfile(filename)
    return fd

def get_disk_name(ovf):
    """Get the disk format and file name from a OVF descriptor."""
    root = etree.fromstring(ovf)
    ovf_ns = root.nsmap['ovf']

    id_attr = '{%s}id' % ovf_ns
    href_attr = '{%s}href' % ovf_ns
    files = {f.get(id_attr): f.get(href_attr) for f in
             root.findall('ovf:References/ovf:File', root.nsmap)}

    # we do not care about more than one disk
    disk = root.find('ovf:DiskSection/ovf:Disk', root.nsmap)
    if disk is not None:
        format_attr = '{%s}format' % ovf_ns
        fileref_attr = '{%s}fileRef' % ovf_ns
        ovf_format = disk.get(format_attr)
        if not ovf_format:
            raise Exception("Expecting some format!")
        (format_url, _) = parse.urldefrag(ovf_format)
        try:
            disk_format = SPECS[format_url]
        except KeyError:
            raise Exception("Unknown format!")
        try:
            disk_file = files[disk.get(fileref_attr)]
        except KeyError:
            raise Exception("Unknown disk!")
        return (disk_format, disk_file)
    return None, None

def get_ovf(ova):
    """Return an OVF descriptor as stored in an OVA file, if any."""
    tf = _get_tarfile(ova)

    ovf = None
    for name in tf.getnames():
        if name.endswith(".ovf"):
            ovf = tf.extractfile(name).read()
            break

    if ovf is None:
        raise exception.InvalidOVAFile(reason="cannot find a .ovf descriptor")
    return ovf


import os
import csv
import math
import numpy as np
import nltk
from nltk.corpus import stopwords
import collections
import string
import re
from sklearn.model_selection import KFold
from keras.models import Sequential
from keras.layers import Dense
from keras.layers import LSTM
from keras.layers.embeddings import Embedding
from keras.preprocessing import sequence
from keras.preprocessing.text import Tokenizer
import time

cachedStopWords = stopwords.words("english")

if __name__ == '__main__':
	print("Starting Main")
	startTime = time.time()
	x = []
	y = []
	fraud = []
	radical = []
	violence = []
	with open("annotPart1.csv",'r', encoding="utf8") as csvFile:
		reader = csv.reader(csvFile)
		p = 0
		for row in reader:
			if(len(row) == 5 and p != 0):
				x.append(row[1])
				temp = []
				temp.append(0 if row[2] == '0' else 1)
				temp.append(0 if row[3] == '0' else 1)
				temp.append(0 if row[4] == '0' else 1)
				fraud.append(0 if row[2] == '0' else 1)
				radical.append(0 if row[3] == '0' else 1)
				violence.append(0 if row[4] == '0' else 1)
				y.append(temp)
			p = p + 1
	csvFile.close
	with open("annot_part2.csv",'r', encoding="utf8") as csvFile:
		reader = csv.reader(csvFile)
		p = 0
		for row in reader:
			if(len(row) == 5 and p != 0):
				x.append(row[1])
				temp = []
				temp.append(0 if row[2] == '0' else 1)
				temp.append(0 if row[3] == '0' else 1)
				temp.append(0 if row[4] == '0' else 1)
				fraud.append(0 if row[2] == '0' else 1)
				radical.append(0 if row[3] == '0' else 1)
				violence.append(0 if row[4] == '0' else 1)
				y.append(temp)
			p = p + 1
	csvFile.close

	print("Size of x:",len(x)," Size of y:",len(y))
	X = []
	for t in x:
		t = re.sub(r'[^\w\s]',' ',t)
		t = ' '.join([word for word in t.split() if word != " "])
		t = t.lower()
		t = ' '.join([word for word in t.split() if word not in cachedStopWords])
		X.append(t)
	print("Type of X:",type(X))
	Features = X
	Fraud = fraud
	Radical = radical
	Violence = violence

	kf = KFold(n_splits=10)
	iteration = 0
	gFraudAccu = 0
	gRadicalAccu = 0
	gViolenceAccu = 0
	gTotalAccu = 0

	vocabSize = 50000
	tokenizer = Tokenizer(num_words= vocabSize)
	tokenised = tokenizer.fit_on_texts(X)

	for train_index, test_index in kf.split(Features):
		iteration += 1
		print("\n\n\n\nMaking nueral Network for iteration:",iteration)
		iterStart = time.time()

		#Making Training and Testing Data
		X_Train = [Features[x] for x in train_index]
		X_Test = [Features[x] for x in test_index]
		fraudTrain = [Fraud[x] for x in train_index]
		fraudTest = [Fraud[x] for x in test_index]
		radicalTrain = [Radical[x] for x in train_index]
		radicalTest = [Radical[x] for x in test_index]
		violenceTrain = [Violence[x] for x in train_index]
		violenceTest = [Violence[x] for x in test_index]

		tokenisedTrain = tokenizer.texts_to_sequences(X_Train)
		tokenisedTest = tokenizer.texts_to_sequences(X_Test)

		max_review_length = 180
		X_Train = sequence.pad_sequences(tokenisedTrain, maxlen=max_review_length,padding='post')
		X_Test = sequence.pad_sequences(tokenisedTest, maxlen=max_review_length,padding='post')

		#Fraud
		fraudModel = Sequential()
		fraudModel.add(Embedding(50000, 100, input_length=max_review_length))
		fraudModel.add(LSTM(100, dropout=0.2, recurrent_dropout=0.2))
		fraudModel.add(Dense(1, activation='sigmoid'))
		fraudModel.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
		fraudModel.fit(X_Train,fraudTrain,epochs=10, batch_size=100)
		fraudScore = fraudModel.evaluate(X_Test,fraudTest,verbose = 100)
		accuFraudLstm = fraudScore[1]
		fraudEndTime = time.time()
		print("\nFraud Training Done for Iteration",iteration,"\nTime:",fraudEndTime - iterStart)
		positiveFraud = [x for x in fraudTest if x == 1]
		print("Number of positive Examples : ",len(positiveFraud),  "  ratio : ", (len(positiveFraud) / len(fraudTest)) )

		#Radical
		radicalModel = Sequential()
		radicalModel.add(Embedding(50000, 100, input_length=max_review_length))
		radicalModel.add(LSTM(100, dropout=0.2, recurrent_dropout=0.2))
		radicalModel.add(Dense(1, activation='sigmoid'))
		radicalModel.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
		radicalModel.fit(X_Train,radicalTrain,epochs=10, batch_size=100)
		radicalScore = radicalModel.evaluate(X_Test,radicalTest,verbose = 100)
		accuRadicalLstm = radicalScore[1]
		radicalEndTime = time.time()
		print("\nRadical Training Done for Iteration",iteration,"\nTime:",radicalEndTime - fraudEndTime)
		positiveRadical = [x for x in radicalTest if x == 1]
		print("Number of positive Examples : ",len(positiveRadical),  "  ratio : ", (len(positiveRadical) / len(radicalTest)) )

		#Violence
		violenceModel = Sequential()
		violenceModel.add(Embedding(50000, 100, input_length=max_review_length))
		violenceModel.add(LSTM(100, dropout=0.2, recurrent_dropout=0.2))
		violenceModel.add(Dense(1, activation='sigmoid'))
		violenceModel.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
		violenceModel.fit(X_Train,violenceTrain,epochs=10, batch_size=100)
		violenceScore = violenceModel.evaluate(X_Test,violenceTest,verbose = 100)
		accuViolenceLstm = violenceScore[1]
		violenceEndTime = time.time()
		print("\nViolence Training Done for Iteration",iteration,"\nTime:",violenceEndTime - radicalEndTime)
		positiveViolence = [x for x in violenceTest if x == 1]
		print("Number of positive Examples : ",len(positiveViolence),  "  ratio : ", (len(positiveViolence) / len(violenceTest)) )

		totalAccu = (accuViolenceLstm + accuRadicalLstm + accuFraudLstm) / 3

		gFraudAccu += accuFraudLstm
		gViolenceAccu += accuViolenceLstm
		gRadicalAccu += accuRadicalLstm
		gTotalAccu += totalAccu

		iterEndTime = time.time()
		print("\n\nAccuracyScores for LSTM Iteration:",iteration,"\nFraud: ",accuFraudLstm,"\nRadical: ",accuRadicalLstm,"\nViolence: ",accuViolenceLstm,"\nTotal Accuracy:",totalAccu,"\nTotal Time:",iterEndTime - iterStart)

	gFraudAccu /= 10
	gViolenceAccu /= 10
	gRadicalAccu /= 10
	gTotalAccu /= 10
	endTime = time.time()

	print("\n\n\n\nOverall AccuracyScores for LSTM :","\nFraud: ",gFraudAccu,"\nRadical: ",gRadicalAccu,"\nViolence: ",gViolenceAccu,"\nTotal Accuracy:",gTotalAccu,"\nTime:",endTime - startTime)


import tensorflow as tf
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
from scipy.interpolate import griddata
import pandas as pd
from NS_model_tf import Sampler, Navier_Stokes2D

if __name__ == '__main__':
    def U_gamma_1(x):
        num = x.shape[0]
        return np.tile(np.array([1.0, 0.0]), (num, 1))

    def U_gamma_2(x):
        num = x.shape[0]
        return np.zeros((num, 2))

    def f(x):
        num = x.shape[0]
        return np.zeros((num, 2))

    def operator(psi, p, x, y, Re, sigma_x=1.0, sigma_y=1.0):
        u = tf.gradients(psi, y)[0] / sigma_y
        v = - tf.gradients(psi, x)[0] / sigma_x

        u_x = tf.gradients(u, x)[0] / sigma_x
        u_y = tf.gradients(u, y)[0] / sigma_y

        v_x = tf.gradients(v, x)[0] / sigma_x
        v_y = tf.gradients(v, y)[0] / sigma_y

        p_x = tf.gradients(p, x)[0] / sigma_x
        p_y = tf.gradients(p, y)[0] / sigma_y

        u_xx = tf.gradients(u_x, x)[0] / sigma_x
        u_yy = tf.gradients(u_y, y)[0] / sigma_y

        v_xx = tf.gradients(v_x, x)[0] / sigma_x
        v_yy = tf.gradients(v_y, y)[0] / sigma_y

        Ru_momentum = u * u_x + v * u_y + p_x - (u_xx + u_yy) / Re
        Rv_momentum = u * v_x + v * v_y + p_y - (v_xx + v_yy) / Re

        return Ru_momentum, Rv_momentum

    # Parameters of equations
    Re = 100.0

    # Domain boundaries
    bc1_coords = np.array([[0.0, 1.0],
                           [1.0, 1.0]])
    bc2_coords = np.array([[0.0, 0.0],
                           [0.0, 1.0]])
    bc3_coords = np.array([[1.0, 0.0],
                           [1.0, 1.0]])
    bc4_coords = np.array([[0.0, 0.0],
                           [1.0, 0.0]])
    dom_coords = np.array([[0.0, 0.0],
                           [1.0, 1.0]])

    # Create boundary conditions samplers
    bc1 = Sampler(2, bc1_coords, lambda x: U_gamma_1(x), name='Dirichlet BC1')
    bc2 = Sampler(2, bc2_coords, lambda x: U_gamma_2(x), name='Dirichlet BC2')
    bc3 = Sampler(2, bc3_coords, lambda x: U_gamma_2(x), name='Dirichlet BC3')
    bc4 = Sampler(2, bc4_coords, lambda x: U_gamma_2(x), name='Dirichlet BC4')
    bcs_sampler = [bc1, bc2, bc3, bc4]

    # Create residual sampler
    res_sampler = Sampler(2, dom_coords, lambda x: f(x), name='Forcing')

    # Define model
    mode = 'M1'
    layers = [2, 50, 50, 50, 2]

    model = Navier_Stokes2D(layers, operator, bcs_sampler, res_sampler, Re, mode)

    # Train model
    model.train(nIter=40001, batch_size=128)

    # Test Data
    nx = 100
    ny = 100  # change to 100
    x = np.linspace(0.0, 1.0, nx)
    y = np.linspace(0.0, 1.0, ny)
    X, Y = np.meshgrid(x, y)

    X_star = np.hstack((X.flatten()[:, None], Y.flatten()[:, None]))

    # Predictions
    psi_pred, p_pred = model.predict_psi_p(X_star)
    u_pred, v_pred = model.predict_uv(X_star)

    psi_star = griddata(X_star, psi_pred.flatten(), (X, Y), method='cubic')
    p_star = griddata(X_star, p_pred.flatten(), (X, Y), method='cubic')
    u_star = griddata(X_star, u_pred.flatten(), (X, Y), method='cubic')
    v_star = griddata(X_star, v_pred.flatten(), (X, Y), method='cubic')

    velocity = np.sqrt(u_pred**2 + v_pred**2)
    velocity_star = griddata(X_star, velocity.flatten(), (X, Y), method='cubic')

    # Reference
    u_ref= np.genfromtxt("reference_u.csv", delimiter=',')
    v_ref= np.genfromtxt("reference_v.csv", delimiter=',')
    velocity_ref = np.sqrt(u_ref**2 + v_ref**2)

    # Relative error
    error = np.linalg.norm(velocity_star - velocity_ref.T, 2) / np.linalg.norm(velocity_ref, 2)
    print('l2 error: {:.2e}'.format(error))

    ### Plot ###
    ###########

    # Reference solution & Prediceted solution
    fig_1 = plt.figure(1, figsize=(18, 5))
    fig_1.add_subplot(1, 3, 1)
    plt.pcolor(X.T, Y.T, velocity_ref, cmap='jet')
    plt.colorbar()
    plt.xlabel('x')
    plt.ylabel('y')
    plt.title('Reference Velocity')

    fig_1.add_subplot(1, 3, 2)
    plt.pcolor(x, Y, velocity_star, cmap='jet')
    plt.colorbar()
    plt.xlabel('x')
    plt.ylabel('y')
    plt.title('Predicted Velocity')
    plt.tight_layout()

    fig_1.add_subplot(1, 3, 3)
    plt.pcolor(X, Y, np.abs(velocity_star - velocity_ref.T), cmap='jet')
    plt.colorbar()
    plt.xlabel('x')
    plt.ylabel('y')
    plt.title('Absolute Error')
    plt.show()

     ## Loss ##
    loss_res = model.loss_res_log
    loss_bcs = model.loss_bcs_log

    fig_2 = plt.figure(2)
    ax = fig_2.add_subplot(1, 1, 1)
    ax.plot(loss_res, label='$\mathcal{L}_{r}$')
    ax.plot(loss_bcs, label='$\mathcal{L}_{u_b}$')
    ax.set_yscale('log')
    ax.set_xlabel('iterations')
    ax.set_ylabel('Loss')
    plt.legend()
    plt.tight_layout()
    plt.show()

    ## Adaptive Constant
    adaptive_constant = model.adpative_constant_bcs_log

    fig_3 = plt.figure(3)
    ax = fig_3.add_subplot(1, 1, 1)
    ax.plot(adaptive_constant, label='$\lambda_{u_b}$')
    ax.set_xlabel('iterations')
    plt.legend()
    plt.tight_layout()
    plt.show()

    ## Gradients #
    data_gradients_res = model.dict_gradients_res_layers
    data_gradients_bcs = model.dict_gradients_bcs_layers

    num_hidden_layers = len(layers) -1
    cnt = 1
    fig_4 = plt.figure(4, figsize=(13, 4))
    for j in range(num_hidden_layers):
        ax = plt.subplot(1, 4, cnt)
        ax.set_title('Layer {}'.format(j + 1))
        ax.set_yscale('symlog')
        gradients_res = data_gradients_res['layer_' + str(j + 1)][-1]
        gradients_bcs = data_gradients_bcs['layer_' + str(j + 1)][-1]

        sns.distplot(gradients_res, hist=False,
                     kde_kws={"shade": False},
                     norm_hist=True, label=r'$\nabla_\theta \mathcal{L}_r$')
        sns.distplot(gradients_bcs, hist=False,
                     kde_kws={"shade": False},
                     norm_hist=True, label=r'$\nabla_\theta \mathcal{L}_{u_b}$')

        ax.get_legend().remove()
        ax.set_xlim([-1.0, 1.0])
        ax.set_ylim([0, 100])
        cnt += 1
    handles, labels = ax.get_legend_handles_labels()

    fig_4.legend(handles, labels, loc="upper left", bbox_to_anchor=(0.35, -0.01),
               borderaxespad=0, bbox_transform=fig_4.transFigure, ncol=2)
    plt.tight_layout()
    plt.show()


from __future__ import division

from vistrails.core.modules.vistrails_module import Module

from ..common import get_numpy
from ..read.read_numpy import NumPyArray

class WriteNumPy(Module):
    """Writes a list as a Numpy file.

    NumPy can use one of two schemes: either 'plain' binary arrays, i.e. just
    the binary representation of the data format (in this case you must specify
    the exact format to get the original data back), or the NPY format, i.e.
    .npy files that know what the actual structure of the array is.
    """
    _input_ports = [
            ('array', '(org.vistrails.vistrails.basic:List)'),
            ('datatype', '(org.vistrails.vistrails.basic:String)',
             {'entry_types': "['enum']",
              'values': "[%r]" % NumPyArray.FORMATS})]
    _output_ports = [('file', '(org.vistrails.vistrails.basic:File)')]

    def compute(self):
        numpy = get_numpy()

        array = self.get_input('array')
        if not isinstance(array, numpy.ndarray):
            array = numpy.array(array)
        dtype = NumPyArray.get_format(self.get_input('datatype'))

        if dtype is NumPyArray.NPY_FMT:
            fileobj = self.interpreter.filePool.create_file(suffix='.npy')
            fname = fileobj.name

            # Numpy's ".NPY" format
            numpy.save(fname, array)
        else:
            fileobj = self.interpreter.filePool.create_file(suffix='.dat')
            fname = fileobj.name

            # Numpy's plain binary format
            array.astype(dtype).tofile(fname)

        self.set_output('file', fileobj)

_modules = [WriteNumPy]

###############################################################################

import unittest

class WriteNumpyTestCase(unittest.TestCase):
    def test_raw_numpy(self):
        """Uses WriteNumPy to write an array in raw format.
        """
        import array
        from vistrails.tests.utils import execute, intercept_result
        from ..identifiers import identifier
        with intercept_result(WriteNumPy, 'file') as results:
            self.assertFalse(execute([
                    ('write|WriteNumPy', identifier, [
                        ('array', [('List', '[0, 1, 258, 6758]')]),
                        ('datatype', [('String', 'uint32')]),
                    ]),
                ]))
        self.assertEqual(len(results), 1)
        expected_bytes = [0, 0, 0, 0,
                          1, 0, 0, 0,
                          2, 1, 0, 0,
                          102, 26, 0, 0]
        with open(results[0].name, 'rb') as fp:
            self.assertEqual(fp.read(),
                             array.array('B', expected_bytes).tostring())

    def test_npy_numpy(self):
        """Uses WriteNumPy to write an array in .NPY format.
        """
        import numpy
        from vistrails.tests.utils import execute, intercept_result
        from ..identifiers import identifier
        with intercept_result(WriteNumPy, 'file') as results:
            self.assertFalse(execute([
                    ('write|WriteNumPy', identifier, [
                        ('array', [('List', '[0, 1, 258, 6758]')]),
                        ('datatype', [('String', 'npy')]),
                    ]),
                ]))
        self.assertEqual(len(results), 1)
        self.assertEqual(list(numpy.load(results[0].name)), [0, 1, 258, 6758])

    def test_write_read(self):
        """Uses WriteNumPy and NumPyArray to write then read an array.
        """
        from vistrails.tests.utils import execute, intercept_result
        from ..identifiers import identifier
        for dtype in ('npy', 'uint32'):
            with intercept_result(NumPyArray, 'value') as results:
                self.assertFalse(execute([
                        ('write|WriteNumPy', identifier, [
                            ('array', [('List', '[0, 1, 258, 6758]')]),
                            ('datatype', [('String', dtype)]),
                        ]),
                        ('read|NumPyArray', identifier, [
                            ('datatype', [('String', dtype)]),
                        ]),
                    ], [
                        (0, 'file', 1, 'file'),
                    ]))
            self.assertEqual(len(results), 1)
            self.assertEqual(list(results[0]), [0, 1, 258, 6758])


import collections
import random
import unittest

import mock

from cardboard import card as c, events, zone as z
from cardboard.tests.util import GameTestCase
from cardboard.util import ANY

ENTER, LEAVE = events.ENTERED_ZONE, events.LEFT_ZONE

class ZoneTest(GameTestCase):

    card = mock.Mock(spec=c.Card)

    def setUp(self):
        super(ZoneTest, self).setUp()
        self.u = z.UnorderedZone(
            name="Emerald Hill", game=self.game, contents=self.library,
        )

        self.o = z.OrderedZone(
            name="Casino Night", game=self.game, contents=self.library,
        )

class TestZones(ZoneTest):
    def test_name(self):
        self.assertEqual(self.u.name, "Emerald Hill")
        self.assertEqual(self.o.name, "Casino Night")

    def test_ordered(self):
        self.assertFalse(self.u.ordered)
        self.assertTrue(self.o.ordered)

    def test_str_repr(self):
        self.assertEqual(str(self.u), "Emerald Hill")
        self.assertEqual(str(self.o), "Casino Night")

        self.assertEqual(repr(self.u), "<Zone: Emerald Hill>")
        self.assertEqual(repr(self.o), "<Zone: Casino Night>")

    def test_contains(self):
        for i in self.library:
            self.assertIn(i, self.u)
            self.assertIn(i, self.o)

        self.assertNotIn(object(), self.u)
        self.assertNotIn(object(), self.o)

    def test_iter(self):
        self.assertEqual(set(self.u), set(self.library))
        self.assertEqual(list(self.o), self.library)

    def test_len(self):
        self.assertEqual(len(self.u), len(self.library))
        self.assertEqual(len(self.o), len(self.library))

    def test_add(self):
        with self.assertTriggers(event=ENTER, card=30, zone=self.u):
            self.u.add(30)

        with self.assertTriggers(event=ENTER, card=30, zone=self.o):
            self.o.add(30)

        self.assertEqual(set(self.u), set(self.library) | {30})
        self.assertEqual(list(self.o), self.library + [30])

    def test_add_already_contains(self):
        NO_OWNER, OWNER = "on the {}", "in {}'s {}"
        u, o = self.u.name, self.o.name

        n = mock.Mock()
        self.u.add(n)
        self.o.add(n)

        self.resetEvents()

        with self.assertRaisesRegexp(ValueError, NO_OWNER.format(u)):
            self.u.add(n)

        with self.assertRaisesRegexp(ValueError, NO_OWNER.format(o)):
            self.o.add(n)

        with self.assertRaisesRegexp(ValueError, OWNER.format(n.owner, u)):
            self.u.owner = n.owner
            self.u.add(n)

        with self.assertRaisesRegexp(ValueError, OWNER.format(n.owner, o)):
            self.o.owner = n.owner
            self.o.add(n)

        # wasn't added twice nor removed
        self.assertIn(self.library[0], self.u)
        self.assertEqual(self.o.count(self.library[0]), 1)

        self.assertFalse(self.events.trigger.called)

    def test_add_owner_redirection(self):
        """
        Adding a card with a different owner than the zone's redirects.

        """

        card = mock.Mock()

        self.u.name, self.o.name = "foo", "bar"
        self.u.owner, self.o.owner = mock.Mock(), mock.Mock()

        self.u.add(card)
        self.o.add(card)

        card.owner.foo.add.assert_called_once_with(card)
        card.owner.bar.add.assert_called_once_with(card)

    def test_move(self):
        self.o.add(self.card)
        self.card.zone = self.o  # on actual cards this is a property

        with self.assertTriggers(event=ENTER, card=self.card, zone=self.u):
            self.u.move(self.card)
            self.card.zone = self.u

        self.assertIn(self.card, self.u)

        with self.assertTriggers(event=ENTER, card=self.card, zone=self.o):
            self.o.move(self.card)

        self.assertIn(self.card, self.o)

    def test_move_to_self(self):
        self.resetEvents()

        # shouldn't even be checking library[0].zone
        with self.assertRaises(ValueError):
            self.u.move(self.library[0])

        with self.assertRaises(ValueError):
            self.o.move(self.library[0])

        # wasn't added twice nor removed
        self.assertIn(self.library[0], self.u)
        self.assertEqual(self.o.count(self.library[0]), 1)

        self.assertFalse(self.events.trigger.called)

    def test_pop(self):
        self.resetEvents()
        e = self.u.pop()
        self.assertLastEventsWere([dict(event=LEAVE, card=e, zone=self.u)])

        self.resetEvents()
        f = self.o.pop()
        self.assertLastEventsWere([dict(event=LEAVE, card=f, zone=self.o)])

        self.assertEqual(set(self.u), set(self.library) - {e})
        self.assertEqual(list(self.o), self.library[:-1])

    def test_remove(self):
        e = self.library[-7]
        self.library.remove(e)

        with self.assertTriggers(event=LEAVE, card=e, zone=self.u):
            self.u.remove(e)

        with self.assertTriggers(event=LEAVE, card=e, zone=self.o):
            self.o.remove(e)

        self.assertEqual(set(self.u), set(self.library))
        self.assertEqual(list(self.o), self.library)

        self.assertRaises(ValueError, self.u.remove, object())
        self.assertRaises(ValueError, self.o.remove, object())

    def test_update(self):
        self.u.update(range(4))

        for i in range(4):
            self.assertIn(i, self.u)

        self.assertEqual(len(self.u), len(self.library) + 4)

        evs = [dict(event=ENTER, card=i, zone=self.u) for i in range(4)]
        self.assertLastEventsWere(evs)

        self.resetEvents()

        self.o.update(range(4))
        self.assertEqual(self.o[-4:], range(4))

        self.assertEqual(len(self.o), len(self.library) + 4)

        evs = [dict(event=ENTER, card=i, zone=self.o) for i in range(4)]
        self.assertLastEventsWere(evs)

    def test_silent(self):
        self.o.add(self.card)
        self.card.zone = self.o
        self.resetEvents()

        self.u.add(20, silent=True)
        self.o.add(20, silent=True)

        self.u.remove(self.library[0], silent=True)
        self.o.remove(self.library[0], silent=True)

        self.u.pop(silent=True)
        self.o.pop(silent=True)

        self.u.move(self.card, silent=True)
        self.card.zone = self.u
        self.o.move(self.card, silent=True)

        self.u.update(range(10), silent=True)
        self.o.update(range(10), silent=True)

        self.assertFalse(self.events.trigger.called)

    def test_iterable(self):
        i = range(10)

        # TODO: This is incomplete, all the methods don't take iterables
        o = z.OrderedZone(game=None, name="Emerald Hill", contents=i)
        u = z.UnorderedZone(game=None, name="Emerald Hill", contents=i)

        i.pop()

        self.assertEqual(list(o), range(10))
        self.assertEqual(list(u), range(10))

class TestOrderedZone(ZoneTest):
    def test_reversed(self):
        self.assertEqual(list(reversed(self.o)), list(reversed(self.library)))

    def test_getitem(self):
        for i, e in enumerate(self.library):
            self.assertEqual(self.o[i], e)

        self.assertEqual(self.o[2:7:2], self.library[2:7:2])

    def test_set_del_item(self):
        self.assertRaises(AttributeError, getattr, self.o, "__setitem__")
        self.assertRaises(AttributeError, getattr, self.o, "__delitem__")

    def test_count(self):
        o = z.OrderedZone(game=None, name="Emerald Hill",
                          contents=[1, 1, 1, 2, 2, 3])

        for i, e in enumerate(range(3, 0, -1), 1):
            self.assertEqual(o.count(e), i)

    def test_index(self):
        e = self.library[4]
        self.assertEqual(self.o.index(e), 4)

    def test_pop_index(self):
        e1 = self.o.pop(0)
        e2 = self.o.pop(4)

        self.library.pop(0)
        self.library.pop(4)

        self.assertEqual(list(self.o), self.library)
        self.assertLastEventsWere([
            {"event" : LEAVE, "card" : e1, "zone" : self.o},
            {"event" : LEAVE, "card" : e2, "zone" : self.o},
        ])

    def test_reverse(self):
        self.o.reverse()
        self.assertEqual(list(self.o), list(reversed(self.library)))

    def test_shuffle(self):
        with mock.patch("cardboard.zone.random.shuffle") as shuffle:
            self.o.shuffle()

        shuffle.assert_called_once_with(self.o._order)

class TestZone(unittest.TestCase):
    def test_zone(self):
        c = mock.Mock()

        for zone in ["battlefield", "exile", "hand"]:
            n = z.zone[zone](game=None, contents=[c])
            self.assertIsInstance(n, z.UnorderedZone)
            self.assertEquals(n.name, zone)
            self.assertIn(c, n)

        for zone in ["graveyard", "library", "stack"]:
            n = z.zone[zone](game=None, contents=[c])
            self.assertIsInstance(n, z.OrderedZone)
            self.assertEquals(n.name, zone)
            self.assertIn(c, n)


from flask import current_app, Blueprint,request, jsonify
from vpnresolve import VPNResolve
import json
import logging

logger = logging.getLogger( "ucn_logger" )

ios_api = Blueprint('ios_api', __name__)

@ios_api.route("/viz/ios/log", methods=['POST'])
def log():

	vpnres = VPNResolve(current_app.config["CIDR"], {"db":current_app.config["MONGODB"],"collection":current_app.config["VPNLOGSCOLLECTION"],"host":current_app.config["MONGOHOST"], "port":current_app.config["MONGOPORT"]})

	host = vpnres.clientip(request)

	if host is None:
		return jsonify(success="False")

	logger.debug("saving ios data for host %s", host)

	data = request.get_json(force=False)

	logger.debug("received data for host %s" % host)

	#shove the processes into the table in bulk!
	success= True

	if 'processes' in data:
		logger.debug("saving ios process data for host %s", host)
		success = current_app.config["datadb"].bulk_insert_processes(host,data['processes'])
		if success:
			logger.debug("sucessfully saved ios process data for host %s", host)
		else:
			logger.error("failed to save ios process data")

	if 'network' in data:
		logger.debug("saving ios network for host %s", host)
		success = success and current_app.config["datadb"].insert_network_data(host, data['network'])
		if success:
			logger.debug("sucessfully saved ios network data for host %s", host)
		else:
			logger.error("failed to save ios network data")
			logger.error(data['network'])

	return jsonify(success= "True" if success else "False")


from io import TextIOWrapper
import os
import argparse

files = [
    'Accurect-Pointer.txt',
    'Endonasal-RII.txt',
    'HeadBand-Reference.txt',
    'Navigation-Pointer.txt',
    'Registration-Pointer.txt'
]

def readFromOriginalFormat(file: TextIOWrapper):
    lines = file.readlines()
    for i, line in enumerate(lines):
        if line.startswith('Num Markers:'):
            numMarkers = int(line.split(':')[1].strip())
        if line.startswith('Marker Positions'):
            break

    data = lines[i+2:i+numMarkers+2]
    data = [float(x) for line in data  for x in line.split()]
    return data, numMarkers

def writeToNewFormat(data: list, numMarkers: int, file: TextIOWrapper):
    file.write(str(numMarkers) + '\n')
    for i in range(0, numMarkers*3, 3):
        file.write('{} {} {}\n'.format(data[i], data[i+1], data[i+2]))
    file.write('\n')

if __name__ == '__main__':
    argparser = argparse.ArgumentParser()
    argparser.add_argument('-d', '--directory', help='root directory of the files', required=True)

    args = argparser.parse_args()

    if not os.path.exists(args.directory):
        os.mkdir(f'{args.directory}/converted/')

    for file in files:
        with open(f'{args.directory}/{file}', 'r') as f:
            data, numMarkers = readFromOriginalFormat(f)

        with open(f'{args.directory}/converted/{file}', 'w') as f:
            writeToNewFormat(data, numMarkers, f)
            print(f'{file} converted')


import numpy as np

from sklearn.tree import DecisionTreeClassifier
from .sampler import FeatureSampler, ObjectSampler

class Bagger:
    def __init__(self, base_estimator, object_sampler, feature_sampler, n_estimators=10, **params):
        """
        n_estimators : int
            number of base estimators
        base_estimator : class
            class for base_estimator with fit(), predict() and predict_proba() methods
        feature_sampler : instance of FeatureSampler
        object_sampler : instance of ObjectSampler
        n_estimators : int
            number of base_estimators
        params : kwargs
            params for base_estimator initialization
        """
        self.n_estimators = n_estimators
        self.base_estimator = base_estimator
        self.feature_sampler = feature_sampler
        self.object_sampler = object_sampler
        self.estimators = []
        self.indices = []
        self.params = params

    def fit(self, X, y):
        """
        for i in range(self.n_estimators):
            1) select random objects and answers for train
            2) select random indices of features for current estimator
            3) fit base_estimator (don't forget to remain only selected features)
            4) save base_estimator (self.estimators) and feature indices (self.indices)

        NOTE that self.base_estimator is class and you should init it with
        self.base_estimator(**self.params) before fitting
        """
        self.estimators = []
        self.indices = []

        for i in range(self.n_estimators):
            X_sampled, y_sampled = self.object_sampler.sample(X, y)
            feature_indices = self.feature_sampler.sample_indices(X.shape[1])

            estimator = self.base_estimator(**self.params)
            estimator.fit(X_sampled[:, feature_indices], y_sampled)

            self.estimators.append(estimator)
            self.indices.append(feature_indices)

        return self

    def predict_proba(self, X):
        """
        Returns
        -------
        probas : numpy ndarrays of shape (n_objects, n_classes)

        Calculate mean value of all probas from base_estimators
        Don't forget, that each estimator has its own feature indices for prediction
        """
        if not (0 < len(self.estimators) == len(self.indices)):
            raise RuntimeError('Bagger is not fitted', (len(self.estimators), len(self.indices)))

        predicts = []
        for estimator, feature_indices in zip(self.estimators, self.indices):
            predict = estimator.predict_proba(X[:, feature_indices])
            predicts.append(predict)
        return np.mean(np.array(predicts), axis=0)

    def predict(self, X):
        """
        Returns
        -------
        predictions : numpy ndarrays of shape (n_objects, )
        """
        return np.argmax(self.predict_proba(X), axis=1)

class RandomForestClassifier(Bagger):
    def __init__(self, n_estimators=30, max_objects_samples=0.9, max_features_samples=0.8,
                 max_depth=None, min_samples_leaf=1, random_state=None, **params):
        base_estimator = DecisionTreeClassifier
        object_sampler = ObjectSampler(max_samples=max_objects_samples, random_state=random_state)
        feature_sampler = FeatureSampler(max_samples=max_features_samples, random_state=random_state)

        super().__init__(
            base_estimator=base_estimator,
            object_sampler=object_sampler,
            feature_sampler=feature_sampler,
            n_estimators=n_estimators,
            max_depth=max_depth,
            min_samples_leaf=min_samples_leaf,
            **params,
        )


import calendar
from datetime import datetime

class Util:
    DATE_FORMAT = '%Y-%m-%d'

    def get_month_start_date(datetime):
        return datetime.date().replace(day=1)

    def get_month_end_date(datetime):
        year = datetime.year
        month = datetime.month
        monthrange = calendar.monthrange(year, month)
        return datetime.date().replace(day=monthrange[1])

class CalendarEvent:

    def __init__(self, date, event):
        self.date = date
        self.event = event

class HTMLEventCalendar(calendar.HTMLCalendar):
    # CSS classes for the day s
    cssclasses = ["mon", "tue", "wed", "thu", "fri", "sat", "sun"]

    # CSS classes for the day s
    cssclasses_weekday_head = cssclasses

    # CSS class for the days before and after current month
    cssclass_noday = "noday"

    # CSS class for the month's head
    cssclass_month_head = "month"

    # CSS class for the month
    cssclass_month = "month"

    # CSS class for the year's table head
    cssclass_year_head = "year"

    # CSS class for the whole year table
    cssclass_year = "year"

    cssclass_event = "calendar-event"

    cssclass_day_number = "day-number"

    def __init__(self, firstweekday=calendar.MONDAY, events={}):
        super().__init__(firstweekday)
        self.events = events

    def get_event(self, day, month, year):
        date = datetime.strptime("{}-{}-{}".format(year, month, day),
                                 Util.DATE_FORMAT)
        date_string = date.strftime(Util.DATE_FORMAT)
        return self.events.get(date_string, '')

    def formatday(self, day, weekday, themonth=None, theyear=None):
        """
        Return a day as a table cell.
        """
        if day == 0:
            # day outside month
            return ' 	' % self.cssclass_noday
        else:
            event = self.get_event(day, themonth, theyear)
            html = """

%s

%d

            	""" % (self.cssclasses[weekday],
                        self.cssclass_event, event,
                        self.cssclass_day_number, day)
            return html

    def formatweek(self, theweek, themonth=None, theyear=None):
        """
        Return a complete week as a table row.
        """
        s = ''.join(self.formatday(d, wd, themonth, theyear)
                    for (d, wd) in theweek)
        return '
%s
' % s

    def formatmonth(self, theyear, themonth, withyear=True):
        """
        Return a formatted month as a table.
        """
        v = []
        a = v.append
        a('
' % (
            self.cssclass_month))
        a('\n')
        a(self.formatmonthname(theyear, themonth, withyear=withyear))
        a('\n')
        a(self.formatweekheader())
        a('\n')
        for week in self.monthdays2calendar(theyear, themonth):
            a(self.formatweek(week, theyear=theyear, themonth=themonth))
            a('\n')
        a('
')
        a('\n')
        return ''.join(v)