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

from base_session_factory import BaseDialogSessionFactory
from simple_dialog_session import SimpleDialogSession

class SimpleDialogSessionFactory(BaseDialogSessionFactory):
    """
    Простейшее хранилище.
    """
    def __init__(self, facts_storage):
        super(SimpleDialogSessionFactory, self).__init__()
        self.sessions = dict()
        self.facts_storage = facts_storage

    def __getitem__(self, interlocutor):
        assert(interlocutor is not None and len(interlocutor)!=0)
        if interlocutor not in self.sessions:
            # Создаем новую сессию для этого пользователя
            self.sessions[interlocutor] = SimpleDialogSession(interlocutor, self.facts_storage )

        return self.sessions[interlocutor]


import os
import warnings
import sys

import pandas as pd
import numpy as np
from sklearn.preprocessing import LabelEncoder, StandardScaler
from sklearn.metrics import precision_recall_fscore_support as score
from sklearn.metrics import accuracy_score
from sklearn.model_selection import train_test_split

from sklearn.preprocessing import StandardScaler
from sklearn.ensemble import RandomForestClassifier

import mlflow
import mlflow.sklearn

from preprocessing_data import preprocessing_train, preprocessing_test

import logging
logging.basicConfig(level=logging.WARN)
logger = logging.getLogger(__name__)

if __name__ == "__main__":

    warnings.filterwarnings("ignore")
    np.random.seed(40)

    train = preprocessing_train()

    X = train.drop(columns=['TARGET'])
    y = train['TARGET']

    X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33, random_state=42)

    sc = StandardScaler()
    X_train = sc.fit_transform(X_train)
    X_test = sc.transform(X_test)

    lr = float(sys.argv[1]) if len(sys.argv) > 1 else 0.1
    ne = int(sys.argv[2]) if len(sys.argv) > 500 else 100
    nj = int(sys.argv[3]) if len(sys.argv) > 3 else 1

    with mlflow.start_run():

        classifier = RandomForestClassifier(n_estimators=20, random_state=0)
        classifier.fit(X_train, y_train)
        predicted_qualities = classifier.predict(X_test)

        precision,recall,fscore,support=score(y_test, predicted_qualities)
        precision0 = precision[0]
        precision1 = precision[1]
        recall0 = recall[0]
        recall1 = recall[1]
        fscore0 = fscore[0]
        fscore1 = fscore[1]
        support0 = support[0]
        support1 = support[1]

        accuracy = accuracy_score(y_test, predicted_qualities)

        print("RandomForest Model (learning_rate=%f, n_estimators=%f, n_jobs=%f):" % (lr, ne, nj))

        mlflow.log_param("learning_rate", lr)
        mlflow.log_param("n_estimators", ne)
        mlflow.log_param("n_jobs", nj)
        mlflow.log_metric("precision0", precision0)
        mlflow.log_metric("precision1", precision1)
        mlflow.log_metric("recall0", recall0)
        mlflow.log_metric("recall1", recall1)
        mlflow.log_metric("fscore0", fscore0)
        mlflow.log_metric("fscore1", fscore1)
        mlflow.log_metric("support0", support0)
        mlflow.log_metric("support1", support1)
        mlflow.log_metric("accuracy", accuracy)

        mlflow.sklearn.log_model(classifier, "RandomForestClassifier")


"""
# Definition for a QuadTree node.
class Node(object):
    def __init__(self, val, isLeaf, topLeft, topRight, bottomLeft, bottomRight):
        self.val = val
        self.isLeaf = isLeaf
        self.topLeft = topLeft
        self.topRight = topRight
        self.bottomLeft = bottomLeft
        self.bottomRight = bottomRight
"""
class Solution(object):
    def intersect(self, quadTree1, quadTree2):
        """
        :type quadTree1: Node
        :type quadTree2: Node
        :rtype: Node
        """
        if quadTree1.isLeaf:
            if quadTree1.val == 1:
                return quadTree1
            else:
                return quadTree2
        if quadTree2.isLeaf:
            if quadTree2.val == 1:
                return quadTree2
            else:
                return quadTree1

        n = Node(
            val=0,
            isLeaf=False,
            topLeft=self.intersect(quadTree1.topLeft, quadTree2.topLeft),
            topRight=self.intersect(quadTree1.topRight, quadTree2.topRight),
            bottomLeft=self.intersect(quadTree1.bottomLeft, quadTree2.bottomLeft),
            bottomRight=self.intersect(quadTree1.bottomRight, quadTree2.bottomRight)
        )
        if n.topLeft.isLeaf and n.topRight.isLeaf and n.bottomLeft.isLeaf and n.bottomRight.isLeaf and \
            n.topLeft.val == n.topRight.val and n.bottomLeft.val == n.bottomRight.val and n.topLeft.val == n.bottomLeft.val:
            return Node(n.topLeft.val, True, None, None, None, None)
        return n


#!/usr/bin/env python
# -*- encoding: utf-8 -*-

# Copyright (c) 2002-2018 "Neo Technology,"
# Network Engine for Objects in Lund AB [http://neotechnology.com]
#
# This file is part of Neo4j.
#
# 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.

from neo4j.v1 import GraphDatabase, ServiceUnavailable
from neo4j.exceptions import ProtocolError
from test.integration.tools import IntegrationTestCase

class DriverTestCase(IntegrationTestCase):

    def test_must_use_valid_url_scheme(self):
        with self.assertRaises(ProtocolError):
            GraphDatabase.driver("x://xxx", auth=self.auth_token)

    def test_connections_are_reused(self):
        with GraphDatabase.driver(self.bolt_uri, auth=self.auth_token) as driver:
            session_1 = driver.session()
            connection_1 = session_1._connection
            session_1.close()
            session_2 = driver.session()
            connection_2 = session_2._connection
            session_2.close()
            assert connection_1 is connection_2

    def test_fail_nicely_when_using_http_port(self):
        uri = "bolt://localhost:7474"
        with self.assertRaises(ServiceUnavailable):
            with GraphDatabase.driver(uri, auth=self.auth_token, encrypted=False):
                pass


#! /usr/bin/env python3
# -*- coding: utf-8 -*-
###
###   Goal:
###      Produce AGP files summarizing scaffolding results of DeCoSTAR
###
###   INPUT:
###      1- New adjacencies file
###         (27avian_dataset/results/decostar/ADseq+scaff_Boltz_kT0.1/DeCoSTAR_27avian_ADseq+scaff_Boltz_kT0.1_Linear_0.1_M_new_adjacencies_with_scaff)
###      2- Genome assemblies directory
###         (27avian_dataset/data/INPUT_DATA/FASTA/SCAFF)
###      3- prefix file
###         (DeCoSTAR_27avian_ADseq+scaff_Boltz_kT0.1_Lin0.1_M2_)
###      4- AGP files path/prefix
###         (27avian_dataset/results/AGP/SCAFF/)
###
###   OUTPUT:
###      - AGP files summarizing scaffolding results of DeCoSTAR
###
###   Name: create_AGP_from_new_adjacencies.py    Author: Yoann Anselmetti
###   Creation date: 2018/06/11                   Last modification: 2020/11/04
###

from sys import argv
from re import search
from os import close, path, listdir, makedirs
from datetime import datetime
import errno
from collections import namedtuple   #New in version 2.6
from Bio import SeqIO

def mkdir_p(dir_path):
   try:
      makedirs(dir_path)
   except OSError as exc: # Python >2.5
      if exc.errno == errno.EEXIST and path.isdir(dir_path):
         pass
      else:
         raise

def get_SCAFF_ID(i):
   SCAFF_ID=""
   i+=1
   if i>=1000000 and i<=9999999:
      SCAFF_ID="SCAFF"+str(i)
   elif i>=100000:
      SCAFF_ID="SCAFF0"+str(i)
   elif i>=10000:
      SCAFF_ID="SCAFF00"+str(i)
   elif i>=1000:
      SCAFF_ID="SCAFF000"+str(i)
   elif i>=100:
      SCAFF_ID="SCAFF0000"+str(i)
   elif i>=10:
      SCAFF_ID="SCAFF00000"+str(i)
   elif i>=1:
      SCAFF_ID="SCAFF000000"+str(i)
   else:
      exit("ERROR too much scaffolds!!! -> (>9999999)")
   return i,SCAFF_ID

def rev_ori(ori):
   if ori=="-":
      return "+"
   elif ori=="+":
      return "-"
   elif ori=="?":
      return "?"
   else:
      exit("ERROR, orientation: \""+ori+"\" is incorrect, it should be \"+\" or \"-\"!!!")

def rev_ctg_order(listCTG):
   new_listCTG=list()
   for i in range(1,len(listCTG)+1):
      rev_ctg=CTG(listCTG[-i].id,rev_ori(listCTG[-i].ori))
      new_listCTG.append(rev_ctg)
   return new_listCTG

def mergeCTG(species,ctg1,ctg2,ori_ctg1,ori_ctg2,dict_newSCAFF_ID,dict_newSCAFF,scaffold_ID_with_ctg_order,scaff_id):
   # Get new ID of ctg1 and ctg2 if they have been previously changed
   new_ctg1,new_ctg2="",""
   if species in dict_newSCAFF_ID:
      if ctg1 in dict_newSCAFF_ID[species]:
         new_ctg1=dict_newSCAFF_ID[species][ctg1]
      if ctg2 in dict_newSCAFF_ID[species]:
         new_ctg2=dict_newSCAFF_ID[species][ctg2]

   # If ctg1 has been previously changed
   new_ctg1_order=list()
   new_ctg2_order=list()
   if new_ctg1:
      first_ctg1=dict_newSCAFF[species][new_ctg1][0]
      last_ctg1=dict_newSCAFF[species][new_ctg1][-1]
      # Get the correct order of contigs in new_ctg1
      if ctg1==first_ctg1.id:
         new_ctg1_order=rev_ctg_order(dict_newSCAFF[species][new_ctg1])
      elif ctg1==last_ctg1.id:
         new_ctg1_order=dict_newSCAFF[species][new_ctg1]
      else:
         exit("ERROR, contig \""+ctg1+"\" should be to the start or the end of the new contig \""+new_ctg1+"\" !!!")
      # Remove the previous version of "new_ctg1"
      dict_newSCAFF[species].pop(new_ctg1,None)

   # If ctg2 has been previously changed
   if new_ctg2:
      first_ctg2=dict_newSCAFF[species][new_ctg2][0]
      last_ctg2=dict_newSCAFF[species][new_ctg2][-1]
      # Get the correct order of contigs in new_27avian_dataset/data/INPUT_DATA/27avian_species.t2
      if ctg2==last_ctg2.id:
         new_ctg2_order=rev_ctg_order(dict_newSCAFF[species][new_ctg2])
      elif ctg2==first_ctg2.id:
         new_ctg2_order=dict_newSCAFF[species][new_ctg2]
      else:
         exit("ERROR, contig \""+ctg2+"\" should be to the start or the end of the new contig \""+new_ctg2+"\" !!!")
      # Remove the previous version of "new_ctg2"
      dict_newSCAFF[species].pop(new_ctg2,None)

   # If ctg1 has NOT been previously changed
   if not new_ctg1_order:
      new_ctg1_order.append(CTG(ctg1,ori_ctg1))
   # If ctg2 has NOT been previously changed
   if not new_ctg2_order:
      new_ctg2_order.append(CTG(ctg2,ori_ctg2))

   # Concatenate the two CTG order to define the new SCAFF
   new_ctg_order=new_ctg1_order+new_ctg2_order

   # Set the ID of the new scaffold
   new_CTG_ID=""
   ### If we set the scaffold ID with the ctg order list
   if scaffold_ID_with_ctg_order:
      for elem in new_ctg_order:
         ctg=elem.id
         ori=elem.ori
         short_ctg=ctg.split(sep)[0]
         if not new_CTG_ID:
            new_CTG_ID+=short_ctg+"("+ori+")"
         else:
            new_CTG_ID+=":"+short_ctg+"("+ori+")"
   ### If we set the scaffold ID with the format "SCAFFXXXXXX"
   else:
      if new_ctg1:
         new_CTG_ID=new_ctg1
      elif new_ctg2:
         new_CTG_ID=new_ctg2
      else:
         scaff_id,new_CTG_ID=get_SCAFF_ID(scaff_id)

   #
   if not species in dict_newSCAFF:
      dict_newSCAFF[species]=dict()
   dict_newSCAFF[species][new_CTG_ID]=new_ctg_order

   if not species in dict_newSCAFF_ID:
      dict_newSCAFF_ID[species]=dict()
   for elem in new_ctg_order:
      ctg=elem.id
      dict_newSCAFF_ID[species][ctg]=new_CTG_ID

   return scaff_id

################
###   MAIN   ###
################
if __name__ == '__main__':

   start_time = datetime.now()

   CTG=namedtuple("CTG",["id","ori"])

   # Recovery of input parameters
   newAdj_file=argv[1]
   FASTA_dir=argv[2]
   file_prefix=argv[3]
   OUTPUT_AGP=argv[4]

   scaffold_ID_with_ctg_order=False
   write_unscaffolded_ctg=True
   default_gap_size=100
   verbose=1
   sep="_size"

   # Create OUTPUT_DIR if not existing
   mkdir_p(OUTPUT_AGP)

   print("1/ Merge scaffolds linked by linearized new adjacencies")
   dict_newSCAFF,dict_newSCAFF_ID,dict_distCTG=dict(),dict(),dict()
   input_file=open(newAdj_file,"r")
   scaff_id=0
   stored_species=""
   for new_adj in input_file:
      r=search("^([^\t]*)\t([^\t]*)\t([^\t]*)\t([^\t]*)\t([^\t]*)\t([^\t]*)\t([^\t]*)\t([^\t]*)\t([^\t]*)\t([^\t]*)\t([^\t]*)\t([^\t]*)\t([^\t]*)\t([^\t]*)\t([^\t]*)\t([^\t]*)\t([^\t\n]*)\n$",new_adj)
      if r:
         species=r.group(1)
         ctg1=r.group(2)
         ctg2=r.group(3)
         ori_ctg1=r.group(4)
         ori_ctg2=r.group(5)
         dist_ctg=r.group(6)
         gf1=r.group(7)
         gf2=r.group(8)
         g1=r.group(9)
         g2=r.group(10)
         ori_g1=r.group(11)
         ori_g2=r.group(12)
         dist_gene=r.group(13)
         vscore=r.group(14)
         dscore=r.group(15)
         links=r.group(16)
         support=r.group(17)

         if species!="#species":
            if stored_species!=species:
               scaff_id=0
               stored_species=species
            dict_distCTG[(ctg1,ctg2)]=dist_ctg
            scaff_id=mergeCTG(species,ctg1,ctg2,ori_ctg1,ori_ctg2,dict_newSCAFF_ID,dict_newSCAFF,scaffold_ID_with_ctg_order,scaff_id)
   input_file.close()
   dict_newSCAFF_ID.clear()

   # Print the new CTG ID after merging initial contigs/scaffoldsin new scaffolds
   if verbose>1:
      if verbose>2:
         print("\n1bis/ Print association between new scaffolds ID and old scaffolds/contigs ID / species:")
      else:
         print("\n1bis/ Print new scaffolds ID / species:")
      # Print new linked scaffolds / species
      for species in sorted(dict_newSCAFF):
         print("\n"+species+":")
         for ctg in dict_newSCAFF[species]:
            print("\t"+ctg)
            if verbose>2:
               for elem in dict_newSCAFF[species][ctg]:
                  print("\t\t", end=' ')
                  print(elem)

   # Print distance between CTG pairs linked by DeCoSTAR
   if verbose>2:
      print("\n\t1ter/ Print distance between CTG pairs linked by DeCoSTAR:")
      for distCTG in sorted(dict_distCTG):
         print(distCTG,"\t",dict_distCTG[distCTG])

   print("\n2/ Write AGP files after scaffolding with linearized new adjacencies predicted by DeCoSTAR")
   for species in sorted(dict_newSCAFF):
      if verbose>0:
         print("\t"+species)
      output_agp=open(OUTPUT_AGP+"/"+file_prefix+species+".agp","w")

      dict_CTG=dict()
      FASTA_FILE=""
      for FASTA in sorted(listdir(FASTA_dir)):
         i=0
         spe=""
         r=search("^([^\.]*)\..*$",FASTA)
         if r:
            spe=r.group(1)
         else:
            exit("!!! ERROR, FASTA file name: "+FASTA+" is incorrectly written (Must be: ${species_name}.fa) !!!")
         if spe==species:
            FASTA_FILE=FASTA_dir+"/"+FASTA
            # Browse FASTA file of current species to get list of scaffolds
            fasta_file=open(FASTA_dir+"/"+FASTA)
            for sequence in SeqIO.parse(fasta_file,"fasta"):
               ctg=sequence.id
               size=len(sequence.seq)
               dict_CTG[ctg]=size
            fasta_file.close()
            break

      for scaff in sorted(dict_newSCAFF[species]):
         listCTG=dict_newSCAFF[species][scaff]
         ID=""
         gap_size=0
         posSTART=1
         posEND=0
         bool_default=False
         stored_CTG=""
         for ctg in listCTG:
            ### Allow to discard duplicate CTG (corresponding to a circularizing adjacency predicted by DeCoSTAR)
            if ctg.id!=stored_CTG:
               stored_CTG=ctg.id
               ctg_size=dict_CTG[ctg.id]
               del dict_CTG[ctg.id]
               # Get size of the gap between the 2 scaffolded contigs (ID and ctg.id)
               if ID:
                  if (ID,ctg.id) in dict_distCTG:
                     if dict_distCTG[(ID,ctg.id)]=="?":
                        bool_default=True
                        gap_size=default_gap_size
                     else:
                        gap_size=int(float(dict_distCTG[(ID,ctg.id)]))
                        # print gap_size
                        if gap_size<0:
                           if verbose>1:
                              print("\t!!!WARNING!!! => NEGATIVE distance between contigs "+ID+" and "+ctg.id)
                  elif (ctg.id,ID) in dict_distCTG:
                     if dict_distCTG[(ctg.id,ID)]=="?":
                        bool_default=True
                        gap_size=default_gap_size
                     else:
                        gap_size=int(float(dict_distCTG[(ctg.id,ID)]))
                        # print gap_size
                        if gap_size<0:
                           if verbose>1:
                              print("\t!!!WARNING!!! => NEGATIVE distance between contigs "+ctg.id+" and "+ID)
                  else:
                     exit("ERROR: CTG adjacency ("+ID+"-"+ctg.id+") is not present in DeCoSTAR predicted adjacencies file: "+newAdj_file)

                  # Write gap in AGP file
                  posEND+=gap_size
                  if bool_default:
                     output_agp.write(scaff+"\t"+str(posSTART)+"\t"+str(posEND)+"\t.\tU\t"+str(gap_size)+"\tscaffold\tno\tna\n")
                  else:
                     output_agp.write(scaff+"\t"+str(posSTART)+"\t"+str(posEND)+"\t.\tN\t"+str(gap_size)+"\tscaffold\tyes\tpaired-ends\n")
                  bool_default=False
                  posSTART=posEND+1
               posEND+=ctg_size

               # Write current contig in the AGP file
               ID=ctg.id
               ori=ctg.ori
               output_agp.write(scaff+"\t"+str(posSTART)+"\t"+str(posEND)+"\t.\tW\t"+ID+"\t1\t"+str(ctg_size)+"\t"+ori+"\n")
               posSTART=posEND+1

      # Write remaining contigs (not scaffolded) in the AGP file
      if write_unscaffolded_ctg:
         for ctg in sorted(dict_CTG):
            ctg_size=dict_CTG[ctg]
            output_agp.write(ctg+"\t1\t"+str(ctg_size)+"\t.\tW\t"+ctg+"\t1\t"+str(ctg_size)+"\t+\n")
      output_agp.close()

   end_time = datetime.now()
   print('\nDuration: {}'.format(end_time-start_time))


from persistent import Persistent

class ConflictFreeLog(Persistent):
  """Scalable conflict-free append-only double-linked list

  Wasted ZODB space due to conflicts is roughly proportional to the number of
  clients that continuously add items at the same time.
  """
  _prev = _next = None
  _tail_count = 0
  _bucket_size = 1000

  def __init__(self, items=(), bucket_size=None):
    self._log = list(items)
    if bucket_size:
      assert bucket_size > 0
      self._bucket_size = bucket_size

  def __len__(self):
    return self._tail_count + len(self._log)

  def _maybe_rotate(self):
    if self._p_estimated_size < self._bucket_size:
      self._p_changed = 1
    else:
      tail = self.__class__()
      tail._log = self._log
      prev = self._prev
      if prev is None:
        prev = self
      else:
        assert not self._next._tail_count
        tail._tail_count = self._tail_count
      tail._prev = prev
      prev._next = tail
      self._prev = tail
      tail._next = self
      self._tail_count += len(self._log)
      self._log = []

  def append(self, item):
    if not self._p_changed:
      self._maybe_rotate()
    self._log.append(item)

  def extend(self, items):
    if not self._p_changed:
      self._maybe_rotate()
    self._log.extend(items)

  def __iadd__(self, other):
    self.extend(other)
    return self

  def __iter__(self):
    bucket = self._next
    if bucket is None:
      bucket = self
    while 1:
      for item in bucket._log:
        yield item
      if bucket is self:
        break
      bucket = bucket._next

  def reversed(self):
    bucket = self
    while 1:
      for item in bucket._log[::-1]:
        yield item
      bucket = bucket._prev
      if bucket in (None, self):
        break

  def _p_resolveConflict(self, old_state, saved_state, new_state):
    # May be called for the head and its predecessor.
    old_tail_count = old_state.get('_tail_count', 0)
    d = new_state.get('_tail_count', 0) - old_tail_count
    # Added elements by us:
    added = new_state['_log'][
      # The following computed value is also non-zero
      # if we rotated during a previous conflict resolution.
      len(old_state['_log']) - d
      :]
    if d:
      if old_tail_count == saved_state.get('_tail_count', 0):
        # We are the first one to rotate. Really rotate.
        # Only the head conflicts in this case.
        return dict(new_state, _log=saved_state['_log'][d:] + added)
      # Another node rotated before us. Revert our rotation.
      # Both the head and its predecessor conflict.
    #else:
      # We didn't rotate. Just add our items to saved head.
      # Only the head conflicts.
    saved_state['_log'] += added
    return saved_state


import csv
import re
from path_transformer.output_path_creator import create_output_path
from test_data.image_transformer.brigthness_transformer import transform_brightness
from path_transformer.path_exception import PathException

def transform_all_images_in_csv_file(csv_input_file_path, image_folder_path):
    with open(csv_input_file_path, mode='r+') as input_file:
        csv_input_reader = csv.reader(input_file, delimiter= ';')

        output_csv_path = create_output_path(csv_input_file_path, ".csv", "(transformed)")
        with open(output_csv_path, mode='w', newline='') as outputFile:
            csv_output_writer = csv.writer(outputFile, delimiter=';', quotechar='"', quoting=csv.QUOTE_MINIMAL)

            line_count = 0
            for row in csv_input_reader:
                if line_count == 0:
                    # We just write the first line containing the column names
                    csv_output_writer.writerow(row)
                    line_count += 1
                else:
                    output_image_names = transform_brightness(row[0], image_folder_path)
                    # We here write all the new images to the csv file
                    for output_name in output_image_names:
                        csv_output_writer.writerow([output_name, row[1]])
                    line_count += 1


from enum import Enum
from Command import Command
import os

""" The parser module for the assembler.
"""

# Constants.
COMMENT_PREFIX = '//'
READ_ONLY = 'r'
DEF_ENCODING = 'utf-8'
EMPTY_LINE = ''

STR_ARITHMETIC = ['add', 'sub', 'neg', 'eq', 'gt', 'lt', 'and', 'or', 'not']
STR_PUSH = 'push'
STR_POP = 'pop'
STR_LABEL = 'label'
STR_GOTO = 'goto'
STR_IF = 'if'
STR_FUNCTION = 'function'
STR_RETURN = 'return'
STR_CALL = 'call'

class CommandType(Enum):
    ''' Enum for the command type.
    '''
    NO_COMMAND = -1
    C_ARITHMETIC = 0
    C_PUSH = 1
    C_POP = 2
    C_LABEL = 3
    C_GOTO = 4
    C_IF = 5
    C_FUNCTION = 6
    C_RETURN = 7
    C_CALL = 8

content = []

def parse(file_name):
    """ Parse a given assembly language file.
    """
    # Clean up when parsing a new file
    global content
    content = []
    current_command = None
    # Read the file and parse lines
    with open(file_name, mode=READ_ONLY, encoding=DEF_ENCODING) as vm_file:
        for line in vm_file:
            # Ignore whitespace & comments in the start and end of the line
            found_comment = line.find(COMMENT_PREFIX)
            if found_comment != -1:
                line = line[:found_comment]

            line = line.strip().split(' ')
            if line[0] == EMPTY_LINE:
                continue
            # Determine whether current line is A/L/C CommandType (L for Label)
            elif line[0] in STR_ARITHMETIC:
                current_command = CommandType.C_ARITHMETIC
            elif line[0] == STR_PUSH:
                current_command = CommandType.C_PUSH
            elif line[0] == STR_POP:
                current_command = CommandType.C_POP
            elif line[0] == STR_LABEL:
                current_command = CommandType.C_LABEL
            elif line[0] == STR_GOTO:
                current_command = CommandType.C_GOTO
            elif line[0] == STR_IF:
                current_command = CommandType.C_IF
            elif line[0] == STR_FUNCTION:
                current_command = CommandType.C_FUNCTION
            elif line[0] == STR_RETURN:
                current_command = CommandType.C_RETURN
            elif line[0] == STR_CALL:
                current_command = CommandType.C_CALL

            # Add the created command to the content list
            content.append(Command(current_command, line))

        # For loop ends here.

    # File is closed here

def get_commands():
    """ Get all commands in a parsed file - use this after running the parse function.
    This is a generator, thus running 'for command in get_commands()' will yield
    all commands in the parsed file in the correct order.
    """
    for command in content:
        yield command


FACTOR_MESSAGES = [
    (3, "Pling"),
    (5, "Plang"),
    (7, "Plong")]

def convert(number):
    """ Converts number to string representation of its "sound",
        using messages already provided to add_new_factor_message."""

    return "".join([message if number % factor == 0 else "" for (factor, message) in FACTOR_MESSAGES]) or str(number)


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

import numpy

import matplotlib.pyplot as plt

from ..core.frequency import FrequencyAxis
from ..core.dfunction import DFunction
from ..core.managers import EnergyUnitsManaged
from ..core.units import cm2int

class AbsSpectrumBase(DFunction, EnergyUnitsManaged):
    """Provides basic container for absorption spectrum

    """

    def __init__(self, axis=None, data=None):
        super().__init__()
        self.axis = axis
        self.data = data

    def set_axis(self, axis):
        """Sets axis atribute

        Parameters
        ----------

        axis : FrequencyAxis object
            Frequency axis object. This object has managed energy units

        """
        self.axis = axis

    def set_data(self, data):
        """Sets data atribute

        Parameters
        ----------

        data : array like object (numpy array)
            Sets the data of the absorption spectrum

        """
        self.data = data

    def set_by_interpolation(self, x, y, xaxis="frequency"):

        from scipy import interpolate

        if xaxis == "frequency":

            om = self.convert_2_internal_u(x)

        elif xaxis == "wavelength":
            # convert to internal (nano meters) units of wavelength

            # convert to energy (internal units)
            # to cm
            om = 1.0e-7*x
            # to 1/cm
            om = 1.0/om
            # to 1/fs
            om = om*cm2int

        if om[1] > om[2]:
            # reverse order
            om = numpy.flip(om,0)
            y = numpy.flip(y,0)

        # equidistant points on the x-axis
        omin = numpy.amin(om)
        omax = numpy.amax(om)
        length = om.shape[0]
        step = (omax-omin)/length

        # new frequency axis
        waxis = FrequencyAxis(omin, length, step)

        # spline interpolation
        tck = interpolate.splrep(om, y, s=0)
        ynew = interpolate.splev(waxis.data, tck, der=0)

        # setting the axis and data
        self.axis = waxis
        self.data = ynew

    def clear_data(self):
        """Sets spectrum data to zero

        """
        shp = self.data.shape
        self.data = numpy.zeros(shp, dtype=numpy.float64)

    def normalize2(self,norm=1.0):
        """Normalizes spectrum to a given value

        """
        mx = numpy.max(self.data)
        self.data = norm*self.data/mx

    def normalize(self):
        """Normalization to one

        """
        self.normalize2(norm=1.0)

    def subtract(self, val):
        """Subtracts a value from the spectrum to shift its base line

        """
        self.data -= val

    def add_to_data(self, spect):
        """Performs addition on the data.

        Expects a compatible object holding absorption spectrum
        and adds its data to the present absorption spectrum.

        Parameters
        ----------

        spect : spectrum containing object
            This object should have a compatible axis and some data

        """

        if self.axis is None:
            self.axis = spect.axis.copy()

        if not numpy.allclose(spect.axis.data, self.axis.data):
            numpy.savetxt("spect_data_wrong.dat", spect.axis.data)
            numpy.savetxt("self_data_wrong.dat", self.axis.data)
            raise Exception("Incompatible axis")

        if self.data is None:
            self.data = numpy.zeros(len(spect.data),
                                    dtype=spect.axis.data.dtype)

        self.data += spect.data

    def load_data(self, filename, ext=None, replace=False):
        """Load the spectrum from a file

        Uses the load method of the DFunction class to load the absorption
        spectrum from a file. It sets the axis type to 'frequency', otherwise
        no changes to the inherited method are applied.

        Parameters
        ----------

        """
        super().load_data(filename, ext=ext, axis='frequency', replace=replace)

    #save method is inherited from DFunction

    def plot(self, **kwargs):
        """ Plotting absorption spectrum using the DFunction plot method

        """
        if "ylabel" not in kwargs:
            ylabel = r'$\alpha(\omega)$ [a.u.]'
            kwargs["ylabel"] = ylabel

        fig = super().plot(**kwargs)
        if fig is not None:
            return fig

    def gaussian_fit(self, N=1, guess=None, plot=False, Nsvf=251):
        from scipy.signal import savgol_filter
        from scipy.interpolate import UnivariateSpline
        """Performs a Gaussian fit of the spectrum based on an initial guess

        Parameters
        ----------

        Nsvf : int
            Length of the Savitzky-Golay filter window (odd integer)

        """
        x = self.axis.data
        y = self.data

        if guess is None:

            raise Exception("Guess is required at this time")
            # FIXME: create a reasonable guess
            guess = [1.0, 11000.0, 300.0, 0.2,
                     11800, 400, 0.2, 12500, 300]

            #
            # Find local maxima and guess their parameters
            #

            # Fit with a given number of Gaussian functions

            if not self._splines_initialized:
                self._set_splines()

            # get first derivative and smooth it
            der = self._spline_r.derivative()
            y1 = der(x)
            y1sm = savgol_filter(y1,Nsvf,polyorder=3)

            # get second derivative and smooth it
            y1sm_spl_der = UnivariateSpline(x,y1sm,s=0).derivative()(x)
            y2sm = savgol_filter(y1sm_spl_der,Nsvf,polyorder=3)

            # find positions of optima by looking for zeros of y1sm

            # isolate maxima by looking at the value of y2sm

            #plt.plot(x, der(x))
            #plt.plot(x, y1sm)
            plt.plot(x, y2sm)
            plt.show()

        def funcf(x, *p):
            return _n_gaussians(x, N, *p)

        # minimize, leastsq,
        from scipy.optimize import curve_fit
        popt, pcov = curve_fit(funcf, x, y, p0=guess)

        if plot:

            plt.plot(x,y)
            plt.plot(x,_n_gaussians(x, N, *popt))
            for i in range(N):
                a = popt[3*i]
                print(i, a)
                b = popt[3*i+1]
                c = popt[3*i+2]
                y = _gaussian(x, a, b, c)
                plt.plot(x, y,'-r')
            plt.show()

        # FIXME: Create a readable report

        return popt, pcov

#    def convert_to_energy(self, eaxis, units):
#        """
#
#        """
#
#        if units == "nm":
#            x = self.axis.data
#            y = self.data
#
#            # to cm
#            x = 1.0e-7*x
#            # to 1/cm
#            x = 1.0/x
#            # to rad/fs
#            x = x*cm2int
#
#            xn = numpy.zeros(x.shape, dtype=x.dtype)
#            yn = numpy.zeros(y.shape, dtype=y.dtype)
#
#            for i in range(len(x)):
#                xn[i] = x[len(x)-i-1]
#                yn[i] = y[len(x)-i-1]
#
#            # spline it
#
#            # evaluate at points if eaxis
#

def _gaussian(x, height, center, fwhm, offset=0.0):
    """Gaussian function with a possible offset

    Parameters
    ----------

    x : float array
        values to calculate Gaussian function at

    height : float
        height of the Gaussian at maximum

    center : float
        position of maximum

    fwhm : float
        full width at half maximum of the Gaussian function

    offset : float
        the value at infinity; effectively an offset on the y-axis

    """

    return height*numpy.exp(-(((x - center)**2)*4.0*numpy.log(2.0))/
                            (fwhm**2)) + offset

def _n_gaussians(x, N, *params):
    """Sum of N Gaussian functions plus an offset from zero

    Parameters
    ----------

    x : float
        values to calculate Gaussians function at

    N : int
        number of Gaussians

    params : floats
        3*N + 1 parameters corresponding to height, center, fwhm  for each
        Gaussian and one value of offset

    """
    n = len(params)
    k = n//3

    if (k*3 == n) and (k == N):

        res = 0.0
        pp = numpy.zeros(3)
        for i in range(k):
            pp[0:3] = params[3*i:3*i+3]
            #pp[3] = 0.0
            arg = tuple(pp)
            res += _gaussian(x, *arg)
        res += params[n-1] # last parameter is an offset
        return res

    else:
        raise Exception("Inconsistend number of parameters")


from skimage import transform as tf
from skimage.feature import (match_descriptors, ORB, plot_matches)
import matplotlib.pyplot as plt
import cv2
from skimage.filters.rank import enhance_contrast, median
from skimage.exposure import equalize_adapthist
from skimage import morphology

# from skimage.measure import ransac as skransac
# from skimage.transform import EuclideanTransform
import numpy as np
from ex4_part1 import *
from scipy import misc

# img1 = misc.imread('BL05.bmp', flatten=0)
# img1 = cv2.medianBlur(img1[:,:,0, 15)

# img2 = misc.imread('FU05.bmp', flatten=0)
# img2 = cv2.medianBlur(img2[:,:,0, 15)

img1 = cv2.imread('BL01-no.tif', 0)

img1 = median(img1, selem=np.ones((12, 12)))

img2 = cv2.imread('FU01-no.tif', 0)
img2 = median(img2, selem=np.ones((12, 12)))

descriptor_extractor = ORB(n_keypoints=200, harris_k=1e-4)

descriptor_extractor.detect_and_extract(img1)
keypoints1 = descriptor_extractor.keypoints
descriptors1 = descriptor_extractor.descriptors

descriptor_extractor.detect_and_extract(img2)
keypoints2 = descriptor_extractor.keypoints
descriptors2 = descriptor_extractor.descriptors

matches12 = match_descriptors(descriptors1, descriptors2, cross_check=True,metric='euclidean')

display_matches(img1, img2, keypoints1[matches12[:, 0]], keypoints2[matches12[:, 1]])

fig, ax = plt.subplots(nrows=2, ncols=1)

plt.gray()

plot_matches(ax[0], img1, img2, keypoints1, keypoints2, matches12)
# plt.scatter(keypoints1[matches12[:, 0]][:, 1], keypoints1[matches12[:, 0]][:, 0])
# plt.scatter(keypoints2[matches12[:, 1]][:, 1] + img1.shape[1], keypoints2[matches12[:, 1]][:, 0])
ax[0].axis('off')
ax[0].set_title("Original Image vs. Transformed Image")

f, inliers = ransac(keypoints1[matches12[:, 0]], keypoints2[matches12[:, 1]], calcPointBasedReg, calcDist,
                    minPtNum=3, iterNum=20000, thDist=5, thInlrRatio=0.2)

# g, skinliers = skransac((keypoints1[matches12[:, 0]], keypoints2[matches12[:, 1]]), EuclideanTransform, min_samples=2,
#                     residual_threshold=10, max_trials=20000)
# inliers_idx = np.nonzero(inliers)[0]
inliers_idx = matches12[inliers]
plot_matches(ax[1], img1, img2, keypoints1, keypoints2, inliers_idx)
# plt.scatter(keypoints1[inliers[:, 0]][:, 1], keypoints1[inliers[:, 0]][:, 0])
# plt.scatter(keypoints2[inliers[:, 1]][:, 1] + img1.shape[1], keypoints2[inliers[:, 1]][:, 0])
ax[1].axis('off')
ax[1].set_title("ransac")

plt.show()

display_matches(img1, img2, keypoints1[matches12[:, 0]], keypoints2[matches12[:, 1]], inliers)


#!/usr/bin/env python

# Copyright (C) 2010 Red Hat, Inc.
#
# This is free software; you can redistribute it and/or modify it
# under the terms of the GNU Lesser General Public License as
# published by the Free Software Foundation; either version 2.1 of
# the License, or (at your option) any later version.
#
# This software is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this software; if not, write to the Free
# Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
# 02110-1301 USA, or see the FSF site: http://www.fsf.org.

from art.rhevm_api.utils.test_utils import get_api
from art.core_api.apis_utils import getDS

ELEMENT = 'tag'
COLLECTION = 'tags'
util = get_api(ELEMENT, COLLECTION)

Tag = getDS('Tag')

def _prepareTagObject(**kwargs):

    tag = Tag()

    if 'name' in kwargs:
        tag.set_name(kwargs.get('name'))

    if 'description' in kwargs:
        tag.set_description(kwargs.get('description'))

    if 'parent' in kwargs:
        parent = util.find(kwargs.pop('parent'))
        tag.set_parent(parent)

    return tag

def addTag(positive, **kwargs):
    '''
    Description: create new tag
    Author: edolinin
    Parameters:
       * name - name of a new tag
       * description - tag description
       * parent - name of the tag to be used as a parent.
    Return: status (True if tag was created properly, False otherwise)
    '''

    tag = _prepareTagObject(**kwargs)
    tag, status = util.create(tag, positive)

    return status

def updateTag(positive, tag, **kwargs):
    '''
    Description: update existed tag
    Author: edolinin
    Parameters:
       * tag - name of a tag that should be updated
       * name - new tag name
       * description - new tag description
       * parent - name of the new parent tag
    Return: status (True if tag was updated properly, False otherwise)
    '''

    tagObj = util.find(tag)
    tagUpd = _prepareTagObject(**kwargs)
    tagUpd, status = util.update(tagObj, tagUpd, positive)
    return status

def removeTag(positive, tag):
    '''
    Description: remove existed tag
    Author: edolinin
    Parameters:
       * tag - name of a tag that should be removed
    Return: status (True if tag was removed properly, False otherwise)
    '''

    tagObj = util.find(tag)
    return util.delete(tagObj, positive)


### Number of functional cell types across sessions

from re import S
import sys, os
import datajoint as dj
import numpy as np

#### LOAD DATABASE #########################################
from .dj_conn import *
imhotte = dj.schema(horst_imaging_db)

from .anatomical_distribution import FilteredCells, FilteredCellsParams

@imhotte
class NumberCellTypes(dj.Computed):
    definition = """
    # Number of (pure) cell types per session and brain region (chose MEC and PAS)
    -> FilteredSessions
    -> FilteredCellsParams
    ---
    n_cells                     : int          # Total number of (filtered) cells in this session
    """

    class MEC(dj.Part):
        definition = """
        # Cell numbers MEC
        -> master
        ---
        mec_n_cells              : int         # MEC Total number of (filtered) cells
        mec_ovc = NULL           : int         # MEC Number of OV cells
        mec_n_grid_95            : int         # MEC Number of grid cells > 95th
        mec_n_info_95            : int         # MEC Number of cells w info content > 95th
        mec_n_hd_95              : int         # MEC Number of HD cells > 95th
        mec_n_border_95          : int         # MEC Number of Border cells > 95th
        mec_n_bv_95              : int         # MEC Number of BV cells > 95th
        mec_n_grid_99            : int         # MEC Number of grid cells > 99th
        mec_n_info_99            : int         # MEC Number of cells w info content > 99th
        mec_n_hd_99              : int         # MEC Number of HD cells > 99th
        mec_n_border_99          : int         # MEC Number of Border cells > 99th
        mec_n_bv_99              : int         # MEC Number of BV cells > 99th
        """

    class PAS(dj.Part):
        definition = """
        # Cell numbers PAS
        -> master
        ---
        pas_n_cells              : int          # PAS Total number of (filtered) cells
        pas_ovc = NULL           : int          # PAS Number of OV cells
        pas_n_grid_95            : int          # PAS Number of grid cells > 95th
        pas_n_info_95            : int          # PAS Number of cells w info content > 95th
        pas_n_hd_95              : int          # PAS Number of HD cells > 95th
        pas_n_border_95          : int          # PAS Number of Border cells > 95th
        pas_n_bv_95              : int          # PAS Number of BV cells > 95th
        pas_n_grid_99            : int          # PAS Number of grid cells > 99th
        pas_n_info_99            : int          # PAS Number of cells w info content > 99th
        pas_n_hd_99              : int          # PAS Number of HD cells > 99th
        pas_n_border_99          : int          # PAS Number of Border cells > 99th
        pas_n_bv_99              : int          # PAS Number of BV cells > 99th
        """

    @property
    def key_source(self):
        return super().key_source & RoisCorrBrainLoc # Filter out those that do not have annotated brain regions

    def make(self, key):
        # Retrieve number of functional cell types per session
        cell_keys_session = FilteredCells & key
        # Brain region filter
        cells_session_mec = cell_keys_session & RoisCorrBrainLoc.MEC
        cells_session_pas = cell_keys_session & RoisCorrBrainLoc.PAS

        # Cell parameter dictionary
        parameter_dict_cell = (FilteredCellsParams & key).fetch1('parameter_dict_cell')

        # Filter cells by score / cutoff
        ovcells       = OVC.proj(is_ovc='is_ovc', session_name='base_session') & 'is_ovc > 0.5'

        bordercells95 = BorderScore * CutoffsBorderScore.proj('borderscore_95') & 'borderscore > borderscore_95'
        bvcells95     = BVScore * CutoffsBVScore.proj('bvs_95') & 'bvs > bvs_95'
        gridcells95   = GridScore * CutoffsGridscore.proj('gridscore_95') & 'gridscore > gridscore_95'
        hdcells95     = AngularRate.Stats * CutoffsMVL.proj('mvl_95') & 'mvl > mvl_95'
        infocells95   = Ratemap.Stats * CutoffsInfoContent.proj('info_content_95') & 'information_content > info_content_95'

        bordercells99 = BorderScore * CutoffsBorderScore.proj('borderscore_99') & 'borderscore > borderscore_99'
        bvcells99     = BVScore * CutoffsBVScore.proj('bvs_99') & 'bvs > bvs_99'
        gridcells99   = GridScore * CutoffsGridscore.proj('gridscore_99') & 'gridscore > gridscore_99'
        hdcells99     = AngularRate.Stats * CutoffsMVL.proj('mvl_99') & 'mvl > mvl_99'
        infocells99   = Ratemap.Stats * CutoffsInfoContent.proj('info_content_99') & 'information_content > info_content_99'

        ##### MEC ######################################################################################################################
        entry_dict_mec = {
            **key,
            'mec_n_cells'      : len(cells_session_mec & parameter_dict_cell),
            'mec_ovc'          : len(ovcells & cells_session_mec & parameter_dict_cell),
            'mec_n_grid_95'    : len(gridcells95 & cells_session_mec & parameter_dict_cell),
            'mec_n_info_95'    : len(infocells95 & cells_session_mec & parameter_dict_cell),
            'mec_n_hd_95'      : len(hdcells95 & cells_session_mec & parameter_dict_cell),
            'mec_n_border_95'  : len(bordercells95 & cells_session_mec & parameter_dict_cell),
            'mec_n_bv_95'      : len(bvcells95 & cells_session_mec & parameter_dict_cell),
            'mec_n_grid_99'    : len(gridcells99 & cells_session_mec & parameter_dict_cell),
            'mec_n_info_99'    : len(infocells99 & cells_session_mec & parameter_dict_cell),
            'mec_n_hd_99'      : len(hdcells99 & cells_session_mec & parameter_dict_cell),
            'mec_n_border_99'  : len(bordercells99 & cells_session_mec & parameter_dict_cell),
            'mec_n_bv_99'      : len(bvcells99 & cells_session_mec & parameter_dict_cell),
        }

        ##### PAS #######################################################################################################################
        entry_dict_pas = {
            **key,
            'pas_n_cells'      : len(cells_session_pas & parameter_dict_cell),
            'pas_ovc'          : len(ovcells & cells_session_pas & parameter_dict_cell),
            'pas_n_grid_95'    : len(gridcells95 & cells_session_pas & parameter_dict_cell),
            'pas_n_info_95'    : len(infocells95 & cells_session_pas & parameter_dict_cell),
            'pas_n_hd_95'      : len(hdcells95 & cells_session_pas & parameter_dict_cell),
            'pas_n_border_95'  : len(bordercells95 & cells_session_pas & parameter_dict_cell),
            'pas_n_bv_95'      : len(bvcells95 & cells_session_pas & parameter_dict_cell),
            'pas_n_grid_99'    : len(gridcells99 & cells_session_pas & parameter_dict_cell),
            'pas_n_info_99'    : len(infocells99 & cells_session_pas & parameter_dict_cell),
            'pas_n_hd_99'      : len(hdcells99 & cells_session_pas & parameter_dict_cell),
            'pas_n_border_99'  : len(bordercells99 & cells_session_pas & parameter_dict_cell),
            'pas_n_bv_99'      : len(bvcells99 & cells_session_pas & parameter_dict_cell),
        }

        # Set non-OV session entries to None for _ovc
        # Check whether this session could have OVC entries
        if not (OVC.proj(is_ovc='is_ovc', session_name='base_session') & key):
            entry_dict_mec['mec_ovc'] = None
            entry_dict_pas['pas_ovc'] = None

        # Insert into table
        self.insert1({**key, **{'n_cells' : len(cell_keys_session)}})
        self.MEC.insert1(entry_dict_mec, ignore_extra_fields=True)
        self.PAS.insert1(entry_dict_pas, ignore_extra_fields=True)

        return


def main():
  #approximate less accurate pi
  pi_1 = 4 * (1 - (1 / 3) + (1 / 5) - (1 / 7) + (1 / 9) - (1 / 11))

  #approximate more accurate pi
  pi_2 = 4 * (1 - (1 / 3) + (1 / 5) - (1 / 7) + (1 / 9) - (1 / 11) + (1 / 13) - (1 / 15))

  #print results
  print("The two approximate values of pi are", pi_1, "and", pi_2)

main()


from kazoo.client import KazooClient
from os import environ
from json import loads

# Get broker on a host
def get_broker(zk, broker):
  try:
    if not zk:
      _zk = KazooClient(hosts=environ['ZK_LIST'], read_only=True)
      _zk.start()
    else:
      _zk = zk

    path="/brokers/ids/" + str(broker)
    if _zk.exists(path):
      n,znstat=_zk.get(path)
      bd = loads(n)
      h=bd["host"]
      p=bd["port"]
  except:
#    if not zk: _zk.stop()
    return

  if not zk: _zk.stop()

  if h:
    return str(h) + ':' + str(p)

  return

def listBrokers(zk):
  return zk.get_children("/brokers/ids", False)

def listTopics(zk):
  return zk.get_children("/brokers/topics")

def listPartitions(zk, topic):
  path = "/brokers/topics/" + topic + "/partitions"
  if zk.exists(path):
    return zk.get_children(path)
#  else:
#    raise KafkaException("Topic " + topic + " doesn't exist")

def getLeaderAddress(zk, topic, partitionId):
  path = "/brokers/topics/" + topic + "/partitions/" + str(partitionId) + "/state"
  if zk.exists(path):
    stateN = zk.get(path)
    stateDic = loads(stateN[0])
    leaderId = stateDic["leader"]
    return get_broker(zk, leaderId)
#  else:
#    raise KafkaException("Topic (" + topic + ") or partition (" + partitionId + ") doesn't exist")


def dfs(graph, start, visited):
    visited[start] = True
    print('-->', start)

    for node in graph[start]:
        if not visited[node]:
            dfs(graph, node, visited)
            print('-->', start)
    return visited

graph_struct = {'0': ['1', '2', '3'],
                '1': ['0', '2'],
                '2': ['0', '1', '4'],
                '3': ['0'],
                '4': ['2']}
visited = {key: False for key in graph_struct.keys()}

dfs(graph_struct, '0', visited)


from urllib.request import urlopen
from json import loads
from tweeter import format_top_artist_tweet, send
from time import sleep

def tweet_top_artist(link):

    top_artists = urlopen(link).read()
    top_artists_json = loads(top_artists.decode())

    artist = top_artists_json['topartists']['artist'][0]['name']
    plays = top_artists_json['topartists']['artist'][0]['playcount']

    tweet = format_top_artist_tweet(artist, plays)
    status = send(tweet)

    return status

def main():
    username = "user-name"
    api_key = "api-key"

    link = "http://ws.audioscrobbler.com/2.0/?method=user.getTopArtists&user=" + username + "&limit=" + str(1) + "&api_key=" + api_key + "&format=json"

    while True:
        result = tweet_top_artist(link)
        sleep(2419200)

if __name__ == '__main__':
    main()


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

from .fonc_type import *

#-----------------------------------------------------------------------------
#TYPE FONCTION
#-----------------------------------------------------------------------------

#Action ----------------------------------------------------------------------
def _set(TXTCRvars, TXTCRtmps, defauts, decode, value):
	item, *value = value.split(' = ')
	item = convert_variable(item, TXTCRvars, defauts, decode, convert_redirection=False)
	if value:
		value = convert_variable(' = '.join(value), TXTCRvars, defauts, decode)

		is_modifier = False
		for Tvars in TXTCRvars:
			if item in Tvars:
				Tvars[item] = value
				is_modifier = True

		if not is_modifier:
			TXTCRvars[-1][item] = value

def _add(TXTCRvars, TXTCRtmps, defauts, decode, value):
	item, *value = value.split(' + ')
	if item[0] in balises: item = decode(item)

	def verif_existe():
		for Tvars in TXTCRvars:
			if item in Tvars: return
		resulta = recup_default(defauts).get(item)
		if resulta: TXTCRvars[-1][item] = resulta
	verif_existe()

	if value:
		value = convert_variable(' + '.join(value), TXTCRvars, defauts, decode)
		for Tvars in TXTCRvars:
			if item in Tvars:
				if isinstance(Tvars[item], list):
					Tvars[item].append(value)
				elif isinstance(Tvars[item], (str, int, float)):
					Tvars[item] += value
				elif isinstance(Tvars[item], dict):
					key, value = value.split(' : ', 1)
					Tvars[item][key] = value
				elif isinstance(Tvars[item], tuple):
					Tvars[item] = Tvars[item] + (value,)

def _del(TXTCRvars, TXTCRtmps, defauts, decode, value):
	item, *value = value.split(' - ')
	item = convert_variable(item, TXTCRvars, defauts, decode, convert_redirection=False)
	if value:
		value = convert_variable(' - '.join(value), TXTCRvars, defauts, decode)
		for Tvars in TXTCRvars:
			if item in Tvars:
				if isinstance(Tvars[item], list):
					Tvars[item].remove(value)
				elif isinstance(Tvars[item], (int, float)):
					Tvars[item] -= value
				elif isinstance(Tvars[item], dict):
					del Tvars[item][value]
				elif isinstance(Tvars[item], tuple):
					Tvars[item] = Tvars[item] - (value,)
				elif isinstance(Tvars[item], str):
					Tvars[item].replace(value, '')
	else:
		for Tvars in TXTCRvars:
			if item in Tvars:
				del Tvars[item]

def _ped(TXTCRvars, TXTCRtmps, defauts, decode, value):
	item, *texte = value.split(' ? ')
	texte = input(' ? '.join(texte))
	item = convert_variable(item, TXTCRvars, defauts, decode, convert_redirection=False)
	TXTCRvars[-1][item] = decode(texte)

def _get(TXTCRvars, TXTCRtmps, defauts, decode, value):
	return convert_variable(value, TXTCRvars, defauts, decode)

def _aff(TXTCRvars, TXTCRtmps, defauts, decode, value):
	if value[0] == '"' and value[-1] == '"':
		value = value[1:-1]
	print(replace_variables(value, TXTCRvars, defauts, decode))

def _typ(TXTCRvars, TXTCRtmps, defauts, decode, value):
	*item, variable = value.split(' = ')
	variable = convert_variable(variable, TXTCRvars, defauts, decode)
	vtype = gettype(variable)
	if not item:
		return vtype
	else:
		TXTCRvars[-1][decode(' = '.join(item))] = vtype

func_actions = {
	#Modification de variable
	'set': lambda **ops: _set(**ops),
	'get': lambda **ops: _get(**ops),
	#Intéraction avec l'utiisateur
	'aff': lambda **ops: _aff(**ops),
	'ped': lambda **ops: _ped(**ops),
	#Ajout/suppresion
	'add': lambda **ops: _add(**ops),
	'del': lambda **ops: _del(**ops),
	#Utilisataire
	'typ': lambda **ops: _typ(**ops)
}

def _len_comparaison(var1, symb, var2):

	if isinstance(var1, (int, float)):
		if isinstance(var2, (str, bytes, tuple, list, dict)):
			return eval('%s %s %s'%(var1, symb, len(var2)))
	if isinstance(var1, (str, bytes, tuple, list, dict)):
		if isinstance(var2, (int, float)):
			return eval('%s %s %s'%(len(var1), symb, var2))

	if symb == '>': return var1 > var2
	if symb == '>=': return var1 >= var2
	if symb == '<': return var1 < var2
	if symb == '<=': return var1 <= var2
	if symb == '==': return var1 == var2

def _verif_type(variable):
	if isinstance(variable, (int, float)) and not isinstance(variable, bool):
		if str(variable)[0] == '-':
			return 'negatif'
		return 'positif'
	return type(variable).__name__.replace('TXTCR', '')

def _type_in(var1, var2):
	variable = var1
	conteneur = var2
	nom_type = _verif_type(variable)
	for partie in conteneur:
		type_partie = _verif_type(partie)
		if nom_type == type_partie:
			return True
	return False

def _objet_in(var1, var2):
	variable = var1
	conteneur = var2
	for partie in conteneur:
		if variable is partie:
			return True
	return False

func_conditions = {
	'>': lambda var1, var2: _len_comparaison(var1, '>', var2),
	'=>': lambda var1, var2: _len_comparaison(var1, '>=', var2),
	'<': lambda var1, var2: _len_comparaison(var1, '<', var2),
	'<=': lambda var1, var2: _len_comparaison(var1, '<=', var2),
	'=': lambda var1, var2: _verif_type(var1) == _verif_type(var2),
	'==': lambda var1, var2: _len_comparaison(var1, '==', var2),
	'===': lambda var1, var2: var1 is var2,
	'in': lambda var1, var2: _type_in(var1, var2),
	'inn': lambda var1, var2: var1 in var2,
	'innn': lambda var1, var2: _objet_in(var1, var2),
	'&': lambda var1, var2: var1 and var2,
	'|': lambda var1, var2: var1 or var2,
	'||': lambda var1, var2: (var1 or var2) != (var1 and var2),
}

#Condition + Action --------------------------------------------------------------------
class Condition:

	def __init__(ss, fonc, vars_tempo, variables, defauts, decode, acts_true, acts_false):
		ss.fonction = fonc
		ss.vars_tempo = vars_tempo
		ss.variables = variables
		ss.defauts = defauts
		ss.decode = decode
		ss.acts_true  = acts_true
		ss.acts_false = acts_false

	def config_condition(ss, texte):
		valeur_actuel = []
		condition_actuel = []

		def ajout_valeur(valeur):
			if valeur: condition_actuel.append(convert_variable(' '.join(valeur), ss.variables, ss.defauts, ss.decode))

		for partie in texte.split(' '):

			if partie.replace('!', '') in func_conditions:
				ajout_valeur(valeur_actuel)
				condition_actuel.append(partie)
				valeur_actuel = []

			elif partie or valeur_actuel: valeur_actuel.append(partie)

		ajout_valeur(valeur_actuel)
		return condition_actuel

	def decoupe_condition(ss, texte):
		liste_condition = []
		while 1:
			début, fin = recup_partie_parentese(texte)
			if fin:
				if début: liste_condition.extend(ss.config_condition(texte[:début]))
				liste_condition.append(ss.decoupe_condition(texte[début+1:fin]))
				texte = texte[fin+1:]
			else:
				liste_condition.extend(ss.config_condition(texte))
				break
		return liste_condition

	def get_values_and_symb(ss, liste):
		for nbr in range(0, len(liste)-1, 2):
			yield [nbr+2] + liste[nbr:nbr+3]

	def is_condition(ss, value):
		if isinstance(value, list) and len(value) >= 3 and value[1].replace('!', '') in func_conditions:
			return True

	def applique_condition(ss, liste_conditions):
		for place, value1, symb, value2 in ss.get_values_and_symb(liste_conditions):

			if ss.is_condition(value1):
				value1 = ss.applique_condition(value1)
			if ss.is_condition(value2):
				value2 = ss.applique_condition(value2)

			notnegation = True
			if symb[0] == '!': symb = symb[1:]; notnegation = False

			liste_conditions[place] = func_conditions.get(symb)(value1, value2) == notnegation

		return liste_conditions[-1]

	def analyse(ss):
		resultat = ss.applique_condition(ss.decoupe_condition(ss.fonction))

		if resultat == False and ss.acts_false:
			resultat = ss.action(ss.acts_false)

		elif resultat == True and ss.acts_true:
			resultat = ss.action(ss.acts_true)

		return resultat

	def action(ss, actions):
		retours = []

		def get_values_and_symb():
			for nbr in range(0, len(actions)-1, 2):
				yield actions[nbr:nbr+2]

		for symb, value in get_values_and_symb():
			func = func_actions.get(symb)
			if not func:
				raise Exception('Func')

			retour = func(value=value, TXTCRvars=ss.variables, TXTCRtmps=ss.vars_tempo, defauts=ss.defauts, decode=ss.decode)
			if retour != None:
				retours.append(retour)

		if not retours:
			return None
		elif len(retours) == 1:
			return retours[0]
		else:
			return tuple(retours)

#Fonction -------------------------------------------------------------
class DefCondition:

	def __init__(ss, vars_tempo, variables, defauts, decode):
		ss.vars_tempo = vars_tempo
		ss.variables = variables
		ss.defauts = defauts
		ss.decode = decode

	def decoupe_action(ss, texte):

		ifs = ['if1','if0']
		func_actions = []

		fin = 0
		mot = ''
		début = 0
		action = []
		actions = []
		in_texte = False
		echapement = False
		balise_fin = ''
		balises_texte = ["'", '"']

		for place, carac in enumerate(texte):

			if not in_texte and carac in balises_texte:
				in_texte = True
				balise_fin = carac

			elif not echapement and carac in [balise_fin] + [';']:
				in_texte = False

			elif carac in ['\\', '=']:
				echapement = True
				continue

			elif not echapement and not in_texte and carac == '>':

				mot = texte[début:place].strip()

				if mot in ifs:
					actions.append(action)
					action = []

				action.append(mot)
				début = place + 1

			echapement = False

		mot = texte[début:place].strip()
		if mot: action.append(mot)
		actions.append(action)

		return actions

	def recup_condition_acts(ss, actions):

		ifs = ['if0','if1']

		condition = None
		acts_false = None
		acts_true = None

		for action in ss.decoupe_action(actions):
			if action[0] not in ifs:
				condition = ' > '.join(action)
			elif action[0] == ifs[0]:
				acts_false = action[1:]
			elif action[0] == ifs[1]:
				acts_true = action[1:]

		#print(condition, acts_true, acts_false)

		return Condition(condition, ss.vars_tempo, ss.variables, ss.defauts, ss.decode, acts_true, acts_false)


n # Import py_entitymatching package
import py_entitymatching as em
import os
import pandas as pd
import numpy as np

import warnings
warnings.filterwarnings(action="ignore", module="scipy", message="^internal gelsd")

# Load the pre-labeled data
metacriticData = em.read_csv_metadata("data/metacritic.csv")
wikiData = em.read_csv_metadata("data/wikiData.csv")

# add ID column to each dataset
metacriticID = ["a" + str(num) for num in np.arange(1, len(metacriticData.index)+1)]
wikiID = ["b" + str(num) for num in np.arange(1, len(wikiData.index)+1)]

col_idx = 0
metacriticData.insert(loc = col_idx, column = 'ID', value = metacriticID)
wikiData.insert(loc = col_idx, column = 'ID', value = wikiID)
em.set_key(wikiData, 'ID')
em.set_key(metacriticData, 'ID')

#read in labeled samples
S = em.read_csv_metadata("candidates_sample.csv",
                         key='_id',
                         ltable=metacriticData, rtable=wikiData,
                         fk_ltable='ltable_ID', fk_rtable='rtable_ID')

# Split S into I an J
i_file = "I.csv"
j_file = "J.csv"
if not os.path.isfile(i_file): #so you don't delete your labels on accident
    IJ = em.split_train_test(S, train_proportion=0.5, random_state=0)
    I = IJ['train']
    J = IJ['test']
    I.to_csv(i_file,sep=",")
    J.to_csv(j_file,sep=",")
    print("Split samples into I and J")
else:
    I = em.read_csv_metadata(i_file,key="_id",ltable=metacriticData,rtable=wikiData,fk_ltable="ltable_ID",fk_rtable="rtable_ID")
    J = em.read_csv_metadata(j_file,key="_id",ltable=metacriticData,rtable=wikiData,fk_ltable="ltable_ID",fk_rtable="rtable_ID")
    print("Reading I and J from files")
print(len(I))
print(len(J))

# Generate a set of features
F = em.get_features_for_matching(metacriticData, wikiData, validate_inferred_attr_types=False)

# Convert the I into a set of feature vectors using F
H = em.extract_feature_vecs(I,
                            feature_table=F,
                            attrs_after='label',
                            show_progress=False)

# create learners
random_state = 0

dt = em.DTMatcher(name='DecisionTree', random_state=random_state)
rf = em.RFMatcher(name='RF', random_state=random_state)
svm = em.SVMMatcher(name='SVM', random_state=random_state)
ln = em.LinRegMatcher(name='LinReg')
lg = em.LogRegMatcher(name='LogReg', random_state=random_state)
nb = em.NBMatcher(name = 'NaiveBayes')

# Impute feature vectors with the mean of the column values.
H = em.impute_table(H,
                exclude_attrs=['_id', 'ltable_ID', 'rtable_ID', 'label'],
                strategy='mean')

#initial results
result = em.select_matcher([dt, rf, svm, ln, lg, nb], table=H,
        exclude_attrs=['_id', 'ltable_ID', 'rtable_ID', 'label'],
        k=5,
        target_attr='label', metric_to_select_matcher='f1', random_state=0)
result['cv_stats']

classifiers = np.array([dt, rf, svm, ln, lg, nb])

# Convert J into a set of feature vectors using F
L = em.extract_feature_vecs(J, feature_table=F,
                            attrs_after='label', show_progress=False)

# Impute feature vectors with the mean of the column values
L = em.impute_table(L,
                exclude_attrs=['_id', 'ltable_ID', 'rtable_ID', 'label'],
                strategy='mean')

for c in classifiers:
    # Train using feature vectors from I
    c.fit(table=H, exclude_attrs=['_id', 'ltable_ID', 'rtable_ID', 'label'], target_attr='label')

    # Predict on L
    predictions = c.predict(table=L, exclude_attrs=['_id', 'ltable_ID', 'rtable_ID', 'label'],
                             append=True, target_attr='predicted', inplace=False)

    predictions[['_id', 'ltable_ID', 'rtable_ID', 'predicted','label']].head()

    # Evaluate the predictions
    print(c.name)
    eval_result = em.eval_matches(predictions, 'label', 'predicted')
    em.print_eval_summary(eval_result)
    print()

### STAGE 4 ###
### FUNCTIONS###

# create function to remove char a/b in list of matches
def removeChar(string):
    return string[1:]

# use this function to edit metacritic genre column since Pop/Rock should be pop rock
def replaceBackslash(string):
    # make sure string is string
    string = str(string)

    # if empty, return the string
    if not string:
        return string

    # otherwise, replace backslash with a space
    return string.replace("/",' ')

def merge_genre_or_label_or_producer(v1, v2):
    # make sure string is string then set to lower case
    v1 = str.lower(str(v1))
    v2 = str.lower(str(v2))

    # remove brackets and single quotes for now
    v1 = v1[v1.find("[")+1:v1.find("]")].replace("'",'').replace("-",' ')
    v2 = v2[v2.find("[")+1:v2.find("]")].replace("'",'').replace("-",' ')

    # in case of multiple values for v1 or v2, we need to split by comma
    v1 = [x.strip() for x in v1.split(',')]
    v2 = [x.strip() for x in v2.split(',')]

    # combine v1 and v2
    v1.extend(v2)

    # remove anything in common
    v1 = list(set(v1))

    # remove any empty string
    v1 = [i for i in v1 if i]

    return v1

def merge_releaseDate(v1, v2):
    # always take wiki release date
    return v2

def merge_metaScore(v1, v2):
    # wiki has no meta score, so return metacritic score (v1)
    return v1

# this function takes two tuples and merges them
def mergeTuple(t1, t2):
    # create empty combined tuple (to add to table E)
    t = []
    for i in np.arange(1,len(t1)):
        if wikiData.columns[i] in ['Album', 'Artist']:
            if len(t1[i]) >= len(t2[i]):
                currT = t1[i]
                t.append(currT)
            else:
                currT = t2[i]
                t.append(currT)
        elif wikiData.columns[i] in ['Genre', 'Label', 'Producer']:
            currT = merge_genre_or_label_or_producer(t1[i], t2[i])
            t.append(currT)
        elif wikiData.columns[i] in ['Release Date']:
            currT = merge_releaseDate(t1[i], t2[i])
            t.append(currT)
        else:
            currT = merge_metaScore(t1[i], t2[i])
            t.append(currT)
    return t

### CODE ###
# read in complete candidate list
C = em.read_csv_metadata("candidates.csv",
                         key='_id',
                         ltable=metacriticData, rtable=wikiData,
                         fk_ltable='ltable_ID', fk_rtable='rtable_ID')

# Convert candidate C into a set of feature vectors using F
K = em.extract_feature_vecs(C,
                            feature_table=F,
                            show_progress=False)
# Impute feature vectors with the mean of the column values
K = em.impute_table(K,
                exclude_attrs=['_id', 'ltable_ID', 'rtable_ID'],
                strategy='mean')

# take best classifier (linear regression) and output predictions (i.e. matches)
predictions = ln.predict(table=K, exclude_attrs=['_id', 'ltable_ID', 'rtable_ID'],
                        append=True, target_attr='predicted', inplace=False)

# save set of matches between two tables
matches = predictions.loc[predictions['predicted'] == 1]
matches = C.loc[matches.loc[:,'_id'],:]
matches.to_csv("matches.csv", index = False)

# get a list of the indices to match
metacriticIndexMatches = [int(removeChar(s))-1 for s in list(predictions.loc[predictions['predicted'] == 1,'ltable_ID'])]
wikiIndexMatches = [int(removeChar(s))-1 for s in list(predictions.loc[predictions['predicted'] == 1,'rtable_ID'])]

# remove "/" in Genre column of metacriticData
metacriticData['Genre'] = metacriticData['Genre'].apply(lambda x: replaceBackslash(x))

# replace all NaNs with an empty string since that'll be easier for me to work with
metacriticData = metacriticData.replace(np.nan, '', regex = True)
wikiData = wikiData.replace(np.nan, '', regex = True)

# initialize table E that merges metacriticData and wikiData
E = pd.DataFrame(columns = metacriticData.columns[1:])

# add all matches
for i in np.arange(0,len(wikiIndexMatches)):
    # merge tuples in wikiIndexMatches and metacriticIndexMatches (exclude ID column)
    metaTuple = metacriticData.loc[metacriticIndexMatches[i]][0:]
    wikiTuple = wikiData.loc[wikiIndexMatches[i]][0:]
    mergedT = mergeTuple(metaTuple, wikiTuple)

    # add to E
    E.loc[i] = mergedT

# add everything NOT in metacriticIndexMatches and wikiIndexMatches
metaIndices = list(range(np.shape(metacriticData)[0]))
wikiIndices = list(range(np.shape(wikiData)[0]))
leftoverMetaIndices = [i for i in metaIndices if i not in metacriticIndexMatches]
leftoverWikiIndices = [i for i in wikiIndices if i not in wikiIndexMatches]

# subset data based on leftover indices (exclude ID column)
subsetMetaData = metacriticData.iloc[leftoverMetaIndices, 1:]
subsetWikiData = wikiData.iloc[leftoverWikiIndices, 1:]

# lower case genre, label, and producer cell values
toLowerCase = ['Genre', 'Label', 'Producer']
for col in toLowerCase:
    subsetMetaData[col] = subsetMetaData[col].str.lower().str.replace("-",' ')
    subsetWikiData[col] = subsetWikiData[col].str.lower().str.replace("-",' ')

# combine with E
E = pd.concat([E, subsetMetaData], axis = 0)
E = pd.concat([E, subsetWikiData], axis = 0)

# change index of E
E.index = range(np.shape(E)[0])

# replace empty strings and lists with NaN
E.replace(r'^\s*$', np.nan, regex=True, inplace = True)

#convert all elements to string first, and then compare with '[]'. Finally use mask function to mark '[]' as na
# then save to "E.csv"
E.mask(E.applymap(str).eq('[]')).to_csv("E.csv", index = False)


from pathlib import Path
import typing as T
import numpy as np
import logging
import struct
from datetime import datetime, timedelta

LSP = 7

# NOT lru_cache
def get_simsize(fn: Path) -> T.Tuple[int, ...]:
    """
    get simulation dimensions from simsize.dat
    in the future, this would be in the .h5 HDF5 output.

    Parameters
    ----------
    fn: pathlib.Path
        filepath to simsize.dat

    Returns
    -------
    size: tuple of int, int, int
        3 integers telling simulation grid size
    """
    fn = Path(fn).expanduser()
    fsize = fn.stat().st_size
    if fsize == 12:
        lxs = struct.unpack("III", fn.open("rb").read(12))
    elif fsize == 8:
        lxs = struct.unpack("II", fn.open("rb").read(8))
    else:
        raise ValueError(f"{fn} is not expected 8 bytes (2-D) or 12 bytes (3-D) long")

    return lxs

def readgrid(fn: Path) -> T.Dict[str, np.ndarray]:
    """
    get simulation dimensions
    in the future, this would be in the .h5 HDF5 output.

    Parameters
    ----------
    fn: pathlib.Path
        filepath to simgrid.dat

    Returns
    -------
    grid: dict
        grid parameters
    """
    lxs = get_simsize(fn.parent / "simsize.dat")
    if len(lxs) == 2:
        return readgrid2(fn, lxs)
    elif len(lxs) == 3:
        return readgrid3(fn, lxs)
    else:
        raise ValueError("lxs must be 2-D or 3-D")

def readgrid2(fn: Path, lxs: T.Sequence[int]) -> T.Dict[str, np.ndarray]:
    """ for Efield """
    if not fn.is_file():
        raise FileNotFoundError(fn)

    read = np.fromfile
    grid: T.Dict[str, T.Any] = {"lx": lxs}
    with fn.open("r") as f:
        grid["mlon"] = read(f, np.float64, lxs[0])
        grid["mlat"] = read(f, np.float64, lxs[1])

    return grid

def readgrid3(fn: Path, lxs: T.Sequence[int]) -> T.Dict[str, np.ndarray]:

    lgridghost = (lxs[0] + 4) * (lxs[1] + 4) * (lxs[2] + 4)
    gridsizeghost = [lxs[0] + 4, lxs[1] + 4, lxs[2] + 4]

    grid: T.Dict[str, T.Any] = {"lx": lxs}

    if not fn.is_file():
        logging.error(f"{fn} grid file is not present. Will try to load rest of data.")
        return grid

    read = np.fromfile

    with fn.open("r") as f:
        for i in (1, 2, 3):
            grid[f"x{i}"] = read(f, np.float64, lxs[i - 1] + 4)
            grid[f"x{i}i"] = read(f, np.float64, lxs[i - 1] + 1)
            grid[f"dx{i}b"] = read(f, np.float64, lxs[i - 1] + 3)
            grid[f"dx{i}h"] = read(f, np.float64, lxs[i - 1])
        for i in (1, 2, 3):
            grid[f"h{i}"] = read(f, np.float64, lgridghost).reshape(gridsizeghost)
        L = [lxs[0] + 1, lxs[1], lxs[2]]
        for i in (1, 2, 3):
            grid[f"h{i}x1i"] = read(f, np.float64, np.prod(L)).reshape(L)
        L = [lxs[0], lxs[1] + 1, lxs[2]]
        for i in (1, 2, 3):
            grid[f"h{i}x2i"] = read(f, np.float64, np.prod(L)).reshape(L)
        L = [lxs[0], lxs[1], lxs[2] + 1]
        for i in (1, 2, 3):
            grid[f"h{i}x3i"] = read(f, np.float64, np.prod(L)).reshape(L)
        for i in (1, 2, 3):
            grid[f"gx{i}"] = read(f, np.float64, np.prod(lxs)).reshape(lxs)
        for k in ("alt", "glat", "glon", "Bmag"):
            grid[k] = read(f, np.float64, np.prod(lxs)).reshape(lxs)
        grid["Bincl"] = read(f, np.float64, lxs[1] * lxs[2]).reshape(lxs[1:])
        grid["nullpts"] = read(f, np.float64, np.prod(lxs)).reshape(lxs)
        if f.tell() == fn.stat().st_size:  # not EOF
            return grid

        L = [lxs[0], lxs[1], lxs[2], 3]
        for i in (1, 2, 3):
            grid[f"e{i}"] = read(f, np.float64, np.prod(L)).reshape(L)
        for k in ("er", "etheta", "ephi"):
            grid[k] = read(f, np.float64, np.prod(L)).reshape(L)
        for k in ("r", "theta", "phi"):
            grid[k] = read(f, np.float64, np.prod(lxs)).reshape(lxs)
        if f.tell() == fn.stat().st_size:  # not EOF
            return grid

        for k in ("x", "y", "z"):
            grid[k] = read(f, np.float64, np.prod(lxs)).reshape(lxs)

    return grid

def load_Efield(fn: Path) -> T.Dict[str, T.Any]:
    """
    load Efield_inputs files that contain input electric field in V/m
    """

    read = np.fromfile

    E: T.Dict[str, np.ndarray] = {}

    E["Nlon"], E["Nlat"] = get_simsize(fn.parent / "simsize.dat")

    assert E["Nlon"] > 0, "must have strictly positive number of longitude cells"
    assert E["Nlat"] > 0, "must have strictly positive number of latitude cells"

    lxs = (0, E["Nlon"], E["Nlat"])

    E.update(readgrid2(fn.parent / "simgrid.dat", (E["Nlon"], E["Nlat"])))

    assert ((E["mlat"] >= -90) & (E["mlat"] <= 90)).all(), f"impossible latitude, was file read correctly? {fn}"

    with fn.open("r") as f:
        """
        NOTE:
        this is mistakenly a float from Matlab
        to keep compatibility with old files, we left it as real64.
        New work should be using HDF5 instead of raw in any case.
        """
        E["flagdirich"] = int(read(f, np.float64, 1))
        for p in ("Exit", "Eyit", "Vminx1it", "Vmaxx1it"):
            E[p] = [("x2", "x3"), read2D(f, lxs)]
        for p in ("Vminx2ist", "Vmaxx2ist"):
            E[p] = [("x2",), read(f, np.float64, E["Nlat"])]
        for p in ("Vminx3ist", "Vmaxx3ist"):
            E[p] = [("x3",), read(f, np.float64, E["Nlon"])]
        filesize = fn.stat().st_size
        if f.tell() != filesize:
            logging.error(f"{fn} size {filesize} != file read position {f.tell()}")

    return E

def loadframe3d_curv(fn: Path, lxs: T.Sequence[int]) -> T.Dict[str, T.Any]:
    """
    end users should normally use loadframe() instead

    Parameters
    ----------
    fn: pathlib.Path
        filename of this timestep of simulation output
    lxs: list of int
        array dimension
    """

    #    grid = readgrid(fn.parent / "inputs/simgrid.dat")
    #    dat = xarray.Dataset(
    #        coords={"x1": grid["x1"][2:-2], "x2": grid["x2"][2:-2], "x3": grid["x3"][2:-2]}
    #    )

    dat: T.Dict[str, T.Any] = {}

    with fn.open("r") as f:
        dat["time"] = read_time(f)

        ns = read4D(f, LSP, lxs)
        dat["ne"] = (("x1", "x2", "x3"), ns[:, :, :, LSP - 1])

        vs1 = read4D(f, LSP, lxs)
        dat["v1"] = (("x1", "x2", "x3"), (ns[:, :, :, :6] * vs1[:, :, :, :6]).sum(axis=3) / dat["ne"][1])

        Ts = read4D(f, LSP, lxs)
        dat["Ti"] = (("x1", "x2", "x3"), (ns[:, :, :, :6] * Ts[:, :, :, :6]).sum(axis=3) / dat["ne"][1])
        dat["Te"] = (("x1", "x2", "x3"), Ts[:, :, :, LSP - 1].squeeze())

        for p in ("J1", "J2", "J3", "v2", "v3"):
            dat[p] = [("x1", "x2", "x3"), read3D(f, lxs)]

        dat["Phitop"] = [("x2", "x3"), read2D(f, lxs)]

    return dat

def loadframe3d_curvavg(fn: Path, lxs: T.Sequence[int]) -> T.Dict[str, T.Any]:
    """
    end users should normally use loadframe() instead

    Parameters
    ----------
    fn: pathlib.Path
        filename of this timestep of simulation output
    lxs: list of int
        array dimension
    """
    #    grid = readgrid(fn.parent / "inputs/simgrid.dat")
    #    dat = xarray.Dataset(
    #        coords={"x1": grid["x1"][2:-2], "x2": grid["x2"][2:-2], "x3": grid["x3"][2:-2]}
    #    )
    dat: T.Dict[str, T.Any] = {}

    with fn.open("r") as f:
        dat["time"] = read_time(f)

        for p in ("ne", "v1", "Ti", "Te", "J1", "J2", "J3", "v2", "v3"):
            dat[p] = [("x1", "x2", "x3"), read3D(f, lxs)]

        dat["Phitop"] = [("x2", "x3"), read2D(f, lxs)]

    return dat

def read4D(f, lsp: int, lxs: T.Sequence[int]) -> np.ndarray:
    """
    end users should normally use laodframe() instead
    """
    if not len(lxs) == 3:
        raise ValueError(f"lxs must have 3 elements, you have lxs={lxs}")

    return np.fromfile(f, np.float64, np.prod(lxs) * lsp).reshape((*lxs, lsp), order="F")

def read3D(f, lxs: T.Sequence[int]) -> np.ndarray:
    """
    end users should normally use loadframe() instead
    """
    if not len(lxs) == 3:
        raise ValueError(f"lxs must have 3 elements, you have lxs={lxs}")

    return np.fromfile(f, np.float64, np.prod(lxs)).reshape(*lxs, order="F")

def read2D(f, lxs: T.Sequence[int]) -> np.ndarray:
    """
    end users should normally use laodframe() instead
    """
    if not len(lxs) == 3:
        raise ValueError(f"lxs must have 3 elements, you have lxs={lxs}")

    return np.fromfile(f, np.float64, np.prod(lxs[1:])).reshape(*lxs[1:], order="F")

def loadglow_aurmap(f, lxs: T.Sequence[int], lwave: int) -> T.Dict[str, T.Any]:
    """
    read the auroral output from GLOW
    """
    if not len(lxs) == 3:
        raise ValueError(f"lxs must have 3 elements, you have lxs={lxs}")
    raw = np.fromfile(f, np.float64, np.prod(lxs[1:]) * lwave).reshape(np.prod(lxs[1:]) * lwave, order="F")
    return {"rayleighs": [("wavelength", "x2", "x3"), raw]}

def read_time(f) -> datetime:
    t = np.fromfile(f, np.float64, 4)
    return datetime(int(t[0]), int(t[1]), int(t[2])) + timedelta(hours=t[3])


import numpy as np
import matplotlib.pyplot as plt

class Blasius :
    def __init__(self, end, stepsize) :
        self.init = 0
        self.end = end
        self.h = stepsize
        self.x = np.arange(self.init, self.end + self.h, self.h)

    def solve(self):
        #f'' = z로 두면 dz/dx = -2 * z * y (f''' = -2 f'' f)
        self.y = np.zeros(self.x.shape)
        self.y1 = np.zeros(self.x.shape)
        self.y2 = np.zeros(self.x.shape)
        self.y3 = np.zeros(self.x.shape)
        self.y[0] = 0
        self.y1[0] = 0
        randominit = np.arange(0, 2, 0.001)
        print('''Searching the value of f''(0)''')
        for init in randominit :
            self.y2[0] = init
            for i in range(len(self.x) - 1) :
                k1 = -2 * self.y[i] * self.y2[i]
                k2 = -2 * (self.y[i] + self.y1[i] * self.h / 2) * (self.y2[i] + k1 * self.h / 2)
                k3 = -2 * (self.y[i] + self.y1[i] * self.h / 2) * (self.y2[i] + k2 * self.h / 2)
                k4 = -2 * (self.y[i] + self.y1[i] * self.h) * (self.y2[i] + k3 * self.h)
                self.y2[i+1] = self.y2[i] + self.h * (k1 + 2*k2 + 2*k3 + k4) / 6
                self.y1[i+1] = self.y1[i] + self.h * self.y2[i]
                self.y[i+1] = self.y[i] + self.h * self.y1[i]
            if 0.999 < abs(self.y1[-1]) < 1.001 :
                print("""Proper initial Value for f''(0) = {} (f'(inf) = {})"""
                      .format(init, self.y1[-1]))
                break
        print('''       x     /      f(x)     /      f'(x)     ''')
        for i in range(len(self.x)) :
            print('''    {}    /     {}     /    {}    '''.
                  format(round(self.x[i], 5), round(self.y[i], 5), round(self.y1[i], 5)))
        # x = 0 부터 x = 2.5까지 f'(x) ** 2의 적분값을 구하기 위한 수치적분
        int = 0
        for i in range(len(self.x)) :
            if self.x[i] > 2.5 :
                break
            int += self.h * self.y1[i]**2
        print('int value = {}'.format(int))

        plt.title('Blasius Equation using Shooting Method and RK4')
        plt.plot(self.x, self.y, 'blue', label = 'f(x)')
        plt.plot(self.x, self.y1, 'red', label = '''f'(x)''')
        plt.plot(self.x, self.y2, 'green', label = '''f''(x)''')
        plt.legend()
        plt.show()

Blasius(6, 0.01).solve()

Blasius Equation/1. Blasius Equation(RK4).py


# -*- coding: utf-8 -*-
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
from gtts import gTTS
from pygame import mixer
from pygame import time
from tempfile import TemporaryFile

class mGTTs:
    def __init__(self):
        pass

    def run(self, text):
        # pygame.init()
        mixer.init()
        sf = TemporaryFile()

        tts = gTTS(text=text, lang='ko')

        clock = time.Clock()
        tts.write_to_fp(sf)
        sf.seek(0)
        mixer.music.load(sf)
        mixer.music.play()

        while mixer.music.get_busy()==True:
            clock.tick(1000)
            print( mixer.music.get_busy())

    def stop(self):
        mixer.music.stop()


from fatpages.models import Fatpage as mFatpage
from django.template import loader, RequestContext
from django.shortcuts import get_object_or_404
from django.http import HttpResponse, HttpResponseRedirect
from django.conf import settings
from django.core.xheaders import populate_xheaders
from django.utils.safestring import mark_safe

DEFAULT_TEMPLATE = 'Fatpages/default.html'

def Fatpage(request, url):
    """
    Flat page view.

    Models: `Fatpages.Fatpages`
    Templates: Uses the template defined by the ``template_name`` field,
        or `Fatpages/default.html` if template_name is not defined.
    Context:
        Fatpage
            `Fatpages.Fatpages` object
    """
    if not url.endswith('/') and settings.APPEND_SLASH:
        return HttpResponseRedirect("%s/" % request.path)
    if not url.startswith('/'):
        url = "/" + url
    f = get_object_or_404(mFatpage, url__exact=url)
    # If registration is required for accessing this page, and the user isn't
    # logged in, redirect to the login page.
    if f.registration_required and not request.user.is_authenticated():
        from django.contrib.auth.views import redirect_to_login
        return redirect_to_login(request.path)
    if f.template_name:
        t = loader.select_template((f.template_name, DEFAULT_TEMPLATE))
    else:
        t = loader.get_template(DEFAULT_TEMPLATE)

    # To avoid having to always use the "|safe" filter in Fatpage templates,
    # mark the title and content as already safe (since they are raw HTML
    # content in the first place).
    f.title = mark_safe(f.title)
    f.content = mark_safe(f.content)
    f.headmatter = mark_safe(f.headmatter)

    c = RequestContext(request, {
        'fatpage': f,
    })
    response = HttpResponse(t.render(c))
    populate_xheaders(request, response, mFatpage, f.id)
    return response


from myhdl import block, always_comb, TristateSignal

@block
def tristate(tri, tx, rx, en):
    """ tristate driver
    Ports:
        tri: tri-state signal
        rx: tri-state input
        tx: the signal to drive the tri-state
        en: enable the tri-state for driving

    taken with permission from
    https://gist.github.com/cfelton/e24ed00b78fa4331f602
    """
    assert isinstance(tri, TristateSignal)
    tri_driver = tri.driver()

    @always_comb
    def tri_in():
        rx.next = bool(tri)

    @always_comb
    def tri_out():
        if en:
            tri_driver.next = tx
        else:
            tri_driver.next = None

    return tri_in, tri_out


from datetime import datetime
from collections import defaultdict
from math import exp

from extreme_amount_question import HighestAmountQuestion
from utils import get_paged_data

def weight(photo):
    taken = datetime.strptime(photo['created_time'][:10], '%Y-%m-%d')
    days_elapsed = (datetime.now() - taken).days + 1
    if days_elapsed < 0:
        raise Exception('Picture is dated ahead of today')
    return int(10 * exp((-0.002) * days_elapsed))

class MostTaggedWithQuestion(HighestAmountQuestion):
    QUESTION_TEXT = \
            'Out of the following, who has %s been tagged most with recently?'

    @classmethod
    def gen(cls, self_data, friend_data):
        tags = defaultdict(int)
        for photo in get_paged_data(friend_data, 'photos'):
            for tag in get_paged_data(photo, 'tags'):
                if 'id' not in tag:
                    continue        # Ignore tags of non-fb objects
                tags[tag['name']] += weight(photo)
        del tags[friend_data['name']]
        return cls(tags)


#!/usr/bin/env python

import rospy
import tf
from nav_msgs.msg import Odometry as odom
from geometry_msgs.msg import Point, Twist, Vector3

THRESHOLD = 0.8
GOAL_POS = Point(4, 4, 0)
RATE = None

def check_if_goal(new_pos, goal_pos):
    if (
        goal_pos.x + THRESHOLD >= new_pos.x >= goal_pos.x - THRESHOLD and
        new_pos.y + THRESHOLD >= new_pos.y >= goal_pos.y - THRESHOLD
    ):
        return True
    else:
        return False

def drive_to_goal(new_pos, goal_pos):
    global RATE

    driver = rospy.Publisher('cmd_vel', Twist)

    pass
    # YOU NEED TO STUDY

def on_pose_change(msg):
    global RATE

    while not rospy.is_shutdown():
        new_pos = msg.pose.pose.position
        arrived = check_if_goal(new_pos, GOAL_POS)

        if not arrived:
            drive_to_goal(new_pos, GOAL_POS)
        else:
            rospy.loginfo('You have arrived at destination')
            break

        RATE.sleep()

def on_shutdown():
    rospy.loginfo('Exiting...')

if __name__ == '__main__':
    global RATE

    rospy.init_node('driver')
    rospy.on_shutdown(on_shutdown)
    RATE = rospy.Rate(10)
    init_pose = rospy.wait_for_message('odom', odom).pose.pose.position

    rospy.Subscriber('odom', odom, on_pose_change)


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

import socket
import datetime
import threading
import subprocess
import BaseHTTPServer

def _lsof(port):
    try:  # try to find which process uses the socket
        process = subprocess.Popen(["sudo", "lsof", "-ti:{}".format(port)], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        stdout, stderr = process.communicate()

        if stderr and not stdout:
            return -1, stderr

        if stdout and not stderr:
            return 0, stdout

        return -1, ""

    except subprocess.CalledProcessError as error:
        return -1, error

class HttpServer:
    def __init__(self, host, port, request_handler):
        try:
            self.http_server = BaseHTTPServer.HTTPServer((host, port), request_handler)

        except socket.error as error:
            if 98 == error.errno: # address already in use error
                # try to find PID of the process that uses the socket
                return_code, pids = _lsof(port)
                if 0 == return_code:  # managed to find PID(s)
                    raise RuntimeError("Address is already used by process(es) with PID(s): {}".format(pids))
                else:  # failed to find PID(s)
                    raise RuntimeError(error.strerror)

            else:  # process other socket errors
                raise RuntimeError(error.strerror)

        except Exception as error:  # process other exceptions
            raise RuntimeError(error)

        self.serve_thread = threading.Thread(target=self.http_server.serve_forever)  # servE because the server.servE_forever
        self.serve_thread.daemon = True

        self.shutdown_thread = threading.Thread(target=self.http_server.shutdown)
        self.shutdown_thread.daemon = True

    def __del__(self):
        self.shutdown()

    def listen(self):
        if False in [hasattr(self, "serve_thread"), hasattr(self, "http_server")]:
            return

        if self.serve_thread.is_alive():
            return

        self.serve_thread.start()
        self.tic = datetime.datetime.now()
        host, port = self.http_server.server_address
        print("  HTTP-server started listening to {}:{}".format(host, port))

    def shutdown(self):
        if False in [hasattr(self, "serve_thread"), hasattr(self, "shutdown_thread")]:
            return

        if self.serve_thread.is_alive():

            if self.shutdown_thread.is_alive():
                return

            self.http_server.shutdown()
            self.shutdown_thread.start()
            self.shutdown_thread.join()
            self.toc = datetime.datetime.now()
            print("  Shutting down HTTP-server. Uptime: {}".format(self.toc-self.tic))


#!/usr/bin/python
import sys
import os
from os import path
import datetime
import pandas as pd
import openpyexcel
from openpyexcel import Workbook as wb, load_workbook as load_wb
from openpyexcel.styles import PatternFill, Border, Side, Alignment, Font
from openpyexcel.utils import get_column_letter
from ExcelStyle import ExcelStyle
import constants as constant
from constants import total_service as total_service

class CreateReadWriteInExcel:

    def __init__(self, excel_style, rows_in_db):
        self.rows_in_db = rows_in_db
        self.rows_in_db.sort(key=self.take_first)
        self.today = datetime.date.today()
        self.excel_style = excel_style

    @staticmethod
    def check_excel_sheet(output_excel_path, worksheet_name):
        workbook = pd.ExcelWriter(output_excel_path, engine='xlsxwriter')
        workbook.save()
        wb = load_wb(output_excel_path)
        ws = wb.worksheets[0]
        ws.title = worksheet_name
        wb.save(output_excel_path)

    @staticmethod
    def check_directory(dir_folder):
        os.makedirs(dir_folder)

    @staticmethod
    def take_first(elem):
        return elem[0]

    @staticmethod
    def calculate_success_rate(ws, row, col):
        return (ws.cell(row=row - 1, column=col + 1).value / ws.cell(row=row - 2, column=col + 1).value) * 100

    @staticmethod
    def calculate_failure_rate(ws, row, col):
        return 100 - ws.cell(row=row-1, column=col+1).value

    def ws_header(self, ws, row, col):
        if ws.cell(row=row, column=col).value is None:
            ws.cell(row=row, column=col).value = constant.SERVICE
            self.excel_style.merge_cells(ws, row, row+1, col, col)
            self.excel_style.style(ws, row, row, col, col, 40, 'Arial', 10, '808080', "solid", 'center')
            col = col + 1
            backup_col = col
        else:
            col = col + 1
            backup_col = col

            while ws.cell(row=row,column=col).value is not None and ws.cell(row=row, column=col).value != self.today.strftime("%d-%b-%Y"):
                col = col+3
                backup_col = col

        ws.cell(row=row, column=col).value = self.today.strftime("%d-%b-%Y")
        self.excel_style.merge_cells(ws, row, row, col, col+2)
        self.excel_style.style(ws, row, row, col, col, 11.14, 'Arial', 10, '808080', "solid", "center")

        row = row + 1
        start_row = row
        start_col = col
        ws.cell(row=row, column=col).value = constant.total_req['total_request']

        col = col + 1
        ws.cell(row=row, column=col).value = constant.total_req['total_response']

        col = col + 1
        ws.cell(row=row, column=col).value = constant.total_req['s_rate']
        self.excel_style.style(ws, start_row, row, start_col, col, 11.14, 'Arial', 10, '808080', "solid", "center")

        row = row + 1

        self.put_value_in_db_from_excel(ws, row, backup_col)

    '''Used for fetching data from database and write to excel'''
    def put_value_in_db_from_excel(self, ws, row, backup_col):

        for list_value in self.rows_in_db:
            if list_value[1] is not None:
                total_service['request'] = total_service['request'] + list_value[1]
                total_service['response'] = total_service['response'] + list_value[2]
            col = 1
            next_value_in_list = 1
            for tuple_value in list_value:
                try:
                    if tuple_value:
                        if next_value_in_list == 2:
                            col = backup_col
                        next_value_in_list = next_value_in_list+1
                        if ws.cell(row=row, column=col).value is None:
                            ws.cell(row=row, column=col).value = tuple_value
                except Exception as e:
                    print(e)
                col = col+1
            if ws.cell(row=row, column=col-2).value is not None:
                ws.cell(row=row, column=col).value = (int(ws.cell(row=row, column=col-1).value)/int(ws.cell(row=row, column=col-2).value))*100
            row = row+1

        self.excel_style.border(ws, 'thin', "000000", 1, row, 1, ws.max_column)
        week = self.today.isocalendar()[1]
        if ws.title == "week-"+str(week):
            self.weekly_graph_values(ws, row, backup_col)

    '''Used for writing values to excel which is used for graph creation'''
    def weekly_graph_values(self, ws, row, backup_col):
        start_row = row
        start_col = 1
        ws.cell(row=row, column=1).value = constant.week_dict['total']
        col = backup_col
        ws.cell(row=row, column=col).value = total_service['request']
        col = col+1
        ws.cell(row=row, column=col).value = total_service['response']
        col = col+1
        ws.cell(row=row, column=col).value = (total_service['response']/total_service['request'])*100
        row = row+1
        col = 1

        ws.cell(row=row, column=col).value = constant.week_dict['weeklyTotalReq']
        if ws.cell(row=row,column=col+1).value is None:
            ws.cell(row=row, column=col+1).value = total_service['request']
        else:
            ws.cell(row=row, column=col+1).value = ws.cell(row=row, column=col+1).value + total_service['request']

        row = row+1
        ws.cell(row=row,column=col).value = constant.week_dict['weeklyTotalRes']
        if ws.cell(row=row,column=col+1).value is None:
            ws.cell(row=row, column=col+1).value = total_service['response']
        else:
            ws.cell(row=row, column=col+1).value = ws.cell(row=row, column=col+1).value + total_service['response']

        row = row+1
        ws.cell(row=row, column=col).value = constant.week_dict['weeklySuccessRate']
        success_rate = self.calculate_success_rate(ws, row, col)
        ws.cell(row=row, column=col+1).value = success_rate

        row = row+1
        ws.cell(row=row, column=col).value = constant.week_dict['weeklyFailureRatePer']
        failure_rate = self.calculate_failure_rate(ws, row, col)
        ws.cell(row=row, column=col+1).value = failure_rate

        self.excel_style.border(ws, 'thin', "000000", start_row, row, 1, ws.max_column)
        self.excel_style.style(ws, start_row, row, start_col, ws.max_column, 11.14, 'Arial', 10, '808080', "solid", "center")
        success_rate_row = row+15
        row = row+3
        start_row = row
        start_col = col
        if ws.cell(row=row, column=col).value != constant.week_dict['weeklyPerfReport']:
            ws.cell(row=row, column=col).value = constant.week_dict['weeklyPerfReport']
            self.excel_style.style(ws, start_row, start_row, col, col, 40, 'Arial', 10, '808080', "solid", 'center')
            self.excel_style.merge_cells(ws, row, row+2, col, col)
            col = col+2
            ws.cell(row=row, column=col).value = constant.TOTAL_REQUEST
            col = col+1
            ws.cell(row=row, column=col).value = constant.TOTAL_RESPONSE
            col = col+1
            ws.cell(row=row, column=col).value = constant.SUCCESS_RATE
            row = row+1
            col = 2
        else:
            row = row+1
            col = 2
            while ws.cell(row=row, column=col).value != self.today.strftime("%d-%b") and ws.cell(row = row, column=col).value is not None:
                row = row+1

        ws.cell(row=row, column=col).value = self.today.strftime("%d-%b")
        col = col+1
        ws.cell(row=row, column=col).value = total_service['request']
        col = col+1
        ws.cell(row=row, column=col).value = total_service['response']
        col = col+1
        ws.cell(row=row, column=col).value = (total_service['response']/total_service['request'])*100

        self.excel_style.border(ws, 'thin', "000000", start_row, row, 1, col)
        self.excel_style.style(ws, start_row, start_row, start_col, col, 11.14, 'Arial', 10, '808080', "solid", 'center')

        col = 1
        start_row = success_rate_row
        ws.cell(row=success_rate_row, column=col).value = constant.week_dict['weeklySuccessRate']
        self.excel_style.style(ws, success_rate_row, success_rate_row, col, col, 40, 'Arial', 10, '808080', "solid", 'center')
        self.excel_style.merge_cells(ws, success_rate_row, success_rate_row+1, col, col)
        col = col+1
        ws.cell(row=success_rate_row, column=col).value = constant.week_dict['weeklySuccessRatePer']
        ws.cell(row=success_rate_row, column=col+1).value = constant.week_dict['weeklyFailureRatePer']

        success_rate_row = success_rate_row+1
        ws.cell(row=success_rate_row, column=col).value = success_rate
        ws.cell(row=success_rate_row, column=col+1).value = failure_rate
        self.excel_style.border(ws, 'thin', "000000", start_row, success_rate_row, 1, col+1)
        self.excel_style.style(ws, start_row, start_row, col, col+1, 11.14, 'Arial', 10, '808080', "solid", 'center')

    '''Used for writing daily and weekly report to excel'''
    def write_in_excel(self):
        workbook_name = constant.WORKBOOK_NAME+'_'+str(self.today)
        self.create_excel(constant.DAILY_REPORT, workbook_name, self.today.strftime("%d-%b-%Y"))
        print("current directory : {}".format(os.getcwd()))
        daily_sheet = os.getcwd()+'/'+constant.DAILY_REPORT+'/'+workbook_name+'.xlsx'
        print("daily sheet : {}".format(daily_sheet))
        daily_wb = load_wb(daily_sheet)
        daily_ws = daily_wb.worksheets[0]
        daily_row = 1
        daily_col = 1

        self.ws_header(daily_ws, daily_row, daily_col)

        week = self.today.isocalendar()[1]
        worksheet_name = "week-"+str(week)
        workbook_name = "WeeklyReport_"+str(week)
        self.create_excel(constant.WEEKLY_REPORT, workbook_name, worksheet_name)

        weekly_sheet = os.getcwd()+'/'+constant.WEEKLY_REPORT+'/'+workbook_name+'.xlsx'
        print("weekly sheet : {}".format(weekly_sheet))
        weekly_wb = load_wb(weekly_sheet)
        weekly_ws = weekly_wb.worksheets[0]
        weekly_row = 1
        weekly_col = 1

        self.ws_header(weekly_ws, weekly_row, weekly_col)
        daily_wb.save(daily_sheet)
        weekly_wb.save(weekly_sheet)

    '''Creates excel output sheet to the destination path'''
    def create_excel(self, folder_name, workbook_name, worksheet_name):
        folder_path = os.getcwd()+'/'+folder_name
        if not os.path.exists(folder_path):
            self.check_directory(folder_path)
        output_excel_path = folder_path+'/'+workbook_name+'.xlsx'
        if not os.path.isfile(output_excel_path):
            self.check_excel_sheet(output_excel_path, worksheet_name)